ceph: code cleanup
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ceph / caps.c
blobd992880d21d4264288ac411c7ff38e14e322950a
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];
117 * Cap reservations
119 * Maintain a global pool of preallocated struct ceph_caps, referenced
120 * by struct ceph_caps_reservations. This ensures that we preallocate
121 * memory needed to successfully process an MDS response. (If an MDS
122 * sends us cap information and we fail to process it, we will have
123 * problems due to the client and MDS being out of sync.)
125 * Reservations are 'owned' by a ceph_cap_reservation context.
127 static spinlock_t caps_list_lock;
128 static struct list_head caps_list; /* unused (reserved or unreserved) */
129 static int caps_total_count; /* total caps allocated */
130 static int caps_use_count; /* in use */
131 static int caps_reserve_count; /* unused, reserved */
132 static int caps_avail_count; /* unused, unreserved */
133 static int caps_min_count; /* keep at least this many (unreserved) */
135 void __init ceph_caps_init(void)
137 INIT_LIST_HEAD(&caps_list);
138 spin_lock_init(&caps_list_lock);
141 void ceph_caps_finalize(void)
143 struct ceph_cap *cap;
145 spin_lock(&caps_list_lock);
146 while (!list_empty(&caps_list)) {
147 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item);
148 list_del(&cap->caps_item);
149 kmem_cache_free(ceph_cap_cachep, cap);
151 caps_total_count = 0;
152 caps_avail_count = 0;
153 caps_use_count = 0;
154 caps_reserve_count = 0;
155 caps_min_count = 0;
156 spin_unlock(&caps_list_lock);
159 void ceph_adjust_min_caps(int delta)
161 spin_lock(&caps_list_lock);
162 caps_min_count += delta;
163 BUG_ON(caps_min_count < 0);
164 spin_unlock(&caps_list_lock);
167 int ceph_reserve_caps(struct ceph_cap_reservation *ctx, int need)
169 int i;
170 struct ceph_cap *cap;
171 int have;
172 int alloc = 0;
173 LIST_HEAD(newcaps);
174 int ret = 0;
176 dout("reserve caps ctx=%p need=%d\n", ctx, need);
178 /* first reserve any caps that are already allocated */
179 spin_lock(&caps_list_lock);
180 if (caps_avail_count >= need)
181 have = need;
182 else
183 have = caps_avail_count;
184 caps_avail_count -= have;
185 caps_reserve_count += have;
186 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
187 caps_avail_count);
188 spin_unlock(&caps_list_lock);
190 for (i = have; i < need; i++) {
191 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
192 if (!cap) {
193 ret = -ENOMEM;
194 goto out_alloc_count;
196 list_add(&cap->caps_item, &newcaps);
197 alloc++;
199 BUG_ON(have + alloc != need);
201 spin_lock(&caps_list_lock);
202 caps_total_count += alloc;
203 caps_reserve_count += alloc;
204 list_splice(&newcaps, &caps_list);
206 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
207 caps_avail_count);
208 spin_unlock(&caps_list_lock);
210 ctx->count = need;
211 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
212 ctx, caps_total_count, caps_use_count, caps_reserve_count,
213 caps_avail_count);
214 return 0;
216 out_alloc_count:
217 /* we didn't manage to reserve as much as we needed */
218 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
219 ctx, need, have);
220 return ret;
223 int ceph_unreserve_caps(struct ceph_cap_reservation *ctx)
225 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
226 if (ctx->count) {
227 spin_lock(&caps_list_lock);
228 BUG_ON(caps_reserve_count < ctx->count);
229 caps_reserve_count -= ctx->count;
230 caps_avail_count += ctx->count;
231 ctx->count = 0;
232 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
233 caps_total_count, caps_use_count, caps_reserve_count,
234 caps_avail_count);
235 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
236 caps_avail_count);
237 spin_unlock(&caps_list_lock);
239 return 0;
242 static struct ceph_cap *get_cap(struct ceph_cap_reservation *ctx)
244 struct ceph_cap *cap = NULL;
246 /* temporary, until we do something about cap import/export */
247 if (!ctx) {
248 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
249 if (cap) {
250 caps_use_count++;
251 caps_total_count++;
253 return cap;
256 spin_lock(&caps_list_lock);
257 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
258 ctx, ctx->count, caps_total_count, caps_use_count,
259 caps_reserve_count, caps_avail_count);
260 BUG_ON(!ctx->count);
261 BUG_ON(ctx->count > caps_reserve_count);
262 BUG_ON(list_empty(&caps_list));
264 ctx->count--;
265 caps_reserve_count--;
266 caps_use_count++;
268 cap = list_first_entry(&caps_list, struct ceph_cap, caps_item);
269 list_del(&cap->caps_item);
271 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
272 caps_avail_count);
273 spin_unlock(&caps_list_lock);
274 return cap;
277 void ceph_put_cap(struct ceph_cap *cap)
279 spin_lock(&caps_list_lock);
280 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
281 cap, caps_total_count, caps_use_count,
282 caps_reserve_count, caps_avail_count);
283 caps_use_count--;
285 * Keep some preallocated caps around (ceph_min_count), to
286 * avoid lots of free/alloc churn.
288 if (caps_avail_count >= caps_reserve_count + caps_min_count) {
289 caps_total_count--;
290 kmem_cache_free(ceph_cap_cachep, cap);
291 } else {
292 caps_avail_count++;
293 list_add(&cap->caps_item, &caps_list);
296 BUG_ON(caps_total_count != caps_use_count + caps_reserve_count +
297 caps_avail_count);
298 spin_unlock(&caps_list_lock);
301 void ceph_reservation_status(struct ceph_client *client,
302 int *total, int *avail, int *used, int *reserved,
303 int *min)
305 if (total)
306 *total = caps_total_count;
307 if (avail)
308 *avail = caps_avail_count;
309 if (used)
310 *used = caps_use_count;
311 if (reserved)
312 *reserved = caps_reserve_count;
313 if (min)
314 *min = caps_min_count;
318 * Find ceph_cap for given mds, if any.
320 * Called with i_lock held.
322 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
324 struct ceph_cap *cap;
325 struct rb_node *n = ci->i_caps.rb_node;
327 while (n) {
328 cap = rb_entry(n, struct ceph_cap, ci_node);
329 if (mds < cap->mds)
330 n = n->rb_left;
331 else if (mds > cap->mds)
332 n = n->rb_right;
333 else
334 return cap;
336 return NULL;
340 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
342 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
344 struct ceph_cap *cap;
345 int mds = -1;
346 struct rb_node *p;
348 /* prefer mds with WR|BUFFER|EXCL caps */
349 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
350 cap = rb_entry(p, struct ceph_cap, ci_node);
351 mds = cap->mds;
352 if (cap->issued & (CEPH_CAP_FILE_WR |
353 CEPH_CAP_FILE_BUFFER |
354 CEPH_CAP_FILE_EXCL))
355 break;
357 return mds;
360 int ceph_get_cap_mds(struct inode *inode)
362 int mds;
363 spin_lock(&inode->i_lock);
364 mds = __ceph_get_cap_mds(ceph_inode(inode));
365 spin_unlock(&inode->i_lock);
366 return mds;
370 * Called under i_lock.
372 static void __insert_cap_node(struct ceph_inode_info *ci,
373 struct ceph_cap *new)
375 struct rb_node **p = &ci->i_caps.rb_node;
376 struct rb_node *parent = NULL;
377 struct ceph_cap *cap = NULL;
379 while (*p) {
380 parent = *p;
381 cap = rb_entry(parent, struct ceph_cap, ci_node);
382 if (new->mds < cap->mds)
383 p = &(*p)->rb_left;
384 else if (new->mds > cap->mds)
385 p = &(*p)->rb_right;
386 else
387 BUG();
390 rb_link_node(&new->ci_node, parent, p);
391 rb_insert_color(&new->ci_node, &ci->i_caps);
395 * (re)set cap hold timeouts, which control the delayed release
396 * of unused caps back to the MDS. Should be called on cap use.
398 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
399 struct ceph_inode_info *ci)
401 struct ceph_mount_args *ma = mdsc->client->mount_args;
403 ci->i_hold_caps_min = round_jiffies(jiffies +
404 ma->caps_wanted_delay_min * HZ);
405 ci->i_hold_caps_max = round_jiffies(jiffies +
406 ma->caps_wanted_delay_max * HZ);
407 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
408 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
412 * (Re)queue cap at the end of the delayed cap release list.
414 * If I_FLUSH is set, leave the inode at the front of the list.
416 * Caller holds i_lock
417 * -> we take mdsc->cap_delay_lock
419 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
420 struct ceph_inode_info *ci)
422 __cap_set_timeouts(mdsc, ci);
423 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
424 ci->i_ceph_flags, ci->i_hold_caps_max);
425 if (!mdsc->stopping) {
426 spin_lock(&mdsc->cap_delay_lock);
427 if (!list_empty(&ci->i_cap_delay_list)) {
428 if (ci->i_ceph_flags & CEPH_I_FLUSH)
429 goto no_change;
430 list_del_init(&ci->i_cap_delay_list);
432 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
433 no_change:
434 spin_unlock(&mdsc->cap_delay_lock);
439 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
440 * indicating we should send a cap message to flush dirty metadata
441 * asap, and move to the front of the delayed cap list.
443 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
444 struct ceph_inode_info *ci)
446 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
447 spin_lock(&mdsc->cap_delay_lock);
448 ci->i_ceph_flags |= CEPH_I_FLUSH;
449 if (!list_empty(&ci->i_cap_delay_list))
450 list_del_init(&ci->i_cap_delay_list);
451 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
452 spin_unlock(&mdsc->cap_delay_lock);
456 * Cancel delayed work on cap.
458 * Caller must hold i_lock.
460 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
461 struct ceph_inode_info *ci)
463 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
464 if (list_empty(&ci->i_cap_delay_list))
465 return;
466 spin_lock(&mdsc->cap_delay_lock);
467 list_del_init(&ci->i_cap_delay_list);
468 spin_unlock(&mdsc->cap_delay_lock);
472 * Common issue checks for add_cap, handle_cap_grant.
474 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
475 unsigned issued)
477 unsigned had = __ceph_caps_issued(ci, NULL);
480 * Each time we receive FILE_CACHE anew, we increment
481 * i_rdcache_gen.
483 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
484 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
485 ci->i_rdcache_gen++;
488 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
489 * don't know what happened to this directory while we didn't
490 * have the cap.
492 if ((issued & CEPH_CAP_FILE_SHARED) &&
493 (had & CEPH_CAP_FILE_SHARED) == 0) {
494 ci->i_shared_gen++;
495 if (S_ISDIR(ci->vfs_inode.i_mode)) {
496 dout(" marking %p NOT complete\n", &ci->vfs_inode);
497 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
503 * Add a capability under the given MDS session.
505 * Caller should hold session snap_rwsem (read) and s_mutex.
507 * @fmode is the open file mode, if we are opening a file, otherwise
508 * it is < 0. (This is so we can atomically add the cap and add an
509 * open file reference to it.)
511 int ceph_add_cap(struct inode *inode,
512 struct ceph_mds_session *session, u64 cap_id,
513 int fmode, unsigned issued, unsigned wanted,
514 unsigned seq, unsigned mseq, u64 realmino, int flags,
515 struct ceph_cap_reservation *caps_reservation)
517 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
518 struct ceph_inode_info *ci = ceph_inode(inode);
519 struct ceph_cap *new_cap = NULL;
520 struct ceph_cap *cap;
521 int mds = session->s_mds;
522 int actual_wanted;
524 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
525 session->s_mds, cap_id, ceph_cap_string(issued), seq);
528 * If we are opening the file, include file mode wanted bits
529 * in wanted.
531 if (fmode >= 0)
532 wanted |= ceph_caps_for_mode(fmode);
534 retry:
535 spin_lock(&inode->i_lock);
536 cap = __get_cap_for_mds(ci, mds);
537 if (!cap) {
538 if (new_cap) {
539 cap = new_cap;
540 new_cap = NULL;
541 } else {
542 spin_unlock(&inode->i_lock);
543 new_cap = get_cap(caps_reservation);
544 if (new_cap == NULL)
545 return -ENOMEM;
546 goto retry;
549 cap->issued = 0;
550 cap->implemented = 0;
551 cap->mds = mds;
552 cap->mds_wanted = 0;
554 cap->ci = ci;
555 __insert_cap_node(ci, cap);
557 /* clear out old exporting info? (i.e. on cap import) */
558 if (ci->i_cap_exporting_mds == mds) {
559 ci->i_cap_exporting_issued = 0;
560 ci->i_cap_exporting_mseq = 0;
561 ci->i_cap_exporting_mds = -1;
564 /* add to session cap list */
565 cap->session = session;
566 spin_lock(&session->s_cap_lock);
567 list_add_tail(&cap->session_caps, &session->s_caps);
568 session->s_nr_caps++;
569 spin_unlock(&session->s_cap_lock);
572 if (!ci->i_snap_realm) {
574 * add this inode to the appropriate snap realm
576 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
577 realmino);
578 if (realm) {
579 ceph_get_snap_realm(mdsc, realm);
580 spin_lock(&realm->inodes_with_caps_lock);
581 ci->i_snap_realm = realm;
582 list_add(&ci->i_snap_realm_item,
583 &realm->inodes_with_caps);
584 spin_unlock(&realm->inodes_with_caps_lock);
585 } else {
586 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
587 realmino);
591 __check_cap_issue(ci, cap, issued);
594 * If we are issued caps we don't want, or the mds' wanted
595 * value appears to be off, queue a check so we'll release
596 * later and/or update the mds wanted value.
598 actual_wanted = __ceph_caps_wanted(ci);
599 if ((wanted & ~actual_wanted) ||
600 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
601 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
602 ceph_cap_string(issued), ceph_cap_string(wanted),
603 ceph_cap_string(actual_wanted));
604 __cap_delay_requeue(mdsc, ci);
607 if (flags & CEPH_CAP_FLAG_AUTH)
608 ci->i_auth_cap = cap;
609 else if (ci->i_auth_cap == cap)
610 ci->i_auth_cap = NULL;
612 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
613 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
614 ceph_cap_string(issued|cap->issued), seq, mds);
615 cap->cap_id = cap_id;
616 cap->issued = issued;
617 cap->implemented |= issued;
618 cap->mds_wanted |= wanted;
619 cap->seq = seq;
620 cap->issue_seq = seq;
621 cap->mseq = mseq;
622 cap->cap_gen = session->s_cap_gen;
624 if (fmode >= 0)
625 __ceph_get_fmode(ci, fmode);
626 spin_unlock(&inode->i_lock);
627 wake_up_all(&ci->i_cap_wq);
628 return 0;
632 * Return true if cap has not timed out and belongs to the current
633 * generation of the MDS session (i.e. has not gone 'stale' due to
634 * us losing touch with the mds).
636 static int __cap_is_valid(struct ceph_cap *cap)
638 unsigned long ttl;
639 u32 gen;
641 spin_lock(&cap->session->s_cap_lock);
642 gen = cap->session->s_cap_gen;
643 ttl = cap->session->s_cap_ttl;
644 spin_unlock(&cap->session->s_cap_lock);
646 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
647 dout("__cap_is_valid %p cap %p issued %s "
648 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
649 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
650 return 0;
653 return 1;
657 * Return set of valid cap bits issued to us. Note that caps time
658 * out, and may be invalidated in bulk if the client session times out
659 * and session->s_cap_gen is bumped.
661 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
663 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
664 struct ceph_cap *cap;
665 struct rb_node *p;
667 if (implemented)
668 *implemented = 0;
669 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
670 cap = rb_entry(p, struct ceph_cap, ci_node);
671 if (!__cap_is_valid(cap))
672 continue;
673 dout("__ceph_caps_issued %p cap %p issued %s\n",
674 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
675 have |= cap->issued;
676 if (implemented)
677 *implemented |= cap->implemented;
679 return have;
683 * Get cap bits issued by caps other than @ocap
685 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
687 int have = ci->i_snap_caps;
688 struct ceph_cap *cap;
689 struct rb_node *p;
691 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
692 cap = rb_entry(p, struct ceph_cap, ci_node);
693 if (cap == ocap)
694 continue;
695 if (!__cap_is_valid(cap))
696 continue;
697 have |= cap->issued;
699 return have;
703 * Move a cap to the end of the LRU (oldest caps at list head, newest
704 * at list tail).
706 static void __touch_cap(struct ceph_cap *cap)
708 struct ceph_mds_session *s = cap->session;
710 spin_lock(&s->s_cap_lock);
711 if (s->s_cap_iterator == NULL) {
712 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
713 s->s_mds);
714 list_move_tail(&cap->session_caps, &s->s_caps);
715 } else {
716 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
717 &cap->ci->vfs_inode, cap, s->s_mds);
719 spin_unlock(&s->s_cap_lock);
723 * Check if we hold the given mask. If so, move the cap(s) to the
724 * front of their respective LRUs. (This is the preferred way for
725 * callers to check for caps they want.)
727 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
729 struct ceph_cap *cap;
730 struct rb_node *p;
731 int have = ci->i_snap_caps;
733 if ((have & mask) == mask) {
734 dout("__ceph_caps_issued_mask %p snap issued %s"
735 " (mask %s)\n", &ci->vfs_inode,
736 ceph_cap_string(have),
737 ceph_cap_string(mask));
738 return 1;
741 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
742 cap = rb_entry(p, struct ceph_cap, ci_node);
743 if (!__cap_is_valid(cap))
744 continue;
745 if ((cap->issued & mask) == mask) {
746 dout("__ceph_caps_issued_mask %p cap %p issued %s"
747 " (mask %s)\n", &ci->vfs_inode, cap,
748 ceph_cap_string(cap->issued),
749 ceph_cap_string(mask));
750 if (touch)
751 __touch_cap(cap);
752 return 1;
755 /* does a combination of caps satisfy mask? */
756 have |= cap->issued;
757 if ((have & mask) == mask) {
758 dout("__ceph_caps_issued_mask %p combo issued %s"
759 " (mask %s)\n", &ci->vfs_inode,
760 ceph_cap_string(cap->issued),
761 ceph_cap_string(mask));
762 if (touch) {
763 struct rb_node *q;
765 /* touch this + preceeding caps */
766 __touch_cap(cap);
767 for (q = rb_first(&ci->i_caps); q != p;
768 q = rb_next(q)) {
769 cap = rb_entry(q, struct ceph_cap,
770 ci_node);
771 if (!__cap_is_valid(cap))
772 continue;
773 __touch_cap(cap);
776 return 1;
780 return 0;
784 * Return true if mask caps are currently being revoked by an MDS.
786 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
788 struct inode *inode = &ci->vfs_inode;
789 struct ceph_cap *cap;
790 struct rb_node *p;
791 int ret = 0;
793 spin_lock(&inode->i_lock);
794 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
795 cap = rb_entry(p, struct ceph_cap, ci_node);
796 if (__cap_is_valid(cap) &&
797 (cap->implemented & ~cap->issued & mask)) {
798 ret = 1;
799 break;
802 spin_unlock(&inode->i_lock);
803 dout("ceph_caps_revoking %p %s = %d\n", inode,
804 ceph_cap_string(mask), ret);
805 return ret;
808 int __ceph_caps_used(struct ceph_inode_info *ci)
810 int used = 0;
811 if (ci->i_pin_ref)
812 used |= CEPH_CAP_PIN;
813 if (ci->i_rd_ref)
814 used |= CEPH_CAP_FILE_RD;
815 if (ci->i_rdcache_ref || ci->i_rdcache_gen)
816 used |= CEPH_CAP_FILE_CACHE;
817 if (ci->i_wr_ref)
818 used |= CEPH_CAP_FILE_WR;
819 if (ci->i_wrbuffer_ref)
820 used |= CEPH_CAP_FILE_BUFFER;
821 return used;
825 * wanted, by virtue of open file modes
827 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
829 int want = 0;
830 int mode;
831 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
832 if (ci->i_nr_by_mode[mode])
833 want |= ceph_caps_for_mode(mode);
834 return want;
838 * Return caps we have registered with the MDS(s) as 'wanted'.
840 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
842 struct ceph_cap *cap;
843 struct rb_node *p;
844 int mds_wanted = 0;
846 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
847 cap = rb_entry(p, struct ceph_cap, ci_node);
848 if (!__cap_is_valid(cap))
849 continue;
850 mds_wanted |= cap->mds_wanted;
852 return mds_wanted;
856 * called under i_lock
858 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
860 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
864 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
866 * caller should hold i_lock.
867 * caller will not hold session s_mutex if called from destroy_inode.
869 void __ceph_remove_cap(struct ceph_cap *cap)
871 struct ceph_mds_session *session = cap->session;
872 struct ceph_inode_info *ci = cap->ci;
873 struct ceph_mds_client *mdsc =
874 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
875 int removed = 0;
877 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
879 /* remove from session list */
880 spin_lock(&session->s_cap_lock);
881 if (session->s_cap_iterator == cap) {
882 /* not yet, we are iterating over this very cap */
883 dout("__ceph_remove_cap delaying %p removal from session %p\n",
884 cap, cap->session);
885 } else {
886 list_del_init(&cap->session_caps);
887 session->s_nr_caps--;
888 cap->session = NULL;
889 removed = 1;
891 /* protect backpointer with s_cap_lock: see iterate_session_caps */
892 cap->ci = NULL;
893 spin_unlock(&session->s_cap_lock);
895 /* remove from inode list */
896 rb_erase(&cap->ci_node, &ci->i_caps);
897 if (ci->i_auth_cap == cap)
898 ci->i_auth_cap = NULL;
900 if (removed)
901 ceph_put_cap(cap);
903 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
904 struct ceph_snap_realm *realm = ci->i_snap_realm;
905 spin_lock(&realm->inodes_with_caps_lock);
906 list_del_init(&ci->i_snap_realm_item);
907 ci->i_snap_realm_counter++;
908 ci->i_snap_realm = NULL;
909 spin_unlock(&realm->inodes_with_caps_lock);
910 ceph_put_snap_realm(mdsc, realm);
912 if (!__ceph_is_any_real_caps(ci))
913 __cap_delay_cancel(mdsc, ci);
917 * Build and send a cap message to the given MDS.
919 * Caller should be holding s_mutex.
921 static int send_cap_msg(struct ceph_mds_session *session,
922 u64 ino, u64 cid, int op,
923 int caps, int wanted, int dirty,
924 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
925 u64 size, u64 max_size,
926 struct timespec *mtime, struct timespec *atime,
927 u64 time_warp_seq,
928 uid_t uid, gid_t gid, mode_t mode,
929 u64 xattr_version,
930 struct ceph_buffer *xattrs_buf,
931 u64 follows)
933 struct ceph_mds_caps *fc;
934 struct ceph_msg *msg;
936 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
937 " seq %u/%u mseq %u follows %lld size %llu/%llu"
938 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
939 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
940 ceph_cap_string(dirty),
941 seq, issue_seq, mseq, follows, size, max_size,
942 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
944 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS);
945 if (!msg)
946 return -ENOMEM;
948 msg->hdr.tid = cpu_to_le64(flush_tid);
950 fc = msg->front.iov_base;
951 memset(fc, 0, sizeof(*fc));
953 fc->cap_id = cpu_to_le64(cid);
954 fc->op = cpu_to_le32(op);
955 fc->seq = cpu_to_le32(seq);
956 fc->issue_seq = cpu_to_le32(issue_seq);
957 fc->migrate_seq = cpu_to_le32(mseq);
958 fc->caps = cpu_to_le32(caps);
959 fc->wanted = cpu_to_le32(wanted);
960 fc->dirty = cpu_to_le32(dirty);
961 fc->ino = cpu_to_le64(ino);
962 fc->snap_follows = cpu_to_le64(follows);
964 fc->size = cpu_to_le64(size);
965 fc->max_size = cpu_to_le64(max_size);
966 if (mtime)
967 ceph_encode_timespec(&fc->mtime, mtime);
968 if (atime)
969 ceph_encode_timespec(&fc->atime, atime);
970 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
972 fc->uid = cpu_to_le32(uid);
973 fc->gid = cpu_to_le32(gid);
974 fc->mode = cpu_to_le32(mode);
976 fc->xattr_version = cpu_to_le64(xattr_version);
977 if (xattrs_buf) {
978 msg->middle = ceph_buffer_get(xattrs_buf);
979 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
980 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
983 ceph_con_send(&session->s_con, msg);
984 return 0;
987 static void __queue_cap_release(struct ceph_mds_session *session,
988 u64 ino, u64 cap_id, u32 migrate_seq,
989 u32 issue_seq)
991 struct ceph_msg *msg;
992 struct ceph_mds_cap_release *head;
993 struct ceph_mds_cap_item *item;
995 spin_lock(&session->s_cap_lock);
996 BUG_ON(!session->s_num_cap_releases);
997 msg = list_first_entry(&session->s_cap_releases,
998 struct ceph_msg, list_head);
1000 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1001 ino, session->s_mds, msg, session->s_num_cap_releases);
1003 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1004 head = msg->front.iov_base;
1005 head->num = cpu_to_le32(le32_to_cpu(head->num) + 1);
1006 item = msg->front.iov_base + msg->front.iov_len;
1007 item->ino = cpu_to_le64(ino);
1008 item->cap_id = cpu_to_le64(cap_id);
1009 item->migrate_seq = cpu_to_le32(migrate_seq);
1010 item->seq = cpu_to_le32(issue_seq);
1012 session->s_num_cap_releases--;
1014 msg->front.iov_len += sizeof(*item);
1015 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1016 dout(" release msg %p full\n", msg);
1017 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1018 } else {
1019 dout(" release msg %p at %d/%d (%d)\n", msg,
1020 (int)le32_to_cpu(head->num),
1021 (int)CEPH_CAPS_PER_RELEASE,
1022 (int)msg->front.iov_len);
1024 spin_unlock(&session->s_cap_lock);
1028 * Queue cap releases when an inode is dropped from our cache. Since
1029 * inode is about to be destroyed, there is no need for i_lock.
1031 void ceph_queue_caps_release(struct inode *inode)
1033 struct ceph_inode_info *ci = ceph_inode(inode);
1034 struct rb_node *p;
1036 p = rb_first(&ci->i_caps);
1037 while (p) {
1038 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1039 struct ceph_mds_session *session = cap->session;
1041 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1042 cap->mseq, cap->issue_seq);
1043 p = rb_next(p);
1044 __ceph_remove_cap(cap);
1049 * Send a cap msg on the given inode. Update our caps state, then
1050 * drop i_lock and send the message.
1052 * Make note of max_size reported/requested from mds, revoked caps
1053 * that have now been implemented.
1055 * Make half-hearted attempt ot to invalidate page cache if we are
1056 * dropping RDCACHE. Note that this will leave behind locked pages
1057 * that we'll then need to deal with elsewhere.
1059 * Return non-zero if delayed release, or we experienced an error
1060 * such that the caller should requeue + retry later.
1062 * called with i_lock, then drops it.
1063 * caller should hold snap_rwsem (read), s_mutex.
1065 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1066 int op, int used, int want, int retain, int flushing,
1067 unsigned *pflush_tid)
1068 __releases(cap->ci->vfs_inode->i_lock)
1070 struct ceph_inode_info *ci = cap->ci;
1071 struct inode *inode = &ci->vfs_inode;
1072 u64 cap_id = cap->cap_id;
1073 int held, revoking, dropping, keep;
1074 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1075 u64 size, max_size;
1076 struct timespec mtime, atime;
1077 int wake = 0;
1078 mode_t mode;
1079 uid_t uid;
1080 gid_t gid;
1081 struct ceph_mds_session *session;
1082 u64 xattr_version = 0;
1083 int delayed = 0;
1084 u64 flush_tid = 0;
1085 int i;
1086 int ret;
1088 held = cap->issued | cap->implemented;
1089 revoking = cap->implemented & ~cap->issued;
1090 retain &= ~revoking;
1091 dropping = cap->issued & ~retain;
1093 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1094 inode, cap, cap->session,
1095 ceph_cap_string(held), ceph_cap_string(held & retain),
1096 ceph_cap_string(revoking));
1097 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1099 session = cap->session;
1101 /* don't release wanted unless we've waited a bit. */
1102 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1103 time_before(jiffies, ci->i_hold_caps_min)) {
1104 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1105 ceph_cap_string(cap->issued),
1106 ceph_cap_string(cap->issued & retain),
1107 ceph_cap_string(cap->mds_wanted),
1108 ceph_cap_string(want));
1109 want |= cap->mds_wanted;
1110 retain |= cap->issued;
1111 delayed = 1;
1113 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1115 cap->issued &= retain; /* drop bits we don't want */
1116 if (cap->implemented & ~cap->issued) {
1118 * Wake up any waiters on wanted -> needed transition.
1119 * This is due to the weird transition from buffered
1120 * to sync IO... we need to flush dirty pages _before_
1121 * allowing sync writes to avoid reordering.
1123 wake = 1;
1125 cap->implemented &= cap->issued | used;
1126 cap->mds_wanted = want;
1128 if (flushing) {
1130 * assign a tid for flush operations so we can avoid
1131 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1132 * clean type races. track latest tid for every bit
1133 * so we can handle flush AxFw, flush Fw, and have the
1134 * first ack clean Ax.
1136 flush_tid = ++ci->i_cap_flush_last_tid;
1137 if (pflush_tid)
1138 *pflush_tid = flush_tid;
1139 dout(" cap_flush_tid %d\n", (int)flush_tid);
1140 for (i = 0; i < CEPH_CAP_BITS; i++)
1141 if (flushing & (1 << i))
1142 ci->i_cap_flush_tid[i] = flush_tid;
1145 keep = cap->implemented;
1146 seq = cap->seq;
1147 issue_seq = cap->issue_seq;
1148 mseq = cap->mseq;
1149 size = inode->i_size;
1150 ci->i_reported_size = size;
1151 max_size = ci->i_wanted_max_size;
1152 ci->i_requested_max_size = max_size;
1153 mtime = inode->i_mtime;
1154 atime = inode->i_atime;
1155 time_warp_seq = ci->i_time_warp_seq;
1156 follows = ci->i_snap_realm->cached_context->seq;
1157 uid = inode->i_uid;
1158 gid = inode->i_gid;
1159 mode = inode->i_mode;
1161 if (dropping & CEPH_CAP_XATTR_EXCL) {
1162 __ceph_build_xattrs_blob(ci);
1163 xattr_version = ci->i_xattrs.version + 1;
1166 spin_unlock(&inode->i_lock);
1168 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1169 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1170 size, max_size, &mtime, &atime, time_warp_seq,
1171 uid, gid, mode,
1172 xattr_version,
1173 (flushing & CEPH_CAP_XATTR_EXCL) ? ci->i_xattrs.blob : NULL,
1174 follows);
1175 if (ret < 0) {
1176 dout("error sending cap msg, must requeue %p\n", inode);
1177 delayed = 1;
1180 if (wake)
1181 wake_up_all(&ci->i_cap_wq);
1183 return delayed;
1187 * When a snapshot is taken, clients accumulate dirty metadata on
1188 * inodes with capabilities in ceph_cap_snaps to describe the file
1189 * state at the time the snapshot was taken. This must be flushed
1190 * asynchronously back to the MDS once sync writes complete and dirty
1191 * data is written out.
1193 * Called under i_lock. Takes s_mutex as needed.
1195 void __ceph_flush_snaps(struct ceph_inode_info *ci,
1196 struct ceph_mds_session **psession)
1197 __releases(ci->vfs_inode->i_lock)
1198 __acquires(ci->vfs_inode->i_lock)
1200 struct inode *inode = &ci->vfs_inode;
1201 int mds;
1202 struct ceph_cap_snap *capsnap;
1203 u32 mseq;
1204 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
1205 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1206 session->s_mutex */
1207 u64 next_follows = 0; /* keep track of how far we've gotten through the
1208 i_cap_snaps list, and skip these entries next time
1209 around to avoid an infinite loop */
1211 if (psession)
1212 session = *psession;
1214 dout("__flush_snaps %p\n", inode);
1215 retry:
1216 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1217 /* avoid an infiniute loop after retry */
1218 if (capsnap->follows < next_follows)
1219 continue;
1221 * we need to wait for sync writes to complete and for dirty
1222 * pages to be written out.
1224 if (capsnap->dirty_pages || capsnap->writing)
1225 continue;
1228 * if cap writeback already occurred, we should have dropped
1229 * the capsnap in ceph_put_wrbuffer_cap_refs.
1231 BUG_ON(capsnap->dirty == 0);
1233 /* pick mds, take s_mutex */
1234 if (ci->i_auth_cap == NULL) {
1235 dout("no auth cap (migrating?), doing nothing\n");
1236 goto out;
1238 mds = ci->i_auth_cap->session->s_mds;
1239 mseq = ci->i_auth_cap->mseq;
1241 if (session && session->s_mds != mds) {
1242 dout("oops, wrong session %p mutex\n", session);
1243 mutex_unlock(&session->s_mutex);
1244 ceph_put_mds_session(session);
1245 session = NULL;
1247 if (!session) {
1248 spin_unlock(&inode->i_lock);
1249 mutex_lock(&mdsc->mutex);
1250 session = __ceph_lookup_mds_session(mdsc, mds);
1251 mutex_unlock(&mdsc->mutex);
1252 if (session) {
1253 dout("inverting session/ino locks on %p\n",
1254 session);
1255 mutex_lock(&session->s_mutex);
1258 * if session == NULL, we raced against a cap
1259 * deletion or migration. retry, and we'll
1260 * get a better @mds value next time.
1262 spin_lock(&inode->i_lock);
1263 goto retry;
1266 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1267 atomic_inc(&capsnap->nref);
1268 if (!list_empty(&capsnap->flushing_item))
1269 list_del_init(&capsnap->flushing_item);
1270 list_add_tail(&capsnap->flushing_item,
1271 &session->s_cap_snaps_flushing);
1272 spin_unlock(&inode->i_lock);
1274 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1275 inode, capsnap, next_follows, capsnap->size);
1276 send_cap_msg(session, ceph_vino(inode).ino, 0,
1277 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1278 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1279 capsnap->size, 0,
1280 &capsnap->mtime, &capsnap->atime,
1281 capsnap->time_warp_seq,
1282 capsnap->uid, capsnap->gid, capsnap->mode,
1283 0, NULL,
1284 capsnap->follows);
1286 next_follows = capsnap->follows + 1;
1287 ceph_put_cap_snap(capsnap);
1289 spin_lock(&inode->i_lock);
1290 goto retry;
1293 /* we flushed them all; remove this inode from the queue */
1294 spin_lock(&mdsc->snap_flush_lock);
1295 list_del_init(&ci->i_snap_flush_item);
1296 spin_unlock(&mdsc->snap_flush_lock);
1298 out:
1299 if (psession)
1300 *psession = session;
1301 else if (session) {
1302 mutex_unlock(&session->s_mutex);
1303 ceph_put_mds_session(session);
1307 static void ceph_flush_snaps(struct ceph_inode_info *ci)
1309 struct inode *inode = &ci->vfs_inode;
1311 spin_lock(&inode->i_lock);
1312 __ceph_flush_snaps(ci, NULL);
1313 spin_unlock(&inode->i_lock);
1317 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1318 * list.
1320 void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
1322 struct ceph_mds_client *mdsc =
1323 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1324 struct inode *inode = &ci->vfs_inode;
1325 int was = ci->i_dirty_caps;
1326 int dirty = 0;
1328 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1329 ceph_cap_string(mask), ceph_cap_string(was),
1330 ceph_cap_string(was | mask));
1331 ci->i_dirty_caps |= mask;
1332 if (was == 0) {
1333 dout(" inode %p now dirty\n", &ci->vfs_inode);
1334 BUG_ON(!list_empty(&ci->i_dirty_item));
1335 spin_lock(&mdsc->cap_dirty_lock);
1336 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1337 spin_unlock(&mdsc->cap_dirty_lock);
1338 if (ci->i_flushing_caps == 0) {
1339 igrab(inode);
1340 dirty |= I_DIRTY_SYNC;
1343 BUG_ON(list_empty(&ci->i_dirty_item));
1344 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1345 (mask & CEPH_CAP_FILE_BUFFER))
1346 dirty |= I_DIRTY_DATASYNC;
1347 if (dirty)
1348 __mark_inode_dirty(inode, dirty);
1349 __cap_delay_requeue(mdsc, ci);
1353 * Add dirty inode to the flushing list. Assigned a seq number so we
1354 * can wait for caps to flush without starving.
1356 * Called under i_lock.
1358 static int __mark_caps_flushing(struct inode *inode,
1359 struct ceph_mds_session *session)
1361 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
1362 struct ceph_inode_info *ci = ceph_inode(inode);
1363 int flushing;
1365 BUG_ON(ci->i_dirty_caps == 0);
1366 BUG_ON(list_empty(&ci->i_dirty_item));
1368 flushing = ci->i_dirty_caps;
1369 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1370 ceph_cap_string(flushing),
1371 ceph_cap_string(ci->i_flushing_caps),
1372 ceph_cap_string(ci->i_flushing_caps | flushing));
1373 ci->i_flushing_caps |= flushing;
1374 ci->i_dirty_caps = 0;
1375 dout(" inode %p now !dirty\n", inode);
1377 spin_lock(&mdsc->cap_dirty_lock);
1378 list_del_init(&ci->i_dirty_item);
1380 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
1381 if (list_empty(&ci->i_flushing_item)) {
1382 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1383 mdsc->num_cap_flushing++;
1384 dout(" inode %p now flushing seq %lld\n", inode,
1385 ci->i_cap_flush_seq);
1386 } else {
1387 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1388 dout(" inode %p now flushing (more) seq %lld\n", inode,
1389 ci->i_cap_flush_seq);
1391 spin_unlock(&mdsc->cap_dirty_lock);
1393 return flushing;
1397 * try to invalidate mapping pages without blocking.
1399 static int mapping_is_empty(struct address_space *mapping)
1401 struct page *page = find_get_page(mapping, 0);
1403 if (!page)
1404 return 1;
1406 put_page(page);
1407 return 0;
1410 static int try_nonblocking_invalidate(struct inode *inode)
1412 struct ceph_inode_info *ci = ceph_inode(inode);
1413 u32 invalidating_gen = ci->i_rdcache_gen;
1415 spin_unlock(&inode->i_lock);
1416 invalidate_mapping_pages(&inode->i_data, 0, -1);
1417 spin_lock(&inode->i_lock);
1419 if (mapping_is_empty(&inode->i_data) &&
1420 invalidating_gen == ci->i_rdcache_gen) {
1421 /* success. */
1422 dout("try_nonblocking_invalidate %p success\n", inode);
1423 ci->i_rdcache_gen = 0;
1424 ci->i_rdcache_revoking = 0;
1425 return 0;
1427 dout("try_nonblocking_invalidate %p failed\n", inode);
1428 return -1;
1432 * Swiss army knife function to examine currently used and wanted
1433 * versus held caps. Release, flush, ack revoked caps to mds as
1434 * appropriate.
1436 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1437 * cap release further.
1438 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1439 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1440 * further delay.
1442 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1443 struct ceph_mds_session *session)
1445 struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode);
1446 struct ceph_mds_client *mdsc = &client->mdsc;
1447 struct inode *inode = &ci->vfs_inode;
1448 struct ceph_cap *cap;
1449 int file_wanted, used;
1450 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1451 int issued, implemented, want, retain, revoking, flushing = 0;
1452 int mds = -1; /* keep track of how far we've gone through i_caps list
1453 to avoid an infinite loop on retry */
1454 struct rb_node *p;
1455 int tried_invalidate = 0;
1456 int delayed = 0, sent = 0, force_requeue = 0, num;
1457 int queue_invalidate = 0;
1458 int is_delayed = flags & CHECK_CAPS_NODELAY;
1460 /* if we are unmounting, flush any unused caps immediately. */
1461 if (mdsc->stopping)
1462 is_delayed = 1;
1464 spin_lock(&inode->i_lock);
1466 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1467 flags |= CHECK_CAPS_FLUSH;
1469 /* flush snaps first time around only */
1470 if (!list_empty(&ci->i_cap_snaps))
1471 __ceph_flush_snaps(ci, &session);
1472 goto retry_locked;
1473 retry:
1474 spin_lock(&inode->i_lock);
1475 retry_locked:
1476 file_wanted = __ceph_caps_file_wanted(ci);
1477 used = __ceph_caps_used(ci);
1478 want = file_wanted | used;
1479 issued = __ceph_caps_issued(ci, &implemented);
1480 revoking = implemented & ~issued;
1482 retain = want | CEPH_CAP_PIN;
1483 if (!mdsc->stopping && inode->i_nlink > 0) {
1484 if (want) {
1485 retain |= CEPH_CAP_ANY; /* be greedy */
1486 } else {
1487 retain |= CEPH_CAP_ANY_SHARED;
1489 * keep RD only if we didn't have the file open RW,
1490 * because then the mds would revoke it anyway to
1491 * journal max_size=0.
1493 if (ci->i_max_size == 0)
1494 retain |= CEPH_CAP_ANY_RD;
1498 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1499 " issued %s revoking %s retain %s %s%s%s\n", inode,
1500 ceph_cap_string(file_wanted),
1501 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1502 ceph_cap_string(ci->i_flushing_caps),
1503 ceph_cap_string(issued), ceph_cap_string(revoking),
1504 ceph_cap_string(retain),
1505 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1506 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1507 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1510 * If we no longer need to hold onto old our caps, and we may
1511 * have cached pages, but don't want them, then try to invalidate.
1512 * If we fail, it's because pages are locked.... try again later.
1514 if ((!is_delayed || mdsc->stopping) &&
1515 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
1516 ci->i_rdcache_gen && /* may have cached pages */
1517 (file_wanted == 0 || /* no open files */
1518 (revoking & (CEPH_CAP_FILE_CACHE|
1519 CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
1520 !tried_invalidate) {
1521 dout("check_caps trying to invalidate on %p\n", inode);
1522 if (try_nonblocking_invalidate(inode) < 0) {
1523 if (revoking & (CEPH_CAP_FILE_CACHE|
1524 CEPH_CAP_FILE_LAZYIO)) {
1525 dout("check_caps queuing invalidate\n");
1526 queue_invalidate = 1;
1527 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1528 } else {
1529 dout("check_caps failed to invalidate pages\n");
1530 /* we failed to invalidate pages. check these
1531 caps again later. */
1532 force_requeue = 1;
1533 __cap_set_timeouts(mdsc, ci);
1536 tried_invalidate = 1;
1537 goto retry_locked;
1540 num = 0;
1541 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1542 cap = rb_entry(p, struct ceph_cap, ci_node);
1543 num++;
1545 /* avoid looping forever */
1546 if (mds >= cap->mds ||
1547 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1548 continue;
1550 /* NOTE: no side-effects allowed, until we take s_mutex */
1552 revoking = cap->implemented & ~cap->issued;
1553 if (revoking)
1554 dout(" mds%d revoking %s\n", cap->mds,
1555 ceph_cap_string(revoking));
1557 if (cap == ci->i_auth_cap &&
1558 (cap->issued & CEPH_CAP_FILE_WR)) {
1559 /* request larger max_size from MDS? */
1560 if (ci->i_wanted_max_size > ci->i_max_size &&
1561 ci->i_wanted_max_size > ci->i_requested_max_size) {
1562 dout("requesting new max_size\n");
1563 goto ack;
1566 /* approaching file_max? */
1567 if ((inode->i_size << 1) >= ci->i_max_size &&
1568 (ci->i_reported_size << 1) < ci->i_max_size) {
1569 dout("i_size approaching max_size\n");
1570 goto ack;
1573 /* flush anything dirty? */
1574 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1575 ci->i_dirty_caps) {
1576 dout("flushing dirty caps\n");
1577 goto ack;
1580 /* completed revocation? going down and there are no caps? */
1581 if (revoking && (revoking & used) == 0) {
1582 dout("completed revocation of %s\n",
1583 ceph_cap_string(cap->implemented & ~cap->issued));
1584 goto ack;
1587 /* want more caps from mds? */
1588 if (want & ~(cap->mds_wanted | cap->issued))
1589 goto ack;
1591 /* things we might delay */
1592 if ((cap->issued & ~retain) == 0 &&
1593 cap->mds_wanted == want)
1594 continue; /* nope, all good */
1596 if (is_delayed)
1597 goto ack;
1599 /* delay? */
1600 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1601 time_before(jiffies, ci->i_hold_caps_max)) {
1602 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1603 ceph_cap_string(cap->issued),
1604 ceph_cap_string(cap->issued & retain),
1605 ceph_cap_string(cap->mds_wanted),
1606 ceph_cap_string(want));
1607 delayed++;
1608 continue;
1611 ack:
1612 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1613 dout(" skipping %p I_NOFLUSH set\n", inode);
1614 continue;
1617 if (session && session != cap->session) {
1618 dout("oops, wrong session %p mutex\n", session);
1619 mutex_unlock(&session->s_mutex);
1620 session = NULL;
1622 if (!session) {
1623 session = cap->session;
1624 if (mutex_trylock(&session->s_mutex) == 0) {
1625 dout("inverting session/ino locks on %p\n",
1626 session);
1627 spin_unlock(&inode->i_lock);
1628 if (took_snap_rwsem) {
1629 up_read(&mdsc->snap_rwsem);
1630 took_snap_rwsem = 0;
1632 mutex_lock(&session->s_mutex);
1633 goto retry;
1636 /* take snap_rwsem after session mutex */
1637 if (!took_snap_rwsem) {
1638 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1639 dout("inverting snap/in locks on %p\n",
1640 inode);
1641 spin_unlock(&inode->i_lock);
1642 down_read(&mdsc->snap_rwsem);
1643 took_snap_rwsem = 1;
1644 goto retry;
1646 took_snap_rwsem = 1;
1649 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1650 flushing = __mark_caps_flushing(inode, session);
1652 mds = cap->mds; /* remember mds, so we don't repeat */
1653 sent++;
1655 /* __send_cap drops i_lock */
1656 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
1657 retain, flushing, NULL);
1658 goto retry; /* retake i_lock and restart our cap scan. */
1662 * Reschedule delayed caps release if we delayed anything,
1663 * otherwise cancel.
1665 if (delayed && is_delayed)
1666 force_requeue = 1; /* __send_cap delayed release; requeue */
1667 if (!delayed && !is_delayed)
1668 __cap_delay_cancel(mdsc, ci);
1669 else if (!is_delayed || force_requeue)
1670 __cap_delay_requeue(mdsc, ci);
1672 spin_unlock(&inode->i_lock);
1674 if (queue_invalidate)
1675 ceph_queue_invalidate(inode);
1677 if (session)
1678 mutex_unlock(&session->s_mutex);
1679 if (took_snap_rwsem)
1680 up_read(&mdsc->snap_rwsem);
1684 * Try to flush dirty caps back to the auth mds.
1686 static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1687 unsigned *flush_tid)
1689 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
1690 struct ceph_inode_info *ci = ceph_inode(inode);
1691 int unlock_session = session ? 0 : 1;
1692 int flushing = 0;
1694 retry:
1695 spin_lock(&inode->i_lock);
1696 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1697 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1698 goto out;
1700 if (ci->i_dirty_caps && ci->i_auth_cap) {
1701 struct ceph_cap *cap = ci->i_auth_cap;
1702 int used = __ceph_caps_used(ci);
1703 int want = __ceph_caps_wanted(ci);
1704 int delayed;
1706 if (!session) {
1707 spin_unlock(&inode->i_lock);
1708 session = cap->session;
1709 mutex_lock(&session->s_mutex);
1710 goto retry;
1712 BUG_ON(session != cap->session);
1713 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1714 goto out;
1716 flushing = __mark_caps_flushing(inode, session);
1718 /* __send_cap drops i_lock */
1719 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1720 cap->issued | cap->implemented, flushing,
1721 flush_tid);
1722 if (!delayed)
1723 goto out_unlocked;
1725 spin_lock(&inode->i_lock);
1726 __cap_delay_requeue(mdsc, ci);
1728 out:
1729 spin_unlock(&inode->i_lock);
1730 out_unlocked:
1731 if (session && unlock_session)
1732 mutex_unlock(&session->s_mutex);
1733 return flushing;
1737 * Return true if we've flushed caps through the given flush_tid.
1739 static int caps_are_flushed(struct inode *inode, unsigned tid)
1741 struct ceph_inode_info *ci = ceph_inode(inode);
1742 int i, ret = 1;
1744 spin_lock(&inode->i_lock);
1745 for (i = 0; i < CEPH_CAP_BITS; i++)
1746 if ((ci->i_flushing_caps & (1 << i)) &&
1747 ci->i_cap_flush_tid[i] <= tid) {
1748 /* still flushing this bit */
1749 ret = 0;
1750 break;
1752 spin_unlock(&inode->i_lock);
1753 return ret;
1757 * Wait on any unsafe replies for the given inode. First wait on the
1758 * newest request, and make that the upper bound. Then, if there are
1759 * more requests, keep waiting on the oldest as long as it is still older
1760 * than the original request.
1762 static void sync_write_wait(struct inode *inode)
1764 struct ceph_inode_info *ci = ceph_inode(inode);
1765 struct list_head *head = &ci->i_unsafe_writes;
1766 struct ceph_osd_request *req;
1767 u64 last_tid;
1769 spin_lock(&ci->i_unsafe_lock);
1770 if (list_empty(head))
1771 goto out;
1773 /* set upper bound as _last_ entry in chain */
1774 req = list_entry(head->prev, struct ceph_osd_request,
1775 r_unsafe_item);
1776 last_tid = req->r_tid;
1778 do {
1779 ceph_osdc_get_request(req);
1780 spin_unlock(&ci->i_unsafe_lock);
1781 dout("sync_write_wait on tid %llu (until %llu)\n",
1782 req->r_tid, last_tid);
1783 wait_for_completion(&req->r_safe_completion);
1784 spin_lock(&ci->i_unsafe_lock);
1785 ceph_osdc_put_request(req);
1788 * from here on look at first entry in chain, since we
1789 * only want to wait for anything older than last_tid
1791 if (list_empty(head))
1792 break;
1793 req = list_entry(head->next, struct ceph_osd_request,
1794 r_unsafe_item);
1795 } while (req->r_tid < last_tid);
1796 out:
1797 spin_unlock(&ci->i_unsafe_lock);
1800 int ceph_fsync(struct file *file, int datasync)
1802 struct inode *inode = file->f_mapping->host;
1803 struct ceph_inode_info *ci = ceph_inode(inode);
1804 unsigned flush_tid;
1805 int ret;
1806 int dirty;
1808 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1809 sync_write_wait(inode);
1811 ret = filemap_write_and_wait(inode->i_mapping);
1812 if (ret < 0)
1813 return ret;
1815 dirty = try_flush_caps(inode, NULL, &flush_tid);
1816 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1819 * only wait on non-file metadata writeback (the mds
1820 * can recover size and mtime, so we don't need to
1821 * wait for that)
1823 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1824 dout("fsync waiting for flush_tid %u\n", flush_tid);
1825 ret = wait_event_interruptible(ci->i_cap_wq,
1826 caps_are_flushed(inode, flush_tid));
1829 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
1830 return ret;
1834 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1835 * queue inode for flush but don't do so immediately, because we can
1836 * get by with fewer MDS messages if we wait for data writeback to
1837 * complete first.
1839 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
1841 struct ceph_inode_info *ci = ceph_inode(inode);
1842 unsigned flush_tid;
1843 int err = 0;
1844 int dirty;
1845 int wait = wbc->sync_mode == WB_SYNC_ALL;
1847 dout("write_inode %p wait=%d\n", inode, wait);
1848 if (wait) {
1849 dirty = try_flush_caps(inode, NULL, &flush_tid);
1850 if (dirty)
1851 err = wait_event_interruptible(ci->i_cap_wq,
1852 caps_are_flushed(inode, flush_tid));
1853 } else {
1854 struct ceph_mds_client *mdsc =
1855 &ceph_sb_to_client(inode->i_sb)->mdsc;
1857 spin_lock(&inode->i_lock);
1858 if (__ceph_caps_dirty(ci))
1859 __cap_delay_requeue_front(mdsc, ci);
1860 spin_unlock(&inode->i_lock);
1862 return err;
1866 * After a recovering MDS goes active, we need to resend any caps
1867 * we were flushing.
1869 * Caller holds session->s_mutex.
1871 static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1872 struct ceph_mds_session *session)
1874 struct ceph_cap_snap *capsnap;
1876 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1877 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1878 flushing_item) {
1879 struct ceph_inode_info *ci = capsnap->ci;
1880 struct inode *inode = &ci->vfs_inode;
1881 struct ceph_cap *cap;
1883 spin_lock(&inode->i_lock);
1884 cap = ci->i_auth_cap;
1885 if (cap && cap->session == session) {
1886 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1887 cap, capsnap);
1888 __ceph_flush_snaps(ci, &session);
1889 } else {
1890 pr_err("%p auth cap %p not mds%d ???\n", inode,
1891 cap, session->s_mds);
1893 spin_unlock(&inode->i_lock);
1897 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1898 struct ceph_mds_session *session)
1900 struct ceph_inode_info *ci;
1902 kick_flushing_capsnaps(mdsc, session);
1904 dout("kick_flushing_caps mds%d\n", session->s_mds);
1905 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1906 struct inode *inode = &ci->vfs_inode;
1907 struct ceph_cap *cap;
1908 int delayed = 0;
1910 spin_lock(&inode->i_lock);
1911 cap = ci->i_auth_cap;
1912 if (cap && cap->session == session) {
1913 dout("kick_flushing_caps %p cap %p %s\n", inode,
1914 cap, ceph_cap_string(ci->i_flushing_caps));
1915 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1916 __ceph_caps_used(ci),
1917 __ceph_caps_wanted(ci),
1918 cap->issued | cap->implemented,
1919 ci->i_flushing_caps, NULL);
1920 if (delayed) {
1921 spin_lock(&inode->i_lock);
1922 __cap_delay_requeue(mdsc, ci);
1923 spin_unlock(&inode->i_lock);
1925 } else {
1926 pr_err("%p auth cap %p not mds%d ???\n", inode,
1927 cap, session->s_mds);
1928 spin_unlock(&inode->i_lock);
1935 * Take references to capabilities we hold, so that we don't release
1936 * them to the MDS prematurely.
1938 * Protected by i_lock.
1940 static void __take_cap_refs(struct ceph_inode_info *ci, int got)
1942 if (got & CEPH_CAP_PIN)
1943 ci->i_pin_ref++;
1944 if (got & CEPH_CAP_FILE_RD)
1945 ci->i_rd_ref++;
1946 if (got & CEPH_CAP_FILE_CACHE)
1947 ci->i_rdcache_ref++;
1948 if (got & CEPH_CAP_FILE_WR)
1949 ci->i_wr_ref++;
1950 if (got & CEPH_CAP_FILE_BUFFER) {
1951 if (ci->i_wrbuffer_ref == 0)
1952 igrab(&ci->vfs_inode);
1953 ci->i_wrbuffer_ref++;
1954 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1955 &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref);
1960 * Try to grab cap references. Specify those refs we @want, and the
1961 * minimal set we @need. Also include the larger offset we are writing
1962 * to (when applicable), and check against max_size here as well.
1963 * Note that caller is responsible for ensuring max_size increases are
1964 * requested from the MDS.
1966 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
1967 int *got, loff_t endoff, int *check_max, int *err)
1969 struct inode *inode = &ci->vfs_inode;
1970 int ret = 0;
1971 int have, implemented;
1972 int file_wanted;
1974 dout("get_cap_refs %p need %s want %s\n", inode,
1975 ceph_cap_string(need), ceph_cap_string(want));
1976 spin_lock(&inode->i_lock);
1978 /* make sure file is actually open */
1979 file_wanted = __ceph_caps_file_wanted(ci);
1980 if ((file_wanted & need) == 0) {
1981 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1982 ceph_cap_string(need), ceph_cap_string(file_wanted));
1983 *err = -EBADF;
1984 ret = 1;
1985 goto out;
1988 if (need & CEPH_CAP_FILE_WR) {
1989 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
1990 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
1991 inode, endoff, ci->i_max_size);
1992 if (endoff > ci->i_wanted_max_size) {
1993 *check_max = 1;
1994 ret = 1;
1996 goto out;
1999 * If a sync write is in progress, we must wait, so that we
2000 * can get a final snapshot value for size+mtime.
2002 if (__ceph_have_pending_cap_snap(ci)) {
2003 dout("get_cap_refs %p cap_snap_pending\n", inode);
2004 goto out;
2007 have = __ceph_caps_issued(ci, &implemented);
2010 * disallow writes while a truncate is pending
2012 if (ci->i_truncate_pending)
2013 have &= ~CEPH_CAP_FILE_WR;
2015 if ((have & need) == need) {
2017 * Look at (implemented & ~have & not) so that we keep waiting
2018 * on transition from wanted -> needed caps. This is needed
2019 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2020 * going before a prior buffered writeback happens.
2022 int not = want & ~(have & need);
2023 int revoking = implemented & ~have;
2024 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2025 inode, ceph_cap_string(have), ceph_cap_string(not),
2026 ceph_cap_string(revoking));
2027 if ((revoking & not) == 0) {
2028 *got = need | (have & want);
2029 __take_cap_refs(ci, *got);
2030 ret = 1;
2032 } else {
2033 dout("get_cap_refs %p have %s needed %s\n", inode,
2034 ceph_cap_string(have), ceph_cap_string(need));
2036 out:
2037 spin_unlock(&inode->i_lock);
2038 dout("get_cap_refs %p ret %d got %s\n", inode,
2039 ret, ceph_cap_string(*got));
2040 return ret;
2044 * Check the offset we are writing up to against our current
2045 * max_size. If necessary, tell the MDS we want to write to
2046 * a larger offset.
2048 static void check_max_size(struct inode *inode, loff_t endoff)
2050 struct ceph_inode_info *ci = ceph_inode(inode);
2051 int check = 0;
2053 /* do we need to explicitly request a larger max_size? */
2054 spin_lock(&inode->i_lock);
2055 if ((endoff >= ci->i_max_size ||
2056 endoff > (inode->i_size << 1)) &&
2057 endoff > ci->i_wanted_max_size) {
2058 dout("write %p at large endoff %llu, req max_size\n",
2059 inode, endoff);
2060 ci->i_wanted_max_size = endoff;
2061 check = 1;
2063 spin_unlock(&inode->i_lock);
2064 if (check)
2065 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2069 * Wait for caps, and take cap references. If we can't get a WR cap
2070 * due to a small max_size, make sure we check_max_size (and possibly
2071 * ask the mds) so we don't get hung up indefinitely.
2073 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2074 loff_t endoff)
2076 int check_max, ret, err;
2078 retry:
2079 if (endoff > 0)
2080 check_max_size(&ci->vfs_inode, endoff);
2081 check_max = 0;
2082 err = 0;
2083 ret = wait_event_interruptible(ci->i_cap_wq,
2084 try_get_cap_refs(ci, need, want,
2085 got, endoff,
2086 &check_max, &err));
2087 if (err)
2088 ret = err;
2089 if (check_max)
2090 goto retry;
2091 return ret;
2095 * Take cap refs. Caller must already know we hold at least one ref
2096 * on the caps in question or we don't know this is safe.
2098 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2100 spin_lock(&ci->vfs_inode.i_lock);
2101 __take_cap_refs(ci, caps);
2102 spin_unlock(&ci->vfs_inode.i_lock);
2106 * Release cap refs.
2108 * If we released the last ref on any given cap, call ceph_check_caps
2109 * to release (or schedule a release).
2111 * If we are releasing a WR cap (from a sync write), finalize any affected
2112 * cap_snap, and wake up any waiters.
2114 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2116 struct inode *inode = &ci->vfs_inode;
2117 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2118 struct ceph_cap_snap *capsnap;
2120 spin_lock(&inode->i_lock);
2121 if (had & CEPH_CAP_PIN)
2122 --ci->i_pin_ref;
2123 if (had & CEPH_CAP_FILE_RD)
2124 if (--ci->i_rd_ref == 0)
2125 last++;
2126 if (had & CEPH_CAP_FILE_CACHE)
2127 if (--ci->i_rdcache_ref == 0)
2128 last++;
2129 if (had & CEPH_CAP_FILE_BUFFER) {
2130 if (--ci->i_wrbuffer_ref == 0) {
2131 last++;
2132 put++;
2134 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2135 inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref);
2137 if (had & CEPH_CAP_FILE_WR)
2138 if (--ci->i_wr_ref == 0) {
2139 last++;
2140 if (!list_empty(&ci->i_cap_snaps)) {
2141 capsnap = list_first_entry(&ci->i_cap_snaps,
2142 struct ceph_cap_snap,
2143 ci_item);
2144 if (capsnap->writing) {
2145 capsnap->writing = 0;
2146 flushsnaps =
2147 __ceph_finish_cap_snap(ci,
2148 capsnap);
2149 wake = 1;
2153 spin_unlock(&inode->i_lock);
2155 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2156 last ? " last" : "", put ? " put" : "");
2158 if (last && !flushsnaps)
2159 ceph_check_caps(ci, 0, NULL);
2160 else if (flushsnaps)
2161 ceph_flush_snaps(ci);
2162 if (wake)
2163 wake_up_all(&ci->i_cap_wq);
2164 if (put)
2165 iput(inode);
2169 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2170 * context. Adjust per-snap dirty page accounting as appropriate.
2171 * Once all dirty data for a cap_snap is flushed, flush snapped file
2172 * metadata back to the MDS. If we dropped the last ref, call
2173 * ceph_check_caps.
2175 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2176 struct ceph_snap_context *snapc)
2178 struct inode *inode = &ci->vfs_inode;
2179 int last = 0;
2180 int complete_capsnap = 0;
2181 int drop_capsnap = 0;
2182 int found = 0;
2183 struct ceph_cap_snap *capsnap = NULL;
2185 spin_lock(&inode->i_lock);
2186 ci->i_wrbuffer_ref -= nr;
2187 last = !ci->i_wrbuffer_ref;
2189 if (ci->i_head_snapc == snapc) {
2190 ci->i_wrbuffer_ref_head -= nr;
2191 if (!ci->i_wrbuffer_ref_head) {
2192 ceph_put_snap_context(ci->i_head_snapc);
2193 ci->i_head_snapc = NULL;
2195 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2196 inode,
2197 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2198 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2199 last ? " LAST" : "");
2200 } else {
2201 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2202 if (capsnap->context == snapc) {
2203 found = 1;
2204 break;
2207 BUG_ON(!found);
2208 capsnap->dirty_pages -= nr;
2209 if (capsnap->dirty_pages == 0) {
2210 complete_capsnap = 1;
2211 if (capsnap->dirty == 0)
2212 /* cap writeback completed before we created
2213 * the cap_snap; no FLUSHSNAP is needed */
2214 drop_capsnap = 1;
2216 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2217 " snap %lld %d/%d -> %d/%d %s%s%s\n",
2218 inode, capsnap, capsnap->context->seq,
2219 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2220 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2221 last ? " (wrbuffer last)" : "",
2222 complete_capsnap ? " (complete capsnap)" : "",
2223 drop_capsnap ? " (drop capsnap)" : "");
2224 if (drop_capsnap) {
2225 ceph_put_snap_context(capsnap->context);
2226 list_del(&capsnap->ci_item);
2227 list_del(&capsnap->flushing_item);
2228 ceph_put_cap_snap(capsnap);
2232 spin_unlock(&inode->i_lock);
2234 if (last) {
2235 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2236 iput(inode);
2237 } else if (complete_capsnap) {
2238 ceph_flush_snaps(ci);
2239 wake_up_all(&ci->i_cap_wq);
2241 if (drop_capsnap)
2242 iput(inode);
2246 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2247 * actually be a revocation if it specifies a smaller cap set.)
2249 * caller holds s_mutex and i_lock, we drop both.
2251 * return value:
2252 * 0 - ok
2253 * 1 - check_caps on auth cap only (writeback)
2254 * 2 - check_caps (ack revoke)
2256 static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2257 struct ceph_mds_session *session,
2258 struct ceph_cap *cap,
2259 struct ceph_buffer *xattr_buf)
2260 __releases(inode->i_lock)
2262 struct ceph_inode_info *ci = ceph_inode(inode);
2263 int mds = session->s_mds;
2264 int seq = le32_to_cpu(grant->seq);
2265 int newcaps = le32_to_cpu(grant->caps);
2266 int issued, implemented, used, wanted, dirty;
2267 u64 size = le64_to_cpu(grant->size);
2268 u64 max_size = le64_to_cpu(grant->max_size);
2269 struct timespec mtime, atime, ctime;
2270 int check_caps = 0;
2271 int wake = 0;
2272 int writeback = 0;
2273 int revoked_rdcache = 0;
2274 int queue_invalidate = 0;
2276 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2277 inode, cap, mds, seq, ceph_cap_string(newcaps));
2278 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2279 inode->i_size);
2282 * If CACHE is being revoked, and we have no dirty buffers,
2283 * try to invalidate (once). (If there are dirty buffers, we
2284 * will invalidate _after_ writeback.)
2286 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2287 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2288 !ci->i_wrbuffer_ref) {
2289 if (try_nonblocking_invalidate(inode) == 0) {
2290 revoked_rdcache = 1;
2291 } else {
2292 /* there were locked pages.. invalidate later
2293 in a separate thread. */
2294 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2295 queue_invalidate = 1;
2296 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2301 /* side effects now are allowed */
2303 issued = __ceph_caps_issued(ci, &implemented);
2304 issued |= implemented | __ceph_caps_dirty(ci);
2306 cap->cap_gen = session->s_cap_gen;
2308 __check_cap_issue(ci, cap, newcaps);
2310 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2311 inode->i_mode = le32_to_cpu(grant->mode);
2312 inode->i_uid = le32_to_cpu(grant->uid);
2313 inode->i_gid = le32_to_cpu(grant->gid);
2314 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2315 inode->i_uid, inode->i_gid);
2318 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
2319 inode->i_nlink = le32_to_cpu(grant->nlink);
2321 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2322 int len = le32_to_cpu(grant->xattr_len);
2323 u64 version = le64_to_cpu(grant->xattr_version);
2325 if (version > ci->i_xattrs.version) {
2326 dout(" got new xattrs v%llu on %p len %d\n",
2327 version, inode, len);
2328 if (ci->i_xattrs.blob)
2329 ceph_buffer_put(ci->i_xattrs.blob);
2330 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2331 ci->i_xattrs.version = version;
2335 /* size/ctime/mtime/atime? */
2336 ceph_fill_file_size(inode, issued,
2337 le32_to_cpu(grant->truncate_seq),
2338 le64_to_cpu(grant->truncate_size), size);
2339 ceph_decode_timespec(&mtime, &grant->mtime);
2340 ceph_decode_timespec(&atime, &grant->atime);
2341 ceph_decode_timespec(&ctime, &grant->ctime);
2342 ceph_fill_file_time(inode, issued,
2343 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2344 &atime);
2346 /* max size increase? */
2347 if (max_size != ci->i_max_size) {
2348 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2349 ci->i_max_size = max_size;
2350 if (max_size >= ci->i_wanted_max_size) {
2351 ci->i_wanted_max_size = 0; /* reset */
2352 ci->i_requested_max_size = 0;
2354 wake = 1;
2357 /* check cap bits */
2358 wanted = __ceph_caps_wanted(ci);
2359 used = __ceph_caps_used(ci);
2360 dirty = __ceph_caps_dirty(ci);
2361 dout(" my wanted = %s, used = %s, dirty %s\n",
2362 ceph_cap_string(wanted),
2363 ceph_cap_string(used),
2364 ceph_cap_string(dirty));
2365 if (wanted != le32_to_cpu(grant->wanted)) {
2366 dout("mds wanted %s -> %s\n",
2367 ceph_cap_string(le32_to_cpu(grant->wanted)),
2368 ceph_cap_string(wanted));
2369 grant->wanted = cpu_to_le32(wanted);
2372 cap->seq = seq;
2374 /* file layout may have changed */
2375 ci->i_layout = grant->layout;
2377 /* revocation, grant, or no-op? */
2378 if (cap->issued & ~newcaps) {
2379 int revoking = cap->issued & ~newcaps;
2381 dout("revocation: %s -> %s (revoking %s)\n",
2382 ceph_cap_string(cap->issued),
2383 ceph_cap_string(newcaps),
2384 ceph_cap_string(revoking));
2385 if (revoking & CEPH_CAP_FILE_BUFFER)
2386 writeback = 1; /* initiate writeback; will delay ack */
2387 else if (revoking == CEPH_CAP_FILE_CACHE &&
2388 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2389 queue_invalidate)
2390 ; /* do nothing yet, invalidation will be queued */
2391 else if (cap == ci->i_auth_cap)
2392 check_caps = 1; /* check auth cap only */
2393 else
2394 check_caps = 2; /* check all caps */
2395 cap->issued = newcaps;
2396 cap->implemented |= newcaps;
2397 } else if (cap->issued == newcaps) {
2398 dout("caps unchanged: %s -> %s\n",
2399 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2400 } else {
2401 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2402 ceph_cap_string(newcaps));
2403 cap->issued = newcaps;
2404 cap->implemented |= newcaps; /* add bits only, to
2405 * avoid stepping on a
2406 * pending revocation */
2407 wake = 1;
2409 BUG_ON(cap->issued & ~cap->implemented);
2411 spin_unlock(&inode->i_lock);
2412 if (writeback)
2414 * queue inode for writeback: we can't actually call
2415 * filemap_write_and_wait, etc. from message handler
2416 * context.
2418 ceph_queue_writeback(inode);
2419 if (queue_invalidate)
2420 ceph_queue_invalidate(inode);
2421 if (wake)
2422 wake_up_all(&ci->i_cap_wq);
2424 if (check_caps == 1)
2425 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2426 session);
2427 else if (check_caps == 2)
2428 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2429 else
2430 mutex_unlock(&session->s_mutex);
2434 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2435 * MDS has been safely committed.
2437 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
2438 struct ceph_mds_caps *m,
2439 struct ceph_mds_session *session,
2440 struct ceph_cap *cap)
2441 __releases(inode->i_lock)
2443 struct ceph_inode_info *ci = ceph_inode(inode);
2444 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
2445 unsigned seq = le32_to_cpu(m->seq);
2446 int dirty = le32_to_cpu(m->dirty);
2447 int cleaned = 0;
2448 int drop = 0;
2449 int i;
2451 for (i = 0; i < CEPH_CAP_BITS; i++)
2452 if ((dirty & (1 << i)) &&
2453 flush_tid == ci->i_cap_flush_tid[i])
2454 cleaned |= 1 << i;
2456 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2457 " flushing %s -> %s\n",
2458 inode, session->s_mds, seq, ceph_cap_string(dirty),
2459 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2460 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2462 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2463 goto out;
2465 ci->i_flushing_caps &= ~cleaned;
2467 spin_lock(&mdsc->cap_dirty_lock);
2468 if (ci->i_flushing_caps == 0) {
2469 list_del_init(&ci->i_flushing_item);
2470 if (!list_empty(&session->s_cap_flushing))
2471 dout(" mds%d still flushing cap on %p\n",
2472 session->s_mds,
2473 &list_entry(session->s_cap_flushing.next,
2474 struct ceph_inode_info,
2475 i_flushing_item)->vfs_inode);
2476 mdsc->num_cap_flushing--;
2477 wake_up_all(&mdsc->cap_flushing_wq);
2478 dout(" inode %p now !flushing\n", inode);
2480 if (ci->i_dirty_caps == 0) {
2481 dout(" inode %p now clean\n", inode);
2482 BUG_ON(!list_empty(&ci->i_dirty_item));
2483 drop = 1;
2484 } else {
2485 BUG_ON(list_empty(&ci->i_dirty_item));
2488 spin_unlock(&mdsc->cap_dirty_lock);
2489 wake_up_all(&ci->i_cap_wq);
2491 out:
2492 spin_unlock(&inode->i_lock);
2493 if (drop)
2494 iput(inode);
2498 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2499 * throw away our cap_snap.
2501 * Caller hold s_mutex.
2503 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
2504 struct ceph_mds_caps *m,
2505 struct ceph_mds_session *session)
2507 struct ceph_inode_info *ci = ceph_inode(inode);
2508 u64 follows = le64_to_cpu(m->snap_follows);
2509 struct ceph_cap_snap *capsnap;
2510 int drop = 0;
2512 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2513 inode, ci, session->s_mds, follows);
2515 spin_lock(&inode->i_lock);
2516 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2517 if (capsnap->follows == follows) {
2518 if (capsnap->flush_tid != flush_tid) {
2519 dout(" cap_snap %p follows %lld tid %lld !="
2520 " %lld\n", capsnap, follows,
2521 flush_tid, capsnap->flush_tid);
2522 break;
2524 WARN_ON(capsnap->dirty_pages || capsnap->writing);
2525 dout(" removing %p cap_snap %p follows %lld\n",
2526 inode, capsnap, follows);
2527 ceph_put_snap_context(capsnap->context);
2528 list_del(&capsnap->ci_item);
2529 list_del(&capsnap->flushing_item);
2530 ceph_put_cap_snap(capsnap);
2531 drop = 1;
2532 break;
2533 } else {
2534 dout(" skipping cap_snap %p follows %lld\n",
2535 capsnap, capsnap->follows);
2538 spin_unlock(&inode->i_lock);
2539 if (drop)
2540 iput(inode);
2544 * Handle TRUNC from MDS, indicating file truncation.
2546 * caller hold s_mutex.
2548 static void handle_cap_trunc(struct inode *inode,
2549 struct ceph_mds_caps *trunc,
2550 struct ceph_mds_session *session)
2551 __releases(inode->i_lock)
2553 struct ceph_inode_info *ci = ceph_inode(inode);
2554 int mds = session->s_mds;
2555 int seq = le32_to_cpu(trunc->seq);
2556 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2557 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2558 u64 size = le64_to_cpu(trunc->size);
2559 int implemented = 0;
2560 int dirty = __ceph_caps_dirty(ci);
2561 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2562 int queue_trunc = 0;
2564 issued |= implemented | dirty;
2566 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2567 inode, mds, seq, truncate_size, truncate_seq);
2568 queue_trunc = ceph_fill_file_size(inode, issued,
2569 truncate_seq, truncate_size, size);
2570 spin_unlock(&inode->i_lock);
2572 if (queue_trunc)
2573 ceph_queue_vmtruncate(inode);
2577 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2578 * different one. If we are the most recent migration we've seen (as
2579 * indicated by mseq), make note of the migrating cap bits for the
2580 * duration (until we see the corresponding IMPORT).
2582 * caller holds s_mutex
2584 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
2585 struct ceph_mds_session *session)
2587 struct ceph_inode_info *ci = ceph_inode(inode);
2588 int mds = session->s_mds;
2589 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2590 struct ceph_cap *cap = NULL, *t;
2591 struct rb_node *p;
2592 int remember = 1;
2594 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2595 inode, ci, mds, mseq);
2597 spin_lock(&inode->i_lock);
2599 /* make sure we haven't seen a higher mseq */
2600 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2601 t = rb_entry(p, struct ceph_cap, ci_node);
2602 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2603 dout(" higher mseq on cap from mds%d\n",
2604 t->session->s_mds);
2605 remember = 0;
2607 if (t->session->s_mds == mds)
2608 cap = t;
2611 if (cap) {
2612 if (remember) {
2613 /* make note */
2614 ci->i_cap_exporting_mds = mds;
2615 ci->i_cap_exporting_mseq = mseq;
2616 ci->i_cap_exporting_issued = cap->issued;
2618 __ceph_remove_cap(cap);
2620 /* else, we already released it */
2622 spin_unlock(&inode->i_lock);
2626 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2627 * clean them up.
2629 * caller holds s_mutex.
2631 static void handle_cap_import(struct ceph_mds_client *mdsc,
2632 struct inode *inode, struct ceph_mds_caps *im,
2633 struct ceph_mds_session *session,
2634 void *snaptrace, int snaptrace_len)
2636 struct ceph_inode_info *ci = ceph_inode(inode);
2637 int mds = session->s_mds;
2638 unsigned issued = le32_to_cpu(im->caps);
2639 unsigned wanted = le32_to_cpu(im->wanted);
2640 unsigned seq = le32_to_cpu(im->seq);
2641 unsigned mseq = le32_to_cpu(im->migrate_seq);
2642 u64 realmino = le64_to_cpu(im->realm);
2643 u64 cap_id = le64_to_cpu(im->cap_id);
2645 if (ci->i_cap_exporting_mds >= 0 &&
2646 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2647 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2648 " - cleared exporting from mds%d\n",
2649 inode, ci, mds, mseq,
2650 ci->i_cap_exporting_mds);
2651 ci->i_cap_exporting_issued = 0;
2652 ci->i_cap_exporting_mseq = 0;
2653 ci->i_cap_exporting_mds = -1;
2654 } else {
2655 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2656 inode, ci, mds, mseq);
2659 down_write(&mdsc->snap_rwsem);
2660 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2661 false);
2662 downgrade_write(&mdsc->snap_rwsem);
2663 ceph_add_cap(inode, session, cap_id, -1,
2664 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2665 NULL /* no caps context */);
2666 try_flush_caps(inode, session, NULL);
2667 up_read(&mdsc->snap_rwsem);
2671 * Handle a caps message from the MDS.
2673 * Identify the appropriate session, inode, and call the right handler
2674 * based on the cap op.
2676 void ceph_handle_caps(struct ceph_mds_session *session,
2677 struct ceph_msg *msg)
2679 struct ceph_mds_client *mdsc = session->s_mdsc;
2680 struct super_block *sb = mdsc->client->sb;
2681 struct inode *inode;
2682 struct ceph_cap *cap;
2683 struct ceph_mds_caps *h;
2684 int mds = session->s_mds;
2685 int op;
2686 u32 seq, mseq;
2687 struct ceph_vino vino;
2688 u64 cap_id;
2689 u64 size, max_size;
2690 u64 tid;
2691 void *snaptrace;
2693 dout("handle_caps from mds%d\n", mds);
2695 /* decode */
2696 tid = le64_to_cpu(msg->hdr.tid);
2697 if (msg->front.iov_len < sizeof(*h))
2698 goto bad;
2699 h = msg->front.iov_base;
2700 snaptrace = h + 1;
2701 op = le32_to_cpu(h->op);
2702 vino.ino = le64_to_cpu(h->ino);
2703 vino.snap = CEPH_NOSNAP;
2704 cap_id = le64_to_cpu(h->cap_id);
2705 seq = le32_to_cpu(h->seq);
2706 mseq = le32_to_cpu(h->migrate_seq);
2707 size = le64_to_cpu(h->size);
2708 max_size = le64_to_cpu(h->max_size);
2710 mutex_lock(&session->s_mutex);
2711 session->s_seq++;
2712 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2713 (unsigned)seq);
2715 /* lookup ino */
2716 inode = ceph_find_inode(sb, vino);
2717 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2718 vino.snap, inode);
2719 if (!inode) {
2720 dout(" i don't have ino %llx\n", vino.ino);
2722 if (op == CEPH_CAP_OP_IMPORT)
2723 __queue_cap_release(session, vino.ino, cap_id,
2724 mseq, seq);
2727 * send any full release message to try to move things
2728 * along for the mds (who clearly thinks we still have this
2729 * cap).
2731 ceph_add_cap_releases(mdsc, session);
2732 ceph_send_cap_releases(mdsc, session);
2733 goto done;
2736 /* these will work even if we don't have a cap yet */
2737 switch (op) {
2738 case CEPH_CAP_OP_FLUSHSNAP_ACK:
2739 handle_cap_flushsnap_ack(inode, tid, h, session);
2740 goto done;
2742 case CEPH_CAP_OP_EXPORT:
2743 handle_cap_export(inode, h, session);
2744 goto done;
2746 case CEPH_CAP_OP_IMPORT:
2747 handle_cap_import(mdsc, inode, h, session,
2748 snaptrace, le32_to_cpu(h->snap_trace_len));
2749 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY,
2750 session);
2751 goto done_unlocked;
2754 /* the rest require a cap */
2755 spin_lock(&inode->i_lock);
2756 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2757 if (!cap) {
2758 dout(" no cap on %p ino %llx.%llx from mds%d\n",
2759 inode, ceph_ino(inode), ceph_snap(inode), mds);
2760 spin_unlock(&inode->i_lock);
2761 goto done;
2764 /* note that each of these drops i_lock for us */
2765 switch (op) {
2766 case CEPH_CAP_OP_REVOKE:
2767 case CEPH_CAP_OP_GRANT:
2768 handle_cap_grant(inode, h, session, cap, msg->middle);
2769 goto done_unlocked;
2771 case CEPH_CAP_OP_FLUSH_ACK:
2772 handle_cap_flush_ack(inode, tid, h, session, cap);
2773 break;
2775 case CEPH_CAP_OP_TRUNC:
2776 handle_cap_trunc(inode, h, session);
2777 break;
2779 default:
2780 spin_unlock(&inode->i_lock);
2781 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2782 ceph_cap_op_name(op));
2785 done:
2786 mutex_unlock(&session->s_mutex);
2787 done_unlocked:
2788 if (inode)
2789 iput(inode);
2790 return;
2792 bad:
2793 pr_err("ceph_handle_caps: corrupt message\n");
2794 ceph_msg_dump(msg);
2795 return;
2799 * Delayed work handler to process end of delayed cap release LRU list.
2801 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
2803 struct ceph_inode_info *ci;
2804 int flags = CHECK_CAPS_NODELAY;
2806 dout("check_delayed_caps\n");
2807 while (1) {
2808 spin_lock(&mdsc->cap_delay_lock);
2809 if (list_empty(&mdsc->cap_delay_list))
2810 break;
2811 ci = list_first_entry(&mdsc->cap_delay_list,
2812 struct ceph_inode_info,
2813 i_cap_delay_list);
2814 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2815 time_before(jiffies, ci->i_hold_caps_max))
2816 break;
2817 list_del_init(&ci->i_cap_delay_list);
2818 spin_unlock(&mdsc->cap_delay_lock);
2819 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2820 ceph_check_caps(ci, flags, NULL);
2822 spin_unlock(&mdsc->cap_delay_lock);
2826 * Flush all dirty caps to the mds
2828 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2830 struct ceph_inode_info *ci, *nci = NULL;
2831 struct inode *inode, *ninode = NULL;
2832 struct list_head *p, *n;
2834 dout("flush_dirty_caps\n");
2835 spin_lock(&mdsc->cap_dirty_lock);
2836 list_for_each_safe(p, n, &mdsc->cap_dirty) {
2837 if (nci) {
2838 ci = nci;
2839 inode = ninode;
2840 ci->i_ceph_flags &= ~CEPH_I_NOFLUSH;
2841 dout("flush_dirty_caps inode %p (was next inode)\n",
2842 inode);
2843 } else {
2844 ci = list_entry(p, struct ceph_inode_info,
2845 i_dirty_item);
2846 inode = igrab(&ci->vfs_inode);
2847 BUG_ON(!inode);
2848 dout("flush_dirty_caps inode %p\n", inode);
2850 if (n != &mdsc->cap_dirty) {
2851 nci = list_entry(n, struct ceph_inode_info,
2852 i_dirty_item);
2853 ninode = igrab(&nci->vfs_inode);
2854 BUG_ON(!ninode);
2855 nci->i_ceph_flags |= CEPH_I_NOFLUSH;
2856 dout("flush_dirty_caps next inode %p, noflush\n",
2857 ninode);
2858 } else {
2859 nci = NULL;
2860 ninode = NULL;
2862 spin_unlock(&mdsc->cap_dirty_lock);
2863 if (inode) {
2864 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
2865 NULL);
2866 iput(inode);
2868 spin_lock(&mdsc->cap_dirty_lock);
2870 spin_unlock(&mdsc->cap_dirty_lock);
2874 * Drop open file reference. If we were the last open file,
2875 * we may need to release capabilities to the MDS (or schedule
2876 * their delayed release).
2878 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2880 struct inode *inode = &ci->vfs_inode;
2881 int last = 0;
2883 spin_lock(&inode->i_lock);
2884 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2885 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2886 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2887 if (--ci->i_nr_by_mode[fmode] == 0)
2888 last++;
2889 spin_unlock(&inode->i_lock);
2891 if (last && ci->i_vino.snap == CEPH_NOSNAP)
2892 ceph_check_caps(ci, 0, NULL);
2896 * Helpers for embedding cap and dentry lease releases into mds
2897 * requests.
2899 * @force is used by dentry_release (below) to force inclusion of a
2900 * record for the directory inode, even when there aren't any caps to
2901 * drop.
2903 int ceph_encode_inode_release(void **p, struct inode *inode,
2904 int mds, int drop, int unless, int force)
2906 struct ceph_inode_info *ci = ceph_inode(inode);
2907 struct ceph_cap *cap;
2908 struct ceph_mds_request_release *rel = *p;
2909 int used, dirty;
2910 int ret = 0;
2912 spin_lock(&inode->i_lock);
2913 used = __ceph_caps_used(ci);
2914 dirty = __ceph_caps_dirty(ci);
2916 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
2917 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
2918 ceph_cap_string(unless));
2920 /* only drop unused, clean caps */
2921 drop &= ~(used | dirty);
2923 cap = __get_cap_for_mds(ci, mds);
2924 if (cap && __cap_is_valid(cap)) {
2925 if (force ||
2926 ((cap->issued & drop) &&
2927 (cap->issued & unless) == 0)) {
2928 if ((cap->issued & drop) &&
2929 (cap->issued & unless) == 0) {
2930 dout("encode_inode_release %p cap %p %s -> "
2931 "%s\n", inode, cap,
2932 ceph_cap_string(cap->issued),
2933 ceph_cap_string(cap->issued & ~drop));
2934 cap->issued &= ~drop;
2935 cap->implemented &= ~drop;
2936 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
2937 int wanted = __ceph_caps_wanted(ci);
2938 dout(" wanted %s -> %s (act %s)\n",
2939 ceph_cap_string(cap->mds_wanted),
2940 ceph_cap_string(cap->mds_wanted &
2941 ~wanted),
2942 ceph_cap_string(wanted));
2943 cap->mds_wanted &= wanted;
2945 } else {
2946 dout("encode_inode_release %p cap %p %s"
2947 " (force)\n", inode, cap,
2948 ceph_cap_string(cap->issued));
2951 rel->ino = cpu_to_le64(ceph_ino(inode));
2952 rel->cap_id = cpu_to_le64(cap->cap_id);
2953 rel->seq = cpu_to_le32(cap->seq);
2954 rel->issue_seq = cpu_to_le32(cap->issue_seq),
2955 rel->mseq = cpu_to_le32(cap->mseq);
2956 rel->caps = cpu_to_le32(cap->issued);
2957 rel->wanted = cpu_to_le32(cap->mds_wanted);
2958 rel->dname_len = 0;
2959 rel->dname_seq = 0;
2960 *p += sizeof(*rel);
2961 ret = 1;
2962 } else {
2963 dout("encode_inode_release %p cap %p %s\n",
2964 inode, cap, ceph_cap_string(cap->issued));
2967 spin_unlock(&inode->i_lock);
2968 return ret;
2971 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
2972 int mds, int drop, int unless)
2974 struct inode *dir = dentry->d_parent->d_inode;
2975 struct ceph_mds_request_release *rel = *p;
2976 struct ceph_dentry_info *di = ceph_dentry(dentry);
2977 int force = 0;
2978 int ret;
2981 * force an record for the directory caps if we have a dentry lease.
2982 * this is racy (can't take i_lock and d_lock together), but it
2983 * doesn't have to be perfect; the mds will revoke anything we don't
2984 * release.
2986 spin_lock(&dentry->d_lock);
2987 if (di->lease_session && di->lease_session->s_mds == mds)
2988 force = 1;
2989 spin_unlock(&dentry->d_lock);
2991 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
2993 spin_lock(&dentry->d_lock);
2994 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
2995 dout("encode_dentry_release %p mds%d seq %d\n",
2996 dentry, mds, (int)di->lease_seq);
2997 rel->dname_len = cpu_to_le32(dentry->d_name.len);
2998 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
2999 *p += dentry->d_name.len;
3000 rel->dname_seq = cpu_to_le32(di->lease_seq);
3001 __ceph_mdsc_drop_dentry_lease(dentry);
3003 spin_unlock(&dentry->d_lock);
3004 return ret;