expfs.c 11.8 KB
Newer Older
L
Linus Torvalds 已提交
1

2
#include <linux/exportfs.h>
L
Linus Torvalds 已提交
3 4 5
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/module.h>
6
#include <linux/mount.h>
L
Linus Torvalds 已提交
7 8
#include <linux/namei.h>

9
#define dprintk(fmt, args...) do{}while(0)
L
Linus Torvalds 已提交
10 11


12 13 14 15 16 17 18 19 20 21 22 23 24 25
static int get_name(struct dentry *dentry, char *name,
		struct dentry *child);


static int exportfs_get_name(struct dentry *dir, char *name,
		struct dentry *child)
{
	struct export_operations *nop = dir->d_sb->s_export_op;

	if (nop->get_name)
		return nop->get_name(dir, name, child);
	else
		return get_name(dir, name, child);
}
L
Linus Torvalds 已提交
26

27 28 29
/*
 * Check if the dentry or any of it's aliases is acceptable.
 */
30 31 32 33 34 35 36
static struct dentry *
find_acceptable_alias(struct dentry *result,
		int (*acceptable)(void *context, struct dentry *dentry),
		void *context)
{
	struct dentry *dentry, *toput = NULL;

37 38 39
	if (acceptable(context, result))
		return result;

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	spin_lock(&dcache_lock);
	list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
		dget_locked(dentry);
		spin_unlock(&dcache_lock);
		if (toput)
			dput(toput);
		if (dentry != result && acceptable(context, dentry)) {
			dput(result);
			return dentry;
		}
		spin_lock(&dcache_lock);
		toput = dentry;
	}
	spin_unlock(&dcache_lock);

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

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/*
 * Find root of a disconnected subtree and return a reference to it.
 */
static struct dentry *
find_disconnected_root(struct dentry *dentry)
{
	dget(dentry);
	spin_lock(&dentry->d_lock);
	while (!IS_ROOT(dentry) &&
	       (dentry->d_parent->d_flags & DCACHE_DISCONNECTED)) {
		struct dentry *parent = dentry->d_parent;
		dget(parent);
		spin_unlock(&dentry->d_lock);
		dput(dentry);
		dentry = parent;
		spin_lock(&dentry->d_lock);
	}
	spin_unlock(&dentry->d_lock);
	return dentry;
}

81 82 83

/*
 * Make sure target_dir is fully connected to the dentry tree.
L
Linus Torvalds 已提交
84
 *
85
 * It may already be, as the flag isn't always updated when connection happens.
L
Linus Torvalds 已提交
86
 */
87 88
static int
reconnect_path(struct super_block *sb, struct dentry *target_dir)
L
Linus Torvalds 已提交
89 90
{
	char nbuf[NAME_MAX+1];
91 92
	int noprogress = 0;
	int err = -ESTALE;
L
Linus Torvalds 已提交
93 94

	/*
95
	 * It is possible that a confused file system might not let us complete
L
Linus Torvalds 已提交
96 97 98 99 100 101 102
	 * 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) {
103
		struct dentry *pd = find_disconnected_root(target_dir);
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117

		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;
		} else if (pd == sb->s_root) {
			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 {
118 119 120 121 122 123 124 125 126 127 128 129 130 131
			/*
			 * 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 已提交
132
			 */
133
			struct dentry *ppd = ERR_PTR(-EACCES);
L
Linus Torvalds 已提交
134 135
			struct dentry *npd;

136
			mutex_lock(&pd->d_inode->i_mutex);
137 138
			if (sb->s_export_op->get_parent)
				ppd = sb->s_export_op->get_parent(pd);
139
			mutex_unlock(&pd->d_inode->i_mutex);
L
Linus Torvalds 已提交
140 141 142

			if (IS_ERR(ppd)) {
				err = PTR_ERR(ppd);
143 144
				dprintk("%s: get_parent of %ld failed, err %d\n",
					__FUNCTION__, pd->d_inode->i_ino, err);
L
Linus Torvalds 已提交
145 146 147
				dput(pd);
				break;
			}
148 149 150

			dprintk("%s: find name of %lu in %lu\n", __FUNCTION__,
				pd->d_inode->i_ino, ppd->d_inode->i_ino);
151
			err = exportfs_get_name(ppd, nbuf, pd);
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160 161
			if (err) {
				dput(ppd);
				dput(pd);
				if (err == -ENOENT)
					/* some race between get_parent and
					 * get_name?  just try again
					 */
					continue;
				break;
			}
162
			dprintk("%s: found name: %s\n", __FUNCTION__, nbuf);
163
			mutex_lock(&ppd->d_inode->i_mutex);
L
Linus Torvalds 已提交
164
			npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
165
			mutex_unlock(&ppd->d_inode->i_mutex);
L
Linus Torvalds 已提交
166 167
			if (IS_ERR(npd)) {
				err = PTR_ERR(npd);
168 169
				dprintk("%s: lookup failed: %d\n",
					__FUNCTION__, err);
L
Linus Torvalds 已提交
170 171 172 173 174 175 176 177 178 179 180 181
				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
182
				printk("%s: npd != pd\n", __FUNCTION__);
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
			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;
198
		return err;
L
Linus Torvalds 已提交
199
	}
200 201 202 203

	return 0;
}

L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
struct getdents_callback {
	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,
217
			loff_t pos, u64 ino, unsigned int d_type)
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
{
	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.
 */
static int get_name(struct dentry *dentry, char *name,
			struct dentry *child)
{
	struct inode *dir = dentry->d_inode;
	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 ...
	 */
	file = dentry_open(dget(dentry), NULL, O_RDONLY);
	error = PTR_ERR(file);
	if (IS_ERR(file))
		goto out;

	error = -EINVAL;
	if (!file->f_op->readdir)
		goto out_close;

	buffer.name = name;
	buffer.ino = child->d_inode->i_ino;
	buffer.found = 0;
	buffer.sequence = 0;
	while (1) {
		int old_seq = buffer.sequence;

		error = vfs_readdir(file, filldir_one, &buffer);

		if (error < 0)
			break;

		error = 0;
		if (buffer.found)
			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
 * @dentry:  the dentry to encode
 * @fh:      where to store the file handle fragment
 * @max_len: maximum length to store there
 * @connectable: whether to store parent information
 *
 * 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.
 */
C
Christoph Hellwig 已提交
305 306
static int export_encode_fh(struct dentry *dentry, struct fid *fid,
		int *max_len, int connectable)
L
Linus Torvalds 已提交
307 308 309
{
	struct inode * inode = dentry->d_inode;
	int len = *max_len;
C
Christoph Hellwig 已提交
310
	int type = FILEID_INO32_GEN;
L
Linus Torvalds 已提交
311 312 313 314 315
	
	if (len < 2 || (connectable && len < 4))
		return 255;

	len = 2;
C
Christoph Hellwig 已提交
316 317
	fid->i32.ino = inode->i_ino;
	fid->i32.gen = inode->i_generation;
L
Linus Torvalds 已提交
318 319 320 321 322
	if (connectable && !S_ISDIR(inode->i_mode)) {
		struct inode *parent;

		spin_lock(&dentry->d_lock);
		parent = dentry->d_parent->d_inode;
C
Christoph Hellwig 已提交
323 324
		fid->i32.parent_ino = parent->i_ino;
		fid->i32.parent_gen = parent->i_generation;
L
Linus Torvalds 已提交
325 326
		spin_unlock(&dentry->d_lock);
		len = 4;
C
Christoph Hellwig 已提交
327
		type = FILEID_INO32_GEN_PARENT;
L
Linus Torvalds 已提交
328 329 330 331 332
	}
	*max_len = len;
	return type;
}

C
Christoph Hellwig 已提交
333
int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
334 335
		int connectable)
{
336 337
 	struct export_operations *nop = dentry->d_sb->s_export_op;
	int error;
338

339
	if (nop->encode_fh)
C
Christoph Hellwig 已提交
340
		error = nop->encode_fh(dentry, fid->raw, max_len, connectable);
341
	else
C
Christoph Hellwig 已提交
342
		error = export_encode_fh(dentry, fid, max_len, connectable);
343 344

	return error;
345 346 347
}
EXPORT_SYMBOL_GPL(exportfs_encode_fh);

C
Christoph Hellwig 已提交
348 349 350
struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
		int fh_len, int fileid_type,
		int (*acceptable)(void *, struct dentry *), void *context)
351 352
{
	struct export_operations *nop = mnt->mnt_sb->s_export_op;
C
Christoph Hellwig 已提交
353 354
	struct dentry *result, *alias;
	int err;
355

C
Christoph Hellwig 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	/*
	 * Try to get any dentry for the given file handle from the filesystem.
	 */
	result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
	if (!result)
		result = ERR_PTR(-ESTALE);
	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) {
			err = reconnect_path(mnt->mnt_sb, result);
			if (err)
				goto err_result;
		}

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

		return result;
386
	} else {
C
Christoph Hellwig 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
		/*
		 * It's not a directory.  Life is a little more complicated.
		 */
		struct dentry *target_dir, *nresult;
		char nbuf[NAME_MAX+1];

		/*
		 * 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);
		if (!target_dir)
			goto err_result;
		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..
		 */
		err = reconnect_path(mnt->mnt_sb, target_dir);
		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.
		 */
		err = exportfs_get_name(target_dir, nbuf, result);
		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;
470 471
	}

C
Christoph Hellwig 已提交
472 473 474
 err_result:
	dput(result);
	return ERR_PTR(err);
475 476 477
}
EXPORT_SYMBOL_GPL(exportfs_decode_fh);

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