patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / fat / inode.c
1 /*
2  *  linux/fs/fat/inode.c
3  *
4  *  Written 1992,1993 by Werner Almesberger
5  *  VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
6  *  Rewritten for the constant inumbers support by Al Viro
7  *
8  *  Fixes:
9  *
10  *      Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
11  */
12
13 #include <linux/module.h>
14 #include <linux/time.h>
15 #include <linux/slab.h>
16 #include <linux/smp_lock.h>
17 #include <linux/seq_file.h>
18 #include <linux/msdos_fs.h>
19 #include <linux/pagemap.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <linux/parser.h>
24 #include <asm/unaligned.h>
25
26 /*
27  * New FAT inode stuff. We do the following:
28  *      a) i_ino is constant and has nothing with on-disk location.
29  *      b) FAT manages its own cache of directory entries.
30  *      c) *This* cache is indexed by on-disk location.
31  *      d) inode has an associated directory entry, all right, but
32  *              it may be unhashed.
33  *      e) currently entries are stored within struct inode. That should
34  *              change.
35  *      f) we deal with races in the following way:
36  *              1. readdir() and lookup() do FAT-dir-cache lookup.
37  *              2. rename() unhashes the F-d-c entry and rehashes it in
38  *                      a new place.
39  *              3. unlink() and rmdir() unhash F-d-c entry.
40  *              4. fat_write_inode() checks whether the thing is unhashed.
41  *                      If it is we silently return. If it isn't we do bread(),
42  *                      check if the location is still valid and retry if it
43  *                      isn't. Otherwise we do changes.
44  *              5. Spinlock is used to protect hash/unhash/location check/lookup
45  *              6. fat_clear_inode() unhashes the F-d-c entry.
46  *              7. lookup() and readdir() do igrab() if they find a F-d-c entry
47  *                      and consider negative result as cache miss.
48  */
49
50 #define FAT_HASH_BITS   8
51 #define FAT_HASH_SIZE   (1UL << FAT_HASH_BITS)
52 #define FAT_HASH_MASK   (FAT_HASH_SIZE-1)
53 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
54 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
55
56 void fat_hash_init(void)
57 {
58         int i;
59         for(i = 0; i < FAT_HASH_SIZE; i++) {
60                 INIT_LIST_HEAD(&fat_inode_hashtable[i]);
61         }
62 }
63
64 static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
65 {
66         unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
67         tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
68         return tmp & FAT_HASH_MASK;
69 }
70
71 void fat_attach(struct inode *inode, loff_t i_pos)
72 {
73         spin_lock(&fat_inode_lock);
74         MSDOS_I(inode)->i_pos = i_pos;
75         list_add(&MSDOS_I(inode)->i_fat_hash,
76                 fat_inode_hashtable + fat_hash(inode->i_sb, i_pos));
77         spin_unlock(&fat_inode_lock);
78 }
79
80 void fat_detach(struct inode *inode)
81 {
82         spin_lock(&fat_inode_lock);
83         MSDOS_I(inode)->i_pos = 0;
84         list_del_init(&MSDOS_I(inode)->i_fat_hash);
85         spin_unlock(&fat_inode_lock);
86 }
87
88 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
89 {
90         struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
91         struct list_head *walk;
92         struct msdos_inode_info *i;
93         struct inode *inode = NULL;
94
95         spin_lock(&fat_inode_lock);
96         list_for_each(walk, p) {
97                 i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
98                 if (i->vfs_inode.i_sb != sb)
99                         continue;
100                 if (i->i_pos != i_pos)
101                         continue;
102                 inode = igrab(&i->vfs_inode);
103                 if (inode)
104                         break;
105         }
106         spin_unlock(&fat_inode_lock);
107         return inode;
108 }
109
110 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
111
112 struct inode *fat_build_inode(struct super_block *sb,
113                         struct msdos_dir_entry *de, loff_t i_pos, int *res)
114 {
115         struct inode *inode;
116         *res = 0;
117         inode = fat_iget(sb, i_pos);
118         if (inode)
119                 goto out;
120         inode = new_inode(sb);
121         *res = -ENOMEM;
122         if (!inode)
123                 goto out;
124         inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
125         inode->i_version = 1;
126         *res = fat_fill_inode(inode, de);
127         if (*res < 0) {
128                 iput(inode);
129                 inode = NULL;
130                 goto out;
131         }
132         fat_attach(inode, i_pos);
133         insert_inode_hash(inode);
134 out:
135         return inode;
136 }
137
138 void fat_delete_inode(struct inode *inode)
139 {
140         if (!is_bad_inode(inode)) {
141                 inode->i_size = 0;
142                 fat_truncate(inode);
143         }
144         clear_inode(inode);
145 }
146
147 void fat_clear_inode(struct inode *inode)
148 {
149         if (is_bad_inode(inode))
150                 return;
151         lock_kernel();
152         spin_lock(&fat_inode_lock);
153         fat_cache_inval_inode(inode);
154         list_del_init(&MSDOS_I(inode)->i_fat_hash);
155         spin_unlock(&fat_inode_lock);
156         unlock_kernel();
157 }
158
159 void fat_put_super(struct super_block *sb)
160 {
161         struct msdos_sb_info *sbi = MSDOS_SB(sb);
162
163         if (!(sb->s_flags & MS_RDONLY))
164                 fat_clusters_flush(sb);
165
166         if (sbi->nls_disk) {
167                 unload_nls(sbi->nls_disk);
168                 sbi->nls_disk = NULL;
169                 sbi->options.codepage = 0;
170         }
171         if (sbi->nls_io) {
172                 unload_nls(sbi->nls_io);
173                 sbi->nls_io = NULL;
174         }
175         /*
176          * Note: the iocharset option might have been specified
177          * without enabling nls_io, so check for it here.
178          */
179         if (sbi->options.iocharset) {
180                 kfree(sbi->options.iocharset);
181                 sbi->options.iocharset = NULL;
182         }
183         sb->s_fs_info = NULL;
184         kfree(sbi);
185 }
186
187 static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
188 {
189         struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
190         struct fat_mount_options *opts = &sbi->options;
191         int isvfat = opts->isvfat;
192
193         if (opts->fs_uid != 0)
194                 seq_printf(m, ",uid=%u", opts->fs_uid);
195         if (opts->fs_gid != 0)
196                 seq_printf(m, ",gid=%u", opts->fs_gid);
197         seq_printf(m, ",fmask=%04o", opts->fs_fmask);
198         seq_printf(m, ",dmask=%04o", opts->fs_dmask);
199         if (sbi->nls_disk)
200                 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
201         if (isvfat) {
202                 if (sbi->nls_io
203                     && strcmp(sbi->nls_io->charset, CONFIG_NLS_DEFAULT))
204                         seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
205
206                 switch (opts->shortname) {
207                 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
208                         seq_puts(m, ",shortname=win95");
209                         break;
210                 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
211                         seq_puts(m, ",shortname=winnt");
212                         break;
213                 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
214                         seq_puts(m, ",shortname=mixed");
215                         break;
216                 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
217                         /* seq_puts(m, ",shortname=lower"); */
218                         break;
219                 default:
220                         seq_puts(m, ",shortname=unknown");
221                         break;
222                 }
223         }
224         if (opts->name_check != 'n')
225                 seq_printf(m, ",check=%c", opts->name_check);
226         if (opts->quiet)
227                 seq_puts(m, ",quiet");
228         if (opts->showexec)
229                 seq_puts(m, ",showexec");
230         if (opts->sys_immutable)
231                 seq_puts(m, ",sys_immutable");
232         if (!isvfat) {
233                 if (opts->dotsOK)
234                         seq_puts(m, ",dotsOK=yes");
235                 if (opts->nocase)
236                         seq_puts(m, ",nocase");
237         } else {
238                 if (opts->utf8)
239                         seq_puts(m, ",utf8");
240                 if (opts->unicode_xlate)
241                         seq_puts(m, ",uni_xlate");
242                 if (!opts->numtail)
243                         seq_puts(m, ",nonumtail");
244         }
245
246         return 0;
247 }
248
249 enum {
250         Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
251         Opt_umask, Opt_dmask, Opt_fmask, Opt_codepage, Opt_nocase,
252         Opt_quiet, Opt_showexec, Opt_debug, Opt_immutable,
253         Opt_dots, Opt_nodots,
254         Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
255         Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
256         Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
257         Opt_obsolate, Opt_err,
258 };
259
260 static match_table_t fat_tokens = {
261         {Opt_check_r, "check=relaxed"},
262         {Opt_check_s, "check=strict"},
263         {Opt_check_n, "check=normal"},
264         {Opt_check_r, "check=r"},
265         {Opt_check_s, "check=s"},
266         {Opt_check_n, "check=n"},
267         {Opt_uid, "uid=%u"},
268         {Opt_gid, "gid=%u"},
269         {Opt_umask, "umask=%o"},
270         {Opt_dmask, "dmask=%o"},
271         {Opt_fmask, "fmask=%o"},
272         {Opt_codepage, "codepage=%u"},
273         {Opt_nocase, "nocase"},
274         {Opt_quiet, "quiet"},
275         {Opt_showexec, "showexec"},
276         {Opt_debug, "debug"},
277         {Opt_immutable, "sys_immutable"},
278         {Opt_obsolate, "conv=binary"},
279         {Opt_obsolate, "conv=text"},
280         {Opt_obsolate, "conv=auto"},
281         {Opt_obsolate, "conv=b"},
282         {Opt_obsolate, "conv=t"},
283         {Opt_obsolate, "conv=a"},
284         {Opt_obsolate, "fat=%u"},
285         {Opt_obsolate, "blocksize=%u"},
286         {Opt_obsolate, "cvf_format=%20s"},
287         {Opt_obsolate, "cvf_options=%100s"},
288         {Opt_obsolate, "posix"},
289         {Opt_err, NULL}
290 };
291 static match_table_t msdos_tokens = {
292         {Opt_nodots, "nodots"},
293         {Opt_nodots, "dotsOK=no"},
294         {Opt_dots, "dots"},
295         {Opt_dots, "dotsOK=yes"},
296         {Opt_err, NULL}
297 };
298 static match_table_t vfat_tokens = {
299         {Opt_charset, "iocharset=%s"},
300         {Opt_shortname_lower, "shortname=lower"},
301         {Opt_shortname_win95, "shortname=win95"},
302         {Opt_shortname_winnt, "shortname=winnt"},
303         {Opt_shortname_mixed, "shortname=mixed"},
304         {Opt_utf8_no, "utf8=0"},                /* 0 or no or false */
305         {Opt_utf8_no, "utf8=no"},
306         {Opt_utf8_no, "utf8=false"},
307         {Opt_utf8_yes, "utf8=1"},               /* empty or 1 or yes or true */
308         {Opt_utf8_yes, "utf8=yes"},
309         {Opt_utf8_yes, "utf8=true"},
310         {Opt_utf8_yes, "utf8"},
311         {Opt_uni_xl_no, "uni_xlate=0"},         /* 0 or no or false */
312         {Opt_uni_xl_no, "uni_xlate=no"},
313         {Opt_uni_xl_no, "uni_xlate=false"},
314         {Opt_uni_xl_yes, "uni_xlate=1"},        /* empty or 1 or yes or true */
315         {Opt_uni_xl_yes, "uni_xlate=yes"},
316         {Opt_uni_xl_yes, "uni_xlate=true"},
317         {Opt_uni_xl_yes, "uni_xlate"},
318         {Opt_nonumtail_no, "nonumtail=0"},      /* 0 or no or false */
319         {Opt_nonumtail_no, "nonumtail=no"},
320         {Opt_nonumtail_no, "nonumtail=false"},
321         {Opt_nonumtail_yes, "nonumtail=1"},     /* empty or 1 or yes or true */
322         {Opt_nonumtail_yes, "nonumtail=yes"},
323         {Opt_nonumtail_yes, "nonumtail=true"},
324         {Opt_nonumtail_yes, "nonumtail"},
325         {Opt_err, NULL}
326 };
327
328 static int parse_options(char *options, int is_vfat, int *debug,
329                          struct fat_mount_options *opts)
330 {
331         char *p;
332         substring_t args[MAX_OPT_ARGS];
333         int option;
334
335         opts->isvfat = is_vfat;
336
337         opts->fs_uid = current->uid;
338         opts->fs_gid = current->gid;
339         opts->fs_fmask = opts->fs_dmask = current->fs->umask;
340         opts->codepage = 0;
341         opts->iocharset = NULL;
342         if (is_vfat)
343                 opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
344         else
345                 opts->shortname = 0;
346         opts->name_check = 'n';
347         opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK =  0;
348         opts->utf8 = opts->unicode_xlate = 0;
349         opts->numtail = 1;
350         opts->nocase = 0;
351         *debug = 0;
352
353         if (!options)
354                 return 1;
355
356         while ((p = strsep(&options, ",")) != NULL) {
357                 int token;
358                 if (!*p)
359                         continue;
360
361                 token = match_token(p, fat_tokens, args);
362                 if (token == Opt_err) {
363                         if (is_vfat)
364                                 token = match_token(p, vfat_tokens, args);
365                         else
366                                 token = match_token(p, msdos_tokens, args);
367                 }
368                 switch (token) {
369                 case Opt_check_s:
370                         opts->name_check = 's';
371                         break;
372                 case Opt_check_r:
373                         opts->name_check = 'r';
374                         break;
375                 case Opt_check_n:
376                         opts->name_check = 'n';
377                         break;
378                 case Opt_nocase:
379                         if (!is_vfat)
380                                 opts->nocase = 1;
381                         else {
382                                 /* for backward compatibility */
383                                 opts->shortname = VFAT_SFN_DISPLAY_WIN95
384                                         | VFAT_SFN_CREATE_WIN95;
385                         }
386                         break;
387                 case Opt_quiet:
388                         opts->quiet = 1;
389                         break;
390                 case Opt_showexec:
391                         opts->showexec = 1;
392                         break;
393                 case Opt_debug:
394                         *debug = 1;
395                         break;
396                 case Opt_immutable:
397                         opts->sys_immutable = 1;
398                         break;
399                 case Opt_uid:
400                         if (match_int(&args[0], &option))
401                                 return 0;
402                         opts->fs_uid = option;
403                         break;
404                 case Opt_gid:
405                         if (match_int(&args[0], &option))
406                                 return 0;
407                         opts->fs_gid = option;
408                         break;
409                 case Opt_umask:
410                         if (match_octal(&args[0], &option))
411                                 return 0;
412                         opts->fs_fmask = opts->fs_dmask = option;
413                         break;
414                 case Opt_dmask:
415                         if (match_octal(&args[0], &option))
416                                 return 0;
417                         opts->fs_dmask = option;
418                         break;
419                 case Opt_fmask:
420                         if (match_octal(&args[0], &option))
421                                 return 0;
422                         opts->fs_fmask = option;
423                         break;
424                 case Opt_codepage:
425                         if (match_int(&args[0], &option))
426                                 return 0;
427                         opts->codepage = option;
428                         break;
429
430                 /* msdos specific */
431                 case Opt_dots:
432                         opts->dotsOK = 1;
433                         break;
434                 case Opt_nodots:
435                         opts->dotsOK = 0;
436                         break;
437
438                 /* vfat specific */
439                 case Opt_charset:
440                         kfree(opts->iocharset);
441                         opts->iocharset = match_strdup(&args[0]);
442                         if (!opts->iocharset)
443                                 return 0;
444                         break;
445                 case Opt_shortname_lower:
446                         opts->shortname = VFAT_SFN_DISPLAY_LOWER
447                                         | VFAT_SFN_CREATE_WIN95;
448                         break;
449                 case Opt_shortname_win95:
450                         opts->shortname = VFAT_SFN_DISPLAY_WIN95
451                                         | VFAT_SFN_CREATE_WIN95;
452                         break;
453                 case Opt_shortname_winnt:
454                         opts->shortname = VFAT_SFN_DISPLAY_WINNT
455                                         | VFAT_SFN_CREATE_WINNT;
456                         break;
457                 case Opt_shortname_mixed:
458                         opts->shortname = VFAT_SFN_DISPLAY_WINNT
459                                         | VFAT_SFN_CREATE_WIN95;
460                         break;
461                 case Opt_utf8_no:               /* 0 or no or false */
462                         opts->utf8 = 0;
463                         break;
464                 case Opt_utf8_yes:              /* empty or 1 or yes or true */
465                         opts->utf8 = 1;
466                         break;
467                 case Opt_uni_xl_no:             /* 0 or no or false */
468                         opts->unicode_xlate = 0;
469                         break;
470                 case Opt_uni_xl_yes:            /* empty or 1 or yes or true */
471                         opts->unicode_xlate = 1;
472                         break;
473                 case Opt_nonumtail_no:          /* 0 or no or false */
474                         opts->numtail = 1;      /* negated option */
475                         break;
476                 case Opt_nonumtail_yes:         /* empty or 1 or yes or true */
477                         opts->numtail = 0;      /* negated option */
478                         break;
479
480                 /* obsolete mount options */
481                 case Opt_obsolate:
482                         printk(KERN_INFO "FAT: \"%s\" option is obsolete, "
483                                "not supported now\n", p);
484                         break;
485                 /* unknown option */
486                 default:
487                         printk(KERN_ERR "FAT: Unrecognized mount option \"%s\" "
488                                "or missing value\n", p);
489                         return 0;
490                 }
491         }
492
493         if (opts->unicode_xlate)
494                 opts->utf8 = 0;
495         
496         return 1;
497 }
498
499 static int fat_calc_dir_size(struct inode *inode)
500 {
501         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
502         int ret, fclus, dclus;
503
504         inode->i_size = 0;
505         if (MSDOS_I(inode)->i_start == 0)
506                 return 0;
507
508         ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
509         if (ret < 0)
510                 return ret;
511         inode->i_size = (fclus + 1) << sbi->cluster_bits;
512
513         return 0;
514 }
515
516 static int fat_read_root(struct inode *inode)
517 {
518         struct super_block *sb = inode->i_sb;
519         struct msdos_sb_info *sbi = MSDOS_SB(sb);
520         int error;
521
522         MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
523         MSDOS_I(inode)->i_pos = 0;
524         inode->i_uid = sbi->options.fs_uid;
525         inode->i_gid = sbi->options.fs_gid;
526         inode->i_version++;
527         inode->i_generation = 0;
528         inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
529         inode->i_op = sbi->dir_ops;
530         inode->i_fop = &fat_dir_operations;
531         if (sbi->fat_bits == 32) {
532                 MSDOS_I(inode)->i_start = sbi->root_cluster;
533                 error = fat_calc_dir_size(inode);
534                 if (error < 0)
535                         return error;
536         } else {
537                 MSDOS_I(inode)->i_start = 0;
538                 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
539         }
540         inode->i_blksize = sbi->cluster_size;
541         inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
542                            & ~((loff_t)sbi->cluster_size - 1)) >> 9;
543         MSDOS_I(inode)->i_logstart = 0;
544         MSDOS_I(inode)->mmu_private = inode->i_size;
545
546         MSDOS_I(inode)->i_attrs = 0;
547         inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
548         inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
549         MSDOS_I(inode)->i_ctime_ms = 0;
550         inode->i_nlink = fat_subdirs(inode)+2;
551
552         return 0;
553 }
554
555 /*
556  * a FAT file handle with fhtype 3 is
557  *  0/  i_ino - for fast, reliable lookup if still in the cache
558  *  1/  i_generation - to see if i_ino is still valid
559  *          bit 0 == 0 iff directory
560  *  2/  i_pos(8-39) - if ino has changed, but still in cache
561  *  3/  i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
562  *  4/  i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
563  *
564  * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
565  * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
566  * of i_logstart is used to store the directory entry offset.
567  */
568
569 struct dentry *fat_decode_fh(struct super_block *sb, __u32 *fh,
570                              int len, int fhtype, 
571                              int (*acceptable)(void *context, struct dentry *de),
572                              void *context)
573 {
574
575         if (fhtype != 3)
576                 return ERR_PTR(-ESTALE);
577         if (len < 5)
578                 return ERR_PTR(-ESTALE);
579
580         return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, context);
581 }
582
583 struct dentry *fat_get_dentry(struct super_block *sb, void *inump)
584 {
585         struct inode *inode = NULL;
586         struct dentry *result;
587         __u32 *fh = inump;
588
589         inode = iget(sb, fh[0]);
590         if (!inode || is_bad_inode(inode) || inode->i_generation != fh[1]) {
591                 if (inode)
592                         iput(inode);
593                 inode = NULL;
594         }
595         if (!inode) {
596                 loff_t i_pos;
597                 int i_logstart = fh[3] & 0x0fffffff;
598
599                 i_pos = (loff_t)fh[2] << 8;
600                 i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
601
602                 /* try 2 - see if i_pos is in F-d-c
603                  * require i_logstart to be the same
604                  * Will fail if you truncate and then re-write
605                  */
606
607                 inode = fat_iget(sb, i_pos);
608                 if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
609                         iput(inode);
610                         inode = NULL;
611                 }
612         }
613         if (!inode) {
614                 /* For now, do nothing
615                  * What we could do is:
616                  * follow the file starting at fh[4], and record
617                  * the ".." entry, and the name of the fh[2] entry.
618                  * The follow the ".." file finding the next step up.
619                  * This way we build a path to the root of
620                  * the tree. If this works, we lookup the path and so
621                  * get this inode into the cache.
622                  * Finally try the fat_iget lookup again
623                  * If that fails, then weare totally out of luck
624                  * But all that is for another day
625                  */
626         }
627         if (!inode)
628                 return ERR_PTR(-ESTALE);
629
630         
631         /* now to find a dentry.
632          * If possible, get a well-connected one
633          */
634         result = d_alloc_anon(inode);
635         if (result == NULL) {
636                 iput(inode);
637                 return ERR_PTR(-ENOMEM);
638         }
639         result->d_op = sb->s_root->d_op;
640         return result;
641 }
642
643 int fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable)
644 {
645         int len = *lenp;
646         struct inode *inode =  de->d_inode;
647         u32 ipos_h, ipos_m, ipos_l;
648         
649         if (len < 5)
650                 return 255; /* no room */
651
652         ipos_h = MSDOS_I(inode)->i_pos >> 8;
653         ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
654         ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
655         *lenp = 5;
656         fh[0] = inode->i_ino;
657         fh[1] = inode->i_generation;
658         fh[2] = ipos_h;
659         fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
660         spin_lock(&de->d_lock);
661         fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart;
662         spin_unlock(&de->d_lock);
663         return 3;
664 }
665
666 struct dentry *fat_get_parent(struct dentry *child)
667 {
668         struct buffer_head *bh=NULL;
669         struct msdos_dir_entry *de = NULL;
670         struct dentry *parent = NULL;
671         int res;
672         loff_t i_pos = 0;
673         struct inode *inode;
674
675         lock_kernel();
676         res = fat_scan(child->d_inode, MSDOS_DOTDOT, &bh, &de, &i_pos);
677
678         if (res < 0)
679                 goto out;
680         inode = fat_build_inode(child->d_sb, de, i_pos, &res);
681         if (res)
682                 goto out;
683         if (!inode)
684                 res = -EACCES;
685         else {
686                 parent = d_alloc_anon(inode);
687                 if (!parent) {
688                         iput(inode);
689                         res = -ENOMEM;
690                 }
691         }
692
693  out:
694         if(bh)
695                 brelse(bh);
696         unlock_kernel();
697         if (res)
698                 return ERR_PTR(res);
699         else
700                 return parent;
701 }
702
703 static kmem_cache_t *fat_inode_cachep;
704
705 static struct inode *fat_alloc_inode(struct super_block *sb)
706 {
707         struct msdos_inode_info *ei;
708         ei = (struct msdos_inode_info *)kmem_cache_alloc(fat_inode_cachep, SLAB_KERNEL);
709         if (!ei)
710                 return NULL;
711         return &ei->vfs_inode;
712 }
713
714 static void fat_destroy_inode(struct inode *inode)
715 {
716         kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
717 }
718
719 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
720 {
721         struct msdos_inode_info *ei = (struct msdos_inode_info *) foo;
722
723         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
724             SLAB_CTOR_CONSTRUCTOR) {
725                 INIT_LIST_HEAD(&ei->i_fat_hash);
726                 inode_init_once(&ei->vfs_inode);
727         }
728 }
729  
730 int __init fat_init_inodecache(void)
731 {
732         fat_inode_cachep = kmem_cache_create("fat_inode_cache",
733                                              sizeof(struct msdos_inode_info),
734                                              0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
735                                              init_once, NULL);
736         if (fat_inode_cachep == NULL)
737                 return -ENOMEM;
738         return 0;
739 }
740
741 void __exit fat_destroy_inodecache(void)
742 {
743         if (kmem_cache_destroy(fat_inode_cachep))
744                 printk(KERN_INFO "fat_inode_cache: not all structures were freed\n");
745 }
746
747 static int fat_remount(struct super_block *sb, int *flags, char *data)
748 {
749         *flags |= MS_NODIRATIME;
750         return 0;
751 }
752
753 static struct super_operations fat_sops = { 
754         .alloc_inode    = fat_alloc_inode,
755         .destroy_inode  = fat_destroy_inode,
756         .write_inode    = fat_write_inode,
757         .delete_inode   = fat_delete_inode,
758         .put_super      = fat_put_super,
759         .statfs         = fat_statfs,
760         .clear_inode    = fat_clear_inode,
761         .remount_fs     = fat_remount,
762
763         .read_inode     = make_bad_inode,
764
765         .show_options   = fat_show_options,
766 };
767
768 static struct export_operations fat_export_ops = {
769         .decode_fh      = fat_decode_fh,
770         .encode_fh      = fat_encode_fh,
771         .get_dentry     = fat_get_dentry,
772         .get_parent     = fat_get_parent,
773 };
774
775 /*
776  * Read the super block of an MS-DOS FS.
777  */
778 int fat_fill_super(struct super_block *sb, void *data, int silent,
779                    struct inode_operations *fs_dir_inode_ops, int isvfat)
780 {
781         struct inode *root_inode = NULL;
782         struct buffer_head *bh;
783         struct fat_boot_sector *b;
784         struct msdos_sb_info *sbi;
785         u16 logical_sector_size;
786         u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
787         int debug, cp, first;
788         unsigned int media;
789         long error;
790         char buf[50];
791
792         sbi = kmalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
793         if (!sbi)
794                 return -ENOMEM;
795         sb->s_fs_info = sbi;
796         memset(sbi, 0, sizeof(struct msdos_sb_info));
797
798         sb->s_flags |= MS_NODIRATIME;
799         sb->s_magic = MSDOS_SUPER_MAGIC;
800         sb->s_op = &fat_sops;
801         sb->s_export_op = &fat_export_ops;
802         sbi->dir_ops = fs_dir_inode_ops;
803
804         error = -EINVAL;
805         if (!parse_options(data, isvfat, &debug, &sbi->options))
806                 goto out_fail;
807
808         fat_cache_init(sb);
809         /* set up enough so that it can read an inode */
810         init_MUTEX(&sbi->fat_lock);
811
812         error = -EIO;
813         sb_min_blocksize(sb, 512);
814         bh = sb_bread(sb, 0);
815         if (bh == NULL) {
816                 printk(KERN_ERR "FAT: unable to read boot sector\n");
817                 goto out_fail;
818         }
819
820         b = (struct fat_boot_sector *) bh->b_data;
821         if (!b->reserved) {
822                 if (!silent)
823                         printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
824                 brelse(bh);
825                 goto out_invalid;
826         }
827         if (!b->fats) {
828                 if (!silent)
829                         printk(KERN_ERR "FAT: bogus number of FAT structure\n");
830                 brelse(bh);
831                 goto out_invalid;
832         }
833         if (!b->secs_track) {
834                 if (!silent)
835                         printk(KERN_ERR "FAT: bogus sectors-per-track value\n");
836                 brelse(bh);
837                 goto out_invalid;
838         }
839         if (!b->heads) {
840                 if (!silent)
841                         printk(KERN_ERR "FAT: bogus number-of-heads value\n");
842                 brelse(bh);
843                 goto out_invalid;
844         }
845         media = b->media;
846         if (!FAT_VALID_MEDIA(media)) {
847                 if (!silent)
848                         printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
849                                media);
850                 brelse(bh);
851                 goto out_invalid;
852         }
853         logical_sector_size = CF_LE_W(get_unaligned((u16 *)&b->sector_size));
854         if (!logical_sector_size
855             || (logical_sector_size & (logical_sector_size - 1))
856             || (logical_sector_size < 512)
857             || (PAGE_CACHE_SIZE < logical_sector_size)) {
858                 if (!silent)
859                         printk(KERN_ERR "FAT: bogus logical sector size %u\n",
860                                logical_sector_size);
861                 brelse(bh);
862                 goto out_invalid;
863         }
864         sbi->sec_per_clus = b->sec_per_clus;
865         if (!sbi->sec_per_clus
866             || (sbi->sec_per_clus & (sbi->sec_per_clus - 1))) {
867                 if (!silent)
868                         printk(KERN_ERR "FAT: bogus sectors per cluster %u\n",
869                                sbi->sec_per_clus);
870                 brelse(bh);
871                 goto out_invalid;
872         }
873
874         if (logical_sector_size < sb->s_blocksize) {
875                 printk(KERN_ERR "FAT: logical sector size too small for device"
876                        " (logical sector size = %u)\n", logical_sector_size);
877                 brelse(bh);
878                 goto out_fail;
879         }
880         if (logical_sector_size > sb->s_blocksize) {
881                 brelse(bh);
882
883                 if (!sb_set_blocksize(sb, logical_sector_size)) {
884                         printk(KERN_ERR "FAT: unable to set blocksize %u\n",
885                                logical_sector_size);
886                         goto out_fail;
887                 }
888                 bh = sb_bread(sb, 0);
889                 if (bh == NULL) {
890                         printk(KERN_ERR "FAT: unable to read boot sector"
891                                " (logical sector size = %lu)\n",
892                                sb->s_blocksize);
893                         goto out_fail;
894                 }
895                 b = (struct fat_boot_sector *) bh->b_data;
896         }
897
898         sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
899         sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
900         sbi->fats = b->fats;
901         sbi->fat_bits = 0;              /* Don't know yet */
902         sbi->fat_start = CF_LE_W(b->reserved);
903         sbi->fat_length = CF_LE_W(b->fat_length);
904         sbi->root_cluster = 0;
905         sbi->free_clusters = -1;        /* Don't know yet */
906         sbi->prev_free = -1;
907
908         if (!sbi->fat_length && b->fat32_length) {
909                 struct fat_boot_fsinfo *fsinfo;
910                 struct buffer_head *fsinfo_bh;
911
912                 /* Must be FAT32 */
913                 sbi->fat_bits = 32;
914                 sbi->fat_length = CF_LE_L(b->fat32_length);
915                 sbi->root_cluster = CF_LE_L(b->root_cluster);
916
917                 sb->s_maxbytes = 0xffffffff;
918                 
919                 /* MC - if info_sector is 0, don't multiply by 0 */
920                 sbi->fsinfo_sector = CF_LE_W(b->info_sector);
921                 if (sbi->fsinfo_sector == 0)
922                         sbi->fsinfo_sector = 1;
923
924                 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
925                 if (fsinfo_bh == NULL) {
926                         printk(KERN_ERR "FAT: bread failed, FSINFO block"
927                                " (sector = %lu)\n", sbi->fsinfo_sector);
928                         brelse(bh);
929                         goto out_fail;
930                 }
931
932                 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
933                 if (!IS_FSINFO(fsinfo)) {
934                         printk(KERN_WARNING
935                                "FAT: Did not find valid FSINFO signature.\n"
936                                "     Found signature1 0x%08x signature2 0x%08x"
937                                " (sector = %lu)\n",
938                                CF_LE_L(fsinfo->signature1),
939                                CF_LE_L(fsinfo->signature2),
940                                sbi->fsinfo_sector);
941                 } else {
942                         sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
943                         sbi->prev_free = CF_LE_L(fsinfo->next_cluster);
944                 }
945
946                 brelse(fsinfo_bh);
947         }
948
949         sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
950         sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
951
952         sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
953         sbi->dir_entries = CF_LE_W(get_unaligned((u16 *)&b->dir_entries));
954         if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
955                 if (!silent)
956                         printk(KERN_ERR "FAT: bogus directroy-entries per block"
957                                " (%u)\n", sbi->dir_entries);
958                 brelse(bh);
959                 goto out_invalid;
960         }
961
962         rootdir_sectors = sbi->dir_entries
963                 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
964         sbi->data_start = sbi->dir_start + rootdir_sectors;
965         total_sectors = CF_LE_W(get_unaligned((u16 *)&b->sectors));
966         if (total_sectors == 0)
967                 total_sectors = CF_LE_L(b->total_sect);
968
969         total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
970
971         if (sbi->fat_bits != 32)
972                 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
973
974         /* check that FAT table does not overflow */
975         fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
976         total_clusters = min(total_clusters, fat_clusters - 2);
977         if (total_clusters > MAX_FAT(sb)) {
978                 if (!silent)
979                         printk(KERN_ERR "FAT: count of clusters too big (%u)\n",
980                                total_clusters);
981                 brelse(bh);
982                 goto out_invalid;
983         }
984
985         sbi->clusters = total_clusters;
986
987         brelse(bh);
988
989         /* validity check of FAT */
990         first = __fat_access(sb, 0, -1);
991         if (first < 0) {
992                 error = first;
993                 goto out_fail;
994         }
995         if (FAT_FIRST_ENT(sb, media) == first) {
996                 /* all is as it should be */
997         } else if (media == 0xf8 && FAT_FIRST_ENT(sb, 0xfe) == first) {
998                 /* bad, reported on pc9800 */
999         } else if (media == 0xf0 && FAT_FIRST_ENT(sb, 0xf8) == first) {
1000                 /* bad, reported with a MO disk on win95/me */
1001         } else if (first == 0) {
1002                 /* bad, reported with a SmartMedia card */
1003         } else {
1004                 if (!silent)
1005                         printk(KERN_ERR "FAT: invalid first entry of FAT "
1006                                "(0x%x != 0x%x)\n",
1007                                FAT_FIRST_ENT(sb, media), first);
1008                 goto out_invalid;
1009         }
1010
1011         error = -EINVAL;
1012         cp = sbi->options.codepage ? sbi->options.codepage : 437;
1013         sprintf(buf, "cp%d", cp);
1014         sbi->nls_disk = load_nls(buf);
1015         if (!sbi->nls_disk) {
1016                 /* Fail only if explicit charset specified */
1017                 if (sbi->options.codepage != 0) {
1018                         printk(KERN_ERR "FAT: codepage %s not found\n", buf);
1019                         goto out_fail;
1020                 }
1021                 sbi->options.codepage = 0; /* already 0?? */
1022                 sbi->nls_disk = load_nls_default();
1023         }
1024
1025         /* FIXME: utf8 is using iocharset for upper/lower conversion */
1026         if (sbi->options.isvfat) {
1027                 if (sbi->options.iocharset != NULL) {
1028                         sbi->nls_io = load_nls(sbi->options.iocharset);
1029                         if (!sbi->nls_io) {
1030                                 printk(KERN_ERR
1031                                        "FAT: IO charset %s not found\n",
1032                                        sbi->options.iocharset);
1033                                 goto out_fail;
1034                         }
1035                 } else
1036                         sbi->nls_io = load_nls_default();
1037         }
1038
1039         error = -ENOMEM;
1040         root_inode = new_inode(sb);
1041         if (!root_inode)
1042                 goto out_fail;
1043         root_inode->i_ino = MSDOS_ROOT_INO;
1044         root_inode->i_version = 1;
1045         error = fat_read_root(root_inode);
1046         if (error < 0)
1047                 goto out_fail;
1048         error = -ENOMEM;
1049         insert_inode_hash(root_inode);
1050         sb->s_root = d_alloc_root(root_inode);
1051         if (!sb->s_root) {
1052                 printk(KERN_ERR "FAT: get root inode failed\n");
1053                 goto out_fail;
1054         }
1055
1056         return 0;
1057
1058 out_invalid:
1059         error = -EINVAL;
1060         if (!silent)
1061                 printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
1062                        " on dev %s.\n", sb->s_id);
1063
1064 out_fail:
1065         if (root_inode)
1066                 iput(root_inode);
1067         if (sbi->nls_io)
1068                 unload_nls(sbi->nls_io);
1069         if (sbi->nls_disk)
1070                 unload_nls(sbi->nls_disk);
1071         if (sbi->options.iocharset)
1072                 kfree(sbi->options.iocharset);
1073         sb->s_fs_info = NULL;
1074         kfree(sbi);
1075         return error;
1076 }
1077
1078 int fat_statfs(struct super_block *sb, struct kstatfs *buf)
1079 {
1080         int free, nr, ret;
1081        
1082         if (MSDOS_SB(sb)->free_clusters != -1)
1083                 free = MSDOS_SB(sb)->free_clusters;
1084         else {
1085                 lock_fat(sb);
1086                 if (MSDOS_SB(sb)->free_clusters != -1)
1087                         free = MSDOS_SB(sb)->free_clusters;
1088                 else {
1089                         free = 0;
1090                         for (nr = 2; nr < MSDOS_SB(sb)->clusters + 2; nr++) {
1091                                 ret = fat_access(sb, nr, -1);
1092                                 if (ret < 0) {
1093                                         unlock_fat(sb);
1094                                         return ret;
1095                                 } else if (ret == FAT_ENT_FREE)
1096                                         free++;
1097                         }
1098                         MSDOS_SB(sb)->free_clusters = free;
1099                 }
1100                 unlock_fat(sb);
1101         }
1102
1103         buf->f_type = sb->s_magic;
1104         buf->f_bsize = MSDOS_SB(sb)->cluster_size;
1105         buf->f_blocks = MSDOS_SB(sb)->clusters;
1106         buf->f_bfree = free;
1107         buf->f_bavail = free;
1108         buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
1109
1110         return 0;
1111 }
1112
1113 static int is_exec(unsigned char *extension)
1114 {
1115         unsigned char *exe_extensions = "EXECOMBAT", *walk;
1116
1117         for (walk = exe_extensions; *walk; walk += 3)
1118                 if (!strncmp(extension, walk, 3))
1119                         return 1;
1120         return 0;
1121 }
1122
1123 static int fat_writepage(struct page *page, struct writeback_control *wbc)
1124 {
1125         return block_write_full_page(page,fat_get_block, wbc);
1126 }
1127 static int fat_readpage(struct file *file, struct page *page)
1128 {
1129         return block_read_full_page(page,fat_get_block);
1130 }
1131
1132 static int
1133 fat_prepare_write(struct file *file, struct page *page,
1134                         unsigned from, unsigned to)
1135 {
1136         kmap(page);
1137         return cont_prepare_write(page,from,to,fat_get_block,
1138                 &MSDOS_I(page->mapping->host)->mmu_private);
1139 }
1140
1141 static int
1142 fat_commit_write(struct file *file, struct page *page,
1143                         unsigned from, unsigned to)
1144 {
1145         kunmap(page);
1146         return generic_commit_write(file, page, from, to);
1147 }
1148
1149 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
1150 {
1151         return generic_block_bmap(mapping,block,fat_get_block);
1152 }
1153 static struct address_space_operations fat_aops = {
1154         .readpage = fat_readpage,
1155         .writepage = fat_writepage,
1156         .sync_page = block_sync_page,
1157         .prepare_write = fat_prepare_write,
1158         .commit_write = fat_commit_write,
1159         .bmap = _fat_bmap
1160 };
1161
1162 /* doesn't deal with root inode */
1163 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
1164 {
1165         struct super_block *sb = inode->i_sb;
1166         struct msdos_sb_info *sbi = MSDOS_SB(sb);
1167         int error;
1168
1169         MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
1170         MSDOS_I(inode)->i_pos = 0;
1171         inode->i_uid = sbi->options.fs_uid;
1172         inode->i_gid = sbi->options.fs_gid;
1173         inode->i_version++;
1174         inode->i_generation = get_seconds();
1175         
1176         if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
1177                 inode->i_generation &= ~1;
1178                 inode->i_mode = MSDOS_MKMODE(de->attr,
1179                         S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
1180                 inode->i_op = sbi->dir_ops;
1181                 inode->i_fop = &fat_dir_operations;
1182
1183                 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1184                 if (sbi->fat_bits == 32)
1185                         MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1186
1187                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1188                 error = fat_calc_dir_size(inode);
1189                 if (error < 0)
1190                         return error;
1191                 MSDOS_I(inode)->mmu_private = inode->i_size;
1192
1193                 inode->i_nlink = fat_subdirs(inode);
1194         } else { /* not a directory */
1195                 inode->i_generation |= 1;
1196                 inode->i_mode = MSDOS_MKMODE(de->attr,
1197                     ((sbi->options.showexec &&
1198                        !is_exec(de->ext))
1199                         ? S_IRUGO|S_IWUGO : S_IRWXUGO)
1200                     & ~sbi->options.fs_fmask) | S_IFREG;
1201                 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1202                 if (sbi->fat_bits == 32)
1203                         MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1204
1205                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1206                 inode->i_size = CF_LE_L(de->size);
1207                 inode->i_op = &fat_file_inode_operations;
1208                 inode->i_fop = &fat_file_operations;
1209                 inode->i_mapping->a_ops = &fat_aops;
1210                 MSDOS_I(inode)->mmu_private = inode->i_size;
1211         }
1212         if(de->attr & ATTR_SYS)
1213                 if (sbi->options.sys_immutable)
1214                         inode->i_flags |= S_IMMUTABLE;
1215         MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
1216         /* this is as close to the truth as we can get ... */
1217         inode->i_blksize = sbi->cluster_size;
1218         inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1219                            & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1220         inode->i_mtime.tv_sec = inode->i_atime.tv_sec =
1221                 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
1222         inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0;
1223         inode->i_ctime.tv_sec =
1224                 MSDOS_SB(sb)->options.isvfat
1225                 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
1226                 : inode->i_mtime.tv_sec;
1227         inode->i_ctime.tv_nsec = de->ctime_ms * 1000000;
1228         MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
1229
1230         return 0;
1231 }
1232
1233 void fat_write_inode(struct inode *inode, int wait)
1234 {
1235         struct super_block *sb = inode->i_sb;
1236         struct buffer_head *bh;
1237         struct msdos_dir_entry *raw_entry;
1238         loff_t i_pos;
1239
1240 retry:
1241         i_pos = MSDOS_I(inode)->i_pos;
1242         if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
1243                 return;
1244         }
1245         lock_kernel();
1246         if (!(bh = sb_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
1247                 printk(KERN_ERR "FAT: unable to read inode block "
1248                        "for updating (i_pos %lld)\n", i_pos);
1249                 unlock_kernel();
1250                 return /* -EIO */;
1251         }
1252         spin_lock(&fat_inode_lock);
1253         if (i_pos != MSDOS_I(inode)->i_pos) {
1254                 spin_unlock(&fat_inode_lock);
1255                 brelse(bh);
1256                 unlock_kernel();
1257                 goto retry;
1258         }
1259
1260         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
1261             [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
1262         if (S_ISDIR(inode->i_mode)) {
1263                 raw_entry->attr = ATTR_DIR;
1264                 raw_entry->size = 0;
1265         }
1266         else {
1267                 raw_entry->attr = ATTR_NONE;
1268                 raw_entry->size = CT_LE_L(inode->i_size);
1269         }
1270         raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
1271             MSDOS_I(inode)->i_attrs;
1272         raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
1273         raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
1274         fat_date_unix2dos(inode->i_mtime.tv_sec,&raw_entry->time,&raw_entry->date);
1275         raw_entry->time = CT_LE_W(raw_entry->time);
1276         raw_entry->date = CT_LE_W(raw_entry->date);
1277         if (MSDOS_SB(sb)->options.isvfat) {
1278                 fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
1279                 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms; /* use i_ctime.tv_nsec? */
1280                 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
1281                 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
1282         }
1283         spin_unlock(&fat_inode_lock);
1284         mark_buffer_dirty(bh);
1285         brelse(bh);
1286         unlock_kernel();
1287 }
1288
1289
1290 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1291 {
1292         struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
1293         struct inode *inode = dentry->d_inode;
1294         int mask, error = 0;
1295
1296         lock_kernel();
1297
1298         /* FAT cannot truncate to a longer file */
1299         if (attr->ia_valid & ATTR_SIZE) {
1300                 if (attr->ia_size > inode->i_size) {
1301                         error = -EPERM;
1302                         goto out;
1303                 }
1304         }
1305
1306         error = inode_change_ok(inode, attr);
1307         if (error) {
1308                 if (sbi->options.quiet)
1309                         error = 0;
1310                 goto out;
1311         }
1312
1313         if (((attr->ia_valid & ATTR_UID) && 
1314              (attr->ia_uid != sbi->options.fs_uid)) ||
1315             ((attr->ia_valid & ATTR_GID) && 
1316              (attr->ia_gid != sbi->options.fs_gid)) ||
1317             ((attr->ia_valid & ATTR_MODE) &&
1318              (attr->ia_mode & ~MSDOS_VALID_MODE)))
1319                 error = -EPERM;
1320
1321         if (error) {
1322                 if (sbi->options.quiet)  
1323                         error = 0;
1324                 goto out;
1325         }
1326         error = inode_setattr(inode, attr);
1327         if (error)
1328                 goto out;
1329
1330         if (S_ISDIR(inode->i_mode))
1331                 mask = sbi->options.fs_dmask;
1332         else
1333                 mask = sbi->options.fs_fmask;
1334         inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
1335 out:
1336         unlock_kernel();
1337         return error;
1338 }
1339 MODULE_LICENSE("GPL");