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