vserver 1.9.5.x5
[linux-2.6.git] / fs / msdos / namei.c
1 /*
2  *  linux/fs/msdos/namei.c
3  *
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  */
8
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>
14
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
21 };
22
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[] = " ";
28
29 #define bad_if_strict(opts) \
30         ((opts)->atari ? bad_if_strict_atari : bad_if_strict_pc)
31
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)
35         /*
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)
40          */
41 {
42         unsigned char *walk;
43         const unsigned char **reserved;
44         unsigned char c;
45         int space;
46
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;
54         }
55         /*
56          * disallow names that _really_ start with a dot for MS-DOS,
57          * GEMDOS does not care
58          */
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;
72         /*
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.
79          */
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;
86         }
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;
94         }
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;
114                         }
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;
122                 }
123                 if (space)
124                         return -EINVAL;
125                 if (opts->name_check == 's' && len)
126                         return -EINVAL;
127         }
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;
136 }
137
138 /***** Locates a directory entry.  Uses unformatted name. */
139 static int msdos_find(struct inode *dir, const unsigned char *name, int len,
140                       struct buffer_head **bh, struct msdos_dir_entry **de,
141                       loff_t *i_pos)
142 {
143         unsigned char msdos_name[MSDOS_NAME];
144         char dotsOK;
145         int res;
146
147         dotsOK = MSDOS_SB(dir->i_sb)->options.dotsOK;
148         res = msdos_format_name(name, len, msdos_name,
149                                 &MSDOS_SB(dir->i_sb)->options);
150         if (res < 0)
151                 return -ENOENT;
152         res = fat_scan(dir, msdos_name, bh, de, i_pos);
153         if (!res && dotsOK) {
154                 if (name[0] == '.') {
155                         if (!((*de)->attr & ATTR_HIDDEN))
156                                 res = -ENOENT;
157                 } else {
158                         if ((*de)->attr & ATTR_HIDDEN)
159                                 res = -ENOENT;
160                 }
161         }
162         return res;
163 }
164
165 /*
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.
170  */
171 static int msdos_hash(struct dentry *dentry, struct qstr *qstr)
172 {
173         struct fat_mount_options *options = &MSDOS_SB(dentry->d_sb)->options;
174         unsigned char msdos_name[MSDOS_NAME];
175         int error;
176
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;
181 }
182
183 /*
184  * Compare two msdos names. If either of the names are invalid,
185  * we fall back to doing the standard name comparison.
186  */
187 static int msdos_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b)
188 {
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;
192
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;
202
203 old_compare:
204         error = 1;
205         if (a->len == b->len)
206                 error = memcmp(a->name, b->name, a->len);
207         goto out;
208 }
209
210 static struct dentry_operations msdos_dentry_operations = {
211         .d_hash         = msdos_hash,
212         .d_compare      = msdos_cmp,
213 };
214
215 /*
216  * AV. Wrappers for FAT sb operations. Is it wise?
217  */
218
219 /***** Get inode using directory and name */
220 static struct dentry *msdos_lookup(struct inode *dir, struct dentry *dentry,
221                                    struct nameidata *nd)
222 {
223         struct super_block *sb = dir->i_sb;
224         struct inode *inode = NULL;
225         struct msdos_dir_entry *de;
226         struct buffer_head *bh = NULL;
227         loff_t i_pos;
228         int res;
229
230         dentry->d_op = &msdos_dentry_operations;
231
232         lock_kernel();
233         res = msdos_find(dir, dentry->d_name.name, dentry->d_name.len, &bh,
234                          &de, &i_pos);
235         if (res == -ENOENT)
236                 goto add;
237         if (res < 0)
238                 goto out;
239         inode = fat_build_inode(sb, de, i_pos, &res);
240         if (res)
241                 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         brelse(bh);
249         unlock_kernel();
250         if (!res)
251                 return dentry;
252         return ERR_PTR(res);
253 }
254
255 /***** Creates a directory entry (name is already formatted). */
256 static int msdos_add_entry(struct inode *dir, const unsigned char *name,
257                            struct buffer_head **bh,
258                            struct msdos_dir_entry **de,
259                            loff_t *i_pos, int is_dir, int is_hid)
260 {
261         int res;
262
263         res = fat_add_entries(dir, 1, bh, de, i_pos);
264         if (res < 0)
265                 return res;
266
267         /*
268          * XXX all times should be set by caller upon successful completion.
269          */
270         dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
271         mark_inode_dirty(dir);
272
273         memcpy((*de)->name, name, MSDOS_NAME);
274         (*de)->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
275         if (is_hid)
276                 (*de)->attr |= ATTR_HIDDEN;
277         (*de)->start = 0;
278         (*de)->starthi = 0;
279         fat_date_unix2dos(dir->i_mtime.tv_sec, &(*de)->time, &(*de)->date);
280         (*de)->size = 0;
281         mark_buffer_dirty(*bh);
282         return 0;
283 }
284
285 /***** Create a file */
286 static int msdos_create(struct inode *dir, struct dentry *dentry, int mode,
287                         struct nameidata *nd)
288 {
289         struct super_block *sb = dir->i_sb;
290         struct buffer_head *bh;
291         struct msdos_dir_entry *de;
292         struct inode *inode;
293         loff_t i_pos;
294         int res, is_hid;
295         unsigned char msdos_name[MSDOS_NAME];
296
297         lock_kernel();
298         res = msdos_format_name(dentry->d_name.name, dentry->d_name.len,
299                                 msdos_name, &MSDOS_SB(sb)->options);
300         if (res < 0) {
301                 unlock_kernel();
302                 return res;
303         }
304         is_hid = (dentry->d_name.name[0] == '.') && (msdos_name[0] != '.');
305         /* Have to do it due to foo vs. .foo conflicts */
306         if (fat_scan(dir, msdos_name, &bh, &de, &i_pos) >= 0) {
307                 brelse(bh);
308                 unlock_kernel();
309                 return -EINVAL;
310         }
311         inode = NULL;
312         res = msdos_add_entry(dir, msdos_name, &bh, &de, &i_pos, 0, is_hid);
313         if (res) {
314                 unlock_kernel();
315                 return res;
316         }
317         inode = fat_build_inode(dir->i_sb, de, i_pos, &res);
318         brelse(bh);
319         if (!inode) {
320                 unlock_kernel();
321                 return res;
322         }
323         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
324         mark_inode_dirty(inode);
325         d_instantiate(dentry, inode);
326         unlock_kernel();
327         return 0;
328 }
329
330 /***** Remove a directory */
331 static int msdos_rmdir(struct inode *dir, struct dentry *dentry)
332 {
333         struct inode *inode = dentry->d_inode;
334         loff_t i_pos;
335         int res;
336         struct buffer_head *bh;
337         struct msdos_dir_entry *de;
338
339         bh = NULL;
340         lock_kernel();
341         res = msdos_find(dir, dentry->d_name.name, dentry->d_name.len,
342                          &bh, &de, &i_pos);
343         if (res < 0)
344                 goto rmdir_done;
345         /*
346          * Check whether the directory is not in use, then check
347          * whether it is empty.
348          */
349         res = fat_dir_empty(inode);
350         if (res)
351                 goto rmdir_done;
352
353         de->name[0] = DELETED_FLAG;
354         mark_buffer_dirty(bh);
355         fat_detach(inode);
356         inode->i_nlink = 0;
357         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
358         dir->i_nlink--;
359         mark_inode_dirty(inode);
360         mark_inode_dirty(dir);
361         res = 0;
362
363 rmdir_done:
364         brelse(bh);
365         unlock_kernel();
366         return res;
367 }
368
369 /***** Make a directory */
370 static int msdos_mkdir(struct inode *dir, struct dentry *dentry, int mode)
371 {
372         struct super_block *sb = dir->i_sb;
373         struct buffer_head *bh;
374         struct msdos_dir_entry *de;
375         struct inode *inode;
376         int res, is_hid;
377         unsigned char msdos_name[MSDOS_NAME];
378         loff_t i_pos;
379
380         lock_kernel();
381         res = msdos_format_name(dentry->d_name.name, dentry->d_name.len,
382                                 msdos_name, &MSDOS_SB(sb)->options);
383         if (res < 0) {
384                 unlock_kernel();
385                 return res;
386         }
387         is_hid = (dentry->d_name.name[0] == '.') && (msdos_name[0] != '.');
388         /* foo vs .foo situation */
389         if (fat_scan(dir, msdos_name, &bh, &de, &i_pos) >= 0)
390                 goto out_exist;
391
392         res = msdos_add_entry(dir, msdos_name, &bh, &de, &i_pos, 1, is_hid);
393         if (res)
394                 goto out_unlock;
395         inode = fat_build_inode(dir->i_sb, de, i_pos, &res);
396         if (!inode) {
397                 brelse(bh);
398                 goto out_unlock;
399         }
400         res = 0;
401
402         dir->i_nlink++;
403         inode->i_nlink = 2;     /* no need to mark them dirty */
404
405         res = fat_new_dir(inode, dir, 0);
406         if (res)
407                 goto mkdir_error;
408
409         brelse(bh);
410         d_instantiate(dentry, inode);
411         res = 0;
412
413 out_unlock:
414         unlock_kernel();
415         return res;
416
417 mkdir_error:
418         inode->i_nlink = 0;
419         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
420         dir->i_nlink--;
421         mark_inode_dirty(inode);
422         mark_inode_dirty(dir);
423         de->name[0] = DELETED_FLAG;
424         mark_buffer_dirty(bh);
425         brelse(bh);
426         fat_detach(inode);
427         iput(inode);
428         goto out_unlock;
429
430 out_exist:
431         brelse(bh);
432         res = -EINVAL;
433         goto out_unlock;
434 }
435
436 /***** Unlink a file */
437 static int msdos_unlink(struct inode *dir, struct dentry *dentry)
438 {
439         struct inode *inode = dentry->d_inode;
440         loff_t i_pos;
441         int res;
442         struct buffer_head *bh;
443         struct msdos_dir_entry *de;
444
445         bh = NULL;
446         lock_kernel();
447         res = msdos_find(dir, dentry->d_name.name, dentry->d_name.len,
448                          &bh, &de, &i_pos);
449         if (res < 0)
450                 goto unlink_done;
451
452         de->name[0] = DELETED_FLAG;
453         mark_buffer_dirty(bh);
454         fat_detach(inode);
455         brelse(bh);
456         inode->i_nlink = 0;
457         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
458         mark_inode_dirty(inode);
459         mark_inode_dirty(dir);
460         res = 0;
461 unlink_done:
462         unlock_kernel();
463         return res;
464 }
465
466 static int do_msdos_rename(struct inode *old_dir, unsigned char *old_name,
467                            struct dentry *old_dentry,
468                            struct inode *new_dir, unsigned char *new_name,
469                            struct dentry *new_dentry,
470                            struct buffer_head *old_bh,
471                            struct msdos_dir_entry *old_de, loff_t old_i_pos,
472                            int is_hid)
473 {
474         struct buffer_head *new_bh = NULL, *dotdot_bh = NULL;
475         struct msdos_dir_entry *new_de, *dotdot_de;
476         struct inode *old_inode, *new_inode;
477         loff_t new_i_pos, dotdot_i_pos;
478         int error;
479         int is_dir;
480
481         old_inode = old_dentry->d_inode;
482         new_inode = new_dentry->d_inode;
483         is_dir = S_ISDIR(old_inode->i_mode);
484
485         if (fat_scan(new_dir, new_name, &new_bh, &new_de, &new_i_pos) >= 0 &&
486             !new_inode)
487                 goto degenerate_case;
488         if (is_dir) {
489                 if (new_inode) {
490                         error = fat_dir_empty(new_inode);
491                         if (error)
492                                 goto out;
493                 }
494                 if (fat_scan(old_inode, MSDOS_DOTDOT, &dotdot_bh,
495                              &dotdot_de, &dotdot_i_pos) < 0) {
496                         error = -EIO;
497                         goto out;
498                 }
499         }
500         if (!new_bh) {
501                 error = msdos_add_entry(new_dir, new_name, &new_bh, &new_de,
502                                         &new_i_pos, is_dir, is_hid);
503                 if (error)
504                         goto out;
505         }
506         new_dir->i_version++;
507
508         /* There we go */
509
510         if (new_inode)
511                 fat_detach(new_inode);
512         old_de->name[0] = DELETED_FLAG;
513         mark_buffer_dirty(old_bh);
514         fat_detach(old_inode);
515         fat_attach(old_inode, new_i_pos);
516         if (is_hid)
517                 MSDOS_I(old_inode)->i_attrs |= ATTR_HIDDEN;
518         else
519                 MSDOS_I(old_inode)->i_attrs &= ~ATTR_HIDDEN;
520         mark_inode_dirty(old_inode);
521         old_dir->i_version++;
522         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
523         mark_inode_dirty(old_dir);
524         if (new_inode) {
525                 new_inode->i_nlink--;
526                 new_inode->i_ctime = CURRENT_TIME_SEC;
527                 mark_inode_dirty(new_inode);
528         }
529         if (dotdot_bh) {
530                 dotdot_de->start = cpu_to_le16(MSDOS_I(new_dir)->i_logstart);
531                 dotdot_de->starthi =
532                         cpu_to_le16((MSDOS_I(new_dir)->i_logstart) >> 16);
533                 mark_buffer_dirty(dotdot_bh);
534                 old_dir->i_nlink--;
535                 mark_inode_dirty(old_dir);
536                 if (new_inode) {
537                         new_inode->i_nlink--;
538                         mark_inode_dirty(new_inode);
539                 } else {
540                         new_dir->i_nlink++;
541                         mark_inode_dirty(new_dir);
542                 }
543         }
544         error = 0;
545 out:
546         brelse(new_bh);
547         brelse(dotdot_bh);
548         return error;
549
550 degenerate_case:
551         error = -EINVAL;
552         if (new_de != old_de)
553                 goto out;
554         if (is_hid)
555                 MSDOS_I(old_inode)->i_attrs |= ATTR_HIDDEN;
556         else
557                 MSDOS_I(old_inode)->i_attrs &= ~ATTR_HIDDEN;
558         mark_inode_dirty(old_inode);
559         old_dir->i_version++;
560         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME_SEC;
561         mark_inode_dirty(old_dir);
562         return 0;
563 }
564
565 /***** Rename, a wrapper for rename_same_dir & rename_diff_dir */
566 static int msdos_rename(struct inode *old_dir, struct dentry *old_dentry,
567                         struct inode *new_dir, struct dentry *new_dentry)
568 {
569         struct buffer_head *old_bh;
570         struct msdos_dir_entry *old_de;
571         loff_t old_i_pos;
572         int error, is_hid, old_hid; /* if new file and old file are hidden */
573         unsigned char old_msdos_name[MSDOS_NAME], new_msdos_name[MSDOS_NAME];
574
575         lock_kernel();
576         error = msdos_format_name(old_dentry->d_name.name,
577                                   old_dentry->d_name.len, old_msdos_name,
578                                   &MSDOS_SB(old_dir->i_sb)->options);
579         if (error < 0)
580                 goto rename_done;
581         error = msdos_format_name(new_dentry->d_name.name,
582                                   new_dentry->d_name.len, new_msdos_name,
583                                   &MSDOS_SB(new_dir->i_sb)->options);
584         if (error < 0)
585                 goto rename_done;
586
587         is_hid =
588              (new_dentry->d_name.name[0] == '.') && (new_msdos_name[0] != '.');
589         old_hid =
590              (old_dentry->d_name.name[0] == '.') && (old_msdos_name[0] != '.');
591
592         error = fat_scan(old_dir, old_msdos_name, &old_bh, &old_de, &old_i_pos);
593         if (error < 0)
594                 goto rename_done;
595
596         error = do_msdos_rename(old_dir, old_msdos_name, old_dentry,
597                                 new_dir, new_msdos_name, new_dentry,
598                                 old_bh, old_de, old_i_pos, is_hid);
599         brelse(old_bh);
600
601 rename_done:
602         unlock_kernel();
603         return error;
604 }
605
606 static struct inode_operations msdos_dir_inode_operations = {
607         .create         = msdos_create,
608         .lookup         = msdos_lookup,
609         .unlink         = msdos_unlink,
610         .mkdir          = msdos_mkdir,
611         .rmdir          = msdos_rmdir,
612         .rename         = msdos_rename,
613         .setattr        = fat_notify_change,
614 };
615
616 static int msdos_fill_super(struct super_block *sb, void *data, int silent)
617 {
618         int res;
619
620         res = fat_fill_super(sb, data, silent, &msdos_dir_inode_operations, 0);
621         if (res)
622                 return res;
623
624         sb->s_root->d_op = &msdos_dentry_operations;
625         return 0;
626 }
627
628 static struct super_block *msdos_get_sb(struct file_system_type *fs_type,
629                                         int flags, const char *dev_name,
630                                         void *data)
631 {
632         return get_sb_bdev(fs_type, flags, dev_name, data, msdos_fill_super);
633 }
634
635 static struct file_system_type msdos_fs_type = {
636         .owner          = THIS_MODULE,
637         .name           = "msdos",
638         .get_sb         = msdos_get_sb,
639         .kill_sb        = kill_block_super,
640         .fs_flags       = FS_REQUIRES_DEV,
641 };
642
643 static int __init init_msdos_fs(void)
644 {
645         return register_filesystem(&msdos_fs_type);
646 }
647
648 static void __exit exit_msdos_fs(void)
649 {
650         unregister_filesystem(&msdos_fs_type);
651 }
652
653 MODULE_LICENSE("GPL");
654 MODULE_AUTHOR("Werner Almesberger");
655 MODULE_DESCRIPTION("MS-DOS filesystem support");
656
657 module_init(init_msdos_fs)
658 module_exit(exit_msdos_fs)