dir.c 14.2 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
}

/* 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)
{
204
	struct inode *inode = d_inode(source_de);
L
Linus Torvalds 已提交
205 206 207 208
        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(d_inode(de));
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
	if (!error) {
		/* VFS may delete the child */
282 283
		if (d_really_is_positive(de))
			clear_nlink(d_inode(de));
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,
294 295
		       struct inode *new_dir, struct dentry *new_dentry,
		       unsigned int flags)
L
Linus Torvalds 已提交
296
{
297 298
	const char *old_name = old_dentry->d_name.name;
	const char *new_name = new_dentry->d_name.name;
L
Linus Torvalds 已提交
299 300
	int old_length = old_dentry->d_name.len;
	int new_length = new_dentry->d_name.len;
301
	int error;
L
Linus Torvalds 已提交
302

303 304 305
	if (flags)
		return -EINVAL;

306 307
	error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
			     coda_i2f(new_dir), old_length, new_length,
L
Linus Torvalds 已提交
308
			     (const char *) old_name, (const char *)new_name);
309
	if (!error) {
310
		if (d_really_is_positive(new_dentry)) {
311
			if (d_is_dir(new_dentry)) {
312 313 314 315 316
				coda_dir_drop_nlink(old_dir);
				coda_dir_inc_nlink(new_dir);
			}
			coda_dir_update_mtime(old_dir);
			coda_dir_update_mtime(new_dir);
317
			coda_flag_inode(d_inode(new_dentry), C_VATTR);
L
Linus Torvalds 已提交
318 319 320
		} else {
			coda_flag_inode(old_dir, C_VATTR);
			coda_flag_inode(new_dir, C_VATTR);
321
		}
L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	}
	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 已提交
346
static int coda_venus_readdir(struct file *coda_file, struct dir_context *ctx)
L
Linus Torvalds 已提交
347
{
348 349 350
	struct coda_file_info *cfi;
	struct coda_inode_info *cii;
	struct file *host_file;
L
Linus Torvalds 已提交
351
	struct venus_dirent *vdir;
A
Al Viro 已提交
352
	unsigned long vdir_size = offsetof(struct venus_dirent, d_name);
L
Linus Torvalds 已提交
353 354 355
	unsigned int type;
	struct qstr name;
	ino_t ino;
356 357 358 359 360 361
	int ret;

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

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

364
	vdir = kmalloc(sizeof(*vdir), GFP_KERNEL);
L
Linus Torvalds 已提交
365 366
	if (!vdir) return -ENOMEM;

A
Al Viro 已提交
367 368 369
	if (!dir_emit_dots(coda_file, ctx))
		goto out;

L
Linus Torvalds 已提交
370 371
	while (1) {
		/* read entries from the directory file */
A
Al Viro 已提交
372
		ret = kernel_read(host_file, ctx->pos - 2, (char *)vdir,
L
Linus Torvalds 已提交
373 374
				  sizeof(*vdir));
		if (ret < 0) {
F
Fabian Frederick 已提交
375 376
			pr_err("%s: read dir %s failed %d\n",
			       __func__, coda_f2s(&cii->c_fid), ret);
L
Linus Torvalds 已提交
377 378 379 380 381 382
			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 已提交
383 384
			pr_err("%s: short read on %s\n",
			       __func__, coda_f2s(&cii->c_fid));
L
Linus Torvalds 已提交
385 386 387 388 389
			ret = -EBADF;
			break;
		}
		/* validate whether the directory file actually makes sense */
		if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
F
Fabian Frederick 已提交
390 391
			pr_err("%s: invalid dir %s\n",
			       __func__, coda_f2s(&cii->c_fid));
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399 400
			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 已提交
401
		    (name.name[1] == '.' && name.len == 2)))
L
Linus Torvalds 已提交
402 403 404 405
			vdir->d_fileno = name.len = 0;

		/* skip null entries */
		if (vdir->d_fileno && name.len) {
406
			ino = vdir->d_fileno;
L
Linus Torvalds 已提交
407
			type = CDT2DT(vdir->d_type);
A
Al Viro 已提交
408 409
			if (!dir_emit(ctx, name.name, name.len, ino, type))
				break;
L
Linus Torvalds 已提交
410 411 412
		}
		/* we'll always have progress because d_reclen is unsigned and
		 * we've already established it is non-zero. */
A
Al Viro 已提交
413
		ctx->pos += vdir->d_reclen;
414
	}
A
Al Viro 已提交
415
out:
L
Linus Torvalds 已提交
416
	kfree(vdir);
A
Al Viro 已提交
417
	return 0;
L
Linus Torvalds 已提交
418 419
}

420 421 422 423 424 425 426 427 428 429 430
/* 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;

431
	if (host_file->f_op->iterate || host_file->f_op->iterate_shared) {
432 433 434
		struct inode *host_inode = file_inode(host_file);
		ret = -ENOENT;
		if (!IS_DEADDIR(host_inode)) {
435 436 437 438 439 440 441 442 443 444 445
			if (host_file->f_op->iterate_shared) {
				inode_lock_shared(host_inode);
				ret = host_file->f_op->iterate_shared(host_file, ctx);
				file_accessed(host_file);
				inode_unlock_shared(host_inode);
			} else {
				inode_lock(host_inode);
				ret = host_file->f_op->iterate(host_file, ctx);
				file_accessed(host_file);
				inode_unlock(host_inode);
			}
446 447 448 449 450 451 452
		}
		return ret;
	}
	/* Venus: we must read Venus dirents from a file */
	return coda_venus_readdir(coda_file, ctx);
}

L
Linus Torvalds 已提交
453
/* called when a cache lookup succeeds */
454
static int coda_dentry_revalidate(struct dentry *de, unsigned int flags)
L
Linus Torvalds 已提交
455
{
456
	struct inode *inode;
L
Linus Torvalds 已提交
457 458
	struct coda_inode_info *cii;

459
	if (flags & LOOKUP_RCU)
460 461
		return -ECHILD;

462
	inode = d_inode(de);
A
Al Viro 已提交
463
	if (!inode || is_root_inode(inode))
L
Linus Torvalds 已提交
464 465 466 467
		goto out;
	if (is_bad_inode(inode))
		goto bad;

468
	cii = ITOC(d_inode(de));
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477
	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 已提交
478
	if (d_count(de) > 1)
L
Linus Torvalds 已提交
479 480 481 482
		/* pretend it's valid, but don't change the flags */
		goto out;

	/* clear the flags. */
483
	spin_lock(&cii->c_lock);
L
Linus Torvalds 已提交
484
	cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
485
	spin_unlock(&cii->c_lock);
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495
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 已提交
496
static int coda_dentry_delete(const struct dentry * dentry)
L
Linus Torvalds 已提交
497 498 499
{
	int flags;

500
	if (d_really_is_negative(dentry)) 
L
Linus Torvalds 已提交
501 502
		return 0;

503 504
	flags = (ITOC(d_inode(dentry))->c_flags) & C_PURGE;
	if (is_bad_inode(d_inode(dentry)) || flags) {
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517
		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 
 */
518
int coda_revalidate_inode(struct inode *inode)
L
Linus Torvalds 已提交
519 520
{
	struct coda_vattr attr;
521
	int error;
L
Linus Torvalds 已提交
522 523 524 525
	int old_mode;
	ino_t old_ino;
	struct coda_inode_info *cii = ITOC(inode);

526 527
	if (!cii->c_flags)
		return 0;
L
Linus Torvalds 已提交
528 529 530

	if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
		error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
531 532
		if (error)
			return -EIO;
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543

		/* 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)) {
544
			pr_warn("inode %ld, fid %s changed type!\n",
545
				inode->i_ino, coda_f2s(&(cii->c_fid)));
L
Linus Torvalds 已提交
546 547 548 549 550
		}

		/* 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)
551
			return -EIO;
L
Linus Torvalds 已提交
552 553
		
		coda_flag_inode_children(inode, C_FLUSH);
554 555

		spin_lock(&cii->c_lock);
L
Linus Torvalds 已提交
556
		cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
557
		spin_unlock(&cii->c_lock);
L
Linus Torvalds 已提交
558 559 560
	}
	return 0;
}
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

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,
576
	.rename2	= coda_rename,
577 578 579 580 581 582 583 584 585 586 587 588 589
	.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,
};