blkdev.h 46 KB

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