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