Merge with 2.3.99-pre1.
[linux-2.6/linux-mips.git] / fs / fat / inode.c
blobcedd3ba2bda254b8ec698788087e2a0b579db8ee
1 /*
2 * linux/fs/fat/inode.c
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
8 * Fixes:
10 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
13 #include <linux/version.h>
14 #define __NO_VERSION__
15 #include <linux/module.h>
17 #include <linux/msdos_fs.h>
18 #include <linux/nls.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/string.h>
23 #include <linux/major.h>
24 #include <linux/blkdev.h>
25 #include <linux/fs.h>
26 #include <linux/stat.h>
27 #include <linux/locks.h>
28 #include <linux/fat_cvf.h>
29 #include <linux/malloc.h>
31 #include "msbuffer.h"
33 #include <asm/uaccess.h>
34 #include <asm/unaligned.h>
36 extern struct cvf_format default_cvf, bigblock_cvf;
38 /* #define FAT_PARANOIA 1 */
39 #define DEBUG_LEVEL 0
40 #ifdef FAT_DEBUG
41 # define PRINTK(x) printk x
42 #else
43 # define PRINTK(x)
44 #endif
45 #if (DEBUG_LEVEL >= 1)
46 # define PRINTK1(x) printk x
47 #else
48 # define PRINTK1(x)
49 #endif
52 * New FAT inode stuff. We do the following:
53 * a) i_ino is constant and has nothing with on-disk location.
54 * b) FAT manages its own cache of directory entries.
55 * c) *This* cache is indexed by on-disk location.
56 * d) inode has an associated directory entry, all right, but
57 * it may be unhashed.
58 * e) currently entries are stored within struct inode. That should
59 * change.
60 * f) we deal with races in the following way:
61 * 1. readdir() and lookup() do FAT-dir-cache lookup.
62 * 2. rename() unhashes the F-d-c entry and rehashes it in
63 * a new place.
64 * 3. unlink() and rmdir() unhash F-d-c entry.
65 * 4. fat_write_inode() checks whether the thing is unhashed.
66 * If it is we silently return. If it isn't we do bread(),
67 * check if the location is still valid and retry if it
68 * isn't. Otherwise we do changes.
69 * 5. Spinlock is used to protect hash/unhash/location check/lookup
70 * 6. fat_clear_inode() unhashes the F-d-c entry.
71 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
72 * and consider negative result as cache miss.
75 #define FAT_HASH_BITS 8
76 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
77 #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
78 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
79 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
81 void fat_hash_init(void) {
82 int i;
83 for(i=0;i<FAT_HASH_SIZE;i++) {
84 INIT_LIST_HEAD(&fat_inode_hashtable[i]);
88 static inline unsigned long fat_hash(struct super_block *sb, int i_pos)
90 unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
91 tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS*2);
92 return tmp & FAT_HASH_MASK;
95 void fat_attach(struct inode *inode, int i_pos) {
96 spin_lock(&fat_inode_lock);
97 MSDOS_I(inode)->i_location = i_pos;
98 list_add(&MSDOS_I(inode)->i_fat_hash,
99 fat_inode_hashtable+fat_hash(inode->i_sb, i_pos));
100 spin_unlock(&fat_inode_lock);
103 void fat_detach(struct inode *inode) {
104 spin_lock(&fat_inode_lock);
105 MSDOS_I(inode)->i_location = 0;
106 list_del(&MSDOS_I(inode)->i_fat_hash);
107 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
108 spin_unlock(&fat_inode_lock);
111 struct inode *fat_iget(struct super_block *sb, int i_pos) {
112 struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
113 struct list_head *walk;
114 struct msdos_inode_info *i;
115 struct inode *inode = NULL;
116 spin_lock(&fat_inode_lock);
117 for(walk=p->next;walk!=p;walk=walk->next) {
118 i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
119 if (i->i_fat_inode->i_sb != sb)
120 continue;
121 if (i->i_location != i_pos)
122 continue;
123 inode = igrab(i->i_fat_inode);
125 spin_unlock(&fat_inode_lock);
126 return inode;
129 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
131 struct inode *fat_build_inode(struct super_block *sb,
132 struct msdos_dir_entry *de, int ino, int *res)
134 struct inode *inode;
135 *res = 0;
136 inode = fat_iget(sb, ino);
137 if (inode)
138 goto out;
139 inode = get_empty_inode();
140 *res = -ENOMEM;
141 if (!inode)
142 goto out;
143 *res = 0;
144 inode->i_sb = sb;
145 inode->i_dev = sb->s_dev;
146 inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
147 fat_fill_inode(inode, de);
148 fat_attach(inode, ino);
149 insert_inode_hash(inode);
150 out:
151 return inode;
154 void fat_delete_inode(struct inode *inode)
156 inode->i_size = 0;
157 fat_truncate(inode);
158 clear_inode(inode);
161 void fat_clear_inode(struct inode *inode)
163 spin_lock(&fat_inode_lock);
164 fat_cache_inval_inode(inode);
165 list_del(&MSDOS_I(inode)->i_fat_hash);
166 spin_unlock(&fat_inode_lock);
169 void fat_put_super(struct super_block *sb)
171 if (MSDOS_SB(sb)->cvf_format->cvf_version) {
172 dec_cvf_format_use_count_by_version(MSDOS_SB(sb)->cvf_format->cvf_version);
173 MSDOS_SB(sb)->cvf_format->unmount_cvf(sb);
175 if (MSDOS_SB(sb)->fat_bits == 32) {
176 fat_clusters_flush(sb);
178 fat_cache_inval_dev(sb->s_dev);
179 set_blocksize (sb->s_dev,BLOCK_SIZE);
180 if (MSDOS_SB(sb)->nls_disk) {
181 unload_nls(MSDOS_SB(sb)->nls_disk);
182 MSDOS_SB(sb)->nls_disk = NULL;
183 MSDOS_SB(sb)->options.codepage = 0;
185 if (MSDOS_SB(sb)->nls_io) {
186 unload_nls(MSDOS_SB(sb)->nls_io);
187 MSDOS_SB(sb)->nls_io = NULL;
190 * Note: the iocharset option might have been specified
191 * without enabling nls_io, so check for it here.
193 if (MSDOS_SB(sb)->options.iocharset) {
194 kfree(MSDOS_SB(sb)->options.iocharset);
195 MSDOS_SB(sb)->options.iocharset = NULL;
200 static int parse_options(char *options,int *fat, int *blksize, int *debug,
201 struct fat_mount_options *opts,
202 char *cvf_format, char *cvf_options)
204 char *this_char,*value,save,*savep;
205 char *p;
206 int ret = 1, len;
208 opts->name_check = 'n';
209 opts->conversion = 'b';
210 opts->fs_uid = current->uid;
211 opts->fs_gid = current->gid;
212 opts->fs_umask = current->fs->umask;
213 opts->quiet = opts->sys_immutable = opts->dotsOK = opts->showexec = 0;
214 opts->codepage = 0;
215 opts->utf8 = 0;
216 opts->iocharset = NULL;
217 *debug = *fat = 0;
219 if (!options)
220 goto out;
221 save = 0;
222 savep = NULL;
223 for (this_char = strtok(options,","); this_char;
224 this_char = strtok(NULL,",")) {
225 if ((value = strchr(this_char,'=')) != NULL) {
226 save = *value;
227 savep = value;
228 *value++ = 0;
230 if (!strcmp(this_char,"check") && value) {
231 if (value[0] && !value[1] && strchr("rns",*value))
232 opts->name_check = *value;
233 else if (!strcmp(value,"relaxed"))
234 opts->name_check = 'r';
235 else if (!strcmp(value,"normal"))
236 opts->name_check = 'n';
237 else if (!strcmp(value,"strict"))
238 opts->name_check = 's';
239 else ret = 0;
241 else if (!strcmp(this_char,"conv") && value) {
242 if (value[0] && !value[1] && strchr("bta",*value))
243 opts->conversion = *value;
244 else if (!strcmp(value,"binary"))
245 opts->conversion = 'b';
246 else if (!strcmp(value,"text"))
247 opts->conversion = 't';
248 else if (!strcmp(value,"auto"))
249 opts->conversion = 'a';
250 else ret = 0;
252 else if (!strcmp(this_char,"dots")) {
253 opts->dotsOK = 1;
255 else if (!strcmp(this_char,"nodots")) {
256 opts->dotsOK = 0;
258 else if (!strcmp(this_char,"showexec")) {
259 opts->showexec = 1;
261 else if (!strcmp(this_char,"dotsOK") && value) {
262 if (!strcmp(value,"yes")) opts->dotsOK = 1;
263 else if (!strcmp(value,"no")) opts->dotsOK = 0;
264 else ret = 0;
266 else if (!strcmp(this_char,"uid")) {
267 if (!value || !*value) ret = 0;
268 else {
269 opts->fs_uid = simple_strtoul(value,&value,0);
270 if (*value) ret = 0;
273 else if (!strcmp(this_char,"gid")) {
274 if (!value || !*value) ret= 0;
275 else {
276 opts->fs_gid = simple_strtoul(value,&value,0);
277 if (*value) ret = 0;
280 else if (!strcmp(this_char,"umask")) {
281 if (!value || !*value) ret = 0;
282 else {
283 opts->fs_umask = simple_strtoul(value,&value,8);
284 if (*value) ret = 0;
287 else if (!strcmp(this_char,"debug")) {
288 if (value) ret = 0;
289 else *debug = 1;
291 else if (!strcmp(this_char,"fat")) {
292 if (!value || !*value) ret = 0;
293 else {
294 *fat = simple_strtoul(value,&value,0);
295 if (*value || (*fat != 12 && *fat != 16 &&
296 *fat != 32))
297 ret = 0;
300 else if (!strcmp(this_char,"quiet")) {
301 if (value) ret = 0;
302 else opts->quiet = 1;
304 else if (!strcmp(this_char,"blocksize")) {
305 if (!value || !*value) ret = 0;
306 else {
307 *blksize = simple_strtoul(value,&value,0);
308 if (*value || (*blksize != 512 &&
309 *blksize != 1024 && *blksize != 2048))
310 ret = 0;
313 else if (!strcmp(this_char,"sys_immutable")) {
314 if (value) ret = 0;
315 else opts->sys_immutable = 1;
317 else if (!strcmp(this_char,"codepage") && value) {
318 opts->codepage = simple_strtoul(value,&value,0);
319 if (*value) ret = 0;
320 else printk ("MSDOS FS: Using codepage %d\n",
321 opts->codepage);
323 else if (!strcmp(this_char,"iocharset") && value) {
324 p = value;
325 while (*value && *value != ',') value++;
326 len = value - p;
327 if (len) {
328 char * buffer = kmalloc(len+1, GFP_KERNEL);
329 if (buffer) {
330 opts->iocharset = buffer;
331 memcpy(buffer, p, len);
332 buffer[len] = 0;
333 printk("MSDOS FS: IO charset %s\n",
334 buffer);
335 } else
336 ret = 0;
339 else if (!strcmp(this_char,"cvf_format")) {
340 if (!value)
341 return 0;
342 strncpy(cvf_format,value,20);
344 else if (!strcmp(this_char,"cvf_options")) {
345 if (!value)
346 return 0;
347 strncpy(cvf_options,value,100);
350 if (this_char != options) *(this_char-1) = ',';
351 if (value) *savep = save;
352 if (ret == 0)
353 break;
355 out:
356 return ret;
359 static void fat_read_root(struct inode *inode)
361 struct super_block *sb = inode->i_sb;
362 struct msdos_sb_info *sbi = MSDOS_SB(sb);
363 int nr;
365 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
366 MSDOS_I(inode)->i_location = 0;
367 MSDOS_I(inode)->i_fat_inode = inode;
368 inode->i_uid = sbi->options.fs_uid;
369 inode->i_gid = sbi->options.fs_gid;
370 inode->i_version = ++event;
371 inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_umask) | S_IFDIR;
372 inode->i_op = sbi->dir_ops;
373 inode->i_fop = &fat_dir_operations;
374 if (sbi->fat_bits == 32) {
375 MSDOS_I(inode)->i_start = sbi->root_cluster;
376 if ((nr = MSDOS_I(inode)->i_start) != 0) {
377 while (nr != -1) {
378 inode->i_size += SECTOR_SIZE*sbi->cluster_size;
379 if (!(nr = fat_access(sb,nr,-1))) {
380 printk("Directory %ld: bad FAT\n",
381 inode->i_ino);
382 break;
386 } else {
387 MSDOS_I(inode)->i_start = 0;
388 inode->i_size = sbi->dir_entries*
389 sizeof(struct msdos_dir_entry);
391 inode->i_blksize = sbi->cluster_size* SECTOR_SIZE;
392 inode->i_blocks =
393 ((inode->i_size+inode->i_blksize-1)>>sbi->cluster_bits) *
394 sbi->cluster_size;
395 MSDOS_I(inode)->i_logstart = 0;
396 MSDOS_I(inode)->mmu_private = inode->i_size;
398 MSDOS_I(inode)->i_attrs = 0;
399 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
400 MSDOS_I(inode)->i_ctime_ms = 0;
401 inode->i_nlink = fat_subdirs(inode)+2;
404 static struct super_operations fat_sops = {
405 write_inode: fat_write_inode,
406 delete_inode: fat_delete_inode,
407 put_super: fat_put_super,
408 statfs: fat_statfs,
409 clear_inode: fat_clear_inode,
413 * Read the super block of an MS-DOS FS.
415 * Note that this may be called from vfat_read_super
416 * with some fields already initialized.
418 struct super_block *
419 fat_read_super(struct super_block *sb, void *data, int silent,
420 struct inode_operations *fs_dir_inode_ops)
422 struct inode *root_inode;
423 struct buffer_head *bh;
424 struct fat_boot_sector *b;
425 struct msdos_sb_info *sbi = MSDOS_SB(sb);
426 char *p;
427 int data_sectors,logical_sector_size,sector_mult,fat_clusters=0;
428 int debug,error,fat,cp;
429 int blksize = 512;
430 int fat32;
431 struct fat_mount_options opts;
432 char buf[50];
433 int i;
434 char cvf_format[21];
435 char cvf_options[101];
437 cvf_format[0] = '\0';
438 cvf_options[0] = '\0';
439 sbi->cvf_format = NULL;
440 sbi->private_data = NULL;
442 sbi->dir_ops = fs_dir_inode_ops;
443 sb->s_op = &fat_sops;
444 if (hardsect_size[MAJOR(sb->s_dev)] != NULL){
445 blksize = hardsect_size[MAJOR(sb->s_dev)][MINOR(sb->s_dev)];
446 if (blksize != 512){
447 printk ("MSDOS: Hardware sector size is %d\n",blksize);
452 opts.isvfat = sbi->options.isvfat;
453 if (!parse_options((char *) data, &fat, &blksize, &debug, &opts,
454 cvf_format, cvf_options)
455 || (blksize != 512 && blksize != 1024 && blksize != 2048))
456 goto out_fail;
457 /* N.B. we should parse directly into the sb structure */
458 memcpy(&(sbi->options), &opts, sizeof(struct fat_mount_options));
460 fat_cache_init();
461 if( blksize > 1024 )
463 /* Force the superblock to a larger size here. */
464 sb->s_blocksize = blksize;
465 set_blocksize(sb->s_dev, blksize);
467 else
469 /* The first read is always 1024 bytes */
470 sb->s_blocksize = 1024;
471 set_blocksize(sb->s_dev, 1024);
473 bh = bread(sb->s_dev, 0, sb->s_blocksize);
474 if (bh == NULL || !buffer_uptodate(bh)) {
475 brelse (bh);
476 goto out_no_bread;
480 * The DOS3 partition size limit is *not* 32M as many people think.
481 * Instead, it is 64K sectors (with the usual sector size being
482 * 512 bytes, leading to a 32M limit).
484 * DOS 3 partition managers got around this problem by faking a
485 * larger sector size, ie treating multiple physical sectors as
486 * a single logical sector.
488 * We can accommodate this scheme by adjusting our cluster size,
489 * fat_start, and data_start by an appropriate value.
491 * (by Drew Eckhardt)
494 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
495 /* don't divide by zero */
497 b = (struct fat_boot_sector *) bh->b_data;
498 logical_sector_size =
499 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
500 sector_mult = logical_sector_size >> SECTOR_BITS;
501 sbi->cluster_size = b->cluster_size*sector_mult;
502 if (!sbi->cluster_size || (sbi->cluster_size & (sbi->cluster_size-1))) {
503 printk("fatfs: bogus cluster size\n");
504 brelse(bh);
505 goto out_invalid;
507 for (sbi->cluster_bits=0;
508 1<<sbi->cluster_bits<sbi->cluster_size;
509 sbi->cluster_bits++)
511 sbi->cluster_bits += SECTOR_BITS;
512 sbi->fats = b->fats;
513 sbi->fat_start = CF_LE_W(b->reserved)*sector_mult;
514 if (!b->fat_length && b->fat32_length) {
515 struct fat_boot_fsinfo *fsinfo;
517 /* Must be FAT32 */
518 fat32 = 1;
519 sbi->fat_length= CF_LE_L(b->fat32_length)*sector_mult;
520 sbi->root_cluster = CF_LE_L(b->root_cluster);
522 /* MC - if info_sector is 0, don't multiply by 0 */
523 if(CF_LE_W(b->info_sector) == 0) {
524 sbi->fsinfo_offset =
525 logical_sector_size + 0x1e0;
526 } else {
527 sbi->fsinfo_offset =
528 (CF_LE_W(b->info_sector) * logical_sector_size)
529 + 0x1e0;
531 if (sbi->fsinfo_offset + sizeof(struct fat_boot_fsinfo) > sb->s_blocksize) {
532 printk("fat_read_super: Bad fsinfo_offset\n");
533 brelse(bh);
534 goto out_invalid;
536 fsinfo = (struct fat_boot_fsinfo *)
537 &bh->b_data[sbi->fsinfo_offset];
538 if (CF_LE_L(fsinfo->signature) != 0x61417272) {
539 printk("fat_read_super: Did not find valid FSINFO "
540 "signature. Found 0x%x\n",
541 CF_LE_L(fsinfo->signature));
542 } else {
543 sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
545 } else {
546 fat32 = 0;
547 sbi->fat_length = CF_LE_W(b->fat_length)*sector_mult;
548 sbi->root_cluster = 0;
549 sbi->free_clusters = -1; /* Don't know yet */
551 sbi->dir_start= CF_LE_W(b->reserved)*sector_mult+
552 b->fats*sbi->fat_length;
553 sbi->dir_entries =
554 CF_LE_W(get_unaligned((unsigned short *) &b->dir_entries));
555 sbi->data_start = sbi->dir_start+ROUND_TO_MULTIPLE((
556 sbi->dir_entries << MSDOS_DIR_BITS) >> SECTOR_BITS,
557 sector_mult);
558 data_sectors = CF_LE_W(get_unaligned((unsigned short *) &b->sectors));
559 if (!data_sectors) {
560 data_sectors = CF_LE_L(b->total_sect);
562 data_sectors = data_sectors * sector_mult - sbi->data_start;
563 error = !b->cluster_size || !sector_mult;
564 if (!error) {
565 sbi->clusters = b->cluster_size ? data_sectors/
566 b->cluster_size/sector_mult : 0;
567 sbi->fat_bits = fat32 ? 32 :
568 (fat ? fat :
569 (sbi->clusters > MSDOS_FAT12 ? 16 : 12));
570 fat_clusters = sbi->fat_length*SECTOR_SIZE*8/
571 sbi->fat_bits;
572 error = !sbi->fats || (sbi->dir_entries &
573 (MSDOS_DPS-1)) || sbi->clusters+2 > fat_clusters+
574 MSDOS_MAX_EXTRA || (logical_sector_size & (SECTOR_SIZE-1))
575 || !b->secs_track || !b->heads;
577 brelse(bh);
578 set_blocksize(sb->s_dev, blksize);
580 This must be done after the brelse because the bh is a dummy
581 allocated by fat_bread (see buffer.c)
583 sb->s_blocksize = blksize; /* Using this small block size solves */
584 /* the misfit with buffer cache and cluster */
585 /* because clusters (DOS) are often aligned */
586 /* on odd sectors. */
587 sb->s_blocksize_bits = blksize == 512 ? 9 : (blksize == 1024 ? 10 : 11);
588 if (!strcmp(cvf_format,"none"))
589 i = -1;
590 else
591 i = detect_cvf(sb,cvf_format);
592 if (i >= 0)
593 error = cvf_formats[i]->mount_cvf(sb,cvf_options);
594 else if (sb->s_blocksize == 512)
595 sbi->cvf_format = &default_cvf;
596 else
597 sbi->cvf_format = &bigblock_cvf;
598 if (error || debug) {
599 /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
600 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
601 "uid=%d,gid=%d,umask=%03o%s]\n",
602 sbi->fat_bits,opts.name_check,
603 opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
604 MSDOS_CAN_BMAP(sbi) ? ",bmap" : "");
605 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld,"
606 "se=%d,ts=%ld,ls=%d,rc=%ld,fc=%u]\n",
607 b->media,sbi->cluster_size,
608 sbi->fats,sbi->fat_start,
609 sbi->fat_length,
610 sbi->dir_start,sbi->dir_entries,
611 sbi->data_start,
612 CF_LE_W(*(unsigned short *) &b->sectors),
613 (unsigned long)b->total_sect,logical_sector_size,
614 sbi->root_cluster,sbi->free_clusters);
615 printk ("Transaction block size = %d\n",blksize);
617 if (i<0) if (sbi->clusters+2 > fat_clusters)
618 sbi->clusters = fat_clusters-2;
619 if (error)
620 goto out_invalid;
622 sb->s_magic = MSDOS_SUPER_MAGIC;
623 /* set up enough so that it can read an inode */
624 init_waitqueue_head(&sbi->fat_wait);
625 init_MUTEX(&sbi->fat_lock);
626 sbi->prev_free = 0;
628 cp = opts.codepage ? opts.codepage : 437;
629 sprintf(buf, "cp%d", cp);
630 sbi->nls_disk = load_nls(buf);
631 if (! sbi->nls_disk) {
632 /* Fail only if explicit charset specified */
633 if (opts.codepage != 0)
634 goto out_fail;
635 sbi->options.codepage = 0; /* already 0?? */
636 sbi->nls_disk = load_nls_default();
639 sbi->nls_io = NULL;
640 if (sbi->options.isvfat && !opts.utf8) {
641 p = opts.iocharset ? opts.iocharset : "iso8859-1";
642 sbi->nls_io = load_nls(p);
643 if (! sbi->nls_io)
644 /* Fail only if explicit charset specified */
645 if (opts.iocharset)
646 goto out_unload_nls;
648 if (! sbi->nls_io)
649 sbi->nls_io = load_nls_default();
651 root_inode=get_empty_inode();
652 if (!root_inode)
653 goto out_unload_nls;
654 root_inode->i_sb = sb;
655 root_inode->i_dev = sb->s_dev;
656 root_inode->i_ino = MSDOS_ROOT_INO;
657 fat_read_root(root_inode);
658 insert_inode_hash(root_inode);
659 sb->s_root = d_alloc_root(root_inode);
660 if (!sb->s_root)
661 goto out_no_root;
662 if(i>=0) {
663 sbi->cvf_format = cvf_formats[i];
664 ++cvf_format_use_count[i];
666 return sb;
668 out_no_root:
669 printk("get root inode failed\n");
670 iput(root_inode);
671 unload_nls(sbi->nls_io);
672 out_unload_nls:
673 unload_nls(sbi->nls_disk);
674 goto out_fail;
675 out_invalid:
676 if (!silent)
677 printk("VFS: Can't find a valid MSDOS filesystem on dev %s.\n",
678 kdevname(sb->s_dev));
679 goto out_fail;
680 out_no_bread:
681 printk("FAT bread failed\n");
682 out_fail:
683 if (opts.iocharset) {
684 printk("VFS: freeing iocharset=%s\n", opts.iocharset);
685 kfree(opts.iocharset);
687 if(sbi->private_data)
688 kfree(sbi->private_data);
689 sbi->private_data=NULL;
691 return NULL;
694 int fat_statfs(struct super_block *sb,struct statfs *buf)
696 int free,nr;
698 if (MSDOS_SB(sb)->cvf_format &&
699 MSDOS_SB(sb)->cvf_format->cvf_statfs)
700 return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf,
701 sizeof(struct statfs));
703 lock_fat(sb);
704 if (MSDOS_SB(sb)->free_clusters != -1)
705 free = MSDOS_SB(sb)->free_clusters;
706 else {
707 free = 0;
708 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
709 if (!fat_access(sb,nr,-1)) free++;
710 MSDOS_SB(sb)->free_clusters = free;
712 unlock_fat(sb);
713 buf->f_type = sb->s_magic;
714 buf->f_bsize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
715 buf->f_blocks = MSDOS_SB(sb)->clusters;
716 buf->f_bfree = free;
717 buf->f_bavail = free;
718 buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
719 return 0;
722 static int is_exec(char *extension)
724 char *exe_extensions = "EXECOMBAT", *walk;
726 for (walk = exe_extensions; *walk; walk += 3)
727 if (!strncmp(extension, walk, 3))
728 return 1;
729 return 0;
732 static int fat_writepage(struct dentry *dentry, struct page *page)
734 return block_write_full_page(page,fat_get_block);
736 static int fat_readpage(struct dentry *dentry, struct page *page)
738 return block_read_full_page(page,fat_get_block);
740 static int fat_prepare_write(struct page *page, unsigned from, unsigned to)
742 return cont_prepare_write(page,from,to,fat_get_block,
743 &MSDOS_I((struct inode*)page->mapping->host)->mmu_private);
745 static int _fat_bmap(struct address_space *mapping, long block)
747 return generic_block_bmap(mapping,block,fat_get_block);
749 static struct address_space_operations fat_aops = {
750 readpage: fat_readpage,
751 writepage: fat_writepage,
752 prepare_write: fat_prepare_write,
753 commit_write: generic_commit_write,
754 bmap: _fat_bmap
757 /* doesn't deal with root inode */
758 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
760 struct super_block *sb = inode->i_sb;
761 struct msdos_sb_info *sbi = MSDOS_SB(sb);
762 int nr;
764 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
765 MSDOS_I(inode)->i_location = 0;
766 MSDOS_I(inode)->i_fat_inode = inode;
767 inode->i_uid = sbi->options.fs_uid;
768 inode->i_gid = sbi->options.fs_gid;
769 inode->i_version = ++event;
770 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
771 inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO &
772 ~sbi->options.fs_umask) | S_IFDIR;
773 inode->i_op = sbi->dir_ops;
774 inode->i_fop = &fat_dir_operations;
776 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
777 if (sbi->fat_bits == 32) {
778 MSDOS_I(inode)->i_start |=
779 (CF_LE_W(de->starthi) << 16);
781 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
782 inode->i_nlink = fat_subdirs(inode);
783 /* includes .., compensating for "self" */
784 #ifdef DEBUG
785 if (!inode->i_nlink) {
786 printk("directory %d: i_nlink == 0\n",inode->i_ino);
787 inode->i_nlink = 1;
789 #endif
790 inode->i_size = 0;
791 if ((nr = MSDOS_I(inode)->i_start) != 0)
792 while (nr != -1) {
793 inode->i_size += SECTOR_SIZE*sbi->cluster_size;
794 if (!(nr = fat_access(sb,nr,-1))) {
795 printk("Directory %ld: bad FAT\n",
796 inode->i_ino);
797 break;
800 MSDOS_I(inode)->mmu_private = inode->i_size;
801 } else { /* not a directory */
802 inode->i_mode = MSDOS_MKMODE(de->attr,
803 ((IS_NOEXEC(inode) ||
804 (sbi->options.showexec &&
805 !is_exec(de->ext)))
806 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
807 & ~sbi->options.fs_umask) | S_IFREG;
808 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
809 if (sbi->fat_bits == 32) {
810 MSDOS_I(inode)->i_start |=
811 (CF_LE_W(de->starthi) << 16);
813 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
814 inode->i_nlink = 1;
815 inode->i_size = CF_LE_L(de->size);
816 inode->i_op = &fat_file_inode_operations;
817 inode->i_fop = &fat_file_operations;
818 inode->i_mapping->a_ops = &fat_aops;
819 MSDOS_I(inode)->mmu_private = inode->i_size;
821 if(de->attr & ATTR_SYS)
822 if (sbi->options.sys_immutable)
823 inode->i_flags |= S_IMMUTABLE;
824 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
825 /* this is as close to the truth as we can get ... */
826 inode->i_blksize = sbi->cluster_size*SECTOR_SIZE;
827 inode->i_blocks =
828 ((inode->i_size+inode->i_blksize-1)>>sbi->cluster_bits) *
829 sbi->cluster_size;
830 inode->i_mtime = inode->i_atime =
831 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
832 inode->i_ctime =
833 MSDOS_SB(sb)->options.isvfat
834 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
835 : inode->i_mtime;
836 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
839 void fat_write_inode(struct inode *inode)
841 struct super_block *sb = inode->i_sb;
842 struct buffer_head *bh;
843 struct msdos_dir_entry *raw_entry;
844 int i_pos;
846 retry:
847 i_pos = MSDOS_I(inode)->i_location;
848 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) return;
849 if (!(bh = fat_bread(sb, i_pos >> MSDOS_DPB_BITS))) {
850 printk("dev = %s, ino = %d\n", kdevname(inode->i_dev), i_pos);
851 fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block");
852 return;
854 spin_lock(&fat_inode_lock);
855 if (i_pos != MSDOS_I(inode)->i_location) {
856 spin_unlock(&fat_inode_lock);
857 fat_brelse(sb, bh);
858 goto retry;
861 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
862 [i_pos & (MSDOS_DPB-1)];
863 if (S_ISDIR(inode->i_mode)) {
864 raw_entry->attr = ATTR_DIR;
865 raw_entry->size = 0;
867 else {
868 raw_entry->attr = ATTR_NONE;
869 raw_entry->size = CT_LE_L(inode->i_size);
871 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
872 MSDOS_I(inode)->i_attrs;
873 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
874 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
875 fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
876 raw_entry->time = CT_LE_W(raw_entry->time);
877 raw_entry->date = CT_LE_W(raw_entry->date);
878 if (MSDOS_SB(sb)->options.isvfat) {
879 fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
880 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms;
881 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
882 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
884 spin_unlock(&fat_inode_lock);
885 fat_mark_buffer_dirty(sb, bh, 1);
886 fat_brelse(sb, bh);
890 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
892 struct super_block *sb = dentry->d_sb;
893 struct inode *inode = dentry->d_inode;
894 int error;
896 error = inode_change_ok(inode, attr);
897 if (error)
898 return MSDOS_SB(sb)->options.quiet ? 0 : error;
900 if (((attr->ia_valid & ATTR_UID) &&
901 (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
902 ((attr->ia_valid & ATTR_GID) &&
903 (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
904 ((attr->ia_valid & ATTR_MODE) &&
905 (attr->ia_mode & ~MSDOS_VALID_MODE)))
906 error = -EPERM;
908 if (error)
909 return MSDOS_SB(sb)->options.quiet ? 0 : error;
911 inode_setattr(inode, attr);
913 if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
914 inode->i_mode &= S_IFMT | S_IRUGO | S_IWUGO;
915 else
916 inode->i_mode |= S_IXUGO;
918 inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
919 & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
920 ~MSDOS_SB(sb)->options.fs_umask;
921 return 0;