CD exfat support for Tomato. https://github.com/dorimanx/exfat-nofuse.
[tomato.git] / release / src-rt / linux / linux-2.6 / fs / exfat / exfat_super.c
blob0cb305e89f41b485e33801aedc026916b996c993
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
5 * Written 1992,1993 by Werner Almesberger
7 * regular file handling primitives for fat-based filesystems
8 */
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
17 * Fixes:
19 * Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
23 * linux/fs/fat/misc.c
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 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
33 * This program is free software; you can redistribute it and/or
34 * modify it under the terms of the GNU General Public License
35 * as published by the Free Software Foundation; either version 2
36 * of the License, or (at your option) any later version.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48 #include <linux/version.h>
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/time.h>
52 #include <linux/slab.h>
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
54 #include <linux/smp_lock.h>
55 #endif
56 #include <linux/seq_file.h>
57 #include <linux/pagemap.h>
58 #include <linux/mpage.h>
59 #include <linux/buffer_head.h>
60 #include <linux/exportfs.h>
61 #include <linux/mount.h>
62 #include <linux/vfs.h>
63 #include <linux/parser.h>
64 #include <linux/uio.h>
65 #include <linux/writeback.h>
66 #include <linux/log2.h>
67 #include <linux/hash.h>
68 #include <linux/backing-dev.h>
69 #include <linux/sched.h>
70 #include <linux/fs_struct.h>
71 #include <linux/namei.h>
72 #include <asm/current.h>
73 #include <asm/unaligned.h>
74 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
75 #include <linux/aio.h>
76 #endif
78 #include "exfat_version.h"
79 #include "exfat_config.h"
80 #include "exfat_global.h"
81 #include "exfat_data.h"
82 #include "exfat_oal.h"
84 #include "exfat_blkdev.h"
85 #include "exfat_cache.h"
86 #include "exfat_part.h"
87 #include "exfat_nls.h"
88 #include "exfat_api.h"
89 #include "exfat.h"
91 #include "exfat_super.h"
93 static struct kmem_cache *exfat_inode_cachep;
95 static int exfat_default_codepage = CONFIG_EXFAT_DEFAULT_CODEPAGE;
96 static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
98 extern struct timezone sys_tz;
100 #define CHECK_ERR(x) BUG_ON(x)
101 #define ELAPSED_TIME 0
103 #if (ELAPSED_TIME == 1)
104 #include <linux/time.h>
106 static UINT32 __t1, __t2;
107 static UINT32 get_current_msec(void)
109 struct timeval tm;
110 do_gettimeofday(&tm);
111 return((UINT32)(tm.tv_sec*1000000 + tm.tv_usec));
113 #define TIME_START() do {__t1 = get_current_msec();} while (0)
114 #define TIME_END() do {__t2 = get_current_msec();} while (0)
115 #define PRINT_TIME(n) do {printk("[EXFAT] Elapsed time %d = %d (usec)\n", n, (__t2 - __t1));} while (0)
116 #else
117 #define TIME_START()
118 #define TIME_END()
119 #define PRINT_TIME(n)
120 #endif
122 #define UNIX_SECS_1980 315532800L
124 #if BITS_PER_LONG == 64
125 #define UNIX_SECS_2108 4354819200L
126 #endif
127 /* days between 1.1.70 and 1.1.80 (2 leap days) */
128 #define DAYS_DELTA_DECADE (365 * 10 + 2)
129 /* 120 (2100 - 1980) isn't leap year */
130 #define NO_LEAP_YEAR_2100 (120)
131 #define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100)
133 #define SECS_PER_MIN (60)
134 #define SECS_PER_HOUR (60 * SECS_PER_MIN)
135 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
137 #define MAKE_LEAP_YEAR(leap_year, year) \
138 do { \
139 if (unlikely(year > NO_LEAP_YEAR_2100)) \
140 leap_year = ((year + 3) / 4) - 1; \
141 else \
142 leap_year = ((year + 3) / 4); \
143 } while(0)
145 /* Linear day numbers of the respective 1sts in non-leap years. */
146 static time_t accum_days_in_year[] = {
147 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
148 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
151 static void _exfat_truncate(struct inode *inode, loff_t old_size);
153 /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
154 void exfat_time_fat2unix(struct exfat_sb_info *sbi, struct timespec *ts,
155 DATE_TIME_T *tp)
157 time_t year = tp->Year;
158 time_t ld;
160 MAKE_LEAP_YEAR(ld, year);
162 if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
163 ld++;
165 ts->tv_sec = tp->Second + tp->Minute * SECS_PER_MIN
166 + tp->Hour * SECS_PER_HOUR
167 + (year * 365 + ld + accum_days_in_year[(tp->Month)] + (tp->Day - 1) + DAYS_DELTA_DECADE) * SECS_PER_DAY
168 + sys_tz.tz_minuteswest * SECS_PER_MIN;
169 ts->tv_nsec = 0;
172 /* Convert linear UNIX date to a FAT time/date pair. */
173 void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec *ts,
174 DATE_TIME_T *tp)
176 time_t second = ts->tv_sec;
177 time_t day, month, year;
178 time_t ld;
180 second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
182 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
183 if (second < UNIX_SECS_1980) {
184 tp->Second = 0;
185 tp->Minute = 0;
186 tp->Hour = 0;
187 tp->Day = 1;
188 tp->Month = 1;
189 tp->Year = 0;
190 return;
192 #if (BITS_PER_LONG == 64)
193 if (second >= UNIX_SECS_2108) {
194 tp->Second = 59;
195 tp->Minute = 59;
196 tp->Hour = 23;
197 tp->Day = 31;
198 tp->Month = 12;
199 tp->Year = 127;
200 return;
202 #endif
203 day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
204 year = day / 365;
205 MAKE_LEAP_YEAR(ld, year);
206 if (year * 365 + ld > day)
207 year--;
209 MAKE_LEAP_YEAR(ld, year);
210 day -= year * 365 + ld;
212 if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
213 month = 2;
214 } else {
215 if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
216 day--;
217 for (month = 1; month < 12; month++) {
218 if (accum_days_in_year[month + 1] > day)
219 break;
222 day -= accum_days_in_year[month];
224 tp->Second = second % SECS_PER_MIN;
225 tp->Minute = (second / SECS_PER_MIN) % 60;
226 tp->Hour = (second / SECS_PER_HOUR) % 24;
227 tp->Day = day + 1;
228 tp->Month = month;
229 tp->Year = year;
232 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
233 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
234 static int exfat_generic_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
235 #else
236 static long exfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
237 #endif
238 static int exfat_sync_inode(struct inode *inode);
239 static struct inode *exfat_build_inode(struct super_block *sb, FILE_ID_T *fid, loff_t i_pos);
240 static void exfat_detach(struct inode *inode);
241 static void exfat_attach(struct inode *inode, loff_t i_pos);
242 static inline unsigned long exfat_hash(loff_t i_pos);
243 static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
244 static void exfat_write_super(struct super_block *sb);
246 static void __lock_super(struct super_block *sb)
248 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
249 lock_super(sb);
250 #else
251 struct exfat_sb_info *sbi = EXFAT_SB(sb);
252 mutex_lock(&sbi->s_lock);
253 #endif
256 static void __unlock_super(struct super_block *sb)
258 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
259 unlock_super(sb);
260 #else
261 struct exfat_sb_info *sbi = EXFAT_SB(sb);
262 mutex_unlock(&sbi->s_lock);
263 #endif
266 static int __is_sb_dirty(struct super_block *sb)
268 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
269 return sb->s_dirt;
270 #else
271 struct exfat_sb_info *sbi = EXFAT_SB(sb);
272 return sbi->s_dirt;
273 #endif
276 static void __set_sb_clean(struct super_block *sb)
278 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
279 sb->s_dirt = 0;
280 #else
281 struct exfat_sb_info *sbi = EXFAT_SB(sb);
282 sbi->s_dirt = 0;
283 #endif
286 /*======================================================================*/
287 /* Directory Entry Operations */
288 /*======================================================================*/
290 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
291 static int exfat_readdir(struct file *filp, struct dir_context *ctx)
292 #else
293 static int exfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
294 #endif
296 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
297 struct inode *inode = file_inode(filp);
298 #else
299 struct inode *inode = filp->f_path.dentry->d_inode;
300 #endif
301 struct super_block *sb = inode->i_sb;
302 struct exfat_sb_info *sbi = EXFAT_SB(sb);
303 FS_INFO_T *p_fs = &(sbi->fs_info);
304 BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
305 DIR_ENTRY_T de;
306 unsigned long inum;
307 loff_t cpos;
308 int err = 0;
310 __lock_super(sb);
312 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
313 cpos = ctx->pos;
314 #else
315 cpos = filp->f_pos;
316 #endif
317 /* Fake . and .. for the root directory. */
318 if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
319 while (cpos < 2) {
320 if (inode->i_ino == EXFAT_ROOT_INO)
321 inum = EXFAT_ROOT_INO;
322 else if (cpos == 0)
323 inum = inode->i_ino;
324 else /* (cpos == 1) */
325 inum = parent_ino(filp->f_path.dentry);
327 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
328 if (!dir_emit_dots(filp, ctx))
329 #else
330 if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
331 #endif
332 goto out;
333 cpos++;
334 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
335 ctx->pos++;
336 #else
337 filp->f_pos++;
338 #endif
340 if (cpos == 2) {
341 cpos = 0;
344 if (cpos & (DENTRY_SIZE - 1)) {
345 err = -ENOENT;
346 goto out;
349 get_new:
350 EXFAT_I(inode)->fid.size = i_size_read(inode);
351 EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
353 err = FsReadDir(inode, &de);
354 if (err) {
355 /* at least we tried to read a sector
356 * move cpos to next sector position (should be aligned)
358 if (err == FFS_MEDIAERR) {
359 cpos += 1 << p_bd->sector_size_bits;
360 cpos &= ~((1 << p_bd->sector_size_bits)-1);
363 err = -EIO;
364 goto end_of_dir;
367 cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
369 if (!de.Name[0])
370 goto end_of_dir;
372 if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
373 inum = inode->i_ino;
374 } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
375 inum = parent_ino(filp->f_path.dentry);
376 } else {
377 loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
378 ((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
380 struct inode *tmp = exfat_iget(sb, i_pos);
381 if (tmp) {
382 inum = tmp->i_ino;
383 iput(tmp);
384 } else {
385 inum = iunique(sb, EXFAT_ROOT_INO);
389 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
390 if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
391 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
392 #else
393 if (filldir(dirent, de.Name, strlen(de.Name), cpos-1, inum,
394 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
395 #endif
396 goto out;
398 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
399 ctx->pos = cpos;
400 #else
401 filp->f_pos = cpos;
402 #endif
403 goto get_new;
405 end_of_dir:
406 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
407 ctx->pos = cpos;
408 #else
409 filp->f_pos = cpos;
410 #endif
411 out:
412 __unlock_super(sb);
413 return err;
416 static int exfat_ioctl_volume_id(struct inode *dir)
418 struct super_block *sb = dir->i_sb;
419 struct exfat_sb_info *sbi = EXFAT_SB(sb);
420 FS_INFO_T *p_fs = &(sbi->fs_info);
422 return p_fs->vol_id;
425 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
426 static int exfat_generic_ioctl(struct inode *inode, struct file *filp,
427 unsigned int cmd, unsigned long arg)
428 #else
429 static long exfat_generic_ioctl(struct file *filp,
430 unsigned int cmd, unsigned long arg)
431 #endif
433 #if EXFAT_CONFIG_KERNEL_DEBUG
434 #if !(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
435 struct inode *inode = filp->f_dentry->d_inode;
436 #endif
437 unsigned int flags;
438 #endif /* EXFAT_CONFIG_KERNEL_DEBUG */
440 switch (cmd) {
441 case EXFAT_IOCTL_GET_VOLUME_ID:
442 return exfat_ioctl_volume_id(inode);
443 #if EXFAT_CONFIG_KERNEL_DEBUG
444 case EXFAT_IOC_GET_DEBUGFLAGS: {
445 struct super_block *sb = inode->i_sb;
446 struct exfat_sb_info *sbi = EXFAT_SB(sb);
448 flags = sbi->debug_flags;
449 return put_user(flags, (int __user *)arg);
451 case EXFAT_IOC_SET_DEBUGFLAGS: {
452 struct super_block *sb = inode->i_sb;
453 struct exfat_sb_info *sbi = EXFAT_SB(sb);
455 if (!capable(CAP_SYS_ADMIN))
456 return -EPERM;
458 if (get_user(flags, (int __user *) arg))
459 return -EFAULT;
461 __lock_super(sb);
462 sbi->debug_flags = flags;
463 __unlock_super(sb);
465 return 0;
467 #endif /* EXFAT_CONFIG_KERNEL_DEBUG */
468 default:
469 return -ENOTTY; /* Inappropriate ioctl for device */
473 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
474 static int exfat_file_fsync(struct file *filp, int datasync)
476 struct inode *inode = filp->f_mapping->host;
477 struct super_block *sb = inode->i_sb;
478 int res, err;
480 res = generic_file_fsync(filp, datasync);
481 err = FsSyncVol(sb, 1);
483 return res ? res : err;
485 #endif
487 const struct file_operations exfat_dir_operations = {
488 .llseek = generic_file_llseek,
489 .read = generic_read_dir,
490 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
491 .iterate = exfat_readdir,
492 #else
493 .readdir = exfat_readdir,
494 #endif
495 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
496 .ioctl = exfat_generic_ioctl,
497 .fsync = exfat_file_fsync,
498 #else
499 .unlocked_ioctl = exfat_generic_ioctl,
500 .fsync = generic_file_fsync,
501 #endif
504 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
505 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
506 bool excl)
507 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
508 static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
509 struct nameidata *nd)
510 #else
511 static int exfat_create(struct inode *dir, struct dentry *dentry, int mode,
512 struct nameidata *nd)
513 #endif
515 struct super_block *sb = dir->i_sb;
516 struct inode *inode;
517 struct timespec ts;
518 FILE_ID_T fid;
519 loff_t i_pos;
520 int err;
522 __lock_super(sb);
524 PRINTK("exfat_create entered\n");
526 ts = CURRENT_TIME_SEC;
528 err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_REGULAR, &fid);
529 if (err) {
530 if (err == FFS_INVALIDPATH)
531 err = -EINVAL;
532 else if (err == FFS_FILEEXIST)
533 err = -EEXIST;
534 else if (err == FFS_FULL)
535 err = -ENOSPC;
536 else if (err == FFS_NAMETOOLONG)
537 err = -ENAMETOOLONG;
538 else
539 err = -EIO;
540 goto out;
542 dir->i_version++;
543 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
544 if (IS_DIRSYNC(dir))
545 (void) exfat_sync_inode(dir);
546 else
547 mark_inode_dirty(dir);
549 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
551 inode = exfat_build_inode(sb, &fid, i_pos);
552 if (IS_ERR(inode)) {
553 err = PTR_ERR(inode);
554 goto out;
556 inode->i_version++;
557 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
558 /* timestamp is already written, so mark_inode_dirty() is unnecessary. */
560 dentry->d_time = dentry->d_parent->d_inode->i_version;
561 d_instantiate(dentry, inode);
563 out:
564 __unlock_super(sb);
565 PRINTK("exfat_create exited\n");
566 return err;
569 static int exfat_find(struct inode *dir, struct qstr *qname,
570 FILE_ID_T *fid)
572 int err;
574 if (qname->len == 0)
575 return -ENOENT;
577 err = FsLookupFile(dir, (UINT8 *) qname->name, fid);
578 if (err)
579 return -ENOENT;
581 return 0;
584 static int exfat_d_anon_disconn(struct dentry *dentry)
586 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
589 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
590 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
591 unsigned int flags)
592 #else
593 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
594 struct nameidata *nd)
595 #endif
597 struct super_block *sb = dir->i_sb;
598 struct inode *inode;
599 struct dentry *alias;
600 int err;
601 FILE_ID_T fid;
602 loff_t i_pos;
603 UINT64 ret;
604 mode_t i_mode;
606 __lock_super(sb);
607 PRINTK("exfat_lookup entered\n");
608 err = exfat_find(dir, &dentry->d_name, &fid);
609 if (err) {
610 if (err == -ENOENT) {
611 inode = NULL;
612 goto out;
614 goto error;
617 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
618 inode = exfat_build_inode(sb, &fid, i_pos);
619 if (IS_ERR(inode)) {
620 err = PTR_ERR(inode);
621 goto error;
624 i_mode = inode->i_mode;
625 if (S_ISLNK(i_mode)) {
626 EXFAT_I(inode)->target = MALLOC(i_size_read(inode)+1);
627 if (!EXFAT_I(inode)->target) {
628 err = -ENOMEM;
629 goto error;
631 FsReadFile(dir, &fid, EXFAT_I(inode)->target, i_size_read(inode), &ret);
632 *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
635 alias = d_find_alias(inode);
636 if (alias && !exfat_d_anon_disconn(alias)) {
637 CHECK_ERR(d_unhashed(alias));
638 if (!S_ISDIR(i_mode))
639 d_move(alias, dentry);
640 iput(inode);
641 __unlock_super(sb);
642 PRINTK("exfat_lookup exited 1\n");
643 return alias;
644 } else {
645 dput(alias);
647 out:
648 __unlock_super(sb);
649 dentry->d_time = dentry->d_parent->d_inode->i_version;
650 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
651 dentry->d_op = sb->s_root->d_op;
652 dentry = d_splice_alias(inode, dentry);
653 if (dentry) {
654 dentry->d_op = sb->s_root->d_op;
655 dentry->d_time = dentry->d_parent->d_inode->i_version;
657 #else
658 dentry = d_splice_alias(inode, dentry);
659 if (dentry)
660 dentry->d_time = dentry->d_parent->d_inode->i_version;
661 #endif
662 PRINTK("exfat_lookup exited 2\n");
663 return dentry;
665 error:
666 __unlock_super(sb);
667 PRINTK("exfat_lookup exited 3\n");
668 return ERR_PTR(err);
671 static int exfat_unlink(struct inode *dir, struct dentry *dentry)
673 struct inode *inode = dentry->d_inode;
674 struct super_block *sb = dir->i_sb;
675 struct timespec ts;
676 int err;
678 __lock_super(sb);
680 PRINTK("exfat_unlink entered\n");
682 ts = CURRENT_TIME_SEC;
684 EXFAT_I(inode)->fid.size = i_size_read(inode);
686 err = FsRemoveFile(dir, &(EXFAT_I(inode)->fid));
687 if (err) {
688 if (err == FFS_PERMISSIONERR)
689 err = -EPERM;
690 else
691 err = -EIO;
692 goto out;
694 dir->i_version++;
695 dir->i_mtime = dir->i_atime = ts;
696 if (IS_DIRSYNC(dir))
697 (void) exfat_sync_inode(dir);
698 else
699 mark_inode_dirty(dir);
701 clear_nlink(inode);
702 inode->i_mtime = inode->i_atime = ts;
703 exfat_detach(inode);
704 remove_inode_hash(inode);
706 out:
707 __unlock_super(sb);
708 PRINTK("exfat_unlink exited\n");
709 return err;
712 static int exfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
714 struct super_block *sb = dir->i_sb;
715 struct inode *inode;
716 struct timespec ts;
717 FILE_ID_T fid;
718 loff_t i_pos;
719 int err;
720 UINT64 len = (UINT64) strlen(target);
721 UINT64 ret;
723 __lock_super(sb);
725 PRINTK("exfat_symlink entered\n");
727 ts = CURRENT_TIME_SEC;
729 err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_SYMLINK, &fid);
730 if (err) {
731 if (err == FFS_INVALIDPATH)
732 err = -EINVAL;
733 else if (err == FFS_FILEEXIST)
734 err = -EEXIST;
735 else if (err == FFS_FULL)
736 err = -ENOSPC;
737 else
738 err = -EIO;
739 goto out;
742 err = FsWriteFile(dir, &fid, (char *) target, len, &ret);
744 if (err) {
745 FsRemoveFile(dir, &fid);
747 if (err == FFS_FULL)
748 err = -ENOSPC;
749 else
750 err = -EIO;
751 goto out;
754 dir->i_version++;
755 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
756 if (IS_DIRSYNC(dir))
757 (void) exfat_sync_inode(dir);
758 else
759 mark_inode_dirty(dir);
761 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
763 inode = exfat_build_inode(sb, &fid, i_pos);
764 if (IS_ERR(inode)) {
765 err = PTR_ERR(inode);
766 goto out;
768 inode->i_version++;
769 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
770 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
772 EXFAT_I(inode)->target = MALLOC(len+1);
773 if (!EXFAT_I(inode)->target) {
774 err = -ENOMEM;
775 goto out;
777 MEMCPY(EXFAT_I(inode)->target, target, len+1);
779 dentry->d_time = dentry->d_parent->d_inode->i_version;
780 d_instantiate(dentry, inode);
782 out:
783 __unlock_super(sb);
784 PRINTK("exfat_symlink exited\n");
785 return err;
788 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
789 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
790 #else
791 static int exfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
792 #endif
794 struct super_block *sb = dir->i_sb;
795 struct inode *inode;
796 struct timespec ts;
797 FILE_ID_T fid;
798 loff_t i_pos;
799 int err;
801 __lock_super(sb);
803 PRINTK("exfat_mkdir entered\n");
805 ts = CURRENT_TIME_SEC;
807 err = FsCreateDir(dir, (UINT8 *) dentry->d_name.name, &fid);
808 if (err) {
809 if (err == FFS_INVALIDPATH)
810 err = -EINVAL;
811 else if (err == FFS_FILEEXIST)
812 err = -EEXIST;
813 else if (err == FFS_FULL)
814 err = -ENOSPC;
815 else if (err == FFS_NAMETOOLONG)
816 err = -ENAMETOOLONG;
817 else
818 err = -EIO;
819 goto out;
821 dir->i_version++;
822 dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
823 if (IS_DIRSYNC(dir))
824 (void) exfat_sync_inode(dir);
825 else
826 mark_inode_dirty(dir);
827 inc_nlink(dir);
829 i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
831 inode = exfat_build_inode(sb, &fid, i_pos);
832 if (IS_ERR(inode)) {
833 err = PTR_ERR(inode);
834 goto out;
836 inode->i_version++;
837 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
838 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
840 dentry->d_time = dentry->d_parent->d_inode->i_version;
841 d_instantiate(dentry, inode);
843 out:
844 __unlock_super(sb);
845 PRINTK("exfat_mkdir exited\n");
846 return err;
849 static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
851 struct inode *inode = dentry->d_inode;
852 struct super_block *sb = dir->i_sb;
853 struct timespec ts;
854 int err;
856 __lock_super(sb);
858 PRINTK("exfat_rmdir entered\n");
860 ts = CURRENT_TIME_SEC;
862 EXFAT_I(inode)->fid.size = i_size_read(inode);
864 err = FsRemoveDir(dir, &(EXFAT_I(inode)->fid));
865 if (err) {
866 if (err == FFS_INVALIDPATH)
867 err = -EINVAL;
868 else if (err == FFS_FILEEXIST)
869 err = -ENOTEMPTY;
870 else if (err == FFS_NOTFOUND)
871 err = -ENOENT;
872 else if (err == FFS_DIRBUSY)
873 err = -EBUSY;
874 else
875 err = -EIO;
876 goto out;
878 dir->i_version++;
879 dir->i_mtime = dir->i_atime = ts;
880 if (IS_DIRSYNC(dir))
881 (void) exfat_sync_inode(dir);
882 else
883 mark_inode_dirty(dir);
884 drop_nlink(dir);
886 clear_nlink(inode);
887 inode->i_mtime = inode->i_atime = ts;
888 exfat_detach(inode);
889 remove_inode_hash(inode);
891 out:
892 __unlock_super(sb);
893 PRINTK("exfat_rmdir exited\n");
894 return err;
897 static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
898 struct inode *new_dir, struct dentry *new_dentry)
900 struct inode *old_inode, *new_inode;
901 struct super_block *sb = old_dir->i_sb;
902 struct timespec ts;
903 loff_t i_pos;
904 int err;
906 __lock_super(sb);
908 PRINTK("exfat_rename entered\n");
910 old_inode = old_dentry->d_inode;
911 new_inode = new_dentry->d_inode;
913 ts = CURRENT_TIME_SEC;
915 EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
917 err = FsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir, new_dentry);
918 if (err) {
919 if (err == FFS_PERMISSIONERR)
920 err = -EPERM;
921 else if (err == FFS_INVALIDPATH)
922 err = -EINVAL;
923 else if (err == FFS_FILEEXIST)
924 err = -EEXIST;
925 else if (err == FFS_NOTFOUND)
926 err = -ENOENT;
927 else if (err == FFS_FULL)
928 err = -ENOSPC;
929 else
930 err = -EIO;
931 goto out;
933 new_dir->i_version++;
934 new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = ts;
935 if (IS_DIRSYNC(new_dir))
936 (void) exfat_sync_inode(new_dir);
937 else
938 mark_inode_dirty(new_dir);
940 i_pos = ((loff_t) EXFAT_I(old_inode)->fid.dir.dir << 32) |
941 (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
943 exfat_detach(old_inode);
944 exfat_attach(old_inode, i_pos);
945 if (IS_DIRSYNC(new_dir))
946 (void) exfat_sync_inode(old_inode);
947 else
948 mark_inode_dirty(old_inode);
950 if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
951 drop_nlink(old_dir);
952 if (!new_inode) inc_nlink(new_dir);
955 old_dir->i_version++;
956 old_dir->i_ctime = old_dir->i_mtime = ts;
957 if (IS_DIRSYNC(old_dir))
958 (void) exfat_sync_inode(old_dir);
959 else
960 mark_inode_dirty(old_dir);
962 if (new_inode) {
963 exfat_detach(new_inode);
964 drop_nlink(new_inode);
965 if (S_ISDIR(new_inode->i_mode))
966 drop_nlink(new_inode);
967 new_inode->i_ctime = ts;
970 out:
971 __unlock_super(sb);
972 PRINTK("exfat_rename exited\n");
973 return err;
976 static int exfat_cont_expand(struct inode *inode, loff_t size)
978 struct address_space *mapping = inode->i_mapping;
979 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
980 int err, err2;
982 if ((err = generic_cont_expand_simple(inode, size)) != 0)
983 return err;
985 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
986 mark_inode_dirty(inode);
988 if (IS_SYNC(inode)) {
989 err = filemap_fdatawrite_range(mapping, start, start + count - 1);
990 err2 = sync_mapping_buffers(mapping);
991 err = (err)?(err):(err2);
992 err2 = write_inode_now(inode, 1);
993 err = (err)?(err):(err2);
994 if (!err) {
995 err = filemap_fdatawait_range(mapping, start, start + count - 1);
998 return err;
1001 static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
1003 mode_t allow_utime = sbi->options.allow_utime;
1005 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,5,0)
1006 if (!uid_eq(current_fsuid(), inode->i_uid))
1007 #else
1008 if (current_fsuid() != inode->i_uid)
1009 #endif
1011 if (in_group_p(inode->i_gid))
1012 allow_utime >>= 3;
1013 if (allow_utime & MAY_WRITE)
1014 return 1;
1017 /* use a default check */
1018 return 0;
1021 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
1022 struct inode *inode, umode_t *mode_ptr)
1024 mode_t i_mode, mask, perm;
1026 i_mode = inode->i_mode;
1028 if (S_ISREG(i_mode) || S_ISLNK(i_mode))
1029 mask = sbi->options.fs_fmask;
1030 else
1031 mask = sbi->options.fs_dmask;
1033 perm = *mode_ptr & ~(S_IFMT | mask);
1035 /* Of the r and x bits, all (subject to umask) must be present.*/
1036 if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO|S_IXUGO)))
1037 return -EPERM;
1039 if (exfat_mode_can_hold_ro(inode)) {
1040 /* Of the w bits, either all (subject to umask) or none must be present. */
1041 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
1042 return -EPERM;
1043 } else {
1044 /* If exfat_mode_can_hold_ro(inode) is false, can't change w bits. */
1045 if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
1046 return -EPERM;
1049 *mode_ptr &= S_IFMT | perm;
1051 return 0;
1054 static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
1057 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
1058 struct inode *inode = dentry->d_inode;
1059 unsigned int ia_valid;
1060 int error;
1061 loff_t old_size;
1063 PRINTK("exfat_setattr entered\n");
1065 if ((attr->ia_valid & ATTR_SIZE)
1066 && (attr->ia_size > i_size_read(inode))) {
1067 error = exfat_cont_expand(inode, attr->ia_size);
1068 if (error || attr->ia_valid == ATTR_SIZE)
1069 return error;
1070 attr->ia_valid &= ~ATTR_SIZE;
1073 ia_valid = attr->ia_valid;
1075 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
1076 && exfat_allow_set_time(sbi, inode)) {
1077 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET);
1080 error = inode_change_ok(inode, attr);
1081 attr->ia_valid = ia_valid;
1082 if (error) {
1083 return error;
1086 if (((attr->ia_valid & ATTR_UID) &&
1087 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,5,0)
1088 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
1089 ((attr->ia_valid & ATTR_GID) &&
1090 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
1091 #else
1092 (attr->ia_uid != sbi->options.fs_uid)) ||
1093 ((attr->ia_valid & ATTR_GID) &&
1094 (attr->ia_gid != sbi->options.fs_gid)) ||
1095 #endif
1096 ((attr->ia_valid & ATTR_MODE) &&
1097 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | S_IRWXUGO)))) {
1098 return -EPERM;
1102 * We don't return -EPERM here. Yes, strange, but this is too
1103 * old behavior.
1105 if (attr->ia_valid & ATTR_MODE) {
1106 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
1107 attr->ia_valid &= ~ATTR_MODE;
1110 EXFAT_I(inode)->fid.size = i_size_read(inode);
1112 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1113 if (attr->ia_valid)
1114 error = inode_setattr(inode, attr);
1115 #else
1116 if (attr->ia_valid & ATTR_SIZE) {
1117 old_size = i_size_read(inode);
1118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1119 down_write(&EXFAT_I(inode)->truncate_lock);
1120 truncate_setsize(inode, attr->ia_size);
1121 _exfat_truncate(inode, old_size);
1122 up_write(&EXFAT_I(inode)->truncate_lock);
1123 #else
1124 truncate_setsize(inode, attr->ia_size);
1125 _exfat_truncate(inode, old_size);
1126 #endif
1128 setattr_copy(inode, attr);
1129 mark_inode_dirty(inode);
1130 #endif
1132 PRINTK("exfat_setattr exited\n");
1133 return error;
1136 static int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1138 struct inode *inode = dentry->d_inode;
1140 PRINTK("exfat_getattr entered\n");
1142 generic_fillattr(inode, stat);
1143 stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
1145 PRINTK("exfat_getattr exited\n");
1146 return 0;
1149 const struct inode_operations exfat_dir_inode_operations = {
1150 .create = exfat_create,
1151 .lookup = exfat_lookup,
1152 .unlink = exfat_unlink,
1153 .symlink = exfat_symlink,
1154 .mkdir = exfat_mkdir,
1155 .rmdir = exfat_rmdir,
1156 .rename = exfat_rename,
1157 .setattr = exfat_setattr,
1158 .getattr = exfat_getattr,
1161 /*======================================================================*/
1162 /* File Operations */
1163 /*======================================================================*/
1165 static void *exfat_follow_link(struct dentry *dentry, struct nameidata *nd)
1167 struct exfat_inode_info *ei = EXFAT_I(dentry->d_inode);
1168 nd_set_link(nd, (char *)(ei->target));
1169 return NULL;
1172 const struct inode_operations exfat_symlink_inode_operations = {
1173 .readlink = generic_readlink,
1174 .follow_link = exfat_follow_link,
1177 static int exfat_file_release(struct inode *inode, struct file *filp)
1179 struct super_block *sb = inode->i_sb;
1181 EXFAT_I(inode)->fid.size = i_size_read(inode);
1182 FsSyncVol(sb, 0);
1183 return 0;
1186 const struct file_operations exfat_file_operations = {
1187 .llseek = generic_file_llseek,
1188 .read = do_sync_read,
1189 .write = do_sync_write,
1190 .aio_read = generic_file_aio_read,
1191 .aio_write = generic_file_aio_write,
1192 .mmap = generic_file_mmap,
1193 .release = exfat_file_release,
1194 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1195 .ioctl = exfat_generic_ioctl,
1196 .fsync = exfat_file_fsync,
1197 #else
1198 .unlocked_ioctl = exfat_generic_ioctl,
1199 .fsync = generic_file_fsync,
1200 #endif
1201 .splice_read = generic_file_splice_read,
1204 static void _exfat_truncate(struct inode *inode, loff_t old_size)
1206 struct super_block *sb = inode->i_sb;
1207 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1208 FS_INFO_T *p_fs = &(sbi->fs_info);
1209 int err;
1211 __lock_super(sb);
1214 * This protects against truncating a file bigger than it was then
1215 * trying to write into the hole.
1217 if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
1218 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1220 if (EXFAT_I(inode)->fid.start_clu == 0) goto out;
1222 err = FsTruncateFile(inode, old_size, i_size_read(inode));
1223 if (err) goto out;
1225 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
1226 if (IS_DIRSYNC(inode))
1227 (void) exfat_sync_inode(inode);
1228 else
1229 mark_inode_dirty(inode);
1231 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
1232 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
1233 out:
1234 __unlock_super(sb);
1237 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
1238 static void exfat_truncate(struct inode *inode)
1240 _exfat_truncate(inode, i_size_read(inode));
1242 #endif
1244 const struct inode_operations exfat_file_inode_operations = {
1245 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,36)
1246 .truncate = exfat_truncate,
1247 #endif
1248 .setattr = exfat_setattr,
1249 .getattr = exfat_getattr,
1252 /*======================================================================*/
1253 /* Address Space Operations */
1254 /*======================================================================*/
1256 static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
1257 unsigned long *mapped_blocks, int *create)
1259 struct super_block *sb = inode->i_sb;
1260 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1261 FS_INFO_T *p_fs = &(sbi->fs_info);
1262 BD_INFO_T *p_bd = &(sbi->bd_info);
1263 const unsigned long blocksize = sb->s_blocksize;
1264 const unsigned char blocksize_bits = sb->s_blocksize_bits;
1265 sector_t last_block;
1266 int err, clu_offset, sec_offset;
1267 unsigned int cluster;
1269 *phys = 0;
1270 *mapped_blocks = 0;
1272 if ((p_fs->vol_type == FAT12) || (p_fs->vol_type == FAT16)) {
1273 if (inode->i_ino == EXFAT_ROOT_INO) {
1274 if (sector < (p_fs->dentries_in_root >> (p_bd->sector_size_bits-DENTRY_SIZE_BITS))) {
1275 *phys = sector + p_fs->root_start_sector;
1276 *mapped_blocks = 1;
1278 return 0;
1282 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
1283 if (sector >= last_block) {
1284 if (*create == 0) return 0;
1285 } else {
1286 *create = 0;
1289 clu_offset = sector >> p_fs->sectors_per_clu_bits; /* cluster offset */
1290 sec_offset = sector & (p_fs->sectors_per_clu - 1); /* sector offset in cluster */
1292 EXFAT_I(inode)->fid.size = i_size_read(inode);
1294 err = FsMapCluster(inode, clu_offset, &cluster);
1296 if (err) {
1297 if (err == FFS_FULL)
1298 return -ENOSPC;
1299 else
1300 return -EIO;
1301 } else if (cluster != CLUSTER_32(~0)) {
1302 *phys = START_SECTOR(cluster) + sec_offset;
1303 *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
1306 return 0;
1309 static int exfat_get_block(struct inode *inode, sector_t iblock,
1310 struct buffer_head *bh_result, int create)
1312 struct super_block *sb = inode->i_sb;
1313 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
1314 int err;
1315 unsigned long mapped_blocks;
1316 sector_t phys;
1318 __lock_super(sb);
1320 err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
1321 if (err) {
1322 __unlock_super(sb);
1323 return err;
1326 if (phys) {
1327 max_blocks = min(mapped_blocks, max_blocks);
1328 if (create) {
1329 EXFAT_I(inode)->mmu_private += max_blocks << sb->s_blocksize_bits;
1330 set_buffer_new(bh_result);
1332 map_bh(bh_result, sb, phys);
1335 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
1336 __unlock_super(sb);
1338 return 0;
1341 static int exfat_readpage(struct file *file, struct page *page)
1343 int ret;
1344 ret = mpage_readpage(page, exfat_get_block);
1345 return ret;
1348 static int exfat_readpages(struct file *file, struct address_space *mapping,
1349 struct list_head *pages, unsigned nr_pages)
1351 int ret;
1352 ret = mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
1353 return ret;
1356 static int exfat_writepage(struct page *page, struct writeback_control *wbc)
1358 int ret;
1359 ret = block_write_full_page(page, exfat_get_block, wbc);
1360 return ret;
1363 static int exfat_writepages(struct address_space *mapping,
1364 struct writeback_control *wbc)
1366 int ret;
1367 ret = mpage_writepages(mapping, wbc, exfat_get_block);
1368 return ret;
1371 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1372 static void exfat_write_failed(struct address_space *mapping, loff_t to)
1374 struct inode *inode = mapping->host;
1375 if (to > i_size_read(inode)) {
1376 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,12,0)
1377 truncate_pagecache(inode, i_size_read(inode));
1378 #else
1379 truncate_pagecache(inode, to, i_size_read(inode));
1380 #endif
1381 EXFAT_I(inode)->fid.size = i_size_read(inode);
1382 _exfat_truncate(inode, i_size_read(inode));
1385 #endif
1388 static int exfat_write_begin(struct file *file, struct address_space *mapping,
1389 loff_t pos, unsigned len, unsigned flags,
1390 struct page **pagep, void **fsdata)
1392 int ret;
1393 *pagep = NULL;
1394 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1395 exfat_get_block,
1396 &EXFAT_I(mapping->host)->mmu_private);
1398 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1399 if (ret < 0)
1400 exfat_write_failed(mapping, pos+len);
1401 #endif
1402 return ret;
1405 static int exfat_write_end(struct file *file, struct address_space *mapping,
1406 loff_t pos, unsigned len, unsigned copied,
1407 struct page *pagep, void *fsdata)
1409 struct inode *inode = mapping->host;
1410 FILE_ID_T *fid = &(EXFAT_I(inode)->fid);
1411 int err;
1413 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
1415 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1416 if (err < len)
1417 exfat_write_failed(mapping, pos+len);
1418 #endif
1420 if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
1421 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1422 fid->attr |= ATTR_ARCHIVE;
1423 mark_inode_dirty(inode);
1425 return err;
1428 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
1429 static ssize_t exfat_direct_IO(int rw, struct kiocb *iocb, struct iov_iter *iter,
1430 loff_t offset)
1431 #else
1432 static ssize_t exfat_direct_IO(int rw, struct kiocb *iocb,
1433 const struct iovec *iov,
1434 loff_t offset, unsigned long nr_segs)
1435 #endif
1437 struct inode *inode = iocb->ki_filp->f_mapping->host;
1438 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1439 struct address_space *mapping = iocb->ki_filp->f_mapping;
1440 #endif
1441 ssize_t ret;
1443 if (rw == WRITE) {
1444 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
1445 if (EXFAT_I(inode)->mmu_private < (offset + iov_iter_count(iter)))
1446 #else
1447 if (EXFAT_I(inode)->mmu_private < (offset + iov_length(iov, nr_segs)))
1448 #endif
1449 return 0;
1451 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,00)
1452 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
1453 ret = blockdev_direct_IO(rw, iocb, inode, iter,
1454 offset, exfat_get_block);
1455 #else
1456 ret = blockdev_direct_IO(rw, iocb, inode, iov,
1457 offset, nr_segs, exfat_get_block);
1458 #endif
1459 #else
1460 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1461 offset, nr_segs, exfat_get_block, NULL);
1462 #endif
1464 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,34)
1465 if ((ret < 0) && (rw & WRITE))
1466 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
1467 exfat_write_failed(mapping, offset+iov_iter_count(iter));
1468 #else
1469 exfat_write_failed(mapping, offset+iov_length(iov, nr_segs));
1470 #endif
1471 #endif
1472 return ret;
1475 static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
1477 sector_t blocknr;
1479 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
1480 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1481 down_read(&EXFAT_I(mapping->host)->truncate_lock);
1482 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
1483 up_read(&EXFAT_I(mapping->host)->truncate_lock);
1484 #else
1485 down_read(&EXFAT_I(mapping->host)->i_alloc_sem);
1486 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
1487 up_read(&EXFAT_I(mapping->host)->i_alloc_sem);
1488 #endif
1490 return blocknr;
1493 const struct address_space_operations exfat_aops = {
1494 .readpage = exfat_readpage,
1495 .readpages = exfat_readpages,
1496 .writepage = exfat_writepage,
1497 .writepages = exfat_writepages,
1498 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
1499 .sync_page = block_sync_page,
1500 #endif
1501 .write_begin = exfat_write_begin,
1502 .write_end = exfat_write_end,
1503 .direct_IO = exfat_direct_IO,
1504 .bmap = _exfat_bmap
1507 /*======================================================================*/
1508 /* Super Operations */
1509 /*======================================================================*/
1511 static inline unsigned long exfat_hash(loff_t i_pos)
1513 return hash_32(i_pos, EXFAT_HASH_BITS);
1516 static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) {
1517 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1518 struct exfat_inode_info *info;
1519 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
1520 struct inode *inode = NULL;
1521 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0)
1522 struct hlist_node *node;
1524 spin_lock(&sbi->inode_hash_lock);
1525 hlist_for_each_entry(info, node, head, i_hash_fat) {
1526 #else
1527 spin_lock(&sbi->inode_hash_lock);
1528 hlist_for_each_entry(info, head, i_hash_fat) {
1529 #endif
1530 CHECK_ERR(info->vfs_inode.i_sb != sb);
1532 if (i_pos != info->i_pos)
1533 continue;
1534 inode = igrab(&info->vfs_inode);
1535 if (inode)
1536 break;
1538 spin_unlock(&sbi->inode_hash_lock);
1539 return inode;
1542 static void exfat_attach(struct inode *inode, loff_t i_pos)
1544 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1545 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
1547 spin_lock(&sbi->inode_hash_lock);
1548 EXFAT_I(inode)->i_pos = i_pos;
1549 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
1550 spin_unlock(&sbi->inode_hash_lock);
1553 static void exfat_detach(struct inode *inode)
1555 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1557 spin_lock(&sbi->inode_hash_lock);
1558 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
1559 EXFAT_I(inode)->i_pos = 0;
1560 spin_unlock(&sbi->inode_hash_lock);
1563 /* doesn't deal with root inode */
1564 static int exfat_fill_inode(struct inode *inode, FILE_ID_T *fid)
1566 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
1567 FS_INFO_T *p_fs = &(sbi->fs_info);
1568 DIR_ENTRY_T info;
1570 memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(FILE_ID_T));
1572 FsReadStat(inode, &info);
1574 EXFAT_I(inode)->i_pos = 0;
1575 EXFAT_I(inode)->target = NULL;
1576 inode->i_uid = sbi->options.fs_uid;
1577 inode->i_gid = sbi->options.fs_gid;
1578 inode->i_version++;
1579 inode->i_generation = get_seconds();
1581 if (info.Attr & ATTR_SUBDIR) { /* directory */
1582 inode->i_generation &= ~1;
1583 inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
1584 inode->i_op = &exfat_dir_inode_operations;
1585 inode->i_fop = &exfat_dir_operations;
1587 i_size_write(inode, info.Size);
1588 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1589 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,00)
1590 set_nlink(inode,info.NumSubdirs);
1591 #else
1592 inode->i_nlink = info.NumSubdirs;
1593 #endif
1594 } else if (info.Attr & ATTR_SYMLINK) { /* symbolic link */
1595 inode->i_generation |= 1;
1596 inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
1597 inode->i_op = &exfat_symlink_inode_operations;
1599 i_size_write(inode, info.Size);
1600 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1601 } else { /* regular file */
1602 inode->i_generation |= 1;
1603 inode->i_mode = exfat_make_mode(sbi, info.Attr, S_IRWXUGO);
1604 inode->i_op = &exfat_file_inode_operations;
1605 inode->i_fop = &exfat_file_operations;
1606 inode->i_mapping->a_ops = &exfat_aops;
1607 inode->i_mapping->nrpages = 0;
1609 i_size_write(inode, info.Size);
1610 EXFAT_I(inode)->mmu_private = i_size_read(inode);
1612 exfat_save_attr(inode, info.Attr);
1614 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
1615 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
1617 exfat_time_fat2unix(sbi, &inode->i_mtime, &info.ModifyTimestamp);
1618 exfat_time_fat2unix(sbi, &inode->i_ctime, &info.CreateTimestamp);
1619 exfat_time_fat2unix(sbi, &inode->i_atime, &info.AccessTimestamp);
1621 return 0;
1624 static struct inode *exfat_build_inode(struct super_block *sb,
1625 FILE_ID_T *fid, loff_t i_pos) {
1626 struct inode *inode;
1627 int err;
1629 inode = exfat_iget(sb, i_pos);
1630 if (inode)
1631 goto out;
1632 inode = new_inode(sb);
1633 if (!inode) {
1634 inode = ERR_PTR(-ENOMEM);
1635 goto out;
1637 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
1638 inode->i_version = 1;
1639 err = exfat_fill_inode(inode, fid);
1640 if (err) {
1641 iput(inode);
1642 inode = ERR_PTR(err);
1643 goto out;
1645 exfat_attach(inode, i_pos);
1646 insert_inode_hash(inode);
1647 out:
1648 return inode;
1651 static int exfat_sync_inode(struct inode *inode)
1653 return exfat_write_inode(inode, NULL);
1656 static struct inode *exfat_alloc_inode(struct super_block *sb) {
1657 struct exfat_inode_info *ei;
1659 ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
1660 if (!ei)
1661 return NULL;
1663 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1664 init_rwsem(&ei->truncate_lock);
1665 #endif
1667 return &ei->vfs_inode;
1670 static void exfat_destroy_inode(struct inode *inode)
1672 FREE(EXFAT_I(inode)->target);
1673 EXFAT_I(inode)->target = NULL;
1675 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
1678 static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
1680 struct super_block *sb = inode->i_sb;
1681 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1682 DIR_ENTRY_T info;
1684 if (inode->i_ino == EXFAT_ROOT_INO)
1685 return 0;
1687 info.Attr = exfat_make_attr(inode);
1688 info.Size = i_size_read(inode);
1690 exfat_time_unix2fat(sbi, &inode->i_mtime, &info.ModifyTimestamp);
1691 exfat_time_unix2fat(sbi, &inode->i_ctime, &info.CreateTimestamp);
1692 exfat_time_unix2fat(sbi, &inode->i_atime, &info.AccessTimestamp);
1694 FsWriteStat(inode, &info);
1696 return 0;
1699 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1700 static void exfat_delete_inode(struct inode *inode)
1702 truncate_inode_pages(&inode->i_data, 0);
1703 clear_inode(inode);
1706 static void exfat_clear_inode(struct inode *inode)
1708 exfat_detach(inode);
1709 remove_inode_hash(inode);
1711 #else
1712 static void exfat_evict_inode(struct inode *inode)
1714 truncate_inode_pages(&inode->i_data, 0);
1716 if (!inode->i_nlink)
1717 i_size_write(inode, 0);
1718 invalidate_inode_buffers(inode);
1719 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,5,0)
1720 end_writeback(inode);
1721 #else
1722 clear_inode(inode);
1723 #endif
1724 exfat_detach(inode);
1726 remove_inode_hash(inode);
1728 #endif
1730 static void exfat_free_super(struct exfat_sb_info *sbi)
1732 if (sbi->nls_disk)
1733 unload_nls(sbi->nls_disk);
1734 if (sbi->nls_io)
1735 unload_nls(sbi->nls_io);
1736 if (sbi->options.iocharset != exfat_default_iocharset)
1737 kfree(sbi->options.iocharset);
1738 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
1739 /* mutex_init is in exfat_fill_super function. only for 3.7+ */
1740 mutex_destroy(&sbi->s_lock);
1741 #endif
1742 kfree(sbi);
1745 static void exfat_put_super(struct super_block *sb)
1747 struct exfat_sb_info *sbi = EXFAT_SB(sb);
1748 if (__is_sb_dirty(sb))
1749 exfat_write_super(sb);
1751 FsUmountVol(sb);
1753 sb->s_fs_info = NULL;
1754 exfat_free_super(sbi);
1757 static void exfat_write_super(struct super_block *sb)
1759 __lock_super(sb);
1761 __set_sb_clean(sb);
1763 if (!(sb->s_flags & MS_RDONLY))
1764 FsSyncVol(sb, 1);
1766 __unlock_super(sb);
1769 static int exfat_sync_fs(struct super_block *sb, int wait)
1771 int err = 0;
1773 if (__is_sb_dirty(sb)) {
1774 __lock_super(sb);
1775 __set_sb_clean(sb);
1776 err = FsSyncVol(sb, 1);
1777 __unlock_super(sb);
1780 return err;
1783 static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
1785 struct super_block *sb = dentry->d_sb;
1786 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1787 FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
1788 VOL_INFO_T info;
1790 if (p_fs->used_clusters == (UINT32) ~0) {
1791 if (FFS_MEDIAERR == FsGetVolInfo(sb, &info))
1792 return -EIO;
1794 } else {
1795 info.FatType = p_fs->vol_type;
1796 info.ClusterSize = p_fs->cluster_size;
1797 info.NumClusters = p_fs->num_clusters - 2;
1798 info.UsedClusters = p_fs->used_clusters;
1799 info.FreeClusters = info.NumClusters - info.UsedClusters;
1801 if (p_fs->dev_ejected)
1802 return -EIO;
1805 buf->f_type = sb->s_magic;
1806 buf->f_bsize = info.ClusterSize;
1807 buf->f_blocks = info.NumClusters;
1808 buf->f_bfree = info.FreeClusters;
1809 buf->f_bavail = info.FreeClusters;
1810 buf->f_fsid.val[0] = (u32)id;
1811 buf->f_fsid.val[1] = (u32)(id >> 32);
1812 buf->f_namelen = 260;
1814 return 0;
1817 static int exfat_remount(struct super_block *sb, int *flags, char *data)
1819 *flags |= MS_NODIRATIME;
1820 return 0;
1823 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
1824 static int exfat_show_options(struct seq_file *m, struct dentry *root)
1826 struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
1827 #else
1828 static int exfat_show_options(struct seq_file *m, struct vfsmount *mnt)
1830 struct exfat_sb_info *sbi = EXFAT_SB(mnt->mnt_sb);
1831 #endif
1832 struct exfat_mount_options *opts = &sbi->options;
1833 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,5,0)
1834 if (__kuid_val(opts->fs_uid))
1835 seq_printf(m, ",uid=%u", __kuid_val(opts->fs_uid));
1836 if (__kgid_val(opts->fs_gid))
1837 seq_printf(m, ",gid=%u", __kgid_val(opts->fs_gid));
1838 #else
1839 if (opts->fs_uid != 0)
1840 seq_printf(m, ",uid=%u", opts->fs_uid);
1841 if (opts->fs_gid != 0)
1842 seq_printf(m, ",gid=%u", opts->fs_gid);
1843 #endif
1844 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
1845 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
1846 if (opts->allow_utime)
1847 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
1848 if (sbi->nls_disk)
1849 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
1850 if (sbi->nls_io)
1851 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
1852 seq_printf(m, ",namecase=%u", opts->casesensitive);
1853 if (opts->errors == EXFAT_ERRORS_CONT)
1854 seq_puts(m, ",errors=continue");
1855 else if (opts->errors == EXFAT_ERRORS_PANIC)
1856 seq_puts(m, ",errors=panic");
1857 else
1858 seq_puts(m, ",errors=remount-ro");
1859 #if EXFAT_CONFIG_DISCARD
1860 if (opts->discard)
1861 seq_printf(m, ",discard");
1862 #endif
1863 return 0;
1866 const struct super_operations exfat_sops = {
1867 .alloc_inode = exfat_alloc_inode,
1868 .destroy_inode = exfat_destroy_inode,
1869 .write_inode = exfat_write_inode,
1870 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
1871 .delete_inode = exfat_delete_inode,
1872 .clear_inode = exfat_clear_inode,
1873 #else
1874 .evict_inode = exfat_evict_inode,
1875 #endif
1876 .put_super = exfat_put_super,
1877 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
1878 .write_super = exfat_write_super,
1879 #endif
1880 .sync_fs = exfat_sync_fs,
1881 .statfs = exfat_statfs,
1882 .remount_fs = exfat_remount,
1883 .show_options = exfat_show_options,
1886 /*======================================================================*/
1887 /* Super Block Read Operations */
1888 /*======================================================================*/
1890 enum {
1891 Opt_uid,
1892 Opt_gid,
1893 Opt_umask,
1894 Opt_dmask,
1895 Opt_fmask,
1896 Opt_allow_utime,
1897 Opt_codepage,
1898 Opt_charset,
1899 Opt_namecase,
1900 Opt_debug,
1901 Opt_err_cont,
1902 Opt_err_panic,
1903 Opt_err_ro,
1904 Opt_err,
1905 #if EXFAT_CONFIG_DISCARD
1906 Opt_discard,
1907 #endif /* EXFAT_CONFIG_DISCARD */
1910 static const match_table_t exfat_tokens = {
1911 {Opt_uid, "uid=%u"},
1912 {Opt_gid, "gid=%u"},
1913 {Opt_umask, "umask=%o"},
1914 {Opt_dmask, "dmask=%o"},
1915 {Opt_fmask, "fmask=%o"},
1916 {Opt_allow_utime, "allow_utime=%o"},
1917 {Opt_codepage, "codepage=%u"},
1918 {Opt_charset, "iocharset=%s"},
1919 {Opt_namecase, "namecase=%u"},
1920 {Opt_debug, "debug"},
1921 {Opt_err_cont, "errors=continue"},
1922 {Opt_err_panic, "errors=panic"},
1923 {Opt_err_ro, "errors=remount-ro"},
1924 #if EXFAT_CONFIG_DISCARD
1925 {Opt_discard, "discard"},
1926 #endif /* EXFAT_CONFIG_DISCARD */
1927 {Opt_err, NULL}
1930 static int parse_options(char *options, int silent, int *debug,
1931 struct exfat_mount_options *opts)
1933 char *p;
1934 substring_t args[MAX_OPT_ARGS];
1935 int option;
1936 char *iocharset;
1938 opts->fs_uid = current_uid();
1939 opts->fs_gid = current_gid();
1940 opts->fs_fmask = opts->fs_dmask = current->fs->umask;
1941 opts->allow_utime = (unsigned short) -1;
1942 opts->codepage = exfat_default_codepage;
1943 opts->iocharset = exfat_default_iocharset;
1944 opts->casesensitive = 0;
1945 opts->errors = EXFAT_ERRORS_RO;
1946 #if EXFAT_CONFIG_DISCARD
1947 opts->discard = 0;
1948 #endif
1949 *debug = 0;
1951 if (!options)
1952 goto out;
1954 while ((p = strsep(&options, ",")) != NULL) {
1955 int token;
1956 if (!*p)
1957 continue;
1959 token = match_token(p, exfat_tokens, args);
1960 switch (token) {
1961 case Opt_uid:
1962 if (match_int(&args[0], &option))
1963 return 0;
1964 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,5,0)
1965 opts->fs_uid = KUIDT_INIT(option);
1966 #else
1967 opts->fs_uid = option;
1968 #endif
1969 break;
1970 case Opt_gid:
1971 if (match_int(&args[0], &option))
1972 return 0;
1973 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,5,0)
1974 opts->fs_gid = KGIDT_INIT(option);
1975 #else
1976 opts->fs_gid = option;
1977 #endif
1978 break;
1979 case Opt_umask:
1980 case Opt_dmask:
1981 case Opt_fmask:
1982 if (match_octal(&args[0], &option))
1983 return 0;
1984 if (token != Opt_dmask)
1985 opts->fs_fmask = option;
1986 if (token != Opt_fmask)
1987 opts->fs_dmask = option;
1988 break;
1989 case Opt_allow_utime:
1990 if (match_octal(&args[0], &option))
1991 return 0;
1992 opts->allow_utime = option & (S_IWGRP | S_IWOTH);
1993 break;
1994 case Opt_codepage:
1995 if (match_int(&args[0], &option))
1996 return 0;
1997 opts->codepage = option;
1998 break;
1999 case Opt_charset:
2000 if (opts->iocharset != exfat_default_iocharset)
2001 kfree(opts->iocharset);
2002 iocharset = match_strdup(&args[0]);
2003 if (!iocharset)
2004 return -ENOMEM;
2005 opts->iocharset = iocharset;
2006 break;
2007 case Opt_namecase:
2008 if (match_int(&args[0], &option))
2009 return 0;
2010 opts->casesensitive = option;
2011 break;
2012 case Opt_err_cont:
2013 opts->errors = EXFAT_ERRORS_CONT;
2014 break;
2015 case Opt_err_panic:
2016 opts->errors = EXFAT_ERRORS_PANIC;
2017 break;
2018 case Opt_err_ro:
2019 opts->errors = EXFAT_ERRORS_RO;
2020 break;
2021 case Opt_debug:
2022 *debug = 1;
2023 break;
2024 #if EXFAT_CONFIG_DISCARD
2025 case Opt_discard:
2026 opts->discard = 1;
2027 break;
2028 #endif /* EXFAT_CONFIG_DISCARD */
2029 default:
2030 if (!silent) {
2031 printk(KERN_ERR "[EXFAT] Unrecognized mount option %s or missing value\n", p);
2033 return -EINVAL;
2037 out:
2038 if (opts->allow_utime == (unsigned short) -1)
2039 opts->allow_utime = ~opts->fs_dmask & (S_IWGRP | S_IWOTH);
2041 return 0;
2044 static void exfat_hash_init(struct super_block *sb)
2046 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2047 int i;
2049 spin_lock_init(&sbi->inode_hash_lock);
2050 for (i = 0; i < EXFAT_HASH_SIZE; i++)
2051 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
2054 static int exfat_read_root(struct inode *inode)
2056 struct super_block *sb = inode->i_sb;
2057 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2058 struct timespec ts;
2059 FS_INFO_T *p_fs = &(sbi->fs_info);
2060 DIR_ENTRY_T info;
2062 ts = CURRENT_TIME_SEC;
2064 EXFAT_I(inode)->fid.dir.dir = p_fs->root_dir;
2065 EXFAT_I(inode)->fid.dir.flags = 0x01;
2066 EXFAT_I(inode)->fid.entry = -1;
2067 EXFAT_I(inode)->fid.start_clu = p_fs->root_dir;
2068 EXFAT_I(inode)->fid.flags = 0x01;
2069 EXFAT_I(inode)->fid.type = TYPE_DIR;
2070 EXFAT_I(inode)->fid.rwoffset = 0;
2071 EXFAT_I(inode)->fid.hint_last_off = -1;
2073 EXFAT_I(inode)->target = NULL;
2075 FsReadStat(inode, &info);
2077 inode->i_uid = sbi->options.fs_uid;
2078 inode->i_gid = sbi->options.fs_gid;
2079 inode->i_version++;
2080 inode->i_generation = 0;
2081 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, S_IRWXUGO);
2082 inode->i_op = &exfat_dir_inode_operations;
2083 inode->i_fop = &exfat_dir_operations;
2085 i_size_write(inode, info.Size);
2086 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
2087 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
2088 EXFAT_I(inode)->i_pos = ((loff_t) p_fs->root_dir << 32) | 0xffffffff;
2089 EXFAT_I(inode)->mmu_private = i_size_read(inode);
2091 exfat_save_attr(inode, ATTR_SUBDIR);
2092 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
2093 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,00)
2094 set_nlink(inode,info.NumSubdirs + 2);
2095 #else
2096 inode->i_nlink = info.NumSubdirs + 2;
2097 #endif
2099 return 0;
2102 static int exfat_fill_super(struct super_block *sb, void *data, int silent)
2104 struct inode *root_inode = NULL;
2105 struct exfat_sb_info *sbi;
2106 int debug, ret;
2107 long error;
2108 char buf[50];
2111 * GFP_KERNEL is ok here, because while we do hold the
2112 * supeblock lock, memory pressure can't call back into
2113 * the filesystem, since we're only just about to mount
2114 * it and have no inodes etc active!
2116 sbi = kzalloc(sizeof(struct exfat_sb_info), GFP_KERNEL);
2117 if (!sbi)
2118 return -ENOMEM;
2119 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
2120 mutex_init(&sbi->s_lock);
2121 #endif
2122 sb->s_fs_info = sbi;
2124 sb->s_flags |= MS_NODIRATIME;
2125 sb->s_magic = EXFAT_SUPER_MAGIC;
2126 sb->s_op = &exfat_sops;
2128 error = parse_options(data, silent, &debug, &sbi->options);
2129 if (error)
2130 goto out_fail;
2132 error = -EIO;
2133 sb_min_blocksize(sb, 512);
2134 sb->s_maxbytes = 0x7fffffffffffffffLL; // maximum file size
2136 ret = FsMountVol(sb);
2137 if (ret) {
2138 if (!silent)
2139 printk(KERN_ERR "[EXFAT] FsMountVol failed\n");
2141 goto out_fail;
2144 /* set up enough so that it can read an inode */
2145 exfat_hash_init(sb);
2148 * The low byte of FAT's first entry must have same value with
2149 * media-field. But in real world, too many devices is
2150 * writing wrong value. So, removed that validity check.
2152 * if (FAT_FIRST_ENT(sb, media) != first)
2155 if (sbi->fs_info.vol_type != EXFAT) {
2156 error = -EINVAL;
2157 sprintf(buf, "cp%d", sbi->options.codepage);
2158 sbi->nls_disk = load_nls(buf);
2159 if (!sbi->nls_disk) {
2160 printk(KERN_ERR "[EXFAT] Codepage %s not found\n", buf);
2161 goto out_fail2;
2165 sbi->nls_io = load_nls(sbi->options.iocharset);
2167 error = -ENOMEM;
2168 root_inode = new_inode(sb);
2169 if (!root_inode)
2170 goto out_fail2;
2171 root_inode->i_ino = EXFAT_ROOT_INO;
2172 root_inode->i_version = 1;
2173 error = exfat_read_root(root_inode);
2174 if (error < 0)
2175 goto out_fail2;
2176 error = -ENOMEM;
2177 exfat_attach(root_inode, EXFAT_I(root_inode)->i_pos);
2178 insert_inode_hash(root_inode);
2179 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,00)
2180 sb->s_root = d_make_root(root_inode);
2181 #else
2182 sb->s_root = d_alloc_root(root_inode);
2183 #endif
2184 if (!sb->s_root) {
2185 printk(KERN_ERR "[EXFAT] Getting the root inode failed\n");
2186 goto out_fail2;
2189 return 0;
2191 out_fail2:
2192 FsUmountVol(sb);
2193 out_fail:
2194 if (root_inode)
2195 iput(root_inode);
2196 sb->s_fs_info = NULL;
2197 exfat_free_super(sbi);
2198 return error;
2200 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
2201 static int exfat_get_sb(struct file_system_type *fs_type,
2202 int flags, const char *dev_name,
2203 void *data, struct vfsmount *mnt)
2205 return get_sb_bdev(fs_type, flags, dev_name, data, exfat_fill_super, mnt);
2207 #else
2208 static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
2209 int flags, const char *dev_name,
2210 void *data) {
2211 return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
2213 #endif
2215 static void init_once(void *foo)
2217 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
2219 INIT_HLIST_NODE(&ei->i_hash_fat);
2220 inode_init_once(&ei->vfs_inode);
2223 static int __init exfat_init_inodecache(void)
2225 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
2226 sizeof(struct exfat_inode_info),
2227 0, (SLAB_RECLAIM_ACCOUNT|
2228 SLAB_MEM_SPREAD),
2229 init_once);
2230 if (exfat_inode_cachep == NULL)
2231 return -ENOMEM;
2232 return 0;
2235 static void __exit exfat_destroy_inodecache(void)
2237 kmem_cache_destroy(exfat_inode_cachep);
2240 #if EXFAT_CONFIG_KERNEL_DEBUG
2241 static void exfat_debug_kill_sb(struct super_block *sb)
2243 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2244 struct block_device *bdev = sb->s_bdev;
2246 long flags;
2248 if (sbi) {
2249 flags = sbi->debug_flags;
2251 if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
2252 /* invalidate_bdev drops all device cache include dirty.
2253 we use this to simulate device removal */
2254 FsReleaseCache(sb);
2255 invalidate_bdev(bdev);
2259 kill_block_super(sb);
2261 #endif /* EXFAT_CONFIG_KERNEL_DEBUG */
2263 static struct file_system_type exfat_fs_type = {
2264 .owner = THIS_MODULE,
2265 .name = "exfat",
2266 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
2267 .get_sb = exfat_get_sb,
2268 #else
2269 .mount = exfat_fs_mount,
2270 #endif
2271 #if EXFAT_CONFIG_KERNEL_DEBUG
2272 .kill_sb = exfat_debug_kill_sb,
2273 #else
2274 .kill_sb = kill_block_super,
2275 #endif /* EXFAT_CONFIG_KERNLE_DEBUG */
2276 .fs_flags = FS_REQUIRES_DEV,
2279 static int __init init_exfat(void)
2281 int err;
2283 err = FsInit();
2284 if (err) {
2285 if (err == FFS_MEMORYERR)
2286 return -ENOMEM;
2287 else
2288 return -EIO;
2291 printk(KERN_INFO "exFAT: Version %s\n", EXFAT_VERSION);
2293 err = exfat_init_inodecache();
2294 if (err) goto out;
2296 err = register_filesystem(&exfat_fs_type);
2297 if (err) goto out;
2299 return 0;
2300 out:
2301 FsShutdown();
2302 return err;
2305 static void __exit exit_exfat(void)
2307 exfat_destroy_inodecache();
2308 unregister_filesystem(&exfat_fs_type);
2309 FsShutdown();
2312 module_init(init_exfat);
2313 module_exit(exit_exfat);
2315 MODULE_LICENSE("GPL");
2316 MODULE_DESCRIPTION("exFAT Filesystem Driver");
2317 #ifdef MODULE_ALIAS_FS
2318 MODULE_ALIAS_FS("exfat");
2319 #endif