Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

block: Move SECTOR_SIZE and SECTOR_SHIFT definitions into <linux/blkdev.h>

It happens often while I'm preparing a patch for a block driver that
I'm wondering: is a definition of SECTOR_SIZE and/or SECTOR_SHIFT
available for this driver? Do I have to introduce definitions of these
constants before I can use these constants? To avoid this confusion,
move the existing definitions of SECTOR_SIZE and SECTOR_SHIFT into the
<linux/blkdev.h> header file such that these become available for all
block drivers. Make the SECTOR_SIZE definition in the uapi msdos_fs.h
header file conditional to avoid that including that header file after
<linux/blkdev.h> causes the compiler to complain about a SECTOR_SIZE
redefinition.

Note: the SECTOR_SIZE / SECTOR_SHIFT / SECTOR_BITS definitions have
not been removed from uapi header files nor from NAND drivers in
which these constants are used for another purpose than converting
block layer offsets and sizes into a number of sectors.

Cc: David S. Miller <davem@davemloft.net>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Nitin Gupta <ngupta@vflare.org>
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Bart Van Assche <bart.vanassche@wdc.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Bart Van Assche and committed by
Jens Axboe
233bde21 ec6dcf63

+38 -41
-1
arch/xtensa/platforms/iss/simdisk.c
··· 21 21 #include <platform/simcall.h> 22 22 23 23 #define SIMDISK_MAJOR 240 24 - #define SECTOR_SHIFT 9 25 24 #define SIMDISK_MINORS 1 26 25 #define MAX_SIMDISK_COUNT 10 27 26
-1
drivers/block/brd.c
··· 24 24 25 25 #include <linux/uaccess.h> 26 26 27 - #define SECTOR_SHIFT 9 28 27 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) 29 28 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT) 30 29
-2
drivers/block/null_blk.c
··· 16 16 #include <linux/badblocks.h> 17 17 #include <linux/fault-inject.h> 18 18 19 - #define SECTOR_SHIFT 9 20 19 #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) 21 20 #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT) 22 - #define SECTOR_SIZE (1 << SECTOR_SHIFT) 23 21 #define SECTOR_MASK (PAGE_SECTORS - 1) 24 22 25 23 #define FREE_BATCH 16
-9
drivers/block/rbd.c
··· 51 51 #define RBD_DEBUG /* Activate rbd_assert() calls */ 52 52 53 53 /* 54 - * The basic unit of block I/O is a sector. It is interpreted in a 55 - * number of contexts in Linux (blk, bio, genhd), but the default is 56 - * universally 512 bytes. These symbols are just slightly more 57 - * meaningful than the bare numbers they represent. 58 - */ 59 - #define SECTOR_SHIFT 9 60 - #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) 61 - 62 - /* 63 54 * Increment the given counter and return its updated value. 64 55 * If the counter is already 0 it will not be incremented. 65 56 * If the counter is already at its maximum value returns
-1
drivers/block/zram/zram_drv.h
··· 37 37 38 38 /*-- End of configurable params */ 39 39 40 - #define SECTOR_SHIFT 9 41 40 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) 42 41 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) 43 42 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
+4 -4
drivers/ide/ide-cd.c
··· 712 712 struct request_queue *q = drive->queue; 713 713 int write = rq_data_dir(rq) == WRITE; 714 714 unsigned short sectors_per_frame = 715 - queue_logical_block_size(q) >> SECTOR_BITS; 715 + queue_logical_block_size(q) >> SECTOR_SHIFT; 716 716 717 717 ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, rq->cmd_flags: 0x%x, " 718 718 "secs_per_frame: %u", ··· 919 919 * end up being bogus. 920 920 */ 921 921 blocklen = be32_to_cpu(capbuf.blocklen); 922 - blocklen = (blocklen >> SECTOR_BITS) << SECTOR_BITS; 922 + blocklen = (blocklen >> SECTOR_SHIFT) << SECTOR_SHIFT; 923 923 switch (blocklen) { 924 924 case 512: 925 925 case 1024: ··· 935 935 } 936 936 937 937 *capacity = 1 + be32_to_cpu(capbuf.lba); 938 - *sectors_per_frame = blocklen >> SECTOR_BITS; 938 + *sectors_per_frame = blocklen >> SECTOR_SHIFT; 939 939 940 940 ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu", 941 941 *capacity, *sectors_per_frame); ··· 1012 1012 drive->probed_capacity = toc->capacity * sectors_per_frame; 1013 1013 1014 1014 blk_queue_logical_block_size(drive->queue, 1015 - sectors_per_frame << SECTOR_BITS); 1015 + sectors_per_frame << SECTOR_SHIFT); 1016 1016 1017 1017 /* first read just the header, so we know how long the TOC is */ 1018 1018 stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
+1 -5
drivers/ide/ide-cd.h
··· 21 21 22 22 /************************************************************************/ 23 23 24 - #define SECTOR_BITS 9 25 - #ifndef SECTOR_SIZE 26 - #define SECTOR_SIZE (1 << SECTOR_BITS) 27 - #endif 28 - #define SECTORS_PER_FRAME (CD_FRAMESIZE >> SECTOR_BITS) 24 + #define SECTORS_PER_FRAME (CD_FRAMESIZE >> SECTOR_SHIFT) 29 25 #define SECTOR_BUFFER_SIZE (CD_FRAMESIZE * 32) 30 26 31 27 /* Capabilities Page size including 8 bytes of Mode Page Header */
-1
drivers/nvdimm/nd.h
··· 29 29 * BTT instance 30 30 */ 31 31 ND_MAX_LANES = 256, 32 - SECTOR_SHIFT = 9, 33 32 INT_LBASIZE_ALIGNMENT = 64, 34 33 NVDIMM_IO_ATOMIC = 1, 35 34 };
-3
drivers/scsi/gdth.h
··· 178 178 #define MSG_SIZE 34 /* size of message structure */ 179 179 #define MSG_REQUEST 0 /* async. event: message */ 180 180 181 - /* cacheservice defines */ 182 - #define SECTOR_SIZE 0x200 /* always 512 bytes per sec. */ 183 - 184 181 /* DPMEM constants */ 185 182 #define DPMEM_MAGIC 0xC0FFEE11 186 183 #define IC_HEADER_BYTES 48
+31 -11
include/linux/blkdev.h
··· 1022 1022 } 1023 1023 1024 1024 /* 1025 + * The basic unit of block I/O is a sector. It is used in a number of contexts 1026 + * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9 1027 + * bytes. Variables of type sector_t represent an offset or size that is a 1028 + * multiple of 512 bytes. Hence these two constants. 1029 + */ 1030 + #ifndef SECTOR_SHIFT 1031 + #define SECTOR_SHIFT 9 1032 + #endif 1033 + #ifndef SECTOR_SIZE 1034 + #define SECTOR_SIZE (1 << SECTOR_SHIFT) 1035 + #endif 1036 + 1037 + /* 1025 1038 * blk_rq_pos() : the current sector 1026 1039 * blk_rq_bytes() : bytes left in the entire request 1027 1040 * blk_rq_cur_bytes() : bytes left in the current segment ··· 1061 1048 1062 1049 static inline unsigned int blk_rq_sectors(const struct request *rq) 1063 1050 { 1064 - return blk_rq_bytes(rq) >> 9; 1051 + return blk_rq_bytes(rq) >> SECTOR_SHIFT; 1065 1052 } 1066 1053 1067 1054 static inline unsigned int blk_rq_cur_sectors(const struct request *rq) 1068 1055 { 1069 - return blk_rq_cur_bytes(rq) >> 9; 1056 + return blk_rq_cur_bytes(rq) >> SECTOR_SHIFT; 1070 1057 } 1071 1058 1072 1059 static inline unsigned int blk_rq_zone_no(struct request *rq) ··· 1096 1083 int op) 1097 1084 { 1098 1085 if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)) 1099 - return min(q->limits.max_discard_sectors, UINT_MAX >> 9); 1086 + return min(q->limits.max_discard_sectors, 1087 + UINT_MAX >> SECTOR_SHIFT); 1100 1088 1101 1089 if (unlikely(op == REQ_OP_WRITE_SAME)) 1102 1090 return q->limits.max_write_same_sectors; ··· 1409 1395 static inline int sb_issue_discard(struct super_block *sb, sector_t block, 1410 1396 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags) 1411 1397 { 1412 - return blkdev_issue_discard(sb->s_bdev, block << (sb->s_blocksize_bits - 9), 1413 - nr_blocks << (sb->s_blocksize_bits - 9), 1398 + return blkdev_issue_discard(sb->s_bdev, 1399 + block << (sb->s_blocksize_bits - 1400 + SECTOR_SHIFT), 1401 + nr_blocks << (sb->s_blocksize_bits - 1402 + SECTOR_SHIFT), 1414 1403 gfp_mask, flags); 1415 1404 } 1416 1405 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block, 1417 1406 sector_t nr_blocks, gfp_t gfp_mask) 1418 1407 { 1419 1408 return blkdev_issue_zeroout(sb->s_bdev, 1420 - block << (sb->s_blocksize_bits - 9), 1421 - nr_blocks << (sb->s_blocksize_bits - 9), 1409 + block << (sb->s_blocksize_bits - 1410 + SECTOR_SHIFT), 1411 + nr_blocks << (sb->s_blocksize_bits - 1412 + SECTOR_SHIFT), 1422 1413 gfp_mask, 0); 1423 1414 } 1424 1415 ··· 1530 1511 static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector) 1531 1512 { 1532 1513 unsigned int granularity = max(lim->physical_block_size, lim->io_min); 1533 - unsigned int alignment = sector_div(sector, granularity >> 9) << 9; 1514 + unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT) 1515 + << SECTOR_SHIFT; 1534 1516 1535 1517 return (granularity + lim->alignment_offset - alignment) % granularity; 1536 1518 } ··· 1565 1545 return 0; 1566 1546 1567 1547 /* Why are these in bytes, not sectors? */ 1568 - alignment = lim->discard_alignment >> 9; 1569 - granularity = lim->discard_granularity >> 9; 1548 + alignment = lim->discard_alignment >> SECTOR_SHIFT; 1549 + granularity = lim->discard_granularity >> SECTOR_SHIFT; 1570 1550 if (!granularity) 1571 1551 return 0; 1572 1552 ··· 1577 1557 offset = (granularity + alignment - offset) % granularity; 1578 1558 1579 1559 /* Turn it back into bytes, gaah */ 1580 - return offset << 9; 1560 + return offset << SECTOR_SHIFT; 1581 1561 } 1582 1562 1583 1563 static inline int bdev_discard_alignment(struct block_device *bdev)
-2
include/linux/device-mapper.h
··· 542 542 #define DMEMIT(x...) sz += ((sz >= maxlen) ? \ 543 543 0 : scnprintf(result + sz, maxlen - sz, x)) 544 544 545 - #define SECTOR_SHIFT 9 546 - 547 545 /* 548 546 * Definitions of return values from target end_io function. 549 547 */
-1
include/linux/ide.h
··· 165 165 */ 166 166 #define PARTN_BITS 6 /* number of minor dev bits for partitions */ 167 167 #define MAX_DRIVES 2 /* per interface; 2 assumed by lots of code */ 168 - #define SECTOR_SIZE 512 169 168 170 169 /* 171 170 * Timeouts for various operations:
+2
include/uapi/linux/msdos_fs.h
··· 10 10 * The MS-DOS filesystem constants/structures 11 11 */ 12 12 13 + #ifndef SECTOR_SIZE 13 14 #define SECTOR_SIZE 512 /* sector size (bytes) */ 15 + #endif 14 16 #define SECTOR_BITS 9 /* log2(SECTOR_SIZE) */ 15 17 #define MSDOS_DPB (MSDOS_DPS) /* dir entries per block */ 16 18 #define MSDOS_DPB_BITS 4 /* log2(MSDOS_DPB) */