/fs/ceph/caps.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 3087 lines · 2272 code · 326 blank · 489 comment · 450 complexity · 420bae9a1f20f122fa6e49b116857865 MD5 · raw file

Large files are truncated click here to view the full file

  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/fs.h>
  3. #include <linux/kernel.h>
  4. #include <linux/sched.h>
  5. #include <linux/slab.h>
  6. #include <linux/vmalloc.h>
  7. #include <linux/wait.h>
  8. #include <linux/writeback.h>
  9. #include "super.h"
  10. #include "mds_client.h"
  11. #include <linux/ceph/decode.h>
  12. #include <linux/ceph/messenger.h>
  13. /*
  14. * Capability management
  15. *
  16. * The Ceph metadata servers control client access to inode metadata
  17. * and file data by issuing capabilities, granting clients permission
  18. * to read and/or write both inode field and file data to OSDs
  19. * (storage nodes). Each capability consists of a set of bits
  20. * indicating which operations are allowed.
  21. *
  22. * If the client holds a *_SHARED cap, the client has a coherent value
  23. * that can be safely read from the cached inode.
  24. *
  25. * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
  26. * client is allowed to change inode attributes (e.g., file size,
  27. * mtime), note its dirty state in the ceph_cap, and asynchronously
  28. * flush that metadata change to the MDS.
  29. *
  30. * In the event of a conflicting operation (perhaps by another
  31. * client), the MDS will revoke the conflicting client capabilities.
  32. *
  33. * In order for a client to cache an inode, it must hold a capability
  34. * with at least one MDS server. When inodes are released, release
  35. * notifications are batched and periodically sent en masse to the MDS
  36. * cluster to release server state.
  37. */
  38. /*
  39. * Generate readable cap strings for debugging output.
  40. */
  41. #define MAX_CAP_STR 20
  42. static char cap_str[MAX_CAP_STR][40];
  43. static DEFINE_SPINLOCK(cap_str_lock);
  44. static int last_cap_str;
  45. static char *gcap_string(char *s, int c)
  46. {
  47. if (c & CEPH_CAP_GSHARED)
  48. *s++ = 's';
  49. if (c & CEPH_CAP_GEXCL)
  50. *s++ = 'x';
  51. if (c & CEPH_CAP_GCACHE)
  52. *s++ = 'c';
  53. if (c & CEPH_CAP_GRD)
  54. *s++ = 'r';
  55. if (c & CEPH_CAP_GWR)
  56. *s++ = 'w';
  57. if (c & CEPH_CAP_GBUFFER)
  58. *s++ = 'b';
  59. if (c & CEPH_CAP_GLAZYIO)
  60. *s++ = 'l';
  61. return s;
  62. }
  63. const char *ceph_cap_string(int caps)
  64. {
  65. int i;
  66. char *s;
  67. int c;
  68. spin_lock(&cap_str_lock);
  69. i = last_cap_str++;
  70. if (last_cap_str == MAX_CAP_STR)
  71. last_cap_str = 0;
  72. spin_unlock(&cap_str_lock);
  73. s = cap_str[i];
  74. if (caps & CEPH_CAP_PIN)
  75. *s++ = 'p';
  76. c = (caps >> CEPH_CAP_SAUTH) & 3;
  77. if (c) {
  78. *s++ = 'A';
  79. s = gcap_string(s, c);
  80. }
  81. c = (caps >> CEPH_CAP_SLINK) & 3;
  82. if (c) {
  83. *s++ = 'L';
  84. s = gcap_string(s, c);
  85. }
  86. c = (caps >> CEPH_CAP_SXATTR) & 3;
  87. if (c) {
  88. *s++ = 'X';
  89. s = gcap_string(s, c);
  90. }
  91. c = caps >> CEPH_CAP_SFILE;
  92. if (c) {
  93. *s++ = 'F';
  94. s = gcap_string(s, c);
  95. }
  96. if (s == cap_str[i])
  97. *s++ = '-';
  98. *s = 0;
  99. return cap_str[i];
  100. }
  101. void ceph_caps_init(struct ceph_mds_client *mdsc)
  102. {
  103. INIT_LIST_HEAD(&mdsc->caps_list);
  104. spin_lock_init(&mdsc->caps_list_lock);
  105. }
  106. void ceph_caps_finalize(struct ceph_mds_client *mdsc)
  107. {
  108. struct ceph_cap *cap;
  109. spin_lock(&mdsc->caps_list_lock);
  110. while (!list_empty(&mdsc->caps_list)) {
  111. cap = list_first_entry(&mdsc->caps_list,
  112. struct ceph_cap, caps_item);
  113. list_del(&cap->caps_item);
  114. kmem_cache_free(ceph_cap_cachep, cap);
  115. }
  116. mdsc->caps_total_count = 0;
  117. mdsc->caps_avail_count = 0;
  118. mdsc->caps_use_count = 0;
  119. mdsc->caps_reserve_count = 0;
  120. mdsc->caps_min_count = 0;
  121. spin_unlock(&mdsc->caps_list_lock);
  122. }
  123. void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
  124. {
  125. spin_lock(&mdsc->caps_list_lock);
  126. mdsc->caps_min_count += delta;
  127. BUG_ON(mdsc->caps_min_count < 0);
  128. spin_unlock(&mdsc->caps_list_lock);
  129. }
  130. int ceph_reserve_caps(struct ceph_mds_client *mdsc,
  131. struct ceph_cap_reservation *ctx, int need)
  132. {
  133. int i;
  134. struct ceph_cap *cap;
  135. int have;
  136. int alloc = 0;
  137. LIST_HEAD(newcaps);
  138. int ret = 0;
  139. dout("reserve caps ctx=%p need=%d\n", ctx, need);
  140. /* first reserve any caps that are already allocated */
  141. spin_lock(&mdsc->caps_list_lock);
  142. if (mdsc->caps_avail_count >= need)
  143. have = need;
  144. else
  145. have = mdsc->caps_avail_count;
  146. mdsc->caps_avail_count -= have;
  147. mdsc->caps_reserve_count += have;
  148. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  149. mdsc->caps_reserve_count +
  150. mdsc->caps_avail_count);
  151. spin_unlock(&mdsc->caps_list_lock);
  152. for (i = have; i < need; i++) {
  153. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  154. if (!cap) {
  155. ret = -ENOMEM;
  156. goto out_alloc_count;
  157. }
  158. list_add(&cap->caps_item, &newcaps);
  159. alloc++;
  160. }
  161. BUG_ON(have + alloc != need);
  162. spin_lock(&mdsc->caps_list_lock);
  163. mdsc->caps_total_count += alloc;
  164. mdsc->caps_reserve_count += alloc;
  165. list_splice(&newcaps, &mdsc->caps_list);
  166. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  167. mdsc->caps_reserve_count +
  168. mdsc->caps_avail_count);
  169. spin_unlock(&mdsc->caps_list_lock);
  170. ctx->count = need;
  171. dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
  172. ctx, mdsc->caps_total_count, mdsc->caps_use_count,
  173. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  174. return 0;
  175. out_alloc_count:
  176. /* we didn't manage to reserve as much as we needed */
  177. pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
  178. ctx, need, have);
  179. return ret;
  180. }
  181. int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
  182. struct ceph_cap_reservation *ctx)
  183. {
  184. dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
  185. if (ctx->count) {
  186. spin_lock(&mdsc->caps_list_lock);
  187. BUG_ON(mdsc->caps_reserve_count < ctx->count);
  188. mdsc->caps_reserve_count -= ctx->count;
  189. mdsc->caps_avail_count += ctx->count;
  190. ctx->count = 0;
  191. dout("unreserve caps %d = %d used + %d resv + %d avail\n",
  192. mdsc->caps_total_count, mdsc->caps_use_count,
  193. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  194. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  195. mdsc->caps_reserve_count +
  196. mdsc->caps_avail_count);
  197. spin_unlock(&mdsc->caps_list_lock);
  198. }
  199. return 0;
  200. }
  201. static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc,
  202. struct ceph_cap_reservation *ctx)
  203. {
  204. struct ceph_cap *cap = NULL;
  205. /* temporary, until we do something about cap import/export */
  206. if (!ctx) {
  207. cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  208. if (cap) {
  209. mdsc->caps_use_count++;
  210. mdsc->caps_total_count++;
  211. }
  212. return cap;
  213. }
  214. spin_lock(&mdsc->caps_list_lock);
  215. dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
  216. ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
  217. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  218. BUG_ON(!ctx->count);
  219. BUG_ON(ctx->count > mdsc->caps_reserve_count);
  220. BUG_ON(list_empty(&mdsc->caps_list));
  221. ctx->count--;
  222. mdsc->caps_reserve_count--;
  223. mdsc->caps_use_count++;
  224. cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
  225. list_del(&cap->caps_item);
  226. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  227. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  228. spin_unlock(&mdsc->caps_list_lock);
  229. return cap;
  230. }
  231. void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
  232. {
  233. spin_lock(&mdsc->caps_list_lock);
  234. dout("put_cap %p %d = %d used + %d resv + %d avail\n",
  235. cap, mdsc->caps_total_count, mdsc->caps_use_count,
  236. mdsc->caps_reserve_count, mdsc->caps_avail_count);
  237. mdsc->caps_use_count--;
  238. /*
  239. * Keep some preallocated caps around (ceph_min_count), to
  240. * avoid lots of free/alloc churn.
  241. */
  242. if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
  243. mdsc->caps_min_count) {
  244. mdsc->caps_total_count--;
  245. kmem_cache_free(ceph_cap_cachep, cap);
  246. } else {
  247. mdsc->caps_avail_count++;
  248. list_add(&cap->caps_item, &mdsc->caps_list);
  249. }
  250. BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  251. mdsc->caps_reserve_count + mdsc->caps_avail_count);
  252. spin_unlock(&mdsc->caps_list_lock);
  253. }
  254. void ceph_reservation_status(struct ceph_fs_client *fsc,
  255. int *total, int *avail, int *used, int *reserved,
  256. int *min)
  257. {
  258. struct ceph_mds_client *mdsc = fsc->mdsc;
  259. if (total)
  260. *total = mdsc->caps_total_count;
  261. if (avail)
  262. *avail = mdsc->caps_avail_count;
  263. if (used)
  264. *used = mdsc->caps_use_count;
  265. if (reserved)
  266. *reserved = mdsc->caps_reserve_count;
  267. if (min)
  268. *min = mdsc->caps_min_count;
  269. }
  270. /*
  271. * Find ceph_cap for given mds, if any.
  272. *
  273. * Called with i_lock held.
  274. */
  275. static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  276. {
  277. struct ceph_cap *cap;
  278. struct rb_node *n = ci->i_caps.rb_node;
  279. while (n) {
  280. cap = rb_entry(n, struct ceph_cap, ci_node);
  281. if (mds < cap->mds)
  282. n = n->rb_left;
  283. else if (mds > cap->mds)
  284. n = n->rb_right;
  285. else
  286. return cap;
  287. }
  288. return NULL;
  289. }
  290. struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  291. {
  292. struct ceph_cap *cap;
  293. spin_lock(&ci->vfs_inode.i_lock);
  294. cap = __get_cap_for_mds(ci, mds);
  295. spin_unlock(&ci->vfs_inode.i_lock);
  296. return cap;
  297. }
  298. /*
  299. * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
  300. */
  301. static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
  302. {
  303. struct ceph_cap *cap;
  304. int mds = -1;
  305. struct rb_node *p;
  306. /* prefer mds with WR|BUFFER|EXCL caps */
  307. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  308. cap = rb_entry(p, struct ceph_cap, ci_node);
  309. mds = cap->mds;
  310. if (cap->issued & (CEPH_CAP_FILE_WR |
  311. CEPH_CAP_FILE_BUFFER |
  312. CEPH_CAP_FILE_EXCL))
  313. break;
  314. }
  315. return mds;
  316. }
  317. int ceph_get_cap_mds(struct inode *inode)
  318. {
  319. int mds;
  320. spin_lock(&inode->i_lock);
  321. mds = __ceph_get_cap_mds(ceph_inode(inode));
  322. spin_unlock(&inode->i_lock);
  323. return mds;
  324. }
  325. /*
  326. * Called under i_lock.
  327. */
  328. static void __insert_cap_node(struct ceph_inode_info *ci,
  329. struct ceph_cap *new)
  330. {
  331. struct rb_node **p = &ci->i_caps.rb_node;
  332. struct rb_node *parent = NULL;
  333. struct ceph_cap *cap = NULL;
  334. while (*p) {
  335. parent = *p;
  336. cap = rb_entry(parent, struct ceph_cap, ci_node);
  337. if (new->mds < cap->mds)
  338. p = &(*p)->rb_left;
  339. else if (new->mds > cap->mds)
  340. p = &(*p)->rb_right;
  341. else
  342. BUG();
  343. }
  344. rb_link_node(&new->ci_node, parent, p);
  345. rb_insert_color(&new->ci_node, &ci->i_caps);
  346. }
  347. /*
  348. * (re)set cap hold timeouts, which control the delayed release
  349. * of unused caps back to the MDS. Should be called on cap use.
  350. */
  351. static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
  352. struct ceph_inode_info *ci)
  353. {
  354. struct ceph_mount_options *ma = mdsc->fsc->mount_options;
  355. ci->i_hold_caps_min = round_jiffies(jiffies +
  356. ma->caps_wanted_delay_min * HZ);
  357. ci->i_hold_caps_max = round_jiffies(jiffies +
  358. ma->caps_wanted_delay_max * HZ);
  359. dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
  360. ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
  361. }
  362. /*
  363. * (Re)queue cap at the end of the delayed cap release list.
  364. *
  365. * If I_FLUSH is set, leave the inode at the front of the list.
  366. *
  367. * Caller holds i_lock
  368. * -> we take mdsc->cap_delay_lock
  369. */
  370. static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
  371. struct ceph_inode_info *ci)
  372. {
  373. __cap_set_timeouts(mdsc, ci);
  374. dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
  375. ci->i_ceph_flags, ci->i_hold_caps_max);
  376. if (!mdsc->stopping) {
  377. spin_lock(&mdsc->cap_delay_lock);
  378. if (!list_empty(&ci->i_cap_delay_list)) {
  379. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  380. goto no_change;
  381. list_del_init(&ci->i_cap_delay_list);
  382. }
  383. list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  384. no_change:
  385. spin_unlock(&mdsc->cap_delay_lock);
  386. }
  387. }
  388. /*
  389. * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
  390. * indicating we should send a cap message to flush dirty metadata
  391. * asap, and move to the front of the delayed cap list.
  392. */
  393. static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
  394. struct ceph_inode_info *ci)
  395. {
  396. dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
  397. spin_lock(&mdsc->cap_delay_lock);
  398. ci->i_ceph_flags |= CEPH_I_FLUSH;
  399. if (!list_empty(&ci->i_cap_delay_list))
  400. list_del_init(&ci->i_cap_delay_list);
  401. list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  402. spin_unlock(&mdsc->cap_delay_lock);
  403. }
  404. /*
  405. * Cancel delayed work on cap.
  406. *
  407. * Caller must hold i_lock.
  408. */
  409. static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
  410. struct ceph_inode_info *ci)
  411. {
  412. dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
  413. if (list_empty(&ci->i_cap_delay_list))
  414. return;
  415. spin_lock(&mdsc->cap_delay_lock);
  416. list_del_init(&ci->i_cap_delay_list);
  417. spin_unlock(&mdsc->cap_delay_lock);
  418. }
  419. /*
  420. * Common issue checks for add_cap, handle_cap_grant.
  421. */
  422. static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
  423. unsigned issued)
  424. {
  425. unsigned had = __ceph_caps_issued(ci, NULL);
  426. /*
  427. * Each time we receive FILE_CACHE anew, we increment
  428. * i_rdcache_gen.
  429. */
  430. if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
  431. (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
  432. ci->i_rdcache_gen++;
  433. /*
  434. * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
  435. * don't know what happened to this directory while we didn't
  436. * have the cap.
  437. */
  438. if ((issued & CEPH_CAP_FILE_SHARED) &&
  439. (had & CEPH_CAP_FILE_SHARED) == 0) {
  440. ci->i_shared_gen++;
  441. if (S_ISDIR(ci->vfs_inode.i_mode)) {
  442. dout(" marking %p NOT complete\n", &ci->vfs_inode);
  443. ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
  444. }
  445. }
  446. }
  447. /*
  448. * Add a capability under the given MDS session.
  449. *
  450. * Caller should hold session snap_rwsem (read) and s_mutex.
  451. *
  452. * @fmode is the open file mode, if we are opening a file, otherwise
  453. * it is < 0. (This is so we can atomically add the cap and add an
  454. * open file reference to it.)
  455. */
  456. int ceph_add_cap(struct inode *inode,
  457. struct ceph_mds_session *session, u64 cap_id,
  458. int fmode, unsigned issued, unsigned wanted,
  459. unsigned seq, unsigned mseq, u64 realmino, int flags,
  460. struct ceph_cap_reservation *caps_reservation)
  461. {
  462. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  463. struct ceph_inode_info *ci = ceph_inode(inode);
  464. struct ceph_cap *new_cap = NULL;
  465. struct ceph_cap *cap;
  466. int mds = session->s_mds;
  467. int actual_wanted;
  468. dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
  469. session->s_mds, cap_id, ceph_cap_string(issued), seq);
  470. /*
  471. * If we are opening the file, include file mode wanted bits
  472. * in wanted.
  473. */
  474. if (fmode >= 0)
  475. wanted |= ceph_caps_for_mode(fmode);
  476. retry:
  477. spin_lock(&inode->i_lock);
  478. cap = __get_cap_for_mds(ci, mds);
  479. if (!cap) {
  480. if (new_cap) {
  481. cap = new_cap;
  482. new_cap = NULL;
  483. } else {
  484. spin_unlock(&inode->i_lock);
  485. new_cap = get_cap(mdsc, caps_reservation);
  486. if (new_cap == NULL)
  487. return -ENOMEM;
  488. goto retry;
  489. }
  490. cap->issued = 0;
  491. cap->implemented = 0;
  492. cap->mds = mds;
  493. cap->mds_wanted = 0;
  494. cap->ci = ci;
  495. __insert_cap_node(ci, cap);
  496. /* clear out old exporting info? (i.e. on cap import) */
  497. if (ci->i_cap_exporting_mds == mds) {
  498. ci->i_cap_exporting_issued = 0;
  499. ci->i_cap_exporting_mseq = 0;
  500. ci->i_cap_exporting_mds = -1;
  501. }
  502. /* add to session cap list */
  503. cap->session = session;
  504. spin_lock(&session->s_cap_lock);
  505. list_add_tail(&cap->session_caps, &session->s_caps);
  506. session->s_nr_caps++;
  507. spin_unlock(&session->s_cap_lock);
  508. } else if (new_cap)
  509. ceph_put_cap(mdsc, new_cap);
  510. if (!ci->i_snap_realm) {
  511. /*
  512. * add this inode to the appropriate snap realm
  513. */
  514. struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
  515. realmino);
  516. if (realm) {
  517. ceph_get_snap_realm(mdsc, realm);
  518. spin_lock(&realm->inodes_with_caps_lock);
  519. ci->i_snap_realm = realm;
  520. list_add(&ci->i_snap_realm_item,
  521. &realm->inodes_with_caps);
  522. spin_unlock(&realm->inodes_with_caps_lock);
  523. } else {
  524. pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
  525. realmino);
  526. WARN_ON(!realm);
  527. }
  528. }
  529. __check_cap_issue(ci, cap, issued);
  530. /*
  531. * If we are issued caps we don't want, or the mds' wanted
  532. * value appears to be off, queue a check so we'll release
  533. * later and/or update the mds wanted value.
  534. */
  535. actual_wanted = __ceph_caps_wanted(ci);
  536. if ((wanted & ~actual_wanted) ||
  537. (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
  538. dout(" issued %s, mds wanted %s, actual %s, queueing\n",
  539. ceph_cap_string(issued), ceph_cap_string(wanted),
  540. ceph_cap_string(actual_wanted));
  541. __cap_delay_requeue(mdsc, ci);
  542. }
  543. if (flags & CEPH_CAP_FLAG_AUTH)
  544. ci->i_auth_cap = cap;
  545. else if (ci->i_auth_cap == cap)
  546. ci->i_auth_cap = NULL;
  547. dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
  548. inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
  549. ceph_cap_string(issued|cap->issued), seq, mds);
  550. cap->cap_id = cap_id;
  551. cap->issued = issued;
  552. cap->implemented |= issued;
  553. cap->mds_wanted |= wanted;
  554. cap->seq = seq;
  555. cap->issue_seq = seq;
  556. cap->mseq = mseq;
  557. cap->cap_gen = session->s_cap_gen;
  558. if (fmode >= 0)
  559. __ceph_get_fmode(ci, fmode);
  560. spin_unlock(&inode->i_lock);
  561. wake_up_all(&ci->i_cap_wq);
  562. return 0;
  563. }
  564. /*
  565. * Return true if cap has not timed out and belongs to the current
  566. * generation of the MDS session (i.e. has not gone 'stale' due to
  567. * us losing touch with the mds).
  568. */
  569. static int __cap_is_valid(struct ceph_cap *cap)
  570. {
  571. unsigned long ttl;
  572. u32 gen;
  573. spin_lock(&cap->session->s_cap_lock);
  574. gen = cap->session->s_cap_gen;
  575. ttl = cap->session->s_cap_ttl;
  576. spin_unlock(&cap->session->s_cap_lock);
  577. if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
  578. dout("__cap_is_valid %p cap %p issued %s "
  579. "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
  580. cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
  581. return 0;
  582. }
  583. return 1;
  584. }
  585. /*
  586. * Return set of valid cap bits issued to us. Note that caps time
  587. * out, and may be invalidated in bulk if the client session times out
  588. * and session->s_cap_gen is bumped.
  589. */
  590. int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
  591. {
  592. int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
  593. struct ceph_cap *cap;
  594. struct rb_node *p;
  595. if (implemented)
  596. *implemented = 0;
  597. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  598. cap = rb_entry(p, struct ceph_cap, ci_node);
  599. if (!__cap_is_valid(cap))
  600. continue;
  601. dout("__ceph_caps_issued %p cap %p issued %s\n",
  602. &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
  603. have |= cap->issued;
  604. if (implemented)
  605. *implemented |= cap->implemented;
  606. }
  607. return have;
  608. }
  609. /*
  610. * Get cap bits issued by caps other than @ocap
  611. */
  612. int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
  613. {
  614. int have = ci->i_snap_caps;
  615. struct ceph_cap *cap;
  616. struct rb_node *p;
  617. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  618. cap = rb_entry(p, struct ceph_cap, ci_node);
  619. if (cap == ocap)
  620. continue;
  621. if (!__cap_is_valid(cap))
  622. continue;
  623. have |= cap->issued;
  624. }
  625. return have;
  626. }
  627. /*
  628. * Move a cap to the end of the LRU (oldest caps at list head, newest
  629. * at list tail).
  630. */
  631. static void __touch_cap(struct ceph_cap *cap)
  632. {
  633. struct ceph_mds_session *s = cap->session;
  634. spin_lock(&s->s_cap_lock);
  635. if (s->s_cap_iterator == NULL) {
  636. dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
  637. s->s_mds);
  638. list_move_tail(&cap->session_caps, &s->s_caps);
  639. } else {
  640. dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
  641. &cap->ci->vfs_inode, cap, s->s_mds);
  642. }
  643. spin_unlock(&s->s_cap_lock);
  644. }
  645. /*
  646. * Check if we hold the given mask. If so, move the cap(s) to the
  647. * front of their respective LRUs. (This is the preferred way for
  648. * callers to check for caps they want.)
  649. */
  650. int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
  651. {
  652. struct ceph_cap *cap;
  653. struct rb_node *p;
  654. int have = ci->i_snap_caps;
  655. if ((have & mask) == mask) {
  656. dout("__ceph_caps_issued_mask %p snap issued %s"
  657. " (mask %s)\n", &ci->vfs_inode,
  658. ceph_cap_string(have),
  659. ceph_cap_string(mask));
  660. return 1;
  661. }
  662. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  663. cap = rb_entry(p, struct ceph_cap, ci_node);
  664. if (!__cap_is_valid(cap))
  665. continue;
  666. if ((cap->issued & mask) == mask) {
  667. dout("__ceph_caps_issued_mask %p cap %p issued %s"
  668. " (mask %s)\n", &ci->vfs_inode, cap,
  669. ceph_cap_string(cap->issued),
  670. ceph_cap_string(mask));
  671. if (touch)
  672. __touch_cap(cap);
  673. return 1;
  674. }
  675. /* does a combination of caps satisfy mask? */
  676. have |= cap->issued;
  677. if ((have & mask) == mask) {
  678. dout("__ceph_caps_issued_mask %p combo issued %s"
  679. " (mask %s)\n", &ci->vfs_inode,
  680. ceph_cap_string(cap->issued),
  681. ceph_cap_string(mask));
  682. if (touch) {
  683. struct rb_node *q;
  684. /* touch this + preceding caps */
  685. __touch_cap(cap);
  686. for (q = rb_first(&ci->i_caps); q != p;
  687. q = rb_next(q)) {
  688. cap = rb_entry(q, struct ceph_cap,
  689. ci_node);
  690. if (!__cap_is_valid(cap))
  691. continue;
  692. __touch_cap(cap);
  693. }
  694. }
  695. return 1;
  696. }
  697. }
  698. return 0;
  699. }
  700. /*
  701. * Return true if mask caps are currently being revoked by an MDS.
  702. */
  703. int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
  704. {
  705. struct inode *inode = &ci->vfs_inode;
  706. struct ceph_cap *cap;
  707. struct rb_node *p;
  708. int ret = 0;
  709. spin_lock(&inode->i_lock);
  710. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  711. cap = rb_entry(p, struct ceph_cap, ci_node);
  712. if (__cap_is_valid(cap) &&
  713. (cap->implemented & ~cap->issued & mask)) {
  714. ret = 1;
  715. break;
  716. }
  717. }
  718. spin_unlock(&inode->i_lock);
  719. dout("ceph_caps_revoking %p %s = %d\n", inode,
  720. ceph_cap_string(mask), ret);
  721. return ret;
  722. }
  723. int __ceph_caps_used(struct ceph_inode_info *ci)
  724. {
  725. int used = 0;
  726. if (ci->i_pin_ref)
  727. used |= CEPH_CAP_PIN;
  728. if (ci->i_rd_ref)
  729. used |= CEPH_CAP_FILE_RD;
  730. if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages)
  731. used |= CEPH_CAP_FILE_CACHE;
  732. if (ci->i_wr_ref)
  733. used |= CEPH_CAP_FILE_WR;
  734. if (ci->i_wb_ref || ci->i_wrbuffer_ref)
  735. used |= CEPH_CAP_FILE_BUFFER;
  736. return used;
  737. }
  738. /*
  739. * wanted, by virtue of open file modes
  740. */
  741. int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
  742. {
  743. int want = 0;
  744. int mode;
  745. for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
  746. if (ci->i_nr_by_mode[mode])
  747. want |= ceph_caps_for_mode(mode);
  748. return want;
  749. }
  750. /*
  751. * Return caps we have registered with the MDS(s) as 'wanted'.
  752. */
  753. int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
  754. {
  755. struct ceph_cap *cap;
  756. struct rb_node *p;
  757. int mds_wanted = 0;
  758. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  759. cap = rb_entry(p, struct ceph_cap, ci_node);
  760. if (!__cap_is_valid(cap))
  761. continue;
  762. mds_wanted |= cap->mds_wanted;
  763. }
  764. return mds_wanted;
  765. }
  766. /*
  767. * called under i_lock
  768. */
  769. static int __ceph_is_any_caps(struct ceph_inode_info *ci)
  770. {
  771. return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
  772. }
  773. /*
  774. * Remove a cap. Take steps to deal with a racing iterate_session_caps.
  775. *
  776. * caller should hold i_lock.
  777. * caller will not hold session s_mutex if called from destroy_inode.
  778. */
  779. void __ceph_remove_cap(struct ceph_cap *cap)
  780. {
  781. struct ceph_mds_session *session = cap->session;
  782. struct ceph_inode_info *ci = cap->ci;
  783. struct ceph_mds_client *mdsc =
  784. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  785. int removed = 0;
  786. dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
  787. /* remove from session list */
  788. spin_lock(&session->s_cap_lock);
  789. if (session->s_cap_iterator == cap) {
  790. /* not yet, we are iterating over this very cap */
  791. dout("__ceph_remove_cap delaying %p removal from session %p\n",
  792. cap, cap->session);
  793. } else {
  794. list_del_init(&cap->session_caps);
  795. session->s_nr_caps--;
  796. cap->session = NULL;
  797. removed = 1;
  798. }
  799. /* protect backpointer with s_cap_lock: see iterate_session_caps */
  800. cap->ci = NULL;
  801. spin_unlock(&session->s_cap_lock);
  802. /* remove from inode list */
  803. rb_erase(&cap->ci_node, &ci->i_caps);
  804. if (ci->i_auth_cap == cap)
  805. ci->i_auth_cap = NULL;
  806. if (removed)
  807. ceph_put_cap(mdsc, cap);
  808. if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
  809. struct ceph_snap_realm *realm = ci->i_snap_realm;
  810. spin_lock(&realm->inodes_with_caps_lock);
  811. list_del_init(&ci->i_snap_realm_item);
  812. ci->i_snap_realm_counter++;
  813. ci->i_snap_realm = NULL;
  814. spin_unlock(&realm->inodes_with_caps_lock);
  815. ceph_put_snap_realm(mdsc, realm);
  816. }
  817. if (!__ceph_is_any_real_caps(ci))
  818. __cap_delay_cancel(mdsc, ci);
  819. }
  820. /*
  821. * Build and send a cap message to the given MDS.
  822. *
  823. * Caller should be holding s_mutex.
  824. */
  825. static int send_cap_msg(struct ceph_mds_session *session,
  826. u64 ino, u64 cid, int op,
  827. int caps, int wanted, int dirty,
  828. u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
  829. u64 size, u64 max_size,
  830. struct timespec *mtime, struct timespec *atime,
  831. u64 time_warp_seq,
  832. uid_t uid, gid_t gid, mode_t mode,
  833. u64 xattr_version,
  834. struct ceph_buffer *xattrs_buf,
  835. u64 follows)
  836. {
  837. struct ceph_mds_caps *fc;
  838. struct ceph_msg *msg;
  839. dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
  840. " seq %u/%u mseq %u follows %lld size %llu/%llu"
  841. " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
  842. cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
  843. ceph_cap_string(dirty),
  844. seq, issue_seq, mseq, follows, size, max_size,
  845. xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
  846. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS);
  847. if (!msg)
  848. return -ENOMEM;
  849. msg->hdr.tid = cpu_to_le64(flush_tid);
  850. fc = msg->front.iov_base;
  851. memset(fc, 0, sizeof(*fc));
  852. fc->cap_id = cpu_to_le64(cid);
  853. fc->op = cpu_to_le32(op);
  854. fc->seq = cpu_to_le32(seq);
  855. fc->issue_seq = cpu_to_le32(issue_seq);
  856. fc->migrate_seq = cpu_to_le32(mseq);
  857. fc->caps = cpu_to_le32(caps);
  858. fc->wanted = cpu_to_le32(wanted);
  859. fc->dirty = cpu_to_le32(dirty);
  860. fc->ino = cpu_to_le64(ino);
  861. fc->snap_follows = cpu_to_le64(follows);
  862. fc->size = cpu_to_le64(size);
  863. fc->max_size = cpu_to_le64(max_size);
  864. if (mtime)
  865. ceph_encode_timespec(&fc->mtime, mtime);
  866. if (atime)
  867. ceph_encode_timespec(&fc->atime, atime);
  868. fc->time_warp_seq = cpu_to_le32(time_warp_seq);
  869. fc->uid = cpu_to_le32(uid);
  870. fc->gid = cpu_to_le32(gid);
  871. fc->mode = cpu_to_le32(mode);
  872. fc->xattr_version = cpu_to_le64(xattr_version);
  873. if (xattrs_buf) {
  874. msg->middle = ceph_buffer_get(xattrs_buf);
  875. fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  876. msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
  877. }
  878. ceph_con_send(&session->s_con, msg);
  879. return 0;
  880. }
  881. static void __queue_cap_release(struct ceph_mds_session *session,
  882. u64 ino, u64 cap_id, u32 migrate_seq,
  883. u32 issue_seq)
  884. {
  885. struct ceph_msg *msg;
  886. struct ceph_mds_cap_release *head;
  887. struct ceph_mds_cap_item *item;
  888. spin_lock(&session->s_cap_lock);
  889. BUG_ON(!session->s_num_cap_releases);
  890. msg = list_first_entry(&session->s_cap_releases,
  891. struct ceph_msg, list_head);
  892. dout(" adding %llx release to mds%d msg %p (%d left)\n",
  893. ino, session->s_mds, msg, session->s_num_cap_releases);
  894. BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
  895. head = msg->front.iov_base;
  896. head->num = cpu_to_le32(le32_to_cpu(head->num) + 1);
  897. item = msg->front.iov_base + msg->front.iov_len;
  898. item->ino = cpu_to_le64(ino);
  899. item->cap_id = cpu_to_le64(cap_id);
  900. item->migrate_seq = cpu_to_le32(migrate_seq);
  901. item->seq = cpu_to_le32(issue_seq);
  902. session->s_num_cap_releases--;
  903. msg->front.iov_len += sizeof(*item);
  904. if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
  905. dout(" release msg %p full\n", msg);
  906. list_move_tail(&msg->list_head, &session->s_cap_releases_done);
  907. } else {
  908. dout(" release msg %p at %d/%d (%d)\n", msg,
  909. (int)le32_to_cpu(head->num),
  910. (int)CEPH_CAPS_PER_RELEASE,
  911. (int)msg->front.iov_len);
  912. }
  913. spin_unlock(&session->s_cap_lock);
  914. }
  915. /*
  916. * Queue cap releases when an inode is dropped from our cache. Since
  917. * inode is about to be destroyed, there is no need for i_lock.
  918. */
  919. void ceph_queue_caps_release(struct inode *inode)
  920. {
  921. struct ceph_inode_info *ci = ceph_inode(inode);
  922. struct rb_node *p;
  923. p = rb_first(&ci->i_caps);
  924. while (p) {
  925. struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
  926. struct ceph_mds_session *session = cap->session;
  927. __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
  928. cap->mseq, cap->issue_seq);
  929. p = rb_next(p);
  930. __ceph_remove_cap(cap);
  931. }
  932. }
  933. /*
  934. * Send a cap msg on the given inode. Update our caps state, then
  935. * drop i_lock and send the message.
  936. *
  937. * Make note of max_size reported/requested from mds, revoked caps
  938. * that have now been implemented.
  939. *
  940. * Make half-hearted attempt ot to invalidate page cache if we are
  941. * dropping RDCACHE. Note that this will leave behind locked pages
  942. * that we'll then need to deal with elsewhere.
  943. *
  944. * Return non-zero if delayed release, or we experienced an error
  945. * such that the caller should requeue + retry later.
  946. *
  947. * called with i_lock, then drops it.
  948. * caller should hold snap_rwsem (read), s_mutex.
  949. */
  950. static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
  951. int op, int used, int want, int retain, int flushing,
  952. unsigned *pflush_tid)
  953. __releases(cap->ci->vfs_inode->i_lock)
  954. {
  955. struct ceph_inode_info *ci = cap->ci;
  956. struct inode *inode = &ci->vfs_inode;
  957. u64 cap_id = cap->cap_id;
  958. int held, revoking, dropping, keep;
  959. u64 seq, issue_seq, mseq, time_warp_seq, follows;
  960. u64 size, max_size;
  961. struct timespec mtime, atime;
  962. int wake = 0;
  963. mode_t mode;
  964. uid_t uid;
  965. gid_t gid;
  966. struct ceph_mds_session *session;
  967. u64 xattr_version = 0;
  968. struct ceph_buffer *xattr_blob = NULL;
  969. int delayed = 0;
  970. u64 flush_tid = 0;
  971. int i;
  972. int ret;
  973. held = cap->issued | cap->implemented;
  974. revoking = cap->implemented & ~cap->issued;
  975. retain &= ~revoking;
  976. dropping = cap->issued & ~retain;
  977. dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
  978. inode, cap, cap->session,
  979. ceph_cap_string(held), ceph_cap_string(held & retain),
  980. ceph_cap_string(revoking));
  981. BUG_ON((retain & CEPH_CAP_PIN) == 0);
  982. session = cap->session;
  983. /* don't release wanted unless we've waited a bit. */
  984. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  985. time_before(jiffies, ci->i_hold_caps_min)) {
  986. dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
  987. ceph_cap_string(cap->issued),
  988. ceph_cap_string(cap->issued & retain),
  989. ceph_cap_string(cap->mds_wanted),
  990. ceph_cap_string(want));
  991. want |= cap->mds_wanted;
  992. retain |= cap->issued;
  993. delayed = 1;
  994. }
  995. ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
  996. cap->issued &= retain; /* drop bits we don't want */
  997. if (cap->implemented & ~cap->issued) {
  998. /*
  999. * Wake up any waiters on wanted -> needed transition.
  1000. * This is due to the weird transition from buffered
  1001. * to sync IO... we need to flush dirty pages _before_
  1002. * allowing sync writes to avoid reordering.
  1003. */
  1004. wake = 1;
  1005. }
  1006. cap->implemented &= cap->issued | used;
  1007. cap->mds_wanted = want;
  1008. if (flushing) {
  1009. /*
  1010. * assign a tid for flush operations so we can avoid
  1011. * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
  1012. * clean type races. track latest tid for every bit
  1013. * so we can handle flush AxFw, flush Fw, and have the
  1014. * first ack clean Ax.
  1015. */
  1016. flush_tid = ++ci->i_cap_flush_last_tid;
  1017. if (pflush_tid)
  1018. *pflush_tid = flush_tid;
  1019. dout(" cap_flush_tid %d\n", (int)flush_tid);
  1020. for (i = 0; i < CEPH_CAP_BITS; i++)
  1021. if (flushing & (1 << i))
  1022. ci->i_cap_flush_tid[i] = flush_tid;
  1023. follows = ci->i_head_snapc->seq;
  1024. } else {
  1025. follows = 0;
  1026. }
  1027. keep = cap->implemented;
  1028. seq = cap->seq;
  1029. issue_seq = cap->issue_seq;
  1030. mseq = cap->mseq;
  1031. size = inode->i_size;
  1032. ci->i_reported_size = size;
  1033. max_size = ci->i_wanted_max_size;
  1034. ci->i_requested_max_size = max_size;
  1035. mtime = inode->i_mtime;
  1036. atime = inode->i_atime;
  1037. time_warp_seq = ci->i_time_warp_seq;
  1038. uid = inode->i_uid;
  1039. gid = inode->i_gid;
  1040. mode = inode->i_mode;
  1041. if (flushing & CEPH_CAP_XATTR_EXCL) {
  1042. __ceph_build_xattrs_blob(ci);
  1043. xattr_blob = ci->i_xattrs.blob;
  1044. xattr_version = ci->i_xattrs.version;
  1045. }
  1046. spin_unlock(&inode->i_lock);
  1047. ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
  1048. op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
  1049. size, max_size, &mtime, &atime, time_warp_seq,
  1050. uid, gid, mode, xattr_version, xattr_blob,
  1051. follows);
  1052. if (ret < 0) {
  1053. dout("error sending cap msg, must requeue %p\n", inode);
  1054. delayed = 1;
  1055. }
  1056. if (wake)
  1057. wake_up_all(&ci->i_cap_wq);
  1058. return delayed;
  1059. }
  1060. /*
  1061. * When a snapshot is taken, clients accumulate dirty metadata on
  1062. * inodes with capabilities in ceph_cap_snaps to describe the file
  1063. * state at the time the snapshot was taken. This must be flushed
  1064. * asynchronously back to the MDS once sync writes complete and dirty
  1065. * data is written out.
  1066. *
  1067. * Unless @again is true, skip cap_snaps that were already sent to
  1068. * the MDS (i.e., during this session).
  1069. *
  1070. * Called under i_lock. Takes s_mutex as needed.
  1071. */
  1072. void __ceph_flush_snaps(struct ceph_inode_info *ci,
  1073. struct ceph_mds_session **psession,
  1074. int again)
  1075. __releases(ci->vfs_inode->i_lock)
  1076. __acquires(ci->vfs_inode->i_lock)
  1077. {
  1078. struct inode *inode = &ci->vfs_inode;
  1079. int mds;
  1080. struct ceph_cap_snap *capsnap;
  1081. u32 mseq;
  1082. struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
  1083. struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
  1084. session->s_mutex */
  1085. u64 next_follows = 0; /* keep track of how far we've gotten through the
  1086. i_cap_snaps list, and skip these entries next time
  1087. around to avoid an infinite loop */
  1088. if (psession)
  1089. session = *psession;
  1090. dout("__flush_snaps %p\n", inode);
  1091. retry:
  1092. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  1093. /* avoid an infiniute loop after retry */
  1094. if (capsnap->follows < next_follows)
  1095. continue;
  1096. /*
  1097. * we need to wait for sync writes to complete and for dirty
  1098. * pages to be written out.
  1099. */
  1100. if (capsnap->dirty_pages || capsnap->writing)
  1101. break;
  1102. /*
  1103. * if cap writeback already occurred, we should have dropped
  1104. * the capsnap in ceph_put_wrbuffer_cap_refs.
  1105. */
  1106. BUG_ON(capsnap->dirty == 0);
  1107. /* pick mds, take s_mutex */
  1108. if (ci->i_auth_cap == NULL) {
  1109. dout("no auth cap (migrating?), doing nothing\n");
  1110. goto out;
  1111. }
  1112. /* only flush each capsnap once */
  1113. if (!again && !list_empty(&capsnap->flushing_item)) {
  1114. dout("already flushed %p, skipping\n", capsnap);
  1115. continue;
  1116. }
  1117. mds = ci->i_auth_cap->session->s_mds;
  1118. mseq = ci->i_auth_cap->mseq;
  1119. if (session && session->s_mds != mds) {
  1120. dout("oops, wrong session %p mutex\n", session);
  1121. mutex_unlock(&session->s_mutex);
  1122. ceph_put_mds_session(session);
  1123. session = NULL;
  1124. }
  1125. if (!session) {
  1126. spin_unlock(&inode->i_lock);
  1127. mutex_lock(&mdsc->mutex);
  1128. session = __ceph_lookup_mds_session(mdsc, mds);
  1129. mutex_unlock(&mdsc->mutex);
  1130. if (session) {
  1131. dout("inverting session/ino locks on %p\n",
  1132. session);
  1133. mutex_lock(&session->s_mutex);
  1134. }
  1135. /*
  1136. * if session == NULL, we raced against a cap
  1137. * deletion or migration. retry, and we'll
  1138. * get a better @mds value next time.
  1139. */
  1140. spin_lock(&inode->i_lock);
  1141. goto retry;
  1142. }
  1143. capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
  1144. atomic_inc(&capsnap->nref);
  1145. if (!list_empty(&capsnap->flushing_item))
  1146. list_del_init(&capsnap->flushing_item);
  1147. list_add_tail(&capsnap->flushing_item,
  1148. &session->s_cap_snaps_flushing);
  1149. spin_unlock(&inode->i_lock);
  1150. dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
  1151. inode, capsnap, capsnap->follows, capsnap->flush_tid);
  1152. send_cap_msg(session, ceph_vino(inode).ino, 0,
  1153. CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
  1154. capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
  1155. capsnap->size, 0,
  1156. &capsnap->mtime, &capsnap->atime,
  1157. capsnap->time_warp_seq,
  1158. capsnap->uid, capsnap->gid, capsnap->mode,
  1159. capsnap->xattr_version, capsnap->xattr_blob,
  1160. capsnap->follows);
  1161. next_follows = capsnap->follows + 1;
  1162. ceph_put_cap_snap(capsnap);
  1163. spin_lock(&inode->i_lock);
  1164. goto retry;
  1165. }
  1166. /* we flushed them all; remove this inode from the queue */
  1167. spin_lock(&mdsc->snap_flush_lock);
  1168. list_del_init(&ci->i_snap_flush_item);
  1169. spin_unlock(&mdsc->snap_flush_lock);
  1170. out:
  1171. if (psession)
  1172. *psession = session;
  1173. else if (session) {
  1174. mutex_unlock(&session->s_mutex);
  1175. ceph_put_mds_session(session);
  1176. }
  1177. }
  1178. static void ceph_flush_snaps(struct ceph_inode_info *ci)
  1179. {
  1180. struct inode *inode = &ci->vfs_inode;
  1181. spin_lock(&inode->i_lock);
  1182. __ceph_flush_snaps(ci, NULL, 0);
  1183. spin_unlock(&inode->i_lock);
  1184. }
  1185. /*
  1186. * Mark caps dirty. If inode is newly dirty, return the dirty flags.
  1187. * Caller is then responsible for calling __mark_inode_dirty with the
  1188. * returned flags value.
  1189. */
  1190. int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
  1191. {
  1192. struct ceph_mds_client *mdsc =
  1193. ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
  1194. struct inode *inode = &ci->vfs_inode;
  1195. int was = ci->i_dirty_caps;
  1196. int dirty = 0;
  1197. dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
  1198. ceph_cap_string(mask), ceph_cap_string(was),
  1199. ceph_cap_string(was | mask));
  1200. ci->i_dirty_caps |= mask;
  1201. if (was == 0) {
  1202. if (!ci->i_head_snapc)
  1203. ci->i_head_snapc = ceph_get_snap_context(
  1204. ci->i_snap_realm->cached_context);
  1205. dout(" inode %p now dirty snapc %p\n", &ci->vfs_inode,
  1206. ci->i_head_snapc);
  1207. BUG_ON(!list_empty(&ci->i_dirty_item));
  1208. spin_lock(&mdsc->cap_dirty_lock);
  1209. list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
  1210. spin_unlock(&mdsc->cap_dirty_lock);
  1211. if (ci->i_flushing_caps == 0) {
  1212. ihold(inode);
  1213. dirty |= I_DIRTY_SYNC;
  1214. }
  1215. }
  1216. BUG_ON(list_empty(&ci->i_dirty_item));
  1217. if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  1218. (mask & CEPH_CAP_FILE_BUFFER))
  1219. dirty |= I_DIRTY_DATASYNC;
  1220. __cap_delay_requeue(mdsc, ci);
  1221. return dirty;
  1222. }
  1223. /*
  1224. * Add dirty inode to the flushing list. Assigned a seq number so we
  1225. * can wait for caps to flush without starving.
  1226. *
  1227. * Called under i_lock.
  1228. */
  1229. static int __mark_caps_flushing(struct inode *inode,
  1230. struct ceph_mds_session *session)
  1231. {
  1232. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1233. struct ceph_inode_info *ci = ceph_inode(inode);
  1234. int flushing;
  1235. BUG_ON(ci->i_dirty_caps == 0);
  1236. BUG_ON(list_empty(&ci->i_dirty_item));
  1237. flushing = ci->i_dirty_caps;
  1238. dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
  1239. ceph_cap_string(flushing),
  1240. ceph_cap_string(ci->i_flushing_caps),
  1241. ceph_cap_string(ci->i_flushing_caps | flushing));
  1242. ci->i_flushing_caps |= flushing;
  1243. ci->i_dirty_caps = 0;
  1244. dout(" inode %p now !dirty\n", inode);
  1245. spin_lock(&mdsc->cap_dirty_lock);
  1246. list_del_init(&ci->i_dirty_item);
  1247. ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
  1248. if (list_empty(&ci->i_flushing_item)) {
  1249. list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1250. mdsc->num_cap_flushing++;
  1251. dout(" inode %p now flushing seq %lld\n", inode,
  1252. ci->i_cap_flush_seq);
  1253. } else {
  1254. list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  1255. dout(" inode %p now flushing (more) seq %lld\n", inode,
  1256. ci->i_cap_flush_seq);
  1257. }
  1258. spin_unlock(&mdsc->cap_dirty_lock);
  1259. return flushing;
  1260. }
  1261. /*
  1262. * try to invalidate mapping pages without blocking.
  1263. */
  1264. static int try_nonblocking_invalidate(struct inode *inode)
  1265. {
  1266. struct ceph_inode_info *ci = ceph_inode(inode);
  1267. u32 invalidating_gen = ci->i_rdcache_gen;
  1268. spin_unlock(&inode->i_lock);
  1269. invalidate_mapping_pages(&inode->i_data, 0, -1);
  1270. spin_lock(&inode->i_lock);
  1271. if (inode->i_data.nrpages == 0 &&
  1272. invalidating_gen == ci->i_rdcache_gen) {
  1273. /* success. */
  1274. dout("try_nonblocking_invalidate %p success\n", inode);
  1275. /* save any racing async invalidate some trouble */
  1276. ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
  1277. return 0;
  1278. }
  1279. dout("try_nonblocking_invalidate %p failed\n", inode);
  1280. return -1;
  1281. }
  1282. /*
  1283. * Swiss army knife function to examine currently used and wanted
  1284. * versus held caps. Release, flush, ack revoked caps to mds as
  1285. * appropriate.
  1286. *
  1287. * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
  1288. * cap release further.
  1289. * CHECK_CAPS_AUTHONLY - we should only check the auth cap
  1290. * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
  1291. * further delay.
  1292. */
  1293. void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  1294. struct ceph_mds_session *session)
  1295. {
  1296. struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
  1297. struct ceph_mds_client *mdsc = fsc->mdsc;
  1298. struct inode *inode = &ci->vfs_inode;
  1299. struct ceph_cap *cap;
  1300. int file_wanted, used;
  1301. int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
  1302. int issued, implemented, want, retain, revoking, flushing = 0;
  1303. int mds = -1; /* keep track of how far we've gone through i_caps list
  1304. to avoid an infinite loop on retry */
  1305. struct rb_node *p;
  1306. int tried_invalidate = 0;
  1307. int delayed = 0, sent = 0, force_requeue = 0, num;
  1308. int queue_invalidate = 0;
  1309. int is_delayed = flags & CHECK_CAPS_NODELAY;
  1310. /* if we are unmounting, flush any unused caps immediately. */
  1311. if (mdsc->stopping)
  1312. is_delayed = 1;
  1313. spin_lock(&inode->i_lock);
  1314. if (ci->i_ceph_flags & CEPH_I_FLUSH)
  1315. flags |= CHECK_CAPS_FLUSH;
  1316. /* flush snaps first time around only */
  1317. if (!list_empty(&ci->i_cap_snaps))
  1318. __ceph_flush_snaps(ci, &session, 0);
  1319. goto retry_locked;
  1320. retry:
  1321. spin_lock(&inode->i_lock);
  1322. retry_locked:
  1323. file_wanted = __ceph_caps_file_wanted(ci);
  1324. used = __ceph_caps_used(ci);
  1325. want = file_wanted | used;
  1326. issued = __ceph_caps_issued(ci, &implemented);
  1327. revoking = implemented & ~issued;
  1328. retain = want | CEPH_CAP_PIN;
  1329. if (!mdsc->stopping && inode->i_nlink > 0) {
  1330. if (want) {
  1331. retain |= CEPH_CAP_ANY; /* be greedy */
  1332. } else {
  1333. retain |= CEPH_CAP_ANY_SHARED;
  1334. /*
  1335. * keep RD only if we didn't have the file open RW,
  1336. * because then the mds would revoke it anyway to
  1337. * journal max_size=0.
  1338. */
  1339. if (ci->i_max_size == 0)
  1340. retain |= CEPH_CAP_ANY_RD;
  1341. }
  1342. }
  1343. dout("check_caps %p file_want %s used %s dirty %s flushing %s"
  1344. " issued %s revoking %s retain %s %s%s%s\n", inode,
  1345. ceph_cap_string(file_wanted),
  1346. ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  1347. ceph_cap_string(ci->i_flushing_caps),
  1348. ceph_cap_string(issued), ceph_cap_string(revoking),
  1349. ceph_cap_string(retain),
  1350. (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
  1351. (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
  1352. (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  1353. /*
  1354. * If we no longer need to hold onto old our caps, and we may
  1355. * have cached pages, but don't want them, then try to invalidate.
  1356. * If we fail, it's because pages are locked.... try again later.
  1357. */
  1358. if ((!is_delayed || mdsc->stopping) &&
  1359. ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
  1360. inode->i_data.nrpages && /* have cached pages */
  1361. (file_wanted == 0 || /* no open files */
  1362. (revoking & (CEPH_CAP_FILE_CACHE|
  1363. CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
  1364. !tried_invalidate) {
  1365. dout("check_caps trying to invalidate on %p\n", inode);
  1366. if (try_nonblocking_invalidate(inode) < 0) {
  1367. if (revoking & (CEPH_CAP_FILE_CACHE|
  1368. CEPH_CAP_FILE_LAZYIO)) {
  1369. dout("check_caps queuing invalidate\n");
  1370. queue_invalidate = 1;
  1371. ci->i_rdcache_revoking = ci->i_rdcache_gen;
  1372. } else {
  1373. dout("check_caps failed to invalidate pages\n");
  1374. /* we failed to invalidate pages. check these
  1375. caps again later. */
  1376. force_requeue = 1;
  1377. __cap_set_timeouts(mdsc, ci);
  1378. }
  1379. }
  1380. tried_invalidate = 1;
  1381. goto retry_locked;
  1382. }
  1383. num = 0;
  1384. for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  1385. cap = rb_entry(p, struct ceph_cap, ci_node);
  1386. num++;
  1387. /* avoid looping forever */
  1388. if (mds >= cap->mds ||
  1389. ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  1390. continue;
  1391. /* NOTE: no side-effects allowed, until we take s_mutex */
  1392. revoking = cap->implemented & ~cap->issued;
  1393. dout(" mds%d cap %p issued %s implemented %s revoking %s\n",
  1394. cap->mds, cap, ceph_cap_string(cap->issued),
  1395. ceph_cap_string(cap->implemented),
  1396. ceph_cap_string(revoking));
  1397. if (cap == ci->i_auth_cap &&
  1398. (cap->issued & CEPH_CAP_FILE_WR)) {
  1399. /* request larger max_size from MDS? */
  1400. if (ci->i_wanted_max_size > ci->i_max_size &&
  1401. ci->i_wanted_max_size > ci->i_requested_max_size) {
  1402. dout("requesting new max_size\n");
  1403. goto ack;
  1404. }
  1405. /* approaching file_max? */
  1406. if ((inode->i_size << 1) >= ci->i_max_size &&
  1407. (ci->i_reported_size << 1) < ci->i_max_size) {
  1408. dout("i_size approaching max_size\n");
  1409. goto ack;
  1410. }
  1411. }
  1412. /* flush anything dirty? */
  1413. if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
  1414. ci->i_dirty_caps) {
  1415. dout("flushing dirty caps\n");
  1416. goto ack;
  1417. }
  1418. /* completed revocation? going down and there are no caps? */
  1419. if (revoking && (revoking & used) == 0) {
  1420. dout("completed revocation of %s\n",
  1421. ceph_cap_string(cap->implemented & ~cap->issued));
  1422. goto ack;
  1423. }
  1424. /* want more caps from mds? */
  1425. if (want & ~(cap->mds_wanted | cap->issued))
  1426. goto ack;
  1427. /* things we might delay */
  1428. if ((cap->issued & ~retain) == 0 &&
  1429. cap->mds_wanted == want)
  1430. continue; /* nope, all good */
  1431. if (is_delayed)
  1432. goto ack;
  1433. /* delay? */
  1434. if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
  1435. time_before(jiffies, ci->i_hold_caps_max)) {
  1436. dout(" delaying issued %s -> %s, wanted %s -> %s\n",
  1437. ceph_cap_string(cap->issued),
  1438. ceph_cap_string(cap->issued & retain),
  1439. ceph_cap_string(cap->mds_wanted),
  1440. ceph_cap_string(want));
  1441. delayed++;
  1442. continue;
  1443. }
  1444. ack:
  1445. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1446. dout(" skipping %p I_NOFLUSH set\n", inode);
  1447. continue;
  1448. }
  1449. if (session && session != cap->session) {
  1450. dout("oops, wrong session %p mutex\n", session);
  1451. mutex_unlock(&session->s_mutex);
  1452. session = NULL;
  1453. }
  1454. if (!session) {
  1455. session = cap->session;
  1456. if (mutex_trylock(&session->s_mutex) == 0) {
  1457. dout("inverting session/ino locks on %p\n",
  1458. session);
  1459. spin_unlock(&inode->i_lock);
  1460. if (took_snap_rwsem) {
  1461. up_read(&mdsc->snap_rwsem);
  1462. took_snap_rwsem = 0;
  1463. }
  1464. mutex_lock(&session->s_mutex);
  1465. goto retry;
  1466. }
  1467. }
  1468. /* take snap_rwsem after session mutex */
  1469. if (!took_snap_rwsem) {
  1470. if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  1471. dout("inverting snap/in locks on %p\n",
  1472. inode);
  1473. spin_unlock(&inode->i_lock);
  1474. down_read(&mdsc->snap_rwsem);
  1475. took_snap_rwsem = 1;
  1476. goto retry;
  1477. }
  1478. took_snap_rwsem = 1;
  1479. }
  1480. if (cap == ci->i_auth_cap && ci->i_dirty_caps)
  1481. flushing = __mark_caps_flushing(inode, session);
  1482. else
  1483. flushing = 0;
  1484. mds = cap->mds; /* remember mds, so we don't repeat */
  1485. sent++;
  1486. /* __send_cap drops i_lock */
  1487. delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
  1488. retain, flushing, NULL);
  1489. goto retry; /* retake i_lock and restart our cap scan. */
  1490. }
  1491. /*
  1492. * Reschedule delayed caps release if we delayed anything,
  1493. * otherwise cancel.
  1494. */
  1495. if (delayed && is_delayed)
  1496. force_requeue = 1; /* __send_cap delayed release; requeue */
  1497. if (!delayed && !is_delayed)
  1498. __cap_delay_cancel(mdsc, ci);
  1499. else if (!is_delayed || force_requeue)
  1500. __cap_delay_requeue(mdsc, ci);
  1501. spin_unlock(&inode->i_lock);
  1502. if (queue_invalidate)
  1503. ceph_queue_invalidate(inode);
  1504. if (session)
  1505. mutex_unlock(&session->s_mutex);
  1506. if (took_snap_rwsem)
  1507. up_read(&mdsc->snap_rwsem);
  1508. }
  1509. /*
  1510. * Try to flush dirty caps back to the auth mds.
  1511. */
  1512. static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
  1513. unsigned *flush_tid)
  1514. {
  1515. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  1516. struct ceph_inode_info *ci = ceph_inode(inode);
  1517. int unlock_session = session ? 0 : 1;
  1518. int flushing = 0;
  1519. retry:
  1520. spin_lock(&inode->i_lock);
  1521. if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
  1522. dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
  1523. goto out;
  1524. }
  1525. if (ci->i_dirty_caps && ci->i_auth_cap) {
  1526. struct ceph_cap *cap = ci->i_auth_cap;
  1527. int used = __ceph_caps_used(ci);
  1528. int want = __ceph_caps_wanted(ci);
  1529. int delayed;
  1530. if (!session) {
  1531. spin_unlock(&inode->i_lock);
  1532. session = cap->session;
  1533. mutex_lock(&session->s_mutex);
  1534. goto retry;
  1535. }
  1536. BUG_ON(session != cap->session);
  1537. if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
  1538. goto out;
  1539. flushing = __mark_caps_flushing(inode, session);
  1540. /* __send_cap drops i_lock */
  1541. delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
  1542. cap->issued | cap->implemented, flushing,
  1543. flush_tid);
  1544. if (!delayed)
  1545. goto out_unlocked;
  1546. spin_lock(&inode->i_lock);
  1547. __cap_delay_requeue(mdsc, ci);
  1548. }
  1549. out:
  1550. spin_unlock(&inode->i_lock);
  1551. out_unlocked:
  1552. if (session && unlock_session)
  1553. mutex_unlock(&session->s_mutex);
  1554. return flushing;
  1555. }
  1556. /*
  1557. * Return true if we've flushed caps through the given flush_tid.
  1558. */
  1559. static int caps_are_flushed(struct inode *inode, unsigned tid)
  1560. {
  1561. struct ceph_inode_info *ci = ceph_inode(inode);
  1562. int i, ret = 1;
  1563. spin_lock(&inode->i_lock);
  1564. for (i = 0; i < CEPH_CAP_BITS; i++)
  1565. if ((ci->i_flushing_caps & (1 << i)) &&
  1566. ci->i_cap_flush_tid[i] <= tid) {
  1567. /* still flushing this bit */
  1568. ret = 0;
  1569. break;
  1570. }
  1571. spin_unlock(&inode->i_lock);
  1572. return ret;
  1573. }
  1574. /*
  1575. * Wait on any unsafe replies for the given inode. First wait on the
  1576. * newest request, and make that the upper bound. Then, if there are
  1577. * more requests, keep waiting on the oldest as long as it is still older
  1578. * than the original request.
  1579. */
  1580. static void sync_write_wait(struct inode *inode)
  1581. {
  1582. struct ceph_inode_info *ci = ceph_inode(inode);
  1583. struct list_head *head = &ci->i_unsafe_writes;
  1584. struct ceph_osd_request *req;
  1585. u64 last_tid;
  1586. spin_lock(&ci->i_unsafe_lock);
  1587. if (list_empty(head))
  1588. goto out;
  1589. /* set upper bound as _last_ entry in chain */
  1590. req = list_entry(head->prev, struct ceph_osd_request,
  1591. r_unsafe_item);
  1592. last_tid = req->r_tid;
  1593. do {
  1594. ceph_osdc_get_request(req);
  1595. spin_unlock(&ci->i_unsafe_lock);
  1596. dout("sync_write_wait on tid %llu (until %llu)\n",
  1597. req->r_tid, last_tid);
  1598. wait_for_completion(&req->r_safe_completion);
  1599. spin_lock(&ci->i_unsafe_lock);
  1600. ceph_osdc_put_request(req);
  1601. /*
  1602. * from here on look at first entry in chain, since we
  1603. * only want to wait for anything older than last_tid
  1604. */
  1605. if (list_empty(head))
  1606. break;
  1607. req = list_entry(head->next, struct ceph_osd_request,
  1608. r_unsafe_item);
  1609. } while (req->r_tid < last_tid);
  1610. out:
  1611. spin_unlock(&ci->i_unsafe_lock);
  1612. }
  1613. int cep