2 * linux/fs/affs/namei.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
8 * (C) 1991 Linus Torvalds - minix filesystem
11 #include <linux/sched.h>
12 #include <linux/affs_fs.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/stat.h>
16 #include <linux/fcntl.h>
17 #include <linux/locks.h>
18 #include <linux/amigaffs.h>
19 #include <asm/uaccess.h>
21 #include <linux/errno.h>
23 typedef int (*toupper_t)(int);
25 extern struct inode_operations affs_symlink_inode_operations;
27 static int affs_toupper(int ch);
28 static int affs_hash_dentry(struct dentry *, struct qstr *);
29 static int affs_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
30 static int affs_intl_toupper(int ch);
31 static int affs_intl_hash_dentry(struct dentry *, struct qstr *);
32 static int affs_intl_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
34 struct dentry_operations affs_dentry_operations = {
35 d_hash: affs_hash_dentry,
36 d_compare: affs_compare_dentry,
39 struct dentry_operations affs_intl_dentry_operations = {
40 d_hash: affs_intl_hash_dentry,
41 d_compare: affs_intl_compare_dentry,
45 /* Simple toupper() for DOS\1 */
50 return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
53 /* International toupper() for DOS\3 ("international") */
56 affs_intl_toupper(int ch)
58 return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
59 && ch <= 0xFE && ch != 0xF7) ?
60 ch - ('a' - 'A') : ch;
63 static inline toupper_t
64 affs_get_toupper(struct super_block *sb)
66 return AFFS_SB->s_flags & SF_INTL ? affs_intl_toupper : affs_toupper;
70 * Note: the dentry argument is the parent dentry.
73 __affs_hash_dentry(struct dentry *dentry, struct qstr *qstr, toupper_t toupper)
75 const u8 *name = qstr->name;
79 i = affs_check_name(qstr->name,qstr->len);
83 hash = init_name_hash();
84 i = min(qstr->len, 30u);
85 for (; i > 0; name++, i--)
86 hash = partial_name_hash(toupper(*name), hash);
87 qstr->hash = end_name_hash(hash);
93 affs_hash_dentry(struct dentry *dentry, struct qstr *qstr)
95 return __affs_hash_dentry(dentry, qstr, affs_toupper);
98 affs_intl_hash_dentry(struct dentry *dentry, struct qstr *qstr)
100 return __affs_hash_dentry(dentry, qstr, affs_intl_toupper);
104 __affs_compare_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b, toupper_t toupper)
106 const u8 *aname = a->name;
107 const u8 *bname = b->name;
110 /* 'a' is the qstr of an already existing dentry, so the name
111 * must be valid. 'b' must be validated first.
114 if (affs_check_name(b->name,b->len))
117 /* If the names are longer than the allowed 30 chars,
118 * the excess is ignored, so their length may differ.
125 } else if (len != b->len)
128 for (; len > 0; len--)
129 if (toupper(*aname++) != toupper(*bname++))
136 affs_compare_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b)
138 return __affs_compare_dentry(dentry, a, b, affs_toupper);
141 affs_intl_compare_dentry(struct dentry *dentry, struct qstr *a, struct qstr *b)
143 return __affs_compare_dentry(dentry, a, b, affs_intl_toupper);
147 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
151 affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
153 const u8 *name = dentry->d_name.name;
154 int len = dentry->d_name.len;
160 } else if (len != *name2)
163 for (name2++; len > 0; len--)
164 if (toupper(*name++) != toupper(*name2++))
170 affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
172 toupper_t toupper = affs_get_toupper(sb);
175 hash = len = min(len, 30u);
176 for (; len > 0; len--)
177 hash = (hash * 13 + toupper(*name++)) & 0x7ff;
179 return hash % AFFS_SB->s_hashsize;
182 static struct buffer_head *
183 affs_find_entry(struct inode *dir, struct dentry *dentry)
185 struct super_block *sb = dir->i_sb;
186 struct buffer_head *bh;
187 toupper_t toupper = affs_get_toupper(sb);
190 pr_debug("AFFS: find_entry(\"%.*s\")\n", (int)dentry->d_name.len, dentry->d_name.name);
192 bh = affs_bread(sb, dir->i_ino);
194 return ERR_PTR(-EIO);
196 key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
202 bh = affs_bread(sb, key);
204 return ERR_PTR(-EIO);
205 if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
207 key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
212 affs_lookup(struct inode *dir, struct dentry *dentry)
214 struct super_block *sb = dir->i_sb;
215 struct buffer_head *bh;
216 struct inode *inode = NULL;
218 pr_debug("AFFS: lookup(\"%.*s\")\n",(int)dentry->d_name.len,dentry->d_name.name);
221 bh = affs_find_entry(dir, dentry);
222 affs_unlock_dir(dir);
224 return ERR_PTR(PTR_ERR(bh));
226 u32 ino = bh->b_blocknr;
228 /* store the real header ino in d_fsdata for faster lookups */
229 dentry->d_fsdata = (void *)(long)ino;
230 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
231 //link to dirs disabled
234 ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
237 inode = iget(sb, ino);
239 return ERR_PTR(-EACCES);
241 dentry->d_op = AFFS_SB->s_flags & SF_INTL ? &affs_intl_dentry_operations : &affs_dentry_operations;
242 d_add(dentry, inode);
247 affs_unlink(struct inode *dir, struct dentry *dentry)
249 pr_debug("AFFS: unlink(dir=%d, \"%.*s\")\n", (u32)dir->i_ino,
250 (int)dentry->d_name.len, dentry->d_name.name);
252 if (!dentry->d_inode)
255 return affs_remove_header(dentry);
259 affs_create(struct inode *dir, struct dentry *dentry, int mode)
261 struct super_block *sb = dir->i_sb;
265 pr_debug("AFFS: create(%lu,\"%.*s\",0%o)\n",dir->i_ino,(int)dentry->d_name.len,
266 dentry->d_name.name,mode);
268 inode = affs_new_inode(dir);
272 inode->i_mode = mode;
274 mark_inode_dirty(inode);
276 inode->i_op = &affs_file_inode_operations;
277 inode->i_fop = &affs_file_operations;
278 inode->i_mapping->a_ops = (AFFS_SB->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
279 error = affs_add_entry(dir, inode, dentry, ST_FILE);
289 affs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
294 pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%o)\n",dir->i_ino,
295 (int)dentry->d_name.len,dentry->d_name.name,mode);
297 inode = affs_new_inode(dir);
301 inode->i_mode = S_IFDIR | mode;
304 inode->i_op = &affs_dir_inode_operations;
305 inode->i_fop = &affs_dir_operations;
307 error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
310 mark_inode_dirty(inode);
318 affs_rmdir(struct inode *dir, struct dentry *dentry)
320 pr_debug("AFFS: rmdir(dir=%u, \"%.*s\")\n", (u32)dir->i_ino,
321 (int)dentry->d_name.len, dentry->d_name.name);
323 if (!dentry->d_inode)
326 return affs_remove_header(dentry);
330 affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
332 struct super_block *sb = dir->i_sb;
333 struct buffer_head *bh;
336 int i, maxlen, error;
339 pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")\n",dir->i_ino,
340 (int)dentry->d_name.len,dentry->d_name.name,symname);
342 maxlen = AFFS_SB->s_hashsize * sizeof(u32) - 1;
344 inode = affs_new_inode(dir);
348 inode->i_op = &affs_symlink_inode_operations;
349 inode->i_data.a_ops = &affs_symlink_aops;
350 inode->i_mode = S_IFLNK | 0777;
354 bh = affs_bread(sb, inode->i_ino);
358 p = (char *)AFFS_HEAD(bh)->table;
360 if (*symname == '/') {
361 while (*symname == '/')
363 while (AFFS_SB->s_volume[i]) /* Cannot overflow */
364 *p++ = AFFS_SB->s_volume[i++];
366 while (i < maxlen && (c = *symname++)) {
367 if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
372 } else if (c == '.' && lc == '/' && *symname == '/') {
381 while (*symname == '/')
385 mark_buffer_dirty_inode(bh, inode);
387 mark_inode_dirty(inode);
389 error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
397 mark_inode_dirty(inode);
403 affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
405 struct inode *inode = old_dentry->d_inode;
408 pr_debug("AFFS: link(%u, %u, \"%.*s\")\n", (u32)inode->i_ino, (u32)dir->i_ino,
409 (int)dentry->d_name.len,dentry->d_name.name);
411 if (S_ISDIR(inode->i_mode))
414 error = affs_add_entry(dir, inode, dentry, S_ISDIR(inode->i_mode) ? ST_LINKDIR : ST_LINKFILE);
417 mark_inode_dirty(inode);
425 affs_rename(struct inode *old_dir, struct dentry *old_dentry,
426 struct inode *new_dir, struct dentry *new_dentry)
428 struct super_block *sb = old_dir->i_sb;
429 struct buffer_head *bh = NULL;
432 pr_debug("AFFS: rename(old=%u,\"%*s\" to new=%u,\"%*s\")\n",
433 (u32)old_dir->i_ino, (int)old_dentry->d_name.len, old_dentry->d_name.name,
434 (u32)new_dir->i_ino, (int)new_dentry->d_name.len, new_dentry->d_name.name);
436 if ((retval = affs_check_name(new_dentry->d_name.name,new_dentry->d_name.len)))
439 /* Unlink destination if it already exists */
440 if (new_dentry->d_inode) {
441 retval = affs_remove_header(new_dentry);
447 bh = affs_bread(sb, old_dentry->d_inode->i_ino);
451 /* Remove header from its parent directory. */
452 affs_lock_dir(old_dir);
453 retval = affs_remove_hash(old_dir, bh);
454 affs_unlock_dir(old_dir);
458 /* And insert it into the new directory with the new name. */
459 affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
460 affs_fix_checksum(sb, bh);
461 affs_lock_dir(new_dir);
462 retval = affs_insert_hash(new_dir, bh);
463 affs_unlock_dir(new_dir);
464 /* TODO: move it back to old_dir, if error? */
467 mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);