dm.c 62 KB

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