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