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