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 #include <linux/iversion.h>
12 #include <linux/filelock.h>
13 #include <linux/jiffies.h>
14
15 #include "super.h"
16 #include "mds_client.h"
17 #include "cache.h"
18 #include "crypto.h"
19 #include <linux/ceph/decode.h>
20 #include <linux/ceph/messenger.h>
21 #include <trace/events/ceph.h>
22
23 /*
24 * Capability management
25 *
26 * The Ceph metadata servers control client access to inode metadata
27 * and file data by issuing capabilities, granting clients permission
28 * to read and/or write both inode field and file data to OSDs
29 * (storage nodes). Each capability consists of a set of bits
30 * indicating which operations are allowed.
31 *
32 * If the client holds a *_SHARED cap, the client has a coherent value
33 * that can be safely read from the cached inode.
34 *
35 * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the
36 * client is allowed to change inode attributes (e.g., file size,
37 * mtime), note its dirty state in the ceph_cap, and asynchronously
38 * flush that metadata change to the MDS.
39 *
40 * In the event of a conflicting operation (perhaps by another
41 * client), the MDS will revoke the conflicting client capabilities.
42 *
43 * In order for a client to cache an inode, it must hold a capability
44 * with at least one MDS server. When inodes are released, release
45 * notifications are batched and periodically sent en masse to the MDS
46 * cluster to release server state.
47 */
48
49 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc);
50 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
51 struct ceph_mds_session *session,
52 struct ceph_inode_info *ci,
53 u64 oldest_flush_tid);
54
55 /*
56 * Generate readable cap strings for debugging output.
57 */
58 #define MAX_CAP_STR 20
59 static char cap_str[MAX_CAP_STR][40];
60 static DEFINE_SPINLOCK(cap_str_lock);
61 static int last_cap_str;
62
gcap_string(char * s,int c)63 static char *gcap_string(char *s, int c)
64 {
65 if (c & CEPH_CAP_GSHARED)
66 *s++ = 's';
67 if (c & CEPH_CAP_GEXCL)
68 *s++ = 'x';
69 if (c & CEPH_CAP_GCACHE)
70 *s++ = 'c';
71 if (c & CEPH_CAP_GRD)
72 *s++ = 'r';
73 if (c & CEPH_CAP_GWR)
74 *s++ = 'w';
75 if (c & CEPH_CAP_GBUFFER)
76 *s++ = 'b';
77 if (c & CEPH_CAP_GWREXTEND)
78 *s++ = 'a';
79 if (c & CEPH_CAP_GLAZYIO)
80 *s++ = 'l';
81 return s;
82 }
83
ceph_cap_string(int caps)84 const char *ceph_cap_string(int caps)
85 {
86 int i;
87 char *s;
88 int c;
89
90 spin_lock(&cap_str_lock);
91 i = last_cap_str++;
92 if (last_cap_str == MAX_CAP_STR)
93 last_cap_str = 0;
94 spin_unlock(&cap_str_lock);
95
96 s = cap_str[i];
97
98 if (caps & CEPH_CAP_PIN)
99 *s++ = 'p';
100
101 c = (caps >> CEPH_CAP_SAUTH) & 3;
102 if (c) {
103 *s++ = 'A';
104 s = gcap_string(s, c);
105 }
106
107 c = (caps >> CEPH_CAP_SLINK) & 3;
108 if (c) {
109 *s++ = 'L';
110 s = gcap_string(s, c);
111 }
112
113 c = (caps >> CEPH_CAP_SXATTR) & 3;
114 if (c) {
115 *s++ = 'X';
116 s = gcap_string(s, c);
117 }
118
119 c = caps >> CEPH_CAP_SFILE;
120 if (c) {
121 *s++ = 'F';
122 s = gcap_string(s, c);
123 }
124
125 if (s == cap_str[i])
126 *s++ = '-';
127 *s = 0;
128 return cap_str[i];
129 }
130
ceph_caps_init(struct ceph_mds_client * mdsc)131 void ceph_caps_init(struct ceph_mds_client *mdsc)
132 {
133 INIT_LIST_HEAD(&mdsc->caps_list);
134 spin_lock_init(&mdsc->caps_list_lock);
135 }
136
ceph_caps_finalize(struct ceph_mds_client * mdsc)137 void ceph_caps_finalize(struct ceph_mds_client *mdsc)
138 {
139 struct ceph_cap *cap;
140
141 spin_lock(&mdsc->caps_list_lock);
142 while (!list_empty(&mdsc->caps_list)) {
143 cap = list_first_entry(&mdsc->caps_list,
144 struct ceph_cap, caps_item);
145 list_del(&cap->caps_item);
146 kmem_cache_free(ceph_cap_cachep, cap);
147 }
148 mdsc->caps_total_count = 0;
149 mdsc->caps_avail_count = 0;
150 mdsc->caps_use_count = 0;
151 mdsc->caps_reserve_count = 0;
152 mdsc->caps_min_count = 0;
153 spin_unlock(&mdsc->caps_list_lock);
154 }
155
ceph_adjust_caps_max_min(struct ceph_mds_client * mdsc,struct ceph_mount_options * fsopt)156 void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc,
157 struct ceph_mount_options *fsopt)
158 {
159 spin_lock(&mdsc->caps_list_lock);
160 mdsc->caps_min_count = fsopt->max_readdir;
161 if (mdsc->caps_min_count < 1024)
162 mdsc->caps_min_count = 1024;
163 mdsc->caps_use_max = fsopt->caps_max;
164 if (mdsc->caps_use_max > 0 &&
165 mdsc->caps_use_max < mdsc->caps_min_count)
166 mdsc->caps_use_max = mdsc->caps_min_count;
167 spin_unlock(&mdsc->caps_list_lock);
168 }
169
__ceph_unreserve_caps(struct ceph_mds_client * mdsc,int nr_caps)170 static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps)
171 {
172 struct ceph_cap *cap;
173 int i;
174
175 if (nr_caps) {
176 BUG_ON(mdsc->caps_reserve_count < nr_caps);
177 mdsc->caps_reserve_count -= nr_caps;
178 if (mdsc->caps_avail_count >=
179 mdsc->caps_reserve_count + mdsc->caps_min_count) {
180 mdsc->caps_total_count -= nr_caps;
181 for (i = 0; i < nr_caps; i++) {
182 cap = list_first_entry(&mdsc->caps_list,
183 struct ceph_cap, caps_item);
184 list_del(&cap->caps_item);
185 kmem_cache_free(ceph_cap_cachep, cap);
186 }
187 } else {
188 mdsc->caps_avail_count += nr_caps;
189 }
190
191 doutc(mdsc->fsc->client,
192 "caps %d = %d used + %d resv + %d avail\n",
193 mdsc->caps_total_count, mdsc->caps_use_count,
194 mdsc->caps_reserve_count, mdsc->caps_avail_count);
195 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
196 mdsc->caps_reserve_count +
197 mdsc->caps_avail_count);
198 }
199 }
200
201 /*
202 * Called under mdsc->mutex.
203 */
ceph_reserve_caps(struct ceph_mds_client * mdsc,struct ceph_cap_reservation * ctx,int need)204 int ceph_reserve_caps(struct ceph_mds_client *mdsc,
205 struct ceph_cap_reservation *ctx, int need)
206 {
207 struct ceph_client *cl = mdsc->fsc->client;
208 int i, j;
209 struct ceph_cap *cap;
210 int have;
211 int alloc = 0;
212 int max_caps;
213 int err = 0;
214 bool trimmed = false;
215 struct ceph_mds_session *s;
216 LIST_HEAD(newcaps);
217
218 doutc(cl, "ctx=%p need=%d\n", ctx, need);
219
220 /* first reserve any caps that are already allocated */
221 spin_lock(&mdsc->caps_list_lock);
222 if (mdsc->caps_avail_count >= need)
223 have = need;
224 else
225 have = mdsc->caps_avail_count;
226 mdsc->caps_avail_count -= have;
227 mdsc->caps_reserve_count += have;
228 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
229 mdsc->caps_reserve_count +
230 mdsc->caps_avail_count);
231 spin_unlock(&mdsc->caps_list_lock);
232
233 for (i = have; i < need; ) {
234 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
235 if (cap) {
236 list_add(&cap->caps_item, &newcaps);
237 alloc++;
238 i++;
239 continue;
240 }
241
242 if (!trimmed) {
243 for (j = 0; j < mdsc->max_sessions; j++) {
244 s = __ceph_lookup_mds_session(mdsc, j);
245 if (!s)
246 continue;
247 mutex_unlock(&mdsc->mutex);
248
249 mutex_lock(&s->s_mutex);
250 max_caps = s->s_nr_caps - (need - i);
251 ceph_trim_caps(mdsc, s, max_caps);
252 mutex_unlock(&s->s_mutex);
253
254 ceph_put_mds_session(s);
255 mutex_lock(&mdsc->mutex);
256 }
257 trimmed = true;
258
259 spin_lock(&mdsc->caps_list_lock);
260 if (mdsc->caps_avail_count) {
261 int more_have;
262 if (mdsc->caps_avail_count >= need - i)
263 more_have = need - i;
264 else
265 more_have = mdsc->caps_avail_count;
266
267 i += more_have;
268 have += more_have;
269 mdsc->caps_avail_count -= more_have;
270 mdsc->caps_reserve_count += more_have;
271
272 }
273 spin_unlock(&mdsc->caps_list_lock);
274
275 continue;
276 }
277
278 pr_warn_client(cl, "ctx=%p ENOMEM need=%d got=%d\n", ctx, need,
279 have + alloc);
280 err = -ENOMEM;
281 break;
282 }
283
284 if (!err) {
285 BUG_ON(have + alloc != need);
286 ctx->count = need;
287 ctx->used = 0;
288 }
289
290 spin_lock(&mdsc->caps_list_lock);
291 mdsc->caps_total_count += alloc;
292 mdsc->caps_reserve_count += alloc;
293 list_splice(&newcaps, &mdsc->caps_list);
294
295 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
296 mdsc->caps_reserve_count +
297 mdsc->caps_avail_count);
298
299 if (err)
300 __ceph_unreserve_caps(mdsc, have + alloc);
301
302 spin_unlock(&mdsc->caps_list_lock);
303
304 doutc(cl, "ctx=%p %d = %d used + %d resv + %d avail\n", ctx,
305 mdsc->caps_total_count, mdsc->caps_use_count,
306 mdsc->caps_reserve_count, mdsc->caps_avail_count);
307 return err;
308 }
309
ceph_unreserve_caps(struct ceph_mds_client * mdsc,struct ceph_cap_reservation * ctx)310 void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
311 struct ceph_cap_reservation *ctx)
312 {
313 struct ceph_client *cl = mdsc->fsc->client;
314 bool reclaim = false;
315 if (!ctx->count)
316 return;
317
318 doutc(cl, "ctx=%p count=%d\n", ctx, ctx->count);
319 spin_lock(&mdsc->caps_list_lock);
320 __ceph_unreserve_caps(mdsc, ctx->count);
321 ctx->count = 0;
322
323 if (mdsc->caps_use_max > 0 &&
324 mdsc->caps_use_count > mdsc->caps_use_max)
325 reclaim = true;
326 spin_unlock(&mdsc->caps_list_lock);
327
328 if (reclaim)
329 ceph_reclaim_caps_nr(mdsc, ctx->used);
330 }
331
ceph_get_cap(struct ceph_mds_client * mdsc,struct ceph_cap_reservation * ctx)332 struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
333 struct ceph_cap_reservation *ctx)
334 {
335 struct ceph_client *cl = mdsc->fsc->client;
336 struct ceph_cap *cap = NULL;
337
338 /* temporary, until we do something about cap import/export */
339 if (!ctx) {
340 cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS);
341 if (cap) {
342 spin_lock(&mdsc->caps_list_lock);
343 mdsc->caps_use_count++;
344 mdsc->caps_total_count++;
345 spin_unlock(&mdsc->caps_list_lock);
346 } else {
347 spin_lock(&mdsc->caps_list_lock);
348 if (mdsc->caps_avail_count) {
349 BUG_ON(list_empty(&mdsc->caps_list));
350
351 mdsc->caps_avail_count--;
352 mdsc->caps_use_count++;
353 cap = list_first_entry(&mdsc->caps_list,
354 struct ceph_cap, caps_item);
355 list_del(&cap->caps_item);
356
357 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
358 mdsc->caps_reserve_count + mdsc->caps_avail_count);
359 }
360 spin_unlock(&mdsc->caps_list_lock);
361 }
362
363 return cap;
364 }
365
366 spin_lock(&mdsc->caps_list_lock);
367 doutc(cl, "ctx=%p (%d) %d = %d used + %d resv + %d avail\n", ctx,
368 ctx->count, mdsc->caps_total_count, mdsc->caps_use_count,
369 mdsc->caps_reserve_count, mdsc->caps_avail_count);
370 BUG_ON(!ctx->count);
371 BUG_ON(ctx->count > mdsc->caps_reserve_count);
372 BUG_ON(list_empty(&mdsc->caps_list));
373
374 ctx->count--;
375 ctx->used++;
376 mdsc->caps_reserve_count--;
377 mdsc->caps_use_count++;
378
379 cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item);
380 list_del(&cap->caps_item);
381
382 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
383 mdsc->caps_reserve_count + mdsc->caps_avail_count);
384 spin_unlock(&mdsc->caps_list_lock);
385 return cap;
386 }
387
ceph_put_cap(struct ceph_mds_client * mdsc,struct ceph_cap * cap)388 void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap)
389 {
390 struct ceph_client *cl = mdsc->fsc->client;
391
392 spin_lock(&mdsc->caps_list_lock);
393 doutc(cl, "%p %d = %d used + %d resv + %d avail\n", cap,
394 mdsc->caps_total_count, mdsc->caps_use_count,
395 mdsc->caps_reserve_count, mdsc->caps_avail_count);
396 mdsc->caps_use_count--;
397 /*
398 * Keep some preallocated caps around (ceph_min_count), to
399 * avoid lots of free/alloc churn.
400 */
401 if (mdsc->caps_avail_count >= mdsc->caps_reserve_count +
402 mdsc->caps_min_count) {
403 mdsc->caps_total_count--;
404 kmem_cache_free(ceph_cap_cachep, cap);
405 } else {
406 mdsc->caps_avail_count++;
407 list_add(&cap->caps_item, &mdsc->caps_list);
408 }
409
410 BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count +
411 mdsc->caps_reserve_count + mdsc->caps_avail_count);
412 spin_unlock(&mdsc->caps_list_lock);
413 }
414
ceph_reservation_status(struct ceph_fs_client * fsc,int * total,int * avail,int * used,int * reserved,int * min)415 void ceph_reservation_status(struct ceph_fs_client *fsc,
416 int *total, int *avail, int *used, int *reserved,
417 int *min)
418 {
419 struct ceph_mds_client *mdsc = fsc->mdsc;
420
421 spin_lock(&mdsc->caps_list_lock);
422
423 if (total)
424 *total = mdsc->caps_total_count;
425 if (avail)
426 *avail = mdsc->caps_avail_count;
427 if (used)
428 *used = mdsc->caps_use_count;
429 if (reserved)
430 *reserved = mdsc->caps_reserve_count;
431 if (min)
432 *min = mdsc->caps_min_count;
433
434 spin_unlock(&mdsc->caps_list_lock);
435 }
436
437 /*
438 * Find ceph_cap for given mds, if any.
439 *
440 * Called with i_ceph_lock held.
441 */
__get_cap_for_mds(struct ceph_inode_info * ci,int mds)442 struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds)
443 {
444 struct ceph_cap *cap;
445 struct rb_node *n = ci->i_caps.rb_node;
446
447 while (n) {
448 cap = rb_entry(n, struct ceph_cap, ci_node);
449 if (mds < cap->mds)
450 n = n->rb_left;
451 else if (mds > cap->mds)
452 n = n->rb_right;
453 else
454 return cap;
455 }
456 return NULL;
457 }
458
ceph_get_cap_for_mds(struct ceph_inode_info * ci,int mds)459 struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds)
460 {
461 struct ceph_cap *cap;
462
463 spin_lock(&ci->i_ceph_lock);
464 cap = __get_cap_for_mds(ci, mds);
465 spin_unlock(&ci->i_ceph_lock);
466 return cap;
467 }
468
469 /*
470 * Called under i_ceph_lock.
471 */
__insert_cap_node(struct ceph_inode_info * ci,struct ceph_cap * new)472 static void __insert_cap_node(struct ceph_inode_info *ci,
473 struct ceph_cap *new)
474 {
475 struct rb_node **p = &ci->i_caps.rb_node;
476 struct rb_node *parent = NULL;
477 struct ceph_cap *cap = NULL;
478
479 while (*p) {
480 parent = *p;
481 cap = rb_entry(parent, struct ceph_cap, ci_node);
482 if (new->mds < cap->mds)
483 p = &(*p)->rb_left;
484 else if (new->mds > cap->mds)
485 p = &(*p)->rb_right;
486 else
487 BUG();
488 }
489
490 rb_link_node(&new->ci_node, parent, p);
491 rb_insert_color(&new->ci_node, &ci->i_caps);
492 }
493
494 /*
495 * (re)set cap hold timeouts, which control the delayed release
496 * of unused caps back to the MDS. Should be called on cap use.
497 */
__cap_set_timeouts(struct ceph_mds_client * mdsc,struct ceph_inode_info * ci)498 static void __cap_set_timeouts(struct ceph_mds_client *mdsc,
499 struct ceph_inode_info *ci)
500 {
501 struct inode *inode = &ci->netfs.inode;
502 struct ceph_mount_options *opt = mdsc->fsc->mount_options;
503
504 ci->i_hold_caps_max = round_jiffies(jiffies +
505 opt->caps_wanted_delay_max * HZ);
506 doutc(mdsc->fsc->client, "%p %llx.%llx %lu\n", inode,
507 ceph_vinop(inode), ci->i_hold_caps_max - jiffies);
508 }
509
510 /*
511 * (Re)queue cap at the end of the delayed cap release list.
512 *
513 * If I_FLUSH is set, leave the inode at the front of the list.
514 *
515 * Caller holds i_ceph_lock
516 * -> we take mdsc->cap_delay_lock
517 */
__cap_delay_requeue(struct ceph_mds_client * mdsc,struct ceph_inode_info * ci)518 static void __cap_delay_requeue(struct ceph_mds_client *mdsc,
519 struct ceph_inode_info *ci)
520 {
521 struct inode *inode = &ci->netfs.inode;
522
523 doutc(mdsc->fsc->client, "%p %llx.%llx flags 0x%lx at %lu\n",
524 inode, ceph_vinop(inode), ci->i_ceph_flags,
525 ci->i_hold_caps_max);
526 if (!mdsc->stopping) {
527 spin_lock(&mdsc->cap_delay_lock);
528 if (!list_empty(&ci->i_cap_delay_list)) {
529 if (ci->i_ceph_flags & CEPH_I_FLUSH)
530 goto no_change;
531 list_del_init(&ci->i_cap_delay_list);
532 }
533 __cap_set_timeouts(mdsc, ci);
534 list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
535 no_change:
536 spin_unlock(&mdsc->cap_delay_lock);
537 }
538 }
539
540 /*
541 * Queue an inode for immediate writeback. Mark inode with I_FLUSH,
542 * indicating we should send a cap message to flush dirty metadata
543 * asap, and move to the front of the delayed cap list.
544 */
__cap_delay_requeue_front(struct ceph_mds_client * mdsc,struct ceph_inode_info * ci)545 static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc,
546 struct ceph_inode_info *ci)
547 {
548 struct inode *inode = &ci->netfs.inode;
549
550 doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode));
551 spin_lock(&mdsc->cap_delay_lock);
552 set_bit(CEPH_I_FLUSH_BIT, &ci->i_ceph_flags);
553 if (!list_empty(&ci->i_cap_delay_list))
554 list_del_init(&ci->i_cap_delay_list);
555 list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list);
556 spin_unlock(&mdsc->cap_delay_lock);
557 }
558
559 /*
560 * Cancel delayed work on cap.
561 *
562 * Caller must hold i_ceph_lock.
563 */
__cap_delay_cancel(struct ceph_mds_client * mdsc,struct ceph_inode_info * ci)564 static void __cap_delay_cancel(struct ceph_mds_client *mdsc,
565 struct ceph_inode_info *ci)
566 {
567 struct inode *inode = &ci->netfs.inode;
568
569 doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode, ceph_vinop(inode));
570 if (list_empty(&ci->i_cap_delay_list))
571 return;
572 spin_lock(&mdsc->cap_delay_lock);
573 list_del_init(&ci->i_cap_delay_list);
574 spin_unlock(&mdsc->cap_delay_lock);
575 }
576
577 /* Common issue checks for add_cap, handle_cap_grant. */
__check_cap_issue(struct ceph_inode_info * ci,struct ceph_cap * cap,unsigned issued)578 static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap,
579 unsigned issued)
580 {
581 struct inode *inode = &ci->netfs.inode;
582 struct ceph_client *cl = ceph_inode_to_client(inode);
583
584 unsigned had = __ceph_caps_issued(ci, NULL);
585
586 lockdep_assert_held(&ci->i_ceph_lock);
587
588 /*
589 * Each time we receive FILE_CACHE anew, we increment
590 * i_rdcache_gen.
591 */
592 if (S_ISREG(ci->netfs.inode.i_mode) &&
593 (issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
594 (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) {
595 ci->i_rdcache_gen++;
596 }
597
598 /*
599 * If FILE_SHARED is newly issued, mark dir not complete. We don't
600 * know what happened to this directory while we didn't have the cap.
601 * If FILE_SHARED is being revoked, also mark dir not complete. It
602 * stops on-going cached readdir.
603 */
604 if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) {
605 if (issued & CEPH_CAP_FILE_SHARED)
606 atomic_inc(&ci->i_shared_gen);
607 if (S_ISDIR(ci->netfs.inode.i_mode)) {
608 doutc(cl, " marking %p NOT complete\n", inode);
609 __ceph_dir_clear_complete(ci);
610 }
611 }
612
613 /* Wipe saved layout if we're losing DIR_CREATE caps */
614 if (S_ISDIR(ci->netfs.inode.i_mode) && (had & CEPH_CAP_DIR_CREATE) &&
615 !(issued & CEPH_CAP_DIR_CREATE)) {
616 ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
617 memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
618 }
619 }
620
621 /**
622 * change_auth_cap_ses - move inode to appropriate lists when auth caps change
623 * @ci: inode to be moved
624 * @session: new auth caps session
625 */
change_auth_cap_ses(struct ceph_inode_info * ci,struct ceph_mds_session * session)626 void change_auth_cap_ses(struct ceph_inode_info *ci,
627 struct ceph_mds_session *session)
628 {
629 lockdep_assert_held(&ci->i_ceph_lock);
630
631 if (list_empty(&ci->i_dirty_item) && list_empty(&ci->i_flushing_item))
632 return;
633
634 spin_lock(&session->s_mdsc->cap_dirty_lock);
635 if (!list_empty(&ci->i_dirty_item))
636 list_move(&ci->i_dirty_item, &session->s_cap_dirty);
637 if (!list_empty(&ci->i_flushing_item))
638 list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing);
639 spin_unlock(&session->s_mdsc->cap_dirty_lock);
640 }
641
642 /*
643 * Add a capability under the given MDS session.
644 *
645 * Caller should hold session snap_rwsem (read) and ci->i_ceph_lock
646 *
647 * @fmode is the open file mode, if we are opening a file, otherwise
648 * it is < 0. (This is so we can atomically add the cap and add an
649 * open file reference to it.)
650 */
ceph_add_cap(struct inode * inode,struct ceph_mds_session * session,u64 cap_id,unsigned issued,unsigned wanted,unsigned seq,unsigned mseq,u64 realmino,int flags,struct ceph_cap ** new_cap)651 void ceph_add_cap(struct inode *inode,
652 struct ceph_mds_session *session, u64 cap_id,
653 unsigned issued, unsigned wanted,
654 unsigned seq, unsigned mseq, u64 realmino, int flags,
655 struct ceph_cap **new_cap)
656 {
657 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
658 struct ceph_client *cl = ceph_inode_to_client(inode);
659 struct ceph_inode_info *ci = ceph_inode(inode);
660 struct ceph_cap *cap;
661 int mds = session->s_mds;
662 int actual_wanted;
663 u32 gen;
664
665 lockdep_assert_held(&ci->i_ceph_lock);
666
667 doutc(cl, "%p %llx.%llx mds%d cap %llx %s seq %d\n", inode,
668 ceph_vinop(inode), session->s_mds, cap_id,
669 ceph_cap_string(issued), seq);
670
671 gen = atomic_read(&session->s_cap_gen);
672
673 cap = __get_cap_for_mds(ci, mds);
674 if (!cap) {
675 cap = *new_cap;
676 *new_cap = NULL;
677
678 cap->issued = 0;
679 cap->implemented = 0;
680 cap->mds = mds;
681 cap->mds_wanted = 0;
682 cap->mseq = 0;
683
684 cap->ci = ci;
685 __insert_cap_node(ci, cap);
686
687 /* add to session cap list */
688 cap->session = session;
689 spin_lock(&session->s_cap_lock);
690 list_add_tail(&cap->session_caps, &session->s_caps);
691 session->s_nr_caps++;
692 atomic64_inc(&mdsc->metric.total_caps);
693 spin_unlock(&session->s_cap_lock);
694 } else {
695 spin_lock(&session->s_cap_lock);
696 list_move_tail(&cap->session_caps, &session->s_caps);
697 spin_unlock(&session->s_cap_lock);
698
699 if (cap->cap_gen < gen)
700 cap->issued = cap->implemented = CEPH_CAP_PIN;
701
702 /*
703 * auth mds of the inode changed. we received the cap export
704 * message, but still haven't received the cap import message.
705 * handle_cap_export() updated the new auth MDS' cap.
706 *
707 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing
708 * a message that was send before the cap import message. So
709 * don't remove caps.
710 */
711 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
712 WARN_ON(cap != ci->i_auth_cap);
713 WARN_ON(cap->cap_id != cap_id);
714 seq = cap->seq;
715 mseq = cap->mseq;
716 issued |= cap->issued;
717 flags |= CEPH_CAP_FLAG_AUTH;
718 }
719 }
720
721 if (!ci->i_snap_realm ||
722 ((flags & CEPH_CAP_FLAG_AUTH) &&
723 realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) {
724 /*
725 * add this inode to the appropriate snap realm
726 */
727 struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc,
728 realmino);
729 if (realm)
730 ceph_change_snap_realm(inode, realm);
731 else
732 WARN(1, "%s: couldn't find snap realm 0x%llx (ino 0x%llx oldrealm 0x%llx)\n",
733 __func__, realmino, ci->i_vino.ino,
734 ci->i_snap_realm ? ci->i_snap_realm->ino : 0);
735 }
736
737 __check_cap_issue(ci, cap, issued);
738
739 /*
740 * If we are issued caps we don't want, or the mds' wanted
741 * value appears to be off, queue a check so we'll release
742 * later and/or update the mds wanted value.
743 */
744 actual_wanted = __ceph_caps_wanted(ci);
745 if ((wanted & ~actual_wanted) ||
746 (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) {
747 doutc(cl, "issued %s, mds wanted %s, actual %s, queueing\n",
748 ceph_cap_string(issued), ceph_cap_string(wanted),
749 ceph_cap_string(actual_wanted));
750 __cap_delay_requeue(mdsc, ci);
751 }
752
753 if (flags & CEPH_CAP_FLAG_AUTH) {
754 if (!ci->i_auth_cap ||
755 ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) {
756 if (ci->i_auth_cap &&
757 ci->i_auth_cap->session != cap->session)
758 change_auth_cap_ses(ci, cap->session);
759 ci->i_auth_cap = cap;
760 cap->mds_wanted = wanted;
761 }
762 } else {
763 WARN_ON(ci->i_auth_cap == cap);
764 }
765
766 doutc(cl, "inode %p %llx.%llx cap %p %s now %s seq %d mds%d\n",
767 inode, ceph_vinop(inode), cap, ceph_cap_string(issued),
768 ceph_cap_string(issued|cap->issued), seq, mds);
769 cap->cap_id = cap_id;
770 cap->issued = issued;
771 cap->implemented |= issued;
772 if (ceph_seq_cmp(mseq, cap->mseq) > 0)
773 cap->mds_wanted = wanted;
774 else
775 cap->mds_wanted |= wanted;
776 cap->seq = seq;
777 cap->issue_seq = seq;
778 cap->mseq = mseq;
779 cap->cap_gen = gen;
780 wake_up_all(&ci->i_cap_wq);
781 }
782
783 /*
784 * Return true if cap has not timed out and belongs to the current
785 * generation of the MDS session (i.e. has not gone 'stale' due to
786 * us losing touch with the mds).
787 */
__cap_is_valid(struct ceph_cap * cap)788 static int __cap_is_valid(struct ceph_cap *cap)
789 {
790 struct inode *inode = &cap->ci->netfs.inode;
791 struct ceph_client *cl = cap->session->s_mdsc->fsc->client;
792 unsigned long ttl;
793 u32 gen;
794
795 gen = atomic_read(&cap->session->s_cap_gen);
796 ttl = cap->session->s_cap_ttl;
797
798 if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) {
799 doutc(cl, "%p %llx.%llx cap %p issued %s but STALE (gen %u vs %u)\n",
800 inode, ceph_vinop(inode), cap,
801 ceph_cap_string(cap->issued), cap->cap_gen, gen);
802 return 0;
803 }
804
805 return 1;
806 }
807
808 /*
809 * Return set of valid cap bits issued to us. Note that caps time
810 * out, and may be invalidated in bulk if the client session times out
811 * and session->s_cap_gen is bumped.
812 */
__ceph_caps_issued(struct ceph_inode_info * ci,int * implemented)813 int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented)
814 {
815 struct inode *inode = &ci->netfs.inode;
816 struct ceph_client *cl = ceph_inode_to_client(inode);
817 int have = ci->i_snap_caps;
818 struct ceph_cap *cap;
819 struct rb_node *p;
820
821 if (implemented)
822 *implemented = 0;
823 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
824 cap = rb_entry(p, struct ceph_cap, ci_node);
825 if (!__cap_is_valid(cap))
826 continue;
827 doutc(cl, "%p %llx.%llx cap %p issued %s\n", inode,
828 ceph_vinop(inode), cap, ceph_cap_string(cap->issued));
829 have |= cap->issued;
830 if (implemented)
831 *implemented |= cap->implemented;
832 }
833 /*
834 * exclude caps issued by non-auth MDS, but are been revoking
835 * by the auth MDS. The non-auth MDS should be revoking/exporting
836 * these caps, but the message is delayed.
837 */
838 if (ci->i_auth_cap) {
839 cap = ci->i_auth_cap;
840 have &= ~cap->implemented | cap->issued;
841 }
842 return have;
843 }
844
845 /*
846 * Get cap bits issued by caps other than @ocap
847 */
__ceph_caps_issued_other(struct ceph_inode_info * ci,struct ceph_cap * ocap)848 int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap)
849 {
850 int have = ci->i_snap_caps;
851 struct ceph_cap *cap;
852 struct rb_node *p;
853
854 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
855 cap = rb_entry(p, struct ceph_cap, ci_node);
856 if (cap == ocap)
857 continue;
858 if (!__cap_is_valid(cap))
859 continue;
860 have |= cap->issued;
861 }
862 return have;
863 }
864
865 /*
866 * Move a cap to the end of the LRU (oldest caps at list head, newest
867 * at list tail).
868 */
__touch_cap(struct ceph_cap * cap)869 static void __touch_cap(struct ceph_cap *cap)
870 {
871 struct inode *inode = &cap->ci->netfs.inode;
872 struct ceph_mds_session *s = cap->session;
873 struct ceph_client *cl = s->s_mdsc->fsc->client;
874
875 spin_lock(&s->s_cap_lock);
876 if (!s->s_cap_iterator) {
877 doutc(cl, "%p %llx.%llx cap %p mds%d\n", inode,
878 ceph_vinop(inode), cap, s->s_mds);
879 list_move_tail(&cap->session_caps, &s->s_caps);
880 } else {
881 doutc(cl, "%p %llx.%llx cap %p mds%d NOP, iterating over caps\n",
882 inode, ceph_vinop(inode), cap, s->s_mds);
883 }
884 spin_unlock(&s->s_cap_lock);
885 }
886
887 /*
888 * Check if we hold the given mask. If so, move the cap(s) to the
889 * front of their respective LRUs. (This is the preferred way for
890 * callers to check for caps they want.)
891 */
__ceph_caps_issued_mask(struct ceph_inode_info * ci,int mask,int touch)892 int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch)
893 {
894 struct inode *inode = &ci->netfs.inode;
895 struct ceph_client *cl = ceph_inode_to_client(inode);
896 struct ceph_cap *cap;
897 struct rb_node *p;
898 int have = ci->i_snap_caps;
899
900 if ((have & mask) == mask) {
901 doutc(cl, "mask %p %llx.%llx snap issued %s (mask %s)\n",
902 inode, ceph_vinop(inode), ceph_cap_string(have),
903 ceph_cap_string(mask));
904 return 1;
905 }
906
907 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
908 cap = rb_entry(p, struct ceph_cap, ci_node);
909 if (!__cap_is_valid(cap))
910 continue;
911 if ((cap->issued & mask) == mask) {
912 doutc(cl, "mask %p %llx.%llx cap %p issued %s (mask %s)\n",
913 inode, ceph_vinop(inode), cap,
914 ceph_cap_string(cap->issued),
915 ceph_cap_string(mask));
916 if (touch)
917 __touch_cap(cap);
918 return 1;
919 }
920
921 /* does a combination of caps satisfy mask? */
922 have |= cap->issued;
923 if ((have & mask) == mask) {
924 doutc(cl, "mask %p %llx.%llx combo issued %s (mask %s)\n",
925 inode, ceph_vinop(inode),
926 ceph_cap_string(cap->issued),
927 ceph_cap_string(mask));
928 if (touch) {
929 struct rb_node *q;
930
931 /* touch this + preceding caps */
932 __touch_cap(cap);
933 for (q = rb_first(&ci->i_caps); q != p;
934 q = rb_next(q)) {
935 cap = rb_entry(q, struct ceph_cap,
936 ci_node);
937 if (!__cap_is_valid(cap))
938 continue;
939 if (cap->issued & mask)
940 __touch_cap(cap);
941 }
942 }
943 return 1;
944 }
945 }
946
947 return 0;
948 }
949
__ceph_caps_issued_mask_metric(struct ceph_inode_info * ci,int mask,int touch)950 int __ceph_caps_issued_mask_metric(struct ceph_inode_info *ci, int mask,
951 int touch)
952 {
953 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(ci->netfs.inode.i_sb);
954 int r;
955
956 r = __ceph_caps_issued_mask(ci, mask, touch);
957 if (r)
958 ceph_update_cap_hit(&fsc->mdsc->metric);
959 else
960 ceph_update_cap_mis(&fsc->mdsc->metric);
961 return r;
962 }
963
964 /*
965 * Return true if mask caps are currently being revoked by an MDS.
966 */
__ceph_caps_revoking_other(struct ceph_inode_info * ci,struct ceph_cap * ocap,int mask)967 int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
968 struct ceph_cap *ocap, int mask)
969 {
970 struct ceph_cap *cap;
971 struct rb_node *p;
972
973 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
974 cap = rb_entry(p, struct ceph_cap, ci_node);
975 if (cap != ocap &&
976 (cap->implemented & ~cap->issued & mask))
977 return 1;
978 }
979 return 0;
980 }
981
__ceph_caps_used(struct ceph_inode_info * ci)982 int __ceph_caps_used(struct ceph_inode_info *ci)
983 {
984 int used = 0;
985 if (ci->i_pin_ref)
986 used |= CEPH_CAP_PIN;
987 if (ci->i_rd_ref)
988 used |= CEPH_CAP_FILE_RD;
989 if (ci->i_rdcache_ref ||
990 (S_ISREG(ci->netfs.inode.i_mode) &&
991 ci->netfs.inode.i_data.nrpages))
992 used |= CEPH_CAP_FILE_CACHE;
993 if (ci->i_wr_ref)
994 used |= CEPH_CAP_FILE_WR;
995 if (ci->i_wb_ref || ci->i_wrbuffer_ref)
996 used |= CEPH_CAP_FILE_BUFFER;
997 if (ci->i_fx_ref)
998 used |= CEPH_CAP_FILE_EXCL;
999 return used;
1000 }
1001
1002 #define FMODE_WAIT_BIAS 1000
1003
1004 /*
1005 * wanted, by virtue of open file modes
1006 */
__ceph_caps_file_wanted(struct ceph_inode_info * ci)1007 int __ceph_caps_file_wanted(struct ceph_inode_info *ci)
1008 {
1009 const int PIN_SHIFT = ffs(CEPH_FILE_MODE_PIN);
1010 const int RD_SHIFT = ffs(CEPH_FILE_MODE_RD);
1011 const int WR_SHIFT = ffs(CEPH_FILE_MODE_WR);
1012 const int LAZY_SHIFT = ffs(CEPH_FILE_MODE_LAZY);
1013 struct ceph_mount_options *opt =
1014 ceph_inode_to_fs_client(&ci->netfs.inode)->mount_options;
1015 unsigned long used_cutoff = jiffies - opt->caps_wanted_delay_max * HZ;
1016 unsigned long idle_cutoff = jiffies - opt->caps_wanted_delay_min * HZ;
1017
1018 if (S_ISDIR(ci->netfs.inode.i_mode)) {
1019 int want = 0;
1020
1021 /* use used_cutoff here, to keep dir's wanted caps longer */
1022 if (ci->i_nr_by_mode[RD_SHIFT] > 0 ||
1023 time_after(ci->i_last_rd, used_cutoff))
1024 want |= CEPH_CAP_ANY_SHARED;
1025
1026 if (ci->i_nr_by_mode[WR_SHIFT] > 0 ||
1027 time_after(ci->i_last_wr, used_cutoff)) {
1028 want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
1029 if (opt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
1030 want |= CEPH_CAP_ANY_DIR_OPS;
1031 }
1032
1033 if (want || ci->i_nr_by_mode[PIN_SHIFT] > 0)
1034 want |= CEPH_CAP_PIN;
1035
1036 return want;
1037 } else {
1038 int bits = 0;
1039
1040 if (ci->i_nr_by_mode[RD_SHIFT] > 0) {
1041 if (ci->i_nr_by_mode[RD_SHIFT] >= FMODE_WAIT_BIAS ||
1042 time_after(ci->i_last_rd, used_cutoff))
1043 bits |= 1 << RD_SHIFT;
1044 } else if (time_after(ci->i_last_rd, idle_cutoff)) {
1045 bits |= 1 << RD_SHIFT;
1046 }
1047
1048 if (ci->i_nr_by_mode[WR_SHIFT] > 0) {
1049 if (ci->i_nr_by_mode[WR_SHIFT] >= FMODE_WAIT_BIAS ||
1050 time_after(ci->i_last_wr, used_cutoff))
1051 bits |= 1 << WR_SHIFT;
1052 } else if (time_after(ci->i_last_wr, idle_cutoff)) {
1053 bits |= 1 << WR_SHIFT;
1054 }
1055
1056 /* check lazyio only when read/write is wanted */
1057 if ((bits & (CEPH_FILE_MODE_RDWR << 1)) &&
1058 ci->i_nr_by_mode[LAZY_SHIFT] > 0)
1059 bits |= 1 << LAZY_SHIFT;
1060
1061 return bits ? ceph_caps_for_mode(bits >> 1) : 0;
1062 }
1063 }
1064
1065 /*
1066 * wanted, by virtue of open file modes AND cap refs (buffered/cached data)
1067 */
__ceph_caps_wanted(struct ceph_inode_info * ci)1068 int __ceph_caps_wanted(struct ceph_inode_info *ci)
1069 {
1070 int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci);
1071 if (S_ISDIR(ci->netfs.inode.i_mode)) {
1072 /* we want EXCL if holding caps of dir ops */
1073 if (w & CEPH_CAP_ANY_DIR_OPS)
1074 w |= CEPH_CAP_FILE_EXCL;
1075 } else {
1076 /* we want EXCL if dirty data */
1077 if (w & CEPH_CAP_FILE_BUFFER)
1078 w |= CEPH_CAP_FILE_EXCL;
1079 }
1080 return w;
1081 }
1082
1083 /*
1084 * Return caps we have registered with the MDS(s) as 'wanted'.
1085 */
__ceph_caps_mds_wanted(struct ceph_inode_info * ci,bool check)1086 int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check)
1087 {
1088 struct ceph_cap *cap;
1089 struct rb_node *p;
1090 int mds_wanted = 0;
1091
1092 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
1093 cap = rb_entry(p, struct ceph_cap, ci_node);
1094 if (check && !__cap_is_valid(cap))
1095 continue;
1096 if (cap == ci->i_auth_cap)
1097 mds_wanted |= cap->mds_wanted;
1098 else
1099 mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR);
1100 }
1101 return mds_wanted;
1102 }
1103
ceph_is_any_caps(struct inode * inode)1104 int ceph_is_any_caps(struct inode *inode)
1105 {
1106 struct ceph_inode_info *ci = ceph_inode(inode);
1107 int ret;
1108
1109 spin_lock(&ci->i_ceph_lock);
1110 ret = __ceph_is_any_real_caps(ci);
1111 spin_unlock(&ci->i_ceph_lock);
1112
1113 return ret;
1114 }
1115
1116 /*
1117 * Remove a cap. Take steps to deal with a racing iterate_session_caps.
1118 *
1119 * caller should hold i_ceph_lock.
1120 * caller will not hold session s_mutex if called from destroy_inode.
1121 */
__ceph_remove_cap(struct ceph_cap * cap,bool queue_release)1122 void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release)
1123 {
1124 struct ceph_mds_session *session = cap->session;
1125 struct ceph_client *cl = session->s_mdsc->fsc->client;
1126 struct ceph_inode_info *ci = cap->ci;
1127 struct inode *inode = &ci->netfs.inode;
1128 struct ceph_mds_client *mdsc;
1129 int removed = 0;
1130
1131 /* 'ci' being NULL means the remove have already occurred */
1132 if (!ci) {
1133 doutc(cl, "inode is NULL\n");
1134 return;
1135 }
1136
1137 lockdep_assert_held(&ci->i_ceph_lock);
1138
1139 doutc(cl, "%p from %p %llx.%llx\n", cap, inode, ceph_vinop(inode));
1140
1141 mdsc = ceph_inode_to_fs_client(&ci->netfs.inode)->mdsc;
1142
1143 /* remove from inode's cap rbtree, and clear auth cap */
1144 rb_erase(&cap->ci_node, &ci->i_caps);
1145 if (ci->i_auth_cap == cap)
1146 ci->i_auth_cap = NULL;
1147
1148 /* remove from session list */
1149 spin_lock(&session->s_cap_lock);
1150 if (session->s_cap_iterator == cap) {
1151 /* not yet, we are iterating over this very cap */
1152 doutc(cl, "delaying %p removal from session %p\n", cap,
1153 cap->session);
1154 } else {
1155 list_del_init(&cap->session_caps);
1156 session->s_nr_caps--;
1157 atomic64_dec(&mdsc->metric.total_caps);
1158 cap->session = NULL;
1159 removed = 1;
1160 }
1161 /* protect backpointer with s_cap_lock: see iterate_session_caps */
1162 cap->ci = NULL;
1163
1164 /*
1165 * s_cap_reconnect is protected by s_cap_lock. no one changes
1166 * s_cap_gen while session is in the reconnect state.
1167 */
1168 if (queue_release &&
1169 (!session->s_cap_reconnect ||
1170 cap->cap_gen == atomic_read(&session->s_cap_gen))) {
1171 cap->queue_release = 1;
1172 if (removed) {
1173 __ceph_queue_cap_release(session, cap);
1174 removed = 0;
1175 }
1176 } else {
1177 cap->queue_release = 0;
1178 }
1179 cap->cap_ino = ci->i_vino.ino;
1180
1181 spin_unlock(&session->s_cap_lock);
1182
1183 if (removed)
1184 ceph_put_cap(mdsc, cap);
1185
1186 if (!__ceph_is_any_real_caps(ci)) {
1187 /* when reconnect denied, we remove session caps forcibly,
1188 * i_wr_ref can be non-zero. If there are ongoing write,
1189 * keep i_snap_realm.
1190 */
1191 if (ci->i_wr_ref == 0 && ci->i_snap_realm)
1192 ceph_change_snap_realm(&ci->netfs.inode, NULL);
1193
1194 __cap_delay_cancel(mdsc, ci);
1195 }
1196 }
1197
ceph_remove_cap(struct ceph_mds_client * mdsc,struct ceph_cap * cap,bool queue_release)1198 void ceph_remove_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap,
1199 bool queue_release)
1200 {
1201 struct ceph_inode_info *ci = cap->ci;
1202 struct ceph_fs_client *fsc;
1203
1204 /* 'ci' being NULL means the remove have already occurred */
1205 if (!ci) {
1206 doutc(mdsc->fsc->client, "inode is NULL\n");
1207 return;
1208 }
1209
1210 lockdep_assert_held(&ci->i_ceph_lock);
1211
1212 fsc = ceph_inode_to_fs_client(&ci->netfs.inode);
1213 WARN_ON_ONCE(ci->i_auth_cap == cap &&
1214 !list_empty(&ci->i_dirty_item) &&
1215 !fsc->blocklisted &&
1216 !ceph_inode_is_shutdown(&ci->netfs.inode));
1217
1218 __ceph_remove_cap(cap, queue_release);
1219 }
1220
1221 struct cap_msg_args {
1222 struct ceph_mds_session *session;
1223 u64 ino, cid, follows;
1224 u64 flush_tid, oldest_flush_tid, size, max_size;
1225 u64 xattr_version;
1226 u64 change_attr;
1227 struct ceph_buffer *xattr_buf;
1228 struct ceph_buffer *old_xattr_buf;
1229 struct timespec64 atime, mtime, ctime, btime;
1230 int op, caps, wanted, dirty;
1231 u32 seq, issue_seq, mseq, time_warp_seq;
1232 u32 flags;
1233 kuid_t uid;
1234 kgid_t gid;
1235 umode_t mode;
1236 bool inline_data;
1237 bool wake;
1238 bool encrypted;
1239 u32 fscrypt_auth_len;
1240 u8 fscrypt_auth[sizeof(struct ceph_fscrypt_auth)]; // for context
1241 };
1242
1243 /* Marshal up the cap msg to the MDS */
encode_cap_msg(struct ceph_msg * msg,struct cap_msg_args * arg)1244 static void encode_cap_msg(struct ceph_msg *msg, struct cap_msg_args *arg)
1245 {
1246 struct ceph_mds_caps *fc;
1247 void *p;
1248 struct ceph_mds_client *mdsc = arg->session->s_mdsc;
1249 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
1250
1251 doutc(mdsc->fsc->client,
1252 "%s %llx %llx caps %s wanted %s dirty %s seq %u/%u"
1253 " tid %llu/%llu mseq %u follows %lld size %llu/%llu"
1254 " xattr_ver %llu xattr_len %d\n",
1255 ceph_cap_op_name(arg->op), arg->cid, arg->ino,
1256 ceph_cap_string(arg->caps), ceph_cap_string(arg->wanted),
1257 ceph_cap_string(arg->dirty), arg->seq, arg->issue_seq,
1258 arg->flush_tid, arg->oldest_flush_tid, arg->mseq, arg->follows,
1259 arg->size, arg->max_size, arg->xattr_version,
1260 arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0);
1261
1262 msg->hdr.version = cpu_to_le16(12);
1263 msg->hdr.tid = cpu_to_le64(arg->flush_tid);
1264
1265 fc = msg->front.iov_base;
1266 memset(fc, 0, sizeof(*fc));
1267
1268 fc->cap_id = cpu_to_le64(arg->cid);
1269 fc->op = cpu_to_le32(arg->op);
1270 fc->seq = cpu_to_le32(arg->seq);
1271 fc->issue_seq = cpu_to_le32(arg->issue_seq);
1272 fc->migrate_seq = cpu_to_le32(arg->mseq);
1273 fc->caps = cpu_to_le32(arg->caps);
1274 fc->wanted = cpu_to_le32(arg->wanted);
1275 fc->dirty = cpu_to_le32(arg->dirty);
1276 fc->ino = cpu_to_le64(arg->ino);
1277 fc->snap_follows = cpu_to_le64(arg->follows);
1278
1279 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1280 if (arg->encrypted)
1281 fc->size = cpu_to_le64(round_up(arg->size,
1282 CEPH_FSCRYPT_BLOCK_SIZE));
1283 else
1284 #endif
1285 fc->size = cpu_to_le64(arg->size);
1286 fc->max_size = cpu_to_le64(arg->max_size);
1287 ceph_encode_timespec64(&fc->mtime, &arg->mtime);
1288 ceph_encode_timespec64(&fc->atime, &arg->atime);
1289 ceph_encode_timespec64(&fc->ctime, &arg->ctime);
1290 fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq);
1291
1292 fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid));
1293 fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid));
1294 fc->mode = cpu_to_le32(arg->mode);
1295
1296 fc->xattr_version = cpu_to_le64(arg->xattr_version);
1297 if (arg->xattr_buf) {
1298 msg->middle = ceph_buffer_get(arg->xattr_buf);
1299 fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1300 msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len);
1301 }
1302
1303 p = fc + 1;
1304 /* flock buffer size (version 2) */
1305 ceph_encode_32(&p, 0);
1306 /* inline version (version 4) */
1307 ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE);
1308 /* inline data size */
1309 ceph_encode_32(&p, 0);
1310 /*
1311 * osd_epoch_barrier (version 5)
1312 * The epoch_barrier is protected osdc->lock, so READ_ONCE here in
1313 * case it was recently changed
1314 */
1315 ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier));
1316 /* oldest_flush_tid (version 6) */
1317 ceph_encode_64(&p, arg->oldest_flush_tid);
1318
1319 /*
1320 * caller_uid/caller_gid (version 7)
1321 *
1322 * Currently, we don't properly track which caller dirtied the caps
1323 * last, and force a flush of them when there is a conflict. For now,
1324 * just set this to 0:0, to emulate how the MDS has worked up to now.
1325 */
1326 ceph_encode_32(&p, 0);
1327 ceph_encode_32(&p, 0);
1328
1329 /* pool namespace (version 8) (mds always ignores this) */
1330 ceph_encode_32(&p, 0);
1331
1332 /* btime and change_attr (version 9) */
1333 ceph_encode_timespec64(p, &arg->btime);
1334 p += sizeof(struct ceph_timespec);
1335 ceph_encode_64(&p, arg->change_attr);
1336
1337 /* Advisory flags (version 10) */
1338 ceph_encode_32(&p, arg->flags);
1339
1340 /* dirstats (version 11) - these are r/o on the client */
1341 ceph_encode_64(&p, 0);
1342 ceph_encode_64(&p, 0);
1343
1344 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1345 /*
1346 * fscrypt_auth and fscrypt_file (version 12)
1347 *
1348 * fscrypt_auth holds the crypto context (if any). fscrypt_file
1349 * tracks the real i_size as an __le64 field (and we use a rounded-up
1350 * i_size in the traditional size field).
1351 */
1352 ceph_encode_32(&p, arg->fscrypt_auth_len);
1353 ceph_encode_copy(&p, arg->fscrypt_auth, arg->fscrypt_auth_len);
1354 ceph_encode_32(&p, sizeof(__le64));
1355 ceph_encode_64(&p, arg->size);
1356 #else /* CONFIG_FS_ENCRYPTION */
1357 ceph_encode_32(&p, 0);
1358 ceph_encode_32(&p, 0);
1359 #endif /* CONFIG_FS_ENCRYPTION */
1360 }
1361
1362 /*
1363 * Queue cap releases when an inode is dropped from our cache.
1364 */
__ceph_remove_caps(struct ceph_inode_info * ci)1365 void __ceph_remove_caps(struct ceph_inode_info *ci)
1366 {
1367 struct inode *inode = &ci->netfs.inode;
1368 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
1369 struct rb_node *p;
1370
1371 /* lock i_ceph_lock, because ceph_d_revalidate(..., LOOKUP_RCU)
1372 * may call __ceph_caps_issued_mask() on a freeing inode. */
1373 spin_lock(&ci->i_ceph_lock);
1374 p = rb_first(&ci->i_caps);
1375 while (p) {
1376 struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
1377 p = rb_next(p);
1378 ceph_remove_cap(mdsc, cap, true);
1379 }
1380 spin_unlock(&ci->i_ceph_lock);
1381 }
1382
1383 /*
1384 * Prepare to send a cap message to an MDS. Update the cap state, and populate
1385 * the arg struct with the parameters that will need to be sent. This should
1386 * be done under the i_ceph_lock to guard against changes to cap state.
1387 *
1388 * Make note of max_size reported/requested from mds, revoked caps
1389 * that have now been implemented.
1390 */
__prep_cap(struct cap_msg_args * arg,struct ceph_cap * cap,int op,int flags,int used,int want,int retain,int flushing,u64 flush_tid,u64 oldest_flush_tid)1391 static void __prep_cap(struct cap_msg_args *arg, struct ceph_cap *cap,
1392 int op, int flags, int used, int want, int retain,
1393 int flushing, u64 flush_tid, u64 oldest_flush_tid)
1394 {
1395 struct ceph_inode_info *ci = cap->ci;
1396 struct inode *inode = &ci->netfs.inode;
1397 struct ceph_client *cl = ceph_inode_to_client(inode);
1398 int held, revoking;
1399
1400 lockdep_assert_held(&ci->i_ceph_lock);
1401
1402 held = cap->issued | cap->implemented;
1403 revoking = cap->implemented & ~cap->issued;
1404 retain &= ~revoking;
1405
1406 doutc(cl, "%p %llx.%llx cap %p session %p %s -> %s (revoking %s)\n",
1407 inode, ceph_vinop(inode), cap, cap->session,
1408 ceph_cap_string(held), ceph_cap_string(held & retain),
1409 ceph_cap_string(revoking));
1410 BUG_ON((retain & CEPH_CAP_PIN) == 0);
1411
1412 clear_bit(CEPH_I_FLUSH_BIT, &ci->i_ceph_flags);
1413
1414 cap->issued &= retain; /* drop bits we don't want */
1415 /*
1416 * Wake up any waiters on wanted -> needed transition. This is due to
1417 * the weird transition from buffered to sync IO... we need to flush
1418 * dirty pages _before_ allowing sync writes to avoid reordering.
1419 */
1420 arg->wake = cap->implemented & ~cap->issued;
1421 cap->implemented &= cap->issued | used;
1422 cap->mds_wanted = want;
1423
1424 arg->session = cap->session;
1425 arg->ino = ceph_vino(inode).ino;
1426 arg->cid = cap->cap_id;
1427 arg->follows = flushing ? ci->i_head_snapc->seq : 0;
1428 arg->flush_tid = flush_tid;
1429 arg->oldest_flush_tid = oldest_flush_tid;
1430 arg->size = i_size_read(inode);
1431 ci->i_reported_size = arg->size;
1432 arg->max_size = ci->i_wanted_max_size;
1433 if (cap == ci->i_auth_cap) {
1434 if (want & CEPH_CAP_ANY_FILE_WR)
1435 ci->i_requested_max_size = arg->max_size;
1436 else
1437 ci->i_requested_max_size = 0;
1438 }
1439
1440 if (flushing & CEPH_CAP_XATTR_EXCL) {
1441 arg->old_xattr_buf = __ceph_build_xattrs_blob(ci);
1442 arg->xattr_version = ci->i_xattrs.version;
1443 arg->xattr_buf = ceph_buffer_get(ci->i_xattrs.blob);
1444 } else {
1445 arg->xattr_buf = NULL;
1446 arg->old_xattr_buf = NULL;
1447 }
1448
1449 arg->mtime = inode_get_mtime(inode);
1450 arg->atime = inode_get_atime(inode);
1451 arg->ctime = inode_get_ctime(inode);
1452 arg->btime = ci->i_btime;
1453 arg->change_attr = inode_peek_iversion_raw(inode);
1454
1455 arg->op = op;
1456 arg->caps = cap->implemented;
1457 arg->wanted = want;
1458 arg->dirty = flushing;
1459
1460 arg->seq = cap->seq;
1461 arg->issue_seq = cap->issue_seq;
1462 arg->mseq = cap->mseq;
1463 arg->time_warp_seq = ci->i_time_warp_seq;
1464
1465 arg->uid = inode->i_uid;
1466 arg->gid = inode->i_gid;
1467 arg->mode = inode->i_mode;
1468
1469 arg->inline_data = ci->i_inline_version != CEPH_INLINE_NONE;
1470 if (!(flags & CEPH_CLIENT_CAPS_PENDING_CAPSNAP) &&
1471 !list_empty(&ci->i_cap_snaps)) {
1472 struct ceph_cap_snap *capsnap;
1473 list_for_each_entry_reverse(capsnap, &ci->i_cap_snaps, ci_item) {
1474 if (capsnap->cap_flush.tid)
1475 break;
1476 if (capsnap->need_flush) {
1477 flags |= CEPH_CLIENT_CAPS_PENDING_CAPSNAP;
1478 break;
1479 }
1480 }
1481 }
1482 arg->flags = flags;
1483 arg->encrypted = IS_ENCRYPTED(inode);
1484 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1485 if (ci->fscrypt_auth_len &&
1486 WARN_ON_ONCE(ci->fscrypt_auth_len > sizeof(struct ceph_fscrypt_auth))) {
1487 /* Don't set this if it's too big */
1488 arg->fscrypt_auth_len = 0;
1489 } else {
1490 arg->fscrypt_auth_len = ci->fscrypt_auth_len;
1491 memcpy(arg->fscrypt_auth, ci->fscrypt_auth,
1492 min_t(size_t, ci->fscrypt_auth_len,
1493 sizeof(arg->fscrypt_auth)));
1494 }
1495 #endif /* CONFIG_FS_ENCRYPTION */
1496 }
1497
1498 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
1499 #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \
1500 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4 + 8)
1501
cap_msg_size(struct cap_msg_args * arg)1502 static inline int cap_msg_size(struct cap_msg_args *arg)
1503 {
1504 return CAP_MSG_FIXED_FIELDS + arg->fscrypt_auth_len;
1505 }
1506 #else
1507 #define CAP_MSG_FIXED_FIELDS (sizeof(struct ceph_mds_caps) + \
1508 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + 4 + 4)
1509
cap_msg_size(struct cap_msg_args * arg)1510 static inline int cap_msg_size(struct cap_msg_args *arg)
1511 {
1512 return CAP_MSG_FIXED_FIELDS;
1513 }
1514 #endif /* CONFIG_FS_ENCRYPTION */
1515
1516 /*
1517 * Send a cap msg on the given inode.
1518 *
1519 * Caller should hold snap_rwsem (read), s_mutex.
1520 */
__send_cap(struct cap_msg_args * arg,struct ceph_inode_info * ci)1521 static void __send_cap(struct cap_msg_args *arg, struct ceph_inode_info *ci)
1522 {
1523 struct ceph_msg *msg;
1524 struct inode *inode = &ci->netfs.inode;
1525 struct ceph_client *cl = ceph_inode_to_client(inode);
1526
1527 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(arg), GFP_NOFS,
1528 false);
1529 if (!msg) {
1530 pr_err_client(cl,
1531 "error allocating cap msg: ino (%llx.%llx)"
1532 " flushing %s tid %llu, requeuing cap.\n",
1533 ceph_vinop(inode), ceph_cap_string(arg->dirty),
1534 arg->flush_tid);
1535 spin_lock(&ci->i_ceph_lock);
1536 __cap_delay_requeue(arg->session->s_mdsc, ci);
1537 spin_unlock(&ci->i_ceph_lock);
1538 return;
1539 }
1540
1541 encode_cap_msg(msg, arg);
1542 ceph_con_send(&arg->session->s_con, msg);
1543 ceph_buffer_put(arg->old_xattr_buf);
1544 ceph_buffer_put(arg->xattr_buf);
1545 if (arg->wake)
1546 wake_up_all(&ci->i_cap_wq);
1547 }
1548
__send_flush_snap(struct inode * inode,struct ceph_mds_session * session,struct ceph_cap_snap * capsnap,u32 mseq,u64 oldest_flush_tid)1549 static inline int __send_flush_snap(struct inode *inode,
1550 struct ceph_mds_session *session,
1551 struct ceph_cap_snap *capsnap,
1552 u32 mseq, u64 oldest_flush_tid)
1553 {
1554 struct cap_msg_args arg;
1555 struct ceph_msg *msg;
1556
1557 arg.session = session;
1558 arg.ino = ceph_vino(inode).ino;
1559 arg.cid = 0;
1560 arg.follows = capsnap->follows;
1561 arg.flush_tid = capsnap->cap_flush.tid;
1562 arg.oldest_flush_tid = oldest_flush_tid;
1563
1564 arg.size = capsnap->size;
1565 arg.max_size = 0;
1566 arg.xattr_version = capsnap->xattr_version;
1567 arg.xattr_buf = capsnap->xattr_blob;
1568 arg.old_xattr_buf = NULL;
1569
1570 arg.atime = capsnap->atime;
1571 arg.mtime = capsnap->mtime;
1572 arg.ctime = capsnap->ctime;
1573 arg.btime = capsnap->btime;
1574 arg.change_attr = capsnap->change_attr;
1575
1576 arg.op = CEPH_CAP_OP_FLUSHSNAP;
1577 arg.caps = capsnap->issued;
1578 arg.wanted = 0;
1579 arg.dirty = capsnap->dirty;
1580
1581 arg.seq = 0;
1582 arg.issue_seq = 0;
1583 arg.mseq = mseq;
1584 arg.time_warp_seq = capsnap->time_warp_seq;
1585
1586 arg.uid = capsnap->uid;
1587 arg.gid = capsnap->gid;
1588 arg.mode = capsnap->mode;
1589
1590 arg.inline_data = capsnap->inline_data;
1591 arg.flags = 0;
1592 arg.wake = false;
1593 arg.encrypted = IS_ENCRYPTED(inode);
1594
1595 /* No fscrypt_auth changes from a capsnap.*/
1596 arg.fscrypt_auth_len = 0;
1597
1598 msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, cap_msg_size(&arg),
1599 GFP_NOFS, false);
1600 if (!msg)
1601 return -ENOMEM;
1602
1603 encode_cap_msg(msg, &arg);
1604 ceph_con_send(&arg.session->s_con, msg);
1605 return 0;
1606 }
1607
1608 /*
1609 * When a snapshot is taken, clients accumulate dirty metadata on
1610 * inodes with capabilities in ceph_cap_snaps to describe the file
1611 * state at the time the snapshot was taken. This must be flushed
1612 * asynchronously back to the MDS once sync writes complete and dirty
1613 * data is written out.
1614 *
1615 * Called under i_ceph_lock.
1616 */
__ceph_flush_snaps(struct ceph_inode_info * ci,struct ceph_mds_session * session)1617 static void __ceph_flush_snaps(struct ceph_inode_info *ci,
1618 struct ceph_mds_session *session)
1619 __releases(ci->i_ceph_lock)
1620 __acquires(ci->i_ceph_lock)
1621 {
1622 struct inode *inode = &ci->netfs.inode;
1623 struct ceph_mds_client *mdsc = session->s_mdsc;
1624 struct ceph_client *cl = mdsc->fsc->client;
1625 struct ceph_cap_snap *capsnap;
1626 u64 oldest_flush_tid = 0;
1627 u64 first_tid = 1, last_tid = 0;
1628
1629 doutc(cl, "%p %llx.%llx session %p\n", inode, ceph_vinop(inode),
1630 session);
1631
1632 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
1633 /*
1634 * we need to wait for sync writes to complete and for dirty
1635 * pages to be written out.
1636 */
1637 if (capsnap->dirty_pages || capsnap->writing)
1638 break;
1639
1640 /* should be removed by ceph_try_drop_cap_snap() */
1641 BUG_ON(!capsnap->need_flush);
1642
1643 /* only flush each capsnap once */
1644 if (capsnap->cap_flush.tid > 0) {
1645 doutc(cl, "already flushed %p, skipping\n", capsnap);
1646 continue;
1647 }
1648
1649 spin_lock(&mdsc->cap_dirty_lock);
1650 capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid;
1651 capsnap->cap_flush.ci = ci;
1652 list_add_tail(&capsnap->cap_flush.g_list,
1653 &mdsc->cap_flush_list);
1654 if (oldest_flush_tid == 0)
1655 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1656 if (list_empty(&ci->i_flushing_item)) {
1657 list_add_tail(&ci->i_flushing_item,
1658 &session->s_cap_flushing);
1659 }
1660 spin_unlock(&mdsc->cap_dirty_lock);
1661
1662 list_add_tail(&capsnap->cap_flush.i_list,
1663 &ci->i_cap_flush_list);
1664
1665 if (first_tid == 1)
1666 first_tid = capsnap->cap_flush.tid;
1667 last_tid = capsnap->cap_flush.tid;
1668 }
1669
1670 clear_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags);
1671
1672 while (first_tid <= last_tid) {
1673 struct ceph_cap *cap = ci->i_auth_cap;
1674 struct ceph_cap_flush *cf = NULL, *iter;
1675 int ret;
1676
1677 if (!(cap && cap->session == session)) {
1678 doutc(cl, "%p %llx.%llx auth cap %p not mds%d, stop\n",
1679 inode, ceph_vinop(inode), cap, session->s_mds);
1680 break;
1681 }
1682
1683 ret = -ENOENT;
1684 list_for_each_entry(iter, &ci->i_cap_flush_list, i_list) {
1685 if (iter->tid >= first_tid) {
1686 cf = iter;
1687 ret = 0;
1688 break;
1689 }
1690 }
1691 if (ret < 0)
1692 break;
1693
1694 first_tid = cf->tid + 1;
1695
1696 capsnap = container_of(cf, struct ceph_cap_snap, cap_flush);
1697 refcount_inc(&capsnap->nref);
1698 spin_unlock(&ci->i_ceph_lock);
1699
1700 doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n", inode,
1701 ceph_vinop(inode), capsnap, cf->tid,
1702 ceph_cap_string(capsnap->dirty));
1703
1704 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
1705 oldest_flush_tid);
1706 if (ret < 0) {
1707 pr_err_client(cl, "error sending cap flushsnap, "
1708 "ino (%llx.%llx) tid %llu follows %llu\n",
1709 ceph_vinop(inode), cf->tid,
1710 capsnap->follows);
1711 }
1712
1713 ceph_put_cap_snap(capsnap);
1714 spin_lock(&ci->i_ceph_lock);
1715 }
1716 }
1717
ceph_flush_snaps(struct ceph_inode_info * ci,struct ceph_mds_session ** psession)1718 void ceph_flush_snaps(struct ceph_inode_info *ci,
1719 struct ceph_mds_session **psession)
1720 {
1721 struct inode *inode = &ci->netfs.inode;
1722 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
1723 struct ceph_client *cl = ceph_inode_to_client(inode);
1724 struct ceph_mds_session *session = NULL;
1725 bool need_put = false;
1726 int mds;
1727
1728 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
1729 if (psession)
1730 session = *psession;
1731 retry:
1732 spin_lock(&ci->i_ceph_lock);
1733 if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) {
1734 doutc(cl, " no capsnap needs flush, doing nothing\n");
1735 goto out;
1736 }
1737 if (!ci->i_auth_cap) {
1738 doutc(cl, " no auth cap (migrating?), doing nothing\n");
1739 goto out;
1740 }
1741
1742 mds = ci->i_auth_cap->session->s_mds;
1743 if (session && session->s_mds != mds) {
1744 doutc(cl, " oops, wrong session %p mutex\n", session);
1745 ceph_put_mds_session(session);
1746 session = NULL;
1747 }
1748 if (!session) {
1749 spin_unlock(&ci->i_ceph_lock);
1750 mutex_lock(&mdsc->mutex);
1751 session = __ceph_lookup_mds_session(mdsc, mds);
1752 mutex_unlock(&mdsc->mutex);
1753 goto retry;
1754 }
1755
1756 // make sure flushsnap messages are sent in proper order.
1757 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
1758 __kick_flushing_caps(mdsc, session, ci, 0);
1759
1760 __ceph_flush_snaps(ci, session);
1761 out:
1762 spin_unlock(&ci->i_ceph_lock);
1763
1764 if (psession)
1765 *psession = session;
1766 else
1767 ceph_put_mds_session(session);
1768 /* we flushed them all; remove this inode from the queue */
1769 spin_lock(&mdsc->snap_flush_lock);
1770 if (!list_empty(&ci->i_snap_flush_item))
1771 need_put = true;
1772 list_del_init(&ci->i_snap_flush_item);
1773 spin_unlock(&mdsc->snap_flush_lock);
1774
1775 if (need_put)
1776 iput(inode);
1777 }
1778
1779 /*
1780 * Mark caps dirty. If inode is newly dirty, return the dirty flags.
1781 * Caller is then responsible for calling __mark_inode_dirty with the
1782 * returned flags value.
1783 */
__ceph_mark_dirty_caps(struct ceph_inode_info * ci,int mask,struct ceph_cap_flush ** pcf)1784 int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
1785 struct ceph_cap_flush **pcf)
1786 {
1787 struct ceph_mds_client *mdsc =
1788 ceph_sb_to_fs_client(ci->netfs.inode.i_sb)->mdsc;
1789 struct inode *inode = &ci->netfs.inode;
1790 struct ceph_client *cl = ceph_inode_to_client(inode);
1791 int was = ci->i_dirty_caps;
1792 int dirty = 0;
1793
1794 lockdep_assert_held(&ci->i_ceph_lock);
1795
1796 if (!ci->i_auth_cap) {
1797 pr_warn_client(cl, "%p %llx.%llx mask %s, "
1798 "but no auth cap (session was closed?)\n",
1799 inode, ceph_vinop(inode),
1800 ceph_cap_string(mask));
1801 return 0;
1802 }
1803
1804 doutc(cl, "%p %llx.%llx %s dirty %s -> %s\n", inode,
1805 ceph_vinop(inode), ceph_cap_string(mask),
1806 ceph_cap_string(was), ceph_cap_string(was | mask));
1807 ci->i_dirty_caps |= mask;
1808 if (was == 0) {
1809 struct ceph_mds_session *session = ci->i_auth_cap->session;
1810
1811 WARN_ON_ONCE(ci->i_prealloc_cap_flush);
1812 swap(ci->i_prealloc_cap_flush, *pcf);
1813
1814 if (!ci->i_head_snapc) {
1815 WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem));
1816 ci->i_head_snapc = ceph_get_snap_context(
1817 ci->i_snap_realm->cached_context);
1818 }
1819 doutc(cl, "%p %llx.%llx now dirty snapc %p auth cap %p\n",
1820 inode, ceph_vinop(inode), ci->i_head_snapc,
1821 ci->i_auth_cap);
1822 BUG_ON(!list_empty(&ci->i_dirty_item));
1823 spin_lock(&mdsc->cap_dirty_lock);
1824 list_add(&ci->i_dirty_item, &session->s_cap_dirty);
1825 spin_unlock(&mdsc->cap_dirty_lock);
1826 if (ci->i_flushing_caps == 0) {
1827 ihold(inode);
1828 dirty |= I_DIRTY_SYNC;
1829 }
1830 } else {
1831 WARN_ON_ONCE(!ci->i_prealloc_cap_flush);
1832 }
1833 BUG_ON(list_empty(&ci->i_dirty_item));
1834 if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) &&
1835 (mask & CEPH_CAP_FILE_BUFFER))
1836 dirty |= I_DIRTY_DATASYNC;
1837 __cap_delay_requeue(mdsc, ci);
1838 return dirty;
1839 }
1840
ceph_alloc_cap_flush(void)1841 struct ceph_cap_flush *ceph_alloc_cap_flush(void)
1842 {
1843 struct ceph_cap_flush *cf;
1844
1845 cf = kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL);
1846 if (!cf)
1847 return NULL;
1848
1849 cf->is_capsnap = false;
1850 cf->ci = NULL;
1851 return cf;
1852 }
1853
ceph_free_cap_flush(struct ceph_cap_flush * cf)1854 void ceph_free_cap_flush(struct ceph_cap_flush *cf)
1855 {
1856 if (cf)
1857 kmem_cache_free(ceph_cap_flush_cachep, cf);
1858 }
1859
__get_oldest_flush_tid(struct ceph_mds_client * mdsc)1860 static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc)
1861 {
1862 if (!list_empty(&mdsc->cap_flush_list)) {
1863 struct ceph_cap_flush *cf =
1864 list_first_entry(&mdsc->cap_flush_list,
1865 struct ceph_cap_flush, g_list);
1866 return cf->tid;
1867 }
1868 return 0;
1869 }
1870
1871 /*
1872 * Remove cap_flush from the mdsc's or inode's flushing cap list.
1873 * Return true if caller needs to wake up flush waiters.
1874 */
__detach_cap_flush_from_mdsc(struct ceph_mds_client * mdsc,struct ceph_cap_flush * cf)1875 static bool __detach_cap_flush_from_mdsc(struct ceph_mds_client *mdsc,
1876 struct ceph_cap_flush *cf)
1877 {
1878 struct ceph_cap_flush *prev;
1879 bool wake = cf->wake;
1880
1881 if (wake && cf->g_list.prev != &mdsc->cap_flush_list) {
1882 prev = list_prev_entry(cf, g_list);
1883 prev->wake = true;
1884 wake = false;
1885 }
1886 list_del_init(&cf->g_list);
1887 return wake;
1888 }
1889
__detach_cap_flush_from_ci(struct ceph_inode_info * ci,struct ceph_cap_flush * cf)1890 static bool __detach_cap_flush_from_ci(struct ceph_inode_info *ci,
1891 struct ceph_cap_flush *cf)
1892 {
1893 struct ceph_cap_flush *prev;
1894 bool wake = cf->wake;
1895
1896 if (wake && cf->i_list.prev != &ci->i_cap_flush_list) {
1897 prev = list_prev_entry(cf, i_list);
1898 prev->wake = true;
1899 wake = false;
1900 }
1901 list_del_init(&cf->i_list);
1902 return wake;
1903 }
1904
1905 /*
1906 * Add dirty inode to the flushing list. Assigned a seq number so we
1907 * can wait for caps to flush without starving.
1908 *
1909 * Called under i_ceph_lock. Returns the flush tid.
1910 */
__mark_caps_flushing(struct inode * inode,struct ceph_mds_session * session,bool wake,u64 * oldest_flush_tid)1911 static u64 __mark_caps_flushing(struct inode *inode,
1912 struct ceph_mds_session *session, bool wake,
1913 u64 *oldest_flush_tid)
1914 {
1915 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
1916 struct ceph_client *cl = ceph_inode_to_client(inode);
1917 struct ceph_inode_info *ci = ceph_inode(inode);
1918 struct ceph_cap_flush *cf = NULL;
1919 int flushing;
1920
1921 lockdep_assert_held(&ci->i_ceph_lock);
1922 BUG_ON(ci->i_dirty_caps == 0);
1923 BUG_ON(list_empty(&ci->i_dirty_item));
1924 BUG_ON(!ci->i_prealloc_cap_flush);
1925
1926 flushing = ci->i_dirty_caps;
1927 doutc(cl, "flushing %s, flushing_caps %s -> %s\n",
1928 ceph_cap_string(flushing),
1929 ceph_cap_string(ci->i_flushing_caps),
1930 ceph_cap_string(ci->i_flushing_caps | flushing));
1931 ci->i_flushing_caps |= flushing;
1932 ci->i_dirty_caps = 0;
1933 doutc(cl, "%p %llx.%llx now !dirty\n", inode, ceph_vinop(inode));
1934
1935 swap(cf, ci->i_prealloc_cap_flush);
1936 cf->ci = ci;
1937 cf->caps = flushing;
1938 cf->wake = wake;
1939
1940 spin_lock(&mdsc->cap_dirty_lock);
1941 list_del_init(&ci->i_dirty_item);
1942
1943 cf->tid = ++mdsc->last_cap_flush_tid;
1944 list_add_tail(&cf->g_list, &mdsc->cap_flush_list);
1945 *oldest_flush_tid = __get_oldest_flush_tid(mdsc);
1946
1947 if (list_empty(&ci->i_flushing_item)) {
1948 list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing);
1949 mdsc->num_cap_flushing++;
1950 }
1951 spin_unlock(&mdsc->cap_dirty_lock);
1952
1953 list_add_tail(&cf->i_list, &ci->i_cap_flush_list);
1954
1955 return cf->tid;
1956 }
1957
1958 /*
1959 * try to invalidate mapping pages without blocking.
1960 */
try_nonblocking_invalidate(struct inode * inode)1961 static int try_nonblocking_invalidate(struct inode *inode)
1962 __releases(ci->i_ceph_lock)
1963 __acquires(ci->i_ceph_lock)
1964 {
1965 struct ceph_client *cl = ceph_inode_to_client(inode);
1966 struct ceph_inode_info *ci = ceph_inode(inode);
1967 u32 invalidating_gen = ci->i_rdcache_gen;
1968
1969 spin_unlock(&ci->i_ceph_lock);
1970 ceph_fscache_invalidate(inode, false);
1971 invalidate_mapping_pages(&inode->i_data, 0, -1);
1972 spin_lock(&ci->i_ceph_lock);
1973
1974 if (inode->i_data.nrpages == 0 &&
1975 invalidating_gen == ci->i_rdcache_gen) {
1976 /* success. */
1977 doutc(cl, "%p %llx.%llx success\n", inode,
1978 ceph_vinop(inode));
1979 /* save any racing async invalidate some trouble */
1980 ci->i_rdcache_revoking = ci->i_rdcache_gen - 1;
1981 return 0;
1982 }
1983 doutc(cl, "%p %llx.%llx failed\n", inode, ceph_vinop(inode));
1984 return -1;
1985 }
1986
__ceph_should_report_size(struct ceph_inode_info * ci)1987 bool __ceph_should_report_size(struct ceph_inode_info *ci)
1988 {
1989 loff_t size = i_size_read(&ci->netfs.inode);
1990 /* mds will adjust max size according to the reported size */
1991 if (ci->i_flushing_caps & CEPH_CAP_FILE_WR)
1992 return false;
1993 if (size >= ci->i_max_size)
1994 return true;
1995 /* half of previous max_size increment has been used */
1996 if (ci->i_max_size > ci->i_reported_size &&
1997 (size << 1) >= ci->i_max_size + ci->i_reported_size)
1998 return true;
1999 return false;
2000 }
2001
2002 /*
2003 * Swiss army knife function to examine currently used and wanted
2004 * versus held caps. Release, flush, ack revoked caps to mds as
2005 * appropriate.
2006 *
2007 * CHECK_CAPS_AUTHONLY - we should only check the auth cap
2008 * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without
2009 * further delay.
2010 * CHECK_CAPS_FLUSH_FORCE - we should flush any caps immediately, without
2011 * further delay.
2012 */
ceph_check_caps(struct ceph_inode_info * ci,int flags)2013 void ceph_check_caps(struct ceph_inode_info *ci, int flags)
2014 {
2015 struct inode *inode = &ci->netfs.inode;
2016 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
2017 struct ceph_client *cl = ceph_inode_to_client(inode);
2018 struct ceph_cap *cap;
2019 u64 flush_tid, oldest_flush_tid;
2020 int file_wanted, used, cap_used;
2021 int issued, implemented, want, retain, revoking, flushing = 0;
2022 int mds = -1; /* keep track of how far we've gone through i_caps list
2023 to avoid an infinite loop on retry */
2024 struct rb_node *p;
2025 bool queue_invalidate = false;
2026 bool tried_invalidate = false;
2027 bool queue_writeback = false;
2028 struct ceph_mds_session *session = NULL;
2029
2030 spin_lock(&ci->i_ceph_lock);
2031 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
2032 set_bit(CEPH_I_ASYNC_CHECK_CAPS_BIT, &ci->i_ceph_flags);
2033
2034 /* Don't send messages until we get async create reply */
2035 spin_unlock(&ci->i_ceph_lock);
2036 return;
2037 }
2038
2039 if (ci->i_ceph_flags & CEPH_I_FLUSH)
2040 flags |= CHECK_CAPS_FLUSH;
2041 retry:
2042 /* Caps wanted by virtue of active open files. */
2043 file_wanted = __ceph_caps_file_wanted(ci);
2044
2045 /* Caps which have active references against them */
2046 used = __ceph_caps_used(ci);
2047
2048 /*
2049 * "issued" represents the current caps that the MDS wants us to have.
2050 * "implemented" is the set that we have been granted, and includes the
2051 * ones that have not yet been returned to the MDS (the "revoking" set,
2052 * usually because they have outstanding references).
2053 */
2054 issued = __ceph_caps_issued(ci, &implemented);
2055 revoking = implemented & ~issued;
2056
2057 want = file_wanted;
2058
2059 /* The ones we currently want to retain (may be adjusted below) */
2060 retain = file_wanted | used | CEPH_CAP_PIN;
2061 if (!mdsc->stopping && inode->i_nlink > 0) {
2062 if (file_wanted) {
2063 retain |= CEPH_CAP_ANY; /* be greedy */
2064 } else if (S_ISDIR(inode->i_mode) &&
2065 (issued & CEPH_CAP_FILE_SHARED) &&
2066 __ceph_dir_is_complete(ci)) {
2067 /*
2068 * If a directory is complete, we want to keep
2069 * the exclusive cap. So that MDS does not end up
2070 * revoking the shared cap on every create/unlink
2071 * operation.
2072 */
2073 if (IS_RDONLY(inode)) {
2074 want = CEPH_CAP_ANY_SHARED;
2075 } else {
2076 want |= CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL;
2077 }
2078 retain |= want;
2079 } else {
2080
2081 retain |= CEPH_CAP_ANY_SHARED;
2082 /*
2083 * keep RD only if we didn't have the file open RW,
2084 * because then the mds would revoke it anyway to
2085 * journal max_size=0.
2086 */
2087 if (ci->i_max_size == 0)
2088 retain |= CEPH_CAP_ANY_RD;
2089 }
2090 }
2091
2092 doutc(cl, "%p %llx.%llx file_want %s used %s dirty %s "
2093 "flushing %s issued %s revoking %s retain %s %s%s%s%s\n",
2094 inode, ceph_vinop(inode), ceph_cap_string(file_wanted),
2095 ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps),
2096 ceph_cap_string(ci->i_flushing_caps),
2097 ceph_cap_string(issued), ceph_cap_string(revoking),
2098 ceph_cap_string(retain),
2099 (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "",
2100 (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : "",
2101 (flags & CHECK_CAPS_NOINVAL) ? " NOINVAL" : "",
2102 (flags & CHECK_CAPS_FLUSH_FORCE) ? " FLUSH_FORCE" : "");
2103
2104 /*
2105 * If we no longer need to hold onto old our caps, and we may
2106 * have cached pages, but don't want them, then try to invalidate.
2107 * If we fail, it's because pages are locked.... try again later.
2108 */
2109 if ((!(flags & CHECK_CAPS_NOINVAL) || mdsc->stopping) &&
2110 S_ISREG(inode->i_mode) &&
2111 !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */
2112 inode->i_data.nrpages && /* have cached pages */
2113 (revoking & (CEPH_CAP_FILE_CACHE|
2114 CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */
2115 !tried_invalidate) {
2116 doutc(cl, "trying to invalidate on %p %llx.%llx\n",
2117 inode, ceph_vinop(inode));
2118 if (try_nonblocking_invalidate(inode) < 0) {
2119 doutc(cl, "queuing invalidate\n");
2120 queue_invalidate = true;
2121 ci->i_rdcache_revoking = ci->i_rdcache_gen;
2122 }
2123 tried_invalidate = true;
2124 goto retry;
2125 }
2126
2127 for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) {
2128 int mflags = 0;
2129 struct cap_msg_args arg;
2130
2131 cap = rb_entry(p, struct ceph_cap, ci_node);
2132
2133 /* avoid looping forever */
2134 if (mds >= cap->mds ||
2135 ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap))
2136 continue;
2137
2138 /*
2139 * If we have an auth cap, we don't need to consider any
2140 * overlapping caps as used.
2141 */
2142 cap_used = used;
2143 if (ci->i_auth_cap && cap != ci->i_auth_cap)
2144 cap_used &= ~ci->i_auth_cap->issued;
2145
2146 revoking = cap->implemented & ~cap->issued;
2147 doutc(cl, " mds%d cap %p used %s issued %s implemented %s revoking %s\n",
2148 cap->mds, cap, ceph_cap_string(cap_used),
2149 ceph_cap_string(cap->issued),
2150 ceph_cap_string(cap->implemented),
2151 ceph_cap_string(revoking));
2152
2153 /* completed revocation? going down and there are no caps? */
2154 if (revoking) {
2155 if ((revoking & cap_used) == 0) {
2156 doutc(cl, "completed revocation of %s\n",
2157 ceph_cap_string(cap->implemented & ~cap->issued));
2158 goto ack;
2159 }
2160
2161 /*
2162 * If the "i_wrbuffer_ref" was increased by mmap or generic
2163 * cache write just before the ceph_check_caps() is called,
2164 * the Fb capability revoking will fail this time. Then we
2165 * must wait for the BDI's delayed work to flush the dirty
2166 * pages and to release the "i_wrbuffer_ref", which will cost
2167 * at most 5 seconds. That means the MDS needs to wait at
2168 * most 5 seconds to finished the Fb capability's revocation.
2169 *
2170 * Let's queue a writeback for it.
2171 */
2172 if (S_ISREG(inode->i_mode) && ci->i_wrbuffer_ref &&
2173 (revoking & CEPH_CAP_FILE_BUFFER))
2174 queue_writeback = true;
2175 }
2176
2177 if (flags & CHECK_CAPS_FLUSH_FORCE) {
2178 doutc(cl, "force to flush caps\n");
2179 goto ack;
2180 }
2181
2182 if (cap == ci->i_auth_cap &&
2183 (cap->issued & CEPH_CAP_FILE_WR)) {
2184 /* request larger max_size from MDS? */
2185 if (ci->i_wanted_max_size > ci->i_max_size &&
2186 ci->i_wanted_max_size > ci->i_requested_max_size) {
2187 doutc(cl, "requesting new max_size\n");
2188 goto ack;
2189 }
2190
2191 /* approaching file_max? */
2192 if (__ceph_should_report_size(ci)) {
2193 doutc(cl, "i_size approaching max_size\n");
2194 goto ack;
2195 }
2196 }
2197 /* flush anything dirty? */
2198 if (cap == ci->i_auth_cap) {
2199 if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) {
2200 doutc(cl, "flushing dirty caps\n");
2201 goto ack;
2202 }
2203 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) {
2204 doutc(cl, "flushing snap caps\n");
2205 goto ack;
2206 }
2207 }
2208
2209 /* want more caps from mds? */
2210 if (want & ~cap->mds_wanted) {
2211 if (want & ~(cap->mds_wanted | cap->issued))
2212 goto ack;
2213 if (!__cap_is_valid(cap))
2214 goto ack;
2215 }
2216
2217 /* things we might delay */
2218 if ((cap->issued & ~retain) == 0)
2219 continue; /* nope, all good */
2220
2221 ack:
2222 ceph_put_mds_session(session);
2223 session = ceph_get_mds_session(cap->session);
2224
2225 /* kick flushing and flush snaps before sending normal
2226 * cap message */
2227 if (cap == ci->i_auth_cap &&
2228 (ci->i_ceph_flags &
2229 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) {
2230 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
2231 __kick_flushing_caps(mdsc, session, ci, 0);
2232 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2233 __ceph_flush_snaps(ci, session);
2234
2235 goto retry;
2236 }
2237
2238 if (cap == ci->i_auth_cap && ci->i_dirty_caps) {
2239 flushing = ci->i_dirty_caps;
2240 flush_tid = __mark_caps_flushing(inode, session, false,
2241 &oldest_flush_tid);
2242 if (flags & CHECK_CAPS_FLUSH &&
2243 list_empty(&session->s_cap_dirty))
2244 mflags |= CEPH_CLIENT_CAPS_SYNC;
2245 } else {
2246 flushing = 0;
2247 flush_tid = 0;
2248 spin_lock(&mdsc->cap_dirty_lock);
2249 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2250 spin_unlock(&mdsc->cap_dirty_lock);
2251 }
2252
2253 mds = cap->mds; /* remember mds, so we don't repeat */
2254
2255 __prep_cap(&arg, cap, CEPH_CAP_OP_UPDATE, mflags, cap_used,
2256 want, retain, flushing, flush_tid, oldest_flush_tid);
2257
2258 spin_unlock(&ci->i_ceph_lock);
2259 __send_cap(&arg, ci);
2260 spin_lock(&ci->i_ceph_lock);
2261
2262 goto retry; /* retake i_ceph_lock and restart our cap scan. */
2263 }
2264
2265 /* periodically re-calculate caps wanted by open files */
2266 if (__ceph_is_any_real_caps(ci) &&
2267 list_empty(&ci->i_cap_delay_list) &&
2268 (file_wanted & ~CEPH_CAP_PIN) &&
2269 !(used & (CEPH_CAP_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
2270 __cap_delay_requeue(mdsc, ci);
2271 }
2272
2273 spin_unlock(&ci->i_ceph_lock);
2274
2275 ceph_put_mds_session(session);
2276 if (queue_writeback)
2277 ceph_queue_writeback(inode);
2278 if (queue_invalidate)
2279 ceph_queue_invalidate(inode);
2280 }
2281
2282 /*
2283 * Try to flush dirty caps back to the auth mds.
2284 */
try_flush_caps(struct inode * inode,u64 * ptid)2285 static int try_flush_caps(struct inode *inode, u64 *ptid)
2286 {
2287 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
2288 struct ceph_inode_info *ci = ceph_inode(inode);
2289 int flushing = 0;
2290 u64 flush_tid = 0, oldest_flush_tid = 0;
2291
2292 spin_lock(&ci->i_ceph_lock);
2293 retry_locked:
2294 if (ci->i_dirty_caps && ci->i_auth_cap) {
2295 struct ceph_cap *cap = ci->i_auth_cap;
2296 struct cap_msg_args arg;
2297 struct ceph_mds_session *session = cap->session;
2298
2299 if (session->s_state < CEPH_MDS_SESSION_OPEN) {
2300 spin_unlock(&ci->i_ceph_lock);
2301 goto out;
2302 }
2303
2304 if (ci->i_ceph_flags &
2305 (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS)) {
2306 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH)
2307 __kick_flushing_caps(mdsc, session, ci, 0);
2308 if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)
2309 __ceph_flush_snaps(ci, session);
2310 goto retry_locked;
2311 }
2312
2313 flushing = ci->i_dirty_caps;
2314 flush_tid = __mark_caps_flushing(inode, session, true,
2315 &oldest_flush_tid);
2316
2317 __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH, CEPH_CLIENT_CAPS_SYNC,
2318 __ceph_caps_used(ci), __ceph_caps_wanted(ci),
2319 (cap->issued | cap->implemented),
2320 flushing, flush_tid, oldest_flush_tid);
2321 spin_unlock(&ci->i_ceph_lock);
2322
2323 __send_cap(&arg, ci);
2324 } else {
2325 if (!list_empty(&ci->i_cap_flush_list)) {
2326 struct ceph_cap_flush *cf =
2327 list_last_entry(&ci->i_cap_flush_list,
2328 struct ceph_cap_flush, i_list);
2329 cf->wake = true;
2330 flush_tid = cf->tid;
2331 }
2332 flushing = ci->i_flushing_caps;
2333 spin_unlock(&ci->i_ceph_lock);
2334 }
2335 out:
2336 *ptid = flush_tid;
2337 return flushing;
2338 }
2339
2340 /*
2341 * Return true if we've flushed caps through the given flush_tid.
2342 */
caps_are_flushed(struct inode * inode,u64 flush_tid)2343 static int caps_are_flushed(struct inode *inode, u64 flush_tid)
2344 {
2345 struct ceph_inode_info *ci = ceph_inode(inode);
2346 int ret = 1;
2347
2348 spin_lock(&ci->i_ceph_lock);
2349 if (!list_empty(&ci->i_cap_flush_list)) {
2350 struct ceph_cap_flush * cf =
2351 list_first_entry(&ci->i_cap_flush_list,
2352 struct ceph_cap_flush, i_list);
2353 if (cf->tid <= flush_tid)
2354 ret = 0;
2355 }
2356 spin_unlock(&ci->i_ceph_lock);
2357 return ret;
2358 }
2359
2360 /*
2361 * flush the mdlog and wait for any unsafe requests to complete.
2362 */
flush_mdlog_and_wait_inode_unsafe_requests(struct inode * inode)2363 static int flush_mdlog_and_wait_inode_unsafe_requests(struct inode *inode)
2364 {
2365 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
2366 struct ceph_client *cl = ceph_inode_to_client(inode);
2367 struct ceph_inode_info *ci = ceph_inode(inode);
2368 struct ceph_mds_request *req1 = NULL, *req2 = NULL;
2369 int ret, err = 0;
2370
2371 spin_lock(&ci->i_unsafe_lock);
2372 if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) {
2373 req1 = list_last_entry(&ci->i_unsafe_dirops,
2374 struct ceph_mds_request,
2375 r_unsafe_dir_item);
2376 ceph_mdsc_get_request(req1);
2377 }
2378 if (!list_empty(&ci->i_unsafe_iops)) {
2379 req2 = list_last_entry(&ci->i_unsafe_iops,
2380 struct ceph_mds_request,
2381 r_unsafe_target_item);
2382 ceph_mdsc_get_request(req2);
2383 }
2384 spin_unlock(&ci->i_unsafe_lock);
2385
2386 /*
2387 * Trigger to flush the journal logs in all the relevant MDSes
2388 * manually, or in the worst case we must wait at most 5 seconds
2389 * to wait the journal logs to be flushed by the MDSes periodically.
2390 */
2391 if (req1 || req2) {
2392 struct ceph_mds_request *req;
2393 struct ceph_mds_session **sessions;
2394 struct ceph_mds_session *s;
2395 unsigned int max_sessions;
2396 int i;
2397
2398 mutex_lock(&mdsc->mutex);
2399 max_sessions = mdsc->max_sessions;
2400
2401 sessions = kzalloc_objs(s, max_sessions);
2402 if (!sessions) {
2403 mutex_unlock(&mdsc->mutex);
2404 err = -ENOMEM;
2405 goto out;
2406 }
2407
2408 spin_lock(&ci->i_unsafe_lock);
2409 if (req1) {
2410 list_for_each_entry(req, &ci->i_unsafe_dirops,
2411 r_unsafe_dir_item) {
2412 s = req->r_session;
2413 if (!s)
2414 continue;
2415 if (!sessions[s->s_mds]) {
2416 s = ceph_get_mds_session(s);
2417 sessions[s->s_mds] = s;
2418 }
2419 }
2420 }
2421 if (req2) {
2422 list_for_each_entry(req, &ci->i_unsafe_iops,
2423 r_unsafe_target_item) {
2424 s = req->r_session;
2425 if (!s)
2426 continue;
2427 if (!sessions[s->s_mds]) {
2428 s = ceph_get_mds_session(s);
2429 sessions[s->s_mds] = s;
2430 }
2431 }
2432 }
2433 spin_unlock(&ci->i_unsafe_lock);
2434
2435 /* the auth MDS */
2436 spin_lock(&ci->i_ceph_lock);
2437 if (ci->i_auth_cap) {
2438 s = ci->i_auth_cap->session;
2439 if (!sessions[s->s_mds])
2440 sessions[s->s_mds] = ceph_get_mds_session(s);
2441 }
2442 spin_unlock(&ci->i_ceph_lock);
2443 mutex_unlock(&mdsc->mutex);
2444
2445 /* send flush mdlog request to MDSes */
2446 for (i = 0; i < max_sessions; i++) {
2447 s = sessions[i];
2448 if (s) {
2449 send_flush_mdlog(s);
2450 ceph_put_mds_session(s);
2451 }
2452 }
2453 kfree(sessions);
2454 }
2455
2456 doutc(cl, "%p %llx.%llx wait on tid %llu %llu\n", inode,
2457 ceph_vinop(inode), req1 ? req1->r_tid : 0ULL,
2458 req2 ? req2->r_tid : 0ULL);
2459 if (req1) {
2460 ret = !wait_for_completion_timeout(&req1->r_safe_completion,
2461 ceph_timeout_jiffies(req1->r_timeout));
2462 if (ret)
2463 err = -EIO;
2464 }
2465 if (req2) {
2466 ret = !wait_for_completion_timeout(&req2->r_safe_completion,
2467 ceph_timeout_jiffies(req2->r_timeout));
2468 if (ret)
2469 err = -EIO;
2470 }
2471
2472 out:
2473 if (req1)
2474 ceph_mdsc_put_request(req1);
2475 if (req2)
2476 ceph_mdsc_put_request(req2);
2477 return err;
2478 }
2479
ceph_fsync(struct file * file,loff_t start,loff_t end,int datasync)2480 int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2481 {
2482 struct inode *inode = file->f_mapping->host;
2483 struct ceph_inode_info *ci = ceph_inode(inode);
2484 struct ceph_client *cl = ceph_inode_to_client(inode);
2485 u64 flush_tid;
2486 int ret, err;
2487 int dirty;
2488
2489 doutc(cl, "%p %llx.%llx%s\n", inode, ceph_vinop(inode),
2490 datasync ? " datasync" : "");
2491
2492 ret = file_write_and_wait_range(file, start, end);
2493 if (datasync)
2494 goto out;
2495
2496 ret = ceph_wait_on_async_create(inode);
2497 if (ret)
2498 goto out;
2499
2500 dirty = try_flush_caps(inode, &flush_tid);
2501 doutc(cl, "dirty caps are %s\n", ceph_cap_string(dirty));
2502
2503 err = flush_mdlog_and_wait_inode_unsafe_requests(inode);
2504
2505 /*
2506 * only wait on non-file metadata writeback (the mds
2507 * can recover size and mtime, so we don't need to
2508 * wait for that)
2509 */
2510 if (!err && (dirty & ~CEPH_CAP_ANY_FILE_WR)) {
2511 err = wait_event_interruptible(ci->i_cap_wq,
2512 caps_are_flushed(inode, flush_tid));
2513 }
2514
2515 if (err < 0)
2516 ret = err;
2517
2518 err = file_check_and_advance_wb_err(file);
2519 if (err < 0)
2520 ret = err;
2521 out:
2522 doutc(cl, "%p %llx.%llx%s result=%d\n", inode, ceph_vinop(inode),
2523 datasync ? " datasync" : "", ret);
2524 return ret;
2525 }
2526
2527 /*
2528 * Flush any dirty caps back to the mds. If we aren't asked to wait,
2529 * queue inode for flush but don't do so immediately, because we can
2530 * get by with fewer MDS messages if we wait for data writeback to
2531 * complete first.
2532 */
ceph_write_inode(struct inode * inode,struct writeback_control * wbc)2533 int ceph_write_inode(struct inode *inode, struct writeback_control *wbc)
2534 {
2535 struct ceph_inode_info *ci = ceph_inode(inode);
2536 struct ceph_client *cl = ceph_inode_to_client(inode);
2537 u64 flush_tid;
2538 int err = 0;
2539 int dirty;
2540 int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync);
2541
2542 doutc(cl, "%p %llx.%llx wait=%d\n", inode, ceph_vinop(inode), wait);
2543 ceph_fscache_unpin_writeback(inode, wbc);
2544 if (wait) {
2545 err = ceph_wait_on_async_create(inode);
2546 if (err)
2547 return err;
2548 dirty = try_flush_caps(inode, &flush_tid);
2549 if (dirty)
2550 err = wait_event_interruptible(ci->i_cap_wq,
2551 caps_are_flushed(inode, flush_tid));
2552 } else {
2553 struct ceph_mds_client *mdsc =
2554 ceph_sb_to_fs_client(inode->i_sb)->mdsc;
2555
2556 spin_lock(&ci->i_ceph_lock);
2557 if (__ceph_caps_dirty(ci))
2558 __cap_delay_requeue_front(mdsc, ci);
2559 spin_unlock(&ci->i_ceph_lock);
2560 }
2561 return err;
2562 }
2563
__kick_flushing_caps(struct ceph_mds_client * mdsc,struct ceph_mds_session * session,struct ceph_inode_info * ci,u64 oldest_flush_tid)2564 static void __kick_flushing_caps(struct ceph_mds_client *mdsc,
2565 struct ceph_mds_session *session,
2566 struct ceph_inode_info *ci,
2567 u64 oldest_flush_tid)
2568 __releases(ci->i_ceph_lock)
2569 __acquires(ci->i_ceph_lock)
2570 {
2571 struct inode *inode = &ci->netfs.inode;
2572 struct ceph_client *cl = mdsc->fsc->client;
2573 struct ceph_cap *cap;
2574 struct ceph_cap_flush *cf;
2575 int ret;
2576 u64 first_tid = 0;
2577 u64 last_snap_flush = 0;
2578
2579 /* Don't do anything until create reply comes in */
2580 if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE)
2581 return;
2582
2583 clear_bit(CEPH_I_KICK_FLUSH_BIT, &ci->i_ceph_flags);
2584
2585 list_for_each_entry_reverse(cf, &ci->i_cap_flush_list, i_list) {
2586 if (cf->is_capsnap) {
2587 last_snap_flush = cf->tid;
2588 break;
2589 }
2590 }
2591
2592 list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) {
2593 if (cf->tid < first_tid)
2594 continue;
2595
2596 cap = ci->i_auth_cap;
2597 if (!(cap && cap->session == session)) {
2598 pr_err_client(cl, "%p auth cap %p not mds%d ???\n",
2599 inode, cap, session->s_mds);
2600 break;
2601 }
2602
2603 first_tid = cf->tid + 1;
2604
2605 if (!cf->is_capsnap) {
2606 struct cap_msg_args arg;
2607
2608 doutc(cl, "%p %llx.%llx cap %p tid %llu %s\n",
2609 inode, ceph_vinop(inode), cap, cf->tid,
2610 ceph_cap_string(cf->caps));
2611 __prep_cap(&arg, cap, CEPH_CAP_OP_FLUSH,
2612 (cf->tid < last_snap_flush ?
2613 CEPH_CLIENT_CAPS_PENDING_CAPSNAP : 0),
2614 __ceph_caps_used(ci),
2615 __ceph_caps_wanted(ci),
2616 (cap->issued | cap->implemented),
2617 cf->caps, cf->tid, oldest_flush_tid);
2618 spin_unlock(&ci->i_ceph_lock);
2619 __send_cap(&arg, ci);
2620 } else {
2621 struct ceph_cap_snap *capsnap =
2622 container_of(cf, struct ceph_cap_snap,
2623 cap_flush);
2624 doutc(cl, "%p %llx.%llx capsnap %p tid %llu %s\n",
2625 inode, ceph_vinop(inode), capsnap, cf->tid,
2626 ceph_cap_string(capsnap->dirty));
2627
2628 refcount_inc(&capsnap->nref);
2629 spin_unlock(&ci->i_ceph_lock);
2630
2631 ret = __send_flush_snap(inode, session, capsnap, cap->mseq,
2632 oldest_flush_tid);
2633 if (ret < 0) {
2634 pr_err_client(cl, "error sending cap flushsnap,"
2635 " %p %llx.%llx tid %llu follows %llu\n",
2636 inode, ceph_vinop(inode), cf->tid,
2637 capsnap->follows);
2638 }
2639
2640 ceph_put_cap_snap(capsnap);
2641 }
2642
2643 spin_lock(&ci->i_ceph_lock);
2644 }
2645 }
2646
ceph_early_kick_flushing_caps(struct ceph_mds_client * mdsc,struct ceph_mds_session * session)2647 void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
2648 struct ceph_mds_session *session)
2649 {
2650 struct ceph_client *cl = mdsc->fsc->client;
2651 struct ceph_inode_info *ci;
2652 struct ceph_cap *cap;
2653 u64 oldest_flush_tid;
2654
2655 doutc(cl, "mds%d\n", session->s_mds);
2656
2657 spin_lock(&mdsc->cap_dirty_lock);
2658 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2659 spin_unlock(&mdsc->cap_dirty_lock);
2660
2661 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2662 struct inode *inode = &ci->netfs.inode;
2663
2664 spin_lock(&ci->i_ceph_lock);
2665 cap = ci->i_auth_cap;
2666 if (!(cap && cap->session == session)) {
2667 pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n",
2668 inode, ceph_vinop(inode), cap,
2669 session->s_mds);
2670 spin_unlock(&ci->i_ceph_lock);
2671 continue;
2672 }
2673
2674
2675 /*
2676 * if flushing caps were revoked, we re-send the cap flush
2677 * in client reconnect stage. This guarantees MDS * processes
2678 * the cap flush message before issuing the flushing caps to
2679 * other client.
2680 */
2681 if ((cap->issued & ci->i_flushing_caps) !=
2682 ci->i_flushing_caps) {
2683 /* encode_caps_cb() also will reset these sequence
2684 * numbers. make sure sequence numbers in cap flush
2685 * message match later reconnect message */
2686 cap->seq = 0;
2687 cap->issue_seq = 0;
2688 cap->mseq = 0;
2689 __kick_flushing_caps(mdsc, session, ci,
2690 oldest_flush_tid);
2691 } else {
2692 set_bit(CEPH_I_KICK_FLUSH_BIT, &ci->i_ceph_flags);
2693 }
2694
2695 spin_unlock(&ci->i_ceph_lock);
2696 }
2697 }
2698
ceph_kick_flushing_caps(struct ceph_mds_client * mdsc,struct ceph_mds_session * session)2699 void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
2700 struct ceph_mds_session *session)
2701 {
2702 struct ceph_client *cl = mdsc->fsc->client;
2703 struct ceph_inode_info *ci;
2704 struct ceph_cap *cap;
2705 u64 oldest_flush_tid;
2706
2707 lockdep_assert_held(&session->s_mutex);
2708
2709 doutc(cl, "mds%d\n", session->s_mds);
2710
2711 spin_lock(&mdsc->cap_dirty_lock);
2712 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2713 spin_unlock(&mdsc->cap_dirty_lock);
2714
2715 list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) {
2716 struct inode *inode = &ci->netfs.inode;
2717
2718 spin_lock(&ci->i_ceph_lock);
2719 cap = ci->i_auth_cap;
2720 if (!(cap && cap->session == session)) {
2721 pr_err_client(cl, "%p %llx.%llx auth cap %p not mds%d ???\n",
2722 inode, ceph_vinop(inode), cap,
2723 session->s_mds);
2724 spin_unlock(&ci->i_ceph_lock);
2725 continue;
2726 }
2727 if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) {
2728 __kick_flushing_caps(mdsc, session, ci,
2729 oldest_flush_tid);
2730 }
2731 spin_unlock(&ci->i_ceph_lock);
2732 }
2733 }
2734
ceph_kick_flushing_inode_caps(struct ceph_mds_session * session,struct ceph_inode_info * ci)2735 void ceph_kick_flushing_inode_caps(struct ceph_mds_session *session,
2736 struct ceph_inode_info *ci)
2737 {
2738 struct ceph_mds_client *mdsc = session->s_mdsc;
2739 struct ceph_cap *cap = ci->i_auth_cap;
2740 struct inode *inode = &ci->netfs.inode;
2741
2742 lockdep_assert_held(&ci->i_ceph_lock);
2743
2744 doutc(mdsc->fsc->client, "%p %llx.%llx flushing %s\n",
2745 inode, ceph_vinop(inode),
2746 ceph_cap_string(ci->i_flushing_caps));
2747
2748 if (!list_empty(&ci->i_cap_flush_list)) {
2749 u64 oldest_flush_tid;
2750 spin_lock(&mdsc->cap_dirty_lock);
2751 list_move_tail(&ci->i_flushing_item,
2752 &cap->session->s_cap_flushing);
2753 oldest_flush_tid = __get_oldest_flush_tid(mdsc);
2754 spin_unlock(&mdsc->cap_dirty_lock);
2755
2756 __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid);
2757 }
2758 }
2759
2760
2761 /*
2762 * Take references to capabilities we hold, so that we don't release
2763 * them to the MDS prematurely.
2764 */
ceph_take_cap_refs(struct ceph_inode_info * ci,int got,bool snap_rwsem_locked)2765 void ceph_take_cap_refs(struct ceph_inode_info *ci, int got,
2766 bool snap_rwsem_locked)
2767 {
2768 struct inode *inode = &ci->netfs.inode;
2769 struct ceph_client *cl = ceph_inode_to_client(inode);
2770
2771 lockdep_assert_held(&ci->i_ceph_lock);
2772
2773 if (got & CEPH_CAP_PIN)
2774 ci->i_pin_ref++;
2775 if (got & CEPH_CAP_FILE_RD)
2776 ci->i_rd_ref++;
2777 if (got & CEPH_CAP_FILE_CACHE)
2778 ci->i_rdcache_ref++;
2779 if (got & CEPH_CAP_FILE_EXCL)
2780 ci->i_fx_ref++;
2781 if (got & CEPH_CAP_FILE_WR) {
2782 if (ci->i_wr_ref == 0 && !ci->i_head_snapc) {
2783 BUG_ON(!snap_rwsem_locked);
2784 ci->i_head_snapc = ceph_get_snap_context(
2785 ci->i_snap_realm->cached_context);
2786 }
2787 ci->i_wr_ref++;
2788 }
2789 if (got & CEPH_CAP_FILE_BUFFER) {
2790 if (ci->i_wb_ref == 0)
2791 ihold(inode);
2792 ci->i_wb_ref++;
2793 doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode,
2794 ceph_vinop(inode), ci->i_wb_ref-1, ci->i_wb_ref);
2795 }
2796 }
2797
2798 /*
2799 * Try to grab cap references. Specify those refs we @want, and the
2800 * minimal set we @need. Also include the larger offset we are writing
2801 * to (when applicable), and check against max_size here as well.
2802 * Note that caller is responsible for ensuring max_size increases are
2803 * requested from the MDS.
2804 *
2805 * Returns 0 if caps were not able to be acquired (yet), 1 if succeed,
2806 * or a negative error code. There are 3 special error codes:
2807 * -EAGAIN: need to sleep but non-blocking is specified
2808 * -EFBIG: ask caller to call check_max_size() and try again.
2809 * -EUCLEAN: ask caller to call ceph_renew_caps() and try again.
2810 */
2811 enum {
2812 /* first 8 bits are reserved for CEPH_FILE_MODE_FOO */
2813 NON_BLOCKING = (1 << 8),
2814 CHECK_FILELOCK = (1 << 9),
2815 };
2816
try_get_cap_refs(struct inode * inode,int need,int want,loff_t endoff,int flags,int * got)2817 static int try_get_cap_refs(struct inode *inode, int need, int want,
2818 loff_t endoff, int flags, int *got)
2819 {
2820 struct ceph_inode_info *ci = ceph_inode(inode);
2821 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
2822 struct ceph_client *cl = ceph_inode_to_client(inode);
2823 int ret = 0;
2824 int have, implemented;
2825 bool snap_rwsem_locked = false;
2826
2827 doutc(cl, "%p %llx.%llx need %s want %s\n", inode,
2828 ceph_vinop(inode), ceph_cap_string(need),
2829 ceph_cap_string(want));
2830
2831 again:
2832 spin_lock(&ci->i_ceph_lock);
2833
2834 if ((flags & CHECK_FILELOCK) &&
2835 test_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags)) {
2836 doutc(cl, "%p %llx.%llx error filelock\n", inode,
2837 ceph_vinop(inode));
2838 ret = -EIO;
2839 goto out_unlock;
2840 }
2841
2842 /* finish pending truncate */
2843 while (ci->i_truncate_pending) {
2844 spin_unlock(&ci->i_ceph_lock);
2845 if (snap_rwsem_locked) {
2846 up_read(&mdsc->snap_rwsem);
2847 snap_rwsem_locked = false;
2848 }
2849 __ceph_do_pending_vmtruncate(inode);
2850 spin_lock(&ci->i_ceph_lock);
2851 }
2852
2853 have = __ceph_caps_issued(ci, &implemented);
2854
2855 if (have & need & CEPH_CAP_FILE_WR) {
2856 if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) {
2857 doutc(cl, "%p %llx.%llx endoff %llu > maxsize %llu\n",
2858 inode, ceph_vinop(inode), endoff, ci->i_max_size);
2859 if (endoff > ci->i_requested_max_size)
2860 ret = ci->i_auth_cap ? -EFBIG : -EUCLEAN;
2861 goto out_unlock;
2862 }
2863 /*
2864 * If a sync write is in progress, we must wait, so that we
2865 * can get a final snapshot value for size+mtime.
2866 */
2867 if (__ceph_have_pending_cap_snap(ci)) {
2868 doutc(cl, "%p %llx.%llx cap_snap_pending\n", inode,
2869 ceph_vinop(inode));
2870 goto out_unlock;
2871 }
2872 }
2873
2874 if ((have & need) == need) {
2875 /*
2876 * Look at (implemented & ~have & not) so that we keep waiting
2877 * on transition from wanted -> needed caps. This is needed
2878 * for WRBUFFER|WR -> WR to avoid a new WR sync write from
2879 * going before a prior buffered writeback happens.
2880 *
2881 * For RDCACHE|RD -> RD, there is not need to wait and we can
2882 * just exclude the revoking caps and force to sync read.
2883 */
2884 int not = want & ~(have & need);
2885 int revoking = implemented & ~have;
2886 int exclude = revoking & not;
2887 doutc(cl, "%p %llx.%llx have %s but not %s (revoking %s)\n",
2888 inode, ceph_vinop(inode), ceph_cap_string(have),
2889 ceph_cap_string(not), ceph_cap_string(revoking));
2890 if (!exclude || !(exclude & CEPH_CAP_FILE_BUFFER)) {
2891 if (!snap_rwsem_locked &&
2892 !ci->i_head_snapc &&
2893 (need & CEPH_CAP_FILE_WR)) {
2894 if (!down_read_trylock(&mdsc->snap_rwsem)) {
2895 /*
2896 * we can not call down_read() when
2897 * task isn't in TASK_RUNNING state
2898 */
2899 if (flags & NON_BLOCKING) {
2900 ret = -EAGAIN;
2901 goto out_unlock;
2902 }
2903
2904 spin_unlock(&ci->i_ceph_lock);
2905 down_read(&mdsc->snap_rwsem);
2906 snap_rwsem_locked = true;
2907 goto again;
2908 }
2909 snap_rwsem_locked = true;
2910 }
2911 if ((have & want) == want)
2912 *got = need | (want & ~exclude);
2913 else
2914 *got = need;
2915 ceph_take_cap_refs(ci, *got, true);
2916 ret = 1;
2917 }
2918 } else {
2919 int session_readonly = false;
2920 int mds_wanted;
2921 if (ci->i_auth_cap &&
2922 (need & (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_EXCL))) {
2923 struct ceph_mds_session *s = ci->i_auth_cap->session;
2924 spin_lock(&s->s_cap_lock);
2925 session_readonly = s->s_readonly;
2926 spin_unlock(&s->s_cap_lock);
2927 }
2928 if (session_readonly) {
2929 doutc(cl, "%p %llx.%llx need %s but mds%d readonly\n",
2930 inode, ceph_vinop(inode), ceph_cap_string(need),
2931 ci->i_auth_cap->mds);
2932 ret = -EROFS;
2933 goto out_unlock;
2934 }
2935
2936 if (ceph_inode_is_shutdown(inode)) {
2937 doutc(cl, "%p %llx.%llx inode is shutdown\n",
2938 inode, ceph_vinop(inode));
2939 ret = -ESTALE;
2940 goto out_unlock;
2941 }
2942 mds_wanted = __ceph_caps_mds_wanted(ci, false);
2943 if (need & ~mds_wanted) {
2944 doutc(cl, "%p %llx.%llx need %s > mds_wanted %s\n",
2945 inode, ceph_vinop(inode), ceph_cap_string(need),
2946 ceph_cap_string(mds_wanted));
2947 ret = -EUCLEAN;
2948 goto out_unlock;
2949 }
2950
2951 doutc(cl, "%p %llx.%llx have %s need %s\n", inode,
2952 ceph_vinop(inode), ceph_cap_string(have),
2953 ceph_cap_string(need));
2954 }
2955 out_unlock:
2956
2957 __ceph_touch_fmode(ci, mdsc, flags);
2958
2959 spin_unlock(&ci->i_ceph_lock);
2960 if (snap_rwsem_locked)
2961 up_read(&mdsc->snap_rwsem);
2962
2963 if (!ret)
2964 ceph_update_cap_mis(&mdsc->metric);
2965 else if (ret == 1)
2966 ceph_update_cap_hit(&mdsc->metric);
2967
2968 doutc(cl, "%p %llx.%llx ret %d got %s\n", inode,
2969 ceph_vinop(inode), ret, ceph_cap_string(*got));
2970 return ret;
2971 }
2972
2973 /*
2974 * Check the offset we are writing up to against our current
2975 * max_size. If necessary, tell the MDS we want to write to
2976 * a larger offset.
2977 */
check_max_size(struct inode * inode,loff_t endoff)2978 static void check_max_size(struct inode *inode, loff_t endoff)
2979 {
2980 struct ceph_inode_info *ci = ceph_inode(inode);
2981 struct ceph_client *cl = ceph_inode_to_client(inode);
2982 int check = 0;
2983
2984 /* do we need to explicitly request a larger max_size? */
2985 spin_lock(&ci->i_ceph_lock);
2986 if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) {
2987 doutc(cl, "write %p %llx.%llx at large endoff %llu, req max_size\n",
2988 inode, ceph_vinop(inode), endoff);
2989 ci->i_wanted_max_size = endoff;
2990 }
2991 /* duplicate ceph_check_caps()'s logic */
2992 if (ci->i_auth_cap &&
2993 (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) &&
2994 ci->i_wanted_max_size > ci->i_max_size &&
2995 ci->i_wanted_max_size > ci->i_requested_max_size)
2996 check = 1;
2997 spin_unlock(&ci->i_ceph_lock);
2998 if (check)
2999 ceph_check_caps(ci, CHECK_CAPS_AUTHONLY);
3000 }
3001
get_used_fmode(int caps)3002 static inline int get_used_fmode(int caps)
3003 {
3004 int fmode = 0;
3005 if (caps & CEPH_CAP_FILE_RD)
3006 fmode |= CEPH_FILE_MODE_RD;
3007 if (caps & CEPH_CAP_FILE_WR)
3008 fmode |= CEPH_FILE_MODE_WR;
3009 return fmode;
3010 }
3011
ceph_try_get_caps(struct inode * inode,int need,int want,bool nonblock,int * got)3012 int ceph_try_get_caps(struct inode *inode, int need, int want,
3013 bool nonblock, int *got)
3014 {
3015 int ret, flags;
3016
3017 BUG_ON(need & ~CEPH_CAP_FILE_RD);
3018 BUG_ON(want & ~(CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO |
3019 CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
3020 CEPH_CAP_ANY_DIR_OPS));
3021 if (need) {
3022 ret = ceph_pool_perm_check(inode, need);
3023 if (ret < 0)
3024 return ret;
3025 }
3026
3027 flags = get_used_fmode(need | want);
3028 if (nonblock)
3029 flags |= NON_BLOCKING;
3030
3031 ret = try_get_cap_refs(inode, need, want, 0, flags, got);
3032 /* three special error codes */
3033 if (ret == -EAGAIN || ret == -EFBIG || ret == -EUCLEAN)
3034 ret = 0;
3035 return ret;
3036 }
3037
3038 /*
3039 * Wait for caps, and take cap references. If we can't get a WR cap
3040 * due to a small max_size, make sure we check_max_size (and possibly
3041 * ask the mds) so we don't get hung up indefinitely.
3042 */
__ceph_get_caps(struct inode * inode,struct ceph_file_info * fi,int need,int want,loff_t endoff,int * got)3043 int __ceph_get_caps(struct inode *inode, struct ceph_file_info *fi, int need,
3044 int want, loff_t endoff, int *got)
3045 {
3046 struct ceph_inode_info *ci = ceph_inode(inode);
3047 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
3048 int ret, _got, flags;
3049
3050 ret = ceph_pool_perm_check(inode, need);
3051 if (ret < 0)
3052 return ret;
3053
3054 if (fi && (fi->fmode & CEPH_FILE_MODE_WR) &&
3055 fi->filp_gen != READ_ONCE(fsc->filp_gen))
3056 return -EBADF;
3057
3058 flags = get_used_fmode(need | want);
3059
3060 while (true) {
3061 flags &= CEPH_FILE_MODE_MASK;
3062 if (vfs_inode_has_locks(inode))
3063 flags |= CHECK_FILELOCK;
3064 _got = 0;
3065 ret = try_get_cap_refs(inode, need, want, endoff,
3066 flags, &_got);
3067 WARN_ON_ONCE(ret == -EAGAIN);
3068 if (!ret) {
3069 #ifdef CONFIG_DEBUG_FS
3070 struct ceph_mds_client *mdsc = fsc->mdsc;
3071 struct cap_wait cw;
3072 #endif
3073 DEFINE_WAIT_FUNC(wait, woken_wake_function);
3074
3075 #ifdef CONFIG_DEBUG_FS
3076 cw.ino = ceph_ino(inode);
3077 cw.tgid = current->tgid;
3078 cw.need = need;
3079 cw.want = want;
3080
3081 spin_lock(&mdsc->caps_list_lock);
3082 list_add(&cw.list, &mdsc->cap_wait_list);
3083 spin_unlock(&mdsc->caps_list_lock);
3084 #endif
3085
3086 /* make sure used fmode not timeout */
3087 ceph_get_fmode(ci, flags, FMODE_WAIT_BIAS);
3088 add_wait_queue(&ci->i_cap_wq, &wait);
3089
3090 flags |= NON_BLOCKING;
3091 while (!(ret = try_get_cap_refs(inode, need, want,
3092 endoff, flags, &_got))) {
3093 if (signal_pending(current)) {
3094 ret = -ERESTARTSYS;
3095 break;
3096 }
3097 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
3098 }
3099
3100 remove_wait_queue(&ci->i_cap_wq, &wait);
3101 ceph_put_fmode(ci, flags, FMODE_WAIT_BIAS);
3102
3103 #ifdef CONFIG_DEBUG_FS
3104 spin_lock(&mdsc->caps_list_lock);
3105 list_del(&cw.list);
3106 spin_unlock(&mdsc->caps_list_lock);
3107 #endif
3108
3109 if (ret == -EAGAIN)
3110 continue;
3111 }
3112
3113 if (fi && (fi->fmode & CEPH_FILE_MODE_WR) &&
3114 fi->filp_gen != READ_ONCE(fsc->filp_gen)) {
3115 if (ret >= 0 && _got)
3116 ceph_put_cap_refs(ci, _got);
3117 return -EBADF;
3118 }
3119
3120 if (ret < 0) {
3121 if (ret == -EFBIG || ret == -EUCLEAN) {
3122 int ret2 = ceph_wait_on_async_create(inode);
3123 if (ret2 < 0)
3124 return ret2;
3125 }
3126 if (ret == -EFBIG) {
3127 check_max_size(inode, endoff);
3128 continue;
3129 }
3130 if (ret == -EUCLEAN) {
3131 /* session was killed, try renew caps */
3132 ret = ceph_renew_caps(inode, flags);
3133 if (ret == 0)
3134 continue;
3135 }
3136 return ret;
3137 }
3138
3139 if (S_ISREG(ci->netfs.inode.i_mode) &&
3140 ceph_has_inline_data(ci) &&
3141 (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) &&
3142 i_size_read(inode) > 0) {
3143 struct page *page =
3144 find_get_page(inode->i_mapping, 0);
3145 if (page) {
3146 bool uptodate = PageUptodate(page);
3147
3148 put_page(page);
3149 if (uptodate)
3150 break;
3151 }
3152 /*
3153 * drop cap refs first because getattr while
3154 * holding * caps refs can cause deadlock.
3155 */
3156 ceph_put_cap_refs(ci, _got);
3157 _got = 0;
3158
3159 /*
3160 * getattr request will bring inline data into
3161 * page cache
3162 */
3163 ret = __ceph_do_getattr(inode, NULL,
3164 CEPH_STAT_CAP_INLINE_DATA,
3165 true);
3166 if (ret < 0)
3167 return ret;
3168 continue;
3169 }
3170 break;
3171 }
3172 *got = _got;
3173 return 0;
3174 }
3175
ceph_get_caps(struct file * filp,int need,int want,loff_t endoff,int * got)3176 int ceph_get_caps(struct file *filp, int need, int want, loff_t endoff,
3177 int *got)
3178 {
3179 struct ceph_file_info *fi = filp->private_data;
3180 struct inode *inode = file_inode(filp);
3181
3182 return __ceph_get_caps(inode, fi, need, want, endoff, got);
3183 }
3184
3185 /*
3186 * Take cap refs. Caller must already know we hold at least one ref
3187 * on the caps in question or we don't know this is safe.
3188 */
ceph_get_cap_refs(struct ceph_inode_info * ci,int caps)3189 void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps)
3190 {
3191 spin_lock(&ci->i_ceph_lock);
3192 ceph_take_cap_refs(ci, caps, false);
3193 spin_unlock(&ci->i_ceph_lock);
3194 }
3195
3196
3197 /*
3198 * drop cap_snap that is not associated with any snapshot.
3199 * we don't need to send FLUSHSNAP message for it.
3200 */
ceph_try_drop_cap_snap(struct ceph_inode_info * ci,struct ceph_cap_snap * capsnap)3201 static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci,
3202 struct ceph_cap_snap *capsnap)
3203 {
3204 struct inode *inode = &ci->netfs.inode;
3205 struct ceph_client *cl = ceph_inode_to_client(inode);
3206
3207 if (!capsnap->need_flush &&
3208 !capsnap->writing && !capsnap->dirty_pages) {
3209 doutc(cl, "%p follows %llu\n", capsnap, capsnap->follows);
3210 BUG_ON(capsnap->cap_flush.tid > 0);
3211 ceph_put_snap_context(capsnap->context);
3212 if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps))
3213 set_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags);
3214
3215 list_del(&capsnap->ci_item);
3216 ceph_put_cap_snap(capsnap);
3217 return 1;
3218 }
3219 return 0;
3220 }
3221
3222 enum put_cap_refs_mode {
3223 PUT_CAP_REFS_SYNC = 0,
3224 PUT_CAP_REFS_ASYNC,
3225 };
3226
3227 /*
3228 * Release cap refs.
3229 *
3230 * If we released the last ref on any given cap, call ceph_check_caps
3231 * to release (or schedule a release).
3232 *
3233 * If we are releasing a WR cap (from a sync write), finalize any affected
3234 * cap_snap, and wake up any waiters.
3235 */
__ceph_put_cap_refs(struct ceph_inode_info * ci,int had,enum put_cap_refs_mode mode)3236 static void __ceph_put_cap_refs(struct ceph_inode_info *ci, int had,
3237 enum put_cap_refs_mode mode)
3238 {
3239 struct inode *inode = &ci->netfs.inode;
3240 struct ceph_client *cl = ceph_inode_to_client(inode);
3241 int last = 0, put = 0, flushsnaps = 0, wake = 0;
3242 bool check_flushsnaps = false;
3243
3244 spin_lock(&ci->i_ceph_lock);
3245 if (had & CEPH_CAP_PIN)
3246 --ci->i_pin_ref;
3247 if (had & CEPH_CAP_FILE_RD)
3248 if (--ci->i_rd_ref == 0)
3249 last++;
3250 if (had & CEPH_CAP_FILE_CACHE)
3251 if (--ci->i_rdcache_ref == 0)
3252 last++;
3253 if (had & CEPH_CAP_FILE_EXCL)
3254 if (--ci->i_fx_ref == 0)
3255 last++;
3256 if (had & CEPH_CAP_FILE_BUFFER) {
3257 if (--ci->i_wb_ref == 0) {
3258 last++;
3259 /* put the ref held by ceph_take_cap_refs() */
3260 put++;
3261 check_flushsnaps = true;
3262 }
3263 doutc(cl, "%p %llx.%llx wb %d -> %d (?)\n", inode,
3264 ceph_vinop(inode), ci->i_wb_ref+1, ci->i_wb_ref);
3265 }
3266 if (had & CEPH_CAP_FILE_WR) {
3267 if (--ci->i_wr_ref == 0) {
3268 /*
3269 * The Fb caps will always be took and released
3270 * together with the Fw caps.
3271 */
3272 WARN_ON_ONCE(ci->i_wb_ref);
3273
3274 last++;
3275 check_flushsnaps = true;
3276 if (ci->i_wrbuffer_ref_head == 0 &&
3277 ci->i_dirty_caps == 0 &&
3278 ci->i_flushing_caps == 0) {
3279 BUG_ON(!ci->i_head_snapc);
3280 ceph_put_snap_context(ci->i_head_snapc);
3281 ci->i_head_snapc = NULL;
3282 }
3283 /* see comment in __ceph_remove_cap() */
3284 if (!__ceph_is_any_real_caps(ci) && ci->i_snap_realm)
3285 ceph_change_snap_realm(inode, NULL);
3286 }
3287 }
3288 if (check_flushsnaps && __ceph_have_pending_cap_snap(ci)) {
3289 struct ceph_cap_snap *capsnap =
3290 list_last_entry(&ci->i_cap_snaps,
3291 struct ceph_cap_snap,
3292 ci_item);
3293
3294 capsnap->writing = 0;
3295 if (ceph_try_drop_cap_snap(ci, capsnap))
3296 /* put the ref held by ceph_queue_cap_snap() */
3297 put++;
3298 else if (__ceph_finish_cap_snap(ci, capsnap))
3299 flushsnaps = 1;
3300 wake = 1;
3301 }
3302 spin_unlock(&ci->i_ceph_lock);
3303
3304 doutc(cl, "%p %llx.%llx had %s%s%s\n", inode, ceph_vinop(inode),
3305 ceph_cap_string(had), last ? " last" : "", put ? " put" : "");
3306
3307 switch (mode) {
3308 case PUT_CAP_REFS_SYNC:
3309 if (last)
3310 ceph_check_caps(ci, 0);
3311 else if (flushsnaps)
3312 ceph_flush_snaps(ci, NULL);
3313 break;
3314 case PUT_CAP_REFS_ASYNC:
3315 if (last)
3316 ceph_queue_check_caps(inode);
3317 else if (flushsnaps)
3318 ceph_queue_flush_snaps(inode);
3319 break;
3320 default:
3321 break;
3322 }
3323 if (wake)
3324 wake_up_all(&ci->i_cap_wq);
3325 while (put-- > 0)
3326 iput(inode);
3327 }
3328
ceph_put_cap_refs(struct ceph_inode_info * ci,int had)3329 void ceph_put_cap_refs(struct ceph_inode_info *ci, int had)
3330 {
3331 __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_SYNC);
3332 }
3333
ceph_put_cap_refs_async(struct ceph_inode_info * ci,int had)3334 void ceph_put_cap_refs_async(struct ceph_inode_info *ci, int had)
3335 {
3336 __ceph_put_cap_refs(ci, had, PUT_CAP_REFS_ASYNC);
3337 }
3338
3339 /*
3340 * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap
3341 * context. Adjust per-snap dirty page accounting as appropriate.
3342 * Once all dirty data for a cap_snap is flushed, flush snapped file
3343 * metadata back to the MDS. If we dropped the last ref, call
3344 * ceph_check_caps.
3345 */
ceph_put_wrbuffer_cap_refs(struct ceph_inode_info * ci,int nr,struct ceph_snap_context * snapc)3346 void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
3347 struct ceph_snap_context *snapc)
3348 {
3349 struct inode *inode = &ci->netfs.inode;
3350 struct ceph_client *cl = ceph_inode_to_client(inode);
3351 struct ceph_cap_snap *capsnap = NULL, *iter;
3352 int put = 0;
3353 bool last = false;
3354 bool flush_snaps = false;
3355 bool complete_capsnap = false;
3356
3357 spin_lock(&ci->i_ceph_lock);
3358 ci->i_wrbuffer_ref -= nr;
3359 if (ci->i_wrbuffer_ref == 0) {
3360 last = true;
3361 put++;
3362 }
3363
3364 if (ci->i_head_snapc == snapc) {
3365 ci->i_wrbuffer_ref_head -= nr;
3366 if (ci->i_wrbuffer_ref_head == 0 &&
3367 ci->i_wr_ref == 0 &&
3368 ci->i_dirty_caps == 0 &&
3369 ci->i_flushing_caps == 0) {
3370 BUG_ON(!ci->i_head_snapc);
3371 ceph_put_snap_context(ci->i_head_snapc);
3372 ci->i_head_snapc = NULL;
3373 }
3374 doutc(cl, "on %p %llx.%llx head %d/%d -> %d/%d %s\n",
3375 inode, ceph_vinop(inode), ci->i_wrbuffer_ref+nr,
3376 ci->i_wrbuffer_ref_head+nr, ci->i_wrbuffer_ref,
3377 ci->i_wrbuffer_ref_head, last ? " LAST" : "");
3378 } else {
3379 list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) {
3380 if (iter->context == snapc) {
3381 capsnap = iter;
3382 break;
3383 }
3384 }
3385
3386 if (!capsnap) {
3387 /*
3388 * The capsnap should already be removed when removing
3389 * auth cap in the case of a forced unmount.
3390 */
3391 WARN_ON_ONCE(ci->i_auth_cap);
3392 goto unlock;
3393 }
3394
3395 capsnap->dirty_pages -= nr;
3396 if (capsnap->dirty_pages == 0) {
3397 complete_capsnap = true;
3398 if (!capsnap->writing) {
3399 if (ceph_try_drop_cap_snap(ci, capsnap)) {
3400 put++;
3401 } else {
3402 set_bit(CEPH_I_FLUSH_SNAPS_BIT, &ci->i_ceph_flags);
3403 flush_snaps = true;
3404 }
3405 }
3406 }
3407 doutc(cl, "%p %llx.%llx cap_snap %p snap %lld %d/%d -> %d/%d %s%s\n",
3408 inode, ceph_vinop(inode), capsnap, capsnap->context->seq,
3409 ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr,
3410 ci->i_wrbuffer_ref, capsnap->dirty_pages,
3411 last ? " (wrbuffer last)" : "",
3412 complete_capsnap ? " (complete capsnap)" : "");
3413 }
3414
3415 unlock:
3416 spin_unlock(&ci->i_ceph_lock);
3417
3418 if (last) {
3419 ceph_check_caps(ci, 0);
3420 } else if (flush_snaps) {
3421 ceph_flush_snaps(ci, NULL);
3422 }
3423 if (complete_capsnap)
3424 wake_up_all(&ci->i_cap_wq);
3425 while (put-- > 0) {
3426 iput(inode);
3427 }
3428 }
3429
3430 /*
3431 * Invalidate unlinked inode's aliases, so we can drop the inode ASAP.
3432 */
invalidate_aliases(struct inode * inode)3433 static void invalidate_aliases(struct inode *inode)
3434 {
3435 struct ceph_client *cl = ceph_inode_to_client(inode);
3436 struct dentry *dn, *prev = NULL;
3437
3438 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
3439 d_prune_aliases(inode);
3440 /*
3441 * For non-directory inode, d_find_alias() only returns
3442 * hashed dentry. After calling d_invalidate(), the
3443 * dentry becomes unhashed.
3444 *
3445 * For directory inode, d_find_alias() can return
3446 * unhashed dentry. But directory inode should have
3447 * one alias at most.
3448 */
3449 while ((dn = d_find_alias(inode))) {
3450 if (dn == prev) {
3451 dput(dn);
3452 break;
3453 }
3454 d_invalidate(dn);
3455 if (prev)
3456 dput(prev);
3457 prev = dn;
3458 }
3459 if (prev)
3460 dput(prev);
3461 }
3462
3463 struct cap_extra_info {
3464 struct ceph_string *pool_ns;
3465 /* inline data */
3466 u64 inline_version;
3467 void *inline_data;
3468 u32 inline_len;
3469 /* dirstat */
3470 bool dirstat_valid;
3471 u64 nfiles;
3472 u64 nsubdirs;
3473 u64 change_attr;
3474 /* currently issued */
3475 int issued;
3476 struct timespec64 btime;
3477 u8 *fscrypt_auth;
3478 u32 fscrypt_auth_len;
3479 u64 fscrypt_file_size;
3480 };
3481
3482 /*
3483 * Handle a cap GRANT message from the MDS. (Note that a GRANT may
3484 * actually be a revocation if it specifies a smaller cap set.)
3485 *
3486 * caller holds s_mutex and i_ceph_lock, we drop both.
3487 */
handle_cap_grant(struct inode * inode,struct ceph_mds_session * session,struct ceph_cap * cap,struct ceph_mds_caps * grant,struct ceph_buffer * xattr_buf,struct cap_extra_info * extra_info)3488 static void handle_cap_grant(struct inode *inode,
3489 struct ceph_mds_session *session,
3490 struct ceph_cap *cap,
3491 struct ceph_mds_caps *grant,
3492 struct ceph_buffer *xattr_buf,
3493 struct cap_extra_info *extra_info)
3494 __releases(ci->i_ceph_lock)
3495 __releases(session->s_mdsc->snap_rwsem)
3496 {
3497 struct ceph_client *cl = ceph_inode_to_client(inode);
3498 struct ceph_inode_info *ci = ceph_inode(inode);
3499 int seq = le32_to_cpu(grant->seq);
3500 int newcaps = le32_to_cpu(grant->caps);
3501 int used, wanted, dirty;
3502 u64 size = le64_to_cpu(grant->size);
3503 u64 max_size = le64_to_cpu(grant->max_size);
3504 unsigned char check_caps = 0;
3505 bool was_stale = cap->cap_gen < atomic_read(&session->s_cap_gen);
3506 bool wake = false;
3507 bool writeback = false;
3508 bool queue_trunc = false;
3509 bool queue_invalidate = false;
3510 bool deleted_inode = false;
3511 bool fill_inline = false;
3512 bool revoke_wait = false;
3513 int flags = 0;
3514
3515 /*
3516 * If there is at least one crypto block then we'll trust
3517 * fscrypt_file_size. If the real length of the file is 0, then
3518 * ignore it (it has probably been truncated down to 0 by the MDS).
3519 */
3520 if (IS_ENCRYPTED(inode) && size)
3521 size = extra_info->fscrypt_file_size;
3522
3523 doutc(cl, "%p %llx.%llx cap %p mds%d seq %d %s\n", inode,
3524 ceph_vinop(inode), cap, session->s_mds, seq,
3525 ceph_cap_string(newcaps));
3526 doutc(cl, " size %llu max_size %llu, i_size %llu\n", size,
3527 max_size, i_size_read(inode));
3528
3529
3530 /*
3531 * If CACHE is being revoked, and we have no dirty buffers,
3532 * try to invalidate (once). (If there are dirty buffers, we
3533 * will invalidate _after_ writeback.)
3534 */
3535 if (S_ISREG(inode->i_mode) && /* don't invalidate readdir cache */
3536 ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) &&
3537 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 &&
3538 !(ci->i_wrbuffer_ref || ci->i_wb_ref)) {
3539 if (try_nonblocking_invalidate(inode)) {
3540 /* there were locked pages.. invalidate later
3541 in a separate thread. */
3542 if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
3543 queue_invalidate = true;
3544 ci->i_rdcache_revoking = ci->i_rdcache_gen;
3545 }
3546 }
3547 }
3548
3549 if (was_stale)
3550 cap->issued = cap->implemented = CEPH_CAP_PIN;
3551
3552 /*
3553 * auth mds of the inode changed. we received the cap export message,
3554 * but still haven't received the cap import message. handle_cap_export
3555 * updated the new auth MDS' cap.
3556 *
3557 * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message
3558 * that was sent before the cap import message. So don't remove caps.
3559 */
3560 if (ceph_seq_cmp(seq, cap->seq) <= 0) {
3561 WARN_ON(cap != ci->i_auth_cap);
3562 WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id));
3563 seq = cap->seq;
3564 newcaps |= cap->issued;
3565 }
3566
3567 /* side effects now are allowed */
3568 cap->cap_gen = atomic_read(&session->s_cap_gen);
3569 cap->seq = seq;
3570
3571 __check_cap_issue(ci, cap, newcaps);
3572
3573 inode_set_max_iversion_raw(inode, extra_info->change_attr);
3574
3575 if ((newcaps & CEPH_CAP_AUTH_SHARED) &&
3576 (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) {
3577 umode_t mode = le32_to_cpu(grant->mode);
3578
3579 if (inode_wrong_type(inode, mode))
3580 pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
3581 ceph_vinop(inode), inode->i_mode, mode);
3582 else
3583 inode->i_mode = mode;
3584 inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid));
3585 inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid));
3586 ci->i_btime = extra_info->btime;
3587 doutc(cl, "%p %llx.%llx mode 0%o uid.gid %d.%d\n", inode,
3588 ceph_vinop(inode), inode->i_mode,
3589 from_kuid(&init_user_ns, inode->i_uid),
3590 from_kgid(&init_user_ns, inode->i_gid));
3591 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
3592 if (ci->fscrypt_auth_len != extra_info->fscrypt_auth_len ||
3593 memcmp(ci->fscrypt_auth, extra_info->fscrypt_auth,
3594 ci->fscrypt_auth_len))
3595 pr_warn_ratelimited_client(cl,
3596 "cap grant attempt to change fscrypt_auth on non-I_NEW inode (old len %d new len %d)\n",
3597 ci->fscrypt_auth_len,
3598 extra_info->fscrypt_auth_len);
3599 #endif
3600 }
3601
3602 if ((newcaps & CEPH_CAP_LINK_SHARED) &&
3603 (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) {
3604 set_nlink(inode, le32_to_cpu(grant->nlink));
3605 if (inode->i_nlink == 0)
3606 deleted_inode = true;
3607 }
3608
3609 if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 &&
3610 grant->xattr_len) {
3611 int len = le32_to_cpu(grant->xattr_len);
3612 u64 version = le64_to_cpu(grant->xattr_version);
3613
3614 if (version > ci->i_xattrs.version) {
3615 doutc(cl, " got new xattrs v%llu on %p %llx.%llx len %d\n",
3616 version, inode, ceph_vinop(inode), len);
3617 if (ci->i_xattrs.blob)
3618 ceph_buffer_put(ci->i_xattrs.blob);
3619 ci->i_xattrs.blob = ceph_buffer_get(xattr_buf);
3620 ci->i_xattrs.version = version;
3621 ceph_forget_all_cached_acls(inode);
3622 ceph_security_invalidate_secctx(inode);
3623 }
3624 }
3625
3626 if (newcaps & CEPH_CAP_ANY_RD) {
3627 struct timespec64 mtime, atime, ctime;
3628 /* ctime/mtime/atime? */
3629 ceph_decode_timespec64(&mtime, &grant->mtime);
3630 ceph_decode_timespec64(&atime, &grant->atime);
3631 ceph_decode_timespec64(&ctime, &grant->ctime);
3632 ceph_fill_file_time(inode, extra_info->issued,
3633 le32_to_cpu(grant->time_warp_seq),
3634 &ctime, &mtime, &atime);
3635 }
3636
3637 if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) {
3638 ci->i_files = extra_info->nfiles;
3639 ci->i_subdirs = extra_info->nsubdirs;
3640 }
3641
3642 if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) {
3643 /* file layout may have changed */
3644 s64 old_pool = ci->i_layout.pool_id;
3645 struct ceph_string *old_ns;
3646
3647 ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout);
3648 old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
3649 lockdep_is_held(&ci->i_ceph_lock));
3650 rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns);
3651
3652 if (ci->i_layout.pool_id != old_pool ||
3653 extra_info->pool_ns != old_ns)
3654 clear_bit(CEPH_I_POOL_PERM_BIT, &ci->i_ceph_flags);
3655
3656 extra_info->pool_ns = old_ns;
3657
3658 /* size/truncate_seq? */
3659 queue_trunc = ceph_fill_file_size(inode, extra_info->issued,
3660 le32_to_cpu(grant->truncate_seq),
3661 le64_to_cpu(grant->truncate_size),
3662 size);
3663 }
3664
3665 if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) {
3666 if (max_size != ci->i_max_size) {
3667 doutc(cl, "max_size %lld -> %llu\n", ci->i_max_size,
3668 max_size);
3669 ci->i_max_size = max_size;
3670 if (max_size >= ci->i_wanted_max_size) {
3671 ci->i_wanted_max_size = 0; /* reset */
3672 ci->i_requested_max_size = 0;
3673 }
3674 wake = true;
3675 }
3676 }
3677
3678 /* check cap bits */
3679 wanted = __ceph_caps_wanted(ci);
3680 used = __ceph_caps_used(ci);
3681 dirty = __ceph_caps_dirty(ci);
3682 doutc(cl, " my wanted = %s, used = %s, dirty %s\n",
3683 ceph_cap_string(wanted), ceph_cap_string(used),
3684 ceph_cap_string(dirty));
3685
3686 if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) &&
3687 (wanted & ~(cap->mds_wanted | newcaps))) {
3688 /*
3689 * If mds is importing cap, prior cap messages that update
3690 * 'wanted' may get dropped by mds (migrate seq mismatch).
3691 *
3692 * We don't send cap message to update 'wanted' if what we
3693 * want are already issued. If mds revokes caps, cap message
3694 * that releases caps also tells mds what we want. But if
3695 * caps got revoked by mds forcedly (session stale). We may
3696 * haven't told mds what we want.
3697 */
3698 check_caps = 1;
3699 }
3700
3701 /* revocation, grant, or no-op? */
3702 if (cap->issued & ~newcaps) {
3703 int revoking = cap->issued & ~newcaps;
3704
3705 doutc(cl, "revocation: %s -> %s (revoking %s)\n",
3706 ceph_cap_string(cap->issued), ceph_cap_string(newcaps),
3707 ceph_cap_string(revoking));
3708 if (S_ISREG(inode->i_mode) &&
3709 (revoking & used & CEPH_CAP_FILE_BUFFER)) {
3710 writeback = true; /* initiate writeback; will delay ack */
3711 revoke_wait = true;
3712 } else if (queue_invalidate &&
3713 revoking == CEPH_CAP_FILE_CACHE &&
3714 (newcaps & CEPH_CAP_FILE_LAZYIO) == 0) {
3715 revoke_wait = true; /* do nothing yet, invalidation will be queued */
3716 } else if (cap == ci->i_auth_cap) {
3717 check_caps = 1; /* check auth cap only */
3718 } else {
3719 check_caps = 2; /* check all caps */
3720 }
3721 /* If there is new caps, try to wake up the waiters */
3722 if (~cap->issued & newcaps)
3723 wake = true;
3724 cap->issued = newcaps;
3725 cap->implemented |= newcaps;
3726 } else if (cap->issued == newcaps) {
3727 doutc(cl, "caps unchanged: %s -> %s\n",
3728 ceph_cap_string(cap->issued),
3729 ceph_cap_string(newcaps));
3730 } else {
3731 doutc(cl, "grant: %s -> %s\n", ceph_cap_string(cap->issued),
3732 ceph_cap_string(newcaps));
3733 /* non-auth MDS is revoking the newly grant caps ? */
3734 if (cap == ci->i_auth_cap &&
3735 __ceph_caps_revoking_other(ci, cap, newcaps))
3736 check_caps = 2;
3737
3738 cap->issued = newcaps;
3739 cap->implemented |= newcaps; /* add bits only, to
3740 * avoid stepping on a
3741 * pending revocation */
3742 wake = true;
3743 }
3744 BUG_ON(cap->issued & ~cap->implemented);
3745
3746 /* don't let check_caps skip sending a response to MDS for revoke msgs */
3747 if (!revoke_wait && le32_to_cpu(grant->op) == CEPH_CAP_OP_REVOKE) {
3748 cap->mds_wanted = 0;
3749 flags |= CHECK_CAPS_FLUSH_FORCE;
3750 if (cap == ci->i_auth_cap)
3751 check_caps = 1; /* check auth cap only */
3752 else
3753 check_caps = 2; /* check all caps */
3754 }
3755
3756 if (extra_info->inline_version > 0 &&
3757 extra_info->inline_version >= ci->i_inline_version) {
3758 ci->i_inline_version = extra_info->inline_version;
3759 if (ci->i_inline_version != CEPH_INLINE_NONE &&
3760 (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)))
3761 fill_inline = true;
3762 }
3763
3764 if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) {
3765 if (ci->i_auth_cap == cap) {
3766 if (newcaps & ~extra_info->issued)
3767 wake = true;
3768
3769 if (ci->i_requested_max_size > max_size ||
3770 !(le32_to_cpu(grant->wanted) & CEPH_CAP_ANY_FILE_WR)) {
3771 /* re-request max_size if necessary */
3772 ci->i_requested_max_size = 0;
3773 wake = true;
3774 }
3775
3776 ceph_kick_flushing_inode_caps(session, ci);
3777 }
3778 up_read(&session->s_mdsc->snap_rwsem);
3779 }
3780 spin_unlock(&ci->i_ceph_lock);
3781
3782 if (fill_inline)
3783 ceph_fill_inline_data(inode, NULL, extra_info->inline_data,
3784 extra_info->inline_len);
3785
3786 if (queue_trunc)
3787 ceph_queue_vmtruncate(inode);
3788
3789 if (writeback)
3790 /*
3791 * queue inode for writeback: we can't actually call
3792 * filemap_write_and_wait, etc. from message handler
3793 * context.
3794 */
3795 ceph_queue_writeback(inode);
3796 if (queue_invalidate)
3797 ceph_queue_invalidate(inode);
3798 if (deleted_inode)
3799 invalidate_aliases(inode);
3800 if (wake)
3801 wake_up_all(&ci->i_cap_wq);
3802
3803 mutex_unlock(&session->s_mutex);
3804 if (check_caps == 1)
3805 ceph_check_caps(ci, flags | CHECK_CAPS_AUTHONLY | CHECK_CAPS_NOINVAL);
3806 else if (check_caps == 2)
3807 ceph_check_caps(ci, flags | CHECK_CAPS_NOINVAL);
3808 }
3809
3810 /*
3811 * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the
3812 * MDS has been safely committed.
3813 */
handle_cap_flush_ack(struct inode * inode,u64 flush_tid,struct ceph_mds_caps * m,struct ceph_mds_session * session,struct ceph_cap * cap)3814 static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid,
3815 struct ceph_mds_caps *m,
3816 struct ceph_mds_session *session,
3817 struct ceph_cap *cap)
3818 __releases(ci->i_ceph_lock)
3819 {
3820 struct ceph_inode_info *ci = ceph_inode(inode);
3821 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
3822 struct ceph_client *cl = mdsc->fsc->client;
3823 struct ceph_cap_flush *cf, *tmp_cf;
3824 LIST_HEAD(to_remove);
3825 unsigned seq = le32_to_cpu(m->seq);
3826 int dirty = le32_to_cpu(m->dirty);
3827 int cleaned = 0;
3828 bool drop = false;
3829 bool wake_ci = false;
3830 bool wake_mdsc = false;
3831
3832 /*
3833 * Flush tids are monotonically increasing and acks arrive in
3834 * order under i_ceph_lock, so this is always the latest tid.
3835 * Diagnostic readers use READ_ONCE() without holding the lock.
3836 */
3837 WRITE_ONCE(ci->i_last_cap_flush_ack, flush_tid);
3838
3839 list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) {
3840 /* Is this the one that was flushed? */
3841 if (cf->tid == flush_tid)
3842 cleaned = cf->caps;
3843
3844 /* Is this a capsnap? */
3845 if (cf->is_capsnap)
3846 continue;
3847
3848 if (cf->tid <= flush_tid) {
3849 /*
3850 * An earlier or current tid. The FLUSH_ACK should
3851 * represent a superset of this flush's caps.
3852 */
3853 wake_ci |= __detach_cap_flush_from_ci(ci, cf);
3854 list_add_tail(&cf->i_list, &to_remove);
3855 } else {
3856 /*
3857 * This is a later one. Any caps in it are still dirty
3858 * so don't count them as cleaned.
3859 */
3860 cleaned &= ~cf->caps;
3861 if (!cleaned)
3862 break;
3863 }
3864 }
3865
3866 doutc(cl, "%p %llx.%llx mds%d seq %d on %s cleaned %s, flushing %s -> %s\n",
3867 inode, ceph_vinop(inode), session->s_mds, seq,
3868 ceph_cap_string(dirty), ceph_cap_string(cleaned),
3869 ceph_cap_string(ci->i_flushing_caps),
3870 ceph_cap_string(ci->i_flushing_caps & ~cleaned));
3871
3872 if (list_empty(&to_remove) && !cleaned)
3873 goto out;
3874
3875 ci->i_flushing_caps &= ~cleaned;
3876
3877 spin_lock(&mdsc->cap_dirty_lock);
3878
3879 list_for_each_entry(cf, &to_remove, i_list)
3880 wake_mdsc |= __detach_cap_flush_from_mdsc(mdsc, cf);
3881
3882 if (ci->i_flushing_caps == 0) {
3883 if (list_empty(&ci->i_cap_flush_list)) {
3884 list_del_init(&ci->i_flushing_item);
3885 if (!list_empty(&session->s_cap_flushing)) {
3886 struct inode *inode =
3887 &list_first_entry(&session->s_cap_flushing,
3888 struct ceph_inode_info,
3889 i_flushing_item)->netfs.inode;
3890 doutc(cl, " mds%d still flushing cap on %p %llx.%llx\n",
3891 session->s_mds, inode, ceph_vinop(inode));
3892 }
3893 }
3894 mdsc->num_cap_flushing--;
3895 doutc(cl, " %p %llx.%llx now !flushing\n", inode,
3896 ceph_vinop(inode));
3897
3898 if (ci->i_dirty_caps == 0) {
3899 doutc(cl, " %p %llx.%llx now clean\n", inode,
3900 ceph_vinop(inode));
3901 BUG_ON(!list_empty(&ci->i_dirty_item));
3902 drop = true;
3903 if (ci->i_wr_ref == 0 &&
3904 ci->i_wrbuffer_ref_head == 0) {
3905 BUG_ON(!ci->i_head_snapc);
3906 ceph_put_snap_context(ci->i_head_snapc);
3907 ci->i_head_snapc = NULL;
3908 }
3909 } else {
3910 BUG_ON(list_empty(&ci->i_dirty_item));
3911 }
3912 }
3913 spin_unlock(&mdsc->cap_dirty_lock);
3914
3915 out:
3916 spin_unlock(&ci->i_ceph_lock);
3917
3918 while (!list_empty(&to_remove)) {
3919 cf = list_first_entry(&to_remove,
3920 struct ceph_cap_flush, i_list);
3921 list_del_init(&cf->i_list);
3922 if (!cf->is_capsnap)
3923 ceph_free_cap_flush(cf);
3924 }
3925
3926 if (wake_ci)
3927 wake_up_all(&ci->i_cap_wq);
3928 if (wake_mdsc)
3929 wake_up_all(&mdsc->cap_flushing_wq);
3930 if (drop)
3931 iput(inode);
3932 }
3933
__ceph_remove_capsnap(struct inode * inode,struct ceph_cap_snap * capsnap,bool * wake_ci,bool * wake_mdsc)3934 void __ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
3935 bool *wake_ci, bool *wake_mdsc)
3936 {
3937 struct ceph_inode_info *ci = ceph_inode(inode);
3938 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
3939 struct ceph_client *cl = mdsc->fsc->client;
3940 bool ret;
3941
3942 lockdep_assert_held(&ci->i_ceph_lock);
3943
3944 doutc(cl, "removing capsnap %p, %p %llx.%llx ci %p\n", capsnap,
3945 inode, ceph_vinop(inode), ci);
3946
3947 list_del_init(&capsnap->ci_item);
3948 ret = __detach_cap_flush_from_ci(ci, &capsnap->cap_flush);
3949 if (wake_ci)
3950 *wake_ci = ret;
3951
3952 spin_lock(&mdsc->cap_dirty_lock);
3953 if (list_empty(&ci->i_cap_flush_list))
3954 list_del_init(&ci->i_flushing_item);
3955
3956 ret = __detach_cap_flush_from_mdsc(mdsc, &capsnap->cap_flush);
3957 if (wake_mdsc)
3958 *wake_mdsc = ret;
3959 spin_unlock(&mdsc->cap_dirty_lock);
3960 }
3961
ceph_remove_capsnap(struct inode * inode,struct ceph_cap_snap * capsnap,bool * wake_ci,bool * wake_mdsc)3962 void ceph_remove_capsnap(struct inode *inode, struct ceph_cap_snap *capsnap,
3963 bool *wake_ci, bool *wake_mdsc)
3964 {
3965 struct ceph_inode_info *ci = ceph_inode(inode);
3966
3967 lockdep_assert_held(&ci->i_ceph_lock);
3968
3969 WARN_ON_ONCE(capsnap->dirty_pages || capsnap->writing);
3970 __ceph_remove_capsnap(inode, capsnap, wake_ci, wake_mdsc);
3971 }
3972
3973 /*
3974 * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can
3975 * throw away our cap_snap.
3976 *
3977 * Caller hold s_mutex.
3978 */
handle_cap_flushsnap_ack(struct inode * inode,u64 flush_tid,struct ceph_mds_caps * m,struct ceph_mds_session * session)3979 static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid,
3980 struct ceph_mds_caps *m,
3981 struct ceph_mds_session *session)
3982 {
3983 struct ceph_inode_info *ci = ceph_inode(inode);
3984 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
3985 struct ceph_client *cl = mdsc->fsc->client;
3986 u64 follows = le64_to_cpu(m->snap_follows);
3987 struct ceph_cap_snap *capsnap = NULL, *iter;
3988 bool wake_ci = false;
3989 bool wake_mdsc = false;
3990
3991 doutc(cl, "%p %llx.%llx ci %p mds%d follows %lld\n", inode,
3992 ceph_vinop(inode), ci, session->s_mds, follows);
3993
3994 spin_lock(&ci->i_ceph_lock);
3995 list_for_each_entry(iter, &ci->i_cap_snaps, ci_item) {
3996 if (iter->follows == follows) {
3997 if (iter->cap_flush.tid != flush_tid) {
3998 doutc(cl, " cap_snap %p follows %lld "
3999 "tid %lld != %lld\n", iter,
4000 follows, flush_tid,
4001 iter->cap_flush.tid);
4002 break;
4003 }
4004 capsnap = iter;
4005 break;
4006 } else {
4007 doutc(cl, " skipping cap_snap %p follows %lld\n",
4008 iter, iter->follows);
4009 }
4010 }
4011 if (capsnap)
4012 ceph_remove_capsnap(inode, capsnap, &wake_ci, &wake_mdsc);
4013 spin_unlock(&ci->i_ceph_lock);
4014
4015 if (capsnap) {
4016 ceph_put_snap_context(capsnap->context);
4017 ceph_put_cap_snap(capsnap);
4018 if (wake_ci)
4019 wake_up_all(&ci->i_cap_wq);
4020 if (wake_mdsc)
4021 wake_up_all(&mdsc->cap_flushing_wq);
4022 iput(inode);
4023 }
4024 }
4025
4026 /*
4027 * Handle TRUNC from MDS, indicating file truncation.
4028 *
4029 * caller hold s_mutex.
4030 */
handle_cap_trunc(struct inode * inode,struct ceph_mds_caps * trunc,struct ceph_mds_session * session,struct cap_extra_info * extra_info)4031 static bool handle_cap_trunc(struct inode *inode,
4032 struct ceph_mds_caps *trunc,
4033 struct ceph_mds_session *session,
4034 struct cap_extra_info *extra_info)
4035 {
4036 struct ceph_inode_info *ci = ceph_inode(inode);
4037 struct ceph_client *cl = ceph_inode_to_client(inode);
4038 int mds = session->s_mds;
4039 int seq = le32_to_cpu(trunc->seq);
4040 u32 truncate_seq = le32_to_cpu(trunc->truncate_seq);
4041 u64 truncate_size = le64_to_cpu(trunc->truncate_size);
4042 u64 size = le64_to_cpu(trunc->size);
4043 int implemented = 0;
4044 int dirty = __ceph_caps_dirty(ci);
4045 int issued = __ceph_caps_issued(ceph_inode(inode), &implemented);
4046 bool queue_trunc = false;
4047
4048 lockdep_assert_held(&ci->i_ceph_lock);
4049
4050 issued |= implemented | dirty;
4051
4052 /*
4053 * If there is at least one crypto block then we'll trust
4054 * fscrypt_file_size. If the real length of the file is 0, then
4055 * ignore it (it has probably been truncated down to 0 by the MDS).
4056 */
4057 if (IS_ENCRYPTED(inode) && size)
4058 size = extra_info->fscrypt_file_size;
4059
4060 doutc(cl, "%p %llx.%llx mds%d seq %d to %lld truncate seq %d\n",
4061 inode, ceph_vinop(inode), mds, seq, truncate_size, truncate_seq);
4062 queue_trunc = ceph_fill_file_size(inode, issued,
4063 truncate_seq, truncate_size, size);
4064 return queue_trunc;
4065 }
4066
4067 /*
4068 * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a
4069 * different one. If we are the most recent migration we've seen (as
4070 * indicated by mseq), make note of the migrating cap bits for the
4071 * duration (until we see the corresponding IMPORT).
4072 *
4073 * caller holds s_mutex
4074 */
handle_cap_export(struct inode * inode,struct ceph_mds_caps * ex,struct ceph_mds_cap_peer * ph,struct ceph_mds_session * session)4075 static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex,
4076 struct ceph_mds_cap_peer *ph,
4077 struct ceph_mds_session *session)
4078 {
4079 struct ceph_mds_client *mdsc = ceph_inode_to_fs_client(inode)->mdsc;
4080 struct ceph_client *cl = mdsc->fsc->client;
4081 struct ceph_mds_session *tsession = NULL;
4082 struct ceph_cap *cap, *tcap, *new_cap = NULL;
4083 struct ceph_inode_info *ci = ceph_inode(inode);
4084 u64 t_cap_id;
4085 u32 t_issue_seq, t_mseq;
4086 int target, issued;
4087 int mds = session->s_mds;
4088
4089 if (ph) {
4090 t_cap_id = le64_to_cpu(ph->cap_id);
4091 t_issue_seq = le32_to_cpu(ph->issue_seq);
4092 t_mseq = le32_to_cpu(ph->mseq);
4093 target = le32_to_cpu(ph->mds);
4094 } else {
4095 t_cap_id = t_issue_seq = t_mseq = 0;
4096 target = -1;
4097 }
4098
4099 doutc(cl, " cap %llx.%llx export to peer %d piseq %u pmseq %u\n",
4100 ceph_vinop(inode), target, t_issue_seq, t_mseq);
4101 retry:
4102 down_read(&mdsc->snap_rwsem);
4103 spin_lock(&ci->i_ceph_lock);
4104 cap = __get_cap_for_mds(ci, mds);
4105 if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id))
4106 goto out_unlock;
4107
4108 if (target < 0) {
4109 ceph_remove_cap(mdsc, cap, false);
4110 goto out_unlock;
4111 }
4112
4113 /*
4114 * now we know we haven't received the cap import message yet
4115 * because the exported cap still exist.
4116 */
4117
4118 issued = cap->issued;
4119 if (issued != cap->implemented)
4120 pr_err_ratelimited_client(cl, "issued != implemented: "
4121 "%p %llx.%llx mds%d seq %d mseq %d"
4122 " issued %s implemented %s\n",
4123 inode, ceph_vinop(inode), mds,
4124 cap->seq, cap->mseq,
4125 ceph_cap_string(issued),
4126 ceph_cap_string(cap->implemented));
4127
4128
4129 tcap = __get_cap_for_mds(ci, target);
4130 if (tcap) {
4131 /* already have caps from the target */
4132 if (tcap->cap_id == t_cap_id &&
4133 ceph_seq_cmp(tcap->seq, t_issue_seq) < 0) {
4134 doutc(cl, " updating import cap %p mds%d\n", tcap,
4135 target);
4136 tcap->cap_id = t_cap_id;
4137 tcap->seq = t_issue_seq - 1;
4138 tcap->issue_seq = t_issue_seq - 1;
4139 tcap->issued |= issued;
4140 tcap->implemented |= issued;
4141 if (cap == ci->i_auth_cap) {
4142 ci->i_auth_cap = tcap;
4143 change_auth_cap_ses(ci, tcap->session);
4144 }
4145 }
4146 ceph_remove_cap(mdsc, cap, false);
4147 goto out_unlock;
4148 } else if (tsession) {
4149 /* add placeholder for the export target */
4150 int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0;
4151 tcap = new_cap;
4152 ceph_add_cap(inode, tsession, t_cap_id, issued, 0,
4153 t_issue_seq - 1, t_mseq, (u64)-1, flag, &new_cap);
4154
4155 if (!list_empty(&ci->i_cap_flush_list) &&
4156 ci->i_auth_cap == tcap) {
4157 spin_lock(&mdsc->cap_dirty_lock);
4158 list_move_tail(&ci->i_flushing_item,
4159 &tcap->session->s_cap_flushing);
4160 spin_unlock(&mdsc->cap_dirty_lock);
4161 }
4162
4163 ceph_remove_cap(mdsc, cap, false);
4164 goto out_unlock;
4165 }
4166
4167 spin_unlock(&ci->i_ceph_lock);
4168 up_read(&mdsc->snap_rwsem);
4169 mutex_unlock(&session->s_mutex);
4170
4171 /* open target session */
4172 tsession = ceph_mdsc_open_export_target_session(mdsc, target);
4173 if (!IS_ERR(tsession)) {
4174 if (mds > target) {
4175 mutex_lock(&session->s_mutex);
4176 mutex_lock_nested(&tsession->s_mutex,
4177 SINGLE_DEPTH_NESTING);
4178 } else {
4179 mutex_lock(&tsession->s_mutex);
4180 mutex_lock_nested(&session->s_mutex,
4181 SINGLE_DEPTH_NESTING);
4182 }
4183 new_cap = ceph_get_cap(mdsc, NULL);
4184 } else {
4185 WARN_ON(1);
4186 tsession = NULL;
4187 target = -1;
4188 mutex_lock(&session->s_mutex);
4189 }
4190 goto retry;
4191
4192 out_unlock:
4193 spin_unlock(&ci->i_ceph_lock);
4194 up_read(&mdsc->snap_rwsem);
4195 mutex_unlock(&session->s_mutex);
4196 if (tsession) {
4197 mutex_unlock(&tsession->s_mutex);
4198 ceph_put_mds_session(tsession);
4199 }
4200 if (new_cap)
4201 ceph_put_cap(mdsc, new_cap);
4202 }
4203
4204 /*
4205 * Handle cap IMPORT.
4206 *
4207 * caller holds s_mutex. acquires i_ceph_lock
4208 */
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)4209 static void handle_cap_import(struct ceph_mds_client *mdsc,
4210 struct inode *inode, struct ceph_mds_caps *im,
4211 struct ceph_mds_cap_peer *ph,
4212 struct ceph_mds_session *session,
4213 struct ceph_cap **target_cap, int *old_issued)
4214 {
4215 struct ceph_inode_info *ci = ceph_inode(inode);
4216 struct ceph_client *cl = mdsc->fsc->client;
4217 struct ceph_cap *cap, *ocap, *new_cap = NULL;
4218 int mds = session->s_mds;
4219 int issued;
4220 unsigned caps = le32_to_cpu(im->caps);
4221 unsigned wanted = le32_to_cpu(im->wanted);
4222 unsigned seq = le32_to_cpu(im->seq);
4223 unsigned mseq = le32_to_cpu(im->migrate_seq);
4224 u64 realmino = le64_to_cpu(im->realm);
4225 u64 cap_id = le64_to_cpu(im->cap_id);
4226 u64 p_cap_id;
4227 u32 piseq = 0;
4228 u32 pmseq = 0;
4229 int peer;
4230
4231 if (ph) {
4232 p_cap_id = le64_to_cpu(ph->cap_id);
4233 peer = le32_to_cpu(ph->mds);
4234 piseq = le32_to_cpu(ph->issue_seq);
4235 pmseq = le32_to_cpu(ph->mseq);
4236 } else {
4237 p_cap_id = 0;
4238 peer = -1;
4239 }
4240
4241 doutc(cl, " cap %llx.%llx import from peer %d piseq %u pmseq %u\n",
4242 ceph_vinop(inode), peer, piseq, pmseq);
4243 retry:
4244 cap = __get_cap_for_mds(ci, mds);
4245 if (!cap) {
4246 if (!new_cap) {
4247 spin_unlock(&ci->i_ceph_lock);
4248 new_cap = ceph_get_cap(mdsc, NULL);
4249 spin_lock(&ci->i_ceph_lock);
4250 goto retry;
4251 }
4252 cap = new_cap;
4253 } else {
4254 if (new_cap) {
4255 ceph_put_cap(mdsc, new_cap);
4256 new_cap = NULL;
4257 }
4258 }
4259
4260 __ceph_caps_issued(ci, &issued);
4261 issued |= __ceph_caps_dirty(ci);
4262
4263 ceph_add_cap(inode, session, cap_id, caps, wanted, seq, mseq,
4264 realmino, CEPH_CAP_FLAG_AUTH, &new_cap);
4265
4266 ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL;
4267 if (ocap && ocap->cap_id == p_cap_id) {
4268 doutc(cl, " remove export cap %p mds%d flags %d\n",
4269 ocap, peer, ph->flags);
4270 if ((ph->flags & CEPH_CAP_FLAG_AUTH) &&
4271 (ocap->seq != piseq ||
4272 ocap->mseq != pmseq)) {
4273 pr_err_ratelimited_client(cl, "mismatched seq/mseq: "
4274 "%p %llx.%llx mds%d seq %d mseq %d"
4275 " importer mds%d has peer seq %d mseq %d\n",
4276 inode, ceph_vinop(inode), peer,
4277 ocap->seq, ocap->mseq, mds, piseq, pmseq);
4278 }
4279 ceph_remove_cap(mdsc, ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE));
4280 }
4281
4282 *old_issued = issued;
4283 *target_cap = cap;
4284 }
4285
4286 #ifdef CONFIG_FS_ENCRYPTION
parse_fscrypt_fields(void ** p,void * end,struct cap_extra_info * extra)4287 static int parse_fscrypt_fields(void **p, void *end,
4288 struct cap_extra_info *extra)
4289 {
4290 u32 len;
4291
4292 ceph_decode_32_safe(p, end, extra->fscrypt_auth_len, bad);
4293 if (extra->fscrypt_auth_len) {
4294 ceph_decode_need(p, end, extra->fscrypt_auth_len, bad);
4295 extra->fscrypt_auth = kmalloc(extra->fscrypt_auth_len,
4296 GFP_KERNEL);
4297 if (!extra->fscrypt_auth)
4298 return -ENOMEM;
4299 ceph_decode_copy_safe(p, end, extra->fscrypt_auth,
4300 extra->fscrypt_auth_len, bad);
4301 }
4302
4303 ceph_decode_32_safe(p, end, len, bad);
4304 if (len >= sizeof(u64)) {
4305 ceph_decode_64_safe(p, end, extra->fscrypt_file_size, bad);
4306 len -= sizeof(u64);
4307 }
4308 ceph_decode_skip_n(p, end, len, bad);
4309 return 0;
4310 bad:
4311 return -EIO;
4312 }
4313 #else
parse_fscrypt_fields(void ** p,void * end,struct cap_extra_info * extra)4314 static int parse_fscrypt_fields(void **p, void *end,
4315 struct cap_extra_info *extra)
4316 {
4317 u32 len;
4318
4319 /* Don't care about these fields unless we're encryption-capable */
4320 ceph_decode_32_safe(p, end, len, bad);
4321 if (len)
4322 ceph_decode_skip_n(p, end, len, bad);
4323 ceph_decode_32_safe(p, end, len, bad);
4324 if (len)
4325 ceph_decode_skip_n(p, end, len, bad);
4326 return 0;
4327 bad:
4328 return -EIO;
4329 }
4330 #endif
4331
4332 /*
4333 * Handle a caps message from the MDS.
4334 *
4335 * Identify the appropriate session, inode, and call the right handler
4336 * based on the cap op.
4337 */
ceph_handle_caps(struct ceph_mds_session * session,struct ceph_msg * msg)4338 void ceph_handle_caps(struct ceph_mds_session *session,
4339 struct ceph_msg *msg)
4340 {
4341 struct ceph_mds_client *mdsc = session->s_mdsc;
4342 struct ceph_client *cl = mdsc->fsc->client;
4343 struct inode *inode;
4344 struct ceph_inode_info *ci;
4345 struct ceph_cap *cap;
4346 struct ceph_mds_caps *h;
4347 struct ceph_mds_cap_peer *peer = NULL;
4348 struct ceph_snap_realm *realm = NULL;
4349 int op;
4350 int msg_version = le16_to_cpu(msg->hdr.version);
4351 u32 seq, mseq, issue_seq;
4352 struct ceph_vino vino;
4353 void *snaptrace;
4354 size_t snaptrace_len;
4355 void *p, *end;
4356 struct cap_extra_info extra_info = {};
4357 bool queue_trunc;
4358 bool close_sessions = false;
4359 bool do_cap_release = false;
4360
4361 if (!ceph_inc_mds_stopping_blocker(mdsc, session))
4362 return;
4363
4364 /* decode */
4365 end = msg->front.iov_base + msg->front.iov_len;
4366 if (msg->front.iov_len < sizeof(*h))
4367 goto bad;
4368 h = msg->front.iov_base;
4369 op = le32_to_cpu(h->op);
4370 vino.ino = le64_to_cpu(h->ino);
4371 vino.snap = CEPH_NOSNAP;
4372 seq = le32_to_cpu(h->seq);
4373 mseq = le32_to_cpu(h->migrate_seq);
4374 issue_seq = le32_to_cpu(h->issue_seq);
4375
4376 snaptrace = h + 1;
4377 snaptrace_len = le32_to_cpu(h->snap_trace_len);
4378 p = snaptrace + snaptrace_len;
4379
4380 if (msg_version >= 2) {
4381 u32 flock_len;
4382 ceph_decode_32_safe(&p, end, flock_len, bad);
4383 if (p + flock_len > end)
4384 goto bad;
4385 p += flock_len;
4386 }
4387
4388 if (msg_version >= 3) {
4389 if (op == CEPH_CAP_OP_IMPORT) {
4390 if (p + sizeof(*peer) > end)
4391 goto bad;
4392 peer = p;
4393 p += sizeof(*peer);
4394 } else if (op == CEPH_CAP_OP_EXPORT) {
4395 /* recorded in unused fields */
4396 peer = (void *)&h->size;
4397 }
4398 }
4399
4400 if (msg_version >= 4) {
4401 ceph_decode_64_safe(&p, end, extra_info.inline_version, bad);
4402 ceph_decode_32_safe(&p, end, extra_info.inline_len, bad);
4403 if (p + extra_info.inline_len > end)
4404 goto bad;
4405 extra_info.inline_data = p;
4406 p += extra_info.inline_len;
4407 }
4408
4409 if (msg_version >= 5) {
4410 struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
4411 u32 epoch_barrier;
4412
4413 ceph_decode_32_safe(&p, end, epoch_barrier, bad);
4414 ceph_osdc_update_epoch_barrier(osdc, epoch_barrier);
4415 }
4416
4417 if (msg_version >= 8) {
4418 u32 pool_ns_len;
4419
4420 /* version >= 6 */
4421 ceph_decode_skip_64(&p, end, bad); // flush_tid
4422 /* version >= 7 */
4423 ceph_decode_skip_32(&p, end, bad); // caller_uid
4424 ceph_decode_skip_32(&p, end, bad); // caller_gid
4425 /* version >= 8 */
4426 ceph_decode_32_safe(&p, end, pool_ns_len, bad);
4427 if (pool_ns_len > 0) {
4428 ceph_decode_need(&p, end, pool_ns_len, bad);
4429 extra_info.pool_ns =
4430 ceph_find_or_create_string(p, pool_ns_len);
4431 p += pool_ns_len;
4432 }
4433 }
4434
4435 if (msg_version >= 9) {
4436 struct ceph_timespec *btime;
4437
4438 if (p + sizeof(*btime) > end)
4439 goto bad;
4440 btime = p;
4441 ceph_decode_timespec64(&extra_info.btime, btime);
4442 p += sizeof(*btime);
4443 ceph_decode_64_safe(&p, end, extra_info.change_attr, bad);
4444 }
4445
4446 if (msg_version >= 11) {
4447 /* version >= 10 */
4448 ceph_decode_skip_32(&p, end, bad); // flags
4449 /* version >= 11 */
4450 extra_info.dirstat_valid = true;
4451 ceph_decode_64_safe(&p, end, extra_info.nfiles, bad);
4452 ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad);
4453 }
4454
4455 if (msg_version >= 12) {
4456 if (parse_fscrypt_fields(&p, end, &extra_info))
4457 goto bad;
4458 }
4459
4460 /* lookup ino */
4461 inode = ceph_find_inode(mdsc->fsc->sb, vino);
4462 doutc(cl, " caps mds%d op %s ino %llx.%llx inode %p seq %u iseq %u mseq %u\n",
4463 session->s_mds, ceph_cap_op_name(op), vino.ino, vino.snap, inode,
4464 seq, issue_seq, mseq);
4465
4466 trace_ceph_handle_caps(mdsc, session, op, &vino, ceph_inode(inode),
4467 seq, issue_seq, mseq);
4468
4469 mutex_lock(&session->s_mutex);
4470
4471 if (!inode) {
4472 doutc(cl, " i don't have ino %llx\n", vino.ino);
4473
4474 switch (op) {
4475 case CEPH_CAP_OP_IMPORT:
4476 case CEPH_CAP_OP_REVOKE:
4477 case CEPH_CAP_OP_GRANT:
4478 do_cap_release = true;
4479 break;
4480 default:
4481 break;
4482 }
4483 goto flush_cap_releases;
4484 }
4485 ci = ceph_inode(inode);
4486
4487 /* these will work even if we don't have a cap yet */
4488 switch (op) {
4489 case CEPH_CAP_OP_FLUSHSNAP_ACK:
4490 handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid),
4491 h, session);
4492 goto done;
4493
4494 case CEPH_CAP_OP_EXPORT:
4495 handle_cap_export(inode, h, peer, session);
4496 goto done_unlocked;
4497
4498 case CEPH_CAP_OP_IMPORT:
4499 realm = NULL;
4500 if (snaptrace_len) {
4501 down_write(&mdsc->snap_rwsem);
4502 if (ceph_update_snap_trace(mdsc, snaptrace,
4503 snaptrace + snaptrace_len,
4504 false, &realm)) {
4505 up_write(&mdsc->snap_rwsem);
4506 close_sessions = true;
4507 goto done;
4508 }
4509 downgrade_write(&mdsc->snap_rwsem);
4510 } else {
4511 down_read(&mdsc->snap_rwsem);
4512 }
4513 spin_lock(&ci->i_ceph_lock);
4514 handle_cap_import(mdsc, inode, h, peer, session,
4515 &cap, &extra_info.issued);
4516 handle_cap_grant(inode, session, cap,
4517 h, msg->middle, &extra_info);
4518 if (realm)
4519 ceph_put_snap_realm(mdsc, realm);
4520 goto done_unlocked;
4521 }
4522
4523 /* the rest require a cap */
4524 spin_lock(&ci->i_ceph_lock);
4525 cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds);
4526 if (!cap) {
4527 doutc(cl, " no cap on %p ino %llx.%llx from mds%d\n",
4528 inode, ceph_ino(inode), ceph_snap(inode),
4529 session->s_mds);
4530 spin_unlock(&ci->i_ceph_lock);
4531 switch (op) {
4532 case CEPH_CAP_OP_REVOKE:
4533 case CEPH_CAP_OP_GRANT:
4534 do_cap_release = true;
4535 break;
4536 default:
4537 break;
4538 }
4539 goto flush_cap_releases;
4540 }
4541
4542 /* note that each of these drops i_ceph_lock for us */
4543 switch (op) {
4544 case CEPH_CAP_OP_REVOKE:
4545 case CEPH_CAP_OP_GRANT:
4546 __ceph_caps_issued(ci, &extra_info.issued);
4547 extra_info.issued |= __ceph_caps_dirty(ci);
4548 handle_cap_grant(inode, session, cap,
4549 h, msg->middle, &extra_info);
4550 goto done_unlocked;
4551
4552 case CEPH_CAP_OP_FLUSH_ACK:
4553 handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid),
4554 h, session, cap);
4555 break;
4556
4557 case CEPH_CAP_OP_TRUNC:
4558 queue_trunc = handle_cap_trunc(inode, h, session,
4559 &extra_info);
4560 spin_unlock(&ci->i_ceph_lock);
4561 if (queue_trunc)
4562 ceph_queue_vmtruncate(inode);
4563 break;
4564
4565 default:
4566 spin_unlock(&ci->i_ceph_lock);
4567 pr_err_client(cl, "unknown cap op %d %s\n", op,
4568 ceph_cap_op_name(op));
4569 }
4570
4571 done:
4572 mutex_unlock(&session->s_mutex);
4573 done_unlocked:
4574 iput(inode);
4575 out:
4576 ceph_dec_mds_stopping_blocker(mdsc);
4577
4578 ceph_put_string(extra_info.pool_ns);
4579
4580 /* Defer closing the sessions after s_mutex lock being released */
4581 if (close_sessions)
4582 ceph_mdsc_close_sessions(mdsc);
4583
4584 kfree(extra_info.fscrypt_auth);
4585 return;
4586
4587 flush_cap_releases:
4588 /*
4589 * send any cap release message to try to move things
4590 * along for the mds (who clearly thinks we still have this
4591 * cap).
4592 */
4593 if (do_cap_release) {
4594 cap = ceph_get_cap(mdsc, NULL);
4595 cap->cap_ino = vino.ino;
4596 cap->queue_release = 1;
4597 cap->cap_id = le64_to_cpu(h->cap_id);
4598 cap->mseq = mseq;
4599 cap->seq = seq;
4600 cap->issue_seq = seq;
4601 spin_lock(&session->s_cap_lock);
4602 __ceph_queue_cap_release(session, cap);
4603 spin_unlock(&session->s_cap_lock);
4604 }
4605 ceph_flush_session_cap_releases(mdsc, session);
4606 goto done;
4607
4608 bad:
4609 pr_err_client(cl, "corrupt message\n");
4610 ceph_msg_dump(msg);
4611 goto out;
4612 }
4613
4614 /*
4615 * Delayed work handler to process end of delayed cap release LRU list.
4616 *
4617 * If new caps are added to the list while processing it, these won't get
4618 * processed in this run. In this case, the ci->i_hold_caps_max will be
4619 * returned so that the work can be scheduled accordingly.
4620 */
ceph_check_delayed_caps(struct ceph_mds_client * mdsc)4621 unsigned long ceph_check_delayed_caps(struct ceph_mds_client *mdsc)
4622 {
4623 struct ceph_client *cl = mdsc->fsc->client;
4624 struct inode *inode;
4625 struct ceph_inode_info *ci;
4626 struct ceph_mount_options *opt = mdsc->fsc->mount_options;
4627 unsigned long delay_max = opt->caps_wanted_delay_max * HZ;
4628 unsigned long loop_start = jiffies;
4629 unsigned long delay = 0;
4630
4631 doutc(cl, "begin\n");
4632 spin_lock(&mdsc->cap_delay_lock);
4633 while (!list_empty(&mdsc->cap_delay_list)) {
4634 ci = list_first_entry(&mdsc->cap_delay_list,
4635 struct ceph_inode_info,
4636 i_cap_delay_list);
4637 if (time_before(loop_start, ci->i_hold_caps_max - delay_max)) {
4638 doutc(cl, "caps added recently. Exiting loop");
4639 delay = ci->i_hold_caps_max;
4640 break;
4641 }
4642 if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 &&
4643 time_before(jiffies, ci->i_hold_caps_max))
4644 break;
4645 list_del_init(&ci->i_cap_delay_list);
4646
4647 inode = igrab(&ci->netfs.inode);
4648 if (inode) {
4649 spin_unlock(&mdsc->cap_delay_lock);
4650 doutc(cl, "on %p %llx.%llx\n", inode,
4651 ceph_vinop(inode));
4652 ceph_check_caps(ci, 0);
4653 iput(inode);
4654 spin_lock(&mdsc->cap_delay_lock);
4655 }
4656
4657 /*
4658 * Make sure too many dirty caps or general
4659 * slowness doesn't block mdsc delayed work,
4660 * preventing send_renew_caps() from running.
4661 */
4662 if (time_after_eq(jiffies, loop_start + 5 * HZ))
4663 break;
4664 }
4665 spin_unlock(&mdsc->cap_delay_lock);
4666 doutc(cl, "done\n");
4667
4668 return delay;
4669 }
4670
4671 /*
4672 * Flush all dirty caps to the mds
4673 */
flush_dirty_session_caps(struct ceph_mds_session * s)4674 static void flush_dirty_session_caps(struct ceph_mds_session *s)
4675 {
4676 struct ceph_mds_client *mdsc = s->s_mdsc;
4677 struct ceph_client *cl = mdsc->fsc->client;
4678 struct ceph_inode_info *ci;
4679 struct inode *inode;
4680
4681 doutc(cl, "begin\n");
4682 spin_lock(&mdsc->cap_dirty_lock);
4683 while (!list_empty(&s->s_cap_dirty)) {
4684 ci = list_first_entry(&s->s_cap_dirty, struct ceph_inode_info,
4685 i_dirty_item);
4686 inode = &ci->netfs.inode;
4687 ihold(inode);
4688 doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
4689 spin_unlock(&mdsc->cap_dirty_lock);
4690 ceph_wait_on_async_create(inode);
4691 ceph_check_caps(ci, CHECK_CAPS_FLUSH);
4692 iput(inode);
4693 spin_lock(&mdsc->cap_dirty_lock);
4694 }
4695 spin_unlock(&mdsc->cap_dirty_lock);
4696 doutc(cl, "done\n");
4697 }
4698
ceph_flush_dirty_caps(struct ceph_mds_client * mdsc)4699 void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc)
4700 {
4701 ceph_mdsc_iterate_sessions(mdsc, flush_dirty_session_caps, true);
4702 }
4703
4704 /*
4705 * Flush all cap releases to the mds
4706 */
flush_cap_releases(struct ceph_mds_session * s)4707 static void flush_cap_releases(struct ceph_mds_session *s)
4708 {
4709 struct ceph_mds_client *mdsc = s->s_mdsc;
4710 struct ceph_client *cl = mdsc->fsc->client;
4711
4712 doutc(cl, "begin\n");
4713 spin_lock(&s->s_cap_lock);
4714 if (s->s_num_cap_releases)
4715 ceph_flush_session_cap_releases(mdsc, s);
4716 spin_unlock(&s->s_cap_lock);
4717 doutc(cl, "done\n");
4718
4719 }
4720
ceph_flush_cap_releases(struct ceph_mds_client * mdsc)4721 void ceph_flush_cap_releases(struct ceph_mds_client *mdsc)
4722 {
4723 ceph_mdsc_iterate_sessions(mdsc, flush_cap_releases, true);
4724 }
4725
__ceph_touch_fmode(struct ceph_inode_info * ci,struct ceph_mds_client * mdsc,int fmode)4726 void __ceph_touch_fmode(struct ceph_inode_info *ci,
4727 struct ceph_mds_client *mdsc, int fmode)
4728 {
4729 unsigned long now = jiffies;
4730 if (fmode & CEPH_FILE_MODE_RD)
4731 ci->i_last_rd = now;
4732 if (fmode & CEPH_FILE_MODE_WR)
4733 ci->i_last_wr = now;
4734 /* queue periodic check */
4735 if (fmode &&
4736 __ceph_is_any_real_caps(ci) &&
4737 list_empty(&ci->i_cap_delay_list))
4738 __cap_delay_requeue(mdsc, ci);
4739 }
4740
ceph_get_fmode(struct ceph_inode_info * ci,int fmode,int count)4741 void ceph_get_fmode(struct ceph_inode_info *ci, int fmode, int count)
4742 {
4743 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb);
4744 int bits = (fmode << 1) | 1;
4745 bool already_opened = false;
4746 int i;
4747
4748 if (count == 1)
4749 atomic64_inc(&mdsc->metric.opened_files);
4750
4751 spin_lock(&ci->i_ceph_lock);
4752 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4753 /*
4754 * If any of the mode ref is larger than 0,
4755 * that means it has been already opened by
4756 * others. Just skip checking the PIN ref.
4757 */
4758 if (i && ci->i_nr_by_mode[i])
4759 already_opened = true;
4760
4761 if (bits & (1 << i))
4762 ci->i_nr_by_mode[i] += count;
4763 }
4764
4765 if (!already_opened)
4766 percpu_counter_inc(&mdsc->metric.opened_inodes);
4767 spin_unlock(&ci->i_ceph_lock);
4768 }
4769
4770 /*
4771 * Drop open file reference. If we were the last open file,
4772 * we may need to release capabilities to the MDS (or schedule
4773 * their delayed release).
4774 */
ceph_put_fmode(struct ceph_inode_info * ci,int fmode,int count)4775 void ceph_put_fmode(struct ceph_inode_info *ci, int fmode, int count)
4776 {
4777 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(ci->netfs.inode.i_sb);
4778 int bits = (fmode << 1) | 1;
4779 bool is_closed = true;
4780 int i;
4781
4782 if (count == 1)
4783 atomic64_dec(&mdsc->metric.opened_files);
4784
4785 spin_lock(&ci->i_ceph_lock);
4786 for (i = 0; i < CEPH_FILE_MODE_BITS; i++) {
4787 if (bits & (1 << i)) {
4788 BUG_ON(ci->i_nr_by_mode[i] < count);
4789 ci->i_nr_by_mode[i] -= count;
4790 }
4791
4792 /*
4793 * If any of the mode ref is not 0 after
4794 * decreased, that means it is still opened
4795 * by others. Just skip checking the PIN ref.
4796 */
4797 if (i && ci->i_nr_by_mode[i])
4798 is_closed = false;
4799 }
4800
4801 if (is_closed)
4802 percpu_counter_dec(&mdsc->metric.opened_inodes);
4803 spin_unlock(&ci->i_ceph_lock);
4804 }
4805
4806 /*
4807 * For a soon-to-be unlinked file, drop the LINK caps. If it
4808 * looks like the link count will hit 0, drop any other caps (other
4809 * than PIN) we don't specifically want (due to the file still being
4810 * open).
4811 */
ceph_drop_caps_for_unlink(struct inode * inode)4812 int ceph_drop_caps_for_unlink(struct inode *inode)
4813 {
4814 struct ceph_inode_info *ci = ceph_inode(inode);
4815 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
4816
4817 spin_lock(&ci->i_ceph_lock);
4818 if (inode->i_nlink == 1) {
4819 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
4820
4821 if (__ceph_caps_dirty(ci)) {
4822 struct ceph_mds_client *mdsc =
4823 ceph_inode_to_fs_client(inode)->mdsc;
4824
4825 doutc(mdsc->fsc->client, "%p %llx.%llx\n", inode,
4826 ceph_vinop(inode));
4827 spin_lock(&mdsc->cap_delay_lock);
4828 set_bit(CEPH_I_FLUSH_BIT, &ci->i_ceph_flags);
4829 if (!list_empty(&ci->i_cap_delay_list))
4830 list_del_init(&ci->i_cap_delay_list);
4831 list_add_tail(&ci->i_cap_delay_list,
4832 &mdsc->cap_unlink_delay_list);
4833 spin_unlock(&mdsc->cap_delay_lock);
4834
4835 /*
4836 * Fire the work immediately, because the MDS maybe
4837 * waiting for caps release.
4838 */
4839 ceph_queue_cap_unlink_work(mdsc);
4840 }
4841 }
4842 spin_unlock(&ci->i_ceph_lock);
4843 return drop;
4844 }
4845
4846 /*
4847 * Helpers for embedding cap and dentry lease releases into mds
4848 * requests.
4849 *
4850 * @force is used by dentry_release (below) to force inclusion of a
4851 * record for the directory inode, even when there aren't any caps to
4852 * drop.
4853 */
ceph_encode_inode_release(void ** p,struct inode * inode,int mds,int drop,int unless,int force)4854 int ceph_encode_inode_release(void **p, struct inode *inode,
4855 int mds, int drop, int unless, int force)
4856 {
4857 struct ceph_inode_info *ci = ceph_inode(inode);
4858 struct ceph_client *cl = ceph_inode_to_client(inode);
4859 struct ceph_cap *cap;
4860 struct ceph_mds_request_release *rel = *p;
4861 int used, dirty;
4862 int ret = 0;
4863
4864 spin_lock(&ci->i_ceph_lock);
4865 used = __ceph_caps_used(ci);
4866 dirty = __ceph_caps_dirty(ci);
4867
4868 doutc(cl, "%p %llx.%llx mds%d used|dirty %s drop %s unless %s\n",
4869 inode, ceph_vinop(inode), mds, ceph_cap_string(used|dirty),
4870 ceph_cap_string(drop), ceph_cap_string(unless));
4871
4872 /* only drop unused, clean caps */
4873 drop &= ~(used | dirty);
4874
4875 cap = __get_cap_for_mds(ci, mds);
4876 if (cap && __cap_is_valid(cap)) {
4877 unless &= cap->issued;
4878 if (unless) {
4879 if (unless & CEPH_CAP_AUTH_EXCL)
4880 drop &= ~CEPH_CAP_AUTH_SHARED;
4881 if (unless & CEPH_CAP_LINK_EXCL)
4882 drop &= ~CEPH_CAP_LINK_SHARED;
4883 if (unless & CEPH_CAP_XATTR_EXCL)
4884 drop &= ~CEPH_CAP_XATTR_SHARED;
4885 if (unless & CEPH_CAP_FILE_EXCL)
4886 drop &= ~CEPH_CAP_FILE_SHARED;
4887 }
4888
4889 if (force || (cap->issued & drop)) {
4890 if (cap->issued & drop) {
4891 int wanted = __ceph_caps_wanted(ci);
4892 doutc(cl, "%p %llx.%llx cap %p %s -> %s, "
4893 "wanted %s -> %s\n", inode,
4894 ceph_vinop(inode), cap,
4895 ceph_cap_string(cap->issued),
4896 ceph_cap_string(cap->issued & ~drop),
4897 ceph_cap_string(cap->mds_wanted),
4898 ceph_cap_string(wanted));
4899
4900 cap->issued &= ~drop;
4901 cap->implemented &= ~drop;
4902 cap->mds_wanted = wanted;
4903 if (cap == ci->i_auth_cap &&
4904 !(wanted & CEPH_CAP_ANY_FILE_WR))
4905 ci->i_requested_max_size = 0;
4906 } else {
4907 doutc(cl, "%p %llx.%llx cap %p %s (force)\n",
4908 inode, ceph_vinop(inode), cap,
4909 ceph_cap_string(cap->issued));
4910 }
4911
4912 rel->ino = cpu_to_le64(ceph_ino(inode));
4913 rel->cap_id = cpu_to_le64(cap->cap_id);
4914 rel->seq = cpu_to_le32(cap->seq);
4915 rel->issue_seq = cpu_to_le32(cap->issue_seq);
4916 rel->mseq = cpu_to_le32(cap->mseq);
4917 rel->caps = cpu_to_le32(cap->implemented);
4918 rel->wanted = cpu_to_le32(cap->mds_wanted);
4919 rel->dname_len = 0;
4920 rel->dname_seq = 0;
4921 *p += sizeof(*rel);
4922 ret = 1;
4923 } else {
4924 doutc(cl, "%p %llx.%llx cap %p %s (noop)\n",
4925 inode, ceph_vinop(inode), cap,
4926 ceph_cap_string(cap->issued));
4927 }
4928 }
4929 spin_unlock(&ci->i_ceph_lock);
4930 return ret;
4931 }
4932
4933 /**
4934 * ceph_encode_dentry_release - encode a dentry release into an outgoing request
4935 * @p: outgoing request buffer
4936 * @dentry: dentry to release
4937 * @dir: dir to release it from
4938 * @mds: mds that we're speaking to
4939 * @drop: caps being dropped
4940 * @unless: unless we have these caps
4941 *
4942 * Encode a dentry release into an outgoing request buffer. Returns 1 if the
4943 * thing was released, or a negative error code otherwise.
4944 */
ceph_encode_dentry_release(void ** p,struct dentry * dentry,struct inode * dir,int mds,int drop,int unless)4945 int ceph_encode_dentry_release(void **p, struct dentry *dentry,
4946 struct inode *dir,
4947 int mds, int drop, int unless)
4948 {
4949 struct ceph_mds_request_release *rel = *p;
4950 struct ceph_dentry_info *di = ceph_dentry(dentry);
4951 struct ceph_client *cl;
4952 int force = 0;
4953 int ret;
4954
4955 /* This shouldn't happen */
4956 BUG_ON(!dir);
4957
4958 /*
4959 * force an record for the directory caps if we have a dentry lease.
4960 * this is racy (can't take i_ceph_lock and d_lock together), but it
4961 * doesn't have to be perfect; the mds will revoke anything we don't
4962 * release.
4963 */
4964 spin_lock(&dentry->d_lock);
4965 if (di->lease_session && di->lease_session->s_mds == mds)
4966 force = 1;
4967 spin_unlock(&dentry->d_lock);
4968
4969 ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force);
4970
4971 cl = ceph_inode_to_client(dir);
4972 spin_lock(&dentry->d_lock);
4973 if (ret && di->lease_session && di->lease_session->s_mds == mds) {
4974 int len = dentry->d_name.len;
4975 doutc(cl, "%p mds%d seq %d\n", dentry, mds,
4976 (int)di->lease_seq);
4977 rel->dname_seq = cpu_to_le32(di->lease_seq);
4978 __ceph_mdsc_drop_dentry_lease(dentry);
4979 memcpy(*p, dentry->d_name.name, len);
4980 spin_unlock(&dentry->d_lock);
4981 if (IS_ENCRYPTED(dir) && fscrypt_has_encryption_key(dir)) {
4982 len = ceph_encode_encrypted_dname(dir, *p, len);
4983 if (len < 0)
4984 return len;
4985 }
4986 rel->dname_len = cpu_to_le32(len);
4987 *p += len;
4988 } else {
4989 spin_unlock(&dentry->d_lock);
4990 }
4991 return ret;
4992 }
4993
remove_capsnaps(struct ceph_mds_client * mdsc,struct inode * inode)4994 static int remove_capsnaps(struct ceph_mds_client *mdsc, struct inode *inode)
4995 {
4996 struct ceph_inode_info *ci = ceph_inode(inode);
4997 struct ceph_client *cl = mdsc->fsc->client;
4998 struct ceph_cap_snap *capsnap;
4999 int capsnap_release = 0;
5000
5001 lockdep_assert_held(&ci->i_ceph_lock);
5002
5003 doutc(cl, "removing capsnaps, ci is %p, %p %llx.%llx\n",
5004 ci, inode, ceph_vinop(inode));
5005
5006 while (!list_empty(&ci->i_cap_snaps)) {
5007 capsnap = list_first_entry(&ci->i_cap_snaps,
5008 struct ceph_cap_snap, ci_item);
5009 __ceph_remove_capsnap(inode, capsnap, NULL, NULL);
5010 ceph_put_snap_context(capsnap->context);
5011 ceph_put_cap_snap(capsnap);
5012 capsnap_release++;
5013 }
5014 wake_up_all(&ci->i_cap_wq);
5015 wake_up_all(&mdsc->cap_flushing_wq);
5016 return capsnap_release;
5017 }
5018
ceph_purge_inode_cap(struct inode * inode,struct ceph_cap * cap,bool * invalidate)5019 int ceph_purge_inode_cap(struct inode *inode, struct ceph_cap *cap, bool *invalidate)
5020 {
5021 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
5022 struct ceph_mds_client *mdsc = fsc->mdsc;
5023 struct ceph_client *cl = fsc->client;
5024 struct ceph_inode_info *ci = ceph_inode(inode);
5025 bool is_auth;
5026 bool dirty_dropped = false;
5027 int iputs = 0;
5028
5029 lockdep_assert_held(&ci->i_ceph_lock);
5030
5031 doutc(cl, "removing cap %p, ci is %p, %p %llx.%llx\n",
5032 cap, ci, inode, ceph_vinop(inode));
5033
5034 is_auth = (cap == ci->i_auth_cap);
5035 __ceph_remove_cap(cap, false);
5036 if (is_auth) {
5037 struct ceph_cap_flush *cf;
5038
5039 if (ceph_inode_is_shutdown(inode)) {
5040 if (inode->i_data.nrpages > 0)
5041 *invalidate = true;
5042 if (ci->i_wrbuffer_ref > 0)
5043 mapping_set_error(&inode->i_data, -EIO);
5044 }
5045
5046 spin_lock(&mdsc->cap_dirty_lock);
5047
5048 /* trash all of the cap flushes for this inode */
5049 while (!list_empty(&ci->i_cap_flush_list)) {
5050 cf = list_first_entry(&ci->i_cap_flush_list,
5051 struct ceph_cap_flush, i_list);
5052 list_del_init(&cf->g_list);
5053 list_del_init(&cf->i_list);
5054 if (!cf->is_capsnap)
5055 ceph_free_cap_flush(cf);
5056 }
5057
5058 if (!list_empty(&ci->i_dirty_item)) {
5059 pr_warn_ratelimited_client(cl,
5060 " dropping dirty %s state for %p %llx.%llx\n",
5061 ceph_cap_string(ci->i_dirty_caps),
5062 inode, ceph_vinop(inode));
5063 ci->i_dirty_caps = 0;
5064 list_del_init(&ci->i_dirty_item);
5065 dirty_dropped = true;
5066 }
5067 if (!list_empty(&ci->i_flushing_item)) {
5068 pr_warn_ratelimited_client(cl,
5069 " dropping dirty+flushing %s state for %p %llx.%llx\n",
5070 ceph_cap_string(ci->i_flushing_caps),
5071 inode, ceph_vinop(inode));
5072 ci->i_flushing_caps = 0;
5073 list_del_init(&ci->i_flushing_item);
5074 mdsc->num_cap_flushing--;
5075 dirty_dropped = true;
5076 }
5077 spin_unlock(&mdsc->cap_dirty_lock);
5078
5079 if (dirty_dropped) {
5080 mapping_set_error(inode->i_mapping, -EIO);
5081
5082 if (ci->i_wrbuffer_ref_head == 0 &&
5083 ci->i_wr_ref == 0 &&
5084 ci->i_dirty_caps == 0 &&
5085 ci->i_flushing_caps == 0) {
5086 ceph_put_snap_context(ci->i_head_snapc);
5087 ci->i_head_snapc = NULL;
5088 }
5089 }
5090
5091 if (atomic_read(&ci->i_filelock_ref) > 0) {
5092 /* make further file lock syscall return -EIO */
5093 set_bit(CEPH_I_ERROR_FILELOCK_BIT, &ci->i_ceph_flags);
5094 pr_warn_ratelimited_client(cl,
5095 " dropping file locks for %p %llx.%llx\n",
5096 inode, ceph_vinop(inode));
5097 }
5098
5099 if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) {
5100 cf = ci->i_prealloc_cap_flush;
5101 ci->i_prealloc_cap_flush = NULL;
5102 if (!cf->is_capsnap)
5103 ceph_free_cap_flush(cf);
5104 }
5105
5106 if (!list_empty(&ci->i_cap_snaps))
5107 iputs = remove_capsnaps(mdsc, inode);
5108 }
5109 if (dirty_dropped)
5110 ++iputs;
5111 return iputs;
5112 }
5113