]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/ceph/caps.c
ceph: Fix infinite loop in __wake_requests
[thirdparty/kernel/linux.git] / fs / ceph / caps.c
CommitLineData
3d14c5d2 1#include <linux/ceph/ceph_debug.h>
a8599bd8
SW
2
3#include <linux/fs.h>
4#include <linux/kernel.h>
5#include <linux/sched.h>
5a0e3ad6 6#include <linux/slab.h>
a8599bd8
SW
7#include <linux/vmalloc.h>
8#include <linux/wait.h>
f1a3d572 9#include <linux/writeback.h>
a8599bd8
SW
10
11#include "super.h"
3d14c5d2
YS
12#include "mds_client.h"
13#include <linux/ceph/decode.h>
14#include <linux/ceph/messenger.h>
a8599bd8
SW
15
16/*
17 * Capability management
18 *
19 * The Ceph metadata servers control client access to inode metadata
20 * and file data by issuing capabilities, granting clients permission
21 * to read and/or write both inode field and file data to OSDs
22 * (storage nodes). Each capability consists of a set of bits
23 * indicating which operations are allowed.
24 *
25 * If the client holds a *_SHARED cap, the client has a coherent value
26 * that can be safely read from the cached inode.
27 *
28 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
29 * client is allowed to change inode attributes (e.g., file size,
30 * mtime), note its dirty state in the ceph_cap, and asynchronously
31 * flush that metadata change to the MDS.
32 *
33 * In the event of a conflicting operation (perhaps by another
34 * client), the MDS will revoke the conflicting client capabilities.
35 *
36 * In order for a client to cache an inode, it must hold a capability
37 * with at least one MDS server. When inodes are released, release
38 * notifications are batched and periodically sent en masse to the MDS
39 * cluster to release server state.
40 */
41
42
43/*
44 * Generate readable cap strings for debugging output.
45 */
46#define MAX_CAP_STR 20
47static char cap_str[MAX_CAP_STR][40];
48static DEFINE_SPINLOCK(cap_str_lock);
49static int last_cap_str;
50
51static char *gcap_string(char *s, int c)
52{
53 if (c & CEPH_CAP_GSHARED)
54 *s++ = 's';
55 if (c & CEPH_CAP_GEXCL)
56 *s++ = 'x';
57 if (c & CEPH_CAP_GCACHE)
58 *s++ = 'c';
59 if (c & CEPH_CAP_GRD)
60 *s++ = 'r';
61 if (c & CEPH_CAP_GWR)
62 *s++ = 'w';
63 if (c & CEPH_CAP_GBUFFER)
64 *s++ = 'b';
65 if (c & CEPH_CAP_GLAZYIO)
66 *s++ = 'l';
67 return s;
68}
69
70const char *ceph_cap_string(int caps)
71{
72 int i;
73 char *s;
74 int c;
75
76 spin_lock(&cap_str_lock);
77 i = last_cap_str++;
78 if (last_cap_str == MAX_CAP_STR)
79 last_cap_str = 0;
80 spin_unlock(&cap_str_lock);
81
82 s = cap_str[i];
83
84 if (caps & CEPH_CAP_PIN)
85 *s++ = 'p';
86
87 c = (caps >> CEPH_CAP_SAUTH) & 3;
88 if (c) {
89 *s++ = 'A';
90 s = gcap_string(s, c);
91 }
92
93 c = (caps >> CEPH_CAP_SLINK) & 3;
94 if (c) {
95 *s++ = 'L';
96 s = gcap_string(s, c);
97 }
98
99 c = (caps >> CEPH_CAP_SXATTR) & 3;
100 if (c) {
101 *s++ = 'X';
102 s = gcap_string(s, c);
103 }
104
105 c = caps >> CEPH_CAP_SFILE;
106 if (c) {
107 *s++ = 'F';
108 s = gcap_string(s, c);
109 }
110
111 if (s == cap_str[i])
112 *s++ = '-';
113 *s = 0;
114 return cap_str[i];
115}
116
37151668 117void ceph_caps_init(struct ceph_mds_client *mdsc)
a8599bd8 118{
37151668
YS
119 INIT_LIST_HEAD(&mdsc->caps_list);
120 spin_lock_init(&mdsc->caps_list_lock);
a8599bd8
SW
121}
122
37151668 123void ceph_caps_finalize(struct ceph_mds_client *mdsc)
a8599bd8
SW
124{
125 struct ceph_cap *cap;
126
37151668
YS
127 spin_lock(&mdsc->caps_list_lock);
128 while (!list_empty(&mdsc->caps_list)) {
129 cap = list_first_entry(&mdsc->caps_list,
130 struct ceph_cap, caps_item);
a8599bd8
SW
131 list_del(&cap->caps_item);
132 kmem_cache_free(ceph_cap_cachep, cap);
133 }
37151668
YS
134 mdsc->caps_total_count = 0;
135 mdsc->caps_avail_count = 0;
136 mdsc->caps_use_count = 0;
137 mdsc->caps_reserve_count = 0;
138 mdsc->caps_min_count = 0;
139 spin_unlock(&mdsc->caps_list_lock);
85ccce43
SW
140}
141
37151668 142void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
85ccce43 143{
37151668
YS
144 spin_lock(&mdsc->caps_list_lock);
145 mdsc->caps_min_count += delta;
146 BUG_ON(mdsc->caps_min_count < 0);
147 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
148}
149
37151668
YS
150int ceph_reserve_caps(struct ceph_mds_client *mdsc,
151 struct ceph_cap_reservation *ctx, int need)
a8599bd8
SW
152{
153 int i;
154 struct ceph_cap *cap;
155 int have;
156 int alloc = 0;
157 LIST_HEAD(newcaps);
158 int ret = 0;
159
160 dout("reserve caps ctx=%p need=%d\n", ctx, need);
161
162 /* first reserve any caps that are already allocated */
37151668
YS
163 spin_lock(&mdsc->caps_list_lock);
164 if (mdsc->caps_avail_count >= need)
a8599bd8
SW
165 have = need;
166 else
37151668
YS
167 have = mdsc->caps_avail_count;
168 mdsc->caps_avail_count -= have;
169 mdsc->caps_reserve_count += have;
170 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
171 mdsc->caps_reserve_count +
172 mdsc->caps_avail_count);
173 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
174
175 for (i = have; i < need; i++) {
176 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
177 if (!cap) {
178 ret = -ENOMEM;
179 goto out_alloc_count;
180 }
181 list_add(&cap->caps_item, &newcaps);
182 alloc++;
183 }
184 BUG_ON(have + alloc != need);
185
37151668
YS
186 spin_lock(&mdsc->caps_list_lock);
187 mdsc->caps_total_count += alloc;
188 mdsc->caps_reserve_count += alloc;
189 list_splice(&newcaps, &mdsc->caps_list);
a8599bd8 190
37151668
YS
191 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
192 mdsc->caps_reserve_count +
193 mdsc->caps_avail_count);
194 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
195
196 ctx->count = need;
197 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
37151668
YS
198 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
199 mdsc->caps_reserve_count, mdsc->caps_avail_count);
a8599bd8
SW
200 return 0;
201
202out_alloc_count:
203 /* we didn't manage to reserve as much as we needed */
204 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
205 ctx, need, have);
206 return ret;
207}
208
37151668
YS
209int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
210 struct ceph_cap_reservation *ctx)
a8599bd8
SW
211{
212 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
213 if (ctx->count) {
37151668
YS
214 spin_lock(&mdsc->caps_list_lock);
215 BUG_ON(mdsc->caps_reserve_count < ctx->count);
216 mdsc->caps_reserve_count -= ctx->count;
217 mdsc->caps_avail_count += ctx->count;
a8599bd8
SW
218 ctx->count = 0;
219 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
37151668
YS
220 mdsc->caps_total_count, mdsc->caps_use_count,
221 mdsc->caps_reserve_count, mdsc->caps_avail_count);
222 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
223 mdsc->caps_reserve_count +
224 mdsc->caps_avail_count);
225 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
226 }
227 return 0;
228}
229
37151668
YS
230static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc,
231 struct ceph_cap_reservation *ctx)
a8599bd8
SW
232{
233 struct ceph_cap *cap = NULL;
234
235 /* temporary, until we do something about cap import/export */
443b3760
SW
236 if (!ctx) {
237 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
238 if (cap) {
4d1d0534 239 spin_lock(&mdsc->caps_list_lock);
37151668
YS
240 mdsc->caps_use_count++;
241 mdsc->caps_total_count++;
4d1d0534 242 spin_unlock(&mdsc->caps_list_lock);
443b3760
SW
243 }
244 return cap;
245 }
a8599bd8 246
37151668 247 spin_lock(&mdsc->caps_list_lock);
a8599bd8 248 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
37151668
YS
249 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
250 mdsc->caps_reserve_count, mdsc->caps_avail_count);
a8599bd8 251 BUG_ON(!ctx->count);
37151668
YS
252 BUG_ON(ctx->count > mdsc->caps_reserve_count);
253 BUG_ON(list_empty(&mdsc->caps_list));
a8599bd8
SW
254
255 ctx->count--;
37151668
YS
256 mdsc->caps_reserve_count--;
257 mdsc->caps_use_count++;
a8599bd8 258
37151668 259 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
a8599bd8
SW
260 list_del(&cap->caps_item);
261
37151668
YS
262 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
263 mdsc->caps_reserve_count + mdsc->caps_avail_count);
264 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
265 return cap;
266}
267
37151668 268void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
a8599bd8 269{
37151668 270 spin_lock(&mdsc->caps_list_lock);
7c1332b8 271 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
37151668
YS
272 cap, mdsc->caps_total_count, mdsc->caps_use_count,
273 mdsc->caps_reserve_count, mdsc->caps_avail_count);
274 mdsc->caps_use_count--;
a8599bd8 275 /*
85ccce43
SW
276 * Keep some preallocated caps around (ceph_min_count), to
277 * avoid lots of free/alloc churn.
a8599bd8 278 */
37151668
YS
279 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
280 mdsc->caps_min_count) {
281 mdsc->caps_total_count--;
a8599bd8
SW
282 kmem_cache_free(ceph_cap_cachep, cap);
283 } else {
37151668
YS
284 mdsc->caps_avail_count++;
285 list_add(&cap->caps_item, &mdsc->caps_list);
a8599bd8
SW
286 }
287
37151668
YS
288 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
289 mdsc->caps_reserve_count + mdsc->caps_avail_count);
290 spin_unlock(&mdsc->caps_list_lock);
a8599bd8
SW
291}
292
3d14c5d2 293void ceph_reservation_status(struct ceph_fs_client *fsc,
85ccce43
SW
294 int *total, int *avail, int *used, int *reserved,
295 int *min)
a8599bd8 296{
3d14c5d2 297 struct ceph_mds_client *mdsc = fsc->mdsc;
37151668 298
a8599bd8 299 if (total)
37151668 300 *total = mdsc->caps_total_count;
a8599bd8 301 if (avail)
37151668 302 *avail = mdsc->caps_avail_count;
a8599bd8 303 if (used)
37151668 304 *used = mdsc->caps_use_count;
a8599bd8 305 if (reserved)
37151668 306 *reserved = mdsc->caps_reserve_count;
85ccce43 307 if (min)
37151668 308 *min = mdsc->caps_min_count;
a8599bd8
SW
309}
310
311/*
312 * Find ceph_cap for given mds, if any.
313 *
be655596 314 * Called with i_ceph_lock held.
a8599bd8
SW
315 */
316static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
317{
318 struct ceph_cap *cap;
319 struct rb_node *n = ci->i_caps.rb_node;
320
321 while (n) {
322 cap = rb_entry(n, struct ceph_cap, ci_node);
323 if (mds < cap->mds)
324 n = n->rb_left;
325 else if (mds > cap->mds)
326 n = n->rb_right;
327 else
328 return cap;
329 }
330 return NULL;
331}
332
2bc50259
GF
333struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
334{
335 struct ceph_cap *cap;
336
be655596 337 spin_lock(&ci->i_ceph_lock);
2bc50259 338 cap = __get_cap_for_mds(ci, mds);
be655596 339 spin_unlock(&ci->i_ceph_lock);
2bc50259
GF
340 return cap;
341}
342
a8599bd8 343/*
33caad32 344 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
a8599bd8 345 */
ca81f3f6 346static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
a8599bd8
SW
347{
348 struct ceph_cap *cap;
349 int mds = -1;
350 struct rb_node *p;
351
33caad32 352 /* prefer mds with WR|BUFFER|EXCL caps */
a8599bd8
SW
353 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
354 cap = rb_entry(p, struct ceph_cap, ci_node);
355 mds = cap->mds;
a8599bd8
SW
356 if (cap->issued & (CEPH_CAP_FILE_WR |
357 CEPH_CAP_FILE_BUFFER |
358 CEPH_CAP_FILE_EXCL))
359 break;
360 }
361 return mds;
362}
363
364int ceph_get_cap_mds(struct inode *inode)
365{
be655596 366 struct ceph_inode_info *ci = ceph_inode(inode);
a8599bd8 367 int mds;
be655596 368 spin_lock(&ci->i_ceph_lock);
ca81f3f6 369 mds = __ceph_get_cap_mds(ceph_inode(inode));
be655596 370 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
371 return mds;
372}
373
374/*
be655596 375 * Called under i_ceph_lock.
a8599bd8
SW
376 */
377static void __insert_cap_node(struct ceph_inode_info *ci,
378 struct ceph_cap *new)
379{
380 struct rb_node **p = &ci->i_caps.rb_node;
381 struct rb_node *parent = NULL;
382 struct ceph_cap *cap = NULL;
383
384 while (*p) {
385 parent = *p;
386 cap = rb_entry(parent, struct ceph_cap, ci_node);
387 if (new->mds < cap->mds)
388 p = &(*p)->rb_left;
389 else if (new->mds > cap->mds)
390 p = &(*p)->rb_right;
391 else
392 BUG();
393 }
394
395 rb_link_node(&new->ci_node, parent, p);
396 rb_insert_color(&new->ci_node, &ci->i_caps);
397}
398
399/*
400 * (re)set cap hold timeouts, which control the delayed release
401 * of unused caps back to the MDS. Should be called on cap use.
402 */
403static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
404 struct ceph_inode_info *ci)
405{
3d14c5d2 406 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
a8599bd8
SW
407
408 ci->i_hold_caps_min = round_jiffies(jiffies +
409 ma->caps_wanted_delay_min * HZ);
410 ci->i_hold_caps_max = round_jiffies(jiffies +
411 ma->caps_wanted_delay_max * HZ);
412 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
413 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
414}
415
416/*
417 * (Re)queue cap at the end of the delayed cap release list.
418 *
419 * If I_FLUSH is set, leave the inode at the front of the list.
420 *
be655596 421 * Caller holds i_ceph_lock
a8599bd8
SW
422 * -> we take mdsc->cap_delay_lock
423 */
424static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
425 struct ceph_inode_info *ci)
426{
427 __cap_set_timeouts(mdsc, ci);
428 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
429 ci->i_ceph_flags, ci->i_hold_caps_max);
430 if (!mdsc->stopping) {
431 spin_lock(&mdsc->cap_delay_lock);
432 if (!list_empty(&ci->i_cap_delay_list)) {
433 if (ci->i_ceph_flags & CEPH_I_FLUSH)
434 goto no_change;
435 list_del_init(&ci->i_cap_delay_list);
436 }
437 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
438no_change:
439 spin_unlock(&mdsc->cap_delay_lock);
440 }
441}
442
443/*
444 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
445 * indicating we should send a cap message to flush dirty metadata
446 * asap, and move to the front of the delayed cap list.
447 */
448static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
449 struct ceph_inode_info *ci)
450{
451 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
452 spin_lock(&mdsc->cap_delay_lock);
453 ci->i_ceph_flags |= CEPH_I_FLUSH;
454 if (!list_empty(&ci->i_cap_delay_list))
455 list_del_init(&ci->i_cap_delay_list);
456 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
457 spin_unlock(&mdsc->cap_delay_lock);
458}
459
460/*
461 * Cancel delayed work on cap.
462 *
be655596 463 * Caller must hold i_ceph_lock.
a8599bd8
SW
464 */
465static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
466 struct ceph_inode_info *ci)
467{
468 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
469 if (list_empty(&ci->i_cap_delay_list))
470 return;
471 spin_lock(&mdsc->cap_delay_lock);
472 list_del_init(&ci->i_cap_delay_list);
473 spin_unlock(&mdsc->cap_delay_lock);
474}
475
476/*
477 * Common issue checks for add_cap, handle_cap_grant.
478 */
479static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
480 unsigned issued)
481{
482 unsigned had = __ceph_caps_issued(ci, NULL);
483
484 /*
485 * Each time we receive FILE_CACHE anew, we increment
486 * i_rdcache_gen.
487 */
2962507c
SW
488 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
489 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
a8599bd8
SW
490 ci->i_rdcache_gen++;
491
492 /*
c6ffe100 493 * if we are newly issued FILE_SHARED, clear D_COMPLETE; we
a8599bd8
SW
494 * don't know what happened to this directory while we didn't
495 * have the cap.
496 */
497 if ((issued & CEPH_CAP_FILE_SHARED) &&
498 (had & CEPH_CAP_FILE_SHARED) == 0) {
499 ci->i_shared_gen++;
c6ffe100
SW
500 if (S_ISDIR(ci->vfs_inode.i_mode))
501 ceph_dir_clear_complete(&ci->vfs_inode);
a8599bd8
SW
502 }
503}
504
505/*
506 * Add a capability under the given MDS session.
507 *
508 * Caller should hold session snap_rwsem (read) and s_mutex.
509 *
510 * @fmode is the open file mode, if we are opening a file, otherwise
511 * it is < 0. (This is so we can atomically add the cap and add an
512 * open file reference to it.)
513 */
514int ceph_add_cap(struct inode *inode,
515 struct ceph_mds_session *session, u64 cap_id,
516 int fmode, unsigned issued, unsigned wanted,
517 unsigned seq, unsigned mseq, u64 realmino, int flags,
518 struct ceph_cap_reservation *caps_reservation)
519{
3d14c5d2 520 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
a8599bd8
SW
521 struct ceph_inode_info *ci = ceph_inode(inode);
522 struct ceph_cap *new_cap = NULL;
523 struct ceph_cap *cap;
524 int mds = session->s_mds;
525 int actual_wanted;
526
527 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
528 session->s_mds, cap_id, ceph_cap_string(issued), seq);
529
530 /*
531 * If we are opening the file, include file mode wanted bits
532 * in wanted.
533 */
534 if (fmode >= 0)
535 wanted |= ceph_caps_for_mode(fmode);
536
537retry:
be655596 538 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
539 cap = __get_cap_for_mds(ci, mds);
540 if (!cap) {
541 if (new_cap) {
542 cap = new_cap;
543 new_cap = NULL;
544 } else {
be655596 545 spin_unlock(&ci->i_ceph_lock);
37151668 546 new_cap = get_cap(mdsc, caps_reservation);
a8599bd8
SW
547 if (new_cap == NULL)
548 return -ENOMEM;
549 goto retry;
550 }
551
552 cap->issued = 0;
553 cap->implemented = 0;
554 cap->mds = mds;
555 cap->mds_wanted = 0;
556
557 cap->ci = ci;
558 __insert_cap_node(ci, cap);
559
560 /* clear out old exporting info? (i.e. on cap import) */
561 if (ci->i_cap_exporting_mds == mds) {
562 ci->i_cap_exporting_issued = 0;
563 ci->i_cap_exporting_mseq = 0;
564 ci->i_cap_exporting_mds = -1;
565 }
566
567 /* add to session cap list */
568 cap->session = session;
569 spin_lock(&session->s_cap_lock);
570 list_add_tail(&cap->session_caps, &session->s_caps);
571 session->s_nr_caps++;
572 spin_unlock(&session->s_cap_lock);
3540303f
SW
573 } else if (new_cap)
574 ceph_put_cap(mdsc, new_cap);
a8599bd8
SW
575
576 if (!ci->i_snap_realm) {
577 /*
578 * add this inode to the appropriate snap realm
579 */
580 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
581 realmino);
582 if (realm) {
583 ceph_get_snap_realm(mdsc, realm);
584 spin_lock(&realm->inodes_with_caps_lock);
585 ci->i_snap_realm = realm;
586 list_add(&ci->i_snap_realm_item,
587 &realm->inodes_with_caps);
588 spin_unlock(&realm->inodes_with_caps_lock);
589 } else {
590 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
591 realmino);
b8cd07e7 592 WARN_ON(!realm);
a8599bd8
SW
593 }
594 }
595
596 __check_cap_issue(ci, cap, issued);
597
598 /*
599 * If we are issued caps we don't want, or the mds' wanted
600 * value appears to be off, queue a check so we'll release
601 * later and/or update the mds wanted value.
602 */
603 actual_wanted = __ceph_caps_wanted(ci);
604 if ((wanted & ~actual_wanted) ||
605 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
606 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
607 ceph_cap_string(issued), ceph_cap_string(wanted),
608 ceph_cap_string(actual_wanted));
609 __cap_delay_requeue(mdsc, ci);
610 }
611
612 if (flags & CEPH_CAP_FLAG_AUTH)
613 ci->i_auth_cap = cap;
614 else if (ci->i_auth_cap == cap)
615 ci->i_auth_cap = NULL;
616
617 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
618 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
619 ceph_cap_string(issued|cap->issued), seq, mds);
620 cap->cap_id = cap_id;
621 cap->issued = issued;
622 cap->implemented |= issued;
623 cap->mds_wanted |= wanted;
624 cap->seq = seq;
625 cap->issue_seq = seq;
626 cap->mseq = mseq;
685f9a5d 627 cap->cap_gen = session->s_cap_gen;
a8599bd8
SW
628
629 if (fmode >= 0)
630 __ceph_get_fmode(ci, fmode);
be655596 631 spin_unlock(&ci->i_ceph_lock);
03066f23 632 wake_up_all(&ci->i_cap_wq);
a8599bd8
SW
633 return 0;
634}
635
636/*
637 * Return true if cap has not timed out and belongs to the current
638 * generation of the MDS session (i.e. has not gone 'stale' due to
639 * us losing touch with the mds).
640 */
641static int __cap_is_valid(struct ceph_cap *cap)
642{
643 unsigned long ttl;
cdac8303 644 u32 gen;
a8599bd8 645
d8fb02ab 646 spin_lock(&cap->session->s_gen_ttl_lock);
a8599bd8
SW
647 gen = cap->session->s_cap_gen;
648 ttl = cap->session->s_cap_ttl;
d8fb02ab 649 spin_unlock(&cap->session->s_gen_ttl_lock);
a8599bd8 650
685f9a5d 651 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
a8599bd8
SW
652 dout("__cap_is_valid %p cap %p issued %s "
653 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
685f9a5d 654 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
a8599bd8
SW
655 return 0;
656 }
657
658 return 1;
659}
660
661/*
662 * Return set of valid cap bits issued to us. Note that caps time
663 * out, and may be invalidated in bulk if the client session times out
664 * and session->s_cap_gen is bumped.
665 */
666int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
667{
7af8f1e4 668 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
a8599bd8
SW
669 struct ceph_cap *cap;
670 struct rb_node *p;
671
672 if (implemented)
673 *implemented = 0;
674 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
675 cap = rb_entry(p, struct ceph_cap, ci_node);
676 if (!__cap_is_valid(cap))
677 continue;
678 dout("__ceph_caps_issued %p cap %p issued %s\n",
679 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
680 have |= cap->issued;
681 if (implemented)
682 *implemented |= cap->implemented;
683 }
684 return have;
685}
686
687/*
688 * Get cap bits issued by caps other than @ocap
689 */
690int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
691{
692 int have = ci->i_snap_caps;
693 struct ceph_cap *cap;
694 struct rb_node *p;
695
696 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
697 cap = rb_entry(p, struct ceph_cap, ci_node);
698 if (cap == ocap)
699 continue;
700 if (!__cap_is_valid(cap))
701 continue;
702 have |= cap->issued;
703 }
704 return have;
705}
706
707/*
708 * Move a cap to the end of the LRU (oldest caps at list head, newest
709 * at list tail).
710 */
711static void __touch_cap(struct ceph_cap *cap)
712{
713 struct ceph_mds_session *s = cap->session;
714
a8599bd8 715 spin_lock(&s->s_cap_lock);
7c1332b8 716 if (s->s_cap_iterator == NULL) {
5dacf091
SW
717 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
718 s->s_mds);
719 list_move_tail(&cap->session_caps, &s->s_caps);
720 } else {
721 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
722 &cap->ci->vfs_inode, cap, s->s_mds);
723 }
a8599bd8
SW
724 spin_unlock(&s->s_cap_lock);
725}
726
727/*
728 * Check if we hold the given mask. If so, move the cap(s) to the
729 * front of their respective LRUs. (This is the preferred way for
730 * callers to check for caps they want.)
731 */
732int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
733{
734 struct ceph_cap *cap;
735 struct rb_node *p;
736 int have = ci->i_snap_caps;
737
738 if ((have & mask) == mask) {
739 dout("__ceph_caps_issued_mask %p snap issued %s"
740 " (mask %s)\n", &ci->vfs_inode,
741 ceph_cap_string(have),
742 ceph_cap_string(mask));
743 return 1;
744 }
745
746 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
747 cap = rb_entry(p, struct ceph_cap, ci_node);
748 if (!__cap_is_valid(cap))
749 continue;
750 if ((cap->issued & mask) == mask) {
751 dout("__ceph_caps_issued_mask %p cap %p issued %s"
752 " (mask %s)\n", &ci->vfs_inode, cap,
753 ceph_cap_string(cap->issued),
754 ceph_cap_string(mask));
755 if (touch)
756 __touch_cap(cap);
757 return 1;
758 }
759
760 /* does a combination of caps satisfy mask? */
761 have |= cap->issued;
762 if ((have & mask) == mask) {
763 dout("__ceph_caps_issued_mask %p combo issued %s"
764 " (mask %s)\n", &ci->vfs_inode,
765 ceph_cap_string(cap->issued),
766 ceph_cap_string(mask));
767 if (touch) {
768 struct rb_node *q;
769
25985edc 770 /* touch this + preceding caps */
a8599bd8
SW
771 __touch_cap(cap);
772 for (q = rb_first(&ci->i_caps); q != p;
773 q = rb_next(q)) {
774 cap = rb_entry(q, struct ceph_cap,
775 ci_node);
776 if (!__cap_is_valid(cap))
777 continue;
778 __touch_cap(cap);
779 }
780 }
781 return 1;
782 }
783 }
784
785 return 0;
786}
787
788/*
789 * Return true if mask caps are currently being revoked by an MDS.
790 */
791int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
792{
793 struct inode *inode = &ci->vfs_inode;
794 struct ceph_cap *cap;
795 struct rb_node *p;
796 int ret = 0;
797
be655596 798 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
799 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
800 cap = rb_entry(p, struct ceph_cap, ci_node);
801 if (__cap_is_valid(cap) &&
802 (cap->implemented & ~cap->issued & mask)) {
803 ret = 1;
804 break;
805 }
806 }
be655596 807 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
808 dout("ceph_caps_revoking %p %s = %d\n", inode,
809 ceph_cap_string(mask), ret);
810 return ret;
811}
812
813int __ceph_caps_used(struct ceph_inode_info *ci)
814{
815 int used = 0;
816 if (ci->i_pin_ref)
817 used |= CEPH_CAP_PIN;
818 if (ci->i_rd_ref)
819 used |= CEPH_CAP_FILE_RD;
a43fb731 820 if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages)
a8599bd8
SW
821 used |= CEPH_CAP_FILE_CACHE;
822 if (ci->i_wr_ref)
823 used |= CEPH_CAP_FILE_WR;
d3d0720d 824 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
a8599bd8
SW
825 used |= CEPH_CAP_FILE_BUFFER;
826 return used;
827}
828
829/*
830 * wanted, by virtue of open file modes
831 */
832int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
833{
834 int want = 0;
835 int mode;
33caad32 836 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
a8599bd8
SW
837 if (ci->i_nr_by_mode[mode])
838 want |= ceph_caps_for_mode(mode);
839 return want;
840}
841
842/*
843 * Return caps we have registered with the MDS(s) as 'wanted'.
844 */
845int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
846{
847 struct ceph_cap *cap;
848 struct rb_node *p;
849 int mds_wanted = 0;
850
851 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
852 cap = rb_entry(p, struct ceph_cap, ci_node);
853 if (!__cap_is_valid(cap))
854 continue;
855 mds_wanted |= cap->mds_wanted;
856 }
857 return mds_wanted;
858}
859
860/*
be655596 861 * called under i_ceph_lock
a8599bd8
SW
862 */
863static int __ceph_is_any_caps(struct ceph_inode_info *ci)
864{
865 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
866}
867
868/*
f818a736
SW
869 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
870 *
be655596 871 * caller should hold i_ceph_lock.
a6369741 872 * caller will not hold session s_mutex if called from destroy_inode.
a8599bd8 873 */
7c1332b8 874void __ceph_remove_cap(struct ceph_cap *cap)
a8599bd8
SW
875{
876 struct ceph_mds_session *session = cap->session;
877 struct ceph_inode_info *ci = cap->ci;
640ef79d 878 struct ceph_mds_client *mdsc =
3d14c5d2 879 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
f818a736 880 int removed = 0;
a8599bd8
SW
881
882 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
883
7c1332b8
SW
884 /* remove from session list */
885 spin_lock(&session->s_cap_lock);
886 if (session->s_cap_iterator == cap) {
887 /* not yet, we are iterating over this very cap */
888 dout("__ceph_remove_cap delaying %p removal from session %p\n",
889 cap, cap->session);
890 } else {
891 list_del_init(&cap->session_caps);
892 session->s_nr_caps--;
893 cap->session = NULL;
f818a736 894 removed = 1;
7c1332b8 895 }
f818a736
SW
896 /* protect backpointer with s_cap_lock: see iterate_session_caps */
897 cap->ci = NULL;
7c1332b8
SW
898 spin_unlock(&session->s_cap_lock);
899
f818a736
SW
900 /* remove from inode list */
901 rb_erase(&cap->ci_node, &ci->i_caps);
902 if (ci->i_auth_cap == cap)
903 ci->i_auth_cap = NULL;
904
905 if (removed)
37151668 906 ceph_put_cap(mdsc, cap);
a8599bd8
SW
907
908 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
909 struct ceph_snap_realm *realm = ci->i_snap_realm;
910 spin_lock(&realm->inodes_with_caps_lock);
911 list_del_init(&ci->i_snap_realm_item);
912 ci->i_snap_realm_counter++;
913 ci->i_snap_realm = NULL;
914 spin_unlock(&realm->inodes_with_caps_lock);
915 ceph_put_snap_realm(mdsc, realm);
916 }
917 if (!__ceph_is_any_real_caps(ci))
918 __cap_delay_cancel(mdsc, ci);
919}
920
921/*
922 * Build and send a cap message to the given MDS.
923 *
924 * Caller should be holding s_mutex.
925 */
926static int send_cap_msg(struct ceph_mds_session *session,
927 u64 ino, u64 cid, int op,
928 int caps, int wanted, int dirty,
929 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
930 u64 size, u64 max_size,
931 struct timespec *mtime, struct timespec *atime,
932 u64 time_warp_seq,
5706b27d 933 uid_t uid, gid_t gid, umode_t mode,
a8599bd8
SW
934 u64 xattr_version,
935 struct ceph_buffer *xattrs_buf,
936 u64 follows)
937{
938 struct ceph_mds_caps *fc;
939 struct ceph_msg *msg;
940
941 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
942 " seq %u/%u mseq %u follows %lld size %llu/%llu"
943 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
944 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
945 ceph_cap_string(dirty),
946 seq, issue_seq, mseq, follows, size, max_size,
947 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
948
b61c2763 949 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false);
a79832f2
SW
950 if (!msg)
951 return -ENOMEM;
a8599bd8 952
6df058c0 953 msg->hdr.tid = cpu_to_le64(flush_tid);
a8599bd8 954
6df058c0 955 fc = msg->front.iov_base;
a8599bd8
SW
956 memset(fc, 0, sizeof(*fc));
957
958 fc->cap_id = cpu_to_le64(cid);
959 fc->op = cpu_to_le32(op);
960 fc->seq = cpu_to_le32(seq);
a8599bd8
SW
961 fc->issue_seq = cpu_to_le32(issue_seq);
962 fc->migrate_seq = cpu_to_le32(mseq);
963 fc->caps = cpu_to_le32(caps);
964 fc->wanted = cpu_to_le32(wanted);
965 fc->dirty = cpu_to_le32(dirty);
966 fc->ino = cpu_to_le64(ino);
967 fc->snap_follows = cpu_to_le64(follows);
968
969 fc->size = cpu_to_le64(size);
970 fc->max_size = cpu_to_le64(max_size);
971 if (mtime)
972 ceph_encode_timespec(&fc->mtime, mtime);
973 if (atime)
974 ceph_encode_timespec(&fc->atime, atime);
975 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
976
977 fc->uid = cpu_to_le32(uid);
978 fc->gid = cpu_to_le32(gid);
979 fc->mode = cpu_to_le32(mode);
980
981 fc->xattr_version = cpu_to_le64(xattr_version);
982 if (xattrs_buf) {
983 msg->middle = ceph_buffer_get(xattrs_buf);
984 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
985 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
986 }
987
988 ceph_con_send(&session->s_con, msg);
989 return 0;
990}
991
3d7ded4d
SW
992static void __queue_cap_release(struct ceph_mds_session *session,
993 u64 ino, u64 cap_id, u32 migrate_seq,
994 u32 issue_seq)
995{
996 struct ceph_msg *msg;
997 struct ceph_mds_cap_release *head;
998 struct ceph_mds_cap_item *item;
999
1000 spin_lock(&session->s_cap_lock);
1001 BUG_ON(!session->s_num_cap_releases);
1002 msg = list_first_entry(&session->s_cap_releases,
1003 struct ceph_msg, list_head);
1004
1005 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1006 ino, session->s_mds, msg, session->s_num_cap_releases);
1007
1008 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1009 head = msg->front.iov_base;
b905a7f8 1010 le32_add_cpu(&head->num, 1);
3d7ded4d
SW
1011 item = msg->front.iov_base + msg->front.iov_len;
1012 item->ino = cpu_to_le64(ino);
1013 item->cap_id = cpu_to_le64(cap_id);
1014 item->migrate_seq = cpu_to_le32(migrate_seq);
1015 item->seq = cpu_to_le32(issue_seq);
1016
1017 session->s_num_cap_releases--;
1018
1019 msg->front.iov_len += sizeof(*item);
1020 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1021 dout(" release msg %p full\n", msg);
1022 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1023 } else {
1024 dout(" release msg %p at %d/%d (%d)\n", msg,
1025 (int)le32_to_cpu(head->num),
1026 (int)CEPH_CAPS_PER_RELEASE,
1027 (int)msg->front.iov_len);
1028 }
1029 spin_unlock(&session->s_cap_lock);
1030}
1031
a8599bd8 1032/*
a6369741 1033 * Queue cap releases when an inode is dropped from our cache. Since
be655596 1034 * inode is about to be destroyed, there is no need for i_ceph_lock.
a8599bd8
SW
1035 */
1036void ceph_queue_caps_release(struct inode *inode)
1037{
1038 struct ceph_inode_info *ci = ceph_inode(inode);
1039 struct rb_node *p;
1040
a8599bd8
SW
1041 p = rb_first(&ci->i_caps);
1042 while (p) {
1043 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1044 struct ceph_mds_session *session = cap->session;
a8599bd8 1045
3d7ded4d
SW
1046 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1047 cap->mseq, cap->issue_seq);
a8599bd8 1048 p = rb_next(p);
7c1332b8 1049 __ceph_remove_cap(cap);
a8599bd8 1050 }
a8599bd8
SW
1051}
1052
1053/*
1054 * Send a cap msg on the given inode. Update our caps state, then
be655596 1055 * drop i_ceph_lock and send the message.
a8599bd8
SW
1056 *
1057 * Make note of max_size reported/requested from mds, revoked caps
1058 * that have now been implemented.
1059 *
1060 * Make half-hearted attempt ot to invalidate page cache if we are
1061 * dropping RDCACHE. Note that this will leave behind locked pages
1062 * that we'll then need to deal with elsewhere.
1063 *
1064 * Return non-zero if delayed release, or we experienced an error
1065 * such that the caller should requeue + retry later.
1066 *
be655596 1067 * called with i_ceph_lock, then drops it.
a8599bd8
SW
1068 * caller should hold snap_rwsem (read), s_mutex.
1069 */
1070static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1071 int op, int used, int want, int retain, int flushing,
1072 unsigned *pflush_tid)
be655596 1073 __releases(cap->ci->i_ceph_lock)
a8599bd8
SW
1074{
1075 struct ceph_inode_info *ci = cap->ci;
1076 struct inode *inode = &ci->vfs_inode;
1077 u64 cap_id = cap->cap_id;
68c28323 1078 int held, revoking, dropping, keep;
a8599bd8
SW
1079 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1080 u64 size, max_size;
1081 struct timespec mtime, atime;
1082 int wake = 0;
5706b27d 1083 umode_t mode;
a8599bd8
SW
1084 uid_t uid;
1085 gid_t gid;
1086 struct ceph_mds_session *session;
1087 u64 xattr_version = 0;
082afec9 1088 struct ceph_buffer *xattr_blob = NULL;
a8599bd8
SW
1089 int delayed = 0;
1090 u64 flush_tid = 0;
1091 int i;
1092 int ret;
1093
68c28323
SW
1094 held = cap->issued | cap->implemented;
1095 revoking = cap->implemented & ~cap->issued;
1096 retain &= ~revoking;
1097 dropping = cap->issued & ~retain;
1098
a8599bd8
SW
1099 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1100 inode, cap, cap->session,
1101 ceph_cap_string(held), ceph_cap_string(held & retain),
1102 ceph_cap_string(revoking));
1103 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1104
1105 session = cap->session;
1106
1107 /* don't release wanted unless we've waited a bit. */
1108 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1109 time_before(jiffies, ci->i_hold_caps_min)) {
1110 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1111 ceph_cap_string(cap->issued),
1112 ceph_cap_string(cap->issued & retain),
1113 ceph_cap_string(cap->mds_wanted),
1114 ceph_cap_string(want));
1115 want |= cap->mds_wanted;
1116 retain |= cap->issued;
1117 delayed = 1;
1118 }
1119 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1120
1121 cap->issued &= retain; /* drop bits we don't want */
1122 if (cap->implemented & ~cap->issued) {
1123 /*
1124 * Wake up any waiters on wanted -> needed transition.
1125 * This is due to the weird transition from buffered
1126 * to sync IO... we need to flush dirty pages _before_
1127 * allowing sync writes to avoid reordering.
1128 */
1129 wake = 1;
1130 }
1131 cap->implemented &= cap->issued | used;
1132 cap->mds_wanted = want;
1133
1134 if (flushing) {
1135 /*
1136 * assign a tid for flush operations so we can avoid
1137 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1138 * clean type races. track latest tid for every bit
1139 * so we can handle flush AxFw, flush Fw, and have the
1140 * first ack clean Ax.
1141 */
1142 flush_tid = ++ci->i_cap_flush_last_tid;
1143 if (pflush_tid)
1144 *pflush_tid = flush_tid;
1145 dout(" cap_flush_tid %d\n", (int)flush_tid);
1146 for (i = 0; i < CEPH_CAP_BITS; i++)
1147 if (flushing & (1 << i))
1148 ci->i_cap_flush_tid[i] = flush_tid;
7d8cb26d
SW
1149
1150 follows = ci->i_head_snapc->seq;
1151 } else {
1152 follows = 0;
a8599bd8
SW
1153 }
1154
1155 keep = cap->implemented;
1156 seq = cap->seq;
1157 issue_seq = cap->issue_seq;
1158 mseq = cap->mseq;
1159 size = inode->i_size;
1160 ci->i_reported_size = size;
1161 max_size = ci->i_wanted_max_size;
1162 ci->i_requested_max_size = max_size;
1163 mtime = inode->i_mtime;
1164 atime = inode->i_atime;
1165 time_warp_seq = ci->i_time_warp_seq;
a8599bd8
SW
1166 uid = inode->i_uid;
1167 gid = inode->i_gid;
1168 mode = inode->i_mode;
1169
082afec9 1170 if (flushing & CEPH_CAP_XATTR_EXCL) {
a8599bd8 1171 __ceph_build_xattrs_blob(ci);
082afec9
SW
1172 xattr_blob = ci->i_xattrs.blob;
1173 xattr_version = ci->i_xattrs.version;
a8599bd8
SW
1174 }
1175
be655596 1176 spin_unlock(&ci->i_ceph_lock);
a8599bd8 1177
a8599bd8
SW
1178 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1179 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1180 size, max_size, &mtime, &atime, time_warp_seq,
082afec9 1181 uid, gid, mode, xattr_version, xattr_blob,
a8599bd8
SW
1182 follows);
1183 if (ret < 0) {
1184 dout("error sending cap msg, must requeue %p\n", inode);
1185 delayed = 1;
1186 }
1187
1188 if (wake)
03066f23 1189 wake_up_all(&ci->i_cap_wq);
a8599bd8
SW
1190
1191 return delayed;
1192}
1193
1194/*
1195 * When a snapshot is taken, clients accumulate dirty metadata on
1196 * inodes with capabilities in ceph_cap_snaps to describe the file
1197 * state at the time the snapshot was taken. This must be flushed
1198 * asynchronously back to the MDS once sync writes complete and dirty
1199 * data is written out.
1200 *
e835124c
SW
1201 * Unless @again is true, skip cap_snaps that were already sent to
1202 * the MDS (i.e., during this session).
1203 *
be655596 1204 * Called under i_ceph_lock. Takes s_mutex as needed.
a8599bd8
SW
1205 */
1206void __ceph_flush_snaps(struct ceph_inode_info *ci,
e835124c
SW
1207 struct ceph_mds_session **psession,
1208 int again)
be655596
SW
1209 __releases(ci->i_ceph_lock)
1210 __acquires(ci->i_ceph_lock)
a8599bd8
SW
1211{
1212 struct inode *inode = &ci->vfs_inode;
1213 int mds;
1214 struct ceph_cap_snap *capsnap;
1215 u32 mseq;
3d14c5d2 1216 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
a8599bd8
SW
1217 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1218 session->s_mutex */
1219 u64 next_follows = 0; /* keep track of how far we've gotten through the
1220 i_cap_snaps list, and skip these entries next time
1221 around to avoid an infinite loop */
1222
1223 if (psession)
1224 session = *psession;
1225
1226 dout("__flush_snaps %p\n", inode);
1227retry:
1228 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1229 /* avoid an infiniute loop after retry */
1230 if (capsnap->follows < next_follows)
1231 continue;
1232 /*
1233 * we need to wait for sync writes to complete and for dirty
1234 * pages to be written out.
1235 */
1236 if (capsnap->dirty_pages || capsnap->writing)
cfc0bf66 1237 break;
a8599bd8 1238
819ccbfa
SW
1239 /*
1240 * if cap writeback already occurred, we should have dropped
1241 * the capsnap in ceph_put_wrbuffer_cap_refs.
1242 */
1243 BUG_ON(capsnap->dirty == 0);
1244
a8599bd8 1245 /* pick mds, take s_mutex */
ca81f3f6
SW
1246 if (ci->i_auth_cap == NULL) {
1247 dout("no auth cap (migrating?), doing nothing\n");
1248 goto out;
1249 }
e835124c
SW
1250
1251 /* only flush each capsnap once */
1252 if (!again && !list_empty(&capsnap->flushing_item)) {
1253 dout("already flushed %p, skipping\n", capsnap);
1254 continue;
1255 }
1256
ca81f3f6
SW
1257 mds = ci->i_auth_cap->session->s_mds;
1258 mseq = ci->i_auth_cap->mseq;
1259
a8599bd8
SW
1260 if (session && session->s_mds != mds) {
1261 dout("oops, wrong session %p mutex\n", session);
1262 mutex_unlock(&session->s_mutex);
1263 ceph_put_mds_session(session);
1264 session = NULL;
1265 }
1266 if (!session) {
be655596 1267 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1268 mutex_lock(&mdsc->mutex);
1269 session = __ceph_lookup_mds_session(mdsc, mds);
1270 mutex_unlock(&mdsc->mutex);
1271 if (session) {
1272 dout("inverting session/ino locks on %p\n",
1273 session);
1274 mutex_lock(&session->s_mutex);
1275 }
1276 /*
1277 * if session == NULL, we raced against a cap
ca81f3f6
SW
1278 * deletion or migration. retry, and we'll
1279 * get a better @mds value next time.
a8599bd8 1280 */
be655596 1281 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1282 goto retry;
1283 }
1284
1285 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1286 atomic_inc(&capsnap->nref);
1287 if (!list_empty(&capsnap->flushing_item))
1288 list_del_init(&capsnap->flushing_item);
1289 list_add_tail(&capsnap->flushing_item,
1290 &session->s_cap_snaps_flushing);
be655596 1291 spin_unlock(&ci->i_ceph_lock);
a8599bd8 1292
cfc0bf66
SW
1293 dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
1294 inode, capsnap, capsnap->follows, capsnap->flush_tid);
a8599bd8
SW
1295 send_cap_msg(session, ceph_vino(inode).ino, 0,
1296 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1297 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1298 capsnap->size, 0,
1299 &capsnap->mtime, &capsnap->atime,
1300 capsnap->time_warp_seq,
1301 capsnap->uid, capsnap->gid, capsnap->mode,
4a625be4 1302 capsnap->xattr_version, capsnap->xattr_blob,
a8599bd8
SW
1303 capsnap->follows);
1304
1305 next_follows = capsnap->follows + 1;
1306 ceph_put_cap_snap(capsnap);
1307
be655596 1308 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1309 goto retry;
1310 }
1311
1312 /* we flushed them all; remove this inode from the queue */
1313 spin_lock(&mdsc->snap_flush_lock);
1314 list_del_init(&ci->i_snap_flush_item);
1315 spin_unlock(&mdsc->snap_flush_lock);
1316
ca81f3f6 1317out:
a8599bd8
SW
1318 if (psession)
1319 *psession = session;
1320 else if (session) {
1321 mutex_unlock(&session->s_mutex);
1322 ceph_put_mds_session(session);
1323 }
1324}
1325
1326static void ceph_flush_snaps(struct ceph_inode_info *ci)
1327{
be655596 1328 spin_lock(&ci->i_ceph_lock);
e835124c 1329 __ceph_flush_snaps(ci, NULL, 0);
be655596 1330 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1331}
1332
76e3b390 1333/*
fca65b4a
SW
1334 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1335 * Caller is then responsible for calling __mark_inode_dirty with the
1336 * returned flags value.
76e3b390 1337 */
fca65b4a 1338int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
76e3b390 1339{
640ef79d 1340 struct ceph_mds_client *mdsc =
3d14c5d2 1341 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
76e3b390
SW
1342 struct inode *inode = &ci->vfs_inode;
1343 int was = ci->i_dirty_caps;
1344 int dirty = 0;
1345
1346 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1347 ceph_cap_string(mask), ceph_cap_string(was),
1348 ceph_cap_string(was | mask));
1349 ci->i_dirty_caps |= mask;
1350 if (was == 0) {
7d8cb26d
SW
1351 if (!ci->i_head_snapc)
1352 ci->i_head_snapc = ceph_get_snap_context(
1353 ci->i_snap_realm->cached_context);
1354 dout(" inode %p now dirty snapc %p\n", &ci->vfs_inode,
1355 ci->i_head_snapc);
76e3b390
SW
1356 BUG_ON(!list_empty(&ci->i_dirty_item));
1357 spin_lock(&mdsc->cap_dirty_lock);
1358 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1359 spin_unlock(&mdsc->cap_dirty_lock);
1360 if (ci->i_flushing_caps == 0) {
3772d26d 1361 ihold(inode);
76e3b390
SW
1362 dirty |= I_DIRTY_SYNC;
1363 }
1364 }
1365 BUG_ON(list_empty(&ci->i_dirty_item));
1366 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1367 (mask & CEPH_CAP_FILE_BUFFER))
1368 dirty |= I_DIRTY_DATASYNC;
76e3b390 1369 __cap_delay_requeue(mdsc, ci);
fca65b4a 1370 return dirty;
76e3b390
SW
1371}
1372
a8599bd8
SW
1373/*
1374 * Add dirty inode to the flushing list. Assigned a seq number so we
1375 * can wait for caps to flush without starving.
cdc35f96 1376 *
be655596 1377 * Called under i_ceph_lock.
a8599bd8 1378 */
cdc35f96 1379static int __mark_caps_flushing(struct inode *inode,
a8599bd8
SW
1380 struct ceph_mds_session *session)
1381{
3d14c5d2 1382 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8 1383 struct ceph_inode_info *ci = ceph_inode(inode);
cdc35f96 1384 int flushing;
50b885b9 1385
cdc35f96 1386 BUG_ON(ci->i_dirty_caps == 0);
a8599bd8 1387 BUG_ON(list_empty(&ci->i_dirty_item));
cdc35f96
SW
1388
1389 flushing = ci->i_dirty_caps;
1390 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1391 ceph_cap_string(flushing),
1392 ceph_cap_string(ci->i_flushing_caps),
1393 ceph_cap_string(ci->i_flushing_caps | flushing));
1394 ci->i_flushing_caps |= flushing;
1395 ci->i_dirty_caps = 0;
afcdaea3 1396 dout(" inode %p now !dirty\n", inode);
cdc35f96 1397
a8599bd8 1398 spin_lock(&mdsc->cap_dirty_lock);
afcdaea3
SW
1399 list_del_init(&ci->i_dirty_item);
1400
1401 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
a8599bd8
SW
1402 if (list_empty(&ci->i_flushing_item)) {
1403 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1404 mdsc->num_cap_flushing++;
afcdaea3
SW
1405 dout(" inode %p now flushing seq %lld\n", inode,
1406 ci->i_cap_flush_seq);
1407 } else {
1408 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1409 dout(" inode %p now flushing (more) seq %lld\n", inode,
a8599bd8
SW
1410 ci->i_cap_flush_seq);
1411 }
1412 spin_unlock(&mdsc->cap_dirty_lock);
cdc35f96
SW
1413
1414 return flushing;
a8599bd8
SW
1415}
1416
5ecad6fd
SW
1417/*
1418 * try to invalidate mapping pages without blocking.
1419 */
5ecad6fd
SW
1420static int try_nonblocking_invalidate(struct inode *inode)
1421{
1422 struct ceph_inode_info *ci = ceph_inode(inode);
1423 u32 invalidating_gen = ci->i_rdcache_gen;
1424
be655596 1425 spin_unlock(&ci->i_ceph_lock);
5ecad6fd 1426 invalidate_mapping_pages(&inode->i_data, 0, -1);
be655596 1427 spin_lock(&ci->i_ceph_lock);
5ecad6fd 1428
18a38193 1429 if (inode->i_data.nrpages == 0 &&
5ecad6fd
SW
1430 invalidating_gen == ci->i_rdcache_gen) {
1431 /* success. */
1432 dout("try_nonblocking_invalidate %p success\n", inode);
cd045cb4
SW
1433 /* save any racing async invalidate some trouble */
1434 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
5ecad6fd
SW
1435 return 0;
1436 }
1437 dout("try_nonblocking_invalidate %p failed\n", inode);
1438 return -1;
1439}
1440
a8599bd8
SW
1441/*
1442 * Swiss army knife function to examine currently used and wanted
1443 * versus held caps. Release, flush, ack revoked caps to mds as
1444 * appropriate.
1445 *
1446 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1447 * cap release further.
1448 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1449 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1450 * further delay.
1451 */
1452void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1453 struct ceph_mds_session *session)
1454{
3d14c5d2
YS
1455 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1456 struct ceph_mds_client *mdsc = fsc->mdsc;
a8599bd8
SW
1457 struct inode *inode = &ci->vfs_inode;
1458 struct ceph_cap *cap;
1459 int file_wanted, used;
1460 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
cbd03635 1461 int issued, implemented, want, retain, revoking, flushing = 0;
a8599bd8
SW
1462 int mds = -1; /* keep track of how far we've gone through i_caps list
1463 to avoid an infinite loop on retry */
1464 struct rb_node *p;
1465 int tried_invalidate = 0;
1466 int delayed = 0, sent = 0, force_requeue = 0, num;
cbd03635 1467 int queue_invalidate = 0;
a8599bd8
SW
1468 int is_delayed = flags & CHECK_CAPS_NODELAY;
1469
1470 /* if we are unmounting, flush any unused caps immediately. */
1471 if (mdsc->stopping)
1472 is_delayed = 1;
1473
be655596 1474 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1475
1476 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1477 flags |= CHECK_CAPS_FLUSH;
1478
1479 /* flush snaps first time around only */
1480 if (!list_empty(&ci->i_cap_snaps))
e835124c 1481 __ceph_flush_snaps(ci, &session, 0);
a8599bd8
SW
1482 goto retry_locked;
1483retry:
be655596 1484 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1485retry_locked:
1486 file_wanted = __ceph_caps_file_wanted(ci);
1487 used = __ceph_caps_used(ci);
1488 want = file_wanted | used;
cbd03635
SW
1489 issued = __ceph_caps_issued(ci, &implemented);
1490 revoking = implemented & ~issued;
a8599bd8
SW
1491
1492 retain = want | CEPH_CAP_PIN;
1493 if (!mdsc->stopping && inode->i_nlink > 0) {
1494 if (want) {
1495 retain |= CEPH_CAP_ANY; /* be greedy */
1496 } else {
1497 retain |= CEPH_CAP_ANY_SHARED;
1498 /*
1499 * keep RD only if we didn't have the file open RW,
1500 * because then the mds would revoke it anyway to
1501 * journal max_size=0.
1502 */
1503 if (ci->i_max_size == 0)
1504 retain |= CEPH_CAP_ANY_RD;
1505 }
1506 }
1507
1508 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
cbd03635 1509 " issued %s revoking %s retain %s %s%s%s\n", inode,
a8599bd8
SW
1510 ceph_cap_string(file_wanted),
1511 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1512 ceph_cap_string(ci->i_flushing_caps),
cbd03635 1513 ceph_cap_string(issued), ceph_cap_string(revoking),
a8599bd8
SW
1514 ceph_cap_string(retain),
1515 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1516 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1517 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1518
1519 /*
1520 * If we no longer need to hold onto old our caps, and we may
1521 * have cached pages, but don't want them, then try to invalidate.
1522 * If we fail, it's because pages are locked.... try again later.
1523 */
1524 if ((!is_delayed || mdsc->stopping) &&
1525 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
93afd449 1526 inode->i_data.nrpages && /* have cached pages */
cbd03635 1527 (file_wanted == 0 || /* no open files */
2962507c
SW
1528 (revoking & (CEPH_CAP_FILE_CACHE|
1529 CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
a8599bd8 1530 !tried_invalidate) {
a8599bd8 1531 dout("check_caps trying to invalidate on %p\n", inode);
5ecad6fd 1532 if (try_nonblocking_invalidate(inode) < 0) {
2962507c
SW
1533 if (revoking & (CEPH_CAP_FILE_CACHE|
1534 CEPH_CAP_FILE_LAZYIO)) {
5ecad6fd
SW
1535 dout("check_caps queuing invalidate\n");
1536 queue_invalidate = 1;
1537 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1538 } else {
1539 dout("check_caps failed to invalidate pages\n");
1540 /* we failed to invalidate pages. check these
1541 caps again later. */
1542 force_requeue = 1;
1543 __cap_set_timeouts(mdsc, ci);
1544 }
a8599bd8
SW
1545 }
1546 tried_invalidate = 1;
1547 goto retry_locked;
1548 }
1549
1550 num = 0;
1551 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1552 cap = rb_entry(p, struct ceph_cap, ci_node);
1553 num++;
1554
1555 /* avoid looping forever */
1556 if (mds >= cap->mds ||
1557 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1558 continue;
1559
1560 /* NOTE: no side-effects allowed, until we take s_mutex */
1561
1562 revoking = cap->implemented & ~cap->issued;
088b3f5e
SW
1563 dout(" mds%d cap %p issued %s implemented %s revoking %s\n",
1564 cap->mds, cap, ceph_cap_string(cap->issued),
1565 ceph_cap_string(cap->implemented),
1566 ceph_cap_string(revoking));
a8599bd8
SW
1567
1568 if (cap == ci->i_auth_cap &&
1569 (cap->issued & CEPH_CAP_FILE_WR)) {
1570 /* request larger max_size from MDS? */
1571 if (ci->i_wanted_max_size > ci->i_max_size &&
1572 ci->i_wanted_max_size > ci->i_requested_max_size) {
1573 dout("requesting new max_size\n");
1574 goto ack;
1575 }
1576
1577 /* approaching file_max? */
1578 if ((inode->i_size << 1) >= ci->i_max_size &&
1579 (ci->i_reported_size << 1) < ci->i_max_size) {
1580 dout("i_size approaching max_size\n");
1581 goto ack;
1582 }
1583 }
1584 /* flush anything dirty? */
1585 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1586 ci->i_dirty_caps) {
1587 dout("flushing dirty caps\n");
1588 goto ack;
1589 }
1590
1591 /* completed revocation? going down and there are no caps? */
1592 if (revoking && (revoking & used) == 0) {
1593 dout("completed revocation of %s\n",
1594 ceph_cap_string(cap->implemented & ~cap->issued));
1595 goto ack;
1596 }
1597
1598 /* want more caps from mds? */
1599 if (want & ~(cap->mds_wanted | cap->issued))
1600 goto ack;
1601
1602 /* things we might delay */
1603 if ((cap->issued & ~retain) == 0 &&
1604 cap->mds_wanted == want)
1605 continue; /* nope, all good */
1606
1607 if (is_delayed)
1608 goto ack;
1609
1610 /* delay? */
1611 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1612 time_before(jiffies, ci->i_hold_caps_max)) {
1613 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1614 ceph_cap_string(cap->issued),
1615 ceph_cap_string(cap->issued & retain),
1616 ceph_cap_string(cap->mds_wanted),
1617 ceph_cap_string(want));
1618 delayed++;
1619 continue;
1620 }
1621
1622ack:
e9964c10
SW
1623 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1624 dout(" skipping %p I_NOFLUSH set\n", inode);
1625 continue;
1626 }
1627
a8599bd8
SW
1628 if (session && session != cap->session) {
1629 dout("oops, wrong session %p mutex\n", session);
1630 mutex_unlock(&session->s_mutex);
1631 session = NULL;
1632 }
1633 if (!session) {
1634 session = cap->session;
1635 if (mutex_trylock(&session->s_mutex) == 0) {
1636 dout("inverting session/ino locks on %p\n",
1637 session);
be655596 1638 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1639 if (took_snap_rwsem) {
1640 up_read(&mdsc->snap_rwsem);
1641 took_snap_rwsem = 0;
1642 }
1643 mutex_lock(&session->s_mutex);
1644 goto retry;
1645 }
1646 }
1647 /* take snap_rwsem after session mutex */
1648 if (!took_snap_rwsem) {
1649 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1650 dout("inverting snap/in locks on %p\n",
1651 inode);
be655596 1652 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1653 down_read(&mdsc->snap_rwsem);
1654 took_snap_rwsem = 1;
1655 goto retry;
1656 }
1657 took_snap_rwsem = 1;
1658 }
1659
cdc35f96
SW
1660 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1661 flushing = __mark_caps_flushing(inode, session);
24be0c48
SW
1662 else
1663 flushing = 0;
a8599bd8
SW
1664
1665 mds = cap->mds; /* remember mds, so we don't repeat */
1666 sent++;
1667
be655596 1668 /* __send_cap drops i_ceph_lock */
a8599bd8
SW
1669 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
1670 retain, flushing, NULL);
be655596 1671 goto retry; /* retake i_ceph_lock and restart our cap scan. */
a8599bd8
SW
1672 }
1673
1674 /*
1675 * Reschedule delayed caps release if we delayed anything,
1676 * otherwise cancel.
1677 */
1678 if (delayed && is_delayed)
1679 force_requeue = 1; /* __send_cap delayed release; requeue */
1680 if (!delayed && !is_delayed)
1681 __cap_delay_cancel(mdsc, ci);
1682 else if (!is_delayed || force_requeue)
1683 __cap_delay_requeue(mdsc, ci);
1684
be655596 1685 spin_unlock(&ci->i_ceph_lock);
a8599bd8 1686
cbd03635 1687 if (queue_invalidate)
3c6f6b79 1688 ceph_queue_invalidate(inode);
cbd03635 1689
cdc2ce05 1690 if (session)
a8599bd8
SW
1691 mutex_unlock(&session->s_mutex);
1692 if (took_snap_rwsem)
1693 up_read(&mdsc->snap_rwsem);
1694}
1695
a8599bd8
SW
1696/*
1697 * Try to flush dirty caps back to the auth mds.
1698 */
1699static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1700 unsigned *flush_tid)
1701{
3d14c5d2 1702 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8
SW
1703 struct ceph_inode_info *ci = ceph_inode(inode);
1704 int unlock_session = session ? 0 : 1;
1705 int flushing = 0;
1706
1707retry:
be655596 1708 spin_lock(&ci->i_ceph_lock);
e9964c10
SW
1709 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1710 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1711 goto out;
1712 }
a8599bd8
SW
1713 if (ci->i_dirty_caps && ci->i_auth_cap) {
1714 struct ceph_cap *cap = ci->i_auth_cap;
1715 int used = __ceph_caps_used(ci);
1716 int want = __ceph_caps_wanted(ci);
1717 int delayed;
1718
1719 if (!session) {
be655596 1720 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1721 session = cap->session;
1722 mutex_lock(&session->s_mutex);
1723 goto retry;
1724 }
1725 BUG_ON(session != cap->session);
1726 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1727 goto out;
1728
cdc35f96 1729 flushing = __mark_caps_flushing(inode, session);
a8599bd8 1730
be655596 1731 /* __send_cap drops i_ceph_lock */
a8599bd8
SW
1732 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1733 cap->issued | cap->implemented, flushing,
1734 flush_tid);
1735 if (!delayed)
1736 goto out_unlocked;
1737
be655596 1738 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1739 __cap_delay_requeue(mdsc, ci);
1740 }
1741out:
be655596 1742 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1743out_unlocked:
1744 if (session && unlock_session)
1745 mutex_unlock(&session->s_mutex);
1746 return flushing;
1747}
1748
1749/*
1750 * Return true if we've flushed caps through the given flush_tid.
1751 */
1752static int caps_are_flushed(struct inode *inode, unsigned tid)
1753{
1754 struct ceph_inode_info *ci = ceph_inode(inode);
a5ee751c 1755 int i, ret = 1;
a8599bd8 1756
be655596 1757 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1758 for (i = 0; i < CEPH_CAP_BITS; i++)
1759 if ((ci->i_flushing_caps & (1 << i)) &&
1760 ci->i_cap_flush_tid[i] <= tid) {
1761 /* still flushing this bit */
1762 ret = 0;
1763 break;
1764 }
be655596 1765 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1766 return ret;
1767}
1768
1769/*
1770 * Wait on any unsafe replies for the given inode. First wait on the
1771 * newest request, and make that the upper bound. Then, if there are
1772 * more requests, keep waiting on the oldest as long as it is still older
1773 * than the original request.
1774 */
1775static void sync_write_wait(struct inode *inode)
1776{
1777 struct ceph_inode_info *ci = ceph_inode(inode);
1778 struct list_head *head = &ci->i_unsafe_writes;
1779 struct ceph_osd_request *req;
1780 u64 last_tid;
1781
1782 spin_lock(&ci->i_unsafe_lock);
1783 if (list_empty(head))
1784 goto out;
1785
1786 /* set upper bound as _last_ entry in chain */
1787 req = list_entry(head->prev, struct ceph_osd_request,
1788 r_unsafe_item);
1789 last_tid = req->r_tid;
1790
1791 do {
1792 ceph_osdc_get_request(req);
1793 spin_unlock(&ci->i_unsafe_lock);
1794 dout("sync_write_wait on tid %llu (until %llu)\n",
1795 req->r_tid, last_tid);
1796 wait_for_completion(&req->r_safe_completion);
1797 spin_lock(&ci->i_unsafe_lock);
1798 ceph_osdc_put_request(req);
1799
1800 /*
1801 * from here on look at first entry in chain, since we
1802 * only want to wait for anything older than last_tid
1803 */
1804 if (list_empty(head))
1805 break;
1806 req = list_entry(head->next, struct ceph_osd_request,
1807 r_unsafe_item);
1808 } while (req->r_tid < last_tid);
1809out:
1810 spin_unlock(&ci->i_unsafe_lock);
1811}
1812
02c24a82 1813int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
a8599bd8 1814{
7ea80859 1815 struct inode *inode = file->f_mapping->host;
a8599bd8
SW
1816 struct ceph_inode_info *ci = ceph_inode(inode);
1817 unsigned flush_tid;
1818 int ret;
1819 int dirty;
1820
1821 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1822 sync_write_wait(inode);
1823
02c24a82 1824 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
a8599bd8
SW
1825 if (ret < 0)
1826 return ret;
02c24a82 1827 mutex_lock(&inode->i_mutex);
a8599bd8
SW
1828
1829 dirty = try_flush_caps(inode, NULL, &flush_tid);
1830 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1831
1832 /*
1833 * only wait on non-file metadata writeback (the mds
1834 * can recover size and mtime, so we don't need to
1835 * wait for that)
1836 */
1837 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1838 dout("fsync waiting for flush_tid %u\n", flush_tid);
1839 ret = wait_event_interruptible(ci->i_cap_wq,
1840 caps_are_flushed(inode, flush_tid));
1841 }
1842
1843 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
02c24a82 1844 mutex_unlock(&inode->i_mutex);
a8599bd8
SW
1845 return ret;
1846}
1847
1848/*
1849 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1850 * queue inode for flush but don't do so immediately, because we can
1851 * get by with fewer MDS messages if we wait for data writeback to
1852 * complete first.
1853 */
f1a3d572 1854int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
a8599bd8
SW
1855{
1856 struct ceph_inode_info *ci = ceph_inode(inode);
1857 unsigned flush_tid;
1858 int err = 0;
1859 int dirty;
f1a3d572 1860 int wait = wbc->sync_mode == WB_SYNC_ALL;
a8599bd8
SW
1861
1862 dout("write_inode %p wait=%d\n", inode, wait);
1863 if (wait) {
1864 dirty = try_flush_caps(inode, NULL, &flush_tid);
1865 if (dirty)
1866 err = wait_event_interruptible(ci->i_cap_wq,
1867 caps_are_flushed(inode, flush_tid));
1868 } else {
640ef79d 1869 struct ceph_mds_client *mdsc =
3d14c5d2 1870 ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8 1871
be655596 1872 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1873 if (__ceph_caps_dirty(ci))
1874 __cap_delay_requeue_front(mdsc, ci);
be655596 1875 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1876 }
1877 return err;
1878}
1879
1880/*
1881 * After a recovering MDS goes active, we need to resend any caps
1882 * we were flushing.
1883 *
1884 * Caller holds session->s_mutex.
1885 */
1886static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1887 struct ceph_mds_session *session)
1888{
1889 struct ceph_cap_snap *capsnap;
1890
1891 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1892 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1893 flushing_item) {
1894 struct ceph_inode_info *ci = capsnap->ci;
1895 struct inode *inode = &ci->vfs_inode;
1896 struct ceph_cap *cap;
1897
be655596 1898 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1899 cap = ci->i_auth_cap;
1900 if (cap && cap->session == session) {
1901 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1902 cap, capsnap);
e835124c 1903 __ceph_flush_snaps(ci, &session, 1);
a8599bd8
SW
1904 } else {
1905 pr_err("%p auth cap %p not mds%d ???\n", inode,
1906 cap, session->s_mds);
a8599bd8 1907 }
be655596 1908 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1909 }
1910}
1911
1912void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1913 struct ceph_mds_session *session)
1914{
1915 struct ceph_inode_info *ci;
1916
1917 kick_flushing_capsnaps(mdsc, session);
1918
1919 dout("kick_flushing_caps mds%d\n", session->s_mds);
1920 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1921 struct inode *inode = &ci->vfs_inode;
1922 struct ceph_cap *cap;
1923 int delayed = 0;
1924
be655596 1925 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
1926 cap = ci->i_auth_cap;
1927 if (cap && cap->session == session) {
1928 dout("kick_flushing_caps %p cap %p %s\n", inode,
1929 cap, ceph_cap_string(ci->i_flushing_caps));
1930 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1931 __ceph_caps_used(ci),
1932 __ceph_caps_wanted(ci),
1933 cap->issued | cap->implemented,
1934 ci->i_flushing_caps, NULL);
1935 if (delayed) {
be655596 1936 spin_lock(&ci->i_ceph_lock);
a8599bd8 1937 __cap_delay_requeue(mdsc, ci);
be655596 1938 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1939 }
1940 } else {
1941 pr_err("%p auth cap %p not mds%d ???\n", inode,
1942 cap, session->s_mds);
be655596 1943 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
1944 }
1945 }
1946}
1947
088b3f5e
SW
1948static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
1949 struct ceph_mds_session *session,
1950 struct inode *inode)
1951{
1952 struct ceph_inode_info *ci = ceph_inode(inode);
1953 struct ceph_cap *cap;
1954 int delayed = 0;
1955
be655596 1956 spin_lock(&ci->i_ceph_lock);
088b3f5e
SW
1957 cap = ci->i_auth_cap;
1958 dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode,
1959 ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq);
1960 __ceph_flush_snaps(ci, &session, 1);
1961 if (ci->i_flushing_caps) {
1962 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1963 __ceph_caps_used(ci),
1964 __ceph_caps_wanted(ci),
1965 cap->issued | cap->implemented,
1966 ci->i_flushing_caps, NULL);
1967 if (delayed) {
be655596 1968 spin_lock(&ci->i_ceph_lock);
088b3f5e 1969 __cap_delay_requeue(mdsc, ci);
be655596 1970 spin_unlock(&ci->i_ceph_lock);
088b3f5e
SW
1971 }
1972 } else {
be655596 1973 spin_unlock(&ci->i_ceph_lock);
088b3f5e
SW
1974 }
1975}
1976
a8599bd8
SW
1977
1978/*
1979 * Take references to capabilities we hold, so that we don't release
1980 * them to the MDS prematurely.
1981 *
be655596 1982 * Protected by i_ceph_lock.
a8599bd8
SW
1983 */
1984static void __take_cap_refs(struct ceph_inode_info *ci, int got)
1985{
1986 if (got & CEPH_CAP_PIN)
1987 ci->i_pin_ref++;
1988 if (got & CEPH_CAP_FILE_RD)
1989 ci->i_rd_ref++;
1990 if (got & CEPH_CAP_FILE_CACHE)
1991 ci->i_rdcache_ref++;
1992 if (got & CEPH_CAP_FILE_WR)
1993 ci->i_wr_ref++;
1994 if (got & CEPH_CAP_FILE_BUFFER) {
d3d0720d 1995 if (ci->i_wb_ref == 0)
3772d26d 1996 ihold(&ci->vfs_inode);
d3d0720d
HC
1997 ci->i_wb_ref++;
1998 dout("__take_cap_refs %p wb %d -> %d (?)\n",
1999 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
a8599bd8
SW
2000 }
2001}
2002
2003/*
2004 * Try to grab cap references. Specify those refs we @want, and the
2005 * minimal set we @need. Also include the larger offset we are writing
2006 * to (when applicable), and check against max_size here as well.
2007 * Note that caller is responsible for ensuring max_size increases are
2008 * requested from the MDS.
2009 */
2010static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2011 int *got, loff_t endoff, int *check_max, int *err)
2012{
2013 struct inode *inode = &ci->vfs_inode;
2014 int ret = 0;
2015 int have, implemented;
195d3ce2 2016 int file_wanted;
a8599bd8
SW
2017
2018 dout("get_cap_refs %p need %s want %s\n", inode,
2019 ceph_cap_string(need), ceph_cap_string(want));
be655596 2020 spin_lock(&ci->i_ceph_lock);
a8599bd8 2021
195d3ce2
SW
2022 /* make sure file is actually open */
2023 file_wanted = __ceph_caps_file_wanted(ci);
2024 if ((file_wanted & need) == 0) {
2025 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2026 ceph_cap_string(need), ceph_cap_string(file_wanted));
a8599bd8
SW
2027 *err = -EBADF;
2028 ret = 1;
2029 goto out;
2030 }
2031
2032 if (need & CEPH_CAP_FILE_WR) {
2033 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2034 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2035 inode, endoff, ci->i_max_size);
2036 if (endoff > ci->i_wanted_max_size) {
2037 *check_max = 1;
2038 ret = 1;
2039 }
2040 goto out;
2041 }
2042 /*
2043 * If a sync write is in progress, we must wait, so that we
2044 * can get a final snapshot value for size+mtime.
2045 */
2046 if (__ceph_have_pending_cap_snap(ci)) {
2047 dout("get_cap_refs %p cap_snap_pending\n", inode);
2048 goto out;
2049 }
2050 }
2051 have = __ceph_caps_issued(ci, &implemented);
2052
2053 /*
2054 * disallow writes while a truncate is pending
2055 */
2056 if (ci->i_truncate_pending)
2057 have &= ~CEPH_CAP_FILE_WR;
2058
2059 if ((have & need) == need) {
2060 /*
2061 * Look at (implemented & ~have & not) so that we keep waiting
2062 * on transition from wanted -> needed caps. This is needed
2063 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2064 * going before a prior buffered writeback happens.
2065 */
2066 int not = want & ~(have & need);
2067 int revoking = implemented & ~have;
2068 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2069 inode, ceph_cap_string(have), ceph_cap_string(not),
2070 ceph_cap_string(revoking));
2071 if ((revoking & not) == 0) {
2072 *got = need | (have & want);
2073 __take_cap_refs(ci, *got);
2074 ret = 1;
2075 }
2076 } else {
2077 dout("get_cap_refs %p have %s needed %s\n", inode,
2078 ceph_cap_string(have), ceph_cap_string(need));
2079 }
2080out:
be655596 2081 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2082 dout("get_cap_refs %p ret %d got %s\n", inode,
2083 ret, ceph_cap_string(*got));
2084 return ret;
2085}
2086
2087/*
2088 * Check the offset we are writing up to against our current
2089 * max_size. If necessary, tell the MDS we want to write to
2090 * a larger offset.
2091 */
2092static void check_max_size(struct inode *inode, loff_t endoff)
2093{
2094 struct ceph_inode_info *ci = ceph_inode(inode);
2095 int check = 0;
2096
2097 /* do we need to explicitly request a larger max_size? */
be655596 2098 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2099 if ((endoff >= ci->i_max_size ||
2100 endoff > (inode->i_size << 1)) &&
2101 endoff > ci->i_wanted_max_size) {
2102 dout("write %p at large endoff %llu, req max_size\n",
2103 inode, endoff);
2104 ci->i_wanted_max_size = endoff;
2105 check = 1;
2106 }
be655596 2107 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2108 if (check)
2109 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2110}
2111
2112/*
2113 * Wait for caps, and take cap references. If we can't get a WR cap
2114 * due to a small max_size, make sure we check_max_size (and possibly
2115 * ask the mds) so we don't get hung up indefinitely.
2116 */
2117int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2118 loff_t endoff)
2119{
2120 int check_max, ret, err;
2121
2122retry:
2123 if (endoff > 0)
2124 check_max_size(&ci->vfs_inode, endoff);
2125 check_max = 0;
2126 err = 0;
2127 ret = wait_event_interruptible(ci->i_cap_wq,
2128 try_get_cap_refs(ci, need, want,
2129 got, endoff,
2130 &check_max, &err));
2131 if (err)
2132 ret = err;
2133 if (check_max)
2134 goto retry;
2135 return ret;
2136}
2137
2138/*
2139 * Take cap refs. Caller must already know we hold at least one ref
2140 * on the caps in question or we don't know this is safe.
2141 */
2142void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2143{
be655596 2144 spin_lock(&ci->i_ceph_lock);
a8599bd8 2145 __take_cap_refs(ci, caps);
be655596 2146 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2147}
2148
2149/*
2150 * Release cap refs.
2151 *
2152 * If we released the last ref on any given cap, call ceph_check_caps
2153 * to release (or schedule a release).
2154 *
2155 * If we are releasing a WR cap (from a sync write), finalize any affected
2156 * cap_snap, and wake up any waiters.
2157 */
2158void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2159{
2160 struct inode *inode = &ci->vfs_inode;
2161 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2162 struct ceph_cap_snap *capsnap;
2163
be655596 2164 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2165 if (had & CEPH_CAP_PIN)
2166 --ci->i_pin_ref;
2167 if (had & CEPH_CAP_FILE_RD)
2168 if (--ci->i_rd_ref == 0)
2169 last++;
2170 if (had & CEPH_CAP_FILE_CACHE)
2171 if (--ci->i_rdcache_ref == 0)
2172 last++;
2173 if (had & CEPH_CAP_FILE_BUFFER) {
d3d0720d 2174 if (--ci->i_wb_ref == 0) {
a8599bd8
SW
2175 last++;
2176 put++;
2177 }
d3d0720d
HC
2178 dout("put_cap_refs %p wb %d -> %d (?)\n",
2179 inode, ci->i_wb_ref+1, ci->i_wb_ref);
a8599bd8
SW
2180 }
2181 if (had & CEPH_CAP_FILE_WR)
2182 if (--ci->i_wr_ref == 0) {
2183 last++;
2184 if (!list_empty(&ci->i_cap_snaps)) {
2185 capsnap = list_first_entry(&ci->i_cap_snaps,
2186 struct ceph_cap_snap,
2187 ci_item);
2188 if (capsnap->writing) {
2189 capsnap->writing = 0;
2190 flushsnaps =
2191 __ceph_finish_cap_snap(ci,
2192 capsnap);
2193 wake = 1;
2194 }
2195 }
2196 }
be655596 2197 spin_unlock(&ci->i_ceph_lock);
a8599bd8 2198
819ccbfa
SW
2199 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2200 last ? " last" : "", put ? " put" : "");
a8599bd8
SW
2201
2202 if (last && !flushsnaps)
2203 ceph_check_caps(ci, 0, NULL);
2204 else if (flushsnaps)
2205 ceph_flush_snaps(ci);
2206 if (wake)
03066f23 2207 wake_up_all(&ci->i_cap_wq);
a8599bd8
SW
2208 if (put)
2209 iput(inode);
2210}
2211
2212/*
2213 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2214 * context. Adjust per-snap dirty page accounting as appropriate.
2215 * Once all dirty data for a cap_snap is flushed, flush snapped file
2216 * metadata back to the MDS. If we dropped the last ref, call
2217 * ceph_check_caps.
2218 */
2219void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2220 struct ceph_snap_context *snapc)
2221{
2222 struct inode *inode = &ci->vfs_inode;
2223 int last = 0;
819ccbfa
SW
2224 int complete_capsnap = 0;
2225 int drop_capsnap = 0;
a8599bd8
SW
2226 int found = 0;
2227 struct ceph_cap_snap *capsnap = NULL;
2228
be655596 2229 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2230 ci->i_wrbuffer_ref -= nr;
2231 last = !ci->i_wrbuffer_ref;
2232
2233 if (ci->i_head_snapc == snapc) {
2234 ci->i_wrbuffer_ref_head -= nr;
7d8cb26d
SW
2235 if (ci->i_wrbuffer_ref_head == 0 &&
2236 ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
2237 BUG_ON(!ci->i_head_snapc);
a8599bd8
SW
2238 ceph_put_snap_context(ci->i_head_snapc);
2239 ci->i_head_snapc = NULL;
2240 }
2241 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2242 inode,
2243 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2244 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2245 last ? " LAST" : "");
2246 } else {
2247 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2248 if (capsnap->context == snapc) {
2249 found = 1;
a8599bd8
SW
2250 break;
2251 }
2252 }
2253 BUG_ON(!found);
819ccbfa
SW
2254 capsnap->dirty_pages -= nr;
2255 if (capsnap->dirty_pages == 0) {
2256 complete_capsnap = 1;
2257 if (capsnap->dirty == 0)
2258 /* cap writeback completed before we created
2259 * the cap_snap; no FLUSHSNAP is needed */
2260 drop_capsnap = 1;
2261 }
a8599bd8 2262 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
819ccbfa 2263 " snap %lld %d/%d -> %d/%d %s%s%s\n",
a8599bd8
SW
2264 inode, capsnap, capsnap->context->seq,
2265 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2266 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2267 last ? " (wrbuffer last)" : "",
819ccbfa
SW
2268 complete_capsnap ? " (complete capsnap)" : "",
2269 drop_capsnap ? " (drop capsnap)" : "");
2270 if (drop_capsnap) {
2271 ceph_put_snap_context(capsnap->context);
2272 list_del(&capsnap->ci_item);
2273 list_del(&capsnap->flushing_item);
2274 ceph_put_cap_snap(capsnap);
2275 }
a8599bd8
SW
2276 }
2277
be655596 2278 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2279
2280 if (last) {
2281 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2282 iput(inode);
819ccbfa 2283 } else if (complete_capsnap) {
a8599bd8 2284 ceph_flush_snaps(ci);
03066f23 2285 wake_up_all(&ci->i_cap_wq);
a8599bd8 2286 }
819ccbfa
SW
2287 if (drop_capsnap)
2288 iput(inode);
a8599bd8
SW
2289}
2290
2291/*
2292 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2293 * actually be a revocation if it specifies a smaller cap set.)
2294 *
be655596 2295 * caller holds s_mutex and i_ceph_lock, we drop both.
15637c8b 2296 *
a8599bd8
SW
2297 * return value:
2298 * 0 - ok
2299 * 1 - check_caps on auth cap only (writeback)
2300 * 2 - check_caps (ack revoke)
2301 */
15637c8b
SW
2302static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2303 struct ceph_mds_session *session,
2304 struct ceph_cap *cap,
2305 struct ceph_buffer *xattr_buf)
be655596 2306 __releases(ci->i_ceph_lock)
a8599bd8
SW
2307{
2308 struct ceph_inode_info *ci = ceph_inode(inode);
2309 int mds = session->s_mds;
2f56f56a 2310 int seq = le32_to_cpu(grant->seq);
a8599bd8
SW
2311 int newcaps = le32_to_cpu(grant->caps);
2312 int issued, implemented, used, wanted, dirty;
2313 u64 size = le64_to_cpu(grant->size);
2314 u64 max_size = le64_to_cpu(grant->max_size);
2315 struct timespec mtime, atime, ctime;
15637c8b 2316 int check_caps = 0;
a8599bd8
SW
2317 int wake = 0;
2318 int writeback = 0;
2319 int revoked_rdcache = 0;
3c6f6b79 2320 int queue_invalidate = 0;
a8599bd8 2321
2f56f56a
SW
2322 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2323 inode, cap, mds, seq, ceph_cap_string(newcaps));
a8599bd8
SW
2324 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2325 inode->i_size);
2326
2327 /*
2328 * If CACHE is being revoked, and we have no dirty buffers,
2329 * try to invalidate (once). (If there are dirty buffers, we
2330 * will invalidate _after_ writeback.)
2331 */
3b454c49
SW
2332 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2333 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
bcd2cbd1 2334 !ci->i_wrbuffer_ref) {
5ecad6fd
SW
2335 if (try_nonblocking_invalidate(inode) == 0) {
2336 revoked_rdcache = 1;
2337 } else {
a8599bd8
SW
2338 /* there were locked pages.. invalidate later
2339 in a separate thread. */
2340 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3c6f6b79 2341 queue_invalidate = 1;
a8599bd8
SW
2342 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2343 }
a8599bd8 2344 }
a8599bd8
SW
2345 }
2346
2347 /* side effects now are allowed */
2348
2349 issued = __ceph_caps_issued(ci, &implemented);
2350 issued |= implemented | __ceph_caps_dirty(ci);
2351
685f9a5d 2352 cap->cap_gen = session->s_cap_gen;
a8599bd8
SW
2353
2354 __check_cap_issue(ci, cap, newcaps);
2355
2356 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2357 inode->i_mode = le32_to_cpu(grant->mode);
2358 inode->i_uid = le32_to_cpu(grant->uid);
2359 inode->i_gid = le32_to_cpu(grant->gid);
2360 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2361 inode->i_uid, inode->i_gid);
2362 }
2363
2364 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
bfe86848 2365 set_nlink(inode, le32_to_cpu(grant->nlink));
a8599bd8
SW
2366
2367 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2368 int len = le32_to_cpu(grant->xattr_len);
2369 u64 version = le64_to_cpu(grant->xattr_version);
2370
2371 if (version > ci->i_xattrs.version) {
2372 dout(" got new xattrs v%llu on %p len %d\n",
2373 version, inode, len);
2374 if (ci->i_xattrs.blob)
2375 ceph_buffer_put(ci->i_xattrs.blob);
2376 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2377 ci->i_xattrs.version = version;
2378 }
2379 }
2380
2381 /* size/ctime/mtime/atime? */
2382 ceph_fill_file_size(inode, issued,
2383 le32_to_cpu(grant->truncate_seq),
2384 le64_to_cpu(grant->truncate_size), size);
2385 ceph_decode_timespec(&mtime, &grant->mtime);
2386 ceph_decode_timespec(&atime, &grant->atime);
2387 ceph_decode_timespec(&ctime, &grant->ctime);
2388 ceph_fill_file_time(inode, issued,
2389 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2390 &atime);
2391
2392 /* max size increase? */
5e62ad30 2393 if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
a8599bd8
SW
2394 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2395 ci->i_max_size = max_size;
2396 if (max_size >= ci->i_wanted_max_size) {
2397 ci->i_wanted_max_size = 0; /* reset */
2398 ci->i_requested_max_size = 0;
2399 }
2400 wake = 1;
2401 }
2402
2403 /* check cap bits */
2404 wanted = __ceph_caps_wanted(ci);
2405 used = __ceph_caps_used(ci);
2406 dirty = __ceph_caps_dirty(ci);
2407 dout(" my wanted = %s, used = %s, dirty %s\n",
2408 ceph_cap_string(wanted),
2409 ceph_cap_string(used),
2410 ceph_cap_string(dirty));
2411 if (wanted != le32_to_cpu(grant->wanted)) {
2412 dout("mds wanted %s -> %s\n",
2413 ceph_cap_string(le32_to_cpu(grant->wanted)),
2414 ceph_cap_string(wanted));
2415 grant->wanted = cpu_to_le32(wanted);
2416 }
2417
2418 cap->seq = seq;
2419
2420 /* file layout may have changed */
2421 ci->i_layout = grant->layout;
2422
2423 /* revocation, grant, or no-op? */
2424 if (cap->issued & ~newcaps) {
3b454c49
SW
2425 int revoking = cap->issued & ~newcaps;
2426
2427 dout("revocation: %s -> %s (revoking %s)\n",
2428 ceph_cap_string(cap->issued),
2429 ceph_cap_string(newcaps),
2430 ceph_cap_string(revoking));
0eb6cd49 2431 if (revoking & used & CEPH_CAP_FILE_BUFFER)
3b454c49
SW
2432 writeback = 1; /* initiate writeback; will delay ack */
2433 else if (revoking == CEPH_CAP_FILE_CACHE &&
2434 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2435 queue_invalidate)
2436 ; /* do nothing yet, invalidation will be queued */
2437 else if (cap == ci->i_auth_cap)
2438 check_caps = 1; /* check auth cap only */
2439 else
2440 check_caps = 2; /* check all caps */
a8599bd8 2441 cap->issued = newcaps;
978097c9 2442 cap->implemented |= newcaps;
a8599bd8
SW
2443 } else if (cap->issued == newcaps) {
2444 dout("caps unchanged: %s -> %s\n",
2445 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2446 } else {
2447 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2448 ceph_cap_string(newcaps));
2449 cap->issued = newcaps;
2450 cap->implemented |= newcaps; /* add bits only, to
2451 * avoid stepping on a
2452 * pending revocation */
2453 wake = 1;
2454 }
978097c9 2455 BUG_ON(cap->issued & ~cap->implemented);
a8599bd8 2456
be655596 2457 spin_unlock(&ci->i_ceph_lock);
3c6f6b79 2458 if (writeback)
a8599bd8
SW
2459 /*
2460 * queue inode for writeback: we can't actually call
2461 * filemap_write_and_wait, etc. from message handler
2462 * context.
2463 */
3c6f6b79
SW
2464 ceph_queue_writeback(inode);
2465 if (queue_invalidate)
2466 ceph_queue_invalidate(inode);
a8599bd8 2467 if (wake)
03066f23 2468 wake_up_all(&ci->i_cap_wq);
15637c8b
SW
2469
2470 if (check_caps == 1)
2471 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2472 session);
2473 else if (check_caps == 2)
2474 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2475 else
2476 mutex_unlock(&session->s_mutex);
a8599bd8
SW
2477}
2478
2479/*
2480 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2481 * MDS has been safely committed.
2482 */
6df058c0 2483static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
a8599bd8
SW
2484 struct ceph_mds_caps *m,
2485 struct ceph_mds_session *session,
2486 struct ceph_cap *cap)
be655596 2487 __releases(ci->i_ceph_lock)
a8599bd8
SW
2488{
2489 struct ceph_inode_info *ci = ceph_inode(inode);
3d14c5d2 2490 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd8
SW
2491 unsigned seq = le32_to_cpu(m->seq);
2492 int dirty = le32_to_cpu(m->dirty);
2493 int cleaned = 0;
afcdaea3 2494 int drop = 0;
a8599bd8
SW
2495 int i;
2496
2497 for (i = 0; i < CEPH_CAP_BITS; i++)
2498 if ((dirty & (1 << i)) &&
2499 flush_tid == ci->i_cap_flush_tid[i])
2500 cleaned |= 1 << i;
2501
2502 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2503 " flushing %s -> %s\n",
2504 inode, session->s_mds, seq, ceph_cap_string(dirty),
2505 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2506 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2507
2508 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2509 goto out;
2510
a8599bd8 2511 ci->i_flushing_caps &= ~cleaned;
a8599bd8
SW
2512
2513 spin_lock(&mdsc->cap_dirty_lock);
2514 if (ci->i_flushing_caps == 0) {
2515 list_del_init(&ci->i_flushing_item);
2516 if (!list_empty(&session->s_cap_flushing))
2517 dout(" mds%d still flushing cap on %p\n",
2518 session->s_mds,
2519 &list_entry(session->s_cap_flushing.next,
2520 struct ceph_inode_info,
2521 i_flushing_item)->vfs_inode);
2522 mdsc->num_cap_flushing--;
03066f23 2523 wake_up_all(&mdsc->cap_flushing_wq);
a8599bd8 2524 dout(" inode %p now !flushing\n", inode);
afcdaea3
SW
2525
2526 if (ci->i_dirty_caps == 0) {
2527 dout(" inode %p now clean\n", inode);
2528 BUG_ON(!list_empty(&ci->i_dirty_item));
2529 drop = 1;
7d8cb26d
SW
2530 if (ci->i_wrbuffer_ref_head == 0) {
2531 BUG_ON(!ci->i_head_snapc);
2532 ceph_put_snap_context(ci->i_head_snapc);
2533 ci->i_head_snapc = NULL;
2534 }
76e3b390
SW
2535 } else {
2536 BUG_ON(list_empty(&ci->i_dirty_item));
afcdaea3 2537 }
a8599bd8
SW
2538 }
2539 spin_unlock(&mdsc->cap_dirty_lock);
03066f23 2540 wake_up_all(&ci->i_cap_wq);
a8599bd8
SW
2541
2542out:
be655596 2543 spin_unlock(&ci->i_ceph_lock);
afcdaea3 2544 if (drop)
a8599bd8
SW
2545 iput(inode);
2546}
2547
2548/*
2549 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2550 * throw away our cap_snap.
2551 *
2552 * Caller hold s_mutex.
2553 */
6df058c0 2554static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
a8599bd8
SW
2555 struct ceph_mds_caps *m,
2556 struct ceph_mds_session *session)
2557{
2558 struct ceph_inode_info *ci = ceph_inode(inode);
2559 u64 follows = le64_to_cpu(m->snap_follows);
a8599bd8
SW
2560 struct ceph_cap_snap *capsnap;
2561 int drop = 0;
2562
2563 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2564 inode, ci, session->s_mds, follows);
2565
be655596 2566 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2567 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2568 if (capsnap->follows == follows) {
2569 if (capsnap->flush_tid != flush_tid) {
2570 dout(" cap_snap %p follows %lld tid %lld !="
2571 " %lld\n", capsnap, follows,
2572 flush_tid, capsnap->flush_tid);
2573 break;
2574 }
2575 WARN_ON(capsnap->dirty_pages || capsnap->writing);
819ccbfa
SW
2576 dout(" removing %p cap_snap %p follows %lld\n",
2577 inode, capsnap, follows);
a8599bd8
SW
2578 ceph_put_snap_context(capsnap->context);
2579 list_del(&capsnap->ci_item);
2580 list_del(&capsnap->flushing_item);
2581 ceph_put_cap_snap(capsnap);
2582 drop = 1;
2583 break;
2584 } else {
2585 dout(" skipping cap_snap %p follows %lld\n",
2586 capsnap, capsnap->follows);
2587 }
2588 }
be655596 2589 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2590 if (drop)
2591 iput(inode);
2592}
2593
2594/*
2595 * Handle TRUNC from MDS, indicating file truncation.
2596 *
2597 * caller hold s_mutex.
2598 */
2599static void handle_cap_trunc(struct inode *inode,
2600 struct ceph_mds_caps *trunc,
2601 struct ceph_mds_session *session)
be655596 2602 __releases(ci->i_ceph_lock)
a8599bd8
SW
2603{
2604 struct ceph_inode_info *ci = ceph_inode(inode);
2605 int mds = session->s_mds;
2606 int seq = le32_to_cpu(trunc->seq);
2607 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2608 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2609 u64 size = le64_to_cpu(trunc->size);
2610 int implemented = 0;
2611 int dirty = __ceph_caps_dirty(ci);
2612 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2613 int queue_trunc = 0;
2614
2615 issued |= implemented | dirty;
2616
2617 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2618 inode, mds, seq, truncate_size, truncate_seq);
2619 queue_trunc = ceph_fill_file_size(inode, issued,
2620 truncate_seq, truncate_size, size);
be655596 2621 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2622
2623 if (queue_trunc)
3c6f6b79 2624 ceph_queue_vmtruncate(inode);
a8599bd8
SW
2625}
2626
2627/*
2628 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2629 * different one. If we are the most recent migration we've seen (as
2630 * indicated by mseq), make note of the migrating cap bits for the
2631 * duration (until we see the corresponding IMPORT).
2632 *
2633 * caller holds s_mutex
2634 */
2635static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
154f42c2
SW
2636 struct ceph_mds_session *session,
2637 int *open_target_sessions)
a8599bd8 2638{
db354052 2639 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
a8599bd8
SW
2640 struct ceph_inode_info *ci = ceph_inode(inode);
2641 int mds = session->s_mds;
2642 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2643 struct ceph_cap *cap = NULL, *t;
2644 struct rb_node *p;
2645 int remember = 1;
2646
2647 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2648 inode, ci, mds, mseq);
2649
be655596 2650 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2651
2652 /* make sure we haven't seen a higher mseq */
2653 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2654 t = rb_entry(p, struct ceph_cap, ci_node);
2655 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2656 dout(" higher mseq on cap from mds%d\n",
2657 t->session->s_mds);
2658 remember = 0;
2659 }
2660 if (t->session->s_mds == mds)
2661 cap = t;
2662 }
2663
2664 if (cap) {
2665 if (remember) {
2666 /* make note */
2667 ci->i_cap_exporting_mds = mds;
2668 ci->i_cap_exporting_mseq = mseq;
2669 ci->i_cap_exporting_issued = cap->issued;
154f42c2
SW
2670
2671 /*
2672 * make sure we have open sessions with all possible
2673 * export targets, so that we get the matching IMPORT
2674 */
2675 *open_target_sessions = 1;
db354052
SW
2676
2677 /*
2678 * we can't flush dirty caps that we've seen the
2679 * EXPORT but no IMPORT for
2680 */
2681 spin_lock(&mdsc->cap_dirty_lock);
2682 if (!list_empty(&ci->i_dirty_item)) {
2683 dout(" moving %p to cap_dirty_migrating\n",
2684 inode);
2685 list_move(&ci->i_dirty_item,
2686 &mdsc->cap_dirty_migrating);
2687 }
2688 spin_unlock(&mdsc->cap_dirty_lock);
a8599bd8 2689 }
7c1332b8 2690 __ceph_remove_cap(cap);
a8599bd8 2691 }
4ea0043a 2692 /* else, we already released it */
a8599bd8 2693
be655596 2694 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2695}
2696
2697/*
2698 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2699 * clean them up.
2700 *
2701 * caller holds s_mutex.
2702 */
2703static void handle_cap_import(struct ceph_mds_client *mdsc,
2704 struct inode *inode, struct ceph_mds_caps *im,
2705 struct ceph_mds_session *session,
2706 void *snaptrace, int snaptrace_len)
2707{
2708 struct ceph_inode_info *ci = ceph_inode(inode);
2709 int mds = session->s_mds;
2710 unsigned issued = le32_to_cpu(im->caps);
2711 unsigned wanted = le32_to_cpu(im->wanted);
2712 unsigned seq = le32_to_cpu(im->seq);
2713 unsigned mseq = le32_to_cpu(im->migrate_seq);
2714 u64 realmino = le64_to_cpu(im->realm);
2715 u64 cap_id = le64_to_cpu(im->cap_id);
2716
2717 if (ci->i_cap_exporting_mds >= 0 &&
2718 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2719 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2720 " - cleared exporting from mds%d\n",
2721 inode, ci, mds, mseq,
2722 ci->i_cap_exporting_mds);
2723 ci->i_cap_exporting_issued = 0;
2724 ci->i_cap_exporting_mseq = 0;
2725 ci->i_cap_exporting_mds = -1;
db354052
SW
2726
2727 spin_lock(&mdsc->cap_dirty_lock);
2728 if (!list_empty(&ci->i_dirty_item)) {
2729 dout(" moving %p back to cap_dirty\n", inode);
2730 list_move(&ci->i_dirty_item, &mdsc->cap_dirty);
2731 }
2732 spin_unlock(&mdsc->cap_dirty_lock);
a8599bd8
SW
2733 } else {
2734 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2735 inode, ci, mds, mseq);
2736 }
2737
2738 down_write(&mdsc->snap_rwsem);
2739 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2740 false);
2741 downgrade_write(&mdsc->snap_rwsem);
2742 ceph_add_cap(inode, session, cap_id, -1,
2743 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2744 NULL /* no caps context */);
088b3f5e 2745 kick_flushing_inode_caps(mdsc, session, inode);
a8599bd8 2746 up_read(&mdsc->snap_rwsem);
feb4cc9b
SW
2747
2748 /* make sure we re-request max_size, if necessary */
be655596 2749 spin_lock(&ci->i_ceph_lock);
feb4cc9b 2750 ci->i_requested_max_size = 0;
be655596 2751 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2752}
2753
2754/*
2755 * Handle a caps message from the MDS.
2756 *
2757 * Identify the appropriate session, inode, and call the right handler
2758 * based on the cap op.
2759 */
2760void ceph_handle_caps(struct ceph_mds_session *session,
2761 struct ceph_msg *msg)
2762{
2763 struct ceph_mds_client *mdsc = session->s_mdsc;
3d14c5d2 2764 struct super_block *sb = mdsc->fsc->sb;
a8599bd8 2765 struct inode *inode;
be655596 2766 struct ceph_inode_info *ci;
a8599bd8
SW
2767 struct ceph_cap *cap;
2768 struct ceph_mds_caps *h;
2600d2dd 2769 int mds = session->s_mds;
a8599bd8 2770 int op;
3d7ded4d 2771 u32 seq, mseq;
a8599bd8
SW
2772 struct ceph_vino vino;
2773 u64 cap_id;
2774 u64 size, max_size;
6df058c0 2775 u64 tid;
70edb55b 2776 void *snaptrace;
ce1fbc8d
SW
2777 size_t snaptrace_len;
2778 void *flock;
2779 u32 flock_len;
154f42c2 2780 int open_target_sessions = 0;
a8599bd8
SW
2781
2782 dout("handle_caps from mds%d\n", mds);
2783
2784 /* decode */
6df058c0 2785 tid = le64_to_cpu(msg->hdr.tid);
a8599bd8
SW
2786 if (msg->front.iov_len < sizeof(*h))
2787 goto bad;
2788 h = msg->front.iov_base;
2789 op = le32_to_cpu(h->op);
2790 vino.ino = le64_to_cpu(h->ino);
2791 vino.snap = CEPH_NOSNAP;
2792 cap_id = le64_to_cpu(h->cap_id);
2793 seq = le32_to_cpu(h->seq);
3d7ded4d 2794 mseq = le32_to_cpu(h->migrate_seq);
a8599bd8
SW
2795 size = le64_to_cpu(h->size);
2796 max_size = le64_to_cpu(h->max_size);
2797
ce1fbc8d
SW
2798 snaptrace = h + 1;
2799 snaptrace_len = le32_to_cpu(h->snap_trace_len);
2800
2801 if (le16_to_cpu(msg->hdr.version) >= 2) {
2802 void *p, *end;
2803
2804 p = snaptrace + snaptrace_len;
2805 end = msg->front.iov_base + msg->front.iov_len;
2806 ceph_decode_32_safe(&p, end, flock_len, bad);
2807 flock = p;
2808 } else {
2809 flock = NULL;
2810 flock_len = 0;
2811 }
2812
a8599bd8
SW
2813 mutex_lock(&session->s_mutex);
2814 session->s_seq++;
2815 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2816 (unsigned)seq);
2817
2818 /* lookup ino */
2819 inode = ceph_find_inode(sb, vino);
be655596 2820 ci = ceph_inode(inode);
a8599bd8
SW
2821 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2822 vino.snap, inode);
2823 if (!inode) {
2824 dout(" i don't have ino %llx\n", vino.ino);
3d7ded4d
SW
2825
2826 if (op == CEPH_CAP_OP_IMPORT)
2827 __queue_cap_release(session, vino.ino, cap_id,
2828 mseq, seq);
21b559de 2829 goto flush_cap_releases;
a8599bd8
SW
2830 }
2831
2832 /* these will work even if we don't have a cap yet */
2833 switch (op) {
2834 case CEPH_CAP_OP_FLUSHSNAP_ACK:
6df058c0 2835 handle_cap_flushsnap_ack(inode, tid, h, session);
a8599bd8
SW
2836 goto done;
2837
2838 case CEPH_CAP_OP_EXPORT:
154f42c2 2839 handle_cap_export(inode, h, session, &open_target_sessions);
a8599bd8
SW
2840 goto done;
2841
2842 case CEPH_CAP_OP_IMPORT:
2843 handle_cap_import(mdsc, inode, h, session,
ce1fbc8d 2844 snaptrace, snaptrace_len);
7e57b81c 2845 ceph_check_caps(ceph_inode(inode), 0, session);
15637c8b 2846 goto done_unlocked;
a8599bd8
SW
2847 }
2848
2849 /* the rest require a cap */
be655596 2850 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2851 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2852 if (!cap) {
9dbd412f 2853 dout(" no cap on %p ino %llx.%llx from mds%d\n",
a8599bd8 2854 inode, ceph_ino(inode), ceph_snap(inode), mds);
be655596 2855 spin_unlock(&ci->i_ceph_lock);
21b559de 2856 goto flush_cap_releases;
a8599bd8
SW
2857 }
2858
be655596 2859 /* note that each of these drops i_ceph_lock for us */
a8599bd8
SW
2860 switch (op) {
2861 case CEPH_CAP_OP_REVOKE:
2862 case CEPH_CAP_OP_GRANT:
15637c8b
SW
2863 handle_cap_grant(inode, h, session, cap, msg->middle);
2864 goto done_unlocked;
a8599bd8
SW
2865
2866 case CEPH_CAP_OP_FLUSH_ACK:
6df058c0 2867 handle_cap_flush_ack(inode, tid, h, session, cap);
a8599bd8
SW
2868 break;
2869
2870 case CEPH_CAP_OP_TRUNC:
2871 handle_cap_trunc(inode, h, session);
2872 break;
2873
2874 default:
be655596 2875 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2876 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2877 ceph_cap_op_name(op));
2878 }
2879
21b559de
GF
2880 goto done;
2881
2882flush_cap_releases:
2883 /*
2884 * send any full release message to try to move things
2885 * along for the mds (who clearly thinks we still have this
2886 * cap).
2887 */
2888 ceph_add_cap_releases(mdsc, session);
2889 ceph_send_cap_releases(mdsc, session);
2890
a8599bd8 2891done:
15637c8b
SW
2892 mutex_unlock(&session->s_mutex);
2893done_unlocked:
a8599bd8
SW
2894 if (inode)
2895 iput(inode);
154f42c2
SW
2896 if (open_target_sessions)
2897 ceph_mdsc_open_export_target_sessions(mdsc, session);
a8599bd8
SW
2898 return;
2899
2900bad:
2901 pr_err("ceph_handle_caps: corrupt message\n");
9ec7cab1 2902 ceph_msg_dump(msg);
a8599bd8
SW
2903 return;
2904}
2905
2906/*
2907 * Delayed work handler to process end of delayed cap release LRU list.
2908 */
afcdaea3 2909void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
a8599bd8
SW
2910{
2911 struct ceph_inode_info *ci;
2912 int flags = CHECK_CAPS_NODELAY;
2913
a8599bd8
SW
2914 dout("check_delayed_caps\n");
2915 while (1) {
2916 spin_lock(&mdsc->cap_delay_lock);
2917 if (list_empty(&mdsc->cap_delay_list))
2918 break;
2919 ci = list_first_entry(&mdsc->cap_delay_list,
2920 struct ceph_inode_info,
2921 i_cap_delay_list);
2922 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2923 time_before(jiffies, ci->i_hold_caps_max))
2924 break;
2925 list_del_init(&ci->i_cap_delay_list);
2926 spin_unlock(&mdsc->cap_delay_lock);
2927 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2928 ceph_check_caps(ci, flags, NULL);
2929 }
2930 spin_unlock(&mdsc->cap_delay_lock);
2931}
2932
afcdaea3
SW
2933/*
2934 * Flush all dirty caps to the mds
2935 */
2936void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2937{
db354052
SW
2938 struct ceph_inode_info *ci;
2939 struct inode *inode;
afcdaea3
SW
2940
2941 dout("flush_dirty_caps\n");
2942 spin_lock(&mdsc->cap_dirty_lock);
db354052
SW
2943 while (!list_empty(&mdsc->cap_dirty)) {
2944 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
2945 i_dirty_item);
70b666c3
SW
2946 inode = &ci->vfs_inode;
2947 ihold(inode);
db354052 2948 dout("flush_dirty_caps %p\n", inode);
afcdaea3 2949 spin_unlock(&mdsc->cap_dirty_lock);
70b666c3
SW
2950 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
2951 iput(inode);
afcdaea3
SW
2952 spin_lock(&mdsc->cap_dirty_lock);
2953 }
2954 spin_unlock(&mdsc->cap_dirty_lock);
db354052 2955 dout("flush_dirty_caps done\n");
afcdaea3
SW
2956}
2957
a8599bd8
SW
2958/*
2959 * Drop open file reference. If we were the last open file,
2960 * we may need to release capabilities to the MDS (or schedule
2961 * their delayed release).
2962 */
2963void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2964{
2965 struct inode *inode = &ci->vfs_inode;
2966 int last = 0;
2967
be655596 2968 spin_lock(&ci->i_ceph_lock);
a8599bd8
SW
2969 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2970 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2971 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2972 if (--ci->i_nr_by_mode[fmode] == 0)
2973 last++;
be655596 2974 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
2975
2976 if (last && ci->i_vino.snap == CEPH_NOSNAP)
2977 ceph_check_caps(ci, 0, NULL);
2978}
2979
2980/*
2981 * Helpers for embedding cap and dentry lease releases into mds
2982 * requests.
2983 *
2984 * @force is used by dentry_release (below) to force inclusion of a
2985 * record for the directory inode, even when there aren't any caps to
2986 * drop.
2987 */
2988int ceph_encode_inode_release(void **p, struct inode *inode,
2989 int mds, int drop, int unless, int force)
2990{
2991 struct ceph_inode_info *ci = ceph_inode(inode);
2992 struct ceph_cap *cap;
2993 struct ceph_mds_request_release *rel = *p;
ec97f88b 2994 int used, dirty;
a8599bd8 2995 int ret = 0;
a8599bd8 2996
be655596 2997 spin_lock(&ci->i_ceph_lock);
916623da 2998 used = __ceph_caps_used(ci);
ec97f88b 2999 dirty = __ceph_caps_dirty(ci);
916623da 3000
ec97f88b
SW
3001 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3002 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
916623da
SW
3003 ceph_cap_string(unless));
3004
ec97f88b
SW
3005 /* only drop unused, clean caps */
3006 drop &= ~(used | dirty);
916623da 3007
a8599bd8
SW
3008 cap = __get_cap_for_mds(ci, mds);
3009 if (cap && __cap_is_valid(cap)) {
3010 if (force ||
3011 ((cap->issued & drop) &&
3012 (cap->issued & unless) == 0)) {
3013 if ((cap->issued & drop) &&
3014 (cap->issued & unless) == 0) {
3015 dout("encode_inode_release %p cap %p %s -> "
3016 "%s\n", inode, cap,
3017 ceph_cap_string(cap->issued),
3018 ceph_cap_string(cap->issued & ~drop));
3019 cap->issued &= ~drop;
3020 cap->implemented &= ~drop;
3021 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
3022 int wanted = __ceph_caps_wanted(ci);
3023 dout(" wanted %s -> %s (act %s)\n",
3024 ceph_cap_string(cap->mds_wanted),
3025 ceph_cap_string(cap->mds_wanted &
3026 ~wanted),
3027 ceph_cap_string(wanted));
3028 cap->mds_wanted &= wanted;
3029 }
3030 } else {
3031 dout("encode_inode_release %p cap %p %s"
3032 " (force)\n", inode, cap,
3033 ceph_cap_string(cap->issued));
3034 }
3035
3036 rel->ino = cpu_to_le64(ceph_ino(inode));
3037 rel->cap_id = cpu_to_le64(cap->cap_id);
3038 rel->seq = cpu_to_le32(cap->seq);
3039 rel->issue_seq = cpu_to_le32(cap->issue_seq),
3040 rel->mseq = cpu_to_le32(cap->mseq);
3041 rel->caps = cpu_to_le32(cap->issued);
3042 rel->wanted = cpu_to_le32(cap->mds_wanted);
3043 rel->dname_len = 0;
3044 rel->dname_seq = 0;
3045 *p += sizeof(*rel);
3046 ret = 1;
3047 } else {
3048 dout("encode_inode_release %p cap %p %s\n",
3049 inode, cap, ceph_cap_string(cap->issued));
3050 }
3051 }
be655596 3052 spin_unlock(&ci->i_ceph_lock);
a8599bd8
SW
3053 return ret;
3054}
3055
3056int ceph_encode_dentry_release(void **p, struct dentry *dentry,
3057 int mds, int drop, int unless)
3058{
3059 struct inode *dir = dentry->d_parent->d_inode;
3060 struct ceph_mds_request_release *rel = *p;
3061 struct ceph_dentry_info *di = ceph_dentry(dentry);
3062 int force = 0;
3063 int ret;
3064
3065 /*
3066 * force an record for the directory caps if we have a dentry lease.
be655596 3067 * this is racy (can't take i_ceph_lock and d_lock together), but it
a8599bd8
SW
3068 * doesn't have to be perfect; the mds will revoke anything we don't
3069 * release.
3070 */
3071 spin_lock(&dentry->d_lock);
3072 if (di->lease_session && di->lease_session->s_mds == mds)
3073 force = 1;
3074 spin_unlock(&dentry->d_lock);
3075
3076 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
3077
3078 spin_lock(&dentry->d_lock);
3079 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
3080 dout("encode_dentry_release %p mds%d seq %d\n",
3081 dentry, mds, (int)di->lease_seq);
3082 rel->dname_len = cpu_to_le32(dentry->d_name.len);
3083 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3084 *p += dentry->d_name.len;
3085 rel->dname_seq = cpu_to_le32(di->lease_seq);
1dadcce3 3086 __ceph_mdsc_drop_dentry_lease(dentry);
a8599bd8
SW
3087 }
3088 spin_unlock(&dentry->d_lock);
3089 return ret;
3090}