Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / fs / fat / inode.c
blobf13260966e262f2ec418239291c68f10a483fb35
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/config.h>
14 #include <linux/version.h>
15 #define __NO_VERSION__
16 #include <linux/module.h>
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/malloc.h>
31 #include <linux/smp_lock.h>
33 #include "msbuffer.h"
35 #include <asm/uaccess.h>
36 #include <asm/unaligned.h>
38 extern struct cvf_format default_cvf, bigblock_cvf;
40 /* #define FAT_PARANOIA 1 */
41 #define DEBUG_LEVEL 0
42 #ifdef FAT_DEBUG
43 # define PRINTK(x) printk x
44 #else
45 # define PRINTK(x)
46 #endif
47 #if (DEBUG_LEVEL >= 1)
48 # define PRINTK1(x) printk x
49 #else
50 # define PRINTK1(x)
51 #endif
54 * New FAT inode stuff. We do the following:
55 * a) i_ino is constant and has nothing with on-disk location.
56 * b) FAT manages its own cache of directory entries.
57 * c) *This* cache is indexed by on-disk location.
58 * d) inode has an associated directory entry, all right, but
59 * it may be unhashed.
60 * e) currently entries are stored within struct inode. That should
61 * change.
62 * f) we deal with races in the following way:
63 * 1. readdir() and lookup() do FAT-dir-cache lookup.
64 * 2. rename() unhashes the F-d-c entry and rehashes it in
65 * a new place.
66 * 3. unlink() and rmdir() unhash F-d-c entry.
67 * 4. fat_write_inode() checks whether the thing is unhashed.
68 * If it is we silently return. If it isn't we do bread(),
69 * check if the location is still valid and retry if it
70 * isn't. Otherwise we do changes.
71 * 5. Spinlock is used to protect hash/unhash/location check/lookup
72 * 6. fat_clear_inode() unhashes the F-d-c entry.
73 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
74 * and consider negative result as cache miss.
77 #define FAT_HASH_BITS 8
78 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
79 #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
80 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
81 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
83 void fat_hash_init(void) {
84 int i;
85 for(i=0;i<FAT_HASH_SIZE;i++) {
86 INIT_LIST_HEAD(&fat_inode_hashtable[i]);
90 static inline unsigned long fat_hash(struct super_block *sb, int i_pos)
92 unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
93 tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS*2);
94 return tmp & FAT_HASH_MASK;
97 void fat_attach(struct inode *inode, int i_pos) {
98 spin_lock(&fat_inode_lock);
99 MSDOS_I(inode)->i_location = i_pos;
100 list_add(&MSDOS_I(inode)->i_fat_hash,
101 fat_inode_hashtable+fat_hash(inode->i_sb, i_pos));
102 spin_unlock(&fat_inode_lock);
105 void fat_detach(struct inode *inode) {
106 spin_lock(&fat_inode_lock);
107 MSDOS_I(inode)->i_location = 0;
108 list_del(&MSDOS_I(inode)->i_fat_hash);
109 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
110 spin_unlock(&fat_inode_lock);
113 struct inode *fat_iget(struct super_block *sb, int i_pos) {
114 struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
115 struct list_head *walk;
116 struct msdos_inode_info *i;
117 struct inode *inode = NULL;
118 spin_lock(&fat_inode_lock);
119 for(walk=p->next;walk!=p;walk=walk->next) {
120 i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
121 if (i->i_fat_inode->i_sb != sb)
122 continue;
123 if (i->i_location != i_pos)
124 continue;
125 inode = igrab(i->i_fat_inode);
127 spin_unlock(&fat_inode_lock);
128 return inode;
131 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
133 struct inode *fat_build_inode(struct super_block *sb,
134 struct msdos_dir_entry *de, int ino, int *res)
136 struct inode *inode;
137 *res = 0;
138 inode = fat_iget(sb, ino);
139 if (inode)
140 goto out;
141 inode = new_inode(sb);
142 *res = -ENOMEM;
143 if (!inode)
144 goto out;
145 *res = 0;
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 lock_kernel();
157 inode->i_size = 0;
158 fat_truncate(inode);
159 unlock_kernel();
160 clear_inode(inode);
163 void fat_clear_inode(struct inode *inode)
165 lock_kernel();
166 spin_lock(&fat_inode_lock);
167 fat_cache_inval_inode(inode);
168 list_del(&MSDOS_I(inode)->i_fat_hash);
169 spin_unlock(&fat_inode_lock);
170 unlock_kernel();
173 void fat_put_super(struct super_block *sb)
175 if (MSDOS_SB(sb)->cvf_format->cvf_version) {
176 dec_cvf_format_use_count_by_version(MSDOS_SB(sb)->cvf_format->cvf_version);
177 MSDOS_SB(sb)->cvf_format->unmount_cvf(sb);
179 if (MSDOS_SB(sb)->fat_bits == 32) {
180 fat_clusters_flush(sb);
182 fat_cache_inval_dev(sb->s_dev);
183 set_blocksize (sb->s_dev,BLOCK_SIZE);
184 if (MSDOS_SB(sb)->nls_disk) {
185 unload_nls(MSDOS_SB(sb)->nls_disk);
186 MSDOS_SB(sb)->nls_disk = NULL;
187 MSDOS_SB(sb)->options.codepage = 0;
189 if (MSDOS_SB(sb)->nls_io) {
190 unload_nls(MSDOS_SB(sb)->nls_io);
191 MSDOS_SB(sb)->nls_io = NULL;
194 * Note: the iocharset option might have been specified
195 * without enabling nls_io, so check for it here.
197 if (MSDOS_SB(sb)->options.iocharset) {
198 kfree(MSDOS_SB(sb)->options.iocharset);
199 MSDOS_SB(sb)->options.iocharset = NULL;
204 static int parse_options(char *options,int *fat, int *blksize, int *debug,
205 struct fat_mount_options *opts,
206 char *cvf_format, char *cvf_options)
208 char *this_char,*value,save,*savep;
209 char *p;
210 int ret = 1, len;
212 opts->name_check = 'n';
213 opts->conversion = 'b';
214 opts->fs_uid = current->uid;
215 opts->fs_gid = current->gid;
216 opts->fs_umask = current->fs->umask;
217 opts->quiet = opts->sys_immutable = opts->dotsOK = opts->showexec = 0;
218 opts->codepage = 0;
219 opts->nocase = 0;
220 opts->utf8 = 0;
221 opts->iocharset = NULL;
222 *debug = *fat = 0;
224 if (!options)
225 goto out;
226 save = 0;
227 savep = NULL;
228 for (this_char = strtok(options,","); this_char;
229 this_char = strtok(NULL,",")) {
230 if ((value = strchr(this_char,'=')) != NULL) {
231 save = *value;
232 savep = value;
233 *value++ = 0;
235 if (!strcmp(this_char,"check") && value) {
236 if (value[0] && !value[1] && strchr("rns",*value))
237 opts->name_check = *value;
238 else if (!strcmp(value,"relaxed"))
239 opts->name_check = 'r';
240 else if (!strcmp(value,"normal"))
241 opts->name_check = 'n';
242 else if (!strcmp(value,"strict"))
243 opts->name_check = 's';
244 else ret = 0;
246 else if (!strcmp(this_char,"conv") && value) {
247 if (value[0] && !value[1] && strchr("bta",*value))
248 opts->conversion = *value;
249 else if (!strcmp(value,"binary"))
250 opts->conversion = 'b';
251 else if (!strcmp(value,"text"))
252 opts->conversion = 't';
253 else if (!strcmp(value,"auto"))
254 opts->conversion = 'a';
255 else ret = 0;
257 else if (!strcmp(this_char,"dots")) {
258 opts->dotsOK = 1;
260 else if (!strcmp(this_char,"nocase")) {
261 opts->nocase = 1;
263 else if (!strcmp(this_char,"nodots")) {
264 opts->dotsOK = 0;
266 else if (!strcmp(this_char,"showexec")) {
267 opts->showexec = 1;
269 else if (!strcmp(this_char,"dotsOK") && value) {
270 if (!strcmp(value,"yes")) opts->dotsOK = 1;
271 else if (!strcmp(value,"no")) opts->dotsOK = 0;
272 else ret = 0;
274 else if (!strcmp(this_char,"uid")) {
275 if (!value || !*value) ret = 0;
276 else {
277 opts->fs_uid = simple_strtoul(value,&value,0);
278 if (*value) ret = 0;
281 else if (!strcmp(this_char,"gid")) {
282 if (!value || !*value) ret= 0;
283 else {
284 opts->fs_gid = simple_strtoul(value,&value,0);
285 if (*value) ret = 0;
288 else if (!strcmp(this_char,"umask")) {
289 if (!value || !*value) ret = 0;
290 else {
291 opts->fs_umask = simple_strtoul(value,&value,8);
292 if (*value) ret = 0;
295 else if (!strcmp(this_char,"debug")) {
296 if (value) ret = 0;
297 else *debug = 1;
299 else if (!strcmp(this_char,"fat")) {
300 if (!value || !*value) ret = 0;
301 else {
302 *fat = simple_strtoul(value,&value,0);
303 if (*value || (*fat != 12 && *fat != 16 &&
304 *fat != 32))
305 ret = 0;
308 else if (!strcmp(this_char,"quiet")) {
309 if (value) ret = 0;
310 else opts->quiet = 1;
312 else if (!strcmp(this_char,"blocksize")) {
313 if (!value || !*value) ret = 0;
314 else {
315 *blksize = simple_strtoul(value,&value,0);
316 if (*value || (*blksize != 512 &&
317 *blksize != 1024 && *blksize != 2048))
318 ret = 0;
321 else if (!strcmp(this_char,"sys_immutable")) {
322 if (value) ret = 0;
323 else opts->sys_immutable = 1;
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);
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;
347 else if (!strcmp(this_char,"cvf_format")) {
348 if (!value)
349 return 0;
350 strncpy(cvf_format,value,20);
352 else if (!strcmp(this_char,"cvf_options")) {
353 if (!value)
354 return 0;
355 strncpy(cvf_options,value,100);
358 if (this_char != options) *(this_char-1) = ',';
359 if (value) *savep = save;
360 if (ret == 0)
361 break;
363 out:
364 return ret;
367 static void fat_read_root(struct inode *inode)
369 struct super_block *sb = inode->i_sb;
370 struct msdos_sb_info *sbi = MSDOS_SB(sb);
371 int nr;
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_mode = (S_IRWXUGO & ~sbi->options.fs_umask) | S_IFDIR;
380 inode->i_op = sbi->dir_ops;
381 inode->i_fop = &fat_dir_operations;
382 if (sbi->fat_bits == 32) {
383 MSDOS_I(inode)->i_start = sbi->root_cluster;
384 if ((nr = MSDOS_I(inode)->i_start) != 0) {
385 while (nr != -1) {
386 inode->i_size += SECTOR_SIZE*sbi->cluster_size;
387 if (!(nr = fat_access(sb,nr,-1))) {
388 printk("Directory %ld: bad FAT\n",
389 inode->i_ino);
390 break;
394 } else {
395 MSDOS_I(inode)->i_start = 0;
396 inode->i_size = sbi->dir_entries*
397 sizeof(struct msdos_dir_entry);
399 inode->i_blksize = sbi->cluster_size* SECTOR_SIZE;
400 inode->i_blocks =
401 ((inode->i_size+inode->i_blksize-1)>>sbi->cluster_bits) *
402 sbi->cluster_size;
403 MSDOS_I(inode)->i_logstart = 0;
404 MSDOS_I(inode)->mmu_private = inode->i_size;
406 MSDOS_I(inode)->i_attrs = 0;
407 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
408 MSDOS_I(inode)->i_ctime_ms = 0;
409 inode->i_nlink = fat_subdirs(inode)+2;
412 static struct super_operations fat_sops = {
413 write_inode: fat_write_inode,
414 delete_inode: fat_delete_inode,
415 put_super: fat_put_super,
416 statfs: fat_statfs,
417 clear_inode: fat_clear_inode,
421 * Read the super block of an MS-DOS FS.
423 * Note that this may be called from vfat_read_super
424 * with some fields already initialized.
426 struct super_block *
427 fat_read_super(struct super_block *sb, void *data, int silent,
428 struct inode_operations *fs_dir_inode_ops)
430 struct inode *root_inode;
431 struct buffer_head *bh;
432 struct fat_boot_sector *b;
433 struct msdos_sb_info *sbi = MSDOS_SB(sb);
434 char *p;
435 int data_sectors,logical_sector_size,sector_mult,fat_clusters=0;
436 int debug,error,fat,cp;
437 int blksize = 512;
438 int fat32;
439 struct fat_mount_options opts;
440 char buf[50];
441 int i;
442 char cvf_format[21];
443 char cvf_options[101];
445 cvf_format[0] = '\0';
446 cvf_options[0] = '\0';
447 sbi->cvf_format = NULL;
448 sbi->private_data = NULL;
450 sbi->dir_ops = fs_dir_inode_ops;
451 sb->s_op = &fat_sops;
452 if (hardsect_size[MAJOR(sb->s_dev)] != NULL){
453 blksize = hardsect_size[MAJOR(sb->s_dev)][MINOR(sb->s_dev)];
454 if (blksize != 512){
455 printk ("MSDOS: Hardware sector size is %d\n",blksize);
460 opts.isvfat = sbi->options.isvfat;
461 if (!parse_options((char *) data, &fat, &blksize, &debug, &opts,
462 cvf_format, cvf_options)
463 || (blksize != 512 && blksize != 1024 && blksize != 2048))
464 goto out_fail;
465 /* N.B. we should parse directly into the sb structure */
466 memcpy(&(sbi->options), &opts, sizeof(struct fat_mount_options));
468 fat_cache_init();
469 if( blksize > 1024 )
471 /* Force the superblock to a larger size here. */
472 sb->s_blocksize = blksize;
473 set_blocksize(sb->s_dev, blksize);
475 else
477 /* The first read is always 1024 bytes */
478 sb->s_blocksize = 1024;
479 set_blocksize(sb->s_dev, 1024);
481 bh = bread(sb->s_dev, 0, sb->s_blocksize);
482 if (bh == NULL || !buffer_uptodate(bh)) {
483 brelse (bh);
484 goto out_no_bread;
488 * The DOS3 partition size limit is *not* 32M as many people think.
489 * Instead, it is 64K sectors (with the usual sector size being
490 * 512 bytes, leading to a 32M limit).
492 * DOS 3 partition managers got around this problem by faking a
493 * larger sector size, ie treating multiple physical sectors as
494 * a single logical sector.
496 * We can accommodate this scheme by adjusting our cluster size,
497 * fat_start, and data_start by an appropriate value.
499 * (by Drew Eckhardt)
502 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
503 /* don't divide by zero */
505 b = (struct fat_boot_sector *) bh->b_data;
506 logical_sector_size =
507 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
508 sector_mult = logical_sector_size >> SECTOR_BITS;
509 sbi->cluster_size = b->cluster_size*sector_mult;
510 if (!sbi->cluster_size || (sbi->cluster_size & (sbi->cluster_size-1))) {
511 printk("fatfs: bogus cluster size\n");
512 brelse(bh);
513 goto out_invalid;
515 for (sbi->cluster_bits=0;
516 1<<sbi->cluster_bits<sbi->cluster_size;
517 sbi->cluster_bits++)
519 sbi->cluster_bits += SECTOR_BITS;
520 sbi->fats = b->fats;
521 sbi->fat_start = CF_LE_W(b->reserved)*sector_mult;
522 if (!b->fat_length && b->fat32_length) {
523 struct fat_boot_fsinfo *fsinfo;
525 /* Must be FAT32 */
526 fat32 = 1;
527 sbi->fat_length= CF_LE_L(b->fat32_length)*sector_mult;
528 sbi->root_cluster = CF_LE_L(b->root_cluster);
530 /* MC - if info_sector is 0, don't multiply by 0 */
531 if(CF_LE_W(b->info_sector) == 0) {
532 sbi->fsinfo_offset =
533 logical_sector_size + 0x1e0;
534 } else {
535 sbi->fsinfo_offset =
536 (CF_LE_W(b->info_sector) * logical_sector_size)
537 + 0x1e0;
539 if (sbi->fsinfo_offset + sizeof(struct fat_boot_fsinfo) > sb->s_blocksize) {
540 printk("fat_read_super: Bad fsinfo_offset\n");
541 brelse(bh);
542 goto out_invalid;
544 fsinfo = (struct fat_boot_fsinfo *)
545 &bh->b_data[sbi->fsinfo_offset];
546 if (CF_LE_L(fsinfo->signature) != 0x61417272) {
547 printk("fat_read_super: Did not find valid FSINFO "
548 "signature. Found 0x%x\n",
549 CF_LE_L(fsinfo->signature));
550 } else {
551 sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
553 } else {
554 fat32 = 0;
555 sbi->fat_length = CF_LE_W(b->fat_length)*sector_mult;
556 sbi->root_cluster = 0;
557 sbi->free_clusters = -1; /* Don't know yet */
559 sbi->dir_start= CF_LE_W(b->reserved)*sector_mult+
560 b->fats*sbi->fat_length;
561 sbi->dir_entries =
562 CF_LE_W(get_unaligned((unsigned short *) &b->dir_entries));
563 sbi->data_start = sbi->dir_start+ROUND_TO_MULTIPLE((
564 sbi->dir_entries << MSDOS_DIR_BITS) >> SECTOR_BITS,
565 sector_mult);
566 data_sectors = CF_LE_W(get_unaligned((unsigned short *) &b->sectors));
567 if (!data_sectors) {
568 data_sectors = CF_LE_L(b->total_sect);
570 data_sectors = data_sectors * sector_mult - sbi->data_start;
571 error = !b->cluster_size || !sector_mult;
572 if (!error) {
573 sbi->clusters = b->cluster_size ? data_sectors/
574 b->cluster_size/sector_mult : 0;
575 sbi->fat_bits = fat32 ? 32 :
576 (fat ? fat :
577 (sbi->clusters > MSDOS_FAT12 ? 16 : 12));
578 fat_clusters = sbi->fat_length*SECTOR_SIZE*8/
579 sbi->fat_bits;
580 error = !sbi->fats || (sbi->dir_entries &
581 (MSDOS_DPS-1)) || sbi->clusters+2 > fat_clusters+
582 MSDOS_MAX_EXTRA || (logical_sector_size & (SECTOR_SIZE-1))
583 || !b->secs_track || !b->heads;
585 brelse(bh);
586 set_blocksize(sb->s_dev, blksize);
588 This must be done after the brelse because the bh is a dummy
589 allocated by fat_bread (see buffer.c)
591 sb->s_blocksize = blksize; /* Using this small block size solves */
592 /* the misfit with buffer cache and cluster */
593 /* because clusters (DOS) are often aligned */
594 /* on odd sectors. */
595 sb->s_blocksize_bits = blksize == 512 ? 9 : (blksize == 1024 ? 10 : 11);
596 if (!strcmp(cvf_format,"none"))
597 i = -1;
598 else
599 i = detect_cvf(sb,cvf_format);
600 if (i >= 0)
601 error = cvf_formats[i]->mount_cvf(sb,cvf_options);
602 else if (sb->s_blocksize == 512)
603 sbi->cvf_format = &default_cvf;
604 else
605 sbi->cvf_format = &bigblock_cvf;
606 if (error || debug) {
607 /* The MSDOS_CAN_BMAP is obsolete, but left just to remember */
608 printk("[MS-DOS FS Rel. 12,FAT %d,check=%c,conv=%c,"
609 "uid=%d,gid=%d,umask=%03o%s]\n",
610 sbi->fat_bits,opts.name_check,
611 opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
612 MSDOS_CAN_BMAP(sbi) ? ",bmap" : "");
613 printk("[me=0x%x,cs=%d,#f=%d,fs=%d,fl=%ld,ds=%ld,de=%d,data=%ld,"
614 "se=%d,ts=%ld,ls=%d,rc=%ld,fc=%u]\n",
615 b->media,sbi->cluster_size,
616 sbi->fats,sbi->fat_start,
617 sbi->fat_length,
618 sbi->dir_start,sbi->dir_entries,
619 sbi->data_start,
620 CF_LE_W(*(unsigned short *) &b->sectors),
621 (unsigned long)b->total_sect,logical_sector_size,
622 sbi->root_cluster,sbi->free_clusters);
623 printk ("Transaction block size = %d\n",blksize);
625 if (i<0) if (sbi->clusters+2 > fat_clusters)
626 sbi->clusters = fat_clusters-2;
627 if (error)
628 goto out_invalid;
630 sb->s_magic = MSDOS_SUPER_MAGIC;
631 /* set up enough so that it can read an inode */
632 init_waitqueue_head(&sbi->fat_wait);
633 init_MUTEX(&sbi->fat_lock);
634 sbi->prev_free = 0;
636 cp = opts.codepage ? opts.codepage : 437;
637 sprintf(buf, "cp%d", cp);
638 sbi->nls_disk = load_nls(buf);
639 if (! sbi->nls_disk) {
640 /* Fail only if explicit charset specified */
641 if (opts.codepage != 0)
642 goto out_fail;
643 sbi->options.codepage = 0; /* already 0?? */
644 sbi->nls_disk = load_nls_default();
647 sbi->nls_io = NULL;
648 if (sbi->options.isvfat && !opts.utf8) {
649 p = opts.iocharset ? opts.iocharset : CONFIG_NLS_DEFAULT;
650 sbi->nls_io = load_nls(p);
651 if (! sbi->nls_io)
652 /* Fail only if explicit charset specified */
653 if (opts.iocharset)
654 goto out_unload_nls;
656 if (! sbi->nls_io)
657 sbi->nls_io = load_nls_default();
659 root_inode=new_inode(sb);
660 if (!root_inode)
661 goto out_unload_nls;
662 root_inode->i_ino = MSDOS_ROOT_INO;
663 fat_read_root(root_inode);
664 insert_inode_hash(root_inode);
665 sb->s_root = d_alloc_root(root_inode);
666 if (!sb->s_root)
667 goto out_no_root;
668 if(i>=0) {
669 sbi->cvf_format = cvf_formats[i];
670 ++cvf_format_use_count[i];
672 return sb;
674 out_no_root:
675 printk("get root inode failed\n");
676 iput(root_inode);
677 unload_nls(sbi->nls_io);
678 out_unload_nls:
679 unload_nls(sbi->nls_disk);
680 goto out_fail;
681 out_invalid:
682 if (!silent)
683 printk("VFS: Can't find a valid MSDOS filesystem on dev %s.\n",
684 kdevname(sb->s_dev));
685 goto out_fail;
686 out_no_bread:
687 printk("FAT bread failed\n");
688 out_fail:
689 if (opts.iocharset) {
690 printk("VFS: freeing iocharset=%s\n", opts.iocharset);
691 kfree(opts.iocharset);
693 if(sbi->private_data)
694 kfree(sbi->private_data);
695 sbi->private_data=NULL;
697 return NULL;
700 int fat_statfs(struct super_block *sb,struct statfs *buf)
702 int free,nr;
704 if (MSDOS_SB(sb)->cvf_format &&
705 MSDOS_SB(sb)->cvf_format->cvf_statfs)
706 return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf,
707 sizeof(struct statfs));
709 lock_fat(sb);
710 if (MSDOS_SB(sb)->free_clusters != -1)
711 free = MSDOS_SB(sb)->free_clusters;
712 else {
713 free = 0;
714 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
715 if (!fat_access(sb,nr,-1)) free++;
716 MSDOS_SB(sb)->free_clusters = free;
718 unlock_fat(sb);
719 buf->f_type = sb->s_magic;
720 buf->f_bsize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
721 buf->f_blocks = MSDOS_SB(sb)->clusters;
722 buf->f_bfree = free;
723 buf->f_bavail = free;
724 buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
725 return 0;
728 static int is_exec(char *extension)
730 char *exe_extensions = "EXECOMBAT", *walk;
732 for (walk = exe_extensions; *walk; walk += 3)
733 if (!strncmp(extension, walk, 3))
734 return 1;
735 return 0;
738 static int fat_writepage(struct page *page)
740 return block_write_full_page(page,fat_get_block);
742 static int fat_readpage(struct file *file, struct page *page)
744 return block_read_full_page(page,fat_get_block);
746 static int fat_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
748 return cont_prepare_write(page,from,to,fat_get_block,
749 &MSDOS_I(page->mapping->host)->mmu_private);
751 static int _fat_bmap(struct address_space *mapping, long block)
753 return generic_block_bmap(mapping,block,fat_get_block);
755 static struct address_space_operations fat_aops = {
756 readpage: fat_readpage,
757 writepage: fat_writepage,
758 sync_page: block_sync_page,
759 prepare_write: fat_prepare_write,
760 commit_write: generic_commit_write,
761 bmap: _fat_bmap
764 /* doesn't deal with root inode */
765 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
767 struct super_block *sb = inode->i_sb;
768 struct msdos_sb_info *sbi = MSDOS_SB(sb);
769 int nr;
771 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
772 MSDOS_I(inode)->i_location = 0;
773 MSDOS_I(inode)->i_fat_inode = inode;
774 inode->i_uid = sbi->options.fs_uid;
775 inode->i_gid = sbi->options.fs_gid;
776 inode->i_version = ++event;
777 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
778 inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO &
779 ~sbi->options.fs_umask) | S_IFDIR;
780 inode->i_op = sbi->dir_ops;
781 inode->i_fop = &fat_dir_operations;
783 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
784 if (sbi->fat_bits == 32) {
785 MSDOS_I(inode)->i_start |=
786 (CF_LE_W(de->starthi) << 16);
788 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
789 inode->i_nlink = fat_subdirs(inode);
790 /* includes .., compensating for "self" */
791 #ifdef DEBUG
792 if (!inode->i_nlink) {
793 printk("directory %d: i_nlink == 0\n",inode->i_ino);
794 inode->i_nlink = 1;
796 #endif
797 if ((nr = MSDOS_I(inode)->i_start) != 0)
798 while (nr != -1) {
799 inode->i_size += SECTOR_SIZE*sbi->cluster_size;
800 if (!(nr = fat_access(sb,nr,-1))) {
801 printk("Directory %ld: bad FAT\n",
802 inode->i_ino);
803 break;
806 MSDOS_I(inode)->mmu_private = inode->i_size;
807 } else { /* not a directory */
808 inode->i_mode = MSDOS_MKMODE(de->attr,
809 ((IS_NOEXEC(inode) ||
810 (sbi->options.showexec &&
811 !is_exec(de->ext)))
812 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
813 & ~sbi->options.fs_umask) | S_IFREG;
814 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
815 if (sbi->fat_bits == 32) {
816 MSDOS_I(inode)->i_start |=
817 (CF_LE_W(de->starthi) << 16);
819 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
820 inode->i_size = CF_LE_L(de->size);
821 inode->i_op = &fat_file_inode_operations;
822 inode->i_fop = &fat_file_operations;
823 inode->i_mapping->a_ops = &fat_aops;
824 MSDOS_I(inode)->mmu_private = inode->i_size;
826 if(de->attr & ATTR_SYS)
827 if (sbi->options.sys_immutable)
828 inode->i_flags |= S_IMMUTABLE;
829 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
830 /* this is as close to the truth as we can get ... */
831 inode->i_blksize = sbi->cluster_size*SECTOR_SIZE;
832 inode->i_blocks =
833 ((inode->i_size+inode->i_blksize-1)>>sbi->cluster_bits) *
834 sbi->cluster_size;
835 inode->i_mtime = inode->i_atime =
836 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
837 inode->i_ctime =
838 MSDOS_SB(sb)->options.isvfat
839 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
840 : inode->i_mtime;
841 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
844 void fat_write_inode(struct inode *inode, int wait)
846 struct super_block *sb = inode->i_sb;
847 struct buffer_head *bh;
848 struct msdos_dir_entry *raw_entry;
849 int i_pos;
851 retry:
852 i_pos = MSDOS_I(inode)->i_location;
853 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
854 return;
856 lock_kernel();
857 if (!(bh = fat_bread(sb, i_pos >> MSDOS_DPB_BITS))) {
858 printk("dev = %s, ino = %d\n", kdevname(inode->i_dev), i_pos);
859 fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block");
860 unlock_kernel();
861 return;
863 spin_lock(&fat_inode_lock);
864 if (i_pos != MSDOS_I(inode)->i_location) {
865 spin_unlock(&fat_inode_lock);
866 fat_brelse(sb, bh);
867 unlock_kernel();
868 goto retry;
871 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
872 [i_pos & (MSDOS_DPB-1)];
873 if (S_ISDIR(inode->i_mode)) {
874 raw_entry->attr = ATTR_DIR;
875 raw_entry->size = 0;
877 else {
878 raw_entry->attr = ATTR_NONE;
879 raw_entry->size = CT_LE_L(inode->i_size);
881 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
882 MSDOS_I(inode)->i_attrs;
883 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
884 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
885 fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
886 raw_entry->time = CT_LE_W(raw_entry->time);
887 raw_entry->date = CT_LE_W(raw_entry->date);
888 if (MSDOS_SB(sb)->options.isvfat) {
889 fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
890 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms;
891 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
892 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
894 spin_unlock(&fat_inode_lock);
895 fat_mark_buffer_dirty(sb, bh);
896 fat_brelse(sb, bh);
897 unlock_kernel();
901 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
903 struct super_block *sb = dentry->d_sb;
904 struct inode *inode = dentry->d_inode;
905 int error;
907 /* FAT cannot truncate to a longer file */
908 if (attr->ia_valid & ATTR_SIZE) {
909 if (attr->ia_size > inode->i_size)
910 return -EPERM;
913 error = inode_change_ok(inode, attr);
914 if (error)
915 return MSDOS_SB(sb)->options.quiet ? 0 : error;
917 if (((attr->ia_valid & ATTR_UID) &&
918 (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
919 ((attr->ia_valid & ATTR_GID) &&
920 (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
921 ((attr->ia_valid & ATTR_MODE) &&
922 (attr->ia_mode & ~MSDOS_VALID_MODE)))
923 error = -EPERM;
925 if (error)
926 return MSDOS_SB(sb)->options.quiet ? 0 : error;
928 inode_setattr(inode, attr);
930 if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
931 inode->i_mode &= S_IFMT | S_IRUGO | S_IWUGO;
932 else
933 inode->i_mode |= S_IXUGO;
935 inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
936 & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
937 ~MSDOS_SB(sb)->options.fs_umask;
938 return 0;