ceph: connect to export targets on cap export
[linux-2.6/btrfs-unstable.git] / fs / ceph / caps.c
blob52befa65fbf7f9a2aaf040f680f9e1ed8c7c8040
1 #include "ceph_debug.h"
3 #include <linux/fs.h>
4 #include <linux/kernel.h>
5 #include <linux/sched.h>
6 #include <linux/slab.h>
7 #include <linux/vmalloc.h>
8 #include <linux/wait.h>
9 #include <linux/writeback.h>
11 #include "super.h"
12 #include "decode.h"
13 #include "messenger.h"
16 * Capability management
18 * The Ceph metadata servers control client access to inode metadata
19 * and file data by issuing capabilities, granting clients permission
20 * to read and/or write both inode field and file data to OSDs
21 * (storage nodes). Each capability consists of a set of bits
22 * indicating which operations are allowed.
24 * If the client holds a *_SHARED cap, the client has a coherent value
25 * that can be safely read from the cached inode.
27 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
28 * client is allowed to change inode attributes (e.g., file size,
29 * mtime), note its dirty state in the ceph_cap, and asynchronously
30 * flush that metadata change to the MDS.
32 * In the event of a conflicting operation (perhaps by another
33 * client), the MDS will revoke the conflicting client capabilities.
35 * In order for a client to cache an inode, it must hold a capability
36 * with at least one MDS server. When inodes are released, release
37 * notifications are batched and periodically sent en masse to the MDS
38 * cluster to release server state.
43 * Generate readable cap strings for debugging output.
45 #define MAX_CAP_STR 20
46 static char cap_str[MAX_CAP_STR][40];
47 static DEFINE_SPINLOCK(cap_str_lock);
48 static int last_cap_str;
50 static char *gcap_string(char *s, int c)
52 if (c & CEPH_CAP_GSHARED)
53 *s++ = 's';
54 if (c & CEPH_CAP_GEXCL)
55 *s++ = 'x';
56 if (c & CEPH_CAP_GCACHE)
57 *s++ = 'c';
58 if (c & CEPH_CAP_GRD)
59 *s++ = 'r';
60 if (c & CEPH_CAP_GWR)
61 *s++ = 'w';
62 if (c & CEPH_CAP_GBUFFER)
63 *s++ = 'b';
64 if (c & CEPH_CAP_GLAZYIO)
65 *s++ = 'l';
66 return s;
69 const char *ceph_cap_string(int caps)
71 int i;
72 char *s;
73 int c;
75 spin_lock(&cap_str_lock);
76 i = last_cap_str++;
77 if (last_cap_str == MAX_CAP_STR)
78 last_cap_str = 0;
79 spin_unlock(&cap_str_lock);
81 s = cap_str[i];
83 if (caps & CEPH_CAP_PIN)
84 *s++ = 'p';
86 c = (caps >> CEPH_CAP_SAUTH) & 3;
87 if (c) {
88 *s++ = 'A';
89 s = gcap_string(s, c);
92 c = (caps >> CEPH_CAP_SLINK) & 3;
93 if (c) {
94 *s++ = 'L';
95 s = gcap_string(s, c);
98 c = (caps >> CEPH_CAP_SXATTR) & 3;
99 if (c) {
100 *s++ = 'X';
101 s = gcap_string(s, c);
104 c = caps >> CEPH_CAP_SFILE;
105 if (c) {
106 *s++ = 'F';
107 s = gcap_string(s, c);
110 if (s == cap_str[i])
111 *s++ = '-';
112 *s = 0;
113 return cap_str[i];
116 void ceph_caps_init(struct ceph_mds_client *mdsc)
118 INIT_LIST_HEAD(&mdsc->caps_list);
119 spin_lock_init(&mdsc->caps_list_lock);
122 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
124 struct ceph_cap *cap;
126 spin_lock(&mdsc->caps_list_lock);
127 while (!list_empty(&mdsc->caps_list)) {
128 cap = list_first_entry(&mdsc->caps_list,
129 struct ceph_cap, caps_item);
130 list_del(&cap->caps_item);
131 kmem_cache_free(ceph_cap_cachep, cap);
133 mdsc->caps_total_count = 0;
134 mdsc->caps_avail_count = 0;
135 mdsc->caps_use_count = 0;
136 mdsc->caps_reserve_count = 0;
137 mdsc->caps_min_count = 0;
138 spin_unlock(&mdsc->caps_list_lock);
141 void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
143 spin_lock(&mdsc->caps_list_lock);
144 mdsc->caps_min_count += delta;
145 BUG_ON(mdsc->caps_min_count < 0);
146 spin_unlock(&mdsc->caps_list_lock);
149 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
150 struct ceph_cap_reservation *ctx, int need)
152 int i;
153 struct ceph_cap *cap;
154 int have;
155 int alloc = 0;
156 LIST_HEAD(newcaps);
157 int ret = 0;
159 dout("reserve caps ctx=%p need=%d\n", ctx, need);
161 /* first reserve any caps that are already allocated */
162 spin_lock(&mdsc->caps_list_lock);
163 if (mdsc->caps_avail_count >= need)
164 have = need;
165 else
166 have = mdsc->caps_avail_count;
167 mdsc->caps_avail_count -= have;
168 mdsc->caps_reserve_count += have;
169 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
170 mdsc->caps_reserve_count +
171 mdsc->caps_avail_count);
172 spin_unlock(&mdsc->caps_list_lock);
174 for (i = have; i < need; i++) {
175 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
176 if (!cap) {
177 ret = -ENOMEM;
178 goto out_alloc_count;
180 list_add(&cap->caps_item, &newcaps);
181 alloc++;
183 BUG_ON(have + alloc != need);
185 spin_lock(&mdsc->caps_list_lock);
186 mdsc->caps_total_count += alloc;
187 mdsc->caps_reserve_count += alloc;
188 list_splice(&newcaps, &mdsc->caps_list);
190 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
191 mdsc->caps_reserve_count +
192 mdsc->caps_avail_count);
193 spin_unlock(&mdsc->caps_list_lock);
195 ctx->count = need;
196 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
197 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
198 mdsc->caps_reserve_count, mdsc->caps_avail_count);
199 return 0;
201 out_alloc_count:
202 /* we didn't manage to reserve as much as we needed */
203 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
204 ctx, need, have);
205 return ret;
208 int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
209 struct ceph_cap_reservation *ctx)
211 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
212 if (ctx->count) {
213 spin_lock(&mdsc->caps_list_lock);
214 BUG_ON(mdsc->caps_reserve_count < ctx->count);
215 mdsc->caps_reserve_count -= ctx->count;
216 mdsc->caps_avail_count += ctx->count;
217 ctx->count = 0;
218 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
219 mdsc->caps_total_count, mdsc->caps_use_count,
220 mdsc->caps_reserve_count, mdsc->caps_avail_count);
221 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
222 mdsc->caps_reserve_count +
223 mdsc->caps_avail_count);
224 spin_unlock(&mdsc->caps_list_lock);
226 return 0;
229 static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc,
230 struct ceph_cap_reservation *ctx)
232 struct ceph_cap *cap = NULL;
234 /* temporary, until we do something about cap import/export */
235 if (!ctx) {
236 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
237 if (cap) {
238 mdsc->caps_use_count++;
239 mdsc->caps_total_count++;
241 return cap;
244 spin_lock(&mdsc->caps_list_lock);
245 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
246 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
247 mdsc->caps_reserve_count, mdsc->caps_avail_count);
248 BUG_ON(!ctx->count);
249 BUG_ON(ctx->count > mdsc->caps_reserve_count);
250 BUG_ON(list_empty(&mdsc->caps_list));
252 ctx->count--;
253 mdsc->caps_reserve_count--;
254 mdsc->caps_use_count++;
256 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
257 list_del(&cap->caps_item);
259 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
260 mdsc->caps_reserve_count + mdsc->caps_avail_count);
261 spin_unlock(&mdsc->caps_list_lock);
262 return cap;
265 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
267 spin_lock(&mdsc->caps_list_lock);
268 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
269 cap, mdsc->caps_total_count, mdsc->caps_use_count,
270 mdsc->caps_reserve_count, mdsc->caps_avail_count);
271 mdsc->caps_use_count--;
273 * Keep some preallocated caps around (ceph_min_count), to
274 * avoid lots of free/alloc churn.
276 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
277 mdsc->caps_min_count) {
278 mdsc->caps_total_count--;
279 kmem_cache_free(ceph_cap_cachep, cap);
280 } else {
281 mdsc->caps_avail_count++;
282 list_add(&cap->caps_item, &mdsc->caps_list);
285 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
286 mdsc->caps_reserve_count + mdsc->caps_avail_count);
287 spin_unlock(&mdsc->caps_list_lock);
290 void ceph_reservation_status(struct ceph_client *client,
291 int *total, int *avail, int *used, int *reserved,
292 int *min)
294 struct ceph_mds_client *mdsc = &client->mdsc;
296 if (total)
297 *total = mdsc->caps_total_count;
298 if (avail)
299 *avail = mdsc->caps_avail_count;
300 if (used)
301 *used = mdsc->caps_use_count;
302 if (reserved)
303 *reserved = mdsc->caps_reserve_count;
304 if (min)
305 *min = mdsc->caps_min_count;
309 * Find ceph_cap for given mds, if any.
311 * Called with i_lock held.
313 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
315 struct ceph_cap *cap;
316 struct rb_node *n = ci->i_caps.rb_node;
318 while (n) {
319 cap = rb_entry(n, struct ceph_cap, ci_node);
320 if (mds < cap->mds)
321 n = n->rb_left;
322 else if (mds > cap->mds)
323 n = n->rb_right;
324 else
325 return cap;
327 return NULL;
331 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
333 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
335 struct ceph_cap *cap;
336 int mds = -1;
337 struct rb_node *p;
339 /* prefer mds with WR|BUFFER|EXCL caps */
340 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
341 cap = rb_entry(p, struct ceph_cap, ci_node);
342 mds = cap->mds;
343 if (cap->issued & (CEPH_CAP_FILE_WR |
344 CEPH_CAP_FILE_BUFFER |
345 CEPH_CAP_FILE_EXCL))
346 break;
348 return mds;
351 int ceph_get_cap_mds(struct inode *inode)
353 int mds;
354 spin_lock(&inode->i_lock);
355 mds = __ceph_get_cap_mds(ceph_inode(inode));
356 spin_unlock(&inode->i_lock);
357 return mds;
361 * Called under i_lock.
363 static void __insert_cap_node(struct ceph_inode_info *ci,
364 struct ceph_cap *new)
366 struct rb_node **p = &ci->i_caps.rb_node;
367 struct rb_node *parent = NULL;
368 struct ceph_cap *cap = NULL;
370 while (*p) {
371 parent = *p;
372 cap = rb_entry(parent, struct ceph_cap, ci_node);
373 if (new->mds < cap->mds)
374 p = &(*p)->rb_left;
375 else if (new->mds > cap->mds)
376 p = &(*p)->rb_right;
377 else
378 BUG();
381 rb_link_node(&new->ci_node, parent, p);
382 rb_insert_color(&new->ci_node, &ci->i_caps);
386 * (re)set cap hold timeouts, which control the delayed release
387 * of unused caps back to the MDS. Should be called on cap use.
389 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
390 struct ceph_inode_info *ci)
392 struct ceph_mount_args *ma = mdsc->client->mount_args;
394 ci->i_hold_caps_min = round_jiffies(jiffies +
395 ma->caps_wanted_delay_min * HZ);
396 ci->i_hold_caps_max = round_jiffies(jiffies +
397 ma->caps_wanted_delay_max * HZ);
398 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
399 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
403 * (Re)queue cap at the end of the delayed cap release list.
405 * If I_FLUSH is set, leave the inode at the front of the list.
407 * Caller holds i_lock
408 * -> we take mdsc->cap_delay_lock
410 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
411 struct ceph_inode_info *ci)
413 __cap_set_timeouts(mdsc, ci);
414 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
415 ci->i_ceph_flags, ci->i_hold_caps_max);
416 if (!mdsc->stopping) {
417 spin_lock(&mdsc->cap_delay_lock);
418 if (!list_empty(&ci->i_cap_delay_list)) {
419 if (ci->i_ceph_flags & CEPH_I_FLUSH)
420 goto no_change;
421 list_del_init(&ci->i_cap_delay_list);
423 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
424 no_change:
425 spin_unlock(&mdsc->cap_delay_lock);
430 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
431 * indicating we should send a cap message to flush dirty metadata
432 * asap, and move to the front of the delayed cap list.
434 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
435 struct ceph_inode_info *ci)
437 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
438 spin_lock(&mdsc->cap_delay_lock);
439 ci->i_ceph_flags |= CEPH_I_FLUSH;
440 if (!list_empty(&ci->i_cap_delay_list))
441 list_del_init(&ci->i_cap_delay_list);
442 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
443 spin_unlock(&mdsc->cap_delay_lock);
447 * Cancel delayed work on cap.
449 * Caller must hold i_lock.
451 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
452 struct ceph_inode_info *ci)
454 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
455 if (list_empty(&ci->i_cap_delay_list))
456 return;
457 spin_lock(&mdsc->cap_delay_lock);
458 list_del_init(&ci->i_cap_delay_list);
459 spin_unlock(&mdsc->cap_delay_lock);
463 * Common issue checks for add_cap, handle_cap_grant.
465 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
466 unsigned issued)
468 unsigned had = __ceph_caps_issued(ci, NULL);
471 * Each time we receive FILE_CACHE anew, we increment
472 * i_rdcache_gen.
474 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
475 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
476 ci->i_rdcache_gen++;
479 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
480 * don't know what happened to this directory while we didn't
481 * have the cap.
483 if ((issued & CEPH_CAP_FILE_SHARED) &&
484 (had & CEPH_CAP_FILE_SHARED) == 0) {
485 ci->i_shared_gen++;
486 if (S_ISDIR(ci->vfs_inode.i_mode)) {
487 dout(" marking %p NOT complete\n", &ci->vfs_inode);
488 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
494 * Add a capability under the given MDS session.
496 * Caller should hold session snap_rwsem (read) and s_mutex.
498 * @fmode is the open file mode, if we are opening a file, otherwise
499 * it is < 0. (This is so we can atomically add the cap and add an
500 * open file reference to it.)
502 int ceph_add_cap(struct inode *inode,
503 struct ceph_mds_session *session, u64 cap_id,
504 int fmode, unsigned issued, unsigned wanted,
505 unsigned seq, unsigned mseq, u64 realmino, int flags,
506 struct ceph_cap_reservation *caps_reservation)
508 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
509 struct ceph_inode_info *ci = ceph_inode(inode);
510 struct ceph_cap *new_cap = NULL;
511 struct ceph_cap *cap;
512 int mds = session->s_mds;
513 int actual_wanted;
515 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
516 session->s_mds, cap_id, ceph_cap_string(issued), seq);
519 * If we are opening the file, include file mode wanted bits
520 * in wanted.
522 if (fmode >= 0)
523 wanted |= ceph_caps_for_mode(fmode);
525 retry:
526 spin_lock(&inode->i_lock);
527 cap = __get_cap_for_mds(ci, mds);
528 if (!cap) {
529 if (new_cap) {
530 cap = new_cap;
531 new_cap = NULL;
532 } else {
533 spin_unlock(&inode->i_lock);
534 new_cap = get_cap(mdsc, caps_reservation);
535 if (new_cap == NULL)
536 return -ENOMEM;
537 goto retry;
540 cap->issued = 0;
541 cap->implemented = 0;
542 cap->mds = mds;
543 cap->mds_wanted = 0;
545 cap->ci = ci;
546 __insert_cap_node(ci, cap);
548 /* clear out old exporting info? (i.e. on cap import) */
549 if (ci->i_cap_exporting_mds == mds) {
550 ci->i_cap_exporting_issued = 0;
551 ci->i_cap_exporting_mseq = 0;
552 ci->i_cap_exporting_mds = -1;
555 /* add to session cap list */
556 cap->session = session;
557 spin_lock(&session->s_cap_lock);
558 list_add_tail(&cap->session_caps, &session->s_caps);
559 session->s_nr_caps++;
560 spin_unlock(&session->s_cap_lock);
563 if (!ci->i_snap_realm) {
565 * add this inode to the appropriate snap realm
567 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
568 realmino);
569 if (realm) {
570 ceph_get_snap_realm(mdsc, realm);
571 spin_lock(&realm->inodes_with_caps_lock);
572 ci->i_snap_realm = realm;
573 list_add(&ci->i_snap_realm_item,
574 &realm->inodes_with_caps);
575 spin_unlock(&realm->inodes_with_caps_lock);
576 } else {
577 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
578 realmino);
582 __check_cap_issue(ci, cap, issued);
585 * If we are issued caps we don't want, or the mds' wanted
586 * value appears to be off, queue a check so we'll release
587 * later and/or update the mds wanted value.
589 actual_wanted = __ceph_caps_wanted(ci);
590 if ((wanted & ~actual_wanted) ||
591 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
592 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
593 ceph_cap_string(issued), ceph_cap_string(wanted),
594 ceph_cap_string(actual_wanted));
595 __cap_delay_requeue(mdsc, ci);
598 if (flags & CEPH_CAP_FLAG_AUTH)
599 ci->i_auth_cap = cap;
600 else if (ci->i_auth_cap == cap)
601 ci->i_auth_cap = NULL;
603 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
604 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
605 ceph_cap_string(issued|cap->issued), seq, mds);
606 cap->cap_id = cap_id;
607 cap->issued = issued;
608 cap->implemented |= issued;
609 cap->mds_wanted |= wanted;
610 cap->seq = seq;
611 cap->issue_seq = seq;
612 cap->mseq = mseq;
613 cap->cap_gen = session->s_cap_gen;
615 if (fmode >= 0)
616 __ceph_get_fmode(ci, fmode);
617 spin_unlock(&inode->i_lock);
618 wake_up_all(&ci->i_cap_wq);
619 return 0;
623 * Return true if cap has not timed out and belongs to the current
624 * generation of the MDS session (i.e. has not gone 'stale' due to
625 * us losing touch with the mds).
627 static int __cap_is_valid(struct ceph_cap *cap)
629 unsigned long ttl;
630 u32 gen;
632 spin_lock(&cap->session->s_cap_lock);
633 gen = cap->session->s_cap_gen;
634 ttl = cap->session->s_cap_ttl;
635 spin_unlock(&cap->session->s_cap_lock);
637 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
638 dout("__cap_is_valid %p cap %p issued %s "
639 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
640 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
641 return 0;
644 return 1;
648 * Return set of valid cap bits issued to us. Note that caps time
649 * out, and may be invalidated in bulk if the client session times out
650 * and session->s_cap_gen is bumped.
652 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
654 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
655 struct ceph_cap *cap;
656 struct rb_node *p;
658 if (implemented)
659 *implemented = 0;
660 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
661 cap = rb_entry(p, struct ceph_cap, ci_node);
662 if (!__cap_is_valid(cap))
663 continue;
664 dout("__ceph_caps_issued %p cap %p issued %s\n",
665 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
666 have |= cap->issued;
667 if (implemented)
668 *implemented |= cap->implemented;
670 return have;
674 * Get cap bits issued by caps other than @ocap
676 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
678 int have = ci->i_snap_caps;
679 struct ceph_cap *cap;
680 struct rb_node *p;
682 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
683 cap = rb_entry(p, struct ceph_cap, ci_node);
684 if (cap == ocap)
685 continue;
686 if (!__cap_is_valid(cap))
687 continue;
688 have |= cap->issued;
690 return have;
694 * Move a cap to the end of the LRU (oldest caps at list head, newest
695 * at list tail).
697 static void __touch_cap(struct ceph_cap *cap)
699 struct ceph_mds_session *s = cap->session;
701 spin_lock(&s->s_cap_lock);
702 if (s->s_cap_iterator == NULL) {
703 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
704 s->s_mds);
705 list_move_tail(&cap->session_caps, &s->s_caps);
706 } else {
707 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
708 &cap->ci->vfs_inode, cap, s->s_mds);
710 spin_unlock(&s->s_cap_lock);
714 * Check if we hold the given mask. If so, move the cap(s) to the
715 * front of their respective LRUs. (This is the preferred way for
716 * callers to check for caps they want.)
718 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
720 struct ceph_cap *cap;
721 struct rb_node *p;
722 int have = ci->i_snap_caps;
724 if ((have & mask) == mask) {
725 dout("__ceph_caps_issued_mask %p snap issued %s"
726 " (mask %s)\n", &ci->vfs_inode,
727 ceph_cap_string(have),
728 ceph_cap_string(mask));
729 return 1;
732 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
733 cap = rb_entry(p, struct ceph_cap, ci_node);
734 if (!__cap_is_valid(cap))
735 continue;
736 if ((cap->issued & mask) == mask) {
737 dout("__ceph_caps_issued_mask %p cap %p issued %s"
738 " (mask %s)\n", &ci->vfs_inode, cap,
739 ceph_cap_string(cap->issued),
740 ceph_cap_string(mask));
741 if (touch)
742 __touch_cap(cap);
743 return 1;
746 /* does a combination of caps satisfy mask? */
747 have |= cap->issued;
748 if ((have & mask) == mask) {
749 dout("__ceph_caps_issued_mask %p combo issued %s"
750 " (mask %s)\n", &ci->vfs_inode,
751 ceph_cap_string(cap->issued),
752 ceph_cap_string(mask));
753 if (touch) {
754 struct rb_node *q;
756 /* touch this + preceeding caps */
757 __touch_cap(cap);
758 for (q = rb_first(&ci->i_caps); q != p;
759 q = rb_next(q)) {
760 cap = rb_entry(q, struct ceph_cap,
761 ci_node);
762 if (!__cap_is_valid(cap))
763 continue;
764 __touch_cap(cap);
767 return 1;
771 return 0;
775 * Return true if mask caps are currently being revoked by an MDS.
777 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
779 struct inode *inode = &ci->vfs_inode;
780 struct ceph_cap *cap;
781 struct rb_node *p;
782 int ret = 0;
784 spin_lock(&inode->i_lock);
785 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
786 cap = rb_entry(p, struct ceph_cap, ci_node);
787 if (__cap_is_valid(cap) &&
788 (cap->implemented & ~cap->issued & mask)) {
789 ret = 1;
790 break;
793 spin_unlock(&inode->i_lock);
794 dout("ceph_caps_revoking %p %s = %d\n", inode,
795 ceph_cap_string(mask), ret);
796 return ret;
799 int __ceph_caps_used(struct ceph_inode_info *ci)
801 int used = 0;
802 if (ci->i_pin_ref)
803 used |= CEPH_CAP_PIN;
804 if (ci->i_rd_ref)
805 used |= CEPH_CAP_FILE_RD;
806 if (ci->i_rdcache_ref || ci->i_rdcache_gen)
807 used |= CEPH_CAP_FILE_CACHE;
808 if (ci->i_wr_ref)
809 used |= CEPH_CAP_FILE_WR;
810 if (ci->i_wrbuffer_ref)
811 used |= CEPH_CAP_FILE_BUFFER;
812 return used;
816 * wanted, by virtue of open file modes
818 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
820 int want = 0;
821 int mode;
822 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
823 if (ci->i_nr_by_mode[mode])
824 want |= ceph_caps_for_mode(mode);
825 return want;
829 * Return caps we have registered with the MDS(s) as 'wanted'.
831 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
833 struct ceph_cap *cap;
834 struct rb_node *p;
835 int mds_wanted = 0;
837 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
838 cap = rb_entry(p, struct ceph_cap, ci_node);
839 if (!__cap_is_valid(cap))
840 continue;
841 mds_wanted |= cap->mds_wanted;
843 return mds_wanted;
847 * called under i_lock
849 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
851 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
855 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
857 * caller should hold i_lock.
858 * caller will not hold session s_mutex if called from destroy_inode.
860 void __ceph_remove_cap(struct ceph_cap *cap)
862 struct ceph_mds_session *session = cap->session;
863 struct ceph_inode_info *ci = cap->ci;
864 struct ceph_mds_client *mdsc =
865 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
866 int removed = 0;
868 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
870 /* remove from session list */
871 spin_lock(&session->s_cap_lock);
872 if (session->s_cap_iterator == cap) {
873 /* not yet, we are iterating over this very cap */
874 dout("__ceph_remove_cap delaying %p removal from session %p\n",
875 cap, cap->session);
876 } else {
877 list_del_init(&cap->session_caps);
878 session->s_nr_caps--;
879 cap->session = NULL;
880 removed = 1;
882 /* protect backpointer with s_cap_lock: see iterate_session_caps */
883 cap->ci = NULL;
884 spin_unlock(&session->s_cap_lock);
886 /* remove from inode list */
887 rb_erase(&cap->ci_node, &ci->i_caps);
888 if (ci->i_auth_cap == cap)
889 ci->i_auth_cap = NULL;
891 if (removed)
892 ceph_put_cap(mdsc, cap);
894 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
895 struct ceph_snap_realm *realm = ci->i_snap_realm;
896 spin_lock(&realm->inodes_with_caps_lock);
897 list_del_init(&ci->i_snap_realm_item);
898 ci->i_snap_realm_counter++;
899 ci->i_snap_realm = NULL;
900 spin_unlock(&realm->inodes_with_caps_lock);
901 ceph_put_snap_realm(mdsc, realm);
903 if (!__ceph_is_any_real_caps(ci))
904 __cap_delay_cancel(mdsc, ci);
908 * Build and send a cap message to the given MDS.
910 * Caller should be holding s_mutex.
912 static int send_cap_msg(struct ceph_mds_session *session,
913 u64 ino, u64 cid, int op,
914 int caps, int wanted, int dirty,
915 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
916 u64 size, u64 max_size,
917 struct timespec *mtime, struct timespec *atime,
918 u64 time_warp_seq,
919 uid_t uid, gid_t gid, mode_t mode,
920 u64 xattr_version,
921 struct ceph_buffer *xattrs_buf,
922 u64 follows)
924 struct ceph_mds_caps *fc;
925 struct ceph_msg *msg;
927 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
928 " seq %u/%u mseq %u follows %lld size %llu/%llu"
929 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
930 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
931 ceph_cap_string(dirty),
932 seq, issue_seq, mseq, follows, size, max_size,
933 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
935 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS);
936 if (!msg)
937 return -ENOMEM;
939 msg->hdr.tid = cpu_to_le64(flush_tid);
941 fc = msg->front.iov_base;
942 memset(fc, 0, sizeof(*fc));
944 fc->cap_id = cpu_to_le64(cid);
945 fc->op = cpu_to_le32(op);
946 fc->seq = cpu_to_le32(seq);
947 fc->issue_seq = cpu_to_le32(issue_seq);
948 fc->migrate_seq = cpu_to_le32(mseq);
949 fc->caps = cpu_to_le32(caps);
950 fc->wanted = cpu_to_le32(wanted);
951 fc->dirty = cpu_to_le32(dirty);
952 fc->ino = cpu_to_le64(ino);
953 fc->snap_follows = cpu_to_le64(follows);
955 fc->size = cpu_to_le64(size);
956 fc->max_size = cpu_to_le64(max_size);
957 if (mtime)
958 ceph_encode_timespec(&fc->mtime, mtime);
959 if (atime)
960 ceph_encode_timespec(&fc->atime, atime);
961 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
963 fc->uid = cpu_to_le32(uid);
964 fc->gid = cpu_to_le32(gid);
965 fc->mode = cpu_to_le32(mode);
967 fc->xattr_version = cpu_to_le64(xattr_version);
968 if (xattrs_buf) {
969 msg->middle = ceph_buffer_get(xattrs_buf);
970 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
971 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
974 ceph_con_send(&session->s_con, msg);
975 return 0;
978 static void __queue_cap_release(struct ceph_mds_session *session,
979 u64 ino, u64 cap_id, u32 migrate_seq,
980 u32 issue_seq)
982 struct ceph_msg *msg;
983 struct ceph_mds_cap_release *head;
984 struct ceph_mds_cap_item *item;
986 spin_lock(&session->s_cap_lock);
987 BUG_ON(!session->s_num_cap_releases);
988 msg = list_first_entry(&session->s_cap_releases,
989 struct ceph_msg, list_head);
991 dout(" adding %llx release to mds%d msg %p (%d left)\n",
992 ino, session->s_mds, msg, session->s_num_cap_releases);
994 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
995 head = msg->front.iov_base;
996 head->num = cpu_to_le32(le32_to_cpu(head->num) + 1);
997 item = msg->front.iov_base + msg->front.iov_len;
998 item->ino = cpu_to_le64(ino);
999 item->cap_id = cpu_to_le64(cap_id);
1000 item->migrate_seq = cpu_to_le32(migrate_seq);
1001 item->seq = cpu_to_le32(issue_seq);
1003 session->s_num_cap_releases--;
1005 msg->front.iov_len += sizeof(*item);
1006 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1007 dout(" release msg %p full\n", msg);
1008 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1009 } else {
1010 dout(" release msg %p at %d/%d (%d)\n", msg,
1011 (int)le32_to_cpu(head->num),
1012 (int)CEPH_CAPS_PER_RELEASE,
1013 (int)msg->front.iov_len);
1015 spin_unlock(&session->s_cap_lock);
1019 * Queue cap releases when an inode is dropped from our cache. Since
1020 * inode is about to be destroyed, there is no need for i_lock.
1022 void ceph_queue_caps_release(struct inode *inode)
1024 struct ceph_inode_info *ci = ceph_inode(inode);
1025 struct rb_node *p;
1027 p = rb_first(&ci->i_caps);
1028 while (p) {
1029 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1030 struct ceph_mds_session *session = cap->session;
1032 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1033 cap->mseq, cap->issue_seq);
1034 p = rb_next(p);
1035 __ceph_remove_cap(cap);
1040 * Send a cap msg on the given inode. Update our caps state, then
1041 * drop i_lock and send the message.
1043 * Make note of max_size reported/requested from mds, revoked caps
1044 * that have now been implemented.
1046 * Make half-hearted attempt ot to invalidate page cache if we are
1047 * dropping RDCACHE. Note that this will leave behind locked pages
1048 * that we'll then need to deal with elsewhere.
1050 * Return non-zero if delayed release, or we experienced an error
1051 * such that the caller should requeue + retry later.
1053 * called with i_lock, then drops it.
1054 * caller should hold snap_rwsem (read), s_mutex.
1056 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1057 int op, int used, int want, int retain, int flushing,
1058 unsigned *pflush_tid)
1059 __releases(cap->ci->vfs_inode->i_lock)
1061 struct ceph_inode_info *ci = cap->ci;
1062 struct inode *inode = &ci->vfs_inode;
1063 u64 cap_id = cap->cap_id;
1064 int held, revoking, dropping, keep;
1065 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1066 u64 size, max_size;
1067 struct timespec mtime, atime;
1068 int wake = 0;
1069 mode_t mode;
1070 uid_t uid;
1071 gid_t gid;
1072 struct ceph_mds_session *session;
1073 u64 xattr_version = 0;
1074 int delayed = 0;
1075 u64 flush_tid = 0;
1076 int i;
1077 int ret;
1079 held = cap->issued | cap->implemented;
1080 revoking = cap->implemented & ~cap->issued;
1081 retain &= ~revoking;
1082 dropping = cap->issued & ~retain;
1084 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1085 inode, cap, cap->session,
1086 ceph_cap_string(held), ceph_cap_string(held & retain),
1087 ceph_cap_string(revoking));
1088 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1090 session = cap->session;
1092 /* don't release wanted unless we've waited a bit. */
1093 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1094 time_before(jiffies, ci->i_hold_caps_min)) {
1095 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1096 ceph_cap_string(cap->issued),
1097 ceph_cap_string(cap->issued & retain),
1098 ceph_cap_string(cap->mds_wanted),
1099 ceph_cap_string(want));
1100 want |= cap->mds_wanted;
1101 retain |= cap->issued;
1102 delayed = 1;
1104 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1106 cap->issued &= retain; /* drop bits we don't want */
1107 if (cap->implemented & ~cap->issued) {
1109 * Wake up any waiters on wanted -> needed transition.
1110 * This is due to the weird transition from buffered
1111 * to sync IO... we need to flush dirty pages _before_
1112 * allowing sync writes to avoid reordering.
1114 wake = 1;
1116 cap->implemented &= cap->issued | used;
1117 cap->mds_wanted = want;
1119 if (flushing) {
1121 * assign a tid for flush operations so we can avoid
1122 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1123 * clean type races. track latest tid for every bit
1124 * so we can handle flush AxFw, flush Fw, and have the
1125 * first ack clean Ax.
1127 flush_tid = ++ci->i_cap_flush_last_tid;
1128 if (pflush_tid)
1129 *pflush_tid = flush_tid;
1130 dout(" cap_flush_tid %d\n", (int)flush_tid);
1131 for (i = 0; i < CEPH_CAP_BITS; i++)
1132 if (flushing & (1 << i))
1133 ci->i_cap_flush_tid[i] = flush_tid;
1136 keep = cap->implemented;
1137 seq = cap->seq;
1138 issue_seq = cap->issue_seq;
1139 mseq = cap->mseq;
1140 size = inode->i_size;
1141 ci->i_reported_size = size;
1142 max_size = ci->i_wanted_max_size;
1143 ci->i_requested_max_size = max_size;
1144 mtime = inode->i_mtime;
1145 atime = inode->i_atime;
1146 time_warp_seq = ci->i_time_warp_seq;
1147 follows = ci->i_snap_realm->cached_context->seq;
1148 uid = inode->i_uid;
1149 gid = inode->i_gid;
1150 mode = inode->i_mode;
1152 if (dropping & CEPH_CAP_XATTR_EXCL) {
1153 __ceph_build_xattrs_blob(ci);
1154 xattr_version = ci->i_xattrs.version + 1;
1157 spin_unlock(&inode->i_lock);
1159 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1160 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1161 size, max_size, &mtime, &atime, time_warp_seq,
1162 uid, gid, mode,
1163 xattr_version,
1164 (flushing & CEPH_CAP_XATTR_EXCL) ? ci->i_xattrs.blob : NULL,
1165 follows);
1166 if (ret < 0) {
1167 dout("error sending cap msg, must requeue %p\n", inode);
1168 delayed = 1;
1171 if (wake)
1172 wake_up_all(&ci->i_cap_wq);
1174 return delayed;
1178 * When a snapshot is taken, clients accumulate dirty metadata on
1179 * inodes with capabilities in ceph_cap_snaps to describe the file
1180 * state at the time the snapshot was taken. This must be flushed
1181 * asynchronously back to the MDS once sync writes complete and dirty
1182 * data is written out.
1184 * Called under i_lock. Takes s_mutex as needed.
1186 void __ceph_flush_snaps(struct ceph_inode_info *ci,
1187 struct ceph_mds_session **psession)
1188 __releases(ci->vfs_inode->i_lock)
1189 __acquires(ci->vfs_inode->i_lock)
1191 struct inode *inode = &ci->vfs_inode;
1192 int mds;
1193 struct ceph_cap_snap *capsnap;
1194 u32 mseq;
1195 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
1196 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1197 session->s_mutex */
1198 u64 next_follows = 0; /* keep track of how far we've gotten through the
1199 i_cap_snaps list, and skip these entries next time
1200 around to avoid an infinite loop */
1202 if (psession)
1203 session = *psession;
1205 dout("__flush_snaps %p\n", inode);
1206 retry:
1207 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1208 /* avoid an infiniute loop after retry */
1209 if (capsnap->follows < next_follows)
1210 continue;
1212 * we need to wait for sync writes to complete and for dirty
1213 * pages to be written out.
1215 if (capsnap->dirty_pages || capsnap->writing)
1216 continue;
1219 * if cap writeback already occurred, we should have dropped
1220 * the capsnap in ceph_put_wrbuffer_cap_refs.
1222 BUG_ON(capsnap->dirty == 0);
1224 /* pick mds, take s_mutex */
1225 if (ci->i_auth_cap == NULL) {
1226 dout("no auth cap (migrating?), doing nothing\n");
1227 goto out;
1229 mds = ci->i_auth_cap->session->s_mds;
1230 mseq = ci->i_auth_cap->mseq;
1232 if (session && session->s_mds != mds) {
1233 dout("oops, wrong session %p mutex\n", session);
1234 mutex_unlock(&session->s_mutex);
1235 ceph_put_mds_session(session);
1236 session = NULL;
1238 if (!session) {
1239 spin_unlock(&inode->i_lock);
1240 mutex_lock(&mdsc->mutex);
1241 session = __ceph_lookup_mds_session(mdsc, mds);
1242 mutex_unlock(&mdsc->mutex);
1243 if (session) {
1244 dout("inverting session/ino locks on %p\n",
1245 session);
1246 mutex_lock(&session->s_mutex);
1249 * if session == NULL, we raced against a cap
1250 * deletion or migration. retry, and we'll
1251 * get a better @mds value next time.
1253 spin_lock(&inode->i_lock);
1254 goto retry;
1257 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1258 atomic_inc(&capsnap->nref);
1259 if (!list_empty(&capsnap->flushing_item))
1260 list_del_init(&capsnap->flushing_item);
1261 list_add_tail(&capsnap->flushing_item,
1262 &session->s_cap_snaps_flushing);
1263 spin_unlock(&inode->i_lock);
1265 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1266 inode, capsnap, next_follows, capsnap->size);
1267 send_cap_msg(session, ceph_vino(inode).ino, 0,
1268 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1269 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1270 capsnap->size, 0,
1271 &capsnap->mtime, &capsnap->atime,
1272 capsnap->time_warp_seq,
1273 capsnap->uid, capsnap->gid, capsnap->mode,
1274 0, NULL,
1275 capsnap->follows);
1277 next_follows = capsnap->follows + 1;
1278 ceph_put_cap_snap(capsnap);
1280 spin_lock(&inode->i_lock);
1281 goto retry;
1284 /* we flushed them all; remove this inode from the queue */
1285 spin_lock(&mdsc->snap_flush_lock);
1286 list_del_init(&ci->i_snap_flush_item);
1287 spin_unlock(&mdsc->snap_flush_lock);
1289 out:
1290 if (psession)
1291 *psession = session;
1292 else if (session) {
1293 mutex_unlock(&session->s_mutex);
1294 ceph_put_mds_session(session);
1298 static void ceph_flush_snaps(struct ceph_inode_info *ci)
1300 struct inode *inode = &ci->vfs_inode;
1302 spin_lock(&inode->i_lock);
1303 __ceph_flush_snaps(ci, NULL);
1304 spin_unlock(&inode->i_lock);
1308 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1309 * list.
1311 void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
1313 struct ceph_mds_client *mdsc =
1314 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1315 struct inode *inode = &ci->vfs_inode;
1316 int was = ci->i_dirty_caps;
1317 int dirty = 0;
1319 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1320 ceph_cap_string(mask), ceph_cap_string(was),
1321 ceph_cap_string(was | mask));
1322 ci->i_dirty_caps |= mask;
1323 if (was == 0) {
1324 dout(" inode %p now dirty\n", &ci->vfs_inode);
1325 BUG_ON(!list_empty(&ci->i_dirty_item));
1326 spin_lock(&mdsc->cap_dirty_lock);
1327 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1328 spin_unlock(&mdsc->cap_dirty_lock);
1329 if (ci->i_flushing_caps == 0) {
1330 igrab(inode);
1331 dirty |= I_DIRTY_SYNC;
1334 BUG_ON(list_empty(&ci->i_dirty_item));
1335 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1336 (mask & CEPH_CAP_FILE_BUFFER))
1337 dirty |= I_DIRTY_DATASYNC;
1338 if (dirty)
1339 __mark_inode_dirty(inode, dirty);
1340 __cap_delay_requeue(mdsc, ci);
1344 * Add dirty inode to the flushing list. Assigned a seq number so we
1345 * can wait for caps to flush without starving.
1347 * Called under i_lock.
1349 static int __mark_caps_flushing(struct inode *inode,
1350 struct ceph_mds_session *session)
1352 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
1353 struct ceph_inode_info *ci = ceph_inode(inode);
1354 int flushing;
1356 BUG_ON(ci->i_dirty_caps == 0);
1357 BUG_ON(list_empty(&ci->i_dirty_item));
1359 flushing = ci->i_dirty_caps;
1360 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1361 ceph_cap_string(flushing),
1362 ceph_cap_string(ci->i_flushing_caps),
1363 ceph_cap_string(ci->i_flushing_caps | flushing));
1364 ci->i_flushing_caps |= flushing;
1365 ci->i_dirty_caps = 0;
1366 dout(" inode %p now !dirty\n", inode);
1368 spin_lock(&mdsc->cap_dirty_lock);
1369 list_del_init(&ci->i_dirty_item);
1371 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
1372 if (list_empty(&ci->i_flushing_item)) {
1373 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1374 mdsc->num_cap_flushing++;
1375 dout(" inode %p now flushing seq %lld\n", inode,
1376 ci->i_cap_flush_seq);
1377 } else {
1378 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1379 dout(" inode %p now flushing (more) seq %lld\n", inode,
1380 ci->i_cap_flush_seq);
1382 spin_unlock(&mdsc->cap_dirty_lock);
1384 return flushing;
1388 * try to invalidate mapping pages without blocking.
1390 static int mapping_is_empty(struct address_space *mapping)
1392 struct page *page = find_get_page(mapping, 0);
1394 if (!page)
1395 return 1;
1397 put_page(page);
1398 return 0;
1401 static int try_nonblocking_invalidate(struct inode *inode)
1403 struct ceph_inode_info *ci = ceph_inode(inode);
1404 u32 invalidating_gen = ci->i_rdcache_gen;
1406 spin_unlock(&inode->i_lock);
1407 invalidate_mapping_pages(&inode->i_data, 0, -1);
1408 spin_lock(&inode->i_lock);
1410 if (mapping_is_empty(&inode->i_data) &&
1411 invalidating_gen == ci->i_rdcache_gen) {
1412 /* success. */
1413 dout("try_nonblocking_invalidate %p success\n", inode);
1414 ci->i_rdcache_gen = 0;
1415 ci->i_rdcache_revoking = 0;
1416 return 0;
1418 dout("try_nonblocking_invalidate %p failed\n", inode);
1419 return -1;
1423 * Swiss army knife function to examine currently used and wanted
1424 * versus held caps. Release, flush, ack revoked caps to mds as
1425 * appropriate.
1427 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1428 * cap release further.
1429 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1430 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1431 * further delay.
1433 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1434 struct ceph_mds_session *session)
1436 struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode);
1437 struct ceph_mds_client *mdsc = &client->mdsc;
1438 struct inode *inode = &ci->vfs_inode;
1439 struct ceph_cap *cap;
1440 int file_wanted, used;
1441 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1442 int issued, implemented, want, retain, revoking, flushing = 0;
1443 int mds = -1; /* keep track of how far we've gone through i_caps list
1444 to avoid an infinite loop on retry */
1445 struct rb_node *p;
1446 int tried_invalidate = 0;
1447 int delayed = 0, sent = 0, force_requeue = 0, num;
1448 int queue_invalidate = 0;
1449 int is_delayed = flags & CHECK_CAPS_NODELAY;
1451 /* if we are unmounting, flush any unused caps immediately. */
1452 if (mdsc->stopping)
1453 is_delayed = 1;
1455 spin_lock(&inode->i_lock);
1457 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1458 flags |= CHECK_CAPS_FLUSH;
1460 /* flush snaps first time around only */
1461 if (!list_empty(&ci->i_cap_snaps))
1462 __ceph_flush_snaps(ci, &session);
1463 goto retry_locked;
1464 retry:
1465 spin_lock(&inode->i_lock);
1466 retry_locked:
1467 file_wanted = __ceph_caps_file_wanted(ci);
1468 used = __ceph_caps_used(ci);
1469 want = file_wanted | used;
1470 issued = __ceph_caps_issued(ci, &implemented);
1471 revoking = implemented & ~issued;
1473 retain = want | CEPH_CAP_PIN;
1474 if (!mdsc->stopping && inode->i_nlink > 0) {
1475 if (want) {
1476 retain |= CEPH_CAP_ANY; /* be greedy */
1477 } else {
1478 retain |= CEPH_CAP_ANY_SHARED;
1480 * keep RD only if we didn't have the file open RW,
1481 * because then the mds would revoke it anyway to
1482 * journal max_size=0.
1484 if (ci->i_max_size == 0)
1485 retain |= CEPH_CAP_ANY_RD;
1489 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1490 " issued %s revoking %s retain %s %s%s%s\n", inode,
1491 ceph_cap_string(file_wanted),
1492 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1493 ceph_cap_string(ci->i_flushing_caps),
1494 ceph_cap_string(issued), ceph_cap_string(revoking),
1495 ceph_cap_string(retain),
1496 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1497 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1498 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1501 * If we no longer need to hold onto old our caps, and we may
1502 * have cached pages, but don't want them, then try to invalidate.
1503 * If we fail, it's because pages are locked.... try again later.
1505 if ((!is_delayed || mdsc->stopping) &&
1506 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
1507 ci->i_rdcache_gen && /* may have cached pages */
1508 (file_wanted == 0 || /* no open files */
1509 (revoking & (CEPH_CAP_FILE_CACHE|
1510 CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
1511 !tried_invalidate) {
1512 dout("check_caps trying to invalidate on %p\n", inode);
1513 if (try_nonblocking_invalidate(inode) < 0) {
1514 if (revoking & (CEPH_CAP_FILE_CACHE|
1515 CEPH_CAP_FILE_LAZYIO)) {
1516 dout("check_caps queuing invalidate\n");
1517 queue_invalidate = 1;
1518 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1519 } else {
1520 dout("check_caps failed to invalidate pages\n");
1521 /* we failed to invalidate pages. check these
1522 caps again later. */
1523 force_requeue = 1;
1524 __cap_set_timeouts(mdsc, ci);
1527 tried_invalidate = 1;
1528 goto retry_locked;
1531 num = 0;
1532 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1533 cap = rb_entry(p, struct ceph_cap, ci_node);
1534 num++;
1536 /* avoid looping forever */
1537 if (mds >= cap->mds ||
1538 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1539 continue;
1541 /* NOTE: no side-effects allowed, until we take s_mutex */
1543 revoking = cap->implemented & ~cap->issued;
1544 if (revoking)
1545 dout(" mds%d revoking %s\n", cap->mds,
1546 ceph_cap_string(revoking));
1548 if (cap == ci->i_auth_cap &&
1549 (cap->issued & CEPH_CAP_FILE_WR)) {
1550 /* request larger max_size from MDS? */
1551 if (ci->i_wanted_max_size > ci->i_max_size &&
1552 ci->i_wanted_max_size > ci->i_requested_max_size) {
1553 dout("requesting new max_size\n");
1554 goto ack;
1557 /* approaching file_max? */
1558 if ((inode->i_size << 1) >= ci->i_max_size &&
1559 (ci->i_reported_size << 1) < ci->i_max_size) {
1560 dout("i_size approaching max_size\n");
1561 goto ack;
1564 /* flush anything dirty? */
1565 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1566 ci->i_dirty_caps) {
1567 dout("flushing dirty caps\n");
1568 goto ack;
1571 /* completed revocation? going down and there are no caps? */
1572 if (revoking && (revoking & used) == 0) {
1573 dout("completed revocation of %s\n",
1574 ceph_cap_string(cap->implemented & ~cap->issued));
1575 goto ack;
1578 /* want more caps from mds? */
1579 if (want & ~(cap->mds_wanted | cap->issued))
1580 goto ack;
1582 /* things we might delay */
1583 if ((cap->issued & ~retain) == 0 &&
1584 cap->mds_wanted == want)
1585 continue; /* nope, all good */
1587 if (is_delayed)
1588 goto ack;
1590 /* delay? */
1591 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1592 time_before(jiffies, ci->i_hold_caps_max)) {
1593 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1594 ceph_cap_string(cap->issued),
1595 ceph_cap_string(cap->issued & retain),
1596 ceph_cap_string(cap->mds_wanted),
1597 ceph_cap_string(want));
1598 delayed++;
1599 continue;
1602 ack:
1603 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1604 dout(" skipping %p I_NOFLUSH set\n", inode);
1605 continue;
1608 if (session && session != cap->session) {
1609 dout("oops, wrong session %p mutex\n", session);
1610 mutex_unlock(&session->s_mutex);
1611 session = NULL;
1613 if (!session) {
1614 session = cap->session;
1615 if (mutex_trylock(&session->s_mutex) == 0) {
1616 dout("inverting session/ino locks on %p\n",
1617 session);
1618 spin_unlock(&inode->i_lock);
1619 if (took_snap_rwsem) {
1620 up_read(&mdsc->snap_rwsem);
1621 took_snap_rwsem = 0;
1623 mutex_lock(&session->s_mutex);
1624 goto retry;
1627 /* take snap_rwsem after session mutex */
1628 if (!took_snap_rwsem) {
1629 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1630 dout("inverting snap/in locks on %p\n",
1631 inode);
1632 spin_unlock(&inode->i_lock);
1633 down_read(&mdsc->snap_rwsem);
1634 took_snap_rwsem = 1;
1635 goto retry;
1637 took_snap_rwsem = 1;
1640 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1641 flushing = __mark_caps_flushing(inode, session);
1643 mds = cap->mds; /* remember mds, so we don't repeat */
1644 sent++;
1646 /* __send_cap drops i_lock */
1647 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
1648 retain, flushing, NULL);
1649 goto retry; /* retake i_lock and restart our cap scan. */
1653 * Reschedule delayed caps release if we delayed anything,
1654 * otherwise cancel.
1656 if (delayed && is_delayed)
1657 force_requeue = 1; /* __send_cap delayed release; requeue */
1658 if (!delayed && !is_delayed)
1659 __cap_delay_cancel(mdsc, ci);
1660 else if (!is_delayed || force_requeue)
1661 __cap_delay_requeue(mdsc, ci);
1663 spin_unlock(&inode->i_lock);
1665 if (queue_invalidate)
1666 ceph_queue_invalidate(inode);
1668 if (session)
1669 mutex_unlock(&session->s_mutex);
1670 if (took_snap_rwsem)
1671 up_read(&mdsc->snap_rwsem);
1675 * Try to flush dirty caps back to the auth mds.
1677 static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1678 unsigned *flush_tid)
1680 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
1681 struct ceph_inode_info *ci = ceph_inode(inode);
1682 int unlock_session = session ? 0 : 1;
1683 int flushing = 0;
1685 retry:
1686 spin_lock(&inode->i_lock);
1687 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1688 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1689 goto out;
1691 if (ci->i_dirty_caps && ci->i_auth_cap) {
1692 struct ceph_cap *cap = ci->i_auth_cap;
1693 int used = __ceph_caps_used(ci);
1694 int want = __ceph_caps_wanted(ci);
1695 int delayed;
1697 if (!session) {
1698 spin_unlock(&inode->i_lock);
1699 session = cap->session;
1700 mutex_lock(&session->s_mutex);
1701 goto retry;
1703 BUG_ON(session != cap->session);
1704 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1705 goto out;
1707 flushing = __mark_caps_flushing(inode, session);
1709 /* __send_cap drops i_lock */
1710 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1711 cap->issued | cap->implemented, flushing,
1712 flush_tid);
1713 if (!delayed)
1714 goto out_unlocked;
1716 spin_lock(&inode->i_lock);
1717 __cap_delay_requeue(mdsc, ci);
1719 out:
1720 spin_unlock(&inode->i_lock);
1721 out_unlocked:
1722 if (session && unlock_session)
1723 mutex_unlock(&session->s_mutex);
1724 return flushing;
1728 * Return true if we've flushed caps through the given flush_tid.
1730 static int caps_are_flushed(struct inode *inode, unsigned tid)
1732 struct ceph_inode_info *ci = ceph_inode(inode);
1733 int i, ret = 1;
1735 spin_lock(&inode->i_lock);
1736 for (i = 0; i < CEPH_CAP_BITS; i++)
1737 if ((ci->i_flushing_caps & (1 << i)) &&
1738 ci->i_cap_flush_tid[i] <= tid) {
1739 /* still flushing this bit */
1740 ret = 0;
1741 break;
1743 spin_unlock(&inode->i_lock);
1744 return ret;
1748 * Wait on any unsafe replies for the given inode. First wait on the
1749 * newest request, and make that the upper bound. Then, if there are
1750 * more requests, keep waiting on the oldest as long as it is still older
1751 * than the original request.
1753 static void sync_write_wait(struct inode *inode)
1755 struct ceph_inode_info *ci = ceph_inode(inode);
1756 struct list_head *head = &ci->i_unsafe_writes;
1757 struct ceph_osd_request *req;
1758 u64 last_tid;
1760 spin_lock(&ci->i_unsafe_lock);
1761 if (list_empty(head))
1762 goto out;
1764 /* set upper bound as _last_ entry in chain */
1765 req = list_entry(head->prev, struct ceph_osd_request,
1766 r_unsafe_item);
1767 last_tid = req->r_tid;
1769 do {
1770 ceph_osdc_get_request(req);
1771 spin_unlock(&ci->i_unsafe_lock);
1772 dout("sync_write_wait on tid %llu (until %llu)\n",
1773 req->r_tid, last_tid);
1774 wait_for_completion(&req->r_safe_completion);
1775 spin_lock(&ci->i_unsafe_lock);
1776 ceph_osdc_put_request(req);
1779 * from here on look at first entry in chain, since we
1780 * only want to wait for anything older than last_tid
1782 if (list_empty(head))
1783 break;
1784 req = list_entry(head->next, struct ceph_osd_request,
1785 r_unsafe_item);
1786 } while (req->r_tid < last_tid);
1787 out:
1788 spin_unlock(&ci->i_unsafe_lock);
1791 int ceph_fsync(struct file *file, int datasync)
1793 struct inode *inode = file->f_mapping->host;
1794 struct ceph_inode_info *ci = ceph_inode(inode);
1795 unsigned flush_tid;
1796 int ret;
1797 int dirty;
1799 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1800 sync_write_wait(inode);
1802 ret = filemap_write_and_wait(inode->i_mapping);
1803 if (ret < 0)
1804 return ret;
1806 dirty = try_flush_caps(inode, NULL, &flush_tid);
1807 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1810 * only wait on non-file metadata writeback (the mds
1811 * can recover size and mtime, so we don't need to
1812 * wait for that)
1814 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1815 dout("fsync waiting for flush_tid %u\n", flush_tid);
1816 ret = wait_event_interruptible(ci->i_cap_wq,
1817 caps_are_flushed(inode, flush_tid));
1820 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
1821 return ret;
1825 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1826 * queue inode for flush but don't do so immediately, because we can
1827 * get by with fewer MDS messages if we wait for data writeback to
1828 * complete first.
1830 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
1832 struct ceph_inode_info *ci = ceph_inode(inode);
1833 unsigned flush_tid;
1834 int err = 0;
1835 int dirty;
1836 int wait = wbc->sync_mode == WB_SYNC_ALL;
1838 dout("write_inode %p wait=%d\n", inode, wait);
1839 if (wait) {
1840 dirty = try_flush_caps(inode, NULL, &flush_tid);
1841 if (dirty)
1842 err = wait_event_interruptible(ci->i_cap_wq,
1843 caps_are_flushed(inode, flush_tid));
1844 } else {
1845 struct ceph_mds_client *mdsc =
1846 &ceph_sb_to_client(inode->i_sb)->mdsc;
1848 spin_lock(&inode->i_lock);
1849 if (__ceph_caps_dirty(ci))
1850 __cap_delay_requeue_front(mdsc, ci);
1851 spin_unlock(&inode->i_lock);
1853 return err;
1857 * After a recovering MDS goes active, we need to resend any caps
1858 * we were flushing.
1860 * Caller holds session->s_mutex.
1862 static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1863 struct ceph_mds_session *session)
1865 struct ceph_cap_snap *capsnap;
1867 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1868 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1869 flushing_item) {
1870 struct ceph_inode_info *ci = capsnap->ci;
1871 struct inode *inode = &ci->vfs_inode;
1872 struct ceph_cap *cap;
1874 spin_lock(&inode->i_lock);
1875 cap = ci->i_auth_cap;
1876 if (cap && cap->session == session) {
1877 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1878 cap, capsnap);
1879 __ceph_flush_snaps(ci, &session);
1880 } else {
1881 pr_err("%p auth cap %p not mds%d ???\n", inode,
1882 cap, session->s_mds);
1884 spin_unlock(&inode->i_lock);
1888 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1889 struct ceph_mds_session *session)
1891 struct ceph_inode_info *ci;
1893 kick_flushing_capsnaps(mdsc, session);
1895 dout("kick_flushing_caps mds%d\n", session->s_mds);
1896 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1897 struct inode *inode = &ci->vfs_inode;
1898 struct ceph_cap *cap;
1899 int delayed = 0;
1901 spin_lock(&inode->i_lock);
1902 cap = ci->i_auth_cap;
1903 if (cap && cap->session == session) {
1904 dout("kick_flushing_caps %p cap %p %s\n", inode,
1905 cap, ceph_cap_string(ci->i_flushing_caps));
1906 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1907 __ceph_caps_used(ci),
1908 __ceph_caps_wanted(ci),
1909 cap->issued | cap->implemented,
1910 ci->i_flushing_caps, NULL);
1911 if (delayed) {
1912 spin_lock(&inode->i_lock);
1913 __cap_delay_requeue(mdsc, ci);
1914 spin_unlock(&inode->i_lock);
1916 } else {
1917 pr_err("%p auth cap %p not mds%d ???\n", inode,
1918 cap, session->s_mds);
1919 spin_unlock(&inode->i_lock);
1926 * Take references to capabilities we hold, so that we don't release
1927 * them to the MDS prematurely.
1929 * Protected by i_lock.
1931 static void __take_cap_refs(struct ceph_inode_info *ci, int got)
1933 if (got & CEPH_CAP_PIN)
1934 ci->i_pin_ref++;
1935 if (got & CEPH_CAP_FILE_RD)
1936 ci->i_rd_ref++;
1937 if (got & CEPH_CAP_FILE_CACHE)
1938 ci->i_rdcache_ref++;
1939 if (got & CEPH_CAP_FILE_WR)
1940 ci->i_wr_ref++;
1941 if (got & CEPH_CAP_FILE_BUFFER) {
1942 if (ci->i_wrbuffer_ref == 0)
1943 igrab(&ci->vfs_inode);
1944 ci->i_wrbuffer_ref++;
1945 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1946 &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref);
1951 * Try to grab cap references. Specify those refs we @want, and the
1952 * minimal set we @need. Also include the larger offset we are writing
1953 * to (when applicable), and check against max_size here as well.
1954 * Note that caller is responsible for ensuring max_size increases are
1955 * requested from the MDS.
1957 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
1958 int *got, loff_t endoff, int *check_max, int *err)
1960 struct inode *inode = &ci->vfs_inode;
1961 int ret = 0;
1962 int have, implemented;
1963 int file_wanted;
1965 dout("get_cap_refs %p need %s want %s\n", inode,
1966 ceph_cap_string(need), ceph_cap_string(want));
1967 spin_lock(&inode->i_lock);
1969 /* make sure file is actually open */
1970 file_wanted = __ceph_caps_file_wanted(ci);
1971 if ((file_wanted & need) == 0) {
1972 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1973 ceph_cap_string(need), ceph_cap_string(file_wanted));
1974 *err = -EBADF;
1975 ret = 1;
1976 goto out;
1979 if (need & CEPH_CAP_FILE_WR) {
1980 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
1981 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1982 inode, endoff, ci->i_max_size);
1983 if (endoff > ci->i_wanted_max_size) {
1984 *check_max = 1;
1985 ret = 1;
1987 goto out;
1990 * If a sync write is in progress, we must wait, so that we
1991 * can get a final snapshot value for size+mtime.
1993 if (__ceph_have_pending_cap_snap(ci)) {
1994 dout("get_cap_refs %p cap_snap_pending\n", inode);
1995 goto out;
1998 have = __ceph_caps_issued(ci, &implemented);
2001 * disallow writes while a truncate is pending
2003 if (ci->i_truncate_pending)
2004 have &= ~CEPH_CAP_FILE_WR;
2006 if ((have & need) == need) {
2008 * Look at (implemented & ~have & not) so that we keep waiting
2009 * on transition from wanted -> needed caps. This is needed
2010 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2011 * going before a prior buffered writeback happens.
2013 int not = want & ~(have & need);
2014 int revoking = implemented & ~have;
2015 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2016 inode, ceph_cap_string(have), ceph_cap_string(not),
2017 ceph_cap_string(revoking));
2018 if ((revoking & not) == 0) {
2019 *got = need | (have & want);
2020 __take_cap_refs(ci, *got);
2021 ret = 1;
2023 } else {
2024 dout("get_cap_refs %p have %s needed %s\n", inode,
2025 ceph_cap_string(have), ceph_cap_string(need));
2027 out:
2028 spin_unlock(&inode->i_lock);
2029 dout("get_cap_refs %p ret %d got %s\n", inode,
2030 ret, ceph_cap_string(*got));
2031 return ret;
2035 * Check the offset we are writing up to against our current
2036 * max_size. If necessary, tell the MDS we want to write to
2037 * a larger offset.
2039 static void check_max_size(struct inode *inode, loff_t endoff)
2041 struct ceph_inode_info *ci = ceph_inode(inode);
2042 int check = 0;
2044 /* do we need to explicitly request a larger max_size? */
2045 spin_lock(&inode->i_lock);
2046 if ((endoff >= ci->i_max_size ||
2047 endoff > (inode->i_size << 1)) &&
2048 endoff > ci->i_wanted_max_size) {
2049 dout("write %p at large endoff %llu, req max_size\n",
2050 inode, endoff);
2051 ci->i_wanted_max_size = endoff;
2052 check = 1;
2054 spin_unlock(&inode->i_lock);
2055 if (check)
2056 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2060 * Wait for caps, and take cap references. If we can't get a WR cap
2061 * due to a small max_size, make sure we check_max_size (and possibly
2062 * ask the mds) so we don't get hung up indefinitely.
2064 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2065 loff_t endoff)
2067 int check_max, ret, err;
2069 retry:
2070 if (endoff > 0)
2071 check_max_size(&ci->vfs_inode, endoff);
2072 check_max = 0;
2073 err = 0;
2074 ret = wait_event_interruptible(ci->i_cap_wq,
2075 try_get_cap_refs(ci, need, want,
2076 got, endoff,
2077 &check_max, &err));
2078 if (err)
2079 ret = err;
2080 if (check_max)
2081 goto retry;
2082 return ret;
2086 * Take cap refs. Caller must already know we hold at least one ref
2087 * on the caps in question or we don't know this is safe.
2089 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2091 spin_lock(&ci->vfs_inode.i_lock);
2092 __take_cap_refs(ci, caps);
2093 spin_unlock(&ci->vfs_inode.i_lock);
2097 * Release cap refs.
2099 * If we released the last ref on any given cap, call ceph_check_caps
2100 * to release (or schedule a release).
2102 * If we are releasing a WR cap (from a sync write), finalize any affected
2103 * cap_snap, and wake up any waiters.
2105 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2107 struct inode *inode = &ci->vfs_inode;
2108 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2109 struct ceph_cap_snap *capsnap;
2111 spin_lock(&inode->i_lock);
2112 if (had & CEPH_CAP_PIN)
2113 --ci->i_pin_ref;
2114 if (had & CEPH_CAP_FILE_RD)
2115 if (--ci->i_rd_ref == 0)
2116 last++;
2117 if (had & CEPH_CAP_FILE_CACHE)
2118 if (--ci->i_rdcache_ref == 0)
2119 last++;
2120 if (had & CEPH_CAP_FILE_BUFFER) {
2121 if (--ci->i_wrbuffer_ref == 0) {
2122 last++;
2123 put++;
2125 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2126 inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref);
2128 if (had & CEPH_CAP_FILE_WR)
2129 if (--ci->i_wr_ref == 0) {
2130 last++;
2131 if (!list_empty(&ci->i_cap_snaps)) {
2132 capsnap = list_first_entry(&ci->i_cap_snaps,
2133 struct ceph_cap_snap,
2134 ci_item);
2135 if (capsnap->writing) {
2136 capsnap->writing = 0;
2137 flushsnaps =
2138 __ceph_finish_cap_snap(ci,
2139 capsnap);
2140 wake = 1;
2144 spin_unlock(&inode->i_lock);
2146 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2147 last ? " last" : "", put ? " put" : "");
2149 if (last && !flushsnaps)
2150 ceph_check_caps(ci, 0, NULL);
2151 else if (flushsnaps)
2152 ceph_flush_snaps(ci);
2153 if (wake)
2154 wake_up_all(&ci->i_cap_wq);
2155 if (put)
2156 iput(inode);
2160 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2161 * context. Adjust per-snap dirty page accounting as appropriate.
2162 * Once all dirty data for a cap_snap is flushed, flush snapped file
2163 * metadata back to the MDS. If we dropped the last ref, call
2164 * ceph_check_caps.
2166 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2167 struct ceph_snap_context *snapc)
2169 struct inode *inode = &ci->vfs_inode;
2170 int last = 0;
2171 int complete_capsnap = 0;
2172 int drop_capsnap = 0;
2173 int found = 0;
2174 struct ceph_cap_snap *capsnap = NULL;
2176 spin_lock(&inode->i_lock);
2177 ci->i_wrbuffer_ref -= nr;
2178 last = !ci->i_wrbuffer_ref;
2180 if (ci->i_head_snapc == snapc) {
2181 ci->i_wrbuffer_ref_head -= nr;
2182 if (!ci->i_wrbuffer_ref_head) {
2183 ceph_put_snap_context(ci->i_head_snapc);
2184 ci->i_head_snapc = NULL;
2186 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2187 inode,
2188 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2189 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2190 last ? " LAST" : "");
2191 } else {
2192 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2193 if (capsnap->context == snapc) {
2194 found = 1;
2195 break;
2198 BUG_ON(!found);
2199 capsnap->dirty_pages -= nr;
2200 if (capsnap->dirty_pages == 0) {
2201 complete_capsnap = 1;
2202 if (capsnap->dirty == 0)
2203 /* cap writeback completed before we created
2204 * the cap_snap; no FLUSHSNAP is needed */
2205 drop_capsnap = 1;
2207 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2208 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2209 inode, capsnap, capsnap->context->seq,
2210 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2211 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2212 last ? " (wrbuffer last)" : "",
2213 complete_capsnap ? " (complete capsnap)" : "",
2214 drop_capsnap ? " (drop capsnap)" : "");
2215 if (drop_capsnap) {
2216 ceph_put_snap_context(capsnap->context);
2217 list_del(&capsnap->ci_item);
2218 list_del(&capsnap->flushing_item);
2219 ceph_put_cap_snap(capsnap);
2223 spin_unlock(&inode->i_lock);
2225 if (last) {
2226 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2227 iput(inode);
2228 } else if (complete_capsnap) {
2229 ceph_flush_snaps(ci);
2230 wake_up_all(&ci->i_cap_wq);
2232 if (drop_capsnap)
2233 iput(inode);
2237 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2238 * actually be a revocation if it specifies a smaller cap set.)
2240 * caller holds s_mutex and i_lock, we drop both.
2242 * return value:
2243 * 0 - ok
2244 * 1 - check_caps on auth cap only (writeback)
2245 * 2 - check_caps (ack revoke)
2247 static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2248 struct ceph_mds_session *session,
2249 struct ceph_cap *cap,
2250 struct ceph_buffer *xattr_buf)
2251 __releases(inode->i_lock)
2253 struct ceph_inode_info *ci = ceph_inode(inode);
2254 int mds = session->s_mds;
2255 int seq = le32_to_cpu(grant->seq);
2256 int newcaps = le32_to_cpu(grant->caps);
2257 int issued, implemented, used, wanted, dirty;
2258 u64 size = le64_to_cpu(grant->size);
2259 u64 max_size = le64_to_cpu(grant->max_size);
2260 struct timespec mtime, atime, ctime;
2261 int check_caps = 0;
2262 int wake = 0;
2263 int writeback = 0;
2264 int revoked_rdcache = 0;
2265 int queue_invalidate = 0;
2267 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2268 inode, cap, mds, seq, ceph_cap_string(newcaps));
2269 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2270 inode->i_size);
2273 * If CACHE is being revoked, and we have no dirty buffers,
2274 * try to invalidate (once). (If there are dirty buffers, we
2275 * will invalidate _after_ writeback.)
2277 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2278 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2279 !ci->i_wrbuffer_ref) {
2280 if (try_nonblocking_invalidate(inode) == 0) {
2281 revoked_rdcache = 1;
2282 } else {
2283 /* there were locked pages.. invalidate later
2284 in a separate thread. */
2285 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2286 queue_invalidate = 1;
2287 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2292 /* side effects now are allowed */
2294 issued = __ceph_caps_issued(ci, &implemented);
2295 issued |= implemented | __ceph_caps_dirty(ci);
2297 cap->cap_gen = session->s_cap_gen;
2299 __check_cap_issue(ci, cap, newcaps);
2301 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2302 inode->i_mode = le32_to_cpu(grant->mode);
2303 inode->i_uid = le32_to_cpu(grant->uid);
2304 inode->i_gid = le32_to_cpu(grant->gid);
2305 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2306 inode->i_uid, inode->i_gid);
2309 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
2310 inode->i_nlink = le32_to_cpu(grant->nlink);
2312 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2313 int len = le32_to_cpu(grant->xattr_len);
2314 u64 version = le64_to_cpu(grant->xattr_version);
2316 if (version > ci->i_xattrs.version) {
2317 dout(" got new xattrs v%llu on %p len %d\n",
2318 version, inode, len);
2319 if (ci->i_xattrs.blob)
2320 ceph_buffer_put(ci->i_xattrs.blob);
2321 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2322 ci->i_xattrs.version = version;
2326 /* size/ctime/mtime/atime? */
2327 ceph_fill_file_size(inode, issued,
2328 le32_to_cpu(grant->truncate_seq),
2329 le64_to_cpu(grant->truncate_size), size);
2330 ceph_decode_timespec(&mtime, &grant->mtime);
2331 ceph_decode_timespec(&atime, &grant->atime);
2332 ceph_decode_timespec(&ctime, &grant->ctime);
2333 ceph_fill_file_time(inode, issued,
2334 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2335 &atime);
2337 /* max size increase? */
2338 if (max_size != ci->i_max_size) {
2339 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2340 ci->i_max_size = max_size;
2341 if (max_size >= ci->i_wanted_max_size) {
2342 ci->i_wanted_max_size = 0; /* reset */
2343 ci->i_requested_max_size = 0;
2345 wake = 1;
2348 /* check cap bits */
2349 wanted = __ceph_caps_wanted(ci);
2350 used = __ceph_caps_used(ci);
2351 dirty = __ceph_caps_dirty(ci);
2352 dout(" my wanted = %s, used = %s, dirty %s\n",
2353 ceph_cap_string(wanted),
2354 ceph_cap_string(used),
2355 ceph_cap_string(dirty));
2356 if (wanted != le32_to_cpu(grant->wanted)) {
2357 dout("mds wanted %s -> %s\n",
2358 ceph_cap_string(le32_to_cpu(grant->wanted)),
2359 ceph_cap_string(wanted));
2360 grant->wanted = cpu_to_le32(wanted);
2363 cap->seq = seq;
2365 /* file layout may have changed */
2366 ci->i_layout = grant->layout;
2368 /* revocation, grant, or no-op? */
2369 if (cap->issued & ~newcaps) {
2370 int revoking = cap->issued & ~newcaps;
2372 dout("revocation: %s -> %s (revoking %s)\n",
2373 ceph_cap_string(cap->issued),
2374 ceph_cap_string(newcaps),
2375 ceph_cap_string(revoking));
2376 if (revoking & CEPH_CAP_FILE_BUFFER)
2377 writeback = 1; /* initiate writeback; will delay ack */
2378 else if (revoking == CEPH_CAP_FILE_CACHE &&
2379 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2380 queue_invalidate)
2381 ; /* do nothing yet, invalidation will be queued */
2382 else if (cap == ci->i_auth_cap)
2383 check_caps = 1; /* check auth cap only */
2384 else
2385 check_caps = 2; /* check all caps */
2386 cap->issued = newcaps;
2387 cap->implemented |= newcaps;
2388 } else if (cap->issued == newcaps) {
2389 dout("caps unchanged: %s -> %s\n",
2390 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2391 } else {
2392 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2393 ceph_cap_string(newcaps));
2394 cap->issued = newcaps;
2395 cap->implemented |= newcaps; /* add bits only, to
2396 * avoid stepping on a
2397 * pending revocation */
2398 wake = 1;
2400 BUG_ON(cap->issued & ~cap->implemented);
2402 spin_unlock(&inode->i_lock);
2403 if (writeback)
2405 * queue inode for writeback: we can't actually call
2406 * filemap_write_and_wait, etc. from message handler
2407 * context.
2409 ceph_queue_writeback(inode);
2410 if (queue_invalidate)
2411 ceph_queue_invalidate(inode);
2412 if (wake)
2413 wake_up_all(&ci->i_cap_wq);
2415 if (check_caps == 1)
2416 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2417 session);
2418 else if (check_caps == 2)
2419 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2420 else
2421 mutex_unlock(&session->s_mutex);
2425 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2426 * MDS has been safely committed.
2428 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
2429 struct ceph_mds_caps *m,
2430 struct ceph_mds_session *session,
2431 struct ceph_cap *cap)
2432 __releases(inode->i_lock)
2434 struct ceph_inode_info *ci = ceph_inode(inode);
2435 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
2436 unsigned seq = le32_to_cpu(m->seq);
2437 int dirty = le32_to_cpu(m->dirty);
2438 int cleaned = 0;
2439 int drop = 0;
2440 int i;
2442 for (i = 0; i < CEPH_CAP_BITS; i++)
2443 if ((dirty & (1 << i)) &&
2444 flush_tid == ci->i_cap_flush_tid[i])
2445 cleaned |= 1 << i;
2447 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2448 " flushing %s -> %s\n",
2449 inode, session->s_mds, seq, ceph_cap_string(dirty),
2450 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2451 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2453 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2454 goto out;
2456 ci->i_flushing_caps &= ~cleaned;
2458 spin_lock(&mdsc->cap_dirty_lock);
2459 if (ci->i_flushing_caps == 0) {
2460 list_del_init(&ci->i_flushing_item);
2461 if (!list_empty(&session->s_cap_flushing))
2462 dout(" mds%d still flushing cap on %p\n",
2463 session->s_mds,
2464 &list_entry(session->s_cap_flushing.next,
2465 struct ceph_inode_info,
2466 i_flushing_item)->vfs_inode);
2467 mdsc->num_cap_flushing--;
2468 wake_up_all(&mdsc->cap_flushing_wq);
2469 dout(" inode %p now !flushing\n", inode);
2471 if (ci->i_dirty_caps == 0) {
2472 dout(" inode %p now clean\n", inode);
2473 BUG_ON(!list_empty(&ci->i_dirty_item));
2474 drop = 1;
2475 } else {
2476 BUG_ON(list_empty(&ci->i_dirty_item));
2479 spin_unlock(&mdsc->cap_dirty_lock);
2480 wake_up_all(&ci->i_cap_wq);
2482 out:
2483 spin_unlock(&inode->i_lock);
2484 if (drop)
2485 iput(inode);
2489 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2490 * throw away our cap_snap.
2492 * Caller hold s_mutex.
2494 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
2495 struct ceph_mds_caps *m,
2496 struct ceph_mds_session *session)
2498 struct ceph_inode_info *ci = ceph_inode(inode);
2499 u64 follows = le64_to_cpu(m->snap_follows);
2500 struct ceph_cap_snap *capsnap;
2501 int drop = 0;
2503 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2504 inode, ci, session->s_mds, follows);
2506 spin_lock(&inode->i_lock);
2507 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2508 if (capsnap->follows == follows) {
2509 if (capsnap->flush_tid != flush_tid) {
2510 dout(" cap_snap %p follows %lld tid %lld !="
2511 " %lld\n", capsnap, follows,
2512 flush_tid, capsnap->flush_tid);
2513 break;
2515 WARN_ON(capsnap->dirty_pages || capsnap->writing);
2516 dout(" removing %p cap_snap %p follows %lld\n",
2517 inode, capsnap, follows);
2518 ceph_put_snap_context(capsnap->context);
2519 list_del(&capsnap->ci_item);
2520 list_del(&capsnap->flushing_item);
2521 ceph_put_cap_snap(capsnap);
2522 drop = 1;
2523 break;
2524 } else {
2525 dout(" skipping cap_snap %p follows %lld\n",
2526 capsnap, capsnap->follows);
2529 spin_unlock(&inode->i_lock);
2530 if (drop)
2531 iput(inode);
2535 * Handle TRUNC from MDS, indicating file truncation.
2537 * caller hold s_mutex.
2539 static void handle_cap_trunc(struct inode *inode,
2540 struct ceph_mds_caps *trunc,
2541 struct ceph_mds_session *session)
2542 __releases(inode->i_lock)
2544 struct ceph_inode_info *ci = ceph_inode(inode);
2545 int mds = session->s_mds;
2546 int seq = le32_to_cpu(trunc->seq);
2547 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2548 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2549 u64 size = le64_to_cpu(trunc->size);
2550 int implemented = 0;
2551 int dirty = __ceph_caps_dirty(ci);
2552 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2553 int queue_trunc = 0;
2555 issued |= implemented | dirty;
2557 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2558 inode, mds, seq, truncate_size, truncate_seq);
2559 queue_trunc = ceph_fill_file_size(inode, issued,
2560 truncate_seq, truncate_size, size);
2561 spin_unlock(&inode->i_lock);
2563 if (queue_trunc)
2564 ceph_queue_vmtruncate(inode);
2568 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2569 * different one. If we are the most recent migration we've seen (as
2570 * indicated by mseq), make note of the migrating cap bits for the
2571 * duration (until we see the corresponding IMPORT).
2573 * caller holds s_mutex
2575 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
2576 struct ceph_mds_session *session,
2577 int *open_target_sessions)
2579 struct ceph_inode_info *ci = ceph_inode(inode);
2580 int mds = session->s_mds;
2581 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2582 struct ceph_cap *cap = NULL, *t;
2583 struct rb_node *p;
2584 int remember = 1;
2586 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2587 inode, ci, mds, mseq);
2589 spin_lock(&inode->i_lock);
2591 /* make sure we haven't seen a higher mseq */
2592 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2593 t = rb_entry(p, struct ceph_cap, ci_node);
2594 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2595 dout(" higher mseq on cap from mds%d\n",
2596 t->session->s_mds);
2597 remember = 0;
2599 if (t->session->s_mds == mds)
2600 cap = t;
2603 if (cap) {
2604 if (remember) {
2605 /* make note */
2606 ci->i_cap_exporting_mds = mds;
2607 ci->i_cap_exporting_mseq = mseq;
2608 ci->i_cap_exporting_issued = cap->issued;
2611 * make sure we have open sessions with all possible
2612 * export targets, so that we get the matching IMPORT
2614 *open_target_sessions = 1;
2616 __ceph_remove_cap(cap);
2618 /* else, we already released it */
2620 spin_unlock(&inode->i_lock);
2624 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2625 * clean them up.
2627 * caller holds s_mutex.
2629 static void handle_cap_import(struct ceph_mds_client *mdsc,
2630 struct inode *inode, struct ceph_mds_caps *im,
2631 struct ceph_mds_session *session,
2632 void *snaptrace, int snaptrace_len)
2634 struct ceph_inode_info *ci = ceph_inode(inode);
2635 int mds = session->s_mds;
2636 unsigned issued = le32_to_cpu(im->caps);
2637 unsigned wanted = le32_to_cpu(im->wanted);
2638 unsigned seq = le32_to_cpu(im->seq);
2639 unsigned mseq = le32_to_cpu(im->migrate_seq);
2640 u64 realmino = le64_to_cpu(im->realm);
2641 u64 cap_id = le64_to_cpu(im->cap_id);
2643 if (ci->i_cap_exporting_mds >= 0 &&
2644 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2645 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2646 " - cleared exporting from mds%d\n",
2647 inode, ci, mds, mseq,
2648 ci->i_cap_exporting_mds);
2649 ci->i_cap_exporting_issued = 0;
2650 ci->i_cap_exporting_mseq = 0;
2651 ci->i_cap_exporting_mds = -1;
2652 } else {
2653 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2654 inode, ci, mds, mseq);
2657 down_write(&mdsc->snap_rwsem);
2658 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2659 false);
2660 downgrade_write(&mdsc->snap_rwsem);
2661 ceph_add_cap(inode, session, cap_id, -1,
2662 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2663 NULL /* no caps context */);
2664 try_flush_caps(inode, session, NULL);
2665 up_read(&mdsc->snap_rwsem);
2669 * Handle a caps message from the MDS.
2671 * Identify the appropriate session, inode, and call the right handler
2672 * based on the cap op.
2674 void ceph_handle_caps(struct ceph_mds_session *session,
2675 struct ceph_msg *msg)
2677 struct ceph_mds_client *mdsc = session->s_mdsc;
2678 struct super_block *sb = mdsc->client->sb;
2679 struct inode *inode;
2680 struct ceph_cap *cap;
2681 struct ceph_mds_caps *h;
2682 int mds = session->s_mds;
2683 int op;
2684 u32 seq, mseq;
2685 struct ceph_vino vino;
2686 u64 cap_id;
2687 u64 size, max_size;
2688 u64 tid;
2689 void *snaptrace;
2690 int open_target_sessions = 0;
2692 dout("handle_caps from mds%d\n", mds);
2694 /* decode */
2695 tid = le64_to_cpu(msg->hdr.tid);
2696 if (msg->front.iov_len < sizeof(*h))
2697 goto bad;
2698 h = msg->front.iov_base;
2699 snaptrace = h + 1;
2700 op = le32_to_cpu(h->op);
2701 vino.ino = le64_to_cpu(h->ino);
2702 vino.snap = CEPH_NOSNAP;
2703 cap_id = le64_to_cpu(h->cap_id);
2704 seq = le32_to_cpu(h->seq);
2705 mseq = le32_to_cpu(h->migrate_seq);
2706 size = le64_to_cpu(h->size);
2707 max_size = le64_to_cpu(h->max_size);
2709 mutex_lock(&session->s_mutex);
2710 session->s_seq++;
2711 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2712 (unsigned)seq);
2714 /* lookup ino */
2715 inode = ceph_find_inode(sb, vino);
2716 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2717 vino.snap, inode);
2718 if (!inode) {
2719 dout(" i don't have ino %llx\n", vino.ino);
2721 if (op == CEPH_CAP_OP_IMPORT)
2722 __queue_cap_release(session, vino.ino, cap_id,
2723 mseq, seq);
2726 * send any full release message to try to move things
2727 * along for the mds (who clearly thinks we still have this
2728 * cap).
2730 ceph_add_cap_releases(mdsc, session);
2731 ceph_send_cap_releases(mdsc, session);
2732 goto done;
2735 /* these will work even if we don't have a cap yet */
2736 switch (op) {
2737 case CEPH_CAP_OP_FLUSHSNAP_ACK:
2738 handle_cap_flushsnap_ack(inode, tid, h, session);
2739 goto done;
2741 case CEPH_CAP_OP_EXPORT:
2742 handle_cap_export(inode, h, session, &open_target_sessions);
2743 goto done;
2745 case CEPH_CAP_OP_IMPORT:
2746 handle_cap_import(mdsc, inode, h, session,
2747 snaptrace, le32_to_cpu(h->snap_trace_len));
2748 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY,
2749 session);
2750 goto done_unlocked;
2753 /* the rest require a cap */
2754 spin_lock(&inode->i_lock);
2755 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2756 if (!cap) {
2757 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2758 inode, ceph_ino(inode), ceph_snap(inode), mds);
2759 spin_unlock(&inode->i_lock);
2760 goto done;
2763 /* note that each of these drops i_lock for us */
2764 switch (op) {
2765 case CEPH_CAP_OP_REVOKE:
2766 case CEPH_CAP_OP_GRANT:
2767 handle_cap_grant(inode, h, session, cap, msg->middle);
2768 goto done_unlocked;
2770 case CEPH_CAP_OP_FLUSH_ACK:
2771 handle_cap_flush_ack(inode, tid, h, session, cap);
2772 break;
2774 case CEPH_CAP_OP_TRUNC:
2775 handle_cap_trunc(inode, h, session);
2776 break;
2778 default:
2779 spin_unlock(&inode->i_lock);
2780 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2781 ceph_cap_op_name(op));
2784 done:
2785 mutex_unlock(&session->s_mutex);
2786 done_unlocked:
2787 if (inode)
2788 iput(inode);
2789 if (open_target_sessions)
2790 ceph_mdsc_open_export_target_sessions(mdsc, session);
2791 return;
2793 bad:
2794 pr_err("ceph_handle_caps: corrupt message\n");
2795 ceph_msg_dump(msg);
2796 return;
2800 * Delayed work handler to process end of delayed cap release LRU list.
2802 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
2804 struct ceph_inode_info *ci;
2805 int flags = CHECK_CAPS_NODELAY;
2807 dout("check_delayed_caps\n");
2808 while (1) {
2809 spin_lock(&mdsc->cap_delay_lock);
2810 if (list_empty(&mdsc->cap_delay_list))
2811 break;
2812 ci = list_first_entry(&mdsc->cap_delay_list,
2813 struct ceph_inode_info,
2814 i_cap_delay_list);
2815 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2816 time_before(jiffies, ci->i_hold_caps_max))
2817 break;
2818 list_del_init(&ci->i_cap_delay_list);
2819 spin_unlock(&mdsc->cap_delay_lock);
2820 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2821 ceph_check_caps(ci, flags, NULL);
2823 spin_unlock(&mdsc->cap_delay_lock);
2827 * Flush all dirty caps to the mds
2829 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2831 struct ceph_inode_info *ci, *nci = NULL;
2832 struct inode *inode, *ninode = NULL;
2833 struct list_head *p, *n;
2835 dout("flush_dirty_caps\n");
2836 spin_lock(&mdsc->cap_dirty_lock);
2837 list_for_each_safe(p, n, &mdsc->cap_dirty) {
2838 if (nci) {
2839 ci = nci;
2840 inode = ninode;
2841 ci->i_ceph_flags &= ~CEPH_I_NOFLUSH;
2842 dout("flush_dirty_caps inode %p (was next inode)\n",
2843 inode);
2844 } else {
2845 ci = list_entry(p, struct ceph_inode_info,
2846 i_dirty_item);
2847 inode = igrab(&ci->vfs_inode);
2848 BUG_ON(!inode);
2849 dout("flush_dirty_caps inode %p\n", inode);
2851 if (n != &mdsc->cap_dirty) {
2852 nci = list_entry(n, struct ceph_inode_info,
2853 i_dirty_item);
2854 ninode = igrab(&nci->vfs_inode);
2855 BUG_ON(!ninode);
2856 nci->i_ceph_flags |= CEPH_I_NOFLUSH;
2857 dout("flush_dirty_caps next inode %p, noflush\n",
2858 ninode);
2859 } else {
2860 nci = NULL;
2861 ninode = NULL;
2863 spin_unlock(&mdsc->cap_dirty_lock);
2864 if (inode) {
2865 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
2866 NULL);
2867 iput(inode);
2869 spin_lock(&mdsc->cap_dirty_lock);
2871 spin_unlock(&mdsc->cap_dirty_lock);
2875 * Drop open file reference. If we were the last open file,
2876 * we may need to release capabilities to the MDS (or schedule
2877 * their delayed release).
2879 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2881 struct inode *inode = &ci->vfs_inode;
2882 int last = 0;
2884 spin_lock(&inode->i_lock);
2885 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2886 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2887 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2888 if (--ci->i_nr_by_mode[fmode] == 0)
2889 last++;
2890 spin_unlock(&inode->i_lock);
2892 if (last && ci->i_vino.snap == CEPH_NOSNAP)
2893 ceph_check_caps(ci, 0, NULL);
2897 * Helpers for embedding cap and dentry lease releases into mds
2898 * requests.
2900 * @force is used by dentry_release (below) to force inclusion of a
2901 * record for the directory inode, even when there aren't any caps to
2902 * drop.
2904 int ceph_encode_inode_release(void **p, struct inode *inode,
2905 int mds, int drop, int unless, int force)
2907 struct ceph_inode_info *ci = ceph_inode(inode);
2908 struct ceph_cap *cap;
2909 struct ceph_mds_request_release *rel = *p;
2910 int used, dirty;
2911 int ret = 0;
2913 spin_lock(&inode->i_lock);
2914 used = __ceph_caps_used(ci);
2915 dirty = __ceph_caps_dirty(ci);
2917 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
2918 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
2919 ceph_cap_string(unless));
2921 /* only drop unused, clean caps */
2922 drop &= ~(used | dirty);
2924 cap = __get_cap_for_mds(ci, mds);
2925 if (cap && __cap_is_valid(cap)) {
2926 if (force ||
2927 ((cap->issued & drop) &&
2928 (cap->issued & unless) == 0)) {
2929 if ((cap->issued & drop) &&
2930 (cap->issued & unless) == 0) {
2931 dout("encode_inode_release %p cap %p %s -> "
2932 "%s\n", inode, cap,
2933 ceph_cap_string(cap->issued),
2934 ceph_cap_string(cap->issued & ~drop));
2935 cap->issued &= ~drop;
2936 cap->implemented &= ~drop;
2937 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
2938 int wanted = __ceph_caps_wanted(ci);
2939 dout(" wanted %s -> %s (act %s)\n",
2940 ceph_cap_string(cap->mds_wanted),
2941 ceph_cap_string(cap->mds_wanted &
2942 ~wanted),
2943 ceph_cap_string(wanted));
2944 cap->mds_wanted &= wanted;
2946 } else {
2947 dout("encode_inode_release %p cap %p %s"
2948 " (force)\n", inode, cap,
2949 ceph_cap_string(cap->issued));
2952 rel->ino = cpu_to_le64(ceph_ino(inode));
2953 rel->cap_id = cpu_to_le64(cap->cap_id);
2954 rel->seq = cpu_to_le32(cap->seq);
2955 rel->issue_seq = cpu_to_le32(cap->issue_seq),
2956 rel->mseq = cpu_to_le32(cap->mseq);
2957 rel->caps = cpu_to_le32(cap->issued);
2958 rel->wanted = cpu_to_le32(cap->mds_wanted);
2959 rel->dname_len = 0;
2960 rel->dname_seq = 0;
2961 *p += sizeof(*rel);
2962 ret = 1;
2963 } else {
2964 dout("encode_inode_release %p cap %p %s\n",
2965 inode, cap, ceph_cap_string(cap->issued));
2968 spin_unlock(&inode->i_lock);
2969 return ret;
2972 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
2973 int mds, int drop, int unless)
2975 struct inode *dir = dentry->d_parent->d_inode;
2976 struct ceph_mds_request_release *rel = *p;
2977 struct ceph_dentry_info *di = ceph_dentry(dentry);
2978 int force = 0;
2979 int ret;
2982 * force an record for the directory caps if we have a dentry lease.
2983 * this is racy (can't take i_lock and d_lock together), but it
2984 * doesn't have to be perfect; the mds will revoke anything we don't
2985 * release.
2987 spin_lock(&dentry->d_lock);
2988 if (di->lease_session && di->lease_session->s_mds == mds)
2989 force = 1;
2990 spin_unlock(&dentry->d_lock);
2992 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
2994 spin_lock(&dentry->d_lock);
2995 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
2996 dout("encode_dentry_release %p mds%d seq %d\n",
2997 dentry, mds, (int)di->lease_seq);
2998 rel->dname_len = cpu_to_le32(dentry->d_name.len);
2999 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3000 *p += dentry->d_name.len;
3001 rel->dname_seq = cpu_to_le32(di->lease_seq);
3002 __ceph_mdsc_drop_dentry_lease(dentry);
3004 spin_unlock(&dentry->d_lock);
3005 return ret;