Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  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 */
 12#include "adfs.h"
 13
 14/*
 15 * For future.  This should probably be per-directory.
 16 */
 17static DEFINE_RWLOCK(adfs_dir_lock);
 18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 19static int
 20adfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 21{
 22	struct inode *inode = filp->f_path.dentry->d_inode;
 23	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
 29	if (filp->f_pos >> 32)
 30		goto out;
 31
 32	ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
 33	if (ret)
 34		goto out;
 35
 36	switch ((unsigned long)filp->f_pos) {
 37	case 0:
 38		if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
 39			goto free_out;
 40		filp->f_pos += 1;
 41
 42	case 1:
 43		if (filldir(dirent, "..", 2, 1, dir.parent_id, DT_DIR) < 0)
 44			goto free_out;
 45		filp->f_pos += 1;
 46
 47	default:
 48		break;
 49	}
 50
 51	read_lock(&adfs_dir_lock);
 52
 53	ret = ops->setpos(&dir, filp->f_pos - 2);
 54	if (ret)
 55		goto unlock_out;
 56	while (ops->getnext(&dir, &obj) == 0) {
 57		if (filldir(dirent, obj.name, obj.name_len,
 58			    filp->f_pos, obj.file_id, DT_UNKNOWN) < 0)
 59			goto unlock_out;
 60		filp->f_pos += 1;
 61	}
 62
 63unlock_out:
 64	read_unlock(&adfs_dir_lock);
 65
 66free_out:
 67	ops->free(&dir);
 68
 69out:
 70	return ret;
 71}
 72
 73int
 74adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
 75{
 76	int ret = -EINVAL;
 77#ifdef CONFIG_ADFS_FS_RW
 78	struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
 79	struct adfs_dir dir;
 80
 81	printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
 82		 obj->file_id, obj->parent_id);
 83
 84	if (!ops->update) {
 85		ret = -EINVAL;
 86		goto out;
 87	}
 88
 89	ret = ops->read(sb, obj->parent_id, 0, &dir);
 90	if (ret)
 91		goto out;
 92
 93	write_lock(&adfs_dir_lock);
 94	ret = ops->update(&dir, obj);
 95	write_unlock(&adfs_dir_lock);
 96
 97	if (wait) {
 98		int err = ops->sync(&dir);
 99		if (!ret)
100			ret = err;
101	}
102
103	ops->free(&dir);
104out:
105#endif
106	return ret;
107}
108
109static int
110adfs_match(struct qstr *name, struct object_info *obj)
111{
112	int i;
113
114	if (name->len != obj->name_len)
115		return 0;
116
117	for (i = 0; i < name->len; i++) {
118		char c1, c2;
 
 
119
120		c1 = name->name[i];
121		c2 = obj->name[i];
122
123		if (c1 >= 'A' && c1 <= 'Z')
124			c1 += 'a' - 'A';
125		if (c2 >= 'A' && c2 <= 'Z')
126			c2 += 'a' - 'A';
127
128		if (c1 != c2)
129			return 0;
130	}
131	return 1;
132}
133
134static int
135adfs_dir_lookup_byname(struct inode *inode, struct qstr *name, struct object_info *obj)
136{
137	struct super_block *sb = inode->i_sb;
138	struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
 
139	struct adfs_dir dir;
 
140	int ret;
141
142	ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
143	if (ret)
144		goto out;
145
146	if (ADFS_I(inode)->parent_id != dir.parent_id) {
147		adfs_error(sb, "parent directory changed under me! (%lx but got %lx)\n",
 
148			   ADFS_I(inode)->parent_id, dir.parent_id);
149		ret = -EIO;
150		goto free_out;
151	}
152
153	obj->parent_id = inode->i_ino;
154
155	/*
156	 * '.' is handled by reserved_lookup() in fs/namei.c
157	 */
158	if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
159		/*
160		 * Currently unable to fill in the rest of 'obj',
161		 * but this is better than nothing.  We need to
162		 * ascend one level to find it's parent.
163		 */
164		obj->name_len = 0;
165		obj->file_id  = obj->parent_id;
166		goto free_out;
167	}
168
169	read_lock(&adfs_dir_lock);
170
171	ret = ops->setpos(&dir, 0);
172	if (ret)
173		goto unlock_out;
174
175	ret = -ENOENT;
 
 
176	while (ops->getnext(&dir, obj) == 0) {
177		if (adfs_match(name, obj)) {
178			ret = 0;
179			break;
180		}
181	}
182
183unlock_out:
184	read_unlock(&adfs_dir_lock);
185
186free_out:
187	ops->free(&dir);
188out:
189	return ret;
190}
191
192const struct file_operations adfs_dir_operations = {
193	.read		= generic_read_dir,
194	.llseek		= generic_file_llseek,
195	.readdir	= adfs_readdir,
196	.fsync		= generic_file_fsync,
197};
198
199static int
200adfs_hash(const struct dentry *parent, const struct inode *inode,
201		struct qstr *qstr)
202{
203	const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
204	const unsigned char *name;
205	unsigned long hash;
206	int i;
207
208	if (qstr->len < name_len)
209		return 0;
210
211	/*
212	 * Truncate the name in place, avoids
213	 * having to define a compare function.
214	 */
215	qstr->len = i = name_len;
216	name = qstr->name;
217	hash = init_name_hash();
218	while (i--) {
219		char c;
220
221		c = *name++;
222		if (c >= 'A' && c <= 'Z')
223			c += 'a' - 'A';
224
225		hash = partial_name_hash(c, hash);
226	}
227	qstr->hash = end_name_hash(hash);
228
229	return 0;
230}
231
232/*
233 * Compare two names, taking note of the name length
234 * requirements of the underlying filesystem.
235 */
236static int
237adfs_compare(const struct dentry *parent, const struct inode *pinode,
238		const struct dentry *dentry, const struct inode *inode,
239		unsigned int len, const char *str, const struct qstr *name)
240{
241	int i;
242
243	if (len != name->len)
244		return 1;
245
246	for (i = 0; i < name->len; i++) {
247		char a, b;
248
249		a = str[i];
250		b = name->name[i];
251
252		if (a >= 'A' && a <= 'Z')
253			a += 'a' - 'A';
254		if (b >= 'A' && b <= 'Z')
255			b += 'a' - 'A';
256
257		if (a != b)
258			return 1;
259	}
260	return 0;
261}
262
263const struct dentry_operations adfs_dentry_operations = {
264	.d_hash		= adfs_hash,
265	.d_compare	= adfs_compare,
266};
267
268static struct dentry *
269adfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
270{
271	struct inode *inode = NULL;
272	struct object_info obj;
273	int error;
274
275	error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
276	if (error == 0) {
277		error = -EACCES;
278		/*
279		 * This only returns NULL if get_empty_inode
280		 * fails.
281		 */
282		inode = adfs_iget(dir->i_sb, &obj);
283		if (inode)
284			error = 0;
 
 
285	}
286	d_add(dentry, inode);
287	return ERR_PTR(error);
288}
289
290/*
291 * directories can handle most operations...
292 */
293const struct inode_operations adfs_dir_inode_operations = {
294	.lookup		= adfs_lookup,
295	.setattr	= adfs_notify_change,
296};
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  linux/fs/adfs/dir.c
  4 *
  5 *  Copyright (C) 1999-2000 Russell King
  6 *
 
 
 
 
  7 *  Common directory handling for ADFS
  8 */
  9#include "adfs.h"
 10
 11/*
 12 * For future.  This should probably be per-directory.
 13 */
 14static DEFINE_RWLOCK(adfs_dir_lock);
 15
 16void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj)
 17{
 18	unsigned int dots, i;
 19
 20	/*
 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.)
 28	 */
 29	for (i = dots = 0; i < obj->name_len; i++)
 30		if (obj->name[i] == '/') {
 31			obj->name[i] = '.';
 32			dots++;
 33		}
 34
 35	if (obj->name_len <= 2 && dots == obj->name_len)
 36		obj->name[0] = '^';
 37
 38	/*
 39	 * If the object is a file, and the user requested the ,xyz hex
 40	 * filetype suffix to the name, check the filetype and append.
 41	 */
 42	if (!(obj->attr & ADFS_NDA_DIRECTORY) && ADFS_SB(dir->sb)->s_ftsuffix) {
 43		u16 filetype = adfs_filetype(obj->loadaddr);
 44
 45		if (filetype != ADFS_FILETYPE_NONE) {
 46			obj->name[obj->name_len++] = ',';
 47			obj->name[obj->name_len++] = hex_asc_lo(filetype >> 8);
 48			obj->name[obj->name_len++] = hex_asc_lo(filetype >> 4);
 49			obj->name[obj->name_len++] = hex_asc_lo(filetype >> 0);
 50		}
 51	}
 52}
 53
 54static int
 55adfs_readdir(struct file *file, struct dir_context *ctx)
 56{
 57	struct inode *inode = file_inode(file);
 58	struct super_block *sb = inode->i_sb;
 59	const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
 60	struct object_info obj;
 61	struct adfs_dir dir;
 62	int ret = 0;
 63
 64	if (ctx->pos >> 32)
 65		return 0;
 66
 67	ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
 68	if (ret)
 69		return ret;
 70
 71	if (ctx->pos == 0) {
 72		if (!dir_emit_dot(file, ctx))
 
 73			goto free_out;
 74		ctx->pos = 1;
 75	}
 76	if (ctx->pos == 1) {
 77		if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
 78			goto free_out;
 79		ctx->pos = 2;
 
 
 
 80	}
 81
 82	read_lock(&adfs_dir_lock);
 83
 84	ret = ops->setpos(&dir, ctx->pos - 2);
 85	if (ret)
 86		goto unlock_out;
 87	while (ops->getnext(&dir, &obj) == 0) {
 88		if (!dir_emit(ctx, obj.name, obj.name_len,
 89			      obj.indaddr, DT_UNKNOWN))
 90			break;
 91		ctx->pos++;
 92	}
 93
 94unlock_out:
 95	read_unlock(&adfs_dir_lock);
 96
 97free_out:
 98	ops->free(&dir);
 
 
 99	return ret;
100}
101
102int
103adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
104{
105	int ret = -EINVAL;
106#ifdef CONFIG_ADFS_FS_RW
107	const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
108	struct adfs_dir dir;
109
110	printk(KERN_INFO "adfs_dir_update: object %06x in dir %06x\n",
111		 obj->indaddr, obj->parent_id);
112
113	if (!ops->update) {
114		ret = -EINVAL;
115		goto out;
116	}
117
118	ret = ops->read(sb, obj->parent_id, 0, &dir);
119	if (ret)
120		goto out;
121
122	write_lock(&adfs_dir_lock);
123	ret = ops->update(&dir, obj);
124	write_unlock(&adfs_dir_lock);
125
126	if (wait) {
127		int err = ops->sync(&dir);
128		if (!ret)
129			ret = err;
130	}
131
132	ops->free(&dir);
133out:
134#endif
135	return ret;
136}
137
138static unsigned char adfs_tolower(unsigned char c)
 
139{
140	if (c >= 'A' && c <= 'Z')
141		c += 'a' - 'A';
142	return c;
143}
144
145static int __adfs_compare(const unsigned char *qstr, u32 qlen,
146			  const char *str, u32 len)
147{
148	u32 i;
149
150	if (qlen != len)
151		return 1;
152
153	for (i = 0; i < qlen; i++)
154		if (adfs_tolower(qstr[i]) != adfs_tolower(str[i]))
155			return 1;
 
156
157	return 0;
 
 
 
158}
159
160static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr,
161				  struct object_info *obj)
162{
163	struct super_block *sb = inode->i_sb;
164	const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
165	const unsigned char *name;
166	struct adfs_dir dir;
167	u32 name_len;
168	int ret;
169
170	ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
171	if (ret)
172		goto out;
173
174	if (ADFS_I(inode)->parent_id != dir.parent_id) {
175		adfs_error(sb,
176			   "parent directory changed under me! (%06x but got %06x)\n",
177			   ADFS_I(inode)->parent_id, dir.parent_id);
178		ret = -EIO;
179		goto free_out;
180	}
181
182	obj->parent_id = inode->i_ino;
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184	read_lock(&adfs_dir_lock);
185
186	ret = ops->setpos(&dir, 0);
187	if (ret)
188		goto unlock_out;
189
190	ret = -ENOENT;
191	name = qstr->name;
192	name_len = qstr->len;
193	while (ops->getnext(&dir, obj) == 0) {
194		if (!__adfs_compare(name, name_len, obj->name, obj->name_len)) {
195			ret = 0;
196			break;
197		}
198	}
199
200unlock_out:
201	read_unlock(&adfs_dir_lock);
202
203free_out:
204	ops->free(&dir);
205out:
206	return ret;
207}
208
209const struct file_operations adfs_dir_operations = {
210	.read		= generic_read_dir,
211	.llseek		= generic_file_llseek,
212	.iterate	= adfs_readdir,
213	.fsync		= generic_file_fsync,
214};
215
216static int
217adfs_hash(const struct dentry *parent, struct qstr *qstr)
 
218{
 
219	const unsigned char *name;
220	unsigned long hash;
221	u32 len;
222
223	if (qstr->len > ADFS_SB(parent->d_sb)->s_namelen)
224		return -ENAMETOOLONG;
225
226	len = qstr->len;
 
 
 
 
227	name = qstr->name;
228	hash = init_name_hash(parent);
229	while (len--)
230		hash = partial_name_hash(adfs_tolower(*name++), hash);
 
 
 
 
 
 
 
231	qstr->hash = end_name_hash(hash);
232
233	return 0;
234}
235
236/*
237 * Compare two names, taking note of the name length
238 * requirements of the underlying filesystem.
239 */
240static int adfs_compare(const struct dentry *dentry, unsigned int len,
241			const char *str, const struct qstr *qstr)
 
 
242{
243	return __adfs_compare(qstr->name, qstr->len, str, len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244}
245
246const struct dentry_operations adfs_dentry_operations = {
247	.d_hash		= adfs_hash,
248	.d_compare	= adfs_compare,
249};
250
251static struct dentry *
252adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
253{
254	struct inode *inode = NULL;
255	struct object_info obj;
256	int error;
257
258	error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
259	if (error == 0) {
 
260		/*
261		 * This only returns NULL if get_empty_inode
262		 * fails.
263		 */
264		inode = adfs_iget(dir->i_sb, &obj);
265		if (!inode)
266			inode = ERR_PTR(-EACCES);
267	} else if (error != -ENOENT) {
268		inode = ERR_PTR(error);
269	}
270	return d_splice_alias(inode, dentry);
 
271}
272
273/*
274 * directories can handle most operations...
275 */
276const struct inode_operations adfs_dir_inode_operations = {
277	.lookup		= adfs_lookup,
278	.setattr	= adfs_notify_change,
279};