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>
16 * For future. This should probably be per-directory.
18 static DEFINE_RWLOCK(adfs_dir_lock);
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;
32 if (filp->f_pos >> 32)
35 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
39 switch ((unsigned long)filp->f_pos) {
41 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
46 if (filldir(dirent, "..", 2, 1, dir.parent_id, DT_DIR) < 0)
54 read_lock(&adfs_dir_lock);
56 ret = ops->setpos(&dir, filp->f_pos - 2);
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)
67 read_unlock(&adfs_dir_lock);
78 adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
81 #ifdef CONFIG_ADFS_FS_RW
82 struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
85 printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
86 obj->file_id, obj->parent_id);
93 ret = ops->read(sb, obj->parent_id, 0, &dir);
97 write_lock(&adfs_dir_lock);
98 ret = ops->update(&dir, obj);
99 write_unlock(&adfs_dir_lock);
102 int err = ops->sync(&dir);
114 adfs_match(struct qstr *name, struct object_info *obj)
118 if (name->len != obj->name_len)
121 for (i = 0; i < name->len; i++) {
127 if (c1 >= 'A' && c1 <= 'Z')
129 if (c2 >= 'A' && c2 <= 'Z')
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;
146 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
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);
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.
169 obj->file_id = obj->parent_id;
173 read_lock(&adfs_dir_lock);
175 ret = ops->setpos(&dir, 0);
180 while (ops->getnext(&dir, obj) == 0) {
181 if (adfs_match(name, obj)) {
188 read_unlock(&adfs_dir_lock);
196 const struct file_operations adfs_dir_operations = {
197 .read = generic_read_dir,
198 .llseek = generic_file_llseek,
199 .readdir = adfs_readdir,
200 .fsync = simple_fsync,
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;
211 if (qstr->len < name_len)
215 * Truncate the name in place, avoids
216 * having to define a compare function.
218 qstr->len = i = name_len;
220 hash = init_name_hash();
225 if (c >= 'A' && c <= 'Z')
228 hash = partial_name_hash(c, hash);
230 qstr->hash = end_name_hash(hash);
236 * Compare two names, taking note of the name length
237 * requirements of the underlying filesystem.
240 adfs_compare(struct dentry *parent, struct qstr *entry, struct qstr *name)
244 if (entry->len != name->len)
247 for (i = 0; i < name->len; i++) {
253 if (a >= 'A' && a <= 'Z')
255 if (b >= 'A' && b <= 'Z')
264 const struct dentry_operations adfs_dentry_operations = {
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;
276 dentry->d_op = &adfs_dentry_operations;
278 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
282 * This only returns NULL if get_empty_inode
285 inode = adfs_iget(dir->i_sb, &obj);
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,