cregit-Linux how code gets into the kernel

Release 4.10 fs/fat/inode.c

Directory: fs/fat
/*
 *  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/pagemap.h>
#include <linux/mpage.h>
#include <linux/vfs.h>
#include <linux/seq_file.h>
#include <linux/parser.h>
#include <linux/uio.h>
#include <linux/blkdev.h>
#include <linux/backing-dev.h>
#include <asm/unaligned.h>
#include "fat.h"

#ifndef CONFIG_FAT_DEFAULT_IOCHARSET
/* if user don't select VFAT, this is undefined. */

#define CONFIG_FAT_DEFAULT_IOCHARSET	""
#endif


#define KB_IN_SECTORS 2

/*
 * A deserialized copy of the on-disk structure laid out in struct
 * fat_boot_sector.
 */

struct fat_bios_param_block {
	
u16	fat_sector_size;
	
u8	fat_sec_per_clus;
	
u16	fat_reserved;
	
u8	fat_fats;
	
u16	fat_dir_entries;
	
u16	fat_sectors;
	
u16	fat_fat_length;
	
u32	fat_total_sect;

	
u8	fat16_state;
	
u32	fat16_vol_id;

	
u32	fat32_length;
	
u32	fat32_root_cluster;
	
u16	fat32_info_sector;
	
u8	fat32_state;
	
u32	fat32_vol_id;
};


static int fat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;

static char fat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;


static struct fat_floppy_defaults {
	
unsigned nr_sectors;
	
unsigned sec_per_clus;
	
unsigned dir_entries;
	
unsigned media;
	
unsigned fat_length;
} 
floppy_defaults[] = {
{
	.nr_sectors = 160 * KB_IN_SECTORS,
	.sec_per_clus = 1,
	.dir_entries = 64,
	.media = 0xFE,
	.fat_length = 1,
},
{
	.nr_sectors = 180 * KB_IN_SECTORS,
	.sec_per_clus = 1,
	.dir_entries = 64,
	.media = 0xFC,
	.fat_length = 2,
},
{
	.nr_sectors = 320 * KB_IN_SECTORS,
	.sec_per_clus = 2,
	.dir_entries = 112,
	.media = 0xFF,
	.fat_length = 1,
},
{
	.nr_sectors = 360 * KB_IN_SECTORS,
	.sec_per_clus = 2,
	.dir_entries = 112,
	.media = 0xFD,
	.fat_length = 2,
},
};


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; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa60100.00%1100.00%
Total60100.00%1100.00%


static inline int __fat_get_block(struct inode *inode, sector_t iblock, unsigned long *max_blocks, struct buffer_head *bh_result, int create) { struct super_block *sb = inode->i_sb; struct msdos_sb_info *sbi = MSDOS_SB(sb); unsigned long mapped_blocks; sector_t phys, last_block; int err, offset; err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false); if (err) return err; if (phys) { map_bh(bh_result, sb, phys); *max_blocks = min(mapped_blocks, *max_blocks); return 0; } if (!create) return 0; if (iblock != MSDOS_I(inode)->mmu_private >> sb->s_blocksize_bits) { fat_fs_error(sb, "corrupted file size (i_pos %lld, %lld)", MSDOS_I(inode)->i_pos, MSDOS_I(inode)->mmu_private); return -EIO; } last_block = inode->i_blocks >> (sb->s_blocksize_bits - 9); offset = (unsigned long)iblock & (sbi->sec_per_clus - 1); /* * allocate a cluster according to the following. * 1) no more available blocks * 2) not part of fallocate region */ if (!offset && !(iblock < last_block)) { /* TODO: multiple cluster allocation would be desirable. */ err = fat_add_cluster(inode); if (err) return err; } /* available blocks on this cluster */ mapped_blocks = sbi->sec_per_clus - offset; *max_blocks = min(mapped_blocks, *max_blocks); MSDOS_I(inode)->mmu_private += *max_blocks << sb->s_blocksize_bits; err = fat_bmap(inode, iblock, &phys, &mapped_blocks, create, false); if (err) return err; BUG_ON(!phys); BUG_ON(*max_blocks != mapped_blocks); set_buffer_new(bh_result); map_bh(bh_result, sb, phys); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa22771.61%736.84%
pre-gitpre-git4915.46%736.84%
namjae jeonnamjae jeon288.83%210.53%
christoph hellwigchristoph hellwig113.47%15.26%
linus torvaldslinus torvalds10.32%15.26%
denis karpovdenis karpov10.32%15.26%
Total317100.00%19100.00%


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; unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; int err; err = __fat_get_block(inode, iblock, &max_blocks, bh_result, create); if (err) return err; bh_result->b_size = max_blocks << sb->s_blocksize_bits; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa7085.37%266.67%
badari pulavartybadari pulavarty1214.63%133.33%
Total82100.00%3100.00%


static int fat_writepage(struct page *page, struct writeback_control *wbc) { return block_write_full_page(page, fat_get_block, wbc); }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa1869.23%266.67%
pre-gitpre-git830.77%133.33%
Total26100.00%3100.00%


static int fat_writepages(struct address_space *mapping, struct writeback_control *wbc) { return mpage_writepages(mapping, wbc, fat_get_block); }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa26100.00%1100.00%
Total26100.00%1100.00%


static int fat_readpage(struct file *file, struct page *page) { return mpage_readpage(page, fat_get_block); }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa1354.17%240.00%
pre-gitpre-git1145.83%360.00%
Total24100.00%5100.00%


static int fat_readpages(struct file *file, struct address_space *mapping, struct list_head *pages, unsigned nr_pages) { return mpage_readpages(mapping, pages, nr_pages, fat_get_block); }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa36100.00%1100.00%
Total36100.00%1100.00%


static void fat_write_failed(struct address_space *mapping, loff_t to) { struct inode *inode = mapping->host; if (to > inode->i_size) { truncate_pagecache(inode, inode->i_size); fat_truncate_blocks(inode, inode->i_size); } }

Contributors

PersonTokensPropCommitsCommitProp
nick pigginnick piggin51100.00%1100.00%
Total51100.00%1100.00%


static int fat_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata) { int err; *pagep = NULL; err = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, fat_get_block, &MSDOS_I(mapping->host)->mmu_private); if (err < 0) fat_write_failed(mapping, pos + len); return err; }

Contributors

PersonTokensPropCommitsCommitProp
nick pigginnick piggin5862.37%233.33%
hirofumi ogawahirofumi ogawa2627.96%116.67%
pre-gitpre-git88.60%233.33%
christoph hellwigchristoph hellwig11.08%116.67%
Total93100.00%6100.00%


static int fat_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *pagep, void *fsdata) { struct inode *inode = mapping->host; int err; err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); if (err < len) fat_write_failed(mapping, pos + len); if (!(err < 0) && !(MSDOS_I(inode)->i_attrs & ATTR_ARCH)) { inode->i_mtime = inode->i_ctime = current_time(inode); MSDOS_I(inode)->i_attrs |= ATTR_ARCH; mark_inode_dirty(inode); } return err; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa7959.40%125.00%
nick pigginnick piggin5037.59%250.00%
deepa dinamanideepa dinamani43.01%125.00%
Total133100.00%4100.00%


static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; struct address_space *mapping = file->f_mapping; struct inode *inode = mapping->host; size_t count = iov_iter_count(iter); loff_t offset = iocb->ki_pos; ssize_t ret; if (iov_iter_rw(iter) == WRITE) { /* * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), * so we need to update the ->mmu_private to block boundary. * * But we must fill the remaining area or hole by nul for * updating ->mmu_private. * * Return 0, and fallback to normal buffered write. */ loff_t size = offset + count; if (MSDOS_I(inode)->mmu_private < size) return 0; } /* * FAT need to use the DIO_LOCKING for avoiding the race * condition of fat_get_block() and ->truncate(). */ ret = blockdev_direct_IO(iocb, inode, iter, fat_get_block); if (ret < 0 && iov_iter_rw(iter) == WRITE) fat_write_failed(mapping, offset + count); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa6951.88%330.00%
nick pigginnick piggin3425.56%220.00%
al viroal viro139.77%220.00%
omar sandovalomar sandoval96.77%110.00%
christoph hellwigchristoph hellwig86.02%220.00%
Total133100.00%10100.00%


static int fat_get_block_bmap(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create) { struct super_block *sb = inode->i_sb; unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; int err; sector_t bmap; unsigned long mapped_blocks; BUG_ON(create != 0); err = fat_bmap(inode, iblock, &bmap, &mapped_blocks, create, true); if (err) return err; if (bmap) { map_bh(bh_result, sb, bmap); max_blocks = min(mapped_blocks, max_blocks); } bh_result->b_size = max_blocks << sb->s_blocksize_bits; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
namjae jeonnamjae jeon123100.00%1100.00%
Total123100.00%1100.00%


static sector_t _fat_bmap(struct address_space *mapping, sector_t block) { sector_t blocknr; /* fat_get_cluster() assumes the requested blocknr isn't truncated. */ down_read(&MSDOS_I(mapping->host)->truncate_lock); blocknr = generic_block_bmap(mapping, block, fat_get_block_bmap); up_read(&MSDOS_I(mapping->host)->truncate_lock); return blocknr; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa4170.69%337.50%
christoph hellwigchristoph hellwig915.52%225.00%
pre-gitpre-git58.62%112.50%
linus torvaldslinus torvalds23.45%112.50%
namjae jeonnamjae jeon11.72%112.50%
Total58100.00%8100.00%

/* * fat_block_truncate_page() zeroes out a mapping from file offset `from' * up to the end of the block which corresponds to `from'. * This is required during truncate to physically zeroout the tail end * of that block so it doesn't yield old data if the file is later grown. * Also, avoid causing failure from fsx for cases of "data past EOF" */
int fat_block_truncate_page(struct inode *inode, loff_t from) { return block_truncate_page(inode->i_mapping, from, fat_get_block); }

Contributors

PersonTokensPropCommitsCommitProp
namjae jeonnamjae jeon25100.00%1100.00%
Total25100.00%1100.00%

static const struct address_space_operations fat_aops = { .readpage = fat_readpage, .readpages = fat_readpages, .writepage = fat_writepage, .writepages = fat_writepages, .write_begin = fat_write_begin, .write_end = fat_write_end, .direct_IO = fat_direct_IO, .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_evict_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]); }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa4682.14%233.33%
pre-gitpre-git712.50%233.33%
linus torvaldslinus torvalds23.57%116.67%
christoph hellwigchristoph hellwig11.79%116.67%
Total56100.00%6100.00%


static inline unsigned long fat_hash(loff_t i_pos) { return hash_32(i_pos, FAT_HASH_BITS); }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa19100.00%2100.00%
Total19100.00%2100.00%


static void dir_hash_init(struct super_block *sb) { struct msdos_sb_info *sbi = MSDOS_SB(sb); int i; spin_lock_init(&sbi->dir_hash_lock); for (i = 0; i < FAT_HASH_SIZE; i++) INIT_HLIST_HEAD(&sbi->dir_hashtable[i]); }

Contributors

PersonTokensPropCommitsCommitProp
steven j. magnanisteven j. magnani56100.00%1100.00%
Total56100.00%1100.00%


void fat_attach(struct inode *inode, loff_t i_pos) { struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); if (inode->i_ino != MSDOS_ROOT_INO) { struct hlist_head *head = sbi->inode_hashtable + fat_hash(i_pos); spin_lock(&sbi->inode_hash_lock); MSDOS_I(inode)->i_pos = i_pos; hlist_add_head(&MSDOS_I(inode)->i_fat_hash, head); spin_unlock(&sbi->inode_hash_lock); } /* If NFS support is enabled, cache the mapping of start cluster * to directory inode. This is used during reconnection of * dentries to the filesystem root. */ if (S_ISDIR(inode->i_mode) && sbi->options.nfs) { struct hlist_head *d_head = sbi->dir_hashtable; d_head += fat_dir_hash(MSDOS_I(inode)->i_logstart); spin_lock(&sbi->dir_hash_lock); hlist_add_head(&MSDOS_I(inode)->i_dir_hash, d_head); spin_unlock(&sbi->dir_hash_lock); } }

Contributors

PersonTokensPropCommitsCommitProp
steven j. magnanisteven j. magnani7850.32%116.67%
hirofumi ogawahirofumi ogawa5233.55%350.00%
pre-gitpre-git2516.13%233.33%
Total155100.00%6100.00%

EXPORT_SYMBOL_GPL(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); if (S_ISDIR(inode->i_mode) && sbi->options.nfs) { spin_lock(&sbi->dir_hash_lock); hlist_del_init(&MSDOS_I(inode)->i_dir_hash); spin_unlock(&sbi->dir_hash_lock); } }

Contributors

PersonTokensPropCommitsCommitProp
steven j. magnanisteven j. magnani4443.14%116.67%
hirofumi ogawahirofumi ogawa2827.45%233.33%
pre-gitpre-git2322.55%233.33%
brian gerstbrian gerst76.86%116.67%
Total102100.00%6100.00%

EXPORT_SYMBOL_GPL(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(i_pos); struct msdos_inode_info *i; struct inode *inode = NULL; spin_lock(&sbi->inode_hash_lock); hlist_for_each_entry(i, 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; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa11499.13%480.00%
linus torvaldslinus torvalds10.87%120.00%
Total115100.00%5100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa4892.31%250.00%
linus torvaldslinus torvalds35.77%125.00%
manuel schollingmanuel scholling11.92%125.00%
Total52100.00%4100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa91100.00%2100.00%
Total91100.00%2100.00%


static int fat_validate_dir(struct inode *dir) { struct super_block *sb = dir->i_sb; if (dir->i_nlink < 2) { /* Directory should have "."/".." entries at least. */ fat_fs_error(sb, "corrupted directory (invalid entries)"); return -EIO; } if (MSDOS_I(dir)->i_start == 0 || MSDOS_I(dir)->i_start == MSDOS_SB(sb)->root_cluster) { /* Directory should point valid cluster. */ fat_fs_error(sb, "corrupted directory (invalid i_start)"); return -EIO; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa84100.00%1100.00%
Total84100.00%1100.00%

/* doesn't deal with root inode */
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 = fat_make_mode(sbi, de->attr, S_IRWXUGO); inode->i_op = sbi->dir_ops; inode->i_fop = &fat_dir_operations; MSDOS_I(inode)->i_start = fat_get_start(sbi, de); 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; set_nlink(inode, fat_subdirs(inode)); error = fat_validate_dir(inode); if (error < 0) return error; } else { /* not a directory */ inode->i_generation |= 1; inode->i_mode = fat_make_mode(sbi, de->attr, ((sbi->options.showexec && !is_exec(de->name + 8)) ? S_IRUGO|S_IWUGO : S_IRWXUGO)); MSDOS_I(inode)->i_start = fat_get_start(sbi, de); 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; } fat_save_attrs(inode, de->attr); inode->i_blocks = ((inode->i_size + (sbi->cluster_size - 1)) & ~((loff_t)sbi->cluster_size - 1)) >> 9; fat_time_fat2unix(sbi, &inode->i_mtime, de->time, de->date, 0); if (sbi->options.isvfat) { fat_time_fat2unix(sbi, &inode->i_ctime, de->ctime, de->cdate, de->ctime_cs); fat_time_fat2unix(sbi, &inode->i_atime, 0, de->adate, 0); } else inode->i_ctime = inode->i_atime = inode->i_mtime; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa45694.21%758.33%
stephane kardasstephane kardas142.89%18.33%
steven j. magnanisteven j. magnani40.83%18.33%
joe petersonjoe peterson40.83%18.33%
miklos szeredimiklos szeredi40.83%18.33%
andrew mortonandrew morton20.41%18.33%
Total484100.00%12100.00%


static inline void fat_lock_build_inode(struct msdos_sb_info *sbi) { if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) mutex_lock(&sbi->nfs_build_inode_lock); }

Contributors

PersonTokensPropCommitsCommitProp
namjae jeonnamjae jeon30100.00%1100.00%
Total30100.00%1100.00%


static inline void fat_unlock_build_inode(struct msdos_sb_info *sbi) { if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) mutex_unlock(&sbi->nfs_build_inode_lock); }

Contributors

PersonTokensPropCommitsCommitProp
namjae jeonnamjae jeon30100.00%1100.00%
Total30100.00%1100.00%


struct inode *fat_build_inode(struct super_block *sb, struct msdos_dir_entry *de, loff_t i_pos) { struct inode *inode; int err; fat_lock_build_inode(MSDOS_SB(sb)); 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: fat_unlock_build_inode(MSDOS_SB(sb)); return inode; }

Contributors

PersonTokensPropCommitsCommitProp
hirofumi ogawahirofumi ogawa13389.26%375.00%
namjae jeonnamjae jeon1610.74%125.00%
Total149100.00%4100.00%

EXPORT_SYMBOL_GPL(fat_build_inode); static int __fat_write_inode(struct inode *