1 /* Some of the source code in this file came from "linux/fs/fat/file.c","linux/fs/fat/inode.c" and "linux/fs/fat/misc.c". */
5 * Written 1992,1993 by Werner Almesberger
7 * regular file handling primitives for fat-based filesystems
11 * linux/fs/fat/inode.c
13 * Written 1992,1993 by Werner Almesberger
14 * VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
15 * Rewritten for the constant inumbers support by Al Viro
19 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
25 * Written 1992,1993 by Werner Almesberger
26 * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
27 * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
31 * linux/fs/vfat/namei.c
33 * Written 1992,1993 by Werner Almesberger
35 * Windows95/Windows NT compatible extended MSDOS filesystem
36 * by Gordon Chaffee Copyright (C) 1995. Send bug reports for the
37 * VFAT filesystem to <chaffee@cs.berkeley.edu>. Specify
38 * what file operation caused you trouble and if you can duplicate
39 * the problem, send a script that demonstrates it.
41 * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
43 * Support Multibyte characters and cleanup by
44 * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
48 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
50 * This program is free software; you can redistribute it and/or
51 * modify it under the terms of the GNU General Public License
52 * as published by the Free Software Foundation; either version 2
53 * of the License, or (at your option) any later version.
55 * This program is distributed in the hope that it will be useful,
56 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 * GNU General Public License for more details.
60 * You should have received a copy of the GNU General Public License
61 * along with this program; if not, write to the Free Software
62 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
65 #include <linux/version.h>
66 #include <linux/module.h>
67 #include <linux/init.h>
68 #include <linux/time.h>
69 #include <linux/slab.h>
70 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
71 #include <linux/smp_lock.h>
73 #include <linux/seq_file.h>
74 #include <linux/pagemap.h>
75 #include <linux/mpage.h>
76 #include <linux/buffer_head.h>
77 #include <linux/exportfs.h>
78 #include <linux/mount.h>
79 #include <linux/vfs.h>
80 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
81 #include <linux/aio.h>
83 #include <linux/parser.h>
84 #include <linux/uio.h>
85 #include <linux/writeback.h>
86 #include <linux/log2.h>
87 #include <linux/hash.h>
88 #include <linux/backing-dev.h>
89 #include <linux/sched.h>
90 #include <linux/fs_struct.h>
91 #include <linux/namei.h>
92 #include <linux/vmalloc.h>
93 #include <asm/current.h>
94 #include <asm/unaligned.h>
96 #include "exfat_version.h"
97 #include "exfat_config.h"
98 #include "exfat_global.h"
99 #include "exfat_data.h"
100 #include "exfat_oal.h"
102 #include "exfat_blkdev.h"
103 #include "exfat_cache.h"
104 #include "exfat_part.h"
105 #include "exfat_nls.h"
106 #include "exfat_api.h"
109 #include "exfat_super.h"
111 static struct kmem_cache *exfat_inode_cachep;
113 static int exfat_default_codepage = DEFAULT_CODEPAGE;
114 static char exfat_default_iocharset[] = DEFAULT_IOCHARSET;
116 extern struct timezone sys_tz;
118 #define CHECK_ERR(x) BUG_ON(x)
119 #define ELAPSED_TIME 0
121 #if (ELAPSED_TIME == 1)
122 #include <linux/time.h>
124 static UINT32 __t1, __t2;
125 static UINT32 get_current_msec(void)
128 do_gettimeofday(&tm);
129 return((UINT32)(tm.tv_sec*1000000 + tm.tv_usec));
131 #define TIME_START() do {__t1 = get_current_msec();} while (0)
132 #define TIME_END() do {__t2 = get_current_msec();} while (0)
133 #define PRINT_TIME(n) do {printk("[EXFAT] Elapsed time %d = %d (usec)\n", n, (__t2 - __t1));} while (0)
137 #define PRINT_TIME(n)
140 #define UNIX_SECS_1980 315532800L
142 #if BITS_PER_LONG == 64
143 #define UNIX_SECS_2108 4354819200L
145 #define DAYS_DELTA_DECADE (365 * 10 + 2)
146 #define NO_LEAP_YEAR_2100 (120)
147 #define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100)
149 #define SECS_PER_MIN (60)
150 #define SECS_PER_HOUR (60 * SECS_PER_MIN)
151 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
153 #define MAKE_LEAP_YEAR(leap_year, year) \
155 if (unlikely(year > NO_LEAP_YEAR_2100)) \
156 leap_year = ((year + 3) / 4) - 1; \
158 leap_year = ((year + 3) / 4); \
161 static time_t accum_days_in_year[] = {
162 0, 0, 31, 59, 90,120,151,181,212,243,273,304,334, 0, 0, 0,
165 static void _exfat_truncate(struct inode *inode, loff_t old_size);
167 void exfat_time_fat2unix(struct exfat_sb_info *sbi, struct timespec *ts,
170 time_t year = tp->Year;
173 MAKE_LEAP_YEAR(ld, year);
175 if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
178 ts->tv_sec = tp->Second + tp->Minute * SECS_PER_MIN
179 + tp->Hour * SECS_PER_HOUR
180 + (year * 365 + ld + accum_days_in_year[(tp->Month)]
181 + (tp->Day - 1) + DAYS_DELTA_DECADE) * SECS_PER_DAY;
183 if(!sbi->options.tz_utc)
184 ts->tv_sec += sys_tz.tz_minuteswest * SECS_PER_MIN;
189 void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec *ts,
192 time_t second = ts->tv_sec;
193 time_t day, month, year;
196 if (!sbi->options.tz_utc)
197 second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
199 if (second < UNIX_SECS_1980) {
208 #if (BITS_PER_LONG == 64)
209 if (second >= UNIX_SECS_2108) {
219 day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
221 MAKE_LEAP_YEAR(ld, year);
222 if (year * 365 + ld > day)
225 MAKE_LEAP_YEAR(ld, year);
226 day -= year * 365 + ld;
228 if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
231 if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
233 for (month = 1; month < 12; month++) {
234 if (accum_days_in_year[month + 1] > day)
238 day -= accum_days_in_year[month];
240 tp->Second = second % SECS_PER_MIN;
241 tp->Minute = (second / SECS_PER_MIN) % 60;
242 tp->Hour = (second / SECS_PER_HOUR) % 24;
248 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
249 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
250 static int exfat_generic_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
252 static long exfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
254 static int exfat_sync_inode(struct inode *inode);
255 static struct inode *exfat_build_inode(struct super_block *sb, FILE_ID_T *fid, loff_t i_pos);
256 static void exfat_detach(struct inode *inode);
257 static void exfat_attach(struct inode *inode, loff_t i_pos);
258 static inline unsigned long exfat_hash(loff_t i_pos);
259 static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
260 static void exfat_write_super(struct super_block *sb);
262 static void __lock_super(struct super_block *sb)
264 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
267 struct exfat_sb_info *sbi = EXFAT_SB(sb);
268 mutex_lock(&sbi->s_lock);
272 static void __unlock_super(struct super_block *sb)
274 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
277 struct exfat_sb_info *sbi = EXFAT_SB(sb);
278 mutex_unlock(&sbi->s_lock);
282 static int __is_sb_dirty(struct super_block *sb)
284 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
287 struct exfat_sb_info *sbi = EXFAT_SB(sb);
292 static void __set_sb_clean(struct super_block *sb)
294 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
297 struct exfat_sb_info *sbi = EXFAT_SB(sb);
302 static void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
304 struct va_format vaf;
306 struct block_device *bdev = sb->s_bdev;
307 dev_t bd_dev = bdev ? bdev->bd_dev : 0;
312 printk("%s[EXFAT] (%s[%d:%d]): %pV\n", level,
313 sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
317 static void exfat_mnt_msg(struct super_block *sb, int mount, int prev_err, const char *msg)
319 exfat_msg(sb, KERN_INFO, "%s %s",
320 msg, prev_err ? "(with previous I/O errors)" : "");
321 ST_LOG("[EXFAT] (%s[%d:%d]):%s %s",sb->s_id, MAJOR(sb->s_dev),MINOR(sb->s_dev),
322 msg, prev_err ? "(with previous I/O errors)" : "");
326 static int __exfat_revalidate(struct dentry *dentry)
331 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
332 static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
334 static int exfat_revalidate(struct dentry *dentry, struct nameidata *nd)
337 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
338 if (flags & LOOKUP_RCU)
340 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
341 if (nd && nd->flags & LOOKUP_RCU)
347 return __exfat_revalidate(dentry);
350 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
351 static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
353 static int exfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
356 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
357 if (flags & LOOKUP_RCU)
359 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
362 if (nd && nd->flags & LOOKUP_RCU)
365 flags = nd ? nd->flags : 0;
367 flags = nd ? nd->flags : 0;
376 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
377 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
380 if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
381 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
386 return __exfat_revalidate(dentry);
389 static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
391 while (len && name[len - 1] == '.')
396 static unsigned int exfat_striptail_len(const struct qstr *qstr)
398 return __exfat_striptail_len(qstr->len, qstr->name);
401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
402 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
404 static int exfat_d_hash(const struct dentry *dentry, const struct inode *inode,
408 qstr->hash = full_name_hash(qstr->name, exfat_striptail_len(qstr));
412 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
413 static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
415 static int exfat_d_hashi(const struct dentry *dentry, const struct inode *inode,
419 struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
420 const unsigned char *name;
425 len = exfat_striptail_len(qstr);
427 hash = init_name_hash();
429 hash = partial_name_hash(nls_tolower(t, *name++), hash);
430 qstr->hash = end_name_hash(hash);
435 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
436 static int exfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
437 unsigned int len, const char *str, const struct qstr *name)
439 static int exfat_cmpi(const struct dentry *parent, const struct inode *pinode,
440 const struct dentry *dentry, const struct inode *inode,
441 unsigned int len, const char *str, const struct qstr *name)
444 struct nls_table *t = EXFAT_SB(parent->d_sb)->nls_io;
445 unsigned int alen, blen;
447 alen = exfat_striptail_len(name);
448 blen = __exfat_striptail_len(len, str);
450 if (nls_strnicmp(t, name->name, str, alen) == 0)
456 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
457 static int exfat_cmp(const struct dentry *parent, const struct dentry *dentry,
458 unsigned int len, const char *str, const struct qstr *name)
460 static int exfat_cmp(const struct dentry *parent, const struct inode *pinode,
461 const struct dentry *dentry, const struct inode *inode,
462 unsigned int len, const char *str, const struct qstr *name)
465 unsigned int alen, blen;
467 alen = exfat_striptail_len(name);
468 blen = __exfat_striptail_len(len, str);
470 if (strncmp(name->name, str, alen) == 0)
476 static const struct dentry_operations exfat_ci_dentry_ops = {
477 .d_revalidate = exfat_revalidate_ci,
478 .d_hash = exfat_d_hashi,
479 .d_compare = exfat_cmpi,
482 static const struct dentry_operations exfat_dentry_ops = {
483 .d_revalidate = exfat_revalidate,
484 .d_hash = exfat_d_hash,
485 .d_compare = exfat_cmp,
488 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
489 static int exfat_readdir(struct file *filp, struct dir_context *ctx)
491 struct inode *inode = filp->f_path.dentry->d_inode;
492 struct super_block *sb = inode->i_sb;
493 struct exfat_sb_info *sbi = EXFAT_SB(sb);
494 FS_INFO_T *p_fs = &(sbi->fs_info);
495 BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
504 if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
505 if (!dir_emit_dots(filp, ctx))
510 if (cpos & (DENTRY_SIZE - 1)) {
516 EXFAT_I(inode)->fid.size = i_size_read(inode);
517 EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
519 err = FsReadDir(inode, &de);
521 if (err == FFS_MEDIAERR) {
522 cpos += 1 << p_bd->sector_size_bits;
523 cpos &= ~((1 << p_bd->sector_size_bits)-1);
530 cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
535 if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
536 if (!dir_emit_dot(filp, ctx))
538 } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
539 if (!dir_emit_dotdot(filp, ctx))
542 loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
543 ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
545 struct inode *tmp = exfat_iget(sb, i_pos);
550 inum = iunique(sb, EXFAT_ROOT_INO);
552 if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
553 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
567 static int exfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
569 struct inode *inode = filp->f_path.dentry->d_inode;
570 struct super_block *sb = inode->i_sb;
571 struct exfat_sb_info *sbi = EXFAT_SB(sb);
572 FS_INFO_T *p_fs = &(sbi->fs_info);
573 BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
582 if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
584 if (inode->i_ino == EXFAT_ROOT_INO)
585 inum = EXFAT_ROOT_INO;
589 inum = parent_ino(filp->f_path.dentry);
591 if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
600 if (cpos & (DENTRY_SIZE - 1)) {
606 EXFAT_I(inode)->fid.size = i_size_read(inode);
607 EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
609 err = FsReadDir(inode, &de);
611 if (err == FFS_MEDIAERR) {
612 cpos += 1 << p_bd->sector_size_bits;
613 cpos &= ~((1 << p_bd->sector_size_bits)-1);
620 cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
625 if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
627 } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
628 inum = parent_ino(filp->f_path.dentry);
630 loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
631 ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
633 struct inode *tmp = exfat_iget(sb, i_pos);
638 inum = iunique(sb, EXFAT_ROOT_INO);
642 if (filldir(dirent, de.Name, strlen(de.Name), cpos, inum,
643 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
657 static int exfat_ioctl_volume_id(struct inode *dir)
659 struct super_block *sb = dir->i_sb;
660 struct exfat_sb_info *sbi = EXFAT_SB(sb);
661 FS_INFO_T *p_fs = &(sbi->fs_info);
666 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
667 static int exfat_generic_ioctl(struct inode *inode, struct file *filp,
668 unsigned int cmd, unsigned long arg)
670 static long exfat_generic_ioctl(struct file *filp,
671 unsigned int cmd, unsigned long arg)
674 #if EXFAT_CONFIG_KERNEL_DEBUG
675 #if !(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
676 struct inode *inode = filp->f_dentry->d_inode;
682 case EXFAT_IOCTL_GET_VOLUME_ID:
683 return exfat_ioctl_volume_id(inode);
684 #if EXFAT_CONFIG_KERNEL_DEBUG
685 case EXFAT_IOC_GET_DEBUGFLAGS: {
686 struct super_block *sb = inode->i_sb;
687 struct exfat_sb_info *sbi = EXFAT_SB(sb);
689 flags = sbi->debug_flags;
690 return put_user(flags, (int __user *)arg);
692 case EXFAT_IOC_SET_DEBUGFLAGS: {
693 struct super_block *sb = inode->i_sb;
694 struct exfat_sb_info *sbi = EXFAT_SB(sb);
696 if (!capable(CAP_SYS_ADMIN))
699 if (get_user(flags, (int __user *) arg))
703 sbi->debug_flags = flags;
714 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
715 static int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
717 static int exfat_file_fsync(struct file *filp, int datasync)
720 struct inode *inode = filp->f_mapping->host;
721 struct super_block *sb = inode->i_sb;
724 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
725 res = generic_file_fsync(filp, start, end, datasync);
727 res = generic_file_fsync(filp, datasync);
729 err = FsSyncVol(sb, 1);
731 return res ? res : err;
735 const struct file_operations exfat_dir_operations = {
736 .llseek = generic_file_llseek,
737 .read = generic_read_dir,
738 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
739 .iterate = exfat_readdir,
741 .readdir = exfat_readdir,
743 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
744 .ioctl = exfat_generic_ioctl,
746 .unlocked_ioctl = exfat_generic_ioctl,
748 .fsync = exfat_file_fsync,
751 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
752 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
754 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
755 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
756 struct nameidata *nd)
758 static int exfat_create(struct inode *dir, struct dentry *dentry, int mode,
759 struct nameidata *nd)
762 struct super_block *sb = dir->i_sb;
771 PRINTK("exfat_create entered\n");
773 ts = CURRENT_TIME_SEC;
775 err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_REGULAR, &fid);
777 if (err == FFS_INVALIDPATH)
779 else if (err == FFS_FILEEXIST)
781 else if (err == FFS_FULL)
783 else if (err == FFS_NAMETOOLONG)
790 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
792 (void) exfat_sync_inode(dir);
794 mark_inode_dirty(dir);
796 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
798 inode = exfat_build_inode(sb, &fid, i_pos);
800 err = PTR_ERR(inode);
804 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
806 dentry->d_time = dentry->d_parent->d_inode->i_version;
807 d_instantiate(dentry, inode);
811 PRINTK("exfat_create exited\n");
815 static int exfat_find(struct inode *dir, struct qstr *qname,
823 err = FsLookupFile(dir, (UINT8 *) qname->name, fid);
830 static int exfat_d_anon_disconn(struct dentry *dentry)
832 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
835 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
836 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
839 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
840 struct nameidata *nd)
843 struct super_block *sb = dir->i_sb;
845 struct dentry *alias;
853 PRINTK("exfat_lookup entered\n");
854 err = exfat_find(dir, &dentry->d_name, &fid);
856 if (err == -ENOENT) {
863 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
864 inode = exfat_build_inode(sb, &fid, i_pos);
866 err = PTR_ERR(inode);
870 i_mode = inode->i_mode;
871 if (S_ISLNK(i_mode)) {
872 EXFAT_I(inode)->target = MALLOC(i_size_read(inode)+1);
873 if (!EXFAT_I(inode)->target) {
877 FsReadFile(dir, &fid, EXFAT_I(inode)->target, i_size_read(inode), &ret);
878 *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
881 alias = d_find_alias(inode);
882 if (alias && !exfat_d_anon_disconn(alias)) {
883 CHECK_ERR(d_unhashed(alias));
884 if (!S_ISDIR(i_mode))
885 d_move(alias, dentry);
888 PRINTK("exfat_lookup exited 1\n");
895 dentry->d_time = dentry->d_parent->d_inode->i_version;
896 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
897 dentry->d_op = sb->s_root->d_op;
898 dentry = d_splice_alias(inode, dentry);
900 dentry->d_op = sb->s_root->d_op;
901 dentry->d_time = dentry->d_parent->d_inode->i_version;
904 dentry = d_splice_alias(inode, dentry);
906 dentry->d_time = dentry->d_parent->d_inode->i_version;
908 PRINTK("exfat_lookup exited 2\n");
913 PRINTK("exfat_lookup exited 3\n");
917 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
919 struct inode *inode = dentry->d_inode;
920 struct super_block *sb = dir->i_sb;
926 PRINTK("exfat_unlink entered\n");
928 ts = CURRENT_TIME_SEC;
930 EXFAT_I(inode)->fid.size = i_size_read(inode);
932 err = FsRemoveEntry(dir, &(EXFAT_I(inode)->fid));
934 if (err == FFS_PERMISSIONERR)
941 dir->i_mtime = dir->i_atime = ts;
943 (void) exfat_sync_inode(dir);
945 mark_inode_dirty(dir);
948 inode->i_mtime = inode->i_atime = ts;
953 PRINTK("exfat_unlink exited\n");
957 static int exfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
959 struct super_block *sb = dir->i_sb;
965 UINT64 len = (UINT64) strlen(target);
970 PRINTK("exfat_symlink entered\n");
972 ts = CURRENT_TIME_SEC;
974 err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_SYMLINK, &fid);
976 if (err == FFS_INVALIDPATH)
978 else if (err == FFS_FILEEXIST)
980 else if (err == FFS_FULL)
987 err = FsWriteFile(dir, &fid, (char *) target, len, &ret);
990 FsRemoveFile(dir, &fid);
1000 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
1001 if (IS_DIRSYNC(dir))
1002 (void) exfat_sync_inode(dir);
1004 mark_inode_dirty(dir);
1006 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
1008 inode = exfat_build_inode(sb, &fid, i_pos);
1009 if (IS_ERR(inode)) {
1010 err = PTR_ERR(inode);
1014 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
1016 EXFAT_I(inode)->target = MALLOC(len+1);
1017 if (!EXFAT_I(inode)->target) {
1021 MEMCPY(EXFAT_I(inode)->target, target, len+1);
1023 dentry->d_time = dentry->d_parent->d_inode->i_version;
1024 d_instantiate(dentry, inode);
1028 PRINTK("exfat_symlink exited\n");
1032 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1033 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1035 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1038 struct super_block *sb = dir->i_sb;
1039 struct inode *inode;
1047 PRINTK("exfat_mkdir entered\n");
1049 ts = CURRENT_TIME_SEC;
1051 err = FsCreateDir(dir, (UINT8 *) dentry->d_name.name, &fid);
1053 if (err == FFS_INVALIDPATH)
1055 else if (err == FFS_FILEEXIST)
1057 else if (err == FFS_FULL)
1059 else if (err == FFS_NAMETOOLONG)
1060 err = -ENAMETOOLONG;
1066 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
1067 if (IS_DIRSYNC(dir))
1068 (void) exfat_sync_inode(dir);
1070 mark_inode_dirty(dir);
1073 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
1075 inode = exfat_build_inode(sb, &fid, i_pos);
1076 if (IS_ERR(inode)) {
1077 err = PTR_ERR(inode);
1081 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
1083 dentry->d_time = dentry->d_parent->d_inode->i_version;
1084 d_instantiate(dentry, inode);
1088 PRINTK("exfat_mkdir exited\n");
1092 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
1094 struct inode *inode = dentry->d_inode;
1095 struct super_block *sb = dir->i_sb;
1101 PRINTK("exfat_rmdir entered\n");
1103 ts = CURRENT_TIME_SEC;
1105 EXFAT_I(inode)->fid.size = i_size_read(inode);
1107 err = FsRemoveDir(dir, &(EXFAT_I(inode)->fid));
1109 if (err == FFS_INVALIDPATH)
1111 else if (err == FFS_FILEEXIST)
1113 else if (err == FFS_NOTFOUND)
1115 else if (err == FFS_DIRBUSY)
1122 dir->i_mtime = dir->i_atime = ts;
1123 if (IS_DIRSYNC(dir))
1124 (void) exfat_sync_inode(dir);
1126 mark_inode_dirty(dir);
1130 inode->i_mtime = inode->i_atime = ts;
1131 exfat_detach(inode);
1132 remove_inode_hash(inode);
1136 PRINTK("exfat_rmdir exited\n");
1140 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
1141 struct inode *new_dir, struct dentry *new_dentry)
1143 struct inode *old_inode, *new_inode;
1144 struct super_block *sb = old_dir->i_sb;
1151 PRINTK("exfat_rename entered\n");
1153 old_inode = old_dentry->d_inode;
1154 new_inode = new_dentry->d_inode;
1156 ts = CURRENT_TIME_SEC;
1158 EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
1160 err = FsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir, new_dentry);
1162 if (err == FFS_PERMISSIONERR)
1164 else if (err == FFS_INVALIDPATH)
1166 else if (err == FFS_FILEEXIST)
1168 else if (err == FFS_NOTFOUND)
1170 else if (err == FFS_FULL)
1176 new_dir->i_version++;
1177 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = ts;
1178 if (IS_DIRSYNC(new_dir))
1179 (void) exfat_sync_inode(new_dir);
1181 mark_inode_dirty(new_dir);
1183 i_pos = ((loff_t) EXFAT_I(old_inode)->fid.dir.dir << 32) |
1184 (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
1186 exfat_detach(old_inode);
1187 exfat_attach(old_inode, i_pos);
1188 if (IS_DIRSYNC(new_dir))
1189 (void) exfat_sync_inode(old_inode);
1191 mark_inode_dirty(old_inode);
1193 if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
1194 drop_nlink(old_dir);
1195 if (!new_inode) inc_nlink(new_dir);
1198 old_dir->i_version++;
1199 old_dir->i_ctime = old_dir->i_mtime = ts;
1200 if (IS_DIRSYNC(old_dir))
1201 (void) exfat_sync_inode(old_dir);
1203 mark_inode_dirty(old_dir);
1206 exfat_detach(new_inode);
1207 drop_nlink(new_inode);
1208 if (S_ISDIR(new_inode->i_mode))
1209 drop_nlink(new_inode);
1210 new_inode->i_ctime = ts;
1215 PRINTK("exfat_rename exited\n");
1219 static int exfat_cont_expand(struct inode *inode, loff_t size)
1221 struct address_space *mapping = inode->i_mapping;
1222 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
1225 if ((err = generic_cont_expand_simple(inode, size)) != 0)
1228 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
1229 mark_inode_dirty(inode);
1231 if (IS_SYNC(inode)) {
1232 err = filemap_fdatawrite_range(mapping, start, start + count - 1);
1233 err2 = sync_mapping_buffers(mapping);
1234 err = (err)?(err):(err2);
1235 err2 = write_inode_now(inode, 1);
1236 err = (err)?(err):(err2);
1238 err = filemap_fdatawait_range(mapping, start, start + count - 1);
1244 static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
1246 mode_t allow_utime = sbi->options.allow_utime;
1248 if (current_fsuid() != inode->i_uid) {
1249 if (in_group_p(inode->i_gid))
1251 if (allow_utime & MAY_WRITE)
1258 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
1259 struct inode *inode, umode_t *mode_ptr)
1261 mode_t i_mode, mask, perm;
1263 i_mode = inode->i_mode;
1265 if (S_ISREG(i_mode) || S_ISLNK(i_mode))
1266 mask = sbi->options.fs_fmask;
1268 mask = sbi->options.fs_dmask;
1270 perm = *mode_ptr & ~(S_IFMT | mask);
1272 if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO|S_IXUGO)))
1275 if (exfat_mode_can_hold_ro(inode)) {
1276 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
1279 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
1283 *mode_ptr &= S_IFMT | perm;
1288 static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
1291 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
1292 struct inode *inode = dentry->d_inode;
1293 unsigned int ia_valid;
1297 PRINTK("exfat_setattr entered\n");
1299 if ((attr->ia_valid & ATTR_SIZE)
1300 && (attr->ia_size > i_size_read(inode))) {
1301 error = exfat_cont_expand(inode, attr->ia_size);
1302 if (error || attr->ia_valid == ATTR_SIZE)
1304 attr->ia_valid &= ~ATTR_SIZE;
1307 ia_valid = attr->ia_valid;
1309 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
1310 && exfat_allow_set_time(sbi, inode)) {
1311 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET);
1314 error = inode_change_ok(inode, attr);
1315 attr->ia_valid = ia_valid;
1320 if (((attr->ia_valid & ATTR_UID) &&
1321 (attr->ia_uid != sbi->options.fs_uid)) ||
1322 ((attr->ia_valid & ATTR_GID) &&
1323 (attr->ia_gid != sbi->options.fs_gid)) ||
1324 ((attr->ia_valid & ATTR_MODE) &&
1325 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | S_IRWXUGO)))) {
1329 if (attr->ia_valid & ATTR_MODE) {
1330 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
1331 attr->ia_valid &= ~ATTR_MODE;
1334 EXFAT_I(inode)->fid.size = i_size_read(inode);
1336 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1338 error = inode_setattr(inode, attr);
1340 if (attr->ia_valid & ATTR_SIZE) {
1341 old_size = i_size_read(inode);
1342 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1343 down_write(&EXFAT_I(inode)->truncate_lock);
1344 truncate_setsize(inode, attr->ia_size);
1345 _exfat_truncate(inode, old_size);
1346 up_write(&EXFAT_I(inode)->truncate_lock);
1348 truncate_setsize(inode, attr->ia_size);
1349 _exfat_truncate(inode, old_size);
1352 setattr_copy(inode, attr);
1353 mark_inode_dirty(inode);
1357 PRINTK("exfat_setattr exited\n");
1361 static int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1363 struct inode *inode = dentry->d_inode;
1365 PRINTK("exfat_getattr entered\n");
1367 generic_fillattr(inode, stat);
1368 stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
1370 PRINTK("exfat_getattr exited\n");
1374 const struct inode_operations exfat_dir_inode_operations = {
1375 .create = exfat_create,
1376 .lookup = exfat_lookup,
1377 .unlink = exfat_unlink,
1378 .symlink = exfat_symlink,
1379 .mkdir = exfat_mkdir,
1380 .rmdir = exfat_rmdir,
1381 .rename = exfat_rename,
1382 .setattr = exfat_setattr,
1383 .getattr = exfat_getattr,
1384 #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
1385 .setxattr = exfat_setxattr,
1386 .getxattr = exfat_getxattr,
1387 .listxattr = exfat_listxattr,
1388 .removexattr = exfat_removexattr,
1392 static void *exfat_follow_link(struct dentry *dentry, struct nameidata *nd)
1394 struct exfat_inode_info *ei = EXFAT_I(dentry->d_inode);
1395 nd_set_link(nd, (char *)(ei->target));
1399 const struct inode_operations exfat_symlink_inode_operations = {
1400 .readlink = generic_readlink,
1401 .follow_link = exfat_follow_link,
1402 #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
1403 .setxattr = exfat_setxattr,
1404 .getxattr = exfat_getxattr,
1405 .listxattr = exfat_listxattr,
1406 .removexattr = exfat_removexattr,
1410 static int exfat_file_release(struct inode *inode, struct file *filp)
1412 struct super_block *sb = inode->i_sb;
1414 EXFAT_I(inode)->fid.size = i_size_read(inode);
1419 const struct file_operations exfat_file_operations = {
1420 .llseek = generic_file_llseek,
1421 .read = do_sync_read,
1422 .write = do_sync_write,
1423 .aio_read = generic_file_aio_read,
1424 .aio_write = generic_file_aio_write,
1425 .mmap = generic_file_mmap,
1426 .release = exfat_file_release,
1427 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1428 .ioctl = exfat_generic_ioctl,
1429 .fsync = exfat_file_fsync,
1431 .unlocked_ioctl = exfat_generic_ioctl,
1432 .fsync = generic_file_fsync,
1434 .splice_read = generic_file_splice_read,
1437 static void _exfat_truncate(struct inode *inode, loff_t old_size)
1439 struct super_block *sb = inode->i_sb;
1440 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1441 FS_INFO_T *p_fs = &(sbi->fs_info);
1446 if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
1447 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1449 if (EXFAT_I(inode)->fid.start_clu == 0) goto out;
1451 err = FsTruncateFile(inode, old_size, i_size_read(inode));
1454 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
1455 if (IS_DIRSYNC(inode))
1456 (void) exfat_sync_inode(inode);
1458 mark_inode_dirty(inode);
1460 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
1461 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
1466 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
1467 static void exfat_truncate(struct inode *inode)
1469 _exfat_truncate(inode, i_size_read(inode));
1473 const struct inode_operations exfat_file_inode_operations = {
1474 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
1475 .truncate = exfat_truncate,
1477 .setattr = exfat_setattr,
1478 .getattr = exfat_getattr,
1479 #ifdef CONFIG_EXFAT_VIRTUAL_XATTR
1480 .setxattr = exfat_setxattr,
1481 .getxattr = exfat_getxattr,
1482 .listxattr = exfat_listxattr,
1483 .removexattr = exfat_removexattr,
1487 static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
1488 unsigned long *mapped_blocks, int *create)
1490 struct super_block *sb = inode->i_sb;
1491 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1492 FS_INFO_T *p_fs = &(sbi->fs_info);
1493 BD_INFO_T *p_bd = &(sbi->bd_info);
1494 const unsigned long blocksize = sb->s_blocksize;
1495 const unsigned char blocksize_bits = sb->s_blocksize_bits;
1496 sector_t last_block;
1497 int err, clu_offset, sec_offset;
1498 unsigned int cluster;
1503 if ((p_fs->vol_type == FAT12) || (p_fs->vol_type == FAT16)) {
1504 if (inode->i_ino == EXFAT_ROOT_INO) {
1505 if (sector < (p_fs->dentries_in_root >> (p_bd->sector_size_bits-DENTRY_SIZE_BITS))) {
1506 *phys = sector + p_fs->root_start_sector;
1513 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
1514 if (sector >= last_block) {
1515 if (*create == 0) return 0;
1520 clu_offset = sector >> p_fs->sectors_per_clu_bits;
1521 sec_offset = sector & (p_fs->sectors_per_clu - 1);
1523 EXFAT_I(inode)->fid.size = i_size_read(inode);
1525 err = FsMapCluster(inode, clu_offset, &cluster);
1528 if (err == FFS_FULL)
1532 } else if (cluster != CLUSTER_32(~0)) {
1533 *phys = START_SECTOR(cluster) + sec_offset;
1534 *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
1540 static int exfat_get_block(struct inode *inode, sector_t iblock,
1541 struct buffer_head *bh_result, int create)
1543 struct super_block *sb = inode->i_sb;
1544 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
1546 unsigned long mapped_blocks;
1551 err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
1558 max_blocks = min(mapped_blocks, max_blocks);
1560 EXFAT_I(inode)->mmu_private += max_blocks << sb->s_blocksize_bits;
1561 set_buffer_new(bh_result);
1563 map_bh(bh_result, sb, phys);
1566 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
1572 static int exfat_readpage(struct file *file, struct page *page)
1575 ret = mpage_readpage(page, exfat_get_block);
1579 static int exfat_readpages(struct file *file, struct address_space *mapping,
1580 struct list_head *pages, unsigned nr_pages)
1583 ret = mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
1587 static int exfat_writepage(struct page *page, struct writeback_control *wbc)
1590 ret = block_write_full_page(page, exfat_get_block, wbc);
1594 static int exfat_writepages(struct address_space *mapping,
1595 struct writeback_control *wbc)
1598 ret = mpage_writepages(mapping, wbc, exfat_get_block);
1602 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1603 static void exfat_write_failed(struct address_space *mapping, loff_t to)
1605 struct inode *inode = mapping->host;
1606 if (to > i_size_read(inode)) {
1608 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
1609 truncate_pagecache(inode, i_size_read(inode));
1611 truncate_pagecache(inode, to, i_size_read(inode));
1613 EXFAT_I(inode)->fid.size = i_size_read(inode);
1614 _exfat_truncate(inode, i_size_read(inode));
1620 static int exfat_write_begin(struct file *file, struct address_space *mapping,
1621 loff_t pos, unsigned len, unsigned flags,
1622 struct page **pagep, void **fsdata)
1626 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1628 &EXFAT_I(mapping->host)->mmu_private);
1630 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1632 exfat_write_failed(mapping, pos+len);
1637 static int exfat_write_end(struct file *file, struct address_space *mapping,
1638 loff_t pos, unsigned len, unsigned copied,
1639 struct page *pagep, void *fsdata)
1641 struct inode *inode = mapping->host;
1642 FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
1645 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
1647 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1649 exfat_write_failed(mapping, pos+len);
1652 if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
1653 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1654 fid->attr |= ATTR_ARCHIVE;
1655 mark_inode_dirty(inode);
1660 static ssize_t exfat_direct_IO(int rw, struct kiocb *iocb,
1661 const struct iovec *iov,
1662 loff_t offset, unsigned long nr_segs)
1664 struct inode *inode = iocb->ki_filp->f_mapping->host;
1665 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1666 struct address_space *mapping = iocb->ki_filp->f_mapping;
1671 if (EXFAT_I(inode)->mmu_private < (offset + iov_length(iov, nr_segs)))
1674 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1675 ret = blockdev_direct_IO(rw, iocb, inode, iov,
1676 offset, nr_segs, exfat_get_block);
1678 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1679 offset, nr_segs, exfat_get_block, NULL);
1682 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1683 if ((ret < 0) && (rw & WRITE))
1684 exfat_write_failed(mapping, offset+iov_length(iov, nr_segs));
1690 static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
1694 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1695 down_read(&EXFAT_I(mapping->host)->truncate_lock);
1696 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
1697 up_read(&EXFAT_I(mapping->host)->truncate_lock);
1699 down_read(&mapping->host->i_alloc_sem);
1700 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
1701 up_read(&mapping->host->i_alloc_sem);
1707 const struct address_space_operations exfat_aops = {
1708 .readpage = exfat_readpage,
1709 .readpages = exfat_readpages,
1710 .writepage = exfat_writepage,
1711 .writepages = exfat_writepages,
1712 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
1713 .sync_page = block_sync_page,
1715 .write_begin = exfat_write_begin,
1716 .write_end = exfat_write_end,
1717 .direct_IO = exfat_direct_IO,
1721 static inline unsigned long exfat_hash(loff_t i_pos)
1723 return hash_32(i_pos, EXFAT_HASH_BITS);
1726 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) {
1727 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1728 struct exfat_inode_info *info;
1729 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
1730 struct inode *inode = NULL;
1731 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
1732 struct hlist_node *node;
1734 spin_lock(&sbi->inode_hash_lock);
1735 hlist_for_each_entry(info, node, head, i_hash_fat) {
1737 spin_lock(&sbi->inode_hash_lock);
1738 hlist_for_each_entry(info, head, i_hash_fat) {
1740 CHECK_ERR(info->vfs_inode.i_sb != sb);
1742 if (i_pos != info->i_pos)
1744 inode = igrab(&info->vfs_inode);
1748 spin_unlock(&sbi->inode_hash_lock);
1752 static void exfat_attach(struct inode *inode, loff_t i_pos)
1754 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1755 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
1757 spin_lock(&sbi->inode_hash_lock);
1758 EXFAT_I(inode)->i_pos = i_pos;
1759 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
1760 spin_unlock(&sbi->inode_hash_lock);
1763 static void exfat_detach(struct inode *inode)
1765 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1767 spin_lock(&sbi->inode_hash_lock);
1768 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
1769 EXFAT_I(inode)->i_pos = 0;
1770 spin_unlock(&sbi->inode_hash_lock);
1773 static int exfat_fill_inode(struct inode *inode, FILE_ID_T *fid)
1775 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1776 FS_INFO_T *p_fs = &(sbi->fs_info);
1779 memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(FILE_ID_T));
1781 FsReadStat(inode, &info);
1783 EXFAT_I(inode)->i_pos = 0;
1784 EXFAT_I(inode)->target = NULL;
1785 inode->i_uid = sbi->options.fs_uid;
1786 inode->i_gid = sbi->options.fs_gid;
1788 inode->i_generation = get_seconds();
1790 if (info.Attr & ATTR_SUBDIR) {
1791 inode->i_generation &= ~1;
1792 inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
1793 inode->i_op = &exfat_dir_inode_operations;
1794 inode->i_fop = &exfat_dir_operations;
1796 i_size_write(inode, info.Size);
1797 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1798 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1799 set_nlink(inode,info.NumSubdirs);
1801 inode->i_nlink = info.NumSubdirs;
1803 } else if (info.Attr & ATTR_SYMLINK) {
1804 inode->i_generation |= 1;
1805 inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
1806 inode->i_op = &exfat_symlink_inode_operations;
1808 i_size_write(inode, info.Size);
1809 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1811 inode->i_generation |= 1;
1812 inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
1813 inode->i_op = &exfat_file_inode_operations;
1814 inode->i_fop = &exfat_file_operations;
1815 inode->i_mapping->a_ops = &exfat_aops;
1816 inode->i_mapping->nrpages = 0;
1818 i_size_write(inode, info.Size);
1819 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1821 exfat_save_attr(inode, info.Attr);
1823 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
1824 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
1826 exfat_time_fat2unix(sbi, &inode->i_mtime, &info.ModifyTimestamp);
1827 exfat_time_fat2unix(sbi, &inode->i_ctime, &info.CreateTimestamp);
1828 exfat_time_fat2unix(sbi, &inode->i_atime, &info.AccessTimestamp);
1833 static struct inode *exfat_build_inode(struct super_block *sb,
1834 FILE_ID_T *fid, loff_t i_pos) {
1835 struct inode *inode;
1838 inode = exfat_iget(sb, i_pos);
1841 inode = new_inode(sb);
1843 inode = ERR_PTR(-ENOMEM);
1846 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
1847 inode->i_version = 1;
1848 err = exfat_fill_inode(inode, fid);
1851 inode = ERR_PTR(err);
1854 exfat_attach(inode, i_pos);
1855 insert_inode_hash(inode);
1860 static int exfat_sync_inode(struct inode *inode)
1862 return exfat_write_inode(inode, NULL);
1865 static struct inode *exfat_alloc_inode(struct super_block *sb) {
1866 struct exfat_inode_info *ei;
1868 ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
1872 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1873 init_rwsem(&ei->truncate_lock);
1876 return &ei->vfs_inode;
1879 static void exfat_destroy_inode(struct inode *inode)
1881 FREE(EXFAT_I(inode)->target);
1882 EXFAT_I(inode)->target = NULL;
1884 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
1887 static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
1889 struct super_block *sb = inode->i_sb;
1890 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1893 if (inode->i_ino == EXFAT_ROOT_INO)
1896 info.Attr = exfat_make_attr(inode);
1897 info.Size = i_size_read(inode);
1899 exfat_time_unix2fat(sbi, &inode->i_mtime, &info.ModifyTimestamp);
1900 exfat_time_unix2fat(sbi, &inode->i_ctime, &info.CreateTimestamp);
1901 exfat_time_unix2fat(sbi, &inode->i_atime, &info.AccessTimestamp);
1903 FsWriteStat(inode, &info);
1908 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1909 static void exfat_delete_inode(struct inode *inode)
1911 truncate_inode_pages(&inode->i_data, 0);
1915 static void exfat_clear_inode(struct inode *inode)
1917 exfat_detach(inode);
1918 remove_inode_hash(inode);
1921 static void exfat_evict_inode(struct inode *inode)
1923 truncate_inode_pages(&inode->i_data, 0);
1925 if (!inode->i_nlink) {
1926 loff_t old_size = i_size_read(inode);
1927 i_size_write(inode, 0);
1928 EXFAT_I(inode)->fid.size = old_size;
1929 FsTruncateFile(inode, old_size, 0);
1932 invalidate_inode_buffers(inode);
1933 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
1934 end_writeback(inode);
1938 exfat_detach(inode);
1940 remove_inode_hash(inode);
1945 static void exfat_put_super(struct super_block *sb)
1947 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1950 exfat_mnt_msg(sb, 0, 0, "trying to unmount...");
1952 if (__is_sb_dirty(sb))
1953 exfat_write_super(sb);
1955 err = FsUmountVol(sb);
1957 if (sbi->nls_disk) {
1958 unload_nls(sbi->nls_disk);
1959 sbi->nls_disk = NULL;
1960 sbi->options.codepage = exfat_default_codepage;
1963 unload_nls(sbi->nls_io);
1966 if (sbi->options.iocharset != exfat_default_iocharset) {
1967 kfree(sbi->options.iocharset);
1968 sbi->options.iocharset = exfat_default_iocharset;
1971 sb->s_fs_info = NULL;
1972 if (!sbi->use_vmalloc)
1977 exfat_mnt_msg(sb, 0, err, "unmounted successfully!");
1980 static void exfat_write_super(struct super_block *sb)
1986 if (!(sb->s_flags & MS_RDONLY))
1992 static int exfat_sync_fs(struct super_block *sb, int wait)
1996 if (__is_sb_dirty(sb)) {
1999 err = FsSyncVol(sb, 1);
2006 static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
2008 struct super_block *sb = dentry->d_sb;
2009 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
2010 FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
2013 if (p_fs->used_clusters == (UINT32) ~0) {
2014 if (FFS_MEDIAERR == FsGetVolInfo(sb, &info))
2018 info.FatType = p_fs->vol_type;
2019 info.ClusterSize = p_fs->cluster_size;
2020 info.NumClusters = p_fs->num_clusters - 2;
2021 info.UsedClusters = p_fs->used_clusters;
2022 info.FreeClusters = info.NumClusters - info.UsedClusters;
2024 if (p_fs->dev_ejected)
2025 printk("[EXFAT] called statfs with previous I/O error.\n");
2028 buf->f_type = sb->s_magic;
2029 buf->f_bsize = info.ClusterSize;
2030 buf->f_blocks = info.NumClusters;
2031 buf->f_bfree = info.FreeClusters;
2032 buf->f_bavail = info.FreeClusters;
2033 buf->f_fsid.val[0] = (u32)id;
2034 buf->f_fsid.val[1] = (u32)(id >> 32);
2035 buf->f_namelen = 260;
2040 static int exfat_remount(struct super_block *sb, int *flags, char *data)
2042 *flags |= MS_NODIRATIME;
2046 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
2047 static int exfat_show_options(struct seq_file *m, struct dentry *root)
2049 struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
2051 static int exfat_show_options(struct seq_file *m, struct vfsmount *mnt)
2053 struct exfat_sb_info *sbi = EXFAT_SB(mnt->mnt_sb);
2055 struct exfat_mount_options *opts = &sbi->options;
2056 FS_INFO_T *p_fs = &(sbi->fs_info);
2058 if (opts->fs_uid != 0)
2059 seq_printf(m, ",uid=%u", opts->fs_uid);
2060 if (opts->fs_gid != 0)
2061 seq_printf(m, ",gid=%u", opts->fs_gid);
2062 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
2063 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
2064 if (opts->allow_utime)
2065 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
2067 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
2069 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
2070 seq_printf(m, ",namecase=%u", opts->casesensitive);
2072 seq_puts(m, ",tz=UTC");
2073 if (opts->errors == EXFAT_ERRORS_CONT)
2074 seq_puts(m, ",errors=continue");
2075 else if (opts->errors == EXFAT_ERRORS_PANIC)
2076 seq_puts(m, ",errors=panic");
2078 seq_puts(m, ",errors=remount-ro");
2079 #if EXFAT_CONFIG_DISCARD
2081 seq_printf(m, ",discard");
2083 if (p_fs->dev_ejected)
2084 seq_puts(m, ",ejected");
2088 const struct super_operations exfat_sops = {
2089 .alloc_inode = exfat_alloc_inode,
2090 .destroy_inode = exfat_destroy_inode,
2091 .write_inode = exfat_write_inode,
2092 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
2093 .delete_inode = exfat_delete_inode,
2094 .clear_inode = exfat_clear_inode,
2096 .evict_inode = exfat_evict_inode,
2098 .put_super = exfat_put_super,
2099 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
2100 .write_super = exfat_write_super,
2102 .sync_fs = exfat_sync_fs,
2103 .statfs = exfat_statfs,
2104 .remount_fs = exfat_remount,
2105 .show_options = exfat_show_options,
2124 #if EXFAT_CONFIG_DISCARD
2129 static const match_table_t exfat_tokens = {
2130 {Opt_uid, "uid=%u"},
2131 {Opt_gid, "gid=%u"},
2132 {Opt_umask, "umask=%o"},
2133 {Opt_dmask, "dmask=%o"},
2134 {Opt_fmask, "fmask=%o"},
2135 {Opt_allow_utime, "allow_utime=%o"},
2136 {Opt_codepage, "codepage=%u"},
2137 {Opt_charset, "iocharset=%s"},
2138 {Opt_namecase, "namecase=%u"},
2139 {Opt_debug, "debug"},
2140 {Opt_tz_utc, "tz=UTC"},
2141 {Opt_err_cont, "errors=continue"},
2142 {Opt_err_panic, "errors=panic"},
2143 {Opt_err_ro, "errors=remount-ro"},
2144 #if EXFAT_CONFIG_DISCARD
2145 {Opt_discard, "discard"},
2150 static int parse_options(char *options, int silent, int *debug,
2151 struct exfat_mount_options *opts)
2154 substring_t args[MAX_OPT_ARGS];
2158 opts->fs_uid = current_uid();
2159 opts->fs_gid = current_gid();
2160 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
2161 opts->allow_utime = (unsigned short) -1;
2162 opts->codepage = exfat_default_codepage;
2163 opts->iocharset = exfat_default_iocharset;
2164 opts->casesensitive = 0;
2166 opts->errors = EXFAT_ERRORS_RO;
2167 #if EXFAT_CONFIG_DISCARD
2175 while ((p = strsep(&options, ",")) != NULL) {
2180 token = match_token(p, exfat_tokens, args);
2183 if (match_int(&args[0], &option))
2185 opts->fs_uid = option;
2188 if (match_int(&args[0], &option))
2190 opts->fs_gid = option;
2195 if (match_octal(&args[0], &option))
2197 if (token != Opt_dmask)
2198 opts->fs_fmask = option;
2199 if (token != Opt_fmask)
2200 opts->fs_dmask = option;
2202 case Opt_allow_utime:
2203 if (match_octal(&args[0], &option))
2205 opts->allow_utime = option & (S_IWGRP | S_IWOTH);
2208 if (match_int(&args[0], &option))
2210 opts->codepage = option;
2213 if (opts->iocharset != exfat_default_iocharset)
2214 kfree(opts->iocharset);
2215 iocharset = match_strdup(&args[0]);
2218 opts->iocharset = iocharset;
2221 if (match_int(&args[0], &option))
2223 opts->casesensitive = option;
2229 opts->errors = EXFAT_ERRORS_CONT;
2232 opts->errors = EXFAT_ERRORS_PANIC;
2235 opts->errors = EXFAT_ERRORS_RO;
2240 #if EXFAT_CONFIG_DISCARD
2247 printk(KERN_ERR "[EXFAT] Unrecognized mount option %s or missing value\n", p);
2254 if (opts->allow_utime == (unsigned short) -1)
2255 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
2260 static void exfat_hash_init(struct super_block *sb)
2262 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2265 spin_lock_init(&sbi->inode_hash_lock);
2266 for (i = 0; i < EXFAT_HASH_SIZE; i++)
2267 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
2270 static int exfat_read_root(struct inode *inode)
2272 struct super_block *sb = inode->i_sb;
2273 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2275 FS_INFO_T *p_fs = &(sbi->fs_info);
2278 ts = CURRENT_TIME_SEC;
2280 EXFAT_I(inode)->fid.dir.dir = p_fs->root_dir;
2281 EXFAT_I(inode)->fid.dir.flags = 0x01;
2282 EXFAT_I(inode)->fid.entry = -1;
2283 EXFAT_I(inode)->fid.start_clu = p_fs->root_dir;
2284 EXFAT_I(inode)->fid.flags = 0x01;
2285 EXFAT_I(inode)->fid.type = TYPE_DIR;
2286 EXFAT_I(inode)->fid.rwoffset = 0;
2287 EXFAT_I(inode)->fid.hint_last_off = -1;
2289 EXFAT_I(inode)->target = NULL;
2291 FsReadStat(inode, &info);
2293 inode->i_uid = sbi->options.fs_uid;
2294 inode->i_gid = sbi->options.fs_gid;
2296 inode->i_generation = 0;
2297 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, S_IRWXUGO);
2298 inode->i_op = &exfat_dir_inode_operations;
2299 inode->i_fop = &exfat_dir_operations;
2301 i_size_write(inode, info.Size);
2302 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
2303 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
2304 EXFAT_I(inode)->i_pos = ((loff_t) p_fs->root_dir << 32) | 0xffffffff;
2305 EXFAT_I(inode)->mmu_private = i_size_read(inode);
2307 exfat_save_attr(inode, ATTR_SUBDIR);
2308 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
2309 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
2310 set_nlink(inode,info.NumSubdirs + 2);
2312 inode->i_nlink = info.NumSubdirs + 2;
2318 static void setup_dops(struct super_block *sb)
2320 if (EXFAT_SB(sb)->options.casesensitive == 0)
2321 sb->s_d_op = &exfat_ci_dentry_ops;
2323 sb->s_d_op = &exfat_dentry_ops;
2327 static int exfat_fill_super(struct super_block *sb, void *data, int silent)
2329 struct inode *root_inode = NULL;
2330 struct exfat_sb_info *sbi;
2335 exfat_mnt_msg(sb, 1, 0, "trying to mount...");
2337 sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
2339 sbi = vzalloc(sizeof(struct exfat_sb_info));
2341 exfat_mnt_msg(sb, 1, 0, "failed to mount! (ENOMEM)");
2344 sbi->use_vmalloc = 1;
2346 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
2347 mutex_init(&sbi->s_lock);
2349 sb->s_fs_info = sbi;
2351 sb->s_flags |= MS_NODIRATIME;
2352 sb->s_magic = EXFAT_SUPER_MAGIC;
2353 sb->s_op = &exfat_sops;
2355 error = parse_options(data, silent, &debug, &sbi->options);
2362 sb_min_blocksize(sb, 512);
2363 sb->s_maxbytes = 0x7fffffffffffffffLL;
2365 ret = FsMountVol(sb);
2368 printk(KERN_ERR "[EXFAT] FsMountVol failed\n");
2373 exfat_hash_init(sb);
2376 sprintf(buf, "cp%d", sbi->options.codepage);
2377 sbi->nls_disk = load_nls(buf);
2378 if (!sbi->nls_disk) {
2379 printk(KERN_ERR "[EXFAT] Codepage %s not found\n", buf);
2383 sbi->nls_io = load_nls(sbi->options.iocharset);
2385 printk(KERN_ERR "[EXFAT] IO charset %s not found\n",
2386 sbi->options.iocharset);
2391 root_inode = new_inode(sb);
2394 root_inode->i_ino = EXFAT_ROOT_INO;
2395 root_inode->i_version = 1;
2396 error = exfat_read_root(root_inode);
2400 exfat_attach(root_inode, EXFAT_I(root_inode)->i_pos);
2401 insert_inode_hash(root_inode);
2402 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
2403 sb->s_root = d_make_root(root_inode);
2405 sb->s_root = d_alloc_root(root_inode);
2408 printk(KERN_ERR "[EXFAT] Getting the root inode failed\n");
2412 exfat_mnt_msg(sb, 1, 0, "mounted successfully!");
2419 exfat_mnt_msg(sb, 1, 0, "failed to mount!");
2424 unload_nls(sbi->nls_io);
2426 unload_nls(sbi->nls_disk);
2427 if (sbi->options.iocharset != exfat_default_iocharset)
2428 kfree(sbi->options.iocharset);
2429 sb->s_fs_info = NULL;
2430 if (!sbi->use_vmalloc)
2436 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
2437 static int exfat_get_sb(struct file_system_type *fs_type,
2438 int flags, const char *dev_name,
2439 void *data, struct vfsmount *mnt)
2441 return get_sb_bdev(fs_type, flags, dev_name, data, exfat_fill_super, mnt);
2444 static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
2445 int flags, const char *dev_name,
2447 return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
2451 static void init_once(void *foo)
2453 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
2455 INIT_HLIST_NODE(&ei->i_hash_fat);
2456 inode_init_once(&ei->vfs_inode);
2459 static int __init exfat_init_inodecache(void)
2461 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
2462 sizeof(struct exfat_inode_info),
2463 0, (SLAB_RECLAIM_ACCOUNT|
2466 if (exfat_inode_cachep == NULL)
2471 static void __exit exfat_destroy_inodecache(void)
2473 kmem_cache_destroy(exfat_inode_cachep);
2476 #if EXFAT_CONFIG_KERNEL_DEBUG
2477 static void exfat_debug_kill_sb(struct super_block *sb)
2479 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2480 struct block_device *bdev = sb->s_bdev;
2485 flags = sbi->debug_flags;
2487 if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
2489 invalidate_bdev(bdev);
2493 kill_block_super(sb);
2497 static struct file_system_type exfat_fs_type = {
2498 .owner = THIS_MODULE,
2500 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
2501 .get_sb = exfat_get_sb,
2503 .mount = exfat_fs_mount,
2505 #if EXFAT_CONFIG_KERNEL_DEBUG
2506 .kill_sb = exfat_debug_kill_sb,
2508 .kill_sb = kill_block_super,
2510 .fs_flags = FS_REQUIRES_DEV,
2513 static int __init init_exfat_fs(void)
2517 printk(KERN_INFO "exFAT: FS Version %s\n", EXFAT_VERSION);
2519 err = exfat_init_inodecache();
2520 if (err) return err;
2522 return register_filesystem(&exfat_fs_type);
2525 static void __exit exit_exfat_fs(void)
2527 exfat_destroy_inodecache();
2528 unregister_filesystem(&exfat_fs_type);
2531 module_init(init_exfat_fs);
2532 module_exit(exit_exfat_fs);
2534 MODULE_LICENSE("GPL");