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