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