1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1999-2000 Russell King
7 * Common directory handling for ADFS
12 * For future. This should probably be per-directory.
14 static DEFINE_RWLOCK(adfs_dir_lock);
16 void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj)
21 * RISC OS allows the use of '/' in directory entry names, so we need
22 * to fix these up. '/' is typically used for FAT compatibility to
23 * represent '.', so do the same conversion here. In any case, '.'
24 * will never be in a RISC OS name since it is used as the pathname
25 * separator. Handle the case where we may generate a '.' or '..'
26 * name, replacing the first character with '^' (the RISC OS "parent
27 * directory" character.)
29 for (i = dots = 0; i < obj->name_len; i++)
30 if (obj->name[i] == '/') {
35 if (obj->name_len <= 2 && dots == obj->name_len)
41 * object is a file and is filetyped and timestamped?
42 * RISC OS 12-bit filetype is stored in load_address[19:8]
44 if ((0 == (obj->attr & ADFS_NDA_DIRECTORY)) &&
45 (0xfff00000 == (0xfff00000 & obj->loadaddr))) {
46 obj->filetype = (__u16) ((0x000fff00 & obj->loadaddr) >> 8);
48 /* optionally append the ,xyz hex filetype suffix */
49 if (ADFS_SB(dir->sb)->s_ftsuffix) {
50 __u16 filetype = obj->filetype;
52 obj->name[obj->name_len++] = ',';
53 obj->name[obj->name_len++] = hex_asc_lo(filetype >> 8);
54 obj->name[obj->name_len++] = hex_asc_lo(filetype >> 4);
55 obj->name[obj->name_len++] = hex_asc_lo(filetype >> 0);
61 adfs_readdir(struct file *file, struct dir_context *ctx)
63 struct inode *inode = file_inode(file);
64 struct super_block *sb = inode->i_sb;
65 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
66 struct object_info obj;
73 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
78 if (!dir_emit_dot(file, ctx))
83 if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
88 read_lock(&adfs_dir_lock);
90 ret = ops->setpos(&dir, ctx->pos - 2);
93 while (ops->getnext(&dir, &obj) == 0) {
94 if (!dir_emit(ctx, obj.name, obj.name_len,
95 obj.file_id, DT_UNKNOWN))
101 read_unlock(&adfs_dir_lock);
109 adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
112 #ifdef CONFIG_ADFS_FS_RW
113 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
116 printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
117 obj->file_id, obj->parent_id);
124 ret = ops->read(sb, obj->parent_id, 0, &dir);
128 write_lock(&adfs_dir_lock);
129 ret = ops->update(&dir, obj);
130 write_unlock(&adfs_dir_lock);
133 int err = ops->sync(&dir);
144 static unsigned char adfs_tolower(unsigned char c)
146 if (c >= 'A' && c <= 'Z')
151 static int __adfs_compare(const unsigned char *qstr, u32 qlen,
152 const char *str, u32 len)
159 for (i = 0; i < qlen; i++)
160 if (adfs_tolower(qstr[i]) != adfs_tolower(str[i]))
166 static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr,
167 struct object_info *obj)
169 struct super_block *sb = inode->i_sb;
170 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
171 const unsigned char *name;
176 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
180 if (ADFS_I(inode)->parent_id != dir.parent_id) {
181 adfs_error(sb, "parent directory changed under me! (%lx but got %x)\n",
182 ADFS_I(inode)->parent_id, dir.parent_id);
187 obj->parent_id = inode->i_ino;
189 read_lock(&adfs_dir_lock);
191 ret = ops->setpos(&dir, 0);
197 name_len = qstr->len;
198 while (ops->getnext(&dir, obj) == 0) {
199 if (!__adfs_compare(name, name_len, obj->name, obj->name_len)) {
206 read_unlock(&adfs_dir_lock);
214 const struct file_operations adfs_dir_operations = {
215 .read = generic_read_dir,
216 .llseek = generic_file_llseek,
217 .iterate = adfs_readdir,
218 .fsync = generic_file_fsync,
222 adfs_hash(const struct dentry *parent, struct qstr *qstr)
224 const unsigned char *name;
228 if (qstr->len > ADFS_SB(parent->d_sb)->s_namelen)
229 return -ENAMETOOLONG;
233 hash = init_name_hash(parent);
235 hash = partial_name_hash(adfs_tolower(*name++), hash);
236 qstr->hash = end_name_hash(hash);
242 * Compare two names, taking note of the name length
243 * requirements of the underlying filesystem.
245 static int adfs_compare(const struct dentry *dentry, unsigned int len,
246 const char *str, const struct qstr *qstr)
248 return __adfs_compare(qstr->name, qstr->len, str, len);
251 const struct dentry_operations adfs_dentry_operations = {
253 .d_compare = adfs_compare,
256 static struct dentry *
257 adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
259 struct inode *inode = NULL;
260 struct object_info obj;
263 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
266 * This only returns NULL if get_empty_inode
269 inode = adfs_iget(dir->i_sb, &obj);
271 inode = ERR_PTR(-EACCES);
272 } else if (error != -ENOENT) {
273 inode = ERR_PTR(error);
275 return d_splice_alias(inode, dentry);
279 * directories can handle most operations...
281 const struct inode_operations adfs_dir_inode_operations = {
282 .lookup = adfs_lookup,
283 .setattr = adfs_notify_change,