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/config.h>
13 #include <linux/version.h>
14 #include <linux/errno.h>
16 #include <linux/adfs_fs.h>
17 #include <linux/sched.h>
18 #include <linux/stat.h>
19 #include <linux/spinlock.h>
24 * For future. This should probably be per-directory.
26 static rwlock_t adfs_dir_lock;
29 adfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
31 struct inode *inode = filp->f_dentry->d_inode;
32 struct super_block *sb = inode->i_sb;
33 struct adfs_dir_ops *ops = sb->u.adfs_sb.s_dir;
34 struct object_info obj;
38 if (filp->f_pos >> 32)
41 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
45 switch ((unsigned long)filp->f_pos) {
47 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
52 if (filldir(dirent, "..", 2, 1, dir.parent_id, DT_DIR) < 0)
60 read_lock(&adfs_dir_lock);
62 ret = ops->setpos(&dir, filp->f_pos - 2);
65 while (ops->getnext(&dir, &obj) == 0) {
66 if (filldir(dirent, obj.name, obj.name_len,
67 filp->f_pos, obj.file_id, DT_UNKNOWN) < 0)
73 read_unlock(&adfs_dir_lock);
83 adfs_dir_update(struct super_block *sb, struct object_info *obj)
86 #ifdef CONFIG_ADFS_FS_RW
87 struct adfs_dir_ops *ops = sb->u.adfs_sb.s_dir;
90 printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
91 obj->file_id, obj->parent_id);
98 ret = ops->read(sb, obj->parent_id, 0, &dir);
102 write_lock(&adfs_dir_lock);
103 ret = ops->update(&dir, obj);
104 write_unlock(&adfs_dir_lock);
113 adfs_match(struct qstr *name, struct object_info *obj)
117 if (name->len != obj->name_len)
120 for (i = 0; i < name->len; i++) {
126 if (c1 >= 'A' && c1 <= 'Z')
128 if (c2 >= 'A' && c2 <= 'Z')
138 adfs_dir_lookup_byname(struct inode *inode, struct qstr *name, struct object_info *obj)
140 struct super_block *sb = inode->i_sb;
141 struct adfs_dir_ops *ops = sb->u.adfs_sb.s_dir;
145 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
149 if (inode->u.adfs_i.parent_id != dir.parent_id) {
150 adfs_error(sb, "parent directory changed under me! (%lx but got %lx)\n",
151 inode->u.adfs_i.parent_id, dir.parent_id);
156 obj->parent_id = inode->i_ino;
159 * '.' is handled by reserved_lookup() in fs/namei.c
161 if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
163 * Currently unable to fill in the rest of 'obj',
164 * but this is better than nothing. We need to
165 * ascend one level to find it's parent.
168 obj->file_id = obj->parent_id;
172 read_lock(&adfs_dir_lock);
174 ret = ops->setpos(&dir, 0);
179 while (ops->getnext(&dir, obj) == 0) {
180 if (adfs_match(name, obj)) {
187 read_unlock(&adfs_dir_lock);
195 struct file_operations adfs_dir_operations = {
196 read: generic_read_dir,
197 readdir: adfs_readdir,
202 adfs_hash(struct dentry *parent, struct qstr *qstr)
204 const unsigned int name_len = parent->d_sb->u.adfs_sb.s_namelen;
205 const unsigned char *name;
209 if (qstr->len < name_len)
213 * Truncate the name in place, avoids
214 * having to define a compare function.
216 qstr->len = i = name_len;
218 hash = init_name_hash();
223 if (c >= 'A' && c <= 'Z')
226 hash = partial_name_hash(c, hash);
228 qstr->hash = end_name_hash(hash);
234 * Compare two names, taking note of the name length
235 * requirements of the underlying filesystem.
238 adfs_compare(struct dentry *parent, struct qstr *entry, struct qstr *name)
242 if (entry->len != name->len)
245 for (i = 0; i < name->len; i++) {
251 if (a >= 'A' && a <= 'Z')
253 if (b >= 'A' && b <= 'Z')
262 struct dentry_operations adfs_dentry_operations = {
264 d_compare: adfs_compare,
267 struct dentry *adfs_lookup(struct inode *dir, struct dentry *dentry)
269 struct inode *inode = NULL;
270 struct object_info obj;
273 dentry->d_op = &adfs_dentry_operations;
274 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
278 * This only returns NULL if get_empty_inode
281 inode = adfs_iget(dir->i_sb, &obj);
285 d_add(dentry, inode);
286 return ERR_PTR(error);
290 * directories can handle most operations...
292 struct inode_operations adfs_dir_inode_operations = {
294 setattr: adfs_notify_change,