snap.c 25 KB

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