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