snap.c 28 KB

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