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