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
12 #include <linux/exportfs.h>
14 typedef int (*toupper_t)(int);
16 /* Simple toupper() for DOS\1 */
21 return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch;
24 /* International toupper() for DOS\3 ("international") */
27 affs_intl_toupper(int ch)
29 return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0
30 && ch <= 0xFE && ch != 0xF7) ?
31 ch - ('a' - 'A') : ch;
34 static inline toupper_t
35 affs_get_toupper(struct super_block *sb)
37 return affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL) ?
38 affs_intl_toupper : affs_toupper;
42 * Note: the dentry argument is the parent dentry.
45 __affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate)
47 const u8 *name = qstr->name;
52 retval = affs_check_name(qstr->name, qstr->len, notruncate);
56 hash = init_name_hash(dentry);
57 len = min(qstr->len, AFFSNAMEMAX);
58 for (; len > 0; name++, len--)
59 hash = partial_name_hash(toupper(*name), hash);
60 qstr->hash = end_name_hash(hash);
66 affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
68 return __affs_hash_dentry(dentry, qstr, affs_toupper,
69 affs_nofilenametruncate(dentry));
74 affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
76 return __affs_hash_dentry(dentry, qstr, affs_intl_toupper,
77 affs_nofilenametruncate(dentry));
81 static inline int __affs_compare_dentry(unsigned int len,
82 const char *str, const struct qstr *name, toupper_t toupper,
85 const u8 *aname = str;
86 const u8 *bname = name->name;
89 * 'str' is the name of an already existing dentry, so the name
90 * must be valid. 'name' must be validated first.
93 if (affs_check_name(name->name, name->len, notruncate))
97 * If the names are longer than the allowed 30 chars,
98 * the excess is ignored, so their length may differ.
100 if (len >= AFFSNAMEMAX) {
101 if (name->len < AFFSNAMEMAX)
104 } else if (len != name->len)
107 for (; len > 0; len--)
108 if (toupper(*aname++) != toupper(*bname++))
115 affs_compare_dentry(const struct dentry *dentry,
116 unsigned int len, const char *str, const struct qstr *name)
119 return __affs_compare_dentry(len, str, name, affs_toupper,
120 affs_nofilenametruncate(dentry));
124 affs_intl_compare_dentry(const struct dentry *dentry,
125 unsigned int len, const char *str, const struct qstr *name)
127 return __affs_compare_dentry(len, str, name, affs_intl_toupper,
128 affs_nofilenametruncate(dentry));
133 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
137 affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper)
139 const u8 *name = dentry->d_name.name;
140 int len = dentry->d_name.len;
142 if (len >= AFFSNAMEMAX) {
143 if (*name2 < AFFSNAMEMAX)
146 } else if (len != *name2)
149 for (name2++; len > 0; len--)
150 if (toupper(*name++) != toupper(*name2++))
156 affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len)
158 toupper_t toupper = affs_get_toupper(sb);
161 hash = len = min(len, AFFSNAMEMAX);
162 for (; len > 0; len--)
163 hash = (hash * 13 + toupper(*name++)) & 0x7ff;
165 return hash % AFFS_SB(sb)->s_hashsize;
168 static struct buffer_head *
169 affs_find_entry(struct inode *dir, struct dentry *dentry)
171 struct super_block *sb = dir->i_sb;
172 struct buffer_head *bh;
173 toupper_t toupper = affs_get_toupper(sb);
176 pr_debug("%s(\"%pd\")\n", __func__, dentry);
178 bh = affs_bread(sb, dir->i_ino);
180 return ERR_PTR(-EIO);
182 key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]);
188 bh = affs_bread(sb, key);
190 return ERR_PTR(-EIO);
191 if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper))
193 key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
198 affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
200 struct super_block *sb = dir->i_sb;
201 struct buffer_head *bh;
202 struct inode *inode = NULL;
204 pr_debug("%s(\"%pd\")\n", __func__, dentry);
207 bh = affs_find_entry(dir, dentry);
208 affs_unlock_dir(dir);
212 u32 ino = bh->b_blocknr;
214 /* store the real header ino in d_fsdata for faster lookups */
215 dentry->d_fsdata = (void *)(long)ino;
216 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
217 //link to dirs disabled
220 ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original);
223 inode = affs_iget(sb, ino);
225 return ERR_CAST(inode);
227 d_add(dentry, inode);
232 affs_unlink(struct inode *dir, struct dentry *dentry)
234 pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
235 d_inode(dentry)->i_ino, dentry);
237 return affs_remove_header(dentry);
241 affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
243 struct super_block *sb = dir->i_sb;
247 pr_debug("%s(%lu,\"%pd\",0%ho)\n",
248 __func__, dir->i_ino, dentry, mode);
250 inode = affs_new_inode(dir);
254 inode->i_mode = mode;
255 affs_mode_to_prot(inode);
256 mark_inode_dirty(inode);
258 inode->i_op = &affs_file_inode_operations;
259 inode->i_fop = &affs_file_operations;
260 inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ?
261 &affs_aops_ofs : &affs_aops;
262 error = affs_add_entry(dir, inode, dentry, ST_FILE);
272 affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
277 pr_debug("%s(%lu,\"%pd\",0%ho)\n",
278 __func__, dir->i_ino, dentry, mode);
280 inode = affs_new_inode(dir);
284 inode->i_mode = S_IFDIR | mode;
285 affs_mode_to_prot(inode);
287 inode->i_op = &affs_dir_inode_operations;
288 inode->i_fop = &affs_dir_operations;
290 error = affs_add_entry(dir, inode, dentry, ST_USERDIR);
293 mark_inode_dirty(inode);
301 affs_rmdir(struct inode *dir, struct dentry *dentry)
303 pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino,
304 d_inode(dentry)->i_ino, dentry);
306 return affs_remove_header(dentry);
310 affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
312 struct super_block *sb = dir->i_sb;
313 struct buffer_head *bh;
316 int i, maxlen, error;
319 pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n",
320 __func__, dir->i_ino, dentry, symname);
322 maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1;
323 inode = affs_new_inode(dir);
327 inode->i_op = &affs_symlink_inode_operations;
328 inode_nohighmem(inode);
329 inode->i_data.a_ops = &affs_symlink_aops;
330 inode->i_mode = S_IFLNK | 0777;
331 affs_mode_to_prot(inode);
334 bh = affs_bread(sb, inode->i_ino);
338 p = (char *)AFFS_HEAD(bh)->table;
340 if (*symname == '/') {
341 struct affs_sb_info *sbi = AFFS_SB(sb);
342 while (*symname == '/')
344 spin_lock(&sbi->symlink_lock);
345 while (sbi->s_volume[i]) /* Cannot overflow */
346 *p++ = sbi->s_volume[i++];
347 spin_unlock(&sbi->symlink_lock);
349 while (i < maxlen && (c = *symname++)) {
350 if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') {
355 } else if (c == '.' && lc == '/' && *symname == '/') {
364 while (*symname == '/')
368 inode->i_size = i + 1;
369 mark_buffer_dirty_inode(bh, inode);
371 mark_inode_dirty(inode);
373 error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK);
381 mark_inode_dirty(inode);
387 affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
389 struct inode *inode = d_inode(old_dentry);
391 pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino,
394 return affs_add_entry(dir, inode, dentry, ST_LINKFILE);
398 affs_rename(struct inode *old_dir, struct dentry *old_dentry,
399 struct inode *new_dir, struct dentry *new_dentry)
401 struct super_block *sb = old_dir->i_sb;
402 struct buffer_head *bh = NULL;
405 retval = affs_check_name(new_dentry->d_name.name,
406 new_dentry->d_name.len,
407 affs_nofilenametruncate(old_dentry));
412 /* Unlink destination if it already exists */
413 if (d_really_is_positive(new_dentry)) {
414 retval = affs_remove_header(new_dentry);
419 bh = affs_bread(sb, d_inode(old_dentry)->i_ino);
423 /* Remove header from its parent directory. */
424 affs_lock_dir(old_dir);
425 retval = affs_remove_hash(old_dir, bh);
426 affs_unlock_dir(old_dir);
430 /* And insert it into the new directory with the new name. */
431 affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry);
432 affs_fix_checksum(sb, bh);
433 affs_lock_dir(new_dir);
434 retval = affs_insert_hash(new_dir, bh);
435 affs_unlock_dir(new_dir);
436 /* TODO: move it back to old_dir, if error? */
439 mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir);
445 affs_xrename(struct inode *old_dir, struct dentry *old_dentry,
446 struct inode *new_dir, struct dentry *new_dentry)
449 struct super_block *sb = old_dir->i_sb;
450 struct buffer_head *bh_old = NULL;
451 struct buffer_head *bh_new = NULL;
454 bh_old = affs_bread(sb, d_inode(old_dentry)->i_ino);
458 bh_new = affs_bread(sb, d_inode(new_dentry)->i_ino);
462 /* Remove old header from its parent directory. */
463 affs_lock_dir(old_dir);
464 retval = affs_remove_hash(old_dir, bh_old);
465 affs_unlock_dir(old_dir);
469 /* Remove new header from its parent directory. */
470 affs_lock_dir(new_dir);
471 retval = affs_remove_hash(new_dir, bh_new);
472 affs_unlock_dir(new_dir);
476 /* Insert old into the new directory with the new name. */
477 affs_copy_name(AFFS_TAIL(sb, bh_old)->name, new_dentry);
478 affs_fix_checksum(sb, bh_old);
479 affs_lock_dir(new_dir);
480 retval = affs_insert_hash(new_dir, bh_old);
481 affs_unlock_dir(new_dir);
483 /* Insert new into the old directory with the old name. */
484 affs_copy_name(AFFS_TAIL(sb, bh_new)->name, old_dentry);
485 affs_fix_checksum(sb, bh_new);
486 affs_lock_dir(old_dir);
487 retval = affs_insert_hash(old_dir, bh_new);
488 affs_unlock_dir(old_dir);
490 mark_buffer_dirty_inode(bh_old, new_dir);
491 mark_buffer_dirty_inode(bh_new, old_dir);
497 int affs_rename2(struct inode *old_dir, struct dentry *old_dentry,
498 struct inode *new_dir, struct dentry *new_dentry,
502 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
505 pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__,
506 old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry);
508 if (flags & RENAME_EXCHANGE)
509 return affs_xrename(old_dir, old_dentry, new_dir, new_dentry);
511 return affs_rename(old_dir, old_dentry, new_dir, new_dentry);
514 static struct dentry *affs_get_parent(struct dentry *child)
516 struct inode *parent;
517 struct buffer_head *bh;
519 bh = affs_bread(child->d_sb, d_inode(child)->i_ino);
521 return ERR_PTR(-EIO);
523 parent = affs_iget(child->d_sb,
524 be32_to_cpu(AFFS_TAIL(child->d_sb, bh)->parent));
527 return ERR_CAST(parent);
529 return d_obtain_alias(parent);
532 static struct inode *affs_nfs_get_inode(struct super_block *sb, u64 ino,
537 if (!affs_validblock(sb, ino))
538 return ERR_PTR(-ESTALE);
540 inode = affs_iget(sb, ino);
542 return ERR_CAST(inode);
547 static struct dentry *affs_fh_to_dentry(struct super_block *sb, struct fid *fid,
548 int fh_len, int fh_type)
550 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
554 static struct dentry *affs_fh_to_parent(struct super_block *sb, struct fid *fid,
555 int fh_len, int fh_type)
557 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
561 const struct export_operations affs_export_ops = {
562 .fh_to_dentry = affs_fh_to_dentry,
563 .fh_to_parent = affs_fh_to_parent,
564 .get_parent = affs_get_parent,
567 const struct dentry_operations affs_dentry_operations = {
568 .d_hash = affs_hash_dentry,
569 .d_compare = affs_compare_dentry,
572 const struct dentry_operations affs_intl_dentry_operations = {
573 .d_hash = affs_intl_hash_dentry,
574 .d_compare = affs_intl_compare_dentry,