[PATCH] fix make rpm versioning
[linux-2.6/history.git] / fs / fat / inode.c
blob55df25851655a6920b56fc79c3e34235bdaeced8
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/module.h>
14 #include <linux/time.h>
15 #include <linux/slab.h>
16 #include <linux/smp_lock.h>
17 #include <linux/seq_file.h>
18 #include <linux/msdos_fs.h>
19 #include <linux/pagemap.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <asm/unaligned.h>
26 * New FAT inode stuff. We do the following:
27 * a) i_ino is constant and has nothing with on-disk location.
28 * b) FAT manages its own cache of directory entries.
29 * c) *This* cache is indexed by on-disk location.
30 * d) inode has an associated directory entry, all right, but
31 * it may be unhashed.
32 * e) currently entries are stored within struct inode. That should
33 * change.
34 * f) we deal with races in the following way:
35 * 1. readdir() and lookup() do FAT-dir-cache lookup.
36 * 2. rename() unhashes the F-d-c entry and rehashes it in
37 * a new place.
38 * 3. unlink() and rmdir() unhash F-d-c entry.
39 * 4. fat_write_inode() checks whether the thing is unhashed.
40 * If it is we silently return. If it isn't we do bread(),
41 * check if the location is still valid and retry if it
42 * isn't. Otherwise we do changes.
43 * 5. Spinlock is used to protect hash/unhash/location check/lookup
44 * 6. fat_clear_inode() unhashes the F-d-c entry.
45 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
46 * and consider negative result as cache miss.
49 #define FAT_HASH_BITS 8
50 #define FAT_HASH_SIZE (1UL << FAT_HASH_BITS)
51 #define FAT_HASH_MASK (FAT_HASH_SIZE-1)
52 static struct list_head fat_inode_hashtable[FAT_HASH_SIZE];
53 spinlock_t fat_inode_lock = SPIN_LOCK_UNLOCKED;
55 void fat_hash_init(void)
57 int i;
58 for(i = 0; i < FAT_HASH_SIZE; i++) {
59 INIT_LIST_HEAD(&fat_inode_hashtable[i]);
63 static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
65 unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
66 tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
67 return tmp & FAT_HASH_MASK;
70 void fat_attach(struct inode *inode, loff_t i_pos)
72 spin_lock(&fat_inode_lock);
73 MSDOS_I(inode)->i_pos = i_pos;
74 list_add(&MSDOS_I(inode)->i_fat_hash,
75 fat_inode_hashtable + fat_hash(inode->i_sb, i_pos));
76 spin_unlock(&fat_inode_lock);
79 void fat_detach(struct inode *inode)
81 spin_lock(&fat_inode_lock);
82 MSDOS_I(inode)->i_pos = 0;
83 list_del_init(&MSDOS_I(inode)->i_fat_hash);
84 spin_unlock(&fat_inode_lock);
87 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
89 struct list_head *p = fat_inode_hashtable + fat_hash(sb, i_pos);
90 struct list_head *walk;
91 struct msdos_inode_info *i;
92 struct inode *inode = NULL;
94 spin_lock(&fat_inode_lock);
95 list_for_each(walk, p) {
96 i = list_entry(walk, struct msdos_inode_info, i_fat_hash);
97 if (i->vfs_inode.i_sb != sb)
98 continue;
99 if (i->i_pos != i_pos)
100 continue;
101 inode = igrab(&i->vfs_inode);
102 if (inode)
103 break;
105 spin_unlock(&fat_inode_lock);
106 return inode;
109 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de);
111 struct inode *fat_build_inode(struct super_block *sb,
112 struct msdos_dir_entry *de, loff_t i_pos, int *res)
114 struct inode *inode;
115 *res = 0;
116 inode = fat_iget(sb, i_pos);
117 if (inode)
118 goto out;
119 inode = new_inode(sb);
120 *res = -ENOMEM;
121 if (!inode)
122 goto out;
123 inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
124 inode->i_version = 1;
125 *res = fat_fill_inode(inode, de);
126 if (*res < 0) {
127 iput(inode);
128 inode = NULL;
129 goto out;
131 fat_attach(inode, i_pos);
132 insert_inode_hash(inode);
133 out:
134 return inode;
137 void fat_delete_inode(struct inode *inode)
139 if (!is_bad_inode(inode)) {
140 inode->i_size = 0;
141 fat_truncate(inode);
143 clear_inode(inode);
146 void fat_clear_inode(struct inode *inode)
148 if (is_bad_inode(inode))
149 return;
150 lock_kernel();
151 spin_lock(&fat_inode_lock);
152 fat_cache_inval_inode(inode);
153 list_del_init(&MSDOS_I(inode)->i_fat_hash);
154 spin_unlock(&fat_inode_lock);
155 unlock_kernel();
158 void fat_put_super(struct super_block *sb)
160 struct msdos_sb_info *sbi = MSDOS_SB(sb);
162 fat_clusters_flush(sb);
163 if (sbi->nls_disk) {
164 unload_nls(sbi->nls_disk);
165 sbi->nls_disk = NULL;
166 sbi->options.codepage = 0;
168 if (sbi->nls_io) {
169 unload_nls(sbi->nls_io);
170 sbi->nls_io = NULL;
173 * Note: the iocharset option might have been specified
174 * without enabling nls_io, so check for it here.
176 if (sbi->options.iocharset) {
177 kfree(sbi->options.iocharset);
178 sbi->options.iocharset = NULL;
180 sb->s_fs_info = NULL;
181 kfree(sbi);
184 static int simple_getbool(char *s, int *setval)
186 if (s) {
187 if (!strcmp(s,"1") || !strcmp(s,"yes") || !strcmp(s,"true"))
188 *setval = 1;
189 else if (!strcmp(s,"0") || !strcmp(s,"no") || !strcmp(s,"false"))
190 *setval = 0;
191 else
192 return 0;
193 } else
194 *setval = 1;
195 return 1;
198 static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
200 struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
201 struct fat_mount_options *opts = &sbi->options;
202 int isvfat = opts->isvfat;
204 if (opts->fs_uid != 0)
205 seq_printf(m, ",uid=%d", opts->fs_uid);
206 if (opts->fs_gid != 0)
207 seq_printf(m, ",gid=%d", opts->fs_gid);
208 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
209 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
210 if (sbi->nls_disk)
211 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
212 if (isvfat) {
213 if (sbi->nls_io
214 && strcmp(sbi->nls_io->charset, CONFIG_NLS_DEFAULT))
215 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
217 switch (opts->shortname) {
218 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
219 seq_puts(m, ",shortname=win95");
220 break;
221 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
222 seq_puts(m, ",shortname=winnt");
223 break;
224 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
225 seq_puts(m, ",shortname=mixed");
226 break;
227 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
228 /* seq_puts(m, ",shortname=lower"); */
229 break;
230 default:
231 seq_puts(m, ",shortname=unknown");
232 break;
235 if (opts->name_check != 'n')
236 seq_printf(m, ",check=%c", opts->name_check);
237 if (opts->quiet)
238 seq_puts(m, ",quiet");
239 if (opts->showexec)
240 seq_puts(m, ",showexec");
241 if (opts->sys_immutable)
242 seq_puts(m, ",sys_immutable");
243 if (!isvfat) {
244 if (opts->dotsOK)
245 seq_puts(m, ",dotsOK=yes");
246 if (opts->nocase)
247 seq_puts(m, ",nocase");
248 } else {
249 if (opts->utf8)
250 seq_puts(m, ",utf8");
251 if (opts->unicode_xlate)
252 seq_puts(m, ",uni_xlate");
253 if (!opts->numtail)
254 seq_puts(m, ",nonumtail");
257 return 0;
260 static int parse_options(char *options, int is_vfat, int *debug,
261 struct fat_mount_options *opts)
263 char *this_char, *value, *p;
264 int ret = 1, val, len;
266 opts->isvfat = is_vfat;
268 opts->fs_uid = current->uid;
269 opts->fs_gid = current->gid;
270 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
271 opts->codepage = 0;
272 opts->iocharset = NULL;
273 if (is_vfat)
274 opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
275 else
276 opts->shortname = 0;
277 opts->name_check = 'n';
278 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
279 opts->utf8 = opts->unicode_xlate = 0;
280 opts->numtail = 1;
281 opts->nocase = 0;
282 *debug = 0;
284 if (!options)
285 goto out;
286 while ((this_char = strsep(&options,",")) != NULL) {
287 if (!*this_char)
288 continue;
289 if ((value = strchr(this_char,'=')) != NULL)
290 *value++ = 0;
292 if (!strcmp(this_char,"check") && value) {
293 if (value[0] && !value[1] && strchr("rns",*value))
294 opts->name_check = *value;
295 else if (!strcmp(value,"relaxed"))
296 opts->name_check = 'r';
297 else if (!strcmp(value,"normal"))
298 opts->name_check = 'n';
299 else if (!strcmp(value,"strict"))
300 opts->name_check = 's';
301 else ret = 0;
303 else if (!strcmp(this_char,"conv") && value) {
304 printk(KERN_INFO "FAT: conv option is obsolete, "
305 "not supported now\n");
307 else if (!strcmp(this_char,"nocase")) {
308 if (!is_vfat)
309 opts->nocase = 1;
310 else {
311 /* for backward compatible */
312 opts->shortname = VFAT_SFN_DISPLAY_WIN95
313 | VFAT_SFN_CREATE_WIN95;
316 else if (!strcmp(this_char,"showexec")) {
317 opts->showexec = 1;
319 else if (!strcmp(this_char,"uid")) {
320 if (!value || !*value) ret = 0;
321 else {
322 opts->fs_uid = simple_strtoul(value,&value,0);
323 if (*value) ret = 0;
326 else if (!strcmp(this_char,"gid")) {
327 if (!value || !*value) ret= 0;
328 else {
329 opts->fs_gid = simple_strtoul(value,&value,0);
330 if (*value) ret = 0;
333 else if (!strcmp(this_char,"umask")) {
334 if (!value || !*value) ret = 0;
335 else {
336 opts->fs_fmask = opts->fs_dmask =
337 simple_strtoul(value,&value,8);
338 if (*value) ret = 0;
341 else if (!strcmp(this_char,"fmask")) {
342 if (!value || !*value) ret = 0;
343 else {
344 opts->fs_fmask = simple_strtoul(value,&value,8);
345 if (*value) ret = 0;
348 else if (!strcmp(this_char,"dmask")) {
349 if (!value || !*value) ret = 0;
350 else {
351 opts->fs_dmask = simple_strtoul(value,&value,8);
352 if (*value) ret = 0;
355 else if (!strcmp(this_char,"debug")) {
356 if (value) ret = 0;
357 else *debug = 1;
359 else if (!strcmp(this_char,"fat")) {
360 printk(KERN_INFO "FAT: fat option is obsolete, "
361 "not supported now\n");
363 else if (!strcmp(this_char,"quiet")) {
364 if (value) ret = 0;
365 else opts->quiet = 1;
367 else if (!strcmp(this_char,"blocksize")) {
368 printk(KERN_INFO "FAT: blocksize option is obsolete, "
369 "not supported now\n");
371 else if (!strcmp(this_char,"sys_immutable")) {
372 if (value) ret = 0;
373 else opts->sys_immutable = 1;
375 else if (!strcmp(this_char,"codepage") && value) {
376 opts->codepage = simple_strtoul(value,&value,0);
377 if (*value) ret = 0;
380 /* msdos specific */
381 else if (!is_vfat && !strcmp(this_char,"dots")) {
382 opts->dotsOK = 1;
384 else if (!is_vfat && !strcmp(this_char,"nodots")) {
385 opts->dotsOK = 0;
387 else if (!is_vfat && !strcmp(this_char,"dotsOK") && value) {
388 if (!strcmp(value,"yes")) opts->dotsOK = 1;
389 else if (!strcmp(value,"no")) opts->dotsOK = 0;
390 else ret = 0;
393 /* vfat specific */
394 else if (is_vfat && !strcmp(this_char,"iocharset") && value) {
395 p = value;
396 while (*value && *value != ',')
397 value++;
398 len = value - p;
399 if (len) {
400 char *buffer;
402 if (opts->iocharset != NULL) {
403 kfree(opts->iocharset);
404 opts->iocharset = NULL;
406 buffer = kmalloc(len + 1, GFP_KERNEL);
407 if (buffer != NULL) {
408 opts->iocharset = buffer;
409 memcpy(buffer, p, len);
410 buffer[len] = 0;
411 } else
412 ret = 0;
415 else if (is_vfat && !strcmp(this_char,"utf8")) {
416 ret = simple_getbool(value, &val);
417 if (ret) opts->utf8 = val;
419 else if (is_vfat && !strcmp(this_char,"uni_xlate")) {
420 ret = simple_getbool(value, &val);
421 if (ret) opts->unicode_xlate = val;
423 else if (is_vfat && !strcmp(this_char,"posix")) {
424 printk(KERN_INFO "FAT: posix option is obsolete, "
425 "not supported now\n");
427 else if (is_vfat && !strcmp(this_char,"nonumtail")) {
428 ret = simple_getbool(value, &val);
429 if (ret) {
430 opts->numtail = !val;
433 else if (is_vfat && !strcmp(this_char, "shortname")) {
434 if (!strcmp(value, "lower"))
435 opts->shortname = VFAT_SFN_DISPLAY_LOWER
436 | VFAT_SFN_CREATE_WIN95;
437 else if (!strcmp(value, "win95"))
438 opts->shortname = VFAT_SFN_DISPLAY_WIN95
439 | VFAT_SFN_CREATE_WIN95;
440 else if (!strcmp(value, "winnt"))
441 opts->shortname = VFAT_SFN_DISPLAY_WINNT
442 | VFAT_SFN_CREATE_WINNT;
443 else if (!strcmp(value, "mixed"))
444 opts->shortname = VFAT_SFN_DISPLAY_WINNT
445 | VFAT_SFN_CREATE_WIN95;
446 else
447 ret = 0;
448 } else {
449 printk(KERN_ERR "FAT: Unrecognized mount option %s\n",
450 this_char);
451 ret = 0;
454 if (ret == 0)
455 break;
457 out:
458 if (opts->unicode_xlate)
459 opts->utf8 = 0;
461 return ret;
464 static int fat_calc_dir_size(struct inode *inode)
466 struct super_block *sb = inode->i_sb;
467 int nr;
469 inode->i_size = 0;
470 if (MSDOS_I(inode)->i_start == 0)
471 return 0;
473 nr = MSDOS_I(inode)->i_start;
474 do {
475 inode->i_size += 1 << MSDOS_SB(sb)->cluster_bits;
476 nr = fat_access(sb, nr, -1);
477 if (nr < 0)
478 return nr;
479 else if (nr == FAT_ENT_FREE) {
480 fat_fs_panic(sb, "Directory %lu: invalid cluster chain",
481 inode->i_ino);
482 return -EIO;
484 if (inode->i_size > FAT_MAX_DIR_SIZE) {
485 fat_fs_panic(sb, "Directory %lu: "
486 "exceeded the maximum size of directory",
487 inode->i_ino);
488 inode->i_size = FAT_MAX_DIR_SIZE;
489 break;
491 } while (nr != FAT_ENT_EOF);
493 return 0;
496 static int fat_read_root(struct inode *inode)
498 struct super_block *sb = inode->i_sb;
499 struct msdos_sb_info *sbi = MSDOS_SB(sb);
500 int error;
502 MSDOS_I(inode)->i_pos = 0;
503 inode->i_uid = sbi->options.fs_uid;
504 inode->i_gid = sbi->options.fs_gid;
505 inode->i_version++;
506 inode->i_generation = 0;
507 inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
508 inode->i_op = sbi->dir_ops;
509 inode->i_fop = &fat_dir_operations;
510 if (sbi->fat_bits == 32) {
511 MSDOS_I(inode)->i_start = sbi->root_cluster;
512 error = fat_calc_dir_size(inode);
513 if (error < 0)
514 return error;
515 } else {
516 MSDOS_I(inode)->i_start = 0;
517 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
519 inode->i_blksize = 1 << sbi->cluster_bits;
520 inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
521 & ~((loff_t)inode->i_blksize - 1)) >> 9;
522 MSDOS_I(inode)->i_logstart = 0;
523 MSDOS_I(inode)->mmu_private = inode->i_size;
525 MSDOS_I(inode)->i_attrs = 0;
526 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
527 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
528 MSDOS_I(inode)->i_ctime_ms = 0;
529 inode->i_nlink = fat_subdirs(inode)+2;
531 return 0;
535 * a FAT file handle with fhtype 3 is
536 * 0/ i_ino - for fast, reliable lookup if still in the cache
537 * 1/ i_generation - to see if i_ino is still valid
538 * bit 0 == 0 iff directory
539 * 2/ i_pos - if ino has changed, but still in cache (hi)
540 * 3/ i_pos - if ino has changed, but still in cache (low)
541 * 4/ i_logstart - to semi-verify inode found at i_location
542 * 5/ parent->i_logstart - maybe used to hunt for the file on disc
546 struct dentry *fat_decode_fh(struct super_block *sb, __u32 *fh,
547 int len, int fhtype,
548 int (*acceptable)(void *context, struct dentry *de),
549 void *context)
552 if (fhtype != 3)
553 return ERR_PTR(-ESTALE);
554 if (len < 6)
555 return ERR_PTR(-ESTALE);
557 return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, context);
560 struct dentry *fat_get_dentry(struct super_block *sb, void *inump)
562 struct inode *inode = NULL;
563 struct dentry *result;
564 __u32 *fh = inump;
566 inode = iget(sb, fh[0]);
567 if (!inode || is_bad_inode(inode) ||
568 inode->i_generation != fh[1]) {
569 if (inode) iput(inode);
570 inode = NULL;
572 if (!inode) {
573 loff_t i_pos = ((loff_t)fh[2] << 32) | fh[3];
575 /* try 2 - see if i_pos is in F-d-c
576 * require i_logstart to be the same
577 * Will fail if you truncate and then re-write
580 inode = fat_iget(sb, i_pos);
581 if (inode && MSDOS_I(inode)->i_logstart != fh[4]) {
582 iput(inode);
583 inode = NULL;
586 if (!inode) {
587 /* For now, do nothing
588 * What we could do is:
589 * follow the file starting at fh[4], and record
590 * the ".." entry, and the name of the fh[2] entry.
591 * The follow the ".." file finding the next step up.
592 * This way we build a path to the root of
593 * the tree. If this works, we lookup the path and so
594 * get this inode into the cache.
595 * Finally try the fat_iget lookup again
596 * If that fails, then weare totally out of luck
597 * But all that is for another day
600 if (!inode)
601 return ERR_PTR(-ESTALE);
604 /* now to find a dentry.
605 * If possible, get a well-connected one
607 result = d_alloc_anon(inode);
608 if (result == NULL) {
609 iput(inode);
610 return ERR_PTR(-ENOMEM);
612 result->d_op = sb->s_root->d_op;
613 return result;
616 int fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable)
618 int len = *lenp;
619 struct inode *inode = de->d_inode;
621 if (len < 6)
622 return 255; /* no room */
623 *lenp = 6;
624 fh[0] = inode->i_ino;
625 fh[1] = inode->i_generation;
626 fh[2] = (__u32)(MSDOS_I(inode)->i_pos >> 32);
627 fh[3] = (__u32)MSDOS_I(inode)->i_pos;
628 fh[4] = MSDOS_I(inode)->i_logstart;
629 spin_lock(&de->d_lock);
630 fh[5] = MSDOS_I(de->d_parent->d_inode)->i_logstart;
631 spin_unlock(&de->d_lock);
632 return 3;
635 struct dentry *fat_get_parent(struct dentry *child)
637 struct buffer_head *bh=NULL;
638 struct msdos_dir_entry *de = NULL;
639 struct dentry *parent = NULL;
640 int res;
641 loff_t i_pos = 0;
642 struct inode *inode;
644 lock_kernel();
645 res = fat_scan(child->d_inode, MSDOS_DOTDOT, &bh, &de, &i_pos);
647 if (res < 0)
648 goto out;
649 inode = fat_build_inode(child->d_sb, de, i_pos, &res);
650 if (res)
651 goto out;
652 if (!inode)
653 res = -EACCES;
654 else {
655 parent = d_alloc_anon(inode);
656 if (!parent) {
657 iput(inode);
658 res = -ENOMEM;
662 out:
663 if(bh)
664 brelse(bh);
665 unlock_kernel();
666 if (res)
667 return ERR_PTR(res);
668 else
669 return parent;
672 static kmem_cache_t *fat_inode_cachep;
674 static struct inode *fat_alloc_inode(struct super_block *sb)
676 struct msdos_inode_info *ei;
677 ei = (struct msdos_inode_info *)kmem_cache_alloc(fat_inode_cachep, SLAB_KERNEL);
678 if (!ei)
679 return NULL;
680 return &ei->vfs_inode;
683 static void fat_destroy_inode(struct inode *inode)
685 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
688 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
690 struct msdos_inode_info *ei = (struct msdos_inode_info *) foo;
692 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
693 SLAB_CTOR_CONSTRUCTOR) {
694 INIT_LIST_HEAD(&ei->i_fat_hash);
695 inode_init_once(&ei->vfs_inode);
699 int __init fat_init_inodecache(void)
701 fat_inode_cachep = kmem_cache_create("fat_inode_cache",
702 sizeof(struct msdos_inode_info),
703 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
704 init_once, NULL);
705 if (fat_inode_cachep == NULL)
706 return -ENOMEM;
707 return 0;
710 void __exit fat_destroy_inodecache(void)
712 if (kmem_cache_destroy(fat_inode_cachep))
713 printk(KERN_INFO "fat_inode_cache: not all structures were freed\n");
716 static struct super_operations fat_sops = {
717 .alloc_inode = fat_alloc_inode,
718 .destroy_inode = fat_destroy_inode,
719 .write_inode = fat_write_inode,
720 .delete_inode = fat_delete_inode,
721 .put_super = fat_put_super,
722 .statfs = fat_statfs,
723 .clear_inode = fat_clear_inode,
725 .read_inode = make_bad_inode,
727 .show_options = fat_show_options,
730 static struct export_operations fat_export_ops = {
731 .decode_fh = fat_decode_fh,
732 .encode_fh = fat_encode_fh,
733 .get_dentry = fat_get_dentry,
734 .get_parent = fat_get_parent,
738 * Read the super block of an MS-DOS FS.
740 int fat_fill_super(struct super_block *sb, void *data, int silent,
741 struct inode_operations *fs_dir_inode_ops, int isvfat)
743 struct inode *root_inode = NULL;
744 struct buffer_head *bh;
745 struct fat_boot_sector *b;
746 struct msdos_sb_info *sbi;
747 int logical_sector_size, fat_clusters, debug, cp, first;
748 unsigned int total_sectors, rootdir_sectors;
749 unsigned char media;
750 long error;
751 char buf[50];
753 sbi = kmalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
754 if (!sbi)
755 return -ENOMEM;
756 sb->s_fs_info = sbi;
757 memset(sbi, 0, sizeof(struct msdos_sb_info));
759 sb->s_magic = MSDOS_SUPER_MAGIC;
760 sb->s_op = &fat_sops;
761 sb->s_export_op = &fat_export_ops;
762 sbi->dir_ops = fs_dir_inode_ops;
764 error = -EINVAL;
765 if (!parse_options(data, isvfat, &debug, &sbi->options))
766 goto out_fail;
768 fat_cache_init(sb);
769 /* set up enough so that it can read an inode */
770 init_MUTEX(&sbi->fat_lock);
772 error = -EIO;
773 sb_min_blocksize(sb, 512);
774 bh = sb_bread(sb, 0);
775 if (bh == NULL) {
776 printk(KERN_ERR "FAT: unable to read boot sector\n");
777 goto out_fail;
780 b = (struct fat_boot_sector *) bh->b_data;
781 if (!b->reserved) {
782 if (!silent)
783 printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
784 brelse(bh);
785 goto out_invalid;
787 if (!b->fats) {
788 if (!silent)
789 printk(KERN_ERR "FAT: bogus number of FAT structure\n");
790 brelse(bh);
791 goto out_invalid;
793 if (!b->secs_track) {
794 if (!silent)
795 printk(KERN_ERR "FAT: bogus sectors-per-track value\n");
796 brelse(bh);
797 goto out_invalid;
799 if (!b->heads) {
800 if (!silent)
801 printk(KERN_ERR "FAT: bogus number-of-heads value\n");
802 brelse(bh);
803 goto out_invalid;
805 media = b->media;
806 if (!FAT_VALID_MEDIA(media)) {
807 if (!silent)
808 printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
809 media);
810 brelse(bh);
811 goto out_invalid;
813 logical_sector_size =
814 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
815 if (!logical_sector_size
816 || (logical_sector_size & (logical_sector_size - 1))
817 || (logical_sector_size < 512)
818 || (PAGE_CACHE_SIZE < logical_sector_size)) {
819 if (!silent)
820 printk(KERN_ERR "FAT: bogus logical sector size %d\n",
821 logical_sector_size);
822 brelse(bh);
823 goto out_invalid;
825 sbi->cluster_size = b->cluster_size;
826 if (!sbi->cluster_size
827 || (sbi->cluster_size & (sbi->cluster_size - 1))) {
828 if (!silent)
829 printk(KERN_ERR "FAT: bogus cluster size %d\n",
830 sbi->cluster_size);
831 brelse(bh);
832 goto out_invalid;
835 if (logical_sector_size < sb->s_blocksize) {
836 printk(KERN_ERR "FAT: logical sector size too small for device"
837 " (logical sector size = %d)\n", logical_sector_size);
838 brelse(bh);
839 goto out_fail;
841 if (logical_sector_size > sb->s_blocksize) {
842 brelse(bh);
844 if (!sb_set_blocksize(sb, logical_sector_size)) {
845 printk(KERN_ERR "FAT: unable to set blocksize %d\n",
846 logical_sector_size);
847 goto out_fail;
849 bh = sb_bread(sb, 0);
850 if (bh == NULL) {
851 printk(KERN_ERR "FAT: unable to read boot sector"
852 " (logical sector size = %lu)\n",
853 sb->s_blocksize);
854 goto out_fail;
856 b = (struct fat_boot_sector *) bh->b_data;
859 sbi->cluster_bits = ffs(sb->s_blocksize * sbi->cluster_size) - 1;
860 sbi->fats = b->fats;
861 sbi->fat_bits = 0; /* Don't know yet */
862 sbi->fat_start = CF_LE_W(b->reserved);
863 sbi->fat_length = CF_LE_W(b->fat_length);
864 sbi->root_cluster = 0;
865 sbi->free_clusters = -1; /* Don't know yet */
866 sbi->prev_free = 0;
868 if (!sbi->fat_length && b->fat32_length) {
869 struct fat_boot_fsinfo *fsinfo;
870 struct buffer_head *fsinfo_bh;
872 /* Must be FAT32 */
873 sbi->fat_bits = 32;
874 sbi->fat_length = CF_LE_L(b->fat32_length);
875 sbi->root_cluster = CF_LE_L(b->root_cluster);
877 sb->s_maxbytes = 0xffffffff;
879 /* MC - if info_sector is 0, don't multiply by 0 */
880 sbi->fsinfo_sector = CF_LE_W(b->info_sector);
881 if (sbi->fsinfo_sector == 0)
882 sbi->fsinfo_sector = 1;
884 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
885 if (fsinfo_bh == NULL) {
886 printk(KERN_ERR "FAT: bread failed, FSINFO block"
887 " (sector = %lu)\n", sbi->fsinfo_sector);
888 brelse(bh);
889 goto out_fail;
892 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
893 if (!IS_FSINFO(fsinfo)) {
894 printk(KERN_WARNING
895 "FAT: Did not find valid FSINFO signature.\n"
896 " Found signature1 0x%08x signature2 0x%08x"
897 " (sector = %lu)\n",
898 CF_LE_L(fsinfo->signature1),
899 CF_LE_L(fsinfo->signature2),
900 sbi->fsinfo_sector);
901 } else {
902 sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
903 sbi->prev_free = CF_LE_L(fsinfo->next_cluster);
906 brelse(fsinfo_bh);
909 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
910 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
912 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
913 sbi->dir_entries =
914 CF_LE_W(get_unaligned((unsigned short *)&b->dir_entries));
915 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
916 printk(KERN_ERR "FAT: bogus directroy-entries per block\n");
917 brelse(bh);
918 goto out_invalid;
921 rootdir_sectors = sbi->dir_entries
922 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
923 sbi->data_start = sbi->dir_start + rootdir_sectors;
924 total_sectors = CF_LE_W(get_unaligned((unsigned short *)&b->sectors));
925 if (total_sectors == 0)
926 total_sectors = CF_LE_L(b->total_sect);
927 sbi->clusters = (total_sectors - sbi->data_start) / sbi->cluster_size;
929 if (sbi->fat_bits != 32)
930 sbi->fat_bits = (sbi->clusters > MSDOS_FAT12) ? 16 : 12;
932 /* check that FAT table does not overflow */
933 fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
934 if (sbi->clusters > fat_clusters - 2)
935 sbi->clusters = fat_clusters - 2;
937 brelse(bh);
939 /* validity check of FAT */
940 first = __fat_access(sb, 0, -1);
941 if (first < 0) {
942 error = first;
943 goto out_fail;
945 if (FAT_FIRST_ENT(sb, media) != first
946 && (media != 0xf8 || FAT_FIRST_ENT(sb, 0xfe) != first)) {
947 if (!silent) {
948 printk(KERN_ERR "FAT: invalid first entry of FAT "
949 "(0x%x != 0x%x)\n",
950 FAT_FIRST_ENT(sb, media), first);
952 goto out_invalid;
955 error = -EINVAL;
956 cp = sbi->options.codepage ? sbi->options.codepage : 437;
957 sprintf(buf, "cp%d", cp);
958 sbi->nls_disk = load_nls(buf);
959 if (!sbi->nls_disk) {
960 /* Fail only if explicit charset specified */
961 if (sbi->options.codepage != 0) {
962 printk(KERN_ERR "FAT: codepage %s not found\n", buf);
963 goto out_fail;
965 sbi->options.codepage = 0; /* already 0?? */
966 sbi->nls_disk = load_nls_default();
969 /* FIXME: utf8 is using iocharset for upper/lower conversion */
970 if (sbi->options.isvfat) {
971 if (sbi->options.iocharset != NULL) {
972 sbi->nls_io = load_nls(sbi->options.iocharset);
973 if (!sbi->nls_io) {
974 printk(KERN_ERR
975 "FAT: IO charset %s not found\n",
976 sbi->options.iocharset);
977 goto out_fail;
979 } else
980 sbi->nls_io = load_nls_default();
983 error = -ENOMEM;
984 root_inode = new_inode(sb);
985 if (!root_inode)
986 goto out_fail;
987 root_inode->i_ino = MSDOS_ROOT_INO;
988 root_inode->i_version = 1;
989 error = fat_read_root(root_inode);
990 if (error < 0)
991 goto out_fail;
992 error = -ENOMEM;
993 insert_inode_hash(root_inode);
994 sb->s_root = d_alloc_root(root_inode);
995 if (!sb->s_root) {
996 printk(KERN_ERR "FAT: get root inode failed\n");
997 goto out_fail;
1000 return 0;
1002 out_invalid:
1003 error = -EINVAL;
1004 if (!silent)
1005 printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
1006 " on dev %s.\n", sb->s_id);
1008 out_fail:
1009 if (root_inode)
1010 iput(root_inode);
1011 if (sbi->nls_io)
1012 unload_nls(sbi->nls_io);
1013 if (sbi->nls_disk)
1014 unload_nls(sbi->nls_disk);
1015 if (sbi->options.iocharset)
1016 kfree(sbi->options.iocharset);
1017 sb->s_fs_info = NULL;
1018 kfree(sbi);
1019 return error;
1022 int fat_statfs(struct super_block *sb, struct kstatfs *buf)
1024 int free,nr;
1026 lock_fat(sb);
1027 if (MSDOS_SB(sb)->free_clusters != -1)
1028 free = MSDOS_SB(sb)->free_clusters;
1029 else {
1030 free = 0;
1031 for (nr = 2; nr < MSDOS_SB(sb)->clusters + 2; nr++)
1032 if (fat_access(sb, nr, -1) == FAT_ENT_FREE)
1033 free++;
1034 MSDOS_SB(sb)->free_clusters = free;
1036 unlock_fat(sb);
1038 buf->f_type = sb->s_magic;
1039 buf->f_bsize = 1 << MSDOS_SB(sb)->cluster_bits;
1040 buf->f_blocks = MSDOS_SB(sb)->clusters;
1041 buf->f_bfree = free;
1042 buf->f_bavail = free;
1043 buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
1045 return 0;
1048 static int is_exec(char *extension)
1050 char *exe_extensions = "EXECOMBAT", *walk;
1052 for (walk = exe_extensions; *walk; walk += 3)
1053 if (!strncmp(extension, walk, 3))
1054 return 1;
1055 return 0;
1058 static int fat_writepage(struct page *page, struct writeback_control *wbc)
1060 return block_write_full_page(page,fat_get_block, wbc);
1062 static int fat_readpage(struct file *file, struct page *page)
1064 return block_read_full_page(page,fat_get_block);
1067 static int
1068 fat_prepare_write(struct file *file, struct page *page,
1069 unsigned from, unsigned to)
1071 kmap(page);
1072 return cont_prepare_write(page,from,to,fat_get_block,
1073 &MSDOS_I(page->mapping->host)->mmu_private);
1076 static int
1077 fat_commit_write(struct file *file, struct page *page,
1078 unsigned from, unsigned to)
1080 kunmap(page);
1081 return generic_commit_write(file, page, from, to);
1084 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
1086 return generic_block_bmap(mapping,block,fat_get_block);
1088 static struct address_space_operations fat_aops = {
1089 .readpage = fat_readpage,
1090 .writepage = fat_writepage,
1091 .sync_page = block_sync_page,
1092 .prepare_write = fat_prepare_write,
1093 .commit_write = fat_commit_write,
1094 .bmap = _fat_bmap
1097 /* doesn't deal with root inode */
1098 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
1100 struct super_block *sb = inode->i_sb;
1101 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1102 int error;
1104 MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
1105 MSDOS_I(inode)->i_pos = 0;
1106 inode->i_uid = sbi->options.fs_uid;
1107 inode->i_gid = sbi->options.fs_gid;
1108 inode->i_version++;
1109 inode->i_generation = get_seconds();
1111 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
1112 inode->i_generation &= ~1;
1113 inode->i_mode = MSDOS_MKMODE(de->attr,
1114 S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
1115 inode->i_op = sbi->dir_ops;
1116 inode->i_fop = &fat_dir_operations;
1118 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1119 if (sbi->fat_bits == 32)
1120 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1122 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1123 error = fat_calc_dir_size(inode);
1124 if (error < 0)
1125 return error;
1126 MSDOS_I(inode)->mmu_private = inode->i_size;
1128 inode->i_nlink = fat_subdirs(inode);
1129 } else { /* not a directory */
1130 inode->i_generation |= 1;
1131 inode->i_mode = MSDOS_MKMODE(de->attr,
1132 ((sbi->options.showexec &&
1133 !is_exec(de->ext))
1134 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
1135 & ~sbi->options.fs_fmask) | S_IFREG;
1136 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1137 if (sbi->fat_bits == 32)
1138 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1140 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1141 inode->i_size = CF_LE_L(de->size);
1142 inode->i_op = &fat_file_inode_operations;
1143 inode->i_fop = &fat_file_operations;
1144 inode->i_mapping->a_ops = &fat_aops;
1145 MSDOS_I(inode)->mmu_private = inode->i_size;
1147 if(de->attr & ATTR_SYS)
1148 if (sbi->options.sys_immutable)
1149 inode->i_flags |= S_IMMUTABLE;
1150 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
1151 /* this is as close to the truth as we can get ... */
1152 inode->i_blksize = 1 << sbi->cluster_bits;
1153 inode->i_blocks = ((inode->i_size + inode->i_blksize - 1)
1154 & ~((loff_t)inode->i_blksize - 1)) >> 9;
1155 inode->i_mtime.tv_sec = inode->i_atime.tv_sec =
1156 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
1157 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0;
1158 inode->i_ctime.tv_sec =
1159 MSDOS_SB(sb)->options.isvfat
1160 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
1161 : inode->i_mtime.tv_sec;
1162 inode->i_ctime.tv_nsec = de->ctime_ms * 1000000;
1163 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
1165 return 0;
1168 void fat_write_inode(struct inode *inode, int wait)
1170 struct super_block *sb = inode->i_sb;
1171 struct buffer_head *bh;
1172 struct msdos_dir_entry *raw_entry;
1173 loff_t i_pos;
1175 retry:
1176 i_pos = MSDOS_I(inode)->i_pos;
1177 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
1178 return;
1180 lock_kernel();
1181 if (!(bh = sb_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
1182 fat_fs_panic(sb, "unable to read i-node block (i_pos %llu)",
1183 i_pos);
1184 unlock_kernel();
1185 return;
1187 spin_lock(&fat_inode_lock);
1188 if (i_pos != MSDOS_I(inode)->i_pos) {
1189 spin_unlock(&fat_inode_lock);
1190 brelse(bh);
1191 unlock_kernel();
1192 goto retry;
1195 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
1196 [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
1197 if (S_ISDIR(inode->i_mode)) {
1198 raw_entry->attr = ATTR_DIR;
1199 raw_entry->size = 0;
1201 else {
1202 raw_entry->attr = ATTR_NONE;
1203 raw_entry->size = CT_LE_L(inode->i_size);
1205 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
1206 MSDOS_I(inode)->i_attrs;
1207 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
1208 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
1209 fat_date_unix2dos(inode->i_mtime.tv_sec,&raw_entry->time,&raw_entry->date);
1210 raw_entry->time = CT_LE_W(raw_entry->time);
1211 raw_entry->date = CT_LE_W(raw_entry->date);
1212 if (MSDOS_SB(sb)->options.isvfat) {
1213 fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
1214 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms; /* use i_ctime.tv_nsec? */
1215 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
1216 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
1218 spin_unlock(&fat_inode_lock);
1219 mark_buffer_dirty(bh);
1220 brelse(bh);
1221 unlock_kernel();
1225 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1227 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
1228 struct inode *inode = dentry->d_inode;
1229 int mask, error = 0;
1231 lock_kernel();
1233 /* FAT cannot truncate to a longer file */
1234 if (attr->ia_valid & ATTR_SIZE) {
1235 if (attr->ia_size > inode->i_size) {
1236 error = -EPERM;
1237 goto out;
1241 error = inode_change_ok(inode, attr);
1242 if (error) {
1243 if (sbi->options.quiet)
1244 error = 0;
1245 goto out;
1248 if (((attr->ia_valid & ATTR_UID) &&
1249 (attr->ia_uid != sbi->options.fs_uid)) ||
1250 ((attr->ia_valid & ATTR_GID) &&
1251 (attr->ia_gid != sbi->options.fs_gid)) ||
1252 ((attr->ia_valid & ATTR_MODE) &&
1253 (attr->ia_mode & ~MSDOS_VALID_MODE)))
1254 error = -EPERM;
1256 if (error) {
1257 if (sbi->options.quiet)
1258 error = 0;
1259 goto out;
1261 error = inode_setattr(inode, attr);
1262 if (error)
1263 goto out;
1265 if (S_ISDIR(inode->i_mode))
1266 mask = sbi->options.fs_dmask;
1267 else
1268 mask = sbi->options.fs_fmask;
1269 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
1270 out:
1271 unlock_kernel();
1272 return error;
1274 MODULE_LICENSE("GPL");