snap.c 25 KB

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