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