expfs.c 14.1 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (C) Neil Brown 2002
 * Copyright (C) Christoph Hellwig 2007
 *
 * This file contains the code mapping from inodes to NFS file handles,
 * and for mapping back from file handles to dentries.
 *
 * For details on why we do all the strange and hairy things in here
9
 * take a look at Documentation/filesystems/nfs/Exporting.
10
 */
11
#include <linux/exportfs.h>
Linus Torvalds's avatar
Linus Torvalds committed
12 13 14
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/module.h>
15
#include <linux/mount.h>
Linus Torvalds's avatar
Linus Torvalds committed
16
#include <linux/namei.h>
17
#include <linux/sched.h>
18
#include <linux/cred.h>
Linus Torvalds's avatar
Linus Torvalds committed
19

20
#define dprintk(fmt, args...) do{}while(0)
Linus Torvalds's avatar
Linus Torvalds committed
21 22


23
static int get_name(const struct path *path, char *name, struct dentry *child);
24 25


26 27
static int exportfs_get_name(struct vfsmount *mnt, struct dentry *dir,
		char *name, struct dentry *child)
28
{
29
	const struct export_operations *nop = dir->d_sb->s_export_op;
30
	struct path path = {.mnt = mnt, .dentry = dir};
31 32 33 34

	if (nop->get_name)
		return nop->get_name(dir, name, child);
	else
35
		return get_name(&path, name, child);
36
}
Linus Torvalds's avatar
Linus Torvalds committed
37

38 39 40
/*
 * Check if the dentry or any of it's aliases is acceptable.
 */
41 42 43 44 45 46
static struct dentry *
find_acceptable_alias(struct dentry *result,
		int (*acceptable)(void *context, struct dentry *dentry),
		void *context)
{
	struct dentry *dentry, *toput = NULL;
47
	struct inode *inode;
48

49 50 51
	if (acceptable(context, result))
		return result;

52 53
	inode = result->d_inode;
	spin_lock(&inode->i_lock);
54
	hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
55
		dget(dentry);
56
		spin_unlock(&inode->i_lock);
57 58 59 60 61 62
		if (toput)
			dput(toput);
		if (dentry != result && acceptable(context, dentry)) {
			dput(result);
			return dentry;
		}
63
		spin_lock(&inode->i_lock);
64 65
		toput = dentry;
	}
66
	spin_unlock(&inode->i_lock);
67 68 69 70 71 72

	if (toput)
		dput(toput);
	return NULL;
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static bool dentry_connected(struct dentry *dentry)
{
	dget(dentry);
	while (dentry->d_flags & DCACHE_DISCONNECTED) {
		struct dentry *parent = dget_parent(dentry);

		dput(dentry);
		if (IS_ROOT(dentry)) {
			dput(parent);
			return false;
		}
		dentry = parent;
	}
	dput(dentry);
	return true;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static void clear_disconnected(struct dentry *dentry)
{
	dget(dentry);
	while (dentry->d_flags & DCACHE_DISCONNECTED) {
		struct dentry *parent = dget_parent(dentry);

		WARN_ON_ONCE(IS_ROOT(dentry));

		spin_lock(&dentry->d_lock);
		dentry->d_flags &= ~DCACHE_DISCONNECTED;
		spin_unlock(&dentry->d_lock);

		dput(dentry);
		dentry = parent;
	}
	dput(dentry);
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
/*
 * Reconnect a directory dentry with its parent.
 *
 * This can return a dentry, or NULL, or an error.
 *
 * In the first case the returned dentry is the parent of the given
 * dentry, and may itself need to be reconnected to its parent.
 *
 * In the NULL case, a concurrent VFS operation has either renamed or
 * removed this directory.  The concurrent operation has reconnected our
 * dentry, so we no longer need to.
 */
static struct dentry *reconnect_one(struct vfsmount *mnt,
		struct dentry *dentry, char *nbuf)
{
	struct dentry *parent;
	struct dentry *tmp;
	int err;

	parent = ERR_PTR(-EACCES);
Al Viro's avatar
Al Viro committed
128
	inode_lock(dentry->d_inode);
129 130
	if (mnt->mnt_sb->s_export_op->get_parent)
		parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
Al Viro's avatar
Al Viro committed
131
	inode_unlock(dentry->d_inode);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

	if (IS_ERR(parent)) {
		dprintk("%s: get_parent of %ld failed, err %d\n",
			__func__, dentry->d_inode->i_ino, PTR_ERR(parent));
		return parent;
	}

	dprintk("%s: find name of %lu in %lu\n", __func__,
		dentry->d_inode->i_ino, parent->d_inode->i_ino);
	err = exportfs_get_name(mnt, parent, nbuf, dentry);
	if (err == -ENOENT)
		goto out_reconnected;
	if (err)
		goto out_err;
	dprintk("%s: found name: %s\n", __func__, nbuf);
147
	tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
148 149
	if (IS_ERR(tmp)) {
		dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
150
		err = PTR_ERR(tmp);
151 152 153
		goto out_err;
	}
	if (tmp != dentry) {
154 155 156 157 158 159
		/*
		 * Somebody has renamed it since exportfs_get_name();
		 * great, since it could've only been renamed if it
		 * got looked up and thus connected, and it would
		 * remain connected afterwards.  We are done.
		 */
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		dput(tmp);
		goto out_reconnected;
	}
	dput(tmp);
	if (IS_ROOT(dentry)) {
		err = -ESTALE;
		goto out_err;
	}
	return parent;

out_err:
	dput(parent);
	return ERR_PTR(err);
out_reconnected:
	dput(parent);
	/*
	 * Someone must have renamed our entry into another parent, in
	 * which case it has been reconnected by the rename.
	 *
	 * Or someone removed it entirely, in which case filehandle
	 * lookup will succeed but the directory is now IS_DEAD and
	 * subsequent operations on it will fail.
	 *
	 * Alternatively, maybe there was no race at all, and the
	 * filesystem is just corrupt and gave us a parent that doesn't
	 * actually contain any entry pointing to this inode.  So,
	 * double check that this worked and return -ESTALE if not:
	 */
	if (!dentry_connected(dentry))
		return ERR_PTR(-ESTALE);
	return NULL;
}

193 194
/*
 * Make sure target_dir is fully connected to the dentry tree.
Linus Torvalds's avatar
Linus Torvalds committed
195
 *
196 197 198 199 200 201 202 203 204 205 206 207 208
 * On successful return, DCACHE_DISCONNECTED will be cleared on
 * target_dir, and target_dir->d_parent->...->d_parent will reach the
 * root of the filesystem.
 *
 * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
 * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
 * set but already be connected.  In that case we'll verify the
 * connection to root and then clear the flag.
 *
 * Note that target_dir could be removed by a concurrent operation.  In
 * that case reconnect_path may still succeed with target_dir fully
 * connected, but further operations using the filehandle will fail when
 * necessary (due to S_DEAD being set on the directory).
Linus Torvalds's avatar
Linus Torvalds committed
209
 */
210
static int
211
reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
Linus Torvalds's avatar
Linus Torvalds committed
212
{
213
	struct dentry *dentry, *parent;
Linus Torvalds's avatar
Linus Torvalds committed
214

215
	dentry = dget(target_dir);
Linus Torvalds's avatar
Linus Torvalds committed
216

217
	while (dentry->d_flags & DCACHE_DISCONNECTED) {
218
		BUG_ON(dentry == mnt->mnt_sb->s_root);
219

220 221 222 223 224 225
		if (IS_ROOT(dentry))
			parent = reconnect_one(mnt, dentry, nbuf);
		else
			parent = dget_parent(dentry);

		if (!parent)
226
			break;
227
		dput(dentry);
228 229 230
		if (IS_ERR(parent))
			return PTR_ERR(parent);
		dentry = parent;
Linus Torvalds's avatar
Linus Torvalds committed
231
	}
232
	dput(dentry);
233
	clear_disconnected(target_dir);
234 235 236
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
237
struct getdents_callback {
238
	struct dir_context ctx;
Linus Torvalds's avatar
Linus Torvalds committed
239 240
	char *name;		/* name that was found. It already points to a
				   buffer NAME_MAX+1 is size */
241
	u64 ino;		/* the inum we are looking for */
Linus Torvalds's avatar
Linus Torvalds committed
242 243 244 245 246 247 248 249
	int found;		/* inode matched? */
	int sequence;		/* sequence counter */
};

/*
 * A rather strange filldir function to capture
 * the name matching the specified inode number.
 */
250
static int filldir_one(struct dir_context *ctx, const char *name, int len,
251
			loff_t pos, u64 ino, unsigned int d_type)
Linus Torvalds's avatar
Linus Torvalds committed
252
{
253 254
	struct getdents_callback *buf =
		container_of(ctx, struct getdents_callback, ctx);
Linus Torvalds's avatar
Linus Torvalds committed
255 256 257
	int result = 0;

	buf->sequence++;
258
	if (buf->ino == ino && len <= NAME_MAX) {
Linus Torvalds's avatar
Linus Torvalds committed
259 260 261 262 263 264 265 266 267 268
		memcpy(buf->name, name, len);
		buf->name[len] = '\0';
		buf->found = 1;
		result = -1;
	}
	return result;
}

/**
 * get_name - default export_operations->get_name function
269
 * @path:   the directory in which to find a name
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275
 * @name:   a pointer to a %NAME_MAX+1 char buffer to store the name
 * @child:  the dentry for the child directory.
 *
 * calls readdir on the parent until it finds an entry with
 * the same inode number as the child, and returns that.
 */
276
static int get_name(const struct path *path, char *name, struct dentry *child)
Linus Torvalds's avatar
Linus Torvalds committed
277
{
278
	const struct cred *cred = current_cred();
279
	struct inode *dir = path->dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
280 281
	int error;
	struct file *file;
282 283 284 285 286
	struct kstat stat;
	struct path child_path = {
		.mnt = path->mnt,
		.dentry = child,
	};
Al Viro's avatar
Al Viro committed
287 288 289 290
	struct getdents_callback buffer = {
		.ctx.actor = filldir_one,
		.name = name,
	};
Linus Torvalds's avatar
Linus Torvalds committed
291 292 293 294 295 296 297

	error = -ENOTDIR;
	if (!dir || !S_ISDIR(dir->i_mode))
		goto out;
	error = -EINVAL;
	if (!dir->i_fop)
		goto out;
298 299 300 301 302 303
	/*
	 * inode->i_ino is unsigned long, kstat->ino is u64, so the
	 * former would be insufficient on 32-bit hosts when the
	 * filesystem supports 64-bit inode numbers.  So we need to
	 * actually call ->getattr, not just read i_ino:
	 */
304 305
	error = vfs_getattr_nosec(&child_path, &stat,
				  STATX_INO, AT_STATX_SYNC_AS_STAT);
306 307 308
	if (error)
		return error;
	buffer.ino = stat.ino;
Linus Torvalds's avatar
Linus Torvalds committed
309 310 311
	/*
	 * Open the directory ...
	 */
312
	file = dentry_open(path, O_RDONLY, cred);
Linus Torvalds's avatar
Linus Torvalds committed
313 314 315 316 317
	error = PTR_ERR(file);
	if (IS_ERR(file))
		goto out;

	error = -EINVAL;
318
	if (!file->f_op->iterate && !file->f_op->iterate_shared)
Linus Torvalds's avatar
Linus Torvalds committed
319 320 321 322 323 324
		goto out_close;

	buffer.sequence = 0;
	while (1) {
		int old_seq = buffer.sequence;

325
		error = iterate_dir(file, &buffer.ctx);
326 327 328 329
		if (buffer.found) {
			error = 0;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

		if (error < 0)
			break;

		error = -ENOENT;
		if (old_seq == buffer.sequence)
			break;
	}

out_close:
	fput(file);
out:
	return error;
}

/**
 * export_encode_fh - default export_operations->encode_fh function
Al Viro's avatar
Al Viro committed
347
 * @inode:   the object to encode
348
 * @fid:     where to store the file handle fragment
Linus Torvalds's avatar
Linus Torvalds committed
349
 * @max_len: maximum length to store there
Al Viro's avatar
Al Viro committed
350
 * @parent:  parent directory inode, if wanted
Linus Torvalds's avatar
Linus Torvalds committed
351 352 353 354 355 356
 *
 * This default encode_fh function assumes that the 32 inode number
 * is suitable for locating an inode, and that the generation number
 * can be used to check that it is still valid.  It places them in the
 * filehandle fragment where export_decode_fh expects to find them.
 */
Al Viro's avatar
Al Viro committed
357 358
static int export_encode_fh(struct inode *inode, struct fid *fid,
		int *max_len, struct inode *parent)
Linus Torvalds's avatar
Linus Torvalds committed
359 360
{
	int len = *max_len;
Christoph Hellwig's avatar
Christoph Hellwig committed
361
	int type = FILEID_INO32_GEN;
362

Al Viro's avatar
Al Viro committed
363
	if (parent && (len < 4)) {
364
		*max_len = 4;
365
		return FILEID_INVALID;
366 367
	} else if (len < 2) {
		*max_len = 2;
368
		return FILEID_INVALID;
369
	}
Linus Torvalds's avatar
Linus Torvalds committed
370 371

	len = 2;
Christoph Hellwig's avatar
Christoph Hellwig committed
372 373
	fid->i32.ino = inode->i_ino;
	fid->i32.gen = inode->i_generation;
Al Viro's avatar
Al Viro committed
374
	if (parent) {
Christoph Hellwig's avatar
Christoph Hellwig committed
375 376
		fid->i32.parent_ino = parent->i_ino;
		fid->i32.parent_gen = parent->i_generation;
Linus Torvalds's avatar
Linus Torvalds committed
377
		len = 4;
Christoph Hellwig's avatar
Christoph Hellwig committed
378
		type = FILEID_INO32_GEN_PARENT;
Linus Torvalds's avatar
Linus Torvalds committed
379 380 381 382 383
	}
	*max_len = len;
	return type;
}

384 385 386 387 388 389 390 391 392 393 394 395
int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
			     int *max_len, struct inode *parent)
{
	const struct export_operations *nop = inode->i_sb->s_export_op;

	if (nop && nop->encode_fh)
		return nop->encode_fh(inode, fid->raw, max_len, parent);

	return export_encode_fh(inode, fid, max_len, parent);
}
EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);

Christoph Hellwig's avatar
Christoph Hellwig committed
396
int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
397 398
		int connectable)
{
399
	int error;
Al Viro's avatar
Al Viro committed
400 401
	struct dentry *p = NULL;
	struct inode *inode = dentry->d_inode, *parent = NULL;
402

Al Viro's avatar
Al Viro committed
403 404 405 406 407 408 409 410
	if (connectable && !S_ISDIR(inode->i_mode)) {
		p = dget_parent(dentry);
		/*
		 * note that while p might've ceased to be our parent already,
		 * it's still pinned by and still positive.
		 */
		parent = p->d_inode;
	}
411 412

	error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
Al Viro's avatar
Al Viro committed
413
	dput(p);
414 415

	return error;
416 417 418
}
EXPORT_SYMBOL_GPL(exportfs_encode_fh);

Christoph Hellwig's avatar
Christoph Hellwig committed
419 420 421
struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
		int fh_len, int fileid_type,
		int (*acceptable)(void *, struct dentry *), void *context)
422
{
423
	const struct export_operations *nop = mnt->mnt_sb->s_export_op;
Christoph Hellwig's avatar
Christoph Hellwig committed
424
	struct dentry *result, *alias;
425
	char nbuf[NAME_MAX+1];
Christoph Hellwig's avatar
Christoph Hellwig committed
426
	int err;
427

Christoph Hellwig's avatar
Christoph Hellwig committed
428 429 430
	/*
	 * Try to get any dentry for the given file handle from the filesystem.
	 */
431 432
	if (!nop || !nop->fh_to_dentry)
		return ERR_PTR(-ESTALE);
Christoph Hellwig's avatar
Christoph Hellwig committed
433
	result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
434 435 436 437
	if (PTR_ERR(result) == -ENOMEM)
		return ERR_CAST(result);
	if (IS_ERR_OR_NULL(result))
		return ERR_PTR(-ESTALE);
Christoph Hellwig's avatar
Christoph Hellwig committed
438

439 440 441 442 443 444 445 446 447
	/*
	 * If no acceptance criteria was specified by caller, a disconnected
	 * dentry is also accepatable. Callers may use this mode to query if
	 * file handle is stale or to get a reference to an inode without
	 * risking the high overhead caused by directory reconnect.
	 */
	if (!acceptable)
		return result;

448
	if (d_is_dir(result)) {
Christoph Hellwig's avatar
Christoph Hellwig committed
449 450 451 452 453 454 455 456 457
		/*
		 * This request is for a directory.
		 *
		 * On the positive side there is only one dentry for each
		 * directory inode.  On the negative side this implies that we
		 * to ensure our dentry is connected all the way up to the
		 * filesystem root.
		 */
		if (result->d_flags & DCACHE_DISCONNECTED) {
458
			err = reconnect_path(mnt, result, nbuf);
Christoph Hellwig's avatar
Christoph Hellwig committed
459 460 461 462 463 464 465 466 467 468
			if (err)
				goto err_result;
		}

		if (!acceptable(context, result)) {
			err = -EACCES;
			goto err_result;
		}

		return result;
469
	} else {
Christoph Hellwig's avatar
Christoph Hellwig committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
		/*
		 * It's not a directory.  Life is a little more complicated.
		 */
		struct dentry *target_dir, *nresult;

		/*
		 * See if either the dentry we just got from the filesystem
		 * or any alias for it is acceptable.  This is always true
		 * if this filesystem is exported without the subtreecheck
		 * option.  If the filesystem is exported with the subtree
		 * check option there's a fair chance we need to look at
		 * the parent directory in the file handle and make sure
		 * it's connected to the filesystem root.
		 */
		alias = find_acceptable_alias(result, acceptable, context);
		if (alias)
			return alias;

		/*
		 * Try to extract a dentry for the parent directory from the
		 * file handle.  If this fails we'll have to give up.
		 */
		err = -ESTALE;
		if (!nop->fh_to_parent)
			goto err_result;

		target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
				fh_len, fileid_type);
498 499
		if (!target_dir)
			goto err_result;
Christoph Hellwig's avatar
Christoph Hellwig committed
500 501 502 503 504 505 506 507 508
		err = PTR_ERR(target_dir);
		if (IS_ERR(target_dir))
			goto err_result;

		/*
		 * And as usual we need to make sure the parent directory is
		 * connected to the filesystem root.  The VFS really doesn't
		 * like disconnected directories..
		 */
509
		err = reconnect_path(mnt, target_dir, nbuf);
Christoph Hellwig's avatar
Christoph Hellwig committed
510 511 512 513 514 515 516 517 518 519
		if (err) {
			dput(target_dir);
			goto err_result;
		}

		/*
		 * Now that we've got both a well-connected parent and a
		 * dentry for the inode we're after, make sure that our
		 * inode is actually connected to the parent.
		 */
520
		err = exportfs_get_name(mnt, target_dir, nbuf, result);
Christoph Hellwig's avatar
Christoph Hellwig committed
521
		if (!err) {
Al Viro's avatar
Al Viro committed
522
			inode_lock(target_dir->d_inode);
Christoph Hellwig's avatar
Christoph Hellwig committed
523 524
			nresult = lookup_one_len(nbuf, target_dir,
						 strlen(nbuf));
Al Viro's avatar
Al Viro committed
525
			inode_unlock(target_dir->d_inode);
Christoph Hellwig's avatar
Christoph Hellwig committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
			if (!IS_ERR(nresult)) {
				if (nresult->d_inode) {
					dput(result);
					result = nresult;
				} else
					dput(nresult);
			}
		}

		/*
		 * At this point we are done with the parent, but it's pinned
		 * by the child dentry anyway.
		 */
		dput(target_dir);

		/*
		 * And finally make sure the dentry is actually acceptable
		 * to NFSD.
		 */
		alias = find_acceptable_alias(result, acceptable, context);
		if (!alias) {
			err = -EACCES;
			goto err_result;
		}

		return alias;
552 553
	}

Christoph Hellwig's avatar
Christoph Hellwig committed
554 555
 err_result:
	dput(result);
556 557
	if (err != -ENOMEM)
		err = -ESTALE;
Christoph Hellwig's avatar
Christoph Hellwig committed
558
	return ERR_PTR(err);
559 560 561
}
EXPORT_SYMBOL_GPL(exportfs_decode_fh);

Linus Torvalds's avatar
Linus Torvalds committed
562
MODULE_LICENSE("GPL");