snap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. static struct ceph_snap_context *empty_snapc;
  267. /*
  268. * build the snap context for a given realm.
  269. */
  270. static int build_snap_context(struct ceph_snap_realm *realm)
  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);
  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. if (num == 0 && realm->seq == empty_snapc->seq) {
  305. ceph_get_snap_context(empty_snapc);
  306. snapc = empty_snapc;
  307. goto done;
  308. }
  309. /* alloc new snap context */
  310. err = -ENOMEM;
  311. if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
  312. goto fail;
  313. snapc = ceph_create_snap_context(num, GFP_NOFS);
  314. if (!snapc)
  315. goto fail;
  316. /* build (reverse sorted) snap vector */
  317. num = 0;
  318. snapc->seq = realm->seq;
  319. if (parent) {
  320. u32 i;
  321. /* include any of parent's snaps occurring _after_ my
  322. parent became my parent */
  323. for (i = 0; i < parent->cached_context->num_snaps; i++)
  324. if (parent->cached_context->snaps[i] >=
  325. realm->parent_since)
  326. snapc->snaps[num++] =
  327. parent->cached_context->snaps[i];
  328. if (parent->cached_context->seq > snapc->seq)
  329. snapc->seq = parent->cached_context->seq;
  330. }
  331. memcpy(snapc->snaps + num, realm->snaps,
  332. sizeof(u64)*realm->num_snaps);
  333. num += realm->num_snaps;
  334. memcpy(snapc->snaps + num, realm->prior_parent_snaps,
  335. sizeof(u64)*realm->num_prior_parent_snaps);
  336. num += realm->num_prior_parent_snaps;
  337. sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
  338. snapc->num_snaps = num;
  339. dout("build_snap_context %llx %p: %p seq %lld (%u snaps)\n",
  340. realm->ino, realm, snapc, snapc->seq,
  341. (unsigned int) snapc->num_snaps);
  342. done:
  343. ceph_put_snap_context(realm->cached_context);
  344. realm->cached_context = snapc;
  345. return 0;
  346. fail:
  347. /*
  348. * if we fail, clear old (incorrect) cached_context... hopefully
  349. * we'll have better luck building it later
  350. */
  351. if (realm->cached_context) {
  352. ceph_put_snap_context(realm->cached_context);
  353. realm->cached_context = NULL;
  354. }
  355. pr_err("build_snap_context %llx %p fail %d\n", realm->ino,
  356. realm, err);
  357. return err;
  358. }
  359. /*
  360. * rebuild snap context for the given realm and all of its children.
  361. */
  362. static void rebuild_snap_realms(struct ceph_snap_realm *realm)
  363. {
  364. struct ceph_snap_realm *child;
  365. dout("rebuild_snap_realms %llx %p\n", realm->ino, realm);
  366. build_snap_context(realm);
  367. list_for_each_entry(child, &realm->children, child_item)
  368. rebuild_snap_realms(child);
  369. }
  370. /*
  371. * helper to allocate and decode an array of snapids. free prior
  372. * instance, if any.
  373. */
  374. static int dup_array(u64 **dst, __le64 *src, u32 num)
  375. {
  376. u32 i;
  377. kfree(*dst);
  378. if (num) {
  379. *dst = kcalloc(num, sizeof(u64), GFP_NOFS);
  380. if (!*dst)
  381. return -ENOMEM;
  382. for (i = 0; i < num; i++)
  383. (*dst)[i] = get_unaligned_le64(src + i);
  384. } else {
  385. *dst = NULL;
  386. }
  387. return 0;
  388. }
  389. /*
  390. * When a snapshot is applied, the size/mtime inode metadata is queued
  391. * in a ceph_cap_snap (one for each snapshot) until writeback
  392. * completes and the metadata can be flushed back to the MDS.
  393. *
  394. * However, if a (sync) write is currently in-progress when we apply
  395. * the snapshot, we have to wait until the write succeeds or fails
  396. * (and a final size/mtime is known). In this case the
  397. * cap_snap->writing = 1, and is said to be "pending." When the write
  398. * finishes, we __ceph_finish_cap_snap().
  399. *
  400. * Caller must hold snap_rwsem for read (i.e., the realm topology won't
  401. * change).
  402. */
  403. void ceph_queue_cap_snap(struct ceph_inode_info *ci)
  404. {
  405. struct inode *inode = &ci->vfs_inode;
  406. struct ceph_cap_snap *capsnap;
  407. int used, dirty;
  408. capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS);
  409. if (!capsnap) {
  410. pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode);
  411. return;
  412. }
  413. spin_lock(&ci->i_ceph_lock);
  414. used = __ceph_caps_used(ci);
  415. dirty = __ceph_caps_dirty(ci);
  416. /*
  417. * If there is a write in progress, treat that as a dirty Fw,
  418. * even though it hasn't completed yet; by the time we finish
  419. * up this capsnap it will be.
  420. */
  421. if (used & CEPH_CAP_FILE_WR)
  422. dirty |= CEPH_CAP_FILE_WR;
  423. if (__ceph_have_pending_cap_snap(ci)) {
  424. /* there is no point in queuing multiple "pending" cap_snaps,
  425. as no new writes are allowed to start when pending, so any
  426. writes in progress now were started before the previous
  427. cap_snap. lucky us. */
  428. dout("queue_cap_snap %p already pending\n", inode);
  429. kfree(capsnap);
  430. } else if (ci->i_snap_realm->cached_context == empty_snapc) {
  431. dout("queue_cap_snap %p empty snapc\n", inode);
  432. kfree(capsnap);
  433. } else if (dirty & (CEPH_CAP_AUTH_EXCL|CEPH_CAP_XATTR_EXCL|
  434. CEPH_CAP_FILE_EXCL|CEPH_CAP_FILE_WR)) {
  435. struct ceph_snap_context *snapc = ci->i_head_snapc;
  436. /*
  437. * if we are a sync write, we may need to go to the snaprealm
  438. * to get the current snapc.
  439. */
  440. if (!snapc)
  441. snapc = ci->i_snap_realm->cached_context;
  442. dout("queue_cap_snap %p cap_snap %p queuing under %p %s\n",
  443. inode, capsnap, snapc, ceph_cap_string(dirty));
  444. ihold(inode);
  445. atomic_set(&capsnap->nref, 1);
  446. capsnap->ci = ci;
  447. INIT_LIST_HEAD(&capsnap->ci_item);
  448. INIT_LIST_HEAD(&capsnap->flushing_item);
  449. capsnap->follows = snapc->seq;
  450. capsnap->issued = __ceph_caps_issued(ci, NULL);
  451. capsnap->dirty = dirty;
  452. capsnap->mode = inode->i_mode;
  453. capsnap->uid = inode->i_uid;
  454. capsnap->gid = inode->i_gid;
  455. if (dirty & CEPH_CAP_XATTR_EXCL) {
  456. __ceph_build_xattrs_blob(ci);
  457. capsnap->xattr_blob =
  458. ceph_buffer_get(ci->i_xattrs.blob);
  459. capsnap->xattr_version = ci->i_xattrs.version;
  460. } else {
  461. capsnap->xattr_blob = NULL;
  462. capsnap->xattr_version = 0;
  463. }
  464. capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  465. /* dirty page count moved from _head to this cap_snap;
  466. all subsequent writes page dirties occur _after_ this
  467. snapshot. */
  468. capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
  469. ci->i_wrbuffer_ref_head = 0;
  470. capsnap->context = snapc;
  471. ci->i_head_snapc =
  472. ceph_get_snap_context(ci->i_snap_realm->cached_context);
  473. dout(" new snapc is %p\n", ci->i_head_snapc);
  474. list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
  475. if (used & CEPH_CAP_FILE_WR) {
  476. dout("queue_cap_snap %p cap_snap %p snapc %p"
  477. " seq %llu used WR, now pending\n", inode,
  478. capsnap, snapc, snapc->seq);
  479. capsnap->writing = 1;
  480. } else {
  481. /* note mtime, size NOW. */
  482. __ceph_finish_cap_snap(ci, capsnap);
  483. }
  484. } else {
  485. dout("queue_cap_snap %p nothing dirty|writing\n", inode);
  486. kfree(capsnap);
  487. }
  488. spin_unlock(&ci->i_ceph_lock);
  489. }
  490. /*
  491. * Finalize the size, mtime for a cap_snap.. that is, settle on final values
  492. * to be used for the snapshot, to be flushed back to the mds.
  493. *
  494. * If capsnap can now be flushed, add to snap_flush list, and return 1.
  495. *
  496. * Caller must hold i_ceph_lock.
  497. */
  498. int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
  499. struct ceph_cap_snap *capsnap)
  500. {
  501. struct inode *inode = &ci->vfs_inode;
  502. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  503. BUG_ON(capsnap->writing);
  504. capsnap->size = inode->i_size;
  505. capsnap->mtime = inode->i_mtime;
  506. capsnap->atime = inode->i_atime;
  507. capsnap->ctime = inode->i_ctime;
  508. capsnap->time_warp_seq = ci->i_time_warp_seq;
  509. if (capsnap->dirty_pages) {
  510. dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu "
  511. "still has %d dirty pages\n", inode, capsnap,
  512. capsnap->context, capsnap->context->seq,
  513. ceph_cap_string(capsnap->dirty), capsnap->size,
  514. capsnap->dirty_pages);
  515. return 0;
  516. }
  517. dout("finish_cap_snap %p cap_snap %p snapc %p %llu %s s=%llu\n",
  518. inode, capsnap, capsnap->context,
  519. capsnap->context->seq, ceph_cap_string(capsnap->dirty),
  520. capsnap->size);
  521. spin_lock(&mdsc->snap_flush_lock);
  522. list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
  523. spin_unlock(&mdsc->snap_flush_lock);
  524. return 1; /* caller may want to ceph_flush_snaps */
  525. }
  526. /*
  527. * Queue cap_snaps for snap writeback for this realm and its children.
  528. * Called under snap_rwsem, so realm topology won't change.
  529. */
  530. static void queue_realm_cap_snaps(struct ceph_snap_realm *realm)
  531. {
  532. struct ceph_inode_info *ci;
  533. struct inode *lastinode = NULL;
  534. struct ceph_snap_realm *child;
  535. dout("queue_realm_cap_snaps %p %llx inodes\n", realm, realm->ino);
  536. spin_lock(&realm->inodes_with_caps_lock);
  537. list_for_each_entry(ci, &realm->inodes_with_caps,
  538. i_snap_realm_item) {
  539. struct inode *inode = igrab(&ci->vfs_inode);
  540. if (!inode)
  541. continue;
  542. spin_unlock(&realm->inodes_with_caps_lock);
  543. iput(lastinode);
  544. lastinode = inode;
  545. ceph_queue_cap_snap(ci);
  546. spin_lock(&realm->inodes_with_caps_lock);
  547. }
  548. spin_unlock(&realm->inodes_with_caps_lock);
  549. iput(lastinode);
  550. list_for_each_entry(child, &realm->children, child_item) {
  551. dout("queue_realm_cap_snaps %p %llx queue child %p %llx\n",
  552. realm, realm->ino, child, child->ino);
  553. list_del_init(&child->dirty_item);
  554. list_add(&child->dirty_item, &realm->dirty_item);
  555. }
  556. list_del_init(&realm->dirty_item);
  557. dout("queue_realm_cap_snaps %p %llx done\n", realm, realm->ino);
  558. }
  559. /*
  560. * Parse and apply a snapblob "snap trace" from the MDS. This specifies
  561. * the snap realm parameters from a given realm and all of its ancestors,
  562. * up to the root.
  563. *
  564. * Caller must hold snap_rwsem for write.
  565. */
  566. int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
  567. void *p, void *e, bool deletion,
  568. struct ceph_snap_realm **realm_ret)
  569. {
  570. struct ceph_mds_snap_realm *ri; /* encoded */
  571. __le64 *snaps; /* encoded */
  572. __le64 *prior_parent_snaps; /* encoded */
  573. struct ceph_snap_realm *realm = NULL;
  574. struct ceph_snap_realm *first_realm = NULL;
  575. int invalidate = 0;
  576. int err = -ENOMEM;
  577. LIST_HEAD(dirty_realms);
  578. dout("update_snap_trace deletion=%d\n", deletion);
  579. more:
  580. ceph_decode_need(&p, e, sizeof(*ri), bad);
  581. ri = p;
  582. p += sizeof(*ri);
  583. ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
  584. le32_to_cpu(ri->num_prior_parent_snaps)), bad);
  585. snaps = p;
  586. p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
  587. prior_parent_snaps = p;
  588. p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
  589. realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
  590. if (!realm) {
  591. realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
  592. if (IS_ERR(realm)) {
  593. err = PTR_ERR(realm);
  594. goto fail;
  595. }
  596. }
  597. /* ensure the parent is correct */
  598. err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
  599. if (err < 0)
  600. goto fail;
  601. invalidate += err;
  602. if (le64_to_cpu(ri->seq) > realm->seq) {
  603. dout("update_snap_trace updating %llx %p %lld -> %lld\n",
  604. realm->ino, realm, realm->seq, le64_to_cpu(ri->seq));
  605. /* update realm parameters, snap lists */
  606. realm->seq = le64_to_cpu(ri->seq);
  607. realm->created = le64_to_cpu(ri->created);
  608. realm->parent_since = le64_to_cpu(ri->parent_since);
  609. realm->num_snaps = le32_to_cpu(ri->num_snaps);
  610. err = dup_array(&realm->snaps, snaps, realm->num_snaps);
  611. if (err < 0)
  612. goto fail;
  613. realm->num_prior_parent_snaps =
  614. le32_to_cpu(ri->num_prior_parent_snaps);
  615. err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
  616. realm->num_prior_parent_snaps);
  617. if (err < 0)
  618. goto fail;
  619. /* queue realm for cap_snap creation */
  620. list_add(&realm->dirty_item, &dirty_realms);
  621. invalidate = 1;
  622. } else if (!realm->cached_context) {
  623. dout("update_snap_trace %llx %p seq %lld new\n",
  624. realm->ino, realm, realm->seq);
  625. invalidate = 1;
  626. } else {
  627. dout("update_snap_trace %llx %p seq %lld unchanged\n",
  628. realm->ino, realm, realm->seq);
  629. }
  630. dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino,
  631. realm, invalidate, p, e);
  632. /* invalidate when we reach the _end_ (root) of the trace */
  633. if (invalidate && p >= e)
  634. rebuild_snap_realms(realm);
  635. if (!first_realm)
  636. first_realm = realm;
  637. else
  638. ceph_put_snap_realm(mdsc, realm);
  639. if (p < e)
  640. goto more;
  641. /*
  642. * queue cap snaps _after_ we've built the new snap contexts,
  643. * so that i_head_snapc can be set appropriately.
  644. */
  645. while (!list_empty(&dirty_realms)) {
  646. realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
  647. dirty_item);
  648. queue_realm_cap_snaps(realm);
  649. }
  650. if (realm_ret)
  651. *realm_ret = first_realm;
  652. else
  653. ceph_put_snap_realm(mdsc, first_realm);
  654. __cleanup_empty_realms(mdsc);
  655. return 0;
  656. bad:
  657. err = -EINVAL;
  658. fail:
  659. if (realm && !IS_ERR(realm))
  660. ceph_put_snap_realm(mdsc, realm);
  661. if (first_realm)
  662. ceph_put_snap_realm(mdsc, first_realm);
  663. pr_err("update_snap_trace error %d\n", err);
  664. return err;
  665. }
  666. /*
  667. * Send any cap_snaps that are queued for flush. Try to carry
  668. * s_mutex across multiple snap flushes to avoid locking overhead.
  669. *
  670. * Caller holds no locks.
  671. */
  672. static void flush_snaps(struct ceph_mds_client *mdsc)
  673. {
  674. struct ceph_inode_info *ci;
  675. struct inode *inode;
  676. struct ceph_mds_session *session = NULL;
  677. dout("flush_snaps\n");
  678. spin_lock(&mdsc->snap_flush_lock);
  679. while (!list_empty(&mdsc->snap_flush_list)) {
  680. ci = list_first_entry(&mdsc->snap_flush_list,
  681. struct ceph_inode_info, i_snap_flush_item);
  682. inode = &ci->vfs_inode;
  683. ihold(inode);
  684. spin_unlock(&mdsc->snap_flush_lock);
  685. spin_lock(&ci->i_ceph_lock);
  686. __ceph_flush_snaps(ci, &session, 0);
  687. spin_unlock(&ci->i_ceph_lock);
  688. iput(inode);
  689. spin_lock(&mdsc->snap_flush_lock);
  690. }
  691. spin_unlock(&mdsc->snap_flush_lock);
  692. if (session) {
  693. mutex_unlock(&session->s_mutex);
  694. ceph_put_mds_session(session);
  695. }
  696. dout("flush_snaps done\n");
  697. }
  698. /*
  699. * Handle a snap notification from the MDS.
  700. *
  701. * This can take two basic forms: the simplest is just a snap creation
  702. * or deletion notification on an existing realm. This should update the
  703. * realm and its children.
  704. *
  705. * The more difficult case is realm creation, due to snap creation at a
  706. * new point in the file hierarchy, or due to a rename that moves a file or
  707. * directory into another realm.
  708. */
  709. void ceph_handle_snap(struct ceph_mds_client *mdsc,
  710. struct ceph_mds_session *session,
  711. struct ceph_msg *msg)
  712. {
  713. struct super_block *sb = mdsc->fsc->sb;
  714. int mds = session->s_mds;
  715. u64 split;
  716. int op;
  717. int trace_len;
  718. struct ceph_snap_realm *realm = NULL;
  719. void *p = msg->front.iov_base;
  720. void *e = p + msg->front.iov_len;
  721. struct ceph_mds_snap_head *h;
  722. int num_split_inos, num_split_realms;
  723. __le64 *split_inos = NULL, *split_realms = NULL;
  724. int i;
  725. int locked_rwsem = 0;
  726. /* decode */
  727. if (msg->front.iov_len < sizeof(*h))
  728. goto bad;
  729. h = p;
  730. op = le32_to_cpu(h->op);
  731. split = le64_to_cpu(h->split); /* non-zero if we are splitting an
  732. * existing realm */
  733. num_split_inos = le32_to_cpu(h->num_split_inos);
  734. num_split_realms = le32_to_cpu(h->num_split_realms);
  735. trace_len = le32_to_cpu(h->trace_len);
  736. p += sizeof(*h);
  737. dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds,
  738. ceph_snap_op_name(op), split, trace_len);
  739. mutex_lock(&session->s_mutex);
  740. session->s_seq++;
  741. mutex_unlock(&session->s_mutex);
  742. down_write(&mdsc->snap_rwsem);
  743. locked_rwsem = 1;
  744. if (op == CEPH_SNAP_OP_SPLIT) {
  745. struct ceph_mds_snap_realm *ri;
  746. /*
  747. * A "split" breaks part of an existing realm off into
  748. * a new realm. The MDS provides a list of inodes
  749. * (with caps) and child realms that belong to the new
  750. * child.
  751. */
  752. split_inos = p;
  753. p += sizeof(u64) * num_split_inos;
  754. split_realms = p;
  755. p += sizeof(u64) * num_split_realms;
  756. ceph_decode_need(&p, e, sizeof(*ri), bad);
  757. /* we will peek at realm info here, but will _not_
  758. * advance p, as the realm update will occur below in
  759. * ceph_update_snap_trace. */
  760. ri = p;
  761. realm = ceph_lookup_snap_realm(mdsc, split);
  762. if (!realm) {
  763. realm = ceph_create_snap_realm(mdsc, split);
  764. if (IS_ERR(realm))
  765. goto out;
  766. }
  767. dout("splitting snap_realm %llx %p\n", realm->ino, realm);
  768. for (i = 0; i < num_split_inos; i++) {
  769. struct ceph_vino vino = {
  770. .ino = le64_to_cpu(split_inos[i]),
  771. .snap = CEPH_NOSNAP,
  772. };
  773. struct inode *inode = ceph_find_inode(sb, vino);
  774. struct ceph_inode_info *ci;
  775. struct ceph_snap_realm *oldrealm;
  776. if (!inode)
  777. continue;
  778. ci = ceph_inode(inode);
  779. spin_lock(&ci->i_ceph_lock);
  780. if (!ci->i_snap_realm)
  781. goto skip_inode;
  782. /*
  783. * If this inode belongs to a realm that was
  784. * created after our new realm, we experienced
  785. * a race (due to another split notifications
  786. * arriving from a different MDS). So skip
  787. * this inode.
  788. */
  789. if (ci->i_snap_realm->created >
  790. le64_to_cpu(ri->created)) {
  791. dout(" leaving %p in newer realm %llx %p\n",
  792. inode, ci->i_snap_realm->ino,
  793. ci->i_snap_realm);
  794. goto skip_inode;
  795. }
  796. dout(" will move %p to split realm %llx %p\n",
  797. inode, realm->ino, realm);
  798. /*
  799. * Move the inode to the new realm
  800. */
  801. spin_lock(&realm->inodes_with_caps_lock);
  802. list_del_init(&ci->i_snap_realm_item);
  803. list_add(&ci->i_snap_realm_item,
  804. &realm->inodes_with_caps);
  805. oldrealm = ci->i_snap_realm;
  806. ci->i_snap_realm = realm;
  807. spin_unlock(&realm->inodes_with_caps_lock);
  808. spin_unlock(&ci->i_ceph_lock);
  809. ceph_get_snap_realm(mdsc, realm);
  810. ceph_put_snap_realm(mdsc, oldrealm);
  811. iput(inode);
  812. continue;
  813. skip_inode:
  814. spin_unlock(&ci->i_ceph_lock);
  815. iput(inode);
  816. }
  817. /* we may have taken some of the old realm's children. */
  818. for (i = 0; i < num_split_realms; i++) {
  819. struct ceph_snap_realm *child =
  820. __lookup_snap_realm(mdsc,
  821. le64_to_cpu(split_realms[i]));
  822. if (!child)
  823. continue;
  824. adjust_snap_realm_parent(mdsc, child, realm->ino);
  825. }
  826. }
  827. /*
  828. * update using the provided snap trace. if we are deleting a
  829. * snap, we can avoid queueing cap_snaps.
  830. */
  831. ceph_update_snap_trace(mdsc, p, e,
  832. op == CEPH_SNAP_OP_DESTROY, NULL);
  833. if (op == CEPH_SNAP_OP_SPLIT)
  834. /* we took a reference when we created the realm, above */
  835. ceph_put_snap_realm(mdsc, realm);
  836. __cleanup_empty_realms(mdsc);
  837. up_write(&mdsc->snap_rwsem);
  838. flush_snaps(mdsc);
  839. return;
  840. bad:
  841. pr_err("corrupt snap message from mds%d\n", mds);
  842. ceph_msg_dump(msg);
  843. out:
  844. if (locked_rwsem)
  845. up_write(&mdsc->snap_rwsem);
  846. return;
  847. }
  848. int __init ceph_snap_init(void)
  849. {
  850. empty_snapc = ceph_create_snap_context(0, GFP_NOFS);
  851. if (!empty_snapc)
  852. return -ENOMEM;
  853. empty_snapc->seq = 1;
  854. return 0;
  855. }
  856. void ceph_snap_exit(void)
  857. {
  858. ceph_put_snap_context(empty_snapc);
  859. }