blob: da0f9b8a3bcb7c906d1beaca275921196860f325 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weila8599bd2009-10-06 11:31:12 -07002
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"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070012#include "mds_client.h"
13#include <linux/ceph/decode.h>
14#include <linux/ceph/messenger.h>
Sage Weila8599bd2009-10-06 11:31:12 -070015
16/*
17 * Capability management
18 *
19 * The Ceph metadata servers control client access to inode metadata
20 * and file data by issuing capabilities, granting clients permission
21 * to read and/or write both inode field and file data to OSDs
22 * (storage nodes). Each capability consists of a set of bits
23 * indicating which operations are allowed.
24 *
25 * If the client holds a *_SHARED cap, the client has a coherent value
26 * that can be safely read from the cached inode.
27 *
28 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
29 * client is allowed to change inode attributes (e.g., file size,
30 * mtime), note its dirty state in the ceph_cap, and asynchronously
31 * flush that metadata change to the MDS.
32 *
33 * In the event of a conflicting operation (perhaps by another
34 * client), the MDS will revoke the conflicting client capabilities.
35 *
36 * In order for a client to cache an inode, it must hold a capability
37 * with at least one MDS server. When inodes are released, release
38 * notifications are batched and periodically sent en masse to the MDS
39 * cluster to release server state.
40 */
41
42
43/*
44 * Generate readable cap strings for debugging output.
45 */
46#define MAX_CAP_STR 20
47static char cap_str[MAX_CAP_STR][40];
48static DEFINE_SPINLOCK(cap_str_lock);
49static int last_cap_str;
50
51static char *gcap_string(char *s, int c)
52{
53 if (c & CEPH_CAP_GSHARED)
54 *s++ = 's';
55 if (c & CEPH_CAP_GEXCL)
56 *s++ = 'x';
57 if (c & CEPH_CAP_GCACHE)
58 *s++ = 'c';
59 if (c & CEPH_CAP_GRD)
60 *s++ = 'r';
61 if (c & CEPH_CAP_GWR)
62 *s++ = 'w';
63 if (c & CEPH_CAP_GBUFFER)
64 *s++ = 'b';
65 if (c & CEPH_CAP_GLAZYIO)
66 *s++ = 'l';
67 return s;
68}
69
70const char *ceph_cap_string(int caps)
71{
72 int i;
73 char *s;
74 int c;
75
76 spin_lock(&cap_str_lock);
77 i = last_cap_str++;
78 if (last_cap_str == MAX_CAP_STR)
79 last_cap_str = 0;
80 spin_unlock(&cap_str_lock);
81
82 s = cap_str[i];
83
84 if (caps & CEPH_CAP_PIN)
85 *s++ = 'p';
86
87 c = (caps >> CEPH_CAP_SAUTH) & 3;
88 if (c) {
89 *s++ = 'A';
90 s = gcap_string(s, c);
91 }
92
93 c = (caps >> CEPH_CAP_SLINK) & 3;
94 if (c) {
95 *s++ = 'L';
96 s = gcap_string(s, c);
97 }
98
99 c = (caps >> CEPH_CAP_SXATTR) & 3;
100 if (c) {
101 *s++ = 'X';
102 s = gcap_string(s, c);
103 }
104
105 c = caps >> CEPH_CAP_SFILE;
106 if (c) {
107 *s++ = 'F';
108 s = gcap_string(s, c);
109 }
110
111 if (s == cap_str[i])
112 *s++ = '-';
113 *s = 0;
114 return cap_str[i];
115}
116
Yehuda Sadeh37151662010-06-17 16:16:12 -0700117void ceph_caps_init(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -0700118{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700119 INIT_LIST_HEAD(&mdsc->caps_list);
120 spin_lock_init(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700121}
122
Yehuda Sadeh37151662010-06-17 16:16:12 -0700123void ceph_caps_finalize(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -0700124{
125 struct ceph_cap *cap;
126
Yehuda Sadeh37151662010-06-17 16:16:12 -0700127 spin_lock(&mdsc->caps_list_lock);
128 while (!list_empty(&mdsc->caps_list)) {
129 cap = list_first_entry(&mdsc->caps_list,
130 struct ceph_cap, caps_item);
Sage Weila8599bd2009-10-06 11:31:12 -0700131 list_del(&cap->caps_item);
132 kmem_cache_free(ceph_cap_cachep, cap);
133 }
Yehuda Sadeh37151662010-06-17 16:16:12 -0700134 mdsc->caps_total_count = 0;
135 mdsc->caps_avail_count = 0;
136 mdsc->caps_use_count = 0;
137 mdsc->caps_reserve_count = 0;
138 mdsc->caps_min_count = 0;
139 spin_unlock(&mdsc->caps_list_lock);
Sage Weil85ccce42010-02-17 10:02:43 -0800140}
141
Yehuda Sadeh37151662010-06-17 16:16:12 -0700142void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
Sage Weil85ccce42010-02-17 10:02:43 -0800143{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700144 spin_lock(&mdsc->caps_list_lock);
145 mdsc->caps_min_count += delta;
146 BUG_ON(mdsc->caps_min_count < 0);
147 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700148}
149
Yehuda Sadeh37151662010-06-17 16:16:12 -0700150int ceph_reserve_caps(struct ceph_mds_client *mdsc,
151 struct ceph_cap_reservation *ctx, int need)
Sage Weila8599bd2009-10-06 11:31:12 -0700152{
153 int i;
154 struct ceph_cap *cap;
155 int have;
156 int alloc = 0;
157 LIST_HEAD(newcaps);
158 int ret = 0;
159
160 dout("reserve caps ctx=%p need=%d\n", ctx, need);
161
162 /* first reserve any caps that are already allocated */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700163 spin_lock(&mdsc->caps_list_lock);
164 if (mdsc->caps_avail_count >= need)
Sage Weila8599bd2009-10-06 11:31:12 -0700165 have = need;
166 else
Yehuda Sadeh37151662010-06-17 16:16:12 -0700167 have = mdsc->caps_avail_count;
168 mdsc->caps_avail_count -= have;
169 mdsc->caps_reserve_count += have;
170 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
171 mdsc->caps_reserve_count +
172 mdsc->caps_avail_count);
173 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700174
175 for (i = have; i < need; i++) {
176 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
177 if (!cap) {
178 ret = -ENOMEM;
179 goto out_alloc_count;
180 }
181 list_add(&cap->caps_item, &newcaps);
182 alloc++;
183 }
184 BUG_ON(have + alloc != need);
185
Yehuda Sadeh37151662010-06-17 16:16:12 -0700186 spin_lock(&mdsc->caps_list_lock);
187 mdsc->caps_total_count += alloc;
188 mdsc->caps_reserve_count += alloc;
189 list_splice(&newcaps, &mdsc->caps_list);
Sage Weila8599bd2009-10-06 11:31:12 -0700190
Yehuda Sadeh37151662010-06-17 16:16:12 -0700191 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
192 mdsc->caps_reserve_count +
193 mdsc->caps_avail_count);
194 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700195
196 ctx->count = need;
197 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700198 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
199 mdsc->caps_reserve_count, mdsc->caps_avail_count);
Sage Weila8599bd2009-10-06 11:31:12 -0700200 return 0;
201
202out_alloc_count:
203 /* we didn't manage to reserve as much as we needed */
204 pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
205 ctx, need, have);
206 return ret;
207}
208
Yehuda Sadeh37151662010-06-17 16:16:12 -0700209int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
210 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700211{
212 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
213 if (ctx->count) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700214 spin_lock(&mdsc->caps_list_lock);
215 BUG_ON(mdsc->caps_reserve_count < ctx->count);
216 mdsc->caps_reserve_count -= ctx->count;
217 mdsc->caps_avail_count += ctx->count;
Sage Weila8599bd2009-10-06 11:31:12 -0700218 ctx->count = 0;
219 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700220 mdsc->caps_total_count, mdsc->caps_use_count,
221 mdsc->caps_reserve_count, mdsc->caps_avail_count);
222 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
223 mdsc->caps_reserve_count +
224 mdsc->caps_avail_count);
225 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700226 }
227 return 0;
228}
229
Yehuda Sadeh37151662010-06-17 16:16:12 -0700230static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc,
231 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700232{
233 struct ceph_cap *cap = NULL;
234
235 /* temporary, until we do something about cap import/export */
Sage Weil443b3762010-06-29 09:28:39 -0700236 if (!ctx) {
237 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
238 if (cap) {
Yan, Zheng4d1d0532012-11-03 10:32:37 +0800239 spin_lock(&mdsc->caps_list_lock);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700240 mdsc->caps_use_count++;
241 mdsc->caps_total_count++;
Yan, Zheng4d1d0532012-11-03 10:32:37 +0800242 spin_unlock(&mdsc->caps_list_lock);
Sage Weil443b3762010-06-29 09:28:39 -0700243 }
244 return cap;
245 }
Sage Weila8599bd2009-10-06 11:31:12 -0700246
Yehuda Sadeh37151662010-06-17 16:16:12 -0700247 spin_lock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700248 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700249 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
250 mdsc->caps_reserve_count, mdsc->caps_avail_count);
Sage Weila8599bd2009-10-06 11:31:12 -0700251 BUG_ON(!ctx->count);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700252 BUG_ON(ctx->count > mdsc->caps_reserve_count);
253 BUG_ON(list_empty(&mdsc->caps_list));
Sage Weila8599bd2009-10-06 11:31:12 -0700254
255 ctx->count--;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700256 mdsc->caps_reserve_count--;
257 mdsc->caps_use_count++;
Sage Weila8599bd2009-10-06 11:31:12 -0700258
Yehuda Sadeh37151662010-06-17 16:16:12 -0700259 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
Sage Weila8599bd2009-10-06 11:31:12 -0700260 list_del(&cap->caps_item);
261
Yehuda Sadeh37151662010-06-17 16:16:12 -0700262 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
263 mdsc->caps_reserve_count + mdsc->caps_avail_count);
264 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700265 return cap;
266}
267
Yehuda Sadeh37151662010-06-17 16:16:12 -0700268void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700269{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700270 spin_lock(&mdsc->caps_list_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800271 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700272 cap, mdsc->caps_total_count, mdsc->caps_use_count,
273 mdsc->caps_reserve_count, mdsc->caps_avail_count);
274 mdsc->caps_use_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700275 /*
Sage Weil85ccce42010-02-17 10:02:43 -0800276 * Keep some preallocated caps around (ceph_min_count), to
277 * avoid lots of free/alloc churn.
Sage Weila8599bd2009-10-06 11:31:12 -0700278 */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700279 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
280 mdsc->caps_min_count) {
281 mdsc->caps_total_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700282 kmem_cache_free(ceph_cap_cachep, cap);
283 } else {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700284 mdsc->caps_avail_count++;
285 list_add(&cap->caps_item, &mdsc->caps_list);
Sage Weila8599bd2009-10-06 11:31:12 -0700286 }
287
Yehuda Sadeh37151662010-06-17 16:16:12 -0700288 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
289 mdsc->caps_reserve_count + mdsc->caps_avail_count);
290 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700291}
292
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700293void ceph_reservation_status(struct ceph_fs_client *fsc,
Sage Weil85ccce42010-02-17 10:02:43 -0800294 int *total, int *avail, int *used, int *reserved,
295 int *min)
Sage Weila8599bd2009-10-06 11:31:12 -0700296{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700297 struct ceph_mds_client *mdsc = fsc->mdsc;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700298
Sage Weila8599bd2009-10-06 11:31:12 -0700299 if (total)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700300 *total = mdsc->caps_total_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700301 if (avail)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700302 *avail = mdsc->caps_avail_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700303 if (used)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700304 *used = mdsc->caps_use_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700305 if (reserved)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700306 *reserved = mdsc->caps_reserve_count;
Sage Weil85ccce42010-02-17 10:02:43 -0800307 if (min)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700308 *min = mdsc->caps_min_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700309}
310
311/*
312 * Find ceph_cap for given mds, if any.
313 *
Sage Weilbe655592011-11-30 09:47:09 -0800314 * Called with i_ceph_lock held.
Sage Weila8599bd2009-10-06 11:31:12 -0700315 */
316static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
317{
318 struct ceph_cap *cap;
319 struct rb_node *n = ci->i_caps.rb_node;
320
321 while (n) {
322 cap = rb_entry(n, struct ceph_cap, ci_node);
323 if (mds < cap->mds)
324 n = n->rb_left;
325 else if (mds > cap->mds)
326 n = n->rb_right;
327 else
328 return cap;
329 }
330 return NULL;
331}
332
Greg Farnum2bc50252010-06-30 12:44:34 -0700333struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
334{
335 struct ceph_cap *cap;
336
Sage Weilbe655592011-11-30 09:47:09 -0800337 spin_lock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700338 cap = __get_cap_for_mds(ci, mds);
Sage Weilbe655592011-11-30 09:47:09 -0800339 spin_unlock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700340 return cap;
341}
342
Sage Weila8599bd2009-10-06 11:31:12 -0700343/*
Sage Weil33caad32010-05-26 14:31:27 -0700344 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
Sage Weila8599bd2009-10-06 11:31:12 -0700345 */
Sage Weilca81f3f2010-06-10 14:21:36 -0700346static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
Sage Weila8599bd2009-10-06 11:31:12 -0700347{
348 struct ceph_cap *cap;
349 int mds = -1;
350 struct rb_node *p;
351
Sage Weil33caad32010-05-26 14:31:27 -0700352 /* prefer mds with WR|BUFFER|EXCL caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700353 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
354 cap = rb_entry(p, struct ceph_cap, ci_node);
355 mds = cap->mds;
Sage Weila8599bd2009-10-06 11:31:12 -0700356 if (cap->issued & (CEPH_CAP_FILE_WR |
357 CEPH_CAP_FILE_BUFFER |
358 CEPH_CAP_FILE_EXCL))
359 break;
360 }
361 return mds;
362}
363
364int ceph_get_cap_mds(struct inode *inode)
365{
Sage Weilbe655592011-11-30 09:47:09 -0800366 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -0700367 int mds;
Sage Weilbe655592011-11-30 09:47:09 -0800368 spin_lock(&ci->i_ceph_lock);
Sage Weilca81f3f2010-06-10 14:21:36 -0700369 mds = __ceph_get_cap_mds(ceph_inode(inode));
Sage Weilbe655592011-11-30 09:47:09 -0800370 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700371 return mds;
372}
373
374/*
Sage Weilbe655592011-11-30 09:47:09 -0800375 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700376 */
377static void __insert_cap_node(struct ceph_inode_info *ci,
378 struct ceph_cap *new)
379{
380 struct rb_node **p = &ci->i_caps.rb_node;
381 struct rb_node *parent = NULL;
382 struct ceph_cap *cap = NULL;
383
384 while (*p) {
385 parent = *p;
386 cap = rb_entry(parent, struct ceph_cap, ci_node);
387 if (new->mds < cap->mds)
388 p = &(*p)->rb_left;
389 else if (new->mds > cap->mds)
390 p = &(*p)->rb_right;
391 else
392 BUG();
393 }
394
395 rb_link_node(&new->ci_node, parent, p);
396 rb_insert_color(&new->ci_node, &ci->i_caps);
397}
398
399/*
400 * (re)set cap hold timeouts, which control the delayed release
401 * of unused caps back to the MDS. Should be called on cap use.
402 */
403static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
404 struct ceph_inode_info *ci)
405{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700406 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
Sage Weila8599bd2009-10-06 11:31:12 -0700407
408 ci->i_hold_caps_min = round_jiffies(jiffies +
409 ma->caps_wanted_delay_min * HZ);
410 ci->i_hold_caps_max = round_jiffies(jiffies +
411 ma->caps_wanted_delay_max * HZ);
412 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
413 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
414}
415
416/*
417 * (Re)queue cap at the end of the delayed cap release list.
418 *
419 * If I_FLUSH is set, leave the inode at the front of the list.
420 *
Sage Weilbe655592011-11-30 09:47:09 -0800421 * Caller holds i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -0700422 * -> we take mdsc->cap_delay_lock
423 */
424static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
425 struct ceph_inode_info *ci)
426{
427 __cap_set_timeouts(mdsc, ci);
428 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
429 ci->i_ceph_flags, ci->i_hold_caps_max);
430 if (!mdsc->stopping) {
431 spin_lock(&mdsc->cap_delay_lock);
432 if (!list_empty(&ci->i_cap_delay_list)) {
433 if (ci->i_ceph_flags & CEPH_I_FLUSH)
434 goto no_change;
435 list_del_init(&ci->i_cap_delay_list);
436 }
437 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
438no_change:
439 spin_unlock(&mdsc->cap_delay_lock);
440 }
441}
442
443/*
444 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
445 * indicating we should send a cap message to flush dirty metadata
446 * asap, and move to the front of the delayed cap list.
447 */
448static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
449 struct ceph_inode_info *ci)
450{
451 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
452 spin_lock(&mdsc->cap_delay_lock);
453 ci->i_ceph_flags |= CEPH_I_FLUSH;
454 if (!list_empty(&ci->i_cap_delay_list))
455 list_del_init(&ci->i_cap_delay_list);
456 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
457 spin_unlock(&mdsc->cap_delay_lock);
458}
459
460/*
461 * Cancel delayed work on cap.
462 *
Sage Weilbe655592011-11-30 09:47:09 -0800463 * Caller must hold i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700464 */
465static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
466 struct ceph_inode_info *ci)
467{
468 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
469 if (list_empty(&ci->i_cap_delay_list))
470 return;
471 spin_lock(&mdsc->cap_delay_lock);
472 list_del_init(&ci->i_cap_delay_list);
473 spin_unlock(&mdsc->cap_delay_lock);
474}
475
476/*
477 * Common issue checks for add_cap, handle_cap_grant.
478 */
479static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
480 unsigned issued)
481{
482 unsigned had = __ceph_caps_issued(ci, NULL);
483
484 /*
485 * Each time we receive FILE_CACHE anew, we increment
486 * i_rdcache_gen.
487 */
Sage Weil29625072010-05-27 10:40:43 -0700488 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
489 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
Sage Weila8599bd2009-10-06 11:31:12 -0700490 ci->i_rdcache_gen++;
491
492 /*
Yan, Zheng2f276c52013-03-13 19:44:32 +0800493 * if we are newly issued FILE_SHARED, mark dir not complete; we
Sage Weila8599bd2009-10-06 11:31:12 -0700494 * don't know what happened to this directory while we didn't
495 * have the cap.
496 */
497 if ((issued & CEPH_CAP_FILE_SHARED) &&
498 (had & CEPH_CAP_FILE_SHARED) == 0) {
499 ci->i_shared_gen++;
Yan, Zhenga8673d62013-02-18 16:38:14 +0800500 if (S_ISDIR(ci->vfs_inode.i_mode)) {
501 dout(" marking %p NOT complete\n", &ci->vfs_inode);
Yan, Zheng2f276c52013-03-13 19:44:32 +0800502 __ceph_dir_clear_complete(ci);
Yan, Zhenga8673d62013-02-18 16:38:14 +0800503 }
Sage Weila8599bd2009-10-06 11:31:12 -0700504 }
505}
506
507/*
508 * Add a capability under the given MDS session.
509 *
510 * Caller should hold session snap_rwsem (read) and s_mutex.
511 *
512 * @fmode is the open file mode, if we are opening a file, otherwise
513 * it is < 0. (This is so we can atomically add the cap and add an
514 * open file reference to it.)
515 */
516int ceph_add_cap(struct inode *inode,
517 struct ceph_mds_session *session, u64 cap_id,
518 int fmode, unsigned issued, unsigned wanted,
519 unsigned seq, unsigned mseq, u64 realmino, int flags,
520 struct ceph_cap_reservation *caps_reservation)
521{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700522 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -0700523 struct ceph_inode_info *ci = ceph_inode(inode);
524 struct ceph_cap *new_cap = NULL;
525 struct ceph_cap *cap;
526 int mds = session->s_mds;
527 int actual_wanted;
528
529 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
530 session->s_mds, cap_id, ceph_cap_string(issued), seq);
531
532 /*
533 * If we are opening the file, include file mode wanted bits
534 * in wanted.
535 */
536 if (fmode >= 0)
537 wanted |= ceph_caps_for_mode(fmode);
538
539retry:
Sage Weilbe655592011-11-30 09:47:09 -0800540 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700541 cap = __get_cap_for_mds(ci, mds);
542 if (!cap) {
543 if (new_cap) {
544 cap = new_cap;
545 new_cap = NULL;
546 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800547 spin_unlock(&ci->i_ceph_lock);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700548 new_cap = get_cap(mdsc, caps_reservation);
Sage Weila8599bd2009-10-06 11:31:12 -0700549 if (new_cap == NULL)
550 return -ENOMEM;
551 goto retry;
552 }
553
554 cap->issued = 0;
555 cap->implemented = 0;
556 cap->mds = mds;
557 cap->mds_wanted = 0;
Yan, Zheng964266c2013-02-27 09:26:09 +0800558 cap->mseq = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700559
560 cap->ci = ci;
561 __insert_cap_node(ci, cap);
562
563 /* clear out old exporting info? (i.e. on cap import) */
564 if (ci->i_cap_exporting_mds == mds) {
565 ci->i_cap_exporting_issued = 0;
566 ci->i_cap_exporting_mseq = 0;
567 ci->i_cap_exporting_mds = -1;
568 }
569
570 /* add to session cap list */
571 cap->session = session;
572 spin_lock(&session->s_cap_lock);
573 list_add_tail(&cap->session_caps, &session->s_caps);
574 session->s_nr_caps++;
575 spin_unlock(&session->s_cap_lock);
Sage Weil35403032011-05-12 15:13:23 -0700576 } else if (new_cap)
577 ceph_put_cap(mdsc, new_cap);
Sage Weila8599bd2009-10-06 11:31:12 -0700578
579 if (!ci->i_snap_realm) {
580 /*
581 * add this inode to the appropriate snap realm
582 */
583 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
584 realmino);
585 if (realm) {
586 ceph_get_snap_realm(mdsc, realm);
587 spin_lock(&realm->inodes_with_caps_lock);
588 ci->i_snap_realm = realm;
589 list_add(&ci->i_snap_realm_item,
590 &realm->inodes_with_caps);
591 spin_unlock(&realm->inodes_with_caps_lock);
592 } else {
593 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
594 realmino);
Sage Weilb8cd07e2010-07-16 12:00:02 -0700595 WARN_ON(!realm);
Sage Weila8599bd2009-10-06 11:31:12 -0700596 }
597 }
598
599 __check_cap_issue(ci, cap, issued);
600
601 /*
602 * If we are issued caps we don't want, or the mds' wanted
603 * value appears to be off, queue a check so we'll release
604 * later and/or update the mds wanted value.
605 */
606 actual_wanted = __ceph_caps_wanted(ci);
607 if ((wanted & ~actual_wanted) ||
608 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
609 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
610 ceph_cap_string(issued), ceph_cap_string(wanted),
611 ceph_cap_string(actual_wanted));
612 __cap_delay_requeue(mdsc, ci);
613 }
614
615 if (flags & CEPH_CAP_FLAG_AUTH)
616 ci->i_auth_cap = cap;
Yan, Zheng8a92a112013-01-04 14:28:07 +0800617 else if (ci->i_auth_cap == cap) {
Sage Weila8599bd2009-10-06 11:31:12 -0700618 ci->i_auth_cap = NULL;
Yan, Zheng8a92a112013-01-04 14:28:07 +0800619 spin_lock(&mdsc->cap_dirty_lock);
620 if (!list_empty(&ci->i_dirty_item)) {
621 dout(" moving %p to cap_dirty_migrating\n", inode);
622 list_move(&ci->i_dirty_item,
623 &mdsc->cap_dirty_migrating);
624 }
625 spin_unlock(&mdsc->cap_dirty_lock);
626 }
Sage Weila8599bd2009-10-06 11:31:12 -0700627
628 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
629 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
630 ceph_cap_string(issued|cap->issued), seq, mds);
631 cap->cap_id = cap_id;
632 cap->issued = issued;
633 cap->implemented |= issued;
Yan, Zheng964266c2013-02-27 09:26:09 +0800634 if (mseq > cap->mseq)
635 cap->mds_wanted = wanted;
636 else
637 cap->mds_wanted |= wanted;
Sage Weila8599bd2009-10-06 11:31:12 -0700638 cap->seq = seq;
639 cap->issue_seq = seq;
640 cap->mseq = mseq;
Sage Weil685f9a5d2009-11-09 12:05:48 -0800641 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700642
643 if (fmode >= 0)
644 __ceph_get_fmode(ci, fmode);
Sage Weilbe655592011-11-30 09:47:09 -0800645 spin_unlock(&ci->i_ceph_lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700646 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -0700647 return 0;
648}
649
650/*
651 * Return true if cap has not timed out and belongs to the current
652 * generation of the MDS session (i.e. has not gone 'stale' due to
653 * us losing touch with the mds).
654 */
655static int __cap_is_valid(struct ceph_cap *cap)
656{
657 unsigned long ttl;
Sage Weilcdac8302009-11-10 16:02:23 -0800658 u32 gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700659
Alex Elderd8fb02a2012-01-12 17:48:10 -0800660 spin_lock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700661 gen = cap->session->s_cap_gen;
662 ttl = cap->session->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800663 spin_unlock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700664
Sage Weil685f9a5d2009-11-09 12:05:48 -0800665 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
Sage Weila8599bd2009-10-06 11:31:12 -0700666 dout("__cap_is_valid %p cap %p issued %s "
667 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
Sage Weil685f9a5d2009-11-09 12:05:48 -0800668 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
Sage Weila8599bd2009-10-06 11:31:12 -0700669 return 0;
670 }
671
672 return 1;
673}
674
675/*
676 * Return set of valid cap bits issued to us. Note that caps time
677 * out, and may be invalidated in bulk if the client session times out
678 * and session->s_cap_gen is bumped.
679 */
680int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
681{
Sage Weil7af8f1e2010-03-01 15:17:34 -0800682 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
Sage Weila8599bd2009-10-06 11:31:12 -0700683 struct ceph_cap *cap;
684 struct rb_node *p;
685
686 if (implemented)
687 *implemented = 0;
688 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
689 cap = rb_entry(p, struct ceph_cap, ci_node);
690 if (!__cap_is_valid(cap))
691 continue;
692 dout("__ceph_caps_issued %p cap %p issued %s\n",
693 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
694 have |= cap->issued;
695 if (implemented)
696 *implemented |= cap->implemented;
697 }
698 return have;
699}
700
701/*
702 * Get cap bits issued by caps other than @ocap
703 */
704int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
705{
706 int have = ci->i_snap_caps;
707 struct ceph_cap *cap;
708 struct rb_node *p;
709
710 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
711 cap = rb_entry(p, struct ceph_cap, ci_node);
712 if (cap == ocap)
713 continue;
714 if (!__cap_is_valid(cap))
715 continue;
716 have |= cap->issued;
717 }
718 return have;
719}
720
721/*
722 * Move a cap to the end of the LRU (oldest caps at list head, newest
723 * at list tail).
724 */
725static void __touch_cap(struct ceph_cap *cap)
726{
727 struct ceph_mds_session *s = cap->session;
728
Sage Weila8599bd2009-10-06 11:31:12 -0700729 spin_lock(&s->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800730 if (s->s_cap_iterator == NULL) {
Sage Weil5dacf092009-12-21 20:40:34 -0800731 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
732 s->s_mds);
733 list_move_tail(&cap->session_caps, &s->s_caps);
734 } else {
735 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
736 &cap->ci->vfs_inode, cap, s->s_mds);
737 }
Sage Weila8599bd2009-10-06 11:31:12 -0700738 spin_unlock(&s->s_cap_lock);
739}
740
741/*
742 * Check if we hold the given mask. If so, move the cap(s) to the
743 * front of their respective LRUs. (This is the preferred way for
744 * callers to check for caps they want.)
745 */
746int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
747{
748 struct ceph_cap *cap;
749 struct rb_node *p;
750 int have = ci->i_snap_caps;
751
752 if ((have & mask) == mask) {
753 dout("__ceph_caps_issued_mask %p snap issued %s"
754 " (mask %s)\n", &ci->vfs_inode,
755 ceph_cap_string(have),
756 ceph_cap_string(mask));
757 return 1;
758 }
759
760 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
761 cap = rb_entry(p, struct ceph_cap, ci_node);
762 if (!__cap_is_valid(cap))
763 continue;
764 if ((cap->issued & mask) == mask) {
765 dout("__ceph_caps_issued_mask %p cap %p issued %s"
766 " (mask %s)\n", &ci->vfs_inode, cap,
767 ceph_cap_string(cap->issued),
768 ceph_cap_string(mask));
769 if (touch)
770 __touch_cap(cap);
771 return 1;
772 }
773
774 /* does a combination of caps satisfy mask? */
775 have |= cap->issued;
776 if ((have & mask) == mask) {
777 dout("__ceph_caps_issued_mask %p combo issued %s"
778 " (mask %s)\n", &ci->vfs_inode,
779 ceph_cap_string(cap->issued),
780 ceph_cap_string(mask));
781 if (touch) {
782 struct rb_node *q;
783
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300784 /* touch this + preceding caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700785 __touch_cap(cap);
786 for (q = rb_first(&ci->i_caps); q != p;
787 q = rb_next(q)) {
788 cap = rb_entry(q, struct ceph_cap,
789 ci_node);
790 if (!__cap_is_valid(cap))
791 continue;
792 __touch_cap(cap);
793 }
794 }
795 return 1;
796 }
797 }
798
799 return 0;
800}
801
802/*
803 * Return true if mask caps are currently being revoked by an MDS.
804 */
805int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
806{
807 struct inode *inode = &ci->vfs_inode;
808 struct ceph_cap *cap;
809 struct rb_node *p;
810 int ret = 0;
811
Sage Weilbe655592011-11-30 09:47:09 -0800812 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700813 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
814 cap = rb_entry(p, struct ceph_cap, ci_node);
815 if (__cap_is_valid(cap) &&
816 (cap->implemented & ~cap->issued & mask)) {
817 ret = 1;
818 break;
819 }
820 }
Sage Weilbe655592011-11-30 09:47:09 -0800821 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700822 dout("ceph_caps_revoking %p %s = %d\n", inode,
823 ceph_cap_string(mask), ret);
824 return ret;
825}
826
827int __ceph_caps_used(struct ceph_inode_info *ci)
828{
829 int used = 0;
830 if (ci->i_pin_ref)
831 used |= CEPH_CAP_PIN;
832 if (ci->i_rd_ref)
833 used |= CEPH_CAP_FILE_RD;
Sage Weila43fb732010-09-17 09:54:08 -0700834 if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages)
Sage Weila8599bd2009-10-06 11:31:12 -0700835 used |= CEPH_CAP_FILE_CACHE;
836 if (ci->i_wr_ref)
837 used |= CEPH_CAP_FILE_WR;
Henry C Changd3d07202011-05-11 10:29:54 +0000838 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
Sage Weila8599bd2009-10-06 11:31:12 -0700839 used |= CEPH_CAP_FILE_BUFFER;
840 return used;
841}
842
843/*
844 * wanted, by virtue of open file modes
845 */
846int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
847{
848 int want = 0;
849 int mode;
Sage Weil33caad32010-05-26 14:31:27 -0700850 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
Sage Weila8599bd2009-10-06 11:31:12 -0700851 if (ci->i_nr_by_mode[mode])
852 want |= ceph_caps_for_mode(mode);
853 return want;
854}
855
856/*
857 * Return caps we have registered with the MDS(s) as 'wanted'.
858 */
859int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
860{
861 struct ceph_cap *cap;
862 struct rb_node *p;
863 int mds_wanted = 0;
864
865 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
866 cap = rb_entry(p, struct ceph_cap, ci_node);
867 if (!__cap_is_valid(cap))
868 continue;
869 mds_wanted |= cap->mds_wanted;
870 }
871 return mds_wanted;
872}
873
874/*
Sage Weilbe655592011-11-30 09:47:09 -0800875 * called under i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -0700876 */
877static int __ceph_is_any_caps(struct ceph_inode_info *ci)
878{
879 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
880}
881
882/*
Sage Weilf818a732010-05-11 20:56:31 -0700883 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
884 *
Sage Weilbe655592011-11-30 09:47:09 -0800885 * caller should hold i_ceph_lock.
Sage Weila6369742010-02-22 13:59:00 -0800886 * caller will not hold session s_mutex if called from destroy_inode.
Sage Weila8599bd2009-10-06 11:31:12 -0700887 */
Sage Weil7c1332b2010-02-16 11:39:45 -0800888void __ceph_remove_cap(struct ceph_cap *cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700889{
890 struct ceph_mds_session *session = cap->session;
891 struct ceph_inode_info *ci = cap->ci;
Cheng Renquan640ef792010-03-26 17:40:33 +0800892 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700893 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weilf818a732010-05-11 20:56:31 -0700894 int removed = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700895
896 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
897
Sage Weil7c1332b2010-02-16 11:39:45 -0800898 /* remove from session list */
899 spin_lock(&session->s_cap_lock);
900 if (session->s_cap_iterator == cap) {
901 /* not yet, we are iterating over this very cap */
902 dout("__ceph_remove_cap delaying %p removal from session %p\n",
903 cap, cap->session);
904 } else {
905 list_del_init(&cap->session_caps);
906 session->s_nr_caps--;
907 cap->session = NULL;
Sage Weilf818a732010-05-11 20:56:31 -0700908 removed = 1;
Sage Weil7c1332b2010-02-16 11:39:45 -0800909 }
Sage Weilf818a732010-05-11 20:56:31 -0700910 /* protect backpointer with s_cap_lock: see iterate_session_caps */
911 cap->ci = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -0800912 spin_unlock(&session->s_cap_lock);
913
Sage Weilf818a732010-05-11 20:56:31 -0700914 /* remove from inode list */
915 rb_erase(&cap->ci_node, &ci->i_caps);
916 if (ci->i_auth_cap == cap)
917 ci->i_auth_cap = NULL;
918
919 if (removed)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700920 ceph_put_cap(mdsc, cap);
Sage Weila8599bd2009-10-06 11:31:12 -0700921
922 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
923 struct ceph_snap_realm *realm = ci->i_snap_realm;
924 spin_lock(&realm->inodes_with_caps_lock);
925 list_del_init(&ci->i_snap_realm_item);
926 ci->i_snap_realm_counter++;
927 ci->i_snap_realm = NULL;
928 spin_unlock(&realm->inodes_with_caps_lock);
929 ceph_put_snap_realm(mdsc, realm);
930 }
931 if (!__ceph_is_any_real_caps(ci))
932 __cap_delay_cancel(mdsc, ci);
933}
934
935/*
936 * Build and send a cap message to the given MDS.
937 *
938 * Caller should be holding s_mutex.
939 */
940static int send_cap_msg(struct ceph_mds_session *session,
941 u64 ino, u64 cid, int op,
942 int caps, int wanted, int dirty,
943 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
944 u64 size, u64 max_size,
945 struct timespec *mtime, struct timespec *atime,
946 u64 time_warp_seq,
Eric W. Biederman05cb11c2013-01-31 02:56:19 -0800947 kuid_t uid, kgid_t gid, umode_t mode,
Sage Weila8599bd2009-10-06 11:31:12 -0700948 u64 xattr_version,
949 struct ceph_buffer *xattrs_buf,
950 u64 follows)
951{
952 struct ceph_mds_caps *fc;
953 struct ceph_msg *msg;
954
955 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
956 " seq %u/%u mseq %u follows %lld size %llu/%llu"
957 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
958 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
959 ceph_cap_string(dirty),
960 seq, issue_seq, mseq, follows, size, max_size,
961 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
962
Sage Weilb61c2762011-08-09 15:03:46 -0700963 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -0700964 if (!msg)
965 return -ENOMEM;
Sage Weila8599bd2009-10-06 11:31:12 -0700966
Sage Weil6df058c2009-12-22 11:24:33 -0800967 msg->hdr.tid = cpu_to_le64(flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -0700968
Sage Weil6df058c2009-12-22 11:24:33 -0800969 fc = msg->front.iov_base;
Sage Weila8599bd2009-10-06 11:31:12 -0700970 memset(fc, 0, sizeof(*fc));
971
972 fc->cap_id = cpu_to_le64(cid);
973 fc->op = cpu_to_le32(op);
974 fc->seq = cpu_to_le32(seq);
Sage Weila8599bd2009-10-06 11:31:12 -0700975 fc->issue_seq = cpu_to_le32(issue_seq);
976 fc->migrate_seq = cpu_to_le32(mseq);
977 fc->caps = cpu_to_le32(caps);
978 fc->wanted = cpu_to_le32(wanted);
979 fc->dirty = cpu_to_le32(dirty);
980 fc->ino = cpu_to_le64(ino);
981 fc->snap_follows = cpu_to_le64(follows);
982
983 fc->size = cpu_to_le64(size);
984 fc->max_size = cpu_to_le64(max_size);
985 if (mtime)
986 ceph_encode_timespec(&fc->mtime, mtime);
987 if (atime)
988 ceph_encode_timespec(&fc->atime, atime);
989 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
990
Eric W. Biederman05cb11c2013-01-31 02:56:19 -0800991 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid));
992 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid));
Sage Weila8599bd2009-10-06 11:31:12 -0700993 fc->mode = cpu_to_le32(mode);
994
995 fc->xattr_version = cpu_to_le64(xattr_version);
996 if (xattrs_buf) {
997 msg->middle = ceph_buffer_get(xattrs_buf);
998 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
999 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
1000 }
1001
1002 ceph_con_send(&session->s_con, msg);
1003 return 0;
1004}
1005
Yan, Zhengd40ee0d2013-02-18 13:43:43 +08001006void __queue_cap_release(struct ceph_mds_session *session,
1007 u64 ino, u64 cap_id, u32 migrate_seq,
1008 u32 issue_seq)
Sage Weil3d7ded42010-06-09 16:47:10 -07001009{
1010 struct ceph_msg *msg;
1011 struct ceph_mds_cap_release *head;
1012 struct ceph_mds_cap_item *item;
1013
1014 spin_lock(&session->s_cap_lock);
1015 BUG_ON(!session->s_num_cap_releases);
1016 msg = list_first_entry(&session->s_cap_releases,
1017 struct ceph_msg, list_head);
1018
1019 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1020 ino, session->s_mds, msg, session->s_num_cap_releases);
1021
1022 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1023 head = msg->front.iov_base;
Wei Yongjunb905a7f2012-09-28 12:59:16 +08001024 le32_add_cpu(&head->num, 1);
Sage Weil3d7ded42010-06-09 16:47:10 -07001025 item = msg->front.iov_base + msg->front.iov_len;
1026 item->ino = cpu_to_le64(ino);
1027 item->cap_id = cpu_to_le64(cap_id);
1028 item->migrate_seq = cpu_to_le32(migrate_seq);
1029 item->seq = cpu_to_le32(issue_seq);
1030
1031 session->s_num_cap_releases--;
1032
1033 msg->front.iov_len += sizeof(*item);
1034 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1035 dout(" release msg %p full\n", msg);
1036 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1037 } else {
1038 dout(" release msg %p at %d/%d (%d)\n", msg,
1039 (int)le32_to_cpu(head->num),
1040 (int)CEPH_CAPS_PER_RELEASE,
1041 (int)msg->front.iov_len);
1042 }
1043 spin_unlock(&session->s_cap_lock);
1044}
1045
Sage Weila8599bd2009-10-06 11:31:12 -07001046/*
Sage Weila6369742010-02-22 13:59:00 -08001047 * Queue cap releases when an inode is dropped from our cache. Since
Sage Weilbe655592011-11-30 09:47:09 -08001048 * inode is about to be destroyed, there is no need for i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001049 */
1050void ceph_queue_caps_release(struct inode *inode)
1051{
1052 struct ceph_inode_info *ci = ceph_inode(inode);
1053 struct rb_node *p;
1054
Sage Weila8599bd2009-10-06 11:31:12 -07001055 p = rb_first(&ci->i_caps);
1056 while (p) {
1057 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1058 struct ceph_mds_session *session = cap->session;
Sage Weila8599bd2009-10-06 11:31:12 -07001059
Sage Weil3d7ded42010-06-09 16:47:10 -07001060 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1061 cap->mseq, cap->issue_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07001062 p = rb_next(p);
Sage Weil7c1332b2010-02-16 11:39:45 -08001063 __ceph_remove_cap(cap);
Sage Weila8599bd2009-10-06 11:31:12 -07001064 }
Sage Weila8599bd2009-10-06 11:31:12 -07001065}
1066
1067/*
1068 * Send a cap msg on the given inode. Update our caps state, then
Sage Weilbe655592011-11-30 09:47:09 -08001069 * drop i_ceph_lock and send the message.
Sage Weila8599bd2009-10-06 11:31:12 -07001070 *
1071 * Make note of max_size reported/requested from mds, revoked caps
1072 * that have now been implemented.
1073 *
1074 * Make half-hearted attempt ot to invalidate page cache if we are
1075 * dropping RDCACHE. Note that this will leave behind locked pages
1076 * that we'll then need to deal with elsewhere.
1077 *
1078 * Return non-zero if delayed release, or we experienced an error
1079 * such that the caller should requeue + retry later.
1080 *
Sage Weilbe655592011-11-30 09:47:09 -08001081 * called with i_ceph_lock, then drops it.
Sage Weila8599bd2009-10-06 11:31:12 -07001082 * caller should hold snap_rwsem (read), s_mutex.
1083 */
1084static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1085 int op, int used, int want, int retain, int flushing,
1086 unsigned *pflush_tid)
Sage Weilbe655592011-11-30 09:47:09 -08001087 __releases(cap->ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001088{
1089 struct ceph_inode_info *ci = cap->ci;
1090 struct inode *inode = &ci->vfs_inode;
1091 u64 cap_id = cap->cap_id;
Sage Weil68c28322010-02-09 13:41:47 -08001092 int held, revoking, dropping, keep;
Sage Weila8599bd2009-10-06 11:31:12 -07001093 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1094 u64 size, max_size;
1095 struct timespec mtime, atime;
1096 int wake = 0;
Al Viro5706b272011-07-26 04:52:22 -04001097 umode_t mode;
Eric W. Biederman05cb11c2013-01-31 02:56:19 -08001098 kuid_t uid;
1099 kgid_t gid;
Sage Weila8599bd2009-10-06 11:31:12 -07001100 struct ceph_mds_session *session;
1101 u64 xattr_version = 0;
Sage Weil082afec2010-08-22 15:16:41 -07001102 struct ceph_buffer *xattr_blob = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07001103 int delayed = 0;
1104 u64 flush_tid = 0;
1105 int i;
1106 int ret;
1107
Sage Weil68c28322010-02-09 13:41:47 -08001108 held = cap->issued | cap->implemented;
1109 revoking = cap->implemented & ~cap->issued;
1110 retain &= ~revoking;
1111 dropping = cap->issued & ~retain;
1112
Sage Weila8599bd2009-10-06 11:31:12 -07001113 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1114 inode, cap, cap->session,
1115 ceph_cap_string(held), ceph_cap_string(held & retain),
1116 ceph_cap_string(revoking));
1117 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1118
1119 session = cap->session;
1120
1121 /* don't release wanted unless we've waited a bit. */
1122 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1123 time_before(jiffies, ci->i_hold_caps_min)) {
1124 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1125 ceph_cap_string(cap->issued),
1126 ceph_cap_string(cap->issued & retain),
1127 ceph_cap_string(cap->mds_wanted),
1128 ceph_cap_string(want));
1129 want |= cap->mds_wanted;
1130 retain |= cap->issued;
1131 delayed = 1;
1132 }
1133 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1134
1135 cap->issued &= retain; /* drop bits we don't want */
1136 if (cap->implemented & ~cap->issued) {
1137 /*
1138 * Wake up any waiters on wanted -> needed transition.
1139 * This is due to the weird transition from buffered
1140 * to sync IO... we need to flush dirty pages _before_
1141 * allowing sync writes to avoid reordering.
1142 */
1143 wake = 1;
1144 }
1145 cap->implemented &= cap->issued | used;
1146 cap->mds_wanted = want;
1147
1148 if (flushing) {
1149 /*
1150 * assign a tid for flush operations so we can avoid
1151 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1152 * clean type races. track latest tid for every bit
1153 * so we can handle flush AxFw, flush Fw, and have the
1154 * first ack clean Ax.
1155 */
1156 flush_tid = ++ci->i_cap_flush_last_tid;
1157 if (pflush_tid)
1158 *pflush_tid = flush_tid;
1159 dout(" cap_flush_tid %d\n", (int)flush_tid);
1160 for (i = 0; i < CEPH_CAP_BITS; i++)
1161 if (flushing & (1 << i))
1162 ci->i_cap_flush_tid[i] = flush_tid;
Sage Weil7d8cb262010-08-24 08:44:16 -07001163
1164 follows = ci->i_head_snapc->seq;
1165 } else {
1166 follows = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001167 }
1168
1169 keep = cap->implemented;
1170 seq = cap->seq;
1171 issue_seq = cap->issue_seq;
1172 mseq = cap->mseq;
1173 size = inode->i_size;
1174 ci->i_reported_size = size;
1175 max_size = ci->i_wanted_max_size;
1176 ci->i_requested_max_size = max_size;
1177 mtime = inode->i_mtime;
1178 atime = inode->i_atime;
1179 time_warp_seq = ci->i_time_warp_seq;
Sage Weila8599bd2009-10-06 11:31:12 -07001180 uid = inode->i_uid;
1181 gid = inode->i_gid;
1182 mode = inode->i_mode;
1183
Sage Weil082afec2010-08-22 15:16:41 -07001184 if (flushing & CEPH_CAP_XATTR_EXCL) {
Sage Weila8599bd2009-10-06 11:31:12 -07001185 __ceph_build_xattrs_blob(ci);
Sage Weil082afec2010-08-22 15:16:41 -07001186 xattr_blob = ci->i_xattrs.blob;
1187 xattr_version = ci->i_xattrs.version;
Sage Weila8599bd2009-10-06 11:31:12 -07001188 }
1189
Sage Weilbe655592011-11-30 09:47:09 -08001190 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001191
Sage Weila8599bd2009-10-06 11:31:12 -07001192 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1193 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1194 size, max_size, &mtime, &atime, time_warp_seq,
Sage Weil082afec2010-08-22 15:16:41 -07001195 uid, gid, mode, xattr_version, xattr_blob,
Sage Weila8599bd2009-10-06 11:31:12 -07001196 follows);
1197 if (ret < 0) {
1198 dout("error sending cap msg, must requeue %p\n", inode);
1199 delayed = 1;
1200 }
1201
1202 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001203 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07001204
1205 return delayed;
1206}
1207
1208/*
1209 * When a snapshot is taken, clients accumulate dirty metadata on
1210 * inodes with capabilities in ceph_cap_snaps to describe the file
1211 * state at the time the snapshot was taken. This must be flushed
1212 * asynchronously back to the MDS once sync writes complete and dirty
1213 * data is written out.
1214 *
Sage Weile8351242010-09-17 08:03:08 -07001215 * Unless @again is true, skip cap_snaps that were already sent to
1216 * the MDS (i.e., during this session).
1217 *
Sage Weilbe655592011-11-30 09:47:09 -08001218 * Called under i_ceph_lock. Takes s_mutex as needed.
Sage Weila8599bd2009-10-06 11:31:12 -07001219 */
1220void __ceph_flush_snaps(struct ceph_inode_info *ci,
Sage Weile8351242010-09-17 08:03:08 -07001221 struct ceph_mds_session **psession,
1222 int again)
Sage Weilbe655592011-11-30 09:47:09 -08001223 __releases(ci->i_ceph_lock)
1224 __acquires(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001225{
1226 struct inode *inode = &ci->vfs_inode;
1227 int mds;
1228 struct ceph_cap_snap *capsnap;
1229 u32 mseq;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001230 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001231 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1232 session->s_mutex */
1233 u64 next_follows = 0; /* keep track of how far we've gotten through the
1234 i_cap_snaps list, and skip these entries next time
1235 around to avoid an infinite loop */
1236
1237 if (psession)
1238 session = *psession;
1239
1240 dout("__flush_snaps %p\n", inode);
1241retry:
1242 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1243 /* avoid an infiniute loop after retry */
1244 if (capsnap->follows < next_follows)
1245 continue;
1246 /*
1247 * we need to wait for sync writes to complete and for dirty
1248 * pages to be written out.
1249 */
1250 if (capsnap->dirty_pages || capsnap->writing)
Sage Weilcfc0bf62010-09-14 15:50:59 -07001251 break;
Sage Weila8599bd2009-10-06 11:31:12 -07001252
Sage Weil819ccbf2010-04-01 09:33:46 -07001253 /*
1254 * if cap writeback already occurred, we should have dropped
1255 * the capsnap in ceph_put_wrbuffer_cap_refs.
1256 */
1257 BUG_ON(capsnap->dirty == 0);
1258
Sage Weila8599bd2009-10-06 11:31:12 -07001259 /* pick mds, take s_mutex */
Sage Weilca81f3f2010-06-10 14:21:36 -07001260 if (ci->i_auth_cap == NULL) {
1261 dout("no auth cap (migrating?), doing nothing\n");
1262 goto out;
1263 }
Sage Weile8351242010-09-17 08:03:08 -07001264
1265 /* only flush each capsnap once */
1266 if (!again && !list_empty(&capsnap->flushing_item)) {
1267 dout("already flushed %p, skipping\n", capsnap);
1268 continue;
1269 }
1270
Sage Weilca81f3f2010-06-10 14:21:36 -07001271 mds = ci->i_auth_cap->session->s_mds;
1272 mseq = ci->i_auth_cap->mseq;
1273
Sage Weila8599bd2009-10-06 11:31:12 -07001274 if (session && session->s_mds != mds) {
1275 dout("oops, wrong session %p mutex\n", session);
1276 mutex_unlock(&session->s_mutex);
1277 ceph_put_mds_session(session);
1278 session = NULL;
1279 }
1280 if (!session) {
Sage Weilbe655592011-11-30 09:47:09 -08001281 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001282 mutex_lock(&mdsc->mutex);
1283 session = __ceph_lookup_mds_session(mdsc, mds);
1284 mutex_unlock(&mdsc->mutex);
1285 if (session) {
1286 dout("inverting session/ino locks on %p\n",
1287 session);
1288 mutex_lock(&session->s_mutex);
1289 }
1290 /*
1291 * if session == NULL, we raced against a cap
Sage Weilca81f3f2010-06-10 14:21:36 -07001292 * deletion or migration. retry, and we'll
1293 * get a better @mds value next time.
Sage Weila8599bd2009-10-06 11:31:12 -07001294 */
Sage Weilbe655592011-11-30 09:47:09 -08001295 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001296 goto retry;
1297 }
1298
1299 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1300 atomic_inc(&capsnap->nref);
1301 if (!list_empty(&capsnap->flushing_item))
1302 list_del_init(&capsnap->flushing_item);
1303 list_add_tail(&capsnap->flushing_item,
1304 &session->s_cap_snaps_flushing);
Sage Weilbe655592011-11-30 09:47:09 -08001305 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001306
Sage Weilcfc0bf62010-09-14 15:50:59 -07001307 dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
1308 inode, capsnap, capsnap->follows, capsnap->flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07001309 send_cap_msg(session, ceph_vino(inode).ino, 0,
1310 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1311 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1312 capsnap->size, 0,
1313 &capsnap->mtime, &capsnap->atime,
1314 capsnap->time_warp_seq,
1315 capsnap->uid, capsnap->gid, capsnap->mode,
Sage Weil4a625be2010-08-22 15:03:56 -07001316 capsnap->xattr_version, capsnap->xattr_blob,
Sage Weila8599bd2009-10-06 11:31:12 -07001317 capsnap->follows);
1318
1319 next_follows = capsnap->follows + 1;
1320 ceph_put_cap_snap(capsnap);
1321
Sage Weilbe655592011-11-30 09:47:09 -08001322 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001323 goto retry;
1324 }
1325
1326 /* we flushed them all; remove this inode from the queue */
1327 spin_lock(&mdsc->snap_flush_lock);
1328 list_del_init(&ci->i_snap_flush_item);
1329 spin_unlock(&mdsc->snap_flush_lock);
1330
Sage Weilca81f3f2010-06-10 14:21:36 -07001331out:
Sage Weila8599bd2009-10-06 11:31:12 -07001332 if (psession)
1333 *psession = session;
1334 else if (session) {
1335 mutex_unlock(&session->s_mutex);
1336 ceph_put_mds_session(session);
1337 }
1338}
1339
1340static void ceph_flush_snaps(struct ceph_inode_info *ci)
1341{
Sage Weilbe655592011-11-30 09:47:09 -08001342 spin_lock(&ci->i_ceph_lock);
Sage Weile8351242010-09-17 08:03:08 -07001343 __ceph_flush_snaps(ci, NULL, 0);
Sage Weilbe655592011-11-30 09:47:09 -08001344 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001345}
1346
1347/*
Sage Weilfca65b42011-05-04 11:33:47 -07001348 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1349 * Caller is then responsible for calling __mark_inode_dirty with the
1350 * returned flags value.
Sage Weil76e3b392009-10-15 18:13:53 -07001351 */
Sage Weilfca65b42011-05-04 11:33:47 -07001352int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
Sage Weil76e3b392009-10-15 18:13:53 -07001353{
Cheng Renquan640ef792010-03-26 17:40:33 +08001354 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001355 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil76e3b392009-10-15 18:13:53 -07001356 struct inode *inode = &ci->vfs_inode;
1357 int was = ci->i_dirty_caps;
1358 int dirty = 0;
1359
1360 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1361 ceph_cap_string(mask), ceph_cap_string(was),
1362 ceph_cap_string(was | mask));
1363 ci->i_dirty_caps |= mask;
1364 if (was == 0) {
Sage Weil7d8cb262010-08-24 08:44:16 -07001365 if (!ci->i_head_snapc)
1366 ci->i_head_snapc = ceph_get_snap_context(
1367 ci->i_snap_realm->cached_context);
Yan, Zheng06852352012-11-19 10:49:07 +08001368 dout(" inode %p now dirty snapc %p auth cap %p\n",
1369 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
Sage Weil76e3b392009-10-15 18:13:53 -07001370 BUG_ON(!list_empty(&ci->i_dirty_item));
1371 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng06852352012-11-19 10:49:07 +08001372 if (ci->i_auth_cap)
1373 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1374 else
1375 list_add(&ci->i_dirty_item,
1376 &mdsc->cap_dirty_migrating);
Sage Weil76e3b392009-10-15 18:13:53 -07001377 spin_unlock(&mdsc->cap_dirty_lock);
1378 if (ci->i_flushing_caps == 0) {
Sage Weil3772d262011-05-03 09:28:08 -07001379 ihold(inode);
Sage Weil76e3b392009-10-15 18:13:53 -07001380 dirty |= I_DIRTY_SYNC;
1381 }
1382 }
1383 BUG_ON(list_empty(&ci->i_dirty_item));
1384 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1385 (mask & CEPH_CAP_FILE_BUFFER))
1386 dirty |= I_DIRTY_DATASYNC;
Sage Weil76e3b392009-10-15 18:13:53 -07001387 __cap_delay_requeue(mdsc, ci);
Sage Weilfca65b42011-05-04 11:33:47 -07001388 return dirty;
Sage Weil76e3b392009-10-15 18:13:53 -07001389}
1390
1391/*
Sage Weila8599bd2009-10-06 11:31:12 -07001392 * Add dirty inode to the flushing list. Assigned a seq number so we
1393 * can wait for caps to flush without starving.
Sage Weilcdc35f92009-10-14 14:24:19 -07001394 *
Sage Weilbe655592011-11-30 09:47:09 -08001395 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001396 */
Sage Weilcdc35f92009-10-14 14:24:19 -07001397static int __mark_caps_flushing(struct inode *inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001398 struct ceph_mds_session *session)
1399{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001400 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001401 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001402 int flushing;
Sage Weil50b885b2009-12-01 14:12:07 -08001403
Sage Weilcdc35f92009-10-14 14:24:19 -07001404 BUG_ON(ci->i_dirty_caps == 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001405 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilcdc35f92009-10-14 14:24:19 -07001406
1407 flushing = ci->i_dirty_caps;
1408 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1409 ceph_cap_string(flushing),
1410 ceph_cap_string(ci->i_flushing_caps),
1411 ceph_cap_string(ci->i_flushing_caps | flushing));
1412 ci->i_flushing_caps |= flushing;
1413 ci->i_dirty_caps = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07001414 dout(" inode %p now !dirty\n", inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001415
Sage Weila8599bd2009-10-06 11:31:12 -07001416 spin_lock(&mdsc->cap_dirty_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07001417 list_del_init(&ci->i_dirty_item);
1418
1419 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
Sage Weila8599bd2009-10-06 11:31:12 -07001420 if (list_empty(&ci->i_flushing_item)) {
1421 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1422 mdsc->num_cap_flushing++;
Sage Weilafcdaea2009-10-14 14:27:38 -07001423 dout(" inode %p now flushing seq %lld\n", inode,
1424 ci->i_cap_flush_seq);
1425 } else {
1426 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1427 dout(" inode %p now flushing (more) seq %lld\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001428 ci->i_cap_flush_seq);
1429 }
1430 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weilcdc35f92009-10-14 14:24:19 -07001431
1432 return flushing;
Sage Weila8599bd2009-10-06 11:31:12 -07001433}
1434
1435/*
Sage Weil5ecad6f2010-02-17 10:43:37 -08001436 * try to invalidate mapping pages without blocking.
1437 */
Sage Weil5ecad6f2010-02-17 10:43:37 -08001438static int try_nonblocking_invalidate(struct inode *inode)
1439{
1440 struct ceph_inode_info *ci = ceph_inode(inode);
1441 u32 invalidating_gen = ci->i_rdcache_gen;
1442
Sage Weilbe655592011-11-30 09:47:09 -08001443 spin_unlock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001444 invalidate_mapping_pages(&inode->i_data, 0, -1);
Sage Weilbe655592011-11-30 09:47:09 -08001445 spin_lock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001446
Sage Weil18a38192010-09-17 10:46:44 -07001447 if (inode->i_data.nrpages == 0 &&
Sage Weil5ecad6f2010-02-17 10:43:37 -08001448 invalidating_gen == ci->i_rdcache_gen) {
1449 /* success. */
1450 dout("try_nonblocking_invalidate %p success\n", inode);
Sage Weilcd045cb2010-11-04 11:05:05 -07001451 /* save any racing async invalidate some trouble */
1452 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
Sage Weil5ecad6f2010-02-17 10:43:37 -08001453 return 0;
1454 }
1455 dout("try_nonblocking_invalidate %p failed\n", inode);
1456 return -1;
1457}
1458
1459/*
Sage Weila8599bd2009-10-06 11:31:12 -07001460 * Swiss army knife function to examine currently used and wanted
1461 * versus held caps. Release, flush, ack revoked caps to mds as
1462 * appropriate.
1463 *
1464 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1465 * cap release further.
1466 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1467 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1468 * further delay.
1469 */
1470void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1471 struct ceph_mds_session *session)
1472{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001473 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1474 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001475 struct inode *inode = &ci->vfs_inode;
1476 struct ceph_cap *cap;
Yan, Zheng395c3122013-01-04 14:37:57 +08001477 int file_wanted, used, cap_used;
Sage Weila8599bd2009-10-06 11:31:12 -07001478 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
Sage Weilcbd03632010-02-09 13:41:18 -08001479 int issued, implemented, want, retain, revoking, flushing = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001480 int mds = -1; /* keep track of how far we've gone through i_caps list
1481 to avoid an infinite loop on retry */
1482 struct rb_node *p;
1483 int tried_invalidate = 0;
1484 int delayed = 0, sent = 0, force_requeue = 0, num;
Sage Weilcbd03632010-02-09 13:41:18 -08001485 int queue_invalidate = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001486 int is_delayed = flags & CHECK_CAPS_NODELAY;
1487
1488 /* if we are unmounting, flush any unused caps immediately. */
1489 if (mdsc->stopping)
1490 is_delayed = 1;
1491
Sage Weilbe655592011-11-30 09:47:09 -08001492 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001493
1494 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1495 flags |= CHECK_CAPS_FLUSH;
1496
1497 /* flush snaps first time around only */
1498 if (!list_empty(&ci->i_cap_snaps))
Sage Weile8351242010-09-17 08:03:08 -07001499 __ceph_flush_snaps(ci, &session, 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001500 goto retry_locked;
1501retry:
Sage Weilbe655592011-11-30 09:47:09 -08001502 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001503retry_locked:
1504 file_wanted = __ceph_caps_file_wanted(ci);
1505 used = __ceph_caps_used(ci);
1506 want = file_wanted | used;
Sage Weilcbd03632010-02-09 13:41:18 -08001507 issued = __ceph_caps_issued(ci, &implemented);
1508 revoking = implemented & ~issued;
Sage Weila8599bd2009-10-06 11:31:12 -07001509
1510 retain = want | CEPH_CAP_PIN;
1511 if (!mdsc->stopping && inode->i_nlink > 0) {
1512 if (want) {
1513 retain |= CEPH_CAP_ANY; /* be greedy */
1514 } else {
1515 retain |= CEPH_CAP_ANY_SHARED;
1516 /*
1517 * keep RD only if we didn't have the file open RW,
1518 * because then the mds would revoke it anyway to
1519 * journal max_size=0.
1520 */
1521 if (ci->i_max_size == 0)
1522 retain |= CEPH_CAP_ANY_RD;
1523 }
1524 }
1525
1526 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
Sage Weilcbd03632010-02-09 13:41:18 -08001527 " issued %s revoking %s retain %s %s%s%s\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001528 ceph_cap_string(file_wanted),
1529 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1530 ceph_cap_string(ci->i_flushing_caps),
Sage Weilcbd03632010-02-09 13:41:18 -08001531 ceph_cap_string(issued), ceph_cap_string(revoking),
Sage Weila8599bd2009-10-06 11:31:12 -07001532 ceph_cap_string(retain),
1533 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1534 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1535 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1536
1537 /*
1538 * If we no longer need to hold onto old our caps, and we may
1539 * have cached pages, but don't want them, then try to invalidate.
1540 * If we fail, it's because pages are locked.... try again later.
1541 */
1542 if ((!is_delayed || mdsc->stopping) &&
1543 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
Sage Weil93afd442010-09-17 08:38:25 -07001544 inode->i_data.nrpages && /* have cached pages */
Sage Weilcbd03632010-02-09 13:41:18 -08001545 (file_wanted == 0 || /* no open files */
Sage Weil29625072010-05-27 10:40:43 -07001546 (revoking & (CEPH_CAP_FILE_CACHE|
1547 CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
Sage Weila8599bd2009-10-06 11:31:12 -07001548 !tried_invalidate) {
Sage Weila8599bd2009-10-06 11:31:12 -07001549 dout("check_caps trying to invalidate on %p\n", inode);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001550 if (try_nonblocking_invalidate(inode) < 0) {
Sage Weil29625072010-05-27 10:40:43 -07001551 if (revoking & (CEPH_CAP_FILE_CACHE|
1552 CEPH_CAP_FILE_LAZYIO)) {
Sage Weil5ecad6f2010-02-17 10:43:37 -08001553 dout("check_caps queuing invalidate\n");
1554 queue_invalidate = 1;
1555 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1556 } else {
1557 dout("check_caps failed to invalidate pages\n");
1558 /* we failed to invalidate pages. check these
1559 caps again later. */
1560 force_requeue = 1;
1561 __cap_set_timeouts(mdsc, ci);
1562 }
Sage Weila8599bd2009-10-06 11:31:12 -07001563 }
1564 tried_invalidate = 1;
1565 goto retry_locked;
1566 }
1567
1568 num = 0;
1569 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1570 cap = rb_entry(p, struct ceph_cap, ci_node);
1571 num++;
1572
1573 /* avoid looping forever */
1574 if (mds >= cap->mds ||
1575 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1576 continue;
1577
1578 /* NOTE: no side-effects allowed, until we take s_mutex */
1579
Yan, Zheng395c3122013-01-04 14:37:57 +08001580 cap_used = used;
1581 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1582 cap_used &= ~ci->i_auth_cap->issued;
1583
Sage Weila8599bd2009-10-06 11:31:12 -07001584 revoking = cap->implemented & ~cap->issued;
Yan, Zheng395c3122013-01-04 14:37:57 +08001585 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
Sage Weil088b3f52011-01-18 08:56:01 -08001586 cap->mds, cap, ceph_cap_string(cap->issued),
Yan, Zheng395c3122013-01-04 14:37:57 +08001587 ceph_cap_string(cap_used),
Sage Weil088b3f52011-01-18 08:56:01 -08001588 ceph_cap_string(cap->implemented),
1589 ceph_cap_string(revoking));
Sage Weila8599bd2009-10-06 11:31:12 -07001590
1591 if (cap == ci->i_auth_cap &&
1592 (cap->issued & CEPH_CAP_FILE_WR)) {
1593 /* request larger max_size from MDS? */
1594 if (ci->i_wanted_max_size > ci->i_max_size &&
1595 ci->i_wanted_max_size > ci->i_requested_max_size) {
1596 dout("requesting new max_size\n");
1597 goto ack;
1598 }
1599
1600 /* approaching file_max? */
1601 if ((inode->i_size << 1) >= ci->i_max_size &&
1602 (ci->i_reported_size << 1) < ci->i_max_size) {
1603 dout("i_size approaching max_size\n");
1604 goto ack;
1605 }
1606 }
1607 /* flush anything dirty? */
1608 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1609 ci->i_dirty_caps) {
1610 dout("flushing dirty caps\n");
1611 goto ack;
1612 }
1613
1614 /* completed revocation? going down and there are no caps? */
Yan, Zheng395c3122013-01-04 14:37:57 +08001615 if (revoking && (revoking & cap_used) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07001616 dout("completed revocation of %s\n",
1617 ceph_cap_string(cap->implemented & ~cap->issued));
1618 goto ack;
1619 }
1620
1621 /* want more caps from mds? */
1622 if (want & ~(cap->mds_wanted | cap->issued))
1623 goto ack;
1624
1625 /* things we might delay */
1626 if ((cap->issued & ~retain) == 0 &&
1627 cap->mds_wanted == want)
1628 continue; /* nope, all good */
1629
1630 if (is_delayed)
1631 goto ack;
1632
1633 /* delay? */
1634 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1635 time_before(jiffies, ci->i_hold_caps_max)) {
1636 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1637 ceph_cap_string(cap->issued),
1638 ceph_cap_string(cap->issued & retain),
1639 ceph_cap_string(cap->mds_wanted),
1640 ceph_cap_string(want));
1641 delayed++;
1642 continue;
1643 }
1644
1645ack:
Sage Weile9964c12010-03-01 15:16:56 -08001646 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1647 dout(" skipping %p I_NOFLUSH set\n", inode);
1648 continue;
1649 }
1650
Sage Weila8599bd2009-10-06 11:31:12 -07001651 if (session && session != cap->session) {
1652 dout("oops, wrong session %p mutex\n", session);
1653 mutex_unlock(&session->s_mutex);
1654 session = NULL;
1655 }
1656 if (!session) {
1657 session = cap->session;
1658 if (mutex_trylock(&session->s_mutex) == 0) {
1659 dout("inverting session/ino locks on %p\n",
1660 session);
Sage Weilbe655592011-11-30 09:47:09 -08001661 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001662 if (took_snap_rwsem) {
1663 up_read(&mdsc->snap_rwsem);
1664 took_snap_rwsem = 0;
1665 }
1666 mutex_lock(&session->s_mutex);
1667 goto retry;
1668 }
1669 }
1670 /* take snap_rwsem after session mutex */
1671 if (!took_snap_rwsem) {
1672 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1673 dout("inverting snap/in locks on %p\n",
1674 inode);
Sage Weilbe655592011-11-30 09:47:09 -08001675 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001676 down_read(&mdsc->snap_rwsem);
1677 took_snap_rwsem = 1;
1678 goto retry;
1679 }
1680 took_snap_rwsem = 1;
1681 }
1682
Sage Weilcdc35f92009-10-14 14:24:19 -07001683 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1684 flushing = __mark_caps_flushing(inode, session);
Sage Weil24be0c42011-01-18 08:48:06 -08001685 else
1686 flushing = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001687
1688 mds = cap->mds; /* remember mds, so we don't repeat */
1689 sent++;
1690
Sage Weilbe655592011-11-30 09:47:09 -08001691 /* __send_cap drops i_ceph_lock */
Yan, Zheng395c3122013-01-04 14:37:57 +08001692 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used,
1693 want, retain, flushing, NULL);
Sage Weilbe655592011-11-30 09:47:09 -08001694 goto retry; /* retake i_ceph_lock and restart our cap scan. */
Sage Weila8599bd2009-10-06 11:31:12 -07001695 }
1696
1697 /*
1698 * Reschedule delayed caps release if we delayed anything,
1699 * otherwise cancel.
1700 */
1701 if (delayed && is_delayed)
1702 force_requeue = 1; /* __send_cap delayed release; requeue */
1703 if (!delayed && !is_delayed)
1704 __cap_delay_cancel(mdsc, ci);
1705 else if (!is_delayed || force_requeue)
1706 __cap_delay_requeue(mdsc, ci);
1707
Sage Weilbe655592011-11-30 09:47:09 -08001708 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001709
Sage Weilcbd03632010-02-09 13:41:18 -08001710 if (queue_invalidate)
Sage Weil3c6f6b72010-02-09 15:24:44 -08001711 ceph_queue_invalidate(inode);
Sage Weilcbd03632010-02-09 13:41:18 -08001712
Sage Weilcdc2ce02010-03-16 13:39:28 -07001713 if (session)
Sage Weila8599bd2009-10-06 11:31:12 -07001714 mutex_unlock(&session->s_mutex);
1715 if (took_snap_rwsem)
1716 up_read(&mdsc->snap_rwsem);
1717}
1718
1719/*
Sage Weila8599bd2009-10-06 11:31:12 -07001720 * Try to flush dirty caps back to the auth mds.
1721 */
1722static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1723 unsigned *flush_tid)
1724{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001725 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001726 struct ceph_inode_info *ci = ceph_inode(inode);
1727 int unlock_session = session ? 0 : 1;
1728 int flushing = 0;
1729
1730retry:
Sage Weilbe655592011-11-30 09:47:09 -08001731 spin_lock(&ci->i_ceph_lock);
Sage Weile9964c12010-03-01 15:16:56 -08001732 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1733 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1734 goto out;
1735 }
Sage Weila8599bd2009-10-06 11:31:12 -07001736 if (ci->i_dirty_caps && ci->i_auth_cap) {
1737 struct ceph_cap *cap = ci->i_auth_cap;
1738 int used = __ceph_caps_used(ci);
1739 int want = __ceph_caps_wanted(ci);
1740 int delayed;
1741
1742 if (!session) {
Sage Weilbe655592011-11-30 09:47:09 -08001743 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001744 session = cap->session;
1745 mutex_lock(&session->s_mutex);
1746 goto retry;
1747 }
1748 BUG_ON(session != cap->session);
1749 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1750 goto out;
1751
Sage Weilcdc35f92009-10-14 14:24:19 -07001752 flushing = __mark_caps_flushing(inode, session);
Sage Weila8599bd2009-10-06 11:31:12 -07001753
Sage Weilbe655592011-11-30 09:47:09 -08001754 /* __send_cap drops i_ceph_lock */
Sage Weila8599bd2009-10-06 11:31:12 -07001755 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1756 cap->issued | cap->implemented, flushing,
1757 flush_tid);
1758 if (!delayed)
1759 goto out_unlocked;
1760
Sage Weilbe655592011-11-30 09:47:09 -08001761 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001762 __cap_delay_requeue(mdsc, ci);
1763 }
1764out:
Sage Weilbe655592011-11-30 09:47:09 -08001765 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001766out_unlocked:
1767 if (session && unlock_session)
1768 mutex_unlock(&session->s_mutex);
1769 return flushing;
1770}
1771
1772/*
1773 * Return true if we've flushed caps through the given flush_tid.
1774 */
1775static int caps_are_flushed(struct inode *inode, unsigned tid)
1776{
1777 struct ceph_inode_info *ci = ceph_inode(inode);
Dan Carpentera5ee7512010-05-07 10:27:14 +02001778 int i, ret = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07001779
Sage Weilbe655592011-11-30 09:47:09 -08001780 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001781 for (i = 0; i < CEPH_CAP_BITS; i++)
1782 if ((ci->i_flushing_caps & (1 << i)) &&
1783 ci->i_cap_flush_tid[i] <= tid) {
1784 /* still flushing this bit */
1785 ret = 0;
1786 break;
1787 }
Sage Weilbe655592011-11-30 09:47:09 -08001788 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001789 return ret;
1790}
1791
1792/*
1793 * Wait on any unsafe replies for the given inode. First wait on the
1794 * newest request, and make that the upper bound. Then, if there are
1795 * more requests, keep waiting on the oldest as long as it is still older
1796 * than the original request.
1797 */
1798static void sync_write_wait(struct inode *inode)
1799{
1800 struct ceph_inode_info *ci = ceph_inode(inode);
1801 struct list_head *head = &ci->i_unsafe_writes;
1802 struct ceph_osd_request *req;
1803 u64 last_tid;
1804
1805 spin_lock(&ci->i_unsafe_lock);
1806 if (list_empty(head))
1807 goto out;
1808
1809 /* set upper bound as _last_ entry in chain */
1810 req = list_entry(head->prev, struct ceph_osd_request,
1811 r_unsafe_item);
1812 last_tid = req->r_tid;
1813
1814 do {
1815 ceph_osdc_get_request(req);
1816 spin_unlock(&ci->i_unsafe_lock);
1817 dout("sync_write_wait on tid %llu (until %llu)\n",
1818 req->r_tid, last_tid);
1819 wait_for_completion(&req->r_safe_completion);
1820 spin_lock(&ci->i_unsafe_lock);
1821 ceph_osdc_put_request(req);
1822
1823 /*
1824 * from here on look at first entry in chain, since we
1825 * only want to wait for anything older than last_tid
1826 */
1827 if (list_empty(head))
1828 break;
1829 req = list_entry(head->next, struct ceph_osd_request,
1830 r_unsafe_item);
1831 } while (req->r_tid < last_tid);
1832out:
1833 spin_unlock(&ci->i_unsafe_lock);
1834}
1835
Josef Bacik02c24a82011-07-16 20:44:56 -04001836int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Sage Weila8599bd2009-10-06 11:31:12 -07001837{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001838 struct inode *inode = file->f_mapping->host;
Sage Weila8599bd2009-10-06 11:31:12 -07001839 struct ceph_inode_info *ci = ceph_inode(inode);
1840 unsigned flush_tid;
1841 int ret;
1842 int dirty;
1843
1844 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1845 sync_write_wait(inode);
1846
Josef Bacik02c24a82011-07-16 20:44:56 -04001847 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Sage Weila8599bd2009-10-06 11:31:12 -07001848 if (ret < 0)
1849 return ret;
Josef Bacik02c24a82011-07-16 20:44:56 -04001850 mutex_lock(&inode->i_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07001851
1852 dirty = try_flush_caps(inode, NULL, &flush_tid);
1853 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1854
1855 /*
1856 * only wait on non-file metadata writeback (the mds
1857 * can recover size and mtime, so we don't need to
1858 * wait for that)
1859 */
1860 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1861 dout("fsync waiting for flush_tid %u\n", flush_tid);
1862 ret = wait_event_interruptible(ci->i_cap_wq,
1863 caps_are_flushed(inode, flush_tid));
1864 }
1865
1866 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
Josef Bacik02c24a82011-07-16 20:44:56 -04001867 mutex_unlock(&inode->i_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07001868 return ret;
1869}
1870
1871/*
1872 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1873 * queue inode for flush but don't do so immediately, because we can
1874 * get by with fewer MDS messages if we wait for data writeback to
1875 * complete first.
1876 */
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11001877int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
Sage Weila8599bd2009-10-06 11:31:12 -07001878{
1879 struct ceph_inode_info *ci = ceph_inode(inode);
1880 unsigned flush_tid;
1881 int err = 0;
1882 int dirty;
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11001883 int wait = wbc->sync_mode == WB_SYNC_ALL;
Sage Weila8599bd2009-10-06 11:31:12 -07001884
1885 dout("write_inode %p wait=%d\n", inode, wait);
1886 if (wait) {
1887 dirty = try_flush_caps(inode, NULL, &flush_tid);
1888 if (dirty)
1889 err = wait_event_interruptible(ci->i_cap_wq,
1890 caps_are_flushed(inode, flush_tid));
1891 } else {
Cheng Renquan640ef792010-03-26 17:40:33 +08001892 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001893 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001894
Sage Weilbe655592011-11-30 09:47:09 -08001895 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001896 if (__ceph_caps_dirty(ci))
1897 __cap_delay_requeue_front(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08001898 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001899 }
1900 return err;
1901}
1902
1903/*
1904 * After a recovering MDS goes active, we need to resend any caps
1905 * we were flushing.
1906 *
1907 * Caller holds session->s_mutex.
1908 */
1909static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1910 struct ceph_mds_session *session)
1911{
1912 struct ceph_cap_snap *capsnap;
1913
1914 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1915 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1916 flushing_item) {
1917 struct ceph_inode_info *ci = capsnap->ci;
1918 struct inode *inode = &ci->vfs_inode;
1919 struct ceph_cap *cap;
1920
Sage Weilbe655592011-11-30 09:47:09 -08001921 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001922 cap = ci->i_auth_cap;
1923 if (cap && cap->session == session) {
1924 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1925 cap, capsnap);
Sage Weile8351242010-09-17 08:03:08 -07001926 __ceph_flush_snaps(ci, &session, 1);
Sage Weila8599bd2009-10-06 11:31:12 -07001927 } else {
1928 pr_err("%p auth cap %p not mds%d ???\n", inode,
1929 cap, session->s_mds);
Sage Weila8599bd2009-10-06 11:31:12 -07001930 }
Sage Weilbe655592011-11-30 09:47:09 -08001931 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001932 }
1933}
1934
1935void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1936 struct ceph_mds_session *session)
1937{
1938 struct ceph_inode_info *ci;
1939
1940 kick_flushing_capsnaps(mdsc, session);
1941
1942 dout("kick_flushing_caps mds%d\n", session->s_mds);
1943 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1944 struct inode *inode = &ci->vfs_inode;
1945 struct ceph_cap *cap;
1946 int delayed = 0;
1947
Sage Weilbe655592011-11-30 09:47:09 -08001948 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001949 cap = ci->i_auth_cap;
1950 if (cap && cap->session == session) {
1951 dout("kick_flushing_caps %p cap %p %s\n", inode,
1952 cap, ceph_cap_string(ci->i_flushing_caps));
1953 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1954 __ceph_caps_used(ci),
1955 __ceph_caps_wanted(ci),
1956 cap->issued | cap->implemented,
1957 ci->i_flushing_caps, NULL);
1958 if (delayed) {
Sage Weilbe655592011-11-30 09:47:09 -08001959 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001960 __cap_delay_requeue(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08001961 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001962 }
1963 } else {
1964 pr_err("%p auth cap %p not mds%d ???\n", inode,
1965 cap, session->s_mds);
Sage Weilbe655592011-11-30 09:47:09 -08001966 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001967 }
1968 }
1969}
1970
Sage Weil088b3f52011-01-18 08:56:01 -08001971static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
1972 struct ceph_mds_session *session,
1973 struct inode *inode)
1974{
1975 struct ceph_inode_info *ci = ceph_inode(inode);
1976 struct ceph_cap *cap;
1977 int delayed = 0;
1978
Sage Weilbe655592011-11-30 09:47:09 -08001979 spin_lock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08001980 cap = ci->i_auth_cap;
1981 dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode,
1982 ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq);
1983 __ceph_flush_snaps(ci, &session, 1);
1984 if (ci->i_flushing_caps) {
1985 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1986 __ceph_caps_used(ci),
1987 __ceph_caps_wanted(ci),
1988 cap->issued | cap->implemented,
1989 ci->i_flushing_caps, NULL);
1990 if (delayed) {
Sage Weilbe655592011-11-30 09:47:09 -08001991 spin_lock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08001992 __cap_delay_requeue(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08001993 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08001994 }
1995 } else {
Sage Weilbe655592011-11-30 09:47:09 -08001996 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08001997 }
1998}
1999
Sage Weila8599bd2009-10-06 11:31:12 -07002000
2001/*
2002 * Take references to capabilities we hold, so that we don't release
2003 * them to the MDS prematurely.
2004 *
Sage Weilbe655592011-11-30 09:47:09 -08002005 * Protected by i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07002006 */
2007static void __take_cap_refs(struct ceph_inode_info *ci, int got)
2008{
2009 if (got & CEPH_CAP_PIN)
2010 ci->i_pin_ref++;
2011 if (got & CEPH_CAP_FILE_RD)
2012 ci->i_rd_ref++;
2013 if (got & CEPH_CAP_FILE_CACHE)
2014 ci->i_rdcache_ref++;
2015 if (got & CEPH_CAP_FILE_WR)
2016 ci->i_wr_ref++;
2017 if (got & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002018 if (ci->i_wb_ref == 0)
Sage Weil3772d262011-05-03 09:28:08 -07002019 ihold(&ci->vfs_inode);
Henry C Changd3d07202011-05-11 10:29:54 +00002020 ci->i_wb_ref++;
2021 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2022 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002023 }
2024}
2025
2026/*
2027 * Try to grab cap references. Specify those refs we @want, and the
2028 * minimal set we @need. Also include the larger offset we are writing
2029 * to (when applicable), and check against max_size here as well.
2030 * Note that caller is responsible for ensuring max_size increases are
2031 * requested from the MDS.
2032 */
2033static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2034 int *got, loff_t endoff, int *check_max, int *err)
2035{
2036 struct inode *inode = &ci->vfs_inode;
2037 int ret = 0;
2038 int have, implemented;
Sage Weil195d3ce2010-03-01 09:57:54 -08002039 int file_wanted;
Sage Weila8599bd2009-10-06 11:31:12 -07002040
2041 dout("get_cap_refs %p need %s want %s\n", inode,
2042 ceph_cap_string(need), ceph_cap_string(want));
Sage Weilbe655592011-11-30 09:47:09 -08002043 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002044
Sage Weil195d3ce2010-03-01 09:57:54 -08002045 /* make sure file is actually open */
2046 file_wanted = __ceph_caps_file_wanted(ci);
2047 if ((file_wanted & need) == 0) {
2048 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2049 ceph_cap_string(need), ceph_cap_string(file_wanted));
Sage Weila8599bd2009-10-06 11:31:12 -07002050 *err = -EBADF;
2051 ret = 1;
2052 goto out;
2053 }
2054
Yan, Zheng37505d52013-04-12 16:11:10 +08002055 /* finish pending truncate */
2056 while (ci->i_truncate_pending) {
2057 spin_unlock(&ci->i_ceph_lock);
2058 __ceph_do_pending_vmtruncate(inode, !(need & CEPH_CAP_FILE_WR));
2059 spin_lock(&ci->i_ceph_lock);
2060 }
2061
Sage Weila8599bd2009-10-06 11:31:12 -07002062 if (need & CEPH_CAP_FILE_WR) {
2063 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2064 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2065 inode, endoff, ci->i_max_size);
2066 if (endoff > ci->i_wanted_max_size) {
2067 *check_max = 1;
2068 ret = 1;
2069 }
2070 goto out;
2071 }
2072 /*
2073 * If a sync write is in progress, we must wait, so that we
2074 * can get a final snapshot value for size+mtime.
2075 */
2076 if (__ceph_have_pending_cap_snap(ci)) {
2077 dout("get_cap_refs %p cap_snap_pending\n", inode);
2078 goto out;
2079 }
2080 }
2081 have = __ceph_caps_issued(ci, &implemented);
2082
Sage Weila8599bd2009-10-06 11:31:12 -07002083 if ((have & need) == need) {
2084 /*
2085 * Look at (implemented & ~have & not) so that we keep waiting
2086 * on transition from wanted -> needed caps. This is needed
2087 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2088 * going before a prior buffered writeback happens.
2089 */
2090 int not = want & ~(have & need);
2091 int revoking = implemented & ~have;
2092 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2093 inode, ceph_cap_string(have), ceph_cap_string(not),
2094 ceph_cap_string(revoking));
2095 if ((revoking & not) == 0) {
2096 *got = need | (have & want);
2097 __take_cap_refs(ci, *got);
2098 ret = 1;
2099 }
2100 } else {
2101 dout("get_cap_refs %p have %s needed %s\n", inode,
2102 ceph_cap_string(have), ceph_cap_string(need));
2103 }
2104out:
Sage Weilbe655592011-11-30 09:47:09 -08002105 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002106 dout("get_cap_refs %p ret %d got %s\n", inode,
2107 ret, ceph_cap_string(*got));
2108 return ret;
2109}
2110
2111/*
2112 * Check the offset we are writing up to against our current
2113 * max_size. If necessary, tell the MDS we want to write to
2114 * a larger offset.
2115 */
2116static void check_max_size(struct inode *inode, loff_t endoff)
2117{
2118 struct ceph_inode_info *ci = ceph_inode(inode);
2119 int check = 0;
2120
2121 /* do we need to explicitly request a larger max_size? */
Sage Weilbe655592011-11-30 09:47:09 -08002122 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002123 if ((endoff >= ci->i_max_size ||
2124 endoff > (inode->i_size << 1)) &&
2125 endoff > ci->i_wanted_max_size) {
2126 dout("write %p at large endoff %llu, req max_size\n",
2127 inode, endoff);
2128 ci->i_wanted_max_size = endoff;
2129 check = 1;
2130 }
Sage Weilbe655592011-11-30 09:47:09 -08002131 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002132 if (check)
2133 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2134}
2135
2136/*
2137 * Wait for caps, and take cap references. If we can't get a WR cap
2138 * due to a small max_size, make sure we check_max_size (and possibly
2139 * ask the mds) so we don't get hung up indefinitely.
2140 */
2141int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2142 loff_t endoff)
2143{
2144 int check_max, ret, err;
2145
2146retry:
2147 if (endoff > 0)
2148 check_max_size(&ci->vfs_inode, endoff);
2149 check_max = 0;
2150 err = 0;
2151 ret = wait_event_interruptible(ci->i_cap_wq,
2152 try_get_cap_refs(ci, need, want,
2153 got, endoff,
2154 &check_max, &err));
2155 if (err)
2156 ret = err;
2157 if (check_max)
2158 goto retry;
2159 return ret;
2160}
2161
2162/*
2163 * Take cap refs. Caller must already know we hold at least one ref
2164 * on the caps in question or we don't know this is safe.
2165 */
2166void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2167{
Sage Weilbe655592011-11-30 09:47:09 -08002168 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002169 __take_cap_refs(ci, caps);
Sage Weilbe655592011-11-30 09:47:09 -08002170 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002171}
2172
2173/*
2174 * Release cap refs.
2175 *
2176 * If we released the last ref on any given cap, call ceph_check_caps
2177 * to release (or schedule a release).
2178 *
2179 * If we are releasing a WR cap (from a sync write), finalize any affected
2180 * cap_snap, and wake up any waiters.
2181 */
2182void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2183{
2184 struct inode *inode = &ci->vfs_inode;
2185 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2186 struct ceph_cap_snap *capsnap;
2187
Sage Weilbe655592011-11-30 09:47:09 -08002188 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002189 if (had & CEPH_CAP_PIN)
2190 --ci->i_pin_ref;
2191 if (had & CEPH_CAP_FILE_RD)
2192 if (--ci->i_rd_ref == 0)
2193 last++;
2194 if (had & CEPH_CAP_FILE_CACHE)
2195 if (--ci->i_rdcache_ref == 0)
2196 last++;
2197 if (had & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002198 if (--ci->i_wb_ref == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07002199 last++;
2200 put++;
2201 }
Henry C Changd3d07202011-05-11 10:29:54 +00002202 dout("put_cap_refs %p wb %d -> %d (?)\n",
2203 inode, ci->i_wb_ref+1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002204 }
2205 if (had & CEPH_CAP_FILE_WR)
2206 if (--ci->i_wr_ref == 0) {
2207 last++;
2208 if (!list_empty(&ci->i_cap_snaps)) {
2209 capsnap = list_first_entry(&ci->i_cap_snaps,
2210 struct ceph_cap_snap,
2211 ci_item);
2212 if (capsnap->writing) {
2213 capsnap->writing = 0;
2214 flushsnaps =
2215 __ceph_finish_cap_snap(ci,
2216 capsnap);
2217 wake = 1;
2218 }
2219 }
2220 }
Sage Weilbe655592011-11-30 09:47:09 -08002221 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002222
Sage Weil819ccbf2010-04-01 09:33:46 -07002223 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2224 last ? " last" : "", put ? " put" : "");
Sage Weila8599bd2009-10-06 11:31:12 -07002225
2226 if (last && !flushsnaps)
2227 ceph_check_caps(ci, 0, NULL);
2228 else if (flushsnaps)
2229 ceph_flush_snaps(ci);
2230 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002231 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002232 if (put)
2233 iput(inode);
2234}
2235
2236/*
2237 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2238 * context. Adjust per-snap dirty page accounting as appropriate.
2239 * Once all dirty data for a cap_snap is flushed, flush snapped file
2240 * metadata back to the MDS. If we dropped the last ref, call
2241 * ceph_check_caps.
2242 */
2243void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2244 struct ceph_snap_context *snapc)
2245{
2246 struct inode *inode = &ci->vfs_inode;
2247 int last = 0;
Sage Weil819ccbf2010-04-01 09:33:46 -07002248 int complete_capsnap = 0;
2249 int drop_capsnap = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002250 int found = 0;
2251 struct ceph_cap_snap *capsnap = NULL;
2252
Sage Weilbe655592011-11-30 09:47:09 -08002253 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002254 ci->i_wrbuffer_ref -= nr;
2255 last = !ci->i_wrbuffer_ref;
2256
2257 if (ci->i_head_snapc == snapc) {
2258 ci->i_wrbuffer_ref_head -= nr;
Sage Weil7d8cb262010-08-24 08:44:16 -07002259 if (ci->i_wrbuffer_ref_head == 0 &&
2260 ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
2261 BUG_ON(!ci->i_head_snapc);
Sage Weila8599bd2009-10-06 11:31:12 -07002262 ceph_put_snap_context(ci->i_head_snapc);
2263 ci->i_head_snapc = NULL;
2264 }
2265 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2266 inode,
2267 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2268 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2269 last ? " LAST" : "");
2270 } else {
2271 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2272 if (capsnap->context == snapc) {
2273 found = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002274 break;
2275 }
2276 }
2277 BUG_ON(!found);
Sage Weil819ccbf2010-04-01 09:33:46 -07002278 capsnap->dirty_pages -= nr;
2279 if (capsnap->dirty_pages == 0) {
2280 complete_capsnap = 1;
2281 if (capsnap->dirty == 0)
2282 /* cap writeback completed before we created
2283 * the cap_snap; no FLUSHSNAP is needed */
2284 drop_capsnap = 1;
2285 }
Sage Weila8599bd2009-10-06 11:31:12 -07002286 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
Sage Weil819ccbf2010-04-01 09:33:46 -07002287 " snap %lld %d/%d -> %d/%d %s%s%s\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002288 inode, capsnap, capsnap->context->seq,
2289 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2290 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2291 last ? " (wrbuffer last)" : "",
Sage Weil819ccbf2010-04-01 09:33:46 -07002292 complete_capsnap ? " (complete capsnap)" : "",
2293 drop_capsnap ? " (drop capsnap)" : "");
2294 if (drop_capsnap) {
2295 ceph_put_snap_context(capsnap->context);
2296 list_del(&capsnap->ci_item);
2297 list_del(&capsnap->flushing_item);
2298 ceph_put_cap_snap(capsnap);
2299 }
Sage Weila8599bd2009-10-06 11:31:12 -07002300 }
2301
Sage Weilbe655592011-11-30 09:47:09 -08002302 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002303
2304 if (last) {
2305 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2306 iput(inode);
Sage Weil819ccbf2010-04-01 09:33:46 -07002307 } else if (complete_capsnap) {
Sage Weila8599bd2009-10-06 11:31:12 -07002308 ceph_flush_snaps(ci);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002309 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002310 }
Sage Weil819ccbf2010-04-01 09:33:46 -07002311 if (drop_capsnap)
2312 iput(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002313}
2314
2315/*
2316 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2317 * actually be a revocation if it specifies a smaller cap set.)
2318 *
Sage Weilbe655592011-11-30 09:47:09 -08002319 * caller holds s_mutex and i_ceph_lock, we drop both.
Sage Weil15637c82010-03-16 13:42:00 -07002320 *
Sage Weila8599bd2009-10-06 11:31:12 -07002321 * return value:
2322 * 0 - ok
2323 * 1 - check_caps on auth cap only (writeback)
2324 * 2 - check_caps (ack revoke)
2325 */
Sage Weil15637c82010-03-16 13:42:00 -07002326static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2327 struct ceph_mds_session *session,
2328 struct ceph_cap *cap,
2329 struct ceph_buffer *xattr_buf)
Sage Weilbe655592011-11-30 09:47:09 -08002330 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07002331{
2332 struct ceph_inode_info *ci = ceph_inode(inode);
2333 int mds = session->s_mds;
Sage Weil2f56f562010-10-27 20:59:49 -07002334 int seq = le32_to_cpu(grant->seq);
Sage Weila8599bd2009-10-06 11:31:12 -07002335 int newcaps = le32_to_cpu(grant->caps);
2336 int issued, implemented, used, wanted, dirty;
2337 u64 size = le64_to_cpu(grant->size);
2338 u64 max_size = le64_to_cpu(grant->max_size);
2339 struct timespec mtime, atime, ctime;
Sage Weil15637c82010-03-16 13:42:00 -07002340 int check_caps = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002341 int wake = 0;
2342 int writeback = 0;
2343 int revoked_rdcache = 0;
Sage Weil3c6f6b72010-02-09 15:24:44 -08002344 int queue_invalidate = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002345
Sage Weil2f56f562010-10-27 20:59:49 -07002346 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2347 inode, cap, mds, seq, ceph_cap_string(newcaps));
Sage Weila8599bd2009-10-06 11:31:12 -07002348 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2349 inode->i_size);
2350
2351 /*
2352 * If CACHE is being revoked, and we have no dirty buffers,
2353 * try to invalidate (once). (If there are dirty buffers, we
2354 * will invalidate _after_ writeback.)
2355 */
Sage Weil3b454c42010-06-10 13:20:33 -07002356 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2357 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
Yehuda Sadehbcd2cbd2010-02-19 00:12:21 +00002358 !ci->i_wrbuffer_ref) {
Sage Weil5ecad6f2010-02-17 10:43:37 -08002359 if (try_nonblocking_invalidate(inode) == 0) {
2360 revoked_rdcache = 1;
2361 } else {
Sage Weila8599bd2009-10-06 11:31:12 -07002362 /* there were locked pages.. invalidate later
2363 in a separate thread. */
2364 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Sage Weil3c6f6b72010-02-09 15:24:44 -08002365 queue_invalidate = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002366 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2367 }
Sage Weila8599bd2009-10-06 11:31:12 -07002368 }
Sage Weila8599bd2009-10-06 11:31:12 -07002369 }
2370
2371 /* side effects now are allowed */
2372
2373 issued = __ceph_caps_issued(ci, &implemented);
2374 issued |= implemented | __ceph_caps_dirty(ci);
2375
Sage Weil685f9a5d2009-11-09 12:05:48 -08002376 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -07002377
2378 __check_cap_issue(ci, cap, newcaps);
2379
2380 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2381 inode->i_mode = le32_to_cpu(grant->mode);
Eric W. Biederman05cb11c2013-01-31 02:56:19 -08002382 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
2383 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
Sage Weila8599bd2009-10-06 11:31:12 -07002384 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08002385 from_kuid(&init_user_ns, inode->i_uid),
2386 from_kgid(&init_user_ns, inode->i_gid));
Sage Weila8599bd2009-10-06 11:31:12 -07002387 }
2388
2389 if ((issued & CEPH_CAP_LINK_EXCL) == 0)
Miklos Szeredibfe86842011-10-28 14:13:29 +02002390 set_nlink(inode, le32_to_cpu(grant->nlink));
Sage Weila8599bd2009-10-06 11:31:12 -07002391
2392 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2393 int len = le32_to_cpu(grant->xattr_len);
2394 u64 version = le64_to_cpu(grant->xattr_version);
2395
2396 if (version > ci->i_xattrs.version) {
2397 dout(" got new xattrs v%llu on %p len %d\n",
2398 version, inode, len);
2399 if (ci->i_xattrs.blob)
2400 ceph_buffer_put(ci->i_xattrs.blob);
2401 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2402 ci->i_xattrs.version = version;
2403 }
2404 }
2405
2406 /* size/ctime/mtime/atime? */
2407 ceph_fill_file_size(inode, issued,
2408 le32_to_cpu(grant->truncate_seq),
2409 le64_to_cpu(grant->truncate_size), size);
2410 ceph_decode_timespec(&mtime, &grant->mtime);
2411 ceph_decode_timespec(&atime, &grant->atime);
2412 ceph_decode_timespec(&ctime, &grant->ctime);
2413 ceph_fill_file_time(inode, issued,
2414 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2415 &atime);
2416
2417 /* max size increase? */
Yan, Zheng5e62ad32012-11-19 10:49:04 +08002418 if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
Sage Weila8599bd2009-10-06 11:31:12 -07002419 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2420 ci->i_max_size = max_size;
2421 if (max_size >= ci->i_wanted_max_size) {
2422 ci->i_wanted_max_size = 0; /* reset */
2423 ci->i_requested_max_size = 0;
2424 }
2425 wake = 1;
2426 }
2427
2428 /* check cap bits */
2429 wanted = __ceph_caps_wanted(ci);
2430 used = __ceph_caps_used(ci);
2431 dirty = __ceph_caps_dirty(ci);
2432 dout(" my wanted = %s, used = %s, dirty %s\n",
2433 ceph_cap_string(wanted),
2434 ceph_cap_string(used),
2435 ceph_cap_string(dirty));
2436 if (wanted != le32_to_cpu(grant->wanted)) {
2437 dout("mds wanted %s -> %s\n",
2438 ceph_cap_string(le32_to_cpu(grant->wanted)),
2439 ceph_cap_string(wanted));
Yan, Zheng390306c2013-01-04 15:30:10 +08002440 /* imported cap may not have correct mds_wanted */
2441 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
2442 check_caps = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002443 }
2444
2445 cap->seq = seq;
2446
2447 /* file layout may have changed */
2448 ci->i_layout = grant->layout;
2449
2450 /* revocation, grant, or no-op? */
2451 if (cap->issued & ~newcaps) {
Sage Weil3b454c42010-06-10 13:20:33 -07002452 int revoking = cap->issued & ~newcaps;
2453
2454 dout("revocation: %s -> %s (revoking %s)\n",
2455 ceph_cap_string(cap->issued),
2456 ceph_cap_string(newcaps),
2457 ceph_cap_string(revoking));
Sage Weil0eb6cd42010-08-05 13:53:18 -07002458 if (revoking & used & CEPH_CAP_FILE_BUFFER)
Sage Weil3b454c42010-06-10 13:20:33 -07002459 writeback = 1; /* initiate writeback; will delay ack */
2460 else if (revoking == CEPH_CAP_FILE_CACHE &&
2461 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2462 queue_invalidate)
2463 ; /* do nothing yet, invalidation will be queued */
2464 else if (cap == ci->i_auth_cap)
2465 check_caps = 1; /* check auth cap only */
2466 else
2467 check_caps = 2; /* check all caps */
Sage Weila8599bd2009-10-06 11:31:12 -07002468 cap->issued = newcaps;
Sage Weil978097c2010-03-08 15:27:53 -08002469 cap->implemented |= newcaps;
Sage Weila8599bd2009-10-06 11:31:12 -07002470 } else if (cap->issued == newcaps) {
2471 dout("caps unchanged: %s -> %s\n",
2472 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2473 } else {
2474 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2475 ceph_cap_string(newcaps));
2476 cap->issued = newcaps;
2477 cap->implemented |= newcaps; /* add bits only, to
2478 * avoid stepping on a
2479 * pending revocation */
2480 wake = 1;
2481 }
Sage Weil978097c2010-03-08 15:27:53 -08002482 BUG_ON(cap->issued & ~cap->implemented);
Sage Weila8599bd2009-10-06 11:31:12 -07002483
Sage Weilbe655592011-11-30 09:47:09 -08002484 spin_unlock(&ci->i_ceph_lock);
Sage Weil3c6f6b72010-02-09 15:24:44 -08002485 if (writeback)
Sage Weila8599bd2009-10-06 11:31:12 -07002486 /*
2487 * queue inode for writeback: we can't actually call
2488 * filemap_write_and_wait, etc. from message handler
2489 * context.
2490 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08002491 ceph_queue_writeback(inode);
2492 if (queue_invalidate)
2493 ceph_queue_invalidate(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002494 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002495 wake_up_all(&ci->i_cap_wq);
Sage Weil15637c82010-03-16 13:42:00 -07002496
2497 if (check_caps == 1)
2498 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2499 session);
2500 else if (check_caps == 2)
2501 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2502 else
2503 mutex_unlock(&session->s_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07002504}
2505
2506/*
2507 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2508 * MDS has been safely committed.
2509 */
Sage Weil6df058c2009-12-22 11:24:33 -08002510static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07002511 struct ceph_mds_caps *m,
2512 struct ceph_mds_session *session,
2513 struct ceph_cap *cap)
Sage Weilbe655592011-11-30 09:47:09 -08002514 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07002515{
2516 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002517 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002518 unsigned seq = le32_to_cpu(m->seq);
2519 int dirty = le32_to_cpu(m->dirty);
2520 int cleaned = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07002521 int drop = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002522 int i;
2523
2524 for (i = 0; i < CEPH_CAP_BITS; i++)
2525 if ((dirty & (1 << i)) &&
2526 flush_tid == ci->i_cap_flush_tid[i])
2527 cleaned |= 1 << i;
2528
2529 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2530 " flushing %s -> %s\n",
2531 inode, session->s_mds, seq, ceph_cap_string(dirty),
2532 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2533 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2534
2535 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2536 goto out;
2537
Sage Weila8599bd2009-10-06 11:31:12 -07002538 ci->i_flushing_caps &= ~cleaned;
Sage Weila8599bd2009-10-06 11:31:12 -07002539
2540 spin_lock(&mdsc->cap_dirty_lock);
2541 if (ci->i_flushing_caps == 0) {
2542 list_del_init(&ci->i_flushing_item);
2543 if (!list_empty(&session->s_cap_flushing))
2544 dout(" mds%d still flushing cap on %p\n",
2545 session->s_mds,
2546 &list_entry(session->s_cap_flushing.next,
2547 struct ceph_inode_info,
2548 i_flushing_item)->vfs_inode);
2549 mdsc->num_cap_flushing--;
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002550 wake_up_all(&mdsc->cap_flushing_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002551 dout(" inode %p now !flushing\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07002552
2553 if (ci->i_dirty_caps == 0) {
2554 dout(" inode %p now clean\n", inode);
2555 BUG_ON(!list_empty(&ci->i_dirty_item));
2556 drop = 1;
Sage Weil7d8cb262010-08-24 08:44:16 -07002557 if (ci->i_wrbuffer_ref_head == 0) {
2558 BUG_ON(!ci->i_head_snapc);
2559 ceph_put_snap_context(ci->i_head_snapc);
2560 ci->i_head_snapc = NULL;
2561 }
Sage Weil76e3b392009-10-15 18:13:53 -07002562 } else {
2563 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilafcdaea2009-10-14 14:27:38 -07002564 }
Sage Weila8599bd2009-10-06 11:31:12 -07002565 }
2566 spin_unlock(&mdsc->cap_dirty_lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002567 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002568
2569out:
Sage Weilbe655592011-11-30 09:47:09 -08002570 spin_unlock(&ci->i_ceph_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07002571 if (drop)
Sage Weila8599bd2009-10-06 11:31:12 -07002572 iput(inode);
2573}
2574
2575/*
2576 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2577 * throw away our cap_snap.
2578 *
2579 * Caller hold s_mutex.
2580 */
Sage Weil6df058c2009-12-22 11:24:33 -08002581static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07002582 struct ceph_mds_caps *m,
2583 struct ceph_mds_session *session)
2584{
2585 struct ceph_inode_info *ci = ceph_inode(inode);
2586 u64 follows = le64_to_cpu(m->snap_follows);
Sage Weila8599bd2009-10-06 11:31:12 -07002587 struct ceph_cap_snap *capsnap;
2588 int drop = 0;
2589
2590 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2591 inode, ci, session->s_mds, follows);
2592
Sage Weilbe655592011-11-30 09:47:09 -08002593 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002594 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2595 if (capsnap->follows == follows) {
2596 if (capsnap->flush_tid != flush_tid) {
2597 dout(" cap_snap %p follows %lld tid %lld !="
2598 " %lld\n", capsnap, follows,
2599 flush_tid, capsnap->flush_tid);
2600 break;
2601 }
2602 WARN_ON(capsnap->dirty_pages || capsnap->writing);
Sage Weil819ccbf2010-04-01 09:33:46 -07002603 dout(" removing %p cap_snap %p follows %lld\n",
2604 inode, capsnap, follows);
Sage Weila8599bd2009-10-06 11:31:12 -07002605 ceph_put_snap_context(capsnap->context);
2606 list_del(&capsnap->ci_item);
2607 list_del(&capsnap->flushing_item);
2608 ceph_put_cap_snap(capsnap);
2609 drop = 1;
2610 break;
2611 } else {
2612 dout(" skipping cap_snap %p follows %lld\n",
2613 capsnap, capsnap->follows);
2614 }
2615 }
Sage Weilbe655592011-11-30 09:47:09 -08002616 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002617 if (drop)
2618 iput(inode);
2619}
2620
2621/*
2622 * Handle TRUNC from MDS, indicating file truncation.
2623 *
2624 * caller hold s_mutex.
2625 */
2626static void handle_cap_trunc(struct inode *inode,
2627 struct ceph_mds_caps *trunc,
2628 struct ceph_mds_session *session)
Sage Weilbe655592011-11-30 09:47:09 -08002629 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07002630{
2631 struct ceph_inode_info *ci = ceph_inode(inode);
2632 int mds = session->s_mds;
2633 int seq = le32_to_cpu(trunc->seq);
2634 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2635 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2636 u64 size = le64_to_cpu(trunc->size);
2637 int implemented = 0;
2638 int dirty = __ceph_caps_dirty(ci);
2639 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2640 int queue_trunc = 0;
2641
2642 issued |= implemented | dirty;
2643
2644 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2645 inode, mds, seq, truncate_size, truncate_seq);
2646 queue_trunc = ceph_fill_file_size(inode, issued,
2647 truncate_seq, truncate_size, size);
Sage Weilbe655592011-11-30 09:47:09 -08002648 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002649
2650 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -08002651 ceph_queue_vmtruncate(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002652}
2653
2654/*
2655 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2656 * different one. If we are the most recent migration we've seen (as
2657 * indicated by mseq), make note of the migrating cap bits for the
2658 * duration (until we see the corresponding IMPORT).
2659 *
2660 * caller holds s_mutex
2661 */
2662static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
Sage Weil154f42c2010-06-21 13:45:04 -07002663 struct ceph_mds_session *session,
2664 int *open_target_sessions)
Sage Weila8599bd2009-10-06 11:31:12 -07002665{
Sage Weildb354052011-05-24 11:46:31 -07002666 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002667 struct ceph_inode_info *ci = ceph_inode(inode);
2668 int mds = session->s_mds;
2669 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2670 struct ceph_cap *cap = NULL, *t;
2671 struct rb_node *p;
2672 int remember = 1;
2673
2674 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2675 inode, ci, mds, mseq);
2676
Sage Weilbe655592011-11-30 09:47:09 -08002677 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002678
2679 /* make sure we haven't seen a higher mseq */
2680 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2681 t = rb_entry(p, struct ceph_cap, ci_node);
2682 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2683 dout(" higher mseq on cap from mds%d\n",
2684 t->session->s_mds);
2685 remember = 0;
2686 }
2687 if (t->session->s_mds == mds)
2688 cap = t;
2689 }
2690
2691 if (cap) {
2692 if (remember) {
2693 /* make note */
2694 ci->i_cap_exporting_mds = mds;
2695 ci->i_cap_exporting_mseq = mseq;
2696 ci->i_cap_exporting_issued = cap->issued;
Sage Weil154f42c2010-06-21 13:45:04 -07002697
2698 /*
2699 * make sure we have open sessions with all possible
2700 * export targets, so that we get the matching IMPORT
2701 */
2702 *open_target_sessions = 1;
Sage Weildb354052011-05-24 11:46:31 -07002703
2704 /*
2705 * we can't flush dirty caps that we've seen the
2706 * EXPORT but no IMPORT for
2707 */
2708 spin_lock(&mdsc->cap_dirty_lock);
2709 if (!list_empty(&ci->i_dirty_item)) {
2710 dout(" moving %p to cap_dirty_migrating\n",
2711 inode);
2712 list_move(&ci->i_dirty_item,
2713 &mdsc->cap_dirty_migrating);
2714 }
2715 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002716 }
Sage Weil7c1332b2010-02-16 11:39:45 -08002717 __ceph_remove_cap(cap);
Sage Weila8599bd2009-10-06 11:31:12 -07002718 }
Sage Weil4ea00432010-03-16 10:36:40 -07002719 /* else, we already released it */
Sage Weila8599bd2009-10-06 11:31:12 -07002720
Sage Weilbe655592011-11-30 09:47:09 -08002721 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002722}
2723
2724/*
2725 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2726 * clean them up.
2727 *
2728 * caller holds s_mutex.
2729 */
2730static void handle_cap_import(struct ceph_mds_client *mdsc,
2731 struct inode *inode, struct ceph_mds_caps *im,
2732 struct ceph_mds_session *session,
2733 void *snaptrace, int snaptrace_len)
2734{
2735 struct ceph_inode_info *ci = ceph_inode(inode);
2736 int mds = session->s_mds;
2737 unsigned issued = le32_to_cpu(im->caps);
2738 unsigned wanted = le32_to_cpu(im->wanted);
2739 unsigned seq = le32_to_cpu(im->seq);
2740 unsigned mseq = le32_to_cpu(im->migrate_seq);
2741 u64 realmino = le64_to_cpu(im->realm);
2742 u64 cap_id = le64_to_cpu(im->cap_id);
2743
2744 if (ci->i_cap_exporting_mds >= 0 &&
2745 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2746 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2747 " - cleared exporting from mds%d\n",
2748 inode, ci, mds, mseq,
2749 ci->i_cap_exporting_mds);
2750 ci->i_cap_exporting_issued = 0;
2751 ci->i_cap_exporting_mseq = 0;
2752 ci->i_cap_exporting_mds = -1;
Sage Weildb354052011-05-24 11:46:31 -07002753
2754 spin_lock(&mdsc->cap_dirty_lock);
2755 if (!list_empty(&ci->i_dirty_item)) {
2756 dout(" moving %p back to cap_dirty\n", inode);
2757 list_move(&ci->i_dirty_item, &mdsc->cap_dirty);
2758 }
2759 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002760 } else {
2761 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2762 inode, ci, mds, mseq);
2763 }
2764
2765 down_write(&mdsc->snap_rwsem);
2766 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2767 false);
2768 downgrade_write(&mdsc->snap_rwsem);
2769 ceph_add_cap(inode, session, cap_id, -1,
2770 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2771 NULL /* no caps context */);
Sage Weil088b3f52011-01-18 08:56:01 -08002772 kick_flushing_inode_caps(mdsc, session, inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002773 up_read(&mdsc->snap_rwsem);
Sage Weilfeb4cc92010-11-07 09:39:00 -08002774
2775 /* make sure we re-request max_size, if necessary */
Sage Weilbe655592011-11-30 09:47:09 -08002776 spin_lock(&ci->i_ceph_lock);
Yan, Zheng0e5e1772012-11-19 10:49:09 +08002777 ci->i_wanted_max_size = 0; /* reset */
Sage Weilfeb4cc92010-11-07 09:39:00 -08002778 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08002779 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002780}
2781
2782/*
2783 * Handle a caps message from the MDS.
2784 *
2785 * Identify the appropriate session, inode, and call the right handler
2786 * based on the cap op.
2787 */
2788void ceph_handle_caps(struct ceph_mds_session *session,
2789 struct ceph_msg *msg)
2790{
2791 struct ceph_mds_client *mdsc = session->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002792 struct super_block *sb = mdsc->fsc->sb;
Sage Weila8599bd2009-10-06 11:31:12 -07002793 struct inode *inode;
Sage Weilbe655592011-11-30 09:47:09 -08002794 struct ceph_inode_info *ci;
Sage Weila8599bd2009-10-06 11:31:12 -07002795 struct ceph_cap *cap;
2796 struct ceph_mds_caps *h;
Sage Weil2600d2d2010-02-22 15:12:16 -08002797 int mds = session->s_mds;
Sage Weila8599bd2009-10-06 11:31:12 -07002798 int op;
Sage Weil3d7ded42010-06-09 16:47:10 -07002799 u32 seq, mseq;
Sage Weila8599bd2009-10-06 11:31:12 -07002800 struct ceph_vino vino;
2801 u64 cap_id;
2802 u64 size, max_size;
Sage Weil6df058c2009-12-22 11:24:33 -08002803 u64 tid;
Sage Weil70edb552010-03-01 13:20:50 -08002804 void *snaptrace;
Sage Weilce1fbc82010-08-02 15:09:39 -07002805 size_t snaptrace_len;
2806 void *flock;
2807 u32 flock_len;
Sage Weil154f42c2010-06-21 13:45:04 -07002808 int open_target_sessions = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002809
2810 dout("handle_caps from mds%d\n", mds);
2811
2812 /* decode */
Sage Weil6df058c2009-12-22 11:24:33 -08002813 tid = le64_to_cpu(msg->hdr.tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002814 if (msg->front.iov_len < sizeof(*h))
2815 goto bad;
2816 h = msg->front.iov_base;
2817 op = le32_to_cpu(h->op);
2818 vino.ino = le64_to_cpu(h->ino);
2819 vino.snap = CEPH_NOSNAP;
2820 cap_id = le64_to_cpu(h->cap_id);
2821 seq = le32_to_cpu(h->seq);
Sage Weil3d7ded42010-06-09 16:47:10 -07002822 mseq = le32_to_cpu(h->migrate_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07002823 size = le64_to_cpu(h->size);
2824 max_size = le64_to_cpu(h->max_size);
2825
Sage Weilce1fbc82010-08-02 15:09:39 -07002826 snaptrace = h + 1;
2827 snaptrace_len = le32_to_cpu(h->snap_trace_len);
2828
2829 if (le16_to_cpu(msg->hdr.version) >= 2) {
2830 void *p, *end;
2831
2832 p = snaptrace + snaptrace_len;
2833 end = msg->front.iov_base + msg->front.iov_len;
2834 ceph_decode_32_safe(&p, end, flock_len, bad);
2835 flock = p;
2836 } else {
2837 flock = NULL;
2838 flock_len = 0;
2839 }
2840
Sage Weila8599bd2009-10-06 11:31:12 -07002841 mutex_lock(&session->s_mutex);
2842 session->s_seq++;
2843 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2844 (unsigned)seq);
2845
Yan, Zheng66f58692013-01-04 14:45:18 +08002846 if (op == CEPH_CAP_OP_IMPORT)
2847 ceph_add_cap_releases(mdsc, session);
2848
Sage Weila8599bd2009-10-06 11:31:12 -07002849 /* lookup ino */
2850 inode = ceph_find_inode(sb, vino);
Sage Weilbe655592011-11-30 09:47:09 -08002851 ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002852 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2853 vino.snap, inode);
2854 if (!inode) {
2855 dout(" i don't have ino %llx\n", vino.ino);
Sage Weil3d7ded42010-06-09 16:47:10 -07002856
2857 if (op == CEPH_CAP_OP_IMPORT)
2858 __queue_cap_release(session, vino.ino, cap_id,
2859 mseq, seq);
Greg Farnum21b559d2010-10-06 15:46:30 -07002860 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07002861 }
2862
2863 /* these will work even if we don't have a cap yet */
2864 switch (op) {
2865 case CEPH_CAP_OP_FLUSHSNAP_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08002866 handle_cap_flushsnap_ack(inode, tid, h, session);
Sage Weila8599bd2009-10-06 11:31:12 -07002867 goto done;
2868
2869 case CEPH_CAP_OP_EXPORT:
Sage Weil154f42c2010-06-21 13:45:04 -07002870 handle_cap_export(inode, h, session, &open_target_sessions);
Sage Weila8599bd2009-10-06 11:31:12 -07002871 goto done;
2872
2873 case CEPH_CAP_OP_IMPORT:
2874 handle_cap_import(mdsc, inode, h, session,
Sage Weilce1fbc82010-08-02 15:09:39 -07002875 snaptrace, snaptrace_len);
Sage Weila8599bd2009-10-06 11:31:12 -07002876 }
2877
2878 /* the rest require a cap */
Sage Weilbe655592011-11-30 09:47:09 -08002879 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002880 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2881 if (!cap) {
Sage Weil9dbd4122010-06-10 13:21:20 -07002882 dout(" no cap on %p ino %llx.%llx from mds%d\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002883 inode, ceph_ino(inode), ceph_snap(inode), mds);
Sage Weilbe655592011-11-30 09:47:09 -08002884 spin_unlock(&ci->i_ceph_lock);
Greg Farnum21b559d2010-10-06 15:46:30 -07002885 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07002886 }
2887
Sage Weilbe655592011-11-30 09:47:09 -08002888 /* note that each of these drops i_ceph_lock for us */
Sage Weila8599bd2009-10-06 11:31:12 -07002889 switch (op) {
2890 case CEPH_CAP_OP_REVOKE:
2891 case CEPH_CAP_OP_GRANT:
Yan, Zheng0e5e1772012-11-19 10:49:09 +08002892 case CEPH_CAP_OP_IMPORT:
Sage Weil15637c82010-03-16 13:42:00 -07002893 handle_cap_grant(inode, h, session, cap, msg->middle);
2894 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07002895
2896 case CEPH_CAP_OP_FLUSH_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08002897 handle_cap_flush_ack(inode, tid, h, session, cap);
Sage Weila8599bd2009-10-06 11:31:12 -07002898 break;
2899
2900 case CEPH_CAP_OP_TRUNC:
2901 handle_cap_trunc(inode, h, session);
2902 break;
2903
2904 default:
Sage Weilbe655592011-11-30 09:47:09 -08002905 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002906 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2907 ceph_cap_op_name(op));
2908 }
2909
Greg Farnum21b559d2010-10-06 15:46:30 -07002910 goto done;
2911
2912flush_cap_releases:
2913 /*
2914 * send any full release message to try to move things
2915 * along for the mds (who clearly thinks we still have this
2916 * cap).
2917 */
2918 ceph_add_cap_releases(mdsc, session);
2919 ceph_send_cap_releases(mdsc, session);
2920
Sage Weila8599bd2009-10-06 11:31:12 -07002921done:
Sage Weil15637c82010-03-16 13:42:00 -07002922 mutex_unlock(&session->s_mutex);
2923done_unlocked:
Sage Weila8599bd2009-10-06 11:31:12 -07002924 if (inode)
2925 iput(inode);
Sage Weil154f42c2010-06-21 13:45:04 -07002926 if (open_target_sessions)
2927 ceph_mdsc_open_export_target_sessions(mdsc, session);
Sage Weila8599bd2009-10-06 11:31:12 -07002928 return;
2929
2930bad:
2931 pr_err("ceph_handle_caps: corrupt message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002932 ceph_msg_dump(msg);
Sage Weila8599bd2009-10-06 11:31:12 -07002933 return;
2934}
2935
2936/*
2937 * Delayed work handler to process end of delayed cap release LRU list.
2938 */
Sage Weilafcdaea2009-10-14 14:27:38 -07002939void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -07002940{
2941 struct ceph_inode_info *ci;
2942 int flags = CHECK_CAPS_NODELAY;
2943
Sage Weila8599bd2009-10-06 11:31:12 -07002944 dout("check_delayed_caps\n");
2945 while (1) {
2946 spin_lock(&mdsc->cap_delay_lock);
2947 if (list_empty(&mdsc->cap_delay_list))
2948 break;
2949 ci = list_first_entry(&mdsc->cap_delay_list,
2950 struct ceph_inode_info,
2951 i_cap_delay_list);
2952 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2953 time_before(jiffies, ci->i_hold_caps_max))
2954 break;
2955 list_del_init(&ci->i_cap_delay_list);
2956 spin_unlock(&mdsc->cap_delay_lock);
2957 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2958 ceph_check_caps(ci, flags, NULL);
2959 }
2960 spin_unlock(&mdsc->cap_delay_lock);
2961}
2962
2963/*
Sage Weilafcdaea2009-10-14 14:27:38 -07002964 * Flush all dirty caps to the mds
2965 */
2966void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2967{
Sage Weildb354052011-05-24 11:46:31 -07002968 struct ceph_inode_info *ci;
2969 struct inode *inode;
Sage Weilafcdaea2009-10-14 14:27:38 -07002970
2971 dout("flush_dirty_caps\n");
2972 spin_lock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07002973 while (!list_empty(&mdsc->cap_dirty)) {
2974 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
2975 i_dirty_item);
Sage Weil70b666c2011-05-27 09:24:26 -07002976 inode = &ci->vfs_inode;
2977 ihold(inode);
Sage Weildb354052011-05-24 11:46:31 -07002978 dout("flush_dirty_caps %p\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07002979 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weil70b666c2011-05-27 09:24:26 -07002980 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
2981 iput(inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07002982 spin_lock(&mdsc->cap_dirty_lock);
2983 }
2984 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07002985 dout("flush_dirty_caps done\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07002986}
2987
2988/*
Sage Weila8599bd2009-10-06 11:31:12 -07002989 * Drop open file reference. If we were the last open file,
2990 * we may need to release capabilities to the MDS (or schedule
2991 * their delayed release).
2992 */
2993void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2994{
2995 struct inode *inode = &ci->vfs_inode;
2996 int last = 0;
2997
Sage Weilbe655592011-11-30 09:47:09 -08002998 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002999 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
3000 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
3001 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
3002 if (--ci->i_nr_by_mode[fmode] == 0)
3003 last++;
Sage Weilbe655592011-11-30 09:47:09 -08003004 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003005
3006 if (last && ci->i_vino.snap == CEPH_NOSNAP)
3007 ceph_check_caps(ci, 0, NULL);
3008}
3009
3010/*
3011 * Helpers for embedding cap and dentry lease releases into mds
3012 * requests.
3013 *
3014 * @force is used by dentry_release (below) to force inclusion of a
3015 * record for the directory inode, even when there aren't any caps to
3016 * drop.
3017 */
3018int ceph_encode_inode_release(void **p, struct inode *inode,
3019 int mds, int drop, int unless, int force)
3020{
3021 struct ceph_inode_info *ci = ceph_inode(inode);
3022 struct ceph_cap *cap;
3023 struct ceph_mds_request_release *rel = *p;
Sage Weilec97f882010-06-24 15:12:37 -07003024 int used, dirty;
Sage Weila8599bd2009-10-06 11:31:12 -07003025 int ret = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07003026
Sage Weilbe655592011-11-30 09:47:09 -08003027 spin_lock(&ci->i_ceph_lock);
Sage Weil916623d2010-03-16 15:01:07 -07003028 used = __ceph_caps_used(ci);
Sage Weilec97f882010-06-24 15:12:37 -07003029 dirty = __ceph_caps_dirty(ci);
Sage Weil916623d2010-03-16 15:01:07 -07003030
Sage Weilec97f882010-06-24 15:12:37 -07003031 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3032 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
Sage Weil916623d2010-03-16 15:01:07 -07003033 ceph_cap_string(unless));
3034
Sage Weilec97f882010-06-24 15:12:37 -07003035 /* only drop unused, clean caps */
3036 drop &= ~(used | dirty);
Sage Weil916623d2010-03-16 15:01:07 -07003037
Sage Weila8599bd2009-10-06 11:31:12 -07003038 cap = __get_cap_for_mds(ci, mds);
3039 if (cap && __cap_is_valid(cap)) {
3040 if (force ||
3041 ((cap->issued & drop) &&
3042 (cap->issued & unless) == 0)) {
3043 if ((cap->issued & drop) &&
3044 (cap->issued & unless) == 0) {
3045 dout("encode_inode_release %p cap %p %s -> "
3046 "%s\n", inode, cap,
3047 ceph_cap_string(cap->issued),
3048 ceph_cap_string(cap->issued & ~drop));
3049 cap->issued &= ~drop;
3050 cap->implemented &= ~drop;
3051 if (ci->i_ceph_flags & CEPH_I_NODELAY) {
3052 int wanted = __ceph_caps_wanted(ci);
3053 dout(" wanted %s -> %s (act %s)\n",
3054 ceph_cap_string(cap->mds_wanted),
3055 ceph_cap_string(cap->mds_wanted &
3056 ~wanted),
3057 ceph_cap_string(wanted));
3058 cap->mds_wanted &= wanted;
3059 }
3060 } else {
3061 dout("encode_inode_release %p cap %p %s"
3062 " (force)\n", inode, cap,
3063 ceph_cap_string(cap->issued));
3064 }
3065
3066 rel->ino = cpu_to_le64(ceph_ino(inode));
3067 rel->cap_id = cpu_to_le64(cap->cap_id);
3068 rel->seq = cpu_to_le32(cap->seq);
3069 rel->issue_seq = cpu_to_le32(cap->issue_seq),
3070 rel->mseq = cpu_to_le32(cap->mseq);
3071 rel->caps = cpu_to_le32(cap->issued);
3072 rel->wanted = cpu_to_le32(cap->mds_wanted);
3073 rel->dname_len = 0;
3074 rel->dname_seq = 0;
3075 *p += sizeof(*rel);
3076 ret = 1;
3077 } else {
3078 dout("encode_inode_release %p cap %p %s\n",
3079 inode, cap, ceph_cap_string(cap->issued));
3080 }
3081 }
Sage Weilbe655592011-11-30 09:47:09 -08003082 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003083 return ret;
3084}
3085
3086int ceph_encode_dentry_release(void **p, struct dentry *dentry,
3087 int mds, int drop, int unless)
3088{
3089 struct inode *dir = dentry->d_parent->d_inode;
3090 struct ceph_mds_request_release *rel = *p;
3091 struct ceph_dentry_info *di = ceph_dentry(dentry);
3092 int force = 0;
3093 int ret;
3094
3095 /*
3096 * force an record for the directory caps if we have a dentry lease.
Sage Weilbe655592011-11-30 09:47:09 -08003097 * this is racy (can't take i_ceph_lock and d_lock together), but it
Sage Weila8599bd2009-10-06 11:31:12 -07003098 * doesn't have to be perfect; the mds will revoke anything we don't
3099 * release.
3100 */
3101 spin_lock(&dentry->d_lock);
3102 if (di->lease_session && di->lease_session->s_mds == mds)
3103 force = 1;
3104 spin_unlock(&dentry->d_lock);
3105
3106 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
3107
3108 spin_lock(&dentry->d_lock);
3109 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
3110 dout("encode_dentry_release %p mds%d seq %d\n",
3111 dentry, mds, (int)di->lease_seq);
3112 rel->dname_len = cpu_to_le32(dentry->d_name.len);
3113 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3114 *p += dentry->d_name.len;
3115 rel->dname_seq = cpu_to_le32(di->lease_seq);
Sage Weil1dadcce2010-07-23 13:54:21 -07003116 __ceph_mdsc_drop_dentry_lease(dentry);
Sage Weila8599bd2009-10-06 11:31:12 -07003117 }
3118 spin_unlock(&dentry->d_lock);
3119 return ret;
3120}