blob: 8e66fb0e743d1facddc9aa8192f7222256e7b312 [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';
72 if (c & CEPH_CAP_GLAZYIO)
73 *s++ = 'l';
74 return s;
75}
76
77const char *ceph_cap_string(int caps)
78{
79 int i;
80 char *s;
81 int c;
82
83 spin_lock(&cap_str_lock);
84 i = last_cap_str++;
85 if (last_cap_str == MAX_CAP_STR)
86 last_cap_str = 0;
87 spin_unlock(&cap_str_lock);
88
89 s = cap_str[i];
90
91 if (caps & CEPH_CAP_PIN)
92 *s++ = 'p';
93
94 c = (caps >> CEPH_CAP_SAUTH) & 3;
95 if (c) {
96 *s++ = 'A';
97 s = gcap_string(s, c);
98 }
99
100 c = (caps >> CEPH_CAP_SLINK) & 3;
101 if (c) {
102 *s++ = 'L';
103 s = gcap_string(s, c);
104 }
105
106 c = (caps >> CEPH_CAP_SXATTR) & 3;
107 if (c) {
108 *s++ = 'X';
109 s = gcap_string(s, c);
110 }
111
112 c = caps >> CEPH_CAP_SFILE;
113 if (c) {
114 *s++ = 'F';
115 s = gcap_string(s, c);
116 }
117
118 if (s == cap_str[i])
119 *s++ = '-';
120 *s = 0;
121 return cap_str[i];
122}
123
Yehuda Sadeh37151662010-06-17 16:16:12 -0700124void ceph_caps_init(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -0700125{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700126 INIT_LIST_HEAD(&mdsc->caps_list);
127 spin_lock_init(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700128}
129
Yehuda Sadeh37151662010-06-17 16:16:12 -0700130void ceph_caps_finalize(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -0700131{
132 struct ceph_cap *cap;
133
Yehuda Sadeh37151662010-06-17 16:16:12 -0700134 spin_lock(&mdsc->caps_list_lock);
135 while (!list_empty(&mdsc->caps_list)) {
136 cap = list_first_entry(&mdsc->caps_list,
137 struct ceph_cap, caps_item);
Sage Weila8599bd2009-10-06 11:31:12 -0700138 list_del(&cap->caps_item);
139 kmem_cache_free(ceph_cap_cachep, cap);
140 }
Yehuda Sadeh37151662010-06-17 16:16:12 -0700141 mdsc->caps_total_count = 0;
142 mdsc->caps_avail_count = 0;
143 mdsc->caps_use_count = 0;
144 mdsc->caps_reserve_count = 0;
145 mdsc->caps_min_count = 0;
146 spin_unlock(&mdsc->caps_list_lock);
Sage Weil85ccce42010-02-17 10:02:43 -0800147}
148
Yehuda Sadeh37151662010-06-17 16:16:12 -0700149void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta)
Sage Weil85ccce42010-02-17 10:02:43 -0800150{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700151 spin_lock(&mdsc->caps_list_lock);
152 mdsc->caps_min_count += delta;
153 BUG_ON(mdsc->caps_min_count < 0);
154 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700155}
156
Zhi Zhange30ee582018-01-24 21:24:33 +0800157/*
158 * Called under mdsc->mutex.
159 */
160int ceph_reserve_caps(struct ceph_mds_client *mdsc,
Yehuda Sadeh37151662010-06-17 16:16:12 -0700161 struct ceph_cap_reservation *ctx, int need)
Sage Weila8599bd2009-10-06 11:31:12 -0700162{
Zhi Zhange30ee582018-01-24 21:24:33 +0800163 int i, j;
Sage Weila8599bd2009-10-06 11:31:12 -0700164 struct ceph_cap *cap;
165 int have;
166 int alloc = 0;
Zhi Zhange30ee582018-01-24 21:24:33 +0800167 int max_caps;
168 bool trimmed = false;
169 struct ceph_mds_session *s;
Sage Weila8599bd2009-10-06 11:31:12 -0700170 LIST_HEAD(newcaps);
Sage Weila8599bd2009-10-06 11:31:12 -0700171
172 dout("reserve caps ctx=%p need=%d\n", ctx, need);
173
174 /* first reserve any caps that are already allocated */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700175 spin_lock(&mdsc->caps_list_lock);
176 if (mdsc->caps_avail_count >= need)
Sage Weila8599bd2009-10-06 11:31:12 -0700177 have = need;
178 else
Yehuda Sadeh37151662010-06-17 16:16:12 -0700179 have = mdsc->caps_avail_count;
180 mdsc->caps_avail_count -= have;
181 mdsc->caps_reserve_count += have;
182 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
183 mdsc->caps_reserve_count +
184 mdsc->caps_avail_count);
185 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700186
187 for (i = have; i < need; i++) {
Zhi Zhange30ee582018-01-24 21:24:33 +0800188retry:
Sage Weila8599bd2009-10-06 11:31:12 -0700189 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
Zhi Zhange30ee582018-01-24 21:24:33 +0800190 if (!cap) {
191 if (!trimmed) {
192 for (j = 0; j < mdsc->max_sessions; j++) {
193 s = __ceph_lookup_mds_session(mdsc, j);
194 if (!s)
195 continue;
196 mutex_unlock(&mdsc->mutex);
197
198 mutex_lock(&s->s_mutex);
199 max_caps = s->s_nr_caps - (need - i);
200 ceph_trim_caps(mdsc, s, max_caps);
201 mutex_unlock(&s->s_mutex);
202
203 ceph_put_mds_session(s);
204 mutex_lock(&mdsc->mutex);
205 }
206 trimmed = true;
207 goto retry;
208 } else {
209 pr_warn("reserve caps ctx=%p ENOMEM "
210 "need=%d got=%d\n",
211 ctx, need, have + alloc);
212 goto out_nomem;
213 }
214 }
Sage Weila8599bd2009-10-06 11:31:12 -0700215 list_add(&cap->caps_item, &newcaps);
216 alloc++;
217 }
Zhi Zhange30ee582018-01-24 21:24:33 +0800218 BUG_ON(have + alloc != need);
Sage Weila8599bd2009-10-06 11:31:12 -0700219
Yehuda Sadeh37151662010-06-17 16:16:12 -0700220 spin_lock(&mdsc->caps_list_lock);
221 mdsc->caps_total_count += alloc;
222 mdsc->caps_reserve_count += alloc;
223 list_splice(&newcaps, &mdsc->caps_list);
Sage Weila8599bd2009-10-06 11:31:12 -0700224
Yehuda Sadeh37151662010-06-17 16:16:12 -0700225 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
226 mdsc->caps_reserve_count +
227 mdsc->caps_avail_count);
228 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700229
230 ctx->count = need;
231 dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700232 ctx, mdsc->caps_total_count, mdsc->caps_use_count,
233 mdsc->caps_reserve_count, mdsc->caps_avail_count);
Zhi Zhange30ee582018-01-24 21:24:33 +0800234 return 0;
235
236out_nomem:
237 while (!list_empty(&newcaps)) {
238 cap = list_first_entry(&newcaps,
239 struct ceph_cap, caps_item);
240 list_del(&cap->caps_item);
241 kmem_cache_free(ceph_cap_cachep, cap);
242 }
243
244 spin_lock(&mdsc->caps_list_lock);
245 mdsc->caps_avail_count += have;
246 mdsc->caps_reserve_count -= have;
247 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
248 mdsc->caps_reserve_count +
249 mdsc->caps_avail_count);
250 spin_unlock(&mdsc->caps_list_lock);
251 return -ENOMEM;
Sage Weila8599bd2009-10-06 11:31:12 -0700252}
253
Yehuda Sadeh37151662010-06-17 16:16:12 -0700254int ceph_unreserve_caps(struct ceph_mds_client *mdsc,
255 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700256{
257 dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count);
258 if (ctx->count) {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700259 spin_lock(&mdsc->caps_list_lock);
260 BUG_ON(mdsc->caps_reserve_count < ctx->count);
261 mdsc->caps_reserve_count -= ctx->count;
262 mdsc->caps_avail_count += ctx->count;
Sage Weila8599bd2009-10-06 11:31:12 -0700263 ctx->count = 0;
264 dout("unreserve caps %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700265 mdsc->caps_total_count, mdsc->caps_use_count,
266 mdsc->caps_reserve_count, mdsc->caps_avail_count);
267 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
268 mdsc->caps_reserve_count +
269 mdsc->caps_avail_count);
270 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700271 }
272 return 0;
273}
274
Yan, Zhengd9df2782014-04-18 09:57:11 +0800275struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
276 struct ceph_cap_reservation *ctx)
Sage Weila8599bd2009-10-06 11:31:12 -0700277{
278 struct ceph_cap *cap = NULL;
279
280 /* temporary, until we do something about cap import/export */
Sage Weil443b3762010-06-29 09:28:39 -0700281 if (!ctx) {
282 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
283 if (cap) {
Yan, Zheng4d1d0532012-11-03 10:32:37 +0800284 spin_lock(&mdsc->caps_list_lock);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700285 mdsc->caps_use_count++;
286 mdsc->caps_total_count++;
Yan, Zheng4d1d0532012-11-03 10:32:37 +0800287 spin_unlock(&mdsc->caps_list_lock);
Sage Weil443b3762010-06-29 09:28:39 -0700288 }
289 return cap;
290 }
Sage Weila8599bd2009-10-06 11:31:12 -0700291
Yehuda Sadeh37151662010-06-17 16:16:12 -0700292 spin_lock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700293 dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700294 ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
295 mdsc->caps_reserve_count, mdsc->caps_avail_count);
Sage Weila8599bd2009-10-06 11:31:12 -0700296 BUG_ON(!ctx->count);
Yehuda Sadeh37151662010-06-17 16:16:12 -0700297 BUG_ON(ctx->count > mdsc->caps_reserve_count);
298 BUG_ON(list_empty(&mdsc->caps_list));
Sage Weila8599bd2009-10-06 11:31:12 -0700299
300 ctx->count--;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700301 mdsc->caps_reserve_count--;
302 mdsc->caps_use_count++;
Sage Weila8599bd2009-10-06 11:31:12 -0700303
Yehuda Sadeh37151662010-06-17 16:16:12 -0700304 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
Sage Weila8599bd2009-10-06 11:31:12 -0700305 list_del(&cap->caps_item);
306
Yehuda Sadeh37151662010-06-17 16:16:12 -0700307 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
308 mdsc->caps_reserve_count + mdsc->caps_avail_count);
309 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700310 return cap;
311}
312
Yehuda Sadeh37151662010-06-17 16:16:12 -0700313void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700314{
Yehuda Sadeh37151662010-06-17 16:16:12 -0700315 spin_lock(&mdsc->caps_list_lock);
Sage Weil7c1332b2010-02-16 11:39:45 -0800316 dout("put_cap %p %d = %d used + %d resv + %d avail\n",
Yehuda Sadeh37151662010-06-17 16:16:12 -0700317 cap, mdsc->caps_total_count, mdsc->caps_use_count,
318 mdsc->caps_reserve_count, mdsc->caps_avail_count);
319 mdsc->caps_use_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700320 /*
Sage Weil85ccce42010-02-17 10:02:43 -0800321 * Keep some preallocated caps around (ceph_min_count), to
322 * avoid lots of free/alloc churn.
Sage Weila8599bd2009-10-06 11:31:12 -0700323 */
Yehuda Sadeh37151662010-06-17 16:16:12 -0700324 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
325 mdsc->caps_min_count) {
326 mdsc->caps_total_count--;
Sage Weila8599bd2009-10-06 11:31:12 -0700327 kmem_cache_free(ceph_cap_cachep, cap);
328 } else {
Yehuda Sadeh37151662010-06-17 16:16:12 -0700329 mdsc->caps_avail_count++;
330 list_add(&cap->caps_item, &mdsc->caps_list);
Sage Weila8599bd2009-10-06 11:31:12 -0700331 }
332
Yehuda Sadeh37151662010-06-17 16:16:12 -0700333 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
334 mdsc->caps_reserve_count + mdsc->caps_avail_count);
335 spin_unlock(&mdsc->caps_list_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700336}
337
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700338void ceph_reservation_status(struct ceph_fs_client *fsc,
Sage Weil85ccce42010-02-17 10:02:43 -0800339 int *total, int *avail, int *used, int *reserved,
340 int *min)
Sage Weila8599bd2009-10-06 11:31:12 -0700341{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700342 struct ceph_mds_client *mdsc = fsc->mdsc;
Yehuda Sadeh37151662010-06-17 16:16:12 -0700343
Sage Weila8599bd2009-10-06 11:31:12 -0700344 if (total)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700345 *total = mdsc->caps_total_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700346 if (avail)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700347 *avail = mdsc->caps_avail_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700348 if (used)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700349 *used = mdsc->caps_use_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700350 if (reserved)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700351 *reserved = mdsc->caps_reserve_count;
Sage Weil85ccce42010-02-17 10:02:43 -0800352 if (min)
Yehuda Sadeh37151662010-06-17 16:16:12 -0700353 *min = mdsc->caps_min_count;
Sage Weila8599bd2009-10-06 11:31:12 -0700354}
355
356/*
357 * Find ceph_cap for given mds, if any.
358 *
Sage Weilbe655592011-11-30 09:47:09 -0800359 * Called with i_ceph_lock held.
Sage Weila8599bd2009-10-06 11:31:12 -0700360 */
361static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
362{
363 struct ceph_cap *cap;
364 struct rb_node *n = ci->i_caps.rb_node;
365
366 while (n) {
367 cap = rb_entry(n, struct ceph_cap, ci_node);
368 if (mds < cap->mds)
369 n = n->rb_left;
370 else if (mds > cap->mds)
371 n = n->rb_right;
372 else
373 return cap;
374 }
375 return NULL;
376}
377
Greg Farnum2bc50252010-06-30 12:44:34 -0700378struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
379{
380 struct ceph_cap *cap;
381
Sage Weilbe655592011-11-30 09:47:09 -0800382 spin_lock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700383 cap = __get_cap_for_mds(ci, mds);
Sage Weilbe655592011-11-30 09:47:09 -0800384 spin_unlock(&ci->i_ceph_lock);
Greg Farnum2bc50252010-06-30 12:44:34 -0700385 return cap;
386}
387
Sage Weila8599bd2009-10-06 11:31:12 -0700388/*
Sage Weil33caad32010-05-26 14:31:27 -0700389 * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1.
Sage Weila8599bd2009-10-06 11:31:12 -0700390 */
Sage Weilca81f3f2010-06-10 14:21:36 -0700391static int __ceph_get_cap_mds(struct ceph_inode_info *ci)
Sage Weila8599bd2009-10-06 11:31:12 -0700392{
393 struct ceph_cap *cap;
394 int mds = -1;
395 struct rb_node *p;
396
Sage Weil33caad32010-05-26 14:31:27 -0700397 /* prefer mds with WR|BUFFER|EXCL caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700398 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
399 cap = rb_entry(p, struct ceph_cap, ci_node);
400 mds = cap->mds;
Sage Weila8599bd2009-10-06 11:31:12 -0700401 if (cap->issued & (CEPH_CAP_FILE_WR |
402 CEPH_CAP_FILE_BUFFER |
403 CEPH_CAP_FILE_EXCL))
404 break;
405 }
406 return mds;
407}
408
409int ceph_get_cap_mds(struct inode *inode)
410{
Sage Weilbe655592011-11-30 09:47:09 -0800411 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -0700412 int mds;
Sage Weilbe655592011-11-30 09:47:09 -0800413 spin_lock(&ci->i_ceph_lock);
Sage Weilca81f3f2010-06-10 14:21:36 -0700414 mds = __ceph_get_cap_mds(ceph_inode(inode));
Sage Weilbe655592011-11-30 09:47:09 -0800415 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700416 return mds;
417}
418
419/*
Sage Weilbe655592011-11-30 09:47:09 -0800420 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700421 */
422static void __insert_cap_node(struct ceph_inode_info *ci,
423 struct ceph_cap *new)
424{
425 struct rb_node **p = &ci->i_caps.rb_node;
426 struct rb_node *parent = NULL;
427 struct ceph_cap *cap = NULL;
428
429 while (*p) {
430 parent = *p;
431 cap = rb_entry(parent, struct ceph_cap, ci_node);
432 if (new->mds < cap->mds)
433 p = &(*p)->rb_left;
434 else if (new->mds > cap->mds)
435 p = &(*p)->rb_right;
436 else
437 BUG();
438 }
439
440 rb_link_node(&new->ci_node, parent, p);
441 rb_insert_color(&new->ci_node, &ci->i_caps);
442}
443
444/*
445 * (re)set cap hold timeouts, which control the delayed release
446 * of unused caps back to the MDS. Should be called on cap use.
447 */
448static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
449 struct ceph_inode_info *ci)
450{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700451 struct ceph_mount_options *ma = mdsc->fsc->mount_options;
Sage Weila8599bd2009-10-06 11:31:12 -0700452
453 ci->i_hold_caps_min = round_jiffies(jiffies +
454 ma->caps_wanted_delay_min * HZ);
455 ci->i_hold_caps_max = round_jiffies(jiffies +
456 ma->caps_wanted_delay_max * HZ);
457 dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode,
458 ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies);
459}
460
461/*
462 * (Re)queue cap at the end of the delayed cap release list.
463 *
464 * If I_FLUSH is set, leave the inode at the front of the list.
465 *
Sage Weilbe655592011-11-30 09:47:09 -0800466 * Caller holds i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -0700467 * -> we take mdsc->cap_delay_lock
468 */
469static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
470 struct ceph_inode_info *ci)
471{
472 __cap_set_timeouts(mdsc, ci);
473 dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode,
474 ci->i_ceph_flags, ci->i_hold_caps_max);
475 if (!mdsc->stopping) {
476 spin_lock(&mdsc->cap_delay_lock);
477 if (!list_empty(&ci->i_cap_delay_list)) {
478 if (ci->i_ceph_flags & CEPH_I_FLUSH)
479 goto no_change;
480 list_del_init(&ci->i_cap_delay_list);
481 }
482 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
483no_change:
484 spin_unlock(&mdsc->cap_delay_lock);
485 }
486}
487
488/*
489 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
490 * indicating we should send a cap message to flush dirty metadata
491 * asap, and move to the front of the delayed cap list.
492 */
493static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
494 struct ceph_inode_info *ci)
495{
496 dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode);
497 spin_lock(&mdsc->cap_delay_lock);
498 ci->i_ceph_flags |= CEPH_I_FLUSH;
499 if (!list_empty(&ci->i_cap_delay_list))
500 list_del_init(&ci->i_cap_delay_list);
501 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
502 spin_unlock(&mdsc->cap_delay_lock);
503}
504
505/*
506 * Cancel delayed work on cap.
507 *
Sage Weilbe655592011-11-30 09:47:09 -0800508 * Caller must hold i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -0700509 */
510static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
511 struct ceph_inode_info *ci)
512{
513 dout("__cap_delay_cancel %p\n", &ci->vfs_inode);
514 if (list_empty(&ci->i_cap_delay_list))
515 return;
516 spin_lock(&mdsc->cap_delay_lock);
517 list_del_init(&ci->i_cap_delay_list);
518 spin_unlock(&mdsc->cap_delay_lock);
519}
520
521/*
522 * Common issue checks for add_cap, handle_cap_grant.
523 */
524static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
525 unsigned issued)
526{
527 unsigned had = __ceph_caps_issued(ci, NULL);
528
529 /*
530 * Each time we receive FILE_CACHE anew, we increment
531 * i_rdcache_gen.
532 */
Sage Weil29625072010-05-27 10:40:43 -0700533 if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400534 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -0700535 ci->i_rdcache_gen++;
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400536 }
Sage Weila8599bd2009-10-06 11:31:12 -0700537
538 /*
Yan, Zheng15b51bd2017-09-06 10:15:16 +0800539 * If FILE_SHARED is newly issued, mark dir not complete. We don't
540 * know what happened to this directory while we didn't have the cap.
541 * If FILE_SHARED is being revoked, also mark dir not complete. It
542 * stops on-going cached readdir.
Sage Weila8599bd2009-10-06 11:31:12 -0700543 */
Yan, Zheng15b51bd2017-09-06 10:15:16 +0800544 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
545 if (issued & CEPH_CAP_FILE_SHARED)
Yan, Zheng97aeb6b2017-11-27 10:47:46 +0800546 atomic_inc(&ci->i_shared_gen);
Yan, Zhenga8673d62013-02-18 16:38:14 +0800547 if (S_ISDIR(ci->vfs_inode.i_mode)) {
548 dout(" marking %p NOT complete\n", &ci->vfs_inode);
Yan, Zheng2f276c52013-03-13 19:44:32 +0800549 __ceph_dir_clear_complete(ci);
Yan, Zhenga8673d62013-02-18 16:38:14 +0800550 }
Sage Weila8599bd2009-10-06 11:31:12 -0700551 }
552}
553
554/*
555 * Add a capability under the given MDS session.
556 *
557 * Caller should hold session snap_rwsem (read) and s_mutex.
558 *
559 * @fmode is the open file mode, if we are opening a file, otherwise
560 * it is < 0. (This is so we can atomically add the cap and add an
561 * open file reference to it.)
562 */
Yan, Zhengd9df2782014-04-18 09:57:11 +0800563void ceph_add_cap(struct inode *inode,
564 struct ceph_mds_session *session, u64 cap_id,
565 int fmode, unsigned issued, unsigned wanted,
566 unsigned seq, unsigned mseq, u64 realmino, int flags,
567 struct ceph_cap **new_cap)
Sage Weila8599bd2009-10-06 11:31:12 -0700568{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700569 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -0700570 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weila8599bd2009-10-06 11:31:12 -0700571 struct ceph_cap *cap;
572 int mds = session->s_mds;
573 int actual_wanted;
574
575 dout("add_cap %p mds%d cap %llx %s seq %d\n", inode,
576 session->s_mds, cap_id, ceph_cap_string(issued), seq);
577
578 /*
579 * If we are opening the file, include file mode wanted bits
580 * in wanted.
581 */
582 if (fmode >= 0)
583 wanted |= ceph_caps_for_mode(fmode);
584
Sage Weila8599bd2009-10-06 11:31:12 -0700585 cap = __get_cap_for_mds(ci, mds);
586 if (!cap) {
Yan, Zhengd9df2782014-04-18 09:57:11 +0800587 cap = *new_cap;
588 *new_cap = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -0700589
590 cap->issued = 0;
591 cap->implemented = 0;
592 cap->mds = mds;
593 cap->mds_wanted = 0;
Yan, Zheng964266c2013-02-27 09:26:09 +0800594 cap->mseq = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700595
596 cap->ci = ci;
597 __insert_cap_node(ci, cap);
598
Sage Weila8599bd2009-10-06 11:31:12 -0700599 /* add to session cap list */
600 cap->session = session;
601 spin_lock(&session->s_cap_lock);
602 list_add_tail(&cap->session_caps, &session->s_caps);
603 session->s_nr_caps++;
604 spin_unlock(&session->s_cap_lock);
Yan, Zheng11df2df2013-11-24 14:44:38 +0800605 } else {
Yan, Zheng11df2df2013-11-24 14:44:38 +0800606 /*
607 * auth mds of the inode changed. we received the cap export
608 * message, but still haven't received the cap import message.
609 * handle_cap_export() updated the new auth MDS' cap.
610 *
611 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
612 * a message that was send before the cap import message. So
613 * don't remove caps.
614 */
615 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
616 WARN_ON(cap != ci->i_auth_cap);
617 WARN_ON(cap->cap_id != cap_id);
618 seq = cap->seq;
619 mseq = cap->mseq;
620 issued |= cap->issued;
621 flags |= CEPH_CAP_FLAG_AUTH;
622 }
623 }
Sage Weila8599bd2009-10-06 11:31:12 -0700624
Yan, Zheng7d9c9192017-12-19 18:00:54 +0800625 if (!ci->i_snap_realm ||
626 ((flags & CEPH_CAP_FLAG_AUTH) &&
627 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
Sage Weila8599bd2009-10-06 11:31:12 -0700628 /*
629 * add this inode to the appropriate snap realm
630 */
631 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
632 realmino);
633 if (realm) {
Yan, Zheng7d9c9192017-12-19 18:00:54 +0800634 struct ceph_snap_realm *oldrealm = ci->i_snap_realm;
635 if (oldrealm) {
636 spin_lock(&oldrealm->inodes_with_caps_lock);
637 list_del_init(&ci->i_snap_realm_item);
638 spin_unlock(&oldrealm->inodes_with_caps_lock);
639 }
640
Sage Weila8599bd2009-10-06 11:31:12 -0700641 spin_lock(&realm->inodes_with_caps_lock);
642 ci->i_snap_realm = realm;
643 list_add(&ci->i_snap_realm_item,
644 &realm->inodes_with_caps);
645 spin_unlock(&realm->inodes_with_caps_lock);
Yan, Zheng7d9c9192017-12-19 18:00:54 +0800646
647 if (oldrealm)
648 ceph_put_snap_realm(mdsc, oldrealm);
Sage Weila8599bd2009-10-06 11:31:12 -0700649 } else {
650 pr_err("ceph_add_cap: couldn't find snap realm %llx\n",
651 realmino);
Sage Weilb8cd07e2010-07-16 12:00:02 -0700652 WARN_ON(!realm);
Sage Weila8599bd2009-10-06 11:31:12 -0700653 }
654 }
655
656 __check_cap_issue(ci, cap, issued);
657
658 /*
659 * If we are issued caps we don't want, or the mds' wanted
660 * value appears to be off, queue a check so we'll release
661 * later and/or update the mds wanted value.
662 */
663 actual_wanted = __ceph_caps_wanted(ci);
664 if ((wanted & ~actual_wanted) ||
665 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
666 dout(" issued %s, mds wanted %s, actual %s, queueing\n",
667 ceph_cap_string(issued), ceph_cap_string(wanted),
668 ceph_cap_string(actual_wanted));
669 __cap_delay_requeue(mdsc, ci);
670 }
671
Yan, Zhengb8c2f3a2013-05-31 16:37:11 +0800672 if (flags & CEPH_CAP_FLAG_AUTH) {
Markus Elfringd37b1d92017-08-20 20:22:02 +0200673 if (!ci->i_auth_cap ||
Yan, Zhengd9ffc4f2014-03-18 10:15:29 +0800674 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
Yan, Zhengb8c2f3a2013-05-31 16:37:11 +0800675 ci->i_auth_cap = cap;
Yan, Zhengd9ffc4f2014-03-18 10:15:29 +0800676 cap->mds_wanted = wanted;
677 }
Yan, Zheng11df2df2013-11-24 14:44:38 +0800678 } else {
679 WARN_ON(ci->i_auth_cap == cap);
Yan, Zheng8a92a112013-01-04 14:28:07 +0800680 }
Sage Weila8599bd2009-10-06 11:31:12 -0700681
682 dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n",
683 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
684 ceph_cap_string(issued|cap->issued), seq, mds);
685 cap->cap_id = cap_id;
686 cap->issued = issued;
687 cap->implemented |= issued;
Yan, Zhengd1b87802013-11-13 14:47:19 +0800688 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
Yan, Zheng964266c2013-02-27 09:26:09 +0800689 cap->mds_wanted = wanted;
690 else
691 cap->mds_wanted |= wanted;
Sage Weila8599bd2009-10-06 11:31:12 -0700692 cap->seq = seq;
693 cap->issue_seq = seq;
694 cap->mseq = mseq;
Sage Weil685f9a5d2009-11-09 12:05:48 -0800695 cap->cap_gen = session->s_cap_gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700696
697 if (fmode >= 0)
698 __ceph_get_fmode(ci, fmode);
Sage Weila8599bd2009-10-06 11:31:12 -0700699}
700
701/*
702 * Return true if cap has not timed out and belongs to the current
703 * generation of the MDS session (i.e. has not gone 'stale' due to
704 * us losing touch with the mds).
705 */
706static int __cap_is_valid(struct ceph_cap *cap)
707{
708 unsigned long ttl;
Sage Weilcdac8302009-11-10 16:02:23 -0800709 u32 gen;
Sage Weila8599bd2009-10-06 11:31:12 -0700710
Alex Elderd8fb02a2012-01-12 17:48:10 -0800711 spin_lock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700712 gen = cap->session->s_cap_gen;
713 ttl = cap->session->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -0800714 spin_unlock(&cap->session->s_gen_ttl_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700715
Sage Weil685f9a5d2009-11-09 12:05:48 -0800716 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
Sage Weila8599bd2009-10-06 11:31:12 -0700717 dout("__cap_is_valid %p cap %p issued %s "
718 "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode,
Sage Weil685f9a5d2009-11-09 12:05:48 -0800719 cap, ceph_cap_string(cap->issued), cap->cap_gen, gen);
Sage Weila8599bd2009-10-06 11:31:12 -0700720 return 0;
721 }
722
723 return 1;
724}
725
726/*
727 * Return set of valid cap bits issued to us. Note that caps time
728 * out, and may be invalidated in bulk if the client session times out
729 * and session->s_cap_gen is bumped.
730 */
731int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
732{
Yan, Zhengd9df2782014-04-18 09:57:11 +0800733 int have = ci->i_snap_caps;
Sage Weila8599bd2009-10-06 11:31:12 -0700734 struct ceph_cap *cap;
735 struct rb_node *p;
736
737 if (implemented)
738 *implemented = 0;
739 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
740 cap = rb_entry(p, struct ceph_cap, ci_node);
741 if (!__cap_is_valid(cap))
742 continue;
743 dout("__ceph_caps_issued %p cap %p issued %s\n",
744 &ci->vfs_inode, cap, ceph_cap_string(cap->issued));
745 have |= cap->issued;
746 if (implemented)
747 *implemented |= cap->implemented;
748 }
Yan, Zhengb1530f52013-07-02 12:40:20 +0800749 /*
750 * exclude caps issued by non-auth MDS, but are been revoking
751 * by the auth MDS. The non-auth MDS should be revoking/exporting
752 * these caps, but the message is delayed.
753 */
754 if (ci->i_auth_cap) {
755 cap = ci->i_auth_cap;
756 have &= ~cap->implemented | cap->issued;
757 }
Sage Weila8599bd2009-10-06 11:31:12 -0700758 return have;
759}
760
761/*
762 * Get cap bits issued by caps other than @ocap
763 */
764int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
765{
766 int have = ci->i_snap_caps;
767 struct ceph_cap *cap;
768 struct rb_node *p;
769
770 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
771 cap = rb_entry(p, struct ceph_cap, ci_node);
772 if (cap == ocap)
773 continue;
774 if (!__cap_is_valid(cap))
775 continue;
776 have |= cap->issued;
777 }
778 return have;
779}
780
781/*
782 * Move a cap to the end of the LRU (oldest caps at list head, newest
783 * at list tail).
784 */
785static void __touch_cap(struct ceph_cap *cap)
786{
787 struct ceph_mds_session *s = cap->session;
788
Sage Weila8599bd2009-10-06 11:31:12 -0700789 spin_lock(&s->s_cap_lock);
Markus Elfringd37b1d92017-08-20 20:22:02 +0200790 if (!s->s_cap_iterator) {
Sage Weil5dacf092009-12-21 20:40:34 -0800791 dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap,
792 s->s_mds);
793 list_move_tail(&cap->session_caps, &s->s_caps);
794 } else {
795 dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n",
796 &cap->ci->vfs_inode, cap, s->s_mds);
797 }
Sage Weila8599bd2009-10-06 11:31:12 -0700798 spin_unlock(&s->s_cap_lock);
799}
800
801/*
802 * Check if we hold the given mask. If so, move the cap(s) to the
803 * front of their respective LRUs. (This is the preferred way for
804 * callers to check for caps they want.)
805 */
806int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
807{
808 struct ceph_cap *cap;
809 struct rb_node *p;
810 int have = ci->i_snap_caps;
811
812 if ((have & mask) == mask) {
813 dout("__ceph_caps_issued_mask %p snap issued %s"
814 " (mask %s)\n", &ci->vfs_inode,
815 ceph_cap_string(have),
816 ceph_cap_string(mask));
817 return 1;
818 }
819
820 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
821 cap = rb_entry(p, struct ceph_cap, ci_node);
822 if (!__cap_is_valid(cap))
823 continue;
824 if ((cap->issued & mask) == mask) {
825 dout("__ceph_caps_issued_mask %p cap %p issued %s"
826 " (mask %s)\n", &ci->vfs_inode, cap,
827 ceph_cap_string(cap->issued),
828 ceph_cap_string(mask));
829 if (touch)
830 __touch_cap(cap);
831 return 1;
832 }
833
834 /* does a combination of caps satisfy mask? */
835 have |= cap->issued;
836 if ((have & mask) == mask) {
837 dout("__ceph_caps_issued_mask %p combo issued %s"
838 " (mask %s)\n", &ci->vfs_inode,
839 ceph_cap_string(cap->issued),
840 ceph_cap_string(mask));
841 if (touch) {
842 struct rb_node *q;
843
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300844 /* touch this + preceding caps */
Sage Weila8599bd2009-10-06 11:31:12 -0700845 __touch_cap(cap);
846 for (q = rb_first(&ci->i_caps); q != p;
847 q = rb_next(q)) {
848 cap = rb_entry(q, struct ceph_cap,
849 ci_node);
850 if (!__cap_is_valid(cap))
851 continue;
852 __touch_cap(cap);
853 }
854 }
855 return 1;
856 }
857 }
858
859 return 0;
860}
861
862/*
863 * Return true if mask caps are currently being revoked by an MDS.
864 */
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800865int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
866 struct ceph_cap *ocap, int mask)
867{
868 struct ceph_cap *cap;
869 struct rb_node *p;
870
871 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
872 cap = rb_entry(p, struct ceph_cap, ci_node);
Yan, Zheng9563f882013-11-22 13:50:45 +0800873 if (cap != ocap &&
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800874 (cap->implemented & ~cap->issued & mask))
875 return 1;
876 }
877 return 0;
878}
879
Sage Weila8599bd2009-10-06 11:31:12 -0700880int ceph_caps_revoking(struct ceph_inode_info *ci, int mask)
881{
882 struct inode *inode = &ci->vfs_inode;
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800883 int ret;
Sage Weila8599bd2009-10-06 11:31:12 -0700884
Sage Weilbe655592011-11-30 09:47:09 -0800885 spin_lock(&ci->i_ceph_lock);
Yan, Zheng6ee6b9532013-07-02 12:40:21 +0800886 ret = __ceph_caps_revoking_other(ci, NULL, mask);
Sage Weilbe655592011-11-30 09:47:09 -0800887 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -0700888 dout("ceph_caps_revoking %p %s = %d\n", inode,
889 ceph_cap_string(mask), ret);
890 return ret;
891}
892
893int __ceph_caps_used(struct ceph_inode_info *ci)
894{
895 int used = 0;
896 if (ci->i_pin_ref)
897 used |= CEPH_CAP_PIN;
898 if (ci->i_rd_ref)
899 used |= CEPH_CAP_FILE_RD;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800900 if (ci->i_rdcache_ref ||
901 (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */
902 ci->vfs_inode.i_data.nrpages))
Sage Weila8599bd2009-10-06 11:31:12 -0700903 used |= CEPH_CAP_FILE_CACHE;
904 if (ci->i_wr_ref)
905 used |= CEPH_CAP_FILE_WR;
Henry C Changd3d07202011-05-11 10:29:54 +0000906 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
Sage Weila8599bd2009-10-06 11:31:12 -0700907 used |= CEPH_CAP_FILE_BUFFER;
908 return used;
909}
910
911/*
912 * wanted, by virtue of open file modes
913 */
914int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
915{
Yan, Zheng774a6a12016-06-06 16:01:39 +0800916 int i, bits = 0;
917 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
918 if (ci->i_nr_by_mode[i])
919 bits |= 1 << i;
920 }
921 if (bits == 0)
922 return 0;
923 return ceph_caps_for_mode(bits >> 1);
Sage Weila8599bd2009-10-06 11:31:12 -0700924}
925
926/*
927 * Return caps we have registered with the MDS(s) as 'wanted'.
928 */
Yan, Zhengc1944fe2017-01-29 22:15:47 +0800929int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
Sage Weila8599bd2009-10-06 11:31:12 -0700930{
931 struct ceph_cap *cap;
932 struct rb_node *p;
933 int mds_wanted = 0;
934
935 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
936 cap = rb_entry(p, struct ceph_cap, ci_node);
Yan, Zhengc1944fe2017-01-29 22:15:47 +0800937 if (check && !__cap_is_valid(cap))
Sage Weila8599bd2009-10-06 11:31:12 -0700938 continue;
Yan, Zhenga2550602014-03-08 09:51:45 +0800939 if (cap == ci->i_auth_cap)
940 mds_wanted |= cap->mds_wanted;
941 else
942 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
Sage Weila8599bd2009-10-06 11:31:12 -0700943 }
944 return mds_wanted;
945}
946
947/*
Sage Weilbe655592011-11-30 09:47:09 -0800948 * called under i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -0700949 */
Yan, Zheng0f439c72018-01-08 14:44:10 +0800950static int __ceph_is_single_caps(struct ceph_inode_info *ci)
951{
952 return rb_first(&ci->i_caps) == rb_last(&ci->i_caps);
953}
954
Sage Weila8599bd2009-10-06 11:31:12 -0700955static int __ceph_is_any_caps(struct ceph_inode_info *ci)
956{
Yan, Zhengd9df2782014-04-18 09:57:11 +0800957 return !RB_EMPTY_ROOT(&ci->i_caps);
Sage Weila8599bd2009-10-06 11:31:12 -0700958}
959
Yan, Zheng9215aee2013-11-30 12:47:41 +0800960int ceph_is_any_caps(struct inode *inode)
961{
962 struct ceph_inode_info *ci = ceph_inode(inode);
963 int ret;
964
965 spin_lock(&ci->i_ceph_lock);
966 ret = __ceph_is_any_caps(ci);
967 spin_unlock(&ci->i_ceph_lock);
968
969 return ret;
970}
971
Yan, Zhengdb40cc12015-03-23 20:12:20 +0800972static void drop_inode_snap_realm(struct ceph_inode_info *ci)
973{
974 struct ceph_snap_realm *realm = ci->i_snap_realm;
975 spin_lock(&realm->inodes_with_caps_lock);
976 list_del_init(&ci->i_snap_realm_item);
977 ci->i_snap_realm_counter++;
978 ci->i_snap_realm = NULL;
979 spin_unlock(&realm->inodes_with_caps_lock);
980 ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc,
981 realm);
982}
983
Sage Weila8599bd2009-10-06 11:31:12 -0700984/*
Sage Weilf818a732010-05-11 20:56:31 -0700985 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
986 *
Sage Weilbe655592011-11-30 09:47:09 -0800987 * caller should hold i_ceph_lock.
Sage Weila6369742010-02-22 13:59:00 -0800988 * caller will not hold session s_mutex if called from destroy_inode.
Sage Weila8599bd2009-10-06 11:31:12 -0700989 */
Yan, Zhenga096b092013-09-22 10:15:58 +0800990void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
Sage Weila8599bd2009-10-06 11:31:12 -0700991{
992 struct ceph_mds_session *session = cap->session;
993 struct ceph_inode_info *ci = cap->ci;
Cheng Renquan640ef792010-03-26 17:40:33 +0800994 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700995 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weilf818a732010-05-11 20:56:31 -0700996 int removed = 0;
Sage Weila8599bd2009-10-06 11:31:12 -0700997
998 dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode);
999
Sage Weil7c1332b2010-02-16 11:39:45 -08001000 /* remove from session list */
1001 spin_lock(&session->s_cap_lock);
1002 if (session->s_cap_iterator == cap) {
1003 /* not yet, we are iterating over this very cap */
1004 dout("__ceph_remove_cap delaying %p removal from session %p\n",
1005 cap, cap->session);
1006 } else {
1007 list_del_init(&cap->session_caps);
1008 session->s_nr_caps--;
1009 cap->session = NULL;
Sage Weilf818a732010-05-11 20:56:31 -07001010 removed = 1;
Sage Weil7c1332b2010-02-16 11:39:45 -08001011 }
Sage Weilf818a732010-05-11 20:56:31 -07001012 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1013 cap->ci = NULL;
Yan, Zheng745a8e32015-05-14 17:22:42 +08001014
1015 /*
1016 * s_cap_reconnect is protected by s_cap_lock. no one changes
1017 * s_cap_gen while session is in the reconnect state.
1018 */
1019 if (queue_release &&
1020 (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) {
1021 cap->queue_release = 1;
1022 if (removed) {
1023 list_add_tail(&cap->session_caps,
1024 &session->s_cap_releases);
1025 session->s_num_cap_releases++;
1026 removed = 0;
1027 }
1028 } else {
1029 cap->queue_release = 0;
1030 }
1031 cap->cap_ino = ci->i_vino.ino;
1032
Sage Weil7c1332b2010-02-16 11:39:45 -08001033 spin_unlock(&session->s_cap_lock);
1034
Sage Weilf818a732010-05-11 20:56:31 -07001035 /* remove from inode list */
1036 rb_erase(&cap->ci_node, &ci->i_caps);
1037 if (ci->i_auth_cap == cap)
1038 ci->i_auth_cap = NULL;
1039
1040 if (removed)
Yehuda Sadeh37151662010-06-17 16:16:12 -07001041 ceph_put_cap(mdsc, cap);
Sage Weila8599bd2009-10-06 11:31:12 -07001042
Yan, Zhengdb40cc12015-03-23 20:12:20 +08001043 /* when reconnect denied, we remove session caps forcibly,
1044 * i_wr_ref can be non-zero. If there are ongoing write,
1045 * keep i_snap_realm.
1046 */
1047 if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm)
1048 drop_inode_snap_realm(ci);
1049
Sage Weila8599bd2009-10-06 11:31:12 -07001050 if (!__ceph_is_any_real_caps(ci))
1051 __cap_delay_cancel(mdsc, ci);
1052}
1053
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001054struct cap_msg_args {
1055 struct ceph_mds_session *session;
1056 u64 ino, cid, follows;
1057 u64 flush_tid, oldest_flush_tid, size, max_size;
1058 u64 xattr_version;
1059 struct ceph_buffer *xattr_buf;
1060 struct timespec atime, mtime, ctime;
1061 int op, caps, wanted, dirty;
1062 u32 seq, issue_seq, mseq, time_warp_seq;
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001063 u32 flags;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001064 kuid_t uid;
1065 kgid_t gid;
1066 umode_t mode;
1067 bool inline_data;
1068};
1069
Sage Weila8599bd2009-10-06 11:31:12 -07001070/*
1071 * Build and send a cap message to the given MDS.
1072 *
1073 * Caller should be holding s_mutex.
1074 */
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001075static int send_cap_msg(struct cap_msg_args *arg)
Sage Weila8599bd2009-10-06 11:31:12 -07001076{
1077 struct ceph_mds_caps *fc;
1078 struct ceph_msg *msg;
Yan, Zhenge20d2582014-11-14 22:39:13 +08001079 void *p;
1080 size_t extra_len;
Jeff Layton43b29672016-11-10 07:42:05 -05001081 struct timespec zerotime = {0};
Jeff Layton92475f02017-04-13 11:07:04 -04001082 struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc;
Sage Weila8599bd2009-10-06 11:31:12 -07001083
1084 dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s"
Yan, Zhenga2971c82015-06-10 11:09:32 +08001085 " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu"
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001086 " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op),
1087 arg->cid, arg->ino, ceph_cap_string(arg->caps),
1088 ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty),
1089 arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid,
1090 arg->mseq, arg->follows, arg->size, arg->max_size,
1091 arg->xattr_version,
1092 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001093
Yan, Zhenga2971c82015-06-10 11:09:32 +08001094 /* flock buffer size + inline version + inline data size +
1095 * osd_epoch_barrier + oldest_flush_tid */
Jeff Layton43b29672016-11-10 07:42:05 -05001096 extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4;
Yan, Zhenge20d2582014-11-14 22:39:13 +08001097 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len,
1098 GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07001099 if (!msg)
1100 return -ENOMEM;
Sage Weila8599bd2009-10-06 11:31:12 -07001101
Jeff Layton43b29672016-11-10 07:42:05 -05001102 msg->hdr.version = cpu_to_le16(10);
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001103 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07001104
Sage Weil6df058c2009-12-22 11:24:33 -08001105 fc = msg->front.iov_base;
Sage Weila8599bd2009-10-06 11:31:12 -07001106 memset(fc, 0, sizeof(*fc));
1107
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001108 fc->cap_id = cpu_to_le64(arg->cid);
1109 fc->op = cpu_to_le32(arg->op);
1110 fc->seq = cpu_to_le32(arg->seq);
1111 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1112 fc->migrate_seq = cpu_to_le32(arg->mseq);
1113 fc->caps = cpu_to_le32(arg->caps);
1114 fc->wanted = cpu_to_le32(arg->wanted);
1115 fc->dirty = cpu_to_le32(arg->dirty);
1116 fc->ino = cpu_to_le64(arg->ino);
1117 fc->snap_follows = cpu_to_le64(arg->follows);
Sage Weila8599bd2009-10-06 11:31:12 -07001118
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001119 fc->size = cpu_to_le64(arg->size);
1120 fc->max_size = cpu_to_le64(arg->max_size);
1121 ceph_encode_timespec(&fc->mtime, &arg->mtime);
1122 ceph_encode_timespec(&fc->atime, &arg->atime);
1123 ceph_encode_timespec(&fc->ctime, &arg->ctime);
1124 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07001125
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001126 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1127 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1128 fc->mode = cpu_to_le32(arg->mode);
Sage Weila8599bd2009-10-06 11:31:12 -07001129
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001130 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1131 if (arg->xattr_buf) {
1132 msg->middle = ceph_buffer_get(arg->xattr_buf);
1133 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1134 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
Jeff Layton96700792016-11-10 07:42:02 -05001135 }
1136
Yan, Zhenge20d2582014-11-14 22:39:13 +08001137 p = fc + 1;
Jeff Layton43b29672016-11-10 07:42:05 -05001138 /* flock buffer size (version 2) */
Yan, Zhenge20d2582014-11-14 22:39:13 +08001139 ceph_encode_32(&p, 0);
Jeff Layton43b29672016-11-10 07:42:05 -05001140 /* inline version (version 4) */
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001141 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
Yan, Zhenge20d2582014-11-14 22:39:13 +08001142 /* inline data size */
1143 ceph_encode_32(&p, 0);
Jeff Layton92475f02017-04-13 11:07:04 -04001144 /*
1145 * osd_epoch_barrier (version 5)
1146 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1147 * case it was recently changed
1148 */
1149 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
Jeff Layton43b29672016-11-10 07:42:05 -05001150 /* oldest_flush_tid (version 6) */
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001151 ceph_encode_64(&p, arg->oldest_flush_tid);
Yan, Zhenge20d2582014-11-14 22:39:13 +08001152
Jeff Layton43b29672016-11-10 07:42:05 -05001153 /*
1154 * caller_uid/caller_gid (version 7)
1155 *
1156 * Currently, we don't properly track which caller dirtied the caps
1157 * last, and force a flush of them when there is a conflict. For now,
1158 * just set this to 0:0, to emulate how the MDS has worked up to now.
1159 */
1160 ceph_encode_32(&p, 0);
1161 ceph_encode_32(&p, 0);
1162
1163 /* pool namespace (version 8) (mds always ignores this) */
1164 ceph_encode_32(&p, 0);
1165
1166 /*
1167 * btime and change_attr (version 9)
1168 *
1169 * We just zero these out for now, as the MDS ignores them unless
1170 * the requisite feature flags are set (which we don't do yet).
1171 */
1172 ceph_encode_timespec(p, &zerotime);
1173 p += sizeof(struct ceph_timespec);
1174 ceph_encode_64(&p, 0);
1175
1176 /* Advisory flags (version 10) */
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001177 ceph_encode_32(&p, arg->flags);
Jeff Layton43b29672016-11-10 07:42:05 -05001178
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001179 ceph_con_send(&arg->session->s_con, msg);
Sage Weila8599bd2009-10-06 11:31:12 -07001180 return 0;
1181}
1182
1183/*
Sage Weila6369742010-02-22 13:59:00 -08001184 * Queue cap releases when an inode is dropped from our cache. Since
Sage Weilbe655592011-11-30 09:47:09 -08001185 * inode is about to be destroyed, there is no need for i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001186 */
1187void ceph_queue_caps_release(struct inode *inode)
1188{
1189 struct ceph_inode_info *ci = ceph_inode(inode);
1190 struct rb_node *p;
1191
Sage Weila8599bd2009-10-06 11:31:12 -07001192 p = rb_first(&ci->i_caps);
1193 while (p) {
1194 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
Sage Weila8599bd2009-10-06 11:31:12 -07001195 p = rb_next(p);
Yan, Zhenga096b092013-09-22 10:15:58 +08001196 __ceph_remove_cap(cap, true);
Sage Weila8599bd2009-10-06 11:31:12 -07001197 }
Sage Weila8599bd2009-10-06 11:31:12 -07001198}
1199
1200/*
1201 * Send a cap msg on the given inode. Update our caps state, then
Sage Weilbe655592011-11-30 09:47:09 -08001202 * drop i_ceph_lock and send the message.
Sage Weila8599bd2009-10-06 11:31:12 -07001203 *
1204 * Make note of max_size reported/requested from mds, revoked caps
1205 * that have now been implemented.
1206 *
1207 * Make half-hearted attempt ot to invalidate page cache if we are
1208 * dropping RDCACHE. Note that this will leave behind locked pages
1209 * that we'll then need to deal with elsewhere.
1210 *
1211 * Return non-zero if delayed release, or we experienced an error
1212 * such that the caller should requeue + retry later.
1213 *
Sage Weilbe655592011-11-30 09:47:09 -08001214 * called with i_ceph_lock, then drops it.
Sage Weila8599bd2009-10-06 11:31:12 -07001215 * caller should hold snap_rwsem (read), s_mutex.
1216 */
1217static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001218 int op, bool sync, int used, int want, int retain,
1219 int flushing, u64 flush_tid, u64 oldest_flush_tid)
Sage Weilbe655592011-11-30 09:47:09 -08001220 __releases(cap->ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001221{
1222 struct ceph_inode_info *ci = cap->ci;
1223 struct inode *inode = &ci->vfs_inode;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001224 struct cap_msg_args arg;
Colin Ian Kingbb0581f2017-10-18 12:34:25 +01001225 int held, revoking;
Sage Weila8599bd2009-10-06 11:31:12 -07001226 int wake = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001227 int delayed = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001228 int ret;
1229
Sage Weil68c28322010-02-09 13:41:47 -08001230 held = cap->issued | cap->implemented;
1231 revoking = cap->implemented & ~cap->issued;
1232 retain &= ~revoking;
Sage Weil68c28322010-02-09 13:41:47 -08001233
Sage Weila8599bd2009-10-06 11:31:12 -07001234 dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n",
1235 inode, cap, cap->session,
1236 ceph_cap_string(held), ceph_cap_string(held & retain),
1237 ceph_cap_string(revoking));
1238 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1239
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001240 arg.session = cap->session;
Sage Weila8599bd2009-10-06 11:31:12 -07001241
1242 /* don't release wanted unless we've waited a bit. */
1243 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1244 time_before(jiffies, ci->i_hold_caps_min)) {
1245 dout(" delaying issued %s -> %s, wanted %s -> %s on send\n",
1246 ceph_cap_string(cap->issued),
1247 ceph_cap_string(cap->issued & retain),
1248 ceph_cap_string(cap->mds_wanted),
1249 ceph_cap_string(want));
1250 want |= cap->mds_wanted;
1251 retain |= cap->issued;
1252 delayed = 1;
1253 }
1254 ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH);
Yan, Zhengeb65b912017-01-12 17:18:00 +08001255 if (want & ~cap->mds_wanted) {
1256 /* user space may open/close single file frequently.
1257 * This avoids droping mds_wanted immediately after
1258 * requesting new mds_wanted.
1259 */
1260 __cap_set_timeouts(mdsc, ci);
1261 }
Sage Weila8599bd2009-10-06 11:31:12 -07001262
1263 cap->issued &= retain; /* drop bits we don't want */
1264 if (cap->implemented & ~cap->issued) {
1265 /*
1266 * Wake up any waiters on wanted -> needed transition.
1267 * This is due to the weird transition from buffered
1268 * to sync IO... we need to flush dirty pages _before_
1269 * allowing sync writes to avoid reordering.
1270 */
1271 wake = 1;
1272 }
1273 cap->implemented &= cap->issued | used;
1274 cap->mds_wanted = want;
1275
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001276 arg.ino = ceph_vino(inode).ino;
1277 arg.cid = cap->cap_id;
1278 arg.follows = flushing ? ci->i_head_snapc->seq : 0;
1279 arg.flush_tid = flush_tid;
1280 arg.oldest_flush_tid = oldest_flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07001281
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001282 arg.size = inode->i_size;
1283 ci->i_reported_size = arg.size;
1284 arg.max_size = ci->i_wanted_max_size;
1285 ci->i_requested_max_size = arg.max_size;
Sage Weila8599bd2009-10-06 11:31:12 -07001286
Sage Weil082afec2010-08-22 15:16:41 -07001287 if (flushing & CEPH_CAP_XATTR_EXCL) {
Sage Weila8599bd2009-10-06 11:31:12 -07001288 __ceph_build_xattrs_blob(ci);
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001289 arg.xattr_version = ci->i_xattrs.version;
1290 arg.xattr_buf = ci->i_xattrs.blob;
1291 } else {
1292 arg.xattr_buf = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07001293 }
1294
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001295 arg.mtime = inode->i_mtime;
1296 arg.atime = inode->i_atime;
1297 arg.ctime = inode->i_ctime;
1298
1299 arg.op = op;
1300 arg.caps = cap->implemented;
1301 arg.wanted = want;
1302 arg.dirty = flushing;
1303
1304 arg.seq = cap->seq;
1305 arg.issue_seq = cap->issue_seq;
1306 arg.mseq = cap->mseq;
1307 arg.time_warp_seq = ci->i_time_warp_seq;
1308
1309 arg.uid = inode->i_uid;
1310 arg.gid = inode->i_gid;
1311 arg.mode = inode->i_mode;
1312
1313 arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
Yan, Zheng95569712017-07-24 17:59:39 +08001314 if (list_empty(&ci->i_cap_snaps))
1315 arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP;
1316 else
1317 arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001318 if (sync)
1319 arg.flags |= CEPH_CLIENT_CAPS_SYNC;
Yan, Zhenge20d2582014-11-14 22:39:13 +08001320
Sage Weilbe655592011-11-30 09:47:09 -08001321 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001322
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001323 ret = send_cap_msg(&arg);
Sage Weila8599bd2009-10-06 11:31:12 -07001324 if (ret < 0) {
1325 dout("error sending cap msg, must requeue %p\n", inode);
1326 delayed = 1;
1327 }
1328
1329 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001330 wake_up_all(&ci->i_cap_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07001331
1332 return delayed;
1333}
1334
Yan, Zheng0e294382016-07-04 18:06:41 +08001335static inline int __send_flush_snap(struct inode *inode,
1336 struct ceph_mds_session *session,
1337 struct ceph_cap_snap *capsnap,
1338 u32 mseq, u64 oldest_flush_tid)
1339{
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001340 struct cap_msg_args arg;
1341
1342 arg.session = session;
1343 arg.ino = ceph_vino(inode).ino;
1344 arg.cid = 0;
1345 arg.follows = capsnap->follows;
1346 arg.flush_tid = capsnap->cap_flush.tid;
1347 arg.oldest_flush_tid = oldest_flush_tid;
1348
1349 arg.size = capsnap->size;
1350 arg.max_size = 0;
1351 arg.xattr_version = capsnap->xattr_version;
1352 arg.xattr_buf = capsnap->xattr_blob;
1353
1354 arg.atime = capsnap->atime;
1355 arg.mtime = capsnap->mtime;
1356 arg.ctime = capsnap->ctime;
1357
1358 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1359 arg.caps = capsnap->issued;
1360 arg.wanted = 0;
1361 arg.dirty = capsnap->dirty;
1362
1363 arg.seq = 0;
1364 arg.issue_seq = 0;
1365 arg.mseq = mseq;
1366 arg.time_warp_seq = capsnap->time_warp_seq;
1367
1368 arg.uid = capsnap->uid;
1369 arg.gid = capsnap->gid;
1370 arg.mode = capsnap->mode;
1371
1372 arg.inline_data = capsnap->inline_data;
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05001373 arg.flags = 0;
Jeff Layton0ff8bfb2016-11-10 07:42:03 -05001374
1375 return send_cap_msg(&arg);
Yan, Zheng0e294382016-07-04 18:06:41 +08001376}
1377
Sage Weila8599bd2009-10-06 11:31:12 -07001378/*
1379 * When a snapshot is taken, clients accumulate dirty metadata on
1380 * inodes with capabilities in ceph_cap_snaps to describe the file
1381 * state at the time the snapshot was taken. This must be flushed
1382 * asynchronously back to the MDS once sync writes complete and dirty
1383 * data is written out.
1384 *
Sage Weilbe655592011-11-30 09:47:09 -08001385 * Called under i_ceph_lock. Takes s_mutex as needed.
Sage Weila8599bd2009-10-06 11:31:12 -07001386 */
Yan, Zhenged9b4302016-07-05 21:08:07 +08001387static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1388 struct ceph_mds_session *session)
Sage Weilbe655592011-11-30 09:47:09 -08001389 __releases(ci->i_ceph_lock)
1390 __acquires(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07001391{
1392 struct inode *inode = &ci->vfs_inode;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001393 struct ceph_mds_client *mdsc = session->s_mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001394 struct ceph_cap_snap *capsnap;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001395 u64 oldest_flush_tid = 0;
1396 u64 first_tid = 1, last_tid = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001397
Yan, Zhenged9b4302016-07-05 21:08:07 +08001398 dout("__flush_snaps %p session %p\n", inode, session);
Sage Weila8599bd2009-10-06 11:31:12 -07001399
Sage Weila8599bd2009-10-06 11:31:12 -07001400 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
Sage Weila8599bd2009-10-06 11:31:12 -07001401 /*
1402 * we need to wait for sync writes to complete and for dirty
1403 * pages to be written out.
1404 */
1405 if (capsnap->dirty_pages || capsnap->writing)
Sage Weilcfc0bf62010-09-14 15:50:59 -07001406 break;
Sage Weila8599bd2009-10-06 11:31:12 -07001407
Yan, Zheng86056092015-05-01 16:57:16 +08001408 /* should be removed by ceph_try_drop_cap_snap() */
1409 BUG_ON(!capsnap->need_flush);
Sage Weil819ccbf2010-04-01 09:33:46 -07001410
Sage Weile8351242010-09-17 08:03:08 -07001411 /* only flush each capsnap once */
Yan, Zheng0e294382016-07-04 18:06:41 +08001412 if (capsnap->cap_flush.tid > 0) {
Yan, Zhenged9b4302016-07-05 21:08:07 +08001413 dout(" already flushed %p, skipping\n", capsnap);
Sage Weile8351242010-09-17 08:03:08 -07001414 continue;
1415 }
1416
Yan, Zheng553adfd2015-06-09 15:48:57 +08001417 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng0e294382016-07-04 18:06:41 +08001418 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1419 list_add_tail(&capsnap->cap_flush.g_list,
1420 &mdsc->cap_flush_list);
Yan, Zhenged9b4302016-07-05 21:08:07 +08001421 if (oldest_flush_tid == 0)
1422 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
Yan, Zheng0e294382016-07-04 18:06:41 +08001423 if (list_empty(&ci->i_flushing_item)) {
1424 list_add_tail(&ci->i_flushing_item,
1425 &session->s_cap_flushing);
1426 }
Yan, Zheng553adfd2015-06-09 15:48:57 +08001427 spin_unlock(&mdsc->cap_dirty_lock);
1428
Yan, Zheng0e294382016-07-04 18:06:41 +08001429 list_add_tail(&capsnap->cap_flush.i_list,
1430 &ci->i_cap_flush_list);
1431
Yan, Zhenged9b4302016-07-05 21:08:07 +08001432 if (first_tid == 1)
1433 first_tid = capsnap->cap_flush.tid;
1434 last_tid = capsnap->cap_flush.tid;
1435 }
1436
1437 ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS;
1438
1439 while (first_tid <= last_tid) {
1440 struct ceph_cap *cap = ci->i_auth_cap;
1441 struct ceph_cap_flush *cf;
1442 int ret;
1443
1444 if (!(cap && cap->session == session)) {
1445 dout("__flush_snaps %p auth cap %p not mds%d, "
1446 "stop\n", inode, cap, session->s_mds);
1447 break;
1448 }
1449
1450 ret = -ENOENT;
1451 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
1452 if (cf->tid >= first_tid) {
1453 ret = 0;
1454 break;
1455 }
1456 }
1457 if (ret < 0)
1458 break;
1459
1460 first_tid = cf->tid + 1;
1461
1462 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
Elena Reshetova805692d2017-03-03 11:15:07 +02001463 refcount_inc(&capsnap->nref);
Sage Weilbe655592011-11-30 09:47:09 -08001464 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001465
Yan, Zhenged9b4302016-07-05 21:08:07 +08001466 dout("__flush_snaps %p capsnap %p tid %llu %s\n",
1467 inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty));
Sage Weila8599bd2009-10-06 11:31:12 -07001468
Yan, Zhenged9b4302016-07-05 21:08:07 +08001469 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1470 oldest_flush_tid);
1471 if (ret < 0) {
1472 pr_err("__flush_snaps: error sending cap flushsnap, "
1473 "ino (%llx.%llx) tid %llu follows %llu\n",
1474 ceph_vinop(inode), cf->tid, capsnap->follows);
1475 }
1476
Sage Weila8599bd2009-10-06 11:31:12 -07001477 ceph_put_cap_snap(capsnap);
Sage Weilbe655592011-11-30 09:47:09 -08001478 spin_lock(&ci->i_ceph_lock);
Yan, Zhenged9b4302016-07-05 21:08:07 +08001479 }
1480}
1481
1482void ceph_flush_snaps(struct ceph_inode_info *ci,
1483 struct ceph_mds_session **psession)
1484{
1485 struct inode *inode = &ci->vfs_inode;
1486 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Yan, Zhenge4d2b162016-08-04 08:43:33 +08001487 struct ceph_mds_session *session = NULL;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001488 int mds;
Yan, Zhenge4d2b162016-08-04 08:43:33 +08001489
Yan, Zhenged9b4302016-07-05 21:08:07 +08001490 dout("ceph_flush_snaps %p\n", inode);
Yan, Zhenge4d2b162016-08-04 08:43:33 +08001491 if (psession)
1492 session = *psession;
Yan, Zhenged9b4302016-07-05 21:08:07 +08001493retry:
1494 spin_lock(&ci->i_ceph_lock);
1495 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1496 dout(" no capsnap needs flush, doing nothing\n");
1497 goto out;
1498 }
1499 if (!ci->i_auth_cap) {
1500 dout(" no auth cap (migrating?), doing nothing\n");
1501 goto out;
1502 }
1503
1504 mds = ci->i_auth_cap->session->s_mds;
1505 if (session && session->s_mds != mds) {
1506 dout(" oops, wrong session %p mutex\n", session);
1507 mutex_unlock(&session->s_mutex);
1508 ceph_put_mds_session(session);
1509 session = NULL;
1510 }
1511 if (!session) {
1512 spin_unlock(&ci->i_ceph_lock);
1513 mutex_lock(&mdsc->mutex);
1514 session = __ceph_lookup_mds_session(mdsc, mds);
1515 mutex_unlock(&mdsc->mutex);
1516 if (session) {
1517 dout(" inverting session/ino locks on %p\n", session);
1518 mutex_lock(&session->s_mutex);
1519 }
Sage Weila8599bd2009-10-06 11:31:12 -07001520 goto retry;
1521 }
1522
Yan, Zheng24d063a2017-08-15 11:37:32 +08001523 // make sure flushsnap messages are sent in proper order.
1524 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
1525 __kick_flushing_caps(mdsc, session, ci, 0);
1526 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1527 }
1528
Yan, Zhenged9b4302016-07-05 21:08:07 +08001529 __ceph_flush_snaps(ci, session);
1530out:
1531 spin_unlock(&ci->i_ceph_lock);
1532
1533 if (psession) {
1534 *psession = session;
Yan, Zhengc858a072017-08-28 15:02:42 +08001535 } else if (session) {
Yan, Zhenged9b4302016-07-05 21:08:07 +08001536 mutex_unlock(&session->s_mutex);
1537 ceph_put_mds_session(session);
1538 }
Sage Weila8599bd2009-10-06 11:31:12 -07001539 /* we flushed them all; remove this inode from the queue */
1540 spin_lock(&mdsc->snap_flush_lock);
1541 list_del_init(&ci->i_snap_flush_item);
1542 spin_unlock(&mdsc->snap_flush_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001543}
1544
1545/*
Sage Weilfca65b42011-05-04 11:33:47 -07001546 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1547 * Caller is then responsible for calling __mark_inode_dirty with the
1548 * returned flags value.
Sage Weil76e3b392009-10-15 18:13:53 -07001549 */
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001550int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1551 struct ceph_cap_flush **pcf)
Sage Weil76e3b392009-10-15 18:13:53 -07001552{
Cheng Renquan640ef792010-03-26 17:40:33 +08001553 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001554 ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc;
Sage Weil76e3b392009-10-15 18:13:53 -07001555 struct inode *inode = &ci->vfs_inode;
1556 int was = ci->i_dirty_caps;
1557 int dirty = 0;
1558
Yan, Zheng571ade32015-03-24 11:36:08 +08001559 if (!ci->i_auth_cap) {
1560 pr_warn("__mark_dirty_caps %p %llx mask %s, "
1561 "but no auth cap (session was closed?)\n",
1562 inode, ceph_ino(inode), ceph_cap_string(mask));
1563 return 0;
1564 }
1565
Sage Weil76e3b392009-10-15 18:13:53 -07001566 dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode,
1567 ceph_cap_string(mask), ceph_cap_string(was),
1568 ceph_cap_string(was | mask));
1569 ci->i_dirty_caps |= mask;
1570 if (was == 0) {
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001571 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1572 swap(ci->i_prealloc_cap_flush, *pcf);
1573
Yan, Zheng604d1b02015-05-01 17:49:16 +08001574 if (!ci->i_head_snapc) {
1575 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
Sage Weil7d8cb262010-08-24 08:44:16 -07001576 ci->i_head_snapc = ceph_get_snap_context(
1577 ci->i_snap_realm->cached_context);
Yan, Zheng604d1b02015-05-01 17:49:16 +08001578 }
Yan, Zheng06852352012-11-19 10:49:07 +08001579 dout(" inode %p now dirty snapc %p auth cap %p\n",
1580 &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap);
Sage Weil76e3b392009-10-15 18:13:53 -07001581 BUG_ON(!list_empty(&ci->i_dirty_item));
1582 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng11df2df2013-11-24 14:44:38 +08001583 list_add(&ci->i_dirty_item, &mdsc->cap_dirty);
Sage Weil76e3b392009-10-15 18:13:53 -07001584 spin_unlock(&mdsc->cap_dirty_lock);
1585 if (ci->i_flushing_caps == 0) {
Sage Weil3772d262011-05-03 09:28:08 -07001586 ihold(inode);
Sage Weil76e3b392009-10-15 18:13:53 -07001587 dirty |= I_DIRTY_SYNC;
1588 }
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001589 } else {
1590 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
Sage Weil76e3b392009-10-15 18:13:53 -07001591 }
1592 BUG_ON(list_empty(&ci->i_dirty_item));
1593 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1594 (mask & CEPH_CAP_FILE_BUFFER))
1595 dirty |= I_DIRTY_DATASYNC;
Sage Weil76e3b392009-10-15 18:13:53 -07001596 __cap_delay_requeue(mdsc, ci);
Sage Weilfca65b42011-05-04 11:33:47 -07001597 return dirty;
Sage Weil76e3b392009-10-15 18:13:53 -07001598}
1599
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001600struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1601{
1602 return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1603}
1604
1605void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1606{
1607 if (cf)
1608 kmem_cache_free(ceph_cap_flush_cachep, cf);
1609}
1610
Yan, Zhenga2971c82015-06-10 11:09:32 +08001611static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1612{
Yan, Zhenge4500b52016-07-06 11:12:56 +08001613 if (!list_empty(&mdsc->cap_flush_list)) {
Yan, Zhenga2971c82015-06-10 11:09:32 +08001614 struct ceph_cap_flush *cf =
Yan, Zhenge4500b52016-07-06 11:12:56 +08001615 list_first_entry(&mdsc->cap_flush_list,
1616 struct ceph_cap_flush, g_list);
Yan, Zhenga2971c82015-06-10 11:09:32 +08001617 return cf->tid;
1618 }
1619 return 0;
1620}
1621
Sage Weil76e3b392009-10-15 18:13:53 -07001622/*
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001623 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1624 * Return true if caller needs to wake up flush waiters.
1625 */
1626static bool __finish_cap_flush(struct ceph_mds_client *mdsc,
1627 struct ceph_inode_info *ci,
1628 struct ceph_cap_flush *cf)
1629{
1630 struct ceph_cap_flush *prev;
1631 bool wake = cf->wake;
1632 if (mdsc) {
1633 /* are there older pending cap flushes? */
1634 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1635 prev = list_prev_entry(cf, g_list);
1636 prev->wake = true;
1637 wake = false;
1638 }
1639 list_del(&cf->g_list);
1640 } else if (ci) {
1641 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1642 prev = list_prev_entry(cf, i_list);
1643 prev->wake = true;
1644 wake = false;
1645 }
1646 list_del(&cf->i_list);
1647 } else {
1648 BUG_ON(1);
1649 }
1650 return wake;
1651}
1652
1653/*
Sage Weila8599bd2009-10-06 11:31:12 -07001654 * Add dirty inode to the flushing list. Assigned a seq number so we
1655 * can wait for caps to flush without starving.
Sage Weilcdc35f92009-10-14 14:24:19 -07001656 *
Sage Weilbe655592011-11-30 09:47:09 -08001657 * Called under i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07001658 */
Sage Weilcdc35f92009-10-14 14:24:19 -07001659static int __mark_caps_flushing(struct inode *inode,
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001660 struct ceph_mds_session *session, bool wake,
Yan, Zhenga2971c82015-06-10 11:09:32 +08001661 u64 *flush_tid, u64 *oldest_flush_tid)
Sage Weila8599bd2009-10-06 11:31:12 -07001662{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001663 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001664 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001665 struct ceph_cap_flush *cf = NULL;
Sage Weilcdc35f92009-10-14 14:24:19 -07001666 int flushing;
Sage Weil50b885b2009-12-01 14:12:07 -08001667
Sage Weilcdc35f92009-10-14 14:24:19 -07001668 BUG_ON(ci->i_dirty_caps == 0);
Sage Weila8599bd2009-10-06 11:31:12 -07001669 BUG_ON(list_empty(&ci->i_dirty_item));
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001670 BUG_ON(!ci->i_prealloc_cap_flush);
Sage Weilcdc35f92009-10-14 14:24:19 -07001671
1672 flushing = ci->i_dirty_caps;
1673 dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n",
1674 ceph_cap_string(flushing),
1675 ceph_cap_string(ci->i_flushing_caps),
1676 ceph_cap_string(ci->i_flushing_caps | flushing));
1677 ci->i_flushing_caps |= flushing;
1678 ci->i_dirty_caps = 0;
Sage Weilafcdaea2009-10-14 14:27:38 -07001679 dout(" inode %p now !dirty\n", inode);
Sage Weilcdc35f92009-10-14 14:24:19 -07001680
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08001681 swap(cf, ci->i_prealloc_cap_flush);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001682 cf->caps = flushing;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001683 cf->wake = wake;
Yan, Zheng553adfd2015-06-09 15:48:57 +08001684
Sage Weila8599bd2009-10-06 11:31:12 -07001685 spin_lock(&mdsc->cap_dirty_lock);
Sage Weilafcdaea2009-10-14 14:27:38 -07001686 list_del_init(&ci->i_dirty_item);
1687
Yan, Zheng553adfd2015-06-09 15:48:57 +08001688 cf->tid = ++mdsc->last_cap_flush_tid;
Yan, Zhenge4500b52016-07-06 11:12:56 +08001689 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
Yan, Zhenga2971c82015-06-10 11:09:32 +08001690 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001691
Sage Weila8599bd2009-10-06 11:31:12 -07001692 if (list_empty(&ci->i_flushing_item)) {
1693 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1694 mdsc->num_cap_flushing++;
Sage Weila8599bd2009-10-06 11:31:12 -07001695 }
1696 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weilcdc35f92009-10-14 14:24:19 -07001697
Yan, Zhenge4500b52016-07-06 11:12:56 +08001698 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001699
1700 *flush_tid = cf->tid;
Sage Weilcdc35f92009-10-14 14:24:19 -07001701 return flushing;
Sage Weila8599bd2009-10-06 11:31:12 -07001702}
1703
1704/*
Sage Weil5ecad6f2010-02-17 10:43:37 -08001705 * try to invalidate mapping pages without blocking.
1706 */
Sage Weil5ecad6f2010-02-17 10:43:37 -08001707static int try_nonblocking_invalidate(struct inode *inode)
1708{
1709 struct ceph_inode_info *ci = ceph_inode(inode);
1710 u32 invalidating_gen = ci->i_rdcache_gen;
1711
Sage Weilbe655592011-11-30 09:47:09 -08001712 spin_unlock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001713 invalidate_mapping_pages(&inode->i_data, 0, -1);
Sage Weilbe655592011-11-30 09:47:09 -08001714 spin_lock(&ci->i_ceph_lock);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001715
Sage Weil18a38192010-09-17 10:46:44 -07001716 if (inode->i_data.nrpages == 0 &&
Sage Weil5ecad6f2010-02-17 10:43:37 -08001717 invalidating_gen == ci->i_rdcache_gen) {
1718 /* success. */
1719 dout("try_nonblocking_invalidate %p success\n", inode);
Sage Weilcd045cb2010-11-04 11:05:05 -07001720 /* save any racing async invalidate some trouble */
1721 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
Sage Weil5ecad6f2010-02-17 10:43:37 -08001722 return 0;
1723 }
1724 dout("try_nonblocking_invalidate %p failed\n", inode);
1725 return -1;
1726}
1727
Yan, Zhengefb0ca72017-05-22 12:03:32 +08001728bool __ceph_should_report_size(struct ceph_inode_info *ci)
1729{
1730 loff_t size = ci->vfs_inode.i_size;
1731 /* mds will adjust max size according to the reported size */
1732 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1733 return false;
1734 if (size >= ci->i_max_size)
1735 return true;
1736 /* half of previous max_size increment has been used */
1737 if (ci->i_max_size > ci->i_reported_size &&
1738 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1739 return true;
1740 return false;
1741}
1742
Sage Weil5ecad6f2010-02-17 10:43:37 -08001743/*
Sage Weila8599bd2009-10-06 11:31:12 -07001744 * Swiss army knife function to examine currently used and wanted
1745 * versus held caps. Release, flush, ack revoked caps to mds as
1746 * appropriate.
1747 *
1748 * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay
1749 * cap release further.
1750 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
1751 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
1752 * further delay.
1753 */
1754void ceph_check_caps(struct ceph_inode_info *ci, int flags,
1755 struct ceph_mds_session *session)
1756{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001757 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1758 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07001759 struct inode *inode = &ci->vfs_inode;
1760 struct ceph_cap *cap;
Yan, Zhenga2971c82015-06-10 11:09:32 +08001761 u64 flush_tid, oldest_flush_tid;
Yan, Zheng395c3122013-01-04 14:37:57 +08001762 int file_wanted, used, cap_used;
Sage Weila8599bd2009-10-06 11:31:12 -07001763 int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */
Sage Weilcbd03632010-02-09 13:41:18 -08001764 int issued, implemented, want, retain, revoking, flushing = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07001765 int mds = -1; /* keep track of how far we've gone through i_caps list
1766 to avoid an infinite loop on retry */
1767 struct rb_node *p;
Yan, Zheng0f439c72018-01-08 14:44:10 +08001768 int delayed = 0, sent = 0;
1769 bool no_delay = flags & CHECK_CAPS_NODELAY;
Yan, Zheng36094042016-07-06 15:37:42 +08001770 bool queue_invalidate = false;
Yan, Zheng36094042016-07-06 15:37:42 +08001771 bool tried_invalidate = false;
Sage Weila8599bd2009-10-06 11:31:12 -07001772
1773 /* if we are unmounting, flush any unused caps immediately. */
1774 if (mdsc->stopping)
Yan, Zheng0f439c72018-01-08 14:44:10 +08001775 no_delay = true;
Sage Weila8599bd2009-10-06 11:31:12 -07001776
Sage Weilbe655592011-11-30 09:47:09 -08001777 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001778
1779 if (ci->i_ceph_flags & CEPH_I_FLUSH)
1780 flags |= CHECK_CAPS_FLUSH;
1781
Yan, Zheng0f439c72018-01-08 14:44:10 +08001782 if (!(flags & CHECK_CAPS_AUTHONLY) ||
1783 (ci->i_auth_cap && __ceph_is_single_caps(ci)))
1784 __cap_delay_cancel(mdsc, ci);
1785
Sage Weila8599bd2009-10-06 11:31:12 -07001786 goto retry_locked;
1787retry:
Sage Weilbe655592011-11-30 09:47:09 -08001788 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001789retry_locked:
1790 file_wanted = __ceph_caps_file_wanted(ci);
1791 used = __ceph_caps_used(ci);
Sage Weilcbd03632010-02-09 13:41:18 -08001792 issued = __ceph_caps_issued(ci, &implemented);
1793 revoking = implemented & ~issued;
Sage Weila8599bd2009-10-06 11:31:12 -07001794
Yan, Zheng41445992015-05-25 17:36:42 +08001795 want = file_wanted;
1796 retain = file_wanted | used | CEPH_CAP_PIN;
Sage Weila8599bd2009-10-06 11:31:12 -07001797 if (!mdsc->stopping && inode->i_nlink > 0) {
Yan, Zheng41445992015-05-25 17:36:42 +08001798 if (file_wanted) {
Sage Weila8599bd2009-10-06 11:31:12 -07001799 retain |= CEPH_CAP_ANY; /* be greedy */
Yan, Zheng32ec4392015-03-26 19:06:00 +08001800 } else if (S_ISDIR(inode->i_mode) &&
1801 (issued & CEPH_CAP_FILE_SHARED) &&
1802 __ceph_dir_is_complete(ci)) {
1803 /*
1804 * If a directory is complete, we want to keep
1805 * the exclusive cap. So that MDS does not end up
1806 * revoking the shared cap on every create/unlink
1807 * operation.
1808 */
1809 want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1810 retain |= want;
Sage Weila8599bd2009-10-06 11:31:12 -07001811 } else {
Yan, Zheng32ec4392015-03-26 19:06:00 +08001812
Sage Weila8599bd2009-10-06 11:31:12 -07001813 retain |= CEPH_CAP_ANY_SHARED;
1814 /*
1815 * keep RD only if we didn't have the file open RW,
1816 * because then the mds would revoke it anyway to
1817 * journal max_size=0.
1818 */
1819 if (ci->i_max_size == 0)
1820 retain |= CEPH_CAP_ANY_RD;
1821 }
1822 }
1823
1824 dout("check_caps %p file_want %s used %s dirty %s flushing %s"
Sage Weilcbd03632010-02-09 13:41:18 -08001825 " issued %s revoking %s retain %s %s%s%s\n", inode,
Sage Weila8599bd2009-10-06 11:31:12 -07001826 ceph_cap_string(file_wanted),
1827 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
1828 ceph_cap_string(ci->i_flushing_caps),
Sage Weilcbd03632010-02-09 13:41:18 -08001829 ceph_cap_string(issued), ceph_cap_string(revoking),
Sage Weila8599bd2009-10-06 11:31:12 -07001830 ceph_cap_string(retain),
1831 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
1832 (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "",
1833 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "");
1834
1835 /*
1836 * If we no longer need to hold onto old our caps, and we may
1837 * have cached pages, but don't want them, then try to invalidate.
1838 * If we fail, it's because pages are locked.... try again later.
1839 */
Yan, Zheng0f439c72018-01-08 14:44:10 +08001840 if ((!no_delay || mdsc->stopping) &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001841 !S_ISDIR(inode->i_mode) && /* ignore readdir cache */
Yan, Zheng9abd4db2016-05-18 20:58:26 +08001842 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001843 inode->i_data.nrpages && /* have cached pages */
Yan, Zheng5e804ac2015-10-26 16:08:43 +08001844 (revoking & (CEPH_CAP_FILE_CACHE|
1845 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
Sage Weila8599bd2009-10-06 11:31:12 -07001846 !tried_invalidate) {
Sage Weila8599bd2009-10-06 11:31:12 -07001847 dout("check_caps trying to invalidate on %p\n", inode);
Sage Weil5ecad6f2010-02-17 10:43:37 -08001848 if (try_nonblocking_invalidate(inode) < 0) {
Yan, Zhengee612d92018-01-08 14:36:25 +08001849 dout("check_caps queuing invalidate\n");
1850 queue_invalidate = true;
1851 ci->i_rdcache_revoking = ci->i_rdcache_gen;
Sage Weila8599bd2009-10-06 11:31:12 -07001852 }
Yan, Zheng36094042016-07-06 15:37:42 +08001853 tried_invalidate = true;
Sage Weila8599bd2009-10-06 11:31:12 -07001854 goto retry_locked;
1855 }
1856
Sage Weila8599bd2009-10-06 11:31:12 -07001857 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1858 cap = rb_entry(p, struct ceph_cap, ci_node);
Sage Weila8599bd2009-10-06 11:31:12 -07001859
1860 /* avoid looping forever */
1861 if (mds >= cap->mds ||
1862 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
1863 continue;
1864
1865 /* NOTE: no side-effects allowed, until we take s_mutex */
1866
Yan, Zheng395c3122013-01-04 14:37:57 +08001867 cap_used = used;
1868 if (ci->i_auth_cap && cap != ci->i_auth_cap)
1869 cap_used &= ~ci->i_auth_cap->issued;
1870
Sage Weila8599bd2009-10-06 11:31:12 -07001871 revoking = cap->implemented & ~cap->issued;
Yan, Zheng395c3122013-01-04 14:37:57 +08001872 dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n",
Yan, Zheng9abd4db2016-05-18 20:58:26 +08001873 cap->mds, cap, ceph_cap_string(cap_used),
1874 ceph_cap_string(cap->issued),
Sage Weil088b3f52011-01-18 08:56:01 -08001875 ceph_cap_string(cap->implemented),
1876 ceph_cap_string(revoking));
Sage Weila8599bd2009-10-06 11:31:12 -07001877
1878 if (cap == ci->i_auth_cap &&
1879 (cap->issued & CEPH_CAP_FILE_WR)) {
1880 /* request larger max_size from MDS? */
1881 if (ci->i_wanted_max_size > ci->i_max_size &&
1882 ci->i_wanted_max_size > ci->i_requested_max_size) {
1883 dout("requesting new max_size\n");
1884 goto ack;
1885 }
1886
1887 /* approaching file_max? */
Yan, Zhengefb0ca72017-05-22 12:03:32 +08001888 if (__ceph_should_report_size(ci)) {
Sage Weila8599bd2009-10-06 11:31:12 -07001889 dout("i_size approaching max_size\n");
1890 goto ack;
1891 }
1892 }
1893 /* flush anything dirty? */
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08001894 if (cap == ci->i_auth_cap) {
1895 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
1896 dout("flushing dirty caps\n");
1897 goto ack;
1898 }
1899 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
1900 dout("flushing snap caps\n");
1901 goto ack;
1902 }
Sage Weila8599bd2009-10-06 11:31:12 -07001903 }
1904
1905 /* completed revocation? going down and there are no caps? */
Yan, Zheng395c3122013-01-04 14:37:57 +08001906 if (revoking && (revoking & cap_used) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07001907 dout("completed revocation of %s\n",
1908 ceph_cap_string(cap->implemented & ~cap->issued));
1909 goto ack;
1910 }
1911
1912 /* want more caps from mds? */
1913 if (want & ~(cap->mds_wanted | cap->issued))
1914 goto ack;
1915
1916 /* things we might delay */
1917 if ((cap->issued & ~retain) == 0 &&
1918 cap->mds_wanted == want)
1919 continue; /* nope, all good */
1920
Yan, Zheng0f439c72018-01-08 14:44:10 +08001921 if (no_delay)
Sage Weila8599bd2009-10-06 11:31:12 -07001922 goto ack;
1923
1924 /* delay? */
1925 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 &&
1926 time_before(jiffies, ci->i_hold_caps_max)) {
1927 dout(" delaying issued %s -> %s, wanted %s -> %s\n",
1928 ceph_cap_string(cap->issued),
1929 ceph_cap_string(cap->issued & retain),
1930 ceph_cap_string(cap->mds_wanted),
1931 ceph_cap_string(want));
1932 delayed++;
1933 continue;
1934 }
1935
1936ack:
Sage Weile9964c12010-03-01 15:16:56 -08001937 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
1938 dout(" skipping %p I_NOFLUSH set\n", inode);
1939 continue;
1940 }
1941
Sage Weila8599bd2009-10-06 11:31:12 -07001942 if (session && session != cap->session) {
1943 dout("oops, wrong session %p mutex\n", session);
1944 mutex_unlock(&session->s_mutex);
1945 session = NULL;
1946 }
1947 if (!session) {
1948 session = cap->session;
1949 if (mutex_trylock(&session->s_mutex) == 0) {
1950 dout("inverting session/ino locks on %p\n",
1951 session);
Sage Weilbe655592011-11-30 09:47:09 -08001952 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001953 if (took_snap_rwsem) {
1954 up_read(&mdsc->snap_rwsem);
1955 took_snap_rwsem = 0;
1956 }
1957 mutex_lock(&session->s_mutex);
1958 goto retry;
1959 }
1960 }
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08001961
1962 /* kick flushing and flush snaps before sending normal
1963 * cap message */
1964 if (cap == ci->i_auth_cap &&
1965 (ci->i_ceph_flags &
1966 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
1967 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
Yan, Zheng24d063a2017-08-15 11:37:32 +08001968 __kick_flushing_caps(mdsc, session, ci, 0);
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08001969 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
1970 }
Yan, Zhenged9b4302016-07-05 21:08:07 +08001971 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
1972 __ceph_flush_snaps(ci, session);
1973
Yan, Zheng7bc00fd2016-07-07 18:34:45 +08001974 goto retry_locked;
1975 }
1976
Sage Weila8599bd2009-10-06 11:31:12 -07001977 /* take snap_rwsem after session mutex */
1978 if (!took_snap_rwsem) {
1979 if (down_read_trylock(&mdsc->snap_rwsem) == 0) {
1980 dout("inverting snap/in locks on %p\n",
1981 inode);
Sage Weilbe655592011-11-30 09:47:09 -08001982 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07001983 down_read(&mdsc->snap_rwsem);
1984 took_snap_rwsem = 1;
1985 goto retry;
1986 }
1987 took_snap_rwsem = 1;
1988 }
1989
Yan, Zheng553adfd2015-06-09 15:48:57 +08001990 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
Yan, Zhengc8799fc2016-07-07 15:22:38 +08001991 flushing = __mark_caps_flushing(inode, session, false,
Yan, Zhenga2971c82015-06-10 11:09:32 +08001992 &flush_tid,
1993 &oldest_flush_tid);
Yan, Zheng553adfd2015-06-09 15:48:57 +08001994 } else {
Sage Weil24be0c42011-01-18 08:48:06 -08001995 flushing = 0;
Yan, Zheng553adfd2015-06-09 15:48:57 +08001996 flush_tid = 0;
Yan, Zhenga2971c82015-06-10 11:09:32 +08001997 spin_lock(&mdsc->cap_dirty_lock);
1998 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1999 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002000 }
Sage Weila8599bd2009-10-06 11:31:12 -07002001
2002 mds = cap->mds; /* remember mds, so we don't repeat */
2003 sent++;
2004
Sage Weilbe655592011-11-30 09:47:09 -08002005 /* __send_cap drops i_ceph_lock */
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05002006 delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false,
2007 cap_used, want, retain, flushing,
2008 flush_tid, oldest_flush_tid);
Sage Weilbe655592011-11-30 09:47:09 -08002009 goto retry; /* retake i_ceph_lock and restart our cap scan. */
Sage Weila8599bd2009-10-06 11:31:12 -07002010 }
2011
Yan, Zheng0f439c72018-01-08 14:44:10 +08002012 /* Reschedule delayed caps release if we delayed anything */
2013 if (delayed)
Sage Weila8599bd2009-10-06 11:31:12 -07002014 __cap_delay_requeue(mdsc, ci);
2015
Sage Weilbe655592011-11-30 09:47:09 -08002016 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002017
Sage Weilcbd03632010-02-09 13:41:18 -08002018 if (queue_invalidate)
Sage Weil3c6f6b72010-02-09 15:24:44 -08002019 ceph_queue_invalidate(inode);
Sage Weilcbd03632010-02-09 13:41:18 -08002020
Sage Weilcdc2ce02010-03-16 13:39:28 -07002021 if (session)
Sage Weila8599bd2009-10-06 11:31:12 -07002022 mutex_unlock(&session->s_mutex);
2023 if (took_snap_rwsem)
2024 up_read(&mdsc->snap_rwsem);
2025}
2026
2027/*
Sage Weila8599bd2009-10-06 11:31:12 -07002028 * Try to flush dirty caps back to the auth mds.
2029 */
Yan, Zheng553adfd2015-06-09 15:48:57 +08002030static int try_flush_caps(struct inode *inode, u64 *ptid)
Sage Weila8599bd2009-10-06 11:31:12 -07002031{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002032 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002033 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng4fe59782013-10-31 16:44:14 +08002034 struct ceph_mds_session *session = NULL;
Yan, Zheng89b52fe2015-05-27 09:59:48 +08002035 int flushing = 0;
Yan, Zhenga2971c82015-06-10 11:09:32 +08002036 u64 flush_tid = 0, oldest_flush_tid = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002037
2038retry:
Sage Weilbe655592011-11-30 09:47:09 -08002039 spin_lock(&ci->i_ceph_lock);
Sage Weile9964c12010-03-01 15:16:56 -08002040 if (ci->i_ceph_flags & CEPH_I_NOFLUSH) {
Jeff Layton6c2838f2017-10-19 08:52:58 -04002041 spin_unlock(&ci->i_ceph_lock);
Sage Weile9964c12010-03-01 15:16:56 -08002042 dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode);
2043 goto out;
2044 }
Sage Weila8599bd2009-10-06 11:31:12 -07002045 if (ci->i_dirty_caps && ci->i_auth_cap) {
2046 struct ceph_cap *cap = ci->i_auth_cap;
2047 int used = __ceph_caps_used(ci);
2048 int want = __ceph_caps_wanted(ci);
2049 int delayed;
2050
Yan, Zheng4fe59782013-10-31 16:44:14 +08002051 if (!session || session != cap->session) {
Sage Weilbe655592011-11-30 09:47:09 -08002052 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng4fe59782013-10-31 16:44:14 +08002053 if (session)
2054 mutex_unlock(&session->s_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07002055 session = cap->session;
2056 mutex_lock(&session->s_mutex);
2057 goto retry;
2058 }
Jeff Layton6c2838f2017-10-19 08:52:58 -04002059 if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) {
2060 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002061 goto out;
Jeff Layton6c2838f2017-10-19 08:52:58 -04002062 }
Sage Weila8599bd2009-10-06 11:31:12 -07002063
Yan, Zhengc8799fc2016-07-07 15:22:38 +08002064 flushing = __mark_caps_flushing(inode, session, true,
2065 &flush_tid, &oldest_flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002066
Sage Weilbe655592011-11-30 09:47:09 -08002067 /* __send_cap drops i_ceph_lock */
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05002068 delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true,
2069 used, want, (cap->issued | cap->implemented),
2070 flushing, flush_tid, oldest_flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002071
Yan, Zheng553adfd2015-06-09 15:48:57 +08002072 if (delayed) {
2073 spin_lock(&ci->i_ceph_lock);
Yan, Zheng89b52fe2015-05-27 09:59:48 +08002074 __cap_delay_requeue(mdsc, ci);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002075 spin_unlock(&ci->i_ceph_lock);
2076 }
2077 } else {
Yan, Zhenge4500b52016-07-06 11:12:56 +08002078 if (!list_empty(&ci->i_cap_flush_list)) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08002079 struct ceph_cap_flush *cf =
Yan, Zhenge4500b52016-07-06 11:12:56 +08002080 list_last_entry(&ci->i_cap_flush_list,
Yan, Zhengc8799fc2016-07-07 15:22:38 +08002081 struct ceph_cap_flush, i_list);
2082 cf->wake = true;
Yan, Zheng553adfd2015-06-09 15:48:57 +08002083 flush_tid = cf->tid;
2084 }
2085 flushing = ci->i_flushing_caps;
2086 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002087 }
2088out:
Yan, Zheng4fe59782013-10-31 16:44:14 +08002089 if (session)
Sage Weila8599bd2009-10-06 11:31:12 -07002090 mutex_unlock(&session->s_mutex);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002091
2092 *ptid = flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002093 return flushing;
2094}
2095
2096/*
2097 * Return true if we've flushed caps through the given flush_tid.
2098 */
Yan, Zheng553adfd2015-06-09 15:48:57 +08002099static int caps_are_flushed(struct inode *inode, u64 flush_tid)
Sage Weila8599bd2009-10-06 11:31:12 -07002100{
2101 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002102 int ret = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002103
Sage Weilbe655592011-11-30 09:47:09 -08002104 spin_lock(&ci->i_ceph_lock);
Yan, Zhenge4500b52016-07-06 11:12:56 +08002105 if (!list_empty(&ci->i_cap_flush_list)) {
2106 struct ceph_cap_flush * cf =
2107 list_first_entry(&ci->i_cap_flush_list,
2108 struct ceph_cap_flush, i_list);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002109 if (cf->tid <= flush_tid)
Sage Weila8599bd2009-10-06 11:31:12 -07002110 ret = 0;
Yan, Zheng89b52fe2015-05-27 09:59:48 +08002111 }
Sage Weilbe655592011-11-30 09:47:09 -08002112 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002113 return ret;
2114}
2115
2116/*
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002117 * wait for any unsafe requests to complete.
Yan, Zhengda819c82015-05-27 11:19:34 +08002118 */
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002119static int unsafe_request_wait(struct inode *inode)
Yan, Zhengda819c82015-05-27 11:19:34 +08002120{
2121 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002122 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2123 int ret, err = 0;
Yan, Zhengda819c82015-05-27 11:19:34 +08002124
2125 spin_lock(&ci->i_unsafe_lock);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002126 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2127 req1 = list_last_entry(&ci->i_unsafe_dirops,
2128 struct ceph_mds_request,
2129 r_unsafe_dir_item);
2130 ceph_mdsc_get_request(req1);
2131 }
2132 if (!list_empty(&ci->i_unsafe_iops)) {
2133 req2 = list_last_entry(&ci->i_unsafe_iops,
2134 struct ceph_mds_request,
2135 r_unsafe_target_item);
2136 ceph_mdsc_get_request(req2);
2137 }
Yan, Zhengda819c82015-05-27 11:19:34 +08002138 spin_unlock(&ci->i_unsafe_lock);
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002139
Jeff Layton4945a082016-11-16 09:45:22 -05002140 dout("unsafe_request_wait %p wait on tid %llu %llu\n",
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002141 inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL);
2142 if (req1) {
2143 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2144 ceph_timeout_jiffies(req1->r_timeout));
2145 if (ret)
2146 err = -EIO;
2147 ceph_mdsc_put_request(req1);
2148 }
2149 if (req2) {
2150 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2151 ceph_timeout_jiffies(req2->r_timeout));
2152 if (ret)
2153 err = -EIO;
2154 ceph_mdsc_put_request(req2);
2155 }
2156 return err;
Yan, Zhengda819c82015-05-27 11:19:34 +08002157}
2158
Josef Bacik02c24a82011-07-16 20:44:56 -04002159int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Sage Weila8599bd2009-10-06 11:31:12 -07002160{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02002161 struct inode *inode = file->f_mapping->host;
Sage Weila8599bd2009-10-06 11:31:12 -07002162 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002163 u64 flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002164 int ret;
2165 int dirty;
2166
2167 dout("fsync %p%s\n", inode, datasync ? " datasync" : "");
Yan, Zheng9a5530c2016-06-15 16:29:18 +08002168
Jeff Laytonb74fcea2017-07-25 10:50:41 -04002169 ret = file_write_and_wait_range(file, start, end);
Sage Weila8599bd2009-10-06 11:31:12 -07002170 if (ret < 0)
Yan, Zhengda819c82015-05-27 11:19:34 +08002171 goto out;
2172
2173 if (datasync)
2174 goto out;
2175
Al Viro59551022016-01-22 15:40:57 -05002176 inode_lock(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002177
Yan, Zheng553adfd2015-06-09 15:48:57 +08002178 dirty = try_flush_caps(inode, &flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002179 dout("fsync dirty caps are %s\n", ceph_cap_string(dirty));
2180
Yan, Zheng68cd5b42015-10-27 18:36:06 +08002181 ret = unsafe_request_wait(inode);
Yan, Zhengda819c82015-05-27 11:19:34 +08002182
Sage Weila8599bd2009-10-06 11:31:12 -07002183 /*
2184 * only wait on non-file metadata writeback (the mds
2185 * can recover size and mtime, so we don't need to
2186 * wait for that)
2187 */
Yan, Zhengda819c82015-05-27 11:19:34 +08002188 if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
Sage Weila8599bd2009-10-06 11:31:12 -07002189 ret = wait_event_interruptible(ci->i_cap_wq,
Yan, Zhengda819c82015-05-27 11:19:34 +08002190 caps_are_flushed(inode, flush_tid));
Sage Weila8599bd2009-10-06 11:31:12 -07002191 }
Al Viro59551022016-01-22 15:40:57 -05002192 inode_unlock(inode);
Yan, Zhengda819c82015-05-27 11:19:34 +08002193out:
2194 dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret);
Sage Weila8599bd2009-10-06 11:31:12 -07002195 return ret;
2196}
2197
2198/*
2199 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2200 * queue inode for flush but don't do so immediately, because we can
2201 * get by with fewer MDS messages if we wait for data writeback to
2202 * complete first.
2203 */
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11002204int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
Sage Weila8599bd2009-10-06 11:31:12 -07002205{
2206 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002207 u64 flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002208 int err = 0;
2209 int dirty;
Stephen Rothwellf1a3d572010-01-18 11:53:08 +11002210 int wait = wbc->sync_mode == WB_SYNC_ALL;
Sage Weila8599bd2009-10-06 11:31:12 -07002211
2212 dout("write_inode %p wait=%d\n", inode, wait);
2213 if (wait) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08002214 dirty = try_flush_caps(inode, &flush_tid);
Sage Weila8599bd2009-10-06 11:31:12 -07002215 if (dirty)
2216 err = wait_event_interruptible(ci->i_cap_wq,
2217 caps_are_flushed(inode, flush_tid));
2218 } else {
Cheng Renquan640ef792010-03-26 17:40:33 +08002219 struct ceph_mds_client *mdsc =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002220 ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002221
Sage Weilbe655592011-11-30 09:47:09 -08002222 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002223 if (__ceph_caps_dirty(ci))
2224 __cap_delay_requeue_front(mdsc, ci);
Sage Weilbe655592011-11-30 09:47:09 -08002225 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002226 }
2227 return err;
2228}
2229
Yan, Zheng0e294382016-07-04 18:06:41 +08002230static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2231 struct ceph_mds_session *session,
2232 struct ceph_inode_info *ci,
2233 u64 oldest_flush_tid)
2234 __releases(ci->i_ceph_lock)
2235 __acquires(ci->i_ceph_lock)
Yan, Zheng553adfd2015-06-09 15:48:57 +08002236{
2237 struct inode *inode = &ci->vfs_inode;
2238 struct ceph_cap *cap;
2239 struct ceph_cap_flush *cf;
Yan, Zheng0e294382016-07-04 18:06:41 +08002240 int ret;
Yan, Zheng553adfd2015-06-09 15:48:57 +08002241 u64 first_tid = 0;
Yan, Zhenga2971c82015-06-10 11:09:32 +08002242
Yan, Zhenge4500b52016-07-06 11:12:56 +08002243 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2244 if (cf->tid < first_tid)
2245 continue;
2246
Yan, Zheng553adfd2015-06-09 15:48:57 +08002247 cap = ci->i_auth_cap;
2248 if (!(cap && cap->session == session)) {
Yan, Zheng0e294382016-07-04 18:06:41 +08002249 pr_err("%p auth cap %p not mds%d ???\n",
2250 inode, cap, session->s_mds);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002251 break;
2252 }
2253
Yan, Zheng553adfd2015-06-09 15:48:57 +08002254 first_tid = cf->tid + 1;
2255
Yan, Zheng0e294382016-07-04 18:06:41 +08002256 if (cf->caps) {
2257 dout("kick_flushing_caps %p cap %p tid %llu %s\n",
2258 inode, cap, cf->tid, ceph_cap_string(cf->caps));
2259 ci->i_ceph_flags |= CEPH_I_NODELAY;
2260 ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH,
Jeff Layton1e4ef0c2016-11-10 07:42:06 -05002261 false, __ceph_caps_used(ci),
Yan, Zheng0e294382016-07-04 18:06:41 +08002262 __ceph_caps_wanted(ci),
2263 cap->issued | cap->implemented,
2264 cf->caps, cf->tid, oldest_flush_tid);
2265 if (ret) {
2266 pr_err("kick_flushing_caps: error sending "
2267 "cap flush, ino (%llx.%llx) "
2268 "tid %llu flushing %s\n",
2269 ceph_vinop(inode), cf->tid,
2270 ceph_cap_string(cf->caps));
2271 }
2272 } else {
2273 struct ceph_cap_snap *capsnap =
2274 container_of(cf, struct ceph_cap_snap,
2275 cap_flush);
2276 dout("kick_flushing_caps %p capsnap %p tid %llu %s\n",
2277 inode, capsnap, cf->tid,
2278 ceph_cap_string(capsnap->dirty));
2279
Elena Reshetova805692d2017-03-03 11:15:07 +02002280 refcount_inc(&capsnap->nref);
Yan, Zheng0e294382016-07-04 18:06:41 +08002281 spin_unlock(&ci->i_ceph_lock);
2282
2283 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2284 oldest_flush_tid);
2285 if (ret < 0) {
2286 pr_err("kick_flushing_caps: error sending "
2287 "cap flushsnap, ino (%llx.%llx) "
2288 "tid %llu follows %llu\n",
2289 ceph_vinop(inode), cf->tid,
2290 capsnap->follows);
2291 }
2292
2293 ceph_put_cap_snap(capsnap);
2294 }
Yan, Zhenge4500b52016-07-06 11:12:56 +08002295
2296 spin_lock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002297 }
Yan, Zheng553adfd2015-06-09 15:48:57 +08002298}
2299
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002300void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2301 struct ceph_mds_session *session)
2302{
2303 struct ceph_inode_info *ci;
2304 struct ceph_cap *cap;
Yan, Zheng0e294382016-07-04 18:06:41 +08002305 u64 oldest_flush_tid;
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002306
2307 dout("early_kick_flushing_caps mds%d\n", session->s_mds);
Yan, Zheng0e294382016-07-04 18:06:41 +08002308
2309 spin_lock(&mdsc->cap_dirty_lock);
2310 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2311 spin_unlock(&mdsc->cap_dirty_lock);
2312
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002313 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2314 spin_lock(&ci->i_ceph_lock);
2315 cap = ci->i_auth_cap;
2316 if (!(cap && cap->session == session)) {
2317 pr_err("%p auth cap %p not mds%d ???\n",
2318 &ci->vfs_inode, cap, session->s_mds);
2319 spin_unlock(&ci->i_ceph_lock);
2320 continue;
2321 }
2322
2323
2324 /*
2325 * if flushing caps were revoked, we re-send the cap flush
2326 * in client reconnect stage. This guarantees MDS * processes
2327 * the cap flush message before issuing the flushing caps to
2328 * other client.
2329 */
2330 if ((cap->issued & ci->i_flushing_caps) !=
2331 ci->i_flushing_caps) {
Yan, Zheng13c2b572016-07-05 16:45:21 +08002332 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
Yan, Zheng0e294382016-07-04 18:06:41 +08002333 __kick_flushing_caps(mdsc, session, ci,
2334 oldest_flush_tid);
Yan, Zheng13c2b572016-07-05 16:45:21 +08002335 } else {
2336 ci->i_ceph_flags |= CEPH_I_KICK_FLUSH;
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002337 }
2338
Yan, Zhenge548e9b2015-06-10 15:17:56 +08002339 spin_unlock(&ci->i_ceph_lock);
2340 }
2341}
2342
Sage Weila8599bd2009-10-06 11:31:12 -07002343void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2344 struct ceph_mds_session *session)
2345{
2346 struct ceph_inode_info *ci;
Yan, Zheng13c2b572016-07-05 16:45:21 +08002347 struct ceph_cap *cap;
Yan, Zheng0e294382016-07-04 18:06:41 +08002348 u64 oldest_flush_tid;
Sage Weila8599bd2009-10-06 11:31:12 -07002349
2350 dout("kick_flushing_caps mds%d\n", session->s_mds);
Yan, Zheng0e294382016-07-04 18:06:41 +08002351
2352 spin_lock(&mdsc->cap_dirty_lock);
2353 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2354 spin_unlock(&mdsc->cap_dirty_lock);
2355
Sage Weila8599bd2009-10-06 11:31:12 -07002356 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
Yan, Zheng0e294382016-07-04 18:06:41 +08002357 spin_lock(&ci->i_ceph_lock);
Yan, Zheng13c2b572016-07-05 16:45:21 +08002358 cap = ci->i_auth_cap;
2359 if (!(cap && cap->session == session)) {
2360 pr_err("%p auth cap %p not mds%d ???\n",
2361 &ci->vfs_inode, cap, session->s_mds);
2362 spin_unlock(&ci->i_ceph_lock);
2363 continue;
2364 }
2365 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2366 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
2367 __kick_flushing_caps(mdsc, session, ci,
2368 oldest_flush_tid);
2369 }
Yan, Zheng0e294382016-07-04 18:06:41 +08002370 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002371 }
2372}
2373
Sage Weil088b3f52011-01-18 08:56:01 -08002374static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc,
2375 struct ceph_mds_session *session,
2376 struct inode *inode)
Yan, Zheng0e294382016-07-04 18:06:41 +08002377 __releases(ci->i_ceph_lock)
Sage Weil088b3f52011-01-18 08:56:01 -08002378{
2379 struct ceph_inode_info *ci = ceph_inode(inode);
2380 struct ceph_cap *cap;
Sage Weil088b3f52011-01-18 08:56:01 -08002381
Sage Weil088b3f52011-01-18 08:56:01 -08002382 cap = ci->i_auth_cap;
Yan, Zheng8310b082015-06-09 17:20:12 +08002383 dout("kick_flushing_inode_caps %p flushing %s\n", inode,
2384 ceph_cap_string(ci->i_flushing_caps));
Yan, Zheng005c4692013-05-31 16:40:24 +08002385
Yan, Zheng0e294382016-07-04 18:06:41 +08002386 if (!list_empty(&ci->i_cap_flush_list)) {
2387 u64 oldest_flush_tid;
Yan, Zheng005c4692013-05-31 16:40:24 +08002388 spin_lock(&mdsc->cap_dirty_lock);
2389 list_move_tail(&ci->i_flushing_item,
2390 &cap->session->s_cap_flushing);
Yan, Zheng0e294382016-07-04 18:06:41 +08002391 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
Yan, Zheng005c4692013-05-31 16:40:24 +08002392 spin_unlock(&mdsc->cap_dirty_lock);
2393
Yan, Zheng13c2b572016-07-05 16:45:21 +08002394 ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH;
Yan, Zheng0e294382016-07-04 18:06:41 +08002395 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
Yan, Zheng553adfd2015-06-09 15:48:57 +08002396 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08002397 } else {
Sage Weilbe655592011-11-30 09:47:09 -08002398 spin_unlock(&ci->i_ceph_lock);
Sage Weil088b3f52011-01-18 08:56:01 -08002399 }
2400}
2401
Sage Weila8599bd2009-10-06 11:31:12 -07002402
2403/*
2404 * Take references to capabilities we hold, so that we don't release
2405 * them to the MDS prematurely.
2406 *
Sage Weilbe655592011-11-30 09:47:09 -08002407 * Protected by i_ceph_lock.
Sage Weila8599bd2009-10-06 11:31:12 -07002408 */
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002409static void __take_cap_refs(struct ceph_inode_info *ci, int got,
2410 bool snap_rwsem_locked)
Sage Weila8599bd2009-10-06 11:31:12 -07002411{
2412 if (got & CEPH_CAP_PIN)
2413 ci->i_pin_ref++;
2414 if (got & CEPH_CAP_FILE_RD)
2415 ci->i_rd_ref++;
2416 if (got & CEPH_CAP_FILE_CACHE)
2417 ci->i_rdcache_ref++;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002418 if (got & CEPH_CAP_FILE_WR) {
2419 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2420 BUG_ON(!snap_rwsem_locked);
2421 ci->i_head_snapc = ceph_get_snap_context(
2422 ci->i_snap_realm->cached_context);
2423 }
Sage Weila8599bd2009-10-06 11:31:12 -07002424 ci->i_wr_ref++;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002425 }
Sage Weila8599bd2009-10-06 11:31:12 -07002426 if (got & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002427 if (ci->i_wb_ref == 0)
Sage Weil3772d262011-05-03 09:28:08 -07002428 ihold(&ci->vfs_inode);
Henry C Changd3d07202011-05-11 10:29:54 +00002429 ci->i_wb_ref++;
2430 dout("__take_cap_refs %p wb %d -> %d (?)\n",
2431 &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002432 }
2433}
2434
2435/*
2436 * Try to grab cap references. Specify those refs we @want, and the
2437 * minimal set we @need. Also include the larger offset we are writing
2438 * to (when applicable), and check against max_size here as well.
2439 * Note that caller is responsible for ensuring max_size increases are
2440 * requested from the MDS.
2441 */
2442static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want,
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002443 loff_t endoff, bool nonblock, int *got, int *err)
Sage Weila8599bd2009-10-06 11:31:12 -07002444{
2445 struct inode *inode = &ci->vfs_inode;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002446 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07002447 int ret = 0;
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002448 int have, implemented;
Sage Weil195d3ce2010-03-01 09:57:54 -08002449 int file_wanted;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002450 bool snap_rwsem_locked = false;
Sage Weila8599bd2009-10-06 11:31:12 -07002451
2452 dout("get_cap_refs %p need %s want %s\n", inode,
2453 ceph_cap_string(need), ceph_cap_string(want));
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002454
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002455again:
Sage Weilbe655592011-11-30 09:47:09 -08002456 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002457
Sage Weil195d3ce2010-03-01 09:57:54 -08002458 /* make sure file is actually open */
2459 file_wanted = __ceph_caps_file_wanted(ci);
Yan, Zheng77310322016-04-08 15:27:16 +08002460 if ((file_wanted & need) != need) {
Sage Weil195d3ce2010-03-01 09:57:54 -08002461 dout("try_get_cap_refs need %s file_wanted %s, EBADF\n",
2462 ceph_cap_string(need), ceph_cap_string(file_wanted));
Sage Weila8599bd2009-10-06 11:31:12 -07002463 *err = -EBADF;
2464 ret = 1;
Yan, Zheng3738daa2014-11-14 22:10:07 +08002465 goto out_unlock;
Sage Weila8599bd2009-10-06 11:31:12 -07002466 }
2467
Yan, Zheng37505d52013-04-12 16:11:10 +08002468 /* finish pending truncate */
2469 while (ci->i_truncate_pending) {
2470 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002471 if (snap_rwsem_locked) {
2472 up_read(&mdsc->snap_rwsem);
2473 snap_rwsem_locked = false;
2474 }
Yan, Zhengb415bf42013-07-02 12:40:19 +08002475 __ceph_do_pending_vmtruncate(inode);
Yan, Zheng37505d52013-04-12 16:11:10 +08002476 spin_lock(&ci->i_ceph_lock);
2477 }
2478
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002479 have = __ceph_caps_issued(ci, &implemented);
2480
2481 if (have & need & CEPH_CAP_FILE_WR) {
Sage Weila8599bd2009-10-06 11:31:12 -07002482 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2483 dout("get_cap_refs %p endoff %llu > maxsize %llu\n",
2484 inode, endoff, ci->i_max_size);
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002485 if (endoff > ci->i_requested_max_size) {
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002486 *err = -EAGAIN;
Sage Weila8599bd2009-10-06 11:31:12 -07002487 ret = 1;
2488 }
Yan, Zheng3738daa2014-11-14 22:10:07 +08002489 goto out_unlock;
Sage Weila8599bd2009-10-06 11:31:12 -07002490 }
2491 /*
2492 * If a sync write is in progress, we must wait, so that we
2493 * can get a final snapshot value for size+mtime.
2494 */
2495 if (__ceph_have_pending_cap_snap(ci)) {
2496 dout("get_cap_refs %p cap_snap_pending\n", inode);
Yan, Zheng3738daa2014-11-14 22:10:07 +08002497 goto out_unlock;
Sage Weila8599bd2009-10-06 11:31:12 -07002498 }
2499 }
Sage Weila8599bd2009-10-06 11:31:12 -07002500
Sage Weila8599bd2009-10-06 11:31:12 -07002501 if ((have & need) == need) {
2502 /*
2503 * Look at (implemented & ~have & not) so that we keep waiting
2504 * on transition from wanted -> needed caps. This is needed
2505 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2506 * going before a prior buffered writeback happens.
2507 */
2508 int not = want & ~(have & need);
2509 int revoking = implemented & ~have;
2510 dout("get_cap_refs %p have %s but not %s (revoking %s)\n",
2511 inode, ceph_cap_string(have), ceph_cap_string(not),
2512 ceph_cap_string(revoking));
2513 if ((revoking & not) == 0) {
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002514 if (!snap_rwsem_locked &&
2515 !ci->i_head_snapc &&
2516 (need & CEPH_CAP_FILE_WR)) {
2517 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2518 /*
2519 * we can not call down_read() when
2520 * task isn't in TASK_RUNNING state
2521 */
2522 if (nonblock) {
2523 *err = -EAGAIN;
2524 ret = 1;
2525 goto out_unlock;
2526 }
2527
2528 spin_unlock(&ci->i_ceph_lock);
2529 down_read(&mdsc->snap_rwsem);
2530 snap_rwsem_locked = true;
2531 goto again;
2532 }
2533 snap_rwsem_locked = true;
2534 }
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002535 *got = need | (have & want);
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +08002536 if ((need & CEPH_CAP_FILE_RD) &&
2537 !(*got & CEPH_CAP_FILE_CACHE))
2538 ceph_disable_fscache_readpage(ci);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002539 __take_cap_refs(ci, *got, true);
Sage Weila8599bd2009-10-06 11:31:12 -07002540 ret = 1;
2541 }
2542 } else {
Yan, Zheng03f4fcb2015-01-05 11:04:04 +08002543 int session_readonly = false;
2544 if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) {
2545 struct ceph_mds_session *s = ci->i_auth_cap->session;
2546 spin_lock(&s->s_cap_lock);
2547 session_readonly = s->s_readonly;
2548 spin_unlock(&s->s_cap_lock);
2549 }
2550 if (session_readonly) {
2551 dout("get_cap_refs %p needed %s but mds%d readonly\n",
2552 inode, ceph_cap_string(need), ci->i_auth_cap->mds);
2553 *err = -EROFS;
2554 ret = 1;
2555 goto out_unlock;
2556 }
2557
Yan, Zheng77310322016-04-08 15:27:16 +08002558 if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) {
2559 int mds_wanted;
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01002560 if (READ_ONCE(mdsc->fsc->mount_state) ==
Yan, Zheng77310322016-04-08 15:27:16 +08002561 CEPH_MOUNT_SHUTDOWN) {
2562 dout("get_cap_refs %p forced umount\n", inode);
2563 *err = -EIO;
2564 ret = 1;
2565 goto out_unlock;
2566 }
Yan, Zhengc1944fe2017-01-29 22:15:47 +08002567 mds_wanted = __ceph_caps_mds_wanted(ci, false);
Yan, Zhengeb65b912017-01-12 17:18:00 +08002568 if (need & ~(mds_wanted & need)) {
Yan, Zheng77310322016-04-08 15:27:16 +08002569 dout("get_cap_refs %p caps were dropped"
2570 " (session killed?)\n", inode);
2571 *err = -ESTALE;
2572 ret = 1;
2573 goto out_unlock;
2574 }
Yan, Zhengeb65b912017-01-12 17:18:00 +08002575 if (!(file_wanted & ~mds_wanted))
Yan, Zheng77310322016-04-08 15:27:16 +08002576 ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED;
Yan, Zheng48fec5d2015-07-01 16:27:46 +08002577 }
2578
Sage Weila8599bd2009-10-06 11:31:12 -07002579 dout("get_cap_refs %p have %s needed %s\n", inode,
2580 ceph_cap_string(have), ceph_cap_string(need));
2581 }
Yan, Zheng3738daa2014-11-14 22:10:07 +08002582out_unlock:
Sage Weilbe655592011-11-30 09:47:09 -08002583 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002584 if (snap_rwsem_locked)
2585 up_read(&mdsc->snap_rwsem);
Yan, Zheng3738daa2014-11-14 22:10:07 +08002586
Sage Weila8599bd2009-10-06 11:31:12 -07002587 dout("get_cap_refs %p ret %d got %s\n", inode,
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002588 ret, ceph_cap_string(*got));
Sage Weila8599bd2009-10-06 11:31:12 -07002589 return ret;
2590}
2591
2592/*
2593 * Check the offset we are writing up to against our current
2594 * max_size. If necessary, tell the MDS we want to write to
2595 * a larger offset.
2596 */
2597static void check_max_size(struct inode *inode, loff_t endoff)
2598{
2599 struct ceph_inode_info *ci = ceph_inode(inode);
2600 int check = 0;
2601
2602 /* do we need to explicitly request a larger max_size? */
Sage Weilbe655592011-11-30 09:47:09 -08002603 spin_lock(&ci->i_ceph_lock);
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002604 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
Sage Weila8599bd2009-10-06 11:31:12 -07002605 dout("write %p at large endoff %llu, req max_size\n",
2606 inode, endoff);
2607 ci->i_wanted_max_size = endoff;
Sage Weila8599bd2009-10-06 11:31:12 -07002608 }
Yan, Zheng3871cbb2013-08-05 14:10:29 +08002609 /* duplicate ceph_check_caps()'s logic */
2610 if (ci->i_auth_cap &&
2611 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2612 ci->i_wanted_max_size > ci->i_max_size &&
2613 ci->i_wanted_max_size > ci->i_requested_max_size)
2614 check = 1;
Sage Weilbe655592011-11-30 09:47:09 -08002615 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002616 if (check)
2617 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
2618}
2619
Yan, Zheng2b1ac852016-10-25 10:51:55 +08002620int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, int *got)
2621{
2622 int ret, err = 0;
2623
2624 BUG_ON(need & ~CEPH_CAP_FILE_RD);
2625 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
2626 ret = ceph_pool_perm_check(ci, need);
2627 if (ret < 0)
2628 return ret;
2629
2630 ret = try_get_cap_refs(ci, need, want, 0, true, got, &err);
2631 if (ret) {
2632 if (err == -EAGAIN) {
2633 ret = 0;
2634 } else if (err < 0) {
2635 ret = err;
2636 }
2637 }
2638 return ret;
2639}
2640
Sage Weila8599bd2009-10-06 11:31:12 -07002641/*
2642 * Wait for caps, and take cap references. If we can't get a WR cap
2643 * due to a small max_size, make sure we check_max_size (and possibly
2644 * ask the mds) so we don't get hung up indefinitely.
2645 */
Yan, Zheng3738daa2014-11-14 22:10:07 +08002646int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
2647 loff_t endoff, int *got, struct page **pinned_page)
Sage Weila8599bd2009-10-06 11:31:12 -07002648{
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002649 int _got, ret, err = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002650
Yan, Zheng10183a62015-04-27 15:33:28 +08002651 ret = ceph_pool_perm_check(ci, need);
2652 if (ret < 0)
2653 return ret;
2654
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002655 while (true) {
2656 if (endoff > 0)
2657 check_max_size(&ci->vfs_inode, endoff);
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002658
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002659 err = 0;
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002660 _got = 0;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002661 ret = try_get_cap_refs(ci, need, want, endoff,
2662 false, &_got, &err);
2663 if (ret) {
2664 if (err == -EAGAIN)
2665 continue;
2666 if (err < 0)
Yan, Zheng77310322016-04-08 15:27:16 +08002667 ret = err;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002668 } else {
Nikolay Borisov5c341ee32016-10-11 12:04:09 +03002669 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2670 add_wait_queue(&ci->i_cap_wq, &wait);
2671
2672 while (!try_get_cap_refs(ci, need, want, endoff,
Yan, Zheng6e09d0f2016-12-22 16:05:43 +08002673 true, &_got, &err)) {
2674 if (signal_pending(current)) {
2675 ret = -ERESTARTSYS;
2676 break;
2677 }
Nikolay Borisov5c341ee32016-10-11 12:04:09 +03002678 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Yan, Zheng6e09d0f2016-12-22 16:05:43 +08002679 }
Nikolay Borisov5c341ee32016-10-11 12:04:09 +03002680
2681 remove_wait_queue(&ci->i_cap_wq, &wait);
2682
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002683 if (err == -EAGAIN)
2684 continue;
2685 if (err < 0)
2686 ret = err;
Yan, Zheng77310322016-04-08 15:27:16 +08002687 }
2688 if (ret < 0) {
2689 if (err == -ESTALE) {
2690 /* session was killed, try renew caps */
2691 ret = ceph_renew_caps(&ci->vfs_inode);
2692 if (ret == 0)
2693 continue;
2694 }
2695 return ret;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002696 }
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002697
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002698 if (ci->i_inline_version != CEPH_INLINE_NONE &&
2699 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
2700 i_size_read(&ci->vfs_inode) > 0) {
2701 struct page *page =
2702 find_get_page(ci->vfs_inode.i_mapping, 0);
2703 if (page) {
2704 if (PageUptodate(page)) {
2705 *pinned_page = page;
2706 break;
2707 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002708 put_page(page);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002709 }
2710 /*
2711 * drop cap refs first because getattr while
2712 * holding * caps refs can cause deadlock.
2713 */
2714 ceph_put_cap_refs(ci, _got);
2715 _got = 0;
2716
2717 /*
2718 * getattr request will bring inline data into
2719 * page cache
2720 */
2721 ret = __ceph_do_getattr(&ci->vfs_inode, NULL,
2722 CEPH_STAT_CAP_INLINE_DATA,
2723 true);
2724 if (ret < 0)
2725 return ret;
2726 continue;
2727 }
2728 break;
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002729 }
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002730
Yan, Zhengf7f7e7a2016-05-18 20:31:55 +08002731 if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE))
2732 ceph_fscache_revalidate_cookie(ci);
2733
Yan, Zhengc4d4a582015-01-09 15:56:18 +08002734 *got = _got;
2735 return 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002736}
2737
2738/*
2739 * Take cap refs. Caller must already know we hold at least one ref
2740 * on the caps in question or we don't know this is safe.
2741 */
2742void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
2743{
Sage Weilbe655592011-11-30 09:47:09 -08002744 spin_lock(&ci->i_ceph_lock);
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002745 __take_cap_refs(ci, caps, false);
Sage Weilbe655592011-11-30 09:47:09 -08002746 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002747}
2748
Yan, Zheng86056092015-05-01 16:57:16 +08002749
2750/*
2751 * drop cap_snap that is not associated with any snapshot.
2752 * we don't need to send FLUSHSNAP message for it.
2753 */
Yan, Zheng70220ac2016-07-06 16:21:30 +08002754static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
2755 struct ceph_cap_snap *capsnap)
Yan, Zheng86056092015-05-01 16:57:16 +08002756{
2757 if (!capsnap->need_flush &&
2758 !capsnap->writing && !capsnap->dirty_pages) {
Yan, Zheng86056092015-05-01 16:57:16 +08002759 dout("dropping cap_snap %p follows %llu\n",
2760 capsnap, capsnap->follows);
Yan, Zheng0e294382016-07-04 18:06:41 +08002761 BUG_ON(capsnap->cap_flush.tid > 0);
Yan, Zheng86056092015-05-01 16:57:16 +08002762 ceph_put_snap_context(capsnap->context);
Yan, Zheng70220ac2016-07-06 16:21:30 +08002763 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
2764 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2765
Yan, Zheng86056092015-05-01 16:57:16 +08002766 list_del(&capsnap->ci_item);
Yan, Zheng86056092015-05-01 16:57:16 +08002767 ceph_put_cap_snap(capsnap);
2768 return 1;
2769 }
2770 return 0;
2771}
2772
Sage Weila8599bd2009-10-06 11:31:12 -07002773/*
2774 * Release cap refs.
2775 *
2776 * If we released the last ref on any given cap, call ceph_check_caps
2777 * to release (or schedule a release).
2778 *
2779 * If we are releasing a WR cap (from a sync write), finalize any affected
2780 * cap_snap, and wake up any waiters.
2781 */
2782void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
2783{
2784 struct inode *inode = &ci->vfs_inode;
2785 int last = 0, put = 0, flushsnaps = 0, wake = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07002786
Sage Weilbe655592011-11-30 09:47:09 -08002787 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002788 if (had & CEPH_CAP_PIN)
2789 --ci->i_pin_ref;
2790 if (had & CEPH_CAP_FILE_RD)
2791 if (--ci->i_rd_ref == 0)
2792 last++;
2793 if (had & CEPH_CAP_FILE_CACHE)
2794 if (--ci->i_rdcache_ref == 0)
2795 last++;
2796 if (had & CEPH_CAP_FILE_BUFFER) {
Henry C Changd3d07202011-05-11 10:29:54 +00002797 if (--ci->i_wb_ref == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07002798 last++;
2799 put++;
2800 }
Henry C Changd3d07202011-05-11 10:29:54 +00002801 dout("put_cap_refs %p wb %d -> %d (?)\n",
2802 inode, ci->i_wb_ref+1, ci->i_wb_ref);
Sage Weila8599bd2009-10-06 11:31:12 -07002803 }
2804 if (had & CEPH_CAP_FILE_WR)
2805 if (--ci->i_wr_ref == 0) {
2806 last++;
Yan, Zheng86056092015-05-01 16:57:16 +08002807 if (__ceph_have_pending_cap_snap(ci)) {
2808 struct ceph_cap_snap *capsnap =
2809 list_last_entry(&ci->i_cap_snaps,
2810 struct ceph_cap_snap,
2811 ci_item);
2812 capsnap->writing = 0;
Yan, Zheng70220ac2016-07-06 16:21:30 +08002813 if (ceph_try_drop_cap_snap(ci, capsnap))
Yan, Zheng86056092015-05-01 16:57:16 +08002814 put++;
2815 else if (__ceph_finish_cap_snap(ci, capsnap))
2816 flushsnaps = 1;
2817 wake = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07002818 }
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002819 if (ci->i_wrbuffer_ref_head == 0 &&
2820 ci->i_dirty_caps == 0 &&
2821 ci->i_flushing_caps == 0) {
2822 BUG_ON(!ci->i_head_snapc);
2823 ceph_put_snap_context(ci->i_head_snapc);
2824 ci->i_head_snapc = NULL;
2825 }
Yan, Zhengdb40cc12015-03-23 20:12:20 +08002826 /* see comment in __ceph_remove_cap() */
2827 if (!__ceph_is_any_caps(ci) && ci->i_snap_realm)
2828 drop_inode_snap_realm(ci);
Sage Weila8599bd2009-10-06 11:31:12 -07002829 }
Sage Weilbe655592011-11-30 09:47:09 -08002830 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002831
Sage Weil819ccbf2010-04-01 09:33:46 -07002832 dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had),
2833 last ? " last" : "", put ? " put" : "");
Sage Weila8599bd2009-10-06 11:31:12 -07002834
2835 if (last && !flushsnaps)
2836 ceph_check_caps(ci, 0, NULL);
2837 else if (flushsnaps)
Yan, Zhenged9b4302016-07-05 21:08:07 +08002838 ceph_flush_snaps(ci, NULL);
Sage Weila8599bd2009-10-06 11:31:12 -07002839 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002840 wake_up_all(&ci->i_cap_wq);
Yan, Zheng86056092015-05-01 16:57:16 +08002841 while (put-- > 0)
Sage Weila8599bd2009-10-06 11:31:12 -07002842 iput(inode);
2843}
2844
2845/*
2846 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
2847 * context. Adjust per-snap dirty page accounting as appropriate.
2848 * Once all dirty data for a cap_snap is flushed, flush snapped file
2849 * metadata back to the MDS. If we dropped the last ref, call
2850 * ceph_check_caps.
2851 */
2852void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
2853 struct ceph_snap_context *snapc)
2854{
2855 struct inode *inode = &ci->vfs_inode;
Sage Weila8599bd2009-10-06 11:31:12 -07002856 struct ceph_cap_snap *capsnap = NULL;
Yan, Zheng70220ac2016-07-06 16:21:30 +08002857 int put = 0;
2858 bool last = false;
2859 bool found = false;
2860 bool flush_snaps = false;
2861 bool complete_capsnap = false;
Sage Weila8599bd2009-10-06 11:31:12 -07002862
Sage Weilbe655592011-11-30 09:47:09 -08002863 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002864 ci->i_wrbuffer_ref -= nr;
Yan, Zheng70220ac2016-07-06 16:21:30 +08002865 if (ci->i_wrbuffer_ref == 0) {
2866 last = true;
2867 put++;
2868 }
Sage Weila8599bd2009-10-06 11:31:12 -07002869
2870 if (ci->i_head_snapc == snapc) {
2871 ci->i_wrbuffer_ref_head -= nr;
Sage Weil7d8cb262010-08-24 08:44:16 -07002872 if (ci->i_wrbuffer_ref_head == 0 &&
Yan, Zheng5dda377c2015-04-30 14:40:54 +08002873 ci->i_wr_ref == 0 &&
2874 ci->i_dirty_caps == 0 &&
2875 ci->i_flushing_caps == 0) {
Sage Weil7d8cb262010-08-24 08:44:16 -07002876 BUG_ON(!ci->i_head_snapc);
Sage Weila8599bd2009-10-06 11:31:12 -07002877 ceph_put_snap_context(ci->i_head_snapc);
2878 ci->i_head_snapc = NULL;
2879 }
2880 dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n",
2881 inode,
2882 ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr,
2883 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
2884 last ? " LAST" : "");
2885 } else {
2886 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
2887 if (capsnap->context == snapc) {
Yan, Zheng70220ac2016-07-06 16:21:30 +08002888 found = true;
Sage Weila8599bd2009-10-06 11:31:12 -07002889 break;
2890 }
2891 }
2892 BUG_ON(!found);
Sage Weil819ccbf2010-04-01 09:33:46 -07002893 capsnap->dirty_pages -= nr;
2894 if (capsnap->dirty_pages == 0) {
Yan, Zheng70220ac2016-07-06 16:21:30 +08002895 complete_capsnap = true;
2896 if (!capsnap->writing) {
2897 if (ceph_try_drop_cap_snap(ci, capsnap)) {
2898 put++;
2899 } else {
2900 ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS;
2901 flush_snaps = true;
2902 }
2903 }
Sage Weil819ccbf2010-04-01 09:33:46 -07002904 }
Sage Weila8599bd2009-10-06 11:31:12 -07002905 dout("put_wrbuffer_cap_refs on %p cap_snap %p "
Yan, Zheng86056092015-05-01 16:57:16 +08002906 " snap %lld %d/%d -> %d/%d %s%s\n",
Sage Weila8599bd2009-10-06 11:31:12 -07002907 inode, capsnap, capsnap->context->seq,
2908 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
2909 ci->i_wrbuffer_ref, capsnap->dirty_pages,
2910 last ? " (wrbuffer last)" : "",
Yan, Zheng86056092015-05-01 16:57:16 +08002911 complete_capsnap ? " (complete capsnap)" : "");
Sage Weila8599bd2009-10-06 11:31:12 -07002912 }
2913
Sage Weilbe655592011-11-30 09:47:09 -08002914 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07002915
2916 if (last) {
2917 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL);
Yan, Zheng70220ac2016-07-06 16:21:30 +08002918 } else if (flush_snaps) {
Yan, Zhenged9b4302016-07-05 21:08:07 +08002919 ceph_flush_snaps(ci, NULL);
Sage Weila8599bd2009-10-06 11:31:12 -07002920 }
Yan, Zheng70220ac2016-07-06 16:21:30 +08002921 if (complete_capsnap)
2922 wake_up_all(&ci->i_cap_wq);
2923 while (put-- > 0)
Sage Weil819ccbf2010-04-01 09:33:46 -07002924 iput(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07002925}
2926
2927/*
Yan, Zhengca20c992013-07-21 10:07:51 +08002928 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
2929 */
2930static void invalidate_aliases(struct inode *inode)
2931{
2932 struct dentry *dn, *prev = NULL;
2933
2934 dout("invalidate_aliases inode %p\n", inode);
2935 d_prune_aliases(inode);
2936 /*
2937 * For non-directory inode, d_find_alias() only returns
J. Bruce Fieldsfc12c802014-01-16 17:42:53 -05002938 * hashed dentry. After calling d_invalidate(), the
2939 * dentry becomes unhashed.
Yan, Zhengca20c992013-07-21 10:07:51 +08002940 *
Yan, Zhenga8d436f2013-09-02 15:19:54 +08002941 * For directory inode, d_find_alias() can return
J. Bruce Fieldsfc12c802014-01-16 17:42:53 -05002942 * unhashed dentry. But directory inode should have
Yan, Zhengca20c992013-07-21 10:07:51 +08002943 * one alias at most.
2944 */
2945 while ((dn = d_find_alias(inode))) {
2946 if (dn == prev) {
2947 dput(dn);
2948 break;
2949 }
Yan, Zhenga8d436f2013-09-02 15:19:54 +08002950 d_invalidate(dn);
Yan, Zhengca20c992013-07-21 10:07:51 +08002951 if (prev)
2952 dput(prev);
2953 prev = dn;
2954 }
2955 if (prev)
2956 dput(prev);
2957}
2958
2959/*
Sage Weila8599bd2009-10-06 11:31:12 -07002960 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
2961 * actually be a revocation if it specifies a smaller cap set.)
2962 *
Sage Weilbe655592011-11-30 09:47:09 -08002963 * caller holds s_mutex and i_ceph_lock, we drop both.
Sage Weila8599bd2009-10-06 11:31:12 -07002964 */
Yan, Zheng2cd698b2014-04-18 13:20:27 +08002965static void handle_cap_grant(struct ceph_mds_client *mdsc,
2966 struct inode *inode, struct ceph_mds_caps *grant,
Yan, Zheng779fe0f2016-03-07 09:35:06 +08002967 struct ceph_string **pns, u64 inline_version,
2968 void *inline_data, u32 inline_len,
Yan, Zheng2cd698b2014-04-18 13:20:27 +08002969 struct ceph_buffer *xattr_buf,
Sage Weil15637c82010-03-16 13:42:00 -07002970 struct ceph_mds_session *session,
Yan, Zheng779fe0f2016-03-07 09:35:06 +08002971 struct ceph_cap *cap, int issued)
Yan, Zheng2cd698b2014-04-18 13:20:27 +08002972 __releases(ci->i_ceph_lock)
Yan, Zheng982d6012014-12-23 15:30:54 +08002973 __releases(mdsc->snap_rwsem)
Sage Weila8599bd2009-10-06 11:31:12 -07002974{
2975 struct ceph_inode_info *ci = ceph_inode(inode);
2976 int mds = session->s_mds;
Sage Weil2f56f562010-10-27 20:59:49 -07002977 int seq = le32_to_cpu(grant->seq);
Sage Weila8599bd2009-10-06 11:31:12 -07002978 int newcaps = le32_to_cpu(grant->caps);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08002979 int used, wanted, dirty;
Sage Weila8599bd2009-10-06 11:31:12 -07002980 u64 size = le64_to_cpu(grant->size);
2981 u64 max_size = le64_to_cpu(grant->max_size);
2982 struct timespec mtime, atime, ctime;
Sage Weil15637c82010-03-16 13:42:00 -07002983 int check_caps = 0;
Fabian Frederickab6c2c32014-10-09 23:16:35 +02002984 bool wake = false;
2985 bool writeback = false;
2986 bool queue_trunc = false;
2987 bool queue_invalidate = false;
Fabian Frederickab6c2c32014-10-09 23:16:35 +02002988 bool deleted_inode = false;
Yan, Zheng31c542a2014-11-14 21:41:55 +08002989 bool fill_inline = false;
Sage Weila8599bd2009-10-06 11:31:12 -07002990
Sage Weil2f56f562010-10-27 20:59:49 -07002991 dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n",
2992 inode, cap, mds, seq, ceph_cap_string(newcaps));
Sage Weila8599bd2009-10-06 11:31:12 -07002993 dout(" size %llu max_size %llu, i_size %llu\n", size, max_size,
2994 inode->i_size);
2995
Yan, Zheng11df2df2013-11-24 14:44:38 +08002996
2997 /*
2998 * auth mds of the inode changed. we received the cap export message,
2999 * but still haven't received the cap import message. handle_cap_export
3000 * updated the new auth MDS' cap.
3001 *
3002 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3003 * that was sent before the cap import message. So don't remove caps.
3004 */
3005 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3006 WARN_ON(cap != ci->i_auth_cap);
3007 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3008 seq = cap->seq;
3009 newcaps |= cap->issued;
3010 }
3011
Sage Weila8599bd2009-10-06 11:31:12 -07003012 /*
3013 * If CACHE is being revoked, and we have no dirty buffers,
3014 * try to invalidate (once). (If there are dirty buffers, we
3015 * will invalidate _after_ writeback.)
3016 */
Yan, Zhengfdd4e152015-06-16 20:48:56 +08003017 if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */
3018 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
Sage Weil3b454c42010-06-10 13:20:33 -07003019 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
Yan, Zheng9abd4db2016-05-18 20:58:26 +08003020 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
Li Wange9075742013-08-15 22:00:25 -07003021 if (try_nonblocking_invalidate(inode)) {
Sage Weila8599bd2009-10-06 11:31:12 -07003022 /* there were locked pages.. invalidate later
3023 in a separate thread. */
3024 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003025 queue_invalidate = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003026 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3027 }
Sage Weila8599bd2009-10-06 11:31:12 -07003028 }
Sage Weila8599bd2009-10-06 11:31:12 -07003029 }
3030
3031 /* side effects now are allowed */
Sage Weil685f9a5d2009-11-09 12:05:48 -08003032 cap->cap_gen = session->s_cap_gen;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003033 cap->seq = seq;
Sage Weila8599bd2009-10-06 11:31:12 -07003034
3035 __check_cap_issue(ci, cap, newcaps);
3036
Yan, Zhengf98a1282014-04-17 08:55:50 +08003037 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3038 (issued & CEPH_CAP_AUTH_EXCL) == 0) {
Sage Weila8599bd2009-10-06 11:31:12 -07003039 inode->i_mode = le32_to_cpu(grant->mode);
Eric W. Biederman05cb11c2013-01-31 02:56:19 -08003040 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3041 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
Sage Weila8599bd2009-10-06 11:31:12 -07003042 dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
Eric W. Biedermanbd2bae62013-01-31 04:05:39 -08003043 from_kuid(&init_user_ns, inode->i_uid),
3044 from_kgid(&init_user_ns, inode->i_gid));
Sage Weila8599bd2009-10-06 11:31:12 -07003045 }
3046
Yan, Zhengf98a1282014-04-17 08:55:50 +08003047 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3048 (issued & CEPH_CAP_LINK_EXCL) == 0) {
Miklos Szeredibfe86842011-10-28 14:13:29 +02003049 set_nlink(inode, le32_to_cpu(grant->nlink));
Yan, Zhengca20c992013-07-21 10:07:51 +08003050 if (inode->i_nlink == 0 &&
3051 (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL)))
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003052 deleted_inode = true;
Yan, Zhengca20c992013-07-21 10:07:51 +08003053 }
Sage Weila8599bd2009-10-06 11:31:12 -07003054
3055 if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) {
3056 int len = le32_to_cpu(grant->xattr_len);
3057 u64 version = le64_to_cpu(grant->xattr_version);
3058
3059 if (version > ci->i_xattrs.version) {
3060 dout(" got new xattrs v%llu on %p len %d\n",
3061 version, inode, len);
3062 if (ci->i_xattrs.blob)
3063 ceph_buffer_put(ci->i_xattrs.blob);
3064 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3065 ci->i_xattrs.version = version;
Guangliang Zhao7221fe42013-11-11 15:18:03 +08003066 ceph_forget_all_cached_acls(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07003067 }
3068 }
3069
Yan, Zhengf98a1282014-04-17 08:55:50 +08003070 if (newcaps & CEPH_CAP_ANY_RD) {
3071 /* ctime/mtime/atime? */
3072 ceph_decode_timespec(&mtime, &grant->mtime);
3073 ceph_decode_timespec(&atime, &grant->atime);
3074 ceph_decode_timespec(&ctime, &grant->ctime);
3075 ceph_fill_file_time(inode, issued,
3076 le32_to_cpu(grant->time_warp_seq),
3077 &ctime, &mtime, &atime);
3078 }
Sage Weila8599bd2009-10-06 11:31:12 -07003079
Yan, Zhengf98a1282014-04-17 08:55:50 +08003080 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3081 /* file layout may have changed */
Yan, Zheng76271512016-02-03 21:24:49 +08003082 s64 old_pool = ci->i_layout.pool_id;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003083 struct ceph_string *old_ns;
3084
Yan, Zheng76271512016-02-03 21:24:49 +08003085 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003086 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3087 lockdep_is_held(&ci->i_ceph_lock));
3088 rcu_assign_pointer(ci->i_layout.pool_ns, *pns);
3089
3090 if (ci->i_layout.pool_id != old_pool || *pns != old_ns)
Yan, Zheng76271512016-02-03 21:24:49 +08003091 ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003092
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003093 *pns = old_ns;
3094
Yan, Zhengf98a1282014-04-17 08:55:50 +08003095 /* size/truncate_seq? */
3096 queue_trunc = ceph_fill_file_size(inode, issued,
3097 le32_to_cpu(grant->truncate_seq),
3098 le64_to_cpu(grant->truncate_size),
3099 size);
Yan, Zheng84eea8c2017-05-16 08:55:34 +08003100 }
3101
3102 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3103 if (max_size != ci->i_max_size) {
Yan, Zhengf98a1282014-04-17 08:55:50 +08003104 dout("max_size %lld -> %llu\n",
3105 ci->i_max_size, max_size);
3106 ci->i_max_size = max_size;
3107 if (max_size >= ci->i_wanted_max_size) {
3108 ci->i_wanted_max_size = 0; /* reset */
3109 ci->i_requested_max_size = 0;
3110 }
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003111 wake = true;
Yan, Zheng84eea8c2017-05-16 08:55:34 +08003112 } else if (ci->i_wanted_max_size > ci->i_max_size &&
3113 ci->i_wanted_max_size > ci->i_requested_max_size) {
3114 /* CEPH_CAP_OP_IMPORT */
3115 wake = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003116 }
Sage Weila8599bd2009-10-06 11:31:12 -07003117 }
3118
3119 /* check cap bits */
3120 wanted = __ceph_caps_wanted(ci);
3121 used = __ceph_caps_used(ci);
3122 dirty = __ceph_caps_dirty(ci);
3123 dout(" my wanted = %s, used = %s, dirty %s\n",
3124 ceph_cap_string(wanted),
3125 ceph_cap_string(used),
3126 ceph_cap_string(dirty));
3127 if (wanted != le32_to_cpu(grant->wanted)) {
3128 dout("mds wanted %s -> %s\n",
3129 ceph_cap_string(le32_to_cpu(grant->wanted)),
3130 ceph_cap_string(wanted));
Yan, Zheng390306c2013-01-04 15:30:10 +08003131 /* imported cap may not have correct mds_wanted */
3132 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT)
3133 check_caps = 1;
Sage Weila8599bd2009-10-06 11:31:12 -07003134 }
3135
Sage Weila8599bd2009-10-06 11:31:12 -07003136 /* revocation, grant, or no-op? */
3137 if (cap->issued & ~newcaps) {
Sage Weil3b454c42010-06-10 13:20:33 -07003138 int revoking = cap->issued & ~newcaps;
3139
3140 dout("revocation: %s -> %s (revoking %s)\n",
3141 ceph_cap_string(cap->issued),
3142 ceph_cap_string(newcaps),
3143 ceph_cap_string(revoking));
Sage Weil0eb6cd42010-08-05 13:53:18 -07003144 if (revoking & used & CEPH_CAP_FILE_BUFFER)
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003145 writeback = true; /* initiate writeback; will delay ack */
Sage Weil3b454c42010-06-10 13:20:33 -07003146 else if (revoking == CEPH_CAP_FILE_CACHE &&
3147 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3148 queue_invalidate)
3149 ; /* do nothing yet, invalidation will be queued */
3150 else if (cap == ci->i_auth_cap)
3151 check_caps = 1; /* check auth cap only */
3152 else
3153 check_caps = 2; /* check all caps */
Sage Weila8599bd2009-10-06 11:31:12 -07003154 cap->issued = newcaps;
Sage Weil978097c2010-03-08 15:27:53 -08003155 cap->implemented |= newcaps;
Sage Weila8599bd2009-10-06 11:31:12 -07003156 } else if (cap->issued == newcaps) {
3157 dout("caps unchanged: %s -> %s\n",
3158 ceph_cap_string(cap->issued), ceph_cap_string(newcaps));
3159 } else {
3160 dout("grant: %s -> %s\n", ceph_cap_string(cap->issued),
3161 ceph_cap_string(newcaps));
Yan, Zheng6ee6b9532013-07-02 12:40:21 +08003162 /* non-auth MDS is revoking the newly grant caps ? */
3163 if (cap == ci->i_auth_cap &&
3164 __ceph_caps_revoking_other(ci, cap, newcaps))
3165 check_caps = 2;
3166
Sage Weila8599bd2009-10-06 11:31:12 -07003167 cap->issued = newcaps;
3168 cap->implemented |= newcaps; /* add bits only, to
3169 * avoid stepping on a
3170 * pending revocation */
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003171 wake = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003172 }
Sage Weil978097c2010-03-08 15:27:53 -08003173 BUG_ON(cap->issued & ~cap->implemented);
Sage Weila8599bd2009-10-06 11:31:12 -07003174
Yan, Zheng31c542a2014-11-14 21:41:55 +08003175 if (inline_version > 0 && inline_version >= ci->i_inline_version) {
3176 ci->i_inline_version = inline_version;
3177 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3178 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3179 fill_inline = true;
3180 }
3181
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003182 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003183 if (newcaps & ~issued)
Fabian Frederickab6c2c32014-10-09 23:16:35 +02003184 wake = true;
Yan, Zheng0e294382016-07-04 18:06:41 +08003185 kick_flushing_inode_caps(mdsc, session, inode);
3186 up_read(&mdsc->snap_rwsem);
3187 } else {
3188 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003189 }
3190
Yan, Zheng31c542a2014-11-14 21:41:55 +08003191 if (fill_inline)
3192 ceph_fill_inline_data(inode, NULL, inline_data, inline_len);
3193
Yan, Zheng14649752016-05-20 15:41:20 +08003194 if (queue_trunc)
Yan, Zhengc6bcda62014-04-11 10:18:07 +08003195 ceph_queue_vmtruncate(inode);
Yan, Zhengc6bcda62014-04-11 10:18:07 +08003196
Sage Weil3c6f6b72010-02-09 15:24:44 -08003197 if (writeback)
Sage Weila8599bd2009-10-06 11:31:12 -07003198 /*
3199 * queue inode for writeback: we can't actually call
3200 * filemap_write_and_wait, etc. from message handler
3201 * context.
3202 */
Sage Weil3c6f6b72010-02-09 15:24:44 -08003203 ceph_queue_writeback(inode);
3204 if (queue_invalidate)
3205 ceph_queue_invalidate(inode);
Yan, Zhengca20c992013-07-21 10:07:51 +08003206 if (deleted_inode)
3207 invalidate_aliases(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07003208 if (wake)
Yehuda Sadeh03066f22010-07-27 13:11:08 -07003209 wake_up_all(&ci->i_cap_wq);
Sage Weil15637c82010-03-16 13:42:00 -07003210
3211 if (check_caps == 1)
3212 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY,
3213 session);
3214 else if (check_caps == 2)
3215 ceph_check_caps(ci, CHECK_CAPS_NODELAY, session);
3216 else
3217 mutex_unlock(&session->s_mutex);
Sage Weila8599bd2009-10-06 11:31:12 -07003218}
3219
3220/*
3221 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3222 * MDS has been safely committed.
3223 */
Sage Weil6df058c2009-12-22 11:24:33 -08003224static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07003225 struct ceph_mds_caps *m,
3226 struct ceph_mds_session *session,
3227 struct ceph_cap *cap)
Sage Weilbe655592011-11-30 09:47:09 -08003228 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07003229{
3230 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003231 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003232 struct ceph_cap_flush *cf, *tmp_cf;
Yan, Zheng553adfd2015-06-09 15:48:57 +08003233 LIST_HEAD(to_remove);
Sage Weila8599bd2009-10-06 11:31:12 -07003234 unsigned seq = le32_to_cpu(m->seq);
3235 int dirty = le32_to_cpu(m->dirty);
3236 int cleaned = 0;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003237 bool drop = false;
Thomas Meyer7271efa2017-10-07 16:02:21 +02003238 bool wake_ci = false;
3239 bool wake_mdsc = false;
Sage Weila8599bd2009-10-06 11:31:12 -07003240
Yan, Zhenge4500b52016-07-06 11:12:56 +08003241 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
Yan, Zheng553adfd2015-06-09 15:48:57 +08003242 if (cf->tid == flush_tid)
3243 cleaned = cf->caps;
Yan, Zheng0e294382016-07-04 18:06:41 +08003244 if (cf->caps == 0) /* capsnap */
3245 continue;
Yan, Zheng553adfd2015-06-09 15:48:57 +08003246 if (cf->tid <= flush_tid) {
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003247 if (__finish_cap_flush(NULL, ci, cf))
3248 wake_ci = true;
Yan, Zhenge4500b52016-07-06 11:12:56 +08003249 list_add_tail(&cf->i_list, &to_remove);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003250 } else {
3251 cleaned &= ~cf->caps;
3252 if (!cleaned)
3253 break;
3254 }
3255 }
Sage Weila8599bd2009-10-06 11:31:12 -07003256
3257 dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s,"
3258 " flushing %s -> %s\n",
3259 inode, session->s_mds, seq, ceph_cap_string(dirty),
3260 ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps),
3261 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3262
Yan, Zheng8310b082015-06-09 17:20:12 +08003263 if (list_empty(&to_remove) && !cleaned)
Sage Weila8599bd2009-10-06 11:31:12 -07003264 goto out;
3265
Sage Weila8599bd2009-10-06 11:31:12 -07003266 ci->i_flushing_caps &= ~cleaned;
Sage Weila8599bd2009-10-06 11:31:12 -07003267
3268 spin_lock(&mdsc->cap_dirty_lock);
Yan, Zheng8310b082015-06-09 17:20:12 +08003269
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003270 list_for_each_entry(cf, &to_remove, i_list) {
3271 if (__finish_cap_flush(mdsc, NULL, cf))
3272 wake_mdsc = true;
Yan, Zheng8310b082015-06-09 17:20:12 +08003273 }
3274
Sage Weila8599bd2009-10-06 11:31:12 -07003275 if (ci->i_flushing_caps == 0) {
Yan, Zheng0e294382016-07-04 18:06:41 +08003276 if (list_empty(&ci->i_cap_flush_list)) {
3277 list_del_init(&ci->i_flushing_item);
3278 if (!list_empty(&session->s_cap_flushing)) {
3279 dout(" mds%d still flushing cap on %p\n",
3280 session->s_mds,
3281 &list_first_entry(&session->s_cap_flushing,
3282 struct ceph_inode_info,
3283 i_flushing_item)->vfs_inode);
3284 }
3285 }
Sage Weila8599bd2009-10-06 11:31:12 -07003286 mdsc->num_cap_flushing--;
Sage Weila8599bd2009-10-06 11:31:12 -07003287 dout(" inode %p now !flushing\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07003288
3289 if (ci->i_dirty_caps == 0) {
3290 dout(" inode %p now clean\n", inode);
3291 BUG_ON(!list_empty(&ci->i_dirty_item));
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003292 drop = true;
Yan, Zheng5dda377c2015-04-30 14:40:54 +08003293 if (ci->i_wr_ref == 0 &&
3294 ci->i_wrbuffer_ref_head == 0) {
Sage Weil7d8cb262010-08-24 08:44:16 -07003295 BUG_ON(!ci->i_head_snapc);
3296 ceph_put_snap_context(ci->i_head_snapc);
3297 ci->i_head_snapc = NULL;
3298 }
Sage Weil76e3b392009-10-15 18:13:53 -07003299 } else {
3300 BUG_ON(list_empty(&ci->i_dirty_item));
Sage Weilafcdaea2009-10-14 14:27:38 -07003301 }
Sage Weila8599bd2009-10-06 11:31:12 -07003302 }
3303 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003304
3305out:
Sage Weilbe655592011-11-30 09:47:09 -08003306 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003307
3308 while (!list_empty(&to_remove)) {
3309 cf = list_first_entry(&to_remove,
Yan, Zhenge4500b52016-07-06 11:12:56 +08003310 struct ceph_cap_flush, i_list);
3311 list_del(&cf->i_list);
Yan, Zhengf66fd9f2015-06-10 17:26:13 +08003312 ceph_free_cap_flush(cf);
Yan, Zheng553adfd2015-06-09 15:48:57 +08003313 }
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003314
3315 if (wake_ci)
3316 wake_up_all(&ci->i_cap_wq);
3317 if (wake_mdsc)
3318 wake_up_all(&mdsc->cap_flushing_wq);
Sage Weilafcdaea2009-10-14 14:27:38 -07003319 if (drop)
Sage Weila8599bd2009-10-06 11:31:12 -07003320 iput(inode);
3321}
3322
3323/*
3324 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3325 * throw away our cap_snap.
3326 *
3327 * Caller hold s_mutex.
3328 */
Sage Weil6df058c2009-12-22 11:24:33 -08003329static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
Sage Weila8599bd2009-10-06 11:31:12 -07003330 struct ceph_mds_caps *m,
3331 struct ceph_mds_session *session)
3332{
3333 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zhengaffbc192015-05-05 21:22:13 +08003334 struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
Sage Weila8599bd2009-10-06 11:31:12 -07003335 u64 follows = le64_to_cpu(m->snap_follows);
Sage Weila8599bd2009-10-06 11:31:12 -07003336 struct ceph_cap_snap *capsnap;
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003337 bool flushed = false;
3338 bool wake_ci = false;
3339 bool wake_mdsc = false;
Sage Weila8599bd2009-10-06 11:31:12 -07003340
3341 dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n",
3342 inode, ci, session->s_mds, follows);
3343
Sage Weilbe655592011-11-30 09:47:09 -08003344 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003345 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
3346 if (capsnap->follows == follows) {
Yan, Zheng0e294382016-07-04 18:06:41 +08003347 if (capsnap->cap_flush.tid != flush_tid) {
Sage Weila8599bd2009-10-06 11:31:12 -07003348 dout(" cap_snap %p follows %lld tid %lld !="
3349 " %lld\n", capsnap, follows,
Yan, Zheng0e294382016-07-04 18:06:41 +08003350 flush_tid, capsnap->cap_flush.tid);
Sage Weila8599bd2009-10-06 11:31:12 -07003351 break;
3352 }
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003353 flushed = true;
Sage Weila8599bd2009-10-06 11:31:12 -07003354 break;
3355 } else {
3356 dout(" skipping cap_snap %p follows %lld\n",
3357 capsnap, capsnap->follows);
3358 }
3359 }
Yan, Zheng0e294382016-07-04 18:06:41 +08003360 if (flushed) {
Yan, Zheng0e294382016-07-04 18:06:41 +08003361 WARN_ON(capsnap->dirty_pages || capsnap->writing);
3362 dout(" removing %p cap_snap %p follows %lld\n",
3363 inode, capsnap, follows);
3364 list_del(&capsnap->ci_item);
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003365 if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush))
3366 wake_ci = true;
Yan, Zheng0e294382016-07-04 18:06:41 +08003367
3368 spin_lock(&mdsc->cap_dirty_lock);
3369
3370 if (list_empty(&ci->i_cap_flush_list))
3371 list_del_init(&ci->i_flushing_item);
3372
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003373 if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush))
3374 wake_mdsc = true;
Yan, Zheng0e294382016-07-04 18:06:41 +08003375
3376 spin_unlock(&mdsc->cap_dirty_lock);
Yan, Zheng0e294382016-07-04 18:06:41 +08003377 }
Sage Weilbe655592011-11-30 09:47:09 -08003378 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng0e294382016-07-04 18:06:41 +08003379 if (flushed) {
3380 ceph_put_snap_context(capsnap->context);
3381 ceph_put_cap_snap(capsnap);
Yan, Zhengc8799fc2016-07-07 15:22:38 +08003382 if (wake_ci)
3383 wake_up_all(&ci->i_cap_wq);
3384 if (wake_mdsc)
3385 wake_up_all(&mdsc->cap_flushing_wq);
Sage Weila8599bd2009-10-06 11:31:12 -07003386 iput(inode);
Yan, Zheng0e294382016-07-04 18:06:41 +08003387 }
Sage Weila8599bd2009-10-06 11:31:12 -07003388}
3389
3390/*
3391 * Handle TRUNC from MDS, indicating file truncation.
3392 *
3393 * caller hold s_mutex.
3394 */
3395static void handle_cap_trunc(struct inode *inode,
3396 struct ceph_mds_caps *trunc,
3397 struct ceph_mds_session *session)
Sage Weilbe655592011-11-30 09:47:09 -08003398 __releases(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07003399{
3400 struct ceph_inode_info *ci = ceph_inode(inode);
3401 int mds = session->s_mds;
3402 int seq = le32_to_cpu(trunc->seq);
3403 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
3404 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
3405 u64 size = le64_to_cpu(trunc->size);
3406 int implemented = 0;
3407 int dirty = __ceph_caps_dirty(ci);
3408 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
3409 int queue_trunc = 0;
3410
3411 issued |= implemented | dirty;
3412
3413 dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n",
3414 inode, mds, seq, truncate_size, truncate_seq);
3415 queue_trunc = ceph_fill_file_size(inode, issued,
3416 truncate_seq, truncate_size, size);
Sage Weilbe655592011-11-30 09:47:09 -08003417 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003418
Yan, Zheng14649752016-05-20 15:41:20 +08003419 if (queue_trunc)
Sage Weil3c6f6b72010-02-09 15:24:44 -08003420 ceph_queue_vmtruncate(inode);
Sage Weila8599bd2009-10-06 11:31:12 -07003421}
3422
3423/*
3424 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
3425 * different one. If we are the most recent migration we've seen (as
3426 * indicated by mseq), make note of the migrating cap bits for the
3427 * duration (until we see the corresponding IMPORT).
3428 *
3429 * caller holds s_mutex
3430 */
3431static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
Yan, Zheng11df2df2013-11-24 14:44:38 +08003432 struct ceph_mds_cap_peer *ph,
3433 struct ceph_mds_session *session)
Sage Weila8599bd2009-10-06 11:31:12 -07003434{
Sage Weildb354052011-05-24 11:46:31 -07003435 struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003436 struct ceph_mds_session *tsession = NULL;
Yan, Zhengd9df2782014-04-18 09:57:11 +08003437 struct ceph_cap *cap, *tcap, *new_cap = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07003438 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003439 u64 t_cap_id;
Sage Weila8599bd2009-10-06 11:31:12 -07003440 unsigned mseq = le32_to_cpu(ex->migrate_seq);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003441 unsigned t_seq, t_mseq;
3442 int target, issued;
3443 int mds = session->s_mds;
Sage Weila8599bd2009-10-06 11:31:12 -07003444
Yan, Zheng11df2df2013-11-24 14:44:38 +08003445 if (ph) {
3446 t_cap_id = le64_to_cpu(ph->cap_id);
3447 t_seq = le32_to_cpu(ph->seq);
3448 t_mseq = le32_to_cpu(ph->mseq);
3449 target = le32_to_cpu(ph->mds);
3450 } else {
3451 t_cap_id = t_seq = t_mseq = 0;
3452 target = -1;
Sage Weila8599bd2009-10-06 11:31:12 -07003453 }
3454
Yan, Zheng11df2df2013-11-24 14:44:38 +08003455 dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n",
3456 inode, ci, mds, mseq, target);
3457retry:
3458 spin_lock(&ci->i_ceph_lock);
3459 cap = __get_cap_for_mds(ci, mds);
Yan, Zhengca665e02014-04-21 15:46:37 +08003460 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
Yan, Zheng11df2df2013-11-24 14:44:38 +08003461 goto out_unlock;
Sage Weil154f42c2010-06-21 13:45:04 -07003462
Yan, Zheng11df2df2013-11-24 14:44:38 +08003463 if (target < 0) {
3464 __ceph_remove_cap(cap, false);
Yan, Zheng77310322016-04-08 15:27:16 +08003465 if (!ci->i_auth_cap)
3466 ci->i_ceph_flags |= CEPH_I_CAP_DROPPED;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003467 goto out_unlock;
3468 }
Sage Weildb354052011-05-24 11:46:31 -07003469
Yan, Zheng11df2df2013-11-24 14:44:38 +08003470 /*
3471 * now we know we haven't received the cap import message yet
3472 * because the exported cap still exist.
3473 */
3474
3475 issued = cap->issued;
Yan, Zhengd84b37f2018-01-03 11:16:27 +08003476 if (issued != cap->implemented)
3477 pr_err_ratelimited("handle_cap_export: issued != implemented: "
3478 "ino (%llx.%llx) mds%d seq %d mseq %d "
3479 "issued %s implemented %s\n",
3480 ceph_vinop(inode), mds, cap->seq, cap->mseq,
3481 ceph_cap_string(issued),
3482 ceph_cap_string(cap->implemented));
3483
Yan, Zheng11df2df2013-11-24 14:44:38 +08003484
3485 tcap = __get_cap_for_mds(ci, target);
3486 if (tcap) {
3487 /* already have caps from the target */
Yan, Zhengfa0aa3b2017-08-28 15:07:42 +08003488 if (tcap->cap_id == t_cap_id &&
Yan, Zheng11df2df2013-11-24 14:44:38 +08003489 ceph_seq_cmp(tcap->seq, t_seq) < 0) {
3490 dout(" updating import cap %p mds%d\n", tcap, target);
3491 tcap->cap_id = t_cap_id;
3492 tcap->seq = t_seq - 1;
3493 tcap->issue_seq = t_seq - 1;
3494 tcap->mseq = t_mseq;
3495 tcap->issued |= issued;
3496 tcap->implemented |= issued;
3497 if (cap == ci->i_auth_cap)
3498 ci->i_auth_cap = tcap;
Yan, Zheng00f06cb2017-01-24 10:02:32 +08003499
Yan, Zheng0e294382016-07-04 18:06:41 +08003500 if (!list_empty(&ci->i_cap_flush_list) &&
3501 ci->i_auth_cap == tcap) {
Yan, Zheng11df2df2013-11-24 14:44:38 +08003502 spin_lock(&mdsc->cap_dirty_lock);
3503 list_move_tail(&ci->i_flushing_item,
3504 &tcap->session->s_cap_flushing);
3505 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07003506 }
Sage Weila8599bd2009-10-06 11:31:12 -07003507 }
Yan, Zhenga096b092013-09-22 10:15:58 +08003508 __ceph_remove_cap(cap, false);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003509 goto out_unlock;
Yan, Zhengd9df2782014-04-18 09:57:11 +08003510 } else if (tsession) {
Yan, Zheng11df2df2013-11-24 14:44:38 +08003511 /* add placeholder for the export tagert */
Yan, Zhengd9df2782014-04-18 09:57:11 +08003512 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
Yan, Zheng00f06cb2017-01-24 10:02:32 +08003513 tcap = new_cap;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003514 ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0,
Yan, Zhengd9df2782014-04-18 09:57:11 +08003515 t_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
3516
Yan, Zheng00f06cb2017-01-24 10:02:32 +08003517 if (!list_empty(&ci->i_cap_flush_list) &&
3518 ci->i_auth_cap == tcap) {
3519 spin_lock(&mdsc->cap_dirty_lock);
3520 list_move_tail(&ci->i_flushing_item,
3521 &tcap->session->s_cap_flushing);
3522 spin_unlock(&mdsc->cap_dirty_lock);
3523 }
3524
Yan, Zhengd9df2782014-04-18 09:57:11 +08003525 __ceph_remove_cap(cap, false);
3526 goto out_unlock;
Yan, Zheng11df2df2013-11-24 14:44:38 +08003527 }
Sage Weila8599bd2009-10-06 11:31:12 -07003528
Sage Weilbe655592011-11-30 09:47:09 -08003529 spin_unlock(&ci->i_ceph_lock);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003530 mutex_unlock(&session->s_mutex);
3531
3532 /* open target session */
3533 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
3534 if (!IS_ERR(tsession)) {
3535 if (mds > target) {
3536 mutex_lock(&session->s_mutex);
3537 mutex_lock_nested(&tsession->s_mutex,
3538 SINGLE_DEPTH_NESTING);
3539 } else {
3540 mutex_lock(&tsession->s_mutex);
3541 mutex_lock_nested(&session->s_mutex,
3542 SINGLE_DEPTH_NESTING);
3543 }
Yan, Zhengd9df2782014-04-18 09:57:11 +08003544 new_cap = ceph_get_cap(mdsc, NULL);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003545 } else {
3546 WARN_ON(1);
3547 tsession = NULL;
3548 target = -1;
3549 }
3550 goto retry;
3551
3552out_unlock:
3553 spin_unlock(&ci->i_ceph_lock);
3554 mutex_unlock(&session->s_mutex);
3555 if (tsession) {
3556 mutex_unlock(&tsession->s_mutex);
3557 ceph_put_mds_session(tsession);
3558 }
Yan, Zhengd9df2782014-04-18 09:57:11 +08003559 if (new_cap)
3560 ceph_put_cap(mdsc, new_cap);
Sage Weila8599bd2009-10-06 11:31:12 -07003561}
3562
3563/*
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003564 * Handle cap IMPORT.
Sage Weila8599bd2009-10-06 11:31:12 -07003565 *
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003566 * caller holds s_mutex. acquires i_ceph_lock
Sage Weila8599bd2009-10-06 11:31:12 -07003567 */
3568static void handle_cap_import(struct ceph_mds_client *mdsc,
3569 struct inode *inode, struct ceph_mds_caps *im,
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003570 struct ceph_mds_cap_peer *ph,
Sage Weila8599bd2009-10-06 11:31:12 -07003571 struct ceph_mds_session *session,
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003572 struct ceph_cap **target_cap, int *old_issued)
3573 __acquires(ci->i_ceph_lock)
Sage Weila8599bd2009-10-06 11:31:12 -07003574{
3575 struct ceph_inode_info *ci = ceph_inode(inode);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003576 struct ceph_cap *cap, *ocap, *new_cap = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07003577 int mds = session->s_mds;
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003578 int issued;
3579 unsigned caps = le32_to_cpu(im->caps);
Sage Weila8599bd2009-10-06 11:31:12 -07003580 unsigned wanted = le32_to_cpu(im->wanted);
3581 unsigned seq = le32_to_cpu(im->seq);
3582 unsigned mseq = le32_to_cpu(im->migrate_seq);
3583 u64 realmino = le64_to_cpu(im->realm);
3584 u64 cap_id = le64_to_cpu(im->cap_id);
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003585 u64 p_cap_id;
3586 int peer;
Sage Weila8599bd2009-10-06 11:31:12 -07003587
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003588 if (ph) {
3589 p_cap_id = le64_to_cpu(ph->cap_id);
3590 peer = le32_to_cpu(ph->mds);
Sage Weila8599bd2009-10-06 11:31:12 -07003591 } else {
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003592 p_cap_id = 0;
3593 peer = -1;
Sage Weila8599bd2009-10-06 11:31:12 -07003594 }
3595
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003596 dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n",
3597 inode, ci, mds, mseq, peer);
3598
Yan, Zhengd9df2782014-04-18 09:57:11 +08003599retry:
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003600 spin_lock(&ci->i_ceph_lock);
Yan, Zhengd9df2782014-04-18 09:57:11 +08003601 cap = __get_cap_for_mds(ci, mds);
3602 if (!cap) {
3603 if (!new_cap) {
3604 spin_unlock(&ci->i_ceph_lock);
3605 new_cap = ceph_get_cap(mdsc, NULL);
3606 goto retry;
3607 }
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003608 cap = new_cap;
3609 } else {
3610 if (new_cap) {
3611 ceph_put_cap(mdsc, new_cap);
3612 new_cap = NULL;
3613 }
Yan, Zhengd9df2782014-04-18 09:57:11 +08003614 }
3615
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003616 __ceph_caps_issued(ci, &issued);
3617 issued |= __ceph_caps_dirty(ci);
3618
3619 ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq,
Yan, Zhengd9df2782014-04-18 09:57:11 +08003620 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
3621
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003622 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
3623 if (ocap && ocap->cap_id == p_cap_id) {
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003624 dout(" remove export cap %p mds%d flags %d\n",
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003625 ocap, peer, ph->flags);
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003626 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003627 (ocap->seq != le32_to_cpu(ph->seq) ||
3628 ocap->mseq != le32_to_cpu(ph->mseq))) {
Yan, Zhengd84b37f2018-01-03 11:16:27 +08003629 pr_err_ratelimited("handle_cap_import: "
3630 "mismatched seq/mseq: ino (%llx.%llx) "
3631 "mds%d seq %d mseq %d importer mds%d "
3632 "has peer seq %d mseq %d\n",
3633 ceph_vinop(inode), peer, ocap->seq,
3634 ocap->mseq, mds, le32_to_cpu(ph->seq),
3635 le32_to_cpu(ph->mseq));
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003636 }
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003637 __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003638 }
3639
3640 /* make sure we re-request max_size, if necessary */
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003641 ci->i_requested_max_size = 0;
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003642
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003643 *old_issued = issued;
3644 *target_cap = cap;
Sage Weila8599bd2009-10-06 11:31:12 -07003645}
3646
3647/*
3648 * Handle a caps message from the MDS.
3649 *
3650 * Identify the appropriate session, inode, and call the right handler
3651 * based on the cap op.
3652 */
3653void ceph_handle_caps(struct ceph_mds_session *session,
3654 struct ceph_msg *msg)
3655{
3656 struct ceph_mds_client *mdsc = session->s_mdsc;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003657 struct super_block *sb = mdsc->fsc->sb;
Sage Weila8599bd2009-10-06 11:31:12 -07003658 struct inode *inode;
Sage Weilbe655592011-11-30 09:47:09 -08003659 struct ceph_inode_info *ci;
Sage Weila8599bd2009-10-06 11:31:12 -07003660 struct ceph_cap *cap;
3661 struct ceph_mds_caps *h;
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003662 struct ceph_mds_cap_peer *peer = NULL;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003663 struct ceph_snap_realm *realm = NULL;
3664 struct ceph_string *pool_ns = NULL;
Sage Weil2600d2d2010-02-22 15:12:16 -08003665 int mds = session->s_mds;
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003666 int op, issued;
Sage Weil3d7ded42010-06-09 16:47:10 -07003667 u32 seq, mseq;
Sage Weila8599bd2009-10-06 11:31:12 -07003668 struct ceph_vino vino;
Sage Weil6df058c2009-12-22 11:24:33 -08003669 u64 tid;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003670 u64 inline_version = 0;
3671 void *inline_data = NULL;
3672 u32 inline_len = 0;
Sage Weil70edb552010-03-01 13:20:50 -08003673 void *snaptrace;
Sage Weilce1fbc82010-08-02 15:09:39 -07003674 size_t snaptrace_len;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003675 void *p, *end;
Sage Weila8599bd2009-10-06 11:31:12 -07003676
3677 dout("handle_caps from mds%d\n", mds);
3678
3679 /* decode */
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003680 end = msg->front.iov_base + msg->front.iov_len;
Sage Weil6df058c2009-12-22 11:24:33 -08003681 tid = le64_to_cpu(msg->hdr.tid);
Sage Weila8599bd2009-10-06 11:31:12 -07003682 if (msg->front.iov_len < sizeof(*h))
3683 goto bad;
3684 h = msg->front.iov_base;
3685 op = le32_to_cpu(h->op);
3686 vino.ino = le64_to_cpu(h->ino);
3687 vino.snap = CEPH_NOSNAP;
Sage Weila8599bd2009-10-06 11:31:12 -07003688 seq = le32_to_cpu(h->seq);
Sage Weil3d7ded42010-06-09 16:47:10 -07003689 mseq = le32_to_cpu(h->migrate_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07003690
Sage Weilce1fbc82010-08-02 15:09:39 -07003691 snaptrace = h + 1;
3692 snaptrace_len = le32_to_cpu(h->snap_trace_len);
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003693 p = snaptrace + snaptrace_len;
Sage Weilce1fbc82010-08-02 15:09:39 -07003694
3695 if (le16_to_cpu(msg->hdr.version) >= 2) {
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003696 u32 flock_len;
Sage Weilce1fbc82010-08-02 15:09:39 -07003697 ceph_decode_32_safe(&p, end, flock_len, bad);
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003698 if (p + flock_len > end)
3699 goto bad;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003700 p += flock_len;
Sage Weilce1fbc82010-08-02 15:09:39 -07003701 }
3702
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003703 if (le16_to_cpu(msg->hdr.version) >= 3) {
3704 if (op == CEPH_CAP_OP_IMPORT) {
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003705 if (p + sizeof(*peer) > end)
3706 goto bad;
3707 peer = p;
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003708 p += sizeof(*peer);
Yan, Zheng11df2df2013-11-24 14:44:38 +08003709 } else if (op == CEPH_CAP_OP_EXPORT) {
3710 /* recorded in unused fields */
3711 peer = (void *)&h->size;
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003712 }
3713 }
3714
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003715 if (le16_to_cpu(msg->hdr.version) >= 4) {
3716 ceph_decode_64_safe(&p, end, inline_version, bad);
3717 ceph_decode_32_safe(&p, end, inline_len, bad);
3718 if (p + inline_len > end)
3719 goto bad;
3720 inline_data = p;
3721 p += inline_len;
3722 }
3723
Jeff Layton92475f02017-04-13 11:07:04 -04003724 if (le16_to_cpu(msg->hdr.version) >= 5) {
3725 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
3726 u32 epoch_barrier;
3727
3728 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
3729 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
3730 }
3731
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003732 if (le16_to_cpu(msg->hdr.version) >= 8) {
3733 u64 flush_tid;
3734 u32 caller_uid, caller_gid;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003735 u32 pool_ns_len;
Jeff Layton92475f02017-04-13 11:07:04 -04003736
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003737 /* version >= 6 */
3738 ceph_decode_64_safe(&p, end, flush_tid, bad);
3739 /* version >= 7 */
3740 ceph_decode_32_safe(&p, end, caller_uid, bad);
3741 ceph_decode_32_safe(&p, end, caller_gid, bad);
3742 /* version >= 8 */
3743 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003744 if (pool_ns_len > 0) {
3745 ceph_decode_need(&p, end, pool_ns_len, bad);
3746 pool_ns = ceph_find_or_create_string(p, pool_ns_len);
3747 p += pool_ns_len;
3748 }
Yan, Zheng5ea5c5e2016-02-14 18:06:41 +08003749 }
3750
Yan, Zheng6cd3bca2014-09-17 07:45:12 +08003751 /* lookup ino */
3752 inode = ceph_find_inode(sb, vino);
3753 ci = ceph_inode(inode);
3754 dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino,
3755 vino.snap, inode);
3756
Sage Weila8599bd2009-10-06 11:31:12 -07003757 mutex_lock(&session->s_mutex);
3758 session->s_seq++;
3759 dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq,
3760 (unsigned)seq);
3761
Sage Weila8599bd2009-10-06 11:31:12 -07003762 if (!inode) {
3763 dout(" i don't have ino %llx\n", vino.ino);
Sage Weil3d7ded42010-06-09 16:47:10 -07003764
Yan, Zhenga096b092013-09-22 10:15:58 +08003765 if (op == CEPH_CAP_OP_IMPORT) {
Yan, Zheng745a8e32015-05-14 17:22:42 +08003766 cap = ceph_get_cap(mdsc, NULL);
3767 cap->cap_ino = vino.ino;
3768 cap->queue_release = 1;
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003769 cap->cap_id = le64_to_cpu(h->cap_id);
Yan, Zheng745a8e32015-05-14 17:22:42 +08003770 cap->mseq = mseq;
3771 cap->seq = seq;
Yan, Zhengdc24de82016-11-17 19:55:30 +08003772 cap->issue_seq = seq;
Yan, Zhenga096b092013-09-22 10:15:58 +08003773 spin_lock(&session->s_cap_lock);
Yan, Zheng745a8e32015-05-14 17:22:42 +08003774 list_add_tail(&cap->session_caps,
3775 &session->s_cap_releases);
3776 session->s_num_cap_releases++;
Yan, Zhenga096b092013-09-22 10:15:58 +08003777 spin_unlock(&session->s_cap_lock);
3778 }
Greg Farnum21b559d2010-10-06 15:46:30 -07003779 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07003780 }
3781
3782 /* these will work even if we don't have a cap yet */
3783 switch (op) {
3784 case CEPH_CAP_OP_FLUSHSNAP_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08003785 handle_cap_flushsnap_ack(inode, tid, h, session);
Sage Weila8599bd2009-10-06 11:31:12 -07003786 goto done;
3787
3788 case CEPH_CAP_OP_EXPORT:
Yan, Zheng11df2df2013-11-24 14:44:38 +08003789 handle_cap_export(inode, h, peer, session);
3790 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07003791
3792 case CEPH_CAP_OP_IMPORT:
Yan, Zheng982d6012014-12-23 15:30:54 +08003793 realm = NULL;
3794 if (snaptrace_len) {
3795 down_write(&mdsc->snap_rwsem);
3796 ceph_update_snap_trace(mdsc, snaptrace,
3797 snaptrace + snaptrace_len,
3798 false, &realm);
3799 downgrade_write(&mdsc->snap_rwsem);
3800 } else {
3801 down_read(&mdsc->snap_rwsem);
3802 }
Yan, Zheng4ee6a912013-11-24 14:43:46 +08003803 handle_cap_import(mdsc, inode, h, peer, session,
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003804 &cap, &issued);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003805 handle_cap_grant(mdsc, inode, h, &pool_ns,
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003806 inline_version, inline_data, inline_len,
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003807 msg->middle, session, cap, issued);
Yan, Zheng982d6012014-12-23 15:30:54 +08003808 if (realm)
3809 ceph_put_snap_realm(mdsc, realm);
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003810 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07003811 }
3812
3813 /* the rest require a cap */
Sage Weilbe655592011-11-30 09:47:09 -08003814 spin_lock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003815 cap = __get_cap_for_mds(ceph_inode(inode), mds);
3816 if (!cap) {
Sage Weil9dbd4122010-06-10 13:21:20 -07003817 dout(" no cap on %p ino %llx.%llx from mds%d\n",
Sage Weila8599bd2009-10-06 11:31:12 -07003818 inode, ceph_ino(inode), ceph_snap(inode), mds);
Sage Weilbe655592011-11-30 09:47:09 -08003819 spin_unlock(&ci->i_ceph_lock);
Greg Farnum21b559d2010-10-06 15:46:30 -07003820 goto flush_cap_releases;
Sage Weila8599bd2009-10-06 11:31:12 -07003821 }
3822
Sage Weilbe655592011-11-30 09:47:09 -08003823 /* note that each of these drops i_ceph_lock for us */
Sage Weila8599bd2009-10-06 11:31:12 -07003824 switch (op) {
3825 case CEPH_CAP_OP_REVOKE:
3826 case CEPH_CAP_OP_GRANT:
Yan, Zheng2cd698b2014-04-18 13:20:27 +08003827 __ceph_caps_issued(ci, &issued);
3828 issued |= __ceph_caps_dirty(ci);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003829 handle_cap_grant(mdsc, inode, h, &pool_ns,
Yan, Zhengfb01d1f2014-11-14 21:29:55 +08003830 inline_version, inline_data, inline_len,
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003831 msg->middle, session, cap, issued);
Sage Weil15637c82010-03-16 13:42:00 -07003832 goto done_unlocked;
Sage Weila8599bd2009-10-06 11:31:12 -07003833
3834 case CEPH_CAP_OP_FLUSH_ACK:
Sage Weil6df058c2009-12-22 11:24:33 -08003835 handle_cap_flush_ack(inode, tid, h, session, cap);
Sage Weila8599bd2009-10-06 11:31:12 -07003836 break;
3837
3838 case CEPH_CAP_OP_TRUNC:
3839 handle_cap_trunc(inode, h, session);
3840 break;
3841
3842 default:
Sage Weilbe655592011-11-30 09:47:09 -08003843 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003844 pr_err("ceph_handle_caps: unknown cap op %d %s\n", op,
3845 ceph_cap_op_name(op));
3846 }
3847
Greg Farnum21b559d2010-10-06 15:46:30 -07003848 goto done;
3849
3850flush_cap_releases:
3851 /*
Yan, Zheng745a8e32015-05-14 17:22:42 +08003852 * send any cap release message to try to move things
Greg Farnum21b559d2010-10-06 15:46:30 -07003853 * along for the mds (who clearly thinks we still have this
3854 * cap).
3855 */
Greg Farnum21b559d2010-10-06 15:46:30 -07003856 ceph_send_cap_releases(mdsc, session);
3857
Sage Weila8599bd2009-10-06 11:31:12 -07003858done:
Sage Weil15637c82010-03-16 13:42:00 -07003859 mutex_unlock(&session->s_mutex);
3860done_unlocked:
SF Markus Elfringe96a6502014-11-02 15:20:59 +01003861 iput(inode);
Yan, Zheng779fe0f2016-03-07 09:35:06 +08003862 ceph_put_string(pool_ns);
Sage Weila8599bd2009-10-06 11:31:12 -07003863 return;
3864
3865bad:
3866 pr_err("ceph_handle_caps: corrupt message\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08003867 ceph_msg_dump(msg);
Sage Weila8599bd2009-10-06 11:31:12 -07003868 return;
3869}
3870
3871/*
3872 * Delayed work handler to process end of delayed cap release LRU list.
3873 */
Sage Weilafcdaea2009-10-14 14:27:38 -07003874void ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
Sage Weila8599bd2009-10-06 11:31:12 -07003875{
Yan, Zheng4b9f2042017-06-27 17:17:24 +08003876 struct inode *inode;
Sage Weila8599bd2009-10-06 11:31:12 -07003877 struct ceph_inode_info *ci;
3878 int flags = CHECK_CAPS_NODELAY;
3879
Sage Weila8599bd2009-10-06 11:31:12 -07003880 dout("check_delayed_caps\n");
3881 while (1) {
3882 spin_lock(&mdsc->cap_delay_lock);
3883 if (list_empty(&mdsc->cap_delay_list))
3884 break;
3885 ci = list_first_entry(&mdsc->cap_delay_list,
3886 struct ceph_inode_info,
3887 i_cap_delay_list);
3888 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
3889 time_before(jiffies, ci->i_hold_caps_max))
3890 break;
3891 list_del_init(&ci->i_cap_delay_list);
Yan, Zheng4b9f2042017-06-27 17:17:24 +08003892
3893 inode = igrab(&ci->vfs_inode);
Sage Weila8599bd2009-10-06 11:31:12 -07003894 spin_unlock(&mdsc->cap_delay_lock);
Yan, Zheng4b9f2042017-06-27 17:17:24 +08003895
3896 if (inode) {
3897 dout("check_delayed_caps on %p\n", inode);
3898 ceph_check_caps(ci, flags, NULL);
3899 iput(inode);
3900 }
Sage Weila8599bd2009-10-06 11:31:12 -07003901 }
3902 spin_unlock(&mdsc->cap_delay_lock);
3903}
3904
3905/*
Sage Weilafcdaea2009-10-14 14:27:38 -07003906 * Flush all dirty caps to the mds
3907 */
3908void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
3909{
Sage Weildb354052011-05-24 11:46:31 -07003910 struct ceph_inode_info *ci;
3911 struct inode *inode;
Sage Weilafcdaea2009-10-14 14:27:38 -07003912
3913 dout("flush_dirty_caps\n");
3914 spin_lock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07003915 while (!list_empty(&mdsc->cap_dirty)) {
3916 ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info,
3917 i_dirty_item);
Sage Weil70b666c2011-05-27 09:24:26 -07003918 inode = &ci->vfs_inode;
3919 ihold(inode);
Sage Weildb354052011-05-24 11:46:31 -07003920 dout("flush_dirty_caps %p\n", inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07003921 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weil70b666c2011-05-27 09:24:26 -07003922 ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL);
3923 iput(inode);
Sage Weilafcdaea2009-10-14 14:27:38 -07003924 spin_lock(&mdsc->cap_dirty_lock);
3925 }
3926 spin_unlock(&mdsc->cap_dirty_lock);
Sage Weildb354052011-05-24 11:46:31 -07003927 dout("flush_dirty_caps done\n");
Sage Weilafcdaea2009-10-14 14:27:38 -07003928}
3929
Yan, Zheng774a6a12016-06-06 16:01:39 +08003930void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode)
3931{
3932 int i;
3933 int bits = (fmode << 1) | 1;
3934 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3935 if (bits & (1 << i))
3936 ci->i_nr_by_mode[i]++;
3937 }
3938}
3939
Sage Weilafcdaea2009-10-14 14:27:38 -07003940/*
Sage Weila8599bd2009-10-06 11:31:12 -07003941 * Drop open file reference. If we were the last open file,
3942 * we may need to release capabilities to the MDS (or schedule
3943 * their delayed release).
3944 */
3945void ceph_put_fmode(struct ceph_inode_info *ci, int fmode)
3946{
Yan, Zheng774a6a12016-06-06 16:01:39 +08003947 int i, last = 0;
3948 int bits = (fmode << 1) | 1;
Sage Weilbe655592011-11-30 09:47:09 -08003949 spin_lock(&ci->i_ceph_lock);
Yan, Zheng774a6a12016-06-06 16:01:39 +08003950 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
3951 if (bits & (1 << i)) {
3952 BUG_ON(ci->i_nr_by_mode[i] == 0);
3953 if (--ci->i_nr_by_mode[i] == 0)
3954 last++;
3955 }
3956 }
3957 dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n",
3958 &ci->vfs_inode, fmode,
3959 ci->i_nr_by_mode[0], ci->i_nr_by_mode[1],
3960 ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]);
Sage Weilbe655592011-11-30 09:47:09 -08003961 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07003962
3963 if (last && ci->i_vino.snap == CEPH_NOSNAP)
3964 ceph_check_caps(ci, 0, NULL);
3965}
3966
3967/*
3968 * Helpers for embedding cap and dentry lease releases into mds
3969 * requests.
3970 *
3971 * @force is used by dentry_release (below) to force inclusion of a
3972 * record for the directory inode, even when there aren't any caps to
3973 * drop.
3974 */
3975int ceph_encode_inode_release(void **p, struct inode *inode,
3976 int mds, int drop, int unless, int force)
3977{
3978 struct ceph_inode_info *ci = ceph_inode(inode);
3979 struct ceph_cap *cap;
3980 struct ceph_mds_request_release *rel = *p;
Sage Weilec97f882010-06-24 15:12:37 -07003981 int used, dirty;
Sage Weila8599bd2009-10-06 11:31:12 -07003982 int ret = 0;
Sage Weila8599bd2009-10-06 11:31:12 -07003983
Sage Weilbe655592011-11-30 09:47:09 -08003984 spin_lock(&ci->i_ceph_lock);
Sage Weil916623d2010-03-16 15:01:07 -07003985 used = __ceph_caps_used(ci);
Sage Weilec97f882010-06-24 15:12:37 -07003986 dirty = __ceph_caps_dirty(ci);
Sage Weil916623d2010-03-16 15:01:07 -07003987
Sage Weilec97f882010-06-24 15:12:37 -07003988 dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n",
3989 inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop),
Sage Weil916623d2010-03-16 15:01:07 -07003990 ceph_cap_string(unless));
3991
Sage Weilec97f882010-06-24 15:12:37 -07003992 /* only drop unused, clean caps */
3993 drop &= ~(used | dirty);
Sage Weil916623d2010-03-16 15:01:07 -07003994
Sage Weila8599bd2009-10-06 11:31:12 -07003995 cap = __get_cap_for_mds(ci, mds);
3996 if (cap && __cap_is_valid(cap)) {
Yan, Zheng222b7f92017-11-23 17:47:15 +08003997 unless &= cap->issued;
3998 if (unless) {
3999 if (unless & CEPH_CAP_AUTH_EXCL)
4000 drop &= ~CEPH_CAP_AUTH_SHARED;
4001 if (unless & CEPH_CAP_LINK_EXCL)
4002 drop &= ~CEPH_CAP_LINK_SHARED;
4003 if (unless & CEPH_CAP_XATTR_EXCL)
4004 drop &= ~CEPH_CAP_XATTR_SHARED;
4005 if (unless & CEPH_CAP_FILE_EXCL)
4006 drop &= ~CEPH_CAP_FILE_SHARED;
4007 }
4008
4009 if (force || (cap->issued & drop)) {
4010 if (cap->issued & drop) {
Yan, Zhengbb137f82013-06-03 18:22:17 +08004011 int wanted = __ceph_caps_wanted(ci);
4012 if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0)
4013 wanted |= cap->mds_wanted;
4014 dout("encode_inode_release %p cap %p "
4015 "%s -> %s, wanted %s -> %s\n", inode, cap,
Sage Weila8599bd2009-10-06 11:31:12 -07004016 ceph_cap_string(cap->issued),
Yan, Zhengbb137f82013-06-03 18:22:17 +08004017 ceph_cap_string(cap->issued & ~drop),
4018 ceph_cap_string(cap->mds_wanted),
4019 ceph_cap_string(wanted));
4020
Sage Weila8599bd2009-10-06 11:31:12 -07004021 cap->issued &= ~drop;
4022 cap->implemented &= ~drop;
Yan, Zhengbb137f82013-06-03 18:22:17 +08004023 cap->mds_wanted = wanted;
Sage Weila8599bd2009-10-06 11:31:12 -07004024 } else {
4025 dout("encode_inode_release %p cap %p %s"
4026 " (force)\n", inode, cap,
4027 ceph_cap_string(cap->issued));
4028 }
4029
4030 rel->ino = cpu_to_le64(ceph_ino(inode));
4031 rel->cap_id = cpu_to_le64(cap->cap_id);
4032 rel->seq = cpu_to_le32(cap->seq);
Himangi Saraogi08a0f242014-07-23 20:11:11 +05304033 rel->issue_seq = cpu_to_le32(cap->issue_seq);
Sage Weila8599bd2009-10-06 11:31:12 -07004034 rel->mseq = cpu_to_le32(cap->mseq);
Yan, Zhengfd7b95c2014-04-17 08:02:02 +08004035 rel->caps = cpu_to_le32(cap->implemented);
Sage Weila8599bd2009-10-06 11:31:12 -07004036 rel->wanted = cpu_to_le32(cap->mds_wanted);
4037 rel->dname_len = 0;
4038 rel->dname_seq = 0;
4039 *p += sizeof(*rel);
4040 ret = 1;
4041 } else {
Yan, Zheng222b7f92017-11-23 17:47:15 +08004042 dout("encode_inode_release %p cap %p %s (noop)\n",
Sage Weila8599bd2009-10-06 11:31:12 -07004043 inode, cap, ceph_cap_string(cap->issued));
4044 }
4045 }
Sage Weilbe655592011-11-30 09:47:09 -08004046 spin_unlock(&ci->i_ceph_lock);
Sage Weila8599bd2009-10-06 11:31:12 -07004047 return ret;
4048}
4049
4050int ceph_encode_dentry_release(void **p, struct dentry *dentry,
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004051 struct inode *dir,
Sage Weila8599bd2009-10-06 11:31:12 -07004052 int mds, int drop, int unless)
4053{
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004054 struct dentry *parent = NULL;
Sage Weila8599bd2009-10-06 11:31:12 -07004055 struct ceph_mds_request_release *rel = *p;
4056 struct ceph_dentry_info *di = ceph_dentry(dentry);
4057 int force = 0;
4058 int ret;
4059
4060 /*
4061 * force an record for the directory caps if we have a dentry lease.
Sage Weilbe655592011-11-30 09:47:09 -08004062 * this is racy (can't take i_ceph_lock and d_lock together), but it
Sage Weila8599bd2009-10-06 11:31:12 -07004063 * doesn't have to be perfect; the mds will revoke anything we don't
4064 * release.
4065 */
4066 spin_lock(&dentry->d_lock);
4067 if (di->lease_session && di->lease_session->s_mds == mds)
4068 force = 1;
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004069 if (!dir) {
4070 parent = dget(dentry->d_parent);
4071 dir = d_inode(parent);
4072 }
Sage Weila8599bd2009-10-06 11:31:12 -07004073 spin_unlock(&dentry->d_lock);
4074
Jeff Laytonca6c8ae2016-12-15 08:37:59 -05004075 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
Jeff Laytonadf0d682016-12-15 08:37:58 -05004076 dput(parent);
Sage Weila8599bd2009-10-06 11:31:12 -07004077
4078 spin_lock(&dentry->d_lock);
4079 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4080 dout("encode_dentry_release %p mds%d seq %d\n",
4081 dentry, mds, (int)di->lease_seq);
4082 rel->dname_len = cpu_to_le32(dentry->d_name.len);
4083 memcpy(*p, dentry->d_name.name, dentry->d_name.len);
4084 *p += dentry->d_name.len;
4085 rel->dname_seq = cpu_to_le32(di->lease_seq);
Sage Weil1dadcce2010-07-23 13:54:21 -07004086 __ceph_mdsc_drop_dentry_lease(dentry);
Sage Weila8599bd2009-10-06 11:31:12 -07004087 }
4088 spin_unlock(&dentry->d_lock);
4089 return ret;
4090}