expfs.c 12.7 KB
Newer Older
C
Christoph Hellwig 已提交
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.
C
Christoph Hellwig 已提交
10
 */
11
#include <linux/exportfs.h>
L
Linus Torvalds 已提交
12 13 14
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/module.h>
15
#include <linux/mount.h>
L
Linus Torvalds 已提交
16
#include <linux/namei.h>
17
#include <linux/sched.h>
L
Linus Torvalds 已提交
18

19
#define dprintk(fmt, args...) do{}while(0)
L
Linus Torvalds 已提交
20 21


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


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

	if (nop->get_name)
		return nop->get_name(dir, name, child);
	else
34
		return get_name(&path, name, child);
35
}
L
Linus Torvalds 已提交
36

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

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

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

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

72 73 74 75 76 77 78
/*
 * Find root of a disconnected subtree and return a reference to it.
 */
static struct dentry *
find_disconnected_root(struct dentry *dentry)
{
	dget(dentry);
C
Christoph Hellwig 已提交
79 80 81 82 83 84 85 86
	while (!IS_ROOT(dentry)) {
		struct dentry *parent = dget_parent(dentry);

		if (!(parent->d_flags & DCACHE_DISCONNECTED)) {
			dput(parent);
			break;
		}

87 88 89 90 91 92
		dput(dentry);
		dentry = parent;
	}
	return dentry;
}

93 94
/*
 * Make sure target_dir is fully connected to the dentry tree.
L
Linus Torvalds 已提交
95
 *
96
 * It may already be, as the flag isn't always updated when connection happens.
L
Linus Torvalds 已提交
97
 */
98
static int
99
reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
L
Linus Torvalds 已提交
100
{
101 102
	int noprogress = 0;
	int err = -ESTALE;
L
Linus Torvalds 已提交
103 104

	/*
105
	 * It is possible that a confused file system might not let us complete
L
Linus Torvalds 已提交
106 107 108 109 110 111 112
	 * the path to the root.  For example, if get_parent returns a directory
	 * in which we cannot find a name for the child.  While this implies a
	 * very sick filesystem we don't want it to cause knfsd to spin.  Hence
	 * the noprogress counter.  If we go through the loop 10 times (2 is
	 * probably enough) without getting anywhere, we just give up
	 */
	while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
113
		struct dentry *pd = find_disconnected_root(target_dir);
L
Linus Torvalds 已提交
114 115 116 117 118 119 120

		if (!IS_ROOT(pd)) {
			/* must have found a connected parent - great */
			spin_lock(&pd->d_lock);
			pd->d_flags &= ~DCACHE_DISCONNECTED;
			spin_unlock(&pd->d_lock);
			noprogress = 0;
C
Christoph Hellwig 已提交
121
		} else if (pd == mnt->mnt_sb->s_root) {
L
Linus Torvalds 已提交
122 123 124 125 126 127
			printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
			spin_lock(&pd->d_lock);
			pd->d_flags &= ~DCACHE_DISCONNECTED;
			spin_unlock(&pd->d_lock);
			noprogress = 0;
		} else {
128 129 130 131 132 133 134 135 136 137 138 139 140 141
			/*
			 * We have hit the top of a disconnected path, try to
			 * find parent and connect.
			 *
			 * Racing with some other process renaming a directory
			 * isn't much of a problem here.  If someone renames
			 * the directory, it will end up properly connected,
			 * which is what we want
			 *
			 * Getting the parent can't be supported generically,
			 * the locking is too icky.
			 *
			 * Instead we just return EACCES.  If server reboots
			 * or inodes get flushed, you lose
L
Linus Torvalds 已提交
142
			 */
143
			struct dentry *ppd = ERR_PTR(-EACCES);
L
Linus Torvalds 已提交
144 145
			struct dentry *npd;

146
			mutex_lock(&pd->d_inode->i_mutex);
C
Christoph Hellwig 已提交
147 148
			if (mnt->mnt_sb->s_export_op->get_parent)
				ppd = mnt->mnt_sb->s_export_op->get_parent(pd);
149
			mutex_unlock(&pd->d_inode->i_mutex);
L
Linus Torvalds 已提交
150 151 152

			if (IS_ERR(ppd)) {
				err = PTR_ERR(ppd);
153
				dprintk("%s: get_parent of %ld failed, err %d\n",
154
					__func__, pd->d_inode->i_ino, err);
L
Linus Torvalds 已提交
155 156 157
				dput(pd);
				break;
			}
158

159
			dprintk("%s: find name of %lu in %lu\n", __func__,
160
				pd->d_inode->i_ino, ppd->d_inode->i_ino);
C
Christoph Hellwig 已提交
161
			err = exportfs_get_name(mnt, ppd, nbuf, pd);
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170 171
			if (err) {
				dput(ppd);
				dput(pd);
				if (err == -ENOENT)
					/* some race between get_parent and
					 * get_name?  just try again
					 */
					continue;
				break;
			}
172
			dprintk("%s: found name: %s\n", __func__, nbuf);
173
			mutex_lock(&ppd->d_inode->i_mutex);
L
Linus Torvalds 已提交
174
			npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
175
			mutex_unlock(&ppd->d_inode->i_mutex);
L
Linus Torvalds 已提交
176 177
			if (IS_ERR(npd)) {
				err = PTR_ERR(npd);
178
				dprintk("%s: lookup failed: %d\n",
179
					__func__, err);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191
				dput(ppd);
				dput(pd);
				break;
			}
			/* we didn't really want npd, we really wanted
			 * a side-effect of the lookup.
			 * hopefully, npd == pd, though it isn't really
			 * a problem if it isn't
			 */
			if (npd == pd)
				noprogress = 0;
			else
192
				printk("%s: npd != pd\n", __func__);
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
			dput(npd);
			dput(ppd);
			if (IS_ROOT(pd)) {
				/* something went wrong, we have to give up */
				dput(pd);
				break;
			}
		}
		dput(pd);
	}

	if (target_dir->d_flags & DCACHE_DISCONNECTED) {
		/* something went wrong - oh-well */
		if (!err)
			err = -ESTALE;
208
		return err;
L
Linus Torvalds 已提交
209
	}
210 211 212 213

	return 0;
}

L
Linus Torvalds 已提交
214
struct getdents_callback {
215
	struct dir_context ctx;
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227
	char *name;		/* name that was found. It already points to a
				   buffer NAME_MAX+1 is size */
	unsigned long ino;	/* the inum we are looking for */
	int found;		/* inode matched? */
	int sequence;		/* sequence counter */
};

/*
 * A rather strange filldir function to capture
 * the name matching the specified inode number.
 */
static int filldir_one(void * __buf, const char * name, int len,
228
			loff_t pos, u64 ino, unsigned int d_type)
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
{
	struct getdents_callback *buf = __buf;
	int result = 0;

	buf->sequence++;
	if (buf->ino == ino) {
		memcpy(buf->name, name, len);
		buf->name[len] = '\0';
		buf->found = 1;
		result = -1;
	}
	return result;
}

/**
 * get_name - default export_operations->get_name function
 * @dentry: the directory in which to find a name
 * @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.
 */
252
static int get_name(const struct path *path, char *name, struct dentry *child)
L
Linus Torvalds 已提交
253
{
254
	const struct cred *cred = current_cred();
255
	struct inode *dir = path->dentry->d_inode;
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268
	int error;
	struct file *file;
	struct getdents_callback buffer;

	error = -ENOTDIR;
	if (!dir || !S_ISDIR(dir->i_mode))
		goto out;
	error = -EINVAL;
	if (!dir->i_fop)
		goto out;
	/*
	 * Open the directory ...
	 */
269
	file = dentry_open(path, O_RDONLY, cred);
L
Linus Torvalds 已提交
270 271 272 273 274
	error = PTR_ERR(file);
	if (IS_ERR(file))
		goto out;

	error = -EINVAL;
A
Al Viro 已提交
275
	if (!file->f_op->iterate)
L
Linus Torvalds 已提交
276 277 278 279 280 281
		goto out_close;

	buffer.name = name;
	buffer.ino = child->d_inode->i_ino;
	buffer.found = 0;
	buffer.sequence = 0;
282
	buffer.ctx.actor = filldir_one;
L
Linus Torvalds 已提交
283 284 285
	while (1) {
		int old_seq = buffer.sequence;

286
		error = iterate_dir(file, &buffer.ctx);
287 288 289 290
		if (buffer.found) {
			error = 0;
			break;
		}
L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

		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
A
Al Viro 已提交
308
 * @inode:   the object to encode
L
Linus Torvalds 已提交
309 310
 * @fh:      where to store the file handle fragment
 * @max_len: maximum length to store there
A
Al Viro 已提交
311
 * @parent:  parent directory inode, if wanted
L
Linus Torvalds 已提交
312 313 314 315 316 317
 *
 * 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.
 */
A
Al Viro 已提交
318 319
static int export_encode_fh(struct inode *inode, struct fid *fid,
		int *max_len, struct inode *parent)
L
Linus Torvalds 已提交
320 321
{
	int len = *max_len;
C
Christoph Hellwig 已提交
322
	int type = FILEID_INO32_GEN;
323

A
Al Viro 已提交
324
	if (parent && (len < 4)) {
325
		*max_len = 4;
326
		return FILEID_INVALID;
327 328
	} else if (len < 2) {
		*max_len = 2;
329
		return FILEID_INVALID;
330
	}
L
Linus Torvalds 已提交
331 332

	len = 2;
C
Christoph Hellwig 已提交
333 334
	fid->i32.ino = inode->i_ino;
	fid->i32.gen = inode->i_generation;
A
Al Viro 已提交
335
	if (parent) {
C
Christoph Hellwig 已提交
336 337
		fid->i32.parent_ino = parent->i_ino;
		fid->i32.parent_gen = parent->i_generation;
L
Linus Torvalds 已提交
338
		len = 4;
C
Christoph Hellwig 已提交
339
		type = FILEID_INO32_GEN_PARENT;
L
Linus Torvalds 已提交
340 341 342 343 344
	}
	*max_len = len;
	return type;
}

345 346 347 348 349 350 351 352 353 354 355 356
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);

C
Christoph Hellwig 已提交
357
int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
358 359
		int connectable)
{
360
	int error;
A
Al Viro 已提交
361 362
	struct dentry *p = NULL;
	struct inode *inode = dentry->d_inode, *parent = NULL;
363

A
Al Viro 已提交
364 365 366 367 368 369 370 371
	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;
	}
372 373

	error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
A
Al Viro 已提交
374
	dput(p);
375 376

	return error;
377 378 379
}
EXPORT_SYMBOL_GPL(exportfs_encode_fh);

C
Christoph Hellwig 已提交
380 381 382
struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
		int fh_len, int fileid_type,
		int (*acceptable)(void *, struct dentry *), void *context)
383
{
384
	const struct export_operations *nop = mnt->mnt_sb->s_export_op;
C
Christoph Hellwig 已提交
385
	struct dentry *result, *alias;
386
	char nbuf[NAME_MAX+1];
C
Christoph Hellwig 已提交
387
	int err;
388

C
Christoph Hellwig 已提交
389 390 391
	/*
	 * Try to get any dentry for the given file handle from the filesystem.
	 */
392 393
	if (!nop || !nop->fh_to_dentry)
		return ERR_PTR(-ESTALE);
C
Christoph Hellwig 已提交
394
	result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
395 396
	if (!result)
		result = ERR_PTR(-ESTALE);
C
Christoph Hellwig 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409
	if (IS_ERR(result))
		return result;

	if (S_ISDIR(result->d_inode->i_mode)) {
		/*
		 * 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) {
410
			err = reconnect_path(mnt, result, nbuf);
C
Christoph Hellwig 已提交
411 412 413 414 415 416 417 418 419 420
			if (err)
				goto err_result;
		}

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

		return result;
421
	} else {
C
Christoph Hellwig 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
		/*
		 * 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);
450 451
		if (!target_dir)
			goto err_result;
C
Christoph Hellwig 已提交
452 453 454 455 456 457 458 459 460
		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..
		 */
461
		err = reconnect_path(mnt, target_dir, nbuf);
C
Christoph Hellwig 已提交
462 463 464 465 466 467 468 469 470 471
		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.
		 */
C
Christoph Hellwig 已提交
472
		err = exportfs_get_name(mnt, target_dir, nbuf, result);
C
Christoph Hellwig 已提交
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 498 499 500 501 502 503
		if (!err) {
			mutex_lock(&target_dir->d_inode->i_mutex);
			nresult = lookup_one_len(nbuf, target_dir,
						 strlen(nbuf));
			mutex_unlock(&target_dir->d_inode->i_mutex);
			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;
504 505
	}

C
Christoph Hellwig 已提交
506 507 508
 err_result:
	dput(result);
	return ERR_PTR(err);
509 510 511
}
EXPORT_SYMBOL_GPL(exportfs_decode_fh);

L
Linus Torvalds 已提交
512
MODULE_LICENSE("GPL");