1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/version.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/time.h>
10 #include <linux/slab.h>
11 #include <linux/seq_file.h>
12 #include <linux/pagemap.h>
13 #include <linux/mpage.h>
14 #include <linux/buffer_head.h>
15 #include <linux/exportfs.h>
16 #include <linux/mount.h>
17 #include <linux/vfs.h>
18 #include <linux/aio.h>
19 #include <linux/iversion.h>
20 #include <linux/parser.h>
21 #include <linux/uio.h>
22 #include <linux/writeback.h>
23 #include <linux/log2.h>
24 #include <linux/hash.h>
25 #include <linux/backing-dev.h>
26 #include <linux/sched.h>
27 #include <linux/fs_struct.h>
28 #include <linux/namei.h>
29 #include <linux/time.h>
31 #include <linux/string.h>
32 #include <linux/nls.h>
33 #include <linux/mutex.h>
34 #include <linux/swap.h>
36 #define EXFAT_VERSION "1.3.0"
40 static struct kmem_cache *exfat_inode_cachep;
42 // FIXME use commented lines
43 // static int exfat_default_codepage = CONFIG_EXFAT_DEFAULT_CODEPAGE;
44 // static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
45 static int exfat_default_codepage = CONFIG_FAT_DEFAULT_CODEPAGE;
46 static char exfat_default_iocharset[] = CONFIG_FAT_DEFAULT_IOCHARSET;
48 #define INC_IVERSION(x) (inode_inc_iversion(x))
49 #define GET_IVERSION(x) (inode_peek_iversion_raw(x))
50 #define SET_IVERSION(x, y) (inode_set_iversion(x, y))
52 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
53 static int exfat_sync_inode(struct inode *inode);
54 static struct inode *exfat_build_inode(struct super_block *sb,
55 struct file_id_t *fid, loff_t i_pos);
56 static int exfat_write_inode(struct inode *inode,
57 struct writeback_control *wbc);
58 static void exfat_write_super(struct super_block *sb);
60 #define UNIX_SECS_1980 315532800L
62 #if BITS_PER_LONG == 64
63 #define UNIX_SECS_2108 4354819200L
66 /* days between 1.1.70 and 1.1.80 (2 leap days) */
67 #define DAYS_DELTA_DECADE (365 * 10 + 2)
68 /* 120 (2100 - 1980) isn't leap year */
69 #define NO_LEAP_YEAR_2100 (120)
70 #define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100)
72 #define SECS_PER_MIN (60)
73 #define SECS_PER_HOUR (60 * SECS_PER_MIN)
74 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
76 #define MAKE_LEAP_YEAR(leap_year, year) \
78 if (unlikely(year > NO_LEAP_YEAR_2100)) \
79 leap_year = ((year + 3) / 4) - 1; \
81 leap_year = ((year + 3) / 4); \
84 /* Linear day numbers of the respective 1sts in non-leap years. */
85 static time_t accum_days_in_year[] = {
86 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
87 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
90 /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
91 static void exfat_time_fat2unix(struct exfat_sb_info *sbi,
92 struct timespec64 *ts, struct date_time_t *tp)
94 time_t year = tp->Year;
97 MAKE_LEAP_YEAR(ld, year);
99 if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
102 ts->tv_sec = tp->Second +
103 tp->Minute * SECS_PER_MIN +
104 tp->Hour * SECS_PER_HOUR +
105 (ld + accum_days_in_year[(tp->Month)] +
106 (tp->Day - 1)) * SECS_PER_DAY +
107 (year * 365 + DAYS_DELTA_DECADE) * SECS_PER_DAY +
108 sys_tz.tz_minuteswest * SECS_PER_MIN;
113 /* Convert linear UNIX date to a FAT time/date pair. */
114 static void exfat_time_unix2fat(struct exfat_sb_info *sbi,
115 struct timespec64 *ts, struct date_time_t *tp)
117 time_t second = ts->tv_sec;
118 time_t day, month, year;
121 second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
123 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
124 if (second < UNIX_SECS_1980) {
133 #if (BITS_PER_LONG == 64)
134 if (second >= UNIX_SECS_2108) {
144 day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
146 MAKE_LEAP_YEAR(ld, year);
147 if (year * 365 + ld > day)
150 MAKE_LEAP_YEAR(ld, year);
151 day -= year * 365 + ld;
153 if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
156 if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
158 for (month = 1; month < 12; month++) {
159 if (accum_days_in_year[month + 1] > day)
163 day -= accum_days_in_year[month];
165 tp->Second = second % SECS_PER_MIN;
166 tp->Minute = (second / SECS_PER_MIN) % 60;
167 tp->Hour = (second / SECS_PER_HOUR) % 24;
173 struct timestamp_t *tm_current(struct timestamp_t *tp)
175 struct timespec64 ts;
176 time_t second, day, leap_day, month, year;
178 ktime_get_real_ts64(&ts);
181 second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
183 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
184 if (second < UNIX_SECS_1980) {
193 #if BITS_PER_LONG == 64
194 if (second >= UNIX_SECS_2108) {
205 day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
208 MAKE_LEAP_YEAR(leap_day, year);
209 if (year * 365 + leap_day > day)
212 MAKE_LEAP_YEAR(leap_day, year);
214 day -= year * 365 + leap_day;
216 if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
219 if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
221 for (month = 1; month < 12; month++) {
222 if (accum_days_in_year[month + 1] > day)
226 day -= accum_days_in_year[month];
228 tp->sec = second % SECS_PER_MIN;
229 tp->min = (second / SECS_PER_MIN) % 60;
230 tp->hour = (second / SECS_PER_HOUR) % 24;
238 static void __lock_super(struct super_block *sb)
240 struct exfat_sb_info *sbi = EXFAT_SB(sb);
242 mutex_lock(&sbi->s_lock);
245 static void __unlock_super(struct super_block *sb)
247 struct exfat_sb_info *sbi = EXFAT_SB(sb);
249 mutex_unlock(&sbi->s_lock);
252 static int __is_sb_dirty(struct super_block *sb)
254 struct exfat_sb_info *sbi = EXFAT_SB(sb);
259 static void __set_sb_clean(struct super_block *sb)
261 struct exfat_sb_info *sbi = EXFAT_SB(sb);
266 static int __exfat_revalidate(struct dentry *dentry)
271 static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
273 if (flags & LOOKUP_RCU)
278 return __exfat_revalidate(dentry);
281 static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
283 if (flags & LOOKUP_RCU)
292 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
295 return __exfat_revalidate(dentry);
298 static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
300 while (len && name[len - 1] == '.')
305 static unsigned int exfat_striptail_len(const struct qstr *qstr)
307 return __exfat_striptail_len(qstr->len, qstr->name);
310 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
312 qstr->hash = full_name_hash(dentry, qstr->name,
313 exfat_striptail_len(qstr));
317 static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
319 struct super_block *sb = dentry->d_sb;
320 const unsigned char *name;
325 len = exfat_striptail_len(qstr);
327 hash = init_name_hash(dentry);
329 hash = partial_name_hash(nls_upper(sb, *name++), hash);
330 qstr->hash = end_name_hash(hash);
335 static int exfat_cmpi(const struct dentry *dentry, unsigned int len,
336 const char *str, const struct qstr *name)
338 struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
339 unsigned int alen, blen;
341 alen = exfat_striptail_len(name);
342 blen = __exfat_striptail_len(len, str);
345 if (strncasecmp(name->name, str, alen) == 0)
348 if (nls_strnicmp(t, name->name, str, alen) == 0)
355 static int exfat_cmp(const struct dentry *dentry, unsigned int len,
356 const char *str, const struct qstr *name)
358 unsigned int alen, blen;
360 alen = exfat_striptail_len(name);
361 blen = __exfat_striptail_len(len, str);
363 if (strncmp(name->name, str, alen) == 0)
369 static const struct dentry_operations exfat_ci_dentry_ops = {
370 .d_revalidate = exfat_revalidate_ci,
371 .d_hash = exfat_d_hashi,
372 .d_compare = exfat_cmpi,
375 static const struct dentry_operations exfat_dentry_ops = {
376 .d_revalidate = exfat_revalidate,
377 .d_hash = exfat_d_hash,
378 .d_compare = exfat_cmp,
381 static DEFINE_SEMAPHORE(z_sem);
383 static inline void fs_sync(struct super_block *sb, bool do_sync)
390 * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to
391 * save ATTR_RO instead of ->i_mode.
393 * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
394 * bit, it's just used as flag for app.
396 static inline int exfat_mode_can_hold_ro(struct inode *inode)
398 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
400 if (S_ISDIR(inode->i_mode))
403 if ((~sbi->options.fs_fmask) & 0222)
408 /* Convert attribute bits and a mask to the UNIX mode. */
409 static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi, u32 attr,
412 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
415 if (attr & ATTR_SUBDIR)
416 return (mode & ~sbi->options.fs_dmask) | S_IFDIR;
417 else if (attr & ATTR_SYMLINK)
418 return (mode & ~sbi->options.fs_dmask) | S_IFLNK;
420 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
423 /* Return the FAT attribute byte for this inode */
424 static inline u32 exfat_make_attr(struct inode *inode)
426 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
427 return (EXFAT_I(inode)->fid.attr) | ATTR_READONLY;
429 return EXFAT_I(inode)->fid.attr;
432 static inline void exfat_save_attr(struct inode *inode, u32 attr)
434 if (exfat_mode_can_hold_ro(inode))
435 EXFAT_I(inode)->fid.attr = attr & ATTR_RWMASK;
437 EXFAT_I(inode)->fid.attr = attr & (ATTR_RWMASK | ATTR_READONLY);
440 static int ffsMountVol(struct super_block *sb)
443 struct pbr_sector_t *p_pbr;
444 struct buffer_head *tmp_bh = NULL;
445 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
446 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
448 pr_info("[EXFAT] trying to mount...\n");
454 sema_init(&p_fs->v_sem, 1);
455 p_fs->dev_ejected = 0;
457 /* open the block device */
460 if (p_bd->sector_size < sb->s_blocksize) {
464 if (p_bd->sector_size > sb->s_blocksize)
465 sb_set_blocksize(sb, p_bd->sector_size);
468 if (sector_read(sb, 0, &tmp_bh, 1) != FFS_SUCCESS) {
473 p_fs->PBR_sector = 0;
475 p_pbr = (struct pbr_sector_t *) tmp_bh->b_data;
477 /* check the validity of PBR */
478 if (GET16_A(p_pbr->signature) != PBR_SIGNATURE) {
486 for (i = 0; i < 53; i++)
491 #ifdef CONFIG_EXFAT_DONT_MOUNT_VFAT
493 printk(KERN_INFO "EXFAT: Attempted to mount VFAT filesystem\n");
496 if (GET16(p_pbr->bpb + 11)) /* num_fat_sectors */
497 ret = fat16_mount(sb, p_pbr);
499 ret = fat32_mount(sb, p_pbr);
502 ret = exfat_mount(sb, p_pbr);
512 if (p_fs->vol_type == EXFAT) {
513 ret = load_alloc_bitmap(sb);
518 ret = load_upcase_table(sb);
520 free_alloc_bitmap(sb);
526 if (p_fs->dev_ejected) {
527 if (p_fs->vol_type == EXFAT) {
528 free_upcase_table(sb);
529 free_alloc_bitmap(sb);
536 pr_info("[EXFAT] mounted successfully\n");
544 static int ffsUmountVol(struct super_block *sb)
546 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
547 int err = FFS_SUCCESS;
549 pr_info("[EXFAT] trying to unmount...\n");
553 /* acquire the lock for file system critical section */
557 fs_set_vol_flags(sb, VOL_CLEAN);
559 if (p_fs->vol_type == EXFAT) {
560 free_upcase_table(sb);
561 free_alloc_bitmap(sb);
567 /* close the block device */
570 if (p_fs->dev_ejected) {
571 pr_info("[EXFAT] unmounted with media errors. Device is already ejected.\n");
577 /* release the lock for file system critical section */
581 pr_info("[EXFAT] unmounted successfully\n");
586 static int ffsGetVolInfo(struct super_block *sb, struct vol_info_t *info)
588 int err = FFS_SUCCESS;
589 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
591 /* check the validity of pointer parameters */
595 /* acquire the lock for file system critical section */
598 if (p_fs->used_clusters == (u32) ~0)
599 p_fs->used_clusters = p_fs->fs_func->count_used_clusters(sb);
601 info->FatType = p_fs->vol_type;
602 info->ClusterSize = p_fs->cluster_size;
603 info->NumClusters = p_fs->num_clusters - 2; /* clu 0 & 1 */
604 info->UsedClusters = p_fs->used_clusters;
605 info->FreeClusters = info->NumClusters - info->UsedClusters;
607 if (p_fs->dev_ejected)
610 /* release the lock for file system critical section */
616 static int ffsSyncVol(struct super_block *sb, bool do_sync)
618 int err = FFS_SUCCESS;
619 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
621 /* acquire the lock for file system critical section */
624 /* synchronize the file system */
625 fs_sync(sb, do_sync);
626 fs_set_vol_flags(sb, VOL_CLEAN);
628 if (p_fs->dev_ejected)
631 /* release the lock for file system critical section */
637 /*----------------------------------------------------------------------*/
638 /* File Operation Functions */
639 /*----------------------------------------------------------------------*/
641 static int ffsLookupFile(struct inode *inode, char *path, struct file_id_t *fid)
643 int ret, dentry, num_entries;
645 struct uni_name_t uni_name;
646 struct dos_name_t dos_name;
647 struct dentry_t *ep, *ep2;
648 struct entry_set_cache_t *es = NULL;
649 struct super_block *sb = inode->i_sb;
650 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
652 pr_debug("%s entered\n", __func__);
654 /* check the validity of pointer parameters */
655 if (!fid || !path || (*path == '\0'))
658 /* acquire the lock for file system critical section */
661 /* check the validity of directory name in the given pathname */
662 ret = resolve_path(inode, path, &dir, &uni_name);
666 ret = get_num_entries_and_dos_name(sb, &dir, &uni_name, &num_entries,
671 /* search the file name for directories */
672 dentry = p_fs->fs_func->find_dir_entry(sb, &dir, &uni_name, num_entries,
673 &dos_name, TYPE_ALL);
679 fid->dir.dir = dir.dir;
680 fid->dir.size = dir.size;
681 fid->dir.flags = dir.flags;
685 fid->type = TYPE_DIR;
687 fid->hint_last_off = -1;
689 fid->attr = ATTR_SUBDIR;
692 fid->start_clu = p_fs->root_dir;
694 if (p_fs->vol_type == EXFAT) {
695 es = get_entry_set_in_dir(sb, &dir, dentry,
703 ep = get_entry_in_dir(sb, &dir, dentry, NULL);
711 fid->type = p_fs->fs_func->get_entry_type(ep);
713 fid->hint_last_off = -1;
714 fid->attr = p_fs->fs_func->get_entry_attr(ep);
716 fid->size = p_fs->fs_func->get_entry_size(ep2);
717 if ((fid->type == TYPE_FILE) && (fid->size == 0)) {
718 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
719 fid->start_clu = CLUSTER_32(~0);
721 fid->flags = p_fs->fs_func->get_entry_flag(ep2);
722 fid->start_clu = p_fs->fs_func->get_entry_clu0(ep2);
725 if (p_fs->vol_type == EXFAT)
726 release_entry_set(es);
729 if (p_fs->dev_ejected)
732 /* release the lock for file system critical section */
738 static int ffsCreateFile(struct inode *inode, char *path, u8 mode,
739 struct file_id_t *fid)
742 struct uni_name_t uni_name;
743 struct super_block *sb = inode->i_sb;
744 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
747 /* check the validity of pointer parameters */
748 if (!fid || !path || (*path == '\0'))
751 /* acquire the lock for file system critical section */
754 /* check the validity of directory name in the given pathname */
755 ret = resolve_path(inode, path, &dir, &uni_name);
759 fs_set_vol_flags(sb, VOL_DIRTY);
761 /* create a new file */
762 ret = create_file(inode, &dir, &uni_name, mode, fid);
764 #ifdef CONFIG_EXFAT_DELAYED_SYNC
766 fs_set_vol_flags(sb, VOL_CLEAN);
769 if (p_fs->dev_ejected)
773 /* release the lock for file system critical section */
779 static int ffsReadFile(struct inode *inode, struct file_id_t *fid, void *buffer,
780 u64 count, u64 *rcount)
782 s32 offset, sec_offset, clu_offset;
786 u64 oneblkread, read_bytes;
787 struct buffer_head *tmp_bh = NULL;
788 struct super_block *sb = inode->i_sb;
789 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
790 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
792 /* check the validity of the given file id */
794 return FFS_INVALIDFID;
796 /* check the validity of pointer parameters */
800 /* acquire the lock for file system critical section */
803 /* check if the given file ID is opened */
804 if (fid->type != TYPE_FILE) {
805 ret = FFS_PERMISSIONERR;
809 if (fid->rwoffset > fid->size)
810 fid->rwoffset = fid->size;
812 if (count > (fid->size - fid->rwoffset))
813 count = fid->size - fid->rwoffset;
825 clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
826 clu = fid->start_clu;
828 if (fid->flags == 0x03) {
831 /* hint information */
832 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
833 (clu_offset >= fid->hint_last_off)) {
834 clu_offset -= fid->hint_last_off;
835 clu = fid->hint_last_clu;
838 while (clu_offset > 0) {
839 /* clu = FAT_read(sb, clu); */
840 if (FAT_read(sb, clu, &clu) == -1)
847 /* hint information */
848 fid->hint_last_off = (s32)(fid->rwoffset >>
849 p_fs->cluster_size_bits);
850 fid->hint_last_clu = clu;
852 /* byte offset in cluster */
853 offset = (s32)(fid->rwoffset & (p_fs->cluster_size-1));
855 /* sector offset in cluster */
856 sec_offset = offset >> p_bd->sector_size_bits;
858 /* byte offset in sector */
859 offset &= p_bd->sector_size_mask;
861 LogSector = START_SECTOR(clu) + sec_offset;
863 oneblkread = (u64)(p_bd->sector_size - offset);
864 if (oneblkread > count)
867 if ((offset == 0) && (oneblkread == p_bd->sector_size)) {
868 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
871 memcpy((char *)buffer + read_bytes,
872 (char *)tmp_bh->b_data, (s32)oneblkread);
874 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
877 memcpy((char *)buffer + read_bytes,
878 (char *)tmp_bh->b_data + offset,
882 read_bytes += oneblkread;
883 fid->rwoffset += oneblkread;
887 /* How did this ever work and not leak a brlse()?? */
889 /* set the size of read bytes */
891 *rcount = read_bytes;
893 if (p_fs->dev_ejected)
897 /* release the lock for file system critical section */
903 static int ffsWriteFile(struct inode *inode, struct file_id_t *fid,
904 void *buffer, u64 count, u64 *wcount)
906 bool modified = false;
907 s32 offset, sec_offset, clu_offset;
908 s32 num_clusters, num_alloc, num_alloced = (s32) ~0;
911 sector_t LogSector, sector = 0;
912 u64 oneblkwrite, write_bytes;
913 struct chain_t new_clu;
914 struct timestamp_t tm;
915 struct dentry_t *ep, *ep2;
916 struct entry_set_cache_t *es = NULL;
917 struct buffer_head *tmp_bh = NULL;
918 struct super_block *sb = inode->i_sb;
919 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
920 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
922 /* check the validity of the given file id */
924 return FFS_INVALIDFID;
926 /* check the validity of pointer parameters */
930 /* acquire the lock for file system critical section */
933 /* check if the given file ID is opened */
934 if (fid->type != TYPE_FILE) {
935 ret = FFS_PERMISSIONERR;
939 if (fid->rwoffset > fid->size)
940 fid->rwoffset = fid->size;
949 fs_set_vol_flags(sb, VOL_DIRTY);
954 num_clusters = (s32)((fid->size-1) >>
955 p_fs->cluster_size_bits) + 1;
960 clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
961 clu = last_clu = fid->start_clu;
963 if (fid->flags == 0x03) {
964 if ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
965 last_clu += clu_offset - 1;
967 if (clu_offset == num_clusters)
968 clu = CLUSTER_32(~0);
973 /* hint information */
974 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
975 (clu_offset >= fid->hint_last_off)) {
976 clu_offset -= fid->hint_last_off;
977 clu = fid->hint_last_clu;
980 while ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
982 /* clu = FAT_read(sb, clu); */
983 if (FAT_read(sb, clu, &clu) == -1) {
991 if (clu == CLUSTER_32(~0)) {
992 num_alloc = (s32)((count - 1) >>
993 p_fs->cluster_size_bits) + 1;
994 new_clu.dir = (last_clu == CLUSTER_32(~0)) ?
995 CLUSTER_32(~0) : last_clu+1;
997 new_clu.flags = fid->flags;
999 /* (1) allocate a chain of clusters */
1000 num_alloced = p_fs->fs_func->alloc_cluster(sb,
1003 if (num_alloced == 0)
1005 if (num_alloced < 0) {
1010 /* (2) append to the FAT chain */
1011 if (last_clu == CLUSTER_32(~0)) {
1012 if (new_clu.flags == 0x01)
1014 fid->start_clu = new_clu.dir;
1017 if (new_clu.flags != fid->flags) {
1018 exfat_chain_cont_cluster(sb,
1024 if (new_clu.flags == 0x01)
1025 FAT_write(sb, last_clu, new_clu.dir);
1028 num_clusters += num_alloced;
1032 /* hint information */
1033 fid->hint_last_off = (s32)(fid->rwoffset >>
1034 p_fs->cluster_size_bits);
1035 fid->hint_last_clu = clu;
1037 /* byte offset in cluster */
1038 offset = (s32)(fid->rwoffset & (p_fs->cluster_size - 1));
1040 /* sector offset in cluster */
1041 sec_offset = offset >> p_bd->sector_size_bits;
1043 /* byte offset in sector */
1044 offset &= p_bd->sector_size_mask;
1046 LogSector = START_SECTOR(clu) + sec_offset;
1048 oneblkwrite = (u64)(p_bd->sector_size - offset);
1049 if (oneblkwrite > count)
1050 oneblkwrite = count;
1052 if ((offset == 0) && (oneblkwrite == p_bd->sector_size)) {
1053 if (sector_read(sb, LogSector, &tmp_bh, 0) !=
1056 memcpy((char *)tmp_bh->b_data,
1057 (char *)buffer + write_bytes, (s32)oneblkwrite);
1058 if (sector_write(sb, LogSector, tmp_bh, 0) !=
1065 ((fid->rwoffset+oneblkwrite) < fid->size)) {
1066 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
1070 if (sector_read(sb, LogSector, &tmp_bh, 0) !=
1075 memcpy((char *)tmp_bh->b_data + offset,
1076 (char *)buffer + write_bytes, (s32)oneblkwrite);
1077 if (sector_write(sb, LogSector, tmp_bh, 0) !=
1084 count -= oneblkwrite;
1085 write_bytes += oneblkwrite;
1086 fid->rwoffset += oneblkwrite;
1088 fid->attr |= ATTR_ARCHIVE;
1090 if (fid->size < fid->rwoffset) {
1091 fid->size = fid->rwoffset;
1098 /* (3) update the direcoty entry */
1099 if (p_fs->vol_type == EXFAT) {
1100 es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry,
1101 ES_ALL_ENTRIES, &ep);
1106 ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or);
1112 p_fs->fs_func->set_entry_time(ep, tm_current(&tm), TM_MODIFY);
1113 p_fs->fs_func->set_entry_attr(ep, fid->attr);
1115 if (p_fs->vol_type != EXFAT)
1116 buf_modify(sb, sector);
1119 if (p_fs->fs_func->get_entry_flag(ep2) != fid->flags)
1120 p_fs->fs_func->set_entry_flag(ep2, fid->flags);
1122 if (p_fs->fs_func->get_entry_size(ep2) != fid->size)
1123 p_fs->fs_func->set_entry_size(ep2, fid->size);
1125 if (p_fs->fs_func->get_entry_clu0(ep2) != fid->start_clu)
1126 p_fs->fs_func->set_entry_clu0(ep2, fid->start_clu);
1128 if (p_fs->vol_type != EXFAT)
1129 buf_modify(sb, sector);
1132 if (p_fs->vol_type == EXFAT) {
1133 update_dir_checksum_with_entry_set(sb, es);
1134 release_entry_set(es);
1137 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1139 fs_set_vol_flags(sb, VOL_CLEAN);
1143 /* set the size of written bytes */
1145 *wcount = write_bytes;
1147 if (num_alloced == 0)
1150 else if (p_fs->dev_ejected)
1154 /* release the lock for file system critical section */
1160 static int ffsTruncateFile(struct inode *inode, u64 old_size, u64 new_size)
1163 u32 last_clu = CLUSTER_32(0);
1165 sector_t sector = 0;
1167 struct timestamp_t tm;
1168 struct dentry_t *ep, *ep2;
1169 struct super_block *sb = inode->i_sb;
1170 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1171 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1172 struct entry_set_cache_t *es = NULL;
1174 pr_debug("%s entered (inode %p size %llu)\n", __func__, inode,
1177 /* acquire the lock for file system critical section */
1180 /* check if the given file ID is opened */
1181 if (fid->type != TYPE_FILE) {
1182 ret = FFS_PERMISSIONERR;
1186 if (fid->size != old_size) {
1187 pr_err("[EXFAT] truncate : can't skip it because of size-mismatch(old:%lld->fid:%lld).\n",
1188 old_size, fid->size);
1191 if (old_size <= new_size) {
1196 fs_set_vol_flags(sb, VOL_DIRTY);
1198 clu.dir = fid->start_clu;
1199 clu.size = (s32)((old_size-1) >> p_fs->cluster_size_bits) + 1;
1200 clu.flags = fid->flags;
1203 num_clusters = (s32)((new_size-1) >>
1204 p_fs->cluster_size_bits) + 1;
1206 if (clu.flags == 0x03) {
1207 clu.dir += num_clusters;
1209 while (num_clusters > 0) {
1211 if (FAT_read(sb, clu.dir, &clu.dir) == -1) {
1219 clu.size -= num_clusters;
1222 fid->size = new_size;
1223 fid->attr |= ATTR_ARCHIVE;
1224 if (new_size == 0) {
1225 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
1226 fid->start_clu = CLUSTER_32(~0);
1229 /* (1) update the directory entry */
1230 if (p_fs->vol_type == EXFAT) {
1231 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1232 ES_ALL_ENTRIES, &ep);
1239 ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or);
1247 p_fs->fs_func->set_entry_time(ep, tm_current(&tm), TM_MODIFY);
1248 p_fs->fs_func->set_entry_attr(ep, fid->attr);
1250 p_fs->fs_func->set_entry_size(ep2, new_size);
1251 if (new_size == 0) {
1252 p_fs->fs_func->set_entry_flag(ep2, 0x01);
1253 p_fs->fs_func->set_entry_clu0(ep2, CLUSTER_32(0));
1256 if (p_fs->vol_type != EXFAT) {
1257 buf_modify(sb, sector);
1259 update_dir_checksum_with_entry_set(sb, es);
1260 release_entry_set(es);
1263 /* (2) cut off from the FAT chain */
1264 if (last_clu != CLUSTER_32(0)) {
1265 if (fid->flags == 0x01)
1266 FAT_write(sb, last_clu, CLUSTER_32(~0));
1269 /* (3) free the clusters */
1270 p_fs->fs_func->free_cluster(sb, &clu, 0);
1272 /* hint information */
1273 fid->hint_last_off = -1;
1274 if (fid->rwoffset > fid->size)
1275 fid->rwoffset = fid->size;
1277 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1279 fs_set_vol_flags(sb, VOL_CLEAN);
1282 if (p_fs->dev_ejected)
1286 pr_debug("%s exited (%d)\n", __func__, ret);
1287 /* release the lock for file system critical section */
1293 static void update_parent_info(struct file_id_t *fid,
1294 struct inode *parent_inode)
1296 struct fs_info_t *p_fs = &(EXFAT_SB(parent_inode->i_sb)->fs_info);
1297 struct file_id_t *parent_fid = &(EXFAT_I(parent_inode)->fid);
1299 if (unlikely((parent_fid->flags != fid->dir.flags) ||
1300 (parent_fid->size !=
1301 (fid->dir.size << p_fs->cluster_size_bits)) ||
1302 (parent_fid->start_clu != fid->dir.dir))) {
1303 fid->dir.dir = parent_fid->start_clu;
1304 fid->dir.flags = parent_fid->flags;
1305 fid->dir.size = ((parent_fid->size + (p_fs->cluster_size-1))
1306 >> p_fs->cluster_size_bits);
1310 static int ffsMoveFile(struct inode *old_parent_inode, struct file_id_t *fid,
1311 struct inode *new_parent_inode, struct dentry *new_dentry)
1315 struct chain_t olddir, newdir;
1316 struct chain_t *p_dir = NULL;
1317 struct uni_name_t uni_name;
1318 struct dentry_t *ep;
1319 struct super_block *sb = old_parent_inode->i_sb;
1320 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1321 u8 *new_path = (u8 *) new_dentry->d_name.name;
1322 struct inode *new_inode = new_dentry->d_inode;
1324 struct file_id_t *new_fid = NULL;
1327 /* check the validity of the given file id */
1329 return FFS_INVALIDFID;
1331 /* check the validity of pointer parameters */
1332 if (!new_path || (*new_path == '\0'))
1335 /* acquire the lock for file system critical section */
1338 update_parent_info(fid, old_parent_inode);
1340 olddir.dir = fid->dir.dir;
1341 olddir.size = fid->dir.size;
1342 olddir.flags = fid->dir.flags;
1344 dentry = fid->entry;
1346 /* check if the old file is "." or ".." */
1347 if (p_fs->vol_type != EXFAT) {
1348 if ((olddir.dir != p_fs->root_dir) && (dentry < 2)) {
1349 ret = FFS_PERMISSIONERR;
1354 ep = get_entry_in_dir(sb, &olddir, dentry, NULL);
1360 if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY) {
1361 ret = FFS_PERMISSIONERR;
1365 /* check whether new dir is existing directory and empty */
1370 new_fid = &EXFAT_I(new_inode)->fid;
1372 update_parent_info(new_fid, new_parent_inode);
1374 p_dir = &(new_fid->dir);
1375 new_entry = new_fid->entry;
1376 ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
1380 entry_type = p_fs->fs_func->get_entry_type(ep);
1382 if (entry_type == TYPE_DIR) {
1383 struct chain_t new_clu;
1385 new_clu.dir = new_fid->start_clu;
1386 new_clu.size = (s32)((new_fid->size - 1) >>
1387 p_fs->cluster_size_bits) + 1;
1388 new_clu.flags = new_fid->flags;
1390 if (!is_dir_empty(sb, &new_clu)) {
1391 ret = FFS_FILEEXIST;
1397 /* check the validity of directory name in the given new pathname */
1398 ret = resolve_path(new_parent_inode, new_path, &newdir, &uni_name);
1402 fs_set_vol_flags(sb, VOL_DIRTY);
1404 if (olddir.dir == newdir.dir)
1405 ret = rename_file(new_parent_inode, &olddir, dentry, &uni_name,
1408 ret = move_file(new_parent_inode, &olddir, dentry, &newdir,
1411 if ((ret == FFS_SUCCESS) && new_inode) {
1412 /* delete entries of new_dir */
1413 ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
1417 num_entries = p_fs->fs_func->count_ext_entries(sb, p_dir,
1419 if (num_entries < 0)
1421 p_fs->fs_func->delete_dir_entry(sb, p_dir, new_entry, 0,
1425 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1427 fs_set_vol_flags(sb, VOL_CLEAN);
1430 if (p_fs->dev_ejected)
1433 /* release the lock for file system critical section */
1439 static int ffsRemoveFile(struct inode *inode, struct file_id_t *fid)
1442 int ret = FFS_SUCCESS;
1443 struct chain_t dir, clu_to_free;
1444 struct dentry_t *ep;
1445 struct super_block *sb = inode->i_sb;
1446 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1448 /* check the validity of the given file id */
1450 return FFS_INVALIDFID;
1452 /* acquire the lock for file system critical section */
1455 dir.dir = fid->dir.dir;
1456 dir.size = fid->dir.size;
1457 dir.flags = fid->dir.flags;
1459 dentry = fid->entry;
1461 ep = get_entry_in_dir(sb, &dir, dentry, NULL);
1467 if (p_fs->fs_func->get_entry_attr(ep) & ATTR_READONLY) {
1468 ret = FFS_PERMISSIONERR;
1471 fs_set_vol_flags(sb, VOL_DIRTY);
1473 /* (1) update the directory entry */
1474 remove_file(inode, &dir, dentry);
1476 clu_to_free.dir = fid->start_clu;
1477 clu_to_free.size = (s32)((fid->size-1) >> p_fs->cluster_size_bits) + 1;
1478 clu_to_free.flags = fid->flags;
1480 /* (2) free the clusters */
1481 p_fs->fs_func->free_cluster(sb, &clu_to_free, 0);
1484 fid->start_clu = CLUSTER_32(~0);
1485 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
1487 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1489 fs_set_vol_flags(sb, VOL_CLEAN);
1492 if (p_fs->dev_ejected)
1495 /* release the lock for file system critical section */
1502 /* Not currently wired up */
1503 static int ffsSetAttr(struct inode *inode, u32 attr)
1506 int ret = FFS_SUCCESS;
1507 sector_t sector = 0;
1508 struct dentry_t *ep;
1509 struct super_block *sb = inode->i_sb;
1510 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1511 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1512 u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
1513 struct entry_set_cache_t *es = NULL;
1515 if (fid->attr == attr) {
1516 if (p_fs->dev_ejected)
1517 return FFS_MEDIAERR;
1522 if ((fid->dir.dir == p_fs->root_dir) &&
1523 (fid->entry == -1)) {
1524 if (p_fs->dev_ejected)
1525 return FFS_MEDIAERR;
1530 /* acquire the lock for file system critical section */
1533 /* get the directory entry of given file */
1534 if (p_fs->vol_type == EXFAT) {
1535 es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry,
1536 ES_ALL_ENTRIES, &ep);
1542 ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or);
1549 type = p_fs->fs_func->get_entry_type(ep);
1551 if (((type == TYPE_FILE) && (attr & ATTR_SUBDIR)) ||
1552 ((type == TYPE_DIR) && (!(attr & ATTR_SUBDIR)))) {
1553 if (p_fs->dev_ejected)
1558 if (p_fs->vol_type == EXFAT)
1559 release_entry_set(es);
1563 fs_set_vol_flags(sb, VOL_DIRTY);
1565 /* set the file attribute */
1567 p_fs->fs_func->set_entry_attr(ep, attr);
1569 if (p_fs->vol_type != EXFAT) {
1570 buf_modify(sb, sector);
1572 update_dir_checksum_with_entry_set(sb, es);
1573 release_entry_set(es);
1576 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1578 fs_set_vol_flags(sb, VOL_CLEAN);
1581 if (p_fs->dev_ejected)
1584 /* release the lock for file system critical section */
1591 static int ffsReadStat(struct inode *inode, struct dir_entry_t *info)
1593 sector_t sector = 0;
1595 int ret = FFS_SUCCESS;
1597 struct uni_name_t uni_name;
1598 struct timestamp_t tm;
1599 struct dentry_t *ep, *ep2;
1600 struct super_block *sb = inode->i_sb;
1601 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1602 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1603 struct entry_set_cache_t *es = NULL;
1604 u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
1606 pr_debug("%s entered\n", __func__);
1608 /* acquire the lock for file system critical section */
1612 if ((fid->dir.dir == p_fs->root_dir) &&
1613 (fid->entry == -1)) {
1614 info->Attr = ATTR_SUBDIR;
1615 memset((char *)&info->CreateTimestamp, 0,
1616 sizeof(struct date_time_t));
1617 memset((char *)&info->ModifyTimestamp, 0,
1618 sizeof(struct date_time_t));
1619 memset((char *)&info->AccessTimestamp, 0,
1620 sizeof(struct date_time_t));
1621 strcpy(info->ShortName, ".");
1622 strcpy(info->Name, ".");
1624 dir.dir = p_fs->root_dir;
1627 if (p_fs->root_dir == CLUSTER_32(0)) {
1628 /* FAT16 root_dir */
1629 info->Size = p_fs->dentries_in_root <<
1632 info->Size = count_num_clusters(sb, &dir) <<
1633 p_fs->cluster_size_bits;
1636 count = count_dos_name_entries(sb, &dir, TYPE_DIR);
1641 info->NumSubdirs = count;
1643 if (p_fs->dev_ejected)
1649 /* get the directory entry of given file or directory */
1650 if (p_fs->vol_type == EXFAT) {
1651 es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry,
1659 ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or);
1665 buf_lock(sb, sector);
1668 /* set FILE_INFO structure using the acquired struct dentry_t */
1669 info->Attr = p_fs->fs_func->get_entry_attr(ep);
1671 p_fs->fs_func->get_entry_time(ep, &tm, TM_CREATE);
1672 info->CreateTimestamp.Year = tm.year;
1673 info->CreateTimestamp.Month = tm.mon;
1674 info->CreateTimestamp.Day = tm.day;
1675 info->CreateTimestamp.Hour = tm.hour;
1676 info->CreateTimestamp.Minute = tm.min;
1677 info->CreateTimestamp.Second = tm.sec;
1678 info->CreateTimestamp.MilliSecond = 0;
1680 p_fs->fs_func->get_entry_time(ep, &tm, TM_MODIFY);
1681 info->ModifyTimestamp.Year = tm.year;
1682 info->ModifyTimestamp.Month = tm.mon;
1683 info->ModifyTimestamp.Day = tm.day;
1684 info->ModifyTimestamp.Hour = tm.hour;
1685 info->ModifyTimestamp.Minute = tm.min;
1686 info->ModifyTimestamp.Second = tm.sec;
1687 info->ModifyTimestamp.MilliSecond = 0;
1689 memset((char *) &info->AccessTimestamp, 0, sizeof(struct date_time_t));
1691 *(uni_name.name) = 0x0;
1692 /* XXX this is very bad for exfat cuz name is already included in es.
1693 * API should be revised
1695 p_fs->fs_func->get_uni_name_from_ext_entry(sb, &(fid->dir), fid->entry,
1697 if (*uni_name.name == 0x0 && p_fs->vol_type != EXFAT)
1698 get_uni_name_from_dos_entry(sb, (struct dos_dentry_t *)ep,
1700 nls_uniname_to_cstring(sb, info->Name, &uni_name);
1702 if (p_fs->vol_type == EXFAT) {
1703 info->NumSubdirs = 2;
1705 buf_unlock(sb, sector);
1706 get_uni_name_from_dos_entry(sb, (struct dos_dentry_t *)ep,
1708 nls_uniname_to_cstring(sb, info->ShortName, &uni_name);
1709 info->NumSubdirs = 0;
1712 info->Size = p_fs->fs_func->get_entry_size(ep2);
1714 if (p_fs->vol_type == EXFAT)
1715 release_entry_set(es);
1718 dir.dir = fid->start_clu;
1721 if (info->Size == 0)
1722 info->Size = (u64)count_num_clusters(sb, &dir) <<
1723 p_fs->cluster_size_bits;
1725 count = count_dos_name_entries(sb, &dir, TYPE_DIR);
1730 info->NumSubdirs += count;
1733 if (p_fs->dev_ejected)
1737 /* release the lock for file system critical section */
1740 pr_debug("%s exited successfully\n", __func__);
1744 static int ffsWriteStat(struct inode *inode, struct dir_entry_t *info)
1746 sector_t sector = 0;
1747 int ret = FFS_SUCCESS;
1748 struct timestamp_t tm;
1749 struct dentry_t *ep, *ep2;
1750 struct entry_set_cache_t *es = NULL;
1751 struct super_block *sb = inode->i_sb;
1752 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1753 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1754 u8 is_dir = (fid->type == TYPE_DIR) ? 1 : 0;
1756 pr_debug("%s entered (inode %p info %p\n", __func__, inode, info);
1758 /* acquire the lock for file system critical section */
1762 if ((fid->dir.dir == p_fs->root_dir) &&
1763 (fid->entry == -1)) {
1764 if (p_fs->dev_ejected)
1771 fs_set_vol_flags(sb, VOL_DIRTY);
1773 /* get the directory entry of given file or directory */
1774 if (p_fs->vol_type == EXFAT) {
1775 es = get_entry_set_in_dir(sb, &(fid->dir), fid->entry,
1776 ES_ALL_ENTRIES, &ep);
1783 /* for other than exfat */
1784 ep = get_entry_in_dir(sb, &(fid->dir), fid->entry, §or);
1792 p_fs->fs_func->set_entry_attr(ep, info->Attr);
1794 /* set FILE_INFO structure using the acquired struct dentry_t */
1795 tm.sec = info->CreateTimestamp.Second;
1796 tm.min = info->CreateTimestamp.Minute;
1797 tm.hour = info->CreateTimestamp.Hour;
1798 tm.day = info->CreateTimestamp.Day;
1799 tm.mon = info->CreateTimestamp.Month;
1800 tm.year = info->CreateTimestamp.Year;
1801 p_fs->fs_func->set_entry_time(ep, &tm, TM_CREATE);
1803 tm.sec = info->ModifyTimestamp.Second;
1804 tm.min = info->ModifyTimestamp.Minute;
1805 tm.hour = info->ModifyTimestamp.Hour;
1806 tm.day = info->ModifyTimestamp.Day;
1807 tm.mon = info->ModifyTimestamp.Month;
1808 tm.year = info->ModifyTimestamp.Year;
1809 p_fs->fs_func->set_entry_time(ep, &tm, TM_MODIFY);
1811 p_fs->fs_func->set_entry_size(ep2, info->Size);
1813 if (p_fs->vol_type != EXFAT) {
1814 buf_modify(sb, sector);
1816 update_dir_checksum_with_entry_set(sb, es);
1817 release_entry_set(es);
1820 if (p_fs->dev_ejected)
1824 /* release the lock for file system critical section */
1827 pr_debug("%s exited (%d)\n", __func__, ret);
1832 static int ffsMapCluster(struct inode *inode, s32 clu_offset, u32 *clu)
1834 s32 num_clusters, num_alloced;
1835 bool modified = false;
1837 int ret = FFS_SUCCESS;
1838 sector_t sector = 0;
1839 struct chain_t new_clu;
1840 struct dentry_t *ep;
1841 struct entry_set_cache_t *es = NULL;
1842 struct super_block *sb = inode->i_sb;
1843 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1844 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1846 /* check the validity of pointer parameters */
1850 /* acquire the lock for file system critical section */
1853 fid->rwoffset = (s64)(clu_offset) << p_fs->cluster_size_bits;
1855 if (EXFAT_I(inode)->mmu_private == 0)
1858 num_clusters = (s32)((EXFAT_I(inode)->mmu_private - 1) >>
1859 p_fs->cluster_size_bits) + 1;
1861 *clu = last_clu = fid->start_clu;
1863 if (fid->flags == 0x03) {
1864 if ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
1865 last_clu += clu_offset - 1;
1867 if (clu_offset == num_clusters)
1868 *clu = CLUSTER_32(~0);
1873 /* hint information */
1874 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
1875 (clu_offset >= fid->hint_last_off)) {
1876 clu_offset -= fid->hint_last_off;
1877 *clu = fid->hint_last_clu;
1880 while ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
1882 if (FAT_read(sb, *clu, clu) == -1) {
1890 if (*clu == CLUSTER_32(~0)) {
1891 fs_set_vol_flags(sb, VOL_DIRTY);
1893 new_clu.dir = (last_clu == CLUSTER_32(~0)) ? CLUSTER_32(~0) :
1896 new_clu.flags = fid->flags;
1898 /* (1) allocate a cluster */
1899 num_alloced = p_fs->fs_func->alloc_cluster(sb, 1, &new_clu);
1900 if (num_alloced < 0) {
1903 } else if (num_alloced == 0) {
1908 /* (2) append to the FAT chain */
1909 if (last_clu == CLUSTER_32(~0)) {
1910 if (new_clu.flags == 0x01)
1912 fid->start_clu = new_clu.dir;
1915 if (new_clu.flags != fid->flags) {
1916 exfat_chain_cont_cluster(sb, fid->start_clu,
1921 if (new_clu.flags == 0x01)
1922 FAT_write(sb, last_clu, new_clu.dir);
1925 num_clusters += num_alloced;
1928 if (p_fs->vol_type == EXFAT) {
1929 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1930 ES_ALL_ENTRIES, &ep);
1935 /* get stream entry */
1939 /* (3) update directory entry */
1941 if (p_fs->vol_type != EXFAT) {
1942 ep = get_entry_in_dir(sb, &(fid->dir),
1943 fid->entry, §or);
1950 if (p_fs->fs_func->get_entry_flag(ep) != fid->flags)
1951 p_fs->fs_func->set_entry_flag(ep, fid->flags);
1953 if (p_fs->fs_func->get_entry_clu0(ep) != fid->start_clu)
1954 p_fs->fs_func->set_entry_clu0(ep,
1957 if (p_fs->vol_type != EXFAT)
1958 buf_modify(sb, sector);
1961 if (p_fs->vol_type == EXFAT) {
1962 update_dir_checksum_with_entry_set(sb, es);
1963 release_entry_set(es);
1966 /* add number of new blocks to inode */
1967 inode->i_blocks += num_alloced << (p_fs->cluster_size_bits - 9);
1970 /* hint information */
1971 fid->hint_last_off = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
1972 fid->hint_last_clu = *clu;
1974 if (p_fs->dev_ejected)
1978 /* release the lock for file system critical section */
1984 /*----------------------------------------------------------------------*/
1985 /* Directory Operation Functions */
1986 /*----------------------------------------------------------------------*/
1988 static int ffsCreateDir(struct inode *inode, char *path, struct file_id_t *fid)
1990 int ret = FFS_SUCCESS;
1992 struct uni_name_t uni_name;
1993 struct super_block *sb = inode->i_sb;
1994 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1996 pr_debug("%s entered\n", __func__);
1998 /* check the validity of pointer parameters */
1999 if (!fid || !path || (*path == '\0'))
2002 /* acquire the lock for file system critical section */
2005 /* check the validity of directory name in the given old pathname */
2006 ret = resolve_path(inode, path, &dir, &uni_name);
2010 fs_set_vol_flags(sb, VOL_DIRTY);
2012 ret = create_dir(inode, &dir, &uni_name, fid);
2014 #ifdef CONFIG_EXFAT_DELAYED_SYNC
2016 fs_set_vol_flags(sb, VOL_CLEAN);
2019 if (p_fs->dev_ejected)
2022 /* release the lock for file system critical section */
2028 static int ffsReadDir(struct inode *inode, struct dir_entry_t *dir_entry)
2030 int i, dentry, clu_offset;
2031 int ret = FFS_SUCCESS;
2032 s32 dentries_per_clu, dentries_per_clu_bits = 0;
2035 struct chain_t dir, clu;
2036 struct uni_name_t uni_name;
2037 struct timestamp_t tm;
2038 struct dentry_t *ep;
2039 struct super_block *sb = inode->i_sb;
2040 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2041 struct fs_func *fs_func = p_fs->fs_func;
2042 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
2044 /* check the validity of pointer parameters */
2048 /* check if the given file ID is opened */
2049 if (fid->type != TYPE_DIR)
2050 return FFS_PERMISSIONERR;
2052 /* acquire the lock for file system critical section */
2055 if (fid->entry == -1) {
2056 dir.dir = p_fs->root_dir;
2059 dir.dir = fid->start_clu;
2060 dir.size = (s32)(fid->size >> p_fs->cluster_size_bits);
2061 dir.flags = fid->flags;
2064 dentry = (s32)fid->rwoffset;
2066 if (dir.dir == CLUSTER_32(0)) {
2067 /* FAT16 root_dir */
2068 dentries_per_clu = p_fs->dentries_in_root;
2070 if (dentry == dentries_per_clu) {
2071 clu.dir = CLUSTER_32(~0);
2074 clu.size = dir.size;
2075 clu.flags = dir.flags;
2078 dentries_per_clu = p_fs->dentries_per_clu;
2079 dentries_per_clu_bits = ilog2(dentries_per_clu);
2081 clu_offset = dentry >> dentries_per_clu_bits;
2083 clu.size = dir.size;
2084 clu.flags = dir.flags;
2086 if (clu.flags == 0x03) {
2087 clu.dir += clu_offset;
2088 clu.size -= clu_offset;
2090 /* hint_information */
2091 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
2092 (clu_offset >= fid->hint_last_off)) {
2093 clu_offset -= fid->hint_last_off;
2094 clu.dir = fid->hint_last_clu;
2097 while (clu_offset > 0) {
2098 /* clu.dir = FAT_read(sb, clu.dir); */
2099 if (FAT_read(sb, clu.dir, &clu.dir) == -1) {
2108 while (clu.dir != CLUSTER_32(~0)) {
2109 if (p_fs->dev_ejected)
2112 if (dir.dir == CLUSTER_32(0)) /* FAT16 root_dir */
2113 i = dentry % dentries_per_clu;
2115 i = dentry & (dentries_per_clu-1);
2117 for ( ; i < dentries_per_clu; i++, dentry++) {
2118 ep = get_entry_in_dir(sb, &clu, i, §or);
2123 type = fs_func->get_entry_type(ep);
2125 if (type == TYPE_UNUSED)
2128 if ((type != TYPE_FILE) && (type != TYPE_DIR))
2131 buf_lock(sb, sector);
2132 dir_entry->Attr = fs_func->get_entry_attr(ep);
2134 fs_func->get_entry_time(ep, &tm, TM_CREATE);
2135 dir_entry->CreateTimestamp.Year = tm.year;
2136 dir_entry->CreateTimestamp.Month = tm.mon;
2137 dir_entry->CreateTimestamp.Day = tm.day;
2138 dir_entry->CreateTimestamp.Hour = tm.hour;
2139 dir_entry->CreateTimestamp.Minute = tm.min;
2140 dir_entry->CreateTimestamp.Second = tm.sec;
2141 dir_entry->CreateTimestamp.MilliSecond = 0;
2143 fs_func->get_entry_time(ep, &tm, TM_MODIFY);
2144 dir_entry->ModifyTimestamp.Year = tm.year;
2145 dir_entry->ModifyTimestamp.Month = tm.mon;
2146 dir_entry->ModifyTimestamp.Day = tm.day;
2147 dir_entry->ModifyTimestamp.Hour = tm.hour;
2148 dir_entry->ModifyTimestamp.Minute = tm.min;
2149 dir_entry->ModifyTimestamp.Second = tm.sec;
2150 dir_entry->ModifyTimestamp.MilliSecond = 0;
2152 memset((char *)&dir_entry->AccessTimestamp, 0,
2153 sizeof(struct date_time_t));
2155 *(uni_name.name) = 0x0;
2156 fs_func->get_uni_name_from_ext_entry(sb, &dir, dentry,
2158 if (*uni_name.name == 0x0 && p_fs->vol_type != EXFAT)
2159 get_uni_name_from_dos_entry(sb,
2160 (struct dos_dentry_t *)ep,
2162 nls_uniname_to_cstring(sb, dir_entry->Name, &uni_name);
2163 buf_unlock(sb, sector);
2165 if (p_fs->vol_type == EXFAT) {
2166 ep = get_entry_in_dir(sb, &clu, i+1, NULL);
2172 get_uni_name_from_dos_entry(sb,
2173 (struct dos_dentry_t *)ep,
2175 nls_uniname_to_cstring(sb, dir_entry->ShortName,
2179 dir_entry->Size = fs_func->get_entry_size(ep);
2181 /* hint information */
2182 if (dir.dir == CLUSTER_32(0)) { /* FAT16 root_dir */
2184 fid->hint_last_off = dentry >>
2185 dentries_per_clu_bits;
2186 fid->hint_last_clu = clu.dir;
2189 fid->rwoffset = (s64) ++dentry;
2191 if (p_fs->dev_ejected)
2196 if (dir.dir == CLUSTER_32(0))
2197 break; /* FAT16 root_dir */
2199 if (clu.flags == 0x03) {
2200 if ((--clu.size) > 0)
2203 clu.dir = CLUSTER_32(~0);
2205 /* clu.dir = FAT_read(sb, clu.dir); */
2206 if (FAT_read(sb, clu.dir, &clu.dir) == -1) {
2213 *(dir_entry->Name) = '\0';
2215 fid->rwoffset = (s64) ++dentry;
2217 if (p_fs->dev_ejected)
2221 /* release the lock for file system critical section */
2227 static int ffsRemoveDir(struct inode *inode, struct file_id_t *fid)
2230 int ret = FFS_SUCCESS;
2231 struct chain_t dir, clu_to_free;
2232 struct super_block *sb = inode->i_sb;
2233 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2235 /* check the validity of the given file id */
2237 return FFS_INVALIDFID;
2239 dir.dir = fid->dir.dir;
2240 dir.size = fid->dir.size;
2241 dir.flags = fid->dir.flags;
2243 dentry = fid->entry;
2245 /* check if the file is "." or ".." */
2246 if (p_fs->vol_type != EXFAT) {
2247 if ((dir.dir != p_fs->root_dir) && (dentry < 2))
2248 return FFS_PERMISSIONERR;
2251 /* acquire the lock for file system critical section */
2254 clu_to_free.dir = fid->start_clu;
2255 clu_to_free.size = (s32)((fid->size-1) >> p_fs->cluster_size_bits) + 1;
2256 clu_to_free.flags = fid->flags;
2258 if (!is_dir_empty(sb, &clu_to_free)) {
2259 ret = FFS_FILEEXIST;
2263 fs_set_vol_flags(sb, VOL_DIRTY);
2265 /* (1) update the directory entry */
2266 remove_file(inode, &dir, dentry);
2268 /* (2) free the clusters */
2269 p_fs->fs_func->free_cluster(sb, &clu_to_free, 1);
2272 fid->start_clu = CLUSTER_32(~0);
2273 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2275 #ifdef CONFIG_EXFAT_DELAYED_SYNC
2277 fs_set_vol_flags(sb, VOL_CLEAN);
2280 if (p_fs->dev_ejected)
2284 /* release the lock for file system critical section */
2290 /*======================================================================*/
2291 /* Directory Entry Operations */
2292 /*======================================================================*/
2294 static int exfat_readdir(struct file *filp, struct dir_context *ctx)
2296 struct inode *inode = file_inode(filp);
2297 struct super_block *sb = inode->i_sb;
2298 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2299 struct fs_info_t *p_fs = &(sbi->fs_info);
2300 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
2301 struct dir_entry_t de;
2309 /* Fake . and .. for the root directory. */
2310 if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
2312 if (inode->i_ino == EXFAT_ROOT_INO)
2313 inum = EXFAT_ROOT_INO;
2315 inum = inode->i_ino;
2316 else /* (cpos == 1) */
2317 inum = parent_ino(filp->f_path.dentry);
2319 if (!dir_emit_dots(filp, ctx))
2327 if (cpos & (DENTRY_SIZE - 1)) {
2333 EXFAT_I(inode)->fid.size = i_size_read(inode);
2334 EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
2336 err = ffsReadDir(inode, &de);
2338 /* at least we tried to read a sector
2339 * move cpos to next sector position (should be aligned)
2341 if (err == FFS_MEDIAERR) {
2342 cpos += 1 << p_bd->sector_size_bits;
2343 cpos &= ~((1 << p_bd->sector_size_bits)-1);
2350 cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
2355 if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
2356 inum = inode->i_ino;
2357 } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
2358 inum = parent_ino(filp->f_path.dentry);
2360 loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
2361 ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
2362 struct inode *tmp = exfat_iget(sb, i_pos);
2368 inum = iunique(sb, EXFAT_ROOT_INO);
2372 if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
2373 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
2386 static int exfat_ioctl_volume_id(struct inode *dir)
2388 struct super_block *sb = dir->i_sb;
2389 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2390 struct fs_info_t *p_fs = &(sbi->fs_info);
2392 return p_fs->vol_id;
2395 static long exfat_generic_ioctl(struct file *filp, unsigned int cmd,
2398 struct inode *inode = filp->f_path.dentry->d_inode;
2399 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2401 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2404 case EXFAT_IOCTL_GET_VOLUME_ID:
2405 return exfat_ioctl_volume_id(inode);
2406 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2407 case EXFAT_IOC_GET_DEBUGFLAGS: {
2408 struct super_block *sb = inode->i_sb;
2409 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2411 flags = sbi->debug_flags;
2412 return put_user(flags, (int __user *)arg);
2414 case EXFAT_IOC_SET_DEBUGFLAGS: {
2415 struct super_block *sb = inode->i_sb;
2416 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2418 if (!capable(CAP_SYS_ADMIN))
2421 if (get_user(flags, (int __user *) arg))
2425 sbi->debug_flags = flags;
2430 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2432 return -ENOTTY; /* Inappropriate ioctl for device */
2436 static const struct file_operations exfat_dir_operations = {
2437 .llseek = generic_file_llseek,
2438 .read = generic_read_dir,
2439 .iterate = exfat_readdir,
2440 .unlocked_ioctl = exfat_generic_ioctl,
2441 .fsync = generic_file_fsync,
2444 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2447 struct super_block *sb = dir->i_sb;
2448 struct inode *inode;
2449 struct file_id_t fid;
2455 pr_debug("%s entered\n", __func__);
2457 err = ffsCreateFile(dir, (u8 *) dentry->d_name.name, FM_REGULAR, &fid);
2459 if (err == FFS_INVALIDPATH)
2461 else if (err == FFS_FILEEXIST)
2463 else if (err == FFS_FULL)
2465 else if (err == FFS_NAMETOOLONG)
2466 err = -ENAMETOOLONG;
2472 dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir);
2473 if (IS_DIRSYNC(dir))
2474 (void) exfat_sync_inode(dir);
2476 mark_inode_dirty(dir);
2478 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2480 inode = exfat_build_inode(sb, &fid, i_pos);
2481 if (IS_ERR(inode)) {
2482 err = PTR_ERR(inode);
2485 INC_IVERSION(inode);
2486 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
2488 * timestamp is already written, so mark_inode_dirty() is unnecessary.
2491 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2492 d_instantiate(dentry, inode);
2496 pr_debug("%s exited\n", __func__);
2500 static int exfat_find(struct inode *dir, struct qstr *qname,
2501 struct file_id_t *fid)
2505 if (qname->len == 0)
2508 err = ffsLookupFile(dir, (u8 *) qname->name, fid);
2515 static int exfat_d_anon_disconn(struct dentry *dentry)
2517 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
2520 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
2523 struct super_block *sb = dir->i_sb;
2524 struct inode *inode;
2525 struct dentry *alias;
2527 struct file_id_t fid;
2533 pr_debug("%s entered\n", __func__);
2534 err = exfat_find(dir, &dentry->d_name, &fid);
2536 if (err == -ENOENT) {
2543 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2544 inode = exfat_build_inode(sb, &fid, i_pos);
2545 if (IS_ERR(inode)) {
2546 err = PTR_ERR(inode);
2550 i_mode = inode->i_mode;
2551 if (S_ISLNK(i_mode) && !EXFAT_I(inode)->target) {
2552 EXFAT_I(inode)->target = kmalloc(i_size_read(inode) + 1,
2554 if (!EXFAT_I(inode)->target) {
2558 ffsReadFile(dir, &fid, EXFAT_I(inode)->target,
2559 i_size_read(inode), &ret);
2560 *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
2563 alias = d_find_alias(inode);
2564 if (alias && !exfat_d_anon_disconn(alias)) {
2565 BUG_ON(d_unhashed(alias));
2566 if (!S_ISDIR(i_mode))
2567 d_move(alias, dentry);
2570 pr_debug("%s exited 1\n", __func__);
2576 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2577 dentry = d_splice_alias(inode, dentry);
2579 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2580 pr_debug("%s exited 2\n", __func__);
2585 pr_debug("%s exited 3\n", __func__);
2586 return ERR_PTR(err);
2589 static inline unsigned long exfat_hash(loff_t i_pos)
2591 return hash_32(i_pos, EXFAT_HASH_BITS);
2594 static void exfat_attach(struct inode *inode, loff_t i_pos)
2596 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
2597 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
2599 spin_lock(&sbi->inode_hash_lock);
2600 EXFAT_I(inode)->i_pos = i_pos;
2601 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
2602 spin_unlock(&sbi->inode_hash_lock);
2605 static void exfat_detach(struct inode *inode)
2607 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
2609 spin_lock(&sbi->inode_hash_lock);
2610 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
2611 EXFAT_I(inode)->i_pos = 0;
2612 spin_unlock(&sbi->inode_hash_lock);
2615 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
2617 struct inode *inode = dentry->d_inode;
2618 struct super_block *sb = dir->i_sb;
2623 pr_debug("%s entered\n", __func__);
2625 EXFAT_I(inode)->fid.size = i_size_read(inode);
2627 err = ffsRemoveFile(dir, &(EXFAT_I(inode)->fid));
2629 if (err == FFS_PERMISSIONERR)
2636 dir->i_mtime = dir->i_atime = current_time(dir);
2637 if (IS_DIRSYNC(dir))
2638 (void) exfat_sync_inode(dir);
2640 mark_inode_dirty(dir);
2643 inode->i_mtime = inode->i_atime = current_time(inode);
2644 exfat_detach(inode);
2645 remove_inode_hash(inode);
2649 pr_debug("%s exited\n", __func__);
2653 static int exfat_symlink(struct inode *dir, struct dentry *dentry,
2656 struct super_block *sb = dir->i_sb;
2657 struct inode *inode;
2658 struct file_id_t fid;
2661 u64 len = (u64) strlen(target);
2666 pr_debug("%s entered\n", __func__);
2668 err = ffsCreateFile(dir, (u8 *) dentry->d_name.name, FM_SYMLINK, &fid);
2670 if (err == FFS_INVALIDPATH)
2672 else if (err == FFS_FILEEXIST)
2674 else if (err == FFS_FULL)
2681 err = ffsWriteFile(dir, &fid, (char *) target, len, &ret);
2684 ffsRemoveFile(dir, &fid);
2686 if (err == FFS_FULL)
2694 dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir);
2695 if (IS_DIRSYNC(dir))
2696 (void) exfat_sync_inode(dir);
2698 mark_inode_dirty(dir);
2700 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2702 inode = exfat_build_inode(sb, &fid, i_pos);
2703 if (IS_ERR(inode)) {
2704 err = PTR_ERR(inode);
2707 INC_IVERSION(inode);
2708 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
2709 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2711 EXFAT_I(inode)->target = kmalloc(len+1, GFP_KERNEL);
2712 if (!EXFAT_I(inode)->target) {
2716 memcpy(EXFAT_I(inode)->target, target, len+1);
2718 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2719 d_instantiate(dentry, inode);
2723 pr_debug("%s exited\n", __func__);
2727 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2729 struct super_block *sb = dir->i_sb;
2730 struct inode *inode;
2731 struct file_id_t fid;
2737 pr_debug("%s entered\n", __func__);
2739 err = ffsCreateDir(dir, (u8 *) dentry->d_name.name, &fid);
2741 if (err == FFS_INVALIDPATH)
2743 else if (err == FFS_FILEEXIST)
2745 else if (err == FFS_FULL)
2747 else if (err == FFS_NAMETOOLONG)
2748 err = -ENAMETOOLONG;
2754 dir->i_ctime = dir->i_mtime = dir->i_atime = current_time(dir);
2755 if (IS_DIRSYNC(dir))
2756 (void) exfat_sync_inode(dir);
2758 mark_inode_dirty(dir);
2761 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2763 inode = exfat_build_inode(sb, &fid, i_pos);
2764 if (IS_ERR(inode)) {
2765 err = PTR_ERR(inode);
2768 INC_IVERSION(inode);
2769 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
2770 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2772 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2773 d_instantiate(dentry, inode);
2777 pr_debug("%s exited\n", __func__);
2781 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
2783 struct inode *inode = dentry->d_inode;
2784 struct super_block *sb = dir->i_sb;
2789 pr_debug("%s entered\n", __func__);
2791 EXFAT_I(inode)->fid.size = i_size_read(inode);
2793 err = ffsRemoveDir(dir, &(EXFAT_I(inode)->fid));
2795 if (err == FFS_INVALIDPATH)
2797 else if (err == FFS_FILEEXIST)
2799 else if (err == FFS_NOTFOUND)
2801 else if (err == FFS_DIRBUSY)
2808 dir->i_mtime = dir->i_atime = current_time(dir);
2809 if (IS_DIRSYNC(dir))
2810 (void) exfat_sync_inode(dir);
2812 mark_inode_dirty(dir);
2816 inode->i_mtime = inode->i_atime = current_time(inode);
2817 exfat_detach(inode);
2818 remove_inode_hash(inode);
2822 pr_debug("%s exited\n", __func__);
2826 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
2827 struct inode *new_dir, struct dentry *new_dentry,
2830 struct inode *old_inode, *new_inode;
2831 struct super_block *sb = old_dir->i_sb;
2840 pr_debug("%s entered\n", __func__);
2842 old_inode = old_dentry->d_inode;
2843 new_inode = new_dentry->d_inode;
2845 EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
2847 err = ffsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir,
2850 if (err == FFS_PERMISSIONERR)
2852 else if (err == FFS_INVALIDPATH)
2854 else if (err == FFS_FILEEXIST)
2856 else if (err == FFS_NOTFOUND)
2858 else if (err == FFS_FULL)
2864 INC_IVERSION(new_dir);
2865 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
2866 current_time(new_dir);
2867 if (IS_DIRSYNC(new_dir))
2868 (void) exfat_sync_inode(new_dir);
2870 mark_inode_dirty(new_dir);
2872 i_pos = ((loff_t) EXFAT_I(old_inode)->fid.dir.dir << 32) |
2873 (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
2875 exfat_detach(old_inode);
2876 exfat_attach(old_inode, i_pos);
2877 if (IS_DIRSYNC(new_dir))
2878 (void) exfat_sync_inode(old_inode);
2880 mark_inode_dirty(old_inode);
2882 if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
2883 drop_nlink(old_dir);
2887 INC_IVERSION(old_dir);
2888 old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
2889 if (IS_DIRSYNC(old_dir))
2890 (void) exfat_sync_inode(old_dir);
2892 mark_inode_dirty(old_dir);
2895 exfat_detach(new_inode);
2896 drop_nlink(new_inode);
2897 if (S_ISDIR(new_inode->i_mode))
2898 drop_nlink(new_inode);
2899 new_inode->i_ctime = current_time(new_inode);
2904 pr_debug("%s exited\n", __func__);
2908 static int exfat_cont_expand(struct inode *inode, loff_t size)
2910 struct address_space *mapping = inode->i_mapping;
2911 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
2914 err = generic_cont_expand_simple(inode, size);
2918 inode->i_ctime = inode->i_mtime = current_time(inode);
2919 mark_inode_dirty(inode);
2921 if (IS_SYNC(inode)) {
2922 err = filemap_fdatawrite_range(mapping, start,
2924 err2 = sync_mapping_buffers(mapping);
2925 err = (err) ? (err) : (err2);
2926 err2 = write_inode_now(inode, 1);
2927 err = (err) ? (err) : (err2);
2929 err = filemap_fdatawait_range(mapping, start,
2935 static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
2937 mode_t allow_utime = sbi->options.allow_utime;
2939 if (!uid_eq(current_fsuid(), inode->i_uid)) {
2940 if (in_group_p(inode->i_gid))
2942 if (allow_utime & MAY_WRITE)
2946 /* use a default check */
2950 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
2951 struct inode *inode, umode_t *mode_ptr)
2953 mode_t i_mode, mask, perm;
2955 i_mode = inode->i_mode;
2957 if (S_ISREG(i_mode) || S_ISLNK(i_mode))
2958 mask = sbi->options.fs_fmask;
2960 mask = sbi->options.fs_dmask;
2962 perm = *mode_ptr & ~(S_IFMT | mask);
2964 /* Of the r and x bits, all (subject to umask) must be present.*/
2965 if ((perm & 0555) != (i_mode & 0555))
2968 if (exfat_mode_can_hold_ro(inode)) {
2970 * Of the w bits, either all (subject to umask) or none must be
2973 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
2977 * If exfat_mode_can_hold_ro(inode) is false, can't change w
2980 if ((perm & 0222) != (0222 & ~mask))
2984 *mode_ptr &= S_IFMT | perm;
2989 static void exfat_truncate(struct inode *inode, loff_t old_size)
2991 struct super_block *sb = inode->i_sb;
2992 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2993 struct fs_info_t *p_fs = &(sbi->fs_info);
2999 * This protects against truncating a file bigger than it was then
3000 * trying to write into the hole.
3002 if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
3003 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3005 if (EXFAT_I(inode)->fid.start_clu == 0)
3008 err = ffsTruncateFile(inode, old_size, i_size_read(inode));
3012 inode->i_ctime = inode->i_mtime = current_time(inode);
3013 if (IS_DIRSYNC(inode))
3014 (void) exfat_sync_inode(inode);
3016 mark_inode_dirty(inode);
3018 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1)) &
3019 ~((loff_t)p_fs->cluster_size - 1)) >> 9;
3024 static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
3026 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
3027 struct inode *inode = dentry->d_inode;
3028 unsigned int ia_valid;
3032 pr_debug("%s entered\n", __func__);
3034 if ((attr->ia_valid & ATTR_SIZE)
3035 && (attr->ia_size > i_size_read(inode))) {
3036 error = exfat_cont_expand(inode, attr->ia_size);
3037 if (error || attr->ia_valid == ATTR_SIZE)
3039 attr->ia_valid &= ~ATTR_SIZE;
3042 ia_valid = attr->ia_valid;
3044 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
3045 && exfat_allow_set_time(sbi, inode)) {
3046 attr->ia_valid &= ~(ATTR_MTIME_SET |
3051 error = setattr_prepare(dentry, attr);
3052 attr->ia_valid = ia_valid;
3056 if (((attr->ia_valid & ATTR_UID) &&
3057 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
3058 ((attr->ia_valid & ATTR_GID) &&
3059 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
3060 ((attr->ia_valid & ATTR_MODE) &&
3061 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
3066 * We don't return -EPERM here. Yes, strange, but this is too
3069 if (attr->ia_valid & ATTR_MODE) {
3070 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
3071 attr->ia_valid &= ~ATTR_MODE;
3074 EXFAT_I(inode)->fid.size = i_size_read(inode);
3076 if (attr->ia_valid & ATTR_SIZE) {
3077 old_size = i_size_read(inode);
3078 down_write(&EXFAT_I(inode)->truncate_lock);
3079 truncate_setsize(inode, attr->ia_size);
3080 exfat_truncate(inode, old_size);
3081 up_write(&EXFAT_I(inode)->truncate_lock);
3083 setattr_copy(inode, attr);
3084 mark_inode_dirty(inode);
3086 pr_debug("%s exited\n", __func__);
3090 static int exfat_getattr(const struct path *path, struct kstat *stat,
3091 u32 request_mask, unsigned int flags)
3093 struct inode *inode = path->dentry->d_inode;
3095 pr_debug("%s entered\n", __func__);
3097 generic_fillattr(inode, stat);
3098 stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
3100 pr_debug("%s exited\n", __func__);
3104 static const struct inode_operations exfat_dir_inode_operations = {
3105 .create = exfat_create,
3106 .lookup = exfat_lookup,
3107 .unlink = exfat_unlink,
3108 .symlink = exfat_symlink,
3109 .mkdir = exfat_mkdir,
3110 .rmdir = exfat_rmdir,
3111 .rename = exfat_rename,
3112 .setattr = exfat_setattr,
3113 .getattr = exfat_getattr,
3116 /*======================================================================*/
3117 /* File Operations */
3118 /*======================================================================*/
3119 static const char *exfat_get_link(struct dentry *dentry, struct inode *inode,
3120 struct delayed_call *done)
3122 struct exfat_inode_info *ei = EXFAT_I(inode);
3125 char *cookie = ei->target;
3128 return (char *)(ei->target);
3133 static const struct inode_operations exfat_symlink_inode_operations = {
3134 .get_link = exfat_get_link,
3137 static int exfat_file_release(struct inode *inode, struct file *filp)
3139 struct super_block *sb = inode->i_sb;
3141 EXFAT_I(inode)->fid.size = i_size_read(inode);
3142 ffsSyncVol(sb, false);
3146 static const struct file_operations exfat_file_operations = {
3147 .llseek = generic_file_llseek,
3148 .read_iter = generic_file_read_iter,
3149 .write_iter = generic_file_write_iter,
3150 .mmap = generic_file_mmap,
3151 .release = exfat_file_release,
3152 .unlocked_ioctl = exfat_generic_ioctl,
3153 .fsync = generic_file_fsync,
3154 .splice_read = generic_file_splice_read,
3157 static const struct inode_operations exfat_file_inode_operations = {
3158 .setattr = exfat_setattr,
3159 .getattr = exfat_getattr,
3162 /*======================================================================*/
3163 /* Address Space Operations */
3164 /*======================================================================*/
3166 static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
3167 unsigned long *mapped_blocks, int *create)
3169 struct super_block *sb = inode->i_sb;
3170 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3171 struct fs_info_t *p_fs = &(sbi->fs_info);
3172 struct bd_info_t *p_bd = &(sbi->bd_info);
3173 const unsigned long blocksize = sb->s_blocksize;
3174 const unsigned char blocksize_bits = sb->s_blocksize_bits;
3175 sector_t last_block;
3176 int err, clu_offset, sec_offset;
3177 unsigned int cluster;
3182 if ((p_fs->vol_type == FAT12) || (p_fs->vol_type == FAT16)) {
3183 if (inode->i_ino == EXFAT_ROOT_INO) {
3185 (p_fs->dentries_in_root >>
3186 (p_bd->sector_size_bits-DENTRY_SIZE_BITS))) {
3187 *phys = sector + p_fs->root_start_sector;
3194 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
3195 if (sector >= last_block) {
3202 /* cluster offset */
3203 clu_offset = sector >> p_fs->sectors_per_clu_bits;
3205 /* sector offset in cluster */
3206 sec_offset = sector & (p_fs->sectors_per_clu - 1);
3208 EXFAT_I(inode)->fid.size = i_size_read(inode);
3210 err = ffsMapCluster(inode, clu_offset, &cluster);
3213 if (err == FFS_FULL)
3217 } else if (cluster != CLUSTER_32(~0)) {
3218 *phys = START_SECTOR(cluster) + sec_offset;
3219 *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
3225 static int exfat_get_block(struct inode *inode, sector_t iblock,
3226 struct buffer_head *bh_result, int create)
3228 struct super_block *sb = inode->i_sb;
3229 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
3231 unsigned long mapped_blocks;
3236 err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
3243 max_blocks = min(mapped_blocks, max_blocks);
3245 EXFAT_I(inode)->mmu_private += max_blocks <<
3246 sb->s_blocksize_bits;
3247 set_buffer_new(bh_result);
3249 map_bh(bh_result, sb, phys);
3252 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
3258 static int exfat_readpage(struct file *file, struct page *page)
3260 return mpage_readpage(page, exfat_get_block);
3263 static int exfat_readpages(struct file *file, struct address_space *mapping,
3264 struct list_head *pages, unsigned int nr_pages)
3266 return mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
3269 static int exfat_writepage(struct page *page, struct writeback_control *wbc)
3271 return block_write_full_page(page, exfat_get_block, wbc);
3274 static int exfat_writepages(struct address_space *mapping,
3275 struct writeback_control *wbc)
3277 return mpage_writepages(mapping, wbc, exfat_get_block);
3280 static void exfat_write_failed(struct address_space *mapping, loff_t to)
3282 struct inode *inode = mapping->host;
3284 if (to > i_size_read(inode)) {
3285 truncate_pagecache(inode, i_size_read(inode));
3286 EXFAT_I(inode)->fid.size = i_size_read(inode);
3287 exfat_truncate(inode, i_size_read(inode));
3291 static int exfat_write_begin(struct file *file, struct address_space *mapping,
3292 loff_t pos, unsigned int len, unsigned int flags,
3293 struct page **pagep, void **fsdata)
3298 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
3300 &EXFAT_I(mapping->host)->mmu_private);
3303 exfat_write_failed(mapping, pos+len);
3307 static int exfat_write_end(struct file *file, struct address_space *mapping,
3308 loff_t pos, unsigned int len, unsigned int copied,
3309 struct page *pagep, void *fsdata)
3311 struct inode *inode = mapping->host;
3312 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
3315 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
3318 exfat_write_failed(mapping, pos+len);
3320 if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
3321 inode->i_mtime = inode->i_ctime = current_time(inode);
3322 fid->attr |= ATTR_ARCHIVE;
3323 mark_inode_dirty(inode);
3328 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3330 struct inode *inode = iocb->ki_filp->f_mapping->host;
3331 struct address_space *mapping = iocb->ki_filp->f_mapping;
3335 rw = iov_iter_rw(iter);
3338 if (EXFAT_I(inode)->mmu_private < iov_iter_count(iter))
3341 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
3343 if ((ret < 0) && (rw & WRITE))
3344 exfat_write_failed(mapping, iov_iter_count(iter));
3348 static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
3352 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
3353 down_read(&EXFAT_I(mapping->host)->truncate_lock);
3354 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
3355 up_read(&EXFAT_I(mapping->host)->truncate_lock);
3360 static const struct address_space_operations exfat_aops = {
3361 .readpage = exfat_readpage,
3362 .readpages = exfat_readpages,
3363 .writepage = exfat_writepage,
3364 .writepages = exfat_writepages,
3365 .write_begin = exfat_write_begin,
3366 .write_end = exfat_write_end,
3367 .direct_IO = exfat_direct_IO,
3371 /*======================================================================*/
3372 /* Super Operations */
3373 /*======================================================================*/
3375 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
3377 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3378 struct exfat_inode_info *info;
3379 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
3380 struct inode *inode = NULL;
3382 spin_lock(&sbi->inode_hash_lock);
3383 hlist_for_each_entry(info, head, i_hash_fat) {
3384 BUG_ON(info->vfs_inode.i_sb != sb);
3386 if (i_pos != info->i_pos)
3388 inode = igrab(&info->vfs_inode);
3392 spin_unlock(&sbi->inode_hash_lock);
3396 /* doesn't deal with root inode */
3397 static int exfat_fill_inode(struct inode *inode, struct file_id_t *fid)
3399 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
3400 struct fs_info_t *p_fs = &(sbi->fs_info);
3401 struct dir_entry_t info;
3403 memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(struct file_id_t));
3405 ffsReadStat(inode, &info);
3407 EXFAT_I(inode)->i_pos = 0;
3408 EXFAT_I(inode)->target = NULL;
3409 inode->i_uid = sbi->options.fs_uid;
3410 inode->i_gid = sbi->options.fs_gid;
3411 INC_IVERSION(inode);
3412 inode->i_generation = get_seconds();
3414 if (info.Attr & ATTR_SUBDIR) { /* directory */
3415 inode->i_generation &= ~1;
3416 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3417 inode->i_op = &exfat_dir_inode_operations;
3418 inode->i_fop = &exfat_dir_operations;
3420 i_size_write(inode, info.Size);
3421 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3422 set_nlink(inode, info.NumSubdirs);
3423 } else if (info.Attr & ATTR_SYMLINK) { /* symbolic link */
3424 inode->i_generation |= 1;
3425 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3426 inode->i_op = &exfat_symlink_inode_operations;
3428 i_size_write(inode, info.Size);
3429 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3430 } else { /* regular file */
3431 inode->i_generation |= 1;
3432 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3433 inode->i_op = &exfat_file_inode_operations;
3434 inode->i_fop = &exfat_file_operations;
3435 inode->i_mapping->a_ops = &exfat_aops;
3436 inode->i_mapping->nrpages = 0;
3438 i_size_write(inode, info.Size);
3439 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3441 exfat_save_attr(inode, info.Attr);
3443 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
3444 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
3446 exfat_time_fat2unix(sbi, &inode->i_mtime, &info.ModifyTimestamp);
3447 exfat_time_fat2unix(sbi, &inode->i_ctime, &info.CreateTimestamp);
3448 exfat_time_fat2unix(sbi, &inode->i_atime, &info.AccessTimestamp);
3453 static struct inode *exfat_build_inode(struct super_block *sb,
3454 struct file_id_t *fid, loff_t i_pos)
3456 struct inode *inode;
3459 inode = exfat_iget(sb, i_pos);
3462 inode = new_inode(sb);
3464 inode = ERR_PTR(-ENOMEM);
3467 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
3468 SET_IVERSION(inode, 1);
3469 err = exfat_fill_inode(inode, fid);
3472 inode = ERR_PTR(err);
3475 exfat_attach(inode, i_pos);
3476 insert_inode_hash(inode);
3481 static int exfat_sync_inode(struct inode *inode)
3483 return exfat_write_inode(inode, NULL);
3486 static struct inode *exfat_alloc_inode(struct super_block *sb)
3488 struct exfat_inode_info *ei;
3490 ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
3494 init_rwsem(&ei->truncate_lock);
3496 return &ei->vfs_inode;
3499 static void exfat_destroy_inode(struct inode *inode)
3501 kfree(EXFAT_I(inode)->target);
3502 EXFAT_I(inode)->target = NULL;
3504 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
3507 static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
3509 struct super_block *sb = inode->i_sb;
3510 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3511 struct dir_entry_t info;
3513 if (inode->i_ino == EXFAT_ROOT_INO)
3516 info.Attr = exfat_make_attr(inode);
3517 info.Size = i_size_read(inode);
3519 exfat_time_unix2fat(sbi, &inode->i_mtime, &info.ModifyTimestamp);
3520 exfat_time_unix2fat(sbi, &inode->i_ctime, &info.CreateTimestamp);
3521 exfat_time_unix2fat(sbi, &inode->i_atime, &info.AccessTimestamp);
3523 ffsWriteStat(inode, &info);
3528 static void exfat_evict_inode(struct inode *inode)
3530 truncate_inode_pages(&inode->i_data, 0);
3532 if (!inode->i_nlink)
3533 i_size_write(inode, 0);
3534 invalidate_inode_buffers(inode);
3536 exfat_detach(inode);
3538 remove_inode_hash(inode);
3541 static void exfat_free_super(struct exfat_sb_info *sbi)
3544 unload_nls(sbi->nls_disk);
3546 unload_nls(sbi->nls_io);
3547 if (sbi->options.iocharset != exfat_default_iocharset)
3548 kfree(sbi->options.iocharset);
3549 /* mutex_init is in exfat_fill_super function. only for 3.7+ */
3550 mutex_destroy(&sbi->s_lock);
3554 static void exfat_put_super(struct super_block *sb)
3556 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3558 if (__is_sb_dirty(sb))
3559 exfat_write_super(sb);
3563 sb->s_fs_info = NULL;
3564 exfat_free_super(sbi);
3567 static void exfat_write_super(struct super_block *sb)
3574 ffsSyncVol(sb, true);
3579 static int exfat_sync_fs(struct super_block *sb, int wait)
3583 if (__is_sb_dirty(sb)) {
3586 err = ffsSyncVol(sb, true);
3593 static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
3595 struct super_block *sb = dentry->d_sb;
3596 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
3597 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3598 struct vol_info_t info;
3600 if (p_fs->used_clusters == (u32) ~0) {
3601 if (ffsGetVolInfo(sb, &info) == FFS_MEDIAERR)
3605 info.FatType = p_fs->vol_type;
3606 info.ClusterSize = p_fs->cluster_size;
3607 info.NumClusters = p_fs->num_clusters - 2;
3608 info.UsedClusters = p_fs->used_clusters;
3609 info.FreeClusters = info.NumClusters - info.UsedClusters;
3611 if (p_fs->dev_ejected)
3612 pr_info("[EXFAT] statfs on device that is ejected\n");
3615 buf->f_type = sb->s_magic;
3616 buf->f_bsize = info.ClusterSize;
3617 buf->f_blocks = info.NumClusters;
3618 buf->f_bfree = info.FreeClusters;
3619 buf->f_bavail = info.FreeClusters;
3620 buf->f_fsid.val[0] = (u32)id;
3621 buf->f_fsid.val[1] = (u32)(id >> 32);
3622 buf->f_namelen = 260;
3627 static int exfat_remount(struct super_block *sb, int *flags, char *data)
3629 *flags |= SB_NODIRATIME;
3633 static int exfat_show_options(struct seq_file *m, struct dentry *root)
3635 struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
3636 struct exfat_mount_options *opts = &sbi->options;
3638 if (__kuid_val(opts->fs_uid))
3639 seq_printf(m, ",uid=%u", __kuid_val(opts->fs_uid));
3640 if (__kgid_val(opts->fs_gid))
3641 seq_printf(m, ",gid=%u", __kgid_val(opts->fs_gid));
3642 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
3643 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
3644 if (opts->allow_utime)
3645 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
3647 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
3649 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
3650 seq_printf(m, ",namecase=%u", opts->casesensitive);
3651 if (opts->errors == EXFAT_ERRORS_CONT)
3652 seq_puts(m, ",errors=continue");
3653 else if (opts->errors == EXFAT_ERRORS_PANIC)
3654 seq_puts(m, ",errors=panic");
3656 seq_puts(m, ",errors=remount-ro");
3657 #ifdef CONFIG_EXFAT_DISCARD
3659 seq_puts(m, ",discard");
3664 static const struct super_operations exfat_sops = {
3665 .alloc_inode = exfat_alloc_inode,
3666 .destroy_inode = exfat_destroy_inode,
3667 .write_inode = exfat_write_inode,
3668 .evict_inode = exfat_evict_inode,
3669 .put_super = exfat_put_super,
3670 .sync_fs = exfat_sync_fs,
3671 .statfs = exfat_statfs,
3672 .remount_fs = exfat_remount,
3673 .show_options = exfat_show_options,
3676 /*======================================================================*/
3677 /* Export Operations */
3678 /*======================================================================*/
3680 static struct inode *exfat_nfs_get_inode(struct super_block *sb, u64 ino,
3683 struct inode *inode = NULL;
3685 if (ino < EXFAT_ROOT_INO)
3687 inode = ilookup(sb, ino);
3689 if (inode && generation && (inode->i_generation != generation)) {
3697 static struct dentry *exfat_fh_to_dentry(struct super_block *sb,
3698 struct fid *fid, int fh_len,
3701 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3702 exfat_nfs_get_inode);
3705 static struct dentry *exfat_fh_to_parent(struct super_block *sb,
3706 struct fid *fid, int fh_len,
3709 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3710 exfat_nfs_get_inode);
3713 static const struct export_operations exfat_export_ops = {
3714 .fh_to_dentry = exfat_fh_to_dentry,
3715 .fh_to_parent = exfat_fh_to_parent,
3718 /*======================================================================*/
3719 /* Super Block Read Operations */
3720 /*======================================================================*/
3738 #ifdef CONFIG_EXFAT_DISCARD
3740 #endif /* EXFAT_CONFIG_DISCARD */
3743 static const match_table_t exfat_tokens = {
3744 {Opt_uid, "uid=%u"},
3745 {Opt_gid, "gid=%u"},
3746 {Opt_umask, "umask=%o"},
3747 {Opt_dmask, "dmask=%o"},
3748 {Opt_fmask, "fmask=%o"},
3749 {Opt_allow_utime, "allow_utime=%o"},
3750 {Opt_codepage, "codepage=%u"},
3751 {Opt_charset, "iocharset=%s"},
3752 {Opt_namecase, "namecase=%u"},
3753 {Opt_debug, "debug"},
3754 {Opt_err_cont, "errors=continue"},
3755 {Opt_err_panic, "errors=panic"},
3756 {Opt_err_ro, "errors=remount-ro"},
3757 {Opt_utf8_hack, "utf8"},
3758 #ifdef CONFIG_EXFAT_DISCARD
3759 {Opt_discard, "discard"},
3760 #endif /* CONFIG_EXFAT_DISCARD */
3764 static int parse_options(char *options, int silent, int *debug,
3765 struct exfat_mount_options *opts)
3768 substring_t args[MAX_OPT_ARGS];
3772 opts->fs_uid = current_uid();
3773 opts->fs_gid = current_gid();
3774 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
3775 opts->allow_utime = (unsigned short) -1;
3776 opts->codepage = exfat_default_codepage;
3777 opts->iocharset = exfat_default_iocharset;
3778 opts->casesensitive = 0;
3779 opts->errors = EXFAT_ERRORS_RO;
3780 #ifdef CONFIG_EXFAT_DISCARD
3788 while ((p = strsep(&options, ","))) {
3794 token = match_token(p, exfat_tokens, args);
3797 if (match_int(&args[0], &option))
3799 opts->fs_uid = KUIDT_INIT(option);
3802 if (match_int(&args[0], &option))
3804 opts->fs_gid = KGIDT_INIT(option);
3809 if (match_octal(&args[0], &option))
3811 if (token != Opt_dmask)
3812 opts->fs_fmask = option;
3813 if (token != Opt_fmask)
3814 opts->fs_dmask = option;
3816 case Opt_allow_utime:
3817 if (match_octal(&args[0], &option))
3819 opts->allow_utime = option & 0022;
3822 if (match_int(&args[0], &option))
3824 opts->codepage = option;
3827 if (opts->iocharset != exfat_default_iocharset)
3828 kfree(opts->iocharset);
3829 iocharset = match_strdup(&args[0]);
3832 opts->iocharset = iocharset;
3835 if (match_int(&args[0], &option))
3837 opts->casesensitive = option;
3840 opts->errors = EXFAT_ERRORS_CONT;
3843 opts->errors = EXFAT_ERRORS_PANIC;
3846 opts->errors = EXFAT_ERRORS_RO;
3851 #ifdef CONFIG_EXFAT_DISCARD
3855 #endif /* CONFIG_EXFAT_DISCARD */
3860 pr_err("[EXFAT] Unrecognized mount option %s or missing value\n",
3867 if (opts->allow_utime == (unsigned short) -1)
3868 opts->allow_utime = ~opts->fs_dmask & 0022;
3873 static void exfat_hash_init(struct super_block *sb)
3875 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3878 spin_lock_init(&sbi->inode_hash_lock);
3879 for (i = 0; i < EXFAT_HASH_SIZE; i++)
3880 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
3883 static int exfat_read_root(struct inode *inode)
3885 struct super_block *sb = inode->i_sb;
3886 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3887 struct fs_info_t *p_fs = &(sbi->fs_info);
3888 struct dir_entry_t info;
3890 EXFAT_I(inode)->fid.dir.dir = p_fs->root_dir;
3891 EXFAT_I(inode)->fid.dir.flags = 0x01;
3892 EXFAT_I(inode)->fid.entry = -1;
3893 EXFAT_I(inode)->fid.start_clu = p_fs->root_dir;
3894 EXFAT_I(inode)->fid.flags = 0x01;
3895 EXFAT_I(inode)->fid.type = TYPE_DIR;
3896 EXFAT_I(inode)->fid.rwoffset = 0;
3897 EXFAT_I(inode)->fid.hint_last_off = -1;
3899 EXFAT_I(inode)->target = NULL;
3901 ffsReadStat(inode, &info);
3903 inode->i_uid = sbi->options.fs_uid;
3904 inode->i_gid = sbi->options.fs_gid;
3905 INC_IVERSION(inode);
3906 inode->i_generation = 0;
3907 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777);
3908 inode->i_op = &exfat_dir_inode_operations;
3909 inode->i_fop = &exfat_dir_operations;
3911 i_size_write(inode, info.Size);
3912 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
3913 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
3914 EXFAT_I(inode)->i_pos = ((loff_t) p_fs->root_dir << 32) | 0xffffffff;
3915 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3917 exfat_save_attr(inode, ATTR_SUBDIR);
3918 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
3919 set_nlink(inode, info.NumSubdirs + 2);
3924 static void setup_dops(struct super_block *sb)
3926 if (EXFAT_SB(sb)->options.casesensitive == 0)
3927 sb->s_d_op = &exfat_ci_dentry_ops;
3929 sb->s_d_op = &exfat_dentry_ops;
3932 static int exfat_fill_super(struct super_block *sb, void *data, int silent)
3934 struct inode *root_inode = NULL;
3935 struct exfat_sb_info *sbi;
3941 * GFP_KERNEL is ok here, because while we do hold the
3942 * supeblock lock, memory pressure can't call back into
3943 * the filesystem, since we're only just about to mount
3944 * it and have no inodes etc active!
3946 sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
3949 mutex_init(&sbi->s_lock);
3950 sb->s_fs_info = sbi;
3951 sb->s_flags |= SB_NODIRATIME;
3952 sb->s_magic = EXFAT_SUPER_MAGIC;
3953 sb->s_op = &exfat_sops;
3954 sb->s_export_op = &exfat_export_ops;
3956 error = parse_options(data, silent, &debug, &sbi->options);
3963 sb_min_blocksize(sb, 512);
3964 sb->s_maxbytes = 0x7fffffffffffffffLL; /* maximum file size */
3966 ret = ffsMountVol(sb);
3969 pr_err("[EXFAT] ffsMountVol failed\n");
3974 /* set up enough so that it can read an inode */
3975 exfat_hash_init(sb);
3978 * The low byte of FAT's first entry must have same value with
3979 * media-field. But in real world, too many devices is
3980 * writing wrong value. So, removed that validity check.
3982 * if (FAT_FIRST_ENT(sb, media) != first)
3985 /* codepage is not meaningful in exfat */
3986 if (sbi->fs_info.vol_type != EXFAT) {
3988 sprintf(buf, "cp%d", sbi->options.codepage);
3989 sbi->nls_disk = load_nls(buf);
3990 if (!sbi->nls_disk) {
3991 pr_err("[EXFAT] Codepage %s not found\n", buf);
3996 sbi->nls_io = load_nls(sbi->options.iocharset);
3999 root_inode = new_inode(sb);
4002 root_inode->i_ino = EXFAT_ROOT_INO;
4003 SET_IVERSION(root_inode, 1);
4005 error = exfat_read_root(root_inode);
4009 exfat_attach(root_inode, EXFAT_I(root_inode)->i_pos);
4010 insert_inode_hash(root_inode);
4011 sb->s_root = d_make_root(root_inode);
4013 pr_err("[EXFAT] Getting the root inode failed\n");
4024 sb->s_fs_info = NULL;
4025 exfat_free_super(sbi);
4029 static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
4030 int flags, const char *dev_name,
4033 return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
4036 static void init_once(void *foo)
4038 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
4040 INIT_HLIST_NODE(&ei->i_hash_fat);
4041 inode_init_once(&ei->vfs_inode);
4044 static int __init exfat_init_inodecache(void)
4046 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
4047 sizeof(struct exfat_inode_info),
4049 (SLAB_RECLAIM_ACCOUNT |
4052 if (!exfat_inode_cachep)
4057 static void __exit exfat_destroy_inodecache(void)
4060 * Make sure all delayed rcu free inodes are flushed before we
4064 kmem_cache_destroy(exfat_inode_cachep);
4067 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
4068 static void exfat_debug_kill_sb(struct super_block *sb)
4070 struct exfat_sb_info *sbi = EXFAT_SB(sb);
4071 struct block_device *bdev = sb->s_bdev;
4072 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
4077 flags = sbi->debug_flags;
4079 if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
4081 * invalidate_bdev drops all device cache include
4082 * dirty. We use this to simulate device removal.
4085 FAT_release_all(sb);
4086 buf_release_all(sb);
4089 invalidate_bdev(bdev);
4093 kill_block_super(sb);
4095 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
4097 static struct file_system_type exfat_fs_type = {
4098 .owner = THIS_MODULE,
4100 .mount = exfat_fs_mount,
4101 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
4102 .kill_sb = exfat_debug_kill_sb,
4104 .kill_sb = kill_block_super,
4105 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
4106 .fs_flags = FS_REQUIRES_DEV,
4109 static int __init init_exfat(void)
4113 BUILD_BUG_ON(sizeof(struct dentry_t) != DENTRY_SIZE);
4114 BUILD_BUG_ON(sizeof(struct dos_dentry_t) != DENTRY_SIZE);
4115 BUILD_BUG_ON(sizeof(struct ext_dentry_t) != DENTRY_SIZE);
4116 BUILD_BUG_ON(sizeof(struct file_dentry_t) != DENTRY_SIZE);
4117 BUILD_BUG_ON(sizeof(struct strm_dentry_t) != DENTRY_SIZE);
4118 BUILD_BUG_ON(sizeof(struct name_dentry_t) != DENTRY_SIZE);
4119 BUILD_BUG_ON(sizeof(struct bmap_dentry_t) != DENTRY_SIZE);
4120 BUILD_BUG_ON(sizeof(struct case_dentry_t) != DENTRY_SIZE);
4121 BUILD_BUG_ON(sizeof(struct volm_dentry_t) != DENTRY_SIZE);
4123 pr_info("exFAT: Version %s\n", EXFAT_VERSION);
4125 err = exfat_init_inodecache();
4129 err = register_filesystem(&exfat_fs_type);
4136 static void __exit exit_exfat(void)
4138 exfat_destroy_inodecache();
4139 unregister_filesystem(&exfat_fs_type);
4142 module_init(init_exfat);
4143 module_exit(exit_exfat);
4145 MODULE_LICENSE("GPL");
4146 MODULE_DESCRIPTION("exFAT Filesystem Driver");
4147 MODULE_ALIAS_FS("exfat");