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>
10 #include <linux/iversion.h>
13 /* Characters that are undesirable in an MS-DOS file name */
14 static unsigned char bad_chars
[] = "*?<>|\"";
15 static unsigned char bad_if_strict
[] = "+=,; ";
17 /***** Formats an MS-DOS file name. Rejects invalid names. */
18 static int msdos_format_name(const unsigned char *name
, int len
,
19 unsigned char *res
, struct fat_mount_options
*opts
)
21 * name is the proposed name, len is its length, res is
22 * the resulting name, opts->name_check is either (r)elaxed,
23 * (n)ormal or (s)trict, opts->dotsOK allows dots at the
24 * beginning of name (for hidden files)
31 if (name
[0] == '.') { /* dotfile because . and .. already done */
33 /* Get rid of dot - test for it elsewhere */
40 * disallow names that _really_ start with a dot
44 for (walk
= res
; len
&& walk
- res
< 8; walk
++) {
47 if (opts
->name_check
!= 'r' && strchr(bad_chars
, c
))
49 if (opts
->name_check
== 's' && strchr(bad_if_strict
, c
))
51 if (c
>= 'A' && c
<= 'Z' && opts
->name_check
== 's')
53 if (c
< ' ' || c
== ':' || c
== '\\')
56 * 0xE5 is legal as a first character, but we must substitute
57 * 0x05 because 0xE5 marks deleted files. Yes, DOS really
59 * It seems that Microsoft hacked DOS to support non-US
60 * characters after the 0xE5 character was already in use to
63 if ((res
== walk
) && (c
== 0xE5))
68 *walk
= (!opts
->nocase
&& c
>= 'a' && c
<= 'z') ? c
- 32 : c
;
72 if (opts
->name_check
== 's' && len
&& c
!= '.') {
78 while (c
!= '.' && len
--)
81 while (walk
- res
< 8)
83 while (len
> 0 && walk
- res
< MSDOS_NAME
) {
86 if (opts
->name_check
!= 'r' && strchr(bad_chars
, c
))
88 if (opts
->name_check
== 's' &&
89 strchr(bad_if_strict
, c
))
91 if (c
< ' ' || c
== ':' || c
== '\\')
94 if (opts
->name_check
== 's')
98 if (c
>= 'A' && c
<= 'Z' && opts
->name_check
== 's')
101 if (!opts
->nocase
&& c
>= 'a' && c
<= 'z')
108 if (opts
->name_check
== 's' && len
)
111 while (walk
- res
< MSDOS_NAME
)
117 /***** Locates a directory entry. Uses unformatted name. */
118 static int msdos_find(struct inode
*dir
, const unsigned char *name
, int len
,
119 struct fat_slot_info
*sinfo
)
121 struct msdos_sb_info
*sbi
= MSDOS_SB(dir
->i_sb
);
122 unsigned char msdos_name
[MSDOS_NAME
];
125 err
= msdos_format_name(name
, len
, msdos_name
, &sbi
->options
);
129 err
= fat_scan(dir
, msdos_name
, sinfo
);
130 if (!err
&& sbi
->options
.dotsOK
) {
131 if (name
[0] == '.') {
132 if (!(sinfo
->de
->attr
& ATTR_HIDDEN
))
135 if (sinfo
->de
->attr
& ATTR_HIDDEN
)
145 * Compute the hash for the msdos name corresponding to the dentry.
146 * Note: if the name is invalid, we leave the hash code unchanged so
147 * that the existing dentry can be used. The msdos fs routines will
148 * return ENOENT or EINVAL as appropriate.
150 static int msdos_hash(const struct dentry
*dentry
, struct qstr
*qstr
)
152 struct fat_mount_options
*options
= &MSDOS_SB(dentry
->d_sb
)->options
;
153 unsigned char msdos_name
[MSDOS_NAME
];
156 error
= msdos_format_name(qstr
->name
, qstr
->len
, msdos_name
, options
);
158 qstr
->hash
= full_name_hash(dentry
, msdos_name
, MSDOS_NAME
);
163 * Compare two msdos names. If either of the names are invalid,
164 * we fall back to doing the standard name comparison.
166 static int msdos_cmp(const struct dentry
*dentry
,
167 unsigned int len
, const char *str
, const struct qstr
*name
)
169 struct fat_mount_options
*options
= &MSDOS_SB(dentry
->d_sb
)->options
;
170 unsigned char a_msdos_name
[MSDOS_NAME
], b_msdos_name
[MSDOS_NAME
];
173 error
= msdos_format_name(name
->name
, name
->len
, a_msdos_name
, options
);
176 error
= msdos_format_name(str
, len
, b_msdos_name
, options
);
179 error
= memcmp(a_msdos_name
, b_msdos_name
, MSDOS_NAME
);
185 if (name
->len
== len
)
186 error
= memcmp(name
->name
, str
, len
);
190 static const struct dentry_operations msdos_dentry_operations
= {
191 .d_hash
= msdos_hash
,
192 .d_compare
= msdos_cmp
,
196 * AV. Wrappers for FAT sb operations. Is it wise?
199 /***** Get inode using directory and name */
200 static struct dentry
*msdos_lookup(struct inode
*dir
, struct dentry
*dentry
,
203 struct super_block
*sb
= dir
->i_sb
;
204 struct fat_slot_info sinfo
;
208 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
209 err
= msdos_find(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &sinfo
);
215 inode
= fat_build_inode(sb
, sinfo
.de
, sinfo
.i_pos
);
219 inode
= ERR_PTR(err
);
221 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
222 return d_splice_alias(inode
, dentry
);
225 /***** Creates a directory entry (name is already formatted). */
226 static int msdos_add_entry(struct inode
*dir
, const unsigned char *name
,
227 int is_dir
, int is_hid
, int cluster
,
228 struct timespec
*ts
, struct fat_slot_info
*sinfo
)
230 struct msdos_sb_info
*sbi
= MSDOS_SB(dir
->i_sb
);
231 struct msdos_dir_entry de
;
235 memcpy(de
.name
, name
, MSDOS_NAME
);
236 de
.attr
= is_dir
? ATTR_DIR
: ATTR_ARCH
;
238 de
.attr
|= ATTR_HIDDEN
;
240 fat_time_unix2fat(sbi
, ts
, &time
, &date
, NULL
);
241 de
.cdate
= de
.adate
= 0;
246 fat_set_start(&de
, cluster
);
249 err
= fat_add_entries(dir
, &de
, 1, sinfo
);
253 dir
->i_ctime
= dir
->i_mtime
= timespec_to_timespec64(*ts
);
255 (void)fat_sync_inode(dir
);
257 mark_inode_dirty(dir
);
262 /***** Create a file */
263 static int msdos_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
266 struct super_block
*sb
= dir
->i_sb
;
267 struct inode
*inode
= NULL
;
268 struct fat_slot_info sinfo
;
269 struct timespec64 ts
;
271 unsigned char msdos_name
[MSDOS_NAME
];
274 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
276 err
= msdos_format_name(dentry
->d_name
.name
, dentry
->d_name
.len
,
277 msdos_name
, &MSDOS_SB(sb
)->options
);
280 is_hid
= (dentry
->d_name
.name
[0] == '.') && (msdos_name
[0] != '.');
281 /* Have to do it due to foo vs. .foo conflicts */
282 if (!fat_scan(dir
, msdos_name
, &sinfo
)) {
288 ts
= current_time(dir
);
289 t
= timespec64_to_timespec(ts
);
290 err
= msdos_add_entry(dir
, msdos_name
, 0, is_hid
, 0, &t
, &sinfo
);
293 inode
= fat_build_inode(sb
, sinfo
.de
, sinfo
.i_pos
);
296 err
= PTR_ERR(inode
);
299 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ts
;
300 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
302 d_instantiate(dentry
, inode
);
304 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
306 err
= fat_flush_inodes(sb
, dir
, inode
);
310 /***** Remove a directory */
311 static int msdos_rmdir(struct inode
*dir
, struct dentry
*dentry
)
313 struct super_block
*sb
= dir
->i_sb
;
314 struct inode
*inode
= d_inode(dentry
);
315 struct fat_slot_info sinfo
;
318 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
319 err
= fat_dir_empty(inode
);
322 err
= msdos_find(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &sinfo
);
326 err
= fat_remove_entries(dir
, &sinfo
); /* and releases bh */
332 inode
->i_ctime
= current_time(inode
);
335 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
337 err
= fat_flush_inodes(sb
, dir
, inode
);
342 /***** Make a directory */
343 static int msdos_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
345 struct super_block
*sb
= dir
->i_sb
;
346 struct fat_slot_info sinfo
;
348 unsigned char msdos_name
[MSDOS_NAME
];
349 struct timespec64 ts
;
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(dir
);
368 t
= timespec64_to_timespec(ts
);
369 cluster
= fat_alloc_new_dir(dir
, &t
);
374 err
= msdos_add_entry(dir
, msdos_name
, 1, is_hid
, cluster
, &t
, &sinfo
);
379 inode
= fat_build_inode(sb
, sinfo
.de
, sinfo
.i_pos
);
382 err
= PTR_ERR(inode
);
383 /* the directory was completed, just return a error */
387 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= ts
;
388 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
390 d_instantiate(dentry
, inode
);
392 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
393 fat_flush_inodes(sb
, dir
, inode
);
397 fat_free_clusters(dir
, cluster
);
399 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
403 /***** Unlink a file */
404 static int msdos_unlink(struct inode
*dir
, struct dentry
*dentry
)
406 struct inode
*inode
= d_inode(dentry
);
407 struct super_block
*sb
= inode
->i_sb
;
408 struct fat_slot_info sinfo
;
411 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
412 err
= msdos_find(dir
, dentry
->d_name
.name
, dentry
->d_name
.len
, &sinfo
);
416 err
= fat_remove_entries(dir
, &sinfo
); /* and releases bh */
420 inode
->i_ctime
= current_time(inode
);
423 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
425 err
= fat_flush_inodes(sb
, dir
, inode
);
430 static int do_msdos_rename(struct inode
*old_dir
, unsigned char *old_name
,
431 struct dentry
*old_dentry
,
432 struct inode
*new_dir
, unsigned char *new_name
,
433 struct dentry
*new_dentry
, int is_hid
)
435 struct buffer_head
*dotdot_bh
;
436 struct msdos_dir_entry
*dotdot_de
;
437 struct inode
*old_inode
, *new_inode
;
438 struct fat_slot_info old_sinfo
, sinfo
;
439 struct timespec64 ts
;
441 int err
, old_attrs
, is_dir
, update_dotdot
, corrupt
= 0;
443 old_sinfo
.bh
= sinfo
.bh
= dotdot_bh
= NULL
;
444 old_inode
= d_inode(old_dentry
);
445 new_inode
= d_inode(new_dentry
);
447 err
= fat_scan(old_dir
, old_name
, &old_sinfo
);
453 is_dir
= S_ISDIR(old_inode
->i_mode
);
454 update_dotdot
= (is_dir
&& old_dir
!= new_dir
);
456 if (fat_get_dotdot_entry(old_inode
, &dotdot_bh
, &dotdot_de
)) {
462 old_attrs
= MSDOS_I(old_inode
)->i_attrs
;
463 err
= fat_scan(new_dir
, new_name
, &sinfo
);
466 /* "foo" -> ".foo" case. just change the ATTR_HIDDEN */
467 if (sinfo
.de
!= old_sinfo
.de
) {
472 MSDOS_I(old_inode
)->i_attrs
|= ATTR_HIDDEN
;
474 MSDOS_I(old_inode
)->i_attrs
&= ~ATTR_HIDDEN
;
475 if (IS_DIRSYNC(old_dir
)) {
476 err
= fat_sync_inode(old_inode
);
478 MSDOS_I(old_inode
)->i_attrs
= old_attrs
;
482 mark_inode_dirty(old_inode
);
484 inode_inc_iversion(old_dir
);
485 old_dir
->i_ctime
= old_dir
->i_mtime
= current_time(old_dir
);
486 if (IS_DIRSYNC(old_dir
))
487 (void)fat_sync_inode(old_dir
);
489 mark_inode_dirty(old_dir
);
494 ts
= current_time(old_inode
);
499 err
= fat_dir_empty(new_inode
);
503 new_i_pos
= MSDOS_I(new_inode
)->i_pos
;
504 fat_detach(new_inode
);
506 struct timespec t
= timespec64_to_timespec(ts
);
507 err
= msdos_add_entry(new_dir
, new_name
, is_dir
, is_hid
, 0,
511 new_i_pos
= sinfo
.i_pos
;
513 inode_inc_iversion(new_dir
);
515 fat_detach(old_inode
);
516 fat_attach(old_inode
, new_i_pos
);
518 MSDOS_I(old_inode
)->i_attrs
|= ATTR_HIDDEN
;
520 MSDOS_I(old_inode
)->i_attrs
&= ~ATTR_HIDDEN
;
521 if (IS_DIRSYNC(new_dir
)) {
522 err
= fat_sync_inode(old_inode
);
526 mark_inode_dirty(old_inode
);
529 fat_set_start(dotdot_de
, MSDOS_I(new_dir
)->i_logstart
);
530 mark_buffer_dirty_inode(dotdot_bh
, old_inode
);
531 if (IS_DIRSYNC(new_dir
)) {
532 err
= sync_dirty_buffer(dotdot_bh
);
541 err
= fat_remove_entries(old_dir
, &old_sinfo
); /* and releases bh */
545 inode_inc_iversion(old_dir
);
546 old_dir
->i_ctime
= old_dir
->i_mtime
= ts
;
547 if (IS_DIRSYNC(old_dir
))
548 (void)fat_sync_inode(old_dir
);
550 mark_inode_dirty(old_dir
);
553 drop_nlink(new_inode
);
555 drop_nlink(new_inode
);
556 new_inode
->i_ctime
= ts
;
561 brelse(old_sinfo
.bh
);
565 /* data cluster is shared, serious corruption */
569 fat_set_start(dotdot_de
, MSDOS_I(old_dir
)->i_logstart
);
570 mark_buffer_dirty_inode(dotdot_bh
, old_inode
);
571 corrupt
|= sync_dirty_buffer(dotdot_bh
);
574 fat_detach(old_inode
);
575 fat_attach(old_inode
, old_sinfo
.i_pos
);
576 MSDOS_I(old_inode
)->i_attrs
= old_attrs
;
578 fat_attach(new_inode
, new_i_pos
);
580 corrupt
|= fat_sync_inode(new_inode
);
583 * If new entry was not sharing the data cluster, it
584 * shouldn't be serious corruption.
586 int err2
= fat_remove_entries(new_dir
, &sinfo
);
592 fat_fs_error(new_dir
->i_sb
,
593 "%s: Filesystem corrupted (i_pos %lld)",
594 __func__
, sinfo
.i_pos
);
599 /***** Rename, a wrapper for rename_same_dir & rename_diff_dir */
600 static int msdos_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
601 struct inode
*new_dir
, struct dentry
*new_dentry
,
604 struct super_block
*sb
= old_dir
->i_sb
;
605 unsigned char old_msdos_name
[MSDOS_NAME
], new_msdos_name
[MSDOS_NAME
];
608 if (flags
& ~RENAME_NOREPLACE
)
611 mutex_lock(&MSDOS_SB(sb
)->s_lock
);
613 err
= msdos_format_name(old_dentry
->d_name
.name
,
614 old_dentry
->d_name
.len
, old_msdos_name
,
615 &MSDOS_SB(old_dir
->i_sb
)->options
);
618 err
= msdos_format_name(new_dentry
->d_name
.name
,
619 new_dentry
->d_name
.len
, new_msdos_name
,
620 &MSDOS_SB(new_dir
->i_sb
)->options
);
625 (new_dentry
->d_name
.name
[0] == '.') && (new_msdos_name
[0] != '.');
627 err
= do_msdos_rename(old_dir
, old_msdos_name
, old_dentry
,
628 new_dir
, new_msdos_name
, new_dentry
, is_hid
);
630 mutex_unlock(&MSDOS_SB(sb
)->s_lock
);
632 err
= fat_flush_inodes(sb
, old_dir
, new_dir
);
636 static const struct inode_operations msdos_dir_inode_operations
= {
637 .create
= msdos_create
,
638 .lookup
= msdos_lookup
,
639 .unlink
= msdos_unlink
,
640 .mkdir
= msdos_mkdir
,
641 .rmdir
= msdos_rmdir
,
642 .rename
= msdos_rename
,
643 .setattr
= fat_setattr
,
644 .getattr
= fat_getattr
,
647 static void setup(struct super_block
*sb
)
649 MSDOS_SB(sb
)->dir_ops
= &msdos_dir_inode_operations
;
650 sb
->s_d_op
= &msdos_dentry_operations
;
651 sb
->s_flags
|= SB_NOATIME
;
654 static int msdos_fill_super(struct super_block
*sb
, void *data
, int silent
)
656 return fat_fill_super(sb
, data
, silent
, 0, setup
);
659 static struct dentry
*msdos_mount(struct file_system_type
*fs_type
,
660 int flags
, const char *dev_name
,
663 return mount_bdev(fs_type
, flags
, dev_name
, data
, msdos_fill_super
);
666 static struct file_system_type msdos_fs_type
= {
667 .owner
= THIS_MODULE
,
669 .mount
= msdos_mount
,
670 .kill_sb
= kill_block_super
,
671 .fs_flags
= FS_REQUIRES_DEV
,
673 MODULE_ALIAS_FS("msdos");
675 static int __init
init_msdos_fs(void)
677 return register_filesystem(&msdos_fs_type
);
680 static void __exit
exit_msdos_fs(void)
682 unregister_filesystem(&msdos_fs_type
);
685 MODULE_LICENSE("GPL");
686 MODULE_AUTHOR("Werner Almesberger");
687 MODULE_DESCRIPTION("MS-DOS filesystem support");
689 module_init(init_msdos_fs
)
690 module_exit(exit_msdos_fs
)