blkdev.h 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  1. #ifndef _LINUX_BLKDEV_H
  2. #define _LINUX_BLKDEV_H
  3. #include <linux/sched.h>
  4. #include <linux/sched/clock.h>
  5. #ifdef CONFIG_BLOCK
  6. #include <linux/major.h>
  7. #include <linux/genhd.h>
  8. #include <linux/list.h>
  9. #include <linux/llist.h>
  10. #include <linux/timer.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/backing-dev-defs.h>
  14. #include <linux/wait.h>
  15. #include <linux/mempool.h>
  16. #include <linux/pfn.h>
  17. #include <linux/bio.h>
  18. #include <linux/stringify.h>
  19. #include <linux/gfp.h>
  20. #include <linux/bsg.h>
  21. #include <linux/smp.h>
  22. #include <linux/rcupdate.h>
  23. #include <linux/percpu-refcount.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/blkzoned.h>
  26. struct module;
  27. struct scsi_ioctl_command;
  28. struct request_queue;
  29. struct elevator_queue;
  30. struct blk_trace;
  31. struct request;
  32. struct sg_io_hdr;
  33. struct bsg_job;
  34. struct blkcg_gq;
  35. struct blk_flush_queue;
  36. struct pr_ops;
  37. struct rq_wb;
  38. struct blk_queue_stats;
  39. struct blk_stat_callback;
  40. #define BLKDEV_MIN_RQ 4
  41. #define BLKDEV_MAX_RQ 128 /* Default maximum */
  42. /* Must be consisitent with blk_mq_poll_stats_bkt() */
  43. #define BLK_MQ_POLL_STATS_BKTS 16
  44. /*
  45. * Maximum number of blkcg policies allowed to be registered concurrently.
  46. * Defined here to simplify include dependency.
  47. */
  48. #define BLKCG_MAX_POLS 3
  49. typedef void (rq_end_io_fn)(struct request *, blk_status_t);
  50. #define BLK_RL_SYNCFULL (1U << 0)
  51. #define BLK_RL_ASYNCFULL (1U << 1)
  52. struct request_list {
  53. struct request_queue *q; /* the queue this rl belongs to */
  54. #ifdef CONFIG_BLK_CGROUP
  55. struct blkcg_gq *blkg; /* blkg this request pool belongs to */
  56. #endif
  57. /*
  58. * count[], starved[], and wait[] are indexed by
  59. * BLK_RW_SYNC/BLK_RW_ASYNC
  60. */
  61. int count[2];
  62. int starved[2];
  63. mempool_t *rq_pool;
  64. wait_queue_head_t wait[2];
  65. unsigned int flags;
  66. };
  67. /*
  68. * request flags */
  69. typedef __u32 __bitwise req_flags_t;
  70. /* elevator knows about this request */
  71. #define RQF_SORTED ((__force req_flags_t)(1 << 0))
  72. /* drive already may have started this one */
  73. #define RQF_STARTED ((__force req_flags_t)(1 << 1))
  74. /* uses tagged queueing */
  75. #define RQF_QUEUED ((__force req_flags_t)(1 << 2))
  76. /* may not be passed by ioscheduler */
  77. #define RQF_SOFTBARRIER ((__force req_flags_t)(1 << 3))
  78. /* request for flush sequence */
  79. #define RQF_FLUSH_SEQ ((__force req_flags_t)(1 << 4))
  80. /* merge of different types, fail separately */
  81. #define RQF_MIXED_MERGE ((__force req_flags_t)(1 << 5))
  82. /* track inflight for MQ */
  83. #define RQF_MQ_INFLIGHT ((__force req_flags_t)(1 << 6))
  84. /* don't call prep for this one */
  85. #define RQF_DONTPREP ((__force req_flags_t)(1 << 7))
  86. /* set for "ide_preempt" requests and also for requests for which the SCSI
  87. "quiesce" state must be ignored. */
  88. #define RQF_PREEMPT ((__force req_flags_t)(1 << 8))
  89. /* contains copies of user pages */
  90. #define RQF_COPY_USER ((__force req_flags_t)(1 << 9))
  91. /* vaguely specified driver internal error. Ignored by the block layer */
  92. #define RQF_FAILED ((__force req_flags_t)(1 << 10))
  93. /* don't warn about errors */
  94. #define RQF_QUIET ((__force req_flags_t)(1 << 11))
  95. /* elevator private data attached */
  96. #define RQF_ELVPRIV ((__force req_flags_t)(1 << 12))
  97. /* account I/O stat */
  98. #define RQF_IO_STAT ((__force req_flags_t)(1 << 13))
  99. /* request came from our alloc pool */
  100. #define RQF_ALLOCED ((__force req_flags_t)(1 << 14))
  101. /* runtime pm request */
  102. #define RQF_PM ((__force req_flags_t)(1 << 15))
  103. /* on IO scheduler merge hash */
  104. #define RQF_HASHED ((__force req_flags_t)(1 << 16))
  105. /* IO stats tracking on */
  106. #define RQF_STATS ((__force req_flags_t)(1 << 17))
  107. /* Look at ->special_vec for the actual data payload instead of the
  108. bio chain. */
  109. #define RQF_SPECIAL_PAYLOAD ((__force req_flags_t)(1 << 18))
  110. /* flags that prevent us from merging requests: */
  111. #define RQF_NOMERGE_FLAGS \
  112. (RQF_STARTED | RQF_SOFTBARRIER | RQF_FLUSH_SEQ | RQF_SPECIAL_PAYLOAD)
  113. /*
  114. * Try to put the fields that are referenced together in the same cacheline.
  115. *
  116. * If you modify this structure, make sure to update blk_rq_init() and
  117. * especially blk_mq_rq_ctx_init() to take care of the added fields.
  118. */
  119. struct request {
  120. struct list_head queuelist;
  121. union {
  122. struct call_single_data csd;
  123. u64 fifo_time;
  124. };
  125. struct request_queue *q;
  126. struct blk_mq_ctx *mq_ctx;
  127. int cpu;
  128. unsigned int cmd_flags; /* op and common flags */
  129. req_flags_t rq_flags;
  130. int internal_tag;
  131. unsigned long atomic_flags;
  132. /* the following two fields are internal, NEVER access directly */
  133. unsigned int __data_len; /* total data len */
  134. int tag;
  135. sector_t __sector; /* sector cursor */
  136. struct bio *bio;
  137. struct bio *biotail;
  138. /*
  139. * The hash is used inside the scheduler, and killed once the
  140. * request reaches the dispatch list. The ipi_list is only used
  141. * to queue the request for softirq completion, which is long
  142. * after the request has been unhashed (and even removed from
  143. * the dispatch list).
  144. */
  145. union {
  146. struct hlist_node hash; /* merge hash */
  147. struct list_head ipi_list;
  148. };
  149. /*
  150. * The rb_node is only used inside the io scheduler, requests
  151. * are pruned when moved to the dispatch queue. So let the
  152. * completion_data share space with the rb_node.
  153. */
  154. union {
  155. struct rb_node rb_node; /* sort/lookup */
  156. struct bio_vec special_vec;
  157. void *completion_data;
  158. int error_count; /* for legacy drivers, don't use */
  159. };
  160. /*
  161. * Three pointers are available for the IO schedulers, if they need
  162. * more they have to dynamically allocate it. Flush requests are
  163. * never put on the IO scheduler. So let the flush fields share
  164. * space with the elevator data.
  165. */
  166. union {
  167. struct {
  168. struct io_cq *icq;
  169. void *priv[2];
  170. } elv;
  171. struct {
  172. unsigned int seq;
  173. struct list_head list;
  174. rq_end_io_fn *saved_end_io;
  175. } flush;
  176. };
  177. struct gendisk *rq_disk;
  178. struct hd_struct *part;
  179. unsigned long start_time;
  180. struct blk_issue_stat issue_stat;
  181. #ifdef CONFIG_BLK_CGROUP
  182. struct request_list *rl; /* rl this rq is alloced from */
  183. unsigned long long start_time_ns;
  184. unsigned long long io_start_time_ns; /* when passed to hardware */
  185. #endif
  186. /* Number of scatter-gather DMA addr+len pairs after
  187. * physical address coalescing is performed.
  188. */
  189. unsigned short nr_phys_segments;
  190. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  191. unsigned short nr_integrity_segments;
  192. #endif
  193. unsigned short ioprio;
  194. unsigned int timeout;
  195. void *special; /* opaque pointer available for LLD use */
  196. unsigned int extra_len; /* length of alignment and padding */
  197. unsigned short write_hint;
  198. unsigned long deadline;
  199. struct list_head timeout_list;
  200. /*
  201. * completion callback.
  202. */
  203. rq_end_io_fn *end_io;
  204. void *end_io_data;
  205. /* for bidi */
  206. struct request *next_rq;
  207. };
  208. static inline bool blk_rq_is_scsi(struct request *rq)
  209. {
  210. return req_op(rq) == REQ_OP_SCSI_IN || req_op(rq) == REQ_OP_SCSI_OUT;
  211. }
  212. static inline bool blk_rq_is_private(struct request *rq)
  213. {
  214. return req_op(rq) == REQ_OP_DRV_IN || req_op(rq) == REQ_OP_DRV_OUT;
  215. }
  216. static inline bool blk_rq_is_passthrough(struct request *rq)
  217. {
  218. return blk_rq_is_scsi(rq) || blk_rq_is_private(rq);
  219. }
  220. static inline unsigned short req_get_ioprio(struct request *req)
  221. {
  222. return req->ioprio;
  223. }
  224. #include <linux/elevator.h>
  225. struct blk_queue_ctx;
  226. typedef void (request_fn_proc) (struct request_queue *q);
  227. typedef blk_qc_t (make_request_fn) (struct request_queue *q, struct bio *bio);
  228. typedef int (prep_rq_fn) (struct request_queue *, struct request *);
  229. typedef void (unprep_rq_fn) (struct request_queue *, struct request *);
  230. struct bio_vec;
  231. typedef void (softirq_done_fn)(struct request *);
  232. typedef int (dma_drain_needed_fn)(struct request *);
  233. typedef int (lld_busy_fn) (struct request_queue *q);
  234. typedef int (bsg_job_fn) (struct bsg_job *);
  235. typedef int (init_rq_fn)(struct request_queue *, struct request *, gfp_t);
  236. typedef void (exit_rq_fn)(struct request_queue *, struct request *);
  237. enum blk_eh_timer_return {
  238. BLK_EH_NOT_HANDLED,
  239. BLK_EH_HANDLED,
  240. BLK_EH_RESET_TIMER,
  241. };
  242. typedef enum blk_eh_timer_return (rq_timed_out_fn)(struct request *);
  243. enum blk_queue_state {
  244. Queue_down,
  245. Queue_up,
  246. };
  247. struct blk_queue_tag {
  248. struct request **tag_index; /* map of busy tags */
  249. unsigned long *tag_map; /* bit map of free/busy tags */
  250. int max_depth; /* what we will send to device */
  251. int real_max_depth; /* what the array can hold */
  252. atomic_t refcnt; /* map can be shared */
  253. int alloc_policy; /* tag allocation policy */
  254. int next_tag; /* next tag */
  255. };
  256. #define BLK_TAG_ALLOC_FIFO 0 /* allocate starting from 0 */
  257. #define BLK_TAG_ALLOC_RR 1 /* allocate starting from last allocated tag */
  258. #define BLK_SCSI_MAX_CMDS (256)
  259. #define BLK_SCSI_CMD_PER_LONG (BLK_SCSI_MAX_CMDS / (sizeof(long) * 8))
  260. /*
  261. * Zoned block device models (zoned limit).
  262. */
  263. enum blk_zoned_model {
  264. BLK_ZONED_NONE, /* Regular block device */
  265. BLK_ZONED_HA, /* Host-aware zoned block device */
  266. BLK_ZONED_HM, /* Host-managed zoned block device */
  267. };
  268. struct queue_limits {
  269. unsigned long bounce_pfn;
  270. unsigned long seg_boundary_mask;
  271. unsigned long virt_boundary_mask;
  272. unsigned int max_hw_sectors;
  273. unsigned int max_dev_sectors;
  274. unsigned int chunk_sectors;
  275. unsigned int max_sectors;
  276. unsigned int max_segment_size;
  277. unsigned int physical_block_size;
  278. unsigned int alignment_offset;
  279. unsigned int io_min;
  280. unsigned int io_opt;
  281. unsigned int max_discard_sectors;
  282. unsigned int max_hw_discard_sectors;
  283. unsigned int max_write_same_sectors;
  284. unsigned int max_write_zeroes_sectors;
  285. unsigned int discard_granularity;
  286. unsigned int discard_alignment;
  287. unsigned short logical_block_size;
  288. unsigned short max_segments;
  289. unsigned short max_integrity_segments;
  290. unsigned short max_discard_segments;
  291. unsigned char misaligned;
  292. unsigned char discard_misaligned;
  293. unsigned char cluster;
  294. unsigned char raid_partial_stripes_expensive;
  295. enum blk_zoned_model zoned;
  296. };
  297. #ifdef CONFIG_BLK_DEV_ZONED
  298. struct blk_zone_report_hdr {
  299. unsigned int nr_zones;
  300. u8 padding[60];
  301. };
  302. extern int blkdev_report_zones(struct block_device *bdev,
  303. sector_t sector, struct blk_zone *zones,
  304. unsigned int *nr_zones, gfp_t gfp_mask);
  305. extern int blkdev_reset_zones(struct block_device *bdev, sector_t sectors,
  306. sector_t nr_sectors, gfp_t gfp_mask);
  307. extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
  308. unsigned int cmd, unsigned long arg);
  309. extern int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
  310. unsigned int cmd, unsigned long arg);
  311. #else /* CONFIG_BLK_DEV_ZONED */
  312. static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
  313. fmode_t mode, unsigned int cmd,
  314. unsigned long arg)
  315. {
  316. return -ENOTTY;
  317. }
  318. static inline int blkdev_reset_zones_ioctl(struct block_device *bdev,
  319. fmode_t mode, unsigned int cmd,
  320. unsigned long arg)
  321. {
  322. return -ENOTTY;
  323. }
  324. #endif /* CONFIG_BLK_DEV_ZONED */
  325. struct request_queue {
  326. /*
  327. * Together with queue_head for cacheline sharing
  328. */
  329. struct list_head queue_head;
  330. struct request *last_merge;
  331. struct elevator_queue *elevator;
  332. int nr_rqs[2]; /* # allocated [a]sync rqs */
  333. int nr_rqs_elvpriv; /* # allocated rqs w/ elvpriv */
  334. atomic_t shared_hctx_restart;
  335. struct blk_queue_stats *stats;
  336. struct rq_wb *rq_wb;
  337. /*
  338. * If blkcg is not used, @q->root_rl serves all requests. If blkcg
  339. * is used, root blkg allocates from @q->root_rl and all other
  340. * blkgs from their own blkg->rl. Which one to use should be
  341. * determined using bio_request_list().
  342. */
  343. struct request_list root_rl;
  344. request_fn_proc *request_fn;
  345. make_request_fn *make_request_fn;
  346. prep_rq_fn *prep_rq_fn;
  347. unprep_rq_fn *unprep_rq_fn;
  348. softirq_done_fn *softirq_done_fn;
  349. rq_timed_out_fn *rq_timed_out_fn;
  350. dma_drain_needed_fn *dma_drain_needed;
  351. lld_busy_fn *lld_busy_fn;
  352. /* Called just after a request is allocated */
  353. init_rq_fn *init_rq_fn;
  354. /* Called just before a request is freed */
  355. exit_rq_fn *exit_rq_fn;
  356. /* Called from inside blk_get_request() */
  357. void (*initialize_rq_fn)(struct request *rq);
  358. const struct blk_mq_ops *mq_ops;
  359. unsigned int *mq_map;
  360. /* sw queues */
  361. struct blk_mq_ctx __percpu *queue_ctx;
  362. unsigned int nr_queues;
  363. unsigned int queue_depth;
  364. /* hw dispatch queues */
  365. struct blk_mq_hw_ctx **queue_hw_ctx;
  366. unsigned int nr_hw_queues;
  367. /*
  368. * Dispatch queue sorting
  369. */
  370. sector_t end_sector;
  371. struct request *boundary_rq;
  372. /*
  373. * Delayed queue handling
  374. */
  375. struct delayed_work delay_work;
  376. struct backing_dev_info *backing_dev_info;
  377. /*
  378. * The queue owner gets to use this for whatever they like.
  379. * ll_rw_blk doesn't touch it.
  380. */
  381. void *queuedata;
  382. /*
  383. * various queue flags, see QUEUE_* below
  384. */
  385. unsigned long queue_flags;
  386. /*
  387. * ida allocated id for this queue. Used to index queues from
  388. * ioctx.
  389. */
  390. int id;
  391. /*
  392. * queue needs bounce pages for pages above this limit
  393. */
  394. gfp_t bounce_gfp;
  395. /*
  396. * protects queue structures from reentrancy. ->__queue_lock should
  397. * _never_ be used directly, it is queue private. always use
  398. * ->queue_lock.
  399. */
  400. spinlock_t __queue_lock;
  401. spinlock_t *queue_lock;
  402. /*
  403. * queue kobject
  404. */
  405. struct kobject kobj;
  406. /*
  407. * mq queue kobject
  408. */
  409. struct kobject mq_kobj;
  410. #ifdef CONFIG_BLK_DEV_INTEGRITY
  411. struct blk_integrity integrity;
  412. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  413. #ifdef CONFIG_PM
  414. struct device *dev;
  415. int rpm_status;
  416. unsigned int nr_pending;
  417. #endif
  418. /*
  419. * queue settings
  420. */
  421. unsigned long nr_requests; /* Max # of requests */
  422. unsigned int nr_congestion_on;
  423. unsigned int nr_congestion_off;
  424. unsigned int nr_batching;
  425. unsigned int dma_drain_size;
  426. void *dma_drain_buffer;
  427. unsigned int dma_pad_mask;
  428. unsigned int dma_alignment;
  429. struct blk_queue_tag *queue_tags;
  430. struct list_head tag_busy_list;
  431. unsigned int nr_sorted;
  432. unsigned int in_flight[2];
  433. /*
  434. * Number of active block driver functions for which blk_drain_queue()
  435. * must wait. Must be incremented around functions that unlock the
  436. * queue_lock internally, e.g. scsi_request_fn().
  437. */
  438. unsigned int request_fn_active;
  439. unsigned int rq_timeout;
  440. int poll_nsec;
  441. struct blk_stat_callback *poll_cb;
  442. struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS];
  443. struct timer_list timeout;
  444. struct work_struct timeout_work;
  445. struct list_head timeout_list;
  446. struct list_head icq_list;
  447. #ifdef CONFIG_BLK_CGROUP
  448. DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
  449. struct blkcg_gq *root_blkg;
  450. struct list_head blkg_list;
  451. #endif
  452. struct queue_limits limits;
  453. /*
  454. * sg stuff
  455. */
  456. unsigned int sg_timeout;
  457. unsigned int sg_reserved_size;
  458. int node;
  459. #ifdef CONFIG_BLK_DEV_IO_TRACE
  460. struct blk_trace *blk_trace;
  461. #endif
  462. /*
  463. * for flush operations
  464. */
  465. struct blk_flush_queue *fq;
  466. struct list_head requeue_list;
  467. spinlock_t requeue_lock;
  468. struct delayed_work requeue_work;
  469. struct mutex sysfs_lock;
  470. int bypass_depth;
  471. atomic_t mq_freeze_depth;
  472. #if defined(CONFIG_BLK_DEV_BSG)
  473. bsg_job_fn *bsg_job_fn;
  474. int bsg_job_size;
  475. struct bsg_class_device bsg_dev;
  476. #endif
  477. #ifdef CONFIG_BLK_DEV_THROTTLING
  478. /* Throttle data */
  479. struct throtl_data *td;
  480. #endif
  481. struct rcu_head rcu_head;
  482. wait_queue_head_t mq_freeze_wq;
  483. struct percpu_ref q_usage_counter;
  484. struct list_head all_q_node;
  485. struct blk_mq_tag_set *tag_set;
  486. struct list_head tag_set_list;
  487. struct bio_set *bio_split;
  488. #ifdef CONFIG_BLK_DEBUG_FS
  489. struct dentry *debugfs_dir;
  490. struct dentry *sched_debugfs_dir;
  491. #endif
  492. bool mq_sysfs_init_done;
  493. size_t cmd_size;
  494. void *rq_alloc_data;
  495. struct work_struct release_work;
  496. };
  497. #define QUEUE_FLAG_QUEUED 1 /* uses generic tag queueing */
  498. #define QUEUE_FLAG_STOPPED 2 /* queue is stopped */
  499. #define QUEUE_FLAG_SYNCFULL 3 /* read queue has been filled */
  500. #define QUEUE_FLAG_ASYNCFULL 4 /* write queue has been filled */
  501. #define QUEUE_FLAG_DYING 5 /* queue being torn down */
  502. #define QUEUE_FLAG_BYPASS 6 /* act as dumb FIFO queue */
  503. #define QUEUE_FLAG_BIDI 7 /* queue supports bidi requests */
  504. #define QUEUE_FLAG_NOMERGES 8 /* disable merge attempts */
  505. #define QUEUE_FLAG_SAME_COMP 9 /* complete on same CPU-group */
  506. #define QUEUE_FLAG_FAIL_IO 10 /* fake timeout */
  507. #define QUEUE_FLAG_STACKABLE 11 /* supports request stacking */
  508. #define QUEUE_FLAG_NONROT 12 /* non-rotational device (SSD) */
  509. #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */
  510. #define QUEUE_FLAG_IO_STAT 13 /* do IO stats */
  511. #define QUEUE_FLAG_DISCARD 14 /* supports DISCARD */
  512. #define QUEUE_FLAG_NOXMERGES 15 /* No extended merges */
  513. #define QUEUE_FLAG_ADD_RANDOM 16 /* Contributes to random pool */
  514. #define QUEUE_FLAG_SECERASE 17 /* supports secure erase */
  515. #define QUEUE_FLAG_SAME_FORCE 18 /* force complete on same CPU */
  516. #define QUEUE_FLAG_DEAD 19 /* queue tear-down finished */
  517. #define QUEUE_FLAG_INIT_DONE 20 /* queue is initialized */
  518. #define QUEUE_FLAG_NO_SG_MERGE 21 /* don't attempt to merge SG segments*/
  519. #define QUEUE_FLAG_POLL 22 /* IO polling enabled if set */
  520. #define QUEUE_FLAG_WC 23 /* Write back caching */
  521. #define QUEUE_FLAG_FUA 24 /* device supports FUA writes */
  522. #define QUEUE_FLAG_FLUSH_NQ 25 /* flush not queueuable */
  523. #define QUEUE_FLAG_DAX 26 /* device supports DAX */
  524. #define QUEUE_FLAG_STATS 27 /* track rq completion times */
  525. #define QUEUE_FLAG_POLL_STATS 28 /* collecting stats for hybrid polling */
  526. #define QUEUE_FLAG_REGISTERED 29 /* queue has been registered to a disk */
  527. #define QUEUE_FLAG_SCSI_PASSTHROUGH 30 /* queue supports SCSI commands */
  528. #define QUEUE_FLAG_QUIESCED 31 /* queue has been quiesced */
  529. #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
  530. (1 << QUEUE_FLAG_STACKABLE) | \
  531. (1 << QUEUE_FLAG_SAME_COMP) | \
  532. (1 << QUEUE_FLAG_ADD_RANDOM))
  533. #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
  534. (1 << QUEUE_FLAG_STACKABLE) | \
  535. (1 << QUEUE_FLAG_SAME_COMP) | \
  536. (1 << QUEUE_FLAG_POLL))
  537. /*
  538. * @q->queue_lock is set while a queue is being initialized. Since we know
  539. * that no other threads access the queue object before @q->queue_lock has
  540. * been set, it is safe to manipulate queue flags without holding the
  541. * queue_lock if @q->queue_lock == NULL. See also blk_alloc_queue_node() and
  542. * blk_init_allocated_queue().
  543. */
  544. static inline void queue_lockdep_assert_held(struct request_queue *q)
  545. {
  546. if (q->queue_lock)
  547. lockdep_assert_held(q->queue_lock);
  548. }
  549. static inline void queue_flag_set_unlocked(unsigned int flag,
  550. struct request_queue *q)
  551. {
  552. __set_bit(flag, &q->queue_flags);
  553. }
  554. static inline int queue_flag_test_and_clear(unsigned int flag,
  555. struct request_queue *q)
  556. {
  557. queue_lockdep_assert_held(q);
  558. if (test_bit(flag, &q->queue_flags)) {
  559. __clear_bit(flag, &q->queue_flags);
  560. return 1;
  561. }
  562. return 0;
  563. }
  564. static inline int queue_flag_test_and_set(unsigned int flag,
  565. struct request_queue *q)
  566. {
  567. queue_lockdep_assert_held(q);
  568. if (!test_bit(flag, &q->queue_flags)) {
  569. __set_bit(flag, &q->queue_flags);
  570. return 0;
  571. }
  572. return 1;
  573. }
  574. static inline void queue_flag_set(unsigned int flag, struct request_queue *q)
  575. {
  576. queue_lockdep_assert_held(q);
  577. __set_bit(flag, &q->queue_flags);
  578. }
  579. static inline void queue_flag_clear_unlocked(unsigned int flag,
  580. struct request_queue *q)
  581. {
  582. __clear_bit(flag, &q->queue_flags);
  583. }
  584. static inline int queue_in_flight(struct request_queue *q)
  585. {
  586. return q->in_flight[0] + q->in_flight[1];
  587. }
  588. static inline void queue_flag_clear(unsigned int flag, struct request_queue *q)
  589. {
  590. queue_lockdep_assert_held(q);
  591. __clear_bit(flag, &q->queue_flags);
  592. }
  593. #define blk_queue_tagged(q) test_bit(QUEUE_FLAG_QUEUED, &(q)->queue_flags)
  594. #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
  595. #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
  596. #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags)
  597. #define blk_queue_bypass(q) test_bit(QUEUE_FLAG_BYPASS, &(q)->queue_flags)
  598. #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
  599. #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
  600. #define blk_queue_noxmerges(q) \
  601. test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
  602. #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
  603. #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
  604. #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
  605. #define blk_queue_stackable(q) \
  606. test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
  607. #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
  608. #define blk_queue_secure_erase(q) \
  609. (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags))
  610. #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
  611. #define blk_queue_scsi_passthrough(q) \
  612. test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags)
  613. #define blk_noretry_request(rq) \
  614. ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
  615. REQ_FAILFAST_DRIVER))
  616. #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
  617. static inline bool blk_account_rq(struct request *rq)
  618. {
  619. return (rq->rq_flags & RQF_STARTED) && !blk_rq_is_passthrough(rq);
  620. }
  621. #define blk_rq_cpu_valid(rq) ((rq)->cpu != -1)
  622. #define blk_bidi_rq(rq) ((rq)->next_rq != NULL)
  623. /* rq->queuelist of dequeued request must be list_empty() */
  624. #define blk_queued_rq(rq) (!list_empty(&(rq)->queuelist))
  625. #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
  626. #define rq_data_dir(rq) (op_is_write(req_op(rq)) ? WRITE : READ)
  627. /*
  628. * Driver can handle struct request, if it either has an old style
  629. * request_fn defined, or is blk-mq based.
  630. */
  631. static inline bool queue_is_rq_based(struct request_queue *q)
  632. {
  633. return q->request_fn || q->mq_ops;
  634. }
  635. static inline unsigned int blk_queue_cluster(struct request_queue *q)
  636. {
  637. return q->limits.cluster;
  638. }
  639. static inline enum blk_zoned_model
  640. blk_queue_zoned_model(struct request_queue *q)
  641. {
  642. return q->limits.zoned;
  643. }
  644. static inline bool blk_queue_is_zoned(struct request_queue *q)
  645. {
  646. switch (blk_queue_zoned_model(q)) {
  647. case BLK_ZONED_HA:
  648. case BLK_ZONED_HM:
  649. return true;
  650. default:
  651. return false;
  652. }
  653. }
  654. static inline unsigned int blk_queue_zone_sectors(struct request_queue *q)
  655. {
  656. return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0;
  657. }
  658. static inline bool rq_is_sync(struct request *rq)
  659. {
  660. return op_is_sync(rq->cmd_flags);
  661. }
  662. static inline bool blk_rl_full(struct request_list *rl, bool sync)
  663. {
  664. unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
  665. return rl->flags & flag;
  666. }
  667. static inline void blk_set_rl_full(struct request_list *rl, bool sync)
  668. {
  669. unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
  670. rl->flags |= flag;
  671. }
  672. static inline void blk_clear_rl_full(struct request_list *rl, bool sync)
  673. {
  674. unsigned int flag = sync ? BLK_RL_SYNCFULL : BLK_RL_ASYNCFULL;
  675. rl->flags &= ~flag;
  676. }
  677. static inline bool rq_mergeable(struct request *rq)
  678. {
  679. if (blk_rq_is_passthrough(rq))
  680. return false;
  681. if (req_op(rq) == REQ_OP_FLUSH)
  682. return false;
  683. if (req_op(rq) == REQ_OP_WRITE_ZEROES)
  684. return false;
  685. if (rq->cmd_flags & REQ_NOMERGE_FLAGS)
  686. return false;
  687. if (rq->rq_flags & RQF_NOMERGE_FLAGS)
  688. return false;
  689. return true;
  690. }
  691. static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
  692. {
  693. if (bio_page(a) == bio_page(b) &&
  694. bio_offset(a) == bio_offset(b))
  695. return true;
  696. return false;
  697. }
  698. static inline unsigned int blk_queue_depth(struct request_queue *q)
  699. {
  700. if (q->queue_depth)
  701. return q->queue_depth;
  702. return q->nr_requests;
  703. }
  704. /*
  705. * q->prep_rq_fn return values
  706. */
  707. enum {
  708. BLKPREP_OK, /* serve it */
  709. BLKPREP_KILL, /* fatal error, kill, return -EIO */
  710. BLKPREP_DEFER, /* leave on queue */
  711. BLKPREP_INVALID, /* invalid command, kill, return -EREMOTEIO */
  712. };
  713. extern unsigned long blk_max_low_pfn, blk_max_pfn;
  714. /*
  715. * standard bounce addresses:
  716. *
  717. * BLK_BOUNCE_HIGH : bounce all highmem pages
  718. * BLK_BOUNCE_ANY : don't bounce anything
  719. * BLK_BOUNCE_ISA : bounce pages above ISA DMA boundary
  720. */
  721. #if BITS_PER_LONG == 32
  722. #define BLK_BOUNCE_HIGH ((u64)blk_max_low_pfn << PAGE_SHIFT)
  723. #else
  724. #define BLK_BOUNCE_HIGH -1ULL
  725. #endif
  726. #define BLK_BOUNCE_ANY (-1ULL)
  727. #define BLK_BOUNCE_ISA (DMA_BIT_MASK(24))
  728. /*
  729. * default timeout for SG_IO if none specified
  730. */
  731. #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
  732. #define BLK_MIN_SG_TIMEOUT (7 * HZ)
  733. #ifdef CONFIG_BOUNCE
  734. extern int init_emergency_isa_pool(void);
  735. extern void blk_queue_bounce(struct request_queue *q, struct bio **bio);
  736. #else
  737. static inline int init_emergency_isa_pool(void)
  738. {
  739. return 0;
  740. }
  741. static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
  742. {
  743. }
  744. #endif /* CONFIG_MMU */
  745. struct rq_map_data {
  746. struct page **pages;
  747. int page_order;
  748. int nr_entries;
  749. unsigned long offset;
  750. int null_mapped;
  751. int from_user;
  752. };
  753. struct req_iterator {
  754. struct bvec_iter iter;
  755. struct bio *bio;
  756. };
  757. /* This should not be used directly - use rq_for_each_segment */
  758. #define for_each_bio(_bio) \
  759. for (; _bio; _bio = _bio->bi_next)
  760. #define __rq_for_each_bio(_bio, rq) \
  761. if ((rq->bio)) \
  762. for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
  763. #define rq_for_each_segment(bvl, _rq, _iter) \
  764. __rq_for_each_bio(_iter.bio, _rq) \
  765. bio_for_each_segment(bvl, _iter.bio, _iter.iter)
  766. #define rq_iter_last(bvec, _iter) \
  767. (_iter.bio->bi_next == NULL && \
  768. bio_iter_last(bvec, _iter.iter))
  769. #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  770. # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
  771. #endif
  772. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  773. extern void rq_flush_dcache_pages(struct request *rq);
  774. #else
  775. static inline void rq_flush_dcache_pages(struct request *rq)
  776. {
  777. }
  778. #endif
  779. #ifdef CONFIG_PRINTK
  780. #define vfs_msg(sb, level, fmt, ...) \
  781. __vfs_msg(sb, level, fmt, ##__VA_ARGS__)
  782. #else
  783. #define vfs_msg(sb, level, fmt, ...) \
  784. do { \
  785. no_printk(fmt, ##__VA_ARGS__); \
  786. __vfs_msg(sb, "", " "); \
  787. } while (0)
  788. #endif
  789. extern int blk_register_queue(struct gendisk *disk);
  790. extern void blk_unregister_queue(struct gendisk *disk);
  791. extern blk_qc_t generic_make_request(struct bio *bio);
  792. extern void blk_rq_init(struct request_queue *q, struct request *rq);
  793. extern void blk_init_request_from_bio(struct request *req, struct bio *bio);
  794. extern void blk_put_request(struct request *);
  795. extern void __blk_put_request(struct request_queue *, struct request *);
  796. extern struct request *blk_get_request(struct request_queue *, unsigned int op,
  797. gfp_t gfp_mask);
  798. extern void blk_requeue_request(struct request_queue *, struct request *);
  799. extern int blk_lld_busy(struct request_queue *q);
  800. extern int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
  801. struct bio_set *bs, gfp_t gfp_mask,
  802. int (*bio_ctr)(struct bio *, struct bio *, void *),
  803. void *data);
  804. extern void blk_rq_unprep_clone(struct request *rq);
  805. extern blk_status_t blk_insert_cloned_request(struct request_queue *q,
  806. struct request *rq);
  807. extern int blk_rq_append_bio(struct request *rq, struct bio *bio);
  808. extern void blk_delay_queue(struct request_queue *, unsigned long);
  809. extern void blk_queue_split(struct request_queue *, struct bio **);
  810. extern void blk_recount_segments(struct request_queue *, struct bio *);
  811. extern int scsi_verify_blk_ioctl(struct block_device *, unsigned int);
  812. extern int scsi_cmd_blk_ioctl(struct block_device *, fmode_t,
  813. unsigned int, void __user *);
  814. extern int scsi_cmd_ioctl(struct request_queue *, struct gendisk *, fmode_t,
  815. unsigned int, void __user *);
  816. extern int sg_scsi_ioctl(struct request_queue *, struct gendisk *, fmode_t,
  817. struct scsi_ioctl_command __user *);
  818. extern int blk_queue_enter(struct request_queue *q, bool nowait);
  819. extern void blk_queue_exit(struct request_queue *q);
  820. extern void blk_start_queue(struct request_queue *q);
  821. extern void blk_start_queue_async(struct request_queue *q);
  822. extern void blk_stop_queue(struct request_queue *q);
  823. extern void blk_sync_queue(struct request_queue *q);
  824. extern void __blk_stop_queue(struct request_queue *q);
  825. extern void __blk_run_queue(struct request_queue *q);
  826. extern void __blk_run_queue_uncond(struct request_queue *q);
  827. extern void blk_run_queue(struct request_queue *);
  828. extern void blk_run_queue_async(struct request_queue *q);
  829. extern int blk_rq_map_user(struct request_queue *, struct request *,
  830. struct rq_map_data *, void __user *, unsigned long,
  831. gfp_t);
  832. extern int blk_rq_unmap_user(struct bio *);
  833. extern int blk_rq_map_kern(struct request_queue *, struct request *, void *, unsigned int, gfp_t);
  834. extern int blk_rq_map_user_iov(struct request_queue *, struct request *,
  835. struct rq_map_data *, const struct iov_iter *,
  836. gfp_t);
  837. extern void blk_execute_rq(struct request_queue *, struct gendisk *,
  838. struct request *, int);
  839. extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
  840. struct request *, int, rq_end_io_fn *);
  841. int blk_status_to_errno(blk_status_t status);
  842. blk_status_t errno_to_blk_status(int errno);
  843. bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie);
  844. static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
  845. {
  846. return bdev->bd_disk->queue; /* this is never NULL */
  847. }
  848. /*
  849. * blk_rq_pos() : the current sector
  850. * blk_rq_bytes() : bytes left in the entire request
  851. * blk_rq_cur_bytes() : bytes left in the current segment
  852. * blk_rq_err_bytes() : bytes left till the next error boundary
  853. * blk_rq_sectors() : sectors left in the entire request
  854. * blk_rq_cur_sectors() : sectors left in the current segment
  855. */
  856. static inline sector_t blk_rq_pos(const struct request *rq)
  857. {
  858. return rq->__sector;
  859. }
  860. static inline unsigned int blk_rq_bytes(const struct request *rq)
  861. {
  862. return rq->__data_len;
  863. }
  864. static inline int blk_rq_cur_bytes(const struct request *rq)
  865. {
  866. return rq->bio ? bio_cur_bytes(rq->bio) : 0;
  867. }
  868. extern unsigned int blk_rq_err_bytes(const struct request *rq);
  869. static inline unsigned int blk_rq_sectors(const struct request *rq)
  870. {
  871. return blk_rq_bytes(rq) >> 9;
  872. }
  873. static inline unsigned int blk_rq_cur_sectors(const struct request *rq)
  874. {
  875. return blk_rq_cur_bytes(rq) >> 9;
  876. }
  877. /*
  878. * Some commands like WRITE SAME have a payload or data transfer size which
  879. * is different from the size of the request. Any driver that supports such
  880. * commands using the RQF_SPECIAL_PAYLOAD flag needs to use this helper to
  881. * calculate the data transfer size.
  882. */
  883. static inline unsigned int blk_rq_payload_bytes(struct request *rq)
  884. {
  885. if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
  886. return rq->special_vec.bv_len;
  887. return blk_rq_bytes(rq);
  888. }
  889. static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
  890. int op)
  891. {
  892. if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
  893. return min(q->limits.max_discard_sectors, UINT_MAX >> 9);
  894. if (unlikely(op == REQ_OP_WRITE_SAME))
  895. return q->limits.max_write_same_sectors;
  896. if (unlikely(op == REQ_OP_WRITE_ZEROES))
  897. return q->limits.max_write_zeroes_sectors;
  898. return q->limits.max_sectors;
  899. }
  900. /*
  901. * Return maximum size of a request at given offset. Only valid for
  902. * file system requests.
  903. */
  904. static inline unsigned int blk_max_size_offset(struct request_queue *q,
  905. sector_t offset)
  906. {
  907. if (!q->limits.chunk_sectors)
  908. return q->limits.max_sectors;
  909. return q->limits.chunk_sectors -
  910. (offset & (q->limits.chunk_sectors - 1));
  911. }
  912. static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
  913. sector_t offset)
  914. {
  915. struct request_queue *q = rq->q;
  916. if (blk_rq_is_passthrough(rq))
  917. return q->limits.max_hw_sectors;
  918. if (!q->limits.chunk_sectors ||
  919. req_op(rq) == REQ_OP_DISCARD ||
  920. req_op(rq) == REQ_OP_SECURE_ERASE)
  921. return blk_queue_get_max_sectors(q, req_op(rq));
  922. return min(blk_max_size_offset(q, offset),
  923. blk_queue_get_max_sectors(q, req_op(rq)));
  924. }
  925. static inline unsigned int blk_rq_count_bios(struct request *rq)
  926. {
  927. unsigned int nr_bios = 0;
  928. struct bio *bio;
  929. __rq_for_each_bio(bio, rq)
  930. nr_bios++;
  931. return nr_bios;
  932. }
  933. /*
  934. * Request issue related functions.
  935. */
  936. extern struct request *blk_peek_request(struct request_queue *q);
  937. extern void blk_start_request(struct request *rq);
  938. extern struct request *blk_fetch_request(struct request_queue *q);
  939. /*
  940. * Request completion related functions.
  941. *
  942. * blk_update_request() completes given number of bytes and updates
  943. * the request without completing it.
  944. *
  945. * blk_end_request() and friends. __blk_end_request() must be called
  946. * with the request queue spinlock acquired.
  947. *
  948. * Several drivers define their own end_request and call
  949. * blk_end_request() for parts of the original function.
  950. * This prevents code duplication in drivers.
  951. */
  952. extern bool blk_update_request(struct request *rq, blk_status_t error,
  953. unsigned int nr_bytes);
  954. extern void blk_finish_request(struct request *rq, blk_status_t error);
  955. extern bool blk_end_request(struct request *rq, blk_status_t error,
  956. unsigned int nr_bytes);
  957. extern void blk_end_request_all(struct request *rq, blk_status_t error);
  958. extern bool __blk_end_request(struct request *rq, blk_status_t error,
  959. unsigned int nr_bytes);
  960. extern void __blk_end_request_all(struct request *rq, blk_status_t error);
  961. extern bool __blk_end_request_cur(struct request *rq, blk_status_t error);
  962. extern void blk_complete_request(struct request *);
  963. extern void __blk_complete_request(struct request *);
  964. extern void blk_abort_request(struct request *);
  965. extern void blk_unprep_request(struct request *);
  966. /*
  967. * Access functions for manipulating queue properties
  968. */
  969. extern struct request_queue *blk_init_queue_node(request_fn_proc *rfn,
  970. spinlock_t *lock, int node_id);
  971. extern struct request_queue *blk_init_queue(request_fn_proc *, spinlock_t *);
  972. extern int blk_init_allocated_queue(struct request_queue *);
  973. extern void blk_cleanup_queue(struct request_queue *);
  974. extern void blk_queue_make_request(struct request_queue *, make_request_fn *);
  975. extern void blk_queue_bounce_limit(struct request_queue *, u64);
  976. extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int);
  977. extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
  978. extern void blk_queue_max_segments(struct request_queue *, unsigned short);
  979. extern void blk_queue_max_discard_segments(struct request_queue *,
  980. unsigned short);
  981. extern void blk_queue_max_segment_size(struct request_queue *, unsigned int);
  982. extern void blk_queue_max_discard_sectors(struct request_queue *q,
  983. unsigned int max_discard_sectors);
  984. extern void blk_queue_max_write_same_sectors(struct request_queue *q,
  985. unsigned int max_write_same_sectors);
  986. extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
  987. unsigned int max_write_same_sectors);
  988. extern void blk_queue_logical_block_size(struct request_queue *, unsigned short);
  989. extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
  990. extern void blk_queue_alignment_offset(struct request_queue *q,
  991. unsigned int alignment);
  992. extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
  993. extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
  994. extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
  995. extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
  996. extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
  997. extern void blk_set_default_limits(struct queue_limits *lim);
  998. extern void blk_set_stacking_limits(struct queue_limits *lim);
  999. extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
  1000. sector_t offset);
  1001. extern int bdev_stack_limits(struct queue_limits *t, struct block_device *bdev,
  1002. sector_t offset);
  1003. extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev,
  1004. sector_t offset);
  1005. extern void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b);
  1006. extern void blk_queue_dma_pad(struct request_queue *, unsigned int);
  1007. extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
  1008. extern int blk_queue_dma_drain(struct request_queue *q,
  1009. dma_drain_needed_fn *dma_drain_needed,
  1010. void *buf, unsigned int size);
  1011. extern void blk_queue_lld_busy(struct request_queue *q, lld_busy_fn *fn);
  1012. extern void blk_queue_segment_boundary(struct request_queue *, unsigned long);
  1013. extern void blk_queue_virt_boundary(struct request_queue *, unsigned long);
  1014. extern void blk_queue_prep_rq(struct request_queue *, prep_rq_fn *pfn);
  1015. extern void blk_queue_unprep_rq(struct request_queue *, unprep_rq_fn *ufn);
  1016. extern void blk_queue_dma_alignment(struct request_queue *, int);
  1017. extern void blk_queue_update_dma_alignment(struct request_queue *, int);
  1018. extern void blk_queue_softirq_done(struct request_queue *, softirq_done_fn *);
  1019. extern void blk_queue_rq_timed_out(struct request_queue *, rq_timed_out_fn *);
  1020. extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
  1021. extern void blk_queue_flush_queueable(struct request_queue *q, bool queueable);
  1022. extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
  1023. /*
  1024. * Number of physical segments as sent to the device.
  1025. *
  1026. * Normally this is the number of discontiguous data segments sent by the
  1027. * submitter. But for data-less command like discard we might have no
  1028. * actual data segments submitted, but the driver might have to add it's
  1029. * own special payload. In that case we still return 1 here so that this
  1030. * special payload will be mapped.
  1031. */
  1032. static inline unsigned short blk_rq_nr_phys_segments(struct request *rq)
  1033. {
  1034. if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
  1035. return 1;
  1036. return rq->nr_phys_segments;
  1037. }
  1038. /*
  1039. * Number of discard segments (or ranges) the driver needs to fill in.
  1040. * Each discard bio merged into a request is counted as one segment.
  1041. */
  1042. static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
  1043. {
  1044. return max_t(unsigned short, rq->nr_phys_segments, 1);
  1045. }
  1046. extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
  1047. extern void blk_dump_rq_flags(struct request *, char *);
  1048. extern long nr_blockdev_pages(void);
  1049. bool __must_check blk_get_queue(struct request_queue *);
  1050. struct request_queue *blk_alloc_queue(gfp_t);
  1051. struct request_queue *blk_alloc_queue_node(gfp_t, int);
  1052. extern void blk_put_queue(struct request_queue *);
  1053. extern void blk_set_queue_dying(struct request_queue *);
  1054. /*
  1055. * block layer runtime pm functions
  1056. */
  1057. #ifdef CONFIG_PM
  1058. extern void blk_pm_runtime_init(struct request_queue *q, struct device *dev);
  1059. extern int blk_pre_runtime_suspend(struct request_queue *q);
  1060. extern void blk_post_runtime_suspend(struct request_queue *q, int err);
  1061. extern void blk_pre_runtime_resume(struct request_queue *q);
  1062. extern void blk_post_runtime_resume(struct request_queue *q, int err);
  1063. extern void blk_set_runtime_active(struct request_queue *q);
  1064. #else
  1065. static inline void blk_pm_runtime_init(struct request_queue *q,
  1066. struct device *dev) {}
  1067. static inline int blk_pre_runtime_suspend(struct request_queue *q)
  1068. {
  1069. return -ENOSYS;
  1070. }
  1071. static inline void blk_post_runtime_suspend(struct request_queue *q, int err) {}
  1072. static inline void blk_pre_runtime_resume(struct request_queue *q) {}
  1073. static inline void blk_post_runtime_resume(struct request_queue *q, int err) {}
  1074. static inline void blk_set_runtime_active(struct request_queue *q) {}
  1075. #endif
  1076. /*
  1077. * blk_plug permits building a queue of related requests by holding the I/O
  1078. * fragments for a short period. This allows merging of sequential requests
  1079. * into single larger request. As the requests are moved from a per-task list to
  1080. * the device's request_queue in a batch, this results in improved scalability
  1081. * as the lock contention for request_queue lock is reduced.
  1082. *
  1083. * It is ok not to disable preemption when adding the request to the plug list
  1084. * or when attempting a merge, because blk_schedule_flush_list() will only flush
  1085. * the plug list when the task sleeps by itself. For details, please see
  1086. * schedule() where blk_schedule_flush_plug() is called.
  1087. */
  1088. struct blk_plug {
  1089. struct list_head list; /* requests */
  1090. struct list_head mq_list; /* blk-mq requests */
  1091. struct list_head cb_list; /* md requires an unplug callback */
  1092. };
  1093. #define BLK_MAX_REQUEST_COUNT 16
  1094. #define BLK_PLUG_FLUSH_SIZE (128 * 1024)
  1095. struct blk_plug_cb;
  1096. typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
  1097. struct blk_plug_cb {
  1098. struct list_head list;
  1099. blk_plug_cb_fn callback;
  1100. void *data;
  1101. };
  1102. extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
  1103. void *data, int size);
  1104. extern void blk_start_plug(struct blk_plug *);
  1105. extern void blk_finish_plug(struct blk_plug *);
  1106. extern void blk_flush_plug_list(struct blk_plug *, bool);
  1107. static inline void blk_flush_plug(struct task_struct *tsk)
  1108. {
  1109. struct blk_plug *plug = tsk->plug;
  1110. if (plug)
  1111. blk_flush_plug_list(plug, false);
  1112. }
  1113. static inline void blk_schedule_flush_plug(struct task_struct *tsk)
  1114. {
  1115. struct blk_plug *plug = tsk->plug;
  1116. if (plug)
  1117. blk_flush_plug_list(plug, true);
  1118. }
  1119. static inline bool blk_needs_flush_plug(struct task_struct *tsk)
  1120. {
  1121. struct blk_plug *plug = tsk->plug;
  1122. return plug &&
  1123. (!list_empty(&plug->list) ||
  1124. !list_empty(&plug->mq_list) ||
  1125. !list_empty(&plug->cb_list));
  1126. }
  1127. /*
  1128. * tag stuff
  1129. */
  1130. extern int blk_queue_start_tag(struct request_queue *, struct request *);
  1131. extern struct request *blk_queue_find_tag(struct request_queue *, int);
  1132. extern void blk_queue_end_tag(struct request_queue *, struct request *);
  1133. extern int blk_queue_init_tags(struct request_queue *, int, struct blk_queue_tag *, int);
  1134. extern void blk_queue_free_tags(struct request_queue *);
  1135. extern int blk_queue_resize_tags(struct request_queue *, int);
  1136. extern void blk_queue_invalidate_tags(struct request_queue *);
  1137. extern struct blk_queue_tag *blk_init_tags(int, int);
  1138. extern void blk_free_tags(struct blk_queue_tag *);
  1139. static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt,
  1140. int tag)
  1141. {
  1142. if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
  1143. return NULL;
  1144. return bqt->tag_index[tag];
  1145. }
  1146. extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *);
  1147. extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
  1148. sector_t nr_sects, gfp_t gfp_mask, struct page *page);
  1149. #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */
  1150. extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  1151. sector_t nr_sects, gfp_t gfp_mask, unsigned long flags);
  1152. extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  1153. sector_t nr_sects, gfp_t gfp_mask, int flags,
  1154. struct bio **biop);
  1155. #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
  1156. #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
  1157. extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  1158. sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
  1159. unsigned flags);
  1160. extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
  1161. sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
  1162. static inline int sb_issue_discard(struct super_block *sb, sector_t block,
  1163. sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
  1164. {
  1165. return blkdev_issue_discard(sb->s_bdev, block << (sb->s_blocksize_bits - 9),
  1166. nr_blocks << (sb->s_blocksize_bits - 9),
  1167. gfp_mask, flags);
  1168. }
  1169. static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
  1170. sector_t nr_blocks, gfp_t gfp_mask)
  1171. {
  1172. return blkdev_issue_zeroout(sb->s_bdev,
  1173. block << (sb->s_blocksize_bits - 9),
  1174. nr_blocks << (sb->s_blocksize_bits - 9),
  1175. gfp_mask, 0);
  1176. }
  1177. extern int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm);
  1178. enum blk_default_limits {
  1179. BLK_MAX_SEGMENTS = 128,
  1180. BLK_SAFE_MAX_SECTORS = 255,
  1181. BLK_DEF_MAX_SECTORS = 2560,
  1182. BLK_MAX_SEGMENT_SIZE = 65536,
  1183. BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
  1184. };
  1185. #define blkdev_entry_to_request(entry) list_entry((entry), struct request, queuelist)
  1186. static inline unsigned long queue_bounce_pfn(struct request_queue *q)
  1187. {
  1188. return q->limits.bounce_pfn;
  1189. }
  1190. static inline unsigned long queue_segment_boundary(struct request_queue *q)
  1191. {
  1192. return q->limits.seg_boundary_mask;
  1193. }
  1194. static inline unsigned long queue_virt_boundary(struct request_queue *q)
  1195. {
  1196. return q->limits.virt_boundary_mask;
  1197. }
  1198. static inline unsigned int queue_max_sectors(struct request_queue *q)
  1199. {
  1200. return q->limits.max_sectors;
  1201. }
  1202. static inline unsigned int queue_max_hw_sectors(struct request_queue *q)
  1203. {
  1204. return q->limits.max_hw_sectors;
  1205. }
  1206. static inline unsigned short queue_max_segments(struct request_queue *q)
  1207. {
  1208. return q->limits.max_segments;
  1209. }
  1210. static inline unsigned short queue_max_discard_segments(struct request_queue *q)
  1211. {
  1212. return q->limits.max_discard_segments;
  1213. }
  1214. static inline unsigned int queue_max_segment_size(struct request_queue *q)
  1215. {
  1216. return q->limits.max_segment_size;
  1217. }
  1218. static inline unsigned short queue_logical_block_size(struct request_queue *q)
  1219. {
  1220. int retval = 512;
  1221. if (q && q->limits.logical_block_size)
  1222. retval = q->limits.logical_block_size;
  1223. return retval;
  1224. }
  1225. static inline unsigned short bdev_logical_block_size(struct block_device *bdev)
  1226. {
  1227. return queue_logical_block_size(bdev_get_queue(bdev));
  1228. }
  1229. static inline unsigned int queue_physical_block_size(struct request_queue *q)
  1230. {
  1231. return q->limits.physical_block_size;
  1232. }
  1233. static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
  1234. {
  1235. return queue_physical_block_size(bdev_get_queue(bdev));
  1236. }
  1237. static inline unsigned int queue_io_min(struct request_queue *q)
  1238. {
  1239. return q->limits.io_min;
  1240. }
  1241. static inline int bdev_io_min(struct block_device *bdev)
  1242. {
  1243. return queue_io_min(bdev_get_queue(bdev));
  1244. }
  1245. static inline unsigned int queue_io_opt(struct request_queue *q)
  1246. {
  1247. return q->limits.io_opt;
  1248. }
  1249. static inline int bdev_io_opt(struct block_device *bdev)
  1250. {
  1251. return queue_io_opt(bdev_get_queue(bdev));
  1252. }
  1253. static inline int queue_alignment_offset(struct request_queue *q)
  1254. {
  1255. if (q->limits.misaligned)
  1256. return -1;
  1257. return q->limits.alignment_offset;
  1258. }
  1259. static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector)
  1260. {
  1261. unsigned int granularity = max(lim->physical_block_size, lim->io_min);
  1262. unsigned int alignment = sector_div(sector, granularity >> 9) << 9;
  1263. return (granularity + lim->alignment_offset - alignment) % granularity;
  1264. }
  1265. static inline int bdev_alignment_offset(struct block_device *bdev)
  1266. {
  1267. struct request_queue *q = bdev_get_queue(bdev);
  1268. if (q->limits.misaligned)
  1269. return -1;
  1270. if (bdev != bdev->bd_contains)
  1271. return bdev->bd_part->alignment_offset;
  1272. return q->limits.alignment_offset;
  1273. }
  1274. static inline int queue_discard_alignment(struct request_queue *q)
  1275. {
  1276. if (q->limits.discard_misaligned)
  1277. return -1;
  1278. return q->limits.discard_alignment;
  1279. }
  1280. static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector)
  1281. {
  1282. unsigned int alignment, granularity, offset;
  1283. if (!lim->max_discard_sectors)
  1284. return 0;
  1285. /* Why are these in bytes, not sectors? */
  1286. alignment = lim->discard_alignment >> 9;
  1287. granularity = lim->discard_granularity >> 9;
  1288. if (!granularity)
  1289. return 0;
  1290. /* Offset of the partition start in 'granularity' sectors */
  1291. offset = sector_div(sector, granularity);
  1292. /* And why do we do this modulus *again* in blkdev_issue_discard()? */
  1293. offset = (granularity + alignment - offset) % granularity;
  1294. /* Turn it back into bytes, gaah */
  1295. return offset << 9;
  1296. }
  1297. static inline int bdev_discard_alignment(struct block_device *bdev)
  1298. {
  1299. struct request_queue *q = bdev_get_queue(bdev);
  1300. if (bdev != bdev->bd_contains)
  1301. return bdev->bd_part->discard_alignment;
  1302. return q->limits.discard_alignment;
  1303. }
  1304. static inline unsigned int bdev_write_same(struct block_device *bdev)
  1305. {
  1306. struct request_queue *q = bdev_get_queue(bdev);
  1307. if (q)
  1308. return q->limits.max_write_same_sectors;
  1309. return 0;
  1310. }
  1311. static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
  1312. {
  1313. struct request_queue *q = bdev_get_queue(bdev);
  1314. if (q)
  1315. return q->limits.max_write_zeroes_sectors;
  1316. return 0;
  1317. }
  1318. static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev)
  1319. {
  1320. struct request_queue *q = bdev_get_queue(bdev);
  1321. if (q)
  1322. return blk_queue_zoned_model(q);
  1323. return BLK_ZONED_NONE;
  1324. }
  1325. static inline bool bdev_is_zoned(struct block_device *bdev)
  1326. {
  1327. struct request_queue *q = bdev_get_queue(bdev);
  1328. if (q)
  1329. return blk_queue_is_zoned(q);
  1330. return false;
  1331. }
  1332. static inline unsigned int bdev_zone_sectors(struct block_device *bdev)
  1333. {
  1334. struct request_queue *q = bdev_get_queue(bdev);
  1335. if (q)
  1336. return blk_queue_zone_sectors(q);
  1337. return 0;
  1338. }
  1339. static inline int queue_dma_alignment(struct request_queue *q)
  1340. {
  1341. return q ? q->dma_alignment : 511;
  1342. }
  1343. static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
  1344. unsigned int len)
  1345. {
  1346. unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
  1347. return !(addr & alignment) && !(len & alignment);
  1348. }
  1349. /* assumes size > 256 */
  1350. static inline unsigned int blksize_bits(unsigned int size)
  1351. {
  1352. unsigned int bits = 8;
  1353. do {
  1354. bits++;
  1355. size >>= 1;
  1356. } while (size > 256);
  1357. return bits;
  1358. }
  1359. static inline unsigned int block_size(struct block_device *bdev)
  1360. {
  1361. return bdev->bd_block_size;
  1362. }
  1363. static inline bool queue_flush_queueable(struct request_queue *q)
  1364. {
  1365. return !test_bit(QUEUE_FLAG_FLUSH_NQ, &q->queue_flags);
  1366. }
  1367. typedef struct {struct page *v;} Sector;
  1368. unsigned char *read_dev_sector(struct block_device *, sector_t, Sector *);
  1369. static inline void put_dev_sector(Sector p)
  1370. {
  1371. put_page(p.v);
  1372. }
  1373. static inline bool __bvec_gap_to_prev(struct request_queue *q,
  1374. struct bio_vec *bprv, unsigned int offset)
  1375. {
  1376. return offset ||
  1377. ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q));
  1378. }
  1379. /*
  1380. * Check if adding a bio_vec after bprv with offset would create a gap in
  1381. * the SG list. Most drivers don't care about this, but some do.
  1382. */
  1383. static inline bool bvec_gap_to_prev(struct request_queue *q,
  1384. struct bio_vec *bprv, unsigned int offset)
  1385. {
  1386. if (!queue_virt_boundary(q))
  1387. return false;
  1388. return __bvec_gap_to_prev(q, bprv, offset);
  1389. }
  1390. /*
  1391. * Check if the two bvecs from two bios can be merged to one segment.
  1392. * If yes, no need to check gap between the two bios since the 1st bio
  1393. * and the 1st bvec in the 2nd bio can be handled in one segment.
  1394. */
  1395. static inline bool bios_segs_mergeable(struct request_queue *q,
  1396. struct bio *prev, struct bio_vec *prev_last_bv,
  1397. struct bio_vec *next_first_bv)
  1398. {
  1399. if (!BIOVEC_PHYS_MERGEABLE(prev_last_bv, next_first_bv))
  1400. return false;
  1401. if (!BIOVEC_SEG_BOUNDARY(q, prev_last_bv, next_first_bv))
  1402. return false;
  1403. if (prev->bi_seg_back_size + next_first_bv->bv_len >
  1404. queue_max_segment_size(q))
  1405. return false;
  1406. return true;
  1407. }
  1408. static inline bool bio_will_gap(struct request_queue *q,
  1409. struct request *prev_rq,
  1410. struct bio *prev,
  1411. struct bio *next)
  1412. {
  1413. if (bio_has_data(prev) && queue_virt_boundary(q)) {
  1414. struct bio_vec pb, nb;
  1415. /*
  1416. * don't merge if the 1st bio starts with non-zero
  1417. * offset, otherwise it is quite difficult to respect
  1418. * sg gap limit. We work hard to merge a huge number of small
  1419. * single bios in case of mkfs.
  1420. */
  1421. if (prev_rq)
  1422. bio_get_first_bvec(prev_rq->bio, &pb);
  1423. else
  1424. bio_get_first_bvec(prev, &pb);
  1425. if (pb.bv_offset)
  1426. return true;
  1427. /*
  1428. * We don't need to worry about the situation that the
  1429. * merged segment ends in unaligned virt boundary:
  1430. *
  1431. * - if 'pb' ends aligned, the merged segment ends aligned
  1432. * - if 'pb' ends unaligned, the next bio must include
  1433. * one single bvec of 'nb', otherwise the 'nb' can't
  1434. * merge with 'pb'
  1435. */
  1436. bio_get_last_bvec(prev, &pb);
  1437. bio_get_first_bvec(next, &nb);
  1438. if (!bios_segs_mergeable(q, prev, &pb, &nb))
  1439. return __bvec_gap_to_prev(q, &pb, nb.bv_offset);
  1440. }
  1441. return false;
  1442. }
  1443. static inline bool req_gap_back_merge(struct request *req, struct bio *bio)
  1444. {
  1445. return bio_will_gap(req->q, req, req->biotail, bio);
  1446. }
  1447. static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
  1448. {
  1449. return bio_will_gap(req->q, NULL, bio, req->bio);
  1450. }
  1451. int kblockd_schedule_work(struct work_struct *work);
  1452. int kblockd_schedule_work_on(int cpu, struct work_struct *work);
  1453. int kblockd_schedule_delayed_work(struct delayed_work *dwork, unsigned long delay);
  1454. int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
  1455. int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
  1456. #ifdef CONFIG_BLK_CGROUP
  1457. /*
  1458. * This should not be using sched_clock(). A real patch is in progress
  1459. * to fix this up, until that is in place we need to disable preemption
  1460. * around sched_clock() in this function and set_io_start_time_ns().
  1461. */
  1462. static inline void set_start_time_ns(struct request *req)
  1463. {
  1464. preempt_disable();
  1465. req->start_time_ns = sched_clock();
  1466. preempt_enable();
  1467. }
  1468. static inline void set_io_start_time_ns(struct request *req)
  1469. {
  1470. preempt_disable();
  1471. req->io_start_time_ns = sched_clock();
  1472. preempt_enable();
  1473. }
  1474. static inline uint64_t rq_start_time_ns(struct request *req)
  1475. {
  1476. return req->start_time_ns;
  1477. }
  1478. static inline uint64_t rq_io_start_time_ns(struct request *req)
  1479. {
  1480. return req->io_start_time_ns;
  1481. }
  1482. #else
  1483. static inline void set_start_time_ns(struct request *req) {}
  1484. static inline void set_io_start_time_ns(struct request *req) {}
  1485. static inline uint64_t rq_start_time_ns(struct request *req)
  1486. {
  1487. return 0;
  1488. }
  1489. static inline uint64_t rq_io_start_time_ns(struct request *req)
  1490. {
  1491. return 0;
  1492. }
  1493. #endif
  1494. #define MODULE_ALIAS_BLOCKDEV(major,minor) \
  1495. MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
  1496. #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
  1497. MODULE_ALIAS("block-major-" __stringify(major) "-*")
  1498. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  1499. enum blk_integrity_flags {
  1500. BLK_INTEGRITY_VERIFY = 1 << 0,
  1501. BLK_INTEGRITY_GENERATE = 1 << 1,
  1502. BLK_INTEGRITY_DEVICE_CAPABLE = 1 << 2,
  1503. BLK_INTEGRITY_IP_CHECKSUM = 1 << 3,
  1504. };
  1505. struct blk_integrity_iter {
  1506. void *prot_buf;
  1507. void *data_buf;
  1508. sector_t seed;
  1509. unsigned int data_size;
  1510. unsigned short interval;
  1511. const char *disk_name;
  1512. };
  1513. typedef blk_status_t (integrity_processing_fn) (struct blk_integrity_iter *);
  1514. struct blk_integrity_profile {
  1515. integrity_processing_fn *generate_fn;
  1516. integrity_processing_fn *verify_fn;
  1517. const char *name;
  1518. };
  1519. extern void blk_integrity_register(struct gendisk *, struct blk_integrity *);
  1520. extern void blk_integrity_unregister(struct gendisk *);
  1521. extern int blk_integrity_compare(struct gendisk *, struct gendisk *);
  1522. extern int blk_rq_map_integrity_sg(struct request_queue *, struct bio *,
  1523. struct scatterlist *);
  1524. extern int blk_rq_count_integrity_sg(struct request_queue *, struct bio *);
  1525. extern bool blk_integrity_merge_rq(struct request_queue *, struct request *,
  1526. struct request *);
  1527. extern bool blk_integrity_merge_bio(struct request_queue *, struct request *,
  1528. struct bio *);
  1529. static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
  1530. {
  1531. struct blk_integrity *bi = &disk->queue->integrity;
  1532. if (!bi->profile)
  1533. return NULL;
  1534. return bi;
  1535. }
  1536. static inline
  1537. struct blk_integrity *bdev_get_integrity(struct block_device *bdev)
  1538. {
  1539. return blk_get_integrity(bdev->bd_disk);
  1540. }
  1541. static inline bool blk_integrity_rq(struct request *rq)
  1542. {
  1543. return rq->cmd_flags & REQ_INTEGRITY;
  1544. }
  1545. static inline void blk_queue_max_integrity_segments(struct request_queue *q,
  1546. unsigned int segs)
  1547. {
  1548. q->limits.max_integrity_segments = segs;
  1549. }
  1550. static inline unsigned short
  1551. queue_max_integrity_segments(struct request_queue *q)
  1552. {
  1553. return q->limits.max_integrity_segments;
  1554. }
  1555. static inline bool integrity_req_gap_back_merge(struct request *req,
  1556. struct bio *next)
  1557. {
  1558. struct bio_integrity_payload *bip = bio_integrity(req->bio);
  1559. struct bio_integrity_payload *bip_next = bio_integrity(next);
  1560. return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
  1561. bip_next->bip_vec[0].bv_offset);
  1562. }
  1563. static inline bool integrity_req_gap_front_merge(struct request *req,
  1564. struct bio *bio)
  1565. {
  1566. struct bio_integrity_payload *bip = bio_integrity(bio);
  1567. struct bio_integrity_payload *bip_next = bio_integrity(req->bio);
  1568. return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
  1569. bip_next->bip_vec[0].bv_offset);
  1570. }
  1571. #else /* CONFIG_BLK_DEV_INTEGRITY */
  1572. struct bio;
  1573. struct block_device;
  1574. struct gendisk;
  1575. struct blk_integrity;
  1576. static inline int blk_integrity_rq(struct request *rq)
  1577. {
  1578. return 0;
  1579. }
  1580. static inline int blk_rq_count_integrity_sg(struct request_queue *q,
  1581. struct bio *b)
  1582. {
  1583. return 0;
  1584. }
  1585. static inline int blk_rq_map_integrity_sg(struct request_queue *q,
  1586. struct bio *b,
  1587. struct scatterlist *s)
  1588. {
  1589. return 0;
  1590. }
  1591. static inline struct blk_integrity *bdev_get_integrity(struct block_device *b)
  1592. {
  1593. return NULL;
  1594. }
  1595. static inline struct blk_integrity *blk_get_integrity(struct gendisk *disk)
  1596. {
  1597. return NULL;
  1598. }
  1599. static inline int blk_integrity_compare(struct gendisk *a, struct gendisk *b)
  1600. {
  1601. return 0;
  1602. }
  1603. static inline void blk_integrity_register(struct gendisk *d,
  1604. struct blk_integrity *b)
  1605. {
  1606. }
  1607. static inline void blk_integrity_unregister(struct gendisk *d)
  1608. {
  1609. }
  1610. static inline void blk_queue_max_integrity_segments(struct request_queue *q,
  1611. unsigned int segs)
  1612. {
  1613. }
  1614. static inline unsigned short queue_max_integrity_segments(struct request_queue *q)
  1615. {
  1616. return 0;
  1617. }
  1618. static inline bool blk_integrity_merge_rq(struct request_queue *rq,
  1619. struct request *r1,
  1620. struct request *r2)
  1621. {
  1622. return true;
  1623. }
  1624. static inline bool blk_integrity_merge_bio(struct request_queue *rq,
  1625. struct request *r,
  1626. struct bio *b)
  1627. {
  1628. return true;
  1629. }
  1630. static inline bool integrity_req_gap_back_merge(struct request *req,
  1631. struct bio *next)
  1632. {
  1633. return false;
  1634. }
  1635. static inline bool integrity_req_gap_front_merge(struct request *req,
  1636. struct bio *bio)
  1637. {
  1638. return false;
  1639. }
  1640. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  1641. struct block_device_operations {
  1642. int (*open) (struct block_device *, fmode_t);
  1643. void (*release) (struct gendisk *, fmode_t);
  1644. int (*rw_page)(struct block_device *, sector_t, struct page *, bool);
  1645. int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
  1646. int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long);
  1647. unsigned int (*check_events) (struct gendisk *disk,
  1648. unsigned int clearing);
  1649. /* ->media_changed() is DEPRECATED, use ->check_events() instead */
  1650. int (*media_changed) (struct gendisk *);
  1651. void (*unlock_native_capacity) (struct gendisk *);
  1652. int (*revalidate_disk) (struct gendisk *);
  1653. int (*getgeo)(struct block_device *, struct hd_geometry *);
  1654. /* this callback is with swap_lock and sometimes page table lock held */
  1655. void (*swap_slot_free_notify) (struct block_device *, unsigned long);
  1656. struct module *owner;
  1657. const struct pr_ops *pr_ops;
  1658. };
  1659. extern int __blkdev_driver_ioctl(struct block_device *, fmode_t, unsigned int,
  1660. unsigned long);
  1661. extern int bdev_read_page(struct block_device *, sector_t, struct page *);
  1662. extern int bdev_write_page(struct block_device *, sector_t, struct page *,
  1663. struct writeback_control *);
  1664. #else /* CONFIG_BLOCK */
  1665. struct block_device;
  1666. /*
  1667. * stubs for when the block layer is configured out
  1668. */
  1669. #define buffer_heads_over_limit 0
  1670. static inline long nr_blockdev_pages(void)
  1671. {
  1672. return 0;
  1673. }
  1674. struct blk_plug {
  1675. };
  1676. static inline void blk_start_plug(struct blk_plug *plug)
  1677. {
  1678. }
  1679. static inline void blk_finish_plug(struct blk_plug *plug)
  1680. {
  1681. }
  1682. static inline void blk_flush_plug(struct task_struct *task)
  1683. {
  1684. }
  1685. static inline void blk_schedule_flush_plug(struct task_struct *task)
  1686. {
  1687. }
  1688. static inline bool blk_needs_flush_plug(struct task_struct *tsk)
  1689. {
  1690. return false;
  1691. }
  1692. static inline int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
  1693. sector_t *error_sector)
  1694. {
  1695. return 0;
  1696. }
  1697. #endif /* CONFIG_BLOCK */
  1698. #endif