dm.c 60 KB

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