dir.c 5.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  linux/fs/adfs/dir.c
 *
 *  Copyright (C) 1999-2000 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  Common directory handling for ADFS
 */
#include "adfs.h"

/*
 * For future.  This should probably be per-directory.
 */
static DEFINE_RWLOCK(adfs_dir_lock);

static int
A
Al Viro 已提交
20
adfs_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
21
{
A
Al Viro 已提交
22
	struct inode *inode = file_inode(file);
L
Linus Torvalds 已提交
23
	struct super_block *sb = inode->i_sb;
24
	const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
L
Linus Torvalds 已提交
25 26 27 28
	struct object_info obj;
	struct adfs_dir dir;
	int ret = 0;

A
Al Viro 已提交
29 30
	if (ctx->pos >> 32)
		return 0;
L
Linus Torvalds 已提交
31 32 33

	ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
	if (ret)
A
Al Viro 已提交
34
		return ret;
L
Linus Torvalds 已提交
35

A
Al Viro 已提交
36 37
	if (ctx->pos == 0) {
		if (!dir_emit_dot(file, ctx))
L
Linus Torvalds 已提交
38
			goto free_out;
A
Al Viro 已提交
39 40 41 42
		ctx->pos = 1;
	}
	if (ctx->pos == 1) {
		if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
L
Linus Torvalds 已提交
43
			goto free_out;
A
Al Viro 已提交
44
		ctx->pos = 2;
L
Linus Torvalds 已提交
45 46 47 48
	}

	read_lock(&adfs_dir_lock);

A
Al Viro 已提交
49
	ret = ops->setpos(&dir, ctx->pos - 2);
L
Linus Torvalds 已提交
50 51 52
	if (ret)
		goto unlock_out;
	while (ops->getnext(&dir, &obj) == 0) {
A
Al Viro 已提交
53 54 55 56
		if (!dir_emit(ctx, obj.name, obj.name_len,
			    obj.file_id, DT_UNKNOWN))
			break;
		ctx->pos++;
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67
	}

unlock_out:
	read_unlock(&adfs_dir_lock);

free_out:
	ops->free(&dir);
	return ret;
}

int
68
adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
L
Linus Torvalds 已提交
69 70 71
{
	int ret = -EINVAL;
#ifdef CONFIG_ADFS_FS_RW
72
	const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	struct adfs_dir dir;

	printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
		 obj->file_id, obj->parent_id);

	if (!ops->update) {
		ret = -EINVAL;
		goto out;
	}

	ret = ops->read(sb, obj->parent_id, 0, &dir);
	if (ret)
		goto out;

	write_lock(&adfs_dir_lock);
	ret = ops->update(&dir, obj);
	write_unlock(&adfs_dir_lock);

91 92 93 94 95 96
	if (wait) {
		int err = ops->sync(&dir);
		if (!ret)
			ret = err;
	}

L
Linus Torvalds 已提交
97 98 99 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 129 130 131
	ops->free(&dir);
out:
#endif
	return ret;
}

static int
adfs_match(struct qstr *name, struct object_info *obj)
{
	int i;

	if (name->len != obj->name_len)
		return 0;

	for (i = 0; i < name->len; i++) {
		char c1, c2;

		c1 = name->name[i];
		c2 = obj->name[i];

		if (c1 >= 'A' && c1 <= 'Z')
			c1 += 'a' - 'A';
		if (c2 >= 'A' && c2 <= 'Z')
			c2 += 'a' - 'A';

		if (c1 != c2)
			return 0;
	}
	return 1;
}

static int
adfs_dir_lookup_byname(struct inode *inode, struct qstr *name, struct object_info *obj)
{
	struct super_block *sb = inode->i_sb;
132
	const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140
	struct adfs_dir dir;
	int ret;

	ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
	if (ret)
		goto out;

	if (ADFS_I(inode)->parent_id != dir.parent_id) {
141
		adfs_error(sb, "parent directory changed under me! (%lx but got %x)\n",
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
			   ADFS_I(inode)->parent_id, dir.parent_id);
		ret = -EIO;
		goto free_out;
	}

	obj->parent_id = inode->i_ino;

	/*
	 * '.' is handled by reserved_lookup() in fs/namei.c
	 */
	if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
		/*
		 * Currently unable to fill in the rest of 'obj',
		 * but this is better than nothing.  We need to
		 * ascend one level to find it's parent.
		 */
		obj->name_len = 0;
		obj->file_id  = obj->parent_id;
		goto free_out;
	}

	read_lock(&adfs_dir_lock);

	ret = ops->setpos(&dir, 0);
	if (ret)
		goto unlock_out;

	ret = -ENOENT;
	while (ops->getnext(&dir, obj) == 0) {
		if (adfs_match(name, obj)) {
			ret = 0;
			break;
		}
	}

unlock_out:
	read_unlock(&adfs_dir_lock);

free_out:
	ops->free(&dir);
out:
	return ret;
}

186
const struct file_operations adfs_dir_operations = {
L
Linus Torvalds 已提交
187
	.read		= generic_read_dir,
188
	.llseek		= generic_file_llseek,
A
Al Viro 已提交
189
	.iterate	= adfs_readdir,
190
	.fsync		= generic_file_fsync,
L
Linus Torvalds 已提交
191 192 193
};

static int
194
adfs_hash(const struct dentry *parent, struct qstr *qstr)
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{
	const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
	const unsigned char *name;
	unsigned long hash;
	int i;

	if (qstr->len < name_len)
		return 0;

	/*
	 * Truncate the name in place, avoids
	 * having to define a compare function.
	 */
	qstr->len = i = name_len;
	name = qstr->name;
210
	hash = init_name_hash(parent);
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	while (i--) {
		char c;

		c = *name++;
		if (c >= 'A' && c <= 'Z')
			c += 'a' - 'A';

		hash = partial_name_hash(c, hash);
	}
	qstr->hash = end_name_hash(hash);

	return 0;
}

/*
 * Compare two names, taking note of the name length
 * requirements of the underlying filesystem.
 */
static int
230
adfs_compare(const struct dentry *dentry,
N
Nick Piggin 已提交
231
		unsigned int len, const char *str, const struct qstr *name)
L
Linus Torvalds 已提交
232 233 234
{
	int i;

N
Nick Piggin 已提交
235
	if (len != name->len)
L
Linus Torvalds 已提交
236 237 238 239 240
		return 1;

	for (i = 0; i < name->len; i++) {
		char a, b;

N
Nick Piggin 已提交
241
		a = str[i];
L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254
		b = name->name[i];

		if (a >= 'A' && a <= 'Z')
			a += 'a' - 'A';
		if (b >= 'A' && b <= 'Z')
			b += 'a' - 'A';

		if (a != b)
			return 1;
	}
	return 0;
}

255
const struct dentry_operations adfs_dentry_operations = {
L
Linus Torvalds 已提交
256 257 258 259 260
	.d_hash		= adfs_hash,
	.d_compare	= adfs_compare,
};

static struct dentry *
A
Al Viro 已提交
261
adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
{
	struct inode *inode = NULL;
	struct object_info obj;
	int error;

	error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
	if (error == 0) {
		error = -EACCES;
		/*
		 * This only returns NULL if get_empty_inode
		 * fails.
		 */
		inode = adfs_iget(dir->i_sb, &obj);
		if (inode)
			error = 0;
	}
	d_add(dentry, inode);
	return ERR_PTR(error);
}

/*
 * directories can handle most operations...
 */
285
const struct inode_operations adfs_dir_inode_operations = {
L
Linus Torvalds 已提交
286 287 288
	.lookup		= adfs_lookup,
	.setattr	= adfs_notify_change,
};