dm.c 59 KB

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