Import 2.3.26pre2
[davej-history.git] / fs / fat / inode.c
blob3951eb48b766fdd70b0cfb257764378862ce756f
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;
198 if (MSDOS_SB(sb)->put_super_callback)
199 MSDOS_SB(sb)->put_super_callback(sb);
200 MOD_DEC_USE_COUNT;
201 return;
205 static int parse_options(char *options,int *fat, int *blksize, int *debug,
206 struct fat_mount_options *opts,
207 char *cvf_format, char *cvf_options)
209 char *this_char,*value,save,*savep;
210 char *p;
211 int ret = 1, len;
213 opts->name_check = 'n';
214 opts->conversion = 'b';
215 opts->fs_uid = current->uid;
216 opts->fs_gid = current->gid;
217 opts->fs_umask = current->fs->umask;
218 opts->quiet = opts->sys_immutable = opts->dotsOK = opts->showexec = 0;
219 opts->codepage = 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,"nodots")) {
261 opts->dotsOK = 0;
263 else if (!strcmp(this_char,"showexec")) {
264 opts->showexec = 1;
266 else if (!strcmp(this_char,"dotsOK") && value) {
267 if (!strcmp(value,"yes")) opts->dotsOK = 1;
268 else if (!strcmp(value,"no")) opts->dotsOK = 0;
269 else ret = 0;
271 else if (!strcmp(this_char,"uid")) {
272 if (!value || !*value) ret = 0;
273 else {
274 opts->fs_uid = simple_strtoul(value,&value,0);
275 if (*value) ret = 0;
278 else if (!strcmp(this_char,"gid")) {
279 if (!value || !*value) ret= 0;
280 else {
281 opts->fs_gid = simple_strtoul(value,&value,0);
282 if (*value) ret = 0;
285 else if (!strcmp(this_char,"umask")) {
286 if (!value || !*value) ret = 0;
287 else {
288 opts->fs_umask = simple_strtoul(value,&value,8);
289 if (*value) ret = 0;
292 else if (!strcmp(this_char,"debug")) {
293 if (value) ret = 0;
294 else *debug = 1;
296 else if (!strcmp(this_char,"fat")) {
297 if (!value || !*value) ret = 0;
298 else {
299 *fat = simple_strtoul(value,&value,0);
300 if (*value || (*fat != 12 && *fat != 16 &&
301 *fat != 32))
302 ret = 0;
305 else if (!strcmp(this_char,"quiet")) {
306 if (value) ret = 0;
307 else opts->quiet = 1;
309 else if (!strcmp(this_char,"blocksize")) {
310 if (!value || !*value) ret = 0;
311 else {
312 *blksize = simple_strtoul(value,&value,0);
313 if (*value || (*blksize != 512 &&
314 *blksize != 1024 && *blksize != 2048))
315 ret = 0;
318 else if (!strcmp(this_char,"sys_immutable")) {
319 if (value) ret = 0;
320 else opts->sys_immutable = 1;
322 else if (!strcmp(this_char,"codepage") && value) {
323 opts->codepage = simple_strtoul(value,&value,0);
324 if (*value) ret = 0;
325 else printk ("MSDOS FS: Using codepage %d\n",
326 opts->codepage);
328 else if (!strcmp(this_char,"iocharset") && value) {
329 p = value;
330 while (*value && *value != ',') value++;
331 len = value - p;
332 if (len) {
333 char * buffer = kmalloc(len+1, GFP_KERNEL);
334 if (buffer) {
335 opts->iocharset = buffer;
336 memcpy(buffer, p, len);
337 buffer[len] = 0;
338 printk("MSDOS FS: IO charset %s\n",
339 buffer);
340 } else
341 ret = 0;
344 else if (!strcmp(this_char,"cvf_format")) {
345 if (!value)
346 return 0;
347 strncpy(cvf_format,value,20);
349 else if (!strcmp(this_char,"cvf_options")) {
350 if (!value)
351 return 0;
352 strncpy(cvf_options,value,100);
355 if (this_char != options) *(this_char-1) = ',';
356 if (value) *savep = save;
357 if (ret == 0)
358 break;
360 out:
361 return ret;
364 static void fat_read_root(struct inode *inode)
366 struct super_block *sb = inode->i_sb;
367 int nr;
369 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
370 MSDOS_I(inode)->i_location = 0;
371 MSDOS_I(inode)->i_fat_inode = inode;
372 inode->i_uid = MSDOS_SB(sb)->options.fs_uid;
373 inode->i_gid = MSDOS_SB(sb)->options.fs_gid;
374 inode->i_version = ++event;
375 inode->i_mode = (S_IRWXUGO & ~MSDOS_SB(sb)->options.fs_umask) | S_IFDIR;
376 inode->i_op = MSDOS_SB(sb)->dir_ops;
377 if (MSDOS_SB(sb)->fat_bits == 32) {
378 MSDOS_I(inode)->i_start = MSDOS_SB(sb)->root_cluster;
379 if ((nr = MSDOS_I(inode)->i_start) != 0) {
380 while (nr != -1) {
381 inode->i_size += SECTOR_SIZE*MSDOS_SB(sb)->cluster_size;
382 if (!(nr = fat_access(sb,nr,-1))) {
383 printk("Directory %ld: bad FAT\n",
384 inode->i_ino);
385 break;
389 } else {
390 MSDOS_I(inode)->i_start = 0;
391 inode->i_size = MSDOS_SB(sb)->dir_entries*
392 sizeof(struct msdos_dir_entry);
394 inode->i_blksize = MSDOS_SB(sb)->cluster_size* SECTOR_SIZE;
395 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
396 inode->i_blksize*MSDOS_SB(sb)->cluster_size;
397 MSDOS_I(inode)->i_logstart = 0;
398 MSDOS_I(inode)->i_realsize = inode->i_size;
400 MSDOS_I(inode)->i_attrs = 0;
401 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
402 MSDOS_I(inode)->i_ctime_ms = 0;
403 inode->i_nlink = fat_subdirs(inode)+2;
406 static struct super_operations fat_sops = {
407 NULL,
408 fat_write_inode,
409 NULL,
410 fat_delete_inode,
411 fat_notify_change,
412 fat_put_super,
413 NULL, /* write_super */
414 fat_statfs,
415 NULL, /* remount */
416 fat_clear_inode
420 * Read the super block of an MS-DOS FS.
422 * Note that this may be called from vfat_read_super
423 * with some fields already initialized.
425 struct super_block *
426 fat_read_super(struct super_block *sb, void *data, int silent,
427 struct inode_operations *fs_dir_inode_ops)
429 struct inode *root_inode;
430 struct buffer_head *bh;
431 struct fat_boot_sector *b;
432 char *p;
433 int data_sectors,logical_sector_size,sector_mult,fat_clusters=0;
434 int debug,error,fat,cp;
435 int blksize = 512;
436 int fat32;
437 struct fat_mount_options opts;
438 char buf[50];
439 int i;
440 char cvf_format[21];
441 char cvf_options[101];
443 cvf_format[0] = '\0';
444 cvf_options[0] = '\0';
445 MSDOS_SB(sb)->cvf_format = NULL;
446 MSDOS_SB(sb)->private_data = NULL;
448 MOD_INC_USE_COUNT;
449 MSDOS_SB(sb)->dir_ops = fs_dir_inode_ops;
450 MSDOS_SB(sb)->put_super_callback = NULL;
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 = MSDOS_SB(sb)->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(&(MSDOS_SB(sb)->options), &opts, sizeof(struct fat_mount_options));
468 fat_cache_init();
469 lock_super(sb);
470 if( blksize > 1024 )
472 /* Force the superblock to a larger size here. */
473 sb->s_blocksize = blksize;
474 set_blocksize(sb->s_dev, blksize);
476 else
478 /* The first read is always 1024 bytes */
479 sb->s_blocksize = 1024;
480 set_blocksize(sb->s_dev, 1024);
482 bh = bread(sb->s_dev, 0, sb->s_blocksize);
483 unlock_super(sb);
484 if (bh == NULL || !buffer_uptodate(bh)) {
485 brelse (bh);
486 goto out_no_bread;
490 * The DOS3 partition size limit is *not* 32M as many people think.
491 * Instead, it is 64K sectors (with the usual sector size being
492 * 512 bytes, leading to a 32M limit).
494 * DOS 3 partition managers got around this problem by faking a
495 * larger sector size, ie treating multiple physical sectors as
496 * a single logical sector.
498 * We can accommodate this scheme by adjusting our cluster size,
499 * fat_start, and data_start by an appropriate value.
501 * (by Drew Eckhardt)
504 #define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
505 /* don't divide by zero */
507 b = (struct fat_boot_sector *) bh->b_data;
508 logical_sector_size =
509 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
510 sector_mult = logical_sector_size >> SECTOR_BITS;
511 MSDOS_SB(sb)->cluster_size = b->cluster_size*sector_mult;
512 MSDOS_SB(sb)->fats = b->fats;
513 MSDOS_SB(sb)->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 MSDOS_SB(sb)->fat_length= CF_LE_L(b->fat32_length)*sector_mult;
520 MSDOS_SB(sb)->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 MSDOS_SB(sb)->fsinfo_offset =
525 logical_sector_size + 0x1e0;
526 } else {
527 MSDOS_SB(sb)->fsinfo_offset =
528 (CF_LE_W(b->info_sector) * logical_sector_size)
529 + 0x1e0;
531 if (MSDOS_SB(sb)->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[MSDOS_SB(sb)->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 MSDOS_SB(sb)->free_clusters = CF_LE_L(fsinfo->free_clusters);
545 } else {
546 fat32 = 0;
547 MSDOS_SB(sb)->fat_length = CF_LE_W(b->fat_length)*sector_mult;
548 MSDOS_SB(sb)->root_cluster = 0;
549 MSDOS_SB(sb)->free_clusters = -1; /* Don't know yet */
551 MSDOS_SB(sb)->dir_start= CF_LE_W(b->reserved)*sector_mult+
552 b->fats*MSDOS_SB(sb)->fat_length;
553 MSDOS_SB(sb)->dir_entries =
554 CF_LE_W(get_unaligned((unsigned short *) &b->dir_entries));
555 MSDOS_SB(sb)->data_start = MSDOS_SB(sb)->dir_start+ROUND_TO_MULTIPLE((
556 MSDOS_SB(sb)->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 - MSDOS_SB(sb)->data_start;
563 error = !b->cluster_size || !sector_mult;
564 if (!error) {
565 MSDOS_SB(sb)->clusters = b->cluster_size ? data_sectors/
566 b->cluster_size/sector_mult : 0;
567 MSDOS_SB(sb)->fat_bits = fat32 ? 32 :
568 (fat ? fat :
569 (MSDOS_SB(sb)->clusters > MSDOS_FAT12 ? 16 : 12));
570 fat_clusters = MSDOS_SB(sb)->fat_length*SECTOR_SIZE*8/
571 MSDOS_SB(sb)->fat_bits;
572 error = !MSDOS_SB(sb)->fats || (MSDOS_SB(sb)->dir_entries &
573 (MSDOS_DPS-1)) || MSDOS_SB(sb)->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 MSDOS_SB(sb)->cvf_format = &default_cvf;
596 else
597 MSDOS_SB(sb)->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 MSDOS_SB(sb)->fat_bits,opts.name_check,
603 opts.conversion,opts.fs_uid,opts.fs_gid,opts.fs_umask,
604 MSDOS_CAN_BMAP(MSDOS_SB(sb)) ? ",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,MSDOS_SB(sb)->cluster_size,
608 MSDOS_SB(sb)->fats,MSDOS_SB(sb)->fat_start,
609 MSDOS_SB(sb)->fat_length,
610 MSDOS_SB(sb)->dir_start,MSDOS_SB(sb)->dir_entries,
611 MSDOS_SB(sb)->data_start,
612 CF_LE_W(*(unsigned short *) &b->sectors),
613 (unsigned long)b->total_sect,logical_sector_size,
614 MSDOS_SB(sb)->root_cluster,MSDOS_SB(sb)->free_clusters);
615 printk ("Transaction block size = %d\n",blksize);
617 if (i<0) if (MSDOS_SB(sb)->clusters+2 > fat_clusters)
618 MSDOS_SB(sb)->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(&MSDOS_SB(sb)->fat_wait);
625 init_MUTEX(&MSDOS_SB(sb)->fat_lock);
626 MSDOS_SB(sb)->prev_free = 0;
628 cp = opts.codepage ? opts.codepage : 437;
629 sprintf(buf, "cp%d", cp);
630 MSDOS_SB(sb)->nls_disk = load_nls(buf);
631 if (! MSDOS_SB(sb)->nls_disk) {
632 /* Fail only if explicit charset specified */
633 if (opts.codepage != 0)
634 goto out_fail;
635 MSDOS_SB(sb)->options.codepage = 0; /* already 0?? */
636 MSDOS_SB(sb)->nls_disk = load_nls_default();
639 MSDOS_SB(sb)->nls_io = NULL;
640 if (MSDOS_SB(sb)->options.isvfat && !opts.utf8) {
641 p = opts.iocharset ? opts.iocharset : "iso8859-1";
642 MSDOS_SB(sb)->nls_io = load_nls(p);
643 if (! MSDOS_SB(sb)->nls_io) {
644 /* Fail only if explicit charset specified */
645 if (opts.iocharset)
646 goto out_unload_nls;
647 MSDOS_SB(sb)->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 MSDOS_SB(sb)->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 if (MSDOS_SB(sb)->nls_io)
672 unload_nls(MSDOS_SB(sb)->nls_io);
673 out_unload_nls:
674 unload_nls(MSDOS_SB(sb)->nls_disk);
675 goto out_fail;
676 out_invalid:
677 if (!silent)
678 printk("VFS: Can't find a valid MSDOS filesystem on dev %s.\n",
679 kdevname(sb->s_dev));
680 goto out_fail;
681 out_no_bread:
682 printk("FAT bread failed\n");
683 out_fail:
684 if (opts.iocharset) {
685 printk("VFS: freeing iocharset=%s\n", opts.iocharset);
686 kfree(opts.iocharset);
688 sb->s_dev = 0;
689 if(MSDOS_SB(sb)->private_data)kfree(MSDOS_SB(sb)->private_data);
690 MSDOS_SB(sb)->private_data=NULL;
692 MOD_DEC_USE_COUNT;
693 return NULL;
696 int fat_statfs(struct super_block *sb,struct statfs *buf, int bufsiz)
698 int free,nr;
699 struct statfs tmp;
701 if (MSDOS_SB(sb)->cvf_format &&
702 MSDOS_SB(sb)->cvf_format->cvf_statfs)
703 return MSDOS_SB(sb)->cvf_format->cvf_statfs(sb,buf,bufsiz);
705 lock_fat(sb);
706 if (MSDOS_SB(sb)->free_clusters != -1)
707 free = MSDOS_SB(sb)->free_clusters;
708 else {
709 free = 0;
710 for (nr = 2; nr < MSDOS_SB(sb)->clusters+2; nr++)
711 if (!fat_access(sb,nr,-1)) free++;
712 MSDOS_SB(sb)->free_clusters = free;
714 unlock_fat(sb);
715 tmp.f_type = sb->s_magic;
716 tmp.f_bsize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
717 tmp.f_blocks = MSDOS_SB(sb)->clusters;
718 tmp.f_bfree = free;
719 tmp.f_bavail = free;
720 tmp.f_files = 0;
721 tmp.f_ffree = 0;
722 tmp.f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
723 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
726 static int is_exec(char *extension)
728 char *exe_extensions = "EXECOMBAT", *walk;
730 for (walk = exe_extensions; *walk; walk += 3)
731 if (!strncmp(extension, walk, 3))
732 return 1;
733 return 0;
736 /* doesn't deal with root inode */
737 static void fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
739 struct super_block *sb = inode->i_sb;
740 int nr;
742 INIT_LIST_HEAD(&MSDOS_I(inode)->i_fat_hash);
743 MSDOS_I(inode)->i_location = 0;
744 MSDOS_I(inode)->i_fat_inode = inode;
745 inode->i_uid = MSDOS_SB(sb)->options.fs_uid;
746 inode->i_gid = MSDOS_SB(sb)->options.fs_gid;
747 inode->i_version = ++event;
748 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
749 inode->i_mode = MSDOS_MKMODE(de->attr,S_IRWXUGO &
750 ~MSDOS_SB(sb)->options.fs_umask) | S_IFDIR;
751 inode->i_op = MSDOS_SB(inode->i_sb)->dir_ops;
753 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
754 if (MSDOS_SB(sb)->fat_bits == 32) {
755 MSDOS_I(inode)->i_start |=
756 (CF_LE_W(de->starthi) << 16);
758 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
759 inode->i_nlink = fat_subdirs(inode);
760 /* includes .., compensating for "self" */
761 #ifdef DEBUG
762 if (!inode->i_nlink) {
763 printk("directory %d: i_nlink == 0\n",inode->i_ino);
764 inode->i_nlink = 1;
766 #endif
767 inode->i_size = 0;
768 if ((nr = MSDOS_I(inode)->i_start) != 0)
769 while (nr != -1) {
770 inode->i_size += SECTOR_SIZE*MSDOS_SB(inode->
771 i_sb)->cluster_size;
772 if (!(nr = fat_access(sb,nr,-1))) {
773 printk("Directory %ld: bad FAT\n",
774 inode->i_ino);
775 break;
778 MSDOS_I(inode)->i_realsize = inode->i_size;
779 } else { /* not a directory */
780 inode->i_mode = MSDOS_MKMODE(de->attr,
781 ((IS_NOEXEC(inode) ||
782 (MSDOS_SB(sb)->options.showexec &&
783 !is_exec(de->ext)))
784 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
785 & ~MSDOS_SB(sb)->options.fs_umask) | S_IFREG;
786 inode->i_op = &fat_file_inode_operations;
787 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
788 if (MSDOS_SB(sb)->fat_bits == 32) {
789 MSDOS_I(inode)->i_start |=
790 (CF_LE_W(de->starthi) << 16);
792 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
793 inode->i_nlink = 1;
794 inode->i_size = CF_LE_L(de->size);
795 MSDOS_I(inode)->i_realsize = ((inode->i_size-1)|(SECTOR_SIZE-1))+1;
797 if(de->attr & ATTR_SYS)
798 if (MSDOS_SB(sb)->options.sys_immutable)
799 inode->i_flags |= S_IMMUTABLE;
800 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
801 /* this is as close to the truth as we can get ... */
802 inode->i_blksize = MSDOS_SB(sb)->cluster_size*SECTOR_SIZE;
803 inode->i_blocks = (inode->i_size+inode->i_blksize-1)/
804 inode->i_blksize*MSDOS_SB(sb)->cluster_size;
805 inode->i_mtime = inode->i_atime =
806 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
807 inode->i_ctime =
808 MSDOS_SB(sb)->options.isvfat
809 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
810 : inode->i_mtime;
811 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
814 void fat_write_inode(struct inode *inode)
816 struct super_block *sb = inode->i_sb;
817 struct buffer_head *bh;
818 struct msdos_dir_entry *raw_entry;
819 int i_pos;
821 retry:
822 i_pos = MSDOS_I(inode)->i_location;
823 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) return;
824 if (!(bh = fat_bread(sb, i_pos >> MSDOS_DPB_BITS))) {
825 printk("dev = %s, ino = %d\n", kdevname(inode->i_dev), i_pos);
826 fat_fs_panic(sb, "msdos_write_inode: unable to read i-node block");
827 return;
829 spin_lock(&fat_inode_lock);
830 if (i_pos != MSDOS_I(inode)->i_location) {
831 spin_unlock(&fat_inode_lock);
832 fat_brelse(sb, bh);
833 goto retry;
836 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
837 [i_pos & (MSDOS_DPB-1)];
838 if (S_ISDIR(inode->i_mode)) {
839 raw_entry->attr = ATTR_DIR;
840 raw_entry->size = 0;
842 else {
843 raw_entry->attr = ATTR_NONE;
844 raw_entry->size = CT_LE_L(inode->i_size);
846 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
847 MSDOS_I(inode)->i_attrs;
848 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
849 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
850 fat_date_unix2dos(inode->i_mtime,&raw_entry->time,&raw_entry->date);
851 raw_entry->time = CT_LE_W(raw_entry->time);
852 raw_entry->date = CT_LE_W(raw_entry->date);
853 if (MSDOS_SB(sb)->options.isvfat) {
854 fat_date_unix2dos(inode->i_ctime,&raw_entry->ctime,&raw_entry->cdate);
855 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms;
856 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
857 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
859 spin_unlock(&fat_inode_lock);
860 fat_mark_buffer_dirty(sb, bh, 1);
861 fat_brelse(sb, bh);
865 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
867 struct super_block *sb = dentry->d_sb;
868 struct inode *inode = dentry->d_inode;
869 int error;
871 error = inode_change_ok(inode, attr);
872 if (error)
873 return MSDOS_SB(sb)->options.quiet ? 0 : error;
875 if (((attr->ia_valid & ATTR_UID) &&
876 (attr->ia_uid != MSDOS_SB(sb)->options.fs_uid)) ||
877 ((attr->ia_valid & ATTR_GID) &&
878 (attr->ia_gid != MSDOS_SB(sb)->options.fs_gid)) ||
879 ((attr->ia_valid & ATTR_MODE) &&
880 (attr->ia_mode & ~MSDOS_VALID_MODE)))
881 error = -EPERM;
883 if (error)
884 return MSDOS_SB(sb)->options.quiet ? 0 : error;
886 inode_setattr(inode, attr);
888 if (IS_NOEXEC(inode) && !S_ISDIR(inode->i_mode))
889 inode->i_mode &= S_IFMT | S_IRUGO | S_IWUGO;
890 else
891 inode->i_mode |= S_IXUGO;
893 inode->i_mode = ((inode->i_mode & S_IFMT) | ((((inode->i_mode & S_IRWXU
894 & ~MSDOS_SB(sb)->options.fs_umask) | S_IRUSR) >> 6)*S_IXUGO)) &
895 ~MSDOS_SB(sb)->options.fs_umask;
896 return 0;
900 #ifdef MODULE
901 int init_module(void)
903 return init_fat_fs();
907 void cleanup_module(void)
909 /* Nothing to be done, really! */
910 return;
912 #endif