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
15 * For future. This should probably be per-directory.
17 static DEFINE_RWLOCK(adfs_dir_lock);
19 void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj)
24 * RISC OS allows the use of '/' in directory entry names, so we need
25 * to fix these up. '/' is typically used for FAT compatibility to
26 * represent '.', so do the same conversion here. In any case, '.'
27 * will never be in a RISC OS name since it is used as the pathname
30 for (i = 0; i < obj->name_len; i++)
31 if (obj->name[i] == '/')
37 * object is a file and is filetyped and timestamped?
38 * RISC OS 12-bit filetype is stored in load_address[19:8]
40 if ((0 == (obj->attr & ADFS_NDA_DIRECTORY)) &&
41 (0xfff00000 == (0xfff00000 & obj->loadaddr))) {
42 obj->filetype = (__u16) ((0x000fff00 & obj->loadaddr) >> 8);
44 /* optionally append the ,xyz hex filetype suffix */
45 if (ADFS_SB(dir->sb)->s_ftsuffix) {
46 __u16 filetype = obj->filetype;
48 obj->name[obj->name_len++] = ',';
49 obj->name[obj->name_len++] = hex_asc_lo(filetype >> 8);
50 obj->name[obj->name_len++] = hex_asc_lo(filetype >> 4);
51 obj->name[obj->name_len++] = hex_asc_lo(filetype >> 0);
57 adfs_readdir(struct file *file, struct dir_context *ctx)
59 struct inode *inode = file_inode(file);
60 struct super_block *sb = inode->i_sb;
61 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
62 struct object_info obj;
69 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
74 if (!dir_emit_dot(file, ctx))
79 if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
84 read_lock(&adfs_dir_lock);
86 ret = ops->setpos(&dir, ctx->pos - 2);
89 while (ops->getnext(&dir, &obj) == 0) {
90 if (!dir_emit(ctx, obj.name, obj.name_len,
91 obj.file_id, DT_UNKNOWN))
97 read_unlock(&adfs_dir_lock);
105 adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
108 #ifdef CONFIG_ADFS_FS_RW
109 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
112 printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
113 obj->file_id, obj->parent_id);
120 ret = ops->read(sb, obj->parent_id, 0, &dir);
124 write_lock(&adfs_dir_lock);
125 ret = ops->update(&dir, obj);
126 write_unlock(&adfs_dir_lock);
129 int err = ops->sync(&dir);
140 static unsigned char adfs_tolower(unsigned char c)
142 if (c >= 'A' && c <= 'Z')
147 static int __adfs_compare(const unsigned char *qstr, u32 qlen,
148 const char *str, u32 len)
155 for (i = 0; i < qlen; i++)
156 if (adfs_tolower(qstr[i]) != adfs_tolower(str[i]))
162 static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr,
163 struct object_info *obj)
165 struct super_block *sb = inode->i_sb;
166 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
167 const unsigned char *name;
172 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
176 if (ADFS_I(inode)->parent_id != dir.parent_id) {
177 adfs_error(sb, "parent directory changed under me! (%lx but got %x)\n",
178 ADFS_I(inode)->parent_id, dir.parent_id);
183 obj->parent_id = inode->i_ino;
185 read_lock(&adfs_dir_lock);
187 ret = ops->setpos(&dir, 0);
193 name_len = qstr->len;
194 while (ops->getnext(&dir, obj) == 0) {
195 if (!__adfs_compare(name, name_len, obj->name, obj->name_len)) {
202 read_unlock(&adfs_dir_lock);
210 const struct file_operations adfs_dir_operations = {
211 .read = generic_read_dir,
212 .llseek = generic_file_llseek,
213 .iterate = adfs_readdir,
214 .fsync = generic_file_fsync,
218 adfs_hash(const struct dentry *parent, struct qstr *qstr)
220 const unsigned char *name;
224 if (qstr->len > ADFS_SB(parent->d_sb)->s_namelen)
225 return -ENAMETOOLONG;
229 hash = init_name_hash(parent);
231 hash = partial_name_hash(adfs_tolower(*name++), hash);
232 qstr->hash = end_name_hash(hash);
238 * Compare two names, taking note of the name length
239 * requirements of the underlying filesystem.
241 static int adfs_compare(const struct dentry *dentry, unsigned int len,
242 const char *str, const struct qstr *qstr)
244 return __adfs_compare(qstr->name, qstr->len, str, len);
247 const struct dentry_operations adfs_dentry_operations = {
249 .d_compare = adfs_compare,
252 static struct dentry *
253 adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
255 struct inode *inode = NULL;
256 struct object_info obj;
259 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
262 * This only returns NULL if get_empty_inode
265 inode = adfs_iget(dir->i_sb, &obj);
267 inode = ERR_PTR(-EACCES);
268 } else if (error != -ENOENT) {
269 inode = ERR_PTR(error);
271 return d_splice_alias(inode, dentry);
275 * directories can handle most operations...
277 const struct inode_operations adfs_dir_inode_operations = {
278 .lookup = adfs_lookup,
279 .setattr = adfs_notify_change,