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