snap.c 26 KB

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