upgrade to linux 2.6.10-1.12_FC2
[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 static void fat_hash_init(struct super_block *sb)
62 {
63         struct msdos_sb_info *sbi = MSDOS_SB(sb);
64         int i;
65
66         spin_lock_init(&sbi->inode_hash_lock);
67         for (i = 0; i < FAT_HASH_SIZE; i++)
68                 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
69 }
70
71 static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
72 {
73         unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
74         tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
75         return tmp & FAT_HASH_MASK;
76 }
77
78 void fat_attach(struct inode *inode, loff_t i_pos)
79 {
80         struct super_block *sb = inode->i_sb;
81         struct msdos_sb_info *sbi = MSDOS_SB(sb);
82
83         spin_lock(&sbi->inode_hash_lock);
84         MSDOS_I(inode)->i_pos = i_pos;
85         hlist_add_head(&MSDOS_I(inode)->i_fat_hash,
86                         sbi->inode_hashtable + fat_hash(sb, i_pos));
87         spin_unlock(&sbi->inode_hash_lock);
88 }
89
90 void fat_detach(struct inode *inode)
91 {
92         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
93         spin_lock(&sbi->inode_hash_lock);
94         MSDOS_I(inode)->i_pos = 0;
95         hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
96         spin_unlock(&sbi->inode_hash_lock);
97 }
98
99 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
100 {
101         struct msdos_sb_info *sbi = MSDOS_SB(sb);
102         struct hlist_head *head = sbi->inode_hashtable + fat_hash(sb, i_pos);
103         struct hlist_node *_p;
104         struct msdos_inode_info *i;
105         struct inode *inode = NULL;
106
107         spin_lock(&sbi->inode_hash_lock);
108         hlist_for_each_entry(i, _p, head, i_fat_hash) {
109                 BUG_ON(i->vfs_inode.i_sb != sb);
110                 if (i->i_pos != i_pos)
111                         continue;
112                 inode = igrab(&i->vfs_inode);
113                 if (inode)
114                         break;
115         }
116         spin_unlock(&sbi->inode_hash_lock);
117         return inode;
118 }
119
120 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
121
122 struct inode *fat_build_inode(struct super_block *sb,
123                         struct msdos_dir_entry *de, loff_t i_pos, int *res)
124 {
125         struct inode *inode;
126         *res = 0;
127         inode = fat_iget(sb, i_pos);
128         if (inode)
129                 goto out;
130         inode = new_inode(sb);
131         *res = -ENOMEM;
132         if (!inode)
133                 goto out;
134         inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
135         inode->i_version = 1;
136         *res = fat_fill_inode(inode, de);
137         if (*res < 0) {
138                 iput(inode);
139                 inode = NULL;
140                 goto out;
141         }
142         fat_attach(inode, i_pos);
143         insert_inode_hash(inode);
144 out:
145         return inode;
146 }
147
148 static void fat_delete_inode(struct inode *inode)
149 {
150         if (!is_bad_inode(inode)) {
151                 inode->i_size = 0;
152                 fat_truncate(inode);
153         }
154         clear_inode(inode);
155 }
156
157 static void fat_clear_inode(struct inode *inode)
158 {
159         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
160
161         if (is_bad_inode(inode))
162                 return;
163         lock_kernel();
164         spin_lock(&sbi->inode_hash_lock);
165         fat_cache_inval_inode(inode);
166         hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
167         spin_unlock(&sbi->inode_hash_lock);
168         unlock_kernel();
169 }
170
171 static void fat_put_super(struct super_block *sb)
172 {
173         struct msdos_sb_info *sbi = MSDOS_SB(sb);
174
175         if (!(sb->s_flags & MS_RDONLY))
176                 fat_clusters_flush(sb);
177
178         if (sbi->nls_disk) {
179                 unload_nls(sbi->nls_disk);
180                 sbi->nls_disk = NULL;
181                 sbi->options.codepage = fat_default_codepage;
182         }
183         if (sbi->nls_io) {
184                 unload_nls(sbi->nls_io);
185                 sbi->nls_io = NULL;
186         }
187         if (sbi->options.iocharset != fat_default_iocharset) {
188                 kfree(sbi->options.iocharset);
189                 sbi->options.iocharset = fat_default_iocharset;
190         }
191
192         sb->s_fs_info = NULL;
193         kfree(sbi);
194 }
195
196 static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
197 {
198         struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
199         struct fat_mount_options *opts = &sbi->options;
200         int isvfat = opts->isvfat;
201
202         if (opts->fs_uid != 0)
203                 seq_printf(m, ",uid=%u", opts->fs_uid);
204         if (opts->fs_gid != 0)
205                 seq_printf(m, ",gid=%u", opts->fs_gid);
206         seq_printf(m, ",fmask=%04o", opts->fs_fmask);
207         seq_printf(m, ",dmask=%04o", opts->fs_dmask);
208         if (sbi->nls_disk && opts->codepage != fat_default_codepage)
209                 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
210         if (isvfat) {
211                 if (sbi->nls_io &&
212                     strcmp(opts->iocharset, fat_default_iocharset))
213                         seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
214
215                 switch (opts->shortname) {
216                 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
217                         seq_puts(m, ",shortname=win95");
218                         break;
219                 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
220                         seq_puts(m, ",shortname=winnt");
221                         break;
222                 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
223                         seq_puts(m, ",shortname=mixed");
224                         break;
225                 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
226                         /* seq_puts(m, ",shortname=lower"); */
227                         break;
228                 default:
229                         seq_puts(m, ",shortname=unknown");
230                         break;
231                 }
232         }
233         if (opts->name_check != 'n')
234                 seq_printf(m, ",check=%c", opts->name_check);
235         if (opts->quiet)
236                 seq_puts(m, ",quiet");
237         if (opts->showexec)
238                 seq_puts(m, ",showexec");
239         if (opts->sys_immutable)
240                 seq_puts(m, ",sys_immutable");
241         if (!isvfat) {
242                 if (opts->dotsOK)
243                         seq_puts(m, ",dotsOK=yes");
244                 if (opts->nocase)
245                         seq_puts(m, ",nocase");
246         } else {
247                 if (opts->utf8)
248                         seq_puts(m, ",utf8");
249                 if (opts->unicode_xlate)
250                         seq_puts(m, ",uni_xlate");
251                 if (!opts->numtail)
252                         seq_puts(m, ",nonumtail");
253         }
254
255         return 0;
256 }
257
258 enum {
259         Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
260         Opt_umask, Opt_dmask, Opt_fmask, Opt_codepage, Opt_nocase,
261         Opt_quiet, Opt_showexec, Opt_debug, Opt_immutable,
262         Opt_dots, Opt_nodots,
263         Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
264         Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
265         Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
266         Opt_obsolate, Opt_err,
267 };
268
269 static match_table_t fat_tokens = {
270         {Opt_check_r, "check=relaxed"},
271         {Opt_check_s, "check=strict"},
272         {Opt_check_n, "check=normal"},
273         {Opt_check_r, "check=r"},
274         {Opt_check_s, "check=s"},
275         {Opt_check_n, "check=n"},
276         {Opt_uid, "uid=%u"},
277         {Opt_gid, "gid=%u"},
278         {Opt_umask, "umask=%o"},
279         {Opt_dmask, "dmask=%o"},
280         {Opt_fmask, "fmask=%o"},
281         {Opt_codepage, "codepage=%u"},
282         {Opt_nocase, "nocase"},
283         {Opt_quiet, "quiet"},
284         {Opt_showexec, "showexec"},
285         {Opt_debug, "debug"},
286         {Opt_immutable, "sys_immutable"},
287         {Opt_obsolate, "conv=binary"},
288         {Opt_obsolate, "conv=text"},
289         {Opt_obsolate, "conv=auto"},
290         {Opt_obsolate, "conv=b"},
291         {Opt_obsolate, "conv=t"},
292         {Opt_obsolate, "conv=a"},
293         {Opt_obsolate, "fat=%u"},
294         {Opt_obsolate, "blocksize=%u"},
295         {Opt_obsolate, "cvf_format=%20s"},
296         {Opt_obsolate, "cvf_options=%100s"},
297         {Opt_obsolate, "posix"},
298         {Opt_err, NULL}
299 };
300 static match_table_t msdos_tokens = {
301         {Opt_nodots, "nodots"},
302         {Opt_nodots, "dotsOK=no"},
303         {Opt_dots, "dots"},
304         {Opt_dots, "dotsOK=yes"},
305         {Opt_err, NULL}
306 };
307 static match_table_t vfat_tokens = {
308         {Opt_charset, "iocharset=%s"},
309         {Opt_shortname_lower, "shortname=lower"},
310         {Opt_shortname_win95, "shortname=win95"},
311         {Opt_shortname_winnt, "shortname=winnt"},
312         {Opt_shortname_mixed, "shortname=mixed"},
313         {Opt_utf8_no, "utf8=0"},                /* 0 or no or false */
314         {Opt_utf8_no, "utf8=no"},
315         {Opt_utf8_no, "utf8=false"},
316         {Opt_utf8_yes, "utf8=1"},               /* empty or 1 or yes or true */
317         {Opt_utf8_yes, "utf8=yes"},
318         {Opt_utf8_yes, "utf8=true"},
319         {Opt_utf8_yes, "utf8"},
320         {Opt_uni_xl_no, "uni_xlate=0"},         /* 0 or no or false */
321         {Opt_uni_xl_no, "uni_xlate=no"},
322         {Opt_uni_xl_no, "uni_xlate=false"},
323         {Opt_uni_xl_yes, "uni_xlate=1"},        /* empty or 1 or yes or true */
324         {Opt_uni_xl_yes, "uni_xlate=yes"},
325         {Opt_uni_xl_yes, "uni_xlate=true"},
326         {Opt_uni_xl_yes, "uni_xlate"},
327         {Opt_nonumtail_no, "nonumtail=0"},      /* 0 or no or false */
328         {Opt_nonumtail_no, "nonumtail=no"},
329         {Opt_nonumtail_no, "nonumtail=false"},
330         {Opt_nonumtail_yes, "nonumtail=1"},     /* empty or 1 or yes or true */
331         {Opt_nonumtail_yes, "nonumtail=yes"},
332         {Opt_nonumtail_yes, "nonumtail=true"},
333         {Opt_nonumtail_yes, "nonumtail"},
334         {Opt_err, NULL}
335 };
336
337 static int parse_options(char *options, int is_vfat, int *debug,
338                          struct fat_mount_options *opts)
339 {
340         char *p;
341         substring_t args[MAX_OPT_ARGS];
342         int option;
343         char *iocharset;
344
345         opts->isvfat = is_vfat;
346
347         opts->fs_uid = current->uid;
348         opts->fs_gid = current->gid;
349         opts->fs_fmask = opts->fs_dmask = current->fs->umask;
350         opts->codepage = fat_default_codepage;
351         opts->iocharset = fat_default_iocharset;
352         if (is_vfat)
353                 opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
354         else
355                 opts->shortname = 0;
356         opts->name_check = 'n';
357         opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK =  0;
358         opts->utf8 = opts->unicode_xlate = 0;
359         opts->numtail = 1;
360         opts->nocase = 0;
361         *debug = 0;
362
363         if (!options)
364                 return 0;
365
366         while ((p = strsep(&options, ",")) != NULL) {
367                 int token;
368                 if (!*p)
369                         continue;
370
371                 token = match_token(p, fat_tokens, args);
372                 if (token == Opt_err) {
373                         if (is_vfat)
374                                 token = match_token(p, vfat_tokens, args);
375                         else
376                                 token = match_token(p, msdos_tokens, args);
377                 }
378                 switch (token) {
379                 case Opt_check_s:
380                         opts->name_check = 's';
381                         break;
382                 case Opt_check_r:
383                         opts->name_check = 'r';
384                         break;
385                 case Opt_check_n:
386                         opts->name_check = 'n';
387                         break;
388                 case Opt_nocase:
389                         if (!is_vfat)
390                                 opts->nocase = 1;
391                         else {
392                                 /* for backward compatibility */
393                                 opts->shortname = VFAT_SFN_DISPLAY_WIN95
394                                         | VFAT_SFN_CREATE_WIN95;
395                         }
396                         break;
397                 case Opt_quiet:
398                         opts->quiet = 1;
399                         break;
400                 case Opt_showexec:
401                         opts->showexec = 1;
402                         break;
403                 case Opt_debug:
404                         *debug = 1;
405                         break;
406                 case Opt_immutable:
407                         opts->sys_immutable = 1;
408                         break;
409                 case Opt_uid:
410                         if (match_int(&args[0], &option))
411                                 return 0;
412                         opts->fs_uid = option;
413                         break;
414                 case Opt_gid:
415                         if (match_int(&args[0], &option))
416                                 return 0;
417                         opts->fs_gid = option;
418                         break;
419                 case Opt_umask:
420                         if (match_octal(&args[0], &option))
421                                 return 0;
422                         opts->fs_fmask = opts->fs_dmask = option;
423                         break;
424                 case Opt_dmask:
425                         if (match_octal(&args[0], &option))
426                                 return 0;
427                         opts->fs_dmask = option;
428                         break;
429                 case Opt_fmask:
430                         if (match_octal(&args[0], &option))
431                                 return 0;
432                         opts->fs_fmask = option;
433                         break;
434                 case Opt_codepage:
435                         if (match_int(&args[0], &option))
436                                 return 0;
437                         opts->codepage = option;
438                         break;
439
440                 /* msdos specific */
441                 case Opt_dots:
442                         opts->dotsOK = 1;
443                         break;
444                 case Opt_nodots:
445                         opts->dotsOK = 0;
446                         break;
447
448                 /* vfat specific */
449                 case Opt_charset:
450                         if (opts->iocharset != fat_default_iocharset)
451                                 kfree(opts->iocharset);
452                         iocharset = match_strdup(&args[0]);
453                         if (!iocharset)
454                                 return -ENOMEM;
455                         opts->iocharset = iocharset;
456                         break;
457                 case Opt_shortname_lower:
458                         opts->shortname = VFAT_SFN_DISPLAY_LOWER
459                                         | VFAT_SFN_CREATE_WIN95;
460                         break;
461                 case Opt_shortname_win95:
462                         opts->shortname = VFAT_SFN_DISPLAY_WIN95
463                                         | VFAT_SFN_CREATE_WIN95;
464                         break;
465                 case Opt_shortname_winnt:
466                         opts->shortname = VFAT_SFN_DISPLAY_WINNT
467                                         | VFAT_SFN_CREATE_WINNT;
468                         break;
469                 case Opt_shortname_mixed:
470                         opts->shortname = VFAT_SFN_DISPLAY_WINNT
471                                         | VFAT_SFN_CREATE_WIN95;
472                         break;
473                 case Opt_utf8_no:               /* 0 or no or false */
474                         opts->utf8 = 0;
475                         break;
476                 case Opt_utf8_yes:              /* empty or 1 or yes or true */
477                         opts->utf8 = 1;
478                         break;
479                 case Opt_uni_xl_no:             /* 0 or no or false */
480                         opts->unicode_xlate = 0;
481                         break;
482                 case Opt_uni_xl_yes:            /* empty or 1 or yes or true */
483                         opts->unicode_xlate = 1;
484                         break;
485                 case Opt_nonumtail_no:          /* 0 or no or false */
486                         opts->numtail = 1;      /* negated option */
487                         break;
488                 case Opt_nonumtail_yes:         /* empty or 1 or yes or true */
489                         opts->numtail = 0;      /* negated option */
490                         break;
491
492                 /* obsolete mount options */
493                 case Opt_obsolate:
494                         printk(KERN_INFO "FAT: \"%s\" option is obsolete, "
495                                "not supported now\n", p);
496                         break;
497                 /* unknown option */
498                 default:
499                         printk(KERN_ERR "FAT: Unrecognized mount option \"%s\" "
500                                "or missing value\n", p);
501                         return -EINVAL;
502                 }
503         }
504         /* UTF8 doesn't provide FAT semantics */
505         if (!strcmp(opts->iocharset, "utf8")) {
506                 printk(KERN_ERR "FAT: utf8 is not a recommended IO charset"
507                        " for FAT filesystems, filesystem will be case sensitive!\n");
508         }
509
510         if (opts->unicode_xlate)
511                 opts->utf8 = 0;
512         
513         return 0;
514 }
515
516 static int fat_calc_dir_size(struct inode *inode)
517 {
518         struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
519         int ret, fclus, dclus;
520
521         inode->i_size = 0;
522         if (MSDOS_I(inode)->i_start == 0)
523                 return 0;
524
525         ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
526         if (ret < 0)
527                 return ret;
528         inode->i_size = (fclus + 1) << sbi->cluster_bits;
529
530         return 0;
531 }
532
533 static int fat_read_root(struct inode *inode)
534 {
535         struct super_block *sb = inode->i_sb;
536         struct msdos_sb_info *sbi = MSDOS_SB(sb);
537         int error;
538
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                 spin_lock_init(&ei->cache_lru_lock);
742                 ei->nr_caches = 0;
743                 ei->cache_valid_id = FAT_CACHE_VALID + 1;
744                 INIT_LIST_HEAD(&ei->cache_lru);
745                 INIT_HLIST_NODE(&ei->i_fat_hash);
746                 inode_init_once(&ei->vfs_inode);
747         }
748 }
749  
750 int __init fat_init_inodecache(void)
751 {
752         fat_inode_cachep = kmem_cache_create("fat_inode_cache",
753                                              sizeof(struct msdos_inode_info),
754                                              0, SLAB_RECLAIM_ACCOUNT,
755                                              init_once, NULL);
756         if (fat_inode_cachep == NULL)
757                 return -ENOMEM;
758         return 0;
759 }
760
761 void __exit fat_destroy_inodecache(void)
762 {
763         if (kmem_cache_destroy(fat_inode_cachep))
764                 printk(KERN_INFO "fat_inode_cache: not all structures were freed\n");
765 }
766
767 static int fat_remount(struct super_block *sb, int *flags, char *data)
768 {
769         *flags |= MS_NODIRATIME;
770         return 0;
771 }
772
773 static struct super_operations fat_sops = { 
774         .alloc_inode    = fat_alloc_inode,
775         .destroy_inode  = fat_destroy_inode,
776         .write_inode    = fat_write_inode,
777         .delete_inode   = fat_delete_inode,
778         .put_super      = fat_put_super,
779         .statfs         = fat_statfs,
780         .clear_inode    = fat_clear_inode,
781         .remount_fs     = fat_remount,
782
783         .read_inode     = make_bad_inode,
784
785         .show_options   = fat_show_options,
786 };
787
788 static struct export_operations fat_export_ops = {
789         .decode_fh      = fat_decode_fh,
790         .encode_fh      = fat_encode_fh,
791         .get_dentry     = fat_get_dentry,
792         .get_parent     = fat_get_parent,
793 };
794
795 /*
796  * Read the super block of an MS-DOS FS.
797  */
798 int fat_fill_super(struct super_block *sb, void *data, int silent,
799                    struct inode_operations *fs_dir_inode_ops, int isvfat)
800 {
801         struct inode *root_inode = NULL;
802         struct buffer_head *bh;
803         struct fat_boot_sector *b;
804         struct msdos_sb_info *sbi;
805         u16 logical_sector_size;
806         u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
807         int debug;
808         unsigned int media;
809         long error;
810         char buf[50];
811
812         sbi = kmalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
813         if (!sbi)
814                 return -ENOMEM;
815         sb->s_fs_info = sbi;
816         memset(sbi, 0, sizeof(struct msdos_sb_info));
817
818         sb->s_flags |= MS_NODIRATIME;
819         sb->s_magic = MSDOS_SUPER_MAGIC;
820         sb->s_op = &fat_sops;
821         sb->s_export_op = &fat_export_ops;
822         sbi->dir_ops = fs_dir_inode_ops;
823
824         error = parse_options(data, isvfat, &debug, &sbi->options);
825         if (error)
826                 goto out_fail;
827
828         /* set up enough so that it can read an inode */
829         fat_hash_init(sb);
830         init_MUTEX(&sbi->fat_lock);
831
832         error = -EIO;
833         sb_min_blocksize(sb, 512);
834         bh = sb_bread(sb, 0);
835         if (bh == NULL) {
836                 printk(KERN_ERR "FAT: unable to read boot sector\n");
837                 goto out_fail;
838         }
839
840         b = (struct fat_boot_sector *) bh->b_data;
841         if (!b->reserved) {
842                 if (!silent)
843                         printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
844                 brelse(bh);
845                 goto out_invalid;
846         }
847         if (!b->fats) {
848                 if (!silent)
849                         printk(KERN_ERR "FAT: bogus number of FAT structure\n");
850                 brelse(bh);
851                 goto out_invalid;
852         }
853
854         /*
855          * Earlier we checked here that b->secs_track and b->head are nonzero,
856          * but it turns out valid FAT filesystems can have zero there.
857          */
858
859         media = b->media;
860         if (!FAT_VALID_MEDIA(media)) {
861                 if (!silent)
862                         printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
863                                media);
864                 brelse(bh);
865                 goto out_invalid;
866         }
867         logical_sector_size =
868                 le16_to_cpu(get_unaligned((__le16 *)&b->sector_size));
869         if (!logical_sector_size
870             || (logical_sector_size & (logical_sector_size - 1))
871             || (logical_sector_size < 512)
872             || (PAGE_CACHE_SIZE < logical_sector_size)) {
873                 if (!silent)
874                         printk(KERN_ERR "FAT: bogus logical sector size %u\n",
875                                logical_sector_size);
876                 brelse(bh);
877                 goto out_invalid;
878         }
879         sbi->sec_per_clus = b->sec_per_clus;
880         if (!sbi->sec_per_clus
881             || (sbi->sec_per_clus & (sbi->sec_per_clus - 1))) {
882                 if (!silent)
883                         printk(KERN_ERR "FAT: bogus sectors per cluster %u\n",
884                                sbi->sec_per_clus);
885                 brelse(bh);
886                 goto out_invalid;
887         }
888
889         if (logical_sector_size < sb->s_blocksize) {
890                 printk(KERN_ERR "FAT: logical sector size too small for device"
891                        " (logical sector size = %u)\n", logical_sector_size);
892                 brelse(bh);
893                 goto out_fail;
894         }
895         if (logical_sector_size > sb->s_blocksize) {
896                 brelse(bh);
897
898                 if (!sb_set_blocksize(sb, logical_sector_size)) {
899                         printk(KERN_ERR "FAT: unable to set blocksize %u\n",
900                                logical_sector_size);
901                         goto out_fail;
902                 }
903                 bh = sb_bread(sb, 0);
904                 if (bh == NULL) {
905                         printk(KERN_ERR "FAT: unable to read boot sector"
906                                " (logical sector size = %lu)\n",
907                                sb->s_blocksize);
908                         goto out_fail;
909                 }
910                 b = (struct fat_boot_sector *) bh->b_data;
911         }
912
913         sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
914         sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
915         sbi->fats = b->fats;
916         sbi->fat_bits = 0;              /* Don't know yet */
917         sbi->fat_start = le16_to_cpu(b->reserved);
918         sbi->fat_length = le16_to_cpu(b->fat_length);
919         sbi->root_cluster = 0;
920         sbi->free_clusters = -1;        /* Don't know yet */
921         sbi->prev_free = -1;
922
923         if (!sbi->fat_length && b->fat32_length) {
924                 struct fat_boot_fsinfo *fsinfo;
925                 struct buffer_head *fsinfo_bh;
926
927                 /* Must be FAT32 */
928                 sbi->fat_bits = 32;
929                 sbi->fat_length = le32_to_cpu(b->fat32_length);
930                 sbi->root_cluster = le32_to_cpu(b->root_cluster);
931
932                 sb->s_maxbytes = 0xffffffff;
933                 
934                 /* MC - if info_sector is 0, don't multiply by 0 */
935                 sbi->fsinfo_sector = le16_to_cpu(b->info_sector);
936                 if (sbi->fsinfo_sector == 0)
937                         sbi->fsinfo_sector = 1;
938
939                 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
940                 if (fsinfo_bh == NULL) {
941                         printk(KERN_ERR "FAT: bread failed, FSINFO block"
942                                " (sector = %lu)\n", sbi->fsinfo_sector);
943                         brelse(bh);
944                         goto out_fail;
945                 }
946
947                 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
948                 if (!IS_FSINFO(fsinfo)) {
949                         printk(KERN_WARNING
950                                "FAT: Did not find valid FSINFO signature.\n"
951                                "     Found signature1 0x%08x signature2 0x%08x"
952                                " (sector = %lu)\n",
953                                le32_to_cpu(fsinfo->signature1),
954                                le32_to_cpu(fsinfo->signature2),
955                                sbi->fsinfo_sector);
956                 } else {
957                         sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
958                         sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
959                 }
960
961                 brelse(fsinfo_bh);
962         }
963
964         sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
965         sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
966
967         sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
968         sbi->dir_entries =
969                 le16_to_cpu(get_unaligned((__le16 *)&b->dir_entries));
970         if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
971                 if (!silent)
972                         printk(KERN_ERR "FAT: bogus directroy-entries per block"
973                                " (%u)\n", sbi->dir_entries);
974                 brelse(bh);
975                 goto out_invalid;
976         }
977
978         rootdir_sectors = sbi->dir_entries
979                 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
980         sbi->data_start = sbi->dir_start + rootdir_sectors;
981         total_sectors = le16_to_cpu(get_unaligned((__le16 *)&b->sectors));
982         if (total_sectors == 0)
983                 total_sectors = le32_to_cpu(b->total_sect);
984
985         total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
986
987         if (sbi->fat_bits != 32)
988                 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
989
990         /* check that FAT table does not overflow */
991         fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
992         total_clusters = min(total_clusters, fat_clusters - 2);
993         if (total_clusters > MAX_FAT(sb)) {
994                 if (!silent)
995                         printk(KERN_ERR "FAT: count of clusters too big (%u)\n",
996                                total_clusters);
997                 brelse(bh);
998                 goto out_invalid;
999         }
1000
1001         sbi->clusters = total_clusters;
1002         /* check the free_clusters, it's not necessarily correct */
1003         if (sbi->free_clusters != -1 && sbi->free_clusters > sbi->clusters)
1004                 sbi->free_clusters = -1;
1005
1006         brelse(bh);
1007
1008         /*
1009          * The low byte of FAT's first entry must have same value with
1010          * media-field.  But in real world, too many devices is
1011          * writing wrong value.  So, removed that validity check.
1012          *
1013          * if (FAT_FIRST_ENT(sb, media) != first)
1014          */
1015
1016         error = -EINVAL;
1017         sprintf(buf, "cp%d", sbi->options.codepage);
1018         sbi->nls_disk = load_nls(buf);
1019         if (!sbi->nls_disk) {
1020                 printk(KERN_ERR "FAT: codepage %s not found\n", buf);
1021                 goto out_fail;
1022         }
1023
1024         /* FIXME: utf8 is using iocharset for upper/lower conversion */
1025         if (sbi->options.isvfat) {
1026                 sbi->nls_io = load_nls(sbi->options.iocharset);
1027                 if (!sbi->nls_io) {
1028                         printk(KERN_ERR "FAT: IO charset %s not found\n",
1029                                sbi->options.iocharset);
1030                         goto out_fail;
1031                 }
1032         }
1033
1034         error = -ENOMEM;
1035         root_inode = new_inode(sb);
1036         if (!root_inode)
1037                 goto out_fail;
1038         root_inode->i_ino = MSDOS_ROOT_INO;
1039         root_inode->i_version = 1;
1040         error = fat_read_root(root_inode);
1041         if (error < 0)
1042                 goto out_fail;
1043         error = -ENOMEM;
1044         insert_inode_hash(root_inode);
1045         sb->s_root = d_alloc_root(root_inode);
1046         if (!sb->s_root) {
1047                 printk(KERN_ERR "FAT: get root inode failed\n");
1048                 goto out_fail;
1049         }
1050
1051         return 0;
1052
1053 out_invalid:
1054         error = -EINVAL;
1055         if (!silent)
1056                 printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
1057                        " on dev %s.\n", sb->s_id);
1058
1059 out_fail:
1060         if (root_inode)
1061                 iput(root_inode);
1062         if (sbi->nls_io)
1063                 unload_nls(sbi->nls_io);
1064         if (sbi->nls_disk)
1065                 unload_nls(sbi->nls_disk);
1066         if (sbi->options.iocharset != fat_default_iocharset)
1067                 kfree(sbi->options.iocharset);
1068         sb->s_fs_info = NULL;
1069         kfree(sbi);
1070         return error;
1071 }
1072
1073 static int fat_statfs(struct super_block *sb, struct kstatfs *buf)
1074 {
1075         int free, nr, ret;
1076        
1077         if (MSDOS_SB(sb)->free_clusters != -1)
1078                 free = MSDOS_SB(sb)->free_clusters;
1079         else {
1080                 lock_fat(sb);
1081                 if (MSDOS_SB(sb)->free_clusters != -1)
1082                         free = MSDOS_SB(sb)->free_clusters;
1083                 else {
1084                         free = 0;
1085                         for (nr = 2; nr < MSDOS_SB(sb)->clusters + 2; nr++) {
1086                                 ret = fat_access(sb, nr, -1);
1087                                 if (ret < 0) {
1088                                         unlock_fat(sb);
1089                                         return ret;
1090                                 } else if (ret == FAT_ENT_FREE)
1091                                         free++;
1092                         }
1093                         MSDOS_SB(sb)->free_clusters = free;
1094                 }
1095                 unlock_fat(sb);
1096         }
1097
1098         buf->f_type = sb->s_magic;
1099         buf->f_bsize = MSDOS_SB(sb)->cluster_size;
1100         buf->f_blocks = MSDOS_SB(sb)->clusters;
1101         buf->f_bfree = free;
1102         buf->f_bavail = free;
1103         buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
1104
1105         return 0;
1106 }
1107
1108 static int is_exec(unsigned char *extension)
1109 {
1110         unsigned char *exe_extensions = "EXECOMBAT", *walk;
1111
1112         for (walk = exe_extensions; *walk; walk += 3)
1113                 if (!strncmp(extension, walk, 3))
1114                         return 1;
1115         return 0;
1116 }
1117
1118 static int fat_writepage(struct page *page, struct writeback_control *wbc)
1119 {
1120         return block_write_full_page(page,fat_get_block, wbc);
1121 }
1122 static int fat_readpage(struct file *file, struct page *page)
1123 {
1124         return block_read_full_page(page,fat_get_block);
1125 }
1126
1127 static int
1128 fat_prepare_write(struct file *file, struct page *page,
1129                         unsigned from, unsigned to)
1130 {
1131         kmap(page);
1132         return cont_prepare_write(page,from,to,fat_get_block,
1133                 &MSDOS_I(page->mapping->host)->mmu_private);
1134 }
1135
1136 static int
1137 fat_commit_write(struct file *file, struct page *page,
1138                         unsigned from, unsigned to)
1139 {
1140         kunmap(page);
1141         return generic_commit_write(file, page, from, to);
1142 }
1143
1144 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
1145 {
1146         return generic_block_bmap(mapping,block,fat_get_block);
1147 }
1148 static struct address_space_operations fat_aops = {
1149         .readpage = fat_readpage,
1150         .writepage = fat_writepage,
1151         .sync_page = block_sync_page,
1152         .prepare_write = fat_prepare_write,
1153         .commit_write = fat_commit_write,
1154         .bmap = _fat_bmap
1155 };
1156
1157 /* doesn't deal with root inode */
1158 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
1159 {
1160         struct super_block *sb = inode->i_sb;
1161         struct msdos_sb_info *sbi = MSDOS_SB(sb);
1162         int error;
1163
1164         MSDOS_I(inode)->i_pos = 0;
1165         inode->i_uid = sbi->options.fs_uid;
1166         inode->i_gid = sbi->options.fs_gid;
1167         inode->i_version++;
1168         inode->i_generation = get_seconds();
1169         
1170         if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
1171                 inode->i_generation &= ~1;
1172                 inode->i_mode = MSDOS_MKMODE(de->attr,
1173                         S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
1174                 inode->i_op = sbi->dir_ops;
1175                 inode->i_fop = &fat_dir_operations;
1176
1177                 MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
1178                 if (sbi->fat_bits == 32)
1179                         MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
1180
1181                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1182                 error = fat_calc_dir_size(inode);
1183                 if (error < 0)
1184                         return error;
1185                 MSDOS_I(inode)->mmu_private = inode->i_size;
1186
1187                 inode->i_nlink = fat_subdirs(inode);
1188         } else { /* not a directory */
1189                 inode->i_generation |= 1;
1190                 inode->i_mode = MSDOS_MKMODE(de->attr,
1191                     ((sbi->options.showexec &&
1192                        !is_exec(de->ext))
1193                         ? S_IRUGO|S_IWUGO : S_IRWXUGO)
1194                     & ~sbi->options.fs_fmask) | S_IFREG;
1195                 MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
1196                 if (sbi->fat_bits == 32)
1197                         MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
1198
1199                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1200                 inode->i_size = le32_to_cpu(de->size);
1201                 inode->i_op = &fat_file_inode_operations;
1202                 inode->i_fop = &fat_file_operations;
1203                 inode->i_mapping->a_ops = &fat_aops;
1204                 MSDOS_I(inode)->mmu_private = inode->i_size;
1205         }
1206         if(de->attr & ATTR_SYS)
1207                 if (sbi->options.sys_immutable)
1208                         inode->i_flags |= S_IMMUTABLE;
1209         MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
1210         /* this is as close to the truth as we can get ... */
1211         inode->i_blksize = sbi->cluster_size;
1212         inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1213                            & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1214         inode->i_mtime.tv_sec = inode->i_atime.tv_sec =
1215                 date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date));
1216         inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0;
1217         inode->i_ctime.tv_sec =
1218                 MSDOS_SB(sb)->options.isvfat
1219                 ? date_dos2unix(le16_to_cpu(de->ctime), le16_to_cpu(de->cdate))
1220                 : inode->i_mtime.tv_sec;
1221         inode->i_ctime.tv_nsec = de->ctime_ms * 1000000;
1222         MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
1223
1224         return 0;
1225 }
1226
1227 static int fat_write_inode(struct inode *inode, int wait)
1228 {
1229         struct super_block *sb = inode->i_sb;
1230         struct msdos_sb_info *sbi = MSDOS_SB(sb);
1231         struct buffer_head *bh;
1232         struct msdos_dir_entry *raw_entry;
1233         loff_t i_pos;
1234
1235 retry:
1236         i_pos = MSDOS_I(inode)->i_pos;
1237         if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
1238                 return 0;
1239         }
1240         lock_kernel();
1241         if (!(bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits))) {
1242                 printk(KERN_ERR "FAT: unable to read inode block "
1243                        "for updating (i_pos %lld)\n", i_pos);
1244                 unlock_kernel();
1245                 return -EIO;
1246         }
1247         spin_lock(&sbi->inode_hash_lock);
1248         if (i_pos != MSDOS_I(inode)->i_pos) {
1249                 spin_unlock(&sbi->inode_hash_lock);
1250                 brelse(bh);
1251                 unlock_kernel();
1252                 goto retry;
1253         }
1254
1255         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
1256             [i_pos & (sbi->dir_per_block - 1)];
1257         if (S_ISDIR(inode->i_mode)) {
1258                 raw_entry->attr = ATTR_DIR;
1259                 raw_entry->size = 0;
1260         }
1261         else {
1262                 raw_entry->attr = ATTR_NONE;
1263                 raw_entry->size = cpu_to_le32(inode->i_size);
1264         }
1265         raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
1266             MSDOS_I(inode)->i_attrs;
1267         raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart);
1268         raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16);
1269         fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time, &raw_entry->date);
1270         if (sbi->options.isvfat) {
1271                 fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
1272                 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms; /* use i_ctime.tv_nsec? */
1273         }
1274         spin_unlock(&sbi->inode_hash_lock);
1275         mark_buffer_dirty(bh);
1276         brelse(bh);
1277         unlock_kernel();
1278         return 0;
1279 }
1280
1281
1282 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1283 {
1284         struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
1285         struct inode *inode = dentry->d_inode;
1286         int mask, error = 0;
1287
1288         lock_kernel();
1289
1290         /* FAT cannot truncate to a longer file */
1291         if (attr->ia_valid & ATTR_SIZE) {
1292                 if (attr->ia_size > inode->i_size) {
1293                         error = -EPERM;
1294                         goto out;
1295                 }
1296         }
1297
1298         error = inode_change_ok(inode, attr);
1299         if (error) {
1300                 if (sbi->options.quiet)
1301                         error = 0;
1302                 goto out;
1303         }
1304
1305         if (((attr->ia_valid & ATTR_UID) && 
1306              (attr->ia_uid != sbi->options.fs_uid)) ||
1307             ((attr->ia_valid & ATTR_GID) && 
1308              (attr->ia_gid != sbi->options.fs_gid)) ||
1309             ((attr->ia_valid & ATTR_MODE) &&
1310              (attr->ia_mode & ~MSDOS_VALID_MODE)))
1311                 error = -EPERM;
1312
1313         if (error) {
1314                 if (sbi->options.quiet)  
1315                         error = 0;
1316                 goto out;
1317         }
1318         error = inode_setattr(inode, attr);
1319         if (error)
1320                 goto out;
1321
1322         if (S_ISDIR(inode->i_mode))
1323                 mask = sbi->options.fs_dmask;
1324         else
1325                 mask = sbi->options.fs_fmask;
1326         inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
1327 out:
1328         unlock_kernel();
1329         return error;
1330 }
1331 MODULE_LICENSE("GPL");