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