dm.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include "dm-uevent.h"
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/blkpg.h>
  14. #include <linux/bio.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/mempool.h>
  17. #include <linux/slab.h>
  18. #include <linux/idr.h>
  19. #include <linux/hdreg.h>
  20. #include <trace/events/block.h>
  21. #define DM_MSG_PREFIX "core"
  22. /*
  23. * Cookies are numeric values sent with CHANGE and REMOVE
  24. * uevents while resuming, removing or renaming the device.
  25. */
  26. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  27. #define DM_COOKIE_LENGTH 24
  28. static const char *_name = DM_NAME;
  29. static unsigned int major = 0;
  30. static unsigned int _major = 0;
  31. static DEFINE_SPINLOCK(_minor_lock);
  32. /*
  33. * For bio-based dm.
  34. * One of these is allocated per bio.
  35. */
  36. struct dm_io {
  37. struct mapped_device *md;
  38. int error;
  39. atomic_t io_count;
  40. struct bio *bio;
  41. unsigned long start_time;
  42. };
  43. /*
  44. * For bio-based dm.
  45. * One of these is allocated per target within a bio. Hopefully
  46. * this will be simplified out one day.
  47. */
  48. struct dm_target_io {
  49. struct dm_io *io;
  50. struct dm_target *ti;
  51. union map_info info;
  52. };
  53. /*
  54. * For request-based dm.
  55. * One of these is allocated per request.
  56. */
  57. struct dm_rq_target_io {
  58. struct mapped_device *md;
  59. struct dm_target *ti;
  60. struct request *orig, clone;
  61. int error;
  62. union map_info info;
  63. };
  64. /*
  65. * For request-based dm.
  66. * One of these is allocated per bio.
  67. */
  68. struct dm_rq_clone_bio_info {
  69. struct bio *orig;
  70. struct request *rq;
  71. };
  72. union map_info *dm_get_mapinfo(struct bio *bio)
  73. {
  74. if (bio && bio->bi_private)
  75. return &((struct dm_target_io *)bio->bi_private)->info;
  76. return NULL;
  77. }
  78. #define MINOR_ALLOCED ((void *)-1)
  79. /*
  80. * Bits for the md->flags field.
  81. */
  82. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  83. #define DMF_SUSPENDED 1
  84. #define DMF_FROZEN 2
  85. #define DMF_FREEING 3
  86. #define DMF_DELETING 4
  87. #define DMF_NOFLUSH_SUSPENDING 5
  88. #define DMF_QUEUE_IO_TO_THREAD 6
  89. /*
  90. * Work processed by per-device workqueue.
  91. */
  92. struct mapped_device {
  93. struct rw_semaphore io_lock;
  94. struct mutex suspend_lock;
  95. rwlock_t map_lock;
  96. atomic_t holders;
  97. atomic_t open_count;
  98. unsigned long flags;
  99. struct request_queue *queue;
  100. struct gendisk *disk;
  101. char name[16];
  102. void *interface_ptr;
  103. /*
  104. * A list of ios that arrived while we were suspended.
  105. */
  106. atomic_t pending;
  107. wait_queue_head_t wait;
  108. struct work_struct work;
  109. struct bio_list deferred;
  110. spinlock_t deferred_lock;
  111. /*
  112. * An error from the barrier request currently being processed.
  113. */
  114. int barrier_error;
  115. /*
  116. * Processing queue (flush/barriers)
  117. */
  118. struct workqueue_struct *wq;
  119. /*
  120. * The current mapping.
  121. */
  122. struct dm_table *map;
  123. /*
  124. * io objects are allocated from here.
  125. */
  126. mempool_t *io_pool;
  127. mempool_t *tio_pool;
  128. struct bio_set *bs;
  129. /*
  130. * Event handling.
  131. */
  132. atomic_t event_nr;
  133. wait_queue_head_t eventq;
  134. atomic_t uevent_seq;
  135. struct list_head uevent_list;
  136. spinlock_t uevent_lock; /* Protect access to uevent_list */
  137. /*
  138. * freeze/thaw support require holding onto a super block
  139. */
  140. struct super_block *frozen_sb;
  141. struct block_device *bdev;
  142. /* forced geometry settings */
  143. struct hd_geometry geometry;
  144. /* sysfs handle */
  145. struct kobject kobj;
  146. /* zero-length barrier that will be cloned and submitted to targets */
  147. struct bio barrier_bio;
  148. };
  149. #define MIN_IOS 256
  150. static struct kmem_cache *_io_cache;
  151. static struct kmem_cache *_tio_cache;
  152. static struct kmem_cache *_rq_tio_cache;
  153. static struct kmem_cache *_rq_bio_info_cache;
  154. static int __init local_init(void)
  155. {
  156. int r = -ENOMEM;
  157. /* allocate a slab for the dm_ios */
  158. _io_cache = KMEM_CACHE(dm_io, 0);
  159. if (!_io_cache)
  160. return r;
  161. /* allocate a slab for the target ios */
  162. _tio_cache = KMEM_CACHE(dm_target_io, 0);
  163. if (!_tio_cache)
  164. goto out_free_io_cache;
  165. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  166. if (!_rq_tio_cache)
  167. goto out_free_tio_cache;
  168. _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
  169. if (!_rq_bio_info_cache)
  170. goto out_free_rq_tio_cache;
  171. r = dm_uevent_init();
  172. if (r)
  173. goto out_free_rq_bio_info_cache;
  174. _major = major;
  175. r = register_blkdev(_major, _name);
  176. if (r < 0)
  177. goto out_uevent_exit;
  178. if (!_major)
  179. _major = r;
  180. return 0;
  181. out_uevent_exit:
  182. dm_uevent_exit();
  183. out_free_rq_bio_info_cache:
  184. kmem_cache_destroy(_rq_bio_info_cache);
  185. out_free_rq_tio_cache:
  186. kmem_cache_destroy(_rq_tio_cache);
  187. out_free_tio_cache:
  188. kmem_cache_destroy(_tio_cache);
  189. out_free_io_cache:
  190. kmem_cache_destroy(_io_cache);
  191. return r;
  192. }
  193. static void local_exit(void)
  194. {
  195. kmem_cache_destroy(_rq_bio_info_cache);
  196. kmem_cache_destroy(_rq_tio_cache);
  197. kmem_cache_destroy(_tio_cache);
  198. kmem_cache_destroy(_io_cache);
  199. unregister_blkdev(_major, _name);
  200. dm_uevent_exit();
  201. _major = 0;
  202. DMINFO("cleaned up");
  203. }
  204. static int (*_inits[])(void) __initdata = {
  205. local_init,
  206. dm_target_init,
  207. dm_linear_init,
  208. dm_stripe_init,
  209. dm_kcopyd_init,
  210. dm_interface_init,
  211. };
  212. static void (*_exits[])(void) = {
  213. local_exit,
  214. dm_target_exit,
  215. dm_linear_exit,
  216. dm_stripe_exit,
  217. dm_kcopyd_exit,
  218. dm_interface_exit,
  219. };
  220. static int __init dm_init(void)
  221. {
  222. const int count = ARRAY_SIZE(_inits);
  223. int r, i;
  224. for (i = 0; i < count; i++) {
  225. r = _inits[i]();
  226. if (r)
  227. goto bad;
  228. }
  229. return 0;
  230. bad:
  231. while (i--)
  232. _exits[i]();
  233. return r;
  234. }
  235. static void __exit dm_exit(void)
  236. {
  237. int i = ARRAY_SIZE(_exits);
  238. while (i--)
  239. _exits[i]();
  240. }
  241. /*
  242. * Block device functions
  243. */
  244. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  245. {
  246. struct mapped_device *md;
  247. spin_lock(&_minor_lock);
  248. md = bdev->bd_disk->private_data;
  249. if (!md)
  250. goto out;
  251. if (test_bit(DMF_FREEING, &md->flags) ||
  252. test_bit(DMF_DELETING, &md->flags)) {
  253. md = NULL;
  254. goto out;
  255. }
  256. dm_get(md);
  257. atomic_inc(&md->open_count);
  258. out:
  259. spin_unlock(&_minor_lock);
  260. return md ? 0 : -ENXIO;
  261. }
  262. static int dm_blk_close(struct gendisk *disk, fmode_t mode)
  263. {
  264. struct mapped_device *md = disk->private_data;
  265. atomic_dec(&md->open_count);
  266. dm_put(md);
  267. return 0;
  268. }
  269. int dm_open_count(struct mapped_device *md)
  270. {
  271. return atomic_read(&md->open_count);
  272. }
  273. /*
  274. * Guarantees nothing is using the device before it's deleted.
  275. */
  276. int dm_lock_for_deletion(struct mapped_device *md)
  277. {
  278. int r = 0;
  279. spin_lock(&_minor_lock);
  280. if (dm_open_count(md))
  281. r = -EBUSY;
  282. else
  283. set_bit(DMF_DELETING, &md->flags);
  284. spin_unlock(&_minor_lock);
  285. return r;
  286. }
  287. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  288. {
  289. struct mapped_device *md = bdev->bd_disk->private_data;
  290. return dm_get_geometry(md, geo);
  291. }
  292. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  293. unsigned int cmd, unsigned long arg)
  294. {
  295. struct mapped_device *md = bdev->bd_disk->private_data;
  296. struct dm_table *map = dm_get_table(md);
  297. struct dm_target *tgt;
  298. int r = -ENOTTY;
  299. if (!map || !dm_table_get_size(map))
  300. goto out;
  301. /* We only support devices that have a single target */
  302. if (dm_table_get_num_targets(map) != 1)
  303. goto out;
  304. tgt = dm_table_get_target(map, 0);
  305. if (dm_suspended(md)) {
  306. r = -EAGAIN;
  307. goto out;
  308. }
  309. if (tgt->type->ioctl)
  310. r = tgt->type->ioctl(tgt, cmd, arg);
  311. out:
  312. dm_table_put(map);
  313. return r;
  314. }
  315. static struct dm_io *alloc_io(struct mapped_device *md)
  316. {
  317. return mempool_alloc(md->io_pool, GFP_NOIO);
  318. }
  319. static void free_io(struct mapped_device *md, struct dm_io *io)
  320. {
  321. mempool_free(io, md->io_pool);
  322. }
  323. static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
  324. {
  325. mempool_free(tio, md->tio_pool);
  326. }
  327. static void start_io_acct(struct dm_io *io)
  328. {
  329. struct mapped_device *md = io->md;
  330. int cpu;
  331. io->start_time = jiffies;
  332. cpu = part_stat_lock();
  333. part_round_stats(cpu, &dm_disk(md)->part0);
  334. part_stat_unlock();
  335. dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
  336. }
  337. static void end_io_acct(struct dm_io *io)
  338. {
  339. struct mapped_device *md = io->md;
  340. struct bio *bio = io->bio;
  341. unsigned long duration = jiffies - io->start_time;
  342. int pending, cpu;
  343. int rw = bio_data_dir(bio);
  344. cpu = part_stat_lock();
  345. part_round_stats(cpu, &dm_disk(md)->part0);
  346. part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
  347. part_stat_unlock();
  348. /*
  349. * After this is decremented the bio must not be touched if it is
  350. * a barrier.
  351. */
  352. dm_disk(md)->part0.in_flight = pending =
  353. atomic_dec_return(&md->pending);
  354. /* nudge anyone waiting on suspend queue */
  355. if (!pending)
  356. wake_up(&md->wait);
  357. }
  358. /*
  359. * Add the bio to the list of deferred io.
  360. */
  361. static void queue_io(struct mapped_device *md, struct bio *bio)
  362. {
  363. down_write(&md->io_lock);
  364. spin_lock_irq(&md->deferred_lock);
  365. bio_list_add(&md->deferred, bio);
  366. spin_unlock_irq(&md->deferred_lock);
  367. if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
  368. queue_work(md->wq, &md->work);
  369. up_write(&md->io_lock);
  370. }
  371. /*
  372. * Everyone (including functions in this file), should use this
  373. * function to access the md->map field, and make sure they call
  374. * dm_table_put() when finished.
  375. */
  376. struct dm_table *dm_get_table(struct mapped_device *md)
  377. {
  378. struct dm_table *t;
  379. read_lock(&md->map_lock);
  380. t = md->map;
  381. if (t)
  382. dm_table_get(t);
  383. read_unlock(&md->map_lock);
  384. return t;
  385. }
  386. /*
  387. * Get the geometry associated with a dm device
  388. */
  389. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  390. {
  391. *geo = md->geometry;
  392. return 0;
  393. }
  394. /*
  395. * Set the geometry of a device.
  396. */
  397. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  398. {
  399. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  400. if (geo->start > sz) {
  401. DMWARN("Start sector is beyond the geometry limits.");
  402. return -EINVAL;
  403. }
  404. md->geometry = *geo;
  405. return 0;
  406. }
  407. /*-----------------------------------------------------------------
  408. * CRUD START:
  409. * A more elegant soln is in the works that uses the queue
  410. * merge fn, unfortunately there are a couple of changes to
  411. * the block layer that I want to make for this. So in the
  412. * interests of getting something for people to use I give
  413. * you this clearly demarcated crap.
  414. *---------------------------------------------------------------*/
  415. static int __noflush_suspending(struct mapped_device *md)
  416. {
  417. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  418. }
  419. /*
  420. * Decrements the number of outstanding ios that a bio has been
  421. * cloned into, completing the original io if necc.
  422. */
  423. static void dec_pending(struct dm_io *io, int error)
  424. {
  425. unsigned long flags;
  426. int io_error;
  427. struct bio *bio;
  428. struct mapped_device *md = io->md;
  429. /* Push-back supersedes any I/O errors */
  430. if (error && !(io->error > 0 && __noflush_suspending(md)))
  431. io->error = error;
  432. if (atomic_dec_and_test(&io->io_count)) {
  433. if (io->error == DM_ENDIO_REQUEUE) {
  434. /*
  435. * Target requested pushing back the I/O.
  436. */
  437. spin_lock_irqsave(&md->deferred_lock, flags);
  438. if (__noflush_suspending(md)) {
  439. if (!bio_barrier(io->bio))
  440. bio_list_add_head(&md->deferred,
  441. io->bio);
  442. } else
  443. /* noflush suspend was interrupted. */
  444. io->error = -EIO;
  445. spin_unlock_irqrestore(&md->deferred_lock, flags);
  446. }
  447. io_error = io->error;
  448. bio = io->bio;
  449. if (bio_barrier(bio)) {
  450. /*
  451. * There can be just one barrier request so we use
  452. * a per-device variable for error reporting.
  453. * Note that you can't touch the bio after end_io_acct
  454. */
  455. if (!md->barrier_error && io_error != -EOPNOTSUPP)
  456. md->barrier_error = io_error;
  457. end_io_acct(io);
  458. } else {
  459. end_io_acct(io);
  460. if (io_error != DM_ENDIO_REQUEUE) {
  461. trace_block_bio_complete(md->queue, bio);
  462. bio_endio(bio, io_error);
  463. }
  464. }
  465. free_io(md, io);
  466. }
  467. }
  468. static void clone_endio(struct bio *bio, int error)
  469. {
  470. int r = 0;
  471. struct dm_target_io *tio = bio->bi_private;
  472. struct dm_io *io = tio->io;
  473. struct mapped_device *md = tio->io->md;
  474. dm_endio_fn endio = tio->ti->type->end_io;
  475. if (!bio_flagged(bio, BIO_UPTODATE) && !error)
  476. error = -EIO;
  477. if (endio) {
  478. r = endio(tio->ti, bio, error, &tio->info);
  479. if (r < 0 || r == DM_ENDIO_REQUEUE)
  480. /*
  481. * error and requeue request are handled
  482. * in dec_pending().
  483. */
  484. error = r;
  485. else if (r == DM_ENDIO_INCOMPLETE)
  486. /* The target will handle the io */
  487. return;
  488. else if (r) {
  489. DMWARN("unimplemented target endio return value: %d", r);
  490. BUG();
  491. }
  492. }
  493. /*
  494. * Store md for cleanup instead of tio which is about to get freed.
  495. */
  496. bio->bi_private = md->bs;
  497. free_tio(md, tio);
  498. bio_put(bio);
  499. dec_pending(io, error);
  500. }
  501. static sector_t max_io_len(struct mapped_device *md,
  502. sector_t sector, struct dm_target *ti)
  503. {
  504. sector_t offset = sector - ti->begin;
  505. sector_t len = ti->len - offset;
  506. /*
  507. * Does the target need to split even further ?
  508. */
  509. if (ti->split_io) {
  510. sector_t boundary;
  511. boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
  512. - offset;
  513. if (len > boundary)
  514. len = boundary;
  515. }
  516. return len;
  517. }
  518. static void __map_bio(struct dm_target *ti, struct bio *clone,
  519. struct dm_target_io *tio)
  520. {
  521. int r;
  522. sector_t sector;
  523. struct mapped_device *md;
  524. clone->bi_end_io = clone_endio;
  525. clone->bi_private = tio;
  526. /*
  527. * Map the clone. If r == 0 we don't need to do
  528. * anything, the target has assumed ownership of
  529. * this io.
  530. */
  531. atomic_inc(&tio->io->io_count);
  532. sector = clone->bi_sector;
  533. r = ti->type->map(ti, clone, &tio->info);
  534. if (r == DM_MAPIO_REMAPPED) {
  535. /* the bio has been remapped so dispatch it */
  536. trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
  537. tio->io->bio->bi_bdev->bd_dev, sector);
  538. generic_make_request(clone);
  539. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  540. /* error the io and bail out, or requeue it if needed */
  541. md = tio->io->md;
  542. dec_pending(tio->io, r);
  543. /*
  544. * Store bio_set for cleanup.
  545. */
  546. clone->bi_private = md->bs;
  547. bio_put(clone);
  548. free_tio(md, tio);
  549. } else if (r) {
  550. DMWARN("unimplemented target map return value: %d", r);
  551. BUG();
  552. }
  553. }
  554. struct clone_info {
  555. struct mapped_device *md;
  556. struct dm_table *map;
  557. struct bio *bio;
  558. struct dm_io *io;
  559. sector_t sector;
  560. sector_t sector_count;
  561. unsigned short idx;
  562. };
  563. static void dm_bio_destructor(struct bio *bio)
  564. {
  565. struct bio_set *bs = bio->bi_private;
  566. bio_free(bio, bs);
  567. }
  568. /*
  569. * Creates a little bio that is just does part of a bvec.
  570. */
  571. static struct bio *split_bvec(struct bio *bio, sector_t sector,
  572. unsigned short idx, unsigned int offset,
  573. unsigned int len, struct bio_set *bs)
  574. {
  575. struct bio *clone;
  576. struct bio_vec *bv = bio->bi_io_vec + idx;
  577. clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
  578. clone->bi_destructor = dm_bio_destructor;
  579. *clone->bi_io_vec = *bv;
  580. clone->bi_sector = sector;
  581. clone->bi_bdev = bio->bi_bdev;
  582. clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
  583. clone->bi_vcnt = 1;
  584. clone->bi_size = to_bytes(len);
  585. clone->bi_io_vec->bv_offset = offset;
  586. clone->bi_io_vec->bv_len = clone->bi_size;
  587. clone->bi_flags |= 1 << BIO_CLONED;
  588. if (bio_integrity(bio)) {
  589. bio_integrity_clone(clone, bio, GFP_NOIO);
  590. bio_integrity_trim(clone,
  591. bio_sector_offset(bio, idx, offset), len);
  592. }
  593. return clone;
  594. }
  595. /*
  596. * Creates a bio that consists of range of complete bvecs.
  597. */
  598. static struct bio *clone_bio(struct bio *bio, sector_t sector,
  599. unsigned short idx, unsigned short bv_count,
  600. unsigned int len, struct bio_set *bs)
  601. {
  602. struct bio *clone;
  603. clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
  604. __bio_clone(clone, bio);
  605. clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
  606. clone->bi_destructor = dm_bio_destructor;
  607. clone->bi_sector = sector;
  608. clone->bi_idx = idx;
  609. clone->bi_vcnt = idx + bv_count;
  610. clone->bi_size = to_bytes(len);
  611. clone->bi_flags &= ~(1 << BIO_SEG_VALID);
  612. if (bio_integrity(bio)) {
  613. bio_integrity_clone(clone, bio, GFP_NOIO);
  614. if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
  615. bio_integrity_trim(clone,
  616. bio_sector_offset(bio, idx, 0), len);
  617. }
  618. return clone;
  619. }
  620. static struct dm_target_io *alloc_tio(struct clone_info *ci,
  621. struct dm_target *ti)
  622. {
  623. struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
  624. tio->io = ci->io;
  625. tio->ti = ti;
  626. memset(&tio->info, 0, sizeof(tio->info));
  627. return tio;
  628. }
  629. static void __flush_target(struct clone_info *ci, struct dm_target *ti,
  630. unsigned flush_nr)
  631. {
  632. struct dm_target_io *tio = alloc_tio(ci, ti);
  633. struct bio *clone;
  634. tio->info.flush_request = flush_nr;
  635. clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
  636. __bio_clone(clone, ci->bio);
  637. clone->bi_destructor = dm_bio_destructor;
  638. __map_bio(ti, clone, tio);
  639. }
  640. static int __clone_and_map_empty_barrier(struct clone_info *ci)
  641. {
  642. unsigned target_nr = 0, flush_nr;
  643. struct dm_target *ti;
  644. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  645. for (flush_nr = 0; flush_nr < ti->num_flush_requests;
  646. flush_nr++)
  647. __flush_target(ci, ti, flush_nr);
  648. ci->sector_count = 0;
  649. return 0;
  650. }
  651. static int __clone_and_map(struct clone_info *ci)
  652. {
  653. struct bio *clone, *bio = ci->bio;
  654. struct dm_target *ti;
  655. sector_t len = 0, max;
  656. struct dm_target_io *tio;
  657. if (unlikely(bio_empty_barrier(bio)))
  658. return __clone_and_map_empty_barrier(ci);
  659. ti = dm_table_find_target(ci->map, ci->sector);
  660. if (!dm_target_is_valid(ti))
  661. return -EIO;
  662. max = max_io_len(ci->md, ci->sector, ti);
  663. /*
  664. * Allocate a target io object.
  665. */
  666. tio = alloc_tio(ci, ti);
  667. if (ci->sector_count <= max) {
  668. /*
  669. * Optimise for the simple case where we can do all of
  670. * the remaining io with a single clone.
  671. */
  672. clone = clone_bio(bio, ci->sector, ci->idx,
  673. bio->bi_vcnt - ci->idx, ci->sector_count,
  674. ci->md->bs);
  675. __map_bio(ti, clone, tio);
  676. ci->sector_count = 0;
  677. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  678. /*
  679. * There are some bvecs that don't span targets.
  680. * Do as many of these as possible.
  681. */
  682. int i;
  683. sector_t remaining = max;
  684. sector_t bv_len;
  685. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  686. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  687. if (bv_len > remaining)
  688. break;
  689. remaining -= bv_len;
  690. len += bv_len;
  691. }
  692. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
  693. ci->md->bs);
  694. __map_bio(ti, clone, tio);
  695. ci->sector += len;
  696. ci->sector_count -= len;
  697. ci->idx = i;
  698. } else {
  699. /*
  700. * Handle a bvec that must be split between two or more targets.
  701. */
  702. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  703. sector_t remaining = to_sector(bv->bv_len);
  704. unsigned int offset = 0;
  705. do {
  706. if (offset) {
  707. ti = dm_table_find_target(ci->map, ci->sector);
  708. if (!dm_target_is_valid(ti))
  709. return -EIO;
  710. max = max_io_len(ci->md, ci->sector, ti);
  711. tio = alloc_tio(ci, ti);
  712. }
  713. len = min(remaining, max);
  714. clone = split_bvec(bio, ci->sector, ci->idx,
  715. bv->bv_offset + offset, len,
  716. ci->md->bs);
  717. __map_bio(ti, clone, tio);
  718. ci->sector += len;
  719. ci->sector_count -= len;
  720. offset += to_bytes(len);
  721. } while (remaining -= len);
  722. ci->idx++;
  723. }
  724. return 0;
  725. }
  726. /*
  727. * Split the bio into several clones and submit it to targets.
  728. */
  729. static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
  730. {
  731. struct clone_info ci;
  732. int error = 0;
  733. ci.map = dm_get_table(md);
  734. if (unlikely(!ci.map)) {
  735. if (!bio_barrier(bio))
  736. bio_io_error(bio);
  737. else
  738. if (!md->barrier_error)
  739. md->barrier_error = -EIO;
  740. return;
  741. }
  742. ci.md = md;
  743. ci.bio = bio;
  744. ci.io = alloc_io(md);
  745. ci.io->error = 0;
  746. atomic_set(&ci.io->io_count, 1);
  747. ci.io->bio = bio;
  748. ci.io->md = md;
  749. ci.sector = bio->bi_sector;
  750. ci.sector_count = bio_sectors(bio);
  751. if (unlikely(bio_empty_barrier(bio)))
  752. ci.sector_count = 1;
  753. ci.idx = bio->bi_idx;
  754. start_io_acct(ci.io);
  755. while (ci.sector_count && !error)
  756. error = __clone_and_map(&ci);
  757. /* drop the extra reference count */
  758. dec_pending(ci.io, error);
  759. dm_table_put(ci.map);
  760. }
  761. /*-----------------------------------------------------------------
  762. * CRUD END
  763. *---------------------------------------------------------------*/
  764. static int dm_merge_bvec(struct request_queue *q,
  765. struct bvec_merge_data *bvm,
  766. struct bio_vec *biovec)
  767. {
  768. struct mapped_device *md = q->queuedata;
  769. struct dm_table *map = dm_get_table(md);
  770. struct dm_target *ti;
  771. sector_t max_sectors;
  772. int max_size = 0;
  773. if (unlikely(!map))
  774. goto out;
  775. ti = dm_table_find_target(map, bvm->bi_sector);
  776. if (!dm_target_is_valid(ti))
  777. goto out_table;
  778. /*
  779. * Find maximum amount of I/O that won't need splitting
  780. */
  781. max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
  782. (sector_t) BIO_MAX_SECTORS);
  783. max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
  784. if (max_size < 0)
  785. max_size = 0;
  786. /*
  787. * merge_bvec_fn() returns number of bytes
  788. * it can accept at this offset
  789. * max is precomputed maximal io size
  790. */
  791. if (max_size && ti->type->merge)
  792. max_size = ti->type->merge(ti, bvm, biovec, max_size);
  793. /*
  794. * If the target doesn't support merge method and some of the devices
  795. * provided their merge_bvec method (we know this by looking at
  796. * queue_max_hw_sectors), then we can't allow bios with multiple vector
  797. * entries. So always set max_size to 0, and the code below allows
  798. * just one page.
  799. */
  800. else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
  801. max_size = 0;
  802. out_table:
  803. dm_table_put(map);
  804. out:
  805. /*
  806. * Always allow an entire first page
  807. */
  808. if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
  809. max_size = biovec->bv_len;
  810. return max_size;
  811. }
  812. /*
  813. * The request function that just remaps the bio built up by
  814. * dm_merge_bvec.
  815. */
  816. static int dm_request(struct request_queue *q, struct bio *bio)
  817. {
  818. int rw = bio_data_dir(bio);
  819. struct mapped_device *md = q->queuedata;
  820. int cpu;
  821. down_read(&md->io_lock);
  822. cpu = part_stat_lock();
  823. part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
  824. part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
  825. part_stat_unlock();
  826. /*
  827. * If we're suspended or the thread is processing barriers
  828. * we have to queue this io for later.
  829. */
  830. if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
  831. unlikely(bio_barrier(bio))) {
  832. up_read(&md->io_lock);
  833. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
  834. bio_rw(bio) == READA) {
  835. bio_io_error(bio);
  836. return 0;
  837. }
  838. queue_io(md, bio);
  839. return 0;
  840. }
  841. __split_and_process_bio(md, bio);
  842. up_read(&md->io_lock);
  843. return 0;
  844. }
  845. static void dm_unplug_all(struct request_queue *q)
  846. {
  847. struct mapped_device *md = q->queuedata;
  848. struct dm_table *map = dm_get_table(md);
  849. if (map) {
  850. dm_table_unplug_all(map);
  851. dm_table_put(map);
  852. }
  853. }
  854. static int dm_any_congested(void *congested_data, int bdi_bits)
  855. {
  856. int r = bdi_bits;
  857. struct mapped_device *md = congested_data;
  858. struct dm_table *map;
  859. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  860. map = dm_get_table(md);
  861. if (map) {
  862. r = dm_table_any_congested(map, bdi_bits);
  863. dm_table_put(map);
  864. }
  865. }
  866. return r;
  867. }
  868. /*-----------------------------------------------------------------
  869. * An IDR is used to keep track of allocated minor numbers.
  870. *---------------------------------------------------------------*/
  871. static DEFINE_IDR(_minor_idr);
  872. static void free_minor(int minor)
  873. {
  874. spin_lock(&_minor_lock);
  875. idr_remove(&_minor_idr, minor);
  876. spin_unlock(&_minor_lock);
  877. }
  878. /*
  879. * See if the device with a specific minor # is free.
  880. */
  881. static int specific_minor(int minor)
  882. {
  883. int r, m;
  884. if (minor >= (1 << MINORBITS))
  885. return -EINVAL;
  886. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  887. if (!r)
  888. return -ENOMEM;
  889. spin_lock(&_minor_lock);
  890. if (idr_find(&_minor_idr, minor)) {
  891. r = -EBUSY;
  892. goto out;
  893. }
  894. r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
  895. if (r)
  896. goto out;
  897. if (m != minor) {
  898. idr_remove(&_minor_idr, m);
  899. r = -EBUSY;
  900. goto out;
  901. }
  902. out:
  903. spin_unlock(&_minor_lock);
  904. return r;
  905. }
  906. static int next_free_minor(int *minor)
  907. {
  908. int r, m;
  909. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  910. if (!r)
  911. return -ENOMEM;
  912. spin_lock(&_minor_lock);
  913. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  914. if (r)
  915. goto out;
  916. if (m >= (1 << MINORBITS)) {
  917. idr_remove(&_minor_idr, m);
  918. r = -ENOSPC;
  919. goto out;
  920. }
  921. *minor = m;
  922. out:
  923. spin_unlock(&_minor_lock);
  924. return r;
  925. }
  926. static struct block_device_operations dm_blk_dops;
  927. static void dm_wq_work(struct work_struct *work);
  928. /*
  929. * Allocate and initialise a blank device with a given minor.
  930. */
  931. static struct mapped_device *alloc_dev(int minor)
  932. {
  933. int r;
  934. struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
  935. void *old_md;
  936. if (!md) {
  937. DMWARN("unable to allocate device, out of memory.");
  938. return NULL;
  939. }
  940. if (!try_module_get(THIS_MODULE))
  941. goto bad_module_get;
  942. /* get a minor number for the dev */
  943. if (minor == DM_ANY_MINOR)
  944. r = next_free_minor(&minor);
  945. else
  946. r = specific_minor(minor);
  947. if (r < 0)
  948. goto bad_minor;
  949. init_rwsem(&md->io_lock);
  950. mutex_init(&md->suspend_lock);
  951. spin_lock_init(&md->deferred_lock);
  952. rwlock_init(&md->map_lock);
  953. atomic_set(&md->holders, 1);
  954. atomic_set(&md->open_count, 0);
  955. atomic_set(&md->event_nr, 0);
  956. atomic_set(&md->uevent_seq, 0);
  957. INIT_LIST_HEAD(&md->uevent_list);
  958. spin_lock_init(&md->uevent_lock);
  959. md->queue = blk_alloc_queue(GFP_KERNEL);
  960. if (!md->queue)
  961. goto bad_queue;
  962. md->queue->queuedata = md;
  963. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  964. md->queue->backing_dev_info.congested_data = md;
  965. blk_queue_make_request(md->queue, dm_request);
  966. blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
  967. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  968. md->queue->unplug_fn = dm_unplug_all;
  969. blk_queue_merge_bvec(md->queue, dm_merge_bvec);
  970. md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
  971. if (!md->io_pool)
  972. goto bad_io_pool;
  973. md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
  974. if (!md->tio_pool)
  975. goto bad_tio_pool;
  976. md->bs = bioset_create(16, 0);
  977. if (!md->bs)
  978. goto bad_no_bioset;
  979. md->disk = alloc_disk(1);
  980. if (!md->disk)
  981. goto bad_disk;
  982. atomic_set(&md->pending, 0);
  983. init_waitqueue_head(&md->wait);
  984. INIT_WORK(&md->work, dm_wq_work);
  985. init_waitqueue_head(&md->eventq);
  986. md->disk->major = _major;
  987. md->disk->first_minor = minor;
  988. md->disk->fops = &dm_blk_dops;
  989. md->disk->queue = md->queue;
  990. md->disk->private_data = md;
  991. sprintf(md->disk->disk_name, "dm-%d", minor);
  992. add_disk(md->disk);
  993. format_dev_t(md->name, MKDEV(_major, minor));
  994. md->wq = create_singlethread_workqueue("kdmflush");
  995. if (!md->wq)
  996. goto bad_thread;
  997. md->bdev = bdget_disk(md->disk, 0);
  998. if (!md->bdev)
  999. goto bad_bdev;
  1000. /* Populate the mapping, nobody knows we exist yet */
  1001. spin_lock(&_minor_lock);
  1002. old_md = idr_replace(&_minor_idr, md, minor);
  1003. spin_unlock(&_minor_lock);
  1004. BUG_ON(old_md != MINOR_ALLOCED);
  1005. return md;
  1006. bad_bdev:
  1007. destroy_workqueue(md->wq);
  1008. bad_thread:
  1009. put_disk(md->disk);
  1010. bad_disk:
  1011. bioset_free(md->bs);
  1012. bad_no_bioset:
  1013. mempool_destroy(md->tio_pool);
  1014. bad_tio_pool:
  1015. mempool_destroy(md->io_pool);
  1016. bad_io_pool:
  1017. blk_cleanup_queue(md->queue);
  1018. bad_queue:
  1019. free_minor(minor);
  1020. bad_minor:
  1021. module_put(THIS_MODULE);
  1022. bad_module_get:
  1023. kfree(md);
  1024. return NULL;
  1025. }
  1026. static void unlock_fs(struct mapped_device *md);
  1027. static void free_dev(struct mapped_device *md)
  1028. {
  1029. int minor = MINOR(disk_devt(md->disk));
  1030. unlock_fs(md);
  1031. bdput(md->bdev);
  1032. destroy_workqueue(md->wq);
  1033. mempool_destroy(md->tio_pool);
  1034. mempool_destroy(md->io_pool);
  1035. bioset_free(md->bs);
  1036. blk_integrity_unregister(md->disk);
  1037. del_gendisk(md->disk);
  1038. free_minor(minor);
  1039. spin_lock(&_minor_lock);
  1040. md->disk->private_data = NULL;
  1041. spin_unlock(&_minor_lock);
  1042. put_disk(md->disk);
  1043. blk_cleanup_queue(md->queue);
  1044. module_put(THIS_MODULE);
  1045. kfree(md);
  1046. }
  1047. /*
  1048. * Bind a table to the device.
  1049. */
  1050. static void event_callback(void *context)
  1051. {
  1052. unsigned long flags;
  1053. LIST_HEAD(uevents);
  1054. struct mapped_device *md = (struct mapped_device *) context;
  1055. spin_lock_irqsave(&md->uevent_lock, flags);
  1056. list_splice_init(&md->uevent_list, &uevents);
  1057. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1058. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1059. atomic_inc(&md->event_nr);
  1060. wake_up(&md->eventq);
  1061. }
  1062. static void __set_size(struct mapped_device *md, sector_t size)
  1063. {
  1064. set_capacity(md->disk, size);
  1065. mutex_lock(&md->bdev->bd_inode->i_mutex);
  1066. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1067. mutex_unlock(&md->bdev->bd_inode->i_mutex);
  1068. }
  1069. static int __bind(struct mapped_device *md, struct dm_table *t)
  1070. {
  1071. struct request_queue *q = md->queue;
  1072. sector_t size;
  1073. size = dm_table_get_size(t);
  1074. /*
  1075. * Wipe any geometry if the size of the table changed.
  1076. */
  1077. if (size != get_capacity(md->disk))
  1078. memset(&md->geometry, 0, sizeof(md->geometry));
  1079. __set_size(md, size);
  1080. if (!size) {
  1081. dm_table_destroy(t);
  1082. return 0;
  1083. }
  1084. dm_table_event_callback(t, event_callback, md);
  1085. write_lock(&md->map_lock);
  1086. md->map = t;
  1087. dm_table_set_restrictions(t, q);
  1088. write_unlock(&md->map_lock);
  1089. return 0;
  1090. }
  1091. static void __unbind(struct mapped_device *md)
  1092. {
  1093. struct dm_table *map = md->map;
  1094. if (!map)
  1095. return;
  1096. dm_table_event_callback(map, NULL, NULL);
  1097. write_lock(&md->map_lock);
  1098. md->map = NULL;
  1099. write_unlock(&md->map_lock);
  1100. dm_table_destroy(map);
  1101. }
  1102. /*
  1103. * Constructor for a new device.
  1104. */
  1105. int dm_create(int minor, struct mapped_device **result)
  1106. {
  1107. struct mapped_device *md;
  1108. md = alloc_dev(minor);
  1109. if (!md)
  1110. return -ENXIO;
  1111. dm_sysfs_init(md);
  1112. *result = md;
  1113. return 0;
  1114. }
  1115. static struct mapped_device *dm_find_md(dev_t dev)
  1116. {
  1117. struct mapped_device *md;
  1118. unsigned minor = MINOR(dev);
  1119. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1120. return NULL;
  1121. spin_lock(&_minor_lock);
  1122. md = idr_find(&_minor_idr, minor);
  1123. if (md && (md == MINOR_ALLOCED ||
  1124. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1125. test_bit(DMF_FREEING, &md->flags))) {
  1126. md = NULL;
  1127. goto out;
  1128. }
  1129. out:
  1130. spin_unlock(&_minor_lock);
  1131. return md;
  1132. }
  1133. struct mapped_device *dm_get_md(dev_t dev)
  1134. {
  1135. struct mapped_device *md = dm_find_md(dev);
  1136. if (md)
  1137. dm_get(md);
  1138. return md;
  1139. }
  1140. void *dm_get_mdptr(struct mapped_device *md)
  1141. {
  1142. return md->interface_ptr;
  1143. }
  1144. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1145. {
  1146. md->interface_ptr = ptr;
  1147. }
  1148. void dm_get(struct mapped_device *md)
  1149. {
  1150. atomic_inc(&md->holders);
  1151. }
  1152. const char *dm_device_name(struct mapped_device *md)
  1153. {
  1154. return md->name;
  1155. }
  1156. EXPORT_SYMBOL_GPL(dm_device_name);
  1157. void dm_put(struct mapped_device *md)
  1158. {
  1159. struct dm_table *map;
  1160. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1161. if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
  1162. map = dm_get_table(md);
  1163. idr_replace(&_minor_idr, MINOR_ALLOCED,
  1164. MINOR(disk_devt(dm_disk(md))));
  1165. set_bit(DMF_FREEING, &md->flags);
  1166. spin_unlock(&_minor_lock);
  1167. if (!dm_suspended(md)) {
  1168. dm_table_presuspend_targets(map);
  1169. dm_table_postsuspend_targets(map);
  1170. }
  1171. dm_sysfs_exit(md);
  1172. dm_table_put(map);
  1173. __unbind(md);
  1174. free_dev(md);
  1175. }
  1176. }
  1177. EXPORT_SYMBOL_GPL(dm_put);
  1178. static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
  1179. {
  1180. int r = 0;
  1181. DECLARE_WAITQUEUE(wait, current);
  1182. dm_unplug_all(md->queue);
  1183. add_wait_queue(&md->wait, &wait);
  1184. while (1) {
  1185. set_current_state(interruptible);
  1186. smp_mb();
  1187. if (!atomic_read(&md->pending))
  1188. break;
  1189. if (interruptible == TASK_INTERRUPTIBLE &&
  1190. signal_pending(current)) {
  1191. r = -EINTR;
  1192. break;
  1193. }
  1194. io_schedule();
  1195. }
  1196. set_current_state(TASK_RUNNING);
  1197. remove_wait_queue(&md->wait, &wait);
  1198. return r;
  1199. }
  1200. static void dm_flush(struct mapped_device *md)
  1201. {
  1202. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  1203. bio_init(&md->barrier_bio);
  1204. md->barrier_bio.bi_bdev = md->bdev;
  1205. md->barrier_bio.bi_rw = WRITE_BARRIER;
  1206. __split_and_process_bio(md, &md->barrier_bio);
  1207. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  1208. }
  1209. static void process_barrier(struct mapped_device *md, struct bio *bio)
  1210. {
  1211. md->barrier_error = 0;
  1212. dm_flush(md);
  1213. if (!bio_empty_barrier(bio)) {
  1214. __split_and_process_bio(md, bio);
  1215. dm_flush(md);
  1216. }
  1217. if (md->barrier_error != DM_ENDIO_REQUEUE)
  1218. bio_endio(bio, md->barrier_error);
  1219. else {
  1220. spin_lock_irq(&md->deferred_lock);
  1221. bio_list_add_head(&md->deferred, bio);
  1222. spin_unlock_irq(&md->deferred_lock);
  1223. }
  1224. }
  1225. /*
  1226. * Process the deferred bios
  1227. */
  1228. static void dm_wq_work(struct work_struct *work)
  1229. {
  1230. struct mapped_device *md = container_of(work, struct mapped_device,
  1231. work);
  1232. struct bio *c;
  1233. down_write(&md->io_lock);
  1234. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1235. spin_lock_irq(&md->deferred_lock);
  1236. c = bio_list_pop(&md->deferred);
  1237. spin_unlock_irq(&md->deferred_lock);
  1238. if (!c) {
  1239. clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
  1240. break;
  1241. }
  1242. up_write(&md->io_lock);
  1243. if (bio_barrier(c))
  1244. process_barrier(md, c);
  1245. else
  1246. __split_and_process_bio(md, c);
  1247. down_write(&md->io_lock);
  1248. }
  1249. up_write(&md->io_lock);
  1250. }
  1251. static void dm_queue_flush(struct mapped_device *md)
  1252. {
  1253. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1254. smp_mb__after_clear_bit();
  1255. queue_work(md->wq, &md->work);
  1256. }
  1257. /*
  1258. * Swap in a new table (destroying old one).
  1259. */
  1260. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1261. {
  1262. int r = -EINVAL;
  1263. mutex_lock(&md->suspend_lock);
  1264. /* device must be suspended */
  1265. if (!dm_suspended(md))
  1266. goto out;
  1267. __unbind(md);
  1268. r = __bind(md, table);
  1269. out:
  1270. mutex_unlock(&md->suspend_lock);
  1271. return r;
  1272. }
  1273. /*
  1274. * Functions to lock and unlock any filesystem running on the
  1275. * device.
  1276. */
  1277. static int lock_fs(struct mapped_device *md)
  1278. {
  1279. int r;
  1280. WARN_ON(md->frozen_sb);
  1281. md->frozen_sb = freeze_bdev(md->bdev);
  1282. if (IS_ERR(md->frozen_sb)) {
  1283. r = PTR_ERR(md->frozen_sb);
  1284. md->frozen_sb = NULL;
  1285. return r;
  1286. }
  1287. set_bit(DMF_FROZEN, &md->flags);
  1288. return 0;
  1289. }
  1290. static void unlock_fs(struct mapped_device *md)
  1291. {
  1292. if (!test_bit(DMF_FROZEN, &md->flags))
  1293. return;
  1294. thaw_bdev(md->bdev, md->frozen_sb);
  1295. md->frozen_sb = NULL;
  1296. clear_bit(DMF_FROZEN, &md->flags);
  1297. }
  1298. /*
  1299. * We need to be able to change a mapping table under a mounted
  1300. * filesystem. For example we might want to move some data in
  1301. * the background. Before the table can be swapped with
  1302. * dm_bind_table, dm_suspend must be called to flush any in
  1303. * flight bios and ensure that any further io gets deferred.
  1304. */
  1305. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1306. {
  1307. struct dm_table *map = NULL;
  1308. int r = 0;
  1309. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1310. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  1311. mutex_lock(&md->suspend_lock);
  1312. if (dm_suspended(md)) {
  1313. r = -EINVAL;
  1314. goto out_unlock;
  1315. }
  1316. map = dm_get_table(md);
  1317. /*
  1318. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  1319. * This flag is cleared before dm_suspend returns.
  1320. */
  1321. if (noflush)
  1322. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1323. /* This does not get reverted if there's an error later. */
  1324. dm_table_presuspend_targets(map);
  1325. /*
  1326. * Flush I/O to the device. noflush supersedes do_lockfs,
  1327. * because lock_fs() needs to flush I/Os.
  1328. */
  1329. if (!noflush && do_lockfs) {
  1330. r = lock_fs(md);
  1331. if (r)
  1332. goto out;
  1333. }
  1334. /*
  1335. * Here we must make sure that no processes are submitting requests
  1336. * to target drivers i.e. no one may be executing
  1337. * __split_and_process_bio. This is called from dm_request and
  1338. * dm_wq_work.
  1339. *
  1340. * To get all processes out of __split_and_process_bio in dm_request,
  1341. * we take the write lock. To prevent any process from reentering
  1342. * __split_and_process_bio from dm_request, we set
  1343. * DMF_QUEUE_IO_TO_THREAD.
  1344. *
  1345. * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
  1346. * and call flush_workqueue(md->wq). flush_workqueue will wait until
  1347. * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
  1348. * further calls to __split_and_process_bio from dm_wq_work.
  1349. */
  1350. down_write(&md->io_lock);
  1351. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1352. set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
  1353. up_write(&md->io_lock);
  1354. flush_workqueue(md->wq);
  1355. /*
  1356. * At this point no more requests are entering target request routines.
  1357. * We call dm_wait_for_completion to wait for all existing requests
  1358. * to finish.
  1359. */
  1360. r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
  1361. down_write(&md->io_lock);
  1362. if (noflush)
  1363. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1364. up_write(&md->io_lock);
  1365. /* were we interrupted ? */
  1366. if (r < 0) {
  1367. dm_queue_flush(md);
  1368. unlock_fs(md);
  1369. goto out; /* pushback list is already flushed, so skip flush */
  1370. }
  1371. /*
  1372. * If dm_wait_for_completion returned 0, the device is completely
  1373. * quiescent now. There is no request-processing activity. All new
  1374. * requests are being added to md->deferred list.
  1375. */
  1376. dm_table_postsuspend_targets(map);
  1377. set_bit(DMF_SUSPENDED, &md->flags);
  1378. out:
  1379. dm_table_put(map);
  1380. out_unlock:
  1381. mutex_unlock(&md->suspend_lock);
  1382. return r;
  1383. }
  1384. int dm_resume(struct mapped_device *md)
  1385. {
  1386. int r = -EINVAL;
  1387. struct dm_table *map = NULL;
  1388. mutex_lock(&md->suspend_lock);
  1389. if (!dm_suspended(md))
  1390. goto out;
  1391. map = dm_get_table(md);
  1392. if (!map || !dm_table_get_size(map))
  1393. goto out;
  1394. r = dm_table_resume_targets(map);
  1395. if (r)
  1396. goto out;
  1397. dm_queue_flush(md);
  1398. unlock_fs(md);
  1399. clear_bit(DMF_SUSPENDED, &md->flags);
  1400. dm_table_unplug_all(map);
  1401. r = 0;
  1402. out:
  1403. dm_table_put(map);
  1404. mutex_unlock(&md->suspend_lock);
  1405. return r;
  1406. }
  1407. /*-----------------------------------------------------------------
  1408. * Event notification.
  1409. *---------------------------------------------------------------*/
  1410. void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  1411. unsigned cookie)
  1412. {
  1413. char udev_cookie[DM_COOKIE_LENGTH];
  1414. char *envp[] = { udev_cookie, NULL };
  1415. if (!cookie)
  1416. kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  1417. else {
  1418. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  1419. DM_COOKIE_ENV_VAR_NAME, cookie);
  1420. kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
  1421. }
  1422. }
  1423. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  1424. {
  1425. return atomic_add_return(1, &md->uevent_seq);
  1426. }
  1427. uint32_t dm_get_event_nr(struct mapped_device *md)
  1428. {
  1429. return atomic_read(&md->event_nr);
  1430. }
  1431. int dm_wait_event(struct mapped_device *md, int event_nr)
  1432. {
  1433. return wait_event_interruptible(md->eventq,
  1434. (event_nr != atomic_read(&md->event_nr)));
  1435. }
  1436. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  1437. {
  1438. unsigned long flags;
  1439. spin_lock_irqsave(&md->uevent_lock, flags);
  1440. list_add(elist, &md->uevent_list);
  1441. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1442. }
  1443. /*
  1444. * The gendisk is only valid as long as you have a reference
  1445. * count on 'md'.
  1446. */
  1447. struct gendisk *dm_disk(struct mapped_device *md)
  1448. {
  1449. return md->disk;
  1450. }
  1451. struct kobject *dm_kobject(struct mapped_device *md)
  1452. {
  1453. return &md->kobj;
  1454. }
  1455. /*
  1456. * struct mapped_device should not be exported outside of dm.c
  1457. * so use this check to verify that kobj is part of md structure
  1458. */
  1459. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  1460. {
  1461. struct mapped_device *md;
  1462. md = container_of(kobj, struct mapped_device, kobj);
  1463. if (&md->kobj != kobj)
  1464. return NULL;
  1465. if (test_bit(DMF_FREEING, &md->flags) ||
  1466. test_bit(DMF_DELETING, &md->flags))
  1467. return NULL;
  1468. dm_get(md);
  1469. return md;
  1470. }
  1471. int dm_suspended(struct mapped_device *md)
  1472. {
  1473. return test_bit(DMF_SUSPENDED, &md->flags);
  1474. }
  1475. int dm_noflush_suspending(struct dm_target *ti)
  1476. {
  1477. struct mapped_device *md = dm_table_get_md(ti->table);
  1478. int r = __noflush_suspending(md);
  1479. dm_put(md);
  1480. return r;
  1481. }
  1482. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  1483. static struct block_device_operations dm_blk_dops = {
  1484. .open = dm_blk_open,
  1485. .release = dm_blk_close,
  1486. .ioctl = dm_blk_ioctl,
  1487. .getgeo = dm_blk_getgeo,
  1488. .owner = THIS_MODULE
  1489. };
  1490. EXPORT_SYMBOL(dm_get_mapinfo);
  1491. /*
  1492. * module hooks
  1493. */
  1494. module_init(dm_init);
  1495. module_exit(dm_exit);
  1496. module_param(major, uint, 0);
  1497. MODULE_PARM_DESC(major, "The major number of the device mapper");
  1498. MODULE_DESCRIPTION(DM_NAME " driver");
  1499. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1500. MODULE_LICENSE("GPL");