dm.c 61 KB

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