Commit | Line | Data |
---|---|---|
963b61eb SW |
1 | #include "ceph_debug.h" |
2 | ||
3 | #include <linux/radix-tree.h> | |
4 | #include <linux/sort.h> | |
5 | ||
6 | #include "super.h" | |
7 | #include "decode.h" | |
8 | ||
9 | /* | |
10 | * Snapshots in ceph are driven in large part by cooperation from the | |
11 | * client. In contrast to local file systems or file servers that | |
12 | * implement snapshots at a single point in the system, ceph's | |
13 | * distributed access to storage requires clients to help decide | |
14 | * whether a write logically occurs before or after a recently created | |
15 | * snapshot. | |
16 | * | |
17 | * This provides a perfect instantanous client-wide snapshot. Between | |
18 | * clients, however, snapshots may appear to be applied at slightly | |
19 | * different points in time, depending on delays in delivering the | |
20 | * snapshot notification. | |
21 | * | |
22 | * Snapshots are _not_ file system-wide. Instead, each snapshot | |
23 | * applies to the subdirectory nested beneath some directory. This | |
24 | * effectively divides the hierarchy into multiple "realms," where all | |
25 | * of the files contained by each realm share the same set of | |
26 | * snapshots. An individual realm's snap set contains snapshots | |
27 | * explicitly created on that realm, as well as any snaps in its | |
28 | * parent's snap set _after_ the point at which the parent became it's | |
29 | * parent (due to, say, a rename). Similarly, snaps from prior parents | |
30 | * during the time intervals during which they were the parent are included. | |
31 | * | |
32 | * The client is spared most of this detail, fortunately... it must only | |
33 | * maintains a hierarchy of realms reflecting the current parent/child | |
34 | * realm relationship, and for each realm has an explicit list of snaps | |
35 | * inherited from prior parents. | |
36 | * | |
37 | * A snap_realm struct is maintained for realms containing every inode | |
38 | * with an open cap in the system. (The needed snap realm information is | |
39 | * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq' | |
40 | * version number is used to ensure that as realm parameters change (new | |
41 | * snapshot, new parent, etc.) the client's realm hierarchy is updated. | |
42 | * | |
43 | * The realm hierarchy drives the generation of a 'snap context' for each | |
44 | * realm, which simply lists the resulting set of snaps for the realm. This | |
45 | * is attached to any writes sent to OSDs. | |
46 | */ | |
47 | /* | |
48 | * Unfortunately error handling is a bit mixed here. If we get a snap | |
49 | * update, but don't have enough memory to update our realm hierarchy, | |
50 | * it's not clear what we can do about it (besides complaining to the | |
51 | * console). | |
52 | */ | |
53 | ||
54 | ||
55 | /* | |
56 | * increase ref count for the realm | |
57 | * | |
58 | * caller must hold snap_rwsem for write. | |
59 | */ | |
60 | void ceph_get_snap_realm(struct ceph_mds_client *mdsc, | |
61 | struct ceph_snap_realm *realm) | |
62 | { | |
63 | dout("get_realm %p %d -> %d\n", realm, | |
64 | atomic_read(&realm->nref), atomic_read(&realm->nref)+1); | |
65 | /* | |
66 | * since we _only_ increment realm refs or empty the empty | |
67 | * list with snap_rwsem held, adjusting the empty list here is | |
68 | * safe. we do need to protect against concurrent empty list | |
69 | * additions, however. | |
70 | */ | |
71 | if (atomic_read(&realm->nref) == 0) { | |
72 | spin_lock(&mdsc->snap_empty_lock); | |
73 | list_del_init(&realm->empty_item); | |
74 | spin_unlock(&mdsc->snap_empty_lock); | |
75 | } | |
76 | ||
77 | atomic_inc(&realm->nref); | |
78 | } | |
79 | ||
80 | /* | |
81 | * create and get the realm rooted at @ino and bump its ref count. | |
82 | * | |
83 | * caller must hold snap_rwsem for write. | |
84 | */ | |
85 | static struct ceph_snap_realm *ceph_create_snap_realm( | |
86 | struct ceph_mds_client *mdsc, | |
87 | u64 ino) | |
88 | { | |
89 | struct ceph_snap_realm *realm; | |
90 | ||
91 | realm = kzalloc(sizeof(*realm), GFP_NOFS); | |
92 | if (!realm) | |
93 | return ERR_PTR(-ENOMEM); | |
94 | ||
95 | radix_tree_insert(&mdsc->snap_realms, ino, realm); | |
96 | ||
97 | atomic_set(&realm->nref, 0); /* tree does not take a ref */ | |
98 | realm->ino = ino; | |
99 | INIT_LIST_HEAD(&realm->children); | |
100 | INIT_LIST_HEAD(&realm->child_item); | |
101 | INIT_LIST_HEAD(&realm->empty_item); | |
102 | INIT_LIST_HEAD(&realm->inodes_with_caps); | |
103 | spin_lock_init(&realm->inodes_with_caps_lock); | |
104 | dout("create_snap_realm %llx %p\n", realm->ino, realm); | |
105 | return realm; | |
106 | } | |
107 | ||
108 | /* | |
109 | * find and get (if found) the realm rooted at @ino and bump its ref count. | |
110 | * | |
111 | * caller must hold snap_rwsem for write. | |
112 | */ | |
113 | struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc, | |
114 | u64 ino) | |
115 | { | |
116 | struct ceph_snap_realm *realm; | |
117 | ||
118 | realm = radix_tree_lookup(&mdsc->snap_realms, ino); | |
119 | if (realm) | |
120 | dout("lookup_snap_realm %llx %p\n", realm->ino, realm); | |
121 | return realm; | |
122 | } | |
123 | ||
124 | static void __put_snap_realm(struct ceph_mds_client *mdsc, | |
125 | struct ceph_snap_realm *realm); | |
126 | ||
127 | /* | |
128 | * called with snap_rwsem (write) | |
129 | */ | |
130 | static void __destroy_snap_realm(struct ceph_mds_client *mdsc, | |
131 | struct ceph_snap_realm *realm) | |
132 | { | |
133 | dout("__destroy_snap_realm %p %llx\n", realm, realm->ino); | |
134 | ||
135 | radix_tree_delete(&mdsc->snap_realms, realm->ino); | |
136 | ||
137 | if (realm->parent) { | |
138 | list_del_init(&realm->child_item); | |
139 | __put_snap_realm(mdsc, realm->parent); | |
140 | } | |
141 | ||
142 | kfree(realm->prior_parent_snaps); | |
143 | kfree(realm->snaps); | |
144 | ceph_put_snap_context(realm->cached_context); | |
145 | kfree(realm); | |
146 | } | |
147 | ||
148 | /* | |
149 | * caller holds snap_rwsem (write) | |
150 | */ | |
151 | static void __put_snap_realm(struct ceph_mds_client *mdsc, | |
152 | struct ceph_snap_realm *realm) | |
153 | { | |
154 | dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, | |
155 | atomic_read(&realm->nref), atomic_read(&realm->nref)-1); | |
156 | if (atomic_dec_and_test(&realm->nref)) | |
157 | __destroy_snap_realm(mdsc, realm); | |
158 | } | |
159 | ||
160 | /* | |
161 | * caller needn't hold any locks | |
162 | */ | |
163 | void ceph_put_snap_realm(struct ceph_mds_client *mdsc, | |
164 | struct ceph_snap_realm *realm) | |
165 | { | |
166 | dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, | |
167 | atomic_read(&realm->nref), atomic_read(&realm->nref)-1); | |
168 | if (!atomic_dec_and_test(&realm->nref)) | |
169 | return; | |
170 | ||
171 | if (down_write_trylock(&mdsc->snap_rwsem)) { | |
172 | __destroy_snap_realm(mdsc, realm); | |
173 | up_write(&mdsc->snap_rwsem); | |
174 | } else { | |
175 | spin_lock(&mdsc->snap_empty_lock); | |
176 | list_add(&mdsc->snap_empty, &realm->empty_item); | |
177 | spin_unlock(&mdsc->snap_empty_lock); | |
178 | } | |
179 | } | |
180 | ||
181 | /* | |
182 | * Clean up any realms whose ref counts have dropped to zero. Note | |
183 | * that this does not include realms who were created but not yet | |
184 | * used. | |
185 | * | |
186 | * Called under snap_rwsem (write) | |
187 | */ | |
188 | static void __cleanup_empty_realms(struct ceph_mds_client *mdsc) | |
189 | { | |
190 | struct ceph_snap_realm *realm; | |
191 | ||
192 | spin_lock(&mdsc->snap_empty_lock); | |
193 | while (!list_empty(&mdsc->snap_empty)) { | |
194 | realm = list_first_entry(&mdsc->snap_empty, | |
195 | struct ceph_snap_realm, empty_item); | |
196 | list_del(&realm->empty_item); | |
197 | spin_unlock(&mdsc->snap_empty_lock); | |
198 | __destroy_snap_realm(mdsc, realm); | |
199 | spin_lock(&mdsc->snap_empty_lock); | |
200 | } | |
201 | spin_unlock(&mdsc->snap_empty_lock); | |
202 | } | |
203 | ||
204 | void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc) | |
205 | { | |
206 | down_write(&mdsc->snap_rwsem); | |
207 | __cleanup_empty_realms(mdsc); | |
208 | up_write(&mdsc->snap_rwsem); | |
209 | } | |
210 | ||
211 | /* | |
212 | * adjust the parent realm of a given @realm. adjust child list, and parent | |
213 | * pointers, and ref counts appropriately. | |
214 | * | |
215 | * return true if parent was changed, 0 if unchanged, <0 on error. | |
216 | * | |
217 | * caller must hold snap_rwsem for write. | |
218 | */ | |
219 | static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc, | |
220 | struct ceph_snap_realm *realm, | |
221 | u64 parentino) | |
222 | { | |
223 | struct ceph_snap_realm *parent; | |
224 | ||
225 | if (realm->parent_ino == parentino) | |
226 | return 0; | |
227 | ||
228 | parent = ceph_lookup_snap_realm(mdsc, parentino); | |
963b61eb SW |
229 | if (!parent) { |
230 | parent = ceph_create_snap_realm(mdsc, parentino); | |
231 | if (IS_ERR(parent)) | |
232 | return PTR_ERR(parent); | |
233 | } | |
234 | dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n", | |
235 | realm->ino, realm, realm->parent_ino, realm->parent, | |
236 | parentino, parent); | |
237 | if (realm->parent) { | |
238 | list_del_init(&realm->child_item); | |
239 | ceph_put_snap_realm(mdsc, realm->parent); | |
240 | } | |
241 | realm->parent_ino = parentino; | |
242 | realm->parent = parent; | |
243 | ceph_get_snap_realm(mdsc, parent); | |
244 | list_add(&realm->child_item, &parent->children); | |
245 | return 1; | |
246 | } | |
247 | ||
248 | ||
249 | static int cmpu64_rev(const void *a, const void *b) | |
250 | { | |
251 | if (*(u64 *)a < *(u64 *)b) | |
252 | return 1; | |
253 | if (*(u64 *)a > *(u64 *)b) | |
254 | return -1; | |
255 | return 0; | |
256 | } | |
257 | ||
258 | /* | |
259 | * build the snap context for a given realm. | |
260 | */ | |
261 | static int build_snap_context(struct ceph_snap_realm *realm) | |
262 | { | |
263 | struct ceph_snap_realm *parent = realm->parent; | |
264 | struct ceph_snap_context *snapc; | |
265 | int err = 0; | |
266 | int i; | |
267 | int num = realm->num_prior_parent_snaps + realm->num_snaps; | |
268 | ||
269 | /* | |
270 | * build parent context, if it hasn't been built. | |
271 | * conservatively estimate that all parent snaps might be | |
272 | * included by us. | |
273 | */ | |
274 | if (parent) { | |
275 | if (!parent->cached_context) { | |
276 | err = build_snap_context(parent); | |
277 | if (err) | |
278 | goto fail; | |
279 | } | |
280 | num += parent->cached_context->num_snaps; | |
281 | } | |
282 | ||
283 | /* do i actually need to update? not if my context seq | |
284 | matches realm seq, and my parents' does to. (this works | |
285 | because we rebuild_snap_realms() works _downward_ in | |
286 | hierarchy after each update.) */ | |
287 | if (realm->cached_context && | |
288 | realm->cached_context->seq <= realm->seq && | |
289 | (!parent || | |
290 | realm->cached_context->seq <= parent->cached_context->seq)) { | |
291 | dout("build_snap_context %llx %p: %p seq %lld (%d snaps)" | |
292 | " (unchanged)\n", | |
293 | realm->ino, realm, realm->cached_context, | |
294 | realm->cached_context->seq, | |
295 | realm->cached_context->num_snaps); | |
296 | return 0; | |
297 | } | |
298 | ||
299 | /* alloc new snap context */ | |
300 | err = -ENOMEM; | |
301 | if (num > ULONG_MAX / sizeof(u64) - sizeof(*snapc)) | |
302 | goto fail; | |
303 | snapc = kzalloc(sizeof(*snapc) + num*sizeof(u64), GFP_NOFS); | |
304 | if (!snapc) | |
305 | goto fail; | |
306 | atomic_set(&snapc->nref, 1); | |
307 | ||
308 | /* build (reverse sorted) snap vector */ | |
309 | num = 0; | |
310 | snapc->seq = realm->seq; | |
311 | if (parent) { | |
312 | /* include any of parent's snaps occuring _after_ my | |
313 | parent became my parent */ | |
314 | for (i = 0; i < parent->cached_context->num_snaps; i++) | |
315 | if (parent->cached_context->snaps[i] >= | |
316 | realm->parent_since) | |
317 | snapc->snaps[num++] = | |
318 | parent->cached_context->snaps[i]; | |
319 | if (parent->cached_context->seq > snapc->seq) | |
320 | snapc->seq = parent->cached_context->seq; | |
321 | } | |
322 | memcpy(snapc->snaps + num, realm->snaps, | |
323 | sizeof(u64)*realm->num_snaps); | |
324 | num += realm->num_snaps; | |
325 | memcpy(snapc->snaps + num, realm->prior_parent_snaps, | |
326 | sizeof(u64)*realm->num_prior_parent_snaps); | |
327 | num += realm->num_prior_parent_snaps; | |
328 | ||
329 | sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL); | |
330 | snapc->num_snaps = num; | |
331 | dout("build_snap_context %llx %p: %p seq %lld (%d snaps)\n", | |
332 | realm->ino, realm, snapc, snapc->seq, snapc->num_snaps); | |
333 | ||
334 | if (realm->cached_context) | |
335 | ceph_put_snap_context(realm->cached_context); | |
336 | realm->cached_context = snapc; | |
337 | return 0; | |
338 | ||
339 | fail: | |
340 | /* | |
341 | * if we fail, clear old (incorrect) cached_context... hopefully | |
342 | * we'll have better luck building it later | |
343 | */ | |
344 | if (realm->cached_context) { | |
345 | ceph_put_snap_context(realm->cached_context); | |
346 | realm->cached_context = NULL; | |
347 | } | |
348 | pr_err("build_snap_context %llx %p fail %d\n", realm->ino, | |
349 | realm, err); | |
350 | return err; | |
351 | } | |
352 | ||
353 | /* | |
354 | * rebuild snap context for the given realm and all of its children. | |
355 | */ | |
356 | static void rebuild_snap_realms(struct ceph_snap_realm *realm) | |
357 | { | |
358 | struct ceph_snap_realm *child; | |
359 | ||
360 | dout("rebuild_snap_realms %llx %p\n", realm->ino, realm); | |
361 | build_snap_context(realm); | |
362 | ||
363 | list_for_each_entry(child, &realm->children, child_item) | |
364 | rebuild_snap_realms(child); | |
365 | } | |
366 | ||
367 | ||
368 | /* | |
369 | * helper to allocate and decode an array of snapids. free prior | |
370 | * instance, if any. | |
371 | */ | |
372 | static int dup_array(u64 **dst, __le64 *src, int num) | |
373 | { | |
374 | int i; | |
375 | ||
376 | kfree(*dst); | |
377 | if (num) { | |
378 | *dst = kcalloc(num, sizeof(u64), GFP_NOFS); | |
379 | if (!*dst) | |
380 | return -ENOMEM; | |
381 | for (i = 0; i < num; i++) | |
382 | (*dst)[i] = get_unaligned_le64(src + i); | |
383 | } else { | |
384 | *dst = NULL; | |
385 | } | |
386 | return 0; | |
387 | } | |
388 | ||
389 | ||
390 | /* | |
391 | * When a snapshot is applied, the size/mtime inode metadata is queued | |
392 | * in a ceph_cap_snap (one for each snapshot) until writeback | |
393 | * completes and the metadata can be flushed back to the MDS. | |
394 | * | |
395 | * However, if a (sync) write is currently in-progress when we apply | |
396 | * the snapshot, we have to wait until the write succeeds or fails | |
397 | * (and a final size/mtime is known). In this case the | |
398 | * cap_snap->writing = 1, and is said to be "pending." When the write | |
399 | * finishes, we __ceph_finish_cap_snap(). | |
400 | * | |
401 | * Caller must hold snap_rwsem for read (i.e., the realm topology won't | |
402 | * change). | |
403 | */ | |
404 | void ceph_queue_cap_snap(struct ceph_inode_info *ci, | |
405 | struct ceph_snap_context *snapc) | |
406 | { | |
407 | struct inode *inode = &ci->vfs_inode; | |
408 | struct ceph_cap_snap *capsnap; | |
409 | int used; | |
410 | ||
411 | capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS); | |
412 | if (!capsnap) { | |
413 | pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode); | |
414 | return; | |
415 | } | |
416 | ||
417 | spin_lock(&inode->i_lock); | |
418 | used = __ceph_caps_used(ci); | |
419 | if (__ceph_have_pending_cap_snap(ci)) { | |
420 | /* there is no point in queuing multiple "pending" cap_snaps, | |
421 | as no new writes are allowed to start when pending, so any | |
422 | writes in progress now were started before the previous | |
423 | cap_snap. lucky us. */ | |
424 | dout("queue_cap_snap %p snapc %p seq %llu used %d" | |
425 | " already pending\n", inode, snapc, snapc->seq, used); | |
426 | kfree(capsnap); | |
427 | } else if (ci->i_wrbuffer_ref_head || (used & CEPH_CAP_FILE_WR)) { | |
428 | igrab(inode); | |
429 | ||
430 | atomic_set(&capsnap->nref, 1); | |
431 | capsnap->ci = ci; | |
432 | INIT_LIST_HEAD(&capsnap->ci_item); | |
433 | INIT_LIST_HEAD(&capsnap->flushing_item); | |
434 | ||
435 | capsnap->follows = snapc->seq - 1; | |
436 | capsnap->context = ceph_get_snap_context(snapc); | |
437 | capsnap->issued = __ceph_caps_issued(ci, NULL); | |
438 | capsnap->dirty = __ceph_caps_dirty(ci); | |
439 | ||
440 | capsnap->mode = inode->i_mode; | |
441 | capsnap->uid = inode->i_uid; | |
442 | capsnap->gid = inode->i_gid; | |
443 | ||
444 | /* fixme? */ | |
445 | capsnap->xattr_blob = NULL; | |
446 | capsnap->xattr_len = 0; | |
447 | ||
448 | /* dirty page count moved from _head to this cap_snap; | |
449 | all subsequent writes page dirties occur _after_ this | |
450 | snapshot. */ | |
451 | capsnap->dirty_pages = ci->i_wrbuffer_ref_head; | |
452 | ci->i_wrbuffer_ref_head = 0; | |
453 | ceph_put_snap_context(ci->i_head_snapc); | |
454 | ci->i_head_snapc = NULL; | |
455 | list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps); | |
456 | ||
457 | if (used & CEPH_CAP_FILE_WR) { | |
458 | dout("queue_cap_snap %p cap_snap %p snapc %p" | |
459 | " seq %llu used WR, now pending\n", inode, | |
460 | capsnap, snapc, snapc->seq); | |
461 | capsnap->writing = 1; | |
462 | } else { | |
463 | /* note mtime, size NOW. */ | |
464 | __ceph_finish_cap_snap(ci, capsnap); | |
465 | } | |
466 | } else { | |
467 | dout("queue_cap_snap %p nothing dirty|writing\n", inode); | |
468 | kfree(capsnap); | |
469 | } | |
470 | ||
471 | spin_unlock(&inode->i_lock); | |
472 | } | |
473 | ||
474 | /* | |
475 | * Finalize the size, mtime for a cap_snap.. that is, settle on final values | |
476 | * to be used for the snapshot, to be flushed back to the mds. | |
477 | * | |
478 | * If capsnap can now be flushed, add to snap_flush list, and return 1. | |
479 | * | |
480 | * Caller must hold i_lock. | |
481 | */ | |
482 | int __ceph_finish_cap_snap(struct ceph_inode_info *ci, | |
483 | struct ceph_cap_snap *capsnap) | |
484 | { | |
485 | struct inode *inode = &ci->vfs_inode; | |
486 | struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc; | |
487 | ||
488 | BUG_ON(capsnap->writing); | |
489 | capsnap->size = inode->i_size; | |
490 | capsnap->mtime = inode->i_mtime; | |
491 | capsnap->atime = inode->i_atime; | |
492 | capsnap->ctime = inode->i_ctime; | |
493 | capsnap->time_warp_seq = ci->i_time_warp_seq; | |
494 | if (capsnap->dirty_pages) { | |
495 | dout("finish_cap_snap %p cap_snap %p snapc %p %llu s=%llu " | |
496 | "still has %d dirty pages\n", inode, capsnap, | |
497 | capsnap->context, capsnap->context->seq, | |
498 | capsnap->size, capsnap->dirty_pages); | |
499 | return 0; | |
500 | } | |
501 | dout("finish_cap_snap %p cap_snap %p snapc %p %llu s=%llu clean\n", | |
502 | inode, capsnap, capsnap->context, | |
503 | capsnap->context->seq, capsnap->size); | |
504 | ||
505 | spin_lock(&mdsc->snap_flush_lock); | |
506 | list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list); | |
507 | spin_unlock(&mdsc->snap_flush_lock); | |
508 | return 1; /* caller may want to ceph_flush_snaps */ | |
509 | } | |
510 | ||
511 | ||
512 | /* | |
513 | * Parse and apply a snapblob "snap trace" from the MDS. This specifies | |
514 | * the snap realm parameters from a given realm and all of its ancestors, | |
515 | * up to the root. | |
516 | * | |
517 | * Caller must hold snap_rwsem for write. | |
518 | */ | |
519 | int ceph_update_snap_trace(struct ceph_mds_client *mdsc, | |
520 | void *p, void *e, bool deletion) | |
521 | { | |
522 | struct ceph_mds_snap_realm *ri; /* encoded */ | |
523 | __le64 *snaps; /* encoded */ | |
524 | __le64 *prior_parent_snaps; /* encoded */ | |
525 | struct ceph_snap_realm *realm; | |
526 | int invalidate = 0; | |
527 | int err = -ENOMEM; | |
528 | ||
529 | dout("update_snap_trace deletion=%d\n", deletion); | |
530 | more: | |
531 | ceph_decode_need(&p, e, sizeof(*ri), bad); | |
532 | ri = p; | |
533 | p += sizeof(*ri); | |
534 | ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) + | |
535 | le32_to_cpu(ri->num_prior_parent_snaps)), bad); | |
536 | snaps = p; | |
537 | p += sizeof(u64) * le32_to_cpu(ri->num_snaps); | |
538 | prior_parent_snaps = p; | |
539 | p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps); | |
540 | ||
541 | realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino)); | |
963b61eb SW |
542 | if (!realm) { |
543 | realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino)); | |
544 | if (IS_ERR(realm)) { | |
545 | err = PTR_ERR(realm); | |
546 | goto fail; | |
547 | } | |
548 | } | |
549 | ||
550 | if (le64_to_cpu(ri->seq) > realm->seq) { | |
551 | dout("update_snap_trace updating %llx %p %lld -> %lld\n", | |
552 | realm->ino, realm, realm->seq, le64_to_cpu(ri->seq)); | |
553 | /* | |
554 | * if the realm seq has changed, queue a cap_snap for every | |
555 | * inode with open caps. we do this _before_ we update | |
556 | * the realm info so that we prepare for writeback under the | |
557 | * _previous_ snap context. | |
558 | * | |
559 | * ...unless it's a snap deletion! | |
560 | */ | |
561 | if (!deletion) { | |
562 | struct ceph_inode_info *ci; | |
563 | struct inode *lastinode = NULL; | |
564 | ||
565 | spin_lock(&realm->inodes_with_caps_lock); | |
566 | list_for_each_entry(ci, &realm->inodes_with_caps, | |
567 | i_snap_realm_item) { | |
568 | struct inode *inode = igrab(&ci->vfs_inode); | |
569 | if (!inode) | |
570 | continue; | |
571 | spin_unlock(&realm->inodes_with_caps_lock); | |
572 | if (lastinode) | |
573 | iput(lastinode); | |
574 | lastinode = inode; | |
575 | ceph_queue_cap_snap(ci, realm->cached_context); | |
576 | spin_lock(&realm->inodes_with_caps_lock); | |
577 | } | |
578 | spin_unlock(&realm->inodes_with_caps_lock); | |
579 | if (lastinode) | |
580 | iput(lastinode); | |
581 | dout("update_snap_trace cap_snaps queued\n"); | |
582 | } | |
583 | ||
584 | } else { | |
585 | dout("update_snap_trace %llx %p seq %lld unchanged\n", | |
586 | realm->ino, realm, realm->seq); | |
587 | } | |
588 | ||
589 | /* ensure the parent is correct */ | |
590 | err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent)); | |
591 | if (err < 0) | |
592 | goto fail; | |
593 | invalidate += err; | |
594 | ||
595 | if (le64_to_cpu(ri->seq) > realm->seq) { | |
596 | /* update realm parameters, snap lists */ | |
597 | realm->seq = le64_to_cpu(ri->seq); | |
598 | realm->created = le64_to_cpu(ri->created); | |
599 | realm->parent_since = le64_to_cpu(ri->parent_since); | |
600 | ||
601 | realm->num_snaps = le32_to_cpu(ri->num_snaps); | |
602 | err = dup_array(&realm->snaps, snaps, realm->num_snaps); | |
603 | if (err < 0) | |
604 | goto fail; | |
605 | ||
606 | realm->num_prior_parent_snaps = | |
607 | le32_to_cpu(ri->num_prior_parent_snaps); | |
608 | err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps, | |
609 | realm->num_prior_parent_snaps); | |
610 | if (err < 0) | |
611 | goto fail; | |
612 | ||
613 | invalidate = 1; | |
614 | } else if (!realm->cached_context) { | |
615 | invalidate = 1; | |
616 | } | |
617 | ||
618 | dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino, | |
619 | realm, invalidate, p, e); | |
620 | ||
621 | if (p < e) | |
622 | goto more; | |
623 | ||
624 | /* invalidate when we reach the _end_ (root) of the trace */ | |
625 | if (invalidate) | |
626 | rebuild_snap_realms(realm); | |
627 | ||
628 | __cleanup_empty_realms(mdsc); | |
629 | return 0; | |
630 | ||
631 | bad: | |
632 | err = -EINVAL; | |
633 | fail: | |
634 | pr_err("update_snap_trace error %d\n", err); | |
635 | return err; | |
636 | } | |
637 | ||
638 | ||
639 | /* | |
640 | * Send any cap_snaps that are queued for flush. Try to carry | |
641 | * s_mutex across multiple snap flushes to avoid locking overhead. | |
642 | * | |
643 | * Caller holds no locks. | |
644 | */ | |
645 | static void flush_snaps(struct ceph_mds_client *mdsc) | |
646 | { | |
647 | struct ceph_inode_info *ci; | |
648 | struct inode *inode; | |
649 | struct ceph_mds_session *session = NULL; | |
650 | ||
651 | dout("flush_snaps\n"); | |
652 | spin_lock(&mdsc->snap_flush_lock); | |
653 | while (!list_empty(&mdsc->snap_flush_list)) { | |
654 | ci = list_first_entry(&mdsc->snap_flush_list, | |
655 | struct ceph_inode_info, i_snap_flush_item); | |
656 | inode = &ci->vfs_inode; | |
657 | igrab(inode); | |
658 | spin_unlock(&mdsc->snap_flush_lock); | |
659 | spin_lock(&inode->i_lock); | |
660 | __ceph_flush_snaps(ci, &session); | |
661 | spin_unlock(&inode->i_lock); | |
662 | iput(inode); | |
663 | spin_lock(&mdsc->snap_flush_lock); | |
664 | } | |
665 | spin_unlock(&mdsc->snap_flush_lock); | |
666 | ||
667 | if (session) { | |
668 | mutex_unlock(&session->s_mutex); | |
669 | ceph_put_mds_session(session); | |
670 | } | |
671 | dout("flush_snaps done\n"); | |
672 | } | |
673 | ||
674 | ||
675 | /* | |
676 | * Handle a snap notification from the MDS. | |
677 | * | |
678 | * This can take two basic forms: the simplest is just a snap creation | |
679 | * or deletion notification on an existing realm. This should update the | |
680 | * realm and its children. | |
681 | * | |
682 | * The more difficult case is realm creation, due to snap creation at a | |
683 | * new point in the file hierarchy, or due to a rename that moves a file or | |
684 | * directory into another realm. | |
685 | */ | |
686 | void ceph_handle_snap(struct ceph_mds_client *mdsc, | |
687 | struct ceph_msg *msg) | |
688 | { | |
689 | struct super_block *sb = mdsc->client->sb; | |
690 | struct ceph_mds_session *session; | |
691 | int mds; | |
692 | u64 split; | |
693 | int op; | |
694 | int trace_len; | |
695 | struct ceph_snap_realm *realm = NULL; | |
696 | void *p = msg->front.iov_base; | |
697 | void *e = p + msg->front.iov_len; | |
698 | struct ceph_mds_snap_head *h; | |
699 | int num_split_inos, num_split_realms; | |
700 | __le64 *split_inos = NULL, *split_realms = NULL; | |
701 | int i; | |
702 | int locked_rwsem = 0; | |
703 | ||
704 | if (msg->hdr.src.name.type != CEPH_ENTITY_TYPE_MDS) | |
705 | return; | |
706 | mds = le64_to_cpu(msg->hdr.src.name.num); | |
707 | ||
708 | /* decode */ | |
709 | if (msg->front.iov_len < sizeof(*h)) | |
710 | goto bad; | |
711 | h = p; | |
712 | op = le32_to_cpu(h->op); | |
713 | split = le64_to_cpu(h->split); /* non-zero if we are splitting an | |
714 | * existing realm */ | |
715 | num_split_inos = le32_to_cpu(h->num_split_inos); | |
716 | num_split_realms = le32_to_cpu(h->num_split_realms); | |
717 | trace_len = le32_to_cpu(h->trace_len); | |
718 | p += sizeof(*h); | |
719 | ||
720 | dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds, | |
721 | ceph_snap_op_name(op), split, trace_len); | |
722 | ||
723 | /* find session */ | |
724 | mutex_lock(&mdsc->mutex); | |
725 | session = __ceph_lookup_mds_session(mdsc, mds); | |
726 | mutex_unlock(&mdsc->mutex); | |
727 | if (!session) { | |
728 | dout("WTF, got snap but no session for mds%d\n", mds); | |
729 | return; | |
730 | } | |
731 | ||
732 | mutex_lock(&session->s_mutex); | |
733 | session->s_seq++; | |
734 | mutex_unlock(&session->s_mutex); | |
735 | ||
736 | down_write(&mdsc->snap_rwsem); | |
737 | locked_rwsem = 1; | |
738 | ||
739 | if (op == CEPH_SNAP_OP_SPLIT) { | |
740 | struct ceph_mds_snap_realm *ri; | |
741 | ||
742 | /* | |
743 | * A "split" breaks part of an existing realm off into | |
744 | * a new realm. The MDS provides a list of inodes | |
745 | * (with caps) and child realms that belong to the new | |
746 | * child. | |
747 | */ | |
748 | split_inos = p; | |
749 | p += sizeof(u64) * num_split_inos; | |
750 | split_realms = p; | |
751 | p += sizeof(u64) * num_split_realms; | |
752 | ceph_decode_need(&p, e, sizeof(*ri), bad); | |
753 | /* we will peek at realm info here, but will _not_ | |
754 | * advance p, as the realm update will occur below in | |
755 | * ceph_update_snap_trace. */ | |
756 | ri = p; | |
757 | ||
758 | realm = ceph_lookup_snap_realm(mdsc, split); | |
963b61eb SW |
759 | if (!realm) { |
760 | realm = ceph_create_snap_realm(mdsc, split); | |
761 | if (IS_ERR(realm)) | |
762 | goto out; | |
763 | } | |
764 | ceph_get_snap_realm(mdsc, realm); | |
765 | ||
766 | dout("splitting snap_realm %llx %p\n", realm->ino, realm); | |
767 | for (i = 0; i < num_split_inos; i++) { | |
768 | struct ceph_vino vino = { | |
769 | .ino = le64_to_cpu(split_inos[i]), | |
770 | .snap = CEPH_NOSNAP, | |
771 | }; | |
772 | struct inode *inode = ceph_find_inode(sb, vino); | |
773 | struct ceph_inode_info *ci; | |
774 | ||
775 | if (!inode) | |
776 | continue; | |
777 | ci = ceph_inode(inode); | |
778 | ||
779 | spin_lock(&inode->i_lock); | |
780 | if (!ci->i_snap_realm) | |
781 | goto skip_inode; | |
782 | /* | |
783 | * If this inode belongs to a realm that was | |
784 | * created after our new realm, we experienced | |
785 | * a race (due to another split notifications | |
786 | * arriving from a different MDS). So skip | |
787 | * this inode. | |
788 | */ | |
789 | if (ci->i_snap_realm->created > | |
790 | le64_to_cpu(ri->created)) { | |
791 | dout(" leaving %p in newer realm %llx %p\n", | |
792 | inode, ci->i_snap_realm->ino, | |
793 | ci->i_snap_realm); | |
794 | goto skip_inode; | |
795 | } | |
796 | dout(" will move %p to split realm %llx %p\n", | |
797 | inode, realm->ino, realm); | |
798 | /* | |
799 | * Remove the inode from the realm's inode | |
800 | * list, but don't add it to the new realm | |
801 | * yet. We don't want the cap_snap to be | |
802 | * queued (again) by ceph_update_snap_trace() | |
803 | * below. Queue it _now_, under the old context. | |
804 | */ | |
805 | list_del_init(&ci->i_snap_realm_item); | |
806 | spin_unlock(&inode->i_lock); | |
807 | ||
808 | ceph_queue_cap_snap(ci, | |
809 | ci->i_snap_realm->cached_context); | |
810 | ||
811 | iput(inode); | |
812 | continue; | |
813 | ||
814 | skip_inode: | |
815 | spin_unlock(&inode->i_lock); | |
816 | iput(inode); | |
817 | } | |
818 | ||
819 | /* we may have taken some of the old realm's children. */ | |
820 | for (i = 0; i < num_split_realms; i++) { | |
821 | struct ceph_snap_realm *child = | |
822 | ceph_lookup_snap_realm(mdsc, | |
823 | le64_to_cpu(split_realms[i])); | |
963b61eb SW |
824 | if (!child) |
825 | continue; | |
826 | adjust_snap_realm_parent(mdsc, child, realm->ino); | |
827 | } | |
828 | } | |
829 | ||
830 | /* | |
831 | * update using the provided snap trace. if we are deleting a | |
832 | * snap, we can avoid queueing cap_snaps. | |
833 | */ | |
834 | ceph_update_snap_trace(mdsc, p, e, | |
835 | op == CEPH_SNAP_OP_DESTROY); | |
836 | ||
837 | if (op == CEPH_SNAP_OP_SPLIT) { | |
838 | /* | |
839 | * ok, _now_ add the inodes into the new realm. | |
840 | */ | |
841 | for (i = 0; i < num_split_inos; i++) { | |
842 | struct ceph_vino vino = { | |
843 | .ino = le64_to_cpu(split_inos[i]), | |
844 | .snap = CEPH_NOSNAP, | |
845 | }; | |
846 | struct inode *inode = ceph_find_inode(sb, vino); | |
847 | struct ceph_inode_info *ci; | |
848 | ||
849 | if (!inode) | |
850 | continue; | |
851 | ci = ceph_inode(inode); | |
852 | spin_lock(&inode->i_lock); | |
853 | if (!ci->i_snap_realm) | |
854 | goto split_skip_inode; | |
855 | ceph_put_snap_realm(mdsc, ci->i_snap_realm); | |
856 | spin_lock(&realm->inodes_with_caps_lock); | |
857 | list_add(&ci->i_snap_realm_item, | |
858 | &realm->inodes_with_caps); | |
859 | ci->i_snap_realm = realm; | |
860 | spin_unlock(&realm->inodes_with_caps_lock); | |
861 | ceph_get_snap_realm(mdsc, realm); | |
862 | split_skip_inode: | |
863 | spin_unlock(&inode->i_lock); | |
864 | iput(inode); | |
865 | } | |
866 | ||
867 | /* we took a reference when we created the realm, above */ | |
868 | ceph_put_snap_realm(mdsc, realm); | |
869 | } | |
870 | ||
871 | __cleanup_empty_realms(mdsc); | |
872 | ||
873 | up_write(&mdsc->snap_rwsem); | |
874 | ||
875 | flush_snaps(mdsc); | |
876 | return; | |
877 | ||
878 | bad: | |
879 | pr_err("corrupt snap message from mds%d\n", mds); | |
880 | out: | |
881 | if (locked_rwsem) | |
882 | up_write(&mdsc->snap_rwsem); | |
883 | return; | |
884 | } | |
885 | ||
886 | ||
887 |