blkdev.h 48 KB

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