blob: bba28a5034ba39e53ce68b32ecad1d03b8d60ba7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weila8599bd2009-10-06 11:31:12 -07003
4#include <linux/fs.h>
5#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +01006#include <linux/sched/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Sage Weila8599bd2009-10-06 11:31:12 -07008#include <linux/vmalloc.h>
9#include <linux/wait.h>
Stephen Rothwellf1a3d572010-01-18 11:53:08 +110010#include <linux/writeback.h>
Sage Weila8599bd2009-10-06 11:31:12 -070011
12#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040014#include "cache.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/decode.h>
16#include <linux/ceph/messenger.h>
Sage Weila8599bd2009-10-06 11:31:12 -070017
18/*
19 * Capability management
20 *
21 * The Ceph metadata servers control client access to inode metadata
22 * and file data by issuing capabilities, granting clients permission
23 * to read and/or write both inode field and file data to OSDs
24 * (storage nodes). Each capability consists of a set of bits
25 * indicating which operations are allowed.
26 *
27 * If the client holds a *_SHARED cap, the client has a coherent value
28 * that can be safely read from the cached inode.
29 *
30 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
31 * client is allowed to change inode attributes (e.g., file size,
32 * mtime), note its dirty state in the ceph_cap, and asynchronously
33 * flush that metadata change to the MDS.
34 *
35 * In the event of a conflicting operation (perhaps by another
36 * client), the MDS will revoke the conflicting client capabilities.
37 *
38 * In order for a client to cache an inode, it must hold a capability
39 * with at least one MDS server. When inodes are released, release
40 * notifications are batched and periodically sent en masse to the MDS
41 * cluster to release server state.
42 */
43
Yan, Zheng0e294382016-07-04 18:06:41 +080044static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
Yan, Zheng7bc00fd2016-07-07 18:34:45 +080045static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
46 struct ceph_mds_session *session,
47 struct ceph_inode_info *ci,
48 u64 oldest_flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -070049
50/*
51 * Generate readable cap strings for debugging output.
52 */
53#define MAX_CAP_STR 20
54static char cap_str[MAX_CAP_STR][40];
55static DEFINE_SPINLOCK(cap_str_lock);
56static int last_cap_str;
57
58static char *gcap_string(char *s, int c)
59{
60 if (c & CEPH_CAP_GSHARED)
61 *s++ = 's';
62 if (c & CEPH_CAP_GEXCL)
63 *s++ = 'x';
64 if (c & CEPH_CAP_GCACHE)
65 *s++ = 'c';
66 if (c & CEPH_CAP_GRD)
67 *s++ = 'r';
68 if (c & CEPH_CAP_GWR)
69 *s++ = 'w';
70 if (c & CEPH_CAP_GBUFFER)
71 *s++ = 'b';
Yan, Zheng49a9f4f2018-04-25 17:30:23 +080072 if (c & CEPH_CAP_GWREXTEND)
73 *s++ = 'a';
Sage Weila8599bd2009-10-06 11:31:12 -070074 if (c & CEPH_CAP_GLAZYIO)
75 *s++ = 'l';
76 return s;
77}
78
79const char *ceph_cap_string(int caps)
80{
81 int i;
82 char *s;
83 int c;
84
85 spin_lock(&cap_str_lock);
86 i = last_cap_str++;
87 if (last_cap_str == MAX_CAP_STR)
88 last_cap_str = 0;
89 spin_unlock(&cap_str_lock);
90
91 s = cap_str[i];
92
93 if (caps & CEPH_CAP_PIN)
94 *s++ = 'p';
95
96 c = (caps >> CEPH_CAP_SAUTH) & 3;
97 if (c) {
98 *s++ = 'A';
99 s = gcap_string(s, c);
100 }
101
102 c = (caps >> CEPH_CAP_SLINK) & 3;
103 if (c) {
104 *s++ = 'L';
105 s = gcap_string(s, c);
106 }
107
108 c = (caps >> CEPH_CAP_SXATTR) & 3;
109 if (c) {
110 *s++ = 'X';
111 s = gcap_string(s, c);
112 }
113
114 c = caps >> CEPH_CAP_SFILE;
115 if (c) {
116 *s++ = 'F';
117 s = gcap_string(s, c);
118 }
119
120 if (s == cap_str[i])
121 *s++ = '-';
122 *s = 0;
123 return cap_str[i];
124}
125
Yehuda Sadeh37151662010-06-17 16:16:12 -0700126void ceph_caps_init(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -0700127{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700128 INIT_LIST_HEAD(&mdsc->caps_list);
129 spin_lock_init(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700130}
131
Yehuda Sadeh37151662010-06-17 16:16:12 -0700132void ceph_caps_finalize(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -0700133{
134 struct ceph_cap *cap;
135
Yehuda Sadeh37151662010-06-17 16:16:12 -0700136 spin_lock(&mdsc->caps_list_lock);
137 while (!list_empty(&mdsc->caps_list)) {
138 cap = list_first_entry(&mdsc->caps_list,
139 struct ceph_cap, caps_item);
Sage Weila8599bd2009-10-06 11:31:12 -0700140 list_del(&cap->caps_item);
141 kmem_cache_free(ceph_cap_cachep, cap);
142 }
Yehuda Sadeh37151662010-06-17 16:16:12 -0700143 mdsc->caps_total_count = 0;
144 mdsc->caps_avail_count = 0;
145 mdsc->caps_use_count = 0;
146 mdsc->caps_reserve_count = 0;
147 mdsc->caps_min_count = 0;
148 spin_unlock(&mdsc->caps_list_lock);
Sage Weil85ccce42010-02-17 10:02:43 -0800149}
150
Yehuda Sadeh37151662010-06-17 16:16:12 -0700151void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
Sage Weil85ccce42010-02-17 10:02:43 -0800152{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700153 spin_lock(&mdsc->caps_list_lock);
154 mdsc->caps_min_count += delta;
155 BUG_ON(mdsc->caps_min_count < 0);
156 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700157}
158
Chengguang Xu7bf8f732018-07-28 23:15:35 +0800159static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
160{
161 struct ceph_cap *cap;
162 int i;
163
164 if (nr_caps) {
165 BUG_ON(mdsc->caps_reserve_count < nr_caps);
166 mdsc->caps_reserve_count -= nr_caps;
167 if (mdsc->caps_avail_count >=
168 mdsc->caps_reserve_count + mdsc->caps_min_count) {
169 mdsc->caps_total_count -= nr_caps;
170 for (i = 0; i < nr_caps; i++) {
171 cap = list_first_entry(&mdsc->caps_list,
172 struct ceph_cap, caps_item);
173 list_del(&cap->caps_item);
174 kmem_cache_free(ceph_cap_cachep, cap);
175 }
176 } else {
177 mdsc->caps_avail_count += nr_caps;
178 }
179
180 dout("%s: caps %d = %d used + %d resv + %d avail\n",
181 __func__,
182 mdsc->caps_total_count, mdsc->caps_use_count,
183 mdsc->caps_reserve_count, mdsc->caps_avail_count);
184 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
185 mdsc->caps_reserve_count +
186 mdsc->caps_avail_count);
187 }
188}
189
Zhi Zhange30ee582018-01-24 21:24:33 +0800190/*
191 * Called under mdsc->mutex.
192 */
193int ceph_reserve_caps(struct ceph_mds_client *mdsc,
Yehuda Sadeh37151662010-06-17 16:16:12 -0700194 struct ceph_cap_reservation *ctx, int need)
Sage Weila8599bd2009-10-06 11:31:12 -0700195{
Zhi Zhange30ee582018-01-24 21:24:33 +0800196 int i, j;
Sage Weila8599bd2009-10-06 11:31:12 -0700197 struct ceph_cap *cap;
198 int have;
199 int alloc = 0;
Zhi Zhange30ee582018-01-24 21:24:33 +0800200 int max_caps;
Chengguang Xue5bc08d2018-07-28 23:15:37 +0800201 int err = 0;
Zhi Zhange30ee582018-01-24 21:24:33 +0800202 bool trimmed = false;
203 struct ceph_mds_session *s;
Sage Weila8599bd2009-10-06 11:31:12 -0700204 LIST_HEAD(newcaps);
Sage Weila8599bd2009-10-06 11:31:12 -0700205
206 dout("reserve caps ctx=%p need=%d\n", ctx, need);
207
208 /* first reserve any caps that are already allocated */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700209 spin_lock(&mdsc->caps_list_lock);
210 if (mdsc->caps_avail_count >= need)
Sage Weila8599bd2009-10-06 11:31:12 -0700211 have = need;
212 else
Yehuda Sadeh37151662010-06-17 16:16:12 -0700213 have = mdsc->caps_avail_count;
214 mdsc->caps_avail_count -= have;
215 mdsc->caps_reserve_count += have;
216 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
217 mdsc->caps_reserve_count +
218 mdsc->caps_avail_count);
219 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700220
Chengguang Xu79cd6742018-02-24 18:36:02 +0800221 for (i = have; i < need; ) {
Sage Weila8599bd2009-10-06 11:31:12 -0700222 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
Chengguang Xu79cd6742018-02-24 18:36:02 +0800223 if (cap) {
224 list_add(&cap->caps_item, &newcaps);
225 alloc++;
226 i++;
227 continue;
Zhi Zhange30ee582018-01-24 21:24:33 +0800228 }
Chengguang Xu79cd6742018-02-24 18:36:02 +0800229
230 if (!trimmed) {
231 for (j = 0; j < mdsc->max_sessions; j++) {
232 s = __ceph_lookup_mds_session(mdsc, j);
233 if (!s)
234 continue;
235 mutex_unlock(&mdsc->mutex);
236
237 mutex_lock(&s->s_mutex);
238 max_caps = s->s_nr_caps - (need - i);
239 ceph_trim_caps(mdsc, s, max_caps);
240 mutex_unlock(&s->s_mutex);
241
242 ceph_put_mds_session(s);
243 mutex_lock(&mdsc->mutex);
244 }
245 trimmed = true;
246
247 spin_lock(&mdsc->caps_list_lock);
248 if (mdsc->caps_avail_count) {
249 int more_have;
250 if (mdsc->caps_avail_count >= need - i)
251 more_have = need - i;
252 else
253 more_have = mdsc->caps_avail_count;
254
255 i += more_have;
256 have += more_have;
257 mdsc->caps_avail_count -= more_have;
258 mdsc->caps_reserve_count += more_have;
259
260 }
261 spin_unlock(&mdsc->caps_list_lock);
262
263 continue;
264 }
265
266 pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n",
267 ctx, need, have + alloc);
Chengguang Xue5bc08d2018-07-28 23:15:37 +0800268 err = -ENOMEM;
269 break;
Sage Weila8599bd2009-10-06 11:31:12 -0700270 }
Chengguang Xue5bc08d2018-07-28 23:15:37 +0800271
272 if (!err) {
273 BUG_ON(have + alloc != need);
274 ctx->count = need;
275 }
Sage Weila8599bd2009-10-06 11:31:12 -0700276
Yehuda Sadeh37151662010-06-17 16:16:12 -0700277 spin_lock(&mdsc->caps_list_lock);
278 mdsc->caps_total_count += alloc;
279 mdsc->caps_reserve_count += alloc;
280 list_splice(&newcaps, &mdsc->caps_list);
Sage Weila8599bd2009-10-06 11:31:12 -0700281
Yehuda Sadeh37151662010-06-17 16:16:12 -0700282 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
283 mdsc->caps_reserve_count +
284 mdsc->caps_avail_count);
Chengguang Xue5bc08d2018-07-28 23:15:37 +0800285
286 if (err)
287 __ceph_unreserve_caps(mdsc, have + alloc);
288
Yehuda Sadeh37151662010-06-17 16:16:12 -0700289 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700290
Sage Weila8599bd2009-10-06 11:31:12 -0700291 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700292 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
293 mdsc->caps_reserve_count, mdsc->caps_avail_count);
Chengguang Xue5bc08d2018-07-28 23:15:37 +0800294 return err;
Sage Weila8599bd2009-10-06 11:31:12 -0700295}
296
Chengguang Xu7bf8f732018-07-28 23:15:35 +0800297void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
Yehuda Sadeh37151662010-06-17 16:16:12 -0700298 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700299{
300 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
Chengguang Xu7bf8f732018-07-28 23:15:35 +0800301 spin_lock(&mdsc->caps_list_lock);
302 __ceph_unreserve_caps(mdsc, ctx->count);
303 ctx->count = 0;
304 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700305}
306
Yan, Zhengd9df2782014-04-18 09:57:11 +0800307struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
308 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700309{
310 struct ceph_cap *cap = NULL;
311
312 /* temporary, until we do something about cap import/export */
Sage Weil443b3762010-06-29 09:28:39 -0700313 if (!ctx) {
314 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
315 if (cap) {
Yan, Zheng4d1d05342012-11-03 10:32:37 +0800316 spin_lock(&mdsc->caps_list_lock);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700317 mdsc->caps_use_count++;
318 mdsc->caps_total_count++;
Yan, Zheng4d1d05342012-11-03 10:32:37 +0800319 spin_unlock(&mdsc->caps_list_lock);
Chengguang Xue327ce02018-02-24 18:35:29 +0800320 } else {
321 spin_lock(&mdsc->caps_list_lock);
322 if (mdsc->caps_avail_count) {
323 BUG_ON(list_empty(&mdsc->caps_list));
324
325 mdsc->caps_avail_count--;
326 mdsc->caps_use_count++;
327 cap = list_first_entry(&mdsc->caps_list,
328 struct ceph_cap, caps_item);
329 list_del(&cap->caps_item);
330
331 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
332 mdsc->caps_reserve_count + mdsc->caps_avail_count);
333 }
334 spin_unlock(&mdsc->caps_list_lock);
Sage Weil443b3762010-06-29 09:28:39 -0700335 }
Chengguang Xue327ce02018-02-24 18:35:29 +0800336
Sage Weil443b3762010-06-29 09:28:39 -0700337 return cap;
338 }
Sage Weila8599bd2009-10-06 11:31:12 -0700339
Yehuda Sadeh37151662010-06-17 16:16:12 -0700340 spin_lock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700341 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700342 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
343 mdsc->caps_reserve_count, mdsc->caps_avail_count);
Sage Weila8599bd2009-10-06 11:31:12 -0700344 BUG_ON(!ctx->count);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700345 BUG_ON(ctx->count > mdsc->caps_reserve_count);
346 BUG_ON(list_empty(&mdsc->caps_list));
Sage Weila8599bd2009-10-06 11:31:12 -0700347
348 ctx->count--;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700349 mdsc->caps_reserve_count--;
350 mdsc->caps_use_count++;
Sage Weila8599bd2009-10-06 11:31:12 -0700351
Yehuda Sadeh37151662010-06-17 16:16:12 -0700352 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
Sage Weila8599bd2009-10-06 11:31:12 -0700353 list_del(&cap->caps_item);
354
Yehuda Sadeh37151662010-06-17 16:16:12 -0700355 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
356 mdsc->caps_reserve_count + mdsc->caps_avail_count);
357 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700358 return cap;
359}
360
Yehuda Sadeh37151662010-06-17 16:16:12 -0700361void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700362{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700363 spin_lock(&mdsc->caps_list_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800364 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700365 cap, mdsc->caps_total_count, mdsc->caps_use_count,
366 mdsc->caps_reserve_count, mdsc->caps_avail_count);
367 mdsc->caps_use_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700368 /*
Sage Weil85ccce42010-02-17 10:02:43 -0800369 * Keep some preallocated caps around (ceph_min_count), to
370 * avoid lots of free/alloc churn.
Sage Weila8599bd2009-10-06 11:31:12 -0700371 */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700372 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
373 mdsc->caps_min_count) {
374 mdsc->caps_total_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700375 kmem_cache_free(ceph_cap_cachep, cap);
376 } else {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700377 mdsc->caps_avail_count++;
378 list_add(&cap->caps_item, &mdsc->caps_list);
Sage Weila8599bd2009-10-06 11:31:12 -0700379 }
380
Yehuda Sadeh37151662010-06-17 16:16:12 -0700381 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
382 mdsc->caps_reserve_count + mdsc->caps_avail_count);
383 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700384}
385
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700386void ceph_reservation_status(struct ceph_fs_client *fsc,
Sage Weil85ccce42010-02-17 10:02:43 -0800387 int *total, int *avail, int *used, int *reserved,
388 int *min)
Sage Weila8599bd2009-10-06 11:31:12 -0700389{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700390 struct ceph_mds_client *mdsc = fsc->mdsc;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700391
Chengguang Xub8840142018-02-23 17:09:38 +0800392 spin_lock(&mdsc->caps_list_lock);
393
Sage Weila8599bd2009-10-06 11:31:12 -0700394 if (total)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700395 *total = mdsc->caps_total_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700396 if (avail)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700397 *avail = mdsc->caps_avail_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700398 if (used)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700399 *used = mdsc->caps_use_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700400 if (reserved)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700401 *reserved = mdsc->caps_reserve_count;
Sage Weil85ccce42010-02-17 10:02:43 -0800402 if (min)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700403 *min = mdsc->caps_min_count;
Chengguang Xub8840142018-02-23 17:09:38 +0800404
405 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700406}
407
408/*
409 * Find ceph_cap for given mds, if any.
410 *
Sage Weilbe655592011-11-30 09:47:09 -0800411 * Called with i_ceph_lock held.
Sage Weila8599bd2009-10-06 11:31:12 -0700412 */
413static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
414{
415 struct ceph_cap *cap;
416 struct rb_node *n = ci->i_caps.rb_node;
417
418 while (n) {
419 cap = rb_entry(n, struct ceph_cap, ci_node);
420 if (mds < cap->mds)
421 n = n->rb_left;
422 else if (mds > cap->mds)
423 n = n->rb_right;
424 else
425 return cap;
426 }
427 return NULL;
428}
429
Greg Farnum2bc50252010-06-30 12:44:34 -0700430struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
431{
432 struct ceph_cap *cap;
433
Sage Weilbe655592011-11-30 09:47:09 -0800434 spin_lock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700435 cap = __get_cap_for_mds(ci, mds);
Sage Weilbe655592011-11-30 09:47:09 -0800436 spin_unlock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700437 return cap;
438}
439
Sage Weila8599bd2009-10-06 11:31:12 -0700440/*
Sage Weil33caad32010-05-26 14:31:27 -0700441 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
Sage Weila8599bd2009-10-06 11:31:12 -0700442 */
Sage Weilca81f3f2010-06-10 14:21:36 -0700443static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
Sage Weila8599bd2009-10-06 11:31:12 -0700444{
445 struct ceph_cap *cap;
446 int mds = -1;
447 struct rb_node *p;
448
Sage Weil33caad32010-05-26 14:31:27 -0700449 /* prefer mds with WR|BUFFER|EXCL caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700450 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
451 cap = rb_entry(p, struct ceph_cap, ci_node);
452 mds = cap->mds;
Sage Weila8599bd2009-10-06 11:31:12 -0700453 if (cap->issued & (CEPH_CAP_FILE_WR |
454 CEPH_CAP_FILE_BUFFER |
455 CEPH_CAP_FILE_EXCL))
456 break;
457 }
458 return mds;
459}
460
461int ceph_get_cap_mds(struct inode *inode)
462{
Sage Weilbe655592011-11-30 09:47:09 -0800463 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -0700464 int mds;
Sage Weilbe655592011-11-30 09:47:09 -0800465 spin_lock(&ci->i_ceph_lock);
Sage Weilca81f3f2010-06-10 14:21:36 -0700466 mds = __ceph_get_cap_mds(ceph_inode(inode));
Sage Weilbe655592011-11-30 09:47:09 -0800467 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700468 return mds;
469}
470
471/*
Sage Weilbe655592011-11-30 09:47:09 -0800472 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700473 */
474static void __insert_cap_node(struct ceph_inode_info *ci,
475 struct ceph_cap *new)
476{
477 struct rb_node **p = &ci->i_caps.rb_node;
478 struct rb_node *parent = NULL;
479 struct ceph_cap *cap = NULL;
480
481 while (*p) {
482 parent = *p;
483 cap = rb_entry(parent, struct ceph_cap, ci_node);
484 if (new->mds < cap->mds)
485 p = &(*p)->rb_left;
486 else if (new->mds > cap->mds)
487 p = &(*p)->rb_right;
488 else
489 BUG();
490 }
491
492 rb_link_node(&new->ci_node, parent, p);
493 rb_insert_color(&new->ci_node, &ci->i_caps);
494}
495
496/*
497 * (re)set cap hold timeouts, which control the delayed release
498 * of unused caps back to the MDS. Should be called on cap use.
499 */
500static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
501 struct ceph_inode_info *ci)
502{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700503 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
Sage Weila8599bd2009-10-06 11:31:12 -0700504
505 ci->i_hold_caps_min = round_jiffies(jiffies +
506 ma->caps_wanted_delay_min * HZ);
507 ci->i_hold_caps_max = round_jiffies(jiffies +
508 ma->caps_wanted_delay_max * HZ);
509 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
510 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
511}
512
513/*
514 * (Re)queue cap at the end of the delayed cap release list.
515 *
516 * If I_FLUSH is set, leave the inode at the front of the list.
517 *
Sage Weilbe655592011-11-30 09:47:09 -0800518 * Caller holds i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -0700519 * -> we take mdsc->cap_delay_lock
520 */
521static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
Xuehan Xu66802882018-10-11 17:55:39 +0800522 struct ceph_inode_info *ci,
523 bool set_timeout)
Sage Weila8599bd2009-10-06 11:31:12 -0700524{
Sage Weila8599bd2009-10-06 11:31:12 -0700525 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
526 ci->i_ceph_flags, ci->i_hold_caps_max);
527 if (!mdsc->stopping) {
528 spin_lock(&mdsc->cap_delay_lock);
529 if (!list_empty(&ci->i_cap_delay_list)) {
530 if (ci->i_ceph_flags & CEPH_I_FLUSH)
531 goto no_change;
532 list_del_init(&ci->i_cap_delay_list);
533 }
Xuehan Xu66802882018-10-11 17:55:39 +0800534 if (set_timeout)
535 __cap_set_timeouts(mdsc, ci);
Sage Weila8599bd2009-10-06 11:31:12 -0700536 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
537no_change:
538 spin_unlock(&mdsc->cap_delay_lock);
539 }
540}
541
542/*
543 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
544 * indicating we should send a cap message to flush dirty metadata
545 * asap, and move to the front of the delayed cap list.
546 */
547static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
548 struct ceph_inode_info *ci)
549{
550 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
551 spin_lock(&mdsc->cap_delay_lock);
552 ci->i_ceph_flags |= CEPH_I_FLUSH;
553 if (!list_empty(&ci->i_cap_delay_list))
554 list_del_init(&ci->i_cap_delay_list);
555 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
556 spin_unlock(&mdsc->cap_delay_lock);
557}
558
559/*
560 * Cancel delayed work on cap.
561 *
Sage Weilbe655592011-11-30 09:47:09 -0800562 * Caller must hold i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700563 */
564static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
565 struct ceph_inode_info *ci)
566{
567 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
568 if (list_empty(&ci->i_cap_delay_list))
569 return;
570 spin_lock(&mdsc->cap_delay_lock);
571 list_del_init(&ci->i_cap_delay_list);
572 spin_unlock(&mdsc->cap_delay_lock);
573}
574
575/*
576 * Common issue checks for add_cap, handle_cap_grant.
577 */
578static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
579 unsigned issued)
580{
581 unsigned had = __ceph_caps_issued(ci, NULL);
582
583 /*
584 * Each time we receive FILE_CACHE anew, we increment
585 * i_rdcache_gen.
586 */
Sage Weil29625072010-05-27 10:40:43 -0700587 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400588 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -0700589 ci->i_rdcache_gen++;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400590 }
Sage Weila8599bd2009-10-06 11:31:12 -0700591
592 /*
Yan, Zheng15b51bd2017-09-06 10:15:16 +0800593 * If FILE_SHARED is newly issued, mark dir not complete. We don't
594 * know what happened to this directory while we didn't have the cap.
595 * If FILE_SHARED is being revoked, also mark dir not complete. It
596 * stops on-going cached readdir.
Sage Weila8599bd2009-10-06 11:31:12 -0700597 */
Yan, Zheng15b51bd2017-09-06 10:15:16 +0800598 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
599 if (issued & CEPH_CAP_FILE_SHARED)
Yan, Zheng97aeb6b2017-11-27 10:47:46 +0800600 atomic_inc(&ci->i_shared_gen);
Yan, Zhenga8673d62013-02-18 16:38:14 +0800601 if (S_ISDIR(ci->vfs_inode.i_mode)) {
602 dout(" marking %p NOT complete\n", &ci->vfs_inode);
Yan, Zheng2f276c52013-03-13 19:44:32 +0800603 __ceph_dir_clear_complete(ci);
Yan, Zhenga8673d62013-02-18 16:38:14 +0800604 }
Sage Weila8599bd2009-10-06 11:31:12 -0700605 }
606}
607
608/*
609 * Add a capability under the given MDS session.
610 *
611 * Caller should hold session snap_rwsem (read) and s_mutex.
612 *
613 * @fmode is the open file mode, if we are opening a file, otherwise
614 * it is < 0. (This is so we can atomically add the cap and add an
615 * open file reference to it.)
616 */
Yan, Zhengd9df2782014-04-18 09:57:11 +0800617void ceph_add_cap(struct inode *inode,
618 struct ceph_mds_session *session, u64 cap_id,
619 int fmode, unsigned issued, unsigned wanted,
620 unsigned seq, unsigned mseq, u64 realmino, int flags,
621 struct ceph_cap **new_cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700622{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700623 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -0700624 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -0700625 struct ceph_cap *cap;
626 int mds = session->s_mds;
627 int actual_wanted;
628
629 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
630 session->s_mds, cap_id, ceph_cap_string(issued), seq);
631
632 /*
633 * If we are opening the file, include file mode wanted bits
634 * in wanted.
635 */
636 if (fmode >= 0)
637 wanted |= ceph_caps_for_mode(fmode);
638
Sage Weila8599bd2009-10-06 11:31:12 -0700639 cap = __get_cap_for_mds(ci, mds);
640 if (!cap) {
Yan, Zhengd9df2782014-04-18 09:57:11 +0800641 cap = *new_cap;
642 *new_cap = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -0700643
644 cap->issued = 0;
645 cap->implemented = 0;
646 cap->mds = mds;
647 cap->mds_wanted = 0;
Yan, Zheng964266c2013-02-27 09:26:09 +0800648 cap->mseq = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700649
650 cap->ci = ci;
651 __insert_cap_node(ci, cap);
652
Sage Weila8599bd2009-10-06 11:31:12 -0700653 /* add to session cap list */
654 cap->session = session;
655 spin_lock(&session->s_cap_lock);
656 list_add_tail(&cap->session_caps, &session->s_caps);
657 session->s_nr_caps++;
658 spin_unlock(&session->s_cap_lock);
Yan, Zheng11df2df2013-11-24 14:44:38 +0800659 } else {
Yan, Zhengd2f8bb22018-12-10 16:35:09 +0800660 if (cap->cap_gen < session->s_cap_gen)
661 cap->issued = cap->implemented = CEPH_CAP_PIN;
662
Yan, Zheng11df2df2013-11-24 14:44:38 +0800663 /*
664 * auth mds of the inode changed. we received the cap export
665 * message, but still haven't received the cap import message.
666 * handle_cap_export() updated the new auth MDS' cap.
667 *
668 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
669 * a message that was send before the cap import message. So
670 * don't remove caps.
671 */
672 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
673 WARN_ON(cap != ci->i_auth_cap);
674 WARN_ON(cap->cap_id != cap_id);
675 seq = cap->seq;
676 mseq = cap->mseq;
677 issued |= cap->issued;
678 flags |= CEPH_CAP_FLAG_AUTH;
679 }
680 }
Sage Weila8599bd2009-10-06 11:31:12 -0700681
Yan, Zheng7d9c9192017-12-19 18:00:54 +0800682 if (!ci->i_snap_realm ||
683 ((flags & CEPH_CAP_FLAG_AUTH) &&
684 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
Sage Weila8599bd2009-10-06 11:31:12 -0700685 /*
686 * add this inode to the appropriate snap realm
687 */
688 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
689 realmino);
690 if (realm) {
Yan, Zheng7d9c9192017-12-19 18:00:54 +0800691 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
692 if (oldrealm) {
693 spin_lock(&oldrealm->inodes_with_caps_lock);
694 list_del_init(&ci->i_snap_realm_item);
695 spin_unlock(&oldrealm->inodes_with_caps_lock);
696 }
697
Sage Weila8599bd2009-10-06 11:31:12 -0700698 spin_lock(&realm->inodes_with_caps_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700699 list_add(&ci->i_snap_realm_item,
700 &realm->inodes_with_caps);
Luis Henriquese3161f12018-01-12 17:19:28 +0000701 ci->i_snap_realm = realm;
702 if (realm->ino == ci->i_vino.ino)
703 realm->inode = inode;
Sage Weila8599bd2009-10-06 11:31:12 -0700704 spin_unlock(&realm->inodes_with_caps_lock);
Yan, Zheng7d9c9192017-12-19 18:00:54 +0800705
706 if (oldrealm)
707 ceph_put_snap_realm(mdsc, oldrealm);
Sage Weila8599bd2009-10-06 11:31:12 -0700708 } else {
709 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
710 realmino);
Sage Weilb8cd07e2010-07-16 12:00:02 -0700711 WARN_ON(!realm);
Sage Weila8599bd2009-10-06 11:31:12 -0700712 }
713 }
714
715 __check_cap_issue(ci, cap, issued);
716
717 /*
718 * If we are issued caps we don't want, or the mds' wanted
719 * value appears to be off, queue a check so we'll release
720 * later and/or update the mds wanted value.
721 */
722 actual_wanted = __ceph_caps_wanted(ci);
723 if ((wanted & ~actual_wanted) ||
724 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
725 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
726 ceph_cap_string(issued), ceph_cap_string(wanted),
727 ceph_cap_string(actual_wanted));
Xuehan Xu66802882018-10-11 17:55:39 +0800728 __cap_delay_requeue(mdsc, ci, true);
Sage Weila8599bd2009-10-06 11:31:12 -0700729 }
730
Yan, Zhengb8c2f3a2013-05-31 16:37:11 +0800731 if (flags & CEPH_CAP_FLAG_AUTH) {
Markus Elfringd37b1d92017-08-20 20:22:02 +0200732 if (!ci->i_auth_cap ||
Yan, Zhengd9ffc4f2014-03-18 10:15:29 +0800733 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
Yan, Zhengb8c2f3a2013-05-31 16:37:11 +0800734 ci->i_auth_cap = cap;
Yan, Zhengd9ffc4f2014-03-18 10:15:29 +0800735 cap->mds_wanted = wanted;
736 }
Yan, Zheng11df2df2013-11-24 14:44:38 +0800737 } else {
738 WARN_ON(ci->i_auth_cap == cap);
Yan, Zheng8a92a112013-01-04 14:28:07 +0800739 }
Sage Weila8599bd2009-10-06 11:31:12 -0700740
741 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
742 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
743 ceph_cap_string(issued|cap->issued), seq, mds);
744 cap->cap_id = cap_id;
745 cap->issued = issued;
746 cap->implemented |= issued;
Yan, Zhengd1b87802013-11-13 14:47:19 +0800747 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
Yan, Zheng964266c2013-02-27 09:26:09 +0800748 cap->mds_wanted = wanted;
749 else
750 cap->mds_wanted |= wanted;
Sage Weila8599bd2009-10-06 11:31:12 -0700751 cap->seq = seq;
752 cap->issue_seq = seq;
753 cap->mseq = mseq;
Sage Weil685f9a5d2009-11-09 12:05:48 -0800754 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700755
756 if (fmode >= 0)
757 __ceph_get_fmode(ci, fmode);
Sage Weila8599bd2009-10-06 11:31:12 -0700758}
759
760/*
761 * Return true if cap has not timed out and belongs to the current
762 * generation of the MDS session (i.e. has not gone 'stale' due to
763 * us losing touch with the mds).
764 */
765static int __cap_is_valid(struct ceph_cap *cap)
766{
767 unsigned long ttl;
Sage Weilcdac8302009-11-10 16:02:23 -0800768 u32 gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700769
Alex Elderd8fb02a2012-01-12 17:48:10 -0800770 spin_lock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700771 gen = cap->session->s_cap_gen;
772 ttl = cap->session->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800773 spin_unlock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700774
Sage Weil685f9a5d2009-11-09 12:05:48 -0800775 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
Sage Weila8599bd2009-10-06 11:31:12 -0700776 dout("__cap_is_valid %p cap %p issued %s "
777 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
Sage Weil685f9a5d2009-11-09 12:05:48 -0800778 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
Sage Weila8599bd2009-10-06 11:31:12 -0700779 return 0;
780 }
781
782 return 1;
783}
784
785/*
786 * Return set of valid cap bits issued to us. Note that caps time
787 * out, and may be invalidated in bulk if the client session times out
788 * and session->s_cap_gen is bumped.
789 */
790int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
791{
Yan, Zhengd9df2782014-04-18 09:57:11 +0800792 int have = ci->i_snap_caps;
Sage Weila8599bd2009-10-06 11:31:12 -0700793 struct ceph_cap *cap;
794 struct rb_node *p;
795
796 if (implemented)
797 *implemented = 0;
798 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
799 cap = rb_entry(p, struct ceph_cap, ci_node);
800 if (!__cap_is_valid(cap))
801 continue;
802 dout("__ceph_caps_issued %p cap %p issued %s\n",
803 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
804 have |= cap->issued;
805 if (implemented)
806 *implemented |= cap->implemented;
807 }
Yan, Zhengb1530f52013-07-02 12:40:20 +0800808 /*
809 * exclude caps issued by non-auth MDS, but are been revoking
810 * by the auth MDS. The non-auth MDS should be revoking/exporting
811 * these caps, but the message is delayed.
812 */
813 if (ci->i_auth_cap) {
814 cap = ci->i_auth_cap;
815 have &= ~cap->implemented | cap->issued;
816 }
Sage Weila8599bd2009-10-06 11:31:12 -0700817 return have;
818}
819
820/*
821 * Get cap bits issued by caps other than @ocap
822 */
823int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
824{
825 int have = ci->i_snap_caps;
826 struct ceph_cap *cap;
827 struct rb_node *p;
828
829 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
830 cap = rb_entry(p, struct ceph_cap, ci_node);
831 if (cap == ocap)
832 continue;
833 if (!__cap_is_valid(cap))
834 continue;
835 have |= cap->issued;
836 }
837 return have;
838}
839
840/*
841 * Move a cap to the end of the LRU (oldest caps at list head, newest
842 * at list tail).
843 */
844static void __touch_cap(struct ceph_cap *cap)
845{
846 struct ceph_mds_session *s = cap->session;
847
Sage Weila8599bd2009-10-06 11:31:12 -0700848 spin_lock(&s->s_cap_lock);
Markus Elfringd37b1d92017-08-20 20:22:02 +0200849 if (!s->s_cap_iterator) {
Sage Weil5dacf092009-12-21 20:40:34 -0800850 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
851 s->s_mds);
852 list_move_tail(&cap->session_caps, &s->s_caps);
853 } else {
854 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
855 &cap->ci->vfs_inode, cap, s->s_mds);
856 }
Sage Weila8599bd2009-10-06 11:31:12 -0700857 spin_unlock(&s->s_cap_lock);
858}
859
860/*
861 * Check if we hold the given mask. If so, move the cap(s) to the
862 * front of their respective LRUs. (This is the preferred way for
863 * callers to check for caps they want.)
864 */
865int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
866{
867 struct ceph_cap *cap;
868 struct rb_node *p;
869 int have = ci->i_snap_caps;
870
871 if ((have & mask) == mask) {
872 dout("__ceph_caps_issued_mask %p snap issued %s"
873 " (mask %s)\n", &ci->vfs_inode,
874 ceph_cap_string(have),
875 ceph_cap_string(mask));
876 return 1;
877 }
878
879 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
880 cap = rb_entry(p, struct ceph_cap, ci_node);
881 if (!__cap_is_valid(cap))
882 continue;
883 if ((cap->issued & mask) == mask) {
884 dout("__ceph_caps_issued_mask %p cap %p issued %s"
885 " (mask %s)\n", &ci->vfs_inode, cap,
886 ceph_cap_string(cap->issued),
887 ceph_cap_string(mask));
888 if (touch)
889 __touch_cap(cap);
890 return 1;
891 }
892
893 /* does a combination of caps satisfy mask? */
894 have |= cap->issued;
895 if ((have & mask) == mask) {
896 dout("__ceph_caps_issued_mask %p combo issued %s"
897 " (mask %s)\n", &ci->vfs_inode,
898 ceph_cap_string(cap->issued),
899 ceph_cap_string(mask));
900 if (touch) {
901 struct rb_node *q;
902
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300903 /* touch this + preceding caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700904 __touch_cap(cap);
905 for (q = rb_first(&ci->i_caps); q != p;
906 q = rb_next(q)) {
907 cap = rb_entry(q, struct ceph_cap,
908 ci_node);
909 if (!__cap_is_valid(cap))
910 continue;
911 __touch_cap(cap);
912 }
913 }
914 return 1;
915 }
916 }
917
918 return 0;
919}
920
921/*
922 * Return true if mask caps are currently being revoked by an MDS.
923 */
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800924int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
925 struct ceph_cap *ocap, int mask)
926{
927 struct ceph_cap *cap;
928 struct rb_node *p;
929
930 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
931 cap = rb_entry(p, struct ceph_cap, ci_node);
Yan, Zheng9563f882013-11-22 13:50:45 +0800932 if (cap != ocap &&
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800933 (cap->implemented & ~cap->issued & mask))
934 return 1;
935 }
936 return 0;
937}
938
Sage Weila8599bd2009-10-06 11:31:12 -0700939int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
940{
941 struct inode *inode = &ci->vfs_inode;
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800942 int ret;
Sage Weila8599bd2009-10-06 11:31:12 -0700943
Sage Weilbe655592011-11-30 09:47:09 -0800944 spin_lock(&ci->i_ceph_lock);
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800945 ret = __ceph_caps_revoking_other(ci, NULL, mask);
Sage Weilbe655592011-11-30 09:47:09 -0800946 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700947 dout("ceph_caps_revoking %p %s = %d\n", inode,
948 ceph_cap_string(mask), ret);
949 return ret;
950}
951
952int __ceph_caps_used(struct ceph_inode_info *ci)
953{
954 int used = 0;
955 if (ci->i_pin_ref)
956 used |= CEPH_CAP_PIN;
957 if (ci->i_rd_ref)
958 used |= CEPH_CAP_FILE_RD;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800959 if (ci->i_rdcache_ref ||
960 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
961 ci->vfs_inode.i_data.nrpages))
Sage Weila8599bd2009-10-06 11:31:12 -0700962 used |= CEPH_CAP_FILE_CACHE;
963 if (ci->i_wr_ref)
964 used |= CEPH_CAP_FILE_WR;
Henry C Changd3d07202011-05-11 10:29:54 +0000965 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
Sage Weila8599bd2009-10-06 11:31:12 -0700966 used |= CEPH_CAP_FILE_BUFFER;
967 return used;
968}
969
970/*
971 * wanted, by virtue of open file modes
972 */
973int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
974{
Yan, Zheng774a6a12016-06-06 16:01:39 +0800975 int i, bits = 0;
976 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
977 if (ci->i_nr_by_mode[i])
978 bits |= 1 << i;
979 }
980 if (bits == 0)
981 return 0;
982 return ceph_caps_for_mode(bits >> 1);
Sage Weila8599bd2009-10-06 11:31:12 -0700983}
984
985/*
986 * Return caps we have registered with the MDS(s) as 'wanted'.
987 */
Yan, Zhengc1944fe2017-01-29 22:15:47 +0800988int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
Sage Weila8599bd2009-10-06 11:31:12 -0700989{
990 struct ceph_cap *cap;
991 struct rb_node *p;
992 int mds_wanted = 0;
993
994 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
995 cap = rb_entry(p, struct ceph_cap, ci_node);
Yan, Zhengc1944fe2017-01-29 22:15:47 +0800996 if (check && !__cap_is_valid(cap))
Sage Weila8599bd2009-10-06 11:31:12 -0700997 continue;
Yan, Zhenga2550602014-03-08 09:51:45 +0800998 if (cap == ci->i_auth_cap)
999 mds_wanted |= cap->mds_wanted;
1000 else
1001 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
Sage Weila8599bd2009-10-06 11:31:12 -07001002 }
1003 return mds_wanted;
1004}
1005
1006/*
Sage Weilbe655592011-11-30 09:47:09 -08001007 * called under i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -07001008 */
Yan, Zheng0f439c72018-01-08 14:44:10 +08001009static int __ceph_is_single_caps(struct ceph_inode_info *ci)
1010{
1011 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
1012}
1013
Sage Weila8599bd2009-10-06 11:31:12 -07001014static int __ceph_is_any_caps(struct ceph_inode_info *ci)
1015{
Yan, Zhengd9df2782014-04-18 09:57:11 +08001016 return !RB_EMPTY_ROOT(&ci->i_caps);
Sage Weila8599bd2009-10-06 11:31:12 -07001017}
1018
Yan, Zheng9215aee2013-11-30 12:47:41 +08001019int ceph_is_any_caps(struct inode *inode)
1020{
1021 struct ceph_inode_info *ci = ceph_inode(inode);
1022 int ret;
1023
1024 spin_lock(&ci->i_ceph_lock);
1025 ret = __ceph_is_any_caps(ci);
1026 spin_unlock(&ci->i_ceph_lock);
1027
1028 return ret;
1029}
1030
Yan, Zhengdb40cc12015-03-23 20:12:20 +08001031static void drop_inode_snap_realm(struct ceph_inode_info *ci)
1032{
1033 struct ceph_snap_realm *realm = ci->i_snap_realm;
1034 spin_lock(&realm->inodes_with_caps_lock);
1035 list_del_init(&ci->i_snap_realm_item);
1036 ci->i_snap_realm_counter++;
1037 ci->i_snap_realm = NULL;
Yan, Zhengd95e6742019-01-10 15:41:09 +08001038 if (realm->ino == ci->i_vino.ino)
1039 realm->inode = NULL;
Yan, Zhengdb40cc12015-03-23 20:12:20 +08001040 spin_unlock(&realm->inodes_with_caps_lock);
1041 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
1042 realm);
1043}
1044
Sage Weila8599bd2009-10-06 11:31:12 -07001045/*
Sage Weilf818a732010-05-11 20:56:31 -07001046 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1047 *
Sage Weilbe655592011-11-30 09:47:09 -08001048 * caller should hold i_ceph_lock.
Sage Weila6369742010-02-22 13:59:00 -08001049 * caller will not hold session s_mutex if called from destroy_inode.
Sage Weila8599bd2009-10-06 11:31:12 -07001050 */
Yan, Zhenga096b092013-09-22 10:15:58 +08001051void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
Sage Weila8599bd2009-10-06 11:31:12 -07001052{
1053 struct ceph_mds_session *session = cap->session;
1054 struct ceph_inode_info *ci = cap->ci;
Cheng Renquan640ef792010-03-26 17:40:33 +08001055 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001056 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weilf818a732010-05-11 20:56:31 -07001057 int removed = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001058
1059 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
1060
Sage Weil7c1332b2010-02-16 11:39:45 -08001061 /* remove from session list */
1062 spin_lock(&session->s_cap_lock);
1063 if (session->s_cap_iterator == cap) {
1064 /* not yet, we are iterating over this very cap */
1065 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1066 cap, cap->session);
1067 } else {
1068 list_del_init(&cap->session_caps);
1069 session->s_nr_caps--;
1070 cap->session = NULL;
Sage Weilf818a732010-05-11 20:56:31 -07001071 removed = 1;
Sage Weil7c1332b2010-02-16 11:39:45 -08001072 }
Sage Weilf818a732010-05-11 20:56:31 -07001073 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1074 cap->ci = NULL;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001075
1076 /*
1077 * s_cap_reconnect is protected by s_cap_lock. no one changes
1078 * s_cap_gen while session is in the reconnect state.
1079 */
1080 if (queue_release &&
1081 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1082 cap->queue_release = 1;
1083 if (removed) {
1084 list_add_tail(&cap->session_caps,
1085 &session->s_cap_releases);
1086 session->s_num_cap_releases++;
1087 removed = 0;
1088 }
1089 } else {
1090 cap->queue_release = 0;
1091 }
1092 cap->cap_ino = ci->i_vino.ino;
1093
Sage Weil7c1332b2010-02-16 11:39:45 -08001094 spin_unlock(&session->s_cap_lock);
1095
Sage Weilf818a732010-05-11 20:56:31 -07001096 /* remove from inode list */
1097 rb_erase(&cap->ci_node, &ci->i_caps);
1098 if (ci->i_auth_cap == cap)
1099 ci->i_auth_cap = NULL;
1100
1101 if (removed)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001102 ceph_put_cap(mdsc, cap);
Sage Weila8599bd2009-10-06 11:31:12 -07001103
Yan, Zhengdb40cc12015-03-23 20:12:20 +08001104 /* when reconnect denied, we remove session caps forcibly,
1105 * i_wr_ref can be non-zero. If there are ongoing write,
1106 * keep i_snap_realm.
1107 */
1108 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1109 drop_inode_snap_realm(ci);
1110
Sage Weila8599bd2009-10-06 11:31:12 -07001111 if (!__ceph_is_any_real_caps(ci))
1112 __cap_delay_cancel(mdsc, ci);
1113}
1114
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001115struct cap_msg_args {
1116 struct ceph_mds_session *session;
1117 u64 ino, cid, follows;
1118 u64 flush_tid, oldest_flush_tid, size, max_size;
1119 u64 xattr_version;
1120 struct ceph_buffer *xattr_buf;
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02001121 struct timespec64 atime, mtime, ctime;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001122 int op, caps, wanted, dirty;
1123 u32 seq, issue_seq, mseq, time_warp_seq;
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001124 u32 flags;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001125 kuid_t uid;
1126 kgid_t gid;
1127 umode_t mode;
1128 bool inline_data;
1129};
1130
Sage Weila8599bd2009-10-06 11:31:12 -07001131/*
1132 * Build and send a cap message to the given MDS.
1133 *
1134 * Caller should be holding s_mutex.
1135 */
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001136static int send_cap_msg(struct cap_msg_args *arg)
Sage Weila8599bd2009-10-06 11:31:12 -07001137{
1138 struct ceph_mds_caps *fc;
1139 struct ceph_msg *msg;
Yan, Zhenge20d2582014-11-14 22:39:13 +08001140 void *p;
1141 size_t extra_len;
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02001142 struct timespec64 zerotime = {0};
Jeff Layton92475f02017-04-13 11:07:04 -04001143 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
Sage Weila8599bd2009-10-06 11:31:12 -07001144
1145 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
Yan, Zhenga2971c82015-06-10 11:09:32 +08001146 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001147 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1148 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1149 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1150 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1151 arg->mseq, arg->follows, arg->size, arg->max_size,
1152 arg->xattr_version,
1153 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001154
Yan, Zhenga2971c82015-06-10 11:09:32 +08001155 /* flock buffer size + inline version + inline data size +
1156 * osd_epoch_barrier + oldest_flush_tid */
Jeff Layton43b29672016-11-10 07:42:05 -05001157 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
Yan, Zhenge20d2582014-11-14 22:39:13 +08001158 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1159 GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07001160 if (!msg)
1161 return -ENOMEM;
Sage Weila8599bd2009-10-06 11:31:12 -07001162
Jeff Layton43b29672016-11-10 07:42:05 -05001163 msg->hdr.version = cpu_to_le16(10);
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001164 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07001165
Sage Weil6df058c2009-12-22 11:24:33 -08001166 fc = msg->front.iov_base;
Sage Weila8599bd2009-10-06 11:31:12 -07001167 memset(fc, 0, sizeof(*fc));
1168
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001169 fc->cap_id = cpu_to_le64(arg->cid);
1170 fc->op = cpu_to_le32(arg->op);
1171 fc->seq = cpu_to_le32(arg->seq);
1172 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1173 fc->migrate_seq = cpu_to_le32(arg->mseq);
1174 fc->caps = cpu_to_le32(arg->caps);
1175 fc->wanted = cpu_to_le32(arg->wanted);
1176 fc->dirty = cpu_to_le32(arg->dirty);
1177 fc->ino = cpu_to_le64(arg->ino);
1178 fc->snap_follows = cpu_to_le64(arg->follows);
Sage Weila8599bd2009-10-06 11:31:12 -07001179
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001180 fc->size = cpu_to_le64(arg->size);
1181 fc->max_size = cpu_to_le64(arg->max_size);
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02001182 ceph_encode_timespec64(&fc->mtime, &arg->mtime);
1183 ceph_encode_timespec64(&fc->atime, &arg->atime);
1184 ceph_encode_timespec64(&fc->ctime, &arg->ctime);
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001185 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07001186
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001187 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1188 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1189 fc->mode = cpu_to_le32(arg->mode);
Sage Weila8599bd2009-10-06 11:31:12 -07001190
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001191 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1192 if (arg->xattr_buf) {
1193 msg->middle = ceph_buffer_get(arg->xattr_buf);
1194 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1195 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
Jeff Layton96700792016-11-10 07:42:02 -05001196 }
1197
Yan, Zhenge20d2582014-11-14 22:39:13 +08001198 p = fc + 1;
Jeff Layton43b29672016-11-10 07:42:05 -05001199 /* flock buffer size (version 2) */
Yan, Zhenge20d2582014-11-14 22:39:13 +08001200 ceph_encode_32(&p, 0);
Jeff Layton43b29672016-11-10 07:42:05 -05001201 /* inline version (version 4) */
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001202 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
Yan, Zhenge20d2582014-11-14 22:39:13 +08001203 /* inline data size */
1204 ceph_encode_32(&p, 0);
Jeff Layton92475f02017-04-13 11:07:04 -04001205 /*
1206 * osd_epoch_barrier (version 5)
1207 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1208 * case it was recently changed
1209 */
1210 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
Jeff Layton43b29672016-11-10 07:42:05 -05001211 /* oldest_flush_tid (version 6) */
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001212 ceph_encode_64(&p, arg->oldest_flush_tid);
Yan, Zhenge20d2582014-11-14 22:39:13 +08001213
Jeff Layton43b29672016-11-10 07:42:05 -05001214 /*
1215 * caller_uid/caller_gid (version 7)
1216 *
1217 * Currently, we don't properly track which caller dirtied the caps
1218 * last, and force a flush of them when there is a conflict. For now,
1219 * just set this to 0:0, to emulate how the MDS has worked up to now.
1220 */
1221 ceph_encode_32(&p, 0);
1222 ceph_encode_32(&p, 0);
1223
1224 /* pool namespace (version 8) (mds always ignores this) */
1225 ceph_encode_32(&p, 0);
1226
1227 /*
1228 * btime and change_attr (version 9)
1229 *
1230 * We just zero these out for now, as the MDS ignores them unless
1231 * the requisite feature flags are set (which we don't do yet).
1232 */
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02001233 ceph_encode_timespec64(p, &zerotime);
Jeff Layton43b29672016-11-10 07:42:05 -05001234 p += sizeof(struct ceph_timespec);
1235 ceph_encode_64(&p, 0);
1236
1237 /* Advisory flags (version 10) */
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001238 ceph_encode_32(&p, arg->flags);
Jeff Layton43b29672016-11-10 07:42:05 -05001239
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001240 ceph_con_send(&arg->session->s_con, msg);
Sage Weila8599bd2009-10-06 11:31:12 -07001241 return 0;
1242}
1243
1244/*
Sage Weila6369742010-02-22 13:59:00 -08001245 * Queue cap releases when an inode is dropped from our cache. Since
Sage Weilbe655592011-11-30 09:47:09 -08001246 * inode is about to be destroyed, there is no need for i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001247 */
1248void ceph_queue_caps_release(struct inode *inode)
1249{
1250 struct ceph_inode_info *ci = ceph_inode(inode);
1251 struct rb_node *p;
1252
Sage Weila8599bd2009-10-06 11:31:12 -07001253 p = rb_first(&ci->i_caps);
1254 while (p) {
1255 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
Sage Weila8599bd2009-10-06 11:31:12 -07001256 p = rb_next(p);
Yan, Zhenga096b092013-09-22 10:15:58 +08001257 __ceph_remove_cap(cap, true);
Sage Weila8599bd2009-10-06 11:31:12 -07001258 }
Sage Weila8599bd2009-10-06 11:31:12 -07001259}
1260
1261/*
1262 * Send a cap msg on the given inode. Update our caps state, then
Sage Weilbe655592011-11-30 09:47:09 -08001263 * drop i_ceph_lock and send the message.
Sage Weila8599bd2009-10-06 11:31:12 -07001264 *
1265 * Make note of max_size reported/requested from mds, revoked caps
1266 * that have now been implemented.
1267 *
1268 * Make half-hearted attempt ot to invalidate page cache if we are
1269 * dropping RDCACHE. Note that this will leave behind locked pages
1270 * that we'll then need to deal with elsewhere.
1271 *
1272 * Return non-zero if delayed release, or we experienced an error
1273 * such that the caller should requeue + retry later.
1274 *
Sage Weilbe655592011-11-30 09:47:09 -08001275 * called with i_ceph_lock, then drops it.
Sage Weila8599bd2009-10-06 11:31:12 -07001276 * caller should hold snap_rwsem (read), s_mutex.
1277 */
1278static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001279 int op, bool sync, int used, int want, int retain,
1280 int flushing, u64 flush_tid, u64 oldest_flush_tid)
Sage Weilbe655592011-11-30 09:47:09 -08001281 __releases(cap->ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001282{
1283 struct ceph_inode_info *ci = cap->ci;
1284 struct inode *inode = &ci->vfs_inode;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001285 struct cap_msg_args arg;
Colin Ian Kingbb0581f2017-10-18 12:34:25 +01001286 int held, revoking;
Sage Weila8599bd2009-10-06 11:31:12 -07001287 int wake = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001288 int delayed = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001289 int ret;
1290
Sage Weil68c28322010-02-09 13:41:47 -08001291 held = cap->issued | cap->implemented;
1292 revoking = cap->implemented & ~cap->issued;
1293 retain &= ~revoking;
Sage Weil68c28322010-02-09 13:41:47 -08001294
Sage Weila8599bd2009-10-06 11:31:12 -07001295 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1296 inode, cap, cap->session,
1297 ceph_cap_string(held), ceph_cap_string(held & retain),
1298 ceph_cap_string(revoking));
1299 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1300
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001301 arg.session = cap->session;
Sage Weila8599bd2009-10-06 11:31:12 -07001302
1303 /* don't release wanted unless we've waited a bit. */
1304 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1305 time_before(jiffies, ci->i_hold_caps_min)) {
1306 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1307 ceph_cap_string(cap->issued),
1308 ceph_cap_string(cap->issued & retain),
1309 ceph_cap_string(cap->mds_wanted),
1310 ceph_cap_string(want));
1311 want |= cap->mds_wanted;
1312 retain |= cap->issued;
1313 delayed = 1;
1314 }
1315 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
Yan, Zhengeb65b912017-01-12 17:18:00 +08001316 if (want & ~cap->mds_wanted) {
1317 /* user space may open/close single file frequently.
1318 * This avoids droping mds_wanted immediately after
1319 * requesting new mds_wanted.
1320 */
1321 __cap_set_timeouts(mdsc, ci);
1322 }
Sage Weila8599bd2009-10-06 11:31:12 -07001323
1324 cap->issued &= retain; /* drop bits we don't want */
1325 if (cap->implemented & ~cap->issued) {
1326 /*
1327 * Wake up any waiters on wanted -> needed transition.
1328 * This is due to the weird transition from buffered
1329 * to sync IO... we need to flush dirty pages _before_
1330 * allowing sync writes to avoid reordering.
1331 */
1332 wake = 1;
1333 }
1334 cap->implemented &= cap->issued | used;
1335 cap->mds_wanted = want;
1336
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001337 arg.ino = ceph_vino(inode).ino;
1338 arg.cid = cap->cap_id;
1339 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1340 arg.flush_tid = flush_tid;
1341 arg.oldest_flush_tid = oldest_flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07001342
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001343 arg.size = inode->i_size;
1344 ci->i_reported_size = arg.size;
1345 arg.max_size = ci->i_wanted_max_size;
1346 ci->i_requested_max_size = arg.max_size;
Sage Weila8599bd2009-10-06 11:31:12 -07001347
Sage Weil082afec2010-08-22 15:16:41 -07001348 if (flushing & CEPH_CAP_XATTR_EXCL) {
Sage Weila8599bd2009-10-06 11:31:12 -07001349 __ceph_build_xattrs_blob(ci);
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001350 arg.xattr_version = ci->i_xattrs.version;
1351 arg.xattr_buf = ci->i_xattrs.blob;
1352 } else {
1353 arg.xattr_buf = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07001354 }
1355
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02001356 arg.mtime = inode->i_mtime;
1357 arg.atime = inode->i_atime;
1358 arg.ctime = inode->i_ctime;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001359
1360 arg.op = op;
1361 arg.caps = cap->implemented;
1362 arg.wanted = want;
1363 arg.dirty = flushing;
1364
1365 arg.seq = cap->seq;
1366 arg.issue_seq = cap->issue_seq;
1367 arg.mseq = cap->mseq;
1368 arg.time_warp_seq = ci->i_time_warp_seq;
1369
1370 arg.uid = inode->i_uid;
1371 arg.gid = inode->i_gid;
1372 arg.mode = inode->i_mode;
1373
1374 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
Yan, Zheng95569712017-07-24 17:59:39 +08001375 if (list_empty(&ci->i_cap_snaps))
1376 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1377 else
1378 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001379 if (sync)
1380 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
Yan, Zhenge20d2582014-11-14 22:39:13 +08001381
Sage Weilbe655592011-11-30 09:47:09 -08001382 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001383
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001384 ret = send_cap_msg(&arg);
Sage Weila8599bd2009-10-06 11:31:12 -07001385 if (ret < 0) {
1386 dout("error sending cap msg, must requeue %p\n", inode);
1387 delayed = 1;
1388 }
1389
1390 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001391 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07001392
1393 return delayed;
1394}
1395
Yan, Zheng0e294382016-07-04 18:06:41 +08001396static inline int __send_flush_snap(struct inode *inode,
1397 struct ceph_mds_session *session,
1398 struct ceph_cap_snap *capsnap,
1399 u32 mseq, u64 oldest_flush_tid)
1400{
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001401 struct cap_msg_args arg;
1402
1403 arg.session = session;
1404 arg.ino = ceph_vino(inode).ino;
1405 arg.cid = 0;
1406 arg.follows = capsnap->follows;
1407 arg.flush_tid = capsnap->cap_flush.tid;
1408 arg.oldest_flush_tid = oldest_flush_tid;
1409
1410 arg.size = capsnap->size;
1411 arg.max_size = 0;
1412 arg.xattr_version = capsnap->xattr_version;
1413 arg.xattr_buf = capsnap->xattr_blob;
1414
1415 arg.atime = capsnap->atime;
1416 arg.mtime = capsnap->mtime;
1417 arg.ctime = capsnap->ctime;
1418
1419 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1420 arg.caps = capsnap->issued;
1421 arg.wanted = 0;
1422 arg.dirty = capsnap->dirty;
1423
1424 arg.seq = 0;
1425 arg.issue_seq = 0;
1426 arg.mseq = mseq;
1427 arg.time_warp_seq = capsnap->time_warp_seq;
1428
1429 arg.uid = capsnap->uid;
1430 arg.gid = capsnap->gid;
1431 arg.mode = capsnap->mode;
1432
1433 arg.inline_data = capsnap->inline_data;
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001434 arg.flags = 0;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001435
1436 return send_cap_msg(&arg);
Yan, Zheng0e294382016-07-04 18:06:41 +08001437}
1438
Sage Weila8599bd2009-10-06 11:31:12 -07001439/*
1440 * When a snapshot is taken, clients accumulate dirty metadata on
1441 * inodes with capabilities in ceph_cap_snaps to describe the file
1442 * state at the time the snapshot was taken. This must be flushed
1443 * asynchronously back to the MDS once sync writes complete and dirty
1444 * data is written out.
1445 *
Sage Weilbe655592011-11-30 09:47:09 -08001446 * Called under i_ceph_lock. Takes s_mutex as needed.
Sage Weila8599bd2009-10-06 11:31:12 -07001447 */
Yan, Zhenged9b4302016-07-05 21:08:07 +08001448static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1449 struct ceph_mds_session *session)
Sage Weilbe655592011-11-30 09:47:09 -08001450 __releases(ci->i_ceph_lock)
1451 __acquires(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001452{
1453 struct inode *inode = &ci->vfs_inode;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001454 struct ceph_mds_client *mdsc = session->s_mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001455 struct ceph_cap_snap *capsnap;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001456 u64 oldest_flush_tid = 0;
1457 u64 first_tid = 1, last_tid = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001458
Yan, Zhenged9b4302016-07-05 21:08:07 +08001459 dout("__flush_snaps %p session %p\n", inode, session);
Sage Weila8599bd2009-10-06 11:31:12 -07001460
Sage Weila8599bd2009-10-06 11:31:12 -07001461 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
Sage Weila8599bd2009-10-06 11:31:12 -07001462 /*
1463 * we need to wait for sync writes to complete and for dirty
1464 * pages to be written out.
1465 */
1466 if (capsnap->dirty_pages || capsnap->writing)
Sage Weilcfc0bf62010-09-14 15:50:59 -07001467 break;
Sage Weila8599bd2009-10-06 11:31:12 -07001468
Yan, Zheng86056092015-05-01 16:57:16 +08001469 /* should be removed by ceph_try_drop_cap_snap() */
1470 BUG_ON(!capsnap->need_flush);
Sage Weil819ccbf2010-04-01 09:33:46 -07001471
Sage Weile8351242010-09-17 08:03:08 -07001472 /* only flush each capsnap once */
Yan, Zheng0e294382016-07-04 18:06:41 +08001473 if (capsnap->cap_flush.tid > 0) {
Yan, Zhenged9b4302016-07-05 21:08:07 +08001474 dout(" already flushed %p, skipping\n", capsnap);
Sage Weile8351242010-09-17 08:03:08 -07001475 continue;
1476 }
1477
Yan, Zheng553adfd2015-06-09 15:48:57 +08001478 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng0e294382016-07-04 18:06:41 +08001479 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1480 list_add_tail(&capsnap->cap_flush.g_list,
1481 &mdsc->cap_flush_list);
Yan, Zhenged9b4302016-07-05 21:08:07 +08001482 if (oldest_flush_tid == 0)
1483 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
Yan, Zheng0e294382016-07-04 18:06:41 +08001484 if (list_empty(&ci->i_flushing_item)) {
1485 list_add_tail(&ci->i_flushing_item,
1486 &session->s_cap_flushing);
1487 }
Yan, Zheng553adfd2015-06-09 15:48:57 +08001488 spin_unlock(&mdsc->cap_dirty_lock);
1489
Yan, Zheng0e294382016-07-04 18:06:41 +08001490 list_add_tail(&capsnap->cap_flush.i_list,
1491 &ci->i_cap_flush_list);
1492
Yan, Zhenged9b4302016-07-05 21:08:07 +08001493 if (first_tid == 1)
1494 first_tid = capsnap->cap_flush.tid;
1495 last_tid = capsnap->cap_flush.tid;
1496 }
1497
1498 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1499
1500 while (first_tid <= last_tid) {
1501 struct ceph_cap *cap = ci->i_auth_cap;
1502 struct ceph_cap_flush *cf;
1503 int ret;
1504
1505 if (!(cap && cap->session == session)) {
1506 dout("__flush_snaps %p auth cap %p not mds%d, "
1507 "stop\n", inode, cap, session->s_mds);
1508 break;
1509 }
1510
1511 ret = -ENOENT;
1512 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1513 if (cf->tid >= first_tid) {
1514 ret = 0;
1515 break;
1516 }
1517 }
1518 if (ret < 0)
1519 break;
1520
1521 first_tid = cf->tid + 1;
1522
1523 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
Elena Reshetova805692d2017-03-03 11:15:07 +02001524 refcount_inc(&capsnap->nref);
Sage Weilbe655592011-11-30 09:47:09 -08001525 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001526
Yan, Zhenged9b4302016-07-05 21:08:07 +08001527 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1528 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
Sage Weila8599bd2009-10-06 11:31:12 -07001529
Yan, Zhenged9b4302016-07-05 21:08:07 +08001530 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1531 oldest_flush_tid);
1532 if (ret < 0) {
1533 pr_err("__flush_snaps: error sending cap flushsnap, "
1534 "ino (%llx.%llx) tid %llu follows %llu\n",
1535 ceph_vinop(inode), cf->tid, capsnap->follows);
1536 }
1537
Sage Weila8599bd2009-10-06 11:31:12 -07001538 ceph_put_cap_snap(capsnap);
Sage Weilbe655592011-11-30 09:47:09 -08001539 spin_lock(&ci->i_ceph_lock);
Yan, Zhenged9b4302016-07-05 21:08:07 +08001540 }
1541}
1542
1543void ceph_flush_snaps(struct ceph_inode_info *ci,
1544 struct ceph_mds_session **psession)
1545{
1546 struct inode *inode = &ci->vfs_inode;
1547 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Yan, Zhenge4d2b162016-08-04 08:43:33 +08001548 struct ceph_mds_session *session = NULL;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001549 int mds;
Yan, Zhenge4d2b162016-08-04 08:43:33 +08001550
Yan, Zhenged9b4302016-07-05 21:08:07 +08001551 dout("ceph_flush_snaps %p\n", inode);
Yan, Zhenge4d2b162016-08-04 08:43:33 +08001552 if (psession)
1553 session = *psession;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001554retry:
1555 spin_lock(&ci->i_ceph_lock);
1556 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1557 dout(" no capsnap needs flush, doing nothing\n");
1558 goto out;
1559 }
1560 if (!ci->i_auth_cap) {
1561 dout(" no auth cap (migrating?), doing nothing\n");
1562 goto out;
1563 }
1564
1565 mds = ci->i_auth_cap->session->s_mds;
1566 if (session && session->s_mds != mds) {
1567 dout(" oops, wrong session %p mutex\n", session);
1568 mutex_unlock(&session->s_mutex);
1569 ceph_put_mds_session(session);
1570 session = NULL;
1571 }
1572 if (!session) {
1573 spin_unlock(&ci->i_ceph_lock);
1574 mutex_lock(&mdsc->mutex);
1575 session = __ceph_lookup_mds_session(mdsc, mds);
1576 mutex_unlock(&mdsc->mutex);
1577 if (session) {
1578 dout(" inverting session/ino locks on %p\n", session);
1579 mutex_lock(&session->s_mutex);
1580 }
Sage Weila8599bd2009-10-06 11:31:12 -07001581 goto retry;
1582 }
1583
Yan, Zheng24d063a2017-08-15 11:37:32 +08001584 // make sure flushsnap messages are sent in proper order.
1585 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1586 __kick_flushing_caps(mdsc, session, ci, 0);
1587 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1588 }
1589
Yan, Zhenged9b4302016-07-05 21:08:07 +08001590 __ceph_flush_snaps(ci, session);
1591out:
1592 spin_unlock(&ci->i_ceph_lock);
1593
1594 if (psession) {
1595 *psession = session;
Yan, Zhengc858a072017-08-28 15:02:42 +08001596 } else if (session) {
Yan, Zhenged9b4302016-07-05 21:08:07 +08001597 mutex_unlock(&session->s_mutex);
1598 ceph_put_mds_session(session);
1599 }
Sage Weila8599bd2009-10-06 11:31:12 -07001600 /* we flushed them all; remove this inode from the queue */
1601 spin_lock(&mdsc->snap_flush_lock);
1602 list_del_init(&ci->i_snap_flush_item);
1603 spin_unlock(&mdsc->snap_flush_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001604}
1605
1606/*
Sage Weilfca65b42011-05-04 11:33:47 -07001607 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1608 * Caller is then responsible for calling __mark_inode_dirty with the
1609 * returned flags value.
Sage Weil76e3b392009-10-15 18:13:53 -07001610 */
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001611int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1612 struct ceph_cap_flush **pcf)
Sage Weil76e3b392009-10-15 18:13:53 -07001613{
Cheng Renquan640ef792010-03-26 17:40:33 +08001614 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001615 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil76e3b392009-10-15 18:13:53 -07001616 struct inode *inode = &ci->vfs_inode;
1617 int was = ci->i_dirty_caps;
1618 int dirty = 0;
1619
Yan, Zheng571ade32015-03-24 11:36:08 +08001620 if (!ci->i_auth_cap) {
1621 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1622 "but no auth cap (session was closed?)\n",
1623 inode, ceph_ino(inode), ceph_cap_string(mask));
1624 return 0;
1625 }
1626
Sage Weil76e3b392009-10-15 18:13:53 -07001627 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1628 ceph_cap_string(mask), ceph_cap_string(was),
1629 ceph_cap_string(was | mask));
1630 ci->i_dirty_caps |= mask;
1631 if (was == 0) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001632 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1633 swap(ci->i_prealloc_cap_flush, *pcf);
1634
Yan, Zheng604d1b02015-05-01 17:49:16 +08001635 if (!ci->i_head_snapc) {
1636 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
Sage Weil7d8cb262010-08-24 08:44:16 -07001637 ci->i_head_snapc = ceph_get_snap_context(
1638 ci->i_snap_realm->cached_context);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001639 }
Yan, Zheng06852352012-11-19 10:49:07 +08001640 dout(" inode %p now dirty snapc %p auth cap %p\n",
1641 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
Sage Weil76e3b392009-10-15 18:13:53 -07001642 BUG_ON(!list_empty(&ci->i_dirty_item));
1643 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng11df2df2013-11-24 14:44:38 +08001644 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
Sage Weil76e3b392009-10-15 18:13:53 -07001645 spin_unlock(&mdsc->cap_dirty_lock);
1646 if (ci->i_flushing_caps == 0) {
Sage Weil3772d262011-05-03 09:28:08 -07001647 ihold(inode);
Sage Weil76e3b392009-10-15 18:13:53 -07001648 dirty |= I_DIRTY_SYNC;
1649 }
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001650 } else {
1651 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
Sage Weil76e3b392009-10-15 18:13:53 -07001652 }
1653 BUG_ON(list_empty(&ci->i_dirty_item));
1654 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1655 (mask & CEPH_CAP_FILE_BUFFER))
1656 dirty |= I_DIRTY_DATASYNC;
Xuehan Xu66802882018-10-11 17:55:39 +08001657 __cap_delay_requeue(mdsc, ci, true);
Sage Weilfca65b42011-05-04 11:33:47 -07001658 return dirty;
Sage Weil76e3b392009-10-15 18:13:53 -07001659}
1660
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001661struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1662{
1663 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1664}
1665
1666void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1667{
1668 if (cf)
1669 kmem_cache_free(ceph_cap_flush_cachep, cf);
1670}
1671
Yan, Zhenga2971c82015-06-10 11:09:32 +08001672static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1673{
Yan, Zhenge4500b52016-07-06 11:12:56 +08001674 if (!list_empty(&mdsc->cap_flush_list)) {
Yan, Zhenga2971c82015-06-10 11:09:32 +08001675 struct ceph_cap_flush *cf =
Yan, Zhenge4500b52016-07-06 11:12:56 +08001676 list_first_entry(&mdsc->cap_flush_list,
1677 struct ceph_cap_flush, g_list);
Yan, Zhenga2971c82015-06-10 11:09:32 +08001678 return cf->tid;
1679 }
1680 return 0;
1681}
1682
Sage Weil76e3b392009-10-15 18:13:53 -07001683/*
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001684 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1685 * Return true if caller needs to wake up flush waiters.
1686 */
1687static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1688 struct ceph_inode_info *ci,
1689 struct ceph_cap_flush *cf)
1690{
1691 struct ceph_cap_flush *prev;
1692 bool wake = cf->wake;
1693 if (mdsc) {
1694 /* are there older pending cap flushes? */
1695 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1696 prev = list_prev_entry(cf, g_list);
1697 prev->wake = true;
1698 wake = false;
1699 }
1700 list_del(&cf->g_list);
1701 } else if (ci) {
1702 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1703 prev = list_prev_entry(cf, i_list);
1704 prev->wake = true;
1705 wake = false;
1706 }
1707 list_del(&cf->i_list);
1708 } else {
1709 BUG_ON(1);
1710 }
1711 return wake;
1712}
1713
1714/*
Sage Weila8599bd2009-10-06 11:31:12 -07001715 * Add dirty inode to the flushing list. Assigned a seq number so we
1716 * can wait for caps to flush without starving.
Sage Weilcdc35f92009-10-14 14:24:19 -07001717 *
Sage Weilbe655592011-11-30 09:47:09 -08001718 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001719 */
Sage Weilcdc35f92009-10-14 14:24:19 -07001720static int __mark_caps_flushing(struct inode *inode,
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001721 struct ceph_mds_session *session, bool wake,
Yan, Zhenga2971c82015-06-10 11:09:32 +08001722 u64 *flush_tid, u64 *oldest_flush_tid)
Sage Weila8599bd2009-10-06 11:31:12 -07001723{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001724 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001725 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001726 struct ceph_cap_flush *cf = NULL;
Sage Weilcdc35f92009-10-14 14:24:19 -07001727 int flushing;
Sage Weil50b885b2009-12-01 14:12:07 -08001728
Sage Weilcdc35f92009-10-14 14:24:19 -07001729 BUG_ON(ci->i_dirty_caps == 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001730 BUG_ON(list_empty(&ci->i_dirty_item));
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001731 BUG_ON(!ci->i_prealloc_cap_flush);
Sage Weilcdc35f92009-10-14 14:24:19 -07001732
1733 flushing = ci->i_dirty_caps;
1734 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1735 ceph_cap_string(flushing),
1736 ceph_cap_string(ci->i_flushing_caps),
1737 ceph_cap_string(ci->i_flushing_caps | flushing));
1738 ci->i_flushing_caps |= flushing;
1739 ci->i_dirty_caps = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07001740 dout(" inode %p now !dirty\n", inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001741
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001742 swap(cf, ci->i_prealloc_cap_flush);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001743 cf->caps = flushing;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001744 cf->wake = wake;
Yan, Zheng553adfd2015-06-09 15:48:57 +08001745
Sage Weila8599bd2009-10-06 11:31:12 -07001746 spin_lock(&mdsc->cap_dirty_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07001747 list_del_init(&ci->i_dirty_item);
1748
Yan, Zheng553adfd2015-06-09 15:48:57 +08001749 cf->tid = ++mdsc->last_cap_flush_tid;
Yan, Zhenge4500b52016-07-06 11:12:56 +08001750 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
Yan, Zhenga2971c82015-06-10 11:09:32 +08001751 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001752
Sage Weila8599bd2009-10-06 11:31:12 -07001753 if (list_empty(&ci->i_flushing_item)) {
1754 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1755 mdsc->num_cap_flushing++;
Sage Weila8599bd2009-10-06 11:31:12 -07001756 }
1757 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weilcdc35f92009-10-14 14:24:19 -07001758
Yan, Zhenge4500b52016-07-06 11:12:56 +08001759 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001760
1761 *flush_tid = cf->tid;
Sage Weilcdc35f92009-10-14 14:24:19 -07001762 return flushing;
Sage Weila8599bd2009-10-06 11:31:12 -07001763}
1764
1765/*
Sage Weil5ecad6f2010-02-17 10:43:37 -08001766 * try to invalidate mapping pages without blocking.
1767 */
Sage Weil5ecad6f2010-02-17 10:43:37 -08001768static int try_nonblocking_invalidate(struct inode *inode)
1769{
1770 struct ceph_inode_info *ci = ceph_inode(inode);
1771 u32 invalidating_gen = ci->i_rdcache_gen;
1772
Sage Weilbe655592011-11-30 09:47:09 -08001773 spin_unlock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001774 invalidate_mapping_pages(&inode->i_data, 0, -1);
Sage Weilbe655592011-11-30 09:47:09 -08001775 spin_lock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001776
Sage Weil18a38192010-09-17 10:46:44 -07001777 if (inode->i_data.nrpages == 0 &&
Sage Weil5ecad6f2010-02-17 10:43:37 -08001778 invalidating_gen == ci->i_rdcache_gen) {
1779 /* success. */
1780 dout("try_nonblocking_invalidate %p success\n", inode);
Sage Weilcd045cb2010-11-04 11:05:05 -07001781 /* save any racing async invalidate some trouble */
1782 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
Sage Weil5ecad6f2010-02-17 10:43:37 -08001783 return 0;
1784 }
1785 dout("try_nonblocking_invalidate %p failed\n", inode);
1786 return -1;
1787}
1788
Yan, Zhengefb0ca72017-05-22 12:03:32 +08001789bool __ceph_should_report_size(struct ceph_inode_info *ci)
1790{
1791 loff_t size = ci->vfs_inode.i_size;
1792 /* mds will adjust max size according to the reported size */
1793 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1794 return false;
1795 if (size >= ci->i_max_size)
1796 return true;
1797 /* half of previous max_size increment has been used */
1798 if (ci->i_max_size > ci->i_reported_size &&
1799 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1800 return true;
1801 return false;
1802}
1803
Sage Weil5ecad6f2010-02-17 10:43:37 -08001804/*
Sage Weila8599bd2009-10-06 11:31:12 -07001805 * Swiss army knife function to examine currently used and wanted
1806 * versus held caps. Release, flush, ack revoked caps to mds as
1807 * appropriate.
1808 *
1809 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1810 * cap release further.
1811 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1812 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1813 * further delay.
1814 */
1815void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1816 struct ceph_mds_session *session)
1817{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001818 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1819 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001820 struct inode *inode = &ci->vfs_inode;
1821 struct ceph_cap *cap;
Yan, Zhenga2971c82015-06-10 11:09:32 +08001822 u64 flush_tid, oldest_flush_tid;
Yan, Zheng395c3122013-01-04 14:37:57 +08001823 int file_wanted, used, cap_used;
Sage Weila8599bd2009-10-06 11:31:12 -07001824 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
Sage Weilcbd03632010-02-09 13:41:18 -08001825 int issued, implemented, want, retain, revoking, flushing = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001826 int mds = -1; /* keep track of how far we've gone through i_caps list
1827 to avoid an infinite loop on retry */
1828 struct rb_node *p;
Yan, Zheng0f439c72018-01-08 14:44:10 +08001829 int delayed = 0, sent = 0;
1830 bool no_delay = flags & CHECK_CAPS_NODELAY;
Yan, Zheng36094042016-07-06 15:37:42 +08001831 bool queue_invalidate = false;
Yan, Zheng36094042016-07-06 15:37:42 +08001832 bool tried_invalidate = false;
Sage Weila8599bd2009-10-06 11:31:12 -07001833
1834 /* if we are unmounting, flush any unused caps immediately. */
1835 if (mdsc->stopping)
Yan, Zheng0f439c72018-01-08 14:44:10 +08001836 no_delay = true;
Sage Weila8599bd2009-10-06 11:31:12 -07001837
Sage Weilbe655592011-11-30 09:47:09 -08001838 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001839
1840 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1841 flags |= CHECK_CAPS_FLUSH;
1842
Yan, Zheng0f439c72018-01-08 14:44:10 +08001843 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1844 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1845 __cap_delay_cancel(mdsc, ci);
1846
Sage Weila8599bd2009-10-06 11:31:12 -07001847 goto retry_locked;
1848retry:
Sage Weilbe655592011-11-30 09:47:09 -08001849 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001850retry_locked:
1851 file_wanted = __ceph_caps_file_wanted(ci);
1852 used = __ceph_caps_used(ci);
Sage Weilcbd03632010-02-09 13:41:18 -08001853 issued = __ceph_caps_issued(ci, &implemented);
1854 revoking = implemented & ~issued;
Sage Weila8599bd2009-10-06 11:31:12 -07001855
Yan, Zheng41445992015-05-25 17:36:42 +08001856 want = file_wanted;
1857 retain = file_wanted | used | CEPH_CAP_PIN;
Sage Weila8599bd2009-10-06 11:31:12 -07001858 if (!mdsc->stopping && inode->i_nlink > 0) {
Yan, Zheng41445992015-05-25 17:36:42 +08001859 if (file_wanted) {
Sage Weila8599bd2009-10-06 11:31:12 -07001860 retain |= CEPH_CAP_ANY; /* be greedy */
Yan, Zheng32ec4392015-03-26 19:06:00 +08001861 } else if (S_ISDIR(inode->i_mode) &&
1862 (issued & CEPH_CAP_FILE_SHARED) &&
Yan, Zheng8a2ac3a2018-12-05 11:29:35 +08001863 __ceph_dir_is_complete(ci)) {
Yan, Zheng32ec4392015-03-26 19:06:00 +08001864 /*
1865 * If a directory is complete, we want to keep
1866 * the exclusive cap. So that MDS does not end up
1867 * revoking the shared cap on every create/unlink
1868 * operation.
1869 */
Yan, Zheng8a2ac3a2018-12-05 11:29:35 +08001870 if (IS_RDONLY(inode))
1871 want = CEPH_CAP_ANY_SHARED;
1872 else
1873 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
Yan, Zheng32ec4392015-03-26 19:06:00 +08001874 retain |= want;
Sage Weila8599bd2009-10-06 11:31:12 -07001875 } else {
Yan, Zheng32ec4392015-03-26 19:06:00 +08001876
Sage Weila8599bd2009-10-06 11:31:12 -07001877 retain |= CEPH_CAP_ANY_SHARED;
1878 /*
1879 * keep RD only if we didn't have the file open RW,
1880 * because then the mds would revoke it anyway to
1881 * journal max_size=0.
1882 */
1883 if (ci->i_max_size == 0)
1884 retain |= CEPH_CAP_ANY_RD;
1885 }
1886 }
1887
1888 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
Sage Weilcbd03632010-02-09 13:41:18 -08001889 " issued %s revoking %s retain %s %s%s%s\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001890 ceph_cap_string(file_wanted),
1891 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1892 ceph_cap_string(ci->i_flushing_caps),
Sage Weilcbd03632010-02-09 13:41:18 -08001893 ceph_cap_string(issued), ceph_cap_string(revoking),
Sage Weila8599bd2009-10-06 11:31:12 -07001894 ceph_cap_string(retain),
1895 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1896 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1897 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1898
1899 /*
1900 * If we no longer need to hold onto old our caps, and we may
1901 * have cached pages, but don't want them, then try to invalidate.
1902 * If we fail, it's because pages are locked.... try again later.
1903 */
Yan, Zheng0f439c72018-01-08 14:44:10 +08001904 if ((!no_delay || mdsc->stopping) &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001905 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
Yan, Zheng9abd4db2016-05-18 20:58:26 +08001906 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001907 inode->i_data.nrpages && /* have cached pages */
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001908 (revoking & (CEPH_CAP_FILE_CACHE|
1909 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
Sage Weila8599bd2009-10-06 11:31:12 -07001910 !tried_invalidate) {
Sage Weila8599bd2009-10-06 11:31:12 -07001911 dout("check_caps trying to invalidate on %p\n", inode);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001912 if (try_nonblocking_invalidate(inode) < 0) {
Yan, Zhengee612d92018-01-08 14:36:25 +08001913 dout("check_caps queuing invalidate\n");
1914 queue_invalidate = true;
1915 ci->i_rdcache_revoking = ci->i_rdcache_gen;
Sage Weila8599bd2009-10-06 11:31:12 -07001916 }
Yan, Zheng36094042016-07-06 15:37:42 +08001917 tried_invalidate = true;
Sage Weila8599bd2009-10-06 11:31:12 -07001918 goto retry_locked;
1919 }
1920
Sage Weila8599bd2009-10-06 11:31:12 -07001921 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1922 cap = rb_entry(p, struct ceph_cap, ci_node);
Sage Weila8599bd2009-10-06 11:31:12 -07001923
1924 /* avoid looping forever */
1925 if (mds >= cap->mds ||
1926 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1927 continue;
1928
1929 /* NOTE: no side-effects allowed, until we take s_mutex */
1930
Yan, Zheng395c3122013-01-04 14:37:57 +08001931 cap_used = used;
1932 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1933 cap_used &= ~ci->i_auth_cap->issued;
1934
Sage Weila8599bd2009-10-06 11:31:12 -07001935 revoking = cap->implemented & ~cap->issued;
Yan, Zheng395c3122013-01-04 14:37:57 +08001936 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
Yan, Zheng9abd4db2016-05-18 20:58:26 +08001937 cap->mds, cap, ceph_cap_string(cap_used),
1938 ceph_cap_string(cap->issued),
Sage Weil088b3f52011-01-18 08:56:01 -08001939 ceph_cap_string(cap->implemented),
1940 ceph_cap_string(revoking));
Sage Weila8599bd2009-10-06 11:31:12 -07001941
1942 if (cap == ci->i_auth_cap &&
1943 (cap->issued & CEPH_CAP_FILE_WR)) {
1944 /* request larger max_size from MDS? */
1945 if (ci->i_wanted_max_size > ci->i_max_size &&
1946 ci->i_wanted_max_size > ci->i_requested_max_size) {
1947 dout("requesting new max_size\n");
1948 goto ack;
1949 }
1950
1951 /* approaching file_max? */
Yan, Zhengefb0ca72017-05-22 12:03:32 +08001952 if (__ceph_should_report_size(ci)) {
Sage Weila8599bd2009-10-06 11:31:12 -07001953 dout("i_size approaching max_size\n");
1954 goto ack;
1955 }
1956 }
1957 /* flush anything dirty? */
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08001958 if (cap == ci->i_auth_cap) {
1959 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1960 dout("flushing dirty caps\n");
1961 goto ack;
1962 }
1963 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1964 dout("flushing snap caps\n");
1965 goto ack;
1966 }
Sage Weila8599bd2009-10-06 11:31:12 -07001967 }
1968
1969 /* completed revocation? going down and there are no caps? */
Yan, Zheng395c3122013-01-04 14:37:57 +08001970 if (revoking && (revoking & cap_used) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07001971 dout("completed revocation of %s\n",
1972 ceph_cap_string(cap->implemented & ~cap->issued));
1973 goto ack;
1974 }
1975
1976 /* want more caps from mds? */
1977 if (want & ~(cap->mds_wanted | cap->issued))
1978 goto ack;
1979
1980 /* things we might delay */
Yan, Zhengfdac94f2018-11-22 15:26:01 +08001981 if ((cap->issued & ~retain) == 0)
Sage Weila8599bd2009-10-06 11:31:12 -07001982 continue; /* nope, all good */
1983
Yan, Zheng0f439c72018-01-08 14:44:10 +08001984 if (no_delay)
Sage Weila8599bd2009-10-06 11:31:12 -07001985 goto ack;
1986
1987 /* delay? */
1988 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1989 time_before(jiffies, ci->i_hold_caps_max)) {
1990 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1991 ceph_cap_string(cap->issued),
1992 ceph_cap_string(cap->issued & retain),
1993 ceph_cap_string(cap->mds_wanted),
1994 ceph_cap_string(want));
1995 delayed++;
1996 continue;
1997 }
1998
1999ack:
Sage Weile9964c12010-03-01 15:16:56 -08002000 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
2001 dout(" skipping %p I_NOFLUSH set\n", inode);
2002 continue;
2003 }
2004
Sage Weila8599bd2009-10-06 11:31:12 -07002005 if (session && session != cap->session) {
2006 dout("oops, wrong session %p mutex\n", session);
2007 mutex_unlock(&session->s_mutex);
2008 session = NULL;
2009 }
2010 if (!session) {
2011 session = cap->session;
2012 if (mutex_trylock(&session->s_mutex) == 0) {
2013 dout("inverting session/ino locks on %p\n",
2014 session);
Sage Weilbe655592011-11-30 09:47:09 -08002015 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002016 if (took_snap_rwsem) {
2017 up_read(&mdsc->snap_rwsem);
2018 took_snap_rwsem = 0;
2019 }
2020 mutex_lock(&session->s_mutex);
2021 goto retry;
2022 }
2023 }
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08002024
2025 /* kick flushing and flush snaps before sending normal
2026 * cap message */
2027 if (cap == ci->i_auth_cap &&
2028 (ci->i_ceph_flags &
2029 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2030 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
Yan, Zheng24d063a2017-08-15 11:37:32 +08002031 __kick_flushing_caps(mdsc, session, ci, 0);
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08002032 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2033 }
Yan, Zhenged9b4302016-07-05 21:08:07 +08002034 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2035 __ceph_flush_snaps(ci, session);
2036
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08002037 goto retry_locked;
2038 }
2039
Sage Weila8599bd2009-10-06 11:31:12 -07002040 /* take snap_rwsem after session mutex */
2041 if (!took_snap_rwsem) {
2042 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
2043 dout("inverting snap/in locks on %p\n",
2044 inode);
Sage Weilbe655592011-11-30 09:47:09 -08002045 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002046 down_read(&mdsc->snap_rwsem);
2047 took_snap_rwsem = 1;
2048 goto retry;
2049 }
2050 took_snap_rwsem = 1;
2051 }
2052
Yan, Zheng553adfd2015-06-09 15:48:57 +08002053 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
Yan, Zhengc8799fc2016-07-07 15:22:38 +08002054 flushing = __mark_caps_flushing(inode, session, false,
Yan, Zhenga2971c82015-06-10 11:09:32 +08002055 &flush_tid,
2056 &oldest_flush_tid);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002057 } else {
Sage Weil24be0c42011-01-18 08:48:06 -08002058 flushing = 0;
Yan, Zheng553adfd2015-06-09 15:48:57 +08002059 flush_tid = 0;
Yan, Zhenga2971c82015-06-10 11:09:32 +08002060 spin_lock(&mdsc->cap_dirty_lock);
2061 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2062 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002063 }
Sage Weila8599bd2009-10-06 11:31:12 -07002064
2065 mds = cap->mds; /* remember mds, so we don't repeat */
2066 sent++;
2067
Sage Weilbe655592011-11-30 09:47:09 -08002068 /* __send_cap drops i_ceph_lock */
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05002069 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2070 cap_used, want, retain, flushing,
2071 flush_tid, oldest_flush_tid);
Sage Weilbe655592011-11-30 09:47:09 -08002072 goto retry; /* retake i_ceph_lock and restart our cap scan. */
Sage Weila8599bd2009-10-06 11:31:12 -07002073 }
2074
Yan, Zheng0f439c72018-01-08 14:44:10 +08002075 /* Reschedule delayed caps release if we delayed anything */
2076 if (delayed)
Xuehan Xu66802882018-10-11 17:55:39 +08002077 __cap_delay_requeue(mdsc, ci, false);
Sage Weila8599bd2009-10-06 11:31:12 -07002078
Sage Weilbe655592011-11-30 09:47:09 -08002079 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002080
Sage Weilcbd03632010-02-09 13:41:18 -08002081 if (queue_invalidate)
Sage Weil3c6f6b72010-02-09 15:24:44 -08002082 ceph_queue_invalidate(inode);
Sage Weilcbd03632010-02-09 13:41:18 -08002083
Sage Weilcdc2ce02010-03-16 13:39:28 -07002084 if (session)
Sage Weila8599bd2009-10-06 11:31:12 -07002085 mutex_unlock(&session->s_mutex);
2086 if (took_snap_rwsem)
2087 up_read(&mdsc->snap_rwsem);
2088}
2089
2090/*
Sage Weila8599bd2009-10-06 11:31:12 -07002091 * Try to flush dirty caps back to the auth mds.
2092 */
Yan, Zheng553adfd2015-06-09 15:48:57 +08002093static int try_flush_caps(struct inode *inode, u64 *ptid)
Sage Weila8599bd2009-10-06 11:31:12 -07002094{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002095 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002096 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng4fe59782013-10-31 16:44:14 +08002097 struct ceph_mds_session *session = NULL;
Yan, Zheng89b52fe2015-05-27 09:59:48 +08002098 int flushing = 0;
Yan, Zhenga2971c82015-06-10 11:09:32 +08002099 u64 flush_tid = 0, oldest_flush_tid = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002100
2101retry:
Sage Weilbe655592011-11-30 09:47:09 -08002102 spin_lock(&ci->i_ceph_lock);
Sage Weile9964c12010-03-01 15:16:56 -08002103 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
Jeff Layton6c2838f2017-10-19 08:52:58 -04002104 spin_unlock(&ci->i_ceph_lock);
Sage Weile9964c12010-03-01 15:16:56 -08002105 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2106 goto out;
2107 }
Sage Weila8599bd2009-10-06 11:31:12 -07002108 if (ci->i_dirty_caps && ci->i_auth_cap) {
2109 struct ceph_cap *cap = ci->i_auth_cap;
2110 int used = __ceph_caps_used(ci);
2111 int want = __ceph_caps_wanted(ci);
2112 int delayed;
2113
Yan, Zheng4fe59782013-10-31 16:44:14 +08002114 if (!session || session != cap->session) {
Sage Weilbe655592011-11-30 09:47:09 -08002115 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng4fe59782013-10-31 16:44:14 +08002116 if (session)
2117 mutex_unlock(&session->s_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07002118 session = cap->session;
2119 mutex_lock(&session->s_mutex);
2120 goto retry;
2121 }
Jeff Layton6c2838f2017-10-19 08:52:58 -04002122 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2123 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002124 goto out;
Jeff Layton6c2838f2017-10-19 08:52:58 -04002125 }
Sage Weila8599bd2009-10-06 11:31:12 -07002126
Yan, Zhengc8799fc2016-07-07 15:22:38 +08002127 flushing = __mark_caps_flushing(inode, session, true,
2128 &flush_tid, &oldest_flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002129
Sage Weilbe655592011-11-30 09:47:09 -08002130 /* __send_cap drops i_ceph_lock */
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05002131 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2132 used, want, (cap->issued | cap->implemented),
2133 flushing, flush_tid, oldest_flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002134
Yan, Zheng553adfd2015-06-09 15:48:57 +08002135 if (delayed) {
2136 spin_lock(&ci->i_ceph_lock);
Xuehan Xu66802882018-10-11 17:55:39 +08002137 __cap_delay_requeue(mdsc, ci, true);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002138 spin_unlock(&ci->i_ceph_lock);
2139 }
2140 } else {
Yan, Zhenge4500b52016-07-06 11:12:56 +08002141 if (!list_empty(&ci->i_cap_flush_list)) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08002142 struct ceph_cap_flush *cf =
Yan, Zhenge4500b52016-07-06 11:12:56 +08002143 list_last_entry(&ci->i_cap_flush_list,
Yan, Zhengc8799fc2016-07-07 15:22:38 +08002144 struct ceph_cap_flush, i_list);
2145 cf->wake = true;
Yan, Zheng553adfd2015-06-09 15:48:57 +08002146 flush_tid = cf->tid;
2147 }
2148 flushing = ci->i_flushing_caps;
2149 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002150 }
2151out:
Yan, Zheng4fe59782013-10-31 16:44:14 +08002152 if (session)
Sage Weila8599bd2009-10-06 11:31:12 -07002153 mutex_unlock(&session->s_mutex);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002154
2155 *ptid = flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002156 return flushing;
2157}
2158
2159/*
2160 * Return true if we've flushed caps through the given flush_tid.
2161 */
Yan, Zheng553adfd2015-06-09 15:48:57 +08002162static int caps_are_flushed(struct inode *inode, u64 flush_tid)
Sage Weila8599bd2009-10-06 11:31:12 -07002163{
2164 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002165 int ret = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002166
Sage Weilbe655592011-11-30 09:47:09 -08002167 spin_lock(&ci->i_ceph_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08002168 if (!list_empty(&ci->i_cap_flush_list)) {
2169 struct ceph_cap_flush * cf =
2170 list_first_entry(&ci->i_cap_flush_list,
2171 struct ceph_cap_flush, i_list);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002172 if (cf->tid <= flush_tid)
Sage Weila8599bd2009-10-06 11:31:12 -07002173 ret = 0;
Yan, Zheng89b52fe2015-05-27 09:59:48 +08002174 }
Sage Weilbe655592011-11-30 09:47:09 -08002175 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002176 return ret;
2177}
2178
2179/*
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002180 * wait for any unsafe requests to complete.
Yan, Zhengda819c82015-05-27 11:19:34 +08002181 */
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002182static int unsafe_request_wait(struct inode *inode)
Yan, Zhengda819c82015-05-27 11:19:34 +08002183{
2184 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002185 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2186 int ret, err = 0;
Yan, Zhengda819c82015-05-27 11:19:34 +08002187
2188 spin_lock(&ci->i_unsafe_lock);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002189 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2190 req1 = list_last_entry(&ci->i_unsafe_dirops,
2191 struct ceph_mds_request,
2192 r_unsafe_dir_item);
2193 ceph_mdsc_get_request(req1);
2194 }
2195 if (!list_empty(&ci->i_unsafe_iops)) {
2196 req2 = list_last_entry(&ci->i_unsafe_iops,
2197 struct ceph_mds_request,
2198 r_unsafe_target_item);
2199 ceph_mdsc_get_request(req2);
2200 }
Yan, Zhengda819c82015-05-27 11:19:34 +08002201 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002202
Jeff Layton4945a082016-11-16 09:45:22 -05002203 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002204 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2205 if (req1) {
2206 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2207 ceph_timeout_jiffies(req1->r_timeout));
2208 if (ret)
2209 err = -EIO;
2210 ceph_mdsc_put_request(req1);
2211 }
2212 if (req2) {
2213 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2214 ceph_timeout_jiffies(req2->r_timeout));
2215 if (ret)
2216 err = -EIO;
2217 ceph_mdsc_put_request(req2);
2218 }
2219 return err;
Yan, Zhengda819c82015-05-27 11:19:34 +08002220}
2221
Josef Bacik02c24a82011-07-16 20:44:56 -04002222int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Sage Weila8599bd2009-10-06 11:31:12 -07002223{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02002224 struct inode *inode = file->f_mapping->host;
Sage Weila8599bd2009-10-06 11:31:12 -07002225 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002226 u64 flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002227 int ret;
2228 int dirty;
2229
2230 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
Yan, Zheng9a5530c2016-06-15 16:29:18 +08002231
Jeff Laytonb74fcea2017-07-25 10:50:41 -04002232 ret = file_write_and_wait_range(file, start, end);
Sage Weila8599bd2009-10-06 11:31:12 -07002233 if (ret < 0)
Yan, Zhengda819c82015-05-27 11:19:34 +08002234 goto out;
2235
2236 if (datasync)
2237 goto out;
2238
Al Viro59551022016-01-22 15:40:57 -05002239 inode_lock(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002240
Yan, Zheng553adfd2015-06-09 15:48:57 +08002241 dirty = try_flush_caps(inode, &flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002242 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2243
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002244 ret = unsafe_request_wait(inode);
Yan, Zhengda819c82015-05-27 11:19:34 +08002245
Sage Weila8599bd2009-10-06 11:31:12 -07002246 /*
2247 * only wait on non-file metadata writeback (the mds
2248 * can recover size and mtime, so we don't need to
2249 * wait for that)
2250 */
Yan, Zhengda819c82015-05-27 11:19:34 +08002251 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
Sage Weila8599bd2009-10-06 11:31:12 -07002252 ret = wait_event_interruptible(ci->i_cap_wq,
Yan, Zhengda819c82015-05-27 11:19:34 +08002253 caps_are_flushed(inode, flush_tid));
Sage Weila8599bd2009-10-06 11:31:12 -07002254 }
Al Viro59551022016-01-22 15:40:57 -05002255 inode_unlock(inode);
Yan, Zhengda819c82015-05-27 11:19:34 +08002256out:
2257 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
Sage Weila8599bd2009-10-06 11:31:12 -07002258 return ret;
2259}
2260
2261/*
2262 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2263 * queue inode for flush but don't do so immediately, because we can
2264 * get by with fewer MDS messages if we wait for data writeback to
2265 * complete first.
2266 */
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11002267int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
Sage Weila8599bd2009-10-06 11:31:12 -07002268{
2269 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002270 u64 flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002271 int err = 0;
2272 int dirty;
Chengguang Xu16515a62018-01-30 10:02:30 +08002273 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
Sage Weila8599bd2009-10-06 11:31:12 -07002274
2275 dout("write_inode %p wait=%d\n", inode, wait);
2276 if (wait) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08002277 dirty = try_flush_caps(inode, &flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002278 if (dirty)
2279 err = wait_event_interruptible(ci->i_cap_wq,
2280 caps_are_flushed(inode, flush_tid));
2281 } else {
Cheng Renquan640ef792010-03-26 17:40:33 +08002282 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002283 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002284
Sage Weilbe655592011-11-30 09:47:09 -08002285 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002286 if (__ceph_caps_dirty(ci))
2287 __cap_delay_requeue_front(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08002288 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002289 }
2290 return err;
2291}
2292
Yan, Zheng0e294382016-07-04 18:06:41 +08002293static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2294 struct ceph_mds_session *session,
2295 struct ceph_inode_info *ci,
2296 u64 oldest_flush_tid)
2297 __releases(ci->i_ceph_lock)
2298 __acquires(ci->i_ceph_lock)
Yan, Zheng553adfd2015-06-09 15:48:57 +08002299{
2300 struct inode *inode = &ci->vfs_inode;
2301 struct ceph_cap *cap;
2302 struct ceph_cap_flush *cf;
Yan, Zheng0e294382016-07-04 18:06:41 +08002303 int ret;
Yan, Zheng553adfd2015-06-09 15:48:57 +08002304 u64 first_tid = 0;
Yan, Zhenga2971c82015-06-10 11:09:32 +08002305
Yan, Zhenge4500b52016-07-06 11:12:56 +08002306 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2307 if (cf->tid < first_tid)
2308 continue;
2309
Yan, Zheng553adfd2015-06-09 15:48:57 +08002310 cap = ci->i_auth_cap;
2311 if (!(cap && cap->session == session)) {
Yan, Zheng0e294382016-07-04 18:06:41 +08002312 pr_err("%p auth cap %p not mds%d ???\n",
2313 inode, cap, session->s_mds);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002314 break;
2315 }
2316
Yan, Zheng553adfd2015-06-09 15:48:57 +08002317 first_tid = cf->tid + 1;
2318
Yan, Zheng0e294382016-07-04 18:06:41 +08002319 if (cf->caps) {
2320 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2321 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2322 ci->i_ceph_flags |= CEPH_I_NODELAY;
2323 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05002324 false, __ceph_caps_used(ci),
Yan, Zheng0e294382016-07-04 18:06:41 +08002325 __ceph_caps_wanted(ci),
2326 cap->issued | cap->implemented,
2327 cf->caps, cf->tid, oldest_flush_tid);
2328 if (ret) {
2329 pr_err("kick_flushing_caps: error sending "
2330 "cap flush, ino (%llx.%llx) "
2331 "tid %llu flushing %s\n",
2332 ceph_vinop(inode), cf->tid,
2333 ceph_cap_string(cf->caps));
2334 }
2335 } else {
2336 struct ceph_cap_snap *capsnap =
2337 container_of(cf, struct ceph_cap_snap,
2338 cap_flush);
2339 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2340 inode, capsnap, cf->tid,
2341 ceph_cap_string(capsnap->dirty));
2342
Elena Reshetova805692d2017-03-03 11:15:07 +02002343 refcount_inc(&capsnap->nref);
Yan, Zheng0e294382016-07-04 18:06:41 +08002344 spin_unlock(&ci->i_ceph_lock);
2345
2346 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2347 oldest_flush_tid);
2348 if (ret < 0) {
2349 pr_err("kick_flushing_caps: error sending "
2350 "cap flushsnap, ino (%llx.%llx) "
2351 "tid %llu follows %llu\n",
2352 ceph_vinop(inode), cf->tid,
2353 capsnap->follows);
2354 }
2355
2356 ceph_put_cap_snap(capsnap);
2357 }
Yan, Zhenge4500b52016-07-06 11:12:56 +08002358
2359 spin_lock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002360 }
Yan, Zheng553adfd2015-06-09 15:48:57 +08002361}
2362
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002363void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2364 struct ceph_mds_session *session)
2365{
2366 struct ceph_inode_info *ci;
2367 struct ceph_cap *cap;
Yan, Zheng0e294382016-07-04 18:06:41 +08002368 u64 oldest_flush_tid;
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002369
2370 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
Yan, Zheng0e294382016-07-04 18:06:41 +08002371
2372 spin_lock(&mdsc->cap_dirty_lock);
2373 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2374 spin_unlock(&mdsc->cap_dirty_lock);
2375
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002376 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2377 spin_lock(&ci->i_ceph_lock);
2378 cap = ci->i_auth_cap;
2379 if (!(cap && cap->session == session)) {
2380 pr_err("%p auth cap %p not mds%d ???\n",
2381 &ci->vfs_inode, cap, session->s_mds);
2382 spin_unlock(&ci->i_ceph_lock);
2383 continue;
2384 }
2385
2386
2387 /*
2388 * if flushing caps were revoked, we re-send the cap flush
2389 * in client reconnect stage. This guarantees MDS * processes
2390 * the cap flush message before issuing the flushing caps to
2391 * other client.
2392 */
2393 if ((cap->issued & ci->i_flushing_caps) !=
2394 ci->i_flushing_caps) {
Yan, Zheng13c2b572016-07-05 16:45:21 +08002395 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
Yan, Zheng0e294382016-07-04 18:06:41 +08002396 __kick_flushing_caps(mdsc, session, ci,
2397 oldest_flush_tid);
Yan, Zheng13c2b572016-07-05 16:45:21 +08002398 } else {
2399 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002400 }
2401
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002402 spin_unlock(&ci->i_ceph_lock);
2403 }
2404}
2405
Sage Weila8599bd2009-10-06 11:31:12 -07002406void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2407 struct ceph_mds_session *session)
2408{
2409 struct ceph_inode_info *ci;
Yan, Zheng13c2b572016-07-05 16:45:21 +08002410 struct ceph_cap *cap;
Yan, Zheng0e294382016-07-04 18:06:41 +08002411 u64 oldest_flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002412
2413 dout("kick_flushing_caps mds%d\n", session->s_mds);
Yan, Zheng0e294382016-07-04 18:06:41 +08002414
2415 spin_lock(&mdsc->cap_dirty_lock);
2416 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2417 spin_unlock(&mdsc->cap_dirty_lock);
2418
Sage Weila8599bd2009-10-06 11:31:12 -07002419 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
Yan, Zheng0e294382016-07-04 18:06:41 +08002420 spin_lock(&ci->i_ceph_lock);
Yan, Zheng13c2b572016-07-05 16:45:21 +08002421 cap = ci->i_auth_cap;
2422 if (!(cap && cap->session == session)) {
2423 pr_err("%p auth cap %p not mds%d ???\n",
2424 &ci->vfs_inode, cap, session->s_mds);
2425 spin_unlock(&ci->i_ceph_lock);
2426 continue;
2427 }
2428 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2429 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2430 __kick_flushing_caps(mdsc, session, ci,
2431 oldest_flush_tid);
2432 }
Yan, Zheng0e294382016-07-04 18:06:41 +08002433 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002434 }
2435}
2436
Sage Weil088b3f52011-01-18 08:56:01 -08002437static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2438 struct ceph_mds_session *session,
2439 struct inode *inode)
Yan, Zheng0e294382016-07-04 18:06:41 +08002440 __releases(ci->i_ceph_lock)
Sage Weil088b3f52011-01-18 08:56:01 -08002441{
2442 struct ceph_inode_info *ci = ceph_inode(inode);
2443 struct ceph_cap *cap;
Sage Weil088b3f52011-01-18 08:56:01 -08002444
Sage Weil088b3f52011-01-18 08:56:01 -08002445 cap = ci->i_auth_cap;
Yan, Zheng8310b082015-06-09 17:20:12 +08002446 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2447 ceph_cap_string(ci->i_flushing_caps));
Yan, Zheng005c4692013-05-31 16:40:24 +08002448
Yan, Zheng0e294382016-07-04 18:06:41 +08002449 if (!list_empty(&ci->i_cap_flush_list)) {
2450 u64 oldest_flush_tid;
Yan, Zheng005c4692013-05-31 16:40:24 +08002451 spin_lock(&mdsc->cap_dirty_lock);
2452 list_move_tail(&ci->i_flushing_item,
2453 &cap->session->s_cap_flushing);
Yan, Zheng0e294382016-07-04 18:06:41 +08002454 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
Yan, Zheng005c4692013-05-31 16:40:24 +08002455 spin_unlock(&mdsc->cap_dirty_lock);
2456
Yan, Zheng13c2b572016-07-05 16:45:21 +08002457 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
Yan, Zheng0e294382016-07-04 18:06:41 +08002458 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002459 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08002460 } else {
Sage Weilbe655592011-11-30 09:47:09 -08002461 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08002462 }
2463}
2464
Sage Weila8599bd2009-10-06 11:31:12 -07002465
2466/*
2467 * Take references to capabilities we hold, so that we don't release
2468 * them to the MDS prematurely.
2469 *
Sage Weilbe655592011-11-30 09:47:09 -08002470 * Protected by i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07002471 */
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002472static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2473 bool snap_rwsem_locked)
Sage Weila8599bd2009-10-06 11:31:12 -07002474{
2475 if (got & CEPH_CAP_PIN)
2476 ci->i_pin_ref++;
2477 if (got & CEPH_CAP_FILE_RD)
2478 ci->i_rd_ref++;
2479 if (got & CEPH_CAP_FILE_CACHE)
2480 ci->i_rdcache_ref++;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002481 if (got & CEPH_CAP_FILE_WR) {
2482 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2483 BUG_ON(!snap_rwsem_locked);
2484 ci->i_head_snapc = ceph_get_snap_context(
2485 ci->i_snap_realm->cached_context);
2486 }
Sage Weila8599bd2009-10-06 11:31:12 -07002487 ci->i_wr_ref++;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002488 }
Sage Weila8599bd2009-10-06 11:31:12 -07002489 if (got & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002490 if (ci->i_wb_ref == 0)
Sage Weil3772d262011-05-03 09:28:08 -07002491 ihold(&ci->vfs_inode);
Henry C Changd3d07202011-05-11 10:29:54 +00002492 ci->i_wb_ref++;
2493 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2494 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002495 }
2496}
2497
2498/*
2499 * Try to grab cap references. Specify those refs we @want, and the
2500 * minimal set we @need. Also include the larger offset we are writing
2501 * to (when applicable), and check against max_size here as well.
2502 * Note that caller is responsible for ensuring max_size increases are
2503 * requested from the MDS.
2504 */
2505static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002506 loff_t endoff, bool nonblock, int *got, int *err)
Sage Weila8599bd2009-10-06 11:31:12 -07002507{
2508 struct inode *inode = &ci->vfs_inode;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002509 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002510 int ret = 0;
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002511 int have, implemented;
Sage Weil195d3ce2010-03-01 09:57:54 -08002512 int file_wanted;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002513 bool snap_rwsem_locked = false;
Sage Weila8599bd2009-10-06 11:31:12 -07002514
2515 dout("get_cap_refs %p need %s want %s\n", inode,
2516 ceph_cap_string(need), ceph_cap_string(want));
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002517
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002518again:
Sage Weilbe655592011-11-30 09:47:09 -08002519 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002520
Sage Weil195d3ce2010-03-01 09:57:54 -08002521 /* make sure file is actually open */
2522 file_wanted = __ceph_caps_file_wanted(ci);
Yan, Zheng77310322016-04-08 15:27:16 +08002523 if ((file_wanted & need) != need) {
Sage Weil195d3ce2010-03-01 09:57:54 -08002524 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2525 ceph_cap_string(need), ceph_cap_string(file_wanted));
Sage Weila8599bd2009-10-06 11:31:12 -07002526 *err = -EBADF;
2527 ret = 1;
Yan, Zheng3738daa2014-11-14 22:10:07 +08002528 goto out_unlock;
Sage Weila8599bd2009-10-06 11:31:12 -07002529 }
2530
Yan, Zheng37505d52013-04-12 16:11:10 +08002531 /* finish pending truncate */
2532 while (ci->i_truncate_pending) {
2533 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002534 if (snap_rwsem_locked) {
2535 up_read(&mdsc->snap_rwsem);
2536 snap_rwsem_locked = false;
2537 }
Yan, Zhengb415bf42013-07-02 12:40:19 +08002538 __ceph_do_pending_vmtruncate(inode);
Yan, Zheng37505d52013-04-12 16:11:10 +08002539 spin_lock(&ci->i_ceph_lock);
2540 }
2541
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002542 have = __ceph_caps_issued(ci, &implemented);
2543
2544 if (have & need & CEPH_CAP_FILE_WR) {
Sage Weila8599bd2009-10-06 11:31:12 -07002545 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2546 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2547 inode, endoff, ci->i_max_size);
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002548 if (endoff > ci->i_requested_max_size) {
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002549 *err = -EAGAIN;
Sage Weila8599bd2009-10-06 11:31:12 -07002550 ret = 1;
2551 }
Yan, Zheng3738daa2014-11-14 22:10:07 +08002552 goto out_unlock;
Sage Weila8599bd2009-10-06 11:31:12 -07002553 }
2554 /*
2555 * If a sync write is in progress, we must wait, so that we
2556 * can get a final snapshot value for size+mtime.
2557 */
2558 if (__ceph_have_pending_cap_snap(ci)) {
2559 dout("get_cap_refs %p cap_snap_pending\n", inode);
Yan, Zheng3738daa2014-11-14 22:10:07 +08002560 goto out_unlock;
Sage Weila8599bd2009-10-06 11:31:12 -07002561 }
2562 }
Sage Weila8599bd2009-10-06 11:31:12 -07002563
Sage Weila8599bd2009-10-06 11:31:12 -07002564 if ((have & need) == need) {
2565 /*
2566 * Look at (implemented & ~have & not) so that we keep waiting
2567 * on transition from wanted -> needed caps. This is needed
2568 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2569 * going before a prior buffered writeback happens.
2570 */
2571 int not = want & ~(have & need);
2572 int revoking = implemented & ~have;
2573 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2574 inode, ceph_cap_string(have), ceph_cap_string(not),
2575 ceph_cap_string(revoking));
2576 if ((revoking & not) == 0) {
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002577 if (!snap_rwsem_locked &&
2578 !ci->i_head_snapc &&
2579 (need & CEPH_CAP_FILE_WR)) {
2580 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2581 /*
2582 * we can not call down_read() when
2583 * task isn't in TASK_RUNNING state
2584 */
2585 if (nonblock) {
2586 *err = -EAGAIN;
2587 ret = 1;
2588 goto out_unlock;
2589 }
2590
2591 spin_unlock(&ci->i_ceph_lock);
2592 down_read(&mdsc->snap_rwsem);
2593 snap_rwsem_locked = true;
2594 goto again;
2595 }
2596 snap_rwsem_locked = true;
2597 }
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002598 *got = need | (have & want);
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +08002599 if ((need & CEPH_CAP_FILE_RD) &&
2600 !(*got & CEPH_CAP_FILE_CACHE))
2601 ceph_disable_fscache_readpage(ci);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002602 __take_cap_refs(ci, *got, true);
Sage Weila8599bd2009-10-06 11:31:12 -07002603 ret = 1;
2604 }
2605 } else {
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002606 int session_readonly = false;
2607 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2608 struct ceph_mds_session *s = ci->i_auth_cap->session;
2609 spin_lock(&s->s_cap_lock);
2610 session_readonly = s->s_readonly;
2611 spin_unlock(&s->s_cap_lock);
2612 }
2613 if (session_readonly) {
2614 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2615 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2616 *err = -EROFS;
2617 ret = 1;
2618 goto out_unlock;
2619 }
2620
Yan, Zheng77310322016-04-08 15:27:16 +08002621 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2622 int mds_wanted;
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002623 if (READ_ONCE(mdsc->fsc->mount_state) ==
Yan, Zheng77310322016-04-08 15:27:16 +08002624 CEPH_MOUNT_SHUTDOWN) {
2625 dout("get_cap_refs %p forced umount\n", inode);
2626 *err = -EIO;
2627 ret = 1;
2628 goto out_unlock;
2629 }
Yan, Zhengc1944fe2017-01-29 22:15:47 +08002630 mds_wanted = __ceph_caps_mds_wanted(ci, false);
Yan, Zhengeb65b912017-01-12 17:18:00 +08002631 if (need & ~(mds_wanted & need)) {
Yan, Zheng77310322016-04-08 15:27:16 +08002632 dout("get_cap_refs %p caps were dropped"
2633 " (session killed?)\n", inode);
2634 *err = -ESTALE;
2635 ret = 1;
2636 goto out_unlock;
2637 }
Yan, Zhengeb65b912017-01-12 17:18:00 +08002638 if (!(file_wanted & ~mds_wanted))
Yan, Zheng77310322016-04-08 15:27:16 +08002639 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002640 }
2641
Sage Weila8599bd2009-10-06 11:31:12 -07002642 dout("get_cap_refs %p have %s needed %s\n", inode,
2643 ceph_cap_string(have), ceph_cap_string(need));
2644 }
Yan, Zheng3738daa2014-11-14 22:10:07 +08002645out_unlock:
Sage Weilbe655592011-11-30 09:47:09 -08002646 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002647 if (snap_rwsem_locked)
2648 up_read(&mdsc->snap_rwsem);
Yan, Zheng3738daa2014-11-14 22:10:07 +08002649
Sage Weila8599bd2009-10-06 11:31:12 -07002650 dout("get_cap_refs %p ret %d got %s\n", inode,
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002651 ret, ceph_cap_string(*got));
Sage Weila8599bd2009-10-06 11:31:12 -07002652 return ret;
2653}
2654
2655/*
2656 * Check the offset we are writing up to against our current
2657 * max_size. If necessary, tell the MDS we want to write to
2658 * a larger offset.
2659 */
2660static void check_max_size(struct inode *inode, loff_t endoff)
2661{
2662 struct ceph_inode_info *ci = ceph_inode(inode);
2663 int check = 0;
2664
2665 /* do we need to explicitly request a larger max_size? */
Sage Weilbe655592011-11-30 09:47:09 -08002666 spin_lock(&ci->i_ceph_lock);
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002667 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
Sage Weila8599bd2009-10-06 11:31:12 -07002668 dout("write %p at large endoff %llu, req max_size\n",
2669 inode, endoff);
2670 ci->i_wanted_max_size = endoff;
Sage Weila8599bd2009-10-06 11:31:12 -07002671 }
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002672 /* duplicate ceph_check_caps()'s logic */
2673 if (ci->i_auth_cap &&
2674 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2675 ci->i_wanted_max_size > ci->i_max_size &&
2676 ci->i_wanted_max_size > ci->i_requested_max_size)
2677 check = 1;
Sage Weilbe655592011-11-30 09:47:09 -08002678 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002679 if (check)
2680 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2681}
2682
Luis Henriques2ee9dd92018-10-15 16:45:57 +01002683int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want,
2684 bool nonblock, int *got)
Yan, Zheng2b1ac852016-10-25 10:51:55 +08002685{
2686 int ret, err = 0;
2687
2688 BUG_ON(need & ~CEPH_CAP_FILE_RD);
Luis Henriques2ee9dd92018-10-15 16:45:57 +01002689 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO|CEPH_CAP_FILE_SHARED));
Yan, Zheng2b1ac852016-10-25 10:51:55 +08002690 ret = ceph_pool_perm_check(ci, need);
2691 if (ret < 0)
2692 return ret;
2693
Luis Henriques2ee9dd92018-10-15 16:45:57 +01002694 ret = try_get_cap_refs(ci, need, want, 0, nonblock, got, &err);
Yan, Zheng2b1ac852016-10-25 10:51:55 +08002695 if (ret) {
2696 if (err == -EAGAIN) {
2697 ret = 0;
2698 } else if (err < 0) {
2699 ret = err;
2700 }
2701 }
2702 return ret;
2703}
2704
Sage Weila8599bd2009-10-06 11:31:12 -07002705/*
2706 * Wait for caps, and take cap references. If we can't get a WR cap
2707 * due to a small max_size, make sure we check_max_size (and possibly
2708 * ask the mds) so we don't get hung up indefinitely.
2709 */
Yan, Zheng3738daa2014-11-14 22:10:07 +08002710int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2711 loff_t endoff, int *got, struct page **pinned_page)
Sage Weila8599bd2009-10-06 11:31:12 -07002712{
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002713 int _got, ret, err = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002714
Yan, Zheng10183a62015-04-27 15:33:28 +08002715 ret = ceph_pool_perm_check(ci, need);
2716 if (ret < 0)
2717 return ret;
2718
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002719 while (true) {
2720 if (endoff > 0)
2721 check_max_size(&ci->vfs_inode, endoff);
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002722
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002723 err = 0;
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002724 _got = 0;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002725 ret = try_get_cap_refs(ci, need, want, endoff,
2726 false, &_got, &err);
2727 if (ret) {
2728 if (err == -EAGAIN)
2729 continue;
2730 if (err < 0)
Yan, Zheng77310322016-04-08 15:27:16 +08002731 ret = err;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002732 } else {
Nikolay Borisov5c341ee32016-10-11 12:04:09 +03002733 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2734 add_wait_queue(&ci->i_cap_wq, &wait);
2735
2736 while (!try_get_cap_refs(ci, need, want, endoff,
Yan, Zheng6e09d0f2016-12-22 16:05:43 +08002737 true, &_got, &err)) {
2738 if (signal_pending(current)) {
2739 ret = -ERESTARTSYS;
2740 break;
2741 }
Nikolay Borisov5c341ee32016-10-11 12:04:09 +03002742 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Yan, Zheng6e09d0f2016-12-22 16:05:43 +08002743 }
Nikolay Borisov5c341ee32016-10-11 12:04:09 +03002744
2745 remove_wait_queue(&ci->i_cap_wq, &wait);
2746
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002747 if (err == -EAGAIN)
2748 continue;
2749 if (err < 0)
2750 ret = err;
Yan, Zheng77310322016-04-08 15:27:16 +08002751 }
2752 if (ret < 0) {
2753 if (err == -ESTALE) {
2754 /* session was killed, try renew caps */
2755 ret = ceph_renew_caps(&ci->vfs_inode);
2756 if (ret == 0)
2757 continue;
2758 }
2759 return ret;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002760 }
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002761
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002762 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2763 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2764 i_size_read(&ci->vfs_inode) > 0) {
2765 struct page *page =
2766 find_get_page(ci->vfs_inode.i_mapping, 0);
2767 if (page) {
2768 if (PageUptodate(page)) {
2769 *pinned_page = page;
2770 break;
2771 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002772 put_page(page);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002773 }
2774 /*
2775 * drop cap refs first because getattr while
2776 * holding * caps refs can cause deadlock.
2777 */
2778 ceph_put_cap_refs(ci, _got);
2779 _got = 0;
2780
2781 /*
2782 * getattr request will bring inline data into
2783 * page cache
2784 */
2785 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2786 CEPH_STAT_CAP_INLINE_DATA,
2787 true);
2788 if (ret < 0)
2789 return ret;
2790 continue;
2791 }
2792 break;
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002793 }
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002794
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +08002795 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2796 ceph_fscache_revalidate_cookie(ci);
2797
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002798 *got = _got;
2799 return 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002800}
2801
2802/*
2803 * Take cap refs. Caller must already know we hold at least one ref
2804 * on the caps in question or we don't know this is safe.
2805 */
2806void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2807{
Sage Weilbe655592011-11-30 09:47:09 -08002808 spin_lock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002809 __take_cap_refs(ci, caps, false);
Sage Weilbe655592011-11-30 09:47:09 -08002810 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002811}
2812
Yan, Zheng86056092015-05-01 16:57:16 +08002813
2814/*
2815 * drop cap_snap that is not associated with any snapshot.
2816 * we don't need to send FLUSHSNAP message for it.
2817 */
Yan, Zheng70220ac2016-07-06 16:21:30 +08002818static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2819 struct ceph_cap_snap *capsnap)
Yan, Zheng86056092015-05-01 16:57:16 +08002820{
2821 if (!capsnap->need_flush &&
2822 !capsnap->writing && !capsnap->dirty_pages) {
Yan, Zheng86056092015-05-01 16:57:16 +08002823 dout("dropping cap_snap %p follows %llu\n",
2824 capsnap, capsnap->follows);
Yan, Zheng0e294382016-07-04 18:06:41 +08002825 BUG_ON(capsnap->cap_flush.tid > 0);
Yan, Zheng86056092015-05-01 16:57:16 +08002826 ceph_put_snap_context(capsnap->context);
Yan, Zheng70220ac2016-07-06 16:21:30 +08002827 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2828 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2829
Yan, Zheng86056092015-05-01 16:57:16 +08002830 list_del(&capsnap->ci_item);
Yan, Zheng86056092015-05-01 16:57:16 +08002831 ceph_put_cap_snap(capsnap);
2832 return 1;
2833 }
2834 return 0;
2835}
2836
Sage Weila8599bd2009-10-06 11:31:12 -07002837/*
2838 * Release cap refs.
2839 *
2840 * If we released the last ref on any given cap, call ceph_check_caps
2841 * to release (or schedule a release).
2842 *
2843 * If we are releasing a WR cap (from a sync write), finalize any affected
2844 * cap_snap, and wake up any waiters.
2845 */
2846void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2847{
2848 struct inode *inode = &ci->vfs_inode;
2849 int last = 0, put = 0, flushsnaps = 0, wake = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002850
Sage Weilbe655592011-11-30 09:47:09 -08002851 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002852 if (had & CEPH_CAP_PIN)
2853 --ci->i_pin_ref;
2854 if (had & CEPH_CAP_FILE_RD)
2855 if (--ci->i_rd_ref == 0)
2856 last++;
2857 if (had & CEPH_CAP_FILE_CACHE)
2858 if (--ci->i_rdcache_ref == 0)
2859 last++;
2860 if (had & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002861 if (--ci->i_wb_ref == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07002862 last++;
2863 put++;
2864 }
Henry C Changd3d07202011-05-11 10:29:54 +00002865 dout("put_cap_refs %p wb %d -> %d (?)\n",
2866 inode, ci->i_wb_ref+1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002867 }
2868 if (had & CEPH_CAP_FILE_WR)
2869 if (--ci->i_wr_ref == 0) {
2870 last++;
Yan, Zheng86056092015-05-01 16:57:16 +08002871 if (__ceph_have_pending_cap_snap(ci)) {
2872 struct ceph_cap_snap *capsnap =
2873 list_last_entry(&ci->i_cap_snaps,
2874 struct ceph_cap_snap,
2875 ci_item);
2876 capsnap->writing = 0;
Yan, Zheng70220ac2016-07-06 16:21:30 +08002877 if (ceph_try_drop_cap_snap(ci, capsnap))
Yan, Zheng86056092015-05-01 16:57:16 +08002878 put++;
2879 else if (__ceph_finish_cap_snap(ci, capsnap))
2880 flushsnaps = 1;
2881 wake = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002882 }
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002883 if (ci->i_wrbuffer_ref_head == 0 &&
2884 ci->i_dirty_caps == 0 &&
2885 ci->i_flushing_caps == 0) {
2886 BUG_ON(!ci->i_head_snapc);
2887 ceph_put_snap_context(ci->i_head_snapc);
2888 ci->i_head_snapc = NULL;
2889 }
Yan, Zhengdb40cc12015-03-23 20:12:20 +08002890 /* see comment in __ceph_remove_cap() */
2891 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2892 drop_inode_snap_realm(ci);
Sage Weila8599bd2009-10-06 11:31:12 -07002893 }
Sage Weilbe655592011-11-30 09:47:09 -08002894 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002895
Sage Weil819ccbf2010-04-01 09:33:46 -07002896 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2897 last ? " last" : "", put ? " put" : "");
Sage Weila8599bd2009-10-06 11:31:12 -07002898
2899 if (last && !flushsnaps)
2900 ceph_check_caps(ci, 0, NULL);
2901 else if (flushsnaps)
Yan, Zhenged9b4302016-07-05 21:08:07 +08002902 ceph_flush_snaps(ci, NULL);
Sage Weila8599bd2009-10-06 11:31:12 -07002903 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002904 wake_up_all(&ci->i_cap_wq);
Yan, Zheng86056092015-05-01 16:57:16 +08002905 while (put-- > 0)
Sage Weila8599bd2009-10-06 11:31:12 -07002906 iput(inode);
2907}
2908
2909/*
2910 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2911 * context. Adjust per-snap dirty page accounting as appropriate.
2912 * Once all dirty data for a cap_snap is flushed, flush snapped file
2913 * metadata back to the MDS. If we dropped the last ref, call
2914 * ceph_check_caps.
2915 */
2916void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2917 struct ceph_snap_context *snapc)
2918{
2919 struct inode *inode = &ci->vfs_inode;
Sage Weila8599bd2009-10-06 11:31:12 -07002920 struct ceph_cap_snap *capsnap = NULL;
Yan, Zheng70220ac2016-07-06 16:21:30 +08002921 int put = 0;
2922 bool last = false;
2923 bool found = false;
2924 bool flush_snaps = false;
2925 bool complete_capsnap = false;
Sage Weila8599bd2009-10-06 11:31:12 -07002926
Sage Weilbe655592011-11-30 09:47:09 -08002927 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002928 ci->i_wrbuffer_ref -= nr;
Yan, Zheng70220ac2016-07-06 16:21:30 +08002929 if (ci->i_wrbuffer_ref == 0) {
2930 last = true;
2931 put++;
2932 }
Sage Weila8599bd2009-10-06 11:31:12 -07002933
2934 if (ci->i_head_snapc == snapc) {
2935 ci->i_wrbuffer_ref_head -= nr;
Sage Weil7d8cb262010-08-24 08:44:16 -07002936 if (ci->i_wrbuffer_ref_head == 0 &&
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002937 ci->i_wr_ref == 0 &&
2938 ci->i_dirty_caps == 0 &&
2939 ci->i_flushing_caps == 0) {
Sage Weil7d8cb262010-08-24 08:44:16 -07002940 BUG_ON(!ci->i_head_snapc);
Sage Weila8599bd2009-10-06 11:31:12 -07002941 ceph_put_snap_context(ci->i_head_snapc);
2942 ci->i_head_snapc = NULL;
2943 }
2944 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2945 inode,
2946 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2947 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2948 last ? " LAST" : "");
2949 } else {
2950 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2951 if (capsnap->context == snapc) {
Yan, Zheng70220ac2016-07-06 16:21:30 +08002952 found = true;
Sage Weila8599bd2009-10-06 11:31:12 -07002953 break;
2954 }
2955 }
2956 BUG_ON(!found);
Sage Weil819ccbf2010-04-01 09:33:46 -07002957 capsnap->dirty_pages -= nr;
2958 if (capsnap->dirty_pages == 0) {
Yan, Zheng70220ac2016-07-06 16:21:30 +08002959 complete_capsnap = true;
2960 if (!capsnap->writing) {
2961 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2962 put++;
2963 } else {
2964 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2965 flush_snaps = true;
2966 }
2967 }
Sage Weil819ccbf2010-04-01 09:33:46 -07002968 }
Sage Weila8599bd2009-10-06 11:31:12 -07002969 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
Yan, Zheng86056092015-05-01 16:57:16 +08002970 " snap %lld %d/%d -> %d/%d %s%s\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002971 inode, capsnap, capsnap->context->seq,
2972 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2973 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2974 last ? " (wrbuffer last)" : "",
Yan, Zheng86056092015-05-01 16:57:16 +08002975 complete_capsnap ? " (complete capsnap)" : "");
Sage Weila8599bd2009-10-06 11:31:12 -07002976 }
2977
Sage Weilbe655592011-11-30 09:47:09 -08002978 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002979
2980 if (last) {
2981 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
Yan, Zheng70220ac2016-07-06 16:21:30 +08002982 } else if (flush_snaps) {
Yan, Zhenged9b4302016-07-05 21:08:07 +08002983 ceph_flush_snaps(ci, NULL);
Sage Weila8599bd2009-10-06 11:31:12 -07002984 }
Yan, Zheng70220ac2016-07-06 16:21:30 +08002985 if (complete_capsnap)
2986 wake_up_all(&ci->i_cap_wq);
2987 while (put-- > 0)
Sage Weil819ccbf2010-04-01 09:33:46 -07002988 iput(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002989}
2990
2991/*
Yan, Zhengca20c992013-07-21 10:07:51 +08002992 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2993 */
2994static void invalidate_aliases(struct inode *inode)
2995{
2996 struct dentry *dn, *prev = NULL;
2997
2998 dout("invalidate_aliases inode %p\n", inode);
2999 d_prune_aliases(inode);
3000 /*
3001 * For non-directory inode, d_find_alias() only returns
J. Bruce Fieldsfc12c802014-01-16 17:42:53 -05003002 * hashed dentry. After calling d_invalidate(), the
3003 * dentry becomes unhashed.
Yan, Zhengca20c992013-07-21 10:07:51 +08003004 *
Yan, Zhenga8d436f2013-09-02 15:19:54 +08003005 * For directory inode, d_find_alias() can return
J. Bruce Fieldsfc12c802014-01-16 17:42:53 -05003006 * unhashed dentry. But directory inode should have
Yan, Zhengca20c992013-07-21 10:07:51 +08003007 * one alias at most.
3008 */
3009 while ((dn = d_find_alias(inode))) {
3010 if (dn == prev) {
3011 dput(dn);
3012 break;
3013 }
Yan, Zhenga8d436f2013-09-02 15:19:54 +08003014 d_invalidate(dn);
Yan, Zhengca20c992013-07-21 10:07:51 +08003015 if (prev)
3016 dput(prev);
3017 prev = dn;
3018 }
3019 if (prev)
3020 dput(prev);
3021}
3022
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003023struct cap_extra_info {
3024 struct ceph_string *pool_ns;
3025 /* inline data */
3026 u64 inline_version;
3027 void *inline_data;
3028 u32 inline_len;
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003029 /* dirstat */
3030 bool dirstat_valid;
3031 u64 nfiles;
3032 u64 nsubdirs;
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003033 /* currently issued */
3034 int issued;
3035};
3036
Yan, Zhengca20c992013-07-21 10:07:51 +08003037/*
Sage Weila8599bd2009-10-06 11:31:12 -07003038 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3039 * actually be a revocation if it specifies a smaller cap set.)
3040 *
Sage Weilbe655592011-11-30 09:47:09 -08003041 * caller holds s_mutex and i_ceph_lock, we drop both.
Sage Weila8599bd2009-10-06 11:31:12 -07003042 */
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003043static void handle_cap_grant(struct inode *inode,
Sage Weil15637c82010-03-16 13:42:00 -07003044 struct ceph_mds_session *session,
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003045 struct ceph_cap *cap,
3046 struct ceph_mds_caps *grant,
3047 struct ceph_buffer *xattr_buf,
3048 struct cap_extra_info *extra_info)
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003049 __releases(ci->i_ceph_lock)
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003050 __releases(session->s_mdsc->snap_rwsem)
Sage Weila8599bd2009-10-06 11:31:12 -07003051{
3052 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weil2f56f562010-10-27 20:59:49 -07003053 int seq = le32_to_cpu(grant->seq);
Sage Weila8599bd2009-10-06 11:31:12 -07003054 int newcaps = le32_to_cpu(grant->caps);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003055 int used, wanted, dirty;
Sage Weila8599bd2009-10-06 11:31:12 -07003056 u64 size = le64_to_cpu(grant->size);
3057 u64 max_size = le64_to_cpu(grant->max_size);
Yan, Zhengfdac94f2018-11-22 15:26:01 +08003058 unsigned char check_caps = 0;
3059 bool was_stale = cap->cap_gen < session->s_cap_gen;
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003060 bool wake = false;
3061 bool writeback = false;
3062 bool queue_trunc = false;
3063 bool queue_invalidate = false;
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003064 bool deleted_inode = false;
Yan, Zheng31c542a2014-11-14 21:41:55 +08003065 bool fill_inline = false;
Sage Weila8599bd2009-10-06 11:31:12 -07003066
Sage Weil2f56f562010-10-27 20:59:49 -07003067 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003068 inode, cap, session->s_mds, seq, ceph_cap_string(newcaps));
Sage Weila8599bd2009-10-06 11:31:12 -07003069 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
3070 inode->i_size);
3071
Yan, Zheng11df2df2013-11-24 14:44:38 +08003072
3073 /*
Sage Weila8599bd2009-10-06 11:31:12 -07003074 * If CACHE is being revoked, and we have no dirty buffers,
3075 * try to invalidate (once). (If there are dirty buffers, we
3076 * will invalidate _after_ writeback.)
3077 */
Yan, Zhengfdd4e152015-06-16 20:48:56 +08003078 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3079 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
Sage Weil3b454c42010-06-10 13:20:33 -07003080 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
Yan, Zheng9abd4db2016-05-18 20:58:26 +08003081 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
Li Wange9075742013-08-15 22:00:25 -07003082 if (try_nonblocking_invalidate(inode)) {
Sage Weila8599bd2009-10-06 11:31:12 -07003083 /* there were locked pages.. invalidate later
3084 in a separate thread. */
3085 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003086 queue_invalidate = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003087 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3088 }
Sage Weila8599bd2009-10-06 11:31:12 -07003089 }
Sage Weila8599bd2009-10-06 11:31:12 -07003090 }
3091
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003092 if (was_stale)
3093 cap->issued = cap->implemented = CEPH_CAP_PIN;
3094
3095 /*
3096 * auth mds of the inode changed. we received the cap export message,
3097 * but still haven't received the cap import message. handle_cap_export
3098 * updated the new auth MDS' cap.
3099 *
3100 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3101 * that was sent before the cap import message. So don't remove caps.
3102 */
3103 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3104 WARN_ON(cap != ci->i_auth_cap);
3105 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3106 seq = cap->seq;
3107 newcaps |= cap->issued;
3108 }
3109
Sage Weila8599bd2009-10-06 11:31:12 -07003110 /* side effects now are allowed */
Sage Weil685f9a5d2009-11-09 12:05:48 -08003111 cap->cap_gen = session->s_cap_gen;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003112 cap->seq = seq;
Sage Weila8599bd2009-10-06 11:31:12 -07003113
3114 __check_cap_issue(ci, cap, newcaps);
3115
Yan, Zhengf98a1282014-04-17 08:55:50 +08003116 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003117 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07003118 inode->i_mode = le32_to_cpu(grant->mode);
Eric W. Biederman05cb11c2013-01-31 02:56:19 -08003119 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3120 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
Sage Weila8599bd2009-10-06 11:31:12 -07003121 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08003122 from_kuid(&init_user_ns, inode->i_uid),
3123 from_kgid(&init_user_ns, inode->i_gid));
Sage Weila8599bd2009-10-06 11:31:12 -07003124 }
3125
Yan, Zhengfa466742018-05-25 11:22:56 +08003126 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003127 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02003128 set_nlink(inode, le32_to_cpu(grant->nlink));
Yan, Zhengca20c992013-07-21 10:07:51 +08003129 if (inode->i_nlink == 0 &&
3130 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003131 deleted_inode = true;
Yan, Zhengca20c992013-07-21 10:07:51 +08003132 }
Sage Weila8599bd2009-10-06 11:31:12 -07003133
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003134 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3135 grant->xattr_len) {
Sage Weila8599bd2009-10-06 11:31:12 -07003136 int len = le32_to_cpu(grant->xattr_len);
3137 u64 version = le64_to_cpu(grant->xattr_version);
3138
3139 if (version > ci->i_xattrs.version) {
3140 dout(" got new xattrs v%llu on %p len %d\n",
3141 version, inode, len);
3142 if (ci->i_xattrs.blob)
3143 ceph_buffer_put(ci->i_xattrs.blob);
3144 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3145 ci->i_xattrs.version = version;
Guangliang Zhao7221fe42013-11-11 15:18:03 +08003146 ceph_forget_all_cached_acls(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07003147 }
3148 }
3149
Yan, Zhengf98a1282014-04-17 08:55:50 +08003150 if (newcaps & CEPH_CAP_ANY_RD) {
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003151 struct timespec64 mtime, atime, ctime;
Yan, Zhengf98a1282014-04-17 08:55:50 +08003152 /* ctime/mtime/atime? */
Arnd Bergmann9bbeab42018-07-13 22:18:36 +02003153 ceph_decode_timespec64(&mtime, &grant->mtime);
3154 ceph_decode_timespec64(&atime, &grant->atime);
3155 ceph_decode_timespec64(&ctime, &grant->ctime);
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003156 ceph_fill_file_time(inode, extra_info->issued,
Yan, Zhengf98a1282014-04-17 08:55:50 +08003157 le32_to_cpu(grant->time_warp_seq),
3158 &ctime, &mtime, &atime);
3159 }
Sage Weila8599bd2009-10-06 11:31:12 -07003160
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003161 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3162 ci->i_files = extra_info->nfiles;
3163 ci->i_subdirs = extra_info->nsubdirs;
3164 }
3165
Yan, Zhengf98a1282014-04-17 08:55:50 +08003166 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3167 /* file layout may have changed */
Yan, Zheng76271512016-02-03 21:24:49 +08003168 s64 old_pool = ci->i_layout.pool_id;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003169 struct ceph_string *old_ns;
3170
Yan, Zheng76271512016-02-03 21:24:49 +08003171 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003172 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3173 lockdep_is_held(&ci->i_ceph_lock));
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003174 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003175
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003176 if (ci->i_layout.pool_id != old_pool ||
3177 extra_info->pool_ns != old_ns)
Yan, Zheng76271512016-02-03 21:24:49 +08003178 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003179
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003180 extra_info->pool_ns = old_ns;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003181
Yan, Zhengf98a1282014-04-17 08:55:50 +08003182 /* size/truncate_seq? */
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003183 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
Yan, Zhengf98a1282014-04-17 08:55:50 +08003184 le32_to_cpu(grant->truncate_seq),
3185 le64_to_cpu(grant->truncate_size),
3186 size);
Yan, Zheng84eea8c2017-05-16 08:55:34 +08003187 }
3188
3189 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3190 if (max_size != ci->i_max_size) {
Yan, Zhengf98a1282014-04-17 08:55:50 +08003191 dout("max_size %lld -> %llu\n",
3192 ci->i_max_size, max_size);
3193 ci->i_max_size = max_size;
3194 if (max_size >= ci->i_wanted_max_size) {
3195 ci->i_wanted_max_size = 0; /* reset */
3196 ci->i_requested_max_size = 0;
3197 }
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003198 wake = true;
Yan, Zheng84eea8c2017-05-16 08:55:34 +08003199 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3200 ci->i_wanted_max_size > ci->i_requested_max_size) {
3201 /* CEPH_CAP_OP_IMPORT */
3202 wake = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003203 }
Sage Weila8599bd2009-10-06 11:31:12 -07003204 }
3205
3206 /* check cap bits */
3207 wanted = __ceph_caps_wanted(ci);
3208 used = __ceph_caps_used(ci);
3209 dirty = __ceph_caps_dirty(ci);
3210 dout(" my wanted = %s, used = %s, dirty %s\n",
3211 ceph_cap_string(wanted),
3212 ceph_cap_string(used),
3213 ceph_cap_string(dirty));
Yan, Zhengfdac94f2018-11-22 15:26:01 +08003214
3215 if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
3216 (wanted & ~(cap->mds_wanted | newcaps))) {
3217 /*
3218 * If mds is importing cap, prior cap messages that update
3219 * 'wanted' may get dropped by mds (migrate seq mismatch).
3220 *
3221 * We don't send cap message to update 'wanted' if what we
3222 * want are already issued. If mds revokes caps, cap message
3223 * that releases caps also tells mds what we want. But if
3224 * caps got revoked by mds forcedly (session stale). We may
3225 * haven't told mds what we want.
3226 */
3227 check_caps = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07003228 }
3229
Sage Weila8599bd2009-10-06 11:31:12 -07003230 /* revocation, grant, or no-op? */
3231 if (cap->issued & ~newcaps) {
Sage Weil3b454c42010-06-10 13:20:33 -07003232 int revoking = cap->issued & ~newcaps;
3233
3234 dout("revocation: %s -> %s (revoking %s)\n",
3235 ceph_cap_string(cap->issued),
3236 ceph_cap_string(newcaps),
3237 ceph_cap_string(revoking));
Sage Weil0eb6cd42010-08-05 13:53:18 -07003238 if (revoking & used & CEPH_CAP_FILE_BUFFER)
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003239 writeback = true; /* initiate writeback; will delay ack */
Sage Weil3b454c42010-06-10 13:20:33 -07003240 else if (revoking == CEPH_CAP_FILE_CACHE &&
3241 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3242 queue_invalidate)
3243 ; /* do nothing yet, invalidation will be queued */
3244 else if (cap == ci->i_auth_cap)
3245 check_caps = 1; /* check auth cap only */
3246 else
3247 check_caps = 2; /* check all caps */
Sage Weila8599bd2009-10-06 11:31:12 -07003248 cap->issued = newcaps;
Sage Weil978097c2010-03-08 15:27:53 -08003249 cap->implemented |= newcaps;
Sage Weila8599bd2009-10-06 11:31:12 -07003250 } else if (cap->issued == newcaps) {
3251 dout("caps unchanged: %s -> %s\n",
3252 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3253 } else {
3254 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3255 ceph_cap_string(newcaps));
Yan, Zheng6ee6b9532013-07-02 12:40:21 +08003256 /* non-auth MDS is revoking the newly grant caps ? */
3257 if (cap == ci->i_auth_cap &&
3258 __ceph_caps_revoking_other(ci, cap, newcaps))
3259 check_caps = 2;
3260
Sage Weila8599bd2009-10-06 11:31:12 -07003261 cap->issued = newcaps;
3262 cap->implemented |= newcaps; /* add bits only, to
3263 * avoid stepping on a
3264 * pending revocation */
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003265 wake = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003266 }
Sage Weil978097c2010-03-08 15:27:53 -08003267 BUG_ON(cap->issued & ~cap->implemented);
Sage Weila8599bd2009-10-06 11:31:12 -07003268
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003269 if (extra_info->inline_version > 0 &&
3270 extra_info->inline_version >= ci->i_inline_version) {
3271 ci->i_inline_version = extra_info->inline_version;
Yan, Zheng31c542a2014-11-14 21:41:55 +08003272 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3273 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3274 fill_inline = true;
3275 }
3276
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003277 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003278 if (newcaps & ~extra_info->issued)
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003279 wake = true;
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003280 kick_flushing_inode_caps(session->s_mdsc, session, inode);
3281 up_read(&session->s_mdsc->snap_rwsem);
Yan, Zheng0e294382016-07-04 18:06:41 +08003282 } else {
3283 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003284 }
3285
Yan, Zheng31c542a2014-11-14 21:41:55 +08003286 if (fill_inline)
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003287 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3288 extra_info->inline_len);
Yan, Zheng31c542a2014-11-14 21:41:55 +08003289
Yan, Zheng14649752016-05-20 15:41:20 +08003290 if (queue_trunc)
Yan, Zhengc6bcda62014-04-11 10:18:07 +08003291 ceph_queue_vmtruncate(inode);
Yan, Zhengc6bcda62014-04-11 10:18:07 +08003292
Sage Weil3c6f6b72010-02-09 15:24:44 -08003293 if (writeback)
Sage Weila8599bd2009-10-06 11:31:12 -07003294 /*
3295 * queue inode for writeback: we can't actually call
3296 * filemap_write_and_wait, etc. from message handler
3297 * context.
3298 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08003299 ceph_queue_writeback(inode);
3300 if (queue_invalidate)
3301 ceph_queue_invalidate(inode);
Yan, Zhengca20c992013-07-21 10:07:51 +08003302 if (deleted_inode)
3303 invalidate_aliases(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07003304 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003305 wake_up_all(&ci->i_cap_wq);
Sage Weil15637c82010-03-16 13:42:00 -07003306
3307 if (check_caps == 1)
3308 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3309 session);
3310 else if (check_caps == 2)
3311 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3312 else
3313 mutex_unlock(&session->s_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07003314}
3315
3316/*
3317 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3318 * MDS has been safely committed.
3319 */
Sage Weil6df058c2009-12-22 11:24:33 -08003320static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07003321 struct ceph_mds_caps *m,
3322 struct ceph_mds_session *session,
3323 struct ceph_cap *cap)
Sage Weilbe655592011-11-30 09:47:09 -08003324 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07003325{
3326 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003327 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003328 struct ceph_cap_flush *cf, *tmp_cf;
Yan, Zheng553adfd2015-06-09 15:48:57 +08003329 LIST_HEAD(to_remove);
Sage Weila8599bd2009-10-06 11:31:12 -07003330 unsigned seq = le32_to_cpu(m->seq);
3331 int dirty = le32_to_cpu(m->dirty);
3332 int cleaned = 0;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003333 bool drop = false;
Thomas Meyer7271efa2017-10-07 16:02:21 +02003334 bool wake_ci = false;
3335 bool wake_mdsc = false;
Sage Weila8599bd2009-10-06 11:31:12 -07003336
Yan, Zhenge4500b52016-07-06 11:12:56 +08003337 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08003338 if (cf->tid == flush_tid)
3339 cleaned = cf->caps;
Yan, Zheng0e294382016-07-04 18:06:41 +08003340 if (cf->caps == 0) /* capsnap */
3341 continue;
Yan, Zheng553adfd2015-06-09 15:48:57 +08003342 if (cf->tid <= flush_tid) {
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003343 if (__finish_cap_flush(NULL, ci, cf))
3344 wake_ci = true;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003345 list_add_tail(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003346 } else {
3347 cleaned &= ~cf->caps;
3348 if (!cleaned)
3349 break;
3350 }
3351 }
Sage Weila8599bd2009-10-06 11:31:12 -07003352
3353 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3354 " flushing %s -> %s\n",
3355 inode, session->s_mds, seq, ceph_cap_string(dirty),
3356 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3357 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3358
Yan, Zheng8310b082015-06-09 17:20:12 +08003359 if (list_empty(&to_remove) && !cleaned)
Sage Weila8599bd2009-10-06 11:31:12 -07003360 goto out;
3361
Sage Weila8599bd2009-10-06 11:31:12 -07003362 ci->i_flushing_caps &= ~cleaned;
Sage Weila8599bd2009-10-06 11:31:12 -07003363
3364 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08003365
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003366 list_for_each_entry(cf, &to_remove, i_list) {
3367 if (__finish_cap_flush(mdsc, NULL, cf))
3368 wake_mdsc = true;
Yan, Zheng8310b082015-06-09 17:20:12 +08003369 }
3370
Sage Weila8599bd2009-10-06 11:31:12 -07003371 if (ci->i_flushing_caps == 0) {
Yan, Zheng0e294382016-07-04 18:06:41 +08003372 if (list_empty(&ci->i_cap_flush_list)) {
3373 list_del_init(&ci->i_flushing_item);
3374 if (!list_empty(&session->s_cap_flushing)) {
3375 dout(" mds%d still flushing cap on %p\n",
3376 session->s_mds,
3377 &list_first_entry(&session->s_cap_flushing,
3378 struct ceph_inode_info,
3379 i_flushing_item)->vfs_inode);
3380 }
3381 }
Sage Weila8599bd2009-10-06 11:31:12 -07003382 mdsc->num_cap_flushing--;
Sage Weila8599bd2009-10-06 11:31:12 -07003383 dout(" inode %p now !flushing\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07003384
3385 if (ci->i_dirty_caps == 0) {
3386 dout(" inode %p now clean\n", inode);
3387 BUG_ON(!list_empty(&ci->i_dirty_item));
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003388 drop = true;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08003389 if (ci->i_wr_ref == 0 &&
3390 ci->i_wrbuffer_ref_head == 0) {
Sage Weil7d8cb262010-08-24 08:44:16 -07003391 BUG_ON(!ci->i_head_snapc);
3392 ceph_put_snap_context(ci->i_head_snapc);
3393 ci->i_head_snapc = NULL;
3394 }
Sage Weil76e3b392009-10-15 18:13:53 -07003395 } else {
3396 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilafcdaea2009-10-14 14:27:38 -07003397 }
Sage Weila8599bd2009-10-06 11:31:12 -07003398 }
3399 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003400
3401out:
Sage Weilbe655592011-11-30 09:47:09 -08003402 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003403
3404 while (!list_empty(&to_remove)) {
3405 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08003406 struct ceph_cap_flush, i_list);
3407 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08003408 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003409 }
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003410
3411 if (wake_ci)
3412 wake_up_all(&ci->i_cap_wq);
3413 if (wake_mdsc)
3414 wake_up_all(&mdsc->cap_flushing_wq);
Sage Weilafcdaea2009-10-14 14:27:38 -07003415 if (drop)
Sage Weila8599bd2009-10-06 11:31:12 -07003416 iput(inode);
3417}
3418
3419/*
3420 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3421 * throw away our cap_snap.
3422 *
3423 * Caller hold s_mutex.
3424 */
Sage Weil6df058c2009-12-22 11:24:33 -08003425static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07003426 struct ceph_mds_caps *m,
3427 struct ceph_mds_session *session)
3428{
3429 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengaffbc192015-05-05 21:22:13 +08003430 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07003431 u64 follows = le64_to_cpu(m->snap_follows);
Sage Weila8599bd2009-10-06 11:31:12 -07003432 struct ceph_cap_snap *capsnap;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003433 bool flushed = false;
3434 bool wake_ci = false;
3435 bool wake_mdsc = false;
Sage Weila8599bd2009-10-06 11:31:12 -07003436
3437 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3438 inode, ci, session->s_mds, follows);
3439
Sage Weilbe655592011-11-30 09:47:09 -08003440 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003441 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3442 if (capsnap->follows == follows) {
Yan, Zheng0e294382016-07-04 18:06:41 +08003443 if (capsnap->cap_flush.tid != flush_tid) {
Sage Weila8599bd2009-10-06 11:31:12 -07003444 dout(" cap_snap %p follows %lld tid %lld !="
3445 " %lld\n", capsnap, follows,
Yan, Zheng0e294382016-07-04 18:06:41 +08003446 flush_tid, capsnap->cap_flush.tid);
Sage Weila8599bd2009-10-06 11:31:12 -07003447 break;
3448 }
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003449 flushed = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003450 break;
3451 } else {
3452 dout(" skipping cap_snap %p follows %lld\n",
3453 capsnap, capsnap->follows);
3454 }
3455 }
Yan, Zheng0e294382016-07-04 18:06:41 +08003456 if (flushed) {
Yan, Zheng0e294382016-07-04 18:06:41 +08003457 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3458 dout(" removing %p cap_snap %p follows %lld\n",
3459 inode, capsnap, follows);
3460 list_del(&capsnap->ci_item);
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003461 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3462 wake_ci = true;
Yan, Zheng0e294382016-07-04 18:06:41 +08003463
3464 spin_lock(&mdsc->cap_dirty_lock);
3465
3466 if (list_empty(&ci->i_cap_flush_list))
3467 list_del_init(&ci->i_flushing_item);
3468
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003469 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3470 wake_mdsc = true;
Yan, Zheng0e294382016-07-04 18:06:41 +08003471
3472 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng0e294382016-07-04 18:06:41 +08003473 }
Sage Weilbe655592011-11-30 09:47:09 -08003474 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng0e294382016-07-04 18:06:41 +08003475 if (flushed) {
3476 ceph_put_snap_context(capsnap->context);
3477 ceph_put_cap_snap(capsnap);
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003478 if (wake_ci)
3479 wake_up_all(&ci->i_cap_wq);
3480 if (wake_mdsc)
3481 wake_up_all(&mdsc->cap_flushing_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07003482 iput(inode);
Yan, Zheng0e294382016-07-04 18:06:41 +08003483 }
Sage Weila8599bd2009-10-06 11:31:12 -07003484}
3485
3486/*
3487 * Handle TRUNC from MDS, indicating file truncation.
3488 *
3489 * caller hold s_mutex.
3490 */
3491static void handle_cap_trunc(struct inode *inode,
3492 struct ceph_mds_caps *trunc,
3493 struct ceph_mds_session *session)
Sage Weilbe655592011-11-30 09:47:09 -08003494 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07003495{
3496 struct ceph_inode_info *ci = ceph_inode(inode);
3497 int mds = session->s_mds;
3498 int seq = le32_to_cpu(trunc->seq);
3499 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3500 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3501 u64 size = le64_to_cpu(trunc->size);
3502 int implemented = 0;
3503 int dirty = __ceph_caps_dirty(ci);
3504 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3505 int queue_trunc = 0;
3506
3507 issued |= implemented | dirty;
3508
3509 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3510 inode, mds, seq, truncate_size, truncate_seq);
3511 queue_trunc = ceph_fill_file_size(inode, issued,
3512 truncate_seq, truncate_size, size);
Sage Weilbe655592011-11-30 09:47:09 -08003513 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003514
Yan, Zheng14649752016-05-20 15:41:20 +08003515 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -08003516 ceph_queue_vmtruncate(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07003517}
3518
3519/*
3520 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3521 * different one. If we are the most recent migration we've seen (as
3522 * indicated by mseq), make note of the migrating cap bits for the
3523 * duration (until we see the corresponding IMPORT).
3524 *
3525 * caller holds s_mutex
3526 */
3527static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
Yan, Zheng11df2df2013-11-24 14:44:38 +08003528 struct ceph_mds_cap_peer *ph,
3529 struct ceph_mds_session *session)
Sage Weila8599bd2009-10-06 11:31:12 -07003530{
Sage Weildb354052011-05-24 11:46:31 -07003531 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003532 struct ceph_mds_session *tsession = NULL;
Yan, Zhengd9df2782014-04-18 09:57:11 +08003533 struct ceph_cap *cap, *tcap, *new_cap = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07003534 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003535 u64 t_cap_id;
Sage Weila8599bd2009-10-06 11:31:12 -07003536 unsigned mseq = le32_to_cpu(ex->migrate_seq);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003537 unsigned t_seq, t_mseq;
3538 int target, issued;
3539 int mds = session->s_mds;
Sage Weila8599bd2009-10-06 11:31:12 -07003540
Yan, Zheng11df2df2013-11-24 14:44:38 +08003541 if (ph) {
3542 t_cap_id = le64_to_cpu(ph->cap_id);
3543 t_seq = le32_to_cpu(ph->seq);
3544 t_mseq = le32_to_cpu(ph->mseq);
3545 target = le32_to_cpu(ph->mds);
3546 } else {
3547 t_cap_id = t_seq = t_mseq = 0;
3548 target = -1;
Sage Weila8599bd2009-10-06 11:31:12 -07003549 }
3550
Yan, Zheng11df2df2013-11-24 14:44:38 +08003551 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3552 inode, ci, mds, mseq, target);
3553retry:
3554 spin_lock(&ci->i_ceph_lock);
3555 cap = __get_cap_for_mds(ci, mds);
Yan, Zhengca665e02014-04-21 15:46:37 +08003556 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
Yan, Zheng11df2df2013-11-24 14:44:38 +08003557 goto out_unlock;
Sage Weil154f42c2010-06-21 13:45:04 -07003558
Yan, Zheng11df2df2013-11-24 14:44:38 +08003559 if (target < 0) {
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003560 if (cap->mds_wanted | cap->issued)
Yan, Zheng77310322016-04-08 15:27:16 +08003561 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
Yan, Zhengd2f8bb22018-12-10 16:35:09 +08003562 __ceph_remove_cap(cap, false);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003563 goto out_unlock;
3564 }
Sage Weildb354052011-05-24 11:46:31 -07003565
Yan, Zheng11df2df2013-11-24 14:44:38 +08003566 /*
3567 * now we know we haven't received the cap import message yet
3568 * because the exported cap still exist.
3569 */
3570
3571 issued = cap->issued;
Yan, Zhengd84b37f2018-01-03 11:16:27 +08003572 if (issued != cap->implemented)
3573 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3574 "ino (%llx.%llx) mds%d seq %d mseq %d "
3575 "issued %s implemented %s\n",
3576 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3577 ceph_cap_string(issued),
3578 ceph_cap_string(cap->implemented));
3579
Yan, Zheng11df2df2013-11-24 14:44:38 +08003580
3581 tcap = __get_cap_for_mds(ci, target);
3582 if (tcap) {
3583 /* already have caps from the target */
Yan, Zhengfa0aa3b2017-08-28 15:07:42 +08003584 if (tcap->cap_id == t_cap_id &&
Yan, Zheng11df2df2013-11-24 14:44:38 +08003585 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3586 dout(" updating import cap %p mds%d\n", tcap, target);
3587 tcap->cap_id = t_cap_id;
3588 tcap->seq = t_seq - 1;
3589 tcap->issue_seq = t_seq - 1;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003590 tcap->issued |= issued;
3591 tcap->implemented |= issued;
3592 if (cap == ci->i_auth_cap)
3593 ci->i_auth_cap = tcap;
Yan, Zheng00f06cb2017-01-24 10:02:32 +08003594
Yan, Zheng0e294382016-07-04 18:06:41 +08003595 if (!list_empty(&ci->i_cap_flush_list) &&
3596 ci->i_auth_cap == tcap) {
Yan, Zheng11df2df2013-11-24 14:44:38 +08003597 spin_lock(&mdsc->cap_dirty_lock);
3598 list_move_tail(&ci->i_flushing_item,
3599 &tcap->session->s_cap_flushing);
3600 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07003601 }
Sage Weila8599bd2009-10-06 11:31:12 -07003602 }
Yan, Zhenga096b092013-09-22 10:15:58 +08003603 __ceph_remove_cap(cap, false);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003604 goto out_unlock;
Yan, Zhengd9df2782014-04-18 09:57:11 +08003605 } else if (tsession) {
Yan, Zheng11df2df2013-11-24 14:44:38 +08003606 /* add placeholder for the export tagert */
Yan, Zhengd9df2782014-04-18 09:57:11 +08003607 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
Yan, Zheng00f06cb2017-01-24 10:02:32 +08003608 tcap = new_cap;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003609 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
Yan, Zhengd9df2782014-04-18 09:57:11 +08003610 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3611
Yan, Zheng00f06cb2017-01-24 10:02:32 +08003612 if (!list_empty(&ci->i_cap_flush_list) &&
3613 ci->i_auth_cap == tcap) {
3614 spin_lock(&mdsc->cap_dirty_lock);
3615 list_move_tail(&ci->i_flushing_item,
3616 &tcap->session->s_cap_flushing);
3617 spin_unlock(&mdsc->cap_dirty_lock);
3618 }
3619
Yan, Zhengd9df2782014-04-18 09:57:11 +08003620 __ceph_remove_cap(cap, false);
3621 goto out_unlock;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003622 }
Sage Weila8599bd2009-10-06 11:31:12 -07003623
Sage Weilbe655592011-11-30 09:47:09 -08003624 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003625 mutex_unlock(&session->s_mutex);
3626
3627 /* open target session */
3628 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3629 if (!IS_ERR(tsession)) {
3630 if (mds > target) {
3631 mutex_lock(&session->s_mutex);
3632 mutex_lock_nested(&tsession->s_mutex,
3633 SINGLE_DEPTH_NESTING);
3634 } else {
3635 mutex_lock(&tsession->s_mutex);
3636 mutex_lock_nested(&session->s_mutex,
3637 SINGLE_DEPTH_NESTING);
3638 }
Yan, Zhengd9df2782014-04-18 09:57:11 +08003639 new_cap = ceph_get_cap(mdsc, NULL);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003640 } else {
3641 WARN_ON(1);
3642 tsession = NULL;
3643 target = -1;
3644 }
3645 goto retry;
3646
3647out_unlock:
3648 spin_unlock(&ci->i_ceph_lock);
3649 mutex_unlock(&session->s_mutex);
3650 if (tsession) {
3651 mutex_unlock(&tsession->s_mutex);
3652 ceph_put_mds_session(tsession);
3653 }
Yan, Zhengd9df2782014-04-18 09:57:11 +08003654 if (new_cap)
3655 ceph_put_cap(mdsc, new_cap);
Sage Weila8599bd2009-10-06 11:31:12 -07003656}
3657
3658/*
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003659 * Handle cap IMPORT.
Sage Weila8599bd2009-10-06 11:31:12 -07003660 *
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003661 * caller holds s_mutex. acquires i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -07003662 */
3663static void handle_cap_import(struct ceph_mds_client *mdsc,
3664 struct inode *inode, struct ceph_mds_caps *im,
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003665 struct ceph_mds_cap_peer *ph,
Sage Weila8599bd2009-10-06 11:31:12 -07003666 struct ceph_mds_session *session,
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003667 struct ceph_cap **target_cap, int *old_issued)
3668 __acquires(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07003669{
3670 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003671 struct ceph_cap *cap, *ocap, *new_cap = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07003672 int mds = session->s_mds;
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003673 int issued;
3674 unsigned caps = le32_to_cpu(im->caps);
Sage Weila8599bd2009-10-06 11:31:12 -07003675 unsigned wanted = le32_to_cpu(im->wanted);
3676 unsigned seq = le32_to_cpu(im->seq);
3677 unsigned mseq = le32_to_cpu(im->migrate_seq);
3678 u64 realmino = le64_to_cpu(im->realm);
3679 u64 cap_id = le64_to_cpu(im->cap_id);
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003680 u64 p_cap_id;
3681 int peer;
Sage Weila8599bd2009-10-06 11:31:12 -07003682
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003683 if (ph) {
3684 p_cap_id = le64_to_cpu(ph->cap_id);
3685 peer = le32_to_cpu(ph->mds);
Sage Weila8599bd2009-10-06 11:31:12 -07003686 } else {
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003687 p_cap_id = 0;
3688 peer = -1;
Sage Weila8599bd2009-10-06 11:31:12 -07003689 }
3690
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003691 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3692 inode, ci, mds, mseq, peer);
3693
Yan, Zhengd9df2782014-04-18 09:57:11 +08003694retry:
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003695 spin_lock(&ci->i_ceph_lock);
Yan, Zhengd9df2782014-04-18 09:57:11 +08003696 cap = __get_cap_for_mds(ci, mds);
3697 if (!cap) {
3698 if (!new_cap) {
3699 spin_unlock(&ci->i_ceph_lock);
3700 new_cap = ceph_get_cap(mdsc, NULL);
3701 goto retry;
3702 }
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003703 cap = new_cap;
3704 } else {
3705 if (new_cap) {
3706 ceph_put_cap(mdsc, new_cap);
3707 new_cap = NULL;
3708 }
Yan, Zhengd9df2782014-04-18 09:57:11 +08003709 }
3710
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003711 __ceph_caps_issued(ci, &issued);
3712 issued |= __ceph_caps_dirty(ci);
3713
3714 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
Yan, Zhengd9df2782014-04-18 09:57:11 +08003715 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3716
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003717 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3718 if (ocap && ocap->cap_id == p_cap_id) {
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003719 dout(" remove export cap %p mds%d flags %d\n",
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003720 ocap, peer, ph->flags);
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003721 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003722 (ocap->seq != le32_to_cpu(ph->seq) ||
3723 ocap->mseq != le32_to_cpu(ph->mseq))) {
Yan, Zhengd84b37f2018-01-03 11:16:27 +08003724 pr_err_ratelimited("handle_cap_import: "
3725 "mismatched seq/mseq: ino (%llx.%llx) "
3726 "mds%d seq %d mseq %d importer mds%d "
3727 "has peer seq %d mseq %d\n",
3728 ceph_vinop(inode), peer, ocap->seq,
3729 ocap->mseq, mds, le32_to_cpu(ph->seq),
3730 le32_to_cpu(ph->mseq));
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003731 }
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003732 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003733 }
3734
3735 /* make sure we re-request max_size, if necessary */
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003736 ci->i_requested_max_size = 0;
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003737
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003738 *old_issued = issued;
3739 *target_cap = cap;
Sage Weila8599bd2009-10-06 11:31:12 -07003740}
3741
3742/*
3743 * Handle a caps message from the MDS.
3744 *
3745 * Identify the appropriate session, inode, and call the right handler
3746 * based on the cap op.
3747 */
3748void ceph_handle_caps(struct ceph_mds_session *session,
3749 struct ceph_msg *msg)
3750{
3751 struct ceph_mds_client *mdsc = session->s_mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07003752 struct inode *inode;
Sage Weilbe655592011-11-30 09:47:09 -08003753 struct ceph_inode_info *ci;
Sage Weila8599bd2009-10-06 11:31:12 -07003754 struct ceph_cap *cap;
3755 struct ceph_mds_caps *h;
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003756 struct ceph_mds_cap_peer *peer = NULL;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003757 struct ceph_snap_realm *realm = NULL;
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003758 int op;
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003759 int msg_version = le16_to_cpu(msg->hdr.version);
Sage Weil3d7ded42010-06-09 16:47:10 -07003760 u32 seq, mseq;
Sage Weila8599bd2009-10-06 11:31:12 -07003761 struct ceph_vino vino;
Sage Weil70edb552010-03-01 13:20:50 -08003762 void *snaptrace;
Sage Weilce1fbc82010-08-02 15:09:39 -07003763 size_t snaptrace_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003764 void *p, *end;
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003765 struct cap_extra_info extra_info = {};
Sage Weila8599bd2009-10-06 11:31:12 -07003766
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003767 dout("handle_caps from mds%d\n", session->s_mds);
Sage Weila8599bd2009-10-06 11:31:12 -07003768
3769 /* decode */
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003770 end = msg->front.iov_base + msg->front.iov_len;
Sage Weila8599bd2009-10-06 11:31:12 -07003771 if (msg->front.iov_len < sizeof(*h))
3772 goto bad;
3773 h = msg->front.iov_base;
3774 op = le32_to_cpu(h->op);
3775 vino.ino = le64_to_cpu(h->ino);
3776 vino.snap = CEPH_NOSNAP;
Sage Weila8599bd2009-10-06 11:31:12 -07003777 seq = le32_to_cpu(h->seq);
Sage Weil3d7ded42010-06-09 16:47:10 -07003778 mseq = le32_to_cpu(h->migrate_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07003779
Sage Weilce1fbc82010-08-02 15:09:39 -07003780 snaptrace = h + 1;
3781 snaptrace_len = le32_to_cpu(h->snap_trace_len);
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003782 p = snaptrace + snaptrace_len;
Sage Weilce1fbc82010-08-02 15:09:39 -07003783
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003784 if (msg_version >= 2) {
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003785 u32 flock_len;
Sage Weilce1fbc82010-08-02 15:09:39 -07003786 ceph_decode_32_safe(&p, end, flock_len, bad);
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003787 if (p + flock_len > end)
3788 goto bad;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003789 p += flock_len;
Sage Weilce1fbc82010-08-02 15:09:39 -07003790 }
3791
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003792 if (msg_version >= 3) {
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003793 if (op == CEPH_CAP_OP_IMPORT) {
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003794 if (p + sizeof(*peer) > end)
3795 goto bad;
3796 peer = p;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003797 p += sizeof(*peer);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003798 } else if (op == CEPH_CAP_OP_EXPORT) {
3799 /* recorded in unused fields */
3800 peer = (void *)&h->size;
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003801 }
3802 }
3803
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003804 if (msg_version >= 4) {
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003805 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
3806 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
3807 if (p + extra_info.inline_len > end)
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003808 goto bad;
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003809 extra_info.inline_data = p;
3810 p += extra_info.inline_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003811 }
3812
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003813 if (msg_version >= 5) {
Jeff Layton92475f02017-04-13 11:07:04 -04003814 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3815 u32 epoch_barrier;
3816
3817 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3818 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3819 }
3820
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003821 if (msg_version >= 8) {
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003822 u64 flush_tid;
3823 u32 caller_uid, caller_gid;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003824 u32 pool_ns_len;
Jeff Layton92475f02017-04-13 11:07:04 -04003825
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003826 /* version >= 6 */
3827 ceph_decode_64_safe(&p, end, flush_tid, bad);
3828 /* version >= 7 */
3829 ceph_decode_32_safe(&p, end, caller_uid, bad);
3830 ceph_decode_32_safe(&p, end, caller_gid, bad);
3831 /* version >= 8 */
3832 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003833 if (pool_ns_len > 0) {
3834 ceph_decode_need(&p, end, pool_ns_len, bad);
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003835 extra_info.pool_ns =
3836 ceph_find_or_create_string(p, pool_ns_len);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003837 p += pool_ns_len;
3838 }
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003839 }
3840
Yan, Zheng4985d6f2018-04-27 11:11:31 +08003841 if (msg_version >= 11) {
3842 struct ceph_timespec *btime;
3843 u64 change_attr;
3844 u32 flags;
3845
3846 /* version >= 9 */
3847 if (p + sizeof(*btime) > end)
3848 goto bad;
3849 btime = p;
3850 p += sizeof(*btime);
3851 ceph_decode_64_safe(&p, end, change_attr, bad);
3852 /* version >= 10 */
3853 ceph_decode_32_safe(&p, end, flags, bad);
3854 /* version >= 11 */
3855 extra_info.dirstat_valid = true;
3856 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
3857 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
3858 }
3859
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003860 /* lookup ino */
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003861 inode = ceph_find_inode(mdsc->fsc->sb, vino);
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003862 ci = ceph_inode(inode);
3863 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3864 vino.snap, inode);
3865
Sage Weila8599bd2009-10-06 11:31:12 -07003866 mutex_lock(&session->s_mutex);
3867 session->s_seq++;
3868 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3869 (unsigned)seq);
3870
Sage Weila8599bd2009-10-06 11:31:12 -07003871 if (!inode) {
3872 dout(" i don't have ino %llx\n", vino.ino);
Sage Weil3d7ded42010-06-09 16:47:10 -07003873
Yan, Zhenga096b092013-09-22 10:15:58 +08003874 if (op == CEPH_CAP_OP_IMPORT) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08003875 cap = ceph_get_cap(mdsc, NULL);
3876 cap->cap_ino = vino.ino;
3877 cap->queue_release = 1;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003878 cap->cap_id = le64_to_cpu(h->cap_id);
Yan, Zheng745a8e32015-05-14 17:22:42 +08003879 cap->mseq = mseq;
3880 cap->seq = seq;
Yan, Zhengdc24de82016-11-17 19:55:30 +08003881 cap->issue_seq = seq;
Yan, Zhenga096b092013-09-22 10:15:58 +08003882 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08003883 list_add_tail(&cap->session_caps,
3884 &session->s_cap_releases);
3885 session->s_num_cap_releases++;
Yan, Zhenga096b092013-09-22 10:15:58 +08003886 spin_unlock(&session->s_cap_lock);
3887 }
Greg Farnum21b559d2010-10-06 15:46:30 -07003888 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07003889 }
3890
3891 /* these will work even if we don't have a cap yet */
3892 switch (op) {
3893 case CEPH_CAP_OP_FLUSHSNAP_ACK:
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003894 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
3895 h, session);
Sage Weila8599bd2009-10-06 11:31:12 -07003896 goto done;
3897
3898 case CEPH_CAP_OP_EXPORT:
Yan, Zheng11df2df2013-11-24 14:44:38 +08003899 handle_cap_export(inode, h, peer, session);
3900 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07003901
3902 case CEPH_CAP_OP_IMPORT:
Yan, Zheng982d6012014-12-23 15:30:54 +08003903 realm = NULL;
3904 if (snaptrace_len) {
3905 down_write(&mdsc->snap_rwsem);
3906 ceph_update_snap_trace(mdsc, snaptrace,
3907 snaptrace + snaptrace_len,
3908 false, &realm);
3909 downgrade_write(&mdsc->snap_rwsem);
3910 } else {
3911 down_read(&mdsc->snap_rwsem);
3912 }
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003913 handle_cap_import(mdsc, inode, h, peer, session,
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003914 &cap, &extra_info.issued);
3915 handle_cap_grant(inode, session, cap,
3916 h, msg->middle, &extra_info);
Yan, Zheng982d6012014-12-23 15:30:54 +08003917 if (realm)
3918 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003919 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07003920 }
3921
3922 /* the rest require a cap */
Sage Weilbe655592011-11-30 09:47:09 -08003923 spin_lock(&ci->i_ceph_lock);
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003924 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
Sage Weila8599bd2009-10-06 11:31:12 -07003925 if (!cap) {
Sage Weil9dbd4122010-06-10 13:21:20 -07003926 dout(" no cap on %p ino %llx.%llx from mds%d\n",
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003927 inode, ceph_ino(inode), ceph_snap(inode),
3928 session->s_mds);
Sage Weilbe655592011-11-30 09:47:09 -08003929 spin_unlock(&ci->i_ceph_lock);
Greg Farnum21b559d2010-10-06 15:46:30 -07003930 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07003931 }
3932
Sage Weilbe655592011-11-30 09:47:09 -08003933 /* note that each of these drops i_ceph_lock for us */
Sage Weila8599bd2009-10-06 11:31:12 -07003934 switch (op) {
3935 case CEPH_CAP_OP_REVOKE:
3936 case CEPH_CAP_OP_GRANT:
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003937 __ceph_caps_issued(ci, &extra_info.issued);
3938 extra_info.issued |= __ceph_caps_dirty(ci);
3939 handle_cap_grant(inode, session, cap,
3940 h, msg->middle, &extra_info);
Sage Weil15637c82010-03-16 13:42:00 -07003941 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07003942
3943 case CEPH_CAP_OP_FLUSH_ACK:
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003944 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
3945 h, session, cap);
Sage Weila8599bd2009-10-06 11:31:12 -07003946 break;
3947
3948 case CEPH_CAP_OP_TRUNC:
3949 handle_cap_trunc(inode, h, session);
3950 break;
3951
3952 default:
Sage Weilbe655592011-11-30 09:47:09 -08003953 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003954 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3955 ceph_cap_op_name(op));
3956 }
3957
Greg Farnum21b559d2010-10-06 15:46:30 -07003958 goto done;
3959
3960flush_cap_releases:
3961 /*
Yan, Zheng745a8e32015-05-14 17:22:42 +08003962 * send any cap release message to try to move things
Greg Farnum21b559d2010-10-06 15:46:30 -07003963 * along for the mds (who clearly thinks we still have this
3964 * cap).
3965 */
Greg Farnum21b559d2010-10-06 15:46:30 -07003966 ceph_send_cap_releases(mdsc, session);
3967
Sage Weila8599bd2009-10-06 11:31:12 -07003968done:
Sage Weil15637c82010-03-16 13:42:00 -07003969 mutex_unlock(&session->s_mutex);
3970done_unlocked:
SF Markus Elfringe96a6502014-11-02 15:20:59 +01003971 iput(inode);
Yan, Zhenga1c6b832018-04-27 10:29:44 +08003972 ceph_put_string(extra_info.pool_ns);
Sage Weila8599bd2009-10-06 11:31:12 -07003973 return;
3974
3975bad:
3976 pr_err("ceph_handle_caps: corrupt message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003977 ceph_msg_dump(msg);
Sage Weila8599bd2009-10-06 11:31:12 -07003978 return;
3979}
3980
3981/*
3982 * Delayed work handler to process end of delayed cap release LRU list.
3983 */
Sage Weilafcdaea2009-10-14 14:27:38 -07003984void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -07003985{
Yan, Zheng4b9f2042017-06-27 17:17:24 +08003986 struct inode *inode;
Sage Weila8599bd2009-10-06 11:31:12 -07003987 struct ceph_inode_info *ci;
3988 int flags = CHECK_CAPS_NODELAY;
3989
Sage Weila8599bd2009-10-06 11:31:12 -07003990 dout("check_delayed_caps\n");
3991 while (1) {
3992 spin_lock(&mdsc->cap_delay_lock);
3993 if (list_empty(&mdsc->cap_delay_list))
3994 break;
3995 ci = list_first_entry(&mdsc->cap_delay_list,
3996 struct ceph_inode_info,
3997 i_cap_delay_list);
3998 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3999 time_before(jiffies, ci->i_hold_caps_max))
4000 break;
4001 list_del_init(&ci->i_cap_delay_list);
Yan, Zheng4b9f2042017-06-27 17:17:24 +08004002
4003 inode = igrab(&ci->vfs_inode);
Sage Weila8599bd2009-10-06 11:31:12 -07004004 spin_unlock(&mdsc->cap_delay_lock);
Yan, Zheng4b9f2042017-06-27 17:17:24 +08004005
4006 if (inode) {
4007 dout("check_delayed_caps on %p\n", inode);
4008 ceph_check_caps(ci, flags, NULL);
4009 iput(inode);
4010 }
Sage Weila8599bd2009-10-06 11:31:12 -07004011 }
4012 spin_unlock(&mdsc->cap_delay_lock);
4013}
4014
4015/*
Sage Weilafcdaea2009-10-14 14:27:38 -07004016 * Flush all dirty caps to the mds
4017 */
4018void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4019{
Sage Weildb354052011-05-24 11:46:31 -07004020 struct ceph_inode_info *ci;
4021 struct inode *inode;
Sage Weilafcdaea2009-10-14 14:27:38 -07004022
4023 dout("flush_dirty_caps\n");
4024 spin_lock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07004025 while (!list_empty(&mdsc->cap_dirty)) {
4026 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
4027 i_dirty_item);
Sage Weil70b666c2011-05-27 09:24:26 -07004028 inode = &ci->vfs_inode;
4029 ihold(inode);
Sage Weildb354052011-05-24 11:46:31 -07004030 dout("flush_dirty_caps %p\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07004031 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weil70b666c2011-05-27 09:24:26 -07004032 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
4033 iput(inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07004034 spin_lock(&mdsc->cap_dirty_lock);
4035 }
4036 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07004037 dout("flush_dirty_caps done\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07004038}
4039
Yan, Zheng774a6a12016-06-06 16:01:39 +08004040void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
4041{
4042 int i;
4043 int bits = (fmode << 1) | 1;
4044 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4045 if (bits & (1 << i))
4046 ci->i_nr_by_mode[i]++;
4047 }
4048}
4049
Sage Weilafcdaea2009-10-14 14:27:38 -07004050/*
Sage Weila8599bd2009-10-06 11:31:12 -07004051 * Drop open file reference. If we were the last open file,
4052 * we may need to release capabilities to the MDS (or schedule
4053 * their delayed release).
4054 */
4055void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
4056{
Yan, Zheng774a6a12016-06-06 16:01:39 +08004057 int i, last = 0;
4058 int bits = (fmode << 1) | 1;
Sage Weilbe655592011-11-30 09:47:09 -08004059 spin_lock(&ci->i_ceph_lock);
Yan, Zheng774a6a12016-06-06 16:01:39 +08004060 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4061 if (bits & (1 << i)) {
4062 BUG_ON(ci->i_nr_by_mode[i] == 0);
4063 if (--ci->i_nr_by_mode[i] == 0)
4064 last++;
4065 }
4066 }
4067 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
4068 &ci->vfs_inode, fmode,
4069 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
4070 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
Sage Weilbe655592011-11-30 09:47:09 -08004071 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07004072
4073 if (last && ci->i_vino.snap == CEPH_NOSNAP)
4074 ceph_check_caps(ci, 0, NULL);
4075}
4076
4077/*
Zhi Zhang6ef0bc62018-01-24 21:24:33 +08004078 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
4079 * looks like the link count will hit 0, drop any other caps (other
4080 * than PIN) we don't specifically want (due to the file still being
4081 * open).
4082 */
4083int ceph_drop_caps_for_unlink(struct inode *inode)
4084{
4085 struct ceph_inode_info *ci = ceph_inode(inode);
4086 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4087
4088 spin_lock(&ci->i_ceph_lock);
4089 if (inode->i_nlink == 1) {
4090 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4091
4092 ci->i_ceph_flags |= CEPH_I_NODELAY;
4093 if (__ceph_caps_dirty(ci)) {
4094 struct ceph_mds_client *mdsc =
4095 ceph_inode_to_client(inode)->mdsc;
4096 __cap_delay_requeue_front(mdsc, ci);
4097 }
4098 }
4099 spin_unlock(&ci->i_ceph_lock);
4100 return drop;
4101}
4102
4103/*
Sage Weila8599bd2009-10-06 11:31:12 -07004104 * Helpers for embedding cap and dentry lease releases into mds
4105 * requests.
4106 *
4107 * @force is used by dentry_release (below) to force inclusion of a
4108 * record for the directory inode, even when there aren't any caps to
4109 * drop.
4110 */
4111int ceph_encode_inode_release(void **p, struct inode *inode,
4112 int mds, int drop, int unless, int force)
4113{
4114 struct ceph_inode_info *ci = ceph_inode(inode);
4115 struct ceph_cap *cap;
4116 struct ceph_mds_request_release *rel = *p;
Sage Weilec97f882010-06-24 15:12:37 -07004117 int used, dirty;
Sage Weila8599bd2009-10-06 11:31:12 -07004118 int ret = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07004119
Sage Weilbe655592011-11-30 09:47:09 -08004120 spin_lock(&ci->i_ceph_lock);
Sage Weil916623d2010-03-16 15:01:07 -07004121 used = __ceph_caps_used(ci);
Sage Weilec97f882010-06-24 15:12:37 -07004122 dirty = __ceph_caps_dirty(ci);
Sage Weil916623d2010-03-16 15:01:07 -07004123
Sage Weilec97f882010-06-24 15:12:37 -07004124 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
4125 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
Sage Weil916623d2010-03-16 15:01:07 -07004126 ceph_cap_string(unless));
4127
Sage Weilec97f882010-06-24 15:12:37 -07004128 /* only drop unused, clean caps */
4129 drop &= ~(used | dirty);
Sage Weil916623d2010-03-16 15:01:07 -07004130
Sage Weila8599bd2009-10-06 11:31:12 -07004131 cap = __get_cap_for_mds(ci, mds);
4132 if (cap && __cap_is_valid(cap)) {
Yan, Zheng222b7f92017-11-23 17:47:15 +08004133 unless &= cap->issued;
4134 if (unless) {
4135 if (unless & CEPH_CAP_AUTH_EXCL)
4136 drop &= ~CEPH_CAP_AUTH_SHARED;
4137 if (unless & CEPH_CAP_LINK_EXCL)
4138 drop &= ~CEPH_CAP_LINK_SHARED;
4139 if (unless & CEPH_CAP_XATTR_EXCL)
4140 drop &= ~CEPH_CAP_XATTR_SHARED;
4141 if (unless & CEPH_CAP_FILE_EXCL)
4142 drop &= ~CEPH_CAP_FILE_SHARED;
4143 }
4144
4145 if (force || (cap->issued & drop)) {
4146 if (cap->issued & drop) {
Yan, Zhengbb137f82013-06-03 18:22:17 +08004147 int wanted = __ceph_caps_wanted(ci);
4148 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4149 wanted |= cap->mds_wanted;
4150 dout("encode_inode_release %p cap %p "
4151 "%s -> %s, wanted %s -> %s\n", inode, cap,
Sage Weila8599bd2009-10-06 11:31:12 -07004152 ceph_cap_string(cap->issued),
Yan, Zhengbb137f82013-06-03 18:22:17 +08004153 ceph_cap_string(cap->issued & ~drop),
4154 ceph_cap_string(cap->mds_wanted),
4155 ceph_cap_string(wanted));
4156
Sage Weila8599bd2009-10-06 11:31:12 -07004157 cap->issued &= ~drop;
4158 cap->implemented &= ~drop;
Yan, Zhengbb137f82013-06-03 18:22:17 +08004159 cap->mds_wanted = wanted;
Sage Weila8599bd2009-10-06 11:31:12 -07004160 } else {
4161 dout("encode_inode_release %p cap %p %s"
4162 " (force)\n", inode, cap,
4163 ceph_cap_string(cap->issued));
4164 }
4165
4166 rel->ino = cpu_to_le64(ceph_ino(inode));
4167 rel->cap_id = cpu_to_le64(cap->cap_id);
4168 rel->seq = cpu_to_le32(cap->seq);
Himangi Saraogi08a0f242014-07-23 20:11:11 +05304169 rel->issue_seq = cpu_to_le32(cap->issue_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07004170 rel->mseq = cpu_to_le32(cap->mseq);
Yan, Zhengfd7b95c2014-04-17 08:02:02 +08004171 rel->caps = cpu_to_le32(cap->implemented);
Sage Weila8599bd2009-10-06 11:31:12 -07004172 rel->wanted = cpu_to_le32(cap->mds_wanted);
4173 rel->dname_len = 0;
4174 rel->dname_seq = 0;
4175 *p += sizeof(*rel);
4176 ret = 1;
4177 } else {
Yan, Zheng222b7f92017-11-23 17:47:15 +08004178 dout("encode_inode_release %p cap %p %s (noop)\n",
Sage Weila8599bd2009-10-06 11:31:12 -07004179 inode, cap, ceph_cap_string(cap->issued));
4180 }
4181 }
Sage Weilbe655592011-11-30 09:47:09 -08004182 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07004183 return ret;
4184}
4185
4186int ceph_encode_dentry_release(void **p, struct dentry *dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004187 struct inode *dir,
Sage Weila8599bd2009-10-06 11:31:12 -07004188 int mds, int drop, int unless)
4189{
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004190 struct dentry *parent = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07004191 struct ceph_mds_request_release *rel = *p;
4192 struct ceph_dentry_info *di = ceph_dentry(dentry);
4193 int force = 0;
4194 int ret;
4195
4196 /*
4197 * force an record for the directory caps if we have a dentry lease.
Sage Weilbe655592011-11-30 09:47:09 -08004198 * this is racy (can't take i_ceph_lock and d_lock together), but it
Sage Weila8599bd2009-10-06 11:31:12 -07004199 * doesn't have to be perfect; the mds will revoke anything we don't
4200 * release.
4201 */
4202 spin_lock(&dentry->d_lock);
4203 if (di->lease_session && di->lease_session->s_mds == mds)
4204 force = 1;
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004205 if (!dir) {
4206 parent = dget(dentry->d_parent);
4207 dir = d_inode(parent);
4208 }
Sage Weila8599bd2009-10-06 11:31:12 -07004209 spin_unlock(&dentry->d_lock);
4210
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004211 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
Jeff Laytonadf0d682016-12-15 08:37:58 -05004212 dput(parent);
Sage Weila8599bd2009-10-06 11:31:12 -07004213
4214 spin_lock(&dentry->d_lock);
4215 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4216 dout("encode_dentry_release %p mds%d seq %d\n",
4217 dentry, mds, (int)di->lease_seq);
4218 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4219 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4220 *p += dentry->d_name.len;
4221 rel->dname_seq = cpu_to_le32(di->lease_seq);
Sage Weil1dadcce2010-07-23 13:54:21 -07004222 __ceph_mdsc_drop_dentry_lease(dentry);
Sage Weila8599bd2009-10-06 11:31:12 -07004223 }
4224 spin_unlock(&dentry->d_lock);
4225 return ret;
4226}