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