You need to sign in or sign up before continuing.
dir.c 35.7 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 29 30

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

const struct inode_operations ceph_dir_iops;
const struct file_operations ceph_dir_fops;
S
Sage Weil 已提交
31
const struct dentry_operations ceph_dentry_ops;
S
Sage Weil 已提交
32 33 34 35 36 37 38 39 40 41 42

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

	if (dentry->d_fsdata)
		return 0;

43
	di = kmem_cache_alloc(ceph_dentry_cachep, GFP_NOFS | __GFP_ZERO);
S
Sage Weil 已提交
44 45 46 47
	if (!di)
		return -ENOMEM;          /* oh well */

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

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

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

S
Sage Weil 已提交
73 74 75 76 77 78 79 80
struct inode *ceph_get_dentry_parent_inode(struct dentry *dentry)
{
	struct inode *inode = NULL;

	if (!dentry)
		return NULL;

	spin_lock(&dentry->d_lock);
81
	if (!IS_ROOT(dentry)) {
S
Sage Weil 已提交
82 83 84 85 86 87
		inode = dentry->d_parent->d_inode;
		ihold(inode);
	}
	spin_unlock(&dentry->d_lock);
	return inode;
}
S
Sage Weil 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102


/*
 * 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 已提交
103 104 105 106 107 108 109 110
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));
}

S
Sage Weil 已提交
111 112 113 114 115 116 117
/*
 * When possible, we try to satisfy a readdir by peeking at the
 * dcache.  We make this work by carefully ordering dentries on
 * d_u.d_child when we initially get results back from the MDS, and
 * falling back to a "normal" sync readdir if any dentries in the dir
 * are dropped.
 *
118
 * Complete dir indicates that we have all dentries in the dir.  It is
S
Sage Weil 已提交
119 120 121
 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
 * the MDS if/when the directory is modified).
 */
122 123
static int __dcache_readdir(struct file *file,  struct dir_context *ctx,
			    u32 shared_gen)
S
Sage Weil 已提交
124
{
A
Al Viro 已提交
125 126
	struct ceph_file_info *fi = file->private_data;
	struct dentry *parent = file->f_dentry;
S
Sage Weil 已提交
127 128 129 130 131 132 133 134 135 136
	struct inode *dir = parent->d_inode;
	struct list_head *p;
	struct dentry *dentry, *last;
	struct ceph_dentry_info *di;
	int err = 0;

	/* claim ref on last dentry we returned */
	last = fi->dentry;
	fi->dentry = NULL;

137 138
	dout("__dcache_readdir %p v%u at %llu (last %p)\n",
	     dir, shared_gen, ctx->pos, last);
S
Sage Weil 已提交
139

N
Nick Piggin 已提交
140
	spin_lock(&parent->d_lock);
S
Sage Weil 已提交
141 142

	/* start at beginning? */
A
Al Viro 已提交
143 144
	if (ctx->pos == 2 || last == NULL ||
	    ctx->pos < ceph_dentry(last)->offset) {
S
Sage Weil 已提交
145 146 147 148 149 150 151 152 153 154 155 156
		if (list_empty(&parent->d_subdirs))
			goto out_unlock;
		p = parent->d_subdirs.prev;
		dout(" initial p %p/%p\n", p->prev, p->next);
	} else {
		p = last->d_u.d_child.prev;
	}

more:
	dentry = list_entry(p, struct dentry, d_u.d_child);
	di = ceph_dentry(dentry);
	while (1) {
S
Sage Weil 已提交
157 158
		dout(" p %p/%p %s d_subdirs %p/%p\n", p->prev, p->next,
		     d_unhashed(dentry) ? "!hashed" : "hashed",
S
Sage Weil 已提交
159 160
		     parent->d_subdirs.prev, parent->d_subdirs.next);
		if (p == &parent->d_subdirs) {
161
			fi->flags |= CEPH_F_ATEND;
S
Sage Weil 已提交
162 163
			goto out_unlock;
		}
N
Nick Piggin 已提交
164
		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
165 166
		if (di->lease_shared_gen == shared_gen &&
		    !d_unhashed(dentry) && dentry->d_inode &&
167
		    ceph_snap(dentry->d_inode) != CEPH_SNAPDIR &&
S
Sage Weil 已提交
168
		    ceph_ino(dentry->d_inode) != CEPH_INO_CEPH &&
Y
Yan, Zheng 已提交
169
		    fpos_cmp(ctx->pos, di->offset) <= 0)
S
Sage Weil 已提交
170 171 172
			break;
		dout(" skipping %p %.*s at %llu (%llu)%s%s\n", dentry,
		     dentry->d_name.len, dentry->d_name.name, di->offset,
A
Al Viro 已提交
173
		     ctx->pos, d_unhashed(dentry) ? " unhashed" : "",
S
Sage Weil 已提交
174
		     !dentry->d_inode ? " null" : "");
N
Nick Piggin 已提交
175
		spin_unlock(&dentry->d_lock);
S
Sage Weil 已提交
176 177 178 179 180
		p = p->prev;
		dentry = list_entry(p, struct dentry, d_u.d_child);
		di = ceph_dentry(dentry);
	}

N
Nick Piggin 已提交
181
	dget_dlock(dentry);
N
Nick Piggin 已提交
182
	spin_unlock(&dentry->d_lock);
N
Nick Piggin 已提交
183
	spin_unlock(&parent->d_lock);
S
Sage Weil 已提交
184

A
Al Viro 已提交
185
	dout(" %llu (%llu) dentry %p %.*s %p\n", di->offset, ctx->pos,
S
Sage Weil 已提交
186
	     dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
A
Al Viro 已提交
187 188 189
	ctx->pos = di->offset;
	if (!dir_emit(ctx, dentry->d_name.name,
		      dentry->d_name.len,
Y
Yehuda Sadeh 已提交
190
		      ceph_translate_ino(dentry->d_sb, dentry->d_inode->i_ino),
A
Al Viro 已提交
191 192
		      dentry->d_inode->i_mode >> 12)) {
		if (last) {
S
Sage Weil 已提交
193 194
			/* remember our position */
			fi->dentry = last;
Y
Yan, Zheng 已提交
195
			fi->next_offset = fpos_off(di->offset);
S
Sage Weil 已提交
196
		}
A
Al Viro 已提交
197 198
		dput(dentry);
		return 0;
S
Sage Weil 已提交
199
	}
200

A
Al Viro 已提交
201 202 203
	if (last)
		dput(last);
	last = dentry;
S
Sage Weil 已提交
204

A
Al Viro 已提交
205
	ctx->pos++;
S
Sage Weil 已提交
206

N
Nick Piggin 已提交
207
	/* make sure a dentry wasn't dropped while we didn't have parent lock */
208 209
	if (!ceph_dir_is_complete(dir)) {
		dout(" lost dir complete on %p; falling back to mds\n", dir);
210 211 212 213
		err = -EAGAIN;
		goto out;
	}

N
Nick Piggin 已提交
214
	spin_lock(&parent->d_lock);
215 216
	p = p->prev;	/* advance to next dentry */
	goto more;
S
Sage Weil 已提交
217 218

out_unlock:
N
Nick Piggin 已提交
219
	spin_unlock(&parent->d_lock);
220 221
out:
	if (last)
S
Sage Weil 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		dput(last);
	return err;
}

/*
 * 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)
{
	kfree(fi->last_name);
	fi->last_name = kmalloc(len+1, GFP_NOFS);
	if (!fi->last_name)
		return -ENOMEM;
	memcpy(fi->last_name, name, len);
	fi->last_name[len] = 0;
	dout("note_last_dentry '%s'\n", fi->last_name);
	return 0;
}

A
Al Viro 已提交
245
static int ceph_readdir(struct file *file, struct dir_context *ctx)
S
Sage Weil 已提交
246
{
A
Al Viro 已提交
247 248
	struct ceph_file_info *fi = file->private_data;
	struct inode *inode = file_inode(file);
S
Sage Weil 已提交
249
	struct ceph_inode_info *ci = ceph_inode(inode);
250 251
	struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
	struct ceph_mds_client *mdsc = fsc->mdsc;
A
Al Viro 已提交
252 253
	unsigned frag = fpos_frag(ctx->pos);
	int off = fpos_off(ctx->pos);
S
Sage Weil 已提交
254 255 256 257
	int err;
	u32 ftype;
	struct ceph_mds_reply_info_parsed *rinfo;

A
Al Viro 已提交
258
	dout("readdir %p file %p frag %u off %u\n", inode, file, frag, off);
259
	if (fi->flags & CEPH_F_ATEND)
S
Sage Weil 已提交
260 261 262
		return 0;

	/* always start with . and .. */
A
Al Viro 已提交
263
	if (ctx->pos == 0) {
S
Sage Weil 已提交
264 265
		/* note dir version at start of readdir so we can tell
		 * if any dentries get dropped */
266
		fi->dir_release_count = atomic_read(&ci->i_release_count);
S
Sage Weil 已提交
267 268

		dout("readdir off 0 -> '.'\n");
A
Al Viro 已提交
269
		if (!dir_emit(ctx, ".", 1, 
Y
Yehuda Sadeh 已提交
270
			    ceph_translate_ino(inode->i_sb, inode->i_ino),
A
Al Viro 已提交
271
			    inode->i_mode >> 12))
S
Sage Weil 已提交
272
			return 0;
A
Al Viro 已提交
273
		ctx->pos = 1;
S
Sage Weil 已提交
274 275
		off = 1;
	}
A
Al Viro 已提交
276 277
	if (ctx->pos == 1) {
		ino_t ino = parent_ino(file->f_dentry);
S
Sage Weil 已提交
278
		dout("readdir off 1 -> '..'\n");
A
Al Viro 已提交
279
		if (!dir_emit(ctx, "..", 2,
Y
Yehuda Sadeh 已提交
280
			    ceph_translate_ino(inode->i_sb, ino),
A
Al Viro 已提交
281
			    inode->i_mode >> 12))
S
Sage Weil 已提交
282
			return 0;
A
Al Viro 已提交
283
		ctx->pos = 2;
S
Sage Weil 已提交
284 285 286 287
		off = 2;
	}

	/* can we use the dcache? */
288
	spin_lock(&ci->i_ceph_lock);
A
Al Viro 已提交
289
	if ((ctx->pos == 2 || fi->dentry) &&
290
	    !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
291
	    ceph_snap(inode) != CEPH_SNAPDIR &&
292
	    __ceph_dir_is_complete(ci) &&
S
Sage Weil 已提交
293
	    __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
294
		u32 shared_gen = ci->i_shared_gen;
295
		spin_unlock(&ci->i_ceph_lock);
296
		err = __dcache_readdir(file, ctx, shared_gen);
297
		if (err != -EAGAIN)
S
Sage Weil 已提交
298
			return err;
299
	} else {
300
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	}
	if (fi->dentry) {
		err = note_last_dentry(fi, fi->dentry->d_name.name,
				       fi->dentry->d_name.len);
		if (err)
			return err;
		dput(fi->dentry);
		fi->dentry = NULL;
	}

	/* 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 */
321
		if (fi->last_readdir) {
S
Sage Weil 已提交
322
			ceph_mdsc_put_request(fi->last_readdir);
323 324
			fi->last_readdir = NULL;
		}
S
Sage Weil 已提交
325 326 327 328 329 330

		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);
331 332 333 334 335
		err = ceph_alloc_readdir_reply_buffer(req, inode);
		if (err) {
			ceph_mdsc_put_request(req);
			return err;
		}
336 337
		req->r_inode = inode;
		ihold(inode);
A
Al Viro 已提交
338
		req->r_dentry = dget(file->f_dentry);
S
Sage Weil 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
		/* 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;
		req->r_path2 = kstrdup(fi->last_name, GFP_NOFS);
		req->r_readdir_offset = fi->next_offset;
		req->r_args.readdir.frag = cpu_to_le32(frag);
		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);

		if (!req->r_did_prepopulate) {
			dout("readdir !did_prepopulate");
358 359
			/* preclude from marking dir complete */
			fi->dir_release_count--;
S
Sage Weil 已提交
360 361 362
		}

		/* note next offset and last dentry name */
363 364 365 366 367 368 369 370 371
		rinfo = &req->r_reply_info;
		if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
			frag = le32_to_cpu(rinfo->dir_dir->frag);
			if (ceph_frag_is_leftmost(frag))
				fi->next_offset = 2;
			else
				fi->next_offset = 0;
			off = fi->next_offset;
		}
Y
Yan, Zheng 已提交
372
		fi->frag = frag;
S
Sage Weil 已提交
373 374 375 376 377 378
		fi->offset = fi->next_offset;
		fi->last_readdir = req;

		if (req->r_reply_info.dir_end) {
			kfree(fi->last_name);
			fi->last_name = NULL;
379 380 381 382
			if (ceph_frag_is_rightmost(frag))
				fi->next_offset = 2;
			else
				fi->next_offset = 0;
S
Sage Weil 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395
		} else {
			err = note_last_dentry(fi,
				       rinfo->dir_dname[rinfo->dir_nr-1],
				       rinfo->dir_dname_len[rinfo->dir_nr-1]);
			if (err)
				return err;
			fi->next_offset += rinfo->dir_nr;
		}
	}

	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 已提交
396 397

	ctx->pos = ceph_make_fpos(frag, off);
398
	while (off >= fi->offset && off - fi->offset < rinfo->dir_nr) {
S
Sage Weil 已提交
399 400
		struct ceph_mds_reply_inode *in =
			rinfo->dir_in[off - fi->offset].in;
401 402 403
		struct ceph_vino vino;
		ino_t ino;

S
Sage Weil 已提交
404
		dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
A
Al Viro 已提交
405
		     off, off - fi->offset, rinfo->dir_nr, ctx->pos,
S
Sage Weil 已提交
406 407 408 409
		     rinfo->dir_dname_len[off - fi->offset],
		     rinfo->dir_dname[off - fi->offset], in);
		BUG_ON(!in);
		ftype = le32_to_cpu(in->mode) >> 12;
410 411 412
		vino.ino = le64_to_cpu(in->ino);
		vino.snap = le64_to_cpu(in->snapid);
		ino = ceph_vino_to_ino(vino);
A
Al Viro 已提交
413
		if (!dir_emit(ctx,
S
Sage Weil 已提交
414 415
			    rinfo->dir_dname[off - fi->offset],
			    rinfo->dir_dname_len[off - fi->offset],
A
Al Viro 已提交
416
			    ceph_translate_ino(inode->i_sb, ino), ftype)) {
S
Sage Weil 已提交
417 418 419 420
			dout("filldir stopping us...\n");
			return 0;
		}
		off++;
A
Al Viro 已提交
421
		ctx->pos++;
S
Sage Weil 已提交
422 423 424 425 426 427 428 429 430 431 432 433
	}

	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);
		off = 0;
A
Al Viro 已提交
434
		ctx->pos = ceph_make_fpos(frag, off);
S
Sage Weil 已提交
435 436 437
		dout("readdir next frag is %x\n", frag);
		goto more;
	}
438
	fi->flags |= CEPH_F_ATEND;
S
Sage Weil 已提交
439 440 441 442 443 444

	/*
	 * 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.
	 */
445
	spin_lock(&ci->i_ceph_lock);
446
	if (atomic_read(&ci->i_release_count) == fi->dir_release_count) {
447
		dout(" marking %p complete\n", inode);
448
		__ceph_dir_set_complete(ci, fi->dir_release_count);
A
Al Viro 已提交
449
		ci->i_max_offset = ctx->pos;
S
Sage Weil 已提交
450
	}
451
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
452

A
Al Viro 已提交
453
	dout("readdir %p file %p done.\n", inode, file);
S
Sage Weil 已提交
454 455 456
	return 0;
}

Y
Yan, Zheng 已提交
457
static void reset_readdir(struct ceph_file_info *fi, unsigned frag)
S
Sage Weil 已提交
458 459 460 461 462 463
{
	if (fi->last_readdir) {
		ceph_mdsc_put_request(fi->last_readdir);
		fi->last_readdir = NULL;
	}
	kfree(fi->last_name);
S
Sage Weil 已提交
464
	fi->last_name = NULL;
Y
Yan, Zheng 已提交
465 466 467 468
	if (ceph_frag_is_leftmost(frag))
		fi->next_offset = 2;  /* compensate for . and .. */
	else
		fi->next_offset = 0;
S
Sage Weil 已提交
469 470 471 472
	if (fi->dentry) {
		dput(fi->dentry);
		fi->dentry = NULL;
	}
473
	fi->flags &= ~CEPH_F_ATEND;
S
Sage Weil 已提交
474 475
}

476
static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
S
Sage Weil 已提交
477 478 479
{
	struct ceph_file_info *fi = file->private_data;
	struct inode *inode = file->f_mapping->host;
Y
Yan, Zheng 已提交
480
	loff_t old_offset = ceph_make_fpos(fi->frag, fi->next_offset);
S
Sage Weil 已提交
481 482 483
	loff_t retval;

	mutex_lock(&inode->i_mutex);
484
	retval = -EINVAL;
485
	switch (whence) {
S
Sage Weil 已提交
486 487 488 489 490
	case SEEK_END:
		offset += inode->i_size + 2;   /* FIXME */
		break;
	case SEEK_CUR:
		offset += file->f_pos;
491 492 493 494
	case SEEK_SET:
		break;
	default:
		goto out;
S
Sage Weil 已提交
495
	}
496

Y
Yan, Zheng 已提交
497
	if (offset >= 0) {
S
Sage Weil 已提交
498 499 500
		if (offset != file->f_pos) {
			file->f_pos = offset;
			file->f_version = 0;
501
			fi->flags &= ~CEPH_F_ATEND;
S
Sage Weil 已提交
502 503 504 505 506 507 508 509
		}
		retval = offset;

		/*
		 * discard buffered readdir content on seekdir(0), or
		 * seek to new frag, or seek prior to current chunk.
		 */
		if (offset == 0 ||
Y
Yan, Zheng 已提交
510
		    fpos_frag(offset) != fi->frag ||
S
Sage Weil 已提交
511 512
		    fpos_off(offset) < fi->offset) {
			dout("dir_llseek dropping %p content\n", file);
Y
Yan, Zheng 已提交
513
			reset_readdir(fi, fpos_frag(offset));
S
Sage Weil 已提交
514 515 516
		}

		/* bump dir_release_count if we did a forward seek */
Y
Yan, Zheng 已提交
517
		if (fpos_cmp(offset, old_offset) > 0)
S
Sage Weil 已提交
518 519
			fi->dir_release_count--;
	}
520
out:
S
Sage Weil 已提交
521 522 523 524 525
	mutex_unlock(&inode->i_mutex);
	return retval;
}

/*
526
 * Handle lookups for the hidden .snap directory.
S
Sage Weil 已提交
527
 */
528 529
int ceph_handle_snapdir(struct ceph_mds_request *req,
			struct dentry *dentry, int err)
S
Sage Weil 已提交
530
{
531
	struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
532
	struct inode *parent = dentry->d_parent->d_inode; /* we hold i_mutex */
S
Sage Weil 已提交
533 534 535

	/* .snap dir? */
	if (err == -ENOENT &&
536
	    ceph_snap(parent) == CEPH_NOSNAP &&
537
	    strcmp(dentry->d_name.name,
538
		   fsc->mount_options->snapdir_name) == 0) {
S
Sage Weil 已提交
539 540 541
		struct inode *inode = ceph_get_snapdir(parent);
		dout("ENOENT on snapdir %p '%.*s', linking to snapdir %p\n",
		     dentry, dentry->d_name.len, dentry->d_name.name, inode);
542
		BUG_ON(!d_unhashed(dentry));
S
Sage Weil 已提交
543 544 545
		d_add(dentry, inode);
		err = 0;
	}
546 547
	return err;
}
S
Sage Weil 已提交
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562
/*
 * 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 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	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",
			     dentry, dentry->d_inode);
			if (dentry->d_inode) {
				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 已提交
586 587 588 589 590 591
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 已提交
592 593 594 595 596
/*
 * 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 已提交
597
				  unsigned int flags)
S
Sage Weil 已提交
598
{
599 600
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	struct ceph_mds_request *req;
	int op;
	int err;

	dout("lookup %p dentry %p '%.*s'\n",
	     dir, dentry, dentry->d_name.len, dentry->d_name.name);

	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? */
	if (dentry->d_inode == NULL) {
		struct ceph_inode_info *ci = ceph_inode(dir);
		struct ceph_dentry_info *di = ceph_dentry(dentry);

620
		spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
621 622
		dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
		if (strncmp(dentry->d_name.name,
623
			    fsc->mount_options->snapdir_name,
S
Sage Weil 已提交
624
			    dentry->d_name.len) &&
S
Sage Weil 已提交
625
		    !is_root_ceph_dentry(dir, dentry) &&
626
		    __ceph_dir_is_complete(ci) &&
S
Sage Weil 已提交
627
		    (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
628
			spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
629 630 631 632 633
			dout(" dir %p complete, -ENOENT\n", dir);
			d_add(dentry, NULL);
			di->lease_shared_gen = ci->i_shared_gen;
			return NULL;
		}
634
		spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
635 636 637 638 639 640
	}

	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 已提交
641
		return ERR_CAST(req);
S
Sage Weil 已提交
642 643 644 645 646 647
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
	/* we only need inode linkage */
	req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
	req->r_locked_dir = dir;
	err = ceph_mdsc_do_request(mdsc, NULL, req);
648
	err = ceph_handle_snapdir(req, dentry, err);
S
Sage Weil 已提交
649 650 651 652 653 654 655 656 657 658 659 660
	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 已提交
661
	struct dentry *result = ceph_lookup(dir, dentry, 0);
S
Sage Weil 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

	if (result && !IS_ERR(result)) {
		/*
		 * We created the item, then did a lookup, and found
		 * it was already linked to another inode we already
		 * had in our cache (and thus got spliced).  Link our
		 * dentry to that inode, but don't hash it, just in
		 * case the VFS wants to dereference it.
		 */
		BUG_ON(!result->d_inode);
		d_instantiate(dentry, result->d_inode);
		return 0;
	}
	return PTR_ERR(result);
}

static int ceph_mknod(struct inode *dir, struct dentry *dentry,
A
Al Viro 已提交
679
		      umode_t mode, dev_t rdev)
S
Sage Weil 已提交
680
{
681 682
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
683 684 685 686 687 688
	struct ceph_mds_request *req;
	int err;

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

A
Al Viro 已提交
689
	dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
S
Sage Weil 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	     dir, dentry, mode, rdev);
	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
	if (IS_ERR(req)) {
		d_drop(dentry);
		return PTR_ERR(req);
	}
	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;
	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);
G
Guangliang Zhao 已提交
707 708

	if (!err)
709 710
		ceph_init_acl(dentry, dentry->d_inode, dir);
	else
S
Sage Weil 已提交
711 712 713 714
		d_drop(dentry);
	return err;
}

A
Al Viro 已提交
715
static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
A
Al Viro 已提交
716
		       bool excl)
S
Sage Weil 已提交
717
{
718
	return ceph_mknod(dir, dentry, mode, 0);
S
Sage Weil 已提交
719 720 721 722 723
}

static int ceph_symlink(struct inode *dir, struct dentry *dentry,
			    const char *dest)
{
724 725
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
	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)) {
		d_drop(dentry);
		return PTR_ERR(req);
	}
	req->r_dentry = dget(dentry);
	req->r_num_caps = 2;
	req->r_path2 = kstrdup(dest, GFP_NOFS);
	req->r_locked_dir = dir;
	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);
748 749 750
	if (!err)
		ceph_init_acl(dentry, dentry->d_inode, dir);
	else
S
Sage Weil 已提交
751 752 753 754
		d_drop(dentry);
	return err;
}

755
static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
S
Sage Weil 已提交
756
{
757 758
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
759 760 761 762 763 764 765 766 767 768
	struct ceph_mds_request *req;
	int err = -EROFS;
	int op;

	if (ceph_snap(dir) == CEPH_SNAPDIR) {
		/* mkdir .snap/foo is a MKSNAP */
		op = CEPH_MDS_OP_MKSNAP;
		dout("mksnap dir %p snap '%.*s' dn %p\n", dir,
		     dentry->d_name.len, dentry->d_name.name, dentry);
	} else if (ceph_snap(dir) == CEPH_NOSNAP) {
769
		dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
S
Sage Weil 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
		op = CEPH_MDS_OP_MKDIR;
	} 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_args.mkdir.mode = cpu_to_le32(mode);
	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);
out:
791 792 793
	if (!err)
		ceph_init_acl(dentry, dentry->d_inode, dir);
	else
S
Sage Weil 已提交
794 795 796 797 798 799 800
		d_drop(dentry);
	return err;
}

static int ceph_link(struct dentry *old_dentry, struct inode *dir,
		     struct dentry *dentry)
{
801 802
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	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;
818
	req->r_old_dentry = dget(old_dentry);
S
Sage Weil 已提交
819 820 821
	req->r_locked_dir = dir;
	req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
	req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
822 823
	/* release LINK_SHARED on source inode (mds will lock it) */
	req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
S
Sage Weil 已提交
824
	err = ceph_mdsc_do_request(mdsc, dir, req);
825
	if (err) {
S
Sage Weil 已提交
826
		d_drop(dentry);
827 828 829 830
	} else if (!req->r_reply_info.head->is_dentry) {
		ihold(old_dentry->d_inode);
		d_instantiate(dentry, old_dentry->d_inode);
	}
S
Sage Weil 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
	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;

846
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
847 848 849 850
	if (inode->i_nlink == 1) {
		drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
		ci->i_ceph_flags |= CEPH_I_NODELAY;
	}
851
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
852 853 854 855 856 857 858 859
	return drop;
}

/*
 * rmdir and unlink are differ only by the metadata op code
 */
static int ceph_unlink(struct inode *dir, struct dentry *dentry)
{
860 861
	struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874
	struct inode *inode = dentry->d_inode;
	struct ceph_mds_request *req;
	int err = -EROFS;
	int op;

	if (ceph_snap(dir) == CEPH_SNAPDIR) {
		/* rmdir .snap/foo is RMSNAP */
		dout("rmsnap dir %p '%.*s' dn %p\n", dir, dentry->d_name.len,
		     dentry->d_name.name, dentry);
		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);
A
Al Viro 已提交
875
		op = S_ISDIR(dentry->d_inode->i_mode) ?
S
Sage Weil 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
			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)
{
901 902
	struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
	struct ceph_mds_client *mdsc = fsc->mdsc;
S
Sage Weil 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915
	struct ceph_mds_request *req;
	int err;

	if (ceph_snap(old_dir) != ceph_snap(new_dir))
		return -EXDEV;
	if (ceph_snap(old_dir) != CEPH_NOSNAP ||
	    ceph_snap(new_dir) != CEPH_NOSNAP)
		return -EROFS;
	dout("rename dir %p dentry %p to dir %p dentry %p\n",
	     old_dir, old_dentry, new_dir, new_dentry);
	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RENAME, USE_AUTH_MDS);
	if (IS_ERR(req))
		return PTR_ERR(req);
916
	ihold(old_dir);
S
Sage Weil 已提交
917 918 919
	req->r_dentry = dget(new_dentry);
	req->r_num_caps = 2;
	req->r_old_dentry = dget(old_dentry);
920
	req->r_old_dentry_dir = old_dir;
S
Sage Weil 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
	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;
	if (new_dentry->d_inode)
		req->r_inode_drop = drop_caps_for_unlink(new_dentry->d_inode);
	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.
		 */
937 938

		/* d_move screws up d_subdirs order */
939
		ceph_dir_clear_complete(new_dir);
940

S
Sage Weil 已提交
941
		d_move(old_dentry, new_dentry);
942 943 944

		/* ensure target dentry is invalidated, despite
		   rehashing bug in vfs_rename_dir */
945
		ceph_invalidate_dentry_lease(new_dentry);
S
Sage Weil 已提交
946 947 948 949 950
	}
	ceph_mdsc_put_request(req);
	return err;
}

951 952 953 954 955 956 957 958 959 960
/*
 * 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 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978

/*
 * 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);
979
	if (di->lease_session) {
S
Sage Weil 已提交
980
		s = di->lease_session;
981
		spin_lock(&s->s_gen_ttl_lock);
S
Sage Weil 已提交
982 983
		gen = s->s_cap_gen;
		ttl = s->s_cap_ttl;
984
		spin_unlock(&s->s_gen_ttl_lock);
S
Sage Weil 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

		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 */
				dir = dentry->d_parent->d_inode;
				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;

1021
	spin_lock(&ci->i_ceph_lock);
S
Sage Weil 已提交
1022 1023
	if (ci->i_shared_gen == di->lease_shared_gen)
		valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1024
	spin_unlock(&ci->i_ceph_lock);
S
Sage Weil 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033
	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.
 */
1034
static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
S
Sage Weil 已提交
1035
{
1036
	int valid = 0;
1037 1038
	struct inode *dir;

1039
	if (flags & LOOKUP_RCU)
1040 1041
		return -ECHILD;

S
Sage Weil 已提交
1042 1043 1044
	dout("d_revalidate %p '%.*s' inode %p offset %lld\n", dentry,
	     dentry->d_name.len, dentry->d_name.name, dentry->d_inode,
	     ceph_dentry(dentry)->offset);
S
Sage Weil 已提交
1045

1046 1047
	dir = ceph_get_dentry_parent_inode(dentry);

S
Sage Weil 已提交
1048 1049 1050 1051
	/* always trust cached snapped dentries, snapdir dentry */
	if (ceph_snap(dir) != CEPH_NOSNAP) {
		dout("d_revalidate %p '%.*s' inode %p is SNAPPED\n", dentry,
		     dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
1052 1053 1054 1055 1056 1057
		valid = 1;
	} else if (dentry->d_inode &&
		   ceph_snap(dentry->d_inode) == CEPH_SNAPDIR) {
		valid = 1;
	} else if (dentry_lease_is_valid(dentry) ||
		   dir_lease_is_valid(dir, dentry)) {
1058 1059 1060 1061
		if (dentry->d_inode)
			valid = ceph_is_any_caps(dentry->d_inode);
		else
			valid = 1;
S
Sage Weil 已提交
1062 1063
	}

1064
	dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1065
	if (valid) {
1066
		ceph_dentry_lru_touch(dentry);
1067 1068
	} else {
		ceph_dir_clear_complete(dir);
1069
		d_drop(dentry);
1070
	}
1071 1072
	iput(dir);
	return valid;
S
Sage Weil 已提交
1073 1074 1075
}

/*
1076
 * Release our ceph_dentry_info.
S
Sage Weil 已提交
1077
 */
1078
static void ceph_d_release(struct dentry *dentry)
S
Sage Weil 已提交
1079 1080 1081
{
	struct ceph_dentry_info *di = ceph_dentry(dentry);

1082
	dout("d_release %p\n", dentry);
1083 1084 1085 1086 1087
	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 已提交
1088 1089 1090
}

static int ceph_snapdir_d_revalidate(struct dentry *dentry,
1091
					  unsigned int flags)
S
Sage Weil 已提交
1092 1093 1094 1095 1096 1097 1098 1099
{
	/*
	 * Eventually, we'll want to revalidate snapped metadata
	 * too... probably...
	 */
	return 1;
}

1100 1101 1102 1103 1104 1105 1106 1107
/*
 * 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 已提交
1108
	dout("ceph_d_prune %p\n", dentry);
1109 1110

	/* do we have a valid parent? */
1111
	if (IS_ROOT(dentry))
1112 1113
		return;

1114
	/* if we are not hashed, we don't affect dir's completeness */
1115 1116
	if (d_unhashed(dentry))
		return;
S
Sage Weil 已提交
1117

1118 1119 1120 1121
	/*
	 * we hold d_lock, so d_parent is stable, and d_fsdata is never
	 * cleared until d_release
	 */
1122
	ceph_dir_clear_complete(dentry->d_parent->d_inode);
1123
}
S
Sage Weil 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132

/*
 * 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 已提交
1133
	struct inode *inode = file_inode(file);
S
Sage Weil 已提交
1134 1135
	struct ceph_inode_info *ci = ceph_inode(inode);
	int left;
1136
	const int bufsize = 1024;
S
Sage Weil 已提交
1137

1138
	if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
S
Sage Weil 已提交
1139 1140 1141
		return -EISDIR;

	if (!cf->dir_info) {
1142
		cf->dir_info = kmalloc(bufsize, GFP_NOFS);
S
Sage Weil 已提交
1143 1144 1145
		if (!cf->dir_info)
			return -ENOMEM;
		cf->dir_info_len =
1146
			snprintf(cf->dir_info, bufsize,
S
Sage Weil 已提交
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 1176 1177 1178 1179
				"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;
}

/*
 * an fsync() on a dir will wait for any uncommitted directory
 * operations to commit.
 */
1180 1181
static int ceph_dir_fsync(struct file *file, loff_t start, loff_t end,
			  int datasync)
S
Sage Weil 已提交
1182
{
A
Al Viro 已提交
1183
	struct inode *inode = file_inode(file);
S
Sage Weil 已提交
1184 1185 1186 1187 1188 1189 1190
	struct ceph_inode_info *ci = ceph_inode(inode);
	struct list_head *head = &ci->i_unsafe_dirops;
	struct ceph_mds_request *req;
	u64 last_tid;
	int ret = 0;

	dout("dir_fsync %p\n", inode);
1191 1192 1193 1194 1195
	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
	if (ret)
		return ret;
	mutex_lock(&inode->i_mutex);

S
Sage Weil 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	spin_lock(&ci->i_unsafe_lock);
	if (list_empty(head))
		goto out;

	req = list_entry(head->prev,
			 struct ceph_mds_request, r_unsafe_dir_item);
	last_tid = req->r_tid;

	do {
		ceph_mdsc_get_request(req);
		spin_unlock(&ci->i_unsafe_lock);
1207

S
Sage Weil 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
		dout("dir_fsync %p wait on tid %llu (until %llu)\n",
		     inode, req->r_tid, last_tid);
		if (req->r_timeout) {
			ret = wait_for_completion_timeout(
				&req->r_safe_completion, req->r_timeout);
			if (ret > 0)
				ret = 0;
			else if (ret == 0)
				ret = -EIO;  /* timed out */
		} else {
			wait_for_completion(&req->r_safe_completion);
		}
		ceph_mdsc_put_request(req);

1222
		spin_lock(&ci->i_unsafe_lock);
S
Sage Weil 已提交
1223 1224 1225 1226 1227 1228 1229
		if (ret || list_empty(head))
			break;
		req = list_entry(head->next,
				 struct ceph_mds_request, r_unsafe_dir_item);
	} while (req->r_tid < last_tid);
out:
	spin_unlock(&ci->i_unsafe_lock);
1230 1231
	mutex_unlock(&inode->i_mutex);

S
Sage Weil 已提交
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
	return ret;
}

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

1245 1246
	dout("dentry_lru_add %p %p '%.*s'\n", di, dn,
	     dn->d_name.len, dn->d_name.name);
1247 1248 1249 1250 1251
	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 已提交
1252 1253 1254 1255 1256 1257 1258
}

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

S
Sage Weil 已提交
1259 1260
	dout("dentry_lru_touch %p %p '%.*s' (offset %lld)\n", di, dn,
	     dn->d_name.len, dn->d_name.name, di->offset);
1261 1262 1263 1264
	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 已提交
1265 1266 1267 1268 1269 1270 1271
}

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

1272 1273
	dout("dentry_lru_del %p %p '%.*s'\n", di, dn,
	     dn->d_name.len, dn->d_name.name);
1274 1275 1276 1277 1278
	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 已提交
1279 1280
}

S
Sage Weil 已提交
1281 1282 1283 1284
/*
 * Return name hash for a given dentry.  This is dependent on
 * the parent directory's hash function.
 */
1285
unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
S
Sage Weil 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
{
	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 已提交
1300 1301
const struct file_operations ceph_dir_fops = {
	.read = ceph_read_dir,
A
Al Viro 已提交
1302
	.iterate = ceph_readdir,
S
Sage Weil 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	.llseek = ceph_dir_llseek,
	.open = ceph_open,
	.release = ceph_release,
	.unlocked_ioctl = ceph_ioctl,
	.fsync = ceph_dir_fsync,
};

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 已提交
1319
	.get_acl = ceph_get_acl,
S
Sage Weil 已提交
1320
	.set_acl = ceph_set_acl,
S
Sage Weil 已提交
1321 1322 1323 1324 1325 1326 1327 1328
	.mknod = ceph_mknod,
	.symlink = ceph_symlink,
	.mkdir = ceph_mkdir,
	.link = ceph_link,
	.unlink = ceph_unlink,
	.rmdir = ceph_unlink,
	.rename = ceph_rename,
	.create = ceph_create,
1329
	.atomic_open = ceph_atomic_open,
S
Sage Weil 已提交
1330 1331
};

S
Sage Weil 已提交
1332
const struct dentry_operations ceph_dentry_ops = {
S
Sage Weil 已提交
1333
	.d_revalidate = ceph_d_revalidate,
1334
	.d_release = ceph_d_release,
1335
	.d_prune = ceph_d_prune,
S
Sage Weil 已提交
1336 1337
};

S
Sage Weil 已提交
1338
const struct dentry_operations ceph_snapdir_dentry_ops = {
S
Sage Weil 已提交
1339
	.d_revalidate = ceph_snapdir_d_revalidate,
1340
	.d_release = ceph_d_release,
S
Sage Weil 已提交
1341 1342
};

S
Sage Weil 已提交
1343
const struct dentry_operations ceph_snap_dentry_ops = {
1344
	.d_release = ceph_d_release,
1345
	.d_prune = ceph_d_prune,
S
Sage Weil 已提交
1346
};