dm.c 34 KB

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