Commit | Line | Data |
---|---|---|
a8599bd8 SW |
1 | #include "ceph_debug.h" |
2 | ||
3 | #include <linux/fs.h> | |
4 | #include <linux/kernel.h> | |
5 | #include <linux/sched.h> | |
5a0e3ad6 | 6 | #include <linux/slab.h> |
a8599bd8 SW |
7 | #include <linux/vmalloc.h> |
8 | #include <linux/wait.h> | |
f1a3d572 | 9 | #include <linux/writeback.h> |
a8599bd8 SW |
10 | |
11 | #include "super.h" | |
12 | #include "decode.h" | |
13 | #include "messenger.h" | |
14 | ||
15 | /* | |
16 | * Capability management | |
17 | * | |
18 | * The Ceph metadata servers control client access to inode metadata | |
19 | * and file data by issuing capabilities, granting clients permission | |
20 | * to read and/or write both inode field and file data to OSDs | |
21 | * (storage nodes). Each capability consists of a set of bits | |
22 | * indicating which operations are allowed. | |
23 | * | |
24 | * If the client holds a *_SHARED cap, the client has a coherent value | |
25 | * that can be safely read from the cached inode. | |
26 | * | |
27 | * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the | |
28 | * client is allowed to change inode attributes (e.g., file size, | |
29 | * mtime), note its dirty state in the ceph_cap, and asynchronously | |
30 | * flush that metadata change to the MDS. | |
31 | * | |
32 | * In the event of a conflicting operation (perhaps by another | |
33 | * client), the MDS will revoke the conflicting client capabilities. | |
34 | * | |
35 | * In order for a client to cache an inode, it must hold a capability | |
36 | * with at least one MDS server. When inodes are released, release | |
37 | * notifications are batched and periodically sent en masse to the MDS | |
38 | * cluster to release server state. | |
39 | */ | |
40 | ||
41 | ||
42 | /* | |
43 | * Generate readable cap strings for debugging output. | |
44 | */ | |
45 | #define MAX_CAP_STR 20 | |
46 | static char cap_str[MAX_CAP_STR][40]; | |
47 | static DEFINE_SPINLOCK(cap_str_lock); | |
48 | static int last_cap_str; | |
49 | ||
50 | static char *gcap_string(char *s, int c) | |
51 | { | |
52 | if (c & CEPH_CAP_GSHARED) | |
53 | *s++ = 's'; | |
54 | if (c & CEPH_CAP_GEXCL) | |
55 | *s++ = 'x'; | |
56 | if (c & CEPH_CAP_GCACHE) | |
57 | *s++ = 'c'; | |
58 | if (c & CEPH_CAP_GRD) | |
59 | *s++ = 'r'; | |
60 | if (c & CEPH_CAP_GWR) | |
61 | *s++ = 'w'; | |
62 | if (c & CEPH_CAP_GBUFFER) | |
63 | *s++ = 'b'; | |
64 | if (c & CEPH_CAP_GLAZYIO) | |
65 | *s++ = 'l'; | |
66 | return s; | |
67 | } | |
68 | ||
69 | const char *ceph_cap_string(int caps) | |
70 | { | |
71 | int i; | |
72 | char *s; | |
73 | int c; | |
74 | ||
75 | spin_lock(&cap_str_lock); | |
76 | i = last_cap_str++; | |
77 | if (last_cap_str == MAX_CAP_STR) | |
78 | last_cap_str = 0; | |
79 | spin_unlock(&cap_str_lock); | |
80 | ||
81 | s = cap_str[i]; | |
82 | ||
83 | if (caps & CEPH_CAP_PIN) | |
84 | *s++ = 'p'; | |
85 | ||
86 | c = (caps >> CEPH_CAP_SAUTH) & 3; | |
87 | if (c) { | |
88 | *s++ = 'A'; | |
89 | s = gcap_string(s, c); | |
90 | } | |
91 | ||
92 | c = (caps >> CEPH_CAP_SLINK) & 3; | |
93 | if (c) { | |
94 | *s++ = 'L'; | |
95 | s = gcap_string(s, c); | |
96 | } | |
97 | ||
98 | c = (caps >> CEPH_CAP_SXATTR) & 3; | |
99 | if (c) { | |
100 | *s++ = 'X'; | |
101 | s = gcap_string(s, c); | |
102 | } | |
103 | ||
104 | c = caps >> CEPH_CAP_SFILE; | |
105 | if (c) { | |
106 | *s++ = 'F'; | |
107 | s = gcap_string(s, c); | |
108 | } | |
109 | ||
110 | if (s == cap_str[i]) | |
111 | *s++ = '-'; | |
112 | *s = 0; | |
113 | return cap_str[i]; | |
114 | } | |
115 | ||
37151668 | 116 | void ceph_caps_init(struct ceph_mds_client *mdsc) |
a8599bd8 | 117 | { |
37151668 YS |
118 | INIT_LIST_HEAD(&mdsc->caps_list); |
119 | spin_lock_init(&mdsc->caps_list_lock); | |
a8599bd8 SW |
120 | } |
121 | ||
37151668 | 122 | void ceph_caps_finalize(struct ceph_mds_client *mdsc) |
a8599bd8 SW |
123 | { |
124 | struct ceph_cap *cap; | |
125 | ||
37151668 YS |
126 | spin_lock(&mdsc->caps_list_lock); |
127 | while (!list_empty(&mdsc->caps_list)) { | |
128 | cap = list_first_entry(&mdsc->caps_list, | |
129 | struct ceph_cap, caps_item); | |
a8599bd8 SW |
130 | list_del(&cap->caps_item); |
131 | kmem_cache_free(ceph_cap_cachep, cap); | |
132 | } | |
37151668 YS |
133 | mdsc->caps_total_count = 0; |
134 | mdsc->caps_avail_count = 0; | |
135 | mdsc->caps_use_count = 0; | |
136 | mdsc->caps_reserve_count = 0; | |
137 | mdsc->caps_min_count = 0; | |
138 | spin_unlock(&mdsc->caps_list_lock); | |
85ccce43 SW |
139 | } |
140 | ||
37151668 | 141 | void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta) |
85ccce43 | 142 | { |
37151668 YS |
143 | spin_lock(&mdsc->caps_list_lock); |
144 | mdsc->caps_min_count += delta; | |
145 | BUG_ON(mdsc->caps_min_count < 0); | |
146 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
147 | } |
148 | ||
37151668 YS |
149 | int ceph_reserve_caps(struct ceph_mds_client *mdsc, |
150 | struct ceph_cap_reservation *ctx, int need) | |
a8599bd8 SW |
151 | { |
152 | int i; | |
153 | struct ceph_cap *cap; | |
154 | int have; | |
155 | int alloc = 0; | |
156 | LIST_HEAD(newcaps); | |
157 | int ret = 0; | |
158 | ||
159 | dout("reserve caps ctx=%p need=%d\n", ctx, need); | |
160 | ||
161 | /* first reserve any caps that are already allocated */ | |
37151668 YS |
162 | spin_lock(&mdsc->caps_list_lock); |
163 | if (mdsc->caps_avail_count >= need) | |
a8599bd8 SW |
164 | have = need; |
165 | else | |
37151668 YS |
166 | have = mdsc->caps_avail_count; |
167 | mdsc->caps_avail_count -= have; | |
168 | mdsc->caps_reserve_count += have; | |
169 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + | |
170 | mdsc->caps_reserve_count + | |
171 | mdsc->caps_avail_count); | |
172 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
173 | |
174 | for (i = have; i < need; i++) { | |
175 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); | |
176 | if (!cap) { | |
177 | ret = -ENOMEM; | |
178 | goto out_alloc_count; | |
179 | } | |
180 | list_add(&cap->caps_item, &newcaps); | |
181 | alloc++; | |
182 | } | |
183 | BUG_ON(have + alloc != need); | |
184 | ||
37151668 YS |
185 | spin_lock(&mdsc->caps_list_lock); |
186 | mdsc->caps_total_count += alloc; | |
187 | mdsc->caps_reserve_count += alloc; | |
188 | list_splice(&newcaps, &mdsc->caps_list); | |
a8599bd8 | 189 | |
37151668 YS |
190 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
191 | mdsc->caps_reserve_count + | |
192 | mdsc->caps_avail_count); | |
193 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
194 | |
195 | ctx->count = need; | |
196 | dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n", | |
37151668 YS |
197 | ctx, mdsc->caps_total_count, mdsc->caps_use_count, |
198 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
a8599bd8 SW |
199 | return 0; |
200 | ||
201 | out_alloc_count: | |
202 | /* we didn't manage to reserve as much as we needed */ | |
203 | pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n", | |
204 | ctx, need, have); | |
205 | return ret; | |
206 | } | |
207 | ||
37151668 YS |
208 | int ceph_unreserve_caps(struct ceph_mds_client *mdsc, |
209 | struct ceph_cap_reservation *ctx) | |
a8599bd8 SW |
210 | { |
211 | dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); | |
212 | if (ctx->count) { | |
37151668 YS |
213 | spin_lock(&mdsc->caps_list_lock); |
214 | BUG_ON(mdsc->caps_reserve_count < ctx->count); | |
215 | mdsc->caps_reserve_count -= ctx->count; | |
216 | mdsc->caps_avail_count += ctx->count; | |
a8599bd8 SW |
217 | ctx->count = 0; |
218 | dout("unreserve caps %d = %d used + %d resv + %d avail\n", | |
37151668 YS |
219 | mdsc->caps_total_count, mdsc->caps_use_count, |
220 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
221 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + | |
222 | mdsc->caps_reserve_count + | |
223 | mdsc->caps_avail_count); | |
224 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
225 | } |
226 | return 0; | |
227 | } | |
228 | ||
37151668 YS |
229 | static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc, |
230 | struct ceph_cap_reservation *ctx) | |
a8599bd8 SW |
231 | { |
232 | struct ceph_cap *cap = NULL; | |
233 | ||
234 | /* temporary, until we do something about cap import/export */ | |
443b3760 SW |
235 | if (!ctx) { |
236 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); | |
237 | if (cap) { | |
37151668 YS |
238 | mdsc->caps_use_count++; |
239 | mdsc->caps_total_count++; | |
443b3760 SW |
240 | } |
241 | return cap; | |
242 | } | |
a8599bd8 | 243 | |
37151668 | 244 | spin_lock(&mdsc->caps_list_lock); |
a8599bd8 | 245 | dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n", |
37151668 YS |
246 | ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, |
247 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
a8599bd8 | 248 | BUG_ON(!ctx->count); |
37151668 YS |
249 | BUG_ON(ctx->count > mdsc->caps_reserve_count); |
250 | BUG_ON(list_empty(&mdsc->caps_list)); | |
a8599bd8 SW |
251 | |
252 | ctx->count--; | |
37151668 YS |
253 | mdsc->caps_reserve_count--; |
254 | mdsc->caps_use_count++; | |
a8599bd8 | 255 | |
37151668 | 256 | cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); |
a8599bd8 SW |
257 | list_del(&cap->caps_item); |
258 | ||
37151668 YS |
259 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
260 | mdsc->caps_reserve_count + mdsc->caps_avail_count); | |
261 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
262 | return cap; |
263 | } | |
264 | ||
37151668 | 265 | void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap) |
a8599bd8 | 266 | { |
37151668 | 267 | spin_lock(&mdsc->caps_list_lock); |
7c1332b8 | 268 | dout("put_cap %p %d = %d used + %d resv + %d avail\n", |
37151668 YS |
269 | cap, mdsc->caps_total_count, mdsc->caps_use_count, |
270 | mdsc->caps_reserve_count, mdsc->caps_avail_count); | |
271 | mdsc->caps_use_count--; | |
a8599bd8 | 272 | /* |
85ccce43 SW |
273 | * Keep some preallocated caps around (ceph_min_count), to |
274 | * avoid lots of free/alloc churn. | |
a8599bd8 | 275 | */ |
37151668 YS |
276 | if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + |
277 | mdsc->caps_min_count) { | |
278 | mdsc->caps_total_count--; | |
a8599bd8 SW |
279 | kmem_cache_free(ceph_cap_cachep, cap); |
280 | } else { | |
37151668 YS |
281 | mdsc->caps_avail_count++; |
282 | list_add(&cap->caps_item, &mdsc->caps_list); | |
a8599bd8 SW |
283 | } |
284 | ||
37151668 YS |
285 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
286 | mdsc->caps_reserve_count + mdsc->caps_avail_count); | |
287 | spin_unlock(&mdsc->caps_list_lock); | |
a8599bd8 SW |
288 | } |
289 | ||
290 | void ceph_reservation_status(struct ceph_client *client, | |
85ccce43 SW |
291 | int *total, int *avail, int *used, int *reserved, |
292 | int *min) | |
a8599bd8 | 293 | { |
37151668 YS |
294 | struct ceph_mds_client *mdsc = &client->mdsc; |
295 | ||
a8599bd8 | 296 | if (total) |
37151668 | 297 | *total = mdsc->caps_total_count; |
a8599bd8 | 298 | if (avail) |
37151668 | 299 | *avail = mdsc->caps_avail_count; |
a8599bd8 | 300 | if (used) |
37151668 | 301 | *used = mdsc->caps_use_count; |
a8599bd8 | 302 | if (reserved) |
37151668 | 303 | *reserved = mdsc->caps_reserve_count; |
85ccce43 | 304 | if (min) |
37151668 | 305 | *min = mdsc->caps_min_count; |
a8599bd8 SW |
306 | } |
307 | ||
308 | /* | |
309 | * Find ceph_cap for given mds, if any. | |
310 | * | |
311 | * Called with i_lock held. | |
312 | */ | |
313 | static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) | |
314 | { | |
315 | struct ceph_cap *cap; | |
316 | struct rb_node *n = ci->i_caps.rb_node; | |
317 | ||
318 | while (n) { | |
319 | cap = rb_entry(n, struct ceph_cap, ci_node); | |
320 | if (mds < cap->mds) | |
321 | n = n->rb_left; | |
322 | else if (mds > cap->mds) | |
323 | n = n->rb_right; | |
324 | else | |
325 | return cap; | |
326 | } | |
327 | return NULL; | |
328 | } | |
329 | ||
2bc50259 GF |
330 | struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
331 | { | |
332 | struct ceph_cap *cap; | |
333 | ||
334 | spin_lock(&ci->vfs_inode.i_lock); | |
335 | cap = __get_cap_for_mds(ci, mds); | |
336 | spin_unlock(&ci->vfs_inode.i_lock); | |
337 | return cap; | |
338 | } | |
339 | ||
a8599bd8 | 340 | /* |
33caad32 | 341 | * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1. |
a8599bd8 | 342 | */ |
ca81f3f6 | 343 | static int __ceph_get_cap_mds(struct ceph_inode_info *ci) |
a8599bd8 SW |
344 | { |
345 | struct ceph_cap *cap; | |
346 | int mds = -1; | |
347 | struct rb_node *p; | |
348 | ||
33caad32 | 349 | /* prefer mds with WR|BUFFER|EXCL caps */ |
a8599bd8 SW |
350 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
351 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
352 | mds = cap->mds; | |
a8599bd8 SW |
353 | if (cap->issued & (CEPH_CAP_FILE_WR | |
354 | CEPH_CAP_FILE_BUFFER | | |
355 | CEPH_CAP_FILE_EXCL)) | |
356 | break; | |
357 | } | |
358 | return mds; | |
359 | } | |
360 | ||
361 | int ceph_get_cap_mds(struct inode *inode) | |
362 | { | |
363 | int mds; | |
364 | spin_lock(&inode->i_lock); | |
ca81f3f6 | 365 | mds = __ceph_get_cap_mds(ceph_inode(inode)); |
a8599bd8 SW |
366 | spin_unlock(&inode->i_lock); |
367 | return mds; | |
368 | } | |
369 | ||
370 | /* | |
371 | * Called under i_lock. | |
372 | */ | |
373 | static void __insert_cap_node(struct ceph_inode_info *ci, | |
374 | struct ceph_cap *new) | |
375 | { | |
376 | struct rb_node **p = &ci->i_caps.rb_node; | |
377 | struct rb_node *parent = NULL; | |
378 | struct ceph_cap *cap = NULL; | |
379 | ||
380 | while (*p) { | |
381 | parent = *p; | |
382 | cap = rb_entry(parent, struct ceph_cap, ci_node); | |
383 | if (new->mds < cap->mds) | |
384 | p = &(*p)->rb_left; | |
385 | else if (new->mds > cap->mds) | |
386 | p = &(*p)->rb_right; | |
387 | else | |
388 | BUG(); | |
389 | } | |
390 | ||
391 | rb_link_node(&new->ci_node, parent, p); | |
392 | rb_insert_color(&new->ci_node, &ci->i_caps); | |
393 | } | |
394 | ||
395 | /* | |
396 | * (re)set cap hold timeouts, which control the delayed release | |
397 | * of unused caps back to the MDS. Should be called on cap use. | |
398 | */ | |
399 | static void __cap_set_timeouts(struct ceph_mds_client *mdsc, | |
400 | struct ceph_inode_info *ci) | |
401 | { | |
6b805185 | 402 | struct ceph_mount_args *ma = mdsc->client->mount_args; |
a8599bd8 SW |
403 | |
404 | ci->i_hold_caps_min = round_jiffies(jiffies + | |
405 | ma->caps_wanted_delay_min * HZ); | |
406 | ci->i_hold_caps_max = round_jiffies(jiffies + | |
407 | ma->caps_wanted_delay_max * HZ); | |
408 | dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, | |
409 | ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); | |
410 | } | |
411 | ||
412 | /* | |
413 | * (Re)queue cap at the end of the delayed cap release list. | |
414 | * | |
415 | * If I_FLUSH is set, leave the inode at the front of the list. | |
416 | * | |
417 | * Caller holds i_lock | |
418 | * -> we take mdsc->cap_delay_lock | |
419 | */ | |
420 | static void __cap_delay_requeue(struct ceph_mds_client *mdsc, | |
421 | struct ceph_inode_info *ci) | |
422 | { | |
423 | __cap_set_timeouts(mdsc, ci); | |
424 | dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, | |
425 | ci->i_ceph_flags, ci->i_hold_caps_max); | |
426 | if (!mdsc->stopping) { | |
427 | spin_lock(&mdsc->cap_delay_lock); | |
428 | if (!list_empty(&ci->i_cap_delay_list)) { | |
429 | if (ci->i_ceph_flags & CEPH_I_FLUSH) | |
430 | goto no_change; | |
431 | list_del_init(&ci->i_cap_delay_list); | |
432 | } | |
433 | list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); | |
434 | no_change: | |
435 | spin_unlock(&mdsc->cap_delay_lock); | |
436 | } | |
437 | } | |
438 | ||
439 | /* | |
440 | * Queue an inode for immediate writeback. Mark inode with I_FLUSH, | |
441 | * indicating we should send a cap message to flush dirty metadata | |
442 | * asap, and move to the front of the delayed cap list. | |
443 | */ | |
444 | static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, | |
445 | struct ceph_inode_info *ci) | |
446 | { | |
447 | dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); | |
448 | spin_lock(&mdsc->cap_delay_lock); | |
449 | ci->i_ceph_flags |= CEPH_I_FLUSH; | |
450 | if (!list_empty(&ci->i_cap_delay_list)) | |
451 | list_del_init(&ci->i_cap_delay_list); | |
452 | list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); | |
453 | spin_unlock(&mdsc->cap_delay_lock); | |
454 | } | |
455 | ||
456 | /* | |
457 | * Cancel delayed work on cap. | |
458 | * | |
459 | * Caller must hold i_lock. | |
460 | */ | |
461 | static void __cap_delay_cancel(struct ceph_mds_client *mdsc, | |
462 | struct ceph_inode_info *ci) | |
463 | { | |
464 | dout("__cap_delay_cancel %p\n", &ci->vfs_inode); | |
465 | if (list_empty(&ci->i_cap_delay_list)) | |
466 | return; | |
467 | spin_lock(&mdsc->cap_delay_lock); | |
468 | list_del_init(&ci->i_cap_delay_list); | |
469 | spin_unlock(&mdsc->cap_delay_lock); | |
470 | } | |
471 | ||
472 | /* | |
473 | * Common issue checks for add_cap, handle_cap_grant. | |
474 | */ | |
475 | static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, | |
476 | unsigned issued) | |
477 | { | |
478 | unsigned had = __ceph_caps_issued(ci, NULL); | |
479 | ||
480 | /* | |
481 | * Each time we receive FILE_CACHE anew, we increment | |
482 | * i_rdcache_gen. | |
483 | */ | |
2962507c SW |
484 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
485 | (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) | |
a8599bd8 SW |
486 | ci->i_rdcache_gen++; |
487 | ||
488 | /* | |
489 | * if we are newly issued FILE_SHARED, clear I_COMPLETE; we | |
490 | * don't know what happened to this directory while we didn't | |
491 | * have the cap. | |
492 | */ | |
493 | if ((issued & CEPH_CAP_FILE_SHARED) && | |
494 | (had & CEPH_CAP_FILE_SHARED) == 0) { | |
495 | ci->i_shared_gen++; | |
496 | if (S_ISDIR(ci->vfs_inode.i_mode)) { | |
497 | dout(" marking %p NOT complete\n", &ci->vfs_inode); | |
498 | ci->i_ceph_flags &= ~CEPH_I_COMPLETE; | |
499 | } | |
500 | } | |
501 | } | |
502 | ||
503 | /* | |
504 | * Add a capability under the given MDS session. | |
505 | * | |
506 | * Caller should hold session snap_rwsem (read) and s_mutex. | |
507 | * | |
508 | * @fmode is the open file mode, if we are opening a file, otherwise | |
509 | * it is < 0. (This is so we can atomically add the cap and add an | |
510 | * open file reference to it.) | |
511 | */ | |
512 | int ceph_add_cap(struct inode *inode, | |
513 | struct ceph_mds_session *session, u64 cap_id, | |
514 | int fmode, unsigned issued, unsigned wanted, | |
515 | unsigned seq, unsigned mseq, u64 realmino, int flags, | |
516 | struct ceph_cap_reservation *caps_reservation) | |
517 | { | |
518 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | |
519 | struct ceph_inode_info *ci = ceph_inode(inode); | |
520 | struct ceph_cap *new_cap = NULL; | |
521 | struct ceph_cap *cap; | |
522 | int mds = session->s_mds; | |
523 | int actual_wanted; | |
524 | ||
525 | dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, | |
526 | session->s_mds, cap_id, ceph_cap_string(issued), seq); | |
527 | ||
528 | /* | |
529 | * If we are opening the file, include file mode wanted bits | |
530 | * in wanted. | |
531 | */ | |
532 | if (fmode >= 0) | |
533 | wanted |= ceph_caps_for_mode(fmode); | |
534 | ||
535 | retry: | |
536 | spin_lock(&inode->i_lock); | |
537 | cap = __get_cap_for_mds(ci, mds); | |
538 | if (!cap) { | |
539 | if (new_cap) { | |
540 | cap = new_cap; | |
541 | new_cap = NULL; | |
542 | } else { | |
543 | spin_unlock(&inode->i_lock); | |
37151668 | 544 | new_cap = get_cap(mdsc, caps_reservation); |
a8599bd8 SW |
545 | if (new_cap == NULL) |
546 | return -ENOMEM; | |
547 | goto retry; | |
548 | } | |
549 | ||
550 | cap->issued = 0; | |
551 | cap->implemented = 0; | |
552 | cap->mds = mds; | |
553 | cap->mds_wanted = 0; | |
554 | ||
555 | cap->ci = ci; | |
556 | __insert_cap_node(ci, cap); | |
557 | ||
558 | /* clear out old exporting info? (i.e. on cap import) */ | |
559 | if (ci->i_cap_exporting_mds == mds) { | |
560 | ci->i_cap_exporting_issued = 0; | |
561 | ci->i_cap_exporting_mseq = 0; | |
562 | ci->i_cap_exporting_mds = -1; | |
563 | } | |
564 | ||
565 | /* add to session cap list */ | |
566 | cap->session = session; | |
567 | spin_lock(&session->s_cap_lock); | |
568 | list_add_tail(&cap->session_caps, &session->s_caps); | |
569 | session->s_nr_caps++; | |
570 | spin_unlock(&session->s_cap_lock); | |
571 | } | |
572 | ||
573 | if (!ci->i_snap_realm) { | |
574 | /* | |
575 | * add this inode to the appropriate snap realm | |
576 | */ | |
577 | struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, | |
578 | realmino); | |
579 | if (realm) { | |
580 | ceph_get_snap_realm(mdsc, realm); | |
581 | spin_lock(&realm->inodes_with_caps_lock); | |
582 | ci->i_snap_realm = realm; | |
583 | list_add(&ci->i_snap_realm_item, | |
584 | &realm->inodes_with_caps); | |
585 | spin_unlock(&realm->inodes_with_caps_lock); | |
586 | } else { | |
587 | pr_err("ceph_add_cap: couldn't find snap realm %llx\n", | |
588 | realmino); | |
b8cd07e7 | 589 | WARN_ON(!realm); |
a8599bd8 SW |
590 | } |
591 | } | |
592 | ||
593 | __check_cap_issue(ci, cap, issued); | |
594 | ||
595 | /* | |
596 | * If we are issued caps we don't want, or the mds' wanted | |
597 | * value appears to be off, queue a check so we'll release | |
598 | * later and/or update the mds wanted value. | |
599 | */ | |
600 | actual_wanted = __ceph_caps_wanted(ci); | |
601 | if ((wanted & ~actual_wanted) || | |
602 | (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { | |
603 | dout(" issued %s, mds wanted %s, actual %s, queueing\n", | |
604 | ceph_cap_string(issued), ceph_cap_string(wanted), | |
605 | ceph_cap_string(actual_wanted)); | |
606 | __cap_delay_requeue(mdsc, ci); | |
607 | } | |
608 | ||
609 | if (flags & CEPH_CAP_FLAG_AUTH) | |
610 | ci->i_auth_cap = cap; | |
611 | else if (ci->i_auth_cap == cap) | |
612 | ci->i_auth_cap = NULL; | |
613 | ||
614 | dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", | |
615 | inode, ceph_vinop(inode), cap, ceph_cap_string(issued), | |
616 | ceph_cap_string(issued|cap->issued), seq, mds); | |
617 | cap->cap_id = cap_id; | |
618 | cap->issued = issued; | |
619 | cap->implemented |= issued; | |
620 | cap->mds_wanted |= wanted; | |
621 | cap->seq = seq; | |
622 | cap->issue_seq = seq; | |
623 | cap->mseq = mseq; | |
685f9a5d | 624 | cap->cap_gen = session->s_cap_gen; |
a8599bd8 SW |
625 | |
626 | if (fmode >= 0) | |
627 | __ceph_get_fmode(ci, fmode); | |
628 | spin_unlock(&inode->i_lock); | |
03066f23 | 629 | wake_up_all(&ci->i_cap_wq); |
a8599bd8 SW |
630 | return 0; |
631 | } | |
632 | ||
633 | /* | |
634 | * Return true if cap has not timed out and belongs to the current | |
635 | * generation of the MDS session (i.e. has not gone 'stale' due to | |
636 | * us losing touch with the mds). | |
637 | */ | |
638 | static int __cap_is_valid(struct ceph_cap *cap) | |
639 | { | |
640 | unsigned long ttl; | |
cdac8303 | 641 | u32 gen; |
a8599bd8 SW |
642 | |
643 | spin_lock(&cap->session->s_cap_lock); | |
644 | gen = cap->session->s_cap_gen; | |
645 | ttl = cap->session->s_cap_ttl; | |
646 | spin_unlock(&cap->session->s_cap_lock); | |
647 | ||
685f9a5d | 648 | if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { |
a8599bd8 SW |
649 | dout("__cap_is_valid %p cap %p issued %s " |
650 | "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, | |
685f9a5d | 651 | cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); |
a8599bd8 SW |
652 | return 0; |
653 | } | |
654 | ||
655 | return 1; | |
656 | } | |
657 | ||
658 | /* | |
659 | * Return set of valid cap bits issued to us. Note that caps time | |
660 | * out, and may be invalidated in bulk if the client session times out | |
661 | * and session->s_cap_gen is bumped. | |
662 | */ | |
663 | int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) | |
664 | { | |
7af8f1e4 | 665 | int have = ci->i_snap_caps | ci->i_cap_exporting_issued; |
a8599bd8 SW |
666 | struct ceph_cap *cap; |
667 | struct rb_node *p; | |
668 | ||
669 | if (implemented) | |
670 | *implemented = 0; | |
671 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
672 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
673 | if (!__cap_is_valid(cap)) | |
674 | continue; | |
675 | dout("__ceph_caps_issued %p cap %p issued %s\n", | |
676 | &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); | |
677 | have |= cap->issued; | |
678 | if (implemented) | |
679 | *implemented |= cap->implemented; | |
680 | } | |
681 | return have; | |
682 | } | |
683 | ||
684 | /* | |
685 | * Get cap bits issued by caps other than @ocap | |
686 | */ | |
687 | int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) | |
688 | { | |
689 | int have = ci->i_snap_caps; | |
690 | struct ceph_cap *cap; | |
691 | struct rb_node *p; | |
692 | ||
693 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
694 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
695 | if (cap == ocap) | |
696 | continue; | |
697 | if (!__cap_is_valid(cap)) | |
698 | continue; | |
699 | have |= cap->issued; | |
700 | } | |
701 | return have; | |
702 | } | |
703 | ||
704 | /* | |
705 | * Move a cap to the end of the LRU (oldest caps at list head, newest | |
706 | * at list tail). | |
707 | */ | |
708 | static void __touch_cap(struct ceph_cap *cap) | |
709 | { | |
710 | struct ceph_mds_session *s = cap->session; | |
711 | ||
a8599bd8 | 712 | spin_lock(&s->s_cap_lock); |
7c1332b8 | 713 | if (s->s_cap_iterator == NULL) { |
5dacf091 SW |
714 | dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, |
715 | s->s_mds); | |
716 | list_move_tail(&cap->session_caps, &s->s_caps); | |
717 | } else { | |
718 | dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", | |
719 | &cap->ci->vfs_inode, cap, s->s_mds); | |
720 | } | |
a8599bd8 SW |
721 | spin_unlock(&s->s_cap_lock); |
722 | } | |
723 | ||
724 | /* | |
725 | * Check if we hold the given mask. If so, move the cap(s) to the | |
726 | * front of their respective LRUs. (This is the preferred way for | |
727 | * callers to check for caps they want.) | |
728 | */ | |
729 | int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) | |
730 | { | |
731 | struct ceph_cap *cap; | |
732 | struct rb_node *p; | |
733 | int have = ci->i_snap_caps; | |
734 | ||
735 | if ((have & mask) == mask) { | |
736 | dout("__ceph_caps_issued_mask %p snap issued %s" | |
737 | " (mask %s)\n", &ci->vfs_inode, | |
738 | ceph_cap_string(have), | |
739 | ceph_cap_string(mask)); | |
740 | return 1; | |
741 | } | |
742 | ||
743 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
744 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
745 | if (!__cap_is_valid(cap)) | |
746 | continue; | |
747 | if ((cap->issued & mask) == mask) { | |
748 | dout("__ceph_caps_issued_mask %p cap %p issued %s" | |
749 | " (mask %s)\n", &ci->vfs_inode, cap, | |
750 | ceph_cap_string(cap->issued), | |
751 | ceph_cap_string(mask)); | |
752 | if (touch) | |
753 | __touch_cap(cap); | |
754 | return 1; | |
755 | } | |
756 | ||
757 | /* does a combination of caps satisfy mask? */ | |
758 | have |= cap->issued; | |
759 | if ((have & mask) == mask) { | |
760 | dout("__ceph_caps_issued_mask %p combo issued %s" | |
761 | " (mask %s)\n", &ci->vfs_inode, | |
762 | ceph_cap_string(cap->issued), | |
763 | ceph_cap_string(mask)); | |
764 | if (touch) { | |
765 | struct rb_node *q; | |
766 | ||
767 | /* touch this + preceeding caps */ | |
768 | __touch_cap(cap); | |
769 | for (q = rb_first(&ci->i_caps); q != p; | |
770 | q = rb_next(q)) { | |
771 | cap = rb_entry(q, struct ceph_cap, | |
772 | ci_node); | |
773 | if (!__cap_is_valid(cap)) | |
774 | continue; | |
775 | __touch_cap(cap); | |
776 | } | |
777 | } | |
778 | return 1; | |
779 | } | |
780 | } | |
781 | ||
782 | return 0; | |
783 | } | |
784 | ||
785 | /* | |
786 | * Return true if mask caps are currently being revoked by an MDS. | |
787 | */ | |
788 | int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) | |
789 | { | |
790 | struct inode *inode = &ci->vfs_inode; | |
791 | struct ceph_cap *cap; | |
792 | struct rb_node *p; | |
793 | int ret = 0; | |
794 | ||
795 | spin_lock(&inode->i_lock); | |
796 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
797 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
798 | if (__cap_is_valid(cap) && | |
799 | (cap->implemented & ~cap->issued & mask)) { | |
800 | ret = 1; | |
801 | break; | |
802 | } | |
803 | } | |
804 | spin_unlock(&inode->i_lock); | |
805 | dout("ceph_caps_revoking %p %s = %d\n", inode, | |
806 | ceph_cap_string(mask), ret); | |
807 | return ret; | |
808 | } | |
809 | ||
810 | int __ceph_caps_used(struct ceph_inode_info *ci) | |
811 | { | |
812 | int used = 0; | |
813 | if (ci->i_pin_ref) | |
814 | used |= CEPH_CAP_PIN; | |
815 | if (ci->i_rd_ref) | |
816 | used |= CEPH_CAP_FILE_RD; | |
817 | if (ci->i_rdcache_ref || ci->i_rdcache_gen) | |
818 | used |= CEPH_CAP_FILE_CACHE; | |
819 | if (ci->i_wr_ref) | |
820 | used |= CEPH_CAP_FILE_WR; | |
821 | if (ci->i_wrbuffer_ref) | |
822 | used |= CEPH_CAP_FILE_BUFFER; | |
823 | return used; | |
824 | } | |
825 | ||
826 | /* | |
827 | * wanted, by virtue of open file modes | |
828 | */ | |
829 | int __ceph_caps_file_wanted(struct ceph_inode_info *ci) | |
830 | { | |
831 | int want = 0; | |
832 | int mode; | |
33caad32 | 833 | for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++) |
a8599bd8 SW |
834 | if (ci->i_nr_by_mode[mode]) |
835 | want |= ceph_caps_for_mode(mode); | |
836 | return want; | |
837 | } | |
838 | ||
839 | /* | |
840 | * Return caps we have registered with the MDS(s) as 'wanted'. | |
841 | */ | |
842 | int __ceph_caps_mds_wanted(struct ceph_inode_info *ci) | |
843 | { | |
844 | struct ceph_cap *cap; | |
845 | struct rb_node *p; | |
846 | int mds_wanted = 0; | |
847 | ||
848 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
849 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
850 | if (!__cap_is_valid(cap)) | |
851 | continue; | |
852 | mds_wanted |= cap->mds_wanted; | |
853 | } | |
854 | return mds_wanted; | |
855 | } | |
856 | ||
857 | /* | |
858 | * called under i_lock | |
859 | */ | |
860 | static int __ceph_is_any_caps(struct ceph_inode_info *ci) | |
861 | { | |
862 | return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0; | |
863 | } | |
864 | ||
865 | /* | |
f818a736 SW |
866 | * Remove a cap. Take steps to deal with a racing iterate_session_caps. |
867 | * | |
a6369741 SW |
868 | * caller should hold i_lock. |
869 | * caller will not hold session s_mutex if called from destroy_inode. | |
a8599bd8 | 870 | */ |
7c1332b8 | 871 | void __ceph_remove_cap(struct ceph_cap *cap) |
a8599bd8 SW |
872 | { |
873 | struct ceph_mds_session *session = cap->session; | |
874 | struct ceph_inode_info *ci = cap->ci; | |
640ef79d CR |
875 | struct ceph_mds_client *mdsc = |
876 | &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; | |
f818a736 | 877 | int removed = 0; |
a8599bd8 SW |
878 | |
879 | dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); | |
880 | ||
7c1332b8 SW |
881 | /* remove from session list */ |
882 | spin_lock(&session->s_cap_lock); | |
883 | if (session->s_cap_iterator == cap) { | |
884 | /* not yet, we are iterating over this very cap */ | |
885 | dout("__ceph_remove_cap delaying %p removal from session %p\n", | |
886 | cap, cap->session); | |
887 | } else { | |
888 | list_del_init(&cap->session_caps); | |
889 | session->s_nr_caps--; | |
890 | cap->session = NULL; | |
f818a736 | 891 | removed = 1; |
7c1332b8 | 892 | } |
f818a736 SW |
893 | /* protect backpointer with s_cap_lock: see iterate_session_caps */ |
894 | cap->ci = NULL; | |
7c1332b8 SW |
895 | spin_unlock(&session->s_cap_lock); |
896 | ||
f818a736 SW |
897 | /* remove from inode list */ |
898 | rb_erase(&cap->ci_node, &ci->i_caps); | |
899 | if (ci->i_auth_cap == cap) | |
900 | ci->i_auth_cap = NULL; | |
901 | ||
902 | if (removed) | |
37151668 | 903 | ceph_put_cap(mdsc, cap); |
a8599bd8 SW |
904 | |
905 | if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) { | |
906 | struct ceph_snap_realm *realm = ci->i_snap_realm; | |
907 | spin_lock(&realm->inodes_with_caps_lock); | |
908 | list_del_init(&ci->i_snap_realm_item); | |
909 | ci->i_snap_realm_counter++; | |
910 | ci->i_snap_realm = NULL; | |
911 | spin_unlock(&realm->inodes_with_caps_lock); | |
912 | ceph_put_snap_realm(mdsc, realm); | |
913 | } | |
914 | if (!__ceph_is_any_real_caps(ci)) | |
915 | __cap_delay_cancel(mdsc, ci); | |
916 | } | |
917 | ||
918 | /* | |
919 | * Build and send a cap message to the given MDS. | |
920 | * | |
921 | * Caller should be holding s_mutex. | |
922 | */ | |
923 | static int send_cap_msg(struct ceph_mds_session *session, | |
924 | u64 ino, u64 cid, int op, | |
925 | int caps, int wanted, int dirty, | |
926 | u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq, | |
927 | u64 size, u64 max_size, | |
928 | struct timespec *mtime, struct timespec *atime, | |
929 | u64 time_warp_seq, | |
930 | uid_t uid, gid_t gid, mode_t mode, | |
931 | u64 xattr_version, | |
932 | struct ceph_buffer *xattrs_buf, | |
933 | u64 follows) | |
934 | { | |
935 | struct ceph_mds_caps *fc; | |
936 | struct ceph_msg *msg; | |
937 | ||
938 | dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" | |
939 | " seq %u/%u mseq %u follows %lld size %llu/%llu" | |
940 | " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op), | |
941 | cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted), | |
942 | ceph_cap_string(dirty), | |
943 | seq, issue_seq, mseq, follows, size, max_size, | |
944 | xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0); | |
945 | ||
34d23762 | 946 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS); |
a79832f2 SW |
947 | if (!msg) |
948 | return -ENOMEM; | |
a8599bd8 | 949 | |
6df058c0 | 950 | msg->hdr.tid = cpu_to_le64(flush_tid); |
a8599bd8 | 951 | |
6df058c0 | 952 | fc = msg->front.iov_base; |
a8599bd8 SW |
953 | memset(fc, 0, sizeof(*fc)); |
954 | ||
955 | fc->cap_id = cpu_to_le64(cid); | |
956 | fc->op = cpu_to_le32(op); | |
957 | fc->seq = cpu_to_le32(seq); | |
a8599bd8 SW |
958 | fc->issue_seq = cpu_to_le32(issue_seq); |
959 | fc->migrate_seq = cpu_to_le32(mseq); | |
960 | fc->caps = cpu_to_le32(caps); | |
961 | fc->wanted = cpu_to_le32(wanted); | |
962 | fc->dirty = cpu_to_le32(dirty); | |
963 | fc->ino = cpu_to_le64(ino); | |
964 | fc->snap_follows = cpu_to_le64(follows); | |
965 | ||
966 | fc->size = cpu_to_le64(size); | |
967 | fc->max_size = cpu_to_le64(max_size); | |
968 | if (mtime) | |
969 | ceph_encode_timespec(&fc->mtime, mtime); | |
970 | if (atime) | |
971 | ceph_encode_timespec(&fc->atime, atime); | |
972 | fc->time_warp_seq = cpu_to_le32(time_warp_seq); | |
973 | ||
974 | fc->uid = cpu_to_le32(uid); | |
975 | fc->gid = cpu_to_le32(gid); | |
976 | fc->mode = cpu_to_le32(mode); | |
977 | ||
978 | fc->xattr_version = cpu_to_le64(xattr_version); | |
979 | if (xattrs_buf) { | |
980 | msg->middle = ceph_buffer_get(xattrs_buf); | |
981 | fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len); | |
982 | msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len); | |
983 | } | |
984 | ||
985 | ceph_con_send(&session->s_con, msg); | |
986 | return 0; | |
987 | } | |
988 | ||
3d7ded4d SW |
989 | static void __queue_cap_release(struct ceph_mds_session *session, |
990 | u64 ino, u64 cap_id, u32 migrate_seq, | |
991 | u32 issue_seq) | |
992 | { | |
993 | struct ceph_msg *msg; | |
994 | struct ceph_mds_cap_release *head; | |
995 | struct ceph_mds_cap_item *item; | |
996 | ||
997 | spin_lock(&session->s_cap_lock); | |
998 | BUG_ON(!session->s_num_cap_releases); | |
999 | msg = list_first_entry(&session->s_cap_releases, | |
1000 | struct ceph_msg, list_head); | |
1001 | ||
1002 | dout(" adding %llx release to mds%d msg %p (%d left)\n", | |
1003 | ino, session->s_mds, msg, session->s_num_cap_releases); | |
1004 | ||
1005 | BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE); | |
1006 | head = msg->front.iov_base; | |
1007 | head->num = cpu_to_le32(le32_to_cpu(head->num) + 1); | |
1008 | item = msg->front.iov_base + msg->front.iov_len; | |
1009 | item->ino = cpu_to_le64(ino); | |
1010 | item->cap_id = cpu_to_le64(cap_id); | |
1011 | item->migrate_seq = cpu_to_le32(migrate_seq); | |
1012 | item->seq = cpu_to_le32(issue_seq); | |
1013 | ||
1014 | session->s_num_cap_releases--; | |
1015 | ||
1016 | msg->front.iov_len += sizeof(*item); | |
1017 | if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { | |
1018 | dout(" release msg %p full\n", msg); | |
1019 | list_move_tail(&msg->list_head, &session->s_cap_releases_done); | |
1020 | } else { | |
1021 | dout(" release msg %p at %d/%d (%d)\n", msg, | |
1022 | (int)le32_to_cpu(head->num), | |
1023 | (int)CEPH_CAPS_PER_RELEASE, | |
1024 | (int)msg->front.iov_len); | |
1025 | } | |
1026 | spin_unlock(&session->s_cap_lock); | |
1027 | } | |
1028 | ||
a8599bd8 | 1029 | /* |
a6369741 SW |
1030 | * Queue cap releases when an inode is dropped from our cache. Since |
1031 | * inode is about to be destroyed, there is no need for i_lock. | |
a8599bd8 SW |
1032 | */ |
1033 | void ceph_queue_caps_release(struct inode *inode) | |
1034 | { | |
1035 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1036 | struct rb_node *p; | |
1037 | ||
a8599bd8 SW |
1038 | p = rb_first(&ci->i_caps); |
1039 | while (p) { | |
1040 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); | |
1041 | struct ceph_mds_session *session = cap->session; | |
a8599bd8 | 1042 | |
3d7ded4d SW |
1043 | __queue_cap_release(session, ceph_ino(inode), cap->cap_id, |
1044 | cap->mseq, cap->issue_seq); | |
a8599bd8 | 1045 | p = rb_next(p); |
7c1332b8 | 1046 | __ceph_remove_cap(cap); |
a8599bd8 | 1047 | } |
a8599bd8 SW |
1048 | } |
1049 | ||
1050 | /* | |
1051 | * Send a cap msg on the given inode. Update our caps state, then | |
1052 | * drop i_lock and send the message. | |
1053 | * | |
1054 | * Make note of max_size reported/requested from mds, revoked caps | |
1055 | * that have now been implemented. | |
1056 | * | |
1057 | * Make half-hearted attempt ot to invalidate page cache if we are | |
1058 | * dropping RDCACHE. Note that this will leave behind locked pages | |
1059 | * that we'll then need to deal with elsewhere. | |
1060 | * | |
1061 | * Return non-zero if delayed release, or we experienced an error | |
1062 | * such that the caller should requeue + retry later. | |
1063 | * | |
1064 | * called with i_lock, then drops it. | |
1065 | * caller should hold snap_rwsem (read), s_mutex. | |
1066 | */ | |
1067 | static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, | |
1068 | int op, int used, int want, int retain, int flushing, | |
1069 | unsigned *pflush_tid) | |
1070 | __releases(cap->ci->vfs_inode->i_lock) | |
1071 | { | |
1072 | struct ceph_inode_info *ci = cap->ci; | |
1073 | struct inode *inode = &ci->vfs_inode; | |
1074 | u64 cap_id = cap->cap_id; | |
68c28323 | 1075 | int held, revoking, dropping, keep; |
a8599bd8 SW |
1076 | u64 seq, issue_seq, mseq, time_warp_seq, follows; |
1077 | u64 size, max_size; | |
1078 | struct timespec mtime, atime; | |
1079 | int wake = 0; | |
1080 | mode_t mode; | |
1081 | uid_t uid; | |
1082 | gid_t gid; | |
1083 | struct ceph_mds_session *session; | |
1084 | u64 xattr_version = 0; | |
082afec9 | 1085 | struct ceph_buffer *xattr_blob = NULL; |
a8599bd8 SW |
1086 | int delayed = 0; |
1087 | u64 flush_tid = 0; | |
1088 | int i; | |
1089 | int ret; | |
1090 | ||
68c28323 SW |
1091 | held = cap->issued | cap->implemented; |
1092 | revoking = cap->implemented & ~cap->issued; | |
1093 | retain &= ~revoking; | |
1094 | dropping = cap->issued & ~retain; | |
1095 | ||
a8599bd8 SW |
1096 | dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", |
1097 | inode, cap, cap->session, | |
1098 | ceph_cap_string(held), ceph_cap_string(held & retain), | |
1099 | ceph_cap_string(revoking)); | |
1100 | BUG_ON((retain & CEPH_CAP_PIN) == 0); | |
1101 | ||
1102 | session = cap->session; | |
1103 | ||
1104 | /* don't release wanted unless we've waited a bit. */ | |
1105 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && | |
1106 | time_before(jiffies, ci->i_hold_caps_min)) { | |
1107 | dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", | |
1108 | ceph_cap_string(cap->issued), | |
1109 | ceph_cap_string(cap->issued & retain), | |
1110 | ceph_cap_string(cap->mds_wanted), | |
1111 | ceph_cap_string(want)); | |
1112 | want |= cap->mds_wanted; | |
1113 | retain |= cap->issued; | |
1114 | delayed = 1; | |
1115 | } | |
1116 | ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); | |
1117 | ||
1118 | cap->issued &= retain; /* drop bits we don't want */ | |
1119 | if (cap->implemented & ~cap->issued) { | |
1120 | /* | |
1121 | * Wake up any waiters on wanted -> needed transition. | |
1122 | * This is due to the weird transition from buffered | |
1123 | * to sync IO... we need to flush dirty pages _before_ | |
1124 | * allowing sync writes to avoid reordering. | |
1125 | */ | |
1126 | wake = 1; | |
1127 | } | |
1128 | cap->implemented &= cap->issued | used; | |
1129 | cap->mds_wanted = want; | |
1130 | ||
1131 | if (flushing) { | |
1132 | /* | |
1133 | * assign a tid for flush operations so we can avoid | |
1134 | * flush1 -> dirty1 -> flush2 -> flushack1 -> mark | |
1135 | * clean type races. track latest tid for every bit | |
1136 | * so we can handle flush AxFw, flush Fw, and have the | |
1137 | * first ack clean Ax. | |
1138 | */ | |
1139 | flush_tid = ++ci->i_cap_flush_last_tid; | |
1140 | if (pflush_tid) | |
1141 | *pflush_tid = flush_tid; | |
1142 | dout(" cap_flush_tid %d\n", (int)flush_tid); | |
1143 | for (i = 0; i < CEPH_CAP_BITS; i++) | |
1144 | if (flushing & (1 << i)) | |
1145 | ci->i_cap_flush_tid[i] = flush_tid; | |
1146 | } | |
1147 | ||
1148 | keep = cap->implemented; | |
1149 | seq = cap->seq; | |
1150 | issue_seq = cap->issue_seq; | |
1151 | mseq = cap->mseq; | |
1152 | size = inode->i_size; | |
1153 | ci->i_reported_size = size; | |
1154 | max_size = ci->i_wanted_max_size; | |
1155 | ci->i_requested_max_size = max_size; | |
1156 | mtime = inode->i_mtime; | |
1157 | atime = inode->i_atime; | |
1158 | time_warp_seq = ci->i_time_warp_seq; | |
1159 | follows = ci->i_snap_realm->cached_context->seq; | |
1160 | uid = inode->i_uid; | |
1161 | gid = inode->i_gid; | |
1162 | mode = inode->i_mode; | |
1163 | ||
082afec9 | 1164 | if (flushing & CEPH_CAP_XATTR_EXCL) { |
a8599bd8 | 1165 | __ceph_build_xattrs_blob(ci); |
082afec9 SW |
1166 | xattr_blob = ci->i_xattrs.blob; |
1167 | xattr_version = ci->i_xattrs.version; | |
a8599bd8 SW |
1168 | } |
1169 | ||
1170 | spin_unlock(&inode->i_lock); | |
1171 | ||
a8599bd8 SW |
1172 | ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id, |
1173 | op, keep, want, flushing, seq, flush_tid, issue_seq, mseq, | |
1174 | size, max_size, &mtime, &atime, time_warp_seq, | |
082afec9 | 1175 | uid, gid, mode, xattr_version, xattr_blob, |
a8599bd8 SW |
1176 | follows); |
1177 | if (ret < 0) { | |
1178 | dout("error sending cap msg, must requeue %p\n", inode); | |
1179 | delayed = 1; | |
1180 | } | |
1181 | ||
1182 | if (wake) | |
03066f23 | 1183 | wake_up_all(&ci->i_cap_wq); |
a8599bd8 SW |
1184 | |
1185 | return delayed; | |
1186 | } | |
1187 | ||
1188 | /* | |
1189 | * When a snapshot is taken, clients accumulate dirty metadata on | |
1190 | * inodes with capabilities in ceph_cap_snaps to describe the file | |
1191 | * state at the time the snapshot was taken. This must be flushed | |
1192 | * asynchronously back to the MDS once sync writes complete and dirty | |
1193 | * data is written out. | |
1194 | * | |
1195 | * Called under i_lock. Takes s_mutex as needed. | |
1196 | */ | |
1197 | void __ceph_flush_snaps(struct ceph_inode_info *ci, | |
1198 | struct ceph_mds_session **psession) | |
cd84db6e YS |
1199 | __releases(ci->vfs_inode->i_lock) |
1200 | __acquires(ci->vfs_inode->i_lock) | |
a8599bd8 SW |
1201 | { |
1202 | struct inode *inode = &ci->vfs_inode; | |
1203 | int mds; | |
1204 | struct ceph_cap_snap *capsnap; | |
1205 | u32 mseq; | |
1206 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | |
1207 | struct ceph_mds_session *session = NULL; /* if session != NULL, we hold | |
1208 | session->s_mutex */ | |
1209 | u64 next_follows = 0; /* keep track of how far we've gotten through the | |
1210 | i_cap_snaps list, and skip these entries next time | |
1211 | around to avoid an infinite loop */ | |
1212 | ||
1213 | if (psession) | |
1214 | session = *psession; | |
1215 | ||
1216 | dout("__flush_snaps %p\n", inode); | |
1217 | retry: | |
1218 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | |
1219 | /* avoid an infiniute loop after retry */ | |
1220 | if (capsnap->follows < next_follows) | |
1221 | continue; | |
1222 | /* | |
1223 | * we need to wait for sync writes to complete and for dirty | |
1224 | * pages to be written out. | |
1225 | */ | |
1226 | if (capsnap->dirty_pages || capsnap->writing) | |
1227 | continue; | |
1228 | ||
819ccbfa SW |
1229 | /* |
1230 | * if cap writeback already occurred, we should have dropped | |
1231 | * the capsnap in ceph_put_wrbuffer_cap_refs. | |
1232 | */ | |
1233 | BUG_ON(capsnap->dirty == 0); | |
1234 | ||
a8599bd8 | 1235 | /* pick mds, take s_mutex */ |
ca81f3f6 SW |
1236 | if (ci->i_auth_cap == NULL) { |
1237 | dout("no auth cap (migrating?), doing nothing\n"); | |
1238 | goto out; | |
1239 | } | |
1240 | mds = ci->i_auth_cap->session->s_mds; | |
1241 | mseq = ci->i_auth_cap->mseq; | |
1242 | ||
a8599bd8 SW |
1243 | if (session && session->s_mds != mds) { |
1244 | dout("oops, wrong session %p mutex\n", session); | |
1245 | mutex_unlock(&session->s_mutex); | |
1246 | ceph_put_mds_session(session); | |
1247 | session = NULL; | |
1248 | } | |
1249 | if (!session) { | |
1250 | spin_unlock(&inode->i_lock); | |
1251 | mutex_lock(&mdsc->mutex); | |
1252 | session = __ceph_lookup_mds_session(mdsc, mds); | |
1253 | mutex_unlock(&mdsc->mutex); | |
1254 | if (session) { | |
1255 | dout("inverting session/ino locks on %p\n", | |
1256 | session); | |
1257 | mutex_lock(&session->s_mutex); | |
1258 | } | |
1259 | /* | |
1260 | * if session == NULL, we raced against a cap | |
ca81f3f6 SW |
1261 | * deletion or migration. retry, and we'll |
1262 | * get a better @mds value next time. | |
a8599bd8 SW |
1263 | */ |
1264 | spin_lock(&inode->i_lock); | |
1265 | goto retry; | |
1266 | } | |
1267 | ||
1268 | capsnap->flush_tid = ++ci->i_cap_flush_last_tid; | |
1269 | atomic_inc(&capsnap->nref); | |
1270 | if (!list_empty(&capsnap->flushing_item)) | |
1271 | list_del_init(&capsnap->flushing_item); | |
1272 | list_add_tail(&capsnap->flushing_item, | |
1273 | &session->s_cap_snaps_flushing); | |
1274 | spin_unlock(&inode->i_lock); | |
1275 | ||
1276 | dout("flush_snaps %p cap_snap %p follows %lld size %llu\n", | |
1277 | inode, capsnap, next_follows, capsnap->size); | |
1278 | send_cap_msg(session, ceph_vino(inode).ino, 0, | |
1279 | CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0, | |
1280 | capsnap->dirty, 0, capsnap->flush_tid, 0, mseq, | |
1281 | capsnap->size, 0, | |
1282 | &capsnap->mtime, &capsnap->atime, | |
1283 | capsnap->time_warp_seq, | |
1284 | capsnap->uid, capsnap->gid, capsnap->mode, | |
1285 | 0, NULL, | |
1286 | capsnap->follows); | |
1287 | ||
1288 | next_follows = capsnap->follows + 1; | |
1289 | ceph_put_cap_snap(capsnap); | |
1290 | ||
1291 | spin_lock(&inode->i_lock); | |
1292 | goto retry; | |
1293 | } | |
1294 | ||
1295 | /* we flushed them all; remove this inode from the queue */ | |
1296 | spin_lock(&mdsc->snap_flush_lock); | |
1297 | list_del_init(&ci->i_snap_flush_item); | |
1298 | spin_unlock(&mdsc->snap_flush_lock); | |
1299 | ||
ca81f3f6 | 1300 | out: |
a8599bd8 SW |
1301 | if (psession) |
1302 | *psession = session; | |
1303 | else if (session) { | |
1304 | mutex_unlock(&session->s_mutex); | |
1305 | ceph_put_mds_session(session); | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | static void ceph_flush_snaps(struct ceph_inode_info *ci) | |
1310 | { | |
1311 | struct inode *inode = &ci->vfs_inode; | |
1312 | ||
1313 | spin_lock(&inode->i_lock); | |
1314 | __ceph_flush_snaps(ci, NULL); | |
1315 | spin_unlock(&inode->i_lock); | |
1316 | } | |
1317 | ||
76e3b390 SW |
1318 | /* |
1319 | * Mark caps dirty. If inode is newly dirty, add to the global dirty | |
1320 | * list. | |
1321 | */ | |
1322 | void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask) | |
1323 | { | |
640ef79d CR |
1324 | struct ceph_mds_client *mdsc = |
1325 | &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; | |
76e3b390 SW |
1326 | struct inode *inode = &ci->vfs_inode; |
1327 | int was = ci->i_dirty_caps; | |
1328 | int dirty = 0; | |
1329 | ||
1330 | dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, | |
1331 | ceph_cap_string(mask), ceph_cap_string(was), | |
1332 | ceph_cap_string(was | mask)); | |
1333 | ci->i_dirty_caps |= mask; | |
1334 | if (was == 0) { | |
1335 | dout(" inode %p now dirty\n", &ci->vfs_inode); | |
1336 | BUG_ON(!list_empty(&ci->i_dirty_item)); | |
1337 | spin_lock(&mdsc->cap_dirty_lock); | |
1338 | list_add(&ci->i_dirty_item, &mdsc->cap_dirty); | |
1339 | spin_unlock(&mdsc->cap_dirty_lock); | |
1340 | if (ci->i_flushing_caps == 0) { | |
1341 | igrab(inode); | |
1342 | dirty |= I_DIRTY_SYNC; | |
1343 | } | |
1344 | } | |
1345 | BUG_ON(list_empty(&ci->i_dirty_item)); | |
1346 | if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && | |
1347 | (mask & CEPH_CAP_FILE_BUFFER)) | |
1348 | dirty |= I_DIRTY_DATASYNC; | |
1349 | if (dirty) | |
1350 | __mark_inode_dirty(inode, dirty); | |
1351 | __cap_delay_requeue(mdsc, ci); | |
1352 | } | |
1353 | ||
a8599bd8 SW |
1354 | /* |
1355 | * Add dirty inode to the flushing list. Assigned a seq number so we | |
1356 | * can wait for caps to flush without starving. | |
cdc35f96 SW |
1357 | * |
1358 | * Called under i_lock. | |
a8599bd8 | 1359 | */ |
cdc35f96 | 1360 | static int __mark_caps_flushing(struct inode *inode, |
a8599bd8 SW |
1361 | struct ceph_mds_session *session) |
1362 | { | |
640ef79d | 1363 | struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; |
a8599bd8 | 1364 | struct ceph_inode_info *ci = ceph_inode(inode); |
cdc35f96 | 1365 | int flushing; |
50b885b9 | 1366 | |
cdc35f96 | 1367 | BUG_ON(ci->i_dirty_caps == 0); |
a8599bd8 | 1368 | BUG_ON(list_empty(&ci->i_dirty_item)); |
cdc35f96 SW |
1369 | |
1370 | flushing = ci->i_dirty_caps; | |
1371 | dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", | |
1372 | ceph_cap_string(flushing), | |
1373 | ceph_cap_string(ci->i_flushing_caps), | |
1374 | ceph_cap_string(ci->i_flushing_caps | flushing)); | |
1375 | ci->i_flushing_caps |= flushing; | |
1376 | ci->i_dirty_caps = 0; | |
afcdaea3 | 1377 | dout(" inode %p now !dirty\n", inode); |
cdc35f96 | 1378 | |
a8599bd8 | 1379 | spin_lock(&mdsc->cap_dirty_lock); |
afcdaea3 SW |
1380 | list_del_init(&ci->i_dirty_item); |
1381 | ||
1382 | ci->i_cap_flush_seq = ++mdsc->cap_flush_seq; | |
a8599bd8 SW |
1383 | if (list_empty(&ci->i_flushing_item)) { |
1384 | list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); | |
1385 | mdsc->num_cap_flushing++; | |
afcdaea3 SW |
1386 | dout(" inode %p now flushing seq %lld\n", inode, |
1387 | ci->i_cap_flush_seq); | |
1388 | } else { | |
1389 | list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); | |
1390 | dout(" inode %p now flushing (more) seq %lld\n", inode, | |
a8599bd8 SW |
1391 | ci->i_cap_flush_seq); |
1392 | } | |
1393 | spin_unlock(&mdsc->cap_dirty_lock); | |
cdc35f96 SW |
1394 | |
1395 | return flushing; | |
a8599bd8 SW |
1396 | } |
1397 | ||
5ecad6fd SW |
1398 | /* |
1399 | * try to invalidate mapping pages without blocking. | |
1400 | */ | |
1401 | static int mapping_is_empty(struct address_space *mapping) | |
1402 | { | |
1403 | struct page *page = find_get_page(mapping, 0); | |
1404 | ||
1405 | if (!page) | |
1406 | return 1; | |
1407 | ||
1408 | put_page(page); | |
1409 | return 0; | |
1410 | } | |
1411 | ||
1412 | static int try_nonblocking_invalidate(struct inode *inode) | |
1413 | { | |
1414 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1415 | u32 invalidating_gen = ci->i_rdcache_gen; | |
1416 | ||
1417 | spin_unlock(&inode->i_lock); | |
1418 | invalidate_mapping_pages(&inode->i_data, 0, -1); | |
1419 | spin_lock(&inode->i_lock); | |
1420 | ||
1421 | if (mapping_is_empty(&inode->i_data) && | |
1422 | invalidating_gen == ci->i_rdcache_gen) { | |
1423 | /* success. */ | |
1424 | dout("try_nonblocking_invalidate %p success\n", inode); | |
1425 | ci->i_rdcache_gen = 0; | |
1426 | ci->i_rdcache_revoking = 0; | |
1427 | return 0; | |
1428 | } | |
1429 | dout("try_nonblocking_invalidate %p failed\n", inode); | |
1430 | return -1; | |
1431 | } | |
1432 | ||
a8599bd8 SW |
1433 | /* |
1434 | * Swiss army knife function to examine currently used and wanted | |
1435 | * versus held caps. Release, flush, ack revoked caps to mds as | |
1436 | * appropriate. | |
1437 | * | |
1438 | * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay | |
1439 | * cap release further. | |
1440 | * CHECK_CAPS_AUTHONLY - we should only check the auth cap | |
1441 | * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without | |
1442 | * further delay. | |
1443 | */ | |
1444 | void ceph_check_caps(struct ceph_inode_info *ci, int flags, | |
1445 | struct ceph_mds_session *session) | |
1446 | { | |
1447 | struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode); | |
1448 | struct ceph_mds_client *mdsc = &client->mdsc; | |
1449 | struct inode *inode = &ci->vfs_inode; | |
1450 | struct ceph_cap *cap; | |
1451 | int file_wanted, used; | |
1452 | int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ | |
cbd03635 | 1453 | int issued, implemented, want, retain, revoking, flushing = 0; |
a8599bd8 SW |
1454 | int mds = -1; /* keep track of how far we've gone through i_caps list |
1455 | to avoid an infinite loop on retry */ | |
1456 | struct rb_node *p; | |
1457 | int tried_invalidate = 0; | |
1458 | int delayed = 0, sent = 0, force_requeue = 0, num; | |
cbd03635 | 1459 | int queue_invalidate = 0; |
a8599bd8 SW |
1460 | int is_delayed = flags & CHECK_CAPS_NODELAY; |
1461 | ||
1462 | /* if we are unmounting, flush any unused caps immediately. */ | |
1463 | if (mdsc->stopping) | |
1464 | is_delayed = 1; | |
1465 | ||
1466 | spin_lock(&inode->i_lock); | |
1467 | ||
1468 | if (ci->i_ceph_flags & CEPH_I_FLUSH) | |
1469 | flags |= CHECK_CAPS_FLUSH; | |
1470 | ||
1471 | /* flush snaps first time around only */ | |
1472 | if (!list_empty(&ci->i_cap_snaps)) | |
1473 | __ceph_flush_snaps(ci, &session); | |
1474 | goto retry_locked; | |
1475 | retry: | |
1476 | spin_lock(&inode->i_lock); | |
1477 | retry_locked: | |
1478 | file_wanted = __ceph_caps_file_wanted(ci); | |
1479 | used = __ceph_caps_used(ci); | |
1480 | want = file_wanted | used; | |
cbd03635 SW |
1481 | issued = __ceph_caps_issued(ci, &implemented); |
1482 | revoking = implemented & ~issued; | |
a8599bd8 SW |
1483 | |
1484 | retain = want | CEPH_CAP_PIN; | |
1485 | if (!mdsc->stopping && inode->i_nlink > 0) { | |
1486 | if (want) { | |
1487 | retain |= CEPH_CAP_ANY; /* be greedy */ | |
1488 | } else { | |
1489 | retain |= CEPH_CAP_ANY_SHARED; | |
1490 | /* | |
1491 | * keep RD only if we didn't have the file open RW, | |
1492 | * because then the mds would revoke it anyway to | |
1493 | * journal max_size=0. | |
1494 | */ | |
1495 | if (ci->i_max_size == 0) | |
1496 | retain |= CEPH_CAP_ANY_RD; | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | dout("check_caps %p file_want %s used %s dirty %s flushing %s" | |
cbd03635 | 1501 | " issued %s revoking %s retain %s %s%s%s\n", inode, |
a8599bd8 SW |
1502 | ceph_cap_string(file_wanted), |
1503 | ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), | |
1504 | ceph_cap_string(ci->i_flushing_caps), | |
cbd03635 | 1505 | ceph_cap_string(issued), ceph_cap_string(revoking), |
a8599bd8 SW |
1506 | ceph_cap_string(retain), |
1507 | (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", | |
1508 | (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", | |
1509 | (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); | |
1510 | ||
1511 | /* | |
1512 | * If we no longer need to hold onto old our caps, and we may | |
1513 | * have cached pages, but don't want them, then try to invalidate. | |
1514 | * If we fail, it's because pages are locked.... try again later. | |
1515 | */ | |
1516 | if ((!is_delayed || mdsc->stopping) && | |
1517 | ci->i_wrbuffer_ref == 0 && /* no dirty pages... */ | |
1518 | ci->i_rdcache_gen && /* may have cached pages */ | |
cbd03635 | 1519 | (file_wanted == 0 || /* no open files */ |
2962507c SW |
1520 | (revoking & (CEPH_CAP_FILE_CACHE| |
1521 | CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */ | |
a8599bd8 | 1522 | !tried_invalidate) { |
a8599bd8 | 1523 | dout("check_caps trying to invalidate on %p\n", inode); |
5ecad6fd | 1524 | if (try_nonblocking_invalidate(inode) < 0) { |
2962507c SW |
1525 | if (revoking & (CEPH_CAP_FILE_CACHE| |
1526 | CEPH_CAP_FILE_LAZYIO)) { | |
5ecad6fd SW |
1527 | dout("check_caps queuing invalidate\n"); |
1528 | queue_invalidate = 1; | |
1529 | ci->i_rdcache_revoking = ci->i_rdcache_gen; | |
1530 | } else { | |
1531 | dout("check_caps failed to invalidate pages\n"); | |
1532 | /* we failed to invalidate pages. check these | |
1533 | caps again later. */ | |
1534 | force_requeue = 1; | |
1535 | __cap_set_timeouts(mdsc, ci); | |
1536 | } | |
a8599bd8 SW |
1537 | } |
1538 | tried_invalidate = 1; | |
1539 | goto retry_locked; | |
1540 | } | |
1541 | ||
1542 | num = 0; | |
1543 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
1544 | cap = rb_entry(p, struct ceph_cap, ci_node); | |
1545 | num++; | |
1546 | ||
1547 | /* avoid looping forever */ | |
1548 | if (mds >= cap->mds || | |
1549 | ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) | |
1550 | continue; | |
1551 | ||
1552 | /* NOTE: no side-effects allowed, until we take s_mutex */ | |
1553 | ||
1554 | revoking = cap->implemented & ~cap->issued; | |
1555 | if (revoking) | |
cbd03635 | 1556 | dout(" mds%d revoking %s\n", cap->mds, |
a8599bd8 SW |
1557 | ceph_cap_string(revoking)); |
1558 | ||
1559 | if (cap == ci->i_auth_cap && | |
1560 | (cap->issued & CEPH_CAP_FILE_WR)) { | |
1561 | /* request larger max_size from MDS? */ | |
1562 | if (ci->i_wanted_max_size > ci->i_max_size && | |
1563 | ci->i_wanted_max_size > ci->i_requested_max_size) { | |
1564 | dout("requesting new max_size\n"); | |
1565 | goto ack; | |
1566 | } | |
1567 | ||
1568 | /* approaching file_max? */ | |
1569 | if ((inode->i_size << 1) >= ci->i_max_size && | |
1570 | (ci->i_reported_size << 1) < ci->i_max_size) { | |
1571 | dout("i_size approaching max_size\n"); | |
1572 | goto ack; | |
1573 | } | |
1574 | } | |
1575 | /* flush anything dirty? */ | |
1576 | if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) && | |
1577 | ci->i_dirty_caps) { | |
1578 | dout("flushing dirty caps\n"); | |
1579 | goto ack; | |
1580 | } | |
1581 | ||
1582 | /* completed revocation? going down and there are no caps? */ | |
1583 | if (revoking && (revoking & used) == 0) { | |
1584 | dout("completed revocation of %s\n", | |
1585 | ceph_cap_string(cap->implemented & ~cap->issued)); | |
1586 | goto ack; | |
1587 | } | |
1588 | ||
1589 | /* want more caps from mds? */ | |
1590 | if (want & ~(cap->mds_wanted | cap->issued)) | |
1591 | goto ack; | |
1592 | ||
1593 | /* things we might delay */ | |
1594 | if ((cap->issued & ~retain) == 0 && | |
1595 | cap->mds_wanted == want) | |
1596 | continue; /* nope, all good */ | |
1597 | ||
1598 | if (is_delayed) | |
1599 | goto ack; | |
1600 | ||
1601 | /* delay? */ | |
1602 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && | |
1603 | time_before(jiffies, ci->i_hold_caps_max)) { | |
1604 | dout(" delaying issued %s -> %s, wanted %s -> %s\n", | |
1605 | ceph_cap_string(cap->issued), | |
1606 | ceph_cap_string(cap->issued & retain), | |
1607 | ceph_cap_string(cap->mds_wanted), | |
1608 | ceph_cap_string(want)); | |
1609 | delayed++; | |
1610 | continue; | |
1611 | } | |
1612 | ||
1613 | ack: | |
e9964c10 SW |
1614 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
1615 | dout(" skipping %p I_NOFLUSH set\n", inode); | |
1616 | continue; | |
1617 | } | |
1618 | ||
a8599bd8 SW |
1619 | if (session && session != cap->session) { |
1620 | dout("oops, wrong session %p mutex\n", session); | |
1621 | mutex_unlock(&session->s_mutex); | |
1622 | session = NULL; | |
1623 | } | |
1624 | if (!session) { | |
1625 | session = cap->session; | |
1626 | if (mutex_trylock(&session->s_mutex) == 0) { | |
1627 | dout("inverting session/ino locks on %p\n", | |
1628 | session); | |
1629 | spin_unlock(&inode->i_lock); | |
1630 | if (took_snap_rwsem) { | |
1631 | up_read(&mdsc->snap_rwsem); | |
1632 | took_snap_rwsem = 0; | |
1633 | } | |
1634 | mutex_lock(&session->s_mutex); | |
1635 | goto retry; | |
1636 | } | |
1637 | } | |
1638 | /* take snap_rwsem after session mutex */ | |
1639 | if (!took_snap_rwsem) { | |
1640 | if (down_read_trylock(&mdsc->snap_rwsem) == 0) { | |
1641 | dout("inverting snap/in locks on %p\n", | |
1642 | inode); | |
1643 | spin_unlock(&inode->i_lock); | |
1644 | down_read(&mdsc->snap_rwsem); | |
1645 | took_snap_rwsem = 1; | |
1646 | goto retry; | |
1647 | } | |
1648 | took_snap_rwsem = 1; | |
1649 | } | |
1650 | ||
cdc35f96 SW |
1651 | if (cap == ci->i_auth_cap && ci->i_dirty_caps) |
1652 | flushing = __mark_caps_flushing(inode, session); | |
a8599bd8 SW |
1653 | |
1654 | mds = cap->mds; /* remember mds, so we don't repeat */ | |
1655 | sent++; | |
1656 | ||
1657 | /* __send_cap drops i_lock */ | |
1658 | delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want, | |
1659 | retain, flushing, NULL); | |
1660 | goto retry; /* retake i_lock and restart our cap scan. */ | |
1661 | } | |
1662 | ||
1663 | /* | |
1664 | * Reschedule delayed caps release if we delayed anything, | |
1665 | * otherwise cancel. | |
1666 | */ | |
1667 | if (delayed && is_delayed) | |
1668 | force_requeue = 1; /* __send_cap delayed release; requeue */ | |
1669 | if (!delayed && !is_delayed) | |
1670 | __cap_delay_cancel(mdsc, ci); | |
1671 | else if (!is_delayed || force_requeue) | |
1672 | __cap_delay_requeue(mdsc, ci); | |
1673 | ||
1674 | spin_unlock(&inode->i_lock); | |
1675 | ||
cbd03635 | 1676 | if (queue_invalidate) |
3c6f6b79 | 1677 | ceph_queue_invalidate(inode); |
cbd03635 | 1678 | |
cdc2ce05 | 1679 | if (session) |
a8599bd8 SW |
1680 | mutex_unlock(&session->s_mutex); |
1681 | if (took_snap_rwsem) | |
1682 | up_read(&mdsc->snap_rwsem); | |
1683 | } | |
1684 | ||
a8599bd8 SW |
1685 | /* |
1686 | * Try to flush dirty caps back to the auth mds. | |
1687 | */ | |
1688 | static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session, | |
1689 | unsigned *flush_tid) | |
1690 | { | |
640ef79d | 1691 | struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; |
a8599bd8 SW |
1692 | struct ceph_inode_info *ci = ceph_inode(inode); |
1693 | int unlock_session = session ? 0 : 1; | |
1694 | int flushing = 0; | |
1695 | ||
1696 | retry: | |
1697 | spin_lock(&inode->i_lock); | |
e9964c10 SW |
1698 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
1699 | dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); | |
1700 | goto out; | |
1701 | } | |
a8599bd8 SW |
1702 | if (ci->i_dirty_caps && ci->i_auth_cap) { |
1703 | struct ceph_cap *cap = ci->i_auth_cap; | |
1704 | int used = __ceph_caps_used(ci); | |
1705 | int want = __ceph_caps_wanted(ci); | |
1706 | int delayed; | |
1707 | ||
1708 | if (!session) { | |
1709 | spin_unlock(&inode->i_lock); | |
1710 | session = cap->session; | |
1711 | mutex_lock(&session->s_mutex); | |
1712 | goto retry; | |
1713 | } | |
1714 | BUG_ON(session != cap->session); | |
1715 | if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) | |
1716 | goto out; | |
1717 | ||
cdc35f96 | 1718 | flushing = __mark_caps_flushing(inode, session); |
a8599bd8 SW |
1719 | |
1720 | /* __send_cap drops i_lock */ | |
1721 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want, | |
1722 | cap->issued | cap->implemented, flushing, | |
1723 | flush_tid); | |
1724 | if (!delayed) | |
1725 | goto out_unlocked; | |
1726 | ||
1727 | spin_lock(&inode->i_lock); | |
1728 | __cap_delay_requeue(mdsc, ci); | |
1729 | } | |
1730 | out: | |
1731 | spin_unlock(&inode->i_lock); | |
1732 | out_unlocked: | |
1733 | if (session && unlock_session) | |
1734 | mutex_unlock(&session->s_mutex); | |
1735 | return flushing; | |
1736 | } | |
1737 | ||
1738 | /* | |
1739 | * Return true if we've flushed caps through the given flush_tid. | |
1740 | */ | |
1741 | static int caps_are_flushed(struct inode *inode, unsigned tid) | |
1742 | { | |
1743 | struct ceph_inode_info *ci = ceph_inode(inode); | |
a5ee751c | 1744 | int i, ret = 1; |
a8599bd8 SW |
1745 | |
1746 | spin_lock(&inode->i_lock); | |
a8599bd8 SW |
1747 | for (i = 0; i < CEPH_CAP_BITS; i++) |
1748 | if ((ci->i_flushing_caps & (1 << i)) && | |
1749 | ci->i_cap_flush_tid[i] <= tid) { | |
1750 | /* still flushing this bit */ | |
1751 | ret = 0; | |
1752 | break; | |
1753 | } | |
1754 | spin_unlock(&inode->i_lock); | |
1755 | return ret; | |
1756 | } | |
1757 | ||
1758 | /* | |
1759 | * Wait on any unsafe replies for the given inode. First wait on the | |
1760 | * newest request, and make that the upper bound. Then, if there are | |
1761 | * more requests, keep waiting on the oldest as long as it is still older | |
1762 | * than the original request. | |
1763 | */ | |
1764 | static void sync_write_wait(struct inode *inode) | |
1765 | { | |
1766 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1767 | struct list_head *head = &ci->i_unsafe_writes; | |
1768 | struct ceph_osd_request *req; | |
1769 | u64 last_tid; | |
1770 | ||
1771 | spin_lock(&ci->i_unsafe_lock); | |
1772 | if (list_empty(head)) | |
1773 | goto out; | |
1774 | ||
1775 | /* set upper bound as _last_ entry in chain */ | |
1776 | req = list_entry(head->prev, struct ceph_osd_request, | |
1777 | r_unsafe_item); | |
1778 | last_tid = req->r_tid; | |
1779 | ||
1780 | do { | |
1781 | ceph_osdc_get_request(req); | |
1782 | spin_unlock(&ci->i_unsafe_lock); | |
1783 | dout("sync_write_wait on tid %llu (until %llu)\n", | |
1784 | req->r_tid, last_tid); | |
1785 | wait_for_completion(&req->r_safe_completion); | |
1786 | spin_lock(&ci->i_unsafe_lock); | |
1787 | ceph_osdc_put_request(req); | |
1788 | ||
1789 | /* | |
1790 | * from here on look at first entry in chain, since we | |
1791 | * only want to wait for anything older than last_tid | |
1792 | */ | |
1793 | if (list_empty(head)) | |
1794 | break; | |
1795 | req = list_entry(head->next, struct ceph_osd_request, | |
1796 | r_unsafe_item); | |
1797 | } while (req->r_tid < last_tid); | |
1798 | out: | |
1799 | spin_unlock(&ci->i_unsafe_lock); | |
1800 | } | |
1801 | ||
7ea80859 | 1802 | int ceph_fsync(struct file *file, int datasync) |
a8599bd8 | 1803 | { |
7ea80859 | 1804 | struct inode *inode = file->f_mapping->host; |
a8599bd8 SW |
1805 | struct ceph_inode_info *ci = ceph_inode(inode); |
1806 | unsigned flush_tid; | |
1807 | int ret; | |
1808 | int dirty; | |
1809 | ||
1810 | dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); | |
1811 | sync_write_wait(inode); | |
1812 | ||
1813 | ret = filemap_write_and_wait(inode->i_mapping); | |
1814 | if (ret < 0) | |
1815 | return ret; | |
1816 | ||
1817 | dirty = try_flush_caps(inode, NULL, &flush_tid); | |
1818 | dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); | |
1819 | ||
1820 | /* | |
1821 | * only wait on non-file metadata writeback (the mds | |
1822 | * can recover size and mtime, so we don't need to | |
1823 | * wait for that) | |
1824 | */ | |
1825 | if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { | |
1826 | dout("fsync waiting for flush_tid %u\n", flush_tid); | |
1827 | ret = wait_event_interruptible(ci->i_cap_wq, | |
1828 | caps_are_flushed(inode, flush_tid)); | |
1829 | } | |
1830 | ||
1831 | dout("fsync %p%s done\n", inode, datasync ? " datasync" : ""); | |
1832 | return ret; | |
1833 | } | |
1834 | ||
1835 | /* | |
1836 | * Flush any dirty caps back to the mds. If we aren't asked to wait, | |
1837 | * queue inode for flush but don't do so immediately, because we can | |
1838 | * get by with fewer MDS messages if we wait for data writeback to | |
1839 | * complete first. | |
1840 | */ | |
f1a3d572 | 1841 | int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) |
a8599bd8 SW |
1842 | { |
1843 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1844 | unsigned flush_tid; | |
1845 | int err = 0; | |
1846 | int dirty; | |
f1a3d572 | 1847 | int wait = wbc->sync_mode == WB_SYNC_ALL; |
a8599bd8 SW |
1848 | |
1849 | dout("write_inode %p wait=%d\n", inode, wait); | |
1850 | if (wait) { | |
1851 | dirty = try_flush_caps(inode, NULL, &flush_tid); | |
1852 | if (dirty) | |
1853 | err = wait_event_interruptible(ci->i_cap_wq, | |
1854 | caps_are_flushed(inode, flush_tid)); | |
1855 | } else { | |
640ef79d CR |
1856 | struct ceph_mds_client *mdsc = |
1857 | &ceph_sb_to_client(inode->i_sb)->mdsc; | |
a8599bd8 SW |
1858 | |
1859 | spin_lock(&inode->i_lock); | |
1860 | if (__ceph_caps_dirty(ci)) | |
1861 | __cap_delay_requeue_front(mdsc, ci); | |
1862 | spin_unlock(&inode->i_lock); | |
1863 | } | |
1864 | return err; | |
1865 | } | |
1866 | ||
1867 | /* | |
1868 | * After a recovering MDS goes active, we need to resend any caps | |
1869 | * we were flushing. | |
1870 | * | |
1871 | * Caller holds session->s_mutex. | |
1872 | */ | |
1873 | static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc, | |
1874 | struct ceph_mds_session *session) | |
1875 | { | |
1876 | struct ceph_cap_snap *capsnap; | |
1877 | ||
1878 | dout("kick_flushing_capsnaps mds%d\n", session->s_mds); | |
1879 | list_for_each_entry(capsnap, &session->s_cap_snaps_flushing, | |
1880 | flushing_item) { | |
1881 | struct ceph_inode_info *ci = capsnap->ci; | |
1882 | struct inode *inode = &ci->vfs_inode; | |
1883 | struct ceph_cap *cap; | |
1884 | ||
1885 | spin_lock(&inode->i_lock); | |
1886 | cap = ci->i_auth_cap; | |
1887 | if (cap && cap->session == session) { | |
1888 | dout("kick_flushing_caps %p cap %p capsnap %p\n", inode, | |
1889 | cap, capsnap); | |
1890 | __ceph_flush_snaps(ci, &session); | |
1891 | } else { | |
1892 | pr_err("%p auth cap %p not mds%d ???\n", inode, | |
1893 | cap, session->s_mds); | |
a8599bd8 | 1894 | } |
0b0c06d1 | 1895 | spin_unlock(&inode->i_lock); |
a8599bd8 SW |
1896 | } |
1897 | } | |
1898 | ||
1899 | void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, | |
1900 | struct ceph_mds_session *session) | |
1901 | { | |
1902 | struct ceph_inode_info *ci; | |
1903 | ||
1904 | kick_flushing_capsnaps(mdsc, session); | |
1905 | ||
1906 | dout("kick_flushing_caps mds%d\n", session->s_mds); | |
1907 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { | |
1908 | struct inode *inode = &ci->vfs_inode; | |
1909 | struct ceph_cap *cap; | |
1910 | int delayed = 0; | |
1911 | ||
1912 | spin_lock(&inode->i_lock); | |
1913 | cap = ci->i_auth_cap; | |
1914 | if (cap && cap->session == session) { | |
1915 | dout("kick_flushing_caps %p cap %p %s\n", inode, | |
1916 | cap, ceph_cap_string(ci->i_flushing_caps)); | |
1917 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, | |
1918 | __ceph_caps_used(ci), | |
1919 | __ceph_caps_wanted(ci), | |
1920 | cap->issued | cap->implemented, | |
1921 | ci->i_flushing_caps, NULL); | |
1922 | if (delayed) { | |
1923 | spin_lock(&inode->i_lock); | |
1924 | __cap_delay_requeue(mdsc, ci); | |
1925 | spin_unlock(&inode->i_lock); | |
1926 | } | |
1927 | } else { | |
1928 | pr_err("%p auth cap %p not mds%d ???\n", inode, | |
1929 | cap, session->s_mds); | |
1930 | spin_unlock(&inode->i_lock); | |
1931 | } | |
1932 | } | |
1933 | } | |
1934 | ||
1935 | ||
1936 | /* | |
1937 | * Take references to capabilities we hold, so that we don't release | |
1938 | * them to the MDS prematurely. | |
1939 | * | |
1940 | * Protected by i_lock. | |
1941 | */ | |
1942 | static void __take_cap_refs(struct ceph_inode_info *ci, int got) | |
1943 | { | |
1944 | if (got & CEPH_CAP_PIN) | |
1945 | ci->i_pin_ref++; | |
1946 | if (got & CEPH_CAP_FILE_RD) | |
1947 | ci->i_rd_ref++; | |
1948 | if (got & CEPH_CAP_FILE_CACHE) | |
1949 | ci->i_rdcache_ref++; | |
1950 | if (got & CEPH_CAP_FILE_WR) | |
1951 | ci->i_wr_ref++; | |
1952 | if (got & CEPH_CAP_FILE_BUFFER) { | |
1953 | if (ci->i_wrbuffer_ref == 0) | |
1954 | igrab(&ci->vfs_inode); | |
1955 | ci->i_wrbuffer_ref++; | |
1956 | dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n", | |
1957 | &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref); | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | /* | |
1962 | * Try to grab cap references. Specify those refs we @want, and the | |
1963 | * minimal set we @need. Also include the larger offset we are writing | |
1964 | * to (when applicable), and check against max_size here as well. | |
1965 | * Note that caller is responsible for ensuring max_size increases are | |
1966 | * requested from the MDS. | |
1967 | */ | |
1968 | static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, | |
1969 | int *got, loff_t endoff, int *check_max, int *err) | |
1970 | { | |
1971 | struct inode *inode = &ci->vfs_inode; | |
1972 | int ret = 0; | |
1973 | int have, implemented; | |
195d3ce2 | 1974 | int file_wanted; |
a8599bd8 SW |
1975 | |
1976 | dout("get_cap_refs %p need %s want %s\n", inode, | |
1977 | ceph_cap_string(need), ceph_cap_string(want)); | |
1978 | spin_lock(&inode->i_lock); | |
1979 | ||
195d3ce2 SW |
1980 | /* make sure file is actually open */ |
1981 | file_wanted = __ceph_caps_file_wanted(ci); | |
1982 | if ((file_wanted & need) == 0) { | |
1983 | dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", | |
1984 | ceph_cap_string(need), ceph_cap_string(file_wanted)); | |
a8599bd8 SW |
1985 | *err = -EBADF; |
1986 | ret = 1; | |
1987 | goto out; | |
1988 | } | |
1989 | ||
1990 | if (need & CEPH_CAP_FILE_WR) { | |
1991 | if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { | |
1992 | dout("get_cap_refs %p endoff %llu > maxsize %llu\n", | |
1993 | inode, endoff, ci->i_max_size); | |
1994 | if (endoff > ci->i_wanted_max_size) { | |
1995 | *check_max = 1; | |
1996 | ret = 1; | |
1997 | } | |
1998 | goto out; | |
1999 | } | |
2000 | /* | |
2001 | * If a sync write is in progress, we must wait, so that we | |
2002 | * can get a final snapshot value for size+mtime. | |
2003 | */ | |
2004 | if (__ceph_have_pending_cap_snap(ci)) { | |
2005 | dout("get_cap_refs %p cap_snap_pending\n", inode); | |
2006 | goto out; | |
2007 | } | |
2008 | } | |
2009 | have = __ceph_caps_issued(ci, &implemented); | |
2010 | ||
2011 | /* | |
2012 | * disallow writes while a truncate is pending | |
2013 | */ | |
2014 | if (ci->i_truncate_pending) | |
2015 | have &= ~CEPH_CAP_FILE_WR; | |
2016 | ||
2017 | if ((have & need) == need) { | |
2018 | /* | |
2019 | * Look at (implemented & ~have & not) so that we keep waiting | |
2020 | * on transition from wanted -> needed caps. This is needed | |
2021 | * for WRBUFFER|WR -> WR to avoid a new WR sync write from | |
2022 | * going before a prior buffered writeback happens. | |
2023 | */ | |
2024 | int not = want & ~(have & need); | |
2025 | int revoking = implemented & ~have; | |
2026 | dout("get_cap_refs %p have %s but not %s (revoking %s)\n", | |
2027 | inode, ceph_cap_string(have), ceph_cap_string(not), | |
2028 | ceph_cap_string(revoking)); | |
2029 | if ((revoking & not) == 0) { | |
2030 | *got = need | (have & want); | |
2031 | __take_cap_refs(ci, *got); | |
2032 | ret = 1; | |
2033 | } | |
2034 | } else { | |
2035 | dout("get_cap_refs %p have %s needed %s\n", inode, | |
2036 | ceph_cap_string(have), ceph_cap_string(need)); | |
2037 | } | |
2038 | out: | |
2039 | spin_unlock(&inode->i_lock); | |
2040 | dout("get_cap_refs %p ret %d got %s\n", inode, | |
2041 | ret, ceph_cap_string(*got)); | |
2042 | return ret; | |
2043 | } | |
2044 | ||
2045 | /* | |
2046 | * Check the offset we are writing up to against our current | |
2047 | * max_size. If necessary, tell the MDS we want to write to | |
2048 | * a larger offset. | |
2049 | */ | |
2050 | static void check_max_size(struct inode *inode, loff_t endoff) | |
2051 | { | |
2052 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2053 | int check = 0; | |
2054 | ||
2055 | /* do we need to explicitly request a larger max_size? */ | |
2056 | spin_lock(&inode->i_lock); | |
2057 | if ((endoff >= ci->i_max_size || | |
2058 | endoff > (inode->i_size << 1)) && | |
2059 | endoff > ci->i_wanted_max_size) { | |
2060 | dout("write %p at large endoff %llu, req max_size\n", | |
2061 | inode, endoff); | |
2062 | ci->i_wanted_max_size = endoff; | |
2063 | check = 1; | |
2064 | } | |
2065 | spin_unlock(&inode->i_lock); | |
2066 | if (check) | |
2067 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); | |
2068 | } | |
2069 | ||
2070 | /* | |
2071 | * Wait for caps, and take cap references. If we can't get a WR cap | |
2072 | * due to a small max_size, make sure we check_max_size (and possibly | |
2073 | * ask the mds) so we don't get hung up indefinitely. | |
2074 | */ | |
2075 | int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got, | |
2076 | loff_t endoff) | |
2077 | { | |
2078 | int check_max, ret, err; | |
2079 | ||
2080 | retry: | |
2081 | if (endoff > 0) | |
2082 | check_max_size(&ci->vfs_inode, endoff); | |
2083 | check_max = 0; | |
2084 | err = 0; | |
2085 | ret = wait_event_interruptible(ci->i_cap_wq, | |
2086 | try_get_cap_refs(ci, need, want, | |
2087 | got, endoff, | |
2088 | &check_max, &err)); | |
2089 | if (err) | |
2090 | ret = err; | |
2091 | if (check_max) | |
2092 | goto retry; | |
2093 | return ret; | |
2094 | } | |
2095 | ||
2096 | /* | |
2097 | * Take cap refs. Caller must already know we hold at least one ref | |
2098 | * on the caps in question or we don't know this is safe. | |
2099 | */ | |
2100 | void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) | |
2101 | { | |
2102 | spin_lock(&ci->vfs_inode.i_lock); | |
2103 | __take_cap_refs(ci, caps); | |
2104 | spin_unlock(&ci->vfs_inode.i_lock); | |
2105 | } | |
2106 | ||
2107 | /* | |
2108 | * Release cap refs. | |
2109 | * | |
2110 | * If we released the last ref on any given cap, call ceph_check_caps | |
2111 | * to release (or schedule a release). | |
2112 | * | |
2113 | * If we are releasing a WR cap (from a sync write), finalize any affected | |
2114 | * cap_snap, and wake up any waiters. | |
2115 | */ | |
2116 | void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) | |
2117 | { | |
2118 | struct inode *inode = &ci->vfs_inode; | |
2119 | int last = 0, put = 0, flushsnaps = 0, wake = 0; | |
2120 | struct ceph_cap_snap *capsnap; | |
2121 | ||
2122 | spin_lock(&inode->i_lock); | |
2123 | if (had & CEPH_CAP_PIN) | |
2124 | --ci->i_pin_ref; | |
2125 | if (had & CEPH_CAP_FILE_RD) | |
2126 | if (--ci->i_rd_ref == 0) | |
2127 | last++; | |
2128 | if (had & CEPH_CAP_FILE_CACHE) | |
2129 | if (--ci->i_rdcache_ref == 0) | |
2130 | last++; | |
2131 | if (had & CEPH_CAP_FILE_BUFFER) { | |
2132 | if (--ci->i_wrbuffer_ref == 0) { | |
2133 | last++; | |
2134 | put++; | |
2135 | } | |
2136 | dout("put_cap_refs %p wrbuffer %d -> %d (?)\n", | |
2137 | inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref); | |
2138 | } | |
2139 | if (had & CEPH_CAP_FILE_WR) | |
2140 | if (--ci->i_wr_ref == 0) { | |
2141 | last++; | |
2142 | if (!list_empty(&ci->i_cap_snaps)) { | |
2143 | capsnap = list_first_entry(&ci->i_cap_snaps, | |
2144 | struct ceph_cap_snap, | |
2145 | ci_item); | |
2146 | if (capsnap->writing) { | |
2147 | capsnap->writing = 0; | |
2148 | flushsnaps = | |
2149 | __ceph_finish_cap_snap(ci, | |
2150 | capsnap); | |
2151 | wake = 1; | |
2152 | } | |
2153 | } | |
2154 | } | |
2155 | spin_unlock(&inode->i_lock); | |
2156 | ||
819ccbfa SW |
2157 | dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had), |
2158 | last ? " last" : "", put ? " put" : ""); | |
a8599bd8 SW |
2159 | |
2160 | if (last && !flushsnaps) | |
2161 | ceph_check_caps(ci, 0, NULL); | |
2162 | else if (flushsnaps) | |
2163 | ceph_flush_snaps(ci); | |
2164 | if (wake) | |
03066f23 | 2165 | wake_up_all(&ci->i_cap_wq); |
a8599bd8 SW |
2166 | if (put) |
2167 | iput(inode); | |
2168 | } | |
2169 | ||
2170 | /* | |
2171 | * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap | |
2172 | * context. Adjust per-snap dirty page accounting as appropriate. | |
2173 | * Once all dirty data for a cap_snap is flushed, flush snapped file | |
2174 | * metadata back to the MDS. If we dropped the last ref, call | |
2175 | * ceph_check_caps. | |
2176 | */ | |
2177 | void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, | |
2178 | struct ceph_snap_context *snapc) | |
2179 | { | |
2180 | struct inode *inode = &ci->vfs_inode; | |
2181 | int last = 0; | |
819ccbfa SW |
2182 | int complete_capsnap = 0; |
2183 | int drop_capsnap = 0; | |
a8599bd8 SW |
2184 | int found = 0; |
2185 | struct ceph_cap_snap *capsnap = NULL; | |
2186 | ||
2187 | spin_lock(&inode->i_lock); | |
2188 | ci->i_wrbuffer_ref -= nr; | |
2189 | last = !ci->i_wrbuffer_ref; | |
2190 | ||
2191 | if (ci->i_head_snapc == snapc) { | |
2192 | ci->i_wrbuffer_ref_head -= nr; | |
2193 | if (!ci->i_wrbuffer_ref_head) { | |
2194 | ceph_put_snap_context(ci->i_head_snapc); | |
2195 | ci->i_head_snapc = NULL; | |
2196 | } | |
2197 | dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", | |
2198 | inode, | |
2199 | ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, | |
2200 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, | |
2201 | last ? " LAST" : ""); | |
2202 | } else { | |
2203 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | |
2204 | if (capsnap->context == snapc) { | |
2205 | found = 1; | |
a8599bd8 SW |
2206 | break; |
2207 | } | |
2208 | } | |
2209 | BUG_ON(!found); | |
819ccbfa SW |
2210 | capsnap->dirty_pages -= nr; |
2211 | if (capsnap->dirty_pages == 0) { | |
2212 | complete_capsnap = 1; | |
2213 | if (capsnap->dirty == 0) | |
2214 | /* cap writeback completed before we created | |
2215 | * the cap_snap; no FLUSHSNAP is needed */ | |
2216 | drop_capsnap = 1; | |
2217 | } | |
a8599bd8 | 2218 | dout("put_wrbuffer_cap_refs on %p cap_snap %p " |
819ccbfa | 2219 | " snap %lld %d/%d -> %d/%d %s%s%s\n", |
a8599bd8 SW |
2220 | inode, capsnap, capsnap->context->seq, |
2221 | ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, | |
2222 | ci->i_wrbuffer_ref, capsnap->dirty_pages, | |
2223 | last ? " (wrbuffer last)" : "", | |
819ccbfa SW |
2224 | complete_capsnap ? " (complete capsnap)" : "", |
2225 | drop_capsnap ? " (drop capsnap)" : ""); | |
2226 | if (drop_capsnap) { | |
2227 | ceph_put_snap_context(capsnap->context); | |
2228 | list_del(&capsnap->ci_item); | |
2229 | list_del(&capsnap->flushing_item); | |
2230 | ceph_put_cap_snap(capsnap); | |
2231 | } | |
a8599bd8 SW |
2232 | } |
2233 | ||
2234 | spin_unlock(&inode->i_lock); | |
2235 | ||
2236 | if (last) { | |
2237 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); | |
2238 | iput(inode); | |
819ccbfa | 2239 | } else if (complete_capsnap) { |
a8599bd8 | 2240 | ceph_flush_snaps(ci); |
03066f23 | 2241 | wake_up_all(&ci->i_cap_wq); |
a8599bd8 | 2242 | } |
819ccbfa SW |
2243 | if (drop_capsnap) |
2244 | iput(inode); | |
a8599bd8 SW |
2245 | } |
2246 | ||
2247 | /* | |
2248 | * Handle a cap GRANT message from the MDS. (Note that a GRANT may | |
2249 | * actually be a revocation if it specifies a smaller cap set.) | |
2250 | * | |
15637c8b SW |
2251 | * caller holds s_mutex and i_lock, we drop both. |
2252 | * | |
a8599bd8 SW |
2253 | * return value: |
2254 | * 0 - ok | |
2255 | * 1 - check_caps on auth cap only (writeback) | |
2256 | * 2 - check_caps (ack revoke) | |
2257 | */ | |
15637c8b SW |
2258 | static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, |
2259 | struct ceph_mds_session *session, | |
2260 | struct ceph_cap *cap, | |
2261 | struct ceph_buffer *xattr_buf) | |
cd84db6e | 2262 | __releases(inode->i_lock) |
a8599bd8 SW |
2263 | { |
2264 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2265 | int mds = session->s_mds; | |
2266 | int seq = le32_to_cpu(grant->seq); | |
2267 | int newcaps = le32_to_cpu(grant->caps); | |
2268 | int issued, implemented, used, wanted, dirty; | |
2269 | u64 size = le64_to_cpu(grant->size); | |
2270 | u64 max_size = le64_to_cpu(grant->max_size); | |
2271 | struct timespec mtime, atime, ctime; | |
15637c8b | 2272 | int check_caps = 0; |
a8599bd8 SW |
2273 | int wake = 0; |
2274 | int writeback = 0; | |
2275 | int revoked_rdcache = 0; | |
3c6f6b79 | 2276 | int queue_invalidate = 0; |
a8599bd8 SW |
2277 | |
2278 | dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", | |
2279 | inode, cap, mds, seq, ceph_cap_string(newcaps)); | |
2280 | dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, | |
2281 | inode->i_size); | |
2282 | ||
2283 | /* | |
2284 | * If CACHE is being revoked, and we have no dirty buffers, | |
2285 | * try to invalidate (once). (If there are dirty buffers, we | |
2286 | * will invalidate _after_ writeback.) | |
2287 | */ | |
3b454c49 SW |
2288 | if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && |
2289 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && | |
bcd2cbd1 | 2290 | !ci->i_wrbuffer_ref) { |
5ecad6fd SW |
2291 | if (try_nonblocking_invalidate(inode) == 0) { |
2292 | revoked_rdcache = 1; | |
2293 | } else { | |
a8599bd8 SW |
2294 | /* there were locked pages.. invalidate later |
2295 | in a separate thread. */ | |
2296 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { | |
3c6f6b79 | 2297 | queue_invalidate = 1; |
a8599bd8 SW |
2298 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
2299 | } | |
a8599bd8 | 2300 | } |
a8599bd8 SW |
2301 | } |
2302 | ||
2303 | /* side effects now are allowed */ | |
2304 | ||
2305 | issued = __ceph_caps_issued(ci, &implemented); | |
2306 | issued |= implemented | __ceph_caps_dirty(ci); | |
2307 | ||
685f9a5d | 2308 | cap->cap_gen = session->s_cap_gen; |
a8599bd8 SW |
2309 | |
2310 | __check_cap_issue(ci, cap, newcaps); | |
2311 | ||
2312 | if ((issued & CEPH_CAP_AUTH_EXCL) == 0) { | |
2313 | inode->i_mode = le32_to_cpu(grant->mode); | |
2314 | inode->i_uid = le32_to_cpu(grant->uid); | |
2315 | inode->i_gid = le32_to_cpu(grant->gid); | |
2316 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, | |
2317 | inode->i_uid, inode->i_gid); | |
2318 | } | |
2319 | ||
2320 | if ((issued & CEPH_CAP_LINK_EXCL) == 0) | |
2321 | inode->i_nlink = le32_to_cpu(grant->nlink); | |
2322 | ||
2323 | if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) { | |
2324 | int len = le32_to_cpu(grant->xattr_len); | |
2325 | u64 version = le64_to_cpu(grant->xattr_version); | |
2326 | ||
2327 | if (version > ci->i_xattrs.version) { | |
2328 | dout(" got new xattrs v%llu on %p len %d\n", | |
2329 | version, inode, len); | |
2330 | if (ci->i_xattrs.blob) | |
2331 | ceph_buffer_put(ci->i_xattrs.blob); | |
2332 | ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); | |
2333 | ci->i_xattrs.version = version; | |
2334 | } | |
2335 | } | |
2336 | ||
2337 | /* size/ctime/mtime/atime? */ | |
2338 | ceph_fill_file_size(inode, issued, | |
2339 | le32_to_cpu(grant->truncate_seq), | |
2340 | le64_to_cpu(grant->truncate_size), size); | |
2341 | ceph_decode_timespec(&mtime, &grant->mtime); | |
2342 | ceph_decode_timespec(&atime, &grant->atime); | |
2343 | ceph_decode_timespec(&ctime, &grant->ctime); | |
2344 | ceph_fill_file_time(inode, issued, | |
2345 | le32_to_cpu(grant->time_warp_seq), &ctime, &mtime, | |
2346 | &atime); | |
2347 | ||
2348 | /* max size increase? */ | |
2349 | if (max_size != ci->i_max_size) { | |
2350 | dout("max_size %lld -> %llu\n", ci->i_max_size, max_size); | |
2351 | ci->i_max_size = max_size; | |
2352 | if (max_size >= ci->i_wanted_max_size) { | |
2353 | ci->i_wanted_max_size = 0; /* reset */ | |
2354 | ci->i_requested_max_size = 0; | |
2355 | } | |
2356 | wake = 1; | |
2357 | } | |
2358 | ||
2359 | /* check cap bits */ | |
2360 | wanted = __ceph_caps_wanted(ci); | |
2361 | used = __ceph_caps_used(ci); | |
2362 | dirty = __ceph_caps_dirty(ci); | |
2363 | dout(" my wanted = %s, used = %s, dirty %s\n", | |
2364 | ceph_cap_string(wanted), | |
2365 | ceph_cap_string(used), | |
2366 | ceph_cap_string(dirty)); | |
2367 | if (wanted != le32_to_cpu(grant->wanted)) { | |
2368 | dout("mds wanted %s -> %s\n", | |
2369 | ceph_cap_string(le32_to_cpu(grant->wanted)), | |
2370 | ceph_cap_string(wanted)); | |
2371 | grant->wanted = cpu_to_le32(wanted); | |
2372 | } | |
2373 | ||
2374 | cap->seq = seq; | |
2375 | ||
2376 | /* file layout may have changed */ | |
2377 | ci->i_layout = grant->layout; | |
2378 | ||
2379 | /* revocation, grant, or no-op? */ | |
2380 | if (cap->issued & ~newcaps) { | |
3b454c49 SW |
2381 | int revoking = cap->issued & ~newcaps; |
2382 | ||
2383 | dout("revocation: %s -> %s (revoking %s)\n", | |
2384 | ceph_cap_string(cap->issued), | |
2385 | ceph_cap_string(newcaps), | |
2386 | ceph_cap_string(revoking)); | |
0eb6cd49 | 2387 | if (revoking & used & CEPH_CAP_FILE_BUFFER) |
3b454c49 SW |
2388 | writeback = 1; /* initiate writeback; will delay ack */ |
2389 | else if (revoking == CEPH_CAP_FILE_CACHE && | |
2390 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && | |
2391 | queue_invalidate) | |
2392 | ; /* do nothing yet, invalidation will be queued */ | |
2393 | else if (cap == ci->i_auth_cap) | |
2394 | check_caps = 1; /* check auth cap only */ | |
2395 | else | |
2396 | check_caps = 2; /* check all caps */ | |
a8599bd8 | 2397 | cap->issued = newcaps; |
978097c9 | 2398 | cap->implemented |= newcaps; |
a8599bd8 SW |
2399 | } else if (cap->issued == newcaps) { |
2400 | dout("caps unchanged: %s -> %s\n", | |
2401 | ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); | |
2402 | } else { | |
2403 | dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), | |
2404 | ceph_cap_string(newcaps)); | |
2405 | cap->issued = newcaps; | |
2406 | cap->implemented |= newcaps; /* add bits only, to | |
2407 | * avoid stepping on a | |
2408 | * pending revocation */ | |
2409 | wake = 1; | |
2410 | } | |
978097c9 | 2411 | BUG_ON(cap->issued & ~cap->implemented); |
a8599bd8 SW |
2412 | |
2413 | spin_unlock(&inode->i_lock); | |
3c6f6b79 | 2414 | if (writeback) |
a8599bd8 SW |
2415 | /* |
2416 | * queue inode for writeback: we can't actually call | |
2417 | * filemap_write_and_wait, etc. from message handler | |
2418 | * context. | |
2419 | */ | |
3c6f6b79 SW |
2420 | ceph_queue_writeback(inode); |
2421 | if (queue_invalidate) | |
2422 | ceph_queue_invalidate(inode); | |
a8599bd8 | 2423 | if (wake) |
03066f23 | 2424 | wake_up_all(&ci->i_cap_wq); |
15637c8b SW |
2425 | |
2426 | if (check_caps == 1) | |
2427 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, | |
2428 | session); | |
2429 | else if (check_caps == 2) | |
2430 | ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); | |
2431 | else | |
2432 | mutex_unlock(&session->s_mutex); | |
a8599bd8 SW |
2433 | } |
2434 | ||
2435 | /* | |
2436 | * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the | |
2437 | * MDS has been safely committed. | |
2438 | */ | |
6df058c0 | 2439 | static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, |
a8599bd8 SW |
2440 | struct ceph_mds_caps *m, |
2441 | struct ceph_mds_session *session, | |
2442 | struct ceph_cap *cap) | |
2443 | __releases(inode->i_lock) | |
2444 | { | |
2445 | struct ceph_inode_info *ci = ceph_inode(inode); | |
640ef79d | 2446 | struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; |
a8599bd8 SW |
2447 | unsigned seq = le32_to_cpu(m->seq); |
2448 | int dirty = le32_to_cpu(m->dirty); | |
2449 | int cleaned = 0; | |
afcdaea3 | 2450 | int drop = 0; |
a8599bd8 SW |
2451 | int i; |
2452 | ||
2453 | for (i = 0; i < CEPH_CAP_BITS; i++) | |
2454 | if ((dirty & (1 << i)) && | |
2455 | flush_tid == ci->i_cap_flush_tid[i]) | |
2456 | cleaned |= 1 << i; | |
2457 | ||
2458 | dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," | |
2459 | " flushing %s -> %s\n", | |
2460 | inode, session->s_mds, seq, ceph_cap_string(dirty), | |
2461 | ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), | |
2462 | ceph_cap_string(ci->i_flushing_caps & ~cleaned)); | |
2463 | ||
2464 | if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned)) | |
2465 | goto out; | |
2466 | ||
a8599bd8 | 2467 | ci->i_flushing_caps &= ~cleaned; |
a8599bd8 SW |
2468 | |
2469 | spin_lock(&mdsc->cap_dirty_lock); | |
2470 | if (ci->i_flushing_caps == 0) { | |
2471 | list_del_init(&ci->i_flushing_item); | |
2472 | if (!list_empty(&session->s_cap_flushing)) | |
2473 | dout(" mds%d still flushing cap on %p\n", | |
2474 | session->s_mds, | |
2475 | &list_entry(session->s_cap_flushing.next, | |
2476 | struct ceph_inode_info, | |
2477 | i_flushing_item)->vfs_inode); | |
2478 | mdsc->num_cap_flushing--; | |
03066f23 | 2479 | wake_up_all(&mdsc->cap_flushing_wq); |
a8599bd8 | 2480 | dout(" inode %p now !flushing\n", inode); |
afcdaea3 SW |
2481 | |
2482 | if (ci->i_dirty_caps == 0) { | |
2483 | dout(" inode %p now clean\n", inode); | |
2484 | BUG_ON(!list_empty(&ci->i_dirty_item)); | |
2485 | drop = 1; | |
76e3b390 SW |
2486 | } else { |
2487 | BUG_ON(list_empty(&ci->i_dirty_item)); | |
afcdaea3 | 2488 | } |
a8599bd8 SW |
2489 | } |
2490 | spin_unlock(&mdsc->cap_dirty_lock); | |
03066f23 | 2491 | wake_up_all(&ci->i_cap_wq); |
a8599bd8 SW |
2492 | |
2493 | out: | |
2494 | spin_unlock(&inode->i_lock); | |
afcdaea3 | 2495 | if (drop) |
a8599bd8 SW |
2496 | iput(inode); |
2497 | } | |
2498 | ||
2499 | /* | |
2500 | * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can | |
2501 | * throw away our cap_snap. | |
2502 | * | |
2503 | * Caller hold s_mutex. | |
2504 | */ | |
6df058c0 | 2505 | static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, |
a8599bd8 SW |
2506 | struct ceph_mds_caps *m, |
2507 | struct ceph_mds_session *session) | |
2508 | { | |
2509 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2510 | u64 follows = le64_to_cpu(m->snap_follows); | |
a8599bd8 SW |
2511 | struct ceph_cap_snap *capsnap; |
2512 | int drop = 0; | |
2513 | ||
2514 | dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", | |
2515 | inode, ci, session->s_mds, follows); | |
2516 | ||
2517 | spin_lock(&inode->i_lock); | |
2518 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | |
2519 | if (capsnap->follows == follows) { | |
2520 | if (capsnap->flush_tid != flush_tid) { | |
2521 | dout(" cap_snap %p follows %lld tid %lld !=" | |
2522 | " %lld\n", capsnap, follows, | |
2523 | flush_tid, capsnap->flush_tid); | |
2524 | break; | |
2525 | } | |
2526 | WARN_ON(capsnap->dirty_pages || capsnap->writing); | |
819ccbfa SW |
2527 | dout(" removing %p cap_snap %p follows %lld\n", |
2528 | inode, capsnap, follows); | |
a8599bd8 SW |
2529 | ceph_put_snap_context(capsnap->context); |
2530 | list_del(&capsnap->ci_item); | |
2531 | list_del(&capsnap->flushing_item); | |
2532 | ceph_put_cap_snap(capsnap); | |
2533 | drop = 1; | |
2534 | break; | |
2535 | } else { | |
2536 | dout(" skipping cap_snap %p follows %lld\n", | |
2537 | capsnap, capsnap->follows); | |
2538 | } | |
2539 | } | |
2540 | spin_unlock(&inode->i_lock); | |
2541 | if (drop) | |
2542 | iput(inode); | |
2543 | } | |
2544 | ||
2545 | /* | |
2546 | * Handle TRUNC from MDS, indicating file truncation. | |
2547 | * | |
2548 | * caller hold s_mutex. | |
2549 | */ | |
2550 | static void handle_cap_trunc(struct inode *inode, | |
2551 | struct ceph_mds_caps *trunc, | |
2552 | struct ceph_mds_session *session) | |
2553 | __releases(inode->i_lock) | |
2554 | { | |
2555 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2556 | int mds = session->s_mds; | |
2557 | int seq = le32_to_cpu(trunc->seq); | |
2558 | u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); | |
2559 | u64 truncate_size = le64_to_cpu(trunc->truncate_size); | |
2560 | u64 size = le64_to_cpu(trunc->size); | |
2561 | int implemented = 0; | |
2562 | int dirty = __ceph_caps_dirty(ci); | |
2563 | int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); | |
2564 | int queue_trunc = 0; | |
2565 | ||
2566 | issued |= implemented | dirty; | |
2567 | ||
2568 | dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", | |
2569 | inode, mds, seq, truncate_size, truncate_seq); | |
2570 | queue_trunc = ceph_fill_file_size(inode, issued, | |
2571 | truncate_seq, truncate_size, size); | |
2572 | spin_unlock(&inode->i_lock); | |
2573 | ||
2574 | if (queue_trunc) | |
3c6f6b79 | 2575 | ceph_queue_vmtruncate(inode); |
a8599bd8 SW |
2576 | } |
2577 | ||
2578 | /* | |
2579 | * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a | |
2580 | * different one. If we are the most recent migration we've seen (as | |
2581 | * indicated by mseq), make note of the migrating cap bits for the | |
2582 | * duration (until we see the corresponding IMPORT). | |
2583 | * | |
2584 | * caller holds s_mutex | |
2585 | */ | |
2586 | static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, | |
154f42c2 SW |
2587 | struct ceph_mds_session *session, |
2588 | int *open_target_sessions) | |
a8599bd8 SW |
2589 | { |
2590 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2591 | int mds = session->s_mds; | |
2592 | unsigned mseq = le32_to_cpu(ex->migrate_seq); | |
2593 | struct ceph_cap *cap = NULL, *t; | |
2594 | struct rb_node *p; | |
2595 | int remember = 1; | |
2596 | ||
2597 | dout("handle_cap_export inode %p ci %p mds%d mseq %d\n", | |
2598 | inode, ci, mds, mseq); | |
2599 | ||
2600 | spin_lock(&inode->i_lock); | |
2601 | ||
2602 | /* make sure we haven't seen a higher mseq */ | |
2603 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | |
2604 | t = rb_entry(p, struct ceph_cap, ci_node); | |
2605 | if (ceph_seq_cmp(t->mseq, mseq) > 0) { | |
2606 | dout(" higher mseq on cap from mds%d\n", | |
2607 | t->session->s_mds); | |
2608 | remember = 0; | |
2609 | } | |
2610 | if (t->session->s_mds == mds) | |
2611 | cap = t; | |
2612 | } | |
2613 | ||
2614 | if (cap) { | |
2615 | if (remember) { | |
2616 | /* make note */ | |
2617 | ci->i_cap_exporting_mds = mds; | |
2618 | ci->i_cap_exporting_mseq = mseq; | |
2619 | ci->i_cap_exporting_issued = cap->issued; | |
154f42c2 SW |
2620 | |
2621 | /* | |
2622 | * make sure we have open sessions with all possible | |
2623 | * export targets, so that we get the matching IMPORT | |
2624 | */ | |
2625 | *open_target_sessions = 1; | |
a8599bd8 | 2626 | } |
7c1332b8 | 2627 | __ceph_remove_cap(cap); |
a8599bd8 | 2628 | } |
4ea0043a | 2629 | /* else, we already released it */ |
a8599bd8 SW |
2630 | |
2631 | spin_unlock(&inode->i_lock); | |
2632 | } | |
2633 | ||
2634 | /* | |
2635 | * Handle cap IMPORT. If there are temp bits from an older EXPORT, | |
2636 | * clean them up. | |
2637 | * | |
2638 | * caller holds s_mutex. | |
2639 | */ | |
2640 | static void handle_cap_import(struct ceph_mds_client *mdsc, | |
2641 | struct inode *inode, struct ceph_mds_caps *im, | |
2642 | struct ceph_mds_session *session, | |
2643 | void *snaptrace, int snaptrace_len) | |
2644 | { | |
2645 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2646 | int mds = session->s_mds; | |
2647 | unsigned issued = le32_to_cpu(im->caps); | |
2648 | unsigned wanted = le32_to_cpu(im->wanted); | |
2649 | unsigned seq = le32_to_cpu(im->seq); | |
2650 | unsigned mseq = le32_to_cpu(im->migrate_seq); | |
2651 | u64 realmino = le64_to_cpu(im->realm); | |
2652 | u64 cap_id = le64_to_cpu(im->cap_id); | |
2653 | ||
2654 | if (ci->i_cap_exporting_mds >= 0 && | |
2655 | ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) { | |
2656 | dout("handle_cap_import inode %p ci %p mds%d mseq %d" | |
2657 | " - cleared exporting from mds%d\n", | |
2658 | inode, ci, mds, mseq, | |
2659 | ci->i_cap_exporting_mds); | |
2660 | ci->i_cap_exporting_issued = 0; | |
2661 | ci->i_cap_exporting_mseq = 0; | |
2662 | ci->i_cap_exporting_mds = -1; | |
2663 | } else { | |
2664 | dout("handle_cap_import inode %p ci %p mds%d mseq %d\n", | |
2665 | inode, ci, mds, mseq); | |
2666 | } | |
2667 | ||
2668 | down_write(&mdsc->snap_rwsem); | |
2669 | ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len, | |
2670 | false); | |
2671 | downgrade_write(&mdsc->snap_rwsem); | |
2672 | ceph_add_cap(inode, session, cap_id, -1, | |
2673 | issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH, | |
2674 | NULL /* no caps context */); | |
2675 | try_flush_caps(inode, session, NULL); | |
2676 | up_read(&mdsc->snap_rwsem); | |
2677 | } | |
2678 | ||
2679 | /* | |
2680 | * Handle a caps message from the MDS. | |
2681 | * | |
2682 | * Identify the appropriate session, inode, and call the right handler | |
2683 | * based on the cap op. | |
2684 | */ | |
2685 | void ceph_handle_caps(struct ceph_mds_session *session, | |
2686 | struct ceph_msg *msg) | |
2687 | { | |
2688 | struct ceph_mds_client *mdsc = session->s_mdsc; | |
2689 | struct super_block *sb = mdsc->client->sb; | |
2690 | struct inode *inode; | |
2691 | struct ceph_cap *cap; | |
2692 | struct ceph_mds_caps *h; | |
2600d2dd | 2693 | int mds = session->s_mds; |
a8599bd8 | 2694 | int op; |
3d7ded4d | 2695 | u32 seq, mseq; |
a8599bd8 SW |
2696 | struct ceph_vino vino; |
2697 | u64 cap_id; | |
2698 | u64 size, max_size; | |
6df058c0 | 2699 | u64 tid; |
70edb55b | 2700 | void *snaptrace; |
ce1fbc8d SW |
2701 | size_t snaptrace_len; |
2702 | void *flock; | |
2703 | u32 flock_len; | |
154f42c2 | 2704 | int open_target_sessions = 0; |
a8599bd8 SW |
2705 | |
2706 | dout("handle_caps from mds%d\n", mds); | |
2707 | ||
2708 | /* decode */ | |
6df058c0 | 2709 | tid = le64_to_cpu(msg->hdr.tid); |
a8599bd8 SW |
2710 | if (msg->front.iov_len < sizeof(*h)) |
2711 | goto bad; | |
2712 | h = msg->front.iov_base; | |
2713 | op = le32_to_cpu(h->op); | |
2714 | vino.ino = le64_to_cpu(h->ino); | |
2715 | vino.snap = CEPH_NOSNAP; | |
2716 | cap_id = le64_to_cpu(h->cap_id); | |
2717 | seq = le32_to_cpu(h->seq); | |
3d7ded4d | 2718 | mseq = le32_to_cpu(h->migrate_seq); |
a8599bd8 SW |
2719 | size = le64_to_cpu(h->size); |
2720 | max_size = le64_to_cpu(h->max_size); | |
2721 | ||
ce1fbc8d SW |
2722 | snaptrace = h + 1; |
2723 | snaptrace_len = le32_to_cpu(h->snap_trace_len); | |
2724 | ||
2725 | if (le16_to_cpu(msg->hdr.version) >= 2) { | |
2726 | void *p, *end; | |
2727 | ||
2728 | p = snaptrace + snaptrace_len; | |
2729 | end = msg->front.iov_base + msg->front.iov_len; | |
2730 | ceph_decode_32_safe(&p, end, flock_len, bad); | |
2731 | flock = p; | |
2732 | } else { | |
2733 | flock = NULL; | |
2734 | flock_len = 0; | |
2735 | } | |
2736 | ||
a8599bd8 SW |
2737 | mutex_lock(&session->s_mutex); |
2738 | session->s_seq++; | |
2739 | dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, | |
2740 | (unsigned)seq); | |
2741 | ||
2742 | /* lookup ino */ | |
2743 | inode = ceph_find_inode(sb, vino); | |
2744 | dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, | |
2745 | vino.snap, inode); | |
2746 | if (!inode) { | |
2747 | dout(" i don't have ino %llx\n", vino.ino); | |
3d7ded4d SW |
2748 | |
2749 | if (op == CEPH_CAP_OP_IMPORT) | |
2750 | __queue_cap_release(session, vino.ino, cap_id, | |
2751 | mseq, seq); | |
2752 | ||
2753 | /* | |
2754 | * send any full release message to try to move things | |
2755 | * along for the mds (who clearly thinks we still have this | |
2756 | * cap). | |
2757 | */ | |
ee6b272b | 2758 | ceph_add_cap_releases(mdsc, session); |
3d7ded4d | 2759 | ceph_send_cap_releases(mdsc, session); |
a8599bd8 SW |
2760 | goto done; |
2761 | } | |
2762 | ||
2763 | /* these will work even if we don't have a cap yet */ | |
2764 | switch (op) { | |
2765 | case CEPH_CAP_OP_FLUSHSNAP_ACK: | |
6df058c0 | 2766 | handle_cap_flushsnap_ack(inode, tid, h, session); |
a8599bd8 SW |
2767 | goto done; |
2768 | ||
2769 | case CEPH_CAP_OP_EXPORT: | |
154f42c2 | 2770 | handle_cap_export(inode, h, session, &open_target_sessions); |
a8599bd8 SW |
2771 | goto done; |
2772 | ||
2773 | case CEPH_CAP_OP_IMPORT: | |
2774 | handle_cap_import(mdsc, inode, h, session, | |
ce1fbc8d | 2775 | snaptrace, snaptrace_len); |
15637c8b SW |
2776 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY, |
2777 | session); | |
2778 | goto done_unlocked; | |
a8599bd8 SW |
2779 | } |
2780 | ||
2781 | /* the rest require a cap */ | |
2782 | spin_lock(&inode->i_lock); | |
2783 | cap = __get_cap_for_mds(ceph_inode(inode), mds); | |
2784 | if (!cap) { | |
9dbd412f | 2785 | dout(" no cap on %p ino %llx.%llx from mds%d\n", |
a8599bd8 SW |
2786 | inode, ceph_ino(inode), ceph_snap(inode), mds); |
2787 | spin_unlock(&inode->i_lock); | |
2788 | goto done; | |
2789 | } | |
2790 | ||
2791 | /* note that each of these drops i_lock for us */ | |
2792 | switch (op) { | |
2793 | case CEPH_CAP_OP_REVOKE: | |
2794 | case CEPH_CAP_OP_GRANT: | |
15637c8b SW |
2795 | handle_cap_grant(inode, h, session, cap, msg->middle); |
2796 | goto done_unlocked; | |
a8599bd8 SW |
2797 | |
2798 | case CEPH_CAP_OP_FLUSH_ACK: | |
6df058c0 | 2799 | handle_cap_flush_ack(inode, tid, h, session, cap); |
a8599bd8 SW |
2800 | break; |
2801 | ||
2802 | case CEPH_CAP_OP_TRUNC: | |
2803 | handle_cap_trunc(inode, h, session); | |
2804 | break; | |
2805 | ||
2806 | default: | |
2807 | spin_unlock(&inode->i_lock); | |
2808 | pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, | |
2809 | ceph_cap_op_name(op)); | |
2810 | } | |
2811 | ||
2812 | done: | |
15637c8b SW |
2813 | mutex_unlock(&session->s_mutex); |
2814 | done_unlocked: | |
a8599bd8 SW |
2815 | if (inode) |
2816 | iput(inode); | |
154f42c2 SW |
2817 | if (open_target_sessions) |
2818 | ceph_mdsc_open_export_target_sessions(mdsc, session); | |
a8599bd8 SW |
2819 | return; |
2820 | ||
2821 | bad: | |
2822 | pr_err("ceph_handle_caps: corrupt message\n"); | |
9ec7cab1 | 2823 | ceph_msg_dump(msg); |
a8599bd8 SW |
2824 | return; |
2825 | } | |
2826 | ||
2827 | /* | |
2828 | * Delayed work handler to process end of delayed cap release LRU list. | |
2829 | */ | |
afcdaea3 | 2830 | void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) |
a8599bd8 SW |
2831 | { |
2832 | struct ceph_inode_info *ci; | |
2833 | int flags = CHECK_CAPS_NODELAY; | |
2834 | ||
a8599bd8 SW |
2835 | dout("check_delayed_caps\n"); |
2836 | while (1) { | |
2837 | spin_lock(&mdsc->cap_delay_lock); | |
2838 | if (list_empty(&mdsc->cap_delay_list)) | |
2839 | break; | |
2840 | ci = list_first_entry(&mdsc->cap_delay_list, | |
2841 | struct ceph_inode_info, | |
2842 | i_cap_delay_list); | |
2843 | if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && | |
2844 | time_before(jiffies, ci->i_hold_caps_max)) | |
2845 | break; | |
2846 | list_del_init(&ci->i_cap_delay_list); | |
2847 | spin_unlock(&mdsc->cap_delay_lock); | |
2848 | dout("check_delayed_caps on %p\n", &ci->vfs_inode); | |
2849 | ceph_check_caps(ci, flags, NULL); | |
2850 | } | |
2851 | spin_unlock(&mdsc->cap_delay_lock); | |
2852 | } | |
2853 | ||
afcdaea3 SW |
2854 | /* |
2855 | * Flush all dirty caps to the mds | |
2856 | */ | |
2857 | void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) | |
2858 | { | |
e9964c10 SW |
2859 | struct ceph_inode_info *ci, *nci = NULL; |
2860 | struct inode *inode, *ninode = NULL; | |
2861 | struct list_head *p, *n; | |
afcdaea3 SW |
2862 | |
2863 | dout("flush_dirty_caps\n"); | |
2864 | spin_lock(&mdsc->cap_dirty_lock); | |
e9964c10 SW |
2865 | list_for_each_safe(p, n, &mdsc->cap_dirty) { |
2866 | if (nci) { | |
2867 | ci = nci; | |
2868 | inode = ninode; | |
2869 | ci->i_ceph_flags &= ~CEPH_I_NOFLUSH; | |
2870 | dout("flush_dirty_caps inode %p (was next inode)\n", | |
2871 | inode); | |
2872 | } else { | |
2873 | ci = list_entry(p, struct ceph_inode_info, | |
2874 | i_dirty_item); | |
2875 | inode = igrab(&ci->vfs_inode); | |
2876 | BUG_ON(!inode); | |
2877 | dout("flush_dirty_caps inode %p\n", inode); | |
2878 | } | |
2879 | if (n != &mdsc->cap_dirty) { | |
2880 | nci = list_entry(n, struct ceph_inode_info, | |
2881 | i_dirty_item); | |
2882 | ninode = igrab(&nci->vfs_inode); | |
2883 | BUG_ON(!ninode); | |
2884 | nci->i_ceph_flags |= CEPH_I_NOFLUSH; | |
2885 | dout("flush_dirty_caps next inode %p, noflush\n", | |
2886 | ninode); | |
2887 | } else { | |
2888 | nci = NULL; | |
2889 | ninode = NULL; | |
2890 | } | |
afcdaea3 SW |
2891 | spin_unlock(&mdsc->cap_dirty_lock); |
2892 | if (inode) { | |
2893 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, | |
2894 | NULL); | |
2895 | iput(inode); | |
2896 | } | |
2897 | spin_lock(&mdsc->cap_dirty_lock); | |
2898 | } | |
2899 | spin_unlock(&mdsc->cap_dirty_lock); | |
2900 | } | |
2901 | ||
a8599bd8 SW |
2902 | /* |
2903 | * Drop open file reference. If we were the last open file, | |
2904 | * we may need to release capabilities to the MDS (or schedule | |
2905 | * their delayed release). | |
2906 | */ | |
2907 | void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) | |
2908 | { | |
2909 | struct inode *inode = &ci->vfs_inode; | |
2910 | int last = 0; | |
2911 | ||
2912 | spin_lock(&inode->i_lock); | |
2913 | dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode, | |
2914 | ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1); | |
2915 | BUG_ON(ci->i_nr_by_mode[fmode] == 0); | |
2916 | if (--ci->i_nr_by_mode[fmode] == 0) | |
2917 | last++; | |
2918 | spin_unlock(&inode->i_lock); | |
2919 | ||
2920 | if (last && ci->i_vino.snap == CEPH_NOSNAP) | |
2921 | ceph_check_caps(ci, 0, NULL); | |
2922 | } | |
2923 | ||
2924 | /* | |
2925 | * Helpers for embedding cap and dentry lease releases into mds | |
2926 | * requests. | |
2927 | * | |
2928 | * @force is used by dentry_release (below) to force inclusion of a | |
2929 | * record for the directory inode, even when there aren't any caps to | |
2930 | * drop. | |
2931 | */ | |
2932 | int ceph_encode_inode_release(void **p, struct inode *inode, | |
2933 | int mds, int drop, int unless, int force) | |
2934 | { | |
2935 | struct ceph_inode_info *ci = ceph_inode(inode); | |
2936 | struct ceph_cap *cap; | |
2937 | struct ceph_mds_request_release *rel = *p; | |
ec97f88b | 2938 | int used, dirty; |
a8599bd8 | 2939 | int ret = 0; |
a8599bd8 SW |
2940 | |
2941 | spin_lock(&inode->i_lock); | |
916623da | 2942 | used = __ceph_caps_used(ci); |
ec97f88b | 2943 | dirty = __ceph_caps_dirty(ci); |
916623da | 2944 | |
ec97f88b SW |
2945 | dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n", |
2946 | inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop), | |
916623da SW |
2947 | ceph_cap_string(unless)); |
2948 | ||
ec97f88b SW |
2949 | /* only drop unused, clean caps */ |
2950 | drop &= ~(used | dirty); | |
916623da | 2951 | |
a8599bd8 SW |
2952 | cap = __get_cap_for_mds(ci, mds); |
2953 | if (cap && __cap_is_valid(cap)) { | |
2954 | if (force || | |
2955 | ((cap->issued & drop) && | |
2956 | (cap->issued & unless) == 0)) { | |
2957 | if ((cap->issued & drop) && | |
2958 | (cap->issued & unless) == 0) { | |
2959 | dout("encode_inode_release %p cap %p %s -> " | |
2960 | "%s\n", inode, cap, | |
2961 | ceph_cap_string(cap->issued), | |
2962 | ceph_cap_string(cap->issued & ~drop)); | |
2963 | cap->issued &= ~drop; | |
2964 | cap->implemented &= ~drop; | |
2965 | if (ci->i_ceph_flags & CEPH_I_NODELAY) { | |
2966 | int wanted = __ceph_caps_wanted(ci); | |
2967 | dout(" wanted %s -> %s (act %s)\n", | |
2968 | ceph_cap_string(cap->mds_wanted), | |
2969 | ceph_cap_string(cap->mds_wanted & | |
2970 | ~wanted), | |
2971 | ceph_cap_string(wanted)); | |
2972 | cap->mds_wanted &= wanted; | |
2973 | } | |
2974 | } else { | |
2975 | dout("encode_inode_release %p cap %p %s" | |
2976 | " (force)\n", inode, cap, | |
2977 | ceph_cap_string(cap->issued)); | |
2978 | } | |
2979 | ||
2980 | rel->ino = cpu_to_le64(ceph_ino(inode)); | |
2981 | rel->cap_id = cpu_to_le64(cap->cap_id); | |
2982 | rel->seq = cpu_to_le32(cap->seq); | |
2983 | rel->issue_seq = cpu_to_le32(cap->issue_seq), | |
2984 | rel->mseq = cpu_to_le32(cap->mseq); | |
2985 | rel->caps = cpu_to_le32(cap->issued); | |
2986 | rel->wanted = cpu_to_le32(cap->mds_wanted); | |
2987 | rel->dname_len = 0; | |
2988 | rel->dname_seq = 0; | |
2989 | *p += sizeof(*rel); | |
2990 | ret = 1; | |
2991 | } else { | |
2992 | dout("encode_inode_release %p cap %p %s\n", | |
2993 | inode, cap, ceph_cap_string(cap->issued)); | |
2994 | } | |
2995 | } | |
2996 | spin_unlock(&inode->i_lock); | |
2997 | return ret; | |
2998 | } | |
2999 | ||
3000 | int ceph_encode_dentry_release(void **p, struct dentry *dentry, | |
3001 | int mds, int drop, int unless) | |
3002 | { | |
3003 | struct inode *dir = dentry->d_parent->d_inode; | |
3004 | struct ceph_mds_request_release *rel = *p; | |
3005 | struct ceph_dentry_info *di = ceph_dentry(dentry); | |
3006 | int force = 0; | |
3007 | int ret; | |
3008 | ||
3009 | /* | |
3010 | * force an record for the directory caps if we have a dentry lease. | |
3011 | * this is racy (can't take i_lock and d_lock together), but it | |
3012 | * doesn't have to be perfect; the mds will revoke anything we don't | |
3013 | * release. | |
3014 | */ | |
3015 | spin_lock(&dentry->d_lock); | |
3016 | if (di->lease_session && di->lease_session->s_mds == mds) | |
3017 | force = 1; | |
3018 | spin_unlock(&dentry->d_lock); | |
3019 | ||
3020 | ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); | |
3021 | ||
3022 | spin_lock(&dentry->d_lock); | |
3023 | if (ret && di->lease_session && di->lease_session->s_mds == mds) { | |
3024 | dout("encode_dentry_release %p mds%d seq %d\n", | |
3025 | dentry, mds, (int)di->lease_seq); | |
3026 | rel->dname_len = cpu_to_le32(dentry->d_name.len); | |
3027 | memcpy(*p, dentry->d_name.name, dentry->d_name.len); | |
3028 | *p += dentry->d_name.len; | |
3029 | rel->dname_seq = cpu_to_le32(di->lease_seq); | |
1dadcce3 | 3030 | __ceph_mdsc_drop_dentry_lease(dentry); |
a8599bd8 SW |
3031 | } |
3032 | spin_unlock(&dentry->d_lock); | |
3033 | return ret; | |
3034 | } |