snap.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/fs.h>
  4. #include <linux/sort.h>
  5. #include <linux/slab.h>
  6. #include <linux/iversion.h>
  7. #include "super.h"
  8. #include "mds_client.h"
  9. #include <linux/ceph/decode.h>
  10. /* unused map expires after 5 minutes */
  11. #define CEPH_SNAPID_MAP_TIMEOUT (5 * 60 * HZ)
  12. /*
  13. * Snapshots in ceph are driven in large part by cooperation from the
  14. * client. In contrast to local file systems or file servers that
  15. * implement snapshots at a single point in the system, ceph's
  16. * distributed access to storage requires clients to help decide
  17. * whether a write logically occurs before or after a recently created
  18. * snapshot.
  19. *
  20. * This provides a perfect instantanous client-wide snapshot. Between
  21. * clients, however, snapshots may appear to be applied at slightly
  22. * different points in time, depending on delays in delivering the
  23. * snapshot notification.
  24. *
  25. * Snapshots are _not_ file system-wide. Instead, each snapshot
  26. * applies to the subdirectory nested beneath some directory. This
  27. * effectively divides the hierarchy into multiple "realms," where all
  28. * of the files contained by each realm share the same set of
  29. * snapshots. An individual realm's snap set contains snapshots
  30. * explicitly created on that realm, as well as any snaps in its
  31. * parent's snap set _after_ the point at which the parent became it's
  32. * parent (due to, say, a rename). Similarly, snaps from prior parents
  33. * during the time intervals during which they were the parent are included.
  34. *
  35. * The client is spared most of this detail, fortunately... it must only
  36. * maintains a hierarchy of realms reflecting the current parent/child
  37. * realm relationship, and for each realm has an explicit list of snaps
  38. * inherited from prior parents.
  39. *
  40. * A snap_realm struct is maintained for realms containing every inode
  41. * with an open cap in the system. (The needed snap realm information is
  42. * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq'
  43. * version number is used to ensure that as realm parameters change (new
  44. * snapshot, new parent, etc.) the client's realm hierarchy is updated.
  45. *
  46. * The realm hierarchy drives the generation of a 'snap context' for each
  47. * realm, which simply lists the resulting set of snaps for the realm. This
  48. * is attached to any writes sent to OSDs.
  49. */
  50. /*
  51. * Unfortunately error handling is a bit mixed here. If we get a snap
  52. * update, but don't have enough memory to update our realm hierarchy,
  53. * it's not clear what we can do about it (besides complaining to the
  54. * console).
  55. */
  56. /*
  57. * increase ref count for the realm
  58. *
  59. * caller must hold snap_rwsem.
  60. */
  61. void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
  62. struct ceph_snap_realm *realm)
  63. {
  64. lockdep_assert_held(&mdsc->snap_rwsem);
  65. /*
  66. * The 0->1 and 1->0 transitions must take the snap_empty_lock
  67. * atomically with the refcount change. Go ahead and bump the
  68. * nref here, unless it's 0, in which case we take the spinlock
  69. * and then do the increment and remove it from the list.
  70. */
  71. if (atomic_inc_not_zero(&realm->nref))
  72. return;
  73. spin_lock(&mdsc->snap_empty_lock);
  74. if (atomic_inc_return(&realm->nref) == 1)
  75. list_del_init(&realm->empty_item);
  76. spin_unlock(&mdsc->snap_empty_lock);
  77. }
  78. static void __insert_snap_realm(struct rb_root *root,
  79. struct ceph_snap_realm *new)
  80. {
  81. struct rb_node **p = &root->rb_node;
  82. struct rb_node *parent = NULL;
  83. struct ceph_snap_realm *r = NULL;
  84. while (*p) {
  85. parent = *p;
  86. r = rb_entry(parent, struct ceph_snap_realm, node);
  87. if (new->ino < r->ino)
  88. p = &(*p)->rb_left;
  89. else if (new->ino > r->ino)
  90. p = &(*p)->rb_right;
  91. else
  92. BUG();
  93. }
  94. rb_link_node(&new->node, parent, p);
  95. rb_insert_color(&new->node, root);
  96. }
  97. /*
  98. * create and get the realm rooted at @ino and bump its ref count.
  99. *
  100. * caller must hold snap_rwsem for write.
  101. */
  102. static struct ceph_snap_realm *ceph_create_snap_realm(
  103. struct ceph_mds_client *mdsc,
  104. u64 ino)
  105. {
  106. struct ceph_snap_realm *realm;
  107. lockdep_assert_held_write(&mdsc->snap_rwsem);
  108. realm = kzalloc(sizeof(*realm), GFP_NOFS);
  109. if (!realm)
  110. return ERR_PTR(-ENOMEM);
  111. /* Do not release the global dummy snaprealm until unmouting */
  112. if (ino == CEPH_INO_GLOBAL_SNAPREALM)
  113. atomic_set(&realm->nref, 2);
  114. else
  115. atomic_set(&realm->nref, 1);
  116. realm->ino = ino;
  117. INIT_LIST_HEAD(&realm->children);
  118. INIT_LIST_HEAD(&realm->child_item);
  119. INIT_LIST_HEAD(&realm->empty_item);
  120. INIT_LIST_HEAD(&realm->dirty_item);
  121. INIT_LIST_HEAD(&realm->rebuild_item);
  122. INIT_LIST_HEAD(&realm->inodes_with_caps);
  123. spin_lock_init(&realm->inodes_with_caps_lock);
  124. __insert_snap_realm(&mdsc->snap_realms, realm);
  125. mdsc->num_snap_realms++;
  126. doutc(mdsc->fsc->client, "%llx %p\n", realm->ino, realm);
  127. return realm;
  128. }
  129. /*
  130. * lookup the realm rooted at @ino.
  131. *
  132. * caller must hold snap_rwsem.
  133. */
  134. static struct ceph_snap_realm *__lookup_snap_realm(struct ceph_mds_client *mdsc,
  135. u64 ino)
  136. {
  137. struct ceph_client *cl = mdsc->fsc->client;
  138. struct rb_node *n = mdsc->snap_realms.rb_node;
  139. struct ceph_snap_realm *r;
  140. lockdep_assert_held(&mdsc->snap_rwsem);
  141. while (n) {
  142. r = rb_entry(n, struct ceph_snap_realm, node);
  143. if (ino < r->ino)
  144. n = n->rb_left;
  145. else if (ino > r->ino)
  146. n = n->rb_right;
  147. else {
  148. doutc(cl, "%llx %p\n", r->ino, r);
  149. return r;
  150. }
  151. }
  152. return NULL;
  153. }
  154. struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
  155. u64 ino)
  156. {
  157. struct ceph_snap_realm *r;
  158. r = __lookup_snap_realm(mdsc, ino);
  159. if (r)
  160. ceph_get_snap_realm(mdsc, r);
  161. return r;
  162. }
  163. static void __put_snap_realm(struct ceph_mds_client *mdsc,
  164. struct ceph_snap_realm *realm);
  165. /*
  166. * called with snap_rwsem (write)
  167. */
  168. static void __destroy_snap_realm(struct ceph_mds_client *mdsc,
  169. struct ceph_snap_realm *realm)
  170. {
  171. struct ceph_client *cl = mdsc->fsc->client;
  172. lockdep_assert_held_write(&mdsc->snap_rwsem);
  173. doutc(cl, "%p %llx\n", realm, realm->ino);
  174. rb_erase(&realm->node, &mdsc->snap_realms);
  175. mdsc->num_snap_realms--;
  176. if (realm->parent) {
  177. list_del_init(&realm->child_item);
  178. __put_snap_realm(mdsc, realm->parent);
  179. }
  180. kfree(realm->prior_parent_snaps);
  181. kfree(realm->snaps);
  182. ceph_put_snap_context(realm->cached_context);
  183. kfree(realm);
  184. }
  185. /*
  186. * caller holds snap_rwsem (write)
  187. */
  188. static void __put_snap_realm(struct ceph_mds_client *mdsc,
  189. struct ceph_snap_realm *realm)
  190. {
  191. lockdep_assert_held_write(&mdsc->snap_rwsem);
  192. /*
  193. * We do not require the snap_empty_lock here, as any caller that
  194. * increments the value must hold the snap_rwsem.
  195. */
  196. if (atomic_dec_and_test(&realm->nref))
  197. __destroy_snap_realm(mdsc, realm);
  198. }
  199. /*
  200. * See comments in ceph_get_snap_realm. Caller needn't hold any locks.
  201. */
  202. void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
  203. struct ceph_snap_realm *realm)
  204. {
  205. if (!atomic_dec_and_lock(&realm->nref, &mdsc->snap_empty_lock))
  206. return;
  207. if (down_write_trylock(&mdsc->snap_rwsem)) {
  208. spin_unlock(&mdsc->snap_empty_lock);
  209. __destroy_snap_realm(mdsc, realm);
  210. up_write(&mdsc->snap_rwsem);
  211. } else {
  212. list_add(&realm->empty_item, &mdsc->snap_empty);
  213. spin_unlock(&mdsc->snap_empty_lock);
  214. }
  215. }
  216. /*
  217. * Clean up any realms whose ref counts have dropped to zero. Note
  218. * that this does not include realms who were created but not yet
  219. * used.
  220. *
  221. * Called under snap_rwsem (write)
  222. */
  223. static void __cleanup_empty_realms(struct ceph_mds_client *mdsc)
  224. {
  225. struct ceph_snap_realm *realm;
  226. lockdep_assert_held_write(&mdsc->snap_rwsem);
  227. spin_lock(&mdsc->snap_empty_lock);
  228. while (!list_empty(&mdsc->snap_empty)) {
  229. realm = list_first_entry(&mdsc->snap_empty,
  230. struct ceph_snap_realm, empty_item);
  231. list_del(&realm->empty_item);
  232. spin_unlock(&mdsc->snap_empty_lock);
  233. __destroy_snap_realm(mdsc, realm);
  234. spin_lock(&mdsc->snap_empty_lock);
  235. }
  236. spin_unlock(&mdsc->snap_empty_lock);
  237. }
  238. void ceph_cleanup_global_and_empty_realms(struct ceph_mds_client *mdsc)
  239. {
  240. struct ceph_snap_realm *global_realm;
  241. down_write(&mdsc->snap_rwsem);
  242. global_realm = __lookup_snap_realm(mdsc, CEPH_INO_GLOBAL_SNAPREALM);
  243. if (global_realm)
  244. ceph_put_snap_realm(mdsc, global_realm);
  245. __cleanup_empty_realms(mdsc);
  246. up_write(&mdsc->snap_rwsem);
  247. }
  248. /*
  249. * adjust the parent realm of a given @realm. adjust child list, and parent
  250. * pointers, and ref counts appropriately.
  251. *
  252. * return true if parent was changed, 0 if unchanged, <0 on error.
  253. *
  254. * caller must hold snap_rwsem for write.
  255. */
  256. static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc,
  257. struct ceph_snap_realm *realm,
  258. u64 parentino)
  259. {
  260. struct ceph_client *cl = mdsc->fsc->client;
  261. struct ceph_snap_realm *parent;
  262. lockdep_assert_held_write(&mdsc->snap_rwsem);
  263. if (realm->parent_ino == parentino)
  264. return 0;
  265. parent = ceph_lookup_snap_realm(mdsc, parentino);
  266. if (!parent) {
  267. parent = ceph_create_snap_realm(mdsc, parentino);
  268. if (IS_ERR(parent))
  269. return PTR_ERR(parent);
  270. }
  271. doutc(cl, "%llx %p: %llx %p -> %llx %p\n", realm->ino, realm,
  272. realm->parent_ino, realm->parent, parentino, parent);
  273. if (realm->parent) {
  274. list_del_init(&realm->child_item);
  275. ceph_put_snap_realm(mdsc, realm->parent);
  276. }
  277. realm->parent_ino = parentino;
  278. realm->parent = parent;
  279. list_add(&realm->child_item, &parent->children);
  280. return 1;
  281. }
  282. static int cmpu64_rev(const void *a, const void *b)
  283. {
  284. if (*(u64 *)a < *(u64 *)b)
  285. return 1;
  286. if (*(u64 *)a > *(u64 *)b)
  287. return -1;
  288. return 0;
  289. }
  290. /*
  291. * build the snap context for a given realm.
  292. */
  293. static int build_snap_context(struct ceph_mds_client *mdsc,
  294. struct ceph_snap_realm *realm,
  295. struct list_head *realm_queue,
  296. struct list_head *dirty_realms)
  297. {
  298. struct ceph_client *cl = mdsc->fsc->client;
  299. struct ceph_snap_realm *parent = realm->parent;
  300. struct ceph_snap_context *snapc;
  301. int err = 0;
  302. u32 num = realm->num_prior_parent_snaps + realm->num_snaps;
  303. /*
  304. * build parent context, if it hasn't been built.
  305. * conservatively estimate that all parent snaps might be
  306. * included by us.
  307. */
  308. if (parent) {
  309. if (!parent->cached_context) {
  310. /* add to the queue head */
  311. list_add(&parent->rebuild_item, realm_queue);
  312. return 1;
  313. }
  314. num += parent->cached_context->num_snaps;
  315. }
  316. /* do i actually need to update? not if my context seq
  317. matches realm seq, and my parents' does to. (this works
  318. because we rebuild_snap_realms() works _downward_ in
  319. hierarchy after each update.) */
  320. if (realm->cached_context &&
  321. realm->cached_context->seq == realm->seq &&
  322. (!parent ||
  323. realm->cached_context->seq >= parent->cached_context->seq)) {
  324. doutc(cl, "%llx %p: %p seq %lld (%u snaps) (unchanged)\n",
  325. realm->ino, realm, realm->cached_context,
  326. realm->cached_context->seq,
  327. (unsigned int)realm->cached_context->num_snaps);
  328. return 0;
  329. }
  330. /* alloc new snap context */
  331. err = -ENOMEM;
  332. if (num > (SIZE_MAX - sizeof(*snapc)) / sizeof(u64))
  333. goto fail;
  334. snapc = ceph_create_snap_context(num, GFP_NOFS);
  335. if (!snapc)
  336. goto fail;
  337. /* build (reverse sorted) snap vector */
  338. num = 0;
  339. snapc->seq = realm->seq;
  340. if (parent) {
  341. u32 i;
  342. /* include any of parent's snaps occurring _after_ my
  343. parent became my parent */
  344. for (i = 0; i < parent->cached_context->num_snaps; i++)
  345. if (parent->cached_context->snaps[i] >=
  346. realm->parent_since)
  347. snapc->snaps[num++] =
  348. parent->cached_context->snaps[i];
  349. if (parent->cached_context->seq > snapc->seq)
  350. snapc->seq = parent->cached_context->seq;
  351. }
  352. memcpy(snapc->snaps + num, realm->snaps,
  353. sizeof(u64)*realm->num_snaps);
  354. num += realm->num_snaps;
  355. memcpy(snapc->snaps + num, realm->prior_parent_snaps,
  356. sizeof(u64)*realm->num_prior_parent_snaps);
  357. num += realm->num_prior_parent_snaps;
  358. sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL);
  359. snapc->num_snaps = num;
  360. doutc(cl, "%llx %p: %p seq %lld (%u snaps)\n", realm->ino, realm,
  361. snapc, snapc->seq, (unsigned int) snapc->num_snaps);
  362. ceph_put_snap_context(realm->cached_context);
  363. realm->cached_context = snapc;
  364. /* queue realm for cap_snap creation */
  365. list_add_tail(&realm->dirty_item, dirty_realms);
  366. return 0;
  367. fail:
  368. /*
  369. * if we fail, clear old (incorrect) cached_context... hopefully
  370. * we'll have better luck building it later
  371. */
  372. if (realm->cached_context) {
  373. ceph_put_snap_context(realm->cached_context);
  374. realm->cached_context = NULL;
  375. }
  376. pr_err_client(cl, "%llx %p fail %d\n", realm->ino, realm, err);
  377. return err;
  378. }
  379. /*
  380. * rebuild snap context for the given realm and all of its children.
  381. */
  382. static void rebuild_snap_realms(struct ceph_mds_client *mdsc,
  383. struct ceph_snap_realm *realm,
  384. struct list_head *dirty_realms)
  385. {
  386. struct ceph_client *cl = mdsc->fsc->client;
  387. LIST_HEAD(realm_queue);
  388. int last = 0;
  389. bool skip = false;
  390. list_add_tail(&realm->rebuild_item, &realm_queue);
  391. while (!list_empty(&realm_queue)) {
  392. struct ceph_snap_realm *_realm, *child;
  393. _realm = list_first_entry(&realm_queue,
  394. struct ceph_snap_realm,
  395. rebuild_item);
  396. /*
  397. * If the last building failed dues to memory
  398. * issue, just empty the realm_queue and return
  399. * to avoid infinite loop.
  400. */
  401. if (last < 0) {
  402. list_del_init(&_realm->rebuild_item);
  403. continue;
  404. }
  405. last = build_snap_context(mdsc, _realm, &realm_queue,
  406. dirty_realms);
  407. doutc(cl, "%llx %p, %s\n", realm->ino, realm,
  408. last > 0 ? "is deferred" : !last ? "succeeded" : "failed");
  409. /* is any child in the list ? */
  410. list_for_each_entry(child, &_realm->children, child_item) {
  411. if (!list_empty(&child->rebuild_item)) {
  412. skip = true;
  413. break;
  414. }
  415. }
  416. if (!skip) {
  417. list_for_each_entry(child, &_realm->children, child_item)
  418. list_add_tail(&child->rebuild_item, &realm_queue);
  419. }
  420. /* last == 1 means need to build parent first */
  421. if (last <= 0)
  422. list_del_init(&_realm->rebuild_item);
  423. }
  424. }
  425. /*
  426. * helper to allocate and decode an array of snapids. free prior
  427. * instance, if any.
  428. */
  429. static int dup_array(u64 **dst, __le64 *src, u32 num)
  430. {
  431. u32 i;
  432. kfree(*dst);
  433. if (num) {
  434. *dst = kcalloc(num, sizeof(u64), GFP_NOFS);
  435. if (!*dst)
  436. return -ENOMEM;
  437. for (i = 0; i < num; i++)
  438. (*dst)[i] = get_unaligned_le64(src + i);
  439. } else {
  440. *dst = NULL;
  441. }
  442. return 0;
  443. }
  444. static bool has_new_snaps(struct ceph_snap_context *o,
  445. struct ceph_snap_context *n)
  446. {
  447. if (n->num_snaps == 0)
  448. return false;
  449. /* snaps are in descending order */
  450. return n->snaps[0] > o->seq;
  451. }
  452. /*
  453. * When a snapshot is applied, the size/mtime inode metadata is queued
  454. * in a ceph_cap_snap (one for each snapshot) until writeback
  455. * completes and the metadata can be flushed back to the MDS.
  456. *
  457. * However, if a (sync) write is currently in-progress when we apply
  458. * the snapshot, we have to wait until the write succeeds or fails
  459. * (and a final size/mtime is known). In this case the
  460. * cap_snap->writing = 1, and is said to be "pending." When the write
  461. * finishes, we __ceph_finish_cap_snap().
  462. *
  463. * Caller must hold snap_rwsem for read (i.e., the realm topology won't
  464. * change).
  465. */
  466. static void ceph_queue_cap_snap(struct ceph_inode_info *ci,
  467. struct ceph_cap_snap **pcapsnap)
  468. {
  469. struct inode *inode = &ci->netfs.inode;
  470. struct ceph_client *cl = ceph_inode_to_client(inode);
  471. struct ceph_snap_context *old_snapc, *new_snapc;
  472. struct ceph_cap_snap *capsnap = *pcapsnap;
  473. struct ceph_buffer *old_blob = NULL;
  474. int used, dirty;
  475. spin_lock(&ci->i_ceph_lock);
  476. used = __ceph_caps_used(ci);
  477. dirty = __ceph_caps_dirty(ci);
  478. old_snapc = ci->i_head_snapc;
  479. new_snapc = ci->i_snap_realm->cached_context;
  480. /*
  481. * If there is a write in progress, treat that as a dirty Fw,
  482. * even though it hasn't completed yet; by the time we finish
  483. * up this capsnap it will be.
  484. */
  485. if (used & CEPH_CAP_FILE_WR)
  486. dirty |= CEPH_CAP_FILE_WR;
  487. if (__ceph_have_pending_cap_snap(ci)) {
  488. /* there is no point in queuing multiple "pending" cap_snaps,
  489. as no new writes are allowed to start when pending, so any
  490. writes in progress now were started before the previous
  491. cap_snap. lucky us. */
  492. doutc(cl, "%p %llx.%llx already pending\n", inode,
  493. ceph_vinop(inode));
  494. goto update_snapc;
  495. }
  496. if (ci->i_wrbuffer_ref_head == 0 &&
  497. !(dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))) {
  498. doutc(cl, "%p %llx.%llx nothing dirty|writing\n", inode,
  499. ceph_vinop(inode));
  500. goto update_snapc;
  501. }
  502. BUG_ON(!old_snapc);
  503. /*
  504. * There is no need to send FLUSHSNAP message to MDS if there is
  505. * no new snapshot. But when there is dirty pages or on-going
  506. * writes, we still need to create cap_snap. cap_snap is needed
  507. * by the write path and page writeback path.
  508. *
  509. * also see ceph_try_drop_cap_snap()
  510. */
  511. if (has_new_snaps(old_snapc, new_snapc)) {
  512. if (dirty & (CEPH_CAP_ANY_EXCL|CEPH_CAP_FILE_WR))
  513. capsnap->need_flush = true;
  514. } else {
  515. if (!(used & CEPH_CAP_FILE_WR) &&
  516. ci->i_wrbuffer_ref_head == 0) {
  517. doutc(cl, "%p %llx.%llx no new_snap|dirty_page|writing\n",
  518. inode, ceph_vinop(inode));
  519. goto update_snapc;
  520. }
  521. }
  522. doutc(cl, "%p %llx.%llx cap_snap %p queuing under %p %s %s\n",
  523. inode, ceph_vinop(inode), capsnap, old_snapc,
  524. ceph_cap_string(dirty), capsnap->need_flush ? "" : "no_flush");
  525. ihold(inode);
  526. capsnap->follows = old_snapc->seq;
  527. capsnap->issued = __ceph_caps_issued(ci, NULL);
  528. capsnap->dirty = dirty;
  529. capsnap->mode = inode->i_mode;
  530. capsnap->uid = inode->i_uid;
  531. capsnap->gid = inode->i_gid;
  532. if (dirty & CEPH_CAP_XATTR_EXCL) {
  533. old_blob = __ceph_build_xattrs_blob(ci);
  534. capsnap->xattr_blob =
  535. ceph_buffer_get(ci->i_xattrs.blob);
  536. capsnap->xattr_version = ci->i_xattrs.version;
  537. } else {
  538. capsnap->xattr_blob = NULL;
  539. capsnap->xattr_version = 0;
  540. }
  541. capsnap->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
  542. /* dirty page count moved from _head to this cap_snap;
  543. all subsequent writes page dirties occur _after_ this
  544. snapshot. */
  545. capsnap->dirty_pages = ci->i_wrbuffer_ref_head;
  546. ci->i_wrbuffer_ref_head = 0;
  547. capsnap->context = old_snapc;
  548. list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps);
  549. if (used & CEPH_CAP_FILE_WR) {
  550. doutc(cl, "%p %llx.%llx cap_snap %p snapc %p seq %llu used WR,"
  551. " now pending\n", inode, ceph_vinop(inode), capsnap,
  552. old_snapc, old_snapc->seq);
  553. capsnap->writing = 1;
  554. } else {
  555. /* note mtime, size NOW. */
  556. __ceph_finish_cap_snap(ci, capsnap);
  557. }
  558. *pcapsnap = NULL;
  559. old_snapc = NULL;
  560. update_snapc:
  561. if (ci->i_wrbuffer_ref_head == 0 &&
  562. ci->i_wr_ref == 0 &&
  563. ci->i_dirty_caps == 0 &&
  564. ci->i_flushing_caps == 0) {
  565. ci->i_head_snapc = NULL;
  566. } else {
  567. ci->i_head_snapc = ceph_get_snap_context(new_snapc);
  568. doutc(cl, " new snapc is %p\n", new_snapc);
  569. }
  570. spin_unlock(&ci->i_ceph_lock);
  571. ceph_buffer_put(old_blob);
  572. ceph_put_snap_context(old_snapc);
  573. }
  574. /*
  575. * Finalize the size, mtime for a cap_snap.. that is, settle on final values
  576. * to be used for the snapshot, to be flushed back to the mds.
  577. *
  578. * If capsnap can now be flushed, add to snap_flush list, and return 1.
  579. *
  580. * Caller must hold i_ceph_lock.
  581. */
  582. int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
  583. struct ceph_cap_snap *capsnap)
  584. {
  585. struct inode *inode = &ci->netfs.inode;
  586. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  587. struct ceph_client *cl = mdsc->fsc->client;
  588. BUG_ON(capsnap->writing);
  589. capsnap->size = i_size_read(inode);
  590. capsnap->mtime = inode_get_mtime(inode);
  591. capsnap->atime = inode_get_atime(inode);
  592. capsnap->ctime = inode_get_ctime(inode);
  593. capsnap->btime = ci->i_btime;
  594. capsnap->change_attr = inode_peek_iversion_raw(inode);
  595. capsnap->time_warp_seq = ci->i_time_warp_seq;
  596. capsnap->truncate_size = ci->i_truncate_size;
  597. capsnap->truncate_seq = ci->i_truncate_seq;
  598. if (capsnap->dirty_pages) {
  599. doutc(cl, "%p %llx.%llx cap_snap %p snapc %p %llu %s "
  600. "s=%llu still has %d dirty pages\n", inode,
  601. ceph_vinop(inode), capsnap, capsnap->context,
  602. capsnap->context->seq,
  603. ceph_cap_string(capsnap->dirty),
  604. capsnap->size, capsnap->dirty_pages);
  605. return 0;
  606. }
  607. /*
  608. * Defer flushing the capsnap if the dirty buffer not flushed yet.
  609. * And trigger to flush the buffer immediately.
  610. */
  611. if (ci->i_wrbuffer_ref) {
  612. doutc(cl, "%p %llx.%llx cap_snap %p snapc %p %llu %s "
  613. "s=%llu used WRBUFFER, delaying\n", inode,
  614. ceph_vinop(inode), capsnap, capsnap->context,
  615. capsnap->context->seq, ceph_cap_string(capsnap->dirty),
  616. capsnap->size);
  617. ceph_queue_writeback(inode);
  618. return 0;
  619. }
  620. ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  621. doutc(cl, "%p %llx.%llx cap_snap %p snapc %p %llu %s s=%llu\n",
  622. inode, ceph_vinop(inode), capsnap, capsnap->context,
  623. capsnap->context->seq, ceph_cap_string(capsnap->dirty),
  624. capsnap->size);
  625. spin_lock(&mdsc->snap_flush_lock);
  626. if (list_empty(&ci->i_snap_flush_item)) {
  627. ihold(inode);
  628. list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list);
  629. }
  630. spin_unlock(&mdsc->snap_flush_lock);
  631. return 1; /* caller may want to ceph_flush_snaps */
  632. }
  633. /*
  634. * Queue cap_snaps for snap writeback for this realm and its children.
  635. * Called under snap_rwsem, so realm topology won't change.
  636. */
  637. static void queue_realm_cap_snaps(struct ceph_mds_client *mdsc,
  638. struct ceph_snap_realm *realm)
  639. {
  640. struct ceph_client *cl = mdsc->fsc->client;
  641. struct ceph_inode_info *ci;
  642. struct inode *lastinode = NULL;
  643. struct ceph_cap_snap *capsnap = NULL;
  644. doutc(cl, "%p %llx inode\n", realm, realm->ino);
  645. spin_lock(&realm->inodes_with_caps_lock);
  646. list_for_each_entry(ci, &realm->inodes_with_caps, i_snap_realm_item) {
  647. struct inode *inode = igrab(&ci->netfs.inode);
  648. if (!inode)
  649. continue;
  650. spin_unlock(&realm->inodes_with_caps_lock);
  651. iput(lastinode);
  652. lastinode = inode;
  653. /*
  654. * Allocate the capsnap memory outside of ceph_queue_cap_snap()
  655. * to reduce very possible but unnecessary frequently memory
  656. * allocate/free in this loop.
  657. */
  658. if (!capsnap) {
  659. capsnap = kmem_cache_zalloc(ceph_cap_snap_cachep, GFP_NOFS);
  660. if (!capsnap) {
  661. pr_err_client(cl,
  662. "ENOMEM allocating ceph_cap_snap on %p\n",
  663. inode);
  664. return;
  665. }
  666. }
  667. capsnap->cap_flush.is_capsnap = true;
  668. refcount_set(&capsnap->nref, 1);
  669. INIT_LIST_HEAD(&capsnap->cap_flush.i_list);
  670. INIT_LIST_HEAD(&capsnap->cap_flush.g_list);
  671. INIT_LIST_HEAD(&capsnap->ci_item);
  672. ceph_queue_cap_snap(ci, &capsnap);
  673. spin_lock(&realm->inodes_with_caps_lock);
  674. }
  675. spin_unlock(&realm->inodes_with_caps_lock);
  676. iput(lastinode);
  677. if (capsnap)
  678. kmem_cache_free(ceph_cap_snap_cachep, capsnap);
  679. doutc(cl, "%p %llx done\n", realm, realm->ino);
  680. }
  681. /*
  682. * Parse and apply a snapblob "snap trace" from the MDS. This specifies
  683. * the snap realm parameters from a given realm and all of its ancestors,
  684. * up to the root.
  685. *
  686. * Caller must hold snap_rwsem for write.
  687. */
  688. int ceph_update_snap_trace(struct ceph_mds_client *mdsc,
  689. void *p, void *e, bool deletion,
  690. struct ceph_snap_realm **realm_ret)
  691. {
  692. struct ceph_client *cl = mdsc->fsc->client;
  693. struct ceph_mds_snap_realm *ri; /* encoded */
  694. __le64 *snaps; /* encoded */
  695. __le64 *prior_parent_snaps; /* encoded */
  696. struct ceph_snap_realm *realm;
  697. struct ceph_snap_realm *first_realm = NULL;
  698. struct ceph_snap_realm *realm_to_rebuild = NULL;
  699. struct ceph_client *client = mdsc->fsc->client;
  700. int rebuild_snapcs;
  701. int err = -ENOMEM;
  702. int ret;
  703. LIST_HEAD(dirty_realms);
  704. lockdep_assert_held_write(&mdsc->snap_rwsem);
  705. doutc(cl, "deletion=%d\n", deletion);
  706. more:
  707. realm = NULL;
  708. rebuild_snapcs = 0;
  709. ceph_decode_need(&p, e, sizeof(*ri), bad);
  710. ri = p;
  711. p += sizeof(*ri);
  712. ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) +
  713. le32_to_cpu(ri->num_prior_parent_snaps)), bad);
  714. snaps = p;
  715. p += sizeof(u64) * le32_to_cpu(ri->num_snaps);
  716. prior_parent_snaps = p;
  717. p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps);
  718. realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino));
  719. if (!realm) {
  720. realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino));
  721. if (IS_ERR(realm)) {
  722. err = PTR_ERR(realm);
  723. goto fail;
  724. }
  725. }
  726. /* ensure the parent is correct */
  727. err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent));
  728. if (err < 0)
  729. goto fail;
  730. rebuild_snapcs += err;
  731. if (le64_to_cpu(ri->seq) > realm->seq) {
  732. doutc(cl, "updating %llx %p %lld -> %lld\n", realm->ino,
  733. realm, realm->seq, le64_to_cpu(ri->seq));
  734. /* update realm parameters, snap lists */
  735. realm->seq = le64_to_cpu(ri->seq);
  736. realm->created = le64_to_cpu(ri->created);
  737. realm->parent_since = le64_to_cpu(ri->parent_since);
  738. realm->num_snaps = le32_to_cpu(ri->num_snaps);
  739. err = dup_array(&realm->snaps, snaps, realm->num_snaps);
  740. if (err < 0)
  741. goto fail;
  742. realm->num_prior_parent_snaps =
  743. le32_to_cpu(ri->num_prior_parent_snaps);
  744. err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps,
  745. realm->num_prior_parent_snaps);
  746. if (err < 0)
  747. goto fail;
  748. if (realm->seq > mdsc->last_snap_seq)
  749. mdsc->last_snap_seq = realm->seq;
  750. rebuild_snapcs = 1;
  751. } else if (!realm->cached_context) {
  752. doutc(cl, "%llx %p seq %lld new\n", realm->ino, realm,
  753. realm->seq);
  754. rebuild_snapcs = 1;
  755. } else {
  756. doutc(cl, "%llx %p seq %lld unchanged\n", realm->ino, realm,
  757. realm->seq);
  758. }
  759. doutc(cl, "done with %llx %p, rebuild_snapcs=%d, %p %p\n", realm->ino,
  760. realm, rebuild_snapcs, p, e);
  761. /*
  762. * this will always track the uppest parent realm from which
  763. * we need to rebuild the snapshot contexts _downward_ in
  764. * hierarchy.
  765. */
  766. if (rebuild_snapcs)
  767. realm_to_rebuild = realm;
  768. /* rebuild_snapcs when we reach the _end_ (root) of the trace */
  769. if (realm_to_rebuild && p >= e)
  770. rebuild_snap_realms(mdsc, realm_to_rebuild, &dirty_realms);
  771. if (!first_realm)
  772. first_realm = realm;
  773. else
  774. ceph_put_snap_realm(mdsc, realm);
  775. if (p < e)
  776. goto more;
  777. /*
  778. * queue cap snaps _after_ we've built the new snap contexts,
  779. * so that i_head_snapc can be set appropriately.
  780. */
  781. while (!list_empty(&dirty_realms)) {
  782. realm = list_first_entry(&dirty_realms, struct ceph_snap_realm,
  783. dirty_item);
  784. list_del_init(&realm->dirty_item);
  785. queue_realm_cap_snaps(mdsc, realm);
  786. }
  787. if (realm_ret)
  788. *realm_ret = first_realm;
  789. else
  790. ceph_put_snap_realm(mdsc, first_realm);
  791. __cleanup_empty_realms(mdsc);
  792. return 0;
  793. bad:
  794. err = -EIO;
  795. fail:
  796. if (realm && !IS_ERR(realm))
  797. ceph_put_snap_realm(mdsc, realm);
  798. if (first_realm)
  799. ceph_put_snap_realm(mdsc, first_realm);
  800. pr_err_client(cl, "error %d\n", err);
  801. /*
  802. * When receiving a corrupted snap trace we don't know what
  803. * exactly has happened in MDS side. And we shouldn't continue
  804. * writing to OSD, which may corrupt the snapshot contents.
  805. *
  806. * Just try to blocklist this kclient and then this kclient
  807. * must be remounted to continue after the corrupted metadata
  808. * fixed in the MDS side.
  809. */
  810. WRITE_ONCE(mdsc->fsc->mount_state, CEPH_MOUNT_FENCE_IO);
  811. ret = ceph_monc_blocklist_add(&client->monc, &client->msgr.inst.addr);
  812. if (ret)
  813. pr_err_client(cl, "failed to blocklist %s: %d\n",
  814. ceph_pr_addr(&client->msgr.inst.addr), ret);
  815. WARN(1, "[client.%lld] %s %s%sdo remount to continue%s",
  816. client->monc.auth->global_id, __func__,
  817. ret ? "" : ceph_pr_addr(&client->msgr.inst.addr),
  818. ret ? "" : " was blocklisted, ",
  819. err == -EIO ? " after corrupted snaptrace is fixed" : "");
  820. return err;
  821. }
  822. /*
  823. * Send any cap_snaps that are queued for flush. Try to carry
  824. * s_mutex across multiple snap flushes to avoid locking overhead.
  825. *
  826. * Caller holds no locks.
  827. */
  828. static void flush_snaps(struct ceph_mds_client *mdsc)
  829. {
  830. struct ceph_client *cl = mdsc->fsc->client;
  831. struct ceph_inode_info *ci;
  832. struct inode *inode;
  833. struct ceph_mds_session *session = NULL;
  834. doutc(cl, "begin\n");
  835. spin_lock(&mdsc->snap_flush_lock);
  836. while (!list_empty(&mdsc->snap_flush_list)) {
  837. ci = list_first_entry(&mdsc->snap_flush_list,
  838. struct ceph_inode_info, i_snap_flush_item);
  839. inode = &ci->netfs.inode;
  840. ihold(inode);
  841. spin_unlock(&mdsc->snap_flush_lock);
  842. ceph_flush_snaps(ci, &session);
  843. iput(inode);
  844. spin_lock(&mdsc->snap_flush_lock);
  845. }
  846. spin_unlock(&mdsc->snap_flush_lock);
  847. ceph_put_mds_session(session);
  848. doutc(cl, "done\n");
  849. }
  850. /**
  851. * ceph_change_snap_realm - change the snap_realm for an inode
  852. * @inode: inode to move to new snap realm
  853. * @realm: new realm to move inode into (may be NULL)
  854. *
  855. * Detach an inode from its old snaprealm (if any) and attach it to
  856. * the new snaprealm (if any). The old snap realm reference held by
  857. * the inode is put. If realm is non-NULL, then the caller's reference
  858. * to it is taken over by the inode.
  859. */
  860. void ceph_change_snap_realm(struct inode *inode, struct ceph_snap_realm *realm)
  861. {
  862. struct ceph_inode_info *ci = ceph_inode(inode);
  863. struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
  864. struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
  865. lockdep_assert_held(&ci->i_ceph_lock);
  866. if (oldrealm) {
  867. spin_lock(&oldrealm->inodes_with_caps_lock);
  868. list_del_init(&ci->i_snap_realm_item);
  869. if (oldrealm->ino == ci->i_vino.ino)
  870. oldrealm->inode = NULL;
  871. spin_unlock(&oldrealm->inodes_with_caps_lock);
  872. ceph_put_snap_realm(mdsc, oldrealm);
  873. }
  874. ci->i_snap_realm = realm;
  875. if (realm) {
  876. spin_lock(&realm->inodes_with_caps_lock);
  877. list_add(&ci->i_snap_realm_item, &realm->inodes_with_caps);
  878. if (realm->ino == ci->i_vino.ino)
  879. realm->inode = inode;
  880. spin_unlock(&realm->inodes_with_caps_lock);
  881. }
  882. }
  883. /*
  884. * Handle a snap notification from the MDS.
  885. *
  886. * This can take two basic forms: the simplest is just a snap creation
  887. * or deletion notification on an existing realm. This should update the
  888. * realm and its children.
  889. *
  890. * The more difficult case is realm creation, due to snap creation at a
  891. * new point in the file hierarchy, or due to a rename that moves a file or
  892. * directory into another realm.
  893. */
  894. void ceph_handle_snap(struct ceph_mds_client *mdsc,
  895. struct ceph_mds_session *session,
  896. struct ceph_msg *msg)
  897. {
  898. struct ceph_client *cl = mdsc->fsc->client;
  899. struct super_block *sb = mdsc->fsc->sb;
  900. int mds = session->s_mds;
  901. u64 split;
  902. int op;
  903. int trace_len;
  904. struct ceph_snap_realm *realm = NULL;
  905. void *p = msg->front.iov_base;
  906. void *e = p + msg->front.iov_len;
  907. struct ceph_mds_snap_head *h;
  908. int num_split_inos, num_split_realms;
  909. __le64 *split_inos = NULL, *split_realms = NULL;
  910. int i;
  911. int locked_rwsem = 0;
  912. bool close_sessions = false;
  913. if (!ceph_inc_mds_stopping_blocker(mdsc, session))
  914. return;
  915. /* decode */
  916. if (msg->front.iov_len < sizeof(*h))
  917. goto bad;
  918. h = p;
  919. op = le32_to_cpu(h->op);
  920. split = le64_to_cpu(h->split); /* non-zero if we are splitting an
  921. * existing realm */
  922. num_split_inos = le32_to_cpu(h->num_split_inos);
  923. num_split_realms = le32_to_cpu(h->num_split_realms);
  924. trace_len = le32_to_cpu(h->trace_len);
  925. p += sizeof(*h);
  926. doutc(cl, "from mds%d op %s split %llx tracelen %d\n", mds,
  927. ceph_snap_op_name(op), split, trace_len);
  928. down_write(&mdsc->snap_rwsem);
  929. locked_rwsem = 1;
  930. if (op == CEPH_SNAP_OP_SPLIT) {
  931. struct ceph_mds_snap_realm *ri;
  932. /*
  933. * A "split" breaks part of an existing realm off into
  934. * a new realm. The MDS provides a list of inodes
  935. * (with caps) and child realms that belong to the new
  936. * child.
  937. */
  938. split_inos = p;
  939. p += sizeof(u64) * num_split_inos;
  940. split_realms = p;
  941. p += sizeof(u64) * num_split_realms;
  942. ceph_decode_need(&p, e, sizeof(*ri), bad);
  943. /* we will peek at realm info here, but will _not_
  944. * advance p, as the realm update will occur below in
  945. * ceph_update_snap_trace. */
  946. ri = p;
  947. realm = ceph_lookup_snap_realm(mdsc, split);
  948. if (!realm) {
  949. realm = ceph_create_snap_realm(mdsc, split);
  950. if (IS_ERR(realm))
  951. goto out;
  952. }
  953. doutc(cl, "splitting snap_realm %llx %p\n", realm->ino, realm);
  954. for (i = 0; i < num_split_inos; i++) {
  955. struct ceph_vino vino = {
  956. .ino = le64_to_cpu(split_inos[i]),
  957. .snap = CEPH_NOSNAP,
  958. };
  959. struct inode *inode = ceph_find_inode(sb, vino);
  960. struct ceph_inode_info *ci;
  961. if (!inode)
  962. continue;
  963. ci = ceph_inode(inode);
  964. spin_lock(&ci->i_ceph_lock);
  965. if (!ci->i_snap_realm)
  966. goto skip_inode;
  967. /*
  968. * If this inode belongs to a realm that was
  969. * created after our new realm, we experienced
  970. * a race (due to another split notifications
  971. * arriving from a different MDS). So skip
  972. * this inode.
  973. */
  974. if (ci->i_snap_realm->created >
  975. le64_to_cpu(ri->created)) {
  976. doutc(cl, " leaving %p %llx.%llx in newer realm %llx %p\n",
  977. inode, ceph_vinop(inode), ci->i_snap_realm->ino,
  978. ci->i_snap_realm);
  979. goto skip_inode;
  980. }
  981. doutc(cl, " will move %p %llx.%llx to split realm %llx %p\n",
  982. inode, ceph_vinop(inode), realm->ino, realm);
  983. ceph_get_snap_realm(mdsc, realm);
  984. ceph_change_snap_realm(inode, realm);
  985. spin_unlock(&ci->i_ceph_lock);
  986. iput(inode);
  987. continue;
  988. skip_inode:
  989. spin_unlock(&ci->i_ceph_lock);
  990. iput(inode);
  991. }
  992. /* we may have taken some of the old realm's children. */
  993. for (i = 0; i < num_split_realms; i++) {
  994. struct ceph_snap_realm *child =
  995. __lookup_snap_realm(mdsc,
  996. le64_to_cpu(split_realms[i]));
  997. if (!child)
  998. continue;
  999. adjust_snap_realm_parent(mdsc, child, realm->ino);
  1000. }
  1001. } else {
  1002. /*
  1003. * In the non-split case both 'num_split_inos' and
  1004. * 'num_split_realms' should be 0, making this a no-op.
  1005. * However the MDS happens to populate 'split_realms' list
  1006. * in one of the UPDATE op cases by mistake.
  1007. *
  1008. * Skip both lists just in case to ensure that 'p' is
  1009. * positioned at the start of realm info, as expected by
  1010. * ceph_update_snap_trace().
  1011. */
  1012. p += sizeof(u64) * num_split_inos;
  1013. p += sizeof(u64) * num_split_realms;
  1014. }
  1015. /*
  1016. * update using the provided snap trace. if we are deleting a
  1017. * snap, we can avoid queueing cap_snaps.
  1018. */
  1019. if (ceph_update_snap_trace(mdsc, p, e,
  1020. op == CEPH_SNAP_OP_DESTROY,
  1021. NULL)) {
  1022. close_sessions = true;
  1023. goto bad;
  1024. }
  1025. if (op == CEPH_SNAP_OP_SPLIT)
  1026. /* we took a reference when we created the realm, above */
  1027. ceph_put_snap_realm(mdsc, realm);
  1028. __cleanup_empty_realms(mdsc);
  1029. up_write(&mdsc->snap_rwsem);
  1030. flush_snaps(mdsc);
  1031. ceph_dec_mds_stopping_blocker(mdsc);
  1032. return;
  1033. bad:
  1034. pr_err_client(cl, "corrupt snap message from mds%d\n", mds);
  1035. ceph_msg_dump(msg);
  1036. out:
  1037. if (locked_rwsem)
  1038. up_write(&mdsc->snap_rwsem);
  1039. ceph_dec_mds_stopping_blocker(mdsc);
  1040. if (close_sessions)
  1041. ceph_mdsc_close_sessions(mdsc);
  1042. return;
  1043. }
  1044. struct ceph_snapid_map* ceph_get_snapid_map(struct ceph_mds_client *mdsc,
  1045. u64 snap)
  1046. {
  1047. struct ceph_client *cl = mdsc->fsc->client;
  1048. struct ceph_snapid_map *sm, *exist;
  1049. struct rb_node **p, *parent;
  1050. int ret;
  1051. exist = NULL;
  1052. spin_lock(&mdsc->snapid_map_lock);
  1053. p = &mdsc->snapid_map_tree.rb_node;
  1054. while (*p) {
  1055. exist = rb_entry(*p, struct ceph_snapid_map, node);
  1056. if (snap > exist->snap) {
  1057. p = &(*p)->rb_left;
  1058. } else if (snap < exist->snap) {
  1059. p = &(*p)->rb_right;
  1060. } else {
  1061. if (atomic_inc_return(&exist->ref) == 1)
  1062. list_del_init(&exist->lru);
  1063. break;
  1064. }
  1065. exist = NULL;
  1066. }
  1067. spin_unlock(&mdsc->snapid_map_lock);
  1068. if (exist) {
  1069. doutc(cl, "found snapid map %llx -> %x\n", exist->snap,
  1070. exist->dev);
  1071. return exist;
  1072. }
  1073. sm = kmalloc(sizeof(*sm), GFP_NOFS);
  1074. if (!sm)
  1075. return NULL;
  1076. ret = get_anon_bdev(&sm->dev);
  1077. if (ret < 0) {
  1078. kfree(sm);
  1079. return NULL;
  1080. }
  1081. INIT_LIST_HEAD(&sm->lru);
  1082. atomic_set(&sm->ref, 1);
  1083. sm->snap = snap;
  1084. exist = NULL;
  1085. parent = NULL;
  1086. p = &mdsc->snapid_map_tree.rb_node;
  1087. spin_lock(&mdsc->snapid_map_lock);
  1088. while (*p) {
  1089. parent = *p;
  1090. exist = rb_entry(*p, struct ceph_snapid_map, node);
  1091. if (snap > exist->snap)
  1092. p = &(*p)->rb_left;
  1093. else if (snap < exist->snap)
  1094. p = &(*p)->rb_right;
  1095. else
  1096. break;
  1097. exist = NULL;
  1098. }
  1099. if (exist) {
  1100. if (atomic_inc_return(&exist->ref) == 1)
  1101. list_del_init(&exist->lru);
  1102. } else {
  1103. rb_link_node(&sm->node, parent, p);
  1104. rb_insert_color(&sm->node, &mdsc->snapid_map_tree);
  1105. }
  1106. spin_unlock(&mdsc->snapid_map_lock);
  1107. if (exist) {
  1108. free_anon_bdev(sm->dev);
  1109. kfree(sm);
  1110. doutc(cl, "found snapid map %llx -> %x\n", exist->snap,
  1111. exist->dev);
  1112. return exist;
  1113. }
  1114. doutc(cl, "create snapid map %llx -> %x\n", sm->snap, sm->dev);
  1115. return sm;
  1116. }
  1117. void ceph_put_snapid_map(struct ceph_mds_client* mdsc,
  1118. struct ceph_snapid_map *sm)
  1119. {
  1120. if (!sm)
  1121. return;
  1122. if (atomic_dec_and_lock(&sm->ref, &mdsc->snapid_map_lock)) {
  1123. if (!RB_EMPTY_NODE(&sm->node)) {
  1124. sm->last_used = jiffies;
  1125. list_add_tail(&sm->lru, &mdsc->snapid_map_lru);
  1126. spin_unlock(&mdsc->snapid_map_lock);
  1127. } else {
  1128. /* already cleaned up by
  1129. * ceph_cleanup_snapid_map() */
  1130. spin_unlock(&mdsc->snapid_map_lock);
  1131. kfree(sm);
  1132. }
  1133. }
  1134. }
  1135. void ceph_trim_snapid_map(struct ceph_mds_client *mdsc)
  1136. {
  1137. struct ceph_client *cl = mdsc->fsc->client;
  1138. struct ceph_snapid_map *sm;
  1139. unsigned long now;
  1140. LIST_HEAD(to_free);
  1141. spin_lock(&mdsc->snapid_map_lock);
  1142. now = jiffies;
  1143. while (!list_empty(&mdsc->snapid_map_lru)) {
  1144. sm = list_first_entry(&mdsc->snapid_map_lru,
  1145. struct ceph_snapid_map, lru);
  1146. if (time_after(sm->last_used + CEPH_SNAPID_MAP_TIMEOUT, now))
  1147. break;
  1148. rb_erase(&sm->node, &mdsc->snapid_map_tree);
  1149. list_move(&sm->lru, &to_free);
  1150. }
  1151. spin_unlock(&mdsc->snapid_map_lock);
  1152. while (!list_empty(&to_free)) {
  1153. sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
  1154. list_del(&sm->lru);
  1155. doutc(cl, "trim snapid map %llx -> %x\n", sm->snap, sm->dev);
  1156. free_anon_bdev(sm->dev);
  1157. kfree(sm);
  1158. }
  1159. }
  1160. void ceph_cleanup_snapid_map(struct ceph_mds_client *mdsc)
  1161. {
  1162. struct ceph_client *cl = mdsc->fsc->client;
  1163. struct ceph_snapid_map *sm;
  1164. struct rb_node *p;
  1165. LIST_HEAD(to_free);
  1166. spin_lock(&mdsc->snapid_map_lock);
  1167. while ((p = rb_first(&mdsc->snapid_map_tree))) {
  1168. sm = rb_entry(p, struct ceph_snapid_map, node);
  1169. rb_erase(p, &mdsc->snapid_map_tree);
  1170. RB_CLEAR_NODE(p);
  1171. list_move(&sm->lru, &to_free);
  1172. }
  1173. spin_unlock(&mdsc->snapid_map_lock);
  1174. while (!list_empty(&to_free)) {
  1175. sm = list_first_entry(&to_free, struct ceph_snapid_map, lru);
  1176. list_del(&sm->lru);
  1177. free_anon_bdev(sm->dev);
  1178. if (WARN_ON_ONCE(atomic_read(&sm->ref))) {
  1179. pr_err_client(cl, "snapid map %llx -> %x still in use\n",
  1180. sm->snap, sm->dev);
  1181. }
  1182. kfree(sm);
  1183. }
  1184. }