snap.c 27 KB

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