tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / fs / exfat / exfat_super.c
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".  */
2 /*
3  *  linux/fs/fat/file.c
4  *
5  *  Written 1992,1993 by Werner Almesberger
6  *
7  *  regular file handling primitives for fat-based filesystems
8  */
9
10 /*
11  *  linux/fs/fat/inode.c
12  *
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
16  *
17  *  Fixes:
18  *
19  *    Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
20  */
21
22 /*
23  *  linux/fs/fat/misc.c
24  *
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)
28  */
29
30 /*
31  *  linux/fs/vfat/namei.c
32  *
33  *  Written 1992,1993 by Werner Almesberger
34  *
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.
40  *
41  *  Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
42  *
43  *  Support Multibyte characters and cleanup by
44  *                              OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
45  */
46
47 /*
48  *  Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
49  *
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.
54  *
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.
59  *
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.
63  */
64
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>
72 #endif
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>
82 #endif
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>
95
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"
101
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"
107 #include "exfat.h"
108
109 #include "exfat_super.h"
110
111 static struct kmem_cache *exfat_inode_cachep;
112
113 static int exfat_default_codepage = DEFAULT_CODEPAGE;
114 static char exfat_default_iocharset[] = DEFAULT_IOCHARSET;
115
116 extern struct timezone sys_tz;
117
118 #define CHECK_ERR(x)    BUG_ON(x)
119 #define ELAPSED_TIME        0
120
121 #if (ELAPSED_TIME == 1)
122 #include <linux/time.h>
123
124 static UINT32 __t1, __t2;
125 static UINT32 get_current_msec(void)
126 {
127         struct timeval tm;
128         do_gettimeofday(&tm);
129         return((UINT32)(tm.tv_sec*1000000 + tm.tv_usec));
130 }
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)
134 #else
135 #define TIME_START()
136 #define TIME_END()
137 #define PRINT_TIME(n)
138 #endif
139
140 #define UNIX_SECS_1980    315532800L
141
142 #if BITS_PER_LONG == 64
143 #define UNIX_SECS_2108    4354819200L
144 #endif
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)
148
149 #define SECS_PER_MIN    (60)
150 #define SECS_PER_HOUR   (60 * SECS_PER_MIN)
151 #define SECS_PER_DAY    (24 * SECS_PER_HOUR)
152
153 #define MAKE_LEAP_YEAR(leap_year, year)                         \
154         do {                                                    \
155                 if (unlikely(year > NO_LEAP_YEAR_2100))         \
156                         leap_year = ((year + 3) / 4) - 1;       \
157                 else                                            \
158                         leap_year = ((year + 3) / 4);           \
159         } while(0)
160
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,
163 };
164
165 static void _exfat_truncate(struct inode *inode, loff_t old_size);
166
167 void exfat_time_fat2unix(struct exfat_sb_info *sbi, struct timespec *ts,
168                                                  DATE_TIME_T *tp)
169 {
170         time_t year = tp->Year;
171         time_t ld;
172
173         MAKE_LEAP_YEAR(ld, year);
174
175         if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
176                 ld++;
177
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;
182
183         if(!sbi->options.tz_utc)
184                 ts->tv_sec += sys_tz.tz_minuteswest * SECS_PER_MIN;
185
186         ts->tv_nsec = 0;
187 }
188
189 void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec *ts,
190                                                  DATE_TIME_T *tp)
191 {
192         time_t second = ts->tv_sec;
193         time_t day, month, year;
194         time_t ld;
195
196         if (!sbi->options.tz_utc)
197                 second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
198
199         if (second < UNIX_SECS_1980) {
200                 tp->Second  = 0;
201                 tp->Minute  = 0;
202                 tp->Hour = 0;
203                 tp->Day  = 1;
204                 tp->Month  = 1;
205                 tp->Year = 0;
206                 return;
207         }
208 #if (BITS_PER_LONG == 64)
209         if (second >= UNIX_SECS_2108) {
210                 tp->Second  = 59;
211                 tp->Minute  = 59;
212                 tp->Hour = 23;
213                 tp->Day  = 31;
214                 tp->Month  = 12;
215                 tp->Year = 127;
216                 return;
217         }
218 #endif
219         day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
220         year = day / 365;
221         MAKE_LEAP_YEAR(ld, year);
222         if (year * 365 + ld > day)
223                 year--;
224
225         MAKE_LEAP_YEAR(ld, year);
226         day -= year * 365 + ld;
227
228         if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
229                 month = 2;
230         } else {
231                 if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
232                         day--;
233                 for (month = 1; month < 12; month++) {
234                         if (accum_days_in_year[month + 1] > day)
235                                 break;
236                 }
237         }
238         day -= accum_days_in_year[month];
239
240         tp->Second  = second % SECS_PER_MIN;
241         tp->Minute  = (second / SECS_PER_MIN) % 60;
242         tp->Hour = (second / SECS_PER_HOUR) % 24;
243         tp->Day  = day + 1;
244         tp->Month  = month;
245         tp->Year = year;
246 }
247
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);
251 #else
252 static long exfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
253 #endif
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);
261
262 static void __lock_super(struct super_block *sb)
263 {
264 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
265         lock_super(sb);
266 #else
267         struct exfat_sb_info *sbi = EXFAT_SB(sb);
268         mutex_lock(&sbi->s_lock);
269 #endif
270 }
271
272 static void __unlock_super(struct super_block *sb)
273 {
274 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
275         unlock_super(sb);
276 #else
277         struct exfat_sb_info *sbi = EXFAT_SB(sb);
278         mutex_unlock(&sbi->s_lock);
279 #endif
280 }
281
282 static int __is_sb_dirty(struct super_block *sb)
283 {
284 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
285         return sb->s_dirt;
286 #else
287         struct exfat_sb_info *sbi = EXFAT_SB(sb);
288         return sbi->s_dirt;
289 #endif
290 }
291
292 static void __set_sb_clean(struct super_block *sb)
293 {
294 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
295         sb->s_dirt = 0;
296 #else
297         struct exfat_sb_info *sbi = EXFAT_SB(sb);
298         sbi->s_dirt = 0;
299 #endif
300 }
301
302 static void exfat_msg(struct super_block *sb, const char *level, const char *fmt, ...)
303 {
304         struct va_format vaf;
305         va_list args;
306         struct block_device *bdev = sb->s_bdev;
307         dev_t bd_dev = bdev ? bdev->bd_dev : 0;
308
309         va_start(args, fmt);
310         vaf.fmt = fmt;
311         vaf.va = &args;
312         printk("%s[EXFAT] (%s[%d:%d]): %pV\n", level,
313                         sb->s_id, MAJOR(bd_dev), MINOR(bd_dev), &vaf);
314         va_end(args);
315 }
316
317 static void exfat_mnt_msg(struct super_block *sb, int mount, int prev_err, const char *msg)
318 {
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)" : "");
323 }
324
325
326 static int __exfat_revalidate(struct dentry *dentry)
327 {
328         return 0;
329 }
330
331 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
332 static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
333 #else
334 static int exfat_revalidate(struct dentry *dentry, struct nameidata *nd)
335 #endif
336 {
337 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
338         if (flags & LOOKUP_RCU)
339                 return -ECHILD;
340 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
341         if (nd && nd->flags & LOOKUP_RCU)
342                 return -ECHILD;
343 #endif
344
345         if (dentry->d_inode)
346                 return 1;
347         return __exfat_revalidate(dentry);
348 }
349
350 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
351 static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
352 #else
353 static int exfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
354 #endif
355 {
356 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
357         if (flags & LOOKUP_RCU)
358                 return -ECHILD;
359 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
360         unsigned int flags;
361
362         if (nd && nd->flags & LOOKUP_RCU)
363                 return -ECHILD;
364
365         flags = nd ? nd->flags : 0;
366 #else
367         flags = nd ? nd->flags : 0;
368 #endif
369
370         if (dentry->d_inode)
371                 return 1;
372
373         if (!flags)
374                 return 0;
375
376 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
377         if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
378                 return 0;
379 #else
380         if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
381                 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
382                         return 0;
383         }
384 #endif
385
386         return __exfat_revalidate(dentry);
387 }
388
389 static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
390 {
391         while (len && name[len - 1] == '.')
392                 len--;
393         return len;
394 }
395
396 static unsigned int exfat_striptail_len(const struct qstr *qstr)
397 {
398         return __exfat_striptail_len(qstr->len, qstr->name);
399 }
400
401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
402 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
403 #else
404 static int exfat_d_hash(const struct dentry *dentry, const struct inode *inode,
405                 struct qstr *qstr)
406 #endif
407 {
408         qstr->hash = full_name_hash(qstr->name, exfat_striptail_len(qstr));
409         return 0;
410 }
411
412 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
413 static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
414 #else
415 static int exfat_d_hashi(const struct dentry *dentry, const struct inode *inode,
416                 struct qstr *qstr)
417 #endif
418 {
419         struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
420         const unsigned char *name;
421         unsigned int len;
422         unsigned long hash;
423
424         name = qstr->name;
425         len = exfat_striptail_len(qstr);
426
427         hash = init_name_hash();
428         while (len--)
429                 hash = partial_name_hash(nls_tolower(t, *name++), hash);
430         qstr->hash = end_name_hash(hash);
431
432         return 0;
433 }
434
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)
438 #else
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)
442 #endif
443 {
444         struct nls_table *t = EXFAT_SB(parent->d_sb)->nls_io;
445         unsigned int alen, blen;
446
447         alen = exfat_striptail_len(name);
448         blen = __exfat_striptail_len(len, str);
449         if (alen == blen) {
450                 if (nls_strnicmp(t, name->name, str, alen) == 0)
451                         return 0;
452         }
453         return 1;
454 }
455
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)
459 #else
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)
463 #endif
464 {
465         unsigned int alen, blen;
466
467         alen = exfat_striptail_len(name);
468         blen = __exfat_striptail_len(len, str);
469         if (alen == blen) {
470                 if (strncmp(name->name, str, alen) == 0)
471                         return 0;
472         }
473         return 1;
474 }
475
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,
480 };
481
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,
486 };
487
488 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
489 static int exfat_readdir(struct file *filp, struct dir_context *ctx)
490 {
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);
496         DIR_ENTRY_T de;
497         unsigned long inum;
498         loff_t cpos;
499         int err = 0;
500
501         __lock_super(sb);
502
503         cpos = ctx->pos;
504         if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
505                 if (!dir_emit_dots(filp, ctx))
506                         goto out;
507                 if (ctx->pos == 2)
508                         cpos = 0;
509         }
510         if (cpos & (DENTRY_SIZE - 1)) {
511                 err = -ENOENT;
512                 goto out;
513         }
514
515 get_new:
516         EXFAT_I(inode)->fid.size = i_size_read(inode);
517         EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
518
519         err = FsReadDir(inode, &de);
520         if (err) {
521                 if (err == FFS_MEDIAERR) {
522                         cpos += 1 << p_bd->sector_size_bits;
523                         cpos &= ~((1 << p_bd->sector_size_bits)-1);
524                 }
525
526                 err = -EIO;
527                 goto end_of_dir;
528         }
529
530         cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
531
532         if (!de.Name[0])
533                 goto end_of_dir;
534
535         if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
536                 if (!dir_emit_dot(filp, ctx))
537                         goto out;
538         } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
539                 if (!dir_emit_dotdot(filp, ctx))
540                         goto out;
541         } else {
542                 loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
543                                            ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
544
545                 struct inode *tmp = exfat_iget(sb, i_pos);
546                 if (tmp) {
547                         inum = tmp->i_ino;
548                         iput(tmp);
549                 } else {
550                         inum = iunique(sb, EXFAT_ROOT_INO);
551                 }
552                 if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
553                                 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
554                         goto out;
555         }
556
557         ctx->pos = cpos;
558         goto get_new;
559
560 end_of_dir:
561         ctx->pos = cpos;
562 out:
563         __unlock_super(sb);
564         return err;
565 }
566 #else
567 static int exfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
568 {
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);
574         DIR_ENTRY_T de;
575         unsigned long inum;
576         loff_t cpos;
577         int err = 0;
578
579         __lock_super(sb);
580
581         cpos = filp->f_pos;
582         if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
583                 while (cpos < 2) {
584                         if (inode->i_ino == EXFAT_ROOT_INO)
585                                 inum = EXFAT_ROOT_INO;
586                         else if (cpos == 0)
587                                 inum = inode->i_ino;
588                         else
589                                 inum = parent_ino(filp->f_path.dentry);
590
591                         if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
592                                 goto out;
593                         cpos++;
594                         filp->f_pos++;
595                 }
596                 if (cpos == 2) {
597                         cpos = 0;
598                 }
599         }
600         if (cpos & (DENTRY_SIZE - 1)) {
601                 err = -ENOENT;
602                 goto out;
603         }
604
605 get_new:
606         EXFAT_I(inode)->fid.size = i_size_read(inode);
607         EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
608
609         err = FsReadDir(inode, &de);
610         if (err) {
611                 if (err == FFS_MEDIAERR) {
612                         cpos += 1 << p_bd->sector_size_bits;
613                         cpos &= ~((1 << p_bd->sector_size_bits)-1);
614                 }
615
616                 err = -EIO;
617                 goto end_of_dir;
618         }
619
620         cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
621
622         if (!de.Name[0])
623                 goto end_of_dir;
624
625         if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
626                 inum = inode->i_ino;
627         } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
628                 inum = parent_ino(filp->f_path.dentry);
629         } else {
630                 loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
631                                            ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
632
633                 struct inode *tmp = exfat_iget(sb, i_pos);
634                 if (tmp) {
635                         inum = tmp->i_ino;
636                         iput(tmp);
637                 } else {
638                         inum = iunique(sb, EXFAT_ROOT_INO);
639                 }
640         }
641
642         if (filldir(dirent, de.Name, strlen(de.Name), cpos, inum,
643                                 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
644                 goto out;
645
646         filp->f_pos = cpos;
647         goto get_new;
648
649 end_of_dir:
650         filp->f_pos = cpos;
651 out:
652         __unlock_super(sb);
653         return err;
654 }
655 #endif
656
657 static int exfat_ioctl_volume_id(struct inode *dir)
658 {
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);
662
663         return p_fs->vol_id;
664 }
665
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)
669 #else
670 static long exfat_generic_ioctl(struct file *filp,
671                                                                 unsigned int cmd, unsigned long arg)
672 #endif
673 {
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;
677 #endif
678         unsigned int flags;
679 #endif
680
681         switch (cmd) {
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);
688
689                 flags = sbi->debug_flags;
690                 return put_user(flags, (int __user *)arg);
691         }
692         case EXFAT_IOC_SET_DEBUGFLAGS: {
693                 struct super_block *sb = inode->i_sb;
694                 struct exfat_sb_info *sbi = EXFAT_SB(sb);
695
696                 if (!capable(CAP_SYS_ADMIN))
697                         return -EPERM;
698
699                 if (get_user(flags, (int __user *) arg))
700                         return -EFAULT;
701
702                 __lock_super(sb);
703                 sbi->debug_flags = flags;
704                 __unlock_super(sb);
705
706                 return 0;
707         }
708 #endif
709         default:
710                 return -ENOTTY;
711         }
712 }
713
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)
716 #else
717 static int exfat_file_fsync(struct file *filp, int datasync)
718 #endif
719 {
720         struct inode *inode = filp->f_mapping->host;
721         struct super_block *sb = inode->i_sb;
722         int res, err;
723
724 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,1,0)
725         res = generic_file_fsync(filp, start, end, datasync);
726 #else
727         res = generic_file_fsync(filp, datasync);
728 #endif
729         err = FsSyncVol(sb, 1);
730
731         return res ? res : err;
732 }
733
734
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,
740 #else
741         .readdir    = exfat_readdir,
742 #endif
743 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
744         .ioctl      = exfat_generic_ioctl,
745 #else
746         .unlocked_ioctl = exfat_generic_ioctl,
747 #endif
748         .fsync      = exfat_file_fsync,
749 };
750
751 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
752 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
753                                                 bool excl)
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)
757 #else
758 static int exfat_create(struct inode *dir, struct dentry *dentry, int mode,
759                                                 struct nameidata *nd)
760 #endif
761 {
762         struct super_block *sb = dir->i_sb;
763         struct inode *inode;
764         struct timespec ts;
765         FILE_ID_T fid;
766         loff_t i_pos;
767         int err;
768
769         __lock_super(sb);
770
771         PRINTK("exfat_create entered\n");
772
773         ts = CURRENT_TIME_SEC;
774
775         err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_REGULAR, &fid);
776         if (err) {
777                 if (err == FFS_INVALIDPATH)
778                         err = -EINVAL;
779                 else if (err == FFS_FILEEXIST)
780                         err = -EEXIST;
781                 else if (err == FFS_FULL)
782                         err = -ENOSPC;
783                 else if (err == FFS_NAMETOOLONG)
784                         err = -ENAMETOOLONG;
785                 else
786                         err = -EIO;
787                 goto out;
788         }
789         dir->i_version++;
790         dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
791         if (IS_DIRSYNC(dir))
792                 (void) exfat_sync_inode(dir);
793         else
794                 mark_inode_dirty(dir);
795
796         i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
797
798         inode = exfat_build_inode(sb, &fid, i_pos);
799         if (IS_ERR(inode)) {
800                 err = PTR_ERR(inode);
801                 goto out;
802         }
803         inode->i_version++;
804         inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
805
806         dentry->d_time = dentry->d_parent->d_inode->i_version;
807         d_instantiate(dentry, inode);
808
809 out:
810         __unlock_super(sb);
811         PRINTK("exfat_create exited\n");
812         return err;
813 }
814
815 static int exfat_find(struct inode *dir, struct qstr *qname,
816                                           FILE_ID_T *fid)
817 {
818         int err;
819
820         if (qname->len == 0)
821                 return -ENOENT;
822
823         err = FsLookupFile(dir, (UINT8 *) qname->name, fid);
824         if (err)
825                 return -ENOENT;
826
827         return 0;
828 }
829
830 static int exfat_d_anon_disconn(struct dentry *dentry)
831 {
832         return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
833 }
834
835 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
836 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
837                                                    unsigned int flags)
838 #else
839 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
840                                                    struct nameidata *nd)
841 #endif
842 {
843         struct super_block *sb = dir->i_sb;
844         struct inode *inode;
845         struct dentry *alias;
846         int err;
847         FILE_ID_T fid;
848         loff_t i_pos;
849         UINT64 ret;
850         mode_t i_mode;
851
852         __lock_super(sb);
853         PRINTK("exfat_lookup entered\n");
854         err = exfat_find(dir, &dentry->d_name, &fid);
855         if (err) {
856                 if (err == -ENOENT) {
857                         inode = NULL;
858                         goto out;
859                 }
860                 goto error;
861         }
862
863         i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
864         inode = exfat_build_inode(sb, &fid, i_pos);
865         if (IS_ERR(inode)) {
866                 err = PTR_ERR(inode);
867                 goto error;
868         }
869
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) {
874                         err = -ENOMEM;
875                         goto error;
876                 }
877                 FsReadFile(dir, &fid, EXFAT_I(inode)->target, i_size_read(inode), &ret);
878                 *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
879         }
880
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);
886                 iput(inode);
887                 __unlock_super(sb);
888                 PRINTK("exfat_lookup exited 1\n");
889                 return alias;
890         } else {
891                 dput(alias);
892         }
893 out:
894         __unlock_super(sb);
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);
899         if (dentry) {
900                 dentry->d_op = sb->s_root->d_op;
901                 dentry->d_time = dentry->d_parent->d_inode->i_version;
902         }
903 #else
904         dentry = d_splice_alias(inode, dentry);
905         if (dentry)
906                 dentry->d_time = dentry->d_parent->d_inode->i_version;
907 #endif
908         PRINTK("exfat_lookup exited 2\n");
909         return dentry;
910
911 error:
912         __unlock_super(sb);
913         PRINTK("exfat_lookup exited 3\n");
914         return ERR_PTR(err);
915 }
916
917 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
918 {
919         struct inode *inode = dentry->d_inode;
920         struct super_block *sb = dir->i_sb;
921         struct timespec ts;
922         int err;
923
924         __lock_super(sb);
925
926         PRINTK("exfat_unlink entered\n");
927
928         ts = CURRENT_TIME_SEC;
929
930         EXFAT_I(inode)->fid.size = i_size_read(inode);
931
932         err = FsRemoveEntry(dir, &(EXFAT_I(inode)->fid));
933         if (err) {
934                 if (err == FFS_PERMISSIONERR)
935                         err = -EPERM;
936                 else
937                         err = -EIO;
938                 goto out;
939         }
940         dir->i_version++;
941         dir->i_mtime = dir->i_atime = ts;
942         if (IS_DIRSYNC(dir))
943                 (void) exfat_sync_inode(dir);
944         else
945                 mark_inode_dirty(dir);
946
947         clear_nlink(inode);
948         inode->i_mtime = inode->i_atime = ts;
949         exfat_detach(inode);
950
951 out:
952         __unlock_super(sb);
953         PRINTK("exfat_unlink exited\n");
954         return err;
955 }
956
957 static int exfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
958 {
959         struct super_block *sb = dir->i_sb;
960         struct inode *inode;
961         struct timespec ts;
962         FILE_ID_T fid;
963         loff_t i_pos;
964         int err;
965         UINT64 len = (UINT64) strlen(target);
966         UINT64 ret;
967
968         __lock_super(sb);
969
970         PRINTK("exfat_symlink entered\n");
971
972         ts = CURRENT_TIME_SEC;
973
974         err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_SYMLINK, &fid);
975         if (err) {
976                 if (err == FFS_INVALIDPATH)
977                         err = -EINVAL;
978                 else if (err == FFS_FILEEXIST)
979                         err = -EEXIST;
980                 else if (err == FFS_FULL)
981                         err = -ENOSPC;
982                 else
983                         err = -EIO;
984                 goto out;
985         }
986
987         err = FsWriteFile(dir, &fid, (char *) target, len, &ret);
988
989         if (err) {
990                 FsRemoveFile(dir, &fid);
991
992                 if (err == FFS_FULL)
993                         err = -ENOSPC;
994                 else
995                         err = -EIO;
996                 goto out;
997         }
998
999         dir->i_version++;
1000         dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
1001         if (IS_DIRSYNC(dir))
1002                 (void) exfat_sync_inode(dir);
1003         else
1004                 mark_inode_dirty(dir);
1005
1006         i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
1007
1008         inode = exfat_build_inode(sb, &fid, i_pos);
1009         if (IS_ERR(inode)) {
1010                 err = PTR_ERR(inode);
1011                 goto out;
1012         }
1013         inode->i_version++;
1014         inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
1015
1016         EXFAT_I(inode)->target = MALLOC(len+1);
1017         if (!EXFAT_I(inode)->target) {
1018                 err = -ENOMEM;
1019                 goto out;
1020         }
1021         MEMCPY(EXFAT_I(inode)->target, target, len+1);
1022
1023         dentry->d_time = dentry->d_parent->d_inode->i_version;
1024         d_instantiate(dentry, inode);
1025
1026 out:
1027         __unlock_super(sb);
1028         PRINTK("exfat_symlink exited\n");
1029         return err;
1030 }
1031
1032 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1033 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1034 #else
1035 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1036 #endif
1037 {
1038         struct super_block *sb = dir->i_sb;
1039         struct inode *inode;
1040         struct timespec ts;
1041         FILE_ID_T fid;
1042         loff_t i_pos;
1043         int err;
1044
1045         __lock_super(sb);
1046
1047         PRINTK("exfat_mkdir entered\n");
1048
1049         ts = CURRENT_TIME_SEC;
1050
1051         err = FsCreateDir(dir, (UINT8 *) dentry->d_name.name, &fid);
1052         if (err) {
1053                 if (err == FFS_INVALIDPATH)
1054                         err = -EINVAL;
1055                 else if (err == FFS_FILEEXIST)
1056                         err = -EEXIST;
1057                 else if (err == FFS_FULL)
1058                         err = -ENOSPC;
1059                 else if (err == FFS_NAMETOOLONG)
1060                         err = -ENAMETOOLONG;
1061                 else
1062                         err = -EIO;
1063                 goto out;
1064         }
1065         dir->i_version++;
1066         dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
1067         if (IS_DIRSYNC(dir))
1068                 (void) exfat_sync_inode(dir);
1069         else
1070                 mark_inode_dirty(dir);
1071         inc_nlink(dir);
1072
1073         i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
1074
1075         inode = exfat_build_inode(sb, &fid, i_pos);
1076         if (IS_ERR(inode)) {
1077                 err = PTR_ERR(inode);
1078                 goto out;
1079         }
1080         inode->i_version++;
1081         inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
1082
1083         dentry->d_time = dentry->d_parent->d_inode->i_version;
1084         d_instantiate(dentry, inode);
1085
1086 out:
1087         __unlock_super(sb);
1088         PRINTK("exfat_mkdir exited\n");
1089         return err;
1090 }
1091
1092 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
1093 {
1094         struct inode *inode = dentry->d_inode;
1095         struct super_block *sb = dir->i_sb;
1096         struct timespec ts;
1097         int err;
1098
1099         __lock_super(sb);
1100
1101         PRINTK("exfat_rmdir entered\n");
1102
1103         ts = CURRENT_TIME_SEC;
1104
1105         EXFAT_I(inode)->fid.size = i_size_read(inode);
1106
1107         err = FsRemoveDir(dir, &(EXFAT_I(inode)->fid));
1108         if (err) {
1109                 if (err == FFS_INVALIDPATH)
1110                         err = -EINVAL;
1111                 else if (err == FFS_FILEEXIST)
1112                         err = -ENOTEMPTY;
1113                 else if (err == FFS_NOTFOUND)
1114                         err = -ENOENT;
1115                 else if (err == FFS_DIRBUSY)
1116                         err = -EBUSY;
1117                 else
1118                         err = -EIO;
1119                 goto out;
1120         }
1121         dir->i_version++;
1122         dir->i_mtime = dir->i_atime = ts;
1123         if (IS_DIRSYNC(dir))
1124                 (void) exfat_sync_inode(dir);
1125         else
1126                 mark_inode_dirty(dir);
1127         drop_nlink(dir);
1128
1129         clear_nlink(inode);
1130         inode->i_mtime = inode->i_atime = ts;
1131         exfat_detach(inode);
1132         remove_inode_hash(inode);
1133
1134 out:
1135         __unlock_super(sb);
1136         PRINTK("exfat_rmdir exited\n");
1137         return err;
1138 }
1139
1140 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
1141                                                 struct inode *new_dir, struct dentry *new_dentry)
1142 {
1143         struct inode *old_inode, *new_inode;
1144         struct super_block *sb = old_dir->i_sb;
1145         struct timespec ts;
1146         loff_t i_pos;
1147         int err;
1148
1149         __lock_super(sb);
1150
1151         PRINTK("exfat_rename entered\n");
1152
1153         old_inode = old_dentry->d_inode;
1154         new_inode = new_dentry->d_inode;
1155
1156         ts = CURRENT_TIME_SEC;
1157
1158         EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
1159
1160         err = FsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir, new_dentry);
1161         if (err) {
1162                 if (err == FFS_PERMISSIONERR)
1163                         err = -EPERM;
1164                 else if (err == FFS_INVALIDPATH)
1165                         err = -EINVAL;
1166                 else if (err == FFS_FILEEXIST)
1167                         err = -EEXIST;
1168                 else if (err == FFS_NOTFOUND)
1169                         err = -ENOENT;
1170                 else if (err == FFS_FULL)
1171                         err = -ENOSPC;
1172                 else
1173                         err = -EIO;
1174                 goto out;
1175         }
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);
1180         else
1181                 mark_inode_dirty(new_dir);
1182
1183         i_pos = ((loff_t) EXFAT_I(old_inode)->fid.dir.dir << 32) |
1184                         (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
1185
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);
1190         else
1191                 mark_inode_dirty(old_inode);
1192
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);
1196         }
1197
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);
1202         else
1203                 mark_inode_dirty(old_dir);
1204
1205         if (new_inode) {
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;
1211         }
1212
1213 out:
1214         __unlock_super(sb);
1215         PRINTK("exfat_rename exited\n");
1216         return err;
1217 }
1218
1219 static int exfat_cont_expand(struct inode *inode, loff_t size)
1220 {
1221         struct address_space *mapping = inode->i_mapping;
1222         loff_t start = i_size_read(inode), count = size - i_size_read(inode);
1223         int err, err2;
1224
1225         if ((err = generic_cont_expand_simple(inode, size)) != 0)
1226                 return err;
1227
1228         inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
1229         mark_inode_dirty(inode);
1230
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);
1237                 if (!err) {
1238                         err =  filemap_fdatawait_range(mapping, start, start + count - 1);
1239                 }
1240         }
1241         return err;
1242 }
1243
1244 static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
1245 {
1246         mode_t allow_utime = sbi->options.allow_utime;
1247
1248         if (current_fsuid() != inode->i_uid) {
1249                 if (in_group_p(inode->i_gid))
1250                         allow_utime >>= 3;
1251                 if (allow_utime & MAY_WRITE)
1252                         return 1;
1253         }
1254
1255         return 0;
1256 }
1257
1258 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
1259                                                            struct inode *inode, umode_t *mode_ptr)
1260 {
1261         mode_t i_mode, mask, perm;
1262
1263         i_mode = inode->i_mode;
1264
1265         if (S_ISREG(i_mode) || S_ISLNK(i_mode))
1266                 mask = sbi->options.fs_fmask;
1267         else
1268                 mask = sbi->options.fs_dmask;
1269
1270         perm = *mode_ptr & ~(S_IFMT | mask);
1271
1272         if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO|S_IXUGO)))
1273                 return -EPERM;
1274
1275         if (exfat_mode_can_hold_ro(inode)) {
1276                 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
1277                         return -EPERM;
1278         } else {
1279                 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
1280                         return -EPERM;
1281         }
1282
1283         *mode_ptr &= S_IFMT | perm;
1284
1285         return 0;
1286 }
1287
1288 static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
1289 {
1290
1291         struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
1292         struct inode *inode = dentry->d_inode;
1293         unsigned int ia_valid;
1294         int error;
1295         loff_t old_size;
1296
1297         PRINTK("exfat_setattr entered\n");
1298
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)
1303                         return error;
1304                 attr->ia_valid &= ~ATTR_SIZE;
1305         }
1306
1307         ia_valid = attr->ia_valid;
1308
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);
1312         }
1313
1314         error = inode_change_ok(inode, attr);
1315         attr->ia_valid = ia_valid;
1316         if (error) {
1317                 return error;
1318         }
1319
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)))) {
1326                 return -EPERM;
1327         }
1328
1329         if (attr->ia_valid & ATTR_MODE) {
1330                 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
1331                         attr->ia_valid &= ~ATTR_MODE;
1332         }
1333
1334         EXFAT_I(inode)->fid.size = i_size_read(inode);
1335
1336 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1337         if (attr->ia_valid)
1338                 error = inode_setattr(inode, attr);
1339 #else
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);
1347 #else
1348                 truncate_setsize(inode, attr->ia_size);
1349                 _exfat_truncate(inode, old_size);
1350 #endif
1351         }
1352         setattr_copy(inode, attr);
1353         mark_inode_dirty(inode);
1354 #endif
1355
1356
1357         PRINTK("exfat_setattr exited\n");
1358         return error;
1359 }
1360
1361 static int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1362 {
1363         struct inode *inode = dentry->d_inode;
1364
1365         PRINTK("exfat_getattr entered\n");
1366
1367         generic_fillattr(inode, stat);
1368         stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
1369
1370         PRINTK("exfat_getattr exited\n");
1371         return 0;
1372 }
1373
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,
1389 #endif
1390 };
1391
1392 static void *exfat_follow_link(struct dentry *dentry, struct nameidata *nd)
1393 {
1394         struct exfat_inode_info *ei = EXFAT_I(dentry->d_inode);
1395         nd_set_link(nd, (char *)(ei->target));
1396         return NULL;
1397 }
1398
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,
1407 #endif
1408 };
1409
1410 static int exfat_file_release(struct inode *inode, struct file *filp)
1411 {
1412         struct super_block *sb = inode->i_sb;
1413
1414         EXFAT_I(inode)->fid.size = i_size_read(inode);
1415         FsSyncVol(sb, 0);
1416         return 0;
1417 }
1418
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,
1430 #else
1431         .unlocked_ioctl  = exfat_generic_ioctl,
1432         .fsync       = generic_file_fsync,
1433 #endif
1434         .splice_read = generic_file_splice_read,
1435 };
1436
1437 static void _exfat_truncate(struct inode *inode, loff_t old_size)
1438 {
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);
1442         int err;
1443
1444         __lock_super(sb);
1445
1446         if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
1447                 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1448
1449         if (EXFAT_I(inode)->fid.start_clu == 0) goto out;
1450
1451         err = FsTruncateFile(inode, old_size, i_size_read(inode));
1452         if (err) goto out;
1453
1454         inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
1455         if (IS_DIRSYNC(inode))
1456                 (void) exfat_sync_inode(inode);
1457         else
1458                 mark_inode_dirty(inode);
1459
1460         inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
1461                                            & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
1462 out:
1463         __unlock_super(sb);
1464 }
1465
1466 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
1467 static void exfat_truncate(struct inode *inode)
1468 {
1469         _exfat_truncate(inode, i_size_read(inode));
1470 }
1471 #endif
1472
1473 const struct inode_operations exfat_file_inode_operations = {
1474 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
1475         .truncate    = exfat_truncate,
1476 #endif
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,
1484 #endif
1485 };
1486
1487 static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
1488                                           unsigned long *mapped_blocks, int *create)
1489 {
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;
1499
1500         *phys = 0;
1501         *mapped_blocks = 0;
1502
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;
1507                                 *mapped_blocks = 1;
1508                         }
1509                         return 0;
1510                 }
1511         }
1512
1513         last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
1514         if (sector >= last_block) {
1515                 if (*create == 0) return 0;
1516         } else {
1517                 *create = 0;
1518         }
1519
1520         clu_offset = sector >> p_fs->sectors_per_clu_bits;
1521         sec_offset = sector & (p_fs->sectors_per_clu - 1);
1522
1523         EXFAT_I(inode)->fid.size = i_size_read(inode);
1524
1525         err = FsMapCluster(inode, clu_offset, &cluster);
1526
1527         if (err) {
1528                 if (err == FFS_FULL)
1529                         return -ENOSPC;
1530                 else
1531                         return -EIO;
1532         } else if (cluster != CLUSTER_32(~0)) {
1533                 *phys = START_SECTOR(cluster) + sec_offset;
1534                 *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
1535         }
1536
1537         return 0;
1538 }
1539
1540 static int exfat_get_block(struct inode *inode, sector_t iblock,
1541                                                    struct buffer_head *bh_result, int create)
1542 {
1543         struct super_block *sb = inode->i_sb;
1544         unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
1545         int err;
1546         unsigned long mapped_blocks;
1547         sector_t phys;
1548
1549         __lock_super(sb);
1550
1551         err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
1552         if (err) {
1553                 __unlock_super(sb);
1554                 return err;
1555         }
1556
1557         if (phys) {
1558                 max_blocks = min(mapped_blocks, max_blocks);
1559                 if (create) {
1560                         EXFAT_I(inode)->mmu_private += max_blocks << sb->s_blocksize_bits;
1561                         set_buffer_new(bh_result);
1562                 }
1563                 map_bh(bh_result, sb, phys);
1564         }
1565
1566         bh_result->b_size = max_blocks << sb->s_blocksize_bits;
1567         __unlock_super(sb);
1568
1569         return 0;
1570 }
1571
1572 static int exfat_readpage(struct file *file, struct page *page)
1573 {
1574         int ret;
1575         ret =  mpage_readpage(page, exfat_get_block);
1576         return ret;
1577 }
1578
1579 static int exfat_readpages(struct file *file, struct address_space *mapping,
1580                                    struct list_head *pages, unsigned nr_pages)
1581 {
1582         int ret;
1583         ret =  mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
1584         return ret;
1585 }
1586
1587 static int exfat_writepage(struct page *page, struct writeback_control *wbc)
1588 {
1589         int ret;
1590         ret = block_write_full_page(page, exfat_get_block, wbc);
1591         return ret;
1592 }
1593
1594 static int exfat_writepages(struct address_space *mapping,
1595                                                 struct writeback_control *wbc)
1596 {
1597         int ret;
1598         ret = mpage_writepages(mapping, wbc, exfat_get_block);
1599         return ret;
1600 }
1601
1602 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1603 static void exfat_write_failed(struct address_space *mapping, loff_t to)
1604 {
1605         struct inode *inode = mapping->host;
1606         if (to > i_size_read(inode)) {
1607
1608 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,00)
1609                 truncate_pagecache(inode, i_size_read(inode));
1610 #else
1611                 truncate_pagecache(inode, to, i_size_read(inode));
1612 #endif
1613                 EXFAT_I(inode)->fid.size = i_size_read(inode);
1614                 _exfat_truncate(inode, i_size_read(inode));
1615         }
1616 }
1617 #endif
1618
1619
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)
1623 {
1624         int ret;
1625         *pagep = NULL;
1626         ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1627                                    exfat_get_block,
1628                                    &EXFAT_I(mapping->host)->mmu_private);
1629
1630 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1631         if (ret < 0)
1632                 exfat_write_failed(mapping, pos+len);
1633 #endif
1634         return ret;
1635 }
1636
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)
1640 {
1641         struct inode *inode = mapping->host;
1642         FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
1643         int err;
1644
1645         err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
1646
1647 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1648         if (err < len)
1649                 exfat_write_failed(mapping, pos+len);
1650 #endif
1651
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);
1656         }
1657         return err;
1658 }
1659
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)
1663 {
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;
1667 #endif
1668         ssize_t ret;
1669
1670         if (rw == WRITE) {
1671                 if (EXFAT_I(inode)->mmu_private < (offset + iov_length(iov, nr_segs)))
1672                         return 0;
1673         }
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);
1677 #else
1678         ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1679                                         offset, nr_segs, exfat_get_block, NULL);
1680 #endif
1681
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));
1685 #endif
1686         return ret;
1687
1688 }
1689
1690 static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
1691 {
1692         sector_t blocknr;
1693
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);
1698 #else
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);
1702 #endif
1703
1704         return blocknr;
1705 }
1706
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,
1714 #endif
1715         .write_begin = exfat_write_begin,
1716         .write_end   = exfat_write_end,
1717         .direct_IO   = exfat_direct_IO,
1718         .bmap        = _exfat_bmap
1719 };
1720
1721 static inline unsigned long exfat_hash(loff_t i_pos)
1722 {
1723         return hash_32(i_pos, EXFAT_HASH_BITS);
1724 }
1725
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;
1733
1734         spin_lock(&sbi->inode_hash_lock);
1735         hlist_for_each_entry(info, node, head, i_hash_fat) {
1736 #else
1737         spin_lock(&sbi->inode_hash_lock);
1738         hlist_for_each_entry(info, head, i_hash_fat) {
1739 #endif
1740                 CHECK_ERR(info->vfs_inode.i_sb != sb);
1741
1742                 if (i_pos != info->i_pos)
1743                         continue;
1744                 inode = igrab(&info->vfs_inode);
1745                 if (inode)
1746                         break;
1747         }
1748         spin_unlock(&sbi->inode_hash_lock);
1749         return inode;
1750 }
1751
1752 static void exfat_attach(struct inode *inode, loff_t i_pos)
1753 {
1754         struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1755         struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
1756
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);
1761 }
1762
1763 static void exfat_detach(struct inode *inode)
1764 {
1765         struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1766
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);
1771 }
1772
1773 static int exfat_fill_inode(struct inode *inode, FILE_ID_T *fid)
1774 {
1775         struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1776         FS_INFO_T *p_fs = &(sbi->fs_info);
1777         DIR_ENTRY_T info;
1778
1779         memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(FILE_ID_T));
1780
1781         FsReadStat(inode, &info);
1782
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;
1787         inode->i_version++;
1788         inode->i_generation = get_seconds();
1789
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;
1795
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);
1800 #else
1801                 inode->i_nlink = info.NumSubdirs;
1802 #endif
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;
1807
1808                 i_size_write(inode, info.Size);
1809                 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1810         } else {
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;
1817
1818                 i_size_write(inode, info.Size);
1819                 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1820         }
1821         exfat_save_attr(inode, info.Attr);
1822
1823         inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
1824                                            & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
1825
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);
1829
1830         return 0;
1831 }
1832
1833 static struct inode *exfat_build_inode(struct super_block *sb,
1834                                                                            FILE_ID_T *fid, loff_t i_pos) {
1835         struct inode *inode;
1836         int err;
1837
1838         inode = exfat_iget(sb, i_pos);
1839         if (inode)
1840                 goto out;
1841         inode = new_inode(sb);
1842         if (!inode) {
1843                 inode = ERR_PTR(-ENOMEM);
1844                 goto out;
1845         }
1846         inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
1847         inode->i_version = 1;
1848         err = exfat_fill_inode(inode, fid);
1849         if (err) {
1850                 iput(inode);
1851                 inode = ERR_PTR(err);
1852                 goto out;
1853         }
1854         exfat_attach(inode, i_pos);
1855         insert_inode_hash(inode);
1856 out:
1857         return inode;
1858 }
1859
1860 static int exfat_sync_inode(struct inode *inode)
1861 {
1862         return exfat_write_inode(inode, NULL);
1863 }
1864
1865 static struct inode *exfat_alloc_inode(struct super_block *sb) {
1866         struct exfat_inode_info *ei;
1867
1868         ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
1869         if (!ei)
1870                 return NULL;
1871
1872 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1873         init_rwsem(&ei->truncate_lock);
1874 #endif
1875
1876         return &ei->vfs_inode;
1877 }
1878
1879 static void exfat_destroy_inode(struct inode *inode)
1880 {
1881         FREE(EXFAT_I(inode)->target);
1882         EXFAT_I(inode)->target = NULL;
1883
1884         kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
1885 }
1886
1887 static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
1888 {
1889         struct super_block *sb = inode->i_sb;
1890         struct exfat_sb_info *sbi = EXFAT_SB(sb);
1891         DIR_ENTRY_T info;
1892
1893         if (inode->i_ino == EXFAT_ROOT_INO)
1894                 return 0;
1895
1896         info.Attr = exfat_make_attr(inode);
1897         info.Size = i_size_read(inode);
1898
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);
1902
1903         FsWriteStat(inode, &info);
1904
1905         return 0;
1906 }
1907
1908 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1909 static void exfat_delete_inode(struct inode *inode)
1910 {
1911         truncate_inode_pages(&inode->i_data, 0);
1912         clear_inode(inode);
1913 }
1914
1915 static void exfat_clear_inode(struct inode *inode)
1916 {
1917         exfat_detach(inode);
1918         remove_inode_hash(inode);
1919 }
1920 #else
1921 static void exfat_evict_inode(struct inode *inode)
1922 {
1923         truncate_inode_pages(&inode->i_data, 0);
1924
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);
1930         }
1931
1932         invalidate_inode_buffers(inode);
1933 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
1934         end_writeback(inode);
1935 #else
1936         clear_inode(inode);
1937 #endif
1938         exfat_detach(inode);
1939
1940         remove_inode_hash(inode);
1941 }
1942 #endif
1943
1944
1945 static void exfat_put_super(struct super_block *sb)
1946 {
1947         struct exfat_sb_info *sbi = EXFAT_SB(sb);
1948         int err;
1949
1950         exfat_mnt_msg(sb, 0, 0, "trying to unmount...");
1951
1952         if (__is_sb_dirty(sb))
1953                 exfat_write_super(sb);
1954
1955         err = FsUmountVol(sb);
1956
1957         if (sbi->nls_disk) {
1958                 unload_nls(sbi->nls_disk);
1959                 sbi->nls_disk = NULL;
1960                 sbi->options.codepage = exfat_default_codepage;
1961         }
1962         if (sbi->nls_io) {
1963                 unload_nls(sbi->nls_io);
1964                 sbi->nls_io = NULL;
1965         }
1966         if (sbi->options.iocharset != exfat_default_iocharset) {
1967                 kfree(sbi->options.iocharset);
1968                 sbi->options.iocharset = exfat_default_iocharset;
1969         }
1970
1971         sb->s_fs_info = NULL;
1972         if (!sbi->use_vmalloc)
1973                 kfree(sbi);
1974         else
1975                 vfree(sbi);
1976
1977         exfat_mnt_msg(sb, 0, err, "unmounted successfully!");
1978 }
1979
1980 static void exfat_write_super(struct super_block *sb)
1981 {
1982         __lock_super(sb);
1983
1984         __set_sb_clean(sb);
1985
1986         if (!(sb->s_flags & MS_RDONLY))
1987                 FsSyncVol(sb, 1);
1988
1989         __unlock_super(sb);
1990 }
1991
1992 static int exfat_sync_fs(struct super_block *sb, int wait)
1993 {
1994         int err = 0;
1995
1996         if (__is_sb_dirty(sb)) {
1997                 __lock_super(sb);
1998                 __set_sb_clean(sb);
1999                 err = FsSyncVol(sb, 1);
2000                 __unlock_super(sb);
2001         }
2002
2003         return err;
2004 }
2005
2006 static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
2007 {
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);
2011         VOL_INFO_T info;
2012
2013         if (p_fs->used_clusters == (UINT32) ~0) {
2014                 if (FFS_MEDIAERR == FsGetVolInfo(sb, &info))
2015                         return -EIO;
2016
2017         } else {
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;
2023
2024                 if (p_fs->dev_ejected)
2025                         printk("[EXFAT] called statfs with previous I/O error.\n");
2026         }
2027
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;
2036
2037         return 0;
2038 }
2039
2040 static int exfat_remount(struct super_block *sb, int *flags, char *data)
2041 {
2042         *flags |= MS_NODIRATIME;
2043         return 0;
2044 }
2045
2046 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
2047 static int exfat_show_options(struct seq_file *m, struct dentry *root)
2048 {
2049         struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
2050 #else
2051 static int exfat_show_options(struct seq_file *m, struct vfsmount *mnt)
2052 {
2053         struct exfat_sb_info *sbi = EXFAT_SB(mnt->mnt_sb);
2054 #endif
2055         struct exfat_mount_options *opts = &sbi->options;
2056         FS_INFO_T *p_fs = &(sbi->fs_info);
2057
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);
2066         if (sbi->nls_disk)
2067                 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
2068         if (sbi->nls_io)
2069                 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
2070         seq_printf(m, ",namecase=%u", opts->casesensitive);
2071         if (opts->tz_utc)
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");
2077         else
2078                 seq_puts(m, ",errors=remount-ro");
2079 #if EXFAT_CONFIG_DISCARD
2080         if (opts->discard)
2081                 seq_printf(m, ",discard");
2082 #endif
2083         if (p_fs->dev_ejected)
2084                 seq_puts(m, ",ejected");
2085         return 0;
2086 }
2087
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,
2095 #else
2096         .evict_inode  = exfat_evict_inode,
2097 #endif
2098         .put_super     = exfat_put_super,
2099 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,00)
2100         .write_super   = exfat_write_super,
2101 #endif
2102         .sync_fs       = exfat_sync_fs,
2103         .statfs        = exfat_statfs,
2104         .remount_fs    = exfat_remount,
2105         .show_options  = exfat_show_options,
2106 };
2107
2108 enum {
2109         Opt_uid,
2110         Opt_gid,
2111         Opt_umask,
2112         Opt_dmask,
2113         Opt_fmask,
2114         Opt_allow_utime,
2115         Opt_codepage,
2116         Opt_charset,
2117         Opt_namecase,
2118         Opt_debug,
2119         Opt_tz_utc,
2120         Opt_err_cont,
2121         Opt_err_panic,
2122         Opt_err_ro,
2123         Opt_err,
2124 #if EXFAT_CONFIG_DISCARD
2125         Opt_discard,
2126 #endif
2127 };
2128
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"},
2146 #endif
2147         {Opt_err, NULL}
2148 };
2149
2150 static int parse_options(char *options, int silent, int *debug,
2151                                                  struct exfat_mount_options *opts)
2152 {
2153         char *p;
2154         substring_t args[MAX_OPT_ARGS];
2155         int option;
2156         char *iocharset;
2157
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;
2165         opts->tz_utc = 0;
2166         opts->errors = EXFAT_ERRORS_RO;
2167 #if EXFAT_CONFIG_DISCARD
2168         opts->discard = 0;
2169 #endif
2170         *debug = 0;
2171
2172         if (!options)
2173                 goto out;
2174
2175         while ((p = strsep(&options, ",")) != NULL) {
2176                 int token;
2177                 if (!*p)
2178                         continue;
2179
2180                 token = match_token(p, exfat_tokens, args);
2181                 switch (token) {
2182                 case Opt_uid:
2183                         if (match_int(&args[0], &option))
2184                                 return 0;
2185                         opts->fs_uid = option;
2186                         break;
2187                 case Opt_gid:
2188                         if (match_int(&args[0], &option))
2189                                 return 0;
2190                         opts->fs_gid = option;
2191                         break;
2192                 case Opt_umask:
2193                 case Opt_dmask:
2194                 case Opt_fmask:
2195                         if (match_octal(&args[0], &option))
2196                                 return 0;
2197                         if (token != Opt_dmask)
2198                                 opts->fs_fmask = option;
2199                         if (token != Opt_fmask)
2200                                 opts->fs_dmask = option;
2201                         break;
2202                 case Opt_allow_utime:
2203                         if (match_octal(&args[0], &option))
2204                                 return 0;
2205                         opts->allow_utime = option & (S_IWGRP | S_IWOTH);
2206                         break;
2207                 case Opt_codepage:
2208                         if (match_int(&args[0], &option))
2209                                 return 0;
2210                         opts->codepage = option;
2211                         break;
2212                 case Opt_charset:
2213                         if (opts->iocharset != exfat_default_iocharset)
2214                                 kfree(opts->iocharset);
2215                         iocharset = match_strdup(&args[0]);
2216                         if (!iocharset)
2217                                 return -ENOMEM;
2218                         opts->iocharset = iocharset;
2219                         break;
2220                 case Opt_namecase:
2221                         if (match_int(&args[0], &option))
2222                                 return 0;
2223                         opts->casesensitive = option;
2224                         break;
2225                 case Opt_tz_utc:
2226                         opts->tz_utc = 1;
2227                         break;
2228                 case Opt_err_cont:
2229                         opts->errors = EXFAT_ERRORS_CONT;
2230                         break;
2231                 case Opt_err_panic:
2232                         opts->errors = EXFAT_ERRORS_PANIC;
2233                         break;
2234                 case Opt_err_ro:
2235                         opts->errors = EXFAT_ERRORS_RO;
2236                         break;
2237                 case Opt_debug:
2238                         *debug = 1;
2239                         break;
2240 #if EXFAT_CONFIG_DISCARD
2241                 case Opt_discard:
2242                         opts->discard = 1;
2243                         break;
2244 #endif
2245                 default:
2246                         if (!silent) {
2247                                 printk(KERN_ERR "[EXFAT] Unrecognized mount option %s or missing value\n", p);
2248                         }
2249                         return -EINVAL;
2250                 }
2251         }
2252
2253 out:
2254         if (opts->allow_utime == (unsigned short) -1)
2255                 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
2256
2257         return 0;
2258 }
2259
2260 static void exfat_hash_init(struct super_block *sb)
2261 {
2262         struct exfat_sb_info *sbi = EXFAT_SB(sb);
2263         int i;
2264
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]);
2268 }
2269
2270 static int exfat_read_root(struct inode *inode)
2271 {
2272         struct super_block *sb = inode->i_sb;
2273         struct exfat_sb_info *sbi = EXFAT_SB(sb);
2274         struct timespec ts;
2275         FS_INFO_T *p_fs = &(sbi->fs_info);
2276         DIR_ENTRY_T info;
2277
2278         ts = CURRENT_TIME_SEC;
2279
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;
2288
2289         EXFAT_I(inode)->target = NULL;
2290
2291         FsReadStat(inode, &info);
2292
2293         inode->i_uid = sbi->options.fs_uid;
2294         inode->i_gid = sbi->options.fs_gid;
2295         inode->i_version++;
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;
2300
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);
2306
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);
2311 #else
2312         inode->i_nlink = info.NumSubdirs + 2;
2313 #endif
2314
2315         return 0;
2316 }
2317
2318 static void setup_dops(struct super_block *sb)
2319 {
2320         if (EXFAT_SB(sb)->options.casesensitive == 0)
2321                 sb->s_d_op = &exfat_ci_dentry_ops;
2322         else
2323                 sb->s_d_op = &exfat_dentry_ops;
2324 }
2325
2326
2327 static int exfat_fill_super(struct super_block *sb, void *data, int silent)
2328 {
2329         struct inode *root_inode = NULL;
2330         struct exfat_sb_info *sbi;
2331         int debug, ret;
2332         long error;
2333         char buf[50];
2334
2335         exfat_mnt_msg(sb, 1, 0, "trying to mount...");
2336
2337         sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
2338         if (!sbi) {
2339                 sbi = vzalloc(sizeof(struct exfat_sb_info));
2340                 if (!sbi) {
2341                         exfat_mnt_msg(sb, 1, 0, "failed to mount! (ENOMEM)");
2342                         return -ENOMEM;
2343                 }
2344                 sbi->use_vmalloc = 1;
2345         }
2346 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
2347         mutex_init(&sbi->s_lock);
2348 #endif
2349         sb->s_fs_info = sbi;
2350
2351         sb->s_flags |= MS_NODIRATIME;
2352         sb->s_magic = EXFAT_SUPER_MAGIC;
2353         sb->s_op = &exfat_sops;
2354
2355         error = parse_options(data, silent, &debug, &sbi->options);
2356         if (error)
2357                 goto out_fail;
2358
2359         setup_dops(sb);
2360
2361         error = -EIO;
2362         sb_min_blocksize(sb, 512);
2363         sb->s_maxbytes = 0x7fffffffffffffffLL;
2364
2365         ret = FsMountVol(sb);
2366         if (ret) {
2367                 if (!silent)
2368                         printk(KERN_ERR "[EXFAT] FsMountVol failed\n");
2369
2370                 goto out_fail;
2371         }
2372
2373         exfat_hash_init(sb);
2374
2375         error = -EINVAL;
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);
2380                 goto out_fail2;
2381         }
2382
2383         sbi->nls_io = load_nls(sbi->options.iocharset);
2384         if (!sbi->nls_io) {
2385                 printk(KERN_ERR "[EXFAT] IO charset %s not found\n",
2386                            sbi->options.iocharset);
2387                 goto out_fail2;
2388         }
2389
2390         error = -ENOMEM;
2391         root_inode = new_inode(sb);
2392         if (!root_inode)
2393                 goto out_fail2;
2394         root_inode->i_ino = EXFAT_ROOT_INO;
2395         root_inode->i_version = 1;
2396         error = exfat_read_root(root_inode);
2397         if (error < 0)
2398                 goto out_fail2;
2399         error = -ENOMEM;
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);
2404 #else
2405         sb->s_root = d_alloc_root(root_inode);
2406 #endif
2407         if (!sb->s_root) {
2408                 printk(KERN_ERR "[EXFAT] Getting the root inode failed\n");
2409                 goto out_fail2;
2410         }
2411
2412         exfat_mnt_msg(sb, 1, 0, "mounted successfully!");
2413
2414         return 0;
2415
2416 out_fail2:
2417         FsUmountVol(sb);
2418 out_fail:
2419         exfat_mnt_msg(sb, 1, 0, "failed to mount!");
2420
2421         if (root_inode)
2422                 iput(root_inode);
2423         if (sbi->nls_io)
2424                 unload_nls(sbi->nls_io);
2425         if (sbi->nls_disk)
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)
2431                 kfree(sbi);
2432         else
2433                 vfree(sbi);
2434         return error;
2435 }
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)
2440 {
2441         return get_sb_bdev(fs_type, flags, dev_name, data, exfat_fill_super, mnt);
2442 }
2443 #else
2444 static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
2445                                                                          int flags, const char *dev_name,
2446                                                                          void *data) {
2447         return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
2448 }
2449 #endif
2450
2451 static void init_once(void *foo)
2452 {
2453         struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
2454
2455         INIT_HLIST_NODE(&ei->i_hash_fat);
2456         inode_init_once(&ei->vfs_inode);
2457 }
2458
2459 static int __init exfat_init_inodecache(void)
2460 {
2461         exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
2462                                                                                    sizeof(struct exfat_inode_info),
2463                                                                                    0, (SLAB_RECLAIM_ACCOUNT|
2464                                                                                                    SLAB_MEM_SPREAD),
2465                                                                                    init_once);
2466         if (exfat_inode_cachep == NULL)
2467                 return -ENOMEM;
2468         return 0;
2469 }
2470
2471 static void __exit exfat_destroy_inodecache(void)
2472 {
2473         kmem_cache_destroy(exfat_inode_cachep);
2474 }
2475
2476 #if EXFAT_CONFIG_KERNEL_DEBUG
2477 static void exfat_debug_kill_sb(struct super_block *sb)
2478 {
2479         struct exfat_sb_info *sbi = EXFAT_SB(sb);
2480         struct block_device *bdev = sb->s_bdev;
2481
2482         long flags;
2483
2484         if (sbi) {
2485                 flags = sbi->debug_flags;
2486
2487                 if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
2488                         FsReleaseCache(sb);
2489                         invalidate_bdev(bdev);
2490                 }
2491         }
2492
2493         kill_block_super(sb);
2494 }
2495 #endif
2496
2497 static struct file_system_type exfat_fs_type = {
2498         .owner       = THIS_MODULE,
2499         .name        = "exfat",
2500 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
2501         .get_sb      = exfat_get_sb,
2502 #else
2503         .mount       = exfat_fs_mount,
2504 #endif
2505 #if EXFAT_CONFIG_KERNEL_DEBUG
2506         .kill_sb    = exfat_debug_kill_sb,
2507 #else
2508         .kill_sb    = kill_block_super,
2509 #endif
2510         .fs_flags    = FS_REQUIRES_DEV,
2511 };
2512
2513 static int __init init_exfat_fs(void)
2514 {
2515         int err;
2516
2517         printk(KERN_INFO "exFAT: FS Version %s\n", EXFAT_VERSION);
2518
2519         err = exfat_init_inodecache();
2520         if (err) return err;
2521
2522         return register_filesystem(&exfat_fs_type);
2523 }
2524
2525 static void __exit exit_exfat_fs(void)
2526 {
2527         exfat_destroy_inodecache();
2528         unregister_filesystem(&exfat_fs_type);
2529 }
2530
2531 module_init(init_exfat_fs);
2532 module_exit(exit_exfat_fs);
2533
2534 MODULE_LICENSE("GPL");