dm.c 61 KB

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