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