Blame view

fs/ceph/caps.c 121 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
2
  #include <linux/ceph/ceph_debug.h>
a8599bd82   Sage Weil   ceph: capability ...
3
4
5
  
  #include <linux/fs.h>
  #include <linux/kernel.h>
174cd4b1e   Ingo Molnar   sched/headers: Pr...
6
  #include <linux/sched/signal.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
7
  #include <linux/slab.h>
a8599bd82   Sage Weil   ceph: capability ...
8
9
  #include <linux/vmalloc.h>
  #include <linux/wait.h>
f1a3d5721   Stephen Rothwell   ceph: update for ...
10
  #include <linux/writeback.h>
176c77c9c   Jeff Layton   ceph: handle chan...
11
  #include <linux/iversion.h>
a8599bd82   Sage Weil   ceph: capability ...
12
13
  
  #include "super.h"
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
14
  #include "mds_client.h"
99ccbd229   Milosz Tanski   ceph: use fscache...
15
  #include "cache.h"
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
16
17
  #include <linux/ceph/decode.h>
  #include <linux/ceph/messenger.h>
a8599bd82   Sage Weil   ceph: capability ...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
  /*
   * Capability management
   *
   * The Ceph metadata servers control client access to inode metadata
   * and file data by issuing capabilities, granting clients permission
   * to read and/or write both inode field and file data to OSDs
   * (storage nodes).  Each capability consists of a set of bits
   * indicating which operations are allowed.
   *
   * If the client holds a *_SHARED cap, the client has a coherent value
   * that can be safely read from the cached inode.
   *
   * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
   * client is allowed to change inode attributes (e.g., file size,
   * mtime), note its dirty state in the ceph_cap, and asynchronously
   * flush that metadata change to the MDS.
   *
   * In the event of a conflicting operation (perhaps by another
   * client), the MDS will revoke the conflicting client capabilities.
   *
   * In order for a client to cache an inode, it must hold a capability
   * with at least one MDS server.  When inodes are released, release
   * notifications are batched and periodically sent en masse to the MDS
   * cluster to release server state.
   */
0e2943878   Yan, Zheng   ceph: unify cap f...
44
  static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
7bc00fddb   Yan, Zheng   ceph: kick cap fl...
45
46
47
48
  static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
  				 struct ceph_mds_session *session,
  				 struct ceph_inode_info *ci,
  				 u64 oldest_flush_tid);
a8599bd82   Sage Weil   ceph: capability ...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  
  /*
   * Generate readable cap strings for debugging output.
   */
  #define MAX_CAP_STR 20
  static char cap_str[MAX_CAP_STR][40];
  static DEFINE_SPINLOCK(cap_str_lock);
  static int last_cap_str;
  
  static char *gcap_string(char *s, int c)
  {
  	if (c & CEPH_CAP_GSHARED)
  		*s++ = 's';
  	if (c & CEPH_CAP_GEXCL)
  		*s++ = 'x';
  	if (c & CEPH_CAP_GCACHE)
  		*s++ = 'c';
  	if (c & CEPH_CAP_GRD)
  		*s++ = 'r';
  	if (c & CEPH_CAP_GWR)
  		*s++ = 'w';
  	if (c & CEPH_CAP_GBUFFER)
  		*s++ = 'b';
49a9f4f67   Yan, Zheng   ceph: always get ...
72
73
  	if (c & CEPH_CAP_GWREXTEND)
  		*s++ = 'a';
a8599bd82   Sage Weil   ceph: capability ...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  	if (c & CEPH_CAP_GLAZYIO)
  		*s++ = 'l';
  	return s;
  }
  
  const char *ceph_cap_string(int caps)
  {
  	int i;
  	char *s;
  	int c;
  
  	spin_lock(&cap_str_lock);
  	i = last_cap_str++;
  	if (last_cap_str == MAX_CAP_STR)
  		last_cap_str = 0;
  	spin_unlock(&cap_str_lock);
  
  	s = cap_str[i];
  
  	if (caps & CEPH_CAP_PIN)
  		*s++ = 'p';
  
  	c = (caps >> CEPH_CAP_SAUTH) & 3;
  	if (c) {
  		*s++ = 'A';
  		s = gcap_string(s, c);
  	}
  
  	c = (caps >> CEPH_CAP_SLINK) & 3;
  	if (c) {
  		*s++ = 'L';
  		s = gcap_string(s, c);
  	}
  
  	c = (caps >> CEPH_CAP_SXATTR) & 3;
  	if (c) {
  		*s++ = 'X';
  		s = gcap_string(s, c);
  	}
  
  	c = caps >> CEPH_CAP_SFILE;
  	if (c) {
  		*s++ = 'F';
  		s = gcap_string(s, c);
  	}
  
  	if (s == cap_str[i])
  		*s++ = '-';
  	*s = 0;
  	return cap_str[i];
  }
37151668b   Yehuda Sadeh   ceph: do caps acc...
125
  void ceph_caps_init(struct ceph_mds_client *mdsc)
a8599bd82   Sage Weil   ceph: capability ...
126
  {
37151668b   Yehuda Sadeh   ceph: do caps acc...
127
128
  	INIT_LIST_HEAD(&mdsc->caps_list);
  	spin_lock_init(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
129
  }
37151668b   Yehuda Sadeh   ceph: do caps acc...
130
  void ceph_caps_finalize(struct ceph_mds_client *mdsc)
a8599bd82   Sage Weil   ceph: capability ...
131
132
  {
  	struct ceph_cap *cap;
37151668b   Yehuda Sadeh   ceph: do caps acc...
133
134
135
136
  	spin_lock(&mdsc->caps_list_lock);
  	while (!list_empty(&mdsc->caps_list)) {
  		cap = list_first_entry(&mdsc->caps_list,
  				       struct ceph_cap, caps_item);
a8599bd82   Sage Weil   ceph: capability ...
137
138
139
  		list_del(&cap->caps_item);
  		kmem_cache_free(ceph_cap_cachep, cap);
  	}
37151668b   Yehuda Sadeh   ceph: do caps acc...
140
141
142
143
144
145
  	mdsc->caps_total_count = 0;
  	mdsc->caps_avail_count = 0;
  	mdsc->caps_use_count = 0;
  	mdsc->caps_reserve_count = 0;
  	mdsc->caps_min_count = 0;
  	spin_unlock(&mdsc->caps_list_lock);
85ccce43a   Sage Weil   ceph: clean up re...
146
  }
fe33032da   Yan, Zheng   ceph: add mount o...
147
148
  void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc,
  			      struct ceph_mount_options *fsopt)
85ccce43a   Sage Weil   ceph: clean up re...
149
  {
37151668b   Yehuda Sadeh   ceph: do caps acc...
150
  	spin_lock(&mdsc->caps_list_lock);
fe33032da   Yan, Zheng   ceph: add mount o...
151
152
153
154
155
156
157
  	mdsc->caps_min_count = fsopt->max_readdir;
  	if (mdsc->caps_min_count < 1024)
  		mdsc->caps_min_count = 1024;
  	mdsc->caps_use_max = fsopt->caps_max;
  	if (mdsc->caps_use_max > 0 &&
  	    mdsc->caps_use_max < mdsc->caps_min_count)
  		mdsc->caps_use_max = mdsc->caps_min_count;
37151668b   Yehuda Sadeh   ceph: do caps acc...
158
  	spin_unlock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
159
  }
7bf8f736c   Chengguang Xu   ceph: refactor ce...
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
  {
  	struct ceph_cap *cap;
  	int i;
  
  	if (nr_caps) {
  		BUG_ON(mdsc->caps_reserve_count < nr_caps);
  		mdsc->caps_reserve_count -= nr_caps;
  		if (mdsc->caps_avail_count >=
  		    mdsc->caps_reserve_count + mdsc->caps_min_count) {
  			mdsc->caps_total_count -= nr_caps;
  			for (i = 0; i < nr_caps; i++) {
  				cap = list_first_entry(&mdsc->caps_list,
  					struct ceph_cap, caps_item);
  				list_del(&cap->caps_item);
  				kmem_cache_free(ceph_cap_cachep, cap);
  			}
  		} else {
  			mdsc->caps_avail_count += nr_caps;
  		}
  
  		dout("%s: caps %d = %d used + %d resv + %d avail
  ",
  		     __func__,
  		     mdsc->caps_total_count, mdsc->caps_use_count,
  		     mdsc->caps_reserve_count, mdsc->caps_avail_count);
  		BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  						 mdsc->caps_reserve_count +
  						 mdsc->caps_avail_count);
  	}
  }
e30ee5812   Zhi Zhang   ceph: try to allo...
191
192
193
194
  /*
   * Called under mdsc->mutex.
   */
  int ceph_reserve_caps(struct ceph_mds_client *mdsc,
37151668b   Yehuda Sadeh   ceph: do caps acc...
195
  		      struct ceph_cap_reservation *ctx, int need)
a8599bd82   Sage Weil   ceph: capability ...
196
  {
e30ee5812   Zhi Zhang   ceph: try to allo...
197
  	int i, j;
a8599bd82   Sage Weil   ceph: capability ...
198
199
200
  	struct ceph_cap *cap;
  	int have;
  	int alloc = 0;
e30ee5812   Zhi Zhang   ceph: try to allo...
201
  	int max_caps;
e5bc08d09   Chengguang Xu   ceph: refactor er...
202
  	int err = 0;
e30ee5812   Zhi Zhang   ceph: try to allo...
203
204
  	bool trimmed = false;
  	struct ceph_mds_session *s;
a8599bd82   Sage Weil   ceph: capability ...
205
  	LIST_HEAD(newcaps);
a8599bd82   Sage Weil   ceph: capability ...
206
207
208
209
210
  
  	dout("reserve caps ctx=%p need=%d
  ", ctx, need);
  
  	/* first reserve any caps that are already allocated */
37151668b   Yehuda Sadeh   ceph: do caps acc...
211
212
  	spin_lock(&mdsc->caps_list_lock);
  	if (mdsc->caps_avail_count >= need)
a8599bd82   Sage Weil   ceph: capability ...
213
214
  		have = need;
  	else
37151668b   Yehuda Sadeh   ceph: do caps acc...
215
216
217
218
219
220
221
  		have = mdsc->caps_avail_count;
  	mdsc->caps_avail_count -= have;
  	mdsc->caps_reserve_count += have;
  	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  					 mdsc->caps_reserve_count +
  					 mdsc->caps_avail_count);
  	spin_unlock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
222

79cd674ae   Chengguang Xu   ceph: optimizing ...
223
  	for (i = have; i < need; ) {
a8599bd82   Sage Weil   ceph: capability ...
224
  		cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
79cd674ae   Chengguang Xu   ceph: optimizing ...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  		if (cap) {
  			list_add(&cap->caps_item, &newcaps);
  			alloc++;
  			i++;
  			continue;
  		}
  
  		if (!trimmed) {
  			for (j = 0; j < mdsc->max_sessions; j++) {
  				s = __ceph_lookup_mds_session(mdsc, j);
  				if (!s)
  					continue;
  				mutex_unlock(&mdsc->mutex);
  
  				mutex_lock(&s->s_mutex);
  				max_caps = s->s_nr_caps - (need - i);
  				ceph_trim_caps(mdsc, s, max_caps);
  				mutex_unlock(&s->s_mutex);
  
  				ceph_put_mds_session(s);
  				mutex_lock(&mdsc->mutex);
e30ee5812   Zhi Zhang   ceph: try to allo...
246
  			}
79cd674ae   Chengguang Xu   ceph: optimizing ...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  			trimmed = true;
  
  			spin_lock(&mdsc->caps_list_lock);
  			if (mdsc->caps_avail_count) {
  				int more_have;
  				if (mdsc->caps_avail_count >= need - i)
  					more_have = need - i;
  				else
  					more_have = mdsc->caps_avail_count;
  
  				i += more_have;
  				have += more_have;
  				mdsc->caps_avail_count -= more_have;
  				mdsc->caps_reserve_count += more_have;
  
  			}
  			spin_unlock(&mdsc->caps_list_lock);
  
  			continue;
e30ee5812   Zhi Zhang   ceph: try to allo...
266
  		}
79cd674ae   Chengguang Xu   ceph: optimizing ...
267
268
269
270
  
  		pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d
  ",
  			ctx, need, have + alloc);
e5bc08d09   Chengguang Xu   ceph: refactor er...
271
272
273
274
275
276
277
  		err = -ENOMEM;
  		break;
  	}
  
  	if (!err) {
  		BUG_ON(have + alloc != need);
  		ctx->count = need;
fe33032da   Yan, Zheng   ceph: add mount o...
278
  		ctx->used = 0;
a8599bd82   Sage Weil   ceph: capability ...
279
  	}
a8599bd82   Sage Weil   ceph: capability ...
280

37151668b   Yehuda Sadeh   ceph: do caps acc...
281
282
283
284
  	spin_lock(&mdsc->caps_list_lock);
  	mdsc->caps_total_count += alloc;
  	mdsc->caps_reserve_count += alloc;
  	list_splice(&newcaps, &mdsc->caps_list);
a8599bd82   Sage Weil   ceph: capability ...
285

37151668b   Yehuda Sadeh   ceph: do caps acc...
286
287
288
  	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  					 mdsc->caps_reserve_count +
  					 mdsc->caps_avail_count);
e5bc08d09   Chengguang Xu   ceph: refactor er...
289
290
291
  
  	if (err)
  		__ceph_unreserve_caps(mdsc, have + alloc);
37151668b   Yehuda Sadeh   ceph: do caps acc...
292
  	spin_unlock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
293

a8599bd82   Sage Weil   ceph: capability ...
294
295
  	dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail
  ",
37151668b   Yehuda Sadeh   ceph: do caps acc...
296
297
  	     ctx, mdsc->caps_total_count, mdsc->caps_use_count,
  	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
e5bc08d09   Chengguang Xu   ceph: refactor er...
298
  	return err;
a8599bd82   Sage Weil   ceph: capability ...
299
  }
7bf8f736c   Chengguang Xu   ceph: refactor ce...
300
  void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
fe33032da   Yan, Zheng   ceph: add mount o...
301
  			 struct ceph_cap_reservation *ctx)
a8599bd82   Sage Weil   ceph: capability ...
302
  {
fe33032da   Yan, Zheng   ceph: add mount o...
303
304
305
  	bool reclaim = false;
  	if (!ctx->count)
  		return;
a8599bd82   Sage Weil   ceph: capability ...
306
307
  	dout("unreserve caps ctx=%p count=%d
  ", ctx, ctx->count);
7bf8f736c   Chengguang Xu   ceph: refactor ce...
308
309
310
  	spin_lock(&mdsc->caps_list_lock);
  	__ceph_unreserve_caps(mdsc, ctx->count);
  	ctx->count = 0;
fe33032da   Yan, Zheng   ceph: add mount o...
311
312
313
314
  
  	if (mdsc->caps_use_max > 0 &&
  	    mdsc->caps_use_count > mdsc->caps_use_max)
  		reclaim = true;
7bf8f736c   Chengguang Xu   ceph: refactor ce...
315
  	spin_unlock(&mdsc->caps_list_lock);
fe33032da   Yan, Zheng   ceph: add mount o...
316
317
318
  
  	if (reclaim)
  		ceph_reclaim_caps_nr(mdsc, ctx->used);
a8599bd82   Sage Weil   ceph: capability ...
319
  }
d9df27835   Yan, Zheng   ceph: pre-allocat...
320
321
  struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
  			      struct ceph_cap_reservation *ctx)
a8599bd82   Sage Weil   ceph: capability ...
322
323
324
325
  {
  	struct ceph_cap *cap = NULL;
  
  	/* temporary, until we do something about cap import/export */
443b3760a   Sage Weil   ceph: fix caps us...
326
327
328
  	if (!ctx) {
  		cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
  		if (cap) {
4d1d0534f   Yan, Zheng   ceph: Hold caps_l...
329
  			spin_lock(&mdsc->caps_list_lock);
37151668b   Yehuda Sadeh   ceph: do caps acc...
330
331
  			mdsc->caps_use_count++;
  			mdsc->caps_total_count++;
4d1d0534f   Yan, Zheng   ceph: Hold caps_l...
332
  			spin_unlock(&mdsc->caps_list_lock);
e327ce068   Chengguang Xu   ceph: optimizing ...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
  		} else {
  			spin_lock(&mdsc->caps_list_lock);
  			if (mdsc->caps_avail_count) {
  				BUG_ON(list_empty(&mdsc->caps_list));
  
  				mdsc->caps_avail_count--;
  				mdsc->caps_use_count++;
  				cap = list_first_entry(&mdsc->caps_list,
  						struct ceph_cap, caps_item);
  				list_del(&cap->caps_item);
  
  				BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  				       mdsc->caps_reserve_count + mdsc->caps_avail_count);
  			}
  			spin_unlock(&mdsc->caps_list_lock);
443b3760a   Sage Weil   ceph: fix caps us...
348
  		}
e327ce068   Chengguang Xu   ceph: optimizing ...
349

443b3760a   Sage Weil   ceph: fix caps us...
350
351
  		return cap;
  	}
a8599bd82   Sage Weil   ceph: capability ...
352

37151668b   Yehuda Sadeh   ceph: do caps acc...
353
  	spin_lock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
354
355
  	dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail
  ",
37151668b   Yehuda Sadeh   ceph: do caps acc...
356
357
  	     ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
  	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
a8599bd82   Sage Weil   ceph: capability ...
358
  	BUG_ON(!ctx->count);
37151668b   Yehuda Sadeh   ceph: do caps acc...
359
360
  	BUG_ON(ctx->count > mdsc->caps_reserve_count);
  	BUG_ON(list_empty(&mdsc->caps_list));
a8599bd82   Sage Weil   ceph: capability ...
361
362
  
  	ctx->count--;
fe33032da   Yan, Zheng   ceph: add mount o...
363
  	ctx->used++;
37151668b   Yehuda Sadeh   ceph: do caps acc...
364
365
  	mdsc->caps_reserve_count--;
  	mdsc->caps_use_count++;
a8599bd82   Sage Weil   ceph: capability ...
366

37151668b   Yehuda Sadeh   ceph: do caps acc...
367
  	cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
a8599bd82   Sage Weil   ceph: capability ...
368
  	list_del(&cap->caps_item);
37151668b   Yehuda Sadeh   ceph: do caps acc...
369
370
371
  	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  	       mdsc->caps_reserve_count + mdsc->caps_avail_count);
  	spin_unlock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
372
373
  	return cap;
  }
37151668b   Yehuda Sadeh   ceph: do caps acc...
374
  void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
a8599bd82   Sage Weil   ceph: capability ...
375
  {
37151668b   Yehuda Sadeh   ceph: do caps acc...
376
  	spin_lock(&mdsc->caps_list_lock);
7c1332b8c   Sage Weil   ceph: fix iterate...
377
378
  	dout("put_cap %p %d = %d used + %d resv + %d avail
  ",
37151668b   Yehuda Sadeh   ceph: do caps acc...
379
380
381
  	     cap, mdsc->caps_total_count, mdsc->caps_use_count,
  	     mdsc->caps_reserve_count, mdsc->caps_avail_count);
  	mdsc->caps_use_count--;
a8599bd82   Sage Weil   ceph: capability ...
382
  	/*
85ccce43a   Sage Weil   ceph: clean up re...
383
384
  	 * Keep some preallocated caps around (ceph_min_count), to
  	 * avoid lots of free/alloc churn.
a8599bd82   Sage Weil   ceph: capability ...
385
  	 */
37151668b   Yehuda Sadeh   ceph: do caps acc...
386
387
388
  	if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
  				      mdsc->caps_min_count) {
  		mdsc->caps_total_count--;
a8599bd82   Sage Weil   ceph: capability ...
389
390
  		kmem_cache_free(ceph_cap_cachep, cap);
  	} else {
37151668b   Yehuda Sadeh   ceph: do caps acc...
391
392
  		mdsc->caps_avail_count++;
  		list_add(&cap->caps_item, &mdsc->caps_list);
a8599bd82   Sage Weil   ceph: capability ...
393
  	}
37151668b   Yehuda Sadeh   ceph: do caps acc...
394
395
396
  	BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
  	       mdsc->caps_reserve_count + mdsc->caps_avail_count);
  	spin_unlock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
397
  }
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
398
  void ceph_reservation_status(struct ceph_fs_client *fsc,
85ccce43a   Sage Weil   ceph: clean up re...
399
400
  			     int *total, int *avail, int *used, int *reserved,
  			     int *min)
a8599bd82   Sage Weil   ceph: capability ...
401
  {
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
402
  	struct ceph_mds_client *mdsc = fsc->mdsc;
37151668b   Yehuda Sadeh   ceph: do caps acc...
403

b884014a9   Chengguang Xu   ceph: adding prot...
404
  	spin_lock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
405
  	if (total)
37151668b   Yehuda Sadeh   ceph: do caps acc...
406
  		*total = mdsc->caps_total_count;
a8599bd82   Sage Weil   ceph: capability ...
407
  	if (avail)
37151668b   Yehuda Sadeh   ceph: do caps acc...
408
  		*avail = mdsc->caps_avail_count;
a8599bd82   Sage Weil   ceph: capability ...
409
  	if (used)
37151668b   Yehuda Sadeh   ceph: do caps acc...
410
  		*used = mdsc->caps_use_count;
a8599bd82   Sage Weil   ceph: capability ...
411
  	if (reserved)
37151668b   Yehuda Sadeh   ceph: do caps acc...
412
  		*reserved = mdsc->caps_reserve_count;
85ccce43a   Sage Weil   ceph: clean up re...
413
  	if (min)
37151668b   Yehuda Sadeh   ceph: do caps acc...
414
  		*min = mdsc->caps_min_count;
b884014a9   Chengguang Xu   ceph: adding prot...
415
416
  
  	spin_unlock(&mdsc->caps_list_lock);
a8599bd82   Sage Weil   ceph: capability ...
417
418
419
420
421
  }
  
  /*
   * Find ceph_cap for given mds, if any.
   *
be655596b   Sage Weil   ceph: use i_ceph_...
422
   * Called with i_ceph_lock held.
a8599bd82   Sage Weil   ceph: capability ...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
   */
  static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  {
  	struct ceph_cap *cap;
  	struct rb_node *n = ci->i_caps.rb_node;
  
  	while (n) {
  		cap = rb_entry(n, struct ceph_cap, ci_node);
  		if (mds < cap->mds)
  			n = n->rb_left;
  		else if (mds > cap->mds)
  			n = n->rb_right;
  		else
  			return cap;
  	}
  	return NULL;
  }
2bc50259f   Greg Farnum   ceph: add ceph_ge...
440
441
442
  struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
  {
  	struct ceph_cap *cap;
be655596b   Sage Weil   ceph: use i_ceph_...
443
  	spin_lock(&ci->i_ceph_lock);
2bc50259f   Greg Farnum   ceph: add ceph_ge...
444
  	cap = __get_cap_for_mds(ci, mds);
be655596b   Sage Weil   ceph: use i_ceph_...
445
  	spin_unlock(&ci->i_ceph_lock);
2bc50259f   Greg Farnum   ceph: add ceph_ge...
446
447
  	return cap;
  }
a8599bd82   Sage Weil   ceph: capability ...
448
  /*
be655596b   Sage Weil   ceph: use i_ceph_...
449
   * Called under i_ceph_lock.
a8599bd82   Sage Weil   ceph: capability ...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
   */
  static void __insert_cap_node(struct ceph_inode_info *ci,
  			      struct ceph_cap *new)
  {
  	struct rb_node **p = &ci->i_caps.rb_node;
  	struct rb_node *parent = NULL;
  	struct ceph_cap *cap = NULL;
  
  	while (*p) {
  		parent = *p;
  		cap = rb_entry(parent, struct ceph_cap, ci_node);
  		if (new->mds < cap->mds)
  			p = &(*p)->rb_left;
  		else if (new->mds > cap->mds)
  			p = &(*p)->rb_right;
  		else
  			BUG();
  	}
  
  	rb_link_node(&new->ci_node, parent, p);
  	rb_insert_color(&new->ci_node, &ci->i_caps);
  }
  
  /*
   * (re)set cap hold timeouts, which control the delayed release
   * of unused caps back to the MDS.  Should be called on cap use.
   */
  static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
  			       struct ceph_inode_info *ci)
  {
fe33032da   Yan, Zheng   ceph: add mount o...
480
  	struct ceph_mount_options *opt = mdsc->fsc->mount_options;
a8599bd82   Sage Weil   ceph: capability ...
481
  	ci->i_hold_caps_max = round_jiffies(jiffies +
fe33032da   Yan, Zheng   ceph: add mount o...
482
  					    opt->caps_wanted_delay_max * HZ);
a0d93e327   Yan, Zheng   ceph: remove dela...
483
484
485
  	dout("__cap_set_timeouts %p %lu
  ", &ci->vfs_inode,
  	     ci->i_hold_caps_max - jiffies);
a8599bd82   Sage Weil   ceph: capability ...
486
487
488
489
490
491
492
  }
  
  /*
   * (Re)queue cap at the end of the delayed cap release list.
   *
   * If I_FLUSH is set, leave the inode at the front of the list.
   *
be655596b   Sage Weil   ceph: use i_ceph_...
493
   * Caller holds i_ceph_lock
a8599bd82   Sage Weil   ceph: capability ...
494
495
496
   *    -> we take mdsc->cap_delay_lock
   */
  static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
a0d93e327   Yan, Zheng   ceph: remove dela...
497
  				struct ceph_inode_info *ci)
a8599bd82   Sage Weil   ceph: capability ...
498
  {
891f3f5a6   Jeff Layton   ceph: add infrast...
499
500
  	dout("__cap_delay_requeue %p flags 0x%lx at %lu
  ", &ci->vfs_inode,
a8599bd82   Sage Weil   ceph: capability ...
501
502
503
504
505
506
507
508
  	     ci->i_ceph_flags, ci->i_hold_caps_max);
  	if (!mdsc->stopping) {
  		spin_lock(&mdsc->cap_delay_lock);
  		if (!list_empty(&ci->i_cap_delay_list)) {
  			if (ci->i_ceph_flags & CEPH_I_FLUSH)
  				goto no_change;
  			list_del_init(&ci->i_cap_delay_list);
  		}
a0d93e327   Yan, Zheng   ceph: remove dela...
509
  		__cap_set_timeouts(mdsc, ci);
a8599bd82   Sage Weil   ceph: capability ...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  		list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  no_change:
  		spin_unlock(&mdsc->cap_delay_lock);
  	}
  }
  
  /*
   * Queue an inode for immediate writeback.  Mark inode with I_FLUSH,
   * indicating we should send a cap message to flush dirty metadata
   * asap, and move to the front of the delayed cap list.
   */
  static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
  				      struct ceph_inode_info *ci)
  {
  	dout("__cap_delay_requeue_front %p
  ", &ci->vfs_inode);
  	spin_lock(&mdsc->cap_delay_lock);
  	ci->i_ceph_flags |= CEPH_I_FLUSH;
  	if (!list_empty(&ci->i_cap_delay_list))
  		list_del_init(&ci->i_cap_delay_list);
  	list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
  	spin_unlock(&mdsc->cap_delay_lock);
  }
  
  /*
   * Cancel delayed work on cap.
   *
be655596b   Sage Weil   ceph: use i_ceph_...
537
   * Caller must hold i_ceph_lock.
a8599bd82   Sage Weil   ceph: capability ...
538
539
540
541
542
543
544
545
546
547
548
549
   */
  static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
  			       struct ceph_inode_info *ci)
  {
  	dout("__cap_delay_cancel %p
  ", &ci->vfs_inode);
  	if (list_empty(&ci->i_cap_delay_list))
  		return;
  	spin_lock(&mdsc->cap_delay_lock);
  	list_del_init(&ci->i_cap_delay_list);
  	spin_unlock(&mdsc->cap_delay_lock);
  }
785892fe8   Jeff Layton   ceph: cache layou...
550
  /* Common issue checks for add_cap, handle_cap_grant. */
a8599bd82   Sage Weil   ceph: capability ...
551
552
553
554
  static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
  			      unsigned issued)
  {
  	unsigned had = __ceph_caps_issued(ci, NULL);
785892fe8   Jeff Layton   ceph: cache layou...
555
  	lockdep_assert_held(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
556
557
558
559
  	/*
  	 * Each time we receive FILE_CACHE anew, we increment
  	 * i_rdcache_gen.
  	 */
525d15e8e   Yan, Zheng   ceph: check inode...
560
561
  	if (S_ISREG(ci->vfs_inode.i_mode) &&
  	    (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
99ccbd229   Milosz Tanski   ceph: use fscache...
562
  	    (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
a8599bd82   Sage Weil   ceph: capability ...
563
  		ci->i_rdcache_gen++;
99ccbd229   Milosz Tanski   ceph: use fscache...
564
  	}
a8599bd82   Sage Weil   ceph: capability ...
565
566
  
  	/*
15b51bd6b   Yan, Zheng   ceph: stop on-goi...
567
568
569
570
  	 * If FILE_SHARED is newly issued, mark dir not complete. We don't
  	 * know what happened to this directory while we didn't have the cap.
  	 * If FILE_SHARED is being revoked, also mark dir not complete. It
  	 * stops on-going cached readdir.
a8599bd82   Sage Weil   ceph: capability ...
571
  	 */
15b51bd6b   Yan, Zheng   ceph: stop on-goi...
572
573
  	if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
  		if (issued & CEPH_CAP_FILE_SHARED)
97aeb6bf9   Yan, Zheng   ceph: use atomic_...
574
  			atomic_inc(&ci->i_shared_gen);
a8673d61a   Yan, Zheng   ceph: use I_COMPL...
575
576
577
  		if (S_ISDIR(ci->vfs_inode.i_mode)) {
  			dout(" marking %p NOT complete
  ", &ci->vfs_inode);
2f276c511   Yan, Zheng   ceph: use i_relea...
578
  			__ceph_dir_clear_complete(ci);
a8673d61a   Yan, Zheng   ceph: use I_COMPL...
579
  		}
a8599bd82   Sage Weil   ceph: capability ...
580
  	}
785892fe8   Jeff Layton   ceph: cache layou...
581
582
583
584
585
586
587
  
  	/* Wipe saved layout if we're losing DIR_CREATE caps */
  	if (S_ISDIR(ci->vfs_inode.i_mode) && (had & CEPH_CAP_DIR_CREATE) &&
  		!(issued & CEPH_CAP_DIR_CREATE)) {
  	     ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
  	     memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
  	}
a8599bd82   Sage Weil   ceph: capability ...
588
  }
1cf03a68e   Jeff Layton   ceph: convert mds...
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  /**
   * change_auth_cap_ses - move inode to appropriate lists when auth caps change
   * @ci: inode to be moved
   * @session: new auth caps session
   */
  static void change_auth_cap_ses(struct ceph_inode_info *ci,
  				struct ceph_mds_session *session)
  {
  	lockdep_assert_held(&ci->i_ceph_lock);
  
  	if (list_empty(&ci->i_dirty_item) && list_empty(&ci->i_flushing_item))
  		return;
  
  	spin_lock(&session->s_mdsc->cap_dirty_lock);
  	if (!list_empty(&ci->i_dirty_item))
  		list_move(&ci->i_dirty_item, &session->s_cap_dirty);
  	if (!list_empty(&ci->i_flushing_item))
  		list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  	spin_unlock(&session->s_mdsc->cap_dirty_lock);
  }
a8599bd82   Sage Weil   ceph: capability ...
609
610
611
  /*
   * Add a capability under the given MDS session.
   *
354c63a00   Jeff Layton   ceph: fix comment...
612
   * Caller should hold session snap_rwsem (read) and ci->i_ceph_lock
a8599bd82   Sage Weil   ceph: capability ...
613
614
615
616
617
   *
   * @fmode is the open file mode, if we are opening a file, otherwise
   * it is < 0.  (This is so we can atomically add the cap and add an
   * open file reference to it.)
   */
d9df27835   Yan, Zheng   ceph: pre-allocat...
618
619
  void ceph_add_cap(struct inode *inode,
  		  struct ceph_mds_session *session, u64 cap_id,
135e671e5   Yan, Zheng   ceph: simplify ca...
620
  		  unsigned issued, unsigned wanted,
d9df27835   Yan, Zheng   ceph: pre-allocat...
621
622
  		  unsigned seq, unsigned mseq, u64 realmino, int flags,
  		  struct ceph_cap **new_cap)
a8599bd82   Sage Weil   ceph: capability ...
623
  {
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
624
  	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
a8599bd82   Sage Weil   ceph: capability ...
625
  	struct ceph_inode_info *ci = ceph_inode(inode);
a8599bd82   Sage Weil   ceph: capability ...
626
627
628
  	struct ceph_cap *cap;
  	int mds = session->s_mds;
  	int actual_wanted;
606d10232   Jeff Layton   ceph: fetch cap_g...
629
  	u32 gen;
a8599bd82   Sage Weil   ceph: capability ...
630

354c63a00   Jeff Layton   ceph: fix comment...
631
  	lockdep_assert_held(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
632
633
634
  	dout("add_cap %p mds%d cap %llx %s seq %d
  ", inode,
  	     session->s_mds, cap_id, ceph_cap_string(issued), seq);
606d10232   Jeff Layton   ceph: fetch cap_g...
635
636
637
  	spin_lock(&session->s_gen_ttl_lock);
  	gen = session->s_cap_gen;
  	spin_unlock(&session->s_gen_ttl_lock);
a8599bd82   Sage Weil   ceph: capability ...
638
639
  	cap = __get_cap_for_mds(ci, mds);
  	if (!cap) {
d9df27835   Yan, Zheng   ceph: pre-allocat...
640
641
  		cap = *new_cap;
  		*new_cap = NULL;
a8599bd82   Sage Weil   ceph: capability ...
642
643
644
645
646
  
  		cap->issued = 0;
  		cap->implemented = 0;
  		cap->mds = mds;
  		cap->mds_wanted = 0;
964266cce   Yan, Zheng   ceph: set mds_wan...
647
  		cap->mseq = 0;
a8599bd82   Sage Weil   ceph: capability ...
648
649
650
  
  		cap->ci = ci;
  		__insert_cap_node(ci, cap);
a8599bd82   Sage Weil   ceph: capability ...
651
652
653
654
655
  		/* add to session cap list */
  		cap->session = session;
  		spin_lock(&session->s_cap_lock);
  		list_add_tail(&cap->session_caps, &session->s_caps);
  		session->s_nr_caps++;
4f1d756de   Xiubo Li   ceph: add global ...
656
  		atomic64_inc(&mdsc->metric.total_caps);
a8599bd82   Sage Weil   ceph: capability ...
657
  		spin_unlock(&session->s_cap_lock);
11df2dfb6   Yan, Zheng   ceph: add importe...
658
  	} else {
32f6511a6   Yan, Zheng   ceph: touch exist...
659
660
661
  		spin_lock(&session->s_cap_lock);
  		list_move_tail(&cap->session_caps, &session->s_caps);
  		spin_unlock(&session->s_cap_lock);
606d10232   Jeff Layton   ceph: fetch cap_g...
662
  		if (cap->cap_gen < gen)
d2f8bb27c   Yan, Zheng   ceph: update want...
663
  			cap->issued = cap->implemented = CEPH_CAP_PIN;
11df2dfb6   Yan, Zheng   ceph: add importe...
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
  		/*
  		 * auth mds of the inode changed. we received the cap export
  		 * message, but still haven't received the cap import message.
  		 * handle_cap_export() updated the new auth MDS' cap.
  		 *
  		 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
  		 * a message that was send before the cap import message. So
  		 * don't remove caps.
  		 */
  		if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  			WARN_ON(cap != ci->i_auth_cap);
  			WARN_ON(cap->cap_id != cap_id);
  			seq = cap->seq;
  			mseq = cap->mseq;
  			issued |= cap->issued;
  			flags |= CEPH_CAP_FLAG_AUTH;
  		}
  	}
a8599bd82   Sage Weil   ceph: capability ...
682

7d9c9193b   Yan, Zheng   ceph: fix incorre...
683
684
685
  	if (!ci->i_snap_realm ||
  	    ((flags & CEPH_CAP_FLAG_AUTH) &&
  	     realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
a8599bd82   Sage Weil   ceph: capability ...
686
687
688
689
690
691
  		/*
  		 * add this inode to the appropriate snap realm
  		 */
  		struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
  							       realmino);
  		if (realm) {
7d9c9193b   Yan, Zheng   ceph: fix incorre...
692
693
694
695
696
697
  			struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
  			if (oldrealm) {
  				spin_lock(&oldrealm->inodes_with_caps_lock);
  				list_del_init(&ci->i_snap_realm_item);
  				spin_unlock(&oldrealm->inodes_with_caps_lock);
  			}
a8599bd82   Sage Weil   ceph: capability ...
698
  			spin_lock(&realm->inodes_with_caps_lock);
a8599bd82   Sage Weil   ceph: capability ...
699
700
  			list_add(&ci->i_snap_realm_item,
  				 &realm->inodes_with_caps);
e3161f17d   Luis Henriques   ceph: quota: cach...
701
702
703
  			ci->i_snap_realm = realm;
  			if (realm->ino == ci->i_vino.ino)
  				realm->inode = inode;
a8599bd82   Sage Weil   ceph: capability ...
704
  			spin_unlock(&realm->inodes_with_caps_lock);
7d9c9193b   Yan, Zheng   ceph: fix incorre...
705
706
707
  
  			if (oldrealm)
  				ceph_put_snap_realm(mdsc, oldrealm);
a8599bd82   Sage Weil   ceph: capability ...
708
709
710
711
  		} else {
  			pr_err("ceph_add_cap: couldn't find snap realm %llx
  ",
  			       realmino);
b8cd07e78   Sage Weil   ceph: warn on mis...
712
  			WARN_ON(!realm);
a8599bd82   Sage Weil   ceph: capability ...
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
  		}
  	}
  
  	__check_cap_issue(ci, cap, issued);
  
  	/*
  	 * If we are issued caps we don't want, or the mds' wanted
  	 * value appears to be off, queue a check so we'll release
  	 * later and/or update the mds wanted value.
  	 */
  	actual_wanted = __ceph_caps_wanted(ci);
  	if ((wanted & ~actual_wanted) ||
  	    (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
  		dout(" issued %s, mds wanted %s, actual %s, queueing
  ",
  		     ceph_cap_string(issued), ceph_cap_string(wanted),
  		     ceph_cap_string(actual_wanted));
a0d93e327   Yan, Zheng   ceph: remove dela...
730
  		__cap_delay_requeue(mdsc, ci);
a8599bd82   Sage Weil   ceph: capability ...
731
  	}
b8c2f3ae2   Yan, Zheng   ceph: check migra...
732
  	if (flags & CEPH_CAP_FLAG_AUTH) {
d37b1d994   Markus Elfring   ceph: adjust 36 c...
733
  		if (!ci->i_auth_cap ||
d9ffc4f77   Yan, Zheng   ceph: set mds_wan...
734
  		    ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
1cf03a68e   Jeff Layton   ceph: convert mds...
735
736
737
  			if (ci->i_auth_cap &&
  			    ci->i_auth_cap->session != cap->session)
  				change_auth_cap_ses(ci, cap->session);
b8c2f3ae2   Yan, Zheng   ceph: check migra...
738
  			ci->i_auth_cap = cap;
d9ffc4f77   Yan, Zheng   ceph: set mds_wan...
739
740
  			cap->mds_wanted = wanted;
  		}
11df2dfb6   Yan, Zheng   ceph: add importe...
741
742
  	} else {
  		WARN_ON(ci->i_auth_cap == cap);
8a92a119b   Yan, Zheng   ceph: move dirty ...
743
  	}
a8599bd82   Sage Weil   ceph: capability ...
744
745
746
747
748
749
750
751
  
  	dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d
  ",
  	     inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
  	     ceph_cap_string(issued|cap->issued), seq, mds);
  	cap->cap_id = cap_id;
  	cap->issued = issued;
  	cap->implemented |= issued;
d1b87809f   Yan, Zheng   ceph: use ceph_se...
752
  	if (ceph_seq_cmp(mseq, cap->mseq) > 0)
964266cce   Yan, Zheng   ceph: set mds_wan...
753
754
755
  		cap->mds_wanted = wanted;
  	else
  		cap->mds_wanted |= wanted;
a8599bd82   Sage Weil   ceph: capability ...
756
757
758
  	cap->seq = seq;
  	cap->issue_seq = seq;
  	cap->mseq = mseq;
606d10232   Jeff Layton   ceph: fetch cap_g...
759
  	cap->cap_gen = gen;
a8599bd82   Sage Weil   ceph: capability ...
760
761
762
763
764
765
766
767
768
769
  }
  
  /*
   * Return true if cap has not timed out and belongs to the current
   * generation of the MDS session (i.e. has not gone 'stale' due to
   * us losing touch with the mds).
   */
  static int __cap_is_valid(struct ceph_cap *cap)
  {
  	unsigned long ttl;
cdac83031   Sage Weil   ceph: remove reco...
770
  	u32 gen;
a8599bd82   Sage Weil   ceph: capability ...
771

d8fb02abd   Alex Elder   ceph: create a ne...
772
  	spin_lock(&cap->session->s_gen_ttl_lock);
a8599bd82   Sage Weil   ceph: capability ...
773
774
  	gen = cap->session->s_cap_gen;
  	ttl = cap->session->s_cap_ttl;
d8fb02abd   Alex Elder   ceph: create a ne...
775
  	spin_unlock(&cap->session->s_gen_ttl_lock);
a8599bd82   Sage Weil   ceph: capability ...
776

685f9a5d1   Sage Weil   ceph: do not conf...
777
  	if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
a8599bd82   Sage Weil   ceph: capability ...
778
779
780
  		dout("__cap_is_valid %p cap %p issued %s "
  		     "but STALE (gen %u vs %u)
  ", &cap->ci->vfs_inode,
685f9a5d1   Sage Weil   ceph: do not conf...
781
  		     cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
a8599bd82   Sage Weil   ceph: capability ...
782
783
784
785
786
787
788
789
790
791
792
793
794
  		return 0;
  	}
  
  	return 1;
  }
  
  /*
   * Return set of valid cap bits issued to us.  Note that caps time
   * out, and may be invalidated in bulk if the client session times out
   * and session->s_cap_gen is bumped.
   */
  int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
  {
d9df27835   Yan, Zheng   ceph: pre-allocat...
795
  	int have = ci->i_snap_caps;
a8599bd82   Sage Weil   ceph: capability ...
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
  	struct ceph_cap *cap;
  	struct rb_node *p;
  
  	if (implemented)
  		*implemented = 0;
  	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  		cap = rb_entry(p, struct ceph_cap, ci_node);
  		if (!__cap_is_valid(cap))
  			continue;
  		dout("__ceph_caps_issued %p cap %p issued %s
  ",
  		     &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
  		have |= cap->issued;
  		if (implemented)
  			*implemented |= cap->implemented;
  	}
b1530f570   Yan, Zheng   ceph: fix cap rev...
812
813
814
815
816
817
818
819
820
  	/*
  	 * exclude caps issued by non-auth MDS, but are been revoking
  	 * by the auth MDS. The non-auth MDS should be revoking/exporting
  	 * these caps, but the message is delayed.
  	 */
  	if (ci->i_auth_cap) {
  		cap = ci->i_auth_cap;
  		have &= ~cap->implemented | cap->issued;
  	}
a8599bd82   Sage Weil   ceph: capability ...
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
  	return have;
  }
  
  /*
   * Get cap bits issued by caps other than @ocap
   */
  int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
  {
  	int have = ci->i_snap_caps;
  	struct ceph_cap *cap;
  	struct rb_node *p;
  
  	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  		cap = rb_entry(p, struct ceph_cap, ci_node);
  		if (cap == ocap)
  			continue;
  		if (!__cap_is_valid(cap))
  			continue;
  		have |= cap->issued;
  	}
  	return have;
  }
  
  /*
   * Move a cap to the end of the LRU (oldest caps at list head, newest
   * at list tail).
   */
  static void __touch_cap(struct ceph_cap *cap)
  {
  	struct ceph_mds_session *s = cap->session;
a8599bd82   Sage Weil   ceph: capability ...
851
  	spin_lock(&s->s_cap_lock);
d37b1d994   Markus Elfring   ceph: adjust 36 c...
852
  	if (!s->s_cap_iterator) {
5dacf0912   Sage Weil   ceph: do not touc...
853
854
855
856
857
858
859
860
861
  		dout("__touch_cap %p cap %p mds%d
  ", &cap->ci->vfs_inode, cap,
  		     s->s_mds);
  		list_move_tail(&cap->session_caps, &s->s_caps);
  	} else {
  		dout("__touch_cap %p cap %p mds%d NOP, iterating over caps
  ",
  		     &cap->ci->vfs_inode, cap, s->s_mds);
  	}
a8599bd82   Sage Weil   ceph: capability ...
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
  	spin_unlock(&s->s_cap_lock);
  }
  
  /*
   * Check if we hold the given mask.  If so, move the cap(s) to the
   * front of their respective LRUs.  (This is the preferred way for
   * callers to check for caps they want.)
   */
  int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
  {
  	struct ceph_cap *cap;
  	struct rb_node *p;
  	int have = ci->i_snap_caps;
  
  	if ((have & mask) == mask) {
ebce3eb2f   Jeff Layton   ceph: fix inode n...
877
878
879
  		dout("__ceph_caps_issued_mask ino 0x%llx snap issued %s"
  		     " (mask %s)
  ", ceph_ino(&ci->vfs_inode),
a8599bd82   Sage Weil   ceph: capability ...
880
881
882
883
884
885
886
887
888
889
  		     ceph_cap_string(have),
  		     ceph_cap_string(mask));
  		return 1;
  	}
  
  	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  		cap = rb_entry(p, struct ceph_cap, ci_node);
  		if (!__cap_is_valid(cap))
  			continue;
  		if ((cap->issued & mask) == mask) {
ebce3eb2f   Jeff Layton   ceph: fix inode n...
890
891
892
  			dout("__ceph_caps_issued_mask ino 0x%llx cap %p issued %s"
  			     " (mask %s)
  ", ceph_ino(&ci->vfs_inode), cap,
a8599bd82   Sage Weil   ceph: capability ...
893
894
895
896
897
898
899
900
901
902
  			     ceph_cap_string(cap->issued),
  			     ceph_cap_string(mask));
  			if (touch)
  				__touch_cap(cap);
  			return 1;
  		}
  
  		/* does a combination of caps satisfy mask? */
  		have |= cap->issued;
  		if ((have & mask) == mask) {
ebce3eb2f   Jeff Layton   ceph: fix inode n...
903
904
905
  			dout("__ceph_caps_issued_mask ino 0x%llx combo issued %s"
  			     " (mask %s)
  ", ceph_ino(&ci->vfs_inode),
a8599bd82   Sage Weil   ceph: capability ...
906
907
908
909
  			     ceph_cap_string(cap->issued),
  			     ceph_cap_string(mask));
  			if (touch) {
  				struct rb_node *q;
25985edce   Lucas De Marchi   Fix common misspe...
910
  				/* touch this + preceding caps */
a8599bd82   Sage Weil   ceph: capability ...
911
912
913
914
915
916
917
  				__touch_cap(cap);
  				for (q = rb_first(&ci->i_caps); q != p;
  				     q = rb_next(q)) {
  					cap = rb_entry(q, struct ceph_cap,
  						       ci_node);
  					if (!__cap_is_valid(cap))
  						continue;
9f8b72b3a   Xiubo Li   ceph: only touch ...
918
919
  					if (cap->issued & mask)
  						__touch_cap(cap);
a8599bd82   Sage Weil   ceph: capability ...
920
921
922
923
924
925
926
927
  				}
  			}
  			return 1;
  		}
  	}
  
  	return 0;
  }
1af16d547   Xiubo Li   ceph: add caps pe...
928
929
930
931
932
933
934
935
936
937
938
939
940
  int __ceph_caps_issued_mask_metric(struct ceph_inode_info *ci, int mask,
  				   int touch)
  {
  	struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
  	int r;
  
  	r = __ceph_caps_issued_mask(ci, mask, touch);
  	if (r)
  		ceph_update_cap_hit(&fsc->mdsc->metric);
  	else
  		ceph_update_cap_mis(&fsc->mdsc->metric);
  	return r;
  }
a8599bd82   Sage Weil   ceph: capability ...
941
942
943
  /*
   * Return true if mask caps are currently being revoked by an MDS.
   */
6ee6b9537   Yan, Zheng   ceph: fix race be...
944
945
  int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
  			       struct ceph_cap *ocap, int mask)
a8599bd82   Sage Weil   ceph: capability ...
946
  {
a8599bd82   Sage Weil   ceph: capability ...
947
948
  	struct ceph_cap *cap;
  	struct rb_node *p;
a8599bd82   Sage Weil   ceph: capability ...
949

a8599bd82   Sage Weil   ceph: capability ...
950
951
  	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  		cap = rb_entry(p, struct ceph_cap, ci_node);
9563f88c1   Yan, Zheng   ceph: fix cache r...
952
  		if (cap != ocap &&
6ee6b9537   Yan, Zheng   ceph: fix race be...
953
954
  		    (cap->implemented & ~cap->issued & mask))
  			return 1;
a8599bd82   Sage Weil   ceph: capability ...
955
  	}
6ee6b9537   Yan, Zheng   ceph: fix race be...
956
957
958
959
960
961
962
963
964
965
  	return 0;
  }
  
  int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
  {
  	struct inode *inode = &ci->vfs_inode;
  	int ret;
  
  	spin_lock(&ci->i_ceph_lock);
  	ret = __ceph_caps_revoking_other(ci, NULL, mask);
be655596b   Sage Weil   ceph: use i_ceph_...
966
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
967
968
969
970
971
972
973
974
975
976
977
978
979
  	dout("ceph_caps_revoking %p %s = %d
  ", inode,
  	     ceph_cap_string(mask), ret);
  	return ret;
  }
  
  int __ceph_caps_used(struct ceph_inode_info *ci)
  {
  	int used = 0;
  	if (ci->i_pin_ref)
  		used |= CEPH_CAP_PIN;
  	if (ci->i_rd_ref)
  		used |= CEPH_CAP_FILE_RD;
fdd4e1583   Yan, Zheng   ceph: rework dcac...
980
  	if (ci->i_rdcache_ref ||
525d15e8e   Yan, Zheng   ceph: check inode...
981
  	    (S_ISREG(ci->vfs_inode.i_mode) &&
fdd4e1583   Yan, Zheng   ceph: rework dcac...
982
  	     ci->vfs_inode.i_data.nrpages))
a8599bd82   Sage Weil   ceph: capability ...
983
984
985
  		used |= CEPH_CAP_FILE_CACHE;
  	if (ci->i_wr_ref)
  		used |= CEPH_CAP_FILE_WR;
d3d0720d4   Henry C Chang   ceph: do not use ...
986
  	if (ci->i_wb_ref || ci->i_wrbuffer_ref)
a8599bd82   Sage Weil   ceph: capability ...
987
  		used |= CEPH_CAP_FILE_BUFFER;
f85122afe   Jeff Layton   ceph: add refcoun...
988
989
  	if (ci->i_fx_ref)
  		used |= CEPH_CAP_FILE_EXCL;
a8599bd82   Sage Weil   ceph: capability ...
990
991
  	return used;
  }
719a2514e   Yan, Zheng   ceph: consider in...
992
  #define FMODE_WAIT_BIAS 1000
a8599bd82   Sage Weil   ceph: capability ...
993
994
995
996
997
  /*
   * wanted, by virtue of open file modes
   */
  int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
  {
719a2514e   Yan, Zheng   ceph: consider in...
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
  	const int PIN_SHIFT = ffs(CEPH_FILE_MODE_PIN);
  	const int RD_SHIFT = ffs(CEPH_FILE_MODE_RD);
  	const int WR_SHIFT = ffs(CEPH_FILE_MODE_WR);
  	const int LAZY_SHIFT = ffs(CEPH_FILE_MODE_LAZY);
  	struct ceph_mount_options *opt =
  		ceph_inode_to_client(&ci->vfs_inode)->mount_options;
  	unsigned long used_cutoff = jiffies - opt->caps_wanted_delay_max * HZ;
  	unsigned long idle_cutoff = jiffies - opt->caps_wanted_delay_min * HZ;
  
  	if (S_ISDIR(ci->vfs_inode.i_mode)) {
  		int want = 0;
  
  		/* use used_cutoff here, to keep dir's wanted caps longer */
  		if (ci->i_nr_by_mode[RD_SHIFT] > 0 ||
  		    time_after(ci->i_last_rd, used_cutoff))
  			want |= CEPH_CAP_ANY_SHARED;
  
  		if (ci->i_nr_by_mode[WR_SHIFT] > 0 ||
  		    time_after(ci->i_last_wr, used_cutoff)) {
  			want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
  			if (opt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
  				want |= CEPH_CAP_ANY_DIR_OPS;
  		}
  
  		if (want || ci->i_nr_by_mode[PIN_SHIFT] > 0)
  			want |= CEPH_CAP_PIN;
  
  		return want;
  	} else {
  		int bits = 0;
  
  		if (ci->i_nr_by_mode[RD_SHIFT] > 0) {
  			if (ci->i_nr_by_mode[RD_SHIFT] >= FMODE_WAIT_BIAS ||
  			    time_after(ci->i_last_rd, used_cutoff))
  				bits |= 1 << RD_SHIFT;
  		} else if (time_after(ci->i_last_rd, idle_cutoff)) {
  			bits |= 1 << RD_SHIFT;
  		}
  
  		if (ci->i_nr_by_mode[WR_SHIFT] > 0) {
  			if (ci->i_nr_by_mode[WR_SHIFT] >= FMODE_WAIT_BIAS ||
  			    time_after(ci->i_last_wr, used_cutoff))
  				bits |= 1 << WR_SHIFT;
  		} else if (time_after(ci->i_last_wr, idle_cutoff)) {
  			bits |= 1 << WR_SHIFT;
  		}
  
  		/* check lazyio only when read/write is wanted */
  		if ((bits & (CEPH_FILE_MODE_RDWR << 1)) &&
  		    ci->i_nr_by_mode[LAZY_SHIFT] > 0)
  			bits |= 1 << LAZY_SHIFT;
  
  		return bits ? ceph_caps_for_mode(bits >> 1) : 0;
774a6a118   Yan, Zheng   ceph: reduce i_nr...
1051
  	}
a8599bd82   Sage Weil   ceph: capability ...
1052
1053
1054
  }
  
  /*
525d15e8e   Yan, Zheng   ceph: check inode...
1055
1056
1057
1058
1059
   * wanted, by virtue of open file modes AND cap refs (buffered/cached data)
   */
  int __ceph_caps_wanted(struct ceph_inode_info *ci)
  {
  	int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci);
a25949b99   Jeff Layton   ceph: cap trackin...
1060
1061
1062
1063
1064
  	if (S_ISDIR(ci->vfs_inode.i_mode)) {
  		/* we want EXCL if holding caps of dir ops */
  		if (w & CEPH_CAP_ANY_DIR_OPS)
  			w |= CEPH_CAP_FILE_EXCL;
  	} else {
525d15e8e   Yan, Zheng   ceph: check inode...
1065
1066
1067
1068
1069
1070
1071
1072
  		/* we want EXCL if dirty data */
  		if (w & CEPH_CAP_FILE_BUFFER)
  			w |= CEPH_CAP_FILE_EXCL;
  	}
  	return w;
  }
  
  /*
a8599bd82   Sage Weil   ceph: capability ...
1073
1074
   * Return caps we have registered with the MDS(s) as 'wanted'.
   */
c1944fedd   Yan, Zheng   ceph: avoid calli...
1075
  int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
a8599bd82   Sage Weil   ceph: capability ...
1076
1077
1078
1079
1080
1081
1082
  {
  	struct ceph_cap *cap;
  	struct rb_node *p;
  	int mds_wanted = 0;
  
  	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
  		cap = rb_entry(p, struct ceph_cap, ci_node);
c1944fedd   Yan, Zheng   ceph: avoid calli...
1083
  		if (check && !__cap_is_valid(cap))
a8599bd82   Sage Weil   ceph: capability ...
1084
  			continue;
a25506045   Yan, Zheng   ceph: make sure w...
1085
1086
1087
1088
  		if (cap == ci->i_auth_cap)
  			mds_wanted |= cap->mds_wanted;
  		else
  			mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
a8599bd82   Sage Weil   ceph: capability ...
1089
1090
1091
  	}
  	return mds_wanted;
  }
9215aeea6   Yan, Zheng   ceph: check inode...
1092
1093
1094
1095
1096
1097
  int ceph_is_any_caps(struct inode *inode)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int ret;
  
  	spin_lock(&ci->i_ceph_lock);
bd84fbcb3   Xiubo Li   ceph: switch to g...
1098
  	ret = __ceph_is_any_real_caps(ci);
9215aeea6   Yan, Zheng   ceph: check inode...
1099
1100
1101
1102
  	spin_unlock(&ci->i_ceph_lock);
  
  	return ret;
  }
db40cc170   Yan, Zheng   ceph: keep i_snap...
1103
1104
1105
1106
1107
1108
1109
  static void drop_inode_snap_realm(struct ceph_inode_info *ci)
  {
  	struct ceph_snap_realm *realm = ci->i_snap_realm;
  	spin_lock(&realm->inodes_with_caps_lock);
  	list_del_init(&ci->i_snap_realm_item);
  	ci->i_snap_realm_counter++;
  	ci->i_snap_realm = NULL;
d95e674c0   Yan, Zheng   ceph: clear inode...
1110
1111
  	if (realm->ino == ci->i_vino.ino)
  		realm->inode = NULL;
db40cc170   Yan, Zheng   ceph: keep i_snap...
1112
1113
1114
1115
  	spin_unlock(&realm->inodes_with_caps_lock);
  	ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
  			    realm);
  }
a8599bd82   Sage Weil   ceph: capability ...
1116
  /*
f818a7367   Sage Weil   ceph: fix cap rem...
1117
1118
   * Remove a cap.  Take steps to deal with a racing iterate_session_caps.
   *
be655596b   Sage Weil   ceph: use i_ceph_...
1119
   * caller should hold i_ceph_lock.
a6369741c   Sage Weil   ceph: fix comment...
1120
   * caller will not hold session s_mutex if called from destroy_inode.
a8599bd82   Sage Weil   ceph: capability ...
1121
   */
a096b09ae   Yan, Zheng   ceph: queue cap r...
1122
  void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
a8599bd82   Sage Weil   ceph: capability ...
1123
1124
1125
  {
  	struct ceph_mds_session *session = cap->session;
  	struct ceph_inode_info *ci = cap->ci;
db1c6b8a2   Luis Henriques   ceph: fix race in...
1126
  	struct ceph_mds_client *mdsc;
f818a7367   Sage Weil   ceph: fix cap rem...
1127
  	int removed = 0;
a8599bd82   Sage Weil   ceph: capability ...
1128

db1c6b8a2   Luis Henriques   ceph: fix race in...
1129
1130
1131
1132
1133
1134
  	/* 'ci' being NULL means the remove have already occurred */
  	if (!ci) {
  		dout("%s: cap inode is NULL
  ", __func__);
  		return;
  	}
a8599bd82   Sage Weil   ceph: capability ...
1135
1136
  	dout("__ceph_remove_cap %p from %p
  ", cap, &ci->vfs_inode);
db1c6b8a2   Luis Henriques   ceph: fix race in...
1137
  	mdsc = ceph_inode_to_client(&ci->vfs_inode)->mdsc;
ea60ed6fc   Luis Henriques   ceph: fix use-aft...
1138
1139
  	/* remove from inode's cap rbtree, and clear auth cap */
  	rb_erase(&cap->ci_node, &ci->i_caps);
1cf03a68e   Jeff Layton   ceph: convert mds...
1140
1141
  	if (ci->i_auth_cap == cap) {
  		WARN_ON_ONCE(!list_empty(&ci->i_dirty_item));
ea60ed6fc   Luis Henriques   ceph: fix use-aft...
1142
  		ci->i_auth_cap = NULL;
1cf03a68e   Jeff Layton   ceph: convert mds...
1143
  	}
ea60ed6fc   Luis Henriques   ceph: fix use-aft...
1144

7c1332b8c   Sage Weil   ceph: fix iterate...
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
  	/* remove from session list */
  	spin_lock(&session->s_cap_lock);
  	if (session->s_cap_iterator == cap) {
  		/* not yet, we are iterating over this very cap */
  		dout("__ceph_remove_cap  delaying %p removal from session %p
  ",
  		     cap, cap->session);
  	} else {
  		list_del_init(&cap->session_caps);
  		session->s_nr_caps--;
4f1d756de   Xiubo Li   ceph: add global ...
1155
  		atomic64_dec(&mdsc->metric.total_caps);
7c1332b8c   Sage Weil   ceph: fix iterate...
1156
  		cap->session = NULL;
f818a7367   Sage Weil   ceph: fix cap rem...
1157
  		removed = 1;
7c1332b8c   Sage Weil   ceph: fix iterate...
1158
  	}
f818a7367   Sage Weil   ceph: fix cap rem...
1159
1160
  	/* protect backpointer with s_cap_lock: see iterate_session_caps */
  	cap->ci = NULL;
745a8e3bc   Yan, Zheng   ceph: don't pre-a...
1161
1162
1163
1164
1165
1166
1167
1168
1169
  
  	/*
  	 * s_cap_reconnect is protected by s_cap_lock. no one changes
  	 * s_cap_gen while session is in the reconnect state.
  	 */
  	if (queue_release &&
  	    (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
  		cap->queue_release = 1;
  		if (removed) {
e3ec8d689   Yan, Zheng   ceph: send cap re...
1170
  			__ceph_queue_cap_release(session, cap);
745a8e3bc   Yan, Zheng   ceph: don't pre-a...
1171
1172
1173
1174
1175
1176
  			removed = 0;
  		}
  	} else {
  		cap->queue_release = 0;
  	}
  	cap->cap_ino = ci->i_vino.ino;
7c1332b8c   Sage Weil   ceph: fix iterate...
1177
  	spin_unlock(&session->s_cap_lock);
f818a7367   Sage Weil   ceph: fix cap rem...
1178
  	if (removed)
37151668b   Yehuda Sadeh   ceph: do caps acc...
1179
  		ceph_put_cap(mdsc, cap);
a8599bd82   Sage Weil   ceph: capability ...
1180

bd84fbcb3   Xiubo Li   ceph: switch to g...
1181
1182
1183
1184
1185
1186
1187
  	if (!__ceph_is_any_real_caps(ci)) {
  		/* when reconnect denied, we remove session caps forcibly,
  		 * i_wr_ref can be non-zero. If there are ongoing write,
  		 * keep i_snap_realm.
  		 */
  		if (ci->i_wr_ref == 0 && ci->i_snap_realm)
  			drop_inode_snap_realm(ci);
db40cc170   Yan, Zheng   ceph: keep i_snap...
1188

a8599bd82   Sage Weil   ceph: capability ...
1189
  		__cap_delay_cancel(mdsc, ci);
bd84fbcb3   Xiubo Li   ceph: switch to g...
1190
  	}
a8599bd82   Sage Weil   ceph: capability ...
1191
  }
0ff8bfb39   Jeff Layton   ceph: define new ...
1192
1193
1194
1195
1196
  struct cap_msg_args {
  	struct ceph_mds_session	*session;
  	u64			ino, cid, follows;
  	u64			flush_tid, oldest_flush_tid, size, max_size;
  	u64			xattr_version;
176c77c9c   Jeff Layton   ceph: handle chan...
1197
  	u64			change_attr;
0ff8bfb39   Jeff Layton   ceph: define new ...
1198
  	struct ceph_buffer	*xattr_buf;
0a454bdd5   Jeff Layton   ceph: reorganize ...
1199
  	struct ceph_buffer	*old_xattr_buf;
ec62b894d   Jeff Layton   ceph: handle btim...
1200
  	struct timespec64	atime, mtime, ctime, btime;
0ff8bfb39   Jeff Layton   ceph: define new ...
1201
1202
  	int			op, caps, wanted, dirty;
  	u32			seq, issue_seq, mseq, time_warp_seq;
1e4ef0c63   Jeff Layton   ceph: add flags p...
1203
  	u32			flags;
0ff8bfb39   Jeff Layton   ceph: define new ...
1204
1205
1206
1207
  	kuid_t			uid;
  	kgid_t			gid;
  	umode_t			mode;
  	bool			inline_data;
0a454bdd5   Jeff Layton   ceph: reorganize ...
1208
  	bool			wake;
0ff8bfb39   Jeff Layton   ceph: define new ...
1209
  };
a8599bd82   Sage Weil   ceph: capability ...
1210
  /*
16d68903f   Jeff Layton   ceph: break up se...
1211
1212
   * cap struct size + flock buffer size + inline version + inline data size +
   * osd_epoch_barrier + oldest_flush_tid
a8599bd82   Sage Weil   ceph: capability ...
1213
   */
16d68903f   Jeff Layton   ceph: break up se...
1214
1215
1216
1217
1218
  #define CAP_MSG_SIZE (sizeof(struct ceph_mds_caps) + \
  		      4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4)
  
  /* Marshal up the cap msg to the MDS */
  static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg)
a8599bd82   Sage Weil   ceph: capability ...
1219
1220
  {
  	struct ceph_mds_caps *fc;
e20d258d7   Yan, Zheng   ceph: flush inlin...
1221
  	void *p;
92475f05b   Jeff Layton   ceph: handle epoc...
1222
  	struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
a8599bd82   Sage Weil   ceph: capability ...
1223

16d68903f   Jeff Layton   ceph: break up se...
1224
1225
1226
1227
1228
1229
1230
  	dout("%s %s %llx %llx caps %s wanted %s dirty %s seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu xattr_ver %llu xattr_len %d
  ",
  	     __func__, ceph_cap_op_name(arg->op), arg->cid, arg->ino,
  	     ceph_cap_string(arg->caps), ceph_cap_string(arg->wanted),
  	     ceph_cap_string(arg->dirty), arg->seq, arg->issue_seq,
  	     arg->flush_tid, arg->oldest_flush_tid, arg->mseq, arg->follows,
  	     arg->size, arg->max_size, arg->xattr_version,
0ff8bfb39   Jeff Layton   ceph: define new ...
1231
  	     arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
a8599bd82   Sage Weil   ceph: capability ...
1232

43b296733   Jeff Layton   ceph: update cap ...
1233
  	msg->hdr.version = cpu_to_le16(10);
0ff8bfb39   Jeff Layton   ceph: define new ...
1234
  	msg->hdr.tid = cpu_to_le64(arg->flush_tid);
a8599bd82   Sage Weil   ceph: capability ...
1235

6df058c02   Sage Weil   ceph: include tra...
1236
  	fc = msg->front.iov_base;
a8599bd82   Sage Weil   ceph: capability ...
1237
  	memset(fc, 0, sizeof(*fc));
0ff8bfb39   Jeff Layton   ceph: define new ...
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
  	fc->cap_id = cpu_to_le64(arg->cid);
  	fc->op = cpu_to_le32(arg->op);
  	fc->seq = cpu_to_le32(arg->seq);
  	fc->issue_seq = cpu_to_le32(arg->issue_seq);
  	fc->migrate_seq = cpu_to_le32(arg->mseq);
  	fc->caps = cpu_to_le32(arg->caps);
  	fc->wanted = cpu_to_le32(arg->wanted);
  	fc->dirty = cpu_to_le32(arg->dirty);
  	fc->ino = cpu_to_le64(arg->ino);
  	fc->snap_follows = cpu_to_le64(arg->follows);
  
  	fc->size = cpu_to_le64(arg->size);
  	fc->max_size = cpu_to_le64(arg->max_size);
9bbeab41c   Arnd Bergmann   ceph: use timespe...
1251
1252
1253
  	ceph_encode_timespec64(&fc->mtime, &arg->mtime);
  	ceph_encode_timespec64(&fc->atime, &arg->atime);
  	ceph_encode_timespec64(&fc->ctime, &arg->ctime);
0ff8bfb39   Jeff Layton   ceph: define new ...
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
  	fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
  
  	fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
  	fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
  	fc->mode = cpu_to_le32(arg->mode);
  
  	fc->xattr_version = cpu_to_le64(arg->xattr_version);
  	if (arg->xattr_buf) {
  		msg->middle = ceph_buffer_get(arg->xattr_buf);
  		fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
  		msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
9670079f5   Jeff Layton   ceph: move xattr ...
1265
  	}
e20d258d7   Yan, Zheng   ceph: flush inlin...
1266
  	p = fc + 1;
43b296733   Jeff Layton   ceph: update cap ...
1267
  	/* flock buffer size (version 2) */
e20d258d7   Yan, Zheng   ceph: flush inlin...
1268
  	ceph_encode_32(&p, 0);
43b296733   Jeff Layton   ceph: update cap ...
1269
  	/* inline version (version 4) */
0ff8bfb39   Jeff Layton   ceph: define new ...
1270
  	ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
e20d258d7   Yan, Zheng   ceph: flush inlin...
1271
1272
  	/* inline data size */
  	ceph_encode_32(&p, 0);
92475f05b   Jeff Layton   ceph: handle epoc...
1273
1274
1275
1276
1277
1278
  	/*
  	 * osd_epoch_barrier (version 5)
  	 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
  	 * case it was recently changed
  	 */
  	ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
43b296733   Jeff Layton   ceph: update cap ...
1279
  	/* oldest_flush_tid (version 6) */
0ff8bfb39   Jeff Layton   ceph: define new ...
1280
  	ceph_encode_64(&p, arg->oldest_flush_tid);
e20d258d7   Yan, Zheng   ceph: flush inlin...
1281

43b296733   Jeff Layton   ceph: update cap ...
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
  	/*
  	 * caller_uid/caller_gid (version 7)
  	 *
  	 * Currently, we don't properly track which caller dirtied the caps
  	 * last, and force a flush of them when there is a conflict. For now,
  	 * just set this to 0:0, to emulate how the MDS has worked up to now.
  	 */
  	ceph_encode_32(&p, 0);
  	ceph_encode_32(&p, 0);
  
  	/* pool namespace (version 8) (mds always ignores this) */
  	ceph_encode_32(&p, 0);
176c77c9c   Jeff Layton   ceph: handle chan...
1294
  	/* btime and change_attr (version 9) */
ec62b894d   Jeff Layton   ceph: handle btim...
1295
  	ceph_encode_timespec64(p, &arg->btime);
43b296733   Jeff Layton   ceph: update cap ...
1296
  	p += sizeof(struct ceph_timespec);
176c77c9c   Jeff Layton   ceph: handle chan...
1297
  	ceph_encode_64(&p, arg->change_attr);
43b296733   Jeff Layton   ceph: update cap ...
1298
1299
  
  	/* Advisory flags (version 10) */
1e4ef0c63   Jeff Layton   ceph: add flags p...
1300
  	ceph_encode_32(&p, arg->flags);
a8599bd82   Sage Weil   ceph: capability ...
1301
1302
1303
  }
  
  /*
d6e478197   Yan, Zheng   ceph: hold i_ceph...
1304
   * Queue cap releases when an inode is dropped from our cache.
a8599bd82   Sage Weil   ceph: capability ...
1305
   */
d6e478197   Yan, Zheng   ceph: hold i_ceph...
1306
  void __ceph_remove_caps(struct ceph_inode_info *ci)
a8599bd82   Sage Weil   ceph: capability ...
1307
  {
a8599bd82   Sage Weil   ceph: capability ...
1308
  	struct rb_node *p;
d6e478197   Yan, Zheng   ceph: hold i_ceph...
1309
1310
1311
  	/* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
  	 * may call __ceph_caps_issued_mask() on a freeing inode. */
  	spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
1312
1313
1314
  	p = rb_first(&ci->i_caps);
  	while (p) {
  		struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
a8599bd82   Sage Weil   ceph: capability ...
1315
  		p = rb_next(p);
a096b09ae   Yan, Zheng   ceph: queue cap r...
1316
  		__ceph_remove_cap(cap, true);
a8599bd82   Sage Weil   ceph: capability ...
1317
  	}
d6e478197   Yan, Zheng   ceph: hold i_ceph...
1318
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
1319
1320
1321
  }
  
  /*
0a454bdd5   Jeff Layton   ceph: reorganize ...
1322
1323
1324
   * Prepare to send a cap message to an MDS. Update the cap state, and populate
   * the arg struct with the parameters that will need to be sent. This should
   * be done under the i_ceph_lock to guard against changes to cap state.
a8599bd82   Sage Weil   ceph: capability ...
1325
1326
1327
   *
   * Make note of max_size reported/requested from mds, revoked caps
   * that have now been implemented.
a8599bd82   Sage Weil   ceph: capability ...
1328
   */
0a454bdd5   Jeff Layton   ceph: reorganize ...
1329
1330
1331
  static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
  		       int op, int flags, int used, int want, int retain,
  		       int flushing, u64 flush_tid, u64 oldest_flush_tid)
a8599bd82   Sage Weil   ceph: capability ...
1332
1333
1334
  {
  	struct ceph_inode_info *ci = cap->ci;
  	struct inode *inode = &ci->vfs_inode;
bb0581f01   Colin Ian King   ceph: remove unus...
1335
  	int held, revoking;
a8599bd82   Sage Weil   ceph: capability ...
1336

0a454bdd5   Jeff Layton   ceph: reorganize ...
1337
  	lockdep_assert_held(&ci->i_ceph_lock);
891f3f5a6   Jeff Layton   ceph: add infrast...
1338

68c283236   Sage Weil   ceph: do not reta...
1339
1340
1341
  	held = cap->issued | cap->implemented;
  	revoking = cap->implemented & ~cap->issued;
  	retain &= ~revoking;
68c283236   Sage Weil   ceph: do not reta...
1342

0a454bdd5   Jeff Layton   ceph: reorganize ...
1343
1344
1345
  	dout("%s %p cap %p session %p %s -> %s (revoking %s)
  ",
  	     __func__, inode, cap, cap->session,
a8599bd82   Sage Weil   ceph: capability ...
1346
1347
1348
  	     ceph_cap_string(held), ceph_cap_string(held & retain),
  	     ceph_cap_string(revoking));
  	BUG_ON((retain & CEPH_CAP_PIN) == 0);
a0d93e327   Yan, Zheng   ceph: remove dela...
1349
  	ci->i_ceph_flags &= ~CEPH_I_FLUSH;
a8599bd82   Sage Weil   ceph: capability ...
1350
1351
  
  	cap->issued &= retain;  /* drop bits we don't want */
0a454bdd5   Jeff Layton   ceph: reorganize ...
1352
1353
1354
1355
1356
1357
  	/*
  	 * Wake up any waiters on wanted -> needed transition. This is due to
  	 * the weird transition from buffered to sync IO... we need to flush
  	 * dirty pages _before_ allowing sync writes to avoid reordering.
  	 */
  	arg->wake = cap->implemented & ~cap->issued;
a8599bd82   Sage Weil   ceph: capability ...
1358
1359
  	cap->implemented &= cap->issued | used;
  	cap->mds_wanted = want;
0a454bdd5   Jeff Layton   ceph: reorganize ...
1360
1361
1362
1363
1364
1365
  	arg->session = cap->session;
  	arg->ino = ceph_vino(inode).ino;
  	arg->cid = cap->cap_id;
  	arg->follows = flushing ? ci->i_head_snapc->seq : 0;
  	arg->flush_tid = flush_tid;
  	arg->oldest_flush_tid = oldest_flush_tid;
0ff8bfb39   Jeff Layton   ceph: define new ...
1366

0a454bdd5   Jeff Layton   ceph: reorganize ...
1367
1368
1369
  	arg->size = inode->i_size;
  	ci->i_reported_size = arg->size;
  	arg->max_size = ci->i_wanted_max_size;
6f05b30ea   Yan, Zheng   ceph: reset i_req...
1370
1371
1372
1373
1374
1375
  	if (cap == ci->i_auth_cap) {
  		if (want & CEPH_CAP_ANY_FILE_WR)
  			ci->i_requested_max_size = arg->max_size;
  		else
  			ci->i_requested_max_size = 0;
  	}
a8599bd82   Sage Weil   ceph: capability ...
1376

082afec92   Sage Weil   ceph: fix xattr c...
1377
  	if (flushing & CEPH_CAP_XATTR_EXCL) {
0a454bdd5   Jeff Layton   ceph: reorganize ...
1378
1379
1380
  		arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
  		arg->xattr_version = ci->i_xattrs.version;
  		arg->xattr_buf = ci->i_xattrs.blob;
0ff8bfb39   Jeff Layton   ceph: define new ...
1381
  	} else {
0a454bdd5   Jeff Layton   ceph: reorganize ...
1382
1383
  		arg->xattr_buf = NULL;
  		arg->old_xattr_buf = NULL;
a8599bd82   Sage Weil   ceph: capability ...
1384
  	}
0a454bdd5   Jeff Layton   ceph: reorganize ...
1385
1386
1387
1388
1389
  	arg->mtime = inode->i_mtime;
  	arg->atime = inode->i_atime;
  	arg->ctime = inode->i_ctime;
  	arg->btime = ci->i_btime;
  	arg->change_attr = inode_peek_iversion_raw(inode);
0ff8bfb39   Jeff Layton   ceph: define new ...
1390

0a454bdd5   Jeff Layton   ceph: reorganize ...
1391
1392
1393
1394
  	arg->op = op;
  	arg->caps = cap->implemented;
  	arg->wanted = want;
  	arg->dirty = flushing;
0ff8bfb39   Jeff Layton   ceph: define new ...
1395

0a454bdd5   Jeff Layton   ceph: reorganize ...
1396
1397
1398
1399
  	arg->seq = cap->seq;
  	arg->issue_seq = cap->issue_seq;
  	arg->mseq = cap->mseq;
  	arg->time_warp_seq = ci->i_time_warp_seq;
0ff8bfb39   Jeff Layton   ceph: define new ...
1400

0a454bdd5   Jeff Layton   ceph: reorganize ...
1401
1402
1403
  	arg->uid = inode->i_uid;
  	arg->gid = inode->i_gid;
  	arg->mode = inode->i_mode;
0ff8bfb39   Jeff Layton   ceph: define new ...
1404

0a454bdd5   Jeff Layton   ceph: reorganize ...
1405
  	arg->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
49ada6e8d   Yan, Zheng   ceph: more precis...
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
  	if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) &&
  	    !list_empty(&ci->i_cap_snaps)) {
  		struct ceph_cap_snap *capsnap;
  		list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) {
  			if (capsnap->cap_flush.tid)
  				break;
  			if (capsnap->need_flush) {
  				flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
  				break;
  			}
  		}
  	}
0a454bdd5   Jeff Layton   ceph: reorganize ...
1418
1419
  	arg->flags = flags;
  }
a8599bd82   Sage Weil   ceph: capability ...
1420

0a454bdd5   Jeff Layton   ceph: reorganize ...
1421
1422
1423
1424
1425
  /*
   * Send a cap msg on the given inode.
   *
   * Caller should hold snap_rwsem (read), s_mutex.
   */
523119808   Jeff Layton   ceph: drop separa...
1426
  static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
0a454bdd5   Jeff Layton   ceph: reorganize ...
1427
  {
16d68903f   Jeff Layton   ceph: break up se...
1428
  	struct ceph_msg *msg;
0a454bdd5   Jeff Layton   ceph: reorganize ...
1429
  	struct inode *inode = &ci->vfs_inode;
12fe3dda7   Luis Henriques   ceph: fix buffer ...
1430

16d68903f   Jeff Layton   ceph: break up se...
1431
1432
1433
1434
  	msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, CAP_MSG_SIZE, GFP_NOFS, false);
  	if (!msg) {
  		pr_err("error allocating cap msg: ino (%llx.%llx) flushing %s tid %llu, requeuing cap.
  ",
0a454bdd5   Jeff Layton   ceph: reorganize ...
1435
1436
  		       ceph_vinop(inode), ceph_cap_string(arg->dirty),
  		       arg->flush_tid);
a0d93e327   Yan, Zheng   ceph: remove dela...
1437
  		spin_lock(&ci->i_ceph_lock);
523119808   Jeff Layton   ceph: drop separa...
1438
  		__cap_delay_requeue(arg->session->s_mdsc, ci);
a0d93e327   Yan, Zheng   ceph: remove dela...
1439
  		spin_unlock(&ci->i_ceph_lock);
16d68903f   Jeff Layton   ceph: break up se...
1440
  		return;
a8599bd82   Sage Weil   ceph: capability ...
1441
  	}
16d68903f   Jeff Layton   ceph: break up se...
1442
1443
  	encode_cap_msg(msg, arg);
  	ceph_con_send(&arg->session->s_con, msg);
0a454bdd5   Jeff Layton   ceph: reorganize ...
1444
  	ceph_buffer_put(arg->old_xattr_buf);
0a454bdd5   Jeff Layton   ceph: reorganize ...
1445
1446
  	if (arg->wake)
  		wake_up_all(&ci->i_cap_wq);
a8599bd82   Sage Weil   ceph: capability ...
1447
  }
0e2943878   Yan, Zheng   ceph: unify cap f...
1448
1449
1450
1451
1452
  static inline int __send_flush_snap(struct inode *inode,
  				    struct ceph_mds_session *session,
  				    struct ceph_cap_snap *capsnap,
  				    u32 mseq, u64 oldest_flush_tid)
  {
0ff8bfb39   Jeff Layton   ceph: define new ...
1453
  	struct cap_msg_args	arg;
16d68903f   Jeff Layton   ceph: break up se...
1454
1455
1456
1457
1458
  	struct ceph_msg		*msg;
  
  	msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, CAP_MSG_SIZE, GFP_NOFS, false);
  	if (!msg)
  		return -ENOMEM;
0ff8bfb39   Jeff Layton   ceph: define new ...
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
  
  	arg.session = session;
  	arg.ino = ceph_vino(inode).ino;
  	arg.cid = 0;
  	arg.follows = capsnap->follows;
  	arg.flush_tid = capsnap->cap_flush.tid;
  	arg.oldest_flush_tid = oldest_flush_tid;
  
  	arg.size = capsnap->size;
  	arg.max_size = 0;
  	arg.xattr_version = capsnap->xattr_version;
  	arg.xattr_buf = capsnap->xattr_blob;
0a454bdd5   Jeff Layton   ceph: reorganize ...
1471
  	arg.old_xattr_buf = NULL;
0ff8bfb39   Jeff Layton   ceph: define new ...
1472
1473
1474
1475
  
  	arg.atime = capsnap->atime;
  	arg.mtime = capsnap->mtime;
  	arg.ctime = capsnap->ctime;
ec62b894d   Jeff Layton   ceph: handle btim...
1476
  	arg.btime = capsnap->btime;
176c77c9c   Jeff Layton   ceph: handle chan...
1477
  	arg.change_attr = capsnap->change_attr;
0ff8bfb39   Jeff Layton   ceph: define new ...
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
  
  	arg.op = CEPH_CAP_OP_FLUSHSNAP;
  	arg.caps = capsnap->issued;
  	arg.wanted = 0;
  	arg.dirty = capsnap->dirty;
  
  	arg.seq = 0;
  	arg.issue_seq = 0;
  	arg.mseq = mseq;
  	arg.time_warp_seq = capsnap->time_warp_seq;
  
  	arg.uid = capsnap->uid;
  	arg.gid = capsnap->gid;
  	arg.mode = capsnap->mode;
  
  	arg.inline_data = capsnap->inline_data;
1e4ef0c63   Jeff Layton   ceph: add flags p...
1494
  	arg.flags = 0;
0a454bdd5   Jeff Layton   ceph: reorganize ...
1495
  	arg.wake = false;
0ff8bfb39   Jeff Layton   ceph: define new ...
1496

16d68903f   Jeff Layton   ceph: break up se...
1497
1498
1499
  	encode_cap_msg(msg, &arg);
  	ceph_con_send(&arg.session->s_con, msg);
  	return 0;
0e2943878   Yan, Zheng   ceph: unify cap f...
1500
  }
a8599bd82   Sage Weil   ceph: capability ...
1501
1502
1503
1504
1505
1506
1507
  /*
   * When a snapshot is taken, clients accumulate dirty metadata on
   * inodes with capabilities in ceph_cap_snaps to describe the file
   * state at the time the snapshot was taken.  This must be flushed
   * asynchronously back to the MDS once sync writes complete and dirty
   * data is written out.
   *
be655596b   Sage Weil   ceph: use i_ceph_...
1508
   * Called under i_ceph_lock.  Takes s_mutex as needed.
a8599bd82   Sage Weil   ceph: capability ...
1509
   */
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1510
1511
  static void __ceph_flush_snaps(struct ceph_inode_info *ci,
  			       struct ceph_mds_session *session)
be655596b   Sage Weil   ceph: use i_ceph_...
1512
1513
  		__releases(ci->i_ceph_lock)
  		__acquires(ci->i_ceph_lock)
a8599bd82   Sage Weil   ceph: capability ...
1514
1515
  {
  	struct inode *inode = &ci->vfs_inode;
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1516
  	struct ceph_mds_client *mdsc = session->s_mdsc;
a8599bd82   Sage Weil   ceph: capability ...
1517
  	struct ceph_cap_snap *capsnap;
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1518
1519
  	u64 oldest_flush_tid = 0;
  	u64 first_tid = 1, last_tid = 0;
a8599bd82   Sage Weil   ceph: capability ...
1520

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1521
1522
  	dout("__flush_snaps %p session %p
  ", inode, session);
a8599bd82   Sage Weil   ceph: capability ...
1523

a8599bd82   Sage Weil   ceph: capability ...
1524
  	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
a8599bd82   Sage Weil   ceph: capability ...
1525
1526
1527
1528
1529
  		/*
  		 * we need to wait for sync writes to complete and for dirty
  		 * pages to be written out.
  		 */
  		if (capsnap->dirty_pages || capsnap->writing)
cfc0bf664   Sage Weil   ceph: stop sendin...
1530
  			break;
a8599bd82   Sage Weil   ceph: capability ...
1531

860560904   Yan, Zheng   ceph: avoid sendi...
1532
1533
  		/* should be removed by ceph_try_drop_cap_snap() */
  		BUG_ON(!capsnap->need_flush);
819ccbfa4   Sage Weil   ceph: fix leaked ...
1534

e835124c2   Sage Weil   ceph: only send o...
1535
  		/* only flush each capsnap once */
0e2943878   Yan, Zheng   ceph: unify cap f...
1536
  		if (capsnap->cap_flush.tid > 0) {
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1537
1538
  			dout(" already flushed %p, skipping
  ", capsnap);
e835124c2   Sage Weil   ceph: only send o...
1539
1540
  			continue;
  		}
553adfd94   Yan, Zheng   ceph: track pendi...
1541
  		spin_lock(&mdsc->cap_dirty_lock);
0e2943878   Yan, Zheng   ceph: unify cap f...
1542
1543
1544
  		capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
  		list_add_tail(&capsnap->cap_flush.g_list,
  			      &mdsc->cap_flush_list);
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1545
1546
  		if (oldest_flush_tid == 0)
  			oldest_flush_tid = __get_oldest_flush_tid(mdsc);
0e2943878   Yan, Zheng   ceph: unify cap f...
1547
1548
1549
1550
  		if (list_empty(&ci->i_flushing_item)) {
  			list_add_tail(&ci->i_flushing_item,
  				      &session->s_cap_flushing);
  		}
553adfd94   Yan, Zheng   ceph: track pendi...
1551
  		spin_unlock(&mdsc->cap_dirty_lock);
0e2943878   Yan, Zheng   ceph: unify cap f...
1552
1553
  		list_add_tail(&capsnap->cap_flush.i_list,
  			      &ci->i_cap_flush_list);
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
  		if (first_tid == 1)
  			first_tid = capsnap->cap_flush.tid;
  		last_tid = capsnap->cap_flush.tid;
  	}
  
  	ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
  
  	while (first_tid <= last_tid) {
  		struct ceph_cap *cap = ci->i_auth_cap;
  		struct ceph_cap_flush *cf;
  		int ret;
  
  		if (!(cap && cap->session == session)) {
  			dout("__flush_snaps %p auth cap %p not mds%d, "
  			     "stop
  ", inode, cap, session->s_mds);
  			break;
  		}
  
  		ret = -ENOENT;
  		list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
  			if (cf->tid >= first_tid) {
  				ret = 0;
  				break;
  			}
  		}
  		if (ret < 0)
  			break;
  
  		first_tid = cf->tid + 1;
  
  		capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
805692d0e   Elena Reshetova   ceph: convert cep...
1586
  		refcount_inc(&capsnap->nref);
be655596b   Sage Weil   ceph: use i_ceph_...
1587
  		spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
1588

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1589
1590
1591
  		dout("__flush_snaps %p capsnap %p tid %llu %s
  ",
  		     inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
a8599bd82   Sage Weil   ceph: capability ...
1592

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1593
1594
1595
1596
1597
1598
1599
1600
  		ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  					oldest_flush_tid);
  		if (ret < 0) {
  			pr_err("__flush_snaps: error sending cap flushsnap, "
  			       "ino (%llx.%llx) tid %llu follows %llu
  ",
  				ceph_vinop(inode), cf->tid, capsnap->follows);
  		}
a8599bd82   Sage Weil   ceph: capability ...
1601

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1602
  		ceph_put_cap_snap(capsnap);
be655596b   Sage Weil   ceph: use i_ceph_...
1603
  		spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
1604
  	}
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1605
  }
a8599bd82   Sage Weil   ceph: capability ...
1606

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1607
1608
1609
1610
1611
  void ceph_flush_snaps(struct ceph_inode_info *ci,
  		      struct ceph_mds_session **psession)
  {
  	struct inode *inode = &ci->vfs_inode;
  	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
e4d2b16a4   Yan, Zheng   ceph: fix null po...
1612
  	struct ceph_mds_session *session = NULL;
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1613
  	int mds;
e4d2b16a4   Yan, Zheng   ceph: fix null po...
1614

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1615
1616
  	dout("ceph_flush_snaps %p
  ", inode);
e4d2b16a4   Yan, Zheng   ceph: fix null po...
1617
1618
  	if (psession)
  		session = *psession;
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
  retry:
  	spin_lock(&ci->i_ceph_lock);
  	if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
  		dout(" no capsnap needs flush, doing nothing
  ");
  		goto out;
  	}
  	if (!ci->i_auth_cap) {
  		dout(" no auth cap (migrating?), doing nothing
  ");
  		goto out;
  	}
a8599bd82   Sage Weil   ceph: capability ...
1631

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1632
1633
1634
1635
  	mds = ci->i_auth_cap->session->s_mds;
  	if (session && session->s_mds != mds) {
  		dout(" oops, wrong session %p mutex
  ", session);
a8599bd82   Sage Weil   ceph: capability ...
1636
1637
  		mutex_unlock(&session->s_mutex);
  		ceph_put_mds_session(session);
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
  		session = NULL;
  	}
  	if (!session) {
  		spin_unlock(&ci->i_ceph_lock);
  		mutex_lock(&mdsc->mutex);
  		session = __ceph_lookup_mds_session(mdsc, mds);
  		mutex_unlock(&mdsc->mutex);
  		if (session) {
  			dout(" inverting session/ino locks on %p
  ", session);
  			mutex_lock(&session->s_mutex);
  		}
  		goto retry;
a8599bd82   Sage Weil   ceph: capability ...
1651
  	}
a8599bd82   Sage Weil   ceph: capability ...
1652

24d063acc   Yan, Zheng   ceph: make sure f...
1653
  	// make sure flushsnap messages are sent in proper order.
054f8d41a   Yan, Zheng   ceph: clear CEPH_...
1654
  	if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
24d063acc   Yan, Zheng   ceph: make sure f...
1655
  		__kick_flushing_caps(mdsc, session, ci, 0);
24d063acc   Yan, Zheng   ceph: make sure f...
1656

ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1657
1658
  	__ceph_flush_snaps(ci, session);
  out:
be655596b   Sage Weil   ceph: use i_ceph_...
1659
  	spin_unlock(&ci->i_ceph_lock);
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1660
1661
1662
  
  	if (psession) {
  		*psession = session;
c858a0709   Yan, Zheng   ceph: fix NULL po...
1663
  	} else if (session) {
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
1664
1665
1666
1667
1668
1669
1670
  		mutex_unlock(&session->s_mutex);
  		ceph_put_mds_session(session);
  	}
  	/* we flushed them all; remove this inode from the queue */
  	spin_lock(&mdsc->snap_flush_lock);
  	list_del_init(&ci->i_snap_flush_item);
  	spin_unlock(&mdsc->snap_flush_lock);
a8599bd82   Sage Weil   ceph: capability ...
1671
1672
1673
  }
  
  /*
fca65b4ad   Sage Weil   ceph: do not call...
1674
1675
1676
   * Mark caps dirty.  If inode is newly dirty, return the dirty flags.
   * Caller is then responsible for calling __mark_inode_dirty with the
   * returned flags value.
76e3b390d   Sage Weil   ceph: move dirty ...
1677
   */
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1678
1679
  int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
  			   struct ceph_cap_flush **pcf)
76e3b390d   Sage Weil   ceph: move dirty ...
1680
  {
640ef79d2   Cheng Renquan   ceph: use ceph_sb...
1681
  	struct ceph_mds_client *mdsc =
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
1682
  		ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
76e3b390d   Sage Weil   ceph: move dirty ...
1683
1684
1685
  	struct inode *inode = &ci->vfs_inode;
  	int was = ci->i_dirty_caps;
  	int dirty = 0;
c7e4f85ce   Jeff Layton   ceph: more caps.c...
1686
  	lockdep_assert_held(&ci->i_ceph_lock);
571ade336   Yan, Zheng   ceph: don't mark ...
1687
1688
1689
1690
1691
1692
1693
  	if (!ci->i_auth_cap) {
  		pr_warn("__mark_dirty_caps %p %llx mask %s, "
  			"but no auth cap (session was closed?)
  ",
  			inode, ceph_ino(inode), ceph_cap_string(mask));
  		return 0;
  	}
76e3b390d   Sage Weil   ceph: move dirty ...
1694
1695
1696
1697
1698
1699
  	dout("__mark_dirty_caps %p %s dirty %s -> %s
  ", &ci->vfs_inode,
  	     ceph_cap_string(mask), ceph_cap_string(was),
  	     ceph_cap_string(was | mask));
  	ci->i_dirty_caps |= mask;
  	if (was == 0) {
1cf03a68e   Jeff Layton   ceph: convert mds...
1700
  		struct ceph_mds_session *session = ci->i_auth_cap->session;
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1701
1702
  		WARN_ON_ONCE(ci->i_prealloc_cap_flush);
  		swap(ci->i_prealloc_cap_flush, *pcf);
604d1b024   Yan, Zheng   ceph: take snap_r...
1703
1704
  		if (!ci->i_head_snapc) {
  			WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
7d8cb26d7   Sage Weil   ceph: maintain i_...
1705
1706
  			ci->i_head_snapc = ceph_get_snap_context(
  				ci->i_snap_realm->cached_context);
604d1b024   Yan, Zheng   ceph: take snap_r...
1707
  		}
0685235ff   Yan, Zheng   ceph: Don't add d...
1708
1709
1710
  		dout(" inode %p now dirty snapc %p auth cap %p
  ",
  		     &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
76e3b390d   Sage Weil   ceph: move dirty ...
1711
1712
  		BUG_ON(!list_empty(&ci->i_dirty_item));
  		spin_lock(&mdsc->cap_dirty_lock);
1cf03a68e   Jeff Layton   ceph: convert mds...
1713
  		list_add(&ci->i_dirty_item, &session->s_cap_dirty);
76e3b390d   Sage Weil   ceph: move dirty ...
1714
1715
  		spin_unlock(&mdsc->cap_dirty_lock);
  		if (ci->i_flushing_caps == 0) {
3772d26d8   Sage Weil   ceph: use ihold()...
1716
  			ihold(inode);
76e3b390d   Sage Weil   ceph: move dirty ...
1717
1718
  			dirty |= I_DIRTY_SYNC;
  		}
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1719
1720
  	} else {
  		WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
76e3b390d   Sage Weil   ceph: move dirty ...
1721
1722
1723
1724
1725
  	}
  	BUG_ON(list_empty(&ci->i_dirty_item));
  	if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
  	    (mask & CEPH_CAP_FILE_BUFFER))
  		dirty |= I_DIRTY_DATASYNC;
a0d93e327   Yan, Zheng   ceph: remove dela...
1726
  	__cap_delay_requeue(mdsc, ci);
fca65b4ad   Sage Weil   ceph: do not call...
1727
  	return dirty;
76e3b390d   Sage Weil   ceph: move dirty ...
1728
  }
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
  struct ceph_cap_flush *ceph_alloc_cap_flush(void)
  {
  	return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
  }
  
  void ceph_free_cap_flush(struct ceph_cap_flush *cf)
  {
  	if (cf)
  		kmem_cache_free(ceph_cap_flush_cachep, cf);
  }
a2971c8cc   Yan, Zheng   ceph: send TID of...
1739
1740
  static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
  {
e4500b5e3   Yan, Zheng   ceph: use list in...
1741
  	if (!list_empty(&mdsc->cap_flush_list)) {
a2971c8cc   Yan, Zheng   ceph: send TID of...
1742
  		struct ceph_cap_flush *cf =
e4500b5e3   Yan, Zheng   ceph: use list in...
1743
1744
  			list_first_entry(&mdsc->cap_flush_list,
  					 struct ceph_cap_flush, g_list);
a2971c8cc   Yan, Zheng   ceph: send TID of...
1745
1746
1747
1748
  		return cf->tid;
  	}
  	return 0;
  }
76e3b390d   Sage Weil   ceph: move dirty ...
1749
  /*
c8799fc46   Yan, Zheng   ceph: optimize ca...
1750
1751
1752
   * Remove cap_flush from the mdsc's or inode's flushing cap list.
   * Return true if caller needs to wake up flush waiters.
   */
681ac6348   Jeff Layton   ceph: split up __...
1753
1754
  static bool __detach_cap_flush_from_mdsc(struct ceph_mds_client *mdsc,
  					 struct ceph_cap_flush *cf)
c8799fc46   Yan, Zheng   ceph: optimize ca...
1755
1756
1757
  {
  	struct ceph_cap_flush *prev;
  	bool wake = cf->wake;
681ac6348   Jeff Layton   ceph: split up __...
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
  
  	if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
  		prev = list_prev_entry(cf, g_list);
  		prev->wake = true;
  		wake = false;
  	}
  	list_del(&cf->g_list);
  	return wake;
  }
  
  static bool __detach_cap_flush_from_ci(struct ceph_inode_info *ci,
  				       struct ceph_cap_flush *cf)
  {
  	struct ceph_cap_flush *prev;
  	bool wake = cf->wake;
  
  	if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
  		prev = list_prev_entry(cf, i_list);
  		prev->wake = true;
  		wake = false;
c8799fc46   Yan, Zheng   ceph: optimize ca...
1778
  	}
681ac6348   Jeff Layton   ceph: split up __...
1779
  	list_del(&cf->i_list);
c8799fc46   Yan, Zheng   ceph: optimize ca...
1780
1781
1782
1783
  	return wake;
  }
  
  /*
a8599bd82   Sage Weil   ceph: capability ...
1784
1785
   * Add dirty inode to the flushing list.  Assigned a seq number so we
   * can wait for caps to flush without starving.
cdc35f962   Sage Weil   ceph: move generi...
1786
   *
9f3345d8e   Jeff Layton   ceph: have __mark...
1787
   * Called under i_ceph_lock. Returns the flush tid.
a8599bd82   Sage Weil   ceph: capability ...
1788
   */
9f3345d8e   Jeff Layton   ceph: have __mark...
1789
  static u64 __mark_caps_flushing(struct inode *inode,
c8799fc46   Yan, Zheng   ceph: optimize ca...
1790
  				struct ceph_mds_session *session, bool wake,
9f3345d8e   Jeff Layton   ceph: have __mark...
1791
  				u64 *oldest_flush_tid)
a8599bd82   Sage Weil   ceph: capability ...
1792
  {
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
1793
  	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd82   Sage Weil   ceph: capability ...
1794
  	struct ceph_inode_info *ci = ceph_inode(inode);
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1795
  	struct ceph_cap_flush *cf = NULL;
cdc35f962   Sage Weil   ceph: move generi...
1796
  	int flushing;
50b885b96   Sage Weil   ceph: whitespace ...
1797

c7e4f85ce   Jeff Layton   ceph: more caps.c...
1798
  	lockdep_assert_held(&ci->i_ceph_lock);
cdc35f962   Sage Weil   ceph: move generi...
1799
  	BUG_ON(ci->i_dirty_caps == 0);
a8599bd82   Sage Weil   ceph: capability ...
1800
  	BUG_ON(list_empty(&ci->i_dirty_item));
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1801
  	BUG_ON(!ci->i_prealloc_cap_flush);
cdc35f962   Sage Weil   ceph: move generi...
1802
1803
1804
1805
1806
1807
1808
1809
1810
  
  	flushing = ci->i_dirty_caps;
  	dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s
  ",
  	     ceph_cap_string(flushing),
  	     ceph_cap_string(ci->i_flushing_caps),
  	     ceph_cap_string(ci->i_flushing_caps | flushing));
  	ci->i_flushing_caps |= flushing;
  	ci->i_dirty_caps = 0;
afcdaea3f   Sage Weil   ceph: flush dirty...
1811
1812
  	dout(" inode %p now !dirty
  ", inode);
cdc35f962   Sage Weil   ceph: move generi...
1813

f66fd9f09   Yan, Zheng   ceph: pre-allocat...
1814
  	swap(cf, ci->i_prealloc_cap_flush);
553adfd94   Yan, Zheng   ceph: track pendi...
1815
  	cf->caps = flushing;
c8799fc46   Yan, Zheng   ceph: optimize ca...
1816
  	cf->wake = wake;
553adfd94   Yan, Zheng   ceph: track pendi...
1817

a8599bd82   Sage Weil   ceph: capability ...
1818
  	spin_lock(&mdsc->cap_dirty_lock);
afcdaea3f   Sage Weil   ceph: flush dirty...
1819
  	list_del_init(&ci->i_dirty_item);
553adfd94   Yan, Zheng   ceph: track pendi...
1820
  	cf->tid = ++mdsc->last_cap_flush_tid;
e4500b5e3   Yan, Zheng   ceph: use list in...
1821
  	list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
a2971c8cc   Yan, Zheng   ceph: send TID of...
1822
  	*oldest_flush_tid = __get_oldest_flush_tid(mdsc);
553adfd94   Yan, Zheng   ceph: track pendi...
1823

a8599bd82   Sage Weil   ceph: capability ...
1824
1825
1826
  	if (list_empty(&ci->i_flushing_item)) {
  		list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
  		mdsc->num_cap_flushing++;
a8599bd82   Sage Weil   ceph: capability ...
1827
1828
  	}
  	spin_unlock(&mdsc->cap_dirty_lock);
cdc35f962   Sage Weil   ceph: move generi...
1829

e4500b5e3   Yan, Zheng   ceph: use list in...
1830
  	list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
553adfd94   Yan, Zheng   ceph: track pendi...
1831

9f3345d8e   Jeff Layton   ceph: have __mark...
1832
  	return cf->tid;
a8599bd82   Sage Weil   ceph: capability ...
1833
1834
1835
  }
  
  /*
5ecad6fd7   Sage Weil   ceph: fix check f...
1836
1837
   * try to invalidate mapping pages without blocking.
   */
5ecad6fd7   Sage Weil   ceph: fix check f...
1838
1839
1840
1841
  static int try_nonblocking_invalidate(struct inode *inode)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	u32 invalidating_gen = ci->i_rdcache_gen;
be655596b   Sage Weil   ceph: use i_ceph_...
1842
  	spin_unlock(&ci->i_ceph_lock);
5ecad6fd7   Sage Weil   ceph: fix check f...
1843
  	invalidate_mapping_pages(&inode->i_data, 0, -1);
be655596b   Sage Weil   ceph: use i_ceph_...
1844
  	spin_lock(&ci->i_ceph_lock);
5ecad6fd7   Sage Weil   ceph: fix check f...
1845

18a38193e   Sage Weil   ceph: use mapping...
1846
  	if (inode->i_data.nrpages == 0 &&
5ecad6fd7   Sage Weil   ceph: fix check f...
1847
1848
1849
1850
  	    invalidating_gen == ci->i_rdcache_gen) {
  		/* success. */
  		dout("try_nonblocking_invalidate %p success
  ", inode);
cd045cb42   Sage Weil   ceph: fix rdcache...
1851
1852
  		/* save any racing async invalidate some trouble */
  		ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
5ecad6fd7   Sage Weil   ceph: fix check f...
1853
1854
1855
1856
1857
1858
  		return 0;
  	}
  	dout("try_nonblocking_invalidate %p failed
  ", inode);
  	return -1;
  }
efb0ca765   Yan, Zheng   ceph: update the ...
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
  bool __ceph_should_report_size(struct ceph_inode_info *ci)
  {
  	loff_t size = ci->vfs_inode.i_size;
  	/* mds will adjust max size according to the reported size */
  	if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
  		return false;
  	if (size >= ci->i_max_size)
  		return true;
  	/* half of previous max_size increment has been used */
  	if (ci->i_max_size > ci->i_reported_size &&
  	    (size << 1) >= ci->i_max_size + ci->i_reported_size)
  		return true;
  	return false;
  }
5ecad6fd7   Sage Weil   ceph: fix check f...
1873
  /*
a8599bd82   Sage Weil   ceph: capability ...
1874
1875
1876
1877
   * Swiss army knife function to examine currently used and wanted
   * versus held caps.  Release, flush, ack revoked caps to mds as
   * appropriate.
   *
a8599bd82   Sage Weil   ceph: capability ...
1878
1879
1880
1881
1882
1883
1884
   *  CHECK_CAPS_AUTHONLY - we should only check the auth cap
   *  CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
   *    further delay.
   */
  void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  		     struct ceph_mds_session *session)
  {
a8599bd82   Sage Weil   ceph: capability ...
1885
  	struct inode *inode = &ci->vfs_inode;
2678da88f   Xiubo Li   ceph: add ceph_sb...
1886
  	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
a8599bd82   Sage Weil   ceph: capability ...
1887
  	struct ceph_cap *cap;
a2971c8cc   Yan, Zheng   ceph: send TID of...
1888
  	u64 flush_tid, oldest_flush_tid;
395c312b9   Yan, Zheng   ceph: allow revok...
1889
  	int file_wanted, used, cap_used;
a8599bd82   Sage Weil   ceph: capability ...
1890
  	int took_snap_rwsem = 0;             /* true if mdsc->snap_rwsem held */
cbd036359   Sage Weil   ceph: cap revocat...
1891
  	int issued, implemented, want, retain, revoking, flushing = 0;
a8599bd82   Sage Weil   ceph: capability ...
1892
1893
1894
  	int mds = -1;   /* keep track of how far we've gone through i_caps list
  			   to avoid an infinite loop on retry */
  	struct rb_node *p;
3609404f8   Yan, Zheng   ceph: update type...
1895
  	bool queue_invalidate = false;
3609404f8   Yan, Zheng   ceph: update type...
1896
  	bool tried_invalidate = false;
a8599bd82   Sage Weil   ceph: capability ...
1897

be655596b   Sage Weil   ceph: use i_ceph_...
1898
  	spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
1899
1900
  	if (ci->i_ceph_flags & CEPH_I_FLUSH)
  		flags |= CHECK_CAPS_FLUSH;
a8599bd82   Sage Weil   ceph: capability ...
1901
1902
  	goto retry_locked;
  retry:
be655596b   Sage Weil   ceph: use i_ceph_...
1903
  	spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
1904
  retry_locked:
c74d79af9   Jeff Layton   ceph: comment cle...
1905
  	/* Caps wanted by virtue of active open files. */
a8599bd82   Sage Weil   ceph: capability ...
1906
  	file_wanted = __ceph_caps_file_wanted(ci);
c74d79af9   Jeff Layton   ceph: comment cle...
1907
1908
  
  	/* Caps which have active references against them */
a8599bd82   Sage Weil   ceph: capability ...
1909
  	used = __ceph_caps_used(ci);
c74d79af9   Jeff Layton   ceph: comment cle...
1910
1911
1912
1913
1914
1915
1916
  
  	/*
  	 * "issued" represents the current caps that the MDS wants us to have.
  	 * "implemented" is the set that we have been granted, and includes the
  	 * ones that have not yet been returned to the MDS (the "revoking" set,
  	 * usually because they have outstanding references).
  	 */
cbd036359   Sage Weil   ceph: cap revocat...
1917
1918
  	issued = __ceph_caps_issued(ci, &implemented);
  	revoking = implemented & ~issued;
a8599bd82   Sage Weil   ceph: capability ...
1919

41445999a   Yan, Zheng   ceph: don't inclu...
1920
  	want = file_wanted;
c74d79af9   Jeff Layton   ceph: comment cle...
1921
1922
  
  	/* The ones we currently want to retain (may be adjusted below) */
41445999a   Yan, Zheng   ceph: don't inclu...
1923
  	retain = file_wanted | used | CEPH_CAP_PIN;
a8599bd82   Sage Weil   ceph: capability ...
1924
  	if (!mdsc->stopping && inode->i_nlink > 0) {
41445999a   Yan, Zheng   ceph: don't inclu...
1925
  		if (file_wanted) {
a8599bd82   Sage Weil   ceph: capability ...
1926
  			retain |= CEPH_CAP_ANY;       /* be greedy */
32ec43977   Yan, Zheng   ceph: hold on to ...
1927
1928
  		} else if (S_ISDIR(inode->i_mode) &&
  			   (issued & CEPH_CAP_FILE_SHARED) &&
8a2ac3a8e   Yan, Zheng   ceph: don't reque...
1929
  			   __ceph_dir_is_complete(ci)) {
32ec43977   Yan, Zheng   ceph: hold on to ...
1930
1931
1932
1933
1934
1935
  			/*
  			 * If a directory is complete, we want to keep
  			 * the exclusive cap. So that MDS does not end up
  			 * revoking the shared cap on every create/unlink
  			 * operation.
  			 */
a25949b99   Jeff Layton   ceph: cap trackin...
1936
  			if (IS_RDONLY(inode)) {
8a2ac3a8e   Yan, Zheng   ceph: don't reque...
1937
  				want = CEPH_CAP_ANY_SHARED;
a25949b99   Jeff Layton   ceph: cap trackin...
1938
  			} else {
719a2514e   Yan, Zheng   ceph: consider in...
1939
  				want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
a25949b99   Jeff Layton   ceph: cap trackin...
1940
  			}
32ec43977   Yan, Zheng   ceph: hold on to ...
1941
  			retain |= want;
a8599bd82   Sage Weil   ceph: capability ...
1942
  		} else {
32ec43977   Yan, Zheng   ceph: hold on to ...
1943

a8599bd82   Sage Weil   ceph: capability ...
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
  			retain |= CEPH_CAP_ANY_SHARED;
  			/*
  			 * keep RD only if we didn't have the file open RW,
  			 * because then the mds would revoke it anyway to
  			 * journal max_size=0.
  			 */
  			if (ci->i_max_size == 0)
  				retain |= CEPH_CAP_ANY_RD;
  		}
  	}
  
  	dout("check_caps %p file_want %s used %s dirty %s flushing %s"
a0d93e327   Yan, Zheng   ceph: remove dela...
1956
1957
  	     " issued %s revoking %s retain %s %s%s
  ", inode,
a8599bd82   Sage Weil   ceph: capability ...
1958
1959
1960
  	     ceph_cap_string(file_wanted),
  	     ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
  	     ceph_cap_string(ci->i_flushing_caps),
cbd036359   Sage Weil   ceph: cap revocat...
1961
  	     ceph_cap_string(issued), ceph_cap_string(revoking),
a8599bd82   Sage Weil   ceph: capability ...
1962
1963
  	     ceph_cap_string(retain),
  	     (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
a8599bd82   Sage Weil   ceph: capability ...
1964
1965
1966
1967
1968
1969
1970
  	     (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
  
  	/*
  	 * If we no longer need to hold onto old our caps, and we may
  	 * have cached pages, but don't want them, then try to invalidate.
  	 * If we fail, it's because pages are locked.... try again later.
  	 */
a0d93e327   Yan, Zheng   ceph: remove dela...
1971
  	if ((!(flags & CHECK_CAPS_NOINVAL) || mdsc->stopping) &&
525d15e8e   Yan, Zheng   ceph: check inode...
1972
  	    S_ISREG(inode->i_mode) &&
9abd4db71   Yan, Zheng   ceph: don't use t...
1973
  	    !(ci->i_wb_ref || ci->i_wrbuffer_ref) &&   /* no dirty pages... */
fdd4e1583   Yan, Zheng   ceph: rework dcac...
1974
  	    inode->i_data.nrpages &&		/* have cached pages */
5e804ac48   Yan, Zheng   ceph: don't inval...
1975
1976
  	    (revoking & (CEPH_CAP_FILE_CACHE|
  			 CEPH_CAP_FILE_LAZYIO)) && /*  or revoking cache */
a8599bd82   Sage Weil   ceph: capability ...
1977
  	    !tried_invalidate) {
a8599bd82   Sage Weil   ceph: capability ...
1978
1979
  		dout("check_caps trying to invalidate on %p
  ", inode);
5ecad6fd7   Sage Weil   ceph: fix check f...
1980
  		if (try_nonblocking_invalidate(inode) < 0) {
ee612d954   Yan, Zheng   ceph: delete unre...
1981
1982
1983
1984
  			dout("check_caps queuing invalidate
  ");
  			queue_invalidate = true;
  			ci->i_rdcache_revoking = ci->i_rdcache_gen;
a8599bd82   Sage Weil   ceph: capability ...
1985
  		}
3609404f8   Yan, Zheng   ceph: update type...
1986
  		tried_invalidate = true;
a8599bd82   Sage Weil   ceph: capability ...
1987
1988
  		goto retry_locked;
  	}
a8599bd82   Sage Weil   ceph: capability ...
1989
  	for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
d67c72e6c   Jeff Layton   ceph: request exp...
1990
  		int mflags = 0;
0a454bdd5   Jeff Layton   ceph: reorganize ...
1991
  		struct cap_msg_args arg;
a8599bd82   Sage Weil   ceph: capability ...
1992
  		cap = rb_entry(p, struct ceph_cap, ci_node);
a8599bd82   Sage Weil   ceph: capability ...
1993
1994
1995
1996
1997
1998
1999
  
  		/* avoid looping forever */
  		if (mds >= cap->mds ||
  		    ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
  			continue;
  
  		/* NOTE: no side-effects allowed, until we take s_mutex */
c74d79af9   Jeff Layton   ceph: comment cle...
2000
2001
2002
2003
  		/*
  		 * If we have an auth cap, we don't need to consider any
  		 * overlapping caps as used.
  		 */
395c312b9   Yan, Zheng   ceph: allow revok...
2004
2005
2006
  		cap_used = used;
  		if (ci->i_auth_cap && cap != ci->i_auth_cap)
  			cap_used &= ~ci->i_auth_cap->issued;
a8599bd82   Sage Weil   ceph: capability ...
2007
  		revoking = cap->implemented & ~cap->issued;
395c312b9   Yan, Zheng   ceph: allow revok...
2008
2009
  		dout(" mds%d cap %p used %s issued %s implemented %s revoking %s
  ",
9abd4db71   Yan, Zheng   ceph: don't use t...
2010
2011
  		     cap->mds, cap, ceph_cap_string(cap_used),
  		     ceph_cap_string(cap->issued),
088b3f5e9   Sage Weil   ceph: fix flushin...
2012
2013
  		     ceph_cap_string(cap->implemented),
  		     ceph_cap_string(revoking));
a8599bd82   Sage Weil   ceph: capability ...
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
  
  		if (cap == ci->i_auth_cap &&
  		    (cap->issued & CEPH_CAP_FILE_WR)) {
  			/* request larger max_size from MDS? */
  			if (ci->i_wanted_max_size > ci->i_max_size &&
  			    ci->i_wanted_max_size > ci->i_requested_max_size) {
  				dout("requesting new max_size
  ");
  				goto ack;
  			}
  
  			/* approaching file_max? */
efb0ca765   Yan, Zheng   ceph: update the ...
2026
  			if (__ceph_should_report_size(ci)) {
a8599bd82   Sage Weil   ceph: capability ...
2027
2028
2029
2030
2031
2032
  				dout("i_size approaching max_size
  ");
  				goto ack;
  			}
  		}
  		/* flush anything dirty? */
7bc00fddb   Yan, Zheng   ceph: kick cap fl...
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
  		if (cap == ci->i_auth_cap) {
  			if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
  				dout("flushing dirty caps
  ");
  				goto ack;
  			}
  			if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
  				dout("flushing snap caps
  ");
  				goto ack;
  			}
a8599bd82   Sage Weil   ceph: capability ...
2044
2045
2046
  		}
  
  		/* completed revocation? going down and there are no caps? */
395c312b9   Yan, Zheng   ceph: allow revok...
2047
  		if (revoking && (revoking & cap_used) == 0) {
a8599bd82   Sage Weil   ceph: capability ...
2048
2049
2050
2051
2052
2053
2054
  			dout("completed revocation of %s
  ",
  			     ceph_cap_string(cap->implemented & ~cap->issued));
  			goto ack;
  		}
  
  		/* want more caps from mds? */
0aa971b6f   Yan, Zheng   ceph: don't skip ...
2055
2056
2057
2058
2059
2060
  		if (want & ~cap->mds_wanted) {
  			if (want & ~(cap->mds_wanted | cap->issued))
  				goto ack;
  			if (!__cap_is_valid(cap))
  				goto ack;
  		}
a8599bd82   Sage Weil   ceph: capability ...
2061
2062
  
  		/* things we might delay */
fdac94fab   Yan, Zheng   ceph: skip updati...
2063
  		if ((cap->issued & ~retain) == 0)
a8599bd82   Sage Weil   ceph: capability ...
2064
  			continue;     /* nope, all good */
a8599bd82   Sage Weil   ceph: capability ...
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
  ack:
  		if (session && session != cap->session) {
  			dout("oops, wrong session %p mutex
  ", session);
  			mutex_unlock(&session->s_mutex);
  			session = NULL;
  		}
  		if (!session) {
  			session = cap->session;
  			if (mutex_trylock(&session->s_mutex) == 0) {
  				dout("inverting session/ino locks on %p
  ",
  				     session);
dc3da0461   Jeff Layton   ceph: fix potenti...
2078
  				session = ceph_get_mds_session(session);
be655596b   Sage Weil   ceph: use i_ceph_...
2079
  				spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2080
2081
2082
2083
  				if (took_snap_rwsem) {
  					up_read(&mdsc->snap_rwsem);
  					took_snap_rwsem = 0;
  				}
dc3da0461   Jeff Layton   ceph: fix potenti...
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
  				if (session) {
  					mutex_lock(&session->s_mutex);
  					ceph_put_mds_session(session);
  				} else {
  					/*
  					 * Because we take the reference while
  					 * holding the i_ceph_lock, it should
  					 * never be NULL. Throw a warning if it
  					 * ever is.
  					 */
  					WARN_ON_ONCE(true);
  				}
a8599bd82   Sage Weil   ceph: capability ...
2096
2097
2098
  				goto retry;
  			}
  		}
7bc00fddb   Yan, Zheng   ceph: kick cap fl...
2099
2100
2101
2102
2103
2104
  
  		/* kick flushing and flush snaps before sending normal
  		 * cap message */
  		if (cap == ci->i_auth_cap &&
  		    (ci->i_ceph_flags &
  		     (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
054f8d41a   Yan, Zheng   ceph: clear CEPH_...
2105
  			if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
24d063acc   Yan, Zheng   ceph: make sure f...
2106
  				__kick_flushing_caps(mdsc, session, ci, 0);
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
2107
2108
  			if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
  				__ceph_flush_snaps(ci, session);
7bc00fddb   Yan, Zheng   ceph: kick cap fl...
2109
2110
  			goto retry_locked;
  		}
a8599bd82   Sage Weil   ceph: capability ...
2111
2112
2113
2114
2115
2116
  		/* take snap_rwsem after session mutex */
  		if (!took_snap_rwsem) {
  			if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
  				dout("inverting snap/in locks on %p
  ",
  				     inode);
be655596b   Sage Weil   ceph: use i_ceph_...
2117
  				spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2118
2119
2120
2121
2122
2123
  				down_read(&mdsc->snap_rwsem);
  				took_snap_rwsem = 1;
  				goto retry;
  			}
  			took_snap_rwsem = 1;
  		}
553adfd94   Yan, Zheng   ceph: track pendi...
2124
  		if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
9f3345d8e   Jeff Layton   ceph: have __mark...
2125
2126
2127
  			flushing = ci->i_dirty_caps;
  			flush_tid = __mark_caps_flushing(inode, session, false,
  							 &oldest_flush_tid);
d67c72e6c   Jeff Layton   ceph: request exp...
2128
2129
2130
  			if (flags & CHECK_CAPS_FLUSH &&
  			    list_empty(&session->s_cap_dirty))
  				mflags |= CEPH_CLIENT_CAPS_SYNC;
553adfd94   Yan, Zheng   ceph: track pendi...
2131
  		} else {
24be0c481   Sage Weil   ceph: fix erroneo...
2132
  			flushing = 0;
553adfd94   Yan, Zheng   ceph: track pendi...
2133
  			flush_tid = 0;
a2971c8cc   Yan, Zheng   ceph: send TID of...
2134
2135
2136
  			spin_lock(&mdsc->cap_dirty_lock);
  			oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  			spin_unlock(&mdsc->cap_dirty_lock);
553adfd94   Yan, Zheng   ceph: track pendi...
2137
  		}
a8599bd82   Sage Weil   ceph: capability ...
2138
2139
  
  		mds = cap->mds;  /* remember mds, so we don't repeat */
a8599bd82   Sage Weil   ceph: capability ...
2140

d67c72e6c   Jeff Layton   ceph: request exp...
2141
2142
  		__prep_cap(&arg, cap, CEPH_CAP_OP_UPDATE, mflags, cap_used,
  			   want, retain, flushing, flush_tid, oldest_flush_tid);
0a454bdd5   Jeff Layton   ceph: reorganize ...
2143
  		spin_unlock(&ci->i_ceph_lock);
523119808   Jeff Layton   ceph: drop separa...
2144
  		__send_cap(&arg, ci);
0a454bdd5   Jeff Layton   ceph: reorganize ...
2145

be655596b   Sage Weil   ceph: use i_ceph_...
2146
  		goto retry; /* retake i_ceph_lock and restart our cap scan. */
a8599bd82   Sage Weil   ceph: capability ...
2147
  	}
a0d93e327   Yan, Zheng   ceph: remove dela...
2148
2149
2150
2151
2152
2153
  	/* periodically re-calculate caps wanted by open files */
  	if (__ceph_is_any_real_caps(ci) &&
  	    list_empty(&ci->i_cap_delay_list) &&
  	    (file_wanted & ~CEPH_CAP_PIN) &&
  	    !(used & (CEPH_CAP_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
  		__cap_delay_requeue(mdsc, ci);
719a2514e   Yan, Zheng   ceph: consider in...
2154
  	}
a8599bd82   Sage Weil   ceph: capability ...
2155

be655596b   Sage Weil   ceph: use i_ceph_...
2156
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2157

cbd036359   Sage Weil   ceph: cap revocat...
2158
  	if (queue_invalidate)
3c6f6b79a   Sage Weil   ceph: cleanup asy...
2159
  		ceph_queue_invalidate(inode);
cbd036359   Sage Weil   ceph: cap revocat...
2160

cdc2ce056   Sage Weil   ceph: fix session...
2161
  	if (session)
a8599bd82   Sage Weil   ceph: capability ...
2162
2163
2164
2165
2166
2167
  		mutex_unlock(&session->s_mutex);
  	if (took_snap_rwsem)
  		up_read(&mdsc->snap_rwsem);
  }
  
  /*
a8599bd82   Sage Weil   ceph: capability ...
2168
2169
   * Try to flush dirty caps back to the auth mds.
   */
553adfd94   Yan, Zheng   ceph: track pendi...
2170
  static int try_flush_caps(struct inode *inode, u64 *ptid)
a8599bd82   Sage Weil   ceph: capability ...
2171
  {
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
2172
  	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd82   Sage Weil   ceph: capability ...
2173
  	struct ceph_inode_info *ci = ceph_inode(inode);
4fe59789a   Yan, Zheng   ceph: handle cap ...
2174
  	struct ceph_mds_session *session = NULL;
89b52fe14   Yan, Zheng   ceph: fix flushin...
2175
  	int flushing = 0;
a2971c8cc   Yan, Zheng   ceph: send TID of...
2176
  	u64 flush_tid = 0, oldest_flush_tid = 0;
a8599bd82   Sage Weil   ceph: capability ...
2177
2178
  
  retry:
be655596b   Sage Weil   ceph: use i_ceph_...
2179
  	spin_lock(&ci->i_ceph_lock);
d6cee9dbd   Yan, Zheng   ceph: kick flushi...
2180
  retry_locked:
a8599bd82   Sage Weil   ceph: capability ...
2181
2182
  	if (ci->i_dirty_caps && ci->i_auth_cap) {
  		struct ceph_cap *cap = ci->i_auth_cap;
0a454bdd5   Jeff Layton   ceph: reorganize ...
2183
  		struct cap_msg_args arg;
a8599bd82   Sage Weil   ceph: capability ...
2184

27b0a3920   Jeff Layton   ceph: remove unne...
2185
  		if (session != cap->session) {
be655596b   Sage Weil   ceph: use i_ceph_...
2186
  			spin_unlock(&ci->i_ceph_lock);
4fe59789a   Yan, Zheng   ceph: handle cap ...
2187
2188
  			if (session)
  				mutex_unlock(&session->s_mutex);
a8599bd82   Sage Weil   ceph: capability ...
2189
2190
2191
2192
  			session = cap->session;
  			mutex_lock(&session->s_mutex);
  			goto retry;
  		}
6c2838fbd   Jeff Layton   ceph: unlock dang...
2193
2194
  		if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
  			spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2195
  			goto out;
6c2838fbd   Jeff Layton   ceph: unlock dang...
2196
  		}
a8599bd82   Sage Weil   ceph: capability ...
2197

d6cee9dbd   Yan, Zheng   ceph: kick flushi...
2198
2199
2200
2201
2202
2203
2204
2205
  		if (ci->i_ceph_flags &
  		    (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) {
  			if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
  				__kick_flushing_caps(mdsc, session, ci, 0);
  			if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
  				__ceph_flush_snaps(ci, session);
  			goto retry_locked;
  		}
9f3345d8e   Jeff Layton   ceph: have __mark...
2206
2207
2208
  		flushing = ci->i_dirty_caps;
  		flush_tid = __mark_caps_flushing(inode, session, true,
  						 &oldest_flush_tid);
a8599bd82   Sage Weil   ceph: capability ...
2209

0a454bdd5   Jeff Layton   ceph: reorganize ...
2210
  		__prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, CEPH_CLIENT_CAPS_SYNC,
a0d93e327   Yan, Zheng   ceph: remove dela...
2211
2212
2213
  			   __ceph_caps_used(ci), __ceph_caps_wanted(ci),
  			   (cap->issued | cap->implemented),
  			   flushing, flush_tid, oldest_flush_tid);
0a454bdd5   Jeff Layton   ceph: reorganize ...
2214
  		spin_unlock(&ci->i_ceph_lock);
523119808   Jeff Layton   ceph: drop separa...
2215
  		__send_cap(&arg, ci);
553adfd94   Yan, Zheng   ceph: track pendi...
2216
  	} else {
e4500b5e3   Yan, Zheng   ceph: use list in...
2217
  		if (!list_empty(&ci->i_cap_flush_list)) {
553adfd94   Yan, Zheng   ceph: track pendi...
2218
  			struct ceph_cap_flush *cf =
e4500b5e3   Yan, Zheng   ceph: use list in...
2219
  				list_last_entry(&ci->i_cap_flush_list,
c8799fc46   Yan, Zheng   ceph: optimize ca...
2220
2221
  						struct ceph_cap_flush, i_list);
  			cf->wake = true;
553adfd94   Yan, Zheng   ceph: track pendi...
2222
2223
2224
2225
  			flush_tid = cf->tid;
  		}
  		flushing = ci->i_flushing_caps;
  		spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2226
2227
  	}
  out:
4fe59789a   Yan, Zheng   ceph: handle cap ...
2228
  	if (session)
a8599bd82   Sage Weil   ceph: capability ...
2229
  		mutex_unlock(&session->s_mutex);
553adfd94   Yan, Zheng   ceph: track pendi...
2230
2231
  
  	*ptid = flush_tid;
a8599bd82   Sage Weil   ceph: capability ...
2232
2233
2234
2235
2236
2237
  	return flushing;
  }
  
  /*
   * Return true if we've flushed caps through the given flush_tid.
   */
553adfd94   Yan, Zheng   ceph: track pendi...
2238
  static int caps_are_flushed(struct inode *inode, u64 flush_tid)
a8599bd82   Sage Weil   ceph: capability ...
2239
2240
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
553adfd94   Yan, Zheng   ceph: track pendi...
2241
  	int ret = 1;
a8599bd82   Sage Weil   ceph: capability ...
2242

be655596b   Sage Weil   ceph: use i_ceph_...
2243
  	spin_lock(&ci->i_ceph_lock);
e4500b5e3   Yan, Zheng   ceph: use list in...
2244
2245
2246
2247
  	if (!list_empty(&ci->i_cap_flush_list)) {
  		struct ceph_cap_flush * cf =
  			list_first_entry(&ci->i_cap_flush_list,
  					 struct ceph_cap_flush, i_list);
553adfd94   Yan, Zheng   ceph: track pendi...
2248
  		if (cf->tid <= flush_tid)
a8599bd82   Sage Weil   ceph: capability ...
2249
  			ret = 0;
89b52fe14   Yan, Zheng   ceph: fix flushin...
2250
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
2251
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2252
2253
2254
2255
  	return ret;
  }
  
  /*
68cd5b4b7   Yan, Zheng   ceph: make fsync(...
2256
   * wait for any unsafe requests to complete.
da819c815   Yan, Zheng   ceph: fix directo...
2257
   */
68cd5b4b7   Yan, Zheng   ceph: make fsync(...
2258
  static int unsafe_request_wait(struct inode *inode)
da819c815   Yan, Zheng   ceph: fix directo...
2259
2260
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
68cd5b4b7   Yan, Zheng   ceph: make fsync(...
2261
2262
  	struct ceph_mds_request *req1 = NULL, *req2 = NULL;
  	int ret, err = 0;
da819c815   Yan, Zheng   ceph: fix directo...
2263
2264
  
  	spin_lock(&ci->i_unsafe_lock);
68cd5b4b7   Yan, Zheng   ceph: make fsync(...
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
  	if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
  		req1 = list_last_entry(&ci->i_unsafe_dirops,
  					struct ceph_mds_request,
  					r_unsafe_dir_item);
  		ceph_mdsc_get_request(req1);
  	}
  	if (!list_empty(&ci->i_unsafe_iops)) {
  		req2 = list_last_entry(&ci->i_unsafe_iops,
  					struct ceph_mds_request,
  					r_unsafe_target_item);
  		ceph_mdsc_get_request(req2);
  	}
  	spin_unlock(&ci->i_unsafe_lock);
da819c815   Yan, Zheng   ceph: fix directo...
2278

4945a0847   Jeff Layton   ceph: fix minor t...
2279
2280
  	dout("unsafe_request_wait %p wait on tid %llu %llu
  ",
68cd5b4b7   Yan, Zheng   ceph: make fsync(...
2281
2282
2283
2284
  	     inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
  	if (req1) {
  		ret = !wait_for_completion_timeout(&req1->r_safe_completion,
  					ceph_timeout_jiffies(req1->r_timeout));
da819c815   Yan, Zheng   ceph: fix directo...
2285
  		if (ret)
68cd5b4b7   Yan, Zheng   ceph: make fsync(...
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
  			err = -EIO;
  		ceph_mdsc_put_request(req1);
  	}
  	if (req2) {
  		ret = !wait_for_completion_timeout(&req2->r_safe_completion,
  					ceph_timeout_jiffies(req2->r_timeout));
  		if (ret)
  			err = -EIO;
  		ceph_mdsc_put_request(req2);
  	}
  	return err;
da819c815   Yan, Zheng   ceph: fix directo...
2297
  }
02c24a821   Josef Bacik   fs: push i_mutex ...
2298
  int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
a8599bd82   Sage Weil   ceph: capability ...
2299
  {
f4b978662   Yan, Zheng   ceph: track and r...
2300
  	struct ceph_file_info *fi = file->private_data;
7ea808591   Christoph Hellwig   drop unused dentr...
2301
  	struct inode *inode = file->f_mapping->host;
a8599bd82   Sage Weil   ceph: capability ...
2302
  	struct ceph_inode_info *ci = ceph_inode(inode);
553adfd94   Yan, Zheng   ceph: track pendi...
2303
  	u64 flush_tid;
f4b978662   Yan, Zheng   ceph: track and r...
2304
  	int ret, err;
a8599bd82   Sage Weil   ceph: capability ...
2305
2306
2307
2308
  	int dirty;
  
  	dout("fsync %p%s
  ", inode, datasync ? " datasync" : "");
9a5530c63   Yan, Zheng   ceph: wait unsafe...
2309

b74fceae7   Jeff Layton   ceph: use errseq_...
2310
  	ret = file_write_and_wait_range(file, start, end);
da819c815   Yan, Zheng   ceph: fix directo...
2311
2312
  	if (datasync)
  		goto out;
891f3f5a6   Jeff Layton   ceph: add infrast...
2313
2314
2315
  	ret = ceph_wait_on_async_create(inode);
  	if (ret)
  		goto out;
553adfd94   Yan, Zheng   ceph: track pendi...
2316
  	dirty = try_flush_caps(inode, &flush_tid);
a8599bd82   Sage Weil   ceph: capability ...
2317
2318
  	dout("fsync dirty caps are %s
  ", ceph_cap_string(dirty));
f4b978662   Yan, Zheng   ceph: track and r...
2319
  	err = unsafe_request_wait(inode);
da819c815   Yan, Zheng   ceph: fix directo...
2320

a8599bd82   Sage Weil   ceph: capability ...
2321
2322
2323
2324
2325
  	/*
  	 * only wait on non-file metadata writeback (the mds
  	 * can recover size and mtime, so we don't need to
  	 * wait for that)
  	 */
f4b978662   Yan, Zheng   ceph: track and r...
2326
2327
  	if (!err && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
  		err = wait_event_interruptible(ci->i_cap_wq,
da819c815   Yan, Zheng   ceph: fix directo...
2328
  					caps_are_flushed(inode, flush_tid));
a8599bd82   Sage Weil   ceph: capability ...
2329
  	}
f4b978662   Yan, Zheng   ceph: track and r...
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
  
  	if (err < 0)
  		ret = err;
  
  	if (errseq_check(&ci->i_meta_err, READ_ONCE(fi->meta_err))) {
  		spin_lock(&file->f_lock);
  		err = errseq_check_and_advance(&ci->i_meta_err,
  					       &fi->meta_err);
  		spin_unlock(&file->f_lock);
  		if (err < 0)
  			ret = err;
  	}
da819c815   Yan, Zheng   ceph: fix directo...
2342
2343
2344
  out:
  	dout("fsync %p%s result=%d
  ", inode, datasync ? " datasync" : "", ret);
a8599bd82   Sage Weil   ceph: capability ...
2345
2346
2347
2348
2349
2350
2351
2352
2353
  	return ret;
  }
  
  /*
   * Flush any dirty caps back to the mds.  If we aren't asked to wait,
   * queue inode for flush but don't do so immediately, because we can
   * get by with fewer MDS messages if we wait for data writeback to
   * complete first.
   */
f1a3d5721   Stephen Rothwell   ceph: update for ...
2354
  int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
a8599bd82   Sage Weil   ceph: capability ...
2355
2356
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
553adfd94   Yan, Zheng   ceph: track pendi...
2357
  	u64 flush_tid;
a8599bd82   Sage Weil   ceph: capability ...
2358
2359
  	int err = 0;
  	int dirty;
16515a6d5   Chengguang Xu   ceph: improving e...
2360
  	int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
a8599bd82   Sage Weil   ceph: capability ...
2361
2362
2363
2364
  
  	dout("write_inode %p wait=%d
  ", inode, wait);
  	if (wait) {
553adfd94   Yan, Zheng   ceph: track pendi...
2365
  		dirty = try_flush_caps(inode, &flush_tid);
a8599bd82   Sage Weil   ceph: capability ...
2366
2367
2368
2369
  		if (dirty)
  			err = wait_event_interruptible(ci->i_cap_wq,
  				       caps_are_flushed(inode, flush_tid));
  	} else {
640ef79d2   Cheng Renquan   ceph: use ceph_sb...
2370
  		struct ceph_mds_client *mdsc =
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
2371
  			ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd82   Sage Weil   ceph: capability ...
2372

be655596b   Sage Weil   ceph: use i_ceph_...
2373
  		spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2374
2375
  		if (__ceph_caps_dirty(ci))
  			__cap_delay_requeue_front(mdsc, ci);
be655596b   Sage Weil   ceph: use i_ceph_...
2376
  		spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2377
2378
2379
  	}
  	return err;
  }
0e2943878   Yan, Zheng   ceph: unify cap f...
2380
2381
2382
2383
2384
2385
  static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
  				 struct ceph_mds_session *session,
  				 struct ceph_inode_info *ci,
  				 u64 oldest_flush_tid)
  	__releases(ci->i_ceph_lock)
  	__acquires(ci->i_ceph_lock)
553adfd94   Yan, Zheng   ceph: track pendi...
2386
2387
2388
2389
  {
  	struct inode *inode = &ci->vfs_inode;
  	struct ceph_cap *cap;
  	struct ceph_cap_flush *cf;
0e2943878   Yan, Zheng   ceph: unify cap f...
2390
  	int ret;
553adfd94   Yan, Zheng   ceph: track pendi...
2391
  	u64 first_tid = 0;
49ada6e8d   Yan, Zheng   ceph: more precis...
2392
  	u64 last_snap_flush = 0;
553adfd94   Yan, Zheng   ceph: track pendi...
2393

054f8d41a   Yan, Zheng   ceph: clear CEPH_...
2394
  	ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
49ada6e8d   Yan, Zheng   ceph: more precis...
2395
2396
2397
2398
2399
2400
  	list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) {
  		if (!cf->caps) {
  			last_snap_flush = cf->tid;
  			break;
  		}
  	}
e4500b5e3   Yan, Zheng   ceph: use list in...
2401
2402
2403
  	list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
  		if (cf->tid < first_tid)
  			continue;
553adfd94   Yan, Zheng   ceph: track pendi...
2404
2405
  		cap = ci->i_auth_cap;
  		if (!(cap && cap->session == session)) {
0e2943878   Yan, Zheng   ceph: unify cap f...
2406
2407
2408
  			pr_err("%p auth cap %p not mds%d ???
  ",
  			       inode, cap, session->s_mds);
553adfd94   Yan, Zheng   ceph: track pendi...
2409
2410
  			break;
  		}
553adfd94   Yan, Zheng   ceph: track pendi...
2411
  		first_tid = cf->tid + 1;
0e2943878   Yan, Zheng   ceph: unify cap f...
2412
  		if (cf->caps) {
0a454bdd5   Jeff Layton   ceph: reorganize ...
2413
  			struct cap_msg_args arg;
0e2943878   Yan, Zheng   ceph: unify cap f...
2414
2415
2416
  			dout("kick_flushing_caps %p cap %p tid %llu %s
  ",
  			     inode, cap, cf->tid, ceph_cap_string(cf->caps));
0a454bdd5   Jeff Layton   ceph: reorganize ...
2417
  			__prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH,
49ada6e8d   Yan, Zheng   ceph: more precis...
2418
2419
2420
  					 (cf->tid < last_snap_flush ?
  					  CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0),
  					  __ceph_caps_used(ci),
0e2943878   Yan, Zheng   ceph: unify cap f...
2421
  					  __ceph_caps_wanted(ci),
49ada6e8d   Yan, Zheng   ceph: more precis...
2422
  					  (cap->issued | cap->implemented),
0e2943878   Yan, Zheng   ceph: unify cap f...
2423
  					  cf->caps, cf->tid, oldest_flush_tid);
0a454bdd5   Jeff Layton   ceph: reorganize ...
2424
  			spin_unlock(&ci->i_ceph_lock);
523119808   Jeff Layton   ceph: drop separa...
2425
  			__send_cap(&arg, ci);
0e2943878   Yan, Zheng   ceph: unify cap f...
2426
2427
2428
2429
2430
2431
2432
2433
  		} else {
  			struct ceph_cap_snap *capsnap =
  					container_of(cf, struct ceph_cap_snap,
  						    cap_flush);
  			dout("kick_flushing_caps %p capsnap %p tid %llu %s
  ",
  			     inode, capsnap, cf->tid,
  			     ceph_cap_string(capsnap->dirty));
805692d0e   Elena Reshetova   ceph: convert cep...
2434
  			refcount_inc(&capsnap->nref);
0e2943878   Yan, Zheng   ceph: unify cap f...
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
  			spin_unlock(&ci->i_ceph_lock);
  
  			ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
  						oldest_flush_tid);
  			if (ret < 0) {
  				pr_err("kick_flushing_caps: error sending "
  					"cap flushsnap, ino (%llx.%llx) "
  					"tid %llu follows %llu
  ",
  					ceph_vinop(inode), cf->tid,
  					capsnap->follows);
  			}
  
  			ceph_put_cap_snap(capsnap);
  		}
e4500b5e3   Yan, Zheng   ceph: use list in...
2450
2451
  
  		spin_lock(&ci->i_ceph_lock);
553adfd94   Yan, Zheng   ceph: track pendi...
2452
  	}
553adfd94   Yan, Zheng   ceph: track pendi...
2453
  }
e548e9b93   Yan, Zheng   ceph: re-send flu...
2454
2455
2456
2457
2458
  void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
  				   struct ceph_mds_session *session)
  {
  	struct ceph_inode_info *ci;
  	struct ceph_cap *cap;
0e2943878   Yan, Zheng   ceph: unify cap f...
2459
  	u64 oldest_flush_tid;
e548e9b93   Yan, Zheng   ceph: re-send flu...
2460
2461
2462
  
  	dout("early_kick_flushing_caps mds%d
  ", session->s_mds);
0e2943878   Yan, Zheng   ceph: unify cap f...
2463
2464
2465
2466
  
  	spin_lock(&mdsc->cap_dirty_lock);
  	oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  	spin_unlock(&mdsc->cap_dirty_lock);
e548e9b93   Yan, Zheng   ceph: re-send flu...
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
  	list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
  		spin_lock(&ci->i_ceph_lock);
  		cap = ci->i_auth_cap;
  		if (!(cap && cap->session == session)) {
  			pr_err("%p auth cap %p not mds%d ???
  ",
  				&ci->vfs_inode, cap, session->s_mds);
  			spin_unlock(&ci->i_ceph_lock);
  			continue;
  		}
  
  
  		/*
  		 * if flushing caps were revoked, we re-send the cap flush
  		 * in client reconnect stage. This guarantees MDS * processes
  		 * the cap flush message before issuing the flushing caps to
  		 * other client.
  		 */
  		if ((cap->issued & ci->i_flushing_caps) !=
  		    ci->i_flushing_caps) {
81c5a1487   Yan, Zheng   ceph: split large...
2487
2488
2489
2490
2491
2492
  			/* encode_caps_cb() also will reset these sequence
  			 * numbers. make sure sequence numbers in cap flush
  			 * message match later reconnect message */
  			cap->seq = 0;
  			cap->issue_seq = 0;
  			cap->mseq = 0;
0e2943878   Yan, Zheng   ceph: unify cap f...
2493
2494
  			__kick_flushing_caps(mdsc, session, ci,
  					     oldest_flush_tid);
13c2b57d8   Yan, Zheng   ceph: avoid sendi...
2495
2496
  		} else {
  			ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
e548e9b93   Yan, Zheng   ceph: re-send flu...
2497
  		}
e548e9b93   Yan, Zheng   ceph: re-send flu...
2498
2499
2500
  		spin_unlock(&ci->i_ceph_lock);
  	}
  }
a8599bd82   Sage Weil   ceph: capability ...
2501
2502
2503
2504
  void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  			     struct ceph_mds_session *session)
  {
  	struct ceph_inode_info *ci;
13c2b57d8   Yan, Zheng   ceph: avoid sendi...
2505
  	struct ceph_cap *cap;
0e2943878   Yan, Zheng   ceph: unify cap f...
2506
  	u64 oldest_flush_tid;
a8599bd82   Sage Weil   ceph: capability ...
2507

829ad4db9   Jeff Layton   ceph: ceph_kick_f...
2508
  	lockdep_assert_held(&session->s_mutex);
a8599bd82   Sage Weil   ceph: capability ...
2509
2510
  	dout("kick_flushing_caps mds%d
  ", session->s_mds);
0e2943878   Yan, Zheng   ceph: unify cap f...
2511
2512
2513
2514
  
  	spin_lock(&mdsc->cap_dirty_lock);
  	oldest_flush_tid = __get_oldest_flush_tid(mdsc);
  	spin_unlock(&mdsc->cap_dirty_lock);
a8599bd82   Sage Weil   ceph: capability ...
2515
  	list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
0e2943878   Yan, Zheng   ceph: unify cap f...
2516
  		spin_lock(&ci->i_ceph_lock);
13c2b57d8   Yan, Zheng   ceph: avoid sendi...
2517
2518
2519
2520
2521
2522
2523
2524
2525
  		cap = ci->i_auth_cap;
  		if (!(cap && cap->session == session)) {
  			pr_err("%p auth cap %p not mds%d ???
  ",
  				&ci->vfs_inode, cap, session->s_mds);
  			spin_unlock(&ci->i_ceph_lock);
  			continue;
  		}
  		if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
13c2b57d8   Yan, Zheng   ceph: avoid sendi...
2526
2527
2528
  			__kick_flushing_caps(mdsc, session, ci,
  					     oldest_flush_tid);
  		}
0e2943878   Yan, Zheng   ceph: unify cap f...
2529
  		spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2530
2531
  	}
  }
e8a4d2677   Jeff Layton   ceph: clean up ki...
2532
2533
  void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session,
  				   struct ceph_inode_info *ci)
088b3f5e9   Sage Weil   ceph: fix flushin...
2534
  {
e8a4d2677   Jeff Layton   ceph: clean up ki...
2535
2536
2537
2538
  	struct ceph_mds_client *mdsc = session->s_mdsc;
  	struct ceph_cap *cap = ci->i_auth_cap;
  
  	lockdep_assert_held(&ci->i_ceph_lock);
088b3f5e9   Sage Weil   ceph: fix flushin...
2539

e8a4d2677   Jeff Layton   ceph: clean up ki...
2540
2541
  	dout("%s %p flushing %s
  ", __func__, &ci->vfs_inode,
8310b0891   Yan, Zheng   ceph: track pendi...
2542
  	     ceph_cap_string(ci->i_flushing_caps));
005c46970   Yan, Zheng   ceph: move inode ...
2543

0e2943878   Yan, Zheng   ceph: unify cap f...
2544
2545
  	if (!list_empty(&ci->i_cap_flush_list)) {
  		u64 oldest_flush_tid;
005c46970   Yan, Zheng   ceph: move inode ...
2546
2547
2548
  		spin_lock(&mdsc->cap_dirty_lock);
  		list_move_tail(&ci->i_flushing_item,
  			       &cap->session->s_cap_flushing);
0e2943878   Yan, Zheng   ceph: unify cap f...
2549
  		oldest_flush_tid = __get_oldest_flush_tid(mdsc);
005c46970   Yan, Zheng   ceph: move inode ...
2550
  		spin_unlock(&mdsc->cap_dirty_lock);
0e2943878   Yan, Zheng   ceph: unify cap f...
2551
  		__kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
088b3f5e9   Sage Weil   ceph: fix flushin...
2552
2553
  	}
  }
a8599bd82   Sage Weil   ceph: capability ...
2554
2555
2556
2557
  
  /*
   * Take references to capabilities we hold, so that we don't release
   * them to the MDS prematurely.
a8599bd82   Sage Weil   ceph: capability ...
2558
   */
40dcf75e8   Jeff Layton   ceph: make __take...
2559
  void ceph_take_cap_refs(struct ceph_inode_info *ci, int got,
5dda377cf   Yan, Zheng   ceph: set i_head_...
2560
  			    bool snap_rwsem_locked)
a8599bd82   Sage Weil   ceph: capability ...
2561
  {
40dcf75e8   Jeff Layton   ceph: make __take...
2562
  	lockdep_assert_held(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2563
2564
2565
2566
2567
2568
  	if (got & CEPH_CAP_PIN)
  		ci->i_pin_ref++;
  	if (got & CEPH_CAP_FILE_RD)
  		ci->i_rd_ref++;
  	if (got & CEPH_CAP_FILE_CACHE)
  		ci->i_rdcache_ref++;
f85122afe   Jeff Layton   ceph: add refcoun...
2569
2570
  	if (got & CEPH_CAP_FILE_EXCL)
  		ci->i_fx_ref++;
5dda377cf   Yan, Zheng   ceph: set i_head_...
2571
2572
2573
2574
2575
2576
  	if (got & CEPH_CAP_FILE_WR) {
  		if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
  			BUG_ON(!snap_rwsem_locked);
  			ci->i_head_snapc = ceph_get_snap_context(
  					ci->i_snap_realm->cached_context);
  		}
a8599bd82   Sage Weil   ceph: capability ...
2577
  		ci->i_wr_ref++;
5dda377cf   Yan, Zheng   ceph: set i_head_...
2578
  	}
a8599bd82   Sage Weil   ceph: capability ...
2579
  	if (got & CEPH_CAP_FILE_BUFFER) {
d3d0720d4   Henry C Chang   ceph: do not use ...
2580
  		if (ci->i_wb_ref == 0)
3772d26d8   Sage Weil   ceph: use ihold()...
2581
  			ihold(&ci->vfs_inode);
d3d0720d4   Henry C Chang   ceph: do not use ...
2582
  		ci->i_wb_ref++;
40dcf75e8   Jeff Layton   ceph: make __take...
2583
2584
  		dout("%s %p wb %d -> %d (?)
  ", __func__,
d3d0720d4   Henry C Chang   ceph: do not use ...
2585
  		     &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
a8599bd82   Sage Weil   ceph: capability ...
2586
2587
2588
2589
2590
2591
2592
2593
2594
  	}
  }
  
  /*
   * Try to grab cap references.  Specify those refs we @want, and the
   * minimal set we @need.  Also include the larger offset we are writing
   * to (when applicable), and check against max_size here as well.
   * Note that caller is responsible for ensuring max_size increases are
   * requested from the MDS.
1199d7da2   Jeff Layton   ceph: simplify ar...
2595
   *
546d40208   Yan, Zheng   ceph: cleanup ret...
2596
2597
2598
2599
2600
   * Returns 0 if caps were not able to be acquired (yet), 1 if succeed,
   * or a negative error code. There are 3 speical error codes:
   *  -EAGAIN: need to sleep but non-blocking is specified
   *  -EFBIG:  ask caller to call check_max_size() and try again.
   *  -ESTALE: ask caller to call ceph_renew_caps() and try again.
a8599bd82   Sage Weil   ceph: capability ...
2601
   */
ff5d913df   Yan, Zheng   ceph: return -EIO...
2602
  enum {
719a2514e   Yan, Zheng   ceph: consider in...
2603
2604
2605
  	/* first 8 bits are reserved for CEPH_FILE_MODE_FOO */
  	NON_BLOCKING	= (1 << 8),
  	CHECK_FILELOCK	= (1 << 9),
ff5d913df   Yan, Zheng   ceph: return -EIO...
2606
  };
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2607
  static int try_get_cap_refs(struct inode *inode, int need, int want,
ff5d913df   Yan, Zheng   ceph: return -EIO...
2608
  			    loff_t endoff, int flags, int *got)
a8599bd82   Sage Weil   ceph: capability ...
2609
  {
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2610
  	struct ceph_inode_info *ci = ceph_inode(inode);
5dda377cf   Yan, Zheng   ceph: set i_head_...
2611
  	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
a8599bd82   Sage Weil   ceph: capability ...
2612
  	int ret = 0;
c4d4a582c   Yan, Zheng   ceph: avoid block...
2613
  	int have, implemented;
5dda377cf   Yan, Zheng   ceph: set i_head_...
2614
  	bool snap_rwsem_locked = false;
a8599bd82   Sage Weil   ceph: capability ...
2615
2616
2617
2618
  
  	dout("get_cap_refs %p need %s want %s
  ", inode,
  	     ceph_cap_string(need), ceph_cap_string(want));
c4d4a582c   Yan, Zheng   ceph: avoid block...
2619

5dda377cf   Yan, Zheng   ceph: set i_head_...
2620
  again:
be655596b   Sage Weil   ceph: use i_ceph_...
2621
  	spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2622

ff5d913df   Yan, Zheng   ceph: return -EIO...
2623
2624
2625
2626
2627
2628
2629
  	if ((flags & CHECK_FILELOCK) &&
  	    (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK)) {
  		dout("try_get_cap_refs %p error filelock
  ", inode);
  		ret = -EIO;
  		goto out_unlock;
  	}
37505d576   Yan, Zheng   ceph: take i_mute...
2630
2631
2632
  	/* finish pending truncate */
  	while (ci->i_truncate_pending) {
  		spin_unlock(&ci->i_ceph_lock);
5dda377cf   Yan, Zheng   ceph: set i_head_...
2633
2634
2635
2636
  		if (snap_rwsem_locked) {
  			up_read(&mdsc->snap_rwsem);
  			snap_rwsem_locked = false;
  		}
b415bf4f9   Yan, Zheng   ceph: fix pending...
2637
  		__ceph_do_pending_vmtruncate(inode);
37505d576   Yan, Zheng   ceph: take i_mute...
2638
2639
  		spin_lock(&ci->i_ceph_lock);
  	}
3871cbb9a   Yan, Zheng   ceph: fix request...
2640
2641
2642
  	have = __ceph_caps_issued(ci, &implemented);
  
  	if (have & need & CEPH_CAP_FILE_WR) {
a8599bd82   Sage Weil   ceph: capability ...
2643
2644
2645
2646
  		if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
  			dout("get_cap_refs %p endoff %llu > maxsize %llu
  ",
  			     inode, endoff, ci->i_max_size);
1199d7da2   Jeff Layton   ceph: simplify ar...
2647
  			if (endoff > ci->i_requested_max_size)
42d70f8e3   Yan, Zheng   ceph: request new...
2648
  				ret = ci->i_auth_cap ? -EFBIG : -ESTALE;
3738daa68   Yan, Zheng   ceph: fetch inlin...
2649
  			goto out_unlock;
a8599bd82   Sage Weil   ceph: capability ...
2650
2651
2652
2653
2654
2655
2656
2657
  		}
  		/*
  		 * If a sync write is in progress, we must wait, so that we
  		 * can get a final snapshot value for size+mtime.
  		 */
  		if (__ceph_have_pending_cap_snap(ci)) {
  			dout("get_cap_refs %p cap_snap_pending
  ", inode);
3738daa68   Yan, Zheng   ceph: fetch inlin...
2658
  			goto out_unlock;
a8599bd82   Sage Weil   ceph: capability ...
2659
2660
  		}
  	}
a8599bd82   Sage Weil   ceph: capability ...
2661

a8599bd82   Sage Weil   ceph: capability ...
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
  	if ((have & need) == need) {
  		/*
  		 * Look at (implemented & ~have & not) so that we keep waiting
  		 * on transition from wanted -> needed caps.  This is needed
  		 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
  		 * going before a prior buffered writeback happens.
  		 */
  		int not = want & ~(have & need);
  		int revoking = implemented & ~have;
  		dout("get_cap_refs %p have %s but not %s (revoking %s)
  ",
  		     inode, ceph_cap_string(have), ceph_cap_string(not),
  		     ceph_cap_string(revoking));
  		if ((revoking & not) == 0) {
5dda377cf   Yan, Zheng   ceph: set i_head_...
2676
2677
2678
2679
2680
2681
2682
2683
  			if (!snap_rwsem_locked &&
  			    !ci->i_head_snapc &&
  			    (need & CEPH_CAP_FILE_WR)) {
  				if (!down_read_trylock(&mdsc->snap_rwsem)) {
  					/*
  					 * we can not call down_read() when
  					 * task isn't in TASK_RUNNING state
  					 */
ff5d913df   Yan, Zheng   ceph: return -EIO...
2684
  					if (flags & NON_BLOCKING) {
1199d7da2   Jeff Layton   ceph: simplify ar...
2685
  						ret = -EAGAIN;
5dda377cf   Yan, Zheng   ceph: set i_head_...
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
  						goto out_unlock;
  					}
  
  					spin_unlock(&ci->i_ceph_lock);
  					down_read(&mdsc->snap_rwsem);
  					snap_rwsem_locked = true;
  					goto again;
  				}
  				snap_rwsem_locked = true;
  			}
173e70e8a   Yan, Zheng   ceph: don't take ...
2696
2697
2698
2699
  			if ((have & want) == want)
  				*got = need | want;
  			else
  				*got = need;
525d15e8e   Yan, Zheng   ceph: check inode...
2700
2701
  			if (S_ISREG(inode->i_mode) &&
  			    (need & CEPH_CAP_FILE_RD) &&
f7f7e7a06   Yan, Zheng   ceph: improve fsc...
2702
2703
  			    !(*got & CEPH_CAP_FILE_CACHE))
  				ceph_disable_fscache_readpage(ci);
40dcf75e8   Jeff Layton   ceph: make __take...
2704
  			ceph_take_cap_refs(ci, *got, true);
a8599bd82   Sage Weil   ceph: capability ...
2705
2706
2707
  			ret = 1;
  		}
  	} else {
03f4fcb02   Yan, Zheng   ceph: handle SESS...
2708
  		int session_readonly = false;
c0e385b10   Yan, Zheng   ceph: always rene...
2709
  		int mds_wanted;
525d15e8e   Yan, Zheng   ceph: check inode...
2710
2711
  		if (ci->i_auth_cap &&
  		    (need & (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_EXCL))) {
03f4fcb02   Yan, Zheng   ceph: handle SESS...
2712
2713
2714
2715
2716
2717
  			struct ceph_mds_session *s = ci->i_auth_cap->session;
  			spin_lock(&s->s_cap_lock);
  			session_readonly = s->s_readonly;
  			spin_unlock(&s->s_cap_lock);
  		}
  		if (session_readonly) {
c0e385b10   Yan, Zheng   ceph: always rene...
2718
2719
  			dout("get_cap_refs %p need %s but mds%d readonly
  ",
03f4fcb02   Yan, Zheng   ceph: handle SESS...
2720
  			     inode, ceph_cap_string(need), ci->i_auth_cap->mds);
1199d7da2   Jeff Layton   ceph: simplify ar...
2721
  			ret = -EROFS;
03f4fcb02   Yan, Zheng   ceph: handle SESS...
2722
2723
  			goto out_unlock;
  		}
c0e385b10   Yan, Zheng   ceph: always rene...
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
  		if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  			dout("get_cap_refs %p forced umount
  ", inode);
  			ret = -EIO;
  			goto out_unlock;
  		}
  		mds_wanted = __ceph_caps_mds_wanted(ci, false);
  		if (need & ~mds_wanted) {
  			dout("get_cap_refs %p need %s > mds_wanted %s
  ",
  			     inode, ceph_cap_string(need),
  			     ceph_cap_string(mds_wanted));
  			ret = -ESTALE;
  			goto out_unlock;
48fec5d0a   Yan, Zheng   ceph: EIO all ope...
2738
  		}
c0e385b10   Yan, Zheng   ceph: always rene...
2739
2740
  		dout("get_cap_refs %p have %s need %s
  ", inode,
a8599bd82   Sage Weil   ceph: capability ...
2741
2742
  		     ceph_cap_string(have), ceph_cap_string(need));
  	}
3738daa68   Yan, Zheng   ceph: fetch inlin...
2743
  out_unlock:
719a2514e   Yan, Zheng   ceph: consider in...
2744
2745
  
  	__ceph_touch_fmode(ci, mdsc, flags);
be655596b   Sage Weil   ceph: use i_ceph_...
2746
  	spin_unlock(&ci->i_ceph_lock);
5dda377cf   Yan, Zheng   ceph: set i_head_...
2747
2748
  	if (snap_rwsem_locked)
  		up_read(&mdsc->snap_rwsem);
3738daa68   Yan, Zheng   ceph: fetch inlin...
2749

1af16d547   Xiubo Li   ceph: add caps pe...
2750
2751
2752
2753
  	if (!ret)
  		ceph_update_cap_mis(&mdsc->metric);
  	else if (ret == 1)
  		ceph_update_cap_hit(&mdsc->metric);
a8599bd82   Sage Weil   ceph: capability ...
2754
2755
  	dout("get_cap_refs %p ret %d got %s
  ", inode,
c4d4a582c   Yan, Zheng   ceph: avoid block...
2756
  	     ret, ceph_cap_string(*got));
a8599bd82   Sage Weil   ceph: capability ...
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
  	return ret;
  }
  
  /*
   * Check the offset we are writing up to against our current
   * max_size.  If necessary, tell the MDS we want to write to
   * a larger offset.
   */
  static void check_max_size(struct inode *inode, loff_t endoff)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int check = 0;
  
  	/* do we need to explicitly request a larger max_size? */
be655596b   Sage Weil   ceph: use i_ceph_...
2771
  	spin_lock(&ci->i_ceph_lock);
3871cbb9a   Yan, Zheng   ceph: fix request...
2772
  	if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
a8599bd82   Sage Weil   ceph: capability ...
2773
2774
2775
2776
  		dout("write %p at large endoff %llu, req max_size
  ",
  		     inode, endoff);
  		ci->i_wanted_max_size = endoff;
a8599bd82   Sage Weil   ceph: capability ...
2777
  	}
3871cbb9a   Yan, Zheng   ceph: fix request...
2778
2779
2780
2781
2782
2783
  	/* duplicate ceph_check_caps()'s logic */
  	if (ci->i_auth_cap &&
  	    (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
  	    ci->i_wanted_max_size > ci->i_max_size &&
  	    ci->i_wanted_max_size > ci->i_requested_max_size)
  		check = 1;
be655596b   Sage Weil   ceph: use i_ceph_...
2784
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2785
2786
2787
  	if (check)
  		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
  }
719a2514e   Yan, Zheng   ceph: consider in...
2788
2789
2790
2791
2792
2793
2794
2795
2796
  static inline int get_used_fmode(int caps)
  {
  	int fmode = 0;
  	if (caps & CEPH_CAP_FILE_RD)
  		fmode |= CEPH_FILE_MODE_RD;
  	if (caps & CEPH_CAP_FILE_WR)
  		fmode |= CEPH_FILE_MODE_WR;
  	return fmode;
  }
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2797
  int ceph_try_get_caps(struct inode *inode, int need, int want,
2ee9dd958   Luis Henriques   ceph: add non-blo...
2798
  		      bool nonblock, int *got)
2b1ac852e   Yan, Zheng   ceph: try getting...
2799
  {
719a2514e   Yan, Zheng   ceph: consider in...
2800
  	int ret, flags;
2b1ac852e   Yan, Zheng   ceph: try getting...
2801
2802
  
  	BUG_ON(need & ~CEPH_CAP_FILE_RD);
a25949b99   Jeff Layton   ceph: cap trackin...
2803
2804
2805
2806
2807
2808
2809
2810
  	BUG_ON(want & ~(CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO |
  			CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
  			CEPH_CAP_ANY_DIR_OPS));
  	if (need) {
  		ret = ceph_pool_perm_check(inode, need);
  		if (ret < 0)
  			return ret;
  	}
2b1ac852e   Yan, Zheng   ceph: try getting...
2811

719a2514e   Yan, Zheng   ceph: consider in...
2812
2813
2814
2815
2816
  	flags = get_used_fmode(need | want);
  	if (nonblock)
  		flags |= NON_BLOCKING;
  
  	ret = try_get_cap_refs(inode, need, want, 0, flags, got);
546d40208   Yan, Zheng   ceph: cleanup ret...
2817
  	/* three special error codes */
7d8976afa   Wu Bo   ceph: fix special...
2818
  	if (ret == -EAGAIN || ret == -EFBIG || ret == -ESTALE)
546d40208   Yan, Zheng   ceph: cleanup ret...
2819
2820
  		ret = 0;
  	return ret;
2b1ac852e   Yan, Zheng   ceph: try getting...
2821
  }
a8599bd82   Sage Weil   ceph: capability ...
2822
2823
2824
2825
2826
  /*
   * Wait for caps, and take cap references.  If we can't get a WR cap
   * due to a small max_size, make sure we check_max_size (and possibly
   * ask the mds) so we don't get hung up indefinitely.
   */
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2827
  int ceph_get_caps(struct file *filp, int need, int want,
3738daa68   Yan, Zheng   ceph: fetch inlin...
2828
  		  loff_t endoff, int *got, struct page **pinned_page)
a8599bd82   Sage Weil   ceph: capability ...
2829
  {
ff5d913df   Yan, Zheng   ceph: return -EIO...
2830
  	struct ceph_file_info *fi = filp->private_data;
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2831
2832
  	struct inode *inode = file_inode(filp);
  	struct ceph_inode_info *ci = ceph_inode(inode);
81f148a91   Yan, Zheng   ceph: invalidate ...
2833
  	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
ff5d913df   Yan, Zheng   ceph: return -EIO...
2834
  	int ret, _got, flags;
a8599bd82   Sage Weil   ceph: capability ...
2835

5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2836
  	ret = ceph_pool_perm_check(inode, need);
10183a695   Yan, Zheng   ceph: check OSD c...
2837
2838
  	if (ret < 0)
  		return ret;
81f148a91   Yan, Zheng   ceph: invalidate ...
2839
2840
2841
  	if ((fi->fmode & CEPH_FILE_MODE_WR) &&
  	    fi->filp_gen != READ_ONCE(fsc->filp_gen))
  		return -EBADF;
719a2514e   Yan, Zheng   ceph: consider in...
2842
  	flags = get_used_fmode(need | want);
5dda377cf   Yan, Zheng   ceph: set i_head_...
2843
  	while (true) {
719a2514e   Yan, Zheng   ceph: consider in...
2844
2845
2846
  		flags &= CEPH_FILE_MODE_MASK;
  		if (atomic_read(&fi->num_locks))
  			flags |= CHECK_FILELOCK;
5dda377cf   Yan, Zheng   ceph: set i_head_...
2847
  		_got = 0;
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2848
  		ret = try_get_cap_refs(inode, need, want, endoff,
ff5d913df   Yan, Zheng   ceph: return -EIO...
2849
  				       flags, &_got);
546d40208   Yan, Zheng   ceph: cleanup ret...
2850
  		WARN_ON_ONCE(ret == -EAGAIN);
7b2f936fc   Yan, Zheng   ceph: fix error h...
2851
  		if (!ret) {
3a3430aff   Jeff Layton   ceph: show tasks ...
2852
2853
  			struct ceph_mds_client *mdsc = fsc->mdsc;
  			struct cap_wait cw;
5c341ee32   Nikolay Borisov   ceph: fix schedul...
2854
  			DEFINE_WAIT_FUNC(wait, woken_wake_function);
3a3430aff   Jeff Layton   ceph: show tasks ...
2855

ebce3eb2f   Jeff Layton   ceph: fix inode n...
2856
  			cw.ino = ceph_ino(inode);
3a3430aff   Jeff Layton   ceph: show tasks ...
2857
2858
2859
2860
2861
2862
2863
  			cw.tgid = current->tgid;
  			cw.need = need;
  			cw.want = want;
  
  			spin_lock(&mdsc->caps_list_lock);
  			list_add(&cw.list, &mdsc->cap_wait_list);
  			spin_unlock(&mdsc->caps_list_lock);
719a2514e   Yan, Zheng   ceph: consider in...
2864
2865
  			/* make sure used fmode not timeout */
  			ceph_get_fmode(ci, flags, FMODE_WAIT_BIAS);
5c341ee32   Nikolay Borisov   ceph: fix schedul...
2866
  			add_wait_queue(&ci->i_cap_wq, &wait);
ff5d913df   Yan, Zheng   ceph: return -EIO...
2867
  			flags |= NON_BLOCKING;
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2868
  			while (!(ret = try_get_cap_refs(inode, need, want,
ff5d913df   Yan, Zheng   ceph: return -EIO...
2869
  							endoff, flags, &_got))) {
6e09d0fb6   Yan, Zheng   ceph: fix ceph_ge...
2870
2871
2872
2873
  				if (signal_pending(current)) {
  					ret = -ERESTARTSYS;
  					break;
  				}
5c341ee32   Nikolay Borisov   ceph: fix schedul...
2874
  				wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
6e09d0fb6   Yan, Zheng   ceph: fix ceph_ge...
2875
  			}
5c341ee32   Nikolay Borisov   ceph: fix schedul...
2876
2877
  
  			remove_wait_queue(&ci->i_cap_wq, &wait);
719a2514e   Yan, Zheng   ceph: consider in...
2878
  			ceph_put_fmode(ci, flags, FMODE_WAIT_BIAS);
3a3430aff   Jeff Layton   ceph: show tasks ...
2879
2880
2881
2882
  
  			spin_lock(&mdsc->caps_list_lock);
  			list_del(&cw.list);
  			spin_unlock(&mdsc->caps_list_lock);
7b2f936fc   Yan, Zheng   ceph: fix error h...
2883
  			if (ret == -EAGAIN)
5dda377cf   Yan, Zheng   ceph: set i_head_...
2884
  				continue;
77310320c   Yan, Zheng   ceph: renew caps ...
2885
  		}
81f148a91   Yan, Zheng   ceph: invalidate ...
2886
2887
2888
2889
2890
2891
2892
  
  		if ((fi->fmode & CEPH_FILE_MODE_WR) &&
  		    fi->filp_gen != READ_ONCE(fsc->filp_gen)) {
  			if (ret >= 0 && _got)
  				ceph_put_cap_refs(ci, _got);
  			return -EBADF;
  		}
7b2f936fc   Yan, Zheng   ceph: fix error h...
2893
  		if (ret < 0) {
9bccb7657   Yan, Zheng   ceph: wait for as...
2894
2895
2896
2897
2898
  			if (ret == -EFBIG || ret == -ESTALE) {
  				int ret2 = ceph_wait_on_async_create(inode);
  				if (ret2 < 0)
  					return ret2;
  			}
546d40208   Yan, Zheng   ceph: cleanup ret...
2899
2900
2901
2902
  			if (ret == -EFBIG) {
  				check_max_size(inode, endoff);
  				continue;
  			}
7b2f936fc   Yan, Zheng   ceph: fix error h...
2903
2904
  			if (ret == -ESTALE) {
  				/* session was killed, try renew caps */
719a2514e   Yan, Zheng   ceph: consider in...
2905
  				ret = ceph_renew_caps(inode, flags);
7b2f936fc   Yan, Zheng   ceph: fix error h...
2906
2907
2908
  				if (ret == 0)
  					continue;
  			}
77310320c   Yan, Zheng   ceph: renew caps ...
2909
  			return ret;
5dda377cf   Yan, Zheng   ceph: set i_head_...
2910
  		}
c4d4a582c   Yan, Zheng   ceph: avoid block...
2911

525d15e8e   Yan, Zheng   ceph: check inode...
2912
2913
  		if (S_ISREG(ci->vfs_inode.i_mode) &&
  		    ci->i_inline_version != CEPH_INLINE_NONE &&
5dda377cf   Yan, Zheng   ceph: set i_head_...
2914
  		    (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2915
  		    i_size_read(inode) > 0) {
5dda377cf   Yan, Zheng   ceph: set i_head_...
2916
  			struct page *page =
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2917
  				find_get_page(inode->i_mapping, 0);
5dda377cf   Yan, Zheng   ceph: set i_head_...
2918
2919
2920
2921
2922
  			if (page) {
  				if (PageUptodate(page)) {
  					*pinned_page = page;
  					break;
  				}
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
2923
  				put_page(page);
c4d4a582c   Yan, Zheng   ceph: avoid block...
2924
  			}
5dda377cf   Yan, Zheng   ceph: set i_head_...
2925
2926
2927
2928
2929
2930
  			/*
  			 * drop cap refs first because getattr while
  			 * holding * caps refs can cause deadlock.
  			 */
  			ceph_put_cap_refs(ci, _got);
  			_got = 0;
c4d4a582c   Yan, Zheng   ceph: avoid block...
2931

5dda377cf   Yan, Zheng   ceph: set i_head_...
2932
2933
2934
2935
  			/*
  			 * getattr request will bring inline data into
  			 * page cache
  			 */
5e3ded1bb   Yan, Zheng   ceph: pass filp t...
2936
  			ret = __ceph_do_getattr(inode, NULL,
5dda377cf   Yan, Zheng   ceph: set i_head_...
2937
2938
2939
2940
2941
2942
2943
  						CEPH_STAT_CAP_INLINE_DATA,
  						true);
  			if (ret < 0)
  				return ret;
  			continue;
  		}
  		break;
c4d4a582c   Yan, Zheng   ceph: avoid block...
2944
  	}
5dda377cf   Yan, Zheng   ceph: set i_head_...
2945

525d15e8e   Yan, Zheng   ceph: check inode...
2946
2947
  	if (S_ISREG(ci->vfs_inode.i_mode) &&
  	    (_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
f7f7e7a06   Yan, Zheng   ceph: improve fsc...
2948
  		ceph_fscache_revalidate_cookie(ci);
c4d4a582c   Yan, Zheng   ceph: avoid block...
2949
2950
  	*got = _got;
  	return 0;
a8599bd82   Sage Weil   ceph: capability ...
2951
2952
2953
2954
2955
2956
2957
2958
  }
  
  /*
   * Take cap refs.  Caller must already know we hold at least one ref
   * on the caps in question or we don't know this is safe.
   */
  void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
  {
be655596b   Sage Weil   ceph: use i_ceph_...
2959
  	spin_lock(&ci->i_ceph_lock);
40dcf75e8   Jeff Layton   ceph: make __take...
2960
  	ceph_take_cap_refs(ci, caps, false);
be655596b   Sage Weil   ceph: use i_ceph_...
2961
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
2962
  }
860560904   Yan, Zheng   ceph: avoid sendi...
2963
2964
2965
2966
2967
  
  /*
   * drop cap_snap that is not associated with any snapshot.
   * we don't need to send FLUSHSNAP message for it.
   */
70220ac8c   Yan, Zheng   ceph: introduce a...
2968
2969
  static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
  				  struct ceph_cap_snap *capsnap)
860560904   Yan, Zheng   ceph: avoid sendi...
2970
2971
2972
  {
  	if (!capsnap->need_flush &&
  	    !capsnap->writing && !capsnap->dirty_pages) {
860560904   Yan, Zheng   ceph: avoid sendi...
2973
2974
2975
  		dout("dropping cap_snap %p follows %llu
  ",
  		     capsnap, capsnap->follows);
0e2943878   Yan, Zheng   ceph: unify cap f...
2976
  		BUG_ON(capsnap->cap_flush.tid > 0);
860560904   Yan, Zheng   ceph: avoid sendi...
2977
  		ceph_put_snap_context(capsnap->context);
70220ac8c   Yan, Zheng   ceph: introduce a...
2978
2979
  		if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
  			ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
860560904   Yan, Zheng   ceph: avoid sendi...
2980
  		list_del(&capsnap->ci_item);
860560904   Yan, Zheng   ceph: avoid sendi...
2981
2982
2983
2984
2985
  		ceph_put_cap_snap(capsnap);
  		return 1;
  	}
  	return 0;
  }
a8599bd82   Sage Weil   ceph: capability ...
2986
2987
2988
2989
2990
2991
2992
2993
2994
  /*
   * Release cap refs.
   *
   * If we released the last ref on any given cap, call ceph_check_caps
   * to release (or schedule a release).
   *
   * If we are releasing a WR cap (from a sync write), finalize any affected
   * cap_snap, and wake up any waiters.
   */
e64f44a88   Xiubo Li   ceph: skip checki...
2995
2996
  static void __ceph_put_cap_refs(struct ceph_inode_info *ci, int had,
  				bool skip_checking_caps)
a8599bd82   Sage Weil   ceph: capability ...
2997
2998
2999
  {
  	struct inode *inode = &ci->vfs_inode;
  	int last = 0, put = 0, flushsnaps = 0, wake = 0;
a8599bd82   Sage Weil   ceph: capability ...
3000

be655596b   Sage Weil   ceph: use i_ceph_...
3001
  	spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
3002
3003
3004
3005
3006
3007
3008
3009
  	if (had & CEPH_CAP_PIN)
  		--ci->i_pin_ref;
  	if (had & CEPH_CAP_FILE_RD)
  		if (--ci->i_rd_ref == 0)
  			last++;
  	if (had & CEPH_CAP_FILE_CACHE)
  		if (--ci->i_rdcache_ref == 0)
  			last++;
f85122afe   Jeff Layton   ceph: add refcoun...
3010
3011
3012
  	if (had & CEPH_CAP_FILE_EXCL)
  		if (--ci->i_fx_ref == 0)
  			last++;
a8599bd82   Sage Weil   ceph: capability ...
3013
  	if (had & CEPH_CAP_FILE_BUFFER) {
d3d0720d4   Henry C Chang   ceph: do not use ...
3014
  		if (--ci->i_wb_ref == 0) {
a8599bd82   Sage Weil   ceph: capability ...
3015
3016
3017
  			last++;
  			put++;
  		}
d3d0720d4   Henry C Chang   ceph: do not use ...
3018
3019
3020
  		dout("put_cap_refs %p wb %d -> %d (?)
  ",
  		     inode, ci->i_wb_ref+1, ci->i_wb_ref);
a8599bd82   Sage Weil   ceph: capability ...
3021
3022
3023
3024
  	}
  	if (had & CEPH_CAP_FILE_WR)
  		if (--ci->i_wr_ref == 0) {
  			last++;
860560904   Yan, Zheng   ceph: avoid sendi...
3025
3026
3027
3028
3029
3030
  			if (__ceph_have_pending_cap_snap(ci)) {
  				struct ceph_cap_snap *capsnap =
  					list_last_entry(&ci->i_cap_snaps,
  							struct ceph_cap_snap,
  							ci_item);
  				capsnap->writing = 0;
70220ac8c   Yan, Zheng   ceph: introduce a...
3031
  				if (ceph_try_drop_cap_snap(ci, capsnap))
860560904   Yan, Zheng   ceph: avoid sendi...
3032
3033
3034
3035
  					put++;
  				else if (__ceph_finish_cap_snap(ci, capsnap))
  					flushsnaps = 1;
  				wake = 1;
a8599bd82   Sage Weil   ceph: capability ...
3036
  			}
5dda377cf   Yan, Zheng   ceph: set i_head_...
3037
3038
3039
3040
3041
3042
3043
  			if (ci->i_wrbuffer_ref_head == 0 &&
  			    ci->i_dirty_caps == 0 &&
  			    ci->i_flushing_caps == 0) {
  				BUG_ON(!ci->i_head_snapc);
  				ceph_put_snap_context(ci->i_head_snapc);
  				ci->i_head_snapc = NULL;
  			}
db40cc170   Yan, Zheng   ceph: keep i_snap...
3044
  			/* see comment in __ceph_remove_cap() */
bd84fbcb3   Xiubo Li   ceph: switch to g...
3045
  			if (!__ceph_is_any_real_caps(ci) && ci->i_snap_realm)
db40cc170   Yan, Zheng   ceph: keep i_snap...
3046
  				drop_inode_snap_realm(ci);
a8599bd82   Sage Weil   ceph: capability ...
3047
  		}
be655596b   Sage Weil   ceph: use i_ceph_...
3048
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
3049

819ccbfa4   Sage Weil   ceph: fix leaked ...
3050
3051
3052
  	dout("put_cap_refs %p had %s%s%s
  ", inode, ceph_cap_string(had),
  	     last ? " last" : "", put ? " put" : "");
a8599bd82   Sage Weil   ceph: capability ...
3053

e64f44a88   Xiubo Li   ceph: skip checki...
3054
  	if (last && !skip_checking_caps)
a8599bd82   Sage Weil   ceph: capability ...
3055
3056
  		ceph_check_caps(ci, 0, NULL);
  	else if (flushsnaps)
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
3057
  		ceph_flush_snaps(ci, NULL);
a8599bd82   Sage Weil   ceph: capability ...
3058
  	if (wake)
03066f234   Yehuda Sadeh   ceph: use complet...
3059
  		wake_up_all(&ci->i_cap_wq);
860560904   Yan, Zheng   ceph: avoid sendi...
3060
  	while (put-- > 0)
a8599bd82   Sage Weil   ceph: capability ...
3061
3062
  		iput(inode);
  }
e64f44a88   Xiubo Li   ceph: skip checki...
3063
3064
3065
3066
3067
3068
3069
3070
3071
  void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
  {
  	__ceph_put_cap_refs(ci, had, false);
  }
  
  void ceph_put_cap_refs_no_check_caps(struct ceph_inode_info *ci, int had)
  {
  	__ceph_put_cap_refs(ci, had, true);
  }
a8599bd82   Sage Weil   ceph: capability ...
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
  /*
   * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
   * context.  Adjust per-snap dirty page accounting as appropriate.
   * Once all dirty data for a cap_snap is flushed, flush snapped file
   * metadata back to the MDS.  If we dropped the last ref, call
   * ceph_check_caps.
   */
  void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  				struct ceph_snap_context *snapc)
  {
  	struct inode *inode = &ci->vfs_inode;
a8599bd82   Sage Weil   ceph: capability ...
3083
  	struct ceph_cap_snap *capsnap = NULL;
70220ac8c   Yan, Zheng   ceph: introduce a...
3084
3085
3086
3087
3088
  	int put = 0;
  	bool last = false;
  	bool found = false;
  	bool flush_snaps = false;
  	bool complete_capsnap = false;
a8599bd82   Sage Weil   ceph: capability ...
3089

be655596b   Sage Weil   ceph: use i_ceph_...
3090
  	spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
3091
  	ci->i_wrbuffer_ref -= nr;
70220ac8c   Yan, Zheng   ceph: introduce a...
3092
3093
3094
3095
  	if (ci->i_wrbuffer_ref == 0) {
  		last = true;
  		put++;
  	}
a8599bd82   Sage Weil   ceph: capability ...
3096
3097
3098
  
  	if (ci->i_head_snapc == snapc) {
  		ci->i_wrbuffer_ref_head -= nr;
7d8cb26d7   Sage Weil   ceph: maintain i_...
3099
  		if (ci->i_wrbuffer_ref_head == 0 &&
5dda377cf   Yan, Zheng   ceph: set i_head_...
3100
3101
3102
  		    ci->i_wr_ref == 0 &&
  		    ci->i_dirty_caps == 0 &&
  		    ci->i_flushing_caps == 0) {
7d8cb26d7   Sage Weil   ceph: maintain i_...
3103
  			BUG_ON(!ci->i_head_snapc);
a8599bd82   Sage Weil   ceph: capability ...
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
  			ceph_put_snap_context(ci->i_head_snapc);
  			ci->i_head_snapc = NULL;
  		}
  		dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s
  ",
  		     inode,
  		     ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
  		     ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  		     last ? " LAST" : "");
  	} else {
  		list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  			if (capsnap->context == snapc) {
70220ac8c   Yan, Zheng   ceph: introduce a...
3116
  				found = true;
a8599bd82   Sage Weil   ceph: capability ...
3117
3118
3119
3120
  				break;
  			}
  		}
  		BUG_ON(!found);
819ccbfa4   Sage Weil   ceph: fix leaked ...
3121
3122
  		capsnap->dirty_pages -= nr;
  		if (capsnap->dirty_pages == 0) {
70220ac8c   Yan, Zheng   ceph: introduce a...
3123
3124
3125
3126
3127
3128
3129
3130
3131
  			complete_capsnap = true;
  			if (!capsnap->writing) {
  				if (ceph_try_drop_cap_snap(ci, capsnap)) {
  					put++;
  				} else {
  					ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
  					flush_snaps = true;
  				}
  			}
819ccbfa4   Sage Weil   ceph: fix leaked ...
3132
  		}
a8599bd82   Sage Weil   ceph: capability ...
3133
  		dout("put_wrbuffer_cap_refs on %p cap_snap %p "
860560904   Yan, Zheng   ceph: avoid sendi...
3134
3135
  		     " snap %lld %d/%d -> %d/%d %s%s
  ",
a8599bd82   Sage Weil   ceph: capability ...
3136
3137
3138
3139
  		     inode, capsnap, capsnap->context->seq,
  		     ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
  		     ci->i_wrbuffer_ref, capsnap->dirty_pages,
  		     last ? " (wrbuffer last)" : "",
860560904   Yan, Zheng   ceph: avoid sendi...
3140
  		     complete_capsnap ? " (complete capsnap)" : "");
a8599bd82   Sage Weil   ceph: capability ...
3141
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
3142
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
3143
3144
  
  	if (last) {
bf73c62e7   Yan, Zheng   ceph: check all m...
3145
  		ceph_check_caps(ci, 0, NULL);
70220ac8c   Yan, Zheng   ceph: introduce a...
3146
  	} else if (flush_snaps) {
ed9b430c9   Yan, Zheng   ceph: cleanup cep...
3147
  		ceph_flush_snaps(ci, NULL);
a8599bd82   Sage Weil   ceph: capability ...
3148
  	}
70220ac8c   Yan, Zheng   ceph: introduce a...
3149
3150
  	if (complete_capsnap)
  		wake_up_all(&ci->i_cap_wq);
3e1d0452e   Yan, Zheng   ceph: avoid iput_...
3151
3152
3153
3154
  	while (put-- > 0) {
  		/* avoid calling iput_final() in osd dispatch threads */
  		ceph_async_iput(inode);
  	}
a8599bd82   Sage Weil   ceph: capability ...
3155
3156
3157
  }
  
  /*
ca20c9919   Yan, Zheng   ceph: trim delete...
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
   * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
   */
  static void invalidate_aliases(struct inode *inode)
  {
  	struct dentry *dn, *prev = NULL;
  
  	dout("invalidate_aliases inode %p
  ", inode);
  	d_prune_aliases(inode);
  	/*
  	 * For non-directory inode, d_find_alias() only returns
fc12c80aa   J. Bruce Fields   ceph: trivial com...
3169
3170
  	 * hashed dentry. After calling d_invalidate(), the
  	 * dentry becomes unhashed.
ca20c9919   Yan, Zheng   ceph: trim delete...
3171
  	 *
a8d436f01   Yan, Zheng   ceph: use d_inval...
3172
  	 * For directory inode, d_find_alias() can return
fc12c80aa   J. Bruce Fields   ceph: trivial com...
3173
  	 * unhashed dentry. But directory inode should have
ca20c9919   Yan, Zheng   ceph: trim delete...
3174
3175
3176
3177
3178
3179
3180
  	 * one alias at most.
  	 */
  	while ((dn = d_find_alias(inode))) {
  		if (dn == prev) {
  			dput(dn);
  			break;
  		}
a8d436f01   Yan, Zheng   ceph: use d_inval...
3181
  		d_invalidate(dn);
ca20c9919   Yan, Zheng   ceph: trim delete...
3182
3183
3184
3185
3186
3187
3188
  		if (prev)
  			dput(prev);
  		prev = dn;
  	}
  	if (prev)
  		dput(prev);
  }
a1c6b8358   Yan, Zheng   ceph: define argu...
3189
3190
3191
3192
3193
3194
  struct cap_extra_info {
  	struct ceph_string *pool_ns;
  	/* inline data */
  	u64 inline_version;
  	void *inline_data;
  	u32 inline_len;
4985d6f9e   Yan, Zheng   ceph: handle the ...
3195
3196
3197
3198
  	/* dirstat */
  	bool dirstat_valid;
  	u64 nfiles;
  	u64 nsubdirs;
176c77c9c   Jeff Layton   ceph: handle chan...
3199
  	u64 change_attr;
a1c6b8358   Yan, Zheng   ceph: define argu...
3200
3201
  	/* currently issued */
  	int issued;
ec62b894d   Jeff Layton   ceph: handle btim...
3202
  	struct timespec64 btime;
a1c6b8358   Yan, Zheng   ceph: define argu...
3203
  };
ca20c9919   Yan, Zheng   ceph: trim delete...
3204
  /*
a8599bd82   Sage Weil   ceph: capability ...
3205
3206
3207
   * Handle a cap GRANT message from the MDS.  (Note that a GRANT may
   * actually be a revocation if it specifies a smaller cap set.)
   *
be655596b   Sage Weil   ceph: use i_ceph_...
3208
   * caller holds s_mutex and i_ceph_lock, we drop both.
a8599bd82   Sage Weil   ceph: capability ...
3209
   */
a1c6b8358   Yan, Zheng   ceph: define argu...
3210
  static void handle_cap_grant(struct inode *inode,
15637c8b1   Sage Weil   ceph: clean up ha...
3211
  			     struct ceph_mds_session *session,
a1c6b8358   Yan, Zheng   ceph: define argu...
3212
3213
3214
3215
  			     struct ceph_cap *cap,
  			     struct ceph_mds_caps *grant,
  			     struct ceph_buffer *xattr_buf,
  			     struct cap_extra_info *extra_info)
2cd698be9   Yan, Zheng   ceph: handle cap ...
3216
  	__releases(ci->i_ceph_lock)
a1c6b8358   Yan, Zheng   ceph: define argu...
3217
  	__releases(session->s_mdsc->snap_rwsem)
a8599bd82   Sage Weil   ceph: capability ...
3218
3219
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
2f56f56ad   Sage Weil   Revert "ceph: upd...
3220
  	int seq = le32_to_cpu(grant->seq);
a8599bd82   Sage Weil   ceph: capability ...
3221
  	int newcaps = le32_to_cpu(grant->caps);
2cd698be9   Yan, Zheng   ceph: handle cap ...
3222
  	int used, wanted, dirty;
a8599bd82   Sage Weil   ceph: capability ...
3223
3224
  	u64 size = le64_to_cpu(grant->size);
  	u64 max_size = le64_to_cpu(grant->max_size);
fdac94fab   Yan, Zheng   ceph: skip updati...
3225
3226
  	unsigned char check_caps = 0;
  	bool was_stale = cap->cap_gen < session->s_cap_gen;
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3227
3228
3229
3230
  	bool wake = false;
  	bool writeback = false;
  	bool queue_trunc = false;
  	bool queue_invalidate = false;
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3231
  	bool deleted_inode = false;
31c542a19   Yan, Zheng   ceph: add inline ...
3232
  	bool fill_inline = false;
a8599bd82   Sage Weil   ceph: capability ...
3233

2f56f56ad   Sage Weil   Revert "ceph: upd...
3234
3235
  	dout("handle_cap_grant inode %p cap %p mds%d seq %d %s
  ",
a1c6b8358   Yan, Zheng   ceph: define argu...
3236
  	     inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
a8599bd82   Sage Weil   ceph: capability ...
3237
3238
3239
  	dout(" size %llu max_size %llu, i_size %llu
  ", size, max_size,
  		inode->i_size);
11df2dfb6   Yan, Zheng   ceph: add importe...
3240
3241
  
  	/*
a8599bd82   Sage Weil   ceph: capability ...
3242
3243
3244
3245
  	 * If CACHE is being revoked, and we have no dirty buffers,
  	 * try to invalidate (once).  (If there are dirty buffers, we
  	 * will invalidate _after_ writeback.)
  	 */
525d15e8e   Yan, Zheng   ceph: check inode...
3246
  	if (S_ISREG(inode->i_mode) && /* don't invalidate readdir cache */
fdd4e1583   Yan, Zheng   ceph: rework dcac...
3247
  	    ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3b454c494   Sage Weil   ceph: simplify ca...
3248
  	    (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
9abd4db71   Yan, Zheng   ceph: don't use t...
3249
  	    !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
e90757432   Li Wang   ceph: remove usel...
3250
  		if (try_nonblocking_invalidate(inode)) {
a8599bd82   Sage Weil   ceph: capability ...
3251
3252
3253
  			/* there were locked pages.. invalidate later
  			   in a separate thread. */
  			if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3254
  				queue_invalidate = true;
a8599bd82   Sage Weil   ceph: capability ...
3255
3256
  				ci->i_rdcache_revoking = ci->i_rdcache_gen;
  			}
a8599bd82   Sage Weil   ceph: capability ...
3257
  		}
a8599bd82   Sage Weil   ceph: capability ...
3258
  	}
d2f8bb27c   Yan, Zheng   ceph: update want...
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
  	if (was_stale)
  		cap->issued = cap->implemented = CEPH_CAP_PIN;
  
  	/*
  	 * auth mds of the inode changed. we received the cap export message,
  	 * but still haven't received the cap import message. handle_cap_export
  	 * updated the new auth MDS' cap.
  	 *
  	 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
  	 * that was sent before the cap import message. So don't remove caps.
  	 */
  	if (ceph_seq_cmp(seq, cap->seq) <= 0) {
  		WARN_ON(cap != ci->i_auth_cap);
  		WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
  		seq = cap->seq;
  		newcaps |= cap->issued;
  	}
a8599bd82   Sage Weil   ceph: capability ...
3276
  	/* side effects now are allowed */
685f9a5d1   Sage Weil   ceph: do not conf...
3277
  	cap->cap_gen = session->s_cap_gen;
11df2dfb6   Yan, Zheng   ceph: add importe...
3278
  	cap->seq = seq;
a8599bd82   Sage Weil   ceph: capability ...
3279
3280
  
  	__check_cap_issue(ci, cap, newcaps);
176c77c9c   Jeff Layton   ceph: handle chan...
3281
  	inode_set_max_iversion_raw(inode, extra_info->change_attr);
f98a128a5   Yan, Zheng   ceph: update inod...
3282
  	if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
a1c6b8358   Yan, Zheng   ceph: define argu...
3283
  	    (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
a8599bd82   Sage Weil   ceph: capability ...
3284
  		inode->i_mode = le32_to_cpu(grant->mode);
05cb11c17   Eric W. Biederman   ceph: Translate b...
3285
3286
  		inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
  		inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
ec62b894d   Jeff Layton   ceph: handle btim...
3287
  		ci->i_btime = extra_info->btime;
a8599bd82   Sage Weil   ceph: capability ...
3288
3289
  		dout("%p mode 0%o uid.gid %d.%d
  ", inode, inode->i_mode,
bd2bae6a6   Eric W. Biederman   ceph: Convert kui...
3290
3291
  		     from_kuid(&init_user_ns, inode->i_uid),
  		     from_kgid(&init_user_ns, inode->i_gid));
a8599bd82   Sage Weil   ceph: capability ...
3292
  	}
fa466743a   Yan, Zheng   ceph: fix wrong c...
3293
  	if ((newcaps & CEPH_CAP_LINK_SHARED) &&
a1c6b8358   Yan, Zheng   ceph: define argu...
3294
  	    (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
bfe868486   Miklos Szeredi   filesystems: add ...
3295
  		set_nlink(inode, le32_to_cpu(grant->nlink));
ca20c9919   Yan, Zheng   ceph: trim delete...
3296
3297
  		if (inode->i_nlink == 0 &&
  		    (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3298
  			deleted_inode = true;
ca20c9919   Yan, Zheng   ceph: trim delete...
3299
  	}
a8599bd82   Sage Weil   ceph: capability ...
3300

a1c6b8358   Yan, Zheng   ceph: define argu...
3301
3302
  	if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
  	    grant->xattr_len) {
a8599bd82   Sage Weil   ceph: capability ...
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
  		int len = le32_to_cpu(grant->xattr_len);
  		u64 version = le64_to_cpu(grant->xattr_version);
  
  		if (version > ci->i_xattrs.version) {
  			dout(" got new xattrs v%llu on %p len %d
  ",
  			     version, inode, len);
  			if (ci->i_xattrs.blob)
  				ceph_buffer_put(ci->i_xattrs.blob);
  			ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
  			ci->i_xattrs.version = version;
7221fe4c2   Guangliang Zhao   ceph: add acl for...
3314
  			ceph_forget_all_cached_acls(inode);
ac6713ccb   Yan, Zheng   ceph: add selinux...
3315
  			ceph_security_invalidate_secctx(inode);
a8599bd82   Sage Weil   ceph: capability ...
3316
3317
  		}
  	}
f98a128a5   Yan, Zheng   ceph: update inod...
3318
  	if (newcaps & CEPH_CAP_ANY_RD) {
9bbeab41c   Arnd Bergmann   ceph: use timespe...
3319
  		struct timespec64 mtime, atime, ctime;
f98a128a5   Yan, Zheng   ceph: update inod...
3320
  		/* ctime/mtime/atime? */
9bbeab41c   Arnd Bergmann   ceph: use timespe...
3321
3322
3323
  		ceph_decode_timespec64(&mtime, &grant->mtime);
  		ceph_decode_timespec64(&atime, &grant->atime);
  		ceph_decode_timespec64(&ctime, &grant->ctime);
a1c6b8358   Yan, Zheng   ceph: define argu...
3324
  		ceph_fill_file_time(inode, extra_info->issued,
f98a128a5   Yan, Zheng   ceph: update inod...
3325
3326
3327
  				    le32_to_cpu(grant->time_warp_seq),
  				    &ctime, &mtime, &atime);
  	}
4985d6f9e   Yan, Zheng   ceph: handle the ...
3328
3329
3330
3331
  	if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
  		ci->i_files = extra_info->nfiles;
  		ci->i_subdirs = extra_info->nsubdirs;
  	}
f98a128a5   Yan, Zheng   ceph: update inod...
3332
3333
  	if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
  		/* file layout may have changed */
7627151ea   Yan, Zheng   libceph: define n...
3334
  		s64 old_pool = ci->i_layout.pool_id;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
3335
  		struct ceph_string *old_ns;
7627151ea   Yan, Zheng   libceph: define n...
3336
  		ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
3337
3338
  		old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
  					lockdep_is_held(&ci->i_ceph_lock));
a1c6b8358   Yan, Zheng   ceph: define argu...
3339
  		rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
3340

a1c6b8358   Yan, Zheng   ceph: define argu...
3341
3342
  		if (ci->i_layout.pool_id != old_pool ||
  		    extra_info->pool_ns != old_ns)
7627151ea   Yan, Zheng   libceph: define n...
3343
  			ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
5ea5c5e0a   Yan, Zheng   ceph: initial CEP...
3344

a1c6b8358   Yan, Zheng   ceph: define argu...
3345
  		extra_info->pool_ns = old_ns;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
3346

f98a128a5   Yan, Zheng   ceph: update inod...
3347
  		/* size/truncate_seq? */
a1c6b8358   Yan, Zheng   ceph: define argu...
3348
  		queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
f98a128a5   Yan, Zheng   ceph: update inod...
3349
3350
3351
  					le32_to_cpu(grant->truncate_seq),
  					le64_to_cpu(grant->truncate_size),
  					size);
84eea8c79   Yan, Zheng   ceph: re-request ...
3352
3353
3354
3355
  	}
  
  	if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
  		if (max_size != ci->i_max_size) {
f98a128a5   Yan, Zheng   ceph: update inod...
3356
3357
3358
3359
3360
3361
3362
3363
  			dout("max_size %lld -> %llu
  ",
  			     ci->i_max_size, max_size);
  			ci->i_max_size = max_size;
  			if (max_size >= ci->i_wanted_max_size) {
  				ci->i_wanted_max_size = 0;  /* reset */
  				ci->i_requested_max_size = 0;
  			}
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3364
  			wake = true;
a8599bd82   Sage Weil   ceph: capability ...
3365
  		}
a8599bd82   Sage Weil   ceph: capability ...
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
  	}
  
  	/* check cap bits */
  	wanted = __ceph_caps_wanted(ci);
  	used = __ceph_caps_used(ci);
  	dirty = __ceph_caps_dirty(ci);
  	dout(" my wanted = %s, used = %s, dirty %s
  ",
  	     ceph_cap_string(wanted),
  	     ceph_cap_string(used),
  	     ceph_cap_string(dirty));
fdac94fab   Yan, Zheng   ceph: skip updati...
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
  
  	if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
  	    (wanted & ~(cap->mds_wanted | newcaps))) {
  		/*
  		 * If mds is importing cap, prior cap messages that update
  		 * 'wanted' may get dropped by mds (migrate seq mismatch).
  		 *
  		 * We don't send cap message to update 'wanted' if what we
  		 * want are already issued. If mds revokes caps, cap message
  		 * that releases caps also tells mds what we want. But if
  		 * caps got revoked by mds forcedly (session stale). We may
  		 * haven't told mds what we want.
  		 */
  		check_caps = 1;
a8599bd82   Sage Weil   ceph: capability ...
3391
  	}
a8599bd82   Sage Weil   ceph: capability ...
3392
3393
  	/* revocation, grant, or no-op? */
  	if (cap->issued & ~newcaps) {
3b454c494   Sage Weil   ceph: simplify ca...
3394
3395
3396
3397
3398
3399
3400
  		int revoking = cap->issued & ~newcaps;
  
  		dout("revocation: %s -> %s (revoking %s)
  ",
  		     ceph_cap_string(cap->issued),
  		     ceph_cap_string(newcaps),
  		     ceph_cap_string(revoking));
525d15e8e   Yan, Zheng   ceph: check inode...
3401
3402
  		if (S_ISREG(inode->i_mode) &&
  		    (revoking & used & CEPH_CAP_FILE_BUFFER))
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3403
  			writeback = true;  /* initiate writeback; will delay ack */
525d15e8e   Yan, Zheng   ceph: check inode...
3404
3405
3406
  		else if (queue_invalidate &&
  			 revoking == CEPH_CAP_FILE_CACHE &&
  			 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0)
3b454c494   Sage Weil   ceph: simplify ca...
3407
3408
3409
3410
3411
  			; /* do nothing yet, invalidation will be queued */
  		else if (cap == ci->i_auth_cap)
  			check_caps = 1; /* check auth cap only */
  		else
  			check_caps = 2; /* check all caps */
a8599bd82   Sage Weil   ceph: capability ...
3412
  		cap->issued = newcaps;
978097c90   Sage Weil   ceph: implemented...
3413
  		cap->implemented |= newcaps;
a8599bd82   Sage Weil   ceph: capability ...
3414
3415
3416
3417
3418
3419
3420
3421
  	} else if (cap->issued == newcaps) {
  		dout("caps unchanged: %s -> %s
  ",
  		     ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
  	} else {
  		dout("grant: %s -> %s
  ", ceph_cap_string(cap->issued),
  		     ceph_cap_string(newcaps));
6ee6b9537   Yan, Zheng   ceph: fix race be...
3422
3423
3424
3425
  		/* non-auth MDS is revoking the newly grant caps ? */
  		if (cap == ci->i_auth_cap &&
  		    __ceph_caps_revoking_other(ci, cap, newcaps))
  		    check_caps = 2;
a8599bd82   Sage Weil   ceph: capability ...
3426
3427
3428
3429
  		cap->issued = newcaps;
  		cap->implemented |= newcaps; /* add bits only, to
  					      * avoid stepping on a
  					      * pending revocation */
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3430
  		wake = true;
a8599bd82   Sage Weil   ceph: capability ...
3431
  	}
978097c90   Sage Weil   ceph: implemented...
3432
  	BUG_ON(cap->issued & ~cap->implemented);
a8599bd82   Sage Weil   ceph: capability ...
3433

a1c6b8358   Yan, Zheng   ceph: define argu...
3434
3435
3436
  	if (extra_info->inline_version > 0 &&
  	    extra_info->inline_version >= ci->i_inline_version) {
  		ci->i_inline_version = extra_info->inline_version;
31c542a19   Yan, Zheng   ceph: add inline ...
3437
3438
3439
3440
  		if (ci->i_inline_version != CEPH_INLINE_NONE &&
  		    (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
  			fill_inline = true;
  	}
6f05b30ea   Yan, Zheng   ceph: reset i_req...
3441
3442
  	if (ci->i_auth_cap == cap &&
  	    le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
a1c6b8358   Yan, Zheng   ceph: define argu...
3443
  		if (newcaps & ~extra_info->issued)
ab6c2c3eb   Fabian Frederick   ceph: fix bool as...
3444
  			wake = true;
6f05b30ea   Yan, Zheng   ceph: reset i_req...
3445
3446
3447
3448
3449
3450
3451
  
  		if (ci->i_requested_max_size > max_size ||
  		    !(le32_to_cpu(grant->wanted) & CEPH_CAP_ANY_FILE_WR)) {
  			/* re-request max_size if necessary */
  			ci->i_requested_max_size = 0;
  			wake = true;
  		}
e8a4d2677   Jeff Layton   ceph: clean up ki...
3452
3453
  		ceph_kick_flushing_inode_caps(session, ci);
  		spin_unlock(&ci->i_ceph_lock);
a1c6b8358   Yan, Zheng   ceph: define argu...
3454
  		up_read(&session->s_mdsc->snap_rwsem);
0e2943878   Yan, Zheng   ceph: unify cap f...
3455
3456
  	} else {
  		spin_unlock(&ci->i_ceph_lock);
2cd698be9   Yan, Zheng   ceph: handle cap ...
3457
  	}
31c542a19   Yan, Zheng   ceph: add inline ...
3458
  	if (fill_inline)
a1c6b8358   Yan, Zheng   ceph: define argu...
3459
3460
  		ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
  				      extra_info->inline_len);
31c542a19   Yan, Zheng   ceph: add inline ...
3461

146497581   Yan, Zheng   ceph: avoid unnec...
3462
  	if (queue_trunc)
c6bcda6f5   Yan, Zheng   ceph: queue vmtru...
3463
  		ceph_queue_vmtruncate(inode);
c6bcda6f5   Yan, Zheng   ceph: queue vmtru...
3464

3c6f6b79a   Sage Weil   ceph: cleanup asy...
3465
  	if (writeback)
a8599bd82   Sage Weil   ceph: capability ...
3466
3467
3468
3469
3470
  		/*
  		 * queue inode for writeback: we can't actually call
  		 * filemap_write_and_wait, etc. from message handler
  		 * context.
  		 */
3c6f6b79a   Sage Weil   ceph: cleanup asy...
3471
3472
3473
  		ceph_queue_writeback(inode);
  	if (queue_invalidate)
  		ceph_queue_invalidate(inode);
ca20c9919   Yan, Zheng   ceph: trim delete...
3474
3475
  	if (deleted_inode)
  		invalidate_aliases(inode);
a8599bd82   Sage Weil   ceph: capability ...
3476
  	if (wake)
03066f234   Yehuda Sadeh   ceph: use complet...
3477
  		wake_up_all(&ci->i_cap_wq);
15637c8b1   Sage Weil   ceph: clean up ha...
3478
3479
  
  	if (check_caps == 1)
a0d93e327   Yan, Zheng   ceph: remove dela...
3480
  		ceph_check_caps(ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_NOINVAL,
15637c8b1   Sage Weil   ceph: clean up ha...
3481
3482
  				session);
  	else if (check_caps == 2)
a0d93e327   Yan, Zheng   ceph: remove dela...
3483
  		ceph_check_caps(ci, CHECK_CAPS_NOINVAL, session);
15637c8b1   Sage Weil   ceph: clean up ha...
3484
3485
  	else
  		mutex_unlock(&session->s_mutex);
a8599bd82   Sage Weil   ceph: capability ...
3486
3487
3488
3489
3490
3491
  }
  
  /*
   * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
   * MDS has been safely committed.
   */
6df058c02   Sage Weil   ceph: include tra...
3492
  static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
a8599bd82   Sage Weil   ceph: capability ...
3493
3494
3495
  				 struct ceph_mds_caps *m,
  				 struct ceph_mds_session *session,
  				 struct ceph_cap *cap)
be655596b   Sage Weil   ceph: use i_ceph_...
3496
  	__releases(ci->i_ceph_lock)
a8599bd82   Sage Weil   ceph: capability ...
3497
3498
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
3d14c5d2b   Yehuda Sadeh   ceph: factor out ...
3499
  	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
e4500b5e3   Yan, Zheng   ceph: use list in...
3500
  	struct ceph_cap_flush *cf, *tmp_cf;
553adfd94   Yan, Zheng   ceph: track pendi...
3501
  	LIST_HEAD(to_remove);
a8599bd82   Sage Weil   ceph: capability ...
3502
3503
3504
  	unsigned seq = le32_to_cpu(m->seq);
  	int dirty = le32_to_cpu(m->dirty);
  	int cleaned = 0;
c8799fc46   Yan, Zheng   ceph: optimize ca...
3505
  	bool drop = false;
7271efa79   Thomas Meyer   ceph: fix bool in...
3506
3507
  	bool wake_ci = false;
  	bool wake_mdsc = false;
a8599bd82   Sage Weil   ceph: capability ...
3508

e4500b5e3   Yan, Zheng   ceph: use list in...
3509
  	list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
d7dbfb4f2   Jeff Layton   ceph: add comment...
3510
  		/* Is this the one that was flushed? */
553adfd94   Yan, Zheng   ceph: track pendi...
3511
3512
  		if (cf->tid == flush_tid)
  			cleaned = cf->caps;
d7dbfb4f2   Jeff Layton   ceph: add comment...
3513
3514
3515
  
  		/* Is this a capsnap? */
  		if (cf->caps == 0)
0e2943878   Yan, Zheng   ceph: unify cap f...
3516
  			continue;
d7dbfb4f2   Jeff Layton   ceph: add comment...
3517

553adfd94   Yan, Zheng   ceph: track pendi...
3518
  		if (cf->tid <= flush_tid) {
d7dbfb4f2   Jeff Layton   ceph: add comment...
3519
3520
3521
3522
  			/*
  			 * An earlier or current tid. The FLUSH_ACK should
  			 * represent a superset of this flush's caps.
  			 */
681ac6348   Jeff Layton   ceph: split up __...
3523
  			wake_ci |= __detach_cap_flush_from_ci(ci, cf);
e4500b5e3   Yan, Zheng   ceph: use list in...
3524
  			list_add_tail(&cf->i_list, &to_remove);
553adfd94   Yan, Zheng   ceph: track pendi...
3525
  		} else {
d7dbfb4f2   Jeff Layton   ceph: add comment...
3526
3527
3528
3529
  			/*
  			 * This is a later one. Any caps in it are still dirty
  			 * so don't count them as cleaned.
  			 */
553adfd94   Yan, Zheng   ceph: track pendi...
3530
3531
3532
3533
3534
  			cleaned &= ~cf->caps;
  			if (!cleaned)
  				break;
  		}
  	}
a8599bd82   Sage Weil   ceph: capability ...
3535
3536
3537
3538
3539
3540
3541
  
  	dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
  	     " flushing %s -> %s
  ",
  	     inode, session->s_mds, seq, ceph_cap_string(dirty),
  	     ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
  	     ceph_cap_string(ci->i_flushing_caps & ~cleaned));
8310b0891   Yan, Zheng   ceph: track pendi...
3542
  	if (list_empty(&to_remove) && !cleaned)
a8599bd82   Sage Weil   ceph: capability ...
3543
  		goto out;
a8599bd82   Sage Weil   ceph: capability ...
3544
  	ci->i_flushing_caps &= ~cleaned;
a8599bd82   Sage Weil   ceph: capability ...
3545
3546
  
  	spin_lock(&mdsc->cap_dirty_lock);
8310b0891   Yan, Zheng   ceph: track pendi...
3547

681ac6348   Jeff Layton   ceph: split up __...
3548
3549
  	list_for_each_entry(cf, &to_remove, i_list)
  		wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc, cf);
8310b0891   Yan, Zheng   ceph: track pendi...
3550

a8599bd82   Sage Weil   ceph: capability ...
3551
  	if (ci->i_flushing_caps == 0) {
0e2943878   Yan, Zheng   ceph: unify cap f...
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
  		if (list_empty(&ci->i_cap_flush_list)) {
  			list_del_init(&ci->i_flushing_item);
  			if (!list_empty(&session->s_cap_flushing)) {
  				dout(" mds%d still flushing cap on %p
  ",
  				     session->s_mds,
  				     &list_first_entry(&session->s_cap_flushing,
  						struct ceph_inode_info,
  						i_flushing_item)->vfs_inode);
  			}
  		}
a8599bd82   Sage Weil   ceph: capability ...
3563
  		mdsc->num_cap_flushing--;
a8599bd82   Sage Weil   ceph: capability ...
3564
3565
  		dout(" inode %p now !flushing
  ", inode);
afcdaea3f   Sage Weil   ceph: flush dirty...
3566
3567
3568
3569
3570
  
  		if (ci->i_dirty_caps == 0) {
  			dout(" inode %p now clean
  ", inode);
  			BUG_ON(!list_empty(&ci->i_dirty_item));
c8799fc46   Yan, Zheng   ceph: optimize ca...
3571
  			drop = true;
5dda377cf   Yan, Zheng   ceph: set i_head_...
3572
3573
  			if (ci->i_wr_ref == 0 &&
  			    ci->i_wrbuffer_ref_head == 0) {
7d8cb26d7   Sage Weil   ceph: maintain i_...
3574
3575
3576
3577
  				BUG_ON(!ci->i_head_snapc);
  				ceph_put_snap_context(ci->i_head_snapc);
  				ci->i_head_snapc = NULL;
  			}
76e3b390d   Sage Weil   ceph: move dirty ...
3578
3579
  		} else {
  			BUG_ON(list_empty(&ci->i_dirty_item));
afcdaea3f   Sage Weil   ceph: flush dirty...
3580
  		}
a8599bd82   Sage Weil   ceph: capability ...
3581
3582
  	}
  	spin_unlock(&mdsc->cap_dirty_lock);
a8599bd82   Sage Weil   ceph: capability ...
3583
3584
  
  out:
be655596b   Sage Weil   ceph: use i_ceph_...
3585
  	spin_unlock(&ci->i_ceph_lock);
553adfd94   Yan, Zheng   ceph: track pendi...
3586
3587
3588
  
  	while (!list_empty(&to_remove)) {
  		cf = list_first_entry(&to_remove,
e4500b5e3   Yan, Zheng   ceph: use list in...
3589
3590
  				      struct ceph_cap_flush, i_list);
  		list_del(&cf->i_list);
f66fd9f09   Yan, Zheng   ceph: pre-allocat...
3591
  		ceph_free_cap_flush(cf);
553adfd94   Yan, Zheng   ceph: track pendi...
3592
  	}
c8799fc46   Yan, Zheng   ceph: optimize ca...
3593
3594
3595
3596
3597
  
  	if (wake_ci)
  		wake_up_all(&ci->i_cap_wq);
  	if (wake_mdsc)
  		wake_up_all(&mdsc->cap_flushing_wq);
afcdaea3f   Sage Weil   ceph: flush dirty...
3598
  	if (drop)
a8599bd82   Sage Weil   ceph: capability ...
3599
3600
3601
3602
3603
3604
3605
3606
3607
  		iput(inode);
  }
  
  /*
   * Handle FLUSHSNAP_ACK.  MDS has flushed snap data to disk and we can
   * throw away our cap_snap.
   *
   * Caller hold s_mutex.
   */
6df058c02   Sage Weil   ceph: include tra...
3608
  static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
a8599bd82   Sage Weil   ceph: capability ...
3609
3610
3611
3612
  				     struct ceph_mds_caps *m,
  				     struct ceph_mds_session *session)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
affbc19a6   Yan, Zheng   ceph: make sure s...
3613
  	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
a8599bd82   Sage Weil   ceph: capability ...
3614
  	u64 follows = le64_to_cpu(m->snap_follows);
a8599bd82   Sage Weil   ceph: capability ...
3615
  	struct ceph_cap_snap *capsnap;
c8799fc46   Yan, Zheng   ceph: optimize ca...
3616
3617
3618
  	bool flushed = false;
  	bool wake_ci = false;
  	bool wake_mdsc = false;
a8599bd82   Sage Weil   ceph: capability ...
3619
3620
3621
3622
  
  	dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld
  ",
  	     inode, ci, session->s_mds, follows);
be655596b   Sage Weil   ceph: use i_ceph_...
3623
  	spin_lock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
3624
3625
  	list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  		if (capsnap->follows == follows) {
0e2943878   Yan, Zheng   ceph: unify cap f...
3626
  			if (capsnap->cap_flush.tid != flush_tid) {
a8599bd82   Sage Weil   ceph: capability ...
3627
3628
3629
  				dout(" cap_snap %p follows %lld tid %lld !="
  				     " %lld
  ", capsnap, follows,
0e2943878   Yan, Zheng   ceph: unify cap f...
3630
  				     flush_tid, capsnap->cap_flush.tid);
a8599bd82   Sage Weil   ceph: capability ...
3631
3632
  				break;
  			}
c8799fc46   Yan, Zheng   ceph: optimize ca...
3633
  			flushed = true;
a8599bd82   Sage Weil   ceph: capability ...
3634
3635
3636
3637
3638
3639
3640
  			break;
  		} else {
  			dout(" skipping cap_snap %p follows %lld
  ",
  			     capsnap, capsnap->follows);
  		}
  	}
0e2943878   Yan, Zheng   ceph: unify cap f...
3641
  	if (flushed) {
0e2943878   Yan, Zheng   ceph: unify cap f...
3642
3643
3644
3645
3646
  		WARN_ON(capsnap->dirty_pages || capsnap->writing);
  		dout(" removing %p cap_snap %p follows %lld
  ",
  		     inode, capsnap, follows);
  		list_del(&capsnap->ci_item);
681ac6348   Jeff Layton   ceph: split up __...
3647
  		wake_ci |= __detach_cap_flush_from_ci(ci, &capsnap->cap_flush);
0e2943878   Yan, Zheng   ceph: unify cap f...
3648
3649
3650
3651
3652
  
  		spin_lock(&mdsc->cap_dirty_lock);
  
  		if (list_empty(&ci->i_cap_flush_list))
  			list_del_init(&ci->i_flushing_item);
681ac6348   Jeff Layton   ceph: split up __...
3653
3654
  		wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc,
  							  &capsnap->cap_flush);
0e2943878   Yan, Zheng   ceph: unify cap f...
3655
  		spin_unlock(&mdsc->cap_dirty_lock);
0e2943878   Yan, Zheng   ceph: unify cap f...
3656
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
3657
  	spin_unlock(&ci->i_ceph_lock);
0e2943878   Yan, Zheng   ceph: unify cap f...
3658
3659
3660
  	if (flushed) {
  		ceph_put_snap_context(capsnap->context);
  		ceph_put_cap_snap(capsnap);
c8799fc46   Yan, Zheng   ceph: optimize ca...
3661
3662
3663
3664
  		if (wake_ci)
  			wake_up_all(&ci->i_cap_wq);
  		if (wake_mdsc)
  			wake_up_all(&mdsc->cap_flushing_wq);
a8599bd82   Sage Weil   ceph: capability ...
3665
  		iput(inode);
0e2943878   Yan, Zheng   ceph: unify cap f...
3666
  	}
a8599bd82   Sage Weil   ceph: capability ...
3667
3668
3669
3670
3671
3672
3673
  }
  
  /*
   * Handle TRUNC from MDS, indicating file truncation.
   *
   * caller hold s_mutex.
   */
7391fba26   Jeff Layton   ceph: don't relea...
3674
  static bool handle_cap_trunc(struct inode *inode,
a8599bd82   Sage Weil   ceph: capability ...
3675
3676
  			     struct ceph_mds_caps *trunc,
  			     struct ceph_mds_session *session)
a8599bd82   Sage Weil   ceph: capability ...
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int mds = session->s_mds;
  	int seq = le32_to_cpu(trunc->seq);
  	u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
  	u64 truncate_size = le64_to_cpu(trunc->truncate_size);
  	u64 size = le64_to_cpu(trunc->size);
  	int implemented = 0;
  	int dirty = __ceph_caps_dirty(ci);
  	int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
7391fba26   Jeff Layton   ceph: don't relea...
3687
3688
3689
  	bool queue_trunc = false;
  
  	lockdep_assert_held(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
3690
3691
3692
3693
3694
3695
3696
3697
  
  	issued |= implemented | dirty;
  
  	dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d
  ",
  	     inode, mds, seq, truncate_size, truncate_seq);
  	queue_trunc = ceph_fill_file_size(inode, issued,
  					  truncate_seq, truncate_size, size);
7391fba26   Jeff Layton   ceph: don't relea...
3698
  	return queue_trunc;
a8599bd82   Sage Weil   ceph: capability ...
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
  }
  
  /*
   * Handle EXPORT from MDS.  Cap is being migrated _from_ this mds to a
   * different one.  If we are the most recent migration we've seen (as
   * indicated by mseq), make note of the migrating cap bits for the
   * duration (until we see the corresponding IMPORT).
   *
   * caller holds s_mutex
   */
  static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
11df2dfb6   Yan, Zheng   ceph: add importe...
3710
3711
  			      struct ceph_mds_cap_peer *ph,
  			      struct ceph_mds_session *session)
a8599bd82   Sage Weil   ceph: capability ...
3712
  {
db3540522   Sage Weil   ceph: fix cap flu...
3713
  	struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
11df2dfb6   Yan, Zheng   ceph: add importe...
3714
  	struct ceph_mds_session *tsession = NULL;
d9df27835   Yan, Zheng   ceph: pre-allocat...
3715
  	struct ceph_cap *cap, *tcap, *new_cap = NULL;
a8599bd82   Sage Weil   ceph: capability ...
3716
  	struct ceph_inode_info *ci = ceph_inode(inode);
11df2dfb6   Yan, Zheng   ceph: add importe...
3717
  	u64 t_cap_id;
a8599bd82   Sage Weil   ceph: capability ...
3718
  	unsigned mseq = le32_to_cpu(ex->migrate_seq);
11df2dfb6   Yan, Zheng   ceph: add importe...
3719
3720
3721
  	unsigned t_seq, t_mseq;
  	int target, issued;
  	int mds = session->s_mds;
a8599bd82   Sage Weil   ceph: capability ...
3722

11df2dfb6   Yan, Zheng   ceph: add importe...
3723
3724
3725
3726
3727
3728
3729
3730
3731
  	if (ph) {
  		t_cap_id = le64_to_cpu(ph->cap_id);
  		t_seq = le32_to_cpu(ph->seq);
  		t_mseq = le32_to_cpu(ph->mseq);
  		target = le32_to_cpu(ph->mds);
  	} else {
  		t_cap_id = t_seq = t_mseq = 0;
  		target = -1;
  	}
a8599bd82   Sage Weil   ceph: capability ...
3732

11df2dfb6   Yan, Zheng   ceph: add importe...
3733
3734
3735
3736
  	dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d
  ",
  	     inode, ci, mds, mseq, target);
  retry:
be655596b   Sage Weil   ceph: use i_ceph_...
3737
  	spin_lock(&ci->i_ceph_lock);
11df2dfb6   Yan, Zheng   ceph: add importe...
3738
  	cap = __get_cap_for_mds(ci, mds);
ca665e028   Yan, Zheng   mds: check cap ID...
3739
  	if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
11df2dfb6   Yan, Zheng   ceph: add importe...
3740
  		goto out_unlock;
a8599bd82   Sage Weil   ceph: capability ...
3741

11df2dfb6   Yan, Zheng   ceph: add importe...
3742
  	if (target < 0) {
d2f8bb27c   Yan, Zheng   ceph: update want...
3743
  		__ceph_remove_cap(cap, false);
11df2dfb6   Yan, Zheng   ceph: add importe...
3744
  		goto out_unlock;
a8599bd82   Sage Weil   ceph: capability ...
3745
  	}
11df2dfb6   Yan, Zheng   ceph: add importe...
3746
3747
3748
3749
  	/*
  	 * now we know we haven't received the cap import message yet
  	 * because the exported cap still exist.
  	 */
db3540522   Sage Weil   ceph: fix cap flu...
3750

11df2dfb6   Yan, Zheng   ceph: add importe...
3751
  	issued = cap->issued;
d84b37f9f   Yan, Zheng   ceph: limit rate ...
3752
3753
3754
3755
3756
3757
3758
3759
  	if (issued != cap->implemented)
  		pr_err_ratelimited("handle_cap_export: issued != implemented: "
  				"ino (%llx.%llx) mds%d seq %d mseq %d "
  				"issued %s implemented %s
  ",
  				ceph_vinop(inode), mds, cap->seq, cap->mseq,
  				ceph_cap_string(issued),
  				ceph_cap_string(cap->implemented));
11df2dfb6   Yan, Zheng   ceph: add importe...
3760
3761
3762
3763
  
  	tcap = __get_cap_for_mds(ci, target);
  	if (tcap) {
  		/* already have caps from the target */
fa0aa3b83   Yan, Zheng   ceph: fix message...
3764
  		if (tcap->cap_id == t_cap_id &&
11df2dfb6   Yan, Zheng   ceph: add importe...
3765
3766
3767
3768
3769
3770
  		    ceph_seq_cmp(tcap->seq, t_seq) < 0) {
  			dout(" updating import cap %p mds%d
  ", tcap, target);
  			tcap->cap_id = t_cap_id;
  			tcap->seq = t_seq - 1;
  			tcap->issue_seq = t_seq - 1;
11df2dfb6   Yan, Zheng   ceph: add importe...
3771
3772
  			tcap->issued |= issued;
  			tcap->implemented |= issued;
1cf03a68e   Jeff Layton   ceph: convert mds...
3773
  			if (cap == ci->i_auth_cap) {
11df2dfb6   Yan, Zheng   ceph: add importe...
3774
  				ci->i_auth_cap = tcap;
1cf03a68e   Jeff Layton   ceph: convert mds...
3775
  				change_auth_cap_ses(ci, tcap->session);
db3540522   Sage Weil   ceph: fix cap flu...
3776
  			}
a8599bd82   Sage Weil   ceph: capability ...
3777
  		}
a096b09ae   Yan, Zheng   ceph: queue cap r...
3778
  		__ceph_remove_cap(cap, false);
11df2dfb6   Yan, Zheng   ceph: add importe...
3779
  		goto out_unlock;
d9df27835   Yan, Zheng   ceph: pre-allocat...
3780
  	} else if (tsession) {
11df2dfb6   Yan, Zheng   ceph: add importe...
3781
  		/* add placeholder for the export tagert */
d9df27835   Yan, Zheng   ceph: pre-allocat...
3782
  		int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
00f06cba5   Yan, Zheng   ceph: make sure f...
3783
  		tcap = new_cap;
135e671e5   Yan, Zheng   ceph: simplify ca...
3784
  		ceph_add_cap(inode, tsession, t_cap_id, issued, 0,
d9df27835   Yan, Zheng   ceph: pre-allocat...
3785
  			     t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
00f06cba5   Yan, Zheng   ceph: make sure f...
3786
3787
3788
3789
3790
3791
3792
  		if (!list_empty(&ci->i_cap_flush_list) &&
  		    ci->i_auth_cap == tcap) {
  			spin_lock(&mdsc->cap_dirty_lock);
  			list_move_tail(&ci->i_flushing_item,
  				       &tcap->session->s_cap_flushing);
  			spin_unlock(&mdsc->cap_dirty_lock);
  		}
d9df27835   Yan, Zheng   ceph: pre-allocat...
3793
3794
  		__ceph_remove_cap(cap, false);
  		goto out_unlock;
a8599bd82   Sage Weil   ceph: capability ...
3795
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
3796
  	spin_unlock(&ci->i_ceph_lock);
11df2dfb6   Yan, Zheng   ceph: add importe...
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
  	mutex_unlock(&session->s_mutex);
  
  	/* open target session */
  	tsession = ceph_mdsc_open_export_target_session(mdsc, target);
  	if (!IS_ERR(tsession)) {
  		if (mds > target) {
  			mutex_lock(&session->s_mutex);
  			mutex_lock_nested(&tsession->s_mutex,
  					  SINGLE_DEPTH_NESTING);
  		} else {
  			mutex_lock(&tsession->s_mutex);
  			mutex_lock_nested(&session->s_mutex,
  					  SINGLE_DEPTH_NESTING);
  		}
d9df27835   Yan, Zheng   ceph: pre-allocat...
3811
  		new_cap = ceph_get_cap(mdsc, NULL);
11df2dfb6   Yan, Zheng   ceph: add importe...
3812
3813
3814
3815
  	} else {
  		WARN_ON(1);
  		tsession = NULL;
  		target = -1;
4d8e28ff3   Wu Bo   ceph: fix double ...
3816
  		mutex_lock(&session->s_mutex);
11df2dfb6   Yan, Zheng   ceph: add importe...
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
  	}
  	goto retry;
  
  out_unlock:
  	spin_unlock(&ci->i_ceph_lock);
  	mutex_unlock(&session->s_mutex);
  	if (tsession) {
  		mutex_unlock(&tsession->s_mutex);
  		ceph_put_mds_session(tsession);
  	}
d9df27835   Yan, Zheng   ceph: pre-allocat...
3827
3828
  	if (new_cap)
  		ceph_put_cap(mdsc, new_cap);
a8599bd82   Sage Weil   ceph: capability ...
3829
3830
3831
  }
  
  /*
2cd698be9   Yan, Zheng   ceph: handle cap ...
3832
   * Handle cap IMPORT.
a8599bd82   Sage Weil   ceph: capability ...
3833
   *
2cd698be9   Yan, Zheng   ceph: handle cap ...
3834
   * caller holds s_mutex. acquires i_ceph_lock
a8599bd82   Sage Weil   ceph: capability ...
3835
3836
3837
   */
  static void handle_cap_import(struct ceph_mds_client *mdsc,
  			      struct inode *inode, struct ceph_mds_caps *im,
4ee6a914e   Yan, Zheng   ceph: remove expo...
3838
  			      struct ceph_mds_cap_peer *ph,
a8599bd82   Sage Weil   ceph: capability ...
3839
  			      struct ceph_mds_session *session,
2cd698be9   Yan, Zheng   ceph: handle cap ...
3840
  			      struct ceph_cap **target_cap, int *old_issued)
a8599bd82   Sage Weil   ceph: capability ...
3841
3842
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
2cd698be9   Yan, Zheng   ceph: handle cap ...
3843
  	struct ceph_cap *cap, *ocap, *new_cap = NULL;
a8599bd82   Sage Weil   ceph: capability ...
3844
  	int mds = session->s_mds;
2cd698be9   Yan, Zheng   ceph: handle cap ...
3845
3846
  	int issued;
  	unsigned caps = le32_to_cpu(im->caps);
a8599bd82   Sage Weil   ceph: capability ...
3847
3848
3849
3850
3851
  	unsigned wanted = le32_to_cpu(im->wanted);
  	unsigned seq = le32_to_cpu(im->seq);
  	unsigned mseq = le32_to_cpu(im->migrate_seq);
  	u64 realmino = le64_to_cpu(im->realm);
  	u64 cap_id = le64_to_cpu(im->cap_id);
4ee6a914e   Yan, Zheng   ceph: remove expo...
3852
3853
  	u64 p_cap_id;
  	int peer;
a8599bd82   Sage Weil   ceph: capability ...
3854

4ee6a914e   Yan, Zheng   ceph: remove expo...
3855
3856
3857
3858
3859
3860
3861
  	if (ph) {
  		p_cap_id = le64_to_cpu(ph->cap_id);
  		peer = le32_to_cpu(ph->mds);
  	} else {
  		p_cap_id = 0;
  		peer = -1;
  	}
db3540522   Sage Weil   ceph: fix cap flu...
3862

4ee6a914e   Yan, Zheng   ceph: remove expo...
3863
3864
3865
  	dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d
  ",
  	     inode, ci, mds, mseq, peer);
d9df27835   Yan, Zheng   ceph: pre-allocat...
3866
  retry:
d9df27835   Yan, Zheng   ceph: pre-allocat...
3867
3868
3869
3870
3871
  	cap = __get_cap_for_mds(ci, mds);
  	if (!cap) {
  		if (!new_cap) {
  			spin_unlock(&ci->i_ceph_lock);
  			new_cap = ceph_get_cap(mdsc, NULL);
783332336   Jeff Layton   ceph: don't take ...
3872
  			spin_lock(&ci->i_ceph_lock);
d9df27835   Yan, Zheng   ceph: pre-allocat...
3873
3874
  			goto retry;
  		}
2cd698be9   Yan, Zheng   ceph: handle cap ...
3875
3876
3877
3878
3879
3880
  		cap = new_cap;
  	} else {
  		if (new_cap) {
  			ceph_put_cap(mdsc, new_cap);
  			new_cap = NULL;
  		}
d9df27835   Yan, Zheng   ceph: pre-allocat...
3881
  	}
2cd698be9   Yan, Zheng   ceph: handle cap ...
3882
3883
  	__ceph_caps_issued(ci, &issued);
  	issued |= __ceph_caps_dirty(ci);
135e671e5   Yan, Zheng   ceph: simplify ca...
3884
  	ceph_add_cap(inode, session, cap_id, caps, wanted, seq, mseq,
d9df27835   Yan, Zheng   ceph: pre-allocat...
3885
  		     realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
2cd698be9   Yan, Zheng   ceph: handle cap ...
3886
3887
  	ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
  	if (ocap && ocap->cap_id == p_cap_id) {
4ee6a914e   Yan, Zheng   ceph: remove expo...
3888
3889
  		dout(" remove export cap %p mds%d flags %d
  ",
2cd698be9   Yan, Zheng   ceph: handle cap ...
3890
  		     ocap, peer, ph->flags);
4ee6a914e   Yan, Zheng   ceph: remove expo...
3891
  		if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
2cd698be9   Yan, Zheng   ceph: handle cap ...
3892
3893
  		    (ocap->seq != le32_to_cpu(ph->seq) ||
  		     ocap->mseq != le32_to_cpu(ph->mseq))) {
d84b37f9f   Yan, Zheng   ceph: limit rate ...
3894
3895
3896
3897
3898
3899
3900
3901
  			pr_err_ratelimited("handle_cap_import: "
  					"mismatched seq/mseq: ino (%llx.%llx) "
  					"mds%d seq %d mseq %d importer mds%d "
  					"has peer seq %d mseq %d
  ",
  					ceph_vinop(inode), peer, ocap->seq,
  					ocap->mseq, mds, le32_to_cpu(ph->seq),
  					le32_to_cpu(ph->mseq));
db3540522   Sage Weil   ceph: fix cap flu...
3902
  		}
2cd698be9   Yan, Zheng   ceph: handle cap ...
3903
  		__ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
a8599bd82   Sage Weil   ceph: capability ...
3904
  	}
2cd698be9   Yan, Zheng   ceph: handle cap ...
3905
3906
  	*old_issued = issued;
  	*target_cap = cap;
a8599bd82   Sage Weil   ceph: capability ...
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
  }
  
  /*
   * Handle a caps message from the MDS.
   *
   * Identify the appropriate session, inode, and call the right handler
   * based on the cap op.
   */
  void ceph_handle_caps(struct ceph_mds_session *session,
  		      struct ceph_msg *msg)
  {
  	struct ceph_mds_client *mdsc = session->s_mdsc;
a8599bd82   Sage Weil   ceph: capability ...
3919
  	struct inode *inode;
be655596b   Sage Weil   ceph: use i_ceph_...
3920
  	struct ceph_inode_info *ci;
a8599bd82   Sage Weil   ceph: capability ...
3921
3922
  	struct ceph_cap *cap;
  	struct ceph_mds_caps *h;
4ee6a914e   Yan, Zheng   ceph: remove expo...
3923
  	struct ceph_mds_cap_peer *peer = NULL;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
3924
  	struct ceph_snap_realm *realm = NULL;
a1c6b8358   Yan, Zheng   ceph: define argu...
3925
  	int op;
4985d6f9e   Yan, Zheng   ceph: handle the ...
3926
  	int msg_version = le16_to_cpu(msg->hdr.version);
3d7ded4d8   Sage Weil   ceph: release cap...
3927
  	u32 seq, mseq;
a8599bd82   Sage Weil   ceph: capability ...
3928
  	struct ceph_vino vino;
70edb55bd   Sage Weil   ceph: fix snaptra...
3929
  	void *snaptrace;
ce1fbc8dd   Sage Weil   ceph: support v2 ...
3930
  	size_t snaptrace_len;
fb01d1f8b   Yan, Zheng   ceph: parse inlin...
3931
  	void *p, *end;
a1c6b8358   Yan, Zheng   ceph: define argu...
3932
  	struct cap_extra_info extra_info = {};
7391fba26   Jeff Layton   ceph: don't relea...
3933
  	bool queue_trunc;
a8599bd82   Sage Weil   ceph: capability ...
3934

a1c6b8358   Yan, Zheng   ceph: define argu...
3935
3936
  	dout("handle_caps from mds%d
  ", session->s_mds);
a8599bd82   Sage Weil   ceph: capability ...
3937
3938
  
  	/* decode */
4ee6a914e   Yan, Zheng   ceph: remove expo...
3939
  	end = msg->front.iov_base + msg->front.iov_len;
a8599bd82   Sage Weil   ceph: capability ...
3940
3941
3942
3943
3944
3945
  	if (msg->front.iov_len < sizeof(*h))
  		goto bad;
  	h = msg->front.iov_base;
  	op = le32_to_cpu(h->op);
  	vino.ino = le64_to_cpu(h->ino);
  	vino.snap = CEPH_NOSNAP;
a8599bd82   Sage Weil   ceph: capability ...
3946
  	seq = le32_to_cpu(h->seq);
3d7ded4d8   Sage Weil   ceph: release cap...
3947
  	mseq = le32_to_cpu(h->migrate_seq);
a8599bd82   Sage Weil   ceph: capability ...
3948

ce1fbc8dd   Sage Weil   ceph: support v2 ...
3949
3950
  	snaptrace = h + 1;
  	snaptrace_len = le32_to_cpu(h->snap_trace_len);
fb01d1f8b   Yan, Zheng   ceph: parse inlin...
3951
  	p = snaptrace + snaptrace_len;
ce1fbc8dd   Sage Weil   ceph: support v2 ...
3952

4985d6f9e   Yan, Zheng   ceph: handle the ...
3953
  	if (msg_version >= 2) {
fb01d1f8b   Yan, Zheng   ceph: parse inlin...
3954
  		u32 flock_len;
ce1fbc8dd   Sage Weil   ceph: support v2 ...
3955
  		ceph_decode_32_safe(&p, end, flock_len, bad);
4ee6a914e   Yan, Zheng   ceph: remove expo...
3956
3957
  		if (p + flock_len > end)
  			goto bad;
fb01d1f8b   Yan, Zheng   ceph: parse inlin...
3958
  		p += flock_len;
ce1fbc8dd   Sage Weil   ceph: support v2 ...
3959
  	}
4985d6f9e   Yan, Zheng   ceph: handle the ...
3960
  	if (msg_version >= 3) {
4ee6a914e   Yan, Zheng   ceph: remove expo...
3961
  		if (op == CEPH_CAP_OP_IMPORT) {
4ee6a914e   Yan, Zheng   ceph: remove expo...
3962
3963
3964
  			if (p + sizeof(*peer) > end)
  				goto bad;
  			peer = p;
fb01d1f8b   Yan, Zheng   ceph: parse inlin...
3965
  			p += sizeof(*peer);
11df2dfb6   Yan, Zheng   ceph: add importe...
3966
3967
3968
  		} else if (op == CEPH_CAP_OP_EXPORT) {
  			/* recorded in unused fields */
  			peer = (void *)&h->size;
4ee6a914e   Yan, Zheng   ceph: remove expo...
3969
3970
  		}
  	}
4985d6f9e   Yan, Zheng   ceph: handle the ...
3971
  	if (msg_version >= 4) {
a1c6b8358   Yan, Zheng   ceph: define argu...
3972
3973
3974
  		ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
  		ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
  		if (p + extra_info.inline_len > end)
fb01d1f8b   Yan, Zheng   ceph: parse inlin...
3975
  			goto bad;
a1c6b8358   Yan, Zheng   ceph: define argu...
3976
3977
  		extra_info.inline_data = p;
  		p += extra_info.inline_len;
fb01d1f8b   Yan, Zheng   ceph: parse inlin...
3978
  	}
4985d6f9e   Yan, Zheng   ceph: handle the ...
3979
  	if (msg_version >= 5) {
92475f05b   Jeff Layton   ceph: handle epoc...
3980
3981
3982
3983
3984
3985
  		struct ceph_osd_client	*osdc = &mdsc->fsc->client->osdc;
  		u32			epoch_barrier;
  
  		ceph_decode_32_safe(&p, end, epoch_barrier, bad);
  		ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
  	}
4985d6f9e   Yan, Zheng   ceph: handle the ...
3986
  	if (msg_version >= 8) {
5ea5c5e0a   Yan, Zheng   ceph: initial CEP...
3987
3988
  		u64 flush_tid;
  		u32 caller_uid, caller_gid;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
3989
  		u32 pool_ns_len;
92475f05b   Jeff Layton   ceph: handle epoc...
3990

5ea5c5e0a   Yan, Zheng   ceph: initial CEP...
3991
3992
3993
3994
3995
3996
3997
  		/* version >= 6 */
  		ceph_decode_64_safe(&p, end, flush_tid, bad);
  		/* version >= 7 */
  		ceph_decode_32_safe(&p, end, caller_uid, bad);
  		ceph_decode_32_safe(&p, end, caller_gid, bad);
  		/* version >= 8 */
  		ceph_decode_32_safe(&p, end, pool_ns_len, bad);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
3998
3999
  		if (pool_ns_len > 0) {
  			ceph_decode_need(&p, end, pool_ns_len, bad);
a1c6b8358   Yan, Zheng   ceph: define argu...
4000
4001
  			extra_info.pool_ns =
  				ceph_find_or_create_string(p, pool_ns_len);
779fe0fb8   Yan, Zheng   ceph: rados pool ...
4002
4003
  			p += pool_ns_len;
  		}
5ea5c5e0a   Yan, Zheng   ceph: initial CEP...
4004
  	}
ec62b894d   Jeff Layton   ceph: handle btim...
4005
  	if (msg_version >= 9) {
4985d6f9e   Yan, Zheng   ceph: handle the ...
4006
  		struct ceph_timespec *btime;
4985d6f9e   Yan, Zheng   ceph: handle the ...
4007

4985d6f9e   Yan, Zheng   ceph: handle the ...
4008
4009
4010
  		if (p + sizeof(*btime) > end)
  			goto bad;
  		btime = p;
ec62b894d   Jeff Layton   ceph: handle btim...
4011
  		ceph_decode_timespec64(&extra_info.btime, btime);
4985d6f9e   Yan, Zheng   ceph: handle the ...
4012
  		p += sizeof(*btime);
176c77c9c   Jeff Layton   ceph: handle chan...
4013
  		ceph_decode_64_safe(&p, end, extra_info.change_attr, bad);
ec62b894d   Jeff Layton   ceph: handle btim...
4014
4015
4016
4017
  	}
  
  	if (msg_version >= 11) {
  		u32 flags;
4985d6f9e   Yan, Zheng   ceph: handle the ...
4018
4019
4020
4021
4022
4023
4024
  		/* version >= 10 */
  		ceph_decode_32_safe(&p, end, flags, bad);
  		/* version >= 11 */
  		extra_info.dirstat_valid = true;
  		ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
  		ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
  	}
6cd3bcad0   Yan, Zheng   ceph: move ceph_f...
4025
  	/* lookup ino */
a1c6b8358   Yan, Zheng   ceph: define argu...
4026
  	inode = ceph_find_inode(mdsc->fsc->sb, vino);
6cd3bcad0   Yan, Zheng   ceph: move ceph_f...
4027
4028
4029
4030
  	ci = ceph_inode(inode);
  	dout(" op %s ino %llx.%llx inode %p
  ", ceph_cap_op_name(op), vino.ino,
  	     vino.snap, inode);
a8599bd82   Sage Weil   ceph: capability ...
4031
  	mutex_lock(&session->s_mutex);
62575e270   Jeff Layton   ceph: check sessi...
4032
  	inc_session_sequence(session);
a8599bd82   Sage Weil   ceph: capability ...
4033
4034
4035
  	dout(" mds%d seq %lld cap seq %u
  ", session->s_mds, session->s_seq,
  	     (unsigned)seq);
a8599bd82   Sage Weil   ceph: capability ...
4036
4037
4038
  	if (!inode) {
  		dout(" i don't have ino %llx
  ", vino.ino);
3d7ded4d8   Sage Weil   ceph: release cap...
4039

a096b09ae   Yan, Zheng   ceph: queue cap r...
4040
  		if (op == CEPH_CAP_OP_IMPORT) {
745a8e3bc   Yan, Zheng   ceph: don't pre-a...
4041
4042
4043
  			cap = ceph_get_cap(mdsc, NULL);
  			cap->cap_ino = vino.ino;
  			cap->queue_release = 1;
779fe0fb8   Yan, Zheng   ceph: rados pool ...
4044
  			cap->cap_id = le64_to_cpu(h->cap_id);
745a8e3bc   Yan, Zheng   ceph: don't pre-a...
4045
4046
  			cap->mseq = mseq;
  			cap->seq = seq;
dc24de82d   Yan, Zheng   ceph: properly se...
4047
  			cap->issue_seq = seq;
a096b09ae   Yan, Zheng   ceph: queue cap r...
4048
  			spin_lock(&session->s_cap_lock);
e3ec8d689   Yan, Zheng   ceph: send cap re...
4049
  			__ceph_queue_cap_release(session, cap);
a096b09ae   Yan, Zheng   ceph: queue cap r...
4050
4051
  			spin_unlock(&session->s_cap_lock);
  		}
fb33c114d   Jeff Layton   ceph: flush relea...
4052
  		goto flush_cap_releases;
a8599bd82   Sage Weil   ceph: capability ...
4053
4054
4055
4056
4057
  	}
  
  	/* these will work even if we don't have a cap yet */
  	switch (op) {
  	case CEPH_CAP_OP_FLUSHSNAP_ACK:
a1c6b8358   Yan, Zheng   ceph: define argu...
4058
4059
  		handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
  					 h, session);
a8599bd82   Sage Weil   ceph: capability ...
4060
4061
4062
  		goto done;
  
  	case CEPH_CAP_OP_EXPORT:
11df2dfb6   Yan, Zheng   ceph: add importe...
4063
4064
  		handle_cap_export(inode, h, peer, session);
  		goto done_unlocked;
a8599bd82   Sage Weil   ceph: capability ...
4065
4066
  
  	case CEPH_CAP_OP_IMPORT:
982d6011b   Yan, Zheng   ceph: improve ref...
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
  		realm = NULL;
  		if (snaptrace_len) {
  			down_write(&mdsc->snap_rwsem);
  			ceph_update_snap_trace(mdsc, snaptrace,
  					       snaptrace + snaptrace_len,
  					       false, &realm);
  			downgrade_write(&mdsc->snap_rwsem);
  		} else {
  			down_read(&mdsc->snap_rwsem);
  		}
783332336   Jeff Layton   ceph: don't take ...
4077
  		spin_lock(&ci->i_ceph_lock);
4ee6a914e   Yan, Zheng   ceph: remove expo...
4078
  		handle_cap_import(mdsc, inode, h, peer, session,
a1c6b8358   Yan, Zheng   ceph: define argu...
4079
4080
4081
  				  &cap, &extra_info.issued);
  		handle_cap_grant(inode, session, cap,
  				 h, msg->middle, &extra_info);
982d6011b   Yan, Zheng   ceph: improve ref...
4082
4083
  		if (realm)
  			ceph_put_snap_realm(mdsc, realm);
2cd698be9   Yan, Zheng   ceph: handle cap ...
4084
  		goto done_unlocked;
a8599bd82   Sage Weil   ceph: capability ...
4085
4086
4087
  	}
  
  	/* the rest require a cap */
be655596b   Sage Weil   ceph: use i_ceph_...
4088
  	spin_lock(&ci->i_ceph_lock);
a1c6b8358   Yan, Zheng   ceph: define argu...
4089
  	cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
a8599bd82   Sage Weil   ceph: capability ...
4090
  	if (!cap) {
9dbd412f5   Sage Weil   ceph: fix mislead...
4091
4092
  		dout(" no cap on %p ino %llx.%llx from mds%d
  ",
a1c6b8358   Yan, Zheng   ceph: define argu...
4093
4094
  		     inode, ceph_ino(inode), ceph_snap(inode),
  		     session->s_mds);
be655596b   Sage Weil   ceph: use i_ceph_...
4095
  		spin_unlock(&ci->i_ceph_lock);
21b559de5   Greg Farnum   ceph: send cap re...
4096
  		goto flush_cap_releases;
a8599bd82   Sage Weil   ceph: capability ...
4097
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
4098
  	/* note that each of these drops i_ceph_lock for us */
a8599bd82   Sage Weil   ceph: capability ...
4099
4100
4101
  	switch (op) {
  	case CEPH_CAP_OP_REVOKE:
  	case CEPH_CAP_OP_GRANT:
a1c6b8358   Yan, Zheng   ceph: define argu...
4102
4103
4104
4105
  		__ceph_caps_issued(ci, &extra_info.issued);
  		extra_info.issued |= __ceph_caps_dirty(ci);
  		handle_cap_grant(inode, session, cap,
  				 h, msg->middle, &extra_info);
15637c8b1   Sage Weil   ceph: clean up ha...
4106
  		goto done_unlocked;
a8599bd82   Sage Weil   ceph: capability ...
4107
4108
  
  	case CEPH_CAP_OP_FLUSH_ACK:
a1c6b8358   Yan, Zheng   ceph: define argu...
4109
4110
  		handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
  				     h, session, cap);
a8599bd82   Sage Weil   ceph: capability ...
4111
4112
4113
  		break;
  
  	case CEPH_CAP_OP_TRUNC:
7391fba26   Jeff Layton   ceph: don't relea...
4114
4115
4116
4117
  		queue_trunc = handle_cap_trunc(inode, h, session);
  		spin_unlock(&ci->i_ceph_lock);
  		if (queue_trunc)
  			ceph_queue_vmtruncate(inode);
a8599bd82   Sage Weil   ceph: capability ...
4118
4119
4120
  		break;
  
  	default:
be655596b   Sage Weil   ceph: use i_ceph_...
4121
  		spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
4122
4123
4124
4125
  		pr_err("ceph_handle_caps: unknown cap op %d %s
  ", op,
  		       ceph_cap_op_name(op));
  	}
e3ec8d689   Yan, Zheng   ceph: send cap re...
4126
4127
4128
  done:
  	mutex_unlock(&session->s_mutex);
  done_unlocked:
e3ec8d689   Yan, Zheng   ceph: send cap re...
4129
  	ceph_put_string(extra_info.pool_ns);
3e1d0452e   Yan, Zheng   ceph: avoid iput_...
4130
4131
  	/* avoid calling iput_final() in mds dispatch threads */
  	ceph_async_iput(inode);
e3ec8d689   Yan, Zheng   ceph: send cap re...
4132
  	return;
21b559de5   Greg Farnum   ceph: send cap re...
4133
4134
4135
  
  flush_cap_releases:
  	/*
745a8e3bc   Yan, Zheng   ceph: don't pre-a...
4136
  	 * send any cap release message to try to move things
21b559de5   Greg Farnum   ceph: send cap re...
4137
4138
4139
  	 * along for the mds (who clearly thinks we still have this
  	 * cap).
  	 */
e3ec8d689   Yan, Zheng   ceph: send cap re...
4140
4141
  	ceph_flush_cap_releases(mdsc, session);
  	goto done;
a8599bd82   Sage Weil   ceph: capability ...
4142
4143
4144
4145
  
  bad:
  	pr_err("ceph_handle_caps: corrupt message
  ");
9ec7cab14   Sage Weil   ceph: hex dump co...
4146
  	ceph_msg_dump(msg);
a8599bd82   Sage Weil   ceph: capability ...
4147
4148
4149
4150
4151
4152
  	return;
  }
  
  /*
   * Delayed work handler to process end of delayed cap release LRU list.
   */
afcdaea3f   Sage Weil   ceph: flush dirty...
4153
  void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
a8599bd82   Sage Weil   ceph: capability ...
4154
  {
4b9f2042f   Yan, Zheng   ceph: avoid acces...
4155
  	struct inode *inode;
a8599bd82   Sage Weil   ceph: capability ...
4156
  	struct ceph_inode_info *ci;
a8599bd82   Sage Weil   ceph: capability ...
4157

a8599bd82   Sage Weil   ceph: capability ...
4158
4159
  	dout("check_delayed_caps
  ");
585d72f33   Jeff Layton   ceph: clean up an...
4160
4161
  	spin_lock(&mdsc->cap_delay_lock);
  	while (!list_empty(&mdsc->cap_delay_list)) {
a8599bd82   Sage Weil   ceph: capability ...
4162
4163
4164
4165
4166
4167
4168
  		ci = list_first_entry(&mdsc->cap_delay_list,
  				      struct ceph_inode_info,
  				      i_cap_delay_list);
  		if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
  		    time_before(jiffies, ci->i_hold_caps_max))
  			break;
  		list_del_init(&ci->i_cap_delay_list);
4b9f2042f   Yan, Zheng   ceph: avoid acces...
4169
4170
  
  		inode = igrab(&ci->vfs_inode);
4b9f2042f   Yan, Zheng   ceph: avoid acces...
4171
  		if (inode) {
585d72f33   Jeff Layton   ceph: clean up an...
4172
  			spin_unlock(&mdsc->cap_delay_lock);
4b9f2042f   Yan, Zheng   ceph: avoid acces...
4173
4174
  			dout("check_delayed_caps on %p
  ", inode);
a0d93e327   Yan, Zheng   ceph: remove dela...
4175
  			ceph_check_caps(ci, 0, NULL);
3e1d0452e   Yan, Zheng   ceph: avoid iput_...
4176
4177
  			/* avoid calling iput_final() in tick thread */
  			ceph_async_iput(inode);
585d72f33   Jeff Layton   ceph: clean up an...
4178
  			spin_lock(&mdsc->cap_delay_lock);
4b9f2042f   Yan, Zheng   ceph: avoid acces...
4179
  		}
a8599bd82   Sage Weil   ceph: capability ...
4180
4181
4182
4183
4184
  	}
  	spin_unlock(&mdsc->cap_delay_lock);
  }
  
  /*
afcdaea3f   Sage Weil   ceph: flush dirty...
4185
4186
   * Flush all dirty caps to the mds
   */
1cf03a68e   Jeff Layton   ceph: convert mds...
4187
  static void flush_dirty_session_caps(struct ceph_mds_session *s)
afcdaea3f   Sage Weil   ceph: flush dirty...
4188
  {
1cf03a68e   Jeff Layton   ceph: convert mds...
4189
  	struct ceph_mds_client *mdsc = s->s_mdsc;
db3540522   Sage Weil   ceph: fix cap flu...
4190
4191
  	struct ceph_inode_info *ci;
  	struct inode *inode;
afcdaea3f   Sage Weil   ceph: flush dirty...
4192
4193
4194
4195
  
  	dout("flush_dirty_caps
  ");
  	spin_lock(&mdsc->cap_dirty_lock);
1cf03a68e   Jeff Layton   ceph: convert mds...
4196
4197
  	while (!list_empty(&s->s_cap_dirty)) {
  		ci = list_first_entry(&s->s_cap_dirty, struct ceph_inode_info,
db3540522   Sage Weil   ceph: fix cap flu...
4198
  				      i_dirty_item);
70b666c3b   Sage Weil   ceph: use ihold w...
4199
4200
  		inode = &ci->vfs_inode;
  		ihold(inode);
db3540522   Sage Weil   ceph: fix cap flu...
4201
4202
  		dout("flush_dirty_caps %p
  ", inode);
afcdaea3f   Sage Weil   ceph: flush dirty...
4203
  		spin_unlock(&mdsc->cap_dirty_lock);
a0d93e327   Yan, Zheng   ceph: remove dela...
4204
  		ceph_check_caps(ci, CHECK_CAPS_FLUSH, NULL);
70b666c3b   Sage Weil   ceph: use ihold w...
4205
  		iput(inode);
afcdaea3f   Sage Weil   ceph: flush dirty...
4206
4207
4208
  		spin_lock(&mdsc->cap_dirty_lock);
  	}
  	spin_unlock(&mdsc->cap_dirty_lock);
db3540522   Sage Weil   ceph: fix cap flu...
4209
4210
  	dout("flush_dirty_caps done
  ");
afcdaea3f   Sage Weil   ceph: flush dirty...
4211
  }
1cf03a68e   Jeff Layton   ceph: convert mds...
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
  static void iterate_sessions(struct ceph_mds_client *mdsc,
  			     void (*cb)(struct ceph_mds_session *))
  {
  	int mds;
  
  	mutex_lock(&mdsc->mutex);
  	for (mds = 0; mds < mdsc->max_sessions; ++mds) {
  		struct ceph_mds_session *s;
  
  		if (!mdsc->sessions[mds])
  			continue;
  
  		s = ceph_get_mds_session(mdsc->sessions[mds]);
  		if (!s)
  			continue;
  
  		mutex_unlock(&mdsc->mutex);
  		cb(s);
  		ceph_put_mds_session(s);
  		mutex_lock(&mdsc->mutex);
  	}
  	mutex_unlock(&mdsc->mutex);
  }
  
  void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
  {
  	iterate_sessions(mdsc, flush_dirty_session_caps);
  }
719a2514e   Yan, Zheng   ceph: consider in...
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
  void __ceph_touch_fmode(struct ceph_inode_info *ci,
  			struct ceph_mds_client *mdsc, int fmode)
  {
  	unsigned long now = jiffies;
  	if (fmode & CEPH_FILE_MODE_RD)
  		ci->i_last_rd = now;
  	if (fmode & CEPH_FILE_MODE_WR)
  		ci->i_last_wr = now;
  	/* queue periodic check */
  	if (fmode &&
  	    __ceph_is_any_real_caps(ci) &&
  	    list_empty(&ci->i_cap_delay_list))
a0d93e327   Yan, Zheng   ceph: remove dela...
4252
  		__cap_delay_requeue(mdsc, ci);
719a2514e   Yan, Zheng   ceph: consider in...
4253
4254
4255
4256
  }
  
  void ceph_get_fmode(struct ceph_inode_info *ci, int fmode, int count)
  {
1dd8d4708   Xiubo Li   ceph: metrics for...
4257
  	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->vfs_inode.i_sb);
719a2514e   Yan, Zheng   ceph: consider in...
4258
  	int bits = (fmode << 1) | 1;
1dd8d4708   Xiubo Li   ceph: metrics for...
4259
4260
4261
4262
4263
  	bool is_opened = false;
  	int i;
  
  	if (count == 1)
  		atomic64_inc(&mdsc->metric.opened_files);
719a2514e   Yan, Zheng   ceph: consider in...
4264
4265
4266
4267
  	spin_lock(&ci->i_ceph_lock);
  	for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  		if (bits & (1 << i))
  			ci->i_nr_by_mode[i] += count;
1dd8d4708   Xiubo Li   ceph: metrics for...
4268
4269
4270
4271
4272
4273
4274
4275
  
  		/*
  		 * If any of the mode ref is larger than 1,
  		 * that means it has been already opened by
  		 * others. Just skip checking the PIN ref.
  		 */
  		if (i && ci->i_nr_by_mode[i] > 1)
  			is_opened = true;
719a2514e   Yan, Zheng   ceph: consider in...
4276
  	}
1dd8d4708   Xiubo Li   ceph: metrics for...
4277
4278
4279
  
  	if (!is_opened)
  		percpu_counter_inc(&mdsc->metric.opened_inodes);
719a2514e   Yan, Zheng   ceph: consider in...
4280
4281
  	spin_unlock(&ci->i_ceph_lock);
  }
afcdaea3f   Sage Weil   ceph: flush dirty...
4282
  /*
a8599bd82   Sage Weil   ceph: capability ...
4283
4284
4285
4286
   * Drop open file reference.  If we were the last open file,
   * we may need to release capabilities to the MDS (or schedule
   * their delayed release).
   */
719a2514e   Yan, Zheng   ceph: consider in...
4287
  void ceph_put_fmode(struct ceph_inode_info *ci, int fmode, int count)
a8599bd82   Sage Weil   ceph: capability ...
4288
  {
1dd8d4708   Xiubo Li   ceph: metrics for...
4289
  	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->vfs_inode.i_sb);
774a6a118   Yan, Zheng   ceph: reduce i_nr...
4290
  	int bits = (fmode << 1) | 1;
1dd8d4708   Xiubo Li   ceph: metrics for...
4291
4292
4293
4294
4295
  	bool is_closed = true;
  	int i;
  
  	if (count == 1)
  		atomic64_dec(&mdsc->metric.opened_files);
be655596b   Sage Weil   ceph: use i_ceph_...
4296
  	spin_lock(&ci->i_ceph_lock);
774a6a118   Yan, Zheng   ceph: reduce i_nr...
4297
4298
  	for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
  		if (bits & (1 << i)) {
719a2514e   Yan, Zheng   ceph: consider in...
4299
4300
  			BUG_ON(ci->i_nr_by_mode[i] < count);
  			ci->i_nr_by_mode[i] -= count;
774a6a118   Yan, Zheng   ceph: reduce i_nr...
4301
  		}
1dd8d4708   Xiubo Li   ceph: metrics for...
4302
4303
4304
4305
4306
4307
4308
4309
  
  		/*
  		 * If any of the mode ref is not 0 after
  		 * decreased, that means it is still opened
  		 * by others. Just skip checking the PIN ref.
  		 */
  		if (i && ci->i_nr_by_mode[i])
  			is_closed = false;
774a6a118   Yan, Zheng   ceph: reduce i_nr...
4310
  	}
1dd8d4708   Xiubo Li   ceph: metrics for...
4311
4312
4313
  
  	if (is_closed)
  		percpu_counter_dec(&mdsc->metric.opened_inodes);
be655596b   Sage Weil   ceph: use i_ceph_...
4314
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
4315
4316
4317
  }
  
  /*
a452bc063   Jeff Layton   ceph: fix comment...
4318
   * For a soon-to-be unlinked file, drop the LINK caps. If it
6ef0bc6dd   Zhi Zhang   ceph: flush dirty...
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
   * looks like the link count will hit 0, drop any other caps (other
   * than PIN) we don't specifically want (due to the file still being
   * open).
   */
  int ceph_drop_caps_for_unlink(struct inode *inode)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
  
  	spin_lock(&ci->i_ceph_lock);
  	if (inode->i_nlink == 1) {
  		drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
6ef0bc6dd   Zhi Zhang   ceph: flush dirty...
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
  		if (__ceph_caps_dirty(ci)) {
  			struct ceph_mds_client *mdsc =
  				ceph_inode_to_client(inode)->mdsc;
  			__cap_delay_requeue_front(mdsc, ci);
  		}
  	}
  	spin_unlock(&ci->i_ceph_lock);
  	return drop;
  }
  
  /*
a8599bd82   Sage Weil   ceph: capability ...
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
   * Helpers for embedding cap and dentry lease releases into mds
   * requests.
   *
   * @force is used by dentry_release (below) to force inclusion of a
   * record for the directory inode, even when there aren't any caps to
   * drop.
   */
  int ceph_encode_inode_release(void **p, struct inode *inode,
  			      int mds, int drop, int unless, int force)
  {
  	struct ceph_inode_info *ci = ceph_inode(inode);
  	struct ceph_cap *cap;
  	struct ceph_mds_request_release *rel = *p;
ec97f88ba   Sage Weil   ceph: only releas...
4355
  	int used, dirty;
a8599bd82   Sage Weil   ceph: capability ...
4356
  	int ret = 0;
a8599bd82   Sage Weil   ceph: capability ...
4357

be655596b   Sage Weil   ceph: use i_ceph_...
4358
  	spin_lock(&ci->i_ceph_lock);
916623da1   Sage Weil   ceph: only releas...
4359
  	used = __ceph_caps_used(ci);
ec97f88ba   Sage Weil   ceph: only releas...
4360
  	dirty = __ceph_caps_dirty(ci);
916623da1   Sage Weil   ceph: only releas...
4361

ec97f88ba   Sage Weil   ceph: only releas...
4362
4363
4364
  	dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s
  ",
  	     inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
916623da1   Sage Weil   ceph: only releas...
4365
  	     ceph_cap_string(unless));
ec97f88ba   Sage Weil   ceph: only releas...
4366
4367
  	/* only drop unused, clean caps */
  	drop &= ~(used | dirty);
916623da1   Sage Weil   ceph: only releas...
4368

a8599bd82   Sage Weil   ceph: capability ...
4369
4370
  	cap = __get_cap_for_mds(ci, mds);
  	if (cap && __cap_is_valid(cap)) {
222b7f90b   Yan, Zheng   ceph: voluntarily...
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
  		unless &= cap->issued;
  		if (unless) {
  			if (unless & CEPH_CAP_AUTH_EXCL)
  				drop &= ~CEPH_CAP_AUTH_SHARED;
  			if (unless & CEPH_CAP_LINK_EXCL)
  				drop &= ~CEPH_CAP_LINK_SHARED;
  			if (unless & CEPH_CAP_XATTR_EXCL)
  				drop &= ~CEPH_CAP_XATTR_SHARED;
  			if (unless & CEPH_CAP_FILE_EXCL)
  				drop &= ~CEPH_CAP_FILE_SHARED;
  		}
  
  		if (force || (cap->issued & drop)) {
  			if (cap->issued & drop) {
bb137f84d   Yan, Zheng   ceph: fix cap rel...
4385
  				int wanted = __ceph_caps_wanted(ci);
bb137f84d   Yan, Zheng   ceph: fix cap rel...
4386
4387
4388
  				dout("encode_inode_release %p cap %p "
  				     "%s -> %s, wanted %s -> %s
  ", inode, cap,
a8599bd82   Sage Weil   ceph: capability ...
4389
  				     ceph_cap_string(cap->issued),
bb137f84d   Yan, Zheng   ceph: fix cap rel...
4390
4391
4392
  				     ceph_cap_string(cap->issued & ~drop),
  				     ceph_cap_string(cap->mds_wanted),
  				     ceph_cap_string(wanted));
a8599bd82   Sage Weil   ceph: capability ...
4393
4394
  				cap->issued &= ~drop;
  				cap->implemented &= ~drop;
bb137f84d   Yan, Zheng   ceph: fix cap rel...
4395
  				cap->mds_wanted = wanted;
6f05b30ea   Yan, Zheng   ceph: reset i_req...
4396
4397
4398
  				if (cap == ci->i_auth_cap &&
  				    !(wanted & CEPH_CAP_ANY_FILE_WR))
  					ci->i_requested_max_size = 0;
a8599bd82   Sage Weil   ceph: capability ...
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
  			} else {
  				dout("encode_inode_release %p cap %p %s"
  				     " (force)
  ", inode, cap,
  				     ceph_cap_string(cap->issued));
  			}
  
  			rel->ino = cpu_to_le64(ceph_ino(inode));
  			rel->cap_id = cpu_to_le64(cap->cap_id);
  			rel->seq = cpu_to_le32(cap->seq);
08a0f24e4   Himangi Saraogi   ceph: replace com...
4409
  			rel->issue_seq = cpu_to_le32(cap->issue_seq);
a8599bd82   Sage Weil   ceph: capability ...
4410
  			rel->mseq = cpu_to_le32(cap->mseq);
fd7b95cd1   Yan, Zheng   ceph: avoid relea...
4411
  			rel->caps = cpu_to_le32(cap->implemented);
a8599bd82   Sage Weil   ceph: capability ...
4412
4413
4414
4415
4416
4417
  			rel->wanted = cpu_to_le32(cap->mds_wanted);
  			rel->dname_len = 0;
  			rel->dname_seq = 0;
  			*p += sizeof(*rel);
  			ret = 1;
  		} else {
222b7f90b   Yan, Zheng   ceph: voluntarily...
4418
4419
  			dout("encode_inode_release %p cap %p %s (noop)
  ",
a8599bd82   Sage Weil   ceph: capability ...
4420
4421
4422
  			     inode, cap, ceph_cap_string(cap->issued));
  		}
  	}
be655596b   Sage Weil   ceph: use i_ceph_...
4423
  	spin_unlock(&ci->i_ceph_lock);
a8599bd82   Sage Weil   ceph: capability ...
4424
4425
4426
4427
  	return ret;
  }
  
  int ceph_encode_dentry_release(void **p, struct dentry *dentry,
ca6c8ae0f   Jeff Layton   ceph: pass parent...
4428
  			       struct inode *dir,
a8599bd82   Sage Weil   ceph: capability ...
4429
4430
  			       int mds, int drop, int unless)
  {
ca6c8ae0f   Jeff Layton   ceph: pass parent...
4431
  	struct dentry *parent = NULL;
a8599bd82   Sage Weil   ceph: capability ...
4432
4433
4434
4435
4436
4437
4438
  	struct ceph_mds_request_release *rel = *p;
  	struct ceph_dentry_info *di = ceph_dentry(dentry);
  	int force = 0;
  	int ret;
  
  	/*
  	 * force an record for the directory caps if we have a dentry lease.
be655596b   Sage Weil   ceph: use i_ceph_...
4439
  	 * this is racy (can't take i_ceph_lock and d_lock together), but it
a8599bd82   Sage Weil   ceph: capability ...
4440
4441
4442
4443
4444
4445
  	 * doesn't have to be perfect; the mds will revoke anything we don't
  	 * release.
  	 */
  	spin_lock(&dentry->d_lock);
  	if (di->lease_session && di->lease_session->s_mds == mds)
  		force = 1;
ca6c8ae0f   Jeff Layton   ceph: pass parent...
4446
4447
4448
4449
  	if (!dir) {
  		parent = dget(dentry->d_parent);
  		dir = d_inode(parent);
  	}
a8599bd82   Sage Weil   ceph: capability ...
4450
  	spin_unlock(&dentry->d_lock);
ca6c8ae0f   Jeff Layton   ceph: pass parent...
4451
  	ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
adf0d6870   Jeff Layton   ceph: fix unsafe ...
4452
  	dput(parent);
a8599bd82   Sage Weil   ceph: capability ...
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
  
  	spin_lock(&dentry->d_lock);
  	if (ret && di->lease_session && di->lease_session->s_mds == mds) {
  		dout("encode_dentry_release %p mds%d seq %d
  ",
  		     dentry, mds, (int)di->lease_seq);
  		rel->dname_len = cpu_to_le32(dentry->d_name.len);
  		memcpy(*p, dentry->d_name.name, dentry->d_name.len);
  		*p += dentry->d_name.len;
  		rel->dname_seq = cpu_to_le32(di->lease_seq);
1dadcce35   Sage Weil   ceph: fix dentry ...
4463
  		__ceph_mdsc_drop_dentry_lease(dentry);
a8599bd82   Sage Weil   ceph: capability ...
4464
4465
4466
4467
  	}
  	spin_unlock(&dentry->d_lock);
  	return ret;
  }