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