xref: /openbmc/linux/fs/ceph/caps.c (revision 5e62ad30)
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"
133d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
143d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
15a8599bd8SSage Weil 
16a8599bd8SSage Weil /*
17a8599bd8SSage Weil  * Capability management
18a8599bd8SSage Weil  *
19a8599bd8SSage Weil  * The Ceph metadata servers control client access to inode metadata
20a8599bd8SSage Weil  * and file data by issuing capabilities, granting clients permission
21a8599bd8SSage Weil  * to read and/or write both inode field and file data to OSDs
22a8599bd8SSage Weil  * (storage nodes).  Each capability consists of a set of bits
23a8599bd8SSage Weil  * indicating which operations are allowed.
24a8599bd8SSage Weil  *
25a8599bd8SSage Weil  * If the client holds a *_SHARED cap, the client has a coherent value
26a8599bd8SSage Weil  * that can be safely read from the cached inode.
27a8599bd8SSage Weil  *
28a8599bd8SSage Weil  * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
29a8599bd8SSage Weil  * client is allowed to change inode attributes (e.g., file size,
30a8599bd8SSage Weil  * mtime), note its dirty state in the ceph_cap, and asynchronously
31a8599bd8SSage Weil  * flush that metadata change to the MDS.
32a8599bd8SSage Weil  *
33a8599bd8SSage Weil  * In the event of a conflicting operation (perhaps by another
34a8599bd8SSage Weil  * client), the MDS will revoke the conflicting client capabilities.
35a8599bd8SSage Weil  *
36a8599bd8SSage Weil  * In order for a client to cache an inode, it must hold a capability
37a8599bd8SSage Weil  * with at least one MDS server.  When inodes are released, release
38a8599bd8SSage Weil  * notifications are batched and periodically sent en masse to the MDS
39a8599bd8SSage Weil  * cluster to release server state.
40a8599bd8SSage Weil  */
41a8599bd8SSage Weil 
42a8599bd8SSage Weil 
43a8599bd8SSage Weil /*
44a8599bd8SSage Weil  * Generate readable cap strings for debugging output.
45a8599bd8SSage Weil  */
46a8599bd8SSage Weil #define MAX_CAP_STR 20
47a8599bd8SSage Weil static char cap_str[MAX_CAP_STR][40];
48a8599bd8SSage Weil static DEFINE_SPINLOCK(cap_str_lock);
49a8599bd8SSage Weil static int last_cap_str;
50a8599bd8SSage Weil 
51a8599bd8SSage Weil static char *gcap_string(char *s, int c)
52a8599bd8SSage Weil {
53a8599bd8SSage Weil 	if (c & CEPH_CAP_GSHARED)
54a8599bd8SSage Weil 		*s++ = 's';
55a8599bd8SSage Weil 	if (c & CEPH_CAP_GEXCL)
56a8599bd8SSage Weil 		*s++ = 'x';
57a8599bd8SSage Weil 	if (c & CEPH_CAP_GCACHE)
58a8599bd8SSage Weil 		*s++ = 'c';
59a8599bd8SSage Weil 	if (c & CEPH_CAP_GRD)
60a8599bd8SSage Weil 		*s++ = 'r';
61a8599bd8SSage Weil 	if (c & CEPH_CAP_GWR)
62a8599bd8SSage Weil 		*s++ = 'w';
63a8599bd8SSage Weil 	if (c & CEPH_CAP_GBUFFER)
64a8599bd8SSage Weil 		*s++ = 'b';
65a8599bd8SSage Weil 	if (c & CEPH_CAP_GLAZYIO)
66a8599bd8SSage Weil 		*s++ = 'l';
67a8599bd8SSage Weil 	return s;
68a8599bd8SSage Weil }
69a8599bd8SSage Weil 
70a8599bd8SSage Weil const char *ceph_cap_string(int caps)
71a8599bd8SSage Weil {
72a8599bd8SSage Weil 	int i;
73a8599bd8SSage Weil 	char *s;
74a8599bd8SSage Weil 	int c;
75a8599bd8SSage Weil 
76a8599bd8SSage Weil 	spin_lock(&cap_str_lock);
77a8599bd8SSage Weil 	i = last_cap_str++;
78a8599bd8SSage Weil 	if (last_cap_str == MAX_CAP_STR)
79a8599bd8SSage Weil 		last_cap_str = 0;
80a8599bd8SSage Weil 	spin_unlock(&cap_str_lock);
81a8599bd8SSage Weil 
82a8599bd8SSage Weil 	s = cap_str[i];
83a8599bd8SSage Weil 
84a8599bd8SSage Weil 	if (caps & CEPH_CAP_PIN)
85a8599bd8SSage Weil 		*s++ = 'p';
86a8599bd8SSage Weil 
87a8599bd8SSage Weil 	c = (caps >> CEPH_CAP_SAUTH) & 3;
88a8599bd8SSage Weil 	if (c) {
89a8599bd8SSage Weil 		*s++ = 'A';
90a8599bd8SSage Weil 		s = gcap_string(s, c);
91a8599bd8SSage Weil 	}
92a8599bd8SSage Weil 
93a8599bd8SSage Weil 	c = (caps >> CEPH_CAP_SLINK) & 3;
94a8599bd8SSage Weil 	if (c) {
95a8599bd8SSage Weil 		*s++ = 'L';
96a8599bd8SSage Weil 		s = gcap_string(s, c);
97a8599bd8SSage Weil 	}
98a8599bd8SSage Weil 
99a8599bd8SSage Weil 	c = (caps >> CEPH_CAP_SXATTR) & 3;
100a8599bd8SSage Weil 	if (c) {
101a8599bd8SSage Weil 		*s++ = 'X';
102a8599bd8SSage Weil 		s = gcap_string(s, c);
103a8599bd8SSage Weil 	}
104a8599bd8SSage Weil 
105a8599bd8SSage Weil 	c = caps >> CEPH_CAP_SFILE;
106a8599bd8SSage Weil 	if (c) {
107a8599bd8SSage Weil 		*s++ = 'F';
108a8599bd8SSage Weil 		s = gcap_string(s, c);
109a8599bd8SSage Weil 	}
110a8599bd8SSage Weil 
111a8599bd8SSage Weil 	if (s == cap_str[i])
112a8599bd8SSage Weil 		*s++ = '-';
113a8599bd8SSage Weil 	*s = 0;
114a8599bd8SSage Weil 	return cap_str[i];
115a8599bd8SSage Weil }
116a8599bd8SSage Weil 
11737151668SYehuda Sadeh void ceph_caps_init(struct ceph_mds_client *mdsc)
118a8599bd8SSage Weil {
11937151668SYehuda Sadeh 	INIT_LIST_HEAD(&mdsc->caps_list);
12037151668SYehuda Sadeh 	spin_lock_init(&mdsc->caps_list_lock);
121a8599bd8SSage Weil }
122a8599bd8SSage Weil 
12337151668SYehuda Sadeh void ceph_caps_finalize(struct ceph_mds_client *mdsc)
124a8599bd8SSage Weil {
125a8599bd8SSage Weil 	struct ceph_cap *cap;
126a8599bd8SSage Weil 
12737151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
12837151668SYehuda Sadeh 	while (!list_empty(&mdsc->caps_list)) {
12937151668SYehuda Sadeh 		cap = list_first_entry(&mdsc->caps_list,
13037151668SYehuda Sadeh 				       struct ceph_cap, caps_item);
131a8599bd8SSage Weil 		list_del(&cap->caps_item);
132a8599bd8SSage Weil 		kmem_cache_free(ceph_cap_cachep, cap);
133a8599bd8SSage Weil 	}
13437151668SYehuda Sadeh 	mdsc->caps_total_count = 0;
13537151668SYehuda Sadeh 	mdsc->caps_avail_count = 0;
13637151668SYehuda Sadeh 	mdsc->caps_use_count = 0;
13737151668SYehuda Sadeh 	mdsc->caps_reserve_count = 0;
13837151668SYehuda Sadeh 	mdsc->caps_min_count = 0;
13937151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
14085ccce43SSage Weil }
14185ccce43SSage Weil 
14237151668SYehuda Sadeh void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
14385ccce43SSage Weil {
14437151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
14537151668SYehuda Sadeh 	mdsc->caps_min_count += delta;
14637151668SYehuda Sadeh 	BUG_ON(mdsc->caps_min_count < 0);
14737151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
148a8599bd8SSage Weil }
149a8599bd8SSage Weil 
15037151668SYehuda Sadeh int ceph_reserve_caps(struct ceph_mds_client *mdsc,
15137151668SYehuda Sadeh 		      struct ceph_cap_reservation *ctx, int need)
152a8599bd8SSage Weil {
153a8599bd8SSage Weil 	int i;
154a8599bd8SSage Weil 	struct ceph_cap *cap;
155a8599bd8SSage Weil 	int have;
156a8599bd8SSage Weil 	int alloc = 0;
157a8599bd8SSage Weil 	LIST_HEAD(newcaps);
158a8599bd8SSage Weil 	int ret = 0;
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);
177a8599bd8SSage Weil 		if (!cap) {
178a8599bd8SSage Weil 			ret = -ENOMEM;
179a8599bd8SSage Weil 			goto out_alloc_count;
180a8599bd8SSage Weil 		}
181a8599bd8SSage Weil 		list_add(&cap->caps_item, &newcaps);
182a8599bd8SSage Weil 		alloc++;
183a8599bd8SSage Weil 	}
184a8599bd8SSage Weil 	BUG_ON(have + alloc != need);
185a8599bd8SSage Weil 
18637151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
18737151668SYehuda Sadeh 	mdsc->caps_total_count += alloc;
18837151668SYehuda Sadeh 	mdsc->caps_reserve_count += alloc;
18937151668SYehuda Sadeh 	list_splice(&newcaps, &mdsc->caps_list);
190a8599bd8SSage Weil 
19137151668SYehuda Sadeh 	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
19237151668SYehuda Sadeh 					 mdsc->caps_reserve_count +
19337151668SYehuda Sadeh 					 mdsc->caps_avail_count);
19437151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
195a8599bd8SSage Weil 
196a8599bd8SSage Weil 	ctx->count = need;
197a8599bd8SSage Weil 	dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
19837151668SYehuda Sadeh 	     ctx, mdsc->caps_total_count, mdsc->caps_use_count,
19937151668SYehuda Sadeh 	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
200a8599bd8SSage Weil 	return 0;
201a8599bd8SSage Weil 
202a8599bd8SSage Weil out_alloc_count:
203a8599bd8SSage Weil 	/* we didn't manage to reserve as much as we needed */
204a8599bd8SSage Weil 	pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
205a8599bd8SSage Weil 		   ctx, need, have);
206a8599bd8SSage Weil 	return ret;
207a8599bd8SSage Weil }
208a8599bd8SSage Weil 
20937151668SYehuda Sadeh int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
21037151668SYehuda Sadeh 			struct ceph_cap_reservation *ctx)
211a8599bd8SSage Weil {
212a8599bd8SSage Weil 	dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
213a8599bd8SSage Weil 	if (ctx->count) {
21437151668SYehuda Sadeh 		spin_lock(&mdsc->caps_list_lock);
21537151668SYehuda Sadeh 		BUG_ON(mdsc->caps_reserve_count < ctx->count);
21637151668SYehuda Sadeh 		mdsc->caps_reserve_count -= ctx->count;
21737151668SYehuda Sadeh 		mdsc->caps_avail_count += ctx->count;
218a8599bd8SSage Weil 		ctx->count = 0;
219a8599bd8SSage Weil 		dout("unreserve caps %d = %d used + %d resv + %d avail\n",
22037151668SYehuda Sadeh 		     mdsc->caps_total_count, mdsc->caps_use_count,
22137151668SYehuda Sadeh 		     mdsc->caps_reserve_count, mdsc->caps_avail_count);
22237151668SYehuda Sadeh 		BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
22337151668SYehuda Sadeh 						 mdsc->caps_reserve_count +
22437151668SYehuda Sadeh 						 mdsc->caps_avail_count);
22537151668SYehuda Sadeh 		spin_unlock(&mdsc->caps_list_lock);
226a8599bd8SSage Weil 	}
227a8599bd8SSage Weil 	return 0;
228a8599bd8SSage Weil }
229a8599bd8SSage Weil 
23037151668SYehuda Sadeh static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc,
23137151668SYehuda Sadeh 				struct ceph_cap_reservation *ctx)
232a8599bd8SSage Weil {
233a8599bd8SSage Weil 	struct ceph_cap *cap = NULL;
234a8599bd8SSage Weil 
235a8599bd8SSage Weil 	/* temporary, until we do something about cap import/export */
236443b3760SSage Weil 	if (!ctx) {
237443b3760SSage Weil 		cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
238443b3760SSage Weil 		if (cap) {
2394d1d0534SYan, Zheng 			spin_lock(&mdsc->caps_list_lock);
24037151668SYehuda Sadeh 			mdsc->caps_use_count++;
24137151668SYehuda Sadeh 			mdsc->caps_total_count++;
2424d1d0534SYan, Zheng 			spin_unlock(&mdsc->caps_list_lock);
243443b3760SSage Weil 		}
244443b3760SSage Weil 		return cap;
245443b3760SSage Weil 	}
246a8599bd8SSage Weil 
24737151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
248a8599bd8SSage Weil 	dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
24937151668SYehuda Sadeh 	     ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
25037151668SYehuda Sadeh 	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
251a8599bd8SSage Weil 	BUG_ON(!ctx->count);
25237151668SYehuda Sadeh 	BUG_ON(ctx->count > mdsc->caps_reserve_count);
25337151668SYehuda Sadeh 	BUG_ON(list_empty(&mdsc->caps_list));
254a8599bd8SSage Weil 
255a8599bd8SSage Weil 	ctx->count--;
25637151668SYehuda Sadeh 	mdsc->caps_reserve_count--;
25737151668SYehuda Sadeh 	mdsc->caps_use_count++;
258a8599bd8SSage Weil 
25937151668SYehuda Sadeh 	cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
260a8599bd8SSage Weil 	list_del(&cap->caps_item);
261a8599bd8SSage Weil 
26237151668SYehuda Sadeh 	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
26337151668SYehuda Sadeh 	       mdsc->caps_reserve_count + mdsc->caps_avail_count);
26437151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
265a8599bd8SSage Weil 	return cap;
266a8599bd8SSage Weil }
267a8599bd8SSage Weil 
26837151668SYehuda Sadeh void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
269a8599bd8SSage Weil {
27037151668SYehuda Sadeh 	spin_lock(&mdsc->caps_list_lock);
2717c1332b8SSage Weil 	dout("put_cap %p %d = %d used + %d resv + %d avail\n",
27237151668SYehuda Sadeh 	     cap, mdsc->caps_total_count, mdsc->caps_use_count,
27337151668SYehuda Sadeh 	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
27437151668SYehuda Sadeh 	mdsc->caps_use_count--;
275a8599bd8SSage Weil 	/*
27685ccce43SSage Weil 	 * Keep some preallocated caps around (ceph_min_count), to
27785ccce43SSage Weil 	 * avoid lots of free/alloc churn.
278a8599bd8SSage Weil 	 */
27937151668SYehuda Sadeh 	if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
28037151668SYehuda Sadeh 				      mdsc->caps_min_count) {
28137151668SYehuda Sadeh 		mdsc->caps_total_count--;
282a8599bd8SSage Weil 		kmem_cache_free(ceph_cap_cachep, cap);
283a8599bd8SSage Weil 	} else {
28437151668SYehuda Sadeh 		mdsc->caps_avail_count++;
28537151668SYehuda Sadeh 		list_add(&cap->caps_item, &mdsc->caps_list);
286a8599bd8SSage Weil 	}
287a8599bd8SSage Weil 
28837151668SYehuda Sadeh 	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
28937151668SYehuda Sadeh 	       mdsc->caps_reserve_count + mdsc->caps_avail_count);
29037151668SYehuda Sadeh 	spin_unlock(&mdsc->caps_list_lock);
291a8599bd8SSage Weil }
292a8599bd8SSage Weil 
2933d14c5d2SYehuda Sadeh void ceph_reservation_status(struct ceph_fs_client *fsc,
29485ccce43SSage Weil 			     int *total, int *avail, int *used, int *reserved,
29585ccce43SSage Weil 			     int *min)
296a8599bd8SSage Weil {
2973d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
29837151668SYehuda Sadeh 
299a8599bd8SSage Weil 	if (total)
30037151668SYehuda Sadeh 		*total = mdsc->caps_total_count;
301a8599bd8SSage Weil 	if (avail)
30237151668SYehuda Sadeh 		*avail = mdsc->caps_avail_count;
303a8599bd8SSage Weil 	if (used)
30437151668SYehuda Sadeh 		*used = mdsc->caps_use_count;
305a8599bd8SSage Weil 	if (reserved)
30637151668SYehuda Sadeh 		*reserved = mdsc->caps_reserve_count;
30785ccce43SSage Weil 	if (min)
30837151668SYehuda Sadeh 		*min = mdsc->caps_min_count;
309a8599bd8SSage Weil }
310a8599bd8SSage Weil 
311a8599bd8SSage Weil /*
312a8599bd8SSage Weil  * Find ceph_cap for given mds, if any.
313a8599bd8SSage Weil  *
314be655596SSage Weil  * Called with i_ceph_lock held.
315a8599bd8SSage Weil  */
316a8599bd8SSage Weil static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
317a8599bd8SSage Weil {
318a8599bd8SSage Weil 	struct ceph_cap *cap;
319a8599bd8SSage Weil 	struct rb_node *n = ci->i_caps.rb_node;
320a8599bd8SSage Weil 
321a8599bd8SSage Weil 	while (n) {
322a8599bd8SSage Weil 		cap = rb_entry(n, struct ceph_cap, ci_node);
323a8599bd8SSage Weil 		if (mds < cap->mds)
324a8599bd8SSage Weil 			n = n->rb_left;
325a8599bd8SSage Weil 		else if (mds > cap->mds)
326a8599bd8SSage Weil 			n = n->rb_right;
327a8599bd8SSage Weil 		else
328a8599bd8SSage Weil 			return cap;
329a8599bd8SSage Weil 	}
330a8599bd8SSage Weil 	return NULL;
331a8599bd8SSage Weil }
332a8599bd8SSage Weil 
3332bc50259SGreg Farnum struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
3342bc50259SGreg Farnum {
3352bc50259SGreg Farnum 	struct ceph_cap *cap;
3362bc50259SGreg Farnum 
337be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
3382bc50259SGreg Farnum 	cap = __get_cap_for_mds(ci, mds);
339be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
3402bc50259SGreg Farnum 	return cap;
3412bc50259SGreg Farnum }
3422bc50259SGreg Farnum 
343a8599bd8SSage Weil /*
34433caad32SSage Weil  * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
345a8599bd8SSage Weil  */
346ca81f3f6SSage Weil static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
347a8599bd8SSage Weil {
348a8599bd8SSage Weil 	struct ceph_cap *cap;
349a8599bd8SSage Weil 	int mds = -1;
350a8599bd8SSage Weil 	struct rb_node *p;
351a8599bd8SSage Weil 
35233caad32SSage Weil 	/* prefer mds with WR|BUFFER|EXCL caps */
353a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
354a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
355a8599bd8SSage Weil 		mds = cap->mds;
356a8599bd8SSage Weil 		if (cap->issued & (CEPH_CAP_FILE_WR |
357a8599bd8SSage Weil 				   CEPH_CAP_FILE_BUFFER |
358a8599bd8SSage Weil 				   CEPH_CAP_FILE_EXCL))
359a8599bd8SSage Weil 			break;
360a8599bd8SSage Weil 	}
361a8599bd8SSage Weil 	return mds;
362a8599bd8SSage Weil }
363a8599bd8SSage Weil 
364a8599bd8SSage Weil int ceph_get_cap_mds(struct inode *inode)
365a8599bd8SSage Weil {
366be655596SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
367a8599bd8SSage Weil 	int mds;
368be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
369ca81f3f6SSage Weil 	mds = __ceph_get_cap_mds(ceph_inode(inode));
370be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
371a8599bd8SSage Weil 	return mds;
372a8599bd8SSage Weil }
373a8599bd8SSage Weil 
374a8599bd8SSage Weil /*
375be655596SSage Weil  * Called under i_ceph_lock.
376a8599bd8SSage Weil  */
377a8599bd8SSage Weil static void __insert_cap_node(struct ceph_inode_info *ci,
378a8599bd8SSage Weil 			      struct ceph_cap *new)
379a8599bd8SSage Weil {
380a8599bd8SSage Weil 	struct rb_node **p = &ci->i_caps.rb_node;
381a8599bd8SSage Weil 	struct rb_node *parent = NULL;
382a8599bd8SSage Weil 	struct ceph_cap *cap = NULL;
383a8599bd8SSage Weil 
384a8599bd8SSage Weil 	while (*p) {
385a8599bd8SSage Weil 		parent = *p;
386a8599bd8SSage Weil 		cap = rb_entry(parent, struct ceph_cap, ci_node);
387a8599bd8SSage Weil 		if (new->mds < cap->mds)
388a8599bd8SSage Weil 			p = &(*p)->rb_left;
389a8599bd8SSage Weil 		else if (new->mds > cap->mds)
390a8599bd8SSage Weil 			p = &(*p)->rb_right;
391a8599bd8SSage Weil 		else
392a8599bd8SSage Weil 			BUG();
393a8599bd8SSage Weil 	}
394a8599bd8SSage Weil 
395a8599bd8SSage Weil 	rb_link_node(&new->ci_node, parent, p);
396a8599bd8SSage Weil 	rb_insert_color(&new->ci_node, &ci->i_caps);
397a8599bd8SSage Weil }
398a8599bd8SSage Weil 
399a8599bd8SSage Weil /*
400a8599bd8SSage Weil  * (re)set cap hold timeouts, which control the delayed release
401a8599bd8SSage Weil  * of unused caps back to the MDS.  Should be called on cap use.
402a8599bd8SSage Weil  */
403a8599bd8SSage Weil static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
404a8599bd8SSage Weil 			       struct ceph_inode_info *ci)
405a8599bd8SSage Weil {
4063d14c5d2SYehuda Sadeh 	struct ceph_mount_options *ma = mdsc->fsc->mount_options;
407a8599bd8SSage Weil 
408a8599bd8SSage Weil 	ci->i_hold_caps_min = round_jiffies(jiffies +
409a8599bd8SSage Weil 					    ma->caps_wanted_delay_min * HZ);
410a8599bd8SSage Weil 	ci->i_hold_caps_max = round_jiffies(jiffies +
411a8599bd8SSage Weil 					    ma->caps_wanted_delay_max * HZ);
412a8599bd8SSage Weil 	dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
413a8599bd8SSage Weil 	     ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
414a8599bd8SSage Weil }
415a8599bd8SSage Weil 
416a8599bd8SSage Weil /*
417a8599bd8SSage Weil  * (Re)queue cap at the end of the delayed cap release list.
418a8599bd8SSage Weil  *
419a8599bd8SSage Weil  * If I_FLUSH is set, leave the inode at the front of the list.
420a8599bd8SSage Weil  *
421be655596SSage Weil  * Caller holds i_ceph_lock
422a8599bd8SSage Weil  *    -> we take mdsc->cap_delay_lock
423a8599bd8SSage Weil  */
424a8599bd8SSage Weil static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
425a8599bd8SSage Weil 				struct ceph_inode_info *ci)
426a8599bd8SSage Weil {
427a8599bd8SSage Weil 	__cap_set_timeouts(mdsc, ci);
428a8599bd8SSage Weil 	dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
429a8599bd8SSage Weil 	     ci->i_ceph_flags, ci->i_hold_caps_max);
430a8599bd8SSage Weil 	if (!mdsc->stopping) {
431a8599bd8SSage Weil 		spin_lock(&mdsc->cap_delay_lock);
432a8599bd8SSage Weil 		if (!list_empty(&ci->i_cap_delay_list)) {
433a8599bd8SSage Weil 			if (ci->i_ceph_flags & CEPH_I_FLUSH)
434a8599bd8SSage Weil 				goto no_change;
435a8599bd8SSage Weil 			list_del_init(&ci->i_cap_delay_list);
436a8599bd8SSage Weil 		}
437a8599bd8SSage Weil 		list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
438a8599bd8SSage Weil no_change:
439a8599bd8SSage Weil 		spin_unlock(&mdsc->cap_delay_lock);
440a8599bd8SSage Weil 	}
441a8599bd8SSage Weil }
442a8599bd8SSage Weil 
443a8599bd8SSage Weil /*
444a8599bd8SSage Weil  * Queue an inode for immediate writeback.  Mark inode with I_FLUSH,
445a8599bd8SSage Weil  * indicating we should send a cap message to flush dirty metadata
446a8599bd8SSage Weil  * asap, and move to the front of the delayed cap list.
447a8599bd8SSage Weil  */
448a8599bd8SSage Weil static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
449a8599bd8SSage Weil 				      struct ceph_inode_info *ci)
450a8599bd8SSage Weil {
451a8599bd8SSage Weil 	dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
452a8599bd8SSage Weil 	spin_lock(&mdsc->cap_delay_lock);
453a8599bd8SSage Weil 	ci->i_ceph_flags |= CEPH_I_FLUSH;
454a8599bd8SSage Weil 	if (!list_empty(&ci->i_cap_delay_list))
455a8599bd8SSage Weil 		list_del_init(&ci->i_cap_delay_list);
456a8599bd8SSage Weil 	list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
457a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_delay_lock);
458a8599bd8SSage Weil }
459a8599bd8SSage Weil 
460a8599bd8SSage Weil /*
461a8599bd8SSage Weil  * Cancel delayed work on cap.
462a8599bd8SSage Weil  *
463be655596SSage Weil  * Caller must hold i_ceph_lock.
464a8599bd8SSage Weil  */
465a8599bd8SSage Weil static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
466a8599bd8SSage Weil 			       struct ceph_inode_info *ci)
467a8599bd8SSage Weil {
468a8599bd8SSage Weil 	dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
469a8599bd8SSage Weil 	if (list_empty(&ci->i_cap_delay_list))
470a8599bd8SSage Weil 		return;
471a8599bd8SSage Weil 	spin_lock(&mdsc->cap_delay_lock);
472a8599bd8SSage Weil 	list_del_init(&ci->i_cap_delay_list);
473a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_delay_lock);
474a8599bd8SSage Weil }
475a8599bd8SSage Weil 
476a8599bd8SSage Weil /*
477a8599bd8SSage Weil  * Common issue checks for add_cap, handle_cap_grant.
478a8599bd8SSage Weil  */
479a8599bd8SSage Weil static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
480a8599bd8SSage Weil 			      unsigned issued)
481a8599bd8SSage Weil {
482a8599bd8SSage Weil 	unsigned had = __ceph_caps_issued(ci, NULL);
483a8599bd8SSage Weil 
484a8599bd8SSage Weil 	/*
485a8599bd8SSage Weil 	 * Each time we receive FILE_CACHE anew, we increment
486a8599bd8SSage Weil 	 * i_rdcache_gen.
487a8599bd8SSage Weil 	 */
4882962507cSSage Weil 	if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
4892962507cSSage Weil 	    (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
490a8599bd8SSage Weil 		ci->i_rdcache_gen++;
491a8599bd8SSage Weil 
492a8599bd8SSage Weil 	/*
493c6ffe100SSage Weil 	 * if we are newly issued FILE_SHARED, clear D_COMPLETE; we
494a8599bd8SSage Weil 	 * don't know what happened to this directory while we didn't
495a8599bd8SSage Weil 	 * have the cap.
496a8599bd8SSage Weil 	 */
497a8599bd8SSage Weil 	if ((issued & CEPH_CAP_FILE_SHARED) &&
498a8599bd8SSage Weil 	    (had & CEPH_CAP_FILE_SHARED) == 0) {
499a8599bd8SSage Weil 		ci->i_shared_gen++;
500c6ffe100SSage Weil 		if (S_ISDIR(ci->vfs_inode.i_mode))
501c6ffe100SSage Weil 			ceph_dir_clear_complete(&ci->vfs_inode);
502a8599bd8SSage Weil 	}
503a8599bd8SSage Weil }
504a8599bd8SSage Weil 
505a8599bd8SSage Weil /*
506a8599bd8SSage Weil  * Add a capability under the given MDS session.
507a8599bd8SSage Weil  *
508a8599bd8SSage Weil  * Caller should hold session snap_rwsem (read) and s_mutex.
509a8599bd8SSage Weil  *
510a8599bd8SSage Weil  * @fmode is the open file mode, if we are opening a file, otherwise
511a8599bd8SSage Weil  * it is < 0.  (This is so we can atomically add the cap and add an
512a8599bd8SSage Weil  * open file reference to it.)
513a8599bd8SSage Weil  */
514a8599bd8SSage Weil int ceph_add_cap(struct inode *inode,
515a8599bd8SSage Weil 		 struct ceph_mds_session *session, u64 cap_id,
516a8599bd8SSage Weil 		 int fmode, unsigned issued, unsigned wanted,
517a8599bd8SSage Weil 		 unsigned seq, unsigned mseq, u64 realmino, int flags,
518a8599bd8SSage Weil 		 struct ceph_cap_reservation *caps_reservation)
519a8599bd8SSage Weil {
5203d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
521a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
522a8599bd8SSage Weil 	struct ceph_cap *new_cap = NULL;
523a8599bd8SSage Weil 	struct ceph_cap *cap;
524a8599bd8SSage Weil 	int mds = session->s_mds;
525a8599bd8SSage Weil 	int actual_wanted;
526a8599bd8SSage Weil 
527a8599bd8SSage Weil 	dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
528a8599bd8SSage Weil 	     session->s_mds, cap_id, ceph_cap_string(issued), seq);
529a8599bd8SSage Weil 
530a8599bd8SSage Weil 	/*
531a8599bd8SSage Weil 	 * If we are opening the file, include file mode wanted bits
532a8599bd8SSage Weil 	 * in wanted.
533a8599bd8SSage Weil 	 */
534a8599bd8SSage Weil 	if (fmode >= 0)
535a8599bd8SSage Weil 		wanted |= ceph_caps_for_mode(fmode);
536a8599bd8SSage Weil 
537a8599bd8SSage Weil retry:
538be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
539a8599bd8SSage Weil 	cap = __get_cap_for_mds(ci, mds);
540a8599bd8SSage Weil 	if (!cap) {
541a8599bd8SSage Weil 		if (new_cap) {
542a8599bd8SSage Weil 			cap = new_cap;
543a8599bd8SSage Weil 			new_cap = NULL;
544a8599bd8SSage Weil 		} else {
545be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
54637151668SYehuda Sadeh 			new_cap = get_cap(mdsc, caps_reservation);
547a8599bd8SSage Weil 			if (new_cap == NULL)
548a8599bd8SSage Weil 				return -ENOMEM;
549a8599bd8SSage Weil 			goto retry;
550a8599bd8SSage Weil 		}
551a8599bd8SSage Weil 
552a8599bd8SSage Weil 		cap->issued = 0;
553a8599bd8SSage Weil 		cap->implemented = 0;
554a8599bd8SSage Weil 		cap->mds = mds;
555a8599bd8SSage Weil 		cap->mds_wanted = 0;
556a8599bd8SSage Weil 
557a8599bd8SSage Weil 		cap->ci = ci;
558a8599bd8SSage Weil 		__insert_cap_node(ci, cap);
559a8599bd8SSage Weil 
560a8599bd8SSage Weil 		/* clear out old exporting info?  (i.e. on cap import) */
561a8599bd8SSage Weil 		if (ci->i_cap_exporting_mds == mds) {
562a8599bd8SSage Weil 			ci->i_cap_exporting_issued = 0;
563a8599bd8SSage Weil 			ci->i_cap_exporting_mseq = 0;
564a8599bd8SSage Weil 			ci->i_cap_exporting_mds = -1;
565a8599bd8SSage Weil 		}
566a8599bd8SSage Weil 
567a8599bd8SSage Weil 		/* add to session cap list */
568a8599bd8SSage Weil 		cap->session = session;
569a8599bd8SSage Weil 		spin_lock(&session->s_cap_lock);
570a8599bd8SSage Weil 		list_add_tail(&cap->session_caps, &session->s_caps);
571a8599bd8SSage Weil 		session->s_nr_caps++;
572a8599bd8SSage Weil 		spin_unlock(&session->s_cap_lock);
5733540303fSSage Weil 	} else if (new_cap)
5743540303fSSage Weil 		ceph_put_cap(mdsc, new_cap);
575a8599bd8SSage Weil 
576a8599bd8SSage Weil 	if (!ci->i_snap_realm) {
577a8599bd8SSage Weil 		/*
578a8599bd8SSage Weil 		 * add this inode to the appropriate snap realm
579a8599bd8SSage Weil 		 */
580a8599bd8SSage Weil 		struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
581a8599bd8SSage Weil 							       realmino);
582a8599bd8SSage Weil 		if (realm) {
583a8599bd8SSage Weil 			ceph_get_snap_realm(mdsc, realm);
584a8599bd8SSage Weil 			spin_lock(&realm->inodes_with_caps_lock);
585a8599bd8SSage Weil 			ci->i_snap_realm = realm;
586a8599bd8SSage Weil 			list_add(&ci->i_snap_realm_item,
587a8599bd8SSage Weil 				 &realm->inodes_with_caps);
588a8599bd8SSage Weil 			spin_unlock(&realm->inodes_with_caps_lock);
589a8599bd8SSage Weil 		} else {
590a8599bd8SSage Weil 			pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
591a8599bd8SSage Weil 			       realmino);
592b8cd07e7SSage Weil 			WARN_ON(!realm);
593a8599bd8SSage Weil 		}
594a8599bd8SSage Weil 	}
595a8599bd8SSage Weil 
596a8599bd8SSage Weil 	__check_cap_issue(ci, cap, issued);
597a8599bd8SSage Weil 
598a8599bd8SSage Weil 	/*
599a8599bd8SSage Weil 	 * If we are issued caps we don't want, or the mds' wanted
600a8599bd8SSage Weil 	 * value appears to be off, queue a check so we'll release
601a8599bd8SSage Weil 	 * later and/or update the mds wanted value.
602a8599bd8SSage Weil 	 */
603a8599bd8SSage Weil 	actual_wanted = __ceph_caps_wanted(ci);
604a8599bd8SSage Weil 	if ((wanted & ~actual_wanted) ||
605a8599bd8SSage Weil 	    (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
606a8599bd8SSage Weil 		dout(" issued %s, mds wanted %s, actual %s, queueing\n",
607a8599bd8SSage Weil 		     ceph_cap_string(issued), ceph_cap_string(wanted),
608a8599bd8SSage Weil 		     ceph_cap_string(actual_wanted));
609a8599bd8SSage Weil 		__cap_delay_requeue(mdsc, ci);
610a8599bd8SSage Weil 	}
611a8599bd8SSage Weil 
612a8599bd8SSage Weil 	if (flags & CEPH_CAP_FLAG_AUTH)
613a8599bd8SSage Weil 		ci->i_auth_cap = cap;
614a8599bd8SSage Weil 	else if (ci->i_auth_cap == cap)
615a8599bd8SSage Weil 		ci->i_auth_cap = NULL;
616a8599bd8SSage Weil 
617a8599bd8SSage Weil 	dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
618a8599bd8SSage Weil 	     inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
619a8599bd8SSage Weil 	     ceph_cap_string(issued|cap->issued), seq, mds);
620a8599bd8SSage Weil 	cap->cap_id = cap_id;
621a8599bd8SSage Weil 	cap->issued = issued;
622a8599bd8SSage Weil 	cap->implemented |= issued;
623a8599bd8SSage Weil 	cap->mds_wanted |= wanted;
624a8599bd8SSage Weil 	cap->seq = seq;
625a8599bd8SSage Weil 	cap->issue_seq = seq;
626a8599bd8SSage Weil 	cap->mseq = mseq;
627685f9a5dSSage Weil 	cap->cap_gen = session->s_cap_gen;
628a8599bd8SSage Weil 
629a8599bd8SSage Weil 	if (fmode >= 0)
630a8599bd8SSage Weil 		__ceph_get_fmode(ci, fmode);
631be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
63203066f23SYehuda Sadeh 	wake_up_all(&ci->i_cap_wq);
633a8599bd8SSage Weil 	return 0;
634a8599bd8SSage Weil }
635a8599bd8SSage Weil 
636a8599bd8SSage Weil /*
637a8599bd8SSage Weil  * Return true if cap has not timed out and belongs to the current
638a8599bd8SSage Weil  * generation of the MDS session (i.e. has not gone 'stale' due to
639a8599bd8SSage Weil  * us losing touch with the mds).
640a8599bd8SSage Weil  */
641a8599bd8SSage Weil static int __cap_is_valid(struct ceph_cap *cap)
642a8599bd8SSage Weil {
643a8599bd8SSage Weil 	unsigned long ttl;
644cdac8303SSage Weil 	u32 gen;
645a8599bd8SSage Weil 
646d8fb02abSAlex Elder 	spin_lock(&cap->session->s_gen_ttl_lock);
647a8599bd8SSage Weil 	gen = cap->session->s_cap_gen;
648a8599bd8SSage Weil 	ttl = cap->session->s_cap_ttl;
649d8fb02abSAlex Elder 	spin_unlock(&cap->session->s_gen_ttl_lock);
650a8599bd8SSage Weil 
651685f9a5dSSage Weil 	if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
652a8599bd8SSage Weil 		dout("__cap_is_valid %p cap %p issued %s "
653a8599bd8SSage Weil 		     "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
654685f9a5dSSage Weil 		     cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
655a8599bd8SSage Weil 		return 0;
656a8599bd8SSage Weil 	}
657a8599bd8SSage Weil 
658a8599bd8SSage Weil 	return 1;
659a8599bd8SSage Weil }
660a8599bd8SSage Weil 
661a8599bd8SSage Weil /*
662a8599bd8SSage Weil  * Return set of valid cap bits issued to us.  Note that caps time
663a8599bd8SSage Weil  * out, and may be invalidated in bulk if the client session times out
664a8599bd8SSage Weil  * and session->s_cap_gen is bumped.
665a8599bd8SSage Weil  */
666a8599bd8SSage Weil int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
667a8599bd8SSage Weil {
6687af8f1e4SSage Weil 	int have = ci->i_snap_caps | ci->i_cap_exporting_issued;
669a8599bd8SSage Weil 	struct ceph_cap *cap;
670a8599bd8SSage Weil 	struct rb_node *p;
671a8599bd8SSage Weil 
672a8599bd8SSage Weil 	if (implemented)
673a8599bd8SSage Weil 		*implemented = 0;
674a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
675a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
676a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
677a8599bd8SSage Weil 			continue;
678a8599bd8SSage Weil 		dout("__ceph_caps_issued %p cap %p issued %s\n",
679a8599bd8SSage Weil 		     &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
680a8599bd8SSage Weil 		have |= cap->issued;
681a8599bd8SSage Weil 		if (implemented)
682a8599bd8SSage Weil 			*implemented |= cap->implemented;
683a8599bd8SSage Weil 	}
684a8599bd8SSage Weil 	return have;
685a8599bd8SSage Weil }
686a8599bd8SSage Weil 
687a8599bd8SSage Weil /*
688a8599bd8SSage Weil  * Get cap bits issued by caps other than @ocap
689a8599bd8SSage Weil  */
690a8599bd8SSage Weil int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
691a8599bd8SSage Weil {
692a8599bd8SSage Weil 	int have = ci->i_snap_caps;
693a8599bd8SSage Weil 	struct ceph_cap *cap;
694a8599bd8SSage Weil 	struct rb_node *p;
695a8599bd8SSage Weil 
696a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
697a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
698a8599bd8SSage Weil 		if (cap == ocap)
699a8599bd8SSage Weil 			continue;
700a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
701a8599bd8SSage Weil 			continue;
702a8599bd8SSage Weil 		have |= cap->issued;
703a8599bd8SSage Weil 	}
704a8599bd8SSage Weil 	return have;
705a8599bd8SSage Weil }
706a8599bd8SSage Weil 
707a8599bd8SSage Weil /*
708a8599bd8SSage Weil  * Move a cap to the end of the LRU (oldest caps at list head, newest
709a8599bd8SSage Weil  * at list tail).
710a8599bd8SSage Weil  */
711a8599bd8SSage Weil static void __touch_cap(struct ceph_cap *cap)
712a8599bd8SSage Weil {
713a8599bd8SSage Weil 	struct ceph_mds_session *s = cap->session;
714a8599bd8SSage Weil 
7155dacf091SSage Weil 	spin_lock(&s->s_cap_lock);
7167c1332b8SSage Weil 	if (s->s_cap_iterator == NULL) {
717a8599bd8SSage Weil 		dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
718a8599bd8SSage Weil 		     s->s_mds);
719a8599bd8SSage Weil 		list_move_tail(&cap->session_caps, &s->s_caps);
7205dacf091SSage Weil 	} else {
7215dacf091SSage Weil 		dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
7225dacf091SSage Weil 		     &cap->ci->vfs_inode, cap, s->s_mds);
7235dacf091SSage Weil 	}
724a8599bd8SSage Weil 	spin_unlock(&s->s_cap_lock);
725a8599bd8SSage Weil }
726a8599bd8SSage Weil 
727a8599bd8SSage Weil /*
728a8599bd8SSage Weil  * Check if we hold the given mask.  If so, move the cap(s) to the
729a8599bd8SSage Weil  * front of their respective LRUs.  (This is the preferred way for
730a8599bd8SSage Weil  * callers to check for caps they want.)
731a8599bd8SSage Weil  */
732a8599bd8SSage Weil int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
733a8599bd8SSage Weil {
734a8599bd8SSage Weil 	struct ceph_cap *cap;
735a8599bd8SSage Weil 	struct rb_node *p;
736a8599bd8SSage Weil 	int have = ci->i_snap_caps;
737a8599bd8SSage Weil 
738a8599bd8SSage Weil 	if ((have & mask) == mask) {
739a8599bd8SSage Weil 		dout("__ceph_caps_issued_mask %p snap issued %s"
740a8599bd8SSage Weil 		     " (mask %s)\n", &ci->vfs_inode,
741a8599bd8SSage Weil 		     ceph_cap_string(have),
742a8599bd8SSage Weil 		     ceph_cap_string(mask));
743a8599bd8SSage Weil 		return 1;
744a8599bd8SSage Weil 	}
745a8599bd8SSage Weil 
746a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
747a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
748a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
749a8599bd8SSage Weil 			continue;
750a8599bd8SSage Weil 		if ((cap->issued & mask) == mask) {
751a8599bd8SSage Weil 			dout("__ceph_caps_issued_mask %p cap %p issued %s"
752a8599bd8SSage Weil 			     " (mask %s)\n", &ci->vfs_inode, cap,
753a8599bd8SSage Weil 			     ceph_cap_string(cap->issued),
754a8599bd8SSage Weil 			     ceph_cap_string(mask));
755a8599bd8SSage Weil 			if (touch)
756a8599bd8SSage Weil 				__touch_cap(cap);
757a8599bd8SSage Weil 			return 1;
758a8599bd8SSage Weil 		}
759a8599bd8SSage Weil 
760a8599bd8SSage Weil 		/* does a combination of caps satisfy mask? */
761a8599bd8SSage Weil 		have |= cap->issued;
762a8599bd8SSage Weil 		if ((have & mask) == mask) {
763a8599bd8SSage Weil 			dout("__ceph_caps_issued_mask %p combo issued %s"
764a8599bd8SSage Weil 			     " (mask %s)\n", &ci->vfs_inode,
765a8599bd8SSage Weil 			     ceph_cap_string(cap->issued),
766a8599bd8SSage Weil 			     ceph_cap_string(mask));
767a8599bd8SSage Weil 			if (touch) {
768a8599bd8SSage Weil 				struct rb_node *q;
769a8599bd8SSage Weil 
77025985edcSLucas De Marchi 				/* touch this + preceding caps */
771a8599bd8SSage Weil 				__touch_cap(cap);
772a8599bd8SSage Weil 				for (q = rb_first(&ci->i_caps); q != p;
773a8599bd8SSage Weil 				     q = rb_next(q)) {
774a8599bd8SSage Weil 					cap = rb_entry(q, struct ceph_cap,
775a8599bd8SSage Weil 						       ci_node);
776a8599bd8SSage Weil 					if (!__cap_is_valid(cap))
777a8599bd8SSage Weil 						continue;
778a8599bd8SSage Weil 					__touch_cap(cap);
779a8599bd8SSage Weil 				}
780a8599bd8SSage Weil 			}
781a8599bd8SSage Weil 			return 1;
782a8599bd8SSage Weil 		}
783a8599bd8SSage Weil 	}
784a8599bd8SSage Weil 
785a8599bd8SSage Weil 	return 0;
786a8599bd8SSage Weil }
787a8599bd8SSage Weil 
788a8599bd8SSage Weil /*
789a8599bd8SSage Weil  * Return true if mask caps are currently being revoked by an MDS.
790a8599bd8SSage Weil  */
791a8599bd8SSage Weil int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
792a8599bd8SSage Weil {
793a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
794a8599bd8SSage Weil 	struct ceph_cap *cap;
795a8599bd8SSage Weil 	struct rb_node *p;
796a8599bd8SSage Weil 	int ret = 0;
797a8599bd8SSage Weil 
798be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
799a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
800a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
801a8599bd8SSage Weil 		if (__cap_is_valid(cap) &&
802a8599bd8SSage Weil 		    (cap->implemented & ~cap->issued & mask)) {
803a8599bd8SSage Weil 			ret = 1;
804a8599bd8SSage Weil 			break;
805a8599bd8SSage Weil 		}
806a8599bd8SSage Weil 	}
807be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
808a8599bd8SSage Weil 	dout("ceph_caps_revoking %p %s = %d\n", inode,
809a8599bd8SSage Weil 	     ceph_cap_string(mask), ret);
810a8599bd8SSage Weil 	return ret;
811a8599bd8SSage Weil }
812a8599bd8SSage Weil 
813a8599bd8SSage Weil int __ceph_caps_used(struct ceph_inode_info *ci)
814a8599bd8SSage Weil {
815a8599bd8SSage Weil 	int used = 0;
816a8599bd8SSage Weil 	if (ci->i_pin_ref)
817a8599bd8SSage Weil 		used |= CEPH_CAP_PIN;
818a8599bd8SSage Weil 	if (ci->i_rd_ref)
819a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_RD;
820a43fb731SSage Weil 	if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages)
821a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_CACHE;
822a8599bd8SSage Weil 	if (ci->i_wr_ref)
823a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_WR;
824d3d0720dSHenry C Chang 	if (ci->i_wb_ref || ci->i_wrbuffer_ref)
825a8599bd8SSage Weil 		used |= CEPH_CAP_FILE_BUFFER;
826a8599bd8SSage Weil 	return used;
827a8599bd8SSage Weil }
828a8599bd8SSage Weil 
829a8599bd8SSage Weil /*
830a8599bd8SSage Weil  * wanted, by virtue of open file modes
831a8599bd8SSage Weil  */
832a8599bd8SSage Weil int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
833a8599bd8SSage Weil {
834a8599bd8SSage Weil 	int want = 0;
835a8599bd8SSage Weil 	int mode;
83633caad32SSage Weil 	for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++)
837a8599bd8SSage Weil 		if (ci->i_nr_by_mode[mode])
838a8599bd8SSage Weil 			want |= ceph_caps_for_mode(mode);
839a8599bd8SSage Weil 	return want;
840a8599bd8SSage Weil }
841a8599bd8SSage Weil 
842a8599bd8SSage Weil /*
843a8599bd8SSage Weil  * Return caps we have registered with the MDS(s) as 'wanted'.
844a8599bd8SSage Weil  */
845a8599bd8SSage Weil int __ceph_caps_mds_wanted(struct ceph_inode_info *ci)
846a8599bd8SSage Weil {
847a8599bd8SSage Weil 	struct ceph_cap *cap;
848a8599bd8SSage Weil 	struct rb_node *p;
849a8599bd8SSage Weil 	int mds_wanted = 0;
850a8599bd8SSage Weil 
851a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
852a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
853a8599bd8SSage Weil 		if (!__cap_is_valid(cap))
854a8599bd8SSage Weil 			continue;
855a8599bd8SSage Weil 		mds_wanted |= cap->mds_wanted;
856a8599bd8SSage Weil 	}
857a8599bd8SSage Weil 	return mds_wanted;
858a8599bd8SSage Weil }
859a8599bd8SSage Weil 
860a8599bd8SSage Weil /*
861be655596SSage Weil  * called under i_ceph_lock
862a8599bd8SSage Weil  */
863a8599bd8SSage Weil static int __ceph_is_any_caps(struct ceph_inode_info *ci)
864a8599bd8SSage Weil {
865a8599bd8SSage Weil 	return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0;
866a8599bd8SSage Weil }
867a8599bd8SSage Weil 
868a8599bd8SSage Weil /*
869f818a736SSage Weil  * Remove a cap.  Take steps to deal with a racing iterate_session_caps.
870f818a736SSage Weil  *
871be655596SSage Weil  * caller should hold i_ceph_lock.
872a6369741SSage Weil  * caller will not hold session s_mutex if called from destroy_inode.
873a8599bd8SSage Weil  */
8747c1332b8SSage Weil void __ceph_remove_cap(struct ceph_cap *cap)
875a8599bd8SSage Weil {
876a8599bd8SSage Weil 	struct ceph_mds_session *session = cap->session;
877a8599bd8SSage Weil 	struct ceph_inode_info *ci = cap->ci;
878640ef79dSCheng Renquan 	struct ceph_mds_client *mdsc =
8793d14c5d2SYehuda Sadeh 		ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
880f818a736SSage Weil 	int removed = 0;
881a8599bd8SSage Weil 
882a8599bd8SSage Weil 	dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
883a8599bd8SSage Weil 
8847c1332b8SSage Weil 	/* remove from session list */
8857c1332b8SSage Weil 	spin_lock(&session->s_cap_lock);
8867c1332b8SSage Weil 	if (session->s_cap_iterator == cap) {
8877c1332b8SSage Weil 		/* not yet, we are iterating over this very cap */
8887c1332b8SSage Weil 		dout("__ceph_remove_cap  delaying %p removal from session %p\n",
8897c1332b8SSage Weil 		     cap, cap->session);
8907c1332b8SSage Weil 	} else {
8917c1332b8SSage Weil 		list_del_init(&cap->session_caps);
8927c1332b8SSage Weil 		session->s_nr_caps--;
8937c1332b8SSage Weil 		cap->session = NULL;
894f818a736SSage Weil 		removed = 1;
8957c1332b8SSage Weil 	}
896f818a736SSage Weil 	/* protect backpointer with s_cap_lock: see iterate_session_caps */
897f818a736SSage Weil 	cap->ci = NULL;
8987c1332b8SSage Weil 	spin_unlock(&session->s_cap_lock);
8997c1332b8SSage Weil 
900f818a736SSage Weil 	/* remove from inode list */
901f818a736SSage Weil 	rb_erase(&cap->ci_node, &ci->i_caps);
902f818a736SSage Weil 	if (ci->i_auth_cap == cap)
903f818a736SSage Weil 		ci->i_auth_cap = NULL;
904f818a736SSage Weil 
905f818a736SSage Weil 	if (removed)
90637151668SYehuda Sadeh 		ceph_put_cap(mdsc, cap);
907a8599bd8SSage Weil 
908a8599bd8SSage Weil 	if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) {
909a8599bd8SSage Weil 		struct ceph_snap_realm *realm = ci->i_snap_realm;
910a8599bd8SSage Weil 		spin_lock(&realm->inodes_with_caps_lock);
911a8599bd8SSage Weil 		list_del_init(&ci->i_snap_realm_item);
912a8599bd8SSage Weil 		ci->i_snap_realm_counter++;
913a8599bd8SSage Weil 		ci->i_snap_realm = NULL;
914a8599bd8SSage Weil 		spin_unlock(&realm->inodes_with_caps_lock);
915a8599bd8SSage Weil 		ceph_put_snap_realm(mdsc, realm);
916a8599bd8SSage Weil 	}
917a8599bd8SSage Weil 	if (!__ceph_is_any_real_caps(ci))
918a8599bd8SSage Weil 		__cap_delay_cancel(mdsc, ci);
919a8599bd8SSage Weil }
920a8599bd8SSage Weil 
921a8599bd8SSage Weil /*
922a8599bd8SSage Weil  * Build and send a cap message to the given MDS.
923a8599bd8SSage Weil  *
924a8599bd8SSage Weil  * Caller should be holding s_mutex.
925a8599bd8SSage Weil  */
926a8599bd8SSage Weil static int send_cap_msg(struct ceph_mds_session *session,
927a8599bd8SSage Weil 			u64 ino, u64 cid, int op,
928a8599bd8SSage Weil 			int caps, int wanted, int dirty,
929a8599bd8SSage Weil 			u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq,
930a8599bd8SSage Weil 			u64 size, u64 max_size,
931a8599bd8SSage Weil 			struct timespec *mtime, struct timespec *atime,
932a8599bd8SSage Weil 			u64 time_warp_seq,
9335706b27dSAl Viro 			uid_t uid, gid_t gid, umode_t mode,
934a8599bd8SSage Weil 			u64 xattr_version,
935a8599bd8SSage Weil 			struct ceph_buffer *xattrs_buf,
936a8599bd8SSage Weil 			u64 follows)
937a8599bd8SSage Weil {
938a8599bd8SSage Weil 	struct ceph_mds_caps *fc;
939a8599bd8SSage Weil 	struct ceph_msg *msg;
940a8599bd8SSage Weil 
941a8599bd8SSage Weil 	dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
942a8599bd8SSage Weil 	     " seq %u/%u mseq %u follows %lld size %llu/%llu"
943a8599bd8SSage Weil 	     " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op),
944a8599bd8SSage Weil 	     cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted),
945a8599bd8SSage Weil 	     ceph_cap_string(dirty),
946a8599bd8SSage Weil 	     seq, issue_seq, mseq, follows, size, max_size,
947a8599bd8SSage Weil 	     xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0);
948a8599bd8SSage Weil 
949b61c2763SSage Weil 	msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false);
950a79832f2SSage Weil 	if (!msg)
951a79832f2SSage Weil 		return -ENOMEM;
952a8599bd8SSage Weil 
9536df058c0SSage Weil 	msg->hdr.tid = cpu_to_le64(flush_tid);
954a8599bd8SSage Weil 
9556df058c0SSage Weil 	fc = msg->front.iov_base;
956a8599bd8SSage Weil 	memset(fc, 0, sizeof(*fc));
957a8599bd8SSage Weil 
958a8599bd8SSage Weil 	fc->cap_id = cpu_to_le64(cid);
959a8599bd8SSage Weil 	fc->op = cpu_to_le32(op);
960a8599bd8SSage Weil 	fc->seq = cpu_to_le32(seq);
961a8599bd8SSage Weil 	fc->issue_seq = cpu_to_le32(issue_seq);
962a8599bd8SSage Weil 	fc->migrate_seq = cpu_to_le32(mseq);
963a8599bd8SSage Weil 	fc->caps = cpu_to_le32(caps);
964a8599bd8SSage Weil 	fc->wanted = cpu_to_le32(wanted);
965a8599bd8SSage Weil 	fc->dirty = cpu_to_le32(dirty);
966a8599bd8SSage Weil 	fc->ino = cpu_to_le64(ino);
967a8599bd8SSage Weil 	fc->snap_follows = cpu_to_le64(follows);
968a8599bd8SSage Weil 
969a8599bd8SSage Weil 	fc->size = cpu_to_le64(size);
970a8599bd8SSage Weil 	fc->max_size = cpu_to_le64(max_size);
971a8599bd8SSage Weil 	if (mtime)
972a8599bd8SSage Weil 		ceph_encode_timespec(&fc->mtime, mtime);
973a8599bd8SSage Weil 	if (atime)
974a8599bd8SSage Weil 		ceph_encode_timespec(&fc->atime, atime);
975a8599bd8SSage Weil 	fc->time_warp_seq = cpu_to_le32(time_warp_seq);
976a8599bd8SSage Weil 
977a8599bd8SSage Weil 	fc->uid = cpu_to_le32(uid);
978a8599bd8SSage Weil 	fc->gid = cpu_to_le32(gid);
979a8599bd8SSage Weil 	fc->mode = cpu_to_le32(mode);
980a8599bd8SSage Weil 
981a8599bd8SSage Weil 	fc->xattr_version = cpu_to_le64(xattr_version);
982a8599bd8SSage Weil 	if (xattrs_buf) {
983a8599bd8SSage Weil 		msg->middle = ceph_buffer_get(xattrs_buf);
984a8599bd8SSage Weil 		fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len);
985a8599bd8SSage Weil 		msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len);
986a8599bd8SSage Weil 	}
987a8599bd8SSage Weil 
988a8599bd8SSage Weil 	ceph_con_send(&session->s_con, msg);
989a8599bd8SSage Weil 	return 0;
990a8599bd8SSage Weil }
991a8599bd8SSage Weil 
9923d7ded4dSSage Weil static void __queue_cap_release(struct ceph_mds_session *session,
9933d7ded4dSSage Weil 				u64 ino, u64 cap_id, u32 migrate_seq,
9943d7ded4dSSage Weil 				u32 issue_seq)
9953d7ded4dSSage Weil {
9963d7ded4dSSage Weil 	struct ceph_msg *msg;
9973d7ded4dSSage Weil 	struct ceph_mds_cap_release *head;
9983d7ded4dSSage Weil 	struct ceph_mds_cap_item *item;
9993d7ded4dSSage Weil 
10003d7ded4dSSage Weil 	spin_lock(&session->s_cap_lock);
10013d7ded4dSSage Weil 	BUG_ON(!session->s_num_cap_releases);
10023d7ded4dSSage Weil 	msg = list_first_entry(&session->s_cap_releases,
10033d7ded4dSSage Weil 			       struct ceph_msg, list_head);
10043d7ded4dSSage Weil 
10053d7ded4dSSage Weil 	dout(" adding %llx release to mds%d msg %p (%d left)\n",
10063d7ded4dSSage Weil 	     ino, session->s_mds, msg, session->s_num_cap_releases);
10073d7ded4dSSage Weil 
10083d7ded4dSSage Weil 	BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE);
10093d7ded4dSSage Weil 	head = msg->front.iov_base;
1010b905a7f8SWei Yongjun 	le32_add_cpu(&head->num, 1);
10113d7ded4dSSage Weil 	item = msg->front.iov_base + msg->front.iov_len;
10123d7ded4dSSage Weil 	item->ino = cpu_to_le64(ino);
10133d7ded4dSSage Weil 	item->cap_id = cpu_to_le64(cap_id);
10143d7ded4dSSage Weil 	item->migrate_seq = cpu_to_le32(migrate_seq);
10153d7ded4dSSage Weil 	item->seq = cpu_to_le32(issue_seq);
10163d7ded4dSSage Weil 
10173d7ded4dSSage Weil 	session->s_num_cap_releases--;
10183d7ded4dSSage Weil 
10193d7ded4dSSage Weil 	msg->front.iov_len += sizeof(*item);
10203d7ded4dSSage Weil 	if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
10213d7ded4dSSage Weil 		dout(" release msg %p full\n", msg);
10223d7ded4dSSage Weil 		list_move_tail(&msg->list_head, &session->s_cap_releases_done);
10233d7ded4dSSage Weil 	} else {
10243d7ded4dSSage Weil 		dout(" release msg %p at %d/%d (%d)\n", msg,
10253d7ded4dSSage Weil 		     (int)le32_to_cpu(head->num),
10263d7ded4dSSage Weil 		     (int)CEPH_CAPS_PER_RELEASE,
10273d7ded4dSSage Weil 		     (int)msg->front.iov_len);
10283d7ded4dSSage Weil 	}
10293d7ded4dSSage Weil 	spin_unlock(&session->s_cap_lock);
10303d7ded4dSSage Weil }
10313d7ded4dSSage Weil 
1032a8599bd8SSage Weil /*
1033a6369741SSage Weil  * Queue cap releases when an inode is dropped from our cache.  Since
1034be655596SSage Weil  * inode is about to be destroyed, there is no need for i_ceph_lock.
1035a8599bd8SSage Weil  */
1036a8599bd8SSage Weil void ceph_queue_caps_release(struct inode *inode)
1037a8599bd8SSage Weil {
1038a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1039a8599bd8SSage Weil 	struct rb_node *p;
1040a8599bd8SSage Weil 
1041a8599bd8SSage Weil 	p = rb_first(&ci->i_caps);
1042a8599bd8SSage Weil 	while (p) {
1043a8599bd8SSage Weil 		struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1044a8599bd8SSage Weil 		struct ceph_mds_session *session = cap->session;
1045a8599bd8SSage Weil 
10463d7ded4dSSage Weil 		__queue_cap_release(session, ceph_ino(inode), cap->cap_id,
10473d7ded4dSSage Weil 				    cap->mseq, cap->issue_seq);
1048a8599bd8SSage Weil 		p = rb_next(p);
10497c1332b8SSage Weil 		__ceph_remove_cap(cap);
1050a8599bd8SSage Weil 	}
1051a8599bd8SSage Weil }
1052a8599bd8SSage Weil 
1053a8599bd8SSage Weil /*
1054a8599bd8SSage Weil  * Send a cap msg on the given inode.  Update our caps state, then
1055be655596SSage Weil  * drop i_ceph_lock and send the message.
1056a8599bd8SSage Weil  *
1057a8599bd8SSage Weil  * Make note of max_size reported/requested from mds, revoked caps
1058a8599bd8SSage Weil  * that have now been implemented.
1059a8599bd8SSage Weil  *
1060a8599bd8SSage Weil  * Make half-hearted attempt ot to invalidate page cache if we are
1061a8599bd8SSage Weil  * dropping RDCACHE.  Note that this will leave behind locked pages
1062a8599bd8SSage Weil  * that we'll then need to deal with elsewhere.
1063a8599bd8SSage Weil  *
1064a8599bd8SSage Weil  * Return non-zero if delayed release, or we experienced an error
1065a8599bd8SSage Weil  * such that the caller should requeue + retry later.
1066a8599bd8SSage Weil  *
1067be655596SSage Weil  * called with i_ceph_lock, then drops it.
1068a8599bd8SSage Weil  * caller should hold snap_rwsem (read), s_mutex.
1069a8599bd8SSage Weil  */
1070a8599bd8SSage Weil static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1071a8599bd8SSage Weil 		      int op, int used, int want, int retain, int flushing,
1072a8599bd8SSage Weil 		      unsigned *pflush_tid)
1073be655596SSage Weil 	__releases(cap->ci->i_ceph_lock)
1074a8599bd8SSage Weil {
1075a8599bd8SSage Weil 	struct ceph_inode_info *ci = cap->ci;
1076a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
1077a8599bd8SSage Weil 	u64 cap_id = cap->cap_id;
107868c28323SSage Weil 	int held, revoking, dropping, keep;
1079a8599bd8SSage Weil 	u64 seq, issue_seq, mseq, time_warp_seq, follows;
1080a8599bd8SSage Weil 	u64 size, max_size;
1081a8599bd8SSage Weil 	struct timespec mtime, atime;
1082a8599bd8SSage Weil 	int wake = 0;
10835706b27dSAl Viro 	umode_t mode;
1084a8599bd8SSage Weil 	uid_t uid;
1085a8599bd8SSage Weil 	gid_t gid;
1086a8599bd8SSage Weil 	struct ceph_mds_session *session;
1087a8599bd8SSage Weil 	u64 xattr_version = 0;
1088082afec9SSage Weil 	struct ceph_buffer *xattr_blob = NULL;
1089a8599bd8SSage Weil 	int delayed = 0;
1090a8599bd8SSage Weil 	u64 flush_tid = 0;
1091a8599bd8SSage Weil 	int i;
1092a8599bd8SSage Weil 	int ret;
1093a8599bd8SSage Weil 
109468c28323SSage Weil 	held = cap->issued | cap->implemented;
109568c28323SSage Weil 	revoking = cap->implemented & ~cap->issued;
109668c28323SSage Weil 	retain &= ~revoking;
109768c28323SSage Weil 	dropping = cap->issued & ~retain;
109868c28323SSage Weil 
1099a8599bd8SSage Weil 	dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1100a8599bd8SSage Weil 	     inode, cap, cap->session,
1101a8599bd8SSage Weil 	     ceph_cap_string(held), ceph_cap_string(held & retain),
1102a8599bd8SSage Weil 	     ceph_cap_string(revoking));
1103a8599bd8SSage Weil 	BUG_ON((retain & CEPH_CAP_PIN) == 0);
1104a8599bd8SSage Weil 
1105a8599bd8SSage Weil 	session = cap->session;
1106a8599bd8SSage Weil 
1107a8599bd8SSage Weil 	/* don't release wanted unless we've waited a bit. */
1108a8599bd8SSage Weil 	if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1109a8599bd8SSage Weil 	    time_before(jiffies, ci->i_hold_caps_min)) {
1110a8599bd8SSage Weil 		dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1111a8599bd8SSage Weil 		     ceph_cap_string(cap->issued),
1112a8599bd8SSage Weil 		     ceph_cap_string(cap->issued & retain),
1113a8599bd8SSage Weil 		     ceph_cap_string(cap->mds_wanted),
1114a8599bd8SSage Weil 		     ceph_cap_string(want));
1115a8599bd8SSage Weil 		want |= cap->mds_wanted;
1116a8599bd8SSage Weil 		retain |= cap->issued;
1117a8599bd8SSage Weil 		delayed = 1;
1118a8599bd8SSage Weil 	}
1119a8599bd8SSage Weil 	ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
1120a8599bd8SSage Weil 
1121a8599bd8SSage Weil 	cap->issued &= retain;  /* drop bits we don't want */
1122a8599bd8SSage Weil 	if (cap->implemented & ~cap->issued) {
1123a8599bd8SSage Weil 		/*
1124a8599bd8SSage Weil 		 * Wake up any waiters on wanted -> needed transition.
1125a8599bd8SSage Weil 		 * This is due to the weird transition from buffered
1126a8599bd8SSage Weil 		 * to sync IO... we need to flush dirty pages _before_
1127a8599bd8SSage Weil 		 * allowing sync writes to avoid reordering.
1128a8599bd8SSage Weil 		 */
1129a8599bd8SSage Weil 		wake = 1;
1130a8599bd8SSage Weil 	}
1131a8599bd8SSage Weil 	cap->implemented &= cap->issued | used;
1132a8599bd8SSage Weil 	cap->mds_wanted = want;
1133a8599bd8SSage Weil 
1134a8599bd8SSage Weil 	if (flushing) {
1135a8599bd8SSage Weil 		/*
1136a8599bd8SSage Weil 		 * assign a tid for flush operations so we can avoid
1137a8599bd8SSage Weil 		 * flush1 -> dirty1 -> flush2 -> flushack1 -> mark
1138a8599bd8SSage Weil 		 * clean type races.  track latest tid for every bit
1139a8599bd8SSage Weil 		 * so we can handle flush AxFw, flush Fw, and have the
1140a8599bd8SSage Weil 		 * first ack clean Ax.
1141a8599bd8SSage Weil 		 */
1142a8599bd8SSage Weil 		flush_tid = ++ci->i_cap_flush_last_tid;
1143a8599bd8SSage Weil 		if (pflush_tid)
1144a8599bd8SSage Weil 			*pflush_tid = flush_tid;
1145a8599bd8SSage Weil 		dout(" cap_flush_tid %d\n", (int)flush_tid);
1146a8599bd8SSage Weil 		for (i = 0; i < CEPH_CAP_BITS; i++)
1147a8599bd8SSage Weil 			if (flushing & (1 << i))
1148a8599bd8SSage Weil 				ci->i_cap_flush_tid[i] = flush_tid;
11497d8cb26dSSage Weil 
11507d8cb26dSSage Weil 		follows = ci->i_head_snapc->seq;
11517d8cb26dSSage Weil 	} else {
11527d8cb26dSSage Weil 		follows = 0;
1153a8599bd8SSage Weil 	}
1154a8599bd8SSage Weil 
1155a8599bd8SSage Weil 	keep = cap->implemented;
1156a8599bd8SSage Weil 	seq = cap->seq;
1157a8599bd8SSage Weil 	issue_seq = cap->issue_seq;
1158a8599bd8SSage Weil 	mseq = cap->mseq;
1159a8599bd8SSage Weil 	size = inode->i_size;
1160a8599bd8SSage Weil 	ci->i_reported_size = size;
1161a8599bd8SSage Weil 	max_size = ci->i_wanted_max_size;
1162a8599bd8SSage Weil 	ci->i_requested_max_size = max_size;
1163a8599bd8SSage Weil 	mtime = inode->i_mtime;
1164a8599bd8SSage Weil 	atime = inode->i_atime;
1165a8599bd8SSage Weil 	time_warp_seq = ci->i_time_warp_seq;
1166a8599bd8SSage Weil 	uid = inode->i_uid;
1167a8599bd8SSage Weil 	gid = inode->i_gid;
1168a8599bd8SSage Weil 	mode = inode->i_mode;
1169a8599bd8SSage Weil 
1170082afec9SSage Weil 	if (flushing & CEPH_CAP_XATTR_EXCL) {
1171a8599bd8SSage Weil 		__ceph_build_xattrs_blob(ci);
1172082afec9SSage Weil 		xattr_blob = ci->i_xattrs.blob;
1173082afec9SSage Weil 		xattr_version = ci->i_xattrs.version;
1174a8599bd8SSage Weil 	}
1175a8599bd8SSage Weil 
1176be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1177a8599bd8SSage Weil 
1178a8599bd8SSage Weil 	ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id,
1179a8599bd8SSage Weil 		op, keep, want, flushing, seq, flush_tid, issue_seq, mseq,
1180a8599bd8SSage Weil 		size, max_size, &mtime, &atime, time_warp_seq,
1181082afec9SSage Weil 		uid, gid, mode, xattr_version, xattr_blob,
1182a8599bd8SSage Weil 		follows);
1183a8599bd8SSage Weil 	if (ret < 0) {
1184a8599bd8SSage Weil 		dout("error sending cap msg, must requeue %p\n", inode);
1185a8599bd8SSage Weil 		delayed = 1;
1186a8599bd8SSage Weil 	}
1187a8599bd8SSage Weil 
1188a8599bd8SSage Weil 	if (wake)
118903066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
1190a8599bd8SSage Weil 
1191a8599bd8SSage Weil 	return delayed;
1192a8599bd8SSage Weil }
1193a8599bd8SSage Weil 
1194a8599bd8SSage Weil /*
1195a8599bd8SSage Weil  * When a snapshot is taken, clients accumulate dirty metadata on
1196a8599bd8SSage Weil  * inodes with capabilities in ceph_cap_snaps to describe the file
1197a8599bd8SSage Weil  * state at the time the snapshot was taken.  This must be flushed
1198a8599bd8SSage Weil  * asynchronously back to the MDS once sync writes complete and dirty
1199a8599bd8SSage Weil  * data is written out.
1200a8599bd8SSage Weil  *
1201e835124cSSage Weil  * Unless @again is true, skip cap_snaps that were already sent to
1202e835124cSSage Weil  * the MDS (i.e., during this session).
1203e835124cSSage Weil  *
1204be655596SSage Weil  * Called under i_ceph_lock.  Takes s_mutex as needed.
1205a8599bd8SSage Weil  */
1206a8599bd8SSage Weil void __ceph_flush_snaps(struct ceph_inode_info *ci,
1207e835124cSSage Weil 			struct ceph_mds_session **psession,
1208e835124cSSage Weil 			int again)
1209be655596SSage Weil 		__releases(ci->i_ceph_lock)
1210be655596SSage Weil 		__acquires(ci->i_ceph_lock)
1211a8599bd8SSage Weil {
1212a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
1213a8599bd8SSage Weil 	int mds;
1214a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
1215a8599bd8SSage Weil 	u32 mseq;
12163d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
1217a8599bd8SSage Weil 	struct ceph_mds_session *session = NULL; /* if session != NULL, we hold
1218a8599bd8SSage Weil 						    session->s_mutex */
1219a8599bd8SSage Weil 	u64 next_follows = 0;  /* keep track of how far we've gotten through the
1220a8599bd8SSage Weil 			     i_cap_snaps list, and skip these entries next time
1221a8599bd8SSage Weil 			     around to avoid an infinite loop */
1222a8599bd8SSage Weil 
1223a8599bd8SSage Weil 	if (psession)
1224a8599bd8SSage Weil 		session = *psession;
1225a8599bd8SSage Weil 
1226a8599bd8SSage Weil 	dout("__flush_snaps %p\n", inode);
1227a8599bd8SSage Weil retry:
1228a8599bd8SSage Weil 	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1229a8599bd8SSage Weil 		/* avoid an infiniute loop after retry */
1230a8599bd8SSage Weil 		if (capsnap->follows < next_follows)
1231a8599bd8SSage Weil 			continue;
1232a8599bd8SSage Weil 		/*
1233a8599bd8SSage Weil 		 * we need to wait for sync writes to complete and for dirty
1234a8599bd8SSage Weil 		 * pages to be written out.
1235a8599bd8SSage Weil 		 */
1236a8599bd8SSage Weil 		if (capsnap->dirty_pages || capsnap->writing)
1237cfc0bf66SSage Weil 			break;
1238a8599bd8SSage Weil 
1239819ccbfaSSage Weil 		/*
1240819ccbfaSSage Weil 		 * if cap writeback already occurred, we should have dropped
1241819ccbfaSSage Weil 		 * the capsnap in ceph_put_wrbuffer_cap_refs.
1242819ccbfaSSage Weil 		 */
1243819ccbfaSSage Weil 		BUG_ON(capsnap->dirty == 0);
1244819ccbfaSSage Weil 
1245a8599bd8SSage Weil 		/* pick mds, take s_mutex */
1246ca81f3f6SSage Weil 		if (ci->i_auth_cap == NULL) {
1247ca81f3f6SSage Weil 			dout("no auth cap (migrating?), doing nothing\n");
1248ca81f3f6SSage Weil 			goto out;
1249ca81f3f6SSage Weil 		}
1250e835124cSSage Weil 
1251e835124cSSage Weil 		/* only flush each capsnap once */
1252e835124cSSage Weil 		if (!again && !list_empty(&capsnap->flushing_item)) {
1253e835124cSSage Weil 			dout("already flushed %p, skipping\n", capsnap);
1254e835124cSSage Weil 			continue;
1255e835124cSSage Weil 		}
1256e835124cSSage Weil 
1257ca81f3f6SSage Weil 		mds = ci->i_auth_cap->session->s_mds;
1258ca81f3f6SSage Weil 		mseq = ci->i_auth_cap->mseq;
1259ca81f3f6SSage Weil 
1260a8599bd8SSage Weil 		if (session && session->s_mds != mds) {
1261a8599bd8SSage Weil 			dout("oops, wrong session %p mutex\n", session);
1262a8599bd8SSage Weil 			mutex_unlock(&session->s_mutex);
1263a8599bd8SSage Weil 			ceph_put_mds_session(session);
1264a8599bd8SSage Weil 			session = NULL;
1265a8599bd8SSage Weil 		}
1266a8599bd8SSage Weil 		if (!session) {
1267be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
1268a8599bd8SSage Weil 			mutex_lock(&mdsc->mutex);
1269a8599bd8SSage Weil 			session = __ceph_lookup_mds_session(mdsc, mds);
1270a8599bd8SSage Weil 			mutex_unlock(&mdsc->mutex);
1271a8599bd8SSage Weil 			if (session) {
1272a8599bd8SSage Weil 				dout("inverting session/ino locks on %p\n",
1273a8599bd8SSage Weil 				     session);
1274a8599bd8SSage Weil 				mutex_lock(&session->s_mutex);
1275a8599bd8SSage Weil 			}
1276a8599bd8SSage Weil 			/*
1277a8599bd8SSage Weil 			 * if session == NULL, we raced against a cap
1278ca81f3f6SSage Weil 			 * deletion or migration.  retry, and we'll
1279ca81f3f6SSage Weil 			 * get a better @mds value next time.
1280a8599bd8SSage Weil 			 */
1281be655596SSage Weil 			spin_lock(&ci->i_ceph_lock);
1282a8599bd8SSage Weil 			goto retry;
1283a8599bd8SSage Weil 		}
1284a8599bd8SSage Weil 
1285a8599bd8SSage Weil 		capsnap->flush_tid = ++ci->i_cap_flush_last_tid;
1286a8599bd8SSage Weil 		atomic_inc(&capsnap->nref);
1287a8599bd8SSage Weil 		if (!list_empty(&capsnap->flushing_item))
1288a8599bd8SSage Weil 			list_del_init(&capsnap->flushing_item);
1289a8599bd8SSage Weil 		list_add_tail(&capsnap->flushing_item,
1290a8599bd8SSage Weil 			      &session->s_cap_snaps_flushing);
1291be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1292a8599bd8SSage Weil 
1293cfc0bf66SSage Weil 		dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n",
1294cfc0bf66SSage Weil 		     inode, capsnap, capsnap->follows, capsnap->flush_tid);
1295a8599bd8SSage Weil 		send_cap_msg(session, ceph_vino(inode).ino, 0,
1296a8599bd8SSage Weil 			     CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0,
1297a8599bd8SSage Weil 			     capsnap->dirty, 0, capsnap->flush_tid, 0, mseq,
1298a8599bd8SSage Weil 			     capsnap->size, 0,
1299a8599bd8SSage Weil 			     &capsnap->mtime, &capsnap->atime,
1300a8599bd8SSage Weil 			     capsnap->time_warp_seq,
1301a8599bd8SSage Weil 			     capsnap->uid, capsnap->gid, capsnap->mode,
13024a625be4SSage Weil 			     capsnap->xattr_version, capsnap->xattr_blob,
1303a8599bd8SSage Weil 			     capsnap->follows);
1304a8599bd8SSage Weil 
1305a8599bd8SSage Weil 		next_follows = capsnap->follows + 1;
1306a8599bd8SSage Weil 		ceph_put_cap_snap(capsnap);
1307a8599bd8SSage Weil 
1308be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1309a8599bd8SSage Weil 		goto retry;
1310a8599bd8SSage Weil 	}
1311a8599bd8SSage Weil 
1312a8599bd8SSage Weil 	/* we flushed them all; remove this inode from the queue */
1313a8599bd8SSage Weil 	spin_lock(&mdsc->snap_flush_lock);
1314a8599bd8SSage Weil 	list_del_init(&ci->i_snap_flush_item);
1315a8599bd8SSage Weil 	spin_unlock(&mdsc->snap_flush_lock);
1316a8599bd8SSage Weil 
1317ca81f3f6SSage Weil out:
1318a8599bd8SSage Weil 	if (psession)
1319a8599bd8SSage Weil 		*psession = session;
1320a8599bd8SSage Weil 	else if (session) {
1321a8599bd8SSage Weil 		mutex_unlock(&session->s_mutex);
1322a8599bd8SSage Weil 		ceph_put_mds_session(session);
1323a8599bd8SSage Weil 	}
1324a8599bd8SSage Weil }
1325a8599bd8SSage Weil 
1326a8599bd8SSage Weil static void ceph_flush_snaps(struct ceph_inode_info *ci)
1327a8599bd8SSage Weil {
1328be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1329e835124cSSage Weil 	__ceph_flush_snaps(ci, NULL, 0);
1330be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1331a8599bd8SSage Weil }
1332a8599bd8SSage Weil 
1333a8599bd8SSage Weil /*
1334fca65b4aSSage Weil  * Mark caps dirty.  If inode is newly dirty, return the dirty flags.
1335fca65b4aSSage Weil  * Caller is then responsible for calling __mark_inode_dirty with the
1336fca65b4aSSage Weil  * returned flags value.
133776e3b390SSage Weil  */
1338fca65b4aSSage Weil int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask)
133976e3b390SSage Weil {
1340640ef79dSCheng Renquan 	struct ceph_mds_client *mdsc =
13413d14c5d2SYehuda Sadeh 		ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
134276e3b390SSage Weil 	struct inode *inode = &ci->vfs_inode;
134376e3b390SSage Weil 	int was = ci->i_dirty_caps;
134476e3b390SSage Weil 	int dirty = 0;
134576e3b390SSage Weil 
134676e3b390SSage Weil 	dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
134776e3b390SSage Weil 	     ceph_cap_string(mask), ceph_cap_string(was),
134876e3b390SSage Weil 	     ceph_cap_string(was | mask));
134976e3b390SSage Weil 	ci->i_dirty_caps |= mask;
135076e3b390SSage Weil 	if (was == 0) {
13517d8cb26dSSage Weil 		if (!ci->i_head_snapc)
13527d8cb26dSSage Weil 			ci->i_head_snapc = ceph_get_snap_context(
13537d8cb26dSSage Weil 				ci->i_snap_realm->cached_context);
13547d8cb26dSSage Weil 		dout(" inode %p now dirty snapc %p\n", &ci->vfs_inode,
13557d8cb26dSSage Weil 			ci->i_head_snapc);
135676e3b390SSage Weil 		BUG_ON(!list_empty(&ci->i_dirty_item));
135776e3b390SSage Weil 		spin_lock(&mdsc->cap_dirty_lock);
135876e3b390SSage Weil 		list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
135976e3b390SSage Weil 		spin_unlock(&mdsc->cap_dirty_lock);
136076e3b390SSage Weil 		if (ci->i_flushing_caps == 0) {
13613772d26dSSage Weil 			ihold(inode);
136276e3b390SSage Weil 			dirty |= I_DIRTY_SYNC;
136376e3b390SSage Weil 		}
136476e3b390SSage Weil 	}
136576e3b390SSage Weil 	BUG_ON(list_empty(&ci->i_dirty_item));
136676e3b390SSage Weil 	if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
136776e3b390SSage Weil 	    (mask & CEPH_CAP_FILE_BUFFER))
136876e3b390SSage Weil 		dirty |= I_DIRTY_DATASYNC;
136976e3b390SSage Weil 	__cap_delay_requeue(mdsc, ci);
1370fca65b4aSSage Weil 	return dirty;
137176e3b390SSage Weil }
137276e3b390SSage Weil 
137376e3b390SSage Weil /*
1374a8599bd8SSage Weil  * Add dirty inode to the flushing list.  Assigned a seq number so we
1375a8599bd8SSage Weil  * can wait for caps to flush without starving.
1376cdc35f96SSage Weil  *
1377be655596SSage Weil  * Called under i_ceph_lock.
1378a8599bd8SSage Weil  */
1379cdc35f96SSage Weil static int __mark_caps_flushing(struct inode *inode,
1380a8599bd8SSage Weil 				 struct ceph_mds_session *session)
1381a8599bd8SSage Weil {
13823d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1383a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1384cdc35f96SSage Weil 	int flushing;
1385a8599bd8SSage Weil 
1386cdc35f96SSage Weil 	BUG_ON(ci->i_dirty_caps == 0);
1387a8599bd8SSage Weil 	BUG_ON(list_empty(&ci->i_dirty_item));
1388cdc35f96SSage Weil 
1389cdc35f96SSage Weil 	flushing = ci->i_dirty_caps;
1390cdc35f96SSage Weil 	dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1391cdc35f96SSage Weil 	     ceph_cap_string(flushing),
1392cdc35f96SSage Weil 	     ceph_cap_string(ci->i_flushing_caps),
1393cdc35f96SSage Weil 	     ceph_cap_string(ci->i_flushing_caps | flushing));
1394cdc35f96SSage Weil 	ci->i_flushing_caps |= flushing;
1395cdc35f96SSage Weil 	ci->i_dirty_caps = 0;
1396afcdaea3SSage Weil 	dout(" inode %p now !dirty\n", inode);
1397cdc35f96SSage Weil 
1398a8599bd8SSage Weil 	spin_lock(&mdsc->cap_dirty_lock);
1399cdc35f96SSage Weil 	list_del_init(&ci->i_dirty_item);
1400afcdaea3SSage Weil 
1401afcdaea3SSage Weil 	ci->i_cap_flush_seq = ++mdsc->cap_flush_seq;
1402afcdaea3SSage Weil 	if (list_empty(&ci->i_flushing_item)) {
1403a8599bd8SSage Weil 		list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1404a8599bd8SSage Weil 		mdsc->num_cap_flushing++;
1405afcdaea3SSage Weil 		dout(" inode %p now flushing seq %lld\n", inode,
1406afcdaea3SSage Weil 		     ci->i_cap_flush_seq);
1407afcdaea3SSage Weil 	} else {
1408afcdaea3SSage Weil 		list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1409afcdaea3SSage Weil 		dout(" inode %p now flushing (more) seq %lld\n", inode,
1410a8599bd8SSage Weil 		     ci->i_cap_flush_seq);
1411a8599bd8SSage Weil 	}
1412a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_dirty_lock);
1413cdc35f96SSage Weil 
1414cdc35f96SSage Weil 	return flushing;
1415a8599bd8SSage Weil }
1416a8599bd8SSage Weil 
1417a8599bd8SSage Weil /*
14185ecad6fdSSage Weil  * try to invalidate mapping pages without blocking.
14195ecad6fdSSage Weil  */
14205ecad6fdSSage Weil static int try_nonblocking_invalidate(struct inode *inode)
14215ecad6fdSSage Weil {
14225ecad6fdSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
14235ecad6fdSSage Weil 	u32 invalidating_gen = ci->i_rdcache_gen;
14245ecad6fdSSage Weil 
1425be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
14265ecad6fdSSage Weil 	invalidate_mapping_pages(&inode->i_data, 0, -1);
1427be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
14285ecad6fdSSage Weil 
142918a38193SSage Weil 	if (inode->i_data.nrpages == 0 &&
14305ecad6fdSSage Weil 	    invalidating_gen == ci->i_rdcache_gen) {
14315ecad6fdSSage Weil 		/* success. */
14325ecad6fdSSage Weil 		dout("try_nonblocking_invalidate %p success\n", inode);
1433cd045cb4SSage Weil 		/* save any racing async invalidate some trouble */
1434cd045cb4SSage Weil 		ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
14355ecad6fdSSage Weil 		return 0;
14365ecad6fdSSage Weil 	}
14375ecad6fdSSage Weil 	dout("try_nonblocking_invalidate %p failed\n", inode);
14385ecad6fdSSage Weil 	return -1;
14395ecad6fdSSage Weil }
14405ecad6fdSSage Weil 
14415ecad6fdSSage Weil /*
1442a8599bd8SSage Weil  * Swiss army knife function to examine currently used and wanted
1443a8599bd8SSage Weil  * versus held caps.  Release, flush, ack revoked caps to mds as
1444a8599bd8SSage Weil  * appropriate.
1445a8599bd8SSage Weil  *
1446a8599bd8SSage Weil  *  CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1447a8599bd8SSage Weil  *    cap release further.
1448a8599bd8SSage Weil  *  CHECK_CAPS_AUTHONLY - we should only check the auth cap
1449a8599bd8SSage Weil  *  CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1450a8599bd8SSage Weil  *    further delay.
1451a8599bd8SSage Weil  */
1452a8599bd8SSage Weil void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1453a8599bd8SSage Weil 		     struct ceph_mds_session *session)
1454a8599bd8SSage Weil {
14553d14c5d2SYehuda Sadeh 	struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
14563d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = fsc->mdsc;
1457a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
1458a8599bd8SSage Weil 	struct ceph_cap *cap;
1459a8599bd8SSage Weil 	int file_wanted, used;
1460a8599bd8SSage Weil 	int took_snap_rwsem = 0;             /* true if mdsc->snap_rwsem held */
1461cbd03635SSage Weil 	int issued, implemented, want, retain, revoking, flushing = 0;
1462a8599bd8SSage Weil 	int mds = -1;   /* keep track of how far we've gone through i_caps list
1463a8599bd8SSage Weil 			   to avoid an infinite loop on retry */
1464a8599bd8SSage Weil 	struct rb_node *p;
1465a8599bd8SSage Weil 	int tried_invalidate = 0;
1466a8599bd8SSage Weil 	int delayed = 0, sent = 0, force_requeue = 0, num;
1467cbd03635SSage Weil 	int queue_invalidate = 0;
1468a8599bd8SSage Weil 	int is_delayed = flags & CHECK_CAPS_NODELAY;
1469a8599bd8SSage Weil 
1470a8599bd8SSage Weil 	/* if we are unmounting, flush any unused caps immediately. */
1471a8599bd8SSage Weil 	if (mdsc->stopping)
1472a8599bd8SSage Weil 		is_delayed = 1;
1473a8599bd8SSage Weil 
1474be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1475a8599bd8SSage Weil 
1476a8599bd8SSage Weil 	if (ci->i_ceph_flags & CEPH_I_FLUSH)
1477a8599bd8SSage Weil 		flags |= CHECK_CAPS_FLUSH;
1478a8599bd8SSage Weil 
1479a8599bd8SSage Weil 	/* flush snaps first time around only */
1480a8599bd8SSage Weil 	if (!list_empty(&ci->i_cap_snaps))
1481e835124cSSage Weil 		__ceph_flush_snaps(ci, &session, 0);
1482a8599bd8SSage Weil 	goto retry_locked;
1483a8599bd8SSage Weil retry:
1484be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1485a8599bd8SSage Weil retry_locked:
1486a8599bd8SSage Weil 	file_wanted = __ceph_caps_file_wanted(ci);
1487a8599bd8SSage Weil 	used = __ceph_caps_used(ci);
1488a8599bd8SSage Weil 	want = file_wanted | used;
1489cbd03635SSage Weil 	issued = __ceph_caps_issued(ci, &implemented);
1490cbd03635SSage Weil 	revoking = implemented & ~issued;
1491a8599bd8SSage Weil 
1492a8599bd8SSage Weil 	retain = want | CEPH_CAP_PIN;
1493a8599bd8SSage Weil 	if (!mdsc->stopping && inode->i_nlink > 0) {
1494a8599bd8SSage Weil 		if (want) {
1495a8599bd8SSage Weil 			retain |= CEPH_CAP_ANY;       /* be greedy */
1496a8599bd8SSage Weil 		} else {
1497a8599bd8SSage Weil 			retain |= CEPH_CAP_ANY_SHARED;
1498a8599bd8SSage Weil 			/*
1499a8599bd8SSage Weil 			 * keep RD only if we didn't have the file open RW,
1500a8599bd8SSage Weil 			 * because then the mds would revoke it anyway to
1501a8599bd8SSage Weil 			 * journal max_size=0.
1502a8599bd8SSage Weil 			 */
1503a8599bd8SSage Weil 			if (ci->i_max_size == 0)
1504a8599bd8SSage Weil 				retain |= CEPH_CAP_ANY_RD;
1505a8599bd8SSage Weil 		}
1506a8599bd8SSage Weil 	}
1507a8599bd8SSage Weil 
1508a8599bd8SSage Weil 	dout("check_caps %p file_want %s used %s dirty %s flushing %s"
1509cbd03635SSage Weil 	     " issued %s revoking %s retain %s %s%s%s\n", inode,
1510a8599bd8SSage Weil 	     ceph_cap_string(file_wanted),
1511a8599bd8SSage Weil 	     ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1512a8599bd8SSage Weil 	     ceph_cap_string(ci->i_flushing_caps),
1513cbd03635SSage Weil 	     ceph_cap_string(issued), ceph_cap_string(revoking),
1514a8599bd8SSage Weil 	     ceph_cap_string(retain),
1515a8599bd8SSage Weil 	     (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1516a8599bd8SSage Weil 	     (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1517a8599bd8SSage Weil 	     (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1518a8599bd8SSage Weil 
1519a8599bd8SSage Weil 	/*
1520a8599bd8SSage Weil 	 * If we no longer need to hold onto old our caps, and we may
1521a8599bd8SSage Weil 	 * have cached pages, but don't want them, then try to invalidate.
1522a8599bd8SSage Weil 	 * If we fail, it's because pages are locked.... try again later.
1523a8599bd8SSage Weil 	 */
1524a8599bd8SSage Weil 	if ((!is_delayed || mdsc->stopping) &&
1525a8599bd8SSage Weil 	    ci->i_wrbuffer_ref == 0 &&               /* no dirty pages... */
152693afd449SSage Weil 	    inode->i_data.nrpages &&                 /* have cached pages */
1527cbd03635SSage Weil 	    (file_wanted == 0 ||                     /* no open files */
15282962507cSSage Weil 	     (revoking & (CEPH_CAP_FILE_CACHE|
15292962507cSSage Weil 			  CEPH_CAP_FILE_LAZYIO))) && /*  or revoking cache */
1530a8599bd8SSage Weil 	    !tried_invalidate) {
1531a8599bd8SSage Weil 		dout("check_caps trying to invalidate on %p\n", inode);
15325ecad6fdSSage Weil 		if (try_nonblocking_invalidate(inode) < 0) {
15332962507cSSage Weil 			if (revoking & (CEPH_CAP_FILE_CACHE|
15342962507cSSage Weil 					CEPH_CAP_FILE_LAZYIO)) {
1535cbd03635SSage Weil 				dout("check_caps queuing invalidate\n");
1536cbd03635SSage Weil 				queue_invalidate = 1;
1537cbd03635SSage Weil 				ci->i_rdcache_revoking = ci->i_rdcache_gen;
1538a8599bd8SSage Weil 			} else {
1539a8599bd8SSage Weil 				dout("check_caps failed to invalidate pages\n");
1540a8599bd8SSage Weil 				/* we failed to invalidate pages.  check these
1541a8599bd8SSage Weil 				   caps again later. */
1542a8599bd8SSage Weil 				force_requeue = 1;
1543a8599bd8SSage Weil 				__cap_set_timeouts(mdsc, ci);
1544a8599bd8SSage Weil 			}
15455ecad6fdSSage Weil 		}
1546a8599bd8SSage Weil 		tried_invalidate = 1;
1547a8599bd8SSage Weil 		goto retry_locked;
1548a8599bd8SSage Weil 	}
1549a8599bd8SSage Weil 
1550a8599bd8SSage Weil 	num = 0;
1551a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1552a8599bd8SSage Weil 		cap = rb_entry(p, struct ceph_cap, ci_node);
1553a8599bd8SSage Weil 		num++;
1554a8599bd8SSage Weil 
1555a8599bd8SSage Weil 		/* avoid looping forever */
1556a8599bd8SSage Weil 		if (mds >= cap->mds ||
1557a8599bd8SSage Weil 		    ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1558a8599bd8SSage Weil 			continue;
1559a8599bd8SSage Weil 
1560a8599bd8SSage Weil 		/* NOTE: no side-effects allowed, until we take s_mutex */
1561a8599bd8SSage Weil 
1562a8599bd8SSage Weil 		revoking = cap->implemented & ~cap->issued;
1563088b3f5eSSage Weil 		dout(" mds%d cap %p issued %s implemented %s revoking %s\n",
1564088b3f5eSSage Weil 		     cap->mds, cap, ceph_cap_string(cap->issued),
1565088b3f5eSSage Weil 		     ceph_cap_string(cap->implemented),
1566a8599bd8SSage Weil 		     ceph_cap_string(revoking));
1567a8599bd8SSage Weil 
1568a8599bd8SSage Weil 		if (cap == ci->i_auth_cap &&
1569a8599bd8SSage Weil 		    (cap->issued & CEPH_CAP_FILE_WR)) {
1570a8599bd8SSage Weil 			/* request larger max_size from MDS? */
1571a8599bd8SSage Weil 			if (ci->i_wanted_max_size > ci->i_max_size &&
1572a8599bd8SSage Weil 			    ci->i_wanted_max_size > ci->i_requested_max_size) {
1573a8599bd8SSage Weil 				dout("requesting new max_size\n");
1574a8599bd8SSage Weil 				goto ack;
1575a8599bd8SSage Weil 			}
1576a8599bd8SSage Weil 
1577a8599bd8SSage Weil 			/* approaching file_max? */
1578a8599bd8SSage Weil 			if ((inode->i_size << 1) >= ci->i_max_size &&
1579a8599bd8SSage Weil 			    (ci->i_reported_size << 1) < ci->i_max_size) {
1580a8599bd8SSage Weil 				dout("i_size approaching max_size\n");
1581a8599bd8SSage Weil 				goto ack;
1582a8599bd8SSage Weil 			}
1583a8599bd8SSage Weil 		}
1584a8599bd8SSage Weil 		/* flush anything dirty? */
1585a8599bd8SSage Weil 		if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) &&
1586a8599bd8SSage Weil 		    ci->i_dirty_caps) {
1587a8599bd8SSage Weil 			dout("flushing dirty caps\n");
1588a8599bd8SSage Weil 			goto ack;
1589a8599bd8SSage Weil 		}
1590a8599bd8SSage Weil 
1591a8599bd8SSage Weil 		/* completed revocation? going down and there are no caps? */
1592a8599bd8SSage Weil 		if (revoking && (revoking & used) == 0) {
1593a8599bd8SSage Weil 			dout("completed revocation of %s\n",
1594a8599bd8SSage Weil 			     ceph_cap_string(cap->implemented & ~cap->issued));
1595a8599bd8SSage Weil 			goto ack;
1596a8599bd8SSage Weil 		}
1597a8599bd8SSage Weil 
1598a8599bd8SSage Weil 		/* want more caps from mds? */
1599a8599bd8SSage Weil 		if (want & ~(cap->mds_wanted | cap->issued))
1600a8599bd8SSage Weil 			goto ack;
1601a8599bd8SSage Weil 
1602a8599bd8SSage Weil 		/* things we might delay */
1603a8599bd8SSage Weil 		if ((cap->issued & ~retain) == 0 &&
1604a8599bd8SSage Weil 		    cap->mds_wanted == want)
1605a8599bd8SSage Weil 			continue;     /* nope, all good */
1606a8599bd8SSage Weil 
1607a8599bd8SSage Weil 		if (is_delayed)
1608a8599bd8SSage Weil 			goto ack;
1609a8599bd8SSage Weil 
1610a8599bd8SSage Weil 		/* delay? */
1611a8599bd8SSage Weil 		if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1612a8599bd8SSage Weil 		    time_before(jiffies, ci->i_hold_caps_max)) {
1613a8599bd8SSage Weil 			dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1614a8599bd8SSage Weil 			     ceph_cap_string(cap->issued),
1615a8599bd8SSage Weil 			     ceph_cap_string(cap->issued & retain),
1616a8599bd8SSage Weil 			     ceph_cap_string(cap->mds_wanted),
1617a8599bd8SSage Weil 			     ceph_cap_string(want));
1618a8599bd8SSage Weil 			delayed++;
1619a8599bd8SSage Weil 			continue;
1620a8599bd8SSage Weil 		}
1621a8599bd8SSage Weil 
1622a8599bd8SSage Weil ack:
1623e9964c10SSage Weil 		if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1624e9964c10SSage Weil 			dout(" skipping %p I_NOFLUSH set\n", inode);
1625e9964c10SSage Weil 			continue;
1626e9964c10SSage Weil 		}
1627e9964c10SSage Weil 
1628a8599bd8SSage Weil 		if (session && session != cap->session) {
1629a8599bd8SSage Weil 			dout("oops, wrong session %p mutex\n", session);
1630a8599bd8SSage Weil 			mutex_unlock(&session->s_mutex);
1631a8599bd8SSage Weil 			session = NULL;
1632a8599bd8SSage Weil 		}
1633a8599bd8SSage Weil 		if (!session) {
1634a8599bd8SSage Weil 			session = cap->session;
1635a8599bd8SSage Weil 			if (mutex_trylock(&session->s_mutex) == 0) {
1636a8599bd8SSage Weil 				dout("inverting session/ino locks on %p\n",
1637a8599bd8SSage Weil 				     session);
1638be655596SSage Weil 				spin_unlock(&ci->i_ceph_lock);
1639a8599bd8SSage Weil 				if (took_snap_rwsem) {
1640a8599bd8SSage Weil 					up_read(&mdsc->snap_rwsem);
1641a8599bd8SSage Weil 					took_snap_rwsem = 0;
1642a8599bd8SSage Weil 				}
1643a8599bd8SSage Weil 				mutex_lock(&session->s_mutex);
1644a8599bd8SSage Weil 				goto retry;
1645a8599bd8SSage Weil 			}
1646a8599bd8SSage Weil 		}
1647a8599bd8SSage Weil 		/* take snap_rwsem after session mutex */
1648a8599bd8SSage Weil 		if (!took_snap_rwsem) {
1649a8599bd8SSage Weil 			if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1650a8599bd8SSage Weil 				dout("inverting snap/in locks on %p\n",
1651a8599bd8SSage Weil 				     inode);
1652be655596SSage Weil 				spin_unlock(&ci->i_ceph_lock);
1653a8599bd8SSage Weil 				down_read(&mdsc->snap_rwsem);
1654a8599bd8SSage Weil 				took_snap_rwsem = 1;
1655a8599bd8SSage Weil 				goto retry;
1656a8599bd8SSage Weil 			}
1657a8599bd8SSage Weil 			took_snap_rwsem = 1;
1658a8599bd8SSage Weil 		}
1659a8599bd8SSage Weil 
1660cdc35f96SSage Weil 		if (cap == ci->i_auth_cap && ci->i_dirty_caps)
1661cdc35f96SSage Weil 			flushing = __mark_caps_flushing(inode, session);
166224be0c48SSage Weil 		else
166324be0c48SSage Weil 			flushing = 0;
1664a8599bd8SSage Weil 
1665a8599bd8SSage Weil 		mds = cap->mds;  /* remember mds, so we don't repeat */
1666a8599bd8SSage Weil 		sent++;
1667a8599bd8SSage Weil 
1668be655596SSage Weil 		/* __send_cap drops i_ceph_lock */
1669a8599bd8SSage Weil 		delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want,
1670a8599bd8SSage Weil 				      retain, flushing, NULL);
1671be655596SSage Weil 		goto retry; /* retake i_ceph_lock and restart our cap scan. */
1672a8599bd8SSage Weil 	}
1673a8599bd8SSage Weil 
1674a8599bd8SSage Weil 	/*
1675a8599bd8SSage Weil 	 * Reschedule delayed caps release if we delayed anything,
1676a8599bd8SSage Weil 	 * otherwise cancel.
1677a8599bd8SSage Weil 	 */
1678a8599bd8SSage Weil 	if (delayed && is_delayed)
1679a8599bd8SSage Weil 		force_requeue = 1;   /* __send_cap delayed release; requeue */
1680a8599bd8SSage Weil 	if (!delayed && !is_delayed)
1681a8599bd8SSage Weil 		__cap_delay_cancel(mdsc, ci);
1682a8599bd8SSage Weil 	else if (!is_delayed || force_requeue)
1683a8599bd8SSage Weil 		__cap_delay_requeue(mdsc, ci);
1684a8599bd8SSage Weil 
1685be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1686a8599bd8SSage Weil 
1687cbd03635SSage Weil 	if (queue_invalidate)
16883c6f6b79SSage Weil 		ceph_queue_invalidate(inode);
1689cbd03635SSage Weil 
1690cdc2ce05SSage Weil 	if (session)
1691a8599bd8SSage Weil 		mutex_unlock(&session->s_mutex);
1692a8599bd8SSage Weil 	if (took_snap_rwsem)
1693a8599bd8SSage Weil 		up_read(&mdsc->snap_rwsem);
1694a8599bd8SSage Weil }
1695a8599bd8SSage Weil 
1696a8599bd8SSage Weil /*
1697a8599bd8SSage Weil  * Try to flush dirty caps back to the auth mds.
1698a8599bd8SSage Weil  */
1699a8599bd8SSage Weil static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session,
1700a8599bd8SSage Weil 			  unsigned *flush_tid)
1701a8599bd8SSage Weil {
17023d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
1703a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1704a8599bd8SSage Weil 	int unlock_session = session ? 0 : 1;
1705a8599bd8SSage Weil 	int flushing = 0;
1706a8599bd8SSage Weil 
1707a8599bd8SSage Weil retry:
1708be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1709e9964c10SSage Weil 	if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1710e9964c10SSage Weil 		dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
1711e9964c10SSage Weil 		goto out;
1712e9964c10SSage Weil 	}
1713a8599bd8SSage Weil 	if (ci->i_dirty_caps && ci->i_auth_cap) {
1714a8599bd8SSage Weil 		struct ceph_cap *cap = ci->i_auth_cap;
1715a8599bd8SSage Weil 		int used = __ceph_caps_used(ci);
1716a8599bd8SSage Weil 		int want = __ceph_caps_wanted(ci);
1717a8599bd8SSage Weil 		int delayed;
1718a8599bd8SSage Weil 
1719a8599bd8SSage Weil 		if (!session) {
1720be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
1721a8599bd8SSage Weil 			session = cap->session;
1722a8599bd8SSage Weil 			mutex_lock(&session->s_mutex);
1723a8599bd8SSage Weil 			goto retry;
1724a8599bd8SSage Weil 		}
1725a8599bd8SSage Weil 		BUG_ON(session != cap->session);
1726a8599bd8SSage Weil 		if (cap->session->s_state < CEPH_MDS_SESSION_OPEN)
1727a8599bd8SSage Weil 			goto out;
1728a8599bd8SSage Weil 
1729cdc35f96SSage Weil 		flushing = __mark_caps_flushing(inode, session);
1730a8599bd8SSage Weil 
1731be655596SSage Weil 		/* __send_cap drops i_ceph_lock */
1732a8599bd8SSage Weil 		delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want,
1733a8599bd8SSage Weil 				     cap->issued | cap->implemented, flushing,
1734a8599bd8SSage Weil 				     flush_tid);
1735a8599bd8SSage Weil 		if (!delayed)
1736a8599bd8SSage Weil 			goto out_unlocked;
1737a8599bd8SSage Weil 
1738be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1739a8599bd8SSage Weil 		__cap_delay_requeue(mdsc, ci);
1740a8599bd8SSage Weil 	}
1741a8599bd8SSage Weil out:
1742be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1743a8599bd8SSage Weil out_unlocked:
1744a8599bd8SSage Weil 	if (session && unlock_session)
1745a8599bd8SSage Weil 		mutex_unlock(&session->s_mutex);
1746a8599bd8SSage Weil 	return flushing;
1747a8599bd8SSage Weil }
1748a8599bd8SSage Weil 
1749a8599bd8SSage Weil /*
1750a8599bd8SSage Weil  * Return true if we've flushed caps through the given flush_tid.
1751a8599bd8SSage Weil  */
1752a8599bd8SSage Weil static int caps_are_flushed(struct inode *inode, unsigned tid)
1753a8599bd8SSage Weil {
1754a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1755a5ee751cSDan Carpenter 	int i, ret = 1;
1756a8599bd8SSage Weil 
1757be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1758a8599bd8SSage Weil 	for (i = 0; i < CEPH_CAP_BITS; i++)
1759a8599bd8SSage Weil 		if ((ci->i_flushing_caps & (1 << i)) &&
1760a8599bd8SSage Weil 		    ci->i_cap_flush_tid[i] <= tid) {
1761a8599bd8SSage Weil 			/* still flushing this bit */
1762a8599bd8SSage Weil 			ret = 0;
1763a8599bd8SSage Weil 			break;
1764a8599bd8SSage Weil 		}
1765be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
1766a8599bd8SSage Weil 	return ret;
1767a8599bd8SSage Weil }
1768a8599bd8SSage Weil 
1769a8599bd8SSage Weil /*
1770a8599bd8SSage Weil  * Wait on any unsafe replies for the given inode.  First wait on the
1771a8599bd8SSage Weil  * newest request, and make that the upper bound.  Then, if there are
1772a8599bd8SSage Weil  * more requests, keep waiting on the oldest as long as it is still older
1773a8599bd8SSage Weil  * than the original request.
1774a8599bd8SSage Weil  */
1775a8599bd8SSage Weil static void sync_write_wait(struct inode *inode)
1776a8599bd8SSage Weil {
1777a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1778a8599bd8SSage Weil 	struct list_head *head = &ci->i_unsafe_writes;
1779a8599bd8SSage Weil 	struct ceph_osd_request *req;
1780a8599bd8SSage Weil 	u64 last_tid;
1781a8599bd8SSage Weil 
1782a8599bd8SSage Weil 	spin_lock(&ci->i_unsafe_lock);
1783a8599bd8SSage Weil 	if (list_empty(head))
1784a8599bd8SSage Weil 		goto out;
1785a8599bd8SSage Weil 
1786a8599bd8SSage Weil 	/* set upper bound as _last_ entry in chain */
1787a8599bd8SSage Weil 	req = list_entry(head->prev, struct ceph_osd_request,
1788a8599bd8SSage Weil 			 r_unsafe_item);
1789a8599bd8SSage Weil 	last_tid = req->r_tid;
1790a8599bd8SSage Weil 
1791a8599bd8SSage Weil 	do {
1792a8599bd8SSage Weil 		ceph_osdc_get_request(req);
1793a8599bd8SSage Weil 		spin_unlock(&ci->i_unsafe_lock);
1794a8599bd8SSage Weil 		dout("sync_write_wait on tid %llu (until %llu)\n",
1795a8599bd8SSage Weil 		     req->r_tid, last_tid);
1796a8599bd8SSage Weil 		wait_for_completion(&req->r_safe_completion);
1797a8599bd8SSage Weil 		spin_lock(&ci->i_unsafe_lock);
1798a8599bd8SSage Weil 		ceph_osdc_put_request(req);
1799a8599bd8SSage Weil 
1800a8599bd8SSage Weil 		/*
1801a8599bd8SSage Weil 		 * from here on look at first entry in chain, since we
1802a8599bd8SSage Weil 		 * only want to wait for anything older than last_tid
1803a8599bd8SSage Weil 		 */
1804a8599bd8SSage Weil 		if (list_empty(head))
1805a8599bd8SSage Weil 			break;
1806a8599bd8SSage Weil 		req = list_entry(head->next, struct ceph_osd_request,
1807a8599bd8SSage Weil 				 r_unsafe_item);
1808a8599bd8SSage Weil 	} while (req->r_tid < last_tid);
1809a8599bd8SSage Weil out:
1810a8599bd8SSage Weil 	spin_unlock(&ci->i_unsafe_lock);
1811a8599bd8SSage Weil }
1812a8599bd8SSage Weil 
181302c24a82SJosef Bacik int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1814a8599bd8SSage Weil {
18157ea80859SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
1816a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1817a8599bd8SSage Weil 	unsigned flush_tid;
1818a8599bd8SSage Weil 	int ret;
1819a8599bd8SSage Weil 	int dirty;
1820a8599bd8SSage Weil 
1821a8599bd8SSage Weil 	dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
1822a8599bd8SSage Weil 	sync_write_wait(inode);
1823a8599bd8SSage Weil 
182402c24a82SJosef Bacik 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
1825a8599bd8SSage Weil 	if (ret < 0)
1826a8599bd8SSage Weil 		return ret;
182702c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
1828a8599bd8SSage Weil 
1829a8599bd8SSage Weil 	dirty = try_flush_caps(inode, NULL, &flush_tid);
1830a8599bd8SSage Weil 	dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
1831a8599bd8SSage Weil 
1832a8599bd8SSage Weil 	/*
1833a8599bd8SSage Weil 	 * only wait on non-file metadata writeback (the mds
1834a8599bd8SSage Weil 	 * can recover size and mtime, so we don't need to
1835a8599bd8SSage Weil 	 * wait for that)
1836a8599bd8SSage Weil 	 */
1837a8599bd8SSage Weil 	if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
1838a8599bd8SSage Weil 		dout("fsync waiting for flush_tid %u\n", flush_tid);
1839a8599bd8SSage Weil 		ret = wait_event_interruptible(ci->i_cap_wq,
1840a8599bd8SSage Weil 				       caps_are_flushed(inode, flush_tid));
1841a8599bd8SSage Weil 	}
1842a8599bd8SSage Weil 
1843a8599bd8SSage Weil 	dout("fsync %p%s done\n", inode, datasync ? " datasync" : "");
184402c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
1845a8599bd8SSage Weil 	return ret;
1846a8599bd8SSage Weil }
1847a8599bd8SSage Weil 
1848a8599bd8SSage Weil /*
1849a8599bd8SSage Weil  * Flush any dirty caps back to the mds.  If we aren't asked to wait,
1850a8599bd8SSage Weil  * queue inode for flush but don't do so immediately, because we can
1851a8599bd8SSage Weil  * get by with fewer MDS messages if we wait for data writeback to
1852a8599bd8SSage Weil  * complete first.
1853a8599bd8SSage Weil  */
1854f1a3d572SStephen Rothwell int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
1855a8599bd8SSage Weil {
1856a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1857a8599bd8SSage Weil 	unsigned flush_tid;
1858a8599bd8SSage Weil 	int err = 0;
1859a8599bd8SSage Weil 	int dirty;
1860f1a3d572SStephen Rothwell 	int wait = wbc->sync_mode == WB_SYNC_ALL;
1861a8599bd8SSage Weil 
1862a8599bd8SSage Weil 	dout("write_inode %p wait=%d\n", inode, wait);
1863a8599bd8SSage Weil 	if (wait) {
1864a8599bd8SSage Weil 		dirty = try_flush_caps(inode, NULL, &flush_tid);
1865a8599bd8SSage Weil 		if (dirty)
1866a8599bd8SSage Weil 			err = wait_event_interruptible(ci->i_cap_wq,
1867a8599bd8SSage Weil 				       caps_are_flushed(inode, flush_tid));
1868a8599bd8SSage Weil 	} else {
1869640ef79dSCheng Renquan 		struct ceph_mds_client *mdsc =
18703d14c5d2SYehuda Sadeh 			ceph_sb_to_client(inode->i_sb)->mdsc;
1871a8599bd8SSage Weil 
1872be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1873a8599bd8SSage Weil 		if (__ceph_caps_dirty(ci))
1874a8599bd8SSage Weil 			__cap_delay_requeue_front(mdsc, ci);
1875be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1876a8599bd8SSage Weil 	}
1877a8599bd8SSage Weil 	return err;
1878a8599bd8SSage Weil }
1879a8599bd8SSage Weil 
1880a8599bd8SSage Weil /*
1881a8599bd8SSage Weil  * After a recovering MDS goes active, we need to resend any caps
1882a8599bd8SSage Weil  * we were flushing.
1883a8599bd8SSage Weil  *
1884a8599bd8SSage Weil  * Caller holds session->s_mutex.
1885a8599bd8SSage Weil  */
1886a8599bd8SSage Weil static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc,
1887a8599bd8SSage Weil 				   struct ceph_mds_session *session)
1888a8599bd8SSage Weil {
1889a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
1890a8599bd8SSage Weil 
1891a8599bd8SSage Weil 	dout("kick_flushing_capsnaps mds%d\n", session->s_mds);
1892a8599bd8SSage Weil 	list_for_each_entry(capsnap, &session->s_cap_snaps_flushing,
1893a8599bd8SSage Weil 			    flushing_item) {
1894a8599bd8SSage Weil 		struct ceph_inode_info *ci = capsnap->ci;
1895a8599bd8SSage Weil 		struct inode *inode = &ci->vfs_inode;
1896a8599bd8SSage Weil 		struct ceph_cap *cap;
1897a8599bd8SSage Weil 
1898be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1899a8599bd8SSage Weil 		cap = ci->i_auth_cap;
1900a8599bd8SSage Weil 		if (cap && cap->session == session) {
1901a8599bd8SSage Weil 			dout("kick_flushing_caps %p cap %p capsnap %p\n", inode,
1902a8599bd8SSage Weil 			     cap, capsnap);
1903e835124cSSage Weil 			__ceph_flush_snaps(ci, &session, 1);
1904a8599bd8SSage Weil 		} else {
1905a8599bd8SSage Weil 			pr_err("%p auth cap %p not mds%d ???\n", inode,
1906a8599bd8SSage Weil 			       cap, session->s_mds);
1907a8599bd8SSage Weil 		}
1908be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1909a8599bd8SSage Weil 	}
1910a8599bd8SSage Weil }
1911a8599bd8SSage Weil 
1912a8599bd8SSage Weil void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
1913a8599bd8SSage Weil 			     struct ceph_mds_session *session)
1914a8599bd8SSage Weil {
1915a8599bd8SSage Weil 	struct ceph_inode_info *ci;
1916a8599bd8SSage Weil 
1917a8599bd8SSage Weil 	kick_flushing_capsnaps(mdsc, session);
1918a8599bd8SSage Weil 
1919a8599bd8SSage Weil 	dout("kick_flushing_caps mds%d\n", session->s_mds);
1920a8599bd8SSage Weil 	list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
1921a8599bd8SSage Weil 		struct inode *inode = &ci->vfs_inode;
1922a8599bd8SSage Weil 		struct ceph_cap *cap;
1923a8599bd8SSage Weil 		int delayed = 0;
1924a8599bd8SSage Weil 
1925be655596SSage Weil 		spin_lock(&ci->i_ceph_lock);
1926a8599bd8SSage Weil 		cap = ci->i_auth_cap;
1927a8599bd8SSage Weil 		if (cap && cap->session == session) {
1928a8599bd8SSage Weil 			dout("kick_flushing_caps %p cap %p %s\n", inode,
1929a8599bd8SSage Weil 			     cap, ceph_cap_string(ci->i_flushing_caps));
1930a8599bd8SSage Weil 			delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1931a8599bd8SSage Weil 					     __ceph_caps_used(ci),
1932a8599bd8SSage Weil 					     __ceph_caps_wanted(ci),
1933a8599bd8SSage Weil 					     cap->issued | cap->implemented,
1934a8599bd8SSage Weil 					     ci->i_flushing_caps, NULL);
1935a8599bd8SSage Weil 			if (delayed) {
1936be655596SSage Weil 				spin_lock(&ci->i_ceph_lock);
1937a8599bd8SSage Weil 				__cap_delay_requeue(mdsc, ci);
1938be655596SSage Weil 				spin_unlock(&ci->i_ceph_lock);
1939a8599bd8SSage Weil 			}
1940a8599bd8SSage Weil 		} else {
1941a8599bd8SSage Weil 			pr_err("%p auth cap %p not mds%d ???\n", inode,
1942a8599bd8SSage Weil 			       cap, session->s_mds);
1943be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
1944a8599bd8SSage Weil 		}
1945a8599bd8SSage Weil 	}
1946a8599bd8SSage Weil }
1947a8599bd8SSage Weil 
1948088b3f5eSSage Weil static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
1949088b3f5eSSage Weil 				     struct ceph_mds_session *session,
1950088b3f5eSSage Weil 				     struct inode *inode)
1951088b3f5eSSage Weil {
1952088b3f5eSSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
1953088b3f5eSSage Weil 	struct ceph_cap *cap;
1954088b3f5eSSage Weil 	int delayed = 0;
1955088b3f5eSSage Weil 
1956be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
1957088b3f5eSSage Weil 	cap = ci->i_auth_cap;
1958088b3f5eSSage Weil 	dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode,
1959088b3f5eSSage Weil 	     ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq);
1960088b3f5eSSage Weil 	__ceph_flush_snaps(ci, &session, 1);
1961088b3f5eSSage Weil 	if (ci->i_flushing_caps) {
1962088b3f5eSSage Weil 		delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
1963088b3f5eSSage Weil 				     __ceph_caps_used(ci),
1964088b3f5eSSage Weil 				     __ceph_caps_wanted(ci),
1965088b3f5eSSage Weil 				     cap->issued | cap->implemented,
1966088b3f5eSSage Weil 				     ci->i_flushing_caps, NULL);
1967088b3f5eSSage Weil 		if (delayed) {
1968be655596SSage Weil 			spin_lock(&ci->i_ceph_lock);
1969088b3f5eSSage Weil 			__cap_delay_requeue(mdsc, ci);
1970be655596SSage Weil 			spin_unlock(&ci->i_ceph_lock);
1971088b3f5eSSage Weil 		}
1972088b3f5eSSage Weil 	} else {
1973be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
1974088b3f5eSSage Weil 	}
1975088b3f5eSSage Weil }
1976088b3f5eSSage Weil 
1977a8599bd8SSage Weil 
1978a8599bd8SSage Weil /*
1979a8599bd8SSage Weil  * Take references to capabilities we hold, so that we don't release
1980a8599bd8SSage Weil  * them to the MDS prematurely.
1981a8599bd8SSage Weil  *
1982be655596SSage Weil  * Protected by i_ceph_lock.
1983a8599bd8SSage Weil  */
1984a8599bd8SSage Weil static void __take_cap_refs(struct ceph_inode_info *ci, int got)
1985a8599bd8SSage Weil {
1986a8599bd8SSage Weil 	if (got & CEPH_CAP_PIN)
1987a8599bd8SSage Weil 		ci->i_pin_ref++;
1988a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_RD)
1989a8599bd8SSage Weil 		ci->i_rd_ref++;
1990a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_CACHE)
1991a8599bd8SSage Weil 		ci->i_rdcache_ref++;
1992a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_WR)
1993a8599bd8SSage Weil 		ci->i_wr_ref++;
1994a8599bd8SSage Weil 	if (got & CEPH_CAP_FILE_BUFFER) {
1995d3d0720dSHenry C Chang 		if (ci->i_wb_ref == 0)
19963772d26dSSage Weil 			ihold(&ci->vfs_inode);
1997d3d0720dSHenry C Chang 		ci->i_wb_ref++;
1998d3d0720dSHenry C Chang 		dout("__take_cap_refs %p wb %d -> %d (?)\n",
1999d3d0720dSHenry C Chang 		     &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
2000a8599bd8SSage Weil 	}
2001a8599bd8SSage Weil }
2002a8599bd8SSage Weil 
2003a8599bd8SSage Weil /*
2004a8599bd8SSage Weil  * Try to grab cap references.  Specify those refs we @want, and the
2005a8599bd8SSage Weil  * minimal set we @need.  Also include the larger offset we are writing
2006a8599bd8SSage Weil  * to (when applicable), and check against max_size here as well.
2007a8599bd8SSage Weil  * Note that caller is responsible for ensuring max_size increases are
2008a8599bd8SSage Weil  * requested from the MDS.
2009a8599bd8SSage Weil  */
2010a8599bd8SSage Weil static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
2011a8599bd8SSage Weil 			    int *got, loff_t endoff, int *check_max, int *err)
2012a8599bd8SSage Weil {
2013a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
2014a8599bd8SSage Weil 	int ret = 0;
2015a8599bd8SSage Weil 	int have, implemented;
2016195d3ce2SSage Weil 	int file_wanted;
2017a8599bd8SSage Weil 
2018a8599bd8SSage Weil 	dout("get_cap_refs %p need %s want %s\n", inode,
2019a8599bd8SSage Weil 	     ceph_cap_string(need), ceph_cap_string(want));
2020be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2021a8599bd8SSage Weil 
2022195d3ce2SSage Weil 	/* make sure file is actually open */
2023195d3ce2SSage Weil 	file_wanted = __ceph_caps_file_wanted(ci);
2024195d3ce2SSage Weil 	if ((file_wanted & need) == 0) {
2025195d3ce2SSage Weil 		dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2026195d3ce2SSage Weil 		     ceph_cap_string(need), ceph_cap_string(file_wanted));
2027a8599bd8SSage Weil 		*err = -EBADF;
2028a8599bd8SSage Weil 		ret = 1;
2029a8599bd8SSage Weil 		goto out;
2030a8599bd8SSage Weil 	}
2031a8599bd8SSage Weil 
2032a8599bd8SSage Weil 	if (need & CEPH_CAP_FILE_WR) {
2033a8599bd8SSage Weil 		if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2034a8599bd8SSage Weil 			dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2035a8599bd8SSage Weil 			     inode, endoff, ci->i_max_size);
2036a8599bd8SSage Weil 			if (endoff > ci->i_wanted_max_size) {
2037a8599bd8SSage Weil 				*check_max = 1;
2038a8599bd8SSage Weil 				ret = 1;
2039a8599bd8SSage Weil 			}
2040a8599bd8SSage Weil 			goto out;
2041a8599bd8SSage Weil 		}
2042a8599bd8SSage Weil 		/*
2043a8599bd8SSage Weil 		 * If a sync write is in progress, we must wait, so that we
2044a8599bd8SSage Weil 		 * can get a final snapshot value for size+mtime.
2045a8599bd8SSage Weil 		 */
2046a8599bd8SSage Weil 		if (__ceph_have_pending_cap_snap(ci)) {
2047a8599bd8SSage Weil 			dout("get_cap_refs %p cap_snap_pending\n", inode);
2048a8599bd8SSage Weil 			goto out;
2049a8599bd8SSage Weil 		}
2050a8599bd8SSage Weil 	}
2051a8599bd8SSage Weil 	have = __ceph_caps_issued(ci, &implemented);
2052a8599bd8SSage Weil 
2053a8599bd8SSage Weil 	/*
2054a8599bd8SSage Weil 	 * disallow writes while a truncate is pending
2055a8599bd8SSage Weil 	 */
2056a8599bd8SSage Weil 	if (ci->i_truncate_pending)
2057a8599bd8SSage Weil 		have &= ~CEPH_CAP_FILE_WR;
2058a8599bd8SSage Weil 
2059a8599bd8SSage Weil 	if ((have & need) == need) {
2060a8599bd8SSage Weil 		/*
2061a8599bd8SSage Weil 		 * Look at (implemented & ~have & not) so that we keep waiting
2062a8599bd8SSage Weil 		 * on transition from wanted -> needed caps.  This is needed
2063a8599bd8SSage Weil 		 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2064a8599bd8SSage Weil 		 * going before a prior buffered writeback happens.
2065a8599bd8SSage Weil 		 */
2066a8599bd8SSage Weil 		int not = want & ~(have & need);
2067a8599bd8SSage Weil 		int revoking = implemented & ~have;
2068a8599bd8SSage Weil 		dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2069a8599bd8SSage Weil 		     inode, ceph_cap_string(have), ceph_cap_string(not),
2070a8599bd8SSage Weil 		     ceph_cap_string(revoking));
2071a8599bd8SSage Weil 		if ((revoking & not) == 0) {
2072a8599bd8SSage Weil 			*got = need | (have & want);
2073a8599bd8SSage Weil 			__take_cap_refs(ci, *got);
2074a8599bd8SSage Weil 			ret = 1;
2075a8599bd8SSage Weil 		}
2076a8599bd8SSage Weil 	} else {
2077a8599bd8SSage Weil 		dout("get_cap_refs %p have %s needed %s\n", inode,
2078a8599bd8SSage Weil 		     ceph_cap_string(have), ceph_cap_string(need));
2079a8599bd8SSage Weil 	}
2080a8599bd8SSage Weil out:
2081be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2082a8599bd8SSage Weil 	dout("get_cap_refs %p ret %d got %s\n", inode,
2083a8599bd8SSage Weil 	     ret, ceph_cap_string(*got));
2084a8599bd8SSage Weil 	return ret;
2085a8599bd8SSage Weil }
2086a8599bd8SSage Weil 
2087a8599bd8SSage Weil /*
2088a8599bd8SSage Weil  * Check the offset we are writing up to against our current
2089a8599bd8SSage Weil  * max_size.  If necessary, tell the MDS we want to write to
2090a8599bd8SSage Weil  * a larger offset.
2091a8599bd8SSage Weil  */
2092a8599bd8SSage Weil static void check_max_size(struct inode *inode, loff_t endoff)
2093a8599bd8SSage Weil {
2094a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2095a8599bd8SSage Weil 	int check = 0;
2096a8599bd8SSage Weil 
2097a8599bd8SSage Weil 	/* do we need to explicitly request a larger max_size? */
2098be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2099a8599bd8SSage Weil 	if ((endoff >= ci->i_max_size ||
2100a8599bd8SSage Weil 	     endoff > (inode->i_size << 1)) &&
2101a8599bd8SSage Weil 	    endoff > ci->i_wanted_max_size) {
2102a8599bd8SSage Weil 		dout("write %p at large endoff %llu, req max_size\n",
2103a8599bd8SSage Weil 		     inode, endoff);
2104a8599bd8SSage Weil 		ci->i_wanted_max_size = endoff;
2105a8599bd8SSage Weil 		check = 1;
2106a8599bd8SSage Weil 	}
2107be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2108a8599bd8SSage Weil 	if (check)
2109a8599bd8SSage Weil 		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2110a8599bd8SSage Weil }
2111a8599bd8SSage Weil 
2112a8599bd8SSage Weil /*
2113a8599bd8SSage Weil  * Wait for caps, and take cap references.  If we can't get a WR cap
2114a8599bd8SSage Weil  * due to a small max_size, make sure we check_max_size (and possibly
2115a8599bd8SSage Weil  * ask the mds) so we don't get hung up indefinitely.
2116a8599bd8SSage Weil  */
2117a8599bd8SSage Weil int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got,
2118a8599bd8SSage Weil 		  loff_t endoff)
2119a8599bd8SSage Weil {
2120a8599bd8SSage Weil 	int check_max, ret, err;
2121a8599bd8SSage Weil 
2122a8599bd8SSage Weil retry:
2123a8599bd8SSage Weil 	if (endoff > 0)
2124a8599bd8SSage Weil 		check_max_size(&ci->vfs_inode, endoff);
2125a8599bd8SSage Weil 	check_max = 0;
2126a8599bd8SSage Weil 	err = 0;
2127a8599bd8SSage Weil 	ret = wait_event_interruptible(ci->i_cap_wq,
2128a8599bd8SSage Weil 				       try_get_cap_refs(ci, need, want,
2129a8599bd8SSage Weil 							got, endoff,
2130a8599bd8SSage Weil 							&check_max, &err));
2131a8599bd8SSage Weil 	if (err)
2132a8599bd8SSage Weil 		ret = err;
2133a8599bd8SSage Weil 	if (check_max)
2134a8599bd8SSage Weil 		goto retry;
2135a8599bd8SSage Weil 	return ret;
2136a8599bd8SSage Weil }
2137a8599bd8SSage Weil 
2138a8599bd8SSage Weil /*
2139a8599bd8SSage Weil  * Take cap refs.  Caller must already know we hold at least one ref
2140a8599bd8SSage Weil  * on the caps in question or we don't know this is safe.
2141a8599bd8SSage Weil  */
2142a8599bd8SSage Weil void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2143a8599bd8SSage Weil {
2144be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2145a8599bd8SSage Weil 	__take_cap_refs(ci, caps);
2146be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2147a8599bd8SSage Weil }
2148a8599bd8SSage Weil 
2149a8599bd8SSage Weil /*
2150a8599bd8SSage Weil  * Release cap refs.
2151a8599bd8SSage Weil  *
2152a8599bd8SSage Weil  * If we released the last ref on any given cap, call ceph_check_caps
2153a8599bd8SSage Weil  * to release (or schedule a release).
2154a8599bd8SSage Weil  *
2155a8599bd8SSage Weil  * If we are releasing a WR cap (from a sync write), finalize any affected
2156a8599bd8SSage Weil  * cap_snap, and wake up any waiters.
2157a8599bd8SSage Weil  */
2158a8599bd8SSage Weil void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2159a8599bd8SSage Weil {
2160a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
2161a8599bd8SSage Weil 	int last = 0, put = 0, flushsnaps = 0, wake = 0;
2162a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
2163a8599bd8SSage Weil 
2164be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2165a8599bd8SSage Weil 	if (had & CEPH_CAP_PIN)
2166a8599bd8SSage Weil 		--ci->i_pin_ref;
2167a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_RD)
2168a8599bd8SSage Weil 		if (--ci->i_rd_ref == 0)
2169a8599bd8SSage Weil 			last++;
2170a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_CACHE)
2171a8599bd8SSage Weil 		if (--ci->i_rdcache_ref == 0)
2172a8599bd8SSage Weil 			last++;
2173a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_BUFFER) {
2174d3d0720dSHenry C Chang 		if (--ci->i_wb_ref == 0) {
2175a8599bd8SSage Weil 			last++;
2176a8599bd8SSage Weil 			put++;
2177a8599bd8SSage Weil 		}
2178d3d0720dSHenry C Chang 		dout("put_cap_refs %p wb %d -> %d (?)\n",
2179d3d0720dSHenry C Chang 		     inode, ci->i_wb_ref+1, ci->i_wb_ref);
2180a8599bd8SSage Weil 	}
2181a8599bd8SSage Weil 	if (had & CEPH_CAP_FILE_WR)
2182a8599bd8SSage Weil 		if (--ci->i_wr_ref == 0) {
2183a8599bd8SSage Weil 			last++;
2184a8599bd8SSage Weil 			if (!list_empty(&ci->i_cap_snaps)) {
2185a8599bd8SSage Weil 				capsnap = list_first_entry(&ci->i_cap_snaps,
2186a8599bd8SSage Weil 						     struct ceph_cap_snap,
2187a8599bd8SSage Weil 						     ci_item);
2188a8599bd8SSage Weil 				if (capsnap->writing) {
2189a8599bd8SSage Weil 					capsnap->writing = 0;
2190a8599bd8SSage Weil 					flushsnaps =
2191a8599bd8SSage Weil 						__ceph_finish_cap_snap(ci,
2192a8599bd8SSage Weil 								       capsnap);
2193a8599bd8SSage Weil 					wake = 1;
2194a8599bd8SSage Weil 				}
2195a8599bd8SSage Weil 			}
2196a8599bd8SSage Weil 		}
2197be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2198a8599bd8SSage Weil 
2199819ccbfaSSage Weil 	dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2200819ccbfaSSage Weil 	     last ? " last" : "", put ? " put" : "");
2201a8599bd8SSage Weil 
2202a8599bd8SSage Weil 	if (last && !flushsnaps)
2203a8599bd8SSage Weil 		ceph_check_caps(ci, 0, NULL);
2204a8599bd8SSage Weil 	else if (flushsnaps)
2205a8599bd8SSage Weil 		ceph_flush_snaps(ci);
2206a8599bd8SSage Weil 	if (wake)
220703066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
2208a8599bd8SSage Weil 	if (put)
2209a8599bd8SSage Weil 		iput(inode);
2210a8599bd8SSage Weil }
2211a8599bd8SSage Weil 
2212a8599bd8SSage Weil /*
2213a8599bd8SSage Weil  * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2214a8599bd8SSage Weil  * context.  Adjust per-snap dirty page accounting as appropriate.
2215a8599bd8SSage Weil  * Once all dirty data for a cap_snap is flushed, flush snapped file
2216a8599bd8SSage Weil  * metadata back to the MDS.  If we dropped the last ref, call
2217a8599bd8SSage Weil  * ceph_check_caps.
2218a8599bd8SSage Weil  */
2219a8599bd8SSage Weil void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2220a8599bd8SSage Weil 				struct ceph_snap_context *snapc)
2221a8599bd8SSage Weil {
2222a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
2223a8599bd8SSage Weil 	int last = 0;
2224819ccbfaSSage Weil 	int complete_capsnap = 0;
2225819ccbfaSSage Weil 	int drop_capsnap = 0;
2226a8599bd8SSage Weil 	int found = 0;
2227a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap = NULL;
2228a8599bd8SSage Weil 
2229be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2230a8599bd8SSage Weil 	ci->i_wrbuffer_ref -= nr;
2231a8599bd8SSage Weil 	last = !ci->i_wrbuffer_ref;
2232a8599bd8SSage Weil 
2233a8599bd8SSage Weil 	if (ci->i_head_snapc == snapc) {
2234a8599bd8SSage Weil 		ci->i_wrbuffer_ref_head -= nr;
22357d8cb26dSSage Weil 		if (ci->i_wrbuffer_ref_head == 0 &&
22367d8cb26dSSage Weil 		    ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) {
22377d8cb26dSSage Weil 			BUG_ON(!ci->i_head_snapc);
2238a8599bd8SSage Weil 			ceph_put_snap_context(ci->i_head_snapc);
2239a8599bd8SSage Weil 			ci->i_head_snapc = NULL;
2240a8599bd8SSage Weil 		}
2241a8599bd8SSage Weil 		dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2242a8599bd8SSage Weil 		     inode,
2243a8599bd8SSage Weil 		     ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2244a8599bd8SSage Weil 		     ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2245a8599bd8SSage Weil 		     last ? " LAST" : "");
2246a8599bd8SSage Weil 	} else {
2247a8599bd8SSage Weil 		list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2248a8599bd8SSage Weil 			if (capsnap->context == snapc) {
2249a8599bd8SSage Weil 				found = 1;
2250a8599bd8SSage Weil 				break;
2251a8599bd8SSage Weil 			}
2252a8599bd8SSage Weil 		}
2253a8599bd8SSage Weil 		BUG_ON(!found);
2254819ccbfaSSage Weil 		capsnap->dirty_pages -= nr;
2255819ccbfaSSage Weil 		if (capsnap->dirty_pages == 0) {
2256819ccbfaSSage Weil 			complete_capsnap = 1;
2257819ccbfaSSage Weil 			if (capsnap->dirty == 0)
2258819ccbfaSSage Weil 				/* cap writeback completed before we created
2259819ccbfaSSage Weil 				 * the cap_snap; no FLUSHSNAP is needed */
2260819ccbfaSSage Weil 				drop_capsnap = 1;
2261819ccbfaSSage Weil 		}
2262a8599bd8SSage Weil 		dout("put_wrbuffer_cap_refs on %p cap_snap %p "
2263819ccbfaSSage Weil 		     " snap %lld %d/%d -> %d/%d %s%s%s\n",
2264a8599bd8SSage Weil 		     inode, capsnap, capsnap->context->seq,
2265a8599bd8SSage Weil 		     ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2266a8599bd8SSage Weil 		     ci->i_wrbuffer_ref, capsnap->dirty_pages,
2267a8599bd8SSage Weil 		     last ? " (wrbuffer last)" : "",
2268819ccbfaSSage Weil 		     complete_capsnap ? " (complete capsnap)" : "",
2269819ccbfaSSage Weil 		     drop_capsnap ? " (drop capsnap)" : "");
2270819ccbfaSSage Weil 		if (drop_capsnap) {
2271819ccbfaSSage Weil 			ceph_put_snap_context(capsnap->context);
2272819ccbfaSSage Weil 			list_del(&capsnap->ci_item);
2273819ccbfaSSage Weil 			list_del(&capsnap->flushing_item);
2274819ccbfaSSage Weil 			ceph_put_cap_snap(capsnap);
2275819ccbfaSSage Weil 		}
2276a8599bd8SSage Weil 	}
2277a8599bd8SSage Weil 
2278be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2279a8599bd8SSage Weil 
2280a8599bd8SSage Weil 	if (last) {
2281a8599bd8SSage Weil 		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2282a8599bd8SSage Weil 		iput(inode);
2283819ccbfaSSage Weil 	} else if (complete_capsnap) {
2284a8599bd8SSage Weil 		ceph_flush_snaps(ci);
228503066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
2286a8599bd8SSage Weil 	}
2287819ccbfaSSage Weil 	if (drop_capsnap)
2288819ccbfaSSage Weil 		iput(inode);
2289a8599bd8SSage Weil }
2290a8599bd8SSage Weil 
2291a8599bd8SSage Weil /*
2292a8599bd8SSage Weil  * Handle a cap GRANT message from the MDS.  (Note that a GRANT may
2293a8599bd8SSage Weil  * actually be a revocation if it specifies a smaller cap set.)
2294a8599bd8SSage Weil  *
2295be655596SSage Weil  * caller holds s_mutex and i_ceph_lock, we drop both.
229615637c8bSSage Weil  *
2297a8599bd8SSage Weil  * return value:
2298a8599bd8SSage Weil  *  0 - ok
2299a8599bd8SSage Weil  *  1 - check_caps on auth cap only (writeback)
2300a8599bd8SSage Weil  *  2 - check_caps (ack revoke)
2301a8599bd8SSage Weil  */
230215637c8bSSage Weil static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant,
2303a8599bd8SSage Weil 			     struct ceph_mds_session *session,
2304a8599bd8SSage Weil 			     struct ceph_cap *cap,
2305a8599bd8SSage Weil 			     struct ceph_buffer *xattr_buf)
2306be655596SSage Weil 		__releases(ci->i_ceph_lock)
2307a8599bd8SSage Weil {
2308a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2309a8599bd8SSage Weil 	int mds = session->s_mds;
23102f56f56aSSage Weil 	int seq = le32_to_cpu(grant->seq);
2311a8599bd8SSage Weil 	int newcaps = le32_to_cpu(grant->caps);
2312a8599bd8SSage Weil 	int issued, implemented, used, wanted, dirty;
2313a8599bd8SSage Weil 	u64 size = le64_to_cpu(grant->size);
2314a8599bd8SSage Weil 	u64 max_size = le64_to_cpu(grant->max_size);
2315a8599bd8SSage Weil 	struct timespec mtime, atime, ctime;
231615637c8bSSage Weil 	int check_caps = 0;
2317a8599bd8SSage Weil 	int wake = 0;
2318a8599bd8SSage Weil 	int writeback = 0;
2319a8599bd8SSage Weil 	int revoked_rdcache = 0;
23203c6f6b79SSage Weil 	int queue_invalidate = 0;
2321a8599bd8SSage Weil 
23222f56f56aSSage Weil 	dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
23232f56f56aSSage Weil 	     inode, cap, mds, seq, ceph_cap_string(newcaps));
2324a8599bd8SSage Weil 	dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2325a8599bd8SSage Weil 		inode->i_size);
2326a8599bd8SSage Weil 
2327a8599bd8SSage Weil 	/*
2328a8599bd8SSage Weil 	 * If CACHE is being revoked, and we have no dirty buffers,
2329a8599bd8SSage Weil 	 * try to invalidate (once).  (If there are dirty buffers, we
2330a8599bd8SSage Weil 	 * will invalidate _after_ writeback.)
2331a8599bd8SSage Weil 	 */
23323b454c49SSage Weil 	if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
23333b454c49SSage Weil 	    (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
2334bcd2cbd1SYehuda Sadeh 	    !ci->i_wrbuffer_ref) {
23355ecad6fdSSage Weil 		if (try_nonblocking_invalidate(inode) == 0) {
23365ecad6fdSSage Weil 			revoked_rdcache = 1;
23375ecad6fdSSage Weil 		} else {
2338a8599bd8SSage Weil 			/* there were locked pages.. invalidate later
2339a8599bd8SSage Weil 			   in a separate thread. */
2340a8599bd8SSage Weil 			if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
23413c6f6b79SSage Weil 				queue_invalidate = 1;
2342a8599bd8SSage Weil 				ci->i_rdcache_revoking = ci->i_rdcache_gen;
2343a8599bd8SSage Weil 			}
2344a8599bd8SSage Weil 		}
2345a8599bd8SSage Weil 	}
2346a8599bd8SSage Weil 
2347a8599bd8SSage Weil 	/* side effects now are allowed */
2348a8599bd8SSage Weil 
2349a8599bd8SSage Weil 	issued = __ceph_caps_issued(ci, &implemented);
2350a8599bd8SSage Weil 	issued |= implemented | __ceph_caps_dirty(ci);
2351a8599bd8SSage Weil 
2352685f9a5dSSage Weil 	cap->cap_gen = session->s_cap_gen;
2353a8599bd8SSage Weil 
2354a8599bd8SSage Weil 	__check_cap_issue(ci, cap, newcaps);
2355a8599bd8SSage Weil 
2356a8599bd8SSage Weil 	if ((issued & CEPH_CAP_AUTH_EXCL) == 0) {
2357a8599bd8SSage Weil 		inode->i_mode = le32_to_cpu(grant->mode);
2358a8599bd8SSage Weil 		inode->i_uid = le32_to_cpu(grant->uid);
2359a8599bd8SSage Weil 		inode->i_gid = le32_to_cpu(grant->gid);
2360a8599bd8SSage Weil 		dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
2361a8599bd8SSage Weil 		     inode->i_uid, inode->i_gid);
2362a8599bd8SSage Weil 	}
2363a8599bd8SSage Weil 
2364a8599bd8SSage Weil 	if ((issued & CEPH_CAP_LINK_EXCL) == 0)
2365bfe86848SMiklos Szeredi 		set_nlink(inode, le32_to_cpu(grant->nlink));
2366a8599bd8SSage Weil 
2367a8599bd8SSage Weil 	if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
2368a8599bd8SSage Weil 		int len = le32_to_cpu(grant->xattr_len);
2369a8599bd8SSage Weil 		u64 version = le64_to_cpu(grant->xattr_version);
2370a8599bd8SSage Weil 
2371a8599bd8SSage Weil 		if (version > ci->i_xattrs.version) {
2372a8599bd8SSage Weil 			dout(" got new xattrs v%llu on %p len %d\n",
2373a8599bd8SSage Weil 			     version, inode, len);
2374a8599bd8SSage Weil 			if (ci->i_xattrs.blob)
2375a8599bd8SSage Weil 				ceph_buffer_put(ci->i_xattrs.blob);
2376a8599bd8SSage Weil 			ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
2377a8599bd8SSage Weil 			ci->i_xattrs.version = version;
2378a8599bd8SSage Weil 		}
2379a8599bd8SSage Weil 	}
2380a8599bd8SSage Weil 
2381a8599bd8SSage Weil 	/* size/ctime/mtime/atime? */
2382a8599bd8SSage Weil 	ceph_fill_file_size(inode, issued,
2383a8599bd8SSage Weil 			    le32_to_cpu(grant->truncate_seq),
2384a8599bd8SSage Weil 			    le64_to_cpu(grant->truncate_size), size);
2385a8599bd8SSage Weil 	ceph_decode_timespec(&mtime, &grant->mtime);
2386a8599bd8SSage Weil 	ceph_decode_timespec(&atime, &grant->atime);
2387a8599bd8SSage Weil 	ceph_decode_timespec(&ctime, &grant->ctime);
2388a8599bd8SSage Weil 	ceph_fill_file_time(inode, issued,
2389a8599bd8SSage Weil 			    le32_to_cpu(grant->time_warp_seq), &ctime, &mtime,
2390a8599bd8SSage Weil 			    &atime);
2391a8599bd8SSage Weil 
2392a8599bd8SSage Weil 	/* max size increase? */
23935e62ad30SYan, Zheng 	if (ci->i_auth_cap == cap && max_size != ci->i_max_size) {
2394a8599bd8SSage Weil 		dout("max_size %lld -> %llu\n", ci->i_max_size, max_size);
2395a8599bd8SSage Weil 		ci->i_max_size = max_size;
2396a8599bd8SSage Weil 		if (max_size >= ci->i_wanted_max_size) {
2397a8599bd8SSage Weil 			ci->i_wanted_max_size = 0;  /* reset */
2398a8599bd8SSage Weil 			ci->i_requested_max_size = 0;
2399a8599bd8SSage Weil 		}
2400a8599bd8SSage Weil 		wake = 1;
2401a8599bd8SSage Weil 	}
2402a8599bd8SSage Weil 
2403a8599bd8SSage Weil 	/* check cap bits */
2404a8599bd8SSage Weil 	wanted = __ceph_caps_wanted(ci);
2405a8599bd8SSage Weil 	used = __ceph_caps_used(ci);
2406a8599bd8SSage Weil 	dirty = __ceph_caps_dirty(ci);
2407a8599bd8SSage Weil 	dout(" my wanted = %s, used = %s, dirty %s\n",
2408a8599bd8SSage Weil 	     ceph_cap_string(wanted),
2409a8599bd8SSage Weil 	     ceph_cap_string(used),
2410a8599bd8SSage Weil 	     ceph_cap_string(dirty));
2411a8599bd8SSage Weil 	if (wanted != le32_to_cpu(grant->wanted)) {
2412a8599bd8SSage Weil 		dout("mds wanted %s -> %s\n",
2413a8599bd8SSage Weil 		     ceph_cap_string(le32_to_cpu(grant->wanted)),
2414a8599bd8SSage Weil 		     ceph_cap_string(wanted));
2415a8599bd8SSage Weil 		grant->wanted = cpu_to_le32(wanted);
2416a8599bd8SSage Weil 	}
2417a8599bd8SSage Weil 
2418a8599bd8SSage Weil 	cap->seq = seq;
2419a8599bd8SSage Weil 
2420a8599bd8SSage Weil 	/* file layout may have changed */
2421a8599bd8SSage Weil 	ci->i_layout = grant->layout;
2422a8599bd8SSage Weil 
2423a8599bd8SSage Weil 	/* revocation, grant, or no-op? */
2424a8599bd8SSage Weil 	if (cap->issued & ~newcaps) {
24253b454c49SSage Weil 		int revoking = cap->issued & ~newcaps;
24263b454c49SSage Weil 
24273b454c49SSage Weil 		dout("revocation: %s -> %s (revoking %s)\n",
24283b454c49SSage Weil 		     ceph_cap_string(cap->issued),
24293b454c49SSage Weil 		     ceph_cap_string(newcaps),
24303b454c49SSage Weil 		     ceph_cap_string(revoking));
24310eb6cd49SSage Weil 		if (revoking & used & CEPH_CAP_FILE_BUFFER)
24323b454c49SSage Weil 			writeback = 1;  /* initiate writeback; will delay ack */
24333b454c49SSage Weil 		else if (revoking == CEPH_CAP_FILE_CACHE &&
24343b454c49SSage Weil 			 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
24353b454c49SSage Weil 			 queue_invalidate)
24363b454c49SSage Weil 			; /* do nothing yet, invalidation will be queued */
24373b454c49SSage Weil 		else if (cap == ci->i_auth_cap)
24383b454c49SSage Weil 			check_caps = 1; /* check auth cap only */
24393b454c49SSage Weil 		else
24403b454c49SSage Weil 			check_caps = 2; /* check all caps */
2441a8599bd8SSage Weil 		cap->issued = newcaps;
2442978097c9SSage Weil 		cap->implemented |= newcaps;
2443a8599bd8SSage Weil 	} else if (cap->issued == newcaps) {
2444a8599bd8SSage Weil 		dout("caps unchanged: %s -> %s\n",
2445a8599bd8SSage Weil 		     ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
2446a8599bd8SSage Weil 	} else {
2447a8599bd8SSage Weil 		dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
2448a8599bd8SSage Weil 		     ceph_cap_string(newcaps));
2449a8599bd8SSage Weil 		cap->issued = newcaps;
2450a8599bd8SSage Weil 		cap->implemented |= newcaps; /* add bits only, to
2451a8599bd8SSage Weil 					      * avoid stepping on a
2452a8599bd8SSage Weil 					      * pending revocation */
2453a8599bd8SSage Weil 		wake = 1;
2454a8599bd8SSage Weil 	}
2455978097c9SSage Weil 	BUG_ON(cap->issued & ~cap->implemented);
2456a8599bd8SSage Weil 
2457be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
24583c6f6b79SSage Weil 	if (writeback)
2459a8599bd8SSage Weil 		/*
2460a8599bd8SSage Weil 		 * queue inode for writeback: we can't actually call
2461a8599bd8SSage Weil 		 * filemap_write_and_wait, etc. from message handler
2462a8599bd8SSage Weil 		 * context.
2463a8599bd8SSage Weil 		 */
24643c6f6b79SSage Weil 		ceph_queue_writeback(inode);
24653c6f6b79SSage Weil 	if (queue_invalidate)
24663c6f6b79SSage Weil 		ceph_queue_invalidate(inode);
2467a8599bd8SSage Weil 	if (wake)
246803066f23SYehuda Sadeh 		wake_up_all(&ci->i_cap_wq);
246915637c8bSSage Weil 
247015637c8bSSage Weil 	if (check_caps == 1)
247115637c8bSSage Weil 		ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
247215637c8bSSage Weil 				session);
247315637c8bSSage Weil 	else if (check_caps == 2)
247415637c8bSSage Weil 		ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
247515637c8bSSage Weil 	else
247615637c8bSSage Weil 		mutex_unlock(&session->s_mutex);
2477a8599bd8SSage Weil }
2478a8599bd8SSage Weil 
2479a8599bd8SSage Weil /*
2480a8599bd8SSage Weil  * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
2481a8599bd8SSage Weil  * MDS has been safely committed.
2482a8599bd8SSage Weil  */
24836df058c0SSage Weil static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
2484a8599bd8SSage Weil 				 struct ceph_mds_caps *m,
2485a8599bd8SSage Weil 				 struct ceph_mds_session *session,
2486a8599bd8SSage Weil 				 struct ceph_cap *cap)
2487be655596SSage Weil 	__releases(ci->i_ceph_lock)
2488a8599bd8SSage Weil {
2489a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
24903d14c5d2SYehuda Sadeh 	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
2491a8599bd8SSage Weil 	unsigned seq = le32_to_cpu(m->seq);
2492a8599bd8SSage Weil 	int dirty = le32_to_cpu(m->dirty);
2493a8599bd8SSage Weil 	int cleaned = 0;
2494afcdaea3SSage Weil 	int drop = 0;
2495a8599bd8SSage Weil 	int i;
2496a8599bd8SSage Weil 
2497a8599bd8SSage Weil 	for (i = 0; i < CEPH_CAP_BITS; i++)
2498a8599bd8SSage Weil 		if ((dirty & (1 << i)) &&
2499a8599bd8SSage Weil 		    flush_tid == ci->i_cap_flush_tid[i])
2500a8599bd8SSage Weil 			cleaned |= 1 << i;
2501a8599bd8SSage Weil 
2502a8599bd8SSage Weil 	dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
2503a8599bd8SSage Weil 	     " flushing %s -> %s\n",
2504a8599bd8SSage Weil 	     inode, session->s_mds, seq, ceph_cap_string(dirty),
2505a8599bd8SSage Weil 	     ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
2506a8599bd8SSage Weil 	     ceph_cap_string(ci->i_flushing_caps & ~cleaned));
2507a8599bd8SSage Weil 
2508a8599bd8SSage Weil 	if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned))
2509a8599bd8SSage Weil 		goto out;
2510a8599bd8SSage Weil 
2511a8599bd8SSage Weil 	ci->i_flushing_caps &= ~cleaned;
2512a8599bd8SSage Weil 
2513a8599bd8SSage Weil 	spin_lock(&mdsc->cap_dirty_lock);
2514a8599bd8SSage Weil 	if (ci->i_flushing_caps == 0) {
2515a8599bd8SSage Weil 		list_del_init(&ci->i_flushing_item);
2516a8599bd8SSage Weil 		if (!list_empty(&session->s_cap_flushing))
2517a8599bd8SSage Weil 			dout(" mds%d still flushing cap on %p\n",
2518a8599bd8SSage Weil 			     session->s_mds,
2519a8599bd8SSage Weil 			     &list_entry(session->s_cap_flushing.next,
2520a8599bd8SSage Weil 					 struct ceph_inode_info,
2521a8599bd8SSage Weil 					 i_flushing_item)->vfs_inode);
2522a8599bd8SSage Weil 		mdsc->num_cap_flushing--;
252303066f23SYehuda Sadeh 		wake_up_all(&mdsc->cap_flushing_wq);
2524a8599bd8SSage Weil 		dout(" inode %p now !flushing\n", inode);
2525afcdaea3SSage Weil 
2526afcdaea3SSage Weil 		if (ci->i_dirty_caps == 0) {
2527a8599bd8SSage Weil 			dout(" inode %p now clean\n", inode);
2528afcdaea3SSage Weil 			BUG_ON(!list_empty(&ci->i_dirty_item));
2529afcdaea3SSage Weil 			drop = 1;
25307d8cb26dSSage Weil 			if (ci->i_wrbuffer_ref_head == 0) {
25317d8cb26dSSage Weil 				BUG_ON(!ci->i_head_snapc);
25327d8cb26dSSage Weil 				ceph_put_snap_context(ci->i_head_snapc);
25337d8cb26dSSage Weil 				ci->i_head_snapc = NULL;
25347d8cb26dSSage Weil 			}
253576e3b390SSage Weil 		} else {
253676e3b390SSage Weil 			BUG_ON(list_empty(&ci->i_dirty_item));
2537afcdaea3SSage Weil 		}
2538a8599bd8SSage Weil 	}
2539a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_dirty_lock);
254003066f23SYehuda Sadeh 	wake_up_all(&ci->i_cap_wq);
2541a8599bd8SSage Weil 
2542a8599bd8SSage Weil out:
2543be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2544afcdaea3SSage Weil 	if (drop)
2545a8599bd8SSage Weil 		iput(inode);
2546a8599bd8SSage Weil }
2547a8599bd8SSage Weil 
2548a8599bd8SSage Weil /*
2549a8599bd8SSage Weil  * Handle FLUSHSNAP_ACK.  MDS has flushed snap data to disk and we can
2550a8599bd8SSage Weil  * throw away our cap_snap.
2551a8599bd8SSage Weil  *
2552a8599bd8SSage Weil  * Caller hold s_mutex.
2553a8599bd8SSage Weil  */
25546df058c0SSage Weil static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
2555a8599bd8SSage Weil 				     struct ceph_mds_caps *m,
2556a8599bd8SSage Weil 				     struct ceph_mds_session *session)
2557a8599bd8SSage Weil {
2558a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2559a8599bd8SSage Weil 	u64 follows = le64_to_cpu(m->snap_follows);
2560a8599bd8SSage Weil 	struct ceph_cap_snap *capsnap;
2561a8599bd8SSage Weil 	int drop = 0;
2562a8599bd8SSage Weil 
2563a8599bd8SSage Weil 	dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
2564a8599bd8SSage Weil 	     inode, ci, session->s_mds, follows);
2565a8599bd8SSage Weil 
2566be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2567a8599bd8SSage Weil 	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2568a8599bd8SSage Weil 		if (capsnap->follows == follows) {
2569a8599bd8SSage Weil 			if (capsnap->flush_tid != flush_tid) {
2570a8599bd8SSage Weil 				dout(" cap_snap %p follows %lld tid %lld !="
2571a8599bd8SSage Weil 				     " %lld\n", capsnap, follows,
2572a8599bd8SSage Weil 				     flush_tid, capsnap->flush_tid);
2573a8599bd8SSage Weil 				break;
2574a8599bd8SSage Weil 			}
2575a8599bd8SSage Weil 			WARN_ON(capsnap->dirty_pages || capsnap->writing);
2576819ccbfaSSage Weil 			dout(" removing %p cap_snap %p follows %lld\n",
2577819ccbfaSSage Weil 			     inode, capsnap, follows);
2578a8599bd8SSage Weil 			ceph_put_snap_context(capsnap->context);
2579a8599bd8SSage Weil 			list_del(&capsnap->ci_item);
2580a8599bd8SSage Weil 			list_del(&capsnap->flushing_item);
2581a8599bd8SSage Weil 			ceph_put_cap_snap(capsnap);
2582a8599bd8SSage Weil 			drop = 1;
2583a8599bd8SSage Weil 			break;
2584a8599bd8SSage Weil 		} else {
2585a8599bd8SSage Weil 			dout(" skipping cap_snap %p follows %lld\n",
2586a8599bd8SSage Weil 			     capsnap, capsnap->follows);
2587a8599bd8SSage Weil 		}
2588a8599bd8SSage Weil 	}
2589be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2590a8599bd8SSage Weil 	if (drop)
2591a8599bd8SSage Weil 		iput(inode);
2592a8599bd8SSage Weil }
2593a8599bd8SSage Weil 
2594a8599bd8SSage Weil /*
2595a8599bd8SSage Weil  * Handle TRUNC from MDS, indicating file truncation.
2596a8599bd8SSage Weil  *
2597a8599bd8SSage Weil  * caller hold s_mutex.
2598a8599bd8SSage Weil  */
2599a8599bd8SSage Weil static void handle_cap_trunc(struct inode *inode,
2600a8599bd8SSage Weil 			     struct ceph_mds_caps *trunc,
2601a8599bd8SSage Weil 			     struct ceph_mds_session *session)
2602be655596SSage Weil 	__releases(ci->i_ceph_lock)
2603a8599bd8SSage Weil {
2604a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2605a8599bd8SSage Weil 	int mds = session->s_mds;
2606a8599bd8SSage Weil 	int seq = le32_to_cpu(trunc->seq);
2607a8599bd8SSage Weil 	u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
2608a8599bd8SSage Weil 	u64 truncate_size = le64_to_cpu(trunc->truncate_size);
2609a8599bd8SSage Weil 	u64 size = le64_to_cpu(trunc->size);
2610a8599bd8SSage Weil 	int implemented = 0;
2611a8599bd8SSage Weil 	int dirty = __ceph_caps_dirty(ci);
2612a8599bd8SSage Weil 	int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
2613a8599bd8SSage Weil 	int queue_trunc = 0;
2614a8599bd8SSage Weil 
2615a8599bd8SSage Weil 	issued |= implemented | dirty;
2616a8599bd8SSage Weil 
2617a8599bd8SSage Weil 	dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
2618a8599bd8SSage Weil 	     inode, mds, seq, truncate_size, truncate_seq);
2619a8599bd8SSage Weil 	queue_trunc = ceph_fill_file_size(inode, issued,
2620a8599bd8SSage Weil 					  truncate_seq, truncate_size, size);
2621be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2622a8599bd8SSage Weil 
2623a8599bd8SSage Weil 	if (queue_trunc)
26243c6f6b79SSage Weil 		ceph_queue_vmtruncate(inode);
2625a8599bd8SSage Weil }
2626a8599bd8SSage Weil 
2627a8599bd8SSage Weil /*
2628a8599bd8SSage Weil  * Handle EXPORT from MDS.  Cap is being migrated _from_ this mds to a
2629a8599bd8SSage Weil  * different one.  If we are the most recent migration we've seen (as
2630a8599bd8SSage Weil  * indicated by mseq), make note of the migrating cap bits for the
2631a8599bd8SSage Weil  * duration (until we see the corresponding IMPORT).
2632a8599bd8SSage Weil  *
2633a8599bd8SSage Weil  * caller holds s_mutex
2634a8599bd8SSage Weil  */
2635a8599bd8SSage Weil static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
2636154f42c2SSage Weil 			      struct ceph_mds_session *session,
2637154f42c2SSage Weil 			      int *open_target_sessions)
2638a8599bd8SSage Weil {
2639db354052SSage Weil 	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
2640a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2641a8599bd8SSage Weil 	int mds = session->s_mds;
2642a8599bd8SSage Weil 	unsigned mseq = le32_to_cpu(ex->migrate_seq);
2643a8599bd8SSage Weil 	struct ceph_cap *cap = NULL, *t;
2644a8599bd8SSage Weil 	struct rb_node *p;
2645a8599bd8SSage Weil 	int remember = 1;
2646a8599bd8SSage Weil 
2647a8599bd8SSage Weil 	dout("handle_cap_export inode %p ci %p mds%d mseq %d\n",
2648a8599bd8SSage Weil 	     inode, ci, mds, mseq);
2649a8599bd8SSage Weil 
2650be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2651a8599bd8SSage Weil 
2652a8599bd8SSage Weil 	/* make sure we haven't seen a higher mseq */
2653a8599bd8SSage Weil 	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2654a8599bd8SSage Weil 		t = rb_entry(p, struct ceph_cap, ci_node);
2655a8599bd8SSage Weil 		if (ceph_seq_cmp(t->mseq, mseq) > 0) {
2656a8599bd8SSage Weil 			dout(" higher mseq on cap from mds%d\n",
2657a8599bd8SSage Weil 			     t->session->s_mds);
2658a8599bd8SSage Weil 			remember = 0;
2659a8599bd8SSage Weil 		}
2660a8599bd8SSage Weil 		if (t->session->s_mds == mds)
2661a8599bd8SSage Weil 			cap = t;
2662a8599bd8SSage Weil 	}
2663a8599bd8SSage Weil 
2664a8599bd8SSage Weil 	if (cap) {
2665a8599bd8SSage Weil 		if (remember) {
2666a8599bd8SSage Weil 			/* make note */
2667a8599bd8SSage Weil 			ci->i_cap_exporting_mds = mds;
2668a8599bd8SSage Weil 			ci->i_cap_exporting_mseq = mseq;
2669a8599bd8SSage Weil 			ci->i_cap_exporting_issued = cap->issued;
2670154f42c2SSage Weil 
2671154f42c2SSage Weil 			/*
2672154f42c2SSage Weil 			 * make sure we have open sessions with all possible
2673154f42c2SSage Weil 			 * export targets, so that we get the matching IMPORT
2674154f42c2SSage Weil 			 */
2675154f42c2SSage Weil 			*open_target_sessions = 1;
2676db354052SSage Weil 
2677db354052SSage Weil 			/*
2678db354052SSage Weil 			 * we can't flush dirty caps that we've seen the
2679db354052SSage Weil 			 * EXPORT but no IMPORT for
2680db354052SSage Weil 			 */
2681db354052SSage Weil 			spin_lock(&mdsc->cap_dirty_lock);
2682db354052SSage Weil 			if (!list_empty(&ci->i_dirty_item)) {
2683db354052SSage Weil 				dout(" moving %p to cap_dirty_migrating\n",
2684db354052SSage Weil 				     inode);
2685db354052SSage Weil 				list_move(&ci->i_dirty_item,
2686db354052SSage Weil 					  &mdsc->cap_dirty_migrating);
2687db354052SSage Weil 			}
2688db354052SSage Weil 			spin_unlock(&mdsc->cap_dirty_lock);
2689a8599bd8SSage Weil 		}
26907c1332b8SSage Weil 		__ceph_remove_cap(cap);
2691a8599bd8SSage Weil 	}
26924ea0043aSSage Weil 	/* else, we already released it */
2693a8599bd8SSage Weil 
2694be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2695a8599bd8SSage Weil }
2696a8599bd8SSage Weil 
2697a8599bd8SSage Weil /*
2698a8599bd8SSage Weil  * Handle cap IMPORT.  If there are temp bits from an older EXPORT,
2699a8599bd8SSage Weil  * clean them up.
2700a8599bd8SSage Weil  *
2701a8599bd8SSage Weil  * caller holds s_mutex.
2702a8599bd8SSage Weil  */
2703a8599bd8SSage Weil static void handle_cap_import(struct ceph_mds_client *mdsc,
2704a8599bd8SSage Weil 			      struct inode *inode, struct ceph_mds_caps *im,
2705a8599bd8SSage Weil 			      struct ceph_mds_session *session,
2706a8599bd8SSage Weil 			      void *snaptrace, int snaptrace_len)
2707a8599bd8SSage Weil {
2708a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2709a8599bd8SSage Weil 	int mds = session->s_mds;
2710a8599bd8SSage Weil 	unsigned issued = le32_to_cpu(im->caps);
2711a8599bd8SSage Weil 	unsigned wanted = le32_to_cpu(im->wanted);
2712a8599bd8SSage Weil 	unsigned seq = le32_to_cpu(im->seq);
2713a8599bd8SSage Weil 	unsigned mseq = le32_to_cpu(im->migrate_seq);
2714a8599bd8SSage Weil 	u64 realmino = le64_to_cpu(im->realm);
2715a8599bd8SSage Weil 	u64 cap_id = le64_to_cpu(im->cap_id);
2716a8599bd8SSage Weil 
2717a8599bd8SSage Weil 	if (ci->i_cap_exporting_mds >= 0 &&
2718a8599bd8SSage Weil 	    ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) {
2719a8599bd8SSage Weil 		dout("handle_cap_import inode %p ci %p mds%d mseq %d"
2720a8599bd8SSage Weil 		     " - cleared exporting from mds%d\n",
2721a8599bd8SSage Weil 		     inode, ci, mds, mseq,
2722a8599bd8SSage Weil 		     ci->i_cap_exporting_mds);
2723a8599bd8SSage Weil 		ci->i_cap_exporting_issued = 0;
2724a8599bd8SSage Weil 		ci->i_cap_exporting_mseq = 0;
2725a8599bd8SSage Weil 		ci->i_cap_exporting_mds = -1;
2726db354052SSage Weil 
2727db354052SSage Weil 		spin_lock(&mdsc->cap_dirty_lock);
2728db354052SSage Weil 		if (!list_empty(&ci->i_dirty_item)) {
2729db354052SSage Weil 			dout(" moving %p back to cap_dirty\n", inode);
2730db354052SSage Weil 			list_move(&ci->i_dirty_item, &mdsc->cap_dirty);
2731db354052SSage Weil 		}
2732db354052SSage Weil 		spin_unlock(&mdsc->cap_dirty_lock);
2733a8599bd8SSage Weil 	} else {
2734a8599bd8SSage Weil 		dout("handle_cap_import inode %p ci %p mds%d mseq %d\n",
2735a8599bd8SSage Weil 		     inode, ci, mds, mseq);
2736a8599bd8SSage Weil 	}
2737a8599bd8SSage Weil 
2738a8599bd8SSage Weil 	down_write(&mdsc->snap_rwsem);
2739a8599bd8SSage Weil 	ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len,
2740a8599bd8SSage Weil 			       false);
2741a8599bd8SSage Weil 	downgrade_write(&mdsc->snap_rwsem);
2742a8599bd8SSage Weil 	ceph_add_cap(inode, session, cap_id, -1,
2743a8599bd8SSage Weil 		     issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH,
2744a8599bd8SSage Weil 		     NULL /* no caps context */);
2745088b3f5eSSage Weil 	kick_flushing_inode_caps(mdsc, session, inode);
2746a8599bd8SSage Weil 	up_read(&mdsc->snap_rwsem);
2747feb4cc9bSSage Weil 
2748feb4cc9bSSage Weil 	/* make sure we re-request max_size, if necessary */
2749be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2750feb4cc9bSSage Weil 	ci->i_requested_max_size = 0;
2751be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2752a8599bd8SSage Weil }
2753a8599bd8SSage Weil 
2754a8599bd8SSage Weil /*
2755a8599bd8SSage Weil  * Handle a caps message from the MDS.
2756a8599bd8SSage Weil  *
2757a8599bd8SSage Weil  * Identify the appropriate session, inode, and call the right handler
2758a8599bd8SSage Weil  * based on the cap op.
2759a8599bd8SSage Weil  */
2760a8599bd8SSage Weil void ceph_handle_caps(struct ceph_mds_session *session,
2761a8599bd8SSage Weil 		      struct ceph_msg *msg)
2762a8599bd8SSage Weil {
2763a8599bd8SSage Weil 	struct ceph_mds_client *mdsc = session->s_mdsc;
27643d14c5d2SYehuda Sadeh 	struct super_block *sb = mdsc->fsc->sb;
2765a8599bd8SSage Weil 	struct inode *inode;
2766be655596SSage Weil 	struct ceph_inode_info *ci;
2767a8599bd8SSage Weil 	struct ceph_cap *cap;
2768a8599bd8SSage Weil 	struct ceph_mds_caps *h;
27692600d2ddSSage Weil 	int mds = session->s_mds;
2770a8599bd8SSage Weil 	int op;
27713d7ded4dSSage Weil 	u32 seq, mseq;
2772a8599bd8SSage Weil 	struct ceph_vino vino;
2773a8599bd8SSage Weil 	u64 cap_id;
2774a8599bd8SSage Weil 	u64 size, max_size;
27756df058c0SSage Weil 	u64 tid;
277670edb55bSSage Weil 	void *snaptrace;
2777ce1fbc8dSSage Weil 	size_t snaptrace_len;
2778ce1fbc8dSSage Weil 	void *flock;
2779ce1fbc8dSSage Weil 	u32 flock_len;
2780154f42c2SSage Weil 	int open_target_sessions = 0;
2781a8599bd8SSage Weil 
2782a8599bd8SSage Weil 	dout("handle_caps from mds%d\n", mds);
2783a8599bd8SSage Weil 
2784a8599bd8SSage Weil 	/* decode */
27856df058c0SSage Weil 	tid = le64_to_cpu(msg->hdr.tid);
2786a8599bd8SSage Weil 	if (msg->front.iov_len < sizeof(*h))
2787a8599bd8SSage Weil 		goto bad;
2788a8599bd8SSage Weil 	h = msg->front.iov_base;
2789a8599bd8SSage Weil 	op = le32_to_cpu(h->op);
2790a8599bd8SSage Weil 	vino.ino = le64_to_cpu(h->ino);
2791a8599bd8SSage Weil 	vino.snap = CEPH_NOSNAP;
2792a8599bd8SSage Weil 	cap_id = le64_to_cpu(h->cap_id);
2793a8599bd8SSage Weil 	seq = le32_to_cpu(h->seq);
27943d7ded4dSSage Weil 	mseq = le32_to_cpu(h->migrate_seq);
2795a8599bd8SSage Weil 	size = le64_to_cpu(h->size);
2796a8599bd8SSage Weil 	max_size = le64_to_cpu(h->max_size);
2797a8599bd8SSage Weil 
2798ce1fbc8dSSage Weil 	snaptrace = h + 1;
2799ce1fbc8dSSage Weil 	snaptrace_len = le32_to_cpu(h->snap_trace_len);
2800ce1fbc8dSSage Weil 
2801ce1fbc8dSSage Weil 	if (le16_to_cpu(msg->hdr.version) >= 2) {
2802ce1fbc8dSSage Weil 		void *p, *end;
2803ce1fbc8dSSage Weil 
2804ce1fbc8dSSage Weil 		p = snaptrace + snaptrace_len;
2805ce1fbc8dSSage Weil 		end = msg->front.iov_base + msg->front.iov_len;
2806ce1fbc8dSSage Weil 		ceph_decode_32_safe(&p, end, flock_len, bad);
2807ce1fbc8dSSage Weil 		flock = p;
2808ce1fbc8dSSage Weil 	} else {
2809ce1fbc8dSSage Weil 		flock = NULL;
2810ce1fbc8dSSage Weil 		flock_len = 0;
2811ce1fbc8dSSage Weil 	}
2812ce1fbc8dSSage Weil 
2813a8599bd8SSage Weil 	mutex_lock(&session->s_mutex);
2814a8599bd8SSage Weil 	session->s_seq++;
2815a8599bd8SSage Weil 	dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
2816a8599bd8SSage Weil 	     (unsigned)seq);
2817a8599bd8SSage Weil 
2818a8599bd8SSage Weil 	/* lookup ino */
2819a8599bd8SSage Weil 	inode = ceph_find_inode(sb, vino);
2820be655596SSage Weil 	ci = ceph_inode(inode);
2821a8599bd8SSage Weil 	dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
2822a8599bd8SSage Weil 	     vino.snap, inode);
2823a8599bd8SSage Weil 	if (!inode) {
2824a8599bd8SSage Weil 		dout(" i don't have ino %llx\n", vino.ino);
28253d7ded4dSSage Weil 
28263d7ded4dSSage Weil 		if (op == CEPH_CAP_OP_IMPORT)
28273d7ded4dSSage Weil 			__queue_cap_release(session, vino.ino, cap_id,
28283d7ded4dSSage Weil 					    mseq, seq);
282921b559deSGreg Farnum 		goto flush_cap_releases;
2830a8599bd8SSage Weil 	}
2831a8599bd8SSage Weil 
2832a8599bd8SSage Weil 	/* these will work even if we don't have a cap yet */
2833a8599bd8SSage Weil 	switch (op) {
2834a8599bd8SSage Weil 	case CEPH_CAP_OP_FLUSHSNAP_ACK:
28356df058c0SSage Weil 		handle_cap_flushsnap_ack(inode, tid, h, session);
2836a8599bd8SSage Weil 		goto done;
2837a8599bd8SSage Weil 
2838a8599bd8SSage Weil 	case CEPH_CAP_OP_EXPORT:
2839154f42c2SSage Weil 		handle_cap_export(inode, h, session, &open_target_sessions);
2840a8599bd8SSage Weil 		goto done;
2841a8599bd8SSage Weil 
2842a8599bd8SSage Weil 	case CEPH_CAP_OP_IMPORT:
2843a8599bd8SSage Weil 		handle_cap_import(mdsc, inode, h, session,
2844ce1fbc8dSSage Weil 				  snaptrace, snaptrace_len);
28457e57b81cSSage Weil 		ceph_check_caps(ceph_inode(inode), 0, session);
284615637c8bSSage Weil 		goto done_unlocked;
2847a8599bd8SSage Weil 	}
2848a8599bd8SSage Weil 
2849a8599bd8SSage Weil 	/* the rest require a cap */
2850be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2851a8599bd8SSage Weil 	cap = __get_cap_for_mds(ceph_inode(inode), mds);
2852a8599bd8SSage Weil 	if (!cap) {
28539dbd412fSSage Weil 		dout(" no cap on %p ino %llx.%llx from mds%d\n",
2854a8599bd8SSage Weil 		     inode, ceph_ino(inode), ceph_snap(inode), mds);
2855be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
285621b559deSGreg Farnum 		goto flush_cap_releases;
2857a8599bd8SSage Weil 	}
2858a8599bd8SSage Weil 
2859be655596SSage Weil 	/* note that each of these drops i_ceph_lock for us */
2860a8599bd8SSage Weil 	switch (op) {
2861a8599bd8SSage Weil 	case CEPH_CAP_OP_REVOKE:
2862a8599bd8SSage Weil 	case CEPH_CAP_OP_GRANT:
286315637c8bSSage Weil 		handle_cap_grant(inode, h, session, cap, msg->middle);
286415637c8bSSage Weil 		goto done_unlocked;
2865a8599bd8SSage Weil 
2866a8599bd8SSage Weil 	case CEPH_CAP_OP_FLUSH_ACK:
28676df058c0SSage Weil 		handle_cap_flush_ack(inode, tid, h, session, cap);
2868a8599bd8SSage Weil 		break;
2869a8599bd8SSage Weil 
2870a8599bd8SSage Weil 	case CEPH_CAP_OP_TRUNC:
2871a8599bd8SSage Weil 		handle_cap_trunc(inode, h, session);
2872a8599bd8SSage Weil 		break;
2873a8599bd8SSage Weil 
2874a8599bd8SSage Weil 	default:
2875be655596SSage Weil 		spin_unlock(&ci->i_ceph_lock);
2876a8599bd8SSage Weil 		pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
2877a8599bd8SSage Weil 		       ceph_cap_op_name(op));
2878a8599bd8SSage Weil 	}
2879a8599bd8SSage Weil 
288021b559deSGreg Farnum 	goto done;
288121b559deSGreg Farnum 
288221b559deSGreg Farnum flush_cap_releases:
288321b559deSGreg Farnum 	/*
288421b559deSGreg Farnum 	 * send any full release message to try to move things
288521b559deSGreg Farnum 	 * along for the mds (who clearly thinks we still have this
288621b559deSGreg Farnum 	 * cap).
288721b559deSGreg Farnum 	 */
288821b559deSGreg Farnum 	ceph_add_cap_releases(mdsc, session);
288921b559deSGreg Farnum 	ceph_send_cap_releases(mdsc, session);
289021b559deSGreg Farnum 
2891a8599bd8SSage Weil done:
2892a8599bd8SSage Weil 	mutex_unlock(&session->s_mutex);
289315637c8bSSage Weil done_unlocked:
2894a8599bd8SSage Weil 	if (inode)
2895a8599bd8SSage Weil 		iput(inode);
2896154f42c2SSage Weil 	if (open_target_sessions)
2897154f42c2SSage Weil 		ceph_mdsc_open_export_target_sessions(mdsc, session);
2898a8599bd8SSage Weil 	return;
2899a8599bd8SSage Weil 
2900a8599bd8SSage Weil bad:
2901a8599bd8SSage Weil 	pr_err("ceph_handle_caps: corrupt message\n");
29029ec7cab1SSage Weil 	ceph_msg_dump(msg);
2903a8599bd8SSage Weil 	return;
2904a8599bd8SSage Weil }
2905a8599bd8SSage Weil 
2906a8599bd8SSage Weil /*
2907a8599bd8SSage Weil  * Delayed work handler to process end of delayed cap release LRU list.
2908a8599bd8SSage Weil  */
2909afcdaea3SSage Weil void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
2910a8599bd8SSage Weil {
2911a8599bd8SSage Weil 	struct ceph_inode_info *ci;
2912a8599bd8SSage Weil 	int flags = CHECK_CAPS_NODELAY;
2913a8599bd8SSage Weil 
2914a8599bd8SSage Weil 	dout("check_delayed_caps\n");
2915a8599bd8SSage Weil 	while (1) {
2916a8599bd8SSage Weil 		spin_lock(&mdsc->cap_delay_lock);
2917a8599bd8SSage Weil 		if (list_empty(&mdsc->cap_delay_list))
2918a8599bd8SSage Weil 			break;
2919a8599bd8SSage Weil 		ci = list_first_entry(&mdsc->cap_delay_list,
2920a8599bd8SSage Weil 				      struct ceph_inode_info,
2921a8599bd8SSage Weil 				      i_cap_delay_list);
2922a8599bd8SSage Weil 		if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
2923a8599bd8SSage Weil 		    time_before(jiffies, ci->i_hold_caps_max))
2924a8599bd8SSage Weil 			break;
2925a8599bd8SSage Weil 		list_del_init(&ci->i_cap_delay_list);
2926a8599bd8SSage Weil 		spin_unlock(&mdsc->cap_delay_lock);
2927a8599bd8SSage Weil 		dout("check_delayed_caps on %p\n", &ci->vfs_inode);
2928a8599bd8SSage Weil 		ceph_check_caps(ci, flags, NULL);
2929a8599bd8SSage Weil 	}
2930a8599bd8SSage Weil 	spin_unlock(&mdsc->cap_delay_lock);
2931a8599bd8SSage Weil }
2932a8599bd8SSage Weil 
2933a8599bd8SSage Weil /*
2934afcdaea3SSage Weil  * Flush all dirty caps to the mds
2935afcdaea3SSage Weil  */
2936afcdaea3SSage Weil void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
2937afcdaea3SSage Weil {
2938db354052SSage Weil 	struct ceph_inode_info *ci;
2939db354052SSage Weil 	struct inode *inode;
2940afcdaea3SSage Weil 
2941afcdaea3SSage Weil 	dout("flush_dirty_caps\n");
2942afcdaea3SSage Weil 	spin_lock(&mdsc->cap_dirty_lock);
2943db354052SSage Weil 	while (!list_empty(&mdsc->cap_dirty)) {
2944db354052SSage Weil 		ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
2945afcdaea3SSage Weil 				      i_dirty_item);
294670b666c3SSage Weil 		inode = &ci->vfs_inode;
294770b666c3SSage Weil 		ihold(inode);
2948db354052SSage Weil 		dout("flush_dirty_caps %p\n", inode);
2949afcdaea3SSage Weil 		spin_unlock(&mdsc->cap_dirty_lock);
295070b666c3SSage Weil 		ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
2951afcdaea3SSage Weil 		iput(inode);
2952afcdaea3SSage Weil 		spin_lock(&mdsc->cap_dirty_lock);
2953afcdaea3SSage Weil 	}
2954afcdaea3SSage Weil 	spin_unlock(&mdsc->cap_dirty_lock);
2955db354052SSage Weil 	dout("flush_dirty_caps done\n");
2956afcdaea3SSage Weil }
2957afcdaea3SSage Weil 
2958afcdaea3SSage Weil /*
2959a8599bd8SSage Weil  * Drop open file reference.  If we were the last open file,
2960a8599bd8SSage Weil  * we may need to release capabilities to the MDS (or schedule
2961a8599bd8SSage Weil  * their delayed release).
2962a8599bd8SSage Weil  */
2963a8599bd8SSage Weil void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
2964a8599bd8SSage Weil {
2965a8599bd8SSage Weil 	struct inode *inode = &ci->vfs_inode;
2966a8599bd8SSage Weil 	int last = 0;
2967a8599bd8SSage Weil 
2968be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2969a8599bd8SSage Weil 	dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode,
2970a8599bd8SSage Weil 	     ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1);
2971a8599bd8SSage Weil 	BUG_ON(ci->i_nr_by_mode[fmode] == 0);
2972a8599bd8SSage Weil 	if (--ci->i_nr_by_mode[fmode] == 0)
2973a8599bd8SSage Weil 		last++;
2974be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
2975a8599bd8SSage Weil 
2976a8599bd8SSage Weil 	if (last && ci->i_vino.snap == CEPH_NOSNAP)
2977a8599bd8SSage Weil 		ceph_check_caps(ci, 0, NULL);
2978a8599bd8SSage Weil }
2979a8599bd8SSage Weil 
2980a8599bd8SSage Weil /*
2981a8599bd8SSage Weil  * Helpers for embedding cap and dentry lease releases into mds
2982a8599bd8SSage Weil  * requests.
2983a8599bd8SSage Weil  *
2984a8599bd8SSage Weil  * @force is used by dentry_release (below) to force inclusion of a
2985a8599bd8SSage Weil  * record for the directory inode, even when there aren't any caps to
2986a8599bd8SSage Weil  * drop.
2987a8599bd8SSage Weil  */
2988a8599bd8SSage Weil int ceph_encode_inode_release(void **p, struct inode *inode,
2989a8599bd8SSage Weil 			      int mds, int drop, int unless, int force)
2990a8599bd8SSage Weil {
2991a8599bd8SSage Weil 	struct ceph_inode_info *ci = ceph_inode(inode);
2992a8599bd8SSage Weil 	struct ceph_cap *cap;
2993a8599bd8SSage Weil 	struct ceph_mds_request_release *rel = *p;
2994ec97f88bSSage Weil 	int used, dirty;
2995a8599bd8SSage Weil 	int ret = 0;
2996a8599bd8SSage Weil 
2997be655596SSage Weil 	spin_lock(&ci->i_ceph_lock);
2998916623daSSage Weil 	used = __ceph_caps_used(ci);
2999ec97f88bSSage Weil 	dirty = __ceph_caps_dirty(ci);
3000916623daSSage Weil 
3001ec97f88bSSage Weil 	dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3002ec97f88bSSage Weil 	     inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
3003916623daSSage Weil 	     ceph_cap_string(unless));
3004916623daSSage Weil 
3005ec97f88bSSage Weil 	/* only drop unused, clean caps */
3006ec97f88bSSage Weil 	drop &= ~(used | dirty);
3007916623daSSage Weil 
3008a8599bd8SSage Weil 	cap = __get_cap_for_mds(ci, mds);
3009a8599bd8SSage Weil 	if (cap && __cap_is_valid(cap)) {
3010a8599bd8SSage Weil 		if (force ||
3011a8599bd8SSage Weil 		    ((cap->issued & drop) &&
3012a8599bd8SSage Weil 		     (cap->issued & unless) == 0)) {
3013a8599bd8SSage Weil 			if ((cap->issued & drop) &&
3014a8599bd8SSage Weil 			    (cap->issued & unless) == 0) {
3015a8599bd8SSage Weil 				dout("encode_inode_release %p cap %p %s -> "
3016a8599bd8SSage Weil 				     "%s\n", inode, cap,
3017a8599bd8SSage Weil 				     ceph_cap_string(cap->issued),
3018a8599bd8SSage Weil 				     ceph_cap_string(cap->issued & ~drop));
3019a8599bd8SSage Weil 				cap->issued &= ~drop;
3020a8599bd8SSage Weil 				cap->implemented &= ~drop;
3021a8599bd8SSage Weil 				if (ci->i_ceph_flags & CEPH_I_NODELAY) {
3022a8599bd8SSage Weil 					int wanted = __ceph_caps_wanted(ci);
3023a8599bd8SSage Weil 					dout("  wanted %s -> %s (act %s)\n",
3024a8599bd8SSage Weil 					     ceph_cap_string(cap->mds_wanted),
3025a8599bd8SSage Weil 					     ceph_cap_string(cap->mds_wanted &
3026a8599bd8SSage Weil 							     ~wanted),
3027a8599bd8SSage Weil 					     ceph_cap_string(wanted));
3028a8599bd8SSage Weil 					cap->mds_wanted &= wanted;
3029a8599bd8SSage Weil 				}
3030a8599bd8SSage Weil 			} else {
3031a8599bd8SSage Weil 				dout("encode_inode_release %p cap %p %s"
3032a8599bd8SSage Weil 				     " (force)\n", inode, cap,
3033a8599bd8SSage Weil 				     ceph_cap_string(cap->issued));
3034a8599bd8SSage Weil 			}
3035a8599bd8SSage Weil 
3036a8599bd8SSage Weil 			rel->ino = cpu_to_le64(ceph_ino(inode));
3037a8599bd8SSage Weil 			rel->cap_id = cpu_to_le64(cap->cap_id);
3038a8599bd8SSage Weil 			rel->seq = cpu_to_le32(cap->seq);
3039a8599bd8SSage Weil 			rel->issue_seq = cpu_to_le32(cap->issue_seq),
3040a8599bd8SSage Weil 			rel->mseq = cpu_to_le32(cap->mseq);
3041a8599bd8SSage Weil 			rel->caps = cpu_to_le32(cap->issued);
3042a8599bd8SSage Weil 			rel->wanted = cpu_to_le32(cap->mds_wanted);
3043a8599bd8SSage Weil 			rel->dname_len = 0;
3044a8599bd8SSage Weil 			rel->dname_seq = 0;
3045a8599bd8SSage Weil 			*p += sizeof(*rel);
3046a8599bd8SSage Weil 			ret = 1;
3047a8599bd8SSage Weil 		} else {
3048a8599bd8SSage Weil 			dout("encode_inode_release %p cap %p %s\n",
3049a8599bd8SSage Weil 			     inode, cap, ceph_cap_string(cap->issued));
3050a8599bd8SSage Weil 		}
3051a8599bd8SSage Weil 	}
3052be655596SSage Weil 	spin_unlock(&ci->i_ceph_lock);
3053a8599bd8SSage Weil 	return ret;
3054a8599bd8SSage Weil }
3055a8599bd8SSage Weil 
3056a8599bd8SSage Weil int ceph_encode_dentry_release(void **p, struct dentry *dentry,
3057a8599bd8SSage Weil 			       int mds, int drop, int unless)
3058a8599bd8SSage Weil {
3059a8599bd8SSage Weil 	struct inode *dir = dentry->d_parent->d_inode;
3060a8599bd8SSage Weil 	struct ceph_mds_request_release *rel = *p;
3061a8599bd8SSage Weil 	struct ceph_dentry_info *di = ceph_dentry(dentry);
3062a8599bd8SSage Weil 	int force = 0;
3063a8599bd8SSage Weil 	int ret;
3064a8599bd8SSage Weil 
3065a8599bd8SSage Weil 	/*
3066a8599bd8SSage Weil 	 * force an record for the directory caps if we have a dentry lease.
3067be655596SSage Weil 	 * this is racy (can't take i_ceph_lock and d_lock together), but it
3068a8599bd8SSage Weil 	 * doesn't have to be perfect; the mds will revoke anything we don't
3069a8599bd8SSage Weil 	 * release.
3070a8599bd8SSage Weil 	 */
3071a8599bd8SSage Weil 	spin_lock(&dentry->d_lock);
3072a8599bd8SSage Weil 	if (di->lease_session && di->lease_session->s_mds == mds)
3073a8599bd8SSage Weil 		force = 1;
3074a8599bd8SSage Weil 	spin_unlock(&dentry->d_lock);
3075a8599bd8SSage Weil 
3076a8599bd8SSage Weil 	ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
3077a8599bd8SSage Weil 
3078a8599bd8SSage Weil 	spin_lock(&dentry->d_lock);
3079a8599bd8SSage Weil 	if (ret && di->lease_session && di->lease_session->s_mds == mds) {
3080a8599bd8SSage Weil 		dout("encode_dentry_release %p mds%d seq %d\n",
3081a8599bd8SSage Weil 		     dentry, mds, (int)di->lease_seq);
3082a8599bd8SSage Weil 		rel->dname_len = cpu_to_le32(dentry->d_name.len);
3083a8599bd8SSage Weil 		memcpy(*p, dentry->d_name.name, dentry->d_name.len);
3084a8599bd8SSage Weil 		*p += dentry->d_name.len;
3085a8599bd8SSage Weil 		rel->dname_seq = cpu_to_le32(di->lease_seq);
30861dadcce3SSage Weil 		__ceph_mdsc_drop_dentry_lease(dentry);
3087a8599bd8SSage Weil 	}
3088a8599bd8SSage Weil 	spin_unlock(&dentry->d_lock);
3089a8599bd8SSage Weil 	return ret;
3090a8599bd8SSage Weil }
3091