blkdev.h 58 KB

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