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