snap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. struct ceph_buffer *old_blob = NULL;
  415. int used, dirty;
  416. capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
  417. if (!capsnap) {
  418. pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode);
  419. return;
  420. }
  421. spin_lock(&ci->i_ceph_lock);
  422. used = __ceph_caps_used(ci);
  423. dirty = __ceph_caps_dirty(ci);
  424. old_snapc = ci->i_head_snapc;
  425. new_snapc = ci->i_snap_realm->cached_context;
  426. /*
  427. * If there is a write in progress, treat that as a dirty Fw,
  428. * even though it hasn't completed yet; by the time we finish
  429. * up this capsnap it will be.
  430. */
  431. if (used & CEPH_CAP_FILE_WR)
  432. dirty |= CEPH_CAP_FILE_WR;
  433. if (__ceph_have_pending_cap_snap(ci)) {
  434. /* there is no point in queuing multiple "pending" cap_snaps,
  435. as no new writes are allowed to start when pending, so any
  436. writes in progress now were started before the previous
  437. cap_snap. lucky us. */
  438. dout("queue_cap_snap %p already pending\n", inode);
  439. goto update_snapc;
  440. }
  441. if (ci->i_wrbuffer_ref_head == 0 &&
  442. !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
  443. dout("queue_cap_snap %p nothing dirty|writing\n", inode);
  444. goto update_snapc;
  445. }
  446. BUG_ON(!old_snapc);
  447. /*
  448. * There is no need to send FLUSHSNAP message to MDS if there is
  449. * no new snapshot. But when there is dirty pages or on-going
  450. * writes, we still need to create cap_snap. cap_snap is needed
  451. * by the write path and page writeback path.
  452. *
  453. * also see ceph_try_drop_cap_snap()
  454. */
  455. if (has_new_snaps(old_snapc, new_snapc)) {
  456. if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
  457. capsnap->need_flush = true;
  458. } else {
  459. if (!(used & CEPH_CAP_FILE_WR) &&
  460. ci->i_wrbuffer_ref_head == 0) {
  461. dout("queue_cap_snap %p "
  462. "no new_snap|dirty_page|writing\n", inode);
  463. goto update_snapc;
  464. }
  465. }
  466. dout("queue_cap_snap %p cap_snap %p queuing under %p %s %s\n",
  467. inode, capsnap, old_snapc, ceph_cap_string(dirty),
  468. capsnap->need_flush ? "" : "no_flush");
  469. ihold(inode);
  470. refcount_set(&capsnap->nref, 1);
  471. INIT_LIST_HEAD(&capsnap->ci_item);
  472. capsnap->follows = old_snapc->seq;
  473. capsnap->issued = __ceph_caps_issued(ci, NULL);
  474. capsnap->dirty = dirty;
  475. capsnap->mode = inode->i_mode;
  476. capsnap->uid = inode->i_uid;
  477. capsnap->gid = inode->i_gid;
  478. if (dirty & CEPH_CAP_XATTR_EXCL) {
  479. old_blob = __ceph_build_xattrs_blob(ci);
  480. capsnap->xattr_blob =
  481. ceph_buffer_get(ci->i_xattrs.blob);
  482. capsnap->xattr_version = ci->i_xattrs.version;
  483. } else {
  484. capsnap->xattr_blob = NULL;
  485. capsnap->xattr_version = 0;
  486. }
  487. capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  488. /* dirty page count moved from _head to this cap_snap;
  489. all subsequent writes page dirties occur _after_ this
  490. snapshot. */
  491. capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
  492. ci->i_wrbuffer_ref_head = 0;
  493. capsnap->context = old_snapc;
  494. list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
  495. if (used & CEPH_CAP_FILE_WR) {
  496. dout("queue_cap_snap %p cap_snap %p snapc %p"
  497. " seq %llu used WR, now pending\n", inode,
  498. capsnap, old_snapc, old_snapc->seq);
  499. capsnap->writing = 1;
  500. } else {
  501. /* note mtime, size NOW. */
  502. __ceph_finish_cap_snap(ci, capsnap);
  503. }
  504. capsnap = NULL;
  505. old_snapc = NULL;
  506. update_snapc:
  507. if (ci->i_wrbuffer_ref_head == 0 &&
  508. ci->i_wr_ref == 0 &&
  509. ci->i_dirty_caps == 0 &&
  510. ci->i_flushing_caps == 0) {
  511. ci->i_head_snapc = NULL;
  512. } else {
  513. ci->i_head_snapc = ceph_get_snap_context(new_snapc);
  514. dout(" new snapc is %p\n", new_snapc);
  515. }
  516. spin_unlock(&ci->i_ceph_lock);
  517. ceph_buffer_put(old_blob);
  518. kfree(capsnap);
  519. ceph_put_snap_context(old_snapc);
  520. }
  521. /*
  522. * Finalize the size, mtime for a cap_snap.. that is, settle on final values
  523. * to be used for the snapshot, to be flushed back to the mds.
  524. *
  525. * If capsnap can now be flushed, add to snap_flush list, and return 1.
  526. *
  527. * Caller must hold i_ceph_lock.
  528. */
  529. int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
  530. struct ceph_cap_snap *capsnap)
  531. {
  532. struct inode *inode = &ci->vfs_inode;
  533. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  534. BUG_ON(capsnap->writing);
  535. capsnap->size = inode->i_size;
  536. capsnap->mtime = inode->i_mtime;
  537. capsnap->atime = inode->i_atime;
  538. capsnap->ctime = inode->i_ctime;
  539. capsnap->time_warp_seq = ci->i_time_warp_seq;
  540. capsnap->truncate_size = ci->i_truncate_size;
  541. capsnap->truncate_seq = ci->i_truncate_seq;
  542. if (capsnap->dirty_pages) {
  543. dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
  544. "still has %d dirty pages\n", inode, capsnap,
  545. capsnap->context, capsnap->context->seq,
  546. ceph_cap_string(capsnap->dirty), capsnap->size,
  547. capsnap->dirty_pages);
  548. return 0;
  549. }
  550. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  551. dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
  552. inode, capsnap, capsnap->context,
  553. capsnap->context->seq, ceph_cap_string(capsnap->dirty),
  554. capsnap->size);
  555. spin_lock(&mdsc->snap_flush_lock);
  556. if (list_empty(&ci->i_snap_flush_item))
  557. list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
  558. spin_unlock(&mdsc->snap_flush_lock);
  559. return 1; /* caller may want to ceph_flush_snaps */
  560. }
  561. /*
  562. * Queue cap_snaps for snap writeback for this realm and its children.
  563. * Called under snap_rwsem, so realm topology won't change.
  564. */
  565. static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
  566. {
  567. struct ceph_inode_info *ci;
  568. struct inode *lastinode = NULL;
  569. dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
  570. spin_lock(&realm->inodes_with_caps_lock);
  571. list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
  572. struct inode *inode = igrab(&ci->vfs_inode);
  573. if (!inode)
  574. continue;
  575. spin_unlock(&realm->inodes_with_caps_lock);
  576. iput(lastinode);
  577. lastinode = inode;
  578. ceph_queue_cap_snap(ci);
  579. spin_lock(&realm->inodes_with_caps_lock);
  580. }
  581. spin_unlock(&realm->inodes_with_caps_lock);
  582. iput(lastinode);
  583. dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
  584. }
  585. /*
  586. * Parse and apply a snapblob "snap trace" from the MDS. This specifies
  587. * the snap realm parameters from a given realm and all of its ancestors,
  588. * up to the root.
  589. *
  590. * Caller must hold snap_rwsem for write.
  591. */
  592. int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
  593. void *p, void *e, bool deletion,
  594. struct ceph_snap_realm **realm_ret)
  595. {
  596. struct ceph_mds_snap_realm *ri; /* encoded */
  597. __le64 *snaps; /* encoded */
  598. __le64 *prior_parent_snaps; /* encoded */
  599. struct ceph_snap_realm *realm = NULL;
  600. struct ceph_snap_realm *first_realm = NULL;
  601. int invalidate = 0;
  602. int err = -ENOMEM;
  603. LIST_HEAD(dirty_realms);
  604. dout("update_snap_trace deletion=%d\n", deletion);
  605. more:
  606. ceph_decode_need(&p, e, sizeof(*ri), bad);
  607. ri = p;
  608. p += sizeof(*ri);
  609. ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
  610. le32_to_cpu(ri->num_prior_parent_snaps)), bad);
  611. snaps = p;
  612. p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
  613. prior_parent_snaps = p;
  614. p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
  615. realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
  616. if (!realm) {
  617. realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
  618. if (IS_ERR(realm)) {
  619. err = PTR_ERR(realm);
  620. goto fail;
  621. }
  622. }
  623. /* ensure the parent is correct */
  624. err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
  625. if (err < 0)
  626. goto fail;
  627. invalidate += err;
  628. if (le64_to_cpu(ri->seq) > realm->seq) {
  629. dout("update_snap_trace updating %llx %p %lld -> %lld\n",
  630. realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
  631. /* update realm parameters, snap lists */
  632. realm->seq = le64_to_cpu(ri->seq);
  633. realm->created = le64_to_cpu(ri->created);
  634. realm->parent_since = le64_to_cpu(ri->parent_since);
  635. realm->num_snaps = le32_to_cpu(ri->num_snaps);
  636. err = dup_array(&realm->snaps, snaps, realm->num_snaps);
  637. if (err < 0)
  638. goto fail;
  639. realm->num_prior_parent_snaps =
  640. le32_to_cpu(ri->num_prior_parent_snaps);
  641. err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
  642. realm->num_prior_parent_snaps);
  643. if (err < 0)
  644. goto fail;
  645. if (realm->seq > mdsc->last_snap_seq)
  646. mdsc->last_snap_seq = realm->seq;
  647. invalidate = 1;
  648. } else if (!realm->cached_context) {
  649. dout("update_snap_trace %llx %p seq %lld new\n",
  650. realm->ino, realm, realm->seq);
  651. invalidate = 1;
  652. } else {
  653. dout("update_snap_trace %llx %p seq %lld unchanged\n",
  654. realm->ino, realm, realm->seq);
  655. }
  656. dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino,
  657. realm, invalidate, p, e);
  658. /* invalidate when we reach the _end_ (root) of the trace */
  659. if (invalidate && p >= e)
  660. rebuild_snap_realms(realm, &dirty_realms);
  661. if (!first_realm)
  662. first_realm = realm;
  663. else
  664. ceph_put_snap_realm(mdsc, realm);
  665. if (p < e)
  666. goto more;
  667. /*
  668. * queue cap snaps _after_ we've built the new snap contexts,
  669. * so that i_head_snapc can be set appropriately.
  670. */
  671. while (!list_empty(&dirty_realms)) {
  672. realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
  673. dirty_item);
  674. list_del_init(&realm->dirty_item);
  675. queue_realm_cap_snaps(realm);
  676. }
  677. if (realm_ret)
  678. *realm_ret = first_realm;
  679. else
  680. ceph_put_snap_realm(mdsc, first_realm);
  681. __cleanup_empty_realms(mdsc);
  682. return 0;
  683. bad:
  684. err = -EINVAL;
  685. fail:
  686. if (realm && !IS_ERR(realm))
  687. ceph_put_snap_realm(mdsc, realm);
  688. if (first_realm)
  689. ceph_put_snap_realm(mdsc, first_realm);
  690. pr_err("update_snap_trace error %d\n", err);
  691. return err;
  692. }
  693. /*
  694. * Send any cap_snaps that are queued for flush. Try to carry
  695. * s_mutex across multiple snap flushes to avoid locking overhead.
  696. *
  697. * Caller holds no locks.
  698. */
  699. static void flush_snaps(struct ceph_mds_client *mdsc)
  700. {
  701. struct ceph_inode_info *ci;
  702. struct inode *inode;
  703. struct ceph_mds_session *session = NULL;
  704. dout("flush_snaps\n");
  705. spin_lock(&mdsc->snap_flush_lock);
  706. while (!list_empty(&mdsc->snap_flush_list)) {
  707. ci = list_first_entry(&mdsc->snap_flush_list,
  708. struct ceph_inode_info, i_snap_flush_item);
  709. inode = &ci->vfs_inode;
  710. ihold(inode);
  711. spin_unlock(&mdsc->snap_flush_lock);
  712. ceph_flush_snaps(ci, &session);
  713. iput(inode);
  714. spin_lock(&mdsc->snap_flush_lock);
  715. }
  716. spin_unlock(&mdsc->snap_flush_lock);
  717. if (session) {
  718. mutex_unlock(&session->s_mutex);
  719. ceph_put_mds_session(session);
  720. }
  721. dout("flush_snaps done\n");
  722. }
  723. /*
  724. * Handle a snap notification from the MDS.
  725. *
  726. * This can take two basic forms: the simplest is just a snap creation
  727. * or deletion notification on an existing realm. This should update the
  728. * realm and its children.
  729. *
  730. * The more difficult case is realm creation, due to snap creation at a
  731. * new point in the file hierarchy, or due to a rename that moves a file or
  732. * directory into another realm.
  733. */
  734. void ceph_handle_snap(struct ceph_mds_client *mdsc,
  735. struct ceph_mds_session *session,
  736. struct ceph_msg *msg)
  737. {
  738. struct super_block *sb = mdsc->fsc->sb;
  739. int mds = session->s_mds;
  740. u64 split;
  741. int op;
  742. int trace_len;
  743. struct ceph_snap_realm *realm = NULL;
  744. void *p = msg->front.iov_base;
  745. void *e = p + msg->front.iov_len;
  746. struct ceph_mds_snap_head *h;
  747. int num_split_inos, num_split_realms;
  748. __le64 *split_inos = NULL, *split_realms = NULL;
  749. int i;
  750. int locked_rwsem = 0;
  751. /* decode */
  752. if (msg->front.iov_len < sizeof(*h))
  753. goto bad;
  754. h = p;
  755. op = le32_to_cpu(h->op);
  756. split = le64_to_cpu(h->split); /* non-zero if we are splitting an
  757. * existing realm */
  758. num_split_inos = le32_to_cpu(h->num_split_inos);
  759. num_split_realms = le32_to_cpu(h->num_split_realms);
  760. trace_len = le32_to_cpu(h->trace_len);
  761. p += sizeof(*h);
  762. dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
  763. ceph_snap_op_name(op), split, trace_len);
  764. mutex_lock(&session->s_mutex);
  765. session->s_seq++;
  766. mutex_unlock(&session->s_mutex);
  767. down_write(&mdsc->snap_rwsem);
  768. locked_rwsem = 1;
  769. if (op == CEPH_SNAP_OP_SPLIT) {
  770. struct ceph_mds_snap_realm *ri;
  771. /*
  772. * A "split" breaks part of an existing realm off into
  773. * a new realm. The MDS provides a list of inodes
  774. * (with caps) and child realms that belong to the new
  775. * child.
  776. */
  777. split_inos = p;
  778. p += sizeof(u64) * num_split_inos;
  779. split_realms = p;
  780. p += sizeof(u64) * num_split_realms;
  781. ceph_decode_need(&p, e, sizeof(*ri), bad);
  782. /* we will peek at realm info here, but will _not_
  783. * advance p, as the realm update will occur below in
  784. * ceph_update_snap_trace. */
  785. ri = p;
  786. realm = ceph_lookup_snap_realm(mdsc, split);
  787. if (!realm) {
  788. realm = ceph_create_snap_realm(mdsc, split);
  789. if (IS_ERR(realm))
  790. goto out;
  791. }
  792. dout("splitting snap_realm %llx %p\n", realm->ino, realm);
  793. for (i = 0; i < num_split_inos; i++) {
  794. struct ceph_vino vino = {
  795. .ino = le64_to_cpu(split_inos[i]),
  796. .snap = CEPH_NOSNAP,
  797. };
  798. struct inode *inode = ceph_find_inode(sb, vino);
  799. struct ceph_inode_info *ci;
  800. struct ceph_snap_realm *oldrealm;
  801. if (!inode)
  802. continue;
  803. ci = ceph_inode(inode);
  804. spin_lock(&ci->i_ceph_lock);
  805. if (!ci->i_snap_realm)
  806. goto skip_inode;
  807. /*
  808. * If this inode belongs to a realm that was
  809. * created after our new realm, we experienced
  810. * a race (due to another split notifications
  811. * arriving from a different MDS). So skip
  812. * this inode.
  813. */
  814. if (ci->i_snap_realm->created >
  815. le64_to_cpu(ri->created)) {
  816. dout(" leaving %p in newer realm %llx %p\n",
  817. inode, ci->i_snap_realm->ino,
  818. ci->i_snap_realm);
  819. goto skip_inode;
  820. }
  821. dout(" will move %p to split realm %llx %p\n",
  822. inode, realm->ino, realm);
  823. /*
  824. * Move the inode to the new realm
  825. */
  826. oldrealm = ci->i_snap_realm;
  827. spin_lock(&oldrealm->inodes_with_caps_lock);
  828. list_del_init(&ci->i_snap_realm_item);
  829. spin_unlock(&oldrealm->inodes_with_caps_lock);
  830. spin_lock(&realm->inodes_with_caps_lock);
  831. list_add(&ci->i_snap_realm_item,
  832. &realm->inodes_with_caps);
  833. ci->i_snap_realm = realm;
  834. if (realm->ino == ci->i_vino.ino)
  835. realm->inode = inode;
  836. spin_unlock(&realm->inodes_with_caps_lock);
  837. spin_unlock(&ci->i_ceph_lock);
  838. ceph_get_snap_realm(mdsc, realm);
  839. ceph_put_snap_realm(mdsc, oldrealm);
  840. iput(inode);
  841. continue;
  842. skip_inode:
  843. spin_unlock(&ci->i_ceph_lock);
  844. iput(inode);
  845. }
  846. /* we may have taken some of the old realm's children. */
  847. for (i = 0; i < num_split_realms; i++) {
  848. struct ceph_snap_realm *child =
  849. __lookup_snap_realm(mdsc,
  850. le64_to_cpu(split_realms[i]));
  851. if (!child)
  852. continue;
  853. adjust_snap_realm_parent(mdsc, child, realm->ino);
  854. }
  855. }
  856. /*
  857. * update using the provided snap trace. if we are deleting a
  858. * snap, we can avoid queueing cap_snaps.
  859. */
  860. ceph_update_snap_trace(mdsc, p, e,
  861. op == CEPH_SNAP_OP_DESTROY, NULL);
  862. if (op == CEPH_SNAP_OP_SPLIT)
  863. /* we took a reference when we created the realm, above */
  864. ceph_put_snap_realm(mdsc, realm);
  865. __cleanup_empty_realms(mdsc);
  866. up_write(&mdsc->snap_rwsem);
  867. flush_snaps(mdsc);
  868. return;
  869. bad:
  870. pr_err("corrupt snap message from mds%d\n", mds);
  871. ceph_msg_dump(msg);
  872. out:
  873. if (locked_rwsem)
  874. up_write(&mdsc->snap_rwsem);
  875. return;
  876. }