PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ceph/snap.c

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