snap.c 25 KB

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