dm.c 59 KB

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