snap.c 25 KB

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