snap.c 26 KB

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