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