Linux 4.19.133
[linux/fpc-iii.git] / fs / ceph / caps.c
bloba2d4eed27f804c0c51e4ea9b718387946e92fa6c
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_GWREXTEND)
73 *s++ = 'a';
74 if (c & CEPH_CAP_GLAZYIO)
75 *s++ = 'l';
76 return s;
79 const char *ceph_cap_string(int caps)
81 int i;
82 char *s;
83 int c;
85 spin_lock(&cap_str_lock);
86 i = last_cap_str++;
87 if (last_cap_str == MAX_CAP_STR)
88 last_cap_str = 0;
89 spin_unlock(&cap_str_lock);
91 s = cap_str[i];
93 if (caps & CEPH_CAP_PIN)
94 *s++ = 'p';
96 c = (caps >> CEPH_CAP_SAUTH) & 3;
97 if (c) {
98 *s++ = 'A';
99 s = gcap_string(s, c);
102 c = (caps >> CEPH_CAP_SLINK) & 3;
103 if (c) {
104 *s++ = 'L';
105 s = gcap_string(s, c);
108 c = (caps >> CEPH_CAP_SXATTR) & 3;
109 if (c) {
110 *s++ = 'X';
111 s = gcap_string(s, c);
114 c = caps >> CEPH_CAP_SFILE;
115 if (c) {
116 *s++ = 'F';
117 s = gcap_string(s, c);
120 if (s == cap_str[i])
121 *s++ = '-';
122 *s = 0;
123 return cap_str[i];
126 void ceph_caps_init(struct ceph_mds_client *mdsc)
128 INIT_LIST_HEAD(&mdsc->caps_list);
129 spin_lock_init(&mdsc->caps_list_lock);
132 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
134 struct ceph_cap *cap;
136 spin_lock(&mdsc->caps_list_lock);
137 while (!list_empty(&mdsc->caps_list)) {
138 cap = list_first_entry(&mdsc->caps_list,
139 struct ceph_cap, caps_item);
140 list_del(&cap->caps_item);
141 kmem_cache_free(ceph_cap_cachep, cap);
143 mdsc->caps_total_count = 0;
144 mdsc->caps_avail_count = 0;
145 mdsc->caps_use_count = 0;
146 mdsc->caps_reserve_count = 0;
147 mdsc->caps_min_count = 0;
148 spin_unlock(&mdsc->caps_list_lock);
151 void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
153 spin_lock(&mdsc->caps_list_lock);
154 mdsc->caps_min_count += delta;
155 BUG_ON(mdsc->caps_min_count < 0);
156 spin_unlock(&mdsc->caps_list_lock);
159 static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
161 struct ceph_cap *cap;
162 int i;
164 if (nr_caps) {
165 BUG_ON(mdsc->caps_reserve_count < nr_caps);
166 mdsc->caps_reserve_count -= nr_caps;
167 if (mdsc->caps_avail_count >=
168 mdsc->caps_reserve_count + mdsc->caps_min_count) {
169 mdsc->caps_total_count -= nr_caps;
170 for (i = 0; i < nr_caps; i++) {
171 cap = list_first_entry(&mdsc->caps_list,
172 struct ceph_cap, caps_item);
173 list_del(&cap->caps_item);
174 kmem_cache_free(ceph_cap_cachep, cap);
176 } else {
177 mdsc->caps_avail_count += nr_caps;
180 dout("%s: caps %d = %d used + %d resv + %d avail\n",
181 __func__,
182 mdsc->caps_total_count, mdsc->caps_use_count,
183 mdsc->caps_reserve_count, mdsc->caps_avail_count);
184 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
185 mdsc->caps_reserve_count +
186 mdsc->caps_avail_count);
191 * Called under mdsc->mutex.
193 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
194 struct ceph_cap_reservation *ctx, int need)
196 int i, j;
197 struct ceph_cap *cap;
198 int have;
199 int alloc = 0;
200 int max_caps;
201 int err = 0;
202 bool trimmed = false;
203 struct ceph_mds_session *s;
204 LIST_HEAD(newcaps);
206 dout("reserve caps ctx=%p need=%d\n", ctx, need);
208 /* first reserve any caps that are already allocated */
209 spin_lock(&mdsc->caps_list_lock);
210 if (mdsc->caps_avail_count >= need)
211 have = need;
212 else
213 have = mdsc->caps_avail_count;
214 mdsc->caps_avail_count -= have;
215 mdsc->caps_reserve_count += have;
216 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
217 mdsc->caps_reserve_count +
218 mdsc->caps_avail_count);
219 spin_unlock(&mdsc->caps_list_lock);
221 for (i = have; i < need; ) {
222 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
223 if (cap) {
224 list_add(&cap->caps_item, &newcaps);
225 alloc++;
226 i++;
227 continue;
230 if (!trimmed) {
231 for (j = 0; j < mdsc->max_sessions; j++) {
232 s = __ceph_lookup_mds_session(mdsc, j);
233 if (!s)
234 continue;
235 mutex_unlock(&mdsc->mutex);
237 mutex_lock(&s->s_mutex);
238 max_caps = s->s_nr_caps - (need - i);
239 ceph_trim_caps(mdsc, s, max_caps);
240 mutex_unlock(&s->s_mutex);
242 ceph_put_mds_session(s);
243 mutex_lock(&mdsc->mutex);
245 trimmed = true;
247 spin_lock(&mdsc->caps_list_lock);
248 if (mdsc->caps_avail_count) {
249 int more_have;
250 if (mdsc->caps_avail_count >= need - i)
251 more_have = need - i;
252 else
253 more_have = mdsc->caps_avail_count;
255 i += more_have;
256 have += more_have;
257 mdsc->caps_avail_count -= more_have;
258 mdsc->caps_reserve_count += more_have;
261 spin_unlock(&mdsc->caps_list_lock);
263 continue;
266 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
267 ctx, need, have + alloc);
268 err = -ENOMEM;
269 break;
272 if (!err) {
273 BUG_ON(have + alloc != need);
274 ctx->count = need;
277 spin_lock(&mdsc->caps_list_lock);
278 mdsc->caps_total_count += alloc;
279 mdsc->caps_reserve_count += alloc;
280 list_splice(&newcaps, &mdsc->caps_list);
282 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
283 mdsc->caps_reserve_count +
284 mdsc->caps_avail_count);
286 if (err)
287 __ceph_unreserve_caps(mdsc, have + alloc);
289 spin_unlock(&mdsc->caps_list_lock);
291 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
292 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
293 mdsc->caps_reserve_count, mdsc->caps_avail_count);
294 return err;
297 void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
298 struct ceph_cap_reservation *ctx)
300 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
301 spin_lock(&mdsc->caps_list_lock);
302 __ceph_unreserve_caps(mdsc, ctx->count);
303 ctx->count = 0;
304 spin_unlock(&mdsc->caps_list_lock);
307 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
308 struct ceph_cap_reservation *ctx)
310 struct ceph_cap *cap = NULL;
312 /* temporary, until we do something about cap import/export */
313 if (!ctx) {
314 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
315 if (cap) {
316 spin_lock(&mdsc->caps_list_lock);
317 mdsc->caps_use_count++;
318 mdsc->caps_total_count++;
319 spin_unlock(&mdsc->caps_list_lock);
320 } else {
321 spin_lock(&mdsc->caps_list_lock);
322 if (mdsc->caps_avail_count) {
323 BUG_ON(list_empty(&mdsc->caps_list));
325 mdsc->caps_avail_count--;
326 mdsc->caps_use_count++;
327 cap = list_first_entry(&mdsc->caps_list,
328 struct ceph_cap, caps_item);
329 list_del(&cap->caps_item);
331 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
332 mdsc->caps_reserve_count + mdsc->caps_avail_count);
334 spin_unlock(&mdsc->caps_list_lock);
337 return cap;
340 spin_lock(&mdsc->caps_list_lock);
341 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
342 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
343 mdsc->caps_reserve_count, mdsc->caps_avail_count);
344 BUG_ON(!ctx->count);
345 BUG_ON(ctx->count > mdsc->caps_reserve_count);
346 BUG_ON(list_empty(&mdsc->caps_list));
348 ctx->count--;
349 mdsc->caps_reserve_count--;
350 mdsc->caps_use_count++;
352 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
353 list_del(&cap->caps_item);
355 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
356 mdsc->caps_reserve_count + mdsc->caps_avail_count);
357 spin_unlock(&mdsc->caps_list_lock);
358 return cap;
361 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
363 spin_lock(&mdsc->caps_list_lock);
364 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
365 cap, mdsc->caps_total_count, mdsc->caps_use_count,
366 mdsc->caps_reserve_count, mdsc->caps_avail_count);
367 mdsc->caps_use_count--;
369 * Keep some preallocated caps around (ceph_min_count), to
370 * avoid lots of free/alloc churn.
372 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
373 mdsc->caps_min_count) {
374 mdsc->caps_total_count--;
375 kmem_cache_free(ceph_cap_cachep, cap);
376 } else {
377 mdsc->caps_avail_count++;
378 list_add(&cap->caps_item, &mdsc->caps_list);
381 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
382 mdsc->caps_reserve_count + mdsc->caps_avail_count);
383 spin_unlock(&mdsc->caps_list_lock);
386 void ceph_reservation_status(struct ceph_fs_client *fsc,
387 int *total, int *avail, int *used, int *reserved,
388 int *min)
390 struct ceph_mds_client *mdsc = fsc->mdsc;
392 spin_lock(&mdsc->caps_list_lock);
394 if (total)
395 *total = mdsc->caps_total_count;
396 if (avail)
397 *avail = mdsc->caps_avail_count;
398 if (used)
399 *used = mdsc->caps_use_count;
400 if (reserved)
401 *reserved = mdsc->caps_reserve_count;
402 if (min)
403 *min = mdsc->caps_min_count;
405 spin_unlock(&mdsc->caps_list_lock);
409 * Find ceph_cap for given mds, if any.
411 * Called with i_ceph_lock held.
413 static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
415 struct ceph_cap *cap;
416 struct rb_node *n = ci->i_caps.rb_node;
418 while (n) {
419 cap = rb_entry(n, struct ceph_cap, ci_node);
420 if (mds < cap->mds)
421 n = n->rb_left;
422 else if (mds > cap->mds)
423 n = n->rb_right;
424 else
425 return cap;
427 return NULL;
430 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
432 struct ceph_cap *cap;
434 spin_lock(&ci->i_ceph_lock);
435 cap = __get_cap_for_mds(ci, mds);
436 spin_unlock(&ci->i_ceph_lock);
437 return cap;
441 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
443 static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
445 struct ceph_cap *cap;
446 int mds = -1;
447 struct rb_node *p;
449 /* prefer mds with WR|BUFFER|EXCL caps */
450 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
451 cap = rb_entry(p, struct ceph_cap, ci_node);
452 mds = cap->mds;
453 if (cap->issued & (CEPH_CAP_FILE_WR |
454 CEPH_CAP_FILE_BUFFER |
455 CEPH_CAP_FILE_EXCL))
456 break;
458 return mds;
461 int ceph_get_cap_mds(struct inode *inode)
463 struct ceph_inode_info *ci = ceph_inode(inode);
464 int mds;
465 spin_lock(&ci->i_ceph_lock);
466 mds = __ceph_get_cap_mds(ceph_inode(inode));
467 spin_unlock(&ci->i_ceph_lock);
468 return mds;
472 * Called under i_ceph_lock.
474 static void __insert_cap_node(struct ceph_inode_info *ci,
475 struct ceph_cap *new)
477 struct rb_node **p = &ci->i_caps.rb_node;
478 struct rb_node *parent = NULL;
479 struct ceph_cap *cap = NULL;
481 while (*p) {
482 parent = *p;
483 cap = rb_entry(parent, struct ceph_cap, ci_node);
484 if (new->mds < cap->mds)
485 p = &(*p)->rb_left;
486 else if (new->mds > cap->mds)
487 p = &(*p)->rb_right;
488 else
489 BUG();
492 rb_link_node(&new->ci_node, parent, p);
493 rb_insert_color(&new->ci_node, &ci->i_caps);
497 * (re)set cap hold timeouts, which control the delayed release
498 * of unused caps back to the MDS. Should be called on cap use.
500 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
501 struct ceph_inode_info *ci)
503 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
505 ci->i_hold_caps_min = round_jiffies(jiffies +
506 ma->caps_wanted_delay_min * HZ);
507 ci->i_hold_caps_max = round_jiffies(jiffies +
508 ma->caps_wanted_delay_max * HZ);
509 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
510 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
514 * (Re)queue cap at the end of the delayed cap release list.
516 * If I_FLUSH is set, leave the inode at the front of the list.
518 * Caller holds i_ceph_lock
519 * -> we take mdsc->cap_delay_lock
521 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
522 struct ceph_inode_info *ci)
524 __cap_set_timeouts(mdsc, ci);
525 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
526 ci->i_ceph_flags, ci->i_hold_caps_max);
527 if (!mdsc->stopping) {
528 spin_lock(&mdsc->cap_delay_lock);
529 if (!list_empty(&ci->i_cap_delay_list)) {
530 if (ci->i_ceph_flags & CEPH_I_FLUSH)
531 goto no_change;
532 list_del_init(&ci->i_cap_delay_list);
534 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
535 no_change:
536 spin_unlock(&mdsc->cap_delay_lock);
541 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
542 * indicating we should send a cap message to flush dirty metadata
543 * asap, and move to the front of the delayed cap list.
545 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
546 struct ceph_inode_info *ci)
548 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
549 spin_lock(&mdsc->cap_delay_lock);
550 ci->i_ceph_flags |= CEPH_I_FLUSH;
551 if (!list_empty(&ci->i_cap_delay_list))
552 list_del_init(&ci->i_cap_delay_list);
553 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
554 spin_unlock(&mdsc->cap_delay_lock);
558 * Cancel delayed work on cap.
560 * Caller must hold i_ceph_lock.
562 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
563 struct ceph_inode_info *ci)
565 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
566 if (list_empty(&ci->i_cap_delay_list))
567 return;
568 spin_lock(&mdsc->cap_delay_lock);
569 list_del_init(&ci->i_cap_delay_list);
570 spin_unlock(&mdsc->cap_delay_lock);
574 * Common issue checks for add_cap, handle_cap_grant.
576 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
577 unsigned issued)
579 unsigned had = __ceph_caps_issued(ci, NULL);
582 * Each time we receive FILE_CACHE anew, we increment
583 * i_rdcache_gen.
585 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
586 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
587 ci->i_rdcache_gen++;
591 * If FILE_SHARED is newly issued, mark dir not complete. We don't
592 * know what happened to this directory while we didn't have the cap.
593 * If FILE_SHARED is being revoked, also mark dir not complete. It
594 * stops on-going cached readdir.
596 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
597 if (issued & CEPH_CAP_FILE_SHARED)
598 atomic_inc(&ci->i_shared_gen);
599 if (S_ISDIR(ci->vfs_inode.i_mode)) {
600 dout(" marking %p NOT complete\n", &ci->vfs_inode);
601 __ceph_dir_clear_complete(ci);
607 * Add a capability under the given MDS session.
609 * Caller should hold session snap_rwsem (read) and s_mutex.
611 * @fmode is the open file mode, if we are opening a file, otherwise
612 * it is < 0. (This is so we can atomically add the cap and add an
613 * open file reference to it.)
615 void ceph_add_cap(struct inode *inode,
616 struct ceph_mds_session *session, u64 cap_id,
617 int fmode, unsigned issued, unsigned wanted,
618 unsigned seq, unsigned mseq, u64 realmino, int flags,
619 struct ceph_cap **new_cap)
621 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
622 struct ceph_inode_info *ci = ceph_inode(inode);
623 struct ceph_cap *cap;
624 int mds = session->s_mds;
625 int actual_wanted;
627 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
628 session->s_mds, cap_id, ceph_cap_string(issued), seq);
631 * If we are opening the file, include file mode wanted bits
632 * in wanted.
634 if (fmode >= 0)
635 wanted |= ceph_caps_for_mode(fmode);
637 cap = __get_cap_for_mds(ci, mds);
638 if (!cap) {
639 cap = *new_cap;
640 *new_cap = NULL;
642 cap->issued = 0;
643 cap->implemented = 0;
644 cap->mds = mds;
645 cap->mds_wanted = 0;
646 cap->mseq = 0;
648 cap->ci = ci;
649 __insert_cap_node(ci, cap);
651 /* add to session cap list */
652 cap->session = session;
653 spin_lock(&session->s_cap_lock);
654 list_add_tail(&cap->session_caps, &session->s_caps);
655 session->s_nr_caps++;
656 spin_unlock(&session->s_cap_lock);
657 } else {
659 * auth mds of the inode changed. we received the cap export
660 * message, but still haven't received the cap import message.
661 * handle_cap_export() updated the new auth MDS' cap.
663 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
664 * a message that was send before the cap import message. So
665 * don't remove caps.
667 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
668 WARN_ON(cap != ci->i_auth_cap);
669 WARN_ON(cap->cap_id != cap_id);
670 seq = cap->seq;
671 mseq = cap->mseq;
672 issued |= cap->issued;
673 flags |= CEPH_CAP_FLAG_AUTH;
677 if (!ci->i_snap_realm ||
678 ((flags & CEPH_CAP_FLAG_AUTH) &&
679 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
681 * add this inode to the appropriate snap realm
683 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
684 realmino);
685 if (realm) {
686 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
687 if (oldrealm) {
688 spin_lock(&oldrealm->inodes_with_caps_lock);
689 list_del_init(&ci->i_snap_realm_item);
690 spin_unlock(&oldrealm->inodes_with_caps_lock);
693 spin_lock(&realm->inodes_with_caps_lock);
694 list_add(&ci->i_snap_realm_item,
695 &realm->inodes_with_caps);
696 ci->i_snap_realm = realm;
697 if (realm->ino == ci->i_vino.ino)
698 realm->inode = inode;
699 spin_unlock(&realm->inodes_with_caps_lock);
701 if (oldrealm)
702 ceph_put_snap_realm(mdsc, oldrealm);
703 } else {
704 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
705 realmino);
706 WARN_ON(!realm);
710 __check_cap_issue(ci, cap, issued);
713 * If we are issued caps we don't want, or the mds' wanted
714 * value appears to be off, queue a check so we'll release
715 * later and/or update the mds wanted value.
717 actual_wanted = __ceph_caps_wanted(ci);
718 if ((wanted & ~actual_wanted) ||
719 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
720 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
721 ceph_cap_string(issued), ceph_cap_string(wanted),
722 ceph_cap_string(actual_wanted));
723 __cap_delay_requeue(mdsc, ci);
726 if (flags & CEPH_CAP_FLAG_AUTH) {
727 if (!ci->i_auth_cap ||
728 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
729 ci->i_auth_cap = cap;
730 cap->mds_wanted = wanted;
732 } else {
733 WARN_ON(ci->i_auth_cap == cap);
736 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
737 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
738 ceph_cap_string(issued|cap->issued), seq, mds);
739 cap->cap_id = cap_id;
740 cap->issued = issued;
741 cap->implemented |= issued;
742 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
743 cap->mds_wanted = wanted;
744 else
745 cap->mds_wanted |= wanted;
746 cap->seq = seq;
747 cap->issue_seq = seq;
748 cap->mseq = mseq;
749 cap->cap_gen = session->s_cap_gen;
751 if (fmode >= 0)
752 __ceph_get_fmode(ci, fmode);
756 * Return true if cap has not timed out and belongs to the current
757 * generation of the MDS session (i.e. has not gone 'stale' due to
758 * us losing touch with the mds).
760 static int __cap_is_valid(struct ceph_cap *cap)
762 unsigned long ttl;
763 u32 gen;
765 spin_lock(&cap->session->s_gen_ttl_lock);
766 gen = cap->session->s_cap_gen;
767 ttl = cap->session->s_cap_ttl;
768 spin_unlock(&cap->session->s_gen_ttl_lock);
770 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
771 dout("__cap_is_valid %p cap %p issued %s "
772 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
773 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
774 return 0;
777 return 1;
781 * Return set of valid cap bits issued to us. Note that caps time
782 * out, and may be invalidated in bulk if the client session times out
783 * and session->s_cap_gen is bumped.
785 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
787 int have = ci->i_snap_caps;
788 struct ceph_cap *cap;
789 struct rb_node *p;
791 if (implemented)
792 *implemented = 0;
793 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
794 cap = rb_entry(p, struct ceph_cap, ci_node);
795 if (!__cap_is_valid(cap))
796 continue;
797 dout("__ceph_caps_issued %p cap %p issued %s\n",
798 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
799 have |= cap->issued;
800 if (implemented)
801 *implemented |= cap->implemented;
804 * exclude caps issued by non-auth MDS, but are been revoking
805 * by the auth MDS. The non-auth MDS should be revoking/exporting
806 * these caps, but the message is delayed.
808 if (ci->i_auth_cap) {
809 cap = ci->i_auth_cap;
810 have &= ~cap->implemented | cap->issued;
812 return have;
816 * Get cap bits issued by caps other than @ocap
818 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
820 int have = ci->i_snap_caps;
821 struct ceph_cap *cap;
822 struct rb_node *p;
824 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
825 cap = rb_entry(p, struct ceph_cap, ci_node);
826 if (cap == ocap)
827 continue;
828 if (!__cap_is_valid(cap))
829 continue;
830 have |= cap->issued;
832 return have;
836 * Move a cap to the end of the LRU (oldest caps at list head, newest
837 * at list tail).
839 static void __touch_cap(struct ceph_cap *cap)
841 struct ceph_mds_session *s = cap->session;
843 spin_lock(&s->s_cap_lock);
844 if (!s->s_cap_iterator) {
845 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
846 s->s_mds);
847 list_move_tail(&cap->session_caps, &s->s_caps);
848 } else {
849 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
850 &cap->ci->vfs_inode, cap, s->s_mds);
852 spin_unlock(&s->s_cap_lock);
856 * Check if we hold the given mask. If so, move the cap(s) to the
857 * front of their respective LRUs. (This is the preferred way for
858 * callers to check for caps they want.)
860 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
862 struct ceph_cap *cap;
863 struct rb_node *p;
864 int have = ci->i_snap_caps;
866 if ((have & mask) == mask) {
867 dout("__ceph_caps_issued_mask %p snap issued %s"
868 " (mask %s)\n", &ci->vfs_inode,
869 ceph_cap_string(have),
870 ceph_cap_string(mask));
871 return 1;
874 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
875 cap = rb_entry(p, struct ceph_cap, ci_node);
876 if (!__cap_is_valid(cap))
877 continue;
878 if ((cap->issued & mask) == mask) {
879 dout("__ceph_caps_issued_mask %p cap %p issued %s"
880 " (mask %s)\n", &ci->vfs_inode, cap,
881 ceph_cap_string(cap->issued),
882 ceph_cap_string(mask));
883 if (touch)
884 __touch_cap(cap);
885 return 1;
888 /* does a combination of caps satisfy mask? */
889 have |= cap->issued;
890 if ((have & mask) == mask) {
891 dout("__ceph_caps_issued_mask %p combo issued %s"
892 " (mask %s)\n", &ci->vfs_inode,
893 ceph_cap_string(cap->issued),
894 ceph_cap_string(mask));
895 if (touch) {
896 struct rb_node *q;
898 /* touch this + preceding caps */
899 __touch_cap(cap);
900 for (q = rb_first(&ci->i_caps); q != p;
901 q = rb_next(q)) {
902 cap = rb_entry(q, struct ceph_cap,
903 ci_node);
904 if (!__cap_is_valid(cap))
905 continue;
906 __touch_cap(cap);
909 return 1;
913 return 0;
917 * Return true if mask caps are currently being revoked by an MDS.
919 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
920 struct ceph_cap *ocap, int mask)
922 struct ceph_cap *cap;
923 struct rb_node *p;
925 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
926 cap = rb_entry(p, struct ceph_cap, ci_node);
927 if (cap != ocap &&
928 (cap->implemented & ~cap->issued & mask))
929 return 1;
931 return 0;
934 int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
936 struct inode *inode = &ci->vfs_inode;
937 int ret;
939 spin_lock(&ci->i_ceph_lock);
940 ret = __ceph_caps_revoking_other(ci, NULL, mask);
941 spin_unlock(&ci->i_ceph_lock);
942 dout("ceph_caps_revoking %p %s = %d\n", inode,
943 ceph_cap_string(mask), ret);
944 return ret;
947 int __ceph_caps_used(struct ceph_inode_info *ci)
949 int used = 0;
950 if (ci->i_pin_ref)
951 used |= CEPH_CAP_PIN;
952 if (ci->i_rd_ref)
953 used |= CEPH_CAP_FILE_RD;
954 if (ci->i_rdcache_ref ||
955 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
956 ci->vfs_inode.i_data.nrpages))
957 used |= CEPH_CAP_FILE_CACHE;
958 if (ci->i_wr_ref)
959 used |= CEPH_CAP_FILE_WR;
960 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
961 used |= CEPH_CAP_FILE_BUFFER;
962 return used;
966 * wanted, by virtue of open file modes
968 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
970 int i, bits = 0;
971 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
972 if (ci->i_nr_by_mode[i])
973 bits |= 1 << i;
975 if (bits == 0)
976 return 0;
977 return ceph_caps_for_mode(bits >> 1);
981 * Return caps we have registered with the MDS(s) as 'wanted'.
983 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
985 struct ceph_cap *cap;
986 struct rb_node *p;
987 int mds_wanted = 0;
989 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
990 cap = rb_entry(p, struct ceph_cap, ci_node);
991 if (check && !__cap_is_valid(cap))
992 continue;
993 if (cap == ci->i_auth_cap)
994 mds_wanted |= cap->mds_wanted;
995 else
996 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
998 return mds_wanted;
1002 * called under i_ceph_lock
1004 static int __ceph_is_single_caps(struct ceph_inode_info *ci)
1006 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
1009 static int __ceph_is_any_caps(struct ceph_inode_info *ci)
1011 return !RB_EMPTY_ROOT(&ci->i_caps);
1014 int ceph_is_any_caps(struct inode *inode)
1016 struct ceph_inode_info *ci = ceph_inode(inode);
1017 int ret;
1019 spin_lock(&ci->i_ceph_lock);
1020 ret = __ceph_is_any_caps(ci);
1021 spin_unlock(&ci->i_ceph_lock);
1023 return ret;
1026 static void drop_inode_snap_realm(struct ceph_inode_info *ci)
1028 struct ceph_snap_realm *realm = ci->i_snap_realm;
1029 spin_lock(&realm->inodes_with_caps_lock);
1030 list_del_init(&ci->i_snap_realm_item);
1031 ci->i_snap_realm_counter++;
1032 ci->i_snap_realm = NULL;
1033 if (realm->ino == ci->i_vino.ino)
1034 realm->inode = NULL;
1035 spin_unlock(&realm->inodes_with_caps_lock);
1036 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
1037 realm);
1041 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1043 * caller should hold i_ceph_lock.
1044 * caller will not hold session s_mutex if called from destroy_inode.
1046 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1048 struct ceph_mds_session *session = cap->session;
1049 struct ceph_inode_info *ci = cap->ci;
1050 struct ceph_mds_client *mdsc =
1051 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1052 int removed = 0;
1054 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
1056 /* remove from inode's cap rbtree, and clear auth cap */
1057 rb_erase(&cap->ci_node, &ci->i_caps);
1058 if (ci->i_auth_cap == cap)
1059 ci->i_auth_cap = NULL;
1061 /* remove from session list */
1062 spin_lock(&session->s_cap_lock);
1063 if (session->s_cap_iterator == cap) {
1064 /* not yet, we are iterating over this very cap */
1065 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1066 cap, cap->session);
1067 } else {
1068 list_del_init(&cap->session_caps);
1069 session->s_nr_caps--;
1070 cap->session = NULL;
1071 removed = 1;
1073 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1074 cap->ci = NULL;
1077 * s_cap_reconnect is protected by s_cap_lock. no one changes
1078 * s_cap_gen while session is in the reconnect state.
1080 if (queue_release &&
1081 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1082 cap->queue_release = 1;
1083 if (removed) {
1084 list_add_tail(&cap->session_caps,
1085 &session->s_cap_releases);
1086 session->s_num_cap_releases++;
1087 removed = 0;
1089 } else {
1090 cap->queue_release = 0;
1092 cap->cap_ino = ci->i_vino.ino;
1094 spin_unlock(&session->s_cap_lock);
1096 if (removed)
1097 ceph_put_cap(mdsc, cap);
1099 /* when reconnect denied, we remove session caps forcibly,
1100 * i_wr_ref can be non-zero. If there are ongoing write,
1101 * keep i_snap_realm.
1103 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1104 drop_inode_snap_realm(ci);
1106 if (!__ceph_is_any_real_caps(ci))
1107 __cap_delay_cancel(mdsc, ci);
1110 struct cap_msg_args {
1111 struct ceph_mds_session *session;
1112 u64 ino, cid, follows;
1113 u64 flush_tid, oldest_flush_tid, size, max_size;
1114 u64 xattr_version;
1115 struct ceph_buffer *xattr_buf;
1116 struct timespec64 atime, mtime, ctime;
1117 int op, caps, wanted, dirty;
1118 u32 seq, issue_seq, mseq, time_warp_seq;
1119 u32 flags;
1120 kuid_t uid;
1121 kgid_t gid;
1122 umode_t mode;
1123 bool inline_data;
1127 * Build and send a cap message to the given MDS.
1129 * Caller should be holding s_mutex.
1131 static int send_cap_msg(struct cap_msg_args *arg)
1133 struct ceph_mds_caps *fc;
1134 struct ceph_msg *msg;
1135 void *p;
1136 size_t extra_len;
1137 struct timespec64 zerotime = {0};
1138 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
1140 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
1141 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1142 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1143 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1144 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1145 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1146 arg->mseq, arg->follows, arg->size, arg->max_size,
1147 arg->xattr_version,
1148 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1150 /* flock buffer size + inline version + inline data size +
1151 * osd_epoch_barrier + oldest_flush_tid */
1152 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
1153 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1154 GFP_NOFS, false);
1155 if (!msg)
1156 return -ENOMEM;
1158 msg->hdr.version = cpu_to_le16(10);
1159 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1161 fc = msg->front.iov_base;
1162 memset(fc, 0, sizeof(*fc));
1164 fc->cap_id = cpu_to_le64(arg->cid);
1165 fc->op = cpu_to_le32(arg->op);
1166 fc->seq = cpu_to_le32(arg->seq);
1167 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1168 fc->migrate_seq = cpu_to_le32(arg->mseq);
1169 fc->caps = cpu_to_le32(arg->caps);
1170 fc->wanted = cpu_to_le32(arg->wanted);
1171 fc->dirty = cpu_to_le32(arg->dirty);
1172 fc->ino = cpu_to_le64(arg->ino);
1173 fc->snap_follows = cpu_to_le64(arg->follows);
1175 fc->size = cpu_to_le64(arg->size);
1176 fc->max_size = cpu_to_le64(arg->max_size);
1177 ceph_encode_timespec64(&fc->mtime, &arg->mtime);
1178 ceph_encode_timespec64(&fc->atime, &arg->atime);
1179 ceph_encode_timespec64(&fc->ctime, &arg->ctime);
1180 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1182 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1183 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1184 fc->mode = cpu_to_le32(arg->mode);
1186 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1187 if (arg->xattr_buf) {
1188 msg->middle = ceph_buffer_get(arg->xattr_buf);
1189 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1190 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1193 p = fc + 1;
1194 /* flock buffer size (version 2) */
1195 ceph_encode_32(&p, 0);
1196 /* inline version (version 4) */
1197 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1198 /* inline data size */
1199 ceph_encode_32(&p, 0);
1201 * osd_epoch_barrier (version 5)
1202 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1203 * case it was recently changed
1205 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1206 /* oldest_flush_tid (version 6) */
1207 ceph_encode_64(&p, arg->oldest_flush_tid);
1210 * caller_uid/caller_gid (version 7)
1212 * Currently, we don't properly track which caller dirtied the caps
1213 * last, and force a flush of them when there is a conflict. For now,
1214 * just set this to 0:0, to emulate how the MDS has worked up to now.
1216 ceph_encode_32(&p, 0);
1217 ceph_encode_32(&p, 0);
1219 /* pool namespace (version 8) (mds always ignores this) */
1220 ceph_encode_32(&p, 0);
1223 * btime and change_attr (version 9)
1225 * We just zero these out for now, as the MDS ignores them unless
1226 * the requisite feature flags are set (which we don't do yet).
1228 ceph_encode_timespec64(p, &zerotime);
1229 p += sizeof(struct ceph_timespec);
1230 ceph_encode_64(&p, 0);
1232 /* Advisory flags (version 10) */
1233 ceph_encode_32(&p, arg->flags);
1235 ceph_con_send(&arg->session->s_con, msg);
1236 return 0;
1240 * Queue cap releases when an inode is dropped from our cache.
1242 void ceph_queue_caps_release(struct inode *inode)
1244 struct ceph_inode_info *ci = ceph_inode(inode);
1245 struct rb_node *p;
1247 /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
1248 * may call __ceph_caps_issued_mask() on a freeing inode. */
1249 spin_lock(&ci->i_ceph_lock);
1250 p = rb_first(&ci->i_caps);
1251 while (p) {
1252 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1253 p = rb_next(p);
1254 __ceph_remove_cap(cap, true);
1256 spin_unlock(&ci->i_ceph_lock);
1260 * Send a cap msg on the given inode. Update our caps state, then
1261 * drop i_ceph_lock and send the message.
1263 * Make note of max_size reported/requested from mds, revoked caps
1264 * that have now been implemented.
1266 * Make half-hearted attempt ot to invalidate page cache if we are
1267 * dropping RDCACHE. Note that this will leave behind locked pages
1268 * that we'll then need to deal with elsewhere.
1270 * Return non-zero if delayed release, or we experienced an error
1271 * such that the caller should requeue + retry later.
1273 * called with i_ceph_lock, then drops it.
1274 * caller should hold snap_rwsem (read), s_mutex.
1276 static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1277 int op, bool sync, int used, int want, int retain,
1278 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1279 __releases(cap->ci->i_ceph_lock)
1281 struct ceph_inode_info *ci = cap->ci;
1282 struct inode *inode = &ci->vfs_inode;
1283 struct ceph_buffer *old_blob = NULL;
1284 struct cap_msg_args arg;
1285 int held, revoking;
1286 int wake = 0;
1287 int delayed = 0;
1288 int ret;
1290 held = cap->issued | cap->implemented;
1291 revoking = cap->implemented & ~cap->issued;
1292 retain &= ~revoking;
1294 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1295 inode, cap, cap->session,
1296 ceph_cap_string(held), ceph_cap_string(held & retain),
1297 ceph_cap_string(revoking));
1298 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1300 arg.session = cap->session;
1302 /* don't release wanted unless we've waited a bit. */
1303 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1304 time_before(jiffies, ci->i_hold_caps_min)) {
1305 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1306 ceph_cap_string(cap->issued),
1307 ceph_cap_string(cap->issued & retain),
1308 ceph_cap_string(cap->mds_wanted),
1309 ceph_cap_string(want));
1310 want |= cap->mds_wanted;
1311 retain |= cap->issued;
1312 delayed = 1;
1314 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1315 if (want & ~cap->mds_wanted) {
1316 /* user space may open/close single file frequently.
1317 * This avoids droping mds_wanted immediately after
1318 * requesting new mds_wanted.
1320 __cap_set_timeouts(mdsc, ci);
1323 cap->issued &= retain; /* drop bits we don't want */
1324 if (cap->implemented & ~cap->issued) {
1326 * Wake up any waiters on wanted -> needed transition.
1327 * This is due to the weird transition from buffered
1328 * to sync IO... we need to flush dirty pages _before_
1329 * allowing sync writes to avoid reordering.
1331 wake = 1;
1333 cap->implemented &= cap->issued | used;
1334 cap->mds_wanted = want;
1336 arg.ino = ceph_vino(inode).ino;
1337 arg.cid = cap->cap_id;
1338 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1339 arg.flush_tid = flush_tid;
1340 arg.oldest_flush_tid = oldest_flush_tid;
1342 arg.size = inode->i_size;
1343 ci->i_reported_size = arg.size;
1344 arg.max_size = ci->i_wanted_max_size;
1345 ci->i_requested_max_size = arg.max_size;
1347 if (flushing & CEPH_CAP_XATTR_EXCL) {
1348 old_blob = __ceph_build_xattrs_blob(ci);
1349 arg.xattr_version = ci->i_xattrs.version;
1350 arg.xattr_buf = ci->i_xattrs.blob;
1351 } else {
1352 arg.xattr_buf = NULL;
1355 arg.mtime = inode->i_mtime;
1356 arg.atime = inode->i_atime;
1357 arg.ctime = inode->i_ctime;
1359 arg.op = op;
1360 arg.caps = cap->implemented;
1361 arg.wanted = want;
1362 arg.dirty = flushing;
1364 arg.seq = cap->seq;
1365 arg.issue_seq = cap->issue_seq;
1366 arg.mseq = cap->mseq;
1367 arg.time_warp_seq = ci->i_time_warp_seq;
1369 arg.uid = inode->i_uid;
1370 arg.gid = inode->i_gid;
1371 arg.mode = inode->i_mode;
1373 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1374 if (list_empty(&ci->i_cap_snaps))
1375 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1376 else
1377 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1378 if (sync)
1379 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
1381 spin_unlock(&ci->i_ceph_lock);
1383 ceph_buffer_put(old_blob);
1385 ret = send_cap_msg(&arg);
1386 if (ret < 0) {
1387 dout("error sending cap msg, must requeue %p\n", inode);
1388 delayed = 1;
1391 if (wake)
1392 wake_up_all(&ci->i_cap_wq);
1394 return delayed;
1397 static inline int __send_flush_snap(struct inode *inode,
1398 struct ceph_mds_session *session,
1399 struct ceph_cap_snap *capsnap,
1400 u32 mseq, u64 oldest_flush_tid)
1402 struct cap_msg_args arg;
1404 arg.session = session;
1405 arg.ino = ceph_vino(inode).ino;
1406 arg.cid = 0;
1407 arg.follows = capsnap->follows;
1408 arg.flush_tid = capsnap->cap_flush.tid;
1409 arg.oldest_flush_tid = oldest_flush_tid;
1411 arg.size = capsnap->size;
1412 arg.max_size = 0;
1413 arg.xattr_version = capsnap->xattr_version;
1414 arg.xattr_buf = capsnap->xattr_blob;
1416 arg.atime = capsnap->atime;
1417 arg.mtime = capsnap->mtime;
1418 arg.ctime = capsnap->ctime;
1420 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1421 arg.caps = capsnap->issued;
1422 arg.wanted = 0;
1423 arg.dirty = capsnap->dirty;
1425 arg.seq = 0;
1426 arg.issue_seq = 0;
1427 arg.mseq = mseq;
1428 arg.time_warp_seq = capsnap->time_warp_seq;
1430 arg.uid = capsnap->uid;
1431 arg.gid = capsnap->gid;
1432 arg.mode = capsnap->mode;
1434 arg.inline_data = capsnap->inline_data;
1435 arg.flags = 0;
1437 return send_cap_msg(&arg);
1441 * When a snapshot is taken, clients accumulate dirty metadata on
1442 * inodes with capabilities in ceph_cap_snaps to describe the file
1443 * state at the time the snapshot was taken. This must be flushed
1444 * asynchronously back to the MDS once sync writes complete and dirty
1445 * data is written out.
1447 * Called under i_ceph_lock. Takes s_mutex as needed.
1449 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1450 struct ceph_mds_session *session)
1451 __releases(ci->i_ceph_lock)
1452 __acquires(ci->i_ceph_lock)
1454 struct inode *inode = &ci->vfs_inode;
1455 struct ceph_mds_client *mdsc = session->s_mdsc;
1456 struct ceph_cap_snap *capsnap;
1457 u64 oldest_flush_tid = 0;
1458 u64 first_tid = 1, last_tid = 0;
1460 dout("__flush_snaps %p session %p\n", inode, session);
1462 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1464 * we need to wait for sync writes to complete and for dirty
1465 * pages to be written out.
1467 if (capsnap->dirty_pages || capsnap->writing)
1468 break;
1470 /* should be removed by ceph_try_drop_cap_snap() */
1471 BUG_ON(!capsnap->need_flush);
1473 /* only flush each capsnap once */
1474 if (capsnap->cap_flush.tid > 0) {
1475 dout(" already flushed %p, skipping\n", capsnap);
1476 continue;
1479 spin_lock(&mdsc->cap_dirty_lock);
1480 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1481 list_add_tail(&capsnap->cap_flush.g_list,
1482 &mdsc->cap_flush_list);
1483 if (oldest_flush_tid == 0)
1484 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1485 if (list_empty(&ci->i_flushing_item)) {
1486 list_add_tail(&ci->i_flushing_item,
1487 &session->s_cap_flushing);
1489 spin_unlock(&mdsc->cap_dirty_lock);
1491 list_add_tail(&capsnap->cap_flush.i_list,
1492 &ci->i_cap_flush_list);
1494 if (first_tid == 1)
1495 first_tid = capsnap->cap_flush.tid;
1496 last_tid = capsnap->cap_flush.tid;
1499 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1501 while (first_tid <= last_tid) {
1502 struct ceph_cap *cap = ci->i_auth_cap;
1503 struct ceph_cap_flush *cf;
1504 int ret;
1506 if (!(cap && cap->session == session)) {
1507 dout("__flush_snaps %p auth cap %p not mds%d, "
1508 "stop\n", inode, cap, session->s_mds);
1509 break;
1512 ret = -ENOENT;
1513 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1514 if (cf->tid >= first_tid) {
1515 ret = 0;
1516 break;
1519 if (ret < 0)
1520 break;
1522 first_tid = cf->tid + 1;
1524 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1525 refcount_inc(&capsnap->nref);
1526 spin_unlock(&ci->i_ceph_lock);
1528 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1529 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
1531 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1532 oldest_flush_tid);
1533 if (ret < 0) {
1534 pr_err("__flush_snaps: error sending cap flushsnap, "
1535 "ino (%llx.%llx) tid %llu follows %llu\n",
1536 ceph_vinop(inode), cf->tid, capsnap->follows);
1539 ceph_put_cap_snap(capsnap);
1540 spin_lock(&ci->i_ceph_lock);
1544 void ceph_flush_snaps(struct ceph_inode_info *ci,
1545 struct ceph_mds_session **psession)
1547 struct inode *inode = &ci->vfs_inode;
1548 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1549 struct ceph_mds_session *session = NULL;
1550 int mds;
1552 dout("ceph_flush_snaps %p\n", inode);
1553 if (psession)
1554 session = *psession;
1555 retry:
1556 spin_lock(&ci->i_ceph_lock);
1557 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1558 dout(" no capsnap needs flush, doing nothing\n");
1559 goto out;
1561 if (!ci->i_auth_cap) {
1562 dout(" no auth cap (migrating?), doing nothing\n");
1563 goto out;
1566 mds = ci->i_auth_cap->session->s_mds;
1567 if (session && session->s_mds != mds) {
1568 dout(" oops, wrong session %p mutex\n", session);
1569 mutex_unlock(&session->s_mutex);
1570 ceph_put_mds_session(session);
1571 session = NULL;
1573 if (!session) {
1574 spin_unlock(&ci->i_ceph_lock);
1575 mutex_lock(&mdsc->mutex);
1576 session = __ceph_lookup_mds_session(mdsc, mds);
1577 mutex_unlock(&mdsc->mutex);
1578 if (session) {
1579 dout(" inverting session/ino locks on %p\n", session);
1580 mutex_lock(&session->s_mutex);
1582 goto retry;
1585 // make sure flushsnap messages are sent in proper order.
1586 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1587 __kick_flushing_caps(mdsc, session, ci, 0);
1588 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1591 __ceph_flush_snaps(ci, session);
1592 out:
1593 spin_unlock(&ci->i_ceph_lock);
1595 if (psession) {
1596 *psession = session;
1597 } else if (session) {
1598 mutex_unlock(&session->s_mutex);
1599 ceph_put_mds_session(session);
1601 /* we flushed them all; remove this inode from the queue */
1602 spin_lock(&mdsc->snap_flush_lock);
1603 list_del_init(&ci->i_snap_flush_item);
1604 spin_unlock(&mdsc->snap_flush_lock);
1608 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1609 * Caller is then responsible for calling __mark_inode_dirty with the
1610 * returned flags value.
1612 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1613 struct ceph_cap_flush **pcf)
1615 struct ceph_mds_client *mdsc =
1616 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
1617 struct inode *inode = &ci->vfs_inode;
1618 int was = ci->i_dirty_caps;
1619 int dirty = 0;
1621 if (!ci->i_auth_cap) {
1622 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1623 "but no auth cap (session was closed?)\n",
1624 inode, ceph_ino(inode), ceph_cap_string(mask));
1625 return 0;
1628 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1629 ceph_cap_string(mask), ceph_cap_string(was),
1630 ceph_cap_string(was | mask));
1631 ci->i_dirty_caps |= mask;
1632 if (was == 0) {
1633 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1634 swap(ci->i_prealloc_cap_flush, *pcf);
1636 if (!ci->i_head_snapc) {
1637 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1638 ci->i_head_snapc = ceph_get_snap_context(
1639 ci->i_snap_realm->cached_context);
1641 dout(" inode %p now dirty snapc %p auth cap %p\n",
1642 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
1643 BUG_ON(!list_empty(&ci->i_dirty_item));
1644 spin_lock(&mdsc->cap_dirty_lock);
1645 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1646 spin_unlock(&mdsc->cap_dirty_lock);
1647 if (ci->i_flushing_caps == 0) {
1648 ihold(inode);
1649 dirty |= I_DIRTY_SYNC;
1651 } else {
1652 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1654 BUG_ON(list_empty(&ci->i_dirty_item));
1655 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1656 (mask & CEPH_CAP_FILE_BUFFER))
1657 dirty |= I_DIRTY_DATASYNC;
1658 __cap_delay_requeue(mdsc, ci);
1659 return dirty;
1662 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1664 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1667 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1669 if (cf)
1670 kmem_cache_free(ceph_cap_flush_cachep, cf);
1673 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1675 if (!list_empty(&mdsc->cap_flush_list)) {
1676 struct ceph_cap_flush *cf =
1677 list_first_entry(&mdsc->cap_flush_list,
1678 struct ceph_cap_flush, g_list);
1679 return cf->tid;
1681 return 0;
1685 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1686 * Return true if caller needs to wake up flush waiters.
1688 static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1689 struct ceph_inode_info *ci,
1690 struct ceph_cap_flush *cf)
1692 struct ceph_cap_flush *prev;
1693 bool wake = cf->wake;
1694 if (mdsc) {
1695 /* are there older pending cap flushes? */
1696 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1697 prev = list_prev_entry(cf, g_list);
1698 prev->wake = true;
1699 wake = false;
1701 list_del(&cf->g_list);
1702 } else if (ci) {
1703 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1704 prev = list_prev_entry(cf, i_list);
1705 prev->wake = true;
1706 wake = false;
1708 list_del(&cf->i_list);
1709 } else {
1710 BUG_ON(1);
1712 return wake;
1716 * Add dirty inode to the flushing list. Assigned a seq number so we
1717 * can wait for caps to flush without starving.
1719 * Called under i_ceph_lock.
1721 static int __mark_caps_flushing(struct inode *inode,
1722 struct ceph_mds_session *session, bool wake,
1723 u64 *flush_tid, u64 *oldest_flush_tid)
1725 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1726 struct ceph_inode_info *ci = ceph_inode(inode);
1727 struct ceph_cap_flush *cf = NULL;
1728 int flushing;
1730 BUG_ON(ci->i_dirty_caps == 0);
1731 BUG_ON(list_empty(&ci->i_dirty_item));
1732 BUG_ON(!ci->i_prealloc_cap_flush);
1734 flushing = ci->i_dirty_caps;
1735 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1736 ceph_cap_string(flushing),
1737 ceph_cap_string(ci->i_flushing_caps),
1738 ceph_cap_string(ci->i_flushing_caps | flushing));
1739 ci->i_flushing_caps |= flushing;
1740 ci->i_dirty_caps = 0;
1741 dout(" inode %p now !dirty\n", inode);
1743 swap(cf, ci->i_prealloc_cap_flush);
1744 cf->caps = flushing;
1745 cf->wake = wake;
1747 spin_lock(&mdsc->cap_dirty_lock);
1748 list_del_init(&ci->i_dirty_item);
1750 cf->tid = ++mdsc->last_cap_flush_tid;
1751 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1752 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1754 if (list_empty(&ci->i_flushing_item)) {
1755 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1756 mdsc->num_cap_flushing++;
1758 spin_unlock(&mdsc->cap_dirty_lock);
1760 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1762 *flush_tid = cf->tid;
1763 return flushing;
1767 * try to invalidate mapping pages without blocking.
1769 static int try_nonblocking_invalidate(struct inode *inode)
1771 struct ceph_inode_info *ci = ceph_inode(inode);
1772 u32 invalidating_gen = ci->i_rdcache_gen;
1774 spin_unlock(&ci->i_ceph_lock);
1775 invalidate_mapping_pages(&inode->i_data, 0, -1);
1776 spin_lock(&ci->i_ceph_lock);
1778 if (inode->i_data.nrpages == 0 &&
1779 invalidating_gen == ci->i_rdcache_gen) {
1780 /* success. */
1781 dout("try_nonblocking_invalidate %p success\n", inode);
1782 /* save any racing async invalidate some trouble */
1783 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1784 return 0;
1786 dout("try_nonblocking_invalidate %p failed\n", inode);
1787 return -1;
1790 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1792 loff_t size = ci->vfs_inode.i_size;
1793 /* mds will adjust max size according to the reported size */
1794 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1795 return false;
1796 if (size >= ci->i_max_size)
1797 return true;
1798 /* half of previous max_size increment has been used */
1799 if (ci->i_max_size > ci->i_reported_size &&
1800 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1801 return true;
1802 return false;
1806 * Swiss army knife function to examine currently used and wanted
1807 * versus held caps. Release, flush, ack revoked caps to mds as
1808 * appropriate.
1810 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1811 * cap release further.
1812 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1813 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1814 * further delay.
1816 void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1817 struct ceph_mds_session *session)
1819 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1820 struct ceph_mds_client *mdsc = fsc->mdsc;
1821 struct inode *inode = &ci->vfs_inode;
1822 struct ceph_cap *cap;
1823 u64 flush_tid, oldest_flush_tid;
1824 int file_wanted, used, cap_used;
1825 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
1826 int issued, implemented, want, retain, revoking, flushing = 0;
1827 int mds = -1; /* keep track of how far we've gone through i_caps list
1828 to avoid an infinite loop on retry */
1829 struct rb_node *p;
1830 int delayed = 0, sent = 0;
1831 bool no_delay = flags & CHECK_CAPS_NODELAY;
1832 bool queue_invalidate = false;
1833 bool tried_invalidate = false;
1835 /* if we are unmounting, flush any unused caps immediately. */
1836 if (mdsc->stopping)
1837 no_delay = true;
1839 spin_lock(&ci->i_ceph_lock);
1841 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1842 flags |= CHECK_CAPS_FLUSH;
1844 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1845 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1846 __cap_delay_cancel(mdsc, ci);
1848 goto retry_locked;
1849 retry:
1850 spin_lock(&ci->i_ceph_lock);
1851 retry_locked:
1852 file_wanted = __ceph_caps_file_wanted(ci);
1853 used = __ceph_caps_used(ci);
1854 issued = __ceph_caps_issued(ci, &implemented);
1855 revoking = implemented & ~issued;
1857 want = file_wanted;
1858 retain = file_wanted | used | CEPH_CAP_PIN;
1859 if (!mdsc->stopping && inode->i_nlink > 0) {
1860 if (file_wanted) {
1861 retain |= CEPH_CAP_ANY; /* be greedy */
1862 } else if (S_ISDIR(inode->i_mode) &&
1863 (issued & CEPH_CAP_FILE_SHARED) &&
1864 __ceph_dir_is_complete(ci)) {
1866 * If a directory is complete, we want to keep
1867 * the exclusive cap. So that MDS does not end up
1868 * revoking the shared cap on every create/unlink
1869 * operation.
1871 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1872 retain |= want;
1873 } else {
1875 retain |= CEPH_CAP_ANY_SHARED;
1877 * keep RD only if we didn't have the file open RW,
1878 * because then the mds would revoke it anyway to
1879 * journal max_size=0.
1881 if (ci->i_max_size == 0)
1882 retain |= CEPH_CAP_ANY_RD;
1886 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1887 " issued %s revoking %s retain %s %s%s%s\n", inode,
1888 ceph_cap_string(file_wanted),
1889 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1890 ceph_cap_string(ci->i_flushing_caps),
1891 ceph_cap_string(issued), ceph_cap_string(revoking),
1892 ceph_cap_string(retain),
1893 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1894 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1895 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1898 * If we no longer need to hold onto old our caps, and we may
1899 * have cached pages, but don't want them, then try to invalidate.
1900 * If we fail, it's because pages are locked.... try again later.
1902 if ((!no_delay || mdsc->stopping) &&
1903 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
1904 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
1905 inode->i_data.nrpages && /* have cached pages */
1906 (revoking & (CEPH_CAP_FILE_CACHE|
1907 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
1908 !tried_invalidate) {
1909 dout("check_caps trying to invalidate on %p\n", inode);
1910 if (try_nonblocking_invalidate(inode) < 0) {
1911 dout("check_caps queuing invalidate\n");
1912 queue_invalidate = true;
1913 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1915 tried_invalidate = true;
1916 goto retry_locked;
1919 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1920 cap = rb_entry(p, struct ceph_cap, ci_node);
1922 /* avoid looping forever */
1923 if (mds >= cap->mds ||
1924 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1925 continue;
1927 /* NOTE: no side-effects allowed, until we take s_mutex */
1929 cap_used = used;
1930 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1931 cap_used &= ~ci->i_auth_cap->issued;
1933 revoking = cap->implemented & ~cap->issued;
1934 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1935 cap->mds, cap, ceph_cap_string(cap_used),
1936 ceph_cap_string(cap->issued),
1937 ceph_cap_string(cap->implemented),
1938 ceph_cap_string(revoking));
1940 if (cap == ci->i_auth_cap &&
1941 (cap->issued & CEPH_CAP_FILE_WR)) {
1942 /* request larger max_size from MDS? */
1943 if (ci->i_wanted_max_size > ci->i_max_size &&
1944 ci->i_wanted_max_size > ci->i_requested_max_size) {
1945 dout("requesting new max_size\n");
1946 goto ack;
1949 /* approaching file_max? */
1950 if (__ceph_should_report_size(ci)) {
1951 dout("i_size approaching max_size\n");
1952 goto ack;
1955 /* flush anything dirty? */
1956 if (cap == ci->i_auth_cap) {
1957 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1958 dout("flushing dirty caps\n");
1959 goto ack;
1961 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1962 dout("flushing snap caps\n");
1963 goto ack;
1967 /* completed revocation? going down and there are no caps? */
1968 if (revoking && (revoking & cap_used) == 0) {
1969 dout("completed revocation of %s\n",
1970 ceph_cap_string(cap->implemented & ~cap->issued));
1971 goto ack;
1974 /* want more caps from mds? */
1975 if (want & ~cap->mds_wanted) {
1976 if (want & ~(cap->mds_wanted | cap->issued))
1977 goto ack;
1978 if (!__cap_is_valid(cap))
1979 goto ack;
1982 /* things we might delay */
1983 if ((cap->issued & ~retain) == 0 &&
1984 cap->mds_wanted == want)
1985 continue; /* nope, all good */
1987 if (no_delay)
1988 goto ack;
1990 /* delay? */
1991 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1992 time_before(jiffies, ci->i_hold_caps_max)) {
1993 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1994 ceph_cap_string(cap->issued),
1995 ceph_cap_string(cap->issued & retain),
1996 ceph_cap_string(cap->mds_wanted),
1997 ceph_cap_string(want));
1998 delayed++;
1999 continue;
2002 ack:
2003 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2004 dout(" skipping %p I_NOFLUSH set\n", inode);
2005 continue;
2008 if (session && session != cap->session) {
2009 dout("oops, wrong session %p mutex\n", session);
2010 mutex_unlock(&session->s_mutex);
2011 session = NULL;
2013 if (!session) {
2014 session = cap->session;
2015 if (mutex_trylock(&session->s_mutex) == 0) {
2016 dout("inverting session/ino locks on %p\n",
2017 session);
2018 spin_unlock(&ci->i_ceph_lock);
2019 if (took_snap_rwsem) {
2020 up_read(&mdsc->snap_rwsem);
2021 took_snap_rwsem = 0;
2023 mutex_lock(&session->s_mutex);
2024 goto retry;
2028 /* kick flushing and flush snaps before sending normal
2029 * cap message */
2030 if (cap == ci->i_auth_cap &&
2031 (ci->i_ceph_flags &
2032 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2033 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2034 __kick_flushing_caps(mdsc, session, ci, 0);
2035 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2037 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2038 __ceph_flush_snaps(ci, session);
2040 goto retry_locked;
2043 /* take snap_rwsem after session mutex */
2044 if (!took_snap_rwsem) {
2045 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
2046 dout("inverting snap/in locks on %p\n",
2047 inode);
2048 spin_unlock(&ci->i_ceph_lock);
2049 down_read(&mdsc->snap_rwsem);
2050 took_snap_rwsem = 1;
2051 goto retry;
2053 took_snap_rwsem = 1;
2056 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
2057 flushing = __mark_caps_flushing(inode, session, false,
2058 &flush_tid,
2059 &oldest_flush_tid);
2060 } else {
2061 flushing = 0;
2062 flush_tid = 0;
2063 spin_lock(&mdsc->cap_dirty_lock);
2064 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2065 spin_unlock(&mdsc->cap_dirty_lock);
2068 mds = cap->mds; /* remember mds, so we don't repeat */
2069 sent++;
2071 /* __send_cap drops i_ceph_lock */
2072 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2073 cap_used, want, retain, flushing,
2074 flush_tid, oldest_flush_tid);
2075 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2078 /* Reschedule delayed caps release if we delayed anything */
2079 if (delayed)
2080 __cap_delay_requeue(mdsc, ci);
2082 spin_unlock(&ci->i_ceph_lock);
2084 if (queue_invalidate)
2085 ceph_queue_invalidate(inode);
2087 if (session)
2088 mutex_unlock(&session->s_mutex);
2089 if (took_snap_rwsem)
2090 up_read(&mdsc->snap_rwsem);
2094 * Try to flush dirty caps back to the auth mds.
2096 static int try_flush_caps(struct inode *inode, u64 *ptid)
2098 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2099 struct ceph_inode_info *ci = ceph_inode(inode);
2100 struct ceph_mds_session *session = NULL;
2101 int flushing = 0;
2102 u64 flush_tid = 0, oldest_flush_tid = 0;
2104 retry:
2105 spin_lock(&ci->i_ceph_lock);
2106 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2107 spin_unlock(&ci->i_ceph_lock);
2108 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2109 goto out;
2111 if (ci->i_dirty_caps && ci->i_auth_cap) {
2112 struct ceph_cap *cap = ci->i_auth_cap;
2113 int used = __ceph_caps_used(ci);
2114 int want = __ceph_caps_wanted(ci);
2115 int delayed;
2117 if (!session || session != cap->session) {
2118 spin_unlock(&ci->i_ceph_lock);
2119 if (session)
2120 mutex_unlock(&session->s_mutex);
2121 session = cap->session;
2122 mutex_lock(&session->s_mutex);
2123 goto retry;
2125 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2126 spin_unlock(&ci->i_ceph_lock);
2127 goto out;
2130 flushing = __mark_caps_flushing(inode, session, true,
2131 &flush_tid, &oldest_flush_tid);
2133 /* __send_cap drops i_ceph_lock */
2134 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2135 used, want, (cap->issued | cap->implemented),
2136 flushing, flush_tid, oldest_flush_tid);
2138 if (delayed) {
2139 spin_lock(&ci->i_ceph_lock);
2140 __cap_delay_requeue(mdsc, ci);
2141 spin_unlock(&ci->i_ceph_lock);
2143 } else {
2144 if (!list_empty(&ci->i_cap_flush_list)) {
2145 struct ceph_cap_flush *cf =
2146 list_last_entry(&ci->i_cap_flush_list,
2147 struct ceph_cap_flush, i_list);
2148 cf->wake = true;
2149 flush_tid = cf->tid;
2151 flushing = ci->i_flushing_caps;
2152 spin_unlock(&ci->i_ceph_lock);
2154 out:
2155 if (session)
2156 mutex_unlock(&session->s_mutex);
2158 *ptid = flush_tid;
2159 return flushing;
2163 * Return true if we've flushed caps through the given flush_tid.
2165 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2167 struct ceph_inode_info *ci = ceph_inode(inode);
2168 int ret = 1;
2170 spin_lock(&ci->i_ceph_lock);
2171 if (!list_empty(&ci->i_cap_flush_list)) {
2172 struct ceph_cap_flush * cf =
2173 list_first_entry(&ci->i_cap_flush_list,
2174 struct ceph_cap_flush, i_list);
2175 if (cf->tid <= flush_tid)
2176 ret = 0;
2178 spin_unlock(&ci->i_ceph_lock);
2179 return ret;
2183 * wait for any unsafe requests to complete.
2185 static int unsafe_request_wait(struct inode *inode)
2187 struct ceph_inode_info *ci = ceph_inode(inode);
2188 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2189 int ret, err = 0;
2191 spin_lock(&ci->i_unsafe_lock);
2192 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2193 req1 = list_last_entry(&ci->i_unsafe_dirops,
2194 struct ceph_mds_request,
2195 r_unsafe_dir_item);
2196 ceph_mdsc_get_request(req1);
2198 if (!list_empty(&ci->i_unsafe_iops)) {
2199 req2 = list_last_entry(&ci->i_unsafe_iops,
2200 struct ceph_mds_request,
2201 r_unsafe_target_item);
2202 ceph_mdsc_get_request(req2);
2204 spin_unlock(&ci->i_unsafe_lock);
2206 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
2207 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2208 if (req1) {
2209 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2210 ceph_timeout_jiffies(req1->r_timeout));
2211 if (ret)
2212 err = -EIO;
2213 ceph_mdsc_put_request(req1);
2215 if (req2) {
2216 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2217 ceph_timeout_jiffies(req2->r_timeout));
2218 if (ret)
2219 err = -EIO;
2220 ceph_mdsc_put_request(req2);
2222 return err;
2225 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2227 struct inode *inode = file->f_mapping->host;
2228 struct ceph_inode_info *ci = ceph_inode(inode);
2229 u64 flush_tid;
2230 int ret;
2231 int dirty;
2233 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
2235 ret = file_write_and_wait_range(file, start, end);
2236 if (ret < 0)
2237 goto out;
2239 if (datasync)
2240 goto out;
2242 inode_lock(inode);
2244 dirty = try_flush_caps(inode, &flush_tid);
2245 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2247 ret = unsafe_request_wait(inode);
2250 * only wait on non-file metadata writeback (the mds
2251 * can recover size and mtime, so we don't need to
2252 * wait for that)
2254 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2255 ret = wait_event_interruptible(ci->i_cap_wq,
2256 caps_are_flushed(inode, flush_tid));
2258 inode_unlock(inode);
2259 out:
2260 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
2261 return ret;
2265 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2266 * queue inode for flush but don't do so immediately, because we can
2267 * get by with fewer MDS messages if we wait for data writeback to
2268 * complete first.
2270 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2272 struct ceph_inode_info *ci = ceph_inode(inode);
2273 u64 flush_tid;
2274 int err = 0;
2275 int dirty;
2276 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2278 dout("write_inode %p wait=%d\n", inode, wait);
2279 if (wait) {
2280 dirty = try_flush_caps(inode, &flush_tid);
2281 if (dirty)
2282 err = wait_event_interruptible(ci->i_cap_wq,
2283 caps_are_flushed(inode, flush_tid));
2284 } else {
2285 struct ceph_mds_client *mdsc =
2286 ceph_sb_to_client(inode->i_sb)->mdsc;
2288 spin_lock(&ci->i_ceph_lock);
2289 if (__ceph_caps_dirty(ci))
2290 __cap_delay_requeue_front(mdsc, ci);
2291 spin_unlock(&ci->i_ceph_lock);
2293 return err;
2296 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2297 struct ceph_mds_session *session,
2298 struct ceph_inode_info *ci,
2299 u64 oldest_flush_tid)
2300 __releases(ci->i_ceph_lock)
2301 __acquires(ci->i_ceph_lock)
2303 struct inode *inode = &ci->vfs_inode;
2304 struct ceph_cap *cap;
2305 struct ceph_cap_flush *cf;
2306 int ret;
2307 u64 first_tid = 0;
2309 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2310 if (cf->tid < first_tid)
2311 continue;
2313 cap = ci->i_auth_cap;
2314 if (!(cap && cap->session == session)) {
2315 pr_err("%p auth cap %p not mds%d ???\n",
2316 inode, cap, session->s_mds);
2317 break;
2320 first_tid = cf->tid + 1;
2322 if (cf->caps) {
2323 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2324 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2325 ci->i_ceph_flags |= CEPH_I_NODELAY;
2326 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2327 false, __ceph_caps_used(ci),
2328 __ceph_caps_wanted(ci),
2329 cap->issued | cap->implemented,
2330 cf->caps, cf->tid, oldest_flush_tid);
2331 if (ret) {
2332 pr_err("kick_flushing_caps: error sending "
2333 "cap flush, ino (%llx.%llx) "
2334 "tid %llu flushing %s\n",
2335 ceph_vinop(inode), cf->tid,
2336 ceph_cap_string(cf->caps));
2338 } else {
2339 struct ceph_cap_snap *capsnap =
2340 container_of(cf, struct ceph_cap_snap,
2341 cap_flush);
2342 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2343 inode, capsnap, cf->tid,
2344 ceph_cap_string(capsnap->dirty));
2346 refcount_inc(&capsnap->nref);
2347 spin_unlock(&ci->i_ceph_lock);
2349 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2350 oldest_flush_tid);
2351 if (ret < 0) {
2352 pr_err("kick_flushing_caps: error sending "
2353 "cap flushsnap, ino (%llx.%llx) "
2354 "tid %llu follows %llu\n",
2355 ceph_vinop(inode), cf->tid,
2356 capsnap->follows);
2359 ceph_put_cap_snap(capsnap);
2362 spin_lock(&ci->i_ceph_lock);
2366 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2367 struct ceph_mds_session *session)
2369 struct ceph_inode_info *ci;
2370 struct ceph_cap *cap;
2371 u64 oldest_flush_tid;
2373 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
2375 spin_lock(&mdsc->cap_dirty_lock);
2376 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2377 spin_unlock(&mdsc->cap_dirty_lock);
2379 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2380 spin_lock(&ci->i_ceph_lock);
2381 cap = ci->i_auth_cap;
2382 if (!(cap && cap->session == session)) {
2383 pr_err("%p auth cap %p not mds%d ???\n",
2384 &ci->vfs_inode, cap, session->s_mds);
2385 spin_unlock(&ci->i_ceph_lock);
2386 continue;
2391 * if flushing caps were revoked, we re-send the cap flush
2392 * in client reconnect stage. This guarantees MDS * processes
2393 * the cap flush message before issuing the flushing caps to
2394 * other client.
2396 if ((cap->issued & ci->i_flushing_caps) !=
2397 ci->i_flushing_caps) {
2398 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2399 __kick_flushing_caps(mdsc, session, ci,
2400 oldest_flush_tid);
2401 } else {
2402 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
2405 spin_unlock(&ci->i_ceph_lock);
2409 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2410 struct ceph_mds_session *session)
2412 struct ceph_inode_info *ci;
2413 struct ceph_cap *cap;
2414 u64 oldest_flush_tid;
2416 dout("kick_flushing_caps mds%d\n", session->s_mds);
2418 spin_lock(&mdsc->cap_dirty_lock);
2419 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2420 spin_unlock(&mdsc->cap_dirty_lock);
2422 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2423 spin_lock(&ci->i_ceph_lock);
2424 cap = ci->i_auth_cap;
2425 if (!(cap && cap->session == session)) {
2426 pr_err("%p auth cap %p not mds%d ???\n",
2427 &ci->vfs_inode, cap, session->s_mds);
2428 spin_unlock(&ci->i_ceph_lock);
2429 continue;
2431 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2432 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2433 __kick_flushing_caps(mdsc, session, ci,
2434 oldest_flush_tid);
2436 spin_unlock(&ci->i_ceph_lock);
2440 static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2441 struct ceph_mds_session *session,
2442 struct inode *inode)
2443 __releases(ci->i_ceph_lock)
2445 struct ceph_inode_info *ci = ceph_inode(inode);
2446 struct ceph_cap *cap;
2448 cap = ci->i_auth_cap;
2449 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2450 ceph_cap_string(ci->i_flushing_caps));
2452 if (!list_empty(&ci->i_cap_flush_list)) {
2453 u64 oldest_flush_tid;
2454 spin_lock(&mdsc->cap_dirty_lock);
2455 list_move_tail(&ci->i_flushing_item,
2456 &cap->session->s_cap_flushing);
2457 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2458 spin_unlock(&mdsc->cap_dirty_lock);
2460 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2461 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2462 spin_unlock(&ci->i_ceph_lock);
2463 } else {
2464 spin_unlock(&ci->i_ceph_lock);
2470 * Take references to capabilities we hold, so that we don't release
2471 * them to the MDS prematurely.
2473 * Protected by i_ceph_lock.
2475 static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2476 bool snap_rwsem_locked)
2478 if (got & CEPH_CAP_PIN)
2479 ci->i_pin_ref++;
2480 if (got & CEPH_CAP_FILE_RD)
2481 ci->i_rd_ref++;
2482 if (got & CEPH_CAP_FILE_CACHE)
2483 ci->i_rdcache_ref++;
2484 if (got & CEPH_CAP_FILE_WR) {
2485 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2486 BUG_ON(!snap_rwsem_locked);
2487 ci->i_head_snapc = ceph_get_snap_context(
2488 ci->i_snap_realm->cached_context);
2490 ci->i_wr_ref++;
2492 if (got & CEPH_CAP_FILE_BUFFER) {
2493 if (ci->i_wb_ref == 0)
2494 ihold(&ci->vfs_inode);
2495 ci->i_wb_ref++;
2496 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2497 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2502 * Try to grab cap references. Specify those refs we @want, and the
2503 * minimal set we @need. Also include the larger offset we are writing
2504 * to (when applicable), and check against max_size here as well.
2505 * Note that caller is responsible for ensuring max_size increases are
2506 * requested from the MDS.
2508 static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2509 loff_t endoff, bool nonblock, int *got, int *err)
2511 struct inode *inode = &ci->vfs_inode;
2512 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2513 int ret = 0;
2514 int have, implemented;
2515 int file_wanted;
2516 bool snap_rwsem_locked = false;
2518 dout("get_cap_refs %p need %s want %s\n", inode,
2519 ceph_cap_string(need), ceph_cap_string(want));
2521 again:
2522 spin_lock(&ci->i_ceph_lock);
2524 /* make sure file is actually open */
2525 file_wanted = __ceph_caps_file_wanted(ci);
2526 if ((file_wanted & need) != need) {
2527 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2528 ceph_cap_string(need), ceph_cap_string(file_wanted));
2529 *err = -EBADF;
2530 ret = 1;
2531 goto out_unlock;
2534 /* finish pending truncate */
2535 while (ci->i_truncate_pending) {
2536 spin_unlock(&ci->i_ceph_lock);
2537 if (snap_rwsem_locked) {
2538 up_read(&mdsc->snap_rwsem);
2539 snap_rwsem_locked = false;
2541 __ceph_do_pending_vmtruncate(inode);
2542 spin_lock(&ci->i_ceph_lock);
2545 have = __ceph_caps_issued(ci, &implemented);
2547 if (have & need & CEPH_CAP_FILE_WR) {
2548 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2549 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2550 inode, endoff, ci->i_max_size);
2551 if (endoff > ci->i_requested_max_size) {
2552 *err = -EAGAIN;
2553 ret = 1;
2555 goto out_unlock;
2558 * If a sync write is in progress, we must wait, so that we
2559 * can get a final snapshot value for size+mtime.
2561 if (__ceph_have_pending_cap_snap(ci)) {
2562 dout("get_cap_refs %p cap_snap_pending\n", inode);
2563 goto out_unlock;
2567 if ((have & need) == need) {
2569 * Look at (implemented & ~have & not) so that we keep waiting
2570 * on transition from wanted -> needed caps. This is needed
2571 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2572 * going before a prior buffered writeback happens.
2574 int not = want & ~(have & need);
2575 int revoking = implemented & ~have;
2576 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2577 inode, ceph_cap_string(have), ceph_cap_string(not),
2578 ceph_cap_string(revoking));
2579 if ((revoking & not) == 0) {
2580 if (!snap_rwsem_locked &&
2581 !ci->i_head_snapc &&
2582 (need & CEPH_CAP_FILE_WR)) {
2583 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2585 * we can not call down_read() when
2586 * task isn't in TASK_RUNNING state
2588 if (nonblock) {
2589 *err = -EAGAIN;
2590 ret = 1;
2591 goto out_unlock;
2594 spin_unlock(&ci->i_ceph_lock);
2595 down_read(&mdsc->snap_rwsem);
2596 snap_rwsem_locked = true;
2597 goto again;
2599 snap_rwsem_locked = true;
2601 *got = need | (have & want);
2602 if ((need & CEPH_CAP_FILE_RD) &&
2603 !(*got & CEPH_CAP_FILE_CACHE))
2604 ceph_disable_fscache_readpage(ci);
2605 __take_cap_refs(ci, *got, true);
2606 ret = 1;
2608 } else {
2609 int session_readonly = false;
2610 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2611 struct ceph_mds_session *s = ci->i_auth_cap->session;
2612 spin_lock(&s->s_cap_lock);
2613 session_readonly = s->s_readonly;
2614 spin_unlock(&s->s_cap_lock);
2616 if (session_readonly) {
2617 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2618 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2619 *err = -EROFS;
2620 ret = 1;
2621 goto out_unlock;
2624 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2625 int mds_wanted;
2626 if (READ_ONCE(mdsc->fsc->mount_state) ==
2627 CEPH_MOUNT_SHUTDOWN) {
2628 dout("get_cap_refs %p forced umount\n", inode);
2629 *err = -EIO;
2630 ret = 1;
2631 goto out_unlock;
2633 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2634 if (need & ~(mds_wanted & need)) {
2635 dout("get_cap_refs %p caps were dropped"
2636 " (session killed?)\n", inode);
2637 *err = -ESTALE;
2638 ret = 1;
2639 goto out_unlock;
2641 if (!(file_wanted & ~mds_wanted))
2642 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
2645 dout("get_cap_refs %p have %s needed %s\n", inode,
2646 ceph_cap_string(have), ceph_cap_string(need));
2648 out_unlock:
2649 spin_unlock(&ci->i_ceph_lock);
2650 if (snap_rwsem_locked)
2651 up_read(&mdsc->snap_rwsem);
2653 dout("get_cap_refs %p ret %d got %s\n", inode,
2654 ret, ceph_cap_string(*got));
2655 return ret;
2659 * Check the offset we are writing up to against our current
2660 * max_size. If necessary, tell the MDS we want to write to
2661 * a larger offset.
2663 static void check_max_size(struct inode *inode, loff_t endoff)
2665 struct ceph_inode_info *ci = ceph_inode(inode);
2666 int check = 0;
2668 /* do we need to explicitly request a larger max_size? */
2669 spin_lock(&ci->i_ceph_lock);
2670 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2671 dout("write %p at large endoff %llu, req max_size\n",
2672 inode, endoff);
2673 ci->i_wanted_max_size = endoff;
2675 /* duplicate ceph_check_caps()'s logic */
2676 if (ci->i_auth_cap &&
2677 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2678 ci->i_wanted_max_size > ci->i_max_size &&
2679 ci->i_wanted_max_size > ci->i_requested_max_size)
2680 check = 1;
2681 spin_unlock(&ci->i_ceph_lock);
2682 if (check)
2683 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2686 int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
2688 int ret, err = 0;
2690 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2691 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
2692 ret = ceph_pool_perm_check(ci, need);
2693 if (ret < 0)
2694 return ret;
2696 ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
2697 if (ret) {
2698 if (err == -EAGAIN) {
2699 ret = 0;
2700 } else if (err < 0) {
2701 ret = err;
2704 return ret;
2708 * Wait for caps, and take cap references. If we can't get a WR cap
2709 * due to a small max_size, make sure we check_max_size (and possibly
2710 * ask the mds) so we don't get hung up indefinitely.
2712 int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2713 loff_t endoff, int *got, struct page **pinned_page)
2715 int _got, ret, err = 0;
2717 ret = ceph_pool_perm_check(ci, need);
2718 if (ret < 0)
2719 return ret;
2721 while (true) {
2722 if (endoff > 0)
2723 check_max_size(&ci->vfs_inode, endoff);
2725 err = 0;
2726 _got = 0;
2727 ret = try_get_cap_refs(ci, need, want, endoff,
2728 false, &_got, &err);
2729 if (ret) {
2730 if (err == -EAGAIN)
2731 continue;
2732 if (err < 0)
2733 ret = err;
2734 } else {
2735 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2736 add_wait_queue(&ci->i_cap_wq, &wait);
2738 while (!try_get_cap_refs(ci, need, want, endoff,
2739 true, &_got, &err)) {
2740 if (signal_pending(current)) {
2741 ret = -ERESTARTSYS;
2742 break;
2744 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2747 remove_wait_queue(&ci->i_cap_wq, &wait);
2749 if (err == -EAGAIN)
2750 continue;
2751 if (err < 0)
2752 ret = err;
2754 if (ret < 0) {
2755 if (err == -ESTALE) {
2756 /* session was killed, try renew caps */
2757 ret = ceph_renew_caps(&ci->vfs_inode);
2758 if (ret == 0)
2759 continue;
2761 return ret;
2764 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2765 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2766 i_size_read(&ci->vfs_inode) > 0) {
2767 struct page *page =
2768 find_get_page(ci->vfs_inode.i_mapping, 0);
2769 if (page) {
2770 if (PageUptodate(page)) {
2771 *pinned_page = page;
2772 break;
2774 put_page(page);
2777 * drop cap refs first because getattr while
2778 * holding * caps refs can cause deadlock.
2780 ceph_put_cap_refs(ci, _got);
2781 _got = 0;
2784 * getattr request will bring inline data into
2785 * page cache
2787 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2788 CEPH_STAT_CAP_INLINE_DATA,
2789 true);
2790 if (ret < 0)
2791 return ret;
2792 continue;
2794 break;
2797 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2798 ceph_fscache_revalidate_cookie(ci);
2800 *got = _got;
2801 return 0;
2805 * Take cap refs. Caller must already know we hold at least one ref
2806 * on the caps in question or we don't know this is safe.
2808 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2810 spin_lock(&ci->i_ceph_lock);
2811 __take_cap_refs(ci, caps, false);
2812 spin_unlock(&ci->i_ceph_lock);
2817 * drop cap_snap that is not associated with any snapshot.
2818 * we don't need to send FLUSHSNAP message for it.
2820 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2821 struct ceph_cap_snap *capsnap)
2823 if (!capsnap->need_flush &&
2824 !capsnap->writing && !capsnap->dirty_pages) {
2825 dout("dropping cap_snap %p follows %llu\n",
2826 capsnap, capsnap->follows);
2827 BUG_ON(capsnap->cap_flush.tid > 0);
2828 ceph_put_snap_context(capsnap->context);
2829 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2830 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2832 list_del(&capsnap->ci_item);
2833 ceph_put_cap_snap(capsnap);
2834 return 1;
2836 return 0;
2840 * Release cap refs.
2842 * If we released the last ref on any given cap, call ceph_check_caps
2843 * to release (or schedule a release).
2845 * If we are releasing a WR cap (from a sync write), finalize any affected
2846 * cap_snap, and wake up any waiters.
2848 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2850 struct inode *inode = &ci->vfs_inode;
2851 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2853 spin_lock(&ci->i_ceph_lock);
2854 if (had & CEPH_CAP_PIN)
2855 --ci->i_pin_ref;
2856 if (had & CEPH_CAP_FILE_RD)
2857 if (--ci->i_rd_ref == 0)
2858 last++;
2859 if (had & CEPH_CAP_FILE_CACHE)
2860 if (--ci->i_rdcache_ref == 0)
2861 last++;
2862 if (had & CEPH_CAP_FILE_BUFFER) {
2863 if (--ci->i_wb_ref == 0) {
2864 last++;
2865 put++;
2867 dout("put_cap_refs %p wb %d -> %d (?)\n",
2868 inode, ci->i_wb_ref+1, ci->i_wb_ref);
2870 if (had & CEPH_CAP_FILE_WR)
2871 if (--ci->i_wr_ref == 0) {
2872 last++;
2873 if (__ceph_have_pending_cap_snap(ci)) {
2874 struct ceph_cap_snap *capsnap =
2875 list_last_entry(&ci->i_cap_snaps,
2876 struct ceph_cap_snap,
2877 ci_item);
2878 capsnap->writing = 0;
2879 if (ceph_try_drop_cap_snap(ci, capsnap))
2880 put++;
2881 else if (__ceph_finish_cap_snap(ci, capsnap))
2882 flushsnaps = 1;
2883 wake = 1;
2885 if (ci->i_wrbuffer_ref_head == 0 &&
2886 ci->i_dirty_caps == 0 &&
2887 ci->i_flushing_caps == 0) {
2888 BUG_ON(!ci->i_head_snapc);
2889 ceph_put_snap_context(ci->i_head_snapc);
2890 ci->i_head_snapc = NULL;
2892 /* see comment in __ceph_remove_cap() */
2893 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2894 drop_inode_snap_realm(ci);
2896 spin_unlock(&ci->i_ceph_lock);
2898 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2899 last ? " last" : "", put ? " put" : "");
2901 if (last && !flushsnaps)
2902 ceph_check_caps(ci, 0, NULL);
2903 else if (flushsnaps)
2904 ceph_flush_snaps(ci, NULL);
2905 if (wake)
2906 wake_up_all(&ci->i_cap_wq);
2907 while (put-- > 0)
2908 iput(inode);
2912 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2913 * context. Adjust per-snap dirty page accounting as appropriate.
2914 * Once all dirty data for a cap_snap is flushed, flush snapped file
2915 * metadata back to the MDS. If we dropped the last ref, call
2916 * ceph_check_caps.
2918 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2919 struct ceph_snap_context *snapc)
2921 struct inode *inode = &ci->vfs_inode;
2922 struct ceph_cap_snap *capsnap = NULL;
2923 int put = 0;
2924 bool last = false;
2925 bool found = false;
2926 bool flush_snaps = false;
2927 bool complete_capsnap = false;
2929 spin_lock(&ci->i_ceph_lock);
2930 ci->i_wrbuffer_ref -= nr;
2931 if (ci->i_wrbuffer_ref == 0) {
2932 last = true;
2933 put++;
2936 if (ci->i_head_snapc == snapc) {
2937 ci->i_wrbuffer_ref_head -= nr;
2938 if (ci->i_wrbuffer_ref_head == 0 &&
2939 ci->i_wr_ref == 0 &&
2940 ci->i_dirty_caps == 0 &&
2941 ci->i_flushing_caps == 0) {
2942 BUG_ON(!ci->i_head_snapc);
2943 ceph_put_snap_context(ci->i_head_snapc);
2944 ci->i_head_snapc = NULL;
2946 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2947 inode,
2948 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2949 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2950 last ? " LAST" : "");
2951 } else {
2952 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2953 if (capsnap->context == snapc) {
2954 found = true;
2955 break;
2958 BUG_ON(!found);
2959 capsnap->dirty_pages -= nr;
2960 if (capsnap->dirty_pages == 0) {
2961 complete_capsnap = true;
2962 if (!capsnap->writing) {
2963 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2964 put++;
2965 } else {
2966 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2967 flush_snaps = true;
2971 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2972 " snap %lld %d/%d -> %d/%d %s%s\n",
2973 inode, capsnap, capsnap->context->seq,
2974 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2975 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2976 last ? " (wrbuffer last)" : "",
2977 complete_capsnap ? " (complete capsnap)" : "");
2980 spin_unlock(&ci->i_ceph_lock);
2982 if (last) {
2983 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2984 } else if (flush_snaps) {
2985 ceph_flush_snaps(ci, NULL);
2987 if (complete_capsnap)
2988 wake_up_all(&ci->i_cap_wq);
2989 while (put-- > 0)
2990 iput(inode);
2994 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2996 static void invalidate_aliases(struct inode *inode)
2998 struct dentry *dn, *prev = NULL;
3000 dout("invalidate_aliases inode %p\n", inode);
3001 d_prune_aliases(inode);
3003 * For non-directory inode, d_find_alias() only returns
3004 * hashed dentry. After calling d_invalidate(), the
3005 * dentry becomes unhashed.
3007 * For directory inode, d_find_alias() can return
3008 * unhashed dentry. But directory inode should have
3009 * one alias at most.
3011 while ((dn = d_find_alias(inode))) {
3012 if (dn == prev) {
3013 dput(dn);
3014 break;
3016 d_invalidate(dn);
3017 if (prev)
3018 dput(prev);
3019 prev = dn;
3021 if (prev)
3022 dput(prev);
3025 struct cap_extra_info {
3026 struct ceph_string *pool_ns;
3027 /* inline data */
3028 u64 inline_version;
3029 void *inline_data;
3030 u32 inline_len;
3031 /* dirstat */
3032 bool dirstat_valid;
3033 u64 nfiles;
3034 u64 nsubdirs;
3035 /* currently issued */
3036 int issued;
3040 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3041 * actually be a revocation if it specifies a smaller cap set.)
3043 * caller holds s_mutex and i_ceph_lock, we drop both.
3045 static void handle_cap_grant(struct inode *inode,
3046 struct ceph_mds_session *session,
3047 struct ceph_cap *cap,
3048 struct ceph_mds_caps *grant,
3049 struct ceph_buffer *xattr_buf,
3050 struct cap_extra_info *extra_info)
3051 __releases(ci->i_ceph_lock)
3052 __releases(session->s_mdsc->snap_rwsem)
3054 struct ceph_inode_info *ci = ceph_inode(inode);
3055 int seq = le32_to_cpu(grant->seq);
3056 int newcaps = le32_to_cpu(grant->caps);
3057 int used, wanted, dirty;
3058 u64 size = le64_to_cpu(grant->size);
3059 u64 max_size = le64_to_cpu(grant->max_size);
3060 int check_caps = 0;
3061 bool wake = false;
3062 bool writeback = false;
3063 bool queue_trunc = false;
3064 bool queue_invalidate = false;
3065 bool deleted_inode = false;
3066 bool fill_inline = false;
3068 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
3069 inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
3070 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
3071 inode->i_size);
3075 * auth mds of the inode changed. we received the cap export message,
3076 * but still haven't received the cap import message. handle_cap_export
3077 * updated the new auth MDS' cap.
3079 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3080 * that was sent before the cap import message. So don't remove caps.
3082 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3083 WARN_ON(cap != ci->i_auth_cap);
3084 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3085 seq = cap->seq;
3086 newcaps |= cap->issued;
3090 * If CACHE is being revoked, and we have no dirty buffers,
3091 * try to invalidate (once). (If there are dirty buffers, we
3092 * will invalidate _after_ writeback.)
3094 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3095 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3096 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3097 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3098 if (try_nonblocking_invalidate(inode)) {
3099 /* there were locked pages.. invalidate later
3100 in a separate thread. */
3101 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3102 queue_invalidate = true;
3103 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3108 /* side effects now are allowed */
3109 cap->cap_gen = session->s_cap_gen;
3110 cap->seq = seq;
3112 __check_cap_issue(ci, cap, newcaps);
3114 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3115 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3116 inode->i_mode = le32_to_cpu(grant->mode);
3117 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3118 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3119 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
3120 from_kuid(&init_user_ns, inode->i_uid),
3121 from_kgid(&init_user_ns, inode->i_gid));
3124 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
3125 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
3126 set_nlink(inode, le32_to_cpu(grant->nlink));
3127 if (inode->i_nlink == 0 &&
3128 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
3129 deleted_inode = true;
3132 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3133 grant->xattr_len) {
3134 int len = le32_to_cpu(grant->xattr_len);
3135 u64 version = le64_to_cpu(grant->xattr_version);
3137 if (version > ci->i_xattrs.version) {
3138 dout(" got new xattrs v%llu on %p len %d\n",
3139 version, inode, len);
3140 if (ci->i_xattrs.blob)
3141 ceph_buffer_put(ci->i_xattrs.blob);
3142 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3143 ci->i_xattrs.version = version;
3144 ceph_forget_all_cached_acls(inode);
3148 if (newcaps & CEPH_CAP_ANY_RD) {
3149 struct timespec64 mtime, atime, ctime;
3150 /* ctime/mtime/atime? */
3151 ceph_decode_timespec64(&mtime, &grant->mtime);
3152 ceph_decode_timespec64(&atime, &grant->atime);
3153 ceph_decode_timespec64(&ctime, &grant->ctime);
3154 ceph_fill_file_time(inode, extra_info->issued,
3155 le32_to_cpu(grant->time_warp_seq),
3156 &ctime, &mtime, &atime);
3159 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3160 ci->i_files = extra_info->nfiles;
3161 ci->i_subdirs = extra_info->nsubdirs;
3164 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3165 /* file layout may have changed */
3166 s64 old_pool = ci->i_layout.pool_id;
3167 struct ceph_string *old_ns;
3169 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3170 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3171 lockdep_is_held(&ci->i_ceph_lock));
3172 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
3174 if (ci->i_layout.pool_id != old_pool ||
3175 extra_info->pool_ns != old_ns)
3176 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
3178 extra_info->pool_ns = old_ns;
3180 /* size/truncate_seq? */
3181 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
3182 le32_to_cpu(grant->truncate_seq),
3183 le64_to_cpu(grant->truncate_size),
3184 size);
3187 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3188 if (max_size != ci->i_max_size) {
3189 dout("max_size %lld -> %llu\n",
3190 ci->i_max_size, max_size);
3191 ci->i_max_size = max_size;
3192 if (max_size >= ci->i_wanted_max_size) {
3193 ci->i_wanted_max_size = 0; /* reset */
3194 ci->i_requested_max_size = 0;
3196 wake = true;
3197 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3198 ci->i_wanted_max_size > ci->i_requested_max_size) {
3199 /* CEPH_CAP_OP_IMPORT */
3200 wake = true;
3204 /* check cap bits */
3205 wanted = __ceph_caps_wanted(ci);
3206 used = __ceph_caps_used(ci);
3207 dirty = __ceph_caps_dirty(ci);
3208 dout(" my wanted = %s, used = %s, dirty %s\n",
3209 ceph_cap_string(wanted),
3210 ceph_cap_string(used),
3211 ceph_cap_string(dirty));
3212 if (wanted != le32_to_cpu(grant->wanted)) {
3213 dout("mds wanted %s -> %s\n",
3214 ceph_cap_string(le32_to_cpu(grant->wanted)),
3215 ceph_cap_string(wanted));
3216 /* imported cap may not have correct mds_wanted */
3217 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
3218 check_caps = 1;
3221 /* revocation, grant, or no-op? */
3222 if (cap->issued & ~newcaps) {
3223 int revoking = cap->issued & ~newcaps;
3225 dout("revocation: %s -> %s (revoking %s)\n",
3226 ceph_cap_string(cap->issued),
3227 ceph_cap_string(newcaps),
3228 ceph_cap_string(revoking));
3229 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3230 writeback = true; /* initiate writeback; will delay ack */
3231 else if (revoking == CEPH_CAP_FILE_CACHE &&
3232 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3233 queue_invalidate)
3234 ; /* do nothing yet, invalidation will be queued */
3235 else if (cap == ci->i_auth_cap)
3236 check_caps = 1; /* check auth cap only */
3237 else
3238 check_caps = 2; /* check all caps */
3239 cap->issued = newcaps;
3240 cap->implemented |= newcaps;
3241 } else if (cap->issued == newcaps) {
3242 dout("caps unchanged: %s -> %s\n",
3243 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3244 } else {
3245 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3246 ceph_cap_string(newcaps));
3247 /* non-auth MDS is revoking the newly grant caps ? */
3248 if (cap == ci->i_auth_cap &&
3249 __ceph_caps_revoking_other(ci, cap, newcaps))
3250 check_caps = 2;
3252 cap->issued = newcaps;
3253 cap->implemented |= newcaps; /* add bits only, to
3254 * avoid stepping on a
3255 * pending revocation */
3256 wake = true;
3258 BUG_ON(cap->issued & ~cap->implemented);
3260 if (extra_info->inline_version > 0 &&
3261 extra_info->inline_version >= ci->i_inline_version) {
3262 ci->i_inline_version = extra_info->inline_version;
3263 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3264 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3265 fill_inline = true;
3268 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3269 if (newcaps & ~extra_info->issued)
3270 wake = true;
3271 kick_flushing_inode_caps(session->s_mdsc, session, inode);
3272 up_read(&session->s_mdsc->snap_rwsem);
3273 } else {
3274 spin_unlock(&ci->i_ceph_lock);
3277 if (fill_inline)
3278 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3279 extra_info->inline_len);
3281 if (queue_trunc)
3282 ceph_queue_vmtruncate(inode);
3284 if (writeback)
3286 * queue inode for writeback: we can't actually call
3287 * filemap_write_and_wait, etc. from message handler
3288 * context.
3290 ceph_queue_writeback(inode);
3291 if (queue_invalidate)
3292 ceph_queue_invalidate(inode);
3293 if (deleted_inode)
3294 invalidate_aliases(inode);
3295 if (wake)
3296 wake_up_all(&ci->i_cap_wq);
3298 if (check_caps == 1)
3299 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3300 session);
3301 else if (check_caps == 2)
3302 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3303 else
3304 mutex_unlock(&session->s_mutex);
3308 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3309 * MDS has been safely committed.
3311 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3312 struct ceph_mds_caps *m,
3313 struct ceph_mds_session *session,
3314 struct ceph_cap *cap)
3315 __releases(ci->i_ceph_lock)
3317 struct ceph_inode_info *ci = ceph_inode(inode);
3318 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3319 struct ceph_cap_flush *cf, *tmp_cf;
3320 LIST_HEAD(to_remove);
3321 unsigned seq = le32_to_cpu(m->seq);
3322 int dirty = le32_to_cpu(m->dirty);
3323 int cleaned = 0;
3324 bool drop = false;
3325 bool wake_ci = false;
3326 bool wake_mdsc = false;
3328 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3329 if (cf->tid == flush_tid)
3330 cleaned = cf->caps;
3331 if (cf->caps == 0) /* capsnap */
3332 continue;
3333 if (cf->tid <= flush_tid) {
3334 if (__finish_cap_flush(NULL, ci, cf))
3335 wake_ci = true;
3336 list_add_tail(&cf->i_list, &to_remove);
3337 } else {
3338 cleaned &= ~cf->caps;
3339 if (!cleaned)
3340 break;
3344 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3345 " flushing %s -> %s\n",
3346 inode, session->s_mds, seq, ceph_cap_string(dirty),
3347 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3348 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3350 if (list_empty(&to_remove) && !cleaned)
3351 goto out;
3353 ci->i_flushing_caps &= ~cleaned;
3355 spin_lock(&mdsc->cap_dirty_lock);
3357 list_for_each_entry(cf, &to_remove, i_list) {
3358 if (__finish_cap_flush(mdsc, NULL, cf))
3359 wake_mdsc = true;
3362 if (ci->i_flushing_caps == 0) {
3363 if (list_empty(&ci->i_cap_flush_list)) {
3364 list_del_init(&ci->i_flushing_item);
3365 if (!list_empty(&session->s_cap_flushing)) {
3366 dout(" mds%d still flushing cap on %p\n",
3367 session->s_mds,
3368 &list_first_entry(&session->s_cap_flushing,
3369 struct ceph_inode_info,
3370 i_flushing_item)->vfs_inode);
3373 mdsc->num_cap_flushing--;
3374 dout(" inode %p now !flushing\n", inode);
3376 if (ci->i_dirty_caps == 0) {
3377 dout(" inode %p now clean\n", inode);
3378 BUG_ON(!list_empty(&ci->i_dirty_item));
3379 drop = true;
3380 if (ci->i_wr_ref == 0 &&
3381 ci->i_wrbuffer_ref_head == 0) {
3382 BUG_ON(!ci->i_head_snapc);
3383 ceph_put_snap_context(ci->i_head_snapc);
3384 ci->i_head_snapc = NULL;
3386 } else {
3387 BUG_ON(list_empty(&ci->i_dirty_item));
3390 spin_unlock(&mdsc->cap_dirty_lock);
3392 out:
3393 spin_unlock(&ci->i_ceph_lock);
3395 while (!list_empty(&to_remove)) {
3396 cf = list_first_entry(&to_remove,
3397 struct ceph_cap_flush, i_list);
3398 list_del(&cf->i_list);
3399 ceph_free_cap_flush(cf);
3402 if (wake_ci)
3403 wake_up_all(&ci->i_cap_wq);
3404 if (wake_mdsc)
3405 wake_up_all(&mdsc->cap_flushing_wq);
3406 if (drop)
3407 iput(inode);
3411 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3412 * throw away our cap_snap.
3414 * Caller hold s_mutex.
3416 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3417 struct ceph_mds_caps *m,
3418 struct ceph_mds_session *session)
3420 struct ceph_inode_info *ci = ceph_inode(inode);
3421 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
3422 u64 follows = le64_to_cpu(m->snap_follows);
3423 struct ceph_cap_snap *capsnap;
3424 bool flushed = false;
3425 bool wake_ci = false;
3426 bool wake_mdsc = false;
3428 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3429 inode, ci, session->s_mds, follows);
3431 spin_lock(&ci->i_ceph_lock);
3432 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3433 if (capsnap->follows == follows) {
3434 if (capsnap->cap_flush.tid != flush_tid) {
3435 dout(" cap_snap %p follows %lld tid %lld !="
3436 " %lld\n", capsnap, follows,
3437 flush_tid, capsnap->cap_flush.tid);
3438 break;
3440 flushed = true;
3441 break;
3442 } else {
3443 dout(" skipping cap_snap %p follows %lld\n",
3444 capsnap, capsnap->follows);
3447 if (flushed) {
3448 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3449 dout(" removing %p cap_snap %p follows %lld\n",
3450 inode, capsnap, follows);
3451 list_del(&capsnap->ci_item);
3452 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3453 wake_ci = true;
3455 spin_lock(&mdsc->cap_dirty_lock);
3457 if (list_empty(&ci->i_cap_flush_list))
3458 list_del_init(&ci->i_flushing_item);
3460 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3461 wake_mdsc = true;
3463 spin_unlock(&mdsc->cap_dirty_lock);
3465 spin_unlock(&ci->i_ceph_lock);
3466 if (flushed) {
3467 ceph_put_snap_context(capsnap->context);
3468 ceph_put_cap_snap(capsnap);
3469 if (wake_ci)
3470 wake_up_all(&ci->i_cap_wq);
3471 if (wake_mdsc)
3472 wake_up_all(&mdsc->cap_flushing_wq);
3473 iput(inode);
3478 * Handle TRUNC from MDS, indicating file truncation.
3480 * caller hold s_mutex.
3482 static void handle_cap_trunc(struct inode *inode,
3483 struct ceph_mds_caps *trunc,
3484 struct ceph_mds_session *session)
3485 __releases(ci->i_ceph_lock)
3487 struct ceph_inode_info *ci = ceph_inode(inode);
3488 int mds = session->s_mds;
3489 int seq = le32_to_cpu(trunc->seq);
3490 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3491 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3492 u64 size = le64_to_cpu(trunc->size);
3493 int implemented = 0;
3494 int dirty = __ceph_caps_dirty(ci);
3495 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3496 int queue_trunc = 0;
3498 issued |= implemented | dirty;
3500 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3501 inode, mds, seq, truncate_size, truncate_seq);
3502 queue_trunc = ceph_fill_file_size(inode, issued,
3503 truncate_seq, truncate_size, size);
3504 spin_unlock(&ci->i_ceph_lock);
3506 if (queue_trunc)
3507 ceph_queue_vmtruncate(inode);
3511 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3512 * different one. If we are the most recent migration we've seen (as
3513 * indicated by mseq), make note of the migrating cap bits for the
3514 * duration (until we see the corresponding IMPORT).
3516 * caller holds s_mutex
3518 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
3519 struct ceph_mds_cap_peer *ph,
3520 struct ceph_mds_session *session)
3522 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
3523 struct ceph_mds_session *tsession = NULL;
3524 struct ceph_cap *cap, *tcap, *new_cap = NULL;
3525 struct ceph_inode_info *ci = ceph_inode(inode);
3526 u64 t_cap_id;
3527 unsigned mseq = le32_to_cpu(ex->migrate_seq);
3528 unsigned t_seq, t_mseq;
3529 int target, issued;
3530 int mds = session->s_mds;
3532 if (ph) {
3533 t_cap_id = le64_to_cpu(ph->cap_id);
3534 t_seq = le32_to_cpu(ph->seq);
3535 t_mseq = le32_to_cpu(ph->mseq);
3536 target = le32_to_cpu(ph->mds);
3537 } else {
3538 t_cap_id = t_seq = t_mseq = 0;
3539 target = -1;
3542 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3543 inode, ci, mds, mseq, target);
3544 retry:
3545 spin_lock(&ci->i_ceph_lock);
3546 cap = __get_cap_for_mds(ci, mds);
3547 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
3548 goto out_unlock;
3550 if (target < 0) {
3551 __ceph_remove_cap(cap, false);
3552 if (!ci->i_auth_cap)
3553 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
3554 goto out_unlock;
3558 * now we know we haven't received the cap import message yet
3559 * because the exported cap still exist.
3562 issued = cap->issued;
3563 if (issued != cap->implemented)
3564 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3565 "ino (%llx.%llx) mds%d seq %d mseq %d "
3566 "issued %s implemented %s\n",
3567 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3568 ceph_cap_string(issued),
3569 ceph_cap_string(cap->implemented));
3572 tcap = __get_cap_for_mds(ci, target);
3573 if (tcap) {
3574 /* already have caps from the target */
3575 if (tcap->cap_id == t_cap_id &&
3576 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3577 dout(" updating import cap %p mds%d\n", tcap, target);
3578 tcap->cap_id = t_cap_id;
3579 tcap->seq = t_seq - 1;
3580 tcap->issue_seq = t_seq - 1;
3581 tcap->issued |= issued;
3582 tcap->implemented |= issued;
3583 if (cap == ci->i_auth_cap)
3584 ci->i_auth_cap = tcap;
3586 if (!list_empty(&ci->i_cap_flush_list) &&
3587 ci->i_auth_cap == tcap) {
3588 spin_lock(&mdsc->cap_dirty_lock);
3589 list_move_tail(&ci->i_flushing_item,
3590 &tcap->session->s_cap_flushing);
3591 spin_unlock(&mdsc->cap_dirty_lock);
3594 __ceph_remove_cap(cap, false);
3595 goto out_unlock;
3596 } else if (tsession) {
3597 /* add placeholder for the export tagert */
3598 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
3599 tcap = new_cap;
3600 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
3601 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3603 if (!list_empty(&ci->i_cap_flush_list) &&
3604 ci->i_auth_cap == tcap) {
3605 spin_lock(&mdsc->cap_dirty_lock);
3606 list_move_tail(&ci->i_flushing_item,
3607 &tcap->session->s_cap_flushing);
3608 spin_unlock(&mdsc->cap_dirty_lock);
3611 __ceph_remove_cap(cap, false);
3612 goto out_unlock;
3615 spin_unlock(&ci->i_ceph_lock);
3616 mutex_unlock(&session->s_mutex);
3618 /* open target session */
3619 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3620 if (!IS_ERR(tsession)) {
3621 if (mds > target) {
3622 mutex_lock(&session->s_mutex);
3623 mutex_lock_nested(&tsession->s_mutex,
3624 SINGLE_DEPTH_NESTING);
3625 } else {
3626 mutex_lock(&tsession->s_mutex);
3627 mutex_lock_nested(&session->s_mutex,
3628 SINGLE_DEPTH_NESTING);
3630 new_cap = ceph_get_cap(mdsc, NULL);
3631 } else {
3632 WARN_ON(1);
3633 tsession = NULL;
3634 target = -1;
3635 mutex_lock(&session->s_mutex);
3637 goto retry;
3639 out_unlock:
3640 spin_unlock(&ci->i_ceph_lock);
3641 mutex_unlock(&session->s_mutex);
3642 if (tsession) {
3643 mutex_unlock(&tsession->s_mutex);
3644 ceph_put_mds_session(tsession);
3646 if (new_cap)
3647 ceph_put_cap(mdsc, new_cap);
3651 * Handle cap IMPORT.
3653 * caller holds s_mutex. acquires i_ceph_lock
3655 static void handle_cap_import(struct ceph_mds_client *mdsc,
3656 struct inode *inode, struct ceph_mds_caps *im,
3657 struct ceph_mds_cap_peer *ph,
3658 struct ceph_mds_session *session,
3659 struct ceph_cap **target_cap, int *old_issued)
3660 __acquires(ci->i_ceph_lock)
3662 struct ceph_inode_info *ci = ceph_inode(inode);
3663 struct ceph_cap *cap, *ocap, *new_cap = NULL;
3664 int mds = session->s_mds;
3665 int issued;
3666 unsigned caps = le32_to_cpu(im->caps);
3667 unsigned wanted = le32_to_cpu(im->wanted);
3668 unsigned seq = le32_to_cpu(im->seq);
3669 unsigned mseq = le32_to_cpu(im->migrate_seq);
3670 u64 realmino = le64_to_cpu(im->realm);
3671 u64 cap_id = le64_to_cpu(im->cap_id);
3672 u64 p_cap_id;
3673 int peer;
3675 if (ph) {
3676 p_cap_id = le64_to_cpu(ph->cap_id);
3677 peer = le32_to_cpu(ph->mds);
3678 } else {
3679 p_cap_id = 0;
3680 peer = -1;
3683 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3684 inode, ci, mds, mseq, peer);
3686 retry:
3687 spin_lock(&ci->i_ceph_lock);
3688 cap = __get_cap_for_mds(ci, mds);
3689 if (!cap) {
3690 if (!new_cap) {
3691 spin_unlock(&ci->i_ceph_lock);
3692 new_cap = ceph_get_cap(mdsc, NULL);
3693 goto retry;
3695 cap = new_cap;
3696 } else {
3697 if (new_cap) {
3698 ceph_put_cap(mdsc, new_cap);
3699 new_cap = NULL;
3703 __ceph_caps_issued(ci, &issued);
3704 issued |= __ceph_caps_dirty(ci);
3706 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
3707 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3709 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3710 if (ocap && ocap->cap_id == p_cap_id) {
3711 dout(" remove export cap %p mds%d flags %d\n",
3712 ocap, peer, ph->flags);
3713 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
3714 (ocap->seq != le32_to_cpu(ph->seq) ||
3715 ocap->mseq != le32_to_cpu(ph->mseq))) {
3716 pr_err_ratelimited("handle_cap_import: "
3717 "mismatched seq/mseq: ino (%llx.%llx) "
3718 "mds%d seq %d mseq %d importer mds%d "
3719 "has peer seq %d mseq %d\n",
3720 ceph_vinop(inode), peer, ocap->seq,
3721 ocap->mseq, mds, le32_to_cpu(ph->seq),
3722 le32_to_cpu(ph->mseq));
3724 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
3727 /* make sure we re-request max_size, if necessary */
3728 ci->i_requested_max_size = 0;
3730 *old_issued = issued;
3731 *target_cap = cap;
3735 * Handle a caps message from the MDS.
3737 * Identify the appropriate session, inode, and call the right handler
3738 * based on the cap op.
3740 void ceph_handle_caps(struct ceph_mds_session *session,
3741 struct ceph_msg *msg)
3743 struct ceph_mds_client *mdsc = session->s_mdsc;
3744 struct inode *inode;
3745 struct ceph_inode_info *ci;
3746 struct ceph_cap *cap;
3747 struct ceph_mds_caps *h;
3748 struct ceph_mds_cap_peer *peer = NULL;
3749 struct ceph_snap_realm *realm = NULL;
3750 int op;
3751 int msg_version = le16_to_cpu(msg->hdr.version);
3752 u32 seq, mseq;
3753 struct ceph_vino vino;
3754 void *snaptrace;
3755 size_t snaptrace_len;
3756 void *p, *end;
3757 struct cap_extra_info extra_info = {};
3759 dout("handle_caps from mds%d\n", session->s_mds);
3761 /* decode */
3762 end = msg->front.iov_base + msg->front.iov_len;
3763 if (msg->front.iov_len < sizeof(*h))
3764 goto bad;
3765 h = msg->front.iov_base;
3766 op = le32_to_cpu(h->op);
3767 vino.ino = le64_to_cpu(h->ino);
3768 vino.snap = CEPH_NOSNAP;
3769 seq = le32_to_cpu(h->seq);
3770 mseq = le32_to_cpu(h->migrate_seq);
3772 snaptrace = h + 1;
3773 snaptrace_len = le32_to_cpu(h->snap_trace_len);
3774 p = snaptrace + snaptrace_len;
3776 if (msg_version >= 2) {
3777 u32 flock_len;
3778 ceph_decode_32_safe(&p, end, flock_len, bad);
3779 if (p + flock_len > end)
3780 goto bad;
3781 p += flock_len;
3784 if (msg_version >= 3) {
3785 if (op == CEPH_CAP_OP_IMPORT) {
3786 if (p + sizeof(*peer) > end)
3787 goto bad;
3788 peer = p;
3789 p += sizeof(*peer);
3790 } else if (op == CEPH_CAP_OP_EXPORT) {
3791 /* recorded in unused fields */
3792 peer = (void *)&h->size;
3796 if (msg_version >= 4) {
3797 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
3798 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
3799 if (p + extra_info.inline_len > end)
3800 goto bad;
3801 extra_info.inline_data = p;
3802 p += extra_info.inline_len;
3805 if (msg_version >= 5) {
3806 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3807 u32 epoch_barrier;
3809 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3810 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3813 if (msg_version >= 8) {
3814 u64 flush_tid;
3815 u32 caller_uid, caller_gid;
3816 u32 pool_ns_len;
3818 /* version >= 6 */
3819 ceph_decode_64_safe(&p, end, flush_tid, bad);
3820 /* version >= 7 */
3821 ceph_decode_32_safe(&p, end, caller_uid, bad);
3822 ceph_decode_32_safe(&p, end, caller_gid, bad);
3823 /* version >= 8 */
3824 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
3825 if (pool_ns_len > 0) {
3826 ceph_decode_need(&p, end, pool_ns_len, bad);
3827 extra_info.pool_ns =
3828 ceph_find_or_create_string(p, pool_ns_len);
3829 p += pool_ns_len;
3833 if (msg_version >= 11) {
3834 struct ceph_timespec *btime;
3835 u64 change_attr;
3836 u32 flags;
3838 /* version >= 9 */
3839 if (p + sizeof(*btime) > end)
3840 goto bad;
3841 btime = p;
3842 p += sizeof(*btime);
3843 ceph_decode_64_safe(&p, end, change_attr, bad);
3844 /* version >= 10 */
3845 ceph_decode_32_safe(&p, end, flags, bad);
3846 /* version >= 11 */
3847 extra_info.dirstat_valid = true;
3848 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
3849 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
3852 /* lookup ino */
3853 inode = ceph_find_inode(mdsc->fsc->sb, vino);
3854 ci = ceph_inode(inode);
3855 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3856 vino.snap, inode);
3858 mutex_lock(&session->s_mutex);
3859 session->s_seq++;
3860 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3861 (unsigned)seq);
3863 if (!inode) {
3864 dout(" i don't have ino %llx\n", vino.ino);
3866 if (op == CEPH_CAP_OP_IMPORT) {
3867 cap = ceph_get_cap(mdsc, NULL);
3868 cap->cap_ino = vino.ino;
3869 cap->queue_release = 1;
3870 cap->cap_id = le64_to_cpu(h->cap_id);
3871 cap->mseq = mseq;
3872 cap->seq = seq;
3873 cap->issue_seq = seq;
3874 spin_lock(&session->s_cap_lock);
3875 list_add_tail(&cap->session_caps,
3876 &session->s_cap_releases);
3877 session->s_num_cap_releases++;
3878 spin_unlock(&session->s_cap_lock);
3880 goto flush_cap_releases;
3883 /* these will work even if we don't have a cap yet */
3884 switch (op) {
3885 case CEPH_CAP_OP_FLUSHSNAP_ACK:
3886 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
3887 h, session);
3888 goto done;
3890 case CEPH_CAP_OP_EXPORT:
3891 handle_cap_export(inode, h, peer, session);
3892 goto done_unlocked;
3894 case CEPH_CAP_OP_IMPORT:
3895 realm = NULL;
3896 if (snaptrace_len) {
3897 down_write(&mdsc->snap_rwsem);
3898 ceph_update_snap_trace(mdsc, snaptrace,
3899 snaptrace + snaptrace_len,
3900 false, &realm);
3901 downgrade_write(&mdsc->snap_rwsem);
3902 } else {
3903 down_read(&mdsc->snap_rwsem);
3905 handle_cap_import(mdsc, inode, h, peer, session,
3906 &cap, &extra_info.issued);
3907 handle_cap_grant(inode, session, cap,
3908 h, msg->middle, &extra_info);
3909 if (realm)
3910 ceph_put_snap_realm(mdsc, realm);
3911 goto done_unlocked;
3914 /* the rest require a cap */
3915 spin_lock(&ci->i_ceph_lock);
3916 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
3917 if (!cap) {
3918 dout(" no cap on %p ino %llx.%llx from mds%d\n",
3919 inode, ceph_ino(inode), ceph_snap(inode),
3920 session->s_mds);
3921 spin_unlock(&ci->i_ceph_lock);
3922 goto flush_cap_releases;
3925 /* note that each of these drops i_ceph_lock for us */
3926 switch (op) {
3927 case CEPH_CAP_OP_REVOKE:
3928 case CEPH_CAP_OP_GRANT:
3929 __ceph_caps_issued(ci, &extra_info.issued);
3930 extra_info.issued |= __ceph_caps_dirty(ci);
3931 handle_cap_grant(inode, session, cap,
3932 h, msg->middle, &extra_info);
3933 goto done_unlocked;
3935 case CEPH_CAP_OP_FLUSH_ACK:
3936 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
3937 h, session, cap);
3938 break;
3940 case CEPH_CAP_OP_TRUNC:
3941 handle_cap_trunc(inode, h, session);
3942 break;
3944 default:
3945 spin_unlock(&ci->i_ceph_lock);
3946 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3947 ceph_cap_op_name(op));
3950 goto done;
3952 flush_cap_releases:
3954 * send any cap release message to try to move things
3955 * along for the mds (who clearly thinks we still have this
3956 * cap).
3958 ceph_send_cap_releases(mdsc, session);
3960 done:
3961 mutex_unlock(&session->s_mutex);
3962 done_unlocked:
3963 iput(inode);
3964 ceph_put_string(extra_info.pool_ns);
3965 return;
3967 bad:
3968 pr_err("ceph_handle_caps: corrupt message\n");
3969 ceph_msg_dump(msg);
3970 return;
3974 * Delayed work handler to process end of delayed cap release LRU list.
3976 void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
3978 struct inode *inode;
3979 struct ceph_inode_info *ci;
3980 int flags = CHECK_CAPS_NODELAY;
3982 dout("check_delayed_caps\n");
3983 while (1) {
3984 spin_lock(&mdsc->cap_delay_lock);
3985 if (list_empty(&mdsc->cap_delay_list))
3986 break;
3987 ci = list_first_entry(&mdsc->cap_delay_list,
3988 struct ceph_inode_info,
3989 i_cap_delay_list);
3990 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3991 time_before(jiffies, ci->i_hold_caps_max))
3992 break;
3993 list_del_init(&ci->i_cap_delay_list);
3995 inode = igrab(&ci->vfs_inode);
3996 spin_unlock(&mdsc->cap_delay_lock);
3998 if (inode) {
3999 dout("check_delayed_caps on %p\n", inode);
4000 ceph_check_caps(ci, flags, NULL);
4001 iput(inode);
4004 spin_unlock(&mdsc->cap_delay_lock);
4008 * Flush all dirty caps to the mds
4010 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4012 struct ceph_inode_info *ci;
4013 struct inode *inode;
4015 dout("flush_dirty_caps\n");
4016 spin_lock(&mdsc->cap_dirty_lock);
4017 while (!list_empty(&mdsc->cap_dirty)) {
4018 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
4019 i_dirty_item);
4020 inode = &ci->vfs_inode;
4021 ihold(inode);
4022 dout("flush_dirty_caps %p\n", inode);
4023 spin_unlock(&mdsc->cap_dirty_lock);
4024 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
4025 iput(inode);
4026 spin_lock(&mdsc->cap_dirty_lock);
4028 spin_unlock(&mdsc->cap_dirty_lock);
4029 dout("flush_dirty_caps done\n");
4032 void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
4034 int i;
4035 int bits = (fmode << 1) | 1;
4036 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4037 if (bits & (1 << i))
4038 ci->i_nr_by_mode[i]++;
4043 * Drop open file reference. If we were the last open file,
4044 * we may need to release capabilities to the MDS (or schedule
4045 * their delayed release).
4047 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
4049 int i, last = 0;
4050 int bits = (fmode << 1) | 1;
4051 spin_lock(&ci->i_ceph_lock);
4052 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4053 if (bits & (1 << i)) {
4054 BUG_ON(ci->i_nr_by_mode[i] == 0);
4055 if (--ci->i_nr_by_mode[i] == 0)
4056 last++;
4059 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
4060 &ci->vfs_inode, fmode,
4061 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
4062 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
4063 spin_unlock(&ci->i_ceph_lock);
4065 if (last && ci->i_vino.snap == CEPH_NOSNAP)
4066 ceph_check_caps(ci, 0, NULL);
4070 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
4071 * looks like the link count will hit 0, drop any other caps (other
4072 * than PIN) we don't specifically want (due to the file still being
4073 * open).
4075 int ceph_drop_caps_for_unlink(struct inode *inode)
4077 struct ceph_inode_info *ci = ceph_inode(inode);
4078 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4080 spin_lock(&ci->i_ceph_lock);
4081 if (inode->i_nlink == 1) {
4082 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4084 ci->i_ceph_flags |= CEPH_I_NODELAY;
4085 if (__ceph_caps_dirty(ci)) {
4086 struct ceph_mds_client *mdsc =
4087 ceph_inode_to_client(inode)->mdsc;
4088 __cap_delay_requeue_front(mdsc, ci);
4091 spin_unlock(&ci->i_ceph_lock);
4092 return drop;
4096 * Helpers for embedding cap and dentry lease releases into mds
4097 * requests.
4099 * @force is used by dentry_release (below) to force inclusion of a
4100 * record for the directory inode, even when there aren't any caps to
4101 * drop.
4103 int ceph_encode_inode_release(void **p, struct inode *inode,
4104 int mds, int drop, int unless, int force)
4106 struct ceph_inode_info *ci = ceph_inode(inode);
4107 struct ceph_cap *cap;
4108 struct ceph_mds_request_release *rel = *p;
4109 int used, dirty;
4110 int ret = 0;
4112 spin_lock(&ci->i_ceph_lock);
4113 used = __ceph_caps_used(ci);
4114 dirty = __ceph_caps_dirty(ci);
4116 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4117 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
4118 ceph_cap_string(unless));
4120 /* only drop unused, clean caps */
4121 drop &= ~(used | dirty);
4123 cap = __get_cap_for_mds(ci, mds);
4124 if (cap && __cap_is_valid(cap)) {
4125 unless &= cap->issued;
4126 if (unless) {
4127 if (unless & CEPH_CAP_AUTH_EXCL)
4128 drop &= ~CEPH_CAP_AUTH_SHARED;
4129 if (unless & CEPH_CAP_LINK_EXCL)
4130 drop &= ~CEPH_CAP_LINK_SHARED;
4131 if (unless & CEPH_CAP_XATTR_EXCL)
4132 drop &= ~CEPH_CAP_XATTR_SHARED;
4133 if (unless & CEPH_CAP_FILE_EXCL)
4134 drop &= ~CEPH_CAP_FILE_SHARED;
4137 if (force || (cap->issued & drop)) {
4138 if (cap->issued & drop) {
4139 int wanted = __ceph_caps_wanted(ci);
4140 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4141 wanted |= cap->mds_wanted;
4142 dout("encode_inode_release %p cap %p "
4143 "%s -> %s, wanted %s -> %s\n", inode, cap,
4144 ceph_cap_string(cap->issued),
4145 ceph_cap_string(cap->issued & ~drop),
4146 ceph_cap_string(cap->mds_wanted),
4147 ceph_cap_string(wanted));
4149 cap->issued &= ~drop;
4150 cap->implemented &= ~drop;
4151 cap->mds_wanted = wanted;
4152 } else {
4153 dout("encode_inode_release %p cap %p %s"
4154 " (force)\n", inode, cap,
4155 ceph_cap_string(cap->issued));
4158 rel->ino = cpu_to_le64(ceph_ino(inode));
4159 rel->cap_id = cpu_to_le64(cap->cap_id);
4160 rel->seq = cpu_to_le32(cap->seq);
4161 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4162 rel->mseq = cpu_to_le32(cap->mseq);
4163 rel->caps = cpu_to_le32(cap->implemented);
4164 rel->wanted = cpu_to_le32(cap->mds_wanted);
4165 rel->dname_len = 0;
4166 rel->dname_seq = 0;
4167 *p += sizeof(*rel);
4168 ret = 1;
4169 } else {
4170 dout("encode_inode_release %p cap %p %s (noop)\n",
4171 inode, cap, ceph_cap_string(cap->issued));
4174 spin_unlock(&ci->i_ceph_lock);
4175 return ret;
4178 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4179 struct inode *dir,
4180 int mds, int drop, int unless)
4182 struct dentry *parent = NULL;
4183 struct ceph_mds_request_release *rel = *p;
4184 struct ceph_dentry_info *di = ceph_dentry(dentry);
4185 int force = 0;
4186 int ret;
4189 * force an record for the directory caps if we have a dentry lease.
4190 * this is racy (can't take i_ceph_lock and d_lock together), but it
4191 * doesn't have to be perfect; the mds will revoke anything we don't
4192 * release.
4194 spin_lock(&dentry->d_lock);
4195 if (di->lease_session && di->lease_session->s_mds == mds)
4196 force = 1;
4197 if (!dir) {
4198 parent = dget(dentry->d_parent);
4199 dir = d_inode(parent);
4201 spin_unlock(&dentry->d_lock);
4203 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4204 dput(parent);
4206 spin_lock(&dentry->d_lock);
4207 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4208 dout("encode_dentry_release %p mds%d seq %d\n",
4209 dentry, mds, (int)di->lease_seq);
4210 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4211 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4212 *p += dentry->d_name.len;
4213 rel->dname_seq = cpu_to_le32(di->lease_seq);
4214 __ceph_mdsc_drop_dentry_lease(dentry);
4216 spin_unlock(&dentry->d_lock);
4217 return ret;