Merge tag 'xtensa-20180225' of git://github.com/jcmvbkbc/linux-xtensa
[cris-mirror.git] / fs / ceph / caps.c
blob6582c4507e6c9d1fdf2876c13c424f0c5830495c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/fs.h>
5 #include <linux/kernel.h>
6 #include <linux/sched/signal.h>
7 #include <linux/slab.h>
8 #include <linux/vmalloc.h>
9 #include <linux/wait.h>
10 #include <linux/writeback.h>
12 #include "super.h"
13 #include "mds_client.h"
14 #include "cache.h"
15 #include <linux/ceph/decode.h>
16 #include <linux/ceph/messenger.h>
19 * Capability management
21 * The Ceph metadata servers control client access to inode metadata
22 * and file data by issuing capabilities, granting clients permission
23 * to read and/or write both inode field and file data to OSDs
24 * (storage nodes). Each capability consists of a set of bits
25 * indicating which operations are allowed.
27 * If the client holds a *_SHARED cap, the client has a coherent value
28 * that can be safely read from the cached inode.
30 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
31 * client is allowed to change inode attributes (e.g., file size,
32 * mtime), note its dirty state in the ceph_cap, and asynchronously
33 * flush that metadata change to the MDS.
35 * In the event of a conflicting operation (perhaps by another
36 * client), the MDS will revoke the conflicting client capabilities.
38 * In order for a client to cache an inode, it must hold a capability
39 * with at least one MDS server. When inodes are released, release
40 * notifications are batched and periodically sent en masse to the MDS
41 * cluster to release server state.
44 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
45 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
46 struct ceph_mds_session *session,
47 struct ceph_inode_info *ci,
48 u64 oldest_flush_tid);
51 * Generate readable cap strings for debugging output.
53 #define MAX_CAP_STR 20
54 static char cap_str[MAX_CAP_STR][40];
55 static DEFINE_SPINLOCK(cap_str_lock);
56 static int last_cap_str;
58 static char *gcap_string(char *s, int c)
60 if (c & CEPH_CAP_GSHARED)
61 *s++ = 's';
62 if (c & CEPH_CAP_GEXCL)
63 *s++ = 'x';
64 if (c & CEPH_CAP_GCACHE)
65 *s++ = 'c';
66 if (c & CEPH_CAP_GRD)
67 *s++ = 'r';
68 if (c & CEPH_CAP_GWR)
69 *s++ = 'w';
70 if (c & CEPH_CAP_GBUFFER)
71 *s++ = 'b';
72 if (c & CEPH_CAP_GLAZYIO)
73 *s++ = 'l';
74 return s;
77 const char *ceph_cap_string(int caps)
79 int i;
80 char *s;
81 int c;
83 spin_lock(&cap_str_lock);
84 i = last_cap_str++;
85 if (last_cap_str == MAX_CAP_STR)
86 last_cap_str = 0;
87 spin_unlock(&cap_str_lock);
89 s = cap_str[i];
91 if (caps & CEPH_CAP_PIN)
92 *s++ = 'p';
94 c = (caps >> CEPH_CAP_SAUTH) & 3;
95 if (c) {
96 *s++ = 'A';
97 s = gcap_string(s, c);
100 c = (caps >> CEPH_CAP_SLINK) & 3;
101 if (c) {
102 *s++ = 'L';
103 s = gcap_string(s, c);
106 c = (caps >> CEPH_CAP_SXATTR) & 3;
107 if (c) {
108 *s++ = 'X';
109 s = gcap_string(s, c);
112 c = caps >> CEPH_CAP_SFILE;
113 if (c) {
114 *s++ = 'F';
115 s = gcap_string(s, c);
118 if (s == cap_str[i])
119 *s++ = '-';
120 *s = 0;
121 return cap_str[i];
124 void ceph_caps_init(struct ceph_mds_client *mdsc)
126 INIT_LIST_HEAD(&mdsc->caps_list);
127 spin_lock_init(&mdsc->caps_list_lock);
130 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
132 struct ceph_cap *cap;
134 spin_lock(&mdsc->caps_list_lock);
135 while (!list_empty(&mdsc->caps_list)) {
136 cap = list_first_entry(&mdsc->caps_list,
137 struct ceph_cap, caps_item);
138 list_del(&cap->caps_item);
139 kmem_cache_free(ceph_cap_cachep, cap);
141 mdsc->caps_total_count = 0;
142 mdsc->caps_avail_count = 0;
143 mdsc->caps_use_count = 0;
144 mdsc->caps_reserve_count = 0;
145 mdsc->caps_min_count = 0;
146 spin_unlock(&mdsc->caps_list_lock);
149 void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
151 spin_lock(&mdsc->caps_list_lock);
152 mdsc->caps_min_count += delta;
153 BUG_ON(mdsc->caps_min_count < 0);
154 spin_unlock(&mdsc->caps_list_lock);
158 * Called under mdsc->mutex.
160 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
161 struct ceph_cap_reservation *ctx, int need)
163 int i, j;
164 struct ceph_cap *cap;
165 int have;
166 int alloc = 0;
167 int max_caps;
168 bool trimmed = false;
169 struct ceph_mds_session *s;
170 LIST_HEAD(newcaps);
172 dout("reserve caps ctx=%p need=%d\n", ctx, need);
174 /* first reserve any caps that are already allocated */
175 spin_lock(&mdsc->caps_list_lock);
176 if (mdsc->caps_avail_count >= need)
177 have = need;
178 else
179 have = mdsc->caps_avail_count;
180 mdsc->caps_avail_count -= have;
181 mdsc->caps_reserve_count += have;
182 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
183 mdsc->caps_reserve_count +
184 mdsc->caps_avail_count);
185 spin_unlock(&mdsc->caps_list_lock);
187 for (i = have; i < need; i++) {
188 retry:
189 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
190 if (!cap) {
191 if (!trimmed) {
192 for (j = 0; j < mdsc->max_sessions; j++) {
193 s = __ceph_lookup_mds_session(mdsc, j);
194 if (!s)
195 continue;
196 mutex_unlock(&mdsc->mutex);
198 mutex_lock(&s->s_mutex);
199 max_caps = s->s_nr_caps - (need - i);
200 ceph_trim_caps(mdsc, s, max_caps);
201 mutex_unlock(&s->s_mutex);
203 ceph_put_mds_session(s);
204 mutex_lock(&mdsc->mutex);
206 trimmed = true;
207 goto retry;
208 } else {
209 pr_warn("reserve caps ctx=%p ENOMEM "
210 "need=%d got=%d\n",
211 ctx, need, have + alloc);
212 goto out_nomem;
215 list_add(&cap->caps_item, &newcaps);
216 alloc++;
218 BUG_ON(have + alloc != need);
220 spin_lock(&mdsc->caps_list_lock);
221 mdsc->caps_total_count += alloc;
222 mdsc->caps_reserve_count += alloc;
223 list_splice(&newcaps, &mdsc->caps_list);
225 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
226 mdsc->caps_reserve_count +
227 mdsc->caps_avail_count);
228 spin_unlock(&mdsc->caps_list_lock);
230 ctx->count = need;
231 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
232 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
233 mdsc->caps_reserve_count, mdsc->caps_avail_count);
234 return 0;
236 out_nomem:
237 while (!list_empty(&newcaps)) {
238 cap = list_first_entry(&newcaps,
239 struct ceph_cap, caps_item);
240 list_del(&cap->caps_item);
241 kmem_cache_free(ceph_cap_cachep, cap);
244 spin_lock(&mdsc->caps_list_lock);
245 mdsc->caps_avail_count += have;
246 mdsc->caps_reserve_count -= have;
247 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
248 mdsc->caps_reserve_count +
249 mdsc->caps_avail_count);
250 spin_unlock(&mdsc->caps_list_lock);
251 return -ENOMEM;
254 int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
255 struct ceph_cap_reservation *ctx)
257 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
258 if (ctx->count) {
259 spin_lock(&mdsc->caps_list_lock);
260 BUG_ON(mdsc->caps_reserve_count < ctx->count);
261 mdsc->caps_reserve_count -= ctx->count;
262 mdsc->caps_avail_count += ctx->count;
263 ctx->count = 0;
264 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
265 mdsc->caps_total_count, mdsc->caps_use_count,
266 mdsc->caps_reserve_count, mdsc->caps_avail_count);
267 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
268 mdsc->caps_reserve_count +
269 mdsc->caps_avail_count);
270 spin_unlock(&mdsc->caps_list_lock);
272 return 0;
275 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
276 struct ceph_cap_reservation *ctx)
278 struct ceph_cap *cap = NULL;
280 /* temporary, until we do something about cap import/export */
281 if (!ctx) {
282 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
283 if (cap) {
284 spin_lock(&mdsc->caps_list_lock);
285 mdsc->caps_use_count++;
286 mdsc->caps_total_count++;
287 spin_unlock(&mdsc->caps_list_lock);
289 return cap;
292 spin_lock(&mdsc->caps_list_lock);
293 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
294 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
295 mdsc->caps_reserve_count, mdsc->caps_avail_count);
296 BUG_ON(!ctx->count);
297 BUG_ON(ctx->count > mdsc->caps_reserve_count);
298 BUG_ON(list_empty(&mdsc->caps_list));
300 ctx->count--;
301 mdsc->caps_reserve_count--;
302 mdsc->caps_use_count++;
304 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
305 list_del(&cap->caps_item);
307 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
308 mdsc->caps_reserve_count + mdsc->caps_avail_count);
309 spin_unlock(&mdsc->caps_list_lock);
310 return cap;
313 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
315 spin_lock(&mdsc->caps_list_lock);
316 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
317 cap, mdsc->caps_total_count, mdsc->caps_use_count,
318 mdsc->caps_reserve_count, mdsc->caps_avail_count);
319 mdsc->caps_use_count--;
321 * Keep some preallocated caps around (ceph_min_count), to
322 * avoid lots of free/alloc churn.
324 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
325 mdsc->caps_min_count) {
326 mdsc->caps_total_count--;
327 kmem_cache_free(ceph_cap_cachep, cap);
328 } else {
329 mdsc->caps_avail_count++;
330 list_add(&cap->caps_item, &mdsc->caps_list);
333 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
334 mdsc->caps_reserve_count + mdsc->caps_avail_count);
335 spin_unlock(&mdsc->caps_list_lock);
338 void ceph_reservation_status(struct ceph_fs_client *fsc,
339 int *total, int *avail, int *used, int *reserved,
340 int *min)
342 struct ceph_mds_client *mdsc = fsc->mdsc;
344 if (total)
345 *total = mdsc->caps_total_count;
346 if (avail)
347 *avail = mdsc->caps_avail_count;
348 if (used)
349 *used = mdsc->caps_use_count;
350 if (reserved)
351 *reserved = mdsc->caps_reserve_count;
352 if (min)
353 *min = mdsc->caps_min_count;
357 * Find ceph_cap for given mds, if any.
359 * Called with i_ceph_lock held.
361 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
363 struct ceph_cap *cap;
364 struct rb_node *n = ci->i_caps.rb_node;
366 while (n) {
367 cap = rb_entry(n, struct ceph_cap, ci_node);
368 if (mds < cap->mds)
369 n = n->rb_left;
370 else if (mds > cap->mds)
371 n = n->rb_right;
372 else
373 return cap;
375 return NULL;
378 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
380 struct ceph_cap *cap;
382 spin_lock(&ci->i_ceph_lock);
383 cap = __get_cap_for_mds(ci, mds);
384 spin_unlock(&ci->i_ceph_lock);
385 return cap;
389 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
391 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
393 struct ceph_cap *cap;
394 int mds = -1;
395 struct rb_node *p;
397 /* prefer mds with WR|BUFFER|EXCL caps */
398 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
399 cap = rb_entry(p, struct ceph_cap, ci_node);
400 mds = cap->mds;
401 if (cap->issued & (CEPH_CAP_FILE_WR |
402 CEPH_CAP_FILE_BUFFER |
403 CEPH_CAP_FILE_EXCL))
404 break;
406 return mds;
409 int ceph_get_cap_mds(struct inode *inode)
411 struct ceph_inode_info *ci = ceph_inode(inode);
412 int mds;
413 spin_lock(&ci->i_ceph_lock);
414 mds = __ceph_get_cap_mds(ceph_inode(inode));
415 spin_unlock(&ci->i_ceph_lock);
416 return mds;
420 * Called under i_ceph_lock.
422 static void __insert_cap_node(struct ceph_inode_info *ci,
423 struct ceph_cap *new)
425 struct rb_node **p = &ci->i_caps.rb_node;
426 struct rb_node *parent = NULL;
427 struct ceph_cap *cap = NULL;
429 while (*p) {
430 parent = *p;
431 cap = rb_entry(parent, struct ceph_cap, ci_node);
432 if (new->mds < cap->mds)
433 p = &(*p)->rb_left;
434 else if (new->mds > cap->mds)
435 p = &(*p)->rb_right;
436 else
437 BUG();
440 rb_link_node(&new->ci_node, parent, p);
441 rb_insert_color(&new->ci_node, &ci->i_caps);
445 * (re)set cap hold timeouts, which control the delayed release
446 * of unused caps back to the MDS. Should be called on cap use.
448 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
449 struct ceph_inode_info *ci)
451 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
453 ci->i_hold_caps_min = round_jiffies(jiffies +
454 ma->caps_wanted_delay_min * HZ);
455 ci->i_hold_caps_max = round_jiffies(jiffies +
456 ma->caps_wanted_delay_max * HZ);
457 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
458 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
462 * (Re)queue cap at the end of the delayed cap release list.
464 * If I_FLUSH is set, leave the inode at the front of the list.
466 * Caller holds i_ceph_lock
467 * -> we take mdsc->cap_delay_lock
469 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
470 struct ceph_inode_info *ci)
472 __cap_set_timeouts(mdsc, ci);
473 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
474 ci->i_ceph_flags, ci->i_hold_caps_max);
475 if (!mdsc->stopping) {
476 spin_lock(&mdsc->cap_delay_lock);
477 if (!list_empty(&ci->i_cap_delay_list)) {
478 if (ci->i_ceph_flags & CEPH_I_FLUSH)
479 goto no_change;
480 list_del_init(&ci->i_cap_delay_list);
482 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
483 no_change:
484 spin_unlock(&mdsc->cap_delay_lock);
489 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
490 * indicating we should send a cap message to flush dirty metadata
491 * asap, and move to the front of the delayed cap list.
493 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
494 struct ceph_inode_info *ci)
496 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
497 spin_lock(&mdsc->cap_delay_lock);
498 ci->i_ceph_flags |= CEPH_I_FLUSH;
499 if (!list_empty(&ci->i_cap_delay_list))
500 list_del_init(&ci->i_cap_delay_list);
501 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
502 spin_unlock(&mdsc->cap_delay_lock);
506 * Cancel delayed work on cap.
508 * Caller must hold i_ceph_lock.
510 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
511 struct ceph_inode_info *ci)
513 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
514 if (list_empty(&ci->i_cap_delay_list))
515 return;
516 spin_lock(&mdsc->cap_delay_lock);
517 list_del_init(&ci->i_cap_delay_list);
518 spin_unlock(&mdsc->cap_delay_lock);
522 * Common issue checks for add_cap, handle_cap_grant.
524 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
525 unsigned issued)
527 unsigned had = __ceph_caps_issued(ci, NULL);
530 * Each time we receive FILE_CACHE anew, we increment
531 * i_rdcache_gen.
533 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
534 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
535 ci->i_rdcache_gen++;
539 * If FILE_SHARED is newly issued, mark dir not complete. We don't
540 * know what happened to this directory while we didn't have the cap.
541 * If FILE_SHARED is being revoked, also mark dir not complete. It
542 * stops on-going cached readdir.
544 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
545 if (issued & CEPH_CAP_FILE_SHARED)
546 atomic_inc(&ci->i_shared_gen);
547 if (S_ISDIR(ci->vfs_inode.i_mode)) {
548 dout(" marking %p NOT complete\n", &ci->vfs_inode);
549 __ceph_dir_clear_complete(ci);
555 * Add a capability under the given MDS session.
557 * Caller should hold session snap_rwsem (read) and s_mutex.
559 * @fmode is the open file mode, if we are opening a file, otherwise
560 * it is < 0. (This is so we can atomically add the cap and add an
561 * open file reference to it.)
563 void ceph_add_cap(struct inode *inode,
564 struct ceph_mds_session *session, u64 cap_id,
565 int fmode, unsigned issued, unsigned wanted,
566 unsigned seq, unsigned mseq, u64 realmino, int flags,
567 struct ceph_cap **new_cap)
569 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
570 struct ceph_inode_info *ci = ceph_inode(inode);
571 struct ceph_cap *cap;
572 int mds = session->s_mds;
573 int actual_wanted;
575 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
576 session->s_mds, cap_id, ceph_cap_string(issued), seq);
579 * If we are opening the file, include file mode wanted bits
580 * in wanted.
582 if (fmode >= 0)
583 wanted |= ceph_caps_for_mode(fmode);
585 cap = __get_cap_for_mds(ci, mds);
586 if (!cap) {
587 cap = *new_cap;
588 *new_cap = NULL;
590 cap->issued = 0;
591 cap->implemented = 0;
592 cap->mds = mds;
593 cap->mds_wanted = 0;
594 cap->mseq = 0;
596 cap->ci = ci;
597 __insert_cap_node(ci, cap);
599 /* add to session cap list */
600 cap->session = session;
601 spin_lock(&session->s_cap_lock);
602 list_add_tail(&cap->session_caps, &session->s_caps);
603 session->s_nr_caps++;
604 spin_unlock(&session->s_cap_lock);
605 } else {
607 * auth mds of the inode changed. we received the cap export
608 * message, but still haven't received the cap import message.
609 * handle_cap_export() updated the new auth MDS' cap.
611 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
612 * a message that was send before the cap import message. So
613 * don't remove caps.
615 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
616 WARN_ON(cap != ci->i_auth_cap);
617 WARN_ON(cap->cap_id != cap_id);
618 seq = cap->seq;
619 mseq = cap->mseq;
620 issued |= cap->issued;
621 flags |= CEPH_CAP_FLAG_AUTH;
625 if (!ci->i_snap_realm ||
626 ((flags & CEPH_CAP_FLAG_AUTH) &&
627 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
629 * add this inode to the appropriate snap realm
631 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
632 realmino);
633 if (realm) {
634 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
635 if (oldrealm) {
636 spin_lock(&oldrealm->inodes_with_caps_lock);
637 list_del_init(&ci->i_snap_realm_item);
638 spin_unlock(&oldrealm->inodes_with_caps_lock);
641 spin_lock(&realm->inodes_with_caps_lock);
642 ci->i_snap_realm = realm;
643 list_add(&ci->i_snap_realm_item,
644 &realm->inodes_with_caps);
645 spin_unlock(&realm->inodes_with_caps_lock);
647 if (oldrealm)
648 ceph_put_snap_realm(mdsc, oldrealm);
649 } else {
650 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
651 realmino);
652 WARN_ON(!realm);
656 __check_cap_issue(ci, cap, issued);
659 * If we are issued caps we don't want, or the mds' wanted
660 * value appears to be off, queue a check so we'll release
661 * later and/or update the mds wanted value.
663 actual_wanted = __ceph_caps_wanted(ci);
664 if ((wanted & ~actual_wanted) ||
665 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
666 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
667 ceph_cap_string(issued), ceph_cap_string(wanted),
668 ceph_cap_string(actual_wanted));
669 __cap_delay_requeue(mdsc, ci);
672 if (flags & CEPH_CAP_FLAG_AUTH) {
673 if (!ci->i_auth_cap ||
674 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
675 ci->i_auth_cap = cap;
676 cap->mds_wanted = wanted;
678 } else {
679 WARN_ON(ci->i_auth_cap == cap);
682 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
683 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
684 ceph_cap_string(issued|cap->issued), seq, mds);
685 cap->cap_id = cap_id;
686 cap->issued = issued;
687 cap->implemented |= issued;
688 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
689 cap->mds_wanted = wanted;
690 else
691 cap->mds_wanted |= wanted;
692 cap->seq = seq;
693 cap->issue_seq = seq;
694 cap->mseq = mseq;
695 cap->cap_gen = session->s_cap_gen;
697 if (fmode >= 0)
698 __ceph_get_fmode(ci, fmode);
702 * Return true if cap has not timed out and belongs to the current
703 * generation of the MDS session (i.e. has not gone 'stale' due to
704 * us losing touch with the mds).
706 static int __cap_is_valid(struct ceph_cap *cap)
708 unsigned long ttl;
709 u32 gen;
711 spin_lock(&cap->session->s_gen_ttl_lock);
712 gen = cap->session->s_cap_gen;
713 ttl = cap->session->s_cap_ttl;
714 spin_unlock(&cap->session->s_gen_ttl_lock);
716 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
717 dout("__cap_is_valid %p cap %p issued %s "
718 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
719 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
720 return 0;
723 return 1;
727 * Return set of valid cap bits issued to us. Note that caps time
728 * out, and may be invalidated in bulk if the client session times out
729 * and session->s_cap_gen is bumped.
731 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
733 int have = ci->i_snap_caps;
734 struct ceph_cap *cap;
735 struct rb_node *p;
737 if (implemented)
738 *implemented = 0;
739 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
740 cap = rb_entry(p, struct ceph_cap, ci_node);
741 if (!__cap_is_valid(cap))
742 continue;
743 dout("__ceph_caps_issued %p cap %p issued %s\n",
744 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
745 have |= cap->issued;
746 if (implemented)
747 *implemented |= cap->implemented;
750 * exclude caps issued by non-auth MDS, but are been revoking
751 * by the auth MDS. The non-auth MDS should be revoking/exporting
752 * these caps, but the message is delayed.
754 if (ci->i_auth_cap) {
755 cap = ci->i_auth_cap;
756 have &= ~cap->implemented | cap->issued;
758 return have;
762 * Get cap bits issued by caps other than @ocap
764 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
766 int have = ci->i_snap_caps;
767 struct ceph_cap *cap;
768 struct rb_node *p;
770 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
771 cap = rb_entry(p, struct ceph_cap, ci_node);
772 if (cap == ocap)
773 continue;
774 if (!__cap_is_valid(cap))
775 continue;
776 have |= cap->issued;
778 return have;
782 * Move a cap to the end of the LRU (oldest caps at list head, newest
783 * at list tail).
785 static void __touch_cap(struct ceph_cap *cap)
787 struct ceph_mds_session *s = cap->session;
789 spin_lock(&s->s_cap_lock);
790 if (!s->s_cap_iterator) {
791 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
792 s->s_mds);
793 list_move_tail(&cap->session_caps, &s->s_caps);
794 } else {
795 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
796 &cap->ci->vfs_inode, cap, s->s_mds);
798 spin_unlock(&s->s_cap_lock);
802 * Check if we hold the given mask. If so, move the cap(s) to the
803 * front of their respective LRUs. (This is the preferred way for
804 * callers to check for caps they want.)
806 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
808 struct ceph_cap *cap;
809 struct rb_node *p;
810 int have = ci->i_snap_caps;
812 if ((have & mask) == mask) {
813 dout("__ceph_caps_issued_mask %p snap issued %s"
814 " (mask %s)\n", &ci->vfs_inode,
815 ceph_cap_string(have),
816 ceph_cap_string(mask));
817 return 1;
820 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
821 cap = rb_entry(p, struct ceph_cap, ci_node);
822 if (!__cap_is_valid(cap))
823 continue;
824 if ((cap->issued & mask) == mask) {
825 dout("__ceph_caps_issued_mask %p cap %p issued %s"
826 " (mask %s)\n", &ci->vfs_inode, cap,
827 ceph_cap_string(cap->issued),
828 ceph_cap_string(mask));
829 if (touch)
830 __touch_cap(cap);
831 return 1;
834 /* does a combination of caps satisfy mask? */
835 have |= cap->issued;
836 if ((have & mask) == mask) {
837 dout("__ceph_caps_issued_mask %p combo issued %s"
838 " (mask %s)\n", &ci->vfs_inode,
839 ceph_cap_string(cap->issued),
840 ceph_cap_string(mask));
841 if (touch) {
842 struct rb_node *q;
844 /* touch this + preceding caps */
845 __touch_cap(cap);
846 for (q = rb_first(&ci->i_caps); q != p;
847 q = rb_next(q)) {
848 cap = rb_entry(q, struct ceph_cap,
849 ci_node);
850 if (!__cap_is_valid(cap))
851 continue;
852 __touch_cap(cap);
855 return 1;
859 return 0;
863 * Return true if mask caps are currently being revoked by an MDS.
865 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
866 struct ceph_cap *ocap, int mask)
868 struct ceph_cap *cap;
869 struct rb_node *p;
871 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
872 cap = rb_entry(p, struct ceph_cap, ci_node);
873 if (cap != ocap &&
874 (cap->implemented & ~cap->issued & mask))
875 return 1;
877 return 0;
880 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
882 struct inode *inode = &ci->vfs_inode;
883 int ret;
885 spin_lock(&ci->i_ceph_lock);
886 ret = __ceph_caps_revoking_other(ci, NULL, mask);
887 spin_unlock(&ci->i_ceph_lock);
888 dout("ceph_caps_revoking %p %s = %d\n", inode,
889 ceph_cap_string(mask), ret);
890 return ret;
893 int __ceph_caps_used(struct ceph_inode_info *ci)
895 int used = 0;
896 if (ci->i_pin_ref)
897 used |= CEPH_CAP_PIN;
898 if (ci->i_rd_ref)
899 used |= CEPH_CAP_FILE_RD;
900 if (ci->i_rdcache_ref ||
901 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
902 ci->vfs_inode.i_data.nrpages))
903 used |= CEPH_CAP_FILE_CACHE;
904 if (ci->i_wr_ref)
905 used |= CEPH_CAP_FILE_WR;
906 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
907 used |= CEPH_CAP_FILE_BUFFER;
908 return used;
912 * wanted, by virtue of open file modes
914 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
916 int i, bits = 0;
917 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
918 if (ci->i_nr_by_mode[i])
919 bits |= 1 << i;
921 if (bits == 0)
922 return 0;
923 return ceph_caps_for_mode(bits >> 1);
927 * Return caps we have registered with the MDS(s) as 'wanted'.
929 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
931 struct ceph_cap *cap;
932 struct rb_node *p;
933 int mds_wanted = 0;
935 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
936 cap = rb_entry(p, struct ceph_cap, ci_node);
937 if (check && !__cap_is_valid(cap))
938 continue;
939 if (cap == ci->i_auth_cap)
940 mds_wanted |= cap->mds_wanted;
941 else
942 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
944 return mds_wanted;
948 * called under i_ceph_lock
950 static int __ceph_is_single_caps(struct ceph_inode_info *ci)
952 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
955 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
957 return !RB_EMPTY_ROOT(&ci->i_caps);
960 int ceph_is_any_caps(struct inode *inode)
962 struct ceph_inode_info *ci = ceph_inode(inode);
963 int ret;
965 spin_lock(&ci->i_ceph_lock);
966 ret = __ceph_is_any_caps(ci);
967 spin_unlock(&ci->i_ceph_lock);
969 return ret;
972 static void drop_inode_snap_realm(struct ceph_inode_info *ci)
974 struct ceph_snap_realm *realm = ci->i_snap_realm;
975 spin_lock(&realm->inodes_with_caps_lock);
976 list_del_init(&ci->i_snap_realm_item);
977 ci->i_snap_realm_counter++;
978 ci->i_snap_realm = NULL;
979 spin_unlock(&realm->inodes_with_caps_lock);
980 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
981 realm);
985 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
987 * caller should hold i_ceph_lock.
988 * caller will not hold session s_mutex if called from destroy_inode.
990 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
992 struct ceph_mds_session *session = cap->session;
993 struct ceph_inode_info *ci = cap->ci;
994 struct ceph_mds_client *mdsc =
995 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
996 int removed = 0;
998 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
1000 /* remove from session list */
1001 spin_lock(&session->s_cap_lock);
1002 if (session->s_cap_iterator == cap) {
1003 /* not yet, we are iterating over this very cap */
1004 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1005 cap, cap->session);
1006 } else {
1007 list_del_init(&cap->session_caps);
1008 session->s_nr_caps--;
1009 cap->session = NULL;
1010 removed = 1;
1012 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1013 cap->ci = NULL;
1016 * s_cap_reconnect is protected by s_cap_lock. no one changes
1017 * s_cap_gen while session is in the reconnect state.
1019 if (queue_release &&
1020 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1021 cap->queue_release = 1;
1022 if (removed) {
1023 list_add_tail(&cap->session_caps,
1024 &session->s_cap_releases);
1025 session->s_num_cap_releases++;
1026 removed = 0;
1028 } else {
1029 cap->queue_release = 0;
1031 cap->cap_ino = ci->i_vino.ino;
1033 spin_unlock(&session->s_cap_lock);
1035 /* remove from inode list */
1036 rb_erase(&cap->ci_node, &ci->i_caps);
1037 if (ci->i_auth_cap == cap)
1038 ci->i_auth_cap = NULL;
1040 if (removed)
1041 ceph_put_cap(mdsc, cap);
1043 /* when reconnect denied, we remove session caps forcibly,
1044 * i_wr_ref can be non-zero. If there are ongoing write,
1045 * keep i_snap_realm.
1047 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1048 drop_inode_snap_realm(ci);
1050 if (!__ceph_is_any_real_caps(ci))
1051 __cap_delay_cancel(mdsc, ci);
1054 struct cap_msg_args {
1055 struct ceph_mds_session *session;
1056 u64 ino, cid, follows;
1057 u64 flush_tid, oldest_flush_tid, size, max_size;
1058 u64 xattr_version;
1059 struct ceph_buffer *xattr_buf;
1060 struct timespec atime, mtime, ctime;
1061 int op, caps, wanted, dirty;
1062 u32 seq, issue_seq, mseq, time_warp_seq;
1063 u32 flags;
1064 kuid_t uid;
1065 kgid_t gid;
1066 umode_t mode;
1067 bool inline_data;
1071 * Build and send a cap message to the given MDS.
1073 * Caller should be holding s_mutex.
1075 static int send_cap_msg(struct cap_msg_args *arg)
1077 struct ceph_mds_caps *fc;
1078 struct ceph_msg *msg;
1079 void *p;
1080 size_t extra_len;
1081 struct timespec zerotime = {0};
1082 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1084 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
1085 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1086 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1087 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1088 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1089 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1090 arg->mseq, arg->follows, arg->size, arg->max_size,
1091 arg->xattr_version,
1092 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1094 /* flock buffer size + inline version + inline data size +
1095 * osd_epoch_barrier + oldest_flush_tid */
1096 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
1097 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1098 GFP_NOFS, false);
1099 if (!msg)
1100 return -ENOMEM;
1102 msg->hdr.version = cpu_to_le16(10);
1103 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1105 fc = msg->front.iov_base;
1106 memset(fc, 0, sizeof(*fc));
1108 fc->cap_id = cpu_to_le64(arg->cid);
1109 fc->op = cpu_to_le32(arg->op);
1110 fc->seq = cpu_to_le32(arg->seq);
1111 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1112 fc->migrate_seq = cpu_to_le32(arg->mseq);
1113 fc->caps = cpu_to_le32(arg->caps);
1114 fc->wanted = cpu_to_le32(arg->wanted);
1115 fc->dirty = cpu_to_le32(arg->dirty);
1116 fc->ino = cpu_to_le64(arg->ino);
1117 fc->snap_follows = cpu_to_le64(arg->follows);
1119 fc->size = cpu_to_le64(arg->size);
1120 fc->max_size = cpu_to_le64(arg->max_size);
1121 ceph_encode_timespec(&fc->mtime, &arg->mtime);
1122 ceph_encode_timespec(&fc->atime, &arg->atime);
1123 ceph_encode_timespec(&fc->ctime, &arg->ctime);
1124 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1126 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1127 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1128 fc->mode = cpu_to_le32(arg->mode);
1130 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1131 if (arg->xattr_buf) {
1132 msg->middle = ceph_buffer_get(arg->xattr_buf);
1133 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1134 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1137 p = fc + 1;
1138 /* flock buffer size (version 2) */
1139 ceph_encode_32(&p, 0);
1140 /* inline version (version 4) */
1141 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1142 /* inline data size */
1143 ceph_encode_32(&p, 0);
1145 * osd_epoch_barrier (version 5)
1146 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1147 * case it was recently changed
1149 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1150 /* oldest_flush_tid (version 6) */
1151 ceph_encode_64(&p, arg->oldest_flush_tid);
1154 * caller_uid/caller_gid (version 7)
1156 * Currently, we don't properly track which caller dirtied the caps
1157 * last, and force a flush of them when there is a conflict. For now,
1158 * just set this to 0:0, to emulate how the MDS has worked up to now.
1160 ceph_encode_32(&p, 0);
1161 ceph_encode_32(&p, 0);
1163 /* pool namespace (version 8) (mds always ignores this) */
1164 ceph_encode_32(&p, 0);
1167 * btime and change_attr (version 9)
1169 * We just zero these out for now, as the MDS ignores them unless
1170 * the requisite feature flags are set (which we don't do yet).
1172 ceph_encode_timespec(p, &zerotime);
1173 p += sizeof(struct ceph_timespec);
1174 ceph_encode_64(&p, 0);
1176 /* Advisory flags (version 10) */
1177 ceph_encode_32(&p, arg->flags);
1179 ceph_con_send(&arg->session->s_con, msg);
1180 return 0;
1184 * Queue cap releases when an inode is dropped from our cache. Since
1185 * inode is about to be destroyed, there is no need for i_ceph_lock.
1187 void ceph_queue_caps_release(struct inode *inode)
1189 struct ceph_inode_info *ci = ceph_inode(inode);
1190 struct rb_node *p;
1192 p = rb_first(&ci->i_caps);
1193 while (p) {
1194 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1195 p = rb_next(p);
1196 __ceph_remove_cap(cap, true);
1201 * Send a cap msg on the given inode. Update our caps state, then
1202 * drop i_ceph_lock and send the message.
1204 * Make note of max_size reported/requested from mds, revoked caps
1205 * that have now been implemented.
1207 * Make half-hearted attempt ot to invalidate page cache if we are
1208 * dropping RDCACHE. Note that this will leave behind locked pages
1209 * that we'll then need to deal with elsewhere.
1211 * Return non-zero if delayed release, or we experienced an error
1212 * such that the caller should requeue + retry later.
1214 * called with i_ceph_lock, then drops it.
1215 * caller should hold snap_rwsem (read), s_mutex.
1217 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1218 int op, bool sync, int used, int want, int retain,
1219 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1220 __releases(cap->ci->i_ceph_lock)
1222 struct ceph_inode_info *ci = cap->ci;
1223 struct inode *inode = &ci->vfs_inode;
1224 struct cap_msg_args arg;
1225 int held, revoking;
1226 int wake = 0;
1227 int delayed = 0;
1228 int ret;
1230 held = cap->issued | cap->implemented;
1231 revoking = cap->implemented & ~cap->issued;
1232 retain &= ~revoking;
1234 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1235 inode, cap, cap->session,
1236 ceph_cap_string(held), ceph_cap_string(held & retain),
1237 ceph_cap_string(revoking));
1238 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1240 arg.session = cap->session;
1242 /* don't release wanted unless we've waited a bit. */
1243 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1244 time_before(jiffies, ci->i_hold_caps_min)) {
1245 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1246 ceph_cap_string(cap->issued),
1247 ceph_cap_string(cap->issued & retain),
1248 ceph_cap_string(cap->mds_wanted),
1249 ceph_cap_string(want));
1250 want |= cap->mds_wanted;
1251 retain |= cap->issued;
1252 delayed = 1;
1254 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1255 if (want & ~cap->mds_wanted) {
1256 /* user space may open/close single file frequently.
1257 * This avoids droping mds_wanted immediately after
1258 * requesting new mds_wanted.
1260 __cap_set_timeouts(mdsc, ci);
1263 cap->issued &= retain; /* drop bits we don't want */
1264 if (cap->implemented & ~cap->issued) {
1266 * Wake up any waiters on wanted -> needed transition.
1267 * This is due to the weird transition from buffered
1268 * to sync IO... we need to flush dirty pages _before_
1269 * allowing sync writes to avoid reordering.
1271 wake = 1;
1273 cap->implemented &= cap->issued | used;
1274 cap->mds_wanted = want;
1276 arg.ino = ceph_vino(inode).ino;
1277 arg.cid = cap->cap_id;
1278 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1279 arg.flush_tid = flush_tid;
1280 arg.oldest_flush_tid = oldest_flush_tid;
1282 arg.size = inode->i_size;
1283 ci->i_reported_size = arg.size;
1284 arg.max_size = ci->i_wanted_max_size;
1285 ci->i_requested_max_size = arg.max_size;
1287 if (flushing & CEPH_CAP_XATTR_EXCL) {
1288 __ceph_build_xattrs_blob(ci);
1289 arg.xattr_version = ci->i_xattrs.version;
1290 arg.xattr_buf = ci->i_xattrs.blob;
1291 } else {
1292 arg.xattr_buf = NULL;
1295 arg.mtime = inode->i_mtime;
1296 arg.atime = inode->i_atime;
1297 arg.ctime = inode->i_ctime;
1299 arg.op = op;
1300 arg.caps = cap->implemented;
1301 arg.wanted = want;
1302 arg.dirty = flushing;
1304 arg.seq = cap->seq;
1305 arg.issue_seq = cap->issue_seq;
1306 arg.mseq = cap->mseq;
1307 arg.time_warp_seq = ci->i_time_warp_seq;
1309 arg.uid = inode->i_uid;
1310 arg.gid = inode->i_gid;
1311 arg.mode = inode->i_mode;
1313 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1314 if (list_empty(&ci->i_cap_snaps))
1315 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1316 else
1317 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1318 if (sync)
1319 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
1321 spin_unlock(&ci->i_ceph_lock);
1323 ret = send_cap_msg(&arg);
1324 if (ret < 0) {
1325 dout("error sending cap msg, must requeue %p\n", inode);
1326 delayed = 1;
1329 if (wake)
1330 wake_up_all(&ci->i_cap_wq);
1332 return delayed;
1335 static inline int __send_flush_snap(struct inode *inode,
1336 struct ceph_mds_session *session,
1337 struct ceph_cap_snap *capsnap,
1338 u32 mseq, u64 oldest_flush_tid)
1340 struct cap_msg_args arg;
1342 arg.session = session;
1343 arg.ino = ceph_vino(inode).ino;
1344 arg.cid = 0;
1345 arg.follows = capsnap->follows;
1346 arg.flush_tid = capsnap->cap_flush.tid;
1347 arg.oldest_flush_tid = oldest_flush_tid;
1349 arg.size = capsnap->size;
1350 arg.max_size = 0;
1351 arg.xattr_version = capsnap->xattr_version;
1352 arg.xattr_buf = capsnap->xattr_blob;
1354 arg.atime = capsnap->atime;
1355 arg.mtime = capsnap->mtime;
1356 arg.ctime = capsnap->ctime;
1358 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1359 arg.caps = capsnap->issued;
1360 arg.wanted = 0;
1361 arg.dirty = capsnap->dirty;
1363 arg.seq = 0;
1364 arg.issue_seq = 0;
1365 arg.mseq = mseq;
1366 arg.time_warp_seq = capsnap->time_warp_seq;
1368 arg.uid = capsnap->uid;
1369 arg.gid = capsnap->gid;
1370 arg.mode = capsnap->mode;
1372 arg.inline_data = capsnap->inline_data;
1373 arg.flags = 0;
1375 return send_cap_msg(&arg);
1379 * When a snapshot is taken, clients accumulate dirty metadata on
1380 * inodes with capabilities in ceph_cap_snaps to describe the file
1381 * state at the time the snapshot was taken. This must be flushed
1382 * asynchronously back to the MDS once sync writes complete and dirty
1383 * data is written out.
1385 * Called under i_ceph_lock. Takes s_mutex as needed.
1387 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1388 struct ceph_mds_session *session)
1389 __releases(ci->i_ceph_lock)
1390 __acquires(ci->i_ceph_lock)
1392 struct inode *inode = &ci->vfs_inode;
1393 struct ceph_mds_client *mdsc = session->s_mdsc;
1394 struct ceph_cap_snap *capsnap;
1395 u64 oldest_flush_tid = 0;
1396 u64 first_tid = 1, last_tid = 0;
1398 dout("__flush_snaps %p session %p\n", inode, session);
1400 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1402 * we need to wait for sync writes to complete and for dirty
1403 * pages to be written out.
1405 if (capsnap->dirty_pages || capsnap->writing)
1406 break;
1408 /* should be removed by ceph_try_drop_cap_snap() */
1409 BUG_ON(!capsnap->need_flush);
1411 /* only flush each capsnap once */
1412 if (capsnap->cap_flush.tid > 0) {
1413 dout(" already flushed %p, skipping\n", capsnap);
1414 continue;
1417 spin_lock(&mdsc->cap_dirty_lock);
1418 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1419 list_add_tail(&capsnap->cap_flush.g_list,
1420 &mdsc->cap_flush_list);
1421 if (oldest_flush_tid == 0)
1422 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1423 if (list_empty(&ci->i_flushing_item)) {
1424 list_add_tail(&ci->i_flushing_item,
1425 &session->s_cap_flushing);
1427 spin_unlock(&mdsc->cap_dirty_lock);
1429 list_add_tail(&capsnap->cap_flush.i_list,
1430 &ci->i_cap_flush_list);
1432 if (first_tid == 1)
1433 first_tid = capsnap->cap_flush.tid;
1434 last_tid = capsnap->cap_flush.tid;
1437 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1439 while (first_tid <= last_tid) {
1440 struct ceph_cap *cap = ci->i_auth_cap;
1441 struct ceph_cap_flush *cf;
1442 int ret;
1444 if (!(cap && cap->session == session)) {
1445 dout("__flush_snaps %p auth cap %p not mds%d, "
1446 "stop\n", inode, cap, session->s_mds);
1447 break;
1450 ret = -ENOENT;
1451 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1452 if (cf->tid >= first_tid) {
1453 ret = 0;
1454 break;
1457 if (ret < 0)
1458 break;
1460 first_tid = cf->tid + 1;
1462 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1463 refcount_inc(&capsnap->nref);
1464 spin_unlock(&ci->i_ceph_lock);
1466 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1467 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1469 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1470 oldest_flush_tid);
1471 if (ret < 0) {
1472 pr_err("__flush_snaps: error sending cap flushsnap, "
1473 "ino (%llx.%llx) tid %llu follows %llu\n",
1474 ceph_vinop(inode), cf->tid, capsnap->follows);
1477 ceph_put_cap_snap(capsnap);
1478 spin_lock(&ci->i_ceph_lock);
1482 void ceph_flush_snaps(struct ceph_inode_info *ci,
1483 struct ceph_mds_session **psession)
1485 struct inode *inode = &ci->vfs_inode;
1486 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1487 struct ceph_mds_session *session = NULL;
1488 int mds;
1490 dout("ceph_flush_snaps %p\n", inode);
1491 if (psession)
1492 session = *psession;
1493 retry:
1494 spin_lock(&ci->i_ceph_lock);
1495 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1496 dout(" no capsnap needs flush, doing nothing\n");
1497 goto out;
1499 if (!ci->i_auth_cap) {
1500 dout(" no auth cap (migrating?), doing nothing\n");
1501 goto out;
1504 mds = ci->i_auth_cap->session->s_mds;
1505 if (session && session->s_mds != mds) {
1506 dout(" oops, wrong session %p mutex\n", session);
1507 mutex_unlock(&session->s_mutex);
1508 ceph_put_mds_session(session);
1509 session = NULL;
1511 if (!session) {
1512 spin_unlock(&ci->i_ceph_lock);
1513 mutex_lock(&mdsc->mutex);
1514 session = __ceph_lookup_mds_session(mdsc, mds);
1515 mutex_unlock(&mdsc->mutex);
1516 if (session) {
1517 dout(" inverting session/ino locks on %p\n", session);
1518 mutex_lock(&session->s_mutex);
1520 goto retry;
1523 // make sure flushsnap messages are sent in proper order.
1524 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1525 __kick_flushing_caps(mdsc, session, ci, 0);
1526 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1529 __ceph_flush_snaps(ci, session);
1530 out:
1531 spin_unlock(&ci->i_ceph_lock);
1533 if (psession) {
1534 *psession = session;
1535 } else if (session) {
1536 mutex_unlock(&session->s_mutex);
1537 ceph_put_mds_session(session);
1539 /* we flushed them all; remove this inode from the queue */
1540 spin_lock(&mdsc->snap_flush_lock);
1541 list_del_init(&ci->i_snap_flush_item);
1542 spin_unlock(&mdsc->snap_flush_lock);
1546 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1547 * Caller is then responsible for calling __mark_inode_dirty with the
1548 * returned flags value.
1550 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1551 struct ceph_cap_flush **pcf)
1553 struct ceph_mds_client *mdsc =
1554 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1555 struct inode *inode = &ci->vfs_inode;
1556 int was = ci->i_dirty_caps;
1557 int dirty = 0;
1559 if (!ci->i_auth_cap) {
1560 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1561 "but no auth cap (session was closed?)\n",
1562 inode, ceph_ino(inode), ceph_cap_string(mask));
1563 return 0;
1566 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1567 ceph_cap_string(mask), ceph_cap_string(was),
1568 ceph_cap_string(was | mask));
1569 ci->i_dirty_caps |= mask;
1570 if (was == 0) {
1571 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1572 swap(ci->i_prealloc_cap_flush, *pcf);
1574 if (!ci->i_head_snapc) {
1575 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1576 ci->i_head_snapc = ceph_get_snap_context(
1577 ci->i_snap_realm->cached_context);
1579 dout(" inode %p now dirty snapc %p auth cap %p\n",
1580 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1581 BUG_ON(!list_empty(&ci->i_dirty_item));
1582 spin_lock(&mdsc->cap_dirty_lock);
1583 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1584 spin_unlock(&mdsc->cap_dirty_lock);
1585 if (ci->i_flushing_caps == 0) {
1586 ihold(inode);
1587 dirty |= I_DIRTY_SYNC;
1589 } else {
1590 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1592 BUG_ON(list_empty(&ci->i_dirty_item));
1593 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1594 (mask & CEPH_CAP_FILE_BUFFER))
1595 dirty |= I_DIRTY_DATASYNC;
1596 __cap_delay_requeue(mdsc, ci);
1597 return dirty;
1600 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1602 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1605 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1607 if (cf)
1608 kmem_cache_free(ceph_cap_flush_cachep, cf);
1611 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1613 if (!list_empty(&mdsc->cap_flush_list)) {
1614 struct ceph_cap_flush *cf =
1615 list_first_entry(&mdsc->cap_flush_list,
1616 struct ceph_cap_flush, g_list);
1617 return cf->tid;
1619 return 0;
1623 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1624 * Return true if caller needs to wake up flush waiters.
1626 static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1627 struct ceph_inode_info *ci,
1628 struct ceph_cap_flush *cf)
1630 struct ceph_cap_flush *prev;
1631 bool wake = cf->wake;
1632 if (mdsc) {
1633 /* are there older pending cap flushes? */
1634 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1635 prev = list_prev_entry(cf, g_list);
1636 prev->wake = true;
1637 wake = false;
1639 list_del(&cf->g_list);
1640 } else if (ci) {
1641 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1642 prev = list_prev_entry(cf, i_list);
1643 prev->wake = true;
1644 wake = false;
1646 list_del(&cf->i_list);
1647 } else {
1648 BUG_ON(1);
1650 return wake;
1654 * Add dirty inode to the flushing list. Assigned a seq number so we
1655 * can wait for caps to flush without starving.
1657 * Called under i_ceph_lock.
1659 static int __mark_caps_flushing(struct inode *inode,
1660 struct ceph_mds_session *session, bool wake,
1661 u64 *flush_tid, u64 *oldest_flush_tid)
1663 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1664 struct ceph_inode_info *ci = ceph_inode(inode);
1665 struct ceph_cap_flush *cf = NULL;
1666 int flushing;
1668 BUG_ON(ci->i_dirty_caps == 0);
1669 BUG_ON(list_empty(&ci->i_dirty_item));
1670 BUG_ON(!ci->i_prealloc_cap_flush);
1672 flushing = ci->i_dirty_caps;
1673 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1674 ceph_cap_string(flushing),
1675 ceph_cap_string(ci->i_flushing_caps),
1676 ceph_cap_string(ci->i_flushing_caps | flushing));
1677 ci->i_flushing_caps |= flushing;
1678 ci->i_dirty_caps = 0;
1679 dout(" inode %p now !dirty\n", inode);
1681 swap(cf, ci->i_prealloc_cap_flush);
1682 cf->caps = flushing;
1683 cf->wake = wake;
1685 spin_lock(&mdsc->cap_dirty_lock);
1686 list_del_init(&ci->i_dirty_item);
1688 cf->tid = ++mdsc->last_cap_flush_tid;
1689 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1690 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1692 if (list_empty(&ci->i_flushing_item)) {
1693 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1694 mdsc->num_cap_flushing++;
1696 spin_unlock(&mdsc->cap_dirty_lock);
1698 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1700 *flush_tid = cf->tid;
1701 return flushing;
1705 * try to invalidate mapping pages without blocking.
1707 static int try_nonblocking_invalidate(struct inode *inode)
1709 struct ceph_inode_info *ci = ceph_inode(inode);
1710 u32 invalidating_gen = ci->i_rdcache_gen;
1712 spin_unlock(&ci->i_ceph_lock);
1713 invalidate_mapping_pages(&inode->i_data, 0, -1);
1714 spin_lock(&ci->i_ceph_lock);
1716 if (inode->i_data.nrpages == 0 &&
1717 invalidating_gen == ci->i_rdcache_gen) {
1718 /* success. */
1719 dout("try_nonblocking_invalidate %p success\n", inode);
1720 /* save any racing async invalidate some trouble */
1721 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1722 return 0;
1724 dout("try_nonblocking_invalidate %p failed\n", inode);
1725 return -1;
1728 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1730 loff_t size = ci->vfs_inode.i_size;
1731 /* mds will adjust max size according to the reported size */
1732 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1733 return false;
1734 if (size >= ci->i_max_size)
1735 return true;
1736 /* half of previous max_size increment has been used */
1737 if (ci->i_max_size > ci->i_reported_size &&
1738 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1739 return true;
1740 return false;
1744 * Swiss army knife function to examine currently used and wanted
1745 * versus held caps. Release, flush, ack revoked caps to mds as
1746 * appropriate.
1748 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1749 * cap release further.
1750 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1751 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1752 * further delay.
1754 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1755 struct ceph_mds_session *session)
1757 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1758 struct ceph_mds_client *mdsc = fsc->mdsc;
1759 struct inode *inode = &ci->vfs_inode;
1760 struct ceph_cap *cap;
1761 u64 flush_tid, oldest_flush_tid;
1762 int file_wanted, used, cap_used;
1763 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1764 int issued, implemented, want, retain, revoking, flushing = 0;
1765 int mds = -1; /* keep track of how far we've gone through i_caps list
1766 to avoid an infinite loop on retry */
1767 struct rb_node *p;
1768 int delayed = 0, sent = 0;
1769 bool no_delay = flags & CHECK_CAPS_NODELAY;
1770 bool queue_invalidate = false;
1771 bool tried_invalidate = false;
1773 /* if we are unmounting, flush any unused caps immediately. */
1774 if (mdsc->stopping)
1775 no_delay = true;
1777 spin_lock(&ci->i_ceph_lock);
1779 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1780 flags |= CHECK_CAPS_FLUSH;
1782 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1783 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1784 __cap_delay_cancel(mdsc, ci);
1786 goto retry_locked;
1787 retry:
1788 spin_lock(&ci->i_ceph_lock);
1789 retry_locked:
1790 file_wanted = __ceph_caps_file_wanted(ci);
1791 used = __ceph_caps_used(ci);
1792 issued = __ceph_caps_issued(ci, &implemented);
1793 revoking = implemented & ~issued;
1795 want = file_wanted;
1796 retain = file_wanted | used | CEPH_CAP_PIN;
1797 if (!mdsc->stopping && inode->i_nlink > 0) {
1798 if (file_wanted) {
1799 retain |= CEPH_CAP_ANY; /* be greedy */
1800 } else if (S_ISDIR(inode->i_mode) &&
1801 (issued & CEPH_CAP_FILE_SHARED) &&
1802 __ceph_dir_is_complete(ci)) {
1804 * If a directory is complete, we want to keep
1805 * the exclusive cap. So that MDS does not end up
1806 * revoking the shared cap on every create/unlink
1807 * operation.
1809 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1810 retain |= want;
1811 } else {
1813 retain |= CEPH_CAP_ANY_SHARED;
1815 * keep RD only if we didn't have the file open RW,
1816 * because then the mds would revoke it anyway to
1817 * journal max_size=0.
1819 if (ci->i_max_size == 0)
1820 retain |= CEPH_CAP_ANY_RD;
1824 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1825 " issued %s revoking %s retain %s %s%s%s\n", inode,
1826 ceph_cap_string(file_wanted),
1827 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1828 ceph_cap_string(ci->i_flushing_caps),
1829 ceph_cap_string(issued), ceph_cap_string(revoking),
1830 ceph_cap_string(retain),
1831 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1832 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1833 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1836 * If we no longer need to hold onto old our caps, and we may
1837 * have cached pages, but don't want them, then try to invalidate.
1838 * If we fail, it's because pages are locked.... try again later.
1840 if ((!no_delay || mdsc->stopping) &&
1841 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
1842 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
1843 inode->i_data.nrpages && /* have cached pages */
1844 (revoking & (CEPH_CAP_FILE_CACHE|
1845 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
1846 !tried_invalidate) {
1847 dout("check_caps trying to invalidate on %p\n", inode);
1848 if (try_nonblocking_invalidate(inode) < 0) {
1849 dout("check_caps queuing invalidate\n");
1850 queue_invalidate = true;
1851 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1853 tried_invalidate = true;
1854 goto retry_locked;
1857 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1858 cap = rb_entry(p, struct ceph_cap, ci_node);
1860 /* avoid looping forever */
1861 if (mds >= cap->mds ||
1862 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1863 continue;
1865 /* NOTE: no side-effects allowed, until we take s_mutex */
1867 cap_used = used;
1868 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1869 cap_used &= ~ci->i_auth_cap->issued;
1871 revoking = cap->implemented & ~cap->issued;
1872 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1873 cap->mds, cap, ceph_cap_string(cap_used),
1874 ceph_cap_string(cap->issued),
1875 ceph_cap_string(cap->implemented),
1876 ceph_cap_string(revoking));
1878 if (cap == ci->i_auth_cap &&
1879 (cap->issued & CEPH_CAP_FILE_WR)) {
1880 /* request larger max_size from MDS? */
1881 if (ci->i_wanted_max_size > ci->i_max_size &&
1882 ci->i_wanted_max_size > ci->i_requested_max_size) {
1883 dout("requesting new max_size\n");
1884 goto ack;
1887 /* approaching file_max? */
1888 if (__ceph_should_report_size(ci)) {
1889 dout("i_size approaching max_size\n");
1890 goto ack;
1893 /* flush anything dirty? */
1894 if (cap == ci->i_auth_cap) {
1895 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1896 dout("flushing dirty caps\n");
1897 goto ack;
1899 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1900 dout("flushing snap caps\n");
1901 goto ack;
1905 /* completed revocation? going down and there are no caps? */
1906 if (revoking && (revoking & cap_used) == 0) {
1907 dout("completed revocation of %s\n",
1908 ceph_cap_string(cap->implemented & ~cap->issued));
1909 goto ack;
1912 /* want more caps from mds? */
1913 if (want & ~(cap->mds_wanted | cap->issued))
1914 goto ack;
1916 /* things we might delay */
1917 if ((cap->issued & ~retain) == 0 &&
1918 cap->mds_wanted == want)
1919 continue; /* nope, all good */
1921 if (no_delay)
1922 goto ack;
1924 /* delay? */
1925 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1926 time_before(jiffies, ci->i_hold_caps_max)) {
1927 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1928 ceph_cap_string(cap->issued),
1929 ceph_cap_string(cap->issued & retain),
1930 ceph_cap_string(cap->mds_wanted),
1931 ceph_cap_string(want));
1932 delayed++;
1933 continue;
1936 ack:
1937 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1938 dout(" skipping %p I_NOFLUSH set\n", inode);
1939 continue;
1942 if (session && session != cap->session) {
1943 dout("oops, wrong session %p mutex\n", session);
1944 mutex_unlock(&session->s_mutex);
1945 session = NULL;
1947 if (!session) {
1948 session = cap->session;
1949 if (mutex_trylock(&session->s_mutex) == 0) {
1950 dout("inverting session/ino locks on %p\n",
1951 session);
1952 spin_unlock(&ci->i_ceph_lock);
1953 if (took_snap_rwsem) {
1954 up_read(&mdsc->snap_rwsem);
1955 took_snap_rwsem = 0;
1957 mutex_lock(&session->s_mutex);
1958 goto retry;
1962 /* kick flushing and flush snaps before sending normal
1963 * cap message */
1964 if (cap == ci->i_auth_cap &&
1965 (ci->i_ceph_flags &
1966 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
1967 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1968 __kick_flushing_caps(mdsc, session, ci, 0);
1969 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1971 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
1972 __ceph_flush_snaps(ci, session);
1974 goto retry_locked;
1977 /* take snap_rwsem after session mutex */
1978 if (!took_snap_rwsem) {
1979 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1980 dout("inverting snap/in locks on %p\n",
1981 inode);
1982 spin_unlock(&ci->i_ceph_lock);
1983 down_read(&mdsc->snap_rwsem);
1984 took_snap_rwsem = 1;
1985 goto retry;
1987 took_snap_rwsem = 1;
1990 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
1991 flushing = __mark_caps_flushing(inode, session, false,
1992 &flush_tid,
1993 &oldest_flush_tid);
1994 } else {
1995 flushing = 0;
1996 flush_tid = 0;
1997 spin_lock(&mdsc->cap_dirty_lock);
1998 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1999 spin_unlock(&mdsc->cap_dirty_lock);
2002 mds = cap->mds; /* remember mds, so we don't repeat */
2003 sent++;
2005 /* __send_cap drops i_ceph_lock */
2006 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2007 cap_used, want, retain, flushing,
2008 flush_tid, oldest_flush_tid);
2009 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2012 /* Reschedule delayed caps release if we delayed anything */
2013 if (delayed)
2014 __cap_delay_requeue(mdsc, ci);
2016 spin_unlock(&ci->i_ceph_lock);
2018 if (queue_invalidate)
2019 ceph_queue_invalidate(inode);
2021 if (session)
2022 mutex_unlock(&session->s_mutex);
2023 if (took_snap_rwsem)
2024 up_read(&mdsc->snap_rwsem);
2028 * Try to flush dirty caps back to the auth mds.
2030 static int try_flush_caps(struct inode *inode, u64 *ptid)
2032 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2033 struct ceph_inode_info *ci = ceph_inode(inode);
2034 struct ceph_mds_session *session = NULL;
2035 int flushing = 0;
2036 u64 flush_tid = 0, oldest_flush_tid = 0;
2038 retry:
2039 spin_lock(&ci->i_ceph_lock);
2040 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2041 spin_unlock(&ci->i_ceph_lock);
2042 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2043 goto out;
2045 if (ci->i_dirty_caps && ci->i_auth_cap) {
2046 struct ceph_cap *cap = ci->i_auth_cap;
2047 int used = __ceph_caps_used(ci);
2048 int want = __ceph_caps_wanted(ci);
2049 int delayed;
2051 if (!session || session != cap->session) {
2052 spin_unlock(&ci->i_ceph_lock);
2053 if (session)
2054 mutex_unlock(&session->s_mutex);
2055 session = cap->session;
2056 mutex_lock(&session->s_mutex);
2057 goto retry;
2059 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2060 spin_unlock(&ci->i_ceph_lock);
2061 goto out;
2064 flushing = __mark_caps_flushing(inode, session, true,
2065 &flush_tid, &oldest_flush_tid);
2067 /* __send_cap drops i_ceph_lock */
2068 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2069 used, want, (cap->issued | cap->implemented),
2070 flushing, flush_tid, oldest_flush_tid);
2072 if (delayed) {
2073 spin_lock(&ci->i_ceph_lock);
2074 __cap_delay_requeue(mdsc, ci);
2075 spin_unlock(&ci->i_ceph_lock);
2077 } else {
2078 if (!list_empty(&ci->i_cap_flush_list)) {
2079 struct ceph_cap_flush *cf =
2080 list_last_entry(&ci->i_cap_flush_list,
2081 struct ceph_cap_flush, i_list);
2082 cf->wake = true;
2083 flush_tid = cf->tid;
2085 flushing = ci->i_flushing_caps;
2086 spin_unlock(&ci->i_ceph_lock);
2088 out:
2089 if (session)
2090 mutex_unlock(&session->s_mutex);
2092 *ptid = flush_tid;
2093 return flushing;
2097 * Return true if we've flushed caps through the given flush_tid.
2099 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2101 struct ceph_inode_info *ci = ceph_inode(inode);
2102 int ret = 1;
2104 spin_lock(&ci->i_ceph_lock);
2105 if (!list_empty(&ci->i_cap_flush_list)) {
2106 struct ceph_cap_flush * cf =
2107 list_first_entry(&ci->i_cap_flush_list,
2108 struct ceph_cap_flush, i_list);
2109 if (cf->tid <= flush_tid)
2110 ret = 0;
2112 spin_unlock(&ci->i_ceph_lock);
2113 return ret;
2117 * wait for any unsafe requests to complete.
2119 static int unsafe_request_wait(struct inode *inode)
2121 struct ceph_inode_info *ci = ceph_inode(inode);
2122 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2123 int ret, err = 0;
2125 spin_lock(&ci->i_unsafe_lock);
2126 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2127 req1 = list_last_entry(&ci->i_unsafe_dirops,
2128 struct ceph_mds_request,
2129 r_unsafe_dir_item);
2130 ceph_mdsc_get_request(req1);
2132 if (!list_empty(&ci->i_unsafe_iops)) {
2133 req2 = list_last_entry(&ci->i_unsafe_iops,
2134 struct ceph_mds_request,
2135 r_unsafe_target_item);
2136 ceph_mdsc_get_request(req2);
2138 spin_unlock(&ci->i_unsafe_lock);
2140 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
2141 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2142 if (req1) {
2143 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2144 ceph_timeout_jiffies(req1->r_timeout));
2145 if (ret)
2146 err = -EIO;
2147 ceph_mdsc_put_request(req1);
2149 if (req2) {
2150 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2151 ceph_timeout_jiffies(req2->r_timeout));
2152 if (ret)
2153 err = -EIO;
2154 ceph_mdsc_put_request(req2);
2156 return err;
2159 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2161 struct inode *inode = file->f_mapping->host;
2162 struct ceph_inode_info *ci = ceph_inode(inode);
2163 u64 flush_tid;
2164 int ret;
2165 int dirty;
2167 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2169 ret = file_write_and_wait_range(file, start, end);
2170 if (ret < 0)
2171 goto out;
2173 if (datasync)
2174 goto out;
2176 inode_lock(inode);
2178 dirty = try_flush_caps(inode, &flush_tid);
2179 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2181 ret = unsafe_request_wait(inode);
2184 * only wait on non-file metadata writeback (the mds
2185 * can recover size and mtime, so we don't need to
2186 * wait for that)
2188 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2189 ret = wait_event_interruptible(ci->i_cap_wq,
2190 caps_are_flushed(inode, flush_tid));
2192 inode_unlock(inode);
2193 out:
2194 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2195 return ret;
2199 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2200 * queue inode for flush but don't do so immediately, because we can
2201 * get by with fewer MDS messages if we wait for data writeback to
2202 * complete first.
2204 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2206 struct ceph_inode_info *ci = ceph_inode(inode);
2207 u64 flush_tid;
2208 int err = 0;
2209 int dirty;
2210 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2212 dout("write_inode %p wait=%d\n", inode, wait);
2213 if (wait) {
2214 dirty = try_flush_caps(inode, &flush_tid);
2215 if (dirty)
2216 err = wait_event_interruptible(ci->i_cap_wq,
2217 caps_are_flushed(inode, flush_tid));
2218 } else {
2219 struct ceph_mds_client *mdsc =
2220 ceph_sb_to_client(inode->i_sb)->mdsc;
2222 spin_lock(&ci->i_ceph_lock);
2223 if (__ceph_caps_dirty(ci))
2224 __cap_delay_requeue_front(mdsc, ci);
2225 spin_unlock(&ci->i_ceph_lock);
2227 return err;
2230 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2231 struct ceph_mds_session *session,
2232 struct ceph_inode_info *ci,
2233 u64 oldest_flush_tid)
2234 __releases(ci->i_ceph_lock)
2235 __acquires(ci->i_ceph_lock)
2237 struct inode *inode = &ci->vfs_inode;
2238 struct ceph_cap *cap;
2239 struct ceph_cap_flush *cf;
2240 int ret;
2241 u64 first_tid = 0;
2243 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2244 if (cf->tid < first_tid)
2245 continue;
2247 cap = ci->i_auth_cap;
2248 if (!(cap && cap->session == session)) {
2249 pr_err("%p auth cap %p not mds%d ???\n",
2250 inode, cap, session->s_mds);
2251 break;
2254 first_tid = cf->tid + 1;
2256 if (cf->caps) {
2257 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2258 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2259 ci->i_ceph_flags |= CEPH_I_NODELAY;
2260 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2261 false, __ceph_caps_used(ci),
2262 __ceph_caps_wanted(ci),
2263 cap->issued | cap->implemented,
2264 cf->caps, cf->tid, oldest_flush_tid);
2265 if (ret) {
2266 pr_err("kick_flushing_caps: error sending "
2267 "cap flush, ino (%llx.%llx) "
2268 "tid %llu flushing %s\n",
2269 ceph_vinop(inode), cf->tid,
2270 ceph_cap_string(cf->caps));
2272 } else {
2273 struct ceph_cap_snap *capsnap =
2274 container_of(cf, struct ceph_cap_snap,
2275 cap_flush);
2276 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2277 inode, capsnap, cf->tid,
2278 ceph_cap_string(capsnap->dirty));
2280 refcount_inc(&capsnap->nref);
2281 spin_unlock(&ci->i_ceph_lock);
2283 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2284 oldest_flush_tid);
2285 if (ret < 0) {
2286 pr_err("kick_flushing_caps: error sending "
2287 "cap flushsnap, ino (%llx.%llx) "
2288 "tid %llu follows %llu\n",
2289 ceph_vinop(inode), cf->tid,
2290 capsnap->follows);
2293 ceph_put_cap_snap(capsnap);
2296 spin_lock(&ci->i_ceph_lock);
2300 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2301 struct ceph_mds_session *session)
2303 struct ceph_inode_info *ci;
2304 struct ceph_cap *cap;
2305 u64 oldest_flush_tid;
2307 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2309 spin_lock(&mdsc->cap_dirty_lock);
2310 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2311 spin_unlock(&mdsc->cap_dirty_lock);
2313 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2314 spin_lock(&ci->i_ceph_lock);
2315 cap = ci->i_auth_cap;
2316 if (!(cap && cap->session == session)) {
2317 pr_err("%p auth cap %p not mds%d ???\n",
2318 &ci->vfs_inode, cap, session->s_mds);
2319 spin_unlock(&ci->i_ceph_lock);
2320 continue;
2325 * if flushing caps were revoked, we re-send the cap flush
2326 * in client reconnect stage. This guarantees MDS * processes
2327 * the cap flush message before issuing the flushing caps to
2328 * other client.
2330 if ((cap->issued & ci->i_flushing_caps) !=
2331 ci->i_flushing_caps) {
2332 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2333 __kick_flushing_caps(mdsc, session, ci,
2334 oldest_flush_tid);
2335 } else {
2336 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2339 spin_unlock(&ci->i_ceph_lock);
2343 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2344 struct ceph_mds_session *session)
2346 struct ceph_inode_info *ci;
2347 struct ceph_cap *cap;
2348 u64 oldest_flush_tid;
2350 dout("kick_flushing_caps mds%d\n", session->s_mds);
2352 spin_lock(&mdsc->cap_dirty_lock);
2353 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2354 spin_unlock(&mdsc->cap_dirty_lock);
2356 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2357 spin_lock(&ci->i_ceph_lock);
2358 cap = ci->i_auth_cap;
2359 if (!(cap && cap->session == session)) {
2360 pr_err("%p auth cap %p not mds%d ???\n",
2361 &ci->vfs_inode, cap, session->s_mds);
2362 spin_unlock(&ci->i_ceph_lock);
2363 continue;
2365 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2366 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2367 __kick_flushing_caps(mdsc, session, ci,
2368 oldest_flush_tid);
2370 spin_unlock(&ci->i_ceph_lock);
2374 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2375 struct ceph_mds_session *session,
2376 struct inode *inode)
2377 __releases(ci->i_ceph_lock)
2379 struct ceph_inode_info *ci = ceph_inode(inode);
2380 struct ceph_cap *cap;
2382 cap = ci->i_auth_cap;
2383 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2384 ceph_cap_string(ci->i_flushing_caps));
2386 if (!list_empty(&ci->i_cap_flush_list)) {
2387 u64 oldest_flush_tid;
2388 spin_lock(&mdsc->cap_dirty_lock);
2389 list_move_tail(&ci->i_flushing_item,
2390 &cap->session->s_cap_flushing);
2391 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2392 spin_unlock(&mdsc->cap_dirty_lock);
2394 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2395 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2396 spin_unlock(&ci->i_ceph_lock);
2397 } else {
2398 spin_unlock(&ci->i_ceph_lock);
2404 * Take references to capabilities we hold, so that we don't release
2405 * them to the MDS prematurely.
2407 * Protected by i_ceph_lock.
2409 static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2410 bool snap_rwsem_locked)
2412 if (got & CEPH_CAP_PIN)
2413 ci->i_pin_ref++;
2414 if (got & CEPH_CAP_FILE_RD)
2415 ci->i_rd_ref++;
2416 if (got & CEPH_CAP_FILE_CACHE)
2417 ci->i_rdcache_ref++;
2418 if (got & CEPH_CAP_FILE_WR) {
2419 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2420 BUG_ON(!snap_rwsem_locked);
2421 ci->i_head_snapc = ceph_get_snap_context(
2422 ci->i_snap_realm->cached_context);
2424 ci->i_wr_ref++;
2426 if (got & CEPH_CAP_FILE_BUFFER) {
2427 if (ci->i_wb_ref == 0)
2428 ihold(&ci->vfs_inode);
2429 ci->i_wb_ref++;
2430 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2431 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2436 * Try to grab cap references. Specify those refs we @want, and the
2437 * minimal set we @need. Also include the larger offset we are writing
2438 * to (when applicable), and check against max_size here as well.
2439 * Note that caller is responsible for ensuring max_size increases are
2440 * requested from the MDS.
2442 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2443 loff_t endoff, bool nonblock, int *got, int *err)
2445 struct inode *inode = &ci->vfs_inode;
2446 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2447 int ret = 0;
2448 int have, implemented;
2449 int file_wanted;
2450 bool snap_rwsem_locked = false;
2452 dout("get_cap_refs %p need %s want %s\n", inode,
2453 ceph_cap_string(need), ceph_cap_string(want));
2455 again:
2456 spin_lock(&ci->i_ceph_lock);
2458 /* make sure file is actually open */
2459 file_wanted = __ceph_caps_file_wanted(ci);
2460 if ((file_wanted & need) != need) {
2461 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2462 ceph_cap_string(need), ceph_cap_string(file_wanted));
2463 *err = -EBADF;
2464 ret = 1;
2465 goto out_unlock;
2468 /* finish pending truncate */
2469 while (ci->i_truncate_pending) {
2470 spin_unlock(&ci->i_ceph_lock);
2471 if (snap_rwsem_locked) {
2472 up_read(&mdsc->snap_rwsem);
2473 snap_rwsem_locked = false;
2475 __ceph_do_pending_vmtruncate(inode);
2476 spin_lock(&ci->i_ceph_lock);
2479 have = __ceph_caps_issued(ci, &implemented);
2481 if (have & need & CEPH_CAP_FILE_WR) {
2482 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2483 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2484 inode, endoff, ci->i_max_size);
2485 if (endoff > ci->i_requested_max_size) {
2486 *err = -EAGAIN;
2487 ret = 1;
2489 goto out_unlock;
2492 * If a sync write is in progress, we must wait, so that we
2493 * can get a final snapshot value for size+mtime.
2495 if (__ceph_have_pending_cap_snap(ci)) {
2496 dout("get_cap_refs %p cap_snap_pending\n", inode);
2497 goto out_unlock;
2501 if ((have & need) == need) {
2503 * Look at (implemented & ~have & not) so that we keep waiting
2504 * on transition from wanted -> needed caps. This is needed
2505 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2506 * going before a prior buffered writeback happens.
2508 int not = want & ~(have & need);
2509 int revoking = implemented & ~have;
2510 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2511 inode, ceph_cap_string(have), ceph_cap_string(not),
2512 ceph_cap_string(revoking));
2513 if ((revoking & not) == 0) {
2514 if (!snap_rwsem_locked &&
2515 !ci->i_head_snapc &&
2516 (need & CEPH_CAP_FILE_WR)) {
2517 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2519 * we can not call down_read() when
2520 * task isn't in TASK_RUNNING state
2522 if (nonblock) {
2523 *err = -EAGAIN;
2524 ret = 1;
2525 goto out_unlock;
2528 spin_unlock(&ci->i_ceph_lock);
2529 down_read(&mdsc->snap_rwsem);
2530 snap_rwsem_locked = true;
2531 goto again;
2533 snap_rwsem_locked = true;
2535 *got = need | (have & want);
2536 if ((need & CEPH_CAP_FILE_RD) &&
2537 !(*got & CEPH_CAP_FILE_CACHE))
2538 ceph_disable_fscache_readpage(ci);
2539 __take_cap_refs(ci, *got, true);
2540 ret = 1;
2542 } else {
2543 int session_readonly = false;
2544 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2545 struct ceph_mds_session *s = ci->i_auth_cap->session;
2546 spin_lock(&s->s_cap_lock);
2547 session_readonly = s->s_readonly;
2548 spin_unlock(&s->s_cap_lock);
2550 if (session_readonly) {
2551 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2552 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2553 *err = -EROFS;
2554 ret = 1;
2555 goto out_unlock;
2558 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2559 int mds_wanted;
2560 if (READ_ONCE(mdsc->fsc->mount_state) ==
2561 CEPH_MOUNT_SHUTDOWN) {
2562 dout("get_cap_refs %p forced umount\n", inode);
2563 *err = -EIO;
2564 ret = 1;
2565 goto out_unlock;
2567 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2568 if (need & ~(mds_wanted & need)) {
2569 dout("get_cap_refs %p caps were dropped"
2570 " (session killed?)\n", inode);
2571 *err = -ESTALE;
2572 ret = 1;
2573 goto out_unlock;
2575 if (!(file_wanted & ~mds_wanted))
2576 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
2579 dout("get_cap_refs %p have %s needed %s\n", inode,
2580 ceph_cap_string(have), ceph_cap_string(need));
2582 out_unlock:
2583 spin_unlock(&ci->i_ceph_lock);
2584 if (snap_rwsem_locked)
2585 up_read(&mdsc->snap_rwsem);
2587 dout("get_cap_refs %p ret %d got %s\n", inode,
2588 ret, ceph_cap_string(*got));
2589 return ret;
2593 * Check the offset we are writing up to against our current
2594 * max_size. If necessary, tell the MDS we want to write to
2595 * a larger offset.
2597 static void check_max_size(struct inode *inode, loff_t endoff)
2599 struct ceph_inode_info *ci = ceph_inode(inode);
2600 int check = 0;
2602 /* do we need to explicitly request a larger max_size? */
2603 spin_lock(&ci->i_ceph_lock);
2604 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2605 dout("write %p at large endoff %llu, req max_size\n",
2606 inode, endoff);
2607 ci->i_wanted_max_size = endoff;
2609 /* duplicate ceph_check_caps()'s logic */
2610 if (ci->i_auth_cap &&
2611 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2612 ci->i_wanted_max_size > ci->i_max_size &&
2613 ci->i_wanted_max_size > ci->i_requested_max_size)
2614 check = 1;
2615 spin_unlock(&ci->i_ceph_lock);
2616 if (check)
2617 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2620 int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
2622 int ret, err = 0;
2624 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2625 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
2626 ret = ceph_pool_perm_check(ci, need);
2627 if (ret < 0)
2628 return ret;
2630 ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
2631 if (ret) {
2632 if (err == -EAGAIN) {
2633 ret = 0;
2634 } else if (err < 0) {
2635 ret = err;
2638 return ret;
2642 * Wait for caps, and take cap references. If we can't get a WR cap
2643 * due to a small max_size, make sure we check_max_size (and possibly
2644 * ask the mds) so we don't get hung up indefinitely.
2646 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2647 loff_t endoff, int *got, struct page **pinned_page)
2649 int _got, ret, err = 0;
2651 ret = ceph_pool_perm_check(ci, need);
2652 if (ret < 0)
2653 return ret;
2655 while (true) {
2656 if (endoff > 0)
2657 check_max_size(&ci->vfs_inode, endoff);
2659 err = 0;
2660 _got = 0;
2661 ret = try_get_cap_refs(ci, need, want, endoff,
2662 false, &_got, &err);
2663 if (ret) {
2664 if (err == -EAGAIN)
2665 continue;
2666 if (err < 0)
2667 ret = err;
2668 } else {
2669 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2670 add_wait_queue(&ci->i_cap_wq, &wait);
2672 while (!try_get_cap_refs(ci, need, want, endoff,
2673 true, &_got, &err)) {
2674 if (signal_pending(current)) {
2675 ret = -ERESTARTSYS;
2676 break;
2678 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2681 remove_wait_queue(&ci->i_cap_wq, &wait);
2683 if (err == -EAGAIN)
2684 continue;
2685 if (err < 0)
2686 ret = err;
2688 if (ret < 0) {
2689 if (err == -ESTALE) {
2690 /* session was killed, try renew caps */
2691 ret = ceph_renew_caps(&ci->vfs_inode);
2692 if (ret == 0)
2693 continue;
2695 return ret;
2698 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2699 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2700 i_size_read(&ci->vfs_inode) > 0) {
2701 struct page *page =
2702 find_get_page(ci->vfs_inode.i_mapping, 0);
2703 if (page) {
2704 if (PageUptodate(page)) {
2705 *pinned_page = page;
2706 break;
2708 put_page(page);
2711 * drop cap refs first because getattr while
2712 * holding * caps refs can cause deadlock.
2714 ceph_put_cap_refs(ci, _got);
2715 _got = 0;
2718 * getattr request will bring inline data into
2719 * page cache
2721 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2722 CEPH_STAT_CAP_INLINE_DATA,
2723 true);
2724 if (ret < 0)
2725 return ret;
2726 continue;
2728 break;
2731 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2732 ceph_fscache_revalidate_cookie(ci);
2734 *got = _got;
2735 return 0;
2739 * Take cap refs. Caller must already know we hold at least one ref
2740 * on the caps in question or we don't know this is safe.
2742 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2744 spin_lock(&ci->i_ceph_lock);
2745 __take_cap_refs(ci, caps, false);
2746 spin_unlock(&ci->i_ceph_lock);
2751 * drop cap_snap that is not associated with any snapshot.
2752 * we don't need to send FLUSHSNAP message for it.
2754 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2755 struct ceph_cap_snap *capsnap)
2757 if (!capsnap->need_flush &&
2758 !capsnap->writing && !capsnap->dirty_pages) {
2759 dout("dropping cap_snap %p follows %llu\n",
2760 capsnap, capsnap->follows);
2761 BUG_ON(capsnap->cap_flush.tid > 0);
2762 ceph_put_snap_context(capsnap->context);
2763 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2764 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2766 list_del(&capsnap->ci_item);
2767 ceph_put_cap_snap(capsnap);
2768 return 1;
2770 return 0;
2774 * Release cap refs.
2776 * If we released the last ref on any given cap, call ceph_check_caps
2777 * to release (or schedule a release).
2779 * If we are releasing a WR cap (from a sync write), finalize any affected
2780 * cap_snap, and wake up any waiters.
2782 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2784 struct inode *inode = &ci->vfs_inode;
2785 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2787 spin_lock(&ci->i_ceph_lock);
2788 if (had & CEPH_CAP_PIN)
2789 --ci->i_pin_ref;
2790 if (had & CEPH_CAP_FILE_RD)
2791 if (--ci->i_rd_ref == 0)
2792 last++;
2793 if (had & CEPH_CAP_FILE_CACHE)
2794 if (--ci->i_rdcache_ref == 0)
2795 last++;
2796 if (had & CEPH_CAP_FILE_BUFFER) {
2797 if (--ci->i_wb_ref == 0) {
2798 last++;
2799 put++;
2801 dout("put_cap_refs %p wb %d -> %d (?)\n",
2802 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2804 if (had & CEPH_CAP_FILE_WR)
2805 if (--ci->i_wr_ref == 0) {
2806 last++;
2807 if (__ceph_have_pending_cap_snap(ci)) {
2808 struct ceph_cap_snap *capsnap =
2809 list_last_entry(&ci->i_cap_snaps,
2810 struct ceph_cap_snap,
2811 ci_item);
2812 capsnap->writing = 0;
2813 if (ceph_try_drop_cap_snap(ci, capsnap))
2814 put++;
2815 else if (__ceph_finish_cap_snap(ci, capsnap))
2816 flushsnaps = 1;
2817 wake = 1;
2819 if (ci->i_wrbuffer_ref_head == 0 &&
2820 ci->i_dirty_caps == 0 &&
2821 ci->i_flushing_caps == 0) {
2822 BUG_ON(!ci->i_head_snapc);
2823 ceph_put_snap_context(ci->i_head_snapc);
2824 ci->i_head_snapc = NULL;
2826 /* see comment in __ceph_remove_cap() */
2827 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2828 drop_inode_snap_realm(ci);
2830 spin_unlock(&ci->i_ceph_lock);
2832 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2833 last ? " last" : "", put ? " put" : "");
2835 if (last && !flushsnaps)
2836 ceph_check_caps(ci, 0, NULL);
2837 else if (flushsnaps)
2838 ceph_flush_snaps(ci, NULL);
2839 if (wake)
2840 wake_up_all(&ci->i_cap_wq);
2841 while (put-- > 0)
2842 iput(inode);
2846 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2847 * context. Adjust per-snap dirty page accounting as appropriate.
2848 * Once all dirty data for a cap_snap is flushed, flush snapped file
2849 * metadata back to the MDS. If we dropped the last ref, call
2850 * ceph_check_caps.
2852 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2853 struct ceph_snap_context *snapc)
2855 struct inode *inode = &ci->vfs_inode;
2856 struct ceph_cap_snap *capsnap = NULL;
2857 int put = 0;
2858 bool last = false;
2859 bool found = false;
2860 bool flush_snaps = false;
2861 bool complete_capsnap = false;
2863 spin_lock(&ci->i_ceph_lock);
2864 ci->i_wrbuffer_ref -= nr;
2865 if (ci->i_wrbuffer_ref == 0) {
2866 last = true;
2867 put++;
2870 if (ci->i_head_snapc == snapc) {
2871 ci->i_wrbuffer_ref_head -= nr;
2872 if (ci->i_wrbuffer_ref_head == 0 &&
2873 ci->i_wr_ref == 0 &&
2874 ci->i_dirty_caps == 0 &&
2875 ci->i_flushing_caps == 0) {
2876 BUG_ON(!ci->i_head_snapc);
2877 ceph_put_snap_context(ci->i_head_snapc);
2878 ci->i_head_snapc = NULL;
2880 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2881 inode,
2882 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2883 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2884 last ? " LAST" : "");
2885 } else {
2886 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2887 if (capsnap->context == snapc) {
2888 found = true;
2889 break;
2892 BUG_ON(!found);
2893 capsnap->dirty_pages -= nr;
2894 if (capsnap->dirty_pages == 0) {
2895 complete_capsnap = true;
2896 if (!capsnap->writing) {
2897 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2898 put++;
2899 } else {
2900 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2901 flush_snaps = true;
2905 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2906 " snap %lld %d/%d -> %d/%d %s%s\n",
2907 inode, capsnap, capsnap->context->seq,
2908 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2909 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2910 last ? " (wrbuffer last)" : "",
2911 complete_capsnap ? " (complete capsnap)" : "");
2914 spin_unlock(&ci->i_ceph_lock);
2916 if (last) {
2917 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2918 } else if (flush_snaps) {
2919 ceph_flush_snaps(ci, NULL);
2921 if (complete_capsnap)
2922 wake_up_all(&ci->i_cap_wq);
2923 while (put-- > 0)
2924 iput(inode);
2928 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2930 static void invalidate_aliases(struct inode *inode)
2932 struct dentry *dn, *prev = NULL;
2934 dout("invalidate_aliases inode %p\n", inode);
2935 d_prune_aliases(inode);
2937 * For non-directory inode, d_find_alias() only returns
2938 * hashed dentry. After calling d_invalidate(), the
2939 * dentry becomes unhashed.
2941 * For directory inode, d_find_alias() can return
2942 * unhashed dentry. But directory inode should have
2943 * one alias at most.
2945 while ((dn = d_find_alias(inode))) {
2946 if (dn == prev) {
2947 dput(dn);
2948 break;
2950 d_invalidate(dn);
2951 if (prev)
2952 dput(prev);
2953 prev = dn;
2955 if (prev)
2956 dput(prev);
2960 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2961 * actually be a revocation if it specifies a smaller cap set.)
2963 * caller holds s_mutex and i_ceph_lock, we drop both.
2965 static void handle_cap_grant(struct ceph_mds_client *mdsc,
2966 struct inode *inode, struct ceph_mds_caps *grant,
2967 struct ceph_string **pns, u64 inline_version,
2968 void *inline_data, u32 inline_len,
2969 struct ceph_buffer *xattr_buf,
2970 struct ceph_mds_session *session,
2971 struct ceph_cap *cap, int issued)
2972 __releases(ci->i_ceph_lock)
2973 __releases(mdsc->snap_rwsem)
2975 struct ceph_inode_info *ci = ceph_inode(inode);
2976 int mds = session->s_mds;
2977 int seq = le32_to_cpu(grant->seq);
2978 int newcaps = le32_to_cpu(grant->caps);
2979 int used, wanted, dirty;
2980 u64 size = le64_to_cpu(grant->size);
2981 u64 max_size = le64_to_cpu(grant->max_size);
2982 struct timespec mtime, atime, ctime;
2983 int check_caps = 0;
2984 bool wake = false;
2985 bool writeback = false;
2986 bool queue_trunc = false;
2987 bool queue_invalidate = false;
2988 bool deleted_inode = false;
2989 bool fill_inline = false;
2991 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2992 inode, cap, mds, seq, ceph_cap_string(newcaps));
2993 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2994 inode->i_size);
2998 * auth mds of the inode changed. we received the cap export message,
2999 * but still haven't received the cap import message. handle_cap_export
3000 * updated the new auth MDS' cap.
3002 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3003 * that was sent before the cap import message. So don't remove caps.
3005 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3006 WARN_ON(cap != ci->i_auth_cap);
3007 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3008 seq = cap->seq;
3009 newcaps |= cap->issued;
3013 * If CACHE is being revoked, and we have no dirty buffers,
3014 * try to invalidate (once). (If there are dirty buffers, we
3015 * will invalidate _after_ writeback.)
3017 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3018 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3019 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3020 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3021 if (try_nonblocking_invalidate(inode)) {
3022 /* there were locked pages.. invalidate later
3023 in a separate thread. */
3024 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3025 queue_invalidate = true;
3026 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3031 /* side effects now are allowed */
3032 cap->cap_gen = session->s_cap_gen;
3033 cap->seq = seq;
3035 __check_cap_issue(ci, cap, newcaps);
3037 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3038 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
3039 inode->i_mode = le32_to_cpu(grant->mode);
3040 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3041 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3042 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3043 from_kuid(&init_user_ns, inode->i_uid),
3044 from_kgid(&init_user_ns, inode->i_gid));
3047 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3048 (issued & CEPH_CAP_LINK_EXCL) == 0) {
3049 set_nlink(inode, le32_to_cpu(grant->nlink));
3050 if (inode->i_nlink == 0 &&
3051 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
3052 deleted_inode = true;
3055 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
3056 int len = le32_to_cpu(grant->xattr_len);
3057 u64 version = le64_to_cpu(grant->xattr_version);
3059 if (version > ci->i_xattrs.version) {
3060 dout(" got new xattrs v%llu on %p len %d\n",
3061 version, inode, len);
3062 if (ci->i_xattrs.blob)
3063 ceph_buffer_put(ci->i_xattrs.blob);
3064 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3065 ci->i_xattrs.version = version;
3066 ceph_forget_all_cached_acls(inode);
3070 if (newcaps & CEPH_CAP_ANY_RD) {
3071 /* ctime/mtime/atime? */
3072 ceph_decode_timespec(&mtime, &grant->mtime);
3073 ceph_decode_timespec(&atime, &grant->atime);
3074 ceph_decode_timespec(&ctime, &grant->ctime);
3075 ceph_fill_file_time(inode, issued,
3076 le32_to_cpu(grant->time_warp_seq),
3077 &ctime, &mtime, &atime);
3080 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3081 /* file layout may have changed */
3082 s64 old_pool = ci->i_layout.pool_id;
3083 struct ceph_string *old_ns;
3085 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3086 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3087 lockdep_is_held(&ci->i_ceph_lock));
3088 rcu_assign_pointer(ci->i_layout.pool_ns, *pns);
3090 if (ci->i_layout.pool_id != old_pool || *pns != old_ns)
3091 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3093 *pns = old_ns;
3095 /* size/truncate_seq? */
3096 queue_trunc = ceph_fill_file_size(inode, issued,
3097 le32_to_cpu(grant->truncate_seq),
3098 le64_to_cpu(grant->truncate_size),
3099 size);
3102 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3103 if (max_size != ci->i_max_size) {
3104 dout("max_size %lld -> %llu\n",
3105 ci->i_max_size, max_size);
3106 ci->i_max_size = max_size;
3107 if (max_size >= ci->i_wanted_max_size) {
3108 ci->i_wanted_max_size = 0; /* reset */
3109 ci->i_requested_max_size = 0;
3111 wake = true;
3112 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3113 ci->i_wanted_max_size > ci->i_requested_max_size) {
3114 /* CEPH_CAP_OP_IMPORT */
3115 wake = true;
3119 /* check cap bits */
3120 wanted = __ceph_caps_wanted(ci);
3121 used = __ceph_caps_used(ci);
3122 dirty = __ceph_caps_dirty(ci);
3123 dout(" my wanted = %s, used = %s, dirty %s\n",
3124 ceph_cap_string(wanted),
3125 ceph_cap_string(used),
3126 ceph_cap_string(dirty));
3127 if (wanted != le32_to_cpu(grant->wanted)) {
3128 dout("mds wanted %s -> %s\n",
3129 ceph_cap_string(le32_to_cpu(grant->wanted)),
3130 ceph_cap_string(wanted));
3131 /* imported cap may not have correct mds_wanted */
3132 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
3133 check_caps = 1;
3136 /* revocation, grant, or no-op? */
3137 if (cap->issued & ~newcaps) {
3138 int revoking = cap->issued & ~newcaps;
3140 dout("revocation: %s -> %s (revoking %s)\n",
3141 ceph_cap_string(cap->issued),
3142 ceph_cap_string(newcaps),
3143 ceph_cap_string(revoking));
3144 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3145 writeback = true; /* initiate writeback; will delay ack */
3146 else if (revoking == CEPH_CAP_FILE_CACHE &&
3147 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3148 queue_invalidate)
3149 ; /* do nothing yet, invalidation will be queued */
3150 else if (cap == ci->i_auth_cap)
3151 check_caps = 1; /* check auth cap only */
3152 else
3153 check_caps = 2; /* check all caps */
3154 cap->issued = newcaps;
3155 cap->implemented |= newcaps;
3156 } else if (cap->issued == newcaps) {
3157 dout("caps unchanged: %s -> %s\n",
3158 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3159 } else {
3160 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3161 ceph_cap_string(newcaps));
3162 /* non-auth MDS is revoking the newly grant caps ? */
3163 if (cap == ci->i_auth_cap &&
3164 __ceph_caps_revoking_other(ci, cap, newcaps))
3165 check_caps = 2;
3167 cap->issued = newcaps;
3168 cap->implemented |= newcaps; /* add bits only, to
3169 * avoid stepping on a
3170 * pending revocation */
3171 wake = true;
3173 BUG_ON(cap->issued & ~cap->implemented);
3175 if (inline_version > 0 && inline_version >= ci->i_inline_version) {
3176 ci->i_inline_version = inline_version;
3177 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3178 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3179 fill_inline = true;
3182 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3183 if (newcaps & ~issued)
3184 wake = true;
3185 kick_flushing_inode_caps(mdsc, session, inode);
3186 up_read(&mdsc->snap_rwsem);
3187 } else {
3188 spin_unlock(&ci->i_ceph_lock);
3191 if (fill_inline)
3192 ceph_fill_inline_data(inode, NULL, inline_data, inline_len);
3194 if (queue_trunc)
3195 ceph_queue_vmtruncate(inode);
3197 if (writeback)
3199 * queue inode for writeback: we can't actually call
3200 * filemap_write_and_wait, etc. from message handler
3201 * context.
3203 ceph_queue_writeback(inode);
3204 if (queue_invalidate)
3205 ceph_queue_invalidate(inode);
3206 if (deleted_inode)
3207 invalidate_aliases(inode);
3208 if (wake)
3209 wake_up_all(&ci->i_cap_wq);
3211 if (check_caps == 1)
3212 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3213 session);
3214 else if (check_caps == 2)
3215 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3216 else
3217 mutex_unlock(&session->s_mutex);
3221 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3222 * MDS has been safely committed.
3224 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3225 struct ceph_mds_caps *m,
3226 struct ceph_mds_session *session,
3227 struct ceph_cap *cap)
3228 __releases(ci->i_ceph_lock)
3230 struct ceph_inode_info *ci = ceph_inode(inode);
3231 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3232 struct ceph_cap_flush *cf, *tmp_cf;
3233 LIST_HEAD(to_remove);
3234 unsigned seq = le32_to_cpu(m->seq);
3235 int dirty = le32_to_cpu(m->dirty);
3236 int cleaned = 0;
3237 bool drop = false;
3238 bool wake_ci = false;
3239 bool wake_mdsc = false;
3241 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3242 if (cf->tid == flush_tid)
3243 cleaned = cf->caps;
3244 if (cf->caps == 0) /* capsnap */
3245 continue;
3246 if (cf->tid <= flush_tid) {
3247 if (__finish_cap_flush(NULL, ci, cf))
3248 wake_ci = true;
3249 list_add_tail(&cf->i_list, &to_remove);
3250 } else {
3251 cleaned &= ~cf->caps;
3252 if (!cleaned)
3253 break;
3257 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3258 " flushing %s -> %s\n",
3259 inode, session->s_mds, seq, ceph_cap_string(dirty),
3260 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3261 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3263 if (list_empty(&to_remove) && !cleaned)
3264 goto out;
3266 ci->i_flushing_caps &= ~cleaned;
3268 spin_lock(&mdsc->cap_dirty_lock);
3270 list_for_each_entry(cf, &to_remove, i_list) {
3271 if (__finish_cap_flush(mdsc, NULL, cf))
3272 wake_mdsc = true;
3275 if (ci->i_flushing_caps == 0) {
3276 if (list_empty(&ci->i_cap_flush_list)) {
3277 list_del_init(&ci->i_flushing_item);
3278 if (!list_empty(&session->s_cap_flushing)) {
3279 dout(" mds%d still flushing cap on %p\n",
3280 session->s_mds,
3281 &list_first_entry(&session->s_cap_flushing,
3282 struct ceph_inode_info,
3283 i_flushing_item)->vfs_inode);
3286 mdsc->num_cap_flushing--;
3287 dout(" inode %p now !flushing\n", inode);
3289 if (ci->i_dirty_caps == 0) {
3290 dout(" inode %p now clean\n", inode);
3291 BUG_ON(!list_empty(&ci->i_dirty_item));
3292 drop = true;
3293 if (ci->i_wr_ref == 0 &&
3294 ci->i_wrbuffer_ref_head == 0) {
3295 BUG_ON(!ci->i_head_snapc);
3296 ceph_put_snap_context(ci->i_head_snapc);
3297 ci->i_head_snapc = NULL;
3299 } else {
3300 BUG_ON(list_empty(&ci->i_dirty_item));
3303 spin_unlock(&mdsc->cap_dirty_lock);
3305 out:
3306 spin_unlock(&ci->i_ceph_lock);
3308 while (!list_empty(&to_remove)) {
3309 cf = list_first_entry(&to_remove,
3310 struct ceph_cap_flush, i_list);
3311 list_del(&cf->i_list);
3312 ceph_free_cap_flush(cf);
3315 if (wake_ci)
3316 wake_up_all(&ci->i_cap_wq);
3317 if (wake_mdsc)
3318 wake_up_all(&mdsc->cap_flushing_wq);
3319 if (drop)
3320 iput(inode);
3324 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3325 * throw away our cap_snap.
3327 * Caller hold s_mutex.
3329 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3330 struct ceph_mds_caps *m,
3331 struct ceph_mds_session *session)
3333 struct ceph_inode_info *ci = ceph_inode(inode);
3334 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3335 u64 follows = le64_to_cpu(m->snap_follows);
3336 struct ceph_cap_snap *capsnap;
3337 bool flushed = false;
3338 bool wake_ci = false;
3339 bool wake_mdsc = false;
3341 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3342 inode, ci, session->s_mds, follows);
3344 spin_lock(&ci->i_ceph_lock);
3345 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3346 if (capsnap->follows == follows) {
3347 if (capsnap->cap_flush.tid != flush_tid) {
3348 dout(" cap_snap %p follows %lld tid %lld !="
3349 " %lld\n", capsnap, follows,
3350 flush_tid, capsnap->cap_flush.tid);
3351 break;
3353 flushed = true;
3354 break;
3355 } else {
3356 dout(" skipping cap_snap %p follows %lld\n",
3357 capsnap, capsnap->follows);
3360 if (flushed) {
3361 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3362 dout(" removing %p cap_snap %p follows %lld\n",
3363 inode, capsnap, follows);
3364 list_del(&capsnap->ci_item);
3365 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3366 wake_ci = true;
3368 spin_lock(&mdsc->cap_dirty_lock);
3370 if (list_empty(&ci->i_cap_flush_list))
3371 list_del_init(&ci->i_flushing_item);
3373 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3374 wake_mdsc = true;
3376 spin_unlock(&mdsc->cap_dirty_lock);
3378 spin_unlock(&ci->i_ceph_lock);
3379 if (flushed) {
3380 ceph_put_snap_context(capsnap->context);
3381 ceph_put_cap_snap(capsnap);
3382 if (wake_ci)
3383 wake_up_all(&ci->i_cap_wq);
3384 if (wake_mdsc)
3385 wake_up_all(&mdsc->cap_flushing_wq);
3386 iput(inode);
3391 * Handle TRUNC from MDS, indicating file truncation.
3393 * caller hold s_mutex.
3395 static void handle_cap_trunc(struct inode *inode,
3396 struct ceph_mds_caps *trunc,
3397 struct ceph_mds_session *session)
3398 __releases(ci->i_ceph_lock)
3400 struct ceph_inode_info *ci = ceph_inode(inode);
3401 int mds = session->s_mds;
3402 int seq = le32_to_cpu(trunc->seq);
3403 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3404 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3405 u64 size = le64_to_cpu(trunc->size);
3406 int implemented = 0;
3407 int dirty = __ceph_caps_dirty(ci);
3408 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3409 int queue_trunc = 0;
3411 issued |= implemented | dirty;
3413 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3414 inode, mds, seq, truncate_size, truncate_seq);
3415 queue_trunc = ceph_fill_file_size(inode, issued,
3416 truncate_seq, truncate_size, size);
3417 spin_unlock(&ci->i_ceph_lock);
3419 if (queue_trunc)
3420 ceph_queue_vmtruncate(inode);
3424 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3425 * different one. If we are the most recent migration we've seen (as
3426 * indicated by mseq), make note of the migrating cap bits for the
3427 * duration (until we see the corresponding IMPORT).
3429 * caller holds s_mutex
3431 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3432 struct ceph_mds_cap_peer *ph,
3433 struct ceph_mds_session *session)
3435 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3436 struct ceph_mds_session *tsession = NULL;
3437 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3438 struct ceph_inode_info *ci = ceph_inode(inode);
3439 u64 t_cap_id;
3440 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3441 unsigned t_seq, t_mseq;
3442 int target, issued;
3443 int mds = session->s_mds;
3445 if (ph) {
3446 t_cap_id = le64_to_cpu(ph->cap_id);
3447 t_seq = le32_to_cpu(ph->seq);
3448 t_mseq = le32_to_cpu(ph->mseq);
3449 target = le32_to_cpu(ph->mds);
3450 } else {
3451 t_cap_id = t_seq = t_mseq = 0;
3452 target = -1;
3455 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3456 inode, ci, mds, mseq, target);
3457 retry:
3458 spin_lock(&ci->i_ceph_lock);
3459 cap = __get_cap_for_mds(ci, mds);
3460 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
3461 goto out_unlock;
3463 if (target < 0) {
3464 __ceph_remove_cap(cap, false);
3465 if (!ci->i_auth_cap)
3466 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
3467 goto out_unlock;
3471 * now we know we haven't received the cap import message yet
3472 * because the exported cap still exist.
3475 issued = cap->issued;
3476 if (issued != cap->implemented)
3477 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3478 "ino (%llx.%llx) mds%d seq %d mseq %d "
3479 "issued %s implemented %s\n",
3480 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3481 ceph_cap_string(issued),
3482 ceph_cap_string(cap->implemented));
3485 tcap = __get_cap_for_mds(ci, target);
3486 if (tcap) {
3487 /* already have caps from the target */
3488 if (tcap->cap_id == t_cap_id &&
3489 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3490 dout(" updating import cap %p mds%d\n", tcap, target);
3491 tcap->cap_id = t_cap_id;
3492 tcap->seq = t_seq - 1;
3493 tcap->issue_seq = t_seq - 1;
3494 tcap->mseq = t_mseq;
3495 tcap->issued |= issued;
3496 tcap->implemented |= issued;
3497 if (cap == ci->i_auth_cap)
3498 ci->i_auth_cap = tcap;
3500 if (!list_empty(&ci->i_cap_flush_list) &&
3501 ci->i_auth_cap == tcap) {
3502 spin_lock(&mdsc->cap_dirty_lock);
3503 list_move_tail(&ci->i_flushing_item,
3504 &tcap->session->s_cap_flushing);
3505 spin_unlock(&mdsc->cap_dirty_lock);
3508 __ceph_remove_cap(cap, false);
3509 goto out_unlock;
3510 } else if (tsession) {
3511 /* add placeholder for the export tagert */
3512 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
3513 tcap = new_cap;
3514 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
3515 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3517 if (!list_empty(&ci->i_cap_flush_list) &&
3518 ci->i_auth_cap == tcap) {
3519 spin_lock(&mdsc->cap_dirty_lock);
3520 list_move_tail(&ci->i_flushing_item,
3521 &tcap->session->s_cap_flushing);
3522 spin_unlock(&mdsc->cap_dirty_lock);
3525 __ceph_remove_cap(cap, false);
3526 goto out_unlock;
3529 spin_unlock(&ci->i_ceph_lock);
3530 mutex_unlock(&session->s_mutex);
3532 /* open target session */
3533 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3534 if (!IS_ERR(tsession)) {
3535 if (mds > target) {
3536 mutex_lock(&session->s_mutex);
3537 mutex_lock_nested(&tsession->s_mutex,
3538 SINGLE_DEPTH_NESTING);
3539 } else {
3540 mutex_lock(&tsession->s_mutex);
3541 mutex_lock_nested(&session->s_mutex,
3542 SINGLE_DEPTH_NESTING);
3544 new_cap = ceph_get_cap(mdsc, NULL);
3545 } else {
3546 WARN_ON(1);
3547 tsession = NULL;
3548 target = -1;
3550 goto retry;
3552 out_unlock:
3553 spin_unlock(&ci->i_ceph_lock);
3554 mutex_unlock(&session->s_mutex);
3555 if (tsession) {
3556 mutex_unlock(&tsession->s_mutex);
3557 ceph_put_mds_session(tsession);
3559 if (new_cap)
3560 ceph_put_cap(mdsc, new_cap);
3564 * Handle cap IMPORT.
3566 * caller holds s_mutex. acquires i_ceph_lock
3568 static void handle_cap_import(struct ceph_mds_client *mdsc,
3569 struct inode *inode, struct ceph_mds_caps *im,
3570 struct ceph_mds_cap_peer *ph,
3571 struct ceph_mds_session *session,
3572 struct ceph_cap **target_cap, int *old_issued)
3573 __acquires(ci->i_ceph_lock)
3575 struct ceph_inode_info *ci = ceph_inode(inode);
3576 struct ceph_cap *cap, *ocap, *new_cap = NULL;
3577 int mds = session->s_mds;
3578 int issued;
3579 unsigned caps = le32_to_cpu(im->caps);
3580 unsigned wanted = le32_to_cpu(im->wanted);
3581 unsigned seq = le32_to_cpu(im->seq);
3582 unsigned mseq = le32_to_cpu(im->migrate_seq);
3583 u64 realmino = le64_to_cpu(im->realm);
3584 u64 cap_id = le64_to_cpu(im->cap_id);
3585 u64 p_cap_id;
3586 int peer;
3588 if (ph) {
3589 p_cap_id = le64_to_cpu(ph->cap_id);
3590 peer = le32_to_cpu(ph->mds);
3591 } else {
3592 p_cap_id = 0;
3593 peer = -1;
3596 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3597 inode, ci, mds, mseq, peer);
3599 retry:
3600 spin_lock(&ci->i_ceph_lock);
3601 cap = __get_cap_for_mds(ci, mds);
3602 if (!cap) {
3603 if (!new_cap) {
3604 spin_unlock(&ci->i_ceph_lock);
3605 new_cap = ceph_get_cap(mdsc, NULL);
3606 goto retry;
3608 cap = new_cap;
3609 } else {
3610 if (new_cap) {
3611 ceph_put_cap(mdsc, new_cap);
3612 new_cap = NULL;
3616 __ceph_caps_issued(ci, &issued);
3617 issued |= __ceph_caps_dirty(ci);
3619 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
3620 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3622 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3623 if (ocap && ocap->cap_id == p_cap_id) {
3624 dout(" remove export cap %p mds%d flags %d\n",
3625 ocap, peer, ph->flags);
3626 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
3627 (ocap->seq != le32_to_cpu(ph->seq) ||
3628 ocap->mseq != le32_to_cpu(ph->mseq))) {
3629 pr_err_ratelimited("handle_cap_import: "
3630 "mismatched seq/mseq: ino (%llx.%llx) "
3631 "mds%d seq %d mseq %d importer mds%d "
3632 "has peer seq %d mseq %d\n",
3633 ceph_vinop(inode), peer, ocap->seq,
3634 ocap->mseq, mds, le32_to_cpu(ph->seq),
3635 le32_to_cpu(ph->mseq));
3637 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
3640 /* make sure we re-request max_size, if necessary */
3641 ci->i_requested_max_size = 0;
3643 *old_issued = issued;
3644 *target_cap = cap;
3648 * Handle a caps message from the MDS.
3650 * Identify the appropriate session, inode, and call the right handler
3651 * based on the cap op.
3653 void ceph_handle_caps(struct ceph_mds_session *session,
3654 struct ceph_msg *msg)
3656 struct ceph_mds_client *mdsc = session->s_mdsc;
3657 struct super_block *sb = mdsc->fsc->sb;
3658 struct inode *inode;
3659 struct ceph_inode_info *ci;
3660 struct ceph_cap *cap;
3661 struct ceph_mds_caps *h;
3662 struct ceph_mds_cap_peer *peer = NULL;
3663 struct ceph_snap_realm *realm = NULL;
3664 struct ceph_string *pool_ns = NULL;
3665 int mds = session->s_mds;
3666 int op, issued;
3667 u32 seq, mseq;
3668 struct ceph_vino vino;
3669 u64 tid;
3670 u64 inline_version = 0;
3671 void *inline_data = NULL;
3672 u32 inline_len = 0;
3673 void *snaptrace;
3674 size_t snaptrace_len;
3675 void *p, *end;
3677 dout("handle_caps from mds%d\n", mds);
3679 /* decode */
3680 end = msg->front.iov_base + msg->front.iov_len;
3681 tid = le64_to_cpu(msg->hdr.tid);
3682 if (msg->front.iov_len < sizeof(*h))
3683 goto bad;
3684 h = msg->front.iov_base;
3685 op = le32_to_cpu(h->op);
3686 vino.ino = le64_to_cpu(h->ino);
3687 vino.snap = CEPH_NOSNAP;
3688 seq = le32_to_cpu(h->seq);
3689 mseq = le32_to_cpu(h->migrate_seq);
3691 snaptrace = h + 1;
3692 snaptrace_len = le32_to_cpu(h->snap_trace_len);
3693 p = snaptrace + snaptrace_len;
3695 if (le16_to_cpu(msg->hdr.version) >= 2) {
3696 u32 flock_len;
3697 ceph_decode_32_safe(&p, end, flock_len, bad);
3698 if (p + flock_len > end)
3699 goto bad;
3700 p += flock_len;
3703 if (le16_to_cpu(msg->hdr.version) >= 3) {
3704 if (op == CEPH_CAP_OP_IMPORT) {
3705 if (p + sizeof(*peer) > end)
3706 goto bad;
3707 peer = p;
3708 p += sizeof(*peer);
3709 } else if (op == CEPH_CAP_OP_EXPORT) {
3710 /* recorded in unused fields */
3711 peer = (void *)&h->size;
3715 if (le16_to_cpu(msg->hdr.version) >= 4) {
3716 ceph_decode_64_safe(&p, end, inline_version, bad);
3717 ceph_decode_32_safe(&p, end, inline_len, bad);
3718 if (p + inline_len > end)
3719 goto bad;
3720 inline_data = p;
3721 p += inline_len;
3724 if (le16_to_cpu(msg->hdr.version) >= 5) {
3725 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3726 u32 epoch_barrier;
3728 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3729 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3732 if (le16_to_cpu(msg->hdr.version) >= 8) {
3733 u64 flush_tid;
3734 u32 caller_uid, caller_gid;
3735 u32 pool_ns_len;
3737 /* version >= 6 */
3738 ceph_decode_64_safe(&p, end, flush_tid, bad);
3739 /* version >= 7 */
3740 ceph_decode_32_safe(&p, end, caller_uid, bad);
3741 ceph_decode_32_safe(&p, end, caller_gid, bad);
3742 /* version >= 8 */
3743 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
3744 if (pool_ns_len > 0) {
3745 ceph_decode_need(&p, end, pool_ns_len, bad);
3746 pool_ns = ceph_find_or_create_string(p, pool_ns_len);
3747 p += pool_ns_len;
3751 /* lookup ino */
3752 inode = ceph_find_inode(sb, vino);
3753 ci = ceph_inode(inode);
3754 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3755 vino.snap, inode);
3757 mutex_lock(&session->s_mutex);
3758 session->s_seq++;
3759 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3760 (unsigned)seq);
3762 if (!inode) {
3763 dout(" i don't have ino %llx\n", vino.ino);
3765 if (op == CEPH_CAP_OP_IMPORT) {
3766 cap = ceph_get_cap(mdsc, NULL);
3767 cap->cap_ino = vino.ino;
3768 cap->queue_release = 1;
3769 cap->cap_id = le64_to_cpu(h->cap_id);
3770 cap->mseq = mseq;
3771 cap->seq = seq;
3772 cap->issue_seq = seq;
3773 spin_lock(&session->s_cap_lock);
3774 list_add_tail(&cap->session_caps,
3775 &session->s_cap_releases);
3776 session->s_num_cap_releases++;
3777 spin_unlock(&session->s_cap_lock);
3779 goto flush_cap_releases;
3782 /* these will work even if we don't have a cap yet */
3783 switch (op) {
3784 case CEPH_CAP_OP_FLUSHSNAP_ACK:
3785 handle_cap_flushsnap_ack(inode, tid, h, session);
3786 goto done;
3788 case CEPH_CAP_OP_EXPORT:
3789 handle_cap_export(inode, h, peer, session);
3790 goto done_unlocked;
3792 case CEPH_CAP_OP_IMPORT:
3793 realm = NULL;
3794 if (snaptrace_len) {
3795 down_write(&mdsc->snap_rwsem);
3796 ceph_update_snap_trace(mdsc, snaptrace,
3797 snaptrace + snaptrace_len,
3798 false, &realm);
3799 downgrade_write(&mdsc->snap_rwsem);
3800 } else {
3801 down_read(&mdsc->snap_rwsem);
3803 handle_cap_import(mdsc, inode, h, peer, session,
3804 &cap, &issued);
3805 handle_cap_grant(mdsc, inode, h, &pool_ns,
3806 inline_version, inline_data, inline_len,
3807 msg->middle, session, cap, issued);
3808 if (realm)
3809 ceph_put_snap_realm(mdsc, realm);
3810 goto done_unlocked;
3813 /* the rest require a cap */
3814 spin_lock(&ci->i_ceph_lock);
3815 cap = __get_cap_for_mds(ceph_inode(inode), mds);
3816 if (!cap) {
3817 dout(" no cap on %p ino %llx.%llx from mds%d\n",
3818 inode, ceph_ino(inode), ceph_snap(inode), mds);
3819 spin_unlock(&ci->i_ceph_lock);
3820 goto flush_cap_releases;
3823 /* note that each of these drops i_ceph_lock for us */
3824 switch (op) {
3825 case CEPH_CAP_OP_REVOKE:
3826 case CEPH_CAP_OP_GRANT:
3827 __ceph_caps_issued(ci, &issued);
3828 issued |= __ceph_caps_dirty(ci);
3829 handle_cap_grant(mdsc, inode, h, &pool_ns,
3830 inline_version, inline_data, inline_len,
3831 msg->middle, session, cap, issued);
3832 goto done_unlocked;
3834 case CEPH_CAP_OP_FLUSH_ACK:
3835 handle_cap_flush_ack(inode, tid, h, session, cap);
3836 break;
3838 case CEPH_CAP_OP_TRUNC:
3839 handle_cap_trunc(inode, h, session);
3840 break;
3842 default:
3843 spin_unlock(&ci->i_ceph_lock);
3844 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3845 ceph_cap_op_name(op));
3848 goto done;
3850 flush_cap_releases:
3852 * send any cap release message to try to move things
3853 * along for the mds (who clearly thinks we still have this
3854 * cap).
3856 ceph_send_cap_releases(mdsc, session);
3858 done:
3859 mutex_unlock(&session->s_mutex);
3860 done_unlocked:
3861 iput(inode);
3862 ceph_put_string(pool_ns);
3863 return;
3865 bad:
3866 pr_err("ceph_handle_caps: corrupt message\n");
3867 ceph_msg_dump(msg);
3868 return;
3872 * Delayed work handler to process end of delayed cap release LRU list.
3874 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
3876 struct inode *inode;
3877 struct ceph_inode_info *ci;
3878 int flags = CHECK_CAPS_NODELAY;
3880 dout("check_delayed_caps\n");
3881 while (1) {
3882 spin_lock(&mdsc->cap_delay_lock);
3883 if (list_empty(&mdsc->cap_delay_list))
3884 break;
3885 ci = list_first_entry(&mdsc->cap_delay_list,
3886 struct ceph_inode_info,
3887 i_cap_delay_list);
3888 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3889 time_before(jiffies, ci->i_hold_caps_max))
3890 break;
3891 list_del_init(&ci->i_cap_delay_list);
3893 inode = igrab(&ci->vfs_inode);
3894 spin_unlock(&mdsc->cap_delay_lock);
3896 if (inode) {
3897 dout("check_delayed_caps on %p\n", inode);
3898 ceph_check_caps(ci, flags, NULL);
3899 iput(inode);
3902 spin_unlock(&mdsc->cap_delay_lock);
3906 * Flush all dirty caps to the mds
3908 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
3910 struct ceph_inode_info *ci;
3911 struct inode *inode;
3913 dout("flush_dirty_caps\n");
3914 spin_lock(&mdsc->cap_dirty_lock);
3915 while (!list_empty(&mdsc->cap_dirty)) {
3916 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
3917 i_dirty_item);
3918 inode = &ci->vfs_inode;
3919 ihold(inode);
3920 dout("flush_dirty_caps %p\n", inode);
3921 spin_unlock(&mdsc->cap_dirty_lock);
3922 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
3923 iput(inode);
3924 spin_lock(&mdsc->cap_dirty_lock);
3926 spin_unlock(&mdsc->cap_dirty_lock);
3927 dout("flush_dirty_caps done\n");
3930 void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
3932 int i;
3933 int bits = (fmode << 1) | 1;
3934 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3935 if (bits & (1 << i))
3936 ci->i_nr_by_mode[i]++;
3941 * Drop open file reference. If we were the last open file,
3942 * we may need to release capabilities to the MDS (or schedule
3943 * their delayed release).
3945 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
3947 int i, last = 0;
3948 int bits = (fmode << 1) | 1;
3949 spin_lock(&ci->i_ceph_lock);
3950 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3951 if (bits & (1 << i)) {
3952 BUG_ON(ci->i_nr_by_mode[i] == 0);
3953 if (--ci->i_nr_by_mode[i] == 0)
3954 last++;
3957 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
3958 &ci->vfs_inode, fmode,
3959 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
3960 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
3961 spin_unlock(&ci->i_ceph_lock);
3963 if (last && ci->i_vino.snap == CEPH_NOSNAP)
3964 ceph_check_caps(ci, 0, NULL);
3968 * Helpers for embedding cap and dentry lease releases into mds
3969 * requests.
3971 * @force is used by dentry_release (below) to force inclusion of a
3972 * record for the directory inode, even when there aren't any caps to
3973 * drop.
3975 int ceph_encode_inode_release(void **p, struct inode *inode,
3976 int mds, int drop, int unless, int force)
3978 struct ceph_inode_info *ci = ceph_inode(inode);
3979 struct ceph_cap *cap;
3980 struct ceph_mds_request_release *rel = *p;
3981 int used, dirty;
3982 int ret = 0;
3984 spin_lock(&ci->i_ceph_lock);
3985 used = __ceph_caps_used(ci);
3986 dirty = __ceph_caps_dirty(ci);
3988 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3989 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
3990 ceph_cap_string(unless));
3992 /* only drop unused, clean caps */
3993 drop &= ~(used | dirty);
3995 cap = __get_cap_for_mds(ci, mds);
3996 if (cap && __cap_is_valid(cap)) {
3997 unless &= cap->issued;
3998 if (unless) {
3999 if (unless & CEPH_CAP_AUTH_EXCL)
4000 drop &= ~CEPH_CAP_AUTH_SHARED;
4001 if (unless & CEPH_CAP_LINK_EXCL)
4002 drop &= ~CEPH_CAP_LINK_SHARED;
4003 if (unless & CEPH_CAP_XATTR_EXCL)
4004 drop &= ~CEPH_CAP_XATTR_SHARED;
4005 if (unless & CEPH_CAP_FILE_EXCL)
4006 drop &= ~CEPH_CAP_FILE_SHARED;
4009 if (force || (cap->issued & drop)) {
4010 if (cap->issued & drop) {
4011 int wanted = __ceph_caps_wanted(ci);
4012 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4013 wanted |= cap->mds_wanted;
4014 dout("encode_inode_release %p cap %p "
4015 "%s -> %s, wanted %s -> %s\n", inode, cap,
4016 ceph_cap_string(cap->issued),
4017 ceph_cap_string(cap->issued & ~drop),
4018 ceph_cap_string(cap->mds_wanted),
4019 ceph_cap_string(wanted));
4021 cap->issued &= ~drop;
4022 cap->implemented &= ~drop;
4023 cap->mds_wanted = wanted;
4024 } else {
4025 dout("encode_inode_release %p cap %p %s"
4026 " (force)\n", inode, cap,
4027 ceph_cap_string(cap->issued));
4030 rel->ino = cpu_to_le64(ceph_ino(inode));
4031 rel->cap_id = cpu_to_le64(cap->cap_id);
4032 rel->seq = cpu_to_le32(cap->seq);
4033 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4034 rel->mseq = cpu_to_le32(cap->mseq);
4035 rel->caps = cpu_to_le32(cap->implemented);
4036 rel->wanted = cpu_to_le32(cap->mds_wanted);
4037 rel->dname_len = 0;
4038 rel->dname_seq = 0;
4039 *p += sizeof(*rel);
4040 ret = 1;
4041 } else {
4042 dout("encode_inode_release %p cap %p %s (noop)\n",
4043 inode, cap, ceph_cap_string(cap->issued));
4046 spin_unlock(&ci->i_ceph_lock);
4047 return ret;
4050 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4051 struct inode *dir,
4052 int mds, int drop, int unless)
4054 struct dentry *parent = NULL;
4055 struct ceph_mds_request_release *rel = *p;
4056 struct ceph_dentry_info *di = ceph_dentry(dentry);
4057 int force = 0;
4058 int ret;
4061 * force an record for the directory caps if we have a dentry lease.
4062 * this is racy (can't take i_ceph_lock and d_lock together), but it
4063 * doesn't have to be perfect; the mds will revoke anything we don't
4064 * release.
4066 spin_lock(&dentry->d_lock);
4067 if (di->lease_session && di->lease_session->s_mds == mds)
4068 force = 1;
4069 if (!dir) {
4070 parent = dget(dentry->d_parent);
4071 dir = d_inode(parent);
4073 spin_unlock(&dentry->d_lock);
4075 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4076 dput(parent);
4078 spin_lock(&dentry->d_lock);
4079 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4080 dout("encode_dentry_release %p mds%d seq %d\n",
4081 dentry, mds, (int)di->lease_seq);
4082 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4083 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4084 *p += dentry->d_name.len;
4085 rel->dname_seq = cpu_to_le32(di->lease_seq);
4086 __ceph_mdsc_drop_dentry_lease(dentry);
4088 spin_unlock(&dentry->d_lock);
4089 return ret;