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