Linux 4.18.10
[linux/fpc-iii.git] / fs / adfs / dir.c
blobe18eff854e1a44009baca3b373ac5b2a765d78be
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 "adfs.h"
15 * For future. This should probably be per-directory.
17 static DEFINE_RWLOCK(adfs_dir_lock);
19 static int
20 adfs_readdir(struct file *file, struct dir_context *ctx)
22 struct inode *inode = file_inode(file);
23 struct super_block *sb = inode->i_sb;
24 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
25 struct object_info obj;
26 struct adfs_dir dir;
27 int ret = 0;
29 if (ctx->pos >> 32)
30 return 0;
32 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
33 if (ret)
34 return ret;
36 if (ctx->pos == 0) {
37 if (!dir_emit_dot(file, ctx))
38 goto free_out;
39 ctx->pos = 1;
41 if (ctx->pos == 1) {
42 if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
43 goto free_out;
44 ctx->pos = 2;
47 read_lock(&adfs_dir_lock);
49 ret = ops->setpos(&dir, ctx->pos - 2);
50 if (ret)
51 goto unlock_out;
52 while (ops->getnext(&dir, &obj) == 0) {
53 if (!dir_emit(ctx, obj.name, obj.name_len,
54 obj.file_id, DT_UNKNOWN))
55 break;
56 ctx->pos++;
59 unlock_out:
60 read_unlock(&adfs_dir_lock);
62 free_out:
63 ops->free(&dir);
64 return ret;
67 int
68 adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
70 int ret = -EINVAL;
71 #ifdef CONFIG_ADFS_FS_RW
72 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
73 struct adfs_dir dir;
75 printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
76 obj->file_id, obj->parent_id);
78 if (!ops->update) {
79 ret = -EINVAL;
80 goto out;
83 ret = ops->read(sb, obj->parent_id, 0, &dir);
84 if (ret)
85 goto out;
87 write_lock(&adfs_dir_lock);
88 ret = ops->update(&dir, obj);
89 write_unlock(&adfs_dir_lock);
91 if (wait) {
92 int err = ops->sync(&dir);
93 if (!ret)
94 ret = err;
97 ops->free(&dir);
98 out:
99 #endif
100 return ret;
103 static int
104 adfs_match(const struct qstr *name, struct object_info *obj)
106 int i;
108 if (name->len != obj->name_len)
109 return 0;
111 for (i = 0; i < name->len; i++) {
112 char c1, c2;
114 c1 = name->name[i];
115 c2 = obj->name[i];
117 if (c1 >= 'A' && c1 <= 'Z')
118 c1 += 'a' - 'A';
119 if (c2 >= 'A' && c2 <= 'Z')
120 c2 += 'a' - 'A';
122 if (c1 != c2)
123 return 0;
125 return 1;
128 static int
129 adfs_dir_lookup_byname(struct inode *inode, const struct qstr *name, struct object_info *obj)
131 struct super_block *sb = inode->i_sb;
132 const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
133 struct adfs_dir dir;
134 int ret;
136 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
137 if (ret)
138 goto out;
140 if (ADFS_I(inode)->parent_id != dir.parent_id) {
141 adfs_error(sb, "parent directory changed under me! (%lx but got %x)\n",
142 ADFS_I(inode)->parent_id, dir.parent_id);
143 ret = -EIO;
144 goto free_out;
147 obj->parent_id = inode->i_ino;
149 read_lock(&adfs_dir_lock);
151 ret = ops->setpos(&dir, 0);
152 if (ret)
153 goto unlock_out;
155 ret = -ENOENT;
156 while (ops->getnext(&dir, obj) == 0) {
157 if (adfs_match(name, obj)) {
158 ret = 0;
159 break;
163 unlock_out:
164 read_unlock(&adfs_dir_lock);
166 free_out:
167 ops->free(&dir);
168 out:
169 return ret;
172 const struct file_operations adfs_dir_operations = {
173 .read = generic_read_dir,
174 .llseek = generic_file_llseek,
175 .iterate = adfs_readdir,
176 .fsync = generic_file_fsync,
179 static int
180 adfs_hash(const struct dentry *parent, struct qstr *qstr)
182 const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
183 const unsigned char *name;
184 unsigned long hash;
185 int i;
187 if (qstr->len < name_len)
188 return 0;
191 * Truncate the name in place, avoids
192 * having to define a compare function.
194 qstr->len = i = name_len;
195 name = qstr->name;
196 hash = init_name_hash(parent);
197 while (i--) {
198 char c;
200 c = *name++;
201 if (c >= 'A' && c <= 'Z')
202 c += 'a' - 'A';
204 hash = partial_name_hash(c, hash);
206 qstr->hash = end_name_hash(hash);
208 return 0;
212 * Compare two names, taking note of the name length
213 * requirements of the underlying filesystem.
215 static int
216 adfs_compare(const struct dentry *dentry,
217 unsigned int len, const char *str, const struct qstr *name)
219 int i;
221 if (len != name->len)
222 return 1;
224 for (i = 0; i < name->len; i++) {
225 char a, b;
227 a = str[i];
228 b = name->name[i];
230 if (a >= 'A' && a <= 'Z')
231 a += 'a' - 'A';
232 if (b >= 'A' && b <= 'Z')
233 b += 'a' - 'A';
235 if (a != b)
236 return 1;
238 return 0;
241 const struct dentry_operations adfs_dentry_operations = {
242 .d_hash = adfs_hash,
243 .d_compare = adfs_compare,
246 static struct dentry *
247 adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
249 struct inode *inode = NULL;
250 struct object_info obj;
251 int error;
253 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
254 if (error == 0) {
256 * This only returns NULL if get_empty_inode
257 * fails.
259 inode = adfs_iget(dir->i_sb, &obj);
260 if (!inode)
261 inode = ERR_PTR(-EACCES);
262 } else if (error != -ENOENT) {
263 inode = ERR_PTR(error);
265 return d_splice_alias(inode, dentry);
269 * directories can handle most operations...
271 const struct inode_operations adfs_dir_inode_operations = {
272 .lookup = adfs_lookup,
273 .setattr = adfs_notify_change,