dm.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895
  1. /*
  2. * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include "dm-uevent.h"
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/blkpg.h>
  14. #include <linux/bio.h>
  15. #include <linux/mempool.h>
  16. #include <linux/slab.h>
  17. #include <linux/idr.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/delay.h>
  20. #include <trace/events/block.h>
  21. #define DM_MSG_PREFIX "core"
  22. #ifdef CONFIG_PRINTK
  23. /*
  24. * ratelimit state to be used in DMXXX_LIMIT().
  25. */
  26. DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
  27. DEFAULT_RATELIMIT_INTERVAL,
  28. DEFAULT_RATELIMIT_BURST);
  29. EXPORT_SYMBOL(dm_ratelimit_state);
  30. #endif
  31. /*
  32. * Cookies are numeric values sent with CHANGE and REMOVE
  33. * uevents while resuming, removing or renaming the device.
  34. */
  35. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  36. #define DM_COOKIE_LENGTH 24
  37. static const char *_name = DM_NAME;
  38. static unsigned int major = 0;
  39. static unsigned int _major = 0;
  40. static DEFINE_IDR(_minor_idr);
  41. static DEFINE_SPINLOCK(_minor_lock);
  42. static void do_deferred_remove(struct work_struct *w);
  43. static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
  44. /*
  45. * For bio-based dm.
  46. * One of these is allocated per bio.
  47. */
  48. struct dm_io {
  49. struct mapped_device *md;
  50. int error;
  51. atomic_t io_count;
  52. struct bio *bio;
  53. unsigned long start_time;
  54. spinlock_t endio_lock;
  55. struct dm_stats_aux stats_aux;
  56. };
  57. /*
  58. * For request-based dm.
  59. * One of these is allocated per request.
  60. */
  61. struct dm_rq_target_io {
  62. struct mapped_device *md;
  63. struct dm_target *ti;
  64. struct request *orig, clone;
  65. int error;
  66. union map_info info;
  67. };
  68. /*
  69. * For request-based dm - the bio clones we allocate are embedded in these
  70. * structs.
  71. *
  72. * We allocate these with bio_alloc_bioset, using the front_pad parameter when
  73. * the bioset is created - this means the bio has to come at the end of the
  74. * struct.
  75. */
  76. struct dm_rq_clone_bio_info {
  77. struct bio *orig;
  78. struct dm_rq_target_io *tio;
  79. struct bio clone;
  80. };
  81. union map_info *dm_get_mapinfo(struct bio *bio)
  82. {
  83. if (bio && bio->bi_private)
  84. return &((struct dm_target_io *)bio->bi_private)->info;
  85. return NULL;
  86. }
  87. union map_info *dm_get_rq_mapinfo(struct request *rq)
  88. {
  89. if (rq && rq->end_io_data)
  90. return &((struct dm_rq_target_io *)rq->end_io_data)->info;
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
  94. #define MINOR_ALLOCED ((void *)-1)
  95. /*
  96. * Bits for the md->flags field.
  97. */
  98. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  99. #define DMF_SUSPENDED 1
  100. #define DMF_FROZEN 2
  101. #define DMF_FREEING 3
  102. #define DMF_DELETING 4
  103. #define DMF_NOFLUSH_SUSPENDING 5
  104. #define DMF_MERGE_IS_OPTIONAL 6
  105. #define DMF_DEFERRED_REMOVE 7
  106. /*
  107. * A dummy definition to make RCU happy.
  108. * struct dm_table should never be dereferenced in this file.
  109. */
  110. struct dm_table {
  111. int undefined__;
  112. };
  113. /*
  114. * Work processed by per-device workqueue.
  115. */
  116. struct mapped_device {
  117. struct srcu_struct io_barrier;
  118. struct mutex suspend_lock;
  119. atomic_t holders;
  120. atomic_t open_count;
  121. /*
  122. * The current mapping.
  123. * Use dm_get_live_table{_fast} or take suspend_lock for
  124. * dereference.
  125. */
  126. struct dm_table *map;
  127. unsigned long flags;
  128. struct request_queue *queue;
  129. unsigned type;
  130. /* Protect queue and type against concurrent access. */
  131. struct mutex type_lock;
  132. struct target_type *immutable_target_type;
  133. struct gendisk *disk;
  134. char name[16];
  135. void *interface_ptr;
  136. /*
  137. * A list of ios that arrived while we were suspended.
  138. */
  139. atomic_t pending[2];
  140. wait_queue_head_t wait;
  141. struct work_struct work;
  142. struct bio_list deferred;
  143. spinlock_t deferred_lock;
  144. /*
  145. * Processing queue (flush)
  146. */
  147. struct workqueue_struct *wq;
  148. /*
  149. * io objects are allocated from here.
  150. */
  151. mempool_t *io_pool;
  152. struct bio_set *bs;
  153. /*
  154. * Event handling.
  155. */
  156. atomic_t event_nr;
  157. wait_queue_head_t eventq;
  158. atomic_t uevent_seq;
  159. struct list_head uevent_list;
  160. spinlock_t uevent_lock; /* Protect access to uevent_list */
  161. /*
  162. * freeze/thaw support require holding onto a super block
  163. */
  164. struct super_block *frozen_sb;
  165. struct block_device *bdev;
  166. /* forced geometry settings */
  167. struct hd_geometry geometry;
  168. /* kobject and completion */
  169. struct dm_kobject_holder kobj_holder;
  170. /* zero-length flush that will be cloned and submitted to targets */
  171. struct bio flush_bio;
  172. struct dm_stats stats;
  173. };
  174. /*
  175. * For mempools pre-allocation at the table loading time.
  176. */
  177. struct dm_md_mempools {
  178. mempool_t *io_pool;
  179. struct bio_set *bs;
  180. };
  181. #define RESERVED_BIO_BASED_IOS 16
  182. #define RESERVED_REQUEST_BASED_IOS 256
  183. #define RESERVED_MAX_IOS 1024
  184. static struct kmem_cache *_io_cache;
  185. static struct kmem_cache *_rq_tio_cache;
  186. /*
  187. * Bio-based DM's mempools' reserved IOs set by the user.
  188. */
  189. static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
  190. /*
  191. * Request-based DM's mempools' reserved IOs set by the user.
  192. */
  193. static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
  194. static unsigned __dm_get_reserved_ios(unsigned *reserved_ios,
  195. unsigned def, unsigned max)
  196. {
  197. unsigned ios = ACCESS_ONCE(*reserved_ios);
  198. unsigned modified_ios = 0;
  199. if (!ios)
  200. modified_ios = def;
  201. else if (ios > max)
  202. modified_ios = max;
  203. if (modified_ios) {
  204. (void)cmpxchg(reserved_ios, ios, modified_ios);
  205. ios = modified_ios;
  206. }
  207. return ios;
  208. }
  209. unsigned dm_get_reserved_bio_based_ios(void)
  210. {
  211. return __dm_get_reserved_ios(&reserved_bio_based_ios,
  212. RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
  213. }
  214. EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
  215. unsigned dm_get_reserved_rq_based_ios(void)
  216. {
  217. return __dm_get_reserved_ios(&reserved_rq_based_ios,
  218. RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
  219. }
  220. EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
  221. static int __init local_init(void)
  222. {
  223. int r = -ENOMEM;
  224. /* allocate a slab for the dm_ios */
  225. _io_cache = KMEM_CACHE(dm_io, 0);
  226. if (!_io_cache)
  227. return r;
  228. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  229. if (!_rq_tio_cache)
  230. goto out_free_io_cache;
  231. r = dm_uevent_init();
  232. if (r)
  233. goto out_free_rq_tio_cache;
  234. _major = major;
  235. r = register_blkdev(_major, _name);
  236. if (r < 0)
  237. goto out_uevent_exit;
  238. if (!_major)
  239. _major = r;
  240. return 0;
  241. out_uevent_exit:
  242. dm_uevent_exit();
  243. out_free_rq_tio_cache:
  244. kmem_cache_destroy(_rq_tio_cache);
  245. out_free_io_cache:
  246. kmem_cache_destroy(_io_cache);
  247. return r;
  248. }
  249. static void local_exit(void)
  250. {
  251. flush_scheduled_work();
  252. kmem_cache_destroy(_rq_tio_cache);
  253. kmem_cache_destroy(_io_cache);
  254. unregister_blkdev(_major, _name);
  255. dm_uevent_exit();
  256. _major = 0;
  257. DMINFO("cleaned up");
  258. }
  259. static int (*_inits[])(void) __initdata = {
  260. local_init,
  261. dm_target_init,
  262. dm_linear_init,
  263. dm_stripe_init,
  264. dm_io_init,
  265. dm_kcopyd_init,
  266. dm_interface_init,
  267. dm_statistics_init,
  268. };
  269. static void (*_exits[])(void) = {
  270. local_exit,
  271. dm_target_exit,
  272. dm_linear_exit,
  273. dm_stripe_exit,
  274. dm_io_exit,
  275. dm_kcopyd_exit,
  276. dm_interface_exit,
  277. dm_statistics_exit,
  278. };
  279. static int __init dm_init(void)
  280. {
  281. const int count = ARRAY_SIZE(_inits);
  282. int r, i;
  283. for (i = 0; i < count; i++) {
  284. r = _inits[i]();
  285. if (r)
  286. goto bad;
  287. }
  288. return 0;
  289. bad:
  290. while (i--)
  291. _exits[i]();
  292. return r;
  293. }
  294. static void __exit dm_exit(void)
  295. {
  296. int i = ARRAY_SIZE(_exits);
  297. while (i--)
  298. _exits[i]();
  299. /*
  300. * Should be empty by this point.
  301. */
  302. idr_destroy(&_minor_idr);
  303. }
  304. /*
  305. * Block device functions
  306. */
  307. int dm_deleting_md(struct mapped_device *md)
  308. {
  309. return test_bit(DMF_DELETING, &md->flags);
  310. }
  311. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  312. {
  313. struct mapped_device *md;
  314. spin_lock(&_minor_lock);
  315. md = bdev->bd_disk->private_data;
  316. if (!md)
  317. goto out;
  318. if (test_bit(DMF_FREEING, &md->flags) ||
  319. dm_deleting_md(md)) {
  320. md = NULL;
  321. goto out;
  322. }
  323. dm_get(md);
  324. atomic_inc(&md->open_count);
  325. out:
  326. spin_unlock(&_minor_lock);
  327. return md ? 0 : -ENXIO;
  328. }
  329. static void dm_blk_close(struct gendisk *disk, fmode_t mode)
  330. {
  331. struct mapped_device *md = disk->private_data;
  332. spin_lock(&_minor_lock);
  333. if (atomic_dec_and_test(&md->open_count) &&
  334. (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
  335. schedule_work(&deferred_remove_work);
  336. dm_put(md);
  337. spin_unlock(&_minor_lock);
  338. }
  339. int dm_open_count(struct mapped_device *md)
  340. {
  341. return atomic_read(&md->open_count);
  342. }
  343. /*
  344. * Guarantees nothing is using the device before it's deleted.
  345. */
  346. int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
  347. {
  348. int r = 0;
  349. spin_lock(&_minor_lock);
  350. if (dm_open_count(md)) {
  351. r = -EBUSY;
  352. if (mark_deferred)
  353. set_bit(DMF_DEFERRED_REMOVE, &md->flags);
  354. } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
  355. r = -EEXIST;
  356. else
  357. set_bit(DMF_DELETING, &md->flags);
  358. spin_unlock(&_minor_lock);
  359. return r;
  360. }
  361. int dm_cancel_deferred_remove(struct mapped_device *md)
  362. {
  363. int r = 0;
  364. spin_lock(&_minor_lock);
  365. if (test_bit(DMF_DELETING, &md->flags))
  366. r = -EBUSY;
  367. else
  368. clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
  369. spin_unlock(&_minor_lock);
  370. return r;
  371. }
  372. static void do_deferred_remove(struct work_struct *w)
  373. {
  374. dm_deferred_remove();
  375. }
  376. sector_t dm_get_size(struct mapped_device *md)
  377. {
  378. return get_capacity(md->disk);
  379. }
  380. struct dm_stats *dm_get_stats(struct mapped_device *md)
  381. {
  382. return &md->stats;
  383. }
  384. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  385. {
  386. struct mapped_device *md = bdev->bd_disk->private_data;
  387. return dm_get_geometry(md, geo);
  388. }
  389. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  390. unsigned int cmd, unsigned long arg)
  391. {
  392. struct mapped_device *md = bdev->bd_disk->private_data;
  393. int srcu_idx;
  394. struct dm_table *map;
  395. struct dm_target *tgt;
  396. int r = -ENOTTY;
  397. retry:
  398. map = dm_get_live_table(md, &srcu_idx);
  399. if (!map || !dm_table_get_size(map))
  400. goto out;
  401. /* We only support devices that have a single target */
  402. if (dm_table_get_num_targets(map) != 1)
  403. goto out;
  404. tgt = dm_table_get_target(map, 0);
  405. if (dm_suspended_md(md)) {
  406. r = -EAGAIN;
  407. goto out;
  408. }
  409. if (tgt->type->ioctl)
  410. r = tgt->type->ioctl(tgt, cmd, arg);
  411. out:
  412. dm_put_live_table(md, srcu_idx);
  413. if (r == -ENOTCONN) {
  414. msleep(10);
  415. goto retry;
  416. }
  417. return r;
  418. }
  419. static struct dm_io *alloc_io(struct mapped_device *md)
  420. {
  421. return mempool_alloc(md->io_pool, GFP_NOIO);
  422. }
  423. static void free_io(struct mapped_device *md, struct dm_io *io)
  424. {
  425. mempool_free(io, md->io_pool);
  426. }
  427. static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
  428. {
  429. bio_put(&tio->clone);
  430. }
  431. static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
  432. gfp_t gfp_mask)
  433. {
  434. return mempool_alloc(md->io_pool, gfp_mask);
  435. }
  436. static void free_rq_tio(struct dm_rq_target_io *tio)
  437. {
  438. mempool_free(tio, tio->md->io_pool);
  439. }
  440. static int md_in_flight(struct mapped_device *md)
  441. {
  442. return atomic_read(&md->pending[READ]) +
  443. atomic_read(&md->pending[WRITE]);
  444. }
  445. static void start_io_acct(struct dm_io *io)
  446. {
  447. struct mapped_device *md = io->md;
  448. struct bio *bio = io->bio;
  449. int cpu;
  450. int rw = bio_data_dir(bio);
  451. io->start_time = jiffies;
  452. cpu = part_stat_lock();
  453. part_round_stats(cpu, &dm_disk(md)->part0);
  454. part_stat_unlock();
  455. atomic_set(&dm_disk(md)->part0.in_flight[rw],
  456. atomic_inc_return(&md->pending[rw]));
  457. if (unlikely(dm_stats_used(&md->stats)))
  458. dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
  459. bio_sectors(bio), false, 0, &io->stats_aux);
  460. }
  461. static void end_io_acct(struct dm_io *io)
  462. {
  463. struct mapped_device *md = io->md;
  464. struct bio *bio = io->bio;
  465. unsigned long duration = jiffies - io->start_time;
  466. int pending, cpu;
  467. int rw = bio_data_dir(bio);
  468. cpu = part_stat_lock();
  469. part_round_stats(cpu, &dm_disk(md)->part0);
  470. part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
  471. part_stat_unlock();
  472. if (unlikely(dm_stats_used(&md->stats)))
  473. dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
  474. bio_sectors(bio), true, duration, &io->stats_aux);
  475. /*
  476. * After this is decremented the bio must not be touched if it is
  477. * a flush.
  478. */
  479. pending = atomic_dec_return(&md->pending[rw]);
  480. atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
  481. pending += atomic_read(&md->pending[rw^0x1]);
  482. /* nudge anyone waiting on suspend queue */
  483. if (!pending)
  484. wake_up(&md->wait);
  485. }
  486. /*
  487. * Add the bio to the list of deferred io.
  488. */
  489. static void queue_io(struct mapped_device *md, struct bio *bio)
  490. {
  491. unsigned long flags;
  492. spin_lock_irqsave(&md->deferred_lock, flags);
  493. bio_list_add(&md->deferred, bio);
  494. spin_unlock_irqrestore(&md->deferred_lock, flags);
  495. queue_work(md->wq, &md->work);
  496. }
  497. /*
  498. * Everyone (including functions in this file), should use this
  499. * function to access the md->map field, and make sure they call
  500. * dm_put_live_table() when finished.
  501. */
  502. struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
  503. {
  504. *srcu_idx = srcu_read_lock(&md->io_barrier);
  505. return srcu_dereference(md->map, &md->io_barrier);
  506. }
  507. void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
  508. {
  509. srcu_read_unlock(&md->io_barrier, srcu_idx);
  510. }
  511. void dm_sync_table(struct mapped_device *md)
  512. {
  513. synchronize_srcu(&md->io_barrier);
  514. synchronize_rcu_expedited();
  515. }
  516. /*
  517. * A fast alternative to dm_get_live_table/dm_put_live_table.
  518. * The caller must not block between these two functions.
  519. */
  520. static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
  521. {
  522. rcu_read_lock();
  523. return rcu_dereference(md->map);
  524. }
  525. static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
  526. {
  527. rcu_read_unlock();
  528. }
  529. /*
  530. * Get the geometry associated with a dm device
  531. */
  532. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  533. {
  534. *geo = md->geometry;
  535. return 0;
  536. }
  537. /*
  538. * Set the geometry of a device.
  539. */
  540. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  541. {
  542. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  543. if (geo->start > sz) {
  544. DMWARN("Start sector is beyond the geometry limits.");
  545. return -EINVAL;
  546. }
  547. md->geometry = *geo;
  548. return 0;
  549. }
  550. /*-----------------------------------------------------------------
  551. * CRUD START:
  552. * A more elegant soln is in the works that uses the queue
  553. * merge fn, unfortunately there are a couple of changes to
  554. * the block layer that I want to make for this. So in the
  555. * interests of getting something for people to use I give
  556. * you this clearly demarcated crap.
  557. *---------------------------------------------------------------*/
  558. static int __noflush_suspending(struct mapped_device *md)
  559. {
  560. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  561. }
  562. /*
  563. * Decrements the number of outstanding ios that a bio has been
  564. * cloned into, completing the original io if necc.
  565. */
  566. static void dec_pending(struct dm_io *io, int error)
  567. {
  568. unsigned long flags;
  569. int io_error;
  570. struct bio *bio;
  571. struct mapped_device *md = io->md;
  572. /* Push-back supersedes any I/O errors */
  573. if (unlikely(error)) {
  574. spin_lock_irqsave(&io->endio_lock, flags);
  575. if (!(io->error > 0 && __noflush_suspending(md)))
  576. io->error = error;
  577. spin_unlock_irqrestore(&io->endio_lock, flags);
  578. }
  579. if (atomic_dec_and_test(&io->io_count)) {
  580. if (io->error == DM_ENDIO_REQUEUE) {
  581. /*
  582. * Target requested pushing back the I/O.
  583. */
  584. spin_lock_irqsave(&md->deferred_lock, flags);
  585. if (__noflush_suspending(md))
  586. bio_list_add_head(&md->deferred, io->bio);
  587. else
  588. /* noflush suspend was interrupted. */
  589. io->error = -EIO;
  590. spin_unlock_irqrestore(&md->deferred_lock, flags);
  591. }
  592. io_error = io->error;
  593. bio = io->bio;
  594. end_io_acct(io);
  595. free_io(md, io);
  596. if (io_error == DM_ENDIO_REQUEUE)
  597. return;
  598. if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
  599. /*
  600. * Preflush done for flush with data, reissue
  601. * without REQ_FLUSH.
  602. */
  603. bio->bi_rw &= ~REQ_FLUSH;
  604. queue_io(md, bio);
  605. } else {
  606. /* done with normal IO or empty flush */
  607. trace_block_bio_complete(md->queue, bio, io_error);
  608. bio_endio(bio, io_error);
  609. }
  610. }
  611. }
  612. static void clone_endio(struct bio *bio, int error)
  613. {
  614. int r = 0;
  615. struct dm_target_io *tio = bio->bi_private;
  616. struct dm_io *io = tio->io;
  617. struct mapped_device *md = tio->io->md;
  618. dm_endio_fn endio = tio->ti->type->end_io;
  619. if (!bio_flagged(bio, BIO_UPTODATE) && !error)
  620. error = -EIO;
  621. if (endio) {
  622. r = endio(tio->ti, bio, error);
  623. if (r < 0 || r == DM_ENDIO_REQUEUE)
  624. /*
  625. * error and requeue request are handled
  626. * in dec_pending().
  627. */
  628. error = r;
  629. else if (r == DM_ENDIO_INCOMPLETE)
  630. /* The target will handle the io */
  631. return;
  632. else if (r) {
  633. DMWARN("unimplemented target endio return value: %d", r);
  634. BUG();
  635. }
  636. }
  637. free_tio(md, tio);
  638. dec_pending(io, error);
  639. }
  640. /*
  641. * Partial completion handling for request-based dm
  642. */
  643. static void end_clone_bio(struct bio *clone, int error)
  644. {
  645. struct dm_rq_clone_bio_info *info = clone->bi_private;
  646. struct dm_rq_target_io *tio = info->tio;
  647. struct bio *bio = info->orig;
  648. unsigned int nr_bytes = info->orig->bi_iter.bi_size;
  649. bio_put(clone);
  650. if (tio->error)
  651. /*
  652. * An error has already been detected on the request.
  653. * Once error occurred, just let clone->end_io() handle
  654. * the remainder.
  655. */
  656. return;
  657. else if (error) {
  658. /*
  659. * Don't notice the error to the upper layer yet.
  660. * The error handling decision is made by the target driver,
  661. * when the request is completed.
  662. */
  663. tio->error = error;
  664. return;
  665. }
  666. /*
  667. * I/O for the bio successfully completed.
  668. * Notice the data completion to the upper layer.
  669. */
  670. /*
  671. * bios are processed from the head of the list.
  672. * So the completing bio should always be rq->bio.
  673. * If it's not, something wrong is happening.
  674. */
  675. if (tio->orig->bio != bio)
  676. DMERR("bio completion is going in the middle of the request");
  677. /*
  678. * Update the original request.
  679. * Do not use blk_end_request() here, because it may complete
  680. * the original request before the clone, and break the ordering.
  681. */
  682. blk_update_request(tio->orig, 0, nr_bytes);
  683. }
  684. /*
  685. * Don't touch any member of the md after calling this function because
  686. * the md may be freed in dm_put() at the end of this function.
  687. * Or do dm_get() before calling this function and dm_put() later.
  688. */
  689. static void rq_completed(struct mapped_device *md, int rw, int run_queue)
  690. {
  691. atomic_dec(&md->pending[rw]);
  692. /* nudge anyone waiting on suspend queue */
  693. if (!md_in_flight(md))
  694. wake_up(&md->wait);
  695. /*
  696. * Run this off this callpath, as drivers could invoke end_io while
  697. * inside their request_fn (and holding the queue lock). Calling
  698. * back into ->request_fn() could deadlock attempting to grab the
  699. * queue lock again.
  700. */
  701. if (run_queue)
  702. blk_run_queue_async(md->queue);
  703. /*
  704. * dm_put() must be at the end of this function. See the comment above
  705. */
  706. dm_put(md);
  707. }
  708. static void free_rq_clone(struct request *clone)
  709. {
  710. struct dm_rq_target_io *tio = clone->end_io_data;
  711. blk_rq_unprep_clone(clone);
  712. free_rq_tio(tio);
  713. }
  714. /*
  715. * Complete the clone and the original request.
  716. * Must be called without queue lock.
  717. */
  718. static void dm_end_request(struct request *clone, int error)
  719. {
  720. int rw = rq_data_dir(clone);
  721. struct dm_rq_target_io *tio = clone->end_io_data;
  722. struct mapped_device *md = tio->md;
  723. struct request *rq = tio->orig;
  724. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  725. rq->errors = clone->errors;
  726. rq->resid_len = clone->resid_len;
  727. if (rq->sense)
  728. /*
  729. * We are using the sense buffer of the original
  730. * request.
  731. * So setting the length of the sense data is enough.
  732. */
  733. rq->sense_len = clone->sense_len;
  734. }
  735. free_rq_clone(clone);
  736. blk_end_request_all(rq, error);
  737. rq_completed(md, rw, true);
  738. }
  739. static void dm_unprep_request(struct request *rq)
  740. {
  741. struct request *clone = rq->special;
  742. rq->special = NULL;
  743. rq->cmd_flags &= ~REQ_DONTPREP;
  744. free_rq_clone(clone);
  745. }
  746. /*
  747. * Requeue the original request of a clone.
  748. */
  749. void dm_requeue_unmapped_request(struct request *clone)
  750. {
  751. int rw = rq_data_dir(clone);
  752. struct dm_rq_target_io *tio = clone->end_io_data;
  753. struct mapped_device *md = tio->md;
  754. struct request *rq = tio->orig;
  755. struct request_queue *q = rq->q;
  756. unsigned long flags;
  757. dm_unprep_request(rq);
  758. spin_lock_irqsave(q->queue_lock, flags);
  759. blk_requeue_request(q, rq);
  760. spin_unlock_irqrestore(q->queue_lock, flags);
  761. rq_completed(md, rw, 0);
  762. }
  763. EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
  764. static void __stop_queue(struct request_queue *q)
  765. {
  766. blk_stop_queue(q);
  767. }
  768. static void stop_queue(struct request_queue *q)
  769. {
  770. unsigned long flags;
  771. spin_lock_irqsave(q->queue_lock, flags);
  772. __stop_queue(q);
  773. spin_unlock_irqrestore(q->queue_lock, flags);
  774. }
  775. static void __start_queue(struct request_queue *q)
  776. {
  777. if (blk_queue_stopped(q))
  778. blk_start_queue(q);
  779. }
  780. static void start_queue(struct request_queue *q)
  781. {
  782. unsigned long flags;
  783. spin_lock_irqsave(q->queue_lock, flags);
  784. __start_queue(q);
  785. spin_unlock_irqrestore(q->queue_lock, flags);
  786. }
  787. static void dm_done(struct request *clone, int error, bool mapped)
  788. {
  789. int r = error;
  790. struct dm_rq_target_io *tio = clone->end_io_data;
  791. dm_request_endio_fn rq_end_io = NULL;
  792. if (tio->ti) {
  793. rq_end_io = tio->ti->type->rq_end_io;
  794. if (mapped && rq_end_io)
  795. r = rq_end_io(tio->ti, clone, error, &tio->info);
  796. }
  797. if (r <= 0)
  798. /* The target wants to complete the I/O */
  799. dm_end_request(clone, r);
  800. else if (r == DM_ENDIO_INCOMPLETE)
  801. /* The target will handle the I/O */
  802. return;
  803. else if (r == DM_ENDIO_REQUEUE)
  804. /* The target wants to requeue the I/O */
  805. dm_requeue_unmapped_request(clone);
  806. else {
  807. DMWARN("unimplemented target endio return value: %d", r);
  808. BUG();
  809. }
  810. }
  811. /*
  812. * Request completion handler for request-based dm
  813. */
  814. static void dm_softirq_done(struct request *rq)
  815. {
  816. bool mapped = true;
  817. struct request *clone = rq->completion_data;
  818. struct dm_rq_target_io *tio = clone->end_io_data;
  819. if (rq->cmd_flags & REQ_FAILED)
  820. mapped = false;
  821. dm_done(clone, tio->error, mapped);
  822. }
  823. /*
  824. * Complete the clone and the original request with the error status
  825. * through softirq context.
  826. */
  827. static void dm_complete_request(struct request *clone, int error)
  828. {
  829. struct dm_rq_target_io *tio = clone->end_io_data;
  830. struct request *rq = tio->orig;
  831. tio->error = error;
  832. rq->completion_data = clone;
  833. blk_complete_request(rq);
  834. }
  835. /*
  836. * Complete the not-mapped clone and the original request with the error status
  837. * through softirq context.
  838. * Target's rq_end_io() function isn't called.
  839. * This may be used when the target's map_rq() function fails.
  840. */
  841. void dm_kill_unmapped_request(struct request *clone, int error)
  842. {
  843. struct dm_rq_target_io *tio = clone->end_io_data;
  844. struct request *rq = tio->orig;
  845. rq->cmd_flags |= REQ_FAILED;
  846. dm_complete_request(clone, error);
  847. }
  848. EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
  849. /*
  850. * Called with the queue lock held
  851. */
  852. static void end_clone_request(struct request *clone, int error)
  853. {
  854. /*
  855. * For just cleaning up the information of the queue in which
  856. * the clone was dispatched.
  857. * The clone is *NOT* freed actually here because it is alloced from
  858. * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
  859. */
  860. __blk_put_request(clone->q, clone);
  861. /*
  862. * Actual request completion is done in a softirq context which doesn't
  863. * hold the queue lock. Otherwise, deadlock could occur because:
  864. * - another request may be submitted by the upper level driver
  865. * of the stacking during the completion
  866. * - the submission which requires queue lock may be done
  867. * against this queue
  868. */
  869. dm_complete_request(clone, error);
  870. }
  871. /*
  872. * Return maximum size of I/O possible at the supplied sector up to the current
  873. * target boundary.
  874. */
  875. static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
  876. {
  877. sector_t target_offset = dm_target_offset(ti, sector);
  878. return ti->len - target_offset;
  879. }
  880. static sector_t max_io_len(sector_t sector, struct dm_target *ti)
  881. {
  882. sector_t len = max_io_len_target_boundary(sector, ti);
  883. sector_t offset, max_len;
  884. /*
  885. * Does the target need to split even further?
  886. */
  887. if (ti->max_io_len) {
  888. offset = dm_target_offset(ti, sector);
  889. if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
  890. max_len = sector_div(offset, ti->max_io_len);
  891. else
  892. max_len = offset & (ti->max_io_len - 1);
  893. max_len = ti->max_io_len - max_len;
  894. if (len > max_len)
  895. len = max_len;
  896. }
  897. return len;
  898. }
  899. int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
  900. {
  901. if (len > UINT_MAX) {
  902. DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
  903. (unsigned long long)len, UINT_MAX);
  904. ti->error = "Maximum size of target IO is too large";
  905. return -EINVAL;
  906. }
  907. ti->max_io_len = (uint32_t) len;
  908. return 0;
  909. }
  910. EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
  911. static void __map_bio(struct dm_target_io *tio)
  912. {
  913. int r;
  914. sector_t sector;
  915. struct mapped_device *md;
  916. struct bio *clone = &tio->clone;
  917. struct dm_target *ti = tio->ti;
  918. clone->bi_end_io = clone_endio;
  919. clone->bi_private = tio;
  920. /*
  921. * Map the clone. If r == 0 we don't need to do
  922. * anything, the target has assumed ownership of
  923. * this io.
  924. */
  925. atomic_inc(&tio->io->io_count);
  926. sector = clone->bi_iter.bi_sector;
  927. r = ti->type->map(ti, clone);
  928. if (r == DM_MAPIO_REMAPPED) {
  929. /* the bio has been remapped so dispatch it */
  930. trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
  931. tio->io->bio->bi_bdev->bd_dev, sector);
  932. generic_make_request(clone);
  933. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  934. /* error the io and bail out, or requeue it if needed */
  935. md = tio->io->md;
  936. dec_pending(tio->io, r);
  937. free_tio(md, tio);
  938. } else if (r) {
  939. DMWARN("unimplemented target map return value: %d", r);
  940. BUG();
  941. }
  942. }
  943. struct clone_info {
  944. struct mapped_device *md;
  945. struct dm_table *map;
  946. struct bio *bio;
  947. struct dm_io *io;
  948. sector_t sector;
  949. sector_t sector_count;
  950. };
  951. static void bio_setup_sector(struct bio *bio, sector_t sector, sector_t len)
  952. {
  953. bio->bi_iter.bi_sector = sector;
  954. bio->bi_iter.bi_size = to_bytes(len);
  955. }
  956. /*
  957. * Creates a bio that consists of range of complete bvecs.
  958. */
  959. static void clone_bio(struct dm_target_io *tio, struct bio *bio,
  960. sector_t sector, unsigned len)
  961. {
  962. struct bio *clone = &tio->clone;
  963. __bio_clone_fast(clone, bio);
  964. if (bio_integrity(bio))
  965. bio_integrity_clone(clone, bio, GFP_NOIO);
  966. bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
  967. clone->bi_iter.bi_size = to_bytes(len);
  968. if (bio_integrity(bio))
  969. bio_integrity_trim(clone, 0, len);
  970. }
  971. static struct dm_target_io *alloc_tio(struct clone_info *ci,
  972. struct dm_target *ti, int nr_iovecs,
  973. unsigned target_bio_nr)
  974. {
  975. struct dm_target_io *tio;
  976. struct bio *clone;
  977. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, ci->md->bs);
  978. tio = container_of(clone, struct dm_target_io, clone);
  979. tio->io = ci->io;
  980. tio->ti = ti;
  981. memset(&tio->info, 0, sizeof(tio->info));
  982. tio->target_bio_nr = target_bio_nr;
  983. return tio;
  984. }
  985. static void __clone_and_map_simple_bio(struct clone_info *ci,
  986. struct dm_target *ti,
  987. unsigned target_bio_nr, sector_t len)
  988. {
  989. struct dm_target_io *tio = alloc_tio(ci, ti, ci->bio->bi_max_vecs, target_bio_nr);
  990. struct bio *clone = &tio->clone;
  991. /*
  992. * Discard requests require the bio's inline iovecs be initialized.
  993. * ci->bio->bi_max_vecs is BIO_INLINE_VECS anyway, for both flush
  994. * and discard, so no need for concern about wasted bvec allocations.
  995. */
  996. __bio_clone_fast(clone, ci->bio);
  997. if (len)
  998. bio_setup_sector(clone, ci->sector, len);
  999. __map_bio(tio);
  1000. }
  1001. static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
  1002. unsigned num_bios, sector_t len)
  1003. {
  1004. unsigned target_bio_nr;
  1005. for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
  1006. __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
  1007. }
  1008. static int __send_empty_flush(struct clone_info *ci)
  1009. {
  1010. unsigned target_nr = 0;
  1011. struct dm_target *ti;
  1012. BUG_ON(bio_has_data(ci->bio));
  1013. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  1014. __send_duplicate_bios(ci, ti, ti->num_flush_bios, 0);
  1015. return 0;
  1016. }
  1017. static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
  1018. sector_t sector, unsigned len)
  1019. {
  1020. struct bio *bio = ci->bio;
  1021. struct dm_target_io *tio;
  1022. unsigned target_bio_nr;
  1023. unsigned num_target_bios = 1;
  1024. /*
  1025. * Does the target want to receive duplicate copies of the bio?
  1026. */
  1027. if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
  1028. num_target_bios = ti->num_write_bios(ti, bio);
  1029. for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
  1030. tio = alloc_tio(ci, ti, 0, target_bio_nr);
  1031. clone_bio(tio, bio, sector, len);
  1032. __map_bio(tio);
  1033. }
  1034. }
  1035. typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
  1036. static unsigned get_num_discard_bios(struct dm_target *ti)
  1037. {
  1038. return ti->num_discard_bios;
  1039. }
  1040. static unsigned get_num_write_same_bios(struct dm_target *ti)
  1041. {
  1042. return ti->num_write_same_bios;
  1043. }
  1044. typedef bool (*is_split_required_fn)(struct dm_target *ti);
  1045. static bool is_split_required_for_discard(struct dm_target *ti)
  1046. {
  1047. return ti->split_discard_bios;
  1048. }
  1049. static int __send_changing_extent_only(struct clone_info *ci,
  1050. get_num_bios_fn get_num_bios,
  1051. is_split_required_fn is_split_required)
  1052. {
  1053. struct dm_target *ti;
  1054. sector_t len;
  1055. unsigned num_bios;
  1056. do {
  1057. ti = dm_table_find_target(ci->map, ci->sector);
  1058. if (!dm_target_is_valid(ti))
  1059. return -EIO;
  1060. /*
  1061. * Even though the device advertised support for this type of
  1062. * request, that does not mean every target supports it, and
  1063. * reconfiguration might also have changed that since the
  1064. * check was performed.
  1065. */
  1066. num_bios = get_num_bios ? get_num_bios(ti) : 0;
  1067. if (!num_bios)
  1068. return -EOPNOTSUPP;
  1069. if (is_split_required && !is_split_required(ti))
  1070. len = min(ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
  1071. else
  1072. len = min(ci->sector_count, max_io_len(ci->sector, ti));
  1073. __send_duplicate_bios(ci, ti, num_bios, len);
  1074. ci->sector += len;
  1075. } while (ci->sector_count -= len);
  1076. return 0;
  1077. }
  1078. static int __send_discard(struct clone_info *ci)
  1079. {
  1080. return __send_changing_extent_only(ci, get_num_discard_bios,
  1081. is_split_required_for_discard);
  1082. }
  1083. static int __send_write_same(struct clone_info *ci)
  1084. {
  1085. return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
  1086. }
  1087. /*
  1088. * Select the correct strategy for processing a non-flush bio.
  1089. */
  1090. static int __split_and_process_non_flush(struct clone_info *ci)
  1091. {
  1092. struct bio *bio = ci->bio;
  1093. struct dm_target *ti;
  1094. unsigned len;
  1095. if (unlikely(bio->bi_rw & REQ_DISCARD))
  1096. return __send_discard(ci);
  1097. else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
  1098. return __send_write_same(ci);
  1099. ti = dm_table_find_target(ci->map, ci->sector);
  1100. if (!dm_target_is_valid(ti))
  1101. return -EIO;
  1102. len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
  1103. __clone_and_map_data_bio(ci, ti, ci->sector, len);
  1104. ci->sector += len;
  1105. ci->sector_count -= len;
  1106. return 0;
  1107. }
  1108. /*
  1109. * Entry point to split a bio into clones and submit them to the targets.
  1110. */
  1111. static void __split_and_process_bio(struct mapped_device *md,
  1112. struct dm_table *map, struct bio *bio)
  1113. {
  1114. struct clone_info ci;
  1115. int error = 0;
  1116. if (unlikely(!map)) {
  1117. bio_io_error(bio);
  1118. return;
  1119. }
  1120. ci.map = map;
  1121. ci.md = md;
  1122. ci.io = alloc_io(md);
  1123. ci.io->error = 0;
  1124. atomic_set(&ci.io->io_count, 1);
  1125. ci.io->bio = bio;
  1126. ci.io->md = md;
  1127. spin_lock_init(&ci.io->endio_lock);
  1128. ci.sector = bio->bi_iter.bi_sector;
  1129. start_io_acct(ci.io);
  1130. if (bio->bi_rw & REQ_FLUSH) {
  1131. ci.bio = &ci.md->flush_bio;
  1132. ci.sector_count = 0;
  1133. error = __send_empty_flush(&ci);
  1134. /* dec_pending submits any data associated with flush */
  1135. } else {
  1136. ci.bio = bio;
  1137. ci.sector_count = bio_sectors(bio);
  1138. while (ci.sector_count && !error)
  1139. error = __split_and_process_non_flush(&ci);
  1140. }
  1141. /* drop the extra reference count */
  1142. dec_pending(ci.io, error);
  1143. }
  1144. /*-----------------------------------------------------------------
  1145. * CRUD END
  1146. *---------------------------------------------------------------*/
  1147. static int dm_merge_bvec(struct request_queue *q,
  1148. struct bvec_merge_data *bvm,
  1149. struct bio_vec *biovec)
  1150. {
  1151. struct mapped_device *md = q->queuedata;
  1152. struct dm_table *map = dm_get_live_table_fast(md);
  1153. struct dm_target *ti;
  1154. sector_t max_sectors;
  1155. int max_size = 0;
  1156. if (unlikely(!map))
  1157. goto out;
  1158. ti = dm_table_find_target(map, bvm->bi_sector);
  1159. if (!dm_target_is_valid(ti))
  1160. goto out;
  1161. /*
  1162. * Find maximum amount of I/O that won't need splitting
  1163. */
  1164. max_sectors = min(max_io_len(bvm->bi_sector, ti),
  1165. (sector_t) BIO_MAX_SECTORS);
  1166. max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
  1167. if (max_size < 0)
  1168. max_size = 0;
  1169. /*
  1170. * merge_bvec_fn() returns number of bytes
  1171. * it can accept at this offset
  1172. * max is precomputed maximal io size
  1173. */
  1174. if (max_size && ti->type->merge)
  1175. max_size = ti->type->merge(ti, bvm, biovec, max_size);
  1176. /*
  1177. * If the target doesn't support merge method and some of the devices
  1178. * provided their merge_bvec method (we know this by looking at
  1179. * queue_max_hw_sectors), then we can't allow bios with multiple vector
  1180. * entries. So always set max_size to 0, and the code below allows
  1181. * just one page.
  1182. */
  1183. else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
  1184. max_size = 0;
  1185. out:
  1186. dm_put_live_table_fast(md);
  1187. /*
  1188. * Always allow an entire first page
  1189. */
  1190. if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
  1191. max_size = biovec->bv_len;
  1192. return max_size;
  1193. }
  1194. /*
  1195. * The request function that just remaps the bio built up by
  1196. * dm_merge_bvec.
  1197. */
  1198. static void _dm_request(struct request_queue *q, struct bio *bio)
  1199. {
  1200. int rw = bio_data_dir(bio);
  1201. struct mapped_device *md = q->queuedata;
  1202. int cpu;
  1203. int srcu_idx;
  1204. struct dm_table *map;
  1205. map = dm_get_live_table(md, &srcu_idx);
  1206. cpu = part_stat_lock();
  1207. part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
  1208. part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
  1209. part_stat_unlock();
  1210. /* if we're suspended, we have to queue this io for later */
  1211. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
  1212. dm_put_live_table(md, srcu_idx);
  1213. if (bio_rw(bio) != READA)
  1214. queue_io(md, bio);
  1215. else
  1216. bio_io_error(bio);
  1217. return;
  1218. }
  1219. __split_and_process_bio(md, map, bio);
  1220. dm_put_live_table(md, srcu_idx);
  1221. return;
  1222. }
  1223. int dm_request_based(struct mapped_device *md)
  1224. {
  1225. return blk_queue_stackable(md->queue);
  1226. }
  1227. static void dm_request(struct request_queue *q, struct bio *bio)
  1228. {
  1229. struct mapped_device *md = q->queuedata;
  1230. if (dm_request_based(md))
  1231. blk_queue_bio(q, bio);
  1232. else
  1233. _dm_request(q, bio);
  1234. }
  1235. void dm_dispatch_request(struct request *rq)
  1236. {
  1237. int r;
  1238. if (blk_queue_io_stat(rq->q))
  1239. rq->cmd_flags |= REQ_IO_STAT;
  1240. rq->start_time = jiffies;
  1241. r = blk_insert_cloned_request(rq->q, rq);
  1242. if (r)
  1243. dm_complete_request(rq, r);
  1244. }
  1245. EXPORT_SYMBOL_GPL(dm_dispatch_request);
  1246. static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
  1247. void *data)
  1248. {
  1249. struct dm_rq_target_io *tio = data;
  1250. struct dm_rq_clone_bio_info *info =
  1251. container_of(bio, struct dm_rq_clone_bio_info, clone);
  1252. info->orig = bio_orig;
  1253. info->tio = tio;
  1254. bio->bi_end_io = end_clone_bio;
  1255. bio->bi_private = info;
  1256. return 0;
  1257. }
  1258. static int setup_clone(struct request *clone, struct request *rq,
  1259. struct dm_rq_target_io *tio)
  1260. {
  1261. int r;
  1262. r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
  1263. dm_rq_bio_constructor, tio);
  1264. if (r)
  1265. return r;
  1266. clone->cmd = rq->cmd;
  1267. clone->cmd_len = rq->cmd_len;
  1268. clone->sense = rq->sense;
  1269. clone->buffer = rq->buffer;
  1270. clone->end_io = end_clone_request;
  1271. clone->end_io_data = tio;
  1272. return 0;
  1273. }
  1274. static struct request *clone_rq(struct request *rq, struct mapped_device *md,
  1275. gfp_t gfp_mask)
  1276. {
  1277. struct request *clone;
  1278. struct dm_rq_target_io *tio;
  1279. tio = alloc_rq_tio(md, gfp_mask);
  1280. if (!tio)
  1281. return NULL;
  1282. tio->md = md;
  1283. tio->ti = NULL;
  1284. tio->orig = rq;
  1285. tio->error = 0;
  1286. memset(&tio->info, 0, sizeof(tio->info));
  1287. clone = &tio->clone;
  1288. if (setup_clone(clone, rq, tio)) {
  1289. /* -ENOMEM */
  1290. free_rq_tio(tio);
  1291. return NULL;
  1292. }
  1293. return clone;
  1294. }
  1295. /*
  1296. * Called with the queue lock held.
  1297. */
  1298. static int dm_prep_fn(struct request_queue *q, struct request *rq)
  1299. {
  1300. struct mapped_device *md = q->queuedata;
  1301. struct request *clone;
  1302. if (unlikely(rq->special)) {
  1303. DMWARN("Already has something in rq->special.");
  1304. return BLKPREP_KILL;
  1305. }
  1306. clone = clone_rq(rq, md, GFP_ATOMIC);
  1307. if (!clone)
  1308. return BLKPREP_DEFER;
  1309. rq->special = clone;
  1310. rq->cmd_flags |= REQ_DONTPREP;
  1311. return BLKPREP_OK;
  1312. }
  1313. /*
  1314. * Returns:
  1315. * 0 : the request has been processed (not requeued)
  1316. * !0 : the request has been requeued
  1317. */
  1318. static int map_request(struct dm_target *ti, struct request *clone,
  1319. struct mapped_device *md)
  1320. {
  1321. int r, requeued = 0;
  1322. struct dm_rq_target_io *tio = clone->end_io_data;
  1323. tio->ti = ti;
  1324. r = ti->type->map_rq(ti, clone, &tio->info);
  1325. switch (r) {
  1326. case DM_MAPIO_SUBMITTED:
  1327. /* The target has taken the I/O to submit by itself later */
  1328. break;
  1329. case DM_MAPIO_REMAPPED:
  1330. /* The target has remapped the I/O so dispatch it */
  1331. trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
  1332. blk_rq_pos(tio->orig));
  1333. dm_dispatch_request(clone);
  1334. break;
  1335. case DM_MAPIO_REQUEUE:
  1336. /* The target wants to requeue the I/O */
  1337. dm_requeue_unmapped_request(clone);
  1338. requeued = 1;
  1339. break;
  1340. default:
  1341. if (r > 0) {
  1342. DMWARN("unimplemented target map return value: %d", r);
  1343. BUG();
  1344. }
  1345. /* The target wants to complete the I/O */
  1346. dm_kill_unmapped_request(clone, r);
  1347. break;
  1348. }
  1349. return requeued;
  1350. }
  1351. static struct request *dm_start_request(struct mapped_device *md, struct request *orig)
  1352. {
  1353. struct request *clone;
  1354. blk_start_request(orig);
  1355. clone = orig->special;
  1356. atomic_inc(&md->pending[rq_data_dir(clone)]);
  1357. /*
  1358. * Hold the md reference here for the in-flight I/O.
  1359. * We can't rely on the reference count by device opener,
  1360. * because the device may be closed during the request completion
  1361. * when all bios are completed.
  1362. * See the comment in rq_completed() too.
  1363. */
  1364. dm_get(md);
  1365. return clone;
  1366. }
  1367. /*
  1368. * q->request_fn for request-based dm.
  1369. * Called with the queue lock held.
  1370. */
  1371. static void dm_request_fn(struct request_queue *q)
  1372. {
  1373. struct mapped_device *md = q->queuedata;
  1374. int srcu_idx;
  1375. struct dm_table *map = dm_get_live_table(md, &srcu_idx);
  1376. struct dm_target *ti;
  1377. struct request *rq, *clone;
  1378. sector_t pos;
  1379. /*
  1380. * For suspend, check blk_queue_stopped() and increment
  1381. * ->pending within a single queue_lock not to increment the
  1382. * number of in-flight I/Os after the queue is stopped in
  1383. * dm_suspend().
  1384. */
  1385. while (!blk_queue_stopped(q)) {
  1386. rq = blk_peek_request(q);
  1387. if (!rq)
  1388. goto delay_and_out;
  1389. /* always use block 0 to find the target for flushes for now */
  1390. pos = 0;
  1391. if (!(rq->cmd_flags & REQ_FLUSH))
  1392. pos = blk_rq_pos(rq);
  1393. ti = dm_table_find_target(map, pos);
  1394. if (!dm_target_is_valid(ti)) {
  1395. /*
  1396. * Must perform setup, that dm_done() requires,
  1397. * before calling dm_kill_unmapped_request
  1398. */
  1399. DMERR_LIMIT("request attempted access beyond the end of device");
  1400. clone = dm_start_request(md, rq);
  1401. dm_kill_unmapped_request(clone, -EIO);
  1402. continue;
  1403. }
  1404. if (ti->type->busy && ti->type->busy(ti))
  1405. goto delay_and_out;
  1406. clone = dm_start_request(md, rq);
  1407. spin_unlock(q->queue_lock);
  1408. if (map_request(ti, clone, md))
  1409. goto requeued;
  1410. BUG_ON(!irqs_disabled());
  1411. spin_lock(q->queue_lock);
  1412. }
  1413. goto out;
  1414. requeued:
  1415. BUG_ON(!irqs_disabled());
  1416. spin_lock(q->queue_lock);
  1417. delay_and_out:
  1418. blk_delay_queue(q, HZ / 10);
  1419. out:
  1420. dm_put_live_table(md, srcu_idx);
  1421. }
  1422. int dm_underlying_device_busy(struct request_queue *q)
  1423. {
  1424. return blk_lld_busy(q);
  1425. }
  1426. EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
  1427. static int dm_lld_busy(struct request_queue *q)
  1428. {
  1429. int r;
  1430. struct mapped_device *md = q->queuedata;
  1431. struct dm_table *map = dm_get_live_table_fast(md);
  1432. if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
  1433. r = 1;
  1434. else
  1435. r = dm_table_any_busy_target(map);
  1436. dm_put_live_table_fast(md);
  1437. return r;
  1438. }
  1439. static int dm_any_congested(void *congested_data, int bdi_bits)
  1440. {
  1441. int r = bdi_bits;
  1442. struct mapped_device *md = congested_data;
  1443. struct dm_table *map;
  1444. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1445. map = dm_get_live_table_fast(md);
  1446. if (map) {
  1447. /*
  1448. * Request-based dm cares about only own queue for
  1449. * the query about congestion status of request_queue
  1450. */
  1451. if (dm_request_based(md))
  1452. r = md->queue->backing_dev_info.state &
  1453. bdi_bits;
  1454. else
  1455. r = dm_table_any_congested(map, bdi_bits);
  1456. }
  1457. dm_put_live_table_fast(md);
  1458. }
  1459. return r;
  1460. }
  1461. /*-----------------------------------------------------------------
  1462. * An IDR is used to keep track of allocated minor numbers.
  1463. *---------------------------------------------------------------*/
  1464. static void free_minor(int minor)
  1465. {
  1466. spin_lock(&_minor_lock);
  1467. idr_remove(&_minor_idr, minor);
  1468. spin_unlock(&_minor_lock);
  1469. }
  1470. /*
  1471. * See if the device with a specific minor # is free.
  1472. */
  1473. static int specific_minor(int minor)
  1474. {
  1475. int r;
  1476. if (minor >= (1 << MINORBITS))
  1477. return -EINVAL;
  1478. idr_preload(GFP_KERNEL);
  1479. spin_lock(&_minor_lock);
  1480. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
  1481. spin_unlock(&_minor_lock);
  1482. idr_preload_end();
  1483. if (r < 0)
  1484. return r == -ENOSPC ? -EBUSY : r;
  1485. return 0;
  1486. }
  1487. static int next_free_minor(int *minor)
  1488. {
  1489. int r;
  1490. idr_preload(GFP_KERNEL);
  1491. spin_lock(&_minor_lock);
  1492. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
  1493. spin_unlock(&_minor_lock);
  1494. idr_preload_end();
  1495. if (r < 0)
  1496. return r;
  1497. *minor = r;
  1498. return 0;
  1499. }
  1500. static const struct block_device_operations dm_blk_dops;
  1501. static void dm_wq_work(struct work_struct *work);
  1502. static void dm_init_md_queue(struct mapped_device *md)
  1503. {
  1504. /*
  1505. * Request-based dm devices cannot be stacked on top of bio-based dm
  1506. * devices. The type of this dm device has not been decided yet.
  1507. * The type is decided at the first table loading time.
  1508. * To prevent problematic device stacking, clear the queue flag
  1509. * for request stacking support until then.
  1510. *
  1511. * This queue is new, so no concurrency on the queue_flags.
  1512. */
  1513. queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
  1514. md->queue->queuedata = md;
  1515. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  1516. md->queue->backing_dev_info.congested_data = md;
  1517. blk_queue_make_request(md->queue, dm_request);
  1518. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  1519. blk_queue_merge_bvec(md->queue, dm_merge_bvec);
  1520. }
  1521. /*
  1522. * Allocate and initialise a blank device with a given minor.
  1523. */
  1524. static struct mapped_device *alloc_dev(int minor)
  1525. {
  1526. int r;
  1527. struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
  1528. void *old_md;
  1529. if (!md) {
  1530. DMWARN("unable to allocate device, out of memory.");
  1531. return NULL;
  1532. }
  1533. if (!try_module_get(THIS_MODULE))
  1534. goto bad_module_get;
  1535. /* get a minor number for the dev */
  1536. if (minor == DM_ANY_MINOR)
  1537. r = next_free_minor(&minor);
  1538. else
  1539. r = specific_minor(minor);
  1540. if (r < 0)
  1541. goto bad_minor;
  1542. r = init_srcu_struct(&md->io_barrier);
  1543. if (r < 0)
  1544. goto bad_io_barrier;
  1545. md->type = DM_TYPE_NONE;
  1546. mutex_init(&md->suspend_lock);
  1547. mutex_init(&md->type_lock);
  1548. spin_lock_init(&md->deferred_lock);
  1549. atomic_set(&md->holders, 1);
  1550. atomic_set(&md->open_count, 0);
  1551. atomic_set(&md->event_nr, 0);
  1552. atomic_set(&md->uevent_seq, 0);
  1553. INIT_LIST_HEAD(&md->uevent_list);
  1554. spin_lock_init(&md->uevent_lock);
  1555. md->queue = blk_alloc_queue(GFP_KERNEL);
  1556. if (!md->queue)
  1557. goto bad_queue;
  1558. dm_init_md_queue(md);
  1559. md->disk = alloc_disk(1);
  1560. if (!md->disk)
  1561. goto bad_disk;
  1562. atomic_set(&md->pending[0], 0);
  1563. atomic_set(&md->pending[1], 0);
  1564. init_waitqueue_head(&md->wait);
  1565. INIT_WORK(&md->work, dm_wq_work);
  1566. init_waitqueue_head(&md->eventq);
  1567. init_completion(&md->kobj_holder.completion);
  1568. md->disk->major = _major;
  1569. md->disk->first_minor = minor;
  1570. md->disk->fops = &dm_blk_dops;
  1571. md->disk->queue = md->queue;
  1572. md->disk->private_data = md;
  1573. sprintf(md->disk->disk_name, "dm-%d", minor);
  1574. add_disk(md->disk);
  1575. format_dev_t(md->name, MKDEV(_major, minor));
  1576. md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
  1577. if (!md->wq)
  1578. goto bad_thread;
  1579. md->bdev = bdget_disk(md->disk, 0);
  1580. if (!md->bdev)
  1581. goto bad_bdev;
  1582. bio_init(&md->flush_bio);
  1583. md->flush_bio.bi_bdev = md->bdev;
  1584. md->flush_bio.bi_rw = WRITE_FLUSH;
  1585. dm_stats_init(&md->stats);
  1586. /* Populate the mapping, nobody knows we exist yet */
  1587. spin_lock(&_minor_lock);
  1588. old_md = idr_replace(&_minor_idr, md, minor);
  1589. spin_unlock(&_minor_lock);
  1590. BUG_ON(old_md != MINOR_ALLOCED);
  1591. return md;
  1592. bad_bdev:
  1593. destroy_workqueue(md->wq);
  1594. bad_thread:
  1595. del_gendisk(md->disk);
  1596. put_disk(md->disk);
  1597. bad_disk:
  1598. blk_cleanup_queue(md->queue);
  1599. bad_queue:
  1600. cleanup_srcu_struct(&md->io_barrier);
  1601. bad_io_barrier:
  1602. free_minor(minor);
  1603. bad_minor:
  1604. module_put(THIS_MODULE);
  1605. bad_module_get:
  1606. kfree(md);
  1607. return NULL;
  1608. }
  1609. static void unlock_fs(struct mapped_device *md);
  1610. static void free_dev(struct mapped_device *md)
  1611. {
  1612. int minor = MINOR(disk_devt(md->disk));
  1613. unlock_fs(md);
  1614. bdput(md->bdev);
  1615. destroy_workqueue(md->wq);
  1616. if (md->io_pool)
  1617. mempool_destroy(md->io_pool);
  1618. if (md->bs)
  1619. bioset_free(md->bs);
  1620. blk_integrity_unregister(md->disk);
  1621. del_gendisk(md->disk);
  1622. cleanup_srcu_struct(&md->io_barrier);
  1623. free_minor(minor);
  1624. spin_lock(&_minor_lock);
  1625. md->disk->private_data = NULL;
  1626. spin_unlock(&_minor_lock);
  1627. put_disk(md->disk);
  1628. blk_cleanup_queue(md->queue);
  1629. dm_stats_cleanup(&md->stats);
  1630. module_put(THIS_MODULE);
  1631. kfree(md);
  1632. }
  1633. static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
  1634. {
  1635. struct dm_md_mempools *p = dm_table_get_md_mempools(t);
  1636. if (md->io_pool && md->bs) {
  1637. /* The md already has necessary mempools. */
  1638. if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
  1639. /*
  1640. * Reload bioset because front_pad may have changed
  1641. * because a different table was loaded.
  1642. */
  1643. bioset_free(md->bs);
  1644. md->bs = p->bs;
  1645. p->bs = NULL;
  1646. } else if (dm_table_get_type(t) == DM_TYPE_REQUEST_BASED) {
  1647. /*
  1648. * There's no need to reload with request-based dm
  1649. * because the size of front_pad doesn't change.
  1650. * Note for future: If you are to reload bioset,
  1651. * prep-ed requests in the queue may refer
  1652. * to bio from the old bioset, so you must walk
  1653. * through the queue to unprep.
  1654. */
  1655. }
  1656. goto out;
  1657. }
  1658. BUG_ON(!p || md->io_pool || md->bs);
  1659. md->io_pool = p->io_pool;
  1660. p->io_pool = NULL;
  1661. md->bs = p->bs;
  1662. p->bs = NULL;
  1663. out:
  1664. /* mempool bind completed, now no need any mempools in the table */
  1665. dm_table_free_md_mempools(t);
  1666. }
  1667. /*
  1668. * Bind a table to the device.
  1669. */
  1670. static void event_callback(void *context)
  1671. {
  1672. unsigned long flags;
  1673. LIST_HEAD(uevents);
  1674. struct mapped_device *md = (struct mapped_device *) context;
  1675. spin_lock_irqsave(&md->uevent_lock, flags);
  1676. list_splice_init(&md->uevent_list, &uevents);
  1677. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1678. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1679. atomic_inc(&md->event_nr);
  1680. wake_up(&md->eventq);
  1681. }
  1682. /*
  1683. * Protected by md->suspend_lock obtained by dm_swap_table().
  1684. */
  1685. static void __set_size(struct mapped_device *md, sector_t size)
  1686. {
  1687. set_capacity(md->disk, size);
  1688. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1689. }
  1690. /*
  1691. * Return 1 if the queue has a compulsory merge_bvec_fn function.
  1692. *
  1693. * If this function returns 0, then the device is either a non-dm
  1694. * device without a merge_bvec_fn, or it is a dm device that is
  1695. * able to split any bios it receives that are too big.
  1696. */
  1697. int dm_queue_merge_is_compulsory(struct request_queue *q)
  1698. {
  1699. struct mapped_device *dev_md;
  1700. if (!q->merge_bvec_fn)
  1701. return 0;
  1702. if (q->make_request_fn == dm_request) {
  1703. dev_md = q->queuedata;
  1704. if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
  1705. return 0;
  1706. }
  1707. return 1;
  1708. }
  1709. static int dm_device_merge_is_compulsory(struct dm_target *ti,
  1710. struct dm_dev *dev, sector_t start,
  1711. sector_t len, void *data)
  1712. {
  1713. struct block_device *bdev = dev->bdev;
  1714. struct request_queue *q = bdev_get_queue(bdev);
  1715. return dm_queue_merge_is_compulsory(q);
  1716. }
  1717. /*
  1718. * Return 1 if it is acceptable to ignore merge_bvec_fn based
  1719. * on the properties of the underlying devices.
  1720. */
  1721. static int dm_table_merge_is_optional(struct dm_table *table)
  1722. {
  1723. unsigned i = 0;
  1724. struct dm_target *ti;
  1725. while (i < dm_table_get_num_targets(table)) {
  1726. ti = dm_table_get_target(table, i++);
  1727. if (ti->type->iterate_devices &&
  1728. ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
  1729. return 0;
  1730. }
  1731. return 1;
  1732. }
  1733. /*
  1734. * Returns old map, which caller must destroy.
  1735. */
  1736. static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
  1737. struct queue_limits *limits)
  1738. {
  1739. struct dm_table *old_map;
  1740. struct request_queue *q = md->queue;
  1741. sector_t size;
  1742. int merge_is_optional;
  1743. size = dm_table_get_size(t);
  1744. /*
  1745. * Wipe any geometry if the size of the table changed.
  1746. */
  1747. if (size != dm_get_size(md))
  1748. memset(&md->geometry, 0, sizeof(md->geometry));
  1749. __set_size(md, size);
  1750. dm_table_event_callback(t, event_callback, md);
  1751. /*
  1752. * The queue hasn't been stopped yet, if the old table type wasn't
  1753. * for request-based during suspension. So stop it to prevent
  1754. * I/O mapping before resume.
  1755. * This must be done before setting the queue restrictions,
  1756. * because request-based dm may be run just after the setting.
  1757. */
  1758. if (dm_table_request_based(t) && !blk_queue_stopped(q))
  1759. stop_queue(q);
  1760. __bind_mempools(md, t);
  1761. merge_is_optional = dm_table_merge_is_optional(t);
  1762. old_map = md->map;
  1763. rcu_assign_pointer(md->map, t);
  1764. md->immutable_target_type = dm_table_get_immutable_target_type(t);
  1765. dm_table_set_restrictions(t, q, limits);
  1766. if (merge_is_optional)
  1767. set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
  1768. else
  1769. clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
  1770. dm_sync_table(md);
  1771. return old_map;
  1772. }
  1773. /*
  1774. * Returns unbound table for the caller to free.
  1775. */
  1776. static struct dm_table *__unbind(struct mapped_device *md)
  1777. {
  1778. struct dm_table *map = md->map;
  1779. if (!map)
  1780. return NULL;
  1781. dm_table_event_callback(map, NULL, NULL);
  1782. rcu_assign_pointer(md->map, NULL);
  1783. dm_sync_table(md);
  1784. return map;
  1785. }
  1786. /*
  1787. * Constructor for a new device.
  1788. */
  1789. int dm_create(int minor, struct mapped_device **result)
  1790. {
  1791. struct mapped_device *md;
  1792. md = alloc_dev(minor);
  1793. if (!md)
  1794. return -ENXIO;
  1795. dm_sysfs_init(md);
  1796. *result = md;
  1797. return 0;
  1798. }
  1799. /*
  1800. * Functions to manage md->type.
  1801. * All are required to hold md->type_lock.
  1802. */
  1803. void dm_lock_md_type(struct mapped_device *md)
  1804. {
  1805. mutex_lock(&md->type_lock);
  1806. }
  1807. void dm_unlock_md_type(struct mapped_device *md)
  1808. {
  1809. mutex_unlock(&md->type_lock);
  1810. }
  1811. void dm_set_md_type(struct mapped_device *md, unsigned type)
  1812. {
  1813. BUG_ON(!mutex_is_locked(&md->type_lock));
  1814. md->type = type;
  1815. }
  1816. unsigned dm_get_md_type(struct mapped_device *md)
  1817. {
  1818. BUG_ON(!mutex_is_locked(&md->type_lock));
  1819. return md->type;
  1820. }
  1821. struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
  1822. {
  1823. return md->immutable_target_type;
  1824. }
  1825. /*
  1826. * The queue_limits are only valid as long as you have a reference
  1827. * count on 'md'.
  1828. */
  1829. struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
  1830. {
  1831. BUG_ON(!atomic_read(&md->holders));
  1832. return &md->queue->limits;
  1833. }
  1834. EXPORT_SYMBOL_GPL(dm_get_queue_limits);
  1835. /*
  1836. * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
  1837. */
  1838. static int dm_init_request_based_queue(struct mapped_device *md)
  1839. {
  1840. struct request_queue *q = NULL;
  1841. if (md->queue->elevator)
  1842. return 1;
  1843. /* Fully initialize the queue */
  1844. q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
  1845. if (!q)
  1846. return 0;
  1847. md->queue = q;
  1848. dm_init_md_queue(md);
  1849. blk_queue_softirq_done(md->queue, dm_softirq_done);
  1850. blk_queue_prep_rq(md->queue, dm_prep_fn);
  1851. blk_queue_lld_busy(md->queue, dm_lld_busy);
  1852. elv_register_queue(md->queue);
  1853. return 1;
  1854. }
  1855. /*
  1856. * Setup the DM device's queue based on md's type
  1857. */
  1858. int dm_setup_md_queue(struct mapped_device *md)
  1859. {
  1860. if ((dm_get_md_type(md) == DM_TYPE_REQUEST_BASED) &&
  1861. !dm_init_request_based_queue(md)) {
  1862. DMWARN("Cannot initialize queue for request-based mapped device");
  1863. return -EINVAL;
  1864. }
  1865. return 0;
  1866. }
  1867. static struct mapped_device *dm_find_md(dev_t dev)
  1868. {
  1869. struct mapped_device *md;
  1870. unsigned minor = MINOR(dev);
  1871. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1872. return NULL;
  1873. spin_lock(&_minor_lock);
  1874. md = idr_find(&_minor_idr, minor);
  1875. if (md && (md == MINOR_ALLOCED ||
  1876. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1877. dm_deleting_md(md) ||
  1878. test_bit(DMF_FREEING, &md->flags))) {
  1879. md = NULL;
  1880. goto out;
  1881. }
  1882. out:
  1883. spin_unlock(&_minor_lock);
  1884. return md;
  1885. }
  1886. struct mapped_device *dm_get_md(dev_t dev)
  1887. {
  1888. struct mapped_device *md = dm_find_md(dev);
  1889. if (md)
  1890. dm_get(md);
  1891. return md;
  1892. }
  1893. EXPORT_SYMBOL_GPL(dm_get_md);
  1894. void *dm_get_mdptr(struct mapped_device *md)
  1895. {
  1896. return md->interface_ptr;
  1897. }
  1898. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1899. {
  1900. md->interface_ptr = ptr;
  1901. }
  1902. void dm_get(struct mapped_device *md)
  1903. {
  1904. atomic_inc(&md->holders);
  1905. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1906. }
  1907. const char *dm_device_name(struct mapped_device *md)
  1908. {
  1909. return md->name;
  1910. }
  1911. EXPORT_SYMBOL_GPL(dm_device_name);
  1912. static void __dm_destroy(struct mapped_device *md, bool wait)
  1913. {
  1914. struct dm_table *map;
  1915. int srcu_idx;
  1916. might_sleep();
  1917. spin_lock(&_minor_lock);
  1918. map = dm_get_live_table(md, &srcu_idx);
  1919. idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
  1920. set_bit(DMF_FREEING, &md->flags);
  1921. spin_unlock(&_minor_lock);
  1922. if (!dm_suspended_md(md)) {
  1923. dm_table_presuspend_targets(map);
  1924. dm_table_postsuspend_targets(map);
  1925. }
  1926. /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
  1927. dm_put_live_table(md, srcu_idx);
  1928. /*
  1929. * Rare, but there may be I/O requests still going to complete,
  1930. * for example. Wait for all references to disappear.
  1931. * No one should increment the reference count of the mapped_device,
  1932. * after the mapped_device state becomes DMF_FREEING.
  1933. */
  1934. if (wait)
  1935. while (atomic_read(&md->holders))
  1936. msleep(1);
  1937. else if (atomic_read(&md->holders))
  1938. DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
  1939. dm_device_name(md), atomic_read(&md->holders));
  1940. dm_sysfs_exit(md);
  1941. dm_table_destroy(__unbind(md));
  1942. free_dev(md);
  1943. }
  1944. void dm_destroy(struct mapped_device *md)
  1945. {
  1946. __dm_destroy(md, true);
  1947. }
  1948. void dm_destroy_immediate(struct mapped_device *md)
  1949. {
  1950. __dm_destroy(md, false);
  1951. }
  1952. void dm_put(struct mapped_device *md)
  1953. {
  1954. atomic_dec(&md->holders);
  1955. }
  1956. EXPORT_SYMBOL_GPL(dm_put);
  1957. static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
  1958. {
  1959. int r = 0;
  1960. DECLARE_WAITQUEUE(wait, current);
  1961. add_wait_queue(&md->wait, &wait);
  1962. while (1) {
  1963. set_current_state(interruptible);
  1964. if (!md_in_flight(md))
  1965. break;
  1966. if (interruptible == TASK_INTERRUPTIBLE &&
  1967. signal_pending(current)) {
  1968. r = -EINTR;
  1969. break;
  1970. }
  1971. io_schedule();
  1972. }
  1973. set_current_state(TASK_RUNNING);
  1974. remove_wait_queue(&md->wait, &wait);
  1975. return r;
  1976. }
  1977. /*
  1978. * Process the deferred bios
  1979. */
  1980. static void dm_wq_work(struct work_struct *work)
  1981. {
  1982. struct mapped_device *md = container_of(work, struct mapped_device,
  1983. work);
  1984. struct bio *c;
  1985. int srcu_idx;
  1986. struct dm_table *map;
  1987. map = dm_get_live_table(md, &srcu_idx);
  1988. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1989. spin_lock_irq(&md->deferred_lock);
  1990. c = bio_list_pop(&md->deferred);
  1991. spin_unlock_irq(&md->deferred_lock);
  1992. if (!c)
  1993. break;
  1994. if (dm_request_based(md))
  1995. generic_make_request(c);
  1996. else
  1997. __split_and_process_bio(md, map, c);
  1998. }
  1999. dm_put_live_table(md, srcu_idx);
  2000. }
  2001. static void dm_queue_flush(struct mapped_device *md)
  2002. {
  2003. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2004. smp_mb__after_clear_bit();
  2005. queue_work(md->wq, &md->work);
  2006. }
  2007. /*
  2008. * Swap in a new table, returning the old one for the caller to destroy.
  2009. */
  2010. struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  2011. {
  2012. struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
  2013. struct queue_limits limits;
  2014. int r;
  2015. mutex_lock(&md->suspend_lock);
  2016. /* device must be suspended */
  2017. if (!dm_suspended_md(md))
  2018. goto out;
  2019. /*
  2020. * If the new table has no data devices, retain the existing limits.
  2021. * This helps multipath with queue_if_no_path if all paths disappear,
  2022. * then new I/O is queued based on these limits, and then some paths
  2023. * reappear.
  2024. */
  2025. if (dm_table_has_no_data_devices(table)) {
  2026. live_map = dm_get_live_table_fast(md);
  2027. if (live_map)
  2028. limits = md->queue->limits;
  2029. dm_put_live_table_fast(md);
  2030. }
  2031. if (!live_map) {
  2032. r = dm_calculate_queue_limits(table, &limits);
  2033. if (r) {
  2034. map = ERR_PTR(r);
  2035. goto out;
  2036. }
  2037. }
  2038. map = __bind(md, table, &limits);
  2039. out:
  2040. mutex_unlock(&md->suspend_lock);
  2041. return map;
  2042. }
  2043. /*
  2044. * Functions to lock and unlock any filesystem running on the
  2045. * device.
  2046. */
  2047. static int lock_fs(struct mapped_device *md)
  2048. {
  2049. int r;
  2050. WARN_ON(md->frozen_sb);
  2051. md->frozen_sb = freeze_bdev(md->bdev);
  2052. if (IS_ERR(md->frozen_sb)) {
  2053. r = PTR_ERR(md->frozen_sb);
  2054. md->frozen_sb = NULL;
  2055. return r;
  2056. }
  2057. set_bit(DMF_FROZEN, &md->flags);
  2058. return 0;
  2059. }
  2060. static void unlock_fs(struct mapped_device *md)
  2061. {
  2062. if (!test_bit(DMF_FROZEN, &md->flags))
  2063. return;
  2064. thaw_bdev(md->bdev, md->frozen_sb);
  2065. md->frozen_sb = NULL;
  2066. clear_bit(DMF_FROZEN, &md->flags);
  2067. }
  2068. /*
  2069. * We need to be able to change a mapping table under a mounted
  2070. * filesystem. For example we might want to move some data in
  2071. * the background. Before the table can be swapped with
  2072. * dm_bind_table, dm_suspend must be called to flush any in
  2073. * flight bios and ensure that any further io gets deferred.
  2074. */
  2075. /*
  2076. * Suspend mechanism in request-based dm.
  2077. *
  2078. * 1. Flush all I/Os by lock_fs() if needed.
  2079. * 2. Stop dispatching any I/O by stopping the request_queue.
  2080. * 3. Wait for all in-flight I/Os to be completed or requeued.
  2081. *
  2082. * To abort suspend, start the request_queue.
  2083. */
  2084. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  2085. {
  2086. struct dm_table *map = NULL;
  2087. int r = 0;
  2088. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  2089. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  2090. mutex_lock(&md->suspend_lock);
  2091. if (dm_suspended_md(md)) {
  2092. r = -EINVAL;
  2093. goto out_unlock;
  2094. }
  2095. map = md->map;
  2096. /*
  2097. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  2098. * This flag is cleared before dm_suspend returns.
  2099. */
  2100. if (noflush)
  2101. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2102. /* This does not get reverted if there's an error later. */
  2103. dm_table_presuspend_targets(map);
  2104. /*
  2105. * Flush I/O to the device.
  2106. * Any I/O submitted after lock_fs() may not be flushed.
  2107. * noflush takes precedence over do_lockfs.
  2108. * (lock_fs() flushes I/Os and waits for them to complete.)
  2109. */
  2110. if (!noflush && do_lockfs) {
  2111. r = lock_fs(md);
  2112. if (r)
  2113. goto out_unlock;
  2114. }
  2115. /*
  2116. * Here we must make sure that no processes are submitting requests
  2117. * to target drivers i.e. no one may be executing
  2118. * __split_and_process_bio. This is called from dm_request and
  2119. * dm_wq_work.
  2120. *
  2121. * To get all processes out of __split_and_process_bio in dm_request,
  2122. * we take the write lock. To prevent any process from reentering
  2123. * __split_and_process_bio from dm_request and quiesce the thread
  2124. * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
  2125. * flush_workqueue(md->wq).
  2126. */
  2127. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2128. synchronize_srcu(&md->io_barrier);
  2129. /*
  2130. * Stop md->queue before flushing md->wq in case request-based
  2131. * dm defers requests to md->wq from md->queue.
  2132. */
  2133. if (dm_request_based(md))
  2134. stop_queue(md->queue);
  2135. flush_workqueue(md->wq);
  2136. /*
  2137. * At this point no more requests are entering target request routines.
  2138. * We call dm_wait_for_completion to wait for all existing requests
  2139. * to finish.
  2140. */
  2141. r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
  2142. if (noflush)
  2143. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2144. synchronize_srcu(&md->io_barrier);
  2145. /* were we interrupted ? */
  2146. if (r < 0) {
  2147. dm_queue_flush(md);
  2148. if (dm_request_based(md))
  2149. start_queue(md->queue);
  2150. unlock_fs(md);
  2151. goto out_unlock; /* pushback list is already flushed, so skip flush */
  2152. }
  2153. /*
  2154. * If dm_wait_for_completion returned 0, the device is completely
  2155. * quiescent now. There is no request-processing activity. All new
  2156. * requests are being added to md->deferred list.
  2157. */
  2158. set_bit(DMF_SUSPENDED, &md->flags);
  2159. dm_table_postsuspend_targets(map);
  2160. out_unlock:
  2161. mutex_unlock(&md->suspend_lock);
  2162. return r;
  2163. }
  2164. int dm_resume(struct mapped_device *md)
  2165. {
  2166. int r = -EINVAL;
  2167. struct dm_table *map = NULL;
  2168. mutex_lock(&md->suspend_lock);
  2169. if (!dm_suspended_md(md))
  2170. goto out;
  2171. map = md->map;
  2172. if (!map || !dm_table_get_size(map))
  2173. goto out;
  2174. r = dm_table_resume_targets(map);
  2175. if (r)
  2176. goto out;
  2177. dm_queue_flush(md);
  2178. /*
  2179. * Flushing deferred I/Os must be done after targets are resumed
  2180. * so that mapping of targets can work correctly.
  2181. * Request-based dm is queueing the deferred I/Os in its request_queue.
  2182. */
  2183. if (dm_request_based(md))
  2184. start_queue(md->queue);
  2185. unlock_fs(md);
  2186. clear_bit(DMF_SUSPENDED, &md->flags);
  2187. r = 0;
  2188. out:
  2189. mutex_unlock(&md->suspend_lock);
  2190. return r;
  2191. }
  2192. /*
  2193. * Internal suspend/resume works like userspace-driven suspend. It waits
  2194. * until all bios finish and prevents issuing new bios to the target drivers.
  2195. * It may be used only from the kernel.
  2196. *
  2197. * Internal suspend holds md->suspend_lock, which prevents interaction with
  2198. * userspace-driven suspend.
  2199. */
  2200. void dm_internal_suspend(struct mapped_device *md)
  2201. {
  2202. mutex_lock(&md->suspend_lock);
  2203. if (dm_suspended_md(md))
  2204. return;
  2205. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2206. synchronize_srcu(&md->io_barrier);
  2207. flush_workqueue(md->wq);
  2208. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  2209. }
  2210. void dm_internal_resume(struct mapped_device *md)
  2211. {
  2212. if (dm_suspended_md(md))
  2213. goto done;
  2214. dm_queue_flush(md);
  2215. done:
  2216. mutex_unlock(&md->suspend_lock);
  2217. }
  2218. /*-----------------------------------------------------------------
  2219. * Event notification.
  2220. *---------------------------------------------------------------*/
  2221. int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2222. unsigned cookie)
  2223. {
  2224. char udev_cookie[DM_COOKIE_LENGTH];
  2225. char *envp[] = { udev_cookie, NULL };
  2226. if (!cookie)
  2227. return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2228. else {
  2229. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2230. DM_COOKIE_ENV_VAR_NAME, cookie);
  2231. return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
  2232. action, envp);
  2233. }
  2234. }
  2235. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2236. {
  2237. return atomic_add_return(1, &md->uevent_seq);
  2238. }
  2239. uint32_t dm_get_event_nr(struct mapped_device *md)
  2240. {
  2241. return atomic_read(&md->event_nr);
  2242. }
  2243. int dm_wait_event(struct mapped_device *md, int event_nr)
  2244. {
  2245. return wait_event_interruptible(md->eventq,
  2246. (event_nr != atomic_read(&md->event_nr)));
  2247. }
  2248. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2249. {
  2250. unsigned long flags;
  2251. spin_lock_irqsave(&md->uevent_lock, flags);
  2252. list_add(elist, &md->uevent_list);
  2253. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2254. }
  2255. /*
  2256. * The gendisk is only valid as long as you have a reference
  2257. * count on 'md'.
  2258. */
  2259. struct gendisk *dm_disk(struct mapped_device *md)
  2260. {
  2261. return md->disk;
  2262. }
  2263. struct kobject *dm_kobject(struct mapped_device *md)
  2264. {
  2265. return &md->kobj_holder.kobj;
  2266. }
  2267. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2268. {
  2269. struct mapped_device *md;
  2270. md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
  2271. if (test_bit(DMF_FREEING, &md->flags) ||
  2272. dm_deleting_md(md))
  2273. return NULL;
  2274. dm_get(md);
  2275. return md;
  2276. }
  2277. int dm_suspended_md(struct mapped_device *md)
  2278. {
  2279. return test_bit(DMF_SUSPENDED, &md->flags);
  2280. }
  2281. int dm_test_deferred_remove_flag(struct mapped_device *md)
  2282. {
  2283. return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
  2284. }
  2285. int dm_suspended(struct dm_target *ti)
  2286. {
  2287. return dm_suspended_md(dm_table_get_md(ti->table));
  2288. }
  2289. EXPORT_SYMBOL_GPL(dm_suspended);
  2290. int dm_noflush_suspending(struct dm_target *ti)
  2291. {
  2292. return __noflush_suspending(dm_table_get_md(ti->table));
  2293. }
  2294. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2295. struct dm_md_mempools *dm_alloc_md_mempools(unsigned type, unsigned integrity, unsigned per_bio_data_size)
  2296. {
  2297. struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
  2298. struct kmem_cache *cachep;
  2299. unsigned int pool_size;
  2300. unsigned int front_pad;
  2301. if (!pools)
  2302. return NULL;
  2303. if (type == DM_TYPE_BIO_BASED) {
  2304. cachep = _io_cache;
  2305. pool_size = dm_get_reserved_bio_based_ios();
  2306. front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
  2307. } else if (type == DM_TYPE_REQUEST_BASED) {
  2308. cachep = _rq_tio_cache;
  2309. pool_size = dm_get_reserved_rq_based_ios();
  2310. front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
  2311. /* per_bio_data_size is not used. See __bind_mempools(). */
  2312. WARN_ON(per_bio_data_size != 0);
  2313. } else
  2314. goto out;
  2315. pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
  2316. if (!pools->io_pool)
  2317. goto out;
  2318. pools->bs = bioset_create(pool_size, front_pad);
  2319. if (!pools->bs)
  2320. goto out;
  2321. if (integrity && bioset_integrity_create(pools->bs, pool_size))
  2322. goto out;
  2323. return pools;
  2324. out:
  2325. dm_free_md_mempools(pools);
  2326. return NULL;
  2327. }
  2328. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2329. {
  2330. if (!pools)
  2331. return;
  2332. if (pools->io_pool)
  2333. mempool_destroy(pools->io_pool);
  2334. if (pools->bs)
  2335. bioset_free(pools->bs);
  2336. kfree(pools);
  2337. }
  2338. static const struct block_device_operations dm_blk_dops = {
  2339. .open = dm_blk_open,
  2340. .release = dm_blk_close,
  2341. .ioctl = dm_blk_ioctl,
  2342. .getgeo = dm_blk_getgeo,
  2343. .owner = THIS_MODULE
  2344. };
  2345. EXPORT_SYMBOL(dm_get_mapinfo);
  2346. /*
  2347. * module hooks
  2348. */
  2349. module_init(dm_init);
  2350. module_exit(dm_exit);
  2351. module_param(major, uint, 0);
  2352. MODULE_PARM_DESC(major, "The major number of the device mapper");
  2353. module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
  2354. MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
  2355. module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
  2356. MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
  2357. MODULE_DESCRIPTION(DM_NAME " driver");
  2358. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2359. MODULE_LICENSE("GPL");