blkdev.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. #ifndef _LINUX_BLKDEV_H
  2. #define _LINUX_BLKDEV_H
  3. #ifdef CONFIG_BLOCK
  4. #include <linux/sched.h>
  5. #include <linux/major.h>
  6. #include <linux/genhd.h>
  7. #include <linux/list.h>
  8. #include <linux/timer.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/backing-dev.h>
  12. #include <linux/wait.h>
  13. #include <linux/mempool.h>
  14. #include <linux/bio.h>
  15. #include <linux/module.h>
  16. #include <linux/stringify.h>
  17. #include <linux/gfp.h>
  18. #include <linux/bsg.h>
  19. #include <linux/smp.h>
  20. #include <asm/scatterlist.h>
  21. struct scsi_ioctl_command;
  22. struct request_queue;
  23. struct elevator_queue;
  24. struct request_pm_state;
  25. struct blk_trace;
  26. struct request;
  27. struct sg_io_hdr;
  28. #define BLKDEV_MIN_RQ 4
  29. #define BLKDEV_MAX_RQ 128 /* Default maximum */
  30. struct request;
  31. typedef void (rq_end_io_fn)(struct request *, int);
  32. struct request_list {
  33. /*
  34. * count[], starved[], and wait[] are indexed by
  35. * BLK_RW_SYNC/BLK_RW_ASYNC
  36. */
  37. int count[2];
  38. int starved[2];
  39. int elvpriv;
  40. mempool_t *rq_pool;
  41. wait_queue_head_t wait[2];
  42. };
  43. /*
  44. * request command types
  45. */
  46. enum rq_cmd_type_bits {
  47. REQ_TYPE_FS = 1, /* fs request */
  48. REQ_TYPE_BLOCK_PC, /* scsi command */
  49. REQ_TYPE_SENSE, /* sense request */
  50. REQ_TYPE_PM_SUSPEND, /* suspend request */
  51. REQ_TYPE_PM_RESUME, /* resume request */
  52. REQ_TYPE_PM_SHUTDOWN, /* shutdown request */
  53. REQ_TYPE_SPECIAL, /* driver defined type */
  54. REQ_TYPE_LINUX_BLOCK, /* generic block layer message */
  55. /*
  56. * for ATA/ATAPI devices. this really doesn't belong here, ide should
  57. * use REQ_TYPE_SPECIAL and use rq->cmd[0] with the range of driver
  58. * private REQ_LB opcodes to differentiate what type of request this is
  59. */
  60. REQ_TYPE_ATA_TASKFILE,
  61. REQ_TYPE_ATA_PC,
  62. };
  63. /*
  64. * For request of type REQ_TYPE_LINUX_BLOCK, rq->cmd[0] is the opcode being
  65. * sent down (similar to how REQ_TYPE_BLOCK_PC means that ->cmd[] holds a
  66. * SCSI cdb.
  67. *
  68. * 0x00 -> 0x3f are driver private, to be used for whatever purpose they need,
  69. * typically to differentiate REQ_TYPE_SPECIAL requests.
  70. *
  71. */
  72. enum {
  73. REQ_LB_OP_EJECT = 0x40, /* eject request */
  74. REQ_LB_OP_FLUSH = 0x41, /* flush request */
  75. REQ_LB_OP_DISCARD = 0x42, /* discard sectors */
  76. };
  77. /*
  78. * request type modified bits. first four bits match BIO_RW* bits, important
  79. */
  80. enum rq_flag_bits {
  81. __REQ_RW, /* not set, read. set, write */
  82. __REQ_FAILFAST_DEV, /* no driver retries of device errors */
  83. __REQ_FAILFAST_TRANSPORT, /* no driver retries of transport errors */
  84. __REQ_FAILFAST_DRIVER, /* no driver retries of driver errors */
  85. /* above flags must match BIO_RW_* */
  86. __REQ_DISCARD, /* request to discard sectors */
  87. __REQ_SORTED, /* elevator knows about this request */
  88. __REQ_SOFTBARRIER, /* may not be passed by ioscheduler */
  89. __REQ_HARDBARRIER, /* may not be passed by drive either */
  90. __REQ_FUA, /* forced unit access */
  91. __REQ_NOMERGE, /* don't touch this for merging */
  92. __REQ_STARTED, /* drive already may have started this one */
  93. __REQ_DONTPREP, /* don't call prep for this one */
  94. __REQ_QUEUED, /* uses queueing */
  95. __REQ_ELVPRIV, /* elevator private data attached */
  96. __REQ_FAILED, /* set if the request failed */
  97. __REQ_QUIET, /* don't worry about errors */
  98. __REQ_PREEMPT, /* set for "ide_preempt" requests */
  99. __REQ_ORDERED_COLOR, /* is before or after barrier */
  100. __REQ_RW_SYNC, /* request is sync (sync write or read) */
  101. __REQ_ALLOCED, /* request came from our alloc pool */
  102. __REQ_RW_META, /* metadata io request */
  103. __REQ_COPY_USER, /* contains copies of user pages */
  104. __REQ_INTEGRITY, /* integrity metadata has been remapped */
  105. __REQ_NOIDLE, /* Don't anticipate more IO after this one */
  106. __REQ_IO_STAT, /* account I/O stat */
  107. __REQ_MIXED_MERGE, /* merge of different types, fail separately */
  108. __REQ_NR_BITS, /* stops here */
  109. };
  110. #define REQ_RW (1 << __REQ_RW)
  111. #define REQ_FAILFAST_DEV (1 << __REQ_FAILFAST_DEV)
  112. #define REQ_FAILFAST_TRANSPORT (1 << __REQ_FAILFAST_TRANSPORT)
  113. #define REQ_FAILFAST_DRIVER (1 << __REQ_FAILFAST_DRIVER)
  114. #define REQ_DISCARD (1 << __REQ_DISCARD)
  115. #define REQ_SORTED (1 << __REQ_SORTED)
  116. #define REQ_SOFTBARRIER (1 << __REQ_SOFTBARRIER)
  117. #define REQ_HARDBARRIER (1 << __REQ_HARDBARRIER)
  118. #define REQ_FUA (1 << __REQ_FUA)
  119. #define REQ_NOMERGE (1 << __REQ_NOMERGE)
  120. #define REQ_STARTED (1 << __REQ_STARTED)
  121. #define REQ_DONTPREP (1 << __REQ_DONTPREP)
  122. #define REQ_QUEUED (1 << __REQ_QUEUED)
  123. #define REQ_ELVPRIV (1 << __REQ_ELVPRIV)
  124. #define REQ_FAILED (1 << __REQ_FAILED)
  125. #define REQ_QUIET (1 << __REQ_QUIET)
  126. #define REQ_PREEMPT (1 << __REQ_PREEMPT)
  127. #define REQ_ORDERED_COLOR (1 << __REQ_ORDERED_COLOR)
  128. #define REQ_RW_SYNC (1 << __REQ_RW_SYNC)
  129. #define REQ_ALLOCED (1 << __REQ_ALLOCED)
  130. #define REQ_RW_META (1 << __REQ_RW_META)
  131. #define REQ_COPY_USER (1 << __REQ_COPY_USER)
  132. #define REQ_INTEGRITY (1 << __REQ_INTEGRITY)
  133. #define REQ_NOIDLE (1 << __REQ_NOIDLE)
  134. #define REQ_IO_STAT (1 << __REQ_IO_STAT)
  135. #define REQ_MIXED_MERGE (1 << __REQ_MIXED_MERGE)
  136. #define REQ_FAILFAST_MASK (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | \
  137. REQ_FAILFAST_DRIVER)
  138. #define BLK_MAX_CDB 16
  139. /*
  140. * try to put the fields that are referenced together in the same cacheline.
  141. * if you modify this structure, be sure to check block/blk-core.c:rq_init()
  142. * as well!
  143. */
  144. struct request {
  145. struct list_head queuelist;
  146. struct call_single_data csd;
  147. int cpu;
  148. struct request_queue *q;
  149. unsigned int cmd_flags;
  150. enum rq_cmd_type_bits cmd_type;
  151. unsigned long atomic_flags;
  152. /* the following two fields are internal, NEVER access directly */
  153. sector_t __sector; /* sector cursor */
  154. unsigned int __data_len; /* total data len */
  155. struct bio *bio;
  156. struct bio *biotail;
  157. struct hlist_node hash; /* merge hash */
  158. /*
  159. * The rb_node is only used inside the io scheduler, requests
  160. * are pruned when moved to the dispatch queue. So let the
  161. * completion_data share space with the rb_node.
  162. */
  163. union {
  164. struct rb_node rb_node; /* sort/lookup */
  165. void *completion_data;
  166. };
  167. /*
  168. * two pointers are available for the IO schedulers, if they need
  169. * more they have to dynamically allocate it.
  170. */
  171. void *elevator_private;
  172. void *elevator_private2;
  173. struct gendisk *rq_disk;
  174. unsigned long start_time;
  175. /* Number of scatter-gather DMA addr+len pairs after
  176. * physical address coalescing is performed.
  177. */
  178. unsigned short nr_phys_segments;
  179. unsigned short ioprio;
  180. void *special; /* opaque pointer available for LLD use */
  181. char *buffer; /* kaddr of the current segment if available */
  182. int tag;
  183. int errors;
  184. int ref_count;
  185. /*
  186. * when request is used as a packet command carrier
  187. */
  188. unsigned short cmd_len;
  189. unsigned char __cmd[BLK_MAX_CDB];
  190. unsigned char *cmd;
  191. unsigned int extra_len; /* length of alignment and padding */
  192. unsigned int sense_len;
  193. unsigned int resid_len; /* residual count */
  194. void *sense;
  195. unsigned long deadline;
  196. struct list_head timeout_list;
  197. unsigned int timeout;
  198. int retries;
  199. /*
  200. * completion callback.
  201. */
  202. rq_end_io_fn *end_io;
  203. void *end_io_data;
  204. /* for bidi */
  205. struct request *next_rq;
  206. };
  207. static inline unsigned short req_get_ioprio(struct request *req)
  208. {
  209. return req->ioprio;
  210. }
  211. /*
  212. * State information carried for REQ_TYPE_PM_SUSPEND and REQ_TYPE_PM_RESUME
  213. * requests. Some step values could eventually be made generic.
  214. */
  215. struct request_pm_state
  216. {
  217. /* PM state machine step value, currently driver specific */
  218. int pm_step;
  219. /* requested PM state value (S1, S2, S3, S4, ...) */
  220. u32 pm_state;
  221. void* data; /* for driver use */
  222. };
  223. #include <linux/elevator.h>
  224. typedef void (request_fn_proc) (struct request_queue *q);
  225. typedef int (make_request_fn) (struct request_queue *q, struct bio *bio);
  226. typedef int (prep_rq_fn) (struct request_queue *, struct request *);
  227. typedef void (unplug_fn) (struct request_queue *);
  228. typedef int (prepare_discard_fn) (struct request_queue *, struct request *);
  229. struct bio_vec;
  230. struct bvec_merge_data {
  231. struct block_device *bi_bdev;
  232. sector_t bi_sector;
  233. unsigned bi_size;
  234. unsigned long bi_rw;
  235. };
  236. typedef int (merge_bvec_fn) (struct request_queue *, struct bvec_merge_data *,
  237. struct bio_vec *);
  238. typedef void (prepare_flush_fn) (struct request_queue *, struct request *);
  239. typedef void (softirq_done_fn)(struct request *);
  240. typedef int (dma_drain_needed_fn)(struct request *);
  241. typedef int (lld_busy_fn) (struct request_queue *q);
  242. enum blk_eh_timer_return {
  243. BLK_EH_NOT_HANDLED,
  244. BLK_EH_HANDLED,
  245. BLK_EH_RESET_TIMER,
  246. };
  247. typedef enum blk_eh_timer_return (rq_timed_out_fn)(struct request *);
  248. enum blk_queue_state {
  249. Queue_down,
  250. Queue_up,
  251. };
  252. struct blk_queue_tag {
  253. struct request **tag_index; /* map of busy tags */
  254. unsigned long *tag_map; /* bit map of free/busy tags */
  255. int busy; /* current depth */
  256. int max_depth; /* what we will send to device */
  257. int real_max_depth; /* what the array can hold */
  258. atomic_t refcnt; /* map can be shared */
  259. };
  260. #define BLK_SCSI_MAX_CMDS (256)
  261. #define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
  262. struct queue_limits {
  263. unsigned long bounce_pfn;
  264. unsigned long seg_boundary_mask;
  265. unsigned int max_hw_sectors;
  266. unsigned int max_sectors;
  267. unsigned int max_segment_size;
  268. unsigned int physical_block_size;
  269. unsigned int alignment_offset;
  270. unsigned int io_min;
  271. unsigned int io_opt;
  272. unsigned short logical_block_size;
  273. unsigned short max_hw_segments;
  274. unsigned short max_phys_segments;
  275. unsigned char misaligned;
  276. unsigned char no_cluster;
  277. };
  278. struct request_queue
  279. {
  280. /*
  281. * Together with queue_head for cacheline sharing
  282. */
  283. struct list_head queue_head;
  284. struct request *last_merge;
  285. struct elevator_queue *elevator;
  286. /*
  287. * the queue request freelist, one for reads and one for writes
  288. */
  289. struct request_list rq;
  290. request_fn_proc *request_fn;
  291. make_request_fn *make_request_fn;
  292. prep_rq_fn *prep_rq_fn;
  293. unplug_fn *unplug_fn;
  294. prepare_discard_fn *prepare_discard_fn;
  295. merge_bvec_fn *merge_bvec_fn;
  296. prepare_flush_fn *prepare_flush_fn;
  297. softirq_done_fn *softirq_done_fn;
  298. rq_timed_out_fn *rq_timed_out_fn;
  299. dma_drain_needed_fn *dma_drain_needed;
  300. lld_busy_fn *lld_busy_fn;
  301. /*
  302. * Dispatch queue sorting
  303. */
  304. sector_t end_sector;
  305. struct request *boundary_rq;
  306. /*
  307. * Auto-unplugging state
  308. */
  309. struct timer_list unplug_timer;
  310. int unplug_thresh; /* After this many requests */
  311. unsigned long unplug_delay; /* After this many jiffies */
  312. struct work_struct unplug_work;
  313. struct backing_dev_info backing_dev_info;
  314. /*
  315. * The queue owner gets to use this for whatever they like.
  316. * ll_rw_blk doesn't touch it.
  317. */
  318. void *queuedata;
  319. /*
  320. * queue needs bounce pages for pages above this limit
  321. */
  322. gfp_t bounce_gfp;
  323. /*
  324. * various queue flags, see QUEUE_* below
  325. */
  326. unsigned long queue_flags;
  327. /*
  328. * protects queue structures from reentrancy. ->__queue_lock should
  329. * _never_ be used directly, it is queue private. always use
  330. * ->queue_lock.
  331. */
  332. spinlock_t __queue_lock;
  333. spinlock_t *queue_lock;
  334. /*
  335. * queue kobject
  336. */
  337. struct kobject kobj;
  338. /*
  339. * queue settings
  340. */
  341. unsigned long nr_requests; /* Max # of requests */
  342. unsigned int nr_congestion_on;
  343. unsigned int nr_congestion_off;
  344. unsigned int nr_batching;
  345. void *dma_drain_buffer;
  346. unsigned int dma_drain_size;
  347. unsigned int dma_pad_mask;
  348. unsigned int dma_alignment;
  349. struct blk_queue_tag *queue_tags;
  350. struct list_head tag_busy_list;
  351. unsigned int nr_sorted;
  352. unsigned int in_flight[2];
  353. unsigned int rq_timeout;
  354. struct timer_list timeout;
  355. struct list_head timeout_list;
  356. struct queue_limits limits;
  357. /*
  358. * sg stuff
  359. */
  360. unsigned int sg_timeout;
  361. unsigned int sg_reserved_size;
  362. int node;
  363. #ifdef CONFIG_BLK_DEV_IO_TRACE
  364. struct blk_trace *blk_trace;
  365. #endif
  366. /*
  367. * reserved for flush operations
  368. */
  369. unsigned int ordered, next_ordered, ordseq;
  370. int orderr, ordcolor;
  371. struct request pre_flush_rq, bar_rq, post_flush_rq;
  372. struct request *orig_bar_rq;
  373. struct mutex sysfs_lock;
  374. #if defined(CONFIG_BLK_DEV_BSG)
  375. struct bsg_class_device bsg_dev;
  376. #endif
  377. };
  378. #define QUEUE_FLAG_CLUSTER 0 /* cluster several segments into 1 */
  379. #define QUEUE_FLAG_QUEUED 1 /* uses generic tag queueing */
  380. #define QUEUE_FLAG_STOPPED 2 /* queue is stopped */
  381. #define QUEUE_FLAG_SYNCFULL 3 /* read queue has been filled */
  382. #define QUEUE_FLAG_ASYNCFULL 4 /* write queue has been filled */
  383. #define QUEUE_FLAG_DEAD 5 /* queue being torn down */
  384. #define QUEUE_FLAG_REENTER 6 /* Re-entrancy avoidance */
  385. #define QUEUE_FLAG_PLUGGED 7 /* queue is plugged */
  386. #define QUEUE_FLAG_ELVSWITCH 8 /* don't use elevator, just do FIFO */
  387. #define QUEUE_FLAG_BIDI 9 /* queue supports bidi requests */
  388. #define QUEUE_FLAG_NOMERGES 10 /* disable merge attempts */
  389. #define QUEUE_FLAG_SAME_COMP 11 /* force complete on same CPU */
  390. #define QUEUE_FLAG_FAIL_IO 12 /* fake timeout */
  391. #define QUEUE_FLAG_STACKABLE 13 /* supports request stacking */
  392. #define QUEUE_FLAG_NONROT 14 /* non-rotational device (SSD) */
  393. #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
  394. #define QUEUE_FLAG_IO_STAT 15 /* do IO stats */
  395. #define QUEUE_FLAG_CQ 16 /* hardware does queuing */
  396. #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
  397. (1 << QUEUE_FLAG_CLUSTER) | \
  398. (1 << QUEUE_FLAG_STACKABLE) | \
  399. (1 << QUEUE_FLAG_SAME_COMP))
  400. static inline int queue_is_locked(struct request_queue *q)
  401. {
  402. #ifdef CONFIG_SMP
  403. spinlock_t *lock = q->queue_lock;
  404. return lock && spin_is_locked(lock);
  405. #else
  406. return 1;
  407. #endif
  408. }
  409. static inline void queue_flag_set_unlocked(unsigned int flag,
  410. struct request_queue *q)
  411. {
  412. __set_bit(flag, &q->queue_flags);
  413. }
  414. static inline int queue_flag_test_and_clear(unsigned int flag,
  415. struct request_queue *q)
  416. {
  417. WARN_ON_ONCE(!queue_is_locked(q));
  418. if (test_bit(flag, &q->queue_flags)) {
  419. __clear_bit(flag, &q->queue_flags);
  420. return 1;
  421. }
  422. return 0;
  423. }
  424. static inline int queue_flag_test_and_set(unsigned int flag,
  425. struct request_queue *q)
  426. {
  427. WARN_ON_ONCE(!queue_is_locked(q));
  428. if (!test_bit(flag, &q->queue_flags)) {
  429. __set_bit(flag, &q->queue_flags);
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. static inline void queue_flag_set(unsigned int flag, struct request_queue *q)
  435. {
  436. WARN_ON_ONCE(!queue_is_locked(q));
  437. __set_bit(flag, &q->queue_flags);
  438. }
  439. static inline void queue_flag_clear_unlocked(unsigned int flag,
  440. struct request_queue *q)
  441. {
  442. __clear_bit(flag, &q->queue_flags);
  443. }
  444. static inline int queue_in_flight(struct request_queue *q)
  445. {
  446. return q->in_flight[0] + q->in_flight[1];
  447. }
  448. static inline void queue_flag_clear(unsigned int flag, struct request_queue *q)
  449. {
  450. WARN_ON_ONCE(!queue_is_locked(q));
  451. __clear_bit(flag, &q->queue_flags);
  452. }
  453. enum {
  454. /*
  455. * Hardbarrier is supported with one of the following methods.
  456. *
  457. * NONE : hardbarrier unsupported
  458. * DRAIN : ordering by draining is enough
  459. * DRAIN_FLUSH : ordering by draining w/ pre and post flushes
  460. * DRAIN_FUA : ordering by draining w/ pre flush and FUA write
  461. * TAG : ordering by tag is enough
  462. * TAG_FLUSH : ordering by tag w/ pre and post flushes
  463. * TAG_FUA : ordering by tag w/ pre flush and FUA write
  464. */
  465. QUEUE_ORDERED_BY_DRAIN = 0x01,
  466. QUEUE_ORDERED_BY_TAG = 0x02,
  467. QUEUE_ORDERED_DO_PREFLUSH = 0x10,
  468. QUEUE_ORDERED_DO_BAR = 0x20,
  469. QUEUE_ORDERED_DO_POSTFLUSH = 0x40,
  470. QUEUE_ORDERED_DO_FUA = 0x80,
  471. QUEUE_ORDERED_NONE = 0x00,
  472. QUEUE_ORDERED_DRAIN = QUEUE_ORDERED_BY_DRAIN |
  473. QUEUE_ORDERED_DO_BAR,
  474. QUEUE_ORDERED_DRAIN_FLUSH = QUEUE_ORDERED_DRAIN |
  475. QUEUE_ORDERED_DO_PREFLUSH |
  476. QUEUE_ORDERED_DO_POSTFLUSH,
  477. QUEUE_ORDERED_DRAIN_FUA = QUEUE_ORDERED_DRAIN |
  478. QUEUE_ORDERED_DO_PREFLUSH |
  479. QUEUE_ORDERED_DO_FUA,
  480. QUEUE_ORDERED_TAG = QUEUE_ORDERED_BY_TAG |
  481. QUEUE_ORDERED_DO_BAR,
  482. QUEUE_ORDERED_TAG_FLUSH = QUEUE_ORDERED_TAG |
  483. QUEUE_ORDERED_DO_PREFLUSH |
  484. QUEUE_ORDERED_DO_POSTFLUSH,
  485. QUEUE_ORDERED_TAG_FUA = QUEUE_ORDERED_TAG |
  486. QUEUE_ORDERED_DO_PREFLUSH |
  487. QUEUE_ORDERED_DO_FUA,
  488. /*
  489. * Ordered operation sequence
  490. */
  491. QUEUE_ORDSEQ_STARTED = 0x01, /* flushing in progress */
  492. QUEUE_ORDSEQ_DRAIN = 0x02, /* waiting for the queue to be drained */
  493. QUEUE_ORDSEQ_PREFLUSH = 0x04, /* pre-flushing in progress */
  494. QUEUE_ORDSEQ_BAR = 0x08, /* original barrier req in progress */
  495. QUEUE_ORDSEQ_POSTFLUSH = 0x10, /* post-flushing in progress */
  496. QUEUE_ORDSEQ_DONE = 0x20,
  497. };
  498. #define blk_queue_plugged(q) test_bit(QUEUE_FLAG_PLUGGED, &(q)->queue_flags)
  499. #define blk_queue_tagged(q) test_bit(QUEUE_FLAG_QUEUED, &(q)->queue_flags)
  500. #define blk_queue_queuing(q) test_bit(QUEUE_FLAG_CQ, &(q)->queue_flags)
  501. #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
  502. #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
  503. #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
  504. #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
  505. #define blk_queue_flushing(q) ((q)->ordseq)
  506. #define blk_queue_stackable(q) \
  507. test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
  508. #define blk_fs_request(rq) ((rq)->cmd_type == REQ_TYPE_FS)
  509. #define blk_pc_request(rq) ((rq)->cmd_type == REQ_TYPE_BLOCK_PC)
  510. #define blk_special_request(rq) ((rq)->cmd_type == REQ_TYPE_SPECIAL)
  511. #define blk_sense_request(rq) ((rq)->cmd_type == REQ_TYPE_SENSE)
  512. #define blk_failfast_dev(rq) ((rq)->cmd_flags & REQ_FAILFAST_DEV)
  513. #define blk_failfast_transport(rq) ((rq)->cmd_flags & REQ_FAILFAST_TRANSPORT)
  514. #define blk_failfast_driver(rq) ((rq)->cmd_flags & REQ_FAILFAST_DRIVER)
  515. #define blk_noretry_request(rq) (blk_failfast_dev(rq) || \
  516. blk_failfast_transport(rq) || \
  517. blk_failfast_driver(rq))
  518. #define blk_rq_started(rq) ((rq)->cmd_flags & REQ_STARTED)
  519. #define blk_rq_io_stat(rq) ((rq)->cmd_flags & REQ_IO_STAT)
  520. #define blk_rq_quiet(rq) ((rq)->cmd_flags & REQ_QUIET)
  521. #define blk_account_rq(rq) (blk_rq_started(rq) && (blk_fs_request(rq) || blk_discard_rq(rq)))
  522. #define blk_pm_suspend_request(rq) ((rq)->cmd_type == REQ_TYPE_PM_SUSPEND)
  523. #define blk_pm_resume_request(rq) ((rq)->cmd_type == REQ_TYPE_PM_RESUME)
  524. #define blk_pm_request(rq) \
  525. (blk_pm_suspend_request(rq) || blk_pm_resume_request(rq))
  526. #define blk_rq_cpu_valid(rq) ((rq)->cpu != -1)
  527. #define blk_sorted_rq(rq) ((rq)->cmd_flags & REQ_SORTED)
  528. #define blk_barrier_rq(rq) ((rq)->cmd_flags & REQ_HARDBARRIER)
  529. #define blk_fua_rq(rq) ((rq)->cmd_flags & REQ_FUA)
  530. #define blk_discard_rq(rq) ((rq)->cmd_flags & REQ_DISCARD)
  531. #define blk_bidi_rq(rq) ((rq)->next_rq != NULL)
  532. /* rq->queuelist of dequeued request must be list_empty() */
  533. #define blk_queued_rq(rq) (!list_empty(&(rq)->queuelist))
  534. #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
  535. #define rq_data_dir(rq) ((rq)->cmd_flags & 1)
  536. /*
  537. * We regard a request as sync, if either a read or a sync write
  538. */
  539. static inline bool rw_is_sync(unsigned int rw_flags)
  540. {
  541. return !(rw_flags & REQ_RW) || (rw_flags & REQ_RW_SYNC);
  542. }
  543. static inline bool rq_is_sync(struct request *rq)
  544. {
  545. return rw_is_sync(rq->cmd_flags);
  546. }
  547. #define rq_is_meta(rq) ((rq)->cmd_flags & REQ_RW_META)
  548. #define rq_noidle(rq) ((rq)->cmd_flags & REQ_NOIDLE)
  549. static inline int blk_queue_full(struct request_queue *q, int sync)
  550. {
  551. if (sync)
  552. return test_bit(QUEUE_FLAG_SYNCFULL, &q->queue_flags);
  553. return test_bit(QUEUE_FLAG_ASYNCFULL, &q->queue_flags);
  554. }
  555. static inline void blk_set_queue_full(struct request_queue *q, int sync)
  556. {
  557. if (sync)
  558. queue_flag_set(QUEUE_FLAG_SYNCFULL, q);
  559. else
  560. queue_flag_set(QUEUE_FLAG_ASYNCFULL, q);
  561. }
  562. static inline void blk_clear_queue_full(struct request_queue *q, int sync)
  563. {
  564. if (sync)
  565. queue_flag_clear(QUEUE_FLAG_SYNCFULL, q);
  566. else
  567. queue_flag_clear(QUEUE_FLAG_ASYNCFULL, q);
  568. }
  569. /*
  570. * mergeable request must not have _NOMERGE or _BARRIER bit set, nor may
  571. * it already be started by driver.
  572. */
  573. #define RQ_NOMERGE_FLAGS \
  574. (REQ_NOMERGE | REQ_STARTED | REQ_HARDBARRIER | REQ_SOFTBARRIER)
  575. #define rq_mergeable(rq) \
  576. (!((rq)->cmd_flags & RQ_NOMERGE_FLAGS) && \
  577. (blk_discard_rq(rq) || blk_fs_request((rq))))
  578. /*
  579. * q->prep_rq_fn return values
  580. */
  581. #define BLKPREP_OK 0 /* serve it */
  582. #define BLKPREP_KILL 1 /* fatal error, kill */
  583. #define BLKPREP_DEFER 2 /* leave on queue */
  584. extern unsigned long blk_max_low_pfn, blk_max_pfn;
  585. /*
  586. * standard bounce addresses:
  587. *
  588. * BLK_BOUNCE_HIGH : bounce all highmem pages
  589. * BLK_BOUNCE_ANY : don't bounce anything
  590. * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
  591. */
  592. #if BITS_PER_LONG == 32
  593. #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
  594. #else
  595. #define BLK_BOUNCE_HIGH -1ULL
  596. #endif
  597. #define BLK_BOUNCE_ANY (-1ULL)
  598. #define BLK_BOUNCE_ISA (ISA_DMA_THRESHOLD)
  599. /*
  600. * default timeout for SG_IO if none specified
  601. */
  602. #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
  603. #define BLK_MIN_SG_TIMEOUT (7 * HZ)
  604. #ifdef CONFIG_BOUNCE
  605. extern int init_emergency_isa_pool(void);
  606. extern void blk_queue_bounce(struct request_queue *q, struct bio **bio);
  607. #else
  608. static inline int init_emergency_isa_pool(void)
  609. {
  610. return 0;
  611. }
  612. static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
  613. {
  614. }
  615. #endif /* CONFIG_MMU */
  616. struct rq_map_data {
  617. struct page **pages;
  618. int page_order;
  619. int nr_entries;
  620. unsigned long offset;
  621. int null_mapped;
  622. int from_user;
  623. };
  624. struct req_iterator {
  625. int i;
  626. struct bio *bio;
  627. };
  628. /* This should not be used directly - use rq_for_each_segment */
  629. #define for_each_bio(_bio) \
  630. for (; _bio; _bio = _bio->bi_next)
  631. #define __rq_for_each_bio(_bio, rq) \
  632. if ((rq->bio)) \
  633. for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
  634. #define rq_for_each_segment(bvl, _rq, _iter) \
  635. __rq_for_each_bio(_iter.bio, _rq) \
  636. bio_for_each_segment(bvl, _iter.bio, _iter.i)
  637. #define rq_iter_last(rq, _iter) \
  638. (_iter.bio->bi_next == NULL && _iter.i == _iter.bio->bi_vcnt-1)
  639. extern int blk_register_queue(struct gendisk *disk);
  640. extern void blk_unregister_queue(struct gendisk *disk);
  641. extern void register_disk(struct gendisk *dev);
  642. extern void generic_make_request(struct bio *bio);
  643. extern void blk_rq_init(struct request_queue *q, struct request *rq);
  644. extern void blk_put_request(struct request *);
  645. extern void __blk_put_request(struct request_queue *, struct request *);
  646. extern struct request *blk_get_request(struct request_queue *, int, gfp_t);
  647. extern struct request *blk_make_request(struct request_queue *, struct bio *,
  648. gfp_t);
  649. extern void blk_insert_request(struct request_queue *, struct request *, int, void *);
  650. extern void blk_requeue_request(struct request_queue *, struct request *);
  651. extern int blk_rq_check_limits(struct request_queue *q, struct request *rq);
  652. extern int blk_lld_busy(struct request_queue *q);
  653. extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
  654. struct bio_set *bs, gfp_t gfp_mask,
  655. int (*bio_ctr)(struct bio *, struct bio *, void *),
  656. void *data);
  657. extern void blk_rq_unprep_clone(struct request *rq);
  658. extern int blk_insert_cloned_request(struct request_queue *q,
  659. struct request *rq);
  660. extern void blk_plug_device(struct request_queue *);
  661. extern void blk_plug_device_unlocked(struct request_queue *);
  662. extern int blk_remove_plug(struct request_queue *);
  663. extern void blk_recount_segments(struct request_queue *, struct bio *);
  664. extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
  665. unsigned int, void __user *);
  666. extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
  667. struct scsi_ioctl_command __user *);
  668. /*
  669. * A queue has just exitted congestion. Note this in the global counter of
  670. * congested queues, and wake up anyone who was waiting for requests to be
  671. * put back.
  672. */
  673. static inline void blk_clear_queue_congested(struct request_queue *q, int sync)
  674. {
  675. clear_bdi_congested(&q->backing_dev_info, sync);
  676. }
  677. /*
  678. * A queue has just entered congestion. Flag that in the queue's VM-visible
  679. * state flags and increment the global gounter of congested queues.
  680. */
  681. static inline void blk_set_queue_congested(struct request_queue *q, int sync)
  682. {
  683. set_bdi_congested(&q->backing_dev_info, sync);
  684. }
  685. extern void blk_start_queue(struct request_queue *q);
  686. extern void blk_stop_queue(struct request_queue *q);
  687. extern void blk_sync_queue(struct request_queue *q);
  688. extern void __blk_stop_queue(struct request_queue *q);
  689. extern void __blk_run_queue(struct request_queue *);
  690. extern void blk_run_queue(struct request_queue *);
  691. extern int blk_rq_map_user(struct request_queue *, struct request *,
  692. struct rq_map_data *, void __user *, unsigned long,
  693. gfp_t);
  694. extern int blk_rq_unmap_user(struct bio *);
  695. extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
  696. extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
  697. struct rq_map_data *, struct sg_iovec *, int,
  698. unsigned int, gfp_t);
  699. extern int blk_execute_rq(struct request_queue *, struct gendisk *,
  700. struct request *, int);
  701. extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
  702. struct request *, int, rq_end_io_fn *);
  703. extern void blk_unplug(struct request_queue *q);
  704. static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
  705. {
  706. return bdev->bd_disk->queue;
  707. }
  708. static inline void blk_run_backing_dev(struct backing_dev_info *bdi,
  709. struct page *page)
  710. {
  711. if (bdi && bdi->unplug_io_fn)
  712. bdi->unplug_io_fn(bdi, page);
  713. }
  714. static inline void blk_run_address_space(struct address_space *mapping)
  715. {
  716. if (mapping)
  717. blk_run_backing_dev(mapping->backing_dev_info, NULL);
  718. }
  719. /*
  720. * blk_rq_pos() : the current sector
  721. * blk_rq_bytes() : bytes left in the entire request
  722. * blk_rq_cur_bytes() : bytes left in the current segment
  723. * blk_rq_err_bytes() : bytes left till the next error boundary
  724. * blk_rq_sectors() : sectors left in the entire request
  725. * blk_rq_cur_sectors() : sectors left in the current segment
  726. * blk_rq_err_sectors() : sectors left till the next error boundary
  727. */
  728. static inline sector_t blk_rq_pos(const struct request *rq)
  729. {
  730. return rq->__sector;
  731. }
  732. static inline unsigned int blk_rq_bytes(const struct request *rq)
  733. {
  734. return rq->__data_len;
  735. }
  736. static inline int blk_rq_cur_bytes(const struct request *rq)
  737. {
  738. return rq->bio ? bio_cur_bytes(rq->bio) : 0;
  739. }
  740. extern unsigned int blk_rq_err_bytes(const struct request *rq);
  741. static inline unsigned int blk_rq_sectors(const struct request *rq)
  742. {
  743. return blk_rq_bytes(rq) >> 9;
  744. }
  745. static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
  746. {
  747. return blk_rq_cur_bytes(rq) >> 9;
  748. }
  749. static inline unsigned int blk_rq_err_sectors(const struct request *rq)
  750. {
  751. return blk_rq_err_bytes(rq) >> 9;
  752. }
  753. /*
  754. * Request issue related functions.
  755. */
  756. extern struct request *blk_peek_request(struct request_queue *q);
  757. extern void blk_start_request(struct request *rq);
  758. extern struct request *blk_fetch_request(struct request_queue *q);
  759. /*
  760. * Request completion related functions.
  761. *
  762. * blk_update_request() completes given number of bytes and updates
  763. * the request without completing it.
  764. *
  765. * blk_end_request() and friends. __blk_end_request() must be called
  766. * with the request queue spinlock acquired.
  767. *
  768. * Several drivers define their own end_request and call
  769. * blk_end_request() for parts of the original function.
  770. * This prevents code duplication in drivers.
  771. */
  772. extern bool blk_update_request(struct request *rq, int error,
  773. unsigned int nr_bytes);
  774. extern bool blk_end_request(struct request *rq, int error,
  775. unsigned int nr_bytes);
  776. extern void blk_end_request_all(struct request *rq, int error);
  777. extern bool blk_end_request_cur(struct request *rq, int error);
  778. extern bool blk_end_request_err(struct request *rq, int error);
  779. extern bool __blk_end_request(struct request *rq, int error,
  780. unsigned int nr_bytes);
  781. extern void __blk_end_request_all(struct request *rq, int error);
  782. extern bool __blk_end_request_cur(struct request *rq, int error);
  783. extern bool __blk_end_request_err(struct request *rq, int error);
  784. extern void blk_complete_request(struct request *);
  785. extern void __blk_complete_request(struct request *);
  786. extern void blk_abort_request(struct request *);
  787. extern void blk_abort_queue(struct request_queue *);
  788. /*
  789. * Access functions for manipulating queue properties
  790. */
  791. extern struct request_queue *blk_init_queue_node(request_fn_proc *rfn,
  792. spinlock_t *lock, int node_id);
  793. extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *);
  794. extern void blk_cleanup_queue(struct request_queue *);
  795. extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
  796. extern void blk_queue_bounce_limit(struct request_queue *, u64);
  797. extern void blk_queue_max_sectors(struct request_queue *, unsigned int);
  798. extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
  799. extern void blk_queue_max_phys_segments(struct request_queue *, unsigned short);
  800. extern void blk_queue_max_hw_segments(struct request_queue *, unsigned short);
  801. extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
  802. extern void blk_queue_logical_block_size(struct request_queue *, unsigned short);
  803. extern void blk_queue_physical_block_size(struct request_queue *, unsigned short);
  804. extern void blk_queue_alignment_offset(struct request_queue *q,
  805. unsigned int alignment);
  806. extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
  807. extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
  808. extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
  809. extern void blk_set_default_limits(struct queue_limits *lim);
  810. extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  811. sector_t offset);
  812. extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
  813. sector_t offset);
  814. extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
  815. extern void blk_queue_dma_pad(struct request_queue *, unsigned int);
  816. extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
  817. extern int blk_queue_dma_drain(struct request_queue *q,
  818. dma_drain_needed_fn *dma_drain_needed,
  819. void *buf, unsigned int size);
  820. extern void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn);
  821. extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
  822. extern void blk_queue_prep_rq(struct request_queue *, prep_rq_fn *pfn);
  823. extern void blk_queue_merge_bvec(struct request_queue *, merge_bvec_fn *);
  824. extern void blk_queue_dma_alignment(struct request_queue *, int);
  825. extern void blk_queue_update_dma_alignment(struct request_queue *, int);
  826. extern void blk_queue_softirq_done(struct request_queue *, softirq_done_fn *);
  827. extern void blk_queue_set_discard(struct request_queue *, prepare_discard_fn *);
  828. extern void blk_queue_rq_timed_out(struct request_queue *, rq_timed_out_fn *);
  829. extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
  830. extern struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev);
  831. extern int blk_queue_ordered(struct request_queue *, unsigned, prepare_flush_fn *);
  832. extern bool blk_do_ordered(struct request_queue *, struct request **);
  833. extern unsigned blk_ordered_cur_seq(struct request_queue *);
  834. extern unsigned blk_ordered_req_seq(struct request *);
  835. extern bool blk_ordered_complete_seq(struct request_queue *, unsigned, int);
  836. extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
  837. extern void blk_dump_rq_flags(struct request *, char *);
  838. extern void generic_unplug_device(struct request_queue *);
  839. extern long nr_blockdev_pages(void);
  840. int blk_get_queue(struct request_queue *);
  841. struct request_queue *blk_alloc_queue(gfp_t);
  842. struct request_queue *blk_alloc_queue_node(gfp_t, int);
  843. extern void blk_put_queue(struct request_queue *);
  844. /*
  845. * tag stuff
  846. */
  847. #define blk_rq_tagged(rq) ((rq)->cmd_flags & REQ_QUEUED)
  848. extern int blk_queue_start_tag(struct request_queue *, struct request *);
  849. extern struct request *blk_queue_find_tag(struct request_queue *, int);
  850. extern void blk_queue_end_tag(struct request_queue *, struct request *);
  851. extern int blk_queue_init_tags(struct request_queue *, int, struct blk_queue_tag *);
  852. extern void blk_queue_free_tags(struct request_queue *);
  853. extern int blk_queue_resize_tags(struct request_queue *, int);
  854. extern void blk_queue_invalidate_tags(struct request_queue *);
  855. extern struct blk_queue_tag *blk_init_tags(int);
  856. extern void blk_free_tags(struct blk_queue_tag *);
  857. static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt,
  858. int tag)
  859. {
  860. if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
  861. return NULL;
  862. return bqt->tag_index[tag];
  863. }
  864. extern int blkdev_issue_flush(struct block_device *, sector_t *);
  865. extern int blkdev_issue_discard(struct block_device *,
  866. sector_t sector, sector_t nr_sects, gfp_t);
  867. static inline int sb_issue_discard(struct super_block *sb,
  868. sector_t block, sector_t nr_blocks)
  869. {
  870. block <<= (sb->s_blocksize_bits - 9);
  871. nr_blocks <<= (sb->s_blocksize_bits - 9);
  872. return blkdev_issue_discard(sb->s_bdev, block, nr_blocks, GFP_KERNEL);
  873. }
  874. extern int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm);
  875. #define MAX_PHYS_SEGMENTS 128
  876. #define MAX_HW_SEGMENTS 128
  877. #define SAFE_MAX_SECTORS 255
  878. #define BLK_DEF_MAX_SECTORS 1024
  879. #define MAX_SEGMENT_SIZE 65536
  880. #define BLK_SEG_BOUNDARY_MASK 0xFFFFFFFFUL
  881. #define blkdev_entry_to_request(entry) list_entry((entry), struct request, queuelist)
  882. static inline unsigned long queue_bounce_pfn(struct request_queue *q)
  883. {
  884. return q->limits.bounce_pfn;
  885. }
  886. static inline unsigned long queue_segment_boundary(struct request_queue *q)
  887. {
  888. return q->limits.seg_boundary_mask;
  889. }
  890. static inline unsigned int queue_max_sectors(struct request_queue *q)
  891. {
  892. return q->limits.max_sectors;
  893. }
  894. static inline unsigned int queue_max_hw_sectors(struct request_queue *q)
  895. {
  896. return q->limits.max_hw_sectors;
  897. }
  898. static inline unsigned short queue_max_hw_segments(struct request_queue *q)
  899. {
  900. return q->limits.max_hw_segments;
  901. }
  902. static inline unsigned short queue_max_phys_segments(struct request_queue *q)
  903. {
  904. return q->limits.max_phys_segments;
  905. }
  906. static inline unsigned int queue_max_segment_size(struct request_queue *q)
  907. {
  908. return q->limits.max_segment_size;
  909. }
  910. static inline unsigned short queue_logical_block_size(struct request_queue *q)
  911. {
  912. int retval = 512;
  913. if (q && q->limits.logical_block_size)
  914. retval = q->limits.logical_block_size;
  915. return retval;
  916. }
  917. static inline unsigned short bdev_logical_block_size(struct block_device *bdev)
  918. {
  919. return queue_logical_block_size(bdev_get_queue(bdev));
  920. }
  921. static inline unsigned int queue_physical_block_size(struct request_queue *q)
  922. {
  923. return q->limits.physical_block_size;
  924. }
  925. static inline unsigned int queue_io_min(struct request_queue *q)
  926. {
  927. return q->limits.io_min;
  928. }
  929. static inline unsigned int queue_io_opt(struct request_queue *q)
  930. {
  931. return q->limits.io_opt;
  932. }
  933. static inline int queue_alignment_offset(struct request_queue *q)
  934. {
  935. if (q && q->limits.misaligned)
  936. return -1;
  937. if (q && q->limits.alignment_offset)
  938. return q->limits.alignment_offset;
  939. return 0;
  940. }
  941. static inline int queue_sector_alignment_offset(struct request_queue *q,
  942. sector_t sector)
  943. {
  944. return ((sector << 9) - q->limits.alignment_offset)
  945. & (q->limits.io_min - 1);
  946. }
  947. static inline int queue_dma_alignment(struct request_queue *q)
  948. {
  949. return q ? q->dma_alignment : 511;
  950. }
  951. static inline int blk_rq_aligned(struct request_queue *q, void *addr,
  952. unsigned int len)
  953. {
  954. unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
  955. return !((unsigned long)addr & alignment) && !(len & alignment);
  956. }
  957. /* assumes size > 256 */
  958. static inline unsigned int blksize_bits(unsigned int size)
  959. {
  960. unsigned int bits = 8;
  961. do {
  962. bits++;
  963. size >>= 1;
  964. } while (size > 256);
  965. return bits;
  966. }
  967. static inline unsigned int block_size(struct block_device *bdev)
  968. {
  969. return bdev->bd_block_size;
  970. }
  971. typedef struct {struct page *v;} Sector;
  972. unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *);
  973. static inline void put_dev_sector(Sector p)
  974. {
  975. page_cache_release(p.v);
  976. }
  977. struct work_struct;
  978. int kblockd_schedule_work(struct request_queue *q, struct work_struct *work);
  979. #define MODULE_ALIAS_BLOCKDEV(major,minor) \
  980. MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
  981. #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
  982. MODULE_ALIAS("block-major-" __stringify(major) "-*")
  983. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  984. #define INTEGRITY_FLAG_READ 2 /* verify data integrity on read */
  985. #define INTEGRITY_FLAG_WRITE 4 /* generate data integrity on write */
  986. struct blk_integrity_exchg {
  987. void *prot_buf;
  988. void *data_buf;
  989. sector_t sector;
  990. unsigned int data_size;
  991. unsigned short sector_size;
  992. const char *disk_name;
  993. };
  994. typedef void (integrity_gen_fn) (struct blk_integrity_exchg *);
  995. typedef int (integrity_vrfy_fn) (struct blk_integrity_exchg *);
  996. typedef void (integrity_set_tag_fn) (void *, void *, unsigned int);
  997. typedef void (integrity_get_tag_fn) (void *, void *, unsigned int);
  998. struct blk_integrity {
  999. integrity_gen_fn *generate_fn;
  1000. integrity_vrfy_fn *verify_fn;
  1001. integrity_set_tag_fn *set_tag_fn;
  1002. integrity_get_tag_fn *get_tag_fn;
  1003. unsigned short flags;
  1004. unsigned short tuple_size;
  1005. unsigned short sector_size;
  1006. unsigned short tag_size;
  1007. const char *name;
  1008. struct kobject kobj;
  1009. };
  1010. extern int blk_integrity_register(struct gendisk *, struct blk_integrity *);
  1011. extern void blk_integrity_unregister(struct gendisk *);
  1012. extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
  1013. extern int blk_rq_map_integrity_sg(struct request *, struct scatterlist *);
  1014. extern int blk_rq_count_integrity_sg(struct request *);
  1015. static inline
  1016. struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
  1017. {
  1018. return bdev->bd_disk->integrity;
  1019. }
  1020. static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
  1021. {
  1022. return disk->integrity;
  1023. }
  1024. static inline int blk_integrity_rq(struct request *rq)
  1025. {
  1026. if (rq->bio == NULL)
  1027. return 0;
  1028. return bio_integrity(rq->bio);
  1029. }
  1030. #else /* CONFIG_BLK_DEV_INTEGRITY */
  1031. #define blk_integrity_rq(rq) (0)
  1032. #define blk_rq_count_integrity_sg(a) (0)
  1033. #define blk_rq_map_integrity_sg(a, b) (0)
  1034. #define bdev_get_integrity(a) (0)
  1035. #define blk_get_integrity(a) (0)
  1036. #define blk_integrity_compare(a, b) (0)
  1037. #define blk_integrity_register(a, b) (0)
  1038. #define blk_integrity_unregister(a) do { } while (0);
  1039. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  1040. struct block_device_operations {
  1041. int (*open) (struct block_device *, fmode_t);
  1042. int (*release) (struct gendisk *, fmode_t);
  1043. int (*locked_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
  1044. int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
  1045. int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
  1046. int (*direct_access) (struct block_device *, sector_t,
  1047. void **, unsigned long *);
  1048. int (*media_changed) (struct gendisk *);
  1049. unsigned long long (*set_capacity) (struct gendisk *,
  1050. unsigned long long);
  1051. int (*revalidate_disk) (struct gendisk *);
  1052. int (*getgeo)(struct block_device *, struct hd_geometry *);
  1053. struct module *owner;
  1054. };
  1055. extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
  1056. unsigned long);
  1057. #else /* CONFIG_BLOCK */
  1058. /*
  1059. * stubs for when the block layer is configured out
  1060. */
  1061. #define buffer_heads_over_limit 0
  1062. static inline long nr_blockdev_pages(void)
  1063. {
  1064. return 0;
  1065. }
  1066. #endif /* CONFIG_BLOCK */
  1067. #endif