dm.c 60 KB

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