dir.c 37.4 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
S
Sage Weil 已提交
2 3 4 5

#include <linux/spinlock.h>
#include <linux/fs_struct.h>
#include <linux/namei.h>
6
#include <linux/slab.h>
S
Sage Weil 已提交
7 8 9
#include <linux/sched.h>

#include "super.h"
10
#include "mds_client.h"
S
Sage Weil 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

/*
 * Directory operations: readdir, lookup, create, link, unlink,
 * rename, etc.
 */

/*
 * Ceph MDS operations are specified in terms of a base ino and
 * relative path.  Thus, the client can specify an operation on a
 * specific inode (e.g., a getattr due to fstat(2)), or as a path
 * relative to, say, the root directory.
 *
 * Normally, we limit ourselves to strict inode ops (no path component)
 * or dentry operations (a single path component relative to an ino).  The
 * exception to this is open_root_dentry(), which will open the mount
 * point by name.
 */

S
Sage Weil 已提交
29
const struct dentry_operations ceph_dentry_ops;
S
Sage Weil 已提交
30 31 32 33 34 35 36 37 38 39 40

/*
 * Initialize ceph dentry state.
 */
int ceph_init_dentry(struct dentry *dentry)
{
	struct ceph_dentry_info *di;

	if (dentry->d_fsdata)
		return 0;

G
Geliang Tang 已提交
41
	di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
S
Sage Weil 已提交
42 43 44 45
	if (!di)
		return -ENOMEM;          /* oh well */

	spin_lock(&dentry->d_lock);
46 47 48
	if (dentry->d_fsdata) {
		/* lost a race */
		kmem_cache_free(ceph_dentry_cachep, di);
S
Sage Weil 已提交
49
		goto out_unlock;
50
	}
51

52
	if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP)
53
		d_set_d_op(dentry, &ceph_dentry_ops);
54
	else if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
55 56 57 58
		d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
	else
		d_set_d_op(dentry, &ceph_snap_dentry_ops);

S
Sage Weil 已提交
59 60 61
	di->dentry = dentry;
	di->lease_session = NULL;
	dentry->d_time = jiffies;
62 63 64
	/* avoid reordering d_fsdata setup so that the check above is safe */
	smp_mb();
	dentry->d_fsdata = di;
S
Sage Weil 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	ceph_dentry_lru_add(dentry);
out_unlock:
	spin_unlock(&dentry->d_lock);
	return 0;
}

/*
 * for readdir, we encode the directory frag and offset within that
 * frag into f_pos.
 */
static unsigned fpos_frag(loff_t p)
{
	return p >> 32;
}
static unsigned fpos_off(loff_t p)
{
	return p & 0xffffffff;
}

Y
Yan, Zheng 已提交
84 85 86 87 88 89 90 91
static int fpos_cmp(loff_t l, loff_t r)
{
	int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
	if (v)
		return v;
	return (int)(fpos_off(l) - fpos_off(r));
}

Y
Yan, Zheng 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/*
 * make note of the last dentry we read, so we can
 * continue at the same lexicographical point,
 * regardless of what dir changes take place on the
 * server.
 */
static int note_last_dentry(struct ceph_file_info *fi, const char *name,
		            int len, unsigned next_offset)
{
	char *buf = kmalloc(len+1, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
	kfree(fi->last_name);
	fi->last_name = buf;
	memcpy(fi->last_name, name, len);
	fi->last_name[len] = 0;
	fi->next_offset = next_offset;
	dout("note_last_dentry '%s'\n", fi->last_name);
	return 0;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

static struct dentry *
__dcache_find_get_entry(struct dentry *parent, u64 idx,
			struct ceph_readdir_cache_control *cache_ctl)
{
	struct inode *dir = d_inode(parent);
	struct dentry *dentry;
	unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
	loff_t ptr_pos = idx * sizeof(struct dentry *);
	pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;

	if (ptr_pos >= i_size_read(dir))
		return NULL;

	if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
		ceph_readdir_cache_release(cache_ctl);
		cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
		if (!cache_ctl->page) {
			dout(" page %lu not found\n", ptr_pgoff);
			return ERR_PTR(-EAGAIN);
		}
		/* reading/filling the cache are serialized by
		   i_mutex, no need to use page lock */
		unlock_page(cache_ctl->page);
		cache_ctl->dentries = kmap(cache_ctl->page);
	}

	cache_ctl->index = idx & idx_mask;

	rcu_read_lock();
	spin_lock(&parent->d_lock);
	/* check i_size again here, because empty directory can be
	 * marked as complete while not holding the i_mutex. */
	if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
		dentry = cache_ctl->dentries[cache_ctl->index];
	else
		dentry = NULL;
	spin_unlock(&parent->d_lock);
	if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
		dentry = NULL;
	rcu_read_unlock();
	return dentry ? : ERR_PTR(-EAGAIN);
}

S
Sage Weil 已提交
157 158 159
/*
 * When possible, we try to satisfy a readdir by peeking at the
 * dcache.  We make this work by carefully ordering dentries on
160
 * d_child when we initially get results back from the MDS, and
S
Sage Weil 已提交
161 162 163
 * falling back to a "normal" sync readdir if any dentries in the dir
 * are dropped.
 *
164
 * Complete dir indicates that we have all dentries in the dir.  It is
S
Sage Weil 已提交
165 166 167
 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
 * the MDS if/when the directory is modified).
 */
168 169
static int __dcache_readdir(struct file *file,  struct dir_context *ctx,
			    u32 shared_gen)
S
Sage Weil 已提交
170
{
A
Al Viro 已提交
171
	struct ceph_file_info *fi = file->private_data;
A
Al Viro 已提交
172
	struct dentry *parent = file->f_path.dentry;
173
	struct inode *dir = d_inode(parent);
Y
Yan, Zheng 已提交
174
	struct dentry *dentry, *last = NULL;
S
Sage Weil 已提交
175
	struct ceph_dentry_info *di;
Y
Yan, Zheng 已提交
176
	struct ceph_readdir_cache_control cache_ctl = {};
177 178
	u64 idx = 0;
	int err = 0;
S
Sage Weil 已提交
179

Y
Yan, Zheng 已提交
180
	dout("__dcache_readdir %p v%u at %llu\n", dir, shared_gen, ctx->pos);
S
Sage Weil 已提交
181

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	/* search start position */
	if (ctx->pos > 2) {
		u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
		while (count > 0) {
			u64 step = count >> 1;
			dentry = __dcache_find_get_entry(parent, idx + step,
							 &cache_ctl);
			if (!dentry) {
				/* use linar search */
				idx = 0;
				break;
			}
			if (IS_ERR(dentry)) {
				err = PTR_ERR(dentry);
				goto out;
			}
			di = ceph_dentry(dentry);
			spin_lock(&dentry->d_lock);
			if (fpos_cmp(di->offset, ctx->pos) < 0) {
				idx += step + 1;
				count -= step + 1;
			} else {
				count = step;
			}
			spin_unlock(&dentry->d_lock);
			dput(dentry);
		}

		dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
S
Sage Weil 已提交
211 212
	}

Y
Yan, Zheng 已提交
213

214 215 216 217
	for (;;) {
		bool emit_dentry = false;
		dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
		if (!dentry) {
218
			fi->flags |= CEPH_F_ATEND;
Y
Yan, Zheng 已提交
219 220
			err = 0;
			break;
S
Sage Weil 已提交
221
		}
222 223 224
		if (IS_ERR(dentry)) {
			err = PTR_ERR(dentry);
			goto out;
Y
Yan, Zheng 已提交
225 226 227 228
		}

		di = ceph_dentry(dentry);
		spin_lock(&dentry->d_lock);
229
		if (di->lease_shared_gen == shared_gen &&
Y
Yan, Zheng 已提交
230 231 232 233
		    d_really_is_positive(dentry) &&
		    fpos_cmp(ctx->pos, di->offset) <= 0) {
			emit_dentry = true;
		}
N
Nick Piggin 已提交
234
		spin_unlock(&dentry->d_lock);
S
Sage Weil 已提交
235

Y
Yan, Zheng 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249
		if (emit_dentry) {
			dout(" %llu (%llu) dentry %p %pd %p\n", di->offset, ctx->pos,
			     dentry, dentry, d_inode(dentry));
			ctx->pos = di->offset;
			if (!dir_emit(ctx, dentry->d_name.name,
				      dentry->d_name.len,
				      ceph_translate_ino(dentry->d_sb,
							 d_inode(dentry)->i_ino),
				      d_inode(dentry)->i_mode >> 12)) {
				dput(dentry);
				err = 0;
				break;
			}
			ctx->pos++;
250

Y
Yan, Zheng 已提交
251 252 253 254 255
			if (last)
				dput(last);
			last = dentry;
		} else {
			dput(dentry);
S
Sage Weil 已提交
256
		}
Y
Yan, Zheng 已提交
257
	}
258
out:
Y
Yan, Zheng 已提交
259 260 261 262 263 264 265 266
	ceph_readdir_cache_release(&cache_ctl);
	if (last) {
		int ret;
		di = ceph_dentry(last);
		ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
				       fpos_off(di->offset) + 1);
		if (ret < 0)
			err = ret;
S
Sage Weil 已提交
267
		dput(last);
Y
Yan, Zheng 已提交
268
	}
S
Sage Weil 已提交
269 270 271
	return err;
}

A
Al Viro 已提交
272
static int ceph_readdir(struct file *file, struct dir_context *ctx)
S
Sage Weil 已提交
273
{
A
Al Viro 已提交
274 275
	struct ceph_file_info *fi = file->private_data;
	struct inode *inode = file_inode(file);
S
Sage Weil 已提交
276
	struct ceph_inode_info *ci = ceph_inode(inode);
277 278
	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
	struct ceph_mds_client *mdsc = fsc->mdsc;
A
Al Viro 已提交
279 280
	unsigned frag = fpos_frag(ctx->pos);
	int off = fpos_off(ctx->pos);
S
Sage Weil 已提交
281 282 283 284
	int err;
	u32 ftype;
	struct ceph_mds_reply_info_parsed *rinfo;

A
Al Viro 已提交
285
	dout("readdir %p file %p frag %u off %u\n", inode, file, frag, off);
286
	if (fi->flags & CEPH_F_ATEND)
S
Sage Weil 已提交
287 288 289
		return 0;

	/* always start with . and .. */
A
Al Viro 已提交
290
	if (ctx->pos == 0) {
S
Sage Weil 已提交
291
		dout("readdir off 0 -> '.'\n");
A
Al Viro 已提交
292
		if (!dir_emit(ctx, ".", 1, 
Y
Yehuda Sadeh 已提交
293
			    ceph_translate_ino(inode->i_sb, inode->i_ino),
A
Al Viro 已提交
294
			    inode->i_mode >> 12))
S
Sage Weil 已提交
295
			return 0;
A
Al Viro 已提交
296
		ctx->pos = 1;
S
Sage Weil 已提交
297 298
		off = 1;
	}
A
Al Viro 已提交
299
	if (ctx->pos == 1) {
A
Al Viro 已提交
300
		ino_t ino = parent_ino(file->f_path.dentry);
S
Sage Weil 已提交
301
		dout("readdir off 1 -> '..'\n");
A
Al Viro 已提交
302
		if (!dir_emit(ctx, "..", 2,
Y
Yehuda Sadeh 已提交
303
			    ceph_translate_ino(inode->i_sb, ino),
A
Al Viro 已提交
304
			    inode->i_mode >> 12))
S
Sage Weil 已提交
305
			return 0;
A
Al Viro 已提交
306
		ctx->pos = 2;
S
Sage Weil 已提交
307 308 309 310
		off = 2;
	}

	/* can we use the dcache? */
311
	spin_lock(&ci->i_ceph_lock);
Y
Yan, Zheng 已提交
312
	if (ceph_test_mount_opt(fsc, DCACHE) &&
313
	    !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
314
	    ceph_snap(inode) != CEPH_SNAPDIR &&
315
	    __ceph_dir_is_complete_ordered(ci) &&
S
Sage Weil 已提交
316
	    __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
317
		u32 shared_gen = ci->i_shared_gen;
318
		spin_unlock(&ci->i_ceph_lock);
319
		err = __dcache_readdir(file, ctx, shared_gen);
320
		if (err != -EAGAIN)
S
Sage Weil 已提交
321
			return err;
322 323
		frag = fpos_frag(ctx->pos);
		off = fpos_off(ctx->pos);
324
	} else {
325
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
326 327 328 329 330 331 332 333 334 335 336
	}

	/* proceed with a normal readdir */
more:
	/* do we have the correct frag content buffered? */
	if (fi->frag != frag || fi->last_readdir == NULL) {
		struct ceph_mds_request *req;
		int op = ceph_snap(inode) == CEPH_SNAPDIR ?
			CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;

		/* discard old result, if any */
337
		if (fi->last_readdir) {
S
Sage Weil 已提交
338
			ceph_mdsc_put_request(fi->last_readdir);
339 340
			fi->last_readdir = NULL;
		}
S
Sage Weil 已提交
341 342 343 344 345 346

		dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
		     ceph_vinop(inode), frag, fi->last_name);
		req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
		if (IS_ERR(req))
			return PTR_ERR(req);
347 348 349 350 351
		err = ceph_alloc_readdir_reply_buffer(req, inode);
		if (err) {
			ceph_mdsc_put_request(req);
			return err;
		}
S
Sage Weil 已提交
352 353 354 355
		/* hints to request -> mds selection code */
		req->r_direct_mode = USE_AUTH_MDS;
		req->r_direct_hash = ceph_frag_value(frag);
		req->r_direct_is_hash = true;
356
		if (fi->last_name) {
357
			req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
358 359 360 361 362
			if (!req->r_path2) {
				ceph_mdsc_put_request(req);
				return -ENOMEM;
			}
		}
Y
Yan, Zheng 已提交
363 364 365
		req->r_dir_release_cnt = fi->dir_release_count;
		req->r_dir_ordered_cnt = fi->dir_ordered_count;
		req->r_readdir_cache_idx = fi->readdir_cache_idx;
S
Sage Weil 已提交
366 367
		req->r_readdir_offset = fi->next_offset;
		req->r_args.readdir.frag = cpu_to_le32(frag);
368 369
		req->r_args.readdir.flags =
				cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
370 371 372 373

		req->r_inode = inode;
		ihold(inode);
		req->r_dentry = dget(file->f_path.dentry);
S
Sage Weil 已提交
374 375 376 377 378 379 380 381 382 383 384 385
		err = ceph_mdsc_do_request(mdsc, NULL, req);
		if (err < 0) {
			ceph_mdsc_put_request(req);
			return err;
		}
		dout("readdir got and parsed readdir result=%d"
		     " on frag %x, end=%d, complete=%d\n", err, frag,
		     (int)req->r_reply_info.dir_end,
		     (int)req->r_reply_info.dir_complete);


		/* note next offset and last dentry name */
386 387 388
		rinfo = &req->r_reply_info;
		if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
			frag = le32_to_cpu(rinfo->dir_dir->frag);
Y
Yan, Zheng 已提交
389 390
			off = req->r_readdir_offset;
			fi->next_offset = off;
391
		}
Y
Yan, Zheng 已提交
392

Y
Yan, Zheng 已提交
393
		fi->frag = frag;
S
Sage Weil 已提交
394 395 396
		fi->offset = fi->next_offset;
		fi->last_readdir = req;

Y
Yan, Zheng 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
		if (req->r_did_prepopulate) {
			fi->readdir_cache_idx = req->r_readdir_cache_idx;
			if (fi->readdir_cache_idx < 0) {
				/* preclude from marking dir ordered */
				fi->dir_ordered_count = 0;
			} else if (ceph_frag_is_leftmost(frag) && off == 2) {
				/* note dir version at start of readdir so
				 * we can tell if any dentries get dropped */
				fi->dir_release_count = req->r_dir_release_cnt;
				fi->dir_ordered_count = req->r_dir_ordered_cnt;
			}
		} else {
			dout("readdir !did_prepopulate");
			/* disable readdir cache */
			fi->readdir_cache_idx = -1;
			/* preclude from marking dir complete */
			fi->dir_release_count = 0;
		}

S
Sage Weil 已提交
416 417 418
		if (req->r_reply_info.dir_end) {
			kfree(fi->last_name);
			fi->last_name = NULL;
Y
Yan, Zheng 已提交
419
			fi->next_offset = 2;
S
Sage Weil 已提交
420
		} else {
421 422 423
			struct ceph_mds_reply_dir_entry *rde =
					rinfo->dir_entries + (rinfo->dir_nr-1);
			err = note_last_dentry(fi, rde->name, rde->name_len,
Y
Yan, Zheng 已提交
424
				       fi->next_offset + rinfo->dir_nr);
S
Sage Weil 已提交
425 426 427 428 429 430 431 432
			if (err)
				return err;
		}
	}

	rinfo = &fi->last_readdir->r_reply_info;
	dout("readdir frag %x num %d off %d chunkoff %d\n", frag,
	     rinfo->dir_nr, off, fi->offset);
A
Al Viro 已提交
433 434

	ctx->pos = ceph_make_fpos(frag, off);
435
	while (off >= fi->offset && off - fi->offset < rinfo->dir_nr) {
436 437
		struct ceph_mds_reply_dir_entry *rde =
			rinfo->dir_entries + (off - fi->offset);
438 439 440
		struct ceph_vino vino;
		ino_t ino;

S
Sage Weil 已提交
441
		dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
A
Al Viro 已提交
442
		     off, off - fi->offset, rinfo->dir_nr, ctx->pos,
443 444 445 446 447
		     rde->name_len, rde->name, &rde->inode.in);
		BUG_ON(!rde->inode.in);
		ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
		vino.ino = le64_to_cpu(rde->inode.in->ino);
		vino.snap = le64_to_cpu(rde->inode.in->snapid);
448
		ino = ceph_vino_to_ino(vino);
449 450
		if (!dir_emit(ctx, rde->name, rde->name_len,
			      ceph_translate_ino(inode->i_sb, ino), ftype)) {
S
Sage Weil 已提交
451 452 453 454
			dout("filldir stopping us...\n");
			return 0;
		}
		off++;
A
Al Viro 已提交
455
		ctx->pos++;
S
Sage Weil 已提交
456 457 458 459 460 461 462 463 464 465 466
	}

	if (fi->last_name) {
		ceph_mdsc_put_request(fi->last_readdir);
		fi->last_readdir = NULL;
		goto more;
	}

	/* more frags? */
	if (!ceph_frag_is_rightmost(frag)) {
		frag = ceph_frag_next(frag);
Y
Yan, Zheng 已提交
467
		off = 2;
A
Al Viro 已提交
468
		ctx->pos = ceph_make_fpos(frag, off);
S
Sage Weil 已提交
469 470 471
		dout("readdir next frag is %x\n", frag);
		goto more;
	}
472
	fi->flags |= CEPH_F_ATEND;
S
Sage Weil 已提交
473 474 475 476 477 478

	/*
	 * if dir_release_count still matches the dir, no dentries
	 * were released during the whole readdir, and we should have
	 * the complete dir contents in our cache.
	 */
Y
Yan, Zheng 已提交
479 480 481
	if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
		spin_lock(&ci->i_ceph_lock);
		if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
482
			dout(" marking %p complete and ordered\n", inode);
Y
Yan, Zheng 已提交
483 484 485 486 487 488
			/* use i_size to track number of entries in
			 * readdir cache */
			BUG_ON(fi->readdir_cache_idx < 0);
			i_size_write(inode, fi->readdir_cache_idx *
				     sizeof(struct dentry*));
		} else {
489
			dout(" marking %p complete\n", inode);
Y
Yan, Zheng 已提交
490
		}
491 492
		__ceph_dir_set_complete(ci, fi->dir_release_count,
					fi->dir_ordered_count);
Y
Yan, Zheng 已提交
493
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
494 495
	}

A
Al Viro 已提交
496
	dout("readdir %p file %p done.\n", inode, file);
S
Sage Weil 已提交
497 498 499
	return 0;
}

Y
Yan, Zheng 已提交
500
static void reset_readdir(struct ceph_file_info *fi, unsigned frag)
S
Sage Weil 已提交
501 502 503 504 505 506
{
	if (fi->last_readdir) {
		ceph_mdsc_put_request(fi->last_readdir);
		fi->last_readdir = NULL;
	}
	kfree(fi->last_name);
S
Sage Weil 已提交
507
	fi->last_name = NULL;
Y
Yan, Zheng 已提交
508 509
	fi->dir_release_count = 0;
	fi->readdir_cache_idx = -1;
Y
Yan, Zheng 已提交
510
	fi->next_offset = 2;  /* compensate for . and .. */
511
	fi->flags &= ~CEPH_F_ATEND;
S
Sage Weil 已提交
512 513
}

514
static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
S
Sage Weil 已提交
515 516 517
{
	struct ceph_file_info *fi = file->private_data;
	struct inode *inode = file->f_mapping->host;
Y
Yan, Zheng 已提交
518
	loff_t old_offset = ceph_make_fpos(fi->frag, fi->next_offset);
S
Sage Weil 已提交
519 520
	loff_t retval;

A
Al Viro 已提交
521
	inode_lock(inode);
522
	retval = -EINVAL;
523
	switch (whence) {
S
Sage Weil 已提交
524 525
	case SEEK_CUR:
		offset += file->f_pos;
526 527
	case SEEK_SET:
		break;
Y
Yan, Zheng 已提交
528 529
	case SEEK_END:
		retval = -EOPNOTSUPP;
530 531
	default:
		goto out;
S
Sage Weil 已提交
532
	}
533

Y
Yan, Zheng 已提交
534
	if (offset >= 0) {
S
Sage Weil 已提交
535 536 537
		if (offset != file->f_pos) {
			file->f_pos = offset;
			file->f_version = 0;
538
			fi->flags &= ~CEPH_F_ATEND;
S
Sage Weil 已提交
539 540 541 542
		}
		retval = offset;

		if (offset == 0 ||
Y
Yan, Zheng 已提交
543
		    fpos_frag(offset) != fi->frag ||
S
Sage Weil 已提交
544
		    fpos_off(offset) < fi->offset) {
Y
Yan, Zheng 已提交
545 546
			/* discard buffered readdir content on seekdir(0), or
			 * seek to new frag, or seek prior to current chunk */
S
Sage Weil 已提交
547
			dout("dir_llseek dropping %p content\n", file);
Y
Yan, Zheng 已提交
548
			reset_readdir(fi, fpos_frag(offset));
Y
Yan, Zheng 已提交
549 550 551 552
		} else if (fpos_cmp(offset, old_offset) > 0) {
			/* reset dir_release_count if we did a forward seek */
			fi->dir_release_count = 0;
			fi->readdir_cache_idx = -1;
S
Sage Weil 已提交
553 554
		}
	}
555
out:
A
Al Viro 已提交
556
	inode_unlock(inode);
S
Sage Weil 已提交
557 558 559 560
	return retval;
}

/*
561
 * Handle lookups for the hidden .snap directory.
S
Sage Weil 已提交
562
 */
563 564
int ceph_handle_snapdir(struct ceph_mds_request *req,
			struct dentry *dentry, int err)
S
Sage Weil 已提交
565
{
566
	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
567
	struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
S
Sage Weil 已提交
568 569 570

	/* .snap dir? */
	if (err == -ENOENT &&
571
	    ceph_snap(parent) == CEPH_NOSNAP &&
572
	    strcmp(dentry->d_name.name,
573
		   fsc->mount_options->snapdir_name) == 0) {
S
Sage Weil 已提交
574
		struct inode *inode = ceph_get_snapdir(parent);
A
Al Viro 已提交
575 576
		dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
		     dentry, dentry, inode);
577
		BUG_ON(!d_unhashed(dentry));
S
Sage Weil 已提交
578 579 580
		d_add(dentry, inode);
		err = 0;
	}
581 582
	return err;
}
S
Sage Weil 已提交
583

584 585 586 587 588 589 590 591 592 593 594 595 596 597
/*
 * Figure out final result of a lookup/open request.
 *
 * Mainly, make sure we return the final req->r_dentry (if it already
 * existed) in place of the original VFS-provided dentry when they
 * differ.
 *
 * Gracefully handle the case where the MDS replies with -ENOENT and
 * no trace (which it may do, at its discretion, e.g., if it doesn't
 * care to issue a lease on the negative dentry).
 */
struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
				  struct dentry *dentry, int err)
{
S
Sage Weil 已提交
598 599 600 601 602
	if (err == -ENOENT) {
		/* no trace? */
		err = 0;
		if (!req->r_reply_info.head->is_dentry) {
			dout("ENOENT and no trace, dentry %p inode %p\n",
603 604
			     dentry, d_inode(dentry));
			if (d_really_is_positive(dentry)) {
S
Sage Weil 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
				d_drop(dentry);
				err = -ENOENT;
			} else {
				d_add(dentry, NULL);
			}
		}
	}
	if (err)
		dentry = ERR_PTR(err);
	else if (dentry != req->r_dentry)
		dentry = dget(req->r_dentry);   /* we got spliced */
	else
		dentry = NULL;
	return dentry;
}

S
Sage Weil 已提交
621 622 623 624 625 626
static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
{
	return ceph_ino(inode) == CEPH_INO_ROOT &&
		strncmp(dentry->d_name.name, ".ceph", 5) == 0;
}

S
Sage Weil 已提交
627 628 629 630 631
/*
 * Look up a single dir entry.  If there is a lookup intent, inform
 * the MDS so that it gets our 'caps wanted' value in a single op.
 */
static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
632
				  unsigned int flags)
S
Sage Weil 已提交
633
{
634 635
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
636 637
	struct ceph_mds_request *req;
	int op;
Y
Yan, Zheng 已提交
638
	int mask;
S
Sage Weil 已提交
639 640
	int err;

A
Al Viro 已提交
641 642
	dout("lookup %p dentry %p '%pd'\n",
	     dir, dentry, dentry);
S
Sage Weil 已提交
643 644 645 646 647 648 649 650 651

	if (dentry->d_name.len > NAME_MAX)
		return ERR_PTR(-ENAMETOOLONG);

	err = ceph_init_dentry(dentry);
	if (err < 0)
		return ERR_PTR(err);

	/* can we conclude ENOENT locally? */
652
	if (d_really_is_negative(dentry)) {
S
Sage Weil 已提交
653 654 655
		struct ceph_inode_info *ci = ceph_inode(dir);
		struct ceph_dentry_info *di = ceph_dentry(dentry);

656
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
657 658
		dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
		if (strncmp(dentry->d_name.name,
659
			    fsc->mount_options->snapdir_name,
S
Sage Weil 已提交
660
			    dentry->d_name.len) &&
S
Sage Weil 已提交
661
		    !is_root_ceph_dentry(dir, dentry) &&
662
		    ceph_test_mount_opt(fsc, DCACHE) &&
663
		    __ceph_dir_is_complete(ci) &&
S
Sage Weil 已提交
664
		    (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
665
			spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
666 667 668 669 670
			dout(" dir %p complete, -ENOENT\n", dir);
			d_add(dentry, NULL);
			di->lease_shared_gen = ci->i_shared_gen;
			return NULL;
		}
671
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
672 673 674 675 676 677
	}

	op = ceph_snap(dir) == CEPH_SNAPDIR ?
		CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
	req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
	if (IS_ERR(req))
J
Julia Lawall 已提交
678
		return ERR_CAST(req);
S
Sage Weil 已提交
679 680
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
Y
Yan, Zheng 已提交
681 682 683 684 685 686

	mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
	if (ceph_security_xattr_wanted(dir))
		mask |= CEPH_CAP_XATTR_SHARED;
	req->r_args.getattr.mask = cpu_to_le32(mask);

S
Sage Weil 已提交
687 688
	req->r_locked_dir = dir;
	err = ceph_mdsc_do_request(mdsc, NULL, req);
689
	err = ceph_handle_snapdir(req, dentry, err);
S
Sage Weil 已提交
690 691 692 693 694 695 696 697 698 699 700 701
	dentry = ceph_finish_lookup(req, dentry, err);
	ceph_mdsc_put_request(req);  /* will dput(dentry) */
	dout("lookup result=%p\n", dentry);
	return dentry;
}

/*
 * If we do a create but get no trace back from the MDS, follow up with
 * a lookup (the VFS expects us to link up the provided dentry).
 */
int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
{
A
Al Viro 已提交
702
	struct dentry *result = ceph_lookup(dir, dentry, 0);
S
Sage Weil 已提交
703 704 705 706 707

	if (result && !IS_ERR(result)) {
		/*
		 * We created the item, then did a lookup, and found
		 * it was already linked to another inode we already
708 709 710 711 712 713 714 715
		 * had in our cache (and thus got spliced). To not
		 * confuse VFS (especially when inode is a directory),
		 * we don't link our dentry to that inode, return an
		 * error instead.
		 *
		 * This event should be rare and it happens only when
		 * we talk to old MDS. Recent MDS does not send traceless
		 * reply for request that creates new inode.
S
Sage Weil 已提交
716
		 */
Y
Yan, Zheng 已提交
717
		d_drop(result);
718
		return -ESTALE;
S
Sage Weil 已提交
719 720 721 722 723
	}
	return PTR_ERR(result);
}

static int ceph_mknod(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
724
		      umode_t mode, dev_t rdev)
S
Sage Weil 已提交
725
{
726 727
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
728
	struct ceph_mds_request *req;
729
	struct ceph_acls_info acls = {};
S
Sage Weil 已提交
730 731 732 733 734
	int err;

	if (ceph_snap(dir) != CEPH_NOSNAP)
		return -EROFS;

735 736 737 738
	err = ceph_pre_init_acls(dir, &mode, &acls);
	if (err < 0)
		return err;

A
Al Viro 已提交
739
	dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
S
Sage Weil 已提交
740 741 742
	     dir, dentry, mode, rdev);
	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
	if (IS_ERR(req)) {
743 744
		err = PTR_ERR(req);
		goto out;
S
Sage Weil 已提交
745 746 747 748 749 750 751 752
	}
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
	req->r_locked_dir = dir;
	req->r_args.mknod.mode = cpu_to_le32(mode);
	req->r_args.mknod.rdev = cpu_to_le32(rdev);
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
753 754 755 756
	if (acls.pagelist) {
		req->r_pagelist = acls.pagelist;
		acls.pagelist = NULL;
	}
S
Sage Weil 已提交
757 758 759 760
	err = ceph_mdsc_do_request(mdsc, dir, req);
	if (!err && !req->r_reply_info.head->is_dentry)
		err = ceph_handle_notrace_create(dir, dentry);
	ceph_mdsc_put_request(req);
761
out:
G
Guangliang Zhao 已提交
762
	if (!err)
763
		ceph_init_inode_acls(d_inode(dentry), &acls);
764
	else
S
Sage Weil 已提交
765
		d_drop(dentry);
766
	ceph_release_acls_info(&acls);
S
Sage Weil 已提交
767 768 769
	return err;
}

A
Al Viro 已提交
770
static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
771
		       bool excl)
S
Sage Weil 已提交
772
{
773
	return ceph_mknod(dir, dentry, mode, 0);
S
Sage Weil 已提交
774 775 776 777 778
}

static int ceph_symlink(struct inode *dir, struct dentry *dentry,
			    const char *dest)
{
779 780
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
781 782 783 784 785 786 787 788 789
	struct ceph_mds_request *req;
	int err;

	if (ceph_snap(dir) != CEPH_NOSNAP)
		return -EROFS;

	dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
	if (IS_ERR(req)) {
790 791
		err = PTR_ERR(req);
		goto out;
S
Sage Weil 已提交
792
	}
793
	req->r_path2 = kstrdup(dest, GFP_KERNEL);
794 795 796 797 798
	if (!req->r_path2) {
		err = -ENOMEM;
		ceph_mdsc_put_request(req);
		goto out;
	}
S
Sage Weil 已提交
799
	req->r_locked_dir = dir;
800 801
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
S
Sage Weil 已提交
802 803 804 805 806 807
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
	err = ceph_mdsc_do_request(mdsc, dir, req);
	if (!err && !req->r_reply_info.head->is_dentry)
		err = ceph_handle_notrace_create(dir, dentry);
	ceph_mdsc_put_request(req);
808 809
out:
	if (err)
S
Sage Weil 已提交
810 811 812 813
		d_drop(dentry);
	return err;
}

814
static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
S
Sage Weil 已提交
815
{
816 817
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
818
	struct ceph_mds_request *req;
819
	struct ceph_acls_info acls = {};
S
Sage Weil 已提交
820 821 822 823 824 825
	int err = -EROFS;
	int op;

	if (ceph_snap(dir) == CEPH_SNAPDIR) {
		/* mkdir .snap/foo is a MKSNAP */
		op = CEPH_MDS_OP_MKSNAP;
A
Al Viro 已提交
826 827
		dout("mksnap dir %p snap '%pd' dn %p\n", dir,
		     dentry, dentry);
S
Sage Weil 已提交
828
	} else if (ceph_snap(dir) == CEPH_NOSNAP) {
829
		dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
S
Sage Weil 已提交
830 831 832 833
		op = CEPH_MDS_OP_MKDIR;
	} else {
		goto out;
	}
834 835 836 837 838 839

	mode |= S_IFDIR;
	err = ceph_pre_init_acls(dir, &mode, &acls);
	if (err < 0)
		goto out;

S
Sage Weil 已提交
840 841 842 843 844 845 846 847 848 849 850 851
	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
	if (IS_ERR(req)) {
		err = PTR_ERR(req);
		goto out;
	}

	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
	req->r_locked_dir = dir;
	req->r_args.mkdir.mode = cpu_to_le32(mode);
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
852 853 854 855
	if (acls.pagelist) {
		req->r_pagelist = acls.pagelist;
		acls.pagelist = NULL;
	}
S
Sage Weil 已提交
856
	err = ceph_mdsc_do_request(mdsc, dir, req);
Y
Yan, Zheng 已提交
857 858 859
	if (!err &&
	    !req->r_reply_info.head->is_target &&
	    !req->r_reply_info.head->is_dentry)
S
Sage Weil 已提交
860 861 862
		err = ceph_handle_notrace_create(dir, dentry);
	ceph_mdsc_put_request(req);
out:
863
	if (!err)
864
		ceph_init_inode_acls(d_inode(dentry), &acls);
865
	else
S
Sage Weil 已提交
866
		d_drop(dentry);
867
	ceph_release_acls_info(&acls);
S
Sage Weil 已提交
868 869 870 871 872 873
	return err;
}

static int ceph_link(struct dentry *old_dentry, struct inode *dir,
		     struct dentry *dentry)
{
874 875
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
	struct ceph_mds_request *req;
	int err;

	if (ceph_snap(dir) != CEPH_NOSNAP)
		return -EROFS;

	dout("link in dir %p old_dentry %p dentry %p\n", dir,
	     old_dentry, dentry);
	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
	if (IS_ERR(req)) {
		d_drop(dentry);
		return PTR_ERR(req);
	}
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
891
	req->r_old_dentry = dget(old_dentry);
S
Sage Weil 已提交
892 893 894
	req->r_locked_dir = dir;
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
895 896
	/* release LINK_SHARED on source inode (mds will lock it) */
	req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
S
Sage Weil 已提交
897
	err = ceph_mdsc_do_request(mdsc, dir, req);
898
	if (err) {
S
Sage Weil 已提交
899
		d_drop(dentry);
900
	} else if (!req->r_reply_info.head->is_dentry) {
901 902
		ihold(d_inode(old_dentry));
		d_instantiate(dentry, d_inode(old_dentry));
903
	}
S
Sage Weil 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
	ceph_mdsc_put_request(req);
	return err;
}

/*
 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps.  If it
 * looks like the link count will hit 0, drop any other caps (other
 * than PIN) we don't specifically want (due to the file still being
 * open).
 */
static int drop_caps_for_unlink(struct inode *inode)
{
	struct ceph_inode_info *ci = ceph_inode(inode);
	int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;

919
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
920 921 922 923
	if (inode->i_nlink == 1) {
		drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
		ci->i_ceph_flags |= CEPH_I_NODELAY;
	}
924
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
925 926 927 928 929 930 931 932
	return drop;
}

/*
 * rmdir and unlink are differ only by the metadata op code
 */
static int ceph_unlink(struct inode *dir, struct dentry *dentry)
{
933 934
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
935
	struct inode *inode = d_inode(dentry);
S
Sage Weil 已提交
936 937 938 939 940 941
	struct ceph_mds_request *req;
	int err = -EROFS;
	int op;

	if (ceph_snap(dir) == CEPH_SNAPDIR) {
		/* rmdir .snap/foo is RMSNAP */
A
Al Viro 已提交
942
		dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
S
Sage Weil 已提交
943 944 945 946
		op = CEPH_MDS_OP_RMSNAP;
	} else if (ceph_snap(dir) == CEPH_NOSNAP) {
		dout("unlink/rmdir dir %p dn %p inode %p\n",
		     dir, dentry, inode);
947
		op = d_is_dir(dentry) ?
S
Sage Weil 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
			CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
	} else
		goto out;
	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
	if (IS_ERR(req)) {
		err = PTR_ERR(req);
		goto out;
	}
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
	req->r_locked_dir = dir;
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
	req->r_inode_drop = drop_caps_for_unlink(inode);
	err = ceph_mdsc_do_request(mdsc, dir, req);
	if (!err && !req->r_reply_info.head->is_dentry)
		d_delete(dentry);
	ceph_mdsc_put_request(req);
out:
	return err;
}

static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
		       struct inode *new_dir, struct dentry *new_dentry)
{
973 974
	struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
975
	struct ceph_mds_request *req;
Y
Yan, Zheng 已提交
976
	int op = CEPH_MDS_OP_RENAME;
S
Sage Weil 已提交
977 978 979 980
	int err;

	if (ceph_snap(old_dir) != ceph_snap(new_dir))
		return -EXDEV;
Y
Yan, Zheng 已提交
981 982 983 984 985 986
	if (ceph_snap(old_dir) != CEPH_NOSNAP) {
		if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
			op = CEPH_MDS_OP_RENAMESNAP;
		else
			return -EROFS;
	}
S
Sage Weil 已提交
987 988
	dout("rename dir %p dentry %p to dir %p dentry %p\n",
	     old_dir, old_dentry, new_dir, new_dentry);
Y
Yan, Zheng 已提交
989
	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
S
Sage Weil 已提交
990 991
	if (IS_ERR(req))
		return PTR_ERR(req);
992
	ihold(old_dir);
S
Sage Weil 已提交
993 994 995
	req->r_dentry = dget(new_dentry);
	req->r_num_caps = 2;
	req->r_old_dentry = dget(old_dentry);
996
	req->r_old_dentry_dir = old_dir;
S
Sage Weil 已提交
997 998 999 1000 1001 1002 1003
	req->r_locked_dir = new_dir;
	req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
	/* release LINK_RDCACHE on source inode (mds will lock it) */
	req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
1004 1005
	if (d_really_is_positive(new_dentry))
		req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
S
Sage Weil 已提交
1006 1007 1008 1009 1010 1011 1012
	err = ceph_mdsc_do_request(mdsc, old_dir, req);
	if (!err && !req->r_reply_info.head->is_dentry) {
		/*
		 * Normally d_move() is done by fill_trace (called by
		 * do_request, above).  If there is no trace, we need
		 * to do it here.
		 */
1013

Y
Yan, Zheng 已提交
1014 1015 1016 1017
		/* d_move screws up sibling dentries' offsets */
		ceph_dir_clear_complete(old_dir);
		ceph_dir_clear_complete(new_dir);

S
Sage Weil 已提交
1018
		d_move(old_dentry, new_dentry);
1019 1020 1021

		/* ensure target dentry is invalidated, despite
		   rehashing bug in vfs_rename_dir */
1022
		ceph_invalidate_dentry_lease(new_dentry);
S
Sage Weil 已提交
1023 1024 1025 1026 1027
	}
	ceph_mdsc_put_request(req);
	return err;
}

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
/*
 * Ensure a dentry lease will no longer revalidate.
 */
void ceph_invalidate_dentry_lease(struct dentry *dentry)
{
	spin_lock(&dentry->d_lock);
	dentry->d_time = jiffies;
	ceph_dentry(dentry)->lease_shared_gen = 0;
	spin_unlock(&dentry->d_lock);
}
S
Sage Weil 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

/*
 * Check if dentry lease is valid.  If not, delete the lease.  Try to
 * renew if the least is more than half up.
 */
static int dentry_lease_is_valid(struct dentry *dentry)
{
	struct ceph_dentry_info *di;
	struct ceph_mds_session *s;
	int valid = 0;
	u32 gen;
	unsigned long ttl;
	struct ceph_mds_session *session = NULL;
	struct inode *dir = NULL;
	u32 seq = 0;

	spin_lock(&dentry->d_lock);
	di = ceph_dentry(dentry);
1056
	if (di->lease_session) {
S
Sage Weil 已提交
1057
		s = di->lease_session;
1058
		spin_lock(&s->s_gen_ttl_lock);
S
Sage Weil 已提交
1059 1060
		gen = s->s_cap_gen;
		ttl = s->s_cap_ttl;
1061
		spin_unlock(&s->s_gen_ttl_lock);
S
Sage Weil 已提交
1062 1063 1064 1065 1066 1067 1068 1069

		if (di->lease_gen == gen &&
		    time_before(jiffies, dentry->d_time) &&
		    time_before(jiffies, ttl)) {
			valid = 1;
			if (di->lease_renew_after &&
			    time_after(jiffies, di->lease_renew_after)) {
				/* we should renew */
1070
				dir = d_inode(dentry->d_parent);
S
Sage Weil 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
				session = ceph_get_mds_session(s);
				seq = di->lease_seq;
				di->lease_renew_after = 0;
				di->lease_renew_from = jiffies;
			}
		}
	}
	spin_unlock(&dentry->d_lock);

	if (session) {
		ceph_mdsc_lease_send_msg(session, dir, dentry,
					 CEPH_MDS_LEASE_RENEW, seq);
		ceph_put_mds_session(session);
	}
	dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
	return valid;
}

/*
 * Check if directory-wide content lease/cap is valid.
 */
static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
{
	struct ceph_inode_info *ci = ceph_inode(dir);
	struct ceph_dentry_info *di = ceph_dentry(dentry);
	int valid = 0;

1098
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
1099 1100
	if (ci->i_shared_gen == di->lease_shared_gen)
		valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1101
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110
	dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
	     dir, (unsigned)ci->i_shared_gen, dentry,
	     (unsigned)di->lease_shared_gen, valid);
	return valid;
}

/*
 * Check if cached dentry can be trusted.
 */
1111
static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
S
Sage Weil 已提交
1112
{
1113
	int valid = 0;
1114
	struct dentry *parent;
1115 1116
	struct inode *dir;

1117
	if (flags & LOOKUP_RCU)
1118 1119
		return -ECHILD;

A
Al Viro 已提交
1120
	dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
1121
	     dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
S
Sage Weil 已提交
1122

1123 1124
	parent = dget_parent(dentry);
	dir = d_inode(parent);
1125

S
Sage Weil 已提交
1126 1127
	/* always trust cached snapped dentries, snapdir dentry */
	if (ceph_snap(dir) != CEPH_NOSNAP) {
A
Al Viro 已提交
1128
		dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1129
		     dentry, d_inode(dentry));
1130
		valid = 1;
1131 1132
	} else if (d_really_is_positive(dentry) &&
		   ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
1133 1134 1135
		valid = 1;
	} else if (dentry_lease_is_valid(dentry) ||
		   dir_lease_is_valid(dir, dentry)) {
1136 1137
		if (d_really_is_positive(dentry))
			valid = ceph_is_any_caps(d_inode(dentry));
1138 1139
		else
			valid = 1;
S
Sage Weil 已提交
1140 1141
	}

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	if (!valid) {
		struct ceph_mds_client *mdsc =
			ceph_sb_to_client(dir->i_sb)->mdsc;
		struct ceph_mds_request *req;
		int op, mask, err;

		op = ceph_snap(dir) == CEPH_SNAPDIR ?
			CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
		req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
		if (!IS_ERR(req)) {
			req->r_dentry = dget(dentry);
			req->r_num_caps = 2;

			mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
			if (ceph_security_xattr_wanted(dir))
				mask |= CEPH_CAP_XATTR_SHARED;
			req->r_args.getattr.mask = mask;

			req->r_locked_dir = dir;
			err = ceph_mdsc_do_request(mdsc, NULL, req);
			if (err == 0 || err == -ENOENT) {
				if (dentry == req->r_dentry) {
					valid = !d_unhashed(dentry);
				} else {
					d_invalidate(req->r_dentry);
					err = -EAGAIN;
				}
			}
			ceph_mdsc_put_request(req);
			dout("d_revalidate %p lookup result=%d\n",
			     dentry, err);
		}
	}

1176
	dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1177
	if (valid) {
1178
		ceph_dentry_lru_touch(dentry);
1179 1180 1181
	} else {
		ceph_dir_clear_complete(dir);
	}
1182 1183

	dput(parent);
1184
	return valid;
S
Sage Weil 已提交
1185 1186 1187
}

/*
1188
 * Release our ceph_dentry_info.
S
Sage Weil 已提交
1189
 */
1190
static void ceph_d_release(struct dentry *dentry)
S
Sage Weil 已提交
1191 1192 1193
{
	struct ceph_dentry_info *di = ceph_dentry(dentry);

1194
	dout("d_release %p\n", dentry);
1195 1196 1197 1198 1199
	ceph_dentry_lru_del(dentry);
	if (di->lease_session)
		ceph_put_mds_session(di->lease_session);
	kmem_cache_free(ceph_dentry_cachep, di);
	dentry->d_fsdata = NULL;
S
Sage Weil 已提交
1200 1201 1202
}

static int ceph_snapdir_d_revalidate(struct dentry *dentry,
1203
					  unsigned int flags)
S
Sage Weil 已提交
1204 1205 1206 1207 1208 1209 1210 1211
{
	/*
	 * Eventually, we'll want to revalidate snapped metadata
	 * too... probably...
	 */
	return 1;
}

1212 1213 1214 1215 1216 1217 1218 1219
/*
 * When the VFS prunes a dentry from the cache, we need to clear the
 * complete flag on the parent directory.
 *
 * Called under dentry->d_lock.
 */
static void ceph_d_prune(struct dentry *dentry)
{
S
Sage Weil 已提交
1220
	dout("ceph_d_prune %p\n", dentry);
1221 1222

	/* do we have a valid parent? */
1223
	if (IS_ROOT(dentry))
1224 1225
		return;

1226
	/* if we are not hashed, we don't affect dir's completeness */
1227 1228
	if (d_unhashed(dentry))
		return;
S
Sage Weil 已提交
1229

1230 1231 1232 1233
	/*
	 * we hold d_lock, so d_parent is stable, and d_fsdata is never
	 * cleared until d_release
	 */
1234
	ceph_dir_clear_complete(d_inode(dentry->d_parent));
1235
}
S
Sage Weil 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244

/*
 * read() on a dir.  This weird interface hack only works if mounted
 * with '-o dirstat'.
 */
static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
			     loff_t *ppos)
{
	struct ceph_file_info *cf = file->private_data;
A
Al Viro 已提交
1245
	struct inode *inode = file_inode(file);
S
Sage Weil 已提交
1246 1247
	struct ceph_inode_info *ci = ceph_inode(inode);
	int left;
1248
	const int bufsize = 1024;
S
Sage Weil 已提交
1249

1250
	if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
S
Sage Weil 已提交
1251 1252 1253
		return -EISDIR;

	if (!cf->dir_info) {
1254
		cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
S
Sage Weil 已提交
1255 1256 1257
		if (!cf->dir_info)
			return -ENOMEM;
		cf->dir_info_len =
1258
			snprintf(cf->dir_info, bufsize,
S
Sage Weil 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
				"entries:   %20lld\n"
				" files:    %20lld\n"
				" subdirs:  %20lld\n"
				"rentries:  %20lld\n"
				" rfiles:   %20lld\n"
				" rsubdirs: %20lld\n"
				"rbytes:    %20lld\n"
				"rctime:    %10ld.%09ld\n",
				ci->i_files + ci->i_subdirs,
				ci->i_files,
				ci->i_subdirs,
				ci->i_rfiles + ci->i_rsubdirs,
				ci->i_rfiles,
				ci->i_rsubdirs,
				ci->i_rbytes,
				(long)ci->i_rctime.tv_sec,
				(long)ci->i_rctime.tv_nsec);
	}

	if (*ppos >= cf->dir_info_len)
		return 0;
	size = min_t(unsigned, size, cf->dir_info_len-*ppos);
	left = copy_to_user(buf, cf->dir_info + *ppos, size);
	if (left == size)
		return -EFAULT;
	*ppos += (size - left);
	return size - left;
}

/*
 * We maintain a private dentry LRU.
 *
 * FIXME: this needs to be changed to a per-mds lru to be useful.
 */
void ceph_dentry_lru_add(struct dentry *dn)
{
	struct ceph_dentry_info *di = ceph_dentry(dn);
	struct ceph_mds_client *mdsc;

A
Al Viro 已提交
1298
	dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
1299 1300 1301 1302 1303
	mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
	spin_lock(&mdsc->dentry_lru_lock);
	list_add_tail(&di->lru, &mdsc->dentry_lru);
	mdsc->num_dentry++;
	spin_unlock(&mdsc->dentry_lru_lock);
S
Sage Weil 已提交
1304 1305 1306 1307 1308 1309 1310
}

void ceph_dentry_lru_touch(struct dentry *dn)
{
	struct ceph_dentry_info *di = ceph_dentry(dn);
	struct ceph_mds_client *mdsc;

A
Al Viro 已提交
1311 1312
	dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
	     di->offset);
1313 1314 1315 1316
	mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
	spin_lock(&mdsc->dentry_lru_lock);
	list_move_tail(&di->lru, &mdsc->dentry_lru);
	spin_unlock(&mdsc->dentry_lru_lock);
S
Sage Weil 已提交
1317 1318 1319 1320 1321 1322 1323
}

void ceph_dentry_lru_del(struct dentry *dn)
{
	struct ceph_dentry_info *di = ceph_dentry(dn);
	struct ceph_mds_client *mdsc;

A
Al Viro 已提交
1324
	dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
1325 1326 1327 1328 1329
	mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
	spin_lock(&mdsc->dentry_lru_lock);
	list_del_init(&di->lru);
	mdsc->num_dentry--;
	spin_unlock(&mdsc->dentry_lru_lock);
S
Sage Weil 已提交
1330 1331
}

S
Sage Weil 已提交
1332 1333 1334 1335
/*
 * Return name hash for a given dentry.  This is dependent on
 * the parent directory's hash function.
 */
1336
unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
S
Sage Weil 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
{
	struct ceph_inode_info *dci = ceph_inode(dir);

	switch (dci->i_dir_layout.dl_dir_hash) {
	case 0:	/* for backward compat */
	case CEPH_STR_HASH_LINUX:
		return dn->d_name.hash;

	default:
		return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
				     dn->d_name.name, dn->d_name.len);
	}
}

S
Sage Weil 已提交
1351 1352
const struct file_operations ceph_dir_fops = {
	.read = ceph_read_dir,
A
Al Viro 已提交
1353
	.iterate = ceph_readdir,
S
Sage Weil 已提交
1354 1355 1356 1357
	.llseek = ceph_dir_llseek,
	.open = ceph_open,
	.release = ceph_release,
	.unlocked_ioctl = ceph_ioctl,
Y
Yan, Zheng 已提交
1358
	.fsync = ceph_fsync,
S
Sage Weil 已提交
1359 1360
};

1361 1362 1363 1364 1365 1366 1367
const struct file_operations ceph_snapdir_fops = {
	.iterate = ceph_readdir,
	.llseek = ceph_dir_llseek,
	.open = ceph_open,
	.release = ceph_release,
};

S
Sage Weil 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376
const struct inode_operations ceph_dir_iops = {
	.lookup = ceph_lookup,
	.permission = ceph_permission,
	.getattr = ceph_getattr,
	.setattr = ceph_setattr,
	.setxattr = ceph_setxattr,
	.getxattr = ceph_getxattr,
	.listxattr = ceph_listxattr,
	.removexattr = ceph_removexattr,
G
Guangliang Zhao 已提交
1377
	.get_acl = ceph_get_acl,
S
Sage Weil 已提交
1378
	.set_acl = ceph_set_acl,
S
Sage Weil 已提交
1379 1380 1381 1382 1383 1384 1385 1386
	.mknod = ceph_mknod,
	.symlink = ceph_symlink,
	.mkdir = ceph_mkdir,
	.link = ceph_link,
	.unlink = ceph_unlink,
	.rmdir = ceph_unlink,
	.rename = ceph_rename,
	.create = ceph_create,
1387
	.atomic_open = ceph_atomic_open,
S
Sage Weil 已提交
1388 1389
};

1390 1391 1392 1393 1394 1395
const struct inode_operations ceph_snapdir_iops = {
	.lookup = ceph_lookup,
	.permission = ceph_permission,
	.getattr = ceph_getattr,
	.mkdir = ceph_mkdir,
	.rmdir = ceph_unlink,
Y
Yan, Zheng 已提交
1396
	.rename = ceph_rename,
1397 1398
};

S
Sage Weil 已提交
1399
const struct dentry_operations ceph_dentry_ops = {
S
Sage Weil 已提交
1400
	.d_revalidate = ceph_d_revalidate,
1401
	.d_release = ceph_d_release,
1402
	.d_prune = ceph_d_prune,
S
Sage Weil 已提交
1403 1404
};

S
Sage Weil 已提交
1405
const struct dentry_operations ceph_snapdir_dentry_ops = {
S
Sage Weil 已提交
1406
	.d_revalidate = ceph_snapdir_d_revalidate,
1407
	.d_release = ceph_d_release,
S
Sage Weil 已提交
1408 1409
};

S
Sage Weil 已提交
1410
const struct dentry_operations ceph_snap_dentry_ops = {
1411
	.d_release = ceph_d_release,
1412
	.d_prune = ceph_d_prune,
S
Sage Weil 已提交
1413
};