ALSA: Fix initiailization of user-space controls
[linux/fpc-iii.git] / fs / msdos / namei.c
blob626a367bcd81145cb6dd4385b098755b58bf2f38
1 /*
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
7 */
9 #include <linux/module.h>
10 #include <linux/time.h>
11 #include <linux/buffer_head.h>
12 #include <linux/msdos_fs.h>
13 #include <linux/smp_lock.h>
15 /* MS-DOS "device special files" */
16 static const unsigned char *reserved_names[] = {
17 "CON ", "PRN ", "NUL ", "AUX ",
18 "LPT1 ", "LPT2 ", "LPT3 ", "LPT4 ",
19 "COM1 ", "COM2 ", "COM3 ", "COM4 ",
20 NULL
23 /* Characters that are undesirable in an MS-DOS file name */
24 static unsigned char bad_chars[] = "*?<>|\"";
25 static unsigned char bad_if_strict_pc[] = "+=,; ";
26 /* GEMDOS is less restrictive */
27 static unsigned char bad_if_strict_atari[] = " ";
29 #define bad_if_strict(opts) \
30 ((opts)->atari ? bad_if_strict_atari : bad_if_strict_pc)
32 /***** Formats an MS-DOS file name. Rejects invalid names. */
33 static int msdos_format_name(const unsigned char *name, int len,
34 unsigned char *res, struct fat_mount_options *opts)
36 * name is the proposed name, len is its length, res is
37 * the resulting name, opts->name_check is either (r)elaxed,
38 * (n)ormal or (s)trict, opts->dotsOK allows dots at the
39 * beginning of name (for hidden files)
42 unsigned char *walk;
43 const unsigned char **reserved;
44 unsigned char c;
45 int space;
47 if (name[0] == '.') { /* dotfile because . and .. already done */
48 if (opts->dotsOK) {
49 /* Get rid of dot - test for it elsewhere */
50 name++;
51 len--;
52 } else if (!opts->atari)
53 return -EINVAL;
56 * disallow names that _really_ start with a dot for MS-DOS,
57 * GEMDOS does not care
59 space = !opts->atari;
60 c = 0;
61 for (walk = res; len && walk - res < 8; walk++) {
62 c = *name++;
63 len--;
64 if (opts->name_check != 'r' && strchr(bad_chars, c))
65 return -EINVAL;
66 if (opts->name_check == 's' && strchr(bad_if_strict(opts), c))
67 return -EINVAL;
68 if (c >= 'A' && c <= 'Z' && opts->name_check == 's')
69 return -EINVAL;
70 if (c < ' ' || c == ':' || c == '\\')
71 return -EINVAL;
73 * 0xE5 is legal as a first character, but we must substitute
74 * 0x05 because 0xE5 marks deleted files. Yes, DOS really
75 * does this.
76 * It seems that Microsoft hacked DOS to support non-US
77 * characters after the 0xE5 character was already in use to
78 * mark deleted files.
80 if ((res == walk) && (c == 0xE5))
81 c = 0x05;
82 if (c == '.')
83 break;
84 space = (c == ' ');
85 *walk = (!opts->nocase && c >= 'a' && c <= 'z') ? c - 32 : c;
87 if (space)
88 return -EINVAL;
89 if (opts->name_check == 's' && len && c != '.') {
90 c = *name++;
91 len--;
92 if (c != '.')
93 return -EINVAL;
95 while (c != '.' && len--)
96 c = *name++;
97 if (c == '.') {
98 while (walk - res < 8)
99 *walk++ = ' ';
100 while (len > 0 && walk - res < MSDOS_NAME) {
101 c = *name++;
102 len--;
103 if (opts->name_check != 'r' && strchr(bad_chars, c))
104 return -EINVAL;
105 if (opts->name_check == 's' &&
106 strchr(bad_if_strict(opts), c))
107 return -EINVAL;
108 if (c < ' ' || c == ':' || c == '\\')
109 return -EINVAL;
110 if (c == '.') {
111 if (opts->name_check == 's')
112 return -EINVAL;
113 break;
115 if (c >= 'A' && c <= 'Z' && opts->name_check == 's')
116 return -EINVAL;
117 space = c == ' ';
118 if (!opts->nocase && c >= 'a' && c <= 'z')
119 *walk++ = c - 32;
120 else
121 *walk++ = c;
123 if (space)
124 return -EINVAL;
125 if (opts->name_check == 's' && len)
126 return -EINVAL;
128 while (walk - res < MSDOS_NAME)
129 *walk++ = ' ';
130 if (!opts->atari)
131 /* GEMDOS is less stupid and has no reserved names */
132 for (reserved = reserved_names; *reserved; reserved++)
133 if (!strncmp(res, *reserved, 8))
134 return -EINVAL;
135 return 0;
138 /***** Locates a directory entry. Uses unformatted name. */
139 static int msdos_find(struct inode *dir, const unsigned char *name, int len,
140 struct fat_slot_info *sinfo)
142 struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
143 unsigned char msdos_name[MSDOS_NAME];
144 int err;
146 err = msdos_format_name(name, len, msdos_name, &sbi->options);
147 if (err)
148 return -ENOENT;
150 err = fat_scan(dir, msdos_name, sinfo);
151 if (!err && sbi->options.dotsOK) {
152 if (name[0] == '.') {
153 if (!(sinfo->de->attr & ATTR_HIDDEN))
154 err = -ENOENT;
155 } else {
156 if (sinfo->de->attr & ATTR_HIDDEN)
157 err = -ENOENT;
159 if (err)
160 brelse(sinfo->bh);
162 return err;
166 * Compute the hash for the msdos name corresponding to the dentry.
167 * Note: if the name is invalid, we leave the hash code unchanged so
168 * that the existing dentry can be used. The msdos fs routines will
169 * return ENOENT or EINVAL as appropriate.
171 static int msdos_hash(struct dentry *dentry, struct qstr *qstr)
173 struct fat_mount_options *options = &MSDOS_SB(dentry->d_sb)->options;
174 unsigned char msdos_name[MSDOS_NAME];
175 int error;
177 error = msdos_format_name(qstr->name, qstr->len, msdos_name, options);
178 if (!error)
179 qstr->hash = full_name_hash(msdos_name, MSDOS_NAME);
180 return 0;
184 * Compare two msdos names. If either of the names are invalid,
185 * we fall back to doing the standard name comparison.
187 static int msdos_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b)
189 struct fat_mount_options *options = &MSDOS_SB(dentry->d_sb)->options;
190 unsigned char a_msdos_name[MSDOS_NAME], b_msdos_name[MSDOS_NAME];
191 int error;
193 error = msdos_format_name(a->name, a->len, a_msdos_name, options);
194 if (error)
195 goto old_compare;
196 error = msdos_format_name(b->name, b->len, b_msdos_name, options);
197 if (error)
198 goto old_compare;
199 error = memcmp(a_msdos_name, b_msdos_name, MSDOS_NAME);
200 out:
201 return error;
203 old_compare:
204 error = 1;
205 if (a->len == b->len)
206 error = memcmp(a->name, b->name, a->len);
207 goto out;
210 static struct dentry_operations msdos_dentry_operations = {
211 .d_hash = msdos_hash,
212 .d_compare = msdos_cmp,
216 * AV. Wrappers for FAT sb operations. Is it wise?
219 /***** Get inode using directory and name */
220 static struct dentry *msdos_lookup(struct inode *dir, struct dentry *dentry,
221 struct nameidata *nd)
223 struct super_block *sb = dir->i_sb;
224 struct fat_slot_info sinfo;
225 struct inode *inode = NULL;
226 int res;
228 dentry->d_op = &msdos_dentry_operations;
230 lock_kernel();
231 res = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &sinfo);
232 if (res == -ENOENT)
233 goto add;
234 if (res < 0)
235 goto out;
236 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
237 brelse(sinfo.bh);
238 if (IS_ERR(inode)) {
239 res = PTR_ERR(inode);
240 goto out;
242 add:
243 res = 0;
244 dentry = d_splice_alias(inode, dentry);
245 if (dentry)
246 dentry->d_op = &msdos_dentry_operations;
247 out:
248 unlock_kernel();
249 if (!res)
250 return dentry;
251 return ERR_PTR(res);
254 /***** Creates a directory entry (name is already formatted). */
255 static int msdos_add_entry(struct inode *dir, const unsigned char *name,
256 int is_dir, int is_hid, int cluster,
257 struct timespec *ts, struct fat_slot_info *sinfo)
259 struct msdos_dir_entry de;
260 __le16 time, date;
261 int err;
263 memcpy(de.name, name, MSDOS_NAME);
264 de.attr = is_dir ? ATTR_DIR : ATTR_ARCH;
265 if (is_hid)
266 de.attr |= ATTR_HIDDEN;
267 de.lcase = 0;
268 fat_date_unix2dos(ts->tv_sec, &time, &date);
269 de.cdate = de.adate = 0;
270 de.ctime = 0;
271 de.ctime_cs = 0;
272 de.time = time;
273 de.date = date;
274 de.start = cpu_to_le16(cluster);
275 de.starthi = cpu_to_le16(cluster >> 16);
276 de.size = 0;
278 err = fat_add_entries(dir, &de, 1, sinfo);
279 if (err)
280 return err;
282 dir->i_ctime = dir->i_mtime = *ts;
283 if (IS_DIRSYNC(dir))
284 (void)fat_sync_inode(dir);
285 else
286 mark_inode_dirty(dir);
288 return 0;
291 /***** Create a file */
292 static int msdos_create(struct inode *dir, struct dentry *dentry, int mode,
293 struct nameidata *nd)
295 struct super_block *sb = dir->i_sb;
296 struct inode *inode;
297 struct fat_slot_info sinfo;
298 struct timespec ts;
299 unsigned char msdos_name[MSDOS_NAME];
300 int err, is_hid;
302 lock_kernel();
304 err = msdos_format_name(dentry->d_name.name, dentry->d_name.len,
305 msdos_name, &MSDOS_SB(sb)->options);
306 if (err)
307 goto out;
308 is_hid = (dentry->d_name.name[0] == '.') && (msdos_name[0] != '.');
309 /* Have to do it due to foo vs. .foo conflicts */
310 if (!fat_scan(dir, msdos_name, &sinfo)) {
311 brelse(sinfo.bh);
312 err = -EINVAL;
313 goto out;
316 ts = CURRENT_TIME_SEC;
317 err = msdos_add_entry(dir, msdos_name, 0, is_hid, 0, &ts, &sinfo);
318 if (err)
319 goto out;
320 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
321 brelse(sinfo.bh);
322 if (IS_ERR(inode)) {
323 err = PTR_ERR(inode);
324 goto out;
326 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
327 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
329 d_instantiate(dentry, inode);
330 out:
331 unlock_kernel();
332 return err;
335 /***** Remove a directory */
336 static int msdos_rmdir(struct inode *dir, struct dentry *dentry)
338 struct inode *inode = dentry->d_inode;
339 struct fat_slot_info sinfo;
340 int err;
342 lock_kernel();
344 * Check whether the directory is not in use, then check
345 * whether it is empty.
347 err = fat_dir_empty(inode);
348 if (err)
349 goto out;
350 err = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &sinfo);
351 if (err)
352 goto out;
354 err = fat_remove_entries(dir, &sinfo); /* and releases bh */
355 if (err)
356 goto out;
357 dir->i_nlink--;
359 inode->i_nlink = 0;
360 inode->i_ctime = CURRENT_TIME_SEC;
361 fat_detach(inode);
362 out:
363 unlock_kernel();
365 return err;
368 /***** Make a directory */
369 static int msdos_mkdir(struct inode *dir, struct dentry *dentry, int mode)
371 struct super_block *sb = dir->i_sb;
372 struct fat_slot_info sinfo;
373 struct inode *inode;
374 unsigned char msdos_name[MSDOS_NAME];
375 struct timespec ts;
376 int err, is_hid, cluster;
378 lock_kernel();
380 err = msdos_format_name(dentry->d_name.name, dentry->d_name.len,
381 msdos_name, &MSDOS_SB(sb)->options);
382 if (err)
383 goto out;
384 is_hid = (dentry->d_name.name[0] == '.') && (msdos_name[0] != '.');
385 /* foo vs .foo situation */
386 if (!fat_scan(dir, msdos_name, &sinfo)) {
387 brelse(sinfo.bh);
388 err = -EINVAL;
389 goto out;
392 ts = CURRENT_TIME_SEC;
393 cluster = fat_alloc_new_dir(dir, &ts);
394 if (cluster < 0) {
395 err = cluster;
396 goto out;
398 err = msdos_add_entry(dir, msdos_name, 1, is_hid, cluster, &ts, &sinfo);
399 if (err)
400 goto out_free;
401 dir->i_nlink++;
403 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
404 brelse(sinfo.bh);
405 if (IS_ERR(inode)) {
406 err = PTR_ERR(inode);
407 /* the directory was completed, just return a error */
408 goto out;
410 inode->i_nlink = 2;
411 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
412 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
414 d_instantiate(dentry, inode);
416 unlock_kernel();
417 return 0;
419 out_free:
420 fat_free_clusters(dir, cluster);
421 out:
422 unlock_kernel();
423 return err;
426 /***** Unlink a file */
427 static int msdos_unlink(struct inode *dir, struct dentry *dentry)
429 struct inode *inode = dentry->d_inode;
430 struct fat_slot_info sinfo;
431 int err;
433 lock_kernel();
434 err = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &sinfo);
435 if (err)
436 goto out;
438 err = fat_remove_entries(dir, &sinfo); /* and releases bh */
439 if (err)
440 goto out;
441 inode->i_nlink = 0;
442 inode->i_ctime = CURRENT_TIME_SEC;
443 fat_detach(inode);
444 out:
445 unlock_kernel();
447 return err;
450 static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,
451 struct dentry *old_dentry,
452 struct inode *new_dir, unsigned char *new_name,
453 struct dentry *new_dentry, int is_hid)
455 struct buffer_head *dotdot_bh;
456 struct msdos_dir_entry *dotdot_de;
457 struct inode *old_inode, *new_inode;
458 struct fat_slot_info old_sinfo, sinfo;
459 struct timespec ts;
460 loff_t dotdot_i_pos, new_i_pos;
461 int err, old_attrs, is_dir, update_dotdot, corrupt = 0;
463 old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
464 old_inode = old_dentry->d_inode;
465 new_inode = new_dentry->d_inode;
467 err = fat_scan(old_dir, old_name, &old_sinfo);
468 if (err) {
469 err = -EIO;
470 goto out;
473 is_dir = S_ISDIR(old_inode->i_mode);
474 update_dotdot = (is_dir && old_dir != new_dir);
475 if (update_dotdot) {
476 if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de,
477 &dotdot_i_pos) < 0) {
478 err = -EIO;
479 goto out;
483 old_attrs = MSDOS_I(old_inode)->i_attrs;
484 err = fat_scan(new_dir, new_name, &sinfo);
485 if (!err) {
486 if (!new_inode) {
487 /* "foo" -> ".foo" case. just change the ATTR_HIDDEN */
488 if (sinfo.de != old_sinfo.de) {
489 err = -EINVAL;
490 goto out;
492 if (is_hid)
493 MSDOS_I(old_inode)->i_attrs |= ATTR_HIDDEN;
494 else
495 MSDOS_I(old_inode)->i_attrs &= ~ATTR_HIDDEN;
496 if (IS_DIRSYNC(old_dir)) {
497 err = fat_sync_inode(old_inode);
498 if (err) {
499 MSDOS_I(old_inode)->i_attrs = old_attrs;
500 goto out;
502 } else
503 mark_inode_dirty(old_inode);
505 old_dir->i_version++;
506 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
507 if (IS_DIRSYNC(old_dir))
508 (void)fat_sync_inode(old_dir);
509 else
510 mark_inode_dirty(old_dir);
511 goto out;
515 ts = CURRENT_TIME_SEC;
516 if (new_inode) {
517 if (err)
518 goto out;
519 if (is_dir) {
520 err = fat_dir_empty(new_inode);
521 if (err)
522 goto out;
524 new_i_pos = MSDOS_I(new_inode)->i_pos;
525 fat_detach(new_inode);
526 } else {
527 err = msdos_add_entry(new_dir, new_name, is_dir, is_hid, 0,
528 &ts, &sinfo);
529 if (err)
530 goto out;
531 new_i_pos = sinfo.i_pos;
533 new_dir->i_version++;
535 fat_detach(old_inode);
536 fat_attach(old_inode, new_i_pos);
537 if (is_hid)
538 MSDOS_I(old_inode)->i_attrs |= ATTR_HIDDEN;
539 else
540 MSDOS_I(old_inode)->i_attrs &= ~ATTR_HIDDEN;
541 if (IS_DIRSYNC(new_dir)) {
542 err = fat_sync_inode(old_inode);
543 if (err)
544 goto error_inode;
545 } else
546 mark_inode_dirty(old_inode);
548 if (update_dotdot) {
549 int start = MSDOS_I(new_dir)->i_logstart;
550 dotdot_de->start = cpu_to_le16(start);
551 dotdot_de->starthi = cpu_to_le16(start >> 16);
552 mark_buffer_dirty(dotdot_bh);
553 if (IS_DIRSYNC(new_dir)) {
554 err = sync_dirty_buffer(dotdot_bh);
555 if (err)
556 goto error_dotdot;
558 old_dir->i_nlink--;
559 if (!new_inode)
560 new_dir->i_nlink++;
563 err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */
564 old_sinfo.bh = NULL;
565 if (err)
566 goto error_dotdot;
567 old_dir->i_version++;
568 old_dir->i_ctime = old_dir->i_mtime = ts;
569 if (IS_DIRSYNC(old_dir))
570 (void)fat_sync_inode(old_dir);
571 else
572 mark_inode_dirty(old_dir);
574 if (new_inode) {
575 if (is_dir)
576 new_inode->i_nlink -= 2;
577 else
578 new_inode->i_nlink--;
579 new_inode->i_ctime = ts;
581 out:
582 brelse(sinfo.bh);
583 brelse(dotdot_bh);
584 brelse(old_sinfo.bh);
585 return err;
587 error_dotdot:
588 /* data cluster is shared, serious corruption */
589 corrupt = 1;
591 if (update_dotdot) {
592 int start = MSDOS_I(old_dir)->i_logstart;
593 dotdot_de->start = cpu_to_le16(start);
594 dotdot_de->starthi = cpu_to_le16(start >> 16);
595 mark_buffer_dirty(dotdot_bh);
596 corrupt |= sync_dirty_buffer(dotdot_bh);
598 error_inode:
599 fat_detach(old_inode);
600 fat_attach(old_inode, old_sinfo.i_pos);
601 MSDOS_I(old_inode)->i_attrs = old_attrs;
602 if (new_inode) {
603 fat_attach(new_inode, new_i_pos);
604 if (corrupt)
605 corrupt |= fat_sync_inode(new_inode);
606 } else {
608 * If new entry was not sharing the data cluster, it
609 * shouldn't be serious corruption.
611 int err2 = fat_remove_entries(new_dir, &sinfo);
612 if (corrupt)
613 corrupt |= err2;
614 sinfo.bh = NULL;
616 if (corrupt < 0) {
617 fat_fs_panic(new_dir->i_sb,
618 "%s: Filesystem corrupted (i_pos %lld)",
619 __FUNCTION__, sinfo.i_pos);
621 goto out;
624 /***** Rename, a wrapper for rename_same_dir & rename_diff_dir */
625 static int msdos_rename(struct inode *old_dir, struct dentry *old_dentry,
626 struct inode *new_dir, struct dentry *new_dentry)
628 unsigned char old_msdos_name[MSDOS_NAME], new_msdos_name[MSDOS_NAME];
629 int err, is_hid;
631 lock_kernel();
633 err = msdos_format_name(old_dentry->d_name.name,
634 old_dentry->d_name.len, old_msdos_name,
635 &MSDOS_SB(old_dir->i_sb)->options);
636 if (err)
637 goto out;
638 err = msdos_format_name(new_dentry->d_name.name,
639 new_dentry->d_name.len, new_msdos_name,
640 &MSDOS_SB(new_dir->i_sb)->options);
641 if (err)
642 goto out;
644 is_hid =
645 (new_dentry->d_name.name[0] == '.') && (new_msdos_name[0] != '.');
647 err = do_msdos_rename(old_dir, old_msdos_name, old_dentry,
648 new_dir, new_msdos_name, new_dentry, is_hid);
649 out:
650 unlock_kernel();
651 return err;
654 static struct inode_operations msdos_dir_inode_operations = {
655 .create = msdos_create,
656 .lookup = msdos_lookup,
657 .unlink = msdos_unlink,
658 .mkdir = msdos_mkdir,
659 .rmdir = msdos_rmdir,
660 .rename = msdos_rename,
661 .setattr = fat_notify_change,
664 static int msdos_fill_super(struct super_block *sb, void *data, int silent)
666 int res;
668 res = fat_fill_super(sb, data, silent, &msdos_dir_inode_operations, 0);
669 if (res)
670 return res;
672 sb->s_flags |= MS_NOATIME;
673 sb->s_root->d_op = &msdos_dentry_operations;
674 return 0;
677 static struct super_block *msdos_get_sb(struct file_system_type *fs_type,
678 int flags, const char *dev_name,
679 void *data)
681 return get_sb_bdev(fs_type, flags, dev_name, data, msdos_fill_super);
684 static struct file_system_type msdos_fs_type = {
685 .owner = THIS_MODULE,
686 .name = "msdos",
687 .get_sb = msdos_get_sb,
688 .kill_sb = kill_block_super,
689 .fs_flags = FS_REQUIRES_DEV,
692 static int __init init_msdos_fs(void)
694 return register_filesystem(&msdos_fs_type);
697 static void __exit exit_msdos_fs(void)
699 unregister_filesystem(&msdos_fs_type);
702 MODULE_LICENSE("GPL");
703 MODULE_AUTHOR("Werner Almesberger");
704 MODULE_DESCRIPTION("MS-DOS filesystem support");
706 module_init(init_msdos_fs)
707 module_exit(exit_msdos_fs)