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
13 typedef int (*toupper_t)(int);
15 static int affs_toupper(int ch);
16 static int affs_hash_dentry(const struct dentry *, struct qstr *);
17 static int affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
18 unsigned int len, const char *str, const struct qstr *name);
19 static int affs_intl_toupper(int ch);
20 static int affs_intl_hash_dentry(const struct dentry *, struct qstr *);
21 static int affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
22 unsigned int len, const char *str, const struct qstr *name);
24 const struct dentry_operations affs_dentry_operations = {
25 .d_hash = affs_hash_dentry,
26 .d_compare = affs_compare_dentry,
29 const struct dentry_operations affs_intl_dentry_operations = {
30 .d_hash = affs_intl_hash_dentry,
31 .d_compare = affs_intl_compare_dentry,
35 /* Simple toupper() for DOS\1 */
40 return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
43 /* International toupper() for DOS\3 ("international") */
46 affs_intl_toupper(int ch)
48 return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
49 && ch <= 0xFE && ch != 0xF7) ?
50 ch - ('a' - 'A') : ch;
53 static inline toupper_t
54 affs_get_toupper(struct super_block *sb)
56 return AFFS_SB(sb)->s_flags & SF_INTL ? affs_intl_toupper : affs_toupper;
60 * Note: the dentry argument is the parent dentry.
63 __affs_hash_dentry(struct qstr *qstr, toupper_t toupper)
65 const u8 *name = qstr->name;
69 i = affs_check_name(qstr->name, qstr->len);
73 hash = init_name_hash();
74 i = min(qstr->len, 30u);
75 for (; i > 0; name++, i--)
76 hash = partial_name_hash(toupper(*name), hash);
77 qstr->hash = end_name_hash(hash);
83 affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
85 return __affs_hash_dentry(qstr, affs_toupper);
88 affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
90 return __affs_hash_dentry(qstr, affs_intl_toupper);
93 static inline int __affs_compare_dentry(unsigned int len,
94 const char *str, const struct qstr *name, toupper_t toupper)
96 const u8 *aname = str;
97 const u8 *bname = name->name;
100 * 'str' is the name of an already existing dentry, so the name
101 * must be valid. 'name' must be validated first.
104 if (affs_check_name(name->name, name->len))
108 * If the names are longer than the allowed 30 chars,
109 * the excess is ignored, so their length may differ.
115 } else if (len != name->len)
118 for (; len > 0; len--)
119 if (toupper(*aname++) != toupper(*bname++))
126 affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
127 unsigned int len, const char *str, const struct qstr *name)
129 return __affs_compare_dentry(len, str, name, affs_toupper);
132 affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
133 unsigned int len, const char *str, const struct qstr *name)
135 return __affs_compare_dentry(len, str, name, affs_intl_toupper);
139 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
143 affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
145 const u8 *name = dentry->d_name.name;
146 int len = dentry->d_name.len;
152 } else if (len != *name2)
155 for (name2++; len > 0; len--)
156 if (toupper(*name++) != toupper(*name2++))
162 affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
164 toupper_t toupper = affs_get_toupper(sb);
167 hash = len = min(len, 30u);
168 for (; len > 0; len--)
169 hash = (hash * 13 + toupper(*name++)) & 0x7ff;
171 return hash % AFFS_SB(sb)->s_hashsize;
174 static struct buffer_head *
175 affs_find_entry(struct inode *dir, struct dentry *dentry)
177 struct super_block *sb = dir->i_sb;
178 struct buffer_head *bh;
179 toupper_t toupper = affs_get_toupper(sb);
182 pr_debug("AFFS: find_entry(\"%.*s\")\n", (int)dentry->d_name.len, dentry->d_name.name);
184 bh = affs_bread(sb, dir->i_ino);
186 return ERR_PTR(-EIO);
188 key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
194 bh = affs_bread(sb, key);
196 return ERR_PTR(-EIO);
197 if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
199 key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
204 affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
206 struct super_block *sb = dir->i_sb;
207 struct buffer_head *bh;
208 struct inode *inode = NULL;
210 pr_debug("AFFS: lookup(\"%.*s\")\n",(int)dentry->d_name.len,dentry->d_name.name);
213 bh = affs_find_entry(dir, dentry);
214 affs_unlock_dir(dir);
218 u32 ino = bh->b_blocknr;
220 /* store the real header ino in d_fsdata for faster lookups */
221 dentry->d_fsdata = (void *)(long)ino;
222 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
223 //link to dirs disabled
226 ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
229 inode = affs_iget(sb, ino);
231 return ERR_CAST(inode);
233 d_add(dentry, inode);
238 affs_unlink(struct inode *dir, struct dentry *dentry)
240 pr_debug("AFFS: unlink(dir=%d, %lu \"%.*s\")\n", (u32)dir->i_ino,
241 dentry->d_inode->i_ino,
242 (int)dentry->d_name.len, dentry->d_name.name);
244 return affs_remove_header(dentry);
248 affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
250 struct super_block *sb = dir->i_sb;
254 pr_debug("AFFS: create(%lu,\"%.*s\",0%ho)\n",dir->i_ino,(int)dentry->d_name.len,
255 dentry->d_name.name,mode);
257 inode = affs_new_inode(dir);
261 inode->i_mode = mode;
263 mark_inode_dirty(inode);
265 inode->i_op = &affs_file_inode_operations;
266 inode->i_fop = &affs_file_operations;
267 inode->i_mapping->a_ops = (AFFS_SB(sb)->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
268 error = affs_add_entry(dir, inode, dentry, ST_FILE);
278 affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
283 pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%ho)\n",dir->i_ino,
284 (int)dentry->d_name.len,dentry->d_name.name,mode);
286 inode = affs_new_inode(dir);
290 inode->i_mode = S_IFDIR | mode;
293 inode->i_op = &affs_dir_inode_operations;
294 inode->i_fop = &affs_dir_operations;
296 error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
299 mark_inode_dirty(inode);
307 affs_rmdir(struct inode *dir, struct dentry *dentry)
309 pr_debug("AFFS: rmdir(dir=%u, %lu \"%.*s\")\n", (u32)dir->i_ino,
310 dentry->d_inode->i_ino,
311 (int)dentry->d_name.len, dentry->d_name.name);
313 return affs_remove_header(dentry);
317 affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
319 struct super_block *sb = dir->i_sb;
320 struct buffer_head *bh;
323 int i, maxlen, error;
326 pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")\n",dir->i_ino,
327 (int)dentry->d_name.len,dentry->d_name.name,symname);
329 maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
330 inode = affs_new_inode(dir);
334 inode->i_op = &affs_symlink_inode_operations;
335 inode->i_data.a_ops = &affs_symlink_aops;
336 inode->i_mode = S_IFLNK | 0777;
340 bh = affs_bread(sb, inode->i_ino);
344 p = (char *)AFFS_HEAD(bh)->table;
346 if (*symname == '/') {
347 struct affs_sb_info *sbi = AFFS_SB(sb);
348 while (*symname == '/')
350 spin_lock(&sbi->symlink_lock);
351 while (sbi->s_volume[i]) /* Cannot overflow */
352 *p++ = sbi->s_volume[i++];
353 spin_unlock(&sbi->symlink_lock);
355 while (i < maxlen && (c = *symname++)) {
356 if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
361 } else if (c == '.' && lc == '/' && *symname == '/') {
370 while (*symname == '/')
374 mark_buffer_dirty_inode(bh, inode);
376 mark_inode_dirty(inode);
378 error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
386 mark_inode_dirty(inode);
392 affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
394 struct inode *inode = old_dentry->d_inode;
396 pr_debug("AFFS: link(%u, %u, \"%.*s\")\n", (u32)inode->i_ino, (u32)dir->i_ino,
397 (int)dentry->d_name.len,dentry->d_name.name);
399 return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
403 affs_rename(struct inode *old_dir, struct dentry *old_dentry,
404 struct inode *new_dir, struct dentry *new_dentry)
406 struct super_block *sb = old_dir->i_sb;
407 struct buffer_head *bh = NULL;
410 pr_debug("AFFS: rename(old=%u,\"%*s\" to new=%u,\"%*s\")\n",
411 (u32)old_dir->i_ino, (int)old_dentry->d_name.len, old_dentry->d_name.name,
412 (u32)new_dir->i_ino, (int)new_dentry->d_name.len, new_dentry->d_name.name);
414 retval = affs_check_name(new_dentry->d_name.name,new_dentry->d_name.len);
418 /* Unlink destination if it already exists */
419 if (new_dentry->d_inode) {
420 retval = affs_remove_header(new_dentry);
425 bh = affs_bread(sb, old_dentry->d_inode->i_ino);
429 /* Remove header from its parent directory. */
430 affs_lock_dir(old_dir);
431 retval = affs_remove_hash(old_dir, bh);
432 affs_unlock_dir(old_dir);
436 /* And insert it into the new directory with the new name. */
437 affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
438 affs_fix_checksum(sb, bh);
439 affs_lock_dir(new_dir);
440 retval = affs_insert_hash(new_dir, bh);
441 affs_unlock_dir(new_dir);
442 /* TODO: move it back to old_dir, if error? */
445 mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);