blkdev.h 50 KB

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