blkdev.h 58 KB

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