dm.c 63 KB

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