MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / fat / inode.c
blobebd105f94c58dbd36c07e96c1e9590ea7c3da8ee
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/init.h>
15 #include <linux/time.h>
16 #include <linux/slab.h>
17 #include <linux/smp_lock.h>
18 #include <linux/seq_file.h>
19 #include <linux/msdos_fs.h>
20 #include <linux/pagemap.h>
21 #include <linux/mpage.h>
22 #include <linux/buffer_head.h>
23 #include <linux/mount.h>
24 #include <linux/vfs.h>
25 #include <linux/parser.h>
26 #include <linux/uio.h>
27 #include <linux/writeback.h>
28 #include <asm/unaligned.h>
30 #ifndef CONFIG_FAT_DEFAULT_IOCHARSET
31 /* if user don't select VFAT, this is undefined. */
32 #define CONFIG_FAT_DEFAULT_IOCHARSET ""
33 #endif
35 static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
36 static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
39 static int fat_add_cluster(struct inode *inode)
41 int err, cluster;
43 err = fat_alloc_clusters(inode, &cluster, 1);
44 if (err)
45 return err;
46 /* FIXME: this cluster should be added after data of this
47 * cluster is writed */
48 err = fat_chain_add(inode, cluster, 1);
49 if (err)
50 fat_free_clusters(inode, cluster);
51 return err;
54 static inline int __fat_get_block(struct inode *inode, sector_t iblock,
55 unsigned long *max_blocks,
56 struct buffer_head *bh_result, int create)
58 struct super_block *sb = inode->i_sb;
59 struct msdos_sb_info *sbi = MSDOS_SB(sb);
60 unsigned long mapped_blocks;
61 sector_t phys;
62 int err, offset;
64 err = fat_bmap(inode, iblock, &phys, &mapped_blocks);
65 if (err)
66 return err;
67 if (phys) {
68 map_bh(bh_result, sb, phys);
69 *max_blocks = min(mapped_blocks, *max_blocks);
70 return 0;
72 if (!create)
73 return 0;
75 if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) {
76 fat_fs_panic(sb, "corrupted file size (i_pos %lld, %lld)",
77 MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private);
78 return -EIO;
81 offset = (unsigned long)iblock & (sbi->sec_per_clus - 1);
82 if (!offset) {
83 /* TODO: multiple cluster allocation would be desirable. */
84 err = fat_add_cluster(inode);
85 if (err)
86 return err;
88 /* available blocks on this cluster */
89 mapped_blocks = sbi->sec_per_clus - offset;
91 *max_blocks = min(mapped_blocks, *max_blocks);
92 MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits;
94 err = fat_bmap(inode, iblock, &phys, &mapped_blocks);
95 if (err)
96 return err;
98 BUG_ON(!phys);
99 BUG_ON(*max_blocks != mapped_blocks);
100 set_buffer_new(bh_result);
101 map_bh(bh_result, sb, phys);
103 return 0;
106 static int fat_get_block(struct inode *inode, sector_t iblock,
107 struct buffer_head *bh_result, int create)
109 struct super_block *sb = inode->i_sb;
110 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
111 int err;
113 err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create);
114 if (err)
115 return err;
116 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
117 return 0;
120 static int fat_writepage(struct page *page, struct writeback_control *wbc)
122 return block_write_full_page(page, fat_get_block, wbc);
125 static int fat_writepages(struct address_space *mapping,
126 struct writeback_control *wbc)
128 return mpage_writepages(mapping, wbc, fat_get_block);
131 static int fat_readpage(struct file *file, struct page *page)
133 return mpage_readpage(page, fat_get_block);
136 static int fat_readpages(struct file *file, struct address_space *mapping,
137 struct list_head *pages, unsigned nr_pages)
139 return mpage_readpages(mapping, pages, nr_pages, fat_get_block);
142 static int fat_prepare_write(struct file *file, struct page *page,
143 unsigned from, unsigned to)
145 #if 1 // add by Victor Yu. 03-07-2007
146 return cont_prepare_write(page, from, to, fat_get_block,
147 &MSDOS_I(page->u.xx.mapping->host)->mmu_private);
148 #else
149 return cont_prepare_write(page, from, to, fat_get_block,
150 &MSDOS_I(page->mapping->host)->mmu_private);
151 #endif
154 static int fat_commit_write(struct file *file, struct page *page,
155 unsigned from, unsigned to)
157 #if 1 // add by Victor Yu. 03-07-2007
158 struct inode *inode = page->u.xx.mapping->host;
159 #else
160 struct inode *inode = page->mapping->host;
161 #endif
162 int err = generic_commit_write(file, page, from, to);
163 if (!err && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) {
164 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
165 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
166 mark_inode_dirty(inode);
168 return err;
171 #ifdef CONFIG_DIRECTIO
172 static ssize_t fat_direct_IO(int rw, struct kiocb *iocb,
173 const struct iovec *iov,
174 loff_t offset, unsigned long nr_segs)
176 struct file *file = iocb->ki_filp;
177 struct inode *inode = file->f_mapping->host;
179 if (rw == WRITE) {
181 * FIXME: blockdev_direct_IO() doesn't use ->prepare_write(),
182 * so we need to update the ->mmu_private to block boundary.
184 * But we must fill the remaining area or hole by nul for
185 * updating ->mmu_private.
187 loff_t size = offset + iov_length(iov, nr_segs);
188 if (MSDOS_I(inode)->mmu_private < size)
189 return -EINVAL;
193 * FAT need to use the DIO_LOCKING for avoiding the race
194 * condition of fat_get_block() and ->truncate().
196 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
197 offset, nr_segs, fat_get_block, NULL);
199 #endif
201 static sector_t _fat_bmap(struct address_space *mapping, sector_t block)
203 return generic_block_bmap(mapping, block, fat_get_block);
206 static const struct address_space_operations fat_aops = {
207 .readpage = fat_readpage,
208 .readpages = fat_readpages,
209 .writepage = fat_writepage,
210 .writepages = fat_writepages,
211 .sync_page = block_sync_page,
212 .prepare_write = fat_prepare_write,
213 .commit_write = fat_commit_write,
214 #ifdef CONFIG_DIRECTIO
215 .direct_IO = fat_direct_IO,
216 #endif
217 .bmap = _fat_bmap
221 * New FAT inode stuff. We do the following:
222 * a) i_ino is constant and has nothing with on-disk location.
223 * b) FAT manages its own cache of directory entries.
224 * c) *This* cache is indexed by on-disk location.
225 * d) inode has an associated directory entry, all right, but
226 * it may be unhashed.
227 * e) currently entries are stored within struct inode. That should
228 * change.
229 * f) we deal with races in the following way:
230 * 1. readdir() and lookup() do FAT-dir-cache lookup.
231 * 2. rename() unhashes the F-d-c entry and rehashes it in
232 * a new place.
233 * 3. unlink() and rmdir() unhash F-d-c entry.
234 * 4. fat_write_inode() checks whether the thing is unhashed.
235 * If it is we silently return. If it isn't we do bread(),
236 * check if the location is still valid and retry if it
237 * isn't. Otherwise we do changes.
238 * 5. Spinlock is used to protect hash/unhash/location check/lookup
239 * 6. fat_clear_inode() unhashes the F-d-c entry.
240 * 7. lookup() and readdir() do igrab() if they find a F-d-c entry
241 * and consider negative result as cache miss.
244 static void fat_hash_init(struct super_block *sb)
246 struct msdos_sb_info *sbi = MSDOS_SB(sb);
247 int i;
249 spin_lock_init(&sbi->inode_hash_lock);
250 for (i = 0; i < FAT_HASH_SIZE; i++)
251 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
254 static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos)
256 unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb;
257 tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2);
258 return tmp & FAT_HASH_MASK;
261 void fat_attach(struct inode *inode, loff_t i_pos)
263 struct super_block *sb = inode->i_sb;
264 struct msdos_sb_info *sbi = MSDOS_SB(sb);
266 spin_lock(&sbi->inode_hash_lock);
267 MSDOS_I(inode)->i_pos = i_pos;
268 hlist_add_head(&MSDOS_I(inode)->i_fat_hash,
269 sbi->inode_hashtable + fat_hash(sb, i_pos));
270 spin_unlock(&sbi->inode_hash_lock);
273 EXPORT_SYMBOL_GPL(fat_attach);
275 void fat_detach(struct inode *inode)
277 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
278 spin_lock(&sbi->inode_hash_lock);
279 MSDOS_I(inode)->i_pos = 0;
280 hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
281 spin_unlock(&sbi->inode_hash_lock);
284 EXPORT_SYMBOL_GPL(fat_detach);
286 struct inode *fat_iget(struct super_block *sb, loff_t i_pos)
288 struct msdos_sb_info *sbi = MSDOS_SB(sb);
289 struct hlist_head *head = sbi->inode_hashtable + fat_hash(sb, i_pos);
290 struct hlist_node *_p;
291 struct msdos_inode_info *i;
292 struct inode *inode = NULL;
294 spin_lock(&sbi->inode_hash_lock);
295 hlist_for_each_entry(i, _p, head, i_fat_hash) {
296 BUG_ON(i->vfs_inode.i_sb != sb);
297 if (i->i_pos != i_pos)
298 continue;
299 inode = igrab(&i->vfs_inode);
300 if (inode)
301 break;
303 spin_unlock(&sbi->inode_hash_lock);
304 return inode;
307 static int is_exec(unsigned char *extension)
309 unsigned char *exe_extensions = "EXECOMBAT", *walk;
311 for (walk = exe_extensions; *walk; walk += 3)
312 if (!strncmp(extension, walk, 3))
313 return 1;
314 return 0;
317 static int fat_calc_dir_size(struct inode *inode)
319 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
320 int ret, fclus, dclus;
322 inode->i_size = 0;
323 if (MSDOS_I(inode)->i_start == 0)
324 return 0;
326 ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus);
327 if (ret < 0)
328 return ret;
329 inode->i_size = (fclus + 1) << sbi->cluster_bits;
331 return 0;
334 /* doesn't deal with root inode */
335 static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de)
337 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
338 int error;
340 MSDOS_I(inode)->i_pos = 0;
341 inode->i_uid = sbi->options.fs_uid;
342 inode->i_gid = sbi->options.fs_gid;
343 inode->i_version++;
344 inode->i_generation = get_seconds();
346 if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) {
347 inode->i_generation &= ~1;
348 inode->i_mode = MSDOS_MKMODE(de->attr,
349 S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
350 inode->i_op = sbi->dir_ops;
351 inode->i_fop = &fat_dir_operations;
353 MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
354 if (sbi->fat_bits == 32)
355 MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
357 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
358 error = fat_calc_dir_size(inode);
359 if (error < 0)
360 return error;
361 MSDOS_I(inode)->mmu_private = inode->i_size;
363 inode->i_nlink = fat_subdirs(inode);
364 } else { /* not a directory */
365 inode->i_generation |= 1;
366 inode->i_mode = MSDOS_MKMODE(de->attr,
367 ((sbi->options.showexec &&
368 !is_exec(de->ext))
369 ? S_IRUGO|S_IWUGO : S_IRWXUGO)
370 & ~sbi->options.fs_fmask) | S_IFREG;
371 MSDOS_I(inode)->i_start = le16_to_cpu(de->start);
372 if (sbi->fat_bits == 32)
373 MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16);
375 MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start;
376 inode->i_size = le32_to_cpu(de->size);
377 inode->i_op = &fat_file_inode_operations;
378 inode->i_fop = &fat_file_operations;
379 inode->i_mapping->a_ops = &fat_aops;
380 MSDOS_I(inode)->mmu_private = inode->i_size;
382 if (de->attr & ATTR_SYS) {
383 if (sbi->options.sys_immutable)
384 inode->i_flags |= S_IMMUTABLE;
386 MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED;
387 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
388 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
389 inode->i_mtime.tv_sec =
390 date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date));
391 inode->i_mtime.tv_nsec = 0;
392 if (sbi->options.isvfat) {
393 int secs = de->ctime_cs / 100;
394 int csecs = de->ctime_cs % 100;
395 inode->i_ctime.tv_sec =
396 date_dos2unix(le16_to_cpu(de->ctime),
397 le16_to_cpu(de->cdate)) + secs;
398 inode->i_ctime.tv_nsec = csecs * 10000000;
399 inode->i_atime.tv_sec =
400 date_dos2unix(0, le16_to_cpu(de->adate));
401 inode->i_atime.tv_nsec = 0;
402 } else
403 inode->i_ctime = inode->i_atime = inode->i_mtime;
405 return 0;
408 struct inode *fat_build_inode(struct super_block *sb,
409 struct msdos_dir_entry *de, loff_t i_pos)
411 struct inode *inode;
412 int err;
414 inode = fat_iget(sb, i_pos);
415 if (inode)
416 goto out;
417 inode = new_inode(sb);
418 if (!inode) {
419 inode = ERR_PTR(-ENOMEM);
420 goto out;
422 inode->i_ino = iunique(sb, MSDOS_ROOT_INO);
423 inode->i_version = 1;
424 err = fat_fill_inode(inode, de);
425 if (err) {
426 iput(inode);
427 inode = ERR_PTR(err);
428 goto out;
430 fat_attach(inode, i_pos);
431 insert_inode_hash(inode);
432 out:
433 return inode;
436 EXPORT_SYMBOL_GPL(fat_build_inode);
438 static void fat_delete_inode(struct inode *inode)
440 truncate_inode_pages(&inode->i_data, 0);
442 if (!is_bad_inode(inode)) {
443 inode->i_size = 0;
444 fat_truncate(inode);
446 clear_inode(inode);
449 static void fat_clear_inode(struct inode *inode)
451 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
453 if (is_bad_inode(inode))
454 return;
455 lock_kernel();
456 spin_lock(&sbi->inode_hash_lock);
457 fat_cache_inval_inode(inode);
458 hlist_del_init(&MSDOS_I(inode)->i_fat_hash);
459 spin_unlock(&sbi->inode_hash_lock);
460 unlock_kernel();
463 static void fat_write_super(struct super_block *sb)
465 sb->s_dirt = 0;
467 if (!(sb->s_flags & MS_RDONLY))
468 fat_clusters_flush(sb);
471 static void fat_put_super(struct super_block *sb)
473 struct msdos_sb_info *sbi = MSDOS_SB(sb);
475 if (sbi->nls_disk) {
476 unload_nls(sbi->nls_disk);
477 sbi->nls_disk = NULL;
478 sbi->options.codepage = fat_default_codepage;
480 if (sbi->nls_io) {
481 unload_nls(sbi->nls_io);
482 sbi->nls_io = NULL;
484 if (sbi->options.iocharset != fat_default_iocharset) {
485 kfree(sbi->options.iocharset);
486 sbi->options.iocharset = fat_default_iocharset;
489 sb->s_fs_info = NULL;
490 kfree(sbi);
493 static kmem_cache_t *fat_inode_cachep;
495 static struct inode *fat_alloc_inode(struct super_block *sb)
497 struct msdos_inode_info *ei;
498 ei = kmem_cache_alloc(fat_inode_cachep, SLAB_KERNEL);
499 if (!ei)
500 return NULL;
501 return &ei->vfs_inode;
504 static void fat_destroy_inode(struct inode *inode)
506 kmem_cache_free(fat_inode_cachep, MSDOS_I(inode));
509 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
511 struct msdos_inode_info *ei = (struct msdos_inode_info *)foo;
513 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
514 SLAB_CTOR_CONSTRUCTOR) {
515 spin_lock_init(&ei->cache_lru_lock);
516 ei->nr_caches = 0;
517 ei->cache_valid_id = FAT_CACHE_VALID + 1;
518 INIT_LIST_HEAD(&ei->cache_lru);
519 INIT_HLIST_NODE(&ei->i_fat_hash);
520 inode_init_once(&ei->vfs_inode);
524 static int __init fat_init_inodecache(void)
526 fat_inode_cachep = kmem_cache_create("fat_inode_cache",
527 sizeof(struct msdos_inode_info),
528 0, (SLAB_RECLAIM_ACCOUNT|
529 SLAB_MEM_SPREAD),
530 init_once, NULL);
531 if (fat_inode_cachep == NULL)
532 return -ENOMEM;
533 return 0;
536 static void __exit fat_destroy_inodecache(void)
538 kmem_cache_destroy(fat_inode_cachep);
541 static int fat_remount(struct super_block *sb, int *flags, char *data)
543 struct msdos_sb_info *sbi = MSDOS_SB(sb);
544 *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME);
545 return 0;
548 static int fat_statfs(struct dentry *dentry, struct kstatfs *buf)
550 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
552 /* If the count of free cluster is still unknown, counts it here. */
553 if (sbi->free_clusters == -1) {
554 int err = fat_count_free_clusters(dentry->d_sb);
555 if (err)
556 return err;
559 buf->f_type = dentry->d_sb->s_magic;
560 buf->f_bsize = sbi->cluster_size;
561 buf->f_blocks = sbi->max_cluster - FAT_START_ENT;
562 buf->f_bfree = sbi->free_clusters;
563 buf->f_bavail = sbi->free_clusters;
564 buf->f_namelen = sbi->options.isvfat ? 260 : 12;
566 return 0;
569 static int fat_write_inode(struct inode *inode, int wait)
571 struct super_block *sb = inode->i_sb;
572 struct msdos_sb_info *sbi = MSDOS_SB(sb);
573 struct buffer_head *bh;
574 struct msdos_dir_entry *raw_entry;
575 loff_t i_pos;
576 int err = 0;
578 retry:
579 i_pos = MSDOS_I(inode)->i_pos;
580 if (inode->i_ino == MSDOS_ROOT_INO || !i_pos)
581 return 0;
583 lock_kernel();
584 bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits);
585 if (!bh) {
586 printk(KERN_ERR "FAT: unable to read inode block "
587 "for updating (i_pos %lld)\n", i_pos);
588 err = -EIO;
589 goto out;
591 spin_lock(&sbi->inode_hash_lock);
592 if (i_pos != MSDOS_I(inode)->i_pos) {
593 spin_unlock(&sbi->inode_hash_lock);
594 brelse(bh);
595 unlock_kernel();
596 goto retry;
599 raw_entry = &((struct msdos_dir_entry *) (bh->b_data))
600 [i_pos & (sbi->dir_per_block - 1)];
601 if (S_ISDIR(inode->i_mode))
602 raw_entry->size = 0;
603 else
604 raw_entry->size = cpu_to_le32(inode->i_size);
605 raw_entry->attr = fat_attr(inode);
606 raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart);
607 raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16);
608 fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time, &raw_entry->date);
609 if (sbi->options.isvfat) {
610 __le16 atime;
611 fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate);
612 fat_date_unix2dos(inode->i_atime.tv_sec,&atime,&raw_entry->adate);
613 raw_entry->ctime_cs = (inode->i_ctime.tv_sec & 1) * 100 +
614 inode->i_ctime.tv_nsec / 10000000;
616 spin_unlock(&sbi->inode_hash_lock);
617 mark_buffer_dirty(bh);
618 if (wait)
619 err = sync_dirty_buffer(bh);
620 brelse(bh);
621 out:
622 unlock_kernel();
623 return err;
626 int fat_sync_inode(struct inode *inode)
628 return fat_write_inode(inode, 1);
631 EXPORT_SYMBOL_GPL(fat_sync_inode);
633 static int fat_show_options(struct seq_file *m, struct vfsmount *mnt);
634 static struct super_operations fat_sops = {
635 .alloc_inode = fat_alloc_inode,
636 .destroy_inode = fat_destroy_inode,
637 .write_inode = fat_write_inode,
638 .delete_inode = fat_delete_inode,
639 .put_super = fat_put_super,
640 .write_super = fat_write_super,
641 .statfs = fat_statfs,
642 .clear_inode = fat_clear_inode,
643 .remount_fs = fat_remount,
645 .read_inode = make_bad_inode,
647 .show_options = fat_show_options,
651 * a FAT file handle with fhtype 3 is
652 * 0/ i_ino - for fast, reliable lookup if still in the cache
653 * 1/ i_generation - to see if i_ino is still valid
654 * bit 0 == 0 iff directory
655 * 2/ i_pos(8-39) - if ino has changed, but still in cache
656 * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
657 * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
659 * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
660 * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
661 * of i_logstart is used to store the directory entry offset.
664 static struct dentry *
665 fat_decode_fh(struct super_block *sb, __u32 *fh, int len, int fhtype,
666 int (*acceptable)(void *context, struct dentry *de),
667 void *context)
669 if (fhtype != 3)
670 return ERR_PTR(-ESTALE);
671 if (len < 5)
672 return ERR_PTR(-ESTALE);
674 return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, context);
677 static struct dentry *fat_get_dentry(struct super_block *sb, void *inump)
679 struct inode *inode = NULL;
680 struct dentry *result;
681 __u32 *fh = inump;
683 inode = iget(sb, fh[0]);
684 if (!inode || is_bad_inode(inode) || inode->i_generation != fh[1]) {
685 if (inode)
686 iput(inode);
687 inode = NULL;
689 if (!inode) {
690 loff_t i_pos;
691 int i_logstart = fh[3] & 0x0fffffff;
693 i_pos = (loff_t)fh[2] << 8;
694 i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
696 /* try 2 - see if i_pos is in F-d-c
697 * require i_logstart to be the same
698 * Will fail if you truncate and then re-write
701 inode = fat_iget(sb, i_pos);
702 if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
703 iput(inode);
704 inode = NULL;
707 if (!inode) {
708 /* For now, do nothing
709 * What we could do is:
710 * follow the file starting at fh[4], and record
711 * the ".." entry, and the name of the fh[2] entry.
712 * The follow the ".." file finding the next step up.
713 * This way we build a path to the root of
714 * the tree. If this works, we lookup the path and so
715 * get this inode into the cache.
716 * Finally try the fat_iget lookup again
717 * If that fails, then weare totally out of luck
718 * But all that is for another day
721 if (!inode)
722 return ERR_PTR(-ESTALE);
725 /* now to find a dentry.
726 * If possible, get a well-connected one
728 result = d_alloc_anon(inode);
729 if (result == NULL) {
730 iput(inode);
731 return ERR_PTR(-ENOMEM);
733 result->d_op = sb->s_root->d_op;
734 return result;
737 static int
738 fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable)
740 int len = *lenp;
741 struct inode *inode = de->d_inode;
742 u32 ipos_h, ipos_m, ipos_l;
744 if (len < 5)
745 return 255; /* no room */
747 ipos_h = MSDOS_I(inode)->i_pos >> 8;
748 ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24;
749 ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28;
750 *lenp = 5;
751 fh[0] = inode->i_ino;
752 fh[1] = inode->i_generation;
753 fh[2] = ipos_h;
754 fh[3] = ipos_m | MSDOS_I(inode)->i_logstart;
755 spin_lock(&de->d_lock);
756 fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart;
757 spin_unlock(&de->d_lock);
758 return 3;
761 static struct dentry *fat_get_parent(struct dentry *child)
763 struct buffer_head *bh;
764 struct msdos_dir_entry *de;
765 loff_t i_pos;
766 struct dentry *parent;
767 struct inode *inode;
768 int err;
770 lock_kernel();
772 err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos);
773 if (err) {
774 parent = ERR_PTR(err);
775 goto out;
777 inode = fat_build_inode(child->d_sb, de, i_pos);
778 brelse(bh);
779 if (IS_ERR(inode)) {
780 parent = ERR_PTR(PTR_ERR(inode));
781 goto out;
783 parent = d_alloc_anon(inode);
784 if (!parent) {
785 iput(inode);
786 parent = ERR_PTR(-ENOMEM);
788 out:
789 unlock_kernel();
791 return parent;
794 static struct export_operations fat_export_ops = {
795 .decode_fh = fat_decode_fh,
796 .encode_fh = fat_encode_fh,
797 .get_dentry = fat_get_dentry,
798 .get_parent = fat_get_parent,
801 static int fat_show_options(struct seq_file *m, struct vfsmount *mnt)
803 struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb);
804 struct fat_mount_options *opts = &sbi->options;
805 int isvfat = opts->isvfat;
807 if (opts->fs_uid != 0)
808 seq_printf(m, ",uid=%u", opts->fs_uid);
809 if (opts->fs_gid != 0)
810 seq_printf(m, ",gid=%u", opts->fs_gid);
811 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
812 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
813 if (sbi->nls_disk)
814 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
815 if (isvfat) {
816 if (sbi->nls_io)
817 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
819 switch (opts->shortname) {
820 case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95:
821 seq_puts(m, ",shortname=win95");
822 break;
823 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT:
824 seq_puts(m, ",shortname=winnt");
825 break;
826 case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95:
827 seq_puts(m, ",shortname=mixed");
828 break;
829 case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95:
830 /* seq_puts(m, ",shortname=lower"); */
831 break;
832 default:
833 seq_puts(m, ",shortname=unknown");
834 break;
837 if (opts->name_check != 'n')
838 seq_printf(m, ",check=%c", opts->name_check);
839 if (opts->quiet)
840 seq_puts(m, ",quiet");
841 if (opts->showexec)
842 seq_puts(m, ",showexec");
843 if (opts->sys_immutable)
844 seq_puts(m, ",sys_immutable");
845 if (!isvfat) {
846 if (opts->dotsOK)
847 seq_puts(m, ",dotsOK=yes");
848 if (opts->nocase)
849 seq_puts(m, ",nocase");
850 } else {
851 if (opts->utf8)
852 seq_puts(m, ",utf8");
853 if (opts->unicode_xlate)
854 seq_puts(m, ",uni_xlate");
855 if (!opts->numtail)
856 seq_puts(m, ",nonumtail");
859 return 0;
862 enum {
863 Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid,
864 Opt_umask, Opt_dmask, Opt_fmask, Opt_codepage, Opt_nocase,
865 Opt_quiet, Opt_showexec, Opt_debug, Opt_immutable,
866 Opt_dots, Opt_nodots,
867 Opt_charset, Opt_shortname_lower, Opt_shortname_win95,
868 Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes,
869 Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes,
870 Opt_obsolate, Opt_flush, Opt_err,
873 static match_table_t fat_tokens = {
874 {Opt_check_r, "check=relaxed"},
875 {Opt_check_s, "check=strict"},
876 {Opt_check_n, "check=normal"},
877 {Opt_check_r, "check=r"},
878 {Opt_check_s, "check=s"},
879 {Opt_check_n, "check=n"},
880 {Opt_uid, "uid=%u"},
881 {Opt_gid, "gid=%u"},
882 {Opt_umask, "umask=%o"},
883 {Opt_dmask, "dmask=%o"},
884 {Opt_fmask, "fmask=%o"},
885 {Opt_codepage, "codepage=%u"},
886 {Opt_nocase, "nocase"},
887 {Opt_quiet, "quiet"},
888 {Opt_showexec, "showexec"},
889 {Opt_debug, "debug"},
890 {Opt_immutable, "sys_immutable"},
891 {Opt_obsolate, "conv=binary"},
892 {Opt_obsolate, "conv=text"},
893 {Opt_obsolate, "conv=auto"},
894 {Opt_obsolate, "conv=b"},
895 {Opt_obsolate, "conv=t"},
896 {Opt_obsolate, "conv=a"},
897 {Opt_obsolate, "fat=%u"},
898 {Opt_obsolate, "blocksize=%u"},
899 {Opt_obsolate, "cvf_format=%20s"},
900 {Opt_obsolate, "cvf_options=%100s"},
901 {Opt_obsolate, "posix"},
902 {Opt_flush, "flush"},
903 {Opt_err, NULL},
905 static match_table_t msdos_tokens = {
906 {Opt_nodots, "nodots"},
907 {Opt_nodots, "dotsOK=no"},
908 {Opt_dots, "dots"},
909 {Opt_dots, "dotsOK=yes"},
910 {Opt_err, NULL}
912 static match_table_t vfat_tokens = {
913 {Opt_charset, "iocharset=%s"},
914 {Opt_shortname_lower, "shortname=lower"},
915 {Opt_shortname_win95, "shortname=win95"},
916 {Opt_shortname_winnt, "shortname=winnt"},
917 {Opt_shortname_mixed, "shortname=mixed"},
918 {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */
919 {Opt_utf8_no, "utf8=no"},
920 {Opt_utf8_no, "utf8=false"},
921 {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */
922 {Opt_utf8_yes, "utf8=yes"},
923 {Opt_utf8_yes, "utf8=true"},
924 {Opt_utf8_yes, "utf8"},
925 {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */
926 {Opt_uni_xl_no, "uni_xlate=no"},
927 {Opt_uni_xl_no, "uni_xlate=false"},
928 {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */
929 {Opt_uni_xl_yes, "uni_xlate=yes"},
930 {Opt_uni_xl_yes, "uni_xlate=true"},
931 {Opt_uni_xl_yes, "uni_xlate"},
932 {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */
933 {Opt_nonumtail_no, "nonumtail=no"},
934 {Opt_nonumtail_no, "nonumtail=false"},
935 {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */
936 {Opt_nonumtail_yes, "nonumtail=yes"},
937 {Opt_nonumtail_yes, "nonumtail=true"},
938 {Opt_nonumtail_yes, "nonumtail"},
939 {Opt_err, NULL}
942 static int parse_options(char *options, int is_vfat, int silent, int *debug,
943 struct fat_mount_options *opts)
945 char *p;
946 substring_t args[MAX_OPT_ARGS];
947 int option;
948 char *iocharset;
950 opts->isvfat = is_vfat;
952 opts->fs_uid = current->uid;
953 opts->fs_gid = current->gid;
954 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
955 opts->codepage = fat_default_codepage;
956 opts->iocharset = fat_default_iocharset;
957 if (is_vfat)
958 opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95;
959 else
960 opts->shortname = 0;
961 opts->name_check = 'n';
962 opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0;
963 opts->utf8 = opts->unicode_xlate = 0;
964 opts->numtail = 1;
965 opts->nocase = 0;
966 *debug = 0;
968 if (!options)
969 return 0;
971 while ((p = strsep(&options, ",")) != NULL) {
972 int token;
973 if (!*p)
974 continue;
976 token = match_token(p, fat_tokens, args);
977 if (token == Opt_err) {
978 if (is_vfat)
979 token = match_token(p, vfat_tokens, args);
980 else
981 token = match_token(p, msdos_tokens, args);
983 switch (token) {
984 case Opt_check_s:
985 opts->name_check = 's';
986 break;
987 case Opt_check_r:
988 opts->name_check = 'r';
989 break;
990 case Opt_check_n:
991 opts->name_check = 'n';
992 break;
993 case Opt_nocase:
994 if (!is_vfat)
995 opts->nocase = 1;
996 else {
997 /* for backward compatibility */
998 opts->shortname = VFAT_SFN_DISPLAY_WIN95
999 | VFAT_SFN_CREATE_WIN95;
1001 break;
1002 case Opt_quiet:
1003 opts->quiet = 1;
1004 break;
1005 case Opt_showexec:
1006 opts->showexec = 1;
1007 break;
1008 case Opt_debug:
1009 *debug = 1;
1010 break;
1011 case Opt_immutable:
1012 opts->sys_immutable = 1;
1013 break;
1014 case Opt_uid:
1015 if (match_int(&args[0], &option))
1016 return 0;
1017 opts->fs_uid = option;
1018 break;
1019 case Opt_gid:
1020 if (match_int(&args[0], &option))
1021 return 0;
1022 opts->fs_gid = option;
1023 break;
1024 case Opt_umask:
1025 if (match_octal(&args[0], &option))
1026 return 0;
1027 opts->fs_fmask = opts->fs_dmask = option;
1028 break;
1029 case Opt_dmask:
1030 if (match_octal(&args[0], &option))
1031 return 0;
1032 opts->fs_dmask = option;
1033 break;
1034 case Opt_fmask:
1035 if (match_octal(&args[0], &option))
1036 return 0;
1037 opts->fs_fmask = option;
1038 break;
1039 case Opt_codepage:
1040 if (match_int(&args[0], &option))
1041 return 0;
1042 opts->codepage = option;
1043 break;
1044 case Opt_flush:
1045 opts->flush = 1;
1046 break;
1048 /* msdos specific */
1049 case Opt_dots:
1050 opts->dotsOK = 1;
1051 break;
1052 case Opt_nodots:
1053 opts->dotsOK = 0;
1054 break;
1056 /* vfat specific */
1057 case Opt_charset:
1058 if (opts->iocharset != fat_default_iocharset)
1059 kfree(opts->iocharset);
1060 iocharset = match_strdup(&args[0]);
1061 if (!iocharset)
1062 return -ENOMEM;
1063 opts->iocharset = iocharset;
1064 break;
1065 case Opt_shortname_lower:
1066 opts->shortname = VFAT_SFN_DISPLAY_LOWER
1067 | VFAT_SFN_CREATE_WIN95;
1068 break;
1069 case Opt_shortname_win95:
1070 opts->shortname = VFAT_SFN_DISPLAY_WIN95
1071 | VFAT_SFN_CREATE_WIN95;
1072 break;
1073 case Opt_shortname_winnt:
1074 opts->shortname = VFAT_SFN_DISPLAY_WINNT
1075 | VFAT_SFN_CREATE_WINNT;
1076 break;
1077 case Opt_shortname_mixed:
1078 opts->shortname = VFAT_SFN_DISPLAY_WINNT
1079 | VFAT_SFN_CREATE_WIN95;
1080 break;
1081 case Opt_utf8_no: /* 0 or no or false */
1082 opts->utf8 = 0;
1083 break;
1084 case Opt_utf8_yes: /* empty or 1 or yes or true */
1085 opts->utf8 = 1;
1086 break;
1087 case Opt_uni_xl_no: /* 0 or no or false */
1088 opts->unicode_xlate = 0;
1089 break;
1090 case Opt_uni_xl_yes: /* empty or 1 or yes or true */
1091 opts->unicode_xlate = 1;
1092 break;
1093 case Opt_nonumtail_no: /* 0 or no or false */
1094 opts->numtail = 1; /* negated option */
1095 break;
1096 case Opt_nonumtail_yes: /* empty or 1 or yes or true */
1097 opts->numtail = 0; /* negated option */
1098 break;
1100 /* obsolete mount options */
1101 case Opt_obsolate:
1102 printk(KERN_INFO "FAT: \"%s\" option is obsolete, "
1103 "not supported now\n", p);
1104 break;
1105 /* unknown option */
1106 default:
1107 if (!silent) {
1108 printk(KERN_ERR
1109 "FAT: Unrecognized mount option \"%s\" "
1110 "or missing value\n", p);
1112 return -EINVAL;
1115 /* UTF-8 doesn't provide FAT semantics */
1116 if (!strcmp(opts->iocharset, "utf8")) {
1117 printk(KERN_ERR "FAT: utf8 is not a recommended IO charset"
1118 " for FAT filesystems, filesystem will be case sensitive!\n");
1121 if (opts->unicode_xlate)
1122 opts->utf8 = 0;
1124 return 0;
1127 static int fat_read_root(struct inode *inode)
1129 struct super_block *sb = inode->i_sb;
1130 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1131 int error;
1133 MSDOS_I(inode)->i_pos = 0;
1134 inode->i_uid = sbi->options.fs_uid;
1135 inode->i_gid = sbi->options.fs_gid;
1136 inode->i_version++;
1137 inode->i_generation = 0;
1138 inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR;
1139 inode->i_op = sbi->dir_ops;
1140 inode->i_fop = &fat_dir_operations;
1141 if (sbi->fat_bits == 32) {
1142 MSDOS_I(inode)->i_start = sbi->root_cluster;
1143 error = fat_calc_dir_size(inode);
1144 if (error < 0)
1145 return error;
1146 } else {
1147 MSDOS_I(inode)->i_start = 0;
1148 inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry);
1150 inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1))
1151 & ~((loff_t)sbi->cluster_size - 1)) >> 9;
1152 MSDOS_I(inode)->i_logstart = 0;
1153 MSDOS_I(inode)->mmu_private = inode->i_size;
1155 MSDOS_I(inode)->i_attrs = ATTR_NONE;
1156 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0;
1157 inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1158 inode->i_nlink = fat_subdirs(inode)+2;
1160 return 0;
1164 * Read the super block of an MS-DOS FS.
1166 int fat_fill_super(struct super_block *sb, void *data, int silent,
1167 struct inode_operations *fs_dir_inode_ops, int isvfat)
1169 struct inode *root_inode = NULL;
1170 struct buffer_head *bh;
1171 struct fat_boot_sector *b;
1172 struct msdos_sb_info *sbi;
1173 u16 logical_sector_size;
1174 u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors;
1175 int debug;
1176 unsigned int media;
1177 long error;
1178 char buf[50];
1180 sbi = kzalloc(sizeof(struct msdos_sb_info), GFP_KERNEL);
1181 if (!sbi)
1182 return -ENOMEM;
1183 sb->s_fs_info = sbi;
1185 sb->s_flags |= MS_NODIRATIME;
1186 sb->s_magic = MSDOS_SUPER_MAGIC;
1187 sb->s_op = &fat_sops;
1188 sb->s_export_op = &fat_export_ops;
1189 sbi->dir_ops = fs_dir_inode_ops;
1191 error = parse_options(data, isvfat, silent, &debug, &sbi->options);
1192 if (error)
1193 goto out_fail;
1195 error = -EIO;
1196 sb_min_blocksize(sb, 512);
1197 bh = sb_bread(sb, 0);
1198 if (bh == NULL) {
1199 printk(KERN_ERR "FAT: unable to read boot sector\n");
1200 goto out_fail;
1203 b = (struct fat_boot_sector *) bh->b_data;
1204 if (!b->reserved) {
1205 if (!silent)
1206 printk(KERN_ERR "FAT: bogus number of reserved sectors\n");
1207 brelse(bh);
1208 goto out_invalid;
1210 if (!b->fats) {
1211 if (!silent)
1212 printk(KERN_ERR "FAT: bogus number of FAT structure\n");
1213 brelse(bh);
1214 goto out_invalid;
1218 * Earlier we checked here that b->secs_track and b->head are nonzero,
1219 * but it turns out valid FAT filesystems can have zero there.
1222 media = b->media;
1223 if (!FAT_VALID_MEDIA(media)) {
1224 if (!silent)
1225 printk(KERN_ERR "FAT: invalid media value (0x%02x)\n",
1226 media);
1227 brelse(bh);
1228 goto out_invalid;
1230 logical_sector_size =
1231 le16_to_cpu(get_unaligned((__le16 *)&b->sector_size));
1232 if (!logical_sector_size
1233 || (logical_sector_size & (logical_sector_size - 1))
1234 || (logical_sector_size < 512)
1235 || (PAGE_CACHE_SIZE < logical_sector_size)) {
1236 if (!silent)
1237 printk(KERN_ERR "FAT: bogus logical sector size %u\n",
1238 logical_sector_size);
1239 brelse(bh);
1240 goto out_invalid;
1242 sbi->sec_per_clus = b->sec_per_clus;
1243 if (!sbi->sec_per_clus
1244 || (sbi->sec_per_clus & (sbi->sec_per_clus - 1))) {
1245 if (!silent)
1246 printk(KERN_ERR "FAT: bogus sectors per cluster %u\n",
1247 sbi->sec_per_clus);
1248 brelse(bh);
1249 goto out_invalid;
1252 if (logical_sector_size < sb->s_blocksize) {
1253 printk(KERN_ERR "FAT: logical sector size too small for device"
1254 " (logical sector size = %u)\n", logical_sector_size);
1255 brelse(bh);
1256 goto out_fail;
1258 if (logical_sector_size > sb->s_blocksize) {
1259 brelse(bh);
1261 if (!sb_set_blocksize(sb, logical_sector_size)) {
1262 printk(KERN_ERR "FAT: unable to set blocksize %u\n",
1263 logical_sector_size);
1264 goto out_fail;
1266 bh = sb_bread(sb, 0);
1267 if (bh == NULL) {
1268 printk(KERN_ERR "FAT: unable to read boot sector"
1269 " (logical sector size = %lu)\n",
1270 sb->s_blocksize);
1271 goto out_fail;
1273 b = (struct fat_boot_sector *) bh->b_data;
1276 sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus;
1277 sbi->cluster_bits = ffs(sbi->cluster_size) - 1;
1278 sbi->fats = b->fats;
1279 sbi->fat_bits = 0; /* Don't know yet */
1280 sbi->fat_start = le16_to_cpu(b->reserved);
1281 sbi->fat_length = le16_to_cpu(b->fat_length);
1282 sbi->root_cluster = 0;
1283 sbi->free_clusters = -1; /* Don't know yet */
1284 sbi->prev_free = FAT_START_ENT;
1286 if (!sbi->fat_length && b->fat32_length) {
1287 struct fat_boot_fsinfo *fsinfo;
1288 struct buffer_head *fsinfo_bh;
1290 /* Must be FAT32 */
1291 sbi->fat_bits = 32;
1292 sbi->fat_length = le32_to_cpu(b->fat32_length);
1293 sbi->root_cluster = le32_to_cpu(b->root_cluster);
1295 sb->s_maxbytes = 0xffffffff;
1297 /* MC - if info_sector is 0, don't multiply by 0 */
1298 sbi->fsinfo_sector = le16_to_cpu(b->info_sector);
1299 if (sbi->fsinfo_sector == 0)
1300 sbi->fsinfo_sector = 1;
1302 fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector);
1303 if (fsinfo_bh == NULL) {
1304 printk(KERN_ERR "FAT: bread failed, FSINFO block"
1305 " (sector = %lu)\n", sbi->fsinfo_sector);
1306 brelse(bh);
1307 goto out_fail;
1310 fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data;
1311 if (!IS_FSINFO(fsinfo)) {
1312 printk(KERN_WARNING
1313 "FAT: Did not find valid FSINFO signature.\n"
1314 " Found signature1 0x%08x signature2 0x%08x"
1315 " (sector = %lu)\n",
1316 le32_to_cpu(fsinfo->signature1),
1317 le32_to_cpu(fsinfo->signature2),
1318 sbi->fsinfo_sector);
1319 } else {
1320 sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters);
1321 sbi->prev_free = le32_to_cpu(fsinfo->next_cluster);
1324 brelse(fsinfo_bh);
1327 sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry);
1328 sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1;
1330 sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length;
1331 sbi->dir_entries =
1332 le16_to_cpu(get_unaligned((__le16 *)&b->dir_entries));
1333 if (sbi->dir_entries & (sbi->dir_per_block - 1)) {
1334 if (!silent)
1335 printk(KERN_ERR "FAT: bogus directroy-entries per block"
1336 " (%u)\n", sbi->dir_entries);
1337 brelse(bh);
1338 goto out_invalid;
1341 rootdir_sectors = sbi->dir_entries
1342 * sizeof(struct msdos_dir_entry) / sb->s_blocksize;
1343 sbi->data_start = sbi->dir_start + rootdir_sectors;
1344 total_sectors = le16_to_cpu(get_unaligned((__le16 *)&b->sectors));
1345 if (total_sectors == 0)
1346 total_sectors = le32_to_cpu(b->total_sect);
1348 total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus;
1350 if (sbi->fat_bits != 32)
1351 sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12;
1353 /* check that FAT table does not overflow */
1354 fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits;
1355 total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT);
1356 if (total_clusters > MAX_FAT(sb)) {
1357 if (!silent)
1358 printk(KERN_ERR "FAT: count of clusters too big (%u)\n",
1359 total_clusters);
1360 brelse(bh);
1361 goto out_invalid;
1364 sbi->max_cluster = total_clusters + FAT_START_ENT;
1365 /* check the free_clusters, it's not necessarily correct */
1366 if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters)
1367 sbi->free_clusters = -1;
1368 /* check the prev_free, it's not necessarily correct */
1369 sbi->prev_free %= sbi->max_cluster;
1370 if (sbi->prev_free < FAT_START_ENT)
1371 sbi->prev_free = FAT_START_ENT;
1373 brelse(bh);
1375 /* set up enough so that it can read an inode */
1376 fat_hash_init(sb);
1377 fat_ent_access_init(sb);
1380 * The low byte of FAT's first entry must have same value with
1381 * media-field. But in real world, too many devices is
1382 * writing wrong value. So, removed that validity check.
1384 * if (FAT_FIRST_ENT(sb, media) != first)
1387 error = -EINVAL;
1388 sprintf(buf, "cp%d", sbi->options.codepage);
1389 sbi->nls_disk = load_nls(buf);
1390 if (!sbi->nls_disk) {
1391 printk(KERN_ERR "FAT: codepage %s not found\n", buf);
1392 goto out_fail;
1395 /* FIXME: utf8 is using iocharset for upper/lower conversion */
1396 if (sbi->options.isvfat) {
1397 sbi->nls_io = load_nls(sbi->options.iocharset);
1398 if (!sbi->nls_io) {
1399 printk(KERN_ERR "FAT: IO charset %s not found\n",
1400 sbi->options.iocharset);
1401 goto out_fail;
1405 error = -ENOMEM;
1406 root_inode = new_inode(sb);
1407 if (!root_inode)
1408 goto out_fail;
1409 root_inode->i_ino = MSDOS_ROOT_INO;
1410 root_inode->i_version = 1;
1411 error = fat_read_root(root_inode);
1412 if (error < 0)
1413 goto out_fail;
1414 error = -ENOMEM;
1415 insert_inode_hash(root_inode);
1416 sb->s_root = d_alloc_root(root_inode);
1417 if (!sb->s_root) {
1418 printk(KERN_ERR "FAT: get root inode failed\n");
1419 goto out_fail;
1422 return 0;
1424 out_invalid:
1425 error = -EINVAL;
1426 if (!silent)
1427 printk(KERN_INFO "VFS: Can't find a valid FAT filesystem"
1428 " on dev %s.\n", sb->s_id);
1430 out_fail:
1431 if (root_inode)
1432 iput(root_inode);
1433 if (sbi->nls_io)
1434 unload_nls(sbi->nls_io);
1435 if (sbi->nls_disk)
1436 unload_nls(sbi->nls_disk);
1437 if (sbi->options.iocharset != fat_default_iocharset)
1438 kfree(sbi->options.iocharset);
1439 sb->s_fs_info = NULL;
1440 kfree(sbi);
1441 return error;
1444 EXPORT_SYMBOL_GPL(fat_fill_super);
1447 * helper function for fat_flush_inodes. This writes both the inode
1448 * and the file data blocks, waiting for in flight data blocks before
1449 * the start of the call. It does not wait for any io started
1450 * during the call
1452 static int writeback_inode(struct inode *inode)
1455 int ret;
1456 struct address_space *mapping = inode->i_mapping;
1457 struct writeback_control wbc = {
1458 .sync_mode = WB_SYNC_NONE,
1459 .nr_to_write = 0,
1461 /* if we used WB_SYNC_ALL, sync_inode waits for the io for the
1462 * inode to finish. So WB_SYNC_NONE is sent down to sync_inode
1463 * and filemap_fdatawrite is used for the data blocks
1465 ret = sync_inode(inode, &wbc);
1466 if (!ret)
1467 ret = filemap_fdatawrite(mapping);
1468 return ret;
1472 * write data and metadata corresponding to i1 and i2. The io is
1473 * started but we do not wait for any of it to finish.
1475 * filemap_flush is used for the block device, so if there is a dirty
1476 * page for a block already in flight, we will not wait and start the
1477 * io over again
1479 int fat_flush_inodes(struct super_block *sb, struct inode *i1, struct inode *i2)
1481 int ret = 0;
1482 if (!MSDOS_SB(sb)->options.flush)
1483 return 0;
1484 if (i1)
1485 ret = writeback_inode(i1);
1486 if (!ret && i2)
1487 ret = writeback_inode(i2);
1488 if (!ret) {
1489 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
1490 ret = filemap_flush(mapping);
1492 return ret;
1494 EXPORT_SYMBOL_GPL(fat_flush_inodes);
1496 static int __init init_fat_fs(void)
1498 int err;
1500 err = fat_cache_init();
1501 if (err)
1502 return err;
1504 err = fat_init_inodecache();
1505 if (err)
1506 goto failed;
1508 return 0;
1510 failed:
1511 fat_cache_destroy();
1512 return err;
1515 static void __exit exit_fat_fs(void)
1517 fat_cache_destroy();
1518 fat_destroy_inodecache();
1521 module_init(init_fat_fs)
1522 module_exit(exit_fat_fs)
1524 MODULE_LICENSE("GPL");