diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 15:20:36 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 15:20:36 -0700 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/fat | |
download | linux-1da177e4c3f41524e886b7f1b8a0c1fc7321cac2.tar.gz linux-1da177e4c3f41524e886b7f1b8a0c1fc7321cac2.tar.bz2 linux-1da177e4c3f41524e886b7f1b8a0c1fc7321cac2.zip |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'fs/fat')
-rw-r--r-- | fs/fat/Makefile | 7 | ||||
-rw-r--r-- | fs/fat/cache.c | 324 | ||||
-rw-r--r-- | fs/fat/dir.c | 1271 | ||||
-rw-r--r-- | fs/fat/fatent.c | 612 | ||||
-rw-r--r-- | fs/fat/file.c | 308 | ||||
-rw-r--r-- | fs/fat/inode.c | 1351 | ||||
-rw-r--r-- | fs/fat/misc.c | 225 |
7 files changed, 4098 insertions, 0 deletions
diff --git a/fs/fat/Makefile b/fs/fat/Makefile new file mode 100644 index 000000000000..bfb5f06cf2c8 --- /dev/null +++ b/fs/fat/Makefile @@ -0,0 +1,7 @@ +# +# Makefile for the Linux fat filesystem support. +# + +obj-$(CONFIG_FAT_FS) += fat.o + +fat-objs := cache.o dir.o fatent.o file.o inode.o misc.o diff --git a/fs/fat/cache.c b/fs/fat/cache.c new file mode 100644 index 000000000000..7c52e465a619 --- /dev/null +++ b/fs/fat/cache.c @@ -0,0 +1,324 @@ +/* + * linux/fs/fat/cache.c + * + * Written 1992,1993 by Werner Almesberger + * + * Mar 1999. AV. Changed cache, so that it uses the starting cluster instead + * of inode number. + * May 1999. AV. Fixed the bogosity with FAT32 (read "FAT28"). Fscking lusers. + */ + +#include <linux/fs.h> +#include <linux/msdos_fs.h> +#include <linux/buffer_head.h> + +/* this must be > 0. */ +#define FAT_MAX_CACHE 8 + +struct fat_cache { + struct list_head cache_list; + int nr_contig; /* number of contiguous clusters */ + int fcluster; /* cluster number in the file. */ + int dcluster; /* cluster number on disk. */ +}; + +struct fat_cache_id { + unsigned int id; + int nr_contig; + int fcluster; + int dcluster; +}; + +static inline int fat_max_cache(struct inode *inode) +{ + return FAT_MAX_CACHE; +} + +static kmem_cache_t *fat_cache_cachep; + +static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags) +{ + struct fat_cache *cache = (struct fat_cache *)foo; + + if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == + SLAB_CTOR_CONSTRUCTOR) + INIT_LIST_HEAD(&cache->cache_list); +} + +int __init fat_cache_init(void) +{ + fat_cache_cachep = kmem_cache_create("fat_cache", + sizeof(struct fat_cache), + 0, SLAB_RECLAIM_ACCOUNT, + init_once, NULL); + if (fat_cache_cachep == NULL) + return -ENOMEM; + return 0; +} + +void __exit fat_cache_destroy(void) +{ + if (kmem_cache_destroy(fat_cache_cachep)) + printk(KERN_INFO "fat_cache: not all structures were freed\n"); +} + +static inline struct fat_cache *fat_cache_alloc(struct inode *inode) +{ + return kmem_cache_alloc(fat_cache_cachep, SLAB_KERNEL); +} + +static inline void fat_cache_free(struct fat_cache *cache) +{ + BUG_ON(!list_empty(&cache->cache_list)); + kmem_cache_free(fat_cache_cachep, cache); +} + +static inline void fat_cache_update_lru(struct inode *inode, + struct fat_cache *cache) +{ + if (MSDOS_I(inode)->cache_lru.next != &cache->cache_list) + list_move(&cache->cache_list, &MSDOS_I(inode)->cache_lru); +} + +static int fat_cache_lookup(struct inode *inode, int fclus, + struct fat_cache_id *cid, + int *cached_fclus, int *cached_dclus) +{ + static struct fat_cache nohit = { .fcluster = 0, }; + + struct fat_cache *hit = &nohit, *p; + int offset = -1; + + spin_lock(&MSDOS_I(inode)->cache_lru_lock); + list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) { + /* Find the cache of "fclus" or nearest cache. */ + if (p->fcluster <= fclus && hit->fcluster < p->fcluster) { + hit = p; + if ((hit->fcluster + hit->nr_contig) < fclus) { + offset = hit->nr_contig; + } else { + offset = fclus - hit->fcluster; + break; + } + } + } + if (hit != &nohit) { + fat_cache_update_lru(inode, hit); + + cid->id = MSDOS_I(inode)->cache_valid_id; + cid->nr_contig = hit->nr_contig; + cid->fcluster = hit->fcluster; + cid->dcluster = hit->dcluster; + *cached_fclus = cid->fcluster + offset; + *cached_dclus = cid->dcluster + offset; + } + spin_unlock(&MSDOS_I(inode)->cache_lru_lock); + + return offset; +} + +static struct fat_cache *fat_cache_merge(struct inode *inode, + struct fat_cache_id *new) +{ + struct fat_cache *p; + + list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) { + /* Find the same part as "new" in cluster-chain. */ + if (p->fcluster == new->fcluster) { + BUG_ON(p->dcluster != new->dcluster); + if (new->nr_contig > p->nr_contig) + p->nr_contig = new->nr_contig; + return p; + } + } + return NULL; +} + +static void fat_cache_add(struct inode *inode, struct fat_cache_id *new) +{ + struct fat_cache *cache, *tmp; + + if (new->fcluster == -1) /* dummy cache */ + return; + + spin_lock(&MSDOS_I(inode)->cache_lru_lock); + if (new->id != FAT_CACHE_VALID && + new->id != MSDOS_I(inode)->cache_valid_id) + goto out; /* this cache was invalidated */ + + cache = fat_cache_merge(inode, new); + if (cache == NULL) { + if (MSDOS_I(inode)->nr_caches < fat_max_cache(inode)) { + MSDOS_I(inode)->nr_caches++; + spin_unlock(&MSDOS_I(inode)->cache_lru_lock); + + tmp = fat_cache_alloc(inode); + spin_lock(&MSDOS_I(inode)->cache_lru_lock); + cache = fat_cache_merge(inode, new); + if (cache != NULL) { + MSDOS_I(inode)->nr_caches--; + fat_cache_free(tmp); + goto out_update_lru; + } + cache = tmp; + } else { + struct list_head *p = MSDOS_I(inode)->cache_lru.prev; + cache = list_entry(p, struct fat_cache, cache_list); + } + cache->fcluster = new->fcluster; + cache->dcluster = new->dcluster; + cache->nr_contig = new->nr_contig; + } +out_update_lru: + fat_cache_update_lru(inode, cache); +out: + spin_unlock(&MSDOS_I(inode)->cache_lru_lock); +} + +/* + * Cache invalidation occurs rarely, thus the LRU chain is not updated. It + * fixes itself after a while. + */ +static void __fat_cache_inval_inode(struct inode *inode) +{ + struct msdos_inode_info *i = MSDOS_I(inode); + struct fat_cache *cache; + + while (!list_empty(&i->cache_lru)) { + cache = list_entry(i->cache_lru.next, struct fat_cache, cache_list); + list_del_init(&cache->cache_list); + i->nr_caches--; + fat_cache_free(cache); + } + /* Update. The copy of caches before this id is discarded. */ + i->cache_valid_id++; + if (i->cache_valid_id == FAT_CACHE_VALID) + i->cache_valid_id++; +} + +void fat_cache_inval_inode(struct inode *inode) +{ + spin_lock(&MSDOS_I(inode)->cache_lru_lock); + __fat_cache_inval_inode(inode); + spin_unlock(&MSDOS_I(inode)->cache_lru_lock); +} + +static inline int cache_contiguous(struct fat_cache_id *cid, int dclus) +{ + cid->nr_contig++; + return ((cid->dcluster + cid->nr_contig) == dclus); +} + +static inline void cache_init(struct fat_cache_id *cid, int fclus, int dclus) +{ + cid->id = FAT_CACHE_VALID; + cid->fcluster = fclus; + cid->dcluster = dclus; + cid->nr_contig = 0; +} + +int fat_get_cluster(struct inode *inode, int cluster, int *fclus, int *dclus) +{ + struct super_block *sb = inode->i_sb; + const int limit = sb->s_maxbytes >> MSDOS_SB(sb)->cluster_bits; + struct fat_entry fatent; + struct fat_cache_id cid; + int nr; + + BUG_ON(MSDOS_I(inode)->i_start == 0); + + *fclus = 0; + *dclus = MSDOS_I(inode)->i_start; + if (cluster == 0) + return 0; + + if (fat_cache_lookup(inode, cluster, &cid, fclus, dclus) < 0) { + /* + * dummy, always not contiguous + * This is reinitialized by cache_init(), later. + */ + cache_init(&cid, -1, -1); + } + + fatent_init(&fatent); + while (*fclus < cluster) { + /* prevent the infinite loop of cluster chain */ + if (*fclus > limit) { + fat_fs_panic(sb, "%s: detected the cluster chain loop" + " (i_pos %lld)", __FUNCTION__, + MSDOS_I(inode)->i_pos); + nr = -EIO; + goto out; + } + + nr = fat_ent_read(inode, &fatent, *dclus); + if (nr < 0) + goto out; + else if (nr == FAT_ENT_FREE) { + fat_fs_panic(sb, "%s: invalid cluster chain" + " (i_pos %lld)", __FUNCTION__, + MSDOS_I(inode)->i_pos); + nr = -EIO; + goto out; + } else if (nr == FAT_ENT_EOF) { + fat_cache_add(inode, &cid); + goto out; + } + (*fclus)++; + *dclus = nr; + if (!cache_contiguous(&cid, *dclus)) + cache_init(&cid, *fclus, *dclus); + } + nr = 0; + fat_cache_add(inode, &cid); +out: + fatent_brelse(&fatent); + return nr; +} + +static int fat_bmap_cluster(struct inode *inode, int cluster) +{ + struct super_block *sb = inode->i_sb; + int ret, fclus, dclus; + + if (MSDOS_I(inode)->i_start == 0) + return 0; + + ret = fat_get_cluster(inode, cluster, &fclus, &dclus); + if (ret < 0) + return ret; + else if (ret == FAT_ENT_EOF) { + fat_fs_panic(sb, "%s: request beyond EOF (i_pos %lld)", + __FUNCTION__, MSDOS_I(inode)->i_pos); + return -EIO; + } + return dclus; +} + +int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + sector_t last_block; + int cluster, offset; + + *phys = 0; + if ((sbi->fat_bits != 32) && (inode->i_ino == MSDOS_ROOT_INO)) { + if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) + *phys = sector + sbi->dir_start; + return 0; + } + last_block = (MSDOS_I(inode)->mmu_private + (sb->s_blocksize - 1)) + >> sb->s_blocksize_bits; + if (sector >= last_block) + return 0; + + cluster = sector >> (sbi->cluster_bits - sb->s_blocksize_bits); + offset = sector & (sbi->sec_per_clus - 1); + cluster = fat_bmap_cluster(inode, cluster); + if (cluster < 0) + return cluster; + else if (cluster) + *phys = fat_clus_to_blknr(sbi, cluster) + offset; + return 0; +} diff --git a/fs/fat/dir.c b/fs/fat/dir.c new file mode 100644 index 000000000000..e5ae1b720dde --- /dev/null +++ b/fs/fat/dir.c @@ -0,0 +1,1271 @@ +/* + * linux/fs/fat/dir.c + * + * directory handling functions for fat-based filesystems + * + * Written 1992,1993 by Werner Almesberger + * + * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu> + * + * VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> + * Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk> + * Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV + * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de> + */ + +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/time.h> +#include <linux/msdos_fs.h> +#include <linux/dirent.h> +#include <linux/smp_lock.h> +#include <linux/buffer_head.h> +#include <asm/uaccess.h> + +static inline loff_t fat_make_i_pos(struct super_block *sb, + struct buffer_head *bh, + struct msdos_dir_entry *de) +{ + return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits) + | (de - (struct msdos_dir_entry *)bh->b_data); +} + +/* Returns the inode number of the directory entry at offset pos. If bh is + non-NULL, it is brelse'd before. Pos is incremented. The buffer header is + returned in bh. + AV. Most often we do it item-by-item. Makes sense to optimize. + AV. OK, there we go: if both bh and de are non-NULL we assume that we just + AV. want the next entry (took one explicit de=NULL in vfat/namei.c). + AV. It's done in fat_get_entry() (inlined), here the slow case lives. + AV. Additionally, when we return -1 (i.e. reached the end of directory) + AV. we make bh NULL. + */ +static int fat__get_entry(struct inode *dir, loff_t *pos, + struct buffer_head **bh, struct msdos_dir_entry **de) +{ + struct super_block *sb = dir->i_sb; + sector_t phys, iblock; + int offset; + int err; + +next: + if (*bh) + brelse(*bh); + + *bh = NULL; + iblock = *pos >> sb->s_blocksize_bits; + err = fat_bmap(dir, iblock, &phys); + if (err || !phys) + return -1; /* beyond EOF or error */ + + *bh = sb_bread(sb, phys); + if (*bh == NULL) { + printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n", + (unsigned long long)phys); + /* skip this block */ + *pos = (iblock + 1) << sb->s_blocksize_bits; + goto next; + } + + offset = *pos & (sb->s_blocksize - 1); + *pos += sizeof(struct msdos_dir_entry); + *de = (struct msdos_dir_entry *)((*bh)->b_data + offset); + + return 0; +} + +static inline int fat_get_entry(struct inode *dir, loff_t *pos, + struct buffer_head **bh, + struct msdos_dir_entry **de) +{ + /* Fast stuff first */ + if (*bh && *de && + (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) { + *pos += sizeof(struct msdos_dir_entry); + (*de)++; + return 0; + } + return fat__get_entry(dir, pos, bh, de); +} + +/* + * Convert Unicode 16 to UTF8, translated Unicode, or ASCII. + * If uni_xlate is enabled and we can't get a 1:1 conversion, use a + * colon as an escape character since it is normally invalid on the vfat + * filesystem. The following four characters are the hexadecimal digits + * of Unicode value. This lets us do a full dump and restore of Unicode + * filenames. We could get into some trouble with long Unicode names, + * but ignore that right now. + * Ahem... Stack smashing in ring 0 isn't fun. Fixed. + */ +static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate, + struct nls_table *nls) +{ + wchar_t *ip, ec; + unsigned char *op, nc; + int charlen; + int k; + + ip = uni; + op = ascii; + + while (*ip) { + ec = *ip++; + if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) { + op += charlen; + } else { + if (uni_xlate == 1) { + *op = ':'; + for (k = 4; k > 0; k--) { + nc = ec & 0xF; + op[k] = nc > 9 ? nc + ('a' - 10) + : nc + '0'; + ec >>= 4; + } + op += 5; + } else { + *op++ = '?'; + } + } + /* We have some slack there, so it's OK */ + if (op>ascii+256) { + op = ascii + 256; + break; + } + } + *op = 0; + return (op - ascii); +} + +static inline int +fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni) +{ + int charlen; + + charlen = t->char2uni(c, clen, uni); + if (charlen < 0) { + *uni = 0x003f; /* a question mark */ + charlen = 1; + } + return charlen; +} + +static inline int +fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni) +{ + int charlen; + wchar_t wc; + + charlen = t->char2uni(c, clen, &wc); + if (charlen < 0) { + *uni = 0x003f; /* a question mark */ + charlen = 1; + } else if (charlen <= 1) { + unsigned char nc = t->charset2lower[*c]; + + if (!nc) + nc = *c; + + if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) { + *uni = 0x003f; /* a question mark */ + charlen = 1; + } + } else + *uni = wc; + + return charlen; +} + +static inline int +fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size, + wchar_t *uni_buf, unsigned short opt, int lower) +{ + int len = 0; + + if (opt & VFAT_SFN_DISPLAY_LOWER) + len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); + else if (opt & VFAT_SFN_DISPLAY_WIN95) + len = fat_short2uni(nls, buf, buf_size, uni_buf); + else if (opt & VFAT_SFN_DISPLAY_WINNT) { + if (lower) + len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); + else + len = fat_short2uni(nls, buf, buf_size, uni_buf); + } else + len = fat_short2uni(nls, buf, buf_size, uni_buf); + + return len; +} + +/* + * Return values: negative -> error, 0 -> not found, positive -> found, + * value is the total amount of slots, including the shortname entry. + */ +int fat_search_long(struct inode *inode, const unsigned char *name, + int name_len, struct fat_slot_info *sinfo) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *bh = NULL; + struct msdos_dir_entry *de; + struct nls_table *nls_io = sbi->nls_io; + struct nls_table *nls_disk = sbi->nls_disk; + wchar_t bufuname[14]; + unsigned char xlate_len, nr_slots; + wchar_t *unicode = NULL; + unsigned char work[8], bufname[260]; /* 256 + 4 */ + int uni_xlate = sbi->options.unicode_xlate; + int utf8 = sbi->options.utf8; + int anycase = (sbi->options.name_check != 's'); + unsigned short opt_shortname = sbi->options.shortname; + loff_t cpos = 0; + int chl, i, j, last_u, err; + + err = -ENOENT; + while(1) { + if (fat_get_entry(inode, &cpos, &bh, &de) == -1) + goto EODir; +parse_record: + nr_slots = 0; + if (de->name[0] == DELETED_FLAG) + continue; + if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) + continue; + if (de->attr != ATTR_EXT && IS_FREE(de->name)) + continue; + if (de->attr == ATTR_EXT) { + struct msdos_dir_slot *ds; + unsigned char id; + unsigned char slot; + unsigned char slots; + unsigned char sum; + unsigned char alias_checksum; + + if (!unicode) { + unicode = (wchar_t *) + __get_free_page(GFP_KERNEL); + if (!unicode) { + brelse(bh); + return -ENOMEM; + } + } +parse_long: + slots = 0; + ds = (struct msdos_dir_slot *) de; + id = ds->id; + if (!(id & 0x40)) + continue; + slots = id & ~0x40; + if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */ + continue; + nr_slots = slots; + alias_checksum = ds->alias_checksum; + + slot = slots; + while (1) { + int offset; + + slot--; + offset = slot * 13; + fat16_towchar(unicode + offset, ds->name0_4, 5); + fat16_towchar(unicode + offset + 5, ds->name5_10, 6); + fat16_towchar(unicode + offset + 11, ds->name11_12, 2); + + if (ds->id & 0x40) { + unicode[offset + 13] = 0; + } + if (fat_get_entry(inode, &cpos, &bh, &de) < 0) + goto EODir; + if (slot == 0) + break; + ds = (struct msdos_dir_slot *) de; + if (ds->attr != ATTR_EXT) + goto parse_record; + if ((ds->id & ~0x40) != slot) + goto parse_long; + if (ds->alias_checksum != alias_checksum) + goto parse_long; + } + if (de->name[0] == DELETED_FLAG) + continue; + if (de->attr == ATTR_EXT) + goto parse_long; + if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME)) + continue; + for (sum = 0, i = 0; i < 11; i++) + sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i]; + if (sum != alias_checksum) + nr_slots = 0; + } + + memcpy(work, de->name, sizeof(de->name)); + /* see namei.c, msdos_format_name */ + if (work[0] == 0x05) + work[0] = 0xE5; + for (i = 0, j = 0, last_u = 0; i < 8;) { + if (!work[i]) break; + chl = fat_shortname2uni(nls_disk, &work[i], 8 - i, + &bufuname[j++], opt_shortname, + de->lcase & CASE_LOWER_BASE); + if (chl <= 1) { + if (work[i] != ' ') + last_u = j; + } else { + last_u = j; + } + i += chl; + } + j = last_u; + fat_short2uni(nls_disk, ".", 1, &bufuname[j++]); + for (i = 0; i < 3;) { + if (!de->ext[i]) break; + chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i, + &bufuname[j++], opt_shortname, + de->lcase & CASE_LOWER_EXT); + if (chl <= 1) { + if (de->ext[i] != ' ') + last_u = j; + } else { + last_u = j; + } + i += chl; + } + if (!last_u) + continue; + + bufuname[last_u] = 0x0000; + xlate_len = utf8 + ?utf8_wcstombs(bufname, bufuname, sizeof(bufname)) + :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io); + if (xlate_len == name_len) + if ((!anycase && !memcmp(name, bufname, xlate_len)) || + (anycase && !nls_strnicmp(nls_io, name, bufname, + xlate_len))) + goto Found; + + if (nr_slots) { + xlate_len = utf8 + ?utf8_wcstombs(bufname, unicode, sizeof(bufname)) + :uni16_to_x8(bufname, unicode, uni_xlate, nls_io); + if (xlate_len != name_len) + continue; + if ((!anycase && !memcmp(name, bufname, xlate_len)) || + (anycase && !nls_strnicmp(nls_io, name, bufname, + xlate_len))) + goto Found; + } + } + +Found: + nr_slots++; /* include the de */ + sinfo->slot_off = cpos - nr_slots * sizeof(*de); + sinfo->nr_slots = nr_slots; + sinfo->de = de; + sinfo->bh = bh; + sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); + err = 0; +EODir: + if (unicode) + free_page((unsigned long)unicode); + + return err; +} + +EXPORT_SYMBOL(fat_search_long); + +struct fat_ioctl_filldir_callback { + struct dirent __user *dirent; + int result; + /* for dir ioctl */ + const char *longname; + int long_len; + const char *shortname; + int short_len; +}; + +static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent, + filldir_t filldir, int short_only, int both) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *bh; + struct msdos_dir_entry *de; + struct nls_table *nls_io = sbi->nls_io; + struct nls_table *nls_disk = sbi->nls_disk; + unsigned char long_slots; + const char *fill_name; + int fill_len; + wchar_t bufuname[14]; + wchar_t *unicode = NULL; + unsigned char c, work[8], bufname[56], *ptname = bufname; + unsigned long lpos, dummy, *furrfu = &lpos; + int uni_xlate = sbi->options.unicode_xlate; + int isvfat = sbi->options.isvfat; + int utf8 = sbi->options.utf8; + int nocase = sbi->options.nocase; + unsigned short opt_shortname = sbi->options.shortname; + unsigned long inum; + int chi, chl, i, i2, j, last, last_u, dotoffset = 0; + loff_t cpos; + int ret = 0; + + lock_kernel(); + + cpos = filp->f_pos; + /* Fake . and .. for the root directory. */ + if (inode->i_ino == MSDOS_ROOT_INO) { + while (cpos < 2) { + if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0) + goto out; + cpos++; + filp->f_pos++; + } + if (cpos == 2) { + dummy = 2; + furrfu = &dummy; + cpos = 0; + } + } + if (cpos & (sizeof(struct msdos_dir_entry)-1)) { + ret = -ENOENT; + goto out; + } + + bh = NULL; +GetNew: + long_slots = 0; + if (fat_get_entry(inode, &cpos, &bh, &de) == -1) + goto EODir; + /* Check for long filename entry */ + if (isvfat) { + if (de->name[0] == DELETED_FLAG) + goto RecEnd; + if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) + goto RecEnd; + if (de->attr != ATTR_EXT && IS_FREE(de->name)) + goto RecEnd; + } else { + if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name)) + goto RecEnd; + } + + if (isvfat && de->attr == ATTR_EXT) { + struct msdos_dir_slot *ds; + unsigned char id; + unsigned char slot; + unsigned char slots; + unsigned char sum; + unsigned char alias_checksum; + + if (!unicode) { + unicode = (wchar_t *)__get_free_page(GFP_KERNEL); + if (!unicode) { + filp->f_pos = cpos; + brelse(bh); + ret = -ENOMEM; + goto out; + } + } +ParseLong: + slots = 0; + ds = (struct msdos_dir_slot *) de; + id = ds->id; + if (!(id & 0x40)) + goto RecEnd; + slots = id & ~0x40; + if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */ + goto RecEnd; + long_slots = slots; + alias_checksum = ds->alias_checksum; + + slot = slots; + while (1) { + int offset; + + slot--; + offset = slot * 13; + fat16_towchar(unicode + offset, ds->name0_4, 5); + fat16_towchar(unicode + offset + 5, ds->name5_10, 6); + fat16_towchar(unicode + offset + 11, ds->name11_12, 2); + + if (ds->id & 0x40) { + unicode[offset + 13] = 0; + } + if (fat_get_entry(inode, &cpos, &bh, &de) == -1) + goto EODir; + if (slot == 0) + break; + ds = (struct msdos_dir_slot *) de; + if (ds->attr != ATTR_EXT) + goto RecEnd; /* XXX */ + if ((ds->id & ~0x40) != slot) + goto ParseLong; + if (ds->alias_checksum != alias_checksum) + goto ParseLong; + } + if (de->name[0] == DELETED_FLAG) + goto RecEnd; + if (de->attr == ATTR_EXT) + goto ParseLong; + if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME)) + goto RecEnd; + for (sum = 0, i = 0; i < 11; i++) + sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i]; + if (sum != alias_checksum) + long_slots = 0; + } + + if (sbi->options.dotsOK) { + ptname = bufname; + dotoffset = 0; + if (de->attr & ATTR_HIDDEN) { + *ptname++ = '.'; + dotoffset = 1; + } + } + + memcpy(work, de->name, sizeof(de->name)); + /* see namei.c, msdos_format_name */ + if (work[0] == 0x05) + work[0] = 0xE5; + for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) { + if (!(c = work[i])) break; + chl = fat_shortname2uni(nls_disk, &work[i], 8 - i, + &bufuname[j++], opt_shortname, + de->lcase & CASE_LOWER_BASE); + if (chl <= 1) { + ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c; + if (c != ' ') { + last = i; + last_u = j; + } + } else { + last_u = j; + for (chi = 0; chi < chl && i < 8; chi++) { + ptname[i] = work[i]; + i++; last = i; + } + } + } + i = last; + j = last_u; + fat_short2uni(nls_disk, ".", 1, &bufuname[j++]); + ptname[i++] = '.'; + for (i2 = 0; i2 < 3;) { + if (!(c = de->ext[i2])) break; + chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2, + &bufuname[j++], opt_shortname, + de->lcase & CASE_LOWER_EXT); + if (chl <= 1) { + i2++; + ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c; + if (c != ' ') { + last = i; + last_u = j; + } + } else { + last_u = j; + for (chi = 0; chi < chl && i2 < 3; chi++) { + ptname[i++] = de->ext[i2++]; + last = i; + } + } + } + if (!last) + goto RecEnd; + + i = last + dotoffset; + j = last_u; + + lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry); + if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) + inum = inode->i_ino; + else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { + inum = parent_ino(filp->f_dentry); + } else { + loff_t i_pos = fat_make_i_pos(sb, bh, de); + struct inode *tmp = fat_iget(sb, i_pos); + if (tmp) { + inum = tmp->i_ino; + iput(tmp); + } else + inum = iunique(sb, MSDOS_ROOT_INO); + } + + if (isvfat) { + bufuname[j] = 0x0000; + i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname)) + : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io); + } + + fill_name = bufname; + fill_len = i; + if (!short_only && long_slots) { + /* convert the unicode long name. 261 is maximum size + * of unicode buffer. (13 * slots + nul) */ + void *longname = unicode + 261; + int buf_size = PAGE_SIZE - (261 * sizeof(unicode[0])); + int long_len = utf8 + ? utf8_wcstombs(longname, unicode, buf_size) + : uni16_to_x8(longname, unicode, uni_xlate, nls_io); + + if (!both) { + fill_name = longname; + fill_len = long_len; + } else { + /* hack for fat_ioctl_filldir() */ + struct fat_ioctl_filldir_callback *p = dirent; + + p->longname = longname; + p->long_len = long_len; + p->shortname = bufname; + p->short_len = i; + fill_name = NULL; + fill_len = 0; + } + } + if (filldir(dirent, fill_name, fill_len, *furrfu, inum, + (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0) + goto FillFailed; + +RecEnd: + furrfu = &lpos; + filp->f_pos = cpos; + goto GetNew; +EODir: + filp->f_pos = cpos; +FillFailed: + if (bh) + brelse(bh); + if (unicode) + free_page((unsigned long)unicode); +out: + unlock_kernel(); + return ret; +} + +static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir) +{ + struct inode *inode = filp->f_dentry->d_inode; + return fat_readdirx(inode, filp, dirent, filldir, 0, 0); +} + +static int fat_ioctl_filldir(void *__buf, const char *name, int name_len, + loff_t offset, ino_t ino, unsigned int d_type) +{ + struct fat_ioctl_filldir_callback *buf = __buf; + struct dirent __user *d1 = buf->dirent; + struct dirent __user *d2 = d1 + 1; + + if (buf->result) + return -EINVAL; + buf->result++; + + if (name != NULL) { + /* dirent has only short name */ + if (name_len >= sizeof(d1->d_name)) + name_len = sizeof(d1->d_name) - 1; + + if (put_user(0, d2->d_name) || + put_user(0, &d2->d_reclen) || + copy_to_user(d1->d_name, name, name_len) || + put_user(0, d1->d_name + name_len) || + put_user(name_len, &d1->d_reclen)) + goto efault; + } else { + /* dirent has short and long name */ + const char *longname = buf->longname; + int long_len = buf->long_len; + const char *shortname = buf->shortname; + int short_len = buf->short_len; + + if (long_len >= sizeof(d1->d_name)) + long_len = sizeof(d1->d_name) - 1; + if (short_len >= sizeof(d1->d_name)) + short_len = sizeof(d1->d_name) - 1; + + if (copy_to_user(d2->d_name, longname, long_len) || + put_user(0, d2->d_name + long_len) || + put_user(long_len, &d2->d_reclen) || + put_user(ino, &d2->d_ino) || + put_user(offset, &d2->d_off) || + copy_to_user(d1->d_name, shortname, short_len) || + put_user(0, d1->d_name + short_len) || + put_user(short_len, &d1->d_reclen)) + goto efault; + } + return 0; +efault: + buf->result = -EFAULT; + return -EFAULT; +} + +static int fat_dir_ioctl(struct inode * inode, struct file * filp, + unsigned int cmd, unsigned long arg) +{ + struct fat_ioctl_filldir_callback buf; + struct dirent __user *d1; + int ret, short_only, both; + + switch (cmd) { + case VFAT_IOCTL_READDIR_SHORT: + short_only = 1; + both = 0; + break; + case VFAT_IOCTL_READDIR_BOTH: + short_only = 0; + both = 1; + break; + default: + return fat_generic_ioctl(inode, filp, cmd, arg); + } + + d1 = (struct dirent __user *)arg; + if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2]))) + return -EFAULT; + /* + * Yes, we don't need this put_user() absolutely. However old + * code didn't return the right value. So, app use this value, + * in order to check whether it is EOF. + */ + if (put_user(0, &d1->d_reclen)) + return -EFAULT; + + buf.dirent = d1; + buf.result = 0; + down(&inode->i_sem); + ret = -ENOENT; + if (!IS_DEADDIR(inode)) { + ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir, + short_only, both); + } + up(&inode->i_sem); + if (ret >= 0) + ret = buf.result; + return ret; +} + +struct file_operations fat_dir_operations = { + .read = generic_read_dir, + .readdir = fat_readdir, + .ioctl = fat_dir_ioctl, + .fsync = file_fsync, +}; + +static int fat_get_short_entry(struct inode *dir, loff_t *pos, + struct buffer_head **bh, + struct msdos_dir_entry **de) +{ + while (fat_get_entry(dir, pos, bh, de) >= 0) { + /* free entry or long name entry or volume label */ + if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME)) + return 0; + } + return -ENOENT; +} + +/* + * The ".." entry can not provide the "struct fat_slot_info" informations + * for inode. So, this function provide the some informations only. + */ +int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh, + struct msdos_dir_entry **de, loff_t *i_pos) +{ + loff_t offset; + + offset = 0; + *bh = NULL; + while (fat_get_short_entry(dir, &offset, bh, de) >= 0) { + if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) { + *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de); + return 0; + } + } + return -ENOENT; +} + +EXPORT_SYMBOL(fat_get_dotdot_entry); + +/* See if directory is empty */ +int fat_dir_empty(struct inode *dir) +{ + struct buffer_head *bh; + struct msdos_dir_entry *de; + loff_t cpos; + int result = 0; + + bh = NULL; + cpos = 0; + while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { + if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) && + strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { + result = -ENOTEMPTY; + break; + } + } + brelse(bh); + return result; +} + +EXPORT_SYMBOL(fat_dir_empty); + +/* + * fat_subdirs counts the number of sub-directories of dir. It can be run + * on directories being created. + */ +int fat_subdirs(struct inode *dir) +{ + struct buffer_head *bh; + struct msdos_dir_entry *de; + loff_t cpos; + int count = 0; + + bh = NULL; + cpos = 0; + while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { + if (de->attr & ATTR_DIR) + count++; + } + brelse(bh); + return count; +} + +/* + * Scans a directory for a given file (name points to its formatted name). + * Returns an error code or zero. + */ +int fat_scan(struct inode *dir, const unsigned char *name, + struct fat_slot_info *sinfo) +{ + struct super_block *sb = dir->i_sb; + + sinfo->slot_off = 0; + sinfo->bh = NULL; + while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh, + &sinfo->de) >= 0) { + if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) { + sinfo->slot_off -= sizeof(*sinfo->de); + sinfo->nr_slots = 1; + sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); + return 0; + } + } + return -ENOENT; +} + +EXPORT_SYMBOL(fat_scan); + +static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots) +{ + struct super_block *sb = dir->i_sb; + struct buffer_head *bh; + struct msdos_dir_entry *de, *endp; + int err = 0, orig_slots; + + while (nr_slots) { + bh = NULL; + if (fat_get_entry(dir, &pos, &bh, &de) < 0) { + err = -EIO; + break; + } + + orig_slots = nr_slots; + endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize); + while (nr_slots && de < endp) { + de->name[0] = DELETED_FLAG; + de++; + nr_slots--; + } + mark_buffer_dirty(bh); + if (IS_DIRSYNC(dir)) + err = sync_dirty_buffer(bh); + brelse(bh); + if (err) + break; + + /* pos is *next* de's position, so this does `- sizeof(de)' */ + pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de); + } + + return err; +} + +int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo) +{ + struct msdos_dir_entry *de; + struct buffer_head *bh; + int err = 0, nr_slots; + + /* + * First stage: Remove the shortname. By this, the directory + * entry is removed. + */ + nr_slots = sinfo->nr_slots; + de = sinfo->de; + sinfo->de = NULL; + bh = sinfo->bh; + sinfo->bh = NULL; + while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) { + de->name[0] = DELETED_FLAG; + de--; + nr_slots--; + } + mark_buffer_dirty(bh); + if (IS_DIRSYNC(dir)) + err = sync_dirty_buffer(bh); + brelse(bh); + if (err) + return err; + dir->i_version++; + + if (nr_slots) { + /* + * Second stage: remove the remaining longname slots. + * (This directory entry is already removed, and so return + * the success) + */ + err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots); + if (err) { + printk(KERN_WARNING + "FAT: Couldn't remove the long name slots\n"); + } + } + + dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC; + if (IS_DIRSYNC(dir)) + (void)fat_sync_inode(dir); + else + mark_inode_dirty(dir); + + return 0; +} + +EXPORT_SYMBOL(fat_remove_entries); + +static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used, + struct buffer_head **bhs, int nr_bhs) +{ + struct super_block *sb = dir->i_sb; + sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus; + int err, i, n; + + /* Zeroing the unused blocks on this cluster */ + blknr += nr_used; + n = nr_used; + while (blknr < last_blknr) { + bhs[n] = sb_getblk(sb, blknr); + if (!bhs[n]) { + err = -ENOMEM; + goto error; + } + memset(bhs[n]->b_data, 0, sb->s_blocksize); + set_buffer_uptodate(bhs[n]); + mark_buffer_dirty(bhs[n]); + + n++; + blknr++; + if (n == nr_bhs) { + if (IS_DIRSYNC(dir)) { + err = fat_sync_bhs(bhs, n); + if (err) + goto error; + } + for (i = 0; i < n; i++) + brelse(bhs[i]); + n = 0; + } + } + if (IS_DIRSYNC(dir)) { + err = fat_sync_bhs(bhs, n); + if (err) + goto error; + } + for (i = 0; i < n; i++) + brelse(bhs[i]); + + return 0; + +error: + for (i = 0; i < n; i++) + bforget(bhs[i]); + return err; +} + +int fat_alloc_new_dir(struct inode *dir, struct timespec *ts) +{ + struct super_block *sb = dir->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *bhs[MAX_BUF_PER_PAGE]; + struct msdos_dir_entry *de; + sector_t blknr; + __le16 date, time; + int err, cluster; + + err = fat_alloc_clusters(dir, &cluster, 1); + if (err) + goto error; + + blknr = fat_clus_to_blknr(sbi, cluster); + bhs[0] = sb_getblk(sb, blknr); + if (!bhs[0]) { + err = -ENOMEM; + goto error_free; + } + + fat_date_unix2dos(ts->tv_sec, &time, &date); + + de = (struct msdos_dir_entry *)bhs[0]->b_data; + /* filling the new directory slots ("." and ".." entries) */ + memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME); + memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME); + de->attr = de[1].attr = ATTR_DIR; + de[0].lcase = de[1].lcase = 0; + de[0].time = de[1].time = time; + de[0].date = de[1].date = date; + de[0].ctime_cs = de[1].ctime_cs = 0; + if (sbi->options.isvfat) { + /* extra timestamps */ + de[0].ctime = de[1].ctime = time; + de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date; + } else { + de[0].ctime = de[1].ctime = 0; + de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0; + } + de[0].start = cpu_to_le16(cluster); + de[0].starthi = cpu_to_le16(cluster >> 16); + de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart); + de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16); + de[0].size = de[1].size = 0; + memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de)); + set_buffer_uptodate(bhs[0]); + mark_buffer_dirty(bhs[0]); + + err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE); + if (err) + goto error_free; + + return cluster; + +error_free: + fat_free_clusters(dir, cluster); +error: + return err; +} + +EXPORT_SYMBOL(fat_alloc_new_dir); + +static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots, + int *nr_cluster, struct msdos_dir_entry **de, + struct buffer_head **bh, loff_t *i_pos) +{ + struct super_block *sb = dir->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *bhs[MAX_BUF_PER_PAGE]; + sector_t blknr, start_blknr, last_blknr; + unsigned long size, copy; + int err, i, n, offset, cluster[2]; + + /* + * The minimum cluster size is 512bytes, and maximum entry + * size is 32*slots (672bytes). So, iff the cluster size is + * 512bytes, we may need two clusters. + */ + size = nr_slots * sizeof(struct msdos_dir_entry); + *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits; + BUG_ON(*nr_cluster > 2); + + err = fat_alloc_clusters(dir, cluster, *nr_cluster); + if (err) + goto error; + + /* + * First stage: Fill the directory entry. NOTE: This cluster + * is not referenced from any inode yet, so updates order is + * not important. + */ + i = n = copy = 0; + do { + start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]); + last_blknr = start_blknr + sbi->sec_per_clus; + while (blknr < last_blknr) { + bhs[n] = sb_getblk(sb, blknr); + if (!bhs[n]) { + err = -ENOMEM; + goto error_nomem; + } + + /* fill the directory entry */ + copy = min(size, sb->s_blocksize); + memcpy(bhs[n]->b_data, slots, copy); + slots += copy; + size -= copy; + set_buffer_uptodate(bhs[n]); + mark_buffer_dirty(bhs[n]); + if (!size) + break; + n++; + blknr++; + } + } while (++i < *nr_cluster); + + memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy); + offset = copy - sizeof(struct msdos_dir_entry); + get_bh(bhs[n]); + *bh = bhs[n]; + *de = (struct msdos_dir_entry *)((*bh)->b_data + offset); + *i_pos = fat_make_i_pos(sb, *bh, *de); + + /* Second stage: clear the rest of cluster, and write outs */ + err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE); + if (err) + goto error_free; + + return cluster[0]; + +error_free: + brelse(*bh); + *bh = NULL; + n = 0; +error_nomem: + for (i = 0; i < n; i++) + bforget(bhs[i]); + fat_free_clusters(dir, cluster[0]); +error: + return err; +} + +int fat_add_entries(struct inode *dir, void *slots, int nr_slots, + struct fat_slot_info *sinfo) +{ + struct super_block *sb = dir->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */ + struct msdos_dir_entry *de; + int err, free_slots, i, nr_bhs; + loff_t pos, i_pos; + + sinfo->nr_slots = nr_slots; + + /* First stage: search free direcotry entries */ + free_slots = nr_bhs = 0; + bh = prev = NULL; + pos = 0; + err = -ENOSPC; + while (fat_get_entry(dir, &pos, &bh, &de) > -1) { + /* check the maximum size of directory */ + if (pos >= FAT_MAX_DIR_SIZE) + goto error; + + if (IS_FREE(de->name)) { + if (prev != bh) { + get_bh(bh); + bhs[nr_bhs] = prev = bh; + nr_bhs++; + } + free_slots++; + if (free_slots == nr_slots) + goto found; + } else { + for (i = 0; i < nr_bhs; i++) + brelse(bhs[i]); + prev = NULL; + free_slots = nr_bhs = 0; + } + } + if (dir->i_ino == MSDOS_ROOT_INO) { + if (sbi->fat_bits != 32) + goto error; + } else if (MSDOS_I(dir)->i_start == 0) { + printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n", + MSDOS_I(dir)->i_pos); + err = -EIO; + goto error; + } + +found: + err = 0; + pos -= free_slots * sizeof(*de); + nr_slots -= free_slots; + if (free_slots) { + /* + * Second stage: filling the free entries with new entries. + * NOTE: If this slots has shortname, first, we write + * the long name slots, then write the short name. + */ + int size = free_slots * sizeof(*de); + int offset = pos & (sb->s_blocksize - 1); + int long_bhs = nr_bhs - (nr_slots == 0); + + /* Fill the long name slots. */ + for (i = 0; i < long_bhs; i++) { + int copy = min_t(int, sb->s_blocksize - offset, size); + memcpy(bhs[i]->b_data + offset, slots, copy); + mark_buffer_dirty(bhs[i]); + offset = 0; + slots += copy; + size -= copy; + } + if (long_bhs && IS_DIRSYNC(dir)) + err = fat_sync_bhs(bhs, long_bhs); + if (!err && i < nr_bhs) { + /* Fill the short name slot. */ + int copy = min_t(int, sb->s_blocksize - offset, size); + memcpy(bhs[i]->b_data + offset, slots, copy); + mark_buffer_dirty(bhs[i]); + if (IS_DIRSYNC(dir)) + err = sync_dirty_buffer(bhs[i]); + } + for (i = 0; i < nr_bhs; i++) + brelse(bhs[i]); + if (err) + goto error_remove; + } + + if (nr_slots) { + int cluster, nr_cluster; + + /* + * Third stage: allocate the cluster for new entries. + * And initialize the cluster with new entries, then + * add the cluster to dir. + */ + cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster, + &de, &bh, &i_pos); + if (cluster < 0) { + err = cluster; + goto error_remove; + } + err = fat_chain_add(dir, cluster, nr_cluster); + if (err) { + fat_free_clusters(dir, cluster); + goto error_remove; + } + if (dir->i_size & (sbi->cluster_size - 1)) { + fat_fs_panic(sb, "Odd directory size"); + dir->i_size = (dir->i_size + sbi->cluster_size - 1) + & ~((loff_t)sbi->cluster_size - 1); + } + dir->i_size += nr_cluster << sbi->cluster_bits; + MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits; + } + sinfo->slot_off = pos; + sinfo->de = de; + sinfo->bh = bh; + sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); + + return 0; + +error: + brelse(bh); + for (i = 0; i < nr_bhs; i++) + brelse(bhs[i]); + return err; + +error_remove: + brelse(bh); + if (free_slots) + __fat_remove_entries(dir, pos, free_slots); + return err; +} + +EXPORT_SYMBOL(fat_add_entries); diff --git a/fs/fat/fatent.c b/fs/fat/fatent.c new file mode 100644 index 000000000000..4164cd54c4d1 --- /dev/null +++ b/fs/fat/fatent.c @@ -0,0 +1,612 @@ +/* + * Copyright (C) 2004, OGAWA Hirofumi + * Released under GPL v2. + */ + +#include <linux/module.h> +#include <linux/fs.h> +#include <linux/msdos_fs.h> + +struct fatent_operations { + void (*ent_blocknr)(struct super_block *, int, int *, sector_t *); + void (*ent_set_ptr)(struct fat_entry *, int); + int (*ent_bread)(struct super_block *, struct fat_entry *, + int, sector_t); + int (*ent_get)(struct fat_entry *); + void (*ent_put)(struct fat_entry *, int); + int (*ent_next)(struct fat_entry *); +}; + +static void fat12_ent_blocknr(struct super_block *sb, int entry, + int *offset, sector_t *blocknr) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + int bytes = entry + (entry >> 1); + WARN_ON(entry < FAT_START_ENT || sbi->max_cluster <= entry); + *offset = bytes & (sb->s_blocksize - 1); + *blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits); +} + +static void fat_ent_blocknr(struct super_block *sb, int entry, + int *offset, sector_t *blocknr) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + int bytes = (entry << sbi->fatent_shift); + WARN_ON(entry < FAT_START_ENT || sbi->max_cluster <= entry); + *offset = bytes & (sb->s_blocksize - 1); + *blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits); +} + +static void fat12_ent_set_ptr(struct fat_entry *fatent, int offset) +{ + struct buffer_head **bhs = fatent->bhs; + if (fatent->nr_bhs == 1) { + WARN_ON(offset >= (bhs[0]->b_size - 1)); + fatent->u.ent12_p[0] = bhs[0]->b_data + offset; + fatent->u.ent12_p[1] = bhs[0]->b_data + (offset + 1); + } else { + WARN_ON(offset != (bhs[0]->b_size - 1)); + fatent->u.ent12_p[0] = bhs[0]->b_data + offset; + fatent->u.ent12_p[1] = bhs[1]->b_data; + } +} + +static void fat16_ent_set_ptr(struct fat_entry *fatent, int offset) +{ + WARN_ON(offset & (2 - 1)); + fatent->u.ent16_p = (__le16 *)(fatent->bhs[0]->b_data + offset); +} + +static void fat32_ent_set_ptr(struct fat_entry *fatent, int offset) +{ + WARN_ON(offset & (4 - 1)); + fatent->u.ent32_p = (__le32 *)(fatent->bhs[0]->b_data + offset); +} + +static int fat12_ent_bread(struct super_block *sb, struct fat_entry *fatent, + int offset, sector_t blocknr) +{ + struct buffer_head **bhs = fatent->bhs; + + WARN_ON(blocknr < MSDOS_SB(sb)->fat_start); + bhs[0] = sb_bread(sb, blocknr); + if (!bhs[0]) + goto err; + + if ((offset + 1) < sb->s_blocksize) + fatent->nr_bhs = 1; + else { + /* This entry is block boundary, it needs the next block */ + blocknr++; + bhs[1] = sb_bread(sb, blocknr); + if (!bhs[1]) + goto err_brelse; + fatent->nr_bhs = 2; + } + fat12_ent_set_ptr(fatent, offset); + return 0; + +err_brelse: + brelse(bhs[0]); +err: + printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n", + (unsigned long long)blocknr); + return -EIO; +} + +static int fat_ent_bread(struct super_block *sb, struct fat_entry *fatent, + int offset, sector_t blocknr) +{ + struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; + + WARN_ON(blocknr < MSDOS_SB(sb)->fat_start); + fatent->bhs[0] = sb_bread(sb, blocknr); + if (!fatent->bhs[0]) { + printk(KERN_ERR "FAT: FAT read failed (blocknr %llu)\n", + (unsigned long long)blocknr); + return -EIO; + } + fatent->nr_bhs = 1; + ops->ent_set_ptr(fatent, offset); + return 0; +} + +static int fat12_ent_get(struct fat_entry *fatent) +{ + u8 **ent12_p = fatent->u.ent12_p; + int next; + + if (fatent->entry & 1) + next = (*ent12_p[0] >> 4) | (*ent12_p[1] << 4); + else + next = (*ent12_p[1] << 8) | *ent12_p[0]; + next &= 0x0fff; + if (next >= BAD_FAT12) + next = FAT_ENT_EOF; + return next; +} + +static int fat16_ent_get(struct fat_entry *fatent) +{ + int next = le16_to_cpu(*fatent->u.ent16_p); + WARN_ON((unsigned long)fatent->u.ent16_p & (2 - 1)); + if (next >= BAD_FAT16) + next = FAT_ENT_EOF; + return next; +} + +static int fat32_ent_get(struct fat_entry *fatent) +{ + int next = le32_to_cpu(*fatent->u.ent32_p) & 0x0fffffff; + WARN_ON((unsigned long)fatent->u.ent32_p & (4 - 1)); + if (next >= BAD_FAT32) + next = FAT_ENT_EOF; + return next; +} + +static void fat12_ent_put(struct fat_entry *fatent, int new) +{ + u8 **ent12_p = fatent->u.ent12_p; + + if (new == FAT_ENT_EOF) + new = EOF_FAT12; + + if (fatent->entry & 1) { + *ent12_p[0] = (new << 4) | (*ent12_p[0] & 0x0f); + *ent12_p[1] = new >> 4; + } else { + *ent12_p[0] = new & 0xff; + *ent12_p[1] = (*ent12_p[1] & 0xf0) | (new >> 8); + } + + mark_buffer_dirty(fatent->bhs[0]); + if (fatent->nr_bhs == 2) + mark_buffer_dirty(fatent->bhs[1]); +} + +static void fat16_ent_put(struct fat_entry *fatent, int new) +{ + if (new == FAT_ENT_EOF) + new = EOF_FAT16; + + *fatent->u.ent16_p = cpu_to_le16(new); + mark_buffer_dirty(fatent->bhs[0]); +} + +static void fat32_ent_put(struct fat_entry *fatent, int new) +{ + if (new == FAT_ENT_EOF) + new = EOF_FAT32; + + WARN_ON(new & 0xf0000000); + new |= le32_to_cpu(*fatent->u.ent32_p) & ~0x0fffffff; + *fatent->u.ent32_p = cpu_to_le32(new); + mark_buffer_dirty(fatent->bhs[0]); +} + +static int fat12_ent_next(struct fat_entry *fatent) +{ + u8 **ent12_p = fatent->u.ent12_p; + struct buffer_head **bhs = fatent->bhs; + u8 *nextp = ent12_p[1] + 1 + (fatent->entry & 1); + + fatent->entry++; + if (fatent->nr_bhs == 1) { + WARN_ON(ent12_p[0] > (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 2))); + WARN_ON(ent12_p[1] > (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))); + if (nextp < (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))) { + ent12_p[0] = nextp - 1; + ent12_p[1] = nextp; + return 1; + } + } else { + WARN_ON(ent12_p[0] != (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))); + WARN_ON(ent12_p[1] != (u8 *)bhs[1]->b_data); + ent12_p[0] = nextp - 1; + ent12_p[1] = nextp; + brelse(bhs[0]); + bhs[0] = bhs[1]; + fatent->nr_bhs = 1; + return 1; + } + ent12_p[0] = NULL; + ent12_p[1] = NULL; + return 0; +} + +static int fat16_ent_next(struct fat_entry *fatent) +{ + const struct buffer_head *bh = fatent->bhs[0]; + fatent->entry++; + if (fatent->u.ent16_p < (__le16 *)(bh->b_data + (bh->b_size - 2))) { + fatent->u.ent16_p++; + return 1; + } + fatent->u.ent16_p = NULL; + return 0; +} + +static int fat32_ent_next(struct fat_entry *fatent) +{ + const struct buffer_head *bh = fatent->bhs[0]; + fatent->entry++; + if (fatent->u.ent32_p < (__le32 *)(bh->b_data + (bh->b_size - 4))) { + fatent->u.ent32_p++; + return 1; + } + fatent->u.ent32_p = NULL; + return 0; +} + +static struct fatent_operations fat12_ops = { + .ent_blocknr = fat12_ent_blocknr, + .ent_set_ptr = fat12_ent_set_ptr, + .ent_bread = fat12_ent_bread, + .ent_get = fat12_ent_get, + .ent_put = fat12_ent_put, + .ent_next = fat12_ent_next, +}; + +static struct fatent_operations fat16_ops = { + .ent_blocknr = fat_ent_blocknr, + .ent_set_ptr = fat16_ent_set_ptr, + .ent_bread = fat_ent_bread, + .ent_get = fat16_ent_get, + .ent_put = fat16_ent_put, + .ent_next = fat16_ent_next, +}; + +static struct fatent_operations fat32_ops = { + .ent_blocknr = fat_ent_blocknr, + .ent_set_ptr = fat32_ent_set_ptr, + .ent_bread = fat_ent_bread, + .ent_get = fat32_ent_get, + .ent_put = fat32_ent_put, + .ent_next = fat32_ent_next, +}; + +static inline void lock_fat(struct msdos_sb_info *sbi) +{ + down(&sbi->fat_lock); +} + +static inline void unlock_fat(struct msdos_sb_info *sbi) +{ + up(&sbi->fat_lock); +} + +void fat_ent_access_init(struct super_block *sb) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + + init_MUTEX(&sbi->fat_lock); + + switch (sbi->fat_bits) { + case 32: + sbi->fatent_shift = 2; + sbi->fatent_ops = &fat32_ops; + break; + case 16: + sbi->fatent_shift = 1; + sbi->fatent_ops = &fat16_ops; + break; + case 12: + sbi->fatent_shift = -1; + sbi->fatent_ops = &fat12_ops; + break; + } +} + +static inline int fat_ent_update_ptr(struct super_block *sb, + struct fat_entry *fatent, + int offset, sector_t blocknr) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct fatent_operations *ops = sbi->fatent_ops; + struct buffer_head **bhs = fatent->bhs; + + /* Is this fatent's blocks including this entry? */ + if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr) + return 0; + /* Does this entry need the next block? */ + if (sbi->fat_bits == 12 && (offset + 1) >= sb->s_blocksize) { + if (fatent->nr_bhs != 2 || bhs[1]->b_blocknr != (blocknr + 1)) + return 0; + } + ops->ent_set_ptr(fatent, offset); + return 1; +} + +int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); + struct fatent_operations *ops = sbi->fatent_ops; + int err, offset; + sector_t blocknr; + + if (entry < FAT_START_ENT || sbi->max_cluster <= entry) { + fatent_brelse(fatent); + fat_fs_panic(sb, "invalid access to FAT (entry 0x%08x)", entry); + return -EIO; + } + + fatent_set_entry(fatent, entry); + ops->ent_blocknr(sb, entry, &offset, &blocknr); + + if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) { + fatent_brelse(fatent); + err = ops->ent_bread(sb, fatent, offset, blocknr); + if (err) + return err; + } + return ops->ent_get(fatent); +} + +/* FIXME: We can write the blocks as more big chunk. */ +static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs, + int nr_bhs) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *c_bh; + int err, n, copy; + + err = 0; + for (copy = 1; copy < sbi->fats; copy++) { + sector_t backup_fat = sbi->fat_length * copy; + + for (n = 0; n < nr_bhs; n++) { + c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr); + if (!c_bh) { + err = -ENOMEM; + goto error; + } + memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize); + set_buffer_uptodate(c_bh); + mark_buffer_dirty(c_bh); + if (sb->s_flags & MS_SYNCHRONOUS) + err = sync_dirty_buffer(c_bh); + brelse(c_bh); + if (err) + goto error; + } + } +error: + return err; +} + +int fat_ent_write(struct inode *inode, struct fat_entry *fatent, + int new, int wait) +{ + struct super_block *sb = inode->i_sb; + struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; + int err; + + ops->ent_put(fatent, new); + if (wait) { + err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs); + if (err) + return err; + } + return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs); +} + +static inline int fat_ent_next(struct msdos_sb_info *sbi, + struct fat_entry *fatent) +{ + if (sbi->fatent_ops->ent_next(fatent)) { + if (fatent->entry < sbi->max_cluster) + return 1; + } + return 0; +} + +static inline int fat_ent_read_block(struct super_block *sb, + struct fat_entry *fatent) +{ + struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops; + sector_t blocknr; + int offset; + + fatent_brelse(fatent); + ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr); + return ops->ent_bread(sb, fatent, offset, blocknr); +} + +static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs, + struct fat_entry *fatent) +{ + int n, i; + + for (n = 0; n < fatent->nr_bhs; n++) { + for (i = 0; i < *nr_bhs; i++) { + if (fatent->bhs[n] == bhs[i]) + break; + } + if (i == *nr_bhs) { + get_bh(fatent->bhs[n]); + bhs[i] = fatent->bhs[n]; + (*nr_bhs)++; + } + } +} + +int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct fatent_operations *ops = sbi->fatent_ops; + struct fat_entry fatent, prev_ent; + struct buffer_head *bhs[MAX_BUF_PER_PAGE]; + int i, count, err, nr_bhs, idx_clus; + + BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */ + + lock_fat(sbi); + if (sbi->free_clusters != -1 && sbi->free_clusters < nr_cluster) { + unlock_fat(sbi); + return -ENOSPC; + } + + err = nr_bhs = idx_clus = 0; + count = FAT_START_ENT; + fatent_init(&prev_ent); + fatent_init(&fatent); + fatent_set_entry(&fatent, sbi->prev_free + 1); + while (count < sbi->max_cluster) { + if (fatent.entry >= sbi->max_cluster) + fatent.entry = FAT_START_ENT; + fatent_set_entry(&fatent, fatent.entry); + err = fat_ent_read_block(sb, &fatent); + if (err) + goto out; + + /* Find the free entries in a block */ + do { + if (ops->ent_get(&fatent) == FAT_ENT_FREE) { + int entry = fatent.entry; + + /* make the cluster chain */ + ops->ent_put(&fatent, FAT_ENT_EOF); + if (prev_ent.nr_bhs) + ops->ent_put(&prev_ent, entry); + + fat_collect_bhs(bhs, &nr_bhs, &fatent); + + sbi->prev_free = entry; + if (sbi->free_clusters != -1) + sbi->free_clusters--; + + cluster[idx_clus] = entry; + idx_clus++; + if (idx_clus == nr_cluster) + goto out; + + /* + * fat_collect_bhs() gets ref-count of bhs, + * so we can still use the prev_ent. + */ + prev_ent = fatent; + } + count++; + if (count == sbi->max_cluster) + break; + } while (fat_ent_next(sbi, &fatent)); + } + + /* Couldn't allocate the free entries */ + sbi->free_clusters = 0; + err = -ENOSPC; + +out: + unlock_fat(sbi); + fatent_brelse(&fatent); + if (!err) { + if (inode_needs_sync(inode)) + err = fat_sync_bhs(bhs, nr_bhs); + if (!err) + err = fat_mirror_bhs(sb, bhs, nr_bhs); + } + for (i = 0; i < nr_bhs; i++) + brelse(bhs[i]); + fat_clusters_flush(sb); + + if (err && idx_clus) + fat_free_clusters(inode, cluster[0]); + + return err; +} + +int fat_free_clusters(struct inode *inode, int cluster) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct fatent_operations *ops = sbi->fatent_ops; + struct fat_entry fatent; + struct buffer_head *bhs[MAX_BUF_PER_PAGE]; + int i, err, nr_bhs; + + nr_bhs = 0; + fatent_init(&fatent); + lock_fat(sbi); + do { + cluster = fat_ent_read(inode, &fatent, cluster); + if (cluster < 0) { + err = cluster; + goto error; + } else if (cluster == FAT_ENT_FREE) { + fat_fs_panic(sb, "%s: deleting FAT entry beyond EOF", + __FUNCTION__); + err = -EIO; + goto error; + } + + ops->ent_put(&fatent, FAT_ENT_FREE); + if (sbi->free_clusters != -1) + sbi->free_clusters++; + + if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) { + if (sb->s_flags & MS_SYNCHRONOUS) { + err = fat_sync_bhs(bhs, nr_bhs); + if (err) + goto error; + } + err = fat_mirror_bhs(sb, bhs, nr_bhs); + if (err) + goto error; + for (i = 0; i < nr_bhs; i++) + brelse(bhs[i]); + nr_bhs = 0; + } + fat_collect_bhs(bhs, &nr_bhs, &fatent); + } while (cluster != FAT_ENT_EOF); + + if (sb->s_flags & MS_SYNCHRONOUS) { + err = fat_sync_bhs(bhs, nr_bhs); + if (err) + goto error; + } + err = fat_mirror_bhs(sb, bhs, nr_bhs); +error: + fatent_brelse(&fatent); + for (i = 0; i < nr_bhs; i++) + brelse(bhs[i]); + unlock_fat(sbi); + + fat_clusters_flush(sb); + + return err; +} + +EXPORT_SYMBOL(fat_free_clusters); + +int fat_count_free_clusters(struct super_block *sb) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct fatent_operations *ops = sbi->fatent_ops; + struct fat_entry fatent; + int err = 0, free; + + lock_fat(sbi); + if (sbi->free_clusters != -1) + goto out; + + free = 0; + fatent_init(&fatent); + fatent_set_entry(&fatent, FAT_START_ENT); + while (fatent.entry < sbi->max_cluster) { + err = fat_ent_read_block(sb, &fatent); + if (err) + goto out; + + do { + if (ops->ent_get(&fatent) == FAT_ENT_FREE) + free++; + } while (fat_ent_next(sbi, &fatent)); + } + sbi->free_clusters = free; + fatent_brelse(&fatent); +out: + unlock_fat(sbi); + return err; +} diff --git a/fs/fat/file.c b/fs/fat/file.c new file mode 100644 index 000000000000..62ffa9139400 --- /dev/null +++ b/fs/fat/file.c @@ -0,0 +1,308 @@ +/* + * linux/fs/fat/file.c + * + * Written 1992,1993 by Werner Almesberger + * + * regular file handling primitives for fat-based filesystems + */ + +#include <linux/module.h> +#include <linux/time.h> +#include <linux/msdos_fs.h> +#include <linux/smp_lock.h> +#include <linux/buffer_head.h> + +static ssize_t fat_file_aio_write(struct kiocb *iocb, const char __user *buf, + size_t count, loff_t pos) +{ + struct inode *inode = iocb->ki_filp->f_dentry->d_inode; + int retval; + + retval = generic_file_aio_write(iocb, buf, count, pos); + if (retval > 0) { + inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; + MSDOS_I(inode)->i_attrs |= ATTR_ARCH; + mark_inode_dirty(inode); +// check the locking rules +// if (IS_SYNC(inode)) +// fat_sync_inode(inode); + } + return retval; +} + +static ssize_t fat_file_writev(struct file *filp, const struct iovec *iov, + unsigned long nr_segs, loff_t *ppos) +{ + struct inode *inode = filp->f_dentry->d_inode; + int retval; + + retval = generic_file_writev(filp, iov, nr_segs, ppos); + if (retval > 0) { + inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; + MSDOS_I(inode)->i_attrs |= ATTR_ARCH; + mark_inode_dirty(inode); + } + return retval; +} + +int fat_generic_ioctl(struct inode *inode, struct file *filp, + unsigned int cmd, unsigned long arg) +{ + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); + u32 __user *user_attr = (u32 __user *)arg; + + switch (cmd) { + case FAT_IOCTL_GET_ATTRIBUTES: + { + u32 attr; + + if (inode->i_ino == MSDOS_ROOT_INO) + attr = ATTR_DIR; + else + attr = fat_attr(inode); + + return put_user(attr, user_attr); + } + case FAT_IOCTL_SET_ATTRIBUTES: + { + u32 attr, oldattr; + int err, is_dir = S_ISDIR(inode->i_mode); + struct iattr ia; + + err = get_user(attr, user_attr); + if (err) + return err; + + down(&inode->i_sem); + + if (IS_RDONLY(inode)) { + err = -EROFS; + goto up; + } + + /* + * ATTR_VOLUME and ATTR_DIR cannot be changed; this also + * prevents the user from turning us into a VFAT + * longname entry. Also, we obviously can't set + * any of the NTFS attributes in the high 24 bits. + */ + attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); + /* Merge in ATTR_VOLUME and ATTR_DIR */ + attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | + (is_dir ? ATTR_DIR : 0); + oldattr = fat_attr(inode); + + /* Equivalent to a chmod() */ + ia.ia_valid = ATTR_MODE | ATTR_CTIME; + if (is_dir) { + ia.ia_mode = MSDOS_MKMODE(attr, + S_IRWXUGO & ~sbi->options.fs_dmask) + | S_IFDIR; + } else { + ia.ia_mode = MSDOS_MKMODE(attr, + (S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)) + & ~sbi->options.fs_fmask) + | S_IFREG; + } + + /* The root directory has no attributes */ + if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { + err = -EINVAL; + goto up; + } + + if (sbi->options.sys_immutable) { + if ((attr | oldattr) & ATTR_SYS) { + if (!capable(CAP_LINUX_IMMUTABLE)) { + err = -EPERM; + goto up; + } + } + } + + /* This MUST be done before doing anything irreversible... */ + err = notify_change(filp->f_dentry, &ia); + if (err) + goto up; + + if (sbi->options.sys_immutable) { + if (attr & ATTR_SYS) + inode->i_flags |= S_IMMUTABLE; + else + inode->i_flags &= S_IMMUTABLE; + } + + MSDOS_I(inode)->i_attrs = attr & ATTR_UNUSED; + mark_inode_dirty(inode); + up: + up(&inode->i_sem); + return err; + } + default: + return -ENOTTY; /* Inappropriate ioctl for device */ + } +} + +struct file_operations fat_file_operations = { + .llseek = generic_file_llseek, + .read = do_sync_read, + .write = do_sync_write, + .readv = generic_file_readv, + .writev = fat_file_writev, + .aio_read = generic_file_aio_read, + .aio_write = fat_file_aio_write, + .mmap = generic_file_mmap, + .ioctl = fat_generic_ioctl, + .fsync = file_fsync, + .sendfile = generic_file_sendfile, +}; + +int fat_notify_change(struct dentry *dentry, struct iattr *attr) +{ + struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); + struct inode *inode = dentry->d_inode; + int mask, error = 0; + + lock_kernel(); + + /* FAT cannot truncate to a longer file */ + if (attr->ia_valid & ATTR_SIZE) { + if (attr->ia_size > inode->i_size) { + error = -EPERM; + goto out; + } + } + + error = inode_change_ok(inode, attr); + if (error) { + if (sbi->options.quiet) + error = 0; + goto out; + } + if (((attr->ia_valid & ATTR_UID) && + (attr->ia_uid != sbi->options.fs_uid)) || + ((attr->ia_valid & ATTR_GID) && + (attr->ia_gid != sbi->options.fs_gid)) || + ((attr->ia_valid & ATTR_MODE) && + (attr->ia_mode & ~MSDOS_VALID_MODE))) + error = -EPERM; + + if (error) { + if (sbi->options.quiet) + error = 0; + goto out; + } + error = inode_setattr(inode, attr); + if (error) + goto out; + + if (S_ISDIR(inode->i_mode)) + mask = sbi->options.fs_dmask; + else + mask = sbi->options.fs_fmask; + inode->i_mode &= S_IFMT | (S_IRWXUGO & ~mask); +out: + unlock_kernel(); + return error; +} + +EXPORT_SYMBOL(fat_notify_change); + +/* Free all clusters after the skip'th cluster. */ +static int fat_free(struct inode *inode, int skip) +{ + struct super_block *sb = inode->i_sb; + int err, wait, free_start, i_start, i_logstart; + + if (MSDOS_I(inode)->i_start == 0) + return 0; + + /* + * Write a new EOF, and get the remaining cluster chain for freeing. + */ + wait = IS_DIRSYNC(inode); + if (skip) { + struct fat_entry fatent; + int ret, fclus, dclus; + + ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); + if (ret < 0) + return ret; + else if (ret == FAT_ENT_EOF) + return 0; + + fatent_init(&fatent); + ret = fat_ent_read(inode, &fatent, dclus); + if (ret == FAT_ENT_EOF) { + fatent_brelse(&fatent); + return 0; + } else if (ret == FAT_ENT_FREE) { + fat_fs_panic(sb, + "%s: invalid cluster chain (i_pos %lld)", + __FUNCTION__, MSDOS_I(inode)->i_pos); + ret = -EIO; + } else if (ret > 0) { + err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); + if (err) + ret = err; + } + fatent_brelse(&fatent); + if (ret < 0) + return ret; + + free_start = ret; + i_start = i_logstart = 0; + fat_cache_inval_inode(inode); + } else { + fat_cache_inval_inode(inode); + + i_start = free_start = MSDOS_I(inode)->i_start; + i_logstart = MSDOS_I(inode)->i_logstart; + MSDOS_I(inode)->i_start = 0; + MSDOS_I(inode)->i_logstart = 0; + } + MSDOS_I(inode)->i_attrs |= ATTR_ARCH; + inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; + if (wait) { + err = fat_sync_inode(inode); + if (err) + goto error; + } else + mark_inode_dirty(inode); + inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); + + /* Freeing the remained cluster chain */ + return fat_free_clusters(inode, free_start); + +error: + if (i_start) { + MSDOS_I(inode)->i_start = i_start; + MSDOS_I(inode)->i_logstart = i_logstart; + } + return err; +} + +void fat_truncate(struct inode *inode) +{ + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); + const unsigned int cluster_size = sbi->cluster_size; + int nr_clusters; + + /* + * This protects against truncating a file bigger than it was then + * trying to write into the hole. + */ + if (MSDOS_I(inode)->mmu_private > inode->i_size) + MSDOS_I(inode)->mmu_private = inode->i_size; + + nr_clusters = (inode->i_size + (cluster_size - 1)) >> sbi->cluster_bits; + + lock_kernel(); + fat_free(inode, nr_clusters); + unlock_kernel(); +} + +struct inode_operations fat_file_inode_operations = { + .truncate = fat_truncate, + .setattr = fat_notify_change, +}; diff --git a/fs/fat/inode.c b/fs/fat/inode.c new file mode 100644 index 000000000000..8ccee8415488 --- /dev/null +++ b/fs/fat/inode.c @@ -0,0 +1,1351 @@ +/* + * linux/fs/fat/inode.c + * + * Written 1992,1993 by Werner Almesberger + * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner + * Rewritten for the constant inumbers support by Al Viro + * + * Fixes: + * + * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0 + */ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/time.h> +#include <linux/slab.h> +#include <linux/smp_lock.h> +#include <linux/seq_file.h> +#include <linux/msdos_fs.h> +#include <linux/pagemap.h> +#include <linux/buffer_head.h> +#include <linux/mount.h> +#include <linux/vfs.h> +#include <linux/parser.h> +#include <asm/unaligned.h> + +#ifndef CONFIG_FAT_DEFAULT_IOCHARSET +/* if user don't select VFAT, this is undefined. */ +#define CONFIG_FAT_DEFAULT_IOCHARSET "" +#endif + +static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE; +static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET; + + +static int fat_add_cluster(struct inode *inode) +{ + int err, cluster; + + err = fat_alloc_clusters(inode, &cluster, 1); + if (err) + return err; + /* FIXME: this cluster should be added after data of this + * cluster is writed */ + err = fat_chain_add(inode, cluster, 1); + if (err) + fat_free_clusters(inode, cluster); + return err; +} + +static int fat_get_block(struct inode *inode, sector_t iblock, + struct buffer_head *bh_result, int create) +{ + struct super_block *sb = inode->i_sb; + sector_t phys; + int err; + + err = fat_bmap(inode, iblock, &phys); + if (err) + return err; + if (phys) { + map_bh(bh_result, sb, phys); + return 0; + } + if (!create) + return 0; + if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) { + fat_fs_panic(sb, "corrupted file size (i_pos %lld, %lld)", + MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private); + return -EIO; + } + if (!((unsigned long)iblock & (MSDOS_SB(sb)->sec_per_clus - 1))) { + err = fat_add_cluster(inode); + if (err) + return err; + } + MSDOS_I(inode)->mmu_private += sb->s_blocksize; + err = fat_bmap(inode, iblock, &phys); + if (err) + return err; + if (!phys) + BUG(); + set_buffer_new(bh_result); + map_bh(bh_result, sb, phys); + return 0; +} + +static int fat_writepage(struct page *page, struct writeback_control *wbc) +{ + return block_write_full_page(page, fat_get_block, wbc); +} + +static int fat_readpage(struct file *file, struct page *page) +{ + return block_read_full_page(page, fat_get_block); +} + +static int fat_prepare_write(struct file *file, struct page *page, + unsigned from, unsigned to) +{ + return cont_prepare_write(page, from, to, fat_get_block, + &MSDOS_I(page->mapping->host)->mmu_private); +} + +static sector_t _fat_bmap(struct address_space *mapping, sector_t block) +{ + return generic_block_bmap(mapping, block, fat_get_block); +} + +static struct address_space_operations fat_aops = { + .readpage = fat_readpage, + .writepage = fat_writepage, + .sync_page = block_sync_page, + .prepare_write = fat_prepare_write, + .commit_write = generic_commit_write, + .bmap = _fat_bmap +}; + +/* + * New FAT inode stuff. We do the following: + * a) i_ino is constant and has nothing with on-disk location. + * b) FAT manages its own cache of directory entries. + * c) *This* cache is indexed by on-disk location. + * d) inode has an associated directory entry, all right, but + * it may be unhashed. + * e) currently entries are stored within struct inode. That should + * change. + * f) we deal with races in the following way: + * 1. readdir() and lookup() do FAT-dir-cache lookup. + * 2. rename() unhashes the F-d-c entry and rehashes it in + * a new place. + * 3. unlink() and rmdir() unhash F-d-c entry. + * 4. fat_write_inode() checks whether the thing is unhashed. + * If it is we silently return. If it isn't we do bread(), + * check if the location is still valid and retry if it + * isn't. Otherwise we do changes. + * 5. Spinlock is used to protect hash/unhash/location check/lookup + * 6. fat_clear_inode() unhashes the F-d-c entry. + * 7. lookup() and readdir() do igrab() if they find a F-d-c entry + * and consider negative result as cache miss. + */ + +static void fat_hash_init(struct super_block *sb) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + int i; + + spin_lock_init(&sbi->inode_hash_lock); + for (i = 0; i < FAT_HASH_SIZE; i++) + INIT_HLIST_HEAD(&sbi->inode_hashtable[i]); +} + +static inline unsigned long fat_hash(struct super_block *sb, loff_t i_pos) +{ + unsigned long tmp = (unsigned long)i_pos | (unsigned long) sb; + tmp = tmp + (tmp >> FAT_HASH_BITS) + (tmp >> FAT_HASH_BITS * 2); + return tmp & FAT_HASH_MASK; +} + +void fat_attach(struct inode *inode, loff_t i_pos) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + + spin_lock(&sbi->inode_hash_lock); + MSDOS_I(inode)->i_pos = i_pos; + hlist_add_head(&MSDOS_I(inode)->i_fat_hash, + sbi->inode_hashtable + fat_hash(sb, i_pos)); + spin_unlock(&sbi->inode_hash_lock); +} + +EXPORT_SYMBOL(fat_attach); + +void fat_detach(struct inode *inode) +{ + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); + spin_lock(&sbi->inode_hash_lock); + MSDOS_I(inode)->i_pos = 0; + hlist_del_init(&MSDOS_I(inode)->i_fat_hash); + spin_unlock(&sbi->inode_hash_lock); +} + +EXPORT_SYMBOL(fat_detach); + +struct inode *fat_iget(struct super_block *sb, loff_t i_pos) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct hlist_head *head = sbi->inode_hashtable + fat_hash(sb, i_pos); + struct hlist_node *_p; + struct msdos_inode_info *i; + struct inode *inode = NULL; + + spin_lock(&sbi->inode_hash_lock); + hlist_for_each_entry(i, _p, head, i_fat_hash) { + BUG_ON(i->vfs_inode.i_sb != sb); + if (i->i_pos != i_pos) + continue; + inode = igrab(&i->vfs_inode); + if (inode) + break; + } + spin_unlock(&sbi->inode_hash_lock); + return inode; +} + +static int is_exec(unsigned char *extension) +{ + unsigned char *exe_extensions = "EXECOMBAT", *walk; + + for (walk = exe_extensions; *walk; walk += 3) + if (!strncmp(extension, walk, 3)) + return 1; + return 0; +} + +static int fat_calc_dir_size(struct inode *inode) +{ + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); + int ret, fclus, dclus; + + inode->i_size = 0; + if (MSDOS_I(inode)->i_start == 0) + return 0; + + ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); + if (ret < 0) + return ret; + inode->i_size = (fclus + 1) << sbi->cluster_bits; + + return 0; +} + +/* doesn't deal with root inode */ +static int fat_fill_inode(struct inode *inode, struct msdos_dir_entry *de) +{ + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); + int error; + + MSDOS_I(inode)->i_pos = 0; + inode->i_uid = sbi->options.fs_uid; + inode->i_gid = sbi->options.fs_gid; + inode->i_version++; + inode->i_generation = get_seconds(); + + if ((de->attr & ATTR_DIR) && !IS_FREE(de->name)) { + inode->i_generation &= ~1; + inode->i_mode = MSDOS_MKMODE(de->attr, + S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR; + inode->i_op = sbi->dir_ops; + inode->i_fop = &fat_dir_operations; + + MSDOS_I(inode)->i_start = le16_to_cpu(de->start); + if (sbi->fat_bits == 32) + MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16); + + MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; + error = fat_calc_dir_size(inode); + if (error < 0) + return error; + MSDOS_I(inode)->mmu_private = inode->i_size; + + inode->i_nlink = fat_subdirs(inode); + } else { /* not a directory */ + inode->i_generation |= 1; + inode->i_mode = MSDOS_MKMODE(de->attr, + ((sbi->options.showexec && + !is_exec(de->ext)) + ? S_IRUGO|S_IWUGO : S_IRWXUGO) + & ~sbi->options.fs_fmask) | S_IFREG; + MSDOS_I(inode)->i_start = le16_to_cpu(de->start); + if (sbi->fat_bits == 32) + MSDOS_I(inode)->i_start |= (le16_to_cpu(de->starthi) << 16); + + MSDOS_I(inode)->i_logstart = MSDOS_I(inode)->i_start; + inode->i_size = le32_to_cpu(de->size); + inode->i_op = &fat_file_inode_operations; + inode->i_fop = &fat_file_operations; + inode->i_mapping->a_ops = &fat_aops; + MSDOS_I(inode)->mmu_private = inode->i_size; + } + if (de->attr & ATTR_SYS) { + if (sbi->options.sys_immutable) + inode->i_flags |= S_IMMUTABLE; + } + MSDOS_I(inode)->i_attrs = de->attr & ATTR_UNUSED; + /* this is as close to the truth as we can get ... */ + inode->i_blksize = sbi->cluster_size; + inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) + & ~((loff_t)sbi->cluster_size - 1)) >> 9; + inode->i_mtime.tv_sec = inode->i_atime.tv_sec = + date_dos2unix(le16_to_cpu(de->time), le16_to_cpu(de->date)); + inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = 0; + if (sbi->options.isvfat) { + int secs = de->ctime_cs / 100; + int csecs = de->ctime_cs % 100; + inode->i_ctime.tv_sec = + date_dos2unix(le16_to_cpu(de->ctime), + le16_to_cpu(de->cdate)) + secs; + inode->i_ctime.tv_nsec = csecs * 10000000; + } else + inode->i_ctime = inode->i_mtime; + + return 0; +} + +struct inode *fat_build_inode(struct super_block *sb, + struct msdos_dir_entry *de, loff_t i_pos) +{ + struct inode *inode; + int err; + + inode = fat_iget(sb, i_pos); + if (inode) + goto out; + inode = new_inode(sb); + if (!inode) { + inode = ERR_PTR(-ENOMEM); + goto out; + } + inode->i_ino = iunique(sb, MSDOS_ROOT_INO); + inode->i_version = 1; + err = fat_fill_inode(inode, de); + if (err) { + iput(inode); + inode = ERR_PTR(err); + goto out; + } + fat_attach(inode, i_pos); + insert_inode_hash(inode); +out: + return inode; +} + +EXPORT_SYMBOL(fat_build_inode); + +static void fat_delete_inode(struct inode *inode) +{ + if (!is_bad_inode(inode)) { + inode->i_size = 0; + fat_truncate(inode); + } + clear_inode(inode); +} + +static void fat_clear_inode(struct inode *inode) +{ + struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); + + if (is_bad_inode(inode)) + return; + lock_kernel(); + spin_lock(&sbi->inode_hash_lock); + fat_cache_inval_inode(inode); + hlist_del_init(&MSDOS_I(inode)->i_fat_hash); + spin_unlock(&sbi->inode_hash_lock); + unlock_kernel(); +} + +static void fat_put_super(struct super_block *sb) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + + if (!(sb->s_flags & MS_RDONLY)) + fat_clusters_flush(sb); + + if (sbi->nls_disk) { + unload_nls(sbi->nls_disk); + sbi->nls_disk = NULL; + sbi->options.codepage = fat_default_codepage; + } + if (sbi->nls_io) { + unload_nls(sbi->nls_io); + sbi->nls_io = NULL; + } + if (sbi->options.iocharset != fat_default_iocharset) { + kfree(sbi->options.iocharset); + sbi->options.iocharset = fat_default_iocharset; + } + + sb->s_fs_info = NULL; + kfree(sbi); +} + +static kmem_cache_t *fat_inode_cachep; + +static struct inode *fat_alloc_inode(struct super_block *sb) +{ + struct msdos_inode_info *ei; + ei = kmem_cache_alloc(fat_inode_cachep, SLAB_KERNEL); + if (!ei) + return NULL; + return &ei->vfs_inode; +} + +static void fat_destroy_inode(struct inode *inode) +{ + kmem_cache_free(fat_inode_cachep, MSDOS_I(inode)); +} + +static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) +{ + struct msdos_inode_info *ei = (struct msdos_inode_info *)foo; + + if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == + SLAB_CTOR_CONSTRUCTOR) { + spin_lock_init(&ei->cache_lru_lock); + ei->nr_caches = 0; + ei->cache_valid_id = FAT_CACHE_VALID + 1; + INIT_LIST_HEAD(&ei->cache_lru); + INIT_HLIST_NODE(&ei->i_fat_hash); + inode_init_once(&ei->vfs_inode); + } +} + +static int __init fat_init_inodecache(void) +{ + fat_inode_cachep = kmem_cache_create("fat_inode_cache", + sizeof(struct msdos_inode_info), + 0, SLAB_RECLAIM_ACCOUNT, + init_once, NULL); + if (fat_inode_cachep == NULL) + return -ENOMEM; + return 0; +} + +static void __exit fat_destroy_inodecache(void) +{ + if (kmem_cache_destroy(fat_inode_cachep)) + printk(KERN_INFO "fat_inode_cache: not all structures were freed\n"); +} + +static int fat_remount(struct super_block *sb, int *flags, char *data) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + *flags |= MS_NODIRATIME | (sbi->options.isvfat ? 0 : MS_NOATIME); + return 0; +} + +static int fat_statfs(struct super_block *sb, struct kstatfs *buf) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + + /* If the count of free cluster is still unknown, counts it here. */ + if (sbi->free_clusters == -1) { + int err = fat_count_free_clusters(sb); + if (err) + return err; + } + + buf->f_type = sb->s_magic; + buf->f_bsize = sbi->cluster_size; + buf->f_blocks = sbi->max_cluster - FAT_START_ENT; + buf->f_bfree = sbi->free_clusters; + buf->f_bavail = sbi->free_clusters; + buf->f_namelen = sbi->options.isvfat ? 260 : 12; + + return 0; +} + +static int fat_write_inode(struct inode *inode, int wait) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *bh; + struct msdos_dir_entry *raw_entry; + loff_t i_pos; + int err = 0; + +retry: + i_pos = MSDOS_I(inode)->i_pos; + if (inode->i_ino == MSDOS_ROOT_INO || !i_pos) + return 0; + + lock_kernel(); + bh = sb_bread(sb, i_pos >> sbi->dir_per_block_bits); + if (!bh) { + printk(KERN_ERR "FAT: unable to read inode block " + "for updating (i_pos %lld)\n", i_pos); + err = -EIO; + goto out; + } + spin_lock(&sbi->inode_hash_lock); + if (i_pos != MSDOS_I(inode)->i_pos) { + spin_unlock(&sbi->inode_hash_lock); + brelse(bh); + unlock_kernel(); + goto retry; + } + + raw_entry = &((struct msdos_dir_entry *) (bh->b_data)) + [i_pos & (sbi->dir_per_block - 1)]; + if (S_ISDIR(inode->i_mode)) + raw_entry->size = 0; + else + raw_entry->size = cpu_to_le32(inode->i_size); + raw_entry->attr = fat_attr(inode); + raw_entry->start = cpu_to_le16(MSDOS_I(inode)->i_logstart); + raw_entry->starthi = cpu_to_le16(MSDOS_I(inode)->i_logstart >> 16); + fat_date_unix2dos(inode->i_mtime.tv_sec, &raw_entry->time, &raw_entry->date); + if (sbi->options.isvfat) { + fat_date_unix2dos(inode->i_ctime.tv_sec,&raw_entry->ctime,&raw_entry->cdate); + raw_entry->ctime_cs = (inode->i_ctime.tv_sec & 1) * 100 + + inode->i_ctime.tv_nsec / 10000000; + } + spin_unlock(&sbi->inode_hash_lock); + mark_buffer_dirty(bh); + if (wait) + err = sync_dirty_buffer(bh); + brelse(bh); +out: + unlock_kernel(); + return err; +} + +int fat_sync_inode(struct inode *inode) +{ + return fat_write_inode(inode, 1); +} + +EXPORT_SYMBOL(fat_sync_inode); + +static int fat_show_options(struct seq_file *m, struct vfsmount *mnt); +static struct super_operations fat_sops = { + .alloc_inode = fat_alloc_inode, + .destroy_inode = fat_destroy_inode, + .write_inode = fat_write_inode, + .delete_inode = fat_delete_inode, + .put_super = fat_put_super, + .statfs = fat_statfs, + .clear_inode = fat_clear_inode, + .remount_fs = fat_remount, + + .read_inode = make_bad_inode, + + .show_options = fat_show_options, +}; + +/* + * a FAT file handle with fhtype 3 is + * 0/ i_ino - for fast, reliable lookup if still in the cache + * 1/ i_generation - to see if i_ino is still valid + * bit 0 == 0 iff directory + * 2/ i_pos(8-39) - if ino has changed, but still in cache + * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos + * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc + * + * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum + * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits + * of i_logstart is used to store the directory entry offset. + */ + +static struct dentry * +fat_decode_fh(struct super_block *sb, __u32 *fh, int len, int fhtype, + int (*acceptable)(void *context, struct dentry *de), + void *context) +{ + if (fhtype != 3) + return ERR_PTR(-ESTALE); + if (len < 5) + return ERR_PTR(-ESTALE); + + return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, context); +} + +static struct dentry *fat_get_dentry(struct super_block *sb, void *inump) +{ + struct inode *inode = NULL; + struct dentry *result; + __u32 *fh = inump; + + inode = iget(sb, fh[0]); + if (!inode || is_bad_inode(inode) || inode->i_generation != fh[1]) { + if (inode) + iput(inode); + inode = NULL; + } + if (!inode) { + loff_t i_pos; + int i_logstart = fh[3] & 0x0fffffff; + + i_pos = (loff_t)fh[2] << 8; + i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28); + + /* try 2 - see if i_pos is in F-d-c + * require i_logstart to be the same + * Will fail if you truncate and then re-write + */ + + inode = fat_iget(sb, i_pos); + if (inode && MSDOS_I(inode)->i_logstart != i_logstart) { + iput(inode); + inode = NULL; + } + } + if (!inode) { + /* For now, do nothing + * What we could do is: + * follow the file starting at fh[4], and record + * the ".." entry, and the name of the fh[2] entry. + * The follow the ".." file finding the next step up. + * This way we build a path to the root of + * the tree. If this works, we lookup the path and so + * get this inode into the cache. + * Finally try the fat_iget lookup again + * If that fails, then weare totally out of luck + * But all that is for another day + */ + } + if (!inode) + return ERR_PTR(-ESTALE); + + + /* now to find a dentry. + * If possible, get a well-connected one + */ + result = d_alloc_anon(inode); + if (result == NULL) { + iput(inode); + return ERR_PTR(-ENOMEM); + } + result->d_op = sb->s_root->d_op; + return result; +} + +static int +fat_encode_fh(struct dentry *de, __u32 *fh, int *lenp, int connectable) +{ + int len = *lenp; + struct inode *inode = de->d_inode; + u32 ipos_h, ipos_m, ipos_l; + + if (len < 5) + return 255; /* no room */ + + ipos_h = MSDOS_I(inode)->i_pos >> 8; + ipos_m = (MSDOS_I(inode)->i_pos & 0xf0) << 24; + ipos_l = (MSDOS_I(inode)->i_pos & 0x0f) << 28; + *lenp = 5; + fh[0] = inode->i_ino; + fh[1] = inode->i_generation; + fh[2] = ipos_h; + fh[3] = ipos_m | MSDOS_I(inode)->i_logstart; + spin_lock(&de->d_lock); + fh[4] = ipos_l | MSDOS_I(de->d_parent->d_inode)->i_logstart; + spin_unlock(&de->d_lock); + return 3; +} + +static struct dentry *fat_get_parent(struct dentry *child) +{ + struct buffer_head *bh; + struct msdos_dir_entry *de; + loff_t i_pos; + struct dentry *parent; + struct inode *inode; + int err; + + lock_kernel(); + + err = fat_get_dotdot_entry(child->d_inode, &bh, &de, &i_pos); + if (err) { + parent = ERR_PTR(err); + goto out; + } + inode = fat_build_inode(child->d_sb, de, i_pos); + brelse(bh); + if (IS_ERR(inode)) { + parent = ERR_PTR(PTR_ERR(inode)); + goto out; + } + parent = d_alloc_anon(inode); + if (!parent) { + iput(inode); + parent = ERR_PTR(-ENOMEM); + } +out: + unlock_kernel(); + + return parent; +} + +static struct export_operations fat_export_ops = { + .decode_fh = fat_decode_fh, + .encode_fh = fat_encode_fh, + .get_dentry = fat_get_dentry, + .get_parent = fat_get_parent, +}; + +static int fat_show_options(struct seq_file *m, struct vfsmount *mnt) +{ + struct msdos_sb_info *sbi = MSDOS_SB(mnt->mnt_sb); + struct fat_mount_options *opts = &sbi->options; + int isvfat = opts->isvfat; + + if (opts->fs_uid != 0) + seq_printf(m, ",uid=%u", opts->fs_uid); + if (opts->fs_gid != 0) + seq_printf(m, ",gid=%u", opts->fs_gid); + seq_printf(m, ",fmask=%04o", opts->fs_fmask); + seq_printf(m, ",dmask=%04o", opts->fs_dmask); + if (sbi->nls_disk) + seq_printf(m, ",codepage=%s", sbi->nls_disk->charset); + if (isvfat) { + if (sbi->nls_io) + seq_printf(m, ",iocharset=%s", sbi->nls_io->charset); + + switch (opts->shortname) { + case VFAT_SFN_DISPLAY_WIN95 | VFAT_SFN_CREATE_WIN95: + seq_puts(m, ",shortname=win95"); + break; + case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WINNT: + seq_puts(m, ",shortname=winnt"); + break; + case VFAT_SFN_DISPLAY_WINNT | VFAT_SFN_CREATE_WIN95: + seq_puts(m, ",shortname=mixed"); + break; + case VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95: + /* seq_puts(m, ",shortname=lower"); */ + break; + default: + seq_puts(m, ",shortname=unknown"); + break; + } + } + if (opts->name_check != 'n') + seq_printf(m, ",check=%c", opts->name_check); + if (opts->quiet) + seq_puts(m, ",quiet"); + if (opts->showexec) + seq_puts(m, ",showexec"); + if (opts->sys_immutable) + seq_puts(m, ",sys_immutable"); + if (!isvfat) { + if (opts->dotsOK) + seq_puts(m, ",dotsOK=yes"); + if (opts->nocase) + seq_puts(m, ",nocase"); + } else { + if (opts->utf8) + seq_puts(m, ",utf8"); + if (opts->unicode_xlate) + seq_puts(m, ",uni_xlate"); + if (!opts->numtail) + seq_puts(m, ",nonumtail"); + } + + return 0; +} + +enum { + Opt_check_n, Opt_check_r, Opt_check_s, Opt_uid, Opt_gid, + Opt_umask, Opt_dmask, Opt_fmask, Opt_codepage, Opt_nocase, + Opt_quiet, Opt_showexec, Opt_debug, Opt_immutable, + Opt_dots, Opt_nodots, + Opt_charset, Opt_shortname_lower, Opt_shortname_win95, + Opt_shortname_winnt, Opt_shortname_mixed, Opt_utf8_no, Opt_utf8_yes, + Opt_uni_xl_no, Opt_uni_xl_yes, Opt_nonumtail_no, Opt_nonumtail_yes, + Opt_obsolate, Opt_err, +}; + +static match_table_t fat_tokens = { + {Opt_check_r, "check=relaxed"}, + {Opt_check_s, "check=strict"}, + {Opt_check_n, "check=normal"}, + {Opt_check_r, "check=r"}, + {Opt_check_s, "check=s"}, + {Opt_check_n, "check=n"}, + {Opt_uid, "uid=%u"}, + {Opt_gid, "gid=%u"}, + {Opt_umask, "umask=%o"}, + {Opt_dmask, "dmask=%o"}, + {Opt_fmask, "fmask=%o"}, + {Opt_codepage, "codepage=%u"}, + {Opt_nocase, "nocase"}, + {Opt_quiet, "quiet"}, + {Opt_showexec, "showexec"}, + {Opt_debug, "debug"}, + {Opt_immutable, "sys_immutable"}, + {Opt_obsolate, "conv=binary"}, + {Opt_obsolate, "conv=text"}, + {Opt_obsolate, "conv=auto"}, + {Opt_obsolate, "conv=b"}, + {Opt_obsolate, "conv=t"}, + {Opt_obsolate, "conv=a"}, + {Opt_obsolate, "fat=%u"}, + {Opt_obsolate, "blocksize=%u"}, + {Opt_obsolate, "cvf_format=%20s"}, + {Opt_obsolate, "cvf_options=%100s"}, + {Opt_obsolate, "posix"}, + {Opt_err, NULL} +}; +static match_table_t msdos_tokens = { + {Opt_nodots, "nodots"}, + {Opt_nodots, "dotsOK=no"}, + {Opt_dots, "dots"}, + {Opt_dots, "dotsOK=yes"}, + {Opt_err, NULL} +}; +static match_table_t vfat_tokens = { + {Opt_charset, "iocharset=%s"}, + {Opt_shortname_lower, "shortname=lower"}, + {Opt_shortname_win95, "shortname=win95"}, + {Opt_shortname_winnt, "shortname=winnt"}, + {Opt_shortname_mixed, "shortname=mixed"}, + {Opt_utf8_no, "utf8=0"}, /* 0 or no or false */ + {Opt_utf8_no, "utf8=no"}, + {Opt_utf8_no, "utf8=false"}, + {Opt_utf8_yes, "utf8=1"}, /* empty or 1 or yes or true */ + {Opt_utf8_yes, "utf8=yes"}, + {Opt_utf8_yes, "utf8=true"}, + {Opt_utf8_yes, "utf8"}, + {Opt_uni_xl_no, "uni_xlate=0"}, /* 0 or no or false */ + {Opt_uni_xl_no, "uni_xlate=no"}, + {Opt_uni_xl_no, "uni_xlate=false"}, + {Opt_uni_xl_yes, "uni_xlate=1"}, /* empty or 1 or yes or true */ + {Opt_uni_xl_yes, "uni_xlate=yes"}, + {Opt_uni_xl_yes, "uni_xlate=true"}, + {Opt_uni_xl_yes, "uni_xlate"}, + {Opt_nonumtail_no, "nonumtail=0"}, /* 0 or no or false */ + {Opt_nonumtail_no, "nonumtail=no"}, + {Opt_nonumtail_no, "nonumtail=false"}, + {Opt_nonumtail_yes, "nonumtail=1"}, /* empty or 1 or yes or true */ + {Opt_nonumtail_yes, "nonumtail=yes"}, + {Opt_nonumtail_yes, "nonumtail=true"}, + {Opt_nonumtail_yes, "nonumtail"}, + {Opt_err, NULL} +}; + +static int parse_options(char *options, int is_vfat, int *debug, + struct fat_mount_options *opts) +{ + char *p; + substring_t args[MAX_OPT_ARGS]; + int option; + char *iocharset; + + opts->isvfat = is_vfat; + + opts->fs_uid = current->uid; + opts->fs_gid = current->gid; + opts->fs_fmask = opts->fs_dmask = current->fs->umask; + opts->codepage = fat_default_codepage; + opts->iocharset = fat_default_iocharset; + if (is_vfat) + opts->shortname = VFAT_SFN_DISPLAY_LOWER|VFAT_SFN_CREATE_WIN95; + else + opts->shortname = 0; + opts->name_check = 'n'; + opts->quiet = opts->showexec = opts->sys_immutable = opts->dotsOK = 0; + opts->utf8 = opts->unicode_xlate = 0; + opts->numtail = 1; + opts->nocase = 0; + *debug = 0; + + if (!options) + return 0; + + while ((p = strsep(&options, ",")) != NULL) { + int token; + if (!*p) + continue; + + token = match_token(p, fat_tokens, args); + if (token == Opt_err) { + if (is_vfat) + token = match_token(p, vfat_tokens, args); + else + token = match_token(p, msdos_tokens, args); + } + switch (token) { + case Opt_check_s: + opts->name_check = 's'; + break; + case Opt_check_r: + opts->name_check = 'r'; + break; + case Opt_check_n: + opts->name_check = 'n'; + break; + case Opt_nocase: + if (!is_vfat) + opts->nocase = 1; + else { + /* for backward compatibility */ + opts->shortname = VFAT_SFN_DISPLAY_WIN95 + | VFAT_SFN_CREATE_WIN95; + } + break; + case Opt_quiet: + opts->quiet = 1; + break; + case Opt_showexec: + opts->showexec = 1; + break; + case Opt_debug: + *debug = 1; + break; + case Opt_immutable: + opts->sys_immutable = 1; + break; + case Opt_uid: + if (match_int(&args[0], &option)) + return 0; + opts->fs_uid = option; + break; + case Opt_gid: + if (match_int(&args[0], &option)) + return 0; + opts->fs_gid = option; + break; + case Opt_umask: + if (match_octal(&args[0], &option)) + return 0; + opts->fs_fmask = opts->fs_dmask = option; + break; + case Opt_dmask: + if (match_octal(&args[0], &option)) + return 0; + opts->fs_dmask = option; + break; + case Opt_fmask: + if (match_octal(&args[0], &option)) + return 0; + opts->fs_fmask = option; + break; + case Opt_codepage: + if (match_int(&args[0], &option)) + return 0; + opts->codepage = option; + break; + + /* msdos specific */ + case Opt_dots: + opts->dotsOK = 1; + break; + case Opt_nodots: + opts->dotsOK = 0; + break; + + /* vfat specific */ + case Opt_charset: + if (opts->iocharset != fat_default_iocharset) + kfree(opts->iocharset); + iocharset = match_strdup(&args[0]); + if (!iocharset) + return -ENOMEM; + opts->iocharset = iocharset; + break; + case Opt_shortname_lower: + opts->shortname = VFAT_SFN_DISPLAY_LOWER + | VFAT_SFN_CREATE_WIN95; + break; + case Opt_shortname_win95: + opts->shortname = VFAT_SFN_DISPLAY_WIN95 + | VFAT_SFN_CREATE_WIN95; + break; + case Opt_shortname_winnt: + opts->shortname = VFAT_SFN_DISPLAY_WINNT + | VFAT_SFN_CREATE_WINNT; + break; + case Opt_shortname_mixed: + opts->shortname = VFAT_SFN_DISPLAY_WINNT + | VFAT_SFN_CREATE_WIN95; + break; + case Opt_utf8_no: /* 0 or no or false */ + opts->utf8 = 0; + break; + case Opt_utf8_yes: /* empty or 1 or yes or true */ + opts->utf8 = 1; + break; + case Opt_uni_xl_no: /* 0 or no or false */ + opts->unicode_xlate = 0; + break; + case Opt_uni_xl_yes: /* empty or 1 or yes or true */ + opts->unicode_xlate = 1; + break; + case Opt_nonumtail_no: /* 0 or no or false */ + opts->numtail = 1; /* negated option */ + break; + case Opt_nonumtail_yes: /* empty or 1 or yes or true */ + opts->numtail = 0; /* negated option */ + break; + + /* obsolete mount options */ + case Opt_obsolate: + printk(KERN_INFO "FAT: \"%s\" option is obsolete, " + "not supported now\n", p); + break; + /* unknown option */ + default: + printk(KERN_ERR "FAT: Unrecognized mount option \"%s\" " + "or missing value\n", p); + return -EINVAL; + } + } + /* UTF8 doesn't provide FAT semantics */ + if (!strcmp(opts->iocharset, "utf8")) { + printk(KERN_ERR "FAT: utf8 is not a recommended IO charset" + " for FAT filesystems, filesystem will be case sensitive!\n"); + } + + if (opts->unicode_xlate) + opts->utf8 = 0; + + return 0; +} + +static int fat_read_root(struct inode *inode) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + int error; + + MSDOS_I(inode)->i_pos = 0; + inode->i_uid = sbi->options.fs_uid; + inode->i_gid = sbi->options.fs_gid; + inode->i_version++; + inode->i_generation = 0; + inode->i_mode = (S_IRWXUGO & ~sbi->options.fs_dmask) | S_IFDIR; + inode->i_op = sbi->dir_ops; + inode->i_fop = &fat_dir_operations; + if (sbi->fat_bits == 32) { + MSDOS_I(inode)->i_start = sbi->root_cluster; + error = fat_calc_dir_size(inode); + if (error < 0) + return error; + } else { + MSDOS_I(inode)->i_start = 0; + inode->i_size = sbi->dir_entries * sizeof(struct msdos_dir_entry); + } + inode->i_blksize = sbi->cluster_size; + inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) + & ~((loff_t)sbi->cluster_size - 1)) >> 9; + MSDOS_I(inode)->i_logstart = 0; + MSDOS_I(inode)->mmu_private = inode->i_size; + + MSDOS_I(inode)->i_attrs = ATTR_NONE; + inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec = 0; + inode->i_mtime.tv_nsec = inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = 0; + inode->i_nlink = fat_subdirs(inode)+2; + + return 0; +} + +/* + * Read the super block of an MS-DOS FS. + */ +int fat_fill_super(struct super_block *sb, void *data, int silent, + struct inode_operations *fs_dir_inode_ops, int isvfat) +{ + struct inode *root_inode = NULL; + struct buffer_head *bh; + struct fat_boot_sector *b; + struct msdos_sb_info *sbi; + u16 logical_sector_size; + u32 total_sectors, total_clusters, fat_clusters, rootdir_sectors; + int debug; + unsigned int media; + long error; + char buf[50]; + + sbi = kmalloc(sizeof(struct msdos_sb_info), GFP_KERNEL); + if (!sbi) + return -ENOMEM; + sb->s_fs_info = sbi; + memset(sbi, 0, sizeof(struct msdos_sb_info)); + + sb->s_flags |= MS_NODIRATIME; + sb->s_magic = MSDOS_SUPER_MAGIC; + sb->s_op = &fat_sops; + sb->s_export_op = &fat_export_ops; + sbi->dir_ops = fs_dir_inode_ops; + + error = parse_options(data, isvfat, &debug, &sbi->options); + if (error) + goto out_fail; + + error = -EIO; + sb_min_blocksize(sb, 512); + bh = sb_bread(sb, 0); + if (bh == NULL) { + printk(KERN_ERR "FAT: unable to read boot sector\n"); + goto out_fail; + } + + b = (struct fat_boot_sector *) bh->b_data; + if (!b->reserved) { + if (!silent) + printk(KERN_ERR "FAT: bogus number of reserved sectors\n"); + brelse(bh); + goto out_invalid; + } + if (!b->fats) { + if (!silent) + printk(KERN_ERR "FAT: bogus number of FAT structure\n"); + brelse(bh); + goto out_invalid; + } + + /* + * Earlier we checked here that b->secs_track and b->head are nonzero, + * but it turns out valid FAT filesystems can have zero there. + */ + + media = b->media; + if (!FAT_VALID_MEDIA(media)) { + if (!silent) + printk(KERN_ERR "FAT: invalid media value (0x%02x)\n", + media); + brelse(bh); + goto out_invalid; + } + logical_sector_size = + le16_to_cpu(get_unaligned((__le16 *)&b->sector_size)); + if (!logical_sector_size + || (logical_sector_size & (logical_sector_size - 1)) + || (logical_sector_size < 512) + || (PAGE_CACHE_SIZE < logical_sector_size)) { + if (!silent) + printk(KERN_ERR "FAT: bogus logical sector size %u\n", + logical_sector_size); + brelse(bh); + goto out_invalid; + } + sbi->sec_per_clus = b->sec_per_clus; + if (!sbi->sec_per_clus + || (sbi->sec_per_clus & (sbi->sec_per_clus - 1))) { + if (!silent) + printk(KERN_ERR "FAT: bogus sectors per cluster %u\n", + sbi->sec_per_clus); + brelse(bh); + goto out_invalid; + } + + if (logical_sector_size < sb->s_blocksize) { + printk(KERN_ERR "FAT: logical sector size too small for device" + " (logical sector size = %u)\n", logical_sector_size); + brelse(bh); + goto out_fail; + } + if (logical_sector_size > sb->s_blocksize) { + brelse(bh); + + if (!sb_set_blocksize(sb, logical_sector_size)) { + printk(KERN_ERR "FAT: unable to set blocksize %u\n", + logical_sector_size); + goto out_fail; + } + bh = sb_bread(sb, 0); + if (bh == NULL) { + printk(KERN_ERR "FAT: unable to read boot sector" + " (logical sector size = %lu)\n", + sb->s_blocksize); + goto out_fail; + } + b = (struct fat_boot_sector *) bh->b_data; + } + + sbi->cluster_size = sb->s_blocksize * sbi->sec_per_clus; + sbi->cluster_bits = ffs(sbi->cluster_size) - 1; + sbi->fats = b->fats; + sbi->fat_bits = 0; /* Don't know yet */ + sbi->fat_start = le16_to_cpu(b->reserved); + sbi->fat_length = le16_to_cpu(b->fat_length); + sbi->root_cluster = 0; + sbi->free_clusters = -1; /* Don't know yet */ + sbi->prev_free = FAT_START_ENT; + + if (!sbi->fat_length && b->fat32_length) { + struct fat_boot_fsinfo *fsinfo; + struct buffer_head *fsinfo_bh; + + /* Must be FAT32 */ + sbi->fat_bits = 32; + sbi->fat_length = le32_to_cpu(b->fat32_length); + sbi->root_cluster = le32_to_cpu(b->root_cluster); + + sb->s_maxbytes = 0xffffffff; + + /* MC - if info_sector is 0, don't multiply by 0 */ + sbi->fsinfo_sector = le16_to_cpu(b->info_sector); + if (sbi->fsinfo_sector == 0) + sbi->fsinfo_sector = 1; + + fsinfo_bh = sb_bread(sb, sbi->fsinfo_sector); + if (fsinfo_bh == NULL) { + printk(KERN_ERR "FAT: bread failed, FSINFO block" + " (sector = %lu)\n", sbi->fsinfo_sector); + brelse(bh); + goto out_fail; + } + + fsinfo = (struct fat_boot_fsinfo *)fsinfo_bh->b_data; + if (!IS_FSINFO(fsinfo)) { + printk(KERN_WARNING + "FAT: Did not find valid FSINFO signature.\n" + " Found signature1 0x%08x signature2 0x%08x" + " (sector = %lu)\n", + le32_to_cpu(fsinfo->signature1), + le32_to_cpu(fsinfo->signature2), + sbi->fsinfo_sector); + } else { + sbi->free_clusters = le32_to_cpu(fsinfo->free_clusters); + sbi->prev_free = le32_to_cpu(fsinfo->next_cluster); + } + + brelse(fsinfo_bh); + } + + sbi->dir_per_block = sb->s_blocksize / sizeof(struct msdos_dir_entry); + sbi->dir_per_block_bits = ffs(sbi->dir_per_block) - 1; + + sbi->dir_start = sbi->fat_start + sbi->fats * sbi->fat_length; + sbi->dir_entries = + le16_to_cpu(get_unaligned((__le16 *)&b->dir_entries)); + if (sbi->dir_entries & (sbi->dir_per_block - 1)) { + if (!silent) + printk(KERN_ERR "FAT: bogus directroy-entries per block" + " (%u)\n", sbi->dir_entries); + brelse(bh); + goto out_invalid; + } + + rootdir_sectors = sbi->dir_entries + * sizeof(struct msdos_dir_entry) / sb->s_blocksize; + sbi->data_start = sbi->dir_start + rootdir_sectors; + total_sectors = le16_to_cpu(get_unaligned((__le16 *)&b->sectors)); + if (total_sectors == 0) + total_sectors = le32_to_cpu(b->total_sect); + + total_clusters = (total_sectors - sbi->data_start) / sbi->sec_per_clus; + + if (sbi->fat_bits != 32) + sbi->fat_bits = (total_clusters > MAX_FAT12) ? 16 : 12; + + /* check that FAT table does not overflow */ + fat_clusters = sbi->fat_length * sb->s_blocksize * 8 / sbi->fat_bits; + total_clusters = min(total_clusters, fat_clusters - FAT_START_ENT); + if (total_clusters > MAX_FAT(sb)) { + if (!silent) + printk(KERN_ERR "FAT: count of clusters too big (%u)\n", + total_clusters); + brelse(bh); + goto out_invalid; + } + + sbi->max_cluster = total_clusters + FAT_START_ENT; + /* check the free_clusters, it's not necessarily correct */ + if (sbi->free_clusters != -1 && sbi->free_clusters > total_clusters) + sbi->free_clusters = -1; + /* check the prev_free, it's not necessarily correct */ + sbi->prev_free %= sbi->max_cluster; + if (sbi->prev_free < FAT_START_ENT) + sbi->prev_free = FAT_START_ENT; + + brelse(bh); + + /* set up enough so that it can read an inode */ + fat_hash_init(sb); + fat_ent_access_init(sb); + + /* + * The low byte of FAT's first entry must have same value with + * media-field. But in real world, too many devices is + * writing wrong value. So, removed that validity check. + * + * if (FAT_FIRST_ENT(sb, media) != first) + */ + + error = -EINVAL; + sprintf(buf, "cp%d", sbi->options.codepage); + sbi->nls_disk = load_nls(buf); + if (!sbi->nls_disk) { + printk(KERN_ERR "FAT: codepage %s not found\n", buf); + goto out_fail; + } + + /* FIXME: utf8 is using iocharset for upper/lower conversion */ + if (sbi->options.isvfat) { + sbi->nls_io = load_nls(sbi->options.iocharset); + if (!sbi->nls_io) { + printk(KERN_ERR "FAT: IO charset %s not found\n", + sbi->options.iocharset); + goto out_fail; + } + } + + error = -ENOMEM; + root_inode = new_inode(sb); + if (!root_inode) + goto out_fail; + root_inode->i_ino = MSDOS_ROOT_INO; + root_inode->i_version = 1; + error = fat_read_root(root_inode); + if (error < 0) + goto out_fail; + error = -ENOMEM; + insert_inode_hash(root_inode); + sb->s_root = d_alloc_root(root_inode); + if (!sb->s_root) { + printk(KERN_ERR "FAT: get root inode failed\n"); + goto out_fail; + } + + return 0; + +out_invalid: + error = -EINVAL; + if (!silent) + printk(KERN_INFO "VFS: Can't find a valid FAT filesystem" + " on dev %s.\n", sb->s_id); + +out_fail: + if (root_inode) + iput(root_inode); + if (sbi->nls_io) + unload_nls(sbi->nls_io); + if (sbi->nls_disk) + unload_nls(sbi->nls_disk); + if (sbi->options.iocharset != fat_default_iocharset) + kfree(sbi->options.iocharset); + sb->s_fs_info = NULL; + kfree(sbi); + return error; +} + +EXPORT_SYMBOL(fat_fill_super); + +int __init fat_cache_init(void); +void __exit fat_cache_destroy(void); + +static int __init init_fat_fs(void) +{ + int ret; + + ret = fat_cache_init(); + if (ret < 0) + return ret; + return fat_init_inodecache(); +} + +static void __exit exit_fat_fs(void) +{ + fat_cache_destroy(); + fat_destroy_inodecache(); +} + +module_init(init_fat_fs) +module_exit(exit_fat_fs) + +MODULE_LICENSE("GPL"); diff --git a/fs/fat/misc.c b/fs/fat/misc.c new file mode 100644 index 000000000000..2a0df2122f5d --- /dev/null +++ b/fs/fat/misc.c @@ -0,0 +1,225 @@ +/* + * linux/fs/fat/misc.c + * + * Written 1992,1993 by Werner Almesberger + * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980 + * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru) + */ + +#include <linux/module.h> +#include <linux/fs.h> +#include <linux/msdos_fs.h> +#include <linux/buffer_head.h> + +/* + * fat_fs_panic reports a severe file system problem and sets the file system + * read-only. The file system can be made writable again by remounting it. + */ +void fat_fs_panic(struct super_block *s, const char *fmt, ...) +{ + va_list args; + + printk(KERN_ERR "FAT: Filesystem panic (dev %s)\n", s->s_id); + + printk(KERN_ERR " "); + va_start(args, fmt); + vprintk(fmt, args); + va_end(args); + printk("\n"); + + if (!(s->s_flags & MS_RDONLY)) { + s->s_flags |= MS_RDONLY; + printk(KERN_ERR " File system has been set read-only\n"); + } +} + +EXPORT_SYMBOL(fat_fs_panic); + +/* Flushes the number of free clusters on FAT32 */ +/* XXX: Need to write one per FSINFO block. Currently only writes 1 */ +void fat_clusters_flush(struct super_block *sb) +{ + struct msdos_sb_info *sbi = MSDOS_SB(sb); + struct buffer_head *bh; + struct fat_boot_fsinfo *fsinfo; + + if (sbi->fat_bits != 32) + return; + + bh = sb_bread(sb, sbi->fsinfo_sector); + if (bh == NULL) { + printk(KERN_ERR "FAT: bread failed in fat_clusters_flush\n"); + return; + } + + fsinfo = (struct fat_boot_fsinfo *)bh->b_data; + /* Sanity check */ + if (!IS_FSINFO(fsinfo)) { + printk(KERN_ERR "FAT: Did not find valid FSINFO signature.\n" + " Found signature1 0x%08x signature2 0x%08x" + " (sector = %lu)\n", + le32_to_cpu(fsinfo->signature1), + le32_to_cpu(fsinfo->signature2), + sbi->fsinfo_sector); + } else { + if (sbi->free_clusters != -1) + fsinfo->free_clusters = cpu_to_le32(sbi->free_clusters); + if (sbi->prev_free != -1) + fsinfo->next_cluster = cpu_to_le32(sbi->prev_free); + mark_buffer_dirty(bh); + if (sb->s_flags & MS_SYNCHRONOUS) + sync_dirty_buffer(bh); + } + brelse(bh); +} + +/* + * fat_chain_add() adds a new cluster to the chain of clusters represented + * by inode. + */ +int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster) +{ + struct super_block *sb = inode->i_sb; + struct msdos_sb_info *sbi = MSDOS_SB(sb); + int ret, new_fclus, last; + + /* + * We must locate the last cluster of the file to add this new + * one (new_dclus) to the end of the link list (the FAT). + */ + last = new_fclus = 0; + if (MSDOS_I(inode)->i_start) { + int fclus, dclus; + + ret = fat_get_cluster(inode, FAT_ENT_EOF, &fclus, &dclus); + if (ret < 0) + return ret; + new_fclus = fclus + 1; + last = dclus; + } + + /* add new one to the last of the cluster chain */ + if (last) { + struct fat_entry fatent; + + fatent_init(&fatent); + ret = fat_ent_read(inode, &fatent, last); + if (ret >= 0) { + int wait = inode_needs_sync(inode); + ret = fat_ent_write(inode, &fatent, new_dclus, wait); + fatent_brelse(&fatent); + } + if (ret < 0) + return ret; +// fat_cache_add(inode, new_fclus, new_dclus); + } else { + MSDOS_I(inode)->i_start = new_dclus; + MSDOS_I(inode)->i_logstart = new_dclus; + /* + * Since generic_osync_inode() synchronize later if + * this is not directory, we don't here. + */ + if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) { + ret = fat_sync_inode(inode); + if (ret) + return ret; + } else + mark_inode_dirty(inode); + } + if (new_fclus != (inode->i_blocks >> (sbi->cluster_bits - 9))) { + fat_fs_panic(sb, "clusters badly computed (%d != %lu)", + new_fclus, inode->i_blocks >> (sbi->cluster_bits - 9)); + fat_cache_inval_inode(inode); + } + inode->i_blocks += nr_cluster << (sbi->cluster_bits - 9); + + return 0; +} + +extern struct timezone sys_tz; + +/* Linear day numbers of the respective 1sts in non-leap years. */ +static int day_n[] = { + /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */ + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0 +}; + +/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */ +int date_dos2unix(unsigned short time, unsigned short date) +{ + int month, year, secs; + + /* + * first subtract and mask after that... Otherwise, if + * date == 0, bad things happen + */ + month = ((date >> 5) - 1) & 15; + year = date >> 9; + secs = (time & 31)*2+60*((time >> 5) & 63)+(time >> 11)*3600+86400* + ((date & 31)-1+day_n[month]+(year/4)+year*365-((year & 3) == 0 && + month < 2 ? 1 : 0)+3653); + /* days since 1.1.70 plus 80's leap day */ + secs += sys_tz.tz_minuteswest*60; + return secs; +} + +/* Convert linear UNIX date to a MS-DOS time/date pair. */ +void fat_date_unix2dos(int unix_date, __le16 *time, __le16 *date) +{ + int day, year, nl_day, month; + + unix_date -= sys_tz.tz_minuteswest*60; + + /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */ + if (unix_date < 315532800) + unix_date = 315532800; + + *time = cpu_to_le16((unix_date % 60)/2+(((unix_date/60) % 60) << 5)+ + (((unix_date/3600) % 24) << 11)); + day = unix_date/86400-3652; + year = day/365; + if ((year+3)/4+365*year > day) + year--; + day -= (year+3)/4+365*year; + if (day == 59 && !(year & 3)) { + nl_day = day; + month = 2; + } else { + nl_day = (year & 3) || day <= 59 ? day : day-1; + for (month = 0; month < 12; month++) { + if (day_n[month] > nl_day) + break; + } + } + *date = cpu_to_le16(nl_day-day_n[month-1]+1+(month << 5)+(year << 9)); +} + +EXPORT_SYMBOL(fat_date_unix2dos); + +int fat_sync_bhs(struct buffer_head **bhs, int nr_bhs) +{ + int i, e, err = 0; + + for (i = 0; i < nr_bhs; i++) { + lock_buffer(bhs[i]); + if (test_clear_buffer_dirty(bhs[i])) { + get_bh(bhs[i]); + bhs[i]->b_end_io = end_buffer_write_sync; + e = submit_bh(WRITE, bhs[i]); + if (!err && e) + err = e; + } else + unlock_buffer(bhs[i]); + } + for (i = 0; i < nr_bhs; i++) { + wait_on_buffer(bhs[i]); + if (buffer_eopnotsupp(bhs[i])) { + clear_buffer_eopnotsupp(bhs[i]); + err = -EOPNOTSUPP; + } else if (!err && !buffer_uptodate(bhs[i])) + err = -EIO; + } + return err; +} + +EXPORT_SYMBOL(fat_sync_bhs); |