blkdev.h 48 KB

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