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/sched.h>
13 #include <linux/affs_fs.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/stat.h>
17 #include <linux/fcntl.h>
18 #include <linux/locks.h>
19 #include <linux/amigaffs.h>
20 #include <asm/uaccess.h>
22 #include <linux/errno.h>
24 /* Simple toupper() for DOS\1 */
27 affs_toupper(unsigned int ch
)
29 return ch
>= 'a' && ch
<= 'z' ? ch
-= ('a' - 'A') : ch
;
32 /* International toupper() for DOS\3 ("international") */
35 affs_intl_toupper(unsigned int ch
)
37 return (ch
>= 'a' && ch
<= 'z') || (ch
>= 0xE0
38 && ch
<= 0xFE && ch
!= 0xF7) ?
39 ch
- ('a' - 'A') : ch
;
42 static int affs_hash_dentry(struct dentry
*, struct qstr
*);
43 static int affs_compare_dentry(struct dentry
*, struct qstr
*, struct qstr
*);
44 struct dentry_operations affs_dentry_operations
= {
45 NULL
, /* d_validate */
46 affs_hash_dentry
, /* d_hash */
47 affs_compare_dentry
, /* d_compare */
52 * Note: the dentry argument is the parent dentry.
55 affs_hash_dentry(struct dentry
*dentry
, struct qstr
*qstr
)
57 unsigned int (*toupper
)(unsigned int) = affs_toupper
;
61 if ((i
= affs_check_name(qstr
->name
,qstr
->len
)))
64 /* Check whether to use the international 'toupper' routine */
65 if (AFFS_I2FSTYPE(dentry
->d_inode
))
66 toupper
= affs_intl_toupper
;
67 hash
= init_name_hash();
68 for (i
= 0; i
< qstr
->len
&& i
< 30; i
++)
69 hash
= partial_name_hash(toupper(qstr
->name
[i
]), hash
);
70 qstr
->hash
= end_name_hash(hash
);
76 affs_compare_dentry(struct dentry
*dentry
, struct qstr
*a
, struct qstr
*b
)
78 unsigned int (*toupper
)(unsigned int) = affs_toupper
;
83 /* 'a' is the qstr of an already existing dentry, so the name
84 * must be valid. 'b' must be validated first.
87 if (affs_check_name(b
->name
,b
->len
))
90 /* If the names are longer than the allowed 30 chars,
91 * the excess is ignored, so their length may differ.
100 /* Check whether to use the international 'toupper' routine */
101 if (AFFS_I2FSTYPE(dentry
->d_inode
))
102 toupper
= affs_intl_toupper
;
104 for (i
= 0; i
< alen
; i
++)
105 if (toupper(a
->name
[i
]) != toupper(b
->name
[i
]))
112 * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure.
116 affs_match(const unsigned char *name
, int len
, const unsigned char *compare
, int dlen
, int intl
)
118 unsigned int (*toupper
)(unsigned int) = intl
? affs_intl_toupper
: affs_toupper
;
129 /* "" means "." ---> so paths like "/usr/lib//libc.a" work */
130 if (!len
&& dlen
== 1 && compare
[0] == '.')
134 for (i
= 0; i
< len
; i
++)
135 if (toupper(name
[i
]) != toupper(compare
[i
]))
141 affs_hash_name(const unsigned char *name
, int len
, int intl
, int hashsize
)
149 for (i
= 0; i
< len
; i
++)
151 x
= (x
* 13 + affs_intl_toupper(name
[i
] & 0xFF)) & 0x7ff;
153 x
= (x
* 13 + affs_toupper(name
[i
] & 0xFF)) & 0x7ff;
158 static struct buffer_head
*
159 affs_find_entry(struct inode
*dir
, struct dentry
*dentry
, unsigned long *ino
)
161 struct buffer_head
*bh
;
162 int intl
= AFFS_I2FSTYPE(dir
);
164 const char *name
= dentry
->d_name
.name
;
165 int namelen
= dentry
->d_name
.len
;
167 pr_debug("AFFS: find_entry(\"%.*s\")\n",namelen
,name
);
169 bh
= affs_bread(dir
->i_dev
,dir
->i_ino
,AFFS_I2BSIZE(dir
));
173 if (namelen
== 1 && name
[0] == '.') {
177 if (namelen
== 2 && name
[0] == '.' && name
[1] == '.') {
178 *ino
= affs_parent_ino(dir
);
182 key
= AFFS_GET_HASHENTRY(bh
->b_data
,affs_hash_name(name
,namelen
,intl
,AFFS_I2HSIZE(dir
)));
185 unsigned char *cname
;
192 bh
= affs_bread(dir
->i_dev
,key
,AFFS_I2BSIZE(dir
));
195 cnamelen
= affs_get_file_name(AFFS_I2BSIZE(dir
),bh
->b_data
,&cname
);
196 if (affs_match(name
,namelen
,cname
,cnamelen
,intl
))
198 key
= be32_to_cpu(FILE_END(bh
->b_data
,dir
)->hash_chain
);
205 affs_lookup(struct inode
*dir
, struct dentry
*dentry
)
208 struct buffer_head
*bh
;
211 pr_debug("AFFS: lookup(\"%.*s\")\n",(int)dentry
->d_name
.len
,dentry
->d_name
.name
);
214 bh
= affs_find_entry(dir
,dentry
,&ino
);
216 if (FILE_END(bh
->b_data
,dir
)->original
)
217 ino
= be32_to_cpu(FILE_END(bh
->b_data
,dir
)->original
);
219 inode
= iget(dir
->i_sb
,ino
);
221 return ERR_PTR(-EACCES
);
223 dentry
->d_op
= &affs_dentry_operations
;
229 affs_unlink(struct inode
*dir
, struct dentry
*dentry
)
232 struct buffer_head
*bh
;
236 pr_debug("AFFS: unlink(dir=%ld,\"%.*s\")\n",dir
->i_ino
,
237 (int)dentry
->d_name
.len
,dentry
->d_name
.name
);
240 if (!(bh
= affs_find_entry(dir
,dentry
,&ino
)))
243 inode
= dentry
->d_inode
;
245 if ((retval
= affs_remove_header(bh
,inode
)) < 0)
248 inode
->i_nlink
= retval
;
249 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
250 dir
->i_version
= ++event
;
251 mark_inode_dirty(inode
);
253 mark_inode_dirty(dir
);
262 affs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
267 pr_debug("AFFS: create(%lu,\"%.*s\",0%o)\n",dir
->i_ino
,(int)dentry
->d_name
.len
,
268 dentry
->d_name
.name
,mode
);
271 inode
= affs_new_inode(dir
);
275 pr_debug("AFFS: ino=%lu\n",inode
->i_ino
);
276 if (dir
->i_sb
->u
.affs_sb
.s_flags
& SF_OFS
)
277 inode
->i_op
= &affs_file_inode_operations_ofs
;
279 inode
->i_op
= &affs_file_inode_operations
;
281 error
= affs_add_entry(dir
,NULL
,inode
,dentry
,ST_FILE
);
284 inode
->i_mode
= mode
;
285 inode
->u
.affs_i
.i_protect
= mode_to_prot(inode
->i_mode
);
286 d_instantiate(dentry
,inode
);
287 mark_inode_dirty(inode
);
288 dir
->i_version
= ++event
;
289 mark_inode_dirty(dir
);
295 mark_inode_dirty(inode
);
301 affs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
306 pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%o)\n",dir
->i_ino
,
307 (int)dentry
->d_name
.len
,dentry
->d_name
.name
,mode
);
310 inode
= affs_new_inode(dir
);
314 inode
->i_op
= &affs_dir_inode_operations
;
315 error
= affs_add_entry(dir
,NULL
,inode
,dentry
,ST_USERDIR
);
318 inode
->i_mode
= S_IFDIR
| S_ISVTX
| mode
;
319 inode
->u
.affs_i
.i_protect
= mode_to_prot(inode
->i_mode
);
320 d_instantiate(dentry
,inode
);
321 mark_inode_dirty(inode
);
322 dir
->i_version
= ++event
;
323 mark_inode_dirty(dir
);
329 mark_inode_dirty(inode
);
335 empty_dir(struct buffer_head
*bh
, int hashsize
)
337 while (--hashsize
>= 0) {
338 if (((struct dir_front
*)bh
->b_data
)->hashtable
[hashsize
])
345 affs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
347 struct inode
*inode
= dentry
->d_inode
;
350 struct buffer_head
*bh
;
352 pr_debug("AFFS: rmdir(dir=%lu,\"%.*s\")\n",dir
->i_ino
,
353 (int)dentry
->d_name
.len
,dentry
->d_name
.name
);
356 if (!(bh
= affs_find_entry(dir
,dentry
,&ino
)))
360 * Make sure the directory is empty and the dentry isn't busy.
363 if (!empty_dir(bh
,AFFS_I2HSIZE(inode
)))
366 if (!list_empty(&dentry
->d_hash
))
369 if ((retval
= affs_remove_header(bh
,inode
)) < 0)
372 inode
->i_nlink
= retval
;
373 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
375 dir
->i_version
= ++event
;
376 mark_inode_dirty(dir
);
377 mark_inode_dirty(inode
);
386 affs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
388 struct buffer_head
*bh
;
392 int i
, maxlen
, error
;
395 pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")\n",dir
->i_ino
,
396 (int)dentry
->d_name
.len
,dentry
->d_name
.name
,symname
);
398 maxlen
= 4 * AFFS_I2HSIZE(dir
) - 1;
400 inode
= affs_new_inode(dir
);
404 inode
->i_op
= &affs_symlink_inode_operations
;
405 inode
->i_mode
= S_IFLNK
| 0777;
406 inode
->u
.affs_i
.i_protect
= mode_to_prot(inode
->i_mode
);
408 bh
= affs_bread(inode
->i_dev
,inode
->i_ino
,AFFS_I2BSIZE(inode
));
412 p
= ((struct slink_front
*)bh
->b_data
)->symname
;
414 if (*symname
== '/') {
415 while (*symname
== '/')
417 while (inode
->i_sb
->u
.affs_sb
.s_volume
[i
]) /* Cannot overflow */
418 *p
++ = inode
->i_sb
->u
.affs_sb
.s_volume
[i
++];
420 while (i
< maxlen
&& (c
= *symname
++)) {
421 if (c
== '.' && lc
== '/' && *symname
== '.' && symname
[1] == '/') {
426 } else if (c
== '.' && lc
== '/' && *symname
== '/') {
435 while (*symname
== '/')
439 mark_buffer_dirty(bh
,1);
441 mark_inode_dirty(inode
);
443 /* N.B. This test shouldn't be necessary ... dentry must be negative */
445 bh
= affs_find_entry(dir
,dentry
,&tmp
);
448 /* N.B. Shouldn't we add the entry before dirtying the buffer? */
449 error
= affs_add_entry(dir
,NULL
,inode
,dentry
,ST_SOFTLINK
);
452 d_instantiate(dentry
,inode
);
453 dir
->i_version
= ++event
;
454 mark_inode_dirty(dir
);
463 mark_inode_dirty(inode
);
469 affs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
471 struct inode
*oldinode
= old_dentry
->d_inode
;
473 struct buffer_head
*bh
;
477 pr_debug("AFFS: link(%lu,%lu,\"%.*s\")\n",oldinode
->i_ino
,dir
->i_ino
,
478 (int)dentry
->d_name
.len
,dentry
->d_name
.name
);
480 /* N.B. Do we need this test? The dentry must be negative ... */
481 bh
= affs_find_entry(dir
,dentry
,&i
);
486 if (oldinode
->u
.affs_i
.i_hlink
) { /* Cannot happen */
487 affs_warning(dir
->i_sb
,"link","Impossible link to link");
491 if (!(inode
= affs_new_inode(dir
)))
494 inode
->i_op
= oldinode
->i_op
;
495 inode
->u
.affs_i
.i_protect
= mode_to_prot(oldinode
->i_mode
);
496 inode
->u
.affs_i
.i_original
= oldinode
->i_ino
;
497 inode
->u
.affs_i
.i_hlink
= 1;
498 inode
->i_mtime
= oldinode
->i_mtime
;
500 if (S_ISDIR(oldinode
->i_mode
))
501 error
= affs_add_entry(dir
,oldinode
,inode
,dentry
,ST_LINKDIR
);
503 error
= affs_add_entry(dir
,oldinode
,inode
,dentry
,ST_LINKFILE
);
507 dir
->i_version
= ++event
;
508 mark_inode_dirty(dir
);
509 mark_inode_dirty(oldinode
);
511 d_instantiate(dentry
,oldinode
);
513 mark_inode_dirty(inode
);
521 affs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
522 struct inode
*new_dir
, struct dentry
*new_dentry
)
524 struct inode
*old_inode
= old_dentry
->d_inode
;
525 struct inode
*new_inode
= new_dentry
->d_inode
;
526 struct buffer_head
*old_bh
;
527 struct buffer_head
*new_bh
;
528 unsigned long old_ino
;
529 unsigned long new_ino
;
532 pr_debug("AFFS: rename(old=%lu,\"%*s\" (inode=%p) to new=%lu,\"%*s\" (inode=%p))\n",
533 old_dir
->i_ino
,old_dentry
->d_name
.len
,old_dentry
->d_name
.name
,old_inode
,
534 new_dir
->i_ino
,new_dentry
->d_name
.len
,new_dentry
->d_name
.name
,new_inode
);
536 if ((retval
= affs_check_name(new_dentry
->d_name
.name
,new_dentry
->d_name
.len
)))
541 old_bh
= affs_find_entry(old_dir
,old_dentry
,&old_ino
);
545 new_bh
= affs_find_entry(new_dir
,new_dentry
,&new_ino
);
546 if (new_bh
&& !new_inode
) {
547 affs_error(old_inode
->i_sb
,"affs_rename",
548 "No inode for entry found (key=%lu)\n",new_ino
);
551 if (S_ISDIR(old_inode
->i_mode
)) {
554 if (!empty_dir(new_bh
,AFFS_I2HSIZE(new_inode
)))
559 if (affs_parent_ino(old_inode
) != old_dir
->i_ino
)
562 /* Unlink destination if it already exists */
564 if ((retval
= affs_remove_header(new_bh
,new_dir
)) < 0)
566 new_inode
->i_nlink
= retval
;
567 mark_inode_dirty(new_inode
);
568 if (new_inode
->i_ino
== new_ino
)
569 new_inode
->i_nlink
= 0;
571 /* Remove header from its parent directory. */
572 if ((retval
= affs_remove_hash(old_bh
,old_dir
)))
574 /* And insert it into the new directory with the new name. */
575 affs_copy_name(FILE_END(old_bh
->b_data
,old_inode
)->file_name
,new_dentry
->d_name
.name
);
576 if ((retval
= affs_insert_hash(new_dir
->i_ino
,old_bh
,new_dir
)))
578 affs_fix_checksum(AFFS_I2BSIZE(new_dir
),old_bh
->b_data
,5);
580 new_dir
->i_ctime
= new_dir
->i_mtime
= old_dir
->i_ctime
581 = old_dir
->i_mtime
= CURRENT_TIME
;
582 new_dir
->i_version
= ++event
;
583 old_dir
->i_version
= ++event
;
585 mark_inode_dirty(new_dir
);
586 mark_inode_dirty(old_dir
);
587 mark_buffer_dirty(old_bh
,1);