dir.c 15.6 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 *  linux/fs/nfs/dir.c
 *
 *  Copyright (C) 1992  Rick Sladkey
 *
 *  nfs directory handling functions
 */

Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12
#ifdef MODULE
#include <linux/module.h>
#endif

13 14 15 16 17 18 19
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/stat.h>
#include <linux/nfs_fs.h>
#include <linux/fcntl.h>
#include <linux/string.h>
#include <linux/kernel.h>
Linus Torvalds's avatar
Linus Torvalds committed
20
#include <linux/malloc.h>
21 22 23 24 25 26
#include <linux/mm.h>

#include <asm/segment.h>	/* for fs functions */

static int nfs_dir_read(struct inode *, struct file *filp, char *buf,
			int count);
Linus Torvalds's avatar
Linus Torvalds committed
27
static int nfs_readdir(struct inode *, struct file *, void *, filldir_t);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
static int nfs_lookup(struct inode *dir, const char *name, int len,
		      struct inode **result);
static int nfs_create(struct inode *dir, const char *name, int len, int mode,
		      struct inode **result);
static int nfs_mkdir(struct inode *dir, const char *name, int len, int mode);
static int nfs_rmdir(struct inode *dir, const char *name, int len);
static int nfs_unlink(struct inode *dir, const char *name, int len);
static int nfs_symlink(struct inode *inode, const char *name, int len,
		       const char *symname);
static int nfs_link(struct inode *oldinode, struct inode *dir,
		    const char *name, int len);
static int nfs_mknod(struct inode *dir, const char *name, int len, int mode,
		     int rdev);
static int nfs_rename(struct inode *old_dir, const char *old_name,
		      int old_len, struct inode *new_dir, const char *new_name,
		      int new_len);

static struct file_operations nfs_dir_operations = {
	NULL,			/* lseek - default */
	nfs_dir_read,		/* read - bad */
	NULL,			/* write - bad */
	nfs_readdir,		/* readdir */
	NULL,			/* select - default */
	NULL,			/* ioctl - default */
	NULL,			/* mmap */
	NULL,			/* no special open code */
54 55
	NULL,			/* no special release code */
	NULL			/* fsync */
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
};

struct inode_operations nfs_dir_inode_operations = {
	&nfs_dir_operations,	/* default directory file-ops */
	nfs_create,		/* create */
	nfs_lookup,		/* lookup */
	nfs_link,		/* link */
	nfs_unlink,		/* unlink */
	nfs_symlink,		/* symlink */
	nfs_mkdir,		/* mkdir */
	nfs_rmdir,		/* rmdir */
	nfs_mknod,		/* mknod */
	nfs_rename,		/* rename */
	NULL,			/* readlink */
	NULL,			/* follow_link */
	NULL,			/* bmap */
	NULL,			/* truncate */
73
	NULL			/* permission */
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
};

static int nfs_dir_read(struct inode *inode, struct file *filp, char *buf,
			int count)
{
	return -EISDIR;
}

/*
 * We need to do caching of directory entries to prevent an
 * incredible amount of RPC traffic.  Only the most recent open
 * directory is cached.  This seems sufficient for most purposes.
 * Technically, we ought to flush the cache on close but this is
 * not a problem in practice.
 */

static int nfs_readdir(struct inode *inode, struct file *filp,
Linus Torvalds's avatar
Linus Torvalds committed
91
		       void *dirent, filldir_t filldir)
92 93 94 95 96 97 98
{
	static int c_dev = 0;
	static int c_ino;
	static int c_size;
	static struct nfs_entry *c_entry = NULL;

	int result;
Linus Torvalds's avatar
Linus Torvalds committed
99
	int i, index = 0;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	struct nfs_entry *entry;

	if (!inode || !S_ISDIR(inode->i_mode)) {
		printk("nfs_readdir: inode is NULL or not a directory\n");
		return -EBADF;
	}

	/* initialize cache memory if it hasn't been used before */

	if (c_entry == NULL) {
		i = sizeof (struct nfs_entry)*NFS_READDIR_CACHE_SIZE;
		c_entry = (struct nfs_entry *) kmalloc(i, GFP_KERNEL);
		for (i = 0; i < NFS_READDIR_CACHE_SIZE; i++) {
			c_entry[i].name = (char *) kmalloc(NFS_MAXNAMLEN + 1,
				GFP_KERNEL);
		}
	}
	entry = NULL;

	/* try to find it in the cache */

	if (inode->i_dev == c_dev && inode->i_ino == c_ino) {
		for (i = 0; i < c_size; i++) {
			if (filp->f_pos == c_entry[i].cookie) {
				if (i == c_size - 1) {
					if (c_entry[i].eof)
						return 0;
				}
				else
Linus Torvalds's avatar
Linus Torvalds committed
129
					entry = c_entry + (index = i + 1);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
				break;
			}
		}
	}

	/* if we didn't find it in the cache, revert to an nfs call */

	if (!entry) {
		result = nfs_proc_readdir(NFS_SERVER(inode), NFS_FH(inode),
			filp->f_pos, NFS_READDIR_CACHE_SIZE, c_entry);
		if (result < 0) {
			c_dev = 0;
			return result;
		}
		if (result > 0) {
			c_dev = inode->i_dev;
			c_ino = inode->i_ino;
			c_size = result;
Linus Torvalds's avatar
Linus Torvalds committed
148
			entry = c_entry + (index = 0);
149 150 151 152
		}
	}

	/* if we found it in the cache or from an nfs call, return results */
Linus Torvalds's avatar
Linus Torvalds committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	if (!entry)
		return 0;
	while (index < c_size) {
		int nextpos = entry->cookie;
		if (filldir(dirent, entry->name, strlen(entry->name), filp->f_pos, entry->fileid) < 0)
			break;
		filp->f_pos = nextpos;
		/* revalidate the cache if we slept in filldir() */
		if (inode->i_dev != c_dev)
			break;
		if (inode->i_ino != c_ino)
			break;
		if (nextpos != entry->cookie)
			break;
		index++;
		entry++;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	}
	return 0;
}

/*
 * Lookup caching is a big win for performance but this is just
 * a trial to see how well it works on a small scale.
 * For example, bash does a lookup on ".." 13 times for each path
 * element when running pwd.  Yes, hard to believe but true.
 * Try pwd in a filesystem mounted with noac.
 *
 * It trades a little cpu time and memory for a lot of network bandwidth.
 * Since the cache is not hashed yet, it is a good idea not to make it too
 * large because every lookup looks through the entire cache even
 * though most of them will fail.
 */

static struct nfs_lookup_cache_entry {
	int dev;
	int inode;
	char filename[NFS_MAXNAMLEN + 1];
	struct nfs_fh fhandle;
	struct nfs_fattr fattr;
	int expiration_date;
} nfs_lookup_cache[NFS_LOOKUP_CACHE_SIZE];

static struct nfs_lookup_cache_entry *nfs_lookup_cache_index(struct inode *dir,
196
							     const char *filename)
197 198 199 200 201 202 203
{
	struct nfs_lookup_cache_entry *entry;
	int i;

	for (i = 0; i < NFS_LOOKUP_CACHE_SIZE; i++) {
		entry = nfs_lookup_cache + i;
		if (entry->dev == dir->i_dev && entry->inode == dir->i_ino
204
		    && !strncmp(filename, entry->filename, NFS_MAXNAMLEN))
205 206 207 208 209
			return entry;
	}
	return NULL;
}

210
static int nfs_lookup_cache_lookup(struct inode *dir, const char *filename,
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
				   struct nfs_fh *fhandle,
				   struct nfs_fattr *fattr)
{
	static int nfs_lookup_cache_in_use = 0;

	struct nfs_lookup_cache_entry *entry;

	if (!nfs_lookup_cache_in_use) {
		memset(nfs_lookup_cache, 0, sizeof(nfs_lookup_cache));
		nfs_lookup_cache_in_use = 1;
	}
	if ((entry = nfs_lookup_cache_index(dir, filename))) {
		if (jiffies > entry->expiration_date) {
			entry->dev = 0;
			return 0;
		}
		*fhandle = entry->fhandle;
		*fattr = entry->fattr;
		return 1;
	}
	return 0;
}

234
static void nfs_lookup_cache_add(struct inode *dir, const char *filename,
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
				 struct nfs_fh *fhandle,
				 struct nfs_fattr *fattr)
{
	static int nfs_lookup_cache_pos = 0;
	struct nfs_lookup_cache_entry *entry;

	/* compensate for bug in SGI NFS server */
	if (fattr->size == -1 || fattr->uid == -1 || fattr->gid == -1
	    || fattr->atime.seconds == -1 || fattr->mtime.seconds == -1)
		return;
	if (!(entry = nfs_lookup_cache_index(dir, filename))) {
		entry = nfs_lookup_cache + nfs_lookup_cache_pos++;
		if (nfs_lookup_cache_pos == NFS_LOOKUP_CACHE_SIZE)
			nfs_lookup_cache_pos = 0;
	}
	entry->dev = dir->i_dev;
	entry->inode = dir->i_ino;
	strcpy(entry->filename, filename);
	entry->fhandle = *fhandle;
	entry->fattr = *fattr;
255 256
	entry->expiration_date = jiffies + (S_ISDIR(fattr->mode)
		? NFS_SERVER(dir)->acdirmax : NFS_SERVER(dir)->acregmax);
257 258
}

259
static void nfs_lookup_cache_remove(struct inode *dir, struct inode *inode,
260
				    const char *filename)
261 262
{
	struct nfs_lookup_cache_entry *entry;
263 264 265
	int dev;
	int fileid;
	int i;
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	if (inode) {
		dev = inode->i_dev;
		fileid = inode->i_ino;
	}
	else if ((entry = nfs_lookup_cache_index(dir, filename))) {
		dev = entry->dev;
		fileid = entry->fattr.fileid;
	}
	else
		return;
	for (i = 0; i < NFS_LOOKUP_CACHE_SIZE; i++) {
		entry = nfs_lookup_cache + i;
		if (entry->dev == dev && entry->fattr.fileid == fileid)
			entry->dev = 0;
	}
282 283 284 285 286 287
}

static void nfs_lookup_cache_refresh(struct inode *file,
				     struct nfs_fattr *fattr)
{
	struct nfs_lookup_cache_entry *entry;
288 289
	int dev = file->i_dev;
	int fileid = file->i_ino;
290 291 292 293
	int i;

	for (i = 0; i < NFS_LOOKUP_CACHE_SIZE; i++) {
		entry = nfs_lookup_cache + i;
294
		if (entry->dev == dev && entry->fattr.fileid == fileid)
295 296 297 298
			entry->fattr = *fattr;
	}
}

299
static int nfs_lookup(struct inode *dir, const char *__name, int len,
300 301 302 303
		      struct inode **result)
{
	struct nfs_fh fhandle;
	struct nfs_fattr fattr;
304
	char name[len > NFS_MAXNAMLEN? 1 : len+1];
305 306 307 308 309 310 311 312 313 314 315 316
	int error;

	*result = NULL;
	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_lookup: inode is NULL or not a directory\n");
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(dir);
		return -ENAMETOOLONG;
	}
317 318
	memcpy(name,__name,len);
	name[len] = '\0';
319
	if (len == 1 && name[0] == '.') { /* cheat for "." */
320 321 322 323
		*result = dir;
		return 0;
	}
	if ((NFS_SERVER(dir)->flags & NFS_MOUNT_NOAC)
324
	    || !nfs_lookup_cache_lookup(dir, name, &fhandle, &fattr)) {
325
		if ((error = nfs_proc_lookup(NFS_SERVER(dir), NFS_FH(dir),
326
		    name, &fhandle, &fattr))) {
327 328 329
			iput(dir);
			return error;
		}
330
		nfs_lookup_cache_add(dir, name, &fhandle, &fattr);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	}
	if (!(*result = nfs_fhget(dir->i_sb, &fhandle, &fattr))) {
		iput(dir);
		return -EACCES;
	}
	iput(dir);
	return 0;
}

static int nfs_create(struct inode *dir, const char *name, int len, int mode,
		      struct inode **result)
{
	struct nfs_sattr sattr;
	struct nfs_fattr fattr;
	struct nfs_fh fhandle;
	int error;

	*result = NULL;
	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_create: inode is NULL or not a directory\n");
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(dir);
		return -ENAMETOOLONG;
	}
	sattr.mode = mode;
359 360
	sattr.uid = sattr.gid = sattr.size = (unsigned) -1;
	sattr.atime.seconds = sattr.mtime.seconds = (unsigned) -1;
361
	if ((error = nfs_proc_create(NFS_SERVER(dir), NFS_FH(dir),
362
		name, &sattr, &fhandle, &fattr))) {
363 364 365 366 367 368 369
		iput(dir);
		return error;
	}
	if (!(*result = nfs_fhget(dir->i_sb, &fhandle, &fattr))) {
		iput(dir);
		return -EACCES;
	}
370
	nfs_lookup_cache_add(dir, name, &fhandle, &fattr);
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	iput(dir);
	return 0;
}

static int nfs_mknod(struct inode *dir, const char *name, int len,
		     int mode, int rdev)
{
	struct nfs_sattr sattr;
	struct nfs_fattr fattr;
	struct nfs_fh fhandle;
	int error;

	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_mknod: inode is NULL or not a directory\n");
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(dir);
		return -ENAMETOOLONG;
	}
	sattr.mode = mode;
393
	sattr.uid = sattr.gid = (unsigned) -1;
394 395 396
	if (S_ISCHR(mode) || S_ISBLK(mode))
		sattr.size = rdev; /* get out your barf bag */
	else
397 398
		sattr.size = (unsigned) -1;
	sattr.atime.seconds = sattr.mtime.seconds = (unsigned) -1;
399
	error = nfs_proc_create(NFS_SERVER(dir), NFS_FH(dir),
400
		name, &sattr, &fhandle, &fattr);
401
	if (!error)
402
		nfs_lookup_cache_add(dir, name, &fhandle, &fattr);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
	iput(dir);
	return error;
}

static int nfs_mkdir(struct inode *dir, const char *name, int len, int mode)
{
	struct nfs_sattr sattr;
	struct nfs_fattr fattr;
	struct nfs_fh fhandle;
	int error;

	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_mkdir: inode is NULL or not a directory\n");
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(dir);
		return -ENAMETOOLONG;
	}
	sattr.mode = mode;
424 425
	sattr.uid = sattr.gid = sattr.size = (unsigned) -1;
	sattr.atime.seconds = sattr.mtime.seconds = (unsigned) -1;
426
	error = nfs_proc_mkdir(NFS_SERVER(dir), NFS_FH(dir),
427
		name, &sattr, &fhandle, &fattr);
428
	if (!error)
429
		nfs_lookup_cache_add(dir, name, &fhandle, &fattr);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	iput(dir);
	return error;
}

static int nfs_rmdir(struct inode *dir, const char *name, int len)
{
	int error;

	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_rmdir: inode is NULL or not a directory\n");
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(dir);
		return -ENAMETOOLONG;
	}
447
	error = nfs_proc_rmdir(NFS_SERVER(dir), NFS_FH(dir), name);
448
	if (!error)
449
		nfs_lookup_cache_remove(dir, NULL, name);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	iput(dir);
	return error;
}

static int nfs_unlink(struct inode *dir, const char *name, int len)
{
	int error;

	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_unlink: inode is NULL or not a directory\n");
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(dir);
		return -ENAMETOOLONG;
	}
467
	error = nfs_proc_remove(NFS_SERVER(dir), NFS_FH(dir), name);
468
	if (!error)
469
		nfs_lookup_cache_remove(dir, NULL, name);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
	iput(dir);
	return error;
}

static int nfs_symlink(struct inode *dir, const char *name, int len,
		       const char *symname)
{
	struct nfs_sattr sattr;
	int error;

	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_symlink: inode is NULL or not a directory\n");
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(dir);
		return -ENAMETOOLONG;
	}
489
	if (strlen(symname) > NFS_MAXPATHLEN) {
490 491 492
		iput(dir);
		return -ENAMETOOLONG;
	}
493
	sattr.mode = S_IFLNK | S_IRWXUGO; /* SunOS 4.1.2 crashes without this! */
494 495
	sattr.uid = sattr.gid = sattr.size = (unsigned) -1;
	sattr.atime.seconds = sattr.mtime.seconds = (unsigned) -1;
496
	error = nfs_proc_symlink(NFS_SERVER(dir), NFS_FH(dir),
497
		name, symname, &sattr);
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	iput(dir);
	return error;
}

static int nfs_link(struct inode *oldinode, struct inode *dir,
		    const char *name, int len)
{
	int error;

	if (!oldinode) {
		printk("nfs_link: old inode is NULL\n");
		iput(oldinode);
		iput(dir);
		return -ENOENT;
	}
	if (!dir || !S_ISDIR(dir->i_mode)) {
		printk("nfs_link: dir is NULL or not a directory\n");
		iput(oldinode);
		iput(dir);
		return -ENOENT;
	}
	if (len > NFS_MAXNAMLEN) {
		iput(oldinode);
		iput(dir);
		return -ENAMETOOLONG;
	}
	error = nfs_proc_link(NFS_SERVER(oldinode), NFS_FH(oldinode),
525
		NFS_FH(dir), name);
526 527
	if (!error)
		nfs_lookup_cache_remove(dir, oldinode, NULL);
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	iput(oldinode);
	iput(dir);
	return error;
}

static int nfs_rename(struct inode *old_dir, const char *old_name, int old_len,
		      struct inode *new_dir, const char *new_name, int new_len)
{
	int error;

	if (!old_dir || !S_ISDIR(old_dir->i_mode)) {
		printk("nfs_rename: old inode is NULL or not a directory\n");
		iput(old_dir);
		iput(new_dir);
		return -ENOENT;
	}
	if (!new_dir || !S_ISDIR(new_dir->i_mode)) {
		printk("nfs_rename: new inode is NULL or not a directory\n");
		iput(old_dir);
		iput(new_dir);
		return -ENOENT;
	}
	if (old_len > NFS_MAXNAMLEN || new_len > NFS_MAXNAMLEN) {
		iput(old_dir);
		iput(new_dir);
		return -ENAMETOOLONG;
	}
	error = nfs_proc_rename(NFS_SERVER(old_dir),
556 557
		NFS_FH(old_dir), old_name,
		NFS_FH(new_dir), new_name);
558
	if (!error) {
559 560
		nfs_lookup_cache_remove(old_dir, NULL, old_name);
		nfs_lookup_cache_remove(new_dir, NULL, new_name);
561
	}
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	iput(old_dir);
	iput(new_dir);
	return error;
}

/*
 * Many nfs protocol calls return the new file attributes after
 * an operation.  Here we update the inode to reflect the state
 * of the server's inode.
 */

void nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
{
	int was_empty;

	if (!inode || !fattr) {
		printk("nfs_refresh_inode: inode or fattr is NULL\n");
		return;
	}
	if (inode->i_ino != fattr->fileid) {
		printk("nfs_refresh_inode: inode number mismatch\n");
		return;
	}
	was_empty = inode->i_mode == 0;
	inode->i_mode = fattr->mode;
	inode->i_nlink = fattr->nlink;
	inode->i_uid = fattr->uid;
	inode->i_gid = fattr->gid;
	inode->i_size = fattr->size;
	inode->i_blksize = fattr->blocksize;
	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
		inode->i_rdev = fattr->rdev;
	else
		inode->i_rdev = 0;
	inode->i_blocks = fattr->blocks;
	inode->i_atime = fattr->atime.seconds;
	inode->i_mtime = fattr->mtime.seconds;
	inode->i_ctime = fattr->ctime.seconds;
	if (was_empty) {
		if (S_ISREG(inode->i_mode))
			inode->i_op = &nfs_file_inode_operations;
		else if (S_ISDIR(inode->i_mode))
			inode->i_op = &nfs_dir_inode_operations;
		else if (S_ISLNK(inode->i_mode))
			inode->i_op = &nfs_symlink_inode_operations;
		else if (S_ISCHR(inode->i_mode))
608
			inode->i_op = &chrdev_inode_operations;
609
		else if (S_ISBLK(inode->i_mode))
610
			inode->i_op = &blkdev_inode_operations;
611 612
		else if (S_ISFIFO(inode->i_mode))
			init_fifo(inode);
613 614 615 616 617 618
		else
			inode->i_op = NULL;
	}
	nfs_lookup_cache_refresh(inode, fattr);
}