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