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