snap.c 25 KB

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