blob: ade28bb058e311dc3b2f120e2405884c4e090f7d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/adfs/dir.c
3 *
4 * Copyright (C) 1999-2000 Russell King
5 *
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.
9 *
10 * Common directory handling for ADFS
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "adfs.h"
13
14/*
15 * For future. This should probably be per-directory.
16 */
17static DEFINE_RWLOCK(adfs_dir_lock);
18
19static int
Al Viro2638ffb2013-05-17 17:30:10 -040020adfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Al Viro2638ffb2013-05-17 17:30:10 -040022 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct super_block *sb = inode->i_sb;
24 struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
25 struct object_info obj;
26 struct adfs_dir dir;
27 int ret = 0;
28
Al Viro2638ffb2013-05-17 17:30:10 -040029 if (ctx->pos >> 32)
30 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
33 if (ret)
Al Viro2638ffb2013-05-17 17:30:10 -040034 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Al Viro2638ffb2013-05-17 17:30:10 -040036 if (ctx->pos == 0) {
37 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 goto free_out;
Al Viro2638ffb2013-05-17 17:30:10 -040039 ctx->pos = 1;
40 }
41 if (ctx->pos == 1) {
42 if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 goto free_out;
Al Viro2638ffb2013-05-17 17:30:10 -040044 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46
47 read_lock(&adfs_dir_lock);
48
Al Viro2638ffb2013-05-17 17:30:10 -040049 ret = ops->setpos(&dir, ctx->pos - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (ret)
51 goto unlock_out;
52 while (ops->getnext(&dir, &obj) == 0) {
Al Viro2638ffb2013-05-17 17:30:10 -040053 if (!dir_emit(ctx, obj.name, obj.name_len,
54 obj.file_id, DT_UNKNOWN))
55 break;
56 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58
59unlock_out:
60 read_unlock(&adfs_dir_lock);
61
62free_out:
63 ops->free(&dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return ret;
65}
66
67int
Al Viroffdc9062009-06-08 00:44:42 -040068adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int ret = -EINVAL;
71#ifdef CONFIG_ADFS_FS_RW
72 struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
73 struct adfs_dir dir;
74
75 printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
76 obj->file_id, obj->parent_id);
77
78 if (!ops->update) {
79 ret = -EINVAL;
80 goto out;
81 }
82
83 ret = ops->read(sb, obj->parent_id, 0, &dir);
84 if (ret)
85 goto out;
86
87 write_lock(&adfs_dir_lock);
88 ret = ops->update(&dir, obj);
89 write_unlock(&adfs_dir_lock);
90
Al Viroffdc9062009-06-08 00:44:42 -040091 if (wait) {
92 int err = ops->sync(&dir);
93 if (!ret)
94 ret = err;
95 }
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ops->free(&dir);
98out:
99#endif
100 return ret;
101}
102
103static int
104adfs_match(struct qstr *name, struct object_info *obj)
105{
106 int i;
107
108 if (name->len != obj->name_len)
109 return 0;
110
111 for (i = 0; i < name->len; i++) {
112 char c1, c2;
113
114 c1 = name->name[i];
115 c2 = obj->name[i];
116
117 if (c1 >= 'A' && c1 <= 'Z')
118 c1 += 'a' - 'A';
119 if (c2 >= 'A' && c2 <= 'Z')
120 c2 += 'a' - 'A';
121
122 if (c1 != c2)
123 return 0;
124 }
125 return 1;
126}
127
128static int
129adfs_dir_lookup_byname(struct inode *inode, struct qstr *name, struct object_info *obj)
130{
131 struct super_block *sb = inode->i_sb;
132 struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
133 struct adfs_dir dir;
134 int ret;
135
136 ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
137 if (ret)
138 goto out;
139
140 if (ADFS_I(inode)->parent_id != dir.parent_id) {
141 adfs_error(sb, "parent directory changed under me! (%lx but got %lx)\n",
142 ADFS_I(inode)->parent_id, dir.parent_id);
143 ret = -EIO;
144 goto free_out;
145 }
146
147 obj->parent_id = inode->i_ino;
148
149 /*
150 * '.' is handled by reserved_lookup() in fs/namei.c
151 */
152 if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
153 /*
154 * Currently unable to fill in the rest of 'obj',
155 * but this is better than nothing. We need to
156 * ascend one level to find it's parent.
157 */
158 obj->name_len = 0;
159 obj->file_id = obj->parent_id;
160 goto free_out;
161 }
162
163 read_lock(&adfs_dir_lock);
164
165 ret = ops->setpos(&dir, 0);
166 if (ret)
167 goto unlock_out;
168
169 ret = -ENOENT;
170 while (ops->getnext(&dir, obj) == 0) {
171 if (adfs_match(name, obj)) {
172 ret = 0;
173 break;
174 }
175 }
176
177unlock_out:
178 read_unlock(&adfs_dir_lock);
179
180free_out:
181 ops->free(&dir);
182out:
183 return ret;
184}
185
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800186const struct file_operations adfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .read = generic_read_dir,
Al Viro59af1582008-08-24 07:24:41 -0400188 .llseek = generic_file_llseek,
Al Viro2638ffb2013-05-17 17:30:10 -0400189 .iterate = adfs_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200190 .fsync = generic_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
193static int
Nick Pigginb1e6a012011-01-07 17:49:28 +1100194adfs_hash(const struct dentry *parent, const struct inode *inode,
195 struct qstr *qstr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
198 const unsigned char *name;
199 unsigned long hash;
200 int i;
201
202 if (qstr->len < name_len)
203 return 0;
204
205 /*
206 * Truncate the name in place, avoids
207 * having to define a compare function.
208 */
209 qstr->len = i = name_len;
210 name = qstr->name;
211 hash = init_name_hash();
212 while (i--) {
213 char c;
214
215 c = *name++;
216 if (c >= 'A' && c <= 'Z')
217 c += 'a' - 'A';
218
219 hash = partial_name_hash(c, hash);
220 }
221 qstr->hash = end_name_hash(hash);
222
223 return 0;
224}
225
226/*
227 * Compare two names, taking note of the name length
228 * requirements of the underlying filesystem.
229 */
230static int
Nick Piggin621e1552011-01-07 17:49:27 +1100231adfs_compare(const struct dentry *parent, const struct inode *pinode,
232 const struct dentry *dentry, const struct inode *inode,
233 unsigned int len, const char *str, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int i;
236
Nick Piggin621e1552011-01-07 17:49:27 +1100237 if (len != name->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 1;
239
240 for (i = 0; i < name->len; i++) {
241 char a, b;
242
Nick Piggin621e1552011-01-07 17:49:27 +1100243 a = str[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 b = name->name[i];
245
246 if (a >= 'A' && a <= 'Z')
247 a += 'a' - 'A';
248 if (b >= 'A' && b <= 'Z')
249 b += 'a' - 'A';
250
251 if (a != b)
252 return 1;
253 }
254 return 0;
255}
256
Al Viroe16404e2009-02-20 05:55:13 +0000257const struct dentry_operations adfs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .d_hash = adfs_hash,
259 .d_compare = adfs_compare,
260};
261
262static struct dentry *
Al Viro00cd8dd2012-06-10 17:13:09 -0400263adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct inode *inode = NULL;
266 struct object_info obj;
267 int error;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
270 if (error == 0) {
271 error = -EACCES;
272 /*
273 * This only returns NULL if get_empty_inode
274 * fails.
275 */
276 inode = adfs_iget(dir->i_sb, &obj);
277 if (inode)
278 error = 0;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 d_add(dentry, inode);
281 return ERR_PTR(error);
282}
283
284/*
285 * directories can handle most operations...
286 */
Arjan van de Ven754661f2007-02-12 00:55:38 -0800287const struct inode_operations adfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .lookup = adfs_lookup,
289 .setattr = adfs_notify_change,
290};