dm.c 62 KB

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