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