1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/time.h>
9 #include <linux/slab.h>
11 #include <linux/seq_file.h>
12 #include <linux/pagemap.h>
13 #include <linux/mpage.h>
14 #include <linux/buffer_head.h>
15 #include <linux/exportfs.h>
16 #include <linux/mount.h>
17 #include <linux/vfs.h>
18 #include <linux/aio.h>
19 #include <linux/iversion.h>
20 #include <linux/parser.h>
21 #include <linux/uio.h>
22 #include <linux/writeback.h>
23 #include <linux/log2.h>
24 #include <linux/hash.h>
25 #include <linux/backing-dev.h>
26 #include <linux/sched.h>
27 #include <linux/fs_struct.h>
28 #include <linux/namei.h>
30 #include <linux/string.h>
31 #include <linux/nls.h>
32 #include <linux/mutex.h>
33 #include <linux/swap.h>
35 #define EXFAT_VERSION "1.3.0"
39 static struct kmem_cache
*exfat_inode_cachep
;
41 static int exfat_default_codepage
= CONFIG_EXFAT_DEFAULT_CODEPAGE
;
42 static char exfat_default_iocharset
[] = CONFIG_EXFAT_DEFAULT_IOCHARSET
;
44 #define INC_IVERSION(x) (inode_inc_iversion(x))
45 #define GET_IVERSION(x) (inode_peek_iversion_raw(x))
46 #define SET_IVERSION(x, y) (inode_set_iversion(x, y))
48 static struct inode
*exfat_iget(struct super_block
*sb
, loff_t i_pos
);
49 static int exfat_sync_inode(struct inode
*inode
);
50 static struct inode
*exfat_build_inode(struct super_block
*sb
,
51 struct file_id_t
*fid
, loff_t i_pos
);
52 static int exfat_write_inode(struct inode
*inode
,
53 struct writeback_control
*wbc
);
54 static void exfat_write_super(struct super_block
*sb
);
56 #define UNIX_SECS_1980 315532800L
57 #define UNIX_SECS_2108 4354819200L
59 /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
60 static void exfat_time_fat2unix(struct timespec64
*ts
, struct date_time_t
*tp
)
62 ts
->tv_sec
= mktime64(tp
->Year
+ 1980, tp
->Month
+ 1, tp
->Day
,
63 tp
->Hour
, tp
->Minute
, tp
->Second
);
65 ts
->tv_nsec
= tp
->MilliSecond
* NSEC_PER_MSEC
;
68 /* Convert linear UNIX date to a FAT time/date pair. */
69 static void exfat_time_unix2fat(struct timespec64
*ts
, struct date_time_t
*tp
)
71 time64_t second
= ts
->tv_sec
;
74 time64_to_tm(second
, 0, &tm
);
76 if (second
< UNIX_SECS_1980
) {
87 if (second
>= UNIX_SECS_2108
) {
88 tp
->MilliSecond
= 999;
98 tp
->MilliSecond
= ts
->tv_nsec
/ NSEC_PER_MSEC
;
99 tp
->Second
= tm
.tm_sec
;
100 tp
->Minute
= tm
.tm_min
;
101 tp
->Hour
= tm
.tm_hour
;
102 tp
->Day
= tm
.tm_mday
;
103 tp
->Month
= tm
.tm_mon
+ 1;
104 tp
->Year
= tm
.tm_year
+ 1900 - 1980;
107 struct timestamp_t
*tm_current(struct timestamp_t
*tp
)
109 time64_t second
= ktime_get_real_seconds();
112 time64_to_tm(second
, 0, &tm
);
114 if (second
< UNIX_SECS_1980
) {
124 if (second
>= UNIX_SECS_2108
) {
136 tp
->hour
= tm
.tm_hour
;
137 tp
->day
= tm
.tm_mday
;
138 tp
->mon
= tm
.tm_mon
+ 1;
139 tp
->year
= tm
.tm_year
+ 1900 - 1980;
144 static void __lock_super(struct super_block
*sb
)
146 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
148 mutex_lock(&sbi
->s_lock
);
151 static void __unlock_super(struct super_block
*sb
)
153 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
155 mutex_unlock(&sbi
->s_lock
);
158 static int __is_sb_dirty(struct super_block
*sb
)
160 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
165 static void __set_sb_clean(struct super_block
*sb
)
167 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
172 static int __exfat_revalidate(struct dentry
*dentry
)
177 static int exfat_revalidate(struct dentry
*dentry
, unsigned int flags
)
179 if (flags
& LOOKUP_RCU
)
184 return __exfat_revalidate(dentry
);
187 static int exfat_revalidate_ci(struct dentry
*dentry
, unsigned int flags
)
189 if (flags
& LOOKUP_RCU
)
198 if (flags
& (LOOKUP_CREATE
| LOOKUP_RENAME_TARGET
))
201 return __exfat_revalidate(dentry
);
204 static unsigned int __exfat_striptail_len(unsigned int len
, const char *name
)
206 while (len
&& name
[len
- 1] == '.')
211 static unsigned int exfat_striptail_len(const struct qstr
*qstr
)
213 return __exfat_striptail_len(qstr
->len
, qstr
->name
);
216 static int exfat_d_hash(const struct dentry
*dentry
, struct qstr
*qstr
)
218 qstr
->hash
= full_name_hash(dentry
, qstr
->name
,
219 exfat_striptail_len(qstr
));
223 static int exfat_d_hashi(const struct dentry
*dentry
, struct qstr
*qstr
)
225 struct super_block
*sb
= dentry
->d_sb
;
226 const unsigned char *name
;
231 len
= exfat_striptail_len(qstr
);
233 hash
= init_name_hash(dentry
);
235 hash
= partial_name_hash(nls_upper(sb
, *name
++), hash
);
236 qstr
->hash
= end_name_hash(hash
);
241 static int exfat_cmpi(const struct dentry
*dentry
, unsigned int len
,
242 const char *str
, const struct qstr
*name
)
244 struct nls_table
*t
= EXFAT_SB(dentry
->d_sb
)->nls_io
;
245 unsigned int alen
, blen
;
247 alen
= exfat_striptail_len(name
);
248 blen
= __exfat_striptail_len(len
, str
);
251 if (strncasecmp(name
->name
, str
, alen
) == 0)
254 if (nls_strnicmp(t
, name
->name
, str
, alen
) == 0)
261 static int exfat_cmp(const struct dentry
*dentry
, unsigned int len
,
262 const char *str
, const struct qstr
*name
)
264 unsigned int alen
, blen
;
266 alen
= exfat_striptail_len(name
);
267 blen
= __exfat_striptail_len(len
, str
);
269 if (strncmp(name
->name
, str
, alen
) == 0)
275 static const struct dentry_operations exfat_ci_dentry_ops
= {
276 .d_revalidate
= exfat_revalidate_ci
,
277 .d_hash
= exfat_d_hashi
,
278 .d_compare
= exfat_cmpi
,
281 static const struct dentry_operations exfat_dentry_ops
= {
282 .d_revalidate
= exfat_revalidate
,
283 .d_hash
= exfat_d_hash
,
284 .d_compare
= exfat_cmp
,
287 static DEFINE_SEMAPHORE(z_sem
);
289 static inline void fs_sync(struct super_block
*sb
, bool do_sync
)
296 * If ->i_mode can't hold S_IWUGO (i.e. ATTR_RO), we use ->i_attrs to
297 * save ATTR_RO instead of ->i_mode.
299 * If it's directory and !sbi->options.rodir, ATTR_RO isn't read-only
300 * bit, it's just used as flag for app.
302 static inline int exfat_mode_can_hold_ro(struct inode
*inode
)
304 struct exfat_sb_info
*sbi
= EXFAT_SB(inode
->i_sb
);
306 if (S_ISDIR(inode
->i_mode
))
309 if ((~sbi
->options
.fs_fmask
) & 0222)
314 /* Convert attribute bits and a mask to the UNIX mode. */
315 static inline mode_t
exfat_make_mode(struct exfat_sb_info
*sbi
, u32 attr
,
318 if ((attr
& ATTR_READONLY
) && !(attr
& ATTR_SUBDIR
))
321 if (attr
& ATTR_SUBDIR
)
322 return (mode
& ~sbi
->options
.fs_dmask
) | S_IFDIR
;
323 else if (attr
& ATTR_SYMLINK
)
324 return (mode
& ~sbi
->options
.fs_dmask
) | S_IFLNK
;
326 return (mode
& ~sbi
->options
.fs_fmask
) | S_IFREG
;
329 /* Return the FAT attribute byte for this inode */
330 static inline u32
exfat_make_attr(struct inode
*inode
)
332 if (exfat_mode_can_hold_ro(inode
) && !(inode
->i_mode
& 0222))
333 return (EXFAT_I(inode
)->fid
.attr
) | ATTR_READONLY
;
335 return EXFAT_I(inode
)->fid
.attr
;
338 static inline void exfat_save_attr(struct inode
*inode
, u32 attr
)
340 if (exfat_mode_can_hold_ro(inode
))
341 EXFAT_I(inode
)->fid
.attr
= attr
& ATTR_RWMASK
;
343 EXFAT_I(inode
)->fid
.attr
= attr
& (ATTR_RWMASK
| ATTR_READONLY
);
346 static int ffsMountVol(struct super_block
*sb
)
349 struct pbr_sector_t
*p_pbr
;
350 struct buffer_head
*tmp_bh
= NULL
;
351 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
352 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
354 pr_info("[EXFAT] trying to mount...\n");
360 sema_init(&p_fs
->v_sem
, 1);
361 p_fs
->dev_ejected
= 0;
363 /* open the block device */
366 if (p_bd
->sector_size
< sb
->s_blocksize
) {
370 if (p_bd
->sector_size
> sb
->s_blocksize
)
371 sb_set_blocksize(sb
, p_bd
->sector_size
);
374 if (sector_read(sb
, 0, &tmp_bh
, 1) != FFS_SUCCESS
) {
379 p_fs
->PBR_sector
= 0;
381 p_pbr
= (struct pbr_sector_t
*)tmp_bh
->b_data
;
383 /* check the validity of PBR */
384 if (GET16_A(p_pbr
->signature
) != PBR_SIGNATURE
) {
392 for (i
= 0; i
< 53; i
++)
397 #ifdef CONFIG_EXFAT_DONT_MOUNT_VFAT
399 printk(KERN_INFO
"EXFAT: Attempted to mount VFAT filesystem\n");
402 if (GET16(p_pbr
->bpb
+ 11)) /* num_fat_sectors */
403 ret
= fat16_mount(sb
, p_pbr
);
405 ret
= fat32_mount(sb
, p_pbr
);
408 ret
= exfat_mount(sb
, p_pbr
);
418 if (p_fs
->vol_type
== EXFAT
) {
419 ret
= load_alloc_bitmap(sb
);
424 ret
= load_upcase_table(sb
);
426 free_alloc_bitmap(sb
);
432 if (p_fs
->dev_ejected
) {
433 if (p_fs
->vol_type
== EXFAT
) {
434 free_upcase_table(sb
);
435 free_alloc_bitmap(sb
);
442 pr_info("[EXFAT] mounted successfully\n");
450 static int ffsUmountVol(struct super_block
*sb
)
452 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
453 int err
= FFS_SUCCESS
;
455 pr_info("[EXFAT] trying to unmount...\n");
459 /* acquire the lock for file system critical section */
463 fs_set_vol_flags(sb
, VOL_CLEAN
);
465 if (p_fs
->vol_type
== EXFAT
) {
466 free_upcase_table(sb
);
467 free_alloc_bitmap(sb
);
473 /* close the block device */
476 if (p_fs
->dev_ejected
) {
477 pr_info("[EXFAT] unmounted with media errors. Device is already ejected.\n");
483 /* release the lock for file system critical section */
487 pr_info("[EXFAT] unmounted successfully\n");
492 static int ffsGetVolInfo(struct super_block
*sb
, struct vol_info_t
*info
)
494 int err
= FFS_SUCCESS
;
495 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
497 /* check the validity of pointer parameters */
501 /* acquire the lock for file system critical section */
504 if (p_fs
->used_clusters
== UINT_MAX
)
505 p_fs
->used_clusters
= p_fs
->fs_func
->count_used_clusters(sb
);
507 info
->FatType
= p_fs
->vol_type
;
508 info
->ClusterSize
= p_fs
->cluster_size
;
509 info
->NumClusters
= p_fs
->num_clusters
- 2; /* clu 0 & 1 */
510 info
->UsedClusters
= p_fs
->used_clusters
;
511 info
->FreeClusters
= info
->NumClusters
- info
->UsedClusters
;
513 if (p_fs
->dev_ejected
)
516 /* release the lock for file system critical section */
522 static int ffsSyncVol(struct super_block
*sb
, bool do_sync
)
524 int err
= FFS_SUCCESS
;
525 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
527 /* acquire the lock for file system critical section */
530 /* synchronize the file system */
531 fs_sync(sb
, do_sync
);
532 fs_set_vol_flags(sb
, VOL_CLEAN
);
534 if (p_fs
->dev_ejected
)
537 /* release the lock for file system critical section */
543 /*----------------------------------------------------------------------*/
544 /* File Operation Functions */
545 /*----------------------------------------------------------------------*/
547 static int ffsLookupFile(struct inode
*inode
, char *path
, struct file_id_t
*fid
)
549 int ret
, dentry
, num_entries
;
551 struct uni_name_t uni_name
;
552 struct dos_name_t dos_name
;
553 struct dentry_t
*ep
, *ep2
;
554 struct entry_set_cache_t
*es
= NULL
;
555 struct super_block
*sb
= inode
->i_sb
;
556 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
558 pr_debug("%s entered\n", __func__
);
560 /* check the validity of pointer parameters */
561 if (!fid
|| !path
|| (*path
== '\0'))
564 /* acquire the lock for file system critical section */
567 /* check the validity of directory name in the given pathname */
568 ret
= resolve_path(inode
, path
, &dir
, &uni_name
);
572 ret
= get_num_entries_and_dos_name(sb
, &dir
, &uni_name
, &num_entries
,
577 /* search the file name for directories */
578 dentry
= p_fs
->fs_func
->find_dir_entry(sb
, &dir
, &uni_name
, num_entries
,
579 &dos_name
, TYPE_ALL
);
585 fid
->dir
.dir
= dir
.dir
;
586 fid
->dir
.size
= dir
.size
;
587 fid
->dir
.flags
= dir
.flags
;
591 fid
->type
= TYPE_DIR
;
593 fid
->hint_last_off
= -1;
595 fid
->attr
= ATTR_SUBDIR
;
598 fid
->start_clu
= p_fs
->root_dir
;
600 if (p_fs
->vol_type
== EXFAT
) {
601 es
= get_entry_set_in_dir(sb
, &dir
, dentry
,
609 ep
= get_entry_in_dir(sb
, &dir
, dentry
, NULL
);
617 fid
->type
= p_fs
->fs_func
->get_entry_type(ep
);
619 fid
->hint_last_off
= -1;
620 fid
->attr
= p_fs
->fs_func
->get_entry_attr(ep
);
622 fid
->size
= p_fs
->fs_func
->get_entry_size(ep2
);
623 if ((fid
->type
== TYPE_FILE
) && (fid
->size
== 0)) {
624 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
625 fid
->start_clu
= CLUSTER_32(~0);
627 fid
->flags
= p_fs
->fs_func
->get_entry_flag(ep2
);
628 fid
->start_clu
= p_fs
->fs_func
->get_entry_clu0(ep2
);
631 if (p_fs
->vol_type
== EXFAT
)
632 release_entry_set(es
);
635 if (p_fs
->dev_ejected
)
638 /* release the lock for file system critical section */
644 static int ffsCreateFile(struct inode
*inode
, char *path
, u8 mode
,
645 struct file_id_t
*fid
)
648 struct uni_name_t uni_name
;
649 struct super_block
*sb
= inode
->i_sb
;
650 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
653 /* check the validity of pointer parameters */
654 if (!fid
|| !path
|| (*path
== '\0'))
657 /* acquire the lock for file system critical section */
660 /* check the validity of directory name in the given pathname */
661 ret
= resolve_path(inode
, path
, &dir
, &uni_name
);
665 fs_set_vol_flags(sb
, VOL_DIRTY
);
667 /* create a new file */
668 ret
= create_file(inode
, &dir
, &uni_name
, mode
, fid
);
670 #ifdef CONFIG_EXFAT_DELAYED_SYNC
672 fs_set_vol_flags(sb
, VOL_CLEAN
);
675 if (p_fs
->dev_ejected
)
679 /* release the lock for file system critical section */
685 static int ffsReadFile(struct inode
*inode
, struct file_id_t
*fid
, void *buffer
,
686 u64 count
, u64
*rcount
)
688 s32 offset
, sec_offset
, clu_offset
;
692 u64 oneblkread
, read_bytes
;
693 struct buffer_head
*tmp_bh
= NULL
;
694 struct super_block
*sb
= inode
->i_sb
;
695 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
696 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
698 /* check the validity of the given file id */
700 return FFS_INVALIDFID
;
702 /* check the validity of pointer parameters */
706 /* acquire the lock for file system critical section */
709 /* check if the given file ID is opened */
710 if (fid
->type
!= TYPE_FILE
) {
711 ret
= FFS_PERMISSIONERR
;
715 if (fid
->rwoffset
> fid
->size
)
716 fid
->rwoffset
= fid
->size
;
718 if (count
> (fid
->size
- fid
->rwoffset
))
719 count
= fid
->size
- fid
->rwoffset
;
731 clu_offset
= (s32
)(fid
->rwoffset
>> p_fs
->cluster_size_bits
);
732 clu
= fid
->start_clu
;
734 if (fid
->flags
== 0x03) {
737 /* hint information */
738 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
739 (clu_offset
>= fid
->hint_last_off
)) {
740 clu_offset
-= fid
->hint_last_off
;
741 clu
= fid
->hint_last_clu
;
744 while (clu_offset
> 0) {
745 /* clu = FAT_read(sb, clu); */
746 if (FAT_read(sb
, clu
, &clu
) == -1)
753 /* hint information */
754 fid
->hint_last_off
= (s32
)(fid
->rwoffset
>>
755 p_fs
->cluster_size_bits
);
756 fid
->hint_last_clu
= clu
;
758 /* byte offset in cluster */
759 offset
= (s32
)(fid
->rwoffset
& (p_fs
->cluster_size
- 1));
761 /* sector offset in cluster */
762 sec_offset
= offset
>> p_bd
->sector_size_bits
;
764 /* byte offset in sector */
765 offset
&= p_bd
->sector_size_mask
;
767 LogSector
= START_SECTOR(clu
) + sec_offset
;
769 oneblkread
= (u64
)(p_bd
->sector_size
- offset
);
770 if (oneblkread
> count
)
773 if ((offset
== 0) && (oneblkread
== p_bd
->sector_size
)) {
774 if (sector_read(sb
, LogSector
, &tmp_bh
, 1) !=
777 memcpy((char *)buffer
+ read_bytes
,
778 (char *)tmp_bh
->b_data
, (s32
)oneblkread
);
780 if (sector_read(sb
, LogSector
, &tmp_bh
, 1) !=
783 memcpy((char *)buffer
+ read_bytes
,
784 (char *)tmp_bh
->b_data
+ offset
,
788 read_bytes
+= oneblkread
;
789 fid
->rwoffset
+= oneblkread
;
793 /* How did this ever work and not leak a brlse()?? */
795 /* set the size of read bytes */
797 *rcount
= read_bytes
;
799 if (p_fs
->dev_ejected
)
803 /* release the lock for file system critical section */
809 static int ffsWriteFile(struct inode
*inode
, struct file_id_t
*fid
,
810 void *buffer
, u64 count
, u64
*wcount
)
812 bool modified
= false;
813 s32 offset
, sec_offset
, clu_offset
;
814 s32 num_clusters
, num_alloc
, num_alloced
= (s32
)~0;
817 sector_t LogSector
, sector
= 0;
818 u64 oneblkwrite
, write_bytes
;
819 struct chain_t new_clu
;
820 struct timestamp_t tm
;
821 struct dentry_t
*ep
, *ep2
;
822 struct entry_set_cache_t
*es
= NULL
;
823 struct buffer_head
*tmp_bh
= NULL
;
824 struct super_block
*sb
= inode
->i_sb
;
825 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
826 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
828 /* check the validity of the given file id */
830 return FFS_INVALIDFID
;
832 /* check the validity of pointer parameters */
836 /* acquire the lock for file system critical section */
839 /* check if the given file ID is opened */
840 if (fid
->type
!= TYPE_FILE
) {
841 ret
= FFS_PERMISSIONERR
;
845 if (fid
->rwoffset
> fid
->size
)
846 fid
->rwoffset
= fid
->size
;
855 fs_set_vol_flags(sb
, VOL_DIRTY
);
860 num_clusters
= (s32
)((fid
->size
- 1) >>
861 p_fs
->cluster_size_bits
) + 1;
866 clu_offset
= (s32
)(fid
->rwoffset
>> p_fs
->cluster_size_bits
);
867 clu
= last_clu
= fid
->start_clu
;
869 if (fid
->flags
== 0x03) {
870 if ((clu_offset
> 0) && (clu
!= CLUSTER_32(~0))) {
871 last_clu
+= clu_offset
- 1;
873 if (clu_offset
== num_clusters
)
874 clu
= CLUSTER_32(~0);
879 /* hint information */
880 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
881 (clu_offset
>= fid
->hint_last_off
)) {
882 clu_offset
-= fid
->hint_last_off
;
883 clu
= fid
->hint_last_clu
;
886 while ((clu_offset
> 0) && (clu
!= CLUSTER_32(~0))) {
888 /* clu = FAT_read(sb, clu); */
889 if (FAT_read(sb
, clu
, &clu
) == -1) {
897 if (clu
== CLUSTER_32(~0)) {
898 num_alloc
= (s32
)((count
- 1) >>
899 p_fs
->cluster_size_bits
) + 1;
900 new_clu
.dir
= (last_clu
== CLUSTER_32(~0)) ?
901 CLUSTER_32(~0) : last_clu
+ 1;
903 new_clu
.flags
= fid
->flags
;
905 /* (1) allocate a chain of clusters */
906 num_alloced
= p_fs
->fs_func
->alloc_cluster(sb
,
909 if (num_alloced
== 0)
911 if (num_alloced
< 0) {
916 /* (2) append to the FAT chain */
917 if (last_clu
== CLUSTER_32(~0)) {
918 if (new_clu
.flags
== 0x01)
920 fid
->start_clu
= new_clu
.dir
;
923 if (new_clu
.flags
!= fid
->flags
) {
924 exfat_chain_cont_cluster(sb
,
930 if (new_clu
.flags
== 0x01)
931 FAT_write(sb
, last_clu
, new_clu
.dir
);
934 num_clusters
+= num_alloced
;
938 /* hint information */
939 fid
->hint_last_off
= (s32
)(fid
->rwoffset
>>
940 p_fs
->cluster_size_bits
);
941 fid
->hint_last_clu
= clu
;
943 /* byte offset in cluster */
944 offset
= (s32
)(fid
->rwoffset
& (p_fs
->cluster_size
- 1));
946 /* sector offset in cluster */
947 sec_offset
= offset
>> p_bd
->sector_size_bits
;
949 /* byte offset in sector */
950 offset
&= p_bd
->sector_size_mask
;
952 LogSector
= START_SECTOR(clu
) + sec_offset
;
954 oneblkwrite
= (u64
)(p_bd
->sector_size
- offset
);
955 if (oneblkwrite
> count
)
958 if ((offset
== 0) && (oneblkwrite
== p_bd
->sector_size
)) {
959 if (sector_read(sb
, LogSector
, &tmp_bh
, 0) !=
962 memcpy((char *)tmp_bh
->b_data
,
963 (char *)buffer
+ write_bytes
, (s32
)oneblkwrite
);
964 if (sector_write(sb
, LogSector
, tmp_bh
, 0) !=
971 ((fid
->rwoffset
+ oneblkwrite
) < fid
->size
)) {
972 if (sector_read(sb
, LogSector
, &tmp_bh
, 1) !=
976 if (sector_read(sb
, LogSector
, &tmp_bh
, 0) !=
981 memcpy((char *)tmp_bh
->b_data
+ offset
,
982 (char *)buffer
+ write_bytes
, (s32
)oneblkwrite
);
983 if (sector_write(sb
, LogSector
, tmp_bh
, 0) !=
990 count
-= oneblkwrite
;
991 write_bytes
+= oneblkwrite
;
992 fid
->rwoffset
+= oneblkwrite
;
994 fid
->attr
|= ATTR_ARCHIVE
;
996 if (fid
->size
< fid
->rwoffset
) {
997 fid
->size
= fid
->rwoffset
;
1004 /* (3) update the direcoty entry */
1005 if (p_fs
->vol_type
== EXFAT
) {
1006 es
= get_entry_set_in_dir(sb
, &(fid
->dir
), fid
->entry
,
1007 ES_ALL_ENTRIES
, &ep
);
1012 ep
= get_entry_in_dir(sb
, &(fid
->dir
), fid
->entry
, §or
);
1018 p_fs
->fs_func
->set_entry_time(ep
, tm_current(&tm
), TM_MODIFY
);
1019 p_fs
->fs_func
->set_entry_attr(ep
, fid
->attr
);
1021 if (p_fs
->vol_type
!= EXFAT
)
1022 buf_modify(sb
, sector
);
1025 if (p_fs
->fs_func
->get_entry_flag(ep2
) != fid
->flags
)
1026 p_fs
->fs_func
->set_entry_flag(ep2
, fid
->flags
);
1028 if (p_fs
->fs_func
->get_entry_size(ep2
) != fid
->size
)
1029 p_fs
->fs_func
->set_entry_size(ep2
, fid
->size
);
1031 if (p_fs
->fs_func
->get_entry_clu0(ep2
) != fid
->start_clu
)
1032 p_fs
->fs_func
->set_entry_clu0(ep2
, fid
->start_clu
);
1034 if (p_fs
->vol_type
!= EXFAT
)
1035 buf_modify(sb
, sector
);
1038 if (p_fs
->vol_type
== EXFAT
) {
1039 update_dir_checksum_with_entry_set(sb
, es
);
1040 release_entry_set(es
);
1043 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1045 fs_set_vol_flags(sb
, VOL_CLEAN
);
1049 /* set the size of written bytes */
1051 *wcount
= write_bytes
;
1053 if (num_alloced
== 0)
1056 else if (p_fs
->dev_ejected
)
1060 /* release the lock for file system critical section */
1066 static int ffsTruncateFile(struct inode
*inode
, u64 old_size
, u64 new_size
)
1069 u32 last_clu
= CLUSTER_32(0);
1071 sector_t sector
= 0;
1073 struct timestamp_t tm
;
1074 struct dentry_t
*ep
, *ep2
;
1075 struct super_block
*sb
= inode
->i_sb
;
1076 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1077 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1078 struct entry_set_cache_t
*es
= NULL
;
1080 pr_debug("%s entered (inode %p size %llu)\n", __func__
, inode
,
1083 /* acquire the lock for file system critical section */
1086 /* check if the given file ID is opened */
1087 if (fid
->type
!= TYPE_FILE
) {
1088 ret
= FFS_PERMISSIONERR
;
1092 if (fid
->size
!= old_size
) {
1093 pr_err("[EXFAT] truncate : can't skip it because of size-mismatch(old:%lld->fid:%lld).\n",
1094 old_size
, fid
->size
);
1097 if (old_size
<= new_size
) {
1102 fs_set_vol_flags(sb
, VOL_DIRTY
);
1104 clu
.dir
= fid
->start_clu
;
1105 clu
.size
= (s32
)((old_size
- 1) >> p_fs
->cluster_size_bits
) + 1;
1106 clu
.flags
= fid
->flags
;
1109 num_clusters
= (s32
)((new_size
- 1) >>
1110 p_fs
->cluster_size_bits
) + 1;
1112 if (clu
.flags
== 0x03) {
1113 clu
.dir
+= num_clusters
;
1115 while (num_clusters
> 0) {
1117 if (FAT_read(sb
, clu
.dir
, &clu
.dir
) == -1) {
1125 clu
.size
-= num_clusters
;
1128 fid
->size
= new_size
;
1129 fid
->attr
|= ATTR_ARCHIVE
;
1130 if (new_size
== 0) {
1131 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
1132 fid
->start_clu
= CLUSTER_32(~0);
1135 /* (1) update the directory entry */
1136 if (p_fs
->vol_type
== EXFAT
) {
1137 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
1138 ES_ALL_ENTRIES
, &ep
);
1145 ep
= get_entry_in_dir(sb
, &(fid
->dir
), fid
->entry
, §or
);
1153 p_fs
->fs_func
->set_entry_time(ep
, tm_current(&tm
), TM_MODIFY
);
1154 p_fs
->fs_func
->set_entry_attr(ep
, fid
->attr
);
1156 p_fs
->fs_func
->set_entry_size(ep2
, new_size
);
1157 if (new_size
== 0) {
1158 p_fs
->fs_func
->set_entry_flag(ep2
, 0x01);
1159 p_fs
->fs_func
->set_entry_clu0(ep2
, CLUSTER_32(0));
1162 if (p_fs
->vol_type
!= EXFAT
) {
1163 buf_modify(sb
, sector
);
1165 update_dir_checksum_with_entry_set(sb
, es
);
1166 release_entry_set(es
);
1169 /* (2) cut off from the FAT chain */
1170 if (last_clu
!= CLUSTER_32(0)) {
1171 if (fid
->flags
== 0x01)
1172 FAT_write(sb
, last_clu
, CLUSTER_32(~0));
1175 /* (3) free the clusters */
1176 p_fs
->fs_func
->free_cluster(sb
, &clu
, 0);
1178 /* hint information */
1179 fid
->hint_last_off
= -1;
1180 if (fid
->rwoffset
> fid
->size
)
1181 fid
->rwoffset
= fid
->size
;
1183 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1185 fs_set_vol_flags(sb
, VOL_CLEAN
);
1188 if (p_fs
->dev_ejected
)
1192 pr_debug("%s exited (%d)\n", __func__
, ret
);
1193 /* release the lock for file system critical section */
1199 static void update_parent_info(struct file_id_t
*fid
,
1200 struct inode
*parent_inode
)
1202 struct fs_info_t
*p_fs
= &(EXFAT_SB(parent_inode
->i_sb
)->fs_info
);
1203 struct file_id_t
*parent_fid
= &(EXFAT_I(parent_inode
)->fid
);
1205 if (unlikely((parent_fid
->flags
!= fid
->dir
.flags
) ||
1206 (parent_fid
->size
!=
1207 (fid
->dir
.size
<< p_fs
->cluster_size_bits
)) ||
1208 (parent_fid
->start_clu
!= fid
->dir
.dir
))) {
1209 fid
->dir
.dir
= parent_fid
->start_clu
;
1210 fid
->dir
.flags
= parent_fid
->flags
;
1211 fid
->dir
.size
= ((parent_fid
->size
+ (p_fs
->cluster_size
- 1))
1212 >> p_fs
->cluster_size_bits
);
1216 static int ffsMoveFile(struct inode
*old_parent_inode
, struct file_id_t
*fid
,
1217 struct inode
*new_parent_inode
, struct dentry
*new_dentry
)
1221 struct chain_t olddir
, newdir
;
1222 struct chain_t
*p_dir
= NULL
;
1223 struct uni_name_t uni_name
;
1224 struct dentry_t
*ep
;
1225 struct super_block
*sb
= old_parent_inode
->i_sb
;
1226 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1227 u8
*new_path
= (u8
*)new_dentry
->d_name
.name
;
1228 struct inode
*new_inode
= new_dentry
->d_inode
;
1230 struct file_id_t
*new_fid
= NULL
;
1233 /* check the validity of the given file id */
1235 return FFS_INVALIDFID
;
1237 /* check the validity of pointer parameters */
1238 if (!new_path
|| (*new_path
== '\0'))
1241 /* acquire the lock for file system critical section */
1244 update_parent_info(fid
, old_parent_inode
);
1246 olddir
.dir
= fid
->dir
.dir
;
1247 olddir
.size
= fid
->dir
.size
;
1248 olddir
.flags
= fid
->dir
.flags
;
1250 dentry
= fid
->entry
;
1252 /* check if the old file is "." or ".." */
1253 if (p_fs
->vol_type
!= EXFAT
) {
1254 if ((olddir
.dir
!= p_fs
->root_dir
) && (dentry
< 2)) {
1255 ret
= FFS_PERMISSIONERR
;
1260 ep
= get_entry_in_dir(sb
, &olddir
, dentry
, NULL
);
1266 if (p_fs
->fs_func
->get_entry_attr(ep
) & ATTR_READONLY
) {
1267 ret
= FFS_PERMISSIONERR
;
1271 /* check whether new dir is existing directory and empty */
1276 new_fid
= &EXFAT_I(new_inode
)->fid
;
1278 update_parent_info(new_fid
, new_parent_inode
);
1280 p_dir
= &(new_fid
->dir
);
1281 new_entry
= new_fid
->entry
;
1282 ep
= get_entry_in_dir(sb
, p_dir
, new_entry
, NULL
);
1286 entry_type
= p_fs
->fs_func
->get_entry_type(ep
);
1288 if (entry_type
== TYPE_DIR
) {
1289 struct chain_t new_clu
;
1291 new_clu
.dir
= new_fid
->start_clu
;
1292 new_clu
.size
= (s32
)((new_fid
->size
- 1) >>
1293 p_fs
->cluster_size_bits
) + 1;
1294 new_clu
.flags
= new_fid
->flags
;
1296 if (!is_dir_empty(sb
, &new_clu
)) {
1297 ret
= FFS_FILEEXIST
;
1303 /* check the validity of directory name in the given new pathname */
1304 ret
= resolve_path(new_parent_inode
, new_path
, &newdir
, &uni_name
);
1308 fs_set_vol_flags(sb
, VOL_DIRTY
);
1310 if (olddir
.dir
== newdir
.dir
)
1311 ret
= rename_file(new_parent_inode
, &olddir
, dentry
, &uni_name
,
1314 ret
= move_file(new_parent_inode
, &olddir
, dentry
, &newdir
,
1317 if ((ret
== FFS_SUCCESS
) && new_inode
) {
1318 /* delete entries of new_dir */
1319 ep
= get_entry_in_dir(sb
, p_dir
, new_entry
, NULL
);
1323 num_entries
= p_fs
->fs_func
->count_ext_entries(sb
, p_dir
,
1325 if (num_entries
< 0)
1327 p_fs
->fs_func
->delete_dir_entry(sb
, p_dir
, new_entry
, 0,
1331 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1333 fs_set_vol_flags(sb
, VOL_CLEAN
);
1336 if (p_fs
->dev_ejected
)
1339 /* release the lock for file system critical section */
1345 static int ffsRemoveFile(struct inode
*inode
, struct file_id_t
*fid
)
1348 int ret
= FFS_SUCCESS
;
1349 struct chain_t dir
, clu_to_free
;
1350 struct dentry_t
*ep
;
1351 struct super_block
*sb
= inode
->i_sb
;
1352 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1354 /* check the validity of the given file id */
1356 return FFS_INVALIDFID
;
1358 /* acquire the lock for file system critical section */
1361 dir
.dir
= fid
->dir
.dir
;
1362 dir
.size
= fid
->dir
.size
;
1363 dir
.flags
= fid
->dir
.flags
;
1365 dentry
= fid
->entry
;
1367 ep
= get_entry_in_dir(sb
, &dir
, dentry
, NULL
);
1373 if (p_fs
->fs_func
->get_entry_attr(ep
) & ATTR_READONLY
) {
1374 ret
= FFS_PERMISSIONERR
;
1377 fs_set_vol_flags(sb
, VOL_DIRTY
);
1379 /* (1) update the directory entry */
1380 remove_file(inode
, &dir
, dentry
);
1382 clu_to_free
.dir
= fid
->start_clu
;
1383 clu_to_free
.size
= (s32
)((fid
->size
- 1) >> p_fs
->cluster_size_bits
) + 1;
1384 clu_to_free
.flags
= fid
->flags
;
1386 /* (2) free the clusters */
1387 p_fs
->fs_func
->free_cluster(sb
, &clu_to_free
, 0);
1390 fid
->start_clu
= CLUSTER_32(~0);
1391 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
1393 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1395 fs_set_vol_flags(sb
, VOL_CLEAN
);
1398 if (p_fs
->dev_ejected
)
1401 /* release the lock for file system critical section */
1408 /* Not currently wired up */
1409 static int ffsSetAttr(struct inode
*inode
, u32 attr
)
1412 int ret
= FFS_SUCCESS
;
1413 sector_t sector
= 0;
1414 struct dentry_t
*ep
;
1415 struct super_block
*sb
= inode
->i_sb
;
1416 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1417 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1418 u8 is_dir
= (fid
->type
== TYPE_DIR
) ? 1 : 0;
1419 struct entry_set_cache_t
*es
= NULL
;
1421 if (fid
->attr
== attr
) {
1422 if (p_fs
->dev_ejected
)
1423 return FFS_MEDIAERR
;
1428 if ((fid
->dir
.dir
== p_fs
->root_dir
) &&
1429 (fid
->entry
== -1)) {
1430 if (p_fs
->dev_ejected
)
1431 return FFS_MEDIAERR
;
1436 /* acquire the lock for file system critical section */
1439 /* get the directory entry of given file */
1440 if (p_fs
->vol_type
== EXFAT
) {
1441 es
= get_entry_set_in_dir(sb
, &(fid
->dir
), fid
->entry
,
1442 ES_ALL_ENTRIES
, &ep
);
1448 ep
= get_entry_in_dir(sb
, &(fid
->dir
), fid
->entry
, §or
);
1455 type
= p_fs
->fs_func
->get_entry_type(ep
);
1457 if (((type
== TYPE_FILE
) && (attr
& ATTR_SUBDIR
)) ||
1458 ((type
== TYPE_DIR
) && (!(attr
& ATTR_SUBDIR
)))) {
1459 if (p_fs
->dev_ejected
)
1464 if (p_fs
->vol_type
== EXFAT
)
1465 release_entry_set(es
);
1469 fs_set_vol_flags(sb
, VOL_DIRTY
);
1471 /* set the file attribute */
1473 p_fs
->fs_func
->set_entry_attr(ep
, attr
);
1475 if (p_fs
->vol_type
!= EXFAT
) {
1476 buf_modify(sb
, sector
);
1478 update_dir_checksum_with_entry_set(sb
, es
);
1479 release_entry_set(es
);
1482 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1484 fs_set_vol_flags(sb
, VOL_CLEAN
);
1487 if (p_fs
->dev_ejected
)
1490 /* release the lock for file system critical section */
1497 static int ffsReadStat(struct inode
*inode
, struct dir_entry_t
*info
)
1499 sector_t sector
= 0;
1501 int ret
= FFS_SUCCESS
;
1503 struct uni_name_t uni_name
;
1504 struct timestamp_t tm
;
1505 struct dentry_t
*ep
, *ep2
;
1506 struct super_block
*sb
= inode
->i_sb
;
1507 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1508 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1509 struct entry_set_cache_t
*es
= NULL
;
1510 u8 is_dir
= (fid
->type
== TYPE_DIR
) ? 1 : 0;
1512 pr_debug("%s entered\n", __func__
);
1514 /* acquire the lock for file system critical section */
1518 if ((fid
->dir
.dir
== p_fs
->root_dir
) &&
1519 (fid
->entry
== -1)) {
1520 info
->Attr
= ATTR_SUBDIR
;
1521 memset((char *)&info
->CreateTimestamp
, 0,
1522 sizeof(struct date_time_t
));
1523 memset((char *)&info
->ModifyTimestamp
, 0,
1524 sizeof(struct date_time_t
));
1525 memset((char *)&info
->AccessTimestamp
, 0,
1526 sizeof(struct date_time_t
));
1527 strcpy(info
->ShortName
, ".");
1528 strcpy(info
->Name
, ".");
1530 dir
.dir
= p_fs
->root_dir
;
1533 if (p_fs
->root_dir
== CLUSTER_32(0)) {
1534 /* FAT16 root_dir */
1535 info
->Size
= p_fs
->dentries_in_root
<<
1538 info
->Size
= count_num_clusters(sb
, &dir
) <<
1539 p_fs
->cluster_size_bits
;
1542 count
= count_dos_name_entries(sb
, &dir
, TYPE_DIR
);
1547 info
->NumSubdirs
= count
;
1549 if (p_fs
->dev_ejected
)
1555 /* get the directory entry of given file or directory */
1556 if (p_fs
->vol_type
== EXFAT
) {
1557 es
= get_entry_set_in_dir(sb
, &(fid
->dir
), fid
->entry
,
1565 ep
= get_entry_in_dir(sb
, &(fid
->dir
), fid
->entry
, §or
);
1571 buf_lock(sb
, sector
);
1574 /* set FILE_INFO structure using the acquired struct dentry_t */
1575 info
->Attr
= p_fs
->fs_func
->get_entry_attr(ep
);
1577 p_fs
->fs_func
->get_entry_time(ep
, &tm
, TM_CREATE
);
1578 info
->CreateTimestamp
.Year
= tm
.year
;
1579 info
->CreateTimestamp
.Month
= tm
.mon
;
1580 info
->CreateTimestamp
.Day
= tm
.day
;
1581 info
->CreateTimestamp
.Hour
= tm
.hour
;
1582 info
->CreateTimestamp
.Minute
= tm
.min
;
1583 info
->CreateTimestamp
.Second
= tm
.sec
;
1584 info
->CreateTimestamp
.MilliSecond
= 0;
1586 p_fs
->fs_func
->get_entry_time(ep
, &tm
, TM_MODIFY
);
1587 info
->ModifyTimestamp
.Year
= tm
.year
;
1588 info
->ModifyTimestamp
.Month
= tm
.mon
;
1589 info
->ModifyTimestamp
.Day
= tm
.day
;
1590 info
->ModifyTimestamp
.Hour
= tm
.hour
;
1591 info
->ModifyTimestamp
.Minute
= tm
.min
;
1592 info
->ModifyTimestamp
.Second
= tm
.sec
;
1593 info
->ModifyTimestamp
.MilliSecond
= 0;
1595 memset((char *)&info
->AccessTimestamp
, 0, sizeof(struct date_time_t
));
1597 *(uni_name
.name
) = 0x0;
1598 /* XXX this is very bad for exfat cuz name is already included in es.
1599 * API should be revised
1601 p_fs
->fs_func
->get_uni_name_from_ext_entry(sb
, &(fid
->dir
), fid
->entry
,
1603 if (*uni_name
.name
== 0x0 && p_fs
->vol_type
!= EXFAT
)
1604 get_uni_name_from_dos_entry(sb
, (struct dos_dentry_t
*)ep
,
1606 nls_uniname_to_cstring(sb
, info
->Name
, &uni_name
);
1608 if (p_fs
->vol_type
== EXFAT
) {
1609 info
->NumSubdirs
= 2;
1611 buf_unlock(sb
, sector
);
1612 get_uni_name_from_dos_entry(sb
, (struct dos_dentry_t
*)ep
,
1614 nls_uniname_to_cstring(sb
, info
->ShortName
, &uni_name
);
1615 info
->NumSubdirs
= 0;
1618 info
->Size
= p_fs
->fs_func
->get_entry_size(ep2
);
1620 if (p_fs
->vol_type
== EXFAT
)
1621 release_entry_set(es
);
1624 dir
.dir
= fid
->start_clu
;
1627 if (info
->Size
== 0)
1628 info
->Size
= (u64
)count_num_clusters(sb
, &dir
) <<
1629 p_fs
->cluster_size_bits
;
1631 count
= count_dos_name_entries(sb
, &dir
, TYPE_DIR
);
1636 info
->NumSubdirs
+= count
;
1639 if (p_fs
->dev_ejected
)
1643 /* release the lock for file system critical section */
1646 pr_debug("%s exited successfully\n", __func__
);
1650 static int ffsWriteStat(struct inode
*inode
, struct dir_entry_t
*info
)
1652 sector_t sector
= 0;
1653 int ret
= FFS_SUCCESS
;
1654 struct timestamp_t tm
;
1655 struct dentry_t
*ep
, *ep2
;
1656 struct entry_set_cache_t
*es
= NULL
;
1657 struct super_block
*sb
= inode
->i_sb
;
1658 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1659 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1660 u8 is_dir
= (fid
->type
== TYPE_DIR
) ? 1 : 0;
1662 pr_debug("%s entered (inode %p info %p\n", __func__
, inode
, info
);
1664 /* acquire the lock for file system critical section */
1668 if ((fid
->dir
.dir
== p_fs
->root_dir
) &&
1669 (fid
->entry
== -1)) {
1670 if (p_fs
->dev_ejected
)
1677 fs_set_vol_flags(sb
, VOL_DIRTY
);
1679 /* get the directory entry of given file or directory */
1680 if (p_fs
->vol_type
== EXFAT
) {
1681 es
= get_entry_set_in_dir(sb
, &(fid
->dir
), fid
->entry
,
1682 ES_ALL_ENTRIES
, &ep
);
1689 /* for other than exfat */
1690 ep
= get_entry_in_dir(sb
, &(fid
->dir
), fid
->entry
, §or
);
1698 p_fs
->fs_func
->set_entry_attr(ep
, info
->Attr
);
1700 /* set FILE_INFO structure using the acquired struct dentry_t */
1701 tm
.sec
= info
->CreateTimestamp
.Second
;
1702 tm
.min
= info
->CreateTimestamp
.Minute
;
1703 tm
.hour
= info
->CreateTimestamp
.Hour
;
1704 tm
.day
= info
->CreateTimestamp
.Day
;
1705 tm
.mon
= info
->CreateTimestamp
.Month
;
1706 tm
.year
= info
->CreateTimestamp
.Year
;
1707 p_fs
->fs_func
->set_entry_time(ep
, &tm
, TM_CREATE
);
1709 tm
.sec
= info
->ModifyTimestamp
.Second
;
1710 tm
.min
= info
->ModifyTimestamp
.Minute
;
1711 tm
.hour
= info
->ModifyTimestamp
.Hour
;
1712 tm
.day
= info
->ModifyTimestamp
.Day
;
1713 tm
.mon
= info
->ModifyTimestamp
.Month
;
1714 tm
.year
= info
->ModifyTimestamp
.Year
;
1715 p_fs
->fs_func
->set_entry_time(ep
, &tm
, TM_MODIFY
);
1717 p_fs
->fs_func
->set_entry_size(ep2
, info
->Size
);
1719 if (p_fs
->vol_type
!= EXFAT
) {
1720 buf_modify(sb
, sector
);
1722 update_dir_checksum_with_entry_set(sb
, es
);
1723 release_entry_set(es
);
1726 if (p_fs
->dev_ejected
)
1730 /* release the lock for file system critical section */
1733 pr_debug("%s exited (%d)\n", __func__
, ret
);
1738 static int ffsMapCluster(struct inode
*inode
, s32 clu_offset
, u32
*clu
)
1740 s32 num_clusters
, num_alloced
;
1741 bool modified
= false;
1743 int ret
= FFS_SUCCESS
;
1744 sector_t sector
= 0;
1745 struct chain_t new_clu
;
1746 struct dentry_t
*ep
;
1747 struct entry_set_cache_t
*es
= NULL
;
1748 struct super_block
*sb
= inode
->i_sb
;
1749 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1750 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1752 /* check the validity of pointer parameters */
1756 /* acquire the lock for file system critical section */
1759 fid
->rwoffset
= (s64
)(clu_offset
) << p_fs
->cluster_size_bits
;
1761 if (EXFAT_I(inode
)->mmu_private
== 0)
1764 num_clusters
= (s32
)((EXFAT_I(inode
)->mmu_private
- 1) >>
1765 p_fs
->cluster_size_bits
) + 1;
1767 *clu
= last_clu
= fid
->start_clu
;
1769 if (fid
->flags
== 0x03) {
1770 if ((clu_offset
> 0) && (*clu
!= CLUSTER_32(~0))) {
1771 last_clu
+= clu_offset
- 1;
1773 if (clu_offset
== num_clusters
)
1774 *clu
= CLUSTER_32(~0);
1779 /* hint information */
1780 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
1781 (clu_offset
>= fid
->hint_last_off
)) {
1782 clu_offset
-= fid
->hint_last_off
;
1783 *clu
= fid
->hint_last_clu
;
1786 while ((clu_offset
> 0) && (*clu
!= CLUSTER_32(~0))) {
1788 if (FAT_read(sb
, *clu
, clu
) == -1) {
1796 if (*clu
== CLUSTER_32(~0)) {
1797 fs_set_vol_flags(sb
, VOL_DIRTY
);
1799 new_clu
.dir
= (last_clu
== CLUSTER_32(~0)) ? CLUSTER_32(~0) :
1802 new_clu
.flags
= fid
->flags
;
1804 /* (1) allocate a cluster */
1805 num_alloced
= p_fs
->fs_func
->alloc_cluster(sb
, 1, &new_clu
);
1806 if (num_alloced
< 0) {
1809 } else if (num_alloced
== 0) {
1814 /* (2) append to the FAT chain */
1815 if (last_clu
== CLUSTER_32(~0)) {
1816 if (new_clu
.flags
== 0x01)
1818 fid
->start_clu
= new_clu
.dir
;
1821 if (new_clu
.flags
!= fid
->flags
) {
1822 exfat_chain_cont_cluster(sb
, fid
->start_clu
,
1827 if (new_clu
.flags
== 0x01)
1828 FAT_write(sb
, last_clu
, new_clu
.dir
);
1831 num_clusters
+= num_alloced
;
1834 if (p_fs
->vol_type
== EXFAT
) {
1835 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
1836 ES_ALL_ENTRIES
, &ep
);
1841 /* get stream entry */
1845 /* (3) update directory entry */
1847 if (p_fs
->vol_type
!= EXFAT
) {
1848 ep
= get_entry_in_dir(sb
, &(fid
->dir
),
1849 fid
->entry
, §or
);
1856 if (p_fs
->fs_func
->get_entry_flag(ep
) != fid
->flags
)
1857 p_fs
->fs_func
->set_entry_flag(ep
, fid
->flags
);
1859 if (p_fs
->fs_func
->get_entry_clu0(ep
) != fid
->start_clu
)
1860 p_fs
->fs_func
->set_entry_clu0(ep
,
1863 if (p_fs
->vol_type
!= EXFAT
)
1864 buf_modify(sb
, sector
);
1867 if (p_fs
->vol_type
== EXFAT
) {
1868 update_dir_checksum_with_entry_set(sb
, es
);
1869 release_entry_set(es
);
1872 /* add number of new blocks to inode */
1873 inode
->i_blocks
+= num_alloced
<< (p_fs
->cluster_size_bits
- 9);
1876 /* hint information */
1877 fid
->hint_last_off
= (s32
)(fid
->rwoffset
>> p_fs
->cluster_size_bits
);
1878 fid
->hint_last_clu
= *clu
;
1880 if (p_fs
->dev_ejected
)
1884 /* release the lock for file system critical section */
1890 /*----------------------------------------------------------------------*/
1891 /* Directory Operation Functions */
1892 /*----------------------------------------------------------------------*/
1894 static int ffsCreateDir(struct inode
*inode
, char *path
, struct file_id_t
*fid
)
1896 int ret
= FFS_SUCCESS
;
1898 struct uni_name_t uni_name
;
1899 struct super_block
*sb
= inode
->i_sb
;
1900 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1902 pr_debug("%s entered\n", __func__
);
1904 /* check the validity of pointer parameters */
1905 if (!fid
|| !path
|| (*path
== '\0'))
1908 /* acquire the lock for file system critical section */
1911 /* check the validity of directory name in the given old pathname */
1912 ret
= resolve_path(inode
, path
, &dir
, &uni_name
);
1916 fs_set_vol_flags(sb
, VOL_DIRTY
);
1918 ret
= create_dir(inode
, &dir
, &uni_name
, fid
);
1920 #ifdef CONFIG_EXFAT_DELAYED_SYNC
1922 fs_set_vol_flags(sb
, VOL_CLEAN
);
1925 if (p_fs
->dev_ejected
)
1928 /* release the lock for file system critical section */
1934 static int ffsReadDir(struct inode
*inode
, struct dir_entry_t
*dir_entry
)
1936 int i
, dentry
, clu_offset
;
1937 int ret
= FFS_SUCCESS
;
1938 s32 dentries_per_clu
, dentries_per_clu_bits
= 0;
1941 struct chain_t dir
, clu
;
1942 struct uni_name_t uni_name
;
1943 struct timestamp_t tm
;
1944 struct dentry_t
*ep
;
1945 struct super_block
*sb
= inode
->i_sb
;
1946 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1947 struct fs_func
*fs_func
= p_fs
->fs_func
;
1948 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1950 /* check the validity of pointer parameters */
1954 /* check if the given file ID is opened */
1955 if (fid
->type
!= TYPE_DIR
)
1956 return FFS_PERMISSIONERR
;
1958 /* acquire the lock for file system critical section */
1961 if (fid
->entry
== -1) {
1962 dir
.dir
= p_fs
->root_dir
;
1965 dir
.dir
= fid
->start_clu
;
1966 dir
.size
= (s32
)(fid
->size
>> p_fs
->cluster_size_bits
);
1967 dir
.flags
= fid
->flags
;
1970 dentry
= (s32
)fid
->rwoffset
;
1972 if (dir
.dir
== CLUSTER_32(0)) {
1973 /* FAT16 root_dir */
1974 dentries_per_clu
= p_fs
->dentries_in_root
;
1976 if (dentry
== dentries_per_clu
) {
1977 clu
.dir
= CLUSTER_32(~0);
1980 clu
.size
= dir
.size
;
1981 clu
.flags
= dir
.flags
;
1984 dentries_per_clu
= p_fs
->dentries_per_clu
;
1985 dentries_per_clu_bits
= ilog2(dentries_per_clu
);
1987 clu_offset
= dentry
>> dentries_per_clu_bits
;
1989 clu
.size
= dir
.size
;
1990 clu
.flags
= dir
.flags
;
1992 if (clu
.flags
== 0x03) {
1993 clu
.dir
+= clu_offset
;
1994 clu
.size
-= clu_offset
;
1996 /* hint_information */
1997 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
1998 (clu_offset
>= fid
->hint_last_off
)) {
1999 clu_offset
-= fid
->hint_last_off
;
2000 clu
.dir
= fid
->hint_last_clu
;
2003 while (clu_offset
> 0) {
2004 /* clu.dir = FAT_read(sb, clu.dir); */
2005 if (FAT_read(sb
, clu
.dir
, &clu
.dir
) == -1) {
2014 while (clu
.dir
!= CLUSTER_32(~0)) {
2015 if (p_fs
->dev_ejected
)
2018 if (dir
.dir
== CLUSTER_32(0)) /* FAT16 root_dir */
2019 i
= dentry
% dentries_per_clu
;
2021 i
= dentry
& (dentries_per_clu
- 1);
2023 for ( ; i
< dentries_per_clu
; i
++, dentry
++) {
2024 ep
= get_entry_in_dir(sb
, &clu
, i
, §or
);
2029 type
= fs_func
->get_entry_type(ep
);
2031 if (type
== TYPE_UNUSED
)
2034 if ((type
!= TYPE_FILE
) && (type
!= TYPE_DIR
))
2037 buf_lock(sb
, sector
);
2038 dir_entry
->Attr
= fs_func
->get_entry_attr(ep
);
2040 fs_func
->get_entry_time(ep
, &tm
, TM_CREATE
);
2041 dir_entry
->CreateTimestamp
.Year
= tm
.year
;
2042 dir_entry
->CreateTimestamp
.Month
= tm
.mon
;
2043 dir_entry
->CreateTimestamp
.Day
= tm
.day
;
2044 dir_entry
->CreateTimestamp
.Hour
= tm
.hour
;
2045 dir_entry
->CreateTimestamp
.Minute
= tm
.min
;
2046 dir_entry
->CreateTimestamp
.Second
= tm
.sec
;
2047 dir_entry
->CreateTimestamp
.MilliSecond
= 0;
2049 fs_func
->get_entry_time(ep
, &tm
, TM_MODIFY
);
2050 dir_entry
->ModifyTimestamp
.Year
= tm
.year
;
2051 dir_entry
->ModifyTimestamp
.Month
= tm
.mon
;
2052 dir_entry
->ModifyTimestamp
.Day
= tm
.day
;
2053 dir_entry
->ModifyTimestamp
.Hour
= tm
.hour
;
2054 dir_entry
->ModifyTimestamp
.Minute
= tm
.min
;
2055 dir_entry
->ModifyTimestamp
.Second
= tm
.sec
;
2056 dir_entry
->ModifyTimestamp
.MilliSecond
= 0;
2058 memset((char *)&dir_entry
->AccessTimestamp
, 0,
2059 sizeof(struct date_time_t
));
2061 *(uni_name
.name
) = 0x0;
2062 fs_func
->get_uni_name_from_ext_entry(sb
, &dir
, dentry
,
2064 if (*uni_name
.name
== 0x0 && p_fs
->vol_type
!= EXFAT
)
2065 get_uni_name_from_dos_entry(sb
,
2066 (struct dos_dentry_t
*)ep
,
2068 nls_uniname_to_cstring(sb
, dir_entry
->Name
, &uni_name
);
2069 buf_unlock(sb
, sector
);
2071 if (p_fs
->vol_type
== EXFAT
) {
2072 ep
= get_entry_in_dir(sb
, &clu
, i
+ 1, NULL
);
2078 get_uni_name_from_dos_entry(sb
,
2079 (struct dos_dentry_t
*)ep
,
2081 nls_uniname_to_cstring(sb
, dir_entry
->ShortName
,
2085 dir_entry
->Size
= fs_func
->get_entry_size(ep
);
2087 /* hint information */
2088 if (dir
.dir
== CLUSTER_32(0)) { /* FAT16 root_dir */
2090 fid
->hint_last_off
= dentry
>>
2091 dentries_per_clu_bits
;
2092 fid
->hint_last_clu
= clu
.dir
;
2095 fid
->rwoffset
= (s64
)(++dentry
);
2097 if (p_fs
->dev_ejected
)
2102 if (dir
.dir
== CLUSTER_32(0))
2103 break; /* FAT16 root_dir */
2105 if (clu
.flags
== 0x03) {
2106 if ((--clu
.size
) > 0)
2109 clu
.dir
= CLUSTER_32(~0);
2111 /* clu.dir = FAT_read(sb, clu.dir); */
2112 if (FAT_read(sb
, clu
.dir
, &clu
.dir
) == -1) {
2119 *(dir_entry
->Name
) = '\0';
2121 fid
->rwoffset
= (s64
)(++dentry
);
2123 if (p_fs
->dev_ejected
)
2127 /* release the lock for file system critical section */
2133 static int ffsRemoveDir(struct inode
*inode
, struct file_id_t
*fid
)
2136 int ret
= FFS_SUCCESS
;
2137 struct chain_t dir
, clu_to_free
;
2138 struct super_block
*sb
= inode
->i_sb
;
2139 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2141 /* check the validity of the given file id */
2143 return FFS_INVALIDFID
;
2145 dir
.dir
= fid
->dir
.dir
;
2146 dir
.size
= fid
->dir
.size
;
2147 dir
.flags
= fid
->dir
.flags
;
2149 dentry
= fid
->entry
;
2151 /* check if the file is "." or ".." */
2152 if (p_fs
->vol_type
!= EXFAT
) {
2153 if ((dir
.dir
!= p_fs
->root_dir
) && (dentry
< 2))
2154 return FFS_PERMISSIONERR
;
2157 /* acquire the lock for file system critical section */
2160 clu_to_free
.dir
= fid
->start_clu
;
2161 clu_to_free
.size
= (s32
)((fid
->size
- 1) >> p_fs
->cluster_size_bits
) + 1;
2162 clu_to_free
.flags
= fid
->flags
;
2164 if (!is_dir_empty(sb
, &clu_to_free
)) {
2165 ret
= FFS_FILEEXIST
;
2169 fs_set_vol_flags(sb
, VOL_DIRTY
);
2171 /* (1) update the directory entry */
2172 remove_file(inode
, &dir
, dentry
);
2174 /* (2) free the clusters */
2175 p_fs
->fs_func
->free_cluster(sb
, &clu_to_free
, 1);
2178 fid
->start_clu
= CLUSTER_32(~0);
2179 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
2181 #ifdef CONFIG_EXFAT_DELAYED_SYNC
2183 fs_set_vol_flags(sb
, VOL_CLEAN
);
2186 if (p_fs
->dev_ejected
)
2190 /* release the lock for file system critical section */
2196 /*======================================================================*/
2197 /* Directory Entry Operations */
2198 /*======================================================================*/
2200 static int exfat_readdir(struct file
*filp
, struct dir_context
*ctx
)
2202 struct inode
*inode
= file_inode(filp
);
2203 struct super_block
*sb
= inode
->i_sb
;
2204 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2205 struct fs_info_t
*p_fs
= &(sbi
->fs_info
);
2206 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
2207 struct dir_entry_t de
;
2215 /* Fake . and .. for the root directory. */
2216 if ((p_fs
->vol_type
== EXFAT
) || (inode
->i_ino
== EXFAT_ROOT_INO
)) {
2218 if (inode
->i_ino
== EXFAT_ROOT_INO
)
2219 inum
= EXFAT_ROOT_INO
;
2221 inum
= inode
->i_ino
;
2222 else /* (cpos == 1) */
2223 inum
= parent_ino(filp
->f_path
.dentry
);
2225 if (!dir_emit_dots(filp
, ctx
))
2233 if (cpos
& (DENTRY_SIZE
- 1)) {
2239 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2240 EXFAT_I(inode
)->fid
.rwoffset
= cpos
>> DENTRY_SIZE_BITS
;
2242 err
= ffsReadDir(inode
, &de
);
2244 /* at least we tried to read a sector
2245 * move cpos to next sector position (should be aligned)
2247 if (err
== FFS_MEDIAERR
) {
2248 cpos
+= 1 << p_bd
->sector_size_bits
;
2249 cpos
&= ~((1 << p_bd
->sector_size_bits
) - 1);
2256 cpos
= EXFAT_I(inode
)->fid
.rwoffset
<< DENTRY_SIZE_BITS
;
2261 if (!memcmp(de
.ShortName
, DOS_CUR_DIR_NAME
, DOS_NAME_LENGTH
)) {
2262 inum
= inode
->i_ino
;
2263 } else if (!memcmp(de
.ShortName
, DOS_PAR_DIR_NAME
, DOS_NAME_LENGTH
)) {
2264 inum
= parent_ino(filp
->f_path
.dentry
);
2266 loff_t i_pos
= ((loff_t
)EXFAT_I(inode
)->fid
.start_clu
<< 32) |
2267 ((EXFAT_I(inode
)->fid
.rwoffset
- 1) & 0xffffffff);
2268 struct inode
*tmp
= exfat_iget(sb
, i_pos
);
2274 inum
= iunique(sb
, EXFAT_ROOT_INO
);
2278 if (!dir_emit(ctx
, de
.Name
, strlen(de
.Name
), inum
,
2279 (de
.Attr
& ATTR_SUBDIR
) ? DT_DIR
: DT_REG
))
2292 static int exfat_ioctl_volume_id(struct inode
*dir
)
2294 struct super_block
*sb
= dir
->i_sb
;
2295 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2296 struct fs_info_t
*p_fs
= &(sbi
->fs_info
);
2298 return p_fs
->vol_id
;
2301 static long exfat_generic_ioctl(struct file
*filp
, unsigned int cmd
,
2304 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2305 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2307 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2310 case EXFAT_IOCTL_GET_VOLUME_ID
:
2311 return exfat_ioctl_volume_id(inode
);
2312 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2313 case EXFAT_IOC_GET_DEBUGFLAGS
: {
2314 struct super_block
*sb
= inode
->i_sb
;
2315 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2317 flags
= sbi
->debug_flags
;
2318 return put_user(flags
, (int __user
*)arg
);
2320 case EXFAT_IOC_SET_DEBUGFLAGS
: {
2321 struct super_block
*sb
= inode
->i_sb
;
2322 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2324 if (!capable(CAP_SYS_ADMIN
))
2327 if (get_user(flags
, (int __user
*)arg
))
2331 sbi
->debug_flags
= flags
;
2336 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2338 return -ENOTTY
; /* Inappropriate ioctl for device */
2342 static const struct file_operations exfat_dir_operations
= {
2343 .llseek
= generic_file_llseek
,
2344 .read
= generic_read_dir
,
2345 .iterate
= exfat_readdir
,
2346 .unlocked_ioctl
= exfat_generic_ioctl
,
2347 .fsync
= generic_file_fsync
,
2350 static int exfat_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2353 struct super_block
*sb
= dir
->i_sb
;
2354 struct inode
*inode
;
2355 struct file_id_t fid
;
2361 pr_debug("%s entered\n", __func__
);
2363 err
= ffsCreateFile(dir
, (u8
*)dentry
->d_name
.name
, FM_REGULAR
, &fid
);
2365 if (err
== FFS_INVALIDPATH
)
2367 else if (err
== FFS_FILEEXIST
)
2369 else if (err
== FFS_FULL
)
2371 else if (err
== FFS_NAMETOOLONG
)
2372 err
= -ENAMETOOLONG
;
2378 dir
->i_ctime
= dir
->i_mtime
= dir
->i_atime
= current_time(dir
);
2379 if (IS_DIRSYNC(dir
))
2380 (void)exfat_sync_inode(dir
);
2382 mark_inode_dirty(dir
);
2384 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2386 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2387 if (IS_ERR(inode
)) {
2388 err
= PTR_ERR(inode
);
2391 INC_IVERSION(inode
);
2392 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
2394 * timestamp is already written, so mark_inode_dirty() is unnecessary.
2397 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2398 d_instantiate(dentry
, inode
);
2402 pr_debug("%s exited\n", __func__
);
2406 static int exfat_find(struct inode
*dir
, struct qstr
*qname
,
2407 struct file_id_t
*fid
)
2411 if (qname
->len
== 0)
2414 err
= ffsLookupFile(dir
, (u8
*)qname
->name
, fid
);
2421 static int exfat_d_anon_disconn(struct dentry
*dentry
)
2423 return IS_ROOT(dentry
) && (dentry
->d_flags
& DCACHE_DISCONNECTED
);
2426 static struct dentry
*exfat_lookup(struct inode
*dir
, struct dentry
*dentry
,
2429 struct super_block
*sb
= dir
->i_sb
;
2430 struct inode
*inode
;
2431 struct dentry
*alias
;
2433 struct file_id_t fid
;
2439 pr_debug("%s entered\n", __func__
);
2440 err
= exfat_find(dir
, &dentry
->d_name
, &fid
);
2442 if (err
== -ENOENT
) {
2449 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2450 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2451 if (IS_ERR(inode
)) {
2452 err
= PTR_ERR(inode
);
2456 i_mode
= inode
->i_mode
;
2457 if (S_ISLNK(i_mode
) && !EXFAT_I(inode
)->target
) {
2458 EXFAT_I(inode
)->target
= kmalloc(i_size_read(inode
) + 1,
2460 if (!EXFAT_I(inode
)->target
) {
2464 ffsReadFile(dir
, &fid
, EXFAT_I(inode
)->target
,
2465 i_size_read(inode
), &ret
);
2466 *(EXFAT_I(inode
)->target
+ i_size_read(inode
)) = '\0';
2469 alias
= d_find_alias(inode
);
2470 if (alias
&& !exfat_d_anon_disconn(alias
)) {
2471 BUG_ON(d_unhashed(alias
));
2472 if (!S_ISDIR(i_mode
))
2473 d_move(alias
, dentry
);
2476 pr_debug("%s exited 1\n", __func__
);
2482 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2483 dentry
= d_splice_alias(inode
, dentry
);
2485 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2486 pr_debug("%s exited 2\n", __func__
);
2491 pr_debug("%s exited 3\n", __func__
);
2492 return ERR_PTR(err
);
2495 static inline unsigned long exfat_hash(loff_t i_pos
)
2497 return hash_32(i_pos
, EXFAT_HASH_BITS
);
2500 static void exfat_attach(struct inode
*inode
, loff_t i_pos
)
2502 struct exfat_sb_info
*sbi
= EXFAT_SB(inode
->i_sb
);
2503 struct hlist_head
*head
= sbi
->inode_hashtable
+ exfat_hash(i_pos
);
2505 spin_lock(&sbi
->inode_hash_lock
);
2506 EXFAT_I(inode
)->i_pos
= i_pos
;
2507 hlist_add_head(&EXFAT_I(inode
)->i_hash_fat
, head
);
2508 spin_unlock(&sbi
->inode_hash_lock
);
2511 static void exfat_detach(struct inode
*inode
)
2513 struct exfat_sb_info
*sbi
= EXFAT_SB(inode
->i_sb
);
2515 spin_lock(&sbi
->inode_hash_lock
);
2516 hlist_del_init(&EXFAT_I(inode
)->i_hash_fat
);
2517 EXFAT_I(inode
)->i_pos
= 0;
2518 spin_unlock(&sbi
->inode_hash_lock
);
2521 static int exfat_unlink(struct inode
*dir
, struct dentry
*dentry
)
2523 struct inode
*inode
= dentry
->d_inode
;
2524 struct super_block
*sb
= dir
->i_sb
;
2529 pr_debug("%s entered\n", __func__
);
2531 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2533 err
= ffsRemoveFile(dir
, &(EXFAT_I(inode
)->fid
));
2535 if (err
== FFS_PERMISSIONERR
)
2542 dir
->i_mtime
= dir
->i_atime
= current_time(dir
);
2543 if (IS_DIRSYNC(dir
))
2544 (void)exfat_sync_inode(dir
);
2546 mark_inode_dirty(dir
);
2549 inode
->i_mtime
= inode
->i_atime
= current_time(inode
);
2550 exfat_detach(inode
);
2551 remove_inode_hash(inode
);
2555 pr_debug("%s exited\n", __func__
);
2559 static int exfat_symlink(struct inode
*dir
, struct dentry
*dentry
,
2562 struct super_block
*sb
= dir
->i_sb
;
2563 struct inode
*inode
;
2564 struct file_id_t fid
;
2567 u64 len
= (u64
)strlen(target
);
2572 pr_debug("%s entered\n", __func__
);
2574 err
= ffsCreateFile(dir
, (u8
*)dentry
->d_name
.name
, FM_SYMLINK
, &fid
);
2576 if (err
== FFS_INVALIDPATH
)
2578 else if (err
== FFS_FILEEXIST
)
2580 else if (err
== FFS_FULL
)
2587 err
= ffsWriteFile(dir
, &fid
, (char *)target
, len
, &ret
);
2590 ffsRemoveFile(dir
, &fid
);
2592 if (err
== FFS_FULL
)
2600 dir
->i_ctime
= dir
->i_mtime
= dir
->i_atime
= current_time(dir
);
2601 if (IS_DIRSYNC(dir
))
2602 (void)exfat_sync_inode(dir
);
2604 mark_inode_dirty(dir
);
2606 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2608 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2609 if (IS_ERR(inode
)) {
2610 err
= PTR_ERR(inode
);
2613 INC_IVERSION(inode
);
2614 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
2615 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2617 EXFAT_I(inode
)->target
= kmemdup(target
, len
+ 1, GFP_KERNEL
);
2618 if (!EXFAT_I(inode
)->target
) {
2623 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2624 d_instantiate(dentry
, inode
);
2628 pr_debug("%s exited\n", __func__
);
2632 static int exfat_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2634 struct super_block
*sb
= dir
->i_sb
;
2635 struct inode
*inode
;
2636 struct file_id_t fid
;
2642 pr_debug("%s entered\n", __func__
);
2644 err
= ffsCreateDir(dir
, (u8
*)dentry
->d_name
.name
, &fid
);
2646 if (err
== FFS_INVALIDPATH
)
2648 else if (err
== FFS_FILEEXIST
)
2650 else if (err
== FFS_FULL
)
2652 else if (err
== FFS_NAMETOOLONG
)
2653 err
= -ENAMETOOLONG
;
2659 dir
->i_ctime
= dir
->i_mtime
= dir
->i_atime
= current_time(dir
);
2660 if (IS_DIRSYNC(dir
))
2661 (void)exfat_sync_inode(dir
);
2663 mark_inode_dirty(dir
);
2666 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2668 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2669 if (IS_ERR(inode
)) {
2670 err
= PTR_ERR(inode
);
2673 INC_IVERSION(inode
);
2674 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
2675 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2677 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2678 d_instantiate(dentry
, inode
);
2682 pr_debug("%s exited\n", __func__
);
2686 static int exfat_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2688 struct inode
*inode
= dentry
->d_inode
;
2689 struct super_block
*sb
= dir
->i_sb
;
2694 pr_debug("%s entered\n", __func__
);
2696 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2698 err
= ffsRemoveDir(dir
, &(EXFAT_I(inode
)->fid
));
2700 if (err
== FFS_INVALIDPATH
)
2702 else if (err
== FFS_FILEEXIST
)
2704 else if (err
== FFS_NOTFOUND
)
2706 else if (err
== FFS_DIRBUSY
)
2713 dir
->i_mtime
= dir
->i_atime
= current_time(dir
);
2714 if (IS_DIRSYNC(dir
))
2715 (void)exfat_sync_inode(dir
);
2717 mark_inode_dirty(dir
);
2721 inode
->i_mtime
= inode
->i_atime
= current_time(inode
);
2722 exfat_detach(inode
);
2723 remove_inode_hash(inode
);
2727 pr_debug("%s exited\n", __func__
);
2731 static int exfat_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2732 struct inode
*new_dir
, struct dentry
*new_dentry
,
2735 struct inode
*old_inode
, *new_inode
;
2736 struct super_block
*sb
= old_dir
->i_sb
;
2745 pr_debug("%s entered\n", __func__
);
2747 old_inode
= old_dentry
->d_inode
;
2748 new_inode
= new_dentry
->d_inode
;
2750 EXFAT_I(old_inode
)->fid
.size
= i_size_read(old_inode
);
2752 err
= ffsMoveFile(old_dir
, &(EXFAT_I(old_inode
)->fid
), new_dir
,
2755 if (err
== FFS_PERMISSIONERR
)
2757 else if (err
== FFS_INVALIDPATH
)
2759 else if (err
== FFS_FILEEXIST
)
2761 else if (err
== FFS_NOTFOUND
)
2763 else if (err
== FFS_FULL
)
2769 INC_IVERSION(new_dir
);
2770 new_dir
->i_ctime
= new_dir
->i_mtime
= new_dir
->i_atime
=
2771 current_time(new_dir
);
2772 if (IS_DIRSYNC(new_dir
))
2773 (void)exfat_sync_inode(new_dir
);
2775 mark_inode_dirty(new_dir
);
2777 i_pos
= ((loff_t
)EXFAT_I(old_inode
)->fid
.dir
.dir
<< 32) |
2778 (EXFAT_I(old_inode
)->fid
.entry
& 0xffffffff);
2780 exfat_detach(old_inode
);
2781 exfat_attach(old_inode
, i_pos
);
2782 if (IS_DIRSYNC(new_dir
))
2783 (void)exfat_sync_inode(old_inode
);
2785 mark_inode_dirty(old_inode
);
2787 if ((S_ISDIR(old_inode
->i_mode
)) && (old_dir
!= new_dir
)) {
2788 drop_nlink(old_dir
);
2792 INC_IVERSION(old_dir
);
2793 old_dir
->i_ctime
= old_dir
->i_mtime
= current_time(old_dir
);
2794 if (IS_DIRSYNC(old_dir
))
2795 (void)exfat_sync_inode(old_dir
);
2797 mark_inode_dirty(old_dir
);
2800 exfat_detach(new_inode
);
2801 drop_nlink(new_inode
);
2802 if (S_ISDIR(new_inode
->i_mode
))
2803 drop_nlink(new_inode
);
2804 new_inode
->i_ctime
= current_time(new_inode
);
2809 pr_debug("%s exited\n", __func__
);
2813 static int exfat_cont_expand(struct inode
*inode
, loff_t size
)
2815 struct address_space
*mapping
= inode
->i_mapping
;
2816 loff_t start
= i_size_read(inode
), count
= size
- i_size_read(inode
);
2819 err
= generic_cont_expand_simple(inode
, size
);
2823 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
2824 mark_inode_dirty(inode
);
2826 if (IS_SYNC(inode
)) {
2827 err
= filemap_fdatawrite_range(mapping
, start
,
2829 err2
= sync_mapping_buffers(mapping
);
2830 err
= (err
) ? (err
) : (err2
);
2831 err2
= write_inode_now(inode
, 1);
2832 err
= (err
) ? (err
) : (err2
);
2834 err
= filemap_fdatawait_range(mapping
, start
,
2840 static int exfat_allow_set_time(struct exfat_sb_info
*sbi
, struct inode
*inode
)
2842 mode_t allow_utime
= sbi
->options
.allow_utime
;
2844 if (!uid_eq(current_fsuid(), inode
->i_uid
)) {
2845 if (in_group_p(inode
->i_gid
))
2847 if (allow_utime
& MAY_WRITE
)
2851 /* use a default check */
2855 static int exfat_sanitize_mode(const struct exfat_sb_info
*sbi
,
2856 struct inode
*inode
, umode_t
*mode_ptr
)
2858 mode_t i_mode
, mask
, perm
;
2860 i_mode
= inode
->i_mode
;
2862 if (S_ISREG(i_mode
) || S_ISLNK(i_mode
))
2863 mask
= sbi
->options
.fs_fmask
;
2865 mask
= sbi
->options
.fs_dmask
;
2867 perm
= *mode_ptr
& ~(S_IFMT
| mask
);
2869 /* Of the r and x bits, all (subject to umask) must be present.*/
2870 if ((perm
& 0555) != (i_mode
& 0555))
2873 if (exfat_mode_can_hold_ro(inode
)) {
2875 * Of the w bits, either all (subject to umask) or none must be
2878 if ((perm
& 0222) && ((perm
& 0222) != (0222 & ~mask
)))
2882 * If exfat_mode_can_hold_ro(inode) is false, can't change w
2885 if ((perm
& 0222) != (0222 & ~mask
))
2889 *mode_ptr
&= S_IFMT
| perm
;
2894 static void exfat_truncate(struct inode
*inode
, loff_t old_size
)
2896 struct super_block
*sb
= inode
->i_sb
;
2897 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2898 struct fs_info_t
*p_fs
= &(sbi
->fs_info
);
2904 * This protects against truncating a file bigger than it was then
2905 * trying to write into the hole.
2907 if (EXFAT_I(inode
)->mmu_private
> i_size_read(inode
))
2908 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
2910 if (EXFAT_I(inode
)->fid
.start_clu
== 0)
2913 err
= ffsTruncateFile(inode
, old_size
, i_size_read(inode
));
2917 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
2918 if (IS_DIRSYNC(inode
))
2919 (void)exfat_sync_inode(inode
);
2921 mark_inode_dirty(inode
);
2923 inode
->i_blocks
= ((i_size_read(inode
) + (p_fs
->cluster_size
- 1)) &
2924 ~((loff_t
)p_fs
->cluster_size
- 1)) >> 9;
2929 static int exfat_setattr(struct dentry
*dentry
, struct iattr
*attr
)
2931 struct exfat_sb_info
*sbi
= EXFAT_SB(dentry
->d_sb
);
2932 struct inode
*inode
= dentry
->d_inode
;
2933 unsigned int ia_valid
;
2937 pr_debug("%s entered\n", __func__
);
2939 if ((attr
->ia_valid
& ATTR_SIZE
)
2940 && (attr
->ia_size
> i_size_read(inode
))) {
2941 error
= exfat_cont_expand(inode
, attr
->ia_size
);
2942 if (error
|| attr
->ia_valid
== ATTR_SIZE
)
2944 attr
->ia_valid
&= ~ATTR_SIZE
;
2947 ia_valid
= attr
->ia_valid
;
2949 if ((ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
| ATTR_TIMES_SET
))
2950 && exfat_allow_set_time(sbi
, inode
)) {
2951 attr
->ia_valid
&= ~(ATTR_MTIME_SET
|
2956 error
= setattr_prepare(dentry
, attr
);
2957 attr
->ia_valid
= ia_valid
;
2961 if (((attr
->ia_valid
& ATTR_UID
) &&
2962 (!uid_eq(attr
->ia_uid
, sbi
->options
.fs_uid
))) ||
2963 ((attr
->ia_valid
& ATTR_GID
) &&
2964 (!gid_eq(attr
->ia_gid
, sbi
->options
.fs_gid
))) ||
2965 ((attr
->ia_valid
& ATTR_MODE
) &&
2966 (attr
->ia_mode
& ~(S_IFREG
| S_IFLNK
| S_IFDIR
| 0777)))) {
2971 * We don't return -EPERM here. Yes, strange, but this is too
2974 if (attr
->ia_valid
& ATTR_MODE
) {
2975 if (exfat_sanitize_mode(sbi
, inode
, &attr
->ia_mode
) < 0)
2976 attr
->ia_valid
&= ~ATTR_MODE
;
2979 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2981 if (attr
->ia_valid
& ATTR_SIZE
) {
2982 old_size
= i_size_read(inode
);
2983 down_write(&EXFAT_I(inode
)->truncate_lock
);
2984 truncate_setsize(inode
, attr
->ia_size
);
2985 exfat_truncate(inode
, old_size
);
2986 up_write(&EXFAT_I(inode
)->truncate_lock
);
2988 setattr_copy(inode
, attr
);
2989 mark_inode_dirty(inode
);
2991 pr_debug("%s exited\n", __func__
);
2995 static int exfat_getattr(const struct path
*path
, struct kstat
*stat
,
2996 u32 request_mask
, unsigned int flags
)
2998 struct inode
*inode
= path
->dentry
->d_inode
;
3000 pr_debug("%s entered\n", __func__
);
3002 generic_fillattr(inode
, stat
);
3003 stat
->blksize
= EXFAT_SB(inode
->i_sb
)->fs_info
.cluster_size
;
3005 pr_debug("%s exited\n", __func__
);
3009 static const struct inode_operations exfat_dir_inode_operations
= {
3010 .create
= exfat_create
,
3011 .lookup
= exfat_lookup
,
3012 .unlink
= exfat_unlink
,
3013 .symlink
= exfat_symlink
,
3014 .mkdir
= exfat_mkdir
,
3015 .rmdir
= exfat_rmdir
,
3016 .rename
= exfat_rename
,
3017 .setattr
= exfat_setattr
,
3018 .getattr
= exfat_getattr
,
3021 /*======================================================================*/
3022 /* File Operations */
3023 /*======================================================================*/
3024 static const char *exfat_get_link(struct dentry
*dentry
, struct inode
*inode
,
3025 struct delayed_call
*done
)
3027 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
3030 char *cookie
= ei
->target
;
3033 return (char *)(ei
->target
);
3038 static const struct inode_operations exfat_symlink_inode_operations
= {
3039 .get_link
= exfat_get_link
,
3042 static int exfat_file_release(struct inode
*inode
, struct file
*filp
)
3044 struct super_block
*sb
= inode
->i_sb
;
3046 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
3047 ffsSyncVol(sb
, false);
3051 static const struct file_operations exfat_file_operations
= {
3052 .llseek
= generic_file_llseek
,
3053 .read_iter
= generic_file_read_iter
,
3054 .write_iter
= generic_file_write_iter
,
3055 .mmap
= generic_file_mmap
,
3056 .release
= exfat_file_release
,
3057 .unlocked_ioctl
= exfat_generic_ioctl
,
3058 .fsync
= generic_file_fsync
,
3059 .splice_read
= generic_file_splice_read
,
3062 static const struct inode_operations exfat_file_inode_operations
= {
3063 .setattr
= exfat_setattr
,
3064 .getattr
= exfat_getattr
,
3067 /*======================================================================*/
3068 /* Address Space Operations */
3069 /*======================================================================*/
3071 static int exfat_bmap(struct inode
*inode
, sector_t sector
, sector_t
*phys
,
3072 unsigned long *mapped_blocks
, int *create
)
3074 struct super_block
*sb
= inode
->i_sb
;
3075 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3076 struct fs_info_t
*p_fs
= &(sbi
->fs_info
);
3077 struct bd_info_t
*p_bd
= &(sbi
->bd_info
);
3078 const unsigned long blocksize
= sb
->s_blocksize
;
3079 const unsigned char blocksize_bits
= sb
->s_blocksize_bits
;
3080 sector_t last_block
;
3081 int err
, clu_offset
, sec_offset
;
3082 unsigned int cluster
;
3087 if ((p_fs
->vol_type
== FAT12
) || (p_fs
->vol_type
== FAT16
)) {
3088 if (inode
->i_ino
== EXFAT_ROOT_INO
) {
3090 (p_fs
->dentries_in_root
>>
3091 (p_bd
->sector_size_bits
- DENTRY_SIZE_BITS
))) {
3092 *phys
= sector
+ p_fs
->root_start_sector
;
3099 last_block
= (i_size_read(inode
) + (blocksize
- 1)) >> blocksize_bits
;
3100 if (sector
>= last_block
) {
3107 /* cluster offset */
3108 clu_offset
= sector
>> p_fs
->sectors_per_clu_bits
;
3110 /* sector offset in cluster */
3111 sec_offset
= sector
& (p_fs
->sectors_per_clu
- 1);
3113 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
3115 err
= ffsMapCluster(inode
, clu_offset
, &cluster
);
3118 if (err
== FFS_FULL
)
3122 } else if (cluster
!= CLUSTER_32(~0)) {
3123 *phys
= START_SECTOR(cluster
) + sec_offset
;
3124 *mapped_blocks
= p_fs
->sectors_per_clu
- sec_offset
;
3130 static int exfat_get_block(struct inode
*inode
, sector_t iblock
,
3131 struct buffer_head
*bh_result
, int create
)
3133 struct super_block
*sb
= inode
->i_sb
;
3134 unsigned long max_blocks
= bh_result
->b_size
>> inode
->i_blkbits
;
3136 unsigned long mapped_blocks
;
3141 err
= exfat_bmap(inode
, iblock
, &phys
, &mapped_blocks
, &create
);
3148 max_blocks
= min(mapped_blocks
, max_blocks
);
3150 EXFAT_I(inode
)->mmu_private
+= max_blocks
<<
3151 sb
->s_blocksize_bits
;
3152 set_buffer_new(bh_result
);
3154 map_bh(bh_result
, sb
, phys
);
3157 bh_result
->b_size
= max_blocks
<< sb
->s_blocksize_bits
;
3163 static int exfat_readpage(struct file
*file
, struct page
*page
)
3165 return mpage_readpage(page
, exfat_get_block
);
3168 static int exfat_readpages(struct file
*file
, struct address_space
*mapping
,
3169 struct list_head
*pages
, unsigned int nr_pages
)
3171 return mpage_readpages(mapping
, pages
, nr_pages
, exfat_get_block
);
3174 static int exfat_writepage(struct page
*page
, struct writeback_control
*wbc
)
3176 return block_write_full_page(page
, exfat_get_block
, wbc
);
3179 static int exfat_writepages(struct address_space
*mapping
,
3180 struct writeback_control
*wbc
)
3182 return mpage_writepages(mapping
, wbc
, exfat_get_block
);
3185 static void exfat_write_failed(struct address_space
*mapping
, loff_t to
)
3187 struct inode
*inode
= mapping
->host
;
3189 if (to
> i_size_read(inode
)) {
3190 truncate_pagecache(inode
, i_size_read(inode
));
3191 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
3192 exfat_truncate(inode
, i_size_read(inode
));
3196 static int exfat_write_begin(struct file
*file
, struct address_space
*mapping
,
3197 loff_t pos
, unsigned int len
, unsigned int flags
,
3198 struct page
**pagep
, void **fsdata
)
3203 ret
= cont_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
3205 &EXFAT_I(mapping
->host
)->mmu_private
);
3208 exfat_write_failed(mapping
, pos
+ len
);
3212 static int exfat_write_end(struct file
*file
, struct address_space
*mapping
,
3213 loff_t pos
, unsigned int len
, unsigned int copied
,
3214 struct page
*pagep
, void *fsdata
)
3216 struct inode
*inode
= mapping
->host
;
3217 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
3220 err
= generic_write_end(file
, mapping
, pos
, len
, copied
, pagep
, fsdata
);
3223 exfat_write_failed(mapping
, pos
+ len
);
3225 if (!(err
< 0) && !(fid
->attr
& ATTR_ARCHIVE
)) {
3226 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
3227 fid
->attr
|= ATTR_ARCHIVE
;
3228 mark_inode_dirty(inode
);
3233 static ssize_t
exfat_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
3235 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
3236 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
3240 rw
= iov_iter_rw(iter
);
3243 if (EXFAT_I(inode
)->mmu_private
< iov_iter_count(iter
))
3246 ret
= blockdev_direct_IO(iocb
, inode
, iter
, exfat_get_block
);
3248 if ((ret
< 0) && (rw
& WRITE
))
3249 exfat_write_failed(mapping
, iov_iter_count(iter
));
3253 static sector_t
_exfat_bmap(struct address_space
*mapping
, sector_t block
)
3257 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
3258 down_read(&EXFAT_I(mapping
->host
)->truncate_lock
);
3259 blocknr
= generic_block_bmap(mapping
, block
, exfat_get_block
);
3260 up_read(&EXFAT_I(mapping
->host
)->truncate_lock
);
3265 static const struct address_space_operations exfat_aops
= {
3266 .readpage
= exfat_readpage
,
3267 .readpages
= exfat_readpages
,
3268 .writepage
= exfat_writepage
,
3269 .writepages
= exfat_writepages
,
3270 .write_begin
= exfat_write_begin
,
3271 .write_end
= exfat_write_end
,
3272 .direct_IO
= exfat_direct_IO
,
3276 /*======================================================================*/
3277 /* Super Operations */
3278 /*======================================================================*/
3280 static struct inode
*exfat_iget(struct super_block
*sb
, loff_t i_pos
)
3282 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3283 struct exfat_inode_info
*info
;
3284 struct hlist_head
*head
= sbi
->inode_hashtable
+ exfat_hash(i_pos
);
3285 struct inode
*inode
= NULL
;
3287 spin_lock(&sbi
->inode_hash_lock
);
3288 hlist_for_each_entry(info
, head
, i_hash_fat
) {
3289 BUG_ON(info
->vfs_inode
.i_sb
!= sb
);
3291 if (i_pos
!= info
->i_pos
)
3293 inode
= igrab(&info
->vfs_inode
);
3297 spin_unlock(&sbi
->inode_hash_lock
);
3301 /* doesn't deal with root inode */
3302 static int exfat_fill_inode(struct inode
*inode
, struct file_id_t
*fid
)
3304 struct exfat_sb_info
*sbi
= EXFAT_SB(inode
->i_sb
);
3305 struct fs_info_t
*p_fs
= &(sbi
->fs_info
);
3306 struct dir_entry_t info
;
3308 memcpy(&(EXFAT_I(inode
)->fid
), fid
, sizeof(struct file_id_t
));
3310 ffsReadStat(inode
, &info
);
3312 EXFAT_I(inode
)->i_pos
= 0;
3313 EXFAT_I(inode
)->target
= NULL
;
3314 inode
->i_uid
= sbi
->options
.fs_uid
;
3315 inode
->i_gid
= sbi
->options
.fs_gid
;
3316 INC_IVERSION(inode
);
3317 inode
->i_generation
= get_seconds();
3319 if (info
.Attr
& ATTR_SUBDIR
) { /* directory */
3320 inode
->i_generation
&= ~1;
3321 inode
->i_mode
= exfat_make_mode(sbi
, info
.Attr
, 0777);
3322 inode
->i_op
= &exfat_dir_inode_operations
;
3323 inode
->i_fop
= &exfat_dir_operations
;
3325 i_size_write(inode
, info
.Size
);
3326 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3327 set_nlink(inode
, info
.NumSubdirs
);
3328 } else if (info
.Attr
& ATTR_SYMLINK
) { /* symbolic link */
3329 inode
->i_generation
|= 1;
3330 inode
->i_mode
= exfat_make_mode(sbi
, info
.Attr
, 0777);
3331 inode
->i_op
= &exfat_symlink_inode_operations
;
3333 i_size_write(inode
, info
.Size
);
3334 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3335 } else { /* regular file */
3336 inode
->i_generation
|= 1;
3337 inode
->i_mode
= exfat_make_mode(sbi
, info
.Attr
, 0777);
3338 inode
->i_op
= &exfat_file_inode_operations
;
3339 inode
->i_fop
= &exfat_file_operations
;
3340 inode
->i_mapping
->a_ops
= &exfat_aops
;
3341 inode
->i_mapping
->nrpages
= 0;
3343 i_size_write(inode
, info
.Size
);
3344 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3346 exfat_save_attr(inode
, info
.Attr
);
3348 inode
->i_blocks
= ((i_size_read(inode
) + (p_fs
->cluster_size
- 1))
3349 & ~((loff_t
)p_fs
->cluster_size
- 1)) >> 9;
3351 exfat_time_fat2unix(&inode
->i_mtime
, &info
.ModifyTimestamp
);
3352 exfat_time_fat2unix(&inode
->i_ctime
, &info
.CreateTimestamp
);
3353 exfat_time_fat2unix(&inode
->i_atime
, &info
.AccessTimestamp
);
3358 static struct inode
*exfat_build_inode(struct super_block
*sb
,
3359 struct file_id_t
*fid
, loff_t i_pos
)
3361 struct inode
*inode
;
3364 inode
= exfat_iget(sb
, i_pos
);
3367 inode
= new_inode(sb
);
3369 inode
= ERR_PTR(-ENOMEM
);
3372 inode
->i_ino
= iunique(sb
, EXFAT_ROOT_INO
);
3373 SET_IVERSION(inode
, 1);
3374 err
= exfat_fill_inode(inode
, fid
);
3377 inode
= ERR_PTR(err
);
3380 exfat_attach(inode
, i_pos
);
3381 insert_inode_hash(inode
);
3386 static int exfat_sync_inode(struct inode
*inode
)
3388 return exfat_write_inode(inode
, NULL
);
3391 static struct inode
*exfat_alloc_inode(struct super_block
*sb
)
3393 struct exfat_inode_info
*ei
;
3395 ei
= kmem_cache_alloc(exfat_inode_cachep
, GFP_NOFS
);
3399 init_rwsem(&ei
->truncate_lock
);
3401 return &ei
->vfs_inode
;
3404 static void exfat_destroy_inode(struct inode
*inode
)
3406 kfree(EXFAT_I(inode
)->target
);
3407 EXFAT_I(inode
)->target
= NULL
;
3409 kmem_cache_free(exfat_inode_cachep
, EXFAT_I(inode
));
3412 static int exfat_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
3414 struct dir_entry_t info
;
3416 if (inode
->i_ino
== EXFAT_ROOT_INO
)
3419 info
.Attr
= exfat_make_attr(inode
);
3420 info
.Size
= i_size_read(inode
);
3422 exfat_time_unix2fat(&inode
->i_mtime
, &info
.ModifyTimestamp
);
3423 exfat_time_unix2fat(&inode
->i_ctime
, &info
.CreateTimestamp
);
3424 exfat_time_unix2fat(&inode
->i_atime
, &info
.AccessTimestamp
);
3426 ffsWriteStat(inode
, &info
);
3431 static void exfat_evict_inode(struct inode
*inode
)
3433 truncate_inode_pages(&inode
->i_data
, 0);
3435 if (!inode
->i_nlink
)
3436 i_size_write(inode
, 0);
3437 invalidate_inode_buffers(inode
);
3439 exfat_detach(inode
);
3441 remove_inode_hash(inode
);
3444 static void exfat_free_super(struct exfat_sb_info
*sbi
)
3447 unload_nls(sbi
->nls_disk
);
3449 unload_nls(sbi
->nls_io
);
3450 if (sbi
->options
.iocharset
!= exfat_default_iocharset
)
3451 kfree(sbi
->options
.iocharset
);
3452 /* mutex_init is in exfat_fill_super function. only for 3.7+ */
3453 mutex_destroy(&sbi
->s_lock
);
3457 static void exfat_put_super(struct super_block
*sb
)
3459 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3461 if (__is_sb_dirty(sb
))
3462 exfat_write_super(sb
);
3466 sb
->s_fs_info
= NULL
;
3467 exfat_free_super(sbi
);
3470 static void exfat_write_super(struct super_block
*sb
)
3477 ffsSyncVol(sb
, true);
3482 static int exfat_sync_fs(struct super_block
*sb
, int wait
)
3486 if (__is_sb_dirty(sb
)) {
3489 err
= ffsSyncVol(sb
, true);
3496 static int exfat_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
3498 struct super_block
*sb
= dentry
->d_sb
;
3499 u64 id
= huge_encode_dev(sb
->s_bdev
->bd_dev
);
3500 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
3501 struct vol_info_t info
;
3503 if (p_fs
->used_clusters
== UINT_MAX
) {
3504 if (ffsGetVolInfo(sb
, &info
) == FFS_MEDIAERR
)
3508 info
.FatType
= p_fs
->vol_type
;
3509 info
.ClusterSize
= p_fs
->cluster_size
;
3510 info
.NumClusters
= p_fs
->num_clusters
- 2;
3511 info
.UsedClusters
= p_fs
->used_clusters
;
3512 info
.FreeClusters
= info
.NumClusters
- info
.UsedClusters
;
3514 if (p_fs
->dev_ejected
)
3515 pr_info("[EXFAT] statfs on device that is ejected\n");
3518 buf
->f_type
= sb
->s_magic
;
3519 buf
->f_bsize
= info
.ClusterSize
;
3520 buf
->f_blocks
= info
.NumClusters
;
3521 buf
->f_bfree
= info
.FreeClusters
;
3522 buf
->f_bavail
= info
.FreeClusters
;
3523 buf
->f_fsid
.val
[0] = (u32
)id
;
3524 buf
->f_fsid
.val
[1] = (u32
)(id
>> 32);
3525 buf
->f_namelen
= 260;
3530 static int exfat_remount(struct super_block
*sb
, int *flags
, char *data
)
3532 *flags
|= SB_NODIRATIME
;
3536 static int exfat_show_options(struct seq_file
*m
, struct dentry
*root
)
3538 struct exfat_sb_info
*sbi
= EXFAT_SB(root
->d_sb
);
3539 struct exfat_mount_options
*opts
= &sbi
->options
;
3541 if (__kuid_val(opts
->fs_uid
))
3542 seq_printf(m
, ",uid=%u", __kuid_val(opts
->fs_uid
));
3543 if (__kgid_val(opts
->fs_gid
))
3544 seq_printf(m
, ",gid=%u", __kgid_val(opts
->fs_gid
));
3545 seq_printf(m
, ",fmask=%04o", opts
->fs_fmask
);
3546 seq_printf(m
, ",dmask=%04o", opts
->fs_dmask
);
3547 if (opts
->allow_utime
)
3548 seq_printf(m
, ",allow_utime=%04o", opts
->allow_utime
);
3550 seq_printf(m
, ",codepage=%s", sbi
->nls_disk
->charset
);
3552 seq_printf(m
, ",iocharset=%s", sbi
->nls_io
->charset
);
3553 seq_printf(m
, ",namecase=%u", opts
->casesensitive
);
3554 if (opts
->errors
== EXFAT_ERRORS_CONT
)
3555 seq_puts(m
, ",errors=continue");
3556 else if (opts
->errors
== EXFAT_ERRORS_PANIC
)
3557 seq_puts(m
, ",errors=panic");
3559 seq_puts(m
, ",errors=remount-ro");
3560 #ifdef CONFIG_EXFAT_DISCARD
3562 seq_puts(m
, ",discard");
3567 static const struct super_operations exfat_sops
= {
3568 .alloc_inode
= exfat_alloc_inode
,
3569 .destroy_inode
= exfat_destroy_inode
,
3570 .write_inode
= exfat_write_inode
,
3571 .evict_inode
= exfat_evict_inode
,
3572 .put_super
= exfat_put_super
,
3573 .sync_fs
= exfat_sync_fs
,
3574 .statfs
= exfat_statfs
,
3575 .remount_fs
= exfat_remount
,
3576 .show_options
= exfat_show_options
,
3579 /*======================================================================*/
3580 /* Export Operations */
3581 /*======================================================================*/
3583 static struct inode
*exfat_nfs_get_inode(struct super_block
*sb
, u64 ino
,
3586 struct inode
*inode
= NULL
;
3588 if (ino
< EXFAT_ROOT_INO
)
3590 inode
= ilookup(sb
, ino
);
3592 if (inode
&& generation
&& (inode
->i_generation
!= generation
)) {
3600 static struct dentry
*exfat_fh_to_dentry(struct super_block
*sb
,
3601 struct fid
*fid
, int fh_len
,
3604 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
3605 exfat_nfs_get_inode
);
3608 static struct dentry
*exfat_fh_to_parent(struct super_block
*sb
,
3609 struct fid
*fid
, int fh_len
,
3612 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
3613 exfat_nfs_get_inode
);
3616 static const struct export_operations exfat_export_ops
= {
3617 .fh_to_dentry
= exfat_fh_to_dentry
,
3618 .fh_to_parent
= exfat_fh_to_parent
,
3621 /*======================================================================*/
3622 /* Super Block Read Operations */
3623 /*======================================================================*/
3641 #ifdef CONFIG_EXFAT_DISCARD
3643 #endif /* EXFAT_CONFIG_DISCARD */
3646 static const match_table_t exfat_tokens
= {
3647 {Opt_uid
, "uid=%u"},
3648 {Opt_gid
, "gid=%u"},
3649 {Opt_umask
, "umask=%o"},
3650 {Opt_dmask
, "dmask=%o"},
3651 {Opt_fmask
, "fmask=%o"},
3652 {Opt_allow_utime
, "allow_utime=%o"},
3653 {Opt_codepage
, "codepage=%u"},
3654 {Opt_charset
, "iocharset=%s"},
3655 {Opt_namecase
, "namecase=%u"},
3656 {Opt_debug
, "debug"},
3657 {Opt_err_cont
, "errors=continue"},
3658 {Opt_err_panic
, "errors=panic"},
3659 {Opt_err_ro
, "errors=remount-ro"},
3660 {Opt_utf8_hack
, "utf8"},
3661 #ifdef CONFIG_EXFAT_DISCARD
3662 {Opt_discard
, "discard"},
3663 #endif /* CONFIG_EXFAT_DISCARD */
3667 static int parse_options(char *options
, int silent
, int *debug
,
3668 struct exfat_mount_options
*opts
)
3671 substring_t args
[MAX_OPT_ARGS
];
3675 opts
->fs_uid
= current_uid();
3676 opts
->fs_gid
= current_gid();
3677 opts
->fs_fmask
= opts
->fs_dmask
= current
->fs
->umask
;
3678 opts
->allow_utime
= U16_MAX
;
3679 opts
->codepage
= exfat_default_codepage
;
3680 opts
->iocharset
= exfat_default_iocharset
;
3681 opts
->casesensitive
= 0;
3682 opts
->errors
= EXFAT_ERRORS_RO
;
3683 #ifdef CONFIG_EXFAT_DISCARD
3691 while ((p
= strsep(&options
, ","))) {
3697 token
= match_token(p
, exfat_tokens
, args
);
3700 if (match_int(&args
[0], &option
))
3702 opts
->fs_uid
= KUIDT_INIT(option
);
3705 if (match_int(&args
[0], &option
))
3707 opts
->fs_gid
= KGIDT_INIT(option
);
3712 if (match_octal(&args
[0], &option
))
3714 if (token
!= Opt_dmask
)
3715 opts
->fs_fmask
= option
;
3716 if (token
!= Opt_fmask
)
3717 opts
->fs_dmask
= option
;
3719 case Opt_allow_utime
:
3720 if (match_octal(&args
[0], &option
))
3722 opts
->allow_utime
= option
& 0022;
3725 if (match_int(&args
[0], &option
))
3727 opts
->codepage
= option
;
3730 if (opts
->iocharset
!= exfat_default_iocharset
)
3731 kfree(opts
->iocharset
);
3732 iocharset
= match_strdup(&args
[0]);
3735 opts
->iocharset
= iocharset
;
3738 if (match_int(&args
[0], &option
))
3740 opts
->casesensitive
= option
;
3743 opts
->errors
= EXFAT_ERRORS_CONT
;
3746 opts
->errors
= EXFAT_ERRORS_PANIC
;
3749 opts
->errors
= EXFAT_ERRORS_RO
;
3754 #ifdef CONFIG_EXFAT_DISCARD
3758 #endif /* CONFIG_EXFAT_DISCARD */
3763 pr_err("[EXFAT] Unrecognized mount option %s or missing value\n",
3770 if (opts
->allow_utime
== U16_MAX
)
3771 opts
->allow_utime
= ~opts
->fs_dmask
& 0022;
3776 static void exfat_hash_init(struct super_block
*sb
)
3778 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3781 spin_lock_init(&sbi
->inode_hash_lock
);
3782 for (i
= 0; i
< EXFAT_HASH_SIZE
; i
++)
3783 INIT_HLIST_HEAD(&sbi
->inode_hashtable
[i
]);
3786 static int exfat_read_root(struct inode
*inode
)
3788 struct super_block
*sb
= inode
->i_sb
;
3789 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3790 struct fs_info_t
*p_fs
= &(sbi
->fs_info
);
3791 struct dir_entry_t info
;
3793 EXFAT_I(inode
)->fid
.dir
.dir
= p_fs
->root_dir
;
3794 EXFAT_I(inode
)->fid
.dir
.flags
= 0x01;
3795 EXFAT_I(inode
)->fid
.entry
= -1;
3796 EXFAT_I(inode
)->fid
.start_clu
= p_fs
->root_dir
;
3797 EXFAT_I(inode
)->fid
.flags
= 0x01;
3798 EXFAT_I(inode
)->fid
.type
= TYPE_DIR
;
3799 EXFAT_I(inode
)->fid
.rwoffset
= 0;
3800 EXFAT_I(inode
)->fid
.hint_last_off
= -1;
3802 EXFAT_I(inode
)->target
= NULL
;
3804 ffsReadStat(inode
, &info
);
3806 inode
->i_uid
= sbi
->options
.fs_uid
;
3807 inode
->i_gid
= sbi
->options
.fs_gid
;
3808 INC_IVERSION(inode
);
3809 inode
->i_generation
= 0;
3810 inode
->i_mode
= exfat_make_mode(sbi
, ATTR_SUBDIR
, 0777);
3811 inode
->i_op
= &exfat_dir_inode_operations
;
3812 inode
->i_fop
= &exfat_dir_operations
;
3814 i_size_write(inode
, info
.Size
);
3815 inode
->i_blocks
= ((i_size_read(inode
) + (p_fs
->cluster_size
- 1))
3816 & ~((loff_t
)p_fs
->cluster_size
- 1)) >> 9;
3817 EXFAT_I(inode
)->i_pos
= ((loff_t
)p_fs
->root_dir
<< 32) | 0xffffffff;
3818 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3820 exfat_save_attr(inode
, ATTR_SUBDIR
);
3821 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
3822 set_nlink(inode
, info
.NumSubdirs
+ 2);
3827 static void setup_dops(struct super_block
*sb
)
3829 if (EXFAT_SB(sb
)->options
.casesensitive
== 0)
3830 sb
->s_d_op
= &exfat_ci_dentry_ops
;
3832 sb
->s_d_op
= &exfat_dentry_ops
;
3835 static int exfat_fill_super(struct super_block
*sb
, void *data
, int silent
)
3837 struct inode
*root_inode
= NULL
;
3838 struct exfat_sb_info
*sbi
;
3844 * GFP_KERNEL is ok here, because while we do hold the
3845 * supeblock lock, memory pressure can't call back into
3846 * the filesystem, since we're only just about to mount
3847 * it and have no inodes etc active!
3849 sbi
= kvzalloc(sizeof(*sbi
), GFP_KERNEL
);
3852 mutex_init(&sbi
->s_lock
);
3853 sb
->s_fs_info
= sbi
;
3854 sb
->s_flags
|= SB_NODIRATIME
;
3855 sb
->s_magic
= EXFAT_SUPER_MAGIC
;
3856 sb
->s_op
= &exfat_sops
;
3857 sb
->s_export_op
= &exfat_export_ops
;
3859 error
= parse_options(data
, silent
, &debug
, &sbi
->options
);
3866 sb_min_blocksize(sb
, 512);
3867 sb
->s_maxbytes
= 0x7fffffffffffffffLL
; /* maximum file size */
3869 ret
= ffsMountVol(sb
);
3872 pr_err("[EXFAT] ffsMountVol failed\n");
3877 /* set up enough so that it can read an inode */
3878 exfat_hash_init(sb
);
3881 * The low byte of FAT's first entry must have same value with
3882 * media-field. But in real world, too many devices is
3883 * writing wrong value. So, removed that validity check.
3885 * if (FAT_FIRST_ENT(sb, media) != first)
3888 /* codepage is not meaningful in exfat */
3889 if (sbi
->fs_info
.vol_type
!= EXFAT
) {
3891 sprintf(buf
, "cp%d", sbi
->options
.codepage
);
3892 sbi
->nls_disk
= load_nls(buf
);
3893 if (!sbi
->nls_disk
) {
3894 pr_err("[EXFAT] Codepage %s not found\n", buf
);
3899 sbi
->nls_io
= load_nls(sbi
->options
.iocharset
);
3902 root_inode
= new_inode(sb
);
3905 root_inode
->i_ino
= EXFAT_ROOT_INO
;
3906 SET_IVERSION(root_inode
, 1);
3908 error
= exfat_read_root(root_inode
);
3912 exfat_attach(root_inode
, EXFAT_I(root_inode
)->i_pos
);
3913 insert_inode_hash(root_inode
);
3914 sb
->s_root
= d_make_root(root_inode
);
3916 pr_err("[EXFAT] Getting the root inode failed\n");
3927 sb
->s_fs_info
= NULL
;
3928 exfat_free_super(sbi
);
3932 static struct dentry
*exfat_fs_mount(struct file_system_type
*fs_type
,
3933 int flags
, const char *dev_name
,
3936 return mount_bdev(fs_type
, flags
, dev_name
, data
, exfat_fill_super
);
3939 static void init_once(void *foo
)
3941 struct exfat_inode_info
*ei
= (struct exfat_inode_info
*)foo
;
3943 INIT_HLIST_NODE(&ei
->i_hash_fat
);
3944 inode_init_once(&ei
->vfs_inode
);
3947 static int __init
exfat_init_inodecache(void)
3949 exfat_inode_cachep
= kmem_cache_create("exfat_inode_cache",
3950 sizeof(struct exfat_inode_info
),
3952 (SLAB_RECLAIM_ACCOUNT
|
3955 if (!exfat_inode_cachep
)
3960 static void __exit
exfat_destroy_inodecache(void)
3963 * Make sure all delayed rcu free inodes are flushed before we
3967 kmem_cache_destroy(exfat_inode_cachep
);
3970 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
3971 static void exfat_debug_kill_sb(struct super_block
*sb
)
3973 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3974 struct block_device
*bdev
= sb
->s_bdev
;
3975 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
3980 flags
= sbi
->debug_flags
;
3982 if (flags
& EXFAT_DEBUGFLAGS_INVALID_UMOUNT
) {
3984 * invalidate_bdev drops all device cache include
3985 * dirty. We use this to simulate device removal.
3988 FAT_release_all(sb
);
3989 buf_release_all(sb
);
3992 invalidate_bdev(bdev
);
3996 kill_block_super(sb
);
3998 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
4000 static struct file_system_type exfat_fs_type
= {
4001 .owner
= THIS_MODULE
,
4003 .mount
= exfat_fs_mount
,
4004 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
4005 .kill_sb
= exfat_debug_kill_sb
,
4007 .kill_sb
= kill_block_super
,
4008 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
4009 .fs_flags
= FS_REQUIRES_DEV
,
4012 static int __init
init_exfat(void)
4016 BUILD_BUG_ON(sizeof(struct dentry_t
) != DENTRY_SIZE
);
4017 BUILD_BUG_ON(sizeof(struct dos_dentry_t
) != DENTRY_SIZE
);
4018 BUILD_BUG_ON(sizeof(struct ext_dentry_t
) != DENTRY_SIZE
);
4019 BUILD_BUG_ON(sizeof(struct file_dentry_t
) != DENTRY_SIZE
);
4020 BUILD_BUG_ON(sizeof(struct strm_dentry_t
) != DENTRY_SIZE
);
4021 BUILD_BUG_ON(sizeof(struct name_dentry_t
) != DENTRY_SIZE
);
4022 BUILD_BUG_ON(sizeof(struct bmap_dentry_t
) != DENTRY_SIZE
);
4023 BUILD_BUG_ON(sizeof(struct case_dentry_t
) != DENTRY_SIZE
);
4024 BUILD_BUG_ON(sizeof(struct volm_dentry_t
) != DENTRY_SIZE
);
4026 pr_info("exFAT: Version %s\n", EXFAT_VERSION
);
4028 err
= exfat_init_inodecache();
4032 err
= register_filesystem(&exfat_fs_type
);
4039 static void __exit
exit_exfat(void)
4041 exfat_destroy_inodecache();
4042 unregister_filesystem(&exfat_fs_type
);
4045 module_init(init_exfat
);
4046 module_exit(exit_exfat
);
4048 MODULE_LICENSE("GPL");
4049 MODULE_DESCRIPTION("exFAT Filesystem Driver");
4050 MODULE_ALIAS_FS("exfat");