ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / vfat / namei.c
1 /*
2  *  linux/fs/vfat/namei.c
3  *
4  *  Written 1992,1993 by Werner Almesberger
5  *
6  *  Windows95/Windows NT compatible extended MSDOS filesystem
7  *    by Gordon Chaffee Copyright (C) 1995.  Send bug reports for the
8  *    VFAT filesystem to <chaffee@cs.berkeley.edu>.  Specify
9  *    what file operation caused you trouble and if you can duplicate
10  *    the problem, send a script that demonstrates it.
11  *
12  *  Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
13  *
14  *  Support Multibyte character and cleanup by
15  *                              OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
16  */
17
18 #include <linux/module.h>
19
20 #include <linux/jiffies.h>
21 #include <linux/msdos_fs.h>
22 #include <linux/ctype.h>
23 #include <linux/slab.h>
24 #include <linux/smp_lock.h>
25 #include <linux/buffer_head.h>
26 #include <linux/namei.h>
27
28 static int vfat_hashi(struct dentry *parent, struct qstr *qstr);
29 static int vfat_hash(struct dentry *parent, struct qstr *qstr);
30 static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b);
31 static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b);
32 static int vfat_revalidate(struct dentry *dentry, struct nameidata *nd);
33
34 static struct dentry_operations vfat_dentry_ops[4] = {
35         {
36                 .d_hash         = vfat_hashi,
37                 .d_compare      = vfat_cmpi,
38         },
39         {
40                 .d_revalidate   = vfat_revalidate,
41                 .d_hash         = vfat_hashi,
42                 .d_compare      = vfat_cmpi,
43         },
44         {
45                 .d_hash         = vfat_hash,
46                 .d_compare      = vfat_cmp,
47         },
48         {
49                 .d_revalidate   = vfat_revalidate,
50                 .d_hash         = vfat_hash,
51                 .d_compare      = vfat_cmp,
52         }
53 };
54
55 static int vfat_revalidate(struct dentry *dentry, struct nameidata *nd)
56 {
57         int ret = 1;
58
59         if (!dentry->d_inode &&
60             nd && !(nd->flags & LOOKUP_CONTINUE) && (nd->flags & LOOKUP_CREATE))
61                 /*
62                  * negative dentry is dropped, in order to make sure
63                  * to use the name which a user desires if this is
64                  * create path.
65                  */
66                 ret = 0;
67         else {
68                 spin_lock(&dentry->d_lock);
69                 if (dentry->d_time != dentry->d_parent->d_inode->i_version)
70                         ret = 0;
71                 spin_unlock(&dentry->d_lock);
72         }
73         return ret;
74 }
75
76 static inline unsigned char
77 vfat_tolower(struct nls_table *t, unsigned char c)
78 {
79         unsigned char nc = t->charset2lower[c];
80
81         return nc ? nc : c;
82 }
83
84 static inline unsigned char
85 vfat_toupper(struct nls_table *t, unsigned char c)
86 {
87         unsigned char nc = t->charset2upper[c];
88
89         return nc ? nc : c;
90 }
91
92 static inline int
93 vfat_strnicmp(struct nls_table *t, const unsigned char *s1,
94                                         const unsigned char *s2, int len)
95 {
96         while(len--)
97                 if (vfat_tolower(t, *s1++) != vfat_tolower(t, *s2++))
98                         return 1;
99
100         return 0;
101 }
102
103 /* returns the length of a struct qstr, ignoring trailing dots */
104 static unsigned int vfat_striptail_len(struct qstr *qstr)
105 {
106         unsigned int len = qstr->len;
107
108         while (len && qstr->name[len-1] == '.')
109                 len--;
110
111         return len;
112 }
113
114 /*
115  * Compute the hash for the vfat name corresponding to the dentry.
116  * Note: if the name is invalid, we leave the hash code unchanged so
117  * that the existing dentry can be used. The vfat fs routines will
118  * return ENOENT or EINVAL as appropriate.
119  */
120 static int vfat_hash(struct dentry *dentry, struct qstr *qstr)
121 {
122         qstr->hash = full_name_hash(qstr->name, vfat_striptail_len(qstr));
123
124         return 0;
125 }
126
127 /*
128  * Compute the hash for the vfat name corresponding to the dentry.
129  * Note: if the name is invalid, we leave the hash code unchanged so
130  * that the existing dentry can be used. The vfat fs routines will
131  * return ENOENT or EINVAL as appropriate.
132  */
133 static int vfat_hashi(struct dentry *dentry, struct qstr *qstr)
134 {
135         struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;
136         const unsigned char *name;
137         unsigned int len;
138         unsigned long hash;
139
140         name = qstr->name;
141         len = vfat_striptail_len(qstr);
142
143         hash = init_name_hash();
144         while (len--)
145                 hash = partial_name_hash(vfat_tolower(t, *name++), hash);
146         qstr->hash = end_name_hash(hash);
147
148         return 0;
149 }
150
151 /*
152  * Case insensitive compare of two vfat names.
153  */
154 static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b)
155 {
156         struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;
157         unsigned int alen, blen;
158
159         /* A filename cannot end in '.' or we treat it like it has none */
160         alen = vfat_striptail_len(a);
161         blen = vfat_striptail_len(b);
162         if (alen == blen) {
163                 if (vfat_strnicmp(t, a->name, b->name, alen) == 0)
164                         return 0;
165         }
166         return 1;
167 }
168
169 /*
170  * Case sensitive compare of two vfat names.
171  */
172 static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b)
173 {
174         unsigned int alen, blen;
175
176         /* A filename cannot end in '.' or we treat it like it has none */
177         alen = vfat_striptail_len(a);
178         blen = vfat_striptail_len(b);
179         if (alen == blen) {
180                 if (strncmp(a->name, b->name, alen) == 0)
181                         return 0;
182         }
183         return 1;
184 }
185
186 /* Characters that are undesirable in an MS-DOS file name */
187
188 static wchar_t bad_chars[] = {
189         /*  `*'     `?'     `<'    `>'      `|'     `"'     `:'     `/' */
190         0x002A, 0x003F, 0x003C, 0x003E, 0x007C, 0x0022, 0x003A, 0x002F,
191         /*  `\' */
192         0x005C, 0,
193 };
194 #define IS_BADCHAR(uni) (vfat_unistrchr(bad_chars, (uni)) != NULL)
195
196 static wchar_t replace_chars[] = {
197         /*  `['     `]'    `;'     `,'     `+'      `=' */
198         0x005B, 0x005D, 0x003B, 0x002C, 0x002B, 0x003D, 0,
199 };
200 #define IS_REPLACECHAR(uni)     (vfat_unistrchr(replace_chars, (uni)) != NULL)
201
202 static wchar_t skip_chars[] = {
203         /*  `.'     ` ' */
204         0x002E, 0x0020, 0,
205 };
206 #define IS_SKIPCHAR(uni) \
207         ((wchar_t)(uni) == skip_chars[0] || (wchar_t)(uni) == skip_chars[1])
208
209 static inline wchar_t *vfat_unistrchr(const wchar_t *s, const wchar_t c)
210 {
211         for(; *s != c; ++s)
212                 if (*s == 0)
213                         return NULL;
214         return (wchar_t *) s;
215 }
216
217 static inline int vfat_is_used_badchars(const wchar_t *s, int len)
218 {
219         int i;
220         
221         for (i = 0; i < len; i++)
222                 if (s[i] < 0x0020 || IS_BADCHAR(s[i]))
223                         return -EINVAL;
224         return 0;
225 }
226
227 static int vfat_valid_longname(const unsigned char *name, unsigned int len)
228 {
229         if (len && name[len-1] == ' ')
230                 return 0;
231         if (len >= 256)
232                 return 0;
233
234         /* MS-DOS "device special files" */
235         if (len == 3 || (len > 3 && name[3] == '.')) {  /* basename == 3 */
236                 if (!strnicmp(name, "aux", 3) ||
237                     !strnicmp(name, "con", 3) ||
238                     !strnicmp(name, "nul", 3) ||
239                     !strnicmp(name, "prn", 3))
240                         return 0;
241         }
242         if (len == 4 || (len > 4 && name[4] == '.')) {  /* basename == 4 */
243                 /* "com1", "com2", ... */
244                 if ('1' <= name[3] && name[3] <= '9') {
245                         if (!strnicmp(name, "com", 3) ||
246                             !strnicmp(name, "lpt", 3))
247                                 return 0;
248                 }
249         }
250
251         return 1;
252 }
253
254 static int vfat_find_form(struct inode *dir, unsigned char *name)
255 {
256         struct msdos_dir_entry *de;
257         struct buffer_head *bh = NULL;
258         loff_t i_pos;
259         int res;
260
261         res = fat_scan(dir, name, &bh, &de, &i_pos);
262         brelse(bh);
263         if (res < 0)
264                 return -ENOENT;
265         return 0;
266 }
267
268 /* 
269  * 1) Valid characters for the 8.3 format alias are any combination of
270  * letters, uppercase alphabets, digits, any of the
271  * following special characters:
272  *     $ % ' ` - @ { } ~ ! # ( ) & _ ^
273  * In this case Longfilename is not stored in disk.
274  *
275  * WinNT's Extension:
276  * File name and extension name is contain uppercase/lowercase
277  * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT.
278  *     
279  * 2) File name is 8.3 format, but it contain the uppercase and
280  * lowercase char, muliti bytes char, etc. In this case numtail is not
281  * added, but Longfilename is stored.
282  * 
283  * 3) When the one except for the above, or the following special
284  * character are contained:
285  *        .   [ ] ; , + =
286  * numtail is added, and Longfilename must be stored in disk .
287  */
288 struct shortname_info {
289         unsigned char lower:1,
290                       upper:1,
291                       valid:1;
292 };
293 #define INIT_SHORTNAME_INFO(x)  do {            \
294         (x)->lower = 1;                         \
295         (x)->upper = 1;                         \
296         (x)->valid = 1;                         \
297 } while (0)
298
299 static inline unsigned char
300 shortname_info_to_lcase(struct shortname_info *base,
301                         struct shortname_info *ext)
302 {
303         unsigned char lcase = 0;
304
305         if (base->valid && ext->valid) {
306                 if (!base->upper && base->lower && (ext->lower || ext->upper))
307                         lcase |= CASE_LOWER_BASE;
308                 if (!ext->upper && ext->lower && (base->lower || base->upper))
309                         lcase |= CASE_LOWER_EXT;
310         }
311
312         return lcase;
313 }
314
315 static inline int to_shortname_char(struct nls_table *nls,
316                                     unsigned char *buf, int buf_size, wchar_t *src,
317                                     struct shortname_info *info)
318 {
319         int len;
320
321         if (IS_SKIPCHAR(*src)) {
322                 info->valid = 0;
323                 return 0;
324         }
325         if (IS_REPLACECHAR(*src)) {
326                 info->valid = 0;
327                 buf[0] = '_';
328                 return 1;
329         }
330         
331         len = nls->uni2char(*src, buf, buf_size);
332         if (len <= 0) {
333                 info->valid = 0;
334                 buf[0] = '_';
335                 len = 1;
336         } else if (len == 1) {
337                 unsigned char prev = buf[0];
338
339                 if (buf[0] >= 0x7F) {
340                         info->lower = 0;
341                         info->upper = 0;
342                 }
343
344                 buf[0] = vfat_toupper(nls, buf[0]);
345                 if (isalpha(buf[0])) {
346                         if (buf[0] == prev)
347                                 info->lower = 0;
348                         else
349                                 info->upper = 0;
350                 }
351         } else {
352                 info->lower = 0;
353                 info->upper = 0;
354         }
355         
356         return len;
357 }
358
359 /*
360  * Given a valid longname, create a unique shortname.  Make sure the
361  * shortname does not exist
362  * Returns negative number on error, 0 for a normal
363  * return, and 1 for valid shortname
364  */
365 static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
366                                  wchar_t *uname, int ulen,
367                                  unsigned char *name_res, unsigned char *lcase)
368 {
369         wchar_t *ip, *ext_start, *end, *name_start;
370         unsigned char base[9], ext[4], buf[8], *p;
371         unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
372         int chl, chi;
373         int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
374         int is_shortname;
375         struct shortname_info base_info, ext_info;
376         unsigned short opt_shortname = MSDOS_SB(dir->i_sb)->options.shortname;
377
378         is_shortname = 1;
379         INIT_SHORTNAME_INFO(&base_info);
380         INIT_SHORTNAME_INFO(&ext_info);
381
382         /* Now, we need to create a shortname from the long name */
383         ext_start = end = &uname[ulen];
384         while (--ext_start >= uname) {
385                 if (*ext_start == 0x002E) { /* is `.' */
386                         if (ext_start == end - 1) {
387                                 sz = ulen;
388                                 ext_start = NULL;
389                         }
390                         break;
391                 }
392         }
393
394         if (ext_start == uname - 1) {
395                 sz = ulen;
396                 ext_start = NULL;
397         } else if (ext_start) {
398                 /*
399                  * Names which start with a dot could be just
400                  * an extension eg. "...test".  In this case Win95
401                  * uses the extension as the name and sets no extension.
402                  */
403                 name_start = &uname[0];
404                 while (name_start < ext_start) {
405                         if (!IS_SKIPCHAR(*name_start))
406                                 break;
407                         name_start++;
408                 }
409                 if (name_start != ext_start) {
410                         sz = ext_start - uname;
411                         ext_start++;
412                 } else {
413                         sz = ulen;
414                         ext_start=NULL;
415                 }
416         }
417
418         numtail_baselen = 6;
419         numtail2_baselen = 2;
420         for (baselen = i = 0, p = base, ip = uname; i < sz; i++, ip++) {
421                 chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
422                                         ip, &base_info);
423                 if (chl == 0)
424                         continue;
425
426                 if (baselen < 2 && (baselen + chl) > 2)
427                         numtail2_baselen = baselen;
428                 if (baselen < 6 && (baselen + chl) > 6)
429                         numtail_baselen = baselen;
430                 for (chi = 0; chi < chl; chi++){
431                         *p++ = charbuf[chi];
432                         baselen++;
433                         if (baselen >= 8)
434                                 break;
435                 }
436                 if (baselen >= 8) {
437                         if ((chi < chl - 1) || (ip + 1) - uname < sz)
438                                 is_shortname = 0;
439                         break;
440                 }
441         }
442         if (baselen == 0) {
443                 return -EINVAL;
444         }
445
446         extlen = 0;
447         if (ext_start) {
448                 for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) {
449                         chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
450                                                 ip, &ext_info);
451                         if (chl == 0)
452                                 continue;
453
454                         if ((extlen + chl) > 3) {
455                                 is_shortname = 0;
456                                 break;
457                         }
458                         for (chi = 0; chi < chl; chi++) {
459                                 *p++ = charbuf[chi];
460                                 extlen++;
461                         }
462                         if (extlen >= 3) {
463                                 if (ip + 1 != end)
464                                         is_shortname = 0;
465                                 break;
466                         }
467                 }
468         }
469         ext[extlen] = '\0';
470         base[baselen] = '\0';
471
472         /* Yes, it can happen. ".\xe5" would do it. */
473         if (base[0] == DELETED_FLAG)
474                 base[0] = 0x05;
475
476         /* OK, at this point we know that base is not longer than 8 symbols,
477          * ext is not longer than 3, base is nonempty, both don't contain
478          * any bad symbols (lowercase transformed to uppercase).
479          */
480
481         memset(name_res, ' ', MSDOS_NAME);
482         memcpy(name_res, base, baselen);
483         memcpy(name_res + 8, ext, extlen);
484         *lcase = 0;
485         if (is_shortname && base_info.valid && ext_info.valid) {
486                 if (vfat_find_form(dir, name_res) == 0)
487                         return -EEXIST;
488
489                 if (opt_shortname & VFAT_SFN_CREATE_WIN95) {
490                         return (base_info.upper && ext_info.upper);
491                 } else if (opt_shortname & VFAT_SFN_CREATE_WINNT) {
492                         if ((base_info.upper || base_info.lower)
493                             && (ext_info.upper || ext_info.lower)) {
494                                 *lcase = shortname_info_to_lcase(&base_info,
495                                                                  &ext_info);
496                                 return 1;
497                         }
498                         return 0;
499                 } else {
500                         BUG();
501                 }
502         }
503         
504         if (MSDOS_SB(dir->i_sb)->options.numtail == 0)
505                 if (vfat_find_form(dir, name_res) < 0)
506                         return 0;
507
508         /*
509          * Try to find a unique extension.  This used to
510          * iterate through all possibilities sequentially,
511          * but that gave extremely bad performance.  Windows
512          * only tries a few cases before using random
513          * values for part of the base.
514          */
515
516         if (baselen>6) {
517                 baselen = numtail_baselen;
518                 name_res[7] = ' ';
519         }
520         name_res[baselen] = '~';
521         for (i = 1; i < 10; i++) {
522                 name_res[baselen+1] = i + '0';
523                 if (vfat_find_form(dir, name_res) < 0)
524                         return 0;
525         }
526
527         i = jiffies & 0xffff;
528         sz = (jiffies >> 16) & 0x7;
529         if (baselen>2) {
530                 baselen = numtail2_baselen;
531                 name_res[7] = ' ';
532         }
533         name_res[baselen+4] = '~';
534         name_res[baselen+5] = '1' + sz;
535         while (1) {
536                 sprintf(buf, "%04X", i);
537                 memcpy(&name_res[baselen], buf, 4);
538                 if (vfat_find_form(dir, name_res) < 0)
539                         break;
540                 i -= 11;
541         }
542         return 0;
543 }
544
545 /* Translate a string, including coded sequences into Unicode */
546 static int
547 xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
548              int *longlen, int *outlen, int escape, int utf8,
549              struct nls_table *nls)
550 {
551         const unsigned char *ip;
552         unsigned char nc;
553         unsigned char *op;
554         unsigned int ec;
555         int i, k, fill;
556         int charlen;
557
558         if (utf8) {
559                 int name_len = strlen(name);
560
561                 *outlen = utf8_mbstowcs((wchar_t *)outname, name, PAGE_SIZE);
562
563                 /*
564                  * We stripped '.'s before and set len appropriately,
565                  * but utf8_mbstowcs doesn't care about len
566                  */
567                 *outlen -= (name_len-len);
568
569                 op = &outname[*outlen * sizeof(wchar_t)];
570         } else {
571                 if (nls) {
572                         for (i = 0, ip = name, op = outname, *outlen = 0;
573                              i < len && *outlen <= 260; *outlen += 1)
574                         {
575                                 if (escape && (*ip == ':')) {
576                                         if (i > len - 5)
577                                                 return -EINVAL;
578                                         ec = 0;
579                                         for (k = 1; k < 5; k++) {
580                                                 nc = ip[k];
581                                                 ec <<= 4;
582                                                 if (nc >= '0' && nc <= '9') {
583                                                         ec |= nc - '0';
584                                                         continue;
585                                                 }
586                                                 if (nc >= 'a' && nc <= 'f') {
587                                                         ec |= nc - ('a' - 10);
588                                                         continue;
589                                                 }
590                                                 if (nc >= 'A' && nc <= 'F') {
591                                                         ec |= nc - ('A' - 10);
592                                                         continue;
593                                                 }
594                                                 return -EINVAL;
595                                         }
596                                         *op++ = ec & 0xFF;
597                                         *op++ = ec >> 8;
598                                         ip += 5;
599                                         i += 5;
600                                 } else {
601                                         if ((charlen = nls->char2uni(ip, len-i, (wchar_t *)op)) < 0)
602                                                 return -EINVAL;
603                                         ip += charlen;
604                                         i += charlen;
605                                         op += 2;
606                                 }
607                         }
608                 } else {
609                         for (i = 0, ip = name, op = outname, *outlen = 0;
610                              i < len && *outlen <= 260; i++, *outlen += 1)
611                         {
612                                 *op++ = *ip++;
613                                 *op++ = 0;
614                         }
615                 }
616         }
617         if (*outlen > 260)
618                 return -ENAMETOOLONG;
619
620         *longlen = *outlen;
621         if (*outlen % 13) {
622                 *op++ = 0;
623                 *op++ = 0;
624                 *outlen += 1;
625                 if (*outlen % 13) {
626                         fill = 13 - (*outlen % 13);
627                         for (i = 0; i < fill; i++) {
628                                 *op++ = 0xff;
629                                 *op++ = 0xff;
630                         }
631                         *outlen += fill;
632                 }
633         }
634
635         return 0;
636 }
637
638 static int vfat_build_slots(struct inode *dir, const unsigned char *name,
639                             int len, struct msdos_dir_slot *ds,
640                             int *slots, int is_dir)
641 {
642         struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
643         struct fat_mount_options *opts = &sbi->options;
644         struct msdos_dir_slot *ps;
645         struct msdos_dir_entry *de;
646         unsigned long page;
647         unsigned char cksum, lcase;
648         unsigned char msdos_name[MSDOS_NAME];
649         wchar_t *uname;
650         int res, slot, ulen, usize, i;
651         loff_t offset;
652
653         *slots = 0;
654         if (!vfat_valid_longname(name, len))
655                 return -EINVAL;
656
657         if(!(page = __get_free_page(GFP_KERNEL)))
658                 return -ENOMEM;
659
660         uname = (wchar_t *)page;
661         res = xlate_to_uni(name, len, (unsigned char *)uname, &ulen, &usize,
662                            opts->unicode_xlate, opts->utf8, sbi->nls_io);
663         if (res < 0)
664                 goto out_free;
665
666         res = vfat_is_used_badchars(uname, ulen);
667         if (res < 0)
668                 goto out_free;
669
670         res = vfat_create_shortname(dir, sbi->nls_disk, uname, ulen,
671                                     msdos_name, &lcase);
672         if (res < 0)
673                 goto out_free;
674         else if (res == 1) {
675                 de = (struct msdos_dir_entry *)ds;
676                 res = 0;
677                 goto shortname;
678         }
679
680         /* build the entry of long file name */
681         *slots = usize / 13;
682         for (cksum = i = 0; i < 11; i++) {
683                 cksum = (((cksum&1)<<7)|((cksum&0xfe)>>1)) + msdos_name[i];
684         }
685
686         for (ps = ds, slot = *slots; slot > 0; slot--, ps++) {
687                 ps->id = slot;
688                 ps->attr = ATTR_EXT;
689                 ps->reserved = 0;
690                 ps->alias_checksum = cksum;
691                 ps->start = 0;
692                 offset = (slot - 1) * 13;
693                 fatwchar_to16(ps->name0_4, uname + offset, 5);
694                 fatwchar_to16(ps->name5_10, uname + offset + 5, 6);
695                 fatwchar_to16(ps->name11_12, uname + offset + 11, 2);
696         }
697         ds[0].id |= 0x40;
698         de = (struct msdos_dir_entry *) ps;
699
700 shortname:
701         /* build the entry of 8.3 alias name */
702         (*slots)++;
703         memcpy(de->name, msdos_name, MSDOS_NAME);
704         de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
705         de->lcase = lcase;
706         de->adate = de->cdate = de->date = 0;
707         de->ctime_ms = de->ctime = de->time = 0;
708         de->start = 0;
709         de->starthi = 0;
710         de->size = 0;
711
712 out_free:
713         free_page(page);
714         return res;
715 }
716
717 static int vfat_add_entry(struct inode *dir,struct qstr* qname,
718                           int is_dir, struct vfat_slot_info *sinfo_out,
719                           struct buffer_head **bh, struct msdos_dir_entry **de)
720 {
721         struct msdos_dir_slot *dir_slots;
722         loff_t offset;
723         int res, slots, slot;
724         unsigned int len;
725         struct msdos_dir_entry *dummy_de;
726         struct buffer_head *dummy_bh;
727         loff_t dummy_i_pos;
728
729         len = vfat_striptail_len(qname);
730         if (len == 0)
731                 return -ENOENT;
732
733         dir_slots =
734                kmalloc(sizeof(struct msdos_dir_slot) * MSDOS_SLOTS, GFP_KERNEL);
735         if (dir_slots == NULL)
736                 return -ENOMEM;
737
738         res = vfat_build_slots(dir, qname->name, len,
739                                dir_slots, &slots, is_dir);
740         if (res < 0)
741                 goto cleanup;
742
743         /* build the empty directory entry of number of slots */
744         offset = fat_add_entries(dir, slots, &dummy_bh, &dummy_de, &dummy_i_pos);
745         if (offset < 0) {
746                 res = offset;
747                 goto cleanup;
748         }
749         brelse(dummy_bh);
750
751         /* Now create the new entry */
752         *bh = NULL;
753         for (slot = 0; slot < slots; slot++) {
754                 if (fat_get_entry(dir, &offset, bh, de, &sinfo_out->i_pos) < 0) {
755                         res = -EIO;
756                         goto cleanup;
757                 }
758                 memcpy(*de, dir_slots + slot, sizeof(struct msdos_dir_slot));
759                 mark_buffer_dirty(*bh);
760         }
761
762         res = 0;
763         /* update timestamp */
764         dir->i_ctime = dir->i_mtime = dir->i_atime = CURRENT_TIME;
765         mark_inode_dirty(dir);
766
767         fat_date_unix2dos(dir->i_mtime.tv_sec, &(*de)->time, &(*de)->date);
768         (*de)->ctime = (*de)->time;
769         (*de)->adate = (*de)->cdate = (*de)->date;
770
771         mark_buffer_dirty(*bh);
772
773         /* slots can't be less than 1 */
774         sinfo_out->long_slots = slots - 1;
775         sinfo_out->longname_offset =
776                 offset - sizeof(struct msdos_dir_slot) * slots;
777
778 cleanup:
779         kfree(dir_slots);
780         return res;
781 }
782
783 static int vfat_find(struct inode *dir,struct qstr* qname,
784         struct vfat_slot_info *sinfo, struct buffer_head **last_bh,
785         struct msdos_dir_entry **last_de)
786 {
787         struct super_block *sb = dir->i_sb;
788         loff_t offset;
789         unsigned int len;
790         int res;
791
792         len = vfat_striptail_len(qname);
793         if (len == 0)
794                 return -ENOENT;
795
796         res = fat_search_long(dir, qname->name, len,
797                               (MSDOS_SB(sb)->options.name_check != 's'),
798                               &offset, &sinfo->longname_offset);
799         if (res>0) {
800                 sinfo->long_slots = res-1;
801                 if (fat_get_entry(dir,&offset,last_bh,last_de,&sinfo->i_pos)>=0)
802                         return 0;
803                 res = -EIO;
804         } 
805         return res ? res : -ENOENT;
806 }
807
808 static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry,
809                 struct nameidata *nd)
810 {
811         int res;
812         struct vfat_slot_info sinfo;
813         struct inode *inode;
814         struct dentry *alias;
815         struct buffer_head *bh = NULL;
816         struct msdos_dir_entry *de;
817         int table;
818         
819         lock_kernel();
820         table = (MSDOS_SB(dir->i_sb)->options.name_check == 's') ? 2 : 0;
821         dentry->d_op = &vfat_dentry_ops[table];
822
823         inode = NULL;
824         res = vfat_find(dir,&dentry->d_name,&sinfo,&bh,&de);
825         if (res < 0) {
826                 table++;
827                 goto error;
828         }
829         inode = fat_build_inode(dir->i_sb, de, sinfo.i_pos, &res);
830         brelse(bh);
831         if (res) {
832                 unlock_kernel();
833                 return ERR_PTR(res);
834         }
835         alias = d_find_alias(inode);
836         if (alias) {
837                 if (d_invalidate(alias)==0)
838                         dput(alias);
839                 else {
840                         iput(inode);
841                         unlock_kernel();
842                         return alias;
843                 }
844                 
845         }
846 error:
847         unlock_kernel();
848         dentry->d_op = &vfat_dentry_ops[table];
849         dentry->d_time = dentry->d_parent->d_inode->i_version;
850         dentry = d_splice_alias(inode, dentry);
851         if (dentry) {
852                 dentry->d_op = &vfat_dentry_ops[table];
853                 dentry->d_time = dentry->d_parent->d_inode->i_version;
854         }
855         return dentry;
856 }
857
858 static int vfat_create(struct inode *dir, struct dentry* dentry, int mode,
859                 struct nameidata *nd)
860 {
861         struct super_block *sb = dir->i_sb;
862         struct inode *inode = NULL;
863         struct buffer_head *bh = NULL;
864         struct msdos_dir_entry *de;
865         struct vfat_slot_info sinfo;
866         int res;
867
868         lock_kernel();
869         res = vfat_add_entry(dir, &dentry->d_name, 0, &sinfo, &bh, &de);
870         if (res < 0)
871                 goto out;
872         inode = fat_build_inode(sb, de, sinfo.i_pos, &res);
873         brelse(bh);
874         if (!inode)
875                 goto out;
876         res = 0;
877         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
878         mark_inode_dirty(inode);
879         inode->i_version++;
880         dir->i_version++;
881         dentry->d_time = dentry->d_parent->d_inode->i_version;
882         d_instantiate(dentry,inode);
883 out:
884         unlock_kernel();
885         return res;
886 }
887
888 static void vfat_remove_entry(struct inode *dir,struct vfat_slot_info *sinfo,
889      struct buffer_head *bh, struct msdos_dir_entry *de)
890 {
891         loff_t offset, i_pos;
892         int i;
893
894         /* remove the shortname */
895         dir->i_mtime = dir->i_atime = CURRENT_TIME;
896         dir->i_version++;
897         mark_inode_dirty(dir);
898         de->name[0] = DELETED_FLAG;
899         mark_buffer_dirty(bh);
900         /* remove the longname */
901         offset = sinfo->longname_offset; de = NULL;
902         for (i = sinfo->long_slots; i > 0; --i) {
903                 if (fat_get_entry(dir, &offset, &bh, &de, &i_pos) < 0)
904                         continue;
905                 de->name[0] = DELETED_FLAG;
906                 de->attr = ATTR_NONE;
907                 mark_buffer_dirty(bh);
908         }
909         brelse(bh);
910 }
911
912 static int vfat_rmdir(struct inode *dir, struct dentry* dentry)
913 {
914         int res;
915         struct vfat_slot_info sinfo;
916         struct buffer_head *bh = NULL;
917         struct msdos_dir_entry *de;
918
919         lock_kernel();
920         res = fat_dir_empty(dentry->d_inode);
921         if (res)
922                 goto out;
923
924         res = vfat_find(dir,&dentry->d_name,&sinfo, &bh, &de);
925         if (res < 0)
926                 goto out;
927         res = 0;
928         dentry->d_inode->i_nlink = 0;
929         dentry->d_inode->i_mtime = dentry->d_inode->i_atime = CURRENT_TIME;
930         fat_detach(dentry->d_inode);
931         mark_inode_dirty(dentry->d_inode);
932         /* releases bh */
933         vfat_remove_entry(dir,&sinfo,bh,de);
934         dir->i_nlink--;
935 out:
936         unlock_kernel();
937         return res;
938 }
939
940 static int vfat_unlink(struct inode *dir, struct dentry *dentry)
941 {
942         int res;
943         struct vfat_slot_info sinfo;
944         struct buffer_head *bh = NULL;
945         struct msdos_dir_entry *de;
946
947         lock_kernel();
948         res = vfat_find(dir,&dentry->d_name,&sinfo,&bh,&de);
949         if (res < 0)
950                 goto out;
951         dentry->d_inode->i_nlink = 0;
952         dentry->d_inode->i_mtime = dentry->d_inode->i_atime = CURRENT_TIME;
953         fat_detach(dentry->d_inode);
954         mark_inode_dirty(dentry->d_inode);
955         /* releases bh */
956         vfat_remove_entry(dir,&sinfo,bh,de);
957 out:
958         unlock_kernel();
959
960         return res;
961 }
962
963 static int vfat_mkdir(struct inode *dir,struct dentry* dentry,int mode)
964 {
965         struct super_block *sb = dir->i_sb;
966         struct inode *inode = NULL;
967         struct vfat_slot_info sinfo;
968         struct buffer_head *bh = NULL;
969         struct msdos_dir_entry *de;
970         int res;
971
972         lock_kernel();
973         res = vfat_add_entry(dir, &dentry->d_name, 1, &sinfo, &bh, &de);
974         if (res < 0)
975                 goto out;
976         inode = fat_build_inode(sb, de, sinfo.i_pos, &res);
977         if (!inode)
978                 goto out_brelse;
979         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
980         mark_inode_dirty(inode);
981         inode->i_version++;
982         dir->i_version++;
983         dir->i_nlink++;
984         inode->i_nlink = 2; /* no need to mark them dirty */
985         res = fat_new_dir(inode, dir, 1);
986         if (res < 0)
987                 goto mkdir_failed;
988         dentry->d_time = dentry->d_parent->d_inode->i_version;
989         d_instantiate(dentry,inode);
990 out_brelse:
991         brelse(bh);
992 out:
993         unlock_kernel();
994         return res;
995
996 mkdir_failed:
997         inode->i_nlink = 0;
998         inode->i_mtime = inode->i_atime = CURRENT_TIME;
999         fat_detach(inode);
1000         mark_inode_dirty(inode);
1001         /* releases bh */
1002         vfat_remove_entry(dir,&sinfo,bh,de);
1003         iput(inode);
1004         dir->i_nlink--;
1005         goto out;
1006 }
1007  
1008 static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
1009                 struct inode *new_dir, struct dentry *new_dentry)
1010 {
1011         struct buffer_head *old_bh,*new_bh,*dotdot_bh;
1012         struct msdos_dir_entry *old_de,*new_de,*dotdot_de;
1013         loff_t dotdot_i_pos;
1014         struct inode *old_inode, *new_inode;
1015         int res, is_dir;
1016         struct vfat_slot_info old_sinfo,sinfo;
1017
1018         old_bh = new_bh = dotdot_bh = NULL;
1019         old_inode = old_dentry->d_inode;
1020         new_inode = new_dentry->d_inode;
1021         lock_kernel();
1022         res = vfat_find(old_dir,&old_dentry->d_name,&old_sinfo,&old_bh,&old_de);
1023         if (res < 0)
1024                 goto rename_done;
1025
1026         is_dir = S_ISDIR(old_inode->i_mode);
1027
1028         if (is_dir) {
1029                 if (fat_scan(old_inode, MSDOS_DOTDOT, &dotdot_bh,
1030                              &dotdot_de, &dotdot_i_pos) < 0) {
1031                         res = -EIO;
1032                         goto rename_done;
1033                 }
1034         }
1035
1036         if (new_dentry->d_inode) {
1037                 res = vfat_find(new_dir,&new_dentry->d_name,&sinfo,&new_bh,
1038                                 &new_de);
1039                 if (res < 0 || MSDOS_I(new_inode)->i_pos != sinfo.i_pos) {
1040                         /* WTF??? Cry and fail. */
1041                         printk(KERN_WARNING "vfat_rename: fs corrupted\n");
1042                         goto rename_done;
1043                 }
1044
1045                 if (is_dir) {
1046                         res = fat_dir_empty(new_inode);
1047                         if (res)
1048                                 goto rename_done;
1049                 }
1050                 fat_detach(new_inode);
1051         } else {
1052                 res = vfat_add_entry(new_dir,&new_dentry->d_name,is_dir,&sinfo,
1053                                         &new_bh,&new_de);
1054                 if (res < 0) goto rename_done;
1055         }
1056
1057         new_dir->i_version++;
1058
1059         /* releases old_bh */
1060         vfat_remove_entry(old_dir,&old_sinfo,old_bh,old_de);
1061         old_bh=NULL;
1062         fat_detach(old_inode);
1063         fat_attach(old_inode, sinfo.i_pos);
1064         mark_inode_dirty(old_inode);
1065
1066         old_dir->i_version++;
1067         old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1068         mark_inode_dirty(old_dir);
1069         if (new_inode) {
1070                 new_inode->i_nlink--;
1071                 new_inode->i_ctime=CURRENT_TIME;
1072         }
1073
1074         if (is_dir) {
1075                 int start = MSDOS_I(new_dir)->i_logstart;
1076                 dotdot_de->start = CT_LE_W(start);
1077                 dotdot_de->starthi = CT_LE_W(start>>16);
1078                 mark_buffer_dirty(dotdot_bh);
1079                 old_dir->i_nlink--;
1080                 if (new_inode) {
1081                         new_inode->i_nlink--;
1082                 } else {
1083                         new_dir->i_nlink++;
1084                         mark_inode_dirty(new_dir);
1085                 }
1086         }
1087
1088 rename_done:
1089         brelse(dotdot_bh);
1090         brelse(old_bh);
1091         brelse(new_bh);
1092         unlock_kernel();
1093         return res;
1094
1095 }
1096
1097 static struct inode_operations vfat_dir_inode_operations = {
1098         .create         = vfat_create,
1099         .lookup         = vfat_lookup,
1100         .unlink         = vfat_unlink,
1101         .mkdir          = vfat_mkdir,
1102         .rmdir          = vfat_rmdir,
1103         .rename         = vfat_rename,
1104         .setattr        = fat_notify_change,
1105 };
1106
1107 static int vfat_fill_super(struct super_block *sb, void *data, int silent)
1108 {
1109         int res;
1110
1111         res = fat_fill_super(sb, data, silent, &vfat_dir_inode_operations, 1);
1112         if (res)
1113                 return res;
1114
1115         if (MSDOS_SB(sb)->options.name_check != 's')
1116                 sb->s_root->d_op = &vfat_dentry_ops[0];
1117         else
1118                 sb->s_root->d_op = &vfat_dentry_ops[2];
1119
1120         return 0;
1121 }
1122
1123 static struct super_block *vfat_get_sb(struct file_system_type *fs_type,
1124         int flags, const char *dev_name, void *data)
1125 {
1126         return get_sb_bdev(fs_type, flags, dev_name, data, vfat_fill_super);
1127 }
1128
1129 static struct file_system_type vfat_fs_type = {
1130         .owner          = THIS_MODULE,
1131         .name           = "vfat",
1132         .get_sb         = vfat_get_sb,
1133         .kill_sb        = kill_block_super,
1134         .fs_flags       = FS_REQUIRES_DEV,
1135 };
1136
1137 static int __init init_vfat_fs(void)
1138 {
1139         return register_filesystem(&vfat_fs_type);
1140 }
1141
1142 static void __exit exit_vfat_fs(void)
1143 {
1144         unregister_filesystem(&vfat_fs_type);
1145 }
1146
1147 MODULE_LICENSE("GPL");
1148 MODULE_DESCRIPTION("VFAT filesystem support");
1149 MODULE_AUTHOR("Gordon Chaffee");
1150
1151 module_init(init_vfat_fs)
1152 module_exit(exit_vfat_fs)