dm.c 62 KB

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