2 * linux/fs/msdos/namei.c
4 * Written 1992,1993 by Werner Almesberger
5 * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
6 * Rewritten for constant inumbers 1999 by Al Viro
9 #include <linux/module.h>
12 /* Characters that are undesirable in an MS-DOS file name */
13 static unsigned char bad_chars
[] = "*?<>|\"";
14 static unsigned char bad_if_strict
[] = "+=,; ";
16 /***** Formats an MS-DOS file name. Rejects invalid names. */
17 static int msdos_format_name(const unsigned char *name
, int len
,
18 unsigned char *res
, struct fat_mount_options
*opts
)
20 * name is the proposed name, len is its length, res is
21 * the resulting name, opts->name_check is either (r)elaxed,
22 * (n)ormal or (s)trict, opts->dotsOK allows dots at the
23 * beginning of name (for hidden files)
30 if (name
[0] == '.') { /* dotfile because . and .. already done */
32 /* Get rid of dot - test for it elsewhere */
39 * disallow names that _really_ start with a dot
43 for (walk
= res
; len
&& walk
- res
< 8; walk
++) {
46 if (opts
->name_check
!= 'r' && strchr(bad_chars
, c
))
48 if (opts
->name_check
== 's' && strchr(bad_if_strict
, c
))
50 if (c
>= 'A' && c
<= 'Z' && opts
->name_check
== 's')
52 if (c
< ' ' || c
== ':' || c
== '\\')
55 * 0xE5 is legal as a first character, but we must substitute
56 * 0x05 because 0xE5 marks deleted files. Yes, DOS really
58 * It seems that Microsoft hacked DOS to support non-US
59 * characters after the 0xE5 character was already in use to
62 if ((res
== walk
) && (c
== 0xE5))
67 *walk
= (!opts
->nocase
&& c
>= 'a' && c
<= 'z') ? c
- 32 : c
;
71 if (opts
->name_check
== 's' && len
&& c
!= '.') {
77 while (c
!= '.' && len
--)
80 while (walk
- res
< 8)
82 while (len
> 0 && walk
- res
< MSDOS_NAME
) {
85 if (opts
->name_check
!= 'r' && strchr(bad_chars
, c
))
87 if (opts
->name_check
== 's' &&
88 strchr(bad_if_strict
, c
))
90 if (c
< ' ' || c
== ':' || c
== '\\')
93 if (opts
->name_check
== 's')
97 if (c
>= 'A' && c
<= 'Z' && opts
->name_check
== 's')
100 if (!opts
->nocase
&& c
>= 'a' && c
<= 'z')
107 if (opts
->name_check
== 's' && len
)
110 while (walk
- res
< MSDOS_NAME
)
116 /***** Locates a directory entry. Uses unformatted name. */
117 static int msdos_find(struct inode
*dir
, const unsigned char *name
, int len
,
118 struct fat_slot_info
*sinfo
)
120 struct msdos_sb_info
*sbi
= MSDOS_SB(dir
->i_sb
);
121 unsigned char msdos_name
[MSDOS_NAME
];
124 err
= msdos_format_name(name
, len
, msdos_name
, &sbi
->options
);
128 err
= fat_scan(dir
, msdos_name
, sinfo
);
129 if (!err
&& sbi
->options
.dotsOK
) {
130 if (name
[0] == '.') {
131 if (!(sinfo
->de
->attr
& ATTR_HIDDEN
))
134 if (sinfo
->de
->attr
& ATTR_HIDDEN
)
144 * Compute the hash for the msdos name corresponding to the dentry.
145 * Note: if the name is invalid, we leave the hash code unchanged so
146 * that the existing dentry can be used. The msdos fs routines will
147 * return ENOENT or EINVAL as appropriate.
149 static int msdos_hash(const struct dentry
*dentry
, struct qstr
*qstr
)
151 struct fat_mount_options
*options
= &MSDOS_SB(dentry
->d_sb
)->options
;
152 unsigned char msdos_name
[MSDOS_NAME
];
155 error
= msdos_format_name(qstr
->name
, qstr
->len
, msdos_name
, options
);
157 qstr
->hash
= full_name_hash(msdos_name
, MSDOS_NAME
);
162 * Compare two msdos names. If either of the names are invalid,
163 * we fall back to doing the standard name comparison.
165 static int msdos_cmp(const struct dentry
*parent
, const struct dentry
*dentry
,
166 unsigned int len
, const char *str
, const struct qstr
*name
)
168 struct fat_mount_options
*options
= &MSDOS_SB(parent
->d_sb
)->options
;
169 unsigned char a_msdos_name
[MSDOS_NAME
], b_msdos_name
[MSDOS_NAME
];
172 error
= msdos_format_name(name
->name
, name
->len
, a_msdos_name
, options
);
175 error
= msdos_format_name(str
, len
, b_msdos_name
, options
);
178 error
= memcmp(a_msdos_name
, b_msdos_name
, MSDOS_NAME
);
184 if (name
->len
== len
)
185 error
= memcmp(name
->name
, str
, len
);
189 static const struct dentry_operations msdos_dentry_operations
= {
190 .d_hash
= msdos_hash
,
191 .d_compare
= msdos_cmp
,
195 * AV. Wrappers for FAT sb operations. Is it wise?
198 /***** Get inode using directory and name */
199 static struct dentry
*msdos_lookup(struct inode
*dir
, struct dentry
*dentry
,
202 struct super_block
*sb
= dir
->i_sb
;
203 struct fat_slot_info sinfo
;
207 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
208 err
= msdos_find(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &sinfo
);
214 inode
= fat_build_inode(sb
, sinfo
.de
, sinfo
.i_pos
);
218 inode
= ERR_PTR(err
);
220 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
221 return d_splice_alias(inode
, dentry
);
224 /***** Creates a directory entry (name is already formatted). */
225 static int msdos_add_entry(struct inode
*dir
, const unsigned char *name
,
226 int is_dir
, int is_hid
, int cluster
,
227 struct timespec
*ts
, struct fat_slot_info
*sinfo
)
229 struct msdos_sb_info
*sbi
= MSDOS_SB(dir
->i_sb
);
230 struct msdos_dir_entry de
;
234 memcpy(de
.name
, name
, MSDOS_NAME
);
235 de
.attr
= is_dir
? ATTR_DIR
: ATTR_ARCH
;
237 de
.attr
|= ATTR_HIDDEN
;
239 fat_time_unix2fat(sbi
, ts
, &time
, &date
, NULL
);
240 de
.cdate
= de
.adate
= 0;
245 fat_set_start(&de
, cluster
);
248 err
= fat_add_entries(dir
, &de
, 1, sinfo
);
252 dir
->i_ctime
= dir
->i_mtime
= *ts
;
254 (void)fat_sync_inode(dir
);
256 mark_inode_dirty(dir
);
261 /***** Create a file */
262 static int msdos_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
265 struct super_block
*sb
= dir
->i_sb
;
266 struct inode
*inode
= NULL
;
267 struct fat_slot_info sinfo
;
269 unsigned char msdos_name
[MSDOS_NAME
];
272 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
274 err
= msdos_format_name(dentry
->d_name
.name
, dentry
->d_name
.len
,
275 msdos_name
, &MSDOS_SB(sb
)->options
);
278 is_hid
= (dentry
->d_name
.name
[0] == '.') && (msdos_name
[0] != '.');
279 /* Have to do it due to foo vs. .foo conflicts */
280 if (!fat_scan(dir
, msdos_name
, &sinfo
)) {
286 ts
= CURRENT_TIME_SEC
;
287 err
= msdos_add_entry(dir
, msdos_name
, 0, is_hid
, 0, &ts
, &sinfo
);
290 inode
= fat_build_inode(sb
, sinfo
.de
, sinfo
.i_pos
);
293 err
= PTR_ERR(inode
);
296 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ts
;
297 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
299 d_instantiate(dentry
, inode
);
301 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
303 err
= fat_flush_inodes(sb
, dir
, inode
);
307 /***** Remove a directory */
308 static int msdos_rmdir(struct inode
*dir
, struct dentry
*dentry
)
310 struct super_block
*sb
= dir
->i_sb
;
311 struct inode
*inode
= d_inode(dentry
);
312 struct fat_slot_info sinfo
;
315 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
317 * Check whether the directory is not in use, then check
318 * whether it is empty.
320 err
= fat_dir_empty(inode
);
323 err
= msdos_find(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &sinfo
);
327 err
= fat_remove_entries(dir
, &sinfo
); /* and releases bh */
333 inode
->i_ctime
= CURRENT_TIME_SEC
;
336 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
338 err
= fat_flush_inodes(sb
, dir
, inode
);
343 /***** Make a directory */
344 static int msdos_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
346 struct super_block
*sb
= dir
->i_sb
;
347 struct fat_slot_info sinfo
;
349 unsigned char msdos_name
[MSDOS_NAME
];
351 int err
, is_hid
, cluster
;
353 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
355 err
= msdos_format_name(dentry
->d_name
.name
, dentry
->d_name
.len
,
356 msdos_name
, &MSDOS_SB(sb
)->options
);
359 is_hid
= (dentry
->d_name
.name
[0] == '.') && (msdos_name
[0] != '.');
360 /* foo vs .foo situation */
361 if (!fat_scan(dir
, msdos_name
, &sinfo
)) {
367 ts
= CURRENT_TIME_SEC
;
368 cluster
= fat_alloc_new_dir(dir
, &ts
);
373 err
= msdos_add_entry(dir
, msdos_name
, 1, is_hid
, cluster
, &ts
, &sinfo
);
378 inode
= fat_build_inode(sb
, sinfo
.de
, sinfo
.i_pos
);
381 err
= PTR_ERR(inode
);
382 /* the directory was completed, just return a error */
386 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ts
;
387 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
389 d_instantiate(dentry
, inode
);
391 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
392 fat_flush_inodes(sb
, dir
, inode
);
396 fat_free_clusters(dir
, cluster
);
398 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
402 /***** Unlink a file */
403 static int msdos_unlink(struct inode
*dir
, struct dentry
*dentry
)
405 struct inode
*inode
= d_inode(dentry
);
406 struct super_block
*sb
= inode
->i_sb
;
407 struct fat_slot_info sinfo
;
410 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
411 err
= msdos_find(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &sinfo
);
415 err
= fat_remove_entries(dir
, &sinfo
); /* and releases bh */
419 inode
->i_ctime
= CURRENT_TIME_SEC
;
422 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
424 err
= fat_flush_inodes(sb
, dir
, inode
);
429 static int do_msdos_rename(struct inode
*old_dir
, unsigned char *old_name
,
430 struct dentry
*old_dentry
,
431 struct inode
*new_dir
, unsigned char *new_name
,
432 struct dentry
*new_dentry
, int is_hid
)
434 struct buffer_head
*dotdot_bh
;
435 struct msdos_dir_entry
*dotdot_de
;
436 struct inode
*old_inode
, *new_inode
;
437 struct fat_slot_info old_sinfo
, sinfo
;
440 int err
, old_attrs
, is_dir
, update_dotdot
, corrupt
= 0;
442 old_sinfo
.bh
= sinfo
.bh
= dotdot_bh
= NULL
;
443 old_inode
= d_inode(old_dentry
);
444 new_inode
= d_inode(new_dentry
);
446 err
= fat_scan(old_dir
, old_name
, &old_sinfo
);
452 is_dir
= S_ISDIR(old_inode
->i_mode
);
453 update_dotdot
= (is_dir
&& old_dir
!= new_dir
);
455 if (fat_get_dotdot_entry(old_inode
, &dotdot_bh
, &dotdot_de
)) {
461 old_attrs
= MSDOS_I(old_inode
)->i_attrs
;
462 err
= fat_scan(new_dir
, new_name
, &sinfo
);
465 /* "foo" -> ".foo" case. just change the ATTR_HIDDEN */
466 if (sinfo
.de
!= old_sinfo
.de
) {
471 MSDOS_I(old_inode
)->i_attrs
|= ATTR_HIDDEN
;
473 MSDOS_I(old_inode
)->i_attrs
&= ~ATTR_HIDDEN
;
474 if (IS_DIRSYNC(old_dir
)) {
475 err
= fat_sync_inode(old_inode
);
477 MSDOS_I(old_inode
)->i_attrs
= old_attrs
;
481 mark_inode_dirty(old_inode
);
483 old_dir
->i_version
++;
484 old_dir
->i_ctime
= old_dir
->i_mtime
= CURRENT_TIME_SEC
;
485 if (IS_DIRSYNC(old_dir
))
486 (void)fat_sync_inode(old_dir
);
488 mark_inode_dirty(old_dir
);
493 ts
= CURRENT_TIME_SEC
;
498 err
= fat_dir_empty(new_inode
);
502 new_i_pos
= MSDOS_I(new_inode
)->i_pos
;
503 fat_detach(new_inode
);
505 err
= msdos_add_entry(new_dir
, new_name
, is_dir
, is_hid
, 0,
509 new_i_pos
= sinfo
.i_pos
;
511 new_dir
->i_version
++;
513 fat_detach(old_inode
);
514 fat_attach(old_inode
, new_i_pos
);
516 MSDOS_I(old_inode
)->i_attrs
|= ATTR_HIDDEN
;
518 MSDOS_I(old_inode
)->i_attrs
&= ~ATTR_HIDDEN
;
519 if (IS_DIRSYNC(new_dir
)) {
520 err
= fat_sync_inode(old_inode
);
524 mark_inode_dirty(old_inode
);
527 fat_set_start(dotdot_de
, MSDOS_I(new_dir
)->i_logstart
);
528 mark_buffer_dirty_inode(dotdot_bh
, old_inode
);
529 if (IS_DIRSYNC(new_dir
)) {
530 err
= sync_dirty_buffer(dotdot_bh
);
539 err
= fat_remove_entries(old_dir
, &old_sinfo
); /* and releases bh */
543 old_dir
->i_version
++;
544 old_dir
->i_ctime
= old_dir
->i_mtime
= ts
;
545 if (IS_DIRSYNC(old_dir
))
546 (void)fat_sync_inode(old_dir
);
548 mark_inode_dirty(old_dir
);
551 drop_nlink(new_inode
);
553 drop_nlink(new_inode
);
554 new_inode
->i_ctime
= ts
;
559 brelse(old_sinfo
.bh
);
563 /* data cluster is shared, serious corruption */
567 fat_set_start(dotdot_de
, MSDOS_I(old_dir
)->i_logstart
);
568 mark_buffer_dirty_inode(dotdot_bh
, old_inode
);
569 corrupt
|= sync_dirty_buffer(dotdot_bh
);
572 fat_detach(old_inode
);
573 fat_attach(old_inode
, old_sinfo
.i_pos
);
574 MSDOS_I(old_inode
)->i_attrs
= old_attrs
;
576 fat_attach(new_inode
, new_i_pos
);
578 corrupt
|= fat_sync_inode(new_inode
);
581 * If new entry was not sharing the data cluster, it
582 * shouldn't be serious corruption.
584 int err2
= fat_remove_entries(new_dir
, &sinfo
);
590 fat_fs_error(new_dir
->i_sb
,
591 "%s: Filesystem corrupted (i_pos %lld)",
592 __func__
, sinfo
.i_pos
);
597 /***** Rename, a wrapper for rename_same_dir & rename_diff_dir */
598 static int msdos_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
599 struct inode
*new_dir
, struct dentry
*new_dentry
)
601 struct super_block
*sb
= old_dir
->i_sb
;
602 unsigned char old_msdos_name
[MSDOS_NAME
], new_msdos_name
[MSDOS_NAME
];
605 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
607 err
= msdos_format_name(old_dentry
->d_name
.name
,
608 old_dentry
->d_name
.len
, old_msdos_name
,
609 &MSDOS_SB(old_dir
->i_sb
)->options
);
612 err
= msdos_format_name(new_dentry
->d_name
.name
,
613 new_dentry
->d_name
.len
, new_msdos_name
,
614 &MSDOS_SB(new_dir
->i_sb
)->options
);
619 (new_dentry
->d_name
.name
[0] == '.') && (new_msdos_name
[0] != '.');
621 err
= do_msdos_rename(old_dir
, old_msdos_name
, old_dentry
,
622 new_dir
, new_msdos_name
, new_dentry
, is_hid
);
624 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
626 err
= fat_flush_inodes(sb
, old_dir
, new_dir
);
630 static const struct inode_operations msdos_dir_inode_operations
= {
631 .create
= msdos_create
,
632 .lookup
= msdos_lookup
,
633 .unlink
= msdos_unlink
,
634 .mkdir
= msdos_mkdir
,
635 .rmdir
= msdos_rmdir
,
636 .rename
= msdos_rename
,
637 .setattr
= fat_setattr
,
638 .getattr
= fat_getattr
,
641 static void setup(struct super_block
*sb
)
643 MSDOS_SB(sb
)->dir_ops
= &msdos_dir_inode_operations
;
644 sb
->s_d_op
= &msdos_dentry_operations
;
645 sb
->s_flags
|= MS_NOATIME
;
648 static int msdos_fill_super(struct super_block
*sb
, void *data
, int silent
)
650 return fat_fill_super(sb
, data
, silent
, 0, setup
);
653 static struct dentry
*msdos_mount(struct file_system_type
*fs_type
,
654 int flags
, const char *dev_name
,
657 return mount_bdev(fs_type
, flags
, dev_name
, data
, msdos_fill_super
);
660 static struct file_system_type msdos_fs_type
= {
661 .owner
= THIS_MODULE
,
663 .mount
= msdos_mount
,
664 .kill_sb
= kill_block_super
,
665 .fs_flags
= FS_REQUIRES_DEV
,
667 MODULE_ALIAS_FS("msdos");
669 static int __init
init_msdos_fs(void)
671 return register_filesystem(&msdos_fs_type
);
674 static void __exit
exit_msdos_fs(void)
676 unregister_filesystem(&msdos_fs_type
);
679 MODULE_LICENSE("GPL");
680 MODULE_AUTHOR("Werner Almesberger");
681 MODULE_DESCRIPTION("MS-DOS filesystem support");
683 module_init(init_msdos_fs
)
684 module_exit(exit_msdos_fs
)