dir.c 17.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14

/*
 * Directory operations for Coda filesystem
 * Original version: (C) 1996 P. Braam and M. Callahan
 * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
 * 
 * Carnegie Mellon encourages users to contribute improvements to
 * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/time.h>
#include <linux/fs.h>
15
#include <linux/slab.h>
L
Linus Torvalds 已提交
16 17 18 19 20
#include <linux/file.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/smp_lock.h>
21
#include <linux/spinlock.h>
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30

#include <asm/uaccess.h>

#include <linux/coda.h>
#include <linux/coda_linux.h>
#include <linux/coda_psdev.h>
#include <linux/coda_fs_i.h>
#include <linux/coda_cache.h>

31 32
#include "coda_int.h"

L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* dir inode-ops */
static int coda_create(struct inode *dir, struct dentry *new, int mode, struct nameidata *nd);
static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, struct nameidata *nd);
static int coda_link(struct dentry *old_dentry, struct inode *dir_inode, 
		     struct dentry *entry);
static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
			const char *symname);
static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, int mode);
static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
static int coda_rename(struct inode *old_inode, struct dentry *old_dentry, 
                       struct inode *new_inode, struct dentry *new_dentry);

/* dir file-ops */
47
static int coda_readdir(struct file *file, void *buf, filldir_t filldir);
L
Linus Torvalds 已提交
48 49 50 51 52 53

/* dentry ops */
static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd);
static int coda_dentry_delete(struct dentry *);

/* support routines */
54 55
static int coda_venus_readdir(struct file *coda_file, void *buf,
			      filldir_t filldir);
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63

/* same as fs/bad_inode.c */
static int coda_return_EIO(void)
{
	return -EIO;
}
#define CODA_EIO_ERROR ((void *) (coda_return_EIO))

64
static const struct dentry_operations coda_dentry_operations =
L
Linus Torvalds 已提交
65 66 67 68 69
{
	.d_revalidate	= coda_dentry_revalidate,
	.d_delete	= coda_dentry_delete,
};

70
const struct inode_operations coda_dir_inode_operations =
L
Linus Torvalds 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
	.create		= coda_create,
	.lookup		= coda_lookup,
	.link		= coda_link,
	.unlink		= coda_unlink,
	.symlink	= coda_symlink,
	.mkdir		= coda_mkdir,
	.rmdir		= coda_rmdir,
	.mknod		= CODA_EIO_ERROR,
	.rename		= coda_rename,
	.permission	= coda_permission,
	.getattr	= coda_getattr,
	.setattr	= coda_setattr,
};

86
const struct file_operations coda_dir_operations = {
L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
	.llseek		= generic_file_llseek,
	.read		= generic_read_dir,
	.readdir	= coda_readdir,
	.open		= coda_open,
	.release	= coda_release,
	.fsync		= coda_fsync,
};


/* inode operations for directories */
/* access routines: lookup, readlink, permission */
static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, struct nameidata *nd)
{
100
	struct inode *inode = NULL;
L
Linus Torvalds 已提交
101 102 103 104 105
	struct CodaFid resfid = { { 0, } };
	int type = 0;
	int error = 0;
	const char *name = entry->d_name.name;
	size_t length = entry->d_name.len;
106 107 108

	if (length > CODA_MAXNAMLEN) {
		printk(KERN_ERR "name too long: lookup, %s (%*s)\n",
L
Linus Torvalds 已提交
109 110 111 112
		       coda_i2s(dir), (int)length, name);
		return ERR_PTR(-ENAMETOOLONG);
	}

113 114 115 116 117 118 119
	/* control object, create inode on the fly */
	if (coda_isroot(dir) && coda_iscontrol(name, length)) {
		error = coda_cnode_makectl(&inode, dir->i_sb);
		type = CODA_NOCACHE;
		goto exit;
	}

L
Linus Torvalds 已提交
120
	lock_kernel();
121 122 123 124 125 126 127 128 129

	error = venus_lookup(dir->i_sb, coda_i2f(dir), name, length,
			     &type, &resfid);
	if (!error)
		error = coda_cnode_make(&inode, &resfid, dir->i_sb);

	unlock_kernel();

	if (error && error != -ENOENT)
L
Linus Torvalds 已提交
130 131 132 133
		return ERR_PTR(error);

exit:
	entry->d_op = &coda_dentry_operations;
134 135 136 137 138

	if (inode && (type & CODA_NOCACHE))
		coda_flag_inode(inode, C_VATTR | C_PURGE);

	return d_splice_alias(inode, entry);
L
Linus Torvalds 已提交
139 140 141
}


142
int coda_permission(struct inode *inode, int mask)
L
Linus Torvalds 已提交
143 144
{
        int error = 0;
145 146

	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
L
Linus Torvalds 已提交
147 148 149 150
 
	if (!mask)
		return 0; 

151 152 153
	if ((mask & MAY_EXEC) && !execute_ok(inode))
		return -EACCES;

L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163 164 165
	lock_kernel();

	if (coda_cache_check(inode, mask))
		goto out; 

        error = venus_access(inode->i_sb, coda_i2f(inode), mask);
    
	if (!error)
		coda_cache_enter(inode, mask);

 out:
	unlock_kernel();
166
	return error;
L
Linus Torvalds 已提交
167 168 169
}


170
static inline void coda_dir_update_mtime(struct inode *dir)
L
Linus Torvalds 已提交
171 172 173 174 175 176 177
{
#ifdef REQUERY_VENUS_FOR_MTIME
	/* invalidate the directory cnode's attributes so we refetch the
	 * attributes from venus next time the inode is referenced */
	coda_flag_inode(dir, C_VATTR);
#else
	/* optimistically we can also act as if our nose bleeds. The
178 179
	 * granularity of the mtime is coarse anyways so we might actually be
	 * right most of the time. Note: we only do this for directories. */
L
Linus Torvalds 已提交
180 181
	dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
#endif
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
}

/* we have to wrap inc_nlink/drop_nlink because sometimes userspace uses a
 * trick to fool GNU find's optimizations. If we can't be sure of the link
 * (because of volume mount points) we set i_nlink to 1 which forces find
 * to consider every child as a possible directory. We should also never
 * see an increment or decrement for deleted directories where i_nlink == 0 */
static inline void coda_dir_inc_nlink(struct inode *dir)
{
	if (dir->i_nlink >= 2)
		inc_nlink(dir);
}

static inline void coda_dir_drop_nlink(struct inode *dir)
{
	if (dir->i_nlink > 2)
		drop_nlink(dir);
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

/* creation routines: create, mknod, mkdir, link, symlink */
static int coda_create(struct inode *dir, struct dentry *de, int mode, struct nameidata *nd)
{
        int error=0;
	const char *name=de->d_name.name;
	int length=de->d_name.len;
	struct inode *inode;
	struct CodaFid newfid;
	struct coda_vattr attrs;

	lock_kernel();

	if (coda_isroot(dir) && coda_iscontrol(name, length)) {
		unlock_kernel();
		return -EPERM;
	}

	error = venus_create(dir->i_sb, coda_i2f(dir), name, length, 
				0, mode, &newfid, &attrs);

        if ( error ) {
		unlock_kernel();
		d_drop(de);
		return error;
	}

	inode = coda_iget(dir->i_sb, &newfid, &attrs);
	if ( IS_ERR(inode) ) {
		unlock_kernel();
		d_drop(de);
		return PTR_ERR(inode);
	}

	/* invalidate the directory cnode's attributes */
235
	coda_dir_update_mtime(dir);
L
Linus Torvalds 已提交
236 237
	unlock_kernel();
	d_instantiate(de, inode);
238
	return 0;
L
Linus Torvalds 已提交
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
}

static int coda_mkdir(struct inode *dir, struct dentry *de, int mode)
{
	struct inode *inode;
	struct coda_vattr attrs;
	const char *name = de->d_name.name;
	int len = de->d_name.len;
	int error;
	struct CodaFid newfid;

	lock_kernel();

	if (coda_isroot(dir) && coda_iscontrol(name, len)) {
		unlock_kernel();
		return -EPERM;
	}

	attrs.va_mode = mode;
	error = venus_mkdir(dir->i_sb, coda_i2f(dir), 
			       name, len, &newfid, &attrs);
        
        if ( error ) {
		unlock_kernel();
		d_drop(de);
		return error;
        }
         
	inode = coda_iget(dir->i_sb, &newfid, &attrs);
	if ( IS_ERR(inode) ) {
		unlock_kernel();
		d_drop(de);
		return PTR_ERR(inode);
	}
273

L
Linus Torvalds 已提交
274
	/* invalidate the directory cnode's attributes */
275 276
	coda_dir_inc_nlink(dir);
	coda_dir_update_mtime(dir);
L
Linus Torvalds 已提交
277 278
	unlock_kernel();
	d_instantiate(de, inode);
279
	return 0;
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
}

/* try to make de an entry in dir_inodde linked to source_de */ 
static int coda_link(struct dentry *source_de, struct inode *dir_inode, 
	  struct dentry *de)
{
	struct inode *inode = source_de->d_inode;
        const char * name = de->d_name.name;
	int len = de->d_name.len;
	int error;

	lock_kernel();

	if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
		unlock_kernel();
		return -EPERM;
	}

	error = venus_link(dir_inode->i_sb, coda_i2f(inode),
			   coda_i2f(dir_inode), (const char *)name, len);

301
	if (error) {
L
Linus Torvalds 已提交
302 303 304 305
		d_drop(de);
		goto out;
	}

306
	coda_dir_update_mtime(dir_inode);
L
Linus Torvalds 已提交
307 308
	atomic_inc(&inode->i_count);
	d_instantiate(de, inode);
309
	inc_nlink(inode);
310

L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321 322
out:
	unlock_kernel();
	return(error);
}


static int coda_symlink(struct inode *dir_inode, struct dentry *de,
			const char *symname)
{
        const char *name = de->d_name.name;
	int len = de->d_name.len;
	int symlen;
323 324
	int error = 0;

L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	lock_kernel();

	if (coda_isroot(dir_inode) && coda_iscontrol(name, len)) {
		unlock_kernel();
		return -EPERM;
	}

	symlen = strlen(symname);
	if ( symlen > CODA_MAXPATHLEN ) {
		unlock_kernel();
		return -ENAMETOOLONG;
	}

	/*
	 * This entry is now negative. Since we do not create
340
	 * an inode for the entry we have to drop it.
L
Linus Torvalds 已提交
341 342
	 */
	d_drop(de);
343
	error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
L
Linus Torvalds 已提交
344 345 346 347
			      symname, symlen);

	/* mtime is no good anymore */
	if ( !error )
348
		coda_dir_update_mtime(dir_inode);
L
Linus Torvalds 已提交
349 350

	unlock_kernel();
351
	return error;
L
Linus Torvalds 已提交
352 353 354
}

/* destruction routines: unlink, rmdir */
355
static int coda_unlink(struct inode *dir, struct dentry *de)
L
Linus Torvalds 已提交
356 357 358 359 360 361 362
{
        int error;
	const char *name = de->d_name.name;
	int len = de->d_name.len;

	lock_kernel();

363 364
	error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
	if ( error ) {
L
Linus Torvalds 已提交
365
		unlock_kernel();
366 367
		return error;
	}
L
Linus Torvalds 已提交
368

369
	coda_dir_update_mtime(dir);
370
	drop_nlink(de->d_inode);
L
Linus Torvalds 已提交
371
	unlock_kernel();
372
	return 0;
L
Linus Torvalds 已提交
373 374
}

375
static int coda_rmdir(struct inode *dir, struct dentry *de)
L
Linus Torvalds 已提交
376 377 378
{
	const char *name = de->d_name.name;
	int len = de->d_name.len;
379
	int error;
L
Linus Torvalds 已提交
380 381 382 383

	lock_kernel();

	error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
384 385 386 387
	if (!error) {
		/* VFS may delete the child */
		if (de->d_inode)
		    de->d_inode->i_nlink = 0;
L
Linus Torvalds 已提交
388

389 390 391
		/* fix the link count of the parent */
		coda_dir_drop_nlink(dir);
		coda_dir_update_mtime(dir);
392
	}
L
Linus Torvalds 已提交
393
	unlock_kernel();
394
	return error;
L
Linus Torvalds 已提交
395 396 397
}

/* rename */
398
static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
L
Linus Torvalds 已提交
399 400
		       struct inode *new_dir, struct dentry *new_dentry)
{
401 402
	const char *old_name = old_dentry->d_name.name;
	const char *new_name = new_dentry->d_name.name;
L
Linus Torvalds 已提交
403 404
	int old_length = old_dentry->d_name.len;
	int new_length = new_dentry->d_name.len;
405
	int error;
L
Linus Torvalds 已提交
406 407 408

	lock_kernel();

409 410
	error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
			     coda_i2f(new_dir), old_length, new_length,
L
Linus Torvalds 已提交
411 412
			     (const char *) old_name, (const char *)new_name);

413
	if ( !error ) {
L
Linus Torvalds 已提交
414
		if ( new_dentry->d_inode ) {
415 416 417 418 419 420
			if ( S_ISDIR(new_dentry->d_inode->i_mode) ) {
				coda_dir_drop_nlink(old_dir);
				coda_dir_inc_nlink(new_dir);
			}
			coda_dir_update_mtime(old_dir);
			coda_dir_update_mtime(new_dir);
L
Linus Torvalds 已提交
421 422 423 424
			coda_flag_inode(new_dentry->d_inode, C_VATTR);
		} else {
			coda_flag_inode(old_dir, C_VATTR);
			coda_flag_inode(new_dir, C_VATTR);
425
		}
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433
	}
	unlock_kernel();

	return error;
}


/* file operations for directories */
434
static int coda_readdir(struct file *coda_file, void *buf, filldir_t filldir)
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443
{
	struct coda_file_info *cfi;
	struct file *host_file;
	int ret;

	cfi = CODA_FTOC(coda_file);
	BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
	host_file = cfi->cfi_container;

444 445 446 447 448 449 450 451 452 453 454 455 456
	if (!host_file->f_op)
		return -ENOTDIR;

	if (host_file->f_op->readdir)
	{
		/* potemkin case: we were handed a directory inode.
		 * We can't use vfs_readdir because we have to keep the file
		 * position in sync between the coda_file and the host_file.
		 * and as such we need grab the inode mutex. */
		struct inode *host_inode = host_file->f_path.dentry->d_inode;

		mutex_lock(&host_inode->i_mutex);
		host_file->f_pos = coda_file->f_pos;
L
Linus Torvalds 已提交
457 458 459

		ret = -ENOENT;
		if (!IS_DEADDIR(host_inode)) {
460
			ret = host_file->f_op->readdir(host_file, buf, filldir);
L
Linus Torvalds 已提交
461 462
			file_accessed(host_file);
		}
463 464 465

		coda_file->f_pos = host_file->f_pos;
		mutex_unlock(&host_inode->i_mutex);
L
Linus Torvalds 已提交
466
	}
467 468
	else /* Venus: we must read Venus dirents from a file */
		ret = coda_venus_readdir(coda_file, buf, filldir);
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

	return ret;
}

static inline unsigned int CDT2DT(unsigned char cdt)
{
	unsigned int dt;

	switch(cdt) {
	case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
	case CDT_FIFO:	  dt = DT_FIFO;    break;
	case CDT_CHR:	  dt = DT_CHR;     break;
	case CDT_DIR:	  dt = DT_DIR;     break;
	case CDT_BLK:	  dt = DT_BLK;     break;
	case CDT_REG:	  dt = DT_REG;     break;
	case CDT_LNK:	  dt = DT_LNK;     break;
	case CDT_SOCK:	  dt = DT_SOCK;    break;
	case CDT_WHT:	  dt = DT_WHT;     break;
	default:	  dt = DT_UNKNOWN; break;
	}
	return dt;
}

/* support routines */
493 494
static int coda_venus_readdir(struct file *coda_file, void *buf,
			      filldir_t filldir)
L
Linus Torvalds 已提交
495 496
{
	int result = 0; /* # of entries returned */
497 498 499 500
	struct coda_file_info *cfi;
	struct coda_inode_info *cii;
	struct file *host_file;
	struct dentry *de;
L
Linus Torvalds 已提交
501 502 503 504 505 506
	struct venus_dirent *vdir;
	unsigned long vdir_size =
	    (unsigned long)(&((struct venus_dirent *)0)->d_name);
	unsigned int type;
	struct qstr name;
	ino_t ino;
507 508 509 510 511 512 513 514
	int ret;

	cfi = CODA_FTOC(coda_file);
	BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
	host_file = cfi->cfi_container;

	de = coda_file->f_path.dentry;
	cii = ITOC(de->d_inode);
L
Linus Torvalds 已提交
515

516
	vdir = kmalloc(sizeof(*vdir), GFP_KERNEL);
L
Linus Torvalds 已提交
517 518
	if (!vdir) return -ENOMEM;

A
Al Viro 已提交
519
	if (coda_file->f_pos == 0) {
520
		ret = filldir(buf, ".", 1, 0, de->d_inode->i_ino, DT_DIR);
A
Al Viro 已提交
521 522
		if (ret < 0)
			goto out;
L
Linus Torvalds 已提交
523
		result++;
524
		coda_file->f_pos++;
A
Al Viro 已提交
525 526
	}
	if (coda_file->f_pos == 1) {
527
		ret = filldir(buf, "..", 2, 1, de->d_parent->d_inode->i_ino, DT_DIR);
A
Al Viro 已提交
528 529
		if (ret < 0)
			goto out;
L
Linus Torvalds 已提交
530
		result++;
531
		coda_file->f_pos++;
A
Al Viro 已提交
532
	}
L
Linus Torvalds 已提交
533 534
	while (1) {
		/* read entries from the directory file */
535
		ret = kernel_read(host_file, coda_file->f_pos - 2, (char *)vdir,
L
Linus Torvalds 已提交
536 537
				  sizeof(*vdir));
		if (ret < 0) {
538 539
			printk(KERN_ERR "coda readdir: read dir %s failed %d\n",
			       coda_f2s(&cii->c_fid), ret);
L
Linus Torvalds 已提交
540 541 542 543 544 545
			break;
		}
		if (ret == 0) break; /* end of directory file reached */

		/* catch truncated reads */
		if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
546 547
			printk(KERN_ERR "coda readdir: short read on %s\n",
			       coda_f2s(&cii->c_fid));
L
Linus Torvalds 已提交
548 549 550 551 552
			ret = -EBADF;
			break;
		}
		/* validate whether the directory file actually makes sense */
		if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
553 554
			printk(KERN_ERR "coda readdir: invalid dir %s\n",
			       coda_f2s(&cii->c_fid));
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
			ret = -EBADF;
			break;
		}

		name.len = vdir->d_namlen;
		name.name = vdir->d_name;

		/* Make sure we skip '.' and '..', we already got those */
		if (name.name[0] == '.' && (name.len == 1 ||
		    (vdir->d_name[1] == '.' && name.len == 2)))
			vdir->d_fileno = name.len = 0;

		/* skip null entries */
		if (vdir->d_fileno && name.len) {
			/* try to look up this entry in the dcache, that way
			 * userspace doesn't have to worry about breaking
			 * getcwd by having mismatched inode numbers for
			 * internal volume mountpoints. */
573
			ino = find_inode_number(de, &name);
L
Linus Torvalds 已提交
574 575 576
			if (!ino) ino = vdir->d_fileno;

			type = CDT2DT(vdir->d_type);
577 578
			ret = filldir(buf, name.name, name.len,
				      coda_file->f_pos, ino, type);
L
Linus Torvalds 已提交
579 580 581 582 583 584
			/* failure means no space for filling in this round */
			if (ret < 0) break;
			result++;
		}
		/* we'll always have progress because d_reclen is unsigned and
		 * we've already established it is non-zero. */
585 586
		coda_file->f_pos += vdir->d_reclen;
	}
A
Al Viro 已提交
587
out:
L
Linus Torvalds 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	kfree(vdir);
	return result ? result : ret;
}

/* called when a cache lookup succeeds */
static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd)
{
	struct inode *inode = de->d_inode;
	struct coda_inode_info *cii;

	if (!inode)
		return 1;
	lock_kernel();
	if (coda_isroot(inode))
		goto out;
	if (is_bad_inode(inode))
		goto bad;

	cii = ITOC(de->d_inode);
	if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
		goto out;

	shrink_dcache_parent(de);

	/* propagate for a flush */
	if (cii->c_flags & C_FLUSH) 
		coda_flag_inode_children(inode, C_FLUSH);

	if (atomic_read(&de->d_count) > 1)
		/* pretend it's valid, but don't change the flags */
		goto out;

	/* clear the flags. */
621
	spin_lock(&cii->c_lock);
L
Linus Torvalds 已提交
622
	cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
623
	spin_unlock(&cii->c_lock);
L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696

bad:
	unlock_kernel();
	return 0;
out:
	unlock_kernel();
	return 1;
}

/*
 * This is the callback from dput() when d_count is going to 0.
 * We use this to unhash dentries with bad inodes.
 */
static int coda_dentry_delete(struct dentry * dentry)
{
	int flags;

	if (!dentry->d_inode) 
		return 0;

	flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
	if (is_bad_inode(dentry->d_inode) || flags) {
		return 1;
	}
	return 0;
}



/*
 * This is called when we want to check if the inode has
 * changed on the server.  Coda makes this easy since the
 * cache manager Venus issues a downcall to the kernel when this 
 * happens 
 */
int coda_revalidate_inode(struct dentry *dentry)
{
	struct coda_vattr attr;
	int error = 0;
	int old_mode;
	ino_t old_ino;
	struct inode *inode = dentry->d_inode;
	struct coda_inode_info *cii = ITOC(inode);

	lock_kernel();
	if ( !cii->c_flags )
		goto ok;

	if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
		error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
		if ( error )
			goto return_bad;

		/* this inode may be lost if:
		   - it's ino changed 
		   - type changes must be permitted for repair and
		   missing mount points.
		*/
		old_mode = inode->i_mode;
		old_ino = inode->i_ino;
		coda_vattr_to_iattr(inode, &attr);

		if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
			printk("Coda: inode %ld, fid %s changed type!\n",
			       inode->i_ino, coda_f2s(&(cii->c_fid)));
		}

		/* the following can happen when a local fid is replaced 
		   with a global one, here we lose and declare the inode bad */
		if (inode->i_ino != old_ino)
			goto return_bad;
		
		coda_flag_inode_children(inode, C_FLUSH);
697 698

		spin_lock(&cii->c_lock);
L
Linus Torvalds 已提交
699
		cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
700
		spin_unlock(&cii->c_lock);
L
Linus Torvalds 已提交
701 702 703 704 705 706 707 708 709 710
	}

ok:
	unlock_kernel();
	return 0;

return_bad:
	unlock_kernel();
	return -EIO;
}