snap.c 27 KB

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