]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
3d14c5d2 | 2 | #include <linux/ceph/ceph_debug.h> |
a8599bd8 SW |
3 | |
4 | #include <linux/fs.h> | |
5 | #include <linux/kernel.h> | |
174cd4b1 | 6 | #include <linux/sched/signal.h> |
5a0e3ad6 | 7 | #include <linux/slab.h> |
a8599bd8 SW |
8 | #include <linux/vmalloc.h> |
9 | #include <linux/wait.h> | |
f1a3d572 | 10 | #include <linux/writeback.h> |
176c77c9 | 11 | #include <linux/iversion.h> |
5970e15d | 12 | #include <linux/filelock.h> |
a8599bd8 SW |
13 | |
14 | #include "super.h" | |
3d14c5d2 | 15 | #include "mds_client.h" |
99ccbd22 | 16 | #include "cache.h" |
2d332d5b | 17 | #include "crypto.h" |
3d14c5d2 YS |
18 | #include <linux/ceph/decode.h> |
19 | #include <linux/ceph/messenger.h> | |
a8599bd8 SW |
20 | |
21 | /* | |
22 | * Capability management | |
23 | * | |
24 | * The Ceph metadata servers control client access to inode metadata | |
25 | * and file data by issuing capabilities, granting clients permission | |
26 | * to read and/or write both inode field and file data to OSDs | |
27 | * (storage nodes). Each capability consists of a set of bits | |
28 | * indicating which operations are allowed. | |
29 | * | |
30 | * If the client holds a *_SHARED cap, the client has a coherent value | |
31 | * that can be safely read from the cached inode. | |
32 | * | |
33 | * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the | |
34 | * client is allowed to change inode attributes (e.g., file size, | |
35 | * mtime), note its dirty state in the ceph_cap, and asynchronously | |
36 | * flush that metadata change to the MDS. | |
37 | * | |
38 | * In the event of a conflicting operation (perhaps by another | |
39 | * client), the MDS will revoke the conflicting client capabilities. | |
40 | * | |
41 | * In order for a client to cache an inode, it must hold a capability | |
42 | * with at least one MDS server. When inodes are released, release | |
43 | * notifications are batched and periodically sent en masse to the MDS | |
44 | * cluster to release server state. | |
45 | */ | |
46 | ||
0e294387 | 47 | static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc); |
7bc00fdd YZ |
48 | static void __kick_flushing_caps(struct ceph_mds_client *mdsc, |
49 | struct ceph_mds_session *session, | |
50 | struct ceph_inode_info *ci, | |
51 | u64 oldest_flush_tid); | |
a8599bd8 SW |
52 | |
53 | /* | |
54 | * Generate readable cap strings for debugging output. | |
55 | */ | |
56 | #define MAX_CAP_STR 20 | |
57 | static char cap_str[MAX_CAP_STR][40]; | |
58 | static DEFINE_SPINLOCK(cap_str_lock); | |
59 | static int last_cap_str; | |
60 | ||
61 | static char *gcap_string(char *s, int c) | |
62 | { | |
63 | if (c & CEPH_CAP_GSHARED) | |
64 | *s++ = 's'; | |
65 | if (c & CEPH_CAP_GEXCL) | |
66 | *s++ = 'x'; | |
67 | if (c & CEPH_CAP_GCACHE) | |
68 | *s++ = 'c'; | |
69 | if (c & CEPH_CAP_GRD) | |
70 | *s++ = 'r'; | |
71 | if (c & CEPH_CAP_GWR) | |
72 | *s++ = 'w'; | |
73 | if (c & CEPH_CAP_GBUFFER) | |
74 | *s++ = 'b'; | |
49a9f4f6 YZ |
75 | if (c & CEPH_CAP_GWREXTEND) |
76 | *s++ = 'a'; | |
a8599bd8 SW |
77 | if (c & CEPH_CAP_GLAZYIO) |
78 | *s++ = 'l'; | |
79 | return s; | |
80 | } | |
81 | ||
82 | const char *ceph_cap_string(int caps) | |
83 | { | |
84 | int i; | |
85 | char *s; | |
86 | int c; | |
87 | ||
88 | spin_lock(&cap_str_lock); | |
89 | i = last_cap_str++; | |
90 | if (last_cap_str == MAX_CAP_STR) | |
91 | last_cap_str = 0; | |
92 | spin_unlock(&cap_str_lock); | |
93 | ||
94 | s = cap_str[i]; | |
95 | ||
96 | if (caps & CEPH_CAP_PIN) | |
97 | *s++ = 'p'; | |
98 | ||
99 | c = (caps >> CEPH_CAP_SAUTH) & 3; | |
100 | if (c) { | |
101 | *s++ = 'A'; | |
102 | s = gcap_string(s, c); | |
103 | } | |
104 | ||
105 | c = (caps >> CEPH_CAP_SLINK) & 3; | |
106 | if (c) { | |
107 | *s++ = 'L'; | |
108 | s = gcap_string(s, c); | |
109 | } | |
110 | ||
111 | c = (caps >> CEPH_CAP_SXATTR) & 3; | |
112 | if (c) { | |
113 | *s++ = 'X'; | |
114 | s = gcap_string(s, c); | |
115 | } | |
116 | ||
117 | c = caps >> CEPH_CAP_SFILE; | |
118 | if (c) { | |
119 | *s++ = 'F'; | |
120 | s = gcap_string(s, c); | |
121 | } | |
122 | ||
123 | if (s == cap_str[i]) | |
124 | *s++ = '-'; | |
125 | *s = 0; | |
126 | return cap_str[i]; | |
127 | } | |
128 | ||
37151668 | 129 | void ceph_caps_init(struct ceph_mds_client *mdsc) |
a8599bd8 | 130 | { |
37151668 YS |
131 | INIT_LIST_HEAD(&mdsc->caps_list); |
132 | spin_lock_init(&mdsc->caps_list_lock); | |
a8599bd8 SW |
133 | } |
134 | ||
37151668 | 135 | void ceph_caps_finalize(struct ceph_mds_client *mdsc) |
a8599bd8 SW |
136 | { |
137 | struct ceph_cap *cap; | |
138 | ||
37151668 YS |
139 | spin_lock(&mdsc->caps_list_lock); |
140 | while (!list_empty(&mdsc->caps_list)) { | |
141 | cap = list_first_entry(&mdsc->caps_list, | |
142 | struct ceph_cap, caps_item); | |
a8599bd8 SW |
143 | list_del(&cap->caps_item); |
144 | kmem_cache_free(ceph_cap_cachep, cap); | |
145 | } | |
37151668 YS |
146 | mdsc->caps_total_count = 0; |
147 | mdsc->caps_avail_count = 0; | |
148 | mdsc->caps_use_count = 0; | |
149 | mdsc->caps_reserve_count = 0; | |
150 | mdsc->caps_min_count = 0; | |
151 | spin_unlock(&mdsc->caps_list_lock); | |
85ccce43 SW |
152 | } |
153 | ||
fe33032d YZ |
154 | void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc, |
155 | struct ceph_mount_options *fsopt) | |
85ccce43 | 156 | { |
37151668 | 157 | spin_lock(&mdsc->caps_list_lock); |
fe33032d YZ |
158 | mdsc->caps_min_count = fsopt->max_readdir; |
159 | if (mdsc->caps_min_count < 1024) | |
160 | mdsc->caps_min_count = 1024; | |
161 | mdsc->caps_use_max = fsopt->caps_max; | |
162 | if (mdsc->caps_use_max > 0 && | |
163 | mdsc->caps_use_max < mdsc->caps_min_count) | |
164 | mdsc->caps_use_max = mdsc->caps_min_count; | |
37151668 | 165 | spin_unlock(&mdsc->caps_list_lock); |
a8599bd8 SW |
166 | } |
167 | ||
7bf8f736 CX |
168 | static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps) |
169 | { | |
170 | struct ceph_cap *cap; | |
171 | int i; | |
172 | ||
173 | if (nr_caps) { | |
174 | BUG_ON(mdsc->caps_reserve_count < nr_caps); | |
175 | mdsc->caps_reserve_count -= nr_caps; | |
176 | if (mdsc->caps_avail_count >= | |
177 | mdsc->caps_reserve_count + mdsc->caps_min_count) { | |
178 | mdsc->caps_total_count -= nr_caps; | |
179 | for (i = 0; i < nr_caps; i++) { | |
180 | cap = list_first_entry(&mdsc->caps_list, | |
181 | struct ceph_cap, caps_item); | |
182 | list_del(&cap->caps_item); | |
183 | kmem_cache_free(ceph_cap_cachep, cap); | |
184 | } | |
185 | } else { | |
186 | mdsc->caps_avail_count += nr_caps; | |
187 | } | |
188 | ||
38d46409 XL |
189 | doutc(mdsc->fsc->client, |
190 | "caps %d = %d used + %d resv + %d avail\n", | |
191 | mdsc->caps_total_count, mdsc->caps_use_count, | |
192 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
7bf8f736 CX |
193 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
194 | mdsc->caps_reserve_count + | |
195 | mdsc->caps_avail_count); | |
196 | } | |
197 | } | |
198 | ||
e30ee581 ZZ |
199 | /* |
200 | * Called under mdsc->mutex. | |
201 | */ | |
202 | int ceph_reserve_caps(struct ceph_mds_client *mdsc, | |
37151668 | 203 | struct ceph_cap_reservation *ctx, int need) |
a8599bd8 | 204 | { |
38d46409 | 205 | struct ceph_client *cl = mdsc->fsc->client; |
e30ee581 | 206 | int i, j; |
a8599bd8 SW |
207 | struct ceph_cap *cap; |
208 | int have; | |
209 | int alloc = 0; | |
e30ee581 | 210 | int max_caps; |
e5bc08d0 | 211 | int err = 0; |
e30ee581 ZZ |
212 | bool trimmed = false; |
213 | struct ceph_mds_session *s; | |
a8599bd8 | 214 | LIST_HEAD(newcaps); |
a8599bd8 | 215 | |
38d46409 | 216 | doutc(cl, "ctx=%p need=%d\n", ctx, need); |
a8599bd8 SW |
217 | |
218 | /* first reserve any caps that are already allocated */ | |
37151668 YS |
219 | spin_lock(&mdsc->caps_list_lock); |
220 | if (mdsc->caps_avail_count >= need) | |
a8599bd8 SW |
221 | have = need; |
222 | else | |
37151668 YS |
223 | have = mdsc->caps_avail_count; |
224 | mdsc->caps_avail_count -= have; | |
225 | mdsc->caps_reserve_count += have; | |
226 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + | |
227 | mdsc->caps_reserve_count + | |
228 | mdsc->caps_avail_count); | |
229 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 | 230 | |
79cd674a | 231 | for (i = have; i < need; ) { |
a8599bd8 | 232 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
79cd674a CX |
233 | if (cap) { |
234 | list_add(&cap->caps_item, &newcaps); | |
235 | alloc++; | |
236 | i++; | |
237 | continue; | |
238 | } | |
239 | ||
240 | if (!trimmed) { | |
241 | for (j = 0; j < mdsc->max_sessions; j++) { | |
242 | s = __ceph_lookup_mds_session(mdsc, j); | |
243 | if (!s) | |
244 | continue; | |
245 | mutex_unlock(&mdsc->mutex); | |
246 | ||
247 | mutex_lock(&s->s_mutex); | |
248 | max_caps = s->s_nr_caps - (need - i); | |
249 | ceph_trim_caps(mdsc, s, max_caps); | |
250 | mutex_unlock(&s->s_mutex); | |
251 | ||
252 | ceph_put_mds_session(s); | |
253 | mutex_lock(&mdsc->mutex); | |
e30ee581 | 254 | } |
79cd674a CX |
255 | trimmed = true; |
256 | ||
257 | spin_lock(&mdsc->caps_list_lock); | |
258 | if (mdsc->caps_avail_count) { | |
259 | int more_have; | |
260 | if (mdsc->caps_avail_count >= need - i) | |
261 | more_have = need - i; | |
262 | else | |
263 | more_have = mdsc->caps_avail_count; | |
264 | ||
265 | i += more_have; | |
266 | have += more_have; | |
267 | mdsc->caps_avail_count -= more_have; | |
268 | mdsc->caps_reserve_count += more_have; | |
269 | ||
270 | } | |
271 | spin_unlock(&mdsc->caps_list_lock); | |
272 | ||
273 | continue; | |
e30ee581 | 274 | } |
79cd674a | 275 | |
38d46409 XL |
276 | pr_warn_client(cl, "ctx=%p ENOMEM need=%d got=%d\n", ctx, need, |
277 | have + alloc); | |
e5bc08d0 CX |
278 | err = -ENOMEM; |
279 | break; | |
280 | } | |
281 | ||
282 | if (!err) { | |
283 | BUG_ON(have + alloc != need); | |
284 | ctx->count = need; | |
fe33032d | 285 | ctx->used = 0; |
a8599bd8 | 286 | } |
a8599bd8 | 287 | |
37151668 YS |
288 | spin_lock(&mdsc->caps_list_lock); |
289 | mdsc->caps_total_count += alloc; | |
290 | mdsc->caps_reserve_count += alloc; | |
291 | list_splice(&newcaps, &mdsc->caps_list); | |
a8599bd8 | 292 | |
37151668 YS |
293 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
294 | mdsc->caps_reserve_count + | |
295 | mdsc->caps_avail_count); | |
e5bc08d0 CX |
296 | |
297 | if (err) | |
298 | __ceph_unreserve_caps(mdsc, have + alloc); | |
299 | ||
37151668 | 300 | spin_unlock(&mdsc->caps_list_lock); |
a8599bd8 | 301 | |
38d46409 XL |
302 | doutc(cl, "ctx=%p %d = %d used + %d resv + %d avail\n", ctx, |
303 | mdsc->caps_total_count, mdsc->caps_use_count, | |
304 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
e5bc08d0 | 305 | return err; |
a8599bd8 SW |
306 | } |
307 | ||
7bf8f736 | 308 | void ceph_unreserve_caps(struct ceph_mds_client *mdsc, |
fe33032d | 309 | struct ceph_cap_reservation *ctx) |
a8599bd8 | 310 | { |
38d46409 | 311 | struct ceph_client *cl = mdsc->fsc->client; |
fe33032d YZ |
312 | bool reclaim = false; |
313 | if (!ctx->count) | |
314 | return; | |
315 | ||
38d46409 | 316 | doutc(cl, "ctx=%p count=%d\n", ctx, ctx->count); |
7bf8f736 CX |
317 | spin_lock(&mdsc->caps_list_lock); |
318 | __ceph_unreserve_caps(mdsc, ctx->count); | |
319 | ctx->count = 0; | |
fe33032d YZ |
320 | |
321 | if (mdsc->caps_use_max > 0 && | |
322 | mdsc->caps_use_count > mdsc->caps_use_max) | |
323 | reclaim = true; | |
7bf8f736 | 324 | spin_unlock(&mdsc->caps_list_lock); |
fe33032d YZ |
325 | |
326 | if (reclaim) | |
327 | ceph_reclaim_caps_nr(mdsc, ctx->used); | |
a8599bd8 SW |
328 | } |
329 | ||
d9df2783 YZ |
330 | struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc, |
331 | struct ceph_cap_reservation *ctx) | |
a8599bd8 | 332 | { |
38d46409 | 333 | struct ceph_client *cl = mdsc->fsc->client; |
a8599bd8 SW |
334 | struct ceph_cap *cap = NULL; |
335 | ||
336 | /* temporary, until we do something about cap import/export */ | |
443b3760 SW |
337 | if (!ctx) { |
338 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); | |
339 | if (cap) { | |
4d1d0534 | 340 | spin_lock(&mdsc->caps_list_lock); |
37151668 YS |
341 | mdsc->caps_use_count++; |
342 | mdsc->caps_total_count++; | |
4d1d0534 | 343 | spin_unlock(&mdsc->caps_list_lock); |
e327ce06 CX |
344 | } else { |
345 | spin_lock(&mdsc->caps_list_lock); | |
346 | if (mdsc->caps_avail_count) { | |
347 | BUG_ON(list_empty(&mdsc->caps_list)); | |
348 | ||
349 | mdsc->caps_avail_count--; | |
350 | mdsc->caps_use_count++; | |
351 | cap = list_first_entry(&mdsc->caps_list, | |
352 | struct ceph_cap, caps_item); | |
353 | list_del(&cap->caps_item); | |
354 | ||
355 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + | |
356 | mdsc->caps_reserve_count + mdsc->caps_avail_count); | |
357 | } | |
358 | spin_unlock(&mdsc->caps_list_lock); | |
443b3760 | 359 | } |
e327ce06 | 360 | |
443b3760 SW |
361 | return cap; |
362 | } | |
a8599bd8 | 363 | |
37151668 | 364 | spin_lock(&mdsc->caps_list_lock); |
38d46409 XL |
365 | doutc(cl, "ctx=%p (%d) %d = %d used + %d resv + %d avail\n", ctx, |
366 | ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, | |
367 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
a8599bd8 | 368 | BUG_ON(!ctx->count); |
37151668 YS |
369 | BUG_ON(ctx->count > mdsc->caps_reserve_count); |
370 | BUG_ON(list_empty(&mdsc->caps_list)); | |
a8599bd8 SW |
371 | |
372 | ctx->count--; | |
fe33032d | 373 | ctx->used++; |
37151668 YS |
374 | mdsc->caps_reserve_count--; |
375 | mdsc->caps_use_count++; | |
a8599bd8 | 376 | |
37151668 | 377 | cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); |
a8599bd8 SW |
378 | list_del(&cap->caps_item); |
379 | ||
37151668 YS |
380 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
381 | mdsc->caps_reserve_count + mdsc->caps_avail_count); | |
382 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
383 | return cap; |
384 | } | |
385 | ||
37151668 | 386 | void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap) |
a8599bd8 | 387 | { |
38d46409 XL |
388 | struct ceph_client *cl = mdsc->fsc->client; |
389 | ||
37151668 | 390 | spin_lock(&mdsc->caps_list_lock); |
38d46409 XL |
391 | doutc(cl, "%p %d = %d used + %d resv + %d avail\n", cap, |
392 | mdsc->caps_total_count, mdsc->caps_use_count, | |
393 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
37151668 | 394 | mdsc->caps_use_count--; |
a8599bd8 | 395 | /* |
85ccce43 SW |
396 | * Keep some preallocated caps around (ceph_min_count), to |
397 | * avoid lots of free/alloc churn. | |
a8599bd8 | 398 | */ |
37151668 YS |
399 | if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + |
400 | mdsc->caps_min_count) { | |
401 | mdsc->caps_total_count--; | |
a8599bd8 SW |
402 | kmem_cache_free(ceph_cap_cachep, cap); |
403 | } else { | |
37151668 YS |
404 | mdsc->caps_avail_count++; |
405 | list_add(&cap->caps_item, &mdsc->caps_list); | |
a8599bd8 SW |
406 | } |
407 | ||
37151668 YS |
408 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
409 | mdsc->caps_reserve_count + mdsc->caps_avail_count); | |
410 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
411 | } |
412 | ||
3d14c5d2 | 413 | void ceph_reservation_status(struct ceph_fs_client *fsc, |
85ccce43 SW |
414 | int *total, int *avail, int *used, int *reserved, |
415 | int *min) | |
a8599bd8 | 416 | { |
3d14c5d2 | 417 | struct ceph_mds_client *mdsc = fsc->mdsc; |
37151668 | 418 | |
b884014a CX |
419 | spin_lock(&mdsc->caps_list_lock); |
420 | ||
a8599bd8 | 421 | if (total) |
37151668 | 422 | *total = mdsc->caps_total_count; |
a8599bd8 | 423 | if (avail) |
37151668 | 424 | *avail = mdsc->caps_avail_count; |
a8599bd8 | 425 | if (used) |
37151668 | 426 | *used = mdsc->caps_use_count; |
a8599bd8 | 427 | if (reserved) |
37151668 | 428 | *reserved = mdsc->caps_reserve_count; |
85ccce43 | 429 | if (min) |
37151668 | 430 | *min = mdsc->caps_min_count; |
b884014a CX |
431 | |
432 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
433 | } |
434 | ||
435 | /* | |
436 | * Find ceph_cap for given mds, if any. | |
437 | * | |
be655596 | 438 | * Called with i_ceph_lock held. |
a8599bd8 | 439 | */ |
aaf67de7 | 440 | struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
a8599bd8 SW |
441 | { |
442 | struct ceph_cap *cap; | |
443 | struct rb_node *n = ci->i_caps.rb_node; | |
444 | ||
445 | while (n) { | |
446 | cap = rb_entry(n, struct ceph_cap, ci_node); | |
447 | if (mds < cap->mds) | |
448 | n = n->rb_left; | |
449 | else if (mds > cap->mds) | |
450 | n = n->rb_right; | |
451 | else | |
452 | return cap; | |
453 | } | |
454 | return NULL; | |
455 | } | |
456 | ||
2bc50259 GF |
457 | struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
458 | { | |
459 | struct ceph_cap *cap; | |
460 | ||
be655596 | 461 | spin_lock(&ci->i_ceph_lock); |
2bc50259 | 462 | cap = __get_cap_for_mds(ci, mds); |
be655596 | 463 | spin_unlock(&ci->i_ceph_lock); |
2bc50259 GF |
464 | return cap; |
465 | } | |
466 | ||
a8599bd8 | 467 | /* |
be655596 | 468 | * Called under i_ceph_lock. |
a8599bd8 SW |
469 | */ |
470 | static void __insert_cap_node(struct ceph_inode_info *ci, | |
471 | struct ceph_cap *new) | |
472 | { | |
473 | struct rb_node **p = &ci->i_caps.rb_node; | |
474 | struct rb_node *parent = NULL; | |
475 | struct ceph_cap *cap = NULL; | |
476 | ||
477 | while (*p) { | |
478 | parent = *p; | |
479 | cap = rb_entry(parent, struct ceph_cap, ci_node); | |
480 | if (new->mds < cap->mds) | |
481 | p = &(*p)->rb_left; | |
482 | else if (new->mds > cap->mds) | |
483 | p = &(*p)->rb_right; | |
484 | else | |
485 | BUG(); | |
486 | } | |
487 | ||
488 | rb_link_node(&new->ci_node, parent, p); | |
489 | rb_insert_color(&new->ci_node, &ci->i_caps); | |
490 | } | |
491 | ||
492 | /* | |
493 | * (re)set cap hold timeouts, which control the delayed release | |
494 | * of unused caps back to the MDS. Should be called on cap use. | |
495 | */ | |
496 | static void __cap_set_timeouts(struct ceph_mds_client *mdsc, | |
497 | struct ceph_inode_info *ci) | |
498 | { | |
38d46409 | 499 | struct inode *inode = &ci->netfs.inode; |
fe33032d | 500 | struct ceph_mount_options *opt = mdsc->fsc->mount_options; |
38d46409 | 501 | |
a8599bd8 | 502 | ci->i_hold_caps_max = round_jiffies(jiffies + |
fe33032d | 503 | opt->caps_wanted_delay_max * HZ); |
38d46409 XL |
504 | doutc(mdsc->fsc->client, "%p %llx.%llx %lu\n", inode, |
505 | ceph_vinop(inode), ci->i_hold_caps_max - jiffies); | |
a8599bd8 SW |
506 | } |
507 | ||
508 | /* | |
509 | * (Re)queue cap at the end of the delayed cap release list. | |
510 | * | |
511 | * If I_FLUSH is set, leave the inode at the front of the list. | |
512 | * | |
be655596 | 513 | * Caller holds i_ceph_lock |
a8599bd8 SW |
514 | * -> we take mdsc->cap_delay_lock |
515 | */ | |
516 | static void __cap_delay_requeue(struct ceph_mds_client *mdsc, | |
a0d93e32 | 517 | struct ceph_inode_info *ci) |
a8599bd8 | 518 | { |
38d46409 XL |
519 | struct inode *inode = &ci->netfs.inode; |
520 | ||
521 | doutc(mdsc->fsc->client, "%p %llx.%llx flags 0x%lx at %lu\n", | |
522 | inode, ceph_vinop(inode), ci->i_ceph_flags, | |
523 | ci->i_hold_caps_max); | |
a8599bd8 SW |
524 | if (!mdsc->stopping) { |
525 | spin_lock(&mdsc->cap_delay_lock); | |
526 | if (!list_empty(&ci->i_cap_delay_list)) { | |
527 | if (ci->i_ceph_flags & CEPH_I_FLUSH) | |
528 | goto no_change; | |
529 | list_del_init(&ci->i_cap_delay_list); | |
530 | } | |
a0d93e32 | 531 | __cap_set_timeouts(mdsc, ci); |
a8599bd8 SW |
532 | list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
533 | no_change: | |
534 | spin_unlock(&mdsc->cap_delay_lock); | |
535 | } | |
536 | } | |
537 | ||
538 | /* | |
539 | * Queue an inode for immediate writeback. Mark inode with I_FLUSH, | |
540 | * indicating we should send a cap message to flush dirty metadata | |
541 | * asap, and move to the front of the delayed cap list. | |
542 | */ | |
543 | static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, | |
544 | struct ceph_inode_info *ci) | |
545 | { | |
38d46409 XL |
546 | struct inode *inode = &ci->netfs.inode; |
547 | ||
548 | doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode)); | |
a8599bd8 SW |
549 | spin_lock(&mdsc->cap_delay_lock); |
550 | ci->i_ceph_flags |= CEPH_I_FLUSH; | |
551 | if (!list_empty(&ci->i_cap_delay_list)) | |
552 | list_del_init(&ci->i_cap_delay_list); | |
553 | list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); | |
554 | spin_unlock(&mdsc->cap_delay_lock); | |
555 | } | |
556 | ||
557 | /* | |
558 | * Cancel delayed work on cap. | |
559 | * | |
be655596 | 560 | * Caller must hold i_ceph_lock. |
a8599bd8 SW |
561 | */ |
562 | static void __cap_delay_cancel(struct ceph_mds_client *mdsc, | |
563 | struct ceph_inode_info *ci) | |
564 | { | |
38d46409 XL |
565 | struct inode *inode = &ci->netfs.inode; |
566 | ||
567 | doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode)); | |
a8599bd8 SW |
568 | if (list_empty(&ci->i_cap_delay_list)) |
569 | return; | |
570 | spin_lock(&mdsc->cap_delay_lock); | |
571 | list_del_init(&ci->i_cap_delay_list); | |
572 | spin_unlock(&mdsc->cap_delay_lock); | |
573 | } | |
574 | ||
785892fe | 575 | /* Common issue checks for add_cap, handle_cap_grant. */ |
a8599bd8 SW |
576 | static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, |
577 | unsigned issued) | |
578 | { | |
38d46409 XL |
579 | struct inode *inode = &ci->netfs.inode; |
580 | struct ceph_client *cl = ceph_inode_to_client(inode); | |
581 | ||
a8599bd8 SW |
582 | unsigned had = __ceph_caps_issued(ci, NULL); |
583 | ||
785892fe JL |
584 | lockdep_assert_held(&ci->i_ceph_lock); |
585 | ||
a8599bd8 SW |
586 | /* |
587 | * Each time we receive FILE_CACHE anew, we increment | |
588 | * i_rdcache_gen. | |
589 | */ | |
874c8ca1 | 590 | if (S_ISREG(ci->netfs.inode.i_mode) && |
525d15e8 | 591 | (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
99ccbd22 | 592 | (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) { |
a8599bd8 | 593 | ci->i_rdcache_gen++; |
99ccbd22 | 594 | } |
a8599bd8 SW |
595 | |
596 | /* | |
15b51bd6 YZ |
597 | * If FILE_SHARED is newly issued, mark dir not complete. We don't |
598 | * know what happened to this directory while we didn't have the cap. | |
599 | * If FILE_SHARED is being revoked, also mark dir not complete. It | |
600 | * stops on-going cached readdir. | |
a8599bd8 | 601 | */ |
15b51bd6 YZ |
602 | if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) { |
603 | if (issued & CEPH_CAP_FILE_SHARED) | |
97aeb6bf | 604 | atomic_inc(&ci->i_shared_gen); |
874c8ca1 | 605 | if (S_ISDIR(ci->netfs.inode.i_mode)) { |
38d46409 | 606 | doutc(cl, " marking %p NOT complete\n", inode); |
2f276c51 | 607 | __ceph_dir_clear_complete(ci); |
a8673d61 | 608 | } |
a8599bd8 | 609 | } |
785892fe JL |
610 | |
611 | /* Wipe saved layout if we're losing DIR_CREATE caps */ | |
874c8ca1 | 612 | if (S_ISDIR(ci->netfs.inode.i_mode) && (had & CEPH_CAP_DIR_CREATE) && |
785892fe JL |
613 | !(issued & CEPH_CAP_DIR_CREATE)) { |
614 | ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns)); | |
615 | memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout)); | |
616 | } | |
a8599bd8 SW |
617 | } |
618 | ||
1cf03a68 JL |
619 | /** |
620 | * change_auth_cap_ses - move inode to appropriate lists when auth caps change | |
621 | * @ci: inode to be moved | |
622 | * @session: new auth caps session | |
623 | */ | |
e19feff9 XL |
624 | void change_auth_cap_ses(struct ceph_inode_info *ci, |
625 | struct ceph_mds_session *session) | |
1cf03a68 JL |
626 | { |
627 | lockdep_assert_held(&ci->i_ceph_lock); | |
628 | ||
629 | if (list_empty(&ci->i_dirty_item) && list_empty(&ci->i_flushing_item)) | |
630 | return; | |
631 | ||
632 | spin_lock(&session->s_mdsc->cap_dirty_lock); | |
633 | if (!list_empty(&ci->i_dirty_item)) | |
634 | list_move(&ci->i_dirty_item, &session->s_cap_dirty); | |
635 | if (!list_empty(&ci->i_flushing_item)) | |
636 | list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); | |
637 | spin_unlock(&session->s_mdsc->cap_dirty_lock); | |
638 | } | |
639 | ||
a8599bd8 SW |
640 | /* |
641 | * Add a capability under the given MDS session. | |
642 | * | |
354c63a0 | 643 | * Caller should hold session snap_rwsem (read) and ci->i_ceph_lock |
a8599bd8 SW |
644 | * |
645 | * @fmode is the open file mode, if we are opening a file, otherwise | |
646 | * it is < 0. (This is so we can atomically add the cap and add an | |
647 | * open file reference to it.) | |
648 | */ | |
d9df2783 YZ |
649 | void ceph_add_cap(struct inode *inode, |
650 | struct ceph_mds_session *session, u64 cap_id, | |
135e671e | 651 | unsigned issued, unsigned wanted, |
d9df2783 YZ |
652 | unsigned seq, unsigned mseq, u64 realmino, int flags, |
653 | struct ceph_cap **new_cap) | |
a8599bd8 | 654 | { |
5995d90d | 655 | struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; |
38d46409 | 656 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 | 657 | struct ceph_inode_info *ci = ceph_inode(inode); |
a8599bd8 SW |
658 | struct ceph_cap *cap; |
659 | int mds = session->s_mds; | |
660 | int actual_wanted; | |
606d1023 | 661 | u32 gen; |
a8599bd8 | 662 | |
354c63a0 JL |
663 | lockdep_assert_held(&ci->i_ceph_lock); |
664 | ||
38d46409 XL |
665 | doutc(cl, "%p %llx.%llx mds%d cap %llx %s seq %d\n", inode, |
666 | ceph_vinop(inode), session->s_mds, cap_id, | |
667 | ceph_cap_string(issued), seq); | |
a8599bd8 | 668 | |
52d60f8e | 669 | gen = atomic_read(&session->s_cap_gen); |
606d1023 | 670 | |
a8599bd8 SW |
671 | cap = __get_cap_for_mds(ci, mds); |
672 | if (!cap) { | |
d9df2783 YZ |
673 | cap = *new_cap; |
674 | *new_cap = NULL; | |
a8599bd8 SW |
675 | |
676 | cap->issued = 0; | |
677 | cap->implemented = 0; | |
678 | cap->mds = mds; | |
679 | cap->mds_wanted = 0; | |
964266cc | 680 | cap->mseq = 0; |
a8599bd8 SW |
681 | |
682 | cap->ci = ci; | |
683 | __insert_cap_node(ci, cap); | |
684 | ||
a8599bd8 SW |
685 | /* add to session cap list */ |
686 | cap->session = session; | |
687 | spin_lock(&session->s_cap_lock); | |
688 | list_add_tail(&cap->session_caps, &session->s_caps); | |
689 | session->s_nr_caps++; | |
4f1d756d | 690 | atomic64_inc(&mdsc->metric.total_caps); |
a8599bd8 | 691 | spin_unlock(&session->s_cap_lock); |
11df2dfb | 692 | } else { |
32f6511a YZ |
693 | spin_lock(&session->s_cap_lock); |
694 | list_move_tail(&cap->session_caps, &session->s_caps); | |
695 | spin_unlock(&session->s_cap_lock); | |
696 | ||
606d1023 | 697 | if (cap->cap_gen < gen) |
d2f8bb27 YZ |
698 | cap->issued = cap->implemented = CEPH_CAP_PIN; |
699 | ||
11df2dfb YZ |
700 | /* |
701 | * auth mds of the inode changed. we received the cap export | |
702 | * message, but still haven't received the cap import message. | |
703 | * handle_cap_export() updated the new auth MDS' cap. | |
704 | * | |
705 | * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing | |
706 | * a message that was send before the cap import message. So | |
707 | * don't remove caps. | |
708 | */ | |
709 | if (ceph_seq_cmp(seq, cap->seq) <= 0) { | |
710 | WARN_ON(cap != ci->i_auth_cap); | |
711 | WARN_ON(cap->cap_id != cap_id); | |
712 | seq = cap->seq; | |
713 | mseq = cap->mseq; | |
714 | issued |= cap->issued; | |
715 | flags |= CEPH_CAP_FLAG_AUTH; | |
716 | } | |
717 | } | |
a8599bd8 | 718 | |
7d9c9193 YZ |
719 | if (!ci->i_snap_realm || |
720 | ((flags & CEPH_CAP_FLAG_AUTH) && | |
721 | realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) { | |
a8599bd8 SW |
722 | /* |
723 | * add this inode to the appropriate snap realm | |
724 | */ | |
725 | struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, | |
726 | realmino); | |
692e1715 | 727 | if (realm) |
0ba92e1c | 728 | ceph_change_snap_realm(inode, realm); |
692e1715 JL |
729 | else |
730 | WARN(1, "%s: couldn't find snap realm 0x%llx (ino 0x%llx oldrealm 0x%llx)\n", | |
731 | __func__, realmino, ci->i_vino.ino, | |
732 | ci->i_snap_realm ? ci->i_snap_realm->ino : 0); | |
a8599bd8 SW |
733 | } |
734 | ||
735 | __check_cap_issue(ci, cap, issued); | |
736 | ||
737 | /* | |
738 | * If we are issued caps we don't want, or the mds' wanted | |
739 | * value appears to be off, queue a check so we'll release | |
740 | * later and/or update the mds wanted value. | |
741 | */ | |
742 | actual_wanted = __ceph_caps_wanted(ci); | |
743 | if ((wanted & ~actual_wanted) || | |
744 | (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { | |
38d46409 XL |
745 | doutc(cl, "issued %s, mds wanted %s, actual %s, queueing\n", |
746 | ceph_cap_string(issued), ceph_cap_string(wanted), | |
747 | ceph_cap_string(actual_wanted)); | |
a0d93e32 | 748 | __cap_delay_requeue(mdsc, ci); |
a8599bd8 SW |
749 | } |
750 | ||
b8c2f3ae | 751 | if (flags & CEPH_CAP_FLAG_AUTH) { |
d37b1d99 | 752 | if (!ci->i_auth_cap || |
d9ffc4f7 | 753 | ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) { |
1cf03a68 JL |
754 | if (ci->i_auth_cap && |
755 | ci->i_auth_cap->session != cap->session) | |
756 | change_auth_cap_ses(ci, cap->session); | |
b8c2f3ae | 757 | ci->i_auth_cap = cap; |
d9ffc4f7 YZ |
758 | cap->mds_wanted = wanted; |
759 | } | |
11df2dfb YZ |
760 | } else { |
761 | WARN_ON(ci->i_auth_cap == cap); | |
8a92a119 | 762 | } |
a8599bd8 | 763 | |
38d46409 XL |
764 | doutc(cl, "inode %p %llx.%llx cap %p %s now %s seq %d mds%d\n", |
765 | inode, ceph_vinop(inode), cap, ceph_cap_string(issued), | |
766 | ceph_cap_string(issued|cap->issued), seq, mds); | |
a8599bd8 SW |
767 | cap->cap_id = cap_id; |
768 | cap->issued = issued; | |
769 | cap->implemented |= issued; | |
d1b87809 | 770 | if (ceph_seq_cmp(mseq, cap->mseq) > 0) |
964266cc YZ |
771 | cap->mds_wanted = wanted; |
772 | else | |
773 | cap->mds_wanted |= wanted; | |
a8599bd8 SW |
774 | cap->seq = seq; |
775 | cap->issue_seq = seq; | |
776 | cap->mseq = mseq; | |
606d1023 | 777 | cap->cap_gen = gen; |
f7913573 | 778 | wake_up_all(&ci->i_cap_wq); |
a8599bd8 SW |
779 | } |
780 | ||
781 | /* | |
782 | * Return true if cap has not timed out and belongs to the current | |
783 | * generation of the MDS session (i.e. has not gone 'stale' due to | |
784 | * us losing touch with the mds). | |
785 | */ | |
786 | static int __cap_is_valid(struct ceph_cap *cap) | |
787 | { | |
38d46409 XL |
788 | struct inode *inode = &cap->ci->netfs.inode; |
789 | struct ceph_client *cl = cap->session->s_mdsc->fsc->client; | |
a8599bd8 | 790 | unsigned long ttl; |
cdac8303 | 791 | u32 gen; |
a8599bd8 | 792 | |
52d60f8e | 793 | gen = atomic_read(&cap->session->s_cap_gen); |
a8599bd8 | 794 | ttl = cap->session->s_cap_ttl; |
a8599bd8 | 795 | |
685f9a5d | 796 | if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { |
38d46409 XL |
797 | doutc(cl, "%p %llx.%llx cap %p issued %s but STALE (gen %u vs %u)\n", |
798 | inode, ceph_vinop(inode), cap, | |
799 | ceph_cap_string(cap->issued), cap->cap_gen, gen); | |
a8599bd8 SW |
800 | return 0; |
801 | } | |
802 | ||
803 | return 1; | |
804 | } | |
805 | ||
806 | /* | |
807 | * Return set of valid cap bits issued to us. Note that caps time | |
808 | * out, and may be invalidated in bulk if the client session times out | |
809 | * and session->s_cap_gen is bumped. | |
810 | */ | |
811 | int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) | |
812 | { | |
38d46409 XL |
813 | struct inode *inode = &ci->netfs.inode; |
814 | struct ceph_client *cl = ceph_inode_to_client(inode); | |
d9df2783 | 815 | int have = ci->i_snap_caps; |
a8599bd8 SW |
816 | struct ceph_cap *cap; |
817 | struct rb_node *p; | |
818 | ||
819 | if (implemented) | |
820 | *implemented = 0; | |
821 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
822 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
823 | if (!__cap_is_valid(cap)) | |
824 | continue; | |
38d46409 XL |
825 | doutc(cl, "%p %llx.%llx cap %p issued %s\n", inode, |
826 | ceph_vinop(inode), cap, ceph_cap_string(cap->issued)); | |
a8599bd8 SW |
827 | have |= cap->issued; |
828 | if (implemented) | |
829 | *implemented |= cap->implemented; | |
830 | } | |
b1530f57 YZ |
831 | /* |
832 | * exclude caps issued by non-auth MDS, but are been revoking | |
833 | * by the auth MDS. The non-auth MDS should be revoking/exporting | |
834 | * these caps, but the message is delayed. | |
835 | */ | |
836 | if (ci->i_auth_cap) { | |
837 | cap = ci->i_auth_cap; | |
838 | have &= ~cap->implemented | cap->issued; | |
839 | } | |
a8599bd8 SW |
840 | return have; |
841 | } | |
842 | ||
843 | /* | |
844 | * Get cap bits issued by caps other than @ocap | |
845 | */ | |
846 | int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) | |
847 | { | |
848 | int have = ci->i_snap_caps; | |
849 | struct ceph_cap *cap; | |
850 | struct rb_node *p; | |
851 | ||
852 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
853 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
854 | if (cap == ocap) | |
855 | continue; | |
856 | if (!__cap_is_valid(cap)) | |
857 | continue; | |
858 | have |= cap->issued; | |
859 | } | |
860 | return have; | |
861 | } | |
862 | ||
863 | /* | |
864 | * Move a cap to the end of the LRU (oldest caps at list head, newest | |
865 | * at list tail). | |
866 | */ | |
867 | static void __touch_cap(struct ceph_cap *cap) | |
868 | { | |
38d46409 | 869 | struct inode *inode = &cap->ci->netfs.inode; |
a8599bd8 | 870 | struct ceph_mds_session *s = cap->session; |
38d46409 | 871 | struct ceph_client *cl = s->s_mdsc->fsc->client; |
a8599bd8 | 872 | |
a8599bd8 | 873 | spin_lock(&s->s_cap_lock); |
d37b1d99 | 874 | if (!s->s_cap_iterator) { |
38d46409 XL |
875 | doutc(cl, "%p %llx.%llx cap %p mds%d\n", inode, |
876 | ceph_vinop(inode), cap, s->s_mds); | |
5dacf091 SW |
877 | list_move_tail(&cap->session_caps, &s->s_caps); |
878 | } else { | |
38d46409 XL |
879 | doutc(cl, "%p %llx.%llx cap %p mds%d NOP, iterating over caps\n", |
880 | inode, ceph_vinop(inode), cap, s->s_mds); | |
5dacf091 | 881 | } |
a8599bd8 SW |
882 | spin_unlock(&s->s_cap_lock); |
883 | } | |
884 | ||
885 | /* | |
886 | * Check if we hold the given mask. If so, move the cap(s) to the | |
887 | * front of their respective LRUs. (This is the preferred way for | |
888 | * callers to check for caps they want.) | |
889 | */ | |
890 | int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) | |
891 | { | |
38d46409 XL |
892 | struct inode *inode = &ci->netfs.inode; |
893 | struct ceph_client *cl = ceph_inode_to_client(inode); | |
a8599bd8 SW |
894 | struct ceph_cap *cap; |
895 | struct rb_node *p; | |
896 | int have = ci->i_snap_caps; | |
897 | ||
898 | if ((have & mask) == mask) { | |
38d46409 XL |
899 | doutc(cl, "mask %p %llx.%llx snap issued %s (mask %s)\n", |
900 | inode, ceph_vinop(inode), ceph_cap_string(have), | |
901 | ceph_cap_string(mask)); | |
a8599bd8 SW |
902 | return 1; |
903 | } | |
904 | ||
905 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
906 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
907 | if (!__cap_is_valid(cap)) | |
908 | continue; | |
909 | if ((cap->issued & mask) == mask) { | |
38d46409 XL |
910 | doutc(cl, "mask %p %llx.%llx cap %p issued %s (mask %s)\n", |
911 | inode, ceph_vinop(inode), cap, | |
912 | ceph_cap_string(cap->issued), | |
913 | ceph_cap_string(mask)); | |
a8599bd8 SW |
914 | if (touch) |
915 | __touch_cap(cap); | |
916 | return 1; | |
917 | } | |
918 | ||
919 | /* does a combination of caps satisfy mask? */ | |
920 | have |= cap->issued; | |
921 | if ((have & mask) == mask) { | |
38d46409 XL |
922 | doutc(cl, "mask %p %llx.%llx combo issued %s (mask %s)\n", |
923 | inode, ceph_vinop(inode), | |
924 | ceph_cap_string(cap->issued), | |
925 | ceph_cap_string(mask)); | |
a8599bd8 SW |
926 | if (touch) { |
927 | struct rb_node *q; | |
928 | ||
25985edc | 929 | /* touch this + preceding caps */ |
a8599bd8 SW |
930 | __touch_cap(cap); |
931 | for (q = rb_first(&ci->i_caps); q != p; | |
932 | q = rb_next(q)) { | |
933 | cap = rb_entry(q, struct ceph_cap, | |
934 | ci_node); | |
935 | if (!__cap_is_valid(cap)) | |
936 | continue; | |
9f8b72b3 XL |
937 | if (cap->issued & mask) |
938 | __touch_cap(cap); | |
a8599bd8 SW |
939 | } |
940 | } | |
941 | return 1; | |
942 | } | |
943 | } | |
944 | ||
945 | return 0; | |
946 | } | |
947 | ||
1af16d54 XL |
948 | int __ceph_caps_issued_mask_metric(struct ceph_inode_info *ci, int mask, |
949 | int touch) | |
950 | { | |
5995d90d | 951 | struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb); |
1af16d54 XL |
952 | int r; |
953 | ||
954 | r = __ceph_caps_issued_mask(ci, mask, touch); | |
955 | if (r) | |
956 | ceph_update_cap_hit(&fsc->mdsc->metric); | |
957 | else | |
958 | ceph_update_cap_mis(&fsc->mdsc->metric); | |
959 | return r; | |
960 | } | |
961 | ||
a8599bd8 SW |
962 | /* |
963 | * Return true if mask caps are currently being revoked by an MDS. | |
964 | */ | |
6ee6b953 YZ |
965 | int __ceph_caps_revoking_other(struct ceph_inode_info *ci, |
966 | struct ceph_cap *ocap, int mask) | |
a8599bd8 | 967 | { |
a8599bd8 SW |
968 | struct ceph_cap *cap; |
969 | struct rb_node *p; | |
a8599bd8 | 970 | |
a8599bd8 SW |
971 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
972 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
9563f88c | 973 | if (cap != ocap && |
6ee6b953 YZ |
974 | (cap->implemented & ~cap->issued & mask)) |
975 | return 1; | |
a8599bd8 | 976 | } |
6ee6b953 YZ |
977 | return 0; |
978 | } | |
979 | ||
980 | int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) | |
981 | { | |
874c8ca1 | 982 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 983 | struct ceph_client *cl = ceph_inode_to_client(inode); |
6ee6b953 YZ |
984 | int ret; |
985 | ||
986 | spin_lock(&ci->i_ceph_lock); | |
987 | ret = __ceph_caps_revoking_other(ci, NULL, mask); | |
be655596 | 988 | spin_unlock(&ci->i_ceph_lock); |
38d46409 XL |
989 | doutc(cl, "%p %llx.%llx %s = %d\n", inode, ceph_vinop(inode), |
990 | ceph_cap_string(mask), ret); | |
a8599bd8 SW |
991 | return ret; |
992 | } | |
993 | ||
994 | int __ceph_caps_used(struct ceph_inode_info *ci) | |
995 | { | |
996 | int used = 0; | |
997 | if (ci->i_pin_ref) | |
998 | used |= CEPH_CAP_PIN; | |
999 | if (ci->i_rd_ref) | |
1000 | used |= CEPH_CAP_FILE_RD; | |
fdd4e158 | 1001 | if (ci->i_rdcache_ref || |
874c8ca1 DH |
1002 | (S_ISREG(ci->netfs.inode.i_mode) && |
1003 | ci->netfs.inode.i_data.nrpages)) | |
a8599bd8 SW |
1004 | used |= CEPH_CAP_FILE_CACHE; |
1005 | if (ci->i_wr_ref) | |
1006 | used |= CEPH_CAP_FILE_WR; | |
d3d0720d | 1007 | if (ci->i_wb_ref || ci->i_wrbuffer_ref) |
a8599bd8 | 1008 | used |= CEPH_CAP_FILE_BUFFER; |
f85122af JL |
1009 | if (ci->i_fx_ref) |
1010 | used |= CEPH_CAP_FILE_EXCL; | |
a8599bd8 SW |
1011 | return used; |
1012 | } | |
1013 | ||
719a2514 YZ |
1014 | #define FMODE_WAIT_BIAS 1000 |
1015 | ||
a8599bd8 SW |
1016 | /* |
1017 | * wanted, by virtue of open file modes | |
1018 | */ | |
1019 | int __ceph_caps_file_wanted(struct ceph_inode_info *ci) | |
1020 | { | |
719a2514 YZ |
1021 | const int PIN_SHIFT = ffs(CEPH_FILE_MODE_PIN); |
1022 | const int RD_SHIFT = ffs(CEPH_FILE_MODE_RD); | |
1023 | const int WR_SHIFT = ffs(CEPH_FILE_MODE_WR); | |
1024 | const int LAZY_SHIFT = ffs(CEPH_FILE_MODE_LAZY); | |
1025 | struct ceph_mount_options *opt = | |
5995d90d | 1026 | ceph_inode_to_fs_client(&ci->netfs.inode)->mount_options; |
719a2514 YZ |
1027 | unsigned long used_cutoff = jiffies - opt->caps_wanted_delay_max * HZ; |
1028 | unsigned long idle_cutoff = jiffies - opt->caps_wanted_delay_min * HZ; | |
1029 | ||
874c8ca1 | 1030 | if (S_ISDIR(ci->netfs.inode.i_mode)) { |
719a2514 YZ |
1031 | int want = 0; |
1032 | ||
1033 | /* use used_cutoff here, to keep dir's wanted caps longer */ | |
1034 | if (ci->i_nr_by_mode[RD_SHIFT] > 0 || | |
1035 | time_after(ci->i_last_rd, used_cutoff)) | |
1036 | want |= CEPH_CAP_ANY_SHARED; | |
1037 | ||
1038 | if (ci->i_nr_by_mode[WR_SHIFT] > 0 || | |
1039 | time_after(ci->i_last_wr, used_cutoff)) { | |
1040 | want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL; | |
1041 | if (opt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS) | |
1042 | want |= CEPH_CAP_ANY_DIR_OPS; | |
1043 | } | |
1044 | ||
1045 | if (want || ci->i_nr_by_mode[PIN_SHIFT] > 0) | |
1046 | want |= CEPH_CAP_PIN; | |
1047 | ||
1048 | return want; | |
1049 | } else { | |
1050 | int bits = 0; | |
1051 | ||
1052 | if (ci->i_nr_by_mode[RD_SHIFT] > 0) { | |
1053 | if (ci->i_nr_by_mode[RD_SHIFT] >= FMODE_WAIT_BIAS || | |
1054 | time_after(ci->i_last_rd, used_cutoff)) | |
1055 | bits |= 1 << RD_SHIFT; | |
1056 | } else if (time_after(ci->i_last_rd, idle_cutoff)) { | |
1057 | bits |= 1 << RD_SHIFT; | |
1058 | } | |
1059 | ||
1060 | if (ci->i_nr_by_mode[WR_SHIFT] > 0) { | |
1061 | if (ci->i_nr_by_mode[WR_SHIFT] >= FMODE_WAIT_BIAS || | |
1062 | time_after(ci->i_last_wr, used_cutoff)) | |
1063 | bits |= 1 << WR_SHIFT; | |
1064 | } else if (time_after(ci->i_last_wr, idle_cutoff)) { | |
1065 | bits |= 1 << WR_SHIFT; | |
1066 | } | |
1067 | ||
1068 | /* check lazyio only when read/write is wanted */ | |
1069 | if ((bits & (CEPH_FILE_MODE_RDWR << 1)) && | |
1070 | ci->i_nr_by_mode[LAZY_SHIFT] > 0) | |
1071 | bits |= 1 << LAZY_SHIFT; | |
1072 | ||
1073 | return bits ? ceph_caps_for_mode(bits >> 1) : 0; | |
774a6a11 | 1074 | } |
a8599bd8 SW |
1075 | } |
1076 | ||
525d15e8 YZ |
1077 | /* |
1078 | * wanted, by virtue of open file modes AND cap refs (buffered/cached data) | |
1079 | */ | |
1080 | int __ceph_caps_wanted(struct ceph_inode_info *ci) | |
1081 | { | |
1082 | int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci); | |
874c8ca1 | 1083 | if (S_ISDIR(ci->netfs.inode.i_mode)) { |
a25949b9 JL |
1084 | /* we want EXCL if holding caps of dir ops */ |
1085 | if (w & CEPH_CAP_ANY_DIR_OPS) | |
1086 | w |= CEPH_CAP_FILE_EXCL; | |
1087 | } else { | |
525d15e8 YZ |
1088 | /* we want EXCL if dirty data */ |
1089 | if (w & CEPH_CAP_FILE_BUFFER) | |
1090 | w |= CEPH_CAP_FILE_EXCL; | |
1091 | } | |
1092 | return w; | |
1093 | } | |
1094 | ||
a8599bd8 SW |
1095 | /* |
1096 | * Return caps we have registered with the MDS(s) as 'wanted'. | |
1097 | */ | |
c1944fed | 1098 | int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check) |
a8599bd8 SW |
1099 | { |
1100 | struct ceph_cap *cap; | |
1101 | struct rb_node *p; | |
1102 | int mds_wanted = 0; | |
1103 | ||
1104 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
1105 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
c1944fed | 1106 | if (check && !__cap_is_valid(cap)) |
a8599bd8 | 1107 | continue; |
a2550604 YZ |
1108 | if (cap == ci->i_auth_cap) |
1109 | mds_wanted |= cap->mds_wanted; | |
1110 | else | |
1111 | mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR); | |
a8599bd8 SW |
1112 | } |
1113 | return mds_wanted; | |
1114 | } | |
1115 | ||
9215aeea YZ |
1116 | int ceph_is_any_caps(struct inode *inode) |
1117 | { | |
1118 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1119 | int ret; | |
1120 | ||
1121 | spin_lock(&ci->i_ceph_lock); | |
bd84fbcb | 1122 | ret = __ceph_is_any_real_caps(ci); |
9215aeea YZ |
1123 | spin_unlock(&ci->i_ceph_lock); |
1124 | ||
1125 | return ret; | |
1126 | } | |
1127 | ||
a8599bd8 | 1128 | /* |
f818a736 SW |
1129 | * Remove a cap. Take steps to deal with a racing iterate_session_caps. |
1130 | * | |
be655596 | 1131 | * caller should hold i_ceph_lock. |
a6369741 | 1132 | * caller will not hold session s_mutex if called from destroy_inode. |
a8599bd8 | 1133 | */ |
a096b09a | 1134 | void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) |
a8599bd8 SW |
1135 | { |
1136 | struct ceph_mds_session *session = cap->session; | |
38d46409 | 1137 | struct ceph_client *cl = session->s_mdsc->fsc->client; |
a8599bd8 | 1138 | struct ceph_inode_info *ci = cap->ci; |
38d46409 | 1139 | struct inode *inode = &ci->netfs.inode; |
e5cafce3 | 1140 | struct ceph_mds_client *mdsc; |
f818a736 | 1141 | int removed = 0; |
a8599bd8 | 1142 | |
e5cafce3 LH |
1143 | /* 'ci' being NULL means the remove have already occurred */ |
1144 | if (!ci) { | |
38d46409 | 1145 | doutc(cl, "inode is NULL\n"); |
e5cafce3 LH |
1146 | return; |
1147 | } | |
1148 | ||
a76d0a9c XL |
1149 | lockdep_assert_held(&ci->i_ceph_lock); |
1150 | ||
38d46409 | 1151 | doutc(cl, "%p from %p %llx.%llx\n", cap, inode, ceph_vinop(inode)); |
a8599bd8 | 1152 | |
5995d90d | 1153 | mdsc = ceph_inode_to_fs_client(&ci->netfs.inode)->mdsc; |
e5cafce3 | 1154 | |
ea60ed6f LH |
1155 | /* remove from inode's cap rbtree, and clear auth cap */ |
1156 | rb_erase(&cap->ci_node, &ci->i_caps); | |
a76d0a9c | 1157 | if (ci->i_auth_cap == cap) |
ea60ed6f LH |
1158 | ci->i_auth_cap = NULL; |
1159 | ||
7c1332b8 SW |
1160 | /* remove from session list */ |
1161 | spin_lock(&session->s_cap_lock); | |
1162 | if (session->s_cap_iterator == cap) { | |
1163 | /* not yet, we are iterating over this very cap */ | |
38d46409 XL |
1164 | doutc(cl, "delaying %p removal from session %p\n", cap, |
1165 | cap->session); | |
7c1332b8 SW |
1166 | } else { |
1167 | list_del_init(&cap->session_caps); | |
1168 | session->s_nr_caps--; | |
4f1d756d | 1169 | atomic64_dec(&mdsc->metric.total_caps); |
7c1332b8 | 1170 | cap->session = NULL; |
f818a736 | 1171 | removed = 1; |
7c1332b8 | 1172 | } |
f818a736 SW |
1173 | /* protect backpointer with s_cap_lock: see iterate_session_caps */ |
1174 | cap->ci = NULL; | |
745a8e3b YZ |
1175 | |
1176 | /* | |
1177 | * s_cap_reconnect is protected by s_cap_lock. no one changes | |
1178 | * s_cap_gen while session is in the reconnect state. | |
1179 | */ | |
1180 | if (queue_release && | |
52d60f8e JL |
1181 | (!session->s_cap_reconnect || |
1182 | cap->cap_gen == atomic_read(&session->s_cap_gen))) { | |
745a8e3b YZ |
1183 | cap->queue_release = 1; |
1184 | if (removed) { | |
e3ec8d68 | 1185 | __ceph_queue_cap_release(session, cap); |
745a8e3b YZ |
1186 | removed = 0; |
1187 | } | |
1188 | } else { | |
1189 | cap->queue_release = 0; | |
1190 | } | |
1191 | cap->cap_ino = ci->i_vino.ino; | |
1192 | ||
7c1332b8 SW |
1193 | spin_unlock(&session->s_cap_lock); |
1194 | ||
f818a736 | 1195 | if (removed) |
37151668 | 1196 | ceph_put_cap(mdsc, cap); |
a8599bd8 | 1197 | |
bd84fbcb XL |
1198 | if (!__ceph_is_any_real_caps(ci)) { |
1199 | /* when reconnect denied, we remove session caps forcibly, | |
1200 | * i_wr_ref can be non-zero. If there are ongoing write, | |
1201 | * keep i_snap_realm. | |
1202 | */ | |
1203 | if (ci->i_wr_ref == 0 && ci->i_snap_realm) | |
874c8ca1 | 1204 | ceph_change_snap_realm(&ci->netfs.inode, NULL); |
db40cc17 | 1205 | |
a8599bd8 | 1206 | __cap_delay_cancel(mdsc, ci); |
bd84fbcb | 1207 | } |
a8599bd8 SW |
1208 | } |
1209 | ||
197b7d79 XL |
1210 | void ceph_remove_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, |
1211 | bool queue_release) | |
a76d0a9c XL |
1212 | { |
1213 | struct ceph_inode_info *ci = cap->ci; | |
1214 | struct ceph_fs_client *fsc; | |
1215 | ||
1216 | /* 'ci' being NULL means the remove have already occurred */ | |
1217 | if (!ci) { | |
38d46409 | 1218 | doutc(mdsc->fsc->client, "inode is NULL\n"); |
a76d0a9c XL |
1219 | return; |
1220 | } | |
1221 | ||
1222 | lockdep_assert_held(&ci->i_ceph_lock); | |
1223 | ||
5995d90d | 1224 | fsc = ceph_inode_to_fs_client(&ci->netfs.inode); |
a76d0a9c XL |
1225 | WARN_ON_ONCE(ci->i_auth_cap == cap && |
1226 | !list_empty(&ci->i_dirty_item) && | |
1227 | !fsc->blocklisted && | |
874c8ca1 | 1228 | !ceph_inode_is_shutdown(&ci->netfs.inode)); |
a76d0a9c XL |
1229 | |
1230 | __ceph_remove_cap(cap, queue_release); | |
1231 | } | |
1232 | ||
0ff8bfb3 JL |
1233 | struct cap_msg_args { |
1234 | struct ceph_mds_session *session; | |
1235 | u64 ino, cid, follows; | |
1236 | u64 flush_tid, oldest_flush_tid, size, max_size; | |
1237 | u64 xattr_version; | |
176c77c9 | 1238 | u64 change_attr; |
0ff8bfb3 | 1239 | struct ceph_buffer *xattr_buf; |
0a454bdd | 1240 | struct ceph_buffer *old_xattr_buf; |
ec62b894 | 1241 | struct timespec64 atime, mtime, ctime, btime; |
0ff8bfb3 JL |
1242 | int op, caps, wanted, dirty; |
1243 | u32 seq, issue_seq, mseq, time_warp_seq; | |
1e4ef0c6 | 1244 | u32 flags; |
0ff8bfb3 JL |
1245 | kuid_t uid; |
1246 | kgid_t gid; | |
1247 | umode_t mode; | |
1248 | bool inline_data; | |
0a454bdd | 1249 | bool wake; |
16be62fc | 1250 | bool encrypted; |
2d332d5b | 1251 | u32 fscrypt_auth_len; |
2d332d5b | 1252 | u8 fscrypt_auth[sizeof(struct ceph_fscrypt_auth)]; // for context |
0ff8bfb3 JL |
1253 | }; |
1254 | ||
16d68903 JL |
1255 | /* Marshal up the cap msg to the MDS */ |
1256 | static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg) | |
a8599bd8 SW |
1257 | { |
1258 | struct ceph_mds_caps *fc; | |
e20d258d | 1259 | void *p; |
38d46409 XL |
1260 | struct ceph_mds_client *mdsc = arg->session->s_mdsc; |
1261 | struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; | |
1262 | ||
1263 | doutc(mdsc->fsc->client, | |
1264 | "%s %llx %llx caps %s wanted %s dirty %s seq %u/%u" | |
1265 | " tid %llu/%llu mseq %u follows %lld size %llu/%llu" | |
1266 | " xattr_ver %llu xattr_len %d\n", | |
1267 | ceph_cap_op_name(arg->op), arg->cid, arg->ino, | |
1268 | ceph_cap_string(arg->caps), ceph_cap_string(arg->wanted), | |
1269 | ceph_cap_string(arg->dirty), arg->seq, arg->issue_seq, | |
1270 | arg->flush_tid, arg->oldest_flush_tid, arg->mseq, arg->follows, | |
1271 | arg->size, arg->max_size, arg->xattr_version, | |
1272 | arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0); | |
a8599bd8 | 1273 | |
2d332d5b | 1274 | msg->hdr.version = cpu_to_le16(12); |
0ff8bfb3 | 1275 | msg->hdr.tid = cpu_to_le64(arg->flush_tid); |
a8599bd8 | 1276 | |
6df058c0 | 1277 | fc = msg->front.iov_base; |
a8599bd8 SW |
1278 | memset(fc, 0, sizeof(*fc)); |
1279 | ||
0ff8bfb3 JL |
1280 | fc->cap_id = cpu_to_le64(arg->cid); |
1281 | fc->op = cpu_to_le32(arg->op); | |
1282 | fc->seq = cpu_to_le32(arg->seq); | |
1283 | fc->issue_seq = cpu_to_le32(arg->issue_seq); | |
1284 | fc->migrate_seq = cpu_to_le32(arg->mseq); | |
1285 | fc->caps = cpu_to_le32(arg->caps); | |
1286 | fc->wanted = cpu_to_le32(arg->wanted); | |
1287 | fc->dirty = cpu_to_le32(arg->dirty); | |
1288 | fc->ino = cpu_to_le64(arg->ino); | |
1289 | fc->snap_follows = cpu_to_le64(arg->follows); | |
1290 | ||
16be62fc JL |
1291 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
1292 | if (arg->encrypted) | |
1293 | fc->size = cpu_to_le64(round_up(arg->size, | |
1294 | CEPH_FSCRYPT_BLOCK_SIZE)); | |
1295 | else | |
1296 | #endif | |
1297 | fc->size = cpu_to_le64(arg->size); | |
0ff8bfb3 | 1298 | fc->max_size = cpu_to_le64(arg->max_size); |
9bbeab41 AB |
1299 | ceph_encode_timespec64(&fc->mtime, &arg->mtime); |
1300 | ceph_encode_timespec64(&fc->atime, &arg->atime); | |
1301 | ceph_encode_timespec64(&fc->ctime, &arg->ctime); | |
0ff8bfb3 JL |
1302 | fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq); |
1303 | ||
1304 | fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid)); | |
1305 | fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid)); | |
1306 | fc->mode = cpu_to_le32(arg->mode); | |
1307 | ||
1308 | fc->xattr_version = cpu_to_le64(arg->xattr_version); | |
1309 | if (arg->xattr_buf) { | |
1310 | msg->middle = ceph_buffer_get(arg->xattr_buf); | |
1311 | fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); | |
1312 | msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); | |
9670079f JL |
1313 | } |
1314 | ||
e20d258d | 1315 | p = fc + 1; |
43b29673 | 1316 | /* flock buffer size (version 2) */ |
e20d258d | 1317 | ceph_encode_32(&p, 0); |
43b29673 | 1318 | /* inline version (version 4) */ |
0ff8bfb3 | 1319 | ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE); |
e20d258d YZ |
1320 | /* inline data size */ |
1321 | ceph_encode_32(&p, 0); | |
92475f05 JL |
1322 | /* |
1323 | * osd_epoch_barrier (version 5) | |
1324 | * The epoch_barrier is protected osdc->lock, so READ_ONCE here in | |
1325 | * case it was recently changed | |
1326 | */ | |
1327 | ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier)); | |
43b29673 | 1328 | /* oldest_flush_tid (version 6) */ |
0ff8bfb3 | 1329 | ceph_encode_64(&p, arg->oldest_flush_tid); |
e20d258d | 1330 | |
43b29673 JL |
1331 | /* |
1332 | * caller_uid/caller_gid (version 7) | |
1333 | * | |
1334 | * Currently, we don't properly track which caller dirtied the caps | |
1335 | * last, and force a flush of them when there is a conflict. For now, | |
1336 | * just set this to 0:0, to emulate how the MDS has worked up to now. | |
1337 | */ | |
1338 | ceph_encode_32(&p, 0); | |
1339 | ceph_encode_32(&p, 0); | |
1340 | ||
1341 | /* pool namespace (version 8) (mds always ignores this) */ | |
1342 | ceph_encode_32(&p, 0); | |
1343 | ||
176c77c9 | 1344 | /* btime and change_attr (version 9) */ |
ec62b894 | 1345 | ceph_encode_timespec64(p, &arg->btime); |
43b29673 | 1346 | p += sizeof(struct ceph_timespec); |
176c77c9 | 1347 | ceph_encode_64(&p, arg->change_attr); |
43b29673 JL |
1348 | |
1349 | /* Advisory flags (version 10) */ | |
1e4ef0c6 | 1350 | ceph_encode_32(&p, arg->flags); |
2d332d5b JL |
1351 | |
1352 | /* dirstats (version 11) - these are r/o on the client */ | |
1353 | ceph_encode_64(&p, 0); | |
1354 | ceph_encode_64(&p, 0); | |
1355 | ||
1356 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) | |
16be62fc JL |
1357 | /* |
1358 | * fscrypt_auth and fscrypt_file (version 12) | |
1359 | * | |
1360 | * fscrypt_auth holds the crypto context (if any). fscrypt_file | |
1361 | * tracks the real i_size as an __le64 field (and we use a rounded-up | |
1362 | * i_size in the traditional size field). | |
1363 | */ | |
2d332d5b JL |
1364 | ceph_encode_32(&p, arg->fscrypt_auth_len); |
1365 | ceph_encode_copy(&p, arg->fscrypt_auth, arg->fscrypt_auth_len); | |
16be62fc JL |
1366 | ceph_encode_32(&p, sizeof(__le64)); |
1367 | ceph_encode_64(&p, arg->size); | |
2d332d5b JL |
1368 | #else /* CONFIG_FS_ENCRYPTION */ |
1369 | ceph_encode_32(&p, 0); | |
1370 | ceph_encode_32(&p, 0); | |
1371 | #endif /* CONFIG_FS_ENCRYPTION */ | |
a8599bd8 SW |
1372 | } |
1373 | ||
1374 | /* | |
d6e47819 | 1375 | * Queue cap releases when an inode is dropped from our cache. |
a8599bd8 | 1376 | */ |
d6e47819 | 1377 | void __ceph_remove_caps(struct ceph_inode_info *ci) |
a8599bd8 | 1378 | { |
197b7d79 | 1379 | struct inode *inode = &ci->netfs.inode; |
5995d90d | 1380 | struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; |
a8599bd8 SW |
1381 | struct rb_node *p; |
1382 | ||
d6e47819 YZ |
1383 | /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU) |
1384 | * may call __ceph_caps_issued_mask() on a freeing inode. */ | |
1385 | spin_lock(&ci->i_ceph_lock); | |
a8599bd8 SW |
1386 | p = rb_first(&ci->i_caps); |
1387 | while (p) { | |
1388 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); | |
a8599bd8 | 1389 | p = rb_next(p); |
197b7d79 | 1390 | ceph_remove_cap(mdsc, cap, true); |
a8599bd8 | 1391 | } |
d6e47819 | 1392 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
1393 | } |
1394 | ||
1395 | /* | |
0a454bdd JL |
1396 | * Prepare to send a cap message to an MDS. Update the cap state, and populate |
1397 | * the arg struct with the parameters that will need to be sent. This should | |
1398 | * be done under the i_ceph_lock to guard against changes to cap state. | |
a8599bd8 SW |
1399 | * |
1400 | * Make note of max_size reported/requested from mds, revoked caps | |
1401 | * that have now been implemented. | |
a8599bd8 | 1402 | */ |
0a454bdd JL |
1403 | static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap, |
1404 | int op, int flags, int used, int want, int retain, | |
1405 | int flushing, u64 flush_tid, u64 oldest_flush_tid) | |
a8599bd8 SW |
1406 | { |
1407 | struct ceph_inode_info *ci = cap->ci; | |
874c8ca1 | 1408 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 1409 | struct ceph_client *cl = ceph_inode_to_client(inode); |
bb0581f0 | 1410 | int held, revoking; |
a8599bd8 | 1411 | |
0a454bdd | 1412 | lockdep_assert_held(&ci->i_ceph_lock); |
891f3f5a | 1413 | |
68c28323 SW |
1414 | held = cap->issued | cap->implemented; |
1415 | revoking = cap->implemented & ~cap->issued; | |
1416 | retain &= ~revoking; | |
68c28323 | 1417 | |
38d46409 XL |
1418 | doutc(cl, "%p %llx.%llx cap %p session %p %s -> %s (revoking %s)\n", |
1419 | inode, ceph_vinop(inode), cap, cap->session, | |
1420 | ceph_cap_string(held), ceph_cap_string(held & retain), | |
1421 | ceph_cap_string(revoking)); | |
a8599bd8 SW |
1422 | BUG_ON((retain & CEPH_CAP_PIN) == 0); |
1423 | ||
a0d93e32 | 1424 | ci->i_ceph_flags &= ~CEPH_I_FLUSH; |
a8599bd8 SW |
1425 | |
1426 | cap->issued &= retain; /* drop bits we don't want */ | |
0a454bdd JL |
1427 | /* |
1428 | * Wake up any waiters on wanted -> needed transition. This is due to | |
1429 | * the weird transition from buffered to sync IO... we need to flush | |
1430 | * dirty pages _before_ allowing sync writes to avoid reordering. | |
1431 | */ | |
1432 | arg->wake = cap->implemented & ~cap->issued; | |
a8599bd8 SW |
1433 | cap->implemented &= cap->issued | used; |
1434 | cap->mds_wanted = want; | |
1435 | ||
0a454bdd JL |
1436 | arg->session = cap->session; |
1437 | arg->ino = ceph_vino(inode).ino; | |
1438 | arg->cid = cap->cap_id; | |
1439 | arg->follows = flushing ? ci->i_head_snapc->seq : 0; | |
1440 | arg->flush_tid = flush_tid; | |
1441 | arg->oldest_flush_tid = oldest_flush_tid; | |
2d6795fb | 1442 | arg->size = i_size_read(inode); |
0a454bdd JL |
1443 | ci->i_reported_size = arg->size; |
1444 | arg->max_size = ci->i_wanted_max_size; | |
6f05b30e YZ |
1445 | if (cap == ci->i_auth_cap) { |
1446 | if (want & CEPH_CAP_ANY_FILE_WR) | |
1447 | ci->i_requested_max_size = arg->max_size; | |
1448 | else | |
1449 | ci->i_requested_max_size = 0; | |
1450 | } | |
a8599bd8 | 1451 | |
082afec9 | 1452 | if (flushing & CEPH_CAP_XATTR_EXCL) { |
0a454bdd JL |
1453 | arg->old_xattr_buf = __ceph_build_xattrs_blob(ci); |
1454 | arg->xattr_version = ci->i_xattrs.version; | |
1455 | arg->xattr_buf = ci->i_xattrs.blob; | |
0ff8bfb3 | 1456 | } else { |
0a454bdd JL |
1457 | arg->xattr_buf = NULL; |
1458 | arg->old_xattr_buf = NULL; | |
a8599bd8 SW |
1459 | } |
1460 | ||
c453bdb5 JL |
1461 | arg->mtime = inode_get_mtime(inode); |
1462 | arg->atime = inode_get_atime(inode); | |
7795aef0 | 1463 | arg->ctime = inode_get_ctime(inode); |
0a454bdd JL |
1464 | arg->btime = ci->i_btime; |
1465 | arg->change_attr = inode_peek_iversion_raw(inode); | |
0ff8bfb3 | 1466 | |
0a454bdd JL |
1467 | arg->op = op; |
1468 | arg->caps = cap->implemented; | |
1469 | arg->wanted = want; | |
1470 | arg->dirty = flushing; | |
0ff8bfb3 | 1471 | |
0a454bdd JL |
1472 | arg->seq = cap->seq; |
1473 | arg->issue_seq = cap->issue_seq; | |
1474 | arg->mseq = cap->mseq; | |
1475 | arg->time_warp_seq = ci->i_time_warp_seq; | |
0ff8bfb3 | 1476 | |
0a454bdd JL |
1477 | arg->uid = inode->i_uid; |
1478 | arg->gid = inode->i_gid; | |
1479 | arg->mode = inode->i_mode; | |
0ff8bfb3 | 1480 | |
0a454bdd | 1481 | arg->inline_data = ci->i_inline_version != CEPH_INLINE_NONE; |
49ada6e8 YZ |
1482 | if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) && |
1483 | !list_empty(&ci->i_cap_snaps)) { | |
1484 | struct ceph_cap_snap *capsnap; | |
1485 | list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) { | |
1486 | if (capsnap->cap_flush.tid) | |
1487 | break; | |
1488 | if (capsnap->need_flush) { | |
1489 | flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP; | |
1490 | break; | |
1491 | } | |
1492 | } | |
1493 | } | |
0a454bdd | 1494 | arg->flags = flags; |
16be62fc | 1495 | arg->encrypted = IS_ENCRYPTED(inode); |
2d332d5b JL |
1496 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
1497 | if (ci->fscrypt_auth_len && | |
1498 | WARN_ON_ONCE(ci->fscrypt_auth_len > sizeof(struct ceph_fscrypt_auth))) { | |
1499 | /* Don't set this if it's too big */ | |
1500 | arg->fscrypt_auth_len = 0; | |
1501 | } else { | |
1502 | arg->fscrypt_auth_len = ci->fscrypt_auth_len; | |
1503 | memcpy(arg->fscrypt_auth, ci->fscrypt_auth, | |
1504 | min_t(size_t, ci->fscrypt_auth_len, | |
1505 | sizeof(arg->fscrypt_auth))); | |
1506 | } | |
2d332d5b | 1507 | #endif /* CONFIG_FS_ENCRYPTION */ |
0a454bdd | 1508 | } |
a8599bd8 | 1509 | |
16be62fc | 1510 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
2d332d5b | 1511 | #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \ |
16be62fc | 1512 | 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4 + 8) |
2d332d5b | 1513 | |
2d332d5b JL |
1514 | static inline int cap_msg_size(struct cap_msg_args *arg) |
1515 | { | |
16be62fc | 1516 | return CAP_MSG_FIXED_FIELDS + arg->fscrypt_auth_len; |
2d332d5b JL |
1517 | } |
1518 | #else | |
16be62fc JL |
1519 | #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \ |
1520 | 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4) | |
1521 | ||
2d332d5b JL |
1522 | static inline int cap_msg_size(struct cap_msg_args *arg) |
1523 | { | |
1524 | return CAP_MSG_FIXED_FIELDS; | |
1525 | } | |
1526 | #endif /* CONFIG_FS_ENCRYPTION */ | |
1527 | ||
0a454bdd JL |
1528 | /* |
1529 | * Send a cap msg on the given inode. | |
1530 | * | |
1531 | * Caller should hold snap_rwsem (read), s_mutex. | |
1532 | */ | |
52311980 | 1533 | static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci) |
0a454bdd | 1534 | { |
16d68903 | 1535 | struct ceph_msg *msg; |
874c8ca1 | 1536 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 1537 | struct ceph_client *cl = ceph_inode_to_client(inode); |
12fe3dda | 1538 | |
2d332d5b JL |
1539 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(arg), GFP_NOFS, |
1540 | false); | |
16d68903 | 1541 | if (!msg) { |
38d46409 XL |
1542 | pr_err_client(cl, |
1543 | "error allocating cap msg: ino (%llx.%llx)" | |
1544 | " flushing %s tid %llu, requeuing cap.\n", | |
1545 | ceph_vinop(inode), ceph_cap_string(arg->dirty), | |
1546 | arg->flush_tid); | |
a0d93e32 | 1547 | spin_lock(&ci->i_ceph_lock); |
52311980 | 1548 | __cap_delay_requeue(arg->session->s_mdsc, ci); |
a0d93e32 | 1549 | spin_unlock(&ci->i_ceph_lock); |
16d68903 | 1550 | return; |
a8599bd8 SW |
1551 | } |
1552 | ||
16d68903 JL |
1553 | encode_cap_msg(msg, arg); |
1554 | ceph_con_send(&arg->session->s_con, msg); | |
0a454bdd | 1555 | ceph_buffer_put(arg->old_xattr_buf); |
0a454bdd JL |
1556 | if (arg->wake) |
1557 | wake_up_all(&ci->i_cap_wq); | |
a8599bd8 SW |
1558 | } |
1559 | ||
0e294387 YZ |
1560 | static inline int __send_flush_snap(struct inode *inode, |
1561 | struct ceph_mds_session *session, | |
1562 | struct ceph_cap_snap *capsnap, | |
1563 | u32 mseq, u64 oldest_flush_tid) | |
1564 | { | |
0ff8bfb3 | 1565 | struct cap_msg_args arg; |
16d68903 JL |
1566 | struct ceph_msg *msg; |
1567 | ||
0ff8bfb3 JL |
1568 | arg.session = session; |
1569 | arg.ino = ceph_vino(inode).ino; | |
1570 | arg.cid = 0; | |
1571 | arg.follows = capsnap->follows; | |
1572 | arg.flush_tid = capsnap->cap_flush.tid; | |
1573 | arg.oldest_flush_tid = oldest_flush_tid; | |
1574 | ||
1575 | arg.size = capsnap->size; | |
1576 | arg.max_size = 0; | |
1577 | arg.xattr_version = capsnap->xattr_version; | |
1578 | arg.xattr_buf = capsnap->xattr_blob; | |
0a454bdd | 1579 | arg.old_xattr_buf = NULL; |
0ff8bfb3 JL |
1580 | |
1581 | arg.atime = capsnap->atime; | |
1582 | arg.mtime = capsnap->mtime; | |
1583 | arg.ctime = capsnap->ctime; | |
ec62b894 | 1584 | arg.btime = capsnap->btime; |
176c77c9 | 1585 | arg.change_attr = capsnap->change_attr; |
0ff8bfb3 JL |
1586 | |
1587 | arg.op = CEPH_CAP_OP_FLUSHSNAP; | |
1588 | arg.caps = capsnap->issued; | |
1589 | arg.wanted = 0; | |
1590 | arg.dirty = capsnap->dirty; | |
1591 | ||
1592 | arg.seq = 0; | |
1593 | arg.issue_seq = 0; | |
1594 | arg.mseq = mseq; | |
1595 | arg.time_warp_seq = capsnap->time_warp_seq; | |
1596 | ||
1597 | arg.uid = capsnap->uid; | |
1598 | arg.gid = capsnap->gid; | |
1599 | arg.mode = capsnap->mode; | |
1600 | ||
1601 | arg.inline_data = capsnap->inline_data; | |
1e4ef0c6 | 1602 | arg.flags = 0; |
0a454bdd | 1603 | arg.wake = false; |
16be62fc | 1604 | arg.encrypted = IS_ENCRYPTED(inode); |
0ff8bfb3 | 1605 | |
16be62fc | 1606 | /* No fscrypt_auth changes from a capsnap.*/ |
2d332d5b | 1607 | arg.fscrypt_auth_len = 0; |
2d332d5b JL |
1608 | |
1609 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(&arg), | |
1610 | GFP_NOFS, false); | |
1611 | if (!msg) | |
1612 | return -ENOMEM; | |
0ff8bfb3 | 1613 | |
16d68903 JL |
1614 | encode_cap_msg(msg, &arg); |
1615 | ceph_con_send(&arg.session->s_con, msg); | |
1616 | return 0; | |
0e294387 YZ |
1617 | } |
1618 | ||
a8599bd8 SW |
1619 | /* |
1620 | * When a snapshot is taken, clients accumulate dirty metadata on | |
1621 | * inodes with capabilities in ceph_cap_snaps to describe the file | |
1622 | * state at the time the snapshot was taken. This must be flushed | |
1623 | * asynchronously back to the MDS once sync writes complete and dirty | |
1624 | * data is written out. | |
1625 | * | |
7732fe16 | 1626 | * Called under i_ceph_lock. |
a8599bd8 | 1627 | */ |
ed9b430c YZ |
1628 | static void __ceph_flush_snaps(struct ceph_inode_info *ci, |
1629 | struct ceph_mds_session *session) | |
be655596 SW |
1630 | __releases(ci->i_ceph_lock) |
1631 | __acquires(ci->i_ceph_lock) | |
a8599bd8 | 1632 | { |
874c8ca1 | 1633 | struct inode *inode = &ci->netfs.inode; |
ed9b430c | 1634 | struct ceph_mds_client *mdsc = session->s_mdsc; |
38d46409 | 1635 | struct ceph_client *cl = mdsc->fsc->client; |
a8599bd8 | 1636 | struct ceph_cap_snap *capsnap; |
ed9b430c YZ |
1637 | u64 oldest_flush_tid = 0; |
1638 | u64 first_tid = 1, last_tid = 0; | |
a8599bd8 | 1639 | |
38d46409 XL |
1640 | doutc(cl, "%p %llx.%llx session %p\n", inode, ceph_vinop(inode), |
1641 | session); | |
a8599bd8 | 1642 | |
a8599bd8 | 1643 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
a8599bd8 SW |
1644 | /* |
1645 | * we need to wait for sync writes to complete and for dirty | |
1646 | * pages to be written out. | |
1647 | */ | |
1648 | if (capsnap->dirty_pages || capsnap->writing) | |
cfc0bf66 | 1649 | break; |
a8599bd8 | 1650 | |
86056090 YZ |
1651 | /* should be removed by ceph_try_drop_cap_snap() */ |
1652 | BUG_ON(!capsnap->need_flush); | |
819ccbfa | 1653 | |
e835124c | 1654 | /* only flush each capsnap once */ |
0e294387 | 1655 | if (capsnap->cap_flush.tid > 0) { |
38d46409 | 1656 | doutc(cl, "already flushed %p, skipping\n", capsnap); |
e835124c SW |
1657 | continue; |
1658 | } | |
1659 | ||
553adfd9 | 1660 | spin_lock(&mdsc->cap_dirty_lock); |
0e294387 YZ |
1661 | capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid; |
1662 | list_add_tail(&capsnap->cap_flush.g_list, | |
1663 | &mdsc->cap_flush_list); | |
ed9b430c YZ |
1664 | if (oldest_flush_tid == 0) |
1665 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); | |
0e294387 YZ |
1666 | if (list_empty(&ci->i_flushing_item)) { |
1667 | list_add_tail(&ci->i_flushing_item, | |
1668 | &session->s_cap_flushing); | |
1669 | } | |
553adfd9 YZ |
1670 | spin_unlock(&mdsc->cap_dirty_lock); |
1671 | ||
0e294387 YZ |
1672 | list_add_tail(&capsnap->cap_flush.i_list, |
1673 | &ci->i_cap_flush_list); | |
1674 | ||
ed9b430c YZ |
1675 | if (first_tid == 1) |
1676 | first_tid = capsnap->cap_flush.tid; | |
1677 | last_tid = capsnap->cap_flush.tid; | |
1678 | } | |
1679 | ||
1680 | ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS; | |
1681 | ||
1682 | while (first_tid <= last_tid) { | |
1683 | struct ceph_cap *cap = ci->i_auth_cap; | |
57a5df0e | 1684 | struct ceph_cap_flush *cf = NULL, *iter; |
ed9b430c YZ |
1685 | int ret; |
1686 | ||
1687 | if (!(cap && cap->session == session)) { | |
38d46409 XL |
1688 | doutc(cl, "%p %llx.%llx auth cap %p not mds%d, stop\n", |
1689 | inode, ceph_vinop(inode), cap, session->s_mds); | |
ed9b430c YZ |
1690 | break; |
1691 | } | |
1692 | ||
1693 | ret = -ENOENT; | |
57a5df0e JK |
1694 | list_for_each_entry(iter, &ci->i_cap_flush_list, i_list) { |
1695 | if (iter->tid >= first_tid) { | |
1696 | cf = iter; | |
ed9b430c YZ |
1697 | ret = 0; |
1698 | break; | |
1699 | } | |
1700 | } | |
1701 | if (ret < 0) | |
1702 | break; | |
1703 | ||
1704 | first_tid = cf->tid + 1; | |
1705 | ||
1706 | capsnap = container_of(cf, struct ceph_cap_snap, cap_flush); | |
805692d0 | 1707 | refcount_inc(&capsnap->nref); |
be655596 | 1708 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 | 1709 | |
38d46409 XL |
1710 | doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n", inode, |
1711 | ceph_vinop(inode), capsnap, cf->tid, | |
1712 | ceph_cap_string(capsnap->dirty)); | |
a8599bd8 | 1713 | |
ed9b430c YZ |
1714 | ret = __send_flush_snap(inode, session, capsnap, cap->mseq, |
1715 | oldest_flush_tid); | |
1716 | if (ret < 0) { | |
38d46409 XL |
1717 | pr_err_client(cl, "error sending cap flushsnap, " |
1718 | "ino (%llx.%llx) tid %llu follows %llu\n", | |
1719 | ceph_vinop(inode), cf->tid, | |
1720 | capsnap->follows); | |
ed9b430c | 1721 | } |
a8599bd8 | 1722 | |
ed9b430c | 1723 | ceph_put_cap_snap(capsnap); |
be655596 | 1724 | spin_lock(&ci->i_ceph_lock); |
a8599bd8 | 1725 | } |
ed9b430c | 1726 | } |
a8599bd8 | 1727 | |
ed9b430c YZ |
1728 | void ceph_flush_snaps(struct ceph_inode_info *ci, |
1729 | struct ceph_mds_session **psession) | |
1730 | { | |
874c8ca1 | 1731 | struct inode *inode = &ci->netfs.inode; |
5995d90d | 1732 | struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; |
38d46409 | 1733 | struct ceph_client *cl = ceph_inode_to_client(inode); |
e4d2b16a | 1734 | struct ceph_mds_session *session = NULL; |
409e873e | 1735 | bool need_put = false; |
ed9b430c | 1736 | int mds; |
e4d2b16a | 1737 | |
38d46409 | 1738 | doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode)); |
e4d2b16a YZ |
1739 | if (psession) |
1740 | session = *psession; | |
ed9b430c YZ |
1741 | retry: |
1742 | spin_lock(&ci->i_ceph_lock); | |
1743 | if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) { | |
38d46409 | 1744 | doutc(cl, " no capsnap needs flush, doing nothing\n"); |
ed9b430c YZ |
1745 | goto out; |
1746 | } | |
1747 | if (!ci->i_auth_cap) { | |
38d46409 | 1748 | doutc(cl, " no auth cap (migrating?), doing nothing\n"); |
ed9b430c YZ |
1749 | goto out; |
1750 | } | |
a8599bd8 | 1751 | |
ed9b430c YZ |
1752 | mds = ci->i_auth_cap->session->s_mds; |
1753 | if (session && session->s_mds != mds) { | |
38d46409 | 1754 | doutc(cl, " oops, wrong session %p mutex\n", session); |
a8599bd8 | 1755 | ceph_put_mds_session(session); |
ed9b430c YZ |
1756 | session = NULL; |
1757 | } | |
1758 | if (!session) { | |
1759 | spin_unlock(&ci->i_ceph_lock); | |
1760 | mutex_lock(&mdsc->mutex); | |
1761 | session = __ceph_lookup_mds_session(mdsc, mds); | |
1762 | mutex_unlock(&mdsc->mutex); | |
ed9b430c | 1763 | goto retry; |
a8599bd8 | 1764 | } |
a8599bd8 | 1765 | |
24d063ac | 1766 | // make sure flushsnap messages are sent in proper order. |
054f8d41 | 1767 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) |
24d063ac | 1768 | __kick_flushing_caps(mdsc, session, ci, 0); |
24d063ac | 1769 | |
ed9b430c YZ |
1770 | __ceph_flush_snaps(ci, session); |
1771 | out: | |
be655596 | 1772 | spin_unlock(&ci->i_ceph_lock); |
ed9b430c | 1773 | |
7732fe16 | 1774 | if (psession) |
ed9b430c | 1775 | *psession = session; |
7732fe16 | 1776 | else |
ed9b430c | 1777 | ceph_put_mds_session(session); |
ed9b430c YZ |
1778 | /* we flushed them all; remove this inode from the queue */ |
1779 | spin_lock(&mdsc->snap_flush_lock); | |
409e873e XL |
1780 | if (!list_empty(&ci->i_snap_flush_item)) |
1781 | need_put = true; | |
ed9b430c YZ |
1782 | list_del_init(&ci->i_snap_flush_item); |
1783 | spin_unlock(&mdsc->snap_flush_lock); | |
409e873e XL |
1784 | |
1785 | if (need_put) | |
1786 | iput(inode); | |
a8599bd8 SW |
1787 | } |
1788 | ||
76e3b390 | 1789 | /* |
fca65b4a SW |
1790 | * Mark caps dirty. If inode is newly dirty, return the dirty flags. |
1791 | * Caller is then responsible for calling __mark_inode_dirty with the | |
1792 | * returned flags value. | |
76e3b390 | 1793 | */ |
f66fd9f0 YZ |
1794 | int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask, |
1795 | struct ceph_cap_flush **pcf) | |
76e3b390 | 1796 | { |
640ef79d | 1797 | struct ceph_mds_client *mdsc = |
5995d90d | 1798 | ceph_sb_to_fs_client(ci->netfs.inode.i_sb)->mdsc; |
874c8ca1 | 1799 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 1800 | struct ceph_client *cl = ceph_inode_to_client(inode); |
76e3b390 SW |
1801 | int was = ci->i_dirty_caps; |
1802 | int dirty = 0; | |
1803 | ||
c7e4f85c JL |
1804 | lockdep_assert_held(&ci->i_ceph_lock); |
1805 | ||
571ade33 | 1806 | if (!ci->i_auth_cap) { |
38d46409 XL |
1807 | pr_warn_client(cl, "%p %llx.%llx mask %s, " |
1808 | "but no auth cap (session was closed?)\n", | |
1809 | inode, ceph_vinop(inode), | |
1810 | ceph_cap_string(mask)); | |
571ade33 YZ |
1811 | return 0; |
1812 | } | |
1813 | ||
38d46409 XL |
1814 | doutc(cl, "%p %llx.%llx %s dirty %s -> %s\n", inode, |
1815 | ceph_vinop(inode), ceph_cap_string(mask), | |
1816 | ceph_cap_string(was), ceph_cap_string(was | mask)); | |
76e3b390 SW |
1817 | ci->i_dirty_caps |= mask; |
1818 | if (was == 0) { | |
1cf03a68 JL |
1819 | struct ceph_mds_session *session = ci->i_auth_cap->session; |
1820 | ||
f66fd9f0 YZ |
1821 | WARN_ON_ONCE(ci->i_prealloc_cap_flush); |
1822 | swap(ci->i_prealloc_cap_flush, *pcf); | |
1823 | ||
604d1b02 YZ |
1824 | if (!ci->i_head_snapc) { |
1825 | WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem)); | |
7d8cb26d SW |
1826 | ci->i_head_snapc = ceph_get_snap_context( |
1827 | ci->i_snap_realm->cached_context); | |
604d1b02 | 1828 | } |
38d46409 XL |
1829 | doutc(cl, "%p %llx.%llx now dirty snapc %p auth cap %p\n", |
1830 | inode, ceph_vinop(inode), ci->i_head_snapc, | |
1831 | ci->i_auth_cap); | |
76e3b390 SW |
1832 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
1833 | spin_lock(&mdsc->cap_dirty_lock); | |
1cf03a68 | 1834 | list_add(&ci->i_dirty_item, &session->s_cap_dirty); |
76e3b390 SW |
1835 | spin_unlock(&mdsc->cap_dirty_lock); |
1836 | if (ci->i_flushing_caps == 0) { | |
3772d26d | 1837 | ihold(inode); |
76e3b390 SW |
1838 | dirty |= I_DIRTY_SYNC; |
1839 | } | |
f66fd9f0 YZ |
1840 | } else { |
1841 | WARN_ON_ONCE(!ci->i_prealloc_cap_flush); | |
76e3b390 SW |
1842 | } |
1843 | BUG_ON(list_empty(&ci->i_dirty_item)); | |
1844 | if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && | |
1845 | (mask & CEPH_CAP_FILE_BUFFER)) | |
1846 | dirty |= I_DIRTY_DATASYNC; | |
a0d93e32 | 1847 | __cap_delay_requeue(mdsc, ci); |
fca65b4a | 1848 | return dirty; |
76e3b390 SW |
1849 | } |
1850 | ||
f66fd9f0 YZ |
1851 | struct ceph_cap_flush *ceph_alloc_cap_flush(void) |
1852 | { | |
b2f9fa1f XL |
1853 | struct ceph_cap_flush *cf; |
1854 | ||
1855 | cf = kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL); | |
05a444d3 CIK |
1856 | if (!cf) |
1857 | return NULL; | |
1858 | ||
b2f9fa1f XL |
1859 | cf->is_capsnap = false; |
1860 | return cf; | |
f66fd9f0 YZ |
1861 | } |
1862 | ||
1863 | void ceph_free_cap_flush(struct ceph_cap_flush *cf) | |
1864 | { | |
1865 | if (cf) | |
1866 | kmem_cache_free(ceph_cap_flush_cachep, cf); | |
1867 | } | |
1868 | ||
a2971c8c YZ |
1869 | static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc) |
1870 | { | |
e4500b5e | 1871 | if (!list_empty(&mdsc->cap_flush_list)) { |
a2971c8c | 1872 | struct ceph_cap_flush *cf = |
e4500b5e YZ |
1873 | list_first_entry(&mdsc->cap_flush_list, |
1874 | struct ceph_cap_flush, g_list); | |
a2971c8c YZ |
1875 | return cf->tid; |
1876 | } | |
1877 | return 0; | |
1878 | } | |
1879 | ||
c8799fc4 YZ |
1880 | /* |
1881 | * Remove cap_flush from the mdsc's or inode's flushing cap list. | |
1882 | * Return true if caller needs to wake up flush waiters. | |
1883 | */ | |
681ac634 JL |
1884 | static bool __detach_cap_flush_from_mdsc(struct ceph_mds_client *mdsc, |
1885 | struct ceph_cap_flush *cf) | |
c8799fc4 YZ |
1886 | { |
1887 | struct ceph_cap_flush *prev; | |
1888 | bool wake = cf->wake; | |
681ac634 JL |
1889 | |
1890 | if (wake && cf->g_list.prev != &mdsc->cap_flush_list) { | |
1891 | prev = list_prev_entry(cf, g_list); | |
1892 | prev->wake = true; | |
1893 | wake = false; | |
1894 | } | |
b2f9fa1f | 1895 | list_del_init(&cf->g_list); |
681ac634 JL |
1896 | return wake; |
1897 | } | |
1898 | ||
1899 | static bool __detach_cap_flush_from_ci(struct ceph_inode_info *ci, | |
1900 | struct ceph_cap_flush *cf) | |
1901 | { | |
1902 | struct ceph_cap_flush *prev; | |
1903 | bool wake = cf->wake; | |
1904 | ||
1905 | if (wake && cf->i_list.prev != &ci->i_cap_flush_list) { | |
1906 | prev = list_prev_entry(cf, i_list); | |
1907 | prev->wake = true; | |
1908 | wake = false; | |
c8799fc4 | 1909 | } |
b2f9fa1f | 1910 | list_del_init(&cf->i_list); |
c8799fc4 YZ |
1911 | return wake; |
1912 | } | |
1913 | ||
a8599bd8 SW |
1914 | /* |
1915 | * Add dirty inode to the flushing list. Assigned a seq number so we | |
1916 | * can wait for caps to flush without starving. | |
cdc35f96 | 1917 | * |
9f3345d8 | 1918 | * Called under i_ceph_lock. Returns the flush tid. |
a8599bd8 | 1919 | */ |
9f3345d8 | 1920 | static u64 __mark_caps_flushing(struct inode *inode, |
c8799fc4 | 1921 | struct ceph_mds_session *session, bool wake, |
9f3345d8 | 1922 | u64 *oldest_flush_tid) |
a8599bd8 | 1923 | { |
5995d90d | 1924 | struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
38d46409 | 1925 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 | 1926 | struct ceph_inode_info *ci = ceph_inode(inode); |
f66fd9f0 | 1927 | struct ceph_cap_flush *cf = NULL; |
cdc35f96 | 1928 | int flushing; |
50b885b9 | 1929 | |
c7e4f85c | 1930 | lockdep_assert_held(&ci->i_ceph_lock); |
cdc35f96 | 1931 | BUG_ON(ci->i_dirty_caps == 0); |
a8599bd8 | 1932 | BUG_ON(list_empty(&ci->i_dirty_item)); |
f66fd9f0 | 1933 | BUG_ON(!ci->i_prealloc_cap_flush); |
cdc35f96 SW |
1934 | |
1935 | flushing = ci->i_dirty_caps; | |
38d46409 XL |
1936 | doutc(cl, "flushing %s, flushing_caps %s -> %s\n", |
1937 | ceph_cap_string(flushing), | |
1938 | ceph_cap_string(ci->i_flushing_caps), | |
1939 | ceph_cap_string(ci->i_flushing_caps | flushing)); | |
cdc35f96 SW |
1940 | ci->i_flushing_caps |= flushing; |
1941 | ci->i_dirty_caps = 0; | |
38d46409 | 1942 | doutc(cl, "%p %llx.%llx now !dirty\n", inode, ceph_vinop(inode)); |
cdc35f96 | 1943 | |
f66fd9f0 | 1944 | swap(cf, ci->i_prealloc_cap_flush); |
553adfd9 | 1945 | cf->caps = flushing; |
c8799fc4 | 1946 | cf->wake = wake; |
553adfd9 | 1947 | |
a8599bd8 | 1948 | spin_lock(&mdsc->cap_dirty_lock); |
afcdaea3 SW |
1949 | list_del_init(&ci->i_dirty_item); |
1950 | ||
553adfd9 | 1951 | cf->tid = ++mdsc->last_cap_flush_tid; |
e4500b5e | 1952 | list_add_tail(&cf->g_list, &mdsc->cap_flush_list); |
a2971c8c | 1953 | *oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
553adfd9 | 1954 | |
a8599bd8 SW |
1955 | if (list_empty(&ci->i_flushing_item)) { |
1956 | list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); | |
1957 | mdsc->num_cap_flushing++; | |
a8599bd8 SW |
1958 | } |
1959 | spin_unlock(&mdsc->cap_dirty_lock); | |
cdc35f96 | 1960 | |
e4500b5e | 1961 | list_add_tail(&cf->i_list, &ci->i_cap_flush_list); |
553adfd9 | 1962 | |
9f3345d8 | 1963 | return cf->tid; |
a8599bd8 SW |
1964 | } |
1965 | ||
5ecad6fd SW |
1966 | /* |
1967 | * try to invalidate mapping pages without blocking. | |
1968 | */ | |
5ecad6fd | 1969 | static int try_nonblocking_invalidate(struct inode *inode) |
3eaf5aa1 JL |
1970 | __releases(ci->i_ceph_lock) |
1971 | __acquires(ci->i_ceph_lock) | |
5ecad6fd | 1972 | { |
38d46409 | 1973 | struct ceph_client *cl = ceph_inode_to_client(inode); |
5ecad6fd SW |
1974 | struct ceph_inode_info *ci = ceph_inode(inode); |
1975 | u32 invalidating_gen = ci->i_rdcache_gen; | |
1976 | ||
be655596 | 1977 | spin_unlock(&ci->i_ceph_lock); |
400e1286 | 1978 | ceph_fscache_invalidate(inode, false); |
5ecad6fd | 1979 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
be655596 | 1980 | spin_lock(&ci->i_ceph_lock); |
5ecad6fd | 1981 | |
18a38193 | 1982 | if (inode->i_data.nrpages == 0 && |
5ecad6fd SW |
1983 | invalidating_gen == ci->i_rdcache_gen) { |
1984 | /* success. */ | |
38d46409 XL |
1985 | doutc(cl, "%p %llx.%llx success\n", inode, |
1986 | ceph_vinop(inode)); | |
cd045cb4 SW |
1987 | /* save any racing async invalidate some trouble */ |
1988 | ci->i_rdcache_revoking = ci->i_rdcache_gen - 1; | |
5ecad6fd SW |
1989 | return 0; |
1990 | } | |
38d46409 | 1991 | doutc(cl, "%p %llx.%llx failed\n", inode, ceph_vinop(inode)); |
5ecad6fd SW |
1992 | return -1; |
1993 | } | |
1994 | ||
efb0ca76 YZ |
1995 | bool __ceph_should_report_size(struct ceph_inode_info *ci) |
1996 | { | |
874c8ca1 | 1997 | loff_t size = i_size_read(&ci->netfs.inode); |
efb0ca76 YZ |
1998 | /* mds will adjust max size according to the reported size */ |
1999 | if (ci->i_flushing_caps & CEPH_CAP_FILE_WR) | |
2000 | return false; | |
2001 | if (size >= ci->i_max_size) | |
2002 | return true; | |
2003 | /* half of previous max_size increment has been used */ | |
2004 | if (ci->i_max_size > ci->i_reported_size && | |
2005 | (size << 1) >= ci->i_max_size + ci->i_reported_size) | |
2006 | return true; | |
2007 | return false; | |
2008 | } | |
2009 | ||
a8599bd8 SW |
2010 | /* |
2011 | * Swiss army knife function to examine currently used and wanted | |
2012 | * versus held caps. Release, flush, ack revoked caps to mds as | |
2013 | * appropriate. | |
2014 | * | |
a8599bd8 SW |
2015 | * CHECK_CAPS_AUTHONLY - we should only check the auth cap |
2016 | * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without | |
2017 | * further delay. | |
2018 | */ | |
e4b731cc | 2019 | void ceph_check_caps(struct ceph_inode_info *ci, int flags) |
a8599bd8 | 2020 | { |
874c8ca1 | 2021 | struct inode *inode = &ci->netfs.inode; |
2678da88 | 2022 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb); |
38d46409 | 2023 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 | 2024 | struct ceph_cap *cap; |
a2971c8c | 2025 | u64 flush_tid, oldest_flush_tid; |
395c312b | 2026 | int file_wanted, used, cap_used; |
cbd03635 | 2027 | int issued, implemented, want, retain, revoking, flushing = 0; |
a8599bd8 SW |
2028 | int mds = -1; /* keep track of how far we've gone through i_caps list |
2029 | to avoid an infinite loop on retry */ | |
2030 | struct rb_node *p; | |
3609404f | 2031 | bool queue_invalidate = false; |
3609404f | 2032 | bool tried_invalidate = false; |
a7437954 | 2033 | bool queue_writeback = false; |
e4b731cc | 2034 | struct ceph_mds_session *session = NULL; |
6a92b08f | 2035 | |
be655596 | 2036 | spin_lock(&ci->i_ceph_lock); |
fbed7045 | 2037 | if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) { |
68c62bee XL |
2038 | ci->i_ceph_flags |= CEPH_I_ASYNC_CHECK_CAPS; |
2039 | ||
fbed7045 JL |
2040 | /* Don't send messages until we get async create reply */ |
2041 | spin_unlock(&ci->i_ceph_lock); | |
fbed7045 JL |
2042 | return; |
2043 | } | |
2044 | ||
a8599bd8 SW |
2045 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
2046 | flags |= CHECK_CAPS_FLUSH; | |
a8599bd8 | 2047 | retry: |
c74d79af | 2048 | /* Caps wanted by virtue of active open files. */ |
a8599bd8 | 2049 | file_wanted = __ceph_caps_file_wanted(ci); |
c74d79af JL |
2050 | |
2051 | /* Caps which have active references against them */ | |
a8599bd8 | 2052 | used = __ceph_caps_used(ci); |
c74d79af JL |
2053 | |
2054 | /* | |
2055 | * "issued" represents the current caps that the MDS wants us to have. | |
2056 | * "implemented" is the set that we have been granted, and includes the | |
2057 | * ones that have not yet been returned to the MDS (the "revoking" set, | |
2058 | * usually because they have outstanding references). | |
2059 | */ | |
cbd03635 SW |
2060 | issued = __ceph_caps_issued(ci, &implemented); |
2061 | revoking = implemented & ~issued; | |
a8599bd8 | 2062 | |
41445999 | 2063 | want = file_wanted; |
c74d79af JL |
2064 | |
2065 | /* The ones we currently want to retain (may be adjusted below) */ | |
41445999 | 2066 | retain = file_wanted | used | CEPH_CAP_PIN; |
a8599bd8 | 2067 | if (!mdsc->stopping && inode->i_nlink > 0) { |
41445999 | 2068 | if (file_wanted) { |
a8599bd8 | 2069 | retain |= CEPH_CAP_ANY; /* be greedy */ |
32ec4397 YZ |
2070 | } else if (S_ISDIR(inode->i_mode) && |
2071 | (issued & CEPH_CAP_FILE_SHARED) && | |
8a2ac3a8 | 2072 | __ceph_dir_is_complete(ci)) { |
32ec4397 YZ |
2073 | /* |
2074 | * If a directory is complete, we want to keep | |
2075 | * the exclusive cap. So that MDS does not end up | |
2076 | * revoking the shared cap on every create/unlink | |
2077 | * operation. | |
2078 | */ | |
a25949b9 | 2079 | if (IS_RDONLY(inode)) { |
8a2ac3a8 | 2080 | want = CEPH_CAP_ANY_SHARED; |
a25949b9 | 2081 | } else { |
719a2514 | 2082 | want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL; |
a25949b9 | 2083 | } |
32ec4397 | 2084 | retain |= want; |
a8599bd8 | 2085 | } else { |
32ec4397 | 2086 | |
a8599bd8 SW |
2087 | retain |= CEPH_CAP_ANY_SHARED; |
2088 | /* | |
2089 | * keep RD only if we didn't have the file open RW, | |
2090 | * because then the mds would revoke it anyway to | |
2091 | * journal max_size=0. | |
2092 | */ | |
2093 | if (ci->i_max_size == 0) | |
2094 | retain |= CEPH_CAP_ANY_RD; | |
2095 | } | |
2096 | } | |
2097 | ||
38d46409 XL |
2098 | doutc(cl, "%p %llx.%llx file_want %s used %s dirty %s " |
2099 | "flushing %s issued %s revoking %s retain %s %s%s%s\n", | |
2100 | inode, ceph_vinop(inode), ceph_cap_string(file_wanted), | |
a8599bd8 SW |
2101 | ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), |
2102 | ceph_cap_string(ci->i_flushing_caps), | |
cbd03635 | 2103 | ceph_cap_string(issued), ceph_cap_string(revoking), |
a8599bd8 SW |
2104 | ceph_cap_string(retain), |
2105 | (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", | |
e027ddb6 XL |
2106 | (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "", |
2107 | (flags & CHECK_CAPS_NOINVAL) ? " NOINVAL" : ""); | |
a8599bd8 SW |
2108 | |
2109 | /* | |
2110 | * If we no longer need to hold onto old our caps, and we may | |
2111 | * have cached pages, but don't want them, then try to invalidate. | |
2112 | * If we fail, it's because pages are locked.... try again later. | |
2113 | */ | |
a0d93e32 | 2114 | if ((!(flags & CHECK_CAPS_NOINVAL) || mdsc->stopping) && |
525d15e8 | 2115 | S_ISREG(inode->i_mode) && |
9abd4db7 | 2116 | !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */ |
fdd4e158 | 2117 | inode->i_data.nrpages && /* have cached pages */ |
5e804ac4 YZ |
2118 | (revoking & (CEPH_CAP_FILE_CACHE| |
2119 | CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */ | |
a8599bd8 | 2120 | !tried_invalidate) { |
38d46409 XL |
2121 | doutc(cl, "trying to invalidate on %p %llx.%llx\n", |
2122 | inode, ceph_vinop(inode)); | |
5ecad6fd | 2123 | if (try_nonblocking_invalidate(inode) < 0) { |
38d46409 | 2124 | doutc(cl, "queuing invalidate\n"); |
ee612d95 YZ |
2125 | queue_invalidate = true; |
2126 | ci->i_rdcache_revoking = ci->i_rdcache_gen; | |
a8599bd8 | 2127 | } |
3609404f | 2128 | tried_invalidate = true; |
6a92b08f | 2129 | goto retry; |
a8599bd8 SW |
2130 | } |
2131 | ||
a8599bd8 | 2132 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
d67c72e6 | 2133 | int mflags = 0; |
0a454bdd JL |
2134 | struct cap_msg_args arg; |
2135 | ||
a8599bd8 | 2136 | cap = rb_entry(p, struct ceph_cap, ci_node); |
a8599bd8 SW |
2137 | |
2138 | /* avoid looping forever */ | |
2139 | if (mds >= cap->mds || | |
2140 | ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) | |
2141 | continue; | |
2142 | ||
c74d79af JL |
2143 | /* |
2144 | * If we have an auth cap, we don't need to consider any | |
2145 | * overlapping caps as used. | |
2146 | */ | |
395c312b YZ |
2147 | cap_used = used; |
2148 | if (ci->i_auth_cap && cap != ci->i_auth_cap) | |
2149 | cap_used &= ~ci->i_auth_cap->issued; | |
2150 | ||
a8599bd8 | 2151 | revoking = cap->implemented & ~cap->issued; |
38d46409 XL |
2152 | doutc(cl, " mds%d cap %p used %s issued %s implemented %s revoking %s\n", |
2153 | cap->mds, cap, ceph_cap_string(cap_used), | |
2154 | ceph_cap_string(cap->issued), | |
2155 | ceph_cap_string(cap->implemented), | |
2156 | ceph_cap_string(revoking)); | |
a8599bd8 SW |
2157 | |
2158 | if (cap == ci->i_auth_cap && | |
2159 | (cap->issued & CEPH_CAP_FILE_WR)) { | |
2160 | /* request larger max_size from MDS? */ | |
2161 | if (ci->i_wanted_max_size > ci->i_max_size && | |
2162 | ci->i_wanted_max_size > ci->i_requested_max_size) { | |
38d46409 | 2163 | doutc(cl, "requesting new max_size\n"); |
a8599bd8 SW |
2164 | goto ack; |
2165 | } | |
2166 | ||
2167 | /* approaching file_max? */ | |
efb0ca76 | 2168 | if (__ceph_should_report_size(ci)) { |
38d46409 | 2169 | doutc(cl, "i_size approaching max_size\n"); |
a8599bd8 SW |
2170 | goto ack; |
2171 | } | |
2172 | } | |
2173 | /* flush anything dirty? */ | |
7bc00fdd YZ |
2174 | if (cap == ci->i_auth_cap) { |
2175 | if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) { | |
38d46409 | 2176 | doutc(cl, "flushing dirty caps\n"); |
7bc00fdd YZ |
2177 | goto ack; |
2178 | } | |
2179 | if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) { | |
38d46409 | 2180 | doutc(cl, "flushing snap caps\n"); |
7bc00fdd YZ |
2181 | goto ack; |
2182 | } | |
a8599bd8 SW |
2183 | } |
2184 | ||
2185 | /* completed revocation? going down and there are no caps? */ | |
a7437954 XL |
2186 | if (revoking) { |
2187 | if ((revoking & cap_used) == 0) { | |
38d46409 | 2188 | doutc(cl, "completed revocation of %s\n", |
a7437954 XL |
2189 | ceph_cap_string(cap->implemented & ~cap->issued)); |
2190 | goto ack; | |
2191 | } | |
2192 | ||
2193 | /* | |
2194 | * If the "i_wrbuffer_ref" was increased by mmap or generic | |
2195 | * cache write just before the ceph_check_caps() is called, | |
2196 | * the Fb capability revoking will fail this time. Then we | |
2197 | * must wait for the BDI's delayed work to flush the dirty | |
2198 | * pages and to release the "i_wrbuffer_ref", which will cost | |
2199 | * at most 5 seconds. That means the MDS needs to wait at | |
2200 | * most 5 seconds to finished the Fb capability's revocation. | |
2201 | * | |
2202 | * Let's queue a writeback for it. | |
2203 | */ | |
2204 | if (S_ISREG(inode->i_mode) && ci->i_wrbuffer_ref && | |
2205 | (revoking & CEPH_CAP_FILE_BUFFER)) | |
2206 | queue_writeback = true; | |
a8599bd8 SW |
2207 | } |
2208 | ||
2209 | /* want more caps from mds? */ | |
0aa971b6 YZ |
2210 | if (want & ~cap->mds_wanted) { |
2211 | if (want & ~(cap->mds_wanted | cap->issued)) | |
2212 | goto ack; | |
2213 | if (!__cap_is_valid(cap)) | |
2214 | goto ack; | |
2215 | } | |
a8599bd8 SW |
2216 | |
2217 | /* things we might delay */ | |
fdac94fa | 2218 | if ((cap->issued & ~retain) == 0) |
a8599bd8 SW |
2219 | continue; /* nope, all good */ |
2220 | ||
a8599bd8 | 2221 | ack: |
6a92b08f JL |
2222 | ceph_put_mds_session(session); |
2223 | session = ceph_get_mds_session(cap->session); | |
7bc00fdd YZ |
2224 | |
2225 | /* kick flushing and flush snaps before sending normal | |
2226 | * cap message */ | |
2227 | if (cap == ci->i_auth_cap && | |
2228 | (ci->i_ceph_flags & | |
2229 | (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) { | |
054f8d41 | 2230 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) |
24d063ac | 2231 | __kick_flushing_caps(mdsc, session, ci, 0); |
ed9b430c YZ |
2232 | if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) |
2233 | __ceph_flush_snaps(ci, session); | |
2234 | ||
6a92b08f | 2235 | goto retry; |
a8599bd8 SW |
2236 | } |
2237 | ||
553adfd9 | 2238 | if (cap == ci->i_auth_cap && ci->i_dirty_caps) { |
9f3345d8 JL |
2239 | flushing = ci->i_dirty_caps; |
2240 | flush_tid = __mark_caps_flushing(inode, session, false, | |
2241 | &oldest_flush_tid); | |
d67c72e6 JL |
2242 | if (flags & CHECK_CAPS_FLUSH && |
2243 | list_empty(&session->s_cap_dirty)) | |
2244 | mflags |= CEPH_CLIENT_CAPS_SYNC; | |
553adfd9 | 2245 | } else { |
24be0c48 | 2246 | flushing = 0; |
553adfd9 | 2247 | flush_tid = 0; |
a2971c8c YZ |
2248 | spin_lock(&mdsc->cap_dirty_lock); |
2249 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); | |
2250 | spin_unlock(&mdsc->cap_dirty_lock); | |
553adfd9 | 2251 | } |
a8599bd8 SW |
2252 | |
2253 | mds = cap->mds; /* remember mds, so we don't repeat */ | |
a8599bd8 | 2254 | |
d67c72e6 JL |
2255 | __prep_cap(&arg, cap, CEPH_CAP_OP_UPDATE, mflags, cap_used, |
2256 | want, retain, flushing, flush_tid, oldest_flush_tid); | |
0a454bdd | 2257 | |
6a92b08f | 2258 | spin_unlock(&ci->i_ceph_lock); |
52311980 | 2259 | __send_cap(&arg, ci); |
6a92b08f | 2260 | spin_lock(&ci->i_ceph_lock); |
0a454bdd | 2261 | |
be655596 | 2262 | goto retry; /* retake i_ceph_lock and restart our cap scan. */ |
a8599bd8 SW |
2263 | } |
2264 | ||
a0d93e32 YZ |
2265 | /* periodically re-calculate caps wanted by open files */ |
2266 | if (__ceph_is_any_real_caps(ci) && | |
2267 | list_empty(&ci->i_cap_delay_list) && | |
2268 | (file_wanted & ~CEPH_CAP_PIN) && | |
2269 | !(used & (CEPH_CAP_FILE_RD | CEPH_CAP_ANY_FILE_WR))) { | |
2270 | __cap_delay_requeue(mdsc, ci); | |
719a2514 | 2271 | } |
a8599bd8 | 2272 | |
be655596 | 2273 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 | 2274 | |
6a92b08f | 2275 | ceph_put_mds_session(session); |
a7437954 XL |
2276 | if (queue_writeback) |
2277 | ceph_queue_writeback(inode); | |
cbd03635 | 2278 | if (queue_invalidate) |
3c6f6b79 | 2279 | ceph_queue_invalidate(inode); |
a8599bd8 SW |
2280 | } |
2281 | ||
a8599bd8 SW |
2282 | /* |
2283 | * Try to flush dirty caps back to the auth mds. | |
2284 | */ | |
553adfd9 | 2285 | static int try_flush_caps(struct inode *inode, u64 *ptid) |
a8599bd8 | 2286 | { |
5995d90d | 2287 | struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
a8599bd8 | 2288 | struct ceph_inode_info *ci = ceph_inode(inode); |
89b52fe1 | 2289 | int flushing = 0; |
a2971c8c | 2290 | u64 flush_tid = 0, oldest_flush_tid = 0; |
a8599bd8 | 2291 | |
be655596 | 2292 | spin_lock(&ci->i_ceph_lock); |
d6cee9db | 2293 | retry_locked: |
a8599bd8 SW |
2294 | if (ci->i_dirty_caps && ci->i_auth_cap) { |
2295 | struct ceph_cap *cap = ci->i_auth_cap; | |
0a454bdd | 2296 | struct cap_msg_args arg; |
0449a352 | 2297 | struct ceph_mds_session *session = cap->session; |
a8599bd8 | 2298 | |
0449a352 | 2299 | if (session->s_state < CEPH_MDS_SESSION_OPEN) { |
6c2838fb | 2300 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 | 2301 | goto out; |
6c2838fb | 2302 | } |
a8599bd8 | 2303 | |
d6cee9db YZ |
2304 | if (ci->i_ceph_flags & |
2305 | (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) { | |
2306 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) | |
2307 | __kick_flushing_caps(mdsc, session, ci, 0); | |
2308 | if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) | |
2309 | __ceph_flush_snaps(ci, session); | |
2310 | goto retry_locked; | |
2311 | } | |
2312 | ||
9f3345d8 JL |
2313 | flushing = ci->i_dirty_caps; |
2314 | flush_tid = __mark_caps_flushing(inode, session, true, | |
2315 | &oldest_flush_tid); | |
a8599bd8 | 2316 | |
0a454bdd | 2317 | __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, CEPH_CLIENT_CAPS_SYNC, |
a0d93e32 YZ |
2318 | __ceph_caps_used(ci), __ceph_caps_wanted(ci), |
2319 | (cap->issued | cap->implemented), | |
2320 | flushing, flush_tid, oldest_flush_tid); | |
0a454bdd JL |
2321 | spin_unlock(&ci->i_ceph_lock); |
2322 | ||
52311980 | 2323 | __send_cap(&arg, ci); |
553adfd9 | 2324 | } else { |
e4500b5e | 2325 | if (!list_empty(&ci->i_cap_flush_list)) { |
553adfd9 | 2326 | struct ceph_cap_flush *cf = |
e4500b5e | 2327 | list_last_entry(&ci->i_cap_flush_list, |
c8799fc4 YZ |
2328 | struct ceph_cap_flush, i_list); |
2329 | cf->wake = true; | |
553adfd9 YZ |
2330 | flush_tid = cf->tid; |
2331 | } | |
2332 | flushing = ci->i_flushing_caps; | |
2333 | spin_unlock(&ci->i_ceph_lock); | |
a8599bd8 SW |
2334 | } |
2335 | out: | |
553adfd9 | 2336 | *ptid = flush_tid; |
a8599bd8 SW |
2337 | return flushing; |
2338 | } | |
2339 | ||
2340 | /* | |
2341 | * Return true if we've flushed caps through the given flush_tid. | |
2342 | */ | |
553adfd9 | 2343 | static int caps_are_flushed(struct inode *inode, u64 flush_tid) |
a8599bd8 SW |
2344 | { |
2345 | struct ceph_inode_info *ci = ceph_inode(inode); | |
553adfd9 | 2346 | int ret = 1; |
a8599bd8 | 2347 | |
be655596 | 2348 | spin_lock(&ci->i_ceph_lock); |
e4500b5e YZ |
2349 | if (!list_empty(&ci->i_cap_flush_list)) { |
2350 | struct ceph_cap_flush * cf = | |
2351 | list_first_entry(&ci->i_cap_flush_list, | |
2352 | struct ceph_cap_flush, i_list); | |
553adfd9 | 2353 | if (cf->tid <= flush_tid) |
a8599bd8 | 2354 | ret = 0; |
89b52fe1 | 2355 | } |
be655596 | 2356 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
2357 | return ret; |
2358 | } | |
2359 | ||
da819c81 | 2360 | /* |
ae067063 | 2361 | * flush the mdlog and wait for any unsafe requests to complete. |
da819c81 | 2362 | */ |
ae067063 | 2363 | static int flush_mdlog_and_wait_inode_unsafe_requests(struct inode *inode) |
da819c81 | 2364 | { |
5995d90d | 2365 | struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
38d46409 | 2366 | struct ceph_client *cl = ceph_inode_to_client(inode); |
da819c81 | 2367 | struct ceph_inode_info *ci = ceph_inode(inode); |
68cd5b4b YZ |
2368 | struct ceph_mds_request *req1 = NULL, *req2 = NULL; |
2369 | int ret, err = 0; | |
da819c81 YZ |
2370 | |
2371 | spin_lock(&ci->i_unsafe_lock); | |
68cd5b4b YZ |
2372 | if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) { |
2373 | req1 = list_last_entry(&ci->i_unsafe_dirops, | |
2374 | struct ceph_mds_request, | |
2375 | r_unsafe_dir_item); | |
2376 | ceph_mdsc_get_request(req1); | |
2377 | } | |
2378 | if (!list_empty(&ci->i_unsafe_iops)) { | |
2379 | req2 = list_last_entry(&ci->i_unsafe_iops, | |
2380 | struct ceph_mds_request, | |
2381 | r_unsafe_target_item); | |
2382 | ceph_mdsc_get_request(req2); | |
2383 | } | |
2384 | spin_unlock(&ci->i_unsafe_lock); | |
da819c81 | 2385 | |
e1a4541e XL |
2386 | /* |
2387 | * Trigger to flush the journal logs in all the relevant MDSes | |
2388 | * manually, or in the worst case we must wait at most 5 seconds | |
2389 | * to wait the journal logs to be flushed by the MDSes periodically. | |
2390 | */ | |
5bd76b8d | 2391 | if (req1 || req2) { |
e1a4541e | 2392 | struct ceph_mds_request *req; |
5bd76b8d XL |
2393 | struct ceph_mds_session **sessions; |
2394 | struct ceph_mds_session *s; | |
2395 | unsigned int max_sessions; | |
e1a4541e XL |
2396 | int i; |
2397 | ||
5bd76b8d XL |
2398 | mutex_lock(&mdsc->mutex); |
2399 | max_sessions = mdsc->max_sessions; | |
2400 | ||
aa1d6272 | 2401 | sessions = kcalloc(max_sessions, sizeof(s), GFP_KERNEL); |
89d43d05 | 2402 | if (!sessions) { |
5bd76b8d | 2403 | mutex_unlock(&mdsc->mutex); |
89d43d05 XL |
2404 | err = -ENOMEM; |
2405 | goto out; | |
2406 | } | |
e1a4541e XL |
2407 | |
2408 | spin_lock(&ci->i_unsafe_lock); | |
2409 | if (req1) { | |
2410 | list_for_each_entry(req, &ci->i_unsafe_dirops, | |
2411 | r_unsafe_dir_item) { | |
2412 | s = req->r_session; | |
7acae618 XL |
2413 | if (!s) |
2414 | continue; | |
e1a4541e XL |
2415 | if (!sessions[s->s_mds]) { |
2416 | s = ceph_get_mds_session(s); | |
2417 | sessions[s->s_mds] = s; | |
2418 | } | |
2419 | } | |
2420 | } | |
2421 | if (req2) { | |
2422 | list_for_each_entry(req, &ci->i_unsafe_iops, | |
2423 | r_unsafe_target_item) { | |
2424 | s = req->r_session; | |
7acae618 XL |
2425 | if (!s) |
2426 | continue; | |
e1a4541e XL |
2427 | if (!sessions[s->s_mds]) { |
2428 | s = ceph_get_mds_session(s); | |
2429 | sessions[s->s_mds] = s; | |
2430 | } | |
2431 | } | |
2432 | } | |
2433 | spin_unlock(&ci->i_unsafe_lock); | |
2434 | ||
2435 | /* the auth MDS */ | |
2436 | spin_lock(&ci->i_ceph_lock); | |
2437 | if (ci->i_auth_cap) { | |
5bd76b8d XL |
2438 | s = ci->i_auth_cap->session; |
2439 | if (!sessions[s->s_mds]) | |
2440 | sessions[s->s_mds] = ceph_get_mds_session(s); | |
e1a4541e XL |
2441 | } |
2442 | spin_unlock(&ci->i_ceph_lock); | |
5bd76b8d | 2443 | mutex_unlock(&mdsc->mutex); |
e1a4541e XL |
2444 | |
2445 | /* send flush mdlog request to MDSes */ | |
89d43d05 | 2446 | for (i = 0; i < max_sessions; i++) { |
e1a4541e XL |
2447 | s = sessions[i]; |
2448 | if (s) { | |
2449 | send_flush_mdlog(s); | |
2450 | ceph_put_mds_session(s); | |
2451 | } | |
2452 | } | |
2453 | kfree(sessions); | |
2454 | } | |
2455 | ||
38d46409 XL |
2456 | doutc(cl, "%p %llx.%llx wait on tid %llu %llu\n", inode, |
2457 | ceph_vinop(inode), req1 ? req1->r_tid : 0ULL, | |
2458 | req2 ? req2->r_tid : 0ULL); | |
68cd5b4b YZ |
2459 | if (req1) { |
2460 | ret = !wait_for_completion_timeout(&req1->r_safe_completion, | |
2461 | ceph_timeout_jiffies(req1->r_timeout)); | |
da819c81 | 2462 | if (ret) |
68cd5b4b | 2463 | err = -EIO; |
68cd5b4b YZ |
2464 | } |
2465 | if (req2) { | |
2466 | ret = !wait_for_completion_timeout(&req2->r_safe_completion, | |
2467 | ceph_timeout_jiffies(req2->r_timeout)); | |
2468 | if (ret) | |
2469 | err = -EIO; | |
68cd5b4b | 2470 | } |
89d43d05 XL |
2471 | |
2472 | out: | |
2473 | if (req1) | |
2474 | ceph_mdsc_put_request(req1); | |
2475 | if (req2) | |
2476 | ceph_mdsc_put_request(req2); | |
68cd5b4b | 2477 | return err; |
da819c81 YZ |
2478 | } |
2479 | ||
02c24a82 | 2480 | int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
a8599bd8 | 2481 | { |
7ea80859 | 2482 | struct inode *inode = file->f_mapping->host; |
a8599bd8 | 2483 | struct ceph_inode_info *ci = ceph_inode(inode); |
38d46409 | 2484 | struct ceph_client *cl = ceph_inode_to_client(inode); |
553adfd9 | 2485 | u64 flush_tid; |
f4b97866 | 2486 | int ret, err; |
a8599bd8 SW |
2487 | int dirty; |
2488 | ||
38d46409 XL |
2489 | doutc(cl, "%p %llx.%llx%s\n", inode, ceph_vinop(inode), |
2490 | datasync ? " datasync" : ""); | |
9a5530c6 | 2491 | |
b74fceae | 2492 | ret = file_write_and_wait_range(file, start, end); |
da819c81 YZ |
2493 | if (datasync) |
2494 | goto out; | |
2495 | ||
891f3f5a JL |
2496 | ret = ceph_wait_on_async_create(inode); |
2497 | if (ret) | |
2498 | goto out; | |
2499 | ||
553adfd9 | 2500 | dirty = try_flush_caps(inode, &flush_tid); |
38d46409 | 2501 | doutc(cl, "dirty caps are %s\n", ceph_cap_string(dirty)); |
a8599bd8 | 2502 | |
ae067063 | 2503 | err = flush_mdlog_and_wait_inode_unsafe_requests(inode); |
da819c81 | 2504 | |
a8599bd8 SW |
2505 | /* |
2506 | * only wait on non-file metadata writeback (the mds | |
2507 | * can recover size and mtime, so we don't need to | |
2508 | * wait for that) | |
2509 | */ | |
f4b97866 YZ |
2510 | if (!err && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { |
2511 | err = wait_event_interruptible(ci->i_cap_wq, | |
da819c81 | 2512 | caps_are_flushed(inode, flush_tid)); |
a8599bd8 | 2513 | } |
f4b97866 YZ |
2514 | |
2515 | if (err < 0) | |
2516 | ret = err; | |
2517 | ||
1bd85aa6 JL |
2518 | err = file_check_and_advance_wb_err(file); |
2519 | if (err < 0) | |
2520 | ret = err; | |
da819c81 | 2521 | out: |
38d46409 XL |
2522 | doutc(cl, "%p %llx.%llx%s result=%d\n", inode, ceph_vinop(inode), |
2523 | datasync ? " datasync" : "", ret); | |
a8599bd8 SW |
2524 | return ret; |
2525 | } | |
2526 | ||
2527 | /* | |
2528 | * Flush any dirty caps back to the mds. If we aren't asked to wait, | |
2529 | * queue inode for flush but don't do so immediately, because we can | |
2530 | * get by with fewer MDS messages if we wait for data writeback to | |
2531 | * complete first. | |
2532 | */ | |
f1a3d572 | 2533 | int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) |
a8599bd8 SW |
2534 | { |
2535 | struct ceph_inode_info *ci = ceph_inode(inode); | |
38d46409 | 2536 | struct ceph_client *cl = ceph_inode_to_client(inode); |
553adfd9 | 2537 | u64 flush_tid; |
a8599bd8 SW |
2538 | int err = 0; |
2539 | int dirty; | |
16515a6d | 2540 | int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync); |
a8599bd8 | 2541 | |
38d46409 | 2542 | doutc(cl, "%p %llx.%llx wait=%d\n", inode, ceph_vinop(inode), wait); |
400e1286 | 2543 | ceph_fscache_unpin_writeback(inode, wbc); |
a8599bd8 | 2544 | if (wait) { |
fbed7045 JL |
2545 | err = ceph_wait_on_async_create(inode); |
2546 | if (err) | |
2547 | return err; | |
553adfd9 | 2548 | dirty = try_flush_caps(inode, &flush_tid); |
a8599bd8 SW |
2549 | if (dirty) |
2550 | err = wait_event_interruptible(ci->i_cap_wq, | |
2551 | caps_are_flushed(inode, flush_tid)); | |
2552 | } else { | |
640ef79d | 2553 | struct ceph_mds_client *mdsc = |
5995d90d | 2554 | ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
a8599bd8 | 2555 | |
be655596 | 2556 | spin_lock(&ci->i_ceph_lock); |
a8599bd8 SW |
2557 | if (__ceph_caps_dirty(ci)) |
2558 | __cap_delay_requeue_front(mdsc, ci); | |
be655596 | 2559 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
2560 | } |
2561 | return err; | |
2562 | } | |
2563 | ||
0e294387 YZ |
2564 | static void __kick_flushing_caps(struct ceph_mds_client *mdsc, |
2565 | struct ceph_mds_session *session, | |
2566 | struct ceph_inode_info *ci, | |
2567 | u64 oldest_flush_tid) | |
2568 | __releases(ci->i_ceph_lock) | |
2569 | __acquires(ci->i_ceph_lock) | |
553adfd9 | 2570 | { |
874c8ca1 | 2571 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 2572 | struct ceph_client *cl = mdsc->fsc->client; |
553adfd9 YZ |
2573 | struct ceph_cap *cap; |
2574 | struct ceph_cap_flush *cf; | |
0e294387 | 2575 | int ret; |
553adfd9 | 2576 | u64 first_tid = 0; |
49ada6e8 | 2577 | u64 last_snap_flush = 0; |
553adfd9 | 2578 | |
fbed7045 JL |
2579 | /* Don't do anything until create reply comes in */ |
2580 | if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) | |
2581 | return; | |
2582 | ||
054f8d41 YZ |
2583 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
2584 | ||
49ada6e8 | 2585 | list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) { |
b2f9fa1f | 2586 | if (cf->is_capsnap) { |
49ada6e8 YZ |
2587 | last_snap_flush = cf->tid; |
2588 | break; | |
2589 | } | |
2590 | } | |
2591 | ||
e4500b5e YZ |
2592 | list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) { |
2593 | if (cf->tid < first_tid) | |
2594 | continue; | |
2595 | ||
553adfd9 YZ |
2596 | cap = ci->i_auth_cap; |
2597 | if (!(cap && cap->session == session)) { | |
38d46409 XL |
2598 | pr_err_client(cl, "%p auth cap %p not mds%d ???\n", |
2599 | inode, cap, session->s_mds); | |
553adfd9 YZ |
2600 | break; |
2601 | } | |
2602 | ||
553adfd9 YZ |
2603 | first_tid = cf->tid + 1; |
2604 | ||
b2f9fa1f | 2605 | if (!cf->is_capsnap) { |
0a454bdd JL |
2606 | struct cap_msg_args arg; |
2607 | ||
38d46409 XL |
2608 | doutc(cl, "%p %llx.%llx cap %p tid %llu %s\n", |
2609 | inode, ceph_vinop(inode), cap, cf->tid, | |
2610 | ceph_cap_string(cf->caps)); | |
0a454bdd | 2611 | __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, |
49ada6e8 YZ |
2612 | (cf->tid < last_snap_flush ? |
2613 | CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0), | |
2614 | __ceph_caps_used(ci), | |
0e294387 | 2615 | __ceph_caps_wanted(ci), |
49ada6e8 | 2616 | (cap->issued | cap->implemented), |
0e294387 | 2617 | cf->caps, cf->tid, oldest_flush_tid); |
0a454bdd | 2618 | spin_unlock(&ci->i_ceph_lock); |
52311980 | 2619 | __send_cap(&arg, ci); |
0e294387 YZ |
2620 | } else { |
2621 | struct ceph_cap_snap *capsnap = | |
2622 | container_of(cf, struct ceph_cap_snap, | |
2623 | cap_flush); | |
38d46409 XL |
2624 | doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n", |
2625 | inode, ceph_vinop(inode), capsnap, cf->tid, | |
2626 | ceph_cap_string(capsnap->dirty)); | |
0e294387 | 2627 | |
805692d0 | 2628 | refcount_inc(&capsnap->nref); |
0e294387 YZ |
2629 | spin_unlock(&ci->i_ceph_lock); |
2630 | ||
2631 | ret = __send_flush_snap(inode, session, capsnap, cap->mseq, | |
2632 | oldest_flush_tid); | |
2633 | if (ret < 0) { | |
38d46409 XL |
2634 | pr_err_client(cl, "error sending cap flushsnap," |
2635 | " %p %llx.%llx tid %llu follows %llu\n", | |
2636 | inode, ceph_vinop(inode), cf->tid, | |
2637 | capsnap->follows); | |
0e294387 YZ |
2638 | } |
2639 | ||
2640 | ceph_put_cap_snap(capsnap); | |
2641 | } | |
e4500b5e YZ |
2642 | |
2643 | spin_lock(&ci->i_ceph_lock); | |
553adfd9 | 2644 | } |
553adfd9 YZ |
2645 | } |
2646 | ||
e548e9b9 YZ |
2647 | void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc, |
2648 | struct ceph_mds_session *session) | |
2649 | { | |
38d46409 | 2650 | struct ceph_client *cl = mdsc->fsc->client; |
e548e9b9 YZ |
2651 | struct ceph_inode_info *ci; |
2652 | struct ceph_cap *cap; | |
0e294387 | 2653 | u64 oldest_flush_tid; |
e548e9b9 | 2654 | |
38d46409 | 2655 | doutc(cl, "mds%d\n", session->s_mds); |
0e294387 YZ |
2656 | |
2657 | spin_lock(&mdsc->cap_dirty_lock); | |
2658 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); | |
2659 | spin_unlock(&mdsc->cap_dirty_lock); | |
2660 | ||
e548e9b9 | 2661 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
38d46409 XL |
2662 | struct inode *inode = &ci->netfs.inode; |
2663 | ||
e548e9b9 YZ |
2664 | spin_lock(&ci->i_ceph_lock); |
2665 | cap = ci->i_auth_cap; | |
2666 | if (!(cap && cap->session == session)) { | |
38d46409 XL |
2667 | pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n", |
2668 | inode, ceph_vinop(inode), cap, | |
2669 | session->s_mds); | |
e548e9b9 YZ |
2670 | spin_unlock(&ci->i_ceph_lock); |
2671 | continue; | |
2672 | } | |
2673 | ||
2674 | ||
2675 | /* | |
2676 | * if flushing caps were revoked, we re-send the cap flush | |
2677 | * in client reconnect stage. This guarantees MDS * processes | |
2678 | * the cap flush message before issuing the flushing caps to | |
2679 | * other client. | |
2680 | */ | |
2681 | if ((cap->issued & ci->i_flushing_caps) != | |
2682 | ci->i_flushing_caps) { | |
81c5a148 YZ |
2683 | /* encode_caps_cb() also will reset these sequence |
2684 | * numbers. make sure sequence numbers in cap flush | |
2685 | * message match later reconnect message */ | |
2686 | cap->seq = 0; | |
2687 | cap->issue_seq = 0; | |
2688 | cap->mseq = 0; | |
0e294387 YZ |
2689 | __kick_flushing_caps(mdsc, session, ci, |
2690 | oldest_flush_tid); | |
13c2b57d YZ |
2691 | } else { |
2692 | ci->i_ceph_flags |= CEPH_I_KICK_FLUSH; | |
e548e9b9 YZ |
2693 | } |
2694 | ||
e548e9b9 YZ |
2695 | spin_unlock(&ci->i_ceph_lock); |
2696 | } | |
2697 | } | |
2698 | ||
a8599bd8 SW |
2699 | void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, |
2700 | struct ceph_mds_session *session) | |
2701 | { | |
38d46409 | 2702 | struct ceph_client *cl = mdsc->fsc->client; |
a8599bd8 | 2703 | struct ceph_inode_info *ci; |
13c2b57d | 2704 | struct ceph_cap *cap; |
0e294387 | 2705 | u64 oldest_flush_tid; |
a8599bd8 | 2706 | |
829ad4db JL |
2707 | lockdep_assert_held(&session->s_mutex); |
2708 | ||
38d46409 | 2709 | doutc(cl, "mds%d\n", session->s_mds); |
0e294387 YZ |
2710 | |
2711 | spin_lock(&mdsc->cap_dirty_lock); | |
2712 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); | |
2713 | spin_unlock(&mdsc->cap_dirty_lock); | |
2714 | ||
a8599bd8 | 2715 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
38d46409 XL |
2716 | struct inode *inode = &ci->netfs.inode; |
2717 | ||
0e294387 | 2718 | spin_lock(&ci->i_ceph_lock); |
13c2b57d YZ |
2719 | cap = ci->i_auth_cap; |
2720 | if (!(cap && cap->session == session)) { | |
38d46409 XL |
2721 | pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n", |
2722 | inode, ceph_vinop(inode), cap, | |
2723 | session->s_mds); | |
13c2b57d YZ |
2724 | spin_unlock(&ci->i_ceph_lock); |
2725 | continue; | |
2726 | } | |
2727 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { | |
13c2b57d YZ |
2728 | __kick_flushing_caps(mdsc, session, ci, |
2729 | oldest_flush_tid); | |
2730 | } | |
0e294387 | 2731 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
2732 | } |
2733 | } | |
2734 | ||
e8a4d267 JL |
2735 | void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session, |
2736 | struct ceph_inode_info *ci) | |
088b3f5e | 2737 | { |
e8a4d267 JL |
2738 | struct ceph_mds_client *mdsc = session->s_mdsc; |
2739 | struct ceph_cap *cap = ci->i_auth_cap; | |
38d46409 | 2740 | struct inode *inode = &ci->netfs.inode; |
e8a4d267 JL |
2741 | |
2742 | lockdep_assert_held(&ci->i_ceph_lock); | |
088b3f5e | 2743 | |
38d46409 XL |
2744 | doutc(mdsc->fsc->client, "%p %llx.%llx flushing %s\n", |
2745 | inode, ceph_vinop(inode), | |
2746 | ceph_cap_string(ci->i_flushing_caps)); | |
005c4697 | 2747 | |
0e294387 YZ |
2748 | if (!list_empty(&ci->i_cap_flush_list)) { |
2749 | u64 oldest_flush_tid; | |
005c4697 YZ |
2750 | spin_lock(&mdsc->cap_dirty_lock); |
2751 | list_move_tail(&ci->i_flushing_item, | |
2752 | &cap->session->s_cap_flushing); | |
0e294387 | 2753 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
005c4697 YZ |
2754 | spin_unlock(&mdsc->cap_dirty_lock); |
2755 | ||
0e294387 | 2756 | __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid); |
088b3f5e SW |
2757 | } |
2758 | } | |
2759 | ||
a8599bd8 SW |
2760 | |
2761 | /* | |
2762 | * Take references to capabilities we hold, so that we don't release | |
2763 | * them to the MDS prematurely. | |
a8599bd8 | 2764 | */ |
40dcf75e | 2765 | void ceph_take_cap_refs(struct ceph_inode_info *ci, int got, |
5dda377c | 2766 | bool snap_rwsem_locked) |
a8599bd8 | 2767 | { |
38d46409 XL |
2768 | struct inode *inode = &ci->netfs.inode; |
2769 | struct ceph_client *cl = ceph_inode_to_client(inode); | |
2770 | ||
40dcf75e JL |
2771 | lockdep_assert_held(&ci->i_ceph_lock); |
2772 | ||
a8599bd8 SW |
2773 | if (got & CEPH_CAP_PIN) |
2774 | ci->i_pin_ref++; | |
2775 | if (got & CEPH_CAP_FILE_RD) | |
2776 | ci->i_rd_ref++; | |
2777 | if (got & CEPH_CAP_FILE_CACHE) | |
2778 | ci->i_rdcache_ref++; | |
f85122af JL |
2779 | if (got & CEPH_CAP_FILE_EXCL) |
2780 | ci->i_fx_ref++; | |
5dda377c YZ |
2781 | if (got & CEPH_CAP_FILE_WR) { |
2782 | if (ci->i_wr_ref == 0 && !ci->i_head_snapc) { | |
2783 | BUG_ON(!snap_rwsem_locked); | |
2784 | ci->i_head_snapc = ceph_get_snap_context( | |
2785 | ci->i_snap_realm->cached_context); | |
2786 | } | |
a8599bd8 | 2787 | ci->i_wr_ref++; |
5dda377c | 2788 | } |
a8599bd8 | 2789 | if (got & CEPH_CAP_FILE_BUFFER) { |
d3d0720d | 2790 | if (ci->i_wb_ref == 0) |
38d46409 | 2791 | ihold(inode); |
d3d0720d | 2792 | ci->i_wb_ref++; |
38d46409 XL |
2793 | doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode, |
2794 | ceph_vinop(inode), ci->i_wb_ref-1, ci->i_wb_ref); | |
a8599bd8 SW |
2795 | } |
2796 | } | |
2797 | ||
2798 | /* | |
2799 | * Try to grab cap references. Specify those refs we @want, and the | |
2800 | * minimal set we @need. Also include the larger offset we are writing | |
2801 | * to (when applicable), and check against max_size here as well. | |
2802 | * Note that caller is responsible for ensuring max_size increases are | |
2803 | * requested from the MDS. | |
1199d7da | 2804 | * |
546d4020 YZ |
2805 | * Returns 0 if caps were not able to be acquired (yet), 1 if succeed, |
2806 | * or a negative error code. There are 3 speical error codes: | |
8006daff JL |
2807 | * -EAGAIN: need to sleep but non-blocking is specified |
2808 | * -EFBIG: ask caller to call check_max_size() and try again. | |
2809 | * -EUCLEAN: ask caller to call ceph_renew_caps() and try again. | |
a8599bd8 | 2810 | */ |
ff5d913d | 2811 | enum { |
719a2514 YZ |
2812 | /* first 8 bits are reserved for CEPH_FILE_MODE_FOO */ |
2813 | NON_BLOCKING = (1 << 8), | |
2814 | CHECK_FILELOCK = (1 << 9), | |
ff5d913d YZ |
2815 | }; |
2816 | ||
5e3ded1b | 2817 | static int try_get_cap_refs(struct inode *inode, int need, int want, |
ff5d913d | 2818 | loff_t endoff, int flags, int *got) |
a8599bd8 | 2819 | { |
5e3ded1b | 2820 | struct ceph_inode_info *ci = ceph_inode(inode); |
5995d90d | 2821 | struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; |
38d46409 | 2822 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 | 2823 | int ret = 0; |
c4d4a582 | 2824 | int have, implemented; |
5dda377c | 2825 | bool snap_rwsem_locked = false; |
a8599bd8 | 2826 | |
38d46409 XL |
2827 | doutc(cl, "%p %llx.%llx need %s want %s\n", inode, |
2828 | ceph_vinop(inode), ceph_cap_string(need), | |
2829 | ceph_cap_string(want)); | |
c4d4a582 | 2830 | |
5dda377c | 2831 | again: |
be655596 | 2832 | spin_lock(&ci->i_ceph_lock); |
a8599bd8 | 2833 | |
ff5d913d YZ |
2834 | if ((flags & CHECK_FILELOCK) && |
2835 | (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK)) { | |
38d46409 XL |
2836 | doutc(cl, "%p %llx.%llx error filelock\n", inode, |
2837 | ceph_vinop(inode)); | |
ff5d913d YZ |
2838 | ret = -EIO; |
2839 | goto out_unlock; | |
2840 | } | |
2841 | ||
37505d57 YZ |
2842 | /* finish pending truncate */ |
2843 | while (ci->i_truncate_pending) { | |
2844 | spin_unlock(&ci->i_ceph_lock); | |
5dda377c YZ |
2845 | if (snap_rwsem_locked) { |
2846 | up_read(&mdsc->snap_rwsem); | |
2847 | snap_rwsem_locked = false; | |
2848 | } | |
b415bf4f | 2849 | __ceph_do_pending_vmtruncate(inode); |
37505d57 YZ |
2850 | spin_lock(&ci->i_ceph_lock); |
2851 | } | |
2852 | ||
3871cbb9 YZ |
2853 | have = __ceph_caps_issued(ci, &implemented); |
2854 | ||
2855 | if (have & need & CEPH_CAP_FILE_WR) { | |
a8599bd8 | 2856 | if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { |
38d46409 XL |
2857 | doutc(cl, "%p %llx.%llx endoff %llu > maxsize %llu\n", |
2858 | inode, ceph_vinop(inode), endoff, ci->i_max_size); | |
1199d7da | 2859 | if (endoff > ci->i_requested_max_size) |
8006daff | 2860 | ret = ci->i_auth_cap ? -EFBIG : -EUCLEAN; |
3738daa6 | 2861 | goto out_unlock; |
a8599bd8 SW |
2862 | } |
2863 | /* | |
2864 | * If a sync write is in progress, we must wait, so that we | |
2865 | * can get a final snapshot value for size+mtime. | |
2866 | */ | |
2867 | if (__ceph_have_pending_cap_snap(ci)) { | |
38d46409 XL |
2868 | doutc(cl, "%p %llx.%llx cap_snap_pending\n", inode, |
2869 | ceph_vinop(inode)); | |
3738daa6 | 2870 | goto out_unlock; |
a8599bd8 SW |
2871 | } |
2872 | } | |
a8599bd8 | 2873 | |
a8599bd8 SW |
2874 | if ((have & need) == need) { |
2875 | /* | |
2876 | * Look at (implemented & ~have & not) so that we keep waiting | |
2877 | * on transition from wanted -> needed caps. This is needed | |
2878 | * for WRBUFFER|WR -> WR to avoid a new WR sync write from | |
2879 | * going before a prior buffered writeback happens. | |
7c3ea987 XL |
2880 | * |
2881 | * For RDCACHE|RD -> RD, there is not need to wait and we can | |
2882 | * just exclude the revoking caps and force to sync read. | |
a8599bd8 SW |
2883 | */ |
2884 | int not = want & ~(have & need); | |
2885 | int revoking = implemented & ~have; | |
7c3ea987 | 2886 | int exclude = revoking & not; |
38d46409 XL |
2887 | doutc(cl, "%p %llx.%llx have %s but not %s (revoking %s)\n", |
2888 | inode, ceph_vinop(inode), ceph_cap_string(have), | |
2889 | ceph_cap_string(not), ceph_cap_string(revoking)); | |
7c3ea987 | 2890 | if (!exclude || !(exclude & CEPH_CAP_FILE_BUFFER)) { |
5dda377c YZ |
2891 | if (!snap_rwsem_locked && |
2892 | !ci->i_head_snapc && | |
2893 | (need & CEPH_CAP_FILE_WR)) { | |
2894 | if (!down_read_trylock(&mdsc->snap_rwsem)) { | |
2895 | /* | |
2896 | * we can not call down_read() when | |
2897 | * task isn't in TASK_RUNNING state | |
2898 | */ | |
ff5d913d | 2899 | if (flags & NON_BLOCKING) { |
1199d7da | 2900 | ret = -EAGAIN; |
5dda377c YZ |
2901 | goto out_unlock; |
2902 | } | |
2903 | ||
2904 | spin_unlock(&ci->i_ceph_lock); | |
2905 | down_read(&mdsc->snap_rwsem); | |
2906 | snap_rwsem_locked = true; | |
2907 | goto again; | |
2908 | } | |
2909 | snap_rwsem_locked = true; | |
2910 | } | |
173e70e8 | 2911 | if ((have & want) == want) |
7c3ea987 | 2912 | *got = need | (want & ~exclude); |
173e70e8 YZ |
2913 | else |
2914 | *got = need; | |
40dcf75e | 2915 | ceph_take_cap_refs(ci, *got, true); |
a8599bd8 SW |
2916 | ret = 1; |
2917 | } | |
2918 | } else { | |
03f4fcb0 | 2919 | int session_readonly = false; |
c0e385b1 | 2920 | int mds_wanted; |
525d15e8 YZ |
2921 | if (ci->i_auth_cap && |
2922 | (need & (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_EXCL))) { | |
03f4fcb0 YZ |
2923 | struct ceph_mds_session *s = ci->i_auth_cap->session; |
2924 | spin_lock(&s->s_cap_lock); | |
2925 | session_readonly = s->s_readonly; | |
2926 | spin_unlock(&s->s_cap_lock); | |
2927 | } | |
2928 | if (session_readonly) { | |
38d46409 XL |
2929 | doutc(cl, "%p %llx.%llx need %s but mds%d readonly\n", |
2930 | inode, ceph_vinop(inode), ceph_cap_string(need), | |
2931 | ci->i_auth_cap->mds); | |
1199d7da | 2932 | ret = -EROFS; |
03f4fcb0 YZ |
2933 | goto out_unlock; |
2934 | } | |
2935 | ||
5d6451b1 | 2936 | if (ceph_inode_is_shutdown(inode)) { |
38d46409 XL |
2937 | doutc(cl, "%p %llx.%llx inode is shutdown\n", |
2938 | inode, ceph_vinop(inode)); | |
5d6451b1 | 2939 | ret = -ESTALE; |
c0e385b1 YZ |
2940 | goto out_unlock; |
2941 | } | |
2942 | mds_wanted = __ceph_caps_mds_wanted(ci, false); | |
2943 | if (need & ~mds_wanted) { | |
38d46409 XL |
2944 | doutc(cl, "%p %llx.%llx need %s > mds_wanted %s\n", |
2945 | inode, ceph_vinop(inode), ceph_cap_string(need), | |
2946 | ceph_cap_string(mds_wanted)); | |
8006daff | 2947 | ret = -EUCLEAN; |
c0e385b1 | 2948 | goto out_unlock; |
48fec5d0 YZ |
2949 | } |
2950 | ||
38d46409 XL |
2951 | doutc(cl, "%p %llx.%llx have %s need %s\n", inode, |
2952 | ceph_vinop(inode), ceph_cap_string(have), | |
2953 | ceph_cap_string(need)); | |
a8599bd8 | 2954 | } |
3738daa6 | 2955 | out_unlock: |
719a2514 YZ |
2956 | |
2957 | __ceph_touch_fmode(ci, mdsc, flags); | |
2958 | ||
be655596 | 2959 | spin_unlock(&ci->i_ceph_lock); |
5dda377c YZ |
2960 | if (snap_rwsem_locked) |
2961 | up_read(&mdsc->snap_rwsem); | |
3738daa6 | 2962 | |
1af16d54 XL |
2963 | if (!ret) |
2964 | ceph_update_cap_mis(&mdsc->metric); | |
2965 | else if (ret == 1) | |
2966 | ceph_update_cap_hit(&mdsc->metric); | |
2967 | ||
38d46409 XL |
2968 | doutc(cl, "%p %llx.%llx ret %d got %s\n", inode, |
2969 | ceph_vinop(inode), ret, ceph_cap_string(*got)); | |
a8599bd8 SW |
2970 | return ret; |
2971 | } | |
2972 | ||
2973 | /* | |
2974 | * Check the offset we are writing up to against our current | |
2975 | * max_size. If necessary, tell the MDS we want to write to | |
2976 | * a larger offset. | |
2977 | */ | |
2978 | static void check_max_size(struct inode *inode, loff_t endoff) | |
2979 | { | |
2980 | struct ceph_inode_info *ci = ceph_inode(inode); | |
38d46409 | 2981 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 SW |
2982 | int check = 0; |
2983 | ||
2984 | /* do we need to explicitly request a larger max_size? */ | |
be655596 | 2985 | spin_lock(&ci->i_ceph_lock); |
3871cbb9 | 2986 | if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) { |
38d46409 XL |
2987 | doutc(cl, "write %p %llx.%llx at large endoff %llu, req max_size\n", |
2988 | inode, ceph_vinop(inode), endoff); | |
a8599bd8 | 2989 | ci->i_wanted_max_size = endoff; |
a8599bd8 | 2990 | } |
3871cbb9 YZ |
2991 | /* duplicate ceph_check_caps()'s logic */ |
2992 | if (ci->i_auth_cap && | |
2993 | (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) && | |
2994 | ci->i_wanted_max_size > ci->i_max_size && | |
2995 | ci->i_wanted_max_size > ci->i_requested_max_size) | |
2996 | check = 1; | |
be655596 | 2997 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 | 2998 | if (check) |
e4b731cc | 2999 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY); |
a8599bd8 SW |
3000 | } |
3001 | ||
719a2514 YZ |
3002 | static inline int get_used_fmode(int caps) |
3003 | { | |
3004 | int fmode = 0; | |
3005 | if (caps & CEPH_CAP_FILE_RD) | |
3006 | fmode |= CEPH_FILE_MODE_RD; | |
3007 | if (caps & CEPH_CAP_FILE_WR) | |
3008 | fmode |= CEPH_FILE_MODE_WR; | |
3009 | return fmode; | |
3010 | } | |
3011 | ||
5e3ded1b | 3012 | int ceph_try_get_caps(struct inode *inode, int need, int want, |
2ee9dd95 | 3013 | bool nonblock, int *got) |
2b1ac852 | 3014 | { |
719a2514 | 3015 | int ret, flags; |
2b1ac852 YZ |
3016 | |
3017 | BUG_ON(need & ~CEPH_CAP_FILE_RD); | |
a25949b9 JL |
3018 | BUG_ON(want & ~(CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO | |
3019 | CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL | | |
3020 | CEPH_CAP_ANY_DIR_OPS)); | |
3021 | if (need) { | |
3022 | ret = ceph_pool_perm_check(inode, need); | |
3023 | if (ret < 0) | |
3024 | return ret; | |
3025 | } | |
2b1ac852 | 3026 | |
719a2514 YZ |
3027 | flags = get_used_fmode(need | want); |
3028 | if (nonblock) | |
3029 | flags |= NON_BLOCKING; | |
3030 | ||
3031 | ret = try_get_cap_refs(inode, need, want, 0, flags, got); | |
546d4020 | 3032 | /* three special error codes */ |
8006daff | 3033 | if (ret == -EAGAIN || ret == -EFBIG || ret == -EUCLEAN) |
546d4020 YZ |
3034 | ret = 0; |
3035 | return ret; | |
2b1ac852 YZ |
3036 | } |
3037 | ||
a8599bd8 SW |
3038 | /* |
3039 | * Wait for caps, and take cap references. If we can't get a WR cap | |
3040 | * due to a small max_size, make sure we check_max_size (and possibly | |
3041 | * ask the mds) so we don't get hung up indefinitely. | |
3042 | */ | |
5c64737d XL |
3043 | int __ceph_get_caps(struct inode *inode, struct ceph_file_info *fi, int need, |
3044 | int want, loff_t endoff, int *got) | |
a8599bd8 | 3045 | { |
5e3ded1b | 3046 | struct ceph_inode_info *ci = ceph_inode(inode); |
5995d90d | 3047 | struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); |
ff5d913d | 3048 | int ret, _got, flags; |
a8599bd8 | 3049 | |
5e3ded1b | 3050 | ret = ceph_pool_perm_check(inode, need); |
10183a69 YZ |
3051 | if (ret < 0) |
3052 | return ret; | |
3053 | ||
5c64737d | 3054 | if (fi && (fi->fmode & CEPH_FILE_MODE_WR) && |
81f148a9 YZ |
3055 | fi->filp_gen != READ_ONCE(fsc->filp_gen)) |
3056 | return -EBADF; | |
3057 | ||
719a2514 YZ |
3058 | flags = get_used_fmode(need | want); |
3059 | ||
5dda377c | 3060 | while (true) { |
719a2514 | 3061 | flags &= CEPH_FILE_MODE_MASK; |
461ab10e | 3062 | if (vfs_inode_has_locks(inode)) |
719a2514 | 3063 | flags |= CHECK_FILELOCK; |
5dda377c | 3064 | _got = 0; |
5e3ded1b | 3065 | ret = try_get_cap_refs(inode, need, want, endoff, |
ff5d913d | 3066 | flags, &_got); |
546d4020 | 3067 | WARN_ON_ONCE(ret == -EAGAIN); |
7b2f936f | 3068 | if (!ret) { |
3a3430af JL |
3069 | struct ceph_mds_client *mdsc = fsc->mdsc; |
3070 | struct cap_wait cw; | |
5c341ee3 | 3071 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
3a3430af | 3072 | |
ebce3eb2 | 3073 | cw.ino = ceph_ino(inode); |
3a3430af JL |
3074 | cw.tgid = current->tgid; |
3075 | cw.need = need; | |
3076 | cw.want = want; | |
3077 | ||
3078 | spin_lock(&mdsc->caps_list_lock); | |
3079 | list_add(&cw.list, &mdsc->cap_wait_list); | |
3080 | spin_unlock(&mdsc->caps_list_lock); | |
3081 | ||
719a2514 YZ |
3082 | /* make sure used fmode not timeout */ |
3083 | ceph_get_fmode(ci, flags, FMODE_WAIT_BIAS); | |
5c341ee3 NB |
3084 | add_wait_queue(&ci->i_cap_wq, &wait); |
3085 | ||
ff5d913d | 3086 | flags |= NON_BLOCKING; |
5e3ded1b | 3087 | while (!(ret = try_get_cap_refs(inode, need, want, |
ff5d913d | 3088 | endoff, flags, &_got))) { |
6e09d0fb YZ |
3089 | if (signal_pending(current)) { |
3090 | ret = -ERESTARTSYS; | |
3091 | break; | |
3092 | } | |
5c341ee3 | 3093 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
6e09d0fb | 3094 | } |
5c341ee3 NB |
3095 | |
3096 | remove_wait_queue(&ci->i_cap_wq, &wait); | |
719a2514 | 3097 | ceph_put_fmode(ci, flags, FMODE_WAIT_BIAS); |
3a3430af JL |
3098 | |
3099 | spin_lock(&mdsc->caps_list_lock); | |
3100 | list_del(&cw.list); | |
3101 | spin_unlock(&mdsc->caps_list_lock); | |
3102 | ||
7b2f936f | 3103 | if (ret == -EAGAIN) |
5dda377c | 3104 | continue; |
77310320 | 3105 | } |
81f148a9 | 3106 | |
5c64737d | 3107 | if (fi && (fi->fmode & CEPH_FILE_MODE_WR) && |
81f148a9 YZ |
3108 | fi->filp_gen != READ_ONCE(fsc->filp_gen)) { |
3109 | if (ret >= 0 && _got) | |
3110 | ceph_put_cap_refs(ci, _got); | |
3111 | return -EBADF; | |
3112 | } | |
3113 | ||
7b2f936f | 3114 | if (ret < 0) { |
8006daff | 3115 | if (ret == -EFBIG || ret == -EUCLEAN) { |
9bccb765 YZ |
3116 | int ret2 = ceph_wait_on_async_create(inode); |
3117 | if (ret2 < 0) | |
3118 | return ret2; | |
3119 | } | |
546d4020 YZ |
3120 | if (ret == -EFBIG) { |
3121 | check_max_size(inode, endoff); | |
3122 | continue; | |
3123 | } | |
8006daff | 3124 | if (ret == -EUCLEAN) { |
7b2f936f | 3125 | /* session was killed, try renew caps */ |
719a2514 | 3126 | ret = ceph_renew_caps(inode, flags); |
7b2f936f YZ |
3127 | if (ret == 0) |
3128 | continue; | |
3129 | } | |
77310320 | 3130 | return ret; |
5dda377c | 3131 | } |
c4d4a582 | 3132 | |
874c8ca1 | 3133 | if (S_ISREG(ci->netfs.inode.i_mode) && |
48490776 | 3134 | ceph_has_inline_data(ci) && |
5dda377c | 3135 | (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
5e3ded1b | 3136 | i_size_read(inode) > 0) { |
5dda377c | 3137 | struct page *page = |
5e3ded1b | 3138 | find_get_page(inode->i_mapping, 0); |
5dda377c | 3139 | if (page) { |
e72968e1 JL |
3140 | bool uptodate = PageUptodate(page); |
3141 | ||
09cbfeaf | 3142 | put_page(page); |
e72968e1 JL |
3143 | if (uptodate) |
3144 | break; | |
c4d4a582 | 3145 | } |
5dda377c YZ |
3146 | /* |
3147 | * drop cap refs first because getattr while | |
3148 | * holding * caps refs can cause deadlock. | |
3149 | */ | |
3150 | ceph_put_cap_refs(ci, _got); | |
3151 | _got = 0; | |
c4d4a582 | 3152 | |
5dda377c YZ |
3153 | /* |
3154 | * getattr request will bring inline data into | |
3155 | * page cache | |
3156 | */ | |
5e3ded1b | 3157 | ret = __ceph_do_getattr(inode, NULL, |
5dda377c YZ |
3158 | CEPH_STAT_CAP_INLINE_DATA, |
3159 | true); | |
3160 | if (ret < 0) | |
3161 | return ret; | |
3162 | continue; | |
3163 | } | |
3164 | break; | |
c4d4a582 | 3165 | } |
c4d4a582 YZ |
3166 | *got = _got; |
3167 | return 0; | |
a8599bd8 SW |
3168 | } |
3169 | ||
5c64737d XL |
3170 | int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff, |
3171 | int *got) | |
3172 | { | |
3173 | struct ceph_file_info *fi = filp->private_data; | |
3174 | struct inode *inode = file_inode(filp); | |
3175 | ||
3176 | return __ceph_get_caps(inode, fi, need, want, endoff, got); | |
3177 | } | |
3178 | ||
a8599bd8 SW |
3179 | /* |
3180 | * Take cap refs. Caller must already know we hold at least one ref | |
3181 | * on the caps in question or we don't know this is safe. | |
3182 | */ | |
3183 | void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) | |
3184 | { | |
be655596 | 3185 | spin_lock(&ci->i_ceph_lock); |
40dcf75e | 3186 | ceph_take_cap_refs(ci, caps, false); |
be655596 | 3187 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
3188 | } |
3189 | ||
86056090 YZ |
3190 | |
3191 | /* | |
3192 | * drop cap_snap that is not associated with any snapshot. | |
3193 | * we don't need to send FLUSHSNAP message for it. | |
3194 | */ | |
70220ac8 YZ |
3195 | static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci, |
3196 | struct ceph_cap_snap *capsnap) | |
86056090 | 3197 | { |
38d46409 XL |
3198 | struct inode *inode = &ci->netfs.inode; |
3199 | struct ceph_client *cl = ceph_inode_to_client(inode); | |
3200 | ||
86056090 YZ |
3201 | if (!capsnap->need_flush && |
3202 | !capsnap->writing && !capsnap->dirty_pages) { | |
38d46409 | 3203 | doutc(cl, "%p follows %llu\n", capsnap, capsnap->follows); |
0e294387 | 3204 | BUG_ON(capsnap->cap_flush.tid > 0); |
86056090 | 3205 | ceph_put_snap_context(capsnap->context); |
70220ac8 YZ |
3206 | if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps)) |
3207 | ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS; | |
3208 | ||
86056090 | 3209 | list_del(&capsnap->ci_item); |
86056090 YZ |
3210 | ceph_put_cap_snap(capsnap); |
3211 | return 1; | |
3212 | } | |
3213 | return 0; | |
3214 | } | |
3215 | ||
a8810cdc JL |
3216 | enum put_cap_refs_mode { |
3217 | PUT_CAP_REFS_SYNC = 0, | |
3218 | PUT_CAP_REFS_NO_CHECK, | |
3219 | PUT_CAP_REFS_ASYNC, | |
3220 | }; | |
3221 | ||
a8599bd8 SW |
3222 | /* |
3223 | * Release cap refs. | |
3224 | * | |
3225 | * If we released the last ref on any given cap, call ceph_check_caps | |
3226 | * to release (or schedule a release). | |
3227 | * | |
3228 | * If we are releasing a WR cap (from a sync write), finalize any affected | |
3229 | * cap_snap, and wake up any waiters. | |
3230 | */ | |
e64f44a8 | 3231 | static void __ceph_put_cap_refs(struct ceph_inode_info *ci, int had, |
a8810cdc | 3232 | enum put_cap_refs_mode mode) |
a8599bd8 | 3233 | { |
874c8ca1 | 3234 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 3235 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 | 3236 | int last = 0, put = 0, flushsnaps = 0, wake = 0; |
558b4510 | 3237 | bool check_flushsnaps = false; |
a8599bd8 | 3238 | |
be655596 | 3239 | spin_lock(&ci->i_ceph_lock); |
a8599bd8 SW |
3240 | if (had & CEPH_CAP_PIN) |
3241 | --ci->i_pin_ref; | |
3242 | if (had & CEPH_CAP_FILE_RD) | |
3243 | if (--ci->i_rd_ref == 0) | |
3244 | last++; | |
3245 | if (had & CEPH_CAP_FILE_CACHE) | |
3246 | if (--ci->i_rdcache_ref == 0) | |
3247 | last++; | |
f85122af JL |
3248 | if (had & CEPH_CAP_FILE_EXCL) |
3249 | if (--ci->i_fx_ref == 0) | |
3250 | last++; | |
a8599bd8 | 3251 | if (had & CEPH_CAP_FILE_BUFFER) { |
d3d0720d | 3252 | if (--ci->i_wb_ref == 0) { |
a8599bd8 | 3253 | last++; |
558b4510 | 3254 | /* put the ref held by ceph_take_cap_refs() */ |
a8599bd8 | 3255 | put++; |
558b4510 | 3256 | check_flushsnaps = true; |
a8599bd8 | 3257 | } |
38d46409 XL |
3258 | doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode, |
3259 | ceph_vinop(inode), ci->i_wb_ref+1, ci->i_wb_ref); | |
a8599bd8 | 3260 | } |
558b4510 | 3261 | if (had & CEPH_CAP_FILE_WR) { |
a8599bd8 | 3262 | if (--ci->i_wr_ref == 0) { |
2d12ad95 XL |
3263 | /* |
3264 | * The Fb caps will always be took and released | |
3265 | * together with the Fw caps. | |
3266 | */ | |
3267 | WARN_ON_ONCE(ci->i_wb_ref); | |
3268 | ||
a8599bd8 | 3269 | last++; |
558b4510 | 3270 | check_flushsnaps = true; |
5dda377c YZ |
3271 | if (ci->i_wrbuffer_ref_head == 0 && |
3272 | ci->i_dirty_caps == 0 && | |
3273 | ci->i_flushing_caps == 0) { | |
3274 | BUG_ON(!ci->i_head_snapc); | |
3275 | ceph_put_snap_context(ci->i_head_snapc); | |
3276 | ci->i_head_snapc = NULL; | |
3277 | } | |
db40cc17 | 3278 | /* see comment in __ceph_remove_cap() */ |
bd84fbcb | 3279 | if (!__ceph_is_any_real_caps(ci) && ci->i_snap_realm) |
0ba92e1c | 3280 | ceph_change_snap_realm(inode, NULL); |
a8599bd8 | 3281 | } |
558b4510 XL |
3282 | } |
3283 | if (check_flushsnaps && __ceph_have_pending_cap_snap(ci)) { | |
3284 | struct ceph_cap_snap *capsnap = | |
3285 | list_last_entry(&ci->i_cap_snaps, | |
3286 | struct ceph_cap_snap, | |
3287 | ci_item); | |
3288 | ||
3289 | capsnap->writing = 0; | |
3290 | if (ceph_try_drop_cap_snap(ci, capsnap)) | |
3291 | /* put the ref held by ceph_queue_cap_snap() */ | |
3292 | put++; | |
3293 | else if (__ceph_finish_cap_snap(ci, capsnap)) | |
3294 | flushsnaps = 1; | |
3295 | wake = 1; | |
3296 | } | |
be655596 | 3297 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 | 3298 | |
38d46409 XL |
3299 | doutc(cl, "%p %llx.%llx had %s%s%s\n", inode, ceph_vinop(inode), |
3300 | ceph_cap_string(had), last ? " last" : "", put ? " put" : ""); | |
a8599bd8 | 3301 | |
a8810cdc JL |
3302 | switch (mode) { |
3303 | case PUT_CAP_REFS_SYNC: | |
64f36da5 | 3304 | if (last) |
e4b731cc | 3305 | ceph_check_caps(ci, 0); |
64f36da5 JL |
3306 | else if (flushsnaps) |
3307 | ceph_flush_snaps(ci, NULL); | |
a8810cdc JL |
3308 | break; |
3309 | case PUT_CAP_REFS_ASYNC: | |
3310 | if (last) | |
3311 | ceph_queue_check_caps(inode); | |
3312 | else if (flushsnaps) | |
3313 | ceph_queue_flush_snaps(inode); | |
3314 | break; | |
3315 | default: | |
3316 | break; | |
64f36da5 | 3317 | } |
a8599bd8 | 3318 | if (wake) |
03066f23 | 3319 | wake_up_all(&ci->i_cap_wq); |
86056090 | 3320 | while (put-- > 0) |
a8599bd8 SW |
3321 | iput(inode); |
3322 | } | |
3323 | ||
e64f44a8 XL |
3324 | void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) |
3325 | { | |
a8810cdc JL |
3326 | __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_SYNC); |
3327 | } | |
3328 | ||
3329 | void ceph_put_cap_refs_async(struct ceph_inode_info *ci, int had) | |
3330 | { | |
3331 | __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_ASYNC); | |
e64f44a8 XL |
3332 | } |
3333 | ||
3334 | void ceph_put_cap_refs_no_check_caps(struct ceph_inode_info *ci, int had) | |
3335 | { | |
a8810cdc | 3336 | __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_NO_CHECK); |
e64f44a8 XL |
3337 | } |
3338 | ||
a8599bd8 SW |
3339 | /* |
3340 | * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap | |
3341 | * context. Adjust per-snap dirty page accounting as appropriate. | |
3342 | * Once all dirty data for a cap_snap is flushed, flush snapped file | |
3343 | * metadata back to the MDS. If we dropped the last ref, call | |
3344 | * ceph_check_caps. | |
3345 | */ | |
3346 | void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, | |
3347 | struct ceph_snap_context *snapc) | |
3348 | { | |
874c8ca1 | 3349 | struct inode *inode = &ci->netfs.inode; |
38d46409 | 3350 | struct ceph_client *cl = ceph_inode_to_client(inode); |
3ffa9d6f | 3351 | struct ceph_cap_snap *capsnap = NULL, *iter; |
70220ac8 YZ |
3352 | int put = 0; |
3353 | bool last = false; | |
70220ac8 YZ |
3354 | bool flush_snaps = false; |
3355 | bool complete_capsnap = false; | |
a8599bd8 | 3356 | |
be655596 | 3357 | spin_lock(&ci->i_ceph_lock); |
a8599bd8 | 3358 | ci->i_wrbuffer_ref -= nr; |
70220ac8 YZ |
3359 | if (ci->i_wrbuffer_ref == 0) { |
3360 | last = true; | |
3361 | put++; | |
3362 | } | |
a8599bd8 SW |
3363 | |
3364 | if (ci->i_head_snapc == snapc) { | |
3365 | ci->i_wrbuffer_ref_head -= nr; | |
7d8cb26d | 3366 | if (ci->i_wrbuffer_ref_head == 0 && |
5dda377c YZ |
3367 | ci->i_wr_ref == 0 && |
3368 | ci->i_dirty_caps == 0 && | |
3369 | ci->i_flushing_caps == 0) { | |
7d8cb26d | 3370 | BUG_ON(!ci->i_head_snapc); |
a8599bd8 SW |
3371 | ceph_put_snap_context(ci->i_head_snapc); |
3372 | ci->i_head_snapc = NULL; | |
3373 | } | |
38d46409 XL |
3374 | doutc(cl, "on %p %llx.%llx head %d/%d -> %d/%d %s\n", |
3375 | inode, ceph_vinop(inode), ci->i_wrbuffer_ref+nr, | |
3376 | ci->i_wrbuffer_ref_head+nr, ci->i_wrbuffer_ref, | |
3377 | ci->i_wrbuffer_ref_head, last ? " LAST" : ""); | |
a8599bd8 | 3378 | } else { |
3ffa9d6f JK |
3379 | list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) { |
3380 | if (iter->context == snapc) { | |
3381 | capsnap = iter; | |
a8599bd8 SW |
3382 | break; |
3383 | } | |
3384 | } | |
a6d37ccd | 3385 | |
3ffa9d6f | 3386 | if (!capsnap) { |
a6d37ccd XL |
3387 | /* |
3388 | * The capsnap should already be removed when removing | |
3389 | * auth cap in the case of a forced unmount. | |
3390 | */ | |
3391 | WARN_ON_ONCE(ci->i_auth_cap); | |
3392 | goto unlock; | |
3393 | } | |
3394 | ||
819ccbfa SW |
3395 | capsnap->dirty_pages -= nr; |
3396 | if (capsnap->dirty_pages == 0) { | |
70220ac8 YZ |
3397 | complete_capsnap = true; |
3398 | if (!capsnap->writing) { | |
3399 | if (ceph_try_drop_cap_snap(ci, capsnap)) { | |
3400 | put++; | |
3401 | } else { | |
3402 | ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS; | |
3403 | flush_snaps = true; | |
3404 | } | |
3405 | } | |
819ccbfa | 3406 | } |
38d46409 XL |
3407 | doutc(cl, "%p %llx.%llx cap_snap %p snap %lld %d/%d -> %d/%d %s%s\n", |
3408 | inode, ceph_vinop(inode), capsnap, capsnap->context->seq, | |
3409 | ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, | |
3410 | ci->i_wrbuffer_ref, capsnap->dirty_pages, | |
3411 | last ? " (wrbuffer last)" : "", | |
3412 | complete_capsnap ? " (complete capsnap)" : ""); | |
a8599bd8 SW |
3413 | } |
3414 | ||
a6d37ccd | 3415 | unlock: |
be655596 | 3416 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
3417 | |
3418 | if (last) { | |
e4b731cc | 3419 | ceph_check_caps(ci, 0); |
70220ac8 | 3420 | } else if (flush_snaps) { |
ed9b430c | 3421 | ceph_flush_snaps(ci, NULL); |
a8599bd8 | 3422 | } |
70220ac8 YZ |
3423 | if (complete_capsnap) |
3424 | wake_up_all(&ci->i_cap_wq); | |
3e1d0452 | 3425 | while (put-- > 0) { |
23c2c76e | 3426 | iput(inode); |
3e1d0452 | 3427 | } |
a8599bd8 SW |
3428 | } |
3429 | ||
ca20c991 YZ |
3430 | /* |
3431 | * Invalidate unlinked inode's aliases, so we can drop the inode ASAP. | |
3432 | */ | |
3433 | static void invalidate_aliases(struct inode *inode) | |
3434 | { | |
38d46409 | 3435 | struct ceph_client *cl = ceph_inode_to_client(inode); |
ca20c991 YZ |
3436 | struct dentry *dn, *prev = NULL; |
3437 | ||
38d46409 | 3438 | doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode)); |
ca20c991 YZ |
3439 | d_prune_aliases(inode); |
3440 | /* | |
3441 | * For non-directory inode, d_find_alias() only returns | |
fc12c80a BF |
3442 | * hashed dentry. After calling d_invalidate(), the |
3443 | * dentry becomes unhashed. | |
ca20c991 | 3444 | * |
a8d436f0 | 3445 | * For directory inode, d_find_alias() can return |
fc12c80a | 3446 | * unhashed dentry. But directory inode should have |
ca20c991 YZ |
3447 | * one alias at most. |
3448 | */ | |
3449 | while ((dn = d_find_alias(inode))) { | |
3450 | if (dn == prev) { | |
3451 | dput(dn); | |
3452 | break; | |
3453 | } | |
a8d436f0 | 3454 | d_invalidate(dn); |
ca20c991 YZ |
3455 | if (prev) |
3456 | dput(prev); | |
3457 | prev = dn; | |
3458 | } | |
3459 | if (prev) | |
3460 | dput(prev); | |
3461 | } | |
3462 | ||
a1c6b835 YZ |
3463 | struct cap_extra_info { |
3464 | struct ceph_string *pool_ns; | |
3465 | /* inline data */ | |
3466 | u64 inline_version; | |
3467 | void *inline_data; | |
3468 | u32 inline_len; | |
4985d6f9 YZ |
3469 | /* dirstat */ |
3470 | bool dirstat_valid; | |
3471 | u64 nfiles; | |
3472 | u64 nsubdirs; | |
176c77c9 | 3473 | u64 change_attr; |
a1c6b835 YZ |
3474 | /* currently issued */ |
3475 | int issued; | |
ec62b894 | 3476 | struct timespec64 btime; |
0d91f0ad JL |
3477 | u8 *fscrypt_auth; |
3478 | u32 fscrypt_auth_len; | |
3479 | u64 fscrypt_file_size; | |
a1c6b835 YZ |
3480 | }; |
3481 | ||
a8599bd8 SW |
3482 | /* |
3483 | * Handle a cap GRANT message from the MDS. (Note that a GRANT may | |
3484 | * actually be a revocation if it specifies a smaller cap set.) | |
3485 | * | |
be655596 | 3486 | * caller holds s_mutex and i_ceph_lock, we drop both. |
a8599bd8 | 3487 | */ |
a1c6b835 | 3488 | static void handle_cap_grant(struct inode *inode, |
15637c8b | 3489 | struct ceph_mds_session *session, |
a1c6b835 YZ |
3490 | struct ceph_cap *cap, |
3491 | struct ceph_mds_caps *grant, | |
3492 | struct ceph_buffer *xattr_buf, | |
3493 | struct cap_extra_info *extra_info) | |
2cd698be | 3494 | __releases(ci->i_ceph_lock) |
a1c6b835 | 3495 | __releases(session->s_mdsc->snap_rwsem) |
a8599bd8 | 3496 | { |
38d46409 | 3497 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 | 3498 | struct ceph_inode_info *ci = ceph_inode(inode); |
2f56f56a | 3499 | int seq = le32_to_cpu(grant->seq); |
a8599bd8 | 3500 | int newcaps = le32_to_cpu(grant->caps); |
2cd698be | 3501 | int used, wanted, dirty; |
a8599bd8 SW |
3502 | u64 size = le64_to_cpu(grant->size); |
3503 | u64 max_size = le64_to_cpu(grant->max_size); | |
fdac94fa | 3504 | unsigned char check_caps = 0; |
52d60f8e | 3505 | bool was_stale = cap->cap_gen < atomic_read(&session->s_cap_gen); |
ab6c2c3e FF |
3506 | bool wake = false; |
3507 | bool writeback = false; | |
3508 | bool queue_trunc = false; | |
3509 | bool queue_invalidate = false; | |
ab6c2c3e | 3510 | bool deleted_inode = false; |
31c542a1 | 3511 | bool fill_inline = false; |
a8599bd8 | 3512 | |
0d91f0ad JL |
3513 | /* |
3514 | * If there is at least one crypto block then we'll trust | |
3515 | * fscrypt_file_size. If the real length of the file is 0, then | |
3516 | * ignore it (it has probably been truncated down to 0 by the MDS). | |
3517 | */ | |
3518 | if (IS_ENCRYPTED(inode) && size) | |
3519 | size = extra_info->fscrypt_file_size; | |
3520 | ||
38d46409 XL |
3521 | doutc(cl, "%p %llx.%llx cap %p mds%d seq %d %s\n", inode, |
3522 | ceph_vinop(inode), cap, session->s_mds, seq, | |
3523 | ceph_cap_string(newcaps)); | |
3524 | doutc(cl, " size %llu max_size %llu, i_size %llu\n", size, | |
3525 | max_size, i_size_read(inode)); | |
a8599bd8 | 3526 | |
11df2dfb | 3527 | |
a8599bd8 SW |
3528 | /* |
3529 | * If CACHE is being revoked, and we have no dirty buffers, | |
3530 | * try to invalidate (once). (If there are dirty buffers, we | |
3531 | * will invalidate _after_ writeback.) | |
3532 | */ | |
525d15e8 | 3533 | if (S_ISREG(inode->i_mode) && /* don't invalidate readdir cache */ |
fdd4e158 | 3534 | ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && |
3b454c49 | 3535 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
9abd4db7 | 3536 | !(ci->i_wrbuffer_ref || ci->i_wb_ref)) { |
e9075743 | 3537 | if (try_nonblocking_invalidate(inode)) { |
a8599bd8 SW |
3538 | /* there were locked pages.. invalidate later |
3539 | in a separate thread. */ | |
3540 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { | |
ab6c2c3e | 3541 | queue_invalidate = true; |
a8599bd8 SW |
3542 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
3543 | } | |
a8599bd8 | 3544 | } |
a8599bd8 SW |
3545 | } |
3546 | ||
d2f8bb27 YZ |
3547 | if (was_stale) |
3548 | cap->issued = cap->implemented = CEPH_CAP_PIN; | |
3549 | ||
3550 | /* | |
3551 | * auth mds of the inode changed. we received the cap export message, | |
3552 | * but still haven't received the cap import message. handle_cap_export | |
3553 | * updated the new auth MDS' cap. | |
3554 | * | |
3555 | * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message | |
3556 | * that was sent before the cap import message. So don't remove caps. | |
3557 | */ | |
3558 | if (ceph_seq_cmp(seq, cap->seq) <= 0) { | |
3559 | WARN_ON(cap != ci->i_auth_cap); | |
3560 | WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id)); | |
3561 | seq = cap->seq; | |
3562 | newcaps |= cap->issued; | |
3563 | } | |
3564 | ||
a8599bd8 | 3565 | /* side effects now are allowed */ |
52d60f8e | 3566 | cap->cap_gen = atomic_read(&session->s_cap_gen); |
11df2dfb | 3567 | cap->seq = seq; |
a8599bd8 SW |
3568 | |
3569 | __check_cap_issue(ci, cap, newcaps); | |
3570 | ||
176c77c9 JL |
3571 | inode_set_max_iversion_raw(inode, extra_info->change_attr); |
3572 | ||
f98a128a | 3573 | if ((newcaps & CEPH_CAP_AUTH_SHARED) && |
a1c6b835 | 3574 | (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) { |
ed94f87c JL |
3575 | umode_t mode = le32_to_cpu(grant->mode); |
3576 | ||
3577 | if (inode_wrong_type(inode, mode)) | |
3578 | pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n", | |
3579 | ceph_vinop(inode), inode->i_mode, mode); | |
3580 | else | |
3581 | inode->i_mode = mode; | |
05cb11c1 EB |
3582 | inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid)); |
3583 | inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid)); | |
ec62b894 | 3584 | ci->i_btime = extra_info->btime; |
38d46409 XL |
3585 | doutc(cl, "%p %llx.%llx mode 0%o uid.gid %d.%d\n", inode, |
3586 | ceph_vinop(inode), inode->i_mode, | |
3587 | from_kuid(&init_user_ns, inode->i_uid), | |
3588 | from_kgid(&init_user_ns, inode->i_gid)); | |
0d91f0ad JL |
3589 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
3590 | if (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len || | |
3591 | memcmp(ci->fscrypt_auth, extra_info->fscrypt_auth, | |
3592 | ci->fscrypt_auth_len)) | |
38d46409 XL |
3593 | pr_warn_ratelimited_client(cl, |
3594 | "cap grant attempt to change fscrypt_auth on non-I_NEW inode (old len %d new len %d)\n", | |
3595 | ci->fscrypt_auth_len, | |
0d91f0ad JL |
3596 | extra_info->fscrypt_auth_len); |
3597 | #endif | |
a8599bd8 SW |
3598 | } |
3599 | ||
fa466743 | 3600 | if ((newcaps & CEPH_CAP_LINK_SHARED) && |
a1c6b835 | 3601 | (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) { |
bfe86848 | 3602 | set_nlink(inode, le32_to_cpu(grant->nlink)); |
76bdbc7a | 3603 | if (inode->i_nlink == 0) |
ab6c2c3e | 3604 | deleted_inode = true; |
ca20c991 | 3605 | } |
a8599bd8 | 3606 | |
a1c6b835 YZ |
3607 | if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 && |
3608 | grant->xattr_len) { | |
a8599bd8 SW |
3609 | int len = le32_to_cpu(grant->xattr_len); |
3610 | u64 version = le64_to_cpu(grant->xattr_version); | |
3611 | ||
3612 | if (version > ci->i_xattrs.version) { | |
38d46409 XL |
3613 | doutc(cl, " got new xattrs v%llu on %p %llx.%llx len %d\n", |
3614 | version, inode, ceph_vinop(inode), len); | |
a8599bd8 SW |
3615 | if (ci->i_xattrs.blob) |
3616 | ceph_buffer_put(ci->i_xattrs.blob); | |
3617 | ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); | |
3618 | ci->i_xattrs.version = version; | |
7221fe4c | 3619 | ceph_forget_all_cached_acls(inode); |
ac6713cc | 3620 | ceph_security_invalidate_secctx(inode); |
a8599bd8 SW |
3621 | } |
3622 | } | |
3623 | ||
f98a128a | 3624 | if (newcaps & CEPH_CAP_ANY_RD) { |
9bbeab41 | 3625 | struct timespec64 mtime, atime, ctime; |
f98a128a | 3626 | /* ctime/mtime/atime? */ |
9bbeab41 AB |
3627 | ceph_decode_timespec64(&mtime, &grant->mtime); |
3628 | ceph_decode_timespec64(&atime, &grant->atime); | |
3629 | ceph_decode_timespec64(&ctime, &grant->ctime); | |
a1c6b835 | 3630 | ceph_fill_file_time(inode, extra_info->issued, |
f98a128a YZ |
3631 | le32_to_cpu(grant->time_warp_seq), |
3632 | &ctime, &mtime, &atime); | |
3633 | } | |
3634 | ||
4985d6f9 YZ |
3635 | if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) { |
3636 | ci->i_files = extra_info->nfiles; | |
3637 | ci->i_subdirs = extra_info->nsubdirs; | |
3638 | } | |
3639 | ||
f98a128a YZ |
3640 | if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) { |
3641 | /* file layout may have changed */ | |
7627151e | 3642 | s64 old_pool = ci->i_layout.pool_id; |
779fe0fb YZ |
3643 | struct ceph_string *old_ns; |
3644 | ||
7627151e | 3645 | ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout); |
779fe0fb YZ |
3646 | old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, |
3647 | lockdep_is_held(&ci->i_ceph_lock)); | |
a1c6b835 | 3648 | rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns); |
779fe0fb | 3649 | |
a1c6b835 YZ |
3650 | if (ci->i_layout.pool_id != old_pool || |
3651 | extra_info->pool_ns != old_ns) | |
7627151e | 3652 | ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; |
5ea5c5e0 | 3653 | |
a1c6b835 | 3654 | extra_info->pool_ns = old_ns; |
779fe0fb | 3655 | |
f98a128a | 3656 | /* size/truncate_seq? */ |
a1c6b835 | 3657 | queue_trunc = ceph_fill_file_size(inode, extra_info->issued, |
f98a128a YZ |
3658 | le32_to_cpu(grant->truncate_seq), |
3659 | le64_to_cpu(grant->truncate_size), | |
3660 | size); | |
84eea8c7 YZ |
3661 | } |
3662 | ||
3663 | if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) { | |
3664 | if (max_size != ci->i_max_size) { | |
38d46409 XL |
3665 | doutc(cl, "max_size %lld -> %llu\n", ci->i_max_size, |
3666 | max_size); | |
f98a128a YZ |
3667 | ci->i_max_size = max_size; |
3668 | if (max_size >= ci->i_wanted_max_size) { | |
3669 | ci->i_wanted_max_size = 0; /* reset */ | |
3670 | ci->i_requested_max_size = 0; | |
3671 | } | |
ab6c2c3e | 3672 | wake = true; |
a8599bd8 | 3673 | } |
a8599bd8 SW |
3674 | } |
3675 | ||
3676 | /* check cap bits */ | |
3677 | wanted = __ceph_caps_wanted(ci); | |
3678 | used = __ceph_caps_used(ci); | |
3679 | dirty = __ceph_caps_dirty(ci); | |
38d46409 XL |
3680 | doutc(cl, " my wanted = %s, used = %s, dirty %s\n", |
3681 | ceph_cap_string(wanted), ceph_cap_string(used), | |
3682 | ceph_cap_string(dirty)); | |
fdac94fa YZ |
3683 | |
3684 | if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) && | |
3685 | (wanted & ~(cap->mds_wanted | newcaps))) { | |
3686 | /* | |
3687 | * If mds is importing cap, prior cap messages that update | |
3688 | * 'wanted' may get dropped by mds (migrate seq mismatch). | |
3689 | * | |
3690 | * We don't send cap message to update 'wanted' if what we | |
3691 | * want are already issued. If mds revokes caps, cap message | |
3692 | * that releases caps also tells mds what we want. But if | |
3693 | * caps got revoked by mds forcedly (session stale). We may | |
3694 | * haven't told mds what we want. | |
3695 | */ | |
3696 | check_caps = 1; | |
a8599bd8 SW |
3697 | } |
3698 | ||
a8599bd8 SW |
3699 | /* revocation, grant, or no-op? */ |
3700 | if (cap->issued & ~newcaps) { | |
3b454c49 SW |
3701 | int revoking = cap->issued & ~newcaps; |
3702 | ||
38d46409 XL |
3703 | doutc(cl, "revocation: %s -> %s (revoking %s)\n", |
3704 | ceph_cap_string(cap->issued), ceph_cap_string(newcaps), | |
3705 | ceph_cap_string(revoking)); | |
525d15e8 YZ |
3706 | if (S_ISREG(inode->i_mode) && |
3707 | (revoking & used & CEPH_CAP_FILE_BUFFER)) | |
ab6c2c3e | 3708 | writeback = true; /* initiate writeback; will delay ack */ |
525d15e8 YZ |
3709 | else if (queue_invalidate && |
3710 | revoking == CEPH_CAP_FILE_CACHE && | |
3711 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0) | |
3b454c49 SW |
3712 | ; /* do nothing yet, invalidation will be queued */ |
3713 | else if (cap == ci->i_auth_cap) | |
3714 | check_caps = 1; /* check auth cap only */ | |
3715 | else | |
3716 | check_caps = 2; /* check all caps */ | |
f7913573 XL |
3717 | /* If there is new caps, try to wake up the waiters */ |
3718 | if (~cap->issued & newcaps) | |
3719 | wake = true; | |
a8599bd8 | 3720 | cap->issued = newcaps; |
978097c9 | 3721 | cap->implemented |= newcaps; |
a8599bd8 | 3722 | } else if (cap->issued == newcaps) { |
38d46409 XL |
3723 | doutc(cl, "caps unchanged: %s -> %s\n", |
3724 | ceph_cap_string(cap->issued), | |
3725 | ceph_cap_string(newcaps)); | |
a8599bd8 | 3726 | } else { |
38d46409 XL |
3727 | doutc(cl, "grant: %s -> %s\n", ceph_cap_string(cap->issued), |
3728 | ceph_cap_string(newcaps)); | |
6ee6b953 YZ |
3729 | /* non-auth MDS is revoking the newly grant caps ? */ |
3730 | if (cap == ci->i_auth_cap && | |
3731 | __ceph_caps_revoking_other(ci, cap, newcaps)) | |
3732 | check_caps = 2; | |
3733 | ||
a8599bd8 SW |
3734 | cap->issued = newcaps; |
3735 | cap->implemented |= newcaps; /* add bits only, to | |
3736 | * avoid stepping on a | |
3737 | * pending revocation */ | |
ab6c2c3e | 3738 | wake = true; |
a8599bd8 | 3739 | } |
978097c9 | 3740 | BUG_ON(cap->issued & ~cap->implemented); |
a8599bd8 | 3741 | |
257e6172 XL |
3742 | /* don't let check_caps skip sending a response to MDS for revoke msgs */ |
3743 | if (le32_to_cpu(grant->op) == CEPH_CAP_OP_REVOKE) { | |
3744 | cap->mds_wanted = 0; | |
3745 | if (cap == ci->i_auth_cap) | |
3746 | check_caps = 1; /* check auth cap only */ | |
3747 | else | |
3748 | check_caps = 2; /* check all caps */ | |
3749 | } | |
3750 | ||
a1c6b835 YZ |
3751 | if (extra_info->inline_version > 0 && |
3752 | extra_info->inline_version >= ci->i_inline_version) { | |
3753 | ci->i_inline_version = extra_info->inline_version; | |
31c542a1 YZ |
3754 | if (ci->i_inline_version != CEPH_INLINE_NONE && |
3755 | (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO))) | |
3756 | fill_inline = true; | |
3757 | } | |
3758 | ||
58dd4385 JL |
3759 | if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) { |
3760 | if (ci->i_auth_cap == cap) { | |
3761 | if (newcaps & ~extra_info->issued) | |
3762 | wake = true; | |
3763 | ||
3764 | if (ci->i_requested_max_size > max_size || | |
3765 | !(le32_to_cpu(grant->wanted) & CEPH_CAP_ANY_FILE_WR)) { | |
3766 | /* re-request max_size if necessary */ | |
3767 | ci->i_requested_max_size = 0; | |
3768 | wake = true; | |
3769 | } | |
6f05b30e | 3770 | |
58dd4385 | 3771 | ceph_kick_flushing_inode_caps(session, ci); |
6f05b30e | 3772 | } |
a1c6b835 | 3773 | up_read(&session->s_mdsc->snap_rwsem); |
2cd698be | 3774 | } |
58dd4385 | 3775 | spin_unlock(&ci->i_ceph_lock); |
2cd698be | 3776 | |
31c542a1 | 3777 | if (fill_inline) |
a1c6b835 YZ |
3778 | ceph_fill_inline_data(inode, NULL, extra_info->inline_data, |
3779 | extra_info->inline_len); | |
31c542a1 | 3780 | |
14649758 | 3781 | if (queue_trunc) |
c6bcda6f | 3782 | ceph_queue_vmtruncate(inode); |
c6bcda6f | 3783 | |
3c6f6b79 | 3784 | if (writeback) |
a8599bd8 SW |
3785 | /* |
3786 | * queue inode for writeback: we can't actually call | |
3787 | * filemap_write_and_wait, etc. from message handler | |
3788 | * context. | |
3789 | */ | |
3c6f6b79 SW |
3790 | ceph_queue_writeback(inode); |
3791 | if (queue_invalidate) | |
3792 | ceph_queue_invalidate(inode); | |
ca20c991 YZ |
3793 | if (deleted_inode) |
3794 | invalidate_aliases(inode); | |
a8599bd8 | 3795 | if (wake) |
03066f23 | 3796 | wake_up_all(&ci->i_cap_wq); |
15637c8b | 3797 | |
6a92b08f | 3798 | mutex_unlock(&session->s_mutex); |
15637c8b | 3799 | if (check_caps == 1) |
e4b731cc | 3800 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_NOINVAL); |
15637c8b | 3801 | else if (check_caps == 2) |
e4b731cc | 3802 | ceph_check_caps(ci, CHECK_CAPS_NOINVAL); |
a8599bd8 SW |
3803 | } |
3804 | ||
3805 | /* | |
3806 | * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the | |
3807 | * MDS has been safely committed. | |
3808 | */ | |
6df058c0 | 3809 | static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, |
a8599bd8 SW |
3810 | struct ceph_mds_caps *m, |
3811 | struct ceph_mds_session *session, | |
3812 | struct ceph_cap *cap) | |
be655596 | 3813 | __releases(ci->i_ceph_lock) |
a8599bd8 SW |
3814 | { |
3815 | struct ceph_inode_info *ci = ceph_inode(inode); | |
5995d90d | 3816 | struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
38d46409 | 3817 | struct ceph_client *cl = mdsc->fsc->client; |
e4500b5e | 3818 | struct ceph_cap_flush *cf, *tmp_cf; |
553adfd9 | 3819 | LIST_HEAD(to_remove); |
a8599bd8 SW |
3820 | unsigned seq = le32_to_cpu(m->seq); |
3821 | int dirty = le32_to_cpu(m->dirty); | |
3822 | int cleaned = 0; | |
c8799fc4 | 3823 | bool drop = false; |
7271efa7 TM |
3824 | bool wake_ci = false; |
3825 | bool wake_mdsc = false; | |
a8599bd8 | 3826 | |
e4500b5e | 3827 | list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) { |
d7dbfb4f | 3828 | /* Is this the one that was flushed? */ |
553adfd9 YZ |
3829 | if (cf->tid == flush_tid) |
3830 | cleaned = cf->caps; | |
d7dbfb4f JL |
3831 | |
3832 | /* Is this a capsnap? */ | |
b2f9fa1f | 3833 | if (cf->is_capsnap) |
0e294387 | 3834 | continue; |
d7dbfb4f | 3835 | |
553adfd9 | 3836 | if (cf->tid <= flush_tid) { |
d7dbfb4f JL |
3837 | /* |
3838 | * An earlier or current tid. The FLUSH_ACK should | |
3839 | * represent a superset of this flush's caps. | |
3840 | */ | |
681ac634 | 3841 | wake_ci |= __detach_cap_flush_from_ci(ci, cf); |
e4500b5e | 3842 | list_add_tail(&cf->i_list, &to_remove); |
553adfd9 | 3843 | } else { |
d7dbfb4f JL |
3844 | /* |
3845 | * This is a later one. Any caps in it are still dirty | |
3846 | * so don't count them as cleaned. | |
3847 | */ | |
553adfd9 YZ |
3848 | cleaned &= ~cf->caps; |
3849 | if (!cleaned) | |
3850 | break; | |
3851 | } | |
3852 | } | |
a8599bd8 | 3853 | |
38d46409 XL |
3854 | doutc(cl, "%p %llx.%llx mds%d seq %d on %s cleaned %s, flushing %s -> %s\n", |
3855 | inode, ceph_vinop(inode), session->s_mds, seq, | |
3856 | ceph_cap_string(dirty), ceph_cap_string(cleaned), | |
3857 | ceph_cap_string(ci->i_flushing_caps), | |
3858 | ceph_cap_string(ci->i_flushing_caps & ~cleaned)); | |
a8599bd8 | 3859 | |
8310b089 | 3860 | if (list_empty(&to_remove) && !cleaned) |
a8599bd8 SW |
3861 | goto out; |
3862 | ||
a8599bd8 | 3863 | ci->i_flushing_caps &= ~cleaned; |
a8599bd8 SW |
3864 | |
3865 | spin_lock(&mdsc->cap_dirty_lock); | |
8310b089 | 3866 | |
681ac634 JL |
3867 | list_for_each_entry(cf, &to_remove, i_list) |
3868 | wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc, cf); | |
8310b089 | 3869 | |
a8599bd8 | 3870 | if (ci->i_flushing_caps == 0) { |
0e294387 YZ |
3871 | if (list_empty(&ci->i_cap_flush_list)) { |
3872 | list_del_init(&ci->i_flushing_item); | |
3873 | if (!list_empty(&session->s_cap_flushing)) { | |
38d46409 XL |
3874 | struct inode *inode = |
3875 | &list_first_entry(&session->s_cap_flushing, | |
3876 | struct ceph_inode_info, | |
3877 | i_flushing_item)->netfs.inode; | |
3878 | doutc(cl, " mds%d still flushing cap on %p %llx.%llx\n", | |
3879 | session->s_mds, inode, ceph_vinop(inode)); | |
0e294387 YZ |
3880 | } |
3881 | } | |
a8599bd8 | 3882 | mdsc->num_cap_flushing--; |
38d46409 XL |
3883 | doutc(cl, " %p %llx.%llx now !flushing\n", inode, |
3884 | ceph_vinop(inode)); | |
afcdaea3 SW |
3885 | |
3886 | if (ci->i_dirty_caps == 0) { | |
38d46409 XL |
3887 | doutc(cl, " %p %llx.%llx now clean\n", inode, |
3888 | ceph_vinop(inode)); | |
afcdaea3 | 3889 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
c8799fc4 | 3890 | drop = true; |
5dda377c YZ |
3891 | if (ci->i_wr_ref == 0 && |
3892 | ci->i_wrbuffer_ref_head == 0) { | |
7d8cb26d SW |
3893 | BUG_ON(!ci->i_head_snapc); |
3894 | ceph_put_snap_context(ci->i_head_snapc); | |
3895 | ci->i_head_snapc = NULL; | |
3896 | } | |
76e3b390 SW |
3897 | } else { |
3898 | BUG_ON(list_empty(&ci->i_dirty_item)); | |
afcdaea3 | 3899 | } |
a8599bd8 SW |
3900 | } |
3901 | spin_unlock(&mdsc->cap_dirty_lock); | |
a8599bd8 SW |
3902 | |
3903 | out: | |
be655596 | 3904 | spin_unlock(&ci->i_ceph_lock); |
553adfd9 YZ |
3905 | |
3906 | while (!list_empty(&to_remove)) { | |
3907 | cf = list_first_entry(&to_remove, | |
e4500b5e | 3908 | struct ceph_cap_flush, i_list); |
b2f9fa1f XL |
3909 | list_del_init(&cf->i_list); |
3910 | if (!cf->is_capsnap) | |
3911 | ceph_free_cap_flush(cf); | |
553adfd9 | 3912 | } |
c8799fc4 YZ |
3913 | |
3914 | if (wake_ci) | |
3915 | wake_up_all(&ci->i_cap_wq); | |
3916 | if (wake_mdsc) | |
3917 | wake_up_all(&mdsc->cap_flushing_wq); | |
afcdaea3 | 3918 | if (drop) |
a8599bd8 SW |
3919 | iput(inode); |
3920 | } | |
3921 | ||
a6d37ccd XL |
3922 | void __ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap, |
3923 | bool *wake_ci, bool *wake_mdsc) | |
3924 | { | |
3925 | struct ceph_inode_info *ci = ceph_inode(inode); | |
5995d90d | 3926 | struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
38d46409 | 3927 | struct ceph_client *cl = mdsc->fsc->client; |
a6d37ccd XL |
3928 | bool ret; |
3929 | ||
3930 | lockdep_assert_held(&ci->i_ceph_lock); | |
3931 | ||
38d46409 XL |
3932 | doutc(cl, "removing capsnap %p, %p %llx.%llx ci %p\n", capsnap, |
3933 | inode, ceph_vinop(inode), ci); | |
a6d37ccd XL |
3934 | |
3935 | list_del_init(&capsnap->ci_item); | |
3936 | ret = __detach_cap_flush_from_ci(ci, &capsnap->cap_flush); | |
3937 | if (wake_ci) | |
3938 | *wake_ci = ret; | |
3939 | ||
3940 | spin_lock(&mdsc->cap_dirty_lock); | |
3941 | if (list_empty(&ci->i_cap_flush_list)) | |
3942 | list_del_init(&ci->i_flushing_item); | |
3943 | ||
3944 | ret = __detach_cap_flush_from_mdsc(mdsc, &capsnap->cap_flush); | |
3945 | if (wake_mdsc) | |
3946 | *wake_mdsc = ret; | |
3947 | spin_unlock(&mdsc->cap_dirty_lock); | |
3948 | } | |
3949 | ||
3950 | void ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap, | |
3951 | bool *wake_ci, bool *wake_mdsc) | |
3952 | { | |
3953 | struct ceph_inode_info *ci = ceph_inode(inode); | |
3954 | ||
3955 | lockdep_assert_held(&ci->i_ceph_lock); | |
3956 | ||
3957 | WARN_ON_ONCE(capsnap->dirty_pages || capsnap->writing); | |
3958 | __ceph_remove_capsnap(inode, capsnap, wake_ci, wake_mdsc); | |
3959 | } | |
3960 | ||
a8599bd8 SW |
3961 | /* |
3962 | * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can | |
3963 | * throw away our cap_snap. | |
3964 | * | |
3965 | * Caller hold s_mutex. | |
3966 | */ | |
6df058c0 | 3967 | static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, |
a8599bd8 SW |
3968 | struct ceph_mds_caps *m, |
3969 | struct ceph_mds_session *session) | |
3970 | { | |
3971 | struct ceph_inode_info *ci = ceph_inode(inode); | |
5995d90d | 3972 | struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc; |
38d46409 | 3973 | struct ceph_client *cl = mdsc->fsc->client; |
a8599bd8 | 3974 | u64 follows = le64_to_cpu(m->snap_follows); |
3ffa9d6f | 3975 | struct ceph_cap_snap *capsnap = NULL, *iter; |
c8799fc4 YZ |
3976 | bool wake_ci = false; |
3977 | bool wake_mdsc = false; | |
a8599bd8 | 3978 | |
38d46409 XL |
3979 | doutc(cl, "%p %llx.%llx ci %p mds%d follows %lld\n", inode, |
3980 | ceph_vinop(inode), ci, session->s_mds, follows); | |
a8599bd8 | 3981 | |
be655596 | 3982 | spin_lock(&ci->i_ceph_lock); |
3ffa9d6f JK |
3983 | list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) { |
3984 | if (iter->follows == follows) { | |
3985 | if (iter->cap_flush.tid != flush_tid) { | |
38d46409 XL |
3986 | doutc(cl, " cap_snap %p follows %lld " |
3987 | "tid %lld != %lld\n", iter, | |
3988 | follows, flush_tid, | |
3989 | iter->cap_flush.tid); | |
a8599bd8 SW |
3990 | break; |
3991 | } | |
3ffa9d6f | 3992 | capsnap = iter; |
a8599bd8 SW |
3993 | break; |
3994 | } else { | |
38d46409 XL |
3995 | doutc(cl, " skipping cap_snap %p follows %lld\n", |
3996 | iter, iter->follows); | |
a8599bd8 SW |
3997 | } |
3998 | } | |
3ffa9d6f | 3999 | if (capsnap) |
a6d37ccd | 4000 | ceph_remove_capsnap(inode, capsnap, &wake_ci, &wake_mdsc); |
be655596 | 4001 | spin_unlock(&ci->i_ceph_lock); |
a6d37ccd | 4002 | |
3ffa9d6f | 4003 | if (capsnap) { |
0e294387 YZ |
4004 | ceph_put_snap_context(capsnap->context); |
4005 | ceph_put_cap_snap(capsnap); | |
c8799fc4 YZ |
4006 | if (wake_ci) |
4007 | wake_up_all(&ci->i_cap_wq); | |
4008 | if (wake_mdsc) | |
4009 | wake_up_all(&mdsc->cap_flushing_wq); | |
a8599bd8 | 4010 | iput(inode); |
0e294387 | 4011 | } |
a8599bd8 SW |
4012 | } |
4013 | ||
4014 | /* | |
4015 | * Handle TRUNC from MDS, indicating file truncation. | |
4016 | * | |
4017 | * caller hold s_mutex. | |
4018 | */ | |
7391fba2 | 4019 | static bool handle_cap_trunc(struct inode *inode, |
a8599bd8 | 4020 | struct ceph_mds_caps *trunc, |
0d91f0ad JL |
4021 | struct ceph_mds_session *session, |
4022 | struct cap_extra_info *extra_info) | |
a8599bd8 SW |
4023 | { |
4024 | struct ceph_inode_info *ci = ceph_inode(inode); | |
38d46409 | 4025 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 SW |
4026 | int mds = session->s_mds; |
4027 | int seq = le32_to_cpu(trunc->seq); | |
4028 | u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); | |
4029 | u64 truncate_size = le64_to_cpu(trunc->truncate_size); | |
4030 | u64 size = le64_to_cpu(trunc->size); | |
4031 | int implemented = 0; | |
4032 | int dirty = __ceph_caps_dirty(ci); | |
4033 | int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); | |
7391fba2 JL |
4034 | bool queue_trunc = false; |
4035 | ||
4036 | lockdep_assert_held(&ci->i_ceph_lock); | |
a8599bd8 SW |
4037 | |
4038 | issued |= implemented | dirty; | |
4039 | ||
0d91f0ad JL |
4040 | /* |
4041 | * If there is at least one crypto block then we'll trust | |
4042 | * fscrypt_file_size. If the real length of the file is 0, then | |
4043 | * ignore it (it has probably been truncated down to 0 by the MDS). | |
4044 | */ | |
4045 | if (IS_ENCRYPTED(inode) && size) | |
4046 | size = extra_info->fscrypt_file_size; | |
4047 | ||
38d46409 XL |
4048 | doutc(cl, "%p %llx.%llx mds%d seq %d to %lld truncate seq %d\n", |
4049 | inode, ceph_vinop(inode), mds, seq, truncate_size, truncate_seq); | |
a8599bd8 SW |
4050 | queue_trunc = ceph_fill_file_size(inode, issued, |
4051 | truncate_seq, truncate_size, size); | |
7391fba2 | 4052 | return queue_trunc; |
a8599bd8 SW |
4053 | } |
4054 | ||
4055 | /* | |
4056 | * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a | |
4057 | * different one. If we are the most recent migration we've seen (as | |
4058 | * indicated by mseq), make note of the migrating cap bits for the | |
4059 | * duration (until we see the corresponding IMPORT). | |
4060 | * | |
4061 | * caller holds s_mutex | |
4062 | */ | |
4063 | static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, | |
11df2dfb YZ |
4064 | struct ceph_mds_cap_peer *ph, |
4065 | struct ceph_mds_session *session) | |
a8599bd8 | 4066 | { |
5995d90d | 4067 | struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc; |
38d46409 | 4068 | struct ceph_client *cl = mdsc->fsc->client; |
11df2dfb | 4069 | struct ceph_mds_session *tsession = NULL; |
d9df2783 | 4070 | struct ceph_cap *cap, *tcap, *new_cap = NULL; |
a8599bd8 | 4071 | struct ceph_inode_info *ci = ceph_inode(inode); |
11df2dfb | 4072 | u64 t_cap_id; |
a8599bd8 | 4073 | unsigned mseq = le32_to_cpu(ex->migrate_seq); |
11df2dfb YZ |
4074 | unsigned t_seq, t_mseq; |
4075 | int target, issued; | |
4076 | int mds = session->s_mds; | |
a8599bd8 | 4077 | |
11df2dfb YZ |
4078 | if (ph) { |
4079 | t_cap_id = le64_to_cpu(ph->cap_id); | |
4080 | t_seq = le32_to_cpu(ph->seq); | |
4081 | t_mseq = le32_to_cpu(ph->mseq); | |
4082 | target = le32_to_cpu(ph->mds); | |
4083 | } else { | |
4084 | t_cap_id = t_seq = t_mseq = 0; | |
4085 | target = -1; | |
4086 | } | |
a8599bd8 | 4087 | |
38d46409 XL |
4088 | doutc(cl, "%p %llx.%llx ci %p mds%d mseq %d target %d\n", |
4089 | inode, ceph_vinop(inode), ci, mds, mseq, target); | |
11df2dfb | 4090 | retry: |
7f47f7f3 | 4091 | down_read(&mdsc->snap_rwsem); |
be655596 | 4092 | spin_lock(&ci->i_ceph_lock); |
11df2dfb | 4093 | cap = __get_cap_for_mds(ci, mds); |
ca665e02 | 4094 | if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id)) |
11df2dfb | 4095 | goto out_unlock; |
a8599bd8 | 4096 | |
11df2dfb | 4097 | if (target < 0) { |
197b7d79 | 4098 | ceph_remove_cap(mdsc, cap, false); |
11df2dfb | 4099 | goto out_unlock; |
a8599bd8 SW |
4100 | } |
4101 | ||
11df2dfb YZ |
4102 | /* |
4103 | * now we know we haven't received the cap import message yet | |
4104 | * because the exported cap still exist. | |
4105 | */ | |
db354052 | 4106 | |
11df2dfb | 4107 | issued = cap->issued; |
d84b37f9 | 4108 | if (issued != cap->implemented) |
38d46409 XL |
4109 | pr_err_ratelimited_client(cl, "issued != implemented: " |
4110 | "%p %llx.%llx mds%d seq %d mseq %d" | |
4111 | " issued %s implemented %s\n", | |
4112 | inode, ceph_vinop(inode), mds, | |
4113 | cap->seq, cap->mseq, | |
4114 | ceph_cap_string(issued), | |
4115 | ceph_cap_string(cap->implemented)); | |
d84b37f9 | 4116 | |
11df2dfb YZ |
4117 | |
4118 | tcap = __get_cap_for_mds(ci, target); | |
4119 | if (tcap) { | |
4120 | /* already have caps from the target */ | |
fa0aa3b8 | 4121 | if (tcap->cap_id == t_cap_id && |
11df2dfb | 4122 | ceph_seq_cmp(tcap->seq, t_seq) < 0) { |
38d46409 XL |
4123 | doutc(cl, " updating import cap %p mds%d\n", tcap, |
4124 | target); | |
11df2dfb YZ |
4125 | tcap->cap_id = t_cap_id; |
4126 | tcap->seq = t_seq - 1; | |
4127 | tcap->issue_seq = t_seq - 1; | |
11df2dfb YZ |
4128 | tcap->issued |= issued; |
4129 | tcap->implemented |= issued; | |
1cf03a68 | 4130 | if (cap == ci->i_auth_cap) { |
11df2dfb | 4131 | ci->i_auth_cap = tcap; |
1cf03a68 | 4132 | change_auth_cap_ses(ci, tcap->session); |
db354052 | 4133 | } |
a8599bd8 | 4134 | } |
197b7d79 | 4135 | ceph_remove_cap(mdsc, cap, false); |
11df2dfb | 4136 | goto out_unlock; |
d9df2783 | 4137 | } else if (tsession) { |
11df2dfb | 4138 | /* add placeholder for the export tagert */ |
d9df2783 | 4139 | int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0; |
00f06cba | 4140 | tcap = new_cap; |
135e671e | 4141 | ceph_add_cap(inode, tsession, t_cap_id, issued, 0, |
d9df2783 YZ |
4142 | t_seq - 1, t_mseq, (u64)-1, flag, &new_cap); |
4143 | ||
00f06cba YZ |
4144 | if (!list_empty(&ci->i_cap_flush_list) && |
4145 | ci->i_auth_cap == tcap) { | |
4146 | spin_lock(&mdsc->cap_dirty_lock); | |
4147 | list_move_tail(&ci->i_flushing_item, | |
4148 | &tcap->session->s_cap_flushing); | |
4149 | spin_unlock(&mdsc->cap_dirty_lock); | |
4150 | } | |
4151 | ||
197b7d79 | 4152 | ceph_remove_cap(mdsc, cap, false); |
d9df2783 | 4153 | goto out_unlock; |
a8599bd8 SW |
4154 | } |
4155 | ||
be655596 | 4156 | spin_unlock(&ci->i_ceph_lock); |
7f47f7f3 | 4157 | up_read(&mdsc->snap_rwsem); |
11df2dfb YZ |
4158 | mutex_unlock(&session->s_mutex); |
4159 | ||
4160 | /* open target session */ | |
4161 | tsession = ceph_mdsc_open_export_target_session(mdsc, target); | |
4162 | if (!IS_ERR(tsession)) { | |
4163 | if (mds > target) { | |
4164 | mutex_lock(&session->s_mutex); | |
4165 | mutex_lock_nested(&tsession->s_mutex, | |
4166 | SINGLE_DEPTH_NESTING); | |
4167 | } else { | |
4168 | mutex_lock(&tsession->s_mutex); | |
4169 | mutex_lock_nested(&session->s_mutex, | |
4170 | SINGLE_DEPTH_NESTING); | |
4171 | } | |
d9df2783 | 4172 | new_cap = ceph_get_cap(mdsc, NULL); |
11df2dfb YZ |
4173 | } else { |
4174 | WARN_ON(1); | |
4175 | tsession = NULL; | |
4176 | target = -1; | |
4d8e28ff | 4177 | mutex_lock(&session->s_mutex); |
11df2dfb YZ |
4178 | } |
4179 | goto retry; | |
4180 | ||
4181 | out_unlock: | |
4182 | spin_unlock(&ci->i_ceph_lock); | |
7f47f7f3 | 4183 | up_read(&mdsc->snap_rwsem); |
11df2dfb YZ |
4184 | mutex_unlock(&session->s_mutex); |
4185 | if (tsession) { | |
4186 | mutex_unlock(&tsession->s_mutex); | |
4187 | ceph_put_mds_session(tsession); | |
4188 | } | |
d9df2783 YZ |
4189 | if (new_cap) |
4190 | ceph_put_cap(mdsc, new_cap); | |
a8599bd8 SW |
4191 | } |
4192 | ||
4193 | /* | |
2cd698be | 4194 | * Handle cap IMPORT. |
a8599bd8 | 4195 | * |
2cd698be | 4196 | * caller holds s_mutex. acquires i_ceph_lock |
a8599bd8 SW |
4197 | */ |
4198 | static void handle_cap_import(struct ceph_mds_client *mdsc, | |
4199 | struct inode *inode, struct ceph_mds_caps *im, | |
4ee6a914 | 4200 | struct ceph_mds_cap_peer *ph, |
a8599bd8 | 4201 | struct ceph_mds_session *session, |
2cd698be | 4202 | struct ceph_cap **target_cap, int *old_issued) |
a8599bd8 SW |
4203 | { |
4204 | struct ceph_inode_info *ci = ceph_inode(inode); | |
38d46409 | 4205 | struct ceph_client *cl = mdsc->fsc->client; |
2cd698be | 4206 | struct ceph_cap *cap, *ocap, *new_cap = NULL; |
a8599bd8 | 4207 | int mds = session->s_mds; |
2cd698be YZ |
4208 | int issued; |
4209 | unsigned caps = le32_to_cpu(im->caps); | |
a8599bd8 SW |
4210 | unsigned wanted = le32_to_cpu(im->wanted); |
4211 | unsigned seq = le32_to_cpu(im->seq); | |
4212 | unsigned mseq = le32_to_cpu(im->migrate_seq); | |
4213 | u64 realmino = le64_to_cpu(im->realm); | |
4214 | u64 cap_id = le64_to_cpu(im->cap_id); | |
4ee6a914 YZ |
4215 | u64 p_cap_id; |
4216 | int peer; | |
a8599bd8 | 4217 | |
4ee6a914 YZ |
4218 | if (ph) { |
4219 | p_cap_id = le64_to_cpu(ph->cap_id); | |
4220 | peer = le32_to_cpu(ph->mds); | |
4221 | } else { | |
4222 | p_cap_id = 0; | |
4223 | peer = -1; | |
4224 | } | |
db354052 | 4225 | |
38d46409 XL |
4226 | doutc(cl, "%p %llx.%llx ci %p mds%d mseq %d peer %d\n", |
4227 | inode, ceph_vinop(inode), ci, mds, mseq, peer); | |
d9df2783 | 4228 | retry: |
d9df2783 YZ |
4229 | cap = __get_cap_for_mds(ci, mds); |
4230 | if (!cap) { | |
4231 | if (!new_cap) { | |
4232 | spin_unlock(&ci->i_ceph_lock); | |
4233 | new_cap = ceph_get_cap(mdsc, NULL); | |
78333233 | 4234 | spin_lock(&ci->i_ceph_lock); |
d9df2783 YZ |
4235 | goto retry; |
4236 | } | |
2cd698be YZ |
4237 | cap = new_cap; |
4238 | } else { | |
4239 | if (new_cap) { | |
4240 | ceph_put_cap(mdsc, new_cap); | |
4241 | new_cap = NULL; | |
4242 | } | |
d9df2783 YZ |
4243 | } |
4244 | ||
2cd698be YZ |
4245 | __ceph_caps_issued(ci, &issued); |
4246 | issued |= __ceph_caps_dirty(ci); | |
4247 | ||
135e671e | 4248 | ceph_add_cap(inode, session, cap_id, caps, wanted, seq, mseq, |
d9df2783 YZ |
4249 | realmino, CEPH_CAP_FLAG_AUTH, &new_cap); |
4250 | ||
2cd698be YZ |
4251 | ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL; |
4252 | if (ocap && ocap->cap_id == p_cap_id) { | |
38d46409 XL |
4253 | doutc(cl, " remove export cap %p mds%d flags %d\n", |
4254 | ocap, peer, ph->flags); | |
4ee6a914 | 4255 | if ((ph->flags & CEPH_CAP_FLAG_AUTH) && |
2cd698be YZ |
4256 | (ocap->seq != le32_to_cpu(ph->seq) || |
4257 | ocap->mseq != le32_to_cpu(ph->mseq))) { | |
38d46409 XL |
4258 | pr_err_ratelimited_client(cl, "mismatched seq/mseq: " |
4259 | "%p %llx.%llx mds%d seq %d mseq %d" | |
4260 | " importer mds%d has peer seq %d mseq %d\n", | |
4261 | inode, ceph_vinop(inode), peer, | |
4262 | ocap->seq, ocap->mseq, mds, | |
4263 | le32_to_cpu(ph->seq), | |
d84b37f9 | 4264 | le32_to_cpu(ph->mseq)); |
db354052 | 4265 | } |
197b7d79 | 4266 | ceph_remove_cap(mdsc, ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE)); |
a8599bd8 SW |
4267 | } |
4268 | ||
2cd698be YZ |
4269 | *old_issued = issued; |
4270 | *target_cap = cap; | |
a8599bd8 SW |
4271 | } |
4272 | ||
0d91f0ad JL |
4273 | #ifdef CONFIG_FS_ENCRYPTION |
4274 | static int parse_fscrypt_fields(void **p, void *end, | |
4275 | struct cap_extra_info *extra) | |
4276 | { | |
4277 | u32 len; | |
4278 | ||
4279 | ceph_decode_32_safe(p, end, extra->fscrypt_auth_len, bad); | |
4280 | if (extra->fscrypt_auth_len) { | |
4281 | ceph_decode_need(p, end, extra->fscrypt_auth_len, bad); | |
4282 | extra->fscrypt_auth = kmalloc(extra->fscrypt_auth_len, | |
4283 | GFP_KERNEL); | |
4284 | if (!extra->fscrypt_auth) | |
4285 | return -ENOMEM; | |
4286 | ceph_decode_copy_safe(p, end, extra->fscrypt_auth, | |
4287 | extra->fscrypt_auth_len, bad); | |
4288 | } | |
4289 | ||
4290 | ceph_decode_32_safe(p, end, len, bad); | |
4291 | if (len >= sizeof(u64)) { | |
4292 | ceph_decode_64_safe(p, end, extra->fscrypt_file_size, bad); | |
4293 | len -= sizeof(u64); | |
4294 | } | |
4295 | ceph_decode_skip_n(p, end, len, bad); | |
4296 | return 0; | |
4297 | bad: | |
4298 | return -EIO; | |
4299 | } | |
4300 | #else | |
4301 | static int parse_fscrypt_fields(void **p, void *end, | |
4302 | struct cap_extra_info *extra) | |
4303 | { | |
4304 | u32 len; | |
4305 | ||
4306 | /* Don't care about these fields unless we're encryption-capable */ | |
4307 | ceph_decode_32_safe(p, end, len, bad); | |
4308 | if (len) | |
4309 | ceph_decode_skip_n(p, end, len, bad); | |
4310 | ceph_decode_32_safe(p, end, len, bad); | |
4311 | if (len) | |
4312 | ceph_decode_skip_n(p, end, len, bad); | |
4313 | return 0; | |
4314 | bad: | |
4315 | return -EIO; | |
4316 | } | |
4317 | #endif | |
4318 | ||
a8599bd8 SW |
4319 | /* |
4320 | * Handle a caps message from the MDS. | |
4321 | * | |
4322 | * Identify the appropriate session, inode, and call the right handler | |
4323 | * based on the cap op. | |
4324 | */ | |
4325 | void ceph_handle_caps(struct ceph_mds_session *session, | |
4326 | struct ceph_msg *msg) | |
4327 | { | |
4328 | struct ceph_mds_client *mdsc = session->s_mdsc; | |
38d46409 | 4329 | struct ceph_client *cl = mdsc->fsc->client; |
a8599bd8 | 4330 | struct inode *inode; |
be655596 | 4331 | struct ceph_inode_info *ci; |
a8599bd8 SW |
4332 | struct ceph_cap *cap; |
4333 | struct ceph_mds_caps *h; | |
4ee6a914 | 4334 | struct ceph_mds_cap_peer *peer = NULL; |
779fe0fb | 4335 | struct ceph_snap_realm *realm = NULL; |
a1c6b835 | 4336 | int op; |
4985d6f9 | 4337 | int msg_version = le16_to_cpu(msg->hdr.version); |
3d7ded4d | 4338 | u32 seq, mseq; |
a8599bd8 | 4339 | struct ceph_vino vino; |
70edb55b | 4340 | void *snaptrace; |
ce1fbc8d | 4341 | size_t snaptrace_len; |
fb01d1f8 | 4342 | void *p, *end; |
a1c6b835 | 4343 | struct cap_extra_info extra_info = {}; |
7391fba2 | 4344 | bool queue_trunc; |
a68e564a | 4345 | bool close_sessions = false; |
ce72d4e0 | 4346 | bool do_cap_release = false; |
a8599bd8 | 4347 | |
38d46409 | 4348 | doutc(cl, "from mds%d\n", session->s_mds); |
a8599bd8 | 4349 | |
e3dfcab2 XL |
4350 | if (!ceph_inc_mds_stopping_blocker(mdsc, session)) |
4351 | return; | |
4352 | ||
a8599bd8 | 4353 | /* decode */ |
4ee6a914 | 4354 | end = msg->front.iov_base + msg->front.iov_len; |
a8599bd8 SW |
4355 | if (msg->front.iov_len < sizeof(*h)) |
4356 | goto bad; | |
4357 | h = msg->front.iov_base; | |
4358 | op = le32_to_cpu(h->op); | |
4359 | vino.ino = le64_to_cpu(h->ino); | |
4360 | vino.snap = CEPH_NOSNAP; | |
a8599bd8 | 4361 | seq = le32_to_cpu(h->seq); |
3d7ded4d | 4362 | mseq = le32_to_cpu(h->migrate_seq); |
a8599bd8 | 4363 | |
ce1fbc8d SW |
4364 | snaptrace = h + 1; |
4365 | snaptrace_len = le32_to_cpu(h->snap_trace_len); | |
fb01d1f8 | 4366 | p = snaptrace + snaptrace_len; |
ce1fbc8d | 4367 | |
4985d6f9 | 4368 | if (msg_version >= 2) { |
fb01d1f8 | 4369 | u32 flock_len; |
ce1fbc8d | 4370 | ceph_decode_32_safe(&p, end, flock_len, bad); |
4ee6a914 YZ |
4371 | if (p + flock_len > end) |
4372 | goto bad; | |
fb01d1f8 | 4373 | p += flock_len; |
ce1fbc8d SW |
4374 | } |
4375 | ||
4985d6f9 | 4376 | if (msg_version >= 3) { |
4ee6a914 | 4377 | if (op == CEPH_CAP_OP_IMPORT) { |
4ee6a914 YZ |
4378 | if (p + sizeof(*peer) > end) |
4379 | goto bad; | |
4380 | peer = p; | |
fb01d1f8 | 4381 | p += sizeof(*peer); |
11df2dfb YZ |
4382 | } else if (op == CEPH_CAP_OP_EXPORT) { |
4383 | /* recorded in unused fields */ | |
4384 | peer = (void *)&h->size; | |
4ee6a914 YZ |
4385 | } |
4386 | } | |
4387 | ||
4985d6f9 | 4388 | if (msg_version >= 4) { |
a1c6b835 YZ |
4389 | ceph_decode_64_safe(&p, end, extra_info.inline_version, bad); |
4390 | ceph_decode_32_safe(&p, end, extra_info.inline_len, bad); | |
4391 | if (p + extra_info.inline_len > end) | |
fb01d1f8 | 4392 | goto bad; |
a1c6b835 YZ |
4393 | extra_info.inline_data = p; |
4394 | p += extra_info.inline_len; | |
fb01d1f8 YZ |
4395 | } |
4396 | ||
4985d6f9 | 4397 | if (msg_version >= 5) { |
92475f05 JL |
4398 | struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; |
4399 | u32 epoch_barrier; | |
4400 | ||
4401 | ceph_decode_32_safe(&p, end, epoch_barrier, bad); | |
4402 | ceph_osdc_update_epoch_barrier(osdc, epoch_barrier); | |
4403 | } | |
4404 | ||
4985d6f9 | 4405 | if (msg_version >= 8) { |
779fe0fb | 4406 | u32 pool_ns_len; |
92475f05 | 4407 | |
5ea5c5e0 | 4408 | /* version >= 6 */ |
06a1ad43 | 4409 | ceph_decode_skip_64(&p, end, bad); // flush_tid |
5ea5c5e0 | 4410 | /* version >= 7 */ |
06a1ad43 JL |
4411 | ceph_decode_skip_32(&p, end, bad); // caller_uid |
4412 | ceph_decode_skip_32(&p, end, bad); // caller_gid | |
5ea5c5e0 YZ |
4413 | /* version >= 8 */ |
4414 | ceph_decode_32_safe(&p, end, pool_ns_len, bad); | |
779fe0fb YZ |
4415 | if (pool_ns_len > 0) { |
4416 | ceph_decode_need(&p, end, pool_ns_len, bad); | |
a1c6b835 YZ |
4417 | extra_info.pool_ns = |
4418 | ceph_find_or_create_string(p, pool_ns_len); | |
779fe0fb YZ |
4419 | p += pool_ns_len; |
4420 | } | |
5ea5c5e0 YZ |
4421 | } |
4422 | ||
ec62b894 | 4423 | if (msg_version >= 9) { |
4985d6f9 | 4424 | struct ceph_timespec *btime; |
4985d6f9 | 4425 | |
4985d6f9 YZ |
4426 | if (p + sizeof(*btime) > end) |
4427 | goto bad; | |
4428 | btime = p; | |
ec62b894 | 4429 | ceph_decode_timespec64(&extra_info.btime, btime); |
4985d6f9 | 4430 | p += sizeof(*btime); |
176c77c9 | 4431 | ceph_decode_64_safe(&p, end, extra_info.change_attr, bad); |
ec62b894 JL |
4432 | } |
4433 | ||
4434 | if (msg_version >= 11) { | |
4985d6f9 | 4435 | /* version >= 10 */ |
06a1ad43 | 4436 | ceph_decode_skip_32(&p, end, bad); // flags |
4985d6f9 YZ |
4437 | /* version >= 11 */ |
4438 | extra_info.dirstat_valid = true; | |
4439 | ceph_decode_64_safe(&p, end, extra_info.nfiles, bad); | |
4440 | ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad); | |
4441 | } | |
4442 | ||
0d91f0ad JL |
4443 | if (msg_version >= 12) { |
4444 | if (parse_fscrypt_fields(&p, end, &extra_info)) | |
4445 | goto bad; | |
4446 | } | |
4447 | ||
6cd3bcad | 4448 | /* lookup ino */ |
a1c6b835 | 4449 | inode = ceph_find_inode(mdsc->fsc->sb, vino); |
38d46409 XL |
4450 | doutc(cl, " op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), |
4451 | vino.ino, vino.snap, inode); | |
6cd3bcad | 4452 | |
a8599bd8 | 4453 | mutex_lock(&session->s_mutex); |
38d46409 XL |
4454 | doutc(cl, " mds%d seq %lld cap seq %u\n", session->s_mds, |
4455 | session->s_seq, (unsigned)seq); | |
a8599bd8 | 4456 | |
a8599bd8 | 4457 | if (!inode) { |
38d46409 | 4458 | doutc(cl, " i don't have ino %llx\n", vino.ino); |
3d7ded4d | 4459 | |
ce72d4e0 XL |
4460 | switch (op) { |
4461 | case CEPH_CAP_OP_IMPORT: | |
4462 | case CEPH_CAP_OP_REVOKE: | |
4463 | case CEPH_CAP_OP_GRANT: | |
4464 | do_cap_release = true; | |
4465 | break; | |
4466 | default: | |
4467 | break; | |
a096b09a | 4468 | } |
fb33c114 | 4469 | goto flush_cap_releases; |
a8599bd8 | 4470 | } |
1ad3bb28 | 4471 | ci = ceph_inode(inode); |
a8599bd8 SW |
4472 | |
4473 | /* these will work even if we don't have a cap yet */ | |
4474 | switch (op) { | |
4475 | case CEPH_CAP_OP_FLUSHSNAP_ACK: | |
a1c6b835 YZ |
4476 | handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid), |
4477 | h, session); | |
a8599bd8 SW |
4478 | goto done; |
4479 | ||
4480 | case CEPH_CAP_OP_EXPORT: | |
11df2dfb YZ |
4481 | handle_cap_export(inode, h, peer, session); |
4482 | goto done_unlocked; | |
a8599bd8 SW |
4483 | |
4484 | case CEPH_CAP_OP_IMPORT: | |
982d6011 YZ |
4485 | realm = NULL; |
4486 | if (snaptrace_len) { | |
4487 | down_write(&mdsc->snap_rwsem); | |
a68e564a XL |
4488 | if (ceph_update_snap_trace(mdsc, snaptrace, |
4489 | snaptrace + snaptrace_len, | |
4490 | false, &realm)) { | |
4491 | up_write(&mdsc->snap_rwsem); | |
4492 | close_sessions = true; | |
4493 | goto done; | |
4494 | } | |
982d6011 YZ |
4495 | downgrade_write(&mdsc->snap_rwsem); |
4496 | } else { | |
4497 | down_read(&mdsc->snap_rwsem); | |
4498 | } | |
78333233 | 4499 | spin_lock(&ci->i_ceph_lock); |
4ee6a914 | 4500 | handle_cap_import(mdsc, inode, h, peer, session, |
a1c6b835 YZ |
4501 | &cap, &extra_info.issued); |
4502 | handle_cap_grant(inode, session, cap, | |
4503 | h, msg->middle, &extra_info); | |
982d6011 YZ |
4504 | if (realm) |
4505 | ceph_put_snap_realm(mdsc, realm); | |
2cd698be | 4506 | goto done_unlocked; |
a8599bd8 SW |
4507 | } |
4508 | ||
4509 | /* the rest require a cap */ | |
be655596 | 4510 | spin_lock(&ci->i_ceph_lock); |
a1c6b835 | 4511 | cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds); |
a8599bd8 | 4512 | if (!cap) { |
38d46409 XL |
4513 | doutc(cl, " no cap on %p ino %llx.%llx from mds%d\n", |
4514 | inode, ceph_ino(inode), ceph_snap(inode), | |
4515 | session->s_mds); | |
be655596 | 4516 | spin_unlock(&ci->i_ceph_lock); |
ce72d4e0 XL |
4517 | switch (op) { |
4518 | case CEPH_CAP_OP_REVOKE: | |
4519 | case CEPH_CAP_OP_GRANT: | |
4520 | do_cap_release = true; | |
4521 | break; | |
4522 | default: | |
4523 | break; | |
4524 | } | |
21b559de | 4525 | goto flush_cap_releases; |
a8599bd8 SW |
4526 | } |
4527 | ||
be655596 | 4528 | /* note that each of these drops i_ceph_lock for us */ |
a8599bd8 SW |
4529 | switch (op) { |
4530 | case CEPH_CAP_OP_REVOKE: | |
4531 | case CEPH_CAP_OP_GRANT: | |
a1c6b835 YZ |
4532 | __ceph_caps_issued(ci, &extra_info.issued); |
4533 | extra_info.issued |= __ceph_caps_dirty(ci); | |
4534 | handle_cap_grant(inode, session, cap, | |
4535 | h, msg->middle, &extra_info); | |
15637c8b | 4536 | goto done_unlocked; |
a8599bd8 SW |
4537 | |
4538 | case CEPH_CAP_OP_FLUSH_ACK: | |
a1c6b835 YZ |
4539 | handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid), |
4540 | h, session, cap); | |
a8599bd8 SW |
4541 | break; |
4542 | ||
4543 | case CEPH_CAP_OP_TRUNC: | |
0d91f0ad JL |
4544 | queue_trunc = handle_cap_trunc(inode, h, session, |
4545 | &extra_info); | |
7391fba2 JL |
4546 | spin_unlock(&ci->i_ceph_lock); |
4547 | if (queue_trunc) | |
4548 | ceph_queue_vmtruncate(inode); | |
a8599bd8 SW |
4549 | break; |
4550 | ||
4551 | default: | |
be655596 | 4552 | spin_unlock(&ci->i_ceph_lock); |
38d46409 XL |
4553 | pr_err_client(cl, "unknown cap op %d %s\n", op, |
4554 | ceph_cap_op_name(op)); | |
a8599bd8 SW |
4555 | } |
4556 | ||
e3ec8d68 YZ |
4557 | done: |
4558 | mutex_unlock(&session->s_mutex); | |
4559 | done_unlocked: | |
23c2c76e | 4560 | iput(inode); |
2ad32cf0 | 4561 | out: |
e3dfcab2 XL |
4562 | ceph_dec_mds_stopping_blocker(mdsc); |
4563 | ||
2ad32cf0 | 4564 | ceph_put_string(extra_info.pool_ns); |
a68e564a XL |
4565 | |
4566 | /* Defer closing the sessions after s_mutex lock being released */ | |
4567 | if (close_sessions) | |
4568 | ceph_mdsc_close_sessions(mdsc); | |
4569 | ||
0d91f0ad | 4570 | kfree(extra_info.fscrypt_auth); |
e3ec8d68 | 4571 | return; |
21b559de GF |
4572 | |
4573 | flush_cap_releases: | |
4574 | /* | |
745a8e3b | 4575 | * send any cap release message to try to move things |
21b559de GF |
4576 | * along for the mds (who clearly thinks we still have this |
4577 | * cap). | |
4578 | */ | |
ce72d4e0 XL |
4579 | if (do_cap_release) { |
4580 | cap = ceph_get_cap(mdsc, NULL); | |
4581 | cap->cap_ino = vino.ino; | |
4582 | cap->queue_release = 1; | |
4583 | cap->cap_id = le64_to_cpu(h->cap_id); | |
4584 | cap->mseq = mseq; | |
4585 | cap->seq = seq; | |
4586 | cap->issue_seq = seq; | |
4587 | spin_lock(&session->s_cap_lock); | |
4588 | __ceph_queue_cap_release(session, cap); | |
4589 | spin_unlock(&session->s_cap_lock); | |
4590 | } | |
e3ec8d68 YZ |
4591 | ceph_flush_cap_releases(mdsc, session); |
4592 | goto done; | |
a8599bd8 SW |
4593 | |
4594 | bad: | |
38d46409 | 4595 | pr_err_client(cl, "corrupt message\n"); |
9ec7cab1 | 4596 | ceph_msg_dump(msg); |
2ad32cf0 | 4597 | goto out; |
a8599bd8 SW |
4598 | } |
4599 | ||
4600 | /* | |
4601 | * Delayed work handler to process end of delayed cap release LRU list. | |
bf2ba432 LH |
4602 | * |
4603 | * If new caps are added to the list while processing it, these won't get | |
4604 | * processed in this run. In this case, the ci->i_hold_caps_max will be | |
4605 | * returned so that the work can be scheduled accordingly. | |
a8599bd8 | 4606 | */ |
bf2ba432 | 4607 | unsigned long ceph_check_delayed_caps(struct ceph_mds_client *mdsc) |
a8599bd8 | 4608 | { |
38d46409 | 4609 | struct ceph_client *cl = mdsc->fsc->client; |
4b9f2042 | 4610 | struct inode *inode; |
a8599bd8 | 4611 | struct ceph_inode_info *ci; |
bf2ba432 LH |
4612 | struct ceph_mount_options *opt = mdsc->fsc->mount_options; |
4613 | unsigned long delay_max = opt->caps_wanted_delay_max * HZ; | |
4614 | unsigned long loop_start = jiffies; | |
4615 | unsigned long delay = 0; | |
a8599bd8 | 4616 | |
38d46409 | 4617 | doutc(cl, "begin\n"); |
585d72f3 JL |
4618 | spin_lock(&mdsc->cap_delay_lock); |
4619 | while (!list_empty(&mdsc->cap_delay_list)) { | |
a8599bd8 SW |
4620 | ci = list_first_entry(&mdsc->cap_delay_list, |
4621 | struct ceph_inode_info, | |
4622 | i_cap_delay_list); | |
bf2ba432 | 4623 | if (time_before(loop_start, ci->i_hold_caps_max - delay_max)) { |
38d46409 | 4624 | doutc(cl, "caps added recently. Exiting loop"); |
bf2ba432 LH |
4625 | delay = ci->i_hold_caps_max; |
4626 | break; | |
4627 | } | |
a8599bd8 SW |
4628 | if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && |
4629 | time_before(jiffies, ci->i_hold_caps_max)) | |
4630 | break; | |
4631 | list_del_init(&ci->i_cap_delay_list); | |
4b9f2042 | 4632 | |
874c8ca1 | 4633 | inode = igrab(&ci->netfs.inode); |
4b9f2042 | 4634 | if (inode) { |
585d72f3 | 4635 | spin_unlock(&mdsc->cap_delay_lock); |
38d46409 XL |
4636 | doutc(cl, "on %p %llx.%llx\n", inode, |
4637 | ceph_vinop(inode)); | |
e4b731cc | 4638 | ceph_check_caps(ci, 0); |
23c2c76e | 4639 | iput(inode); |
585d72f3 | 4640 | spin_lock(&mdsc->cap_delay_lock); |
4b9f2042 | 4641 | } |
a8599bd8 SW |
4642 | } |
4643 | spin_unlock(&mdsc->cap_delay_lock); | |
38d46409 | 4644 | doutc(cl, "done\n"); |
bf2ba432 LH |
4645 | |
4646 | return delay; | |
a8599bd8 SW |
4647 | } |
4648 | ||
afcdaea3 SW |
4649 | /* |
4650 | * Flush all dirty caps to the mds | |
4651 | */ | |
1cf03a68 | 4652 | static void flush_dirty_session_caps(struct ceph_mds_session *s) |
afcdaea3 | 4653 | { |
1cf03a68 | 4654 | struct ceph_mds_client *mdsc = s->s_mdsc; |
38d46409 | 4655 | struct ceph_client *cl = mdsc->fsc->client; |
db354052 SW |
4656 | struct ceph_inode_info *ci; |
4657 | struct inode *inode; | |
afcdaea3 | 4658 | |
38d46409 | 4659 | doutc(cl, "begin\n"); |
afcdaea3 | 4660 | spin_lock(&mdsc->cap_dirty_lock); |
1cf03a68 JL |
4661 | while (!list_empty(&s->s_cap_dirty)) { |
4662 | ci = list_first_entry(&s->s_cap_dirty, struct ceph_inode_info, | |
db354052 | 4663 | i_dirty_item); |
874c8ca1 | 4664 | inode = &ci->netfs.inode; |
70b666c3 | 4665 | ihold(inode); |
38d46409 | 4666 | doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode)); |
afcdaea3 | 4667 | spin_unlock(&mdsc->cap_dirty_lock); |
8692969e | 4668 | ceph_wait_on_async_create(inode); |
e4b731cc | 4669 | ceph_check_caps(ci, CHECK_CAPS_FLUSH); |
70b666c3 | 4670 | iput(inode); |
afcdaea3 SW |
4671 | spin_lock(&mdsc->cap_dirty_lock); |
4672 | } | |
4673 | spin_unlock(&mdsc->cap_dirty_lock); | |
38d46409 | 4674 | doutc(cl, "done\n"); |
afcdaea3 SW |
4675 | } |
4676 | ||
1cf03a68 JL |
4677 | void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) |
4678 | { | |
59b312f3 | 4679 | ceph_mdsc_iterate_sessions(mdsc, flush_dirty_session_caps, true); |
1cf03a68 JL |
4680 | } |
4681 | ||
719a2514 YZ |
4682 | void __ceph_touch_fmode(struct ceph_inode_info *ci, |
4683 | struct ceph_mds_client *mdsc, int fmode) | |
4684 | { | |
4685 | unsigned long now = jiffies; | |
4686 | if (fmode & CEPH_FILE_MODE_RD) | |
4687 | ci->i_last_rd = now; | |
4688 | if (fmode & CEPH_FILE_MODE_WR) | |
4689 | ci->i_last_wr = now; | |
4690 | /* queue periodic check */ | |
4691 | if (fmode && | |
4692 | __ceph_is_any_real_caps(ci) && | |
4693 | list_empty(&ci->i_cap_delay_list)) | |
a0d93e32 | 4694 | __cap_delay_requeue(mdsc, ci); |
719a2514 YZ |
4695 | } |
4696 | ||
4697 | void ceph_get_fmode(struct ceph_inode_info *ci, int fmode, int count) | |
4698 | { | |
874c8ca1 | 4699 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb); |
719a2514 | 4700 | int bits = (fmode << 1) | 1; |
973e5245 | 4701 | bool already_opened = false; |
1dd8d470 XL |
4702 | int i; |
4703 | ||
4704 | if (count == 1) | |
4705 | atomic64_inc(&mdsc->metric.opened_files); | |
4706 | ||
719a2514 YZ |
4707 | spin_lock(&ci->i_ceph_lock); |
4708 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { | |
1dd8d470 | 4709 | /* |
973e5245 | 4710 | * If any of the mode ref is larger than 0, |
1dd8d470 XL |
4711 | * that means it has been already opened by |
4712 | * others. Just skip checking the PIN ref. | |
4713 | */ | |
973e5245 WH |
4714 | if (i && ci->i_nr_by_mode[i]) |
4715 | already_opened = true; | |
4716 | ||
4717 | if (bits & (1 << i)) | |
4718 | ci->i_nr_by_mode[i] += count; | |
719a2514 | 4719 | } |
1dd8d470 | 4720 | |
973e5245 | 4721 | if (!already_opened) |
1dd8d470 | 4722 | percpu_counter_inc(&mdsc->metric.opened_inodes); |
719a2514 YZ |
4723 | spin_unlock(&ci->i_ceph_lock); |
4724 | } | |
4725 | ||
a8599bd8 SW |
4726 | /* |
4727 | * Drop open file reference. If we were the last open file, | |
4728 | * we may need to release capabilities to the MDS (or schedule | |
4729 | * their delayed release). | |
4730 | */ | |
719a2514 | 4731 | void ceph_put_fmode(struct ceph_inode_info *ci, int fmode, int count) |
a8599bd8 | 4732 | { |
874c8ca1 | 4733 | struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb); |
774a6a11 | 4734 | int bits = (fmode << 1) | 1; |
1dd8d470 XL |
4735 | bool is_closed = true; |
4736 | int i; | |
4737 | ||
4738 | if (count == 1) | |
4739 | atomic64_dec(&mdsc->metric.opened_files); | |
4740 | ||
be655596 | 4741 | spin_lock(&ci->i_ceph_lock); |
774a6a11 YZ |
4742 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { |
4743 | if (bits & (1 << i)) { | |
719a2514 YZ |
4744 | BUG_ON(ci->i_nr_by_mode[i] < count); |
4745 | ci->i_nr_by_mode[i] -= count; | |
774a6a11 | 4746 | } |
1dd8d470 XL |
4747 | |
4748 | /* | |
4749 | * If any of the mode ref is not 0 after | |
4750 | * decreased, that means it is still opened | |
4751 | * by others. Just skip checking the PIN ref. | |
4752 | */ | |
4753 | if (i && ci->i_nr_by_mode[i]) | |
4754 | is_closed = false; | |
774a6a11 | 4755 | } |
1dd8d470 XL |
4756 | |
4757 | if (is_closed) | |
4758 | percpu_counter_dec(&mdsc->metric.opened_inodes); | |
be655596 | 4759 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
4760 | } |
4761 | ||
6ef0bc6d | 4762 | /* |
a452bc06 | 4763 | * For a soon-to-be unlinked file, drop the LINK caps. If it |
6ef0bc6d ZZ |
4764 | * looks like the link count will hit 0, drop any other caps (other |
4765 | * than PIN) we don't specifically want (due to the file still being | |
4766 | * open). | |
4767 | */ | |
4768 | int ceph_drop_caps_for_unlink(struct inode *inode) | |
4769 | { | |
4770 | struct ceph_inode_info *ci = ceph_inode(inode); | |
4771 | int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; | |
4772 | ||
4773 | spin_lock(&ci->i_ceph_lock); | |
4774 | if (inode->i_nlink == 1) { | |
4775 | drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN); | |
4776 | ||
6ef0bc6d ZZ |
4777 | if (__ceph_caps_dirty(ci)) { |
4778 | struct ceph_mds_client *mdsc = | |
5995d90d | 4779 | ceph_inode_to_fs_client(inode)->mdsc; |
6ef0bc6d ZZ |
4780 | __cap_delay_requeue_front(mdsc, ci); |
4781 | } | |
4782 | } | |
4783 | spin_unlock(&ci->i_ceph_lock); | |
4784 | return drop; | |
4785 | } | |
4786 | ||
a8599bd8 SW |
4787 | /* |
4788 | * Helpers for embedding cap and dentry lease releases into mds | |
4789 | * requests. | |
4790 | * | |
4791 | * @force is used by dentry_release (below) to force inclusion of a | |
4792 | * record for the directory inode, even when there aren't any caps to | |
4793 | * drop. | |
4794 | */ | |
4795 | int ceph_encode_inode_release(void **p, struct inode *inode, | |
4796 | int mds, int drop, int unless, int force) | |
4797 | { | |
4798 | struct ceph_inode_info *ci = ceph_inode(inode); | |
38d46409 | 4799 | struct ceph_client *cl = ceph_inode_to_client(inode); |
a8599bd8 SW |
4800 | struct ceph_cap *cap; |
4801 | struct ceph_mds_request_release *rel = *p; | |
ec97f88b | 4802 | int used, dirty; |
a8599bd8 | 4803 | int ret = 0; |
a8599bd8 | 4804 | |
be655596 | 4805 | spin_lock(&ci->i_ceph_lock); |
916623da | 4806 | used = __ceph_caps_used(ci); |
ec97f88b | 4807 | dirty = __ceph_caps_dirty(ci); |
916623da | 4808 | |
38d46409 XL |
4809 | doutc(cl, "%p %llx.%llx mds%d used|dirty %s drop %s unless %s\n", |
4810 | inode, ceph_vinop(inode), mds, ceph_cap_string(used|dirty), | |
4811 | ceph_cap_string(drop), ceph_cap_string(unless)); | |
916623da | 4812 | |
ec97f88b SW |
4813 | /* only drop unused, clean caps */ |
4814 | drop &= ~(used | dirty); | |
916623da | 4815 | |
a8599bd8 SW |
4816 | cap = __get_cap_for_mds(ci, mds); |
4817 | if (cap && __cap_is_valid(cap)) { | |
222b7f90 YZ |
4818 | unless &= cap->issued; |
4819 | if (unless) { | |
4820 | if (unless & CEPH_CAP_AUTH_EXCL) | |
4821 | drop &= ~CEPH_CAP_AUTH_SHARED; | |
4822 | if (unless & CEPH_CAP_LINK_EXCL) | |
4823 | drop &= ~CEPH_CAP_LINK_SHARED; | |
4824 | if (unless & CEPH_CAP_XATTR_EXCL) | |
4825 | drop &= ~CEPH_CAP_XATTR_SHARED; | |
4826 | if (unless & CEPH_CAP_FILE_EXCL) | |
4827 | drop &= ~CEPH_CAP_FILE_SHARED; | |
4828 | } | |
4829 | ||
4830 | if (force || (cap->issued & drop)) { | |
4831 | if (cap->issued & drop) { | |
bb137f84 | 4832 | int wanted = __ceph_caps_wanted(ci); |
38d46409 XL |
4833 | doutc(cl, "%p %llx.%llx cap %p %s -> %s, " |
4834 | "wanted %s -> %s\n", inode, | |
4835 | ceph_vinop(inode), cap, | |
4836 | ceph_cap_string(cap->issued), | |
4837 | ceph_cap_string(cap->issued & ~drop), | |
4838 | ceph_cap_string(cap->mds_wanted), | |
4839 | ceph_cap_string(wanted)); | |
bb137f84 | 4840 | |
a8599bd8 SW |
4841 | cap->issued &= ~drop; |
4842 | cap->implemented &= ~drop; | |
bb137f84 | 4843 | cap->mds_wanted = wanted; |
6f05b30e YZ |
4844 | if (cap == ci->i_auth_cap && |
4845 | !(wanted & CEPH_CAP_ANY_FILE_WR)) | |
4846 | ci->i_requested_max_size = 0; | |
a8599bd8 | 4847 | } else { |
38d46409 XL |
4848 | doutc(cl, "%p %llx.%llx cap %p %s (force)\n", |
4849 | inode, ceph_vinop(inode), cap, | |
4850 | ceph_cap_string(cap->issued)); | |
a8599bd8 SW |
4851 | } |
4852 | ||
4853 | rel->ino = cpu_to_le64(ceph_ino(inode)); | |
4854 | rel->cap_id = cpu_to_le64(cap->cap_id); | |
4855 | rel->seq = cpu_to_le32(cap->seq); | |
08a0f24e | 4856 | rel->issue_seq = cpu_to_le32(cap->issue_seq); |
a8599bd8 | 4857 | rel->mseq = cpu_to_le32(cap->mseq); |
fd7b95cd | 4858 | rel->caps = cpu_to_le32(cap->implemented); |
a8599bd8 SW |
4859 | rel->wanted = cpu_to_le32(cap->mds_wanted); |
4860 | rel->dname_len = 0; | |
4861 | rel->dname_seq = 0; | |
4862 | *p += sizeof(*rel); | |
4863 | ret = 1; | |
4864 | } else { | |
38d46409 XL |
4865 | doutc(cl, "%p %llx.%llx cap %p %s (noop)\n", |
4866 | inode, ceph_vinop(inode), cap, | |
4867 | ceph_cap_string(cap->issued)); | |
a8599bd8 SW |
4868 | } |
4869 | } | |
be655596 | 4870 | spin_unlock(&ci->i_ceph_lock); |
a8599bd8 SW |
4871 | return ret; |
4872 | } | |
4873 | ||
3fd945a7 JL |
4874 | /** |
4875 | * ceph_encode_dentry_release - encode a dentry release into an outgoing request | |
4876 | * @p: outgoing request buffer | |
4877 | * @dentry: dentry to release | |
4878 | * @dir: dir to release it from | |
4879 | * @mds: mds that we're speaking to | |
4880 | * @drop: caps being dropped | |
4881 | * @unless: unless we have these caps | |
4882 | * | |
4883 | * Encode a dentry release into an outgoing request buffer. Returns 1 if the | |
4884 | * thing was released, or a negative error code otherwise. | |
4885 | */ | |
a8599bd8 | 4886 | int ceph_encode_dentry_release(void **p, struct dentry *dentry, |
ca6c8ae0 | 4887 | struct inode *dir, |
a8599bd8 SW |
4888 | int mds, int drop, int unless) |
4889 | { | |
ca6c8ae0 | 4890 | struct dentry *parent = NULL; |
a8599bd8 SW |
4891 | struct ceph_mds_request_release *rel = *p; |
4892 | struct ceph_dentry_info *di = ceph_dentry(dentry); | |
38d46409 | 4893 | struct ceph_client *cl; |
a8599bd8 SW |
4894 | int force = 0; |
4895 | int ret; | |
4896 | ||
4897 | /* | |
4898 | * force an record for the directory caps if we have a dentry lease. | |
be655596 | 4899 | * this is racy (can't take i_ceph_lock and d_lock together), but it |
a8599bd8 SW |
4900 | * doesn't have to be perfect; the mds will revoke anything we don't |
4901 | * release. | |
4902 | */ | |
4903 | spin_lock(&dentry->d_lock); | |
4904 | if (di->lease_session && di->lease_session->s_mds == mds) | |
4905 | force = 1; | |
ca6c8ae0 JL |
4906 | if (!dir) { |
4907 | parent = dget(dentry->d_parent); | |
4908 | dir = d_inode(parent); | |
4909 | } | |
a8599bd8 SW |
4910 | spin_unlock(&dentry->d_lock); |
4911 | ||
ca6c8ae0 | 4912 | ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); |
adf0d687 | 4913 | dput(parent); |
a8599bd8 | 4914 | |
38d46409 | 4915 | cl = ceph_inode_to_client(dir); |
a8599bd8 SW |
4916 | spin_lock(&dentry->d_lock); |
4917 | if (ret && di->lease_session && di->lease_session->s_mds == mds) { | |
38d46409 XL |
4918 | doutc(cl, "%p mds%d seq %d\n", dentry, mds, |
4919 | (int)di->lease_seq); | |
a8599bd8 | 4920 | rel->dname_seq = cpu_to_le32(di->lease_seq); |
1dadcce3 | 4921 | __ceph_mdsc_drop_dentry_lease(dentry); |
3fd945a7 JL |
4922 | spin_unlock(&dentry->d_lock); |
4923 | if (IS_ENCRYPTED(dir) && fscrypt_has_encryption_key(dir)) { | |
4924 | int ret2 = ceph_encode_encrypted_fname(dir, dentry, *p); | |
4925 | ||
4926 | if (ret2 < 0) | |
4927 | return ret2; | |
4928 | ||
4929 | rel->dname_len = cpu_to_le32(ret2); | |
4930 | *p += ret2; | |
4931 | } else { | |
4932 | rel->dname_len = cpu_to_le32(dentry->d_name.len); | |
4933 | memcpy(*p, dentry->d_name.name, dentry->d_name.len); | |
4934 | *p += dentry->d_name.len; | |
4935 | } | |
4936 | } else { | |
4937 | spin_unlock(&dentry->d_lock); | |
a8599bd8 | 4938 | } |
a8599bd8 SW |
4939 | return ret; |
4940 | } | |
36e6da98 JL |
4941 | |
4942 | static int remove_capsnaps(struct ceph_mds_client *mdsc, struct inode *inode) | |
4943 | { | |
4944 | struct ceph_inode_info *ci = ceph_inode(inode); | |
38d46409 | 4945 | struct ceph_client *cl = mdsc->fsc->client; |
36e6da98 JL |
4946 | struct ceph_cap_snap *capsnap; |
4947 | int capsnap_release = 0; | |
4948 | ||
4949 | lockdep_assert_held(&ci->i_ceph_lock); | |
4950 | ||
38d46409 XL |
4951 | doutc(cl, "removing capsnaps, ci is %p, %p %llx.%llx\n", |
4952 | ci, inode, ceph_vinop(inode)); | |
36e6da98 JL |
4953 | |
4954 | while (!list_empty(&ci->i_cap_snaps)) { | |
4955 | capsnap = list_first_entry(&ci->i_cap_snaps, | |
4956 | struct ceph_cap_snap, ci_item); | |
4957 | __ceph_remove_capsnap(inode, capsnap, NULL, NULL); | |
4958 | ceph_put_snap_context(capsnap->context); | |
4959 | ceph_put_cap_snap(capsnap); | |
4960 | capsnap_release++; | |
4961 | } | |
4962 | wake_up_all(&ci->i_cap_wq); | |
4963 | wake_up_all(&mdsc->cap_flushing_wq); | |
4964 | return capsnap_release; | |
4965 | } | |
4966 | ||
4967 | int ceph_purge_inode_cap(struct inode *inode, struct ceph_cap *cap, bool *invalidate) | |
4968 | { | |
5995d90d | 4969 | struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode); |
36e6da98 | 4970 | struct ceph_mds_client *mdsc = fsc->mdsc; |
38d46409 | 4971 | struct ceph_client *cl = fsc->client; |
36e6da98 JL |
4972 | struct ceph_inode_info *ci = ceph_inode(inode); |
4973 | bool is_auth; | |
4974 | bool dirty_dropped = false; | |
4975 | int iputs = 0; | |
4976 | ||
4977 | lockdep_assert_held(&ci->i_ceph_lock); | |
4978 | ||
38d46409 XL |
4979 | doutc(cl, "removing cap %p, ci is %p, %p %llx.%llx\n", |
4980 | cap, ci, inode, ceph_vinop(inode)); | |
36e6da98 JL |
4981 | |
4982 | is_auth = (cap == ci->i_auth_cap); | |
4983 | __ceph_remove_cap(cap, false); | |
4984 | if (is_auth) { | |
4985 | struct ceph_cap_flush *cf; | |
4986 | ||
5d6451b1 | 4987 | if (ceph_inode_is_shutdown(inode)) { |
36e6da98 JL |
4988 | if (inode->i_data.nrpages > 0) |
4989 | *invalidate = true; | |
4990 | if (ci->i_wrbuffer_ref > 0) | |
4991 | mapping_set_error(&inode->i_data, -EIO); | |
4992 | } | |
4993 | ||
4994 | spin_lock(&mdsc->cap_dirty_lock); | |
4995 | ||
4996 | /* trash all of the cap flushes for this inode */ | |
4997 | while (!list_empty(&ci->i_cap_flush_list)) { | |
4998 | cf = list_first_entry(&ci->i_cap_flush_list, | |
4999 | struct ceph_cap_flush, i_list); | |
5000 | list_del_init(&cf->g_list); | |
5001 | list_del_init(&cf->i_list); | |
5002 | if (!cf->is_capsnap) | |
5003 | ceph_free_cap_flush(cf); | |
5004 | } | |
5005 | ||
5006 | if (!list_empty(&ci->i_dirty_item)) { | |
38d46409 XL |
5007 | pr_warn_ratelimited_client(cl, |
5008 | " dropping dirty %s state for %p %llx.%llx\n", | |
36e6da98 | 5009 | ceph_cap_string(ci->i_dirty_caps), |
38d46409 | 5010 | inode, ceph_vinop(inode)); |
36e6da98 JL |
5011 | ci->i_dirty_caps = 0; |
5012 | list_del_init(&ci->i_dirty_item); | |
5013 | dirty_dropped = true; | |
5014 | } | |
5015 | if (!list_empty(&ci->i_flushing_item)) { | |
38d46409 XL |
5016 | pr_warn_ratelimited_client(cl, |
5017 | " dropping dirty+flushing %s state for %p %llx.%llx\n", | |
36e6da98 | 5018 | ceph_cap_string(ci->i_flushing_caps), |
38d46409 | 5019 | inode, ceph_vinop(inode)); |
36e6da98 JL |
5020 | ci->i_flushing_caps = 0; |
5021 | list_del_init(&ci->i_flushing_item); | |
5022 | mdsc->num_cap_flushing--; | |
5023 | dirty_dropped = true; | |
5024 | } | |
5025 | spin_unlock(&mdsc->cap_dirty_lock); | |
5026 | ||
5027 | if (dirty_dropped) { | |
5028 | mapping_set_error(inode->i_mapping, -EIO); | |
5029 | ||
5030 | if (ci->i_wrbuffer_ref_head == 0 && | |
5031 | ci->i_wr_ref == 0 && | |
5032 | ci->i_dirty_caps == 0 && | |
5033 | ci->i_flushing_caps == 0) { | |
5034 | ceph_put_snap_context(ci->i_head_snapc); | |
5035 | ci->i_head_snapc = NULL; | |
5036 | } | |
5037 | } | |
5038 | ||
5039 | if (atomic_read(&ci->i_filelock_ref) > 0) { | |
5040 | /* make further file lock syscall return -EIO */ | |
5041 | ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK; | |
38d46409 XL |
5042 | pr_warn_ratelimited_client(cl, |
5043 | " dropping file locks for %p %llx.%llx\n", | |
5044 | inode, ceph_vinop(inode)); | |
36e6da98 JL |
5045 | } |
5046 | ||
5047 | if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) { | |
5048 | cf = ci->i_prealloc_cap_flush; | |
5049 | ci->i_prealloc_cap_flush = NULL; | |
5050 | if (!cf->is_capsnap) | |
5051 | ceph_free_cap_flush(cf); | |
5052 | } | |
5053 | ||
5054 | if (!list_empty(&ci->i_cap_snaps)) | |
5055 | iputs = remove_capsnaps(mdsc, inode); | |
5056 | } | |
5057 | if (dirty_dropped) | |
5058 | ++iputs; | |
5059 | return iputs; | |
5060 | } |