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