Linux 2.6.35-rc2
[linux/fpc-iii.git] / fs / adfs / dir.c
blobf4287e4de744093d8f71d10a1e443057befe6e22
1 /*
2 * linux/fs/adfs/dir.c
4 * Copyright (C) 1999-2000 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Common directory handling for ADFS
12 #include <linux/smp_lock.h>
13 #include "adfs.h"
16 * For future. This should probably be per-directory.
18 static DEFINE_RWLOCK(adfs_dir_lock);
20 static int
21 adfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
23 struct inode *inode = filp->f_path.dentry->d_inode;
24 struct super_block *sb = inode->i_sb;
25 struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
26 struct object_info obj;
27 struct adfs_dir dir;
28 int ret = 0;
30 lock_kernel();
32 if (filp->f_pos >> 32)
33 goto out;
35 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
36 if (ret)
37 goto out;
39 switch ((unsigned long)filp->f_pos) {
40 case 0:
41 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
42 goto free_out;
43 filp->f_pos += 1;
45 case 1:
46 if (filldir(dirent, "..", 2, 1, dir.parent_id, DT_DIR) < 0)
47 goto free_out;
48 filp->f_pos += 1;
50 default:
51 break;
54 read_lock(&adfs_dir_lock);
56 ret = ops->setpos(&dir, filp->f_pos - 2);
57 if (ret)
58 goto unlock_out;
59 while (ops->getnext(&dir, &obj) == 0) {
60 if (filldir(dirent, obj.name, obj.name_len,
61 filp->f_pos, obj.file_id, DT_UNKNOWN) < 0)
62 goto unlock_out;
63 filp->f_pos += 1;
66 unlock_out:
67 read_unlock(&adfs_dir_lock);
69 free_out:
70 ops->free(&dir);
72 out:
73 unlock_kernel();
74 return ret;
77 int
78 adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
80 int ret = -EINVAL;
81 #ifdef CONFIG_ADFS_FS_RW
82 struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
83 struct adfs_dir dir;
85 printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
86 obj->file_id, obj->parent_id);
88 if (!ops->update) {
89 ret = -EINVAL;
90 goto out;
93 ret = ops->read(sb, obj->parent_id, 0, &dir);
94 if (ret)
95 goto out;
97 write_lock(&adfs_dir_lock);
98 ret = ops->update(&dir, obj);
99 write_unlock(&adfs_dir_lock);
101 if (wait) {
102 int err = ops->sync(&dir);
103 if (!ret)
104 ret = err;
107 ops->free(&dir);
108 out:
109 #endif
110 return ret;
113 static int
114 adfs_match(struct qstr *name, struct object_info *obj)
116 int i;
118 if (name->len != obj->name_len)
119 return 0;
121 for (i = 0; i < name->len; i++) {
122 char c1, c2;
124 c1 = name->name[i];
125 c2 = obj->name[i];
127 if (c1 >= 'A' && c1 <= 'Z')
128 c1 += 'a' - 'A';
129 if (c2 >= 'A' && c2 <= 'Z')
130 c2 += 'a' - 'A';
132 if (c1 != c2)
133 return 0;
135 return 1;
138 static int
139 adfs_dir_lookup_byname(struct inode *inode, struct qstr *name, struct object_info *obj)
141 struct super_block *sb = inode->i_sb;
142 struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
143 struct adfs_dir dir;
144 int ret;
146 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
147 if (ret)
148 goto out;
150 if (ADFS_I(inode)->parent_id != dir.parent_id) {
151 adfs_error(sb, "parent directory changed under me! (%lx but got %lx)\n",
152 ADFS_I(inode)->parent_id, dir.parent_id);
153 ret = -EIO;
154 goto free_out;
157 obj->parent_id = inode->i_ino;
160 * '.' is handled by reserved_lookup() in fs/namei.c
162 if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
164 * Currently unable to fill in the rest of 'obj',
165 * but this is better than nothing. We need to
166 * ascend one level to find it's parent.
168 obj->name_len = 0;
169 obj->file_id = obj->parent_id;
170 goto free_out;
173 read_lock(&adfs_dir_lock);
175 ret = ops->setpos(&dir, 0);
176 if (ret)
177 goto unlock_out;
179 ret = -ENOENT;
180 while (ops->getnext(&dir, obj) == 0) {
181 if (adfs_match(name, obj)) {
182 ret = 0;
183 break;
187 unlock_out:
188 read_unlock(&adfs_dir_lock);
190 free_out:
191 ops->free(&dir);
192 out:
193 return ret;
196 const struct file_operations adfs_dir_operations = {
197 .read = generic_read_dir,
198 .llseek = generic_file_llseek,
199 .readdir = adfs_readdir,
200 .fsync = generic_file_fsync,
203 static int
204 adfs_hash(struct dentry *parent, struct qstr *qstr)
206 const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
207 const unsigned char *name;
208 unsigned long hash;
209 int i;
211 if (qstr->len < name_len)
212 return 0;
215 * Truncate the name in place, avoids
216 * having to define a compare function.
218 qstr->len = i = name_len;
219 name = qstr->name;
220 hash = init_name_hash();
221 while (i--) {
222 char c;
224 c = *name++;
225 if (c >= 'A' && c <= 'Z')
226 c += 'a' - 'A';
228 hash = partial_name_hash(c, hash);
230 qstr->hash = end_name_hash(hash);
232 return 0;
236 * Compare two names, taking note of the name length
237 * requirements of the underlying filesystem.
239 static int
240 adfs_compare(struct dentry *parent, struct qstr *entry, struct qstr *name)
242 int i;
244 if (entry->len != name->len)
245 return 1;
247 for (i = 0; i < name->len; i++) {
248 char a, b;
250 a = entry->name[i];
251 b = name->name[i];
253 if (a >= 'A' && a <= 'Z')
254 a += 'a' - 'A';
255 if (b >= 'A' && b <= 'Z')
256 b += 'a' - 'A';
258 if (a != b)
259 return 1;
261 return 0;
264 const struct dentry_operations adfs_dentry_operations = {
265 .d_hash = adfs_hash,
266 .d_compare = adfs_compare,
269 static struct dentry *
270 adfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
272 struct inode *inode = NULL;
273 struct object_info obj;
274 int error;
276 dentry->d_op = &adfs_dentry_operations;
277 lock_kernel();
278 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
279 if (error == 0) {
280 error = -EACCES;
282 * This only returns NULL if get_empty_inode
283 * fails.
285 inode = adfs_iget(dir->i_sb, &obj);
286 if (inode)
287 error = 0;
289 unlock_kernel();
290 d_add(dentry, inode);
291 return ERR_PTR(error);
295 * directories can handle most operations...
297 const struct inode_operations adfs_dir_inode_operations = {
298 .lookup = adfs_lookup,
299 .setattr = adfs_notify_change,