dm.c 60 KB

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