dm.c 59 KB

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