blob: a2069b6680aed83eb0be0af7c584b4a619ae239a [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
Greg Farnum2bc50252010-06-30 12:44:34 -0700330struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
331{
332 struct ceph_cap *cap;
333
334 spin_lock(&ci->vfs_inode.i_lock);
335 cap = __get_cap_for_mds(ci, mds);
336 spin_unlock(&ci->vfs_inode.i_lock);
337 return cap;
338}
339
Sage Weila8599bd2009-10-06 11:31:12 -0700340/*
Sage Weil33caad32010-05-26 14:31:27 -0700341 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
Sage Weila8599bd2009-10-06 11:31:12 -0700342 */
Sage Weilca81f3f2010-06-10 14:21:36 -0700343static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
Sage Weila8599bd2009-10-06 11:31:12 -0700344{
345 struct ceph_cap *cap;
346 int mds = -1;
347 struct rb_node *p;
348
Sage Weil33caad32010-05-26 14:31:27 -0700349 /* prefer mds with WR|BUFFER|EXCL caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700350 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
351 cap = rb_entry(p, struct ceph_cap, ci_node);
352 mds = cap->mds;
Sage Weila8599bd2009-10-06 11:31:12 -0700353 if (cap->issued & (CEPH_CAP_FILE_WR |
354 CEPH_CAP_FILE_BUFFER |
355 CEPH_CAP_FILE_EXCL))
356 break;
357 }
358 return mds;
359}
360
361int ceph_get_cap_mds(struct inode *inode)
362{
363 int mds;
364 spin_lock(&inode->i_lock);
Sage Weilca81f3f2010-06-10 14:21:36 -0700365 mds = __ceph_get_cap_mds(ceph_inode(inode));
Sage Weila8599bd2009-10-06 11:31:12 -0700366 spin_unlock(&inode->i_lock);
367 return mds;
368}
369
370/*
371 * Called under i_lock.
372 */
373static void __insert_cap_node(struct ceph_inode_info *ci,
374 struct ceph_cap *new)
375{
376 struct rb_node **p = &ci->i_caps.rb_node;
377 struct rb_node *parent = NULL;
378 struct ceph_cap *cap = NULL;
379
380 while (*p) {
381 parent = *p;
382 cap = rb_entry(parent, struct ceph_cap, ci_node);
383 if (new->mds < cap->mds)
384 p = &(*p)->rb_left;
385 else if (new->mds > cap->mds)
386 p = &(*p)->rb_right;
387 else
388 BUG();
389 }
390
391 rb_link_node(&new->ci_node, parent, p);
392 rb_insert_color(&new->ci_node, &ci->i_caps);
393}
394
395/*
396 * (re)set cap hold timeouts, which control the delayed release
397 * of unused caps back to the MDS. Should be called on cap use.
398 */
399static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
400 struct ceph_inode_info *ci)
401{
Sage Weil6b805182009-10-27 11:50:50 -0700402 struct ceph_mount_args *ma = mdsc->client->mount_args;
Sage Weila8599bd2009-10-06 11:31:12 -0700403
404 ci->i_hold_caps_min = round_jiffies(jiffies +
405 ma->caps_wanted_delay_min * HZ);
406 ci->i_hold_caps_max = round_jiffies(jiffies +
407 ma->caps_wanted_delay_max * HZ);
408 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
409 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
410}
411
412/*
413 * (Re)queue cap at the end of the delayed cap release list.
414 *
415 * If I_FLUSH is set, leave the inode at the front of the list.
416 *
417 * Caller holds i_lock
418 * -> we take mdsc->cap_delay_lock
419 */
420static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
421 struct ceph_inode_info *ci)
422{
423 __cap_set_timeouts(mdsc, ci);
424 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
425 ci->i_ceph_flags, ci->i_hold_caps_max);
426 if (!mdsc->stopping) {
427 spin_lock(&mdsc->cap_delay_lock);
428 if (!list_empty(&ci->i_cap_delay_list)) {
429 if (ci->i_ceph_flags & CEPH_I_FLUSH)
430 goto no_change;
431 list_del_init(&ci->i_cap_delay_list);
432 }
433 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
434no_change:
435 spin_unlock(&mdsc->cap_delay_lock);
436 }
437}
438
439/*
440 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
441 * indicating we should send a cap message to flush dirty metadata
442 * asap, and move to the front of the delayed cap list.
443 */
444static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
445 struct ceph_inode_info *ci)
446{
447 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
448 spin_lock(&mdsc->cap_delay_lock);
449 ci->i_ceph_flags |= CEPH_I_FLUSH;
450 if (!list_empty(&ci->i_cap_delay_list))
451 list_del_init(&ci->i_cap_delay_list);
452 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
453 spin_unlock(&mdsc->cap_delay_lock);
454}
455
456/*
457 * Cancel delayed work on cap.
458 *
459 * Caller must hold i_lock.
460 */
461static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
462 struct ceph_inode_info *ci)
463{
464 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
465 if (list_empty(&ci->i_cap_delay_list))
466 return;
467 spin_lock(&mdsc->cap_delay_lock);
468 list_del_init(&ci->i_cap_delay_list);
469 spin_unlock(&mdsc->cap_delay_lock);
470}
471
472/*
473 * Common issue checks for add_cap, handle_cap_grant.
474 */
475static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
476 unsigned issued)
477{
478 unsigned had = __ceph_caps_issued(ci, NULL);
479
480 /*
481 * Each time we receive FILE_CACHE anew, we increment
482 * i_rdcache_gen.
483 */
Sage Weil29625072010-05-27 10:40:43 -0700484 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
485 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
Sage Weila8599bd2009-10-06 11:31:12 -0700486 ci->i_rdcache_gen++;
487
488 /*
489 * if we are newly issued FILE_SHARED, clear I_COMPLETE; we
490 * don't know what happened to this directory while we didn't
491 * have the cap.
492 */
493 if ((issued & CEPH_CAP_FILE_SHARED) &&
494 (had & CEPH_CAP_FILE_SHARED) == 0) {
495 ci->i_shared_gen++;
496 if (S_ISDIR(ci->vfs_inode.i_mode)) {
497 dout(" marking %p NOT complete\n", &ci->vfs_inode);
498 ci->i_ceph_flags &= ~CEPH_I_COMPLETE;
499 }
500 }
501}
502
503/*
504 * Add a capability under the given MDS session.
505 *
506 * Caller should hold session snap_rwsem (read) and s_mutex.
507 *
508 * @fmode is the open file mode, if we are opening a file, otherwise
509 * it is < 0. (This is so we can atomically add the cap and add an
510 * open file reference to it.)
511 */
512int ceph_add_cap(struct inode *inode,
513 struct ceph_mds_session *session, u64 cap_id,
514 int fmode, unsigned issued, unsigned wanted,
515 unsigned seq, unsigned mseq, u64 realmino, int flags,
516 struct ceph_cap_reservation *caps_reservation)
517{
518 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
519 struct ceph_inode_info *ci = ceph_inode(inode);
520 struct ceph_cap *new_cap = NULL;
521 struct ceph_cap *cap;
522 int mds = session->s_mds;
523 int actual_wanted;
524
525 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
526 session->s_mds, cap_id, ceph_cap_string(issued), seq);
527
528 /*
529 * If we are opening the file, include file mode wanted bits
530 * in wanted.
531 */
532 if (fmode >= 0)
533 wanted |= ceph_caps_for_mode(fmode);
534
535retry:
536 spin_lock(&inode->i_lock);
537 cap = __get_cap_for_mds(ci, mds);
538 if (!cap) {
539 if (new_cap) {
540 cap = new_cap;
541 new_cap = NULL;
542 } else {
543 spin_unlock(&inode->i_lock);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700544 new_cap = get_cap(mdsc, caps_reservation);
Sage Weila8599bd2009-10-06 11:31:12 -0700545 if (new_cap == NULL)
546 return -ENOMEM;
547 goto retry;
548 }
549
550 cap->issued = 0;
551 cap->implemented = 0;
552 cap->mds = mds;
553 cap->mds_wanted = 0;
554
555 cap->ci = ci;
556 __insert_cap_node(ci, cap);
557
558 /* clear out old exporting info? (i.e. on cap import) */
559 if (ci->i_cap_exporting_mds == mds) {
560 ci->i_cap_exporting_issued = 0;
561 ci->i_cap_exporting_mseq = 0;
562 ci->i_cap_exporting_mds = -1;
563 }
564
565 /* add to session cap list */
566 cap->session = session;
567 spin_lock(&session->s_cap_lock);
568 list_add_tail(&cap->session_caps, &session->s_caps);
569 session->s_nr_caps++;
570 spin_unlock(&session->s_cap_lock);
571 }
572
573 if (!ci->i_snap_realm) {
574 /*
575 * add this inode to the appropriate snap realm
576 */
577 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
578 realmino);
579 if (realm) {
580 ceph_get_snap_realm(mdsc, realm);
581 spin_lock(&realm->inodes_with_caps_lock);
582 ci->i_snap_realm = realm;
583 list_add(&ci->i_snap_realm_item,
584 &realm->inodes_with_caps);
585 spin_unlock(&realm->inodes_with_caps_lock);
586 } else {
587 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
588 realmino);
Sage Weilb8cd07e2010-07-16 12:00:02 -0700589 WARN_ON(!realm);
Sage Weila8599bd2009-10-06 11:31:12 -0700590 }
591 }
592
593 __check_cap_issue(ci, cap, issued);
594
595 /*
596 * If we are issued caps we don't want, or the mds' wanted
597 * value appears to be off, queue a check so we'll release
598 * later and/or update the mds wanted value.
599 */
600 actual_wanted = __ceph_caps_wanted(ci);
601 if ((wanted & ~actual_wanted) ||
602 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
603 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
604 ceph_cap_string(issued), ceph_cap_string(wanted),
605 ceph_cap_string(actual_wanted));
606 __cap_delay_requeue(mdsc, ci);
607 }
608
609 if (flags & CEPH_CAP_FLAG_AUTH)
610 ci->i_auth_cap = cap;
611 else if (ci->i_auth_cap == cap)
612 ci->i_auth_cap = NULL;
613
614 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
615 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
616 ceph_cap_string(issued|cap->issued), seq, mds);
617 cap->cap_id = cap_id;
618 cap->issued = issued;
619 cap->implemented |= issued;
620 cap->mds_wanted |= wanted;
621 cap->seq = seq;
622 cap->issue_seq = seq;
623 cap->mseq = mseq;
Sage Weil685f9a5d2009-11-09 12:05:48 -0800624 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700625
626 if (fmode >= 0)
627 __ceph_get_fmode(ci, fmode);
628 spin_unlock(&inode->i_lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700629 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -0700630 return 0;
631}
632
633/*
634 * Return true if cap has not timed out and belongs to the current
635 * generation of the MDS session (i.e. has not gone 'stale' due to
636 * us losing touch with the mds).
637 */
638static int __cap_is_valid(struct ceph_cap *cap)
639{
640 unsigned long ttl;
Sage Weilcdac8302009-11-10 16:02:23 -0800641 u32 gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700642
643 spin_lock(&cap->session->s_cap_lock);
644 gen = cap->session->s_cap_gen;
645 ttl = cap->session->s_cap_ttl;
646 spin_unlock(&cap->session->s_cap_lock);
647
Sage Weil685f9a5d2009-11-09 12:05:48 -0800648 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
Sage Weila8599bd2009-10-06 11:31:12 -0700649 dout("__cap_is_valid %p cap %p issued %s "
650 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
Sage Weil685f9a5d2009-11-09 12:05:48 -0800651 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
Sage Weila8599bd2009-10-06 11:31:12 -0700652 return 0;
653 }
654
655 return 1;
656}
657
658/*
659 * Return set of valid cap bits issued to us. Note that caps time
660 * out, and may be invalidated in bulk if the client session times out
661 * and session->s_cap_gen is bumped.
662 */
663int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
664{
Sage Weil7af8f1e2010-03-01 15:17:34 -0800665 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
Sage Weila8599bd2009-10-06 11:31:12 -0700666 struct ceph_cap *cap;
667 struct rb_node *p;
668
669 if (implemented)
670 *implemented = 0;
671 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
672 cap = rb_entry(p, struct ceph_cap, ci_node);
673 if (!__cap_is_valid(cap))
674 continue;
675 dout("__ceph_caps_issued %p cap %p issued %s\n",
676 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
677 have |= cap->issued;
678 if (implemented)
679 *implemented |= cap->implemented;
680 }
681 return have;
682}
683
684/*
685 * Get cap bits issued by caps other than @ocap
686 */
687int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
688{
689 int have = ci->i_snap_caps;
690 struct ceph_cap *cap;
691 struct rb_node *p;
692
693 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
694 cap = rb_entry(p, struct ceph_cap, ci_node);
695 if (cap == ocap)
696 continue;
697 if (!__cap_is_valid(cap))
698 continue;
699 have |= cap->issued;
700 }
701 return have;
702}
703
704/*
705 * Move a cap to the end of the LRU (oldest caps at list head, newest
706 * at list tail).
707 */
708static void __touch_cap(struct ceph_cap *cap)
709{
710 struct ceph_mds_session *s = cap->session;
711
Sage Weila8599bd2009-10-06 11:31:12 -0700712 spin_lock(&s->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800713 if (s->s_cap_iterator == NULL) {
Sage Weil5dacf092009-12-21 20:40:34 -0800714 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
715 s->s_mds);
716 list_move_tail(&cap->session_caps, &s->s_caps);
717 } else {
718 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
719 &cap->ci->vfs_inode, cap, s->s_mds);
720 }
Sage Weila8599bd2009-10-06 11:31:12 -0700721 spin_unlock(&s->s_cap_lock);
722}
723
724/*
725 * Check if we hold the given mask. If so, move the cap(s) to the
726 * front of their respective LRUs. (This is the preferred way for
727 * callers to check for caps they want.)
728 */
729int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
730{
731 struct ceph_cap *cap;
732 struct rb_node *p;
733 int have = ci->i_snap_caps;
734
735 if ((have & mask) == mask) {
736 dout("__ceph_caps_issued_mask %p snap issued %s"
737 " (mask %s)\n", &ci->vfs_inode,
738 ceph_cap_string(have),
739 ceph_cap_string(mask));
740 return 1;
741 }
742
743 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
744 cap = rb_entry(p, struct ceph_cap, ci_node);
745 if (!__cap_is_valid(cap))
746 continue;
747 if ((cap->issued & mask) == mask) {
748 dout("__ceph_caps_issued_mask %p cap %p issued %s"
749 " (mask %s)\n", &ci->vfs_inode, cap,
750 ceph_cap_string(cap->issued),
751 ceph_cap_string(mask));
752 if (touch)
753 __touch_cap(cap);
754 return 1;
755 }
756
757 /* does a combination of caps satisfy mask? */
758 have |= cap->issued;
759 if ((have & mask) == mask) {
760 dout("__ceph_caps_issued_mask %p combo issued %s"
761 " (mask %s)\n", &ci->vfs_inode,
762 ceph_cap_string(cap->issued),
763 ceph_cap_string(mask));
764 if (touch) {
765 struct rb_node *q;
766
767 /* touch this + preceeding caps */
768 __touch_cap(cap);
769 for (q = rb_first(&ci->i_caps); q != p;
770 q = rb_next(q)) {
771 cap = rb_entry(q, struct ceph_cap,
772 ci_node);
773 if (!__cap_is_valid(cap))
774 continue;
775 __touch_cap(cap);
776 }
777 }
778 return 1;
779 }
780 }
781
782 return 0;
783}
784
785/*
786 * Return true if mask caps are currently being revoked by an MDS.
787 */
788int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
789{
790 struct inode *inode = &ci->vfs_inode;
791 struct ceph_cap *cap;
792 struct rb_node *p;
793 int ret = 0;
794
795 spin_lock(&inode->i_lock);
796 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
797 cap = rb_entry(p, struct ceph_cap, ci_node);
798 if (__cap_is_valid(cap) &&
799 (cap->implemented & ~cap->issued & mask)) {
800 ret = 1;
801 break;
802 }
803 }
804 spin_unlock(&inode->i_lock);
805 dout("ceph_caps_revoking %p %s = %d\n", inode,
806 ceph_cap_string(mask), ret);
807 return ret;
808}
809
810int __ceph_caps_used(struct ceph_inode_info *ci)
811{
812 int used = 0;
813 if (ci->i_pin_ref)
814 used |= CEPH_CAP_PIN;
815 if (ci->i_rd_ref)
816 used |= CEPH_CAP_FILE_RD;
817 if (ci->i_rdcache_ref || ci->i_rdcache_gen)
818 used |= CEPH_CAP_FILE_CACHE;
819 if (ci->i_wr_ref)
820 used |= CEPH_CAP_FILE_WR;
821 if (ci->i_wrbuffer_ref)
822 used |= CEPH_CAP_FILE_BUFFER;
823 return used;
824}
825
826/*
827 * wanted, by virtue of open file modes
828 */
829int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
830{
831 int want = 0;
832 int mode;
Sage Weil33caad32010-05-26 14:31:27 -0700833 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
Sage Weila8599bd2009-10-06 11:31:12 -0700834 if (ci->i_nr_by_mode[mode])
835 want |= ceph_caps_for_mode(mode);
836 return want;
837}
838
839/*
840 * Return caps we have registered with the MDS(s) as 'wanted'.
841 */
842int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
843{
844 struct ceph_cap *cap;
845 struct rb_node *p;
846 int mds_wanted = 0;
847
848 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
849 cap = rb_entry(p, struct ceph_cap, ci_node);
850 if (!__cap_is_valid(cap))
851 continue;
852 mds_wanted |= cap->mds_wanted;
853 }
854 return mds_wanted;
855}
856
857/*
858 * called under i_lock
859 */
860static int __ceph_is_any_caps(struct ceph_inode_info *ci)
861{
862 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
863}
864
865/*
Sage Weilf818a732010-05-11 20:56:31 -0700866 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
867 *
Sage Weila6369742010-02-22 13:59:00 -0800868 * caller should hold i_lock.
869 * caller will not hold session s_mutex if called from destroy_inode.
Sage Weila8599bd2009-10-06 11:31:12 -0700870 */
Sage Weil7c1332b2010-02-16 11:39:45 -0800871void __ceph_remove_cap(struct ceph_cap *cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700872{
873 struct ceph_mds_session *session = cap->session;
874 struct ceph_inode_info *ci = cap->ci;
Cheng Renquan640ef792010-03-26 17:40:33 +0800875 struct ceph_mds_client *mdsc =
876 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weilf818a732010-05-11 20:56:31 -0700877 int removed = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700878
879 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
880
Sage Weil7c1332b2010-02-16 11:39:45 -0800881 /* remove from session list */
882 spin_lock(&session->s_cap_lock);
883 if (session->s_cap_iterator == cap) {
884 /* not yet, we are iterating over this very cap */
885 dout("__ceph_remove_cap delaying %p removal from session %p\n",
886 cap, cap->session);
887 } else {
888 list_del_init(&cap->session_caps);
889 session->s_nr_caps--;
890 cap->session = NULL;
Sage Weilf818a732010-05-11 20:56:31 -0700891 removed = 1;
Sage Weil7c1332b2010-02-16 11:39:45 -0800892 }
Sage Weilf818a732010-05-11 20:56:31 -0700893 /* protect backpointer with s_cap_lock: see iterate_session_caps */
894 cap->ci = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -0800895 spin_unlock(&session->s_cap_lock);
896
Sage Weilf818a732010-05-11 20:56:31 -0700897 /* remove from inode list */
898 rb_erase(&cap->ci_node, &ci->i_caps);
899 if (ci->i_auth_cap == cap)
900 ci->i_auth_cap = NULL;
901
902 if (removed)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700903 ceph_put_cap(mdsc, cap);
Sage Weila8599bd2009-10-06 11:31:12 -0700904
905 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
906 struct ceph_snap_realm *realm = ci->i_snap_realm;
907 spin_lock(&realm->inodes_with_caps_lock);
908 list_del_init(&ci->i_snap_realm_item);
909 ci->i_snap_realm_counter++;
910 ci->i_snap_realm = NULL;
911 spin_unlock(&realm->inodes_with_caps_lock);
912 ceph_put_snap_realm(mdsc, realm);
913 }
914 if (!__ceph_is_any_real_caps(ci))
915 __cap_delay_cancel(mdsc, ci);
916}
917
918/*
919 * Build and send a cap message to the given MDS.
920 *
921 * Caller should be holding s_mutex.
922 */
923static int send_cap_msg(struct ceph_mds_session *session,
924 u64 ino, u64 cid, int op,
925 int caps, int wanted, int dirty,
926 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
927 u64 size, u64 max_size,
928 struct timespec *mtime, struct timespec *atime,
929 u64 time_warp_seq,
930 uid_t uid, gid_t gid, mode_t mode,
931 u64 xattr_version,
932 struct ceph_buffer *xattrs_buf,
933 u64 follows)
934{
935 struct ceph_mds_caps *fc;
936 struct ceph_msg *msg;
937
938 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
939 " seq %u/%u mseq %u follows %lld size %llu/%llu"
940 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
941 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
942 ceph_cap_string(dirty),
943 seq, issue_seq, mseq, follows, size, max_size,
944 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
945
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700946 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700947 if (!msg)
948 return -ENOMEM;
Sage Weila8599bd2009-10-06 11:31:12 -0700949
Sage Weil6df058c2009-12-22 11:24:33 -0800950 msg->hdr.tid = cpu_to_le64(flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -0700951
Sage Weil6df058c2009-12-22 11:24:33 -0800952 fc = msg->front.iov_base;
Sage Weila8599bd2009-10-06 11:31:12 -0700953 memset(fc, 0, sizeof(*fc));
954
955 fc->cap_id = cpu_to_le64(cid);
956 fc->op = cpu_to_le32(op);
957 fc->seq = cpu_to_le32(seq);
Sage Weila8599bd2009-10-06 11:31:12 -0700958 fc->issue_seq = cpu_to_le32(issue_seq);
959 fc->migrate_seq = cpu_to_le32(mseq);
960 fc->caps = cpu_to_le32(caps);
961 fc->wanted = cpu_to_le32(wanted);
962 fc->dirty = cpu_to_le32(dirty);
963 fc->ino = cpu_to_le64(ino);
964 fc->snap_follows = cpu_to_le64(follows);
965
966 fc->size = cpu_to_le64(size);
967 fc->max_size = cpu_to_le64(max_size);
968 if (mtime)
969 ceph_encode_timespec(&fc->mtime, mtime);
970 if (atime)
971 ceph_encode_timespec(&fc->atime, atime);
972 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
973
974 fc->uid = cpu_to_le32(uid);
975 fc->gid = cpu_to_le32(gid);
976 fc->mode = cpu_to_le32(mode);
977
978 fc->xattr_version = cpu_to_le64(xattr_version);
979 if (xattrs_buf) {
980 msg->middle = ceph_buffer_get(xattrs_buf);
981 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
982 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
983 }
984
985 ceph_con_send(&session->s_con, msg);
986 return 0;
987}
988
Sage Weil3d7ded42010-06-09 16:47:10 -0700989static void __queue_cap_release(struct ceph_mds_session *session,
990 u64 ino, u64 cap_id, u32 migrate_seq,
991 u32 issue_seq)
992{
993 struct ceph_msg *msg;
994 struct ceph_mds_cap_release *head;
995 struct ceph_mds_cap_item *item;
996
997 spin_lock(&session->s_cap_lock);
998 BUG_ON(!session->s_num_cap_releases);
999 msg = list_first_entry(&session->s_cap_releases,
1000 struct ceph_msg, list_head);
1001
1002 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1003 ino, session->s_mds, msg, session->s_num_cap_releases);
1004
1005 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1006 head = msg->front.iov_base;
1007 head->num = cpu_to_le32(le32_to_cpu(head->num) + 1);
1008 item = msg->front.iov_base + msg->front.iov_len;
1009 item->ino = cpu_to_le64(ino);
1010 item->cap_id = cpu_to_le64(cap_id);
1011 item->migrate_seq = cpu_to_le32(migrate_seq);
1012 item->seq = cpu_to_le32(issue_seq);
1013
1014 session->s_num_cap_releases--;
1015
1016 msg->front.iov_len += sizeof(*item);
1017 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1018 dout(" release msg %p full\n", msg);
1019 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1020 } else {
1021 dout(" release msg %p at %d/%d (%d)\n", msg,
1022 (int)le32_to_cpu(head->num),
1023 (int)CEPH_CAPS_PER_RELEASE,
1024 (int)msg->front.iov_len);
1025 }
1026 spin_unlock(&session->s_cap_lock);
1027}
1028
Sage Weila8599bd2009-10-06 11:31:12 -07001029/*
Sage Weila6369742010-02-22 13:59:00 -08001030 * Queue cap releases when an inode is dropped from our cache. Since
1031 * inode is about to be destroyed, there is no need for i_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001032 */
1033void ceph_queue_caps_release(struct inode *inode)
1034{
1035 struct ceph_inode_info *ci = ceph_inode(inode);
1036 struct rb_node *p;
1037
Sage Weila8599bd2009-10-06 11:31:12 -07001038 p = rb_first(&ci->i_caps);
1039 while (p) {
1040 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1041 struct ceph_mds_session *session = cap->session;
Sage Weila8599bd2009-10-06 11:31:12 -07001042
Sage Weil3d7ded42010-06-09 16:47:10 -07001043 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1044 cap->mseq, cap->issue_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07001045 p = rb_next(p);
Sage Weil7c1332b2010-02-16 11:39:45 -08001046 __ceph_remove_cap(cap);
Sage Weila8599bd2009-10-06 11:31:12 -07001047 }
Sage Weila8599bd2009-10-06 11:31:12 -07001048}
1049
1050/*
1051 * Send a cap msg on the given inode. Update our caps state, then
1052 * drop i_lock and send the message.
1053 *
1054 * Make note of max_size reported/requested from mds, revoked caps
1055 * that have now been implemented.
1056 *
1057 * Make half-hearted attempt ot to invalidate page cache if we are
1058 * dropping RDCACHE. Note that this will leave behind locked pages
1059 * that we'll then need to deal with elsewhere.
1060 *
1061 * Return non-zero if delayed release, or we experienced an error
1062 * such that the caller should requeue + retry later.
1063 *
1064 * called with i_lock, then drops it.
1065 * caller should hold snap_rwsem (read), s_mutex.
1066 */
1067static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1068 int op, int used, int want, int retain, int flushing,
1069 unsigned *pflush_tid)
1070 __releases(cap->ci->vfs_inode->i_lock)
1071{
1072 struct ceph_inode_info *ci = cap->ci;
1073 struct inode *inode = &ci->vfs_inode;
1074 u64 cap_id = cap->cap_id;
Sage Weil68c28322010-02-09 13:41:47 -08001075 int held, revoking, dropping, keep;
Sage Weila8599bd2009-10-06 11:31:12 -07001076 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1077 u64 size, max_size;
1078 struct timespec mtime, atime;
1079 int wake = 0;
1080 mode_t mode;
1081 uid_t uid;
1082 gid_t gid;
1083 struct ceph_mds_session *session;
1084 u64 xattr_version = 0;
Sage Weil082afec2010-08-22 15:16:41 -07001085 struct ceph_buffer *xattr_blob = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07001086 int delayed = 0;
1087 u64 flush_tid = 0;
1088 int i;
1089 int ret;
1090
Sage Weil68c28322010-02-09 13:41:47 -08001091 held = cap->issued | cap->implemented;
1092 revoking = cap->implemented & ~cap->issued;
1093 retain &= ~revoking;
1094 dropping = cap->issued & ~retain;
1095
Sage Weila8599bd2009-10-06 11:31:12 -07001096 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1097 inode, cap, cap->session,
1098 ceph_cap_string(held), ceph_cap_string(held & retain),
1099 ceph_cap_string(revoking));
1100 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1101
1102 session = cap->session;
1103
1104 /* don't release wanted unless we've waited a bit. */
1105 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1106 time_before(jiffies, ci->i_hold_caps_min)) {
1107 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1108 ceph_cap_string(cap->issued),
1109 ceph_cap_string(cap->issued & retain),
1110 ceph_cap_string(cap->mds_wanted),
1111 ceph_cap_string(want));
1112 want |= cap->mds_wanted;
1113 retain |= cap->issued;
1114 delayed = 1;
1115 }
1116 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1117
1118 cap->issued &= retain; /* drop bits we don't want */
1119 if (cap->implemented & ~cap->issued) {
1120 /*
1121 * Wake up any waiters on wanted -> needed transition.
1122 * This is due to the weird transition from buffered
1123 * to sync IO... we need to flush dirty pages _before_
1124 * allowing sync writes to avoid reordering.
1125 */
1126 wake = 1;
1127 }
1128 cap->implemented &= cap->issued | used;
1129 cap->mds_wanted = want;
1130
1131 if (flushing) {
1132 /*
1133 * assign a tid for flush operations so we can avoid
1134 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1135 * clean type races. track latest tid for every bit
1136 * so we can handle flush AxFw, flush Fw, and have the
1137 * first ack clean Ax.
1138 */
1139 flush_tid = ++ci->i_cap_flush_last_tid;
1140 if (pflush_tid)
1141 *pflush_tid = flush_tid;
1142 dout(" cap_flush_tid %d\n", (int)flush_tid);
1143 for (i = 0; i < CEPH_CAP_BITS; i++)
1144 if (flushing & (1 << i))
1145 ci->i_cap_flush_tid[i] = flush_tid;
Sage Weil7d8cb262010-08-24 08:44:16 -07001146
1147 follows = ci->i_head_snapc->seq;
1148 } else {
1149 follows = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001150 }
1151
1152 keep = cap->implemented;
1153 seq = cap->seq;
1154 issue_seq = cap->issue_seq;
1155 mseq = cap->mseq;
1156 size = inode->i_size;
1157 ci->i_reported_size = size;
1158 max_size = ci->i_wanted_max_size;
1159 ci->i_requested_max_size = max_size;
1160 mtime = inode->i_mtime;
1161 atime = inode->i_atime;
1162 time_warp_seq = ci->i_time_warp_seq;
Sage Weila8599bd2009-10-06 11:31:12 -07001163 uid = inode->i_uid;
1164 gid = inode->i_gid;
1165 mode = inode->i_mode;
1166
Sage Weil082afec2010-08-22 15:16:41 -07001167 if (flushing & CEPH_CAP_XATTR_EXCL) {
Sage Weila8599bd2009-10-06 11:31:12 -07001168 __ceph_build_xattrs_blob(ci);
Sage Weil082afec2010-08-22 15:16:41 -07001169 xattr_blob = ci->i_xattrs.blob;
1170 xattr_version = ci->i_xattrs.version;
Sage Weila8599bd2009-10-06 11:31:12 -07001171 }
1172
1173 spin_unlock(&inode->i_lock);
1174
Sage Weila8599bd2009-10-06 11:31:12 -07001175 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1176 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1177 size, max_size, &mtime, &atime, time_warp_seq,
Sage Weil082afec2010-08-22 15:16:41 -07001178 uid, gid, mode, xattr_version, xattr_blob,
Sage Weila8599bd2009-10-06 11:31:12 -07001179 follows);
1180 if (ret < 0) {
1181 dout("error sending cap msg, must requeue %p\n", inode);
1182 delayed = 1;
1183 }
1184
1185 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001186 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07001187
1188 return delayed;
1189}
1190
1191/*
1192 * When a snapshot is taken, clients accumulate dirty metadata on
1193 * inodes with capabilities in ceph_cap_snaps to describe the file
1194 * state at the time the snapshot was taken. This must be flushed
1195 * asynchronously back to the MDS once sync writes complete and dirty
1196 * data is written out.
1197 *
1198 * Called under i_lock. Takes s_mutex as needed.
1199 */
1200void __ceph_flush_snaps(struct ceph_inode_info *ci,
1201 struct ceph_mds_session **psession)
Yehuda Sadehcd84db62010-06-11 16:58:48 -07001202 __releases(ci->vfs_inode->i_lock)
1203 __acquires(ci->vfs_inode->i_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001204{
1205 struct inode *inode = &ci->vfs_inode;
1206 int mds;
1207 struct ceph_cap_snap *capsnap;
1208 u32 mseq;
1209 struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc;
1210 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1211 session->s_mutex */
1212 u64 next_follows = 0; /* keep track of how far we've gotten through the
1213 i_cap_snaps list, and skip these entries next time
1214 around to avoid an infinite loop */
1215
1216 if (psession)
1217 session = *psession;
1218
1219 dout("__flush_snaps %p\n", inode);
1220retry:
1221 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1222 /* avoid an infiniute loop after retry */
1223 if (capsnap->follows < next_follows)
1224 continue;
1225 /*
1226 * we need to wait for sync writes to complete and for dirty
1227 * pages to be written out.
1228 */
1229 if (capsnap->dirty_pages || capsnap->writing)
1230 continue;
1231
Sage Weil819ccbf2010-04-01 09:33:46 -07001232 /*
1233 * if cap writeback already occurred, we should have dropped
1234 * the capsnap in ceph_put_wrbuffer_cap_refs.
1235 */
1236 BUG_ON(capsnap->dirty == 0);
1237
Sage Weila8599bd2009-10-06 11:31:12 -07001238 /* pick mds, take s_mutex */
Sage Weilca81f3f2010-06-10 14:21:36 -07001239 if (ci->i_auth_cap == NULL) {
1240 dout("no auth cap (migrating?), doing nothing\n");
1241 goto out;
1242 }
1243 mds = ci->i_auth_cap->session->s_mds;
1244 mseq = ci->i_auth_cap->mseq;
1245
Sage Weila8599bd2009-10-06 11:31:12 -07001246 if (session && session->s_mds != mds) {
1247 dout("oops, wrong session %p mutex\n", session);
1248 mutex_unlock(&session->s_mutex);
1249 ceph_put_mds_session(session);
1250 session = NULL;
1251 }
1252 if (!session) {
1253 spin_unlock(&inode->i_lock);
1254 mutex_lock(&mdsc->mutex);
1255 session = __ceph_lookup_mds_session(mdsc, mds);
1256 mutex_unlock(&mdsc->mutex);
1257 if (session) {
1258 dout("inverting session/ino locks on %p\n",
1259 session);
1260 mutex_lock(&session->s_mutex);
1261 }
1262 /*
1263 * if session == NULL, we raced against a cap
Sage Weilca81f3f2010-06-10 14:21:36 -07001264 * deletion or migration. retry, and we'll
1265 * get a better @mds value next time.
Sage Weila8599bd2009-10-06 11:31:12 -07001266 */
1267 spin_lock(&inode->i_lock);
1268 goto retry;
1269 }
1270
1271 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1272 atomic_inc(&capsnap->nref);
1273 if (!list_empty(&capsnap->flushing_item))
1274 list_del_init(&capsnap->flushing_item);
1275 list_add_tail(&capsnap->flushing_item,
1276 &session->s_cap_snaps_flushing);
1277 spin_unlock(&inode->i_lock);
1278
1279 dout("flush_snaps %p cap_snap %p follows %lld size %llu\n",
1280 inode, capsnap, next_follows, capsnap->size);
1281 send_cap_msg(session, ceph_vino(inode).ino, 0,
1282 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1283 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1284 capsnap->size, 0,
1285 &capsnap->mtime, &capsnap->atime,
1286 capsnap->time_warp_seq,
1287 capsnap->uid, capsnap->gid, capsnap->mode,
Sage Weil4a625be2010-08-22 15:03:56 -07001288 capsnap->xattr_version, capsnap->xattr_blob,
Sage Weila8599bd2009-10-06 11:31:12 -07001289 capsnap->follows);
1290
1291 next_follows = capsnap->follows + 1;
1292 ceph_put_cap_snap(capsnap);
1293
1294 spin_lock(&inode->i_lock);
1295 goto retry;
1296 }
1297
1298 /* we flushed them all; remove this inode from the queue */
1299 spin_lock(&mdsc->snap_flush_lock);
1300 list_del_init(&ci->i_snap_flush_item);
1301 spin_unlock(&mdsc->snap_flush_lock);
1302
Sage Weilca81f3f2010-06-10 14:21:36 -07001303out:
Sage Weila8599bd2009-10-06 11:31:12 -07001304 if (psession)
1305 *psession = session;
1306 else if (session) {
1307 mutex_unlock(&session->s_mutex);
1308 ceph_put_mds_session(session);
1309 }
1310}
1311
1312static void ceph_flush_snaps(struct ceph_inode_info *ci)
1313{
1314 struct inode *inode = &ci->vfs_inode;
1315
1316 spin_lock(&inode->i_lock);
1317 __ceph_flush_snaps(ci, NULL);
1318 spin_unlock(&inode->i_lock);
1319}
1320
1321/*
Sage Weil76e3b392009-10-15 18:13:53 -07001322 * Mark caps dirty. If inode is newly dirty, add to the global dirty
1323 * list.
1324 */
1325void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
1326{
Cheng Renquan640ef792010-03-26 17:40:33 +08001327 struct ceph_mds_client *mdsc =
1328 &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil76e3b392009-10-15 18:13:53 -07001329 struct inode *inode = &ci->vfs_inode;
1330 int was = ci->i_dirty_caps;
1331 int dirty = 0;
1332
1333 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1334 ceph_cap_string(mask), ceph_cap_string(was),
1335 ceph_cap_string(was | mask));
1336 ci->i_dirty_caps |= mask;
1337 if (was == 0) {
Sage Weil7d8cb262010-08-24 08:44:16 -07001338 if (!ci->i_head_snapc)
1339 ci->i_head_snapc = ceph_get_snap_context(
1340 ci->i_snap_realm->cached_context);
1341 dout(" inode %p now dirty snapc %p\n", &ci->vfs_inode,
1342 ci->i_head_snapc);
Sage Weil76e3b392009-10-15 18:13:53 -07001343 BUG_ON(!list_empty(&ci->i_dirty_item));
1344 spin_lock(&mdsc->cap_dirty_lock);
1345 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1346 spin_unlock(&mdsc->cap_dirty_lock);
1347 if (ci->i_flushing_caps == 0) {
1348 igrab(inode);
1349 dirty |= I_DIRTY_SYNC;
1350 }
1351 }
1352 BUG_ON(list_empty(&ci->i_dirty_item));
1353 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1354 (mask & CEPH_CAP_FILE_BUFFER))
1355 dirty |= I_DIRTY_DATASYNC;
1356 if (dirty)
1357 __mark_inode_dirty(inode, dirty);
1358 __cap_delay_requeue(mdsc, ci);
1359}
1360
1361/*
Sage Weila8599bd2009-10-06 11:31:12 -07001362 * Add dirty inode to the flushing list. Assigned a seq number so we
1363 * can wait for caps to flush without starving.
Sage Weilcdc35f92009-10-14 14:24:19 -07001364 *
1365 * Called under i_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001366 */
Sage Weilcdc35f92009-10-14 14:24:19 -07001367static int __mark_caps_flushing(struct inode *inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001368 struct ceph_mds_session *session)
1369{
Cheng Renquan640ef792010-03-26 17:40:33 +08001370 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001371 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001372 int flushing;
Sage Weil50b885b2009-12-01 14:12:07 -08001373
Sage Weilcdc35f92009-10-14 14:24:19 -07001374 BUG_ON(ci->i_dirty_caps == 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001375 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilcdc35f92009-10-14 14:24:19 -07001376
1377 flushing = ci->i_dirty_caps;
1378 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1379 ceph_cap_string(flushing),
1380 ceph_cap_string(ci->i_flushing_caps),
1381 ceph_cap_string(ci->i_flushing_caps | flushing));
1382 ci->i_flushing_caps |= flushing;
1383 ci->i_dirty_caps = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07001384 dout(" inode %p now !dirty\n", inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001385
Sage Weila8599bd2009-10-06 11:31:12 -07001386 spin_lock(&mdsc->cap_dirty_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07001387 list_del_init(&ci->i_dirty_item);
1388
1389 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
Sage Weila8599bd2009-10-06 11:31:12 -07001390 if (list_empty(&ci->i_flushing_item)) {
1391 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1392 mdsc->num_cap_flushing++;
Sage Weilafcdaea2009-10-14 14:27:38 -07001393 dout(" inode %p now flushing seq %lld\n", inode,
1394 ci->i_cap_flush_seq);
1395 } else {
1396 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1397 dout(" inode %p now flushing (more) seq %lld\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001398 ci->i_cap_flush_seq);
1399 }
1400 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weilcdc35f92009-10-14 14:24:19 -07001401
1402 return flushing;
Sage Weila8599bd2009-10-06 11:31:12 -07001403}
1404
1405/*
Sage Weil5ecad6f2010-02-17 10:43:37 -08001406 * try to invalidate mapping pages without blocking.
1407 */
1408static int mapping_is_empty(struct address_space *mapping)
1409{
1410 struct page *page = find_get_page(mapping, 0);
1411
1412 if (!page)
1413 return 1;
1414
1415 put_page(page);
1416 return 0;
1417}
1418
1419static int try_nonblocking_invalidate(struct inode *inode)
1420{
1421 struct ceph_inode_info *ci = ceph_inode(inode);
1422 u32 invalidating_gen = ci->i_rdcache_gen;
1423
1424 spin_unlock(&inode->i_lock);
1425 invalidate_mapping_pages(&inode->i_data, 0, -1);
1426 spin_lock(&inode->i_lock);
1427
1428 if (mapping_is_empty(&inode->i_data) &&
1429 invalidating_gen == ci->i_rdcache_gen) {
1430 /* success. */
1431 dout("try_nonblocking_invalidate %p success\n", inode);
1432 ci->i_rdcache_gen = 0;
1433 ci->i_rdcache_revoking = 0;
1434 return 0;
1435 }
1436 dout("try_nonblocking_invalidate %p failed\n", inode);
1437 return -1;
1438}
1439
1440/*
Sage Weila8599bd2009-10-06 11:31:12 -07001441 * Swiss army knife function to examine currently used and wanted
1442 * versus held caps. Release, flush, ack revoked caps to mds as
1443 * appropriate.
1444 *
1445 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1446 * cap release further.
1447 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1448 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1449 * further delay.
1450 */
1451void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1452 struct ceph_mds_session *session)
1453{
1454 struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode);
1455 struct ceph_mds_client *mdsc = &client->mdsc;
1456 struct inode *inode = &ci->vfs_inode;
1457 struct ceph_cap *cap;
1458 int file_wanted, used;
1459 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
Sage Weilcbd03632010-02-09 13:41:18 -08001460 int issued, implemented, want, retain, revoking, flushing = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001461 int mds = -1; /* keep track of how far we've gone through i_caps list
1462 to avoid an infinite loop on retry */
1463 struct rb_node *p;
1464 int tried_invalidate = 0;
1465 int delayed = 0, sent = 0, force_requeue = 0, num;
Sage Weilcbd03632010-02-09 13:41:18 -08001466 int queue_invalidate = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001467 int is_delayed = flags & CHECK_CAPS_NODELAY;
1468
1469 /* if we are unmounting, flush any unused caps immediately. */
1470 if (mdsc->stopping)
1471 is_delayed = 1;
1472
1473 spin_lock(&inode->i_lock);
1474
1475 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1476 flags |= CHECK_CAPS_FLUSH;
1477
1478 /* flush snaps first time around only */
1479 if (!list_empty(&ci->i_cap_snaps))
1480 __ceph_flush_snaps(ci, &session);
1481 goto retry_locked;
1482retry:
1483 spin_lock(&inode->i_lock);
1484retry_locked:
1485 file_wanted = __ceph_caps_file_wanted(ci);
1486 used = __ceph_caps_used(ci);
1487 want = file_wanted | used;
Sage Weilcbd03632010-02-09 13:41:18 -08001488 issued = __ceph_caps_issued(ci, &implemented);
1489 revoking = implemented & ~issued;
Sage Weila8599bd2009-10-06 11:31:12 -07001490
1491 retain = want | CEPH_CAP_PIN;
1492 if (!mdsc->stopping && inode->i_nlink > 0) {
1493 if (want) {
1494 retain |= CEPH_CAP_ANY; /* be greedy */
1495 } else {
1496 retain |= CEPH_CAP_ANY_SHARED;
1497 /*
1498 * keep RD only if we didn't have the file open RW,
1499 * because then the mds would revoke it anyway to
1500 * journal max_size=0.
1501 */
1502 if (ci->i_max_size == 0)
1503 retain |= CEPH_CAP_ANY_RD;
1504 }
1505 }
1506
1507 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
Sage Weilcbd03632010-02-09 13:41:18 -08001508 " issued %s revoking %s retain %s %s%s%s\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001509 ceph_cap_string(file_wanted),
1510 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1511 ceph_cap_string(ci->i_flushing_caps),
Sage Weilcbd03632010-02-09 13:41:18 -08001512 ceph_cap_string(issued), ceph_cap_string(revoking),
Sage Weila8599bd2009-10-06 11:31:12 -07001513 ceph_cap_string(retain),
1514 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1515 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1516 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1517
1518 /*
1519 * If we no longer need to hold onto old our caps, and we may
1520 * have cached pages, but don't want them, then try to invalidate.
1521 * If we fail, it's because pages are locked.... try again later.
1522 */
1523 if ((!is_delayed || mdsc->stopping) &&
1524 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
1525 ci->i_rdcache_gen && /* may have cached pages */
Sage Weilcbd03632010-02-09 13:41:18 -08001526 (file_wanted == 0 || /* no open files */
Sage Weil29625072010-05-27 10:40:43 -07001527 (revoking & (CEPH_CAP_FILE_CACHE|
1528 CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
Sage Weila8599bd2009-10-06 11:31:12 -07001529 !tried_invalidate) {
Sage Weila8599bd2009-10-06 11:31:12 -07001530 dout("check_caps trying to invalidate on %p\n", inode);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001531 if (try_nonblocking_invalidate(inode) < 0) {
Sage Weil29625072010-05-27 10:40:43 -07001532 if (revoking & (CEPH_CAP_FILE_CACHE|
1533 CEPH_CAP_FILE_LAZYIO)) {
Sage Weil5ecad6f2010-02-17 10:43:37 -08001534 dout("check_caps queuing invalidate\n");
1535 queue_invalidate = 1;
1536 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1537 } else {
1538 dout("check_caps failed to invalidate pages\n");
1539 /* we failed to invalidate pages. check these
1540 caps again later. */
1541 force_requeue = 1;
1542 __cap_set_timeouts(mdsc, ci);
1543 }
Sage Weila8599bd2009-10-06 11:31:12 -07001544 }
1545 tried_invalidate = 1;
1546 goto retry_locked;
1547 }
1548
1549 num = 0;
1550 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1551 cap = rb_entry(p, struct ceph_cap, ci_node);
1552 num++;
1553
1554 /* avoid looping forever */
1555 if (mds >= cap->mds ||
1556 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1557 continue;
1558
1559 /* NOTE: no side-effects allowed, until we take s_mutex */
1560
1561 revoking = cap->implemented & ~cap->issued;
1562 if (revoking)
Sage Weilcbd03632010-02-09 13:41:18 -08001563 dout(" mds%d revoking %s\n", cap->mds,
Sage Weila8599bd2009-10-06 11:31:12 -07001564 ceph_cap_string(revoking));
1565
1566 if (cap == ci->i_auth_cap &&
1567 (cap->issued & CEPH_CAP_FILE_WR)) {
1568 /* request larger max_size from MDS? */
1569 if (ci->i_wanted_max_size > ci->i_max_size &&
1570 ci->i_wanted_max_size > ci->i_requested_max_size) {
1571 dout("requesting new max_size\n");
1572 goto ack;
1573 }
1574
1575 /* approaching file_max? */
1576 if ((inode->i_size << 1) >= ci->i_max_size &&
1577 (ci->i_reported_size << 1) < ci->i_max_size) {
1578 dout("i_size approaching max_size\n");
1579 goto ack;
1580 }
1581 }
1582 /* flush anything dirty? */
1583 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1584 ci->i_dirty_caps) {
1585 dout("flushing dirty caps\n");
1586 goto ack;
1587 }
1588
1589 /* completed revocation? going down and there are no caps? */
1590 if (revoking && (revoking & used) == 0) {
1591 dout("completed revocation of %s\n",
1592 ceph_cap_string(cap->implemented & ~cap->issued));
1593 goto ack;
1594 }
1595
1596 /* want more caps from mds? */
1597 if (want & ~(cap->mds_wanted | cap->issued))
1598 goto ack;
1599
1600 /* things we might delay */
1601 if ((cap->issued & ~retain) == 0 &&
1602 cap->mds_wanted == want)
1603 continue; /* nope, all good */
1604
1605 if (is_delayed)
1606 goto ack;
1607
1608 /* delay? */
1609 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1610 time_before(jiffies, ci->i_hold_caps_max)) {
1611 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1612 ceph_cap_string(cap->issued),
1613 ceph_cap_string(cap->issued & retain),
1614 ceph_cap_string(cap->mds_wanted),
1615 ceph_cap_string(want));
1616 delayed++;
1617 continue;
1618 }
1619
1620ack:
Sage Weile9964c12010-03-01 15:16:56 -08001621 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1622 dout(" skipping %p I_NOFLUSH set\n", inode);
1623 continue;
1624 }
1625
Sage Weila8599bd2009-10-06 11:31:12 -07001626 if (session && session != cap->session) {
1627 dout("oops, wrong session %p mutex\n", session);
1628 mutex_unlock(&session->s_mutex);
1629 session = NULL;
1630 }
1631 if (!session) {
1632 session = cap->session;
1633 if (mutex_trylock(&session->s_mutex) == 0) {
1634 dout("inverting session/ino locks on %p\n",
1635 session);
1636 spin_unlock(&inode->i_lock);
1637 if (took_snap_rwsem) {
1638 up_read(&mdsc->snap_rwsem);
1639 took_snap_rwsem = 0;
1640 }
1641 mutex_lock(&session->s_mutex);
1642 goto retry;
1643 }
1644 }
1645 /* take snap_rwsem after session mutex */
1646 if (!took_snap_rwsem) {
1647 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1648 dout("inverting snap/in locks on %p\n",
1649 inode);
1650 spin_unlock(&inode->i_lock);
1651 down_read(&mdsc->snap_rwsem);
1652 took_snap_rwsem = 1;
1653 goto retry;
1654 }
1655 took_snap_rwsem = 1;
1656 }
1657
Sage Weilcdc35f92009-10-14 14:24:19 -07001658 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1659 flushing = __mark_caps_flushing(inode, session);
Sage Weila8599bd2009-10-06 11:31:12 -07001660
1661 mds = cap->mds; /* remember mds, so we don't repeat */
1662 sent++;
1663
1664 /* __send_cap drops i_lock */
1665 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
1666 retain, flushing, NULL);
1667 goto retry; /* retake i_lock and restart our cap scan. */
1668 }
1669
1670 /*
1671 * Reschedule delayed caps release if we delayed anything,
1672 * otherwise cancel.
1673 */
1674 if (delayed && is_delayed)
1675 force_requeue = 1; /* __send_cap delayed release; requeue */
1676 if (!delayed && !is_delayed)
1677 __cap_delay_cancel(mdsc, ci);
1678 else if (!is_delayed || force_requeue)
1679 __cap_delay_requeue(mdsc, ci);
1680
1681 spin_unlock(&inode->i_lock);
1682
Sage Weilcbd03632010-02-09 13:41:18 -08001683 if (queue_invalidate)
Sage Weil3c6f6b72010-02-09 15:24:44 -08001684 ceph_queue_invalidate(inode);
Sage Weilcbd03632010-02-09 13:41:18 -08001685
Sage Weilcdc2ce02010-03-16 13:39:28 -07001686 if (session)
Sage Weila8599bd2009-10-06 11:31:12 -07001687 mutex_unlock(&session->s_mutex);
1688 if (took_snap_rwsem)
1689 up_read(&mdsc->snap_rwsem);
1690}
1691
1692/*
Sage Weila8599bd2009-10-06 11:31:12 -07001693 * Try to flush dirty caps back to the auth mds.
1694 */
1695static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1696 unsigned *flush_tid)
1697{
Cheng Renquan640ef792010-03-26 17:40:33 +08001698 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001699 struct ceph_inode_info *ci = ceph_inode(inode);
1700 int unlock_session = session ? 0 : 1;
1701 int flushing = 0;
1702
1703retry:
1704 spin_lock(&inode->i_lock);
Sage Weile9964c12010-03-01 15:16:56 -08001705 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1706 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1707 goto out;
1708 }
Sage Weila8599bd2009-10-06 11:31:12 -07001709 if (ci->i_dirty_caps && ci->i_auth_cap) {
1710 struct ceph_cap *cap = ci->i_auth_cap;
1711 int used = __ceph_caps_used(ci);
1712 int want = __ceph_caps_wanted(ci);
1713 int delayed;
1714
1715 if (!session) {
1716 spin_unlock(&inode->i_lock);
1717 session = cap->session;
1718 mutex_lock(&session->s_mutex);
1719 goto retry;
1720 }
1721 BUG_ON(session != cap->session);
1722 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1723 goto out;
1724
Sage Weilcdc35f92009-10-14 14:24:19 -07001725 flushing = __mark_caps_flushing(inode, session);
Sage Weila8599bd2009-10-06 11:31:12 -07001726
1727 /* __send_cap drops i_lock */
1728 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1729 cap->issued | cap->implemented, flushing,
1730 flush_tid);
1731 if (!delayed)
1732 goto out_unlocked;
1733
1734 spin_lock(&inode->i_lock);
1735 __cap_delay_requeue(mdsc, ci);
1736 }
1737out:
1738 spin_unlock(&inode->i_lock);
1739out_unlocked:
1740 if (session && unlock_session)
1741 mutex_unlock(&session->s_mutex);
1742 return flushing;
1743}
1744
1745/*
1746 * Return true if we've flushed caps through the given flush_tid.
1747 */
1748static int caps_are_flushed(struct inode *inode, unsigned tid)
1749{
1750 struct ceph_inode_info *ci = ceph_inode(inode);
Dan Carpentera5ee7512010-05-07 10:27:14 +02001751 int i, ret = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07001752
1753 spin_lock(&inode->i_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001754 for (i = 0; i < CEPH_CAP_BITS; i++)
1755 if ((ci->i_flushing_caps & (1 << i)) &&
1756 ci->i_cap_flush_tid[i] <= tid) {
1757 /* still flushing this bit */
1758 ret = 0;
1759 break;
1760 }
1761 spin_unlock(&inode->i_lock);
1762 return ret;
1763}
1764
1765/*
1766 * Wait on any unsafe replies for the given inode. First wait on the
1767 * newest request, and make that the upper bound. Then, if there are
1768 * more requests, keep waiting on the oldest as long as it is still older
1769 * than the original request.
1770 */
1771static void sync_write_wait(struct inode *inode)
1772{
1773 struct ceph_inode_info *ci = ceph_inode(inode);
1774 struct list_head *head = &ci->i_unsafe_writes;
1775 struct ceph_osd_request *req;
1776 u64 last_tid;
1777
1778 spin_lock(&ci->i_unsafe_lock);
1779 if (list_empty(head))
1780 goto out;
1781
1782 /* set upper bound as _last_ entry in chain */
1783 req = list_entry(head->prev, struct ceph_osd_request,
1784 r_unsafe_item);
1785 last_tid = req->r_tid;
1786
1787 do {
1788 ceph_osdc_get_request(req);
1789 spin_unlock(&ci->i_unsafe_lock);
1790 dout("sync_write_wait on tid %llu (until %llu)\n",
1791 req->r_tid, last_tid);
1792 wait_for_completion(&req->r_safe_completion);
1793 spin_lock(&ci->i_unsafe_lock);
1794 ceph_osdc_put_request(req);
1795
1796 /*
1797 * from here on look at first entry in chain, since we
1798 * only want to wait for anything older than last_tid
1799 */
1800 if (list_empty(head))
1801 break;
1802 req = list_entry(head->next, struct ceph_osd_request,
1803 r_unsafe_item);
1804 } while (req->r_tid < last_tid);
1805out:
1806 spin_unlock(&ci->i_unsafe_lock);
1807}
1808
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001809int ceph_fsync(struct file *file, int datasync)
Sage Weila8599bd2009-10-06 11:31:12 -07001810{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001811 struct inode *inode = file->f_mapping->host;
Sage Weila8599bd2009-10-06 11:31:12 -07001812 struct ceph_inode_info *ci = ceph_inode(inode);
1813 unsigned flush_tid;
1814 int ret;
1815 int dirty;
1816
1817 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1818 sync_write_wait(inode);
1819
1820 ret = filemap_write_and_wait(inode->i_mapping);
1821 if (ret < 0)
1822 return ret;
1823
1824 dirty = try_flush_caps(inode, NULL, &flush_tid);
1825 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1826
1827 /*
1828 * only wait on non-file metadata writeback (the mds
1829 * can recover size and mtime, so we don't need to
1830 * wait for that)
1831 */
1832 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1833 dout("fsync waiting for flush_tid %u\n", flush_tid);
1834 ret = wait_event_interruptible(ci->i_cap_wq,
1835 caps_are_flushed(inode, flush_tid));
1836 }
1837
1838 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
1839 return ret;
1840}
1841
1842/*
1843 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1844 * queue inode for flush but don't do so immediately, because we can
1845 * get by with fewer MDS messages if we wait for data writeback to
1846 * complete first.
1847 */
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11001848int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
Sage Weila8599bd2009-10-06 11:31:12 -07001849{
1850 struct ceph_inode_info *ci = ceph_inode(inode);
1851 unsigned flush_tid;
1852 int err = 0;
1853 int dirty;
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11001854 int wait = wbc->sync_mode == WB_SYNC_ALL;
Sage Weila8599bd2009-10-06 11:31:12 -07001855
1856 dout("write_inode %p wait=%d\n", inode, wait);
1857 if (wait) {
1858 dirty = try_flush_caps(inode, NULL, &flush_tid);
1859 if (dirty)
1860 err = wait_event_interruptible(ci->i_cap_wq,
1861 caps_are_flushed(inode, flush_tid));
1862 } else {
Cheng Renquan640ef792010-03-26 17:40:33 +08001863 struct ceph_mds_client *mdsc =
1864 &ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001865
1866 spin_lock(&inode->i_lock);
1867 if (__ceph_caps_dirty(ci))
1868 __cap_delay_requeue_front(mdsc, ci);
1869 spin_unlock(&inode->i_lock);
1870 }
1871 return err;
1872}
1873
1874/*
1875 * After a recovering MDS goes active, we need to resend any caps
1876 * we were flushing.
1877 *
1878 * Caller holds session->s_mutex.
1879 */
1880static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1881 struct ceph_mds_session *session)
1882{
1883 struct ceph_cap_snap *capsnap;
1884
1885 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1886 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1887 flushing_item) {
1888 struct ceph_inode_info *ci = capsnap->ci;
1889 struct inode *inode = &ci->vfs_inode;
1890 struct ceph_cap *cap;
1891
1892 spin_lock(&inode->i_lock);
1893 cap = ci->i_auth_cap;
1894 if (cap && cap->session == session) {
1895 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1896 cap, capsnap);
1897 __ceph_flush_snaps(ci, &session);
1898 } else {
1899 pr_err("%p auth cap %p not mds%d ???\n", inode,
1900 cap, session->s_mds);
Sage Weila8599bd2009-10-06 11:31:12 -07001901 }
Sage Weil0b0c06d2010-04-20 10:27:13 -07001902 spin_unlock(&inode->i_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001903 }
1904}
1905
1906void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1907 struct ceph_mds_session *session)
1908{
1909 struct ceph_inode_info *ci;
1910
1911 kick_flushing_capsnaps(mdsc, session);
1912
1913 dout("kick_flushing_caps mds%d\n", session->s_mds);
1914 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1915 struct inode *inode = &ci->vfs_inode;
1916 struct ceph_cap *cap;
1917 int delayed = 0;
1918
1919 spin_lock(&inode->i_lock);
1920 cap = ci->i_auth_cap;
1921 if (cap && cap->session == session) {
1922 dout("kick_flushing_caps %p cap %p %s\n", inode,
1923 cap, ceph_cap_string(ci->i_flushing_caps));
1924 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1925 __ceph_caps_used(ci),
1926 __ceph_caps_wanted(ci),
1927 cap->issued | cap->implemented,
1928 ci->i_flushing_caps, NULL);
1929 if (delayed) {
1930 spin_lock(&inode->i_lock);
1931 __cap_delay_requeue(mdsc, ci);
1932 spin_unlock(&inode->i_lock);
1933 }
1934 } else {
1935 pr_err("%p auth cap %p not mds%d ???\n", inode,
1936 cap, session->s_mds);
1937 spin_unlock(&inode->i_lock);
1938 }
1939 }
1940}
1941
1942
1943/*
1944 * Take references to capabilities we hold, so that we don't release
1945 * them to the MDS prematurely.
1946 *
1947 * Protected by i_lock.
1948 */
1949static void __take_cap_refs(struct ceph_inode_info *ci, int got)
1950{
1951 if (got & CEPH_CAP_PIN)
1952 ci->i_pin_ref++;
1953 if (got & CEPH_CAP_FILE_RD)
1954 ci->i_rd_ref++;
1955 if (got & CEPH_CAP_FILE_CACHE)
1956 ci->i_rdcache_ref++;
1957 if (got & CEPH_CAP_FILE_WR)
1958 ci->i_wr_ref++;
1959 if (got & CEPH_CAP_FILE_BUFFER) {
1960 if (ci->i_wrbuffer_ref == 0)
1961 igrab(&ci->vfs_inode);
1962 ci->i_wrbuffer_ref++;
1963 dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n",
1964 &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref);
1965 }
1966}
1967
1968/*
1969 * Try to grab cap references. Specify those refs we @want, and the
1970 * minimal set we @need. Also include the larger offset we are writing
1971 * to (when applicable), and check against max_size here as well.
1972 * Note that caller is responsible for ensuring max_size increases are
1973 * requested from the MDS.
1974 */
1975static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
1976 int *got, loff_t endoff, int *check_max, int *err)
1977{
1978 struct inode *inode = &ci->vfs_inode;
1979 int ret = 0;
1980 int have, implemented;
Sage Weil195d3ce2010-03-01 09:57:54 -08001981 int file_wanted;
Sage Weila8599bd2009-10-06 11:31:12 -07001982
1983 dout("get_cap_refs %p need %s want %s\n", inode,
1984 ceph_cap_string(need), ceph_cap_string(want));
1985 spin_lock(&inode->i_lock);
1986
Sage Weil195d3ce2010-03-01 09:57:54 -08001987 /* make sure file is actually open */
1988 file_wanted = __ceph_caps_file_wanted(ci);
1989 if ((file_wanted & need) == 0) {
1990 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
1991 ceph_cap_string(need), ceph_cap_string(file_wanted));
Sage Weila8599bd2009-10-06 11:31:12 -07001992 *err = -EBADF;
1993 ret = 1;
1994 goto out;
1995 }
1996
1997 if (need & CEPH_CAP_FILE_WR) {
1998 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
1999 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2000 inode, endoff, ci->i_max_size);
2001 if (endoff > ci->i_wanted_max_size) {
2002 *check_max = 1;
2003 ret = 1;
2004 }
2005 goto out;
2006 }
2007 /*
2008 * If a sync write is in progress, we must wait, so that we
2009 * can get a final snapshot value for size+mtime.
2010 */
2011 if (__ceph_have_pending_cap_snap(ci)) {
2012 dout("get_cap_refs %p cap_snap_pending\n", inode);
2013 goto out;
2014 }
2015 }
2016 have = __ceph_caps_issued(ci, &implemented);
2017
2018 /*
2019 * disallow writes while a truncate is pending
2020 */
2021 if (ci->i_truncate_pending)
2022 have &= ~CEPH_CAP_FILE_WR;
2023
2024 if ((have & need) == need) {
2025 /*
2026 * Look at (implemented & ~have & not) so that we keep waiting
2027 * on transition from wanted -> needed caps. This is needed
2028 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2029 * going before a prior buffered writeback happens.
2030 */
2031 int not = want & ~(have & need);
2032 int revoking = implemented & ~have;
2033 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2034 inode, ceph_cap_string(have), ceph_cap_string(not),
2035 ceph_cap_string(revoking));
2036 if ((revoking & not) == 0) {
2037 *got = need | (have & want);
2038 __take_cap_refs(ci, *got);
2039 ret = 1;
2040 }
2041 } else {
2042 dout("get_cap_refs %p have %s needed %s\n", inode,
2043 ceph_cap_string(have), ceph_cap_string(need));
2044 }
2045out:
2046 spin_unlock(&inode->i_lock);
2047 dout("get_cap_refs %p ret %d got %s\n", inode,
2048 ret, ceph_cap_string(*got));
2049 return ret;
2050}
2051
2052/*
2053 * Check the offset we are writing up to against our current
2054 * max_size. If necessary, tell the MDS we want to write to
2055 * a larger offset.
2056 */
2057static void check_max_size(struct inode *inode, loff_t endoff)
2058{
2059 struct ceph_inode_info *ci = ceph_inode(inode);
2060 int check = 0;
2061
2062 /* do we need to explicitly request a larger max_size? */
2063 spin_lock(&inode->i_lock);
2064 if ((endoff >= ci->i_max_size ||
2065 endoff > (inode->i_size << 1)) &&
2066 endoff > ci->i_wanted_max_size) {
2067 dout("write %p at large endoff %llu, req max_size\n",
2068 inode, endoff);
2069 ci->i_wanted_max_size = endoff;
2070 check = 1;
2071 }
2072 spin_unlock(&inode->i_lock);
2073 if (check)
2074 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2075}
2076
2077/*
2078 * Wait for caps, and take cap references. If we can't get a WR cap
2079 * due to a small max_size, make sure we check_max_size (and possibly
2080 * ask the mds) so we don't get hung up indefinitely.
2081 */
2082int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2083 loff_t endoff)
2084{
2085 int check_max, ret, err;
2086
2087retry:
2088 if (endoff > 0)
2089 check_max_size(&ci->vfs_inode, endoff);
2090 check_max = 0;
2091 err = 0;
2092 ret = wait_event_interruptible(ci->i_cap_wq,
2093 try_get_cap_refs(ci, need, want,
2094 got, endoff,
2095 &check_max, &err));
2096 if (err)
2097 ret = err;
2098 if (check_max)
2099 goto retry;
2100 return ret;
2101}
2102
2103/*
2104 * Take cap refs. Caller must already know we hold at least one ref
2105 * on the caps in question or we don't know this is safe.
2106 */
2107void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2108{
2109 spin_lock(&ci->vfs_inode.i_lock);
2110 __take_cap_refs(ci, caps);
2111 spin_unlock(&ci->vfs_inode.i_lock);
2112}
2113
2114/*
2115 * Release cap refs.
2116 *
2117 * If we released the last ref on any given cap, call ceph_check_caps
2118 * to release (or schedule a release).
2119 *
2120 * If we are releasing a WR cap (from a sync write), finalize any affected
2121 * cap_snap, and wake up any waiters.
2122 */
2123void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2124{
2125 struct inode *inode = &ci->vfs_inode;
2126 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2127 struct ceph_cap_snap *capsnap;
2128
2129 spin_lock(&inode->i_lock);
2130 if (had & CEPH_CAP_PIN)
2131 --ci->i_pin_ref;
2132 if (had & CEPH_CAP_FILE_RD)
2133 if (--ci->i_rd_ref == 0)
2134 last++;
2135 if (had & CEPH_CAP_FILE_CACHE)
2136 if (--ci->i_rdcache_ref == 0)
2137 last++;
2138 if (had & CEPH_CAP_FILE_BUFFER) {
2139 if (--ci->i_wrbuffer_ref == 0) {
2140 last++;
2141 put++;
2142 }
2143 dout("put_cap_refs %p wrbuffer %d -> %d (?)\n",
2144 inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref);
2145 }
2146 if (had & CEPH_CAP_FILE_WR)
2147 if (--ci->i_wr_ref == 0) {
2148 last++;
2149 if (!list_empty(&ci->i_cap_snaps)) {
2150 capsnap = list_first_entry(&ci->i_cap_snaps,
2151 struct ceph_cap_snap,
2152 ci_item);
2153 if (capsnap->writing) {
2154 capsnap->writing = 0;
2155 flushsnaps =
2156 __ceph_finish_cap_snap(ci,
2157 capsnap);
2158 wake = 1;
2159 }
2160 }
2161 }
2162 spin_unlock(&inode->i_lock);
2163
Sage Weil819ccbf2010-04-01 09:33:46 -07002164 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2165 last ? " last" : "", put ? " put" : "");
Sage Weila8599bd2009-10-06 11:31:12 -07002166
2167 if (last && !flushsnaps)
2168 ceph_check_caps(ci, 0, NULL);
2169 else if (flushsnaps)
2170 ceph_flush_snaps(ci);
2171 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002172 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002173 if (put)
2174 iput(inode);
2175}
2176
2177/*
2178 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2179 * context. Adjust per-snap dirty page accounting as appropriate.
2180 * Once all dirty data for a cap_snap is flushed, flush snapped file
2181 * metadata back to the MDS. If we dropped the last ref, call
2182 * ceph_check_caps.
2183 */
2184void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2185 struct ceph_snap_context *snapc)
2186{
2187 struct inode *inode = &ci->vfs_inode;
2188 int last = 0;
Sage Weil819ccbf2010-04-01 09:33:46 -07002189 int complete_capsnap = 0;
2190 int drop_capsnap = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002191 int found = 0;
2192 struct ceph_cap_snap *capsnap = NULL;
2193
2194 spin_lock(&inode->i_lock);
2195 ci->i_wrbuffer_ref -= nr;
2196 last = !ci->i_wrbuffer_ref;
2197
2198 if (ci->i_head_snapc == snapc) {
2199 ci->i_wrbuffer_ref_head -= nr;
Sage Weil7d8cb262010-08-24 08:44:16 -07002200 if (ci->i_wrbuffer_ref_head == 0 &&
2201 ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
2202 BUG_ON(!ci->i_head_snapc);
Sage Weila8599bd2009-10-06 11:31:12 -07002203 ceph_put_snap_context(ci->i_head_snapc);
2204 ci->i_head_snapc = NULL;
2205 }
2206 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2207 inode,
2208 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2209 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2210 last ? " LAST" : "");
2211 } else {
2212 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2213 if (capsnap->context == snapc) {
2214 found = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002215 break;
2216 }
2217 }
2218 BUG_ON(!found);
Sage Weil819ccbf2010-04-01 09:33:46 -07002219 capsnap->dirty_pages -= nr;
2220 if (capsnap->dirty_pages == 0) {
2221 complete_capsnap = 1;
2222 if (capsnap->dirty == 0)
2223 /* cap writeback completed before we created
2224 * the cap_snap; no FLUSHSNAP is needed */
2225 drop_capsnap = 1;
2226 }
Sage Weila8599bd2009-10-06 11:31:12 -07002227 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
Sage Weil819ccbf2010-04-01 09:33:46 -07002228 " snap %lld %d/%d -> %d/%d %s%s%s\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002229 inode, capsnap, capsnap->context->seq,
2230 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2231 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2232 last ? " (wrbuffer last)" : "",
Sage Weil819ccbf2010-04-01 09:33:46 -07002233 complete_capsnap ? " (complete capsnap)" : "",
2234 drop_capsnap ? " (drop capsnap)" : "");
2235 if (drop_capsnap) {
2236 ceph_put_snap_context(capsnap->context);
2237 list_del(&capsnap->ci_item);
2238 list_del(&capsnap->flushing_item);
2239 ceph_put_cap_snap(capsnap);
2240 }
Sage Weila8599bd2009-10-06 11:31:12 -07002241 }
2242
2243 spin_unlock(&inode->i_lock);
2244
2245 if (last) {
2246 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2247 iput(inode);
Sage Weil819ccbf2010-04-01 09:33:46 -07002248 } else if (complete_capsnap) {
Sage Weila8599bd2009-10-06 11:31:12 -07002249 ceph_flush_snaps(ci);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002250 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002251 }
Sage Weil819ccbf2010-04-01 09:33:46 -07002252 if (drop_capsnap)
2253 iput(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002254}
2255
2256/*
2257 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2258 * actually be a revocation if it specifies a smaller cap set.)
2259 *
Sage Weil15637c82010-03-16 13:42:00 -07002260 * caller holds s_mutex and i_lock, we drop both.
2261 *
Sage Weila8599bd2009-10-06 11:31:12 -07002262 * return value:
2263 * 0 - ok
2264 * 1 - check_caps on auth cap only (writeback)
2265 * 2 - check_caps (ack revoke)
2266 */
Sage Weil15637c82010-03-16 13:42:00 -07002267static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2268 struct ceph_mds_session *session,
2269 struct ceph_cap *cap,
2270 struct ceph_buffer *xattr_buf)
Yehuda Sadehcd84db62010-06-11 16:58:48 -07002271 __releases(inode->i_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07002272{
2273 struct ceph_inode_info *ci = ceph_inode(inode);
2274 int mds = session->s_mds;
2275 int seq = le32_to_cpu(grant->seq);
2276 int newcaps = le32_to_cpu(grant->caps);
2277 int issued, implemented, used, wanted, dirty;
2278 u64 size = le64_to_cpu(grant->size);
2279 u64 max_size = le64_to_cpu(grant->max_size);
2280 struct timespec mtime, atime, ctime;
Sage Weil15637c82010-03-16 13:42:00 -07002281 int check_caps = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002282 int wake = 0;
2283 int writeback = 0;
2284 int revoked_rdcache = 0;
Sage Weil3c6f6b72010-02-09 15:24:44 -08002285 int queue_invalidate = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002286
2287 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2288 inode, cap, mds, seq, ceph_cap_string(newcaps));
2289 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2290 inode->i_size);
2291
2292 /*
2293 * If CACHE is being revoked, and we have no dirty buffers,
2294 * try to invalidate (once). (If there are dirty buffers, we
2295 * will invalidate _after_ writeback.)
2296 */
Sage Weil3b454c42010-06-10 13:20:33 -07002297 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2298 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
Yehuda Sadehbcd2cbd2010-02-19 00:12:21 +00002299 !ci->i_wrbuffer_ref) {
Sage Weil5ecad6f2010-02-17 10:43:37 -08002300 if (try_nonblocking_invalidate(inode) == 0) {
2301 revoked_rdcache = 1;
2302 } else {
Sage Weila8599bd2009-10-06 11:31:12 -07002303 /* there were locked pages.. invalidate later
2304 in a separate thread. */
2305 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Sage Weil3c6f6b72010-02-09 15:24:44 -08002306 queue_invalidate = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002307 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2308 }
Sage Weila8599bd2009-10-06 11:31:12 -07002309 }
Sage Weila8599bd2009-10-06 11:31:12 -07002310 }
2311
2312 /* side effects now are allowed */
2313
2314 issued = __ceph_caps_issued(ci, &implemented);
2315 issued |= implemented | __ceph_caps_dirty(ci);
2316
Sage Weil685f9a5d2009-11-09 12:05:48 -08002317 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -07002318
2319 __check_cap_issue(ci, cap, newcaps);
2320
2321 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2322 inode->i_mode = le32_to_cpu(grant->mode);
2323 inode->i_uid = le32_to_cpu(grant->uid);
2324 inode->i_gid = le32_to_cpu(grant->gid);
2325 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2326 inode->i_uid, inode->i_gid);
2327 }
2328
2329 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
2330 inode->i_nlink = le32_to_cpu(grant->nlink);
2331
2332 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2333 int len = le32_to_cpu(grant->xattr_len);
2334 u64 version = le64_to_cpu(grant->xattr_version);
2335
2336 if (version > ci->i_xattrs.version) {
2337 dout(" got new xattrs v%llu on %p len %d\n",
2338 version, inode, len);
2339 if (ci->i_xattrs.blob)
2340 ceph_buffer_put(ci->i_xattrs.blob);
2341 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2342 ci->i_xattrs.version = version;
2343 }
2344 }
2345
2346 /* size/ctime/mtime/atime? */
2347 ceph_fill_file_size(inode, issued,
2348 le32_to_cpu(grant->truncate_seq),
2349 le64_to_cpu(grant->truncate_size), size);
2350 ceph_decode_timespec(&mtime, &grant->mtime);
2351 ceph_decode_timespec(&atime, &grant->atime);
2352 ceph_decode_timespec(&ctime, &grant->ctime);
2353 ceph_fill_file_time(inode, issued,
2354 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2355 &atime);
2356
2357 /* max size increase? */
2358 if (max_size != ci->i_max_size) {
2359 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2360 ci->i_max_size = max_size;
2361 if (max_size >= ci->i_wanted_max_size) {
2362 ci->i_wanted_max_size = 0; /* reset */
2363 ci->i_requested_max_size = 0;
2364 }
2365 wake = 1;
2366 }
2367
2368 /* check cap bits */
2369 wanted = __ceph_caps_wanted(ci);
2370 used = __ceph_caps_used(ci);
2371 dirty = __ceph_caps_dirty(ci);
2372 dout(" my wanted = %s, used = %s, dirty %s\n",
2373 ceph_cap_string(wanted),
2374 ceph_cap_string(used),
2375 ceph_cap_string(dirty));
2376 if (wanted != le32_to_cpu(grant->wanted)) {
2377 dout("mds wanted %s -> %s\n",
2378 ceph_cap_string(le32_to_cpu(grant->wanted)),
2379 ceph_cap_string(wanted));
2380 grant->wanted = cpu_to_le32(wanted);
2381 }
2382
2383 cap->seq = seq;
2384
2385 /* file layout may have changed */
2386 ci->i_layout = grant->layout;
2387
2388 /* revocation, grant, or no-op? */
2389 if (cap->issued & ~newcaps) {
Sage Weil3b454c42010-06-10 13:20:33 -07002390 int revoking = cap->issued & ~newcaps;
2391
2392 dout("revocation: %s -> %s (revoking %s)\n",
2393 ceph_cap_string(cap->issued),
2394 ceph_cap_string(newcaps),
2395 ceph_cap_string(revoking));
Sage Weil0eb6cd42010-08-05 13:53:18 -07002396 if (revoking & used & CEPH_CAP_FILE_BUFFER)
Sage Weil3b454c42010-06-10 13:20:33 -07002397 writeback = 1; /* initiate writeback; will delay ack */
2398 else if (revoking == CEPH_CAP_FILE_CACHE &&
2399 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2400 queue_invalidate)
2401 ; /* do nothing yet, invalidation will be queued */
2402 else if (cap == ci->i_auth_cap)
2403 check_caps = 1; /* check auth cap only */
2404 else
2405 check_caps = 2; /* check all caps */
Sage Weila8599bd2009-10-06 11:31:12 -07002406 cap->issued = newcaps;
Sage Weil978097c2010-03-08 15:27:53 -08002407 cap->implemented |= newcaps;
Sage Weila8599bd2009-10-06 11:31:12 -07002408 } else if (cap->issued == newcaps) {
2409 dout("caps unchanged: %s -> %s\n",
2410 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2411 } else {
2412 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2413 ceph_cap_string(newcaps));
2414 cap->issued = newcaps;
2415 cap->implemented |= newcaps; /* add bits only, to
2416 * avoid stepping on a
2417 * pending revocation */
2418 wake = 1;
2419 }
Sage Weil978097c2010-03-08 15:27:53 -08002420 BUG_ON(cap->issued & ~cap->implemented);
Sage Weila8599bd2009-10-06 11:31:12 -07002421
2422 spin_unlock(&inode->i_lock);
Sage Weil3c6f6b72010-02-09 15:24:44 -08002423 if (writeback)
Sage Weila8599bd2009-10-06 11:31:12 -07002424 /*
2425 * queue inode for writeback: we can't actually call
2426 * filemap_write_and_wait, etc. from message handler
2427 * context.
2428 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08002429 ceph_queue_writeback(inode);
2430 if (queue_invalidate)
2431 ceph_queue_invalidate(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002432 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002433 wake_up_all(&ci->i_cap_wq);
Sage Weil15637c82010-03-16 13:42:00 -07002434
2435 if (check_caps == 1)
2436 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2437 session);
2438 else if (check_caps == 2)
2439 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2440 else
2441 mutex_unlock(&session->s_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07002442}
2443
2444/*
2445 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2446 * MDS has been safely committed.
2447 */
Sage Weil6df058c2009-12-22 11:24:33 -08002448static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07002449 struct ceph_mds_caps *m,
2450 struct ceph_mds_session *session,
2451 struct ceph_cap *cap)
2452 __releases(inode->i_lock)
2453{
2454 struct ceph_inode_info *ci = ceph_inode(inode);
Cheng Renquan640ef792010-03-26 17:40:33 +08002455 struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002456 unsigned seq = le32_to_cpu(m->seq);
2457 int dirty = le32_to_cpu(m->dirty);
2458 int cleaned = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07002459 int drop = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002460 int i;
2461
2462 for (i = 0; i < CEPH_CAP_BITS; i++)
2463 if ((dirty & (1 << i)) &&
2464 flush_tid == ci->i_cap_flush_tid[i])
2465 cleaned |= 1 << i;
2466
2467 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2468 " flushing %s -> %s\n",
2469 inode, session->s_mds, seq, ceph_cap_string(dirty),
2470 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2471 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2472
2473 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2474 goto out;
2475
Sage Weila8599bd2009-10-06 11:31:12 -07002476 ci->i_flushing_caps &= ~cleaned;
Sage Weila8599bd2009-10-06 11:31:12 -07002477
2478 spin_lock(&mdsc->cap_dirty_lock);
2479 if (ci->i_flushing_caps == 0) {
2480 list_del_init(&ci->i_flushing_item);
2481 if (!list_empty(&session->s_cap_flushing))
2482 dout(" mds%d still flushing cap on %p\n",
2483 session->s_mds,
2484 &list_entry(session->s_cap_flushing.next,
2485 struct ceph_inode_info,
2486 i_flushing_item)->vfs_inode);
2487 mdsc->num_cap_flushing--;
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002488 wake_up_all(&mdsc->cap_flushing_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002489 dout(" inode %p now !flushing\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07002490
2491 if (ci->i_dirty_caps == 0) {
2492 dout(" inode %p now clean\n", inode);
2493 BUG_ON(!list_empty(&ci->i_dirty_item));
2494 drop = 1;
Sage Weil7d8cb262010-08-24 08:44:16 -07002495 if (ci->i_wrbuffer_ref_head == 0) {
2496 BUG_ON(!ci->i_head_snapc);
2497 ceph_put_snap_context(ci->i_head_snapc);
2498 ci->i_head_snapc = NULL;
2499 }
Sage Weil76e3b392009-10-15 18:13:53 -07002500 } else {
2501 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilafcdaea2009-10-14 14:27:38 -07002502 }
Sage Weila8599bd2009-10-06 11:31:12 -07002503 }
2504 spin_unlock(&mdsc->cap_dirty_lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002505 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002506
2507out:
2508 spin_unlock(&inode->i_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07002509 if (drop)
Sage Weila8599bd2009-10-06 11:31:12 -07002510 iput(inode);
2511}
2512
2513/*
2514 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2515 * throw away our cap_snap.
2516 *
2517 * Caller hold s_mutex.
2518 */
Sage Weil6df058c2009-12-22 11:24:33 -08002519static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07002520 struct ceph_mds_caps *m,
2521 struct ceph_mds_session *session)
2522{
2523 struct ceph_inode_info *ci = ceph_inode(inode);
2524 u64 follows = le64_to_cpu(m->snap_follows);
Sage Weila8599bd2009-10-06 11:31:12 -07002525 struct ceph_cap_snap *capsnap;
2526 int drop = 0;
2527
2528 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2529 inode, ci, session->s_mds, follows);
2530
2531 spin_lock(&inode->i_lock);
2532 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2533 if (capsnap->follows == follows) {
2534 if (capsnap->flush_tid != flush_tid) {
2535 dout(" cap_snap %p follows %lld tid %lld !="
2536 " %lld\n", capsnap, follows,
2537 flush_tid, capsnap->flush_tid);
2538 break;
2539 }
2540 WARN_ON(capsnap->dirty_pages || capsnap->writing);
Sage Weil819ccbf2010-04-01 09:33:46 -07002541 dout(" removing %p cap_snap %p follows %lld\n",
2542 inode, capsnap, follows);
Sage Weila8599bd2009-10-06 11:31:12 -07002543 ceph_put_snap_context(capsnap->context);
2544 list_del(&capsnap->ci_item);
2545 list_del(&capsnap->flushing_item);
2546 ceph_put_cap_snap(capsnap);
2547 drop = 1;
2548 break;
2549 } else {
2550 dout(" skipping cap_snap %p follows %lld\n",
2551 capsnap, capsnap->follows);
2552 }
2553 }
2554 spin_unlock(&inode->i_lock);
2555 if (drop)
2556 iput(inode);
2557}
2558
2559/*
2560 * Handle TRUNC from MDS, indicating file truncation.
2561 *
2562 * caller hold s_mutex.
2563 */
2564static void handle_cap_trunc(struct inode *inode,
2565 struct ceph_mds_caps *trunc,
2566 struct ceph_mds_session *session)
2567 __releases(inode->i_lock)
2568{
2569 struct ceph_inode_info *ci = ceph_inode(inode);
2570 int mds = session->s_mds;
2571 int seq = le32_to_cpu(trunc->seq);
2572 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2573 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2574 u64 size = le64_to_cpu(trunc->size);
2575 int implemented = 0;
2576 int dirty = __ceph_caps_dirty(ci);
2577 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2578 int queue_trunc = 0;
2579
2580 issued |= implemented | dirty;
2581
2582 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2583 inode, mds, seq, truncate_size, truncate_seq);
2584 queue_trunc = ceph_fill_file_size(inode, issued,
2585 truncate_seq, truncate_size, size);
2586 spin_unlock(&inode->i_lock);
2587
2588 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -08002589 ceph_queue_vmtruncate(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002590}
2591
2592/*
2593 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2594 * different one. If we are the most recent migration we've seen (as
2595 * indicated by mseq), make note of the migrating cap bits for the
2596 * duration (until we see the corresponding IMPORT).
2597 *
2598 * caller holds s_mutex
2599 */
2600static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
Sage Weil154f42c2010-06-21 13:45:04 -07002601 struct ceph_mds_session *session,
2602 int *open_target_sessions)
Sage Weila8599bd2009-10-06 11:31:12 -07002603{
2604 struct ceph_inode_info *ci = ceph_inode(inode);
2605 int mds = session->s_mds;
2606 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2607 struct ceph_cap *cap = NULL, *t;
2608 struct rb_node *p;
2609 int remember = 1;
2610
2611 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2612 inode, ci, mds, mseq);
2613
2614 spin_lock(&inode->i_lock);
2615
2616 /* make sure we haven't seen a higher mseq */
2617 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2618 t = rb_entry(p, struct ceph_cap, ci_node);
2619 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2620 dout(" higher mseq on cap from mds%d\n",
2621 t->session->s_mds);
2622 remember = 0;
2623 }
2624 if (t->session->s_mds == mds)
2625 cap = t;
2626 }
2627
2628 if (cap) {
2629 if (remember) {
2630 /* make note */
2631 ci->i_cap_exporting_mds = mds;
2632 ci->i_cap_exporting_mseq = mseq;
2633 ci->i_cap_exporting_issued = cap->issued;
Sage Weil154f42c2010-06-21 13:45:04 -07002634
2635 /*
2636 * make sure we have open sessions with all possible
2637 * export targets, so that we get the matching IMPORT
2638 */
2639 *open_target_sessions = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002640 }
Sage Weil7c1332b2010-02-16 11:39:45 -08002641 __ceph_remove_cap(cap);
Sage Weila8599bd2009-10-06 11:31:12 -07002642 }
Sage Weil4ea00432010-03-16 10:36:40 -07002643 /* else, we already released it */
Sage Weila8599bd2009-10-06 11:31:12 -07002644
2645 spin_unlock(&inode->i_lock);
2646}
2647
2648/*
2649 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2650 * clean them up.
2651 *
2652 * caller holds s_mutex.
2653 */
2654static void handle_cap_import(struct ceph_mds_client *mdsc,
2655 struct inode *inode, struct ceph_mds_caps *im,
2656 struct ceph_mds_session *session,
2657 void *snaptrace, int snaptrace_len)
2658{
2659 struct ceph_inode_info *ci = ceph_inode(inode);
2660 int mds = session->s_mds;
2661 unsigned issued = le32_to_cpu(im->caps);
2662 unsigned wanted = le32_to_cpu(im->wanted);
2663 unsigned seq = le32_to_cpu(im->seq);
2664 unsigned mseq = le32_to_cpu(im->migrate_seq);
2665 u64 realmino = le64_to_cpu(im->realm);
2666 u64 cap_id = le64_to_cpu(im->cap_id);
2667
2668 if (ci->i_cap_exporting_mds >= 0 &&
2669 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2670 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2671 " - cleared exporting from mds%d\n",
2672 inode, ci, mds, mseq,
2673 ci->i_cap_exporting_mds);
2674 ci->i_cap_exporting_issued = 0;
2675 ci->i_cap_exporting_mseq = 0;
2676 ci->i_cap_exporting_mds = -1;
2677 } else {
2678 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2679 inode, ci, mds, mseq);
2680 }
2681
2682 down_write(&mdsc->snap_rwsem);
2683 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2684 false);
2685 downgrade_write(&mdsc->snap_rwsem);
2686 ceph_add_cap(inode, session, cap_id, -1,
2687 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2688 NULL /* no caps context */);
2689 try_flush_caps(inode, session, NULL);
2690 up_read(&mdsc->snap_rwsem);
2691}
2692
2693/*
2694 * Handle a caps message from the MDS.
2695 *
2696 * Identify the appropriate session, inode, and call the right handler
2697 * based on the cap op.
2698 */
2699void ceph_handle_caps(struct ceph_mds_session *session,
2700 struct ceph_msg *msg)
2701{
2702 struct ceph_mds_client *mdsc = session->s_mdsc;
2703 struct super_block *sb = mdsc->client->sb;
2704 struct inode *inode;
2705 struct ceph_cap *cap;
2706 struct ceph_mds_caps *h;
Sage Weil2600d2d2010-02-22 15:12:16 -08002707 int mds = session->s_mds;
Sage Weila8599bd2009-10-06 11:31:12 -07002708 int op;
Sage Weil3d7ded42010-06-09 16:47:10 -07002709 u32 seq, mseq;
Sage Weila8599bd2009-10-06 11:31:12 -07002710 struct ceph_vino vino;
2711 u64 cap_id;
2712 u64 size, max_size;
Sage Weil6df058c2009-12-22 11:24:33 -08002713 u64 tid;
Sage Weil70edb552010-03-01 13:20:50 -08002714 void *snaptrace;
Sage Weilce1fbc82010-08-02 15:09:39 -07002715 size_t snaptrace_len;
2716 void *flock;
2717 u32 flock_len;
Sage Weil154f42c2010-06-21 13:45:04 -07002718 int open_target_sessions = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002719
2720 dout("handle_caps from mds%d\n", mds);
2721
2722 /* decode */
Sage Weil6df058c2009-12-22 11:24:33 -08002723 tid = le64_to_cpu(msg->hdr.tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002724 if (msg->front.iov_len < sizeof(*h))
2725 goto bad;
2726 h = msg->front.iov_base;
2727 op = le32_to_cpu(h->op);
2728 vino.ino = le64_to_cpu(h->ino);
2729 vino.snap = CEPH_NOSNAP;
2730 cap_id = le64_to_cpu(h->cap_id);
2731 seq = le32_to_cpu(h->seq);
Sage Weil3d7ded42010-06-09 16:47:10 -07002732 mseq = le32_to_cpu(h->migrate_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07002733 size = le64_to_cpu(h->size);
2734 max_size = le64_to_cpu(h->max_size);
2735
Sage Weilce1fbc82010-08-02 15:09:39 -07002736 snaptrace = h + 1;
2737 snaptrace_len = le32_to_cpu(h->snap_trace_len);
2738
2739 if (le16_to_cpu(msg->hdr.version) >= 2) {
2740 void *p, *end;
2741
2742 p = snaptrace + snaptrace_len;
2743 end = msg->front.iov_base + msg->front.iov_len;
2744 ceph_decode_32_safe(&p, end, flock_len, bad);
2745 flock = p;
2746 } else {
2747 flock = NULL;
2748 flock_len = 0;
2749 }
2750
Sage Weila8599bd2009-10-06 11:31:12 -07002751 mutex_lock(&session->s_mutex);
2752 session->s_seq++;
2753 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2754 (unsigned)seq);
2755
2756 /* lookup ino */
2757 inode = ceph_find_inode(sb, vino);
2758 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2759 vino.snap, inode);
2760 if (!inode) {
2761 dout(" i don't have ino %llx\n", vino.ino);
Sage Weil3d7ded42010-06-09 16:47:10 -07002762
2763 if (op == CEPH_CAP_OP_IMPORT)
2764 __queue_cap_release(session, vino.ino, cap_id,
2765 mseq, seq);
2766
2767 /*
2768 * send any full release message to try to move things
2769 * along for the mds (who clearly thinks we still have this
2770 * cap).
2771 */
Sage Weilee6b2722010-06-10 12:55:52 -07002772 ceph_add_cap_releases(mdsc, session);
Sage Weil3d7ded42010-06-09 16:47:10 -07002773 ceph_send_cap_releases(mdsc, session);
Sage Weila8599bd2009-10-06 11:31:12 -07002774 goto done;
2775 }
2776
2777 /* these will work even if we don't have a cap yet */
2778 switch (op) {
2779 case CEPH_CAP_OP_FLUSHSNAP_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08002780 handle_cap_flushsnap_ack(inode, tid, h, session);
Sage Weila8599bd2009-10-06 11:31:12 -07002781 goto done;
2782
2783 case CEPH_CAP_OP_EXPORT:
Sage Weil154f42c2010-06-21 13:45:04 -07002784 handle_cap_export(inode, h, session, &open_target_sessions);
Sage Weila8599bd2009-10-06 11:31:12 -07002785 goto done;
2786
2787 case CEPH_CAP_OP_IMPORT:
2788 handle_cap_import(mdsc, inode, h, session,
Sage Weilce1fbc82010-08-02 15:09:39 -07002789 snaptrace, snaptrace_len);
Sage Weil15637c82010-03-16 13:42:00 -07002790 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY,
2791 session);
2792 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07002793 }
2794
2795 /* the rest require a cap */
2796 spin_lock(&inode->i_lock);
2797 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2798 if (!cap) {
Sage Weil9dbd4122010-06-10 13:21:20 -07002799 dout(" no cap on %p ino %llx.%llx from mds%d\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002800 inode, ceph_ino(inode), ceph_snap(inode), mds);
2801 spin_unlock(&inode->i_lock);
2802 goto done;
2803 }
2804
2805 /* note that each of these drops i_lock for us */
2806 switch (op) {
2807 case CEPH_CAP_OP_REVOKE:
2808 case CEPH_CAP_OP_GRANT:
Sage Weil15637c82010-03-16 13:42:00 -07002809 handle_cap_grant(inode, h, session, cap, msg->middle);
2810 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07002811
2812 case CEPH_CAP_OP_FLUSH_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08002813 handle_cap_flush_ack(inode, tid, h, session, cap);
Sage Weila8599bd2009-10-06 11:31:12 -07002814 break;
2815
2816 case CEPH_CAP_OP_TRUNC:
2817 handle_cap_trunc(inode, h, session);
2818 break;
2819
2820 default:
2821 spin_unlock(&inode->i_lock);
2822 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2823 ceph_cap_op_name(op));
2824 }
2825
2826done:
Sage Weil15637c82010-03-16 13:42:00 -07002827 mutex_unlock(&session->s_mutex);
2828done_unlocked:
Sage Weila8599bd2009-10-06 11:31:12 -07002829 if (inode)
2830 iput(inode);
Sage Weil154f42c2010-06-21 13:45:04 -07002831 if (open_target_sessions)
2832 ceph_mdsc_open_export_target_sessions(mdsc, session);
Sage Weila8599bd2009-10-06 11:31:12 -07002833 return;
2834
2835bad:
2836 pr_err("ceph_handle_caps: corrupt message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002837 ceph_msg_dump(msg);
Sage Weila8599bd2009-10-06 11:31:12 -07002838 return;
2839}
2840
2841/*
2842 * Delayed work handler to process end of delayed cap release LRU list.
2843 */
Sage Weilafcdaea2009-10-14 14:27:38 -07002844void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -07002845{
2846 struct ceph_inode_info *ci;
2847 int flags = CHECK_CAPS_NODELAY;
2848
Sage Weila8599bd2009-10-06 11:31:12 -07002849 dout("check_delayed_caps\n");
2850 while (1) {
2851 spin_lock(&mdsc->cap_delay_lock);
2852 if (list_empty(&mdsc->cap_delay_list))
2853 break;
2854 ci = list_first_entry(&mdsc->cap_delay_list,
2855 struct ceph_inode_info,
2856 i_cap_delay_list);
2857 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2858 time_before(jiffies, ci->i_hold_caps_max))
2859 break;
2860 list_del_init(&ci->i_cap_delay_list);
2861 spin_unlock(&mdsc->cap_delay_lock);
2862 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2863 ceph_check_caps(ci, flags, NULL);
2864 }
2865 spin_unlock(&mdsc->cap_delay_lock);
2866}
2867
2868/*
Sage Weilafcdaea2009-10-14 14:27:38 -07002869 * Flush all dirty caps to the mds
2870 */
2871void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2872{
Sage Weile9964c12010-03-01 15:16:56 -08002873 struct ceph_inode_info *ci, *nci = NULL;
2874 struct inode *inode, *ninode = NULL;
2875 struct list_head *p, *n;
Sage Weilafcdaea2009-10-14 14:27:38 -07002876
2877 dout("flush_dirty_caps\n");
2878 spin_lock(&mdsc->cap_dirty_lock);
Sage Weile9964c12010-03-01 15:16:56 -08002879 list_for_each_safe(p, n, &mdsc->cap_dirty) {
2880 if (nci) {
2881 ci = nci;
2882 inode = ninode;
2883 ci->i_ceph_flags &= ~CEPH_I_NOFLUSH;
2884 dout("flush_dirty_caps inode %p (was next inode)\n",
2885 inode);
2886 } else {
2887 ci = list_entry(p, struct ceph_inode_info,
2888 i_dirty_item);
2889 inode = igrab(&ci->vfs_inode);
2890 BUG_ON(!inode);
2891 dout("flush_dirty_caps inode %p\n", inode);
2892 }
2893 if (n != &mdsc->cap_dirty) {
2894 nci = list_entry(n, struct ceph_inode_info,
2895 i_dirty_item);
2896 ninode = igrab(&nci->vfs_inode);
2897 BUG_ON(!ninode);
2898 nci->i_ceph_flags |= CEPH_I_NOFLUSH;
2899 dout("flush_dirty_caps next inode %p, noflush\n",
2900 ninode);
2901 } else {
2902 nci = NULL;
2903 ninode = NULL;
2904 }
Sage Weilafcdaea2009-10-14 14:27:38 -07002905 spin_unlock(&mdsc->cap_dirty_lock);
2906 if (inode) {
2907 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH,
2908 NULL);
2909 iput(inode);
2910 }
2911 spin_lock(&mdsc->cap_dirty_lock);
2912 }
2913 spin_unlock(&mdsc->cap_dirty_lock);
2914}
2915
2916/*
Sage Weila8599bd2009-10-06 11:31:12 -07002917 * Drop open file reference. If we were the last open file,
2918 * we may need to release capabilities to the MDS (or schedule
2919 * their delayed release).
2920 */
2921void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2922{
2923 struct inode *inode = &ci->vfs_inode;
2924 int last = 0;
2925
2926 spin_lock(&inode->i_lock);
2927 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2928 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2929 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2930 if (--ci->i_nr_by_mode[fmode] == 0)
2931 last++;
2932 spin_unlock(&inode->i_lock);
2933
2934 if (last && ci->i_vino.snap == CEPH_NOSNAP)
2935 ceph_check_caps(ci, 0, NULL);
2936}
2937
2938/*
2939 * Helpers for embedding cap and dentry lease releases into mds
2940 * requests.
2941 *
2942 * @force is used by dentry_release (below) to force inclusion of a
2943 * record for the directory inode, even when there aren't any caps to
2944 * drop.
2945 */
2946int ceph_encode_inode_release(void **p, struct inode *inode,
2947 int mds, int drop, int unless, int force)
2948{
2949 struct ceph_inode_info *ci = ceph_inode(inode);
2950 struct ceph_cap *cap;
2951 struct ceph_mds_request_release *rel = *p;
Sage Weilec97f882010-06-24 15:12:37 -07002952 int used, dirty;
Sage Weila8599bd2009-10-06 11:31:12 -07002953 int ret = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002954
2955 spin_lock(&inode->i_lock);
Sage Weil916623d2010-03-16 15:01:07 -07002956 used = __ceph_caps_used(ci);
Sage Weilec97f882010-06-24 15:12:37 -07002957 dirty = __ceph_caps_dirty(ci);
Sage Weil916623d2010-03-16 15:01:07 -07002958
Sage Weilec97f882010-06-24 15:12:37 -07002959 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
2960 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
Sage Weil916623d2010-03-16 15:01:07 -07002961 ceph_cap_string(unless));
2962
Sage Weilec97f882010-06-24 15:12:37 -07002963 /* only drop unused, clean caps */
2964 drop &= ~(used | dirty);
Sage Weil916623d2010-03-16 15:01:07 -07002965
Sage Weila8599bd2009-10-06 11:31:12 -07002966 cap = __get_cap_for_mds(ci, mds);
2967 if (cap && __cap_is_valid(cap)) {
2968 if (force ||
2969 ((cap->issued & drop) &&
2970 (cap->issued & unless) == 0)) {
2971 if ((cap->issued & drop) &&
2972 (cap->issued & unless) == 0) {
2973 dout("encode_inode_release %p cap %p %s -> "
2974 "%s\n", inode, cap,
2975 ceph_cap_string(cap->issued),
2976 ceph_cap_string(cap->issued & ~drop));
2977 cap->issued &= ~drop;
2978 cap->implemented &= ~drop;
2979 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
2980 int wanted = __ceph_caps_wanted(ci);
2981 dout(" wanted %s -> %s (act %s)\n",
2982 ceph_cap_string(cap->mds_wanted),
2983 ceph_cap_string(cap->mds_wanted &
2984 ~wanted),
2985 ceph_cap_string(wanted));
2986 cap->mds_wanted &= wanted;
2987 }
2988 } else {
2989 dout("encode_inode_release %p cap %p %s"
2990 " (force)\n", inode, cap,
2991 ceph_cap_string(cap->issued));
2992 }
2993
2994 rel->ino = cpu_to_le64(ceph_ino(inode));
2995 rel->cap_id = cpu_to_le64(cap->cap_id);
2996 rel->seq = cpu_to_le32(cap->seq);
2997 rel->issue_seq = cpu_to_le32(cap->issue_seq),
2998 rel->mseq = cpu_to_le32(cap->mseq);
2999 rel->caps = cpu_to_le32(cap->issued);
3000 rel->wanted = cpu_to_le32(cap->mds_wanted);
3001 rel->dname_len = 0;
3002 rel->dname_seq = 0;
3003 *p += sizeof(*rel);
3004 ret = 1;
3005 } else {
3006 dout("encode_inode_release %p cap %p %s\n",
3007 inode, cap, ceph_cap_string(cap->issued));
3008 }
3009 }
3010 spin_unlock(&inode->i_lock);
3011 return ret;
3012}
3013
3014int ceph_encode_dentry_release(void **p, struct dentry *dentry,
3015 int mds, int drop, int unless)
3016{
3017 struct inode *dir = dentry->d_parent->d_inode;
3018 struct ceph_mds_request_release *rel = *p;
3019 struct ceph_dentry_info *di = ceph_dentry(dentry);
3020 int force = 0;
3021 int ret;
3022
3023 /*
3024 * force an record for the directory caps if we have a dentry lease.
3025 * this is racy (can't take i_lock and d_lock together), but it
3026 * doesn't have to be perfect; the mds will revoke anything we don't
3027 * release.
3028 */
3029 spin_lock(&dentry->d_lock);
3030 if (di->lease_session && di->lease_session->s_mds == mds)
3031 force = 1;
3032 spin_unlock(&dentry->d_lock);
3033
3034 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
3035
3036 spin_lock(&dentry->d_lock);
3037 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
3038 dout("encode_dentry_release %p mds%d seq %d\n",
3039 dentry, mds, (int)di->lease_seq);
3040 rel->dname_len = cpu_to_le32(dentry->d_name.len);
3041 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3042 *p += dentry->d_name.len;
3043 rel->dname_seq = cpu_to_le32(di->lease_seq);
Sage Weil1dadcce2010-07-23 13:54:21 -07003044 __ceph_mdsc_drop_dentry_lease(dentry);
Sage Weila8599bd2009-10-06 11:31:12 -07003045 }
3046 spin_unlock(&dentry->d_lock);
3047 return ret;
3048}