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