dir.c 39.5 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
Sage Weil's avatar
Sage Weil committed
2 3 4 5

#include <linux/spinlock.h>
#include <linux/fs_struct.h>
#include <linux/namei.h>
6
#include <linux/slab.h>
Sage Weil's avatar
Sage Weil committed
7
#include <linux/sched.h>
8
#include <linux/xattr.h>
Sage Weil's avatar
Sage Weil committed
9 10

#include "super.h"
11
#include "mds_client.h"
Sage Weil's avatar
Sage Weil committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

/*
 * 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.
 */

Sage Weil's avatar
Sage Weil committed
30
const struct dentry_operations ceph_dentry_ops;
Sage Weil's avatar
Sage Weil committed
31 32 33 34

/*
 * Initialize ceph dentry state.
 */
Al Viro's avatar
Al Viro committed
35
static int ceph_d_init(struct dentry *dentry)
Sage Weil's avatar
Sage Weil committed
36 37 38
{
	struct ceph_dentry_info *di;

Geliang Tang's avatar
Geliang Tang committed
39
	di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
Sage Weil's avatar
Sage Weil committed
40 41 42 43 44
	if (!di)
		return -ENOMEM;          /* oh well */

	di->dentry = dentry;
	di->lease_session = NULL;
Miklos Szeredi's avatar
Miklos Szeredi committed
45
	di->time = jiffies;
46
	dentry->d_fsdata = di;
Sage Weil's avatar
Sage Weil committed
47 48 49 50 51
	ceph_dentry_lru_add(dentry);
	return 0;
}

/*
52 53 54 55 56 57
 * for f_pos for readdir:
 * - hash order:
 *	(0xff << 52) | ((24 bits hash) << 28) |
 *	(the nth entry has hash collision);
 * - frag+name order;
 *	((frag value) << 28) | (the nth entry in frag);
Sage Weil's avatar
Sage Weil committed
58
 */
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#define OFFSET_BITS	28
#define OFFSET_MASK	((1 << OFFSET_BITS) - 1)
#define HASH_ORDER	(0xffull << (OFFSET_BITS + 24))
loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
{
	loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
	if (hash_order)
		fpos |= HASH_ORDER;
	return fpos;
}

static bool is_hash_order(loff_t p)
{
	return (p & HASH_ORDER) == HASH_ORDER;
}

Sage Weil's avatar
Sage Weil committed
75 76
static unsigned fpos_frag(loff_t p)
{
77
	return p >> OFFSET_BITS;
Sage Weil's avatar
Sage Weil committed
78
}
79 80 81 82 83 84

static unsigned fpos_hash(loff_t p)
{
	return ceph_frag_value(fpos_frag(p));
}

Sage Weil's avatar
Sage Weil committed
85 86
static unsigned fpos_off(loff_t p)
{
87
	return p & OFFSET_MASK;
Sage Weil's avatar
Sage Weil committed
88 89
}

Yan, Zheng's avatar
Yan, Zheng committed
90 91 92 93 94 95 96 97
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));
}

Yan, Zheng's avatar
Yan, Zheng committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*
 * 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;
}

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 157 158 159 160 161 162

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);
}

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

186
	dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
Sage Weil's avatar
Sage Weil committed
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	/* 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);
		}
Sage Weil's avatar
Sage Weil committed
215

216
		dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
Sage Weil's avatar
Sage Weil committed
217 218
	}

Yan, Zheng's avatar
Yan, Zheng committed
219

220 221 222 223
	for (;;) {
		bool emit_dentry = false;
		dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
		if (!dentry) {
224
			fi->flags |= CEPH_F_ATEND;
Yan, Zheng's avatar
Yan, Zheng committed
225 226
			err = 0;
			break;
Sage Weil's avatar
Sage Weil committed
227
		}
228 229 230
		if (IS_ERR(dentry)) {
			err = PTR_ERR(dentry);
			goto out;
Yan, Zheng's avatar
Yan, Zheng committed
231 232 233 234
		}

		di = ceph_dentry(dentry);
		spin_lock(&dentry->d_lock);
235
		if (di->lease_shared_gen == shared_gen &&
Yan, Zheng's avatar
Yan, Zheng committed
236 237 238 239
		    d_really_is_positive(dentry) &&
		    fpos_cmp(ctx->pos, di->offset) <= 0) {
			emit_dentry = true;
		}
Nick Piggin's avatar
Nick Piggin committed
240
		spin_unlock(&dentry->d_lock);
Sage Weil's avatar
Sage Weil committed
241

Yan, Zheng's avatar
Yan, Zheng committed
242
		if (emit_dentry) {
243
			dout(" %llx dentry %p %pd %p\n", di->offset,
Yan, Zheng's avatar
Yan, Zheng committed
244 245 246 247 248 249 250 251 252 253 254 255
			     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++;
256

Yan, Zheng's avatar
Yan, Zheng committed
257 258 259 260 261
			if (last)
				dput(last);
			last = dentry;
		} else {
			dput(dentry);
Sage Weil's avatar
Sage Weil committed
262
		}
Yan, Zheng's avatar
Yan, Zheng committed
263
	}
264
out:
Yan, Zheng's avatar
Yan, Zheng committed
265 266 267 268 269 270 271 272
	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;
Sage Weil's avatar
Sage Weil committed
273
		dput(last);
Yan, Zheng's avatar
Yan, Zheng committed
274
	}
Sage Weil's avatar
Sage Weil committed
275 276 277
	return err;
}

278 279 280 281 282 283 284 285 286 287
static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
{
	if (!fi->last_readdir)
		return true;
	if (is_hash_order(pos))
		return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
	else
		return fi->frag != fpos_frag(pos);
}

Al Viro's avatar
Al Viro committed
288
static int ceph_readdir(struct file *file, struct dir_context *ctx)
Sage Weil's avatar
Sage Weil committed
289
{
Al Viro's avatar
Al Viro committed
290 291
	struct ceph_file_info *fi = file->private_data;
	struct inode *inode = file_inode(file);
Sage Weil's avatar
Sage Weil committed
292
	struct ceph_inode_info *ci = ceph_inode(inode);
293 294
	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
	struct ceph_mds_client *mdsc = fsc->mdsc;
295
	int i;
Sage Weil's avatar
Sage Weil committed
296 297 298 299
	int err;
	u32 ftype;
	struct ceph_mds_reply_info_parsed *rinfo;

300
	dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
301
	if (fi->flags & CEPH_F_ATEND)
Sage Weil's avatar
Sage Weil committed
302 303 304
		return 0;

	/* always start with . and .. */
Al Viro's avatar
Al Viro committed
305
	if (ctx->pos == 0) {
Sage Weil's avatar
Sage Weil committed
306
		dout("readdir off 0 -> '.'\n");
Al Viro's avatar
Al Viro committed
307
		if (!dir_emit(ctx, ".", 1, 
Yehuda Sadeh's avatar
Yehuda Sadeh committed
308
			    ceph_translate_ino(inode->i_sb, inode->i_ino),
Al Viro's avatar
Al Viro committed
309
			    inode->i_mode >> 12))
Sage Weil's avatar
Sage Weil committed
310
			return 0;
Al Viro's avatar
Al Viro committed
311
		ctx->pos = 1;
Sage Weil's avatar
Sage Weil committed
312
	}
Al Viro's avatar
Al Viro committed
313
	if (ctx->pos == 1) {
Al Viro's avatar
Al Viro committed
314
		ino_t ino = parent_ino(file->f_path.dentry);
Sage Weil's avatar
Sage Weil committed
315
		dout("readdir off 1 -> '..'\n");
Al Viro's avatar
Al Viro committed
316
		if (!dir_emit(ctx, "..", 2,
Yehuda Sadeh's avatar
Yehuda Sadeh committed
317
			    ceph_translate_ino(inode->i_sb, ino),
Al Viro's avatar
Al Viro committed
318
			    inode->i_mode >> 12))
Sage Weil's avatar
Sage Weil committed
319
			return 0;
Al Viro's avatar
Al Viro committed
320
		ctx->pos = 2;
Sage Weil's avatar
Sage Weil committed
321 322 323
	}

	/* can we use the dcache? */
324
	spin_lock(&ci->i_ceph_lock);
Yan, Zheng's avatar
Yan, Zheng committed
325
	if (ceph_test_mount_opt(fsc, DCACHE) &&
326
	    !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
327
	    ceph_snap(inode) != CEPH_SNAPDIR &&
328
	    __ceph_dir_is_complete_ordered(ci) &&
Sage Weil's avatar
Sage Weil committed
329
	    __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
330
		u32 shared_gen = ci->i_shared_gen;
331
		spin_unlock(&ci->i_ceph_lock);
332
		err = __dcache_readdir(file, ctx, shared_gen);
333
		if (err != -EAGAIN)
Sage Weil's avatar
Sage Weil committed
334
			return err;
335
	} else {
336
		spin_unlock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
337 338 339 340 341
	}

	/* proceed with a normal readdir */
more:
	/* do we have the correct frag content buffered? */
342
	if (need_send_readdir(fi, ctx->pos)) {
Sage Weil's avatar
Sage Weil committed
343
		struct ceph_mds_request *req;
344
		unsigned frag;
Sage Weil's avatar
Sage Weil committed
345 346 347 348
		int op = ceph_snap(inode) == CEPH_SNAPDIR ?
			CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;

		/* discard old result, if any */
349
		if (fi->last_readdir) {
Sage Weil's avatar
Sage Weil committed
350
			ceph_mdsc_put_request(fi->last_readdir);
351 352
			fi->last_readdir = NULL;
		}
Sage Weil's avatar
Sage Weil committed
353

354 355 356 357 358 359 360
		if (is_hash_order(ctx->pos)) {
			frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
						NULL, NULL);
		} else {
			frag = fpos_frag(ctx->pos);
		}

Sage Weil's avatar
Sage Weil committed
361 362 363 364 365
		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);
366 367 368 369 370
		err = ceph_alloc_readdir_reply_buffer(req, inode);
		if (err) {
			ceph_mdsc_put_request(req);
			return err;
		}
Sage Weil's avatar
Sage Weil committed
371 372 373
		/* hints to request -> mds selection code */
		req->r_direct_mode = USE_AUTH_MDS;
		req->r_direct_hash = ceph_frag_value(frag);
374
		__set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
375
		if (fi->last_name) {
376
			req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
377 378 379 380 381
			if (!req->r_path2) {
				ceph_mdsc_put_request(req);
				return -ENOMEM;
			}
		}
Yan, Zheng's avatar
Yan, Zheng committed
382 383 384
		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;
Sage Weil's avatar
Sage Weil committed
385 386
		req->r_readdir_offset = fi->next_offset;
		req->r_args.readdir.frag = cpu_to_le32(frag);
387 388
		req->r_args.readdir.flags =
				cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
389 390 391 392

		req->r_inode = inode;
		ihold(inode);
		req->r_dentry = dget(file->f_path.dentry);
Sage Weil's avatar
Sage Weil committed
393 394 395 396 397
		err = ceph_mdsc_do_request(mdsc, NULL, req);
		if (err < 0) {
			ceph_mdsc_put_request(req);
			return err;
		}
398 399 400
		dout("readdir got and parsed readdir result=%d on "
		     "frag %x, end=%d, complete=%d, hash_order=%d\n",
		     err, frag,
Sage Weil's avatar
Sage Weil committed
401
		     (int)req->r_reply_info.dir_end,
402 403
		     (int)req->r_reply_info.dir_complete,
		     (int)req->r_reply_info.hash_order);
Sage Weil's avatar
Sage Weil committed
404

405 406 407
		rinfo = &req->r_reply_info;
		if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
			frag = le32_to_cpu(rinfo->dir_dir->frag);
408 409 410 411 412 413 414
			if (!rinfo->hash_order) {
				fi->next_offset = req->r_readdir_offset;
				/* adjust ctx->pos to beginning of frag */
				ctx->pos = ceph_make_fpos(frag,
							  fi->next_offset,
							  false);
			}
415
		}
Yan, Zheng's avatar
Yan, Zheng committed
416

Yan, Zheng's avatar
Yan, Zheng committed
417
		fi->frag = frag;
Sage Weil's avatar
Sage Weil committed
418 419
		fi->last_readdir = req;

420
		if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
Yan, Zheng's avatar
Yan, Zheng committed
421 422 423 424
			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;
425 426
			} else if (ceph_frag_is_leftmost(frag) &&
				   fi->next_offset == 2) {
Yan, Zheng's avatar
Yan, Zheng committed
427 428 429 430 431 432 433 434 435 436 437 438 439
				/* 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;
		}

440 441
		/* note next offset and last dentry name */
		if (rinfo->dir_nr > 0) {
442 443
			struct ceph_mds_reply_dir_entry *rde =
					rinfo->dir_entries + (rinfo->dir_nr-1);
444 445
			unsigned next_offset = req->r_reply_info.dir_end ?
					2 : (fpos_off(rde->offset) + 1);
446
			err = note_last_dentry(fi, rde->name, rde->name_len,
447
					       next_offset);
Sage Weil's avatar
Sage Weil committed
448 449
			if (err)
				return err;
450 451 452
		} else if (req->r_reply_info.dir_end) {
			fi->next_offset = 2;
			/* keep last name */
Sage Weil's avatar
Sage Weil committed
453 454 455 456
		}
	}

	rinfo = &fi->last_readdir->r_reply_info;
457
	dout("readdir frag %x num %d pos %llx chunk first %llx\n",
458
	     fi->frag, rinfo->dir_nr, ctx->pos,
459
	     rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
Al Viro's avatar
Al Viro committed
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
	i = 0;
	/* search start position */
	if (rinfo->dir_nr > 0) {
		int step, nr = rinfo->dir_nr;
		while (nr > 0) {
			step = nr >> 1;
			if (rinfo->dir_entries[i + step].offset < ctx->pos) {
				i +=  step + 1;
				nr -= step + 1;
			} else {
				nr = step;
			}
		}
	}
	for (; i < rinfo->dir_nr; i++) {
		struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
477 478 479
		struct ceph_vino vino;
		ino_t ino;

480 481 482 483 484
		BUG_ON(rde->offset < ctx->pos);

		ctx->pos = rde->offset;
		dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
		     i, rinfo->dir_nr, ctx->pos,
485
		     rde->name_len, rde->name, &rde->inode.in);
486

487 488 489 490
		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);
491
		ino = ceph_vino_to_ino(vino);
492

493 494
		if (!dir_emit(ctx, rde->name, rde->name_len,
			      ceph_translate_ino(inode->i_sb, ino), ftype)) {
Sage Weil's avatar
Sage Weil committed
495 496 497
			dout("filldir stopping us...\n");
			return 0;
		}
Al Viro's avatar
Al Viro committed
498
		ctx->pos++;
Sage Weil's avatar
Sage Weil committed
499 500
	}

501
	if (fi->next_offset > 2) {
Sage Weil's avatar
Sage Weil committed
502 503 504 505 506 507
		ceph_mdsc_put_request(fi->last_readdir);
		fi->last_readdir = NULL;
		goto more;
	}

	/* more frags? */
508 509 510 511 512 513 514 515 516 517 518 519 520
	if (!ceph_frag_is_rightmost(fi->frag)) {
		unsigned frag = ceph_frag_next(fi->frag);
		if (is_hash_order(ctx->pos)) {
			loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
							fi->next_offset, true);
			if (new_pos > ctx->pos)
				ctx->pos = new_pos;
			/* keep last_name */
		} else {
			ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
			kfree(fi->last_name);
			fi->last_name = NULL;
		}
Sage Weil's avatar
Sage Weil committed
521 522 523
		dout("readdir next frag is %x\n", frag);
		goto more;
	}
524
	fi->flags |= CEPH_F_ATEND;
Sage Weil's avatar
Sage Weil committed
525 526 527 528 529 530

	/*
	 * 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.
	 */
Yan, Zheng's avatar
Yan, Zheng committed
531 532 533
	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)) {
534
			dout(" marking %p complete and ordered\n", inode);
Yan, Zheng's avatar
Yan, Zheng committed
535 536 537 538 539 540
			/* 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 {
541
			dout(" marking %p complete\n", inode);
Yan, Zheng's avatar
Yan, Zheng committed
542
		}
543 544
		__ceph_dir_set_complete(ci, fi->dir_release_count,
					fi->dir_ordered_count);
Yan, Zheng's avatar
Yan, Zheng committed
545
		spin_unlock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
546 547
	}

Al Viro's avatar
Al Viro committed
548
	dout("readdir %p file %p done.\n", inode, file);
Sage Weil's avatar
Sage Weil committed
549 550 551
	return 0;
}

552
static void reset_readdir(struct ceph_file_info *fi)
Sage Weil's avatar
Sage Weil committed
553 554 555 556 557 558
{
	if (fi->last_readdir) {
		ceph_mdsc_put_request(fi->last_readdir);
		fi->last_readdir = NULL;
	}
	kfree(fi->last_name);
Sage Weil's avatar
Sage Weil committed
559
	fi->last_name = NULL;
Yan, Zheng's avatar
Yan, Zheng committed
560 561
	fi->dir_release_count = 0;
	fi->readdir_cache_idx = -1;
Yan, Zheng's avatar
Yan, Zheng committed
562
	fi->next_offset = 2;  /* compensate for . and .. */
563
	fi->flags &= ~CEPH_F_ATEND;
Sage Weil's avatar
Sage Weil committed
564 565
}

566 567 568 569 570 571 572
/*
 * discard buffered readdir content on seekdir(0), or seek to new frag,
 * or seek prior to current chunk
 */
static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
{
	struct ceph_mds_reply_info_parsed *rinfo;
573
	loff_t chunk_offset;
574 575
	if (new_pos == 0)
		return true;
576 577 578
	if (is_hash_order(new_pos)) {
		/* no need to reset last_name for a forward seek when
		 * dentries are sotred in hash order */
579
	} else if (fi->frag != fpos_frag(new_pos)) {
580
		return true;
581
	}
582 583 584
	rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
	if (!rinfo || !rinfo->dir_nr)
		return true;
585 586 587
	chunk_offset = rinfo->dir_entries[0].offset;
	return new_pos < chunk_offset ||
	       is_hash_order(new_pos) != is_hash_order(chunk_offset);
588 589
}

590
static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
Sage Weil's avatar
Sage Weil committed
591 592 593 594 595
{
	struct ceph_file_info *fi = file->private_data;
	struct inode *inode = file->f_mapping->host;
	loff_t retval;

Al Viro's avatar
Al Viro committed
596
	inode_lock(inode);
597
	retval = -EINVAL;
598
	switch (whence) {
Sage Weil's avatar
Sage Weil committed
599 600
	case SEEK_CUR:
		offset += file->f_pos;
601 602
	case SEEK_SET:
		break;
Yan, Zheng's avatar
Yan, Zheng committed
603 604
	case SEEK_END:
		retval = -EOPNOTSUPP;
605 606
	default:
		goto out;
Sage Weil's avatar
Sage Weil committed
607
	}
608

Yan, Zheng's avatar
Yan, Zheng committed
609
	if (offset >= 0) {
610 611 612 613 614 615 616 617 618 619
		if (need_reset_readdir(fi, offset)) {
			dout("dir_llseek dropping %p content\n", file);
			reset_readdir(fi);
		} else if (is_hash_order(offset) && offset > file->f_pos) {
			/* for hash offset, we don't know if a forward seek
			 * is within same frag */
			fi->dir_release_count = 0;
			fi->readdir_cache_idx = -1;
		}

Sage Weil's avatar
Sage Weil committed
620 621 622
		if (offset != file->f_pos) {
			file->f_pos = offset;
			file->f_version = 0;
623
			fi->flags &= ~CEPH_F_ATEND;
Sage Weil's avatar
Sage Weil committed
624 625 626
		}
		retval = offset;
	}
627
out:
Al Viro's avatar
Al Viro committed
628
	inode_unlock(inode);
Sage Weil's avatar
Sage Weil committed
629 630 631 632
	return retval;
}

/*
633
 * Handle lookups for the hidden .snap directory.
Sage Weil's avatar
Sage Weil committed
634
 */
635 636
int ceph_handle_snapdir(struct ceph_mds_request *req,
			struct dentry *dentry, int err)
Sage Weil's avatar
Sage Weil committed
637
{
638
	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
639
	struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
Sage Weil's avatar
Sage Weil committed
640 641 642

	/* .snap dir? */
	if (err == -ENOENT &&
643
	    ceph_snap(parent) == CEPH_NOSNAP &&
644
	    strcmp(dentry->d_name.name,
645
		   fsc->mount_options->snapdir_name) == 0) {
Sage Weil's avatar
Sage Weil committed
646
		struct inode *inode = ceph_get_snapdir(parent);
Al Viro's avatar
Al Viro committed
647 648
		dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
		     dentry, dentry, inode);
649
		BUG_ON(!d_unhashed(dentry));
Sage Weil's avatar
Sage Weil committed
650 651 652
		d_add(dentry, inode);
		err = 0;
	}
653 654
	return err;
}
Sage Weil's avatar
Sage Weil committed
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669
/*
 * 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)
{
Sage Weil's avatar
Sage Weil committed
670 671 672 673 674
	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",
675 676
			     dentry, d_inode(dentry));
			if (d_really_is_positive(dentry)) {
Sage Weil's avatar
Sage Weil committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
				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;
}

693
static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
694 695 696 697 698
{
	return ceph_ino(inode) == CEPH_INO_ROOT &&
		strncmp(dentry->d_name.name, ".ceph", 5) == 0;
}

Sage Weil's avatar
Sage Weil committed
699 700 701 702 703
/*
 * 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,
704
				  unsigned int flags)
Sage Weil's avatar
Sage Weil committed
705
{
706 707
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil's avatar
Sage Weil committed
708 709
	struct ceph_mds_request *req;
	int op;
710
	int mask;
Sage Weil's avatar
Sage Weil committed
711 712
	int err;

Al Viro's avatar
Al Viro committed
713 714
	dout("lookup %p dentry %p '%pd'\n",
	     dir, dentry, dentry);
Sage Weil's avatar
Sage Weil committed
715 716 717 718 719

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

	/* can we conclude ENOENT locally? */
720
	if (d_really_is_negative(dentry)) {
Sage Weil's avatar
Sage Weil committed
721 722 723
		struct ceph_inode_info *ci = ceph_inode(dir);
		struct ceph_dentry_info *di = ceph_dentry(dentry);

724
		spin_lock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
725 726
		dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
		if (strncmp(dentry->d_name.name,
727
			    fsc->mount_options->snapdir_name,
Sage Weil's avatar
Sage Weil committed
728
			    dentry->d_name.len) &&
729
		    !is_root_ceph_dentry(dir, dentry) &&
730
		    ceph_test_mount_opt(fsc, DCACHE) &&
731
		    __ceph_dir_is_complete(ci) &&
Sage Weil's avatar
Sage Weil committed
732
		    (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
733
			spin_unlock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
734 735 736 737 738
			dout(" dir %p complete, -ENOENT\n", dir);
			d_add(dentry, NULL);
			di->lease_shared_gen = ci->i_shared_gen;
			return NULL;
		}
739
		spin_unlock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
740 741 742 743 744 745
	}

	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))
Julia Lawall's avatar
Julia Lawall committed
746
		return ERR_CAST(req);
Sage Weil's avatar
Sage Weil committed
747 748
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
749 750 751 752 753 754

	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);

755 756
	req->r_parent = dir;
	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil's avatar
Sage Weil committed
757
	err = ceph_mdsc_do_request(mdsc, NULL, req);
758
	err = ceph_handle_snapdir(req, dentry, err);
Sage Weil's avatar
Sage Weil committed
759 760 761 762 763 764 765 766 767 768 769 770
	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)
{
771
	struct dentry *result = ceph_lookup(dir, dentry, 0);
Sage Weil's avatar
Sage Weil committed
772 773 774 775 776

	if (result && !IS_ERR(result)) {
		/*
		 * We created the item, then did a lookup, and found
		 * it was already linked to another inode we already
777 778 779 780 781 782 783 784
		 * 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.
Sage Weil's avatar
Sage Weil committed
785
		 */
Yan, Zheng's avatar
Yan, Zheng committed
786
		d_drop(result);
787
		return -ESTALE;
Sage Weil's avatar
Sage Weil committed
788 789 790 791 792
	}
	return PTR_ERR(result);
}

static int ceph_mknod(struct inode *dir, struct dentry *dentry,
Al Viro's avatar
Al Viro committed
793
		      umode_t mode, dev_t rdev)
Sage Weil's avatar
Sage Weil committed
794
{
795 796
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil's avatar
Sage Weil committed
797
	struct ceph_mds_request *req;
798
	struct ceph_acls_info acls = {};
Sage Weil's avatar
Sage Weil committed
799 800 801 802 803
	int err;

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

804 805 806 807
	err = ceph_pre_init_acls(dir, &mode, &acls);
	if (err < 0)
		return err;

Al Viro's avatar
Al Viro committed
808
	dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
Sage Weil's avatar
Sage Weil committed
809 810 811
	     dir, dentry, mode, rdev);
	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
	if (IS_ERR(req)) {
812 813
		err = PTR_ERR(req);
		goto out;
Sage Weil's avatar
Sage Weil committed
814 815 816
	}
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
817 818
	req->r_parent = dir;
	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil's avatar
Sage Weil committed
819 820 821 822
	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;
823 824 825 826
	if (acls.pagelist) {
		req->r_pagelist = acls.pagelist;
		acls.pagelist = NULL;
	}
Sage Weil's avatar
Sage Weil committed
827 828 829 830
	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);
831
out:
Guangliang Zhao's avatar
Guangliang Zhao committed
832
	if (!err)
833
		ceph_init_inode_acls(d_inode(dentry), &acls);
834
	else
Sage Weil's avatar
Sage Weil committed
835
		d_drop(dentry);
836
	ceph_release_acls_info(&acls);
Sage Weil's avatar
Sage Weil committed
837 838 839
	return err;
}

Al Viro's avatar
Al Viro committed
840
static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro's avatar
Al Viro committed
841
		       bool excl)
Sage Weil's avatar
Sage Weil committed
842
{
843
	return ceph_mknod(dir, dentry, mode, 0);
Sage Weil's avatar
Sage Weil committed
844 845 846 847 848
}

static int ceph_symlink(struct inode *dir, struct dentry *dentry,
			    const char *dest)
{
849 850
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil's avatar
Sage Weil committed
851 852 853 854 855 856 857 858 859
	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)) {
860 861
		err = PTR_ERR(req);
		goto out;
Sage Weil's avatar
Sage Weil committed
862
	}
863
	req->r_path2 = kstrdup(dest, GFP_KERNEL);
864 865 866 867 868
	if (!req->r_path2) {
		err = -ENOMEM;
		ceph_mdsc_put_request(req);
		goto out;
	}
869 870
	req->r_parent = dir;
	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
871 872
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
Sage Weil's avatar
Sage Weil committed
873 874 875 876 877 878
	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);
879 880
out:
	if (err)
Sage Weil's avatar
Sage Weil committed
881 882 883 884
		d_drop(dentry);
	return err;
}

885
static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Sage Weil's avatar
Sage Weil committed
886
{
887 888
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil's avatar
Sage Weil committed
889
	struct ceph_mds_request *req;
890
	struct ceph_acls_info acls = {};
Sage Weil's avatar
Sage Weil committed
891 892 893 894 895 896
	int err = -EROFS;
	int op;

	if (ceph_snap(dir) == CEPH_SNAPDIR) {
		/* mkdir .snap/foo is a MKSNAP */
		op = CEPH_MDS_OP_MKSNAP;
Al Viro's avatar
Al Viro committed
897 898
		dout("mksnap dir %p snap '%pd' dn %p\n", dir,
		     dentry, dentry);
Sage Weil's avatar
Sage Weil committed
899
	} else if (ceph_snap(dir) == CEPH_NOSNAP) {
900
		dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
Sage Weil's avatar
Sage Weil committed
901 902 903 904
		op = CEPH_MDS_OP_MKDIR;
	} else {
		goto out;
	}
905 906 907 908 909 910

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

Sage Weil's avatar
Sage Weil committed
911 912 913 914 915 916 917 918
	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;
919 920
	req->r_parent = dir;
	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil's avatar
Sage Weil committed
921 922 923
	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;
924 925 926 927
	if (acls.pagelist) {
		req->r_pagelist = acls.pagelist;
		acls.pagelist = NULL;
	}
Sage Weil's avatar
Sage Weil committed
928
	err = ceph_mdsc_do_request(mdsc, dir, req);
Yan, Zheng's avatar
Yan, Zheng committed
929 930 931
	if (!err &&
	    !req->r_reply_info.head->is_target &&
	    !req->r_reply_info.head->is_dentry)
Sage Weil's avatar
Sage Weil committed
932 933 934
		err = ceph_handle_notrace_create(dir, dentry);
	ceph_mdsc_put_request(req);
out:
935
	if (!err)
936
		ceph_init_inode_acls(d_inode(dentry), &acls);
937
	else
Sage Weil's avatar
Sage Weil committed
938
		d_drop(dentry);
939
	ceph_release_acls_info(&acls);
Sage Weil's avatar
Sage Weil committed
940 941 942 943 944 945
	return err;
}

static int ceph_link(struct dentry *old_dentry, struct inode *dir,
		     struct dentry *dentry)
{
946 947
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil's avatar
Sage Weil committed
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	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;
963
	req->r_old_dentry = dget(old_dentry);
964 965
	req->r_parent = dir;
	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil's avatar
Sage Weil committed
966 967
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
968 969
	/* release LINK_SHARED on source inode (mds will lock it) */
	req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
Sage Weil's avatar
Sage Weil committed
970
	err = ceph_mdsc_do_request(mdsc, dir, req);
971
	if (err) {
Sage Weil's avatar
Sage Weil committed
972
		d_drop(dentry);
973
	} else if (!req->r_reply_info.head->is_dentry) {
974 975
		ihold(d_inode(old_dentry));
		d_instantiate(dentry, d_inode(old_dentry));
976
	}
Sage Weil's avatar
Sage Weil committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
	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;

992
	spin_lock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
993 994 995 996
	if (inode->i_nlink == 1) {
		drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
		ci->i_ceph_flags |= CEPH_I_NODELAY;
	}
997
	spin_unlock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
998 999 1000 1001 1002 1003 1004 1005
	return drop;
}

/*
 * rmdir and unlink are differ only by the metadata op code
 */
static int ceph_unlink(struct inode *dir, struct dentry *dentry)
{
1006 1007
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
1008
	struct inode *inode = d_inode(dentry);
Sage Weil's avatar
Sage Weil committed
1009 1010 1011 1012 1013 1014
	struct ceph_mds_request *req;
	int err = -EROFS;
	int op;

	if (ceph_snap(dir) == CEPH_SNAPDIR) {
		/* rmdir .snap/foo is RMSNAP */
Al Viro's avatar
Al Viro committed
1015
		dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
Sage Weil's avatar
Sage Weil committed
1016 1017 1018 1019
		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);
1020
		op = d_is_dir(dentry) ?
Sage Weil's avatar
Sage Weil committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
			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;
1031 1032
	req->r_parent = dir;
	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil's avatar
Sage Weil committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	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,
1045 1046
		       struct inode *new_dir, struct dentry *new_dentry,
		       unsigned int flags)
Sage Weil's avatar
Sage Weil committed
1047
{
1048 1049
	struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil's avatar
Sage Weil committed
1050
	struct ceph_mds_request *req;
Yan, Zheng's avatar
Yan, Zheng committed
1051
	int op = CEPH_MDS_OP_RENAME;
Sage Weil's avatar
Sage Weil committed
1052 1053
	int err;

1054 1055 1056
	if (flags)
		return -EINVAL;

Sage Weil's avatar
Sage Weil committed
1057 1058
	if (ceph_snap(old_dir) != ceph_snap(new_dir))
		return -EXDEV;
Yan, Zheng's avatar
Yan, Zheng committed
1059 1060 1061 1062 1063 1064
	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;
	}
Sage Weil's avatar
Sage Weil committed
1065 1066
	dout("rename dir %p dentry %p to dir %p dentry %p\n",
	     old_dir, old_dentry, new_dir, new_dentry);
Yan, Zheng's avatar
Yan, Zheng committed
1067
	req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Sage Weil's avatar
Sage Weil committed
1068 1069
	if (IS_ERR(req))
		return PTR_ERR(req);
1070
	ihold(old_dir);
Sage Weil's avatar
Sage Weil committed
1071 1072 1073
	req->r_dentry = dget(new_dentry);
	req->r_num_caps = 2;
	req->r_old_dentry = dget(old_dentry);
1074
	req->r_old_dentry_dir = old_dir;
1075 1076
	req->r_parent = new_dir;
	set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil's avatar
Sage Weil committed
1077 1078 1079 1080 1081 1082
	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;
1083 1084
	if (d_really_is_positive(new_dentry))
		req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
Sage Weil's avatar
Sage Weil committed
1085 1086 1087 1088 1089 1090 1091
	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.
		 */
1092

Yan, Zheng's avatar
Yan, Zheng committed
1093 1094 1095 1096
		/* d_move screws up sibling dentries' offsets */
		ceph_dir_clear_complete(old_dir);
		ceph_dir_clear_complete(new_dir);

Sage Weil's avatar
Sage Weil committed
1097
		d_move(old_dentry, new_dentry);
1098 1099 1100

		/* ensure target dentry is invalidated, despite
		   rehashing bug in vfs_rename_dir */
1101
		ceph_invalidate_dentry_lease(new_dentry);
Sage Weil's avatar
Sage Weil committed
1102 1103 1104 1105 1106
	}
	ceph_mdsc_put_request(req);
	return err;
}

1107 1108 1109 1110 1111 1112
/*
 * Ensure a dentry lease will no longer revalidate.
 */
void ceph_invalidate_dentry_lease(struct dentry *dentry)
{
	spin_lock(&dentry->d_lock);
Miklos Szeredi's avatar
Miklos Szeredi committed
1113
	ceph_dentry(dentry)->time = jiffies;
1114 1115 1116
	ceph_dentry(dentry)->lease_shared_gen = 0;
	spin_unlock(&dentry->d_lock);
}
Sage Weil's avatar
Sage Weil committed
1117 1118 1119 1120 1121

/*
 * Check if dentry lease is valid.  If not, delete the lease.  Try to
 * renew if the least is more than half up.
 */
1122 1123
static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
				 struct inode *dir)
Sage Weil's avatar
Sage Weil committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
{
	struct ceph_dentry_info *di;
	struct ceph_mds_session *s;
	int valid = 0;
	u32 gen;
	unsigned long ttl;
	struct ceph_mds_session *session = NULL;
	u32 seq = 0;

	spin_lock(&dentry->d_lock);
	di = ceph_dentry(dentry);
1135
	if (di && di->lease_session) {
Sage Weil's avatar
Sage Weil committed
1136
		s = di->lease_session;
1137
		spin_lock(&s->s_gen_ttl_lock);
Sage Weil's avatar
Sage Weil committed
1138 1139
		gen = s->s_cap_gen;
		ttl = s->s_cap_ttl;
1140
		spin_unlock(&s->s_gen_ttl_lock);
Sage Weil's avatar
Sage Weil committed
1141 1142

		if (di->lease_gen == gen &&
Miklos Szeredi's avatar
Miklos Szeredi committed
1143
		    time_before(jiffies, di->time) &&
Sage Weil's avatar
Sage Weil committed
1144 1145 1146 1147
		    time_before(jiffies, ttl)) {
			valid = 1;
			if (di->lease_renew_after &&
			    time_after(jiffies, di->lease_renew_after)) {
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
				/*
				 * We should renew. If we're in RCU walk mode
				 * though, we can't do that so just return
				 * -ECHILD.
				 */
				if (flags & LOOKUP_RCU) {
					valid = -ECHILD;
				} else {
					session = ceph_get_mds_session(s);
					seq = di->lease_seq;
					di->lease_renew_after = 0;
					di->lease_renew_from = jiffies;
				}
Sage Weil's avatar
Sage Weil committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
			}
		}
	}
	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;

1184
	spin_lock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
1185 1186
	if (ci->i_shared_gen == di->lease_shared_gen)
		valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1187
	spin_unlock(&ci->i_ceph_lock);
Sage Weil's avatar
Sage Weil committed
1188 1189 1190 1191 1192 1193 1194 1195 1196
	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.
 */
1197
static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
Sage Weil's avatar
Sage Weil committed
1198
{
1199
	int valid = 0;
1200
	struct dentry *parent;
1201 1202
	struct inode *dir;

1203
	if (flags & LOOKUP_RCU) {
1204
		parent = READ_ONCE(dentry->d_parent);
1205 1206 1207 1208 1209 1210 1211
		dir = d_inode_rcu(parent);
		if (!dir)
			return -ECHILD;
	} else {
		parent = dget_parent(dentry);
		dir = d_inode(parent);
	}
</