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