| // SPDX-License-Identifier: GPL-2.0-or-later |
| /* |
| * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. |
| */ |
| |
| #include <linux/module.h> |
| #include <linux/init.h> |
| #include <linux/time.h> |
| #include <linux/slab.h> |
| #include <linux/mm.h> |
| #include <linux/seq_file.h> |
| #include <linux/pagemap.h> |
| #include <linux/mpage.h> |
| #include <linux/buffer_head.h> |
| #include <linux/exportfs.h> |
| #include <linux/mount.h> |
| #include <linux/vfs.h> |
| #include <linux/aio.h> |
| #include <linux/iversion.h> |
| #include <linux/parser.h> |
| #include <linux/uio.h> |
| #include <linux/writeback.h> |
| #include <linux/log2.h> |
| #include <linux/hash.h> |
| #include <linux/backing-dev.h> |
| #include <linux/sched.h> |
| #include <linux/fs_struct.h> |
| #include <linux/namei.h> |
| |
| #include <linux/string.h> |
| #include <linux/nls.h> |
| #include <linux/mutex.h> |
| #include <linux/swap.h> |
| |
| #define EXFAT_VERSION "1.3.0" |
| |
| #include "exfat.h" |
| |
| static struct kmem_cache *exfat_inode_cachep; |
| |
| static int exfat_default_codepage = CONFIG_EXFAT_DEFAULT_CODEPAGE; |
| static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET; |
| |
| #define INC_IVERSION(x) (inode_inc_iversion(x)) |
| #define GET_IVERSION(x) (inode_peek_iversion_raw(x)) |
| #define SET_IVERSION(x, y) (inode_set_iversion(x, y)) |
| |
| static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos); |
| static int exfat_sync_inode(struct inode *inode); |
| static struct inode *exfat_build_inode(struct super_block *sb, |
| struct file_id_t *fid, loff_t i_pos); |
| static int exfat_write_inode(struct inode *inode, |
| struct writeback_control *wbc); |
| static void exfat_write_super(struct super_block *sb); |
| |
| #define UNIX_SECS_1980 315532800L |
| #define UNIX_SECS_2108 4354819200L |
| |
| /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */ |
| static void exfat_time_fat2unix(struct timespec64 *ts, struct date_time_t *tp) |
| { |
| ts->tv_sec = mktime64(tp->Year + 1980, tp->Month, tp->Day, |
| tp->Hour, tp->Minute, tp->Second); |
| |
| ts->tv_nsec = tp->MilliSecond * NSEC_PER_MSEC; |
| } |
| |
| /* Convert linear UNIX date to a FAT time/date pair. */ |
| static void exfat_time_unix2fat(struct timespec64 *ts, struct date_time_t *tp) |
| { |
| time64_t second = ts->tv_sec; |
| struct tm tm; |
| |
| time64_to_tm(second, 0, &tm); |
| |
| if (second < UNIX_SECS_1980) { |
| tp->MilliSecond = 0; |
| tp->Second = 0; |
| tp->Minute = 0; |
| tp->Hour = 0; |
| tp->Day = 1; |
| tp->Month = 1; |
| tp->Year = 0; |
| return; |
| } |
| |
| if (second >= UNIX_SECS_2108) { |
| tp->MilliSecond = 999; |
| tp->Second = 59; |
| tp->Minute = 59; |
| tp->Hour = 23; |
| tp->Day = 31; |
| tp->Month = 12; |
| tp->Year = 127; |
| return; |
| } |
| |
| tp->MilliSecond = ts->tv_nsec / NSEC_PER_MSEC; |
| tp->Second = tm.tm_sec; |
| tp->Minute = tm.tm_min; |
| tp->Hour = tm.tm_hour; |
| tp->Day = tm.tm_mday; |
| tp->Month = tm.tm_mon + 1; |
| tp->Year = tm.tm_year + 1900 - 1980; |
| } |
| |
| struct timestamp_t *tm_current(struct timestamp_t *tp) |
| { |
| time64_t second = ktime_get_real_seconds(); |
| struct tm tm; |
| |
| time64_to_tm(second, 0, &tm); |
| |
| if (second < UNIX_SECS_1980) { |
| tp->sec = 0; |
| tp->min = 0; |
| tp->hour = 0; |
| tp->day = 1; |
| tp->mon = 1; |
| tp->year = 0; |
| return tp; |
| } |
| |
| if (second >= UNIX_SECS_2108) { |
| tp->sec = 59; |
| tp->min = 59; |
| tp->hour = 23; |
| tp->day = 31; |
| tp->mon = 12; |
| tp->year = 127; |
| return tp; |
| } |
| |
| tp->sec = tm.tm_sec; |
| tp->min = tm.tm_min; |
| tp->hour = tm.tm_hour; |
| tp->day = tm.tm_mday; |
| tp->mon = tm.tm_mon + 1; |
| tp->year = tm.tm_year + 1900 - 1980; |
| |
| return tp; |
| } |
| |
| static void __lock_super(struct super_block *sb) |
| { |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| |
| mutex_lock(&sbi->s_lock); |
| } |
| |
| static void __unlock_super(struct super_block *sb) |
| { |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| |
| mutex_unlock(&sbi->s_lock); |
| } |
| |
| static int __is_sb_dirty(struct super_block *sb) |
| { |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| |
| return sbi->s_dirt; |
| } |
| |
| static void __set_sb_clean(struct super_block *sb) |
| { |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| |
| sbi->s_dirt = 0; |
| } |
| |
| static int __exfat_revalidate(struct dentry *dentry) |
| { |
| return 0; |
| } |
| |
| static int exfat_revalidate(struct dentry *dentry, unsigned int flags) |
| { |
| if (flags & LOOKUP_RCU) |
| return -ECHILD; |
| |
| if (dentry->d_inode) |
| return 1; |
| return __exfat_revalidate(dentry); |
| } |
| |
| static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags) |
| { |
| if (flags & LOOKUP_RCU) |
| return -ECHILD; |
| |
| if (dentry->d_inode) |
| return 1; |
| |
| if (!flags) |
| return 0; |
| |
| if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
| return 0; |
| |
| return __exfat_revalidate(dentry); |
| } |
| |
| static unsigned int __exfat_striptail_len(unsigned int len, const char *name) |
| { |
| while (len && name[len - 1] == '.') |
| len--; |
| return len; |
| } |
| |
| static unsigned int exfat_striptail_len(const struct qstr *qstr) |
| { |
| return __exfat_striptail_len(qstr->len, qstr->name); |
| } |
| |
| static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr) |
| { |
| qstr->hash = full_name_hash(dentry, qstr->name, |
| exfat_striptail_len(qstr)); |
| return 0; |
| } |
| |
| static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr) |
| { |
| struct super_block *sb = dentry->d_sb; |
| const unsigned char *name; |
| unsigned int len; |
| unsigned long hash; |
| |
| name = qstr->name; |
| len = exfat_striptail_len(qstr); |
| |
| hash = init_name_hash(dentry); |
| while (len--) |
| hash = partial_name_hash(nls_upper(sb, *name++), hash); |
| qstr->hash = end_name_hash(hash); |
| |
| return 0; |
| } |
| |
| static int exfat_cmpi(const struct dentry *dentry, unsigned int len, |
| const char *str, const struct qstr *name) |
| { |
| struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io; |
| unsigned int alen, blen; |
| |
| alen = exfat_striptail_len(name); |
| blen = __exfat_striptail_len(len, str); |
| if (alen == blen) { |
| if (!t) { |
| if (strncasecmp(name->name, str, alen) == 0) |
| return 0; |
| } else { |
| if (nls_strnicmp(t, name->name, str, alen) == 0) |
| return 0; |
| } |
| } |
| return 1; |
| } |
| |
| static int exfat_cmp(const struct dentry *dentry, unsigned int len, |
| const char *str, const struct qstr *name) |
| { |
| unsigned int alen, blen; |
| |
| alen = exfat_striptail_len(name); |
| blen = __exfat_striptail_len(len, str); |
| if (alen == blen) { |
| if (strncmp(name->name, str, alen) == 0) |
| return 0; |
| } |
| return 1; |
| } |
| |
| static const struct dentry_operations exfat_ci_dentry_ops = { |
| .d_revalidate = exfat_revalidate_ci, |
| .d_hash = exfat_d_hashi, |
| .d_compare = exfat_cmpi, |
| }; |
| |
| static const struct dentry_operations exfat_dentry_ops = { |
| .d_revalidate = exfat_revalidate, |
| .d_hash = exfat_d_hash, |
| .d_compare = exfat_cmp, |
| }; |
| |
| static DEFINE_SEMAPHORE(z_sem); |
| |
| static inline void fs_sync(struct super_block *sb, bool do_sync) |
| { |
| if (do_sync) |
| bdev_sync(sb); |
| } |
| |
| /* |
| * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to |
| * save ATTR_RO instead of ->i_mode. |
| * |
| * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only |
| * bit, it's just used as flag for app. |
| */ |
| static inline int exfat_mode_can_hold_ro(struct inode *inode) |
| { |
| struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); |
| |
| if (S_ISDIR(inode->i_mode)) |
| return 0; |
| |
| if ((~sbi->options.fs_fmask) & 0222) |
| return 1; |
| return 0; |
| } |
| |
| /* Convert attribute bits and a mask to the UNIX mode. */ |
| static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi, u32 attr, |
| mode_t mode) |
| { |
| if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR)) |
| mode &= ~0222; |
| |
| if (attr & ATTR_SUBDIR) |
| return (mode & ~sbi->options.fs_dmask) | S_IFDIR; |
| else if (attr & ATTR_SYMLINK) |
| return (mode & ~sbi->options.fs_dmask) | S_IFLNK; |
| else |
| return (mode & ~sbi->options.fs_fmask) | S_IFREG; |
| } |
| |
| /* Return the FAT attribute byte for this inode */ |
| static inline u32 exfat_make_attr(struct inode *inode) |
| { |
| if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222)) |
| return (EXFAT_I(inode)->fid.attr) | ATTR_READONLY; |
| else |
| return EXFAT_I(inode)->fid.attr; |
| } |
| |
| static inline void exfat_save_attr(struct inode *inode, u32 attr) |
| { |
| if (exfat_mode_can_hold_ro(inode)) |
| EXFAT_I(inode)->fid.attr = attr & ATTR_RWMASK; |
| else |
| EXFAT_I(inode)->fid.attr = attr & (ATTR_RWMASK | ATTR_READONLY); |
| } |
| |
| static int ffsMountVol(struct super_block *sb) |
| { |
| int i, ret; |
| struct pbr_sector_t *p_pbr; |
| struct buffer_head *tmp_bh = NULL; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info); |
| |
| pr_info("[EXFAT] trying to mount...\n"); |
| |
| down(&z_sem); |
| |
| buf_init(sb); |
| |
| sema_init(&p_fs->v_sem, 1); |
| p_fs->dev_ejected = 0; |
| |
| /* open the block device */ |
| bdev_open(sb); |
| |
| if (p_bd->sector_size < sb->s_blocksize) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| if (p_bd->sector_size > sb->s_blocksize) |
| sb_set_blocksize(sb, p_bd->sector_size); |
| |
| /* read Sector 0 */ |
| if (sector_read(sb, 0, &tmp_bh, 1) != FFS_SUCCESS) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| |
| p_fs->PBR_sector = 0; |
| |
| p_pbr = (struct pbr_sector_t *)tmp_bh->b_data; |
| |
| /* check the validity of PBR */ |
| if (GET16_A(p_pbr->signature) != PBR_SIGNATURE) { |
| brelse(tmp_bh); |
| bdev_close(sb); |
| ret = FFS_FORMATERR; |
| goto out; |
| } |
| |
| /* fill fs_struct */ |
| for (i = 0; i < 53; i++) |
| if (p_pbr->bpb[i]) |
| break; |
| |
| if (i < 53) { |
| #ifdef CONFIG_EXFAT_DONT_MOUNT_VFAT |
| ret = -EINVAL; |
| printk(KERN_INFO "EXFAT: Attempted to mount VFAT filesystem\n"); |
| goto out; |
| #else |
| if (GET16(p_pbr->bpb + 11)) /* num_fat_sectors */ |
| ret = fat16_mount(sb, p_pbr); |
| else |
| ret = fat32_mount(sb, p_pbr); |
| #endif |
| } else { |
| ret = exfat_mount(sb, p_pbr); |
| } |
| |
| brelse(tmp_bh); |
| |
| if (ret) { |
| bdev_close(sb); |
| goto out; |
| } |
| |
| if (p_fs->vol_type == EXFAT) { |
| ret = load_alloc_bitmap(sb); |
| if (ret) { |
| bdev_close(sb); |
| goto out; |
| } |
| ret = load_upcase_table(sb); |
| if (ret) { |
| free_alloc_bitmap(sb); |
| bdev_close(sb); |
| goto out; |
| } |
| } |
| |
| if (p_fs->dev_ejected) { |
| if (p_fs->vol_type == EXFAT) { |
| free_upcase_table(sb); |
| free_alloc_bitmap(sb); |
| } |
| bdev_close(sb); |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| |
| pr_info("[EXFAT] mounted successfully\n"); |
| |
| out: |
| up(&z_sem); |
| |
| return ret; |
| } |
| |
| static int ffsUmountVol(struct super_block *sb) |
| { |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| int err = FFS_SUCCESS; |
| |
| pr_info("[EXFAT] trying to unmount...\n"); |
| |
| down(&z_sem); |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| |
| if (p_fs->vol_type == EXFAT) { |
| free_upcase_table(sb); |
| free_alloc_bitmap(sb); |
| } |
| |
| FAT_release_all(sb); |
| buf_release_all(sb); |
| |
| /* close the block device */ |
| bdev_close(sb); |
| |
| if (p_fs->dev_ejected) { |
| pr_info("[EXFAT] unmounted with media errors. Device is already ejected.\n"); |
| err = FFS_MEDIAERR; |
| } |
| |
| buf_shutdown(sb); |
| |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| up(&z_sem); |
| |
| pr_info("[EXFAT] unmounted successfully\n"); |
| |
| return err; |
| } |
| |
| static int ffsGetVolInfo(struct super_block *sb, struct vol_info_t *info) |
| { |
| int err = FFS_SUCCESS; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| |
| /* check the validity of pointer parameters */ |
| if (!info) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| if (p_fs->used_clusters == UINT_MAX) |
| p_fs->used_clusters = p_fs->fs_func->count_used_clusters(sb); |
| |
| info->FatType = p_fs->vol_type; |
| info->ClusterSize = p_fs->cluster_size; |
| info->NumClusters = p_fs->num_clusters - 2; /* clu 0 & 1 */ |
| info->UsedClusters = p_fs->used_clusters; |
| info->FreeClusters = info->NumClusters - info->UsedClusters; |
| |
| if (p_fs->dev_ejected) |
| err = FFS_MEDIAERR; |
| |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return err; |
| } |
| |
| static int ffsSyncVol(struct super_block *sb, bool do_sync) |
| { |
| int err = FFS_SUCCESS; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* synchronize the file system */ |
| fs_sync(sb, do_sync); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| |
| if (p_fs->dev_ejected) |
| err = FFS_MEDIAERR; |
| |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return err; |
| } |
| |
| /*----------------------------------------------------------------------*/ |
| /* File Operation Functions */ |
| /*----------------------------------------------------------------------*/ |
| |
| static int ffsLookupFile(struct inode *inode, char *path, struct file_id_t *fid) |
| { |
| int ret, dentry, num_entries; |
| struct chain_t dir; |
| struct uni_name_t uni_name; |
| struct dos_name_t dos_name; |
| struct dentry_t *ep, *ep2; |
| struct entry_set_cache_t *es = NULL; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| /* check the validity of pointer parameters */ |
| if (!fid || !path || (*path == '\0')) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* check the validity of directory name in the given pathname */ |
| ret = resolve_path(inode, path, &dir, &uni_name); |
| if (ret) |
| goto out; |
| |
| ret = get_num_entries_and_dos_name(sb, &dir, &uni_name, &num_entries, |
| &dos_name); |
| if (ret) |
| goto out; |
| |
| /* search the file name for directories */ |
| dentry = p_fs->fs_func->find_dir_entry(sb, &dir, &uni_name, num_entries, |
| &dos_name, TYPE_ALL); |
| if (dentry < -1) { |
| ret = FFS_NOTFOUND; |
| goto out; |
| } |
| |
| fid->dir.dir = dir.dir; |
| fid->dir.size = dir.size; |
| fid->dir.flags = dir.flags; |
| fid->entry = dentry; |
| |
| if (dentry == -1) { |
| fid->type = TYPE_DIR; |
| fid->rwoffset = 0; |
| fid->hint_last_off = -1; |
| |
| fid->attr = ATTR_SUBDIR; |
| fid->flags = 0x01; |
| fid->size = 0; |
| fid->start_clu = p_fs->root_dir; |
| } else { |
| if (p_fs->vol_type == EXFAT) { |
| es = get_entry_set_in_dir(sb, &dir, dentry, |
| ES_2_ENTRIES, &ep); |
| if (!es) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep + 1; |
| } else { |
| ep = get_entry_in_dir(sb, &dir, dentry, NULL); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep; |
| } |
| |
| fid->type = p_fs->fs_func->get_entry_type(ep); |
| fid->rwoffset = 0; |
| fid->hint_last_off = -1; |
| fid->attr = p_fs->fs_func->get_entry_attr(ep); |
| |
| fid->size = p_fs->fs_func->get_entry_size(ep2); |
| if ((fid->type == TYPE_FILE) && (fid->size == 0)) { |
| fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01; |
| fid->start_clu = CLUSTER_32(~0); |
| } else { |
| fid->flags = p_fs->fs_func->get_entry_flag(ep2); |
| fid->start_clu = p_fs->fs_func->get_entry_clu0(ep2); |
| } |
| |
| if (p_fs->vol_type == EXFAT) |
| release_entry_set(es); |
| } |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static int ffsCreateFile(struct inode *inode, char *path, u8 mode, |
| struct file_id_t *fid) |
| { |
| struct chain_t dir; |
| struct uni_name_t uni_name; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| int ret; |
| |
| /* check the validity of pointer parameters */ |
| if (!fid || !path || (*path == '\0')) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* check the validity of directory name in the given pathname */ |
| ret = resolve_path(inode, path, &dir, &uni_name); |
| if (ret) |
| goto out; |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| /* create a new file */ |
| ret = create_file(inode, &dir, &uni_name, mode, fid); |
| |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static int ffsReadFile(struct inode *inode, struct file_id_t *fid, void *buffer, |
| u64 count, u64 *rcount) |
| { |
| s32 offset, sec_offset, clu_offset; |
| u32 clu; |
| int ret = 0; |
| sector_t LogSector; |
| u64 oneblkread, read_bytes; |
| struct buffer_head *tmp_bh = NULL; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info); |
| |
| /* check the validity of the given file id */ |
| if (!fid) |
| return FFS_INVALIDFID; |
| |
| /* check the validity of pointer parameters */ |
| if (!buffer) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* check if the given file ID is opened */ |
| if (fid->type != TYPE_FILE) { |
| ret = FFS_PERMISSIONERR; |
| goto out; |
| } |
| |
| if (fid->rwoffset > fid->size) |
| fid->rwoffset = fid->size; |
| |
| if (count > (fid->size - fid->rwoffset)) |
| count = fid->size - fid->rwoffset; |
| |
| if (count == 0) { |
| if (rcount) |
| *rcount = 0; |
| ret = FFS_EOF; |
| goto out; |
| } |
| |
| read_bytes = 0; |
| |
| while (count > 0) { |
| clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits); |
| clu = fid->start_clu; |
| |
| if (fid->flags == 0x03) { |
| clu += clu_offset; |
| } else { |
| /* hint information */ |
| if ((clu_offset > 0) && (fid->hint_last_off > 0) && |
| (clu_offset >= fid->hint_last_off)) { |
| clu_offset -= fid->hint_last_off; |
| clu = fid->hint_last_clu; |
| } |
| |
| while (clu_offset > 0) { |
| /* clu = FAT_read(sb, clu); */ |
| if (FAT_read(sb, clu, &clu) == -1) |
| return FFS_MEDIAERR; |
| |
| clu_offset--; |
| } |
| } |
| |
| /* hint information */ |
| fid->hint_last_off = (s32)(fid->rwoffset >> |
| p_fs->cluster_size_bits); |
| fid->hint_last_clu = clu; |
| |
| /* byte offset in cluster */ |
| offset = (s32)(fid->rwoffset & (p_fs->cluster_size - 1)); |
| |
| /* sector offset in cluster */ |
| sec_offset = offset >> p_bd->sector_size_bits; |
| |
| /* byte offset in sector */ |
| offset &= p_bd->sector_size_mask; |
| |
| LogSector = START_SECTOR(clu) + sec_offset; |
| |
| oneblkread = (u64)(p_bd->sector_size - offset); |
| if (oneblkread > count) |
| oneblkread = count; |
| |
| if ((offset == 0) && (oneblkread == p_bd->sector_size)) { |
| if (sector_read(sb, LogSector, &tmp_bh, 1) != |
| FFS_SUCCESS) |
| goto err_out; |
| memcpy((char *)buffer + read_bytes, |
| (char *)tmp_bh->b_data, (s32)oneblkread); |
| } else { |
| if (sector_read(sb, LogSector, &tmp_bh, 1) != |
| FFS_SUCCESS) |
| goto err_out; |
| memcpy((char *)buffer + read_bytes, |
| (char *)tmp_bh->b_data + offset, |
| (s32)oneblkread); |
| } |
| count -= oneblkread; |
| read_bytes += oneblkread; |
| fid->rwoffset += oneblkread; |
| } |
| brelse(tmp_bh); |
| |
| /* How did this ever work and not leak a brlse()?? */ |
| err_out: |
| /* set the size of read bytes */ |
| if (rcount) |
| *rcount = read_bytes; |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static int ffsWriteFile(struct inode *inode, struct file_id_t *fid, |
| void *buffer, u64 count, u64 *wcount) |
| { |
| bool modified = false; |
| s32 offset, sec_offset, clu_offset; |
| s32 num_clusters, num_alloc, num_alloced = (s32)~0; |
| int ret = 0; |
| u32 clu, last_clu; |
| sector_t LogSector, sector = 0; |
| u64 oneblkwrite, write_bytes; |
| struct chain_t new_clu; |
| struct timestamp_t tm; |
| struct dentry_t *ep, *ep2; |
| struct entry_set_cache_t *es = NULL; |
| struct buffer_head *tmp_bh = NULL; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info); |
| |
| /* check the validity of the given file id */ |
| if (!fid) |
| return FFS_INVALIDFID; |
| |
| /* check the validity of pointer parameters */ |
| if (!buffer) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* check if the given file ID is opened */ |
| if (fid->type != TYPE_FILE) { |
| ret = FFS_PERMISSIONERR; |
| goto out; |
| } |
| |
| if (fid->rwoffset > fid->size) |
| fid->rwoffset = fid->size; |
| |
| if (count == 0) { |
| if (wcount) |
| *wcount = 0; |
| ret = FFS_SUCCESS; |
| goto out; |
| } |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| if (fid->size == 0) |
| num_clusters = 0; |
| else |
| num_clusters = (s32)((fid->size - 1) >> |
| p_fs->cluster_size_bits) + 1; |
| |
| write_bytes = 0; |
| |
| while (count > 0) { |
| clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits); |
| clu = last_clu = fid->start_clu; |
| |
| if (fid->flags == 0x03) { |
| if ((clu_offset > 0) && (clu != CLUSTER_32(~0))) { |
| last_clu += clu_offset - 1; |
| |
| if (clu_offset == num_clusters) |
| clu = CLUSTER_32(~0); |
| else |
| clu += clu_offset; |
| } |
| } else { |
| /* hint information */ |
| if ((clu_offset > 0) && (fid->hint_last_off > 0) && |
| (clu_offset >= fid->hint_last_off)) { |
| clu_offset -= fid->hint_last_off; |
| clu = fid->hint_last_clu; |
| } |
| |
| while ((clu_offset > 0) && (clu != CLUSTER_32(~0))) { |
| last_clu = clu; |
| /* clu = FAT_read(sb, clu); */ |
| if (FAT_read(sb, clu, &clu) == -1) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| clu_offset--; |
| } |
| } |
| |
| if (clu == CLUSTER_32(~0)) { |
| num_alloc = (s32)((count - 1) >> |
| p_fs->cluster_size_bits) + 1; |
| new_clu.dir = (last_clu == CLUSTER_32(~0)) ? |
| CLUSTER_32(~0) : last_clu + 1; |
| new_clu.size = 0; |
| new_clu.flags = fid->flags; |
| |
| /* (1) allocate a chain of clusters */ |
| num_alloced = p_fs->fs_func->alloc_cluster(sb, |
| num_alloc, |
| &new_clu); |
| if (num_alloced == 0) |
| break; |
| if (num_alloced < 0) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| |
| /* (2) append to the FAT chain */ |
| if (last_clu == CLUSTER_32(~0)) { |
| if (new_clu.flags == 0x01) |
| fid->flags = 0x01; |
| fid->start_clu = new_clu.dir; |
| modified = true; |
| } else { |
| if (new_clu.flags != fid->flags) { |
| exfat_chain_cont_cluster(sb, |
| fid->start_clu, |
| num_clusters); |
| fid->flags = 0x01; |
| modified = true; |
| } |
| if (new_clu.flags == 0x01) |
| FAT_write(sb, last_clu, new_clu.dir); |
| } |
| |
| num_clusters += num_alloced; |
| clu = new_clu.dir; |
| } |
| |
| /* hint information */ |
| fid->hint_last_off = (s32)(fid->rwoffset >> |
| p_fs->cluster_size_bits); |
| fid->hint_last_clu = clu; |
| |
| /* byte offset in cluster */ |
| offset = (s32)(fid->rwoffset & (p_fs->cluster_size - 1)); |
| |
| /* sector offset in cluster */ |
| sec_offset = offset >> p_bd->sector_size_bits; |
| |
| /* byte offset in sector */ |
| offset &= p_bd->sector_size_mask; |
| |
| LogSector = START_SECTOR(clu) + sec_offset; |
| |
| oneblkwrite = (u64)(p_bd->sector_size - offset); |
| if (oneblkwrite > count) |
| oneblkwrite = count; |
| |
| if ((offset == 0) && (oneblkwrite == p_bd->sector_size)) { |
| if (sector_read(sb, LogSector, &tmp_bh, 0) != |
| FFS_SUCCESS) |
| goto err_out; |
| memcpy((char *)tmp_bh->b_data, |
| (char *)buffer + write_bytes, (s32)oneblkwrite); |
| if (sector_write(sb, LogSector, tmp_bh, 0) != |
| FFS_SUCCESS) { |
| brelse(tmp_bh); |
| goto err_out; |
| } |
| } else { |
| if ((offset > 0) || |
| ((fid->rwoffset + oneblkwrite) < fid->size)) { |
| if (sector_read(sb, LogSector, &tmp_bh, 1) != |
| FFS_SUCCESS) |
| goto err_out; |
| } else { |
| if (sector_read(sb, LogSector, &tmp_bh, 0) != |
| FFS_SUCCESS) |
| goto err_out; |
| } |
| |
| memcpy((char *)tmp_bh->b_data + offset, |
| (char *)buffer + write_bytes, (s32)oneblkwrite); |
| if (sector_write(sb, LogSector, tmp_bh, 0) != |
| FFS_SUCCESS) { |
| brelse(tmp_bh); |
| goto err_out; |
| } |
| } |
| |
| count -= oneblkwrite; |
| write_bytes += oneblkwrite; |
| fid->rwoffset += oneblkwrite; |
| |
| fid->attr |= ATTR_ARCHIVE; |
| |
| if (fid->size < fid->rwoffset) { |
| fid->size = fid->rwoffset; |
| modified = true; |
| } |
| } |
| |
| brelse(tmp_bh); |
| |
| /* (3) update the direcoty entry */ |
| if (p_fs->vol_type == EXFAT) { |
| es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, |
| ES_ALL_ENTRIES, &ep); |
| if (!es) |
| goto err_out; |
| ep2 = ep + 1; |
| } else { |
| ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or); |
| if (!ep) |
| goto err_out; |
| ep2 = ep; |
| } |
| |
| p_fs->fs_func->set_entry_time(ep, tm_current(&tm), TM_MODIFY); |
| p_fs->fs_func->set_entry_attr(ep, fid->attr); |
| |
| if (p_fs->vol_type != EXFAT) |
| buf_modify(sb, sector); |
| |
| if (modified) { |
| if (p_fs->fs_func->get_entry_flag(ep2) != fid->flags) |
| p_fs->fs_func->set_entry_flag(ep2, fid->flags); |
| |
| if (p_fs->fs_func->get_entry_size(ep2) != fid->size) |
| p_fs->fs_func->set_entry_size(ep2, fid->size); |
| |
| if (p_fs->fs_func->get_entry_clu0(ep2) != fid->start_clu) |
| p_fs->fs_func->set_entry_clu0(ep2, fid->start_clu); |
| |
| if (p_fs->vol_type != EXFAT) |
| buf_modify(sb, sector); |
| } |
| |
| if (p_fs->vol_type == EXFAT) { |
| update_dir_checksum_with_entry_set(sb, es); |
| release_entry_set(es); |
| } |
| |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| err_out: |
| /* set the size of written bytes */ |
| if (wcount) |
| *wcount = write_bytes; |
| |
| if (num_alloced == 0) |
| ret = FFS_FULL; |
| |
| else if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static int ffsTruncateFile(struct inode *inode, u64 old_size, u64 new_size) |
| { |
| s32 num_clusters; |
| u32 last_clu = CLUSTER_32(0); |
| int ret = 0; |
| sector_t sector = 0; |
| struct chain_t clu; |
| struct timestamp_t tm; |
| struct dentry_t *ep, *ep2; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct file_id_t *fid = &(EXFAT_I(inode)->fid); |
| struct entry_set_cache_t *es = NULL; |
| |
| pr_debug("%s entered (inode %p size %llu)\n", __func__, inode, |
| new_size); |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* check if the given file ID is opened */ |
| if (fid->type != TYPE_FILE) { |
| ret = FFS_PERMISSIONERR; |
| goto out; |
| } |
| |
| if (fid->size != old_size) { |
| pr_err("[EXFAT] truncate : can't skip it because of size-mismatch(old:%lld->fid:%lld).\n", |
| old_size, fid->size); |
| } |
| |
| if (old_size <= new_size) { |
| ret = FFS_SUCCESS; |
| goto out; |
| } |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| clu.dir = fid->start_clu; |
| clu.size = (s32)((old_size - 1) >> p_fs->cluster_size_bits) + 1; |
| clu.flags = fid->flags; |
| |
| if (new_size > 0) { |
| num_clusters = (s32)((new_size - 1) >> |
| p_fs->cluster_size_bits) + 1; |
| |
| if (clu.flags == 0x03) { |
| clu.dir += num_clusters; |
| } else { |
| while (num_clusters > 0) { |
| last_clu = clu.dir; |
| if (FAT_read(sb, clu.dir, &clu.dir) == -1) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| num_clusters--; |
| } |
| } |
| |
| clu.size -= num_clusters; |
| } |
| |
| fid->size = new_size; |
| fid->attr |= ATTR_ARCHIVE; |
| if (new_size == 0) { |
| fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01; |
| fid->start_clu = CLUSTER_32(~0); |
| } |
| |
| /* (1) update the directory entry */ |
| if (p_fs->vol_type == EXFAT) { |
| es = get_entry_set_in_dir(sb, &fid->dir, fid->entry, |
| ES_ALL_ENTRIES, &ep); |
| if (!es) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep + 1; |
| } else { |
| ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep; |
| } |
| |
| p_fs->fs_func->set_entry_time(ep, tm_current(&tm), TM_MODIFY); |
| p_fs->fs_func->set_entry_attr(ep, fid->attr); |
| |
| p_fs->fs_func->set_entry_size(ep2, new_size); |
| if (new_size == 0) { |
| p_fs->fs_func->set_entry_flag(ep2, 0x01); |
| p_fs->fs_func->set_entry_clu0(ep2, CLUSTER_32(0)); |
| } |
| |
| if (p_fs->vol_type != EXFAT) { |
| buf_modify(sb, sector); |
| } else { |
| update_dir_checksum_with_entry_set(sb, es); |
| release_entry_set(es); |
| } |
| |
| /* (2) cut off from the FAT chain */ |
| if (last_clu != CLUSTER_32(0)) { |
| if (fid->flags == 0x01) |
| FAT_write(sb, last_clu, CLUSTER_32(~0)); |
| } |
| |
| /* (3) free the clusters */ |
| p_fs->fs_func->free_cluster(sb, &clu, 0); |
| |
| /* hint information */ |
| fid->hint_last_off = -1; |
| if (fid->rwoffset > fid->size) |
| fid->rwoffset = fid->size; |
| |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| pr_debug("%s exited (%d)\n", __func__, ret); |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static void update_parent_info(struct file_id_t *fid, |
| struct inode *parent_inode) |
| { |
| struct fs_info_t *p_fs = &(EXFAT_SB(parent_inode->i_sb)->fs_info); |
| struct file_id_t *parent_fid = &(EXFAT_I(parent_inode)->fid); |
| |
| if (unlikely((parent_fid->flags != fid->dir.flags) || |
| (parent_fid->size != |
| (fid->dir.size << p_fs->cluster_size_bits)) || |
| (parent_fid->start_clu != fid->dir.dir))) { |
| fid->dir.dir = parent_fid->start_clu; |
| fid->dir.flags = parent_fid->flags; |
| fid->dir.size = ((parent_fid->size + (p_fs->cluster_size - 1)) |
| >> p_fs->cluster_size_bits); |
| } |
| } |
| |
| static int ffsMoveFile(struct inode *old_parent_inode, struct file_id_t *fid, |
| struct inode *new_parent_inode, struct dentry *new_dentry) |
| { |
| s32 ret; |
| s32 dentry; |
| struct chain_t olddir, newdir; |
| struct chain_t *p_dir = NULL; |
| struct uni_name_t uni_name; |
| struct dentry_t *ep; |
| struct super_block *sb = old_parent_inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| u8 *new_path = (u8 *)new_dentry->d_name.name; |
| struct inode *new_inode = new_dentry->d_inode; |
| int num_entries; |
| struct file_id_t *new_fid = NULL; |
| s32 new_entry = 0; |
| |
| /* check the validity of the given file id */ |
| if (!fid) |
| return FFS_INVALIDFID; |
| |
| /* check the validity of pointer parameters */ |
| if (!new_path || (*new_path == '\0')) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| update_parent_info(fid, old_parent_inode); |
| |
| olddir.dir = fid->dir.dir; |
| olddir.size = fid->dir.size; |
| olddir.flags = fid->dir.flags; |
| |
| dentry = fid->entry; |
| |
| /* check if the old file is "." or ".." */ |
| if (p_fs->vol_type != EXFAT) { |
| if ((olddir.dir != p_fs->root_dir) && (dentry < 2)) { |
| ret = FFS_PERMISSIONERR; |
| goto out2; |
| } |
| } |
| |
| ep = get_entry_in_dir(sb, &olddir, dentry, NULL); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out2; |
| } |
| |
| if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY) { |
| ret = FFS_PERMISSIONERR; |
| goto out2; |
| } |
| |
| /* check whether new dir is existing directory and empty */ |
| if (new_inode) { |
| u32 entry_type; |
| |
| ret = FFS_MEDIAERR; |
| new_fid = &EXFAT_I(new_inode)->fid; |
| |
| update_parent_info(new_fid, new_parent_inode); |
| |
| p_dir = &(new_fid->dir); |
| new_entry = new_fid->entry; |
| ep = get_entry_in_dir(sb, p_dir, new_entry, NULL); |
| if (!ep) |
| goto out; |
| |
| entry_type = p_fs->fs_func->get_entry_type(ep); |
| |
| if (entry_type == TYPE_DIR) { |
| struct chain_t new_clu; |
| |
| new_clu.dir = new_fid->start_clu; |
| new_clu.size = (s32)((new_fid->size - 1) >> |
| p_fs->cluster_size_bits) + 1; |
| new_clu.flags = new_fid->flags; |
| |
| if (!is_dir_empty(sb, &new_clu)) { |
| ret = FFS_FILEEXIST; |
| goto out; |
| } |
| } |
| } |
| |
| /* check the validity of directory name in the given new pathname */ |
| ret = resolve_path(new_parent_inode, new_path, &newdir, &uni_name); |
| if (ret) |
| goto out2; |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| if (olddir.dir == newdir.dir) |
| ret = exfat_rename_file(new_parent_inode, &olddir, dentry, |
| &uni_name, fid); |
| else |
| ret = move_file(new_parent_inode, &olddir, dentry, &newdir, |
| &uni_name, fid); |
| |
| if ((ret == FFS_SUCCESS) && new_inode) { |
| /* delete entries of new_dir */ |
| ep = get_entry_in_dir(sb, p_dir, new_entry, NULL); |
| if (!ep) |
| goto out; |
| |
| num_entries = p_fs->fs_func->count_ext_entries(sb, p_dir, |
| new_entry, ep); |
| if (num_entries < 0) |
| goto out; |
| p_fs->fs_func->delete_dir_entry(sb, p_dir, new_entry, 0, |
| num_entries + 1); |
| } |
| out: |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| out2: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static int ffsRemoveFile(struct inode *inode, struct file_id_t *fid) |
| { |
| s32 dentry; |
| int ret = FFS_SUCCESS; |
| struct chain_t dir, clu_to_free; |
| struct dentry_t *ep; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| |
| /* check the validity of the given file id */ |
| if (!fid) |
| return FFS_INVALIDFID; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| dir.dir = fid->dir.dir; |
| dir.size = fid->dir.size; |
| dir.flags = fid->dir.flags; |
| |
| dentry = fid->entry; |
| |
| ep = get_entry_in_dir(sb, &dir, dentry, NULL); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| |
| if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY) { |
| ret = FFS_PERMISSIONERR; |
| goto out; |
| } |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| /* (1) update the directory entry */ |
| remove_file(inode, &dir, dentry); |
| |
| clu_to_free.dir = fid->start_clu; |
| clu_to_free.size = (s32)((fid->size - 1) >> p_fs->cluster_size_bits) + 1; |
| clu_to_free.flags = fid->flags; |
| |
| /* (2) free the clusters */ |
| p_fs->fs_func->free_cluster(sb, &clu_to_free, 0); |
| |
| fid->size = 0; |
| fid->start_clu = CLUSTER_32(~0); |
| fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01; |
| |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| #if 0 |
| /* Not currently wired up */ |
| static int ffsSetAttr(struct inode *inode, u32 attr) |
| { |
| u32 type; |
| int ret = FFS_SUCCESS; |
| sector_t sector = 0; |
| struct dentry_t *ep; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct file_id_t *fid = &(EXFAT_I(inode)->fid); |
| u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0; |
| struct entry_set_cache_t *es = NULL; |
| |
| if (fid->attr == attr) { |
| if (p_fs->dev_ejected) |
| return FFS_MEDIAERR; |
| return FFS_SUCCESS; |
| } |
| |
| if (is_dir) { |
| if ((fid->dir.dir == p_fs->root_dir) && |
| (fid->entry == -1)) { |
| if (p_fs->dev_ejected) |
| return FFS_MEDIAERR; |
| return FFS_SUCCESS; |
| } |
| } |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* get the directory entry of given file */ |
| if (p_fs->vol_type == EXFAT) { |
| es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, |
| ES_ALL_ENTRIES, &ep); |
| if (!es) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| } else { |
| ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| } |
| |
| type = p_fs->fs_func->get_entry_type(ep); |
| |
| if (((type == TYPE_FILE) && (attr & ATTR_SUBDIR)) || |
| ((type == TYPE_DIR) && (!(attr & ATTR_SUBDIR)))) { |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| else |
| ret = FFS_ERROR; |
| |
| if (p_fs->vol_type == EXFAT) |
| release_entry_set(es); |
| goto out; |
| } |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| /* set the file attribute */ |
| fid->attr = attr; |
| p_fs->fs_func->set_entry_attr(ep, attr); |
| |
| if (p_fs->vol_type != EXFAT) { |
| buf_modify(sb, sector); |
| } else { |
| update_dir_checksum_with_entry_set(sb, es); |
| release_entry_set(es); |
| } |
| |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| #endif |
| |
| static int ffsReadStat(struct inode *inode, struct dir_entry_t *info) |
| { |
| sector_t sector = 0; |
| s32 count; |
| int ret = FFS_SUCCESS; |
| struct chain_t dir; |
| struct uni_name_t uni_name; |
| struct timestamp_t tm; |
| struct dentry_t *ep, *ep2; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct file_id_t *fid = &(EXFAT_I(inode)->fid); |
| struct entry_set_cache_t *es = NULL; |
| u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0; |
| |
| pr_debug("%s entered\n", __func__); |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| if (is_dir) { |
| if ((fid->dir.dir == p_fs->root_dir) && |
| (fid->entry == -1)) { |
| info->Attr = ATTR_SUBDIR; |
| memset((char *)&info->CreateTimestamp, 0, |
| sizeof(struct date_time_t)); |
| memset((char *)&info->ModifyTimestamp, 0, |
| sizeof(struct date_time_t)); |
| memset((char *)&info->AccessTimestamp, 0, |
| sizeof(struct date_time_t)); |
| strcpy(info->ShortName, "."); |
| strcpy(info->Name, "."); |
| |
| dir.dir = p_fs->root_dir; |
| dir.flags = 0x01; |
| |
| if (p_fs->root_dir == CLUSTER_32(0)) { |
| /* FAT16 root_dir */ |
| info->Size = p_fs->dentries_in_root << |
| DENTRY_SIZE_BITS; |
| } else { |
| info->Size = count_num_clusters(sb, &dir) << |
| p_fs->cluster_size_bits; |
| } |
| |
| count = count_dos_name_entries(sb, &dir, TYPE_DIR); |
| if (count < 0) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| info->NumSubdirs = count; |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| } |
| |
| /* get the directory entry of given file or directory */ |
| if (p_fs->vol_type == EXFAT) { |
| es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, |
| ES_2_ENTRIES, &ep); |
| if (!es) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep + 1; |
| } else { |
| ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep; |
| buf_lock(sb, sector); |
| } |
| |
| /* set FILE_INFO structure using the acquired struct dentry_t */ |
| info->Attr = p_fs->fs_func->get_entry_attr(ep); |
| |
| p_fs->fs_func->get_entry_time(ep, &tm, TM_CREATE); |
| info->CreateTimestamp.Year = tm.year; |
| info->CreateTimestamp.Month = tm.mon; |
| info->CreateTimestamp.Day = tm.day; |
| info->CreateTimestamp.Hour = tm.hour; |
| info->CreateTimestamp.Minute = tm.min; |
| info->CreateTimestamp.Second = tm.sec; |
| info->CreateTimestamp.MilliSecond = 0; |
| |
| p_fs->fs_func->get_entry_time(ep, &tm, TM_MODIFY); |
| info->ModifyTimestamp.Year = tm.year; |
| info->ModifyTimestamp.Month = tm.mon; |
| info->ModifyTimestamp.Day = tm.day; |
| info->ModifyTimestamp.Hour = tm.hour; |
| info->ModifyTimestamp.Minute = tm.min; |
| info->ModifyTimestamp.Second = tm.sec; |
| info->ModifyTimestamp.MilliSecond = 0; |
| |
| memset((char *)&info->AccessTimestamp, 0, sizeof(struct date_time_t)); |
| |
| *(uni_name.name) = 0x0; |
| /* XXX this is very bad for exfat cuz name is already included in es. |
| * API should be revised |
| */ |
| p_fs->fs_func->get_uni_name_from_ext_entry(sb, &(fid->dir), fid->entry, |
| uni_name.name); |
| if (*uni_name.name == 0x0 && p_fs->vol_type != EXFAT) |
| get_uni_name_from_dos_entry(sb, (struct dos_dentry_t *)ep, |
| &uni_name, 0x1); |
| nls_uniname_to_cstring(sb, info->Name, &uni_name); |
| |
| if (p_fs->vol_type == EXFAT) { |
| info->NumSubdirs = 2; |
| } else { |
| buf_unlock(sb, sector); |
| get_uni_name_from_dos_entry(sb, (struct dos_dentry_t *)ep, |
| &uni_name, 0x0); |
| nls_uniname_to_cstring(sb, info->ShortName, &uni_name); |
| info->NumSubdirs = 0; |
| } |
| |
| info->Size = p_fs->fs_func->get_entry_size(ep2); |
| |
| if (p_fs->vol_type == EXFAT) |
| release_entry_set(es); |
| |
| if (is_dir) { |
| dir.dir = fid->start_clu; |
| dir.flags = 0x01; |
| |
| if (info->Size == 0) |
| info->Size = (u64)count_num_clusters(sb, &dir) << |
| p_fs->cluster_size_bits; |
| |
| count = count_dos_name_entries(sb, &dir, TYPE_DIR); |
| if (count < 0) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| info->NumSubdirs += count; |
| } |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| pr_debug("%s exited successfully\n", __func__); |
| return ret; |
| } |
| |
| static int ffsWriteStat(struct inode *inode, struct dir_entry_t *info) |
| { |
| sector_t sector = 0; |
| int ret = FFS_SUCCESS; |
| struct timestamp_t tm; |
| struct dentry_t *ep, *ep2; |
| struct entry_set_cache_t *es = NULL; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct file_id_t *fid = &(EXFAT_I(inode)->fid); |
| u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0; |
| |
| pr_debug("%s entered (inode %p info %p\n", __func__, inode, info); |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| if (is_dir) { |
| if ((fid->dir.dir == p_fs->root_dir) && |
| (fid->entry == -1)) { |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| ret = FFS_SUCCESS; |
| goto out; |
| } |
| } |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| /* get the directory entry of given file or directory */ |
| if (p_fs->vol_type == EXFAT) { |
| es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry, |
| ES_ALL_ENTRIES, &ep); |
| if (!es) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep + 1; |
| } else { |
| /* for other than exfat */ |
| ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| ep2 = ep; |
| } |
| |
| p_fs->fs_func->set_entry_attr(ep, info->Attr); |
| |
| /* set FILE_INFO structure using the acquired struct dentry_t */ |
| tm.sec = info->CreateTimestamp.Second; |
| tm.min = info->CreateTimestamp.Minute; |
| tm.hour = info->CreateTimestamp.Hour; |
| tm.day = info->CreateTimestamp.Day; |
| tm.mon = info->CreateTimestamp.Month; |
| tm.year = info->CreateTimestamp.Year; |
| p_fs->fs_func->set_entry_time(ep, &tm, TM_CREATE); |
| |
| tm.sec = info->ModifyTimestamp.Second; |
| tm.min = info->ModifyTimestamp.Minute; |
| tm.hour = info->ModifyTimestamp.Hour; |
| tm.day = info->ModifyTimestamp.Day; |
| tm.mon = info->ModifyTimestamp.Month; |
| tm.year = info->ModifyTimestamp.Year; |
| p_fs->fs_func->set_entry_time(ep, &tm, TM_MODIFY); |
| |
| p_fs->fs_func->set_entry_size(ep2, info->Size); |
| |
| if (p_fs->vol_type != EXFAT) { |
| buf_modify(sb, sector); |
| } else { |
| update_dir_checksum_with_entry_set(sb, es); |
| release_entry_set(es); |
| } |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| pr_debug("%s exited (%d)\n", __func__, ret); |
| |
| return ret; |
| } |
| |
| static int ffsMapCluster(struct inode *inode, s32 clu_offset, u32 *clu) |
| { |
| s32 num_clusters, num_alloced; |
| bool modified = false; |
| u32 last_clu; |
| int ret = FFS_SUCCESS; |
| sector_t sector = 0; |
| struct chain_t new_clu; |
| struct dentry_t *ep; |
| struct entry_set_cache_t *es = NULL; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct file_id_t *fid = &(EXFAT_I(inode)->fid); |
| |
| /* check the validity of pointer parameters */ |
| if (!clu) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| fid->rwoffset = (s64)(clu_offset) << p_fs->cluster_size_bits; |
| |
| if (EXFAT_I(inode)->mmu_private == 0) |
| num_clusters = 0; |
| else |
| num_clusters = (s32)((EXFAT_I(inode)->mmu_private - 1) >> |
| p_fs->cluster_size_bits) + 1; |
| |
| *clu = last_clu = fid->start_clu; |
| |
| if (fid->flags == 0x03) { |
| if ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) { |
| last_clu += clu_offset - 1; |
| |
| if (clu_offset == num_clusters) |
| *clu = CLUSTER_32(~0); |
| else |
| *clu += clu_offset; |
| } |
| } else { |
| /* hint information */ |
| if ((clu_offset > 0) && (fid->hint_last_off > 0) && |
| (clu_offset >= fid->hint_last_off)) { |
| clu_offset -= fid->hint_last_off; |
| *clu = fid->hint_last_clu; |
| } |
| |
| while ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) { |
| last_clu = *clu; |
| if (FAT_read(sb, *clu, clu) == -1) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| clu_offset--; |
| } |
| } |
| |
| if (*clu == CLUSTER_32(~0)) { |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| new_clu.dir = (last_clu == CLUSTER_32(~0)) ? CLUSTER_32(~0) : |
| last_clu + 1; |
| new_clu.size = 0; |
| new_clu.flags = fid->flags; |
| |
| /* (1) allocate a cluster */ |
| num_alloced = p_fs->fs_func->alloc_cluster(sb, 1, &new_clu); |
| if (num_alloced < 0) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } else if (num_alloced == 0) { |
| ret = FFS_FULL; |
| goto out; |
| } |
| |
| /* (2) append to the FAT chain */ |
| if (last_clu == CLUSTER_32(~0)) { |
| if (new_clu.flags == 0x01) |
| fid->flags = 0x01; |
| fid->start_clu = new_clu.dir; |
| modified = true; |
| } else { |
| if (new_clu.flags != fid->flags) { |
| exfat_chain_cont_cluster(sb, fid->start_clu, |
| num_clusters); |
| fid->flags = 0x01; |
| modified = true; |
| } |
| if (new_clu.flags == 0x01) |
| FAT_write(sb, last_clu, new_clu.dir); |
| } |
| |
| num_clusters += num_alloced; |
| *clu = new_clu.dir; |
| |
| if (p_fs->vol_type == EXFAT) { |
| es = get_entry_set_in_dir(sb, &fid->dir, fid->entry, |
| ES_ALL_ENTRIES, &ep); |
| if (!es) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| /* get stream entry */ |
| ep++; |
| } |
| |
| /* (3) update directory entry */ |
| if (modified) { |
| if (p_fs->vol_type != EXFAT) { |
| ep = get_entry_in_dir(sb, &(fid->dir), |
| fid->entry, §or); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| } |
| |
| if (p_fs->fs_func->get_entry_flag(ep) != fid->flags) |
| p_fs->fs_func->set_entry_flag(ep, fid->flags); |
| |
| if (p_fs->fs_func->get_entry_clu0(ep) != fid->start_clu) |
| p_fs->fs_func->set_entry_clu0(ep, |
| fid->start_clu); |
| |
| if (p_fs->vol_type != EXFAT) |
| buf_modify(sb, sector); |
| } |
| |
| if (p_fs->vol_type == EXFAT) { |
| update_dir_checksum_with_entry_set(sb, es); |
| release_entry_set(es); |
| } |
| |
| /* add number of new blocks to inode */ |
| inode->i_blocks += num_alloced << (p_fs->cluster_size_bits - 9); |
| } |
| |
| /* hint information */ |
| fid->hint_last_off = (s32)(fid->rwoffset >> p_fs->cluster_size_bits); |
| fid->hint_last_clu = *clu; |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| /*----------------------------------------------------------------------*/ |
| /* Directory Operation Functions */ |
| /*----------------------------------------------------------------------*/ |
| |
| static int ffsCreateDir(struct inode *inode, char *path, struct file_id_t *fid) |
| { |
| int ret = FFS_SUCCESS; |
| struct chain_t dir; |
| struct uni_name_t uni_name; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| /* check the validity of pointer parameters */ |
| if (!fid || !path || (*path == '\0')) |
| return FFS_ERROR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| /* check the validity of directory name in the given old pathname */ |
| ret = resolve_path(inode, path, &dir, &uni_name); |
| if (ret) |
| goto out; |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| ret = create_dir(inode, &dir, &uni_name, fid); |
| |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static int ffsReadDir(struct inode *inode, struct dir_entry_t *dir_entry) |
| { |
| int i, dentry, clu_offset; |
| int ret = FFS_SUCCESS; |
| s32 dentries_per_clu, dentries_per_clu_bits = 0; |
| u32 type; |
| sector_t sector; |
| struct chain_t dir, clu; |
| struct uni_name_t uni_name; |
| struct timestamp_t tm; |
| struct dentry_t *ep; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| struct fs_func *fs_func = p_fs->fs_func; |
| struct file_id_t *fid = &(EXFAT_I(inode)->fid); |
| |
| /* check the validity of pointer parameters */ |
| if (!dir_entry) |
| return FFS_ERROR; |
| |
| /* check if the given file ID is opened */ |
| if (fid->type != TYPE_DIR) |
| return FFS_PERMISSIONERR; |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| if (fid->entry == -1) { |
| dir.dir = p_fs->root_dir; |
| dir.flags = 0x01; |
| } else { |
| dir.dir = fid->start_clu; |
| dir.size = (s32)(fid->size >> p_fs->cluster_size_bits); |
| dir.flags = fid->flags; |
| } |
| |
| dentry = (s32)fid->rwoffset; |
| |
| if (dir.dir == CLUSTER_32(0)) { |
| /* FAT16 root_dir */ |
| dentries_per_clu = p_fs->dentries_in_root; |
| |
| if (dentry == dentries_per_clu) { |
| clu.dir = CLUSTER_32(~0); |
| } else { |
| clu.dir = dir.dir; |
| clu.size = dir.size; |
| clu.flags = dir.flags; |
| } |
| } else { |
| dentries_per_clu = p_fs->dentries_per_clu; |
| dentries_per_clu_bits = ilog2(dentries_per_clu); |
| |
| clu_offset = dentry >> dentries_per_clu_bits; |
| clu.dir = dir.dir; |
| clu.size = dir.size; |
| clu.flags = dir.flags; |
| |
| if (clu.flags == 0x03) { |
| clu.dir += clu_offset; |
| clu.size -= clu_offset; |
| } else { |
| /* hint_information */ |
| if ((clu_offset > 0) && (fid->hint_last_off > 0) && |
| (clu_offset >= fid->hint_last_off)) { |
| clu_offset -= fid->hint_last_off; |
| clu.dir = fid->hint_last_clu; |
| } |
| |
| while (clu_offset > 0) { |
| /* clu.dir = FAT_read(sb, clu.dir); */ |
| if (FAT_read(sb, clu.dir, &clu.dir) == -1) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| clu_offset--; |
| } |
| } |
| } |
| |
| while (clu.dir != CLUSTER_32(~0)) { |
| if (p_fs->dev_ejected) |
| break; |
| |
| if (dir.dir == CLUSTER_32(0)) /* FAT16 root_dir */ |
| i = dentry % dentries_per_clu; |
| else |
| i = dentry & (dentries_per_clu - 1); |
| |
| for ( ; i < dentries_per_clu; i++, dentry++) { |
| ep = get_entry_in_dir(sb, &clu, i, §or); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| type = fs_func->get_entry_type(ep); |
| |
| if (type == TYPE_UNUSED) |
| break; |
| |
| if ((type != TYPE_FILE) && (type != TYPE_DIR)) |
| continue; |
| |
| buf_lock(sb, sector); |
| dir_entry->Attr = fs_func->get_entry_attr(ep); |
| |
| fs_func->get_entry_time(ep, &tm, TM_CREATE); |
| dir_entry->CreateTimestamp.Year = tm.year; |
| dir_entry->CreateTimestamp.Month = tm.mon; |
| dir_entry->CreateTimestamp.Day = tm.day; |
| dir_entry->CreateTimestamp.Hour = tm.hour; |
| dir_entry->CreateTimestamp.Minute = tm.min; |
| dir_entry->CreateTimestamp.Second = tm.sec; |
| dir_entry->CreateTimestamp.MilliSecond = 0; |
| |
| fs_func->get_entry_time(ep, &tm, TM_MODIFY); |
| dir_entry->ModifyTimestamp.Year = tm.year; |
| dir_entry->ModifyTimestamp.Month = tm.mon; |
| dir_entry->ModifyTimestamp.Day = tm.day; |
| dir_entry->ModifyTimestamp.Hour = tm.hour; |
| dir_entry->ModifyTimestamp.Minute = tm.min; |
| dir_entry->ModifyTimestamp.Second = tm.sec; |
| dir_entry->ModifyTimestamp.MilliSecond = 0; |
| |
| memset((char *)&dir_entry->AccessTimestamp, 0, |
| sizeof(struct date_time_t)); |
| |
| *(uni_name.name) = 0x0; |
| fs_func->get_uni_name_from_ext_entry(sb, &dir, dentry, |
| uni_name.name); |
| if (*uni_name.name == 0x0 && p_fs->vol_type != EXFAT) |
| get_uni_name_from_dos_entry(sb, |
| (struct dos_dentry_t *)ep, |
| &uni_name, 0x1); |
| nls_uniname_to_cstring(sb, dir_entry->Name, &uni_name); |
| buf_unlock(sb, sector); |
| |
| if (p_fs->vol_type == EXFAT) { |
| ep = get_entry_in_dir(sb, &clu, i + 1, NULL); |
| if (!ep) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| } else { |
| get_uni_name_from_dos_entry(sb, |
| (struct dos_dentry_t *)ep, |
| &uni_name, 0x0); |
| nls_uniname_to_cstring(sb, dir_entry->ShortName, |
| &uni_name); |
| } |
| |
| dir_entry->Size = fs_func->get_entry_size(ep); |
| |
| /* hint information */ |
| if (dir.dir == CLUSTER_32(0)) { /* FAT16 root_dir */ |
| } else { |
| fid->hint_last_off = dentry >> |
| dentries_per_clu_bits; |
| fid->hint_last_clu = clu.dir; |
| } |
| |
| fid->rwoffset = (s64)(++dentry); |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| |
| if (dir.dir == CLUSTER_32(0)) |
| break; /* FAT16 root_dir */ |
| |
| if (clu.flags == 0x03) { |
| if ((--clu.size) > 0) |
| clu.dir++; |
| else |
| clu.dir = CLUSTER_32(~0); |
| } else { |
| /* clu.dir = FAT_read(sb, clu.dir); */ |
| if (FAT_read(sb, clu.dir, &clu.dir) == -1) { |
| ret = FFS_MEDIAERR; |
| goto out; |
| } |
| } |
| } |
| |
| *(dir_entry->Name) = '\0'; |
| |
| fid->rwoffset = (s64)(++dentry); |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| static int ffsRemoveDir(struct inode *inode, struct file_id_t *fid) |
| { |
| s32 dentry; |
| int ret = FFS_SUCCESS; |
| struct chain_t dir, clu_to_free; |
| struct super_block *sb = inode->i_sb; |
| struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info); |
| |
| /* check the validity of the given file id */ |
| if (!fid) |
| return FFS_INVALIDFID; |
| |
| dir.dir = fid->dir.dir; |
| dir.size = fid->dir.size; |
| dir.flags = fid->dir.flags; |
| |
| dentry = fid->entry; |
| |
| /* check if the file is "." or ".." */ |
| if (p_fs->vol_type != EXFAT) { |
| if ((dir.dir != p_fs->root_dir) && (dentry < 2)) |
| return FFS_PERMISSIONERR; |
| } |
| |
| /* acquire the lock for file system critical section */ |
| down(&p_fs->v_sem); |
| |
| clu_to_free.dir = fid->start_clu; |
| clu_to_free.size = (s32)((fid->size - 1) >> p_fs->cluster_size_bits) + 1; |
| clu_to_free.flags = fid->flags; |
| |
| if (!is_dir_empty(sb, &clu_to_free)) { |
| ret = FFS_FILEEXIST; |
| goto out; |
| } |
| |
| fs_set_vol_flags(sb, VOL_DIRTY); |
| |
| /* (1) update the directory entry */ |
| remove_file(inode, &dir, dentry); |
| |
| /* (2) free the clusters */ |
| p_fs->fs_func->free_cluster(sb, &clu_to_free, 1); |
| |
| fid->size = 0; |
| fid->start_clu = CLUSTER_32(~0); |
| fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01; |
| |
| #ifdef CONFIG_EXFAT_DELAYED_SYNC |
| fs_sync(sb, false); |
| fs_set_vol_flags(sb, VOL_CLEAN); |
| #endif |
| |
| if (p_fs->dev_ejected) |
| ret = FFS_MEDIAERR; |
| |
| out: |
| /* release the lock for file system critical section */ |
| up(&p_fs->v_sem); |
| |
| return ret; |
| } |
| |
| /*======================================================================*/ |
| /* Directory Entry Operations */ |
| /*======================================================================*/ |
| |
| static int exfat_readdir(struct file *filp, struct dir_context *ctx) |
| { |
| struct inode *inode = file_inode(filp); |
| struct super_block *sb = inode->i_sb; |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| struct fs_info_t *p_fs = &(sbi->fs_info); |
| struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info); |
| struct dir_entry_t de; |
| unsigned long inum; |
| loff_t cpos; |
| int err = 0; |
| |
| __lock_super(sb); |
| |
| cpos = ctx->pos; |
| /* Fake . and .. for the root directory. */ |
| if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) { |
| while (cpos < 2) { |
| if (inode->i_ino == EXFAT_ROOT_INO) |
| inum = EXFAT_ROOT_INO; |
| else if (cpos == 0) |
| inum = inode->i_ino; |
| else /* (cpos == 1) */ |
| inum = parent_ino(filp->f_path.dentry); |
| |
| if (!dir_emit_dots(filp, ctx)) |
| goto out; |
| cpos++; |
| ctx->pos++; |
| } |
| if (cpos == 2) |
| cpos = 0; |
| } |
| if (cpos & (DENTRY_SIZE - 1)) { |
| err = -ENOENT; |
| goto out; |
| } |
| |
| get_new: |
| EXFAT_I(inode)->fid.size = i_size_read(inode); |
| EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS; |
| |
| err = ffsReadDir(inode, &de); |
| if (err) { |
| /* at least we tried to read a sector |
| * move cpos to next sector position (should be aligned) |
| */ |
| if (err == FFS_MEDIAERR) { |
| cpos += 1 << p_bd->sector_size_bits; |
| cpos &= ~((1 << p_bd->sector_size_bits) - 1); |
| } |
| |
| err = -EIO; |
| goto end_of_dir; |
| } |
| |
| cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS; |
| |
| if (!de.Name[0]) |
| goto end_of_dir; |
| |
| if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) { |
| inum = inode->i_ino; |
| } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) { |
| inum = parent_ino(filp->f_path.dentry); |
| } else { |
| loff_t i_pos = ((loff_t)EXFAT_I(inode)->fid.start_clu << 32) | |
| ((EXFAT_I(inode)->fid.rwoffset - 1) & 0xffffffff); |
| struct inode *tmp = exfat_iget(sb, i_pos); |
| |
| if (tmp) { |
| inum = tmp->i_ino; |
| iput(tmp); |
| } else { |
| inum = iunique(sb, EXFAT_ROOT_INO); |
| } |
| } |
| |
| if (!dir_emit(ctx, de.Name, strlen(de.Name), inum, |
| (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG)) |
| goto out; |
| |
| ctx->pos = cpos; |
| goto get_new; |
| |
| end_of_dir: |
| ctx->pos = cpos; |
| out: |
| __unlock_super(sb); |
| return err; |
| } |
| |
| static int exfat_ioctl_volume_id(struct inode *dir) |
| { |
| struct super_block *sb = dir->i_sb; |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| struct fs_info_t *p_fs = &(sbi->fs_info); |
| |
| return p_fs->vol_id; |
| } |
| |
| static long exfat_generic_ioctl(struct file *filp, unsigned int cmd, |
| unsigned long arg) |
| { |
| struct inode *inode = filp->f_path.dentry->d_inode; |
| #ifdef CONFIG_EXFAT_KERNEL_DEBUG |
| unsigned int flags; |
| #endif /* CONFIG_EXFAT_KERNEL_DEBUG */ |
| |
| switch (cmd) { |
| case EXFAT_IOCTL_GET_VOLUME_ID: |
| return exfat_ioctl_volume_id(inode); |
| #ifdef CONFIG_EXFAT_KERNEL_DEBUG |
| case EXFAT_IOC_GET_DEBUGFLAGS: { |
| struct super_block *sb = inode->i_sb; |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| |
| flags = sbi->debug_flags; |
| return put_user(flags, (int __user *)arg); |
| } |
| case EXFAT_IOC_SET_DEBUGFLAGS: { |
| struct super_block *sb = inode->i_sb; |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| |
| if (!capable(CAP_SYS_ADMIN)) |
| return -EPERM; |
| |
| if (get_user(flags, (int __user *)arg)) |
| return -EFAULT; |
| |
| __lock_super(sb); |
| sbi->debug_flags = flags; |
| __unlock_super(sb); |
| |
| return 0; |
| } |
| #endif /* CONFIG_EXFAT_KERNEL_DEBUG */ |
| default: |
| return -ENOTTY; /* Inappropriate ioctl for device */ |
| } |
| } |
| |
| static const struct file_operations exfat_dir_operations = { |
| .llseek = generic_file_llseek, |
| .read = generic_read_dir, |
| .iterate = exfat_readdir, |
| .unlocked_ioctl = exfat_generic_ioctl, |
| .fsync = generic_file_fsync, |
| }; |
| |
| static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
| bool excl) |
| { |
| struct super_block *sb = dir->i_sb; |
| struct inode *inode; |
| struct file_id_t fid; |
| loff_t i_pos; |
| int err; |
| |
| __lock_super(sb); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| err = ffsCreateFile(dir, (u8 *)dentry->d_name.name, FM_REGULAR, &fid); |
| if (err) { |
| if (err == FFS_INVALIDPATH) |
| err = -EINVAL; |
| else if (err == FFS_FILEEXIST) |
| err = -EEXIST; |
| else if (err == FFS_FULL) |
| err = -ENOSPC; |
| else if (err == FFS_NAMETOOLONG) |
| err = -ENAMETOOLONG; |
| else |
| err = -EIO; |
| goto out; |
| } |
| INC_IVERSION(dir); |
| dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir); |
| if (IS_DIRSYNC(dir)) |
| (void)exfat_sync_inode(dir); |
| else |
| mark_inode_dirty(dir); |
| |
| i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff); |
| |
| inode = exfat_build_inode(sb, &fid, i_pos); |
| if (IS_ERR(inode)) { |
| err = PTR_ERR(inode); |
| goto out; |
| } |
| INC_IVERSION(inode); |
| inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
| /* |
| * timestamp is already written, so mark_inode_dirty() is unnecessary. |
| */ |
| |
| dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode); |
| d_instantiate(dentry, inode); |
| |
| out: |
| __unlock_super(sb); |
| pr_debug("%s exited\n", __func__); |
| return err; |
| } |
| |
| static int exfat_find(struct inode *dir, struct qstr *qname, |
| struct file_id_t *fid) |
| { |
| int err; |
| |
| if (qname->len == 0) |
| return -ENOENT; |
| |
| err = ffsLookupFile(dir, (u8 *)qname->name, fid); |
| if (err) |
| return -ENOENT; |
| |
| return 0; |
| } |
| |
| static int exfat_d_anon_disconn(struct dentry *dentry) |
| { |
| return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED); |
| } |
| |
| static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry, |
| unsigned int flags) |
| { |
| struct super_block *sb = dir->i_sb; |
| struct inode *inode; |
| struct dentry *alias; |
| int err; |
| struct file_id_t fid; |
| loff_t i_pos; |
| u64 ret; |
| mode_t i_mode; |
| |
| __lock_super(sb); |
| pr_debug("%s entered\n", __func__); |
| err = exfat_find(dir, &dentry->d_name, &fid); |
| if (err) { |
| if (err == -ENOENT) { |
| inode = NULL; |
| goto out; |
| } |
| goto error; |
| } |
| |
| i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff); |
| inode = exfat_build_inode(sb, &fid, i_pos); |
| if (IS_ERR(inode)) { |
| err = PTR_ERR(inode); |
| goto error; |
| } |
| |
| i_mode = inode->i_mode; |
| if (S_ISLNK(i_mode) && !EXFAT_I(inode)->target) { |
| EXFAT_I(inode)->target = kmalloc(i_size_read(inode) + 1, |
| GFP_KERNEL); |
| if (!EXFAT_I(inode)->target) { |
| err = -ENOMEM; |
| goto error; |
| } |
| ffsReadFile(dir, &fid, EXFAT_I(inode)->target, |
| i_size_read(inode), &ret); |
| *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0'; |
| } |
| |
| alias = d_find_alias(inode); |
| if (alias && !exfat_d_anon_disconn(alias)) { |
| BUG_ON(d_unhashed(alias)); |
| if (!S_ISDIR(i_mode)) |
| d_move(alias, dentry); |
| iput(inode); |
| __unlock_super(sb); |
| pr_debug("%s exited 1\n", __func__); |
| return alias; |
| } |
| dput(alias); |
| out: |
| __unlock_super(sb); |
| dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode); |
| dentry = d_splice_alias(inode, dentry); |
| if (dentry) |
| dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode); |
| pr_debug("%s exited 2\n", __func__); |
| return dentry; |
| |
| error: |
| __unlock_super(sb); |
| pr_debug("%s exited 3\n", __func__); |
| return ERR_PTR(err); |
| } |
| |
| static inline unsigned long exfat_hash(loff_t i_pos) |
| { |
| return hash_32(i_pos, EXFAT_HASH_BITS); |
| } |
| |
| static void exfat_attach(struct inode *inode, loff_t i_pos) |
| { |
| struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); |
| struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); |
| |
| spin_lock(&sbi->inode_hash_lock); |
| EXFAT_I(inode)->i_pos = i_pos; |
| hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head); |
| spin_unlock(&sbi->inode_hash_lock); |
| } |
| |
| static void exfat_detach(struct inode *inode) |
| { |
| struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); |
| |
| spin_lock(&sbi->inode_hash_lock); |
| hlist_del_init(&EXFAT_I(inode)->i_hash_fat); |
| EXFAT_I(inode)->i_pos = 0; |
| spin_unlock(&sbi->inode_hash_lock); |
| } |
| |
| static int exfat_unlink(struct inode *dir, struct dentry *dentry) |
| { |
| struct inode *inode = dentry->d_inode; |
| struct super_block *sb = dir->i_sb; |
| int err; |
| |
| __lock_super(sb); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| EXFAT_I(inode)->fid.size = i_size_read(inode); |
| |
| err = ffsRemoveFile(dir, &(EXFAT_I(inode)->fid)); |
| if (err) { |
| if (err == FFS_PERMISSIONERR) |
| err = -EPERM; |
| else |
| err = -EIO; |
| goto out; |
| } |
| INC_IVERSION(dir); |
| dir->i_mtime = dir->i_atime = current_time(dir); |
| if (IS_DIRSYNC(dir)) |
| (void)exfat_sync_inode(dir); |
| else |
| mark_inode_dirty(dir); |
| |
| clear_nlink(inode); |
| inode->i_mtime = inode->i_atime = current_time(inode); |
| exfat_detach(inode); |
| remove_inode_hash(inode); |
| |
| out: |
| __unlock_super(sb); |
| pr_debug("%s exited\n", __func__); |
| return err; |
| } |
| |
| static int exfat_symlink(struct inode *dir, struct dentry *dentry, |
| const char *target) |
| { |
| struct super_block *sb = dir->i_sb; |
| struct inode *inode; |
| struct file_id_t fid; |
| loff_t i_pos; |
| int err; |
| u64 len = (u64)strlen(target); |
| u64 ret; |
| |
| __lock_super(sb); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| err = ffsCreateFile(dir, (u8 *)dentry->d_name.name, FM_SYMLINK, &fid); |
| if (err) { |
| if (err == FFS_INVALIDPATH) |
| err = -EINVAL; |
| else if (err == FFS_FILEEXIST) |
| err = -EEXIST; |
| else if (err == FFS_FULL) |
| err = -ENOSPC; |
| else |
| err = -EIO; |
| goto out; |
| } |
| |
| err = ffsWriteFile(dir, &fid, (char *)target, len, &ret); |
| |
| if (err) { |
| ffsRemoveFile(dir, &fid); |
| |
| if (err == FFS_FULL) |
| err = -ENOSPC; |
| else |
| err = -EIO; |
| goto out; |
| } |
| |
| INC_IVERSION(dir); |
| dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir); |
| if (IS_DIRSYNC(dir)) |
| (void)exfat_sync_inode(dir); |
| else |
| mark_inode_dirty(dir); |
| |
| i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff); |
| |
| inode = exfat_build_inode(sb, &fid, i_pos); |
| if (IS_ERR(inode)) { |
| err = PTR_ERR(inode); |
| goto out; |
| } |
| INC_IVERSION(inode); |
| inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
| /* timestamp is already written, so mark_inode_dirty() is unneeded. */ |
| |
| EXFAT_I(inode)->target = kmemdup(target, len + 1, GFP_KERNEL); |
| if (!EXFAT_I(inode)->target) { |
| err = -ENOMEM; |
| goto out; |
| } |
| |
| dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode); |
| d_instantiate(dentry, inode); |
| |
| out: |
| __unlock_super(sb); |
| pr_debug("%s exited\n", __func__); |
| return err; |
| } |
| |
| static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| { |
| struct super_block *sb = dir->i_sb; |
| struct inode *inode; |
| struct file_id_t fid; |
| loff_t i_pos; |
| int err; |
| |
| __lock_super(sb); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| err = ffsCreateDir(dir, (u8 *)dentry->d_name.name, &fid); |
| if (err) { |
| if (err == FFS_INVALIDPATH) |
| err = -EINVAL; |
| else if (err == FFS_FILEEXIST) |
| err = -EEXIST; |
| else if (err == FFS_FULL) |
| err = -ENOSPC; |
| else if (err == FFS_NAMETOOLONG) |
| err = -ENAMETOOLONG; |
| else |
| err = -EIO; |
| goto out; |
| } |
| INC_IVERSION(dir); |
| dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir); |
| if (IS_DIRSYNC(dir)) |
| (void)exfat_sync_inode(dir); |
| else |
| mark_inode_dirty(dir); |
| inc_nlink(dir); |
| |
| i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff); |
| |
| inode = exfat_build_inode(sb, &fid, i_pos); |
| if (IS_ERR(inode)) { |
| err = PTR_ERR(inode); |
| goto out; |
| } |
| INC_IVERSION(inode); |
| inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
| /* timestamp is already written, so mark_inode_dirty() is unneeded. */ |
| |
| dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode); |
| d_instantiate(dentry, inode); |
| |
| out: |
| __unlock_super(sb); |
| pr_debug("%s exited\n", __func__); |
| return err; |
| } |
| |
| static int exfat_rmdir(struct inode *dir, struct dentry *dentry) |
| { |
| struct inode *inode = dentry->d_inode; |
| struct super_block *sb = dir->i_sb; |
| int err; |
| |
| __lock_super(sb); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| EXFAT_I(inode)->fid.size = i_size_read(inode); |
| |
| err = ffsRemoveDir(dir, &(EXFAT_I(inode)->fid)); |
| if (err) { |
| if (err == FFS_INVALIDPATH) |
| err = -EINVAL; |
| else if (err == FFS_FILEEXIST) |
| err = -ENOTEMPTY; |
| else if (err == FFS_NOTFOUND) |
| err = -ENOENT; |
| else if (err == FFS_DIRBUSY) |
| err = -EBUSY; |
| else |
| err = -EIO; |
| goto out; |
| } |
| INC_IVERSION(dir); |
| dir->i_mtime = dir->i_atime = current_time(dir); |
| if (IS_DIRSYNC(dir)) |
| (void)exfat_sync_inode(dir); |
| else |
| mark_inode_dirty(dir); |
| drop_nlink(dir); |
| |
| clear_nlink(inode); |
| inode->i_mtime = inode->i_atime = current_time(inode); |
| exfat_detach(inode); |
| remove_inode_hash(inode); |
| |
| out: |
| __unlock_super(sb); |
| pr_debug("%s exited\n", __func__); |
| return err; |
| } |
| |
| static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry, |
| struct inode *new_dir, struct dentry *new_dentry, |
| unsigned int flags) |
| { |
| struct inode *old_inode, *new_inode; |
| struct super_block *sb = old_dir->i_sb; |
| loff_t i_pos; |
| int err; |
| |
| if (flags) |
| return -EINVAL; |
| |
| __lock_super(sb); |
| |
| pr_debug("%s entered\n", __func__); |
| |
| old_inode = old_dentry->d_inode; |
| new_inode = new_dentry->d_inode; |
| |
| EXFAT_I(old_inode)->fid.size = i_size_read(old_inode); |
| |
| err = ffsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir, |
| new_dentry); |
| if (err) { |
| if (err == FFS_PERMISSIONERR) |
| err = -EPERM; |
| else if (err == FFS_INVALIDPATH) |
| err = -EINVAL; |
| else if (err == FFS_FILEEXIST) |
| err = -EEXIST; |
| else if (err == FFS_NOTFOUND) |
| err = -ENOENT; |
| else if (err == FFS_FULL) |
| err = -ENOSPC; |
| else |
| err = -EIO; |
| goto out; |
| } |
| INC_IVERSION(new_dir); |
| new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = |
| current_time(new_dir); |
| if (IS_DIRSYNC(new_dir)) |
| (void)exfat_sync_inode(new_dir); |
| else |
| mark_inode_dirty(new_dir); |
| |
| i_pos = ((loff_t)EXFAT_I(old_inode)->fid.dir.dir << 32) | |
| (EXFAT_I(old_inode)->fid.entry & 0xffffffff); |
| |
| exfat_detach(old_inode); |
| exfat_attach(old_inode, i_pos); |
| if (IS_DIRSYNC(new_dir)) |
| (void)exfat_sync_inode(old_inode); |
| else |
| mark_inode_dirty(old_inode); |
| |
| if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) { |
| drop_nlink(old_dir); |
| if (!new_inode) |
| inc_nlink(new_dir); |
| } |
| INC_IVERSION(old_dir); |
| old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir); |
| if (IS_DIRSYNC(old_dir)) |
| (void)exfat_sync_inode(old_dir); |
| else |
| mark_inode_dirty(old_dir); |
| |
| if (new_inode) { |
| exfat_detach(new_inode); |
| drop_nlink(new_inode); |
| if (S_ISDIR(new_inode->i_mode)) |
| drop_nlink(new_inode); |
| new_inode->i_ctime = current_time(new_inode); |
| } |
| |
| out: |
| __unlock_super(sb); |
| pr_debug("%s exited\n", __func__); |
| return err; |
| } |
| |
| static int exfat_cont_expand(struct inode *inode, loff_t size) |
| { |
| struct address_space *mapping = inode->i_mapping; |
| loff_t start = i_size_read(inode), count = size - i_size_read(inode); |
| int err, err2; |
| |
| err = generic_cont_expand_simple(inode, size); |
| if (err != 0) |
| return err; |
| |
| inode->i_ctime = inode->i_mtime = current_time(inode); |
| mark_inode_dirty(inode); |
| |
| if (IS_SYNC(inode)) { |
| err = filemap_fdatawrite_range(mapping, start, |
| start + count - 1); |
| err2 = sync_mapping_buffers(mapping); |
| err = (err) ? (err) : (err2); |
| err2 = write_inode_now(inode, 1); |
| err = (err) ? (err) : (err2); |
| if (!err) |
| err = filemap_fdatawait_range(mapping, start, |
| start + count - 1); |
| } |
| return err; |
| } |
| |
| static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode) |
| { |
| mode_t allow_utime = sbi->options.allow_utime; |
| |
| if (!uid_eq(current_fsuid(), inode->i_uid)) { |
| if (in_group_p(inode->i_gid)) |
| allow_utime >>= 3; |
| if (allow_utime & MAY_WRITE) |
| return 1; |
| } |
| |
| /* use a default check */ |
| return 0; |
| } |
| |
| static int exfat_sanitize_mode(const struct exfat_sb_info *sbi, |
| struct inode *inode, umode_t *mode_ptr) |
| { |
| mode_t i_mode, mask, perm; |
| |
| i_mode = inode->i_mode; |
| |
| if (S_ISREG(i_mode) || S_ISLNK(i_mode)) |
| mask = sbi->options.fs_fmask; |
| else |
| mask = sbi->options.fs_dmask; |
| |
| perm = *mode_ptr & ~(S_IFMT | mask); |
| |
| /* Of the r and x bits, all (subject to umask) must be present.*/ |
| if ((perm & 0555) != (i_mode & 0555)) |
| return -EPERM; |
| |
| if (exfat_mode_can_hold_ro(inode)) { |
| /* |
| * Of the w bits, either all (subject to umask) or none must be |
| * present. |
| */ |
| if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask))) |
| return -EPERM; |
| } else { |
| /* |
| * If exfat_mode_can_hold_ro(inode) is false, can't change w |
| * bits. |
| */ |
| if ((perm & 0222) != (0222 & ~mask)) |
| return -EPERM; |
| } |
| |
| *mode_ptr &= S_IFMT | perm; |
| |
| return 0; |
| } |
| |
| static void exfat_truncate(struct inode *inode, loff_t old_size) |
| { |
| struct super_block *sb = inode->i_sb; |
| struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| struct fs_info_t *p_fs = &(sbi->fs_info); |
| int err; |
| |
| __lock_super(sb); |
| |
| /* |
| * This protects against truncating a file bigger than it was then |
| * trying to write into the hole. |
| */ |
| if (EXFAT_I(inode)->mmu_private > i_size_read(inode)) |
| EXFAT_I(inode)->mmu_private = i_size_read(inode); |
| |
| if (EXFAT_I(inode)->fid.start_clu == 0) |
| goto out; |
| |
| err = ffsTruncateFile(inode, old_size, i_size_read(inode)); |
| if (err) |
| goto out; |
| |
| inode->i_ctime = inode->i_mtime = current_time(inode); |
| if (IS_DIRSYNC(inode)) |
| (void)exfat_sync_inode(inode); |
| else |
| mark_inode_dirty(inode); |
| |
| inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1)) & |
| ~((loff_t)p_fs->cluster_size - 1)) >> 9; |
| out: |
| __unlock_super(sb); |
| } |
| |
| static int exfat_setattr(struct dentry *dentry, struct iattr *attr) |
| { |
| |