dm.c 61 KB

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