1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/spinlock.h>
5 #include <linux/namei.h>
6 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/xattr.h>
9
10 #include "super.h"
11 #include "mds_client.h"
12 #include "crypto.h"
13
14 /*
15 * Directory operations: readdir, lookup, create, link, unlink,
16 * rename, etc.
17 */
18
19 /*
20 * Ceph MDS operations are specified in terms of a base ino and
21 * relative path. Thus, the client can specify an operation on a
22 * specific inode (e.g., a getattr due to fstat(2)), or as a path
23 * relative to, say, the root directory.
24 *
25 * Normally, we limit ourselves to strict inode ops (no path component)
26 * or dentry operations (a single path component relative to an ino). The
27 * exception to this is open_root_dentry(), which will open the mount
28 * point by name.
29 */
30
31 const struct dentry_operations ceph_dentry_ops;
32
33 static bool __dentry_lease_is_valid(struct ceph_dentry_info *di);
34 static int __dir_lease_try_check(const struct dentry *dentry);
35
36 /*
37 * Initialize ceph dentry state.
38 */
ceph_d_init(struct dentry * dentry)39 static int ceph_d_init(struct dentry *dentry)
40 {
41 struct ceph_dentry_info *di;
42 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dentry->d_sb);
43
44 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
45 if (!di)
46 return -ENOMEM; /* oh well */
47
48 di->dentry = dentry;
49 di->lease_session = NULL;
50 di->time = jiffies;
51 dentry->d_fsdata = di;
52 INIT_LIST_HEAD(&di->lease_list);
53
54 atomic64_inc(&mdsc->metric.total_dentries);
55
56 return 0;
57 }
58
59 /*
60 * for f_pos for readdir:
61 * - hash order:
62 * (0xff << 52) | ((24 bits hash) << 28) |
63 * (the nth entry has hash collision);
64 * - frag+name order;
65 * ((frag value) << 28) | (the nth entry in frag);
66 */
67 #define OFFSET_BITS 28
68 #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
69 #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
ceph_make_fpos(unsigned high,unsigned off,bool hash_order)70 loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
71 {
72 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
73 if (hash_order)
74 fpos |= HASH_ORDER;
75 return fpos;
76 }
77
is_hash_order(loff_t p)78 static bool is_hash_order(loff_t p)
79 {
80 return (p & HASH_ORDER) == HASH_ORDER;
81 }
82
fpos_frag(loff_t p)83 static unsigned fpos_frag(loff_t p)
84 {
85 return p >> OFFSET_BITS;
86 }
87
fpos_hash(loff_t p)88 static unsigned fpos_hash(loff_t p)
89 {
90 return ceph_frag_value(fpos_frag(p));
91 }
92
fpos_off(loff_t p)93 static unsigned fpos_off(loff_t p)
94 {
95 return p & OFFSET_MASK;
96 }
97
fpos_cmp(loff_t l,loff_t r)98 static int fpos_cmp(loff_t l, loff_t r)
99 {
100 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
101 if (v)
102 return v;
103 return (int)(fpos_off(l) - fpos_off(r));
104 }
105
106 /*
107 * make note of the last dentry we read, so we can
108 * continue at the same lexicographical point,
109 * regardless of what dir changes take place on the
110 * server.
111 */
note_last_dentry(struct ceph_fs_client * fsc,struct ceph_dir_file_info * dfi,const char * name,int len,unsigned next_offset)112 static int note_last_dentry(struct ceph_fs_client *fsc,
113 struct ceph_dir_file_info *dfi,
114 const char *name,
115 int len, unsigned next_offset)
116 {
117 char *buf = kmalloc(len+1, GFP_KERNEL);
118 if (!buf)
119 return -ENOMEM;
120 kfree(dfi->last_name);
121 dfi->last_name = buf;
122 memcpy(dfi->last_name, name, len);
123 dfi->last_name[len] = 0;
124 dfi->next_offset = next_offset;
125 doutc(fsc->client, "'%s'\n", dfi->last_name);
126 return 0;
127 }
128
129
130 static struct dentry *
__dcache_find_get_entry(struct dentry * parent,u64 idx,struct ceph_readdir_cache_control * cache_ctl)131 __dcache_find_get_entry(struct dentry *parent, u64 idx,
132 struct ceph_readdir_cache_control *cache_ctl)
133 {
134 struct inode *dir = d_inode(parent);
135 struct ceph_client *cl = ceph_inode_to_client(dir);
136 struct dentry *dentry;
137 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
138 loff_t ptr_pos = idx * sizeof(struct dentry *);
139 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
140
141 if (ptr_pos >= i_size_read(dir))
142 return NULL;
143
144 if (!cache_ctl->folio || ptr_pgoff != cache_ctl->folio->index) {
145 ceph_readdir_cache_release(cache_ctl);
146 cache_ctl->folio = filemap_lock_folio(&dir->i_data, ptr_pgoff);
147 if (IS_ERR(cache_ctl->folio)) {
148 cache_ctl->folio = NULL;
149 doutc(cl, " folio %lu not found\n", ptr_pgoff);
150 return ERR_PTR(-EAGAIN);
151 }
152 /* reading/filling the cache are serialized by
153 i_rwsem, no need to use folio lock */
154 folio_unlock(cache_ctl->folio);
155 cache_ctl->dentries = kmap_local_folio(cache_ctl->folio, 0);
156 }
157
158 cache_ctl->index = idx & idx_mask;
159
160 rcu_read_lock();
161 spin_lock(&parent->d_lock);
162 /* check i_size again here, because empty directory can be
163 * marked as complete while not holding the i_rwsem. */
164 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
165 dentry = cache_ctl->dentries[cache_ctl->index];
166 else
167 dentry = NULL;
168 spin_unlock(&parent->d_lock);
169 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
170 dentry = NULL;
171 rcu_read_unlock();
172 return dentry ? : ERR_PTR(-EAGAIN);
173 }
174
175 /*
176 * When possible, we try to satisfy a readdir by peeking at the
177 * dcache. We make this work by carefully ordering dentries on
178 * d_children when we initially get results back from the MDS, and
179 * falling back to a "normal" sync readdir if any dentries in the dir
180 * are dropped.
181 *
182 * Complete dir indicates that we have all dentries in the dir. It is
183 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
184 * the MDS if/when the directory is modified).
185 */
__dcache_readdir(struct file * file,struct dir_context * ctx,int shared_gen)186 static int __dcache_readdir(struct file *file, struct dir_context *ctx,
187 int shared_gen)
188 {
189 struct ceph_dir_file_info *dfi = file->private_data;
190 struct dentry *parent = file->f_path.dentry;
191 struct inode *dir = d_inode(parent);
192 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(dir);
193 struct ceph_client *cl = ceph_inode_to_client(dir);
194 struct dentry *dentry, *last = NULL;
195 struct ceph_dentry_info *di;
196 struct ceph_readdir_cache_control cache_ctl = {};
197 u64 idx = 0;
198 int err = 0;
199
200 doutc(cl, "%p %llx.%llx v%u at %llx\n", dir, ceph_vinop(dir),
201 (unsigned)shared_gen, ctx->pos);
202
203 /* search start position */
204 if (ctx->pos > 2) {
205 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
206 while (count > 0) {
207 u64 step = count >> 1;
208 dentry = __dcache_find_get_entry(parent, idx + step,
209 &cache_ctl);
210 if (!dentry) {
211 /* use linear search */
212 idx = 0;
213 break;
214 }
215 if (IS_ERR(dentry)) {
216 err = PTR_ERR(dentry);
217 goto out;
218 }
219 di = ceph_dentry(dentry);
220 spin_lock(&dentry->d_lock);
221 if (fpos_cmp(di->offset, ctx->pos) < 0) {
222 idx += step + 1;
223 count -= step + 1;
224 } else {
225 count = step;
226 }
227 spin_unlock(&dentry->d_lock);
228 dput(dentry);
229 }
230
231 doutc(cl, "%p %llx.%llx cache idx %llu\n", dir,
232 ceph_vinop(dir), idx);
233 }
234
235
236 for (;;) {
237 bool emit_dentry = false;
238 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
239 if (!dentry) {
240 dfi->file_info.flags |= CEPH_F_ATEND;
241 err = 0;
242 break;
243 }
244 if (IS_ERR(dentry)) {
245 err = PTR_ERR(dentry);
246 goto out;
247 }
248
249 spin_lock(&dentry->d_lock);
250 di = ceph_dentry(dentry);
251 if (d_unhashed(dentry) ||
252 d_really_is_negative(dentry) ||
253 di->lease_shared_gen != shared_gen ||
254 ((dentry->d_flags & DCACHE_NOKEY_NAME) &&
255 fscrypt_has_encryption_key(dir))) {
256 spin_unlock(&dentry->d_lock);
257 dput(dentry);
258 err = -EAGAIN;
259 goto out;
260 }
261 if (fpos_cmp(ctx->pos, di->offset) <= 0) {
262 __ceph_dentry_dir_lease_touch(di);
263 emit_dentry = true;
264 }
265 spin_unlock(&dentry->d_lock);
266
267 if (emit_dentry) {
268 doutc(cl, " %llx dentry %p %pd %p\n", di->offset,
269 dentry, dentry, d_inode(dentry));
270 ctx->pos = di->offset;
271 if (!dir_emit(ctx, dentry->d_name.name,
272 dentry->d_name.len, ceph_present_inode(d_inode(dentry)),
273 d_inode(dentry)->i_mode >> 12)) {
274 dput(dentry);
275 err = 0;
276 break;
277 }
278 ctx->pos++;
279
280 if (last)
281 dput(last);
282 last = dentry;
283 } else {
284 dput(dentry);
285 }
286 }
287 out:
288 ceph_readdir_cache_release(&cache_ctl);
289 if (last) {
290 int ret;
291 di = ceph_dentry(last);
292 ret = note_last_dentry(fsc, dfi, last->d_name.name,
293 last->d_name.len,
294 fpos_off(di->offset) + 1);
295 if (ret < 0)
296 err = ret;
297 dput(last);
298 /* last_name no longer match cache index */
299 if (dfi->readdir_cache_idx >= 0) {
300 dfi->readdir_cache_idx = -1;
301 dfi->dir_release_count = 0;
302 }
303 }
304 return err;
305 }
306
need_send_readdir(struct ceph_dir_file_info * dfi,loff_t pos)307 static bool need_send_readdir(struct ceph_dir_file_info *dfi, loff_t pos)
308 {
309 if (!dfi->last_readdir)
310 return true;
311 if (is_hash_order(pos))
312 return !ceph_frag_contains_value(dfi->frag, fpos_hash(pos));
313 else
314 return dfi->frag != fpos_frag(pos);
315 }
316
ceph_readdir(struct file * file,struct dir_context * ctx)317 static int ceph_readdir(struct file *file, struct dir_context *ctx)
318 {
319 struct ceph_dir_file_info *dfi = file->private_data;
320 struct inode *inode = file_inode(file);
321 struct ceph_inode_info *ci = ceph_inode(inode);
322 struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
323 struct ceph_mds_client *mdsc = fsc->mdsc;
324 struct ceph_client *cl = fsc->client;
325 int i;
326 int err;
327 unsigned frag = -1;
328 struct ceph_mds_reply_info_parsed *rinfo;
329
330 doutc(cl, "%p %llx.%llx file %p pos %llx\n", inode,
331 ceph_vinop(inode), file, ctx->pos);
332 if (dfi->file_info.flags & CEPH_F_ATEND)
333 return 0;
334
335 /* always start with . and .. */
336 if (ctx->pos == 0) {
337 doutc(cl, "%p %llx.%llx off 0 -> '.'\n", inode,
338 ceph_vinop(inode));
339 if (!dir_emit(ctx, ".", 1, ceph_present_inode(inode),
340 inode->i_mode >> 12))
341 return 0;
342 ctx->pos = 1;
343 }
344 if (ctx->pos == 1) {
345 u64 ino;
346 struct dentry *dentry = file->f_path.dentry;
347
348 spin_lock(&dentry->d_lock);
349 ino = ceph_present_inode(dentry->d_parent->d_inode);
350 spin_unlock(&dentry->d_lock);
351
352 doutc(cl, "%p %llx.%llx off 1 -> '..'\n", inode,
353 ceph_vinop(inode));
354 if (!dir_emit(ctx, "..", 2, ino, inode->i_mode >> 12))
355 return 0;
356 ctx->pos = 2;
357 }
358
359 err = ceph_fscrypt_prepare_readdir(inode);
360 if (err < 0)
361 return err;
362
363 spin_lock(&ci->i_ceph_lock);
364 /* request Fx cap. if have Fx, we don't need to release Fs cap
365 * for later create/unlink. */
366 __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_WR);
367 /* can we use the dcache? */
368 if (ceph_test_mount_opt(fsc, DCACHE) &&
369 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
370 ceph_snap(inode) != CEPH_SNAPDIR &&
371 __ceph_dir_is_complete_ordered(ci) &&
372 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) {
373 int shared_gen = atomic_read(&ci->i_shared_gen);
374
375 spin_unlock(&ci->i_ceph_lock);
376 err = __dcache_readdir(file, ctx, shared_gen);
377 if (err != -EAGAIN)
378 return err;
379 } else {
380 spin_unlock(&ci->i_ceph_lock);
381 }
382
383 /* proceed with a normal readdir */
384 more:
385 /* do we have the correct frag content buffered? */
386 if (need_send_readdir(dfi, ctx->pos)) {
387 struct ceph_mds_request *req;
388 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
389 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
390
391 /* discard old result, if any */
392 if (dfi->last_readdir) {
393 ceph_mdsc_put_request(dfi->last_readdir);
394 dfi->last_readdir = NULL;
395 }
396
397 if (is_hash_order(ctx->pos)) {
398 /* fragtree isn't always accurate. choose frag
399 * based on previous reply when possible. */
400 if (frag == (unsigned)-1)
401 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
402 NULL, NULL);
403 } else {
404 frag = fpos_frag(ctx->pos);
405 }
406
407 doutc(cl, "fetching %p %llx.%llx frag %x offset '%s'\n",
408 inode, ceph_vinop(inode), frag, dfi->last_name);
409 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
410 if (IS_ERR(req))
411 return PTR_ERR(req);
412
413 err = ceph_alloc_readdir_reply_buffer(req, inode);
414 if (err) {
415 ceph_mdsc_put_request(req);
416 return err;
417 }
418 /* hints to request -> mds selection code */
419 req->r_direct_mode = USE_AUTH_MDS;
420 if (op == CEPH_MDS_OP_READDIR) {
421 req->r_direct_hash = ceph_frag_value(frag);
422 __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
423 req->r_inode_drop = CEPH_CAP_FILE_EXCL;
424 }
425 if (dfi->last_name) {
426 struct qstr d_name = { .name = dfi->last_name,
427 .len = strlen(dfi->last_name) };
428
429 req->r_path2 = kzalloc(NAME_MAX + 1, GFP_KERNEL);
430 if (!req->r_path2) {
431 ceph_mdsc_put_request(req);
432 return -ENOMEM;
433 }
434
435 err = ceph_encode_encrypted_dname(inode, &d_name,
436 req->r_path2);
437 if (err < 0) {
438 ceph_mdsc_put_request(req);
439 return err;
440 }
441 } else if (is_hash_order(ctx->pos)) {
442 req->r_args.readdir.offset_hash =
443 cpu_to_le32(fpos_hash(ctx->pos));
444 }
445
446 req->r_dir_release_cnt = dfi->dir_release_count;
447 req->r_dir_ordered_cnt = dfi->dir_ordered_count;
448 req->r_readdir_cache_idx = dfi->readdir_cache_idx;
449 req->r_readdir_offset = dfi->next_offset;
450 req->r_args.readdir.frag = cpu_to_le32(frag);
451 req->r_args.readdir.flags =
452 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
453
454 req->r_inode = inode;
455 ihold(inode);
456 req->r_dentry = dget(file->f_path.dentry);
457 err = ceph_mdsc_do_request(mdsc, NULL, req);
458 if (err < 0) {
459 ceph_mdsc_put_request(req);
460 return err;
461 }
462 doutc(cl, "%p %llx.%llx got and parsed readdir result=%d"
463 "on frag %x, end=%d, complete=%d, hash_order=%d\n",
464 inode, ceph_vinop(inode), err, frag,
465 (int)req->r_reply_info.dir_end,
466 (int)req->r_reply_info.dir_complete,
467 (int)req->r_reply_info.hash_order);
468
469 rinfo = &req->r_reply_info;
470 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
471 frag = le32_to_cpu(rinfo->dir_dir->frag);
472 if (!rinfo->hash_order) {
473 dfi->next_offset = req->r_readdir_offset;
474 /* adjust ctx->pos to beginning of frag */
475 ctx->pos = ceph_make_fpos(frag,
476 dfi->next_offset,
477 false);
478 }
479 }
480
481 dfi->frag = frag;
482 dfi->last_readdir = req;
483
484 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
485 dfi->readdir_cache_idx = req->r_readdir_cache_idx;
486 if (dfi->readdir_cache_idx < 0) {
487 /* preclude from marking dir ordered */
488 dfi->dir_ordered_count = 0;
489 } else if (ceph_frag_is_leftmost(frag) &&
490 dfi->next_offset == 2) {
491 /* note dir version at start of readdir so
492 * we can tell if any dentries get dropped */
493 dfi->dir_release_count = req->r_dir_release_cnt;
494 dfi->dir_ordered_count = req->r_dir_ordered_cnt;
495 }
496 } else {
497 doutc(cl, "%p %llx.%llx !did_prepopulate\n", inode,
498 ceph_vinop(inode));
499 /* disable readdir cache */
500 dfi->readdir_cache_idx = -1;
501 /* preclude from marking dir complete */
502 dfi->dir_release_count = 0;
503 }
504
505 /* note next offset and last dentry name */
506 if (rinfo->dir_nr > 0) {
507 struct ceph_mds_reply_dir_entry *rde =
508 rinfo->dir_entries + (rinfo->dir_nr-1);
509 unsigned next_offset = req->r_reply_info.dir_end ?
510 2 : (fpos_off(rde->offset) + 1);
511 err = note_last_dentry(fsc, dfi, rde->name,
512 rde->name_len, next_offset);
513 if (err) {
514 ceph_mdsc_put_request(dfi->last_readdir);
515 dfi->last_readdir = NULL;
516 return err;
517 }
518 } else if (req->r_reply_info.dir_end) {
519 dfi->next_offset = 2;
520 /* keep last name */
521 }
522 }
523
524 rinfo = &dfi->last_readdir->r_reply_info;
525 doutc(cl, "%p %llx.%llx frag %x num %d pos %llx chunk first %llx\n",
526 inode, ceph_vinop(inode), dfi->frag, rinfo->dir_nr, ctx->pos,
527 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
528
529 i = 0;
530 /* search start position */
531 if (rinfo->dir_nr > 0) {
532 int step, nr = rinfo->dir_nr;
533 while (nr > 0) {
534 step = nr >> 1;
535 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
536 i += step + 1;
537 nr -= step + 1;
538 } else {
539 nr = step;
540 }
541 }
542 }
543 for (; i < rinfo->dir_nr; i++) {
544 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
545
546 if (rde->offset < ctx->pos) {
547 pr_warn_client(cl,
548 "%p %llx.%llx rde->offset 0x%llx ctx->pos 0x%llx\n",
549 inode, ceph_vinop(inode), rde->offset, ctx->pos);
550 return -EIO;
551 }
552
553 if (WARN_ON_ONCE(!rde->inode.in))
554 return -EIO;
555
556 ctx->pos = rde->offset;
557 doutc(cl, "%p %llx.%llx (%d/%d) -> %llx '%.*s' %p\n", inode,
558 ceph_vinop(inode), i, rinfo->dir_nr, ctx->pos,
559 rde->name_len, rde->name, &rde->inode.in);
560
561 if (!dir_emit(ctx, rde->name, rde->name_len,
562 ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
563 le32_to_cpu(rde->inode.in->mode) >> 12)) {
564 /*
565 * NOTE: Here no need to put the 'dfi->last_readdir',
566 * because when dir_emit stops us it's most likely
567 * doesn't have enough memory, etc. So for next readdir
568 * it will continue.
569 */
570 doutc(cl, "filldir stopping us...\n");
571 return 0;
572 }
573
574 /* Reset the lengths to their original allocated vals */
575 ctx->pos++;
576 }
577
578 ceph_mdsc_put_request(dfi->last_readdir);
579 dfi->last_readdir = NULL;
580
581 if (dfi->next_offset > 2) {
582 frag = dfi->frag;
583 goto more;
584 }
585
586 /* more frags? */
587 if (!ceph_frag_is_rightmost(dfi->frag)) {
588 frag = ceph_frag_next(dfi->frag);
589 if (is_hash_order(ctx->pos)) {
590 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
591 dfi->next_offset, true);
592 if (new_pos > ctx->pos)
593 ctx->pos = new_pos;
594 /* keep last_name */
595 } else {
596 ctx->pos = ceph_make_fpos(frag, dfi->next_offset,
597 false);
598 kfree(dfi->last_name);
599 dfi->last_name = NULL;
600 }
601 doutc(cl, "%p %llx.%llx next frag is %x\n", inode,
602 ceph_vinop(inode), frag);
603 goto more;
604 }
605 dfi->file_info.flags |= CEPH_F_ATEND;
606
607 /*
608 * if dir_release_count still matches the dir, no dentries
609 * were released during the whole readdir, and we should have
610 * the complete dir contents in our cache.
611 */
612 if (atomic64_read(&ci->i_release_count) ==
613 dfi->dir_release_count) {
614 spin_lock(&ci->i_ceph_lock);
615 if (dfi->dir_ordered_count ==
616 atomic64_read(&ci->i_ordered_count)) {
617 doutc(cl, " marking %p %llx.%llx complete and ordered\n",
618 inode, ceph_vinop(inode));
619 /* use i_size to track number of entries in
620 * readdir cache */
621 BUG_ON(dfi->readdir_cache_idx < 0);
622 i_size_write(inode, dfi->readdir_cache_idx *
623 sizeof(struct dentry*));
624 } else {
625 doutc(cl, " marking %llx.%llx complete\n",
626 ceph_vinop(inode));
627 }
628 __ceph_dir_set_complete(ci, dfi->dir_release_count,
629 dfi->dir_ordered_count);
630 spin_unlock(&ci->i_ceph_lock);
631 }
632 doutc(cl, "%p %llx.%llx file %p done.\n", inode, ceph_vinop(inode),
633 file);
634 return 0;
635 }
636
reset_readdir(struct ceph_dir_file_info * dfi)637 static void reset_readdir(struct ceph_dir_file_info *dfi)
638 {
639 if (dfi->last_readdir) {
640 ceph_mdsc_put_request(dfi->last_readdir);
641 dfi->last_readdir = NULL;
642 }
643 kfree(dfi->last_name);
644 dfi->last_name = NULL;
645 dfi->dir_release_count = 0;
646 dfi->readdir_cache_idx = -1;
647 dfi->next_offset = 2; /* compensate for . and .. */
648 dfi->file_info.flags &= ~CEPH_F_ATEND;
649 }
650
651 /*
652 * discard buffered readdir content on seekdir(0), or seek to new frag,
653 * or seek prior to current chunk
654 */
need_reset_readdir(struct ceph_dir_file_info * dfi,loff_t new_pos)655 static bool need_reset_readdir(struct ceph_dir_file_info *dfi, loff_t new_pos)
656 {
657 struct ceph_mds_reply_info_parsed *rinfo;
658 loff_t chunk_offset;
659 if (new_pos == 0)
660 return true;
661 if (is_hash_order(new_pos)) {
662 /* no need to reset last_name for a forward seek when
663 * dentries are sorted in hash order */
664 } else if (dfi->frag != fpos_frag(new_pos)) {
665 return true;
666 }
667 rinfo = dfi->last_readdir ? &dfi->last_readdir->r_reply_info : NULL;
668 if (!rinfo || !rinfo->dir_nr)
669 return true;
670 chunk_offset = rinfo->dir_entries[0].offset;
671 return new_pos < chunk_offset ||
672 is_hash_order(new_pos) != is_hash_order(chunk_offset);
673 }
674
ceph_dir_llseek(struct file * file,loff_t offset,int whence)675 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
676 {
677 struct ceph_dir_file_info *dfi = file->private_data;
678 struct inode *inode = file->f_mapping->host;
679 struct ceph_client *cl = ceph_inode_to_client(inode);
680 loff_t retval;
681
682 inode_lock(inode);
683 retval = -EINVAL;
684 switch (whence) {
685 case SEEK_CUR:
686 offset += file->f_pos;
687 break;
688 case SEEK_SET:
689 break;
690 case SEEK_END:
691 retval = -EOPNOTSUPP;
692 goto out;
693 default:
694 goto out;
695 }
696
697 if (offset >= 0) {
698 if (need_reset_readdir(dfi, offset)) {
699 doutc(cl, "%p %llx.%llx dropping %p content\n",
700 inode, ceph_vinop(inode), file);
701 reset_readdir(dfi);
702 } else if (is_hash_order(offset) && offset > file->f_pos) {
703 /* for hash offset, we don't know if a forward seek
704 * is within same frag */
705 dfi->dir_release_count = 0;
706 dfi->readdir_cache_idx = -1;
707 }
708
709 if (offset != file->f_pos) {
710 file->f_pos = offset;
711 dfi->file_info.flags &= ~CEPH_F_ATEND;
712 }
713 retval = offset;
714 }
715 out:
716 inode_unlock(inode);
717 return retval;
718 }
719
720 /*
721 * Handle lookups for the hidden .snap directory.
722 */
ceph_handle_snapdir(struct ceph_mds_request * req,struct dentry * dentry)723 struct dentry *ceph_handle_snapdir(struct ceph_mds_request *req,
724 struct dentry *dentry)
725 {
726 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dentry->d_sb);
727 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_rwsem */
728 struct ceph_client *cl = ceph_inode_to_client(parent);
729
730 /* .snap dir? */
731 if (ceph_snap(parent) == CEPH_NOSNAP &&
732 strcmp(dentry->d_name.name, fsc->mount_options->snapdir_name) == 0) {
733 struct dentry *res;
734 struct inode *inode = ceph_get_snapdir(parent);
735
736 res = d_splice_alias(inode, dentry);
737 doutc(cl, "ENOENT on snapdir %p '%pd', linking to "
738 "snapdir %p %llx.%llx. Spliced dentry %p\n",
739 dentry, dentry, inode, ceph_vinop(inode), res);
740 if (res)
741 dentry = res;
742 }
743 return dentry;
744 }
745
746 /*
747 * Figure out final result of a lookup/open request.
748 *
749 * Mainly, make sure we return the final req->r_dentry (if it already
750 * existed) in place of the original VFS-provided dentry when they
751 * differ.
752 *
753 * Gracefully handle the case where the MDS replies with -ENOENT and
754 * no trace (which it may do, at its discretion, e.g., if it doesn't
755 * care to issue a lease on the negative dentry).
756 */
ceph_finish_lookup(struct ceph_mds_request * req,struct dentry * dentry,int err)757 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
758 struct dentry *dentry, int err)
759 {
760 struct ceph_client *cl = req->r_mdsc->fsc->client;
761
762 if (err == -ENOENT) {
763 /* no trace? */
764 err = 0;
765 if (!req->r_reply_info.head->is_dentry) {
766 doutc(cl,
767 "ENOENT and no trace, dentry %p inode %llx.%llx\n",
768 dentry, ceph_vinop(d_inode(dentry)));
769 if (d_really_is_positive(dentry)) {
770 d_drop(dentry);
771 err = -ENOENT;
772 } else {
773 d_add(dentry, NULL);
774 }
775 }
776 }
777 if (err)
778 dentry = ERR_PTR(err);
779 else if (dentry != req->r_dentry)
780 dentry = dget(req->r_dentry); /* we got spliced */
781 else
782 dentry = NULL;
783 return dentry;
784 }
785
is_root_ceph_dentry(struct inode * inode,struct dentry * dentry)786 static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
787 {
788 return ceph_ino(inode) == CEPH_INO_ROOT &&
789 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
790 }
791
792 /*
793 * Look up a single dir entry. If there is a lookup intent, inform
794 * the MDS so that it gets our 'caps wanted' value in a single op.
795 */
ceph_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)796 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
797 unsigned int flags)
798 {
799 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb);
800 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
801 struct ceph_client *cl = fsc->client;
802 struct ceph_mds_request *req;
803 int op;
804 int mask;
805 int err;
806
807 doutc(cl, "%p %llx.%llx/'%pd' dentry %p\n", dir, ceph_vinop(dir),
808 dentry, dentry);
809
810 if (dentry->d_name.len > NAME_MAX)
811 return ERR_PTR(-ENAMETOOLONG);
812
813 if (IS_ENCRYPTED(dir)) {
814 bool had_key = fscrypt_has_encryption_key(dir);
815
816 err = fscrypt_prepare_lookup_partial(dir, dentry);
817 if (err < 0)
818 return ERR_PTR(err);
819
820 /* mark directory as incomplete if it has been unlocked */
821 if (!had_key && fscrypt_has_encryption_key(dir))
822 ceph_dir_clear_complete(dir);
823 }
824
825 /* can we conclude ENOENT locally? */
826 if (d_really_is_negative(dentry)) {
827 struct ceph_inode_info *ci = ceph_inode(dir);
828 struct ceph_dentry_info *di = ceph_dentry(dentry);
829
830 spin_lock(&ci->i_ceph_lock);
831 doutc(cl, " dir %llx.%llx flags are 0x%lx\n",
832 ceph_vinop(dir), ci->i_ceph_flags);
833 if (strncmp(dentry->d_name.name,
834 fsc->mount_options->snapdir_name,
835 dentry->d_name.len) &&
836 !is_root_ceph_dentry(dir, dentry) &&
837 ceph_test_mount_opt(fsc, DCACHE) &&
838 __ceph_dir_is_complete(ci) &&
839 __ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) {
840 __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD);
841 spin_unlock(&ci->i_ceph_lock);
842 doutc(cl, " dir %llx.%llx complete, -ENOENT\n",
843 ceph_vinop(dir));
844 d_add(dentry, NULL);
845 di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
846 return NULL;
847 }
848 spin_unlock(&ci->i_ceph_lock);
849 }
850
851 op = ceph_snap(dir) == CEPH_SNAPDIR ?
852 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
853 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
854 if (IS_ERR(req))
855 return ERR_CAST(req);
856 req->r_dentry = dget(dentry);
857 req->r_num_caps = 2;
858
859 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
860 if (ceph_security_xattr_wanted(dir))
861 mask |= CEPH_CAP_XATTR_SHARED;
862 req->r_args.getattr.mask = cpu_to_le32(mask);
863
864 ihold(dir);
865 req->r_parent = dir;
866 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
867 err = ceph_mdsc_do_request(mdsc, NULL, req);
868 if (err == -ENOENT) {
869 struct dentry *res;
870
871 res = ceph_handle_snapdir(req, dentry);
872 if (IS_ERR(res)) {
873 err = PTR_ERR(res);
874 } else {
875 dentry = res;
876 err = 0;
877 }
878 }
879 dentry = ceph_finish_lookup(req, dentry, err);
880 ceph_mdsc_put_request(req); /* will dput(dentry) */
881 doutc(cl, "result=%p\n", dentry);
882 return dentry;
883 }
884
885 /*
886 * If we do a create but get no trace back from the MDS, follow up with
887 * a lookup (the VFS expects us to link up the provided dentry).
888 */
ceph_handle_notrace_create(struct inode * dir,struct dentry * dentry)889 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
890 {
891 struct dentry *result = ceph_lookup(dir, dentry, 0);
892
893 if (result && !IS_ERR(result)) {
894 /*
895 * We created the item, then did a lookup, and found
896 * it was already linked to another inode we already
897 * had in our cache (and thus got spliced). To not
898 * confuse VFS (especially when inode is a directory),
899 * we don't link our dentry to that inode, return an
900 * error instead.
901 *
902 * This event should be rare and it happens only when
903 * we talk to old MDS. Recent MDS does not send traceless
904 * reply for request that creates new inode.
905 */
906 d_drop(result);
907 return -ESTALE;
908 }
909 return PTR_ERR(result);
910 }
911
ceph_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)912 static int ceph_mknod(struct mnt_idmap *idmap, struct inode *dir,
913 struct dentry *dentry, umode_t mode, dev_t rdev)
914 {
915 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
916 struct ceph_client *cl = mdsc->fsc->client;
917 struct ceph_mds_request *req;
918 struct ceph_acl_sec_ctx as_ctx = {};
919 int err;
920
921 if (ceph_snap(dir) != CEPH_NOSNAP)
922 return -EROFS;
923
924 err = ceph_wait_on_conflict_unlink(dentry);
925 if (err)
926 return err;
927
928 if (ceph_quota_is_max_files_exceeded(dir)) {
929 err = -EDQUOT;
930 goto out;
931 }
932
933 doutc(cl, "%p %llx.%llx/'%pd' dentry %p mode 0%ho rdev %d\n",
934 dir, ceph_vinop(dir), dentry, dentry, mode, rdev);
935 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
936 if (IS_ERR(req)) {
937 err = PTR_ERR(req);
938 goto out;
939 }
940
941 req->r_new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx);
942 if (IS_ERR(req->r_new_inode)) {
943 err = PTR_ERR(req->r_new_inode);
944 req->r_new_inode = NULL;
945 goto out_req;
946 }
947
948 if (S_ISREG(mode) && IS_ENCRYPTED(dir))
949 set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
950
951 req->r_dentry = dget(dentry);
952 req->r_num_caps = 2;
953 req->r_parent = dir;
954 ihold(dir);
955 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
956 req->r_mnt_idmap = mnt_idmap_get(idmap);
957 req->r_args.mknod.mode = cpu_to_le32(mode);
958 req->r_args.mknod.rdev = cpu_to_le32(rdev);
959 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL |
960 CEPH_CAP_XATTR_EXCL;
961 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
962
963 ceph_as_ctx_to_req(req, &as_ctx);
964
965 err = ceph_mdsc_do_request(mdsc, dir, req);
966 if (!err && !req->r_reply_info.head->is_dentry)
967 err = ceph_handle_notrace_create(dir, dentry);
968 out_req:
969 ceph_mdsc_put_request(req);
970 out:
971 if (!err)
972 ceph_init_inode_acls(d_inode(dentry), &as_ctx);
973 else
974 d_drop(dentry);
975 ceph_release_acl_sec_ctx(&as_ctx);
976 return err;
977 }
978
ceph_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)979 static int ceph_create(struct mnt_idmap *idmap, struct inode *dir,
980 struct dentry *dentry, umode_t mode, bool excl)
981 {
982 return ceph_mknod(idmap, dir, dentry, mode, 0);
983 }
984
985 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
prep_encrypted_symlink_target(struct ceph_mds_request * req,const char * dest)986 static int prep_encrypted_symlink_target(struct ceph_mds_request *req,
987 const char *dest)
988 {
989 int err;
990 int len = strlen(dest);
991 struct fscrypt_str osd_link = FSTR_INIT(NULL, 0);
992
993 err = fscrypt_prepare_symlink(req->r_parent, dest, len, PATH_MAX,
994 &osd_link);
995 if (err)
996 goto out;
997
998 err = fscrypt_encrypt_symlink(req->r_new_inode, dest, len, &osd_link);
999 if (err)
1000 goto out;
1001
1002 req->r_path2 = kmalloc(CEPH_BASE64_CHARS(osd_link.len) + 1, GFP_KERNEL);
1003 if (!req->r_path2) {
1004 err = -ENOMEM;
1005 goto out;
1006 }
1007
1008 len = ceph_base64_encode(osd_link.name, osd_link.len, req->r_path2);
1009 req->r_path2[len] = '\0';
1010 out:
1011 fscrypt_fname_free_buffer(&osd_link);
1012 return err;
1013 }
1014 #else
prep_encrypted_symlink_target(struct ceph_mds_request * req,const char * dest)1015 static int prep_encrypted_symlink_target(struct ceph_mds_request *req,
1016 const char *dest)
1017 {
1018 return -EOPNOTSUPP;
1019 }
1020 #endif
1021
ceph_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * dest)1022 static int ceph_symlink(struct mnt_idmap *idmap, struct inode *dir,
1023 struct dentry *dentry, const char *dest)
1024 {
1025 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
1026 struct ceph_client *cl = mdsc->fsc->client;
1027 struct ceph_mds_request *req;
1028 struct ceph_acl_sec_ctx as_ctx = {};
1029 umode_t mode = S_IFLNK | 0777;
1030 int err;
1031
1032 if (ceph_snap(dir) != CEPH_NOSNAP)
1033 return -EROFS;
1034
1035 err = ceph_wait_on_conflict_unlink(dentry);
1036 if (err)
1037 return err;
1038
1039 if (ceph_quota_is_max_files_exceeded(dir)) {
1040 err = -EDQUOT;
1041 goto out;
1042 }
1043
1044 doutc(cl, "%p %llx.%llx/'%pd' to '%s'\n", dir, ceph_vinop(dir), dentry,
1045 dest);
1046 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
1047 if (IS_ERR(req)) {
1048 err = PTR_ERR(req);
1049 goto out;
1050 }
1051
1052 req->r_new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx);
1053 if (IS_ERR(req->r_new_inode)) {
1054 err = PTR_ERR(req->r_new_inode);
1055 req->r_new_inode = NULL;
1056 goto out_req;
1057 }
1058
1059 req->r_parent = dir;
1060 ihold(dir);
1061
1062 if (IS_ENCRYPTED(req->r_new_inode)) {
1063 err = prep_encrypted_symlink_target(req, dest);
1064 if (err)
1065 goto out_req;
1066 } else {
1067 req->r_path2 = kstrdup(dest, GFP_KERNEL);
1068 if (!req->r_path2) {
1069 err = -ENOMEM;
1070 goto out_req;
1071 }
1072 }
1073
1074 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1075 req->r_mnt_idmap = mnt_idmap_get(idmap);
1076 req->r_dentry = dget(dentry);
1077 req->r_num_caps = 2;
1078 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL |
1079 CEPH_CAP_XATTR_EXCL;
1080 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1081
1082 ceph_as_ctx_to_req(req, &as_ctx);
1083
1084 err = ceph_mdsc_do_request(mdsc, dir, req);
1085 if (!err && !req->r_reply_info.head->is_dentry)
1086 err = ceph_handle_notrace_create(dir, dentry);
1087 out_req:
1088 ceph_mdsc_put_request(req);
1089 out:
1090 if (err)
1091 d_drop(dentry);
1092 ceph_release_acl_sec_ctx(&as_ctx);
1093 return err;
1094 }
1095
ceph_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)1096 static struct dentry *ceph_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1097 struct dentry *dentry, umode_t mode)
1098 {
1099 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
1100 struct ceph_client *cl = mdsc->fsc->client;
1101 struct ceph_mds_request *req;
1102 struct ceph_acl_sec_ctx as_ctx = {};
1103 struct dentry *ret;
1104 int err;
1105 int op;
1106
1107 err = ceph_wait_on_conflict_unlink(dentry);
1108 if (err)
1109 return ERR_PTR(err);
1110
1111 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1112 /* mkdir .snap/foo is a MKSNAP */
1113 op = CEPH_MDS_OP_MKSNAP;
1114 doutc(cl, "mksnap %llx.%llx/'%pd' dentry %p\n",
1115 ceph_vinop(dir), dentry, dentry);
1116 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1117 doutc(cl, "mkdir %llx.%llx/'%pd' dentry %p mode 0%ho\n",
1118 ceph_vinop(dir), dentry, dentry, mode);
1119 op = CEPH_MDS_OP_MKDIR;
1120 } else {
1121 ret = ERR_PTR(-EROFS);
1122 goto out;
1123 }
1124
1125 if (op == CEPH_MDS_OP_MKDIR &&
1126 ceph_quota_is_max_files_exceeded(dir)) {
1127 ret = ERR_PTR(-EDQUOT);
1128 goto out;
1129 }
1130 if ((op == CEPH_MDS_OP_MKSNAP) && IS_ENCRYPTED(dir) &&
1131 !fscrypt_has_encryption_key(dir)) {
1132 ret = ERR_PTR(-ENOKEY);
1133 goto out;
1134 }
1135
1136
1137 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1138 if (IS_ERR(req)) {
1139 ret = ERR_CAST(req);
1140 goto out;
1141 }
1142
1143 mode |= S_IFDIR;
1144 req->r_new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx);
1145 if (IS_ERR(req->r_new_inode)) {
1146 ret = ERR_CAST(req->r_new_inode);
1147 req->r_new_inode = NULL;
1148 goto out_req;
1149 }
1150
1151 req->r_dentry = dget(dentry);
1152 req->r_num_caps = 2;
1153 req->r_parent = dir;
1154 ihold(dir);
1155 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1156 if (op == CEPH_MDS_OP_MKDIR)
1157 req->r_mnt_idmap = mnt_idmap_get(idmap);
1158 req->r_args.mkdir.mode = cpu_to_le32(mode);
1159 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL |
1160 CEPH_CAP_XATTR_EXCL;
1161 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1162
1163 ceph_as_ctx_to_req(req, &as_ctx);
1164
1165 err = ceph_mdsc_do_request(mdsc, dir, req);
1166 if (!err &&
1167 !req->r_reply_info.head->is_target &&
1168 !req->r_reply_info.head->is_dentry)
1169 err = ceph_handle_notrace_create(dir, dentry);
1170 ret = ERR_PTR(err);
1171 out_req:
1172 if (!IS_ERR(ret) && req->r_dentry != dentry)
1173 /* Some other dentry was spliced in */
1174 ret = dget(req->r_dentry);
1175 ceph_mdsc_put_request(req);
1176 out:
1177 if (!IS_ERR(ret)) {
1178 if (ret)
1179 dentry = ret;
1180 ceph_init_inode_acls(d_inode(dentry), &as_ctx);
1181 } else {
1182 d_drop(dentry);
1183 }
1184 ceph_release_acl_sec_ctx(&as_ctx);
1185 return ret;
1186 }
1187
ceph_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)1188 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
1189 struct dentry *dentry)
1190 {
1191 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
1192 struct ceph_client *cl = mdsc->fsc->client;
1193 struct ceph_mds_request *req;
1194 int err;
1195
1196 if (dentry->d_flags & DCACHE_DISCONNECTED)
1197 return -EINVAL;
1198
1199 err = ceph_wait_on_conflict_unlink(dentry);
1200 if (err)
1201 return err;
1202
1203 if (ceph_snap(dir) != CEPH_NOSNAP)
1204 return -EROFS;
1205
1206 err = fscrypt_prepare_link(old_dentry, dir, dentry);
1207 if (err)
1208 return err;
1209
1210 doutc(cl, "%p %llx.%llx/'%pd' to '%pd'\n", dir, ceph_vinop(dir),
1211 old_dentry, dentry);
1212 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
1213 if (IS_ERR(req)) {
1214 d_drop(dentry);
1215 return PTR_ERR(req);
1216 }
1217 req->r_dentry = dget(dentry);
1218 req->r_num_caps = 2;
1219 req->r_old_dentry = dget(old_dentry);
1220 /*
1221 * The old_dentry maybe a DCACHE_DISCONNECTED dentry, then we
1222 * will just pass the ino# to MDSs.
1223 */
1224 if (old_dentry->d_flags & DCACHE_DISCONNECTED)
1225 req->r_ino2 = ceph_vino(d_inode(old_dentry));
1226 req->r_parent = dir;
1227 ihold(dir);
1228 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1229 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL;
1230 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1231 /* release LINK_SHARED on source inode (mds will lock it) */
1232 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1233 err = ceph_mdsc_do_request(mdsc, dir, req);
1234 if (err) {
1235 d_drop(dentry);
1236 } else if (!req->r_reply_info.head->is_dentry) {
1237 ihold(d_inode(old_dentry));
1238 d_instantiate(dentry, d_inode(old_dentry));
1239 }
1240 ceph_mdsc_put_request(req);
1241 return err;
1242 }
1243
ceph_async_unlink_cb(struct ceph_mds_client * mdsc,struct ceph_mds_request * req)1244 static void ceph_async_unlink_cb(struct ceph_mds_client *mdsc,
1245 struct ceph_mds_request *req)
1246 {
1247 struct dentry *dentry = req->r_dentry;
1248 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dentry->d_sb);
1249 struct ceph_client *cl = fsc->client;
1250 struct ceph_dentry_info *di = ceph_dentry(dentry);
1251 int result = req->r_err ? req->r_err :
1252 le32_to_cpu(req->r_reply_info.head->result);
1253
1254 if (!test_bit(CEPH_DENTRY_ASYNC_UNLINK_BIT, &di->flags))
1255 pr_warn_client(cl,
1256 "dentry %p:%pd async unlink bit is not set\n",
1257 dentry, dentry);
1258
1259 spin_lock(&fsc->async_unlink_conflict_lock);
1260 hash_del_rcu(&di->hnode);
1261 spin_unlock(&fsc->async_unlink_conflict_lock);
1262
1263 spin_lock(&dentry->d_lock);
1264 di->flags &= ~CEPH_DENTRY_ASYNC_UNLINK;
1265 wake_up_bit(&di->flags, CEPH_DENTRY_ASYNC_UNLINK_BIT);
1266 spin_unlock(&dentry->d_lock);
1267
1268 synchronize_rcu();
1269
1270 if (result == -EJUKEBOX)
1271 goto out;
1272
1273 /* If op failed, mark everyone involved for errors */
1274 if (result) {
1275 int pathlen = 0;
1276 u64 base = 0;
1277 char *path = ceph_mdsc_build_path(mdsc, dentry, &pathlen,
1278 &base, 0);
1279
1280 /* mark error on parent + clear complete */
1281 mapping_set_error(req->r_parent->i_mapping, result);
1282 ceph_dir_clear_complete(req->r_parent);
1283
1284 /* drop the dentry -- we don't know its status */
1285 if (!d_unhashed(dentry))
1286 d_drop(dentry);
1287
1288 /* mark inode itself for an error (since metadata is bogus) */
1289 mapping_set_error(req->r_old_inode->i_mapping, result);
1290
1291 pr_warn_client(cl, "failure path=(%llx)%s result=%d!\n",
1292 base, IS_ERR(path) ? "<<bad>>" : path, result);
1293 ceph_mdsc_free_path(path, pathlen);
1294 }
1295 out:
1296 iput(req->r_old_inode);
1297 ceph_mdsc_release_dir_caps(req);
1298 }
1299
get_caps_for_async_unlink(struct inode * dir,struct dentry * dentry)1300 static int get_caps_for_async_unlink(struct inode *dir, struct dentry *dentry)
1301 {
1302 struct ceph_inode_info *ci = ceph_inode(dir);
1303 struct ceph_dentry_info *di;
1304 int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_UNLINK;
1305
1306 spin_lock(&ci->i_ceph_lock);
1307 if ((__ceph_caps_issued(ci, NULL) & want) == want) {
1308 ceph_take_cap_refs(ci, want, false);
1309 got = want;
1310 }
1311 spin_unlock(&ci->i_ceph_lock);
1312
1313 /* If we didn't get anything, return 0 */
1314 if (!got)
1315 return 0;
1316
1317 spin_lock(&dentry->d_lock);
1318 di = ceph_dentry(dentry);
1319 /*
1320 * - We are holding Fx, which implies Fs caps.
1321 * - Only support async unlink for primary linkage
1322 */
1323 if (atomic_read(&ci->i_shared_gen) != di->lease_shared_gen ||
1324 !(di->flags & CEPH_DENTRY_PRIMARY_LINK))
1325 want = 0;
1326 spin_unlock(&dentry->d_lock);
1327
1328 /* Do we still want what we've got? */
1329 if (want == got)
1330 return got;
1331
1332 ceph_put_cap_refs(ci, got);
1333 return 0;
1334 }
1335
1336 /*
1337 * rmdir and unlink are differ only by the metadata op code
1338 */
ceph_unlink(struct inode * dir,struct dentry * dentry)1339 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1340 {
1341 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb);
1342 struct ceph_client *cl = fsc->client;
1343 struct ceph_mds_client *mdsc = fsc->mdsc;
1344 struct inode *inode = d_inode(dentry);
1345 struct ceph_mds_request *req;
1346 bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
1347 struct dentry *dn;
1348 int err = -EROFS;
1349 int op;
1350 char *path;
1351 int pathlen;
1352 u64 pathbase;
1353
1354 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1355 /* rmdir .snap/foo is RMSNAP */
1356 doutc(cl, "rmsnap %llx.%llx/'%pd' dn\n", ceph_vinop(dir),
1357 dentry);
1358 op = CEPH_MDS_OP_RMSNAP;
1359 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1360 doutc(cl, "unlink/rmdir %llx.%llx/'%pd' inode %llx.%llx\n",
1361 ceph_vinop(dir), dentry, ceph_vinop(inode));
1362 op = d_is_dir(dentry) ?
1363 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1364 } else
1365 goto out;
1366
1367 dn = d_find_alias(dir);
1368 if (!dn) {
1369 try_async = false;
1370 } else {
1371 path = ceph_mdsc_build_path(mdsc, dn, &pathlen, &pathbase, 0);
1372 if (IS_ERR(path)) {
1373 try_async = false;
1374 err = 0;
1375 } else {
1376 err = ceph_mds_check_access(mdsc, path, MAY_WRITE);
1377 }
1378 ceph_mdsc_free_path(path, pathlen);
1379 dput(dn);
1380
1381 /* For none EACCES cases will let the MDS do the mds auth check */
1382 if (err == -EACCES) {
1383 return err;
1384 } else if (err < 0) {
1385 try_async = false;
1386 err = 0;
1387 }
1388 }
1389
1390 retry:
1391 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1392 if (IS_ERR(req)) {
1393 err = PTR_ERR(req);
1394 goto out;
1395 }
1396 req->r_dentry = dget(dentry);
1397 req->r_num_caps = 2;
1398 req->r_parent = dir;
1399 ihold(dir);
1400 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL;
1401 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1402 req->r_inode_drop = ceph_drop_caps_for_unlink(inode);
1403
1404 if (try_async && op == CEPH_MDS_OP_UNLINK &&
1405 (req->r_dir_caps = get_caps_for_async_unlink(dir, dentry))) {
1406 struct ceph_dentry_info *di = ceph_dentry(dentry);
1407
1408 doutc(cl, "async unlink on %llx.%llx/'%pd' caps=%s",
1409 ceph_vinop(dir), dentry,
1410 ceph_cap_string(req->r_dir_caps));
1411 set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
1412 req->r_callback = ceph_async_unlink_cb;
1413 req->r_old_inode = d_inode(dentry);
1414 ihold(req->r_old_inode);
1415
1416 spin_lock(&dentry->d_lock);
1417 di->flags |= CEPH_DENTRY_ASYNC_UNLINK;
1418 spin_unlock(&dentry->d_lock);
1419
1420 spin_lock(&fsc->async_unlink_conflict_lock);
1421 hash_add_rcu(fsc->async_unlink_conflict, &di->hnode,
1422 dentry->d_name.hash);
1423 spin_unlock(&fsc->async_unlink_conflict_lock);
1424
1425 err = ceph_mdsc_submit_request(mdsc, dir, req);
1426 if (!err) {
1427 /*
1428 * We have enough caps, so we assume that the unlink
1429 * will succeed. Fix up the target inode and dcache.
1430 */
1431 drop_nlink(inode);
1432 d_delete(dentry);
1433 } else {
1434 spin_lock(&fsc->async_unlink_conflict_lock);
1435 hash_del_rcu(&di->hnode);
1436 spin_unlock(&fsc->async_unlink_conflict_lock);
1437
1438 spin_lock(&dentry->d_lock);
1439 di->flags &= ~CEPH_DENTRY_ASYNC_UNLINK;
1440 spin_unlock(&dentry->d_lock);
1441
1442 if (err == -EJUKEBOX) {
1443 try_async = false;
1444 ceph_mdsc_put_request(req);
1445 goto retry;
1446 }
1447 }
1448 } else {
1449 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1450 err = ceph_mdsc_do_request(mdsc, dir, req);
1451 if (!err && !req->r_reply_info.head->is_dentry)
1452 d_delete(dentry);
1453 }
1454
1455 ceph_mdsc_put_request(req);
1456 out:
1457 return err;
1458 }
1459
ceph_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1460 static int ceph_rename(struct mnt_idmap *idmap, struct inode *old_dir,
1461 struct dentry *old_dentry, struct inode *new_dir,
1462 struct dentry *new_dentry, unsigned int flags)
1463 {
1464 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(old_dir->i_sb);
1465 struct ceph_client *cl = mdsc->fsc->client;
1466 struct ceph_mds_request *req;
1467 int op = CEPH_MDS_OP_RENAME;
1468 int err;
1469
1470 if (flags)
1471 return -EINVAL;
1472
1473 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1474 return -EXDEV;
1475 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1476 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1477 op = CEPH_MDS_OP_RENAMESNAP;
1478 else
1479 return -EROFS;
1480 }
1481 /* don't allow cross-quota renames */
1482 if ((old_dir != new_dir) &&
1483 (!ceph_quota_is_same_realm(old_dir, new_dir)))
1484 return -EXDEV;
1485
1486 err = ceph_wait_on_conflict_unlink(new_dentry);
1487 if (err)
1488 return err;
1489
1490 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1491 flags);
1492 if (err)
1493 return err;
1494
1495 doutc(cl, "%llx.%llx/'%pd' to %llx.%llx/'%pd'\n",
1496 ceph_vinop(old_dir), old_dentry, ceph_vinop(new_dir),
1497 new_dentry);
1498 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1499 if (IS_ERR(req))
1500 return PTR_ERR(req);
1501 ihold(old_dir);
1502 req->r_dentry = dget(new_dentry);
1503 req->r_num_caps = 2;
1504 req->r_old_dentry = dget(old_dentry);
1505 req->r_old_dentry_dir = old_dir;
1506 req->r_parent = new_dir;
1507 ihold(new_dir);
1508 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1509 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL;
1510 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1511 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_XATTR_EXCL;
1512 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1513 /* release LINK_RDCACHE on source inode (mds will lock it) */
1514 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1515 if (d_really_is_positive(new_dentry)) {
1516 req->r_inode_drop =
1517 ceph_drop_caps_for_unlink(d_inode(new_dentry));
1518 }
1519 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1520 if (!err && !req->r_reply_info.head->is_dentry) {
1521 /*
1522 * Normally d_move() is done by fill_trace (called by
1523 * do_request, above). If there is no trace, we need
1524 * to do it here.
1525 */
1526 d_move(old_dentry, new_dentry);
1527 }
1528 ceph_mdsc_put_request(req);
1529 return err;
1530 }
1531
1532 /*
1533 * Move dentry to tail of mdsc->dentry_leases list when lease is updated.
1534 * Leases at front of the list will expire first. (Assume all leases have
1535 * similar duration)
1536 *
1537 * Called under dentry->d_lock.
1538 */
__ceph_dentry_lease_touch(struct ceph_dentry_info * di)1539 void __ceph_dentry_lease_touch(struct ceph_dentry_info *di)
1540 {
1541 struct dentry *dn = di->dentry;
1542 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dn->d_sb)->mdsc;
1543 struct ceph_client *cl = mdsc->fsc->client;
1544
1545 doutc(cl, "%p %p '%pd'\n", di, dn, dn);
1546
1547 di->flags |= CEPH_DENTRY_LEASE_LIST;
1548 if (di->flags & CEPH_DENTRY_SHRINK_LIST) {
1549 di->flags |= CEPH_DENTRY_REFERENCED;
1550 return;
1551 }
1552
1553 spin_lock(&mdsc->dentry_list_lock);
1554 list_move_tail(&di->lease_list, &mdsc->dentry_leases);
1555 spin_unlock(&mdsc->dentry_list_lock);
1556 }
1557
__dentry_dir_lease_touch(struct ceph_mds_client * mdsc,struct ceph_dentry_info * di)1558 static void __dentry_dir_lease_touch(struct ceph_mds_client* mdsc,
1559 struct ceph_dentry_info *di)
1560 {
1561 di->flags &= ~(CEPH_DENTRY_LEASE_LIST | CEPH_DENTRY_REFERENCED);
1562 di->lease_gen = 0;
1563 di->time = jiffies;
1564 list_move_tail(&di->lease_list, &mdsc->dentry_dir_leases);
1565 }
1566
1567 /*
1568 * When dir lease is used, add dentry to tail of mdsc->dentry_dir_leases
1569 * list if it's not in the list, otherwise set 'referenced' flag.
1570 *
1571 * Called under dentry->d_lock.
1572 */
__ceph_dentry_dir_lease_touch(struct ceph_dentry_info * di)1573 void __ceph_dentry_dir_lease_touch(struct ceph_dentry_info *di)
1574 {
1575 struct dentry *dn = di->dentry;
1576 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dn->d_sb)->mdsc;
1577 struct ceph_client *cl = mdsc->fsc->client;
1578
1579 doutc(cl, "%p %p '%pd' (offset 0x%llx)\n", di, dn, dn, di->offset);
1580
1581 if (!list_empty(&di->lease_list)) {
1582 if (di->flags & CEPH_DENTRY_LEASE_LIST) {
1583 /* don't remove dentry from dentry lease list
1584 * if its lease is valid */
1585 if (__dentry_lease_is_valid(di))
1586 return;
1587 } else {
1588 di->flags |= CEPH_DENTRY_REFERENCED;
1589 return;
1590 }
1591 }
1592
1593 if (di->flags & CEPH_DENTRY_SHRINK_LIST) {
1594 di->flags |= CEPH_DENTRY_REFERENCED;
1595 di->flags &= ~CEPH_DENTRY_LEASE_LIST;
1596 return;
1597 }
1598
1599 spin_lock(&mdsc->dentry_list_lock);
1600 __dentry_dir_lease_touch(mdsc, di);
1601 spin_unlock(&mdsc->dentry_list_lock);
1602 }
1603
__dentry_lease_unlist(struct ceph_dentry_info * di)1604 static void __dentry_lease_unlist(struct ceph_dentry_info *di)
1605 {
1606 struct ceph_mds_client *mdsc;
1607 if (di->flags & CEPH_DENTRY_SHRINK_LIST)
1608 return;
1609 if (list_empty(&di->lease_list))
1610 return;
1611
1612 mdsc = ceph_sb_to_fs_client(di->dentry->d_sb)->mdsc;
1613 spin_lock(&mdsc->dentry_list_lock);
1614 list_del_init(&di->lease_list);
1615 spin_unlock(&mdsc->dentry_list_lock);
1616 }
1617
1618 enum {
1619 KEEP = 0,
1620 DELETE = 1,
1621 TOUCH = 2,
1622 STOP = 4,
1623 };
1624
1625 struct ceph_lease_walk_control {
1626 bool dir_lease;
1627 bool expire_dir_lease;
1628 unsigned long nr_to_scan;
1629 unsigned long dir_lease_ttl;
1630 };
1631
1632 static int __dir_lease_check(const struct dentry *, struct ceph_lease_walk_control *);
1633 static int __dentry_lease_check(const struct dentry *);
1634
1635 static unsigned long
__dentry_leases_walk(struct ceph_mds_client * mdsc,struct ceph_lease_walk_control * lwc)1636 __dentry_leases_walk(struct ceph_mds_client *mdsc,
1637 struct ceph_lease_walk_control *lwc)
1638 {
1639 struct ceph_dentry_info *di, *tmp;
1640 struct dentry *dentry, *last = NULL;
1641 struct list_head* list;
1642 LIST_HEAD(dispose);
1643 unsigned long freed = 0;
1644 int ret = 0;
1645
1646 list = lwc->dir_lease ? &mdsc->dentry_dir_leases : &mdsc->dentry_leases;
1647 spin_lock(&mdsc->dentry_list_lock);
1648 list_for_each_entry_safe(di, tmp, list, lease_list) {
1649 if (!lwc->nr_to_scan)
1650 break;
1651 --lwc->nr_to_scan;
1652
1653 dentry = di->dentry;
1654 if (last == dentry)
1655 break;
1656
1657 if (!spin_trylock(&dentry->d_lock))
1658 continue;
1659
1660 if (__lockref_is_dead(&dentry->d_lockref)) {
1661 list_del_init(&di->lease_list);
1662 goto next;
1663 }
1664
1665 if (lwc->dir_lease)
1666 ret = __dir_lease_check(dentry, lwc);
1667 else
1668 ret = __dentry_lease_check(dentry);
1669 if (ret & TOUCH) {
1670 /* move it into tail of dir lease list */
1671 __dentry_dir_lease_touch(mdsc, di);
1672 if (!last)
1673 last = dentry;
1674 }
1675 if (ret & DELETE) {
1676 /* stale lease */
1677 di->flags &= ~CEPH_DENTRY_REFERENCED;
1678 if (dentry->d_lockref.count > 0) {
1679 /* update_dentry_lease() will re-add
1680 * it to lease list, or
1681 * ceph_d_delete() will return 1 when
1682 * last reference is dropped */
1683 list_del_init(&di->lease_list);
1684 } else {
1685 di->flags |= CEPH_DENTRY_SHRINK_LIST;
1686 list_move_tail(&di->lease_list, &dispose);
1687 dget_dlock(dentry);
1688 }
1689 }
1690 next:
1691 spin_unlock(&dentry->d_lock);
1692 if (ret & STOP)
1693 break;
1694 }
1695 spin_unlock(&mdsc->dentry_list_lock);
1696
1697 while (!list_empty(&dispose)) {
1698 di = list_first_entry(&dispose, struct ceph_dentry_info,
1699 lease_list);
1700 dentry = di->dentry;
1701 spin_lock(&dentry->d_lock);
1702
1703 list_del_init(&di->lease_list);
1704 di->flags &= ~CEPH_DENTRY_SHRINK_LIST;
1705 if (di->flags & CEPH_DENTRY_REFERENCED) {
1706 spin_lock(&mdsc->dentry_list_lock);
1707 if (di->flags & CEPH_DENTRY_LEASE_LIST) {
1708 list_add_tail(&di->lease_list,
1709 &mdsc->dentry_leases);
1710 } else {
1711 __dentry_dir_lease_touch(mdsc, di);
1712 }
1713 spin_unlock(&mdsc->dentry_list_lock);
1714 } else {
1715 freed++;
1716 }
1717
1718 spin_unlock(&dentry->d_lock);
1719 /* ceph_d_delete() does the trick */
1720 dput(dentry);
1721 }
1722 return freed;
1723 }
1724
__dentry_lease_check(const struct dentry * dentry)1725 static int __dentry_lease_check(const struct dentry *dentry)
1726 {
1727 struct ceph_dentry_info *di = ceph_dentry(dentry);
1728 int ret;
1729
1730 if (__dentry_lease_is_valid(di))
1731 return STOP;
1732 ret = __dir_lease_try_check(dentry);
1733 if (ret == -EBUSY)
1734 return KEEP;
1735 if (ret > 0)
1736 return TOUCH;
1737 return DELETE;
1738 }
1739
__dir_lease_check(const struct dentry * dentry,struct ceph_lease_walk_control * lwc)1740 static int __dir_lease_check(const struct dentry *dentry,
1741 struct ceph_lease_walk_control *lwc)
1742 {
1743 struct ceph_dentry_info *di = ceph_dentry(dentry);
1744
1745 int ret = __dir_lease_try_check(dentry);
1746 if (ret == -EBUSY)
1747 return KEEP;
1748 if (ret > 0) {
1749 if (time_before(jiffies, di->time + lwc->dir_lease_ttl))
1750 return STOP;
1751 /* Move dentry to tail of dir lease list if we don't want
1752 * to delete it. So dentries in the list are checked in a
1753 * round robin manner */
1754 if (!lwc->expire_dir_lease)
1755 return TOUCH;
1756 if (dentry->d_lockref.count > 0 ||
1757 (di->flags & CEPH_DENTRY_REFERENCED))
1758 return TOUCH;
1759 /* invalidate dir lease */
1760 di->lease_shared_gen = 0;
1761 }
1762 return DELETE;
1763 }
1764
ceph_trim_dentries(struct ceph_mds_client * mdsc)1765 int ceph_trim_dentries(struct ceph_mds_client *mdsc)
1766 {
1767 struct ceph_lease_walk_control lwc;
1768 unsigned long count;
1769 unsigned long freed;
1770
1771 spin_lock(&mdsc->caps_list_lock);
1772 if (mdsc->caps_use_max > 0 &&
1773 mdsc->caps_use_count > mdsc->caps_use_max)
1774 count = mdsc->caps_use_count - mdsc->caps_use_max;
1775 else
1776 count = 0;
1777 spin_unlock(&mdsc->caps_list_lock);
1778
1779 lwc.dir_lease = false;
1780 lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE * 2;
1781 freed = __dentry_leases_walk(mdsc, &lwc);
1782 if (!lwc.nr_to_scan) /* more invalid leases */
1783 return -EAGAIN;
1784
1785 if (lwc.nr_to_scan < CEPH_CAPS_PER_RELEASE)
1786 lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE;
1787
1788 lwc.dir_lease = true;
1789 lwc.expire_dir_lease = freed < count;
1790 lwc.dir_lease_ttl = mdsc->fsc->mount_options->caps_wanted_delay_max * HZ;
1791 freed +=__dentry_leases_walk(mdsc, &lwc);
1792 if (!lwc.nr_to_scan) /* more to check */
1793 return -EAGAIN;
1794
1795 return freed > 0 ? 1 : 0;
1796 }
1797
1798 /*
1799 * Ensure a dentry lease will no longer revalidate.
1800 */
ceph_invalidate_dentry_lease(struct dentry * dentry)1801 void ceph_invalidate_dentry_lease(struct dentry *dentry)
1802 {
1803 struct ceph_dentry_info *di = ceph_dentry(dentry);
1804 spin_lock(&dentry->d_lock);
1805 di->time = jiffies;
1806 di->lease_shared_gen = 0;
1807 di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1808 __dentry_lease_unlist(di);
1809 spin_unlock(&dentry->d_lock);
1810 }
1811
1812 /*
1813 * Check if dentry lease is valid. If not, delete the lease. Try to
1814 * renew if the least is more than half up.
1815 */
__dentry_lease_is_valid(struct ceph_dentry_info * di)1816 static bool __dentry_lease_is_valid(struct ceph_dentry_info *di)
1817 {
1818 struct ceph_mds_session *session;
1819
1820 if (!di->lease_gen)
1821 return false;
1822
1823 session = di->lease_session;
1824 if (session) {
1825 u32 gen;
1826 unsigned long ttl;
1827
1828 gen = atomic_read(&session->s_cap_gen);
1829 ttl = session->s_cap_ttl;
1830
1831 if (di->lease_gen == gen &&
1832 time_before(jiffies, ttl) &&
1833 time_before(jiffies, di->time))
1834 return true;
1835 }
1836 di->lease_gen = 0;
1837 return false;
1838 }
1839
dentry_lease_is_valid(struct dentry * dentry,unsigned int flags)1840 static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags)
1841 {
1842 struct ceph_dentry_info *di;
1843 struct ceph_mds_session *session = NULL;
1844 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dentry->d_sb)->mdsc;
1845 struct ceph_client *cl = mdsc->fsc->client;
1846 u32 seq = 0;
1847 int valid = 0;
1848
1849 spin_lock(&dentry->d_lock);
1850 di = ceph_dentry(dentry);
1851 if (di && __dentry_lease_is_valid(di)) {
1852 valid = 1;
1853
1854 if (di->lease_renew_after &&
1855 time_after(jiffies, di->lease_renew_after)) {
1856 /*
1857 * We should renew. If we're in RCU walk mode
1858 * though, we can't do that so just return
1859 * -ECHILD.
1860 */
1861 if (flags & LOOKUP_RCU) {
1862 valid = -ECHILD;
1863 } else {
1864 session = ceph_get_mds_session(di->lease_session);
1865 seq = di->lease_seq;
1866 di->lease_renew_after = 0;
1867 di->lease_renew_from = jiffies;
1868 }
1869 }
1870 }
1871 spin_unlock(&dentry->d_lock);
1872
1873 if (session) {
1874 ceph_mdsc_lease_send_msg(session, dentry,
1875 CEPH_MDS_LEASE_RENEW, seq);
1876 ceph_put_mds_session(session);
1877 }
1878 doutc(cl, "dentry %p = %d\n", dentry, valid);
1879 return valid;
1880 }
1881
1882 /*
1883 * Called under dentry->d_lock.
1884 */
__dir_lease_try_check(const struct dentry * dentry)1885 static int __dir_lease_try_check(const struct dentry *dentry)
1886 {
1887 struct ceph_dentry_info *di = ceph_dentry(dentry);
1888 struct inode *dir;
1889 struct ceph_inode_info *ci;
1890 int valid = 0;
1891
1892 if (!di->lease_shared_gen)
1893 return 0;
1894 if (IS_ROOT(dentry))
1895 return 0;
1896
1897 dir = d_inode(dentry->d_parent);
1898 ci = ceph_inode(dir);
1899
1900 if (spin_trylock(&ci->i_ceph_lock)) {
1901 if (atomic_read(&ci->i_shared_gen) == di->lease_shared_gen &&
1902 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 0))
1903 valid = 1;
1904 spin_unlock(&ci->i_ceph_lock);
1905 } else {
1906 valid = -EBUSY;
1907 }
1908
1909 if (!valid)
1910 di->lease_shared_gen = 0;
1911 return valid;
1912 }
1913
1914 /*
1915 * Check if directory-wide content lease/cap is valid.
1916 */
dir_lease_is_valid(struct inode * dir,struct dentry * dentry,struct ceph_mds_client * mdsc)1917 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry,
1918 struct ceph_mds_client *mdsc)
1919 {
1920 struct ceph_inode_info *ci = ceph_inode(dir);
1921 struct ceph_client *cl = mdsc->fsc->client;
1922 int valid;
1923 int shared_gen;
1924
1925 spin_lock(&ci->i_ceph_lock);
1926 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1927 if (valid) {
1928 __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD);
1929 shared_gen = atomic_read(&ci->i_shared_gen);
1930 }
1931 spin_unlock(&ci->i_ceph_lock);
1932 if (valid) {
1933 struct ceph_dentry_info *di;
1934 spin_lock(&dentry->d_lock);
1935 di = ceph_dentry(dentry);
1936 if (dir == d_inode(dentry->d_parent) &&
1937 di && di->lease_shared_gen == shared_gen)
1938 __ceph_dentry_dir_lease_touch(di);
1939 else
1940 valid = 0;
1941 spin_unlock(&dentry->d_lock);
1942 }
1943 doutc(cl, "dir %p %llx.%llx v%u dentry %p '%pd' = %d\n", dir,
1944 ceph_vinop(dir), (unsigned)atomic_read(&ci->i_shared_gen),
1945 dentry, dentry, valid);
1946 return valid;
1947 }
1948
1949 /*
1950 * Check if cached dentry can be trusted.
1951 */
ceph_d_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)1952 static int ceph_d_revalidate(struct inode *dir, const struct qstr *name,
1953 struct dentry *dentry, unsigned int flags)
1954 {
1955 struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(dentry->d_sb)->mdsc;
1956 struct ceph_client *cl = mdsc->fsc->client;
1957 int valid = 0;
1958 struct inode *inode;
1959
1960 valid = fscrypt_d_revalidate(dir, name, dentry, flags);
1961 if (valid <= 0)
1962 return valid;
1963
1964 inode = d_inode_rcu(dentry);
1965
1966 doutc(cl, "%p '%pd' inode %p offset 0x%llx nokey %d\n",
1967 dentry, dentry, inode, ceph_dentry(dentry)->offset,
1968 !!(dentry->d_flags & DCACHE_NOKEY_NAME));
1969
1970 mdsc = ceph_sb_to_fs_client(dir->i_sb)->mdsc;
1971
1972 /* always trust cached snapped dentries, snapdir dentry */
1973 if (ceph_snap(dir) != CEPH_NOSNAP) {
1974 doutc(cl, "%p '%pd' inode %p is SNAPPED\n", dentry,
1975 dentry, inode);
1976 valid = 1;
1977 } else if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1978 valid = 1;
1979 } else {
1980 valid = dentry_lease_is_valid(dentry, flags);
1981 if (valid == -ECHILD)
1982 return valid;
1983 if (valid || dir_lease_is_valid(dir, dentry, mdsc)) {
1984 if (inode)
1985 valid = ceph_is_any_caps(inode);
1986 else
1987 valid = 1;
1988 }
1989 }
1990
1991 if (!valid) {
1992 struct ceph_mds_request *req;
1993 int op, err;
1994 u32 mask;
1995
1996 if (flags & LOOKUP_RCU)
1997 return -ECHILD;
1998
1999 percpu_counter_inc(&mdsc->metric.d_lease_mis);
2000
2001 op = ceph_snap(dir) == CEPH_SNAPDIR ?
2002 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
2003 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
2004 if (!IS_ERR(req)) {
2005 req->r_dentry = dget(dentry);
2006 req->r_num_caps = 2;
2007 req->r_parent = dir;
2008 ihold(dir);
2009
2010 req->r_dname = name;
2011
2012 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
2013 if (ceph_security_xattr_wanted(dir))
2014 mask |= CEPH_CAP_XATTR_SHARED;
2015 req->r_args.getattr.mask = cpu_to_le32(mask);
2016
2017 err = ceph_mdsc_do_request(mdsc, NULL, req);
2018 switch (err) {
2019 case 0:
2020 if (d_really_is_positive(dentry) &&
2021 d_inode(dentry) == req->r_target_inode)
2022 valid = 1;
2023 break;
2024 case -ENOENT:
2025 if (d_really_is_negative(dentry))
2026 valid = 1;
2027 fallthrough;
2028 default:
2029 break;
2030 }
2031 ceph_mdsc_put_request(req);
2032 doutc(cl, "%p '%pd', lookup result=%d\n", dentry,
2033 dentry, err);
2034 }
2035 } else {
2036 percpu_counter_inc(&mdsc->metric.d_lease_hit);
2037 }
2038
2039 doutc(cl, "%p '%pd' %s\n", dentry, dentry, valid ? "valid" : "invalid");
2040 if (!valid)
2041 ceph_dir_clear_complete(dir);
2042 return valid;
2043 }
2044
2045 /*
2046 * Delete unused dentry that doesn't have valid lease
2047 *
2048 * Called under dentry->d_lock.
2049 */
ceph_d_delete(const struct dentry * dentry)2050 static int ceph_d_delete(const struct dentry *dentry)
2051 {
2052 struct ceph_dentry_info *di;
2053
2054 /* won't release caps */
2055 if (d_really_is_negative(dentry))
2056 return 0;
2057 if (ceph_snap(d_inode(dentry)) != CEPH_NOSNAP)
2058 return 0;
2059 /* valid lease? */
2060 di = ceph_dentry(dentry);
2061 if (di) {
2062 if (__dentry_lease_is_valid(di))
2063 return 0;
2064 if (__dir_lease_try_check(dentry))
2065 return 0;
2066 }
2067 return 1;
2068 }
2069
2070 /*
2071 * Release our ceph_dentry_info.
2072 */
ceph_d_release(struct dentry * dentry)2073 static void ceph_d_release(struct dentry *dentry)
2074 {
2075 struct ceph_dentry_info *di = ceph_dentry(dentry);
2076 struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dentry->d_sb);
2077
2078 doutc(fsc->client, "dentry %p '%pd'\n", dentry, dentry);
2079
2080 atomic64_dec(&fsc->mdsc->metric.total_dentries);
2081
2082 spin_lock(&dentry->d_lock);
2083 __dentry_lease_unlist(di);
2084 dentry->d_fsdata = NULL;
2085 spin_unlock(&dentry->d_lock);
2086
2087 ceph_put_mds_session(di->lease_session);
2088 kmem_cache_free(ceph_dentry_cachep, di);
2089 }
2090
2091 /*
2092 * When the VFS prunes a dentry from the cache, we need to clear the
2093 * complete flag on the parent directory.
2094 *
2095 * Called under dentry->d_lock.
2096 */
ceph_d_prune(struct dentry * dentry)2097 static void ceph_d_prune(struct dentry *dentry)
2098 {
2099 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dentry->d_sb);
2100 struct ceph_client *cl = mdsc->fsc->client;
2101 struct ceph_inode_info *dir_ci;
2102 struct ceph_dentry_info *di;
2103
2104 doutc(cl, "dentry %p '%pd'\n", dentry, dentry);
2105
2106 /* do we have a valid parent? */
2107 if (IS_ROOT(dentry))
2108 return;
2109
2110 /* we hold d_lock, so d_parent is stable */
2111 dir_ci = ceph_inode(d_inode(dentry->d_parent));
2112 if (dir_ci->i_vino.snap == CEPH_SNAPDIR)
2113 return;
2114
2115 /* who calls d_delete() should also disable dcache readdir */
2116 if (d_really_is_negative(dentry))
2117 return;
2118
2119 /* d_fsdata does not get cleared until d_release */
2120 if (!d_unhashed(dentry)) {
2121 __ceph_dir_clear_complete(dir_ci);
2122 return;
2123 }
2124
2125 /* Disable dcache readdir just in case that someone called d_drop()
2126 * or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED
2127 * properly (dcache readdir is still enabled) */
2128 di = ceph_dentry(dentry);
2129 if (di->offset > 0 &&
2130 di->lease_shared_gen == atomic_read(&dir_ci->i_shared_gen))
2131 __ceph_dir_clear_ordered(dir_ci);
2132 }
2133
2134 /*
2135 * read() on a dir. This weird interface hack only works if mounted
2136 * with '-o dirstat'.
2137 */
ceph_read_dir(struct file * file,char __user * buf,size_t size,loff_t * ppos)2138 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
2139 loff_t *ppos)
2140 {
2141 struct ceph_dir_file_info *dfi = file->private_data;
2142 struct inode *inode = file_inode(file);
2143 struct ceph_inode_info *ci = ceph_inode(inode);
2144 int left;
2145 const int bufsize = 1024;
2146
2147 if (!ceph_test_mount_opt(ceph_sb_to_fs_client(inode->i_sb), DIRSTAT))
2148 return -EISDIR;
2149
2150 if (!dfi->dir_info) {
2151 dfi->dir_info = kmalloc(bufsize, GFP_KERNEL);
2152 if (!dfi->dir_info)
2153 return -ENOMEM;
2154 dfi->dir_info_len =
2155 snprintf(dfi->dir_info, bufsize,
2156 "entries: %20lld\n"
2157 " files: %20lld\n"
2158 " subdirs: %20lld\n"
2159 "rentries: %20lld\n"
2160 " rfiles: %20lld\n"
2161 " rsubdirs: %20lld\n"
2162 "rbytes: %20lld\n"
2163 "rctime: %10lld.%09ld\n",
2164 ci->i_files + ci->i_subdirs,
2165 ci->i_files,
2166 ci->i_subdirs,
2167 ci->i_rfiles + ci->i_rsubdirs,
2168 ci->i_rfiles,
2169 ci->i_rsubdirs,
2170 ci->i_rbytes,
2171 ci->i_rctime.tv_sec,
2172 ci->i_rctime.tv_nsec);
2173 }
2174
2175 if (*ppos >= dfi->dir_info_len)
2176 return 0;
2177 size = min_t(unsigned, size, dfi->dir_info_len-*ppos);
2178 left = copy_to_user(buf, dfi->dir_info + *ppos, size);
2179 if (left == size)
2180 return -EFAULT;
2181 *ppos += (size - left);
2182 return size - left;
2183 }
2184
2185
2186
2187 /*
2188 * Return name hash for a given dentry. This is dependent on
2189 * the parent directory's hash function.
2190 */
ceph_dentry_hash(struct inode * dir,struct dentry * dn)2191 unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
2192 {
2193 struct ceph_inode_info *dci = ceph_inode(dir);
2194 unsigned hash;
2195
2196 switch (dci->i_dir_layout.dl_dir_hash) {
2197 case 0: /* for backward compat */
2198 case CEPH_STR_HASH_LINUX:
2199 return dn->d_name.hash;
2200
2201 default:
2202 spin_lock(&dn->d_lock);
2203 hash = ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
2204 dn->d_name.name, dn->d_name.len);
2205 spin_unlock(&dn->d_lock);
2206 return hash;
2207 }
2208 }
2209
2210 WRAP_DIR_ITER(ceph_readdir) // FIXME!
2211 const struct file_operations ceph_dir_fops = {
2212 .read = ceph_read_dir,
2213 .iterate_shared = shared_ceph_readdir,
2214 .llseek = ceph_dir_llseek,
2215 .open = ceph_open,
2216 .release = ceph_release,
2217 .unlocked_ioctl = ceph_ioctl,
2218 .compat_ioctl = compat_ptr_ioctl,
2219 .fsync = ceph_fsync,
2220 .lock = ceph_lock,
2221 .flock = ceph_flock,
2222 };
2223
2224 const struct file_operations ceph_snapdir_fops = {
2225 .iterate_shared = shared_ceph_readdir,
2226 .llseek = ceph_dir_llseek,
2227 .open = ceph_open,
2228 .release = ceph_release,
2229 };
2230
2231 const struct inode_operations ceph_dir_iops = {
2232 .lookup = ceph_lookup,
2233 .permission = ceph_permission,
2234 .getattr = ceph_getattr,
2235 .setattr = ceph_setattr,
2236 .listxattr = ceph_listxattr,
2237 .get_inode_acl = ceph_get_acl,
2238 .set_acl = ceph_set_acl,
2239 .mknod = ceph_mknod,
2240 .symlink = ceph_symlink,
2241 .mkdir = ceph_mkdir,
2242 .link = ceph_link,
2243 .unlink = ceph_unlink,
2244 .rmdir = ceph_unlink,
2245 .rename = ceph_rename,
2246 .create = ceph_create,
2247 .atomic_open = ceph_atomic_open,
2248 };
2249
2250 const struct inode_operations ceph_snapdir_iops = {
2251 .lookup = ceph_lookup,
2252 .permission = ceph_permission,
2253 .getattr = ceph_getattr,
2254 .mkdir = ceph_mkdir,
2255 .rmdir = ceph_unlink,
2256 .rename = ceph_rename,
2257 };
2258
2259 const struct dentry_operations ceph_dentry_ops = {
2260 .d_revalidate = ceph_d_revalidate,
2261 .d_delete = ceph_d_delete,
2262 .d_release = ceph_d_release,
2263 .d_prune = ceph_d_prune,
2264 .d_init = ceph_d_init,
2265 };
2266