dm.c 59 KB

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