dm-snap.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/blkdev.h>
  8. #include <linux/device-mapper.h>
  9. #include <linux/delay.h>
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/list.h>
  14. #include <linux/list_bl.h>
  15. #include <linux/mempool.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/log2.h>
  20. #include <linux/dm-kcopyd.h>
  21. #include "dm.h"
  22. #include "dm-exception-store.h"
  23. #define DM_MSG_PREFIX "snapshots"
  24. static const char dm_snapshot_merge_target_name[] = "snapshot-merge";
  25. #define dm_target_is_snapshot_merge(ti) \
  26. ((ti)->type->name == dm_snapshot_merge_target_name)
  27. /*
  28. * The size of the mempool used to track chunks in use.
  29. */
  30. #define MIN_IOS 256
  31. #define DM_TRACKED_CHUNK_HASH_SIZE 16
  32. #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
  33. (DM_TRACKED_CHUNK_HASH_SIZE - 1))
  34. struct dm_hlist_head {
  35. struct hlist_head head;
  36. spinlock_t lock;
  37. };
  38. struct dm_exception_table {
  39. uint32_t hash_mask;
  40. unsigned int hash_shift;
  41. struct dm_hlist_head *table;
  42. };
  43. struct dm_snapshot {
  44. struct rw_semaphore lock;
  45. struct dm_dev *origin;
  46. struct dm_dev *cow;
  47. struct dm_target *ti;
  48. /* List of snapshots per Origin */
  49. struct list_head list;
  50. /*
  51. * You can't use a snapshot if this is 0 (e.g. if full).
  52. * A snapshot-merge target never clears this.
  53. */
  54. int valid;
  55. /*
  56. * The snapshot overflowed because of a write to the snapshot device.
  57. * We don't have to invalidate the snapshot in this case, but we need
  58. * to prevent further writes.
  59. */
  60. int snapshot_overflowed;
  61. /* Origin writes don't trigger exceptions until this is set */
  62. int active;
  63. atomic_t pending_exceptions_count;
  64. spinlock_t pe_allocation_lock;
  65. /* Protected by "pe_allocation_lock" */
  66. sector_t exception_start_sequence;
  67. /* Protected by kcopyd single-threaded callback */
  68. sector_t exception_complete_sequence;
  69. /*
  70. * A list of pending exceptions that completed out of order.
  71. * Protected by kcopyd single-threaded callback.
  72. */
  73. struct rb_root out_of_order_tree;
  74. mempool_t pending_pool;
  75. struct dm_exception_table pending;
  76. struct dm_exception_table complete;
  77. /*
  78. * pe_lock protects all pending_exception operations and access
  79. * as well as the snapshot_bios list.
  80. */
  81. spinlock_t pe_lock;
  82. /* Chunks with outstanding reads */
  83. spinlock_t tracked_chunk_lock;
  84. struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
  85. /* The on disk metadata handler */
  86. struct dm_exception_store *store;
  87. unsigned int in_progress;
  88. struct wait_queue_head in_progress_wait;
  89. struct dm_kcopyd_client *kcopyd_client;
  90. /* Wait for events based on state_bits */
  91. unsigned long state_bits;
  92. /* Range of chunks currently being merged. */
  93. chunk_t first_merging_chunk;
  94. int num_merging_chunks;
  95. /*
  96. * The merge operation failed if this flag is set.
  97. * Failure modes are handled as follows:
  98. * - I/O error reading the header
  99. * => don't load the target; abort.
  100. * - Header does not have "valid" flag set
  101. * => use the origin; forget about the snapshot.
  102. * - I/O error when reading exceptions
  103. * => don't load the target; abort.
  104. * (We can't use the intermediate origin state.)
  105. * - I/O error while merging
  106. * => stop merging; set merge_failed; process I/O normally.
  107. */
  108. bool merge_failed:1;
  109. bool discard_zeroes_cow:1;
  110. bool discard_passdown_origin:1;
  111. /*
  112. * Incoming bios that overlap with chunks being merged must wait
  113. * for them to be committed.
  114. */
  115. struct bio_list bios_queued_during_merge;
  116. };
  117. /*
  118. * state_bits:
  119. * RUNNING_MERGE - Merge operation is in progress.
  120. * SHUTDOWN_MERGE - Set to signal that merge needs to be stopped;
  121. * cleared afterwards.
  122. */
  123. #define RUNNING_MERGE 0
  124. #define SHUTDOWN_MERGE 1
  125. /*
  126. * Maximum number of chunks being copied on write.
  127. *
  128. * The value was decided experimentally as a trade-off between memory
  129. * consumption, stalling the kernel's workqueues and maintaining a high enough
  130. * throughput.
  131. */
  132. #define DEFAULT_COW_THRESHOLD 2048
  133. static unsigned int cow_threshold = DEFAULT_COW_THRESHOLD;
  134. module_param_named(snapshot_cow_threshold, cow_threshold, uint, 0644);
  135. MODULE_PARM_DESC(snapshot_cow_threshold, "Maximum number of chunks being copied on write");
  136. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
  137. "A percentage of time allocated for copy on write");
  138. struct dm_dev *dm_snap_origin(struct dm_snapshot *s)
  139. {
  140. return s->origin;
  141. }
  142. EXPORT_SYMBOL(dm_snap_origin);
  143. struct dm_dev *dm_snap_cow(struct dm_snapshot *s)
  144. {
  145. return s->cow;
  146. }
  147. EXPORT_SYMBOL(dm_snap_cow);
  148. static sector_t chunk_to_sector(struct dm_exception_store *store,
  149. chunk_t chunk)
  150. {
  151. return chunk << store->chunk_shift;
  152. }
  153. static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
  154. {
  155. /*
  156. * There is only ever one instance of a particular block
  157. * device so we can compare pointers safely.
  158. */
  159. return lhs == rhs;
  160. }
  161. struct dm_snap_pending_exception {
  162. struct dm_exception e;
  163. /*
  164. * Origin buffers waiting for this to complete are held
  165. * in a bio list
  166. */
  167. struct bio_list origin_bios;
  168. struct bio_list snapshot_bios;
  169. /* Pointer back to snapshot context */
  170. struct dm_snapshot *snap;
  171. /*
  172. * 1 indicates the exception has already been sent to
  173. * kcopyd.
  174. */
  175. int started;
  176. /* There was copying error. */
  177. int copy_error;
  178. /* A sequence number, it is used for in-order completion. */
  179. sector_t exception_sequence;
  180. struct rb_node out_of_order_node;
  181. /*
  182. * For writing a complete chunk, bypassing the copy.
  183. */
  184. struct bio *full_bio;
  185. bio_end_io_t *full_bio_end_io;
  186. };
  187. /*
  188. * Hash table mapping origin volumes to lists of snapshots and
  189. * a lock to protect it
  190. */
  191. static struct kmem_cache *exception_cache;
  192. static struct kmem_cache *pending_cache;
  193. struct dm_snap_tracked_chunk {
  194. struct hlist_node node;
  195. chunk_t chunk;
  196. };
  197. static void init_tracked_chunk(struct bio *bio)
  198. {
  199. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  200. INIT_HLIST_NODE(&c->node);
  201. }
  202. static bool is_bio_tracked(struct bio *bio)
  203. {
  204. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  205. return !hlist_unhashed(&c->node);
  206. }
  207. static void track_chunk(struct dm_snapshot *s, struct bio *bio, chunk_t chunk)
  208. {
  209. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  210. c->chunk = chunk;
  211. spin_lock_irq(&s->tracked_chunk_lock);
  212. hlist_add_head(&c->node,
  213. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
  214. spin_unlock_irq(&s->tracked_chunk_lock);
  215. }
  216. static void stop_tracking_chunk(struct dm_snapshot *s, struct bio *bio)
  217. {
  218. struct dm_snap_tracked_chunk *c = dm_per_bio_data(bio, sizeof(struct dm_snap_tracked_chunk));
  219. unsigned long flags;
  220. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  221. hlist_del(&c->node);
  222. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  223. }
  224. static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
  225. {
  226. struct dm_snap_tracked_chunk *c;
  227. int found = 0;
  228. spin_lock_irq(&s->tracked_chunk_lock);
  229. hlist_for_each_entry(c,
  230. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
  231. if (c->chunk == chunk) {
  232. found = 1;
  233. break;
  234. }
  235. }
  236. spin_unlock_irq(&s->tracked_chunk_lock);
  237. return found;
  238. }
  239. /*
  240. * This conflicting I/O is extremely improbable in the caller,
  241. * so fsleep(1000) is sufficient and there is no need for a wait queue.
  242. */
  243. static void __check_for_conflicting_io(struct dm_snapshot *s, chunk_t chunk)
  244. {
  245. while (__chunk_is_tracked(s, chunk))
  246. fsleep(1000);
  247. }
  248. /*
  249. * One of these per registered origin, held in the snapshot_origins hash
  250. */
  251. struct origin {
  252. /* The origin device */
  253. struct block_device *bdev;
  254. struct list_head hash_list;
  255. /* List of snapshots for this origin */
  256. struct list_head snapshots;
  257. };
  258. /*
  259. * This structure is allocated for each origin target
  260. */
  261. struct dm_origin {
  262. struct dm_dev *dev;
  263. struct dm_target *ti;
  264. unsigned int split_boundary;
  265. struct list_head hash_list;
  266. };
  267. /*
  268. * Size of the hash table for origin volumes. If we make this
  269. * the size of the minors list then it should be nearly perfect
  270. */
  271. #define ORIGIN_HASH_SIZE 256
  272. #define ORIGIN_MASK 0xFF
  273. static struct list_head *_origins;
  274. static struct list_head *_dm_origins;
  275. static struct rw_semaphore _origins_lock;
  276. static DECLARE_WAIT_QUEUE_HEAD(_pending_exceptions_done);
  277. static DEFINE_SPINLOCK(_pending_exceptions_done_spinlock);
  278. static uint64_t _pending_exceptions_done_count;
  279. static int init_origin_hash(void)
  280. {
  281. int i;
  282. _origins = kmalloc_objs(struct list_head, ORIGIN_HASH_SIZE);
  283. if (!_origins) {
  284. DMERR("unable to allocate memory for _origins");
  285. return -ENOMEM;
  286. }
  287. for (i = 0; i < ORIGIN_HASH_SIZE; i++)
  288. INIT_LIST_HEAD(_origins + i);
  289. _dm_origins = kmalloc_objs(struct list_head, ORIGIN_HASH_SIZE);
  290. if (!_dm_origins) {
  291. DMERR("unable to allocate memory for _dm_origins");
  292. kfree(_origins);
  293. return -ENOMEM;
  294. }
  295. for (i = 0; i < ORIGIN_HASH_SIZE; i++)
  296. INIT_LIST_HEAD(_dm_origins + i);
  297. init_rwsem(&_origins_lock);
  298. return 0;
  299. }
  300. static void exit_origin_hash(void)
  301. {
  302. kfree(_origins);
  303. kfree(_dm_origins);
  304. }
  305. static unsigned int origin_hash(struct block_device *bdev)
  306. {
  307. return bdev->bd_dev & ORIGIN_MASK;
  308. }
  309. static struct origin *__lookup_origin(struct block_device *origin)
  310. {
  311. struct list_head *ol;
  312. struct origin *o;
  313. ol = &_origins[origin_hash(origin)];
  314. list_for_each_entry(o, ol, hash_list)
  315. if (bdev_equal(o->bdev, origin))
  316. return o;
  317. return NULL;
  318. }
  319. static void __insert_origin(struct origin *o)
  320. {
  321. struct list_head *sl = &_origins[origin_hash(o->bdev)];
  322. list_add_tail(&o->hash_list, sl);
  323. }
  324. static struct dm_origin *__lookup_dm_origin(struct block_device *origin)
  325. {
  326. struct list_head *ol;
  327. struct dm_origin *o;
  328. ol = &_dm_origins[origin_hash(origin)];
  329. list_for_each_entry(o, ol, hash_list)
  330. if (bdev_equal(o->dev->bdev, origin))
  331. return o;
  332. return NULL;
  333. }
  334. static void __insert_dm_origin(struct dm_origin *o)
  335. {
  336. struct list_head *sl = &_dm_origins[origin_hash(o->dev->bdev)];
  337. list_add_tail(&o->hash_list, sl);
  338. }
  339. static void __remove_dm_origin(struct dm_origin *o)
  340. {
  341. list_del(&o->hash_list);
  342. }
  343. /*
  344. * _origins_lock must be held when calling this function.
  345. * Returns number of snapshots registered using the supplied cow device, plus:
  346. * snap_src - a snapshot suitable for use as a source of exception handover
  347. * snap_dest - a snapshot capable of receiving exception handover.
  348. * snap_merge - an existing snapshot-merge target linked to the same origin.
  349. * There can be at most one snapshot-merge target. The parameter is optional.
  350. *
  351. * Possible return values and states of snap_src and snap_dest.
  352. * 0: NULL, NULL - first new snapshot
  353. * 1: snap_src, NULL - normal snapshot
  354. * 2: snap_src, snap_dest - waiting for handover
  355. * 2: snap_src, NULL - handed over, waiting for old to be deleted
  356. * 1: NULL, snap_dest - source got destroyed without handover
  357. */
  358. static int __find_snapshots_sharing_cow(struct dm_snapshot *snap,
  359. struct dm_snapshot **snap_src,
  360. struct dm_snapshot **snap_dest,
  361. struct dm_snapshot **snap_merge)
  362. {
  363. struct dm_snapshot *s;
  364. struct origin *o;
  365. int count = 0;
  366. int active;
  367. o = __lookup_origin(snap->origin->bdev);
  368. if (!o)
  369. goto out;
  370. list_for_each_entry(s, &o->snapshots, list) {
  371. if (dm_target_is_snapshot_merge(s->ti) && snap_merge)
  372. *snap_merge = s;
  373. if (!bdev_equal(s->cow->bdev, snap->cow->bdev))
  374. continue;
  375. down_read(&s->lock);
  376. active = s->active;
  377. up_read(&s->lock);
  378. if (active) {
  379. if (snap_src)
  380. *snap_src = s;
  381. } else if (snap_dest)
  382. *snap_dest = s;
  383. count++;
  384. }
  385. out:
  386. return count;
  387. }
  388. /*
  389. * On success, returns 1 if this snapshot is a handover destination,
  390. * otherwise returns 0.
  391. */
  392. static int __validate_exception_handover(struct dm_snapshot *snap)
  393. {
  394. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
  395. struct dm_snapshot *snap_merge = NULL;
  396. /* Does snapshot need exceptions handed over to it? */
  397. if ((__find_snapshots_sharing_cow(snap, &snap_src, &snap_dest,
  398. &snap_merge) == 2) ||
  399. snap_dest) {
  400. snap->ti->error = "Snapshot cow pairing for exception table handover failed";
  401. return -EINVAL;
  402. }
  403. /*
  404. * If no snap_src was found, snap cannot become a handover
  405. * destination.
  406. */
  407. if (!snap_src)
  408. return 0;
  409. /*
  410. * Non-snapshot-merge handover?
  411. */
  412. if (!dm_target_is_snapshot_merge(snap->ti))
  413. return 1;
  414. /*
  415. * Do not allow more than one merging snapshot.
  416. */
  417. if (snap_merge) {
  418. snap->ti->error = "A snapshot is already merging.";
  419. return -EINVAL;
  420. }
  421. if (!snap_src->store->type->prepare_merge ||
  422. !snap_src->store->type->commit_merge) {
  423. snap->ti->error = "Snapshot exception store does not support snapshot-merge.";
  424. return -EINVAL;
  425. }
  426. return 1;
  427. }
  428. static void __insert_snapshot(struct origin *o, struct dm_snapshot *s)
  429. {
  430. struct dm_snapshot *l;
  431. /* Sort the list according to chunk size, largest-first smallest-last */
  432. list_for_each_entry(l, &o->snapshots, list)
  433. if (l->store->chunk_size < s->store->chunk_size)
  434. break;
  435. list_add_tail(&s->list, &l->list);
  436. }
  437. /*
  438. * Make a note of the snapshot and its origin so we can look it
  439. * up when the origin has a write on it.
  440. *
  441. * Also validate snapshot exception store handovers.
  442. * On success, returns 1 if this registration is a handover destination,
  443. * otherwise returns 0.
  444. */
  445. static int register_snapshot(struct dm_snapshot *snap)
  446. {
  447. struct origin *o, *new_o = NULL;
  448. struct block_device *bdev = snap->origin->bdev;
  449. int r = 0;
  450. new_o = kmalloc_obj(*new_o);
  451. if (!new_o)
  452. return -ENOMEM;
  453. down_write(&_origins_lock);
  454. r = __validate_exception_handover(snap);
  455. if (r < 0) {
  456. kfree(new_o);
  457. goto out;
  458. }
  459. o = __lookup_origin(bdev);
  460. if (o)
  461. kfree(new_o);
  462. else {
  463. /* New origin */
  464. o = new_o;
  465. /* Initialise the struct */
  466. INIT_LIST_HEAD(&o->snapshots);
  467. o->bdev = bdev;
  468. __insert_origin(o);
  469. }
  470. __insert_snapshot(o, snap);
  471. out:
  472. up_write(&_origins_lock);
  473. return r;
  474. }
  475. /*
  476. * Move snapshot to correct place in list according to chunk size.
  477. */
  478. static void reregister_snapshot(struct dm_snapshot *s)
  479. {
  480. struct block_device *bdev = s->origin->bdev;
  481. down_write(&_origins_lock);
  482. list_del(&s->list);
  483. __insert_snapshot(__lookup_origin(bdev), s);
  484. up_write(&_origins_lock);
  485. }
  486. static void unregister_snapshot(struct dm_snapshot *s)
  487. {
  488. struct origin *o;
  489. down_write(&_origins_lock);
  490. o = __lookup_origin(s->origin->bdev);
  491. list_del(&s->list);
  492. if (o && list_empty(&o->snapshots)) {
  493. list_del(&o->hash_list);
  494. kfree(o);
  495. }
  496. up_write(&_origins_lock);
  497. }
  498. /*
  499. * Implementation of the exception hash tables.
  500. * The lowest hash_shift bits of the chunk number are ignored, allowing
  501. * some consecutive chunks to be grouped together.
  502. */
  503. static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk);
  504. /* Lock to protect access to the completed and pending exception hash tables. */
  505. struct dm_exception_table_lock {
  506. spinlock_t *complete_slot;
  507. spinlock_t *pending_slot;
  508. };
  509. static void dm_exception_table_lock_init(struct dm_snapshot *s, chunk_t chunk,
  510. struct dm_exception_table_lock *lock)
  511. {
  512. struct dm_exception_table *complete = &s->complete;
  513. struct dm_exception_table *pending = &s->pending;
  514. lock->complete_slot = &complete->table[exception_hash(complete, chunk)].lock;
  515. lock->pending_slot = &pending->table[exception_hash(pending, chunk)].lock;
  516. }
  517. static void dm_exception_table_lock(struct dm_exception_table_lock *lock)
  518. {
  519. spin_lock_nested(lock->complete_slot, 1);
  520. spin_lock_nested(lock->pending_slot, 2);
  521. }
  522. static void dm_exception_table_unlock(struct dm_exception_table_lock *lock)
  523. {
  524. spin_unlock(lock->pending_slot);
  525. spin_unlock(lock->complete_slot);
  526. }
  527. static int dm_exception_table_init(struct dm_exception_table *et,
  528. uint32_t size, unsigned int hash_shift)
  529. {
  530. unsigned int i;
  531. et->hash_shift = hash_shift;
  532. et->hash_mask = size - 1;
  533. et->table = kvmalloc_objs(struct dm_hlist_head, size);
  534. if (!et->table)
  535. return -ENOMEM;
  536. for (i = 0; i < size; i++) {
  537. INIT_HLIST_HEAD(&et->table[i].head);
  538. spin_lock_init(&et->table[i].lock);
  539. }
  540. return 0;
  541. }
  542. static void dm_exception_table_exit(struct dm_exception_table *et,
  543. struct kmem_cache *mem)
  544. {
  545. struct dm_hlist_head *slot;
  546. struct dm_exception *ex;
  547. struct hlist_node *pos;
  548. int i, size;
  549. size = et->hash_mask + 1;
  550. for (i = 0; i < size; i++) {
  551. slot = et->table + i;
  552. hlist_for_each_entry_safe(ex, pos, &slot->head, hash_list) {
  553. hlist_del(&ex->hash_list);
  554. kmem_cache_free(mem, ex);
  555. cond_resched();
  556. }
  557. }
  558. kvfree(et->table);
  559. }
  560. static uint32_t exception_hash(struct dm_exception_table *et, chunk_t chunk)
  561. {
  562. return (chunk >> et->hash_shift) & et->hash_mask;
  563. }
  564. static void dm_remove_exception(struct dm_exception *e)
  565. {
  566. hlist_del(&e->hash_list);
  567. }
  568. /*
  569. * Return the exception data for a sector, or NULL if not
  570. * remapped.
  571. */
  572. static struct dm_exception *dm_lookup_exception(struct dm_exception_table *et,
  573. chunk_t chunk)
  574. {
  575. struct hlist_head *slot;
  576. struct dm_exception *e;
  577. slot = &et->table[exception_hash(et, chunk)].head;
  578. hlist_for_each_entry(e, slot, hash_list)
  579. if (chunk >= e->old_chunk &&
  580. chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
  581. return e;
  582. return NULL;
  583. }
  584. static struct dm_exception *alloc_completed_exception(gfp_t gfp)
  585. {
  586. struct dm_exception *e;
  587. e = kmem_cache_alloc(exception_cache, gfp);
  588. if (!e && gfp == GFP_NOIO)
  589. e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
  590. return e;
  591. }
  592. static void free_completed_exception(struct dm_exception *e)
  593. {
  594. kmem_cache_free(exception_cache, e);
  595. }
  596. static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
  597. {
  598. struct dm_snap_pending_exception *pe = mempool_alloc(&s->pending_pool,
  599. GFP_NOIO);
  600. atomic_inc(&s->pending_exceptions_count);
  601. pe->snap = s;
  602. return pe;
  603. }
  604. static void free_pending_exception(struct dm_snap_pending_exception *pe)
  605. {
  606. struct dm_snapshot *s = pe->snap;
  607. mempool_free(pe, &s->pending_pool);
  608. smp_mb__before_atomic();
  609. atomic_dec(&s->pending_exceptions_count);
  610. }
  611. static void dm_insert_exception(struct dm_exception_table *eh,
  612. struct dm_exception *new_e)
  613. {
  614. struct hlist_head *l;
  615. struct dm_exception *e = NULL;
  616. l = &eh->table[exception_hash(eh, new_e->old_chunk)].head;
  617. /* Add immediately if this table doesn't support consecutive chunks */
  618. if (!eh->hash_shift)
  619. goto out;
  620. /* List is ordered by old_chunk */
  621. hlist_for_each_entry(e, l, hash_list) {
  622. /* Insert after an existing chunk? */
  623. if (new_e->old_chunk == (e->old_chunk +
  624. dm_consecutive_chunk_count(e) + 1) &&
  625. new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
  626. dm_consecutive_chunk_count(e) + 1)) {
  627. dm_consecutive_chunk_count_inc(e);
  628. free_completed_exception(new_e);
  629. return;
  630. }
  631. /* Insert before an existing chunk? */
  632. if (new_e->old_chunk == (e->old_chunk - 1) &&
  633. new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
  634. dm_consecutive_chunk_count_inc(e);
  635. e->old_chunk--;
  636. e->new_chunk--;
  637. free_completed_exception(new_e);
  638. return;
  639. }
  640. if (new_e->old_chunk < e->old_chunk)
  641. break;
  642. }
  643. out:
  644. if (!e) {
  645. /*
  646. * Either the table doesn't support consecutive chunks or slot
  647. * l is empty.
  648. */
  649. hlist_add_head(&new_e->hash_list, l);
  650. } else if (new_e->old_chunk < e->old_chunk) {
  651. /* Add before an existing exception */
  652. hlist_add_before(&new_e->hash_list, &e->hash_list);
  653. } else {
  654. /* Add to l's tail: e is the last exception in this slot */
  655. hlist_add_behind(&new_e->hash_list, &e->hash_list);
  656. }
  657. }
  658. /*
  659. * Callback used by the exception stores to load exceptions when
  660. * initialising.
  661. */
  662. static int dm_add_exception(void *context, chunk_t old, chunk_t new)
  663. {
  664. struct dm_snapshot *s = context;
  665. struct dm_exception *e;
  666. e = alloc_completed_exception(GFP_KERNEL);
  667. if (!e)
  668. return -ENOMEM;
  669. e->old_chunk = old;
  670. /* Consecutive_count is implicitly initialised to zero */
  671. e->new_chunk = new;
  672. dm_insert_exception(&s->complete, e);
  673. return 0;
  674. }
  675. /*
  676. * Return a minimum chunk size of all snapshots that have the specified origin.
  677. * Return zero if the origin has no snapshots.
  678. */
  679. static uint32_t __minimum_chunk_size(struct origin *o)
  680. {
  681. struct dm_snapshot *snap;
  682. unsigned int chunk_size = rounddown_pow_of_two(UINT_MAX);
  683. if (o)
  684. list_for_each_entry(snap, &o->snapshots, list)
  685. chunk_size = min_not_zero(chunk_size,
  686. snap->store->chunk_size);
  687. return (uint32_t) chunk_size;
  688. }
  689. /*
  690. * Hard coded magic.
  691. */
  692. static int calc_max_buckets(void)
  693. {
  694. /* use a fixed size of 2MB */
  695. unsigned long mem = 2 * 1024 * 1024;
  696. mem /= sizeof(struct dm_hlist_head);
  697. return mem;
  698. }
  699. /*
  700. * Allocate room for a suitable hash table.
  701. */
  702. static int init_hash_tables(struct dm_snapshot *s)
  703. {
  704. sector_t hash_size, cow_dev_size, max_buckets;
  705. /*
  706. * Calculate based on the size of the original volume or
  707. * the COW volume...
  708. */
  709. cow_dev_size = get_dev_size(s->cow->bdev);
  710. max_buckets = calc_max_buckets();
  711. hash_size = cow_dev_size >> s->store->chunk_shift;
  712. hash_size = min(hash_size, max_buckets);
  713. if (hash_size < 64)
  714. hash_size = 64;
  715. hash_size = rounddown_pow_of_two(hash_size);
  716. if (dm_exception_table_init(&s->complete, hash_size,
  717. DM_CHUNK_CONSECUTIVE_BITS))
  718. return -ENOMEM;
  719. /*
  720. * Allocate hash table for in-flight exceptions
  721. * Make this smaller than the real hash table
  722. */
  723. hash_size >>= 3;
  724. if (hash_size < 64)
  725. hash_size = 64;
  726. if (dm_exception_table_init(&s->pending, hash_size, 0)) {
  727. dm_exception_table_exit(&s->complete, exception_cache);
  728. return -ENOMEM;
  729. }
  730. return 0;
  731. }
  732. static void merge_shutdown(struct dm_snapshot *s)
  733. {
  734. clear_bit_unlock(RUNNING_MERGE, &s->state_bits);
  735. smp_mb__after_atomic();
  736. wake_up_bit(&s->state_bits, RUNNING_MERGE);
  737. }
  738. static struct bio *__release_queued_bios_after_merge(struct dm_snapshot *s)
  739. {
  740. s->first_merging_chunk = 0;
  741. s->num_merging_chunks = 0;
  742. return bio_list_get(&s->bios_queued_during_merge);
  743. }
  744. /*
  745. * Remove one chunk from the index of completed exceptions.
  746. */
  747. static int __remove_single_exception_chunk(struct dm_snapshot *s,
  748. chunk_t old_chunk)
  749. {
  750. struct dm_exception *e;
  751. e = dm_lookup_exception(&s->complete, old_chunk);
  752. if (!e) {
  753. DMERR("Corruption detected: exception for block %llu is on disk but not in memory",
  754. (unsigned long long)old_chunk);
  755. return -EINVAL;
  756. }
  757. /*
  758. * If this is the only chunk using this exception, remove exception.
  759. */
  760. if (!dm_consecutive_chunk_count(e)) {
  761. dm_remove_exception(e);
  762. free_completed_exception(e);
  763. return 0;
  764. }
  765. /*
  766. * The chunk may be either at the beginning or the end of a
  767. * group of consecutive chunks - never in the middle. We are
  768. * removing chunks in the opposite order to that in which they
  769. * were added, so this should always be true.
  770. * Decrement the consecutive chunk counter and adjust the
  771. * starting point if necessary.
  772. */
  773. if (old_chunk == e->old_chunk) {
  774. e->old_chunk++;
  775. e->new_chunk++;
  776. } else if (old_chunk != e->old_chunk +
  777. dm_consecutive_chunk_count(e)) {
  778. DMERR("Attempt to merge block %llu from the middle of a chunk range [%llu - %llu]",
  779. (unsigned long long)old_chunk,
  780. (unsigned long long)e->old_chunk,
  781. (unsigned long long)
  782. e->old_chunk + dm_consecutive_chunk_count(e));
  783. return -EINVAL;
  784. }
  785. dm_consecutive_chunk_count_dec(e);
  786. return 0;
  787. }
  788. static void flush_bios(struct bio *bio);
  789. static int remove_single_exception_chunk(struct dm_snapshot *s)
  790. {
  791. struct bio *b = NULL;
  792. int r;
  793. chunk_t old_chunk = s->first_merging_chunk + s->num_merging_chunks - 1;
  794. down_write(&s->lock);
  795. /*
  796. * Process chunks (and associated exceptions) in reverse order
  797. * so that dm_consecutive_chunk_count_dec() accounting works.
  798. */
  799. do {
  800. r = __remove_single_exception_chunk(s, old_chunk);
  801. if (r)
  802. goto out;
  803. } while (old_chunk-- > s->first_merging_chunk);
  804. b = __release_queued_bios_after_merge(s);
  805. out:
  806. up_write(&s->lock);
  807. if (b)
  808. flush_bios(b);
  809. return r;
  810. }
  811. static int origin_write_extent(struct dm_snapshot *merging_snap,
  812. sector_t sector, unsigned int chunk_size);
  813. static void merge_callback(int read_err, unsigned long write_err,
  814. void *context);
  815. static uint64_t read_pending_exceptions_done_count(void)
  816. {
  817. uint64_t pending_exceptions_done;
  818. spin_lock(&_pending_exceptions_done_spinlock);
  819. pending_exceptions_done = _pending_exceptions_done_count;
  820. spin_unlock(&_pending_exceptions_done_spinlock);
  821. return pending_exceptions_done;
  822. }
  823. static void increment_pending_exceptions_done_count(void)
  824. {
  825. spin_lock(&_pending_exceptions_done_spinlock);
  826. _pending_exceptions_done_count++;
  827. spin_unlock(&_pending_exceptions_done_spinlock);
  828. wake_up_all(&_pending_exceptions_done);
  829. }
  830. static void snapshot_merge_next_chunks(struct dm_snapshot *s)
  831. {
  832. int i, linear_chunks;
  833. chunk_t old_chunk, new_chunk;
  834. struct dm_io_region src, dest;
  835. sector_t io_size;
  836. uint64_t previous_count;
  837. BUG_ON(!test_bit(RUNNING_MERGE, &s->state_bits));
  838. if (unlikely(test_bit(SHUTDOWN_MERGE, &s->state_bits)))
  839. goto shut;
  840. /*
  841. * valid flag never changes during merge, so no lock required.
  842. */
  843. if (!s->valid) {
  844. DMERR("Snapshot is invalid: can't merge");
  845. goto shut;
  846. }
  847. linear_chunks = s->store->type->prepare_merge(s->store, &old_chunk,
  848. &new_chunk);
  849. if (linear_chunks <= 0) {
  850. if (linear_chunks < 0) {
  851. DMERR("Read error in exception store: shutting down merge");
  852. down_write(&s->lock);
  853. s->merge_failed = true;
  854. up_write(&s->lock);
  855. }
  856. goto shut;
  857. }
  858. /* Adjust old_chunk and new_chunk to reflect start of linear region */
  859. old_chunk = old_chunk + 1 - linear_chunks;
  860. new_chunk = new_chunk + 1 - linear_chunks;
  861. /*
  862. * Use one (potentially large) I/O to copy all 'linear_chunks'
  863. * from the exception store to the origin
  864. */
  865. io_size = linear_chunks * s->store->chunk_size;
  866. dest.bdev = s->origin->bdev;
  867. dest.sector = chunk_to_sector(s->store, old_chunk);
  868. dest.count = min(io_size, get_dev_size(dest.bdev) - dest.sector);
  869. src.bdev = s->cow->bdev;
  870. src.sector = chunk_to_sector(s->store, new_chunk);
  871. src.count = dest.count;
  872. /*
  873. * Reallocate any exceptions needed in other snapshots then
  874. * wait for the pending exceptions to complete.
  875. * Each time any pending exception (globally on the system)
  876. * completes we are woken and repeat the process to find out
  877. * if we can proceed. While this may not seem a particularly
  878. * efficient algorithm, it is not expected to have any
  879. * significant impact on performance.
  880. */
  881. previous_count = read_pending_exceptions_done_count();
  882. while (origin_write_extent(s, dest.sector, io_size)) {
  883. wait_event(_pending_exceptions_done,
  884. (read_pending_exceptions_done_count() !=
  885. previous_count));
  886. /* Retry after the wait, until all exceptions are done. */
  887. previous_count = read_pending_exceptions_done_count();
  888. }
  889. down_write(&s->lock);
  890. s->first_merging_chunk = old_chunk;
  891. s->num_merging_chunks = linear_chunks;
  892. up_write(&s->lock);
  893. /* Wait until writes to all 'linear_chunks' drain */
  894. for (i = 0; i < linear_chunks; i++)
  895. __check_for_conflicting_io(s, old_chunk + i);
  896. dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, merge_callback, s);
  897. return;
  898. shut:
  899. merge_shutdown(s);
  900. }
  901. static void error_bios(struct bio *bio);
  902. static void merge_callback(int read_err, unsigned long write_err, void *context)
  903. {
  904. struct dm_snapshot *s = context;
  905. struct bio *b = NULL;
  906. if (read_err || write_err) {
  907. if (read_err)
  908. DMERR("Read error: shutting down merge.");
  909. else
  910. DMERR("Write error: shutting down merge.");
  911. goto shut;
  912. }
  913. if (blkdev_issue_flush(s->origin->bdev) < 0) {
  914. DMERR("Flush after merge failed: shutting down merge");
  915. goto shut;
  916. }
  917. if (s->store->type->commit_merge(s->store,
  918. s->num_merging_chunks) < 0) {
  919. DMERR("Write error in exception store: shutting down merge");
  920. goto shut;
  921. }
  922. if (remove_single_exception_chunk(s) < 0)
  923. goto shut;
  924. snapshot_merge_next_chunks(s);
  925. return;
  926. shut:
  927. down_write(&s->lock);
  928. s->merge_failed = true;
  929. b = __release_queued_bios_after_merge(s);
  930. up_write(&s->lock);
  931. error_bios(b);
  932. merge_shutdown(s);
  933. }
  934. static void start_merge(struct dm_snapshot *s)
  935. {
  936. if (!test_and_set_bit(RUNNING_MERGE, &s->state_bits))
  937. snapshot_merge_next_chunks(s);
  938. }
  939. /*
  940. * Stop the merging process and wait until it finishes.
  941. */
  942. static void stop_merge(struct dm_snapshot *s)
  943. {
  944. set_bit(SHUTDOWN_MERGE, &s->state_bits);
  945. wait_on_bit(&s->state_bits, RUNNING_MERGE, TASK_UNINTERRUPTIBLE);
  946. clear_bit(SHUTDOWN_MERGE, &s->state_bits);
  947. }
  948. static int parse_snapshot_features(struct dm_arg_set *as, struct dm_snapshot *s,
  949. struct dm_target *ti)
  950. {
  951. int r;
  952. unsigned int argc;
  953. const char *arg_name;
  954. static const struct dm_arg _args[] = {
  955. {0, 2, "Invalid number of feature arguments"},
  956. };
  957. /*
  958. * No feature arguments supplied.
  959. */
  960. if (!as->argc)
  961. return 0;
  962. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  963. if (r)
  964. return -EINVAL;
  965. while (argc && !r) {
  966. arg_name = dm_shift_arg(as);
  967. argc--;
  968. if (!strcasecmp(arg_name, "discard_zeroes_cow"))
  969. s->discard_zeroes_cow = true;
  970. else if (!strcasecmp(arg_name, "discard_passdown_origin"))
  971. s->discard_passdown_origin = true;
  972. else {
  973. ti->error = "Unrecognised feature requested";
  974. r = -EINVAL;
  975. break;
  976. }
  977. }
  978. if (!s->discard_zeroes_cow && s->discard_passdown_origin) {
  979. /*
  980. * TODO: really these are disjoint.. but ti->num_discard_bios
  981. * and dm_bio_get_target_bio_nr() require rigid constraints.
  982. */
  983. ti->error = "discard_passdown_origin feature depends on discard_zeroes_cow";
  984. r = -EINVAL;
  985. }
  986. return r;
  987. }
  988. /*
  989. * Construct a snapshot mapping:
  990. * <origin_dev> <COW-dev> <p|po|n> <chunk-size> [<# feature args> [<arg>]*]
  991. */
  992. static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  993. {
  994. struct dm_snapshot *s;
  995. struct dm_arg_set as;
  996. int i;
  997. int r = -EINVAL;
  998. char *origin_path, *cow_path;
  999. unsigned int args_used, num_flush_bios = 1;
  1000. blk_mode_t origin_mode = BLK_OPEN_READ;
  1001. if (argc < 4) {
  1002. ti->error = "requires 4 or more arguments";
  1003. r = -EINVAL;
  1004. goto bad;
  1005. }
  1006. if (dm_target_is_snapshot_merge(ti)) {
  1007. num_flush_bios = 2;
  1008. origin_mode = BLK_OPEN_WRITE;
  1009. }
  1010. s = kzalloc_obj(*s);
  1011. if (!s) {
  1012. ti->error = "Cannot allocate private snapshot structure";
  1013. r = -ENOMEM;
  1014. goto bad;
  1015. }
  1016. as.argc = argc;
  1017. as.argv = argv;
  1018. dm_consume_args(&as, 4);
  1019. r = parse_snapshot_features(&as, s, ti);
  1020. if (r)
  1021. goto bad_features;
  1022. origin_path = argv[0];
  1023. argv++;
  1024. argc--;
  1025. r = dm_get_device(ti, origin_path, origin_mode, &s->origin);
  1026. if (r) {
  1027. ti->error = "Cannot get origin device";
  1028. goto bad_origin;
  1029. }
  1030. cow_path = argv[0];
  1031. argv++;
  1032. argc--;
  1033. r = dm_get_device(ti, cow_path, dm_table_get_mode(ti->table), &s->cow);
  1034. if (r) {
  1035. ti->error = "Cannot get COW device";
  1036. goto bad_cow;
  1037. }
  1038. if (s->cow->bdev && s->cow->bdev == s->origin->bdev) {
  1039. ti->error = "COW device cannot be the same as origin device";
  1040. r = -EINVAL;
  1041. goto bad_store;
  1042. }
  1043. r = dm_exception_store_create(ti, argc, argv, s, &args_used, &s->store);
  1044. if (r) {
  1045. ti->error = "Couldn't create exception store";
  1046. r = -EINVAL;
  1047. goto bad_store;
  1048. }
  1049. argv += args_used;
  1050. argc -= args_used;
  1051. s->ti = ti;
  1052. s->valid = 1;
  1053. s->snapshot_overflowed = 0;
  1054. s->active = 0;
  1055. atomic_set(&s->pending_exceptions_count, 0);
  1056. spin_lock_init(&s->pe_allocation_lock);
  1057. s->exception_start_sequence = 0;
  1058. s->exception_complete_sequence = 0;
  1059. s->out_of_order_tree = RB_ROOT;
  1060. init_rwsem(&s->lock);
  1061. INIT_LIST_HEAD(&s->list);
  1062. spin_lock_init(&s->pe_lock);
  1063. s->state_bits = 0;
  1064. s->merge_failed = false;
  1065. s->first_merging_chunk = 0;
  1066. s->num_merging_chunks = 0;
  1067. bio_list_init(&s->bios_queued_during_merge);
  1068. /* Allocate hash table for COW data */
  1069. if (init_hash_tables(s)) {
  1070. ti->error = "Unable to allocate hash table space";
  1071. r = -ENOMEM;
  1072. goto bad_hash_tables;
  1073. }
  1074. init_waitqueue_head(&s->in_progress_wait);
  1075. s->kcopyd_client = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  1076. if (IS_ERR(s->kcopyd_client)) {
  1077. r = PTR_ERR(s->kcopyd_client);
  1078. ti->error = "Could not create kcopyd client";
  1079. goto bad_kcopyd;
  1080. }
  1081. r = mempool_init_slab_pool(&s->pending_pool, MIN_IOS, pending_cache);
  1082. if (r) {
  1083. ti->error = "Could not allocate mempool for pending exceptions";
  1084. goto bad_pending_pool;
  1085. }
  1086. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  1087. INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
  1088. spin_lock_init(&s->tracked_chunk_lock);
  1089. ti->private = s;
  1090. ti->num_flush_bios = num_flush_bios;
  1091. if (s->discard_zeroes_cow)
  1092. ti->num_discard_bios = (s->discard_passdown_origin ? 2 : 1);
  1093. ti->per_io_data_size = sizeof(struct dm_snap_tracked_chunk);
  1094. /* Add snapshot to the list of snapshots for this origin */
  1095. /* Exceptions aren't triggered till snapshot_resume() is called */
  1096. r = register_snapshot(s);
  1097. if (r == -ENOMEM) {
  1098. ti->error = "Snapshot origin struct allocation failed";
  1099. goto bad_load_and_register;
  1100. } else if (r < 0) {
  1101. /* invalid handover, register_snapshot has set ti->error */
  1102. goto bad_load_and_register;
  1103. }
  1104. /*
  1105. * Metadata must only be loaded into one table at once, so skip this
  1106. * if metadata will be handed over during resume.
  1107. * Chunk size will be set during the handover - set it to zero to
  1108. * ensure it's ignored.
  1109. */
  1110. if (r > 0) {
  1111. s->store->chunk_size = 0;
  1112. return 0;
  1113. }
  1114. r = s->store->type->read_metadata(s->store, dm_add_exception,
  1115. (void *)s);
  1116. if (r < 0) {
  1117. ti->error = "Failed to read snapshot metadata";
  1118. goto bad_read_metadata;
  1119. } else if (r > 0) {
  1120. s->valid = 0;
  1121. DMWARN("Snapshot is marked invalid.");
  1122. }
  1123. if (!s->store->chunk_size) {
  1124. ti->error = "Chunk size not set";
  1125. r = -EINVAL;
  1126. goto bad_read_metadata;
  1127. }
  1128. r = dm_set_target_max_io_len(ti, s->store->chunk_size);
  1129. if (r)
  1130. goto bad_read_metadata;
  1131. return 0;
  1132. bad_read_metadata:
  1133. unregister_snapshot(s);
  1134. bad_load_and_register:
  1135. mempool_exit(&s->pending_pool);
  1136. bad_pending_pool:
  1137. dm_kcopyd_client_destroy(s->kcopyd_client);
  1138. bad_kcopyd:
  1139. dm_exception_table_exit(&s->pending, pending_cache);
  1140. dm_exception_table_exit(&s->complete, exception_cache);
  1141. bad_hash_tables:
  1142. dm_exception_store_destroy(s->store);
  1143. bad_store:
  1144. dm_put_device(ti, s->cow);
  1145. bad_cow:
  1146. dm_put_device(ti, s->origin);
  1147. bad_origin:
  1148. bad_features:
  1149. kfree(s);
  1150. bad:
  1151. return r;
  1152. }
  1153. static void __free_exceptions(struct dm_snapshot *s)
  1154. {
  1155. dm_kcopyd_client_destroy(s->kcopyd_client);
  1156. s->kcopyd_client = NULL;
  1157. dm_exception_table_exit(&s->pending, pending_cache);
  1158. dm_exception_table_exit(&s->complete, exception_cache);
  1159. }
  1160. static void __handover_exceptions(struct dm_snapshot *snap_src,
  1161. struct dm_snapshot *snap_dest)
  1162. {
  1163. union {
  1164. struct dm_exception_table table_swap;
  1165. struct dm_exception_store *store_swap;
  1166. } u;
  1167. /*
  1168. * Swap all snapshot context information between the two instances.
  1169. */
  1170. u.table_swap = snap_dest->complete;
  1171. snap_dest->complete = snap_src->complete;
  1172. snap_src->complete = u.table_swap;
  1173. u.store_swap = snap_dest->store;
  1174. snap_dest->store = snap_src->store;
  1175. snap_dest->store->userspace_supports_overflow = u.store_swap->userspace_supports_overflow;
  1176. snap_src->store = u.store_swap;
  1177. snap_dest->store->snap = snap_dest;
  1178. snap_src->store->snap = snap_src;
  1179. snap_dest->ti->max_io_len = snap_dest->store->chunk_size;
  1180. snap_dest->valid = snap_src->valid;
  1181. snap_dest->snapshot_overflowed = snap_src->snapshot_overflowed;
  1182. /*
  1183. * Set source invalid to ensure it receives no further I/O.
  1184. */
  1185. snap_src->valid = 0;
  1186. }
  1187. static void snapshot_dtr(struct dm_target *ti)
  1188. {
  1189. #ifdef CONFIG_DM_DEBUG
  1190. int i;
  1191. #endif
  1192. struct dm_snapshot *s = ti->private;
  1193. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
  1194. down_read(&_origins_lock);
  1195. /* Check whether exception handover must be cancelled */
  1196. (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
  1197. if (snap_src && snap_dest && (s == snap_src)) {
  1198. down_write(&snap_dest->lock);
  1199. snap_dest->valid = 0;
  1200. up_write(&snap_dest->lock);
  1201. DMERR("Cancelling snapshot handover.");
  1202. }
  1203. up_read(&_origins_lock);
  1204. if (dm_target_is_snapshot_merge(ti))
  1205. stop_merge(s);
  1206. /* Prevent further origin writes from using this snapshot. */
  1207. /* After this returns there can be no new kcopyd jobs. */
  1208. unregister_snapshot(s);
  1209. while (atomic_read(&s->pending_exceptions_count))
  1210. fsleep(1000);
  1211. /*
  1212. * Ensure instructions in mempool_exit aren't reordered
  1213. * before atomic_read.
  1214. */
  1215. smp_mb();
  1216. #ifdef CONFIG_DM_DEBUG
  1217. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  1218. BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
  1219. #endif
  1220. __free_exceptions(s);
  1221. mempool_exit(&s->pending_pool);
  1222. dm_exception_store_destroy(s->store);
  1223. dm_put_device(ti, s->cow);
  1224. dm_put_device(ti, s->origin);
  1225. WARN_ON(s->in_progress);
  1226. kfree(s);
  1227. }
  1228. static void account_start_copy(struct dm_snapshot *s)
  1229. {
  1230. spin_lock(&s->in_progress_wait.lock);
  1231. s->in_progress++;
  1232. spin_unlock(&s->in_progress_wait.lock);
  1233. }
  1234. static void account_end_copy(struct dm_snapshot *s)
  1235. {
  1236. spin_lock(&s->in_progress_wait.lock);
  1237. BUG_ON(!s->in_progress);
  1238. s->in_progress--;
  1239. if (likely(s->in_progress <= cow_threshold) &&
  1240. unlikely(waitqueue_active(&s->in_progress_wait)))
  1241. wake_up_locked(&s->in_progress_wait);
  1242. spin_unlock(&s->in_progress_wait.lock);
  1243. }
  1244. static bool wait_for_in_progress(struct dm_snapshot *s, bool unlock_origins)
  1245. {
  1246. if (unlikely(s->in_progress > cow_threshold)) {
  1247. spin_lock(&s->in_progress_wait.lock);
  1248. if (likely(s->in_progress > cow_threshold)) {
  1249. /*
  1250. * NOTE: this throttle doesn't account for whether
  1251. * the caller is servicing an IO that will trigger a COW
  1252. * so excess throttling may result for chunks not required
  1253. * to be COW'd. But if cow_threshold was reached, extra
  1254. * throttling is unlikely to negatively impact performance.
  1255. */
  1256. DECLARE_WAITQUEUE(wait, current);
  1257. __add_wait_queue(&s->in_progress_wait, &wait);
  1258. __set_current_state(TASK_UNINTERRUPTIBLE);
  1259. spin_unlock(&s->in_progress_wait.lock);
  1260. if (unlock_origins)
  1261. up_read(&_origins_lock);
  1262. io_schedule();
  1263. remove_wait_queue(&s->in_progress_wait, &wait);
  1264. return false;
  1265. }
  1266. spin_unlock(&s->in_progress_wait.lock);
  1267. }
  1268. return true;
  1269. }
  1270. /*
  1271. * Flush a list of buffers.
  1272. */
  1273. static void flush_bios(struct bio *bio)
  1274. {
  1275. struct bio *n;
  1276. while (bio) {
  1277. n = bio->bi_next;
  1278. bio->bi_next = NULL;
  1279. submit_bio_noacct(bio);
  1280. bio = n;
  1281. }
  1282. }
  1283. static int do_origin(struct dm_dev *origin, struct bio *bio, bool limit);
  1284. /*
  1285. * Flush a list of buffers.
  1286. */
  1287. static void retry_origin_bios(struct dm_snapshot *s, struct bio *bio)
  1288. {
  1289. struct bio *n;
  1290. int r;
  1291. while (bio) {
  1292. n = bio->bi_next;
  1293. bio->bi_next = NULL;
  1294. r = do_origin(s->origin, bio, false);
  1295. if (r == DM_MAPIO_REMAPPED)
  1296. submit_bio_noacct(bio);
  1297. bio = n;
  1298. }
  1299. }
  1300. /*
  1301. * Error a list of buffers.
  1302. */
  1303. static void error_bios(struct bio *bio)
  1304. {
  1305. struct bio *n;
  1306. while (bio) {
  1307. n = bio->bi_next;
  1308. bio->bi_next = NULL;
  1309. bio_io_error(bio);
  1310. bio = n;
  1311. }
  1312. }
  1313. static void __invalidate_snapshot(struct dm_snapshot *s, int err)
  1314. {
  1315. if (!s->valid)
  1316. return;
  1317. if (err == -EIO)
  1318. DMERR("Invalidating snapshot: Error reading/writing.");
  1319. else if (err == -ENOMEM)
  1320. DMERR("Invalidating snapshot: Unable to allocate exception.");
  1321. if (s->store->type->drop_snapshot)
  1322. s->store->type->drop_snapshot(s->store);
  1323. s->valid = 0;
  1324. dm_table_event(s->ti->table);
  1325. }
  1326. static void invalidate_snapshot(struct dm_snapshot *s, int err)
  1327. {
  1328. down_write(&s->lock);
  1329. __invalidate_snapshot(s, err);
  1330. up_write(&s->lock);
  1331. }
  1332. static void pending_complete(void *context, int success)
  1333. {
  1334. struct dm_snap_pending_exception *pe = context;
  1335. struct dm_exception *e;
  1336. struct dm_snapshot *s = pe->snap;
  1337. struct bio *origin_bios = NULL;
  1338. struct bio *snapshot_bios = NULL;
  1339. struct bio *full_bio = NULL;
  1340. struct dm_exception_table_lock lock;
  1341. int error = 0;
  1342. dm_exception_table_lock_init(s, pe->e.old_chunk, &lock);
  1343. if (!success) {
  1344. /* Read/write error - snapshot is unusable */
  1345. invalidate_snapshot(s, -EIO);
  1346. error = 1;
  1347. dm_exception_table_lock(&lock);
  1348. goto out;
  1349. }
  1350. e = alloc_completed_exception(GFP_NOIO);
  1351. if (!e) {
  1352. invalidate_snapshot(s, -ENOMEM);
  1353. error = 1;
  1354. dm_exception_table_lock(&lock);
  1355. goto out;
  1356. }
  1357. *e = pe->e;
  1358. down_read(&s->lock);
  1359. dm_exception_table_lock(&lock);
  1360. if (!s->valid) {
  1361. up_read(&s->lock);
  1362. free_completed_exception(e);
  1363. error = 1;
  1364. goto out;
  1365. }
  1366. /*
  1367. * Add a proper exception. After inserting the completed exception all
  1368. * subsequent snapshot reads to this chunk will be redirected to the
  1369. * COW device. This ensures that we do not starve. Moreover, as long
  1370. * as the pending exception exists, neither origin writes nor snapshot
  1371. * merging can overwrite the chunk in origin.
  1372. */
  1373. dm_insert_exception(&s->complete, e);
  1374. up_read(&s->lock);
  1375. /* Wait for conflicting reads to drain */
  1376. if (__chunk_is_tracked(s, pe->e.old_chunk)) {
  1377. dm_exception_table_unlock(&lock);
  1378. __check_for_conflicting_io(s, pe->e.old_chunk);
  1379. dm_exception_table_lock(&lock);
  1380. }
  1381. out:
  1382. /* Remove the in-flight exception from the list */
  1383. dm_remove_exception(&pe->e);
  1384. dm_exception_table_unlock(&lock);
  1385. snapshot_bios = bio_list_get(&pe->snapshot_bios);
  1386. origin_bios = bio_list_get(&pe->origin_bios);
  1387. full_bio = pe->full_bio;
  1388. if (full_bio)
  1389. full_bio->bi_end_io = pe->full_bio_end_io;
  1390. increment_pending_exceptions_done_count();
  1391. /* Submit any pending write bios */
  1392. if (error) {
  1393. if (full_bio)
  1394. bio_io_error(full_bio);
  1395. error_bios(snapshot_bios);
  1396. } else {
  1397. if (full_bio)
  1398. bio_endio(full_bio);
  1399. flush_bios(snapshot_bios);
  1400. }
  1401. retry_origin_bios(s, origin_bios);
  1402. free_pending_exception(pe);
  1403. }
  1404. static void complete_exception(struct dm_snap_pending_exception *pe)
  1405. {
  1406. struct dm_snapshot *s = pe->snap;
  1407. /* Update the metadata if we are persistent */
  1408. s->store->type->commit_exception(s->store, &pe->e, !pe->copy_error,
  1409. pending_complete, pe);
  1410. }
  1411. /*
  1412. * Called when the copy I/O has finished. kcopyd actually runs
  1413. * this code so don't block.
  1414. */
  1415. static void copy_callback(int read_err, unsigned long write_err, void *context)
  1416. {
  1417. struct dm_snap_pending_exception *pe = context;
  1418. struct dm_snapshot *s = pe->snap;
  1419. pe->copy_error = read_err || write_err;
  1420. if (pe->exception_sequence == s->exception_complete_sequence) {
  1421. struct rb_node *next;
  1422. s->exception_complete_sequence++;
  1423. complete_exception(pe);
  1424. next = rb_first(&s->out_of_order_tree);
  1425. while (next) {
  1426. pe = rb_entry(next, struct dm_snap_pending_exception,
  1427. out_of_order_node);
  1428. if (pe->exception_sequence != s->exception_complete_sequence)
  1429. break;
  1430. next = rb_next(next);
  1431. s->exception_complete_sequence++;
  1432. rb_erase(&pe->out_of_order_node, &s->out_of_order_tree);
  1433. complete_exception(pe);
  1434. cond_resched();
  1435. }
  1436. } else {
  1437. struct rb_node *parent = NULL;
  1438. struct rb_node **p = &s->out_of_order_tree.rb_node;
  1439. struct dm_snap_pending_exception *pe2;
  1440. while (*p) {
  1441. pe2 = rb_entry(*p, struct dm_snap_pending_exception, out_of_order_node);
  1442. parent = *p;
  1443. BUG_ON(pe->exception_sequence == pe2->exception_sequence);
  1444. if (pe->exception_sequence < pe2->exception_sequence)
  1445. p = &((*p)->rb_left);
  1446. else
  1447. p = &((*p)->rb_right);
  1448. }
  1449. rb_link_node(&pe->out_of_order_node, parent, p);
  1450. rb_insert_color(&pe->out_of_order_node, &s->out_of_order_tree);
  1451. }
  1452. account_end_copy(s);
  1453. }
  1454. /*
  1455. * Dispatches the copy operation to kcopyd.
  1456. */
  1457. static void start_copy(struct dm_snap_pending_exception *pe)
  1458. {
  1459. struct dm_snapshot *s = pe->snap;
  1460. struct dm_io_region src, dest;
  1461. struct block_device *bdev = s->origin->bdev;
  1462. sector_t dev_size;
  1463. dev_size = get_dev_size(bdev);
  1464. src.bdev = bdev;
  1465. src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
  1466. src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
  1467. dest.bdev = s->cow->bdev;
  1468. dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
  1469. dest.count = src.count;
  1470. /* Hand over to kcopyd */
  1471. account_start_copy(s);
  1472. dm_kcopyd_copy(s->kcopyd_client, &src, 1, &dest, 0, copy_callback, pe);
  1473. }
  1474. static void full_bio_end_io(struct bio *bio)
  1475. {
  1476. void *callback_data = bio->bi_private;
  1477. dm_kcopyd_do_callback(callback_data, 0, bio->bi_status ? 1 : 0);
  1478. }
  1479. static void start_full_bio(struct dm_snap_pending_exception *pe,
  1480. struct bio *bio)
  1481. {
  1482. struct dm_snapshot *s = pe->snap;
  1483. void *callback_data;
  1484. pe->full_bio = bio;
  1485. pe->full_bio_end_io = bio->bi_end_io;
  1486. account_start_copy(s);
  1487. callback_data = dm_kcopyd_prepare_callback(s->kcopyd_client,
  1488. copy_callback, pe);
  1489. bio->bi_end_io = full_bio_end_io;
  1490. bio->bi_private = callback_data;
  1491. submit_bio_noacct(bio);
  1492. }
  1493. static struct dm_snap_pending_exception *
  1494. __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
  1495. {
  1496. struct dm_exception *e = dm_lookup_exception(&s->pending, chunk);
  1497. if (!e)
  1498. return NULL;
  1499. return container_of(e, struct dm_snap_pending_exception, e);
  1500. }
  1501. /*
  1502. * Inserts a pending exception into the pending table.
  1503. *
  1504. * NOTE: a write lock must be held on the chunk's pending exception table slot
  1505. * before calling this.
  1506. */
  1507. static struct dm_snap_pending_exception *
  1508. __insert_pending_exception(struct dm_snapshot *s,
  1509. struct dm_snap_pending_exception *pe, chunk_t chunk)
  1510. {
  1511. pe->e.old_chunk = chunk;
  1512. bio_list_init(&pe->origin_bios);
  1513. bio_list_init(&pe->snapshot_bios);
  1514. pe->started = 0;
  1515. pe->full_bio = NULL;
  1516. spin_lock(&s->pe_allocation_lock);
  1517. if (s->store->type->prepare_exception(s->store, &pe->e)) {
  1518. spin_unlock(&s->pe_allocation_lock);
  1519. free_pending_exception(pe);
  1520. return NULL;
  1521. }
  1522. pe->exception_sequence = s->exception_start_sequence++;
  1523. spin_unlock(&s->pe_allocation_lock);
  1524. dm_insert_exception(&s->pending, &pe->e);
  1525. return pe;
  1526. }
  1527. /*
  1528. * Looks to see if this snapshot already has a pending exception
  1529. * for this chunk, otherwise it allocates a new one and inserts
  1530. * it into the pending table.
  1531. *
  1532. * NOTE: a write lock must be held on the chunk's pending exception table slot
  1533. * before calling this.
  1534. */
  1535. static struct dm_snap_pending_exception *
  1536. __find_pending_exception(struct dm_snapshot *s,
  1537. struct dm_snap_pending_exception *pe, chunk_t chunk)
  1538. {
  1539. struct dm_snap_pending_exception *pe2;
  1540. pe2 = __lookup_pending_exception(s, chunk);
  1541. if (pe2) {
  1542. free_pending_exception(pe);
  1543. return pe2;
  1544. }
  1545. return __insert_pending_exception(s, pe, chunk);
  1546. }
  1547. static void remap_exception(struct dm_snapshot *s, struct dm_exception *e,
  1548. struct bio *bio, chunk_t chunk)
  1549. {
  1550. bio_set_dev(bio, s->cow->bdev);
  1551. bio->bi_iter.bi_sector =
  1552. chunk_to_sector(s->store, dm_chunk_number(e->new_chunk) +
  1553. (chunk - e->old_chunk)) +
  1554. (bio->bi_iter.bi_sector & s->store->chunk_mask);
  1555. }
  1556. static void zero_callback(int read_err, unsigned long write_err, void *context)
  1557. {
  1558. struct bio *bio = context;
  1559. struct dm_snapshot *s = bio->bi_private;
  1560. account_end_copy(s);
  1561. bio->bi_status = write_err ? BLK_STS_IOERR : 0;
  1562. bio_endio(bio);
  1563. }
  1564. static void zero_exception(struct dm_snapshot *s, struct dm_exception *e,
  1565. struct bio *bio, chunk_t chunk)
  1566. {
  1567. struct dm_io_region dest;
  1568. dest.bdev = s->cow->bdev;
  1569. dest.sector = bio->bi_iter.bi_sector;
  1570. dest.count = s->store->chunk_size;
  1571. account_start_copy(s);
  1572. WARN_ON_ONCE(bio->bi_private);
  1573. bio->bi_private = s;
  1574. dm_kcopyd_zero(s->kcopyd_client, 1, &dest, 0, zero_callback, bio);
  1575. }
  1576. static bool io_overlaps_chunk(struct dm_snapshot *s, struct bio *bio)
  1577. {
  1578. return bio->bi_iter.bi_size ==
  1579. (s->store->chunk_size << SECTOR_SHIFT);
  1580. }
  1581. static int snapshot_map(struct dm_target *ti, struct bio *bio)
  1582. {
  1583. struct dm_exception *e;
  1584. struct dm_snapshot *s = ti->private;
  1585. int r = DM_MAPIO_REMAPPED;
  1586. chunk_t chunk;
  1587. struct dm_snap_pending_exception *pe = NULL;
  1588. struct dm_exception_table_lock lock;
  1589. init_tracked_chunk(bio);
  1590. if (bio->bi_opf & REQ_PREFLUSH) {
  1591. bio_set_dev(bio, s->cow->bdev);
  1592. return DM_MAPIO_REMAPPED;
  1593. }
  1594. chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
  1595. dm_exception_table_lock_init(s, chunk, &lock);
  1596. /* Full snapshots are not usable */
  1597. /* To get here the table must be live so s->active is always set. */
  1598. if (!s->valid)
  1599. return DM_MAPIO_KILL;
  1600. if (bio_data_dir(bio) == WRITE) {
  1601. while (unlikely(!wait_for_in_progress(s, false)))
  1602. ; /* wait_for_in_progress() has slept */
  1603. }
  1604. down_read(&s->lock);
  1605. dm_exception_table_lock(&lock);
  1606. if (!s->valid || (unlikely(s->snapshot_overflowed) &&
  1607. bio_data_dir(bio) == WRITE)) {
  1608. r = DM_MAPIO_KILL;
  1609. goto out_unlock;
  1610. }
  1611. if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
  1612. if (s->discard_passdown_origin && dm_bio_get_target_bio_nr(bio)) {
  1613. /*
  1614. * passdown discard to origin (without triggering
  1615. * snapshot exceptions via do_origin; doing so would
  1616. * defeat the goal of freeing space in origin that is
  1617. * implied by the "discard_passdown_origin" feature)
  1618. */
  1619. bio_set_dev(bio, s->origin->bdev);
  1620. track_chunk(s, bio, chunk);
  1621. goto out_unlock;
  1622. }
  1623. /* discard to snapshot (target_bio_nr == 0) zeroes exceptions */
  1624. }
  1625. /* If the block is already remapped - use that, else remap it */
  1626. e = dm_lookup_exception(&s->complete, chunk);
  1627. if (e) {
  1628. remap_exception(s, e, bio, chunk);
  1629. if (unlikely(bio_op(bio) == REQ_OP_DISCARD) &&
  1630. io_overlaps_chunk(s, bio)) {
  1631. dm_exception_table_unlock(&lock);
  1632. up_read(&s->lock);
  1633. zero_exception(s, e, bio, chunk);
  1634. r = DM_MAPIO_SUBMITTED; /* discard is not issued */
  1635. goto out;
  1636. }
  1637. goto out_unlock;
  1638. }
  1639. if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
  1640. /*
  1641. * If no exception exists, complete discard immediately
  1642. * otherwise it'll trigger copy-out.
  1643. */
  1644. bio_endio(bio);
  1645. r = DM_MAPIO_SUBMITTED;
  1646. goto out_unlock;
  1647. }
  1648. /*
  1649. * Write to snapshot - higher level takes care of RW/RO
  1650. * flags so we should only get this if we are
  1651. * writable.
  1652. */
  1653. if (bio_data_dir(bio) == WRITE) {
  1654. pe = __lookup_pending_exception(s, chunk);
  1655. if (!pe) {
  1656. dm_exception_table_unlock(&lock);
  1657. pe = alloc_pending_exception(s);
  1658. dm_exception_table_lock(&lock);
  1659. e = dm_lookup_exception(&s->complete, chunk);
  1660. if (e) {
  1661. free_pending_exception(pe);
  1662. remap_exception(s, e, bio, chunk);
  1663. goto out_unlock;
  1664. }
  1665. pe = __find_pending_exception(s, pe, chunk);
  1666. if (!pe) {
  1667. dm_exception_table_unlock(&lock);
  1668. up_read(&s->lock);
  1669. down_write(&s->lock);
  1670. if (s->store->userspace_supports_overflow) {
  1671. if (s->valid && !s->snapshot_overflowed) {
  1672. s->snapshot_overflowed = 1;
  1673. DMERR("Snapshot overflowed: Unable to allocate exception.");
  1674. }
  1675. } else
  1676. __invalidate_snapshot(s, -ENOMEM);
  1677. up_write(&s->lock);
  1678. r = DM_MAPIO_KILL;
  1679. goto out;
  1680. }
  1681. }
  1682. remap_exception(s, &pe->e, bio, chunk);
  1683. r = DM_MAPIO_SUBMITTED;
  1684. if (!pe->started && io_overlaps_chunk(s, bio)) {
  1685. pe->started = 1;
  1686. dm_exception_table_unlock(&lock);
  1687. up_read(&s->lock);
  1688. start_full_bio(pe, bio);
  1689. goto out;
  1690. }
  1691. bio_list_add(&pe->snapshot_bios, bio);
  1692. if (!pe->started) {
  1693. /* this is protected by the exception table lock */
  1694. pe->started = 1;
  1695. dm_exception_table_unlock(&lock);
  1696. up_read(&s->lock);
  1697. start_copy(pe);
  1698. goto out;
  1699. }
  1700. } else {
  1701. bio_set_dev(bio, s->origin->bdev);
  1702. track_chunk(s, bio, chunk);
  1703. }
  1704. out_unlock:
  1705. dm_exception_table_unlock(&lock);
  1706. up_read(&s->lock);
  1707. out:
  1708. return r;
  1709. }
  1710. /*
  1711. * A snapshot-merge target behaves like a combination of a snapshot
  1712. * target and a snapshot-origin target. It only generates new
  1713. * exceptions in other snapshots and not in the one that is being
  1714. * merged.
  1715. *
  1716. * For each chunk, if there is an existing exception, it is used to
  1717. * redirect I/O to the cow device. Otherwise I/O is sent to the origin,
  1718. * which in turn might generate exceptions in other snapshots.
  1719. * If merging is currently taking place on the chunk in question, the
  1720. * I/O is deferred by adding it to s->bios_queued_during_merge.
  1721. */
  1722. static int snapshot_merge_map(struct dm_target *ti, struct bio *bio)
  1723. {
  1724. struct dm_exception *e;
  1725. struct dm_snapshot *s = ti->private;
  1726. int r = DM_MAPIO_REMAPPED;
  1727. chunk_t chunk;
  1728. init_tracked_chunk(bio);
  1729. if (bio->bi_opf & REQ_PREFLUSH) {
  1730. if (!dm_bio_get_target_bio_nr(bio))
  1731. bio_set_dev(bio, s->origin->bdev);
  1732. else
  1733. bio_set_dev(bio, s->cow->bdev);
  1734. return DM_MAPIO_REMAPPED;
  1735. }
  1736. if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
  1737. /* Once merging, discards no longer effect change */
  1738. bio_endio(bio);
  1739. return DM_MAPIO_SUBMITTED;
  1740. }
  1741. chunk = sector_to_chunk(s->store, bio->bi_iter.bi_sector);
  1742. down_write(&s->lock);
  1743. /* Full merging snapshots are redirected to the origin */
  1744. if (!s->valid)
  1745. goto redirect_to_origin;
  1746. /* If the block is already remapped - use that */
  1747. e = dm_lookup_exception(&s->complete, chunk);
  1748. if (e) {
  1749. /* Queue writes overlapping with chunks being merged */
  1750. if (bio_data_dir(bio) == WRITE &&
  1751. chunk >= s->first_merging_chunk &&
  1752. chunk < (s->first_merging_chunk +
  1753. s->num_merging_chunks)) {
  1754. bio_set_dev(bio, s->origin->bdev);
  1755. bio_list_add(&s->bios_queued_during_merge, bio);
  1756. r = DM_MAPIO_SUBMITTED;
  1757. goto out_unlock;
  1758. }
  1759. remap_exception(s, e, bio, chunk);
  1760. if (bio_data_dir(bio) == WRITE)
  1761. track_chunk(s, bio, chunk);
  1762. goto out_unlock;
  1763. }
  1764. redirect_to_origin:
  1765. bio_set_dev(bio, s->origin->bdev);
  1766. if (bio_data_dir(bio) == WRITE) {
  1767. up_write(&s->lock);
  1768. return do_origin(s->origin, bio, false);
  1769. }
  1770. out_unlock:
  1771. up_write(&s->lock);
  1772. return r;
  1773. }
  1774. static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
  1775. blk_status_t *error)
  1776. {
  1777. struct dm_snapshot *s = ti->private;
  1778. if (is_bio_tracked(bio))
  1779. stop_tracking_chunk(s, bio);
  1780. return DM_ENDIO_DONE;
  1781. }
  1782. static void snapshot_merge_presuspend(struct dm_target *ti)
  1783. {
  1784. struct dm_snapshot *s = ti->private;
  1785. stop_merge(s);
  1786. }
  1787. static int snapshot_preresume(struct dm_target *ti)
  1788. {
  1789. int r = 0;
  1790. struct dm_snapshot *s = ti->private;
  1791. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
  1792. down_read(&_origins_lock);
  1793. (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
  1794. if (snap_src && snap_dest) {
  1795. down_read(&snap_src->lock);
  1796. if (s == snap_src) {
  1797. DMERR("Unable to resume snapshot source until handover completes.");
  1798. r = -EINVAL;
  1799. } else if (!dm_suspended(snap_src->ti)) {
  1800. DMERR("Unable to perform snapshot handover until source is suspended.");
  1801. r = -EINVAL;
  1802. }
  1803. up_read(&snap_src->lock);
  1804. }
  1805. up_read(&_origins_lock);
  1806. return r;
  1807. }
  1808. static void snapshot_resume(struct dm_target *ti)
  1809. {
  1810. struct dm_snapshot *s = ti->private;
  1811. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL, *snap_merging = NULL;
  1812. struct dm_origin *o;
  1813. struct mapped_device *origin_md = NULL;
  1814. bool must_restart_merging = false;
  1815. down_read(&_origins_lock);
  1816. o = __lookup_dm_origin(s->origin->bdev);
  1817. if (o)
  1818. origin_md = dm_table_get_md(o->ti->table);
  1819. if (!origin_md) {
  1820. (void) __find_snapshots_sharing_cow(s, NULL, NULL, &snap_merging);
  1821. if (snap_merging)
  1822. origin_md = dm_table_get_md(snap_merging->ti->table);
  1823. }
  1824. if (origin_md == dm_table_get_md(ti->table))
  1825. origin_md = NULL;
  1826. if (origin_md) {
  1827. if (dm_hold(origin_md))
  1828. origin_md = NULL;
  1829. }
  1830. up_read(&_origins_lock);
  1831. if (origin_md) {
  1832. dm_internal_suspend_fast(origin_md);
  1833. if (snap_merging && test_bit(RUNNING_MERGE, &snap_merging->state_bits)) {
  1834. must_restart_merging = true;
  1835. stop_merge(snap_merging);
  1836. }
  1837. }
  1838. down_read(&_origins_lock);
  1839. (void) __find_snapshots_sharing_cow(s, &snap_src, &snap_dest, NULL);
  1840. if (snap_src && snap_dest) {
  1841. down_write(&snap_src->lock);
  1842. down_write_nested(&snap_dest->lock, SINGLE_DEPTH_NESTING);
  1843. __handover_exceptions(snap_src, snap_dest);
  1844. up_write(&snap_dest->lock);
  1845. up_write(&snap_src->lock);
  1846. }
  1847. up_read(&_origins_lock);
  1848. if (origin_md) {
  1849. if (must_restart_merging)
  1850. start_merge(snap_merging);
  1851. dm_internal_resume_fast(origin_md);
  1852. dm_put(origin_md);
  1853. }
  1854. /* Now we have correct chunk size, reregister */
  1855. reregister_snapshot(s);
  1856. down_write(&s->lock);
  1857. s->active = 1;
  1858. up_write(&s->lock);
  1859. }
  1860. static uint32_t get_origin_minimum_chunksize(struct block_device *bdev)
  1861. {
  1862. uint32_t min_chunksize;
  1863. down_read(&_origins_lock);
  1864. min_chunksize = __minimum_chunk_size(__lookup_origin(bdev));
  1865. up_read(&_origins_lock);
  1866. return min_chunksize;
  1867. }
  1868. static void snapshot_merge_resume(struct dm_target *ti)
  1869. {
  1870. struct dm_snapshot *s = ti->private;
  1871. /*
  1872. * Handover exceptions from existing snapshot.
  1873. */
  1874. snapshot_resume(ti);
  1875. /*
  1876. * snapshot-merge acts as an origin, so set ti->max_io_len
  1877. */
  1878. ti->max_io_len = get_origin_minimum_chunksize(s->origin->bdev);
  1879. start_merge(s);
  1880. }
  1881. static void snapshot_status(struct dm_target *ti, status_type_t type,
  1882. unsigned int status_flags, char *result, unsigned int maxlen)
  1883. {
  1884. unsigned int sz = 0;
  1885. struct dm_snapshot *snap = ti->private;
  1886. unsigned int num_features;
  1887. switch (type) {
  1888. case STATUSTYPE_INFO:
  1889. down_write(&snap->lock);
  1890. if (!snap->valid)
  1891. DMEMIT("Invalid");
  1892. else if (snap->merge_failed)
  1893. DMEMIT("Merge failed");
  1894. else if (snap->snapshot_overflowed)
  1895. DMEMIT("Overflow");
  1896. else {
  1897. if (snap->store->type->usage) {
  1898. sector_t total_sectors, sectors_allocated,
  1899. metadata_sectors;
  1900. snap->store->type->usage(snap->store,
  1901. &total_sectors,
  1902. &sectors_allocated,
  1903. &metadata_sectors);
  1904. DMEMIT("%llu/%llu %llu",
  1905. (unsigned long long)sectors_allocated,
  1906. (unsigned long long)total_sectors,
  1907. (unsigned long long)metadata_sectors);
  1908. } else
  1909. DMEMIT("Unknown");
  1910. }
  1911. up_write(&snap->lock);
  1912. break;
  1913. case STATUSTYPE_TABLE:
  1914. /*
  1915. * kdevname returns a static pointer so we need
  1916. * to make private copies if the output is to
  1917. * make sense.
  1918. */
  1919. DMEMIT("%s %s", snap->origin->name, snap->cow->name);
  1920. sz += snap->store->type->status(snap->store, type, result + sz,
  1921. maxlen - sz);
  1922. num_features = snap->discard_zeroes_cow + snap->discard_passdown_origin;
  1923. if (num_features) {
  1924. DMEMIT(" %u", num_features);
  1925. if (snap->discard_zeroes_cow)
  1926. DMEMIT(" discard_zeroes_cow");
  1927. if (snap->discard_passdown_origin)
  1928. DMEMIT(" discard_passdown_origin");
  1929. }
  1930. break;
  1931. case STATUSTYPE_IMA:
  1932. DMEMIT_TARGET_NAME_VERSION(ti->type);
  1933. DMEMIT(",snap_origin_name=%s", snap->origin->name);
  1934. DMEMIT(",snap_cow_name=%s", snap->cow->name);
  1935. DMEMIT(",snap_valid=%c", snap->valid ? 'y' : 'n');
  1936. DMEMIT(",snap_merge_failed=%c", snap->merge_failed ? 'y' : 'n');
  1937. DMEMIT(",snapshot_overflowed=%c", snap->snapshot_overflowed ? 'y' : 'n');
  1938. DMEMIT(";");
  1939. break;
  1940. }
  1941. }
  1942. static int snapshot_iterate_devices(struct dm_target *ti,
  1943. iterate_devices_callout_fn fn, void *data)
  1944. {
  1945. struct dm_snapshot *snap = ti->private;
  1946. int r;
  1947. r = fn(ti, snap->origin, 0, ti->len, data);
  1948. if (!r)
  1949. r = fn(ti, snap->cow, 0, get_dev_size(snap->cow->bdev), data);
  1950. return r;
  1951. }
  1952. static void snapshot_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1953. {
  1954. struct dm_snapshot *snap = ti->private;
  1955. if (snap->discard_zeroes_cow) {
  1956. struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
  1957. down_read(&_origins_lock);
  1958. (void) __find_snapshots_sharing_cow(snap, &snap_src, &snap_dest, NULL);
  1959. if (snap_src && snap_dest)
  1960. snap = snap_src;
  1961. /* All discards are split on chunk_size boundary */
  1962. limits->discard_granularity = snap->store->chunk_size;
  1963. limits->max_hw_discard_sectors = snap->store->chunk_size;
  1964. up_read(&_origins_lock);
  1965. }
  1966. }
  1967. /*
  1968. *---------------------------------------------------------------
  1969. * Origin methods
  1970. *---------------------------------------------------------------
  1971. */
  1972. /*
  1973. * If no exceptions need creating, DM_MAPIO_REMAPPED is returned and any
  1974. * supplied bio was ignored. The caller may submit it immediately.
  1975. * (No remapping actually occurs as the origin is always a direct linear
  1976. * map.)
  1977. *
  1978. * If further exceptions are required, DM_MAPIO_SUBMITTED is returned
  1979. * and any supplied bio is added to a list to be submitted once all
  1980. * the necessary exceptions exist.
  1981. */
  1982. static int __origin_write(struct list_head *snapshots, sector_t sector,
  1983. struct bio *bio)
  1984. {
  1985. int r = DM_MAPIO_REMAPPED;
  1986. struct dm_snapshot *snap;
  1987. struct dm_exception *e;
  1988. struct dm_snap_pending_exception *pe, *pe2;
  1989. struct dm_snap_pending_exception *pe_to_start_now = NULL;
  1990. struct dm_snap_pending_exception *pe_to_start_last = NULL;
  1991. struct dm_exception_table_lock lock;
  1992. chunk_t chunk;
  1993. /* Do all the snapshots on this origin */
  1994. list_for_each_entry(snap, snapshots, list) {
  1995. /*
  1996. * Don't make new exceptions in a merging snapshot
  1997. * because it has effectively been deleted
  1998. */
  1999. if (dm_target_is_snapshot_merge(snap->ti))
  2000. continue;
  2001. /* Nothing to do if writing beyond end of snapshot */
  2002. if (sector >= dm_table_get_size(snap->ti->table))
  2003. continue;
  2004. /*
  2005. * Remember, different snapshots can have
  2006. * different chunk sizes.
  2007. */
  2008. chunk = sector_to_chunk(snap->store, sector);
  2009. dm_exception_table_lock_init(snap, chunk, &lock);
  2010. down_read(&snap->lock);
  2011. dm_exception_table_lock(&lock);
  2012. /* Only deal with valid and active snapshots */
  2013. if (!snap->valid || !snap->active)
  2014. goto next_snapshot;
  2015. pe = __lookup_pending_exception(snap, chunk);
  2016. if (!pe) {
  2017. /*
  2018. * Check exception table to see if block is already
  2019. * remapped in this snapshot and trigger an exception
  2020. * if not.
  2021. */
  2022. e = dm_lookup_exception(&snap->complete, chunk);
  2023. if (e)
  2024. goto next_snapshot;
  2025. dm_exception_table_unlock(&lock);
  2026. pe = alloc_pending_exception(snap);
  2027. dm_exception_table_lock(&lock);
  2028. pe2 = __lookup_pending_exception(snap, chunk);
  2029. if (!pe2) {
  2030. e = dm_lookup_exception(&snap->complete, chunk);
  2031. if (e) {
  2032. free_pending_exception(pe);
  2033. goto next_snapshot;
  2034. }
  2035. pe = __insert_pending_exception(snap, pe, chunk);
  2036. if (!pe) {
  2037. dm_exception_table_unlock(&lock);
  2038. up_read(&snap->lock);
  2039. invalidate_snapshot(snap, -ENOMEM);
  2040. continue;
  2041. }
  2042. } else {
  2043. free_pending_exception(pe);
  2044. pe = pe2;
  2045. }
  2046. }
  2047. r = DM_MAPIO_SUBMITTED;
  2048. /*
  2049. * If an origin bio was supplied, queue it to wait for the
  2050. * completion of this exception, and start this one last,
  2051. * at the end of the function.
  2052. */
  2053. if (bio) {
  2054. bio_list_add(&pe->origin_bios, bio);
  2055. bio = NULL;
  2056. if (!pe->started) {
  2057. pe->started = 1;
  2058. pe_to_start_last = pe;
  2059. }
  2060. }
  2061. if (!pe->started) {
  2062. pe->started = 1;
  2063. pe_to_start_now = pe;
  2064. }
  2065. next_snapshot:
  2066. dm_exception_table_unlock(&lock);
  2067. up_read(&snap->lock);
  2068. if (pe_to_start_now) {
  2069. start_copy(pe_to_start_now);
  2070. pe_to_start_now = NULL;
  2071. }
  2072. }
  2073. /*
  2074. * Submit the exception against which the bio is queued last,
  2075. * to give the other exceptions a head start.
  2076. */
  2077. if (pe_to_start_last)
  2078. start_copy(pe_to_start_last);
  2079. return r;
  2080. }
  2081. /*
  2082. * Called on a write from the origin driver.
  2083. */
  2084. static int do_origin(struct dm_dev *origin, struct bio *bio, bool limit)
  2085. {
  2086. struct origin *o;
  2087. int r = DM_MAPIO_REMAPPED;
  2088. again:
  2089. down_read(&_origins_lock);
  2090. o = __lookup_origin(origin->bdev);
  2091. if (o) {
  2092. if (limit) {
  2093. struct dm_snapshot *s;
  2094. list_for_each_entry(s, &o->snapshots, list)
  2095. if (unlikely(!wait_for_in_progress(s, true)))
  2096. goto again;
  2097. }
  2098. r = __origin_write(&o->snapshots, bio->bi_iter.bi_sector, bio);
  2099. }
  2100. up_read(&_origins_lock);
  2101. return r;
  2102. }
  2103. /*
  2104. * Trigger exceptions in all non-merging snapshots.
  2105. *
  2106. * The chunk size of the merging snapshot may be larger than the chunk
  2107. * size of some other snapshot so we may need to reallocate multiple
  2108. * chunks in other snapshots.
  2109. *
  2110. * We scan all the overlapping exceptions in the other snapshots.
  2111. * Returns 1 if anything was reallocated and must be waited for,
  2112. * otherwise returns 0.
  2113. *
  2114. * size must be a multiple of merging_snap's chunk_size.
  2115. */
  2116. static int origin_write_extent(struct dm_snapshot *merging_snap,
  2117. sector_t sector, unsigned int size)
  2118. {
  2119. int must_wait = 0;
  2120. sector_t n;
  2121. struct origin *o;
  2122. /*
  2123. * The origin's __minimum_chunk_size() got stored in max_io_len
  2124. * by snapshot_merge_resume().
  2125. */
  2126. down_read(&_origins_lock);
  2127. o = __lookup_origin(merging_snap->origin->bdev);
  2128. for (n = 0; n < size; n += merging_snap->ti->max_io_len)
  2129. if (__origin_write(&o->snapshots, sector + n, NULL) ==
  2130. DM_MAPIO_SUBMITTED)
  2131. must_wait = 1;
  2132. up_read(&_origins_lock);
  2133. return must_wait;
  2134. }
  2135. /*
  2136. * Origin: maps a linear range of a device, with hooks for snapshotting.
  2137. */
  2138. /*
  2139. * Construct an origin mapping: <dev_path>
  2140. * The context for an origin is merely a 'struct dm_dev *'
  2141. * pointing to the real device.
  2142. */
  2143. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  2144. {
  2145. int r;
  2146. struct dm_origin *o;
  2147. if (argc != 1) {
  2148. ti->error = "origin: incorrect number of arguments";
  2149. return -EINVAL;
  2150. }
  2151. o = kmalloc_obj(struct dm_origin);
  2152. if (!o) {
  2153. ti->error = "Cannot allocate private origin structure";
  2154. r = -ENOMEM;
  2155. goto bad_alloc;
  2156. }
  2157. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &o->dev);
  2158. if (r) {
  2159. ti->error = "Cannot get target device";
  2160. goto bad_open;
  2161. }
  2162. o->ti = ti;
  2163. ti->private = o;
  2164. ti->num_flush_bios = 1;
  2165. return 0;
  2166. bad_open:
  2167. kfree(o);
  2168. bad_alloc:
  2169. return r;
  2170. }
  2171. static void origin_dtr(struct dm_target *ti)
  2172. {
  2173. struct dm_origin *o = ti->private;
  2174. dm_put_device(ti, o->dev);
  2175. kfree(o);
  2176. }
  2177. static int origin_map(struct dm_target *ti, struct bio *bio)
  2178. {
  2179. struct dm_origin *o = ti->private;
  2180. unsigned int available_sectors;
  2181. bio_set_dev(bio, o->dev->bdev);
  2182. if (unlikely(bio->bi_opf & REQ_PREFLUSH))
  2183. return DM_MAPIO_REMAPPED;
  2184. if (bio_data_dir(bio) != WRITE)
  2185. return DM_MAPIO_REMAPPED;
  2186. available_sectors = o->split_boundary -
  2187. ((unsigned int)bio->bi_iter.bi_sector & (o->split_boundary - 1));
  2188. if (bio_sectors(bio) > available_sectors)
  2189. dm_accept_partial_bio(bio, available_sectors);
  2190. /* Only tell snapshots if this is a write */
  2191. return do_origin(o->dev, bio, true);
  2192. }
  2193. /*
  2194. * Set the target "max_io_len" field to the minimum of all the snapshots'
  2195. * chunk sizes.
  2196. */
  2197. static void origin_resume(struct dm_target *ti)
  2198. {
  2199. struct dm_origin *o = ti->private;
  2200. o->split_boundary = get_origin_minimum_chunksize(o->dev->bdev);
  2201. down_write(&_origins_lock);
  2202. __insert_dm_origin(o);
  2203. up_write(&_origins_lock);
  2204. }
  2205. static void origin_postsuspend(struct dm_target *ti)
  2206. {
  2207. struct dm_origin *o = ti->private;
  2208. down_write(&_origins_lock);
  2209. __remove_dm_origin(o);
  2210. up_write(&_origins_lock);
  2211. }
  2212. static void origin_status(struct dm_target *ti, status_type_t type,
  2213. unsigned int status_flags, char *result, unsigned int maxlen)
  2214. {
  2215. struct dm_origin *o = ti->private;
  2216. switch (type) {
  2217. case STATUSTYPE_INFO:
  2218. result[0] = '\0';
  2219. break;
  2220. case STATUSTYPE_TABLE:
  2221. snprintf(result, maxlen, "%s", o->dev->name);
  2222. break;
  2223. case STATUSTYPE_IMA:
  2224. result[0] = '\0';
  2225. break;
  2226. }
  2227. }
  2228. static int origin_iterate_devices(struct dm_target *ti,
  2229. iterate_devices_callout_fn fn, void *data)
  2230. {
  2231. struct dm_origin *o = ti->private;
  2232. return fn(ti, o->dev, 0, ti->len, data);
  2233. }
  2234. static struct target_type origin_target = {
  2235. .name = "snapshot-origin",
  2236. .version = {1, 9, 0},
  2237. .module = THIS_MODULE,
  2238. .ctr = origin_ctr,
  2239. .dtr = origin_dtr,
  2240. .map = origin_map,
  2241. .resume = origin_resume,
  2242. .postsuspend = origin_postsuspend,
  2243. .status = origin_status,
  2244. .iterate_devices = origin_iterate_devices,
  2245. };
  2246. static struct target_type snapshot_target = {
  2247. .name = "snapshot",
  2248. .version = {1, 16, 0},
  2249. .module = THIS_MODULE,
  2250. .ctr = snapshot_ctr,
  2251. .dtr = snapshot_dtr,
  2252. .map = snapshot_map,
  2253. .end_io = snapshot_end_io,
  2254. .preresume = snapshot_preresume,
  2255. .resume = snapshot_resume,
  2256. .status = snapshot_status,
  2257. .iterate_devices = snapshot_iterate_devices,
  2258. .io_hints = snapshot_io_hints,
  2259. };
  2260. static struct target_type merge_target = {
  2261. .name = dm_snapshot_merge_target_name,
  2262. .version = {1, 5, 0},
  2263. .module = THIS_MODULE,
  2264. .ctr = snapshot_ctr,
  2265. .dtr = snapshot_dtr,
  2266. .map = snapshot_merge_map,
  2267. .end_io = snapshot_end_io,
  2268. .presuspend = snapshot_merge_presuspend,
  2269. .preresume = snapshot_preresume,
  2270. .resume = snapshot_merge_resume,
  2271. .status = snapshot_status,
  2272. .iterate_devices = snapshot_iterate_devices,
  2273. .io_hints = snapshot_io_hints,
  2274. };
  2275. static int __init dm_snapshot_init(void)
  2276. {
  2277. int r;
  2278. r = dm_exception_store_init();
  2279. if (r) {
  2280. DMERR("Failed to initialize exception stores");
  2281. return r;
  2282. }
  2283. r = init_origin_hash();
  2284. if (r) {
  2285. DMERR("init_origin_hash failed.");
  2286. goto bad_origin_hash;
  2287. }
  2288. exception_cache = KMEM_CACHE(dm_exception, 0);
  2289. if (!exception_cache) {
  2290. DMERR("Couldn't create exception cache.");
  2291. r = -ENOMEM;
  2292. goto bad_exception_cache;
  2293. }
  2294. pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
  2295. if (!pending_cache) {
  2296. DMERR("Couldn't create pending cache.");
  2297. r = -ENOMEM;
  2298. goto bad_pending_cache;
  2299. }
  2300. r = dm_register_target(&snapshot_target);
  2301. if (r < 0)
  2302. goto bad_register_snapshot_target;
  2303. r = dm_register_target(&origin_target);
  2304. if (r < 0)
  2305. goto bad_register_origin_target;
  2306. r = dm_register_target(&merge_target);
  2307. if (r < 0)
  2308. goto bad_register_merge_target;
  2309. return 0;
  2310. bad_register_merge_target:
  2311. dm_unregister_target(&origin_target);
  2312. bad_register_origin_target:
  2313. dm_unregister_target(&snapshot_target);
  2314. bad_register_snapshot_target:
  2315. kmem_cache_destroy(pending_cache);
  2316. bad_pending_cache:
  2317. kmem_cache_destroy(exception_cache);
  2318. bad_exception_cache:
  2319. exit_origin_hash();
  2320. bad_origin_hash:
  2321. dm_exception_store_exit();
  2322. return r;
  2323. }
  2324. static void __exit dm_snapshot_exit(void)
  2325. {
  2326. dm_unregister_target(&snapshot_target);
  2327. dm_unregister_target(&origin_target);
  2328. dm_unregister_target(&merge_target);
  2329. exit_origin_hash();
  2330. kmem_cache_destroy(pending_cache);
  2331. kmem_cache_destroy(exception_cache);
  2332. dm_exception_store_exit();
  2333. }
  2334. /* Module hooks */
  2335. module_init(dm_snapshot_init);
  2336. module_exit(dm_snapshot_exit);
  2337. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  2338. MODULE_AUTHOR("Joe Thornber");
  2339. MODULE_LICENSE("GPL");
  2340. MODULE_ALIAS("dm-snapshot-origin");
  2341. MODULE_ALIAS("dm-snapshot-merge");