]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/ceph/caps.c
libceph: adding missing message types to ceph_msg_type_name()
[thirdparty/kernel/linux.git] / fs / ceph / caps.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
3d14c5d2 2#include <linux/ceph/ceph_debug.h>
a8599bd8
SW
3
4#include <linux/fs.h>
5#include <linux/kernel.h>
174cd4b1 6#include <linux/sched/signal.h>
5a0e3ad6 7#include <linux/slab.h>
a8599bd8
SW
8#include <linux/vmalloc.h>
9#include <linux/wait.h>
f1a3d572 10#include <linux/writeback.h>
a8599bd8
SW
11
12#include "super.h"
3d14c5d2 13#include "mds_client.h"
99ccbd22 14#include "cache.h"
3d14c5d2
YS
15#include <linux/ceph/decode.h>
16#include <linux/ceph/messenger.h>
a8599bd8
SW
17
18/*
19 * Capability management
20 *
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.
26 *
27 * If the client holds a *_SHARED cap, the client has a coherent value
28 * that can be safely read from the cached inode.
29 *
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.
34 *
35 * In the event of a conflicting operation (perhaps by another
36 * client), the MDS will revoke the conflicting client capabilities.
37 *
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.
42 */
43
0e294387 44static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
7bc00fdd
YZ
45static 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);
a8599bd8
SW
49
50/*
51 * Generate readable cap strings for debugging output.
52 */
53#define MAX_CAP_STR 20
54static char cap_str[MAX_CAP_STR][40];
55static DEFINE_SPINLOCK(cap_str_lock);
56static int last_cap_str;
57
58static char *gcap_string(char *s, int c)
59{
60 if (c & CEPH_CAP_GSHARED)
61 *s++ = 's';
62 if (c & CEPH_CAP_GEXCL)
63 *s++ = 'x';
64 if (c & CEPH_CAP_GCACHE)
65 *s++ = 'c';
66 if (c & CEPH_CAP_GRD)
67 *s++ = 'r';
68 if (c & CEPH_CAP_GWR)
69 *s++ = 'w';
70 if (c & CEPH_CAP_GBUFFER)
71 *s++ = 'b';
72 if (c & CEPH_CAP_GLAZYIO)
73 *s++ = 'l';
74 return s;
75}
76
77const char *ceph_cap_string(int caps)
78{
79 int i;
80 char *s;
81 int c;
82
83 spin_lock(&cap_str_lock);
84 i = last_cap_str++;
85 if (last_cap_str == MAX_CAP_STR)
86 last_cap_str = 0;
87 spin_unlock(&cap_str_lock);
88
89 s = cap_str[i];
90
91 if (caps & CEPH_CAP_PIN)
92 *s++ = 'p';
93
94 c = (caps >> CEPH_CAP_SAUTH) & 3;
95 if (c) {
96 *s++ = 'A';
97 s = gcap_string(s, c);
98 }
99
100 c = (caps >> CEPH_CAP_SLINK) & 3;
101 if (c) {
102 *s++ = 'L';
103 s = gcap_string(s, c);
104 }
105
106 c = (caps >> CEPH_CAP_SXATTR) & 3;
107 if (c) {
108 *s++ = 'X';
109 s = gcap_string(s, c);
110 }
111
112 c = caps >> CEPH_CAP_SFILE;
113 if (c) {
114 *s++ = 'F';
115 s = gcap_string(s, c);
116 }
117
118 if (s == cap_str[i])
119 *s++ = '-';
120 *s = 0;
121 return cap_str[i];
122}
123
37151668 124void ceph_caps_init(struct ceph_mds_client *mdsc)
a8599bd8 125{
37151668
YS
126 INIT_LIST_HEAD(&mdsc->caps_list);
127 spin_lock_init(&mdsc->caps_list_lock);
a8599bd8
SW
128}
129
37151668 130void ceph_caps_finalize(struct ceph_mds_client *mdsc)
a8599bd8
SW
131{
132 struct ceph_cap *cap;
133
37151668
YS
134 spin_lock(&mdsc->caps_list_lock);
135 while (!list_empty(&mdsc->caps_list)) {
136 cap = list_first_entry(&mdsc->caps_list,
137 struct ceph_cap, caps_item);
a8599bd8
SW
138 list_del(&cap->caps_item);
139 kmem_cache_free(ceph_cap_cachep, cap);
140 }
37151668
YS
141 mdsc->caps_total_count = 0;
142 mdsc->caps_avail_count = 0;
143 mdsc->caps_use_count = 0;
144 mdsc->caps_reserve_count = 0;
145 mdsc->caps_min_count = 0;
146 spin_unlock(&mdsc->caps_list_lock);
85ccce43
SW
147}
148
37151668 149void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
85ccce43 150{
37151668
YS
151 spin_lock(&mdsc->caps_list_lock);
152 mdsc->caps_min_count += delta;
153 BUG_ON(mdsc->caps_min_count < 0);
154 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
155}
156
e30ee581
ZZ
157/*
158 * Called under mdsc->mutex.
159 */
160int ceph_reserve_caps(struct ceph_mds_client *mdsc,
37151668 161 struct ceph_cap_reservation *ctx, int need)
a8599bd8 162{
e30ee581 163 int i, j;
a8599bd8
SW
164 struct ceph_cap *cap;
165 int have;
166 int alloc = 0;
e30ee581
ZZ
167 int max_caps;
168 bool trimmed = false;
169 struct ceph_mds_session *s;
a8599bd8 170 LIST_HEAD(newcaps);
a8599bd8
SW
171
172 dout("reserve caps ctx=%p need=%d\n", ctx, need);
173
174 /* first reserve any caps that are already allocated */
37151668
YS
175 spin_lock(&mdsc->caps_list_lock);
176 if (mdsc->caps_avail_count >= need)
a8599bd8
SW
177 have = need;
178 else
37151668
YS
179 have = mdsc->caps_avail_count;
180 mdsc->caps_avail_count -= have;
181 mdsc->caps_reserve_count += have;
182 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
183 mdsc->caps_reserve_count +
184 mdsc->caps_avail_count);
185 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
186
187 for (i = have; i < need; i++) {
e30ee581 188retry:
a8599bd8 189 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
e30ee581
ZZ
190 if (!cap) {
191 if (!trimmed) {
192 for (j = 0; j < mdsc->max_sessions; j++) {
193 s = __ceph_lookup_mds_session(mdsc, j);
194 if (!s)
195 continue;
196 mutex_unlock(&mdsc->mutex);
197
198 mutex_lock(&s->s_mutex);
199 max_caps = s->s_nr_caps - (need - i);
200 ceph_trim_caps(mdsc, s, max_caps);
201 mutex_unlock(&s->s_mutex);
202
203 ceph_put_mds_session(s);
204 mutex_lock(&mdsc->mutex);
205 }
206 trimmed = true;
207 goto retry;
208 } else {
209 pr_warn("reserve caps ctx=%p ENOMEM "
210 "need=%d got=%d\n",
211 ctx, need, have + alloc);
212 goto out_nomem;
213 }
214 }
a8599bd8
SW
215 list_add(&cap->caps_item, &newcaps);
216 alloc++;
217 }
e30ee581 218 BUG_ON(have + alloc != need);
a8599bd8 219
37151668
YS
220 spin_lock(&mdsc->caps_list_lock);
221 mdsc->caps_total_count += alloc;
222 mdsc->caps_reserve_count += alloc;
223 list_splice(&newcaps, &mdsc->caps_list);
a8599bd8 224
37151668
YS
225 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
226 mdsc->caps_reserve_count +
227 mdsc->caps_avail_count);
228 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
229
230 ctx->count = need;
231 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
37151668
YS
232 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
233 mdsc->caps_reserve_count, mdsc->caps_avail_count);
e30ee581
ZZ
234 return 0;
235
236out_nomem:
237 while (!list_empty(&newcaps)) {
238 cap = list_first_entry(&newcaps,
239 struct ceph_cap, caps_item);
240 list_del(&cap->caps_item);
241 kmem_cache_free(ceph_cap_cachep, cap);
242 }
243
244 spin_lock(&mdsc->caps_list_lock);
245 mdsc->caps_avail_count += have;
246 mdsc->caps_reserve_count -= have;
247 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
248 mdsc->caps_reserve_count +
249 mdsc->caps_avail_count);
250 spin_unlock(&mdsc->caps_list_lock);
251 return -ENOMEM;
a8599bd8
SW
252}
253
37151668
YS
254int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
255 struct ceph_cap_reservation *ctx)
a8599bd8
SW
256{
257 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
258 if (ctx->count) {
37151668
YS
259 spin_lock(&mdsc->caps_list_lock);
260 BUG_ON(mdsc->caps_reserve_count < ctx->count);
261 mdsc->caps_reserve_count -= ctx->count;
262 mdsc->caps_avail_count += ctx->count;
a8599bd8
SW
263 ctx->count = 0;
264 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
37151668
YS
265 mdsc->caps_total_count, mdsc->caps_use_count,
266 mdsc->caps_reserve_count, mdsc->caps_avail_count);
267 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
268 mdsc->caps_reserve_count +
269 mdsc->caps_avail_count);
270 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
271 }
272 return 0;
273}
274
d9df2783
YZ
275struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
276 struct ceph_cap_reservation *ctx)
a8599bd8
SW
277{
278 struct ceph_cap *cap = NULL;
279
280 /* temporary, until we do something about cap import/export */
443b3760
SW
281 if (!ctx) {
282 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
283 if (cap) {
4d1d0534 284 spin_lock(&mdsc->caps_list_lock);
37151668
YS
285 mdsc->caps_use_count++;
286 mdsc->caps_total_count++;
4d1d0534 287 spin_unlock(&mdsc->caps_list_lock);
443b3760
SW
288 }
289 return cap;
290 }
a8599bd8 291
37151668 292 spin_lock(&mdsc->caps_list_lock);
a8599bd8 293 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
37151668
YS
294 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
295 mdsc->caps_reserve_count, mdsc->caps_avail_count);
a8599bd8 296 BUG_ON(!ctx->count);
37151668
YS
297 BUG_ON(ctx->count > mdsc->caps_reserve_count);
298 BUG_ON(list_empty(&mdsc->caps_list));
a8599bd8
SW
299
300 ctx->count--;
37151668
YS
301 mdsc->caps_reserve_count--;
302 mdsc->caps_use_count++;
a8599bd8 303
37151668 304 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
a8599bd8
SW
305 list_del(&cap->caps_item);
306
37151668
YS
307 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
308 mdsc->caps_reserve_count + mdsc->caps_avail_count);
309 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
310 return cap;
311}
312
37151668 313void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
a8599bd8 314{
37151668 315 spin_lock(&mdsc->caps_list_lock);
7c1332b8 316 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
37151668
YS
317 cap, mdsc->caps_total_count, mdsc->caps_use_count,
318 mdsc->caps_reserve_count, mdsc->caps_avail_count);
319 mdsc->caps_use_count--;
a8599bd8 320 /*
85ccce43
SW
321 * Keep some preallocated caps around (ceph_min_count), to
322 * avoid lots of free/alloc churn.
a8599bd8 323 */
37151668
YS
324 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
325 mdsc->caps_min_count) {
326 mdsc->caps_total_count--;
a8599bd8
SW
327 kmem_cache_free(ceph_cap_cachep, cap);
328 } else {
37151668
YS
329 mdsc->caps_avail_count++;
330 list_add(&cap->caps_item, &mdsc->caps_list);
a8599bd8
SW
331 }
332
37151668
YS
333 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
334 mdsc->caps_reserve_count + mdsc->caps_avail_count);
335 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
336}
337
3d14c5d2 338void ceph_reservation_status(struct ceph_fs_client *fsc,
85ccce43
SW
339 int *total, int *avail, int *used, int *reserved,
340 int *min)
a8599bd8 341{
3d14c5d2 342 struct ceph_mds_client *mdsc = fsc->mdsc;
37151668 343
a8599bd8 344 if (total)
37151668 345 *total = mdsc->caps_total_count;
a8599bd8 346 if (avail)
37151668 347 *avail = mdsc->caps_avail_count;
a8599bd8 348 if (used)
37151668 349 *used = mdsc->caps_use_count;
a8599bd8 350 if (reserved)
37151668 351 *reserved = mdsc->caps_reserve_count;
85ccce43 352 if (min)
37151668 353 *min = mdsc->caps_min_count;
a8599bd8
SW
354}
355
356/*
357 * Find ceph_cap for given mds, if any.
358 *
be655596 359 * Called with i_ceph_lock held.
a8599bd8
SW
360 */
361static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
362{
363 struct ceph_cap *cap;
364 struct rb_node *n = ci->i_caps.rb_node;
365
366 while (n) {
367 cap = rb_entry(n, struct ceph_cap, ci_node);
368 if (mds < cap->mds)
369 n = n->rb_left;
370 else if (mds > cap->mds)
371 n = n->rb_right;
372 else
373 return cap;
374 }
375 return NULL;
376}
377
2bc50259
GF
378struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
379{
380 struct ceph_cap *cap;
381
be655596 382 spin_lock(&ci->i_ceph_lock);
2bc50259 383 cap = __get_cap_for_mds(ci, mds);
be655596 384 spin_unlock(&ci->i_ceph_lock);
2bc50259
GF
385 return cap;
386}
387
a8599bd8 388/*
33caad32 389 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
a8599bd8 390 */
ca81f3f6 391static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
a8599bd8
SW
392{
393 struct ceph_cap *cap;
394 int mds = -1;
395 struct rb_node *p;
396
33caad32 397 /* prefer mds with WR|BUFFER|EXCL caps */
a8599bd8
SW
398 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
399 cap = rb_entry(p, struct ceph_cap, ci_node);
400 mds = cap->mds;
a8599bd8
SW
401 if (cap->issued & (CEPH_CAP_FILE_WR |
402 CEPH_CAP_FILE_BUFFER |
403 CEPH_CAP_FILE_EXCL))
404 break;
405 }
406 return mds;
407}
408
409int ceph_get_cap_mds(struct inode *inode)
410{
be655596 411 struct ceph_inode_info *ci = ceph_inode(inode);
a8599bd8 412 int mds;
be655596 413 spin_lock(&ci->i_ceph_lock);
ca81f3f6 414 mds = __ceph_get_cap_mds(ceph_inode(inode));
be655596 415 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
416 return mds;
417}
418
419/*
be655596 420 * Called under i_ceph_lock.
a8599bd8
SW
421 */
422static void __insert_cap_node(struct ceph_inode_info *ci,
423 struct ceph_cap *new)
424{
425 struct rb_node **p = &ci->i_caps.rb_node;
426 struct rb_node *parent = NULL;
427 struct ceph_cap *cap = NULL;
428
429 while (*p) {
430 parent = *p;
431 cap = rb_entry(parent, struct ceph_cap, ci_node);
432 if (new->mds < cap->mds)
433 p = &(*p)->rb_left;
434 else if (new->mds > cap->mds)
435 p = &(*p)->rb_right;
436 else
437 BUG();
438 }
439
440 rb_link_node(&new->ci_node, parent, p);
441 rb_insert_color(&new->ci_node, &ci->i_caps);
442}
443
444/*
445 * (re)set cap hold timeouts, which control the delayed release
446 * of unused caps back to the MDS. Should be called on cap use.
447 */
448static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
449 struct ceph_inode_info *ci)
450{
3d14c5d2 451 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
a8599bd8
SW
452
453 ci->i_hold_caps_min = round_jiffies(jiffies +
454 ma->caps_wanted_delay_min * HZ);
455 ci->i_hold_caps_max = round_jiffies(jiffies +
456 ma->caps_wanted_delay_max * HZ);
457 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
458 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
459}
460
461/*
462 * (Re)queue cap at the end of the delayed cap release list.
463 *
464 * If I_FLUSH is set, leave the inode at the front of the list.
465 *
be655596 466 * Caller holds i_ceph_lock
a8599bd8
SW
467 * -> we take mdsc->cap_delay_lock
468 */
469static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
470 struct ceph_inode_info *ci)
471{
472 __cap_set_timeouts(mdsc, ci);
473 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
474 ci->i_ceph_flags, ci->i_hold_caps_max);
475 if (!mdsc->stopping) {
476 spin_lock(&mdsc->cap_delay_lock);
477 if (!list_empty(&ci->i_cap_delay_list)) {
478 if (ci->i_ceph_flags & CEPH_I_FLUSH)
479 goto no_change;
480 list_del_init(&ci->i_cap_delay_list);
481 }
482 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
483no_change:
484 spin_unlock(&mdsc->cap_delay_lock);
485 }
486}
487
488/*
489 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
490 * indicating we should send a cap message to flush dirty metadata
491 * asap, and move to the front of the delayed cap list.
492 */
493static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
494 struct ceph_inode_info *ci)
495{
496 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
497 spin_lock(&mdsc->cap_delay_lock);
498 ci->i_ceph_flags |= CEPH_I_FLUSH;
499 if (!list_empty(&ci->i_cap_delay_list))
500 list_del_init(&ci->i_cap_delay_list);
501 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
502 spin_unlock(&mdsc->cap_delay_lock);
503}
504
505/*
506 * Cancel delayed work on cap.
507 *
be655596 508 * Caller must hold i_ceph_lock.
a8599bd8
SW
509 */
510static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
511 struct ceph_inode_info *ci)
512{
513 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
514 if (list_empty(&ci->i_cap_delay_list))
515 return;
516 spin_lock(&mdsc->cap_delay_lock);
517 list_del_init(&ci->i_cap_delay_list);
518 spin_unlock(&mdsc->cap_delay_lock);
519}
520
521/*
522 * Common issue checks for add_cap, handle_cap_grant.
523 */
524static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
525 unsigned issued)
526{
527 unsigned had = __ceph_caps_issued(ci, NULL);
528
529 /*
530 * Each time we receive FILE_CACHE anew, we increment
531 * i_rdcache_gen.
532 */
2962507c 533 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
99ccbd22 534 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
a8599bd8 535 ci->i_rdcache_gen++;
99ccbd22 536 }
a8599bd8
SW
537
538 /*
15b51bd6
YZ
539 * If FILE_SHARED is newly issued, mark dir not complete. We don't
540 * know what happened to this directory while we didn't have the cap.
541 * If FILE_SHARED is being revoked, also mark dir not complete. It
542 * stops on-going cached readdir.
a8599bd8 543 */
15b51bd6
YZ
544 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
545 if (issued & CEPH_CAP_FILE_SHARED)
97aeb6bf 546 atomic_inc(&ci->i_shared_gen);
a8673d61
YZ
547 if (S_ISDIR(ci->vfs_inode.i_mode)) {
548 dout(" marking %p NOT complete\n", &ci->vfs_inode);
2f276c51 549 __ceph_dir_clear_complete(ci);
a8673d61 550 }
a8599bd8
SW
551 }
552}
553
554/*
555 * Add a capability under the given MDS session.
556 *
557 * Caller should hold session snap_rwsem (read) and s_mutex.
558 *
559 * @fmode is the open file mode, if we are opening a file, otherwise
560 * it is < 0. (This is so we can atomically add the cap and add an
561 * open file reference to it.)
562 */
d9df2783
YZ
563void ceph_add_cap(struct inode *inode,
564 struct ceph_mds_session *session, u64 cap_id,
565 int fmode, unsigned issued, unsigned wanted,
566 unsigned seq, unsigned mseq, u64 realmino, int flags,
567 struct ceph_cap **new_cap)
a8599bd8 568{
3d14c5d2 569 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
a8599bd8 570 struct ceph_inode_info *ci = ceph_inode(inode);
a8599bd8
SW
571 struct ceph_cap *cap;
572 int mds = session->s_mds;
573 int actual_wanted;
574
575 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
576 session->s_mds, cap_id, ceph_cap_string(issued), seq);
577
578 /*
579 * If we are opening the file, include file mode wanted bits
580 * in wanted.
581 */
582 if (fmode >= 0)
583 wanted |= ceph_caps_for_mode(fmode);
584
a8599bd8
SW
585 cap = __get_cap_for_mds(ci, mds);
586 if (!cap) {
d9df2783
YZ
587 cap = *new_cap;
588 *new_cap = NULL;
a8599bd8
SW
589
590 cap->issued = 0;
591 cap->implemented = 0;
592 cap->mds = mds;
593 cap->mds_wanted = 0;
964266cc 594 cap->mseq = 0;
a8599bd8
SW
595
596 cap->ci = ci;
597 __insert_cap_node(ci, cap);
598
a8599bd8
SW
599 /* add to session cap list */
600 cap->session = session;
601 spin_lock(&session->s_cap_lock);
602 list_add_tail(&cap->session_caps, &session->s_caps);
603 session->s_nr_caps++;
604 spin_unlock(&session->s_cap_lock);
11df2dfb 605 } else {
11df2dfb
YZ
606 /*
607 * auth mds of the inode changed. we received the cap export
608 * message, but still haven't received the cap import message.
609 * handle_cap_export() updated the new auth MDS' cap.
610 *
611 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
612 * a message that was send before the cap import message. So
613 * don't remove caps.
614 */
615 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
616 WARN_ON(cap != ci->i_auth_cap);
617 WARN_ON(cap->cap_id != cap_id);
618 seq = cap->seq;
619 mseq = cap->mseq;
620 issued |= cap->issued;
621 flags |= CEPH_CAP_FLAG_AUTH;
622 }
623 }
a8599bd8 624
7d9c9193
YZ
625 if (!ci->i_snap_realm ||
626 ((flags & CEPH_CAP_FLAG_AUTH) &&
627 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
a8599bd8
SW
628 /*
629 * add this inode to the appropriate snap realm
630 */
631 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
632 realmino);
633 if (realm) {
7d9c9193
YZ
634 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
635 if (oldrealm) {
636 spin_lock(&oldrealm->inodes_with_caps_lock);
637 list_del_init(&ci->i_snap_realm_item);
638 spin_unlock(&oldrealm->inodes_with_caps_lock);
639 }
640
a8599bd8
SW
641 spin_lock(&realm->inodes_with_caps_lock);
642 ci->i_snap_realm = realm;
643 list_add(&ci->i_snap_realm_item,
644 &realm->inodes_with_caps);
645 spin_unlock(&realm->inodes_with_caps_lock);
7d9c9193
YZ
646
647 if (oldrealm)
648 ceph_put_snap_realm(mdsc, oldrealm);
a8599bd8
SW
649 } else {
650 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
651 realmino);
b8cd07e7 652 WARN_ON(!realm);
a8599bd8
SW
653 }
654 }
655
656 __check_cap_issue(ci, cap, issued);
657
658 /*
659 * If we are issued caps we don't want, or the mds' wanted
660 * value appears to be off, queue a check so we'll release
661 * later and/or update the mds wanted value.
662 */
663 actual_wanted = __ceph_caps_wanted(ci);
664 if ((wanted & ~actual_wanted) ||
665 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
666 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
667 ceph_cap_string(issued), ceph_cap_string(wanted),
668 ceph_cap_string(actual_wanted));
669 __cap_delay_requeue(mdsc, ci);
670 }
671
b8c2f3ae 672 if (flags & CEPH_CAP_FLAG_AUTH) {
d37b1d99 673 if (!ci->i_auth_cap ||
d9ffc4f7 674 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
b8c2f3ae 675 ci->i_auth_cap = cap;
d9ffc4f7
YZ
676 cap->mds_wanted = wanted;
677 }
11df2dfb
YZ
678 } else {
679 WARN_ON(ci->i_auth_cap == cap);
8a92a119 680 }
a8599bd8
SW
681
682 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
683 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
684 ceph_cap_string(issued|cap->issued), seq, mds);
685 cap->cap_id = cap_id;
686 cap->issued = issued;
687 cap->implemented |= issued;
d1b87809 688 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
964266cc
YZ
689 cap->mds_wanted = wanted;
690 else
691 cap->mds_wanted |= wanted;
a8599bd8
SW
692 cap->seq = seq;
693 cap->issue_seq = seq;
694 cap->mseq = mseq;
685f9a5d 695 cap->cap_gen = session->s_cap_gen;
a8599bd8
SW
696
697 if (fmode >= 0)
698 __ceph_get_fmode(ci, fmode);
a8599bd8
SW
699}
700
701/*
702 * Return true if cap has not timed out and belongs to the current
703 * generation of the MDS session (i.e. has not gone 'stale' due to
704 * us losing touch with the mds).
705 */
706static int __cap_is_valid(struct ceph_cap *cap)
707{
708 unsigned long ttl;
cdac8303 709 u32 gen;
a8599bd8 710
d8fb02ab 711 spin_lock(&cap->session->s_gen_ttl_lock);
a8599bd8
SW
712 gen = cap->session->s_cap_gen;
713 ttl = cap->session->s_cap_ttl;
d8fb02ab 714 spin_unlock(&cap->session->s_gen_ttl_lock);
a8599bd8 715
685f9a5d 716 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
a8599bd8
SW
717 dout("__cap_is_valid %p cap %p issued %s "
718 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
685f9a5d 719 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
a8599bd8
SW
720 return 0;
721 }
722
723 return 1;
724}
725
726/*
727 * Return set of valid cap bits issued to us. Note that caps time
728 * out, and may be invalidated in bulk if the client session times out
729 * and session->s_cap_gen is bumped.
730 */
731int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
732{
d9df2783 733 int have = ci->i_snap_caps;
a8599bd8
SW
734 struct ceph_cap *cap;
735 struct rb_node *p;
736
737 if (implemented)
738 *implemented = 0;
739 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
740 cap = rb_entry(p, struct ceph_cap, ci_node);
741 if (!__cap_is_valid(cap))
742 continue;
743 dout("__ceph_caps_issued %p cap %p issued %s\n",
744 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
745 have |= cap->issued;
746 if (implemented)
747 *implemented |= cap->implemented;
748 }
b1530f57
YZ
749 /*
750 * exclude caps issued by non-auth MDS, but are been revoking
751 * by the auth MDS. The non-auth MDS should be revoking/exporting
752 * these caps, but the message is delayed.
753 */
754 if (ci->i_auth_cap) {
755 cap = ci->i_auth_cap;
756 have &= ~cap->implemented | cap->issued;
757 }
a8599bd8
SW
758 return have;
759}
760
761/*
762 * Get cap bits issued by caps other than @ocap
763 */
764int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
765{
766 int have = ci->i_snap_caps;
767 struct ceph_cap *cap;
768 struct rb_node *p;
769
770 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
771 cap = rb_entry(p, struct ceph_cap, ci_node);
772 if (cap == ocap)
773 continue;
774 if (!__cap_is_valid(cap))
775 continue;
776 have |= cap->issued;
777 }
778 return have;
779}
780
781/*
782 * Move a cap to the end of the LRU (oldest caps at list head, newest
783 * at list tail).
784 */
785static void __touch_cap(struct ceph_cap *cap)
786{
787 struct ceph_mds_session *s = cap->session;
788
a8599bd8 789 spin_lock(&s->s_cap_lock);
d37b1d99 790 if (!s->s_cap_iterator) {
5dacf091
SW
791 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
792 s->s_mds);
793 list_move_tail(&cap->session_caps, &s->s_caps);
794 } else {
795 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
796 &cap->ci->vfs_inode, cap, s->s_mds);
797 }
a8599bd8
SW
798 spin_unlock(&s->s_cap_lock);
799}
800
801/*
802 * Check if we hold the given mask. If so, move the cap(s) to the
803 * front of their respective LRUs. (This is the preferred way for
804 * callers to check for caps they want.)
805 */
806int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
807{
808 struct ceph_cap *cap;
809 struct rb_node *p;
810 int have = ci->i_snap_caps;
811
812 if ((have & mask) == mask) {
813 dout("__ceph_caps_issued_mask %p snap issued %s"
814 " (mask %s)\n", &ci->vfs_inode,
815 ceph_cap_string(have),
816 ceph_cap_string(mask));
817 return 1;
818 }
819
820 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
821 cap = rb_entry(p, struct ceph_cap, ci_node);
822 if (!__cap_is_valid(cap))
823 continue;
824 if ((cap->issued & mask) == mask) {
825 dout("__ceph_caps_issued_mask %p cap %p issued %s"
826 " (mask %s)\n", &ci->vfs_inode, cap,
827 ceph_cap_string(cap->issued),
828 ceph_cap_string(mask));
829 if (touch)
830 __touch_cap(cap);
831 return 1;
832 }
833
834 /* does a combination of caps satisfy mask? */
835 have |= cap->issued;
836 if ((have & mask) == mask) {
837 dout("__ceph_caps_issued_mask %p combo issued %s"
838 " (mask %s)\n", &ci->vfs_inode,
839 ceph_cap_string(cap->issued),
840 ceph_cap_string(mask));
841 if (touch) {
842 struct rb_node *q;
843
25985edc 844 /* touch this + preceding caps */
a8599bd8
SW
845 __touch_cap(cap);
846 for (q = rb_first(&ci->i_caps); q != p;
847 q = rb_next(q)) {
848 cap = rb_entry(q, struct ceph_cap,
849 ci_node);
850 if (!__cap_is_valid(cap))
851 continue;
852 __touch_cap(cap);
853 }
854 }
855 return 1;
856 }
857 }
858
859 return 0;
860}
861
862/*
863 * Return true if mask caps are currently being revoked by an MDS.
864 */
6ee6b953
YZ
865int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
866 struct ceph_cap *ocap, int mask)
a8599bd8 867{
a8599bd8
SW
868 struct ceph_cap *cap;
869 struct rb_node *p;
a8599bd8 870
a8599bd8
SW
871 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
872 cap = rb_entry(p, struct ceph_cap, ci_node);
9563f88c 873 if (cap != ocap &&
6ee6b953
YZ
874 (cap->implemented & ~cap->issued & mask))
875 return 1;
a8599bd8 876 }
6ee6b953
YZ
877 return 0;
878}
879
880int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
881{
882 struct inode *inode = &ci->vfs_inode;
883 int ret;
884
885 spin_lock(&ci->i_ceph_lock);
886 ret = __ceph_caps_revoking_other(ci, NULL, mask);
be655596 887 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
888 dout("ceph_caps_revoking %p %s = %d\n", inode,
889 ceph_cap_string(mask), ret);
890 return ret;
891}
892
893int __ceph_caps_used(struct ceph_inode_info *ci)
894{
895 int used = 0;
896 if (ci->i_pin_ref)
897 used |= CEPH_CAP_PIN;
898 if (ci->i_rd_ref)
899 used |= CEPH_CAP_FILE_RD;
fdd4e158
YZ
900 if (ci->i_rdcache_ref ||
901 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
902 ci->vfs_inode.i_data.nrpages))
a8599bd8
SW
903 used |= CEPH_CAP_FILE_CACHE;
904 if (ci->i_wr_ref)
905 used |= CEPH_CAP_FILE_WR;
d3d0720d 906 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
a8599bd8
SW
907 used |= CEPH_CAP_FILE_BUFFER;
908 return used;
909}
910
911/*
912 * wanted, by virtue of open file modes
913 */
914int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
915{
774a6a11
YZ
916 int i, bits = 0;
917 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
918 if (ci->i_nr_by_mode[i])
919 bits |= 1 << i;
920 }
921 if (bits == 0)
922 return 0;
923 return ceph_caps_for_mode(bits >> 1);
a8599bd8
SW
924}
925
926/*
927 * Return caps we have registered with the MDS(s) as 'wanted'.
928 */
c1944fed 929int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
a8599bd8
SW
930{
931 struct ceph_cap *cap;
932 struct rb_node *p;
933 int mds_wanted = 0;
934
935 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
936 cap = rb_entry(p, struct ceph_cap, ci_node);
c1944fed 937 if (check && !__cap_is_valid(cap))
a8599bd8 938 continue;
a2550604
YZ
939 if (cap == ci->i_auth_cap)
940 mds_wanted |= cap->mds_wanted;
941 else
942 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
a8599bd8
SW
943 }
944 return mds_wanted;
945}
946
947/*
be655596 948 * called under i_ceph_lock
a8599bd8 949 */
0f439c74
YZ
950static int __ceph_is_single_caps(struct ceph_inode_info *ci)
951{
952 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
953}
954
a8599bd8
SW
955static int __ceph_is_any_caps(struct ceph_inode_info *ci)
956{
d9df2783 957 return !RB_EMPTY_ROOT(&ci->i_caps);
a8599bd8
SW
958}
959
9215aeea
YZ
960int ceph_is_any_caps(struct inode *inode)
961{
962 struct ceph_inode_info *ci = ceph_inode(inode);
963 int ret;
964
965 spin_lock(&ci->i_ceph_lock);
966 ret = __ceph_is_any_caps(ci);
967 spin_unlock(&ci->i_ceph_lock);
968
969 return ret;
970}
971
db40cc17
YZ
972static void drop_inode_snap_realm(struct ceph_inode_info *ci)
973{
974 struct ceph_snap_realm *realm = ci->i_snap_realm;
975 spin_lock(&realm->inodes_with_caps_lock);
976 list_del_init(&ci->i_snap_realm_item);
977 ci->i_snap_realm_counter++;
978 ci->i_snap_realm = NULL;
979 spin_unlock(&realm->inodes_with_caps_lock);
980 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
981 realm);
982}
983
a8599bd8 984/*
f818a736
SW
985 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
986 *
be655596 987 * caller should hold i_ceph_lock.
a6369741 988 * caller will not hold session s_mutex if called from destroy_inode.
a8599bd8 989 */
a096b09a 990void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
a8599bd8
SW
991{
992 struct ceph_mds_session *session = cap->session;
993 struct ceph_inode_info *ci = cap->ci;
640ef79d 994 struct ceph_mds_client *mdsc =
3d14c5d2 995 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
f818a736 996 int removed = 0;
a8599bd8
SW
997
998 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
999
7c1332b8
SW
1000 /* remove from session list */
1001 spin_lock(&session->s_cap_lock);
1002 if (session->s_cap_iterator == cap) {
1003 /* not yet, we are iterating over this very cap */
1004 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1005 cap, cap->session);
1006 } else {
1007 list_del_init(&cap->session_caps);
1008 session->s_nr_caps--;
1009 cap->session = NULL;
f818a736 1010 removed = 1;
7c1332b8 1011 }
f818a736
SW
1012 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1013 cap->ci = NULL;
745a8e3b
YZ
1014
1015 /*
1016 * s_cap_reconnect is protected by s_cap_lock. no one changes
1017 * s_cap_gen while session is in the reconnect state.
1018 */
1019 if (queue_release &&
1020 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1021 cap->queue_release = 1;
1022 if (removed) {
1023 list_add_tail(&cap->session_caps,
1024 &session->s_cap_releases);
1025 session->s_num_cap_releases++;
1026 removed = 0;
1027 }
1028 } else {
1029 cap->queue_release = 0;
1030 }
1031 cap->cap_ino = ci->i_vino.ino;
1032
7c1332b8
SW
1033 spin_unlock(&session->s_cap_lock);
1034
f818a736
SW
1035 /* remove from inode list */
1036 rb_erase(&cap->ci_node, &ci->i_caps);
1037 if (ci->i_auth_cap == cap)
1038 ci->i_auth_cap = NULL;
1039
1040 if (removed)
37151668 1041 ceph_put_cap(mdsc, cap);
a8599bd8 1042
db40cc17
YZ
1043 /* when reconnect denied, we remove session caps forcibly,
1044 * i_wr_ref can be non-zero. If there are ongoing write,
1045 * keep i_snap_realm.
1046 */
1047 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1048 drop_inode_snap_realm(ci);
1049
a8599bd8
SW
1050 if (!__ceph_is_any_real_caps(ci))
1051 __cap_delay_cancel(mdsc, ci);
1052}
1053
0ff8bfb3
JL
1054struct cap_msg_args {
1055 struct ceph_mds_session *session;
1056 u64 ino, cid, follows;
1057 u64 flush_tid, oldest_flush_tid, size, max_size;
1058 u64 xattr_version;
1059 struct ceph_buffer *xattr_buf;
1060 struct timespec atime, mtime, ctime;
1061 int op, caps, wanted, dirty;
1062 u32 seq, issue_seq, mseq, time_warp_seq;
1e4ef0c6 1063 u32 flags;
0ff8bfb3
JL
1064 kuid_t uid;
1065 kgid_t gid;
1066 umode_t mode;
1067 bool inline_data;
1068};
1069
a8599bd8
SW
1070/*
1071 * Build and send a cap message to the given MDS.
1072 *
1073 * Caller should be holding s_mutex.
1074 */
0ff8bfb3 1075static int send_cap_msg(struct cap_msg_args *arg)
a8599bd8
SW
1076{
1077 struct ceph_mds_caps *fc;
1078 struct ceph_msg *msg;
e20d258d
YZ
1079 void *p;
1080 size_t extra_len;
43b29673 1081 struct timespec zerotime = {0};
92475f05 1082 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
a8599bd8
SW
1083
1084 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
a2971c8c 1085 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
0ff8bfb3
JL
1086 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1087 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1088 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1089 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1090 arg->mseq, arg->follows, arg->size, arg->max_size,
1091 arg->xattr_version,
1092 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
a8599bd8 1093
a2971c8c
YZ
1094 /* flock buffer size + inline version + inline data size +
1095 * osd_epoch_barrier + oldest_flush_tid */
43b29673 1096 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
e20d258d
YZ
1097 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1098 GFP_NOFS, false);
a79832f2
SW
1099 if (!msg)
1100 return -ENOMEM;
a8599bd8 1101
43b29673 1102 msg->hdr.version = cpu_to_le16(10);
0ff8bfb3 1103 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
a8599bd8 1104
6df058c0 1105 fc = msg->front.iov_base;
a8599bd8
SW
1106 memset(fc, 0, sizeof(*fc));
1107
0ff8bfb3
JL
1108 fc->cap_id = cpu_to_le64(arg->cid);
1109 fc->op = cpu_to_le32(arg->op);
1110 fc->seq = cpu_to_le32(arg->seq);
1111 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1112 fc->migrate_seq = cpu_to_le32(arg->mseq);
1113 fc->caps = cpu_to_le32(arg->caps);
1114 fc->wanted = cpu_to_le32(arg->wanted);
1115 fc->dirty = cpu_to_le32(arg->dirty);
1116 fc->ino = cpu_to_le64(arg->ino);
1117 fc->snap_follows = cpu_to_le64(arg->follows);
1118
1119 fc->size = cpu_to_le64(arg->size);
1120 fc->max_size = cpu_to_le64(arg->max_size);
1121 ceph_encode_timespec(&fc->mtime, &arg->mtime);
1122 ceph_encode_timespec(&fc->atime, &arg->atime);
1123 ceph_encode_timespec(&fc->ctime, &arg->ctime);
1124 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1125
1126 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1127 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1128 fc->mode = cpu_to_le32(arg->mode);
1129
1130 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1131 if (arg->xattr_buf) {
1132 msg->middle = ceph_buffer_get(arg->xattr_buf);
1133 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1134 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
9670079f
JL
1135 }
1136
e20d258d 1137 p = fc + 1;
43b29673 1138 /* flock buffer size (version 2) */
e20d258d 1139 ceph_encode_32(&p, 0);
43b29673 1140 /* inline version (version 4) */
0ff8bfb3 1141 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
e20d258d
YZ
1142 /* inline data size */
1143 ceph_encode_32(&p, 0);
92475f05
JL
1144 /*
1145 * osd_epoch_barrier (version 5)
1146 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1147 * case it was recently changed
1148 */
1149 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
43b29673 1150 /* oldest_flush_tid (version 6) */
0ff8bfb3 1151 ceph_encode_64(&p, arg->oldest_flush_tid);
e20d258d 1152
43b29673
JL
1153 /*
1154 * caller_uid/caller_gid (version 7)
1155 *
1156 * Currently, we don't properly track which caller dirtied the caps
1157 * last, and force a flush of them when there is a conflict. For now,
1158 * just set this to 0:0, to emulate how the MDS has worked up to now.
1159 */
1160 ceph_encode_32(&p, 0);
1161 ceph_encode_32(&p, 0);
1162
1163 /* pool namespace (version 8) (mds always ignores this) */
1164 ceph_encode_32(&p, 0);
1165
1166 /*
1167 * btime and change_attr (version 9)
1168 *
1169 * We just zero these out for now, as the MDS ignores them unless
1170 * the requisite feature flags are set (which we don't do yet).
1171 */
1172 ceph_encode_timespec(p, &zerotime);
1173 p += sizeof(struct ceph_timespec);
1174 ceph_encode_64(&p, 0);
1175
1176 /* Advisory flags (version 10) */
1e4ef0c6 1177 ceph_encode_32(&p, arg->flags);
43b29673 1178
0ff8bfb3 1179 ceph_con_send(&arg->session->s_con, msg);
a8599bd8
SW
1180 return 0;
1181}
1182
1183/*
a6369741 1184 * Queue cap releases when an inode is dropped from our cache. Since
be655596 1185 * inode is about to be destroyed, there is no need for i_ceph_lock.
a8599bd8
SW
1186 */
1187void ceph_queue_caps_release(struct inode *inode)
1188{
1189 struct ceph_inode_info *ci = ceph_inode(inode);
1190 struct rb_node *p;
1191
a8599bd8
SW
1192 p = rb_first(&ci->i_caps);
1193 while (p) {
1194 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
a8599bd8 1195 p = rb_next(p);
a096b09a 1196 __ceph_remove_cap(cap, true);
a8599bd8 1197 }
a8599bd8
SW
1198}
1199
1200/*
1201 * Send a cap msg on the given inode. Update our caps state, then
be655596 1202 * drop i_ceph_lock and send the message.
a8599bd8
SW
1203 *
1204 * Make note of max_size reported/requested from mds, revoked caps
1205 * that have now been implemented.
1206 *
1207 * Make half-hearted attempt ot to invalidate page cache if we are
1208 * dropping RDCACHE. Note that this will leave behind locked pages
1209 * that we'll then need to deal with elsewhere.
1210 *
1211 * Return non-zero if delayed release, or we experienced an error
1212 * such that the caller should requeue + retry later.
1213 *
be655596 1214 * called with i_ceph_lock, then drops it.
a8599bd8
SW
1215 * caller should hold snap_rwsem (read), s_mutex.
1216 */
1217static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1e4ef0c6
JL
1218 int op, bool sync, int used, int want, int retain,
1219 int flushing, u64 flush_tid, u64 oldest_flush_tid)
be655596 1220 __releases(cap->ci->i_ceph_lock)
a8599bd8
SW
1221{
1222 struct ceph_inode_info *ci = cap->ci;
1223 struct inode *inode = &ci->vfs_inode;
0ff8bfb3 1224 struct cap_msg_args arg;
bb0581f0 1225 int held, revoking;
a8599bd8 1226 int wake = 0;
a8599bd8 1227 int delayed = 0;
a8599bd8
SW
1228 int ret;
1229
68c28323
SW
1230 held = cap->issued | cap->implemented;
1231 revoking = cap->implemented & ~cap->issued;
1232 retain &= ~revoking;
68c28323 1233
a8599bd8
SW
1234 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1235 inode, cap, cap->session,
1236 ceph_cap_string(held), ceph_cap_string(held & retain),
1237 ceph_cap_string(revoking));
1238 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1239
0ff8bfb3 1240 arg.session = cap->session;
a8599bd8
SW
1241
1242 /* don't release wanted unless we've waited a bit. */
1243 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1244 time_before(jiffies, ci->i_hold_caps_min)) {
1245 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1246 ceph_cap_string(cap->issued),
1247 ceph_cap_string(cap->issued & retain),
1248 ceph_cap_string(cap->mds_wanted),
1249 ceph_cap_string(want));
1250 want |= cap->mds_wanted;
1251 retain |= cap->issued;
1252 delayed = 1;
1253 }
1254 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
eb65b919
YZ
1255 if (want & ~cap->mds_wanted) {
1256 /* user space may open/close single file frequently.
1257 * This avoids droping mds_wanted immediately after
1258 * requesting new mds_wanted.
1259 */
1260 __cap_set_timeouts(mdsc, ci);
1261 }
a8599bd8
SW
1262
1263 cap->issued &= retain; /* drop bits we don't want */
1264 if (cap->implemented & ~cap->issued) {
1265 /*
1266 * Wake up any waiters on wanted -> needed transition.
1267 * This is due to the weird transition from buffered
1268 * to sync IO... we need to flush dirty pages _before_
1269 * allowing sync writes to avoid reordering.
1270 */
1271 wake = 1;
1272 }
1273 cap->implemented &= cap->issued | used;
1274 cap->mds_wanted = want;
1275
0ff8bfb3
JL
1276 arg.ino = ceph_vino(inode).ino;
1277 arg.cid = cap->cap_id;
1278 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1279 arg.flush_tid = flush_tid;
1280 arg.oldest_flush_tid = oldest_flush_tid;
1281
1282 arg.size = inode->i_size;
1283 ci->i_reported_size = arg.size;
1284 arg.max_size = ci->i_wanted_max_size;
1285 ci->i_requested_max_size = arg.max_size;
a8599bd8 1286
082afec9 1287 if (flushing & CEPH_CAP_XATTR_EXCL) {
a8599bd8 1288 __ceph_build_xattrs_blob(ci);
0ff8bfb3
JL
1289 arg.xattr_version = ci->i_xattrs.version;
1290 arg.xattr_buf = ci->i_xattrs.blob;
1291 } else {
1292 arg.xattr_buf = NULL;
a8599bd8
SW
1293 }
1294
0ff8bfb3
JL
1295 arg.mtime = inode->i_mtime;
1296 arg.atime = inode->i_atime;
1297 arg.ctime = inode->i_ctime;
1298
1299 arg.op = op;
1300 arg.caps = cap->implemented;
1301 arg.wanted = want;
1302 arg.dirty = flushing;
1303
1304 arg.seq = cap->seq;
1305 arg.issue_seq = cap->issue_seq;
1306 arg.mseq = cap->mseq;
1307 arg.time_warp_seq = ci->i_time_warp_seq;
1308
1309 arg.uid = inode->i_uid;
1310 arg.gid = inode->i_gid;
1311 arg.mode = inode->i_mode;
1312
1313 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
95569713
YZ
1314 if (list_empty(&ci->i_cap_snaps))
1315 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1316 else
1317 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1e4ef0c6
JL
1318 if (sync)
1319 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
e20d258d 1320
be655596 1321 spin_unlock(&ci->i_ceph_lock);
a8599bd8 1322
0ff8bfb3 1323 ret = send_cap_msg(&arg);
a8599bd8
SW
1324 if (ret < 0) {
1325 dout("error sending cap msg, must requeue %p\n", inode);
1326 delayed = 1;
1327 }
1328
1329 if (wake)
03066f23 1330 wake_up_all(&ci->i_cap_wq);
a8599bd8
SW
1331
1332 return delayed;
1333}
1334
0e294387
YZ
1335static inline int __send_flush_snap(struct inode *inode,
1336 struct ceph_mds_session *session,
1337 struct ceph_cap_snap *capsnap,
1338 u32 mseq, u64 oldest_flush_tid)
1339{
0ff8bfb3
JL
1340 struct cap_msg_args arg;
1341
1342 arg.session = session;
1343 arg.ino = ceph_vino(inode).ino;
1344 arg.cid = 0;
1345 arg.follows = capsnap->follows;
1346 arg.flush_tid = capsnap->cap_flush.tid;
1347 arg.oldest_flush_tid = oldest_flush_tid;
1348
1349 arg.size = capsnap->size;
1350 arg.max_size = 0;
1351 arg.xattr_version = capsnap->xattr_version;
1352 arg.xattr_buf = capsnap->xattr_blob;
1353
1354 arg.atime = capsnap->atime;
1355 arg.mtime = capsnap->mtime;
1356 arg.ctime = capsnap->ctime;
1357
1358 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1359 arg.caps = capsnap->issued;
1360 arg.wanted = 0;
1361 arg.dirty = capsnap->dirty;
1362
1363 arg.seq = 0;
1364 arg.issue_seq = 0;
1365 arg.mseq = mseq;
1366 arg.time_warp_seq = capsnap->time_warp_seq;
1367
1368 arg.uid = capsnap->uid;
1369 arg.gid = capsnap->gid;
1370 arg.mode = capsnap->mode;
1371
1372 arg.inline_data = capsnap->inline_data;
1e4ef0c6 1373 arg.flags = 0;
0ff8bfb3
JL
1374
1375 return send_cap_msg(&arg);
0e294387
YZ
1376}
1377
a8599bd8
SW
1378/*
1379 * When a snapshot is taken, clients accumulate dirty metadata on
1380 * inodes with capabilities in ceph_cap_snaps to describe the file
1381 * state at the time the snapshot was taken. This must be flushed
1382 * asynchronously back to the MDS once sync writes complete and dirty
1383 * data is written out.
1384 *
be655596 1385 * Called under i_ceph_lock. Takes s_mutex as needed.
a8599bd8 1386 */
ed9b430c
YZ
1387static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1388 struct ceph_mds_session *session)
be655596
SW
1389 __releases(ci->i_ceph_lock)
1390 __acquires(ci->i_ceph_lock)
a8599bd8
SW
1391{
1392 struct inode *inode = &ci->vfs_inode;
ed9b430c 1393 struct ceph_mds_client *mdsc = session->s_mdsc;
a8599bd8 1394 struct ceph_cap_snap *capsnap;
ed9b430c
YZ
1395 u64 oldest_flush_tid = 0;
1396 u64 first_tid = 1, last_tid = 0;
a8599bd8 1397
ed9b430c 1398 dout("__flush_snaps %p session %p\n", inode, session);
a8599bd8 1399
a8599bd8 1400 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
a8599bd8
SW
1401 /*
1402 * we need to wait for sync writes to complete and for dirty
1403 * pages to be written out.
1404 */
1405 if (capsnap->dirty_pages || capsnap->writing)
cfc0bf66 1406 break;
a8599bd8 1407
86056090
YZ
1408 /* should be removed by ceph_try_drop_cap_snap() */
1409 BUG_ON(!capsnap->need_flush);
819ccbfa 1410
e835124c 1411 /* only flush each capsnap once */
0e294387 1412 if (capsnap->cap_flush.tid > 0) {
ed9b430c 1413 dout(" already flushed %p, skipping\n", capsnap);
e835124c
SW
1414 continue;
1415 }
1416
553adfd9 1417 spin_lock(&mdsc->cap_dirty_lock);
0e294387
YZ
1418 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1419 list_add_tail(&capsnap->cap_flush.g_list,
1420 &mdsc->cap_flush_list);
ed9b430c
YZ
1421 if (oldest_flush_tid == 0)
1422 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
0e294387
YZ
1423 if (list_empty(&ci->i_flushing_item)) {
1424 list_add_tail(&ci->i_flushing_item,
1425 &session->s_cap_flushing);
1426 }
553adfd9
YZ
1427 spin_unlock(&mdsc->cap_dirty_lock);
1428
0e294387
YZ
1429 list_add_tail(&capsnap->cap_flush.i_list,
1430 &ci->i_cap_flush_list);
1431
ed9b430c
YZ
1432 if (first_tid == 1)
1433 first_tid = capsnap->cap_flush.tid;
1434 last_tid = capsnap->cap_flush.tid;
1435 }
1436
1437 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1438
1439 while (first_tid <= last_tid) {
1440 struct ceph_cap *cap = ci->i_auth_cap;
1441 struct ceph_cap_flush *cf;
1442 int ret;
1443
1444 if (!(cap && cap->session == session)) {
1445 dout("__flush_snaps %p auth cap %p not mds%d, "
1446 "stop\n", inode, cap, session->s_mds);
1447 break;
1448 }
1449
1450 ret = -ENOENT;
1451 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1452 if (cf->tid >= first_tid) {
1453 ret = 0;
1454 break;
1455 }
1456 }
1457 if (ret < 0)
1458 break;
1459
1460 first_tid = cf->tid + 1;
1461
1462 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
805692d0 1463 refcount_inc(&capsnap->nref);
be655596 1464 spin_unlock(&ci->i_ceph_lock);
a8599bd8 1465
ed9b430c
YZ
1466 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1467 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
a8599bd8 1468
ed9b430c
YZ
1469 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1470 oldest_flush_tid);
1471 if (ret < 0) {
1472 pr_err("__flush_snaps: error sending cap flushsnap, "
1473 "ino (%llx.%llx) tid %llu follows %llu\n",
1474 ceph_vinop(inode), cf->tid, capsnap->follows);
1475 }
a8599bd8 1476
ed9b430c 1477 ceph_put_cap_snap(capsnap);
be655596 1478 spin_lock(&ci->i_ceph_lock);
a8599bd8 1479 }
ed9b430c 1480}
a8599bd8 1481
ed9b430c
YZ
1482void ceph_flush_snaps(struct ceph_inode_info *ci,
1483 struct ceph_mds_session **psession)
1484{
1485 struct inode *inode = &ci->vfs_inode;
1486 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
e4d2b16a 1487 struct ceph_mds_session *session = NULL;
ed9b430c 1488 int mds;
e4d2b16a 1489
ed9b430c 1490 dout("ceph_flush_snaps %p\n", inode);
e4d2b16a
YZ
1491 if (psession)
1492 session = *psession;
ed9b430c
YZ
1493retry:
1494 spin_lock(&ci->i_ceph_lock);
1495 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1496 dout(" no capsnap needs flush, doing nothing\n");
1497 goto out;
1498 }
1499 if (!ci->i_auth_cap) {
1500 dout(" no auth cap (migrating?), doing nothing\n");
1501 goto out;
1502 }
a8599bd8 1503
ed9b430c
YZ
1504 mds = ci->i_auth_cap->session->s_mds;
1505 if (session && session->s_mds != mds) {
1506 dout(" oops, wrong session %p mutex\n", session);
a8599bd8
SW
1507 mutex_unlock(&session->s_mutex);
1508 ceph_put_mds_session(session);
ed9b430c
YZ
1509 session = NULL;
1510 }
1511 if (!session) {
1512 spin_unlock(&ci->i_ceph_lock);
1513 mutex_lock(&mdsc->mutex);
1514 session = __ceph_lookup_mds_session(mdsc, mds);
1515 mutex_unlock(&mdsc->mutex);
1516 if (session) {
1517 dout(" inverting session/ino locks on %p\n", session);
1518 mutex_lock(&session->s_mutex);
1519 }
1520 goto retry;
a8599bd8 1521 }
a8599bd8 1522
24d063ac
YZ
1523 // make sure flushsnap messages are sent in proper order.
1524 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1525 __kick_flushing_caps(mdsc, session, ci, 0);
1526 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1527 }
1528
ed9b430c
YZ
1529 __ceph_flush_snaps(ci, session);
1530out:
be655596 1531 spin_unlock(&ci->i_ceph_lock);
ed9b430c
YZ
1532
1533 if (psession) {
1534 *psession = session;
c858a070 1535 } else if (session) {
ed9b430c
YZ
1536 mutex_unlock(&session->s_mutex);
1537 ceph_put_mds_session(session);
1538 }
1539 /* we flushed them all; remove this inode from the queue */
1540 spin_lock(&mdsc->snap_flush_lock);
1541 list_del_init(&ci->i_snap_flush_item);
1542 spin_unlock(&mdsc->snap_flush_lock);
a8599bd8
SW
1543}
1544
76e3b390 1545/*
fca65b4a
SW
1546 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1547 * Caller is then responsible for calling __mark_inode_dirty with the
1548 * returned flags value.
76e3b390 1549 */
f66fd9f0
YZ
1550int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1551 struct ceph_cap_flush **pcf)
76e3b390 1552{
640ef79d 1553 struct ceph_mds_client *mdsc =
3d14c5d2 1554 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
76e3b390
SW
1555 struct inode *inode = &ci->vfs_inode;
1556 int was = ci->i_dirty_caps;
1557 int dirty = 0;
1558
571ade33
YZ
1559 if (!ci->i_auth_cap) {
1560 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1561 "but no auth cap (session was closed?)\n",
1562 inode, ceph_ino(inode), ceph_cap_string(mask));
1563 return 0;
1564 }
1565
76e3b390
SW
1566 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1567 ceph_cap_string(mask), ceph_cap_string(was),
1568 ceph_cap_string(was | mask));
1569 ci->i_dirty_caps |= mask;
1570 if (was == 0) {
f66fd9f0
YZ
1571 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1572 swap(ci->i_prealloc_cap_flush, *pcf);
1573
604d1b02
YZ
1574 if (!ci->i_head_snapc) {
1575 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
7d8cb26d
SW
1576 ci->i_head_snapc = ceph_get_snap_context(
1577 ci->i_snap_realm->cached_context);
604d1b02 1578 }
0685235f
YZ
1579 dout(" inode %p now dirty snapc %p auth cap %p\n",
1580 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
76e3b390
SW
1581 BUG_ON(!list_empty(&ci->i_dirty_item));
1582 spin_lock(&mdsc->cap_dirty_lock);
11df2dfb 1583 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
76e3b390
SW
1584 spin_unlock(&mdsc->cap_dirty_lock);
1585 if (ci->i_flushing_caps == 0) {
3772d26d 1586 ihold(inode);
76e3b390
SW
1587 dirty |= I_DIRTY_SYNC;
1588 }
f66fd9f0
YZ
1589 } else {
1590 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
76e3b390
SW
1591 }
1592 BUG_ON(list_empty(&ci->i_dirty_item));
1593 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1594 (mask & CEPH_CAP_FILE_BUFFER))
1595 dirty |= I_DIRTY_DATASYNC;
76e3b390 1596 __cap_delay_requeue(mdsc, ci);
fca65b4a 1597 return dirty;
76e3b390
SW
1598}
1599
f66fd9f0
YZ
1600struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1601{
1602 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1603}
1604
1605void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1606{
1607 if (cf)
1608 kmem_cache_free(ceph_cap_flush_cachep, cf);
1609}
1610
a2971c8c
YZ
1611static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1612{
e4500b5e 1613 if (!list_empty(&mdsc->cap_flush_list)) {
a2971c8c 1614 struct ceph_cap_flush *cf =
e4500b5e
YZ
1615 list_first_entry(&mdsc->cap_flush_list,
1616 struct ceph_cap_flush, g_list);
a2971c8c
YZ
1617 return cf->tid;
1618 }
1619 return 0;
1620}
1621
c8799fc4
YZ
1622/*
1623 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1624 * Return true if caller needs to wake up flush waiters.
1625 */
1626static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1627 struct ceph_inode_info *ci,
1628 struct ceph_cap_flush *cf)
1629{
1630 struct ceph_cap_flush *prev;
1631 bool wake = cf->wake;
1632 if (mdsc) {
1633 /* are there older pending cap flushes? */
1634 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1635 prev = list_prev_entry(cf, g_list);
1636 prev->wake = true;
1637 wake = false;
1638 }
1639 list_del(&cf->g_list);
1640 } else if (ci) {
1641 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1642 prev = list_prev_entry(cf, i_list);
1643 prev->wake = true;
1644 wake = false;
1645 }
1646 list_del(&cf->i_list);
1647 } else {
1648 BUG_ON(1);
1649 }
1650 return wake;
1651}
1652
a8599bd8
SW
1653/*
1654 * Add dirty inode to the flushing list. Assigned a seq number so we
1655 * can wait for caps to flush without starving.
cdc35f96 1656 *
be655596 1657 * Called under i_ceph_lock.
a8599bd8 1658 */
cdc35f96 1659static int __mark_caps_flushing(struct inode *inode,
c8799fc4 1660 struct ceph_mds_session *session, bool wake,
a2971c8c 1661 u64 *flush_tid, u64 *oldest_flush_tid)
a8599bd8 1662{
3d14c5d2 1663 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8 1664 struct ceph_inode_info *ci = ceph_inode(inode);
f66fd9f0 1665 struct ceph_cap_flush *cf = NULL;
cdc35f96 1666 int flushing;
50b885b9 1667
cdc35f96 1668 BUG_ON(ci->i_dirty_caps == 0);
a8599bd8 1669 BUG_ON(list_empty(&ci->i_dirty_item));
f66fd9f0 1670 BUG_ON(!ci->i_prealloc_cap_flush);
cdc35f96
SW
1671
1672 flushing = ci->i_dirty_caps;
1673 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1674 ceph_cap_string(flushing),
1675 ceph_cap_string(ci->i_flushing_caps),
1676 ceph_cap_string(ci->i_flushing_caps | flushing));
1677 ci->i_flushing_caps |= flushing;
1678 ci->i_dirty_caps = 0;
afcdaea3 1679 dout(" inode %p now !dirty\n", inode);
cdc35f96 1680
f66fd9f0 1681 swap(cf, ci->i_prealloc_cap_flush);
553adfd9 1682 cf->caps = flushing;
c8799fc4 1683 cf->wake = wake;
553adfd9 1684
a8599bd8 1685 spin_lock(&mdsc->cap_dirty_lock);
afcdaea3
SW
1686 list_del_init(&ci->i_dirty_item);
1687
553adfd9 1688 cf->tid = ++mdsc->last_cap_flush_tid;
e4500b5e 1689 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
a2971c8c 1690 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
553adfd9 1691
a8599bd8
SW
1692 if (list_empty(&ci->i_flushing_item)) {
1693 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1694 mdsc->num_cap_flushing++;
a8599bd8
SW
1695 }
1696 spin_unlock(&mdsc->cap_dirty_lock);
cdc35f96 1697
e4500b5e 1698 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
553adfd9
YZ
1699
1700 *flush_tid = cf->tid;
cdc35f96 1701 return flushing;
a8599bd8
SW
1702}
1703
5ecad6fd
SW
1704/*
1705 * try to invalidate mapping pages without blocking.
1706 */
5ecad6fd
SW
1707static int try_nonblocking_invalidate(struct inode *inode)
1708{
1709 struct ceph_inode_info *ci = ceph_inode(inode);
1710 u32 invalidating_gen = ci->i_rdcache_gen;
1711
be655596 1712 spin_unlock(&ci->i_ceph_lock);
5ecad6fd 1713 invalidate_mapping_pages(&inode->i_data, 0, -1);
be655596 1714 spin_lock(&ci->i_ceph_lock);
5ecad6fd 1715
18a38193 1716 if (inode->i_data.nrpages == 0 &&
5ecad6fd
SW
1717 invalidating_gen == ci->i_rdcache_gen) {
1718 /* success. */
1719 dout("try_nonblocking_invalidate %p success\n", inode);
cd045cb4
SW
1720 /* save any racing async invalidate some trouble */
1721 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
5ecad6fd
SW
1722 return 0;
1723 }
1724 dout("try_nonblocking_invalidate %p failed\n", inode);
1725 return -1;
1726}
1727
efb0ca76
YZ
1728bool __ceph_should_report_size(struct ceph_inode_info *ci)
1729{
1730 loff_t size = ci->vfs_inode.i_size;
1731 /* mds will adjust max size according to the reported size */
1732 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1733 return false;
1734 if (size >= ci->i_max_size)
1735 return true;
1736 /* half of previous max_size increment has been used */
1737 if (ci->i_max_size > ci->i_reported_size &&
1738 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1739 return true;
1740 return false;
1741}
1742
a8599bd8
SW
1743/*
1744 * Swiss army knife function to examine currently used and wanted
1745 * versus held caps. Release, flush, ack revoked caps to mds as
1746 * appropriate.
1747 *
1748 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1749 * cap release further.
1750 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1751 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1752 * further delay.
1753 */
1754void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1755 struct ceph_mds_session *session)
1756{
3d14c5d2
YS
1757 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1758 struct ceph_mds_client *mdsc = fsc->mdsc;
a8599bd8
SW
1759 struct inode *inode = &ci->vfs_inode;
1760 struct ceph_cap *cap;
a2971c8c 1761 u64 flush_tid, oldest_flush_tid;
395c312b 1762 int file_wanted, used, cap_used;
a8599bd8 1763 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
cbd03635 1764 int issued, implemented, want, retain, revoking, flushing = 0;
a8599bd8
SW
1765 int mds = -1; /* keep track of how far we've gone through i_caps list
1766 to avoid an infinite loop on retry */
1767 struct rb_node *p;
0f439c74
YZ
1768 int delayed = 0, sent = 0;
1769 bool no_delay = flags & CHECK_CAPS_NODELAY;
3609404f 1770 bool queue_invalidate = false;
3609404f 1771 bool tried_invalidate = false;
a8599bd8
SW
1772
1773 /* if we are unmounting, flush any unused caps immediately. */
1774 if (mdsc->stopping)
0f439c74 1775 no_delay = true;
a8599bd8 1776
be655596 1777 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1778
1779 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1780 flags |= CHECK_CAPS_FLUSH;
1781
0f439c74
YZ
1782 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1783 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1784 __cap_delay_cancel(mdsc, ci);
1785
a8599bd8
SW
1786 goto retry_locked;
1787retry:
be655596 1788 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1789retry_locked:
1790 file_wanted = __ceph_caps_file_wanted(ci);
1791 used = __ceph_caps_used(ci);
cbd03635
SW
1792 issued = __ceph_caps_issued(ci, &implemented);
1793 revoking = implemented & ~issued;
a8599bd8 1794
41445999
YZ
1795 want = file_wanted;
1796 retain = file_wanted | used | CEPH_CAP_PIN;
a8599bd8 1797 if (!mdsc->stopping && inode->i_nlink > 0) {
41445999 1798 if (file_wanted) {
a8599bd8 1799 retain |= CEPH_CAP_ANY; /* be greedy */
32ec4397
YZ
1800 } else if (S_ISDIR(inode->i_mode) &&
1801 (issued & CEPH_CAP_FILE_SHARED) &&
1802 __ceph_dir_is_complete(ci)) {
1803 /*
1804 * If a directory is complete, we want to keep
1805 * the exclusive cap. So that MDS does not end up
1806 * revoking the shared cap on every create/unlink
1807 * operation.
1808 */
1809 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1810 retain |= want;
a8599bd8 1811 } else {
32ec4397 1812
a8599bd8
SW
1813 retain |= CEPH_CAP_ANY_SHARED;
1814 /*
1815 * keep RD only if we didn't have the file open RW,
1816 * because then the mds would revoke it anyway to
1817 * journal max_size=0.
1818 */
1819 if (ci->i_max_size == 0)
1820 retain |= CEPH_CAP_ANY_RD;
1821 }
1822 }
1823
1824 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
cbd03635 1825 " issued %s revoking %s retain %s %s%s%s\n", inode,
a8599bd8
SW
1826 ceph_cap_string(file_wanted),
1827 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1828 ceph_cap_string(ci->i_flushing_caps),
cbd03635 1829 ceph_cap_string(issued), ceph_cap_string(revoking),
a8599bd8
SW
1830 ceph_cap_string(retain),
1831 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1832 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1833 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1834
1835 /*
1836 * If we no longer need to hold onto old our caps, and we may
1837 * have cached pages, but don't want them, then try to invalidate.
1838 * If we fail, it's because pages are locked.... try again later.
1839 */
0f439c74 1840 if ((!no_delay || mdsc->stopping) &&
fdd4e158 1841 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
9abd4db7 1842 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
fdd4e158 1843 inode->i_data.nrpages && /* have cached pages */
5e804ac4
YZ
1844 (revoking & (CEPH_CAP_FILE_CACHE|
1845 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
a8599bd8 1846 !tried_invalidate) {
a8599bd8 1847 dout("check_caps trying to invalidate on %p\n", inode);
5ecad6fd 1848 if (try_nonblocking_invalidate(inode) < 0) {
ee612d95
YZ
1849 dout("check_caps queuing invalidate\n");
1850 queue_invalidate = true;
1851 ci->i_rdcache_revoking = ci->i_rdcache_gen;
a8599bd8 1852 }
3609404f 1853 tried_invalidate = true;
a8599bd8
SW
1854 goto retry_locked;
1855 }
1856
a8599bd8
SW
1857 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1858 cap = rb_entry(p, struct ceph_cap, ci_node);
a8599bd8
SW
1859
1860 /* avoid looping forever */
1861 if (mds >= cap->mds ||
1862 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1863 continue;
1864
1865 /* NOTE: no side-effects allowed, until we take s_mutex */
1866
395c312b
YZ
1867 cap_used = used;
1868 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1869 cap_used &= ~ci->i_auth_cap->issued;
1870
a8599bd8 1871 revoking = cap->implemented & ~cap->issued;
395c312b 1872 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
9abd4db7
YZ
1873 cap->mds, cap, ceph_cap_string(cap_used),
1874 ceph_cap_string(cap->issued),
088b3f5e
SW
1875 ceph_cap_string(cap->implemented),
1876 ceph_cap_string(revoking));
a8599bd8
SW
1877
1878 if (cap == ci->i_auth_cap &&
1879 (cap->issued & CEPH_CAP_FILE_WR)) {
1880 /* request larger max_size from MDS? */
1881 if (ci->i_wanted_max_size > ci->i_max_size &&
1882 ci->i_wanted_max_size > ci->i_requested_max_size) {
1883 dout("requesting new max_size\n");
1884 goto ack;
1885 }
1886
1887 /* approaching file_max? */
efb0ca76 1888 if (__ceph_should_report_size(ci)) {
a8599bd8
SW
1889 dout("i_size approaching max_size\n");
1890 goto ack;
1891 }
1892 }
1893 /* flush anything dirty? */
7bc00fdd
YZ
1894 if (cap == ci->i_auth_cap) {
1895 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1896 dout("flushing dirty caps\n");
1897 goto ack;
1898 }
1899 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1900 dout("flushing snap caps\n");
1901 goto ack;
1902 }
a8599bd8
SW
1903 }
1904
1905 /* completed revocation? going down and there are no caps? */
395c312b 1906 if (revoking && (revoking & cap_used) == 0) {
a8599bd8
SW
1907 dout("completed revocation of %s\n",
1908 ceph_cap_string(cap->implemented & ~cap->issued));
1909 goto ack;
1910 }
1911
1912 /* want more caps from mds? */
1913 if (want & ~(cap->mds_wanted | cap->issued))
1914 goto ack;
1915
1916 /* things we might delay */
1917 if ((cap->issued & ~retain) == 0 &&
1918 cap->mds_wanted == want)
1919 continue; /* nope, all good */
1920
0f439c74 1921 if (no_delay)
a8599bd8
SW
1922 goto ack;
1923
1924 /* delay? */
1925 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1926 time_before(jiffies, ci->i_hold_caps_max)) {
1927 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1928 ceph_cap_string(cap->issued),
1929 ceph_cap_string(cap->issued & retain),
1930 ceph_cap_string(cap->mds_wanted),
1931 ceph_cap_string(want));
1932 delayed++;
1933 continue;
1934 }
1935
1936ack:
e9964c10
SW
1937 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1938 dout(" skipping %p I_NOFLUSH set\n", inode);
1939 continue;
1940 }
1941
a8599bd8
SW
1942 if (session && session != cap->session) {
1943 dout("oops, wrong session %p mutex\n", session);
1944 mutex_unlock(&session->s_mutex);
1945 session = NULL;
1946 }
1947 if (!session) {
1948 session = cap->session;
1949 if (mutex_trylock(&session->s_mutex) == 0) {
1950 dout("inverting session/ino locks on %p\n",
1951 session);
be655596 1952 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1953 if (took_snap_rwsem) {
1954 up_read(&mdsc->snap_rwsem);
1955 took_snap_rwsem = 0;
1956 }
1957 mutex_lock(&session->s_mutex);
1958 goto retry;
1959 }
1960 }
7bc00fdd
YZ
1961
1962 /* kick flushing and flush snaps before sending normal
1963 * cap message */
1964 if (cap == ci->i_auth_cap &&
1965 (ci->i_ceph_flags &
1966 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
1967 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
24d063ac 1968 __kick_flushing_caps(mdsc, session, ci, 0);
7bc00fdd
YZ
1969 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1970 }
ed9b430c
YZ
1971 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
1972 __ceph_flush_snaps(ci, session);
1973
7bc00fdd
YZ
1974 goto retry_locked;
1975 }
1976
a8599bd8
SW
1977 /* take snap_rwsem after session mutex */
1978 if (!took_snap_rwsem) {
1979 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1980 dout("inverting snap/in locks on %p\n",
1981 inode);
be655596 1982 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1983 down_read(&mdsc->snap_rwsem);
1984 took_snap_rwsem = 1;
1985 goto retry;
1986 }
1987 took_snap_rwsem = 1;
1988 }
1989
553adfd9 1990 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
c8799fc4 1991 flushing = __mark_caps_flushing(inode, session, false,
a2971c8c
YZ
1992 &flush_tid,
1993 &oldest_flush_tid);
553adfd9 1994 } else {
24be0c48 1995 flushing = 0;
553adfd9 1996 flush_tid = 0;
a2971c8c
YZ
1997 spin_lock(&mdsc->cap_dirty_lock);
1998 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1999 spin_unlock(&mdsc->cap_dirty_lock);
553adfd9 2000 }
a8599bd8
SW
2001
2002 mds = cap->mds; /* remember mds, so we don't repeat */
2003 sent++;
2004
be655596 2005 /* __send_cap drops i_ceph_lock */
1e4ef0c6
JL
2006 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2007 cap_used, want, retain, flushing,
2008 flush_tid, oldest_flush_tid);
be655596 2009 goto retry; /* retake i_ceph_lock and restart our cap scan. */
a8599bd8
SW
2010 }
2011
0f439c74
YZ
2012 /* Reschedule delayed caps release if we delayed anything */
2013 if (delayed)
a8599bd8
SW
2014 __cap_delay_requeue(mdsc, ci);
2015
be655596 2016 spin_unlock(&ci->i_ceph_lock);
a8599bd8 2017
cbd03635 2018 if (queue_invalidate)
3c6f6b79 2019 ceph_queue_invalidate(inode);
cbd03635 2020
cdc2ce05 2021 if (session)
a8599bd8
SW
2022 mutex_unlock(&session->s_mutex);
2023 if (took_snap_rwsem)
2024 up_read(&mdsc->snap_rwsem);
2025}
2026
a8599bd8
SW
2027/*
2028 * Try to flush dirty caps back to the auth mds.
2029 */
553adfd9 2030static int try_flush_caps(struct inode *inode, u64 *ptid)
a8599bd8 2031{
3d14c5d2 2032 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8 2033 struct ceph_inode_info *ci = ceph_inode(inode);
4fe59789 2034 struct ceph_mds_session *session = NULL;
89b52fe1 2035 int flushing = 0;
a2971c8c 2036 u64 flush_tid = 0, oldest_flush_tid = 0;
a8599bd8
SW
2037
2038retry:
be655596 2039 spin_lock(&ci->i_ceph_lock);
e9964c10 2040 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
6c2838fb 2041 spin_unlock(&ci->i_ceph_lock);
e9964c10
SW
2042 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2043 goto out;
2044 }
a8599bd8
SW
2045 if (ci->i_dirty_caps && ci->i_auth_cap) {
2046 struct ceph_cap *cap = ci->i_auth_cap;
2047 int used = __ceph_caps_used(ci);
2048 int want = __ceph_caps_wanted(ci);
2049 int delayed;
2050
4fe59789 2051 if (!session || session != cap->session) {
be655596 2052 spin_unlock(&ci->i_ceph_lock);
4fe59789
YZ
2053 if (session)
2054 mutex_unlock(&session->s_mutex);
a8599bd8
SW
2055 session = cap->session;
2056 mutex_lock(&session->s_mutex);
2057 goto retry;
2058 }
6c2838fb
JL
2059 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2060 spin_unlock(&ci->i_ceph_lock);
a8599bd8 2061 goto out;
6c2838fb 2062 }
a8599bd8 2063
c8799fc4
YZ
2064 flushing = __mark_caps_flushing(inode, session, true,
2065 &flush_tid, &oldest_flush_tid);
a8599bd8 2066
be655596 2067 /* __send_cap drops i_ceph_lock */
1e4ef0c6
JL
2068 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2069 used, want, (cap->issued | cap->implemented),
2070 flushing, flush_tid, oldest_flush_tid);
a8599bd8 2071
553adfd9
YZ
2072 if (delayed) {
2073 spin_lock(&ci->i_ceph_lock);
89b52fe1 2074 __cap_delay_requeue(mdsc, ci);
553adfd9
YZ
2075 spin_unlock(&ci->i_ceph_lock);
2076 }
2077 } else {
e4500b5e 2078 if (!list_empty(&ci->i_cap_flush_list)) {
553adfd9 2079 struct ceph_cap_flush *cf =
e4500b5e 2080 list_last_entry(&ci->i_cap_flush_list,
c8799fc4
YZ
2081 struct ceph_cap_flush, i_list);
2082 cf->wake = true;
553adfd9
YZ
2083 flush_tid = cf->tid;
2084 }
2085 flushing = ci->i_flushing_caps;
2086 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2087 }
2088out:
4fe59789 2089 if (session)
a8599bd8 2090 mutex_unlock(&session->s_mutex);
553adfd9
YZ
2091
2092 *ptid = flush_tid;
a8599bd8
SW
2093 return flushing;
2094}
2095
2096/*
2097 * Return true if we've flushed caps through the given flush_tid.
2098 */
553adfd9 2099static int caps_are_flushed(struct inode *inode, u64 flush_tid)
a8599bd8
SW
2100{
2101 struct ceph_inode_info *ci = ceph_inode(inode);
553adfd9 2102 int ret = 1;
a8599bd8 2103
be655596 2104 spin_lock(&ci->i_ceph_lock);
e4500b5e
YZ
2105 if (!list_empty(&ci->i_cap_flush_list)) {
2106 struct ceph_cap_flush * cf =
2107 list_first_entry(&ci->i_cap_flush_list,
2108 struct ceph_cap_flush, i_list);
553adfd9 2109 if (cf->tid <= flush_tid)
a8599bd8 2110 ret = 0;
89b52fe1 2111 }
be655596 2112 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2113 return ret;
2114}
2115
da819c81 2116/*
68cd5b4b 2117 * wait for any unsafe requests to complete.
da819c81 2118 */
68cd5b4b 2119static int unsafe_request_wait(struct inode *inode)
da819c81
YZ
2120{
2121 struct ceph_inode_info *ci = ceph_inode(inode);
68cd5b4b
YZ
2122 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2123 int ret, err = 0;
da819c81
YZ
2124
2125 spin_lock(&ci->i_unsafe_lock);
68cd5b4b
YZ
2126 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2127 req1 = list_last_entry(&ci->i_unsafe_dirops,
2128 struct ceph_mds_request,
2129 r_unsafe_dir_item);
2130 ceph_mdsc_get_request(req1);
2131 }
2132 if (!list_empty(&ci->i_unsafe_iops)) {
2133 req2 = list_last_entry(&ci->i_unsafe_iops,
2134 struct ceph_mds_request,
2135 r_unsafe_target_item);
2136 ceph_mdsc_get_request(req2);
2137 }
2138 spin_unlock(&ci->i_unsafe_lock);
da819c81 2139
4945a084 2140 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
68cd5b4b
YZ
2141 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2142 if (req1) {
2143 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2144 ceph_timeout_jiffies(req1->r_timeout));
da819c81 2145 if (ret)
68cd5b4b
YZ
2146 err = -EIO;
2147 ceph_mdsc_put_request(req1);
2148 }
2149 if (req2) {
2150 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2151 ceph_timeout_jiffies(req2->r_timeout));
2152 if (ret)
2153 err = -EIO;
2154 ceph_mdsc_put_request(req2);
2155 }
2156 return err;
da819c81
YZ
2157}
2158
02c24a82 2159int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
a8599bd8 2160{
7ea80859 2161 struct inode *inode = file->f_mapping->host;
a8599bd8 2162 struct ceph_inode_info *ci = ceph_inode(inode);
553adfd9 2163 u64 flush_tid;
a8599bd8
SW
2164 int ret;
2165 int dirty;
2166
2167 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
9a5530c6 2168
b74fceae 2169 ret = file_write_and_wait_range(file, start, end);
a8599bd8 2170 if (ret < 0)
da819c81
YZ
2171 goto out;
2172
2173 if (datasync)
2174 goto out;
2175
5955102c 2176 inode_lock(inode);
a8599bd8 2177
553adfd9 2178 dirty = try_flush_caps(inode, &flush_tid);
a8599bd8
SW
2179 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2180
68cd5b4b 2181 ret = unsafe_request_wait(inode);
da819c81 2182
a8599bd8
SW
2183 /*
2184 * only wait on non-file metadata writeback (the mds
2185 * can recover size and mtime, so we don't need to
2186 * wait for that)
2187 */
da819c81 2188 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
a8599bd8 2189 ret = wait_event_interruptible(ci->i_cap_wq,
da819c81 2190 caps_are_flushed(inode, flush_tid));
a8599bd8 2191 }
5955102c 2192 inode_unlock(inode);
da819c81
YZ
2193out:
2194 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
a8599bd8
SW
2195 return ret;
2196}
2197
2198/*
2199 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2200 * queue inode for flush but don't do so immediately, because we can
2201 * get by with fewer MDS messages if we wait for data writeback to
2202 * complete first.
2203 */
f1a3d572 2204int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
a8599bd8
SW
2205{
2206 struct ceph_inode_info *ci = ceph_inode(inode);
553adfd9 2207 u64 flush_tid;
a8599bd8
SW
2208 int err = 0;
2209 int dirty;
16515a6d 2210 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
a8599bd8
SW
2211
2212 dout("write_inode %p wait=%d\n", inode, wait);
2213 if (wait) {
553adfd9 2214 dirty = try_flush_caps(inode, &flush_tid);
a8599bd8
SW
2215 if (dirty)
2216 err = wait_event_interruptible(ci->i_cap_wq,
2217 caps_are_flushed(inode, flush_tid));
2218 } else {
640ef79d 2219 struct ceph_mds_client *mdsc =
3d14c5d2 2220 ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8 2221
be655596 2222 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2223 if (__ceph_caps_dirty(ci))
2224 __cap_delay_requeue_front(mdsc, ci);
be655596 2225 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2226 }
2227 return err;
2228}
2229
0e294387
YZ
2230static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2231 struct ceph_mds_session *session,
2232 struct ceph_inode_info *ci,
2233 u64 oldest_flush_tid)
2234 __releases(ci->i_ceph_lock)
2235 __acquires(ci->i_ceph_lock)
553adfd9
YZ
2236{
2237 struct inode *inode = &ci->vfs_inode;
2238 struct ceph_cap *cap;
2239 struct ceph_cap_flush *cf;
0e294387 2240 int ret;
553adfd9
YZ
2241 u64 first_tid = 0;
2242
e4500b5e
YZ
2243 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2244 if (cf->tid < first_tid)
2245 continue;
2246
553adfd9
YZ
2247 cap = ci->i_auth_cap;
2248 if (!(cap && cap->session == session)) {
0e294387
YZ
2249 pr_err("%p auth cap %p not mds%d ???\n",
2250 inode, cap, session->s_mds);
553adfd9
YZ
2251 break;
2252 }
2253
553adfd9
YZ
2254 first_tid = cf->tid + 1;
2255
0e294387
YZ
2256 if (cf->caps) {
2257 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2258 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2259 ci->i_ceph_flags |= CEPH_I_NODELAY;
2260 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1e4ef0c6 2261 false, __ceph_caps_used(ci),
0e294387
YZ
2262 __ceph_caps_wanted(ci),
2263 cap->issued | cap->implemented,
2264 cf->caps, cf->tid, oldest_flush_tid);
2265 if (ret) {
2266 pr_err("kick_flushing_caps: error sending "
2267 "cap flush, ino (%llx.%llx) "
2268 "tid %llu flushing %s\n",
2269 ceph_vinop(inode), cf->tid,
2270 ceph_cap_string(cf->caps));
2271 }
2272 } else {
2273 struct ceph_cap_snap *capsnap =
2274 container_of(cf, struct ceph_cap_snap,
2275 cap_flush);
2276 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2277 inode, capsnap, cf->tid,
2278 ceph_cap_string(capsnap->dirty));
2279
805692d0 2280 refcount_inc(&capsnap->nref);
0e294387
YZ
2281 spin_unlock(&ci->i_ceph_lock);
2282
2283 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2284 oldest_flush_tid);
2285 if (ret < 0) {
2286 pr_err("kick_flushing_caps: error sending "
2287 "cap flushsnap, ino (%llx.%llx) "
2288 "tid %llu follows %llu\n",
2289 ceph_vinop(inode), cf->tid,
2290 capsnap->follows);
2291 }
2292
2293 ceph_put_cap_snap(capsnap);
2294 }
e4500b5e
YZ
2295
2296 spin_lock(&ci->i_ceph_lock);
553adfd9 2297 }
553adfd9
YZ
2298}
2299
e548e9b9
YZ
2300void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2301 struct ceph_mds_session *session)
2302{
2303 struct ceph_inode_info *ci;
2304 struct ceph_cap *cap;
0e294387 2305 u64 oldest_flush_tid;
e548e9b9
YZ
2306
2307 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
0e294387
YZ
2308
2309 spin_lock(&mdsc->cap_dirty_lock);
2310 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2311 spin_unlock(&mdsc->cap_dirty_lock);
2312
e548e9b9
YZ
2313 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2314 spin_lock(&ci->i_ceph_lock);
2315 cap = ci->i_auth_cap;
2316 if (!(cap && cap->session == session)) {
2317 pr_err("%p auth cap %p not mds%d ???\n",
2318 &ci->vfs_inode, cap, session->s_mds);
2319 spin_unlock(&ci->i_ceph_lock);
2320 continue;
2321 }
2322
2323
2324 /*
2325 * if flushing caps were revoked, we re-send the cap flush
2326 * in client reconnect stage. This guarantees MDS * processes
2327 * the cap flush message before issuing the flushing caps to
2328 * other client.
2329 */
2330 if ((cap->issued & ci->i_flushing_caps) !=
2331 ci->i_flushing_caps) {
13c2b57d 2332 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
0e294387
YZ
2333 __kick_flushing_caps(mdsc, session, ci,
2334 oldest_flush_tid);
13c2b57d
YZ
2335 } else {
2336 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
e548e9b9
YZ
2337 }
2338
e548e9b9
YZ
2339 spin_unlock(&ci->i_ceph_lock);
2340 }
2341}
2342
a8599bd8
SW
2343void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2344 struct ceph_mds_session *session)
2345{
2346 struct ceph_inode_info *ci;
13c2b57d 2347 struct ceph_cap *cap;
0e294387 2348 u64 oldest_flush_tid;
a8599bd8
SW
2349
2350 dout("kick_flushing_caps mds%d\n", session->s_mds);
0e294387
YZ
2351
2352 spin_lock(&mdsc->cap_dirty_lock);
2353 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2354 spin_unlock(&mdsc->cap_dirty_lock);
2355
a8599bd8 2356 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
0e294387 2357 spin_lock(&ci->i_ceph_lock);
13c2b57d
YZ
2358 cap = ci->i_auth_cap;
2359 if (!(cap && cap->session == session)) {
2360 pr_err("%p auth cap %p not mds%d ???\n",
2361 &ci->vfs_inode, cap, session->s_mds);
2362 spin_unlock(&ci->i_ceph_lock);
2363 continue;
2364 }
2365 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2366 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2367 __kick_flushing_caps(mdsc, session, ci,
2368 oldest_flush_tid);
2369 }
0e294387 2370 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2371 }
2372}
2373
088b3f5e
SW
2374static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2375 struct ceph_mds_session *session,
2376 struct inode *inode)
0e294387 2377 __releases(ci->i_ceph_lock)
088b3f5e
SW
2378{
2379 struct ceph_inode_info *ci = ceph_inode(inode);
2380 struct ceph_cap *cap;
088b3f5e 2381
088b3f5e 2382 cap = ci->i_auth_cap;
8310b089
YZ
2383 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2384 ceph_cap_string(ci->i_flushing_caps));
005c4697 2385
0e294387
YZ
2386 if (!list_empty(&ci->i_cap_flush_list)) {
2387 u64 oldest_flush_tid;
005c4697
YZ
2388 spin_lock(&mdsc->cap_dirty_lock);
2389 list_move_tail(&ci->i_flushing_item,
2390 &cap->session->s_cap_flushing);
0e294387 2391 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
005c4697
YZ
2392 spin_unlock(&mdsc->cap_dirty_lock);
2393
13c2b57d 2394 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
0e294387 2395 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
553adfd9 2396 spin_unlock(&ci->i_ceph_lock);
088b3f5e 2397 } else {
be655596 2398 spin_unlock(&ci->i_ceph_lock);
088b3f5e
SW
2399 }
2400}
2401
a8599bd8
SW
2402
2403/*
2404 * Take references to capabilities we hold, so that we don't release
2405 * them to the MDS prematurely.
2406 *
be655596 2407 * Protected by i_ceph_lock.
a8599bd8 2408 */
5dda377c
YZ
2409static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2410 bool snap_rwsem_locked)
a8599bd8
SW
2411{
2412 if (got & CEPH_CAP_PIN)
2413 ci->i_pin_ref++;
2414 if (got & CEPH_CAP_FILE_RD)
2415 ci->i_rd_ref++;
2416 if (got & CEPH_CAP_FILE_CACHE)
2417 ci->i_rdcache_ref++;
5dda377c
YZ
2418 if (got & CEPH_CAP_FILE_WR) {
2419 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2420 BUG_ON(!snap_rwsem_locked);
2421 ci->i_head_snapc = ceph_get_snap_context(
2422 ci->i_snap_realm->cached_context);
2423 }
a8599bd8 2424 ci->i_wr_ref++;
5dda377c 2425 }
a8599bd8 2426 if (got & CEPH_CAP_FILE_BUFFER) {
d3d0720d 2427 if (ci->i_wb_ref == 0)
3772d26d 2428 ihold(&ci->vfs_inode);
d3d0720d
HC
2429 ci->i_wb_ref++;
2430 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2431 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
a8599bd8
SW
2432 }
2433}
2434
2435/*
2436 * Try to grab cap references. Specify those refs we @want, and the
2437 * minimal set we @need. Also include the larger offset we are writing
2438 * to (when applicable), and check against max_size here as well.
2439 * Note that caller is responsible for ensuring max_size increases are
2440 * requested from the MDS.
2441 */
2442static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
5dda377c 2443 loff_t endoff, bool nonblock, int *got, int *err)
a8599bd8
SW
2444{
2445 struct inode *inode = &ci->vfs_inode;
5dda377c 2446 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
a8599bd8 2447 int ret = 0;
c4d4a582 2448 int have, implemented;
195d3ce2 2449 int file_wanted;
5dda377c 2450 bool snap_rwsem_locked = false;
a8599bd8
SW
2451
2452 dout("get_cap_refs %p need %s want %s\n", inode,
2453 ceph_cap_string(need), ceph_cap_string(want));
c4d4a582 2454
5dda377c 2455again:
be655596 2456 spin_lock(&ci->i_ceph_lock);
a8599bd8 2457
195d3ce2
SW
2458 /* make sure file is actually open */
2459 file_wanted = __ceph_caps_file_wanted(ci);
77310320 2460 if ((file_wanted & need) != need) {
195d3ce2
SW
2461 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2462 ceph_cap_string(need), ceph_cap_string(file_wanted));
a8599bd8
SW
2463 *err = -EBADF;
2464 ret = 1;
3738daa6 2465 goto out_unlock;
a8599bd8
SW
2466 }
2467
37505d57
YZ
2468 /* finish pending truncate */
2469 while (ci->i_truncate_pending) {
2470 spin_unlock(&ci->i_ceph_lock);
5dda377c
YZ
2471 if (snap_rwsem_locked) {
2472 up_read(&mdsc->snap_rwsem);
2473 snap_rwsem_locked = false;
2474 }
b415bf4f 2475 __ceph_do_pending_vmtruncate(inode);
37505d57
YZ
2476 spin_lock(&ci->i_ceph_lock);
2477 }
2478
3871cbb9
YZ
2479 have = __ceph_caps_issued(ci, &implemented);
2480
2481 if (have & need & CEPH_CAP_FILE_WR) {
a8599bd8
SW
2482 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2483 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2484 inode, endoff, ci->i_max_size);
3871cbb9 2485 if (endoff > ci->i_requested_max_size) {
5dda377c 2486 *err = -EAGAIN;
a8599bd8
SW
2487 ret = 1;
2488 }
3738daa6 2489 goto out_unlock;
a8599bd8
SW
2490 }
2491 /*
2492 * If a sync write is in progress, we must wait, so that we
2493 * can get a final snapshot value for size+mtime.
2494 */
2495 if (__ceph_have_pending_cap_snap(ci)) {
2496 dout("get_cap_refs %p cap_snap_pending\n", inode);
3738daa6 2497 goto out_unlock;
a8599bd8
SW
2498 }
2499 }
a8599bd8 2500
a8599bd8
SW
2501 if ((have & need) == need) {
2502 /*
2503 * Look at (implemented & ~have & not) so that we keep waiting
2504 * on transition from wanted -> needed caps. This is needed
2505 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2506 * going before a prior buffered writeback happens.
2507 */
2508 int not = want & ~(have & need);
2509 int revoking = implemented & ~have;
2510 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2511 inode, ceph_cap_string(have), ceph_cap_string(not),
2512 ceph_cap_string(revoking));
2513 if ((revoking & not) == 0) {
5dda377c
YZ
2514 if (!snap_rwsem_locked &&
2515 !ci->i_head_snapc &&
2516 (need & CEPH_CAP_FILE_WR)) {
2517 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2518 /*
2519 * we can not call down_read() when
2520 * task isn't in TASK_RUNNING state
2521 */
2522 if (nonblock) {
2523 *err = -EAGAIN;
2524 ret = 1;
2525 goto out_unlock;
2526 }
2527
2528 spin_unlock(&ci->i_ceph_lock);
2529 down_read(&mdsc->snap_rwsem);
2530 snap_rwsem_locked = true;
2531 goto again;
2532 }
2533 snap_rwsem_locked = true;
2534 }
c4d4a582 2535 *got = need | (have & want);
f7f7e7a0
YZ
2536 if ((need & CEPH_CAP_FILE_RD) &&
2537 !(*got & CEPH_CAP_FILE_CACHE))
2538 ceph_disable_fscache_readpage(ci);
5dda377c 2539 __take_cap_refs(ci, *got, true);
a8599bd8
SW
2540 ret = 1;
2541 }
2542 } else {
03f4fcb0
YZ
2543 int session_readonly = false;
2544 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2545 struct ceph_mds_session *s = ci->i_auth_cap->session;
2546 spin_lock(&s->s_cap_lock);
2547 session_readonly = s->s_readonly;
2548 spin_unlock(&s->s_cap_lock);
2549 }
2550 if (session_readonly) {
2551 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2552 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2553 *err = -EROFS;
2554 ret = 1;
2555 goto out_unlock;
2556 }
2557
77310320
YZ
2558 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2559 int mds_wanted;
52953d55 2560 if (READ_ONCE(mdsc->fsc->mount_state) ==
77310320
YZ
2561 CEPH_MOUNT_SHUTDOWN) {
2562 dout("get_cap_refs %p forced umount\n", inode);
2563 *err = -EIO;
2564 ret = 1;
2565 goto out_unlock;
2566 }
c1944fed 2567 mds_wanted = __ceph_caps_mds_wanted(ci, false);
eb65b919 2568 if (need & ~(mds_wanted & need)) {
77310320
YZ
2569 dout("get_cap_refs %p caps were dropped"
2570 " (session killed?)\n", inode);
2571 *err = -ESTALE;
2572 ret = 1;
2573 goto out_unlock;
2574 }
eb65b919 2575 if (!(file_wanted & ~mds_wanted))
77310320 2576 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
48fec5d0
YZ
2577 }
2578
a8599bd8
SW
2579 dout("get_cap_refs %p have %s needed %s\n", inode,
2580 ceph_cap_string(have), ceph_cap_string(need));
2581 }
3738daa6 2582out_unlock:
be655596 2583 spin_unlock(&ci->i_ceph_lock);
5dda377c
YZ
2584 if (snap_rwsem_locked)
2585 up_read(&mdsc->snap_rwsem);
3738daa6 2586
a8599bd8 2587 dout("get_cap_refs %p ret %d got %s\n", inode,
c4d4a582 2588 ret, ceph_cap_string(*got));
a8599bd8
SW
2589 return ret;
2590}
2591
2592/*
2593 * Check the offset we are writing up to against our current
2594 * max_size. If necessary, tell the MDS we want to write to
2595 * a larger offset.
2596 */
2597static void check_max_size(struct inode *inode, loff_t endoff)
2598{
2599 struct ceph_inode_info *ci = ceph_inode(inode);
2600 int check = 0;
2601
2602 /* do we need to explicitly request a larger max_size? */
be655596 2603 spin_lock(&ci->i_ceph_lock);
3871cbb9 2604 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
a8599bd8
SW
2605 dout("write %p at large endoff %llu, req max_size\n",
2606 inode, endoff);
2607 ci->i_wanted_max_size = endoff;
a8599bd8 2608 }
3871cbb9
YZ
2609 /* duplicate ceph_check_caps()'s logic */
2610 if (ci->i_auth_cap &&
2611 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2612 ci->i_wanted_max_size > ci->i_max_size &&
2613 ci->i_wanted_max_size > ci->i_requested_max_size)
2614 check = 1;
be655596 2615 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2616 if (check)
2617 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2618}
2619
2b1ac852
YZ
2620int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
2621{
2622 int ret, err = 0;
2623
2624 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2625 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
2626 ret = ceph_pool_perm_check(ci, need);
2627 if (ret < 0)
2628 return ret;
2629
2630 ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
2631 if (ret) {
2632 if (err == -EAGAIN) {
2633 ret = 0;
2634 } else if (err < 0) {
2635 ret = err;
2636 }
2637 }
2638 return ret;
2639}
2640
a8599bd8
SW
2641/*
2642 * Wait for caps, and take cap references. If we can't get a WR cap
2643 * due to a small max_size, make sure we check_max_size (and possibly
2644 * ask the mds) so we don't get hung up indefinitely.
2645 */
3738daa6
YZ
2646int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2647 loff_t endoff, int *got, struct page **pinned_page)
a8599bd8 2648{
5dda377c 2649 int _got, ret, err = 0;
a8599bd8 2650
10183a69
YZ
2651 ret = ceph_pool_perm_check(ci, need);
2652 if (ret < 0)
2653 return ret;
2654
5dda377c
YZ
2655 while (true) {
2656 if (endoff > 0)
2657 check_max_size(&ci->vfs_inode, endoff);
c4d4a582 2658
5dda377c
YZ
2659 err = 0;
2660 _got = 0;
2661 ret = try_get_cap_refs(ci, need, want, endoff,
2662 false, &_got, &err);
2663 if (ret) {
2664 if (err == -EAGAIN)
2665 continue;
2666 if (err < 0)
77310320 2667 ret = err;
5dda377c 2668 } else {
5c341ee3
NB
2669 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2670 add_wait_queue(&ci->i_cap_wq, &wait);
2671
2672 while (!try_get_cap_refs(ci, need, want, endoff,
6e09d0fb
YZ
2673 true, &_got, &err)) {
2674 if (signal_pending(current)) {
2675 ret = -ERESTARTSYS;
2676 break;
2677 }
5c341ee3 2678 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
6e09d0fb 2679 }
5c341ee3
NB
2680
2681 remove_wait_queue(&ci->i_cap_wq, &wait);
2682
5dda377c
YZ
2683 if (err == -EAGAIN)
2684 continue;
2685 if (err < 0)
2686 ret = err;
77310320
YZ
2687 }
2688 if (ret < 0) {
2689 if (err == -ESTALE) {
2690 /* session was killed, try renew caps */
2691 ret = ceph_renew_caps(&ci->vfs_inode);
2692 if (ret == 0)
2693 continue;
2694 }
2695 return ret;
5dda377c 2696 }
c4d4a582 2697
5dda377c
YZ
2698 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2699 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2700 i_size_read(&ci->vfs_inode) > 0) {
2701 struct page *page =
2702 find_get_page(ci->vfs_inode.i_mapping, 0);
2703 if (page) {
2704 if (PageUptodate(page)) {
2705 *pinned_page = page;
2706 break;
2707 }
09cbfeaf 2708 put_page(page);
c4d4a582 2709 }
5dda377c
YZ
2710 /*
2711 * drop cap refs first because getattr while
2712 * holding * caps refs can cause deadlock.
2713 */
2714 ceph_put_cap_refs(ci, _got);
2715 _got = 0;
c4d4a582 2716
5dda377c
YZ
2717 /*
2718 * getattr request will bring inline data into
2719 * page cache
2720 */
2721 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2722 CEPH_STAT_CAP_INLINE_DATA,
2723 true);
2724 if (ret < 0)
2725 return ret;
2726 continue;
2727 }
2728 break;
c4d4a582 2729 }
5dda377c 2730
f7f7e7a0
YZ
2731 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2732 ceph_fscache_revalidate_cookie(ci);
2733
c4d4a582
YZ
2734 *got = _got;
2735 return 0;
a8599bd8
SW
2736}
2737
2738/*
2739 * Take cap refs. Caller must already know we hold at least one ref
2740 * on the caps in question or we don't know this is safe.
2741 */
2742void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2743{
be655596 2744 spin_lock(&ci->i_ceph_lock);
5dda377c 2745 __take_cap_refs(ci, caps, false);
be655596 2746 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2747}
2748
86056090
YZ
2749
2750/*
2751 * drop cap_snap that is not associated with any snapshot.
2752 * we don't need to send FLUSHSNAP message for it.
2753 */
70220ac8
YZ
2754static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2755 struct ceph_cap_snap *capsnap)
86056090
YZ
2756{
2757 if (!capsnap->need_flush &&
2758 !capsnap->writing && !capsnap->dirty_pages) {
86056090
YZ
2759 dout("dropping cap_snap %p follows %llu\n",
2760 capsnap, capsnap->follows);
0e294387 2761 BUG_ON(capsnap->cap_flush.tid > 0);
86056090 2762 ceph_put_snap_context(capsnap->context);
70220ac8
YZ
2763 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2764 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2765
86056090 2766 list_del(&capsnap->ci_item);
86056090
YZ
2767 ceph_put_cap_snap(capsnap);
2768 return 1;
2769 }
2770 return 0;
2771}
2772
a8599bd8
SW
2773/*
2774 * Release cap refs.
2775 *
2776 * If we released the last ref on any given cap, call ceph_check_caps
2777 * to release (or schedule a release).
2778 *
2779 * If we are releasing a WR cap (from a sync write), finalize any affected
2780 * cap_snap, and wake up any waiters.
2781 */
2782void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2783{
2784 struct inode *inode = &ci->vfs_inode;
2785 int last = 0, put = 0, flushsnaps = 0, wake = 0;
a8599bd8 2786
be655596 2787 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2788 if (had & CEPH_CAP_PIN)
2789 --ci->i_pin_ref;
2790 if (had & CEPH_CAP_FILE_RD)
2791 if (--ci->i_rd_ref == 0)
2792 last++;
2793 if (had & CEPH_CAP_FILE_CACHE)
2794 if (--ci->i_rdcache_ref == 0)
2795 last++;
2796 if (had & CEPH_CAP_FILE_BUFFER) {
d3d0720d 2797 if (--ci->i_wb_ref == 0) {
a8599bd8
SW
2798 last++;
2799 put++;
2800 }
d3d0720d
HC
2801 dout("put_cap_refs %p wb %d -> %d (?)\n",
2802 inode, ci->i_wb_ref+1, ci->i_wb_ref);
a8599bd8
SW
2803 }
2804 if (had & CEPH_CAP_FILE_WR)
2805 if (--ci->i_wr_ref == 0) {
2806 last++;
86056090
YZ
2807 if (__ceph_have_pending_cap_snap(ci)) {
2808 struct ceph_cap_snap *capsnap =
2809 list_last_entry(&ci->i_cap_snaps,
2810 struct ceph_cap_snap,
2811 ci_item);
2812 capsnap->writing = 0;
70220ac8 2813 if (ceph_try_drop_cap_snap(ci, capsnap))
86056090
YZ
2814 put++;
2815 else if (__ceph_finish_cap_snap(ci, capsnap))
2816 flushsnaps = 1;
2817 wake = 1;
a8599bd8 2818 }
5dda377c
YZ
2819 if (ci->i_wrbuffer_ref_head == 0 &&
2820 ci->i_dirty_caps == 0 &&
2821 ci->i_flushing_caps == 0) {
2822 BUG_ON(!ci->i_head_snapc);
2823 ceph_put_snap_context(ci->i_head_snapc);
2824 ci->i_head_snapc = NULL;
2825 }
db40cc17
YZ
2826 /* see comment in __ceph_remove_cap() */
2827 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2828 drop_inode_snap_realm(ci);
a8599bd8 2829 }
be655596 2830 spin_unlock(&ci->i_ceph_lock);
a8599bd8 2831
819ccbfa
SW
2832 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2833 last ? " last" : "", put ? " put" : "");
a8599bd8
SW
2834
2835 if (last && !flushsnaps)
2836 ceph_check_caps(ci, 0, NULL);
2837 else if (flushsnaps)
ed9b430c 2838 ceph_flush_snaps(ci, NULL);
a8599bd8 2839 if (wake)
03066f23 2840 wake_up_all(&ci->i_cap_wq);
86056090 2841 while (put-- > 0)
a8599bd8
SW
2842 iput(inode);
2843}
2844
2845/*
2846 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2847 * context. Adjust per-snap dirty page accounting as appropriate.
2848 * Once all dirty data for a cap_snap is flushed, flush snapped file
2849 * metadata back to the MDS. If we dropped the last ref, call
2850 * ceph_check_caps.
2851 */
2852void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2853 struct ceph_snap_context *snapc)
2854{
2855 struct inode *inode = &ci->vfs_inode;
a8599bd8 2856 struct ceph_cap_snap *capsnap = NULL;
70220ac8
YZ
2857 int put = 0;
2858 bool last = false;
2859 bool found = false;
2860 bool flush_snaps = false;
2861 bool complete_capsnap = false;
a8599bd8 2862
be655596 2863 spin_lock(&ci->i_ceph_lock);
a8599bd8 2864 ci->i_wrbuffer_ref -= nr;
70220ac8
YZ
2865 if (ci->i_wrbuffer_ref == 0) {
2866 last = true;
2867 put++;
2868 }
a8599bd8
SW
2869
2870 if (ci->i_head_snapc == snapc) {
2871 ci->i_wrbuffer_ref_head -= nr;
7d8cb26d 2872 if (ci->i_wrbuffer_ref_head == 0 &&
5dda377c
YZ
2873 ci->i_wr_ref == 0 &&
2874 ci->i_dirty_caps == 0 &&
2875 ci->i_flushing_caps == 0) {
7d8cb26d 2876 BUG_ON(!ci->i_head_snapc);
a8599bd8
SW
2877 ceph_put_snap_context(ci->i_head_snapc);
2878 ci->i_head_snapc = NULL;
2879 }
2880 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2881 inode,
2882 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2883 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2884 last ? " LAST" : "");
2885 } else {
2886 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2887 if (capsnap->context == snapc) {
70220ac8 2888 found = true;
a8599bd8
SW
2889 break;
2890 }
2891 }
2892 BUG_ON(!found);
819ccbfa
SW
2893 capsnap->dirty_pages -= nr;
2894 if (capsnap->dirty_pages == 0) {
70220ac8
YZ
2895 complete_capsnap = true;
2896 if (!capsnap->writing) {
2897 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2898 put++;
2899 } else {
2900 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2901 flush_snaps = true;
2902 }
2903 }
819ccbfa 2904 }
a8599bd8 2905 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
86056090 2906 " snap %lld %d/%d -> %d/%d %s%s\n",
a8599bd8
SW
2907 inode, capsnap, capsnap->context->seq,
2908 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2909 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2910 last ? " (wrbuffer last)" : "",
86056090 2911 complete_capsnap ? " (complete capsnap)" : "");
a8599bd8
SW
2912 }
2913
be655596 2914 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2915
2916 if (last) {
2917 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
70220ac8 2918 } else if (flush_snaps) {
ed9b430c 2919 ceph_flush_snaps(ci, NULL);
a8599bd8 2920 }
70220ac8
YZ
2921 if (complete_capsnap)
2922 wake_up_all(&ci->i_cap_wq);
2923 while (put-- > 0)
819ccbfa 2924 iput(inode);
a8599bd8
SW
2925}
2926
ca20c991
YZ
2927/*
2928 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2929 */
2930static void invalidate_aliases(struct inode *inode)
2931{
2932 struct dentry *dn, *prev = NULL;
2933
2934 dout("invalidate_aliases inode %p\n", inode);
2935 d_prune_aliases(inode);
2936 /*
2937 * For non-directory inode, d_find_alias() only returns
fc12c80a
BF
2938 * hashed dentry. After calling d_invalidate(), the
2939 * dentry becomes unhashed.
ca20c991 2940 *
a8d436f0 2941 * For directory inode, d_find_alias() can return
fc12c80a 2942 * unhashed dentry. But directory inode should have
ca20c991
YZ
2943 * one alias at most.
2944 */
2945 while ((dn = d_find_alias(inode))) {
2946 if (dn == prev) {
2947 dput(dn);
2948 break;
2949 }
a8d436f0 2950 d_invalidate(dn);
ca20c991
YZ
2951 if (prev)
2952 dput(prev);
2953 prev = dn;
2954 }
2955 if (prev)
2956 dput(prev);
2957}
2958
a8599bd8
SW
2959/*
2960 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2961 * actually be a revocation if it specifies a smaller cap set.)
2962 *
be655596 2963 * caller holds s_mutex and i_ceph_lock, we drop both.
a8599bd8 2964 */
2cd698be
YZ
2965static void handle_cap_grant(struct ceph_mds_client *mdsc,
2966 struct inode *inode, struct ceph_mds_caps *grant,
779fe0fb
YZ
2967 struct ceph_string **pns, u64 inline_version,
2968 void *inline_data, u32 inline_len,
2cd698be 2969 struct ceph_buffer *xattr_buf,
15637c8b 2970 struct ceph_mds_session *session,
779fe0fb 2971 struct ceph_cap *cap, int issued)
2cd698be 2972 __releases(ci->i_ceph_lock)
982d6011 2973 __releases(mdsc->snap_rwsem)
a8599bd8
SW
2974{
2975 struct ceph_inode_info *ci = ceph_inode(inode);
2976 int mds = session->s_mds;
2f56f56a 2977 int seq = le32_to_cpu(grant->seq);
a8599bd8 2978 int newcaps = le32_to_cpu(grant->caps);
2cd698be 2979 int used, wanted, dirty;
a8599bd8
SW
2980 u64 size = le64_to_cpu(grant->size);
2981 u64 max_size = le64_to_cpu(grant->max_size);
2982 struct timespec mtime, atime, ctime;
15637c8b 2983 int check_caps = 0;
ab6c2c3e
FF
2984 bool wake = false;
2985 bool writeback = false;
2986 bool queue_trunc = false;
2987 bool queue_invalidate = false;
ab6c2c3e 2988 bool deleted_inode = false;
31c542a1 2989 bool fill_inline = false;
a8599bd8 2990
2f56f56a
SW
2991 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2992 inode, cap, mds, seq, ceph_cap_string(newcaps));
a8599bd8
SW
2993 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2994 inode->i_size);
2995
11df2dfb
YZ
2996
2997 /*
2998 * auth mds of the inode changed. we received the cap export message,
2999 * but still haven't received the cap import message. handle_cap_export
3000 * updated the new auth MDS' cap.
3001 *
3002 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3003 * that was sent before the cap import message. So don't remove caps.
3004 */
3005 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3006 WARN_ON(cap != ci->i_auth_cap);
3007 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3008 seq = cap->seq;
3009 newcaps |= cap->issued;
3010 }
3011
a8599bd8
SW
3012 /*
3013 * If CACHE is being revoked, and we have no dirty buffers,
3014 * try to invalidate (once). (If there are dirty buffers, we
3015 * will invalidate _after_ writeback.)
3016 */
fdd4e158
YZ
3017 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3018 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3b454c49 3019 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
9abd4db7 3020 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
e9075743 3021 if (try_nonblocking_invalidate(inode)) {
a8599bd8
SW
3022 /* there were locked pages.. invalidate later
3023 in a separate thread. */
3024 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
ab6c2c3e 3025 queue_invalidate = true;
a8599bd8
SW
3026 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3027 }
a8599bd8 3028 }
a8599bd8
SW
3029 }
3030
3031 /* side effects now are allowed */
685f9a5d 3032 cap->cap_gen = session->s_cap_gen;
11df2dfb 3033 cap->seq = seq;
a8599bd8
SW
3034
3035 __check_cap_issue(ci, cap, newcaps);
3036
f98a128a
YZ
3037 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3038 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
a8599bd8 3039 inode->i_mode = le32_to_cpu(grant->mode);
05cb11c1
EB
3040 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3041 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
a8599bd8 3042 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
bd2bae6a
EB
3043 from_kuid(&init_user_ns, inode->i_uid),
3044 from_kgid(&init_user_ns, inode->i_gid));
a8599bd8
SW
3045 }
3046
f98a128a
YZ
3047 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3048 (issued & CEPH_CAP_LINK_EXCL) == 0) {
bfe86848 3049 set_nlink(inode, le32_to_cpu(grant->nlink));
ca20c991
YZ
3050 if (inode->i_nlink == 0 &&
3051 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
ab6c2c3e 3052 deleted_inode = true;
ca20c991 3053 }
a8599bd8
SW
3054
3055 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
3056 int len = le32_to_cpu(grant->xattr_len);
3057 u64 version = le64_to_cpu(grant->xattr_version);
3058
3059 if (version > ci->i_xattrs.version) {
3060 dout(" got new xattrs v%llu on %p len %d\n",
3061 version, inode, len);
3062 if (ci->i_xattrs.blob)
3063 ceph_buffer_put(ci->i_xattrs.blob);
3064 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3065 ci->i_xattrs.version = version;
7221fe4c 3066 ceph_forget_all_cached_acls(inode);
a8599bd8
SW
3067 }
3068 }
3069
f98a128a
YZ
3070 if (newcaps & CEPH_CAP_ANY_RD) {
3071 /* ctime/mtime/atime? */
3072 ceph_decode_timespec(&mtime, &grant->mtime);
3073 ceph_decode_timespec(&atime, &grant->atime);
3074 ceph_decode_timespec(&ctime, &grant->ctime);
3075 ceph_fill_file_time(inode, issued,
3076 le32_to_cpu(grant->time_warp_seq),
3077 &ctime, &mtime, &atime);
3078 }
3079
3080 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3081 /* file layout may have changed */
7627151e 3082 s64 old_pool = ci->i_layout.pool_id;
779fe0fb
YZ
3083 struct ceph_string *old_ns;
3084
7627151e 3085 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
779fe0fb
YZ
3086 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3087 lockdep_is_held(&ci->i_ceph_lock));
3088 rcu_assign_pointer(ci->i_layout.pool_ns, *pns);
3089
3090 if (ci->i_layout.pool_id != old_pool || *pns != old_ns)
7627151e 3091 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
5ea5c5e0 3092
779fe0fb
YZ
3093 *pns = old_ns;
3094
f98a128a
YZ
3095 /* size/truncate_seq? */
3096 queue_trunc = ceph_fill_file_size(inode, issued,
3097 le32_to_cpu(grant->truncate_seq),
3098 le64_to_cpu(grant->truncate_size),
3099 size);
84eea8c7
YZ
3100 }
3101
3102 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3103 if (max_size != ci->i_max_size) {
f98a128a
YZ
3104 dout("max_size %lld -> %llu\n",
3105 ci->i_max_size, max_size);
3106 ci->i_max_size = max_size;
3107 if (max_size >= ci->i_wanted_max_size) {
3108 ci->i_wanted_max_size = 0; /* reset */
3109 ci->i_requested_max_size = 0;
3110 }
ab6c2c3e 3111 wake = true;
84eea8c7
YZ
3112 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3113 ci->i_wanted_max_size > ci->i_requested_max_size) {
3114 /* CEPH_CAP_OP_IMPORT */
3115 wake = true;
a8599bd8 3116 }
a8599bd8
SW
3117 }
3118
3119 /* check cap bits */
3120 wanted = __ceph_caps_wanted(ci);
3121 used = __ceph_caps_used(ci);
3122 dirty = __ceph_caps_dirty(ci);
3123 dout(" my wanted = %s, used = %s, dirty %s\n",
3124 ceph_cap_string(wanted),
3125 ceph_cap_string(used),
3126 ceph_cap_string(dirty));
3127 if (wanted != le32_to_cpu(grant->wanted)) {
3128 dout("mds wanted %s -> %s\n",
3129 ceph_cap_string(le32_to_cpu(grant->wanted)),
3130 ceph_cap_string(wanted));
390306c3
YZ
3131 /* imported cap may not have correct mds_wanted */
3132 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
3133 check_caps = 1;
a8599bd8
SW
3134 }
3135
a8599bd8
SW
3136 /* revocation, grant, or no-op? */
3137 if (cap->issued & ~newcaps) {
3b454c49
SW
3138 int revoking = cap->issued & ~newcaps;
3139
3140 dout("revocation: %s -> %s (revoking %s)\n",
3141 ceph_cap_string(cap->issued),
3142 ceph_cap_string(newcaps),
3143 ceph_cap_string(revoking));
0eb6cd49 3144 if (revoking & used & CEPH_CAP_FILE_BUFFER)
ab6c2c3e 3145 writeback = true; /* initiate writeback; will delay ack */
3b454c49
SW
3146 else if (revoking == CEPH_CAP_FILE_CACHE &&
3147 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3148 queue_invalidate)
3149 ; /* do nothing yet, invalidation will be queued */
3150 else if (cap == ci->i_auth_cap)
3151 check_caps = 1; /* check auth cap only */
3152 else
3153 check_caps = 2; /* check all caps */
a8599bd8 3154 cap->issued = newcaps;
978097c9 3155 cap->implemented |= newcaps;
a8599bd8
SW
3156 } else if (cap->issued == newcaps) {
3157 dout("caps unchanged: %s -> %s\n",
3158 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3159 } else {
3160 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3161 ceph_cap_string(newcaps));
6ee6b953
YZ
3162 /* non-auth MDS is revoking the newly grant caps ? */
3163 if (cap == ci->i_auth_cap &&
3164 __ceph_caps_revoking_other(ci, cap, newcaps))
3165 check_caps = 2;
3166
a8599bd8
SW
3167 cap->issued = newcaps;
3168 cap->implemented |= newcaps; /* add bits only, to
3169 * avoid stepping on a
3170 * pending revocation */
ab6c2c3e 3171 wake = true;
a8599bd8 3172 }
978097c9 3173 BUG_ON(cap->issued & ~cap->implemented);
a8599bd8 3174
31c542a1
YZ
3175 if (inline_version > 0 && inline_version >= ci->i_inline_version) {
3176 ci->i_inline_version = inline_version;
3177 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3178 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3179 fill_inline = true;
3180 }
3181
2cd698be 3182 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
2cd698be 3183 if (newcaps & ~issued)
ab6c2c3e 3184 wake = true;
0e294387
YZ
3185 kick_flushing_inode_caps(mdsc, session, inode);
3186 up_read(&mdsc->snap_rwsem);
3187 } else {
3188 spin_unlock(&ci->i_ceph_lock);
2cd698be
YZ
3189 }
3190
31c542a1
YZ
3191 if (fill_inline)
3192 ceph_fill_inline_data(inode, NULL, inline_data, inline_len);
3193
14649758 3194 if (queue_trunc)
c6bcda6f 3195 ceph_queue_vmtruncate(inode);
c6bcda6f 3196
3c6f6b79 3197 if (writeback)
a8599bd8
SW
3198 /*
3199 * queue inode for writeback: we can't actually call
3200 * filemap_write_and_wait, etc. from message handler
3201 * context.
3202 */
3c6f6b79
SW
3203 ceph_queue_writeback(inode);
3204 if (queue_invalidate)
3205 ceph_queue_invalidate(inode);
ca20c991
YZ
3206 if (deleted_inode)
3207 invalidate_aliases(inode);
a8599bd8 3208 if (wake)
03066f23 3209 wake_up_all(&ci->i_cap_wq);
15637c8b
SW
3210
3211 if (check_caps == 1)
3212 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3213 session);
3214 else if (check_caps == 2)
3215 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3216 else
3217 mutex_unlock(&session->s_mutex);
a8599bd8
SW
3218}
3219
3220/*
3221 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3222 * MDS has been safely committed.
3223 */
6df058c0 3224static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
a8599bd8
SW
3225 struct ceph_mds_caps *m,
3226 struct ceph_mds_session *session,
3227 struct ceph_cap *cap)
be655596 3228 __releases(ci->i_ceph_lock)
a8599bd8
SW
3229{
3230 struct ceph_inode_info *ci = ceph_inode(inode);
3d14c5d2 3231 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
e4500b5e 3232 struct ceph_cap_flush *cf, *tmp_cf;
553adfd9 3233 LIST_HEAD(to_remove);
a8599bd8
SW
3234 unsigned seq = le32_to_cpu(m->seq);
3235 int dirty = le32_to_cpu(m->dirty);
3236 int cleaned = 0;
c8799fc4 3237 bool drop = false;
7271efa7
TM
3238 bool wake_ci = false;
3239 bool wake_mdsc = false;
a8599bd8 3240
e4500b5e 3241 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
553adfd9
YZ
3242 if (cf->tid == flush_tid)
3243 cleaned = cf->caps;
0e294387
YZ
3244 if (cf->caps == 0) /* capsnap */
3245 continue;
553adfd9 3246 if (cf->tid <= flush_tid) {
c8799fc4
YZ
3247 if (__finish_cap_flush(NULL, ci, cf))
3248 wake_ci = true;
e4500b5e 3249 list_add_tail(&cf->i_list, &to_remove);
553adfd9
YZ
3250 } else {
3251 cleaned &= ~cf->caps;
3252 if (!cleaned)
3253 break;
3254 }
3255 }
a8599bd8
SW
3256
3257 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3258 " flushing %s -> %s\n",
3259 inode, session->s_mds, seq, ceph_cap_string(dirty),
3260 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3261 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3262
8310b089 3263 if (list_empty(&to_remove) && !cleaned)
a8599bd8
SW
3264 goto out;
3265
a8599bd8 3266 ci->i_flushing_caps &= ~cleaned;
a8599bd8
SW
3267
3268 spin_lock(&mdsc->cap_dirty_lock);
8310b089 3269
c8799fc4
YZ
3270 list_for_each_entry(cf, &to_remove, i_list) {
3271 if (__finish_cap_flush(mdsc, NULL, cf))
3272 wake_mdsc = true;
8310b089
YZ
3273 }
3274
a8599bd8 3275 if (ci->i_flushing_caps == 0) {
0e294387
YZ
3276 if (list_empty(&ci->i_cap_flush_list)) {
3277 list_del_init(&ci->i_flushing_item);
3278 if (!list_empty(&session->s_cap_flushing)) {
3279 dout(" mds%d still flushing cap on %p\n",
3280 session->s_mds,
3281 &list_first_entry(&session->s_cap_flushing,
3282 struct ceph_inode_info,
3283 i_flushing_item)->vfs_inode);
3284 }
3285 }
a8599bd8 3286 mdsc->num_cap_flushing--;
a8599bd8 3287 dout(" inode %p now !flushing\n", inode);
afcdaea3
SW
3288
3289 if (ci->i_dirty_caps == 0) {
3290 dout(" inode %p now clean\n", inode);
3291 BUG_ON(!list_empty(&ci->i_dirty_item));
c8799fc4 3292 drop = true;
5dda377c
YZ
3293 if (ci->i_wr_ref == 0 &&
3294 ci->i_wrbuffer_ref_head == 0) {
7d8cb26d
SW
3295 BUG_ON(!ci->i_head_snapc);
3296 ceph_put_snap_context(ci->i_head_snapc);
3297 ci->i_head_snapc = NULL;
3298 }
76e3b390
SW
3299 } else {
3300 BUG_ON(list_empty(&ci->i_dirty_item));
afcdaea3 3301 }
a8599bd8
SW
3302 }
3303 spin_unlock(&mdsc->cap_dirty_lock);
a8599bd8
SW
3304
3305out:
be655596 3306 spin_unlock(&ci->i_ceph_lock);
553adfd9
YZ
3307
3308 while (!list_empty(&to_remove)) {
3309 cf = list_first_entry(&to_remove,
e4500b5e
YZ
3310 struct ceph_cap_flush, i_list);
3311 list_del(&cf->i_list);
f66fd9f0 3312 ceph_free_cap_flush(cf);
553adfd9 3313 }
c8799fc4
YZ
3314
3315 if (wake_ci)
3316 wake_up_all(&ci->i_cap_wq);
3317 if (wake_mdsc)
3318 wake_up_all(&mdsc->cap_flushing_wq);
afcdaea3 3319 if (drop)
a8599bd8
SW
3320 iput(inode);
3321}
3322
3323/*
3324 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3325 * throw away our cap_snap.
3326 *
3327 * Caller hold s_mutex.
3328 */
6df058c0 3329static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
a8599bd8
SW
3330 struct ceph_mds_caps *m,
3331 struct ceph_mds_session *session)
3332{
3333 struct ceph_inode_info *ci = ceph_inode(inode);
affbc19a 3334 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8 3335 u64 follows = le64_to_cpu(m->snap_follows);
a8599bd8 3336 struct ceph_cap_snap *capsnap;
c8799fc4
YZ
3337 bool flushed = false;
3338 bool wake_ci = false;
3339 bool wake_mdsc = false;
a8599bd8
SW
3340
3341 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3342 inode, ci, session->s_mds, follows);
3343
be655596 3344 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
3345 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3346 if (capsnap->follows == follows) {
0e294387 3347 if (capsnap->cap_flush.tid != flush_tid) {
a8599bd8
SW
3348 dout(" cap_snap %p follows %lld tid %lld !="
3349 " %lld\n", capsnap, follows,
0e294387 3350 flush_tid, capsnap->cap_flush.tid);
a8599bd8
SW
3351 break;
3352 }
c8799fc4 3353 flushed = true;
a8599bd8
SW
3354 break;
3355 } else {
3356 dout(" skipping cap_snap %p follows %lld\n",
3357 capsnap, capsnap->follows);
3358 }
3359 }
0e294387 3360 if (flushed) {
0e294387
YZ
3361 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3362 dout(" removing %p cap_snap %p follows %lld\n",
3363 inode, capsnap, follows);
3364 list_del(&capsnap->ci_item);
c8799fc4
YZ
3365 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3366 wake_ci = true;
0e294387
YZ
3367
3368 spin_lock(&mdsc->cap_dirty_lock);
3369
3370 if (list_empty(&ci->i_cap_flush_list))
3371 list_del_init(&ci->i_flushing_item);
3372
c8799fc4
YZ
3373 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3374 wake_mdsc = true;
0e294387
YZ
3375
3376 spin_unlock(&mdsc->cap_dirty_lock);
0e294387 3377 }
be655596 3378 spin_unlock(&ci->i_ceph_lock);
0e294387
YZ
3379 if (flushed) {
3380 ceph_put_snap_context(capsnap->context);
3381 ceph_put_cap_snap(capsnap);
c8799fc4
YZ
3382 if (wake_ci)
3383 wake_up_all(&ci->i_cap_wq);
3384 if (wake_mdsc)
3385 wake_up_all(&mdsc->cap_flushing_wq);
a8599bd8 3386 iput(inode);
0e294387 3387 }
a8599bd8
SW
3388}
3389
3390/*
3391 * Handle TRUNC from MDS, indicating file truncation.
3392 *
3393 * caller hold s_mutex.
3394 */
3395static void handle_cap_trunc(struct inode *inode,
3396 struct ceph_mds_caps *trunc,
3397 struct ceph_mds_session *session)
be655596 3398 __releases(ci->i_ceph_lock)
a8599bd8
SW
3399{
3400 struct ceph_inode_info *ci = ceph_inode(inode);
3401 int mds = session->s_mds;
3402 int seq = le32_to_cpu(trunc->seq);
3403 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3404 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3405 u64 size = le64_to_cpu(trunc->size);
3406 int implemented = 0;
3407 int dirty = __ceph_caps_dirty(ci);
3408 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3409 int queue_trunc = 0;
3410
3411 issued |= implemented | dirty;
3412
3413 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3414 inode, mds, seq, truncate_size, truncate_seq);
3415 queue_trunc = ceph_fill_file_size(inode, issued,
3416 truncate_seq, truncate_size, size);
be655596 3417 spin_unlock(&ci->i_ceph_lock);
a8599bd8 3418
14649758 3419 if (queue_trunc)
3c6f6b79 3420 ceph_queue_vmtruncate(inode);
a8599bd8
SW
3421}
3422
3423/*
3424 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3425 * different one. If we are the most recent migration we've seen (as
3426 * indicated by mseq), make note of the migrating cap bits for the
3427 * duration (until we see the corresponding IMPORT).
3428 *
3429 * caller holds s_mutex
3430 */
3431static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
11df2dfb
YZ
3432 struct ceph_mds_cap_peer *ph,
3433 struct ceph_mds_session *session)
a8599bd8 3434{
db354052 3435 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
11df2dfb 3436 struct ceph_mds_session *tsession = NULL;
d9df2783 3437 struct ceph_cap *cap, *tcap, *new_cap = NULL;
a8599bd8 3438 struct ceph_inode_info *ci = ceph_inode(inode);
11df2dfb 3439 u64 t_cap_id;
a8599bd8 3440 unsigned mseq = le32_to_cpu(ex->migrate_seq);
11df2dfb
YZ
3441 unsigned t_seq, t_mseq;
3442 int target, issued;
3443 int mds = session->s_mds;
a8599bd8 3444
11df2dfb
YZ
3445 if (ph) {
3446 t_cap_id = le64_to_cpu(ph->cap_id);
3447 t_seq = le32_to_cpu(ph->seq);
3448 t_mseq = le32_to_cpu(ph->mseq);
3449 target = le32_to_cpu(ph->mds);
3450 } else {
3451 t_cap_id = t_seq = t_mseq = 0;
3452 target = -1;
3453 }
a8599bd8 3454
11df2dfb
YZ
3455 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3456 inode, ci, mds, mseq, target);
3457retry:
be655596 3458 spin_lock(&ci->i_ceph_lock);
11df2dfb 3459 cap = __get_cap_for_mds(ci, mds);
ca665e02 3460 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
11df2dfb 3461 goto out_unlock;
a8599bd8 3462
11df2dfb
YZ
3463 if (target < 0) {
3464 __ceph_remove_cap(cap, false);
77310320
YZ
3465 if (!ci->i_auth_cap)
3466 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
11df2dfb 3467 goto out_unlock;
a8599bd8
SW
3468 }
3469
11df2dfb
YZ
3470 /*
3471 * now we know we haven't received the cap import message yet
3472 * because the exported cap still exist.
3473 */
db354052 3474
11df2dfb 3475 issued = cap->issued;
d84b37f9
YZ
3476 if (issued != cap->implemented)
3477 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3478 "ino (%llx.%llx) mds%d seq %d mseq %d "
3479 "issued %s implemented %s\n",
3480 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3481 ceph_cap_string(issued),
3482 ceph_cap_string(cap->implemented));
3483
11df2dfb
YZ
3484
3485 tcap = __get_cap_for_mds(ci, target);
3486 if (tcap) {
3487 /* already have caps from the target */
fa0aa3b8 3488 if (tcap->cap_id == t_cap_id &&
11df2dfb
YZ
3489 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3490 dout(" updating import cap %p mds%d\n", tcap, target);
3491 tcap->cap_id = t_cap_id;
3492 tcap->seq = t_seq - 1;
3493 tcap->issue_seq = t_seq - 1;
3494 tcap->mseq = t_mseq;
3495 tcap->issued |= issued;
3496 tcap->implemented |= issued;
3497 if (cap == ci->i_auth_cap)
3498 ci->i_auth_cap = tcap;
00f06cba 3499
0e294387
YZ
3500 if (!list_empty(&ci->i_cap_flush_list) &&
3501 ci->i_auth_cap == tcap) {
11df2dfb
YZ
3502 spin_lock(&mdsc->cap_dirty_lock);
3503 list_move_tail(&ci->i_flushing_item,
3504 &tcap->session->s_cap_flushing);
3505 spin_unlock(&mdsc->cap_dirty_lock);
db354052 3506 }
a8599bd8 3507 }
a096b09a 3508 __ceph_remove_cap(cap, false);
11df2dfb 3509 goto out_unlock;
d9df2783 3510 } else if (tsession) {
11df2dfb 3511 /* add placeholder for the export tagert */
d9df2783 3512 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
00f06cba 3513 tcap = new_cap;
11df2dfb 3514 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
d9df2783
YZ
3515 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3516
00f06cba
YZ
3517 if (!list_empty(&ci->i_cap_flush_list) &&
3518 ci->i_auth_cap == tcap) {
3519 spin_lock(&mdsc->cap_dirty_lock);
3520 list_move_tail(&ci->i_flushing_item,
3521 &tcap->session->s_cap_flushing);
3522 spin_unlock(&mdsc->cap_dirty_lock);
3523 }
3524
d9df2783
YZ
3525 __ceph_remove_cap(cap, false);
3526 goto out_unlock;
a8599bd8
SW
3527 }
3528
be655596 3529 spin_unlock(&ci->i_ceph_lock);
11df2dfb
YZ
3530 mutex_unlock(&session->s_mutex);
3531
3532 /* open target session */
3533 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3534 if (!IS_ERR(tsession)) {
3535 if (mds > target) {
3536 mutex_lock(&session->s_mutex);
3537 mutex_lock_nested(&tsession->s_mutex,
3538 SINGLE_DEPTH_NESTING);
3539 } else {
3540 mutex_lock(&tsession->s_mutex);
3541 mutex_lock_nested(&session->s_mutex,
3542 SINGLE_DEPTH_NESTING);
3543 }
d9df2783 3544 new_cap = ceph_get_cap(mdsc, NULL);
11df2dfb
YZ
3545 } else {
3546 WARN_ON(1);
3547 tsession = NULL;
3548 target = -1;
3549 }
3550 goto retry;
3551
3552out_unlock:
3553 spin_unlock(&ci->i_ceph_lock);
3554 mutex_unlock(&session->s_mutex);
3555 if (tsession) {
3556 mutex_unlock(&tsession->s_mutex);
3557 ceph_put_mds_session(tsession);
3558 }
d9df2783
YZ
3559 if (new_cap)
3560 ceph_put_cap(mdsc, new_cap);
a8599bd8
SW
3561}
3562
3563/*
2cd698be 3564 * Handle cap IMPORT.
a8599bd8 3565 *
2cd698be 3566 * caller holds s_mutex. acquires i_ceph_lock
a8599bd8
SW
3567 */
3568static void handle_cap_import(struct ceph_mds_client *mdsc,
3569 struct inode *inode, struct ceph_mds_caps *im,
4ee6a914 3570 struct ceph_mds_cap_peer *ph,
a8599bd8 3571 struct ceph_mds_session *session,
2cd698be
YZ
3572 struct ceph_cap **target_cap, int *old_issued)
3573 __acquires(ci->i_ceph_lock)
a8599bd8
SW
3574{
3575 struct ceph_inode_info *ci = ceph_inode(inode);
2cd698be 3576 struct ceph_cap *cap, *ocap, *new_cap = NULL;
a8599bd8 3577 int mds = session->s_mds;
2cd698be
YZ
3578 int issued;
3579 unsigned caps = le32_to_cpu(im->caps);
a8599bd8
SW
3580 unsigned wanted = le32_to_cpu(im->wanted);
3581 unsigned seq = le32_to_cpu(im->seq);
3582 unsigned mseq = le32_to_cpu(im->migrate_seq);
3583 u64 realmino = le64_to_cpu(im->realm);
3584 u64 cap_id = le64_to_cpu(im->cap_id);
4ee6a914
YZ
3585 u64 p_cap_id;
3586 int peer;
a8599bd8 3587
4ee6a914
YZ
3588 if (ph) {
3589 p_cap_id = le64_to_cpu(ph->cap_id);
3590 peer = le32_to_cpu(ph->mds);
3591 } else {
3592 p_cap_id = 0;
3593 peer = -1;
3594 }
db354052 3595
4ee6a914
YZ
3596 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3597 inode, ci, mds, mseq, peer);
3598
d9df2783 3599retry:
4ee6a914 3600 spin_lock(&ci->i_ceph_lock);
d9df2783
YZ
3601 cap = __get_cap_for_mds(ci, mds);
3602 if (!cap) {
3603 if (!new_cap) {
3604 spin_unlock(&ci->i_ceph_lock);
3605 new_cap = ceph_get_cap(mdsc, NULL);
3606 goto retry;
3607 }
2cd698be
YZ
3608 cap = new_cap;
3609 } else {
3610 if (new_cap) {
3611 ceph_put_cap(mdsc, new_cap);
3612 new_cap = NULL;
3613 }
d9df2783
YZ
3614 }
3615
2cd698be
YZ
3616 __ceph_caps_issued(ci, &issued);
3617 issued |= __ceph_caps_dirty(ci);
3618
3619 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
d9df2783
YZ
3620 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3621
2cd698be
YZ
3622 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3623 if (ocap && ocap->cap_id == p_cap_id) {
4ee6a914 3624 dout(" remove export cap %p mds%d flags %d\n",
2cd698be 3625 ocap, peer, ph->flags);
4ee6a914 3626 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
2cd698be
YZ
3627 (ocap->seq != le32_to_cpu(ph->seq) ||
3628 ocap->mseq != le32_to_cpu(ph->mseq))) {
d84b37f9
YZ
3629 pr_err_ratelimited("handle_cap_import: "
3630 "mismatched seq/mseq: ino (%llx.%llx) "
3631 "mds%d seq %d mseq %d importer mds%d "
3632 "has peer seq %d mseq %d\n",
3633 ceph_vinop(inode), peer, ocap->seq,
3634 ocap->mseq, mds, le32_to_cpu(ph->seq),
3635 le32_to_cpu(ph->mseq));
db354052 3636 }
2cd698be 3637 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
a8599bd8
SW
3638 }
3639
4ee6a914 3640 /* make sure we re-request max_size, if necessary */
4ee6a914 3641 ci->i_requested_max_size = 0;
d9df2783 3642
2cd698be
YZ
3643 *old_issued = issued;
3644 *target_cap = cap;
a8599bd8
SW
3645}
3646
3647/*
3648 * Handle a caps message from the MDS.
3649 *
3650 * Identify the appropriate session, inode, and call the right handler
3651 * based on the cap op.
3652 */
3653void ceph_handle_caps(struct ceph_mds_session *session,
3654 struct ceph_msg *msg)
3655{
3656 struct ceph_mds_client *mdsc = session->s_mdsc;
3d14c5d2 3657 struct super_block *sb = mdsc->fsc->sb;
a8599bd8 3658 struct inode *inode;
be655596 3659 struct ceph_inode_info *ci;
a8599bd8
SW
3660 struct ceph_cap *cap;
3661 struct ceph_mds_caps *h;
4ee6a914 3662 struct ceph_mds_cap_peer *peer = NULL;
779fe0fb
YZ
3663 struct ceph_snap_realm *realm = NULL;
3664 struct ceph_string *pool_ns = NULL;
2600d2dd 3665 int mds = session->s_mds;
2cd698be 3666 int op, issued;
3d7ded4d 3667 u32 seq, mseq;
a8599bd8 3668 struct ceph_vino vino;
6df058c0 3669 u64 tid;
fb01d1f8
YZ
3670 u64 inline_version = 0;
3671 void *inline_data = NULL;
3672 u32 inline_len = 0;
70edb55b 3673 void *snaptrace;
ce1fbc8d 3674 size_t snaptrace_len;
fb01d1f8 3675 void *p, *end;
a8599bd8
SW
3676
3677 dout("handle_caps from mds%d\n", mds);
3678
3679 /* decode */
4ee6a914 3680 end = msg->front.iov_base + msg->front.iov_len;
6df058c0 3681 tid = le64_to_cpu(msg->hdr.tid);
a8599bd8
SW
3682 if (msg->front.iov_len < sizeof(*h))
3683 goto bad;
3684 h = msg->front.iov_base;
3685 op = le32_to_cpu(h->op);
3686 vino.ino = le64_to_cpu(h->ino);
3687 vino.snap = CEPH_NOSNAP;
a8599bd8 3688 seq = le32_to_cpu(h->seq);
3d7ded4d 3689 mseq = le32_to_cpu(h->migrate_seq);
a8599bd8 3690
ce1fbc8d
SW
3691 snaptrace = h + 1;
3692 snaptrace_len = le32_to_cpu(h->snap_trace_len);
fb01d1f8 3693 p = snaptrace + snaptrace_len;
ce1fbc8d
SW
3694
3695 if (le16_to_cpu(msg->hdr.version) >= 2) {
fb01d1f8 3696 u32 flock_len;
ce1fbc8d 3697 ceph_decode_32_safe(&p, end, flock_len, bad);
4ee6a914
YZ
3698 if (p + flock_len > end)
3699 goto bad;
fb01d1f8 3700 p += flock_len;
ce1fbc8d
SW
3701 }
3702
4ee6a914
YZ
3703 if (le16_to_cpu(msg->hdr.version) >= 3) {
3704 if (op == CEPH_CAP_OP_IMPORT) {
4ee6a914
YZ
3705 if (p + sizeof(*peer) > end)
3706 goto bad;
3707 peer = p;
fb01d1f8 3708 p += sizeof(*peer);
11df2dfb
YZ
3709 } else if (op == CEPH_CAP_OP_EXPORT) {
3710 /* recorded in unused fields */
3711 peer = (void *)&h->size;
4ee6a914
YZ
3712 }
3713 }
3714
fb01d1f8
YZ
3715 if (le16_to_cpu(msg->hdr.version) >= 4) {
3716 ceph_decode_64_safe(&p, end, inline_version, bad);
3717 ceph_decode_32_safe(&p, end, inline_len, bad);
3718 if (p + inline_len > end)
3719 goto bad;
3720 inline_data = p;
3721 p += inline_len;
3722 }
3723
92475f05
JL
3724 if (le16_to_cpu(msg->hdr.version) >= 5) {
3725 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3726 u32 epoch_barrier;
3727
3728 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3729 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3730 }
3731
5ea5c5e0
YZ
3732 if (le16_to_cpu(msg->hdr.version) >= 8) {
3733 u64 flush_tid;
3734 u32 caller_uid, caller_gid;
779fe0fb 3735 u32 pool_ns_len;
92475f05 3736
5ea5c5e0
YZ
3737 /* version >= 6 */
3738 ceph_decode_64_safe(&p, end, flush_tid, bad);
3739 /* version >= 7 */
3740 ceph_decode_32_safe(&p, end, caller_uid, bad);
3741 ceph_decode_32_safe(&p, end, caller_gid, bad);
3742 /* version >= 8 */
3743 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
779fe0fb
YZ
3744 if (pool_ns_len > 0) {
3745 ceph_decode_need(&p, end, pool_ns_len, bad);
3746 pool_ns = ceph_find_or_create_string(p, pool_ns_len);
3747 p += pool_ns_len;
3748 }
5ea5c5e0
YZ
3749 }
3750
6cd3bcad
YZ
3751 /* lookup ino */
3752 inode = ceph_find_inode(sb, vino);
3753 ci = ceph_inode(inode);
3754 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3755 vino.snap, inode);
3756
a8599bd8
SW
3757 mutex_lock(&session->s_mutex);
3758 session->s_seq++;
3759 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3760 (unsigned)seq);
3761
a8599bd8
SW
3762 if (!inode) {
3763 dout(" i don't have ino %llx\n", vino.ino);
3d7ded4d 3764
a096b09a 3765 if (op == CEPH_CAP_OP_IMPORT) {
745a8e3b
YZ
3766 cap = ceph_get_cap(mdsc, NULL);
3767 cap->cap_ino = vino.ino;
3768 cap->queue_release = 1;
779fe0fb 3769 cap->cap_id = le64_to_cpu(h->cap_id);
745a8e3b
YZ
3770 cap->mseq = mseq;
3771 cap->seq = seq;
dc24de82 3772 cap->issue_seq = seq;
a096b09a 3773 spin_lock(&session->s_cap_lock);
745a8e3b
YZ
3774 list_add_tail(&cap->session_caps,
3775 &session->s_cap_releases);
3776 session->s_num_cap_releases++;
a096b09a
YZ
3777 spin_unlock(&session->s_cap_lock);
3778 }
21b559de 3779 goto flush_cap_releases;
a8599bd8
SW
3780 }
3781
3782 /* these will work even if we don't have a cap yet */
3783 switch (op) {
3784 case CEPH_CAP_OP_FLUSHSNAP_ACK:
6df058c0 3785 handle_cap_flushsnap_ack(inode, tid, h, session);
a8599bd8
SW
3786 goto done;
3787
3788 case CEPH_CAP_OP_EXPORT:
11df2dfb
YZ
3789 handle_cap_export(inode, h, peer, session);
3790 goto done_unlocked;
a8599bd8
SW
3791
3792 case CEPH_CAP_OP_IMPORT:
982d6011
YZ
3793 realm = NULL;
3794 if (snaptrace_len) {
3795 down_write(&mdsc->snap_rwsem);
3796 ceph_update_snap_trace(mdsc, snaptrace,
3797 snaptrace + snaptrace_len,
3798 false, &realm);
3799 downgrade_write(&mdsc->snap_rwsem);
3800 } else {
3801 down_read(&mdsc->snap_rwsem);
3802 }
4ee6a914 3803 handle_cap_import(mdsc, inode, h, peer, session,
2cd698be 3804 &cap, &issued);
779fe0fb 3805 handle_cap_grant(mdsc, inode, h, &pool_ns,
fb01d1f8 3806 inline_version, inline_data, inline_len,
779fe0fb 3807 msg->middle, session, cap, issued);
982d6011
YZ
3808 if (realm)
3809 ceph_put_snap_realm(mdsc, realm);
2cd698be 3810 goto done_unlocked;
a8599bd8
SW
3811 }
3812
3813 /* the rest require a cap */
be655596 3814 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
3815 cap = __get_cap_for_mds(ceph_inode(inode), mds);
3816 if (!cap) {
9dbd412f 3817 dout(" no cap on %p ino %llx.%llx from mds%d\n",
a8599bd8 3818 inode, ceph_ino(inode), ceph_snap(inode), mds);
be655596 3819 spin_unlock(&ci->i_ceph_lock);
21b559de 3820 goto flush_cap_releases;
a8599bd8
SW
3821 }
3822
be655596 3823 /* note that each of these drops i_ceph_lock for us */
a8599bd8
SW
3824 switch (op) {
3825 case CEPH_CAP_OP_REVOKE:
3826 case CEPH_CAP_OP_GRANT:
2cd698be
YZ
3827 __ceph_caps_issued(ci, &issued);
3828 issued |= __ceph_caps_dirty(ci);
779fe0fb 3829 handle_cap_grant(mdsc, inode, h, &pool_ns,
fb01d1f8 3830 inline_version, inline_data, inline_len,
779fe0fb 3831 msg->middle, session, cap, issued);
15637c8b 3832 goto done_unlocked;
a8599bd8
SW
3833
3834 case CEPH_CAP_OP_FLUSH_ACK:
6df058c0 3835 handle_cap_flush_ack(inode, tid, h, session, cap);
a8599bd8
SW
3836 break;
3837
3838 case CEPH_CAP_OP_TRUNC:
3839 handle_cap_trunc(inode, h, session);
3840 break;
3841
3842 default:
be655596 3843 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
3844 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3845 ceph_cap_op_name(op));
3846 }
3847
21b559de
GF
3848 goto done;
3849
3850flush_cap_releases:
3851 /*
745a8e3b 3852 * send any cap release message to try to move things
21b559de
GF
3853 * along for the mds (who clearly thinks we still have this
3854 * cap).
3855 */
21b559de
GF
3856 ceph_send_cap_releases(mdsc, session);
3857
a8599bd8 3858done:
15637c8b
SW
3859 mutex_unlock(&session->s_mutex);
3860done_unlocked:
e96a650a 3861 iput(inode);
779fe0fb 3862 ceph_put_string(pool_ns);
a8599bd8
SW
3863 return;
3864
3865bad:
3866 pr_err("ceph_handle_caps: corrupt message\n");
9ec7cab1 3867 ceph_msg_dump(msg);
a8599bd8
SW
3868 return;
3869}
3870
3871/*
3872 * Delayed work handler to process end of delayed cap release LRU list.
3873 */
afcdaea3 3874void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
a8599bd8 3875{
4b9f2042 3876 struct inode *inode;
a8599bd8
SW
3877 struct ceph_inode_info *ci;
3878 int flags = CHECK_CAPS_NODELAY;
3879
a8599bd8
SW
3880 dout("check_delayed_caps\n");
3881 while (1) {
3882 spin_lock(&mdsc->cap_delay_lock);
3883 if (list_empty(&mdsc->cap_delay_list))
3884 break;
3885 ci = list_first_entry(&mdsc->cap_delay_list,
3886 struct ceph_inode_info,
3887 i_cap_delay_list);
3888 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3889 time_before(jiffies, ci->i_hold_caps_max))
3890 break;
3891 list_del_init(&ci->i_cap_delay_list);
4b9f2042
YZ
3892
3893 inode = igrab(&ci->vfs_inode);
a8599bd8 3894 spin_unlock(&mdsc->cap_delay_lock);
4b9f2042
YZ
3895
3896 if (inode) {
3897 dout("check_delayed_caps on %p\n", inode);
3898 ceph_check_caps(ci, flags, NULL);
3899 iput(inode);
3900 }
a8599bd8
SW
3901 }
3902 spin_unlock(&mdsc->cap_delay_lock);
3903}
3904
afcdaea3
SW
3905/*
3906 * Flush all dirty caps to the mds
3907 */
3908void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
3909{
db354052
SW
3910 struct ceph_inode_info *ci;
3911 struct inode *inode;
afcdaea3
SW
3912
3913 dout("flush_dirty_caps\n");
3914 spin_lock(&mdsc->cap_dirty_lock);
db354052
SW
3915 while (!list_empty(&mdsc->cap_dirty)) {
3916 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
3917 i_dirty_item);
70b666c3
SW
3918 inode = &ci->vfs_inode;
3919 ihold(inode);
db354052 3920 dout("flush_dirty_caps %p\n", inode);
afcdaea3 3921 spin_unlock(&mdsc->cap_dirty_lock);
70b666c3
SW
3922 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
3923 iput(inode);
afcdaea3
SW
3924 spin_lock(&mdsc->cap_dirty_lock);
3925 }
3926 spin_unlock(&mdsc->cap_dirty_lock);
db354052 3927 dout("flush_dirty_caps done\n");
afcdaea3
SW
3928}
3929
774a6a11
YZ
3930void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
3931{
3932 int i;
3933 int bits = (fmode << 1) | 1;
3934 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3935 if (bits & (1 << i))
3936 ci->i_nr_by_mode[i]++;
3937 }
3938}
3939
a8599bd8
SW
3940/*
3941 * Drop open file reference. If we were the last open file,
3942 * we may need to release capabilities to the MDS (or schedule
3943 * their delayed release).
3944 */
3945void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
3946{
774a6a11
YZ
3947 int i, last = 0;
3948 int bits = (fmode << 1) | 1;
be655596 3949 spin_lock(&ci->i_ceph_lock);
774a6a11
YZ
3950 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3951 if (bits & (1 << i)) {
3952 BUG_ON(ci->i_nr_by_mode[i] == 0);
3953 if (--ci->i_nr_by_mode[i] == 0)
3954 last++;
3955 }
3956 }
3957 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
3958 &ci->vfs_inode, fmode,
3959 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
3960 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
be655596 3961 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
3962
3963 if (last && ci->i_vino.snap == CEPH_NOSNAP)
3964 ceph_check_caps(ci, 0, NULL);
3965}
3966
6ef0bc6d
ZZ
3967/*
3968 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
3969 * looks like the link count will hit 0, drop any other caps (other
3970 * than PIN) we don't specifically want (due to the file still being
3971 * open).
3972 */
3973int ceph_drop_caps_for_unlink(struct inode *inode)
3974{
3975 struct ceph_inode_info *ci = ceph_inode(inode);
3976 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
3977
3978 spin_lock(&ci->i_ceph_lock);
3979 if (inode->i_nlink == 1) {
3980 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
3981
3982 ci->i_ceph_flags |= CEPH_I_NODELAY;
3983 if (__ceph_caps_dirty(ci)) {
3984 struct ceph_mds_client *mdsc =
3985 ceph_inode_to_client(inode)->mdsc;
3986 __cap_delay_requeue_front(mdsc, ci);
3987 }
3988 }
3989 spin_unlock(&ci->i_ceph_lock);
3990 return drop;
3991}
3992
a8599bd8
SW
3993/*
3994 * Helpers for embedding cap and dentry lease releases into mds
3995 * requests.
3996 *
3997 * @force is used by dentry_release (below) to force inclusion of a
3998 * record for the directory inode, even when there aren't any caps to
3999 * drop.
4000 */
4001int ceph_encode_inode_release(void **p, struct inode *inode,
4002 int mds, int drop, int unless, int force)
4003{
4004 struct ceph_inode_info *ci = ceph_inode(inode);
4005 struct ceph_cap *cap;
4006 struct ceph_mds_request_release *rel = *p;
ec97f88b 4007 int used, dirty;
a8599bd8 4008 int ret = 0;
a8599bd8 4009
be655596 4010 spin_lock(&ci->i_ceph_lock);
916623da 4011 used = __ceph_caps_used(ci);
ec97f88b 4012 dirty = __ceph_caps_dirty(ci);
916623da 4013
ec97f88b
SW
4014 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4015 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
916623da
SW
4016 ceph_cap_string(unless));
4017
ec97f88b
SW
4018 /* only drop unused, clean caps */
4019 drop &= ~(used | dirty);
916623da 4020
a8599bd8
SW
4021 cap = __get_cap_for_mds(ci, mds);
4022 if (cap && __cap_is_valid(cap)) {
222b7f90
YZ
4023 unless &= cap->issued;
4024 if (unless) {
4025 if (unless & CEPH_CAP_AUTH_EXCL)
4026 drop &= ~CEPH_CAP_AUTH_SHARED;
4027 if (unless & CEPH_CAP_LINK_EXCL)
4028 drop &= ~CEPH_CAP_LINK_SHARED;
4029 if (unless & CEPH_CAP_XATTR_EXCL)
4030 drop &= ~CEPH_CAP_XATTR_SHARED;
4031 if (unless & CEPH_CAP_FILE_EXCL)
4032 drop &= ~CEPH_CAP_FILE_SHARED;
4033 }
4034
4035 if (force || (cap->issued & drop)) {
4036 if (cap->issued & drop) {
bb137f84
YZ
4037 int wanted = __ceph_caps_wanted(ci);
4038 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4039 wanted |= cap->mds_wanted;
4040 dout("encode_inode_release %p cap %p "
4041 "%s -> %s, wanted %s -> %s\n", inode, cap,
a8599bd8 4042 ceph_cap_string(cap->issued),
bb137f84
YZ
4043 ceph_cap_string(cap->issued & ~drop),
4044 ceph_cap_string(cap->mds_wanted),
4045 ceph_cap_string(wanted));
4046
a8599bd8
SW
4047 cap->issued &= ~drop;
4048 cap->implemented &= ~drop;
bb137f84 4049 cap->mds_wanted = wanted;
a8599bd8
SW
4050 } else {
4051 dout("encode_inode_release %p cap %p %s"
4052 " (force)\n", inode, cap,
4053 ceph_cap_string(cap->issued));
4054 }
4055
4056 rel->ino = cpu_to_le64(ceph_ino(inode));
4057 rel->cap_id = cpu_to_le64(cap->cap_id);
4058 rel->seq = cpu_to_le32(cap->seq);
08a0f24e 4059 rel->issue_seq = cpu_to_le32(cap->issue_seq);
a8599bd8 4060 rel->mseq = cpu_to_le32(cap->mseq);
fd7b95cd 4061 rel->caps = cpu_to_le32(cap->implemented);
a8599bd8
SW
4062 rel->wanted = cpu_to_le32(cap->mds_wanted);
4063 rel->dname_len = 0;
4064 rel->dname_seq = 0;
4065 *p += sizeof(*rel);
4066 ret = 1;
4067 } else {
222b7f90 4068 dout("encode_inode_release %p cap %p %s (noop)\n",
a8599bd8
SW
4069 inode, cap, ceph_cap_string(cap->issued));
4070 }
4071 }
be655596 4072 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
4073 return ret;
4074}
4075
4076int ceph_encode_dentry_release(void **p, struct dentry *dentry,
ca6c8ae0 4077 struct inode *dir,
a8599bd8
SW
4078 int mds, int drop, int unless)
4079{
ca6c8ae0 4080 struct dentry *parent = NULL;
a8599bd8
SW
4081 struct ceph_mds_request_release *rel = *p;
4082 struct ceph_dentry_info *di = ceph_dentry(dentry);
4083 int force = 0;
4084 int ret;
4085
4086 /*
4087 * force an record for the directory caps if we have a dentry lease.
be655596 4088 * this is racy (can't take i_ceph_lock and d_lock together), but it
a8599bd8
SW
4089 * doesn't have to be perfect; the mds will revoke anything we don't
4090 * release.
4091 */
4092 spin_lock(&dentry->d_lock);
4093 if (di->lease_session && di->lease_session->s_mds == mds)
4094 force = 1;
ca6c8ae0
JL
4095 if (!dir) {
4096 parent = dget(dentry->d_parent);
4097 dir = d_inode(parent);
4098 }
a8599bd8
SW
4099 spin_unlock(&dentry->d_lock);
4100
ca6c8ae0 4101 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
adf0d687 4102 dput(parent);
a8599bd8
SW
4103
4104 spin_lock(&dentry->d_lock);
4105 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4106 dout("encode_dentry_release %p mds%d seq %d\n",
4107 dentry, mds, (int)di->lease_seq);
4108 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4109 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4110 *p += dentry->d_name.len;
4111 rel->dname_seq = cpu_to_le32(di->lease_seq);
1dadcce3 4112 __ceph_mdsc_drop_dentry_lease(dentry);
a8599bd8
SW
4113 }
4114 spin_unlock(&dentry->d_lock);
4115 return ret;
4116}