blob: 5a26bc1dd799adc45a75f0b477c25a84c19844bb [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
majianpeng93faca62013-06-26 11:15:27 +0800150void ceph_reserve_caps(struct ceph_mds_client *mdsc,
Yehuda Sadeh37151662010-06-17 16:16:12 -0700151 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);
Sage Weila8599bd2009-10-06 11:31:12 -0700158
159 dout("reserve caps ctx=%p need=%d\n", ctx, need);
160
161 /* first reserve any caps that are already allocated */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700162 spin_lock(&mdsc->caps_list_lock);
163 if (mdsc->caps_avail_count >= need)
Sage Weila8599bd2009-10-06 11:31:12 -0700164 have = need;
165 else
Yehuda Sadeh37151662010-06-17 16:16:12 -0700166 have = mdsc->caps_avail_count;
167 mdsc->caps_avail_count -= have;
168 mdsc->caps_reserve_count += have;
169 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
170 mdsc->caps_reserve_count +
171 mdsc->caps_avail_count);
172 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700173
174 for (i = have; i < need; i++) {
175 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
majianpeng93faca62013-06-26 11:15:27 +0800176 if (!cap)
177 break;
Sage Weila8599bd2009-10-06 11:31:12 -0700178 list_add(&cap->caps_item, &newcaps);
179 alloc++;
180 }
majianpeng93faca62013-06-26 11:15:27 +0800181 /* we didn't manage to reserve as much as we needed */
182 if (have + alloc != need)
183 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
184 ctx, need, have + alloc);
Sage Weila8599bd2009-10-06 11:31:12 -0700185
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}
201
Yehuda Sadeh37151662010-06-17 16:16:12 -0700202int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
203 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700204{
205 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
206 if (ctx->count) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700207 spin_lock(&mdsc->caps_list_lock);
208 BUG_ON(mdsc->caps_reserve_count < ctx->count);
209 mdsc->caps_reserve_count -= ctx->count;
210 mdsc->caps_avail_count += ctx->count;
Sage Weila8599bd2009-10-06 11:31:12 -0700211 ctx->count = 0;
212 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700213 mdsc->caps_total_count, mdsc->caps_use_count,
214 mdsc->caps_reserve_count, mdsc->caps_avail_count);
215 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
216 mdsc->caps_reserve_count +
217 mdsc->caps_avail_count);
218 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700219 }
220 return 0;
221}
222
Yehuda Sadeh37151662010-06-17 16:16:12 -0700223static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc,
224 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700225{
226 struct ceph_cap *cap = NULL;
227
228 /* temporary, until we do something about cap import/export */
Sage Weil443b3762010-06-29 09:28:39 -0700229 if (!ctx) {
230 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
231 if (cap) {
Yan, Zheng4d1d05342012-11-03 10:32:37 +0800232 spin_lock(&mdsc->caps_list_lock);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700233 mdsc->caps_use_count++;
234 mdsc->caps_total_count++;
Yan, Zheng4d1d05342012-11-03 10:32:37 +0800235 spin_unlock(&mdsc->caps_list_lock);
Sage Weil443b3762010-06-29 09:28:39 -0700236 }
237 return cap;
238 }
Sage Weila8599bd2009-10-06 11:31:12 -0700239
Yehuda Sadeh37151662010-06-17 16:16:12 -0700240 spin_lock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700241 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700242 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
243 mdsc->caps_reserve_count, mdsc->caps_avail_count);
Sage Weila8599bd2009-10-06 11:31:12 -0700244 BUG_ON(!ctx->count);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700245 BUG_ON(ctx->count > mdsc->caps_reserve_count);
246 BUG_ON(list_empty(&mdsc->caps_list));
Sage Weila8599bd2009-10-06 11:31:12 -0700247
248 ctx->count--;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700249 mdsc->caps_reserve_count--;
250 mdsc->caps_use_count++;
Sage Weila8599bd2009-10-06 11:31:12 -0700251
Yehuda Sadeh37151662010-06-17 16:16:12 -0700252 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
Sage Weila8599bd2009-10-06 11:31:12 -0700253 list_del(&cap->caps_item);
254
Yehuda Sadeh37151662010-06-17 16:16:12 -0700255 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
256 mdsc->caps_reserve_count + mdsc->caps_avail_count);
257 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700258 return cap;
259}
260
Yehuda Sadeh37151662010-06-17 16:16:12 -0700261void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700262{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700263 spin_lock(&mdsc->caps_list_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800264 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700265 cap, mdsc->caps_total_count, mdsc->caps_use_count,
266 mdsc->caps_reserve_count, mdsc->caps_avail_count);
267 mdsc->caps_use_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700268 /*
Sage Weil85ccce42010-02-17 10:02:43 -0800269 * Keep some preallocated caps around (ceph_min_count), to
270 * avoid lots of free/alloc churn.
Sage Weila8599bd2009-10-06 11:31:12 -0700271 */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700272 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
273 mdsc->caps_min_count) {
274 mdsc->caps_total_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700275 kmem_cache_free(ceph_cap_cachep, cap);
276 } else {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700277 mdsc->caps_avail_count++;
278 list_add(&cap->caps_item, &mdsc->caps_list);
Sage Weila8599bd2009-10-06 11:31:12 -0700279 }
280
Yehuda Sadeh37151662010-06-17 16:16:12 -0700281 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
282 mdsc->caps_reserve_count + mdsc->caps_avail_count);
283 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700284}
285
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700286void ceph_reservation_status(struct ceph_fs_client *fsc,
Sage Weil85ccce42010-02-17 10:02:43 -0800287 int *total, int *avail, int *used, int *reserved,
288 int *min)
Sage Weila8599bd2009-10-06 11:31:12 -0700289{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700290 struct ceph_mds_client *mdsc = fsc->mdsc;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700291
Sage Weila8599bd2009-10-06 11:31:12 -0700292 if (total)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700293 *total = mdsc->caps_total_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700294 if (avail)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700295 *avail = mdsc->caps_avail_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700296 if (used)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700297 *used = mdsc->caps_use_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700298 if (reserved)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700299 *reserved = mdsc->caps_reserve_count;
Sage Weil85ccce42010-02-17 10:02:43 -0800300 if (min)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700301 *min = mdsc->caps_min_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700302}
303
304/*
305 * Find ceph_cap for given mds, if any.
306 *
Sage Weilbe655592011-11-30 09:47:09 -0800307 * Called with i_ceph_lock held.
Sage Weila8599bd2009-10-06 11:31:12 -0700308 */
309static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
310{
311 struct ceph_cap *cap;
312 struct rb_node *n = ci->i_caps.rb_node;
313
314 while (n) {
315 cap = rb_entry(n, struct ceph_cap, ci_node);
316 if (mds < cap->mds)
317 n = n->rb_left;
318 else if (mds > cap->mds)
319 n = n->rb_right;
320 else
321 return cap;
322 }
323 return NULL;
324}
325
Greg Farnum2bc50252010-06-30 12:44:34 -0700326struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
327{
328 struct ceph_cap *cap;
329
Sage Weilbe655592011-11-30 09:47:09 -0800330 spin_lock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700331 cap = __get_cap_for_mds(ci, mds);
Sage Weilbe655592011-11-30 09:47:09 -0800332 spin_unlock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700333 return cap;
334}
335
Sage Weila8599bd2009-10-06 11:31:12 -0700336/*
Sage Weil33caad32010-05-26 14:31:27 -0700337 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
Sage Weila8599bd2009-10-06 11:31:12 -0700338 */
Sage Weilca81f3f2010-06-10 14:21:36 -0700339static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
Sage Weila8599bd2009-10-06 11:31:12 -0700340{
341 struct ceph_cap *cap;
342 int mds = -1;
343 struct rb_node *p;
344
Sage Weil33caad32010-05-26 14:31:27 -0700345 /* prefer mds with WR|BUFFER|EXCL caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700346 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
347 cap = rb_entry(p, struct ceph_cap, ci_node);
348 mds = cap->mds;
Sage Weila8599bd2009-10-06 11:31:12 -0700349 if (cap->issued & (CEPH_CAP_FILE_WR |
350 CEPH_CAP_FILE_BUFFER |
351 CEPH_CAP_FILE_EXCL))
352 break;
353 }
354 return mds;
355}
356
357int ceph_get_cap_mds(struct inode *inode)
358{
Sage Weilbe655592011-11-30 09:47:09 -0800359 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -0700360 int mds;
Sage Weilbe655592011-11-30 09:47:09 -0800361 spin_lock(&ci->i_ceph_lock);
Sage Weilca81f3f2010-06-10 14:21:36 -0700362 mds = __ceph_get_cap_mds(ceph_inode(inode));
Sage Weilbe655592011-11-30 09:47:09 -0800363 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700364 return mds;
365}
366
367/*
Sage Weilbe655592011-11-30 09:47:09 -0800368 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700369 */
370static void __insert_cap_node(struct ceph_inode_info *ci,
371 struct ceph_cap *new)
372{
373 struct rb_node **p = &ci->i_caps.rb_node;
374 struct rb_node *parent = NULL;
375 struct ceph_cap *cap = NULL;
376
377 while (*p) {
378 parent = *p;
379 cap = rb_entry(parent, struct ceph_cap, ci_node);
380 if (new->mds < cap->mds)
381 p = &(*p)->rb_left;
382 else if (new->mds > cap->mds)
383 p = &(*p)->rb_right;
384 else
385 BUG();
386 }
387
388 rb_link_node(&new->ci_node, parent, p);
389 rb_insert_color(&new->ci_node, &ci->i_caps);
390}
391
392/*
393 * (re)set cap hold timeouts, which control the delayed release
394 * of unused caps back to the MDS. Should be called on cap use.
395 */
396static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
397 struct ceph_inode_info *ci)
398{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700399 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
Sage Weila8599bd2009-10-06 11:31:12 -0700400
401 ci->i_hold_caps_min = round_jiffies(jiffies +
402 ma->caps_wanted_delay_min * HZ);
403 ci->i_hold_caps_max = round_jiffies(jiffies +
404 ma->caps_wanted_delay_max * HZ);
405 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
406 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
407}
408
409/*
410 * (Re)queue cap at the end of the delayed cap release list.
411 *
412 * If I_FLUSH is set, leave the inode at the front of the list.
413 *
Sage Weilbe655592011-11-30 09:47:09 -0800414 * Caller holds i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -0700415 * -> we take mdsc->cap_delay_lock
416 */
417static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
418 struct ceph_inode_info *ci)
419{
420 __cap_set_timeouts(mdsc, ci);
421 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
422 ci->i_ceph_flags, ci->i_hold_caps_max);
423 if (!mdsc->stopping) {
424 spin_lock(&mdsc->cap_delay_lock);
425 if (!list_empty(&ci->i_cap_delay_list)) {
426 if (ci->i_ceph_flags & CEPH_I_FLUSH)
427 goto no_change;
428 list_del_init(&ci->i_cap_delay_list);
429 }
430 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
431no_change:
432 spin_unlock(&mdsc->cap_delay_lock);
433 }
434}
435
436/*
437 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
438 * indicating we should send a cap message to flush dirty metadata
439 * asap, and move to the front of the delayed cap list.
440 */
441static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
442 struct ceph_inode_info *ci)
443{
444 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
445 spin_lock(&mdsc->cap_delay_lock);
446 ci->i_ceph_flags |= CEPH_I_FLUSH;
447 if (!list_empty(&ci->i_cap_delay_list))
448 list_del_init(&ci->i_cap_delay_list);
449 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
450 spin_unlock(&mdsc->cap_delay_lock);
451}
452
453/*
454 * Cancel delayed work on cap.
455 *
Sage Weilbe655592011-11-30 09:47:09 -0800456 * Caller must hold i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700457 */
458static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
459 struct ceph_inode_info *ci)
460{
461 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
462 if (list_empty(&ci->i_cap_delay_list))
463 return;
464 spin_lock(&mdsc->cap_delay_lock);
465 list_del_init(&ci->i_cap_delay_list);
466 spin_unlock(&mdsc->cap_delay_lock);
467}
468
469/*
470 * Common issue checks for add_cap, handle_cap_grant.
471 */
472static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
473 unsigned issued)
474{
475 unsigned had = __ceph_caps_issued(ci, NULL);
476
477 /*
478 * Each time we receive FILE_CACHE anew, we increment
479 * i_rdcache_gen.
480 */
Sage Weil29625072010-05-27 10:40:43 -0700481 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
482 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
Sage Weila8599bd2009-10-06 11:31:12 -0700483 ci->i_rdcache_gen++;
484
485 /*
Yan, Zheng2f276c52013-03-13 19:44:32 +0800486 * if we are newly issued FILE_SHARED, mark dir not complete; we
Sage Weila8599bd2009-10-06 11:31:12 -0700487 * don't know what happened to this directory while we didn't
488 * have the cap.
489 */
490 if ((issued & CEPH_CAP_FILE_SHARED) &&
491 (had & CEPH_CAP_FILE_SHARED) == 0) {
492 ci->i_shared_gen++;
Yan, Zhenga8673d62013-02-18 16:38:14 +0800493 if (S_ISDIR(ci->vfs_inode.i_mode)) {
494 dout(" marking %p NOT complete\n", &ci->vfs_inode);
Yan, Zheng2f276c52013-03-13 19:44:32 +0800495 __ceph_dir_clear_complete(ci);
Yan, Zhenga8673d62013-02-18 16:38:14 +0800496 }
Sage Weila8599bd2009-10-06 11:31:12 -0700497 }
498}
499
500/*
501 * Add a capability under the given MDS session.
502 *
503 * Caller should hold session snap_rwsem (read) and s_mutex.
504 *
505 * @fmode is the open file mode, if we are opening a file, otherwise
506 * it is < 0. (This is so we can atomically add the cap and add an
507 * open file reference to it.)
508 */
509int ceph_add_cap(struct inode *inode,
510 struct ceph_mds_session *session, u64 cap_id,
511 int fmode, unsigned issued, unsigned wanted,
512 unsigned seq, unsigned mseq, u64 realmino, int flags,
513 struct ceph_cap_reservation *caps_reservation)
514{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700515 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -0700516 struct ceph_inode_info *ci = ceph_inode(inode);
517 struct ceph_cap *new_cap = NULL;
518 struct ceph_cap *cap;
519 int mds = session->s_mds;
520 int actual_wanted;
521
522 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
523 session->s_mds, cap_id, ceph_cap_string(issued), seq);
524
525 /*
526 * If we are opening the file, include file mode wanted bits
527 * in wanted.
528 */
529 if (fmode >= 0)
530 wanted |= ceph_caps_for_mode(fmode);
531
532retry:
Sage Weilbe655592011-11-30 09:47:09 -0800533 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700534 cap = __get_cap_for_mds(ci, mds);
535 if (!cap) {
536 if (new_cap) {
537 cap = new_cap;
538 new_cap = NULL;
539 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800540 spin_unlock(&ci->i_ceph_lock);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700541 new_cap = get_cap(mdsc, caps_reservation);
Sage Weila8599bd2009-10-06 11:31:12 -0700542 if (new_cap == NULL)
543 return -ENOMEM;
544 goto retry;
545 }
546
547 cap->issued = 0;
548 cap->implemented = 0;
549 cap->mds = mds;
550 cap->mds_wanted = 0;
Yan, Zheng964266c2013-02-27 09:26:09 +0800551 cap->mseq = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700552
553 cap->ci = ci;
554 __insert_cap_node(ci, cap);
555
556 /* clear out old exporting info? (i.e. on cap import) */
557 if (ci->i_cap_exporting_mds == mds) {
558 ci->i_cap_exporting_issued = 0;
559 ci->i_cap_exporting_mseq = 0;
560 ci->i_cap_exporting_mds = -1;
561 }
562
563 /* add to session cap list */
564 cap->session = session;
565 spin_lock(&session->s_cap_lock);
566 list_add_tail(&cap->session_caps, &session->s_caps);
567 session->s_nr_caps++;
568 spin_unlock(&session->s_cap_lock);
Sage Weil35403032011-05-12 15:13:23 -0700569 } else if (new_cap)
570 ceph_put_cap(mdsc, new_cap);
Sage Weila8599bd2009-10-06 11:31:12 -0700571
572 if (!ci->i_snap_realm) {
573 /*
574 * add this inode to the appropriate snap realm
575 */
576 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
577 realmino);
578 if (realm) {
579 ceph_get_snap_realm(mdsc, realm);
580 spin_lock(&realm->inodes_with_caps_lock);
581 ci->i_snap_realm = realm;
582 list_add(&ci->i_snap_realm_item,
583 &realm->inodes_with_caps);
584 spin_unlock(&realm->inodes_with_caps_lock);
585 } else {
586 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
587 realmino);
Sage Weilb8cd07e2010-07-16 12:00:02 -0700588 WARN_ON(!realm);
Sage Weila8599bd2009-10-06 11:31:12 -0700589 }
590 }
591
592 __check_cap_issue(ci, cap, issued);
593
594 /*
595 * If we are issued caps we don't want, or the mds' wanted
596 * value appears to be off, queue a check so we'll release
597 * later and/or update the mds wanted value.
598 */
599 actual_wanted = __ceph_caps_wanted(ci);
600 if ((wanted & ~actual_wanted) ||
601 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
602 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
603 ceph_cap_string(issued), ceph_cap_string(wanted),
604 ceph_cap_string(actual_wanted));
605 __cap_delay_requeue(mdsc, ci);
606 }
607
Yan, Zhengb8c2f3a2013-05-31 16:37:11 +0800608 if (flags & CEPH_CAP_FLAG_AUTH) {
609 if (ci->i_auth_cap == NULL ||
610 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0)
611 ci->i_auth_cap = cap;
612 } else if (ci->i_auth_cap == cap) {
Sage Weila8599bd2009-10-06 11:31:12 -0700613 ci->i_auth_cap = NULL;
Yan, Zheng8a92a112013-01-04 14:28:07 +0800614 spin_lock(&mdsc->cap_dirty_lock);
615 if (!list_empty(&ci->i_dirty_item)) {
616 dout(" moving %p to cap_dirty_migrating\n", inode);
617 list_move(&ci->i_dirty_item,
618 &mdsc->cap_dirty_migrating);
619 }
620 spin_unlock(&mdsc->cap_dirty_lock);
621 }
Sage Weila8599bd2009-10-06 11:31:12 -0700622
623 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
624 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
625 ceph_cap_string(issued|cap->issued), seq, mds);
626 cap->cap_id = cap_id;
627 cap->issued = issued;
628 cap->implemented |= issued;
Yan, Zheng964266c2013-02-27 09:26:09 +0800629 if (mseq > cap->mseq)
630 cap->mds_wanted = wanted;
631 else
632 cap->mds_wanted |= wanted;
Sage Weila8599bd2009-10-06 11:31:12 -0700633 cap->seq = seq;
634 cap->issue_seq = seq;
635 cap->mseq = mseq;
Sage Weil685f9a5d2009-11-09 12:05:48 -0800636 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700637
638 if (fmode >= 0)
639 __ceph_get_fmode(ci, fmode);
Sage Weilbe655592011-11-30 09:47:09 -0800640 spin_unlock(&ci->i_ceph_lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700641 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -0700642 return 0;
643}
644
645/*
646 * Return true if cap has not timed out and belongs to the current
647 * generation of the MDS session (i.e. has not gone 'stale' due to
648 * us losing touch with the mds).
649 */
650static int __cap_is_valid(struct ceph_cap *cap)
651{
652 unsigned long ttl;
Sage Weilcdac8302009-11-10 16:02:23 -0800653 u32 gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700654
Alex Elderd8fb02a2012-01-12 17:48:10 -0800655 spin_lock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700656 gen = cap->session->s_cap_gen;
657 ttl = cap->session->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800658 spin_unlock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700659
Sage Weil685f9a5d2009-11-09 12:05:48 -0800660 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
Sage Weila8599bd2009-10-06 11:31:12 -0700661 dout("__cap_is_valid %p cap %p issued %s "
662 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
Sage Weil685f9a5d2009-11-09 12:05:48 -0800663 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
Sage Weila8599bd2009-10-06 11:31:12 -0700664 return 0;
665 }
666
667 return 1;
668}
669
670/*
671 * Return set of valid cap bits issued to us. Note that caps time
672 * out, and may be invalidated in bulk if the client session times out
673 * and session->s_cap_gen is bumped.
674 */
675int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
676{
Sage Weil7af8f1e2010-03-01 15:17:34 -0800677 int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
Sage Weila8599bd2009-10-06 11:31:12 -0700678 struct ceph_cap *cap;
679 struct rb_node *p;
680
681 if (implemented)
682 *implemented = 0;
683 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
684 cap = rb_entry(p, struct ceph_cap, ci_node);
685 if (!__cap_is_valid(cap))
686 continue;
687 dout("__ceph_caps_issued %p cap %p issued %s\n",
688 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
689 have |= cap->issued;
690 if (implemented)
691 *implemented |= cap->implemented;
692 }
Yan, Zhengb1530f52013-07-02 12:40:20 +0800693 /*
694 * exclude caps issued by non-auth MDS, but are been revoking
695 * by the auth MDS. The non-auth MDS should be revoking/exporting
696 * these caps, but the message is delayed.
697 */
698 if (ci->i_auth_cap) {
699 cap = ci->i_auth_cap;
700 have &= ~cap->implemented | cap->issued;
701 }
Sage Weila8599bd2009-10-06 11:31:12 -0700702 return have;
703}
704
705/*
706 * Get cap bits issued by caps other than @ocap
707 */
708int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
709{
710 int have = ci->i_snap_caps;
711 struct ceph_cap *cap;
712 struct rb_node *p;
713
714 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
715 cap = rb_entry(p, struct ceph_cap, ci_node);
716 if (cap == ocap)
717 continue;
718 if (!__cap_is_valid(cap))
719 continue;
720 have |= cap->issued;
721 }
722 return have;
723}
724
725/*
726 * Move a cap to the end of the LRU (oldest caps at list head, newest
727 * at list tail).
728 */
729static void __touch_cap(struct ceph_cap *cap)
730{
731 struct ceph_mds_session *s = cap->session;
732
Sage Weila8599bd2009-10-06 11:31:12 -0700733 spin_lock(&s->s_cap_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800734 if (s->s_cap_iterator == NULL) {
Sage Weil5dacf092009-12-21 20:40:34 -0800735 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
736 s->s_mds);
737 list_move_tail(&cap->session_caps, &s->s_caps);
738 } else {
739 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
740 &cap->ci->vfs_inode, cap, s->s_mds);
741 }
Sage Weila8599bd2009-10-06 11:31:12 -0700742 spin_unlock(&s->s_cap_lock);
743}
744
745/*
746 * Check if we hold the given mask. If so, move the cap(s) to the
747 * front of their respective LRUs. (This is the preferred way for
748 * callers to check for caps they want.)
749 */
750int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
751{
752 struct ceph_cap *cap;
753 struct rb_node *p;
754 int have = ci->i_snap_caps;
755
756 if ((have & mask) == mask) {
757 dout("__ceph_caps_issued_mask %p snap issued %s"
758 " (mask %s)\n", &ci->vfs_inode,
759 ceph_cap_string(have),
760 ceph_cap_string(mask));
761 return 1;
762 }
763
764 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
765 cap = rb_entry(p, struct ceph_cap, ci_node);
766 if (!__cap_is_valid(cap))
767 continue;
768 if ((cap->issued & mask) == mask) {
769 dout("__ceph_caps_issued_mask %p cap %p issued %s"
770 " (mask %s)\n", &ci->vfs_inode, cap,
771 ceph_cap_string(cap->issued),
772 ceph_cap_string(mask));
773 if (touch)
774 __touch_cap(cap);
775 return 1;
776 }
777
778 /* does a combination of caps satisfy mask? */
779 have |= cap->issued;
780 if ((have & mask) == mask) {
781 dout("__ceph_caps_issued_mask %p combo issued %s"
782 " (mask %s)\n", &ci->vfs_inode,
783 ceph_cap_string(cap->issued),
784 ceph_cap_string(mask));
785 if (touch) {
786 struct rb_node *q;
787
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300788 /* touch this + preceding caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700789 __touch_cap(cap);
790 for (q = rb_first(&ci->i_caps); q != p;
791 q = rb_next(q)) {
792 cap = rb_entry(q, struct ceph_cap,
793 ci_node);
794 if (!__cap_is_valid(cap))
795 continue;
796 __touch_cap(cap);
797 }
798 }
799 return 1;
800 }
801 }
802
803 return 0;
804}
805
806/*
807 * Return true if mask caps are currently being revoked by an MDS.
808 */
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800809int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
810 struct ceph_cap *ocap, int mask)
811{
812 struct ceph_cap *cap;
813 struct rb_node *p;
814
815 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
816 cap = rb_entry(p, struct ceph_cap, ci_node);
817 if (cap != ocap && __cap_is_valid(cap) &&
818 (cap->implemented & ~cap->issued & mask))
819 return 1;
820 }
821 return 0;
822}
823
Sage Weila8599bd2009-10-06 11:31:12 -0700824int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
825{
826 struct inode *inode = &ci->vfs_inode;
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800827 int ret;
Sage Weila8599bd2009-10-06 11:31:12 -0700828
Sage Weilbe655592011-11-30 09:47:09 -0800829 spin_lock(&ci->i_ceph_lock);
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800830 ret = __ceph_caps_revoking_other(ci, NULL, mask);
Sage Weilbe655592011-11-30 09:47:09 -0800831 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700832 dout("ceph_caps_revoking %p %s = %d\n", inode,
833 ceph_cap_string(mask), ret);
834 return ret;
835}
836
837int __ceph_caps_used(struct ceph_inode_info *ci)
838{
839 int used = 0;
840 if (ci->i_pin_ref)
841 used |= CEPH_CAP_PIN;
842 if (ci->i_rd_ref)
843 used |= CEPH_CAP_FILE_RD;
Sage Weila43fb732010-09-17 09:54:08 -0700844 if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages)
Sage Weila8599bd2009-10-06 11:31:12 -0700845 used |= CEPH_CAP_FILE_CACHE;
846 if (ci->i_wr_ref)
847 used |= CEPH_CAP_FILE_WR;
Henry C Changd3d07202011-05-11 10:29:54 +0000848 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
Sage Weila8599bd2009-10-06 11:31:12 -0700849 used |= CEPH_CAP_FILE_BUFFER;
850 return used;
851}
852
853/*
854 * wanted, by virtue of open file modes
855 */
856int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
857{
858 int want = 0;
859 int mode;
Sage Weil33caad32010-05-26 14:31:27 -0700860 for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
Sage Weila8599bd2009-10-06 11:31:12 -0700861 if (ci->i_nr_by_mode[mode])
862 want |= ceph_caps_for_mode(mode);
863 return want;
864}
865
866/*
867 * Return caps we have registered with the MDS(s) as 'wanted'.
868 */
869int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
870{
871 struct ceph_cap *cap;
872 struct rb_node *p;
873 int mds_wanted = 0;
874
875 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
876 cap = rb_entry(p, struct ceph_cap, ci_node);
877 if (!__cap_is_valid(cap))
878 continue;
879 mds_wanted |= cap->mds_wanted;
880 }
881 return mds_wanted;
882}
883
884/*
Sage Weilbe655592011-11-30 09:47:09 -0800885 * called under i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -0700886 */
887static int __ceph_is_any_caps(struct ceph_inode_info *ci)
888{
889 return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
890}
891
892/*
Sage Weilf818a732010-05-11 20:56:31 -0700893 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
894 *
Sage Weilbe655592011-11-30 09:47:09 -0800895 * caller should hold i_ceph_lock.
Sage Weila6369742010-02-22 13:59:00 -0800896 * caller will not hold session s_mutex if called from destroy_inode.
Sage Weila8599bd2009-10-06 11:31:12 -0700897 */
Sage Weil7c1332b2010-02-16 11:39:45 -0800898void __ceph_remove_cap(struct ceph_cap *cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700899{
900 struct ceph_mds_session *session = cap->session;
901 struct ceph_inode_info *ci = cap->ci;
Cheng Renquan640ef792010-03-26 17:40:33 +0800902 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700903 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weilf818a732010-05-11 20:56:31 -0700904 int removed = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700905
906 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
907
Sage Weil7c1332b2010-02-16 11:39:45 -0800908 /* remove from session list */
909 spin_lock(&session->s_cap_lock);
910 if (session->s_cap_iterator == cap) {
911 /* not yet, we are iterating over this very cap */
912 dout("__ceph_remove_cap delaying %p removal from session %p\n",
913 cap, cap->session);
914 } else {
915 list_del_init(&cap->session_caps);
916 session->s_nr_caps--;
917 cap->session = NULL;
Sage Weilf818a732010-05-11 20:56:31 -0700918 removed = 1;
Sage Weil7c1332b2010-02-16 11:39:45 -0800919 }
Sage Weilf818a732010-05-11 20:56:31 -0700920 /* protect backpointer with s_cap_lock: see iterate_session_caps */
921 cap->ci = NULL;
Sage Weil7c1332b2010-02-16 11:39:45 -0800922 spin_unlock(&session->s_cap_lock);
923
Sage Weilf818a732010-05-11 20:56:31 -0700924 /* remove from inode list */
925 rb_erase(&cap->ci_node, &ci->i_caps);
926 if (ci->i_auth_cap == cap)
927 ci->i_auth_cap = NULL;
928
929 if (removed)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700930 ceph_put_cap(mdsc, cap);
Sage Weila8599bd2009-10-06 11:31:12 -0700931
932 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
933 struct ceph_snap_realm *realm = ci->i_snap_realm;
934 spin_lock(&realm->inodes_with_caps_lock);
935 list_del_init(&ci->i_snap_realm_item);
936 ci->i_snap_realm_counter++;
937 ci->i_snap_realm = NULL;
938 spin_unlock(&realm->inodes_with_caps_lock);
939 ceph_put_snap_realm(mdsc, realm);
940 }
941 if (!__ceph_is_any_real_caps(ci))
942 __cap_delay_cancel(mdsc, ci);
943}
944
945/*
946 * Build and send a cap message to the given MDS.
947 *
948 * Caller should be holding s_mutex.
949 */
950static int send_cap_msg(struct ceph_mds_session *session,
951 u64 ino, u64 cid, int op,
952 int caps, int wanted, int dirty,
953 u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
954 u64 size, u64 max_size,
955 struct timespec *mtime, struct timespec *atime,
956 u64 time_warp_seq,
Eric W. Biederman05cb11c2013-01-31 02:56:19 -0800957 kuid_t uid, kgid_t gid, umode_t mode,
Sage Weila8599bd2009-10-06 11:31:12 -0700958 u64 xattr_version,
959 struct ceph_buffer *xattrs_buf,
960 u64 follows)
961{
962 struct ceph_mds_caps *fc;
963 struct ceph_msg *msg;
964
965 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
966 " seq %u/%u mseq %u follows %lld size %llu/%llu"
967 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
968 cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
969 ceph_cap_string(dirty),
970 seq, issue_seq, mseq, follows, size, max_size,
971 xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
972
Sage Weilb61c2762011-08-09 15:03:46 -0700973 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -0700974 if (!msg)
975 return -ENOMEM;
Sage Weila8599bd2009-10-06 11:31:12 -0700976
Sage Weil6df058c2009-12-22 11:24:33 -0800977 msg->hdr.tid = cpu_to_le64(flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -0700978
Sage Weil6df058c2009-12-22 11:24:33 -0800979 fc = msg->front.iov_base;
Sage Weila8599bd2009-10-06 11:31:12 -0700980 memset(fc, 0, sizeof(*fc));
981
982 fc->cap_id = cpu_to_le64(cid);
983 fc->op = cpu_to_le32(op);
984 fc->seq = cpu_to_le32(seq);
Sage Weila8599bd2009-10-06 11:31:12 -0700985 fc->issue_seq = cpu_to_le32(issue_seq);
986 fc->migrate_seq = cpu_to_le32(mseq);
987 fc->caps = cpu_to_le32(caps);
988 fc->wanted = cpu_to_le32(wanted);
989 fc->dirty = cpu_to_le32(dirty);
990 fc->ino = cpu_to_le64(ino);
991 fc->snap_follows = cpu_to_le64(follows);
992
993 fc->size = cpu_to_le64(size);
994 fc->max_size = cpu_to_le64(max_size);
995 if (mtime)
996 ceph_encode_timespec(&fc->mtime, mtime);
997 if (atime)
998 ceph_encode_timespec(&fc->atime, atime);
999 fc->time_warp_seq = cpu_to_le32(time_warp_seq);
1000
Eric W. Biederman05cb11c2013-01-31 02:56:19 -08001001 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid));
1002 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid));
Sage Weila8599bd2009-10-06 11:31:12 -07001003 fc->mode = cpu_to_le32(mode);
1004
1005 fc->xattr_version = cpu_to_le64(xattr_version);
1006 if (xattrs_buf) {
1007 msg->middle = ceph_buffer_get(xattrs_buf);
1008 fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
1009 msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
1010 }
1011
1012 ceph_con_send(&session->s_con, msg);
1013 return 0;
1014}
1015
Yan, Zhengd40ee0d2013-02-18 13:43:43 +08001016void __queue_cap_release(struct ceph_mds_session *session,
1017 u64 ino, u64 cap_id, u32 migrate_seq,
1018 u32 issue_seq)
Sage Weil3d7ded42010-06-09 16:47:10 -07001019{
1020 struct ceph_msg *msg;
1021 struct ceph_mds_cap_release *head;
1022 struct ceph_mds_cap_item *item;
1023
1024 spin_lock(&session->s_cap_lock);
1025 BUG_ON(!session->s_num_cap_releases);
1026 msg = list_first_entry(&session->s_cap_releases,
1027 struct ceph_msg, list_head);
1028
1029 dout(" adding %llx release to mds%d msg %p (%d left)\n",
1030 ino, session->s_mds, msg, session->s_num_cap_releases);
1031
1032 BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
1033 head = msg->front.iov_base;
Wei Yongjunb905a7f2012-09-28 12:59:16 +08001034 le32_add_cpu(&head->num, 1);
Sage Weil3d7ded42010-06-09 16:47:10 -07001035 item = msg->front.iov_base + msg->front.iov_len;
1036 item->ino = cpu_to_le64(ino);
1037 item->cap_id = cpu_to_le64(cap_id);
1038 item->migrate_seq = cpu_to_le32(migrate_seq);
1039 item->seq = cpu_to_le32(issue_seq);
1040
1041 session->s_num_cap_releases--;
1042
1043 msg->front.iov_len += sizeof(*item);
1044 if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
1045 dout(" release msg %p full\n", msg);
1046 list_move_tail(&msg->list_head, &session->s_cap_releases_done);
1047 } else {
1048 dout(" release msg %p at %d/%d (%d)\n", msg,
1049 (int)le32_to_cpu(head->num),
1050 (int)CEPH_CAPS_PER_RELEASE,
1051 (int)msg->front.iov_len);
1052 }
1053 spin_unlock(&session->s_cap_lock);
1054}
1055
Sage Weila8599bd2009-10-06 11:31:12 -07001056/*
Sage Weila6369742010-02-22 13:59:00 -08001057 * Queue cap releases when an inode is dropped from our cache. Since
Sage Weilbe655592011-11-30 09:47:09 -08001058 * inode is about to be destroyed, there is no need for i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001059 */
1060void ceph_queue_caps_release(struct inode *inode)
1061{
1062 struct ceph_inode_info *ci = ceph_inode(inode);
1063 struct rb_node *p;
1064
Sage Weila8599bd2009-10-06 11:31:12 -07001065 p = rb_first(&ci->i_caps);
1066 while (p) {
1067 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1068 struct ceph_mds_session *session = cap->session;
Sage Weila8599bd2009-10-06 11:31:12 -07001069
Sage Weil3d7ded42010-06-09 16:47:10 -07001070 __queue_cap_release(session, ceph_ino(inode), cap->cap_id,
1071 cap->mseq, cap->issue_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07001072 p = rb_next(p);
Sage Weil7c1332b2010-02-16 11:39:45 -08001073 __ceph_remove_cap(cap);
Sage Weila8599bd2009-10-06 11:31:12 -07001074 }
Sage Weila8599bd2009-10-06 11:31:12 -07001075}
1076
1077/*
1078 * Send a cap msg on the given inode. Update our caps state, then
Sage Weilbe655592011-11-30 09:47:09 -08001079 * drop i_ceph_lock and send the message.
Sage Weila8599bd2009-10-06 11:31:12 -07001080 *
1081 * Make note of max_size reported/requested from mds, revoked caps
1082 * that have now been implemented.
1083 *
1084 * Make half-hearted attempt ot to invalidate page cache if we are
1085 * dropping RDCACHE. Note that this will leave behind locked pages
1086 * that we'll then need to deal with elsewhere.
1087 *
1088 * Return non-zero if delayed release, or we experienced an error
1089 * such that the caller should requeue + retry later.
1090 *
Sage Weilbe655592011-11-30 09:47:09 -08001091 * called with i_ceph_lock, then drops it.
Sage Weila8599bd2009-10-06 11:31:12 -07001092 * caller should hold snap_rwsem (read), s_mutex.
1093 */
1094static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1095 int op, int used, int want, int retain, int flushing,
1096 unsigned *pflush_tid)
Sage Weilbe655592011-11-30 09:47:09 -08001097 __releases(cap->ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001098{
1099 struct ceph_inode_info *ci = cap->ci;
1100 struct inode *inode = &ci->vfs_inode;
1101 u64 cap_id = cap->cap_id;
Sage Weil68c28322010-02-09 13:41:47 -08001102 int held, revoking, dropping, keep;
Sage Weila8599bd2009-10-06 11:31:12 -07001103 u64 seq, issue_seq, mseq, time_warp_seq, follows;
1104 u64 size, max_size;
1105 struct timespec mtime, atime;
1106 int wake = 0;
Al Viro5706b272011-07-26 04:52:22 -04001107 umode_t mode;
Eric W. Biederman05cb11c2013-01-31 02:56:19 -08001108 kuid_t uid;
1109 kgid_t gid;
Sage Weila8599bd2009-10-06 11:31:12 -07001110 struct ceph_mds_session *session;
1111 u64 xattr_version = 0;
Sage Weil082afec2010-08-22 15:16:41 -07001112 struct ceph_buffer *xattr_blob = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07001113 int delayed = 0;
1114 u64 flush_tid = 0;
1115 int i;
1116 int ret;
1117
Sage Weil68c28322010-02-09 13:41:47 -08001118 held = cap->issued | cap->implemented;
1119 revoking = cap->implemented & ~cap->issued;
1120 retain &= ~revoking;
1121 dropping = cap->issued & ~retain;
1122
Sage Weila8599bd2009-10-06 11:31:12 -07001123 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1124 inode, cap, cap->session,
1125 ceph_cap_string(held), ceph_cap_string(held & retain),
1126 ceph_cap_string(revoking));
1127 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1128
1129 session = cap->session;
1130
1131 /* don't release wanted unless we've waited a bit. */
1132 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1133 time_before(jiffies, ci->i_hold_caps_min)) {
1134 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1135 ceph_cap_string(cap->issued),
1136 ceph_cap_string(cap->issued & retain),
1137 ceph_cap_string(cap->mds_wanted),
1138 ceph_cap_string(want));
1139 want |= cap->mds_wanted;
1140 retain |= cap->issued;
1141 delayed = 1;
1142 }
1143 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1144
1145 cap->issued &= retain; /* drop bits we don't want */
1146 if (cap->implemented & ~cap->issued) {
1147 /*
1148 * Wake up any waiters on wanted -> needed transition.
1149 * This is due to the weird transition from buffered
1150 * to sync IO... we need to flush dirty pages _before_
1151 * allowing sync writes to avoid reordering.
1152 */
1153 wake = 1;
1154 }
1155 cap->implemented &= cap->issued | used;
1156 cap->mds_wanted = want;
1157
1158 if (flushing) {
1159 /*
1160 * assign a tid for flush operations so we can avoid
1161 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1162 * clean type races. track latest tid for every bit
1163 * so we can handle flush AxFw, flush Fw, and have the
1164 * first ack clean Ax.
1165 */
1166 flush_tid = ++ci->i_cap_flush_last_tid;
1167 if (pflush_tid)
1168 *pflush_tid = flush_tid;
1169 dout(" cap_flush_tid %d\n", (int)flush_tid);
1170 for (i = 0; i < CEPH_CAP_BITS; i++)
1171 if (flushing & (1 << i))
1172 ci->i_cap_flush_tid[i] = flush_tid;
Sage Weil7d8cb262010-08-24 08:44:16 -07001173
1174 follows = ci->i_head_snapc->seq;
1175 } else {
1176 follows = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001177 }
1178
1179 keep = cap->implemented;
1180 seq = cap->seq;
1181 issue_seq = cap->issue_seq;
1182 mseq = cap->mseq;
1183 size = inode->i_size;
1184 ci->i_reported_size = size;
1185 max_size = ci->i_wanted_max_size;
1186 ci->i_requested_max_size = max_size;
1187 mtime = inode->i_mtime;
1188 atime = inode->i_atime;
1189 time_warp_seq = ci->i_time_warp_seq;
Sage Weila8599bd2009-10-06 11:31:12 -07001190 uid = inode->i_uid;
1191 gid = inode->i_gid;
1192 mode = inode->i_mode;
1193
Sage Weil082afec2010-08-22 15:16:41 -07001194 if (flushing & CEPH_CAP_XATTR_EXCL) {
Sage Weila8599bd2009-10-06 11:31:12 -07001195 __ceph_build_xattrs_blob(ci);
Sage Weil082afec2010-08-22 15:16:41 -07001196 xattr_blob = ci->i_xattrs.blob;
1197 xattr_version = ci->i_xattrs.version;
Sage Weila8599bd2009-10-06 11:31:12 -07001198 }
1199
Sage Weilbe655592011-11-30 09:47:09 -08001200 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001201
Sage Weila8599bd2009-10-06 11:31:12 -07001202 ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1203 op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1204 size, max_size, &mtime, &atime, time_warp_seq,
Sage Weil082afec2010-08-22 15:16:41 -07001205 uid, gid, mode, xattr_version, xattr_blob,
Sage Weila8599bd2009-10-06 11:31:12 -07001206 follows);
1207 if (ret < 0) {
1208 dout("error sending cap msg, must requeue %p\n", inode);
1209 delayed = 1;
1210 }
1211
1212 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001213 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07001214
1215 return delayed;
1216}
1217
1218/*
1219 * When a snapshot is taken, clients accumulate dirty metadata on
1220 * inodes with capabilities in ceph_cap_snaps to describe the file
1221 * state at the time the snapshot was taken. This must be flushed
1222 * asynchronously back to the MDS once sync writes complete and dirty
1223 * data is written out.
1224 *
Sage Weile8351242010-09-17 08:03:08 -07001225 * Unless @again is true, skip cap_snaps that were already sent to
1226 * the MDS (i.e., during this session).
1227 *
Sage Weilbe655592011-11-30 09:47:09 -08001228 * Called under i_ceph_lock. Takes s_mutex as needed.
Sage Weila8599bd2009-10-06 11:31:12 -07001229 */
1230void __ceph_flush_snaps(struct ceph_inode_info *ci,
Sage Weile8351242010-09-17 08:03:08 -07001231 struct ceph_mds_session **psession,
1232 int again)
Sage Weilbe655592011-11-30 09:47:09 -08001233 __releases(ci->i_ceph_lock)
1234 __acquires(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001235{
1236 struct inode *inode = &ci->vfs_inode;
1237 int mds;
1238 struct ceph_cap_snap *capsnap;
1239 u32 mseq;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001240 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001241 struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1242 session->s_mutex */
1243 u64 next_follows = 0; /* keep track of how far we've gotten through the
1244 i_cap_snaps list, and skip these entries next time
1245 around to avoid an infinite loop */
1246
1247 if (psession)
1248 session = *psession;
1249
1250 dout("__flush_snaps %p\n", inode);
1251retry:
1252 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1253 /* avoid an infiniute loop after retry */
1254 if (capsnap->follows < next_follows)
1255 continue;
1256 /*
1257 * we need to wait for sync writes to complete and for dirty
1258 * pages to be written out.
1259 */
1260 if (capsnap->dirty_pages || capsnap->writing)
Sage Weilcfc0bf62010-09-14 15:50:59 -07001261 break;
Sage Weila8599bd2009-10-06 11:31:12 -07001262
Sage Weil819ccbf2010-04-01 09:33:46 -07001263 /*
1264 * if cap writeback already occurred, we should have dropped
1265 * the capsnap in ceph_put_wrbuffer_cap_refs.
1266 */
1267 BUG_ON(capsnap->dirty == 0);
1268
Sage Weila8599bd2009-10-06 11:31:12 -07001269 /* pick mds, take s_mutex */
Sage Weilca81f3f2010-06-10 14:21:36 -07001270 if (ci->i_auth_cap == NULL) {
1271 dout("no auth cap (migrating?), doing nothing\n");
1272 goto out;
1273 }
Sage Weile8351242010-09-17 08:03:08 -07001274
1275 /* only flush each capsnap once */
1276 if (!again && !list_empty(&capsnap->flushing_item)) {
1277 dout("already flushed %p, skipping\n", capsnap);
1278 continue;
1279 }
1280
Sage Weilca81f3f2010-06-10 14:21:36 -07001281 mds = ci->i_auth_cap->session->s_mds;
1282 mseq = ci->i_auth_cap->mseq;
1283
Sage Weila8599bd2009-10-06 11:31:12 -07001284 if (session && session->s_mds != mds) {
1285 dout("oops, wrong session %p mutex\n", session);
1286 mutex_unlock(&session->s_mutex);
1287 ceph_put_mds_session(session);
1288 session = NULL;
1289 }
1290 if (!session) {
Sage Weilbe655592011-11-30 09:47:09 -08001291 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001292 mutex_lock(&mdsc->mutex);
1293 session = __ceph_lookup_mds_session(mdsc, mds);
1294 mutex_unlock(&mdsc->mutex);
1295 if (session) {
1296 dout("inverting session/ino locks on %p\n",
1297 session);
1298 mutex_lock(&session->s_mutex);
1299 }
1300 /*
1301 * if session == NULL, we raced against a cap
Sage Weilca81f3f2010-06-10 14:21:36 -07001302 * deletion or migration. retry, and we'll
1303 * get a better @mds value next time.
Sage Weila8599bd2009-10-06 11:31:12 -07001304 */
Sage Weilbe655592011-11-30 09:47:09 -08001305 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001306 goto retry;
1307 }
1308
1309 capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1310 atomic_inc(&capsnap->nref);
1311 if (!list_empty(&capsnap->flushing_item))
1312 list_del_init(&capsnap->flushing_item);
1313 list_add_tail(&capsnap->flushing_item,
1314 &session->s_cap_snaps_flushing);
Sage Weilbe655592011-11-30 09:47:09 -08001315 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001316
Sage Weilcfc0bf62010-09-14 15:50:59 -07001317 dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
1318 inode, capsnap, capsnap->follows, capsnap->flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07001319 send_cap_msg(session, ceph_vino(inode).ino, 0,
1320 CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1321 capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1322 capsnap->size, 0,
1323 &capsnap->mtime, &capsnap->atime,
1324 capsnap->time_warp_seq,
1325 capsnap->uid, capsnap->gid, capsnap->mode,
Sage Weil4a625be2010-08-22 15:03:56 -07001326 capsnap->xattr_version, capsnap->xattr_blob,
Sage Weila8599bd2009-10-06 11:31:12 -07001327 capsnap->follows);
1328
1329 next_follows = capsnap->follows + 1;
1330 ceph_put_cap_snap(capsnap);
1331
Sage Weilbe655592011-11-30 09:47:09 -08001332 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001333 goto retry;
1334 }
1335
1336 /* we flushed them all; remove this inode from the queue */
1337 spin_lock(&mdsc->snap_flush_lock);
1338 list_del_init(&ci->i_snap_flush_item);
1339 spin_unlock(&mdsc->snap_flush_lock);
1340
Sage Weilca81f3f2010-06-10 14:21:36 -07001341out:
Sage Weila8599bd2009-10-06 11:31:12 -07001342 if (psession)
1343 *psession = session;
1344 else if (session) {
1345 mutex_unlock(&session->s_mutex);
1346 ceph_put_mds_session(session);
1347 }
1348}
1349
1350static void ceph_flush_snaps(struct ceph_inode_info *ci)
1351{
Sage Weilbe655592011-11-30 09:47:09 -08001352 spin_lock(&ci->i_ceph_lock);
Sage Weile8351242010-09-17 08:03:08 -07001353 __ceph_flush_snaps(ci, NULL, 0);
Sage Weilbe655592011-11-30 09:47:09 -08001354 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001355}
1356
1357/*
Sage Weilfca65b42011-05-04 11:33:47 -07001358 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1359 * Caller is then responsible for calling __mark_inode_dirty with the
1360 * returned flags value.
Sage Weil76e3b392009-10-15 18:13:53 -07001361 */
Sage Weilfca65b42011-05-04 11:33:47 -07001362int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
Sage Weil76e3b392009-10-15 18:13:53 -07001363{
Cheng Renquan640ef792010-03-26 17:40:33 +08001364 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001365 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil76e3b392009-10-15 18:13:53 -07001366 struct inode *inode = &ci->vfs_inode;
1367 int was = ci->i_dirty_caps;
1368 int dirty = 0;
1369
1370 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1371 ceph_cap_string(mask), ceph_cap_string(was),
1372 ceph_cap_string(was | mask));
1373 ci->i_dirty_caps |= mask;
1374 if (was == 0) {
Sage Weil7d8cb262010-08-24 08:44:16 -07001375 if (!ci->i_head_snapc)
1376 ci->i_head_snapc = ceph_get_snap_context(
1377 ci->i_snap_realm->cached_context);
Yan, Zheng06852352012-11-19 10:49:07 +08001378 dout(" inode %p now dirty snapc %p auth cap %p\n",
1379 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
Sage Weil76e3b392009-10-15 18:13:53 -07001380 BUG_ON(!list_empty(&ci->i_dirty_item));
1381 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng06852352012-11-19 10:49:07 +08001382 if (ci->i_auth_cap)
1383 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
1384 else
1385 list_add(&ci->i_dirty_item,
1386 &mdsc->cap_dirty_migrating);
Sage Weil76e3b392009-10-15 18:13:53 -07001387 spin_unlock(&mdsc->cap_dirty_lock);
1388 if (ci->i_flushing_caps == 0) {
Sage Weil3772d262011-05-03 09:28:08 -07001389 ihold(inode);
Sage Weil76e3b392009-10-15 18:13:53 -07001390 dirty |= I_DIRTY_SYNC;
1391 }
1392 }
1393 BUG_ON(list_empty(&ci->i_dirty_item));
1394 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1395 (mask & CEPH_CAP_FILE_BUFFER))
1396 dirty |= I_DIRTY_DATASYNC;
Sage Weil76e3b392009-10-15 18:13:53 -07001397 __cap_delay_requeue(mdsc, ci);
Sage Weilfca65b42011-05-04 11:33:47 -07001398 return dirty;
Sage Weil76e3b392009-10-15 18:13:53 -07001399}
1400
1401/*
Sage Weila8599bd2009-10-06 11:31:12 -07001402 * Add dirty inode to the flushing list. Assigned a seq number so we
1403 * can wait for caps to flush without starving.
Sage Weilcdc35f92009-10-14 14:24:19 -07001404 *
Sage Weilbe655592011-11-30 09:47:09 -08001405 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001406 */
Sage Weilcdc35f92009-10-14 14:24:19 -07001407static int __mark_caps_flushing(struct inode *inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001408 struct ceph_mds_session *session)
1409{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001410 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001411 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001412 int flushing;
Sage Weil50b885b2009-12-01 14:12:07 -08001413
Sage Weilcdc35f92009-10-14 14:24:19 -07001414 BUG_ON(ci->i_dirty_caps == 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001415 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilcdc35f92009-10-14 14:24:19 -07001416
1417 flushing = ci->i_dirty_caps;
1418 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1419 ceph_cap_string(flushing),
1420 ceph_cap_string(ci->i_flushing_caps),
1421 ceph_cap_string(ci->i_flushing_caps | flushing));
1422 ci->i_flushing_caps |= flushing;
1423 ci->i_dirty_caps = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07001424 dout(" inode %p now !dirty\n", inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001425
Sage Weila8599bd2009-10-06 11:31:12 -07001426 spin_lock(&mdsc->cap_dirty_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07001427 list_del_init(&ci->i_dirty_item);
1428
1429 ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
Sage Weila8599bd2009-10-06 11:31:12 -07001430 if (list_empty(&ci->i_flushing_item)) {
1431 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1432 mdsc->num_cap_flushing++;
Sage Weilafcdaea2009-10-14 14:27:38 -07001433 dout(" inode %p now flushing seq %lld\n", inode,
1434 ci->i_cap_flush_seq);
1435 } else {
1436 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1437 dout(" inode %p now flushing (more) seq %lld\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001438 ci->i_cap_flush_seq);
1439 }
1440 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weilcdc35f92009-10-14 14:24:19 -07001441
1442 return flushing;
Sage Weila8599bd2009-10-06 11:31:12 -07001443}
1444
1445/*
Sage Weil5ecad6f2010-02-17 10:43:37 -08001446 * try to invalidate mapping pages without blocking.
1447 */
Sage Weil5ecad6f2010-02-17 10:43:37 -08001448static int try_nonblocking_invalidate(struct inode *inode)
1449{
1450 struct ceph_inode_info *ci = ceph_inode(inode);
1451 u32 invalidating_gen = ci->i_rdcache_gen;
1452
Sage Weilbe655592011-11-30 09:47:09 -08001453 spin_unlock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001454 invalidate_mapping_pages(&inode->i_data, 0, -1);
Sage Weilbe655592011-11-30 09:47:09 -08001455 spin_lock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001456
Sage Weil18a38192010-09-17 10:46:44 -07001457 if (inode->i_data.nrpages == 0 &&
Sage Weil5ecad6f2010-02-17 10:43:37 -08001458 invalidating_gen == ci->i_rdcache_gen) {
1459 /* success. */
1460 dout("try_nonblocking_invalidate %p success\n", inode);
Sage Weilcd045cb2010-11-04 11:05:05 -07001461 /* save any racing async invalidate some trouble */
1462 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
Sage Weil5ecad6f2010-02-17 10:43:37 -08001463 return 0;
1464 }
1465 dout("try_nonblocking_invalidate %p failed\n", inode);
1466 return -1;
1467}
1468
1469/*
Sage Weila8599bd2009-10-06 11:31:12 -07001470 * Swiss army knife function to examine currently used and wanted
1471 * versus held caps. Release, flush, ack revoked caps to mds as
1472 * appropriate.
1473 *
1474 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1475 * cap release further.
1476 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1477 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1478 * further delay.
1479 */
1480void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1481 struct ceph_mds_session *session)
1482{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001483 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1484 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001485 struct inode *inode = &ci->vfs_inode;
1486 struct ceph_cap *cap;
Yan, Zheng395c3122013-01-04 14:37:57 +08001487 int file_wanted, used, cap_used;
Sage Weila8599bd2009-10-06 11:31:12 -07001488 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
Sage Weilcbd03632010-02-09 13:41:18 -08001489 int issued, implemented, want, retain, revoking, flushing = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001490 int mds = -1; /* keep track of how far we've gone through i_caps list
1491 to avoid an infinite loop on retry */
1492 struct rb_node *p;
1493 int tried_invalidate = 0;
1494 int delayed = 0, sent = 0, force_requeue = 0, num;
Sage Weilcbd03632010-02-09 13:41:18 -08001495 int queue_invalidate = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001496 int is_delayed = flags & CHECK_CAPS_NODELAY;
1497
1498 /* if we are unmounting, flush any unused caps immediately. */
1499 if (mdsc->stopping)
1500 is_delayed = 1;
1501
Sage Weilbe655592011-11-30 09:47:09 -08001502 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001503
1504 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1505 flags |= CHECK_CAPS_FLUSH;
1506
1507 /* flush snaps first time around only */
1508 if (!list_empty(&ci->i_cap_snaps))
Sage Weile8351242010-09-17 08:03:08 -07001509 __ceph_flush_snaps(ci, &session, 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001510 goto retry_locked;
1511retry:
Sage Weilbe655592011-11-30 09:47:09 -08001512 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001513retry_locked:
1514 file_wanted = __ceph_caps_file_wanted(ci);
1515 used = __ceph_caps_used(ci);
1516 want = file_wanted | used;
Sage Weilcbd03632010-02-09 13:41:18 -08001517 issued = __ceph_caps_issued(ci, &implemented);
1518 revoking = implemented & ~issued;
Sage Weila8599bd2009-10-06 11:31:12 -07001519
1520 retain = want | CEPH_CAP_PIN;
1521 if (!mdsc->stopping && inode->i_nlink > 0) {
1522 if (want) {
1523 retain |= CEPH_CAP_ANY; /* be greedy */
1524 } else {
1525 retain |= CEPH_CAP_ANY_SHARED;
1526 /*
1527 * keep RD only if we didn't have the file open RW,
1528 * because then the mds would revoke it anyway to
1529 * journal max_size=0.
1530 */
1531 if (ci->i_max_size == 0)
1532 retain |= CEPH_CAP_ANY_RD;
1533 }
1534 }
1535
1536 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
Sage Weilcbd03632010-02-09 13:41:18 -08001537 " issued %s revoking %s retain %s %s%s%s\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001538 ceph_cap_string(file_wanted),
1539 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1540 ceph_cap_string(ci->i_flushing_caps),
Sage Weilcbd03632010-02-09 13:41:18 -08001541 ceph_cap_string(issued), ceph_cap_string(revoking),
Sage Weila8599bd2009-10-06 11:31:12 -07001542 ceph_cap_string(retain),
1543 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1544 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1545 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1546
1547 /*
1548 * If we no longer need to hold onto old our caps, and we may
1549 * have cached pages, but don't want them, then try to invalidate.
1550 * If we fail, it's because pages are locked.... try again later.
1551 */
1552 if ((!is_delayed || mdsc->stopping) &&
1553 ci->i_wrbuffer_ref == 0 && /* no dirty pages... */
Sage Weil93afd442010-09-17 08:38:25 -07001554 inode->i_data.nrpages && /* have cached pages */
Sage Weilcbd03632010-02-09 13:41:18 -08001555 (file_wanted == 0 || /* no open files */
Sage Weil29625072010-05-27 10:40:43 -07001556 (revoking & (CEPH_CAP_FILE_CACHE|
1557 CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */
Sage Weila8599bd2009-10-06 11:31:12 -07001558 !tried_invalidate) {
Sage Weila8599bd2009-10-06 11:31:12 -07001559 dout("check_caps trying to invalidate on %p\n", inode);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001560 if (try_nonblocking_invalidate(inode) < 0) {
Sage Weil29625072010-05-27 10:40:43 -07001561 if (revoking & (CEPH_CAP_FILE_CACHE|
1562 CEPH_CAP_FILE_LAZYIO)) {
Sage Weil5ecad6f2010-02-17 10:43:37 -08001563 dout("check_caps queuing invalidate\n");
1564 queue_invalidate = 1;
1565 ci->i_rdcache_revoking = ci->i_rdcache_gen;
1566 } else {
1567 dout("check_caps failed to invalidate pages\n");
1568 /* we failed to invalidate pages. check these
1569 caps again later. */
1570 force_requeue = 1;
1571 __cap_set_timeouts(mdsc, ci);
1572 }
Sage Weila8599bd2009-10-06 11:31:12 -07001573 }
1574 tried_invalidate = 1;
1575 goto retry_locked;
1576 }
1577
1578 num = 0;
1579 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1580 cap = rb_entry(p, struct ceph_cap, ci_node);
1581 num++;
1582
1583 /* avoid looping forever */
1584 if (mds >= cap->mds ||
1585 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1586 continue;
1587
1588 /* NOTE: no side-effects allowed, until we take s_mutex */
1589
Yan, Zheng395c3122013-01-04 14:37:57 +08001590 cap_used = used;
1591 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1592 cap_used &= ~ci->i_auth_cap->issued;
1593
Sage Weila8599bd2009-10-06 11:31:12 -07001594 revoking = cap->implemented & ~cap->issued;
Yan, Zheng395c3122013-01-04 14:37:57 +08001595 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
Sage Weil088b3f52011-01-18 08:56:01 -08001596 cap->mds, cap, ceph_cap_string(cap->issued),
Yan, Zheng395c3122013-01-04 14:37:57 +08001597 ceph_cap_string(cap_used),
Sage Weil088b3f52011-01-18 08:56:01 -08001598 ceph_cap_string(cap->implemented),
1599 ceph_cap_string(revoking));
Sage Weila8599bd2009-10-06 11:31:12 -07001600
1601 if (cap == ci->i_auth_cap &&
1602 (cap->issued & CEPH_CAP_FILE_WR)) {
1603 /* request larger max_size from MDS? */
1604 if (ci->i_wanted_max_size > ci->i_max_size &&
1605 ci->i_wanted_max_size > ci->i_requested_max_size) {
1606 dout("requesting new max_size\n");
1607 goto ack;
1608 }
1609
1610 /* approaching file_max? */
1611 if ((inode->i_size << 1) >= ci->i_max_size &&
1612 (ci->i_reported_size << 1) < ci->i_max_size) {
1613 dout("i_size approaching max_size\n");
1614 goto ack;
1615 }
1616 }
1617 /* flush anything dirty? */
1618 if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1619 ci->i_dirty_caps) {
1620 dout("flushing dirty caps\n");
1621 goto ack;
1622 }
1623
1624 /* completed revocation? going down and there are no caps? */
Yan, Zheng395c3122013-01-04 14:37:57 +08001625 if (revoking && (revoking & cap_used) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07001626 dout("completed revocation of %s\n",
1627 ceph_cap_string(cap->implemented & ~cap->issued));
1628 goto ack;
1629 }
1630
1631 /* want more caps from mds? */
1632 if (want & ~(cap->mds_wanted | cap->issued))
1633 goto ack;
1634
1635 /* things we might delay */
1636 if ((cap->issued & ~retain) == 0 &&
1637 cap->mds_wanted == want)
1638 continue; /* nope, all good */
1639
1640 if (is_delayed)
1641 goto ack;
1642
1643 /* delay? */
1644 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1645 time_before(jiffies, ci->i_hold_caps_max)) {
1646 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1647 ceph_cap_string(cap->issued),
1648 ceph_cap_string(cap->issued & retain),
1649 ceph_cap_string(cap->mds_wanted),
1650 ceph_cap_string(want));
1651 delayed++;
1652 continue;
1653 }
1654
1655ack:
Sage Weile9964c12010-03-01 15:16:56 -08001656 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1657 dout(" skipping %p I_NOFLUSH set\n", inode);
1658 continue;
1659 }
1660
Sage Weila8599bd2009-10-06 11:31:12 -07001661 if (session && session != cap->session) {
1662 dout("oops, wrong session %p mutex\n", session);
1663 mutex_unlock(&session->s_mutex);
1664 session = NULL;
1665 }
1666 if (!session) {
1667 session = cap->session;
1668 if (mutex_trylock(&session->s_mutex) == 0) {
1669 dout("inverting session/ino locks on %p\n",
1670 session);
Sage Weilbe655592011-11-30 09:47:09 -08001671 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001672 if (took_snap_rwsem) {
1673 up_read(&mdsc->snap_rwsem);
1674 took_snap_rwsem = 0;
1675 }
1676 mutex_lock(&session->s_mutex);
1677 goto retry;
1678 }
1679 }
1680 /* take snap_rwsem after session mutex */
1681 if (!took_snap_rwsem) {
1682 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1683 dout("inverting snap/in locks on %p\n",
1684 inode);
Sage Weilbe655592011-11-30 09:47:09 -08001685 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001686 down_read(&mdsc->snap_rwsem);
1687 took_snap_rwsem = 1;
1688 goto retry;
1689 }
1690 took_snap_rwsem = 1;
1691 }
1692
Sage Weilcdc35f92009-10-14 14:24:19 -07001693 if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1694 flushing = __mark_caps_flushing(inode, session);
Sage Weil24be0c42011-01-18 08:48:06 -08001695 else
1696 flushing = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001697
1698 mds = cap->mds; /* remember mds, so we don't repeat */
1699 sent++;
1700
Sage Weilbe655592011-11-30 09:47:09 -08001701 /* __send_cap drops i_ceph_lock */
Yan, Zheng395c3122013-01-04 14:37:57 +08001702 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used,
1703 want, retain, flushing, NULL);
Sage Weilbe655592011-11-30 09:47:09 -08001704 goto retry; /* retake i_ceph_lock and restart our cap scan. */
Sage Weila8599bd2009-10-06 11:31:12 -07001705 }
1706
1707 /*
1708 * Reschedule delayed caps release if we delayed anything,
1709 * otherwise cancel.
1710 */
1711 if (delayed && is_delayed)
1712 force_requeue = 1; /* __send_cap delayed release; requeue */
1713 if (!delayed && !is_delayed)
1714 __cap_delay_cancel(mdsc, ci);
1715 else if (!is_delayed || force_requeue)
1716 __cap_delay_requeue(mdsc, ci);
1717
Sage Weilbe655592011-11-30 09:47:09 -08001718 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001719
Sage Weilcbd03632010-02-09 13:41:18 -08001720 if (queue_invalidate)
Sage Weil3c6f6b72010-02-09 15:24:44 -08001721 ceph_queue_invalidate(inode);
Sage Weilcbd03632010-02-09 13:41:18 -08001722
Sage Weilcdc2ce02010-03-16 13:39:28 -07001723 if (session)
Sage Weila8599bd2009-10-06 11:31:12 -07001724 mutex_unlock(&session->s_mutex);
1725 if (took_snap_rwsem)
1726 up_read(&mdsc->snap_rwsem);
1727}
1728
1729/*
Sage Weila8599bd2009-10-06 11:31:12 -07001730 * Try to flush dirty caps back to the auth mds.
1731 */
1732static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1733 unsigned *flush_tid)
1734{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001735 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001736 struct ceph_inode_info *ci = ceph_inode(inode);
1737 int unlock_session = session ? 0 : 1;
1738 int flushing = 0;
1739
1740retry:
Sage Weilbe655592011-11-30 09:47:09 -08001741 spin_lock(&ci->i_ceph_lock);
Sage Weile9964c12010-03-01 15:16:56 -08001742 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1743 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1744 goto out;
1745 }
Sage Weila8599bd2009-10-06 11:31:12 -07001746 if (ci->i_dirty_caps && ci->i_auth_cap) {
1747 struct ceph_cap *cap = ci->i_auth_cap;
1748 int used = __ceph_caps_used(ci);
1749 int want = __ceph_caps_wanted(ci);
1750 int delayed;
1751
1752 if (!session) {
Sage Weilbe655592011-11-30 09:47:09 -08001753 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001754 session = cap->session;
1755 mutex_lock(&session->s_mutex);
1756 goto retry;
1757 }
1758 BUG_ON(session != cap->session);
1759 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1760 goto out;
1761
Sage Weilcdc35f92009-10-14 14:24:19 -07001762 flushing = __mark_caps_flushing(inode, session);
Sage Weila8599bd2009-10-06 11:31:12 -07001763
Sage Weilbe655592011-11-30 09:47:09 -08001764 /* __send_cap drops i_ceph_lock */
Sage Weila8599bd2009-10-06 11:31:12 -07001765 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1766 cap->issued | cap->implemented, flushing,
1767 flush_tid);
1768 if (!delayed)
1769 goto out_unlocked;
1770
Sage Weilbe655592011-11-30 09:47:09 -08001771 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001772 __cap_delay_requeue(mdsc, ci);
1773 }
1774out:
Sage Weilbe655592011-11-30 09:47:09 -08001775 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001776out_unlocked:
1777 if (session && unlock_session)
1778 mutex_unlock(&session->s_mutex);
1779 return flushing;
1780}
1781
1782/*
1783 * Return true if we've flushed caps through the given flush_tid.
1784 */
1785static int caps_are_flushed(struct inode *inode, unsigned tid)
1786{
1787 struct ceph_inode_info *ci = ceph_inode(inode);
Dan Carpentera5ee7512010-05-07 10:27:14 +02001788 int i, ret = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07001789
Sage Weilbe655592011-11-30 09:47:09 -08001790 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001791 for (i = 0; i < CEPH_CAP_BITS; i++)
1792 if ((ci->i_flushing_caps & (1 << i)) &&
1793 ci->i_cap_flush_tid[i] <= tid) {
1794 /* still flushing this bit */
1795 ret = 0;
1796 break;
1797 }
Sage Weilbe655592011-11-30 09:47:09 -08001798 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001799 return ret;
1800}
1801
1802/*
1803 * Wait on any unsafe replies for the given inode. First wait on the
1804 * newest request, and make that the upper bound. Then, if there are
1805 * more requests, keep waiting on the oldest as long as it is still older
1806 * than the original request.
1807 */
1808static void sync_write_wait(struct inode *inode)
1809{
1810 struct ceph_inode_info *ci = ceph_inode(inode);
1811 struct list_head *head = &ci->i_unsafe_writes;
1812 struct ceph_osd_request *req;
1813 u64 last_tid;
1814
1815 spin_lock(&ci->i_unsafe_lock);
1816 if (list_empty(head))
1817 goto out;
1818
1819 /* set upper bound as _last_ entry in chain */
1820 req = list_entry(head->prev, struct ceph_osd_request,
1821 r_unsafe_item);
1822 last_tid = req->r_tid;
1823
1824 do {
1825 ceph_osdc_get_request(req);
1826 spin_unlock(&ci->i_unsafe_lock);
1827 dout("sync_write_wait on tid %llu (until %llu)\n",
1828 req->r_tid, last_tid);
1829 wait_for_completion(&req->r_safe_completion);
1830 spin_lock(&ci->i_unsafe_lock);
1831 ceph_osdc_put_request(req);
1832
1833 /*
1834 * from here on look at first entry in chain, since we
1835 * only want to wait for anything older than last_tid
1836 */
1837 if (list_empty(head))
1838 break;
1839 req = list_entry(head->next, struct ceph_osd_request,
1840 r_unsafe_item);
1841 } while (req->r_tid < last_tid);
1842out:
1843 spin_unlock(&ci->i_unsafe_lock);
1844}
1845
Josef Bacik02c24a82011-07-16 20:44:56 -04001846int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Sage Weila8599bd2009-10-06 11:31:12 -07001847{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001848 struct inode *inode = file->f_mapping->host;
Sage Weila8599bd2009-10-06 11:31:12 -07001849 struct ceph_inode_info *ci = ceph_inode(inode);
1850 unsigned flush_tid;
1851 int ret;
1852 int dirty;
1853
1854 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1855 sync_write_wait(inode);
1856
Josef Bacik02c24a82011-07-16 20:44:56 -04001857 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
Sage Weila8599bd2009-10-06 11:31:12 -07001858 if (ret < 0)
1859 return ret;
Josef Bacik02c24a82011-07-16 20:44:56 -04001860 mutex_lock(&inode->i_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07001861
1862 dirty = try_flush_caps(inode, NULL, &flush_tid);
1863 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1864
1865 /*
1866 * only wait on non-file metadata writeback (the mds
1867 * can recover size and mtime, so we don't need to
1868 * wait for that)
1869 */
1870 if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1871 dout("fsync waiting for flush_tid %u\n", flush_tid);
1872 ret = wait_event_interruptible(ci->i_cap_wq,
1873 caps_are_flushed(inode, flush_tid));
1874 }
1875
1876 dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
Josef Bacik02c24a82011-07-16 20:44:56 -04001877 mutex_unlock(&inode->i_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07001878 return ret;
1879}
1880
1881/*
1882 * Flush any dirty caps back to the mds. If we aren't asked to wait,
1883 * queue inode for flush but don't do so immediately, because we can
1884 * get by with fewer MDS messages if we wait for data writeback to
1885 * complete first.
1886 */
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11001887int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
Sage Weila8599bd2009-10-06 11:31:12 -07001888{
1889 struct ceph_inode_info *ci = ceph_inode(inode);
1890 unsigned flush_tid;
1891 int err = 0;
1892 int dirty;
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11001893 int wait = wbc->sync_mode == WB_SYNC_ALL;
Sage Weila8599bd2009-10-06 11:31:12 -07001894
1895 dout("write_inode %p wait=%d\n", inode, wait);
1896 if (wait) {
1897 dirty = try_flush_caps(inode, NULL, &flush_tid);
1898 if (dirty)
1899 err = wait_event_interruptible(ci->i_cap_wq,
1900 caps_are_flushed(inode, flush_tid));
1901 } else {
Cheng Renquan640ef792010-03-26 17:40:33 +08001902 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001903 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001904
Sage Weilbe655592011-11-30 09:47:09 -08001905 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001906 if (__ceph_caps_dirty(ci))
1907 __cap_delay_requeue_front(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08001908 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001909 }
1910 return err;
1911}
1912
1913/*
1914 * After a recovering MDS goes active, we need to resend any caps
1915 * we were flushing.
1916 *
1917 * Caller holds session->s_mutex.
1918 */
1919static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1920 struct ceph_mds_session *session)
1921{
1922 struct ceph_cap_snap *capsnap;
1923
1924 dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1925 list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1926 flushing_item) {
1927 struct ceph_inode_info *ci = capsnap->ci;
1928 struct inode *inode = &ci->vfs_inode;
1929 struct ceph_cap *cap;
1930
Sage Weilbe655592011-11-30 09:47:09 -08001931 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001932 cap = ci->i_auth_cap;
1933 if (cap && cap->session == session) {
1934 dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1935 cap, capsnap);
Sage Weile8351242010-09-17 08:03:08 -07001936 __ceph_flush_snaps(ci, &session, 1);
Sage Weila8599bd2009-10-06 11:31:12 -07001937 } else {
1938 pr_err("%p auth cap %p not mds%d ???\n", inode,
1939 cap, session->s_mds);
Sage Weila8599bd2009-10-06 11:31:12 -07001940 }
Sage Weilbe655592011-11-30 09:47:09 -08001941 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001942 }
1943}
1944
1945void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1946 struct ceph_mds_session *session)
1947{
1948 struct ceph_inode_info *ci;
1949
1950 kick_flushing_capsnaps(mdsc, session);
1951
1952 dout("kick_flushing_caps mds%d\n", session->s_mds);
1953 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1954 struct inode *inode = &ci->vfs_inode;
1955 struct ceph_cap *cap;
1956 int delayed = 0;
1957
Sage Weilbe655592011-11-30 09:47:09 -08001958 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001959 cap = ci->i_auth_cap;
1960 if (cap && cap->session == session) {
1961 dout("kick_flushing_caps %p cap %p %s\n", inode,
1962 cap, ceph_cap_string(ci->i_flushing_caps));
1963 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1964 __ceph_caps_used(ci),
1965 __ceph_caps_wanted(ci),
1966 cap->issued | cap->implemented,
1967 ci->i_flushing_caps, NULL);
1968 if (delayed) {
Sage Weilbe655592011-11-30 09:47:09 -08001969 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001970 __cap_delay_requeue(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08001971 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001972 }
1973 } else {
1974 pr_err("%p auth cap %p not mds%d ???\n", inode,
1975 cap, session->s_mds);
Sage Weilbe655592011-11-30 09:47:09 -08001976 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001977 }
1978 }
1979}
1980
Sage Weil088b3f52011-01-18 08:56:01 -08001981static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
1982 struct ceph_mds_session *session,
1983 struct inode *inode)
1984{
1985 struct ceph_inode_info *ci = ceph_inode(inode);
1986 struct ceph_cap *cap;
1987 int delayed = 0;
1988
Sage Weilbe655592011-11-30 09:47:09 -08001989 spin_lock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08001990 cap = ci->i_auth_cap;
1991 dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode,
1992 ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq);
Yan, Zheng005c4692013-05-31 16:40:24 +08001993
Sage Weil088b3f52011-01-18 08:56:01 -08001994 __ceph_flush_snaps(ci, &session, 1);
Yan, Zheng005c4692013-05-31 16:40:24 +08001995
Sage Weil088b3f52011-01-18 08:56:01 -08001996 if (ci->i_flushing_caps) {
Yan, Zheng005c4692013-05-31 16:40:24 +08001997 spin_lock(&mdsc->cap_dirty_lock);
1998 list_move_tail(&ci->i_flushing_item,
1999 &cap->session->s_cap_flushing);
2000 spin_unlock(&mdsc->cap_dirty_lock);
2001
Sage Weil088b3f52011-01-18 08:56:01 -08002002 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2003 __ceph_caps_used(ci),
2004 __ceph_caps_wanted(ci),
2005 cap->issued | cap->implemented,
2006 ci->i_flushing_caps, NULL);
2007 if (delayed) {
Sage Weilbe655592011-11-30 09:47:09 -08002008 spin_lock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08002009 __cap_delay_requeue(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08002010 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08002011 }
2012 } else {
Sage Weilbe655592011-11-30 09:47:09 -08002013 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08002014 }
2015}
2016
Sage Weila8599bd2009-10-06 11:31:12 -07002017
2018/*
2019 * Take references to capabilities we hold, so that we don't release
2020 * them to the MDS prematurely.
2021 *
Sage Weilbe655592011-11-30 09:47:09 -08002022 * Protected by i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07002023 */
2024static void __take_cap_refs(struct ceph_inode_info *ci, int got)
2025{
2026 if (got & CEPH_CAP_PIN)
2027 ci->i_pin_ref++;
2028 if (got & CEPH_CAP_FILE_RD)
2029 ci->i_rd_ref++;
2030 if (got & CEPH_CAP_FILE_CACHE)
2031 ci->i_rdcache_ref++;
2032 if (got & CEPH_CAP_FILE_WR)
2033 ci->i_wr_ref++;
2034 if (got & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002035 if (ci->i_wb_ref == 0)
Sage Weil3772d262011-05-03 09:28:08 -07002036 ihold(&ci->vfs_inode);
Henry C Changd3d07202011-05-11 10:29:54 +00002037 ci->i_wb_ref++;
2038 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2039 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002040 }
2041}
2042
2043/*
2044 * Try to grab cap references. Specify those refs we @want, and the
2045 * minimal set we @need. Also include the larger offset we are writing
2046 * to (when applicable), and check against max_size here as well.
2047 * Note that caller is responsible for ensuring max_size increases are
2048 * requested from the MDS.
2049 */
2050static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2051 int *got, loff_t endoff, int *check_max, int *err)
2052{
2053 struct inode *inode = &ci->vfs_inode;
2054 int ret = 0;
2055 int have, implemented;
Sage Weil195d3ce2010-03-01 09:57:54 -08002056 int file_wanted;
Sage Weila8599bd2009-10-06 11:31:12 -07002057
2058 dout("get_cap_refs %p need %s want %s\n", inode,
2059 ceph_cap_string(need), ceph_cap_string(want));
Sage Weilbe655592011-11-30 09:47:09 -08002060 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002061
Sage Weil195d3ce2010-03-01 09:57:54 -08002062 /* make sure file is actually open */
2063 file_wanted = __ceph_caps_file_wanted(ci);
2064 if ((file_wanted & need) == 0) {
2065 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2066 ceph_cap_string(need), ceph_cap_string(file_wanted));
Sage Weila8599bd2009-10-06 11:31:12 -07002067 *err = -EBADF;
2068 ret = 1;
2069 goto out;
2070 }
2071
Yan, Zheng37505d52013-04-12 16:11:10 +08002072 /* finish pending truncate */
2073 while (ci->i_truncate_pending) {
2074 spin_unlock(&ci->i_ceph_lock);
Yan, Zhengb415bf42013-07-02 12:40:19 +08002075 __ceph_do_pending_vmtruncate(inode);
Yan, Zheng37505d52013-04-12 16:11:10 +08002076 spin_lock(&ci->i_ceph_lock);
2077 }
2078
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002079 have = __ceph_caps_issued(ci, &implemented);
2080
2081 if (have & need & CEPH_CAP_FILE_WR) {
Sage Weila8599bd2009-10-06 11:31:12 -07002082 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2083 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2084 inode, endoff, ci->i_max_size);
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002085 if (endoff > ci->i_requested_max_size) {
Sage Weila8599bd2009-10-06 11:31:12 -07002086 *check_max = 1;
2087 ret = 1;
2088 }
2089 goto out;
2090 }
2091 /*
2092 * If a sync write is in progress, we must wait, so that we
2093 * can get a final snapshot value for size+mtime.
2094 */
2095 if (__ceph_have_pending_cap_snap(ci)) {
2096 dout("get_cap_refs %p cap_snap_pending\n", inode);
2097 goto out;
2098 }
2099 }
Sage Weila8599bd2009-10-06 11:31:12 -07002100
Sage Weila8599bd2009-10-06 11:31:12 -07002101 if ((have & need) == need) {
2102 /*
2103 * Look at (implemented & ~have & not) so that we keep waiting
2104 * on transition from wanted -> needed caps. This is needed
2105 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2106 * going before a prior buffered writeback happens.
2107 */
2108 int not = want & ~(have & need);
2109 int revoking = implemented & ~have;
2110 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2111 inode, ceph_cap_string(have), ceph_cap_string(not),
2112 ceph_cap_string(revoking));
2113 if ((revoking & not) == 0) {
2114 *got = need | (have & want);
2115 __take_cap_refs(ci, *got);
2116 ret = 1;
2117 }
2118 } else {
2119 dout("get_cap_refs %p have %s needed %s\n", inode,
2120 ceph_cap_string(have), ceph_cap_string(need));
2121 }
2122out:
Sage Weilbe655592011-11-30 09:47:09 -08002123 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002124 dout("get_cap_refs %p ret %d got %s\n", inode,
2125 ret, ceph_cap_string(*got));
2126 return ret;
2127}
2128
2129/*
2130 * Check the offset we are writing up to against our current
2131 * max_size. If necessary, tell the MDS we want to write to
2132 * a larger offset.
2133 */
2134static void check_max_size(struct inode *inode, loff_t endoff)
2135{
2136 struct ceph_inode_info *ci = ceph_inode(inode);
2137 int check = 0;
2138
2139 /* do we need to explicitly request a larger max_size? */
Sage Weilbe655592011-11-30 09:47:09 -08002140 spin_lock(&ci->i_ceph_lock);
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002141 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
Sage Weila8599bd2009-10-06 11:31:12 -07002142 dout("write %p at large endoff %llu, req max_size\n",
2143 inode, endoff);
2144 ci->i_wanted_max_size = endoff;
Sage Weila8599bd2009-10-06 11:31:12 -07002145 }
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002146 /* duplicate ceph_check_caps()'s logic */
2147 if (ci->i_auth_cap &&
2148 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2149 ci->i_wanted_max_size > ci->i_max_size &&
2150 ci->i_wanted_max_size > ci->i_requested_max_size)
2151 check = 1;
Sage Weilbe655592011-11-30 09:47:09 -08002152 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002153 if (check)
2154 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2155}
2156
2157/*
2158 * Wait for caps, and take cap references. If we can't get a WR cap
2159 * due to a small max_size, make sure we check_max_size (and possibly
2160 * ask the mds) so we don't get hung up indefinitely.
2161 */
2162int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2163 loff_t endoff)
2164{
2165 int check_max, ret, err;
2166
2167retry:
2168 if (endoff > 0)
2169 check_max_size(&ci->vfs_inode, endoff);
2170 check_max = 0;
2171 err = 0;
2172 ret = wait_event_interruptible(ci->i_cap_wq,
2173 try_get_cap_refs(ci, need, want,
2174 got, endoff,
2175 &check_max, &err));
2176 if (err)
2177 ret = err;
2178 if (check_max)
2179 goto retry;
2180 return ret;
2181}
2182
2183/*
2184 * Take cap refs. Caller must already know we hold at least one ref
2185 * on the caps in question or we don't know this is safe.
2186 */
2187void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2188{
Sage Weilbe655592011-11-30 09:47:09 -08002189 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002190 __take_cap_refs(ci, caps);
Sage Weilbe655592011-11-30 09:47:09 -08002191 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002192}
2193
2194/*
2195 * Release cap refs.
2196 *
2197 * If we released the last ref on any given cap, call ceph_check_caps
2198 * to release (or schedule a release).
2199 *
2200 * If we are releasing a WR cap (from a sync write), finalize any affected
2201 * cap_snap, and wake up any waiters.
2202 */
2203void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2204{
2205 struct inode *inode = &ci->vfs_inode;
2206 int last = 0, put = 0, flushsnaps = 0, wake = 0;
2207 struct ceph_cap_snap *capsnap;
2208
Sage Weilbe655592011-11-30 09:47:09 -08002209 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002210 if (had & CEPH_CAP_PIN)
2211 --ci->i_pin_ref;
2212 if (had & CEPH_CAP_FILE_RD)
2213 if (--ci->i_rd_ref == 0)
2214 last++;
2215 if (had & CEPH_CAP_FILE_CACHE)
2216 if (--ci->i_rdcache_ref == 0)
2217 last++;
2218 if (had & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002219 if (--ci->i_wb_ref == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07002220 last++;
2221 put++;
2222 }
Henry C Changd3d07202011-05-11 10:29:54 +00002223 dout("put_cap_refs %p wb %d -> %d (?)\n",
2224 inode, ci->i_wb_ref+1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002225 }
2226 if (had & CEPH_CAP_FILE_WR)
2227 if (--ci->i_wr_ref == 0) {
2228 last++;
2229 if (!list_empty(&ci->i_cap_snaps)) {
2230 capsnap = list_first_entry(&ci->i_cap_snaps,
2231 struct ceph_cap_snap,
2232 ci_item);
2233 if (capsnap->writing) {
2234 capsnap->writing = 0;
2235 flushsnaps =
2236 __ceph_finish_cap_snap(ci,
2237 capsnap);
2238 wake = 1;
2239 }
2240 }
2241 }
Sage Weilbe655592011-11-30 09:47:09 -08002242 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002243
Sage Weil819ccbf2010-04-01 09:33:46 -07002244 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2245 last ? " last" : "", put ? " put" : "");
Sage Weila8599bd2009-10-06 11:31:12 -07002246
2247 if (last && !flushsnaps)
2248 ceph_check_caps(ci, 0, NULL);
2249 else if (flushsnaps)
2250 ceph_flush_snaps(ci);
2251 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002252 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002253 if (put)
2254 iput(inode);
2255}
2256
2257/*
2258 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2259 * context. Adjust per-snap dirty page accounting as appropriate.
2260 * Once all dirty data for a cap_snap is flushed, flush snapped file
2261 * metadata back to the MDS. If we dropped the last ref, call
2262 * ceph_check_caps.
2263 */
2264void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2265 struct ceph_snap_context *snapc)
2266{
2267 struct inode *inode = &ci->vfs_inode;
2268 int last = 0;
Sage Weil819ccbf2010-04-01 09:33:46 -07002269 int complete_capsnap = 0;
2270 int drop_capsnap = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002271 int found = 0;
2272 struct ceph_cap_snap *capsnap = NULL;
2273
Sage Weilbe655592011-11-30 09:47:09 -08002274 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002275 ci->i_wrbuffer_ref -= nr;
2276 last = !ci->i_wrbuffer_ref;
2277
2278 if (ci->i_head_snapc == snapc) {
2279 ci->i_wrbuffer_ref_head -= nr;
Sage Weil7d8cb262010-08-24 08:44:16 -07002280 if (ci->i_wrbuffer_ref_head == 0 &&
2281 ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
2282 BUG_ON(!ci->i_head_snapc);
Sage Weila8599bd2009-10-06 11:31:12 -07002283 ceph_put_snap_context(ci->i_head_snapc);
2284 ci->i_head_snapc = NULL;
2285 }
2286 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2287 inode,
2288 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2289 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2290 last ? " LAST" : "");
2291 } else {
2292 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2293 if (capsnap->context == snapc) {
2294 found = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002295 break;
2296 }
2297 }
2298 BUG_ON(!found);
Sage Weil819ccbf2010-04-01 09:33:46 -07002299 capsnap->dirty_pages -= nr;
2300 if (capsnap->dirty_pages == 0) {
2301 complete_capsnap = 1;
2302 if (capsnap->dirty == 0)
2303 /* cap writeback completed before we created
2304 * the cap_snap; no FLUSHSNAP is needed */
2305 drop_capsnap = 1;
2306 }
Sage Weila8599bd2009-10-06 11:31:12 -07002307 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
Sage Weil819ccbf2010-04-01 09:33:46 -07002308 " snap %lld %d/%d -> %d/%d %s%s%s\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002309 inode, capsnap, capsnap->context->seq,
2310 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2311 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2312 last ? " (wrbuffer last)" : "",
Sage Weil819ccbf2010-04-01 09:33:46 -07002313 complete_capsnap ? " (complete capsnap)" : "",
2314 drop_capsnap ? " (drop capsnap)" : "");
2315 if (drop_capsnap) {
2316 ceph_put_snap_context(capsnap->context);
2317 list_del(&capsnap->ci_item);
2318 list_del(&capsnap->flushing_item);
2319 ceph_put_cap_snap(capsnap);
2320 }
Sage Weila8599bd2009-10-06 11:31:12 -07002321 }
2322
Sage Weilbe655592011-11-30 09:47:09 -08002323 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002324
2325 if (last) {
2326 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2327 iput(inode);
Sage Weil819ccbf2010-04-01 09:33:46 -07002328 } else if (complete_capsnap) {
Sage Weila8599bd2009-10-06 11:31:12 -07002329 ceph_flush_snaps(ci);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002330 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002331 }
Sage Weil819ccbf2010-04-01 09:33:46 -07002332 if (drop_capsnap)
2333 iput(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002334}
2335
2336/*
Yan, Zhengca20c992013-07-21 10:07:51 +08002337 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2338 */
2339static void invalidate_aliases(struct inode *inode)
2340{
2341 struct dentry *dn, *prev = NULL;
2342
2343 dout("invalidate_aliases inode %p\n", inode);
2344 d_prune_aliases(inode);
2345 /*
2346 * For non-directory inode, d_find_alias() only returns
2347 * connected dentry. After calling d_delete(), the dentry
2348 * become disconnected.
2349 *
2350 * For directory inode, d_find_alias() only can return
2351 * disconnected dentry. But directory inode should have
2352 * one alias at most.
2353 */
2354 while ((dn = d_find_alias(inode))) {
2355 if (dn == prev) {
2356 dput(dn);
2357 break;
2358 }
2359 d_delete(dn);
2360 if (prev)
2361 dput(prev);
2362 prev = dn;
2363 }
2364 if (prev)
2365 dput(prev);
2366}
2367
2368/*
Sage Weila8599bd2009-10-06 11:31:12 -07002369 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2370 * actually be a revocation if it specifies a smaller cap set.)
2371 *
Sage Weilbe655592011-11-30 09:47:09 -08002372 * caller holds s_mutex and i_ceph_lock, we drop both.
Sage Weil15637c82010-03-16 13:42:00 -07002373 *
Sage Weila8599bd2009-10-06 11:31:12 -07002374 * return value:
2375 * 0 - ok
2376 * 1 - check_caps on auth cap only (writeback)
2377 * 2 - check_caps (ack revoke)
2378 */
Sage Weil15637c82010-03-16 13:42:00 -07002379static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2380 struct ceph_mds_session *session,
2381 struct ceph_cap *cap,
2382 struct ceph_buffer *xattr_buf)
Sage Weilbe655592011-11-30 09:47:09 -08002383 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07002384{
2385 struct ceph_inode_info *ci = ceph_inode(inode);
2386 int mds = session->s_mds;
Sage Weil2f56f562010-10-27 20:59:49 -07002387 int seq = le32_to_cpu(grant->seq);
Sage Weila8599bd2009-10-06 11:31:12 -07002388 int newcaps = le32_to_cpu(grant->caps);
2389 int issued, implemented, used, wanted, dirty;
2390 u64 size = le64_to_cpu(grant->size);
2391 u64 max_size = le64_to_cpu(grant->max_size);
2392 struct timespec mtime, atime, ctime;
Sage Weil15637c82010-03-16 13:42:00 -07002393 int check_caps = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002394 int wake = 0;
2395 int writeback = 0;
Sage Weil3c6f6b72010-02-09 15:24:44 -08002396 int queue_invalidate = 0;
Yan, Zhengca20c992013-07-21 10:07:51 +08002397 int deleted_inode = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002398
Sage Weil2f56f562010-10-27 20:59:49 -07002399 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2400 inode, cap, mds, seq, ceph_cap_string(newcaps));
Sage Weila8599bd2009-10-06 11:31:12 -07002401 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2402 inode->i_size);
2403
2404 /*
2405 * If CACHE is being revoked, and we have no dirty buffers,
2406 * try to invalidate (once). (If there are dirty buffers, we
2407 * will invalidate _after_ writeback.)
2408 */
Sage Weil3b454c42010-06-10 13:20:33 -07002409 if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
2410 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
Yehuda Sadehbcd2cbd2010-02-19 00:12:21 +00002411 !ci->i_wrbuffer_ref) {
Li Wange9075742013-08-15 22:00:25 -07002412 if (try_nonblocking_invalidate(inode)) {
Sage Weila8599bd2009-10-06 11:31:12 -07002413 /* there were locked pages.. invalidate later
2414 in a separate thread. */
2415 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Sage Weil3c6f6b72010-02-09 15:24:44 -08002416 queue_invalidate = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002417 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2418 }
Sage Weila8599bd2009-10-06 11:31:12 -07002419 }
Sage Weila8599bd2009-10-06 11:31:12 -07002420 }
2421
2422 /* side effects now are allowed */
2423
2424 issued = __ceph_caps_issued(ci, &implemented);
2425 issued |= implemented | __ceph_caps_dirty(ci);
2426
Sage Weil685f9a5d2009-11-09 12:05:48 -08002427 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -07002428
2429 __check_cap_issue(ci, cap, newcaps);
2430
2431 if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2432 inode->i_mode = le32_to_cpu(grant->mode);
Eric W. Biederman05cb11c2013-01-31 02:56:19 -08002433 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
2434 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
Sage Weila8599bd2009-10-06 11:31:12 -07002435 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08002436 from_kuid(&init_user_ns, inode->i_uid),
2437 from_kgid(&init_user_ns, inode->i_gid));
Sage Weila8599bd2009-10-06 11:31:12 -07002438 }
2439
Yan, Zhengca20c992013-07-21 10:07:51 +08002440 if ((issued & CEPH_CAP_LINK_EXCL) == 0) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02002441 set_nlink(inode, le32_to_cpu(grant->nlink));
Yan, Zhengca20c992013-07-21 10:07:51 +08002442 if (inode->i_nlink == 0 &&
2443 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
2444 deleted_inode = 1;
2445 }
Sage Weila8599bd2009-10-06 11:31:12 -07002446
2447 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2448 int len = le32_to_cpu(grant->xattr_len);
2449 u64 version = le64_to_cpu(grant->xattr_version);
2450
2451 if (version > ci->i_xattrs.version) {
2452 dout(" got new xattrs v%llu on %p len %d\n",
2453 version, inode, len);
2454 if (ci->i_xattrs.blob)
2455 ceph_buffer_put(ci->i_xattrs.blob);
2456 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2457 ci->i_xattrs.version = version;
2458 }
2459 }
2460
2461 /* size/ctime/mtime/atime? */
2462 ceph_fill_file_size(inode, issued,
2463 le32_to_cpu(grant->truncate_seq),
2464 le64_to_cpu(grant->truncate_size), size);
2465 ceph_decode_timespec(&mtime, &grant->mtime);
2466 ceph_decode_timespec(&atime, &grant->atime);
2467 ceph_decode_timespec(&ctime, &grant->ctime);
2468 ceph_fill_file_time(inode, issued,
2469 le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2470 &atime);
2471
2472 /* max size increase? */
Yan, Zheng5e62ad32012-11-19 10:49:04 +08002473 if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
Sage Weila8599bd2009-10-06 11:31:12 -07002474 dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2475 ci->i_max_size = max_size;
2476 if (max_size >= ci->i_wanted_max_size) {
2477 ci->i_wanted_max_size = 0; /* reset */
2478 ci->i_requested_max_size = 0;
2479 }
2480 wake = 1;
2481 }
2482
2483 /* check cap bits */
2484 wanted = __ceph_caps_wanted(ci);
2485 used = __ceph_caps_used(ci);
2486 dirty = __ceph_caps_dirty(ci);
2487 dout(" my wanted = %s, used = %s, dirty %s\n",
2488 ceph_cap_string(wanted),
2489 ceph_cap_string(used),
2490 ceph_cap_string(dirty));
2491 if (wanted != le32_to_cpu(grant->wanted)) {
2492 dout("mds wanted %s -> %s\n",
2493 ceph_cap_string(le32_to_cpu(grant->wanted)),
2494 ceph_cap_string(wanted));
Yan, Zheng390306c2013-01-04 15:30:10 +08002495 /* imported cap may not have correct mds_wanted */
2496 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
2497 check_caps = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002498 }
2499
2500 cap->seq = seq;
2501
2502 /* file layout may have changed */
2503 ci->i_layout = grant->layout;
2504
2505 /* revocation, grant, or no-op? */
2506 if (cap->issued & ~newcaps) {
Sage Weil3b454c42010-06-10 13:20:33 -07002507 int revoking = cap->issued & ~newcaps;
2508
2509 dout("revocation: %s -> %s (revoking %s)\n",
2510 ceph_cap_string(cap->issued),
2511 ceph_cap_string(newcaps),
2512 ceph_cap_string(revoking));
Sage Weil0eb6cd42010-08-05 13:53:18 -07002513 if (revoking & used & CEPH_CAP_FILE_BUFFER)
Sage Weil3b454c42010-06-10 13:20:33 -07002514 writeback = 1; /* initiate writeback; will delay ack */
2515 else if (revoking == CEPH_CAP_FILE_CACHE &&
2516 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2517 queue_invalidate)
2518 ; /* do nothing yet, invalidation will be queued */
2519 else if (cap == ci->i_auth_cap)
2520 check_caps = 1; /* check auth cap only */
2521 else
2522 check_caps = 2; /* check all caps */
Sage Weila8599bd2009-10-06 11:31:12 -07002523 cap->issued = newcaps;
Sage Weil978097c2010-03-08 15:27:53 -08002524 cap->implemented |= newcaps;
Sage Weila8599bd2009-10-06 11:31:12 -07002525 } else if (cap->issued == newcaps) {
2526 dout("caps unchanged: %s -> %s\n",
2527 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2528 } else {
2529 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2530 ceph_cap_string(newcaps));
Yan, Zheng6ee6b9532013-07-02 12:40:21 +08002531 /* non-auth MDS is revoking the newly grant caps ? */
2532 if (cap == ci->i_auth_cap &&
2533 __ceph_caps_revoking_other(ci, cap, newcaps))
2534 check_caps = 2;
2535
Sage Weila8599bd2009-10-06 11:31:12 -07002536 cap->issued = newcaps;
2537 cap->implemented |= newcaps; /* add bits only, to
2538 * avoid stepping on a
2539 * pending revocation */
2540 wake = 1;
2541 }
Sage Weil978097c2010-03-08 15:27:53 -08002542 BUG_ON(cap->issued & ~cap->implemented);
Sage Weila8599bd2009-10-06 11:31:12 -07002543
Sage Weilbe655592011-11-30 09:47:09 -08002544 spin_unlock(&ci->i_ceph_lock);
Sage Weil3c6f6b72010-02-09 15:24:44 -08002545 if (writeback)
Sage Weila8599bd2009-10-06 11:31:12 -07002546 /*
2547 * queue inode for writeback: we can't actually call
2548 * filemap_write_and_wait, etc. from message handler
2549 * context.
2550 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08002551 ceph_queue_writeback(inode);
2552 if (queue_invalidate)
2553 ceph_queue_invalidate(inode);
Yan, Zhengca20c992013-07-21 10:07:51 +08002554 if (deleted_inode)
2555 invalidate_aliases(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002556 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002557 wake_up_all(&ci->i_cap_wq);
Sage Weil15637c82010-03-16 13:42:00 -07002558
2559 if (check_caps == 1)
2560 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
2561 session);
2562 else if (check_caps == 2)
2563 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
2564 else
2565 mutex_unlock(&session->s_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07002566}
2567
2568/*
2569 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2570 * MDS has been safely committed.
2571 */
Sage Weil6df058c2009-12-22 11:24:33 -08002572static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07002573 struct ceph_mds_caps *m,
2574 struct ceph_mds_session *session,
2575 struct ceph_cap *cap)
Sage Weilbe655592011-11-30 09:47:09 -08002576 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07002577{
2578 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002579 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002580 unsigned seq = le32_to_cpu(m->seq);
2581 int dirty = le32_to_cpu(m->dirty);
2582 int cleaned = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07002583 int drop = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002584 int i;
2585
2586 for (i = 0; i < CEPH_CAP_BITS; i++)
2587 if ((dirty & (1 << i)) &&
2588 flush_tid == ci->i_cap_flush_tid[i])
2589 cleaned |= 1 << i;
2590
2591 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2592 " flushing %s -> %s\n",
2593 inode, session->s_mds, seq, ceph_cap_string(dirty),
2594 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2595 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2596
2597 if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2598 goto out;
2599
Sage Weila8599bd2009-10-06 11:31:12 -07002600 ci->i_flushing_caps &= ~cleaned;
Sage Weila8599bd2009-10-06 11:31:12 -07002601
2602 spin_lock(&mdsc->cap_dirty_lock);
2603 if (ci->i_flushing_caps == 0) {
2604 list_del_init(&ci->i_flushing_item);
2605 if (!list_empty(&session->s_cap_flushing))
2606 dout(" mds%d still flushing cap on %p\n",
2607 session->s_mds,
2608 &list_entry(session->s_cap_flushing.next,
2609 struct ceph_inode_info,
2610 i_flushing_item)->vfs_inode);
2611 mdsc->num_cap_flushing--;
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002612 wake_up_all(&mdsc->cap_flushing_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002613 dout(" inode %p now !flushing\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07002614
2615 if (ci->i_dirty_caps == 0) {
2616 dout(" inode %p now clean\n", inode);
2617 BUG_ON(!list_empty(&ci->i_dirty_item));
2618 drop = 1;
Sage Weil7d8cb262010-08-24 08:44:16 -07002619 if (ci->i_wrbuffer_ref_head == 0) {
2620 BUG_ON(!ci->i_head_snapc);
2621 ceph_put_snap_context(ci->i_head_snapc);
2622 ci->i_head_snapc = NULL;
2623 }
Sage Weil76e3b392009-10-15 18:13:53 -07002624 } else {
2625 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilafcdaea2009-10-14 14:27:38 -07002626 }
Sage Weila8599bd2009-10-06 11:31:12 -07002627 }
2628 spin_unlock(&mdsc->cap_dirty_lock);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002629 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07002630
2631out:
Sage Weilbe655592011-11-30 09:47:09 -08002632 spin_unlock(&ci->i_ceph_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07002633 if (drop)
Sage Weila8599bd2009-10-06 11:31:12 -07002634 iput(inode);
2635}
2636
2637/*
2638 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
2639 * throw away our cap_snap.
2640 *
2641 * Caller hold s_mutex.
2642 */
Sage Weil6df058c2009-12-22 11:24:33 -08002643static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07002644 struct ceph_mds_caps *m,
2645 struct ceph_mds_session *session)
2646{
2647 struct ceph_inode_info *ci = ceph_inode(inode);
2648 u64 follows = le64_to_cpu(m->snap_follows);
Sage Weila8599bd2009-10-06 11:31:12 -07002649 struct ceph_cap_snap *capsnap;
2650 int drop = 0;
2651
2652 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2653 inode, ci, session->s_mds, follows);
2654
Sage Weilbe655592011-11-30 09:47:09 -08002655 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002656 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2657 if (capsnap->follows == follows) {
2658 if (capsnap->flush_tid != flush_tid) {
2659 dout(" cap_snap %p follows %lld tid %lld !="
2660 " %lld\n", capsnap, follows,
2661 flush_tid, capsnap->flush_tid);
2662 break;
2663 }
2664 WARN_ON(capsnap->dirty_pages || capsnap->writing);
Sage Weil819ccbf2010-04-01 09:33:46 -07002665 dout(" removing %p cap_snap %p follows %lld\n",
2666 inode, capsnap, follows);
Sage Weila8599bd2009-10-06 11:31:12 -07002667 ceph_put_snap_context(capsnap->context);
2668 list_del(&capsnap->ci_item);
2669 list_del(&capsnap->flushing_item);
2670 ceph_put_cap_snap(capsnap);
2671 drop = 1;
2672 break;
2673 } else {
2674 dout(" skipping cap_snap %p follows %lld\n",
2675 capsnap, capsnap->follows);
2676 }
2677 }
Sage Weilbe655592011-11-30 09:47:09 -08002678 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002679 if (drop)
2680 iput(inode);
2681}
2682
2683/*
2684 * Handle TRUNC from MDS, indicating file truncation.
2685 *
2686 * caller hold s_mutex.
2687 */
2688static void handle_cap_trunc(struct inode *inode,
2689 struct ceph_mds_caps *trunc,
2690 struct ceph_mds_session *session)
Sage Weilbe655592011-11-30 09:47:09 -08002691 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07002692{
2693 struct ceph_inode_info *ci = ceph_inode(inode);
2694 int mds = session->s_mds;
2695 int seq = le32_to_cpu(trunc->seq);
2696 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2697 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2698 u64 size = le64_to_cpu(trunc->size);
2699 int implemented = 0;
2700 int dirty = __ceph_caps_dirty(ci);
2701 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2702 int queue_trunc = 0;
2703
2704 issued |= implemented | dirty;
2705
2706 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2707 inode, mds, seq, truncate_size, truncate_seq);
2708 queue_trunc = ceph_fill_file_size(inode, issued,
2709 truncate_seq, truncate_size, size);
Sage Weilbe655592011-11-30 09:47:09 -08002710 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002711
2712 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -08002713 ceph_queue_vmtruncate(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002714}
2715
2716/*
2717 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
2718 * different one. If we are the most recent migration we've seen (as
2719 * indicated by mseq), make note of the migrating cap bits for the
2720 * duration (until we see the corresponding IMPORT).
2721 *
2722 * caller holds s_mutex
2723 */
2724static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
Sage Weil154f42c2010-06-21 13:45:04 -07002725 struct ceph_mds_session *session,
2726 int *open_target_sessions)
Sage Weila8599bd2009-10-06 11:31:12 -07002727{
Sage Weildb354052011-05-24 11:46:31 -07002728 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002729 struct ceph_inode_info *ci = ceph_inode(inode);
2730 int mds = session->s_mds;
2731 unsigned mseq = le32_to_cpu(ex->migrate_seq);
2732 struct ceph_cap *cap = NULL, *t;
2733 struct rb_node *p;
2734 int remember = 1;
2735
2736 dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2737 inode, ci, mds, mseq);
2738
Sage Weilbe655592011-11-30 09:47:09 -08002739 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002740
2741 /* make sure we haven't seen a higher mseq */
2742 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2743 t = rb_entry(p, struct ceph_cap, ci_node);
2744 if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2745 dout(" higher mseq on cap from mds%d\n",
2746 t->session->s_mds);
2747 remember = 0;
2748 }
2749 if (t->session->s_mds == mds)
2750 cap = t;
2751 }
2752
2753 if (cap) {
2754 if (remember) {
2755 /* make note */
2756 ci->i_cap_exporting_mds = mds;
2757 ci->i_cap_exporting_mseq = mseq;
2758 ci->i_cap_exporting_issued = cap->issued;
Sage Weil154f42c2010-06-21 13:45:04 -07002759
2760 /*
2761 * make sure we have open sessions with all possible
2762 * export targets, so that we get the matching IMPORT
2763 */
2764 *open_target_sessions = 1;
Sage Weildb354052011-05-24 11:46:31 -07002765
2766 /*
2767 * we can't flush dirty caps that we've seen the
2768 * EXPORT but no IMPORT for
2769 */
2770 spin_lock(&mdsc->cap_dirty_lock);
2771 if (!list_empty(&ci->i_dirty_item)) {
2772 dout(" moving %p to cap_dirty_migrating\n",
2773 inode);
2774 list_move(&ci->i_dirty_item,
2775 &mdsc->cap_dirty_migrating);
2776 }
2777 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002778 }
Sage Weil7c1332b2010-02-16 11:39:45 -08002779 __ceph_remove_cap(cap);
Sage Weila8599bd2009-10-06 11:31:12 -07002780 }
Sage Weil4ea00432010-03-16 10:36:40 -07002781 /* else, we already released it */
Sage Weila8599bd2009-10-06 11:31:12 -07002782
Sage Weilbe655592011-11-30 09:47:09 -08002783 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002784}
2785
2786/*
2787 * Handle cap IMPORT. If there are temp bits from an older EXPORT,
2788 * clean them up.
2789 *
2790 * caller holds s_mutex.
2791 */
2792static void handle_cap_import(struct ceph_mds_client *mdsc,
2793 struct inode *inode, struct ceph_mds_caps *im,
2794 struct ceph_mds_session *session,
2795 void *snaptrace, int snaptrace_len)
2796{
2797 struct ceph_inode_info *ci = ceph_inode(inode);
2798 int mds = session->s_mds;
2799 unsigned issued = le32_to_cpu(im->caps);
2800 unsigned wanted = le32_to_cpu(im->wanted);
2801 unsigned seq = le32_to_cpu(im->seq);
2802 unsigned mseq = le32_to_cpu(im->migrate_seq);
2803 u64 realmino = le64_to_cpu(im->realm);
2804 u64 cap_id = le64_to_cpu(im->cap_id);
2805
2806 if (ci->i_cap_exporting_mds >= 0 &&
2807 ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2808 dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2809 " - cleared exporting from mds%d\n",
2810 inode, ci, mds, mseq,
2811 ci->i_cap_exporting_mds);
2812 ci->i_cap_exporting_issued = 0;
2813 ci->i_cap_exporting_mseq = 0;
2814 ci->i_cap_exporting_mds = -1;
Sage Weildb354052011-05-24 11:46:31 -07002815
2816 spin_lock(&mdsc->cap_dirty_lock);
2817 if (!list_empty(&ci->i_dirty_item)) {
2818 dout(" moving %p back to cap_dirty\n", inode);
2819 list_move(&ci->i_dirty_item, &mdsc->cap_dirty);
2820 }
2821 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002822 } else {
2823 dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2824 inode, ci, mds, mseq);
2825 }
2826
2827 down_write(&mdsc->snap_rwsem);
2828 ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2829 false);
2830 downgrade_write(&mdsc->snap_rwsem);
2831 ceph_add_cap(inode, session, cap_id, -1,
2832 issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2833 NULL /* no caps context */);
Sage Weil088b3f52011-01-18 08:56:01 -08002834 kick_flushing_inode_caps(mdsc, session, inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002835 up_read(&mdsc->snap_rwsem);
Sage Weilfeb4cc92010-11-07 09:39:00 -08002836
2837 /* make sure we re-request max_size, if necessary */
Sage Weilbe655592011-11-30 09:47:09 -08002838 spin_lock(&ci->i_ceph_lock);
Yan, Zheng0e5e1772012-11-19 10:49:09 +08002839 ci->i_wanted_max_size = 0; /* reset */
Sage Weilfeb4cc92010-11-07 09:39:00 -08002840 ci->i_requested_max_size = 0;
Sage Weilbe655592011-11-30 09:47:09 -08002841 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002842}
2843
2844/*
2845 * Handle a caps message from the MDS.
2846 *
2847 * Identify the appropriate session, inode, and call the right handler
2848 * based on the cap op.
2849 */
2850void ceph_handle_caps(struct ceph_mds_session *session,
2851 struct ceph_msg *msg)
2852{
2853 struct ceph_mds_client *mdsc = session->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002854 struct super_block *sb = mdsc->fsc->sb;
Sage Weila8599bd2009-10-06 11:31:12 -07002855 struct inode *inode;
Sage Weilbe655592011-11-30 09:47:09 -08002856 struct ceph_inode_info *ci;
Sage Weila8599bd2009-10-06 11:31:12 -07002857 struct ceph_cap *cap;
2858 struct ceph_mds_caps *h;
Sage Weil2600d2d2010-02-22 15:12:16 -08002859 int mds = session->s_mds;
Sage Weila8599bd2009-10-06 11:31:12 -07002860 int op;
Sage Weil3d7ded42010-06-09 16:47:10 -07002861 u32 seq, mseq;
Sage Weila8599bd2009-10-06 11:31:12 -07002862 struct ceph_vino vino;
2863 u64 cap_id;
2864 u64 size, max_size;
Sage Weil6df058c2009-12-22 11:24:33 -08002865 u64 tid;
Sage Weil70edb552010-03-01 13:20:50 -08002866 void *snaptrace;
Sage Weilce1fbc82010-08-02 15:09:39 -07002867 size_t snaptrace_len;
2868 void *flock;
2869 u32 flock_len;
Sage Weil154f42c2010-06-21 13:45:04 -07002870 int open_target_sessions = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002871
2872 dout("handle_caps from mds%d\n", mds);
2873
2874 /* decode */
Sage Weil6df058c2009-12-22 11:24:33 -08002875 tid = le64_to_cpu(msg->hdr.tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002876 if (msg->front.iov_len < sizeof(*h))
2877 goto bad;
2878 h = msg->front.iov_base;
2879 op = le32_to_cpu(h->op);
2880 vino.ino = le64_to_cpu(h->ino);
2881 vino.snap = CEPH_NOSNAP;
2882 cap_id = le64_to_cpu(h->cap_id);
2883 seq = le32_to_cpu(h->seq);
Sage Weil3d7ded42010-06-09 16:47:10 -07002884 mseq = le32_to_cpu(h->migrate_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07002885 size = le64_to_cpu(h->size);
2886 max_size = le64_to_cpu(h->max_size);
2887
Sage Weilce1fbc82010-08-02 15:09:39 -07002888 snaptrace = h + 1;
2889 snaptrace_len = le32_to_cpu(h->snap_trace_len);
2890
2891 if (le16_to_cpu(msg->hdr.version) >= 2) {
2892 void *p, *end;
2893
2894 p = snaptrace + snaptrace_len;
2895 end = msg->front.iov_base + msg->front.iov_len;
2896 ceph_decode_32_safe(&p, end, flock_len, bad);
2897 flock = p;
2898 } else {
2899 flock = NULL;
2900 flock_len = 0;
2901 }
2902
Sage Weila8599bd2009-10-06 11:31:12 -07002903 mutex_lock(&session->s_mutex);
2904 session->s_seq++;
2905 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2906 (unsigned)seq);
2907
Yan, Zheng66f58692013-01-04 14:45:18 +08002908 if (op == CEPH_CAP_OP_IMPORT)
2909 ceph_add_cap_releases(mdsc, session);
2910
Sage Weila8599bd2009-10-06 11:31:12 -07002911 /* lookup ino */
2912 inode = ceph_find_inode(sb, vino);
Sage Weilbe655592011-11-30 09:47:09 -08002913 ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002914 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2915 vino.snap, inode);
2916 if (!inode) {
2917 dout(" i don't have ino %llx\n", vino.ino);
Sage Weil3d7ded42010-06-09 16:47:10 -07002918
2919 if (op == CEPH_CAP_OP_IMPORT)
2920 __queue_cap_release(session, vino.ino, cap_id,
2921 mseq, seq);
Greg Farnum21b559d2010-10-06 15:46:30 -07002922 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07002923 }
2924
2925 /* these will work even if we don't have a cap yet */
2926 switch (op) {
2927 case CEPH_CAP_OP_FLUSHSNAP_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08002928 handle_cap_flushsnap_ack(inode, tid, h, session);
Sage Weila8599bd2009-10-06 11:31:12 -07002929 goto done;
2930
2931 case CEPH_CAP_OP_EXPORT:
Sage Weil154f42c2010-06-21 13:45:04 -07002932 handle_cap_export(inode, h, session, &open_target_sessions);
Sage Weila8599bd2009-10-06 11:31:12 -07002933 goto done;
2934
2935 case CEPH_CAP_OP_IMPORT:
2936 handle_cap_import(mdsc, inode, h, session,
Sage Weilce1fbc82010-08-02 15:09:39 -07002937 snaptrace, snaptrace_len);
Sage Weila8599bd2009-10-06 11:31:12 -07002938 }
2939
2940 /* the rest require a cap */
Sage Weilbe655592011-11-30 09:47:09 -08002941 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002942 cap = __get_cap_for_mds(ceph_inode(inode), mds);
2943 if (!cap) {
Sage Weil9dbd4122010-06-10 13:21:20 -07002944 dout(" no cap on %p ino %llx.%llx from mds%d\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002945 inode, ceph_ino(inode), ceph_snap(inode), mds);
Sage Weilbe655592011-11-30 09:47:09 -08002946 spin_unlock(&ci->i_ceph_lock);
Greg Farnum21b559d2010-10-06 15:46:30 -07002947 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07002948 }
2949
Sage Weilbe655592011-11-30 09:47:09 -08002950 /* note that each of these drops i_ceph_lock for us */
Sage Weila8599bd2009-10-06 11:31:12 -07002951 switch (op) {
2952 case CEPH_CAP_OP_REVOKE:
2953 case CEPH_CAP_OP_GRANT:
Yan, Zheng0e5e1772012-11-19 10:49:09 +08002954 case CEPH_CAP_OP_IMPORT:
Sage Weil15637c82010-03-16 13:42:00 -07002955 handle_cap_grant(inode, h, session, cap, msg->middle);
2956 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07002957
2958 case CEPH_CAP_OP_FLUSH_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08002959 handle_cap_flush_ack(inode, tid, h, session, cap);
Sage Weila8599bd2009-10-06 11:31:12 -07002960 break;
2961
2962 case CEPH_CAP_OP_TRUNC:
2963 handle_cap_trunc(inode, h, session);
2964 break;
2965
2966 default:
Sage Weilbe655592011-11-30 09:47:09 -08002967 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002968 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2969 ceph_cap_op_name(op));
2970 }
2971
Greg Farnum21b559d2010-10-06 15:46:30 -07002972 goto done;
2973
2974flush_cap_releases:
2975 /*
2976 * send any full release message to try to move things
2977 * along for the mds (who clearly thinks we still have this
2978 * cap).
2979 */
2980 ceph_add_cap_releases(mdsc, session);
2981 ceph_send_cap_releases(mdsc, session);
2982
Sage Weila8599bd2009-10-06 11:31:12 -07002983done:
Sage Weil15637c82010-03-16 13:42:00 -07002984 mutex_unlock(&session->s_mutex);
2985done_unlocked:
Sage Weila8599bd2009-10-06 11:31:12 -07002986 if (inode)
2987 iput(inode);
Sage Weil154f42c2010-06-21 13:45:04 -07002988 if (open_target_sessions)
2989 ceph_mdsc_open_export_target_sessions(mdsc, session);
Sage Weila8599bd2009-10-06 11:31:12 -07002990 return;
2991
2992bad:
2993 pr_err("ceph_handle_caps: corrupt message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002994 ceph_msg_dump(msg);
Sage Weila8599bd2009-10-06 11:31:12 -07002995 return;
2996}
2997
2998/*
2999 * Delayed work handler to process end of delayed cap release LRU list.
3000 */
Sage Weilafcdaea2009-10-14 14:27:38 -07003001void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -07003002{
3003 struct ceph_inode_info *ci;
3004 int flags = CHECK_CAPS_NODELAY;
3005
Sage Weila8599bd2009-10-06 11:31:12 -07003006 dout("check_delayed_caps\n");
3007 while (1) {
3008 spin_lock(&mdsc->cap_delay_lock);
3009 if (list_empty(&mdsc->cap_delay_list))
3010 break;
3011 ci = list_first_entry(&mdsc->cap_delay_list,
3012 struct ceph_inode_info,
3013 i_cap_delay_list);
3014 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3015 time_before(jiffies, ci->i_hold_caps_max))
3016 break;
3017 list_del_init(&ci->i_cap_delay_list);
3018 spin_unlock(&mdsc->cap_delay_lock);
3019 dout("check_delayed_caps on %p\n", &ci->vfs_inode);
3020 ceph_check_caps(ci, flags, NULL);
3021 }
3022 spin_unlock(&mdsc->cap_delay_lock);
3023}
3024
3025/*
Sage Weilafcdaea2009-10-14 14:27:38 -07003026 * Flush all dirty caps to the mds
3027 */
3028void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
3029{
Sage Weildb354052011-05-24 11:46:31 -07003030 struct ceph_inode_info *ci;
3031 struct inode *inode;
Sage Weilafcdaea2009-10-14 14:27:38 -07003032
3033 dout("flush_dirty_caps\n");
3034 spin_lock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07003035 while (!list_empty(&mdsc->cap_dirty)) {
3036 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
3037 i_dirty_item);
Sage Weil70b666c2011-05-27 09:24:26 -07003038 inode = &ci->vfs_inode;
3039 ihold(inode);
Sage Weildb354052011-05-24 11:46:31 -07003040 dout("flush_dirty_caps %p\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07003041 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weil70b666c2011-05-27 09:24:26 -07003042 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
3043 iput(inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07003044 spin_lock(&mdsc->cap_dirty_lock);
3045 }
3046 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07003047 dout("flush_dirty_caps done\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003048}
3049
3050/*
Sage Weila8599bd2009-10-06 11:31:12 -07003051 * Drop open file reference. If we were the last open file,
3052 * we may need to release capabilities to the MDS (or schedule
3053 * their delayed release).
3054 */
3055void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
3056{
3057 struct inode *inode = &ci->vfs_inode;
3058 int last = 0;
3059
Sage Weilbe655592011-11-30 09:47:09 -08003060 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003061 dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
3062 ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
3063 BUG_ON(ci->i_nr_by_mode[fmode] == 0);
3064 if (--ci->i_nr_by_mode[fmode] == 0)
3065 last++;
Sage Weilbe655592011-11-30 09:47:09 -08003066 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003067
3068 if (last && ci->i_vino.snap == CEPH_NOSNAP)
3069 ceph_check_caps(ci, 0, NULL);
3070}
3071
3072/*
3073 * Helpers for embedding cap and dentry lease releases into mds
3074 * requests.
3075 *
3076 * @force is used by dentry_release (below) to force inclusion of a
3077 * record for the directory inode, even when there aren't any caps to
3078 * drop.
3079 */
3080int ceph_encode_inode_release(void **p, struct inode *inode,
3081 int mds, int drop, int unless, int force)
3082{
3083 struct ceph_inode_info *ci = ceph_inode(inode);
3084 struct ceph_cap *cap;
3085 struct ceph_mds_request_release *rel = *p;
Sage Weilec97f882010-06-24 15:12:37 -07003086 int used, dirty;
Sage Weila8599bd2009-10-06 11:31:12 -07003087 int ret = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07003088
Sage Weilbe655592011-11-30 09:47:09 -08003089 spin_lock(&ci->i_ceph_lock);
Sage Weil916623d2010-03-16 15:01:07 -07003090 used = __ceph_caps_used(ci);
Sage Weilec97f882010-06-24 15:12:37 -07003091 dirty = __ceph_caps_dirty(ci);
Sage Weil916623d2010-03-16 15:01:07 -07003092
Sage Weilec97f882010-06-24 15:12:37 -07003093 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3094 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
Sage Weil916623d2010-03-16 15:01:07 -07003095 ceph_cap_string(unless));
3096
Sage Weilec97f882010-06-24 15:12:37 -07003097 /* only drop unused, clean caps */
3098 drop &= ~(used | dirty);
Sage Weil916623d2010-03-16 15:01:07 -07003099
Sage Weila8599bd2009-10-06 11:31:12 -07003100 cap = __get_cap_for_mds(ci, mds);
3101 if (cap && __cap_is_valid(cap)) {
3102 if (force ||
3103 ((cap->issued & drop) &&
3104 (cap->issued & unless) == 0)) {
3105 if ((cap->issued & drop) &&
3106 (cap->issued & unless) == 0) {
Yan, Zhengbb137f82013-06-03 18:22:17 +08003107 int wanted = __ceph_caps_wanted(ci);
3108 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
3109 wanted |= cap->mds_wanted;
3110 dout("encode_inode_release %p cap %p "
3111 "%s -> %s, wanted %s -> %s\n", inode, cap,
Sage Weila8599bd2009-10-06 11:31:12 -07003112 ceph_cap_string(cap->issued),
Yan, Zhengbb137f82013-06-03 18:22:17 +08003113 ceph_cap_string(cap->issued & ~drop),
3114 ceph_cap_string(cap->mds_wanted),
3115 ceph_cap_string(wanted));
3116
Sage Weila8599bd2009-10-06 11:31:12 -07003117 cap->issued &= ~drop;
3118 cap->implemented &= ~drop;
Yan, Zhengbb137f82013-06-03 18:22:17 +08003119 cap->mds_wanted = wanted;
Sage Weila8599bd2009-10-06 11:31:12 -07003120 } else {
3121 dout("encode_inode_release %p cap %p %s"
3122 " (force)\n", inode, cap,
3123 ceph_cap_string(cap->issued));
3124 }
3125
3126 rel->ino = cpu_to_le64(ceph_ino(inode));
3127 rel->cap_id = cpu_to_le64(cap->cap_id);
3128 rel->seq = cpu_to_le32(cap->seq);
3129 rel->issue_seq = cpu_to_le32(cap->issue_seq),
3130 rel->mseq = cpu_to_le32(cap->mseq);
3131 rel->caps = cpu_to_le32(cap->issued);
3132 rel->wanted = cpu_to_le32(cap->mds_wanted);
3133 rel->dname_len = 0;
3134 rel->dname_seq = 0;
3135 *p += sizeof(*rel);
3136 ret = 1;
3137 } else {
3138 dout("encode_inode_release %p cap %p %s\n",
3139 inode, cap, ceph_cap_string(cap->issued));
3140 }
3141 }
Sage Weilbe655592011-11-30 09:47:09 -08003142 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003143 return ret;
3144}
3145
3146int ceph_encode_dentry_release(void **p, struct dentry *dentry,
3147 int mds, int drop, int unless)
3148{
3149 struct inode *dir = dentry->d_parent->d_inode;
3150 struct ceph_mds_request_release *rel = *p;
3151 struct ceph_dentry_info *di = ceph_dentry(dentry);
3152 int force = 0;
3153 int ret;
3154
3155 /*
3156 * force an record for the directory caps if we have a dentry lease.
Sage Weilbe655592011-11-30 09:47:09 -08003157 * this is racy (can't take i_ceph_lock and d_lock together), but it
Sage Weila8599bd2009-10-06 11:31:12 -07003158 * doesn't have to be perfect; the mds will revoke anything we don't
3159 * release.
3160 */
3161 spin_lock(&dentry->d_lock);
3162 if (di->lease_session && di->lease_session->s_mds == mds)
3163 force = 1;
3164 spin_unlock(&dentry->d_lock);
3165
3166 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
3167
3168 spin_lock(&dentry->d_lock);
3169 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
3170 dout("encode_dentry_release %p mds%d seq %d\n",
3171 dentry, mds, (int)di->lease_seq);
3172 rel->dname_len = cpu_to_le32(dentry->d_name.len);
3173 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3174 *p += dentry->d_name.len;
3175 rel->dname_seq = cpu_to_le32(di->lease_seq);
Sage Weil1dadcce2010-07-23 13:54:21 -07003176 __ceph_mdsc_drop_dentry_lease(dentry);
Sage Weila8599bd2009-10-06 11:31:12 -07003177 }
3178 spin_unlock(&dentry->d_lock);
3179 return ret;
3180}