1 #include <linux/ceph/ceph_debug.h>
2
3 #include <linux/spinlock.h>
4 #include <linux/fs_struct.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
13 /*
14 * Directory operations: readdir, lookup, create, link, unlink,
15 * rename, etc.
16 */
17
18 /*
19 * Ceph MDS operations are specified in terms of a base ino and
20 * relative path. Thus, the client can specify an operation on a
21 * specific inode (e.g., a getattr due to fstat(2)), or as a path
22 * relative to, say, the root directory.
23 *
24 * Normally, we limit ourselves to strict inode ops (no path component)
25 * or dentry operations (a single path component relative to an ino). The
26 * exception to this is open_root_dentry(), which will open the mount
27 * point by name.
28 */
29
30 const struct dentry_operations ceph_dentry_ops;
31
32 /*
33 * Initialize ceph dentry state.
34 */
ceph_init_dentry(struct dentry * dentry)35 int ceph_init_dentry(struct dentry *dentry)
36 {
37 struct ceph_dentry_info *di;
38
39 if (dentry->d_fsdata)
40 return 0;
41
42 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
43 if (!di)
44 return -ENOMEM; /* oh well */
45
46 spin_lock(&dentry->d_lock);
47 if (dentry->d_fsdata) {
48 /* lost a race */
49 kmem_cache_free(ceph_dentry_cachep, di);
50 goto out_unlock;
51 }
52
53 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP)
54 d_set_d_op(dentry, &ceph_dentry_ops);
55 else if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
56 d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
57 else
58 d_set_d_op(dentry, &ceph_snap_dentry_ops);
59
60 di->dentry = dentry;
61 di->lease_session = NULL;
62 di->time = jiffies;
63 /* avoid reordering d_fsdata setup so that the check above is safe */
64 smp_mb();
65 dentry->d_fsdata = di;
66 ceph_dentry_lru_add(dentry);
67 out_unlock:
68 spin_unlock(&dentry->d_lock);
69 return 0;
70 }
71
72 /*
73 * for f_pos for readdir:
74 * - hash order:
75 * (0xff << 52) | ((24 bits hash) << 28) |
76 * (the nth entry has hash collision);
77 * - frag+name order;
78 * ((frag value) << 28) | (the nth entry in frag);
79 */
80 #define OFFSET_BITS 28
81 #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
82 #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
ceph_make_fpos(unsigned high,unsigned off,bool hash_order)83 loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
84 {
85 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
86 if (hash_order)
87 fpos |= HASH_ORDER;
88 return fpos;
89 }
90
is_hash_order(loff_t p)91 static bool is_hash_order(loff_t p)
92 {
93 return (p & HASH_ORDER) == HASH_ORDER;
94 }
95
fpos_frag(loff_t p)96 static unsigned fpos_frag(loff_t p)
97 {
98 return p >> OFFSET_BITS;
99 }
100
fpos_hash(loff_t p)101 static unsigned fpos_hash(loff_t p)
102 {
103 return ceph_frag_value(fpos_frag(p));
104 }
105
fpos_off(loff_t p)106 static unsigned fpos_off(loff_t p)
107 {
108 return p & OFFSET_MASK;
109 }
110
fpos_cmp(loff_t l,loff_t r)111 static int fpos_cmp(loff_t l, loff_t r)
112 {
113 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
114 if (v)
115 return v;
116 return (int)(fpos_off(l) - fpos_off(r));
117 }
118
119 /*
120 * make note of the last dentry we read, so we can
121 * continue at the same lexicographical point,
122 * regardless of what dir changes take place on the
123 * server.
124 */
note_last_dentry(struct ceph_file_info * fi,const char * name,int len,unsigned next_offset)125 static int note_last_dentry(struct ceph_file_info *fi, const char *name,
126 int len, unsigned next_offset)
127 {
128 char *buf = kmalloc(len+1, GFP_KERNEL);
129 if (!buf)
130 return -ENOMEM;
131 kfree(fi->last_name);
132 fi->last_name = buf;
133 memcpy(fi->last_name, name, len);
134 fi->last_name[len] = 0;
135 fi->next_offset = next_offset;
136 dout("note_last_dentry '%s'\n", fi->last_name);
137 return 0;
138 }
139
140
141 static struct dentry *
__dcache_find_get_entry(struct dentry * parent,u64 idx,struct ceph_readdir_cache_control * cache_ctl)142 __dcache_find_get_entry(struct dentry *parent, u64 idx,
143 struct ceph_readdir_cache_control *cache_ctl)
144 {
145 struct inode *dir = d_inode(parent);
146 struct dentry *dentry;
147 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
148 loff_t ptr_pos = idx * sizeof(struct dentry *);
149 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
150
151 if (ptr_pos >= i_size_read(dir))
152 return NULL;
153
154 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
155 ceph_readdir_cache_release(cache_ctl);
156 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
157 if (!cache_ctl->page) {
158 dout(" page %lu not found\n", ptr_pgoff);
159 return ERR_PTR(-EAGAIN);
160 }
161 /* reading/filling the cache are serialized by
162 i_mutex, no need to use page lock */
163 unlock_page(cache_ctl->page);
164 cache_ctl->dentries = kmap(cache_ctl->page);
165 }
166
167 cache_ctl->index = idx & idx_mask;
168
169 rcu_read_lock();
170 spin_lock(&parent->d_lock);
171 /* check i_size again here, because empty directory can be
172 * marked as complete while not holding the i_mutex. */
173 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
174 dentry = cache_ctl->dentries[cache_ctl->index];
175 else
176 dentry = NULL;
177 spin_unlock(&parent->d_lock);
178 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
179 dentry = NULL;
180 rcu_read_unlock();
181 return dentry ? : ERR_PTR(-EAGAIN);
182 }
183
184 /*
185 * When possible, we try to satisfy a readdir by peeking at the
186 * dcache. We make this work by carefully ordering dentries on
187 * d_child when we initially get results back from the MDS, and
188 * falling back to a "normal" sync readdir if any dentries in the dir
189 * are dropped.
190 *
191 * Complete dir indicates that we have all dentries in the dir. It is
192 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
193 * the MDS if/when the directory is modified).
194 */
__dcache_readdir(struct file * file,struct dir_context * ctx,u32 shared_gen)195 static int __dcache_readdir(struct file *file, struct dir_context *ctx,
196 u32 shared_gen)
197 {
198 struct ceph_file_info *fi = file->private_data;
199 struct dentry *parent = file->f_path.dentry;
200 struct inode *dir = d_inode(parent);
201 struct dentry *dentry, *last = NULL;
202 struct ceph_dentry_info *di;
203 struct ceph_readdir_cache_control cache_ctl = {};
204 u64 idx = 0;
205 int err = 0;
206
207 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
208
209 /* search start position */
210 if (ctx->pos > 2) {
211 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
212 while (count > 0) {
213 u64 step = count >> 1;
214 dentry = __dcache_find_get_entry(parent, idx + step,
215 &cache_ctl);
216 if (!dentry) {
217 /* use linar search */
218 idx = 0;
219 break;
220 }
221 if (IS_ERR(dentry)) {
222 err = PTR_ERR(dentry);
223 goto out;
224 }
225 di = ceph_dentry(dentry);
226 spin_lock(&dentry->d_lock);
227 if (fpos_cmp(di->offset, ctx->pos) < 0) {
228 idx += step + 1;
229 count -= step + 1;
230 } else {
231 count = step;
232 }
233 spin_unlock(&dentry->d_lock);
234 dput(dentry);
235 }
236
237 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
238 }
239
240
241 for (;;) {
242 bool emit_dentry = false;
243 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
244 if (!dentry) {
245 fi->flags |= CEPH_F_ATEND;
246 err = 0;
247 break;
248 }
249 if (IS_ERR(dentry)) {
250 err = PTR_ERR(dentry);
251 goto out;
252 }
253
254 di = ceph_dentry(dentry);
255 spin_lock(&dentry->d_lock);
256 if (di->lease_shared_gen == shared_gen &&
257 d_really_is_positive(dentry) &&
258 fpos_cmp(ctx->pos, di->offset) <= 0) {
259 emit_dentry = true;
260 }
261 spin_unlock(&dentry->d_lock);
262
263 if (emit_dentry) {
264 dout(" %llx dentry %p %pd %p\n", di->offset,
265 dentry, dentry, d_inode(dentry));
266 ctx->pos = di->offset;
267 if (!dir_emit(ctx, dentry->d_name.name,
268 dentry->d_name.len,
269 ceph_translate_ino(dentry->d_sb,
270 d_inode(dentry)->i_ino),
271 d_inode(dentry)->i_mode >> 12)) {
272 dput(dentry);
273 err = 0;
274 break;
275 }
276 ctx->pos++;
277
278 if (last)
279 dput(last);
280 last = dentry;
281 } else {
282 dput(dentry);
283 }
284 }
285 out:
286 ceph_readdir_cache_release(&cache_ctl);
287 if (last) {
288 int ret;
289 di = ceph_dentry(last);
290 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
291 fpos_off(di->offset) + 1);
292 if (ret < 0)
293 err = ret;
294 dput(last);
295 /* last_name no longer match cache index */
296 if (fi->readdir_cache_idx >= 0) {
297 fi->readdir_cache_idx = -1;
298 fi->dir_release_count = 0;
299 }
300 }
301 return err;
302 }
303
need_send_readdir(struct ceph_file_info * fi,loff_t pos)304 static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
305 {
306 if (!fi->last_readdir)
307 return true;
308 if (is_hash_order(pos))
309 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
310 else
311 return fi->frag != fpos_frag(pos);
312 }
313
ceph_readdir(struct file * file,struct dir_context * ctx)314 static int ceph_readdir(struct file *file, struct dir_context *ctx)
315 {
316 struct ceph_file_info *fi = file->private_data;
317 struct inode *inode = file_inode(file);
318 struct ceph_inode_info *ci = ceph_inode(inode);
319 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
320 struct ceph_mds_client *mdsc = fsc->mdsc;
321 int i;
322 int err;
323 unsigned frag = -1;
324 struct ceph_mds_reply_info_parsed *rinfo;
325
326 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
327 if (fi->flags & CEPH_F_ATEND)
328 return 0;
329
330 /* always start with . and .. */
331 if (ctx->pos == 0) {
332 dout("readdir off 0 -> '.'\n");
333 if (!dir_emit(ctx, ".", 1,
334 ceph_translate_ino(inode->i_sb, inode->i_ino),
335 inode->i_mode >> 12))
336 return 0;
337 ctx->pos = 1;
338 }
339 if (ctx->pos == 1) {
340 ino_t ino = parent_ino(file->f_path.dentry);
341 dout("readdir off 1 -> '..'\n");
342 if (!dir_emit(ctx, "..", 2,
343 ceph_translate_ino(inode->i_sb, ino),
344 inode->i_mode >> 12))
345 return 0;
346 ctx->pos = 2;
347 }
348
349 /* can we use the dcache? */
350 spin_lock(&ci->i_ceph_lock);
351 if (ceph_test_mount_opt(fsc, DCACHE) &&
352 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
353 ceph_snap(inode) != CEPH_SNAPDIR &&
354 __ceph_dir_is_complete_ordered(ci) &&
355 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
356 u32 shared_gen = ci->i_shared_gen;
357 spin_unlock(&ci->i_ceph_lock);
358 err = __dcache_readdir(file, ctx, shared_gen);
359 if (err != -EAGAIN)
360 return err;
361 } else {
362 spin_unlock(&ci->i_ceph_lock);
363 }
364
365 /* proceed with a normal readdir */
366 more:
367 /* do we have the correct frag content buffered? */
368 if (need_send_readdir(fi, ctx->pos)) {
369 struct ceph_mds_request *req;
370 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
371 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
372
373 /* discard old result, if any */
374 if (fi->last_readdir) {
375 ceph_mdsc_put_request(fi->last_readdir);
376 fi->last_readdir = NULL;
377 }
378
379 if (is_hash_order(ctx->pos)) {
380 /* fragtree isn't always accurate. choose frag
381 * based on previous reply when possible. */
382 if (frag == (unsigned)-1)
383 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
384 NULL, NULL);
385 } else {
386 frag = fpos_frag(ctx->pos);
387 }
388
389 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
390 ceph_vinop(inode), frag, fi->last_name);
391 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
392 if (IS_ERR(req))
393 return PTR_ERR(req);
394 err = ceph_alloc_readdir_reply_buffer(req, inode);
395 if (err) {
396 ceph_mdsc_put_request(req);
397 return err;
398 }
399 /* hints to request -> mds selection code */
400 req->r_direct_mode = USE_AUTH_MDS;
401 req->r_direct_hash = ceph_frag_value(frag);
402 req->r_direct_is_hash = true;
403 if (fi->last_name) {
404 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
405 if (!req->r_path2) {
406 ceph_mdsc_put_request(req);
407 return -ENOMEM;
408 }
409 }
410 req->r_dir_release_cnt = fi->dir_release_count;
411 req->r_dir_ordered_cnt = fi->dir_ordered_count;
412 req->r_readdir_cache_idx = fi->readdir_cache_idx;
413 req->r_readdir_offset = fi->next_offset;
414 req->r_args.readdir.frag = cpu_to_le32(frag);
415 req->r_args.readdir.flags =
416 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
417
418 req->r_inode = inode;
419 ihold(inode);
420 req->r_dentry = dget(file->f_path.dentry);
421 err = ceph_mdsc_do_request(mdsc, NULL, req);
422 if (err < 0) {
423 ceph_mdsc_put_request(req);
424 return err;
425 }
426 dout("readdir got and parsed readdir result=%d on "
427 "frag %x, end=%d, complete=%d, hash_order=%d\n",
428 err, frag,
429 (int)req->r_reply_info.dir_end,
430 (int)req->r_reply_info.dir_complete,
431 (int)req->r_reply_info.hash_order);
432
433 rinfo = &req->r_reply_info;
434 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
435 frag = le32_to_cpu(rinfo->dir_dir->frag);
436 if (!rinfo->hash_order) {
437 fi->next_offset = req->r_readdir_offset;
438 /* adjust ctx->pos to beginning of frag */
439 ctx->pos = ceph_make_fpos(frag,
440 fi->next_offset,
441 false);
442 }
443 }
444
445 fi->frag = frag;
446 fi->last_readdir = req;
447
448 if (req->r_did_prepopulate) {
449 fi->readdir_cache_idx = req->r_readdir_cache_idx;
450 if (fi->readdir_cache_idx < 0) {
451 /* preclude from marking dir ordered */
452 fi->dir_ordered_count = 0;
453 } else if (ceph_frag_is_leftmost(frag) &&
454 fi->next_offset == 2) {
455 /* note dir version at start of readdir so
456 * we can tell if any dentries get dropped */
457 fi->dir_release_count = req->r_dir_release_cnt;
458 fi->dir_ordered_count = req->r_dir_ordered_cnt;
459 }
460 } else {
461 dout("readdir !did_prepopulate");
462 /* disable readdir cache */
463 fi->readdir_cache_idx = -1;
464 /* preclude from marking dir complete */
465 fi->dir_release_count = 0;
466 }
467
468 /* note next offset and last dentry name */
469 if (rinfo->dir_nr > 0) {
470 struct ceph_mds_reply_dir_entry *rde =
471 rinfo->dir_entries + (rinfo->dir_nr-1);
472 unsigned next_offset = req->r_reply_info.dir_end ?
473 2 : (fpos_off(rde->offset) + 1);
474 err = note_last_dentry(fi, rde->name, rde->name_len,
475 next_offset);
476 if (err)
477 return err;
478 } else if (req->r_reply_info.dir_end) {
479 fi->next_offset = 2;
480 /* keep last name */
481 }
482 }
483
484 rinfo = &fi->last_readdir->r_reply_info;
485 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
486 fi->frag, rinfo->dir_nr, ctx->pos,
487 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
488
489 i = 0;
490 /* search start position */
491 if (rinfo->dir_nr > 0) {
492 int step, nr = rinfo->dir_nr;
493 while (nr > 0) {
494 step = nr >> 1;
495 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
496 i += step + 1;
497 nr -= step + 1;
498 } else {
499 nr = step;
500 }
501 }
502 }
503 for (; i < rinfo->dir_nr; i++) {
504 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
505 struct ceph_vino vino;
506 ino_t ino;
507 u32 ftype;
508
509 BUG_ON(rde->offset < ctx->pos);
510
511 ctx->pos = rde->offset;
512 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
513 i, rinfo->dir_nr, ctx->pos,
514 rde->name_len, rde->name, &rde->inode.in);
515
516 BUG_ON(!rde->inode.in);
517 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
518 vino.ino = le64_to_cpu(rde->inode.in->ino);
519 vino.snap = le64_to_cpu(rde->inode.in->snapid);
520 ino = ceph_vino_to_ino(vino);
521
522 if (!dir_emit(ctx, rde->name, rde->name_len,
523 ceph_translate_ino(inode->i_sb, ino), ftype)) {
524 dout("filldir stopping us...\n");
525 return 0;
526 }
527 ctx->pos++;
528 }
529
530 ceph_mdsc_put_request(fi->last_readdir);
531 fi->last_readdir = NULL;
532
533 if (fi->next_offset > 2) {
534 frag = fi->frag;
535 goto more;
536 }
537
538 /* more frags? */
539 if (!ceph_frag_is_rightmost(fi->frag)) {
540 frag = ceph_frag_next(fi->frag);
541 if (is_hash_order(ctx->pos)) {
542 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
543 fi->next_offset, true);
544 if (new_pos > ctx->pos)
545 ctx->pos = new_pos;
546 /* keep last_name */
547 } else {
548 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
549 kfree(fi->last_name);
550 fi->last_name = NULL;
551 }
552 dout("readdir next frag is %x\n", frag);
553 goto more;
554 }
555 fi->flags |= CEPH_F_ATEND;
556
557 /*
558 * if dir_release_count still matches the dir, no dentries
559 * were released during the whole readdir, and we should have
560 * the complete dir contents in our cache.
561 */
562 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
563 spin_lock(&ci->i_ceph_lock);
564 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
565 dout(" marking %p complete and ordered\n", inode);
566 /* use i_size to track number of entries in
567 * readdir cache */
568 BUG_ON(fi->readdir_cache_idx < 0);
569 i_size_write(inode, fi->readdir_cache_idx *
570 sizeof(struct dentry*));
571 } else {
572 dout(" marking %p complete\n", inode);
573 }
574 __ceph_dir_set_complete(ci, fi->dir_release_count,
575 fi->dir_ordered_count);
576 spin_unlock(&ci->i_ceph_lock);
577 }
578
579 dout("readdir %p file %p done.\n", inode, file);
580 return 0;
581 }
582
reset_readdir(struct ceph_file_info * fi)583 static void reset_readdir(struct ceph_file_info *fi)
584 {
585 if (fi->last_readdir) {
586 ceph_mdsc_put_request(fi->last_readdir);
587 fi->last_readdir = NULL;
588 }
589 kfree(fi->last_name);
590 fi->last_name = NULL;
591 fi->dir_release_count = 0;
592 fi->readdir_cache_idx = -1;
593 fi->next_offset = 2; /* compensate for . and .. */
594 fi->flags &= ~CEPH_F_ATEND;
595 }
596
597 /*
598 * discard buffered readdir content on seekdir(0), or seek to new frag,
599 * or seek prior to current chunk
600 */
need_reset_readdir(struct ceph_file_info * fi,loff_t new_pos)601 static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
602 {
603 struct ceph_mds_reply_info_parsed *rinfo;
604 loff_t chunk_offset;
605 if (new_pos == 0)
606 return true;
607 if (is_hash_order(new_pos)) {
608 /* no need to reset last_name for a forward seek when
609 * dentries are sotred in hash order */
610 } else if (fi->frag != fpos_frag(new_pos)) {
611 return true;
612 }
613 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
614 if (!rinfo || !rinfo->dir_nr)
615 return true;
616 chunk_offset = rinfo->dir_entries[0].offset;
617 return new_pos < chunk_offset ||
618 is_hash_order(new_pos) != is_hash_order(chunk_offset);
619 }
620
ceph_dir_llseek(struct file * file,loff_t offset,int whence)621 static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
622 {
623 struct ceph_file_info *fi = file->private_data;
624 struct inode *inode = file->f_mapping->host;
625 loff_t retval;
626
627 inode_lock(inode);
628 retval = -EINVAL;
629 switch (whence) {
630 case SEEK_CUR:
631 offset += file->f_pos;
632 case SEEK_SET:
633 break;
634 case SEEK_END:
635 retval = -EOPNOTSUPP;
636 default:
637 goto out;
638 }
639
640 if (offset >= 0) {
641 if (need_reset_readdir(fi, offset)) {
642 dout("dir_llseek dropping %p content\n", file);
643 reset_readdir(fi);
644 } else if (is_hash_order(offset) && offset > file->f_pos) {
645 /* for hash offset, we don't know if a forward seek
646 * is within same frag */
647 fi->dir_release_count = 0;
648 fi->readdir_cache_idx = -1;
649 }
650
651 if (offset != file->f_pos) {
652 file->f_pos = offset;
653 file->f_version = 0;
654 fi->flags &= ~CEPH_F_ATEND;
655 }
656 retval = offset;
657 }
658 out:
659 inode_unlock(inode);
660 return retval;
661 }
662
663 /*
664 * Handle lookups for the hidden .snap directory.
665 */
ceph_handle_snapdir(struct ceph_mds_request * req,struct dentry * dentry,int err)666 int ceph_handle_snapdir(struct ceph_mds_request *req,
667 struct dentry *dentry, int err)
668 {
669 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
670 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
671
672 /* .snap dir? */
673 if (err == -ENOENT &&
674 ceph_snap(parent) == CEPH_NOSNAP &&
675 strcmp(dentry->d_name.name,
676 fsc->mount_options->snapdir_name) == 0) {
677 struct inode *inode = ceph_get_snapdir(parent);
678 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
679 dentry, dentry, inode);
680 BUG_ON(!d_unhashed(dentry));
681 d_add(dentry, inode);
682 err = 0;
683 }
684 return err;
685 }
686
687 /*
688 * Figure out final result of a lookup/open request.
689 *
690 * Mainly, make sure we return the final req->r_dentry (if it already
691 * existed) in place of the original VFS-provided dentry when they
692 * differ.
693 *
694 * Gracefully handle the case where the MDS replies with -ENOENT and
695 * no trace (which it may do, at its discretion, e.g., if it doesn't
696 * care to issue a lease on the negative dentry).
697 */
ceph_finish_lookup(struct ceph_mds_request * req,struct dentry * dentry,int err)698 struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
699 struct dentry *dentry, int err)
700 {
701 if (err == -ENOENT) {
702 /* no trace? */
703 err = 0;
704 if (!req->r_reply_info.head->is_dentry) {
705 dout("ENOENT and no trace, dentry %p inode %p\n",
706 dentry, d_inode(dentry));
707 if (d_really_is_positive(dentry)) {
708 d_drop(dentry);
709 err = -ENOENT;
710 } else {
711 d_add(dentry, NULL);
712 }
713 }
714 }
715 if (err)
716 dentry = ERR_PTR(err);
717 else if (dentry != req->r_dentry)
718 dentry = dget(req->r_dentry); /* we got spliced */
719 else
720 dentry = NULL;
721 return dentry;
722 }
723
is_root_ceph_dentry(struct inode * inode,struct dentry * dentry)724 static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
725 {
726 return ceph_ino(inode) == CEPH_INO_ROOT &&
727 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
728 }
729
730 /*
731 * Look up a single dir entry. If there is a lookup intent, inform
732 * the MDS so that it gets our 'caps wanted' value in a single op.
733 */
ceph_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)734 static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
735 unsigned int flags)
736 {
737 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
738 struct ceph_mds_client *mdsc = fsc->mdsc;
739 struct ceph_mds_request *req;
740 int op;
741 int mask;
742 int err;
743
744 dout("lookup %p dentry %p '%pd'\n",
745 dir, dentry, dentry);
746
747 if (dentry->d_name.len > NAME_MAX)
748 return ERR_PTR(-ENAMETOOLONG);
749
750 err = ceph_init_dentry(dentry);
751 if (err < 0)
752 return ERR_PTR(err);
753
754 /* can we conclude ENOENT locally? */
755 if (d_really_is_negative(dentry)) {
756 struct ceph_inode_info *ci = ceph_inode(dir);
757 struct ceph_dentry_info *di = ceph_dentry(dentry);
758
759 spin_lock(&ci->i_ceph_lock);
760 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
761 if (strncmp(dentry->d_name.name,
762 fsc->mount_options->snapdir_name,
763 dentry->d_name.len) &&
764 !is_root_ceph_dentry(dir, dentry) &&
765 ceph_test_mount_opt(fsc, DCACHE) &&
766 __ceph_dir_is_complete(ci) &&
767 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
768 spin_unlock(&ci->i_ceph_lock);
769 dout(" dir %p complete, -ENOENT\n", dir);
770 d_add(dentry, NULL);
771 di->lease_shared_gen = ci->i_shared_gen;
772 return NULL;
773 }
774 spin_unlock(&ci->i_ceph_lock);
775 }
776
777 op = ceph_snap(dir) == CEPH_SNAPDIR ?
778 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
779 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
780 if (IS_ERR(req))
781 return ERR_CAST(req);
782 req->r_dentry = dget(dentry);
783 req->r_num_caps = 2;
784
785 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
786 if (ceph_security_xattr_wanted(dir))
787 mask |= CEPH_CAP_XATTR_SHARED;
788 req->r_args.getattr.mask = cpu_to_le32(mask);
789
790 req->r_locked_dir = dir;
791 err = ceph_mdsc_do_request(mdsc, NULL, req);
792 err = ceph_handle_snapdir(req, dentry, err);
793 dentry = ceph_finish_lookup(req, dentry, err);
794 ceph_mdsc_put_request(req); /* will dput(dentry) */
795 dout("lookup result=%p\n", dentry);
796 return dentry;
797 }
798
799 /*
800 * If we do a create but get no trace back from the MDS, follow up with
801 * a lookup (the VFS expects us to link up the provided dentry).
802 */
ceph_handle_notrace_create(struct inode * dir,struct dentry * dentry)803 int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
804 {
805 struct dentry *result = ceph_lookup(dir, dentry, 0);
806
807 if (result && !IS_ERR(result)) {
808 /*
809 * We created the item, then did a lookup, and found
810 * it was already linked to another inode we already
811 * had in our cache (and thus got spliced). To not
812 * confuse VFS (especially when inode is a directory),
813 * we don't link our dentry to that inode, return an
814 * error instead.
815 *
816 * This event should be rare and it happens only when
817 * we talk to old MDS. Recent MDS does not send traceless
818 * reply for request that creates new inode.
819 */
820 d_drop(result);
821 return -ESTALE;
822 }
823 return PTR_ERR(result);
824 }
825
ceph_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)826 static int ceph_mknod(struct inode *dir, struct dentry *dentry,
827 umode_t mode, dev_t rdev)
828 {
829 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
830 struct ceph_mds_client *mdsc = fsc->mdsc;
831 struct ceph_mds_request *req;
832 struct ceph_acls_info acls = {};
833 int err;
834
835 if (ceph_snap(dir) != CEPH_NOSNAP)
836 return -EROFS;
837
838 err = ceph_pre_init_acls(dir, &mode, &acls);
839 if (err < 0)
840 return err;
841
842 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
843 dir, dentry, mode, rdev);
844 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
845 if (IS_ERR(req)) {
846 err = PTR_ERR(req);
847 goto out;
848 }
849 req->r_dentry = dget(dentry);
850 req->r_num_caps = 2;
851 req->r_locked_dir = dir;
852 req->r_args.mknod.mode = cpu_to_le32(mode);
853 req->r_args.mknod.rdev = cpu_to_le32(rdev);
854 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
855 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
856 if (acls.pagelist) {
857 req->r_pagelist = acls.pagelist;
858 acls.pagelist = NULL;
859 }
860 err = ceph_mdsc_do_request(mdsc, dir, req);
861 if (!err && !req->r_reply_info.head->is_dentry)
862 err = ceph_handle_notrace_create(dir, dentry);
863 ceph_mdsc_put_request(req);
864 out:
865 if (!err)
866 ceph_init_inode_acls(d_inode(dentry), &acls);
867 else
868 d_drop(dentry);
869 ceph_release_acls_info(&acls);
870 return err;
871 }
872
ceph_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)873 static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
874 bool excl)
875 {
876 return ceph_mknod(dir, dentry, mode, 0);
877 }
878
ceph_symlink(struct inode * dir,struct dentry * dentry,const char * dest)879 static int ceph_symlink(struct inode *dir, struct dentry *dentry,
880 const char *dest)
881 {
882 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
883 struct ceph_mds_client *mdsc = fsc->mdsc;
884 struct ceph_mds_request *req;
885 int err;
886
887 if (ceph_snap(dir) != CEPH_NOSNAP)
888 return -EROFS;
889
890 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
891 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
892 if (IS_ERR(req)) {
893 err = PTR_ERR(req);
894 goto out;
895 }
896 req->r_path2 = kstrdup(dest, GFP_KERNEL);
897 if (!req->r_path2) {
898 err = -ENOMEM;
899 ceph_mdsc_put_request(req);
900 goto out;
901 }
902 req->r_locked_dir = dir;
903 req->r_dentry = dget(dentry);
904 req->r_num_caps = 2;
905 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
906 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
907 err = ceph_mdsc_do_request(mdsc, dir, req);
908 if (!err && !req->r_reply_info.head->is_dentry)
909 err = ceph_handle_notrace_create(dir, dentry);
910 ceph_mdsc_put_request(req);
911 out:
912 if (err)
913 d_drop(dentry);
914 return err;
915 }
916
ceph_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)917 static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
918 {
919 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
920 struct ceph_mds_client *mdsc = fsc->mdsc;
921 struct ceph_mds_request *req;
922 struct ceph_acls_info acls = {};
923 int err = -EROFS;
924 int op;
925
926 if (ceph_snap(dir) == CEPH_SNAPDIR) {
927 /* mkdir .snap/foo is a MKSNAP */
928 op = CEPH_MDS_OP_MKSNAP;
929 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
930 dentry, dentry);
931 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
932 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
933 op = CEPH_MDS_OP_MKDIR;
934 } else {
935 goto out;
936 }
937
938 mode |= S_IFDIR;
939 err = ceph_pre_init_acls(dir, &mode, &acls);
940 if (err < 0)
941 goto out;
942
943 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
944 if (IS_ERR(req)) {
945 err = PTR_ERR(req);
946 goto out;
947 }
948
949 req->r_dentry = dget(dentry);
950 req->r_num_caps = 2;
951 req->r_locked_dir = dir;
952 req->r_args.mkdir.mode = cpu_to_le32(mode);
953 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
954 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
955 if (acls.pagelist) {
956 req->r_pagelist = acls.pagelist;
957 acls.pagelist = NULL;
958 }
959 err = ceph_mdsc_do_request(mdsc, dir, req);
960 if (!err &&
961 !req->r_reply_info.head->is_target &&
962 !req->r_reply_info.head->is_dentry)
963 err = ceph_handle_notrace_create(dir, dentry);
964 ceph_mdsc_put_request(req);
965 out:
966 if (!err)
967 ceph_init_inode_acls(d_inode(dentry), &acls);
968 else
969 d_drop(dentry);
970 ceph_release_acls_info(&acls);
971 return err;
972 }
973
ceph_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)974 static int ceph_link(struct dentry *old_dentry, struct inode *dir,
975 struct dentry *dentry)
976 {
977 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
978 struct ceph_mds_client *mdsc = fsc->mdsc;
979 struct ceph_mds_request *req;
980 int err;
981
982 if (ceph_snap(dir) != CEPH_NOSNAP)
983 return -EROFS;
984
985 dout("link in dir %p old_dentry %p dentry %p\n", dir,
986 old_dentry, dentry);
987 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
988 if (IS_ERR(req)) {
989 d_drop(dentry);
990 return PTR_ERR(req);
991 }
992 req->r_dentry = dget(dentry);
993 req->r_num_caps = 2;
994 req->r_old_dentry = dget(old_dentry);
995 req->r_locked_dir = dir;
996 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
997 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
998 /* release LINK_SHARED on source inode (mds will lock it) */
999 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
1000 err = ceph_mdsc_do_request(mdsc, dir, req);
1001 if (err) {
1002 d_drop(dentry);
1003 } else if (!req->r_reply_info.head->is_dentry) {
1004 ihold(d_inode(old_dentry));
1005 d_instantiate(dentry, d_inode(old_dentry));
1006 }
1007 ceph_mdsc_put_request(req);
1008 return err;
1009 }
1010
1011 /*
1012 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1013 * looks like the link count will hit 0, drop any other caps (other
1014 * than PIN) we don't specifically want (due to the file still being
1015 * open).
1016 */
drop_caps_for_unlink(struct inode * inode)1017 static int drop_caps_for_unlink(struct inode *inode)
1018 {
1019 struct ceph_inode_info *ci = ceph_inode(inode);
1020 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1021
1022 spin_lock(&ci->i_ceph_lock);
1023 if (inode->i_nlink == 1) {
1024 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1025 ci->i_ceph_flags |= CEPH_I_NODELAY;
1026 }
1027 spin_unlock(&ci->i_ceph_lock);
1028 return drop;
1029 }
1030
1031 /*
1032 * rmdir and unlink are differ only by the metadata op code
1033 */
ceph_unlink(struct inode * dir,struct dentry * dentry)1034 static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1035 {
1036 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1037 struct ceph_mds_client *mdsc = fsc->mdsc;
1038 struct inode *inode = d_inode(dentry);
1039 struct ceph_mds_request *req;
1040 int err = -EROFS;
1041 int op;
1042
1043 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1044 /* rmdir .snap/foo is RMSNAP */
1045 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1046 op = CEPH_MDS_OP_RMSNAP;
1047 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1048 dout("unlink/rmdir dir %p dn %p inode %p\n",
1049 dir, dentry, inode);
1050 op = d_is_dir(dentry) ?
1051 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1052 } else
1053 goto out;
1054 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1055 if (IS_ERR(req)) {
1056 err = PTR_ERR(req);
1057 goto out;
1058 }
1059 req->r_dentry = dget(dentry);
1060 req->r_num_caps = 2;
1061 req->r_locked_dir = dir;
1062 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1063 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1064 req->r_inode_drop = drop_caps_for_unlink(inode);
1065 err = ceph_mdsc_do_request(mdsc, dir, req);
1066 if (!err && !req->r_reply_info.head->is_dentry)
1067 d_delete(dentry);
1068 ceph_mdsc_put_request(req);
1069 out:
1070 return err;
1071 }
1072
ceph_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1073 static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1074 struct inode *new_dir, struct dentry *new_dentry,
1075 unsigned int flags)
1076 {
1077 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1078 struct ceph_mds_client *mdsc = fsc->mdsc;
1079 struct ceph_mds_request *req;
1080 int op = CEPH_MDS_OP_RENAME;
1081 int err;
1082
1083 if (flags)
1084 return -EINVAL;
1085
1086 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1087 return -EXDEV;
1088 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1089 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1090 op = CEPH_MDS_OP_RENAMESNAP;
1091 else
1092 return -EROFS;
1093 }
1094 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1095 old_dir, old_dentry, new_dir, new_dentry);
1096 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1097 if (IS_ERR(req))
1098 return PTR_ERR(req);
1099 ihold(old_dir);
1100 req->r_dentry = dget(new_dentry);
1101 req->r_num_caps = 2;
1102 req->r_old_dentry = dget(old_dentry);
1103 req->r_old_dentry_dir = old_dir;
1104 req->r_locked_dir = new_dir;
1105 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1106 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1107 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1108 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1109 /* release LINK_RDCACHE on source inode (mds will lock it) */
1110 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
1111 if (d_really_is_positive(new_dentry))
1112 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
1113 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1114 if (!err && !req->r_reply_info.head->is_dentry) {
1115 /*
1116 * Normally d_move() is done by fill_trace (called by
1117 * do_request, above). If there is no trace, we need
1118 * to do it here.
1119 */
1120
1121 /* d_move screws up sibling dentries' offsets */
1122 ceph_dir_clear_complete(old_dir);
1123 ceph_dir_clear_complete(new_dir);
1124
1125 d_move(old_dentry, new_dentry);
1126
1127 /* ensure target dentry is invalidated, despite
1128 rehashing bug in vfs_rename_dir */
1129 ceph_invalidate_dentry_lease(new_dentry);
1130 }
1131 ceph_mdsc_put_request(req);
1132 return err;
1133 }
1134
1135 /*
1136 * Ensure a dentry lease will no longer revalidate.
1137 */
ceph_invalidate_dentry_lease(struct dentry * dentry)1138 void ceph_invalidate_dentry_lease(struct dentry *dentry)
1139 {
1140 spin_lock(&dentry->d_lock);
1141 ceph_dentry(dentry)->time = jiffies;
1142 ceph_dentry(dentry)->lease_shared_gen = 0;
1143 spin_unlock(&dentry->d_lock);
1144 }
1145
1146 /*
1147 * Check if dentry lease is valid. If not, delete the lease. Try to
1148 * renew if the least is more than half up.
1149 */
dentry_lease_is_valid(struct dentry * dentry,unsigned int flags,struct inode * dir)1150 static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1151 struct inode *dir)
1152 {
1153 struct ceph_dentry_info *di;
1154 struct ceph_mds_session *s;
1155 int valid = 0;
1156 u32 gen;
1157 unsigned long ttl;
1158 struct ceph_mds_session *session = NULL;
1159 u32 seq = 0;
1160
1161 spin_lock(&dentry->d_lock);
1162 di = ceph_dentry(dentry);
1163 if (di && di->lease_session) {
1164 s = di->lease_session;
1165 spin_lock(&s->s_gen_ttl_lock);
1166 gen = s->s_cap_gen;
1167 ttl = s->s_cap_ttl;
1168 spin_unlock(&s->s_gen_ttl_lock);
1169
1170 if (di->lease_gen == gen &&
1171 time_before(jiffies, di->time) &&
1172 time_before(jiffies, ttl)) {
1173 valid = 1;
1174 if (di->lease_renew_after &&
1175 time_after(jiffies, di->lease_renew_after)) {
1176 /*
1177 * We should renew. If we're in RCU walk mode
1178 * though, we can't do that so just return
1179 * -ECHILD.
1180 */
1181 if (flags & LOOKUP_RCU) {
1182 valid = -ECHILD;
1183 } else {
1184 session = ceph_get_mds_session(s);
1185 seq = di->lease_seq;
1186 di->lease_renew_after = 0;
1187 di->lease_renew_from = jiffies;
1188 }
1189 }
1190 }
1191 }
1192 spin_unlock(&dentry->d_lock);
1193
1194 if (session) {
1195 ceph_mdsc_lease_send_msg(session, dir, dentry,
1196 CEPH_MDS_LEASE_RENEW, seq);
1197 ceph_put_mds_session(session);
1198 }
1199 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1200 return valid;
1201 }
1202
1203 /*
1204 * Check if directory-wide content lease/cap is valid.
1205 */
dir_lease_is_valid(struct inode * dir,struct dentry * dentry)1206 static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1207 {
1208 struct ceph_inode_info *ci = ceph_inode(dir);
1209 struct ceph_dentry_info *di = ceph_dentry(dentry);
1210 int valid = 0;
1211
1212 spin_lock(&ci->i_ceph_lock);
1213 if (ci->i_shared_gen == di->lease_shared_gen)
1214 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1215 spin_unlock(&ci->i_ceph_lock);
1216 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1217 dir, (unsigned)ci->i_shared_gen, dentry,
1218 (unsigned)di->lease_shared_gen, valid);
1219 return valid;
1220 }
1221
1222 /*
1223 * Check if cached dentry can be trusted.
1224 */
ceph_d_revalidate(struct dentry * dentry,unsigned int flags)1225 static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1226 {
1227 int valid = 0;
1228 struct dentry *parent;
1229 struct inode *dir;
1230
1231 if (flags & LOOKUP_RCU) {
1232 parent = ACCESS_ONCE(dentry->d_parent);
1233 dir = d_inode_rcu(parent);
1234 if (!dir)
1235 return -ECHILD;
1236 } else {
1237 parent = dget_parent(dentry);
1238 dir = d_inode(parent);
1239 }
1240
1241 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1242 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
1243
1244 /* always trust cached snapped dentries, snapdir dentry */
1245 if (ceph_snap(dir) != CEPH_NOSNAP) {
1246 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1247 dentry, d_inode(dentry));
1248 valid = 1;
1249 } else if (d_really_is_positive(dentry) &&
1250 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
1251 valid = 1;
1252 } else {
1253 valid = dentry_lease_is_valid(dentry, flags, dir);
1254 if (valid == -ECHILD)
1255 return valid;
1256 if (valid || dir_lease_is_valid(dir, dentry)) {
1257 if (d_really_is_positive(dentry))
1258 valid = ceph_is_any_caps(d_inode(dentry));
1259 else
1260 valid = 1;
1261 }
1262 }
1263
1264 if (!valid) {
1265 struct ceph_mds_client *mdsc =
1266 ceph_sb_to_client(dir->i_sb)->mdsc;
1267 struct ceph_mds_request *req;
1268 int op, err;
1269 u32 mask;
1270
1271 if (flags & LOOKUP_RCU)
1272 return -ECHILD;
1273
1274 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1275 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_GETATTR;
1276 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1277 if (!IS_ERR(req)) {
1278 req->r_dentry = dget(dentry);
1279 req->r_num_caps = op == CEPH_MDS_OP_GETATTR ? 1 : 2;
1280
1281 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1282 if (ceph_security_xattr_wanted(dir))
1283 mask |= CEPH_CAP_XATTR_SHARED;
1284 req->r_args.getattr.mask = cpu_to_le32(mask);
1285
1286 err = ceph_mdsc_do_request(mdsc, NULL, req);
1287 switch (err) {
1288 case 0:
1289 if (d_really_is_positive(dentry) &&
1290 d_inode(dentry) == req->r_target_inode)
1291 valid = 1;
1292 break;
1293 case -ENOENT:
1294 if (d_really_is_negative(dentry))
1295 valid = 1;
1296 /* Fallthrough */
1297 default:
1298 break;
1299 }
1300 ceph_mdsc_put_request(req);
1301 dout("d_revalidate %p lookup result=%d\n",
1302 dentry, err);
1303 }
1304 }
1305
1306 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1307 if (valid) {
1308 ceph_dentry_lru_touch(dentry);
1309 } else {
1310 ceph_dir_clear_complete(dir);
1311 }
1312
1313 if (!(flags & LOOKUP_RCU))
1314 dput(parent);
1315 return valid;
1316 }
1317
1318 /*
1319 * Release our ceph_dentry_info.
1320 */
ceph_d_release(struct dentry * dentry)1321 static void ceph_d_release(struct dentry *dentry)
1322 {
1323 struct ceph_dentry_info *di = ceph_dentry(dentry);
1324
1325 dout("d_release %p\n", dentry);
1326 ceph_dentry_lru_del(dentry);
1327
1328 spin_lock(&dentry->d_lock);
1329 dentry->d_fsdata = NULL;
1330 spin_unlock(&dentry->d_lock);
1331
1332 if (di->lease_session)
1333 ceph_put_mds_session(di->lease_session);
1334 kmem_cache_free(ceph_dentry_cachep, di);
1335 }
1336
ceph_snapdir_d_revalidate(struct dentry * dentry,unsigned int flags)1337 static int ceph_snapdir_d_revalidate(struct dentry *dentry,
1338 unsigned int flags)
1339 {
1340 /*
1341 * Eventually, we'll want to revalidate snapped metadata
1342 * too... probably...
1343 */
1344 return 1;
1345 }
1346
1347 /*
1348 * When the VFS prunes a dentry from the cache, we need to clear the
1349 * complete flag on the parent directory.
1350 *
1351 * Called under dentry->d_lock.
1352 */
ceph_d_prune(struct dentry * dentry)1353 static void ceph_d_prune(struct dentry *dentry)
1354 {
1355 dout("ceph_d_prune %p\n", dentry);
1356
1357 /* do we have a valid parent? */
1358 if (IS_ROOT(dentry))
1359 return;
1360
1361 /* if we are not hashed, we don't affect dir's completeness */
1362 if (d_unhashed(dentry))
1363 return;
1364
1365 /*
1366 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1367 * cleared until d_release
1368 */
1369 ceph_dir_clear_complete(d_inode(dentry->d_parent));
1370 }
1371
1372 /*
1373 * read() on a dir. This weird interface hack only works if mounted
1374 * with '-o dirstat'.
1375 */
ceph_read_dir(struct file * file,char __user * buf,size_t size,loff_t * ppos)1376 static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1377 loff_t *ppos)
1378 {
1379 struct ceph_file_info *cf = file->private_data;
1380 struct inode *inode = file_inode(file);
1381 struct ceph_inode_info *ci = ceph_inode(inode);
1382 int left;
1383 const int bufsize = 1024;
1384
1385 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1386 return -EISDIR;
1387
1388 if (!cf->dir_info) {
1389 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
1390 if (!cf->dir_info)
1391 return -ENOMEM;
1392 cf->dir_info_len =
1393 snprintf(cf->dir_info, bufsize,
1394 "entries: %20lld\n"
1395 " files: %20lld\n"
1396 " subdirs: %20lld\n"
1397 "rentries: %20lld\n"
1398 " rfiles: %20lld\n"
1399 " rsubdirs: %20lld\n"
1400 "rbytes: %20lld\n"
1401 "rctime: %10ld.%09ld\n",
1402 ci->i_files + ci->i_subdirs,
1403 ci->i_files,
1404 ci->i_subdirs,
1405 ci->i_rfiles + ci->i_rsubdirs,
1406 ci->i_rfiles,
1407 ci->i_rsubdirs,
1408 ci->i_rbytes,
1409 (long)ci->i_rctime.tv_sec,
1410 (long)ci->i_rctime.tv_nsec);
1411 }
1412
1413 if (*ppos >= cf->dir_info_len)
1414 return 0;
1415 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1416 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1417 if (left == size)
1418 return -EFAULT;
1419 *ppos += (size - left);
1420 return size - left;
1421 }
1422
1423 /*
1424 * We maintain a private dentry LRU.
1425 *
1426 * FIXME: this needs to be changed to a per-mds lru to be useful.
1427 */
ceph_dentry_lru_add(struct dentry * dn)1428 void ceph_dentry_lru_add(struct dentry *dn)
1429 {
1430 struct ceph_dentry_info *di = ceph_dentry(dn);
1431 struct ceph_mds_client *mdsc;
1432
1433 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
1434 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1435 spin_lock(&mdsc->dentry_lru_lock);
1436 list_add_tail(&di->lru, &mdsc->dentry_lru);
1437 mdsc->num_dentry++;
1438 spin_unlock(&mdsc->dentry_lru_lock);
1439 }
1440
ceph_dentry_lru_touch(struct dentry * dn)1441 void ceph_dentry_lru_touch(struct dentry *dn)
1442 {
1443 struct ceph_dentry_info *di = ceph_dentry(dn);
1444 struct ceph_mds_client *mdsc;
1445
1446 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1447 di->offset);
1448 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1449 spin_lock(&mdsc->dentry_lru_lock);
1450 list_move_tail(&di->lru, &mdsc->dentry_lru);
1451 spin_unlock(&mdsc->dentry_lru_lock);
1452 }
1453
ceph_dentry_lru_del(struct dentry * dn)1454 void ceph_dentry_lru_del(struct dentry *dn)
1455 {
1456 struct ceph_dentry_info *di = ceph_dentry(dn);
1457 struct ceph_mds_client *mdsc;
1458
1459 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
1460 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1461 spin_lock(&mdsc->dentry_lru_lock);
1462 list_del_init(&di->lru);
1463 mdsc->num_dentry--;
1464 spin_unlock(&mdsc->dentry_lru_lock);
1465 }
1466
1467 /*
1468 * Return name hash for a given dentry. This is dependent on
1469 * the parent directory's hash function.
1470 */
ceph_dentry_hash(struct inode * dir,struct dentry * dn)1471 unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1472 {
1473 struct ceph_inode_info *dci = ceph_inode(dir);
1474
1475 switch (dci->i_dir_layout.dl_dir_hash) {
1476 case 0: /* for backward compat */
1477 case CEPH_STR_HASH_LINUX:
1478 return dn->d_name.hash;
1479
1480 default:
1481 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1482 dn->d_name.name, dn->d_name.len);
1483 }
1484 }
1485
1486 const struct file_operations ceph_dir_fops = {
1487 .read = ceph_read_dir,
1488 .iterate = ceph_readdir,
1489 .llseek = ceph_dir_llseek,
1490 .open = ceph_open,
1491 .release = ceph_release,
1492 .unlocked_ioctl = ceph_ioctl,
1493 .fsync = ceph_fsync,
1494 };
1495
1496 const struct file_operations ceph_snapdir_fops = {
1497 .iterate = ceph_readdir,
1498 .llseek = ceph_dir_llseek,
1499 .open = ceph_open,
1500 .release = ceph_release,
1501 };
1502
1503 const struct inode_operations ceph_dir_iops = {
1504 .lookup = ceph_lookup,
1505 .permission = ceph_permission,
1506 .getattr = ceph_getattr,
1507 .setattr = ceph_setattr,
1508 .listxattr = ceph_listxattr,
1509 .get_acl = ceph_get_acl,
1510 .set_acl = ceph_set_acl,
1511 .mknod = ceph_mknod,
1512 .symlink = ceph_symlink,
1513 .mkdir = ceph_mkdir,
1514 .link = ceph_link,
1515 .unlink = ceph_unlink,
1516 .rmdir = ceph_unlink,
1517 .rename = ceph_rename,
1518 .create = ceph_create,
1519 .atomic_open = ceph_atomic_open,
1520 };
1521
1522 const struct inode_operations ceph_snapdir_iops = {
1523 .lookup = ceph_lookup,
1524 .permission = ceph_permission,
1525 .getattr = ceph_getattr,
1526 .mkdir = ceph_mkdir,
1527 .rmdir = ceph_unlink,
1528 .rename = ceph_rename,
1529 };
1530
1531 const struct dentry_operations ceph_dentry_ops = {
1532 .d_revalidate = ceph_d_revalidate,
1533 .d_release = ceph_d_release,
1534 .d_prune = ceph_d_prune,
1535 };
1536
1537 const struct dentry_operations ceph_snapdir_dentry_ops = {
1538 .d_revalidate = ceph_snapdir_d_revalidate,
1539 .d_release = ceph_d_release,
1540 };
1541
1542 const struct dentry_operations ceph_snap_dentry_ops = {
1543 .d_release = ceph_d_release,
1544 .d_prune = ceph_d_prune,
1545 };
1546