dm.c 63 KB

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