snap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. struct list_head* dirty_realms)
  271. {
  272. struct ceph_snap_realm *parent = realm->parent;
  273. struct ceph_snap_context *snapc;
  274. int err = 0;
  275. u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
  276. /*
  277. * build parent context, if it hasn't been built.
  278. * conservatively estimate that all parent snaps might be
  279. * included by us.
  280. */
  281. if (parent) {
  282. if (!parent->cached_context) {
  283. err = build_snap_context(parent, dirty_realms);
  284. if (err)
  285. goto fail;
  286. }
  287. num += parent->cached_context->num_snaps;
  288. }
  289. /* do i actually need to update? not if my context seq
  290. matches realm seq, and my parents' does to. (this works
  291. because we rebuild_snap_realms() works _downward_ in
  292. hierarchy after each update.) */
  293. if (realm->cached_context &&
  294. realm->cached_context->seq == realm->seq &&
  295. (!parent ||
  296. realm->cached_context->seq >= parent->cached_context->seq)) {
  297. dout("build_snap_context %llx %p: %p seq %lld (%u snaps)"
  298. " (unchanged)\n",
  299. realm->ino, realm, realm->cached_context,
  300. realm->cached_context->seq,
  301. (unsigned int)realm->cached_context->num_snaps);
  302. return 0;
  303. }
  304. /* alloc new snap context */
  305. err = -ENOMEM;
  306. if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
  307. goto fail;
  308. snapc = ceph_create_snap_context(num, GFP_NOFS);
  309. if (!snapc)
  310. goto fail;
  311. /* build (reverse sorted) snap vector */
  312. num = 0;
  313. snapc->seq = realm->seq;
  314. if (parent) {
  315. u32 i;
  316. /* include any of parent's snaps occurring _after_ my
  317. parent became my parent */
  318. for (i = 0; i < parent->cached_context->num_snaps; i++)
  319. if (parent->cached_context->snaps[i] >=
  320. realm->parent_since)
  321. snapc->snaps[num++] =
  322. parent->cached_context->snaps[i];
  323. if (parent->cached_context->seq > snapc->seq)
  324. snapc->seq = parent->cached_context->seq;
  325. }
  326. memcpy(snapc->snaps + num, realm->snaps,
  327. sizeof(u64)*realm->num_snaps);
  328. num += realm->num_snaps;
  329. memcpy(snapc->snaps + num, realm->prior_parent_snaps,
  330. sizeof(u64)*realm->num_prior_parent_snaps);
  331. num += realm->num_prior_parent_snaps;
  332. sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
  333. snapc->num_snaps = num;
  334. dout("build_snap_context %llx %p: %p seq %lld (%u snaps)\n",
  335. realm->ino, realm, snapc, snapc->seq,
  336. (unsigned int) snapc->num_snaps);
  337. if (realm->cached_context) {
  338. ceph_put_snap_context(realm->cached_context);
  339. /* queue realm for cap_snap creation */
  340. list_add_tail(&realm->dirty_item, dirty_realms);
  341. }
  342. realm->cached_context = snapc;
  343. return 0;
  344. fail:
  345. /*
  346. * if we fail, clear old (incorrect) cached_context... hopefully
  347. * we'll have better luck building it later
  348. */
  349. if (realm->cached_context) {
  350. ceph_put_snap_context(realm->cached_context);
  351. realm->cached_context = NULL;
  352. }
  353. pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
  354. realm, err);
  355. return err;
  356. }
  357. /*
  358. * rebuild snap context for the given realm and all of its children.
  359. */
  360. static void rebuild_snap_realms(struct ceph_snap_realm *realm,
  361. struct list_head *dirty_realms)
  362. {
  363. struct ceph_snap_realm *child;
  364. dout("rebuild_snap_realms %llx %p\n", realm->ino, realm);
  365. build_snap_context(realm, dirty_realms);
  366. list_for_each_entry(child, &realm->children, child_item)
  367. rebuild_snap_realms(child, dirty_realms);
  368. }
  369. /*
  370. * helper to allocate and decode an array of snapids. free prior
  371. * instance, if any.
  372. */
  373. static int dup_array(u64 **dst, __le64 *src, u32 num)
  374. {
  375. u32 i;
  376. kfree(*dst);
  377. if (num) {
  378. *dst = kcalloc(num, sizeof(u64), GFP_NOFS);
  379. if (!*dst)
  380. return -ENOMEM;
  381. for (i = 0; i < num; i++)
  382. (*dst)[i] = get_unaligned_le64(src + i);
  383. } else {
  384. *dst = NULL;
  385. }
  386. return 0;
  387. }
  388. static bool has_new_snaps(struct ceph_snap_context *o,
  389. struct ceph_snap_context *n)
  390. {
  391. if (n->num_snaps == 0)
  392. return false;
  393. /* snaps are in descending order */
  394. return n->snaps[0] > o->seq;
  395. }
  396. /*
  397. * When a snapshot is applied, the size/mtime inode metadata is queued
  398. * in a ceph_cap_snap (one for each snapshot) until writeback
  399. * completes and the metadata can be flushed back to the MDS.
  400. *
  401. * However, if a (sync) write is currently in-progress when we apply
  402. * the snapshot, we have to wait until the write succeeds or fails
  403. * (and a final size/mtime is known). In this case the
  404. * cap_snap->writing = 1, and is said to be "pending." When the write
  405. * finishes, we __ceph_finish_cap_snap().
  406. *
  407. * Caller must hold snap_rwsem for read (i.e., the realm topology won't
  408. * change).
  409. */
  410. void ceph_queue_cap_snap(struct ceph_inode_info *ci)
  411. {
  412. struct inode *inode = &ci->vfs_inode;
  413. struct ceph_cap_snap *capsnap;
  414. struct ceph_snap_context *old_snapc, *new_snapc;
  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. __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_head_snapc) {
  508. ci->i_head_snapc = ceph_get_snap_context(new_snapc);
  509. dout(" new snapc is %p\n", new_snapc);
  510. }
  511. spin_unlock(&ci->i_ceph_lock);
  512. kfree(capsnap);
  513. ceph_put_snap_context(old_snapc);
  514. }
  515. /*
  516. * Finalize the size, mtime for a cap_snap.. that is, settle on final values
  517. * to be used for the snapshot, to be flushed back to the mds.
  518. *
  519. * If capsnap can now be flushed, add to snap_flush list, and return 1.
  520. *
  521. * Caller must hold i_ceph_lock.
  522. */
  523. int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
  524. struct ceph_cap_snap *capsnap)
  525. {
  526. struct inode *inode = &ci->vfs_inode;
  527. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  528. BUG_ON(capsnap->writing);
  529. capsnap->size = inode->i_size;
  530. capsnap->mtime = inode->i_mtime;
  531. capsnap->atime = inode->i_atime;
  532. capsnap->ctime = inode->i_ctime;
  533. capsnap->time_warp_seq = ci->i_time_warp_seq;
  534. capsnap->truncate_size = ci->i_truncate_size;
  535. capsnap->truncate_seq = ci->i_truncate_seq;
  536. if (capsnap->dirty_pages) {
  537. dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
  538. "still has %d dirty pages\n", inode, capsnap,
  539. capsnap->context, capsnap->context->seq,
  540. ceph_cap_string(capsnap->dirty), capsnap->size,
  541. capsnap->dirty_pages);
  542. return 0;
  543. }
  544. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  545. dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
  546. inode, capsnap, capsnap->context,
  547. capsnap->context->seq, ceph_cap_string(capsnap->dirty),
  548. capsnap->size);
  549. spin_lock(&mdsc->snap_flush_lock);
  550. list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
  551. spin_unlock(&mdsc->snap_flush_lock);
  552. return 1; /* caller may want to ceph_flush_snaps */
  553. }
  554. /*
  555. * Queue cap_snaps for snap writeback for this realm and its children.
  556. * Called under snap_rwsem, so realm topology won't change.
  557. */
  558. static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
  559. {
  560. struct ceph_inode_info *ci;
  561. struct inode *lastinode = NULL;
  562. dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
  563. spin_lock(&realm->inodes_with_caps_lock);
  564. list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
  565. struct inode *inode = igrab(&ci->vfs_inode);
  566. if (!inode)
  567. continue;
  568. spin_unlock(&realm->inodes_with_caps_lock);
  569. iput(lastinode);
  570. lastinode = inode;
  571. ceph_queue_cap_snap(ci);
  572. spin_lock(&realm->inodes_with_caps_lock);
  573. }
  574. spin_unlock(&realm->inodes_with_caps_lock);
  575. iput(lastinode);
  576. dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
  577. }
  578. /*
  579. * Parse and apply a snapblob "snap trace" from the MDS. This specifies
  580. * the snap realm parameters from a given realm and all of its ancestors,
  581. * up to the root.
  582. *
  583. * Caller must hold snap_rwsem for write.
  584. */
  585. int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
  586. void *p, void *e, bool deletion,
  587. struct ceph_snap_realm **realm_ret)
  588. {
  589. struct ceph_mds_snap_realm *ri; /* encoded */
  590. __le64 *snaps; /* encoded */
  591. __le64 *prior_parent_snaps; /* encoded */
  592. struct ceph_snap_realm *realm = NULL;
  593. struct ceph_snap_realm *first_realm = NULL;
  594. int invalidate = 0;
  595. int err = -ENOMEM;
  596. LIST_HEAD(dirty_realms);
  597. dout("update_snap_trace deletion=%d\n", deletion);
  598. more:
  599. ceph_decode_need(&p, e, sizeof(*ri), bad);
  600. ri = p;
  601. p += sizeof(*ri);
  602. ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
  603. le32_to_cpu(ri->num_prior_parent_snaps)), bad);
  604. snaps = p;
  605. p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
  606. prior_parent_snaps = p;
  607. p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
  608. realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
  609. if (!realm) {
  610. realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
  611. if (IS_ERR(realm)) {
  612. err = PTR_ERR(realm);
  613. goto fail;
  614. }
  615. }
  616. /* ensure the parent is correct */
  617. err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
  618. if (err < 0)
  619. goto fail;
  620. invalidate += err;
  621. if (le64_to_cpu(ri->seq) > realm->seq) {
  622. dout("update_snap_trace updating %llx %p %lld -> %lld\n",
  623. realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
  624. /* update realm parameters, snap lists */
  625. realm->seq = le64_to_cpu(ri->seq);
  626. realm->created = le64_to_cpu(ri->created);
  627. realm->parent_since = le64_to_cpu(ri->parent_since);
  628. realm->num_snaps = le32_to_cpu(ri->num_snaps);
  629. err = dup_array(&realm->snaps, snaps, realm->num_snaps);
  630. if (err < 0)
  631. goto fail;
  632. realm->num_prior_parent_snaps =
  633. le32_to_cpu(ri->num_prior_parent_snaps);
  634. err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
  635. realm->num_prior_parent_snaps);
  636. if (err < 0)
  637. goto fail;
  638. if (realm->seq > mdsc->last_snap_seq)
  639. mdsc->last_snap_seq = realm->seq;
  640. invalidate = 1;
  641. } else if (!realm->cached_context) {
  642. dout("update_snap_trace %llx %p seq %lld new\n",
  643. realm->ino, realm, realm->seq);
  644. invalidate = 1;
  645. } else {
  646. dout("update_snap_trace %llx %p seq %lld unchanged\n",
  647. realm->ino, realm, realm->seq);
  648. }
  649. dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino,
  650. realm, invalidate, p, e);
  651. /* invalidate when we reach the _end_ (root) of the trace */
  652. if (invalidate && p >= e)
  653. rebuild_snap_realms(realm, &dirty_realms);
  654. if (!first_realm)
  655. first_realm = realm;
  656. else
  657. ceph_put_snap_realm(mdsc, realm);
  658. if (p < e)
  659. goto more;
  660. /*
  661. * queue cap snaps _after_ we've built the new snap contexts,
  662. * so that i_head_snapc can be set appropriately.
  663. */
  664. while (!list_empty(&dirty_realms)) {
  665. realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
  666. dirty_item);
  667. list_del_init(&realm->dirty_item);
  668. queue_realm_cap_snaps(realm);
  669. }
  670. if (realm_ret)
  671. *realm_ret = first_realm;
  672. else
  673. ceph_put_snap_realm(mdsc, first_realm);
  674. __cleanup_empty_realms(mdsc);
  675. return 0;
  676. bad:
  677. err = -EINVAL;
  678. fail:
  679. if (realm && !IS_ERR(realm))
  680. ceph_put_snap_realm(mdsc, realm);
  681. if (first_realm)
  682. ceph_put_snap_realm(mdsc, first_realm);
  683. pr_err("update_snap_trace error %d\n", err);
  684. return err;
  685. }
  686. /*
  687. * Send any cap_snaps that are queued for flush. Try to carry
  688. * s_mutex across multiple snap flushes to avoid locking overhead.
  689. *
  690. * Caller holds no locks.
  691. */
  692. static void flush_snaps(struct ceph_mds_client *mdsc)
  693. {
  694. struct ceph_inode_info *ci;
  695. struct inode *inode;
  696. struct ceph_mds_session *session = NULL;
  697. dout("flush_snaps\n");
  698. spin_lock(&mdsc->snap_flush_lock);
  699. while (!list_empty(&mdsc->snap_flush_list)) {
  700. ci = list_first_entry(&mdsc->snap_flush_list,
  701. struct ceph_inode_info, i_snap_flush_item);
  702. inode = &ci->vfs_inode;
  703. ihold(inode);
  704. spin_unlock(&mdsc->snap_flush_lock);
  705. ceph_flush_snaps(ci, &session);
  706. iput(inode);
  707. spin_lock(&mdsc->snap_flush_lock);
  708. }
  709. spin_unlock(&mdsc->snap_flush_lock);
  710. if (session) {
  711. mutex_unlock(&session->s_mutex);
  712. ceph_put_mds_session(session);
  713. }
  714. dout("flush_snaps done\n");
  715. }
  716. /*
  717. * Handle a snap notification from the MDS.
  718. *
  719. * This can take two basic forms: the simplest is just a snap creation
  720. * or deletion notification on an existing realm. This should update the
  721. * realm and its children.
  722. *
  723. * The more difficult case is realm creation, due to snap creation at a
  724. * new point in the file hierarchy, or due to a rename that moves a file or
  725. * directory into another realm.
  726. */
  727. void ceph_handle_snap(struct ceph_mds_client *mdsc,
  728. struct ceph_mds_session *session,
  729. struct ceph_msg *msg)
  730. {
  731. struct super_block *sb = mdsc->fsc->sb;
  732. int mds = session->s_mds;
  733. u64 split;
  734. int op;
  735. int trace_len;
  736. struct ceph_snap_realm *realm = NULL;
  737. void *p = msg->front.iov_base;
  738. void *e = p + msg->front.iov_len;
  739. struct ceph_mds_snap_head *h;
  740. int num_split_inos, num_split_realms;
  741. __le64 *split_inos = NULL, *split_realms = NULL;
  742. int i;
  743. int locked_rwsem = 0;
  744. /* decode */
  745. if (msg->front.iov_len < sizeof(*h))
  746. goto bad;
  747. h = p;
  748. op = le32_to_cpu(h->op);
  749. split = le64_to_cpu(h->split); /* non-zero if we are splitting an
  750. * existing realm */
  751. num_split_inos = le32_to_cpu(h->num_split_inos);
  752. num_split_realms = le32_to_cpu(h->num_split_realms);
  753. trace_len = le32_to_cpu(h->trace_len);
  754. p += sizeof(*h);
  755. dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
  756. ceph_snap_op_name(op), split, trace_len);
  757. mutex_lock(&session->s_mutex);
  758. session->s_seq++;
  759. mutex_unlock(&session->s_mutex);
  760. down_write(&mdsc->snap_rwsem);
  761. locked_rwsem = 1;
  762. if (op == CEPH_SNAP_OP_SPLIT) {
  763. struct ceph_mds_snap_realm *ri;
  764. /*
  765. * A "split" breaks part of an existing realm off into
  766. * a new realm. The MDS provides a list of inodes
  767. * (with caps) and child realms that belong to the new
  768. * child.
  769. */
  770. split_inos = p;
  771. p += sizeof(u64) * num_split_inos;
  772. split_realms = p;
  773. p += sizeof(u64) * num_split_realms;
  774. ceph_decode_need(&p, e, sizeof(*ri), bad);
  775. /* we will peek at realm info here, but will _not_
  776. * advance p, as the realm update will occur below in
  777. * ceph_update_snap_trace. */
  778. ri = p;
  779. realm = ceph_lookup_snap_realm(mdsc, split);
  780. if (!realm) {
  781. realm = ceph_create_snap_realm(mdsc, split);
  782. if (IS_ERR(realm))
  783. goto out;
  784. }
  785. dout("splitting snap_realm %llx %p\n", realm->ino, realm);
  786. for (i = 0; i < num_split_inos; i++) {
  787. struct ceph_vino vino = {
  788. .ino = le64_to_cpu(split_inos[i]),
  789. .snap = CEPH_NOSNAP,
  790. };
  791. struct inode *inode = ceph_find_inode(sb, vino);
  792. struct ceph_inode_info *ci;
  793. struct ceph_snap_realm *oldrealm;
  794. if (!inode)
  795. continue;
  796. ci = ceph_inode(inode);
  797. spin_lock(&ci->i_ceph_lock);
  798. if (!ci->i_snap_realm)
  799. goto skip_inode;
  800. /*
  801. * If this inode belongs to a realm that was
  802. * created after our new realm, we experienced
  803. * a race (due to another split notifications
  804. * arriving from a different MDS). So skip
  805. * this inode.
  806. */
  807. if (ci->i_snap_realm->created >
  808. le64_to_cpu(ri->created)) {
  809. dout(" leaving %p in newer realm %llx %p\n",
  810. inode, ci->i_snap_realm->ino,
  811. ci->i_snap_realm);
  812. goto skip_inode;
  813. }
  814. dout(" will move %p to split realm %llx %p\n",
  815. inode, realm->ino, realm);
  816. /*
  817. * Move the inode to the new realm
  818. */
  819. spin_lock(&realm->inodes_with_caps_lock);
  820. list_del_init(&ci->i_snap_realm_item);
  821. list_add(&ci->i_snap_realm_item,
  822. &realm->inodes_with_caps);
  823. oldrealm = ci->i_snap_realm;
  824. ci->i_snap_realm = realm;
  825. spin_unlock(&realm->inodes_with_caps_lock);
  826. spin_unlock(&ci->i_ceph_lock);
  827. ceph_get_snap_realm(mdsc, realm);
  828. ceph_put_snap_realm(mdsc, oldrealm);
  829. iput(inode);
  830. continue;
  831. skip_inode:
  832. spin_unlock(&ci->i_ceph_lock);
  833. iput(inode);
  834. }
  835. /* we may have taken some of the old realm's children. */
  836. for (i = 0; i < num_split_realms; i++) {
  837. struct ceph_snap_realm *child =
  838. __lookup_snap_realm(mdsc,
  839. le64_to_cpu(split_realms[i]));
  840. if (!child)
  841. continue;
  842. adjust_snap_realm_parent(mdsc, child, realm->ino);
  843. }
  844. }
  845. /*
  846. * update using the provided snap trace. if we are deleting a
  847. * snap, we can avoid queueing cap_snaps.
  848. */
  849. ceph_update_snap_trace(mdsc, p, e,
  850. op == CEPH_SNAP_OP_DESTROY, NULL);
  851. if (op == CEPH_SNAP_OP_SPLIT)
  852. /* we took a reference when we created the realm, above */
  853. ceph_put_snap_realm(mdsc, realm);
  854. __cleanup_empty_realms(mdsc);
  855. up_write(&mdsc->snap_rwsem);
  856. flush_snaps(mdsc);
  857. return;
  858. bad:
  859. pr_err("corrupt snap message from mds%d\n", mds);
  860. ceph_msg_dump(msg);
  861. out:
  862. if (locked_rwsem)
  863. up_write(&mdsc->snap_rwsem);
  864. return;
  865. }