expfs.c 13.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1

2
#include <linux/exportfs.h>
Linus Torvalds's avatar
Linus Torvalds committed
3 4 5
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/module.h>
6
#include <linux/mount.h>
Linus Torvalds's avatar
Linus Torvalds committed
7 8
#include <linux/namei.h>

9
#define dprintk(fmt, args...) do{}while(0)
Linus Torvalds's avatar
Linus Torvalds committed
10 11


12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
static int get_name(struct dentry *dentry, char *name,
		struct dentry *child);


static struct dentry *exportfs_get_dentry(struct super_block *sb, void *obj)
{
	struct dentry *result = ERR_PTR(-ESTALE);

	if (sb->s_export_op->get_dentry) {
		result = sb->s_export_op->get_dentry(sb, obj);
		if (!result)
			result = ERR_PTR(-ESTALE);
	}

	return result;
}

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);
}
Linus Torvalds's avatar
Linus Torvalds committed
39

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

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

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	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;
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/*
 * 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;
}

94 95 96

/*
 * Make sure target_dir is fully connected to the dentry tree.
Linus Torvalds's avatar
Linus Torvalds committed
97
 *
98
 * It may already be, as the flag isn't always updated when connection happens.
Linus Torvalds's avatar
Linus Torvalds committed
99
 */
100 101
static int
reconnect_path(struct super_block *sb, struct dentry *target_dir)
Linus Torvalds's avatar
Linus Torvalds committed
102 103
{
	char nbuf[NAME_MAX+1];
104 105
	int noprogress = 0;
	int err = -ESTALE;
Linus Torvalds's avatar
Linus Torvalds committed
106 107

	/*
108
	 * It is possible that a confused file system might not let us complete
Linus Torvalds's avatar
Linus Torvalds committed
109 110 111 112 113 114 115
	 * 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) {
116
		struct dentry *pd = find_disconnected_root(target_dir);
Linus Torvalds's avatar
Linus Torvalds committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130

		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 {
131 132 133 134 135 136 137 138 139 140 141 142 143 144
			/*
			 * 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
Linus Torvalds's avatar
Linus Torvalds committed
145
			 */
146
			struct dentry *ppd = ERR_PTR(-EACCES);
Linus Torvalds's avatar
Linus Torvalds committed
147 148
			struct dentry *npd;

149
			mutex_lock(&pd->d_inode->i_mutex);
150 151
			if (sb->s_export_op->get_parent)
				ppd = sb->s_export_op->get_parent(pd);
152
			mutex_unlock(&pd->d_inode->i_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
153 154 155

			if (IS_ERR(ppd)) {
				err = PTR_ERR(ppd);
156 157
				dprintk("%s: get_parent of %ld failed, err %d\n",
					__FUNCTION__, pd->d_inode->i_ino, err);
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160
				dput(pd);
				break;
			}
161 162 163

			dprintk("%s: find name of %lu in %lu\n", __FUNCTION__,
				pd->d_inode->i_ino, ppd->d_inode->i_ino);
164
			err = exportfs_get_name(ppd, nbuf, pd);
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168 169 170 171 172 173 174
			if (err) {
				dput(ppd);
				dput(pd);
				if (err == -ENOENT)
					/* some race between get_parent and
					 * get_name?  just try again
					 */
					continue;
				break;
			}
175
			dprintk("%s: found name: %s\n", __FUNCTION__, nbuf);
176
			mutex_lock(&ppd->d_inode->i_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
177
			npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
178
			mutex_unlock(&ppd->d_inode->i_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
179 180
			if (IS_ERR(npd)) {
				err = PTR_ERR(npd);
181 182
				dprintk("%s: lookup failed: %d\n",
					__FUNCTION__, err);
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186 187 188 189 190 191 192 193 194
				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
195
				printk("%s: npd != pd\n", __FUNCTION__);
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
			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;
211
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
212
	}
213 214 215 216 217 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

	return 0;
}

/**
 * find_exported_dentry - helper routine to implement export_operations->decode_fh
 * @sb:		The &super_block identifying the filesystem
 * @obj:	An opaque identifier of the object to be found - passed to
 *		get_inode
 * @parent:	An optional opqaue identifier of the parent of the object.
 * @acceptable:	A function used to test possible &dentries to see if they are
 *		acceptable
 * @context:	A parameter to @acceptable so that it knows on what basis to
 *		judge.
 *
 * find_exported_dentry is the central helper routine to enable file systems
 * to provide the decode_fh() export_operation.  It's main task is to take
 * an &inode, find or create an appropriate &dentry structure, and possibly
 * splice this into the dcache in the correct place.
 *
 * The decode_fh() operation provided by the filesystem should call
 * find_exported_dentry() with the same parameters that it received except
 * that instead of the file handle fragment, pointers to opaque identifiers
 * for the object and optionally its parent are passed.  The default decode_fh
 * routine passes one pointer to the start of the filehandle fragment, and
 * one 8 bytes into the fragment.  It is expected that most filesystems will
 * take this approach, though the offset to the parent identifier may well be
 * different.
 *
 * find_exported_dentry() will call get_dentry to get an dentry pointer from
 * the file system.  If any &dentry in the d_alias list is acceptable, it will
 * be returned.  Otherwise find_exported_dentry() will attempt to splice a new
 * &dentry into the dcache using get_name() and get_parent() to find the
 * appropriate place.
 */

struct dentry *
find_exported_dentry(struct super_block *sb, void *obj, void *parent,
		     int (*acceptable)(void *context, struct dentry *de),
		     void *context)
{
	struct dentry *result, *alias;
	int err = -ESTALE;

	/*
	 * Attempt to find the inode.
	 */
	result = exportfs_get_dentry(sb, obj);
	if (IS_ERR(result))
		return result;

	if (S_ISDIR(result->d_inode->i_mode)) {
		if (!(result->d_flags & DCACHE_DISCONNECTED)) {
			if (acceptable(context, result))
				return result;
			err = -EACCES;
			goto err_result;
		}

		err = reconnect_path(sb, result);
		if (err)
			goto err_result;
	} else {
		struct dentry *target_dir, *nresult;
		char nbuf[NAME_MAX+1];

		alias = find_acceptable_alias(result, acceptable, context);
		if (alias)
			return alias;

		if (parent == NULL)
			goto err_result;

		target_dir = exportfs_get_dentry(sb,parent);
		if (IS_ERR(target_dir)) {
			err = PTR_ERR(target_dir);
			goto err_result;
		}

		err = reconnect_path(sb, target_dir);
		if (err) {
			dput(target_dir);
			goto err_result;
		}

		/*
		 * As we weren't after a directory, have one more step to go.
		 */
301
		err = exportfs_get_name(target_dir, nbuf, result);
Linus Torvalds's avatar
Linus Torvalds committed
302
		if (!err) {
303
			mutex_lock(&target_dir->d_inode->i_mutex);
304 305
			nresult = lookup_one_len(nbuf, target_dir,
						 strlen(nbuf));
306
			mutex_unlock(&target_dir->d_inode->i_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
307 308 309 310 311 312 313 314
			if (!IS_ERR(nresult)) {
				if (nresult->d_inode) {
					dput(result);
					result = nresult;
				} else
					dput(nresult);
			}
		}
315
		dput(target_dir);
Linus Torvalds's avatar
Linus Torvalds committed
316
	}
317 318 319 320

	alias = find_acceptable_alias(result, acceptable, context);
	if (alias)
		return alias;
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323 324 325

	/* drat - I just cannot find anything acceptable */
	dput(result);
	/* It might be justifiable to return ESTALE here,
	 * but the filehandle at-least looks reasonable good
326
	 * and it may just be a permission problem, so returning
Linus Torvalds's avatar
Linus Torvalds committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	 * -EACCESS is safer
	 */
	return ERR_PTR(-EACCES);

 err_result:
	dput(result);
	return ERR_PTR(err);
}

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,
349
			loff_t pos, u64 ino, unsigned int d_type)
Linus Torvalds's avatar
Linus Torvalds committed
350 351 352 353 354 355 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 386 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
{
	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.
 */
Christoph Hellwig's avatar
Christoph Hellwig committed
437 438
static int export_encode_fh(struct dentry *dentry, struct fid *fid,
		int *max_len, int connectable)
Linus Torvalds's avatar
Linus Torvalds committed
439 440 441
{
	struct inode * inode = dentry->d_inode;
	int len = *max_len;
Christoph Hellwig's avatar
Christoph Hellwig committed
442
	int type = FILEID_INO32_GEN;
Linus Torvalds's avatar
Linus Torvalds committed
443 444 445 446 447
	
	if (len < 2 || (connectable && len < 4))
		return 255;

	len = 2;
Christoph Hellwig's avatar
Christoph Hellwig committed
448 449
	fid->i32.ino = inode->i_ino;
	fid->i32.gen = inode->i_generation;
Linus Torvalds's avatar
Linus Torvalds committed
450 451 452 453 454
	if (connectable && !S_ISDIR(inode->i_mode)) {
		struct inode *parent;

		spin_lock(&dentry->d_lock);
		parent = dentry->d_parent->d_inode;
Christoph Hellwig's avatar
Christoph Hellwig committed
455 456
		fid->i32.parent_ino = parent->i_ino;
		fid->i32.parent_gen = parent->i_generation;
Linus Torvalds's avatar
Linus Torvalds committed
457 458
		spin_unlock(&dentry->d_lock);
		len = 4;
Christoph Hellwig's avatar
Christoph Hellwig committed
459
		type = FILEID_INO32_GEN_PARENT;
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464 465 466 467 468 469 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
	}
	*max_len = len;
	return type;
}


/**
 * export_decode_fh - default export_operations->decode_fh function
 * @sb:  The superblock
 * @fh:  pointer to the file handle fragment
 * @fh_len: length of file handle fragment
 * @acceptable: function for testing acceptability of dentrys
 * @context:   context for @acceptable
 *
 * This is the default decode_fh() function.
 * a fileid_type of 1 indicates that the filehandlefragment
 * just contains an object identifier understood by  get_dentry.
 * a fileid_type of 2 says that there is also a directory
 * identifier 8 bytes in to the filehandlefragement.
 */
static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
			      int fileid_type,
			 int (*acceptable)(void *context, struct dentry *de),
			 void *context)
{
	__u32 parent[2];
	parent[0] = parent[1] = 0;
	if (fh_len < 2 || fileid_type > 2)
		return NULL;
	if (fileid_type == 2) {
		if (fh_len > 2) parent[0] = fh[2];
		if (fh_len > 3) parent[1] = fh[3];
	}
	return find_exported_dentry(sb, fh, parent,
				   acceptable, context);
}

Christoph Hellwig's avatar
Christoph Hellwig committed
497
int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
498 499
		int connectable)
{
500 501
 	struct export_operations *nop = dentry->d_sb->s_export_op;
	int error;
502

503
	if (nop->encode_fh)
Christoph Hellwig's avatar
Christoph Hellwig committed
504
		error = nop->encode_fh(dentry, fid->raw, max_len, connectable);
505
	else
Christoph Hellwig's avatar
Christoph Hellwig committed
506
		error = export_encode_fh(dentry, fid, max_len, connectable);
507 508

	return error;
509 510 511
}
EXPORT_SYMBOL_GPL(exportfs_encode_fh);

Christoph Hellwig's avatar
Christoph Hellwig committed
512 513 514
struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
		int fh_len, int fileid_type,
		int (*acceptable)(void *, struct dentry *), void *context)
515 516
{
	struct export_operations *nop = mnt->mnt_sb->s_export_op;
517
	struct dentry *result;
518

519
	if (nop->decode_fh) {
Christoph Hellwig's avatar
Christoph Hellwig committed
520 521
		result = nop->decode_fh(mnt->mnt_sb, fid->raw, fh_len,
					fileid_type, acceptable, context);
522
	} else {
Christoph Hellwig's avatar
Christoph Hellwig committed
523 524
		result = export_decode_fh(mnt->mnt_sb, fid->raw, fh_len,
					  fileid_type, acceptable, context);
525 526 527
	}

	return result;
528 529 530
}
EXPORT_SYMBOL_GPL(exportfs_decode_fh);

Linus Torvalds's avatar
Linus Torvalds committed
531 532 533
EXPORT_SYMBOL(find_exported_dentry);

MODULE_LICENSE("GPL");