v2.4.8 -> v2.4.8.1
[opensuse:kernel.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/config.h>
14 #include <linux/version.h>
15 #define __NO_VERSION__
16 #include <linux/module.h>
17
18 #include <linux/msdos_fs.h>
19 #include <linux/nls.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/major.h>
25 #include <linux/blkdev.h>
26 #include <linux/fs.h>
27 #include <linux/stat.h>
28 #include <linux/locks.h>
29 #include <linux/fat_cvf.h>
30 #include <linux/slab.h>
31 #include <linux/bitops.h>
32 #include <linux/smp_lock.h>
33
34 #include "msbuffer.h"
35
36 #include <asm/uaccess.h>
37 #include <asm/unaligned.h>
38
39 extern struct cvf_format default_cvf;
40
41 /* #define FAT_PARANOIA 1 */
42 #define DEBUG_LEVEL 0
43 #ifdef FAT_DEBUG
44 #  define PRINTK(x) printk x
45 #else
46 #  define PRINTK(x)
47 #endif
48 #if (DEBUG_LEVEL >= 1)
49 #  define PRINTK1(x) printk x
50 #else
51 #  define PRINTK1(x)
52 #endif
53
54 /*
55  * New FAT inode stuff. We do the following:
56  *      a) i_ino is constant and has nothing with on-disk location.
57  *      b) FAT manages its own cache of directory entries.
58  *      c) *This* cache is indexed by on-disk location.
59  *      d) inode has an associated directory entry, all right, but
60  *              it may be unhashed.
61  *      e) currently entries are stored within struct inode. That should
62  *              change.
63  *      f) we deal with races in the following way:
64  *              1. readdir() and lookup() do FAT-dir-cache lookup.
65  *              2. rename() unhashes the F-d-c entry and rehashes it in
66  *                      a new place.
67  *              3. unlink() and rmdir() unhash F-d-c entry.
68  *              4. fat_write_inode() checks whether the thing is unhashed.
69  *                      If it is we silently return. If it isn't we do bread(),
70  *                      check if the location is still valid and retry if it
71  *                      isn't. Otherwise we do changes.
72  *              5. Spinlock is used to protect hash/unhash/location check/lookup
73  *              6. fat_clear_inode() unhashes the F-d-c entry.
74  *              7. lookup() and readdir() do igrab() if they find a F-d-c entry
75  *                      and consider negative result as cache miss.
76  */
77
78 #define FAT_HASH_BITS   8
79 #define FAT_HASH_SIZE   (1UL << FAT_HASH_BITS)
80 #define FAT_HASH_MASK   (FAT_HASH_SIZE-1)
81 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
82 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
83
84 void fat_hash_init(void) {
85         int i;
86         for(i=0;i<FAT_HASH_SIZE;i++) {
87                 INIT_LIST_HEAD(&fat_inode_hashtable[i]);
88         }
89 }
90
91 static inline unsigned long fat_hash(struct super_block *sb, int i_pos)
92 {
93         unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
94         tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS*2);
95         return tmp & FAT_HASH_MASK;
96 }
97
98 void fat_attach(struct inode *inode, int i_pos) {
99         spin_lock(&fat_inode_lock);
100         MSDOS_I(inode)->i_location = i_pos;
101         list_add(&MSDOS_I(inode)->i_fat_hash,
102                 fat_inode_hashtable+fat_hash(inode->i_sb, i_pos));
103         spin_unlock(&fat_inode_lock);
104 }
105
106 void fat_detach(struct inode *inode) {
107         spin_lock(&fat_inode_lock);
108         MSDOS_I(inode)->i_location = 0;
109         list_del(&MSDOS_I(inode)->i_fat_hash);
110         INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
111         spin_unlock(&fat_inode_lock);
112 }
113
114 struct inode *fat_iget(struct super_block *sb, int i_pos) {
115         struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
116         struct list_head *walk;
117         struct msdos_inode_info *i;
118         struct inode *inode = NULL;
119         spin_lock(&fat_inode_lock);
120         for(walk=p->next;walk!=p;walk=walk->next) {
121                 i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
122                 if (i->i_fat_inode->i_sb != sb)
123                         continue;
124                 if (i->i_location != i_pos)
125                         continue;
126                 inode = igrab(i->i_fat_inode);
127         }
128         spin_unlock(&fat_inode_lock);
129         return inode;
130 }
131
132 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
133
134 struct inode *fat_build_inode(struct super_block *sb,
135                                 struct msdos_dir_entry *de, int ino, int *res)
136 {
137         struct inode *inode;
138         *res = 0;
139         inode = fat_iget(sb, ino);
140         if (inode)
141                 goto out;
142         inode = new_inode(sb);
143         *res = -ENOMEM;
144         if (!inode)
145                 goto out;
146         *res = 0;
147         inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
148         fat_fill_inode(inode, de);
149         fat_attach(inode, ino);
150         insert_inode_hash(inode);
151 out:
152         return inode;
153 }
154
155 void fat_delete_inode(struct inode *inode)
156 {
157         if (!is_bad_inode(inode)) {
158                 lock_kernel();
159                 inode->i_size = 0;
160                 fat_truncate(inode);
161                 unlock_kernel();
162         }
163         clear_inode(inode);
164 }
165
166 void fat_clear_inode(struct inode *inode)
167 {
168         if (is_bad_inode(inode))
169                 return;
170         lock_kernel();
171         spin_lock(&fat_inode_lock);
172         fat_cache_inval_inode(inode);
173         list_del(&MSDOS_I(inode)->i_fat_hash);
174         spin_unlock(&fat_inode_lock);
175         unlock_kernel();
176 }
177
178 void fat_put_super(struct super_block *sb)
179 {
180         if (MSDOS_SB(sb)->cvf_format->cvf_version) {
181                 dec_cvf_format_use_count_by_version(MSDOS_SB(sb)->cvf_format->cvf_version);
182                 MSDOS_SB(sb)->cvf_format->unmount_cvf(sb);
183         }
184         if (MSDOS_SB(sb)->fat_bits == 32) {
185                 fat_clusters_flush(sb);
186         }
187         fat_cache_inval_dev(sb->s_dev);
188         set_blocksize (sb->s_dev,BLOCK_SIZE);
189         if (MSDOS_SB(sb)->nls_disk) {
190                 unload_nls(MSDOS_SB(sb)->nls_disk);
191                 MSDOS_SB(sb)->nls_disk = NULL;
192                 MSDOS_SB(sb)->options.codepage = 0;
193         }
194         if (MSDOS_SB(sb)->nls_io) {
195                 unload_nls(MSDOS_SB(sb)->nls_io);
196                 MSDOS_SB(sb)->nls_io = NULL;
197         }
198         /*
199          * Note: the iocharset option might have been specified
200          * without enabling nls_io, so check for it here.
201          */
202         if (MSDOS_SB(sb)->options.iocharset) {
203                 kfree(MSDOS_SB(sb)->options.iocharset);
204                 MSDOS_SB(sb)->options.iocharset = NULL;
205         }
206 }
207
208
209 static int parse_options(char *options,int *fat, int *debug,
210                          struct fat_mount_options *opts,
211                          char *cvf_format, char *cvf_options)
212 {
213         char *this_char,*value,save,*savep;
214         char *p;
215         int ret = 1, len;
216
217         opts->name_check = 'n';
218         opts->conversion = 'b';
219         opts->fs_uid = current->uid;
220         opts->fs_gid = current->gid;
221         opts->fs_umask = current->fs->umask;
222         opts->quiet = opts->sys_immutable = opts->dotsOK = opts->showexec = 0;
223         opts->codepage = 0;
224         opts->nocase = 0;
225         opts->utf8 = 0;
226         opts->iocharset = NULL;
227         *debug = *fat = 0;
228
229         if (!options)
230                 goto out;
231         save = 0;
232         savep = NULL;
233         for (this_char = strtok(options,","); this_char;
234              this_char = strtok(NULL,",")) {
235                 if ((value = strchr(this_char,'=')) != NULL) {
236                         save = *value;
237                         savep = value;
238                         *value++ = 0;
239                 }
240                 if (!strcmp(this_char,"check") && value) {
241                         if (value[0] && !value[1] && strchr("rns",*value))
242                                 opts->name_check = *value;
243                         else if (!strcmp(value,"relaxed"))
244                                 opts->name_check = 'r';
245                         else if (!strcmp(value,"normal"))
246                                 opts->name_check = 'n';
247                         else if (!strcmp(value,"strict"))
248                                 opts->name_check = 's';
249                         else ret = 0;
250                 }
251                 else if (!strcmp(this_char,"conv") && value) {
252                         if (value[0] && !value[1] && strchr("bta",*value))
253                                 opts->conversion = *value;
254                         else if (!strcmp(value,"binary"))
255                                 opts->conversion = 'b';
256                         else if (!strcmp(value,"text"))
257                                 opts->conversion = 't';
258                         else if (!strcmp(value,"auto"))
259                                 opts->conversion = 'a';
260                         else ret = 0;
261                 }
262                 else if (!strcmp(this_char,"dots")) {
263                         opts->dotsOK = 1;
264                 }
265                 else if (!strcmp(this_char,"nocase")) {
266                         opts->nocase = 1;
267                 }
268                 else if (!strcmp(this_char,"nodots")) {
269                         opts->dotsOK = 0;
270                 }
271                 else if (!strcmp(this_char,"showexec")) {
272                         opts->showexec = 1;
273                 }
274                 else if (!strcmp(this_char,"dotsOK") && value) {
275                         if (!strcmp(value,"yes")) opts->dotsOK = 1;
276                         else if (!strcmp(value,"no")) opts->dotsOK = 0;
277                         else ret = 0;
278                 }
279                 else if (!strcmp(this_char,"uid")) {
280                         if (!value || !*value) ret = 0;
281                         else {
282                                 opts->fs_uid = simple_strtoul(value,&value,0);
283                                 if (*value) ret = 0;
284                         }
285                 }
286                 else if (!strcmp(this_char,"gid")) {
287                         if (!value || !*value) ret= 0;
288                         else {
289                                 opts->fs_gid = simple_strtoul(value,&value,0);
290                                 if (*value) ret = 0;
291                         }
292                 }
293                 else if (!strcmp(this_char,"umask")) {
294                         if (!value || !*value) ret = 0;
295                         else {
296                                 opts->fs_umask = simple_strtoul(value,&value,8);
297                                 if (*value) ret = 0;
298                         }
299                 }
300                 else if (!strcmp(this_char,"debug")) {
301                         if (value) ret = 0;
302                         else *debug = 1;
303                 }
304                 else if (!strcmp(this_char,"fat")) {
305                         if (!value || !*value) ret = 0;
306                         else {
307                                 *fat = simple_strtoul(value,&value,0);
308                                 if (*value || (*fat != 12 && *fat != 16 &&
309                                                *fat != 32)) 
310                                         ret = 0;
311                         }
312                 }
313                 else if (!strcmp(this_char,"quiet")) {
314                         if (value) ret = 0;
315                         else opts->quiet = 1;
316                 }
317                 else if (!strcmp(this_char,"blocksize")) {
318                         printk("FAT: blocksize option is obsolete, "
319                                "not supported now\n");
320                 }
321                 else if (!strcmp(this_char,"sys_immutable")) {
322                         if (value) ret = 0;
323                         else opts->sys_immutable = 1;
324                 }
325                 else if (!strcmp(this_char,"codepage") && value) {
326                         opts->codepage = simple_strtoul(value,&value,0);
327                         if (*value) ret = 0;
328                         else printk ("MSDOS FS: Using codepage %d\n",
329                                         opts->codepage);
330                 }
331                 else if (!strcmp(this_char,"iocharset") && value) {
332                         p = value;
333                         while (*value && *value != ',') value++;
334                         len = value - p;
335                         if (len) { 
336                                 char * buffer = kmalloc(len+1, GFP_KERNEL);
337                                 if (buffer) {
338                                         opts->iocharset = buffer;
339                                         memcpy(buffer, p, len);
340                                         buffer[len] = 0;
341                                         printk("MSDOS FS: IO charset %s\n",
342                                                 buffer);
343                                 } else
344                                         ret = 0;
345                         }
346                 }
347                 else if (!strcmp(this_char,"cvf_format")) {
348                         if (!value)
349                                 return 0;
350                         strncpy(cvf_format,value,20);
351                 }
352                 else if (!strcmp(this_char,"cvf_options")) {
353                         if (!value)
354                                 return 0;
355                         strncpy(cvf_options,value,100);
356                 }
357
358                 if (this_char != options) *(this_char-1) = ',';
359                 if (value) *savep = save;
360                 if (ret == 0)
361                         break;
362         }
363 out:
364         return ret;
365 }
366
367 static void fat_read_root(struct inode *inode)
368 {
369         struct super_block *sb = inode->i_sb;
370         struct msdos_sb_info *sbi = MSDOS_SB(sb);
371         int nr;
372
373         INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
374         MSDOS_I(inode)->i_location = 0;
375         MSDOS_I(inode)->i_fat_inode = inode;
376         inode->i_uid = sbi->options.fs_uid;
377         inode->i_gid = sbi->options.fs_gid;
378         inode->i_version = ++event;
379         inode->i_generation = 0;
380         inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_umask) | S_IFDIR;
381         inode->i_op = sbi->dir_ops;
382         inode->i_fop = &fat_dir_operations;
383         if (sbi->fat_bits == 32) {
384                 MSDOS_I(inode)->i_start = sbi->root_cluster;
385                 if ((nr = MSDOS_I(inode)->i_start) != 0) {
386                         while (nr != -1) {
387                                 inode->i_size += 1 << sbi->cluster_bits;
388                                 if (!(nr = fat_access(sb, nr, -1))) {
389                                         printk("Directory %ld: bad FAT\n",
390                                                inode->i_ino);
391                                         break;
392                                 }
393                         }
394                 }
395         } else {
396                 MSDOS_I(inode)->i_start = 0;
397                 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
398         }
399         inode->i_blksize = 1 << sbi->cluster_bits;
400         inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
401                            & ~(inode->i_blksize - 1)) / 512;
402         MSDOS_I(inode)->i_logstart = 0;
403         MSDOS_I(inode)->mmu_private = inode->i_size;
404
405         MSDOS_I(inode)->i_attrs = 0;
406         inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
407         MSDOS_I(inode)->i_ctime_ms = 0;
408         inode->i_nlink = fat_subdirs(inode)+2;
409 }
410
411 /*
412  * a FAT file handle with fhtype 3 is
413  *  0/  i_ino - for fast, reliable lookup if still in the cache
414  *  1/  i_generation - to see if i_ino is still valid
415  *          bit 0 == 0 iff directory
416  *  2/  i_location - if ino has changed, but still in cache
417  *  3/  i_logstart - to semi-verify inode found at i_location
418  *  4/  parent->i_logstart - maybe used to hunt for the file on disc
419  *
420  */
421 struct dentry *fat_fh_to_dentry(struct super_block *sb, __u32 *fh,
422                                 int len, int fhtype, int parent)
423 {
424         struct inode *inode = NULL;
425         struct list_head *lp;
426         struct dentry *result;
427
428         if (fhtype != 3)
429                 return NULL;
430         if (len < 5)
431                 return NULL;
432         if (parent)
433                 return NULL; /* We cannot find the parent,
434                                 It better just *be* there */
435
436         inode = iget(sb, fh[0]);
437         if (!inode || is_bad_inode(inode) ||
438             inode->i_generation != fh[1]) {
439                 if (inode) iput(inode);
440                 inode = NULL;
441         }
442         if (!inode) {
443                 /* try 2 - see if i_location is in F-d-c
444                  * require i_logstart to be the same
445                  * Will fail if you truncate and then re-write
446                  */
447
448                 inode = fat_iget(sb, fh[2]);
449                 if (inode && MSDOS_I(inode)->i_logstart != fh[3]) {
450                         iput(inode);
451                         inode = NULL;
452                 }
453         }
454         if (!inode) {
455                 /* For now, do nothing
456                  * What we could do is:
457                  * follow the file starting at fh[4], and record
458                  * the ".." entry, and the name of the fh[2] entry.
459                  * The follow the ".." file finding the next step up.
460                  * This way we build a path to the root of
461                  * the tree. If this works, we lookup the path and so
462                  * get this inode into the cache.
463                  * Finally try the fat_iget lookup again
464                  * If that fails, then weare totally out of luck
465                  * But all that is for another day
466                  */
467         }
468         if (!inode)
469                 return ERR_PTR(-ESTALE);
470
471         
472         /* now to find a dentry.
473          * If possible, get a well-connected one
474          *
475          * Given the way that we found the inode, it *MUST* be
476          * well-connected, but it is easiest to just copy the
477          * code.
478          */
479         spin_lock(&dcache_lock);
480         for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
481                 result = list_entry(lp,struct dentry, d_alias);
482                 if (! (result->d_flags & DCACHE_NFSD_DISCONNECTED)) {
483                         dget_locked(result);
484                         result->d_vfs_flags |= DCACHE_REFERENCED;
485                         spin_unlock(&dcache_lock);
486                         iput(inode);
487                         return result;
488                 }
489         }
490         spin_unlock(&dcache_lock);
491         result = d_alloc_root(inode);
492         if (result == NULL) {
493                 iput(inode);
494                 return ERR_PTR(-ENOMEM);
495         }
496         result->d_flags |= DCACHE_NFSD_DISCONNECTED;
497         return result;
498
499                 
500 }
501
502 int fat_dentry_to_fh(struct dentry *de, __u32 *fh, int *lenp, int needparent)
503 {
504         int len = *lenp;
505         struct inode *inode =  de->d_inode;
506         
507         if (len < 5)
508                 return 255; /* no room */
509         *lenp = 5;
510         fh[0] = inode->i_ino;
511         fh[1] = inode->i_generation;
512         fh[2] = MSDOS_I(inode)->i_location;
513         fh[3] = MSDOS_I(inode)->i_logstart;
514         fh[4] = MSDOS_I(de->d_parent->d_inode)->i_logstart;
515         return 3;
516 }
517
518 static struct super_operations fat_sops = { 
519         write_inode:    fat_write_inode,
520         delete_inode:   fat_delete_inode,
521         put_super:      fat_put_super,
522         statfs:         fat_statfs,
523         clear_inode:    fat_clear_inode,
524
525         read_inode:     make_bad_inode,
526         fh_to_dentry:   fat_fh_to_dentry,
527         dentry_to_fh:   fat_dentry_to_fh,
528 };
529
530 /*
531  * Read the super block of an MS-DOS FS.
532  *
533  * Note that this may be called from vfat_read_super
534  * with some fields already initialized.
535  */
536 struct super_block *
537 fat_read_super(struct super_block *sb, void *data, int silent,
538                 struct inode_operations *fs_dir_inode_ops)
539 {
540         struct inode *root_inode;
541         struct buffer_head *bh;
542         struct fat_boot_sector *b;
543         struct msdos_sb_info *sbi = MSDOS_SB(sb);
544         char *p;
545         int logical_sector_size, hard_blksize, fat_clusters = 0;
546         unsigned int total_sectors, rootdir_sectors;
547         int fat32, debug, error, fat, cp;
548         struct fat_mount_options opts;
549         char buf[50];
550         int i;
551         char cvf_format[21];
552         char cvf_options[101];
553
554         cvf_format[0] = '\0';
555         cvf_options[0] = '\0';
556         sbi->cvf_format = NULL;
557         sbi->private_data = NULL;
558
559         sbi->dir_ops = fs_dir_inode_ops;
560
561         sb->s_maxbytes = MAX_NON_LFS;
562         sb->s_op = &fat_sops;
563
564         hard_blksize = get_hardsect_size(sb->s_dev);
565         if (!hard_blksize)
566                 hard_blksize = 512;
567
568         opts.isvfat = sbi->options.isvfat;
569         if (!parse_options((char *) data, &fat, &debug, &opts,
570                            cvf_format, cvf_options))
571                 goto out_fail;
572         /* N.B. we should parse directly into the sb structure */
573         memcpy(&(sbi->options), &opts, sizeof(struct fat_mount_options));
574
575         fat_cache_init();
576
577         sb->s_blocksize = hard_blksize;
578         set_blocksize(sb->s_dev, hard_blksize);
579         bh = bread(sb->s_dev, 0, sb->s_blocksize);
580         if (bh == NULL) {
581                 printk("FAT: unable to read boot sector\n");
582                 goto out_fail;
583         }
584
585 /*
586  * The DOS3 partition size limit is *not* 32M as many people think.  
587  * Instead, it is 64K sectors (with the usual sector size being
588  * 512 bytes, leading to a 32M limit).
589  * 
590  * DOS 3 partition managers got around this problem by faking a 
591  * larger sector size, ie treating multiple physical sectors as 
592  * a single logical sector.
593  * 
594  * We can accommodate this scheme by adjusting our cluster size,
595  * fat_start, and data_start by an appropriate value.
596  *
597  * (by Drew Eckhardt)
598  */
599
600
601         b = (struct fat_boot_sector *) bh->b_data;
602         logical_sector_size =
603                 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
604         if (!logical_sector_size
605             || (logical_sector_size & (logical_sector_size - 1))) {
606                 printk("FAT: bogus logical sector size %d\n",
607                        logical_sector_size);
608                 brelse(bh);
609                 goto out_invalid;
610         }
611
612         sbi->cluster_size = b->cluster_size;
613         if (!sbi->cluster_size
614             || (sbi->cluster_size & (sbi->cluster_size - 1))) {
615                 printk("FAT: bogus cluster size %d\n", sbi->cluster_size);
616                 brelse(bh);
617                 goto out_invalid;
618         }
619
620         if (logical_sector_size < hard_blksize) {
621                 printk("FAT: logical sector size too small for device"
622                        " (logical sector size = %d)\n", logical_sector_size);
623                 brelse(bh);
624                 goto out_invalid;
625         }
626
627         sbi->cluster_bits = ffs(logical_sector_size * sbi->cluster_size) - 1;
628         sbi->fats = b->fats;
629         sbi->fat_start = CF_LE_W(b->reserved);
630         if (!b->fat_length && b->fat32_length) {
631                 struct fat_boot_fsinfo *fsinfo;
632                 struct buffer_head *fsinfo_bh;
633                 int fsinfo_block, fsinfo_offset;
634
635                 /* Must be FAT32 */
636                 fat32 = 1;
637                 sbi->fat_length = CF_LE_L(b->fat32_length);
638                 sbi->root_cluster = CF_LE_L(b->root_cluster);
639
640                 sbi->fsinfo_sector = CF_LE_W(b->info_sector);
641                 /* MC - if info_sector is 0, don't multiply by 0 */
642                 if (sbi->fsinfo_sector == 0)
643                         sbi->fsinfo_sector = 1;
644
645                 fsinfo_block =
646                         (sbi->fsinfo_sector * logical_sector_size) / hard_blksize;
647                 fsinfo_offset =
648                         (sbi->fsinfo_sector * logical_sector_size) % hard_blksize;
649                 fsinfo_bh = bh;
650                 if (bh->b_blocknr != fsinfo_block) {
651                         fsinfo_bh = bread(sb->s_dev, fsinfo_block, hard_blksize);
652                         if (fsinfo_bh == NULL) {
653                                 printk("FAT: bread failed, FSINFO block"
654                                        " (blocknr = %d)\n", fsinfo_block);
655                                 brelse(bh);
656                                 goto out_invalid;
657                         }
658                 }
659                 fsinfo = (struct fat_boot_fsinfo *)&fsinfo_bh->b_data[fsinfo_offset];
660                 if (!IS_FSINFO(fsinfo)) {
661                         printk("FAT: Did not find valid FSINFO signature.\n"
662                                "Found signature1 0x%x signature2 0x%x sector=%ld.\n",
663                                CF_LE_L(fsinfo->signature1),
664                                CF_LE_L(fsinfo->signature2),
665                                sbi->fsinfo_sector);
666                 } else {
667                         sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
668                 }
669
670                 if (bh->b_blocknr != fsinfo_block)
671                         brelse(fsinfo_bh);
672         } else {
673                 fat32 = 0;
674                 sbi->fat_length = CF_LE_W(b->fat_length);
675                 sbi->root_cluster = 0;
676                 sbi->free_clusters = -1; /* Don't know yet */
677         }
678
679         sbi->dir_per_block = logical_sector_size / sizeof(struct msdos_dir_entry);
680         sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
681
682         sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
683         sbi->dir_entries =
684                 CF_LE_W(get_unaligned((unsigned short *)&b->dir_entries));
685         rootdir_sectors = sbi->dir_entries
686                 * sizeof(struct msdos_dir_entry) / logical_sector_size;
687         sbi->data_start = sbi->dir_start + rootdir_sectors;
688         total_sectors = CF_LE_W(get_unaligned((unsigned short *)&b->sectors));
689         if (total_sectors == 0)
690                 total_sectors = CF_LE_L(b->total_sect);
691         sbi->clusters = (total_sectors - sbi->data_start) / sbi->cluster_size;
692
693         error = 0;
694         if (!error) {
695                 sbi->fat_bits = fat32 ? 32 :
696                         (fat ? fat :
697                          (sbi->clusters > MSDOS_FAT12 ? 16 : 12));
698                 fat_clusters =
699                         sbi->fat_length * logical_sector_size * 8 / sbi->fat_bits;
700                 error = !sbi->fats || (sbi->dir_entries & (sbi->dir_per_block - 1))
701                         || sbi->clusters + 2 > fat_clusters + MSDOS_MAX_EXTRA
702                         || logical_sector_size < 512
703                         || PAGE_CACHE_SIZE < logical_sector_size
704                         || !b->secs_track || !b->heads;
705         }
706         brelse(bh);
707
708         sb->s_blocksize = logical_sector_size;
709         sb->s_blocksize_bits = ffs(logical_sector_size) - 1;
710         set_blocksize(sb->s_dev, sb->s_blocksize);
711         sbi->cvf_format = &default_cvf;
712         if (!strcmp(cvf_format, "none"))
713                 i = -1;
714         else
715                 i = detect_cvf(sb,cvf_format);
716         if (i >= 0)
717                 error = cvf_formats[i]->mount_cvf(sb, cvf_options);
718         if (error || debug) {
719                 /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
720                 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
721                        "uid=%d,gid=%d,umask=%03o%s]\n",
722                        sbi->fat_bits,opts.name_check,
723                        opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
724                        MSDOS_CAN_BMAP(sbi) ? ",bmap" : "");
725                 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld,"
726                        "se=%u,ts=%u,ls=%d,rc=%ld,fc=%u]\n",
727                        b->media, sbi->cluster_size, sbi->fats,
728                        sbi->fat_start, sbi->fat_length, sbi->dir_start,
729                        sbi->dir_entries, sbi->data_start,
730                        CF_LE_W(get_unaligned((unsigned short *)&b->sectors)),
731                        CF_LE_L(b->total_sect), logical_sector_size,
732                        sbi->root_cluster, sbi->free_clusters);
733                 printk ("hard sector size = %d\n", hard_blksize);
734         }
735         if (i < 0)
736                 if (sbi->clusters + 2 > fat_clusters)
737                         sbi->clusters = fat_clusters - 2;
738         if (error)
739                 goto out_invalid;
740
741         sb->s_magic = MSDOS_SUPER_MAGIC;
742         /* set up enough so that it can read an inode */
743         init_waitqueue_head(&sbi->fat_wait);
744         init_MUTEX(&sbi->fat_lock);
745         sbi->prev_free = 0;
746
747         cp = opts.codepage ? opts.codepage : 437;
748         sprintf(buf, "cp%d", cp);
749         sbi->nls_disk = load_nls(buf);
750         if (! sbi->nls_disk) {
751                 /* Fail only if explicit charset specified */
752                 if (opts.codepage != 0)
753                         goto out_fail;
754                 sbi->options.codepage = 0; /* already 0?? */
755                 sbi->nls_disk = load_nls_default();
756         }
757
758         sbi->nls_io = NULL;
759         if (sbi->options.isvfat && !opts.utf8) {
760                 p = opts.iocharset ? opts.iocharset : CONFIG_NLS_DEFAULT;
761                 sbi->nls_io = load_nls(p);
762                 if (! sbi->nls_io)
763                         /* Fail only if explicit charset specified */
764                         if (opts.iocharset)
765                                 goto out_unload_nls;
766         }
767         if (! sbi->nls_io)
768                 sbi->nls_io = load_nls_default();
769
770         root_inode = new_inode(sb);
771         if (!root_inode)
772                 goto out_unload_nls;
773         root_inode->i_ino = MSDOS_ROOT_INO;
774         fat_read_root(root_inode);
775         insert_inode_hash(root_inode);
776         sb->s_root = d_alloc_root(root_inode);
777         if (!sb->s_root)
778                 goto out_no_root;
779         if(i >= 0) {
780                 sbi->cvf_format = cvf_formats[i];
781                 ++cvf_format_use_count[i];
782         }
783         return sb;
784
785 out_no_root:
786         printk("FAT: get root inode failed\n");
787         iput(root_inode);
788         unload_nls(sbi->nls_io);
789 out_unload_nls:
790         unload_nls(sbi->nls_disk);
791         goto out_fail;
792 out_invalid:
793         if (!silent) {
794                 printk("VFS: Can't find a valid FAT filesystem on dev %s.\n",
795                         kdevname(sb->s_dev));
796         }
797 out_fail:
798         if (opts.iocharset) {
799                 printk("FAT: freeing iocharset=%s\n", opts.iocharset);
800                 kfree(opts.iocharset);
801         }
802         if(sbi->private_data)
803                 kfree(sbi->private_data);
804         sbi->private_data = NULL;
805  
806         return NULL;
807 }
808
809 int fat_statfs(struct super_block *sb,struct statfs *buf)
810 {
811         int free,nr;
812        
813         if (MSDOS_SB(sb)->cvf_format &&
814             MSDOS_SB(sb)->cvf_format->cvf_statfs)
815                 return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf,
816                                                 sizeof(struct statfs));
817           
818         lock_fat(sb);
819         if (MSDOS_SB(sb)->free_clusters != -1)
820                 free = MSDOS_SB(sb)->free_clusters;
821         else {
822                 free = 0;
823                 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
824                         if (!fat_access(sb,nr,-1)) free++;
825                 MSDOS_SB(sb)->free_clusters = free;
826         }
827         unlock_fat(sb);
828         buf->f_type = sb->s_magic;
829         buf->f_bsize = 1 << MSDOS_SB(sb)->cluster_bits;
830         buf->f_blocks = MSDOS_SB(sb)->clusters;
831         buf->f_bfree = free;
832         buf->f_bavail = free;
833         buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
834         return 0;
835 }
836
837 static int is_exec(char *extension)
838 {
839         char *exe_extensions = "EXECOMBAT", *walk;
840
841         for (walk = exe_extensions; *walk; walk += 3)
842                 if (!strncmp(extension, walk, 3))
843                         return 1;
844         return 0;
845 }
846
847 static int fat_writepage(struct page *page)
848 {
849         return block_write_full_page(page,fat_get_block);
850 }
851 static int fat_readpage(struct file *file, struct page *page)
852 {
853         return block_read_full_page(page,fat_get_block);
854 }
855 static int fat_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
856 {
857         return cont_prepare_write(page,from,to,fat_get_block,
858                 &MSDOS_I(page->mapping->host)->mmu_private);
859 }
860 static int _fat_bmap(struct address_space *mapping, long block)
861 {
862         return generic_block_bmap(mapping,block,fat_get_block);
863 }
864 static struct address_space_operations fat_aops = {
865         readpage: fat_readpage,
866         writepage: fat_writepage,
867         sync_page: block_sync_page,
868         prepare_write: fat_prepare_write,
869         commit_write: generic_commit_write,
870         bmap: _fat_bmap
871 };
872
873 /* doesn't deal with root inode */
874 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
875 {
876         struct super_block *sb = inode->i_sb;
877         struct msdos_sb_info *sbi = MSDOS_SB(sb);
878         int nr;
879
880         INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
881         MSDOS_I(inode)->i_location = 0;
882         MSDOS_I(inode)->i_fat_inode = inode;
883         inode->i_uid = sbi->options.fs_uid;
884         inode->i_gid = sbi->options.fs_gid;
885         inode->i_version = ++event;
886         inode->i_generation = CURRENT_TIME;
887         
888         if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
889                 inode->i_generation &= ~1;
890                 inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO &
891                     ~sbi->options.fs_umask) | S_IFDIR;
892                 inode->i_op = sbi->dir_ops;
893                 inode->i_fop = &fat_dir_operations;
894
895                 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
896                 if (sbi->fat_bits == 32) {
897                         MSDOS_I(inode)->i_start |=
898                                 (CF_LE_W(de->starthi) << 16);
899                 }
900                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
901                 inode->i_nlink = fat_subdirs(inode);
902                     /* includes .., compensating for "self" */
903 #ifdef DEBUG
904                 if (!inode->i_nlink) {
905                         printk("directory %d: i_nlink == 0\n",inode->i_ino);
906                         inode->i_nlink = 1;
907                 }
908 #endif
909                 if ((nr = MSDOS_I(inode)->i_start) != 0)
910                         while (nr != -1) {
911                                 inode->i_size += 1 << sbi->cluster_bits;
912                                 if (!(nr = fat_access(sb, nr, -1))) {
913                                         printk("Directory %ld: bad FAT\n",
914                                             inode->i_ino);
915                                         break;
916                                 }
917                         }
918                 MSDOS_I(inode)->mmu_private = inode->i_size;
919         } else { /* not a directory */
920                 inode->i_generation |= 1;
921                 inode->i_mode = MSDOS_MKMODE(de->attr,
922                     ((IS_NOEXEC(inode) || 
923                       (sbi->options.showexec &&
924                        !is_exec(de->ext)))
925                         ? S_IRUGO|S_IWUGO : S_IRWXUGO)
926                     & ~sbi->options.fs_umask) | S_IFREG;
927                 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
928                 if (sbi->fat_bits == 32) {
929                         MSDOS_I(inode)->i_start |=
930                                 (CF_LE_W(de->starthi) << 16);
931                 }
932                 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
933                 inode->i_size = CF_LE_L(de->size);
934                 inode->i_op = &fat_file_inode_operations;
935                 inode->i_fop = &fat_file_operations;
936                 inode->i_mapping->a_ops = &fat_aops;
937                 MSDOS_I(inode)->mmu_private = inode->i_size;
938         }
939         if(de->attr & ATTR_SYS)
940                 if (sbi->options.sys_immutable)
941                         inode->i_flags |= S_IMMUTABLE;
942         MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
943         /* this is as close to the truth as we can get ... */
944         inode->i_blksize = 1 << sbi->cluster_bits;
945         inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
946                            & ~(inode->i_blksize - 1)) / 512;
947         inode->i_mtime = inode->i_atime =
948                 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
949         inode->i_ctime =
950                 MSDOS_SB(sb)->options.isvfat
951                 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
952                 : inode->i_mtime;
953         MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
954 }
955
956 void fat_write_inode(struct inode *inode, int wait)
957 {
958         struct super_block *sb = inode->i_sb;
959         struct buffer_head *bh;
960         struct msdos_dir_entry *raw_entry;
961         int i_pos;
962
963 retry:
964         i_pos = MSDOS_I(inode)->i_location;
965         if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
966                 return;
967         }
968         lock_kernel();
969         if (!(bh = fat_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
970                 printk("dev = %s, ino = %d\n", kdevname(inode->i_dev), i_pos);
971                 fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block");
972                 unlock_kernel();
973                 return;
974         }
975         spin_lock(&fat_inode_lock);
976         if (i_pos != MSDOS_I(inode)->i_location) {
977                 spin_unlock(&fat_inode_lock);
978                 fat_brelse(sb, bh);
979                 unlock_kernel();
980                 goto retry;
981         }
982
983         raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
984             [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
985         if (S_ISDIR(inode->i_mode)) {
986                 raw_entry->attr = ATTR_DIR;
987                 raw_entry->size = 0;
988         }
989         else {
990                 raw_entry->attr = ATTR_NONE;
991                 raw_entry->size = CT_LE_L(inode->i_size);
992         }
993         raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
994             MSDOS_I(inode)->i_attrs;
995         raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
996         raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
997         fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
998         raw_entry->time = CT_LE_W(raw_entry->time);
999         raw_entry->date = CT_LE_W(raw_entry->date);
1000         if (MSDOS_SB(sb)->options.isvfat) {
1001                 fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
1002                 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms;
1003                 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
1004                 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
1005         }
1006         spin_unlock(&fat_inode_lock);
1007         fat_mark_buffer_dirty(sb, bh);
1008         fat_brelse(sb, bh);
1009         unlock_kernel();
1010 }
1011
1012
1013 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1014 {
1015         struct super_block *sb = dentry->d_sb;
1016         struct inode *inode = dentry->d_inode;
1017         int error;
1018
1019         /* FAT cannot truncate to a longer file */
1020         if (attr->ia_valid & ATTR_SIZE) {
1021                 if (attr->ia_size > inode->i_size)
1022                         return -EPERM;
1023         }
1024
1025         error = inode_change_ok(inode, attr);
1026         if (error)
1027                 return MSDOS_SB(sb)->options.quiet ? 0 : error;
1028
1029         if (((attr->ia_valid & ATTR_UID) && 
1030              (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
1031             ((attr->ia_valid & ATTR_GID) && 
1032              (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
1033             ((attr->ia_valid & ATTR_MODE) &&
1034              (attr->ia_mode & ~MSDOS_VALID_MODE)))
1035                 error = -EPERM;
1036
1037         if (error)
1038                 return MSDOS_SB(sb)->options.quiet ? 0 : error;
1039
1040         inode_setattr(inode, attr);
1041
1042         if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
1043                 inode->i_mode &= S_IFMT | S_IRUGO | S_IWUGO;
1044         else
1045                 inode->i_mode |= S_IXUGO;
1046
1047         inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
1048             & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
1049             ~MSDOS_SB(sb)->options.fs_umask;
1050         return 0;
1051 }