dm.c 60 KB

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