dm.c 64 KB

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