[PATCH] ->cluster_size cleanup (11/11)
[linux-2.6/history.git] / fs / fat / inode.c
blob4d23eafeca3d2cda638ab8109efc201099fe683c
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 if (!(sb->s_flags & MS_RDONLY))
163 fat_clusters_flush(sb);
165 if (sbi->nls_disk) {
166 unload_nls(sbi->nls_disk);
167 sbi->nls_disk = NULL;
168 sbi->options.codepage = 0;
170 if (sbi->nls_io) {
171 unload_nls(sbi->nls_io);
172 sbi->nls_io = NULL;
175 * Note: the iocharset option might have been specified
176 * without enabling nls_io, so check for it here.
178 if (sbi->options.iocharset) {
179 kfree(sbi->options.iocharset);
180 sbi->options.iocharset = NULL;
182 sb->s_fs_info = NULL;
183 kfree(sbi);
186 static int simple_getbool(char *s, int *setval)
188 if (s) {
189 if (!strcmp(s,"1") || !strcmp(s,"yes") || !strcmp(s,"true"))
190 *setval = 1;
191 else if (!strcmp(s,"0") || !strcmp(s,"no") || !strcmp(s,"false"))
192 *setval = 0;
193 else
194 return 0;
195 } else
196 *setval = 1;
197 return 1;
200 static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
202 struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
203 struct fat_mount_options *opts = &sbi->options;
204 int isvfat = opts->isvfat;
206 if (opts->fs_uid != 0)
207 seq_printf(m, ",uid=%d", opts->fs_uid);
208 if (opts->fs_gid != 0)
209 seq_printf(m, ",gid=%d", opts->fs_gid);
210 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
211 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
212 if (sbi->nls_disk)
213 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
214 if (isvfat) {
215 if (sbi->nls_io
216 && strcmp(sbi->nls_io->charset, CONFIG_NLS_DEFAULT))
217 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
219 switch (opts->shortname) {
220 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
221 seq_puts(m, ",shortname=win95");
222 break;
223 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
224 seq_puts(m, ",shortname=winnt");
225 break;
226 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
227 seq_puts(m, ",shortname=mixed");
228 break;
229 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
230 /* seq_puts(m, ",shortname=lower"); */
231 break;
232 default:
233 seq_puts(m, ",shortname=unknown");
234 break;
237 if (opts->name_check != 'n')
238 seq_printf(m, ",check=%c", opts->name_check);
239 if (opts->quiet)
240 seq_puts(m, ",quiet");
241 if (opts->showexec)
242 seq_puts(m, ",showexec");
243 if (opts->sys_immutable)
244 seq_puts(m, ",sys_immutable");
245 if (!isvfat) {
246 if (opts->dotsOK)
247 seq_puts(m, ",dotsOK=yes");
248 if (opts->nocase)
249 seq_puts(m, ",nocase");
250 } else {
251 if (opts->utf8)
252 seq_puts(m, ",utf8");
253 if (opts->unicode_xlate)
254 seq_puts(m, ",uni_xlate");
255 if (!opts->numtail)
256 seq_puts(m, ",nonumtail");
259 return 0;
262 static int parse_options(char *options, int is_vfat, int *debug,
263 struct fat_mount_options *opts)
265 char *this_char, *value, *p;
266 int ret = 1, val, len;
268 opts->isvfat = is_vfat;
270 opts->fs_uid = current->uid;
271 opts->fs_gid = current->gid;
272 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
273 opts->codepage = 0;
274 opts->iocharset = NULL;
275 if (is_vfat)
276 opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
277 else
278 opts->shortname = 0;
279 opts->name_check = 'n';
280 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
281 opts->utf8 = opts->unicode_xlate = 0;
282 opts->numtail = 1;
283 opts->nocase = 0;
284 *debug = 0;
286 if (!options)
287 goto out;
288 while ((this_char = strsep(&options,",")) != NULL) {
289 if (!*this_char)
290 continue;
291 if ((value = strchr(this_char,'=')) != NULL)
292 *value++ = 0;
294 if (!strcmp(this_char,"check") && value) {
295 if (value[0] && !value[1] && strchr("rns",*value))
296 opts->name_check = *value;
297 else if (!strcmp(value,"relaxed"))
298 opts->name_check = 'r';
299 else if (!strcmp(value,"normal"))
300 opts->name_check = 'n';
301 else if (!strcmp(value,"strict"))
302 opts->name_check = 's';
303 else ret = 0;
305 else if (!strcmp(this_char,"conv") && value) {
306 printk(KERN_INFO "FAT: conv option is obsolete, "
307 "not supported now\n");
309 else if (!strcmp(this_char,"nocase")) {
310 if (!is_vfat)
311 opts->nocase = 1;
312 else {
313 /* for backward compatible */
314 opts->shortname = VFAT_SFN_DISPLAY_WIN95
315 | VFAT_SFN_CREATE_WIN95;
318 else if (!strcmp(this_char,"showexec")) {
319 opts->showexec = 1;
321 else if (!strcmp(this_char,"uid")) {
322 if (!value || !*value) ret = 0;
323 else {
324 opts->fs_uid = simple_strtoul(value,&value,0);
325 if (*value) ret = 0;
328 else if (!strcmp(this_char,"gid")) {
329 if (!value || !*value) ret= 0;
330 else {
331 opts->fs_gid = simple_strtoul(value,&value,0);
332 if (*value) ret = 0;
335 else if (!strcmp(this_char,"umask")) {
336 if (!value || !*value) ret = 0;
337 else {
338 opts->fs_fmask = opts->fs_dmask =
339 simple_strtoul(value,&value,8);
340 if (*value) ret = 0;
343 else if (!strcmp(this_char,"fmask")) {
344 if (!value || !*value) ret = 0;
345 else {
346 opts->fs_fmask = simple_strtoul(value,&value,8);
347 if (*value) ret = 0;
350 else if (!strcmp(this_char,"dmask")) {
351 if (!value || !*value) ret = 0;
352 else {
353 opts->fs_dmask = simple_strtoul(value,&value,8);
354 if (*value) ret = 0;
357 else if (!strcmp(this_char,"debug")) {
358 if (value) ret = 0;
359 else *debug = 1;
361 else if (!strcmp(this_char,"fat")) {
362 printk(KERN_INFO "FAT: fat option is obsolete, "
363 "not supported now\n");
365 else if (!strcmp(this_char,"quiet")) {
366 if (value) ret = 0;
367 else opts->quiet = 1;
369 else if (!strcmp(this_char,"blocksize")) {
370 printk(KERN_INFO "FAT: blocksize option is obsolete, "
371 "not supported now\n");
373 else if (!strcmp(this_char,"sys_immutable")) {
374 if (value) ret = 0;
375 else opts->sys_immutable = 1;
377 else if (!strcmp(this_char,"codepage") && value) {
378 opts->codepage = simple_strtoul(value,&value,0);
379 if (*value) ret = 0;
382 /* msdos specific */
383 else if (!is_vfat && !strcmp(this_char,"dots")) {
384 opts->dotsOK = 1;
386 else if (!is_vfat && !strcmp(this_char,"nodots")) {
387 opts->dotsOK = 0;
389 else if (!is_vfat && !strcmp(this_char,"dotsOK") && value) {
390 if (!strcmp(value,"yes")) opts->dotsOK = 1;
391 else if (!strcmp(value,"no")) opts->dotsOK = 0;
392 else ret = 0;
395 /* vfat specific */
396 else if (is_vfat && !strcmp(this_char,"iocharset") && value) {
397 p = value;
398 while (*value && *value != ',')
399 value++;
400 len = value - p;
401 if (len) {
402 char *buffer;
404 if (opts->iocharset != NULL) {
405 kfree(opts->iocharset);
406 opts->iocharset = NULL;
408 buffer = kmalloc(len + 1, GFP_KERNEL);
409 if (buffer != NULL) {
410 opts->iocharset = buffer;
411 memcpy(buffer, p, len);
412 buffer[len] = 0;
413 } else
414 ret = 0;
417 else if (is_vfat && !strcmp(this_char,"utf8")) {
418 ret = simple_getbool(value, &val);
419 if (ret) opts->utf8 = val;
421 else if (is_vfat && !strcmp(this_char,"uni_xlate")) {
422 ret = simple_getbool(value, &val);
423 if (ret) opts->unicode_xlate = val;
425 else if (is_vfat && !strcmp(this_char,"posix")) {
426 printk(KERN_INFO "FAT: posix option is obsolete, "
427 "not supported now\n");
429 else if (is_vfat && !strcmp(this_char,"nonumtail")) {
430 ret = simple_getbool(value, &val);
431 if (ret) {
432 opts->numtail = !val;
435 else if (is_vfat && !strcmp(this_char, "shortname")) {
436 if (!strcmp(value, "lower"))
437 opts->shortname = VFAT_SFN_DISPLAY_LOWER
438 | VFAT_SFN_CREATE_WIN95;
439 else if (!strcmp(value, "win95"))
440 opts->shortname = VFAT_SFN_DISPLAY_WIN95
441 | VFAT_SFN_CREATE_WIN95;
442 else if (!strcmp(value, "winnt"))
443 opts->shortname = VFAT_SFN_DISPLAY_WINNT
444 | VFAT_SFN_CREATE_WINNT;
445 else if (!strcmp(value, "mixed"))
446 opts->shortname = VFAT_SFN_DISPLAY_WINNT
447 | VFAT_SFN_CREATE_WIN95;
448 else
449 ret = 0;
450 } else {
451 printk(KERN_ERR "FAT: Unrecognized mount option %s\n",
452 this_char);
453 ret = 0;
456 if (ret == 0)
457 break;
459 out:
460 if (opts->unicode_xlate)
461 opts->utf8 = 0;
463 return ret;
466 static int fat_calc_dir_size(struct inode *inode)
468 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
469 int ret, fclus, dclus;
471 inode->i_size = 0;
472 if (MSDOS_I(inode)->i_start == 0)
473 return 0;
475 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
476 if (ret < 0)
477 return ret;
478 inode->i_size = (fclus + 1) << sbi->cluster_bits;
480 return 0;
483 static int fat_read_root(struct inode *inode)
485 struct super_block *sb = inode->i_sb;
486 struct msdos_sb_info *sbi = MSDOS_SB(sb);
487 int error;
489 MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
490 MSDOS_I(inode)->i_pos = 0;
491 inode->i_uid = sbi->options.fs_uid;
492 inode->i_gid = sbi->options.fs_gid;
493 inode->i_version++;
494 inode->i_generation = 0;
495 inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
496 inode->i_op = sbi->dir_ops;
497 inode->i_fop = &fat_dir_operations;
498 if (sbi->fat_bits == 32) {
499 MSDOS_I(inode)->i_start = sbi->root_cluster;
500 error = fat_calc_dir_size(inode);
501 if (error < 0)
502 return error;
503 } else {
504 MSDOS_I(inode)->i_start = 0;
505 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
507 inode->i_blksize = sbi->cluster_size;
508 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
509 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
510 MSDOS_I(inode)->i_logstart = 0;
511 MSDOS_I(inode)->mmu_private = inode->i_size;
513 MSDOS_I(inode)->i_attrs = 0;
514 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
515 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
516 MSDOS_I(inode)->i_ctime_ms = 0;
517 inode->i_nlink = fat_subdirs(inode)+2;
519 return 0;
523 * a FAT file handle with fhtype 3 is
524 * 0/ i_ino - for fast, reliable lookup if still in the cache
525 * 1/ i_generation - to see if i_ino is still valid
526 * bit 0 == 0 iff directory
527 * 2/ i_pos - if ino has changed, but still in cache (hi)
528 * 3/ i_pos - if ino has changed, but still in cache (low)
529 * 4/ i_logstart - to semi-verify inode found at i_location
530 * 5/ parent->i_logstart - maybe used to hunt for the file on disc
534 struct dentry *fat_decode_fh(struct super_block *sb, __u32 *fh,
535 int len, int fhtype,
536 int (*acceptable)(void *context, struct dentry *de),
537 void *context)
540 if (fhtype != 3)
541 return ERR_PTR(-ESTALE);
542 if (len < 6)
543 return ERR_PTR(-ESTALE);
545 return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, context);
548 struct dentry *fat_get_dentry(struct super_block *sb, void *inump)
550 struct inode *inode = NULL;
551 struct dentry *result;
552 __u32 *fh = inump;
554 inode = iget(sb, fh[0]);
555 if (!inode || is_bad_inode(inode) ||
556 inode->i_generation != fh[1]) {
557 if (inode) iput(inode);
558 inode = NULL;
560 if (!inode) {
561 loff_t i_pos = ((loff_t)fh[2] << 32) | fh[3];
563 /* try 2 - see if i_pos is in F-d-c
564 * require i_logstart to be the same
565 * Will fail if you truncate and then re-write
568 inode = fat_iget(sb, i_pos);
569 if (inode && MSDOS_I(inode)->i_logstart != fh[4]) {
570 iput(inode);
571 inode = NULL;
574 if (!inode) {
575 /* For now, do nothing
576 * What we could do is:
577 * follow the file starting at fh[4], and record
578 * the ".." entry, and the name of the fh[2] entry.
579 * The follow the ".." file finding the next step up.
580 * This way we build a path to the root of
581 * the tree. If this works, we lookup the path and so
582 * get this inode into the cache.
583 * Finally try the fat_iget lookup again
584 * If that fails, then weare totally out of luck
585 * But all that is for another day
588 if (!inode)
589 return ERR_PTR(-ESTALE);
592 /* now to find a dentry.
593 * If possible, get a well-connected one
595 result = d_alloc_anon(inode);
596 if (result == NULL) {
597 iput(inode);
598 return ERR_PTR(-ENOMEM);
600 result->d_op = sb->s_root->d_op;
601 return result;
604 int fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable)
606 int len = *lenp;
607 struct inode *inode = de->d_inode;
609 if (len < 6)
610 return 255; /* no room */
611 *lenp = 6;
612 fh[0] = inode->i_ino;
613 fh[1] = inode->i_generation;
614 fh[2] = (__u32)(MSDOS_I(inode)->i_pos >> 32);
615 fh[3] = (__u32)MSDOS_I(inode)->i_pos;
616 fh[4] = MSDOS_I(inode)->i_logstart;
617 spin_lock(&de->d_lock);
618 fh[5] = MSDOS_I(de->d_parent->d_inode)->i_logstart;
619 spin_unlock(&de->d_lock);
620 return 3;
623 struct dentry *fat_get_parent(struct dentry *child)
625 struct buffer_head *bh=NULL;
626 struct msdos_dir_entry *de = NULL;
627 struct dentry *parent = NULL;
628 int res;
629 loff_t i_pos = 0;
630 struct inode *inode;
632 lock_kernel();
633 res = fat_scan(child->d_inode, MSDOS_DOTDOT, &bh, &de, &i_pos);
635 if (res < 0)
636 goto out;
637 inode = fat_build_inode(child->d_sb, de, i_pos, &res);
638 if (res)
639 goto out;
640 if (!inode)
641 res = -EACCES;
642 else {
643 parent = d_alloc_anon(inode);
644 if (!parent) {
645 iput(inode);
646 res = -ENOMEM;
650 out:
651 if(bh)
652 brelse(bh);
653 unlock_kernel();
654 if (res)
655 return ERR_PTR(res);
656 else
657 return parent;
660 static kmem_cache_t *fat_inode_cachep;
662 static struct inode *fat_alloc_inode(struct super_block *sb)
664 struct msdos_inode_info *ei;
665 ei = (struct msdos_inode_info *)kmem_cache_alloc(fat_inode_cachep, SLAB_KERNEL);
666 if (!ei)
667 return NULL;
668 return &ei->vfs_inode;
671 static void fat_destroy_inode(struct inode *inode)
673 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
676 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
678 struct msdos_inode_info *ei = (struct msdos_inode_info *) foo;
680 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
681 SLAB_CTOR_CONSTRUCTOR) {
682 INIT_LIST_HEAD(&ei->i_fat_hash);
683 inode_init_once(&ei->vfs_inode);
687 int __init fat_init_inodecache(void)
689 fat_inode_cachep = kmem_cache_create("fat_inode_cache",
690 sizeof(struct msdos_inode_info),
691 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
692 init_once, NULL);
693 if (fat_inode_cachep == NULL)
694 return -ENOMEM;
695 return 0;
698 void __exit fat_destroy_inodecache(void)
700 if (kmem_cache_destroy(fat_inode_cachep))
701 printk(KERN_INFO "fat_inode_cache: not all structures were freed\n");
704 static struct super_operations fat_sops = {
705 .alloc_inode = fat_alloc_inode,
706 .destroy_inode = fat_destroy_inode,
707 .write_inode = fat_write_inode,
708 .delete_inode = fat_delete_inode,
709 .put_super = fat_put_super,
710 .statfs = fat_statfs,
711 .clear_inode = fat_clear_inode,
713 .read_inode = make_bad_inode,
715 .show_options = fat_show_options,
718 static struct export_operations fat_export_ops = {
719 .decode_fh = fat_decode_fh,
720 .encode_fh = fat_encode_fh,
721 .get_dentry = fat_get_dentry,
722 .get_parent = fat_get_parent,
726 * Read the super block of an MS-DOS FS.
728 int fat_fill_super(struct super_block *sb, void *data, int silent,
729 struct inode_operations *fs_dir_inode_ops, int isvfat)
731 struct inode *root_inode = NULL;
732 struct buffer_head *bh;
733 struct fat_boot_sector *b;
734 struct msdos_sb_info *sbi;
735 int logical_sector_size, fat_clusters, debug, cp, first;
736 unsigned int total_sectors, rootdir_sectors;
737 unsigned int media;
738 long error;
739 char buf[50];
741 sbi = kmalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
742 if (!sbi)
743 return -ENOMEM;
744 sb->s_fs_info = sbi;
745 memset(sbi, 0, sizeof(struct msdos_sb_info));
747 sb->s_magic = MSDOS_SUPER_MAGIC;
748 sb->s_op = &fat_sops;
749 sb->s_export_op = &fat_export_ops;
750 sbi->dir_ops = fs_dir_inode_ops;
752 error = -EINVAL;
753 if (!parse_options(data, isvfat, &debug, &sbi->options))
754 goto out_fail;
756 fat_cache_init(sb);
757 /* set up enough so that it can read an inode */
758 init_MUTEX(&sbi->fat_lock);
760 error = -EIO;
761 sb_min_blocksize(sb, 512);
762 bh = sb_bread(sb, 0);
763 if (bh == NULL) {
764 printk(KERN_ERR "FAT: unable to read boot sector\n");
765 goto out_fail;
768 b = (struct fat_boot_sector *) bh->b_data;
769 if (!b->reserved) {
770 if (!silent)
771 printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
772 brelse(bh);
773 goto out_invalid;
775 if (!b->fats) {
776 if (!silent)
777 printk(KERN_ERR "FAT: bogus number of FAT structure\n");
778 brelse(bh);
779 goto out_invalid;
781 if (!b->secs_track) {
782 if (!silent)
783 printk(KERN_ERR "FAT: bogus sectors-per-track value\n");
784 brelse(bh);
785 goto out_invalid;
787 if (!b->heads) {
788 if (!silent)
789 printk(KERN_ERR "FAT: bogus number-of-heads value\n");
790 brelse(bh);
791 goto out_invalid;
793 media = b->media;
794 if (!FAT_VALID_MEDIA(media)) {
795 if (!silent)
796 printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
797 media);
798 brelse(bh);
799 goto out_invalid;
801 logical_sector_size =
802 CF_LE_W(get_unaligned((unsigned short *) &b->sector_size));
803 if (!logical_sector_size
804 || (logical_sector_size & (logical_sector_size - 1))
805 || (logical_sector_size < 512)
806 || (PAGE_CACHE_SIZE < logical_sector_size)) {
807 if (!silent)
808 printk(KERN_ERR "FAT: bogus logical sector size %d\n",
809 logical_sector_size);
810 brelse(bh);
811 goto out_invalid;
813 sbi->sec_per_clus = b->sec_per_clus;
814 if (!sbi->sec_per_clus
815 || (sbi->sec_per_clus & (sbi->sec_per_clus - 1))) {
816 if (!silent)
817 printk(KERN_ERR "FAT: bogus sectors per cluster %d\n",
818 sbi->sec_per_clus);
819 brelse(bh);
820 goto out_invalid;
823 if (logical_sector_size < sb->s_blocksize) {
824 printk(KERN_ERR "FAT: logical sector size too small for device"
825 " (logical sector size = %d)\n", logical_sector_size);
826 brelse(bh);
827 goto out_fail;
829 if (logical_sector_size > sb->s_blocksize) {
830 brelse(bh);
832 if (!sb_set_blocksize(sb, logical_sector_size)) {
833 printk(KERN_ERR "FAT: unable to set blocksize %d\n",
834 logical_sector_size);
835 goto out_fail;
837 bh = sb_bread(sb, 0);
838 if (bh == NULL) {
839 printk(KERN_ERR "FAT: unable to read boot sector"
840 " (logical sector size = %lu)\n",
841 sb->s_blocksize);
842 goto out_fail;
844 b = (struct fat_boot_sector *) bh->b_data;
847 sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
848 sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
849 sbi->fats = b->fats;
850 sbi->fat_bits = 0; /* Don't know yet */
851 sbi->fat_start = CF_LE_W(b->reserved);
852 sbi->fat_length = CF_LE_W(b->fat_length);
853 sbi->root_cluster = 0;
854 sbi->free_clusters = -1; /* Don't know yet */
855 sbi->prev_free = 0;
857 if (!sbi->fat_length && b->fat32_length) {
858 struct fat_boot_fsinfo *fsinfo;
859 struct buffer_head *fsinfo_bh;
861 /* Must be FAT32 */
862 sbi->fat_bits = 32;
863 sbi->fat_length = CF_LE_L(b->fat32_length);
864 sbi->root_cluster = CF_LE_L(b->root_cluster);
866 sb->s_maxbytes = 0xffffffff;
868 /* MC - if info_sector is 0, don't multiply by 0 */
869 sbi->fsinfo_sector = CF_LE_W(b->info_sector);
870 if (sbi->fsinfo_sector == 0)
871 sbi->fsinfo_sector = 1;
873 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
874 if (fsinfo_bh == NULL) {
875 printk(KERN_ERR "FAT: bread failed, FSINFO block"
876 " (sector = %lu)\n", sbi->fsinfo_sector);
877 brelse(bh);
878 goto out_fail;
881 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
882 if (!IS_FSINFO(fsinfo)) {
883 printk(KERN_WARNING
884 "FAT: Did not find valid FSINFO signature.\n"
885 " Found signature1 0x%08x signature2 0x%08x"
886 " (sector = %lu)\n",
887 CF_LE_L(fsinfo->signature1),
888 CF_LE_L(fsinfo->signature2),
889 sbi->fsinfo_sector);
890 } else {
891 sbi->free_clusters = CF_LE_L(fsinfo->free_clusters);
892 sbi->prev_free = CF_LE_L(fsinfo->next_cluster);
895 brelse(fsinfo_bh);
898 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
899 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
901 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
902 sbi->dir_entries =
903 CF_LE_W(get_unaligned((unsigned short *)&b->dir_entries));
904 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
905 printk(KERN_ERR "FAT: bogus directroy-entries per block\n");
906 brelse(bh);
907 goto out_invalid;
910 rootdir_sectors = sbi->dir_entries
911 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
912 sbi->data_start = sbi->dir_start + rootdir_sectors;
913 total_sectors = CF_LE_W(get_unaligned((unsigned short *)&b->sectors));
914 if (total_sectors == 0)
915 total_sectors = CF_LE_L(b->total_sect);
916 sbi->clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
918 if (sbi->fat_bits != 32)
919 sbi->fat_bits = (sbi->clusters > MSDOS_FAT12) ? 16 : 12;
921 /* check that FAT table does not overflow */
922 fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
923 if (sbi->clusters > fat_clusters - 2)
924 sbi->clusters = fat_clusters - 2;
926 brelse(bh);
928 /* validity check of FAT */
929 first = __fat_access(sb, 0, -1);
930 if (first < 0) {
931 error = first;
932 goto out_fail;
934 if (FAT_FIRST_ENT(sb, media) != first
935 && (media != 0xf8 || FAT_FIRST_ENT(sb, 0xfe) != first)) {
936 if (!silent) {
937 printk(KERN_ERR "FAT: invalid first entry of FAT "
938 "(0x%x != 0x%x)\n",
939 FAT_FIRST_ENT(sb, media), first);
941 goto out_invalid;
944 error = -EINVAL;
945 cp = sbi->options.codepage ? sbi->options.codepage : 437;
946 sprintf(buf, "cp%d", cp);
947 sbi->nls_disk = load_nls(buf);
948 if (!sbi->nls_disk) {
949 /* Fail only if explicit charset specified */
950 if (sbi->options.codepage != 0) {
951 printk(KERN_ERR "FAT: codepage %s not found\n", buf);
952 goto out_fail;
954 sbi->options.codepage = 0; /* already 0?? */
955 sbi->nls_disk = load_nls_default();
958 /* FIXME: utf8 is using iocharset for upper/lower conversion */
959 if (sbi->options.isvfat) {
960 if (sbi->options.iocharset != NULL) {
961 sbi->nls_io = load_nls(sbi->options.iocharset);
962 if (!sbi->nls_io) {
963 printk(KERN_ERR
964 "FAT: IO charset %s not found\n",
965 sbi->options.iocharset);
966 goto out_fail;
968 } else
969 sbi->nls_io = load_nls_default();
972 error = -ENOMEM;
973 root_inode = new_inode(sb);
974 if (!root_inode)
975 goto out_fail;
976 root_inode->i_ino = MSDOS_ROOT_INO;
977 root_inode->i_version = 1;
978 error = fat_read_root(root_inode);
979 if (error < 0)
980 goto out_fail;
981 error = -ENOMEM;
982 insert_inode_hash(root_inode);
983 sb->s_root = d_alloc_root(root_inode);
984 if (!sb->s_root) {
985 printk(KERN_ERR "FAT: get root inode failed\n");
986 goto out_fail;
989 return 0;
991 out_invalid:
992 error = -EINVAL;
993 if (!silent)
994 printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
995 " on dev %s.\n", sb->s_id);
997 out_fail:
998 if (root_inode)
999 iput(root_inode);
1000 if (sbi->nls_io)
1001 unload_nls(sbi->nls_io);
1002 if (sbi->nls_disk)
1003 unload_nls(sbi->nls_disk);
1004 if (sbi->options.iocharset)
1005 kfree(sbi->options.iocharset);
1006 sb->s_fs_info = NULL;
1007 kfree(sbi);
1008 return error;
1011 int fat_statfs(struct super_block *sb, struct kstatfs *buf)
1013 int free, nr;
1015 if (MSDOS_SB(sb)->free_clusters != -1)
1016 free = MSDOS_SB(sb)->free_clusters;
1017 else {
1018 lock_fat(sb);
1019 if (MSDOS_SB(sb)->free_clusters != -1)
1020 free = MSDOS_SB(sb)->free_clusters;
1021 else {
1022 free = 0;
1023 for (nr = 2; nr < MSDOS_SB(sb)->clusters + 2; nr++)
1024 if (fat_access(sb, nr, -1) == FAT_ENT_FREE)
1025 free++;
1026 MSDOS_SB(sb)->free_clusters = free;
1028 unlock_fat(sb);
1031 buf->f_type = sb->s_magic;
1032 buf->f_bsize = MSDOS_SB(sb)->cluster_size;
1033 buf->f_blocks = MSDOS_SB(sb)->clusters;
1034 buf->f_bfree = free;
1035 buf->f_bavail = free;
1036 buf->f_namelen = MSDOS_SB(sb)->options.isvfat ? 260 : 12;
1038 return 0;
1041 static int is_exec(unsigned char *extension)
1043 unsigned char *exe_extensions = "EXECOMBAT", *walk;
1045 for (walk = exe_extensions; *walk; walk += 3)
1046 if (!strncmp(extension, walk, 3))
1047 return 1;
1048 return 0;
1051 static int fat_writepage(struct page *page, struct writeback_control *wbc)
1053 return block_write_full_page(page,fat_get_block, wbc);
1055 static int fat_readpage(struct file *file, struct page *page)
1057 return block_read_full_page(page,fat_get_block);
1060 static int
1061 fat_prepare_write(struct file *file, struct page *page,
1062 unsigned from, unsigned to)
1064 kmap(page);
1065 return cont_prepare_write(page,from,to,fat_get_block,
1066 &MSDOS_I(page->mapping->host)->mmu_private);
1069 static int
1070 fat_commit_write(struct file *file, struct page *page,
1071 unsigned from, unsigned to)
1073 kunmap(page);
1074 return generic_commit_write(file, page, from, to);
1077 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
1079 return generic_block_bmap(mapping,block,fat_get_block);
1081 static struct address_space_operations fat_aops = {
1082 .readpage = fat_readpage,
1083 .writepage = fat_writepage,
1084 .sync_page = block_sync_page,
1085 .prepare_write = fat_prepare_write,
1086 .commit_write = fat_commit_write,
1087 .bmap = _fat_bmap
1090 /* doesn't deal with root inode */
1091 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
1093 struct super_block *sb = inode->i_sb;
1094 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1095 int error;
1097 MSDOS_I(inode)->file_cluster = MSDOS_I(inode)->disk_cluster = 0;
1098 MSDOS_I(inode)->i_pos = 0;
1099 inode->i_uid = sbi->options.fs_uid;
1100 inode->i_gid = sbi->options.fs_gid;
1101 inode->i_version++;
1102 inode->i_generation = get_seconds();
1104 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
1105 inode->i_generation &= ~1;
1106 inode->i_mode = MSDOS_MKMODE(de->attr,
1107 S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
1108 inode->i_op = sbi->dir_ops;
1109 inode->i_fop = &fat_dir_operations;
1111 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1112 if (sbi->fat_bits == 32)
1113 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1115 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1116 error = fat_calc_dir_size(inode);
1117 if (error < 0)
1118 return error;
1119 MSDOS_I(inode)->mmu_private = inode->i_size;
1121 inode->i_nlink = fat_subdirs(inode);
1122 } else { /* not a directory */
1123 inode->i_generation |= 1;
1124 inode->i_mode = MSDOS_MKMODE(de->attr,
1125 ((sbi->options.showexec &&
1126 !is_exec(de->ext))
1127 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
1128 & ~sbi->options.fs_fmask) | S_IFREG;
1129 MSDOS_I(inode)->i_start = CF_LE_W(de->start);
1130 if (sbi->fat_bits == 32)
1131 MSDOS_I(inode)->i_start |= (CF_LE_W(de->starthi) << 16);
1133 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
1134 inode->i_size = CF_LE_L(de->size);
1135 inode->i_op = &fat_file_inode_operations;
1136 inode->i_fop = &fat_file_operations;
1137 inode->i_mapping->a_ops = &fat_aops;
1138 MSDOS_I(inode)->mmu_private = inode->i_size;
1140 if(de->attr & ATTR_SYS)
1141 if (sbi->options.sys_immutable)
1142 inode->i_flags |= S_IMMUTABLE;
1143 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
1144 /* this is as close to the truth as we can get ... */
1145 inode->i_blksize = sbi->cluster_size;
1146 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1147 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1148 inode->i_mtime.tv_sec = inode->i_atime.tv_sec =
1149 date_dos2unix(CF_LE_W(de->time),CF_LE_W(de->date));
1150 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0;
1151 inode->i_ctime.tv_sec =
1152 MSDOS_SB(sb)->options.isvfat
1153 ? date_dos2unix(CF_LE_W(de->ctime),CF_LE_W(de->cdate))
1154 : inode->i_mtime.tv_sec;
1155 inode->i_ctime.tv_nsec = de->ctime_ms * 1000000;
1156 MSDOS_I(inode)->i_ctime_ms = de->ctime_ms;
1158 return 0;
1161 void fat_write_inode(struct inode *inode, int wait)
1163 struct super_block *sb = inode->i_sb;
1164 struct buffer_head *bh;
1165 struct msdos_dir_entry *raw_entry;
1166 loff_t i_pos;
1168 retry:
1169 i_pos = MSDOS_I(inode)->i_pos;
1170 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) {
1171 return;
1173 lock_kernel();
1174 if (!(bh = sb_bread(sb, i_pos >> MSDOS_SB(sb)->dir_per_block_bits))) {
1175 fat_fs_panic(sb, "unable to read i-node block (i_pos %llu)",
1176 i_pos);
1177 unlock_kernel();
1178 return;
1180 spin_lock(&fat_inode_lock);
1181 if (i_pos != MSDOS_I(inode)->i_pos) {
1182 spin_unlock(&fat_inode_lock);
1183 brelse(bh);
1184 unlock_kernel();
1185 goto retry;
1188 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
1189 [i_pos & (MSDOS_SB(sb)->dir_per_block - 1)];
1190 if (S_ISDIR(inode->i_mode)) {
1191 raw_entry->attr = ATTR_DIR;
1192 raw_entry->size = 0;
1194 else {
1195 raw_entry->attr = ATTR_NONE;
1196 raw_entry->size = CT_LE_L(inode->i_size);
1198 raw_entry->attr |= MSDOS_MKATTR(inode->i_mode) |
1199 MSDOS_I(inode)->i_attrs;
1200 raw_entry->start = CT_LE_W(MSDOS_I(inode)->i_logstart);
1201 raw_entry->starthi = CT_LE_W(MSDOS_I(inode)->i_logstart >> 16);
1202 fat_date_unix2dos(inode->i_mtime.tv_sec,&raw_entry->time,&raw_entry->date);
1203 raw_entry->time = CT_LE_W(raw_entry->time);
1204 raw_entry->date = CT_LE_W(raw_entry->date);
1205 if (MSDOS_SB(sb)->options.isvfat) {
1206 fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
1207 raw_entry->ctime_ms = MSDOS_I(inode)->i_ctime_ms; /* use i_ctime.tv_nsec? */
1208 raw_entry->ctime = CT_LE_W(raw_entry->ctime);
1209 raw_entry->cdate = CT_LE_W(raw_entry->cdate);
1211 spin_unlock(&fat_inode_lock);
1212 mark_buffer_dirty(bh);
1213 brelse(bh);
1214 unlock_kernel();
1218 int fat_notify_change(struct dentry * dentry, struct iattr * attr)
1220 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
1221 struct inode *inode = dentry->d_inode;
1222 int mask, error = 0;
1224 lock_kernel();
1226 /* FAT cannot truncate to a longer file */
1227 if (attr->ia_valid & ATTR_SIZE) {
1228 if (attr->ia_size > inode->i_size) {
1229 error = -EPERM;
1230 goto out;
1234 error = inode_change_ok(inode, attr);
1235 if (error) {
1236 if (sbi->options.quiet)
1237 error = 0;
1238 goto out;
1241 if (((attr->ia_valid & ATTR_UID) &&
1242 (attr->ia_uid != sbi->options.fs_uid)) ||
1243 ((attr->ia_valid & ATTR_GID) &&
1244 (attr->ia_gid != sbi->options.fs_gid)) ||
1245 ((attr->ia_valid & ATTR_MODE) &&
1246 (attr->ia_mode & ~MSDOS_VALID_MODE)))
1247 error = -EPERM;
1249 if (error) {
1250 if (sbi->options.quiet)
1251 error = 0;
1252 goto out;
1254 error = inode_setattr(inode, attr);
1255 if (error)
1256 goto out;
1258 if (S_ISDIR(inode->i_mode))
1259 mask = sbi->options.fs_dmask;
1260 else
1261 mask = sbi->options.fs_fmask;
1262 inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask);
1263 out:
1264 unlock_kernel();
1265 return error;
1267 MODULE_LICENSE("GPL");