blkdev.h 58 KB

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