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