xref: /openbmc/linux/fs/ceph/caps.c (revision 3231300b)
13d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
2a8599bd8SSage Weil 
3a8599bd8SSage Weil #include <linux/fs.h>
4a8599bd8SSage Weil #include <linux/kernel.h>
5a8599bd8SSage Weil #include <linux/sched.h>
65a0e3ad6STejun Heo #include <linux/slab.h>
7a8599bd8SSage Weil #include <linux/vmalloc.h>
8a8599bd8SSage Weil #include <linux/wait.h>
9f1a3d572SStephen Rothwell #include <linux/writeback.h>
10a8599bd8SSage Weil 
11a8599bd8SSage Weil #include "super.h"
123d14c5d2SYehuda Sadeh #include "mds_client.h"
1399ccbd22SMilosz Tanski #include "cache.h"
143d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
153d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
16a8599bd8SSage Weil 
17a8599bd8SSage Weil /*
18a8599bd8SSage Weil  * Capability management
19a8599bd8SSage Weil  *
20a8599bd8SSage Weil  * The Ceph metadata servers control client access to inode metadata
21a8599bd8SSage Weil  * and file data by issuing capabilities, granting clients permission
22a8599bd8SSage Weil  * to read and/or write both inode field and file data to OSDs
23a8599bd8SSage Weil  * (storage nodes).  Each capability consists of a set of bits
24a8599bd8SSage Weil  * indicating which operations are allowed.
25a8599bd8SSage Weil  *
26a8599bd8SSage Weil  * If the client holds a *_SHARED cap, the client has a coherent value
27a8599bd8SSage Weil  * that can be safely read from the cached inode.
28a8599bd8SSage Weil  *
29a8599bd8SSage Weil  * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
30a8599bd8SSage Weil  * client is allowed to change inode attributes (e.g., file size,
31a8599bd8SSage Weil  * mtime), note its dirty state in the ceph_cap, and asynchronously
32a8599bd8SSage Weil  * flush that metadata change to the MDS.
33a8599bd8SSage Weil  *
34a8599bd8SSage Weil  * In the event of a conflicting operation (perhaps by another
35a8599bd8SSage Weil  * client), the MDS will revoke the conflicting client capabilities.
36a8599bd8SSage Weil  *
37a8599bd8SSage Weil  * In order for a client to cache an inode, it must hold a capability
38a8599bd8SSage Weil  * with at least one MDS server.  When inodes are released, release
39a8599bd8SSage Weil  * notifications are batched and periodically sent en masse to the MDS
40a8599bd8SSage Weil  * cluster to release server state.
41a8599bd8SSage Weil  */
42a8599bd8SSage Weil 
43a8599bd8SSage Weil 
44a8599bd8SSage Weil /*
45a8599bd8SSage Weil  * Generate readable cap strings for debugging output.
46a8599bd8SSage Weil  */
47a8599bd8SSage Weil #define MAX_CAP_STR 20
48a8599bd8SSage Weil static char cap_str[MAX_CAP_STR][40];
49a8599bd8SSage Weil static DEFINE_SPINLOCK(cap_str_lock);
50a8599bd8SSage Weil static int last_cap_str;
51a8599bd8SSage Weil 
52a8599bd8SSage Weil static char *gcap_string(char *s, int c)
53a8599bd8SSage Weil {
54a8599bd8SSage Weil 	if (c & CEPH_CAP_GSHARED)
55a8599bd8SSage Weil 		*s++ = 's';
56a8599bd8SSage Weil 	if (c & CEPH_CAP_GEXCL)
57a8599bd8SSage Weil 		*s++ = 'x';
58a8599bd8SSage Weil 	if (c & CEPH_CAP_GCACHE)
59a8599bd8SSage Weil 		*s++ = 'c';
60a8599bd8SSage Weil 	if (c & CEPH_CAP_GRD)
61a8599bd8SSage Weil 		*s++ = 'r';
62a8599bd8SSage Weil 	if (c & CEPH_CAP_GWR)
63a8599bd8SSage Weil 		*s++ = 'w';
64a8599bd8SSage Weil 	if (c & CEPH_CAP_GBUFFER)
65a8599bd8SSage Weil 		*s++ = 'b';
66a8599bd8SSage Weil 	if (c & CEPH_CAP_GLAZYIO)
67a8599bd8SSage Weil 		*s++ = 'l';
68a8599bd8SSage Weil 	return s;
69a8599bd8SSage Weil }
70a8599bd8SSage Weil 
71a8599bd8SSage Weil const char *ceph_cap_string(int caps)
72a8599bd8SSage Weil {
73a8599bd8SSage Weil 	int i;
74a8599bd8SSage Weil 	char *s;
75a8599bd8SSage Weil 	int c;
76a8599bd8SSage Weil 
77a8599bd8SSage Weil 	spin_lock(&cap_str_lock);
78a8599bd8SSage Weil 	i = last_cap_str++;
79a8599bd8SSage Weil 	if (last_cap_str == MAX_CAP_STR)
80a8599bd8SSage Weil 		last_cap_str = 0;
81a8599bd8SSage Weil 	spin_unlock(&cap_str_lock);
82a8599bd8SSage Weil 
83a8599bd8SSage Weil 	s = cap_str[i];
84a8599bd8SSage Weil 
85a8599bd8SSage Weil 	if (caps & CEPH_CAP_PIN)
86a8599bd8SSage Weil 		*s++ = 'p';
87a8599bd8SSage Weil 
88a8599bd8SSage Weil 	c = (caps >> CEPH_CAP_SAUTH) & 3;
89a8599bd8SSage Weil 	if (c) {
90a8599bd8SSage Weil 		*s++ = 'A';
91a8599bd8SSage Weil 		s = gcap_string(s, c);
92a8599bd8SSage Weil 	}
93a8599bd8SSage Weil 
94a8599bd8SSage Weil 	c = (caps >> CEPH_CAP_SLINK) & 3;
95a8599bd8SSage Weil 	if (c) {
96a8599bd8SSage Weil 		*s++ = 'L';
97a8599bd8SSage Weil 		s = gcap_string(s, c);
98a8599bd8SSage Weil 	}
99a8599bd8SSage Weil 
100a8599bd8SSage Weil 	c = (caps >> CEPH_CAP_SXATTR) & 3;
101a8599bd8SSage Weil 	if (c) {
102a8599bd8SSage Weil 		*s++ = 'X';
103a8599bd8SSage Weil 		s = gcap_string(s, c);
104a8599bd8SSage Weil 	}
105a8599bd8SSage Weil 
106a8599bd8SSage Weil 	c = caps >> CEPH_CAP_SFILE;
107a8599bd8SSage Weil 	if (c) {
108a8599bd8SSage Weil 		*s++ = 'F';
109a8599bd8SSage Weil 		s = gcap_string(s, c);
110a8599bd8SSage Weil 	}
111a8599bd8SSage Weil 
112a8599bd8SSage Weil 	if (s == cap_str[i])
113a8599bd8SSage Weil 		*s++ = '-';
114a8599bd8SSage Weil 	*s = 0;
115a8599bd8SSage Weil 	return cap_str[i];
116a8599bd8SSage Weil }
117a8599bd8SSage Weil 
11837151668SYehuda Sadeh void ceph_caps_init(struct ceph_mds_client *mdsc)
119a8599bd8SSage Weil {
12037151668SYehuda Sadeh 	INIT_LIST_HEAD(&mdsc->caps_list);
12137151668SYehuda Sadeh 	spin_lock_init(&mdsc->caps_list_lock);
122a8599bd8SSage Weil }
123a8599bd8SSage Weil 
12437151668SYehuda Sadeh void ceph_caps_finalize(struct ceph_mds_client *mdsc)
125a8599bd8SSage Weil {
126a8599bd8SSage Weil 	struct ceph_cap *cap;
127a8599bd8SSage Weil 
12837151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
12937151668SYehuda Sadeh 	while (!list_empty(&mdsc->caps_list)) {
13037151668SYehuda Sadeh 		cap = list_first_entry(&mdsc->caps_list,
13137151668SYehuda Sadeh 				       struct ceph_cap, caps_item);
132a8599bd8SSage Weil 		list_del(&cap->caps_item);
133a8599bd8SSage Weil 		kmem_cache_free(ceph_cap_cachep, cap);
134a8599bd8SSage Weil 	}
13537151668SYehuda Sadeh 	mdsc->caps_total_count = 0;
13637151668SYehuda Sadeh 	mdsc->caps_avail_count = 0;
13737151668SYehuda Sadeh 	mdsc->caps_use_count = 0;
13837151668SYehuda Sadeh 	mdsc->caps_reserve_count = 0;
13937151668SYehuda Sadeh 	mdsc->caps_min_count = 0;
14037151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
14185ccce43SSage Weil }
14285ccce43SSage Weil 
14337151668SYehuda Sadeh void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
14485ccce43SSage Weil {
14537151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
14637151668SYehuda Sadeh 	mdsc->caps_min_count += delta;
14737151668SYehuda Sadeh 	BUG_ON(mdsc->caps_min_count < 0);
14837151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
149a8599bd8SSage Weil }
150a8599bd8SSage Weil 
15193faca6eSmajianpeng void ceph_reserve_caps(struct ceph_mds_client *mdsc,
15237151668SYehuda Sadeh 		      struct ceph_cap_reservation *ctx, int need)
153a8599bd8SSage Weil {
154a8599bd8SSage Weil 	int i;
155a8599bd8SSage Weil 	struct ceph_cap *cap;
156a8599bd8SSage Weil 	int have;
157a8599bd8SSage Weil 	int alloc = 0;
158a8599bd8SSage Weil 	LIST_HEAD(newcaps);
159a8599bd8SSage Weil 
160a8599bd8SSage Weil 	dout("reserve caps ctx=%p need=%d\n", ctx, need);
161a8599bd8SSage Weil 
162a8599bd8SSage Weil 	/* first reserve any caps that are already allocated */
16337151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
16437151668SYehuda Sadeh 	if (mdsc->caps_avail_count >= need)
165a8599bd8SSage Weil 		have = need;
166a8599bd8SSage Weil 	else
16737151668SYehuda Sadeh 		have = mdsc->caps_avail_count;
16837151668SYehuda Sadeh 	mdsc->caps_avail_count -= have;
16937151668SYehuda Sadeh 	mdsc->caps_reserve_count += have;
17037151668SYehuda Sadeh 	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
17137151668SYehuda Sadeh 					 mdsc->caps_reserve_count +
17237151668SYehuda Sadeh 					 mdsc->caps_avail_count);
17337151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
174a8599bd8SSage Weil 
175a8599bd8SSage Weil 	for (i = have; i < need; i++) {
176a8599bd8SSage Weil 		cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
17793faca6eSmajianpeng 		if (!cap)
17893faca6eSmajianpeng 			break;
179a8599bd8SSage Weil 		list_add(&cap->caps_item, &newcaps);
180a8599bd8SSage Weil 		alloc++;
181a8599bd8SSage Weil 	}
18293faca6eSmajianpeng 	/* we didn't manage to reserve as much as we needed */
18393faca6eSmajianpeng 	if (have + alloc != need)
18493faca6eSmajianpeng 		pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
18593faca6eSmajianpeng 			ctx, need, have + alloc);
186a8599bd8SSage Weil 
18737151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
18837151668SYehuda Sadeh 	mdsc->caps_total_count += alloc;
18937151668SYehuda Sadeh 	mdsc->caps_reserve_count += alloc;
19037151668SYehuda Sadeh 	list_splice(&newcaps, &mdsc->caps_list);
191a8599bd8SSage Weil 
19237151668SYehuda Sadeh 	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
19337151668SYehuda Sadeh 					 mdsc->caps_reserve_count +
19437151668SYehuda Sadeh 					 mdsc->caps_avail_count);
19537151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
196a8599bd8SSage Weil 
197a8599bd8SSage Weil 	ctx->count = need;
198a8599bd8SSage Weil 	dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
19937151668SYehuda Sadeh 	     ctx, mdsc->caps_total_count, mdsc->caps_use_count,
20037151668SYehuda Sadeh 	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
201a8599bd8SSage Weil }
202a8599bd8SSage Weil 
20337151668SYehuda Sadeh int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
20437151668SYehuda Sadeh 			struct ceph_cap_reservation *ctx)
205a8599bd8SSage Weil {
206a8599bd8SSage Weil 	dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
207a8599bd8SSage Weil 	if (ctx->count) {
20837151668SYehuda Sadeh 		spin_lock(&mdsc->caps_list_lock);
20937151668SYehuda Sadeh 		BUG_ON(mdsc->caps_reserve_count < ctx->count);
21037151668SYehuda Sadeh 		mdsc->caps_reserve_count -= ctx->count;
21137151668SYehuda Sadeh 		mdsc->caps_avail_count += ctx->count;
212a8599bd8SSage Weil 		ctx->count = 0;
213a8599bd8SSage Weil 		dout("unreserve caps %d = %d used + %d resv + %d avail\n",
21437151668SYehuda Sadeh 		     mdsc->caps_total_count, mdsc->caps_use_count,
21537151668SYehuda Sadeh 		     mdsc->caps_reserve_count, mdsc->caps_avail_count);
21637151668SYehuda Sadeh 		BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
21737151668SYehuda Sadeh 						 mdsc->caps_reserve_count +
21837151668SYehuda Sadeh 						 mdsc->caps_avail_count);
21937151668SYehuda Sadeh 		spin_unlock(&mdsc->caps_list_lock);
220a8599bd8SSage Weil 	}
221a8599bd8SSage Weil 	return 0;
222a8599bd8SSage Weil }
223a8599bd8SSage Weil 
224d9df2783SYan, Zheng struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
22537151668SYehuda Sadeh 			      struct ceph_cap_reservation *ctx)
226a8599bd8SSage Weil {
227a8599bd8SSage Weil 	struct ceph_cap *cap = NULL;
228a8599bd8SSage Weil 
229a8599bd8SSage Weil 	/* temporary, until we do something about cap import/export */
230443b3760SSage Weil 	if (!ctx) {
231443b3760SSage Weil 		cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
232443b3760SSage Weil 		if (cap) {
2334d1d0534SYan, Zheng 			spin_lock(&mdsc->caps_list_lock);
23437151668SYehuda Sadeh 			mdsc->caps_use_count++;
23537151668SYehuda Sadeh 			mdsc->caps_total_count++;
2364d1d0534SYan, Zheng 			spin_unlock(&mdsc->caps_list_lock);
237443b3760SSage Weil 		}
238443b3760SSage Weil 		return cap;
239443b3760SSage Weil 	}
240a8599bd8SSage Weil 
24137151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
242a8599bd8SSage Weil 	dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
24337151668SYehuda Sadeh 	     ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
24437151668SYehuda Sadeh 	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
245a8599bd8SSage Weil 	BUG_ON(!ctx->count);
24637151668SYehuda Sadeh 	BUG_ON(ctx->count > mdsc->caps_reserve_count);
24737151668SYehuda Sadeh 	BUG_ON(list_empty(&mdsc->caps_list));
248a8599bd8SSage Weil 
249a8599bd8SSage Weil 	ctx->count--;
25037151668SYehuda Sadeh 	mdsc->caps_reserve_count--;
25137151668SYehuda Sadeh 	mdsc->caps_use_count++;
252a8599bd8SSage Weil 
25337151668SYehuda Sadeh 	cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
254a8599bd8SSage Weil 	list_del(&cap->caps_item);
255a8599bd8SSage Weil 
25637151668SYehuda Sadeh 	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
25737151668SYehuda Sadeh 	       mdsc->caps_reserve_count + mdsc->caps_avail_count);
25837151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
259a8599bd8SSage Weil 	return cap;
260a8599bd8SSage Weil }
261a8599bd8SSage Weil 
26237151668SYehuda Sadeh void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
263a8599bd8SSage Weil {
26437151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
2657c1332b8SSage Weil 	dout("put_cap %p %d = %d used + %d resv + %d avail\n",
26637151668SYehuda Sadeh 	     cap, mdsc->caps_total_count, mdsc->caps_use_count,
26737151668SYehuda Sadeh 	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
26837151668SYehuda Sadeh 	mdsc->caps_use_count--;
269a8599bd8SSage Weil 	/*
27085ccce43SSage Weil 	 * Keep some preallocated caps around (ceph_min_count), to
27185ccce43SSage Weil 	 * avoid lots of free/alloc churn.
272a8599bd8SSage Weil 	 */
27337151668SYehuda Sadeh 	if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
27437151668SYehuda Sadeh 				      mdsc->caps_min_count) {
27537151668SYehuda Sadeh 		mdsc->caps_total_count--;
276a8599bd8SSage Weil 		kmem_cache_free(ceph_cap_cachep, cap);
277a8599bd8SSage Weil 	} else {
27837151668SYehuda Sadeh 		mdsc->caps_avail_count++;
27937151668SYehuda Sadeh 		list_add(&cap->caps_item, &mdsc->caps_list);
280a8599bd8SSage Weil 	}
281a8599bd8SSage Weil 
28237151668SYehuda Sadeh 	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
28337151668SYehuda Sadeh 	       mdsc->caps_reserve_count + mdsc->caps_avail_count);
28437151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
285a8599bd8SSage Weil }
286a8599bd8SSage Weil 
2873d14c5d2SYehuda Sadeh void ceph_reservation_status(struct ceph_fs_client *fsc,
28885ccce43SSage Weil 			     int *total, int *avail, int *used, int *reserved,
28985ccce43SSage Weil 			     int *min)
290a8599bd8SSage Weil {
2913d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
29237151668SYehuda Sadeh 
293a8599bd8SSage Weil 	if (total)
29437151668SYehuda Sadeh 		*total = mdsc->caps_total_count;
295a8599bd8SSage Weil 	if (avail)
29637151668SYehuda Sadeh 		*avail = mdsc->caps_avail_count;
297a8599bd8SSage Weil 	if (used)
29837151668SYehuda Sadeh 		*used = mdsc->caps_use_count;
299a8599bd8SSage Weil 	if (reserved)
30037151668SYehuda Sadeh 		*reserved = mdsc->caps_reserve_count;
30185ccce43SSage Weil 	if (min)
30237151668SYehuda Sadeh 		*min = mdsc->caps_min_count;
303a8599bd8SSage Weil }
304a8599bd8SSage Weil 
305a8599bd8SSage Weil /*
306a8599bd8SSage Weil  * Find ceph_cap for given mds, if any.
307a8599bd8SSage Weil  *
308be655596SSage Weil  * Called with i_ceph_lock held.
309a8599bd8SSage Weil  */
310a8599bd8SSage Weil static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
311a8599bd8SSage Weil {
312a8599bd8SSage Weil 	struct ceph_cap *cap;
313a8599bd8SSage Weil 	struct rb_node *n = ci->i_caps.rb_node;
314a8599bd8SSage Weil 
315a8599bd8SSage Weil 	while (n) {
316a8599bd8SSage Weil 		cap = rb_entry(n, struct ceph_cap, ci_node);
317a8599bd8SSage Weil 		if (mds < cap->mds)
318a8599bd8SSage Weil 			n = n->rb_left;
319a8599bd8SSage Weil 		else if (mds > cap->mds)
320a8599bd8SSage Weil 			n = n->rb_right;
321a8599bd8SSage Weil 		else
322a8599bd8SSage Weil 			return cap;
323a8599bd8SSage Weil 	}
324a8599bd8SSage Weil 	return NULL;
325a8599bd8SSage Weil }
326a8599bd8SSage Weil 
3272bc50259SGreg Farnum struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
3282bc50259SGreg Farnum {
3292bc50259SGreg Farnum 	struct ceph_cap *cap;
3302bc50259SGreg Farnum 
331be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
3322bc50259SGreg Farnum 	cap = __get_cap_for_mds(ci, mds);
333be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
3342bc50259SGreg Farnum 	return cap;
3352bc50259SGreg Farnum }
3362bc50259SGreg Farnum 
337a8599bd8SSage Weil /*
33833caad32SSage Weil  * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
339a8599bd8SSage Weil  */
340ca81f3f6SSage Weil static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
341a8599bd8SSage Weil {
342a8599bd8SSage Weil 	struct ceph_cap *cap;
343a8599bd8SSage Weil 	int mds = -1;
344a8599bd8SSage Weil 	struct rb_node *p;
345a8599bd8SSage Weil 
34633caad32SSage Weil 	/* prefer mds with WR|BUFFER|EXCL caps */
347a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
348a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
349a8599bd8SSage Weil 		mds = cap->mds;
350a8599bd8SSage Weil 		if (cap->issued & (CEPH_CAP_FILE_WR |
351a8599bd8SSage Weil 				   CEPH_CAP_FILE_BUFFER |
352a8599bd8SSage Weil 				   CEPH_CAP_FILE_EXCL))
353a8599bd8SSage Weil 			break;
354a8599bd8SSage Weil 	}
355a8599bd8SSage Weil 	return mds;
356a8599bd8SSage Weil }
357a8599bd8SSage Weil 
358a8599bd8SSage Weil int ceph_get_cap_mds(struct inode *inode)
359a8599bd8SSage Weil {
360be655596SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
361a8599bd8SSage Weil 	int mds;
362be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
363ca81f3f6SSage Weil 	mds = __ceph_get_cap_mds(ceph_inode(inode));
364be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
365a8599bd8SSage Weil 	return mds;
366a8599bd8SSage Weil }
367a8599bd8SSage Weil 
368a8599bd8SSage Weil /*
369be655596SSage Weil  * Called under i_ceph_lock.
370a8599bd8SSage Weil  */
371a8599bd8SSage Weil static void __insert_cap_node(struct ceph_inode_info *ci,
372a8599bd8SSage Weil 			      struct ceph_cap *new)
373a8599bd8SSage Weil {
374a8599bd8SSage Weil 	struct rb_node **p = &ci->i_caps.rb_node;
375a8599bd8SSage Weil 	struct rb_node *parent = NULL;
376a8599bd8SSage Weil 	struct ceph_cap *cap = NULL;
377a8599bd8SSage Weil 
378a8599bd8SSage Weil 	while (*p) {
379a8599bd8SSage Weil 		parent = *p;
380a8599bd8SSage Weil 		cap = rb_entry(parent, struct ceph_cap, ci_node);
381a8599bd8SSage Weil 		if (new->mds < cap->mds)
382a8599bd8SSage Weil 			p = &(*p)->rb_left;
383a8599bd8SSage Weil 		else if (new->mds > cap->mds)
384a8599bd8SSage Weil 			p = &(*p)->rb_right;
385a8599bd8SSage Weil 		else
386a8599bd8SSage Weil 			BUG();
387a8599bd8SSage Weil 	}
388a8599bd8SSage Weil 
389a8599bd8SSage Weil 	rb_link_node(&new->ci_node, parent, p);
390a8599bd8SSage Weil 	rb_insert_color(&new->ci_node, &ci->i_caps);
391a8599bd8SSage Weil }
392a8599bd8SSage Weil 
393a8599bd8SSage Weil /*
394a8599bd8SSage Weil  * (re)set cap hold timeouts, which control the delayed release
395a8599bd8SSage Weil  * of unused caps back to the MDS.  Should be called on cap use.
396a8599bd8SSage Weil  */
397a8599bd8SSage Weil static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
398a8599bd8SSage Weil 			       struct ceph_inode_info *ci)
399a8599bd8SSage Weil {
4003d14c5d2SYehuda Sadeh 	struct ceph_mount_options *ma = mdsc->fsc->mount_options;
401a8599bd8SSage Weil 
402a8599bd8SSage Weil 	ci->i_hold_caps_min = round_jiffies(jiffies +
403a8599bd8SSage Weil 					    ma->caps_wanted_delay_min * HZ);
404a8599bd8SSage Weil 	ci->i_hold_caps_max = round_jiffies(jiffies +
405a8599bd8SSage Weil 					    ma->caps_wanted_delay_max * HZ);
406a8599bd8SSage Weil 	dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
407a8599bd8SSage Weil 	     ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
408a8599bd8SSage Weil }
409a8599bd8SSage Weil 
410a8599bd8SSage Weil /*
411a8599bd8SSage Weil  * (Re)queue cap at the end of the delayed cap release list.
412a8599bd8SSage Weil  *
413a8599bd8SSage Weil  * If I_FLUSH is set, leave the inode at the front of the list.
414a8599bd8SSage Weil  *
415be655596SSage Weil  * Caller holds i_ceph_lock
416a8599bd8SSage Weil  *    -> we take mdsc->cap_delay_lock
417a8599bd8SSage Weil  */
418a8599bd8SSage Weil static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
419a8599bd8SSage Weil 				struct ceph_inode_info *ci)
420a8599bd8SSage Weil {
421a8599bd8SSage Weil 	__cap_set_timeouts(mdsc, ci);
422a8599bd8SSage Weil 	dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
423a8599bd8SSage Weil 	     ci->i_ceph_flags, ci->i_hold_caps_max);
424a8599bd8SSage Weil 	if (!mdsc->stopping) {
425a8599bd8SSage Weil 		spin_lock(&mdsc->cap_delay_lock);
426a8599bd8SSage Weil 		if (!list_empty(&ci->i_cap_delay_list)) {
427a8599bd8SSage Weil 			if (ci->i_ceph_flags & CEPH_I_FLUSH)
428a8599bd8SSage Weil 				goto no_change;
429a8599bd8SSage Weil 			list_del_init(&ci->i_cap_delay_list);
430a8599bd8SSage Weil 		}
431a8599bd8SSage Weil 		list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
432a8599bd8SSage Weil no_change:
433a8599bd8SSage Weil 		spin_unlock(&mdsc->cap_delay_lock);
434a8599bd8SSage Weil 	}
435a8599bd8SSage Weil }
436a8599bd8SSage Weil 
437a8599bd8SSage Weil /*
438a8599bd8SSage Weil  * Queue an inode for immediate writeback.  Mark inode with I_FLUSH,
439a8599bd8SSage Weil  * indicating we should send a cap message to flush dirty metadata
440a8599bd8SSage Weil  * asap, and move to the front of the delayed cap list.
441a8599bd8SSage Weil  */
442a8599bd8SSage Weil static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
443a8599bd8SSage Weil 				      struct ceph_inode_info *ci)
444a8599bd8SSage Weil {
445a8599bd8SSage Weil 	dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
446a8599bd8SSage Weil 	spin_lock(&mdsc->cap_delay_lock);
447a8599bd8SSage Weil 	ci->i_ceph_flags |= CEPH_I_FLUSH;
448a8599bd8SSage Weil 	if (!list_empty(&ci->i_cap_delay_list))
449a8599bd8SSage Weil 		list_del_init(&ci->i_cap_delay_list);
450a8599bd8SSage Weil 	list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
451a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_delay_lock);
452a8599bd8SSage Weil }
453a8599bd8SSage Weil 
454a8599bd8SSage Weil /*
455a8599bd8SSage Weil  * Cancel delayed work on cap.
456a8599bd8SSage Weil  *
457be655596SSage Weil  * Caller must hold i_ceph_lock.
458a8599bd8SSage Weil  */
459a8599bd8SSage Weil static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
460a8599bd8SSage Weil 			       struct ceph_inode_info *ci)
461a8599bd8SSage Weil {
462a8599bd8SSage Weil 	dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
463a8599bd8SSage Weil 	if (list_empty(&ci->i_cap_delay_list))
464a8599bd8SSage Weil 		return;
465a8599bd8SSage Weil 	spin_lock(&mdsc->cap_delay_lock);
466a8599bd8SSage Weil 	list_del_init(&ci->i_cap_delay_list);
467a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_delay_lock);
468a8599bd8SSage Weil }
469a8599bd8SSage Weil 
470a8599bd8SSage Weil /*
471a8599bd8SSage Weil  * Common issue checks for add_cap, handle_cap_grant.
472a8599bd8SSage Weil  */
473a8599bd8SSage Weil static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
474a8599bd8SSage Weil 			      unsigned issued)
475a8599bd8SSage Weil {
476a8599bd8SSage Weil 	unsigned had = __ceph_caps_issued(ci, NULL);
477a8599bd8SSage Weil 
478a8599bd8SSage Weil 	/*
479a8599bd8SSage Weil 	 * Each time we receive FILE_CACHE anew, we increment
480a8599bd8SSage Weil 	 * i_rdcache_gen.
481a8599bd8SSage Weil 	 */
4822962507cSSage Weil 	if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
48399ccbd22SMilosz Tanski 	    (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
484a8599bd8SSage Weil 		ci->i_rdcache_gen++;
48599ccbd22SMilosz Tanski 	}
486a8599bd8SSage Weil 
487a8599bd8SSage Weil 	/*
4882f276c51SYan, Zheng 	 * if we are newly issued FILE_SHARED, mark dir not complete; we
489a8599bd8SSage Weil 	 * don't know what happened to this directory while we didn't
490a8599bd8SSage Weil 	 * have the cap.
491a8599bd8SSage Weil 	 */
492a8599bd8SSage Weil 	if ((issued & CEPH_CAP_FILE_SHARED) &&
493a8599bd8SSage Weil 	    (had & CEPH_CAP_FILE_SHARED) == 0) {
494a8599bd8SSage Weil 		ci->i_shared_gen++;
495a8673d61SYan, Zheng 		if (S_ISDIR(ci->vfs_inode.i_mode)) {
496a8673d61SYan, Zheng 			dout(" marking %p NOT complete\n", &ci->vfs_inode);
4972f276c51SYan, Zheng 			__ceph_dir_clear_complete(ci);
498a8673d61SYan, Zheng 		}
499a8599bd8SSage Weil 	}
500a8599bd8SSage Weil }
501a8599bd8SSage Weil 
502a8599bd8SSage Weil /*
503a8599bd8SSage Weil  * Add a capability under the given MDS session.
504a8599bd8SSage Weil  *
505a8599bd8SSage Weil  * Caller should hold session snap_rwsem (read) and s_mutex.
506a8599bd8SSage Weil  *
507a8599bd8SSage Weil  * @fmode is the open file mode, if we are opening a file, otherwise
508a8599bd8SSage Weil  * it is < 0.  (This is so we can atomically add the cap and add an
509a8599bd8SSage Weil  * open file reference to it.)
510a8599bd8SSage Weil  */
511d9df2783SYan, Zheng void ceph_add_cap(struct inode *inode,
512a8599bd8SSage Weil 		  struct ceph_mds_session *session, u64 cap_id,
513a8599bd8SSage Weil 		  int fmode, unsigned issued, unsigned wanted,
514a8599bd8SSage Weil 		  unsigned seq, unsigned mseq, u64 realmino, int flags,
515d9df2783SYan, Zheng 		  struct ceph_cap **new_cap)
516a8599bd8SSage Weil {
5173d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
518a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
519a8599bd8SSage Weil 	struct ceph_cap *cap;
520a8599bd8SSage Weil 	int mds = session->s_mds;
521a8599bd8SSage Weil 	int actual_wanted;
522a8599bd8SSage Weil 
523a8599bd8SSage Weil 	dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
524a8599bd8SSage Weil 	     session->s_mds, cap_id, ceph_cap_string(issued), seq);
525a8599bd8SSage Weil 
526a8599bd8SSage Weil 	/*
527a8599bd8SSage Weil 	 * If we are opening the file, include file mode wanted bits
528a8599bd8SSage Weil 	 * in wanted.
529a8599bd8SSage Weil 	 */
530a8599bd8SSage Weil 	if (fmode >= 0)
531a8599bd8SSage Weil 		wanted |= ceph_caps_for_mode(fmode);
532a8599bd8SSage Weil 
533a8599bd8SSage Weil 	cap = __get_cap_for_mds(ci, mds);
534a8599bd8SSage Weil 	if (!cap) {
535d9df2783SYan, Zheng 		cap = *new_cap;
536d9df2783SYan, Zheng 		*new_cap = NULL;
537a8599bd8SSage Weil 
538a8599bd8SSage Weil 		cap->issued = 0;
539a8599bd8SSage Weil 		cap->implemented = 0;
540a8599bd8SSage Weil 		cap->mds = mds;
541a8599bd8SSage Weil 		cap->mds_wanted = 0;
542964266ccSYan, Zheng 		cap->mseq = 0;
543a8599bd8SSage Weil 
544a8599bd8SSage Weil 		cap->ci = ci;
545a8599bd8SSage Weil 		__insert_cap_node(ci, cap);
546a8599bd8SSage Weil 
547a8599bd8SSage Weil 		/* add to session cap list */
548a8599bd8SSage Weil 		cap->session = session;
549a8599bd8SSage Weil 		spin_lock(&session->s_cap_lock);
550a8599bd8SSage Weil 		list_add_tail(&cap->session_caps, &session->s_caps);
551a8599bd8SSage Weil 		session->s_nr_caps++;
552a8599bd8SSage Weil 		spin_unlock(&session->s_cap_lock);
55311df2dfbSYan, Zheng 	} else {
55411df2dfbSYan, Zheng 		/*
55511df2dfbSYan, Zheng 		 * auth mds of the inode changed. we received the cap export
55611df2dfbSYan, Zheng 		 * message, but still haven't received the cap import message.
55711df2dfbSYan, Zheng 		 * handle_cap_export() updated the new auth MDS' cap.
55811df2dfbSYan, Zheng 		 *
55911df2dfbSYan, Zheng 		 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
56011df2dfbSYan, Zheng 		 * a message that was send before the cap import message. So
56111df2dfbSYan, Zheng 		 * don't remove caps.
56211df2dfbSYan, Zheng 		 */
56311df2dfbSYan, Zheng 		if (ceph_seq_cmp(seq, cap->seq) <= 0) {
56411df2dfbSYan, Zheng 			WARN_ON(cap != ci->i_auth_cap);
56511df2dfbSYan, Zheng 			WARN_ON(cap->cap_id != cap_id);
56611df2dfbSYan, Zheng 			seq = cap->seq;
56711df2dfbSYan, Zheng 			mseq = cap->mseq;
56811df2dfbSYan, Zheng 			issued |= cap->issued;
56911df2dfbSYan, Zheng 			flags |= CEPH_CAP_FLAG_AUTH;
57011df2dfbSYan, Zheng 		}
57111df2dfbSYan, Zheng 	}
57211df2dfbSYan, Zheng 
573a8599bd8SSage Weil 	if (!ci->i_snap_realm) {
574a8599bd8SSage Weil 		/*
575a8599bd8SSage Weil 		 * add this inode to the appropriate snap realm
576a8599bd8SSage Weil 		 */
577a8599bd8SSage Weil 		struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
578a8599bd8SSage Weil 							       realmino);
579a8599bd8SSage Weil 		if (realm) {
580a8599bd8SSage Weil 			ceph_get_snap_realm(mdsc, realm);
581a8599bd8SSage Weil 			spin_lock(&realm->inodes_with_caps_lock);
582a8599bd8SSage Weil 			ci->i_snap_realm = realm;
583a8599bd8SSage Weil 			list_add(&ci->i_snap_realm_item,
584a8599bd8SSage Weil 				 &realm->inodes_with_caps);
585a8599bd8SSage Weil 			spin_unlock(&realm->inodes_with_caps_lock);
586a8599bd8SSage Weil 		} else {
587a8599bd8SSage Weil 			pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
588a8599bd8SSage Weil 			       realmino);
589b8cd07e7SSage Weil 			WARN_ON(!realm);
590a8599bd8SSage Weil 		}
591a8599bd8SSage Weil 	}
592a8599bd8SSage Weil 
593a8599bd8SSage Weil 	__check_cap_issue(ci, cap, issued);
594a8599bd8SSage Weil 
595a8599bd8SSage Weil 	/*
596a8599bd8SSage Weil 	 * If we are issued caps we don't want, or the mds' wanted
597a8599bd8SSage Weil 	 * value appears to be off, queue a check so we'll release
598a8599bd8SSage Weil 	 * later and/or update the mds wanted value.
599a8599bd8SSage Weil 	 */
600a8599bd8SSage Weil 	actual_wanted = __ceph_caps_wanted(ci);
601a8599bd8SSage Weil 	if ((wanted & ~actual_wanted) ||
602a8599bd8SSage Weil 	    (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
603a8599bd8SSage Weil 		dout(" issued %s, mds wanted %s, actual %s, queueing\n",
604a8599bd8SSage Weil 		     ceph_cap_string(issued), ceph_cap_string(wanted),
605a8599bd8SSage Weil 		     ceph_cap_string(actual_wanted));
606a8599bd8SSage Weil 		__cap_delay_requeue(mdsc, ci);
607a8599bd8SSage Weil 	}
608a8599bd8SSage Weil 
609b8c2f3aeSYan, Zheng 	if (flags & CEPH_CAP_FLAG_AUTH) {
610b8c2f3aeSYan, Zheng 		if (ci->i_auth_cap == NULL ||
611d9ffc4f7SYan, Zheng 		    ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
612a8599bd8SSage Weil 			ci->i_auth_cap = cap;
613d9ffc4f7SYan, Zheng 			cap->mds_wanted = wanted;
614d9ffc4f7SYan, Zheng 		}
61511df2dfbSYan, Zheng 	} else {
61611df2dfbSYan, Zheng 		WARN_ON(ci->i_auth_cap == cap);
6178a92a119SYan, Zheng 	}
618a8599bd8SSage Weil 
619a8599bd8SSage Weil 	dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
620a8599bd8SSage Weil 	     inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
621a8599bd8SSage Weil 	     ceph_cap_string(issued|cap->issued), seq, mds);
622a8599bd8SSage Weil 	cap->cap_id = cap_id;
623a8599bd8SSage Weil 	cap->issued = issued;
624a8599bd8SSage Weil 	cap->implemented |= issued;
625d1b87809SYan, Zheng 	if (ceph_seq_cmp(mseq, cap->mseq) > 0)
626964266ccSYan, Zheng 		cap->mds_wanted = wanted;
627964266ccSYan, Zheng 	else
628a8599bd8SSage Weil 		cap->mds_wanted |= wanted;
629a8599bd8SSage Weil 	cap->seq = seq;
630a8599bd8SSage Weil 	cap->issue_seq = seq;
631a8599bd8SSage Weil 	cap->mseq = mseq;
632685f9a5dSSage Weil 	cap->cap_gen = session->s_cap_gen;
633a8599bd8SSage Weil 
634a8599bd8SSage Weil 	if (fmode >= 0)
635a8599bd8SSage Weil 		__ceph_get_fmode(ci, fmode);
636a8599bd8SSage Weil }
637a8599bd8SSage Weil 
638a8599bd8SSage Weil /*
639a8599bd8SSage Weil  * Return true if cap has not timed out and belongs to the current
640a8599bd8SSage Weil  * generation of the MDS session (i.e. has not gone 'stale' due to
641a8599bd8SSage Weil  * us losing touch with the mds).
642a8599bd8SSage Weil  */
643a8599bd8SSage Weil static int __cap_is_valid(struct ceph_cap *cap)
644a8599bd8SSage Weil {
645a8599bd8SSage Weil 	unsigned long ttl;
646cdac8303SSage Weil 	u32 gen;
647a8599bd8SSage Weil 
648d8fb02abSAlex Elder 	spin_lock(&cap->session->s_gen_ttl_lock);
649a8599bd8SSage Weil 	gen = cap->session->s_cap_gen;
650a8599bd8SSage Weil 	ttl = cap->session->s_cap_ttl;
651d8fb02abSAlex Elder 	spin_unlock(&cap->session->s_gen_ttl_lock);
652a8599bd8SSage Weil 
653685f9a5dSSage Weil 	if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
654a8599bd8SSage Weil 		dout("__cap_is_valid %p cap %p issued %s "
655a8599bd8SSage Weil 		     "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
656685f9a5dSSage Weil 		     cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
657a8599bd8SSage Weil 		return 0;
658a8599bd8SSage Weil 	}
659a8599bd8SSage Weil 
660a8599bd8SSage Weil 	return 1;
661a8599bd8SSage Weil }
662a8599bd8SSage Weil 
663a8599bd8SSage Weil /*
664a8599bd8SSage Weil  * Return set of valid cap bits issued to us.  Note that caps time
665a8599bd8SSage Weil  * out, and may be invalidated in bulk if the client session times out
666a8599bd8SSage Weil  * and session->s_cap_gen is bumped.
667a8599bd8SSage Weil  */
668a8599bd8SSage Weil int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
669a8599bd8SSage Weil {
670d9df2783SYan, Zheng 	int have = ci->i_snap_caps;
671a8599bd8SSage Weil 	struct ceph_cap *cap;
672a8599bd8SSage Weil 	struct rb_node *p;
673a8599bd8SSage Weil 
674a8599bd8SSage Weil 	if (implemented)
675a8599bd8SSage Weil 		*implemented = 0;
676a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
677a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
678a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
679a8599bd8SSage Weil 			continue;
680a8599bd8SSage Weil 		dout("__ceph_caps_issued %p cap %p issued %s\n",
681a8599bd8SSage Weil 		     &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
682a8599bd8SSage Weil 		have |= cap->issued;
683a8599bd8SSage Weil 		if (implemented)
684a8599bd8SSage Weil 			*implemented |= cap->implemented;
685a8599bd8SSage Weil 	}
686b1530f57SYan, Zheng 	/*
687b1530f57SYan, Zheng 	 * exclude caps issued by non-auth MDS, but are been revoking
688b1530f57SYan, Zheng 	 * by the auth MDS. The non-auth MDS should be revoking/exporting
689b1530f57SYan, Zheng 	 * these caps, but the message is delayed.
690b1530f57SYan, Zheng 	 */
691b1530f57SYan, Zheng 	if (ci->i_auth_cap) {
692b1530f57SYan, Zheng 		cap = ci->i_auth_cap;
693b1530f57SYan, Zheng 		have &= ~cap->implemented | cap->issued;
694b1530f57SYan, Zheng 	}
695a8599bd8SSage Weil 	return have;
696a8599bd8SSage Weil }
697a8599bd8SSage Weil 
698a8599bd8SSage Weil /*
699a8599bd8SSage Weil  * Get cap bits issued by caps other than @ocap
700a8599bd8SSage Weil  */
701a8599bd8SSage Weil int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
702a8599bd8SSage Weil {
703a8599bd8SSage Weil 	int have = ci->i_snap_caps;
704a8599bd8SSage Weil 	struct ceph_cap *cap;
705a8599bd8SSage Weil 	struct rb_node *p;
706a8599bd8SSage Weil 
707a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
708a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
709a8599bd8SSage Weil 		if (cap == ocap)
710a8599bd8SSage Weil 			continue;
711a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
712a8599bd8SSage Weil 			continue;
713a8599bd8SSage Weil 		have |= cap->issued;
714a8599bd8SSage Weil 	}
715a8599bd8SSage Weil 	return have;
716a8599bd8SSage Weil }
717a8599bd8SSage Weil 
718a8599bd8SSage Weil /*
719a8599bd8SSage Weil  * Move a cap to the end of the LRU (oldest caps at list head, newest
720a8599bd8SSage Weil  * at list tail).
721a8599bd8SSage Weil  */
722a8599bd8SSage Weil static void __touch_cap(struct ceph_cap *cap)
723a8599bd8SSage Weil {
724a8599bd8SSage Weil 	struct ceph_mds_session *s = cap->session;
725a8599bd8SSage Weil 
7265dacf091SSage Weil 	spin_lock(&s->s_cap_lock);
7277c1332b8SSage Weil 	if (s->s_cap_iterator == NULL) {
728a8599bd8SSage Weil 		dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
729a8599bd8SSage Weil 		     s->s_mds);
730a8599bd8SSage Weil 		list_move_tail(&cap->session_caps, &s->s_caps);
7315dacf091SSage Weil 	} else {
7325dacf091SSage Weil 		dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
7335dacf091SSage Weil 		     &cap->ci->vfs_inode, cap, s->s_mds);
7345dacf091SSage Weil 	}
735a8599bd8SSage Weil 	spin_unlock(&s->s_cap_lock);
736a8599bd8SSage Weil }
737a8599bd8SSage Weil 
738a8599bd8SSage Weil /*
739a8599bd8SSage Weil  * Check if we hold the given mask.  If so, move the cap(s) to the
740a8599bd8SSage Weil  * front of their respective LRUs.  (This is the preferred way for
741a8599bd8SSage Weil  * callers to check for caps they want.)
742a8599bd8SSage Weil  */
743a8599bd8SSage Weil int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
744a8599bd8SSage Weil {
745a8599bd8SSage Weil 	struct ceph_cap *cap;
746a8599bd8SSage Weil 	struct rb_node *p;
747a8599bd8SSage Weil 	int have = ci->i_snap_caps;
748a8599bd8SSage Weil 
749a8599bd8SSage Weil 	if ((have & mask) == mask) {
750a8599bd8SSage Weil 		dout("__ceph_caps_issued_mask %p snap issued %s"
751a8599bd8SSage Weil 		     " (mask %s)\n", &ci->vfs_inode,
752a8599bd8SSage Weil 		     ceph_cap_string(have),
753a8599bd8SSage Weil 		     ceph_cap_string(mask));
754a8599bd8SSage Weil 		return 1;
755a8599bd8SSage Weil 	}
756a8599bd8SSage Weil 
757a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
758a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
759a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
760a8599bd8SSage Weil 			continue;
761a8599bd8SSage Weil 		if ((cap->issued & mask) == mask) {
762a8599bd8SSage Weil 			dout("__ceph_caps_issued_mask %p cap %p issued %s"
763a8599bd8SSage Weil 			     " (mask %s)\n", &ci->vfs_inode, cap,
764a8599bd8SSage Weil 			     ceph_cap_string(cap->issued),
765a8599bd8SSage Weil 			     ceph_cap_string(mask));
766a8599bd8SSage Weil 			if (touch)
767a8599bd8SSage Weil 				__touch_cap(cap);
768a8599bd8SSage Weil 			return 1;
769a8599bd8SSage Weil 		}
770a8599bd8SSage Weil 
771a8599bd8SSage Weil 		/* does a combination of caps satisfy mask? */
772a8599bd8SSage Weil 		have |= cap->issued;
773a8599bd8SSage Weil 		if ((have & mask) == mask) {
774a8599bd8SSage Weil 			dout("__ceph_caps_issued_mask %p combo issued %s"
775a8599bd8SSage Weil 			     " (mask %s)\n", &ci->vfs_inode,
776a8599bd8SSage Weil 			     ceph_cap_string(cap->issued),
777a8599bd8SSage Weil 			     ceph_cap_string(mask));
778a8599bd8SSage Weil 			if (touch) {
779a8599bd8SSage Weil 				struct rb_node *q;
780a8599bd8SSage Weil 
78125985edcSLucas De Marchi 				/* touch this + preceding caps */
782a8599bd8SSage Weil 				__touch_cap(cap);
783a8599bd8SSage Weil 				for (q = rb_first(&ci->i_caps); q != p;
784a8599bd8SSage Weil 				     q = rb_next(q)) {
785a8599bd8SSage Weil 					cap = rb_entry(q, struct ceph_cap,
786a8599bd8SSage Weil 						       ci_node);
787a8599bd8SSage Weil 					if (!__cap_is_valid(cap))
788a8599bd8SSage Weil 						continue;
789a8599bd8SSage Weil 					__touch_cap(cap);
790a8599bd8SSage Weil 				}
791a8599bd8SSage Weil 			}
792a8599bd8SSage Weil 			return 1;
793a8599bd8SSage Weil 		}
794a8599bd8SSage Weil 	}
795a8599bd8SSage Weil 
796a8599bd8SSage Weil 	return 0;
797a8599bd8SSage Weil }
798a8599bd8SSage Weil 
799a8599bd8SSage Weil /*
800a8599bd8SSage Weil  * Return true if mask caps are currently being revoked by an MDS.
801a8599bd8SSage Weil  */
8026ee6b953SYan, Zheng int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
8036ee6b953SYan, Zheng 			       struct ceph_cap *ocap, int mask)
8046ee6b953SYan, Zheng {
8056ee6b953SYan, Zheng 	struct ceph_cap *cap;
8066ee6b953SYan, Zheng 	struct rb_node *p;
8076ee6b953SYan, Zheng 
8086ee6b953SYan, Zheng 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
8096ee6b953SYan, Zheng 		cap = rb_entry(p, struct ceph_cap, ci_node);
8109563f88cSYan, Zheng 		if (cap != ocap &&
8116ee6b953SYan, Zheng 		    (cap->implemented & ~cap->issued & mask))
8126ee6b953SYan, Zheng 			return 1;
8136ee6b953SYan, Zheng 	}
8146ee6b953SYan, Zheng 	return 0;
8156ee6b953SYan, Zheng }
8166ee6b953SYan, Zheng 
817a8599bd8SSage Weil int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
818a8599bd8SSage Weil {
819a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
8206ee6b953SYan, Zheng 	int ret;
821a8599bd8SSage Weil 
822be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
8236ee6b953SYan, Zheng 	ret = __ceph_caps_revoking_other(ci, NULL, mask);
824be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
825a8599bd8SSage Weil 	dout("ceph_caps_revoking %p %s = %d\n", inode,
826a8599bd8SSage Weil 	     ceph_cap_string(mask), ret);
827a8599bd8SSage Weil 	return ret;
828a8599bd8SSage Weil }
829a8599bd8SSage Weil 
830a8599bd8SSage Weil int __ceph_caps_used(struct ceph_inode_info *ci)
831a8599bd8SSage Weil {
832a8599bd8SSage Weil 	int used = 0;
833a8599bd8SSage Weil 	if (ci->i_pin_ref)
834a8599bd8SSage Weil 		used |= CEPH_CAP_PIN;
835a8599bd8SSage Weil 	if (ci->i_rd_ref)
836a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_RD;
837a43fb731SSage Weil 	if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages)
838a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_CACHE;
839a8599bd8SSage Weil 	if (ci->i_wr_ref)
840a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_WR;
841d3d0720dSHenry C Chang 	if (ci->i_wb_ref || ci->i_wrbuffer_ref)
842a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_BUFFER;
843a8599bd8SSage Weil 	return used;
844a8599bd8SSage Weil }
845a8599bd8SSage Weil 
846a8599bd8SSage Weil /*
847a8599bd8SSage Weil  * wanted, by virtue of open file modes
848a8599bd8SSage Weil  */
849a8599bd8SSage Weil int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
850a8599bd8SSage Weil {
851a8599bd8SSage Weil 	int want = 0;
852a8599bd8SSage Weil 	int mode;
85333caad32SSage Weil 	for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
854a8599bd8SSage Weil 		if (ci->i_nr_by_mode[mode])
855a8599bd8SSage Weil 			want |= ceph_caps_for_mode(mode);
856a8599bd8SSage Weil 	return want;
857a8599bd8SSage Weil }
858a8599bd8SSage Weil 
859a8599bd8SSage Weil /*
860a8599bd8SSage Weil  * Return caps we have registered with the MDS(s) as 'wanted'.
861a8599bd8SSage Weil  */
862a8599bd8SSage Weil int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
863a8599bd8SSage Weil {
864a8599bd8SSage Weil 	struct ceph_cap *cap;
865a8599bd8SSage Weil 	struct rb_node *p;
866a8599bd8SSage Weil 	int mds_wanted = 0;
867a8599bd8SSage Weil 
868a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
869a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
870a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
871a8599bd8SSage Weil 			continue;
872a2550604SYan, Zheng 		if (cap == ci->i_auth_cap)
873a8599bd8SSage Weil 			mds_wanted |= cap->mds_wanted;
874a2550604SYan, Zheng 		else
875a2550604SYan, Zheng 			mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
876a8599bd8SSage Weil 	}
877a8599bd8SSage Weil 	return mds_wanted;
878a8599bd8SSage Weil }
879a8599bd8SSage Weil 
880a8599bd8SSage Weil /*
881be655596SSage Weil  * called under i_ceph_lock
882a8599bd8SSage Weil  */
883a8599bd8SSage Weil static int __ceph_is_any_caps(struct ceph_inode_info *ci)
884a8599bd8SSage Weil {
885d9df2783SYan, Zheng 	return !RB_EMPTY_ROOT(&ci->i_caps);
886a8599bd8SSage Weil }
887a8599bd8SSage Weil 
8889215aeeaSYan, Zheng int ceph_is_any_caps(struct inode *inode)
8899215aeeaSYan, Zheng {
8909215aeeaSYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
8919215aeeaSYan, Zheng 	int ret;
8929215aeeaSYan, Zheng 
8939215aeeaSYan, Zheng 	spin_lock(&ci->i_ceph_lock);
8949215aeeaSYan, Zheng 	ret = __ceph_is_any_caps(ci);
8959215aeeaSYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
8969215aeeaSYan, Zheng 
8979215aeeaSYan, Zheng 	return ret;
8989215aeeaSYan, Zheng }
8999215aeeaSYan, Zheng 
900a8599bd8SSage Weil /*
901f818a736SSage Weil  * Remove a cap.  Take steps to deal with a racing iterate_session_caps.
902f818a736SSage Weil  *
903be655596SSage Weil  * caller should hold i_ceph_lock.
904a6369741SSage Weil  * caller will not hold session s_mutex if called from destroy_inode.
905a8599bd8SSage Weil  */
906a096b09aSYan, Zheng void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
907a8599bd8SSage Weil {
908a8599bd8SSage Weil 	struct ceph_mds_session *session = cap->session;
909a8599bd8SSage Weil 	struct ceph_inode_info *ci = cap->ci;
910640ef79dSCheng Renquan 	struct ceph_mds_client *mdsc =
9113d14c5d2SYehuda Sadeh 		ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
912f818a736SSage Weil 	int removed = 0;
913a8599bd8SSage Weil 
914a8599bd8SSage Weil 	dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
915a8599bd8SSage Weil 
9167c1332b8SSage Weil 	/* remove from session list */
9177c1332b8SSage Weil 	spin_lock(&session->s_cap_lock);
91899a9c273SYan, Zheng 	/*
91999a9c273SYan, Zheng 	 * s_cap_reconnect is protected by s_cap_lock. no one changes
92099a9c273SYan, Zheng 	 * s_cap_gen while session is in the reconnect state.
92199a9c273SYan, Zheng 	 */
92299a9c273SYan, Zheng 	if (queue_release &&
92399a9c273SYan, Zheng 	    (!session->s_cap_reconnect ||
92499a9c273SYan, Zheng 	     cap->cap_gen == session->s_cap_gen))
925a096b09aSYan, Zheng 		__queue_cap_release(session, ci->i_vino.ino, cap->cap_id,
926a096b09aSYan, Zheng 				    cap->mseq, cap->issue_seq);
927a096b09aSYan, Zheng 
9287c1332b8SSage Weil 	if (session->s_cap_iterator == cap) {
9297c1332b8SSage Weil 		/* not yet, we are iterating over this very cap */
9307c1332b8SSage Weil 		dout("__ceph_remove_cap  delaying %p removal from session %p\n",
9317c1332b8SSage Weil 		     cap, cap->session);
9327c1332b8SSage Weil 	} else {
9337c1332b8SSage Weil 		list_del_init(&cap->session_caps);
9347c1332b8SSage Weil 		session->s_nr_caps--;
9357c1332b8SSage Weil 		cap->session = NULL;
936f818a736SSage Weil 		removed = 1;
9377c1332b8SSage Weil 	}
938f818a736SSage Weil 	/* protect backpointer with s_cap_lock: see iterate_session_caps */
939f818a736SSage Weil 	cap->ci = NULL;
9407c1332b8SSage Weil 	spin_unlock(&session->s_cap_lock);
9417c1332b8SSage Weil 
942f818a736SSage Weil 	/* remove from inode list */
943f818a736SSage Weil 	rb_erase(&cap->ci_node, &ci->i_caps);
944f818a736SSage Weil 	if (ci->i_auth_cap == cap)
945f818a736SSage Weil 		ci->i_auth_cap = NULL;
946f818a736SSage Weil 
947f818a736SSage Weil 	if (removed)
94837151668SYehuda Sadeh 		ceph_put_cap(mdsc, cap);
949a8599bd8SSage Weil 
950a8599bd8SSage Weil 	if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
951a8599bd8SSage Weil 		struct ceph_snap_realm *realm = ci->i_snap_realm;
952a8599bd8SSage Weil 		spin_lock(&realm->inodes_with_caps_lock);
953a8599bd8SSage Weil 		list_del_init(&ci->i_snap_realm_item);
954a8599bd8SSage Weil 		ci->i_snap_realm_counter++;
955a8599bd8SSage Weil 		ci->i_snap_realm = NULL;
956a8599bd8SSage Weil 		spin_unlock(&realm->inodes_with_caps_lock);
957a8599bd8SSage Weil 		ceph_put_snap_realm(mdsc, realm);
958a8599bd8SSage Weil 	}
959a8599bd8SSage Weil 	if (!__ceph_is_any_real_caps(ci))
960a8599bd8SSage Weil 		__cap_delay_cancel(mdsc, ci);
961a8599bd8SSage Weil }
962a8599bd8SSage Weil 
963a8599bd8SSage Weil /*
964a8599bd8SSage Weil  * Build and send a cap message to the given MDS.
965a8599bd8SSage Weil  *
966a8599bd8SSage Weil  * Caller should be holding s_mutex.
967a8599bd8SSage Weil  */
968a8599bd8SSage Weil static int send_cap_msg(struct ceph_mds_session *session,
969a8599bd8SSage Weil 			u64 ino, u64 cid, int op,
970a8599bd8SSage Weil 			int caps, int wanted, int dirty,
971a8599bd8SSage Weil 			u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
972a8599bd8SSage Weil 			u64 size, u64 max_size,
973a8599bd8SSage Weil 			struct timespec *mtime, struct timespec *atime,
974a8599bd8SSage Weil 			u64 time_warp_seq,
97505cb11c1SEric W. Biederman 			kuid_t uid, kgid_t gid, umode_t mode,
976a8599bd8SSage Weil 			u64 xattr_version,
977a8599bd8SSage Weil 			struct ceph_buffer *xattrs_buf,
978a8599bd8SSage Weil 			u64 follows)
979a8599bd8SSage Weil {
980a8599bd8SSage Weil 	struct ceph_mds_caps *fc;
981a8599bd8SSage Weil 	struct ceph_msg *msg;
982a8599bd8SSage Weil 
983a8599bd8SSage Weil 	dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
984a8599bd8SSage Weil 	     " seq %u/%u mseq %u follows %lld size %llu/%llu"
985a8599bd8SSage Weil 	     " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
986a8599bd8SSage Weil 	     cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
987a8599bd8SSage Weil 	     ceph_cap_string(dirty),
988a8599bd8SSage Weil 	     seq, issue_seq, mseq, follows, size, max_size,
989a8599bd8SSage Weil 	     xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
990a8599bd8SSage Weil 
991b61c2763SSage Weil 	msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false);
992a79832f2SSage Weil 	if (!msg)
993a79832f2SSage Weil 		return -ENOMEM;
994a8599bd8SSage Weil 
9956df058c0SSage Weil 	msg->hdr.tid = cpu_to_le64(flush_tid);
996a8599bd8SSage Weil 
9976df058c0SSage Weil 	fc = msg->front.iov_base;
998a8599bd8SSage Weil 	memset(fc, 0, sizeof(*fc));
999a8599bd8SSage Weil 
1000a8599bd8SSage Weil 	fc->cap_id = cpu_to_le64(cid);
1001a8599bd8SSage Weil 	fc->op = cpu_to_le32(op);
1002a8599bd8SSage Weil 	fc->seq = cpu_to_le32(seq);
1003a8599bd8SSage Weil 	fc->issue_seq = cpu_to_le32(issue_seq);
1004a8599bd8SSage Weil 	fc->migrate_seq = cpu_to_le32(mseq);
1005a8599bd8SSage Weil 	fc->caps = cpu_to_le32(caps);
1006a8599bd8SSage Weil 	fc->wanted = cpu_to_le32(wanted);
1007a8599bd8SSage Weil 	fc->dirty = cpu_to_le32(dirty);
1008a8599bd8SSage Weil 	fc->ino = cpu_to_le64(ino);
1009a8599bd8SSage Weil 	fc->snap_follows = cpu_to_le64(follows);
1010a8599bd8SSage Weil 
1011a8599bd8SSage Weil 	fc->size = cpu_to_le64(size);
1012a8599bd8SSage Weil 	fc->max_size = cpu_to_le64(max_size);
1013a8599bd8SSage Weil 	if (mtime)
1014a8599bd8SSage Weil 		ceph_encode_timespec(&fc->mtime, mtime);
1015a8599bd8SSage Weil 	if (atime)
1016a8599bd8SSage Weil 		ceph_encode_timespec(&fc->atime, atime);
1017a8599bd8SSage Weil 	fc->time_warp_seq = cpu_to_le32(time_warp_seq);
1018a8599bd8SSage Weil 
101905cb11c1SEric W. Biederman 	fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid));
102005cb11c1SEric W. Biederman 	fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid));
1021a8599bd8SSage Weil 	fc->mode = cpu_to_le32(mode);
1022a8599bd8SSage Weil 
1023a8599bd8SSage Weil 	fc->xattr_version = cpu_to_le64(xattr_version);
1024a8599bd8SSage Weil 	if (xattrs_buf) {
1025a8599bd8SSage Weil 		msg->middle = ceph_buffer_get(xattrs_buf);
1026a8599bd8SSage Weil 		fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
1027a8599bd8SSage Weil 		msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
1028a8599bd8SSage Weil 	}
1029a8599bd8SSage Weil 
1030a8599bd8SSage Weil 	ceph_con_send(&session->s_con, msg);
1031a8599bd8SSage Weil 	return 0;
1032a8599bd8SSage Weil }
1033a8599bd8SSage Weil 
1034d40ee0dcSYan, Zheng void __queue_cap_release(struct ceph_mds_session *session,
10353d7ded4dSSage Weil 			 u64 ino, u64 cap_id, u32 migrate_seq,
10363d7ded4dSSage Weil 			 u32 issue_seq)
10373d7ded4dSSage Weil {
10383d7ded4dSSage Weil 	struct ceph_msg *msg;
10393d7ded4dSSage Weil 	struct ceph_mds_cap_release *head;
10403d7ded4dSSage Weil 	struct ceph_mds_cap_item *item;
10413d7ded4dSSage Weil 
10423d7ded4dSSage Weil 	BUG_ON(!session->s_num_cap_releases);
10433d7ded4dSSage Weil 	msg = list_first_entry(&session->s_cap_releases,
10443d7ded4dSSage Weil 			       struct ceph_msg, list_head);
10453d7ded4dSSage Weil 
10463d7ded4dSSage Weil 	dout(" adding %llx release to mds%d msg %p (%d left)\n",
10473d7ded4dSSage Weil 	     ino, session->s_mds, msg, session->s_num_cap_releases);
10483d7ded4dSSage Weil 
10493d7ded4dSSage Weil 	BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
10503d7ded4dSSage Weil 	head = msg->front.iov_base;
1051b905a7f8SWei Yongjun 	le32_add_cpu(&head->num, 1);
10523d7ded4dSSage Weil 	item = msg->front.iov_base + msg->front.iov_len;
10533d7ded4dSSage Weil 	item->ino = cpu_to_le64(ino);
10543d7ded4dSSage Weil 	item->cap_id = cpu_to_le64(cap_id);
10553d7ded4dSSage Weil 	item->migrate_seq = cpu_to_le32(migrate_seq);
10563d7ded4dSSage Weil 	item->seq = cpu_to_le32(issue_seq);
10573d7ded4dSSage Weil 
10583d7ded4dSSage Weil 	session->s_num_cap_releases--;
10593d7ded4dSSage Weil 
10603d7ded4dSSage Weil 	msg->front.iov_len += sizeof(*item);
10613d7ded4dSSage Weil 	if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
10623d7ded4dSSage Weil 		dout(" release msg %p full\n", msg);
10633d7ded4dSSage Weil 		list_move_tail(&msg->list_head, &session->s_cap_releases_done);
10643d7ded4dSSage Weil 	} else {
10653d7ded4dSSage Weil 		dout(" release msg %p at %d/%d (%d)\n", msg,
10663d7ded4dSSage Weil 		     (int)le32_to_cpu(head->num),
10673d7ded4dSSage Weil 		     (int)CEPH_CAPS_PER_RELEASE,
10683d7ded4dSSage Weil 		     (int)msg->front.iov_len);
10693d7ded4dSSage Weil 	}
10703d7ded4dSSage Weil }
10713d7ded4dSSage Weil 
1072a8599bd8SSage Weil /*
1073a6369741SSage Weil  * Queue cap releases when an inode is dropped from our cache.  Since
1074be655596SSage Weil  * inode is about to be destroyed, there is no need for i_ceph_lock.
1075a8599bd8SSage Weil  */
1076a8599bd8SSage Weil void ceph_queue_caps_release(struct inode *inode)
1077a8599bd8SSage Weil {
1078a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1079a8599bd8SSage Weil 	struct rb_node *p;
1080a8599bd8SSage Weil 
1081a8599bd8SSage Weil 	p = rb_first(&ci->i_caps);
1082a8599bd8SSage Weil 	while (p) {
1083a8599bd8SSage Weil 		struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1084a8599bd8SSage Weil 		p = rb_next(p);
1085a096b09aSYan, Zheng 		__ceph_remove_cap(cap, true);
1086a8599bd8SSage Weil 	}
1087a8599bd8SSage Weil }
1088a8599bd8SSage Weil 
1089a8599bd8SSage Weil /*
1090a8599bd8SSage Weil  * Send a cap msg on the given inode.  Update our caps state, then
1091be655596SSage Weil  * drop i_ceph_lock and send the message.
1092a8599bd8SSage Weil  *
1093a8599bd8SSage Weil  * Make note of max_size reported/requested from mds, revoked caps
1094a8599bd8SSage Weil  * that have now been implemented.
1095a8599bd8SSage Weil  *
1096a8599bd8SSage Weil  * Make half-hearted attempt ot to invalidate page cache if we are
1097a8599bd8SSage Weil  * dropping RDCACHE.  Note that this will leave behind locked pages
1098a8599bd8SSage Weil  * that we'll then need to deal with elsewhere.
1099a8599bd8SSage Weil  *
1100a8599bd8SSage Weil  * Return non-zero if delayed release, or we experienced an error
1101a8599bd8SSage Weil  * such that the caller should requeue + retry later.
1102a8599bd8SSage Weil  *
1103be655596SSage Weil  * called with i_ceph_lock, then drops it.
1104a8599bd8SSage Weil  * caller should hold snap_rwsem (read), s_mutex.
1105a8599bd8SSage Weil  */
1106a8599bd8SSage Weil static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1107a8599bd8SSage Weil 		      int op, int used, int want, int retain, int flushing,
1108a8599bd8SSage Weil 		      unsigned *pflush_tid)
1109be655596SSage Weil 	__releases(cap->ci->i_ceph_lock)
1110a8599bd8SSage Weil {
1111a8599bd8SSage Weil 	struct ceph_inode_info *ci = cap->ci;
1112a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
1113a8599bd8SSage Weil 	u64 cap_id = cap->cap_id;
111468c28323SSage Weil 	int held, revoking, dropping, keep;
1115a8599bd8SSage Weil 	u64 seq, issue_seq, mseq, time_warp_seq, follows;
1116a8599bd8SSage Weil 	u64 size, max_size;
1117a8599bd8SSage Weil 	struct timespec mtime, atime;
1118a8599bd8SSage Weil 	int wake = 0;
11195706b27dSAl Viro 	umode_t mode;
112005cb11c1SEric W. Biederman 	kuid_t uid;
112105cb11c1SEric W. Biederman 	kgid_t gid;
1122a8599bd8SSage Weil 	struct ceph_mds_session *session;
1123a8599bd8SSage Weil 	u64 xattr_version = 0;
1124082afec9SSage Weil 	struct ceph_buffer *xattr_blob = NULL;
1125a8599bd8SSage Weil 	int delayed = 0;
1126a8599bd8SSage Weil 	u64 flush_tid = 0;
1127a8599bd8SSage Weil 	int i;
1128a8599bd8SSage Weil 	int ret;
1129a8599bd8SSage Weil 
113068c28323SSage Weil 	held = cap->issued | cap->implemented;
113168c28323SSage Weil 	revoking = cap->implemented & ~cap->issued;
113268c28323SSage Weil 	retain &= ~revoking;
113368c28323SSage Weil 	dropping = cap->issued & ~retain;
113468c28323SSage Weil 
1135a8599bd8SSage Weil 	dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1136a8599bd8SSage Weil 	     inode, cap, cap->session,
1137a8599bd8SSage Weil 	     ceph_cap_string(held), ceph_cap_string(held & retain),
1138a8599bd8SSage Weil 	     ceph_cap_string(revoking));
1139a8599bd8SSage Weil 	BUG_ON((retain & CEPH_CAP_PIN) == 0);
1140a8599bd8SSage Weil 
1141a8599bd8SSage Weil 	session = cap->session;
1142a8599bd8SSage Weil 
1143a8599bd8SSage Weil 	/* don't release wanted unless we've waited a bit. */
1144a8599bd8SSage Weil 	if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1145a8599bd8SSage Weil 	    time_before(jiffies, ci->i_hold_caps_min)) {
1146a8599bd8SSage Weil 		dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1147a8599bd8SSage Weil 		     ceph_cap_string(cap->issued),
1148a8599bd8SSage Weil 		     ceph_cap_string(cap->issued & retain),
1149a8599bd8SSage Weil 		     ceph_cap_string(cap->mds_wanted),
1150a8599bd8SSage Weil 		     ceph_cap_string(want));
1151a8599bd8SSage Weil 		want |= cap->mds_wanted;
1152a8599bd8SSage Weil 		retain |= cap->issued;
1153a8599bd8SSage Weil 		delayed = 1;
1154a8599bd8SSage Weil 	}
1155a8599bd8SSage Weil 	ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1156a8599bd8SSage Weil 
1157a8599bd8SSage Weil 	cap->issued &= retain;  /* drop bits we don't want */
1158a8599bd8SSage Weil 	if (cap->implemented & ~cap->issued) {
1159a8599bd8SSage Weil 		/*
1160a8599bd8SSage Weil 		 * Wake up any waiters on wanted -> needed transition.
1161a8599bd8SSage Weil 		 * This is due to the weird transition from buffered
1162a8599bd8SSage Weil 		 * to sync IO... we need to flush dirty pages _before_
1163a8599bd8SSage Weil 		 * allowing sync writes to avoid reordering.
1164a8599bd8SSage Weil 		 */
1165a8599bd8SSage Weil 		wake = 1;
1166a8599bd8SSage Weil 	}
1167a8599bd8SSage Weil 	cap->implemented &= cap->issued | used;
1168a8599bd8SSage Weil 	cap->mds_wanted = want;
1169a8599bd8SSage Weil 
1170a8599bd8SSage Weil 	if (flushing) {
1171a8599bd8SSage Weil 		/*
1172a8599bd8SSage Weil 		 * assign a tid for flush operations so we can avoid
1173a8599bd8SSage Weil 		 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1174a8599bd8SSage Weil 		 * clean type races.  track latest tid for every bit
1175a8599bd8SSage Weil 		 * so we can handle flush AxFw, flush Fw, and have the
1176a8599bd8SSage Weil 		 * first ack clean Ax.
1177a8599bd8SSage Weil 		 */
1178a8599bd8SSage Weil 		flush_tid = ++ci->i_cap_flush_last_tid;
1179a8599bd8SSage Weil 		if (pflush_tid)
1180a8599bd8SSage Weil 			*pflush_tid = flush_tid;
1181a8599bd8SSage Weil 		dout(" cap_flush_tid %d\n", (int)flush_tid);
1182a8599bd8SSage Weil 		for (i = 0; i < CEPH_CAP_BITS; i++)
1183a8599bd8SSage Weil 			if (flushing & (1 << i))
1184a8599bd8SSage Weil 				ci->i_cap_flush_tid[i] = flush_tid;
11857d8cb26dSSage Weil 
11867d8cb26dSSage Weil 		follows = ci->i_head_snapc->seq;
11877d8cb26dSSage Weil 	} else {
11887d8cb26dSSage Weil 		follows = 0;
1189a8599bd8SSage Weil 	}
1190a8599bd8SSage Weil 
1191a8599bd8SSage Weil 	keep = cap->implemented;
1192a8599bd8SSage Weil 	seq = cap->seq;
1193a8599bd8SSage Weil 	issue_seq = cap->issue_seq;
1194a8599bd8SSage Weil 	mseq = cap->mseq;
1195a8599bd8SSage Weil 	size = inode->i_size;
1196a8599bd8SSage Weil 	ci->i_reported_size = size;
1197a8599bd8SSage Weil 	max_size = ci->i_wanted_max_size;
1198a8599bd8SSage Weil 	ci->i_requested_max_size = max_size;
1199a8599bd8SSage Weil 	mtime = inode->i_mtime;
1200a8599bd8SSage Weil 	atime = inode->i_atime;
1201a8599bd8SSage Weil 	time_warp_seq = ci->i_time_warp_seq;
1202a8599bd8SSage Weil 	uid = inode->i_uid;
1203a8599bd8SSage Weil 	gid = inode->i_gid;
1204a8599bd8SSage Weil 	mode = inode->i_mode;
1205a8599bd8SSage Weil 
1206082afec9SSage Weil 	if (flushing & CEPH_CAP_XATTR_EXCL) {
1207a8599bd8SSage Weil 		__ceph_build_xattrs_blob(ci);
1208082afec9SSage Weil 		xattr_blob = ci->i_xattrs.blob;
1209082afec9SSage Weil 		xattr_version = ci->i_xattrs.version;
1210a8599bd8SSage Weil 	}
1211a8599bd8SSage Weil 
1212be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1213a8599bd8SSage Weil 
1214a8599bd8SSage Weil 	ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1215a8599bd8SSage Weil 		op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1216a8599bd8SSage Weil 		size, max_size, &mtime, &atime, time_warp_seq,
1217082afec9SSage Weil 		uid, gid, mode, xattr_version, xattr_blob,
1218a8599bd8SSage Weil 		follows);
1219a8599bd8SSage Weil 	if (ret < 0) {
1220a8599bd8SSage Weil 		dout("error sending cap msg, must requeue %p\n", inode);
1221a8599bd8SSage Weil 		delayed = 1;
1222a8599bd8SSage Weil 	}
1223a8599bd8SSage Weil 
1224a8599bd8SSage Weil 	if (wake)
122503066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
1226a8599bd8SSage Weil 
1227a8599bd8SSage Weil 	return delayed;
1228a8599bd8SSage Weil }
1229a8599bd8SSage Weil 
1230a8599bd8SSage Weil /*
1231a8599bd8SSage Weil  * When a snapshot is taken, clients accumulate dirty metadata on
1232a8599bd8SSage Weil  * inodes with capabilities in ceph_cap_snaps to describe the file
1233a8599bd8SSage Weil  * state at the time the snapshot was taken.  This must be flushed
1234a8599bd8SSage Weil  * asynchronously back to the MDS once sync writes complete and dirty
1235a8599bd8SSage Weil  * data is written out.
1236a8599bd8SSage Weil  *
1237e835124cSSage Weil  * Unless @again is true, skip cap_snaps that were already sent to
1238e835124cSSage Weil  * the MDS (i.e., during this session).
1239e835124cSSage Weil  *
1240be655596SSage Weil  * Called under i_ceph_lock.  Takes s_mutex as needed.
1241a8599bd8SSage Weil  */
1242a8599bd8SSage Weil void __ceph_flush_snaps(struct ceph_inode_info *ci,
1243e835124cSSage Weil 			struct ceph_mds_session **psession,
1244e835124cSSage Weil 			int again)
1245be655596SSage Weil 		__releases(ci->i_ceph_lock)
1246be655596SSage Weil 		__acquires(ci->i_ceph_lock)
1247a8599bd8SSage Weil {
1248a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
1249a8599bd8SSage Weil 	int mds;
1250a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
1251a8599bd8SSage Weil 	u32 mseq;
12523d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1253a8599bd8SSage Weil 	struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1254a8599bd8SSage Weil 						    session->s_mutex */
1255a8599bd8SSage Weil 	u64 next_follows = 0;  /* keep track of how far we've gotten through the
1256a8599bd8SSage Weil 			     i_cap_snaps list, and skip these entries next time
1257a8599bd8SSage Weil 			     around to avoid an infinite loop */
1258a8599bd8SSage Weil 
1259a8599bd8SSage Weil 	if (psession)
1260a8599bd8SSage Weil 		session = *psession;
1261a8599bd8SSage Weil 
1262a8599bd8SSage Weil 	dout("__flush_snaps %p\n", inode);
1263a8599bd8SSage Weil retry:
1264a8599bd8SSage Weil 	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1265a8599bd8SSage Weil 		/* avoid an infiniute loop after retry */
1266a8599bd8SSage Weil 		if (capsnap->follows < next_follows)
1267a8599bd8SSage Weil 			continue;
1268a8599bd8SSage Weil 		/*
1269a8599bd8SSage Weil 		 * we need to wait for sync writes to complete and for dirty
1270a8599bd8SSage Weil 		 * pages to be written out.
1271a8599bd8SSage Weil 		 */
1272a8599bd8SSage Weil 		if (capsnap->dirty_pages || capsnap->writing)
1273cfc0bf66SSage Weil 			break;
1274a8599bd8SSage Weil 
1275819ccbfaSSage Weil 		/*
1276819ccbfaSSage Weil 		 * if cap writeback already occurred, we should have dropped
1277819ccbfaSSage Weil 		 * the capsnap in ceph_put_wrbuffer_cap_refs.
1278819ccbfaSSage Weil 		 */
1279819ccbfaSSage Weil 		BUG_ON(capsnap->dirty == 0);
1280819ccbfaSSage Weil 
1281a8599bd8SSage Weil 		/* pick mds, take s_mutex */
1282ca81f3f6SSage Weil 		if (ci->i_auth_cap == NULL) {
1283ca81f3f6SSage Weil 			dout("no auth cap (migrating?), doing nothing\n");
1284ca81f3f6SSage Weil 			goto out;
1285ca81f3f6SSage Weil 		}
1286e835124cSSage Weil 
1287e835124cSSage Weil 		/* only flush each capsnap once */
1288e835124cSSage Weil 		if (!again && !list_empty(&capsnap->flushing_item)) {
1289e835124cSSage Weil 			dout("already flushed %p, skipping\n", capsnap);
1290e835124cSSage Weil 			continue;
1291e835124cSSage Weil 		}
1292e835124cSSage Weil 
1293ca81f3f6SSage Weil 		mds = ci->i_auth_cap->session->s_mds;
1294ca81f3f6SSage Weil 		mseq = ci->i_auth_cap->mseq;
1295ca81f3f6SSage Weil 
1296a8599bd8SSage Weil 		if (session && session->s_mds != mds) {
1297a8599bd8SSage Weil 			dout("oops, wrong session %p mutex\n", session);
1298a8599bd8SSage Weil 			mutex_unlock(&session->s_mutex);
1299a8599bd8SSage Weil 			ceph_put_mds_session(session);
1300a8599bd8SSage Weil 			session = NULL;
1301a8599bd8SSage Weil 		}
1302a8599bd8SSage Weil 		if (!session) {
1303be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
1304a8599bd8SSage Weil 			mutex_lock(&mdsc->mutex);
1305a8599bd8SSage Weil 			session = __ceph_lookup_mds_session(mdsc, mds);
1306a8599bd8SSage Weil 			mutex_unlock(&mdsc->mutex);
1307a8599bd8SSage Weil 			if (session) {
1308a8599bd8SSage Weil 				dout("inverting session/ino locks on %p\n",
1309a8599bd8SSage Weil 				     session);
1310a8599bd8SSage Weil 				mutex_lock(&session->s_mutex);
1311a8599bd8SSage Weil 			}
1312a8599bd8SSage Weil 			/*
1313a8599bd8SSage Weil 			 * if session == NULL, we raced against a cap
1314ca81f3f6SSage Weil 			 * deletion or migration.  retry, and we'll
1315ca81f3f6SSage Weil 			 * get a better @mds value next time.
1316a8599bd8SSage Weil 			 */
1317be655596SSage Weil 			spin_lock(&ci->i_ceph_lock);
1318a8599bd8SSage Weil 			goto retry;
1319a8599bd8SSage Weil 		}
1320a8599bd8SSage Weil 
1321a8599bd8SSage Weil 		capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1322a8599bd8SSage Weil 		atomic_inc(&capsnap->nref);
1323a8599bd8SSage Weil 		if (!list_empty(&capsnap->flushing_item))
1324a8599bd8SSage Weil 			list_del_init(&capsnap->flushing_item);
1325a8599bd8SSage Weil 		list_add_tail(&capsnap->flushing_item,
1326a8599bd8SSage Weil 			      &session->s_cap_snaps_flushing);
1327be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1328a8599bd8SSage Weil 
1329cfc0bf66SSage Weil 		dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
1330cfc0bf66SSage Weil 		     inode, capsnap, capsnap->follows, capsnap->flush_tid);
1331a8599bd8SSage Weil 		send_cap_msg(session, ceph_vino(inode).ino, 0,
1332a8599bd8SSage Weil 			     CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1333a8599bd8SSage Weil 			     capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1334a8599bd8SSage Weil 			     capsnap->size, 0,
1335a8599bd8SSage Weil 			     &capsnap->mtime, &capsnap->atime,
1336a8599bd8SSage Weil 			     capsnap->time_warp_seq,
1337a8599bd8SSage Weil 			     capsnap->uid, capsnap->gid, capsnap->mode,
13384a625be4SSage Weil 			     capsnap->xattr_version, capsnap->xattr_blob,
1339a8599bd8SSage Weil 			     capsnap->follows);
1340a8599bd8SSage Weil 
1341a8599bd8SSage Weil 		next_follows = capsnap->follows + 1;
1342a8599bd8SSage Weil 		ceph_put_cap_snap(capsnap);
1343a8599bd8SSage Weil 
1344be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1345a8599bd8SSage Weil 		goto retry;
1346a8599bd8SSage Weil 	}
1347a8599bd8SSage Weil 
1348a8599bd8SSage Weil 	/* we flushed them all; remove this inode from the queue */
1349a8599bd8SSage Weil 	spin_lock(&mdsc->snap_flush_lock);
1350a8599bd8SSage Weil 	list_del_init(&ci->i_snap_flush_item);
1351a8599bd8SSage Weil 	spin_unlock(&mdsc->snap_flush_lock);
1352a8599bd8SSage Weil 
1353ca81f3f6SSage Weil out:
1354a8599bd8SSage Weil 	if (psession)
1355a8599bd8SSage Weil 		*psession = session;
1356a8599bd8SSage Weil 	else if (session) {
1357a8599bd8SSage Weil 		mutex_unlock(&session->s_mutex);
1358a8599bd8SSage Weil 		ceph_put_mds_session(session);
1359a8599bd8SSage Weil 	}
1360a8599bd8SSage Weil }
1361a8599bd8SSage Weil 
1362a8599bd8SSage Weil static void ceph_flush_snaps(struct ceph_inode_info *ci)
1363a8599bd8SSage Weil {
1364be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1365e835124cSSage Weil 	__ceph_flush_snaps(ci, NULL, 0);
1366be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1367a8599bd8SSage Weil }
1368a8599bd8SSage Weil 
1369a8599bd8SSage Weil /*
1370fca65b4aSSage Weil  * Mark caps dirty.  If inode is newly dirty, return the dirty flags.
1371fca65b4aSSage Weil  * Caller is then responsible for calling __mark_inode_dirty with the
1372fca65b4aSSage Weil  * returned flags value.
137376e3b390SSage Weil  */
1374fca65b4aSSage Weil int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
137576e3b390SSage Weil {
1376640ef79dSCheng Renquan 	struct ceph_mds_client *mdsc =
13773d14c5d2SYehuda Sadeh 		ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
137876e3b390SSage Weil 	struct inode *inode = &ci->vfs_inode;
137976e3b390SSage Weil 	int was = ci->i_dirty_caps;
138076e3b390SSage Weil 	int dirty = 0;
138176e3b390SSage Weil 
138276e3b390SSage Weil 	dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
138376e3b390SSage Weil 	     ceph_cap_string(mask), ceph_cap_string(was),
138476e3b390SSage Weil 	     ceph_cap_string(was | mask));
138576e3b390SSage Weil 	ci->i_dirty_caps |= mask;
138676e3b390SSage Weil 	if (was == 0) {
13877d8cb26dSSage Weil 		if (!ci->i_head_snapc)
13887d8cb26dSSage Weil 			ci->i_head_snapc = ceph_get_snap_context(
13897d8cb26dSSage Weil 				ci->i_snap_realm->cached_context);
13900685235fSYan, Zheng 		dout(" inode %p now dirty snapc %p auth cap %p\n",
13910685235fSYan, Zheng 		     &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
139211df2dfbSYan, Zheng 		WARN_ON(!ci->i_auth_cap);
139376e3b390SSage Weil 		BUG_ON(!list_empty(&ci->i_dirty_item));
139476e3b390SSage Weil 		spin_lock(&mdsc->cap_dirty_lock);
139576e3b390SSage Weil 		list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
139676e3b390SSage Weil 		spin_unlock(&mdsc->cap_dirty_lock);
139776e3b390SSage Weil 		if (ci->i_flushing_caps == 0) {
13983772d26dSSage Weil 			ihold(inode);
139976e3b390SSage Weil 			dirty |= I_DIRTY_SYNC;
140076e3b390SSage Weil 		}
140176e3b390SSage Weil 	}
140276e3b390SSage Weil 	BUG_ON(list_empty(&ci->i_dirty_item));
140376e3b390SSage Weil 	if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
140476e3b390SSage Weil 	    (mask & CEPH_CAP_FILE_BUFFER))
140576e3b390SSage Weil 		dirty |= I_DIRTY_DATASYNC;
140676e3b390SSage Weil 	__cap_delay_requeue(mdsc, ci);
1407fca65b4aSSage Weil 	return dirty;
140876e3b390SSage Weil }
140976e3b390SSage Weil 
141076e3b390SSage Weil /*
1411a8599bd8SSage Weil  * Add dirty inode to the flushing list.  Assigned a seq number so we
1412a8599bd8SSage Weil  * can wait for caps to flush without starving.
1413cdc35f96SSage Weil  *
1414be655596SSage Weil  * Called under i_ceph_lock.
1415a8599bd8SSage Weil  */
1416cdc35f96SSage Weil static int __mark_caps_flushing(struct inode *inode,
1417a8599bd8SSage Weil 				 struct ceph_mds_session *session)
1418a8599bd8SSage Weil {
14193d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1420a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1421cdc35f96SSage Weil 	int flushing;
1422a8599bd8SSage Weil 
1423cdc35f96SSage Weil 	BUG_ON(ci->i_dirty_caps == 0);
1424a8599bd8SSage Weil 	BUG_ON(list_empty(&ci->i_dirty_item));
1425cdc35f96SSage Weil 
1426cdc35f96SSage Weil 	flushing = ci->i_dirty_caps;
1427cdc35f96SSage Weil 	dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1428cdc35f96SSage Weil 	     ceph_cap_string(flushing),
1429cdc35f96SSage Weil 	     ceph_cap_string(ci->i_flushing_caps),
1430cdc35f96SSage Weil 	     ceph_cap_string(ci->i_flushing_caps | flushing));
1431cdc35f96SSage Weil 	ci->i_flushing_caps |= flushing;
1432cdc35f96SSage Weil 	ci->i_dirty_caps = 0;
1433afcdaea3SSage Weil 	dout(" inode %p now !dirty\n", inode);
1434cdc35f96SSage Weil 
1435a8599bd8SSage Weil 	spin_lock(&mdsc->cap_dirty_lock);
1436cdc35f96SSage Weil 	list_del_init(&ci->i_dirty_item);
1437afcdaea3SSage Weil 
1438afcdaea3SSage Weil 	ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
1439afcdaea3SSage Weil 	if (list_empty(&ci->i_flushing_item)) {
1440a8599bd8SSage Weil 		list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1441a8599bd8SSage Weil 		mdsc->num_cap_flushing++;
1442afcdaea3SSage Weil 		dout(" inode %p now flushing seq %lld\n", inode,
1443afcdaea3SSage Weil 		     ci->i_cap_flush_seq);
1444afcdaea3SSage Weil 	} else {
1445afcdaea3SSage Weil 		list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1446afcdaea3SSage Weil 		dout(" inode %p now flushing (more) seq %lld\n", inode,
1447a8599bd8SSage Weil 		     ci->i_cap_flush_seq);
1448a8599bd8SSage Weil 	}
1449a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_dirty_lock);
1450cdc35f96SSage Weil 
1451cdc35f96SSage Weil 	return flushing;
1452a8599bd8SSage Weil }
1453a8599bd8SSage Weil 
1454a8599bd8SSage Weil /*
14555ecad6fdSSage Weil  * try to invalidate mapping pages without blocking.
14565ecad6fdSSage Weil  */
14575ecad6fdSSage Weil static int try_nonblocking_invalidate(struct inode *inode)
14585ecad6fdSSage Weil {
14595ecad6fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
14605ecad6fdSSage Weil 	u32 invalidating_gen = ci->i_rdcache_gen;
14615ecad6fdSSage Weil 
1462be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
14635ecad6fdSSage Weil 	invalidate_mapping_pages(&inode->i_data, 0, -1);
1464be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
14655ecad6fdSSage Weil 
146618a38193SSage Weil 	if (inode->i_data.nrpages == 0 &&
14675ecad6fdSSage Weil 	    invalidating_gen == ci->i_rdcache_gen) {
14685ecad6fdSSage Weil 		/* success. */
14695ecad6fdSSage Weil 		dout("try_nonblocking_invalidate %p success\n", inode);
1470cd045cb4SSage Weil 		/* save any racing async invalidate some trouble */
1471cd045cb4SSage Weil 		ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
14725ecad6fdSSage Weil 		return 0;
14735ecad6fdSSage Weil 	}
14745ecad6fdSSage Weil 	dout("try_nonblocking_invalidate %p failed\n", inode);
14755ecad6fdSSage Weil 	return -1;
14765ecad6fdSSage Weil }
14775ecad6fdSSage Weil 
14785ecad6fdSSage Weil /*
1479a8599bd8SSage Weil  * Swiss army knife function to examine currently used and wanted
1480a8599bd8SSage Weil  * versus held caps.  Release, flush, ack revoked caps to mds as
1481a8599bd8SSage Weil  * appropriate.
1482a8599bd8SSage Weil  *
1483a8599bd8SSage Weil  *  CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1484a8599bd8SSage Weil  *    cap release further.
1485a8599bd8SSage Weil  *  CHECK_CAPS_AUTHONLY - we should only check the auth cap
1486a8599bd8SSage Weil  *  CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1487a8599bd8SSage Weil  *    further delay.
1488a8599bd8SSage Weil  */
1489a8599bd8SSage Weil void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1490a8599bd8SSage Weil 		     struct ceph_mds_session *session)
1491a8599bd8SSage Weil {
14923d14c5d2SYehuda Sadeh 	struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
14933d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
1494a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
1495a8599bd8SSage Weil 	struct ceph_cap *cap;
1496395c312bSYan, Zheng 	int file_wanted, used, cap_used;
1497a8599bd8SSage Weil 	int took_snap_rwsem = 0;             /* true if mdsc->snap_rwsem held */
1498cbd03635SSage Weil 	int issued, implemented, want, retain, revoking, flushing = 0;
1499a8599bd8SSage Weil 	int mds = -1;   /* keep track of how far we've gone through i_caps list
1500a8599bd8SSage Weil 			   to avoid an infinite loop on retry */
1501a8599bd8SSage Weil 	struct rb_node *p;
1502a8599bd8SSage Weil 	int tried_invalidate = 0;
1503a8599bd8SSage Weil 	int delayed = 0, sent = 0, force_requeue = 0, num;
1504cbd03635SSage Weil 	int queue_invalidate = 0;
1505a8599bd8SSage Weil 	int is_delayed = flags & CHECK_CAPS_NODELAY;
1506a8599bd8SSage Weil 
1507a8599bd8SSage Weil 	/* if we are unmounting, flush any unused caps immediately. */
1508a8599bd8SSage Weil 	if (mdsc->stopping)
1509a8599bd8SSage Weil 		is_delayed = 1;
1510a8599bd8SSage Weil 
1511be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1512a8599bd8SSage Weil 
1513a8599bd8SSage Weil 	if (ci->i_ceph_flags & CEPH_I_FLUSH)
1514a8599bd8SSage Weil 		flags |= CHECK_CAPS_FLUSH;
1515a8599bd8SSage Weil 
1516a8599bd8SSage Weil 	/* flush snaps first time around only */
1517a8599bd8SSage Weil 	if (!list_empty(&ci->i_cap_snaps))
1518e835124cSSage Weil 		__ceph_flush_snaps(ci, &session, 0);
1519a8599bd8SSage Weil 	goto retry_locked;
1520a8599bd8SSage Weil retry:
1521be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1522a8599bd8SSage Weil retry_locked:
1523a8599bd8SSage Weil 	file_wanted = __ceph_caps_file_wanted(ci);
1524a8599bd8SSage Weil 	used = __ceph_caps_used(ci);
1525a8599bd8SSage Weil 	want = file_wanted | used;
1526cbd03635SSage Weil 	issued = __ceph_caps_issued(ci, &implemented);
1527cbd03635SSage Weil 	revoking = implemented & ~issued;
1528a8599bd8SSage Weil 
1529a8599bd8SSage Weil 	retain = want | CEPH_CAP_PIN;
1530a8599bd8SSage Weil 	if (!mdsc->stopping && inode->i_nlink > 0) {
1531a8599bd8SSage Weil 		if (want) {
1532a8599bd8SSage Weil 			retain |= CEPH_CAP_ANY;       /* be greedy */
1533a8599bd8SSage Weil 		} else {
1534a8599bd8SSage Weil 			retain |= CEPH_CAP_ANY_SHARED;
1535a8599bd8SSage Weil 			/*
1536a8599bd8SSage Weil 			 * keep RD only if we didn't have the file open RW,
1537a8599bd8SSage Weil 			 * because then the mds would revoke it anyway to
1538a8599bd8SSage Weil 			 * journal max_size=0.
1539a8599bd8SSage Weil 			 */
1540a8599bd8SSage Weil 			if (ci->i_max_size == 0)
1541a8599bd8SSage Weil 				retain |= CEPH_CAP_ANY_RD;
1542a8599bd8SSage Weil 		}
1543a8599bd8SSage Weil 	}
1544a8599bd8SSage Weil 
1545a8599bd8SSage Weil 	dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1546cbd03635SSage Weil 	     " issued %s revoking %s retain %s %s%s%s\n", inode,
1547a8599bd8SSage Weil 	     ceph_cap_string(file_wanted),
1548a8599bd8SSage Weil 	     ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1549a8599bd8SSage Weil 	     ceph_cap_string(ci->i_flushing_caps),
1550cbd03635SSage Weil 	     ceph_cap_string(issued), ceph_cap_string(revoking),
1551a8599bd8SSage Weil 	     ceph_cap_string(retain),
1552a8599bd8SSage Weil 	     (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1553a8599bd8SSage Weil 	     (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1554a8599bd8SSage Weil 	     (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1555a8599bd8SSage Weil 
1556a8599bd8SSage Weil 	/*
1557a8599bd8SSage Weil 	 * If we no longer need to hold onto old our caps, and we may
1558a8599bd8SSage Weil 	 * have cached pages, but don't want them, then try to invalidate.
1559a8599bd8SSage Weil 	 * If we fail, it's because pages are locked.... try again later.
1560a8599bd8SSage Weil 	 */
1561a8599bd8SSage Weil 	if ((!is_delayed || mdsc->stopping) &&
1562a8599bd8SSage Weil 	    ci->i_wrbuffer_ref == 0 &&               /* no dirty pages... */
156393afd449SSage Weil 	    inode->i_data.nrpages &&                 /* have cached pages */
1564cbd03635SSage Weil 	    (file_wanted == 0 ||                     /* no open files */
15652962507cSSage Weil 	     (revoking & (CEPH_CAP_FILE_CACHE|
15662962507cSSage Weil 			  CEPH_CAP_FILE_LAZYIO))) && /*  or revoking cache */
1567a8599bd8SSage Weil 	    !tried_invalidate) {
1568a8599bd8SSage Weil 		dout("check_caps trying to invalidate on %p\n", inode);
15695ecad6fdSSage Weil 		if (try_nonblocking_invalidate(inode) < 0) {
15702962507cSSage Weil 			if (revoking & (CEPH_CAP_FILE_CACHE|
15712962507cSSage Weil 					CEPH_CAP_FILE_LAZYIO)) {
1572cbd03635SSage Weil 				dout("check_caps queuing invalidate\n");
1573cbd03635SSage Weil 				queue_invalidate = 1;
1574cbd03635SSage Weil 				ci->i_rdcache_revoking = ci->i_rdcache_gen;
1575a8599bd8SSage Weil 			} else {
1576a8599bd8SSage Weil 				dout("check_caps failed to invalidate pages\n");
1577a8599bd8SSage Weil 				/* we failed to invalidate pages.  check these
1578a8599bd8SSage Weil 				   caps again later. */
1579a8599bd8SSage Weil 				force_requeue = 1;
1580a8599bd8SSage Weil 				__cap_set_timeouts(mdsc, ci);
1581a8599bd8SSage Weil 			}
15825ecad6fdSSage Weil 		}
1583a8599bd8SSage Weil 		tried_invalidate = 1;
1584a8599bd8SSage Weil 		goto retry_locked;
1585a8599bd8SSage Weil 	}
1586a8599bd8SSage Weil 
1587a8599bd8SSage Weil 	num = 0;
1588a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1589a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
1590a8599bd8SSage Weil 		num++;
1591a8599bd8SSage Weil 
1592a8599bd8SSage Weil 		/* avoid looping forever */
1593a8599bd8SSage Weil 		if (mds >= cap->mds ||
1594a8599bd8SSage Weil 		    ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1595a8599bd8SSage Weil 			continue;
1596a8599bd8SSage Weil 
1597a8599bd8SSage Weil 		/* NOTE: no side-effects allowed, until we take s_mutex */
1598a8599bd8SSage Weil 
1599395c312bSYan, Zheng 		cap_used = used;
1600395c312bSYan, Zheng 		if (ci->i_auth_cap && cap != ci->i_auth_cap)
1601395c312bSYan, Zheng 			cap_used &= ~ci->i_auth_cap->issued;
1602395c312bSYan, Zheng 
1603a8599bd8SSage Weil 		revoking = cap->implemented & ~cap->issued;
1604395c312bSYan, Zheng 		dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
1605088b3f5eSSage Weil 		     cap->mds, cap, ceph_cap_string(cap->issued),
1606395c312bSYan, Zheng 		     ceph_cap_string(cap_used),
1607088b3f5eSSage Weil 		     ceph_cap_string(cap->implemented),
1608a8599bd8SSage Weil 		     ceph_cap_string(revoking));
1609a8599bd8SSage Weil 
1610a8599bd8SSage Weil 		if (cap == ci->i_auth_cap &&
1611a8599bd8SSage Weil 		    (cap->issued & CEPH_CAP_FILE_WR)) {
1612a8599bd8SSage Weil 			/* request larger max_size from MDS? */
1613a8599bd8SSage Weil 			if (ci->i_wanted_max_size > ci->i_max_size &&
1614a8599bd8SSage Weil 			    ci->i_wanted_max_size > ci->i_requested_max_size) {
1615a8599bd8SSage Weil 				dout("requesting new max_size\n");
1616a8599bd8SSage Weil 				goto ack;
1617a8599bd8SSage Weil 			}
1618a8599bd8SSage Weil 
1619a8599bd8SSage Weil 			/* approaching file_max? */
1620a8599bd8SSage Weil 			if ((inode->i_size << 1) >= ci->i_max_size &&
1621a8599bd8SSage Weil 			    (ci->i_reported_size << 1) < ci->i_max_size) {
1622a8599bd8SSage Weil 				dout("i_size approaching max_size\n");
1623a8599bd8SSage Weil 				goto ack;
1624a8599bd8SSage Weil 			}
1625a8599bd8SSage Weil 		}
1626a8599bd8SSage Weil 		/* flush anything dirty? */
1627a8599bd8SSage Weil 		if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1628a8599bd8SSage Weil 		    ci->i_dirty_caps) {
1629a8599bd8SSage Weil 			dout("flushing dirty caps\n");
1630a8599bd8SSage Weil 			goto ack;
1631a8599bd8SSage Weil 		}
1632a8599bd8SSage Weil 
1633a8599bd8SSage Weil 		/* completed revocation? going down and there are no caps? */
1634395c312bSYan, Zheng 		if (revoking && (revoking & cap_used) == 0) {
1635a8599bd8SSage Weil 			dout("completed revocation of %s\n",
1636a8599bd8SSage Weil 			     ceph_cap_string(cap->implemented & ~cap->issued));
1637a8599bd8SSage Weil 			goto ack;
1638a8599bd8SSage Weil 		}
1639a8599bd8SSage Weil 
1640a8599bd8SSage Weil 		/* want more caps from mds? */
1641a8599bd8SSage Weil 		if (want & ~(cap->mds_wanted | cap->issued))
1642a8599bd8SSage Weil 			goto ack;
1643a8599bd8SSage Weil 
1644a8599bd8SSage Weil 		/* things we might delay */
1645a8599bd8SSage Weil 		if ((cap->issued & ~retain) == 0 &&
1646a8599bd8SSage Weil 		    cap->mds_wanted == want)
1647a8599bd8SSage Weil 			continue;     /* nope, all good */
1648a8599bd8SSage Weil 
1649a8599bd8SSage Weil 		if (is_delayed)
1650a8599bd8SSage Weil 			goto ack;
1651a8599bd8SSage Weil 
1652a8599bd8SSage Weil 		/* delay? */
1653a8599bd8SSage Weil 		if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1654a8599bd8SSage Weil 		    time_before(jiffies, ci->i_hold_caps_max)) {
1655a8599bd8SSage Weil 			dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1656a8599bd8SSage Weil 			     ceph_cap_string(cap->issued),
1657a8599bd8SSage Weil 			     ceph_cap_string(cap->issued & retain),
1658a8599bd8SSage Weil 			     ceph_cap_string(cap->mds_wanted),
1659a8599bd8SSage Weil 			     ceph_cap_string(want));
1660a8599bd8SSage Weil 			delayed++;
1661a8599bd8SSage Weil 			continue;
1662a8599bd8SSage Weil 		}
1663a8599bd8SSage Weil 
1664a8599bd8SSage Weil ack:
1665e9964c10SSage Weil 		if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1666e9964c10SSage Weil 			dout(" skipping %p I_NOFLUSH set\n", inode);
1667e9964c10SSage Weil 			continue;
1668e9964c10SSage Weil 		}
1669e9964c10SSage Weil 
1670a8599bd8SSage Weil 		if (session && session != cap->session) {
1671a8599bd8SSage Weil 			dout("oops, wrong session %p mutex\n", session);
1672a8599bd8SSage Weil 			mutex_unlock(&session->s_mutex);
1673a8599bd8SSage Weil 			session = NULL;
1674a8599bd8SSage Weil 		}
1675a8599bd8SSage Weil 		if (!session) {
1676a8599bd8SSage Weil 			session = cap->session;
1677a8599bd8SSage Weil 			if (mutex_trylock(&session->s_mutex) == 0) {
1678a8599bd8SSage Weil 				dout("inverting session/ino locks on %p\n",
1679a8599bd8SSage Weil 				     session);
1680be655596SSage Weil 				spin_unlock(&ci->i_ceph_lock);
1681a8599bd8SSage Weil 				if (took_snap_rwsem) {
1682a8599bd8SSage Weil 					up_read(&mdsc->snap_rwsem);
1683a8599bd8SSage Weil 					took_snap_rwsem = 0;
1684a8599bd8SSage Weil 				}
1685a8599bd8SSage Weil 				mutex_lock(&session->s_mutex);
1686a8599bd8SSage Weil 				goto retry;
1687a8599bd8SSage Weil 			}
1688a8599bd8SSage Weil 		}
1689a8599bd8SSage Weil 		/* take snap_rwsem after session mutex */
1690a8599bd8SSage Weil 		if (!took_snap_rwsem) {
1691a8599bd8SSage Weil 			if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1692a8599bd8SSage Weil 				dout("inverting snap/in locks on %p\n",
1693a8599bd8SSage Weil 				     inode);
1694be655596SSage Weil 				spin_unlock(&ci->i_ceph_lock);
1695a8599bd8SSage Weil 				down_read(&mdsc->snap_rwsem);
1696a8599bd8SSage Weil 				took_snap_rwsem = 1;
1697a8599bd8SSage Weil 				goto retry;
1698a8599bd8SSage Weil 			}
1699a8599bd8SSage Weil 			took_snap_rwsem = 1;
1700a8599bd8SSage Weil 		}
1701a8599bd8SSage Weil 
1702cdc35f96SSage Weil 		if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1703cdc35f96SSage Weil 			flushing = __mark_caps_flushing(inode, session);
170424be0c48SSage Weil 		else
170524be0c48SSage Weil 			flushing = 0;
1706a8599bd8SSage Weil 
1707a8599bd8SSage Weil 		mds = cap->mds;  /* remember mds, so we don't repeat */
1708a8599bd8SSage Weil 		sent++;
1709a8599bd8SSage Weil 
1710be655596SSage Weil 		/* __send_cap drops i_ceph_lock */
1711395c312bSYan, Zheng 		delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used,
1712395c312bSYan, Zheng 				      want, retain, flushing, NULL);
1713be655596SSage Weil 		goto retry; /* retake i_ceph_lock and restart our cap scan. */
1714a8599bd8SSage Weil 	}
1715a8599bd8SSage Weil 
1716a8599bd8SSage Weil 	/*
1717a8599bd8SSage Weil 	 * Reschedule delayed caps release if we delayed anything,
1718a8599bd8SSage Weil 	 * otherwise cancel.
1719a8599bd8SSage Weil 	 */
1720a8599bd8SSage Weil 	if (delayed && is_delayed)
1721a8599bd8SSage Weil 		force_requeue = 1;   /* __send_cap delayed release; requeue */
1722a8599bd8SSage Weil 	if (!delayed && !is_delayed)
1723a8599bd8SSage Weil 		__cap_delay_cancel(mdsc, ci);
1724a8599bd8SSage Weil 	else if (!is_delayed || force_requeue)
1725a8599bd8SSage Weil 		__cap_delay_requeue(mdsc, ci);
1726a8599bd8SSage Weil 
1727be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1728a8599bd8SSage Weil 
1729cbd03635SSage Weil 	if (queue_invalidate)
17303c6f6b79SSage Weil 		ceph_queue_invalidate(inode);
1731cbd03635SSage Weil 
1732cdc2ce05SSage Weil 	if (session)
1733a8599bd8SSage Weil 		mutex_unlock(&session->s_mutex);
1734a8599bd8SSage Weil 	if (took_snap_rwsem)
1735a8599bd8SSage Weil 		up_read(&mdsc->snap_rwsem);
1736a8599bd8SSage Weil }
1737a8599bd8SSage Weil 
1738a8599bd8SSage Weil /*
1739a8599bd8SSage Weil  * Try to flush dirty caps back to the auth mds.
1740a8599bd8SSage Weil  */
17414fe59789SYan, Zheng static int try_flush_caps(struct inode *inode, unsigned *flush_tid)
1742a8599bd8SSage Weil {
17433d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1744a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1745a8599bd8SSage Weil 	int flushing = 0;
17464fe59789SYan, Zheng 	struct ceph_mds_session *session = NULL;
1747a8599bd8SSage Weil 
1748a8599bd8SSage Weil retry:
1749be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1750e9964c10SSage Weil 	if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1751e9964c10SSage Weil 		dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1752e9964c10SSage Weil 		goto out;
1753e9964c10SSage Weil 	}
1754a8599bd8SSage Weil 	if (ci->i_dirty_caps && ci->i_auth_cap) {
1755a8599bd8SSage Weil 		struct ceph_cap *cap = ci->i_auth_cap;
1756a8599bd8SSage Weil 		int used = __ceph_caps_used(ci);
1757a8599bd8SSage Weil 		int want = __ceph_caps_wanted(ci);
1758a8599bd8SSage Weil 		int delayed;
1759a8599bd8SSage Weil 
17604fe59789SYan, Zheng 		if (!session || session != cap->session) {
1761be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
17624fe59789SYan, Zheng 			if (session)
17634fe59789SYan, Zheng 				mutex_unlock(&session->s_mutex);
1764a8599bd8SSage Weil 			session = cap->session;
1765a8599bd8SSage Weil 			mutex_lock(&session->s_mutex);
1766a8599bd8SSage Weil 			goto retry;
1767a8599bd8SSage Weil 		}
1768a8599bd8SSage Weil 		if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1769a8599bd8SSage Weil 			goto out;
1770a8599bd8SSage Weil 
1771cdc35f96SSage Weil 		flushing = __mark_caps_flushing(inode, session);
1772a8599bd8SSage Weil 
1773be655596SSage Weil 		/* __send_cap drops i_ceph_lock */
1774a8599bd8SSage Weil 		delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1775a8599bd8SSage Weil 				     cap->issued | cap->implemented, flushing,
1776a8599bd8SSage Weil 				     flush_tid);
1777a8599bd8SSage Weil 		if (!delayed)
1778a8599bd8SSage Weil 			goto out_unlocked;
1779a8599bd8SSage Weil 
1780be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1781a8599bd8SSage Weil 		__cap_delay_requeue(mdsc, ci);
1782a8599bd8SSage Weil 	}
1783a8599bd8SSage Weil out:
1784be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1785a8599bd8SSage Weil out_unlocked:
17864fe59789SYan, Zheng 	if (session)
1787a8599bd8SSage Weil 		mutex_unlock(&session->s_mutex);
1788a8599bd8SSage Weil 	return flushing;
1789a8599bd8SSage Weil }
1790a8599bd8SSage Weil 
1791a8599bd8SSage Weil /*
1792a8599bd8SSage Weil  * Return true if we've flushed caps through the given flush_tid.
1793a8599bd8SSage Weil  */
1794a8599bd8SSage Weil static int caps_are_flushed(struct inode *inode, unsigned tid)
1795a8599bd8SSage Weil {
1796a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1797a5ee751cSDan Carpenter 	int i, ret = 1;
1798a8599bd8SSage Weil 
1799be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1800a8599bd8SSage Weil 	for (i = 0; i < CEPH_CAP_BITS; i++)
1801a8599bd8SSage Weil 		if ((ci->i_flushing_caps & (1 << i)) &&
1802a8599bd8SSage Weil 		    ci->i_cap_flush_tid[i] <= tid) {
1803a8599bd8SSage Weil 			/* still flushing this bit */
1804a8599bd8SSage Weil 			ret = 0;
1805a8599bd8SSage Weil 			break;
1806a8599bd8SSage Weil 		}
1807be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1808a8599bd8SSage Weil 	return ret;
1809a8599bd8SSage Weil }
1810a8599bd8SSage Weil 
1811a8599bd8SSage Weil /*
1812a8599bd8SSage Weil  * Wait on any unsafe replies for the given inode.  First wait on the
1813a8599bd8SSage Weil  * newest request, and make that the upper bound.  Then, if there are
1814a8599bd8SSage Weil  * more requests, keep waiting on the oldest as long as it is still older
1815a8599bd8SSage Weil  * than the original request.
1816a8599bd8SSage Weil  */
1817a8599bd8SSage Weil static void sync_write_wait(struct inode *inode)
1818a8599bd8SSage Weil {
1819a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1820a8599bd8SSage Weil 	struct list_head *head = &ci->i_unsafe_writes;
1821a8599bd8SSage Weil 	struct ceph_osd_request *req;
1822a8599bd8SSage Weil 	u64 last_tid;
1823a8599bd8SSage Weil 
1824a8599bd8SSage Weil 	spin_lock(&ci->i_unsafe_lock);
1825a8599bd8SSage Weil 	if (list_empty(head))
1826a8599bd8SSage Weil 		goto out;
1827a8599bd8SSage Weil 
1828a8599bd8SSage Weil 	/* set upper bound as _last_ entry in chain */
1829a8599bd8SSage Weil 	req = list_entry(head->prev, struct ceph_osd_request,
1830a8599bd8SSage Weil 			 r_unsafe_item);
1831a8599bd8SSage Weil 	last_tid = req->r_tid;
1832a8599bd8SSage Weil 
1833a8599bd8SSage Weil 	do {
1834a8599bd8SSage Weil 		ceph_osdc_get_request(req);
1835a8599bd8SSage Weil 		spin_unlock(&ci->i_unsafe_lock);
1836a8599bd8SSage Weil 		dout("sync_write_wait on tid %llu (until %llu)\n",
1837a8599bd8SSage Weil 		     req->r_tid, last_tid);
1838a8599bd8SSage Weil 		wait_for_completion(&req->r_safe_completion);
1839a8599bd8SSage Weil 		spin_lock(&ci->i_unsafe_lock);
1840a8599bd8SSage Weil 		ceph_osdc_put_request(req);
1841a8599bd8SSage Weil 
1842a8599bd8SSage Weil 		/*
1843a8599bd8SSage Weil 		 * from here on look at first entry in chain, since we
1844a8599bd8SSage Weil 		 * only want to wait for anything older than last_tid
1845a8599bd8SSage Weil 		 */
1846a8599bd8SSage Weil 		if (list_empty(head))
1847a8599bd8SSage Weil 			break;
1848a8599bd8SSage Weil 		req = list_entry(head->next, struct ceph_osd_request,
1849a8599bd8SSage Weil 				 r_unsafe_item);
1850a8599bd8SSage Weil 	} while (req->r_tid < last_tid);
1851a8599bd8SSage Weil out:
1852a8599bd8SSage Weil 	spin_unlock(&ci->i_unsafe_lock);
1853a8599bd8SSage Weil }
1854a8599bd8SSage Weil 
185502c24a82SJosef Bacik int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1856a8599bd8SSage Weil {
18577ea80859SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
1858a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1859a8599bd8SSage Weil 	unsigned flush_tid;
1860a8599bd8SSage Weil 	int ret;
1861a8599bd8SSage Weil 	int dirty;
1862a8599bd8SSage Weil 
1863a8599bd8SSage Weil 	dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1864a8599bd8SSage Weil 	sync_write_wait(inode);
1865a8599bd8SSage Weil 
186602c24a82SJosef Bacik 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
1867a8599bd8SSage Weil 	if (ret < 0)
1868a8599bd8SSage Weil 		return ret;
186902c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
1870a8599bd8SSage Weil 
18714fe59789SYan, Zheng 	dirty = try_flush_caps(inode, &flush_tid);
1872a8599bd8SSage Weil 	dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1873a8599bd8SSage Weil 
1874a8599bd8SSage Weil 	/*
1875a8599bd8SSage Weil 	 * only wait on non-file metadata writeback (the mds
1876a8599bd8SSage Weil 	 * can recover size and mtime, so we don't need to
1877a8599bd8SSage Weil 	 * wait for that)
1878a8599bd8SSage Weil 	 */
1879a8599bd8SSage Weil 	if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1880a8599bd8SSage Weil 		dout("fsync waiting for flush_tid %u\n", flush_tid);
1881a8599bd8SSage Weil 		ret = wait_event_interruptible(ci->i_cap_wq,
1882a8599bd8SSage Weil 				       caps_are_flushed(inode, flush_tid));
1883a8599bd8SSage Weil 	}
1884a8599bd8SSage Weil 
1885a8599bd8SSage Weil 	dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
188602c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
1887a8599bd8SSage Weil 	return ret;
1888a8599bd8SSage Weil }
1889a8599bd8SSage Weil 
1890a8599bd8SSage Weil /*
1891a8599bd8SSage Weil  * Flush any dirty caps back to the mds.  If we aren't asked to wait,
1892a8599bd8SSage Weil  * queue inode for flush but don't do so immediately, because we can
1893a8599bd8SSage Weil  * get by with fewer MDS messages if we wait for data writeback to
1894a8599bd8SSage Weil  * complete first.
1895a8599bd8SSage Weil  */
1896f1a3d572SStephen Rothwell int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
1897a8599bd8SSage Weil {
1898a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1899a8599bd8SSage Weil 	unsigned flush_tid;
1900a8599bd8SSage Weil 	int err = 0;
1901a8599bd8SSage Weil 	int dirty;
1902f1a3d572SStephen Rothwell 	int wait = wbc->sync_mode == WB_SYNC_ALL;
1903a8599bd8SSage Weil 
1904a8599bd8SSage Weil 	dout("write_inode %p wait=%d\n", inode, wait);
1905a8599bd8SSage Weil 	if (wait) {
19064fe59789SYan, Zheng 		dirty = try_flush_caps(inode, &flush_tid);
1907a8599bd8SSage Weil 		if (dirty)
1908a8599bd8SSage Weil 			err = wait_event_interruptible(ci->i_cap_wq,
1909a8599bd8SSage Weil 				       caps_are_flushed(inode, flush_tid));
1910a8599bd8SSage Weil 	} else {
1911640ef79dSCheng Renquan 		struct ceph_mds_client *mdsc =
19123d14c5d2SYehuda Sadeh 			ceph_sb_to_client(inode->i_sb)->mdsc;
1913a8599bd8SSage Weil 
1914be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1915a8599bd8SSage Weil 		if (__ceph_caps_dirty(ci))
1916a8599bd8SSage Weil 			__cap_delay_requeue_front(mdsc, ci);
1917be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1918a8599bd8SSage Weil 	}
1919a8599bd8SSage Weil 	return err;
1920a8599bd8SSage Weil }
1921a8599bd8SSage Weil 
1922a8599bd8SSage Weil /*
1923a8599bd8SSage Weil  * After a recovering MDS goes active, we need to resend any caps
1924a8599bd8SSage Weil  * we were flushing.
1925a8599bd8SSage Weil  *
1926a8599bd8SSage Weil  * Caller holds session->s_mutex.
1927a8599bd8SSage Weil  */
1928a8599bd8SSage Weil static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1929a8599bd8SSage Weil 				   struct ceph_mds_session *session)
1930a8599bd8SSage Weil {
1931a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
1932a8599bd8SSage Weil 
1933a8599bd8SSage Weil 	dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1934a8599bd8SSage Weil 	list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1935a8599bd8SSage Weil 			    flushing_item) {
1936a8599bd8SSage Weil 		struct ceph_inode_info *ci = capsnap->ci;
1937a8599bd8SSage Weil 		struct inode *inode = &ci->vfs_inode;
1938a8599bd8SSage Weil 		struct ceph_cap *cap;
1939a8599bd8SSage Weil 
1940be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1941a8599bd8SSage Weil 		cap = ci->i_auth_cap;
1942a8599bd8SSage Weil 		if (cap && cap->session == session) {
1943a8599bd8SSage Weil 			dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1944a8599bd8SSage Weil 			     cap, capsnap);
1945e835124cSSage Weil 			__ceph_flush_snaps(ci, &session, 1);
1946a8599bd8SSage Weil 		} else {
1947a8599bd8SSage Weil 			pr_err("%p auth cap %p not mds%d ???\n", inode,
1948a8599bd8SSage Weil 			       cap, session->s_mds);
1949a8599bd8SSage Weil 		}
1950be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1951a8599bd8SSage Weil 	}
1952a8599bd8SSage Weil }
1953a8599bd8SSage Weil 
1954a8599bd8SSage Weil void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1955a8599bd8SSage Weil 			     struct ceph_mds_session *session)
1956a8599bd8SSage Weil {
1957a8599bd8SSage Weil 	struct ceph_inode_info *ci;
1958a8599bd8SSage Weil 
1959a8599bd8SSage Weil 	kick_flushing_capsnaps(mdsc, session);
1960a8599bd8SSage Weil 
1961a8599bd8SSage Weil 	dout("kick_flushing_caps mds%d\n", session->s_mds);
1962a8599bd8SSage Weil 	list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1963a8599bd8SSage Weil 		struct inode *inode = &ci->vfs_inode;
1964a8599bd8SSage Weil 		struct ceph_cap *cap;
1965a8599bd8SSage Weil 		int delayed = 0;
1966a8599bd8SSage Weil 
1967be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1968a8599bd8SSage Weil 		cap = ci->i_auth_cap;
1969a8599bd8SSage Weil 		if (cap && cap->session == session) {
1970a8599bd8SSage Weil 			dout("kick_flushing_caps %p cap %p %s\n", inode,
1971a8599bd8SSage Weil 			     cap, ceph_cap_string(ci->i_flushing_caps));
1972a8599bd8SSage Weil 			delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1973a8599bd8SSage Weil 					     __ceph_caps_used(ci),
1974a8599bd8SSage Weil 					     __ceph_caps_wanted(ci),
1975a8599bd8SSage Weil 					     cap->issued | cap->implemented,
1976a8599bd8SSage Weil 					     ci->i_flushing_caps, NULL);
1977a8599bd8SSage Weil 			if (delayed) {
1978be655596SSage Weil 				spin_lock(&ci->i_ceph_lock);
1979a8599bd8SSage Weil 				__cap_delay_requeue(mdsc, ci);
1980be655596SSage Weil 				spin_unlock(&ci->i_ceph_lock);
1981a8599bd8SSage Weil 			}
1982a8599bd8SSage Weil 		} else {
1983a8599bd8SSage Weil 			pr_err("%p auth cap %p not mds%d ???\n", inode,
1984a8599bd8SSage Weil 			       cap, session->s_mds);
1985be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
1986a8599bd8SSage Weil 		}
1987a8599bd8SSage Weil 	}
1988a8599bd8SSage Weil }
1989a8599bd8SSage Weil 
1990088b3f5eSSage Weil static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
1991088b3f5eSSage Weil 				     struct ceph_mds_session *session,
1992088b3f5eSSage Weil 				     struct inode *inode)
1993088b3f5eSSage Weil {
1994088b3f5eSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1995088b3f5eSSage Weil 	struct ceph_cap *cap;
1996088b3f5eSSage Weil 	int delayed = 0;
1997088b3f5eSSage Weil 
1998be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1999088b3f5eSSage Weil 	cap = ci->i_auth_cap;
2000088b3f5eSSage Weil 	dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode,
2001088b3f5eSSage Weil 	     ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq);
2002005c4697SYan, Zheng 
2003088b3f5eSSage Weil 	__ceph_flush_snaps(ci, &session, 1);
2004005c4697SYan, Zheng 
2005088b3f5eSSage Weil 	if (ci->i_flushing_caps) {
2006005c4697SYan, Zheng 		spin_lock(&mdsc->cap_dirty_lock);
2007005c4697SYan, Zheng 		list_move_tail(&ci->i_flushing_item,
2008005c4697SYan, Zheng 			       &cap->session->s_cap_flushing);
2009005c4697SYan, Zheng 		spin_unlock(&mdsc->cap_dirty_lock);
2010005c4697SYan, Zheng 
2011088b3f5eSSage Weil 		delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
2012088b3f5eSSage Weil 				     __ceph_caps_used(ci),
2013088b3f5eSSage Weil 				     __ceph_caps_wanted(ci),
2014088b3f5eSSage Weil 				     cap->issued | cap->implemented,
2015088b3f5eSSage Weil 				     ci->i_flushing_caps, NULL);
2016088b3f5eSSage Weil 		if (delayed) {
2017be655596SSage Weil 			spin_lock(&ci->i_ceph_lock);
2018088b3f5eSSage Weil 			__cap_delay_requeue(mdsc, ci);
2019be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
2020088b3f5eSSage Weil 		}
2021088b3f5eSSage Weil 	} else {
2022be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
2023088b3f5eSSage Weil 	}
2024088b3f5eSSage Weil }
2025088b3f5eSSage Weil 
2026a8599bd8SSage Weil 
2027a8599bd8SSage Weil /*
2028a8599bd8SSage Weil  * Take references to capabilities we hold, so that we don't release
2029a8599bd8SSage Weil  * them to the MDS prematurely.
2030a8599bd8SSage Weil  *
2031be655596SSage Weil  * Protected by i_ceph_lock.
2032a8599bd8SSage Weil  */
2033a8599bd8SSage Weil static void __take_cap_refs(struct ceph_inode_info *ci, int got)
2034a8599bd8SSage Weil {
2035a8599bd8SSage Weil 	if (got & CEPH_CAP_PIN)
2036a8599bd8SSage Weil 		ci->i_pin_ref++;
2037a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_RD)
2038a8599bd8SSage Weil 		ci->i_rd_ref++;
2039a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_CACHE)
2040a8599bd8SSage Weil 		ci->i_rdcache_ref++;
2041a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_WR)
2042a8599bd8SSage Weil 		ci->i_wr_ref++;
2043a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_BUFFER) {
2044d3d0720dSHenry C Chang 		if (ci->i_wb_ref == 0)
20453772d26dSSage Weil 			ihold(&ci->vfs_inode);
2046d3d0720dSHenry C Chang 		ci->i_wb_ref++;
2047d3d0720dSHenry C Chang 		dout("__take_cap_refs %p wb %d -> %d (?)\n",
2048d3d0720dSHenry C Chang 		     &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2049a8599bd8SSage Weil 	}
2050a8599bd8SSage Weil }
2051a8599bd8SSage Weil 
2052a8599bd8SSage Weil /*
2053a8599bd8SSage Weil  * Try to grab cap references.  Specify those refs we @want, and the
2054a8599bd8SSage Weil  * minimal set we @need.  Also include the larger offset we are writing
2055a8599bd8SSage Weil  * to (when applicable), and check against max_size here as well.
2056a8599bd8SSage Weil  * Note that caller is responsible for ensuring max_size increases are
2057a8599bd8SSage Weil  * requested from the MDS.
2058a8599bd8SSage Weil  */
2059a8599bd8SSage Weil static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2060a8599bd8SSage Weil 			    int *got, loff_t endoff, int *check_max, int *err)
2061a8599bd8SSage Weil {
2062a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
2063a8599bd8SSage Weil 	int ret = 0;
2064a8599bd8SSage Weil 	int have, implemented;
2065195d3ce2SSage Weil 	int file_wanted;
2066a8599bd8SSage Weil 
2067a8599bd8SSage Weil 	dout("get_cap_refs %p need %s want %s\n", inode,
2068a8599bd8SSage Weil 	     ceph_cap_string(need), ceph_cap_string(want));
2069be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2070a8599bd8SSage Weil 
2071195d3ce2SSage Weil 	/* make sure file is actually open */
2072195d3ce2SSage Weil 	file_wanted = __ceph_caps_file_wanted(ci);
2073195d3ce2SSage Weil 	if ((file_wanted & need) == 0) {
2074195d3ce2SSage Weil 		dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2075195d3ce2SSage Weil 		     ceph_cap_string(need), ceph_cap_string(file_wanted));
2076a8599bd8SSage Weil 		*err = -EBADF;
2077a8599bd8SSage Weil 		ret = 1;
2078a8599bd8SSage Weil 		goto out;
2079a8599bd8SSage Weil 	}
2080a8599bd8SSage Weil 
208137505d57SYan, Zheng 	/* finish pending truncate */
208237505d57SYan, Zheng 	while (ci->i_truncate_pending) {
208337505d57SYan, Zheng 		spin_unlock(&ci->i_ceph_lock);
2084b415bf4fSYan, Zheng 		__ceph_do_pending_vmtruncate(inode);
208537505d57SYan, Zheng 		spin_lock(&ci->i_ceph_lock);
208637505d57SYan, Zheng 	}
208737505d57SYan, Zheng 
20883871cbb9SYan, Zheng 	have = __ceph_caps_issued(ci, &implemented);
20893871cbb9SYan, Zheng 
20903871cbb9SYan, Zheng 	if (have & need & CEPH_CAP_FILE_WR) {
2091a8599bd8SSage Weil 		if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2092a8599bd8SSage Weil 			dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2093a8599bd8SSage Weil 			     inode, endoff, ci->i_max_size);
20943871cbb9SYan, Zheng 			if (endoff > ci->i_requested_max_size) {
2095a8599bd8SSage Weil 				*check_max = 1;
2096a8599bd8SSage Weil 				ret = 1;
2097a8599bd8SSage Weil 			}
2098a8599bd8SSage Weil 			goto out;
2099a8599bd8SSage Weil 		}
2100a8599bd8SSage Weil 		/*
2101a8599bd8SSage Weil 		 * If a sync write is in progress, we must wait, so that we
2102a8599bd8SSage Weil 		 * can get a final snapshot value for size+mtime.
2103a8599bd8SSage Weil 		 */
2104a8599bd8SSage Weil 		if (__ceph_have_pending_cap_snap(ci)) {
2105a8599bd8SSage Weil 			dout("get_cap_refs %p cap_snap_pending\n", inode);
2106a8599bd8SSage Weil 			goto out;
2107a8599bd8SSage Weil 		}
2108a8599bd8SSage Weil 	}
2109a8599bd8SSage Weil 
2110a8599bd8SSage Weil 	if ((have & need) == need) {
2111a8599bd8SSage Weil 		/*
2112a8599bd8SSage Weil 		 * Look at (implemented & ~have & not) so that we keep waiting
2113a8599bd8SSage Weil 		 * on transition from wanted -> needed caps.  This is needed
2114a8599bd8SSage Weil 		 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2115a8599bd8SSage Weil 		 * going before a prior buffered writeback happens.
2116a8599bd8SSage Weil 		 */
2117a8599bd8SSage Weil 		int not = want & ~(have & need);
2118a8599bd8SSage Weil 		int revoking = implemented & ~have;
2119a8599bd8SSage Weil 		dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2120a8599bd8SSage Weil 		     inode, ceph_cap_string(have), ceph_cap_string(not),
2121a8599bd8SSage Weil 		     ceph_cap_string(revoking));
2122a8599bd8SSage Weil 		if ((revoking & not) == 0) {
2123a8599bd8SSage Weil 			*got = need | (have & want);
2124a8599bd8SSage Weil 			__take_cap_refs(ci, *got);
2125a8599bd8SSage Weil 			ret = 1;
2126a8599bd8SSage Weil 		}
2127a8599bd8SSage Weil 	} else {
2128a8599bd8SSage Weil 		dout("get_cap_refs %p have %s needed %s\n", inode,
2129a8599bd8SSage Weil 		     ceph_cap_string(have), ceph_cap_string(need));
2130a8599bd8SSage Weil 	}
2131a8599bd8SSage Weil out:
2132be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2133a8599bd8SSage Weil 	dout("get_cap_refs %p ret %d got %s\n", inode,
2134a8599bd8SSage Weil 	     ret, ceph_cap_string(*got));
2135a8599bd8SSage Weil 	return ret;
2136a8599bd8SSage Weil }
2137a8599bd8SSage Weil 
2138a8599bd8SSage Weil /*
2139a8599bd8SSage Weil  * Check the offset we are writing up to against our current
2140a8599bd8SSage Weil  * max_size.  If necessary, tell the MDS we want to write to
2141a8599bd8SSage Weil  * a larger offset.
2142a8599bd8SSage Weil  */
2143a8599bd8SSage Weil static void check_max_size(struct inode *inode, loff_t endoff)
2144a8599bd8SSage Weil {
2145a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2146a8599bd8SSage Weil 	int check = 0;
2147a8599bd8SSage Weil 
2148a8599bd8SSage Weil 	/* do we need to explicitly request a larger max_size? */
2149be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
21503871cbb9SYan, Zheng 	if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2151a8599bd8SSage Weil 		dout("write %p at large endoff %llu, req max_size\n",
2152a8599bd8SSage Weil 		     inode, endoff);
2153a8599bd8SSage Weil 		ci->i_wanted_max_size = endoff;
2154a8599bd8SSage Weil 	}
21553871cbb9SYan, Zheng 	/* duplicate ceph_check_caps()'s logic */
21563871cbb9SYan, Zheng 	if (ci->i_auth_cap &&
21573871cbb9SYan, Zheng 	    (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
21583871cbb9SYan, Zheng 	    ci->i_wanted_max_size > ci->i_max_size &&
21593871cbb9SYan, Zheng 	    ci->i_wanted_max_size > ci->i_requested_max_size)
21603871cbb9SYan, Zheng 		check = 1;
2161be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2162a8599bd8SSage Weil 	if (check)
2163a8599bd8SSage Weil 		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2164a8599bd8SSage Weil }
2165a8599bd8SSage Weil 
2166a8599bd8SSage Weil /*
2167a8599bd8SSage Weil  * Wait for caps, and take cap references.  If we can't get a WR cap
2168a8599bd8SSage Weil  * due to a small max_size, make sure we check_max_size (and possibly
2169a8599bd8SSage Weil  * ask the mds) so we don't get hung up indefinitely.
2170a8599bd8SSage Weil  */
2171a8599bd8SSage Weil int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2172a8599bd8SSage Weil 		  loff_t endoff)
2173a8599bd8SSage Weil {
2174a8599bd8SSage Weil 	int check_max, ret, err;
2175a8599bd8SSage Weil 
2176a8599bd8SSage Weil retry:
2177a8599bd8SSage Weil 	if (endoff > 0)
2178a8599bd8SSage Weil 		check_max_size(&ci->vfs_inode, endoff);
2179a8599bd8SSage Weil 	check_max = 0;
2180a8599bd8SSage Weil 	err = 0;
2181a8599bd8SSage Weil 	ret = wait_event_interruptible(ci->i_cap_wq,
2182a8599bd8SSage Weil 				       try_get_cap_refs(ci, need, want,
2183a8599bd8SSage Weil 							got, endoff,
2184a8599bd8SSage Weil 							&check_max, &err));
2185a8599bd8SSage Weil 	if (err)
2186a8599bd8SSage Weil 		ret = err;
2187a8599bd8SSage Weil 	if (check_max)
2188a8599bd8SSage Weil 		goto retry;
2189a8599bd8SSage Weil 	return ret;
2190a8599bd8SSage Weil }
2191a8599bd8SSage Weil 
2192a8599bd8SSage Weil /*
2193a8599bd8SSage Weil  * Take cap refs.  Caller must already know we hold at least one ref
2194a8599bd8SSage Weil  * on the caps in question or we don't know this is safe.
2195a8599bd8SSage Weil  */
2196a8599bd8SSage Weil void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2197a8599bd8SSage Weil {
2198be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2199a8599bd8SSage Weil 	__take_cap_refs(ci, caps);
2200be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2201a8599bd8SSage Weil }
2202a8599bd8SSage Weil 
2203a8599bd8SSage Weil /*
2204a8599bd8SSage Weil  * Release cap refs.
2205a8599bd8SSage Weil  *
2206a8599bd8SSage Weil  * If we released the last ref on any given cap, call ceph_check_caps
2207a8599bd8SSage Weil  * to release (or schedule a release).
2208a8599bd8SSage Weil  *
2209a8599bd8SSage Weil  * If we are releasing a WR cap (from a sync write), finalize any affected
2210a8599bd8SSage Weil  * cap_snap, and wake up any waiters.
2211a8599bd8SSage Weil  */
2212a8599bd8SSage Weil void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2213a8599bd8SSage Weil {
2214a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
2215a8599bd8SSage Weil 	int last = 0, put = 0, flushsnaps = 0, wake = 0;
2216a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
2217a8599bd8SSage Weil 
2218be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2219a8599bd8SSage Weil 	if (had & CEPH_CAP_PIN)
2220a8599bd8SSage Weil 		--ci->i_pin_ref;
2221a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_RD)
2222a8599bd8SSage Weil 		if (--ci->i_rd_ref == 0)
2223a8599bd8SSage Weil 			last++;
2224a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_CACHE)
2225a8599bd8SSage Weil 		if (--ci->i_rdcache_ref == 0)
2226a8599bd8SSage Weil 			last++;
2227a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_BUFFER) {
2228d3d0720dSHenry C Chang 		if (--ci->i_wb_ref == 0) {
2229a8599bd8SSage Weil 			last++;
2230a8599bd8SSage Weil 			put++;
2231a8599bd8SSage Weil 		}
2232d3d0720dSHenry C Chang 		dout("put_cap_refs %p wb %d -> %d (?)\n",
2233d3d0720dSHenry C Chang 		     inode, ci->i_wb_ref+1, ci->i_wb_ref);
2234a8599bd8SSage Weil 	}
2235a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_WR)
2236a8599bd8SSage Weil 		if (--ci->i_wr_ref == 0) {
2237a8599bd8SSage Weil 			last++;
2238a8599bd8SSage Weil 			if (!list_empty(&ci->i_cap_snaps)) {
2239a8599bd8SSage Weil 				capsnap = list_first_entry(&ci->i_cap_snaps,
2240a8599bd8SSage Weil 						     struct ceph_cap_snap,
2241a8599bd8SSage Weil 						     ci_item);
2242a8599bd8SSage Weil 				if (capsnap->writing) {
2243a8599bd8SSage Weil 					capsnap->writing = 0;
2244a8599bd8SSage Weil 					flushsnaps =
2245a8599bd8SSage Weil 						__ceph_finish_cap_snap(ci,
2246a8599bd8SSage Weil 								       capsnap);
2247a8599bd8SSage Weil 					wake = 1;
2248a8599bd8SSage Weil 				}
2249a8599bd8SSage Weil 			}
2250a8599bd8SSage Weil 		}
2251be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2252a8599bd8SSage Weil 
2253819ccbfaSSage Weil 	dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2254819ccbfaSSage Weil 	     last ? " last" : "", put ? " put" : "");
2255a8599bd8SSage Weil 
2256a8599bd8SSage Weil 	if (last && !flushsnaps)
2257a8599bd8SSage Weil 		ceph_check_caps(ci, 0, NULL);
2258a8599bd8SSage Weil 	else if (flushsnaps)
2259a8599bd8SSage Weil 		ceph_flush_snaps(ci);
2260a8599bd8SSage Weil 	if (wake)
226103066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
2262a8599bd8SSage Weil 	if (put)
2263a8599bd8SSage Weil 		iput(inode);
2264a8599bd8SSage Weil }
2265a8599bd8SSage Weil 
2266a8599bd8SSage Weil /*
2267a8599bd8SSage Weil  * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2268a8599bd8SSage Weil  * context.  Adjust per-snap dirty page accounting as appropriate.
2269a8599bd8SSage Weil  * Once all dirty data for a cap_snap is flushed, flush snapped file
2270a8599bd8SSage Weil  * metadata back to the MDS.  If we dropped the last ref, call
2271a8599bd8SSage Weil  * ceph_check_caps.
2272a8599bd8SSage Weil  */
2273a8599bd8SSage Weil void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2274a8599bd8SSage Weil 				struct ceph_snap_context *snapc)
2275a8599bd8SSage Weil {
2276a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
2277a8599bd8SSage Weil 	int last = 0;
2278819ccbfaSSage Weil 	int complete_capsnap = 0;
2279819ccbfaSSage Weil 	int drop_capsnap = 0;
2280a8599bd8SSage Weil 	int found = 0;
2281a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap = NULL;
2282a8599bd8SSage Weil 
2283be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2284a8599bd8SSage Weil 	ci->i_wrbuffer_ref -= nr;
2285a8599bd8SSage Weil 	last = !ci->i_wrbuffer_ref;
2286a8599bd8SSage Weil 
2287a8599bd8SSage Weil 	if (ci->i_head_snapc == snapc) {
2288a8599bd8SSage Weil 		ci->i_wrbuffer_ref_head -= nr;
22897d8cb26dSSage Weil 		if (ci->i_wrbuffer_ref_head == 0 &&
22907d8cb26dSSage Weil 		    ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
22917d8cb26dSSage Weil 			BUG_ON(!ci->i_head_snapc);
2292a8599bd8SSage Weil 			ceph_put_snap_context(ci->i_head_snapc);
2293a8599bd8SSage Weil 			ci->i_head_snapc = NULL;
2294a8599bd8SSage Weil 		}
2295a8599bd8SSage Weil 		dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2296a8599bd8SSage Weil 		     inode,
2297a8599bd8SSage Weil 		     ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2298a8599bd8SSage Weil 		     ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2299a8599bd8SSage Weil 		     last ? " LAST" : "");
2300a8599bd8SSage Weil 	} else {
2301a8599bd8SSage Weil 		list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2302a8599bd8SSage Weil 			if (capsnap->context == snapc) {
2303a8599bd8SSage Weil 				found = 1;
2304a8599bd8SSage Weil 				break;
2305a8599bd8SSage Weil 			}
2306a8599bd8SSage Weil 		}
2307a8599bd8SSage Weil 		BUG_ON(!found);
2308819ccbfaSSage Weil 		capsnap->dirty_pages -= nr;
2309819ccbfaSSage Weil 		if (capsnap->dirty_pages == 0) {
2310819ccbfaSSage Weil 			complete_capsnap = 1;
2311819ccbfaSSage Weil 			if (capsnap->dirty == 0)
2312819ccbfaSSage Weil 				/* cap writeback completed before we created
2313819ccbfaSSage Weil 				 * the cap_snap; no FLUSHSNAP is needed */
2314819ccbfaSSage Weil 				drop_capsnap = 1;
2315819ccbfaSSage Weil 		}
2316a8599bd8SSage Weil 		dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2317819ccbfaSSage Weil 		     " snap %lld %d/%d -> %d/%d %s%s%s\n",
2318a8599bd8SSage Weil 		     inode, capsnap, capsnap->context->seq,
2319a8599bd8SSage Weil 		     ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2320a8599bd8SSage Weil 		     ci->i_wrbuffer_ref, capsnap->dirty_pages,
2321a8599bd8SSage Weil 		     last ? " (wrbuffer last)" : "",
2322819ccbfaSSage Weil 		     complete_capsnap ? " (complete capsnap)" : "",
2323819ccbfaSSage Weil 		     drop_capsnap ? " (drop capsnap)" : "");
2324819ccbfaSSage Weil 		if (drop_capsnap) {
2325819ccbfaSSage Weil 			ceph_put_snap_context(capsnap->context);
2326819ccbfaSSage Weil 			list_del(&capsnap->ci_item);
2327819ccbfaSSage Weil 			list_del(&capsnap->flushing_item);
2328819ccbfaSSage Weil 			ceph_put_cap_snap(capsnap);
2329819ccbfaSSage Weil 		}
2330a8599bd8SSage Weil 	}
2331a8599bd8SSage Weil 
2332be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2333a8599bd8SSage Weil 
2334a8599bd8SSage Weil 	if (last) {
2335a8599bd8SSage Weil 		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2336a8599bd8SSage Weil 		iput(inode);
2337819ccbfaSSage Weil 	} else if (complete_capsnap) {
2338a8599bd8SSage Weil 		ceph_flush_snaps(ci);
233903066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
2340a8599bd8SSage Weil 	}
2341819ccbfaSSage Weil 	if (drop_capsnap)
2342819ccbfaSSage Weil 		iput(inode);
2343a8599bd8SSage Weil }
2344a8599bd8SSage Weil 
2345a8599bd8SSage Weil /*
2346ca20c991SYan, Zheng  * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2347ca20c991SYan, Zheng  */
2348ca20c991SYan, Zheng static void invalidate_aliases(struct inode *inode)
2349ca20c991SYan, Zheng {
2350ca20c991SYan, Zheng 	struct dentry *dn, *prev = NULL;
2351ca20c991SYan, Zheng 
2352ca20c991SYan, Zheng 	dout("invalidate_aliases inode %p\n", inode);
2353ca20c991SYan, Zheng 	d_prune_aliases(inode);
2354ca20c991SYan, Zheng 	/*
2355ca20c991SYan, Zheng 	 * For non-directory inode, d_find_alias() only returns
2356fc12c80aSJ. Bruce Fields 	 * hashed dentry. After calling d_invalidate(), the
2357fc12c80aSJ. Bruce Fields 	 * dentry becomes unhashed.
2358ca20c991SYan, Zheng 	 *
2359a8d436f0SYan, Zheng 	 * For directory inode, d_find_alias() can return
2360fc12c80aSJ. Bruce Fields 	 * unhashed dentry. But directory inode should have
2361ca20c991SYan, Zheng 	 * one alias at most.
2362ca20c991SYan, Zheng 	 */
2363ca20c991SYan, Zheng 	while ((dn = d_find_alias(inode))) {
2364ca20c991SYan, Zheng 		if (dn == prev) {
2365ca20c991SYan, Zheng 			dput(dn);
2366ca20c991SYan, Zheng 			break;
2367ca20c991SYan, Zheng 		}
2368a8d436f0SYan, Zheng 		d_invalidate(dn);
2369ca20c991SYan, Zheng 		if (prev)
2370ca20c991SYan, Zheng 			dput(prev);
2371ca20c991SYan, Zheng 		prev = dn;
2372ca20c991SYan, Zheng 	}
2373ca20c991SYan, Zheng 	if (prev)
2374ca20c991SYan, Zheng 		dput(prev);
2375ca20c991SYan, Zheng }
2376ca20c991SYan, Zheng 
2377ca20c991SYan, Zheng /*
2378a8599bd8SSage Weil  * Handle a cap GRANT message from the MDS.  (Note that a GRANT may
2379a8599bd8SSage Weil  * actually be a revocation if it specifies a smaller cap set.)
2380a8599bd8SSage Weil  *
2381be655596SSage Weil  * caller holds s_mutex and i_ceph_lock, we drop both.
2382a8599bd8SSage Weil  */
23832cd698beSYan, Zheng static void handle_cap_grant(struct ceph_mds_client *mdsc,
23842cd698beSYan, Zheng 			     struct inode *inode, struct ceph_mds_caps *grant,
23852cd698beSYan, Zheng 			     void *snaptrace, int snaptrace_len,
23862cd698beSYan, Zheng 			     struct ceph_buffer *xattr_buf,
2387a8599bd8SSage Weil 			     struct ceph_mds_session *session,
23882cd698beSYan, Zheng 			     struct ceph_cap *cap, int issued)
2389be655596SSage Weil 	__releases(ci->i_ceph_lock)
2390a8599bd8SSage Weil {
2391a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2392a8599bd8SSage Weil 	int mds = session->s_mds;
23932f56f56aSSage Weil 	int seq = le32_to_cpu(grant->seq);
2394a8599bd8SSage Weil 	int newcaps = le32_to_cpu(grant->caps);
23952cd698beSYan, Zheng 	int used, wanted, dirty;
2396a8599bd8SSage Weil 	u64 size = le64_to_cpu(grant->size);
2397a8599bd8SSage Weil 	u64 max_size = le64_to_cpu(grant->max_size);
2398a8599bd8SSage Weil 	struct timespec mtime, atime, ctime;
239915637c8bSSage Weil 	int check_caps = 0;
2400ab6c2c3eSFabian Frederick 	bool wake = false;
2401ab6c2c3eSFabian Frederick 	bool writeback = false;
2402ab6c2c3eSFabian Frederick 	bool queue_trunc = false;
2403ab6c2c3eSFabian Frederick 	bool queue_invalidate = false;
2404ab6c2c3eSFabian Frederick 	bool queue_revalidate = false;
2405ab6c2c3eSFabian Frederick 	bool deleted_inode = false;
2406a8599bd8SSage Weil 
24072f56f56aSSage Weil 	dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
24082f56f56aSSage Weil 	     inode, cap, mds, seq, ceph_cap_string(newcaps));
2409a8599bd8SSage Weil 	dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2410a8599bd8SSage Weil 		inode->i_size);
2411a8599bd8SSage Weil 
241211df2dfbSYan, Zheng 
241311df2dfbSYan, Zheng 	/*
241411df2dfbSYan, Zheng 	 * auth mds of the inode changed. we received the cap export message,
241511df2dfbSYan, Zheng 	 * but still haven't received the cap import message. handle_cap_export
241611df2dfbSYan, Zheng 	 * updated the new auth MDS' cap.
241711df2dfbSYan, Zheng 	 *
241811df2dfbSYan, Zheng 	 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
241911df2dfbSYan, Zheng 	 * that was sent before the cap import message. So don't remove caps.
242011df2dfbSYan, Zheng 	 */
242111df2dfbSYan, Zheng 	if (ceph_seq_cmp(seq, cap->seq) <= 0) {
242211df2dfbSYan, Zheng 		WARN_ON(cap != ci->i_auth_cap);
242311df2dfbSYan, Zheng 		WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
242411df2dfbSYan, Zheng 		seq = cap->seq;
242511df2dfbSYan, Zheng 		newcaps |= cap->issued;
242611df2dfbSYan, Zheng 	}
242711df2dfbSYan, Zheng 
2428a8599bd8SSage Weil 	/*
2429a8599bd8SSage Weil 	 * If CACHE is being revoked, and we have no dirty buffers,
2430a8599bd8SSage Weil 	 * try to invalidate (once).  (If there are dirty buffers, we
2431a8599bd8SSage Weil 	 * will invalidate _after_ writeback.)
2432a8599bd8SSage Weil 	 */
24333b454c49SSage Weil 	if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
24343b454c49SSage Weil 	    (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2435bcd2cbd1SYehuda Sadeh 	    !ci->i_wrbuffer_ref) {
2436e9075743SLi Wang 		if (try_nonblocking_invalidate(inode)) {
2437a8599bd8SSage Weil 			/* there were locked pages.. invalidate later
2438a8599bd8SSage Weil 			   in a separate thread. */
2439a8599bd8SSage Weil 			if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
2440ab6c2c3eSFabian Frederick 				queue_invalidate = true;
2441a8599bd8SSage Weil 				ci->i_rdcache_revoking = ci->i_rdcache_gen;
2442a8599bd8SSage Weil 			}
2443a8599bd8SSage Weil 		}
244499ccbd22SMilosz Tanski 
244599ccbd22SMilosz Tanski 		ceph_fscache_invalidate(inode);
2446a8599bd8SSage Weil 	}
2447a8599bd8SSage Weil 
2448a8599bd8SSage Weil 	/* side effects now are allowed */
2449685f9a5dSSage Weil 	cap->cap_gen = session->s_cap_gen;
245011df2dfbSYan, Zheng 	cap->seq = seq;
2451a8599bd8SSage Weil 
2452a8599bd8SSage Weil 	__check_cap_issue(ci, cap, newcaps);
2453a8599bd8SSage Weil 
2454f98a128aSYan, Zheng 	if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
2455f98a128aSYan, Zheng 	    (issued & CEPH_CAP_AUTH_EXCL) == 0) {
2456a8599bd8SSage Weil 		inode->i_mode = le32_to_cpu(grant->mode);
245705cb11c1SEric W. Biederman 		inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
245805cb11c1SEric W. Biederman 		inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
2459a8599bd8SSage Weil 		dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2460bd2bae6aSEric W. Biederman 		     from_kuid(&init_user_ns, inode->i_uid),
2461bd2bae6aSEric W. Biederman 		     from_kgid(&init_user_ns, inode->i_gid));
2462a8599bd8SSage Weil 	}
2463a8599bd8SSage Weil 
2464f98a128aSYan, Zheng 	if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
2465f98a128aSYan, Zheng 	    (issued & CEPH_CAP_LINK_EXCL) == 0) {
2466bfe86848SMiklos Szeredi 		set_nlink(inode, le32_to_cpu(grant->nlink));
2467ca20c991SYan, Zheng 		if (inode->i_nlink == 0 &&
2468ca20c991SYan, Zheng 		    (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
2469ab6c2c3eSFabian Frederick 			deleted_inode = true;
2470ca20c991SYan, Zheng 	}
2471a8599bd8SSage Weil 
2472a8599bd8SSage Weil 	if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2473a8599bd8SSage Weil 		int len = le32_to_cpu(grant->xattr_len);
2474a8599bd8SSage Weil 		u64 version = le64_to_cpu(grant->xattr_version);
2475a8599bd8SSage Weil 
2476a8599bd8SSage Weil 		if (version > ci->i_xattrs.version) {
2477a8599bd8SSage Weil 			dout(" got new xattrs v%llu on %p len %d\n",
2478a8599bd8SSage Weil 			     version, inode, len);
2479a8599bd8SSage Weil 			if (ci->i_xattrs.blob)
2480a8599bd8SSage Weil 				ceph_buffer_put(ci->i_xattrs.blob);
2481a8599bd8SSage Weil 			ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2482a8599bd8SSage Weil 			ci->i_xattrs.version = version;
24837221fe4cSGuangliang Zhao 			ceph_forget_all_cached_acls(inode);
2484a8599bd8SSage Weil 		}
2485a8599bd8SSage Weil 	}
2486a8599bd8SSage Weil 
248799ccbd22SMilosz Tanski 	/* Do we need to revalidate our fscache cookie. Don't bother on the
248899ccbd22SMilosz Tanski 	 * first cache cap as we already validate at cookie creation time. */
248999ccbd22SMilosz Tanski 	if ((issued & CEPH_CAP_FILE_CACHE) && ci->i_rdcache_gen > 1)
2490ab6c2c3eSFabian Frederick 		queue_revalidate = true;
249199ccbd22SMilosz Tanski 
2492f98a128aSYan, Zheng 	if (newcaps & CEPH_CAP_ANY_RD) {
2493f98a128aSYan, Zheng 		/* ctime/mtime/atime? */
2494a8599bd8SSage Weil 		ceph_decode_timespec(&mtime, &grant->mtime);
2495a8599bd8SSage Weil 		ceph_decode_timespec(&atime, &grant->atime);
2496a8599bd8SSage Weil 		ceph_decode_timespec(&ctime, &grant->ctime);
2497a8599bd8SSage Weil 		ceph_fill_file_time(inode, issued,
2498f98a128aSYan, Zheng 				    le32_to_cpu(grant->time_warp_seq),
2499f98a128aSYan, Zheng 				    &ctime, &mtime, &atime);
2500f98a128aSYan, Zheng 	}
2501a8599bd8SSage Weil 
2502f98a128aSYan, Zheng 	if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
250311df2dfbSYan, Zheng 		/* file layout may have changed */
250411df2dfbSYan, Zheng 		ci->i_layout = grant->layout;
2505f98a128aSYan, Zheng 		/* size/truncate_seq? */
2506f98a128aSYan, Zheng 		queue_trunc = ceph_fill_file_size(inode, issued,
2507f98a128aSYan, Zheng 					le32_to_cpu(grant->truncate_seq),
2508f98a128aSYan, Zheng 					le64_to_cpu(grant->truncate_size),
2509f98a128aSYan, Zheng 					size);
2510a8599bd8SSage Weil 		/* max size increase? */
25115e62ad30SYan, Zheng 		if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
2512f98a128aSYan, Zheng 			dout("max_size %lld -> %llu\n",
2513f98a128aSYan, Zheng 			     ci->i_max_size, max_size);
2514a8599bd8SSage Weil 			ci->i_max_size = max_size;
2515a8599bd8SSage Weil 			if (max_size >= ci->i_wanted_max_size) {
2516a8599bd8SSage Weil 				ci->i_wanted_max_size = 0;  /* reset */
2517a8599bd8SSage Weil 				ci->i_requested_max_size = 0;
2518a8599bd8SSage Weil 			}
2519ab6c2c3eSFabian Frederick 			wake = true;
2520a8599bd8SSage Weil 		}
2521f98a128aSYan, Zheng 	}
2522a8599bd8SSage Weil 
2523a8599bd8SSage Weil 	/* check cap bits */
2524a8599bd8SSage Weil 	wanted = __ceph_caps_wanted(ci);
2525a8599bd8SSage Weil 	used = __ceph_caps_used(ci);
2526a8599bd8SSage Weil 	dirty = __ceph_caps_dirty(ci);
2527a8599bd8SSage Weil 	dout(" my wanted = %s, used = %s, dirty %s\n",
2528a8599bd8SSage Weil 	     ceph_cap_string(wanted),
2529a8599bd8SSage Weil 	     ceph_cap_string(used),
2530a8599bd8SSage Weil 	     ceph_cap_string(dirty));
2531a8599bd8SSage Weil 	if (wanted != le32_to_cpu(grant->wanted)) {
2532a8599bd8SSage Weil 		dout("mds wanted %s -> %s\n",
2533a8599bd8SSage Weil 		     ceph_cap_string(le32_to_cpu(grant->wanted)),
2534a8599bd8SSage Weil 		     ceph_cap_string(wanted));
2535390306c3SYan, Zheng 		/* imported cap may not have correct mds_wanted */
2536390306c3SYan, Zheng 		if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
2537390306c3SYan, Zheng 			check_caps = 1;
2538a8599bd8SSage Weil 	}
2539a8599bd8SSage Weil 
2540a8599bd8SSage Weil 	/* revocation, grant, or no-op? */
2541a8599bd8SSage Weil 	if (cap->issued & ~newcaps) {
25423b454c49SSage Weil 		int revoking = cap->issued & ~newcaps;
25433b454c49SSage Weil 
25443b454c49SSage Weil 		dout("revocation: %s -> %s (revoking %s)\n",
25453b454c49SSage Weil 		     ceph_cap_string(cap->issued),
25463b454c49SSage Weil 		     ceph_cap_string(newcaps),
25473b454c49SSage Weil 		     ceph_cap_string(revoking));
25480eb6cd49SSage Weil 		if (revoking & used & CEPH_CAP_FILE_BUFFER)
2549ab6c2c3eSFabian Frederick 			writeback = true;  /* initiate writeback; will delay ack */
25503b454c49SSage Weil 		else if (revoking == CEPH_CAP_FILE_CACHE &&
25513b454c49SSage Weil 			 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
25523b454c49SSage Weil 			 queue_invalidate)
25533b454c49SSage Weil 			; /* do nothing yet, invalidation will be queued */
25543b454c49SSage Weil 		else if (cap == ci->i_auth_cap)
25553b454c49SSage Weil 			check_caps = 1; /* check auth cap only */
25563b454c49SSage Weil 		else
25573b454c49SSage Weil 			check_caps = 2; /* check all caps */
2558a8599bd8SSage Weil 		cap->issued = newcaps;
2559978097c9SSage Weil 		cap->implemented |= newcaps;
2560a8599bd8SSage Weil 	} else if (cap->issued == newcaps) {
2561a8599bd8SSage Weil 		dout("caps unchanged: %s -> %s\n",
2562a8599bd8SSage Weil 		     ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2563a8599bd8SSage Weil 	} else {
2564a8599bd8SSage Weil 		dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2565a8599bd8SSage Weil 		     ceph_cap_string(newcaps));
25666ee6b953SYan, Zheng 		/* non-auth MDS is revoking the newly grant caps ? */
25676ee6b953SYan, Zheng 		if (cap == ci->i_auth_cap &&
25686ee6b953SYan, Zheng 		    __ceph_caps_revoking_other(ci, cap, newcaps))
25696ee6b953SYan, Zheng 		    check_caps = 2;
25706ee6b953SYan, Zheng 
2571a8599bd8SSage Weil 		cap->issued = newcaps;
2572a8599bd8SSage Weil 		cap->implemented |= newcaps; /* add bits only, to
2573a8599bd8SSage Weil 					      * avoid stepping on a
2574a8599bd8SSage Weil 					      * pending revocation */
2575ab6c2c3eSFabian Frederick 		wake = true;
2576a8599bd8SSage Weil 	}
2577978097c9SSage Weil 	BUG_ON(cap->issued & ~cap->implemented);
2578a8599bd8SSage Weil 
2579be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
258099ccbd22SMilosz Tanski 
25812cd698beSYan, Zheng 	if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
25822cd698beSYan, Zheng 		down_write(&mdsc->snap_rwsem);
25832cd698beSYan, Zheng 		ceph_update_snap_trace(mdsc, snaptrace,
25842cd698beSYan, Zheng 				       snaptrace + snaptrace_len, false);
25852cd698beSYan, Zheng 		downgrade_write(&mdsc->snap_rwsem);
25862cd698beSYan, Zheng 		kick_flushing_inode_caps(mdsc, session, inode);
25872cd698beSYan, Zheng 		up_read(&mdsc->snap_rwsem);
25882cd698beSYan, Zheng 		if (newcaps & ~issued)
2589ab6c2c3eSFabian Frederick 			wake = true;
25902cd698beSYan, Zheng 	}
25912cd698beSYan, Zheng 
2592c6bcda6fSYan, Zheng 	if (queue_trunc) {
2593c6bcda6fSYan, Zheng 		ceph_queue_vmtruncate(inode);
2594c6bcda6fSYan, Zheng 		ceph_queue_revalidate(inode);
2595c6bcda6fSYan, Zheng 	} else if (queue_revalidate)
2596c6bcda6fSYan, Zheng 		ceph_queue_revalidate(inode);
2597c6bcda6fSYan, Zheng 
25983c6f6b79SSage Weil 	if (writeback)
2599a8599bd8SSage Weil 		/*
2600a8599bd8SSage Weil 		 * queue inode for writeback: we can't actually call
2601a8599bd8SSage Weil 		 * filemap_write_and_wait, etc. from message handler
2602a8599bd8SSage Weil 		 * context.
2603a8599bd8SSage Weil 		 */
26043c6f6b79SSage Weil 		ceph_queue_writeback(inode);
26053c6f6b79SSage Weil 	if (queue_invalidate)
26063c6f6b79SSage Weil 		ceph_queue_invalidate(inode);
2607ca20c991SYan, Zheng 	if (deleted_inode)
2608ca20c991SYan, Zheng 		invalidate_aliases(inode);
2609a8599bd8SSage Weil 	if (wake)
261003066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
261115637c8bSSage Weil 
261215637c8bSSage Weil 	if (check_caps == 1)
261315637c8bSSage Weil 		ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
261415637c8bSSage Weil 				session);
261515637c8bSSage Weil 	else if (check_caps == 2)
261615637c8bSSage Weil 		ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
261715637c8bSSage Weil 	else
261815637c8bSSage Weil 		mutex_unlock(&session->s_mutex);
2619a8599bd8SSage Weil }
2620a8599bd8SSage Weil 
2621a8599bd8SSage Weil /*
2622a8599bd8SSage Weil  * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2623a8599bd8SSage Weil  * MDS has been safely committed.
2624a8599bd8SSage Weil  */
26256df058c0SSage Weil static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
2626a8599bd8SSage Weil 				 struct ceph_mds_caps *m,
2627a8599bd8SSage Weil 				 struct ceph_mds_session *session,
2628a8599bd8SSage Weil 				 struct ceph_cap *cap)
2629be655596SSage Weil 	__releases(ci->i_ceph_lock)
2630a8599bd8SSage Weil {
2631a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
26323d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2633a8599bd8SSage Weil 	unsigned seq = le32_to_cpu(m->seq);
2634a8599bd8SSage Weil 	int dirty = le32_to_cpu(m->dirty);
2635a8599bd8SSage Weil 	int cleaned = 0;
2636afcdaea3SSage Weil 	int drop = 0;
2637a8599bd8SSage Weil 	int i;
2638a8599bd8SSage Weil 
2639a8599bd8SSage Weil 	for (i = 0; i < CEPH_CAP_BITS; i++)
2640a8599bd8SSage Weil 		if ((dirty & (1 << i)) &&
26413231300bSYan, Zheng 		    (u16)flush_tid == ci->i_cap_flush_tid[i])
2642a8599bd8SSage Weil 			cleaned |= 1 << i;
2643a8599bd8SSage Weil 
2644a8599bd8SSage Weil 	dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2645a8599bd8SSage Weil 	     " flushing %s -> %s\n",
2646a8599bd8SSage Weil 	     inode, session->s_mds, seq, ceph_cap_string(dirty),
2647a8599bd8SSage Weil 	     ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2648a8599bd8SSage Weil 	     ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2649a8599bd8SSage Weil 
2650a8599bd8SSage Weil 	if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2651a8599bd8SSage Weil 		goto out;
2652a8599bd8SSage Weil 
2653a8599bd8SSage Weil 	ci->i_flushing_caps &= ~cleaned;
2654a8599bd8SSage Weil 
2655a8599bd8SSage Weil 	spin_lock(&mdsc->cap_dirty_lock);
2656a8599bd8SSage Weil 	if (ci->i_flushing_caps == 0) {
2657a8599bd8SSage Weil 		list_del_init(&ci->i_flushing_item);
2658a8599bd8SSage Weil 		if (!list_empty(&session->s_cap_flushing))
2659a8599bd8SSage Weil 			dout(" mds%d still flushing cap on %p\n",
2660a8599bd8SSage Weil 			     session->s_mds,
2661a8599bd8SSage Weil 			     &list_entry(session->s_cap_flushing.next,
2662a8599bd8SSage Weil 					 struct ceph_inode_info,
2663a8599bd8SSage Weil 					 i_flushing_item)->vfs_inode);
2664a8599bd8SSage Weil 		mdsc->num_cap_flushing--;
266503066f23SYehuda Sadeh 		wake_up_all(&mdsc->cap_flushing_wq);
2666a8599bd8SSage Weil 		dout(" inode %p now !flushing\n", inode);
2667afcdaea3SSage Weil 
2668afcdaea3SSage Weil 		if (ci->i_dirty_caps == 0) {
2669a8599bd8SSage Weil 			dout(" inode %p now clean\n", inode);
2670afcdaea3SSage Weil 			BUG_ON(!list_empty(&ci->i_dirty_item));
2671afcdaea3SSage Weil 			drop = 1;
26727d8cb26dSSage Weil 			if (ci->i_wrbuffer_ref_head == 0) {
26737d8cb26dSSage Weil 				BUG_ON(!ci->i_head_snapc);
26747d8cb26dSSage Weil 				ceph_put_snap_context(ci->i_head_snapc);
26757d8cb26dSSage Weil 				ci->i_head_snapc = NULL;
26767d8cb26dSSage Weil 			}
267776e3b390SSage Weil 		} else {
267876e3b390SSage Weil 			BUG_ON(list_empty(&ci->i_dirty_item));
2679afcdaea3SSage Weil 		}
2680a8599bd8SSage Weil 	}
2681a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_dirty_lock);
268203066f23SYehuda Sadeh 	wake_up_all(&ci->i_cap_wq);
2683a8599bd8SSage Weil 
2684a8599bd8SSage Weil out:
2685be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2686afcdaea3SSage Weil 	if (drop)
2687a8599bd8SSage Weil 		iput(inode);
2688a8599bd8SSage Weil }
2689a8599bd8SSage Weil 
2690a8599bd8SSage Weil /*
2691a8599bd8SSage Weil  * Handle FLUSHSNAP_ACK.  MDS has flushed snap data to disk and we can
2692a8599bd8SSage Weil  * throw away our cap_snap.
2693a8599bd8SSage Weil  *
2694a8599bd8SSage Weil  * Caller hold s_mutex.
2695a8599bd8SSage Weil  */
26966df058c0SSage Weil static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
2697a8599bd8SSage Weil 				     struct ceph_mds_caps *m,
2698a8599bd8SSage Weil 				     struct ceph_mds_session *session)
2699a8599bd8SSage Weil {
2700a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2701a8599bd8SSage Weil 	u64 follows = le64_to_cpu(m->snap_follows);
2702a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
2703a8599bd8SSage Weil 	int drop = 0;
2704a8599bd8SSage Weil 
2705a8599bd8SSage Weil 	dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2706a8599bd8SSage Weil 	     inode, ci, session->s_mds, follows);
2707a8599bd8SSage Weil 
2708be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2709a8599bd8SSage Weil 	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2710a8599bd8SSage Weil 		if (capsnap->follows == follows) {
2711a8599bd8SSage Weil 			if (capsnap->flush_tid != flush_tid) {
2712a8599bd8SSage Weil 				dout(" cap_snap %p follows %lld tid %lld !="
2713a8599bd8SSage Weil 				     " %lld\n", capsnap, follows,
2714a8599bd8SSage Weil 				     flush_tid, capsnap->flush_tid);
2715a8599bd8SSage Weil 				break;
2716a8599bd8SSage Weil 			}
2717a8599bd8SSage Weil 			WARN_ON(capsnap->dirty_pages || capsnap->writing);
2718819ccbfaSSage Weil 			dout(" removing %p cap_snap %p follows %lld\n",
2719819ccbfaSSage Weil 			     inode, capsnap, follows);
2720a8599bd8SSage Weil 			ceph_put_snap_context(capsnap->context);
2721a8599bd8SSage Weil 			list_del(&capsnap->ci_item);
2722a8599bd8SSage Weil 			list_del(&capsnap->flushing_item);
2723a8599bd8SSage Weil 			ceph_put_cap_snap(capsnap);
2724a8599bd8SSage Weil 			drop = 1;
2725a8599bd8SSage Weil 			break;
2726a8599bd8SSage Weil 		} else {
2727a8599bd8SSage Weil 			dout(" skipping cap_snap %p follows %lld\n",
2728a8599bd8SSage Weil 			     capsnap, capsnap->follows);
2729a8599bd8SSage Weil 		}
2730a8599bd8SSage Weil 	}
2731be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2732a8599bd8SSage Weil 	if (drop)
2733a8599bd8SSage Weil 		iput(inode);
2734a8599bd8SSage Weil }
2735a8599bd8SSage Weil 
2736a8599bd8SSage Weil /*
2737a8599bd8SSage Weil  * Handle TRUNC from MDS, indicating file truncation.
2738a8599bd8SSage Weil  *
2739a8599bd8SSage Weil  * caller hold s_mutex.
2740a8599bd8SSage Weil  */
2741a8599bd8SSage Weil static void handle_cap_trunc(struct inode *inode,
2742a8599bd8SSage Weil 			     struct ceph_mds_caps *trunc,
2743a8599bd8SSage Weil 			     struct ceph_mds_session *session)
2744be655596SSage Weil 	__releases(ci->i_ceph_lock)
2745a8599bd8SSage Weil {
2746a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2747a8599bd8SSage Weil 	int mds = session->s_mds;
2748a8599bd8SSage Weil 	int seq = le32_to_cpu(trunc->seq);
2749a8599bd8SSage Weil 	u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2750a8599bd8SSage Weil 	u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2751a8599bd8SSage Weil 	u64 size = le64_to_cpu(trunc->size);
2752a8599bd8SSage Weil 	int implemented = 0;
2753a8599bd8SSage Weil 	int dirty = __ceph_caps_dirty(ci);
2754a8599bd8SSage Weil 	int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2755a8599bd8SSage Weil 	int queue_trunc = 0;
2756a8599bd8SSage Weil 
2757a8599bd8SSage Weil 	issued |= implemented | dirty;
2758a8599bd8SSage Weil 
2759a8599bd8SSage Weil 	dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2760a8599bd8SSage Weil 	     inode, mds, seq, truncate_size, truncate_seq);
2761a8599bd8SSage Weil 	queue_trunc = ceph_fill_file_size(inode, issued,
2762a8599bd8SSage Weil 					  truncate_seq, truncate_size, size);
2763be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2764a8599bd8SSage Weil 
276599ccbd22SMilosz Tanski 	if (queue_trunc) {
27663c6f6b79SSage Weil 		ceph_queue_vmtruncate(inode);
276799ccbd22SMilosz Tanski 		ceph_fscache_invalidate(inode);
276899ccbd22SMilosz Tanski 	}
2769a8599bd8SSage Weil }
2770a8599bd8SSage Weil 
2771a8599bd8SSage Weil /*
2772a8599bd8SSage Weil  * Handle EXPORT from MDS.  Cap is being migrated _from_ this mds to a
2773a8599bd8SSage Weil  * different one.  If we are the most recent migration we've seen (as
2774a8599bd8SSage Weil  * indicated by mseq), make note of the migrating cap bits for the
2775a8599bd8SSage Weil  * duration (until we see the corresponding IMPORT).
2776a8599bd8SSage Weil  *
2777a8599bd8SSage Weil  * caller holds s_mutex
2778a8599bd8SSage Weil  */
2779a8599bd8SSage Weil static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
278011df2dfbSYan, Zheng 			      struct ceph_mds_cap_peer *ph,
278111df2dfbSYan, Zheng 			      struct ceph_mds_session *session)
2782a8599bd8SSage Weil {
2783db354052SSage Weil 	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
278411df2dfbSYan, Zheng 	struct ceph_mds_session *tsession = NULL;
2785d9df2783SYan, Zheng 	struct ceph_cap *cap, *tcap, *new_cap = NULL;
2786a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
278711df2dfbSYan, Zheng 	u64 t_cap_id;
2788a8599bd8SSage Weil 	unsigned mseq = le32_to_cpu(ex->migrate_seq);
278911df2dfbSYan, Zheng 	unsigned t_seq, t_mseq;
279011df2dfbSYan, Zheng 	int target, issued;
279111df2dfbSYan, Zheng 	int mds = session->s_mds;
2792a8599bd8SSage Weil 
279311df2dfbSYan, Zheng 	if (ph) {
279411df2dfbSYan, Zheng 		t_cap_id = le64_to_cpu(ph->cap_id);
279511df2dfbSYan, Zheng 		t_seq = le32_to_cpu(ph->seq);
279611df2dfbSYan, Zheng 		t_mseq = le32_to_cpu(ph->mseq);
279711df2dfbSYan, Zheng 		target = le32_to_cpu(ph->mds);
279811df2dfbSYan, Zheng 	} else {
279911df2dfbSYan, Zheng 		t_cap_id = t_seq = t_mseq = 0;
280011df2dfbSYan, Zheng 		target = -1;
280111df2dfbSYan, Zheng 	}
2802a8599bd8SSage Weil 
280311df2dfbSYan, Zheng 	dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
280411df2dfbSYan, Zheng 	     inode, ci, mds, mseq, target);
280511df2dfbSYan, Zheng retry:
2806be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
280711df2dfbSYan, Zheng 	cap = __get_cap_for_mds(ci, mds);
2808ca665e02SYan, Zheng 	if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
280911df2dfbSYan, Zheng 		goto out_unlock;
2810a8599bd8SSage Weil 
281111df2dfbSYan, Zheng 	if (target < 0) {
281211df2dfbSYan, Zheng 		__ceph_remove_cap(cap, false);
281311df2dfbSYan, Zheng 		goto out_unlock;
2814a8599bd8SSage Weil 	}
2815a8599bd8SSage Weil 
2816154f42c2SSage Weil 	/*
281711df2dfbSYan, Zheng 	 * now we know we haven't received the cap import message yet
281811df2dfbSYan, Zheng 	 * because the exported cap still exist.
2819154f42c2SSage Weil 	 */
2820db354052SSage Weil 
282111df2dfbSYan, Zheng 	issued = cap->issued;
282211df2dfbSYan, Zheng 	WARN_ON(issued != cap->implemented);
282311df2dfbSYan, Zheng 
282411df2dfbSYan, Zheng 	tcap = __get_cap_for_mds(ci, target);
282511df2dfbSYan, Zheng 	if (tcap) {
282611df2dfbSYan, Zheng 		/* already have caps from the target */
282711df2dfbSYan, Zheng 		if (tcap->cap_id != t_cap_id ||
282811df2dfbSYan, Zheng 		    ceph_seq_cmp(tcap->seq, t_seq) < 0) {
282911df2dfbSYan, Zheng 			dout(" updating import cap %p mds%d\n", tcap, target);
283011df2dfbSYan, Zheng 			tcap->cap_id = t_cap_id;
283111df2dfbSYan, Zheng 			tcap->seq = t_seq - 1;
283211df2dfbSYan, Zheng 			tcap->issue_seq = t_seq - 1;
283311df2dfbSYan, Zheng 			tcap->mseq = t_mseq;
283411df2dfbSYan, Zheng 			tcap->issued |= issued;
283511df2dfbSYan, Zheng 			tcap->implemented |= issued;
283611df2dfbSYan, Zheng 			if (cap == ci->i_auth_cap)
283711df2dfbSYan, Zheng 				ci->i_auth_cap = tcap;
283811df2dfbSYan, Zheng 			if (ci->i_flushing_caps && ci->i_auth_cap == tcap) {
2839db354052SSage Weil 				spin_lock(&mdsc->cap_dirty_lock);
284011df2dfbSYan, Zheng 				list_move_tail(&ci->i_flushing_item,
284111df2dfbSYan, Zheng 					       &tcap->session->s_cap_flushing);
2842db354052SSage Weil 				spin_unlock(&mdsc->cap_dirty_lock);
2843a8599bd8SSage Weil 			}
2844a8599bd8SSage Weil 		}
284511df2dfbSYan, Zheng 		__ceph_remove_cap(cap, false);
284611df2dfbSYan, Zheng 		goto out_unlock;
2847d9df2783SYan, Zheng 	} else if (tsession) {
284811df2dfbSYan, Zheng 		/* add placeholder for the export tagert */
2849d9df2783SYan, Zheng 		int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
285011df2dfbSYan, Zheng 		ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
2851d9df2783SYan, Zheng 			     t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
2852d9df2783SYan, Zheng 
2853d9df2783SYan, Zheng 		__ceph_remove_cap(cap, false);
2854d9df2783SYan, Zheng 		goto out_unlock;
285511df2dfbSYan, Zheng 	}
2856a8599bd8SSage Weil 
2857be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
285811df2dfbSYan, Zheng 	mutex_unlock(&session->s_mutex);
285911df2dfbSYan, Zheng 
286011df2dfbSYan, Zheng 	/* open target session */
286111df2dfbSYan, Zheng 	tsession = ceph_mdsc_open_export_target_session(mdsc, target);
286211df2dfbSYan, Zheng 	if (!IS_ERR(tsession)) {
286311df2dfbSYan, Zheng 		if (mds > target) {
286411df2dfbSYan, Zheng 			mutex_lock(&session->s_mutex);
286511df2dfbSYan, Zheng 			mutex_lock_nested(&tsession->s_mutex,
286611df2dfbSYan, Zheng 					  SINGLE_DEPTH_NESTING);
286711df2dfbSYan, Zheng 		} else {
286811df2dfbSYan, Zheng 			mutex_lock(&tsession->s_mutex);
286911df2dfbSYan, Zheng 			mutex_lock_nested(&session->s_mutex,
287011df2dfbSYan, Zheng 					  SINGLE_DEPTH_NESTING);
287111df2dfbSYan, Zheng 		}
287211df2dfbSYan, Zheng 		ceph_add_cap_releases(mdsc, tsession);
2873d9df2783SYan, Zheng 		new_cap = ceph_get_cap(mdsc, NULL);
287411df2dfbSYan, Zheng 	} else {
287511df2dfbSYan, Zheng 		WARN_ON(1);
287611df2dfbSYan, Zheng 		tsession = NULL;
287711df2dfbSYan, Zheng 		target = -1;
287811df2dfbSYan, Zheng 	}
287911df2dfbSYan, Zheng 	goto retry;
288011df2dfbSYan, Zheng 
288111df2dfbSYan, Zheng out_unlock:
288211df2dfbSYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
288311df2dfbSYan, Zheng 	mutex_unlock(&session->s_mutex);
288411df2dfbSYan, Zheng 	if (tsession) {
288511df2dfbSYan, Zheng 		mutex_unlock(&tsession->s_mutex);
288611df2dfbSYan, Zheng 		ceph_put_mds_session(tsession);
288711df2dfbSYan, Zheng 	}
2888d9df2783SYan, Zheng 	if (new_cap)
2889d9df2783SYan, Zheng 		ceph_put_cap(mdsc, new_cap);
2890a8599bd8SSage Weil }
2891a8599bd8SSage Weil 
2892a8599bd8SSage Weil /*
28932cd698beSYan, Zheng  * Handle cap IMPORT.
2894a8599bd8SSage Weil  *
28952cd698beSYan, Zheng  * caller holds s_mutex. acquires i_ceph_lock
2896a8599bd8SSage Weil  */
2897a8599bd8SSage Weil static void handle_cap_import(struct ceph_mds_client *mdsc,
2898a8599bd8SSage Weil 			      struct inode *inode, struct ceph_mds_caps *im,
28994ee6a914SYan, Zheng 			      struct ceph_mds_cap_peer *ph,
2900a8599bd8SSage Weil 			      struct ceph_mds_session *session,
29012cd698beSYan, Zheng 			      struct ceph_cap **target_cap, int *old_issued)
29022cd698beSYan, Zheng 	__acquires(ci->i_ceph_lock)
2903a8599bd8SSage Weil {
2904a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
29052cd698beSYan, Zheng 	struct ceph_cap *cap, *ocap, *new_cap = NULL;
2906a8599bd8SSage Weil 	int mds = session->s_mds;
29072cd698beSYan, Zheng 	int issued;
29082cd698beSYan, Zheng 	unsigned caps = le32_to_cpu(im->caps);
2909a8599bd8SSage Weil 	unsigned wanted = le32_to_cpu(im->wanted);
2910a8599bd8SSage Weil 	unsigned seq = le32_to_cpu(im->seq);
2911a8599bd8SSage Weil 	unsigned mseq = le32_to_cpu(im->migrate_seq);
2912a8599bd8SSage Weil 	u64 realmino = le64_to_cpu(im->realm);
2913a8599bd8SSage Weil 	u64 cap_id = le64_to_cpu(im->cap_id);
29144ee6a914SYan, Zheng 	u64 p_cap_id;
29154ee6a914SYan, Zheng 	int peer;
2916a8599bd8SSage Weil 
29174ee6a914SYan, Zheng 	if (ph) {
29184ee6a914SYan, Zheng 		p_cap_id = le64_to_cpu(ph->cap_id);
29194ee6a914SYan, Zheng 		peer = le32_to_cpu(ph->mds);
2920a8599bd8SSage Weil 	} else {
29214ee6a914SYan, Zheng 		p_cap_id = 0;
29224ee6a914SYan, Zheng 		peer = -1;
2923a8599bd8SSage Weil 	}
2924a8599bd8SSage Weil 
29254ee6a914SYan, Zheng 	dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
29264ee6a914SYan, Zheng 	     inode, ci, mds, mseq, peer);
29274ee6a914SYan, Zheng 
2928d9df2783SYan, Zheng retry:
29294ee6a914SYan, Zheng 	spin_lock(&ci->i_ceph_lock);
2930d9df2783SYan, Zheng 	cap = __get_cap_for_mds(ci, mds);
2931d9df2783SYan, Zheng 	if (!cap) {
2932d9df2783SYan, Zheng 		if (!new_cap) {
2933d9df2783SYan, Zheng 			spin_unlock(&ci->i_ceph_lock);
2934d9df2783SYan, Zheng 			new_cap = ceph_get_cap(mdsc, NULL);
2935d9df2783SYan, Zheng 			goto retry;
2936d9df2783SYan, Zheng 		}
29372cd698beSYan, Zheng 		cap = new_cap;
29382cd698beSYan, Zheng 	} else {
29392cd698beSYan, Zheng 		if (new_cap) {
29402cd698beSYan, Zheng 			ceph_put_cap(mdsc, new_cap);
29412cd698beSYan, Zheng 			new_cap = NULL;
29422cd698beSYan, Zheng 		}
2943d9df2783SYan, Zheng 	}
2944d9df2783SYan, Zheng 
29452cd698beSYan, Zheng 	__ceph_caps_issued(ci, &issued);
29462cd698beSYan, Zheng 	issued |= __ceph_caps_dirty(ci);
29472cd698beSYan, Zheng 
29482cd698beSYan, Zheng 	ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
2949d9df2783SYan, Zheng 		     realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
2950d9df2783SYan, Zheng 
29512cd698beSYan, Zheng 	ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
29522cd698beSYan, Zheng 	if (ocap && ocap->cap_id == p_cap_id) {
29534ee6a914SYan, Zheng 		dout(" remove export cap %p mds%d flags %d\n",
29542cd698beSYan, Zheng 		     ocap, peer, ph->flags);
29554ee6a914SYan, Zheng 		if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
29562cd698beSYan, Zheng 		    (ocap->seq != le32_to_cpu(ph->seq) ||
29572cd698beSYan, Zheng 		     ocap->mseq != le32_to_cpu(ph->mseq))) {
29584ee6a914SYan, Zheng 			pr_err("handle_cap_import: mismatched seq/mseq: "
29594ee6a914SYan, Zheng 			       "ino (%llx.%llx) mds%d seq %d mseq %d "
29604ee6a914SYan, Zheng 			       "importer mds%d has peer seq %d mseq %d\n",
29612cd698beSYan, Zheng 			       ceph_vinop(inode), peer, ocap->seq,
29622cd698beSYan, Zheng 			       ocap->mseq, mds, le32_to_cpu(ph->seq),
29634ee6a914SYan, Zheng 			       le32_to_cpu(ph->mseq));
29644ee6a914SYan, Zheng 		}
29652cd698beSYan, Zheng 		__ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
29664ee6a914SYan, Zheng 	}
29674ee6a914SYan, Zheng 
29684ee6a914SYan, Zheng 	/* make sure we re-request max_size, if necessary */
29694ee6a914SYan, Zheng 	ci->i_wanted_max_size = 0;
29704ee6a914SYan, Zheng 	ci->i_requested_max_size = 0;
29714ee6a914SYan, Zheng 
29722cd698beSYan, Zheng 	*old_issued = issued;
29732cd698beSYan, Zheng 	*target_cap = cap;
2974a8599bd8SSage Weil }
2975a8599bd8SSage Weil 
2976a8599bd8SSage Weil /*
2977a8599bd8SSage Weil  * Handle a caps message from the MDS.
2978a8599bd8SSage Weil  *
2979a8599bd8SSage Weil  * Identify the appropriate session, inode, and call the right handler
2980a8599bd8SSage Weil  * based on the cap op.
2981a8599bd8SSage Weil  */
2982a8599bd8SSage Weil void ceph_handle_caps(struct ceph_mds_session *session,
2983a8599bd8SSage Weil 		      struct ceph_msg *msg)
2984a8599bd8SSage Weil {
2985a8599bd8SSage Weil 	struct ceph_mds_client *mdsc = session->s_mdsc;
29863d14c5d2SYehuda Sadeh 	struct super_block *sb = mdsc->fsc->sb;
2987a8599bd8SSage Weil 	struct inode *inode;
2988be655596SSage Weil 	struct ceph_inode_info *ci;
2989a8599bd8SSage Weil 	struct ceph_cap *cap;
2990a8599bd8SSage Weil 	struct ceph_mds_caps *h;
29914ee6a914SYan, Zheng 	struct ceph_mds_cap_peer *peer = NULL;
29922600d2ddSSage Weil 	int mds = session->s_mds;
29932cd698beSYan, Zheng 	int op, issued;
29943d7ded4dSSage Weil 	u32 seq, mseq;
2995a8599bd8SSage Weil 	struct ceph_vino vino;
2996a8599bd8SSage Weil 	u64 cap_id;
2997a8599bd8SSage Weil 	u64 size, max_size;
29986df058c0SSage Weil 	u64 tid;
299970edb55bSSage Weil 	void *snaptrace;
3000ce1fbc8dSSage Weil 	size_t snaptrace_len;
3001ce1fbc8dSSage Weil 	void *flock;
30024ee6a914SYan, Zheng 	void *end;
3003ce1fbc8dSSage Weil 	u32 flock_len;
3004a8599bd8SSage Weil 
3005a8599bd8SSage Weil 	dout("handle_caps from mds%d\n", mds);
3006a8599bd8SSage Weil 
3007a8599bd8SSage Weil 	/* decode */
30084ee6a914SYan, Zheng 	end = msg->front.iov_base + msg->front.iov_len;
30096df058c0SSage Weil 	tid = le64_to_cpu(msg->hdr.tid);
3010a8599bd8SSage Weil 	if (msg->front.iov_len < sizeof(*h))
3011a8599bd8SSage Weil 		goto bad;
3012a8599bd8SSage Weil 	h = msg->front.iov_base;
3013a8599bd8SSage Weil 	op = le32_to_cpu(h->op);
3014a8599bd8SSage Weil 	vino.ino = le64_to_cpu(h->ino);
3015a8599bd8SSage Weil 	vino.snap = CEPH_NOSNAP;
3016a8599bd8SSage Weil 	cap_id = le64_to_cpu(h->cap_id);
3017a8599bd8SSage Weil 	seq = le32_to_cpu(h->seq);
30183d7ded4dSSage Weil 	mseq = le32_to_cpu(h->migrate_seq);
3019a8599bd8SSage Weil 	size = le64_to_cpu(h->size);
3020a8599bd8SSage Weil 	max_size = le64_to_cpu(h->max_size);
3021a8599bd8SSage Weil 
3022ce1fbc8dSSage Weil 	snaptrace = h + 1;
3023ce1fbc8dSSage Weil 	snaptrace_len = le32_to_cpu(h->snap_trace_len);
3024ce1fbc8dSSage Weil 
3025ce1fbc8dSSage Weil 	if (le16_to_cpu(msg->hdr.version) >= 2) {
30264ee6a914SYan, Zheng 		void *p = snaptrace + snaptrace_len;
3027ce1fbc8dSSage Weil 		ceph_decode_32_safe(&p, end, flock_len, bad);
30284ee6a914SYan, Zheng 		if (p + flock_len > end)
30294ee6a914SYan, Zheng 			goto bad;
3030ce1fbc8dSSage Weil 		flock = p;
3031ce1fbc8dSSage Weil 	} else {
3032ce1fbc8dSSage Weil 		flock = NULL;
3033ce1fbc8dSSage Weil 		flock_len = 0;
3034ce1fbc8dSSage Weil 	}
3035ce1fbc8dSSage Weil 
30364ee6a914SYan, Zheng 	if (le16_to_cpu(msg->hdr.version) >= 3) {
30374ee6a914SYan, Zheng 		if (op == CEPH_CAP_OP_IMPORT) {
30384ee6a914SYan, Zheng 			void *p = flock + flock_len;
30394ee6a914SYan, Zheng 			if (p + sizeof(*peer) > end)
30404ee6a914SYan, Zheng 				goto bad;
30414ee6a914SYan, Zheng 			peer = p;
304211df2dfbSYan, Zheng 		} else if (op == CEPH_CAP_OP_EXPORT) {
304311df2dfbSYan, Zheng 			/* recorded in unused fields */
304411df2dfbSYan, Zheng 			peer = (void *)&h->size;
30454ee6a914SYan, Zheng 		}
30464ee6a914SYan, Zheng 	}
30474ee6a914SYan, Zheng 
30486cd3bcadSYan, Zheng 	/* lookup ino */
30496cd3bcadSYan, Zheng 	inode = ceph_find_inode(sb, vino);
30506cd3bcadSYan, Zheng 	ci = ceph_inode(inode);
30516cd3bcadSYan, Zheng 	dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
30526cd3bcadSYan, Zheng 	     vino.snap, inode);
30536cd3bcadSYan, Zheng 
3054a8599bd8SSage Weil 	mutex_lock(&session->s_mutex);
3055a8599bd8SSage Weil 	session->s_seq++;
3056a8599bd8SSage Weil 	dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3057a8599bd8SSage Weil 	     (unsigned)seq);
3058a8599bd8SSage Weil 
305966f58691SYan, Zheng 	if (op == CEPH_CAP_OP_IMPORT)
306066f58691SYan, Zheng 		ceph_add_cap_releases(mdsc, session);
306166f58691SYan, Zheng 
3062a8599bd8SSage Weil 	if (!inode) {
3063a8599bd8SSage Weil 		dout(" i don't have ino %llx\n", vino.ino);
30643d7ded4dSSage Weil 
3065a096b09aSYan, Zheng 		if (op == CEPH_CAP_OP_IMPORT) {
3066a096b09aSYan, Zheng 			spin_lock(&session->s_cap_lock);
30673d7ded4dSSage Weil 			__queue_cap_release(session, vino.ino, cap_id,
30683d7ded4dSSage Weil 					    mseq, seq);
3069a096b09aSYan, Zheng 			spin_unlock(&session->s_cap_lock);
3070a096b09aSYan, Zheng 		}
307121b559deSGreg Farnum 		goto flush_cap_releases;
3072a8599bd8SSage Weil 	}
3073a8599bd8SSage Weil 
3074a8599bd8SSage Weil 	/* these will work even if we don't have a cap yet */
3075a8599bd8SSage Weil 	switch (op) {
3076a8599bd8SSage Weil 	case CEPH_CAP_OP_FLUSHSNAP_ACK:
30776df058c0SSage Weil 		handle_cap_flushsnap_ack(inode, tid, h, session);
3078a8599bd8SSage Weil 		goto done;
3079a8599bd8SSage Weil 
3080a8599bd8SSage Weil 	case CEPH_CAP_OP_EXPORT:
308111df2dfbSYan, Zheng 		handle_cap_export(inode, h, peer, session);
308211df2dfbSYan, Zheng 		goto done_unlocked;
3083a8599bd8SSage Weil 
3084a8599bd8SSage Weil 	case CEPH_CAP_OP_IMPORT:
30854ee6a914SYan, Zheng 		handle_cap_import(mdsc, inode, h, peer, session,
30862cd698beSYan, Zheng 				  &cap, &issued);
30872cd698beSYan, Zheng 		handle_cap_grant(mdsc, inode, h,  snaptrace, snaptrace_len,
30882cd698beSYan, Zheng 				 msg->middle, session, cap, issued);
30892cd698beSYan, Zheng 		goto done_unlocked;
3090a8599bd8SSage Weil 	}
3091a8599bd8SSage Weil 
3092a8599bd8SSage Weil 	/* the rest require a cap */
3093be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
3094a8599bd8SSage Weil 	cap = __get_cap_for_mds(ceph_inode(inode), mds);
3095a8599bd8SSage Weil 	if (!cap) {
30969dbd412fSSage Weil 		dout(" no cap on %p ino %llx.%llx from mds%d\n",
3097a8599bd8SSage Weil 		     inode, ceph_ino(inode), ceph_snap(inode), mds);
3098be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
309921b559deSGreg Farnum 		goto flush_cap_releases;
3100a8599bd8SSage Weil 	}
3101a8599bd8SSage Weil 
3102be655596SSage Weil 	/* note that each of these drops i_ceph_lock for us */
3103a8599bd8SSage Weil 	switch (op) {
3104a8599bd8SSage Weil 	case CEPH_CAP_OP_REVOKE:
3105a8599bd8SSage Weil 	case CEPH_CAP_OP_GRANT:
31062cd698beSYan, Zheng 		__ceph_caps_issued(ci, &issued);
31072cd698beSYan, Zheng 		issued |= __ceph_caps_dirty(ci);
31082cd698beSYan, Zheng 		handle_cap_grant(mdsc, inode, h, NULL, 0, msg->middle,
31092cd698beSYan, Zheng 				 session, cap, issued);
311015637c8bSSage Weil 		goto done_unlocked;
3111a8599bd8SSage Weil 
3112a8599bd8SSage Weil 	case CEPH_CAP_OP_FLUSH_ACK:
31136df058c0SSage Weil 		handle_cap_flush_ack(inode, tid, h, session, cap);
3114a8599bd8SSage Weil 		break;
3115a8599bd8SSage Weil 
3116a8599bd8SSage Weil 	case CEPH_CAP_OP_TRUNC:
3117a8599bd8SSage Weil 		handle_cap_trunc(inode, h, session);
3118a8599bd8SSage Weil 		break;
3119a8599bd8SSage Weil 
3120a8599bd8SSage Weil 	default:
3121be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
3122a8599bd8SSage Weil 		pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3123a8599bd8SSage Weil 		       ceph_cap_op_name(op));
3124a8599bd8SSage Weil 	}
3125a8599bd8SSage Weil 
312621b559deSGreg Farnum 	goto done;
312721b559deSGreg Farnum 
312821b559deSGreg Farnum flush_cap_releases:
312921b559deSGreg Farnum 	/*
313021b559deSGreg Farnum 	 * send any full release message to try to move things
313121b559deSGreg Farnum 	 * along for the mds (who clearly thinks we still have this
313221b559deSGreg Farnum 	 * cap).
313321b559deSGreg Farnum 	 */
313421b559deSGreg Farnum 	ceph_add_cap_releases(mdsc, session);
313521b559deSGreg Farnum 	ceph_send_cap_releases(mdsc, session);
313621b559deSGreg Farnum 
3137a8599bd8SSage Weil done:
3138a8599bd8SSage Weil 	mutex_unlock(&session->s_mutex);
313915637c8bSSage Weil done_unlocked:
3140a8599bd8SSage Weil 	if (inode)
3141a8599bd8SSage Weil 		iput(inode);
3142a8599bd8SSage Weil 	return;
3143a8599bd8SSage Weil 
3144a8599bd8SSage Weil bad:
3145a8599bd8SSage Weil 	pr_err("ceph_handle_caps: corrupt message\n");
31469ec7cab1SSage Weil 	ceph_msg_dump(msg);
3147a8599bd8SSage Weil 	return;
3148a8599bd8SSage Weil }
3149a8599bd8SSage Weil 
3150a8599bd8SSage Weil /*
3151a8599bd8SSage Weil  * Delayed work handler to process end of delayed cap release LRU list.
3152a8599bd8SSage Weil  */
3153afcdaea3SSage Weil void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
3154a8599bd8SSage Weil {
3155a8599bd8SSage Weil 	struct ceph_inode_info *ci;
3156a8599bd8SSage Weil 	int flags = CHECK_CAPS_NODELAY;
3157a8599bd8SSage Weil 
3158a8599bd8SSage Weil 	dout("check_delayed_caps\n");
3159a8599bd8SSage Weil 	while (1) {
3160a8599bd8SSage Weil 		spin_lock(&mdsc->cap_delay_lock);
3161a8599bd8SSage Weil 		if (list_empty(&mdsc->cap_delay_list))
3162a8599bd8SSage Weil 			break;
3163a8599bd8SSage Weil 		ci = list_first_entry(&mdsc->cap_delay_list,
3164a8599bd8SSage Weil 				      struct ceph_inode_info,
3165a8599bd8SSage Weil 				      i_cap_delay_list);
3166a8599bd8SSage Weil 		if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3167a8599bd8SSage Weil 		    time_before(jiffies, ci->i_hold_caps_max))
3168a8599bd8SSage Weil 			break;
3169a8599bd8SSage Weil 		list_del_init(&ci->i_cap_delay_list);
3170a8599bd8SSage Weil 		spin_unlock(&mdsc->cap_delay_lock);
3171a8599bd8SSage Weil 		dout("check_delayed_caps on %p\n", &ci->vfs_inode);
3172a8599bd8SSage Weil 		ceph_check_caps(ci, flags, NULL);
3173a8599bd8SSage Weil 	}
3174a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_delay_lock);
3175a8599bd8SSage Weil }
3176a8599bd8SSage Weil 
3177a8599bd8SSage Weil /*
3178afcdaea3SSage Weil  * Flush all dirty caps to the mds
3179afcdaea3SSage Weil  */
3180afcdaea3SSage Weil void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
3181afcdaea3SSage Weil {
3182db354052SSage Weil 	struct ceph_inode_info *ci;
3183db354052SSage Weil 	struct inode *inode;
3184afcdaea3SSage Weil 
3185afcdaea3SSage Weil 	dout("flush_dirty_caps\n");
3186afcdaea3SSage Weil 	spin_lock(&mdsc->cap_dirty_lock);
3187db354052SSage Weil 	while (!list_empty(&mdsc->cap_dirty)) {
3188db354052SSage Weil 		ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
3189afcdaea3SSage Weil 				      i_dirty_item);
319070b666c3SSage Weil 		inode = &ci->vfs_inode;
319170b666c3SSage Weil 		ihold(inode);
3192db354052SSage Weil 		dout("flush_dirty_caps %p\n", inode);
3193afcdaea3SSage Weil 		spin_unlock(&mdsc->cap_dirty_lock);
319470b666c3SSage Weil 		ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
3195afcdaea3SSage Weil 		iput(inode);
3196afcdaea3SSage Weil 		spin_lock(&mdsc->cap_dirty_lock);
3197afcdaea3SSage Weil 	}
3198afcdaea3SSage Weil 	spin_unlock(&mdsc->cap_dirty_lock);
3199db354052SSage Weil 	dout("flush_dirty_caps done\n");
3200afcdaea3SSage Weil }
3201afcdaea3SSage Weil 
3202afcdaea3SSage Weil /*
3203a8599bd8SSage Weil  * Drop open file reference.  If we were the last open file,
3204a8599bd8SSage Weil  * we may need to release capabilities to the MDS (or schedule
3205a8599bd8SSage Weil  * their delayed release).
3206a8599bd8SSage Weil  */
3207a8599bd8SSage Weil void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
3208a8599bd8SSage Weil {
3209a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
3210a8599bd8SSage Weil 	int last = 0;
3211a8599bd8SSage Weil 
3212be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
3213a8599bd8SSage Weil 	dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
3214a8599bd8SSage Weil 	     ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
3215a8599bd8SSage Weil 	BUG_ON(ci->i_nr_by_mode[fmode] == 0);
3216a8599bd8SSage Weil 	if (--ci->i_nr_by_mode[fmode] == 0)
3217a8599bd8SSage Weil 		last++;
3218be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
3219a8599bd8SSage Weil 
3220a8599bd8SSage Weil 	if (last && ci->i_vino.snap == CEPH_NOSNAP)
3221a8599bd8SSage Weil 		ceph_check_caps(ci, 0, NULL);
3222a8599bd8SSage Weil }
3223a8599bd8SSage Weil 
3224a8599bd8SSage Weil /*
3225a8599bd8SSage Weil  * Helpers for embedding cap and dentry lease releases into mds
3226a8599bd8SSage Weil  * requests.
3227a8599bd8SSage Weil  *
3228a8599bd8SSage Weil  * @force is used by dentry_release (below) to force inclusion of a
3229a8599bd8SSage Weil  * record for the directory inode, even when there aren't any caps to
3230a8599bd8SSage Weil  * drop.
3231a8599bd8SSage Weil  */
3232a8599bd8SSage Weil int ceph_encode_inode_release(void **p, struct inode *inode,
3233a8599bd8SSage Weil 			      int mds, int drop, int unless, int force)
3234a8599bd8SSage Weil {
3235a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
3236a8599bd8SSage Weil 	struct ceph_cap *cap;
3237a8599bd8SSage Weil 	struct ceph_mds_request_release *rel = *p;
3238ec97f88bSSage Weil 	int used, dirty;
3239a8599bd8SSage Weil 	int ret = 0;
3240a8599bd8SSage Weil 
3241be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
3242916623daSSage Weil 	used = __ceph_caps_used(ci);
3243ec97f88bSSage Weil 	dirty = __ceph_caps_dirty(ci);
3244916623daSSage Weil 
3245ec97f88bSSage Weil 	dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3246ec97f88bSSage Weil 	     inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
3247916623daSSage Weil 	     ceph_cap_string(unless));
3248916623daSSage Weil 
3249ec97f88bSSage Weil 	/* only drop unused, clean caps */
3250ec97f88bSSage Weil 	drop &= ~(used | dirty);
3251916623daSSage Weil 
3252a8599bd8SSage Weil 	cap = __get_cap_for_mds(ci, mds);
3253a8599bd8SSage Weil 	if (cap && __cap_is_valid(cap)) {
3254a8599bd8SSage Weil 		if (force ||
3255a8599bd8SSage Weil 		    ((cap->issued & drop) &&
3256a8599bd8SSage Weil 		     (cap->issued & unless) == 0)) {
3257a8599bd8SSage Weil 			if ((cap->issued & drop) &&
3258a8599bd8SSage Weil 			    (cap->issued & unless) == 0) {
3259bb137f84SYan, Zheng 				int wanted = __ceph_caps_wanted(ci);
3260bb137f84SYan, Zheng 				if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
3261bb137f84SYan, Zheng 					wanted |= cap->mds_wanted;
3262bb137f84SYan, Zheng 				dout("encode_inode_release %p cap %p "
3263bb137f84SYan, Zheng 				     "%s -> %s, wanted %s -> %s\n", inode, cap,
3264a8599bd8SSage Weil 				     ceph_cap_string(cap->issued),
3265bb137f84SYan, Zheng 				     ceph_cap_string(cap->issued & ~drop),
3266bb137f84SYan, Zheng 				     ceph_cap_string(cap->mds_wanted),
3267bb137f84SYan, Zheng 				     ceph_cap_string(wanted));
3268bb137f84SYan, Zheng 
3269a8599bd8SSage Weil 				cap->issued &= ~drop;
3270a8599bd8SSage Weil 				cap->implemented &= ~drop;
3271bb137f84SYan, Zheng 				cap->mds_wanted = wanted;
3272a8599bd8SSage Weil 			} else {
3273a8599bd8SSage Weil 				dout("encode_inode_release %p cap %p %s"
3274a8599bd8SSage Weil 				     " (force)\n", inode, cap,
3275a8599bd8SSage Weil 				     ceph_cap_string(cap->issued));
3276a8599bd8SSage Weil 			}
3277a8599bd8SSage Weil 
3278a8599bd8SSage Weil 			rel->ino = cpu_to_le64(ceph_ino(inode));
3279a8599bd8SSage Weil 			rel->cap_id = cpu_to_le64(cap->cap_id);
3280a8599bd8SSage Weil 			rel->seq = cpu_to_le32(cap->seq);
328108a0f24eSHimangi Saraogi 			rel->issue_seq = cpu_to_le32(cap->issue_seq);
3282a8599bd8SSage Weil 			rel->mseq = cpu_to_le32(cap->mseq);
3283fd7b95cdSYan, Zheng 			rel->caps = cpu_to_le32(cap->implemented);
3284a8599bd8SSage Weil 			rel->wanted = cpu_to_le32(cap->mds_wanted);
3285a8599bd8SSage Weil 			rel->dname_len = 0;
3286a8599bd8SSage Weil 			rel->dname_seq = 0;
3287a8599bd8SSage Weil 			*p += sizeof(*rel);
3288a8599bd8SSage Weil 			ret = 1;
3289a8599bd8SSage Weil 		} else {
3290a8599bd8SSage Weil 			dout("encode_inode_release %p cap %p %s\n",
3291a8599bd8SSage Weil 			     inode, cap, ceph_cap_string(cap->issued));
3292a8599bd8SSage Weil 		}
3293a8599bd8SSage Weil 	}
3294be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
3295a8599bd8SSage Weil 	return ret;
3296a8599bd8SSage Weil }
3297a8599bd8SSage Weil 
3298a8599bd8SSage Weil int ceph_encode_dentry_release(void **p, struct dentry *dentry,
3299a8599bd8SSage Weil 			       int mds, int drop, int unless)
3300a8599bd8SSage Weil {
3301a8599bd8SSage Weil 	struct inode *dir = dentry->d_parent->d_inode;
3302a8599bd8SSage Weil 	struct ceph_mds_request_release *rel = *p;
3303a8599bd8SSage Weil 	struct ceph_dentry_info *di = ceph_dentry(dentry);
3304a8599bd8SSage Weil 	int force = 0;
3305a8599bd8SSage Weil 	int ret;
3306a8599bd8SSage Weil 
3307a8599bd8SSage Weil 	/*
3308a8599bd8SSage Weil 	 * force an record for the directory caps if we have a dentry lease.
3309be655596SSage Weil 	 * this is racy (can't take i_ceph_lock and d_lock together), but it
3310a8599bd8SSage Weil 	 * doesn't have to be perfect; the mds will revoke anything we don't
3311a8599bd8SSage Weil 	 * release.
3312a8599bd8SSage Weil 	 */
3313a8599bd8SSage Weil 	spin_lock(&dentry->d_lock);
3314a8599bd8SSage Weil 	if (di->lease_session && di->lease_session->s_mds == mds)
3315a8599bd8SSage Weil 		force = 1;
3316a8599bd8SSage Weil 	spin_unlock(&dentry->d_lock);
3317a8599bd8SSage Weil 
3318a8599bd8SSage Weil 	ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
3319a8599bd8SSage Weil 
3320a8599bd8SSage Weil 	spin_lock(&dentry->d_lock);
3321a8599bd8SSage Weil 	if (ret && di->lease_session && di->lease_session->s_mds == mds) {
3322a8599bd8SSage Weil 		dout("encode_dentry_release %p mds%d seq %d\n",
3323a8599bd8SSage Weil 		     dentry, mds, (int)di->lease_seq);
3324a8599bd8SSage Weil 		rel->dname_len = cpu_to_le32(dentry->d_name.len);
3325a8599bd8SSage Weil 		memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3326a8599bd8SSage Weil 		*p += dentry->d_name.len;
3327a8599bd8SSage Weil 		rel->dname_seq = cpu_to_le32(di->lease_seq);
33281dadcce3SSage Weil 		__ceph_mdsc_drop_dentry_lease(dentry);
3329a8599bd8SSage Weil 	}
3330a8599bd8SSage Weil 	spin_unlock(&dentry->d_lock);
3331a8599bd8SSage Weil 	return ret;
3332a8599bd8SSage Weil }
3333