dir.c 13.9 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
#include <linux/file.h>
#include <linux/stat.h>
#include <linux/errno.h>
#include <linux/string.h>
20
#include <linux/spinlock.h>
21
#include <linux/namei.h>
F
Fabian Frederick 已提交
22
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
23 24 25

#include <linux/coda.h>
#include <linux/coda_psdev.h>
26 27
#include "coda_linux.h"
#include "coda_cache.h"
L
Linus Torvalds 已提交
28

29 30
#include "coda_int.h"

L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39
/* same as fs/bad_inode.c */
static int coda_return_EIO(void)
{
	return -EIO;
}
#define CODA_EIO_ERROR ((void *) (coda_return_EIO))

/* inode operations for directories */
/* access routines: lookup, readlink, permission */
A
Al Viro 已提交
40
static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, unsigned int flags)
L
Linus Torvalds 已提交
41
{
42
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
43 44
	const char *name = entry->d_name.name;
	size_t length = entry->d_name.len;
45 46
	struct inode *inode;
	int type = 0;
47 48

	if (length > CODA_MAXNAMLEN) {
49
		pr_err("name too long: lookup, %s (%*s)\n",
L
Linus Torvalds 已提交
50 51 52 53
		       coda_i2s(dir), (int)length, name);
		return ERR_PTR(-ENAMETOOLONG);
	}

54
	/* control object, create inode on the fly */
A
Al Viro 已提交
55
	if (is_root_inode(dir) && coda_iscontrol(name, length)) {
56
		inode = coda_cnode_makectl(sb);
57
		type = CODA_NOCACHE;
58 59 60 61 62
	} else {
		struct CodaFid fid = { { 0, } };
		int error = venus_lookup(sb, coda_i2f(dir), name, length,
				     &type, &fid);
		inode = !error ? coda_cnode_make(&fid, sb) : ERR_PTR(error);
63 64
	}

65
	if (!IS_ERR(inode) && (type & CODA_NOCACHE))
66 67
		coda_flag_inode(inode, C_VATTR | C_PURGE);

68 69 70
	if (inode == ERR_PTR(-ENOENT))
		inode = NULL;

71
	return d_splice_alias(inode, entry);
L
Linus Torvalds 已提交
72 73 74
}


75
int coda_permission(struct inode *inode, int mask)
L
Linus Torvalds 已提交
76
{
77
	int error;
78

79
	if (mask & MAY_NOT_BLOCK)
80 81
		return -ECHILD;

82
	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
L
Linus Torvalds 已提交
83 84
 
	if (!mask)
85
		return 0;
L
Linus Torvalds 已提交
86

87 88 89
	if ((mask & MAY_EXEC) && !execute_ok(inode))
		return -EACCES;

L
Linus Torvalds 已提交
90
	if (coda_cache_check(inode, mask))
91
		return 0;
L
Linus Torvalds 已提交
92

93
	error = venus_access(inode->i_sb, coda_i2f(inode), mask);
L
Linus Torvalds 已提交
94 95 96 97
    
	if (!error)
		coda_cache_enter(inode, mask);

98
	return error;
L
Linus Torvalds 已提交
99 100 101
}


102
static inline void coda_dir_update_mtime(struct inode *dir)
L
Linus Torvalds 已提交
103 104 105 106 107 108 109
{
#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
110 111
	 * 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 已提交
112 113
	dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
#endif
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

/* 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 已提交
131 132 133
}

/* creation routines: create, mknod, mkdir, link, symlink */
A
Al Viro 已提交
134
static int coda_create(struct inode *dir, struct dentry *de, umode_t mode, bool excl)
L
Linus Torvalds 已提交
135
{
136
	int error;
L
Linus Torvalds 已提交
137 138 139 140 141 142
	const char *name=de->d_name.name;
	int length=de->d_name.len;
	struct inode *inode;
	struct CodaFid newfid;
	struct coda_vattr attrs;

A
Al Viro 已提交
143
	if (is_root_inode(dir) && coda_iscontrol(name, length))
L
Linus Torvalds 已提交
144 145 146 147
		return -EPERM;

	error = venus_create(dir->i_sb, coda_i2f(dir), name, length, 
				0, mode, &newfid, &attrs);
148 149
	if (error)
		goto err_out;
L
Linus Torvalds 已提交
150 151

	inode = coda_iget(dir->i_sb, &newfid, &attrs);
152 153 154
	if (IS_ERR(inode)) {
		error = PTR_ERR(inode);
		goto err_out;
L
Linus Torvalds 已提交
155 156 157
	}

	/* invalidate the directory cnode's attributes */
158
	coda_dir_update_mtime(dir);
L
Linus Torvalds 已提交
159
	d_instantiate(de, inode);
160
	return 0;
161 162 163
err_out:
	d_drop(de);
	return error;
L
Linus Torvalds 已提交
164 165
}

166
static int coda_mkdir(struct inode *dir, struct dentry *de, umode_t mode)
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174
{
	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;

A
Al Viro 已提交
175
	if (is_root_inode(dir) && coda_iscontrol(name, len))
L
Linus Torvalds 已提交
176 177 178 179 180
		return -EPERM;

	attrs.va_mode = mode;
	error = venus_mkdir(dir->i_sb, coda_i2f(dir), 
			       name, len, &newfid, &attrs);
181 182
	if (error)
		goto err_out;
L
Linus Torvalds 已提交
183 184
         
	inode = coda_iget(dir->i_sb, &newfid, &attrs);
185 186 187
	if (IS_ERR(inode)) {
		error = PTR_ERR(inode);
		goto err_out;
L
Linus Torvalds 已提交
188
	}
189

L
Linus Torvalds 已提交
190
	/* invalidate the directory cnode's attributes */
191 192
	coda_dir_inc_nlink(dir);
	coda_dir_update_mtime(dir);
L
Linus Torvalds 已提交
193
	d_instantiate(de, inode);
194
	return 0;
195 196 197
err_out:
	d_drop(de);
	return error;
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208
}

/* 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;

A
Al Viro 已提交
209
	if (is_root_inode(dir_inode) && coda_iscontrol(name, len))
L
Linus Torvalds 已提交
210 211 212 213
		return -EPERM;

	error = venus_link(dir_inode->i_sb, coda_i2f(inode),
			   coda_i2f(dir_inode), (const char *)name, len);
214
	if (error) {
L
Linus Torvalds 已提交
215
		d_drop(de);
216
		return error;
L
Linus Torvalds 已提交
217 218
	}

219
	coda_dir_update_mtime(dir_inode);
A
Al Viro 已提交
220
	ihold(inode);
L
Linus Torvalds 已提交
221
	d_instantiate(de, inode);
222
	inc_nlink(inode);
223
	return 0;
L
Linus Torvalds 已提交
224 225 226 227 228 229
}


static int coda_symlink(struct inode *dir_inode, struct dentry *de,
			const char *symname)
{
230
	const char *name = de->d_name.name;
L
Linus Torvalds 已提交
231 232
	int len = de->d_name.len;
	int symlen;
233
	int error;
L
Linus Torvalds 已提交
234

A
Al Viro 已提交
235
	if (is_root_inode(dir_inode) && coda_iscontrol(name, len))
L
Linus Torvalds 已提交
236 237 238
		return -EPERM;

	symlen = strlen(symname);
239
	if (symlen > CODA_MAXPATHLEN)
L
Linus Torvalds 已提交
240 241 242 243
		return -ENAMETOOLONG;

	/*
	 * This entry is now negative. Since we do not create
244
	 * an inode for the entry we have to drop it.
L
Linus Torvalds 已提交
245 246
	 */
	d_drop(de);
247
	error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
L
Linus Torvalds 已提交
248 249 250
			      symname, symlen);

	/* mtime is no good anymore */
251
	if (!error)
252
		coda_dir_update_mtime(dir_inode);
L
Linus Torvalds 已提交
253

254
	return error;
L
Linus Torvalds 已提交
255 256 257
}

/* destruction routines: unlink, rmdir */
258
static int coda_unlink(struct inode *dir, struct dentry *de)
L
Linus Torvalds 已提交
259 260 261 262 263
{
        int error;
	const char *name = de->d_name.name;
	int len = de->d_name.len;

264
	error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
265
	if (error)
266
		return error;
L
Linus Torvalds 已提交
267

268
	coda_dir_update_mtime(dir);
269
	drop_nlink(de->d_inode);
270
	return 0;
L
Linus Torvalds 已提交
271 272
}

273
static int coda_rmdir(struct inode *dir, struct dentry *de)
L
Linus Torvalds 已提交
274 275 276
{
	const char *name = de->d_name.name;
	int len = de->d_name.len;
277
	int error;
L
Linus Torvalds 已提交
278 279

	error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
280 281 282
	if (!error) {
		/* VFS may delete the child */
		if (de->d_inode)
283
			clear_nlink(de->d_inode);
L
Linus Torvalds 已提交
284

285 286 287
		/* fix the link count of the parent */
		coda_dir_drop_nlink(dir);
		coda_dir_update_mtime(dir);
288
	}
289
	return error;
L
Linus Torvalds 已提交
290 291 292
}

/* rename */
293
static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
L
Linus Torvalds 已提交
294 295
		       struct inode *new_dir, struct dentry *new_dentry)
{
296 297
	const char *old_name = old_dentry->d_name.name;
	const char *new_name = new_dentry->d_name.name;
L
Linus Torvalds 已提交
298 299
	int old_length = old_dentry->d_name.len;
	int new_length = new_dentry->d_name.len;
300
	int error;
L
Linus Torvalds 已提交
301

302 303
	error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
			     coda_i2f(new_dir), old_length, new_length,
L
Linus Torvalds 已提交
304
			     (const char *) old_name, (const char *)new_name);
305 306
	if (!error) {
		if (new_dentry->d_inode) {
307
			if (d_is_dir(new_dentry)) {
308 309 310 311 312
				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 已提交
313 314 315 316
			coda_flag_inode(new_dentry->d_inode, C_VATTR);
		} else {
			coda_flag_inode(old_dir, C_VATTR);
			coda_flag_inode(new_dir, C_VATTR);
317
		}
L
Linus Torvalds 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	}
	return error;
}

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 */
A
Al Viro 已提交
342
static int coda_venus_readdir(struct file *coda_file, struct dir_context *ctx)
L
Linus Torvalds 已提交
343
{
344 345 346
	struct coda_file_info *cfi;
	struct coda_inode_info *cii;
	struct file *host_file;
L
Linus Torvalds 已提交
347
	struct venus_dirent *vdir;
A
Al Viro 已提交
348
	unsigned long vdir_size = offsetof(struct venus_dirent, d_name);
L
Linus Torvalds 已提交
349 350 351
	unsigned int type;
	struct qstr name;
	ino_t ino;
352 353 354 355 356 357
	int ret;

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

A
Al Viro 已提交
358
	cii = ITOC(file_inode(coda_file));
L
Linus Torvalds 已提交
359

360
	vdir = kmalloc(sizeof(*vdir), GFP_KERNEL);
L
Linus Torvalds 已提交
361 362
	if (!vdir) return -ENOMEM;

A
Al Viro 已提交
363 364 365
	if (!dir_emit_dots(coda_file, ctx))
		goto out;

L
Linus Torvalds 已提交
366 367
	while (1) {
		/* read entries from the directory file */
A
Al Viro 已提交
368
		ret = kernel_read(host_file, ctx->pos - 2, (char *)vdir,
L
Linus Torvalds 已提交
369 370
				  sizeof(*vdir));
		if (ret < 0) {
F
Fabian Frederick 已提交
371 372
			pr_err("%s: read dir %s failed %d\n",
			       __func__, coda_f2s(&cii->c_fid), ret);
L
Linus Torvalds 已提交
373 374 375 376 377 378
			break;
		}
		if (ret == 0) break; /* end of directory file reached */

		/* catch truncated reads */
		if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
F
Fabian Frederick 已提交
379 380
			pr_err("%s: short read on %s\n",
			       __func__, coda_f2s(&cii->c_fid));
L
Linus Torvalds 已提交
381 382 383 384 385
			ret = -EBADF;
			break;
		}
		/* validate whether the directory file actually makes sense */
		if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
F
Fabian Frederick 已提交
386 387
			pr_err("%s: invalid dir %s\n",
			       __func__, coda_f2s(&cii->c_fid));
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396
			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 ||
A
Al Viro 已提交
397
		    (name.name[1] == '.' && name.len == 2)))
L
Linus Torvalds 已提交
398 399 400 401
			vdir->d_fileno = name.len = 0;

		/* skip null entries */
		if (vdir->d_fileno && name.len) {
402
			ino = vdir->d_fileno;
L
Linus Torvalds 已提交
403
			type = CDT2DT(vdir->d_type);
A
Al Viro 已提交
404 405
			if (!dir_emit(ctx, name.name, name.len, ino, type))
				break;
L
Linus Torvalds 已提交
406 407 408
		}
		/* we'll always have progress because d_reclen is unsigned and
		 * we've already established it is non-zero. */
A
Al Viro 已提交
409
		ctx->pos += vdir->d_reclen;
410
	}
A
Al Viro 已提交
411
out:
L
Linus Torvalds 已提交
412
	kfree(vdir);
A
Al Viro 已提交
413
	return 0;
L
Linus Torvalds 已提交
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
/* file operations for directories */
static int coda_readdir(struct file *coda_file, struct dir_context *ctx)
{
	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;

	if (host_file->f_op->iterate) {
		struct inode *host_inode = file_inode(host_file);

		mutex_lock(&host_inode->i_mutex);
		ret = -ENOENT;
		if (!IS_DEADDIR(host_inode)) {
			ret = host_file->f_op->iterate(host_file, ctx);
			file_accessed(host_file);
		}
		mutex_unlock(&host_inode->i_mutex);
		return ret;
	}
	/* Venus: we must read Venus dirents from a file */
	return coda_venus_readdir(coda_file, ctx);
}

L
Linus Torvalds 已提交
443
/* called when a cache lookup succeeds */
444
static int coda_dentry_revalidate(struct dentry *de, unsigned int flags)
L
Linus Torvalds 已提交
445
{
446
	struct inode *inode;
L
Linus Torvalds 已提交
447 448
	struct coda_inode_info *cii;

449
	if (flags & LOOKUP_RCU)
450 451 452
		return -ECHILD;

	inode = de->d_inode;
A
Al Viro 已提交
453
	if (!inode || is_root_inode(inode))
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467
		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);

A
Al Viro 已提交
468
	if (d_count(de) > 1)
L
Linus Torvalds 已提交
469 470 471 472
		/* pretend it's valid, but don't change the flags */
		goto out;

	/* clear the flags. */
473
	spin_lock(&cii->c_lock);
L
Linus Torvalds 已提交
474
	cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
475
	spin_unlock(&cii->c_lock);
L
Linus Torvalds 已提交
476 477 478 479 480 481 482 483 484 485
bad:
	return 0;
out:
	return 1;
}

/*
 * This is the callback from dput() when d_count is going to 0.
 * We use this to unhash dentries with bad inodes.
 */
N
Nick Piggin 已提交
486
static int coda_dentry_delete(const struct dentry * dentry)
L
Linus Torvalds 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
{
	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 
 */
508
int coda_revalidate_inode(struct inode *inode)
L
Linus Torvalds 已提交
509 510
{
	struct coda_vattr attr;
511
	int error;
L
Linus Torvalds 已提交
512 513 514 515
	int old_mode;
	ino_t old_ino;
	struct coda_inode_info *cii = ITOC(inode);

516 517
	if (!cii->c_flags)
		return 0;
L
Linus Torvalds 已提交
518 519 520

	if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
		error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
521 522
		if (error)
			return -EIO;
L
Linus Torvalds 已提交
523 524 525 526 527 528 529 530 531 532 533

		/* 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)) {
534
			pr_warn("inode %ld, fid %s changed type!\n",
535
				inode->i_ino, coda_f2s(&(cii->c_fid)));
L
Linus Torvalds 已提交
536 537 538 539 540
		}

		/* 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)
541
			return -EIO;
L
Linus Torvalds 已提交
542 543
		
		coda_flag_inode_children(inode, C_FLUSH);
544 545

		spin_lock(&cii->c_lock);
L
Linus Torvalds 已提交
546
		cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
547
		spin_unlock(&cii->c_lock);
L
Linus Torvalds 已提交
548 549 550
	}
	return 0;
}
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

const struct dentry_operations coda_dentry_operations = {
	.d_revalidate	= coda_dentry_revalidate,
	.d_delete	= coda_dentry_delete,
};

const struct inode_operations coda_dir_inode_operations = {
	.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,
};

const struct file_operations coda_dir_operations = {
	.llseek		= generic_file_llseek,
	.read		= generic_read_dir,
	.iterate	= coda_readdir,
	.open		= coda_open,
	.release	= coda_release,
	.fsync		= coda_fsync,
};