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>
29 #include <linux/random.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_MUTEX(z_mutex
);
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");
356 mutex_lock(&z_mutex
);
360 mutex_init(&p_fs
->v_mutex
);
361 p_fs
->dev_ejected
= 0;
363 /* open the block device */
366 if (p_bd
->sector_size
< sb
->s_blocksize
) {
367 printk(KERN_INFO
"EXFAT: mount failed - sector size %d less than blocksize %ld\n",
368 p_bd
->sector_size
, sb
->s_blocksize
);
372 if (p_bd
->sector_size
> sb
->s_blocksize
)
373 sb_set_blocksize(sb
, p_bd
->sector_size
);
376 if (sector_read(sb
, 0, &tmp_bh
, 1) != 0) {
381 p_fs
->PBR_sector
= 0;
383 p_pbr
= (struct pbr_sector_t
*)tmp_bh
->b_data
;
385 /* check the validity of PBR */
386 if (GET16_A(p_pbr
->signature
) != PBR_SIGNATURE
) {
388 exfat_bdev_close(sb
);
394 for (i
= 0; i
< 53; i
++)
399 /* Not sure how we'd get here, but complain if it does */
401 pr_info("EXFAT: Attempted to mount VFAT filesystem\n");
404 ret
= exfat_mount(sb
, p_pbr
);
410 exfat_bdev_close(sb
);
414 ret
= load_alloc_bitmap(sb
);
416 exfat_bdev_close(sb
);
419 ret
= load_upcase_table(sb
);
421 free_alloc_bitmap(sb
);
422 exfat_bdev_close(sb
);
426 if (p_fs
->dev_ejected
) {
427 free_upcase_table(sb
);
428 free_alloc_bitmap(sb
);
429 exfat_bdev_close(sb
);
434 pr_info("[EXFAT] mounted successfully\n");
437 mutex_unlock(&z_mutex
);
442 static int ffsUmountVol(struct super_block
*sb
)
444 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
447 pr_info("[EXFAT] trying to unmount...\n");
449 mutex_lock(&z_mutex
);
451 /* acquire the lock for file system critical section */
452 mutex_lock(&p_fs
->v_mutex
);
455 fs_set_vol_flags(sb
, VOL_CLEAN
);
457 free_upcase_table(sb
);
458 free_alloc_bitmap(sb
);
460 exfat_fat_release_all(sb
);
461 exfat_buf_release_all(sb
);
463 /* close the block device */
464 exfat_bdev_close(sb
);
466 if (p_fs
->dev_ejected
) {
467 pr_info("[EXFAT] unmounted with media errors. Device is already ejected.\n");
471 exfat_buf_shutdown(sb
);
473 /* release the lock for file system critical section */
474 mutex_unlock(&p_fs
->v_mutex
);
475 mutex_unlock(&z_mutex
);
477 pr_info("[EXFAT] unmounted successfully\n");
482 static int ffsGetVolInfo(struct super_block
*sb
, struct vol_info_t
*info
)
485 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
487 /* check the validity of pointer parameters */
491 /* acquire the lock for file system critical section */
492 mutex_lock(&p_fs
->v_mutex
);
494 if (p_fs
->used_clusters
== UINT_MAX
)
495 p_fs
->used_clusters
= p_fs
->fs_func
->count_used_clusters(sb
);
497 info
->FatType
= p_fs
->vol_type
;
498 info
->ClusterSize
= p_fs
->cluster_size
;
499 info
->NumClusters
= p_fs
->num_clusters
- 2; /* clu 0 & 1 */
500 info
->UsedClusters
= p_fs
->used_clusters
;
501 info
->FreeClusters
= info
->NumClusters
- info
->UsedClusters
;
503 if (p_fs
->dev_ejected
)
506 /* release the lock for file system critical section */
507 mutex_unlock(&p_fs
->v_mutex
);
512 static int ffsSyncVol(struct super_block
*sb
, bool do_sync
)
515 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
517 /* acquire the lock for file system critical section */
518 mutex_lock(&p_fs
->v_mutex
);
520 /* synchronize the file system */
521 fs_sync(sb
, do_sync
);
522 fs_set_vol_flags(sb
, VOL_CLEAN
);
524 if (p_fs
->dev_ejected
)
527 /* release the lock for file system critical section */
528 mutex_unlock(&p_fs
->v_mutex
);
533 /*----------------------------------------------------------------------*/
534 /* File Operation Functions */
535 /*----------------------------------------------------------------------*/
537 static int ffsLookupFile(struct inode
*inode
, char *path
, struct file_id_t
*fid
)
539 int ret
, dentry
, num_entries
;
541 struct uni_name_t uni_name
;
542 struct dos_name_t dos_name
;
543 struct dentry_t
*ep
, *ep2
;
544 struct entry_set_cache_t
*es
= NULL
;
545 struct super_block
*sb
= inode
->i_sb
;
546 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
548 pr_debug("%s entered\n", __func__
);
550 /* check the validity of pointer parameters */
551 if (!fid
|| !path
|| (*path
== '\0'))
554 /* acquire the lock for file system critical section */
555 mutex_lock(&p_fs
->v_mutex
);
557 /* check the validity of directory name in the given pathname */
558 ret
= resolve_path(inode
, path
, &dir
, &uni_name
);
562 ret
= get_num_entries_and_dos_name(sb
, &dir
, &uni_name
, &num_entries
,
567 /* search the file name for directories */
568 dentry
= p_fs
->fs_func
->find_dir_entry(sb
, &dir
, &uni_name
, num_entries
,
569 &dos_name
, TYPE_ALL
);
575 fid
->dir
.dir
= dir
.dir
;
576 fid
->dir
.size
= dir
.size
;
577 fid
->dir
.flags
= dir
.flags
;
581 fid
->type
= TYPE_DIR
;
583 fid
->hint_last_off
= -1;
585 fid
->attr
= ATTR_SUBDIR
;
588 fid
->start_clu
= p_fs
->root_dir
;
590 es
= get_entry_set_in_dir(sb
, &dir
, dentry
,
598 fid
->type
= p_fs
->fs_func
->get_entry_type(ep
);
600 fid
->hint_last_off
= -1;
601 fid
->attr
= p_fs
->fs_func
->get_entry_attr(ep
);
603 fid
->size
= p_fs
->fs_func
->get_entry_size(ep2
);
604 if ((fid
->type
== TYPE_FILE
) && (fid
->size
== 0)) {
605 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
606 fid
->start_clu
= CLUSTER_32(~0);
608 fid
->flags
= p_fs
->fs_func
->get_entry_flag(ep2
);
609 fid
->start_clu
= p_fs
->fs_func
->get_entry_clu0(ep2
);
612 release_entry_set(es
);
615 if (p_fs
->dev_ejected
)
618 /* release the lock for file system critical section */
619 mutex_unlock(&p_fs
->v_mutex
);
624 static int ffsCreateFile(struct inode
*inode
, char *path
, u8 mode
,
625 struct file_id_t
*fid
)
628 struct uni_name_t uni_name
;
629 struct super_block
*sb
= inode
->i_sb
;
630 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
633 /* check the validity of pointer parameters */
634 if (!fid
|| !path
|| (*path
== '\0'))
637 /* acquire the lock for file system critical section */
638 mutex_lock(&p_fs
->v_mutex
);
640 /* check the validity of directory name in the given pathname */
641 ret
= resolve_path(inode
, path
, &dir
, &uni_name
);
645 fs_set_vol_flags(sb
, VOL_DIRTY
);
647 /* create a new file */
648 ret
= create_file(inode
, &dir
, &uni_name
, mode
, fid
);
650 #ifndef CONFIG_EXFAT_DELAYED_SYNC
652 fs_set_vol_flags(sb
, VOL_CLEAN
);
655 if (p_fs
->dev_ejected
)
659 /* release the lock for file system critical section */
660 mutex_unlock(&p_fs
->v_mutex
);
665 static int ffsReadFile(struct inode
*inode
, struct file_id_t
*fid
, void *buffer
,
666 u64 count
, u64
*rcount
)
668 s32 offset
, sec_offset
, clu_offset
;
672 u64 oneblkread
, read_bytes
;
673 struct buffer_head
*tmp_bh
= NULL
;
674 struct super_block
*sb
= inode
->i_sb
;
675 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
676 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
678 /* check the validity of the given file id */
682 /* check the validity of pointer parameters */
686 /* acquire the lock for file system critical section */
687 mutex_lock(&p_fs
->v_mutex
);
689 /* check if the given file ID is opened */
690 if (fid
->type
!= TYPE_FILE
) {
695 if (fid
->rwoffset
> fid
->size
)
696 fid
->rwoffset
= fid
->size
;
698 if (count
> (fid
->size
- fid
->rwoffset
))
699 count
= fid
->size
- fid
->rwoffset
;
711 clu_offset
= (s32
)(fid
->rwoffset
>> p_fs
->cluster_size_bits
);
712 clu
= fid
->start_clu
;
714 if (fid
->flags
== 0x03) {
717 /* hint information */
718 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
719 (clu_offset
>= fid
->hint_last_off
)) {
720 clu_offset
-= fid
->hint_last_off
;
721 clu
= fid
->hint_last_clu
;
724 while (clu_offset
> 0) {
725 /* clu = exfat_fat_read(sb, clu); */
726 if (exfat_fat_read(sb
, clu
, &clu
) == -1) {
735 /* hint information */
736 fid
->hint_last_off
= (s32
)(fid
->rwoffset
>>
737 p_fs
->cluster_size_bits
);
738 fid
->hint_last_clu
= clu
;
740 /* byte offset in cluster */
741 offset
= (s32
)(fid
->rwoffset
& (p_fs
->cluster_size
- 1));
743 /* sector offset in cluster */
744 sec_offset
= offset
>> p_bd
->sector_size_bits
;
746 /* byte offset in sector */
747 offset
&= p_bd
->sector_size_mask
;
749 LogSector
= START_SECTOR(clu
) + sec_offset
;
751 oneblkread
= (u64
)(p_bd
->sector_size
- offset
);
752 if (oneblkread
> count
)
755 if ((offset
== 0) && (oneblkread
== p_bd
->sector_size
)) {
756 if (sector_read(sb
, LogSector
, &tmp_bh
, 1) !=
759 memcpy((char *)buffer
+ read_bytes
,
760 (char *)tmp_bh
->b_data
, (s32
)oneblkread
);
762 if (sector_read(sb
, LogSector
, &tmp_bh
, 1) !=
765 memcpy((char *)buffer
+ read_bytes
,
766 (char *)tmp_bh
->b_data
+ offset
,
770 read_bytes
+= oneblkread
;
771 fid
->rwoffset
+= oneblkread
;
775 /* How did this ever work and not leak a brlse()?? */
777 /* set the size of read bytes */
779 *rcount
= read_bytes
;
781 if (p_fs
->dev_ejected
)
785 /* release the lock for file system critical section */
786 mutex_unlock(&p_fs
->v_mutex
);
791 static int ffsWriteFile(struct inode
*inode
, struct file_id_t
*fid
,
792 void *buffer
, u64 count
, u64
*wcount
)
794 bool modified
= false;
795 s32 offset
, sec_offset
, clu_offset
;
796 s32 num_clusters
, num_alloc
, num_alloced
= (s32
)~0;
800 u64 oneblkwrite
, write_bytes
;
801 struct chain_t new_clu
;
802 struct timestamp_t tm
;
803 struct dentry_t
*ep
, *ep2
;
804 struct entry_set_cache_t
*es
= NULL
;
805 struct buffer_head
*tmp_bh
= NULL
;
806 struct super_block
*sb
= inode
->i_sb
;
807 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
808 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
810 /* check the validity of the given file id */
814 /* check the validity of pointer parameters */
818 /* acquire the lock for file system critical section */
819 mutex_lock(&p_fs
->v_mutex
);
821 /* check if the given file ID is opened */
822 if (fid
->type
!= TYPE_FILE
) {
827 if (fid
->rwoffset
> fid
->size
)
828 fid
->rwoffset
= fid
->size
;
837 fs_set_vol_flags(sb
, VOL_DIRTY
);
842 num_clusters
= (s32
)((fid
->size
- 1) >>
843 p_fs
->cluster_size_bits
) + 1;
848 clu_offset
= (s32
)(fid
->rwoffset
>> p_fs
->cluster_size_bits
);
849 clu
= fid
->start_clu
;
850 last_clu
= fid
->start_clu
;
852 if (fid
->flags
== 0x03) {
853 if ((clu_offset
> 0) && (clu
!= CLUSTER_32(~0))) {
854 last_clu
+= clu_offset
- 1;
856 if (clu_offset
== num_clusters
)
857 clu
= CLUSTER_32(~0);
862 /* hint information */
863 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
864 (clu_offset
>= fid
->hint_last_off
)) {
865 clu_offset
-= fid
->hint_last_off
;
866 clu
= fid
->hint_last_clu
;
869 while ((clu_offset
> 0) && (clu
!= CLUSTER_32(~0))) {
871 /* clu = exfat_fat_read(sb, clu); */
872 if (exfat_fat_read(sb
, clu
, &clu
) == -1) {
880 if (clu
== CLUSTER_32(~0)) {
881 num_alloc
= (s32
)((count
- 1) >>
882 p_fs
->cluster_size_bits
) + 1;
883 new_clu
.dir
= (last_clu
== CLUSTER_32(~0)) ?
884 CLUSTER_32(~0) : last_clu
+ 1;
886 new_clu
.flags
= fid
->flags
;
888 /* (1) allocate a chain of clusters */
889 num_alloced
= p_fs
->fs_func
->alloc_cluster(sb
,
892 if (num_alloced
== 0)
894 if (num_alloced
< 0) {
899 /* (2) append to the FAT chain */
900 if (last_clu
== CLUSTER_32(~0)) {
901 if (new_clu
.flags
== 0x01)
903 fid
->start_clu
= new_clu
.dir
;
906 if (new_clu
.flags
!= fid
->flags
) {
907 exfat_chain_cont_cluster(sb
,
913 if (new_clu
.flags
== 0x01)
914 exfat_fat_write(sb
, last_clu
, new_clu
.dir
);
917 num_clusters
+= num_alloced
;
921 /* hint information */
922 fid
->hint_last_off
= (s32
)(fid
->rwoffset
>>
923 p_fs
->cluster_size_bits
);
924 fid
->hint_last_clu
= clu
;
926 /* byte offset in cluster */
927 offset
= (s32
)(fid
->rwoffset
& (p_fs
->cluster_size
- 1));
929 /* sector offset in cluster */
930 sec_offset
= offset
>> p_bd
->sector_size_bits
;
932 /* byte offset in sector */
933 offset
&= p_bd
->sector_size_mask
;
935 LogSector
= START_SECTOR(clu
) + sec_offset
;
937 oneblkwrite
= (u64
)(p_bd
->sector_size
- offset
);
938 if (oneblkwrite
> count
)
941 if ((offset
== 0) && (oneblkwrite
== p_bd
->sector_size
)) {
942 if (sector_read(sb
, LogSector
, &tmp_bh
, 0) !=
945 memcpy((char *)tmp_bh
->b_data
,
946 (char *)buffer
+ write_bytes
, (s32
)oneblkwrite
);
947 if (sector_write(sb
, LogSector
, tmp_bh
, 0) !=
954 ((fid
->rwoffset
+ oneblkwrite
) < fid
->size
)) {
955 if (sector_read(sb
, LogSector
, &tmp_bh
, 1) !=
959 if (sector_read(sb
, LogSector
, &tmp_bh
, 0) !=
964 memcpy((char *)tmp_bh
->b_data
+ offset
,
965 (char *)buffer
+ write_bytes
, (s32
)oneblkwrite
);
966 if (sector_write(sb
, LogSector
, tmp_bh
, 0) !=
973 count
-= oneblkwrite
;
974 write_bytes
+= oneblkwrite
;
975 fid
->rwoffset
+= oneblkwrite
;
977 fid
->attr
|= ATTR_ARCHIVE
;
979 if (fid
->size
< fid
->rwoffset
) {
980 fid
->size
= fid
->rwoffset
;
987 /* (3) update the direcoty entry */
988 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
989 ES_ALL_ENTRIES
, &ep
);
994 p_fs
->fs_func
->set_entry_time(ep
, tm_current(&tm
), TM_MODIFY
);
995 p_fs
->fs_func
->set_entry_attr(ep
, fid
->attr
);
998 if (p_fs
->fs_func
->get_entry_flag(ep2
) != fid
->flags
)
999 p_fs
->fs_func
->set_entry_flag(ep2
, fid
->flags
);
1001 if (p_fs
->fs_func
->get_entry_size(ep2
) != fid
->size
)
1002 p_fs
->fs_func
->set_entry_size(ep2
, fid
->size
);
1004 if (p_fs
->fs_func
->get_entry_clu0(ep2
) != fid
->start_clu
)
1005 p_fs
->fs_func
->set_entry_clu0(ep2
, fid
->start_clu
);
1008 update_dir_checksum_with_entry_set(sb
, es
);
1009 release_entry_set(es
);
1011 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1013 fs_set_vol_flags(sb
, VOL_CLEAN
);
1017 /* set the size of written bytes */
1019 *wcount
= write_bytes
;
1021 if (num_alloced
== 0)
1024 else if (p_fs
->dev_ejected
)
1028 /* release the lock for file system critical section */
1029 mutex_unlock(&p_fs
->v_mutex
);
1034 static int ffsTruncateFile(struct inode
*inode
, u64 old_size
, u64 new_size
)
1037 u32 last_clu
= CLUSTER_32(0);
1040 struct timestamp_t tm
;
1041 struct dentry_t
*ep
, *ep2
;
1042 struct super_block
*sb
= inode
->i_sb
;
1043 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1044 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1045 struct entry_set_cache_t
*es
= NULL
;
1047 pr_debug("%s entered (inode %p size %llu)\n", __func__
, inode
,
1050 /* acquire the lock for file system critical section */
1051 mutex_lock(&p_fs
->v_mutex
);
1053 /* check if the given file ID is opened */
1054 if (fid
->type
!= TYPE_FILE
) {
1059 if (fid
->size
!= old_size
) {
1060 pr_err("[EXFAT] truncate : can't skip it because of size-mismatch(old:%lld->fid:%lld).\n",
1061 old_size
, fid
->size
);
1064 if (old_size
<= new_size
) {
1069 fs_set_vol_flags(sb
, VOL_DIRTY
);
1071 clu
.dir
= fid
->start_clu
;
1072 clu
.size
= (s32
)((old_size
- 1) >> p_fs
->cluster_size_bits
) + 1;
1073 clu
.flags
= fid
->flags
;
1076 num_clusters
= (s32
)((new_size
- 1) >>
1077 p_fs
->cluster_size_bits
) + 1;
1079 if (clu
.flags
== 0x03) {
1080 clu
.dir
+= num_clusters
;
1082 while (num_clusters
> 0) {
1084 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) == -1) {
1092 clu
.size
-= num_clusters
;
1095 fid
->size
= new_size
;
1096 fid
->attr
|= ATTR_ARCHIVE
;
1097 if (new_size
== 0) {
1098 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
1099 fid
->start_clu
= CLUSTER_32(~0);
1102 /* (1) update the directory entry */
1103 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
1104 ES_ALL_ENTRIES
, &ep
);
1111 p_fs
->fs_func
->set_entry_time(ep
, tm_current(&tm
), TM_MODIFY
);
1112 p_fs
->fs_func
->set_entry_attr(ep
, fid
->attr
);
1114 p_fs
->fs_func
->set_entry_size(ep2
, new_size
);
1115 if (new_size
== 0) {
1116 p_fs
->fs_func
->set_entry_flag(ep2
, 0x01);
1117 p_fs
->fs_func
->set_entry_clu0(ep2
, CLUSTER_32(0));
1120 update_dir_checksum_with_entry_set(sb
, es
);
1121 release_entry_set(es
);
1123 /* (2) cut off from the FAT chain */
1124 if (last_clu
!= CLUSTER_32(0)) {
1125 if (fid
->flags
== 0x01)
1126 exfat_fat_write(sb
, last_clu
, CLUSTER_32(~0));
1129 /* (3) free the clusters */
1130 p_fs
->fs_func
->free_cluster(sb
, &clu
, 0);
1132 /* hint information */
1133 fid
->hint_last_off
= -1;
1134 if (fid
->rwoffset
> fid
->size
)
1135 fid
->rwoffset
= fid
->size
;
1137 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1139 fs_set_vol_flags(sb
, VOL_CLEAN
);
1142 if (p_fs
->dev_ejected
)
1146 pr_debug("%s exited (%d)\n", __func__
, ret
);
1147 /* release the lock for file system critical section */
1148 mutex_unlock(&p_fs
->v_mutex
);
1153 static void update_parent_info(struct file_id_t
*fid
,
1154 struct inode
*parent_inode
)
1156 struct fs_info_t
*p_fs
= &(EXFAT_SB(parent_inode
->i_sb
)->fs_info
);
1157 struct file_id_t
*parent_fid
= &(EXFAT_I(parent_inode
)->fid
);
1159 if (unlikely((parent_fid
->flags
!= fid
->dir
.flags
) ||
1160 (parent_fid
->size
!=
1161 (fid
->dir
.size
<< p_fs
->cluster_size_bits
)) ||
1162 (parent_fid
->start_clu
!= fid
->dir
.dir
))) {
1163 fid
->dir
.dir
= parent_fid
->start_clu
;
1164 fid
->dir
.flags
= parent_fid
->flags
;
1165 fid
->dir
.size
= ((parent_fid
->size
+ (p_fs
->cluster_size
- 1))
1166 >> p_fs
->cluster_size_bits
);
1170 static int ffsMoveFile(struct inode
*old_parent_inode
, struct file_id_t
*fid
,
1171 struct inode
*new_parent_inode
, struct dentry
*new_dentry
)
1175 struct chain_t olddir
, newdir
;
1176 struct chain_t
*p_dir
= NULL
;
1177 struct uni_name_t uni_name
;
1178 struct dentry_t
*ep
;
1179 struct super_block
*sb
= old_parent_inode
->i_sb
;
1180 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1181 u8
*new_path
= (u8
*)new_dentry
->d_name
.name
;
1182 struct inode
*new_inode
= new_dentry
->d_inode
;
1184 struct file_id_t
*new_fid
= NULL
;
1187 /* check the validity of the given file id */
1191 /* check the validity of pointer parameters */
1192 if (!new_path
|| (*new_path
== '\0'))
1195 /* acquire the lock for file system critical section */
1196 mutex_lock(&p_fs
->v_mutex
);
1198 update_parent_info(fid
, old_parent_inode
);
1200 olddir
.dir
= fid
->dir
.dir
;
1201 olddir
.size
= fid
->dir
.size
;
1202 olddir
.flags
= fid
->dir
.flags
;
1204 dentry
= fid
->entry
;
1206 /* check if the old file is "." or ".." */
1207 if (p_fs
->vol_type
!= EXFAT
) {
1208 if ((olddir
.dir
!= p_fs
->root_dir
) && (dentry
< 2)) {
1214 ep
= get_entry_in_dir(sb
, &olddir
, dentry
, NULL
);
1220 if (p_fs
->fs_func
->get_entry_attr(ep
) & ATTR_READONLY
) {
1225 /* check whether new dir is existing directory and empty */
1230 new_fid
= &EXFAT_I(new_inode
)->fid
;
1232 update_parent_info(new_fid
, new_parent_inode
);
1234 p_dir
= &new_fid
->dir
;
1235 new_entry
= new_fid
->entry
;
1236 ep
= get_entry_in_dir(sb
, p_dir
, new_entry
, NULL
);
1240 entry_type
= p_fs
->fs_func
->get_entry_type(ep
);
1242 if (entry_type
== TYPE_DIR
) {
1243 struct chain_t new_clu
;
1245 new_clu
.dir
= new_fid
->start_clu
;
1246 new_clu
.size
= (s32
)((new_fid
->size
- 1) >>
1247 p_fs
->cluster_size_bits
) + 1;
1248 new_clu
.flags
= new_fid
->flags
;
1250 if (!is_dir_empty(sb
, &new_clu
)) {
1257 /* check the validity of directory name in the given new pathname */
1258 ret
= resolve_path(new_parent_inode
, new_path
, &newdir
, &uni_name
);
1262 fs_set_vol_flags(sb
, VOL_DIRTY
);
1264 if (olddir
.dir
== newdir
.dir
)
1265 ret
= rename_file(new_parent_inode
, &olddir
, dentry
, &uni_name
,
1268 ret
= move_file(new_parent_inode
, &olddir
, dentry
, &newdir
,
1271 if ((ret
== 0) && new_inode
) {
1272 /* delete entries of new_dir */
1273 ep
= get_entry_in_dir(sb
, p_dir
, new_entry
, NULL
);
1277 num_entries
= p_fs
->fs_func
->count_ext_entries(sb
, p_dir
,
1279 if (num_entries
< 0)
1281 p_fs
->fs_func
->delete_dir_entry(sb
, p_dir
, new_entry
, 0,
1285 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1287 fs_set_vol_flags(sb
, VOL_CLEAN
);
1290 if (p_fs
->dev_ejected
)
1293 /* release the lock for file system critical section */
1294 mutex_unlock(&p_fs
->v_mutex
);
1299 static int ffsRemoveFile(struct inode
*inode
, struct file_id_t
*fid
)
1303 struct chain_t dir
, clu_to_free
;
1304 struct dentry_t
*ep
;
1305 struct super_block
*sb
= inode
->i_sb
;
1306 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1308 /* check the validity of the given file id */
1312 /* acquire the lock for file system critical section */
1313 mutex_lock(&p_fs
->v_mutex
);
1315 dir
.dir
= fid
->dir
.dir
;
1316 dir
.size
= fid
->dir
.size
;
1317 dir
.flags
= fid
->dir
.flags
;
1319 dentry
= fid
->entry
;
1321 ep
= get_entry_in_dir(sb
, &dir
, dentry
, NULL
);
1327 if (p_fs
->fs_func
->get_entry_attr(ep
) & ATTR_READONLY
) {
1331 fs_set_vol_flags(sb
, VOL_DIRTY
);
1333 /* (1) update the directory entry */
1334 remove_file(inode
, &dir
, dentry
);
1336 clu_to_free
.dir
= fid
->start_clu
;
1337 clu_to_free
.size
= (s32
)((fid
->size
- 1) >> p_fs
->cluster_size_bits
) + 1;
1338 clu_to_free
.flags
= fid
->flags
;
1340 /* (2) free the clusters */
1341 p_fs
->fs_func
->free_cluster(sb
, &clu_to_free
, 0);
1344 fid
->start_clu
= CLUSTER_32(~0);
1345 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
1347 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1349 fs_set_vol_flags(sb
, VOL_CLEAN
);
1352 if (p_fs
->dev_ejected
)
1355 /* release the lock for file system critical section */
1356 mutex_unlock(&p_fs
->v_mutex
);
1362 /* Not currently wired up */
1363 static int ffsSetAttr(struct inode
*inode
, u32 attr
)
1367 sector_t sector
= 0;
1368 struct dentry_t
*ep
;
1369 struct super_block
*sb
= inode
->i_sb
;
1370 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1371 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1372 u8 is_dir
= (fid
->type
== TYPE_DIR
) ? 1 : 0;
1373 struct entry_set_cache_t
*es
= NULL
;
1375 if (fid
->attr
== attr
) {
1376 if (p_fs
->dev_ejected
)
1382 if ((fid
->dir
.dir
== p_fs
->root_dir
) &&
1383 (fid
->entry
== -1)) {
1384 if (p_fs
->dev_ejected
)
1390 /* acquire the lock for file system critical section */
1391 mutex_lock(&p_fs
->v_mutex
);
1393 /* get the directory entry of given file */
1394 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
1395 ES_ALL_ENTRIES
, &ep
);
1401 type
= p_fs
->fs_func
->get_entry_type(ep
);
1403 if (((type
== TYPE_FILE
) && (attr
& ATTR_SUBDIR
)) ||
1404 ((type
== TYPE_DIR
) && (!(attr
& ATTR_SUBDIR
)))) {
1405 if (p_fs
->dev_ejected
)
1410 release_entry_set(es
);
1414 fs_set_vol_flags(sb
, VOL_DIRTY
);
1416 /* set the file attribute */
1418 p_fs
->fs_func
->set_entry_attr(ep
, attr
);
1420 update_dir_checksum_with_entry_set(sb
, es
);
1421 release_entry_set(es
);
1423 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1425 fs_set_vol_flags(sb
, VOL_CLEAN
);
1428 if (p_fs
->dev_ejected
)
1431 /* release the lock for file system critical section */
1432 mutex_unlock(&p_fs
->v_mutex
);
1438 static int ffsReadStat(struct inode
*inode
, struct dir_entry_t
*info
)
1443 struct uni_name_t uni_name
;
1444 struct timestamp_t tm
;
1445 struct dentry_t
*ep
, *ep2
;
1446 struct super_block
*sb
= inode
->i_sb
;
1447 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1448 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1449 struct entry_set_cache_t
*es
= NULL
;
1450 u8 is_dir
= (fid
->type
== TYPE_DIR
) ? 1 : 0;
1452 pr_debug("%s entered\n", __func__
);
1454 /* acquire the lock for file system critical section */
1455 mutex_lock(&p_fs
->v_mutex
);
1458 if ((fid
->dir
.dir
== p_fs
->root_dir
) &&
1459 (fid
->entry
== -1)) {
1460 info
->Attr
= ATTR_SUBDIR
;
1461 memset((char *)&info
->CreateTimestamp
, 0,
1462 sizeof(struct date_time_t
));
1463 memset((char *)&info
->ModifyTimestamp
, 0,
1464 sizeof(struct date_time_t
));
1465 memset((char *)&info
->AccessTimestamp
, 0,
1466 sizeof(struct date_time_t
));
1467 strcpy(info
->ShortName
, ".");
1468 strcpy(info
->Name
, ".");
1470 dir
.dir
= p_fs
->root_dir
;
1473 if (p_fs
->root_dir
== CLUSTER_32(0)) {
1474 /* FAT16 root_dir */
1475 info
->Size
= p_fs
->dentries_in_root
<<
1478 info
->Size
= count_num_clusters(sb
, &dir
) <<
1479 p_fs
->cluster_size_bits
;
1482 count
= count_dos_name_entries(sb
, &dir
, TYPE_DIR
);
1484 ret
= count
; /* propogate error upward */
1487 info
->NumSubdirs
= count
;
1489 if (p_fs
->dev_ejected
)
1495 /* get the directory entry of given file or directory */
1496 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
1504 /* set FILE_INFO structure using the acquired struct dentry_t */
1505 info
->Attr
= p_fs
->fs_func
->get_entry_attr(ep
);
1507 p_fs
->fs_func
->get_entry_time(ep
, &tm
, TM_CREATE
);
1508 info
->CreateTimestamp
.Year
= tm
.year
;
1509 info
->CreateTimestamp
.Month
= tm
.mon
;
1510 info
->CreateTimestamp
.Day
= tm
.day
;
1511 info
->CreateTimestamp
.Hour
= tm
.hour
;
1512 info
->CreateTimestamp
.Minute
= tm
.min
;
1513 info
->CreateTimestamp
.Second
= tm
.sec
;
1514 info
->CreateTimestamp
.MilliSecond
= 0;
1516 p_fs
->fs_func
->get_entry_time(ep
, &tm
, TM_MODIFY
);
1517 info
->ModifyTimestamp
.Year
= tm
.year
;
1518 info
->ModifyTimestamp
.Month
= tm
.mon
;
1519 info
->ModifyTimestamp
.Day
= tm
.day
;
1520 info
->ModifyTimestamp
.Hour
= tm
.hour
;
1521 info
->ModifyTimestamp
.Minute
= tm
.min
;
1522 info
->ModifyTimestamp
.Second
= tm
.sec
;
1523 info
->ModifyTimestamp
.MilliSecond
= 0;
1525 memset((char *)&info
->AccessTimestamp
, 0, sizeof(struct date_time_t
));
1527 *uni_name
.name
= 0x0;
1528 /* XXX this is very bad for exfat cuz name is already included in es.
1529 * API should be revised
1531 p_fs
->fs_func
->get_uni_name_from_ext_entry(sb
, &fid
->dir
, fid
->entry
,
1533 nls_uniname_to_cstring(sb
, info
->Name
, &uni_name
);
1535 info
->NumSubdirs
= 2;
1537 info
->Size
= p_fs
->fs_func
->get_entry_size(ep2
);
1539 release_entry_set(es
);
1542 dir
.dir
= fid
->start_clu
;
1545 if (info
->Size
== 0)
1546 info
->Size
= (u64
)count_num_clusters(sb
, &dir
) <<
1547 p_fs
->cluster_size_bits
;
1549 count
= count_dos_name_entries(sb
, &dir
, TYPE_DIR
);
1551 ret
= count
; /* propogate error upward */
1554 info
->NumSubdirs
+= count
;
1557 if (p_fs
->dev_ejected
)
1561 /* release the lock for file system critical section */
1562 mutex_unlock(&p_fs
->v_mutex
);
1564 pr_debug("%s exited successfully\n", __func__
);
1568 static int ffsWriteStat(struct inode
*inode
, struct dir_entry_t
*info
)
1571 struct timestamp_t tm
;
1572 struct dentry_t
*ep
, *ep2
;
1573 struct entry_set_cache_t
*es
= NULL
;
1574 struct super_block
*sb
= inode
->i_sb
;
1575 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1576 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1577 u8 is_dir
= (fid
->type
== TYPE_DIR
) ? 1 : 0;
1579 pr_debug("%s entered (inode %p info %p\n", __func__
, inode
, info
);
1581 /* acquire the lock for file system critical section */
1582 mutex_lock(&p_fs
->v_mutex
);
1585 if ((fid
->dir
.dir
== p_fs
->root_dir
) &&
1586 (fid
->entry
== -1)) {
1587 if (p_fs
->dev_ejected
)
1594 fs_set_vol_flags(sb
, VOL_DIRTY
);
1596 /* get the directory entry of given file or directory */
1597 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
1598 ES_ALL_ENTRIES
, &ep
);
1605 p_fs
->fs_func
->set_entry_attr(ep
, info
->Attr
);
1607 /* set FILE_INFO structure using the acquired struct dentry_t */
1608 tm
.sec
= info
->CreateTimestamp
.Second
;
1609 tm
.min
= info
->CreateTimestamp
.Minute
;
1610 tm
.hour
= info
->CreateTimestamp
.Hour
;
1611 tm
.day
= info
->CreateTimestamp
.Day
;
1612 tm
.mon
= info
->CreateTimestamp
.Month
;
1613 tm
.year
= info
->CreateTimestamp
.Year
;
1614 p_fs
->fs_func
->set_entry_time(ep
, &tm
, TM_CREATE
);
1616 tm
.sec
= info
->ModifyTimestamp
.Second
;
1617 tm
.min
= info
->ModifyTimestamp
.Minute
;
1618 tm
.hour
= info
->ModifyTimestamp
.Hour
;
1619 tm
.day
= info
->ModifyTimestamp
.Day
;
1620 tm
.mon
= info
->ModifyTimestamp
.Month
;
1621 tm
.year
= info
->ModifyTimestamp
.Year
;
1622 p_fs
->fs_func
->set_entry_time(ep
, &tm
, TM_MODIFY
);
1624 p_fs
->fs_func
->set_entry_size(ep2
, info
->Size
);
1626 update_dir_checksum_with_entry_set(sb
, es
);
1627 release_entry_set(es
);
1629 if (p_fs
->dev_ejected
)
1633 /* release the lock for file system critical section */
1634 mutex_unlock(&p_fs
->v_mutex
);
1636 pr_debug("%s exited (%d)\n", __func__
, ret
);
1641 static int ffsMapCluster(struct inode
*inode
, s32 clu_offset
, u32
*clu
)
1643 s32 num_clusters
, num_alloced
;
1644 bool modified
= false;
1647 struct chain_t new_clu
;
1648 struct dentry_t
*ep
;
1649 struct entry_set_cache_t
*es
= NULL
;
1650 struct super_block
*sb
= inode
->i_sb
;
1651 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1652 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1654 /* check the validity of pointer parameters */
1658 /* acquire the lock for file system critical section */
1659 mutex_lock(&p_fs
->v_mutex
);
1661 fid
->rwoffset
= (s64
)(clu_offset
) << p_fs
->cluster_size_bits
;
1663 if (EXFAT_I(inode
)->mmu_private
== 0)
1666 num_clusters
= (s32
)((EXFAT_I(inode
)->mmu_private
- 1) >>
1667 p_fs
->cluster_size_bits
) + 1;
1669 *clu
= last_clu
= fid
->start_clu
;
1671 if (fid
->flags
== 0x03) {
1672 if ((clu_offset
> 0) && (*clu
!= CLUSTER_32(~0))) {
1673 last_clu
+= clu_offset
- 1;
1675 if (clu_offset
== num_clusters
)
1676 *clu
= CLUSTER_32(~0);
1681 /* hint information */
1682 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
1683 (clu_offset
>= fid
->hint_last_off
)) {
1684 clu_offset
-= fid
->hint_last_off
;
1685 *clu
= fid
->hint_last_clu
;
1688 while ((clu_offset
> 0) && (*clu
!= CLUSTER_32(~0))) {
1690 if (exfat_fat_read(sb
, *clu
, clu
) == -1) {
1698 if (*clu
== CLUSTER_32(~0)) {
1699 fs_set_vol_flags(sb
, VOL_DIRTY
);
1701 new_clu
.dir
= (last_clu
== CLUSTER_32(~0)) ? CLUSTER_32(~0) :
1704 new_clu
.flags
= fid
->flags
;
1706 /* (1) allocate a cluster */
1707 num_alloced
= p_fs
->fs_func
->alloc_cluster(sb
, 1, &new_clu
);
1708 if (num_alloced
< 0) {
1711 } else if (num_alloced
== 0) {
1716 /* (2) append to the FAT chain */
1717 if (last_clu
== CLUSTER_32(~0)) {
1718 if (new_clu
.flags
== 0x01)
1720 fid
->start_clu
= new_clu
.dir
;
1723 if (new_clu
.flags
!= fid
->flags
) {
1724 exfat_chain_cont_cluster(sb
, fid
->start_clu
,
1729 if (new_clu
.flags
== 0x01)
1730 exfat_fat_write(sb
, last_clu
, new_clu
.dir
);
1733 num_clusters
+= num_alloced
;
1736 es
= get_entry_set_in_dir(sb
, &fid
->dir
, fid
->entry
,
1737 ES_ALL_ENTRIES
, &ep
);
1742 /* get stream entry */
1745 /* (3) update directory entry */
1747 if (p_fs
->fs_func
->get_entry_flag(ep
) != fid
->flags
)
1748 p_fs
->fs_func
->set_entry_flag(ep
, fid
->flags
);
1750 if (p_fs
->fs_func
->get_entry_clu0(ep
) != fid
->start_clu
)
1751 p_fs
->fs_func
->set_entry_clu0(ep
,
1756 update_dir_checksum_with_entry_set(sb
, es
);
1757 release_entry_set(es
);
1759 /* add number of new blocks to inode */
1760 inode
->i_blocks
+= num_alloced
<< (p_fs
->cluster_size_bits
- 9);
1763 /* hint information */
1764 fid
->hint_last_off
= (s32
)(fid
->rwoffset
>> p_fs
->cluster_size_bits
);
1765 fid
->hint_last_clu
= *clu
;
1767 if (p_fs
->dev_ejected
)
1771 /* release the lock for file system critical section */
1772 mutex_unlock(&p_fs
->v_mutex
);
1777 /*----------------------------------------------------------------------*/
1778 /* Directory Operation Functions */
1779 /*----------------------------------------------------------------------*/
1781 static int ffsCreateDir(struct inode
*inode
, char *path
, struct file_id_t
*fid
)
1785 struct uni_name_t uni_name
;
1786 struct super_block
*sb
= inode
->i_sb
;
1787 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1789 pr_debug("%s entered\n", __func__
);
1791 /* check the validity of pointer parameters */
1792 if (!fid
|| !path
|| (*path
== '\0'))
1795 /* acquire the lock for file system critical section */
1796 mutex_lock(&p_fs
->v_mutex
);
1798 /* check the validity of directory name in the given old pathname */
1799 ret
= resolve_path(inode
, path
, &dir
, &uni_name
);
1803 fs_set_vol_flags(sb
, VOL_DIRTY
);
1805 ret
= create_dir(inode
, &dir
, &uni_name
, fid
);
1807 #ifndef CONFIG_EXFAT_DELAYED_SYNC
1809 fs_set_vol_flags(sb
, VOL_CLEAN
);
1812 if (p_fs
->dev_ejected
)
1815 /* release the lock for file system critical section */
1816 mutex_unlock(&p_fs
->v_mutex
);
1821 static int ffsReadDir(struct inode
*inode
, struct dir_entry_t
*dir_entry
)
1823 int i
, dentry
, clu_offset
;
1825 s32 dentries_per_clu
, dentries_per_clu_bits
= 0;
1828 struct chain_t dir
, clu
;
1829 struct uni_name_t uni_name
;
1830 struct timestamp_t tm
;
1831 struct dentry_t
*ep
;
1832 struct super_block
*sb
= inode
->i_sb
;
1833 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
1834 struct fs_func
*fs_func
= p_fs
->fs_func
;
1835 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
1837 /* check the validity of pointer parameters */
1841 /* check if the given file ID is opened */
1842 if (fid
->type
!= TYPE_DIR
)
1845 /* acquire the lock for file system critical section */
1846 mutex_lock(&p_fs
->v_mutex
);
1848 if (fid
->entry
== -1) {
1849 dir
.dir
= p_fs
->root_dir
;
1852 dir
.dir
= fid
->start_clu
;
1853 dir
.size
= (s32
)(fid
->size
>> p_fs
->cluster_size_bits
);
1854 dir
.flags
= fid
->flags
;
1857 dentry
= (s32
)fid
->rwoffset
;
1859 if (dir
.dir
== CLUSTER_32(0)) {
1860 /* FAT16 root_dir */
1861 dentries_per_clu
= p_fs
->dentries_in_root
;
1863 if (dentry
== dentries_per_clu
) {
1864 clu
.dir
= CLUSTER_32(~0);
1867 clu
.size
= dir
.size
;
1868 clu
.flags
= dir
.flags
;
1871 dentries_per_clu
= p_fs
->dentries_per_clu
;
1872 dentries_per_clu_bits
= ilog2(dentries_per_clu
);
1874 clu_offset
= dentry
>> dentries_per_clu_bits
;
1876 clu
.size
= dir
.size
;
1877 clu
.flags
= dir
.flags
;
1879 if (clu
.flags
== 0x03) {
1880 clu
.dir
+= clu_offset
;
1881 clu
.size
-= clu_offset
;
1883 /* hint_information */
1884 if ((clu_offset
> 0) && (fid
->hint_last_off
> 0) &&
1885 (clu_offset
>= fid
->hint_last_off
)) {
1886 clu_offset
-= fid
->hint_last_off
;
1887 clu
.dir
= fid
->hint_last_clu
;
1890 while (clu_offset
> 0) {
1891 /* clu.dir = exfat_fat_read(sb, clu.dir); */
1892 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) == -1) {
1901 while (clu
.dir
!= CLUSTER_32(~0)) {
1902 if (p_fs
->dev_ejected
)
1905 if (dir
.dir
== CLUSTER_32(0)) /* FAT16 root_dir */
1906 i
= dentry
% dentries_per_clu
;
1908 i
= dentry
& (dentries_per_clu
- 1);
1910 for ( ; i
< dentries_per_clu
; i
++, dentry
++) {
1911 ep
= get_entry_in_dir(sb
, &clu
, i
, §or
);
1916 type
= fs_func
->get_entry_type(ep
);
1918 if (type
== TYPE_UNUSED
)
1921 if ((type
!= TYPE_FILE
) && (type
!= TYPE_DIR
))
1924 exfat_buf_lock(sb
, sector
);
1925 dir_entry
->Attr
= fs_func
->get_entry_attr(ep
);
1927 fs_func
->get_entry_time(ep
, &tm
, TM_CREATE
);
1928 dir_entry
->CreateTimestamp
.Year
= tm
.year
;
1929 dir_entry
->CreateTimestamp
.Month
= tm
.mon
;
1930 dir_entry
->CreateTimestamp
.Day
= tm
.day
;
1931 dir_entry
->CreateTimestamp
.Hour
= tm
.hour
;
1932 dir_entry
->CreateTimestamp
.Minute
= tm
.min
;
1933 dir_entry
->CreateTimestamp
.Second
= tm
.sec
;
1934 dir_entry
->CreateTimestamp
.MilliSecond
= 0;
1936 fs_func
->get_entry_time(ep
, &tm
, TM_MODIFY
);
1937 dir_entry
->ModifyTimestamp
.Year
= tm
.year
;
1938 dir_entry
->ModifyTimestamp
.Month
= tm
.mon
;
1939 dir_entry
->ModifyTimestamp
.Day
= tm
.day
;
1940 dir_entry
->ModifyTimestamp
.Hour
= tm
.hour
;
1941 dir_entry
->ModifyTimestamp
.Minute
= tm
.min
;
1942 dir_entry
->ModifyTimestamp
.Second
= tm
.sec
;
1943 dir_entry
->ModifyTimestamp
.MilliSecond
= 0;
1945 memset((char *)&dir_entry
->AccessTimestamp
, 0,
1946 sizeof(struct date_time_t
));
1948 *uni_name
.name
= 0x0;
1949 fs_func
->get_uni_name_from_ext_entry(sb
, &dir
, dentry
,
1951 nls_uniname_to_cstring(sb
, dir_entry
->Name
, &uni_name
);
1952 exfat_buf_unlock(sb
, sector
);
1954 ep
= get_entry_in_dir(sb
, &clu
, i
+ 1, NULL
);
1960 dir_entry
->Size
= fs_func
->get_entry_size(ep
);
1962 /* hint information */
1963 if (dir
.dir
== CLUSTER_32(0)) { /* FAT16 root_dir */
1965 fid
->hint_last_off
= dentry
>>
1966 dentries_per_clu_bits
;
1967 fid
->hint_last_clu
= clu
.dir
;
1970 fid
->rwoffset
= (s64
)(++dentry
);
1972 if (p_fs
->dev_ejected
)
1977 if (dir
.dir
== CLUSTER_32(0))
1978 break; /* FAT16 root_dir */
1980 if (clu
.flags
== 0x03) {
1981 if ((--clu
.size
) > 0)
1984 clu
.dir
= CLUSTER_32(~0);
1986 /* clu.dir = exfat_fat_read(sb, clu.dir); */
1987 if (exfat_fat_read(sb
, clu
.dir
, &clu
.dir
) == -1) {
1994 *dir_entry
->Name
= '\0';
1996 fid
->rwoffset
= (s64
)(++dentry
);
1998 if (p_fs
->dev_ejected
)
2002 /* release the lock for file system critical section */
2003 mutex_unlock(&p_fs
->v_mutex
);
2008 static int ffsRemoveDir(struct inode
*inode
, struct file_id_t
*fid
)
2012 struct chain_t dir
, clu_to_free
;
2013 struct super_block
*sb
= inode
->i_sb
;
2014 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
2016 /* check the validity of the given file id */
2020 dir
.dir
= fid
->dir
.dir
;
2021 dir
.size
= fid
->dir
.size
;
2022 dir
.flags
= fid
->dir
.flags
;
2024 dentry
= fid
->entry
;
2026 /* check if the file is "." or ".." */
2027 if (p_fs
->vol_type
!= EXFAT
) {
2028 if ((dir
.dir
!= p_fs
->root_dir
) && (dentry
< 2))
2032 /* acquire the lock for file system critical section */
2033 mutex_lock(&p_fs
->v_mutex
);
2035 clu_to_free
.dir
= fid
->start_clu
;
2036 clu_to_free
.size
= (s32
)((fid
->size
- 1) >> p_fs
->cluster_size_bits
) + 1;
2037 clu_to_free
.flags
= fid
->flags
;
2039 if (!is_dir_empty(sb
, &clu_to_free
)) {
2044 fs_set_vol_flags(sb
, VOL_DIRTY
);
2046 /* (1) update the directory entry */
2047 remove_file(inode
, &dir
, dentry
);
2049 /* (2) free the clusters */
2050 p_fs
->fs_func
->free_cluster(sb
, &clu_to_free
, 1);
2053 fid
->start_clu
= CLUSTER_32(~0);
2054 fid
->flags
= (p_fs
->vol_type
== EXFAT
) ? 0x03 : 0x01;
2056 #ifndef CONFIG_EXFAT_DELAYED_SYNC
2058 fs_set_vol_flags(sb
, VOL_CLEAN
);
2061 if (p_fs
->dev_ejected
)
2065 /* release the lock for file system critical section */
2066 mutex_unlock(&p_fs
->v_mutex
);
2071 /*======================================================================*/
2072 /* Directory Entry Operations */
2073 /*======================================================================*/
2075 static int exfat_readdir(struct file
*filp
, struct dir_context
*ctx
)
2077 struct inode
*inode
= file_inode(filp
);
2078 struct super_block
*sb
= inode
->i_sb
;
2079 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2080 struct fs_info_t
*p_fs
= &sbi
->fs_info
;
2081 struct bd_info_t
*p_bd
= &(EXFAT_SB(sb
)->bd_info
);
2082 struct dir_entry_t de
;
2090 /* Fake . and .. for the root directory. */
2091 if ((p_fs
->vol_type
== EXFAT
) || (inode
->i_ino
== EXFAT_ROOT_INO
)) {
2093 if (inode
->i_ino
== EXFAT_ROOT_INO
)
2094 inum
= EXFAT_ROOT_INO
;
2096 inum
= inode
->i_ino
;
2097 else /* (cpos == 1) */
2098 inum
= parent_ino(filp
->f_path
.dentry
);
2100 if (!dir_emit_dots(filp
, ctx
))
2108 if (cpos
& (DENTRY_SIZE
- 1)) {
2114 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2115 EXFAT_I(inode
)->fid
.rwoffset
= cpos
>> DENTRY_SIZE_BITS
;
2117 err
= ffsReadDir(inode
, &de
);
2119 /* at least we tried to read a sector
2120 * move cpos to next sector position (should be aligned)
2123 cpos
+= 1 << p_bd
->sector_size_bits
;
2124 cpos
&= ~((1 << p_bd
->sector_size_bits
) - 1);
2130 cpos
= EXFAT_I(inode
)->fid
.rwoffset
<< DENTRY_SIZE_BITS
;
2135 if (!memcmp(de
.ShortName
, DOS_CUR_DIR_NAME
, DOS_NAME_LENGTH
)) {
2136 inum
= inode
->i_ino
;
2137 } else if (!memcmp(de
.ShortName
, DOS_PAR_DIR_NAME
, DOS_NAME_LENGTH
)) {
2138 inum
= parent_ino(filp
->f_path
.dentry
);
2140 loff_t i_pos
= ((loff_t
)EXFAT_I(inode
)->fid
.start_clu
<< 32) |
2141 ((EXFAT_I(inode
)->fid
.rwoffset
- 1) & 0xffffffff);
2142 struct inode
*tmp
= exfat_iget(sb
, i_pos
);
2148 inum
= iunique(sb
, EXFAT_ROOT_INO
);
2152 if (!dir_emit(ctx
, de
.Name
, strlen(de
.Name
), inum
,
2153 (de
.Attr
& ATTR_SUBDIR
) ? DT_DIR
: DT_REG
))
2166 static int exfat_ioctl_volume_id(struct inode
*dir
)
2168 struct super_block
*sb
= dir
->i_sb
;
2169 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2170 struct fs_info_t
*p_fs
= &sbi
->fs_info
;
2172 return p_fs
->vol_id
;
2175 static long exfat_generic_ioctl(struct file
*filp
, unsigned int cmd
,
2178 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
2179 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2181 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2184 case EXFAT_IOCTL_GET_VOLUME_ID
:
2185 return exfat_ioctl_volume_id(inode
);
2186 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
2187 case EXFAT_IOC_GET_DEBUGFLAGS
: {
2188 struct super_block
*sb
= inode
->i_sb
;
2189 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2191 flags
= sbi
->debug_flags
;
2192 return put_user(flags
, (int __user
*)arg
);
2194 case EXFAT_IOC_SET_DEBUGFLAGS
: {
2195 struct super_block
*sb
= inode
->i_sb
;
2196 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2198 if (!capable(CAP_SYS_ADMIN
))
2201 if (get_user(flags
, (int __user
*)arg
))
2205 sbi
->debug_flags
= flags
;
2210 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
2212 return -ENOTTY
; /* Inappropriate ioctl for device */
2216 static const struct file_operations exfat_dir_operations
= {
2217 .llseek
= generic_file_llseek
,
2218 .read
= generic_read_dir
,
2219 .iterate
= exfat_readdir
,
2220 .unlocked_ioctl
= exfat_generic_ioctl
,
2221 .fsync
= generic_file_fsync
,
2224 static int exfat_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2227 struct super_block
*sb
= dir
->i_sb
;
2228 struct timespec64 curtime
;
2229 struct inode
*inode
;
2230 struct file_id_t fid
;
2236 pr_debug("%s entered\n", __func__
);
2238 err
= ffsCreateFile(dir
, (u8
*)dentry
->d_name
.name
, FM_REGULAR
, &fid
);
2243 curtime
= current_time(dir
);
2244 dir
->i_ctime
= curtime
;
2245 dir
->i_mtime
= curtime
;
2246 dir
->i_atime
= curtime
;
2247 if (IS_DIRSYNC(dir
))
2248 (void)exfat_sync_inode(dir
);
2250 mark_inode_dirty(dir
);
2252 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2254 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2255 if (IS_ERR(inode
)) {
2256 err
= PTR_ERR(inode
);
2259 INC_IVERSION(inode
);
2260 curtime
= current_time(inode
);
2261 inode
->i_mtime
= curtime
;
2262 inode
->i_atime
= curtime
;
2263 inode
->i_ctime
= curtime
;
2265 * timestamp is already written, so mark_inode_dirty() is unnecessary.
2268 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2269 d_instantiate(dentry
, inode
);
2273 pr_debug("%s exited\n", __func__
);
2277 static int exfat_find(struct inode
*dir
, struct qstr
*qname
,
2278 struct file_id_t
*fid
)
2282 if (qname
->len
== 0)
2285 err
= ffsLookupFile(dir
, (u8
*)qname
->name
, fid
);
2292 static int exfat_d_anon_disconn(struct dentry
*dentry
)
2294 return IS_ROOT(dentry
) && (dentry
->d_flags
& DCACHE_DISCONNECTED
);
2297 static struct dentry
*exfat_lookup(struct inode
*dir
, struct dentry
*dentry
,
2300 struct super_block
*sb
= dir
->i_sb
;
2301 struct inode
*inode
;
2302 struct dentry
*alias
;
2304 struct file_id_t fid
;
2310 pr_debug("%s entered\n", __func__
);
2311 err
= exfat_find(dir
, &dentry
->d_name
, &fid
);
2313 if (err
== -ENOENT
) {
2320 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2321 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2322 if (IS_ERR(inode
)) {
2323 err
= PTR_ERR(inode
);
2327 i_mode
= inode
->i_mode
;
2328 if (S_ISLNK(i_mode
) && !EXFAT_I(inode
)->target
) {
2329 EXFAT_I(inode
)->target
= kmalloc(i_size_read(inode
) + 1,
2331 if (!EXFAT_I(inode
)->target
) {
2335 ffsReadFile(dir
, &fid
, EXFAT_I(inode
)->target
,
2336 i_size_read(inode
), &ret
);
2337 *(EXFAT_I(inode
)->target
+ i_size_read(inode
)) = '\0';
2340 alias
= d_find_alias(inode
);
2341 if (alias
&& !exfat_d_anon_disconn(alias
)) {
2342 BUG_ON(d_unhashed(alias
));
2343 if (!S_ISDIR(i_mode
))
2344 d_move(alias
, dentry
);
2347 pr_debug("%s exited 1\n", __func__
);
2353 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2354 dentry
= d_splice_alias(inode
, dentry
);
2356 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2357 pr_debug("%s exited 2\n", __func__
);
2362 pr_debug("%s exited 3\n", __func__
);
2363 return ERR_PTR(err
);
2366 static inline unsigned long exfat_hash(loff_t i_pos
)
2368 return hash_32(i_pos
, EXFAT_HASH_BITS
);
2371 static void exfat_attach(struct inode
*inode
, loff_t i_pos
)
2373 struct exfat_sb_info
*sbi
= EXFAT_SB(inode
->i_sb
);
2374 struct hlist_head
*head
= sbi
->inode_hashtable
+ exfat_hash(i_pos
);
2376 spin_lock(&sbi
->inode_hash_lock
);
2377 EXFAT_I(inode
)->i_pos
= i_pos
;
2378 hlist_add_head(&EXFAT_I(inode
)->i_hash_fat
, head
);
2379 spin_unlock(&sbi
->inode_hash_lock
);
2382 static void exfat_detach(struct inode
*inode
)
2384 struct exfat_sb_info
*sbi
= EXFAT_SB(inode
->i_sb
);
2386 spin_lock(&sbi
->inode_hash_lock
);
2387 hlist_del_init(&EXFAT_I(inode
)->i_hash_fat
);
2388 EXFAT_I(inode
)->i_pos
= 0;
2389 spin_unlock(&sbi
->inode_hash_lock
);
2392 static int exfat_unlink(struct inode
*dir
, struct dentry
*dentry
)
2394 struct inode
*inode
= dentry
->d_inode
;
2395 struct super_block
*sb
= dir
->i_sb
;
2396 struct timespec64 curtime
;
2401 pr_debug("%s entered\n", __func__
);
2403 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2405 err
= ffsRemoveFile(dir
, &(EXFAT_I(inode
)->fid
));
2410 curtime
= current_time(dir
);
2411 dir
->i_mtime
= curtime
;
2412 dir
->i_atime
= curtime
;
2413 if (IS_DIRSYNC(dir
))
2414 (void)exfat_sync_inode(dir
);
2416 mark_inode_dirty(dir
);
2419 curtime
= current_time(inode
);
2420 inode
->i_mtime
= curtime
;
2421 inode
->i_atime
= curtime
;
2422 exfat_detach(inode
);
2423 remove_inode_hash(inode
);
2427 pr_debug("%s exited\n", __func__
);
2431 static int exfat_symlink(struct inode
*dir
, struct dentry
*dentry
,
2434 struct super_block
*sb
= dir
->i_sb
;
2435 struct timespec64 curtime
;
2436 struct inode
*inode
;
2437 struct file_id_t fid
;
2440 u64 len
= (u64
)strlen(target
);
2445 pr_debug("%s entered\n", __func__
);
2447 err
= ffsCreateFile(dir
, (u8
*)dentry
->d_name
.name
, FM_SYMLINK
, &fid
);
2452 err
= ffsWriteFile(dir
, &fid
, (char *)target
, len
, &ret
);
2455 ffsRemoveFile(dir
, &fid
);
2460 curtime
= current_time(dir
);
2461 dir
->i_ctime
= curtime
;
2462 dir
->i_mtime
= curtime
;
2463 dir
->i_atime
= curtime
;
2464 if (IS_DIRSYNC(dir
))
2465 (void)exfat_sync_inode(dir
);
2467 mark_inode_dirty(dir
);
2469 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2471 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2472 if (IS_ERR(inode
)) {
2473 err
= PTR_ERR(inode
);
2476 INC_IVERSION(inode
);
2477 curtime
= current_time(inode
);
2478 inode
->i_mtime
= curtime
;
2479 inode
->i_atime
= curtime
;
2480 inode
->i_ctime
= curtime
;
2481 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2483 EXFAT_I(inode
)->target
= kmemdup(target
, len
+ 1, GFP_KERNEL
);
2484 if (!EXFAT_I(inode
)->target
) {
2489 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2490 d_instantiate(dentry
, inode
);
2494 pr_debug("%s exited\n", __func__
);
2498 static int exfat_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2500 struct super_block
*sb
= dir
->i_sb
;
2501 struct timespec64 curtime
;
2502 struct inode
*inode
;
2503 struct file_id_t fid
;
2509 pr_debug("%s entered\n", __func__
);
2511 err
= ffsCreateDir(dir
, (u8
*)dentry
->d_name
.name
, &fid
);
2516 curtime
= current_time(dir
);
2517 dir
->i_ctime
= curtime
;
2518 dir
->i_mtime
= curtime
;
2519 dir
->i_atime
= curtime
;
2520 if (IS_DIRSYNC(dir
))
2521 (void)exfat_sync_inode(dir
);
2523 mark_inode_dirty(dir
);
2526 i_pos
= ((loff_t
)fid
.dir
.dir
<< 32) | (fid
.entry
& 0xffffffff);
2528 inode
= exfat_build_inode(sb
, &fid
, i_pos
);
2529 if (IS_ERR(inode
)) {
2530 err
= PTR_ERR(inode
);
2533 INC_IVERSION(inode
);
2534 curtime
= current_time(inode
);
2535 inode
->i_mtime
= curtime
;
2536 inode
->i_atime
= curtime
;
2537 inode
->i_ctime
= curtime
;
2538 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
2540 dentry
->d_time
= GET_IVERSION(dentry
->d_parent
->d_inode
);
2541 d_instantiate(dentry
, inode
);
2545 pr_debug("%s exited\n", __func__
);
2549 static int exfat_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2551 struct inode
*inode
= dentry
->d_inode
;
2552 struct super_block
*sb
= dir
->i_sb
;
2553 struct timespec64 curtime
;
2558 pr_debug("%s entered\n", __func__
);
2560 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2562 err
= ffsRemoveDir(dir
, &(EXFAT_I(inode
)->fid
));
2567 curtime
= current_time(dir
);
2568 dir
->i_mtime
= curtime
;
2569 dir
->i_atime
= curtime
;
2570 if (IS_DIRSYNC(dir
))
2571 (void)exfat_sync_inode(dir
);
2573 mark_inode_dirty(dir
);
2577 curtime
= current_time(inode
);
2578 inode
->i_mtime
= curtime
;
2579 inode
->i_atime
= curtime
;
2580 exfat_detach(inode
);
2581 remove_inode_hash(inode
);
2585 pr_debug("%s exited\n", __func__
);
2589 static int exfat_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2590 struct inode
*new_dir
, struct dentry
*new_dentry
,
2593 struct inode
*old_inode
, *new_inode
;
2594 struct super_block
*sb
= old_dir
->i_sb
;
2595 struct timespec64 curtime
;
2604 pr_debug("%s entered\n", __func__
);
2606 old_inode
= old_dentry
->d_inode
;
2607 new_inode
= new_dentry
->d_inode
;
2609 EXFAT_I(old_inode
)->fid
.size
= i_size_read(old_inode
);
2611 err
= ffsMoveFile(old_dir
, &(EXFAT_I(old_inode
)->fid
), new_dir
,
2616 INC_IVERSION(new_dir
);
2617 curtime
= current_time(new_dir
);
2618 new_dir
->i_ctime
= curtime
;
2619 new_dir
->i_mtime
= curtime
;
2620 new_dir
->i_atime
= curtime
;
2622 if (IS_DIRSYNC(new_dir
))
2623 (void)exfat_sync_inode(new_dir
);
2625 mark_inode_dirty(new_dir
);
2627 i_pos
= ((loff_t
)EXFAT_I(old_inode
)->fid
.dir
.dir
<< 32) |
2628 (EXFAT_I(old_inode
)->fid
.entry
& 0xffffffff);
2630 exfat_detach(old_inode
);
2631 exfat_attach(old_inode
, i_pos
);
2632 if (IS_DIRSYNC(new_dir
))
2633 (void)exfat_sync_inode(old_inode
);
2635 mark_inode_dirty(old_inode
);
2637 if ((S_ISDIR(old_inode
->i_mode
)) && (old_dir
!= new_dir
)) {
2638 drop_nlink(old_dir
);
2642 INC_IVERSION(old_dir
);
2643 curtime
= current_time(old_dir
);
2644 old_dir
->i_ctime
= curtime
;
2645 old_dir
->i_mtime
= curtime
;
2646 if (IS_DIRSYNC(old_dir
))
2647 (void)exfat_sync_inode(old_dir
);
2649 mark_inode_dirty(old_dir
);
2652 exfat_detach(new_inode
);
2653 drop_nlink(new_inode
);
2654 if (S_ISDIR(new_inode
->i_mode
))
2655 drop_nlink(new_inode
);
2656 new_inode
->i_ctime
= current_time(new_inode
);
2661 pr_debug("%s exited\n", __func__
);
2665 static int exfat_cont_expand(struct inode
*inode
, loff_t size
)
2667 struct address_space
*mapping
= inode
->i_mapping
;
2668 loff_t start
= i_size_read(inode
), count
= size
- i_size_read(inode
);
2669 struct timespec64 curtime
;
2672 err
= generic_cont_expand_simple(inode
, size
);
2676 curtime
= current_time(inode
);
2677 inode
->i_ctime
= curtime
;
2678 inode
->i_mtime
= curtime
;
2679 mark_inode_dirty(inode
);
2681 if (IS_SYNC(inode
)) {
2682 err
= filemap_fdatawrite_range(mapping
, start
,
2684 err2
= sync_mapping_buffers(mapping
);
2685 err
= (err
) ? (err
) : (err2
);
2686 err2
= write_inode_now(inode
, 1);
2687 err
= (err
) ? (err
) : (err2
);
2689 err
= filemap_fdatawait_range(mapping
, start
,
2695 static int exfat_allow_set_time(struct exfat_sb_info
*sbi
, struct inode
*inode
)
2697 mode_t allow_utime
= sbi
->options
.allow_utime
;
2699 if (!uid_eq(current_fsuid(), inode
->i_uid
)) {
2700 if (in_group_p(inode
->i_gid
))
2702 if (allow_utime
& MAY_WRITE
)
2706 /* use a default check */
2710 static int exfat_sanitize_mode(const struct exfat_sb_info
*sbi
,
2711 struct inode
*inode
, umode_t
*mode_ptr
)
2713 mode_t i_mode
, mask
, perm
;
2715 i_mode
= inode
->i_mode
;
2717 if (S_ISREG(i_mode
) || S_ISLNK(i_mode
))
2718 mask
= sbi
->options
.fs_fmask
;
2720 mask
= sbi
->options
.fs_dmask
;
2722 perm
= *mode_ptr
& ~(S_IFMT
| mask
);
2724 /* Of the r and x bits, all (subject to umask) must be present.*/
2725 if ((perm
& 0555) != (i_mode
& 0555))
2728 if (exfat_mode_can_hold_ro(inode
)) {
2730 * Of the w bits, either all (subject to umask) or none must be
2733 if ((perm
& 0222) && ((perm
& 0222) != (0222 & ~mask
)))
2737 * If exfat_mode_can_hold_ro(inode) is false, can't change w
2740 if ((perm
& 0222) != (0222 & ~mask
))
2744 *mode_ptr
&= S_IFMT
| perm
;
2749 static void exfat_truncate(struct inode
*inode
, loff_t old_size
)
2751 struct super_block
*sb
= inode
->i_sb
;
2752 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2753 struct fs_info_t
*p_fs
= &sbi
->fs_info
;
2754 struct timespec64 curtime
;
2760 * This protects against truncating a file bigger than it was then
2761 * trying to write into the hole.
2763 if (EXFAT_I(inode
)->mmu_private
> i_size_read(inode
))
2764 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
2766 if (EXFAT_I(inode
)->fid
.start_clu
== 0)
2769 err
= ffsTruncateFile(inode
, old_size
, i_size_read(inode
));
2773 curtime
= current_time(inode
);
2774 inode
->i_ctime
= curtime
;
2775 inode
->i_mtime
= curtime
;
2776 if (IS_DIRSYNC(inode
))
2777 (void)exfat_sync_inode(inode
);
2779 mark_inode_dirty(inode
);
2781 inode
->i_blocks
= ((i_size_read(inode
) + (p_fs
->cluster_size
- 1)) &
2782 ~((loff_t
)p_fs
->cluster_size
- 1)) >> 9;
2787 static int exfat_setattr(struct dentry
*dentry
, struct iattr
*attr
)
2789 struct exfat_sb_info
*sbi
= EXFAT_SB(dentry
->d_sb
);
2790 struct inode
*inode
= dentry
->d_inode
;
2791 unsigned int ia_valid
;
2795 pr_debug("%s entered\n", __func__
);
2797 if ((attr
->ia_valid
& ATTR_SIZE
) &&
2798 attr
->ia_size
> i_size_read(inode
)) {
2799 error
= exfat_cont_expand(inode
, attr
->ia_size
);
2800 if (error
|| attr
->ia_valid
== ATTR_SIZE
)
2802 attr
->ia_valid
&= ~ATTR_SIZE
;
2805 ia_valid
= attr
->ia_valid
;
2807 if ((ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
| ATTR_TIMES_SET
)) &&
2808 exfat_allow_set_time(sbi
, inode
)) {
2809 attr
->ia_valid
&= ~(ATTR_MTIME_SET
|
2814 error
= setattr_prepare(dentry
, attr
);
2815 attr
->ia_valid
= ia_valid
;
2819 if (((attr
->ia_valid
& ATTR_UID
) &&
2820 (!uid_eq(attr
->ia_uid
, sbi
->options
.fs_uid
))) ||
2821 ((attr
->ia_valid
& ATTR_GID
) &&
2822 (!gid_eq(attr
->ia_gid
, sbi
->options
.fs_gid
))) ||
2823 ((attr
->ia_valid
& ATTR_MODE
) &&
2824 (attr
->ia_mode
& ~(S_IFREG
| S_IFLNK
| S_IFDIR
| 0777)))) {
2829 * We don't return -EPERM here. Yes, strange, but this is too
2832 if (attr
->ia_valid
& ATTR_MODE
) {
2833 if (exfat_sanitize_mode(sbi
, inode
, &attr
->ia_mode
) < 0)
2834 attr
->ia_valid
&= ~ATTR_MODE
;
2837 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2839 if (attr
->ia_valid
& ATTR_SIZE
) {
2840 old_size
= i_size_read(inode
);
2841 down_write(&EXFAT_I(inode
)->truncate_lock
);
2842 truncate_setsize(inode
, attr
->ia_size
);
2843 exfat_truncate(inode
, old_size
);
2844 up_write(&EXFAT_I(inode
)->truncate_lock
);
2846 setattr_copy(inode
, attr
);
2847 mark_inode_dirty(inode
);
2849 pr_debug("%s exited\n", __func__
);
2853 static int exfat_getattr(const struct path
*path
, struct kstat
*stat
,
2854 u32 request_mask
, unsigned int flags
)
2856 struct inode
*inode
= path
->dentry
->d_inode
;
2858 pr_debug("%s entered\n", __func__
);
2860 generic_fillattr(inode
, stat
);
2861 stat
->blksize
= EXFAT_SB(inode
->i_sb
)->fs_info
.cluster_size
;
2863 pr_debug("%s exited\n", __func__
);
2867 static const struct inode_operations exfat_dir_inode_operations
= {
2868 .create
= exfat_create
,
2869 .lookup
= exfat_lookup
,
2870 .unlink
= exfat_unlink
,
2871 .symlink
= exfat_symlink
,
2872 .mkdir
= exfat_mkdir
,
2873 .rmdir
= exfat_rmdir
,
2874 .rename
= exfat_rename
,
2875 .setattr
= exfat_setattr
,
2876 .getattr
= exfat_getattr
,
2879 /*======================================================================*/
2880 /* File Operations */
2881 /*======================================================================*/
2882 static const char *exfat_get_link(struct dentry
*dentry
, struct inode
*inode
,
2883 struct delayed_call
*done
)
2885 struct exfat_inode_info
*ei
= EXFAT_I(inode
);
2888 char *cookie
= ei
->target
;
2891 return (char *)(ei
->target
);
2896 static const struct inode_operations exfat_symlink_inode_operations
= {
2897 .get_link
= exfat_get_link
,
2900 static int exfat_file_release(struct inode
*inode
, struct file
*filp
)
2902 struct super_block
*sb
= inode
->i_sb
;
2904 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2905 ffsSyncVol(sb
, false);
2909 static const struct file_operations exfat_file_operations
= {
2910 .llseek
= generic_file_llseek
,
2911 .read_iter
= generic_file_read_iter
,
2912 .write_iter
= generic_file_write_iter
,
2913 .mmap
= generic_file_mmap
,
2914 .release
= exfat_file_release
,
2915 .unlocked_ioctl
= exfat_generic_ioctl
,
2916 .fsync
= generic_file_fsync
,
2917 .splice_read
= generic_file_splice_read
,
2920 static const struct inode_operations exfat_file_inode_operations
= {
2921 .setattr
= exfat_setattr
,
2922 .getattr
= exfat_getattr
,
2925 /*======================================================================*/
2926 /* Address Space Operations */
2927 /*======================================================================*/
2929 static int exfat_bmap(struct inode
*inode
, sector_t sector
, sector_t
*phys
,
2930 unsigned long *mapped_blocks
, int *create
)
2932 struct super_block
*sb
= inode
->i_sb
;
2933 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
2934 struct fs_info_t
*p_fs
= &sbi
->fs_info
;
2935 const unsigned long blocksize
= sb
->s_blocksize
;
2936 const unsigned char blocksize_bits
= sb
->s_blocksize_bits
;
2937 sector_t last_block
;
2938 int err
, clu_offset
, sec_offset
;
2939 unsigned int cluster
;
2944 last_block
= (i_size_read(inode
) + (blocksize
- 1)) >> blocksize_bits
;
2945 if (sector
>= last_block
) {
2952 /* cluster offset */
2953 clu_offset
= sector
>> p_fs
->sectors_per_clu_bits
;
2955 /* sector offset in cluster */
2956 sec_offset
= sector
& (p_fs
->sectors_per_clu
- 1);
2958 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
2960 err
= ffsMapCluster(inode
, clu_offset
, &cluster
);
2962 if (!err
&& (cluster
!= CLUSTER_32(~0))) {
2963 *phys
= START_SECTOR(cluster
) + sec_offset
;
2964 *mapped_blocks
= p_fs
->sectors_per_clu
- sec_offset
;
2970 static int exfat_get_block(struct inode
*inode
, sector_t iblock
,
2971 struct buffer_head
*bh_result
, int create
)
2973 struct super_block
*sb
= inode
->i_sb
;
2974 unsigned long max_blocks
= bh_result
->b_size
>> inode
->i_blkbits
;
2976 unsigned long mapped_blocks
;
2981 err
= exfat_bmap(inode
, iblock
, &phys
, &mapped_blocks
, &create
);
2988 max_blocks
= min(mapped_blocks
, max_blocks
);
2990 EXFAT_I(inode
)->mmu_private
+= max_blocks
<<
2991 sb
->s_blocksize_bits
;
2992 set_buffer_new(bh_result
);
2994 map_bh(bh_result
, sb
, phys
);
2997 bh_result
->b_size
= max_blocks
<< sb
->s_blocksize_bits
;
3003 static int exfat_readpage(struct file
*file
, struct page
*page
)
3005 return mpage_readpage(page
, exfat_get_block
);
3008 static int exfat_readpages(struct file
*file
, struct address_space
*mapping
,
3009 struct list_head
*pages
, unsigned int nr_pages
)
3011 return mpage_readpages(mapping
, pages
, nr_pages
, exfat_get_block
);
3014 static int exfat_writepage(struct page
*page
, struct writeback_control
*wbc
)
3016 return block_write_full_page(page
, exfat_get_block
, wbc
);
3019 static int exfat_writepages(struct address_space
*mapping
,
3020 struct writeback_control
*wbc
)
3022 return mpage_writepages(mapping
, wbc
, exfat_get_block
);
3025 static void exfat_write_failed(struct address_space
*mapping
, loff_t to
)
3027 struct inode
*inode
= mapping
->host
;
3029 if (to
> i_size_read(inode
)) {
3030 truncate_pagecache(inode
, i_size_read(inode
));
3031 EXFAT_I(inode
)->fid
.size
= i_size_read(inode
);
3032 exfat_truncate(inode
, i_size_read(inode
));
3036 static int exfat_write_begin(struct file
*file
, struct address_space
*mapping
,
3037 loff_t pos
, unsigned int len
, unsigned int flags
,
3038 struct page
**pagep
, void **fsdata
)
3043 ret
= cont_write_begin(file
, mapping
, pos
, len
, flags
, pagep
, fsdata
,
3045 &EXFAT_I(mapping
->host
)->mmu_private
);
3048 exfat_write_failed(mapping
, pos
+ len
);
3052 static int exfat_write_end(struct file
*file
, struct address_space
*mapping
,
3053 loff_t pos
, unsigned int len
, unsigned int copied
,
3054 struct page
*pagep
, void *fsdata
)
3056 struct inode
*inode
= mapping
->host
;
3057 struct file_id_t
*fid
= &(EXFAT_I(inode
)->fid
);
3058 struct timespec64 curtime
;
3061 err
= generic_write_end(file
, mapping
, pos
, len
, copied
, pagep
, fsdata
);
3064 exfat_write_failed(mapping
, pos
+ len
);
3066 if (!(err
< 0) && !(fid
->attr
& ATTR_ARCHIVE
)) {
3067 curtime
= current_time(inode
);
3068 inode
->i_mtime
= curtime
;
3069 inode
->i_ctime
= curtime
;
3070 fid
->attr
|= ATTR_ARCHIVE
;
3071 mark_inode_dirty(inode
);
3076 static ssize_t
exfat_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
3078 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
3079 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
3083 rw
= iov_iter_rw(iter
);
3086 if (EXFAT_I(inode
)->mmu_private
< iov_iter_count(iter
))
3089 ret
= blockdev_direct_IO(iocb
, inode
, iter
, exfat_get_block
);
3091 if ((ret
< 0) && (rw
& WRITE
))
3092 exfat_write_failed(mapping
, iov_iter_count(iter
));
3096 static sector_t
_exfat_bmap(struct address_space
*mapping
, sector_t block
)
3100 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
3101 down_read(&EXFAT_I(mapping
->host
)->truncate_lock
);
3102 blocknr
= generic_block_bmap(mapping
, block
, exfat_get_block
);
3103 up_read(&EXFAT_I(mapping
->host
)->truncate_lock
);
3108 static const struct address_space_operations exfat_aops
= {
3109 .readpage
= exfat_readpage
,
3110 .readpages
= exfat_readpages
,
3111 .writepage
= exfat_writepage
,
3112 .writepages
= exfat_writepages
,
3113 .write_begin
= exfat_write_begin
,
3114 .write_end
= exfat_write_end
,
3115 .direct_IO
= exfat_direct_IO
,
3119 /*======================================================================*/
3120 /* Super Operations */
3121 /*======================================================================*/
3123 static struct inode
*exfat_iget(struct super_block
*sb
, loff_t i_pos
)
3125 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3126 struct exfat_inode_info
*info
;
3127 struct hlist_head
*head
= sbi
->inode_hashtable
+ exfat_hash(i_pos
);
3128 struct inode
*inode
= NULL
;
3130 spin_lock(&sbi
->inode_hash_lock
);
3131 hlist_for_each_entry(info
, head
, i_hash_fat
) {
3132 BUG_ON(info
->vfs_inode
.i_sb
!= sb
);
3134 if (i_pos
!= info
->i_pos
)
3136 inode
= igrab(&info
->vfs_inode
);
3140 spin_unlock(&sbi
->inode_hash_lock
);
3144 /* doesn't deal with root inode */
3145 static int exfat_fill_inode(struct inode
*inode
, struct file_id_t
*fid
)
3147 struct exfat_sb_info
*sbi
= EXFAT_SB(inode
->i_sb
);
3148 struct fs_info_t
*p_fs
= &sbi
->fs_info
;
3149 struct dir_entry_t info
;
3151 memcpy(&(EXFAT_I(inode
)->fid
), fid
, sizeof(struct file_id_t
));
3153 ffsReadStat(inode
, &info
);
3155 EXFAT_I(inode
)->i_pos
= 0;
3156 EXFAT_I(inode
)->target
= NULL
;
3157 inode
->i_uid
= sbi
->options
.fs_uid
;
3158 inode
->i_gid
= sbi
->options
.fs_gid
;
3159 INC_IVERSION(inode
);
3160 inode
->i_generation
= prandom_u32();
3162 if (info
.Attr
& ATTR_SUBDIR
) { /* directory */
3163 inode
->i_generation
&= ~1;
3164 inode
->i_mode
= exfat_make_mode(sbi
, info
.Attr
, 0777);
3165 inode
->i_op
= &exfat_dir_inode_operations
;
3166 inode
->i_fop
= &exfat_dir_operations
;
3168 i_size_write(inode
, info
.Size
);
3169 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3170 set_nlink(inode
, info
.NumSubdirs
);
3171 } else if (info
.Attr
& ATTR_SYMLINK
) { /* symbolic link */
3172 inode
->i_generation
|= 1;
3173 inode
->i_mode
= exfat_make_mode(sbi
, info
.Attr
, 0777);
3174 inode
->i_op
= &exfat_symlink_inode_operations
;
3176 i_size_write(inode
, info
.Size
);
3177 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3178 } else { /* regular file */
3179 inode
->i_generation
|= 1;
3180 inode
->i_mode
= exfat_make_mode(sbi
, info
.Attr
, 0777);
3181 inode
->i_op
= &exfat_file_inode_operations
;
3182 inode
->i_fop
= &exfat_file_operations
;
3183 inode
->i_mapping
->a_ops
= &exfat_aops
;
3184 inode
->i_mapping
->nrpages
= 0;
3186 i_size_write(inode
, info
.Size
);
3187 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3189 exfat_save_attr(inode
, info
.Attr
);
3191 inode
->i_blocks
= ((i_size_read(inode
) + (p_fs
->cluster_size
- 1))
3192 & ~((loff_t
)p_fs
->cluster_size
- 1)) >> 9;
3194 exfat_time_fat2unix(&inode
->i_mtime
, &info
.ModifyTimestamp
);
3195 exfat_time_fat2unix(&inode
->i_ctime
, &info
.CreateTimestamp
);
3196 exfat_time_fat2unix(&inode
->i_atime
, &info
.AccessTimestamp
);
3201 static struct inode
*exfat_build_inode(struct super_block
*sb
,
3202 struct file_id_t
*fid
, loff_t i_pos
)
3204 struct inode
*inode
;
3207 inode
= exfat_iget(sb
, i_pos
);
3210 inode
= new_inode(sb
);
3212 inode
= ERR_PTR(-ENOMEM
);
3215 inode
->i_ino
= iunique(sb
, EXFAT_ROOT_INO
);
3216 SET_IVERSION(inode
, 1);
3217 err
= exfat_fill_inode(inode
, fid
);
3220 inode
= ERR_PTR(err
);
3223 exfat_attach(inode
, i_pos
);
3224 insert_inode_hash(inode
);
3229 static int exfat_sync_inode(struct inode
*inode
)
3231 return exfat_write_inode(inode
, NULL
);
3234 static struct inode
*exfat_alloc_inode(struct super_block
*sb
)
3236 struct exfat_inode_info
*ei
;
3238 ei
= kmem_cache_alloc(exfat_inode_cachep
, GFP_NOFS
);
3242 init_rwsem(&ei
->truncate_lock
);
3244 return &ei
->vfs_inode
;
3247 static void exfat_destroy_inode(struct inode
*inode
)
3249 kfree(EXFAT_I(inode
)->target
);
3250 EXFAT_I(inode
)->target
= NULL
;
3252 kmem_cache_free(exfat_inode_cachep
, EXFAT_I(inode
));
3255 static int exfat_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
3257 struct dir_entry_t info
;
3259 if (inode
->i_ino
== EXFAT_ROOT_INO
)
3262 info
.Attr
= exfat_make_attr(inode
);
3263 info
.Size
= i_size_read(inode
);
3265 exfat_time_unix2fat(&inode
->i_mtime
, &info
.ModifyTimestamp
);
3266 exfat_time_unix2fat(&inode
->i_ctime
, &info
.CreateTimestamp
);
3267 exfat_time_unix2fat(&inode
->i_atime
, &info
.AccessTimestamp
);
3269 ffsWriteStat(inode
, &info
);
3274 static void exfat_evict_inode(struct inode
*inode
)
3276 truncate_inode_pages(&inode
->i_data
, 0);
3278 if (!inode
->i_nlink
)
3279 i_size_write(inode
, 0);
3280 invalidate_inode_buffers(inode
);
3282 exfat_detach(inode
);
3284 remove_inode_hash(inode
);
3287 static void exfat_free_super(struct exfat_sb_info
*sbi
)
3290 unload_nls(sbi
->nls_disk
);
3292 unload_nls(sbi
->nls_io
);
3293 if (sbi
->options
.iocharset
!= exfat_default_iocharset
)
3294 kfree(sbi
->options
.iocharset
);
3295 /* mutex_init is in exfat_fill_super function. only for 3.7+ */
3296 mutex_destroy(&sbi
->s_lock
);
3300 static void exfat_put_super(struct super_block
*sb
)
3302 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3304 if (__is_sb_dirty(sb
))
3305 exfat_write_super(sb
);
3309 sb
->s_fs_info
= NULL
;
3310 exfat_free_super(sbi
);
3313 static void exfat_write_super(struct super_block
*sb
)
3320 ffsSyncVol(sb
, true);
3325 static int exfat_sync_fs(struct super_block
*sb
, int wait
)
3329 if (__is_sb_dirty(sb
)) {
3332 err
= ffsSyncVol(sb
, true);
3339 static int exfat_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
3341 struct super_block
*sb
= dentry
->d_sb
;
3342 u64 id
= huge_encode_dev(sb
->s_bdev
->bd_dev
);
3343 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
3344 struct vol_info_t info
;
3346 if (p_fs
->used_clusters
== UINT_MAX
) {
3347 if (ffsGetVolInfo(sb
, &info
) == -EIO
)
3351 info
.FatType
= p_fs
->vol_type
;
3352 info
.ClusterSize
= p_fs
->cluster_size
;
3353 info
.NumClusters
= p_fs
->num_clusters
- 2;
3354 info
.UsedClusters
= p_fs
->used_clusters
;
3355 info
.FreeClusters
= info
.NumClusters
- info
.UsedClusters
;
3357 if (p_fs
->dev_ejected
)
3358 pr_info("[EXFAT] statfs on device that is ejected\n");
3361 buf
->f_type
= sb
->s_magic
;
3362 buf
->f_bsize
= info
.ClusterSize
;
3363 buf
->f_blocks
= info
.NumClusters
;
3364 buf
->f_bfree
= info
.FreeClusters
;
3365 buf
->f_bavail
= info
.FreeClusters
;
3366 buf
->f_fsid
.val
[0] = (u32
)id
;
3367 buf
->f_fsid
.val
[1] = (u32
)(id
>> 32);
3368 buf
->f_namelen
= 260;
3373 static int exfat_remount(struct super_block
*sb
, int *flags
, char *data
)
3375 *flags
|= SB_NODIRATIME
;
3379 static int exfat_show_options(struct seq_file
*m
, struct dentry
*root
)
3381 struct exfat_sb_info
*sbi
= EXFAT_SB(root
->d_sb
);
3382 struct exfat_mount_options
*opts
= &sbi
->options
;
3384 if (__kuid_val(opts
->fs_uid
))
3385 seq_printf(m
, ",uid=%u", __kuid_val(opts
->fs_uid
));
3386 if (__kgid_val(opts
->fs_gid
))
3387 seq_printf(m
, ",gid=%u", __kgid_val(opts
->fs_gid
));
3388 seq_printf(m
, ",fmask=%04o", opts
->fs_fmask
);
3389 seq_printf(m
, ",dmask=%04o", opts
->fs_dmask
);
3390 if (opts
->allow_utime
)
3391 seq_printf(m
, ",allow_utime=%04o", opts
->allow_utime
);
3393 seq_printf(m
, ",codepage=%s", sbi
->nls_disk
->charset
);
3395 seq_printf(m
, ",iocharset=%s", sbi
->nls_io
->charset
);
3396 seq_printf(m
, ",namecase=%u", opts
->casesensitive
);
3397 if (opts
->errors
== EXFAT_ERRORS_CONT
)
3398 seq_puts(m
, ",errors=continue");
3399 else if (opts
->errors
== EXFAT_ERRORS_PANIC
)
3400 seq_puts(m
, ",errors=panic");
3402 seq_puts(m
, ",errors=remount-ro");
3403 #ifdef CONFIG_EXFAT_DISCARD
3405 seq_puts(m
, ",discard");
3410 static const struct super_operations exfat_sops
= {
3411 .alloc_inode
= exfat_alloc_inode
,
3412 .destroy_inode
= exfat_destroy_inode
,
3413 .write_inode
= exfat_write_inode
,
3414 .evict_inode
= exfat_evict_inode
,
3415 .put_super
= exfat_put_super
,
3416 .sync_fs
= exfat_sync_fs
,
3417 .statfs
= exfat_statfs
,
3418 .remount_fs
= exfat_remount
,
3419 .show_options
= exfat_show_options
,
3422 /*======================================================================*/
3423 /* Export Operations */
3424 /*======================================================================*/
3426 static struct inode
*exfat_nfs_get_inode(struct super_block
*sb
, u64 ino
,
3429 struct inode
*inode
= NULL
;
3431 if (ino
< EXFAT_ROOT_INO
)
3433 inode
= ilookup(sb
, ino
);
3435 if (inode
&& generation
&& (inode
->i_generation
!= generation
)) {
3443 static struct dentry
*exfat_fh_to_dentry(struct super_block
*sb
,
3444 struct fid
*fid
, int fh_len
,
3447 return generic_fh_to_dentry(sb
, fid
, fh_len
, fh_type
,
3448 exfat_nfs_get_inode
);
3451 static struct dentry
*exfat_fh_to_parent(struct super_block
*sb
,
3452 struct fid
*fid
, int fh_len
,
3455 return generic_fh_to_parent(sb
, fid
, fh_len
, fh_type
,
3456 exfat_nfs_get_inode
);
3459 static const struct export_operations exfat_export_ops
= {
3460 .fh_to_dentry
= exfat_fh_to_dentry
,
3461 .fh_to_parent
= exfat_fh_to_parent
,
3464 /*======================================================================*/
3465 /* Super Block Read Operations */
3466 /*======================================================================*/
3484 #ifdef CONFIG_EXFAT_DISCARD
3486 #endif /* EXFAT_CONFIG_DISCARD */
3489 static const match_table_t exfat_tokens
= {
3490 {Opt_uid
, "uid=%u"},
3491 {Opt_gid
, "gid=%u"},
3492 {Opt_umask
, "umask=%o"},
3493 {Opt_dmask
, "dmask=%o"},
3494 {Opt_fmask
, "fmask=%o"},
3495 {Opt_allow_utime
, "allow_utime=%o"},
3496 {Opt_codepage
, "codepage=%u"},
3497 {Opt_charset
, "iocharset=%s"},
3498 {Opt_namecase
, "namecase=%u"},
3499 {Opt_debug
, "debug"},
3500 {Opt_err_cont
, "errors=continue"},
3501 {Opt_err_panic
, "errors=panic"},
3502 {Opt_err_ro
, "errors=remount-ro"},
3503 {Opt_utf8_hack
, "utf8"},
3504 #ifdef CONFIG_EXFAT_DISCARD
3505 {Opt_discard
, "discard"},
3506 #endif /* CONFIG_EXFAT_DISCARD */
3510 static int parse_options(char *options
, int silent
, int *debug
,
3511 struct exfat_mount_options
*opts
)
3514 substring_t args
[MAX_OPT_ARGS
];
3518 opts
->fs_uid
= current_uid();
3519 opts
->fs_gid
= current_gid();
3520 opts
->fs_fmask
= current
->fs
->umask
;
3521 opts
->fs_dmask
= current
->fs
->umask
;
3522 opts
->allow_utime
= U16_MAX
;
3523 opts
->codepage
= exfat_default_codepage
;
3524 opts
->iocharset
= exfat_default_iocharset
;
3525 opts
->casesensitive
= 0;
3526 opts
->errors
= EXFAT_ERRORS_RO
;
3527 #ifdef CONFIG_EXFAT_DISCARD
3535 while ((p
= strsep(&options
, ","))) {
3541 token
= match_token(p
, exfat_tokens
, args
);
3544 if (match_int(&args
[0], &option
))
3546 opts
->fs_uid
= KUIDT_INIT(option
);
3549 if (match_int(&args
[0], &option
))
3551 opts
->fs_gid
= KGIDT_INIT(option
);
3556 if (match_octal(&args
[0], &option
))
3558 if (token
!= Opt_dmask
)
3559 opts
->fs_fmask
= option
;
3560 if (token
!= Opt_fmask
)
3561 opts
->fs_dmask
= option
;
3563 case Opt_allow_utime
:
3564 if (match_octal(&args
[0], &option
))
3566 opts
->allow_utime
= option
& 0022;
3569 if (match_int(&args
[0], &option
))
3571 opts
->codepage
= option
;
3574 if (opts
->iocharset
!= exfat_default_iocharset
)
3575 kfree(opts
->iocharset
);
3576 iocharset
= match_strdup(&args
[0]);
3579 opts
->iocharset
= iocharset
;
3582 if (match_int(&args
[0], &option
))
3584 opts
->casesensitive
= option
;
3587 opts
->errors
= EXFAT_ERRORS_CONT
;
3590 opts
->errors
= EXFAT_ERRORS_PANIC
;
3593 opts
->errors
= EXFAT_ERRORS_RO
;
3598 #ifdef CONFIG_EXFAT_DISCARD
3602 #endif /* CONFIG_EXFAT_DISCARD */
3607 pr_err("[EXFAT] Unrecognized mount option %s or missing value\n",
3614 if (opts
->allow_utime
== U16_MAX
)
3615 opts
->allow_utime
= ~opts
->fs_dmask
& 0022;
3620 static void exfat_hash_init(struct super_block
*sb
)
3622 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3625 spin_lock_init(&sbi
->inode_hash_lock
);
3626 for (i
= 0; i
< EXFAT_HASH_SIZE
; i
++)
3627 INIT_HLIST_HEAD(&sbi
->inode_hashtable
[i
]);
3630 static int exfat_read_root(struct inode
*inode
)
3632 struct super_block
*sb
= inode
->i_sb
;
3633 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3634 struct fs_info_t
*p_fs
= &sbi
->fs_info
;
3635 struct timespec64 curtime
;
3636 struct dir_entry_t info
;
3638 EXFAT_I(inode
)->fid
.dir
.dir
= p_fs
->root_dir
;
3639 EXFAT_I(inode
)->fid
.dir
.flags
= 0x01;
3640 EXFAT_I(inode
)->fid
.entry
= -1;
3641 EXFAT_I(inode
)->fid
.start_clu
= p_fs
->root_dir
;
3642 EXFAT_I(inode
)->fid
.flags
= 0x01;
3643 EXFAT_I(inode
)->fid
.type
= TYPE_DIR
;
3644 EXFAT_I(inode
)->fid
.rwoffset
= 0;
3645 EXFAT_I(inode
)->fid
.hint_last_off
= -1;
3647 EXFAT_I(inode
)->target
= NULL
;
3649 ffsReadStat(inode
, &info
);
3651 inode
->i_uid
= sbi
->options
.fs_uid
;
3652 inode
->i_gid
= sbi
->options
.fs_gid
;
3653 INC_IVERSION(inode
);
3654 inode
->i_generation
= 0;
3655 inode
->i_mode
= exfat_make_mode(sbi
, ATTR_SUBDIR
, 0777);
3656 inode
->i_op
= &exfat_dir_inode_operations
;
3657 inode
->i_fop
= &exfat_dir_operations
;
3659 i_size_write(inode
, info
.Size
);
3660 inode
->i_blocks
= ((i_size_read(inode
) + (p_fs
->cluster_size
- 1))
3661 & ~((loff_t
)p_fs
->cluster_size
- 1)) >> 9;
3662 EXFAT_I(inode
)->i_pos
= ((loff_t
)p_fs
->root_dir
<< 32) | 0xffffffff;
3663 EXFAT_I(inode
)->mmu_private
= i_size_read(inode
);
3665 exfat_save_attr(inode
, ATTR_SUBDIR
);
3666 curtime
= current_time(inode
);
3667 inode
->i_mtime
= curtime
;
3668 inode
->i_atime
= curtime
;
3669 inode
->i_ctime
= curtime
;
3670 set_nlink(inode
, info
.NumSubdirs
+ 2);
3675 static void setup_dops(struct super_block
*sb
)
3677 if (EXFAT_SB(sb
)->options
.casesensitive
== 0)
3678 sb
->s_d_op
= &exfat_ci_dentry_ops
;
3680 sb
->s_d_op
= &exfat_dentry_ops
;
3683 static int exfat_fill_super(struct super_block
*sb
, void *data
, int silent
)
3685 struct inode
*root_inode
= NULL
;
3686 struct exfat_sb_info
*sbi
;
3691 * GFP_KERNEL is ok here, because while we do hold the
3692 * supeblock lock, memory pressure can't call back into
3693 * the filesystem, since we're only just about to mount
3694 * it and have no inodes etc active!
3696 sbi
= kvzalloc(sizeof(*sbi
), GFP_KERNEL
);
3699 mutex_init(&sbi
->s_lock
);
3700 sb
->s_fs_info
= sbi
;
3701 sb
->s_flags
|= SB_NODIRATIME
;
3702 sb
->s_magic
= EXFAT_SUPER_MAGIC
;
3703 sb
->s_op
= &exfat_sops
;
3704 sb
->s_export_op
= &exfat_export_ops
;
3706 error
= parse_options(data
, silent
, &debug
, &sbi
->options
);
3713 sb_min_blocksize(sb
, 512);
3714 sb
->s_maxbytes
= 0x7fffffffffffffffLL
; /* maximum file size */
3716 ret
= ffsMountVol(sb
);
3719 pr_err("[EXFAT] ffsMountVol failed\n");
3724 /* set up enough so that it can read an inode */
3725 exfat_hash_init(sb
);
3728 * The low byte of FAT's first entry must have same value with
3729 * media-field. But in real world, too many devices is
3730 * writing wrong value. So, removed that validity check.
3732 * if (FAT_FIRST_ENT(sb, media) != first)
3735 sbi
->nls_io
= load_nls(sbi
->options
.iocharset
);
3738 root_inode
= new_inode(sb
);
3741 root_inode
->i_ino
= EXFAT_ROOT_INO
;
3742 SET_IVERSION(root_inode
, 1);
3744 error
= exfat_read_root(root_inode
);
3748 exfat_attach(root_inode
, EXFAT_I(root_inode
)->i_pos
);
3749 insert_inode_hash(root_inode
);
3750 sb
->s_root
= d_make_root(root_inode
);
3752 pr_err("[EXFAT] Getting the root inode failed\n");
3763 sb
->s_fs_info
= NULL
;
3764 exfat_free_super(sbi
);
3768 static struct dentry
*exfat_fs_mount(struct file_system_type
*fs_type
,
3769 int flags
, const char *dev_name
,
3772 return mount_bdev(fs_type
, flags
, dev_name
, data
, exfat_fill_super
);
3775 static void init_once(void *foo
)
3777 struct exfat_inode_info
*ei
= (struct exfat_inode_info
*)foo
;
3779 INIT_HLIST_NODE(&ei
->i_hash_fat
);
3780 inode_init_once(&ei
->vfs_inode
);
3783 static int __init
exfat_init_inodecache(void)
3785 exfat_inode_cachep
= kmem_cache_create("exfat_inode_cache",
3786 sizeof(struct exfat_inode_info
),
3788 (SLAB_RECLAIM_ACCOUNT
|
3791 if (!exfat_inode_cachep
)
3796 static void __exit
exfat_destroy_inodecache(void)
3799 * Make sure all delayed rcu free inodes are flushed before we
3803 kmem_cache_destroy(exfat_inode_cachep
);
3806 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
3807 static void exfat_debug_kill_sb(struct super_block
*sb
)
3809 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
3810 struct block_device
*bdev
= sb
->s_bdev
;
3811 struct fs_info_t
*p_fs
= &(EXFAT_SB(sb
)->fs_info
);
3816 flags
= sbi
->debug_flags
;
3818 if (flags
& EXFAT_DEBUGFLAGS_INVALID_UMOUNT
) {
3820 * invalidate_bdev drops all device cache include
3821 * dirty. We use this to simulate device removal.
3823 mutex_lock(&p_fs
->v_mutex
);
3824 exfat_fat_release_all(sb
);
3825 exfat_buf_release_all(sb
);
3826 mutex_unlock(&p_fs
->v_mutex
);
3828 invalidate_bdev(bdev
);
3832 kill_block_super(sb
);
3834 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
3836 static struct file_system_type exfat_fs_type
= {
3837 .owner
= THIS_MODULE
,
3839 .mount
= exfat_fs_mount
,
3840 #ifdef CONFIG_EXFAT_KERNEL_DEBUG
3841 .kill_sb
= exfat_debug_kill_sb
,
3843 .kill_sb
= kill_block_super
,
3844 #endif /* CONFIG_EXFAT_KERNEL_DEBUG */
3845 .fs_flags
= FS_REQUIRES_DEV
,
3848 static int __init
init_exfat(void)
3852 BUILD_BUG_ON(sizeof(struct dentry_t
) != DENTRY_SIZE
);
3853 BUILD_BUG_ON(sizeof(struct dos_dentry_t
) != DENTRY_SIZE
);
3854 BUILD_BUG_ON(sizeof(struct ext_dentry_t
) != DENTRY_SIZE
);
3855 BUILD_BUG_ON(sizeof(struct file_dentry_t
) != DENTRY_SIZE
);
3856 BUILD_BUG_ON(sizeof(struct strm_dentry_t
) != DENTRY_SIZE
);
3857 BUILD_BUG_ON(sizeof(struct name_dentry_t
) != DENTRY_SIZE
);
3858 BUILD_BUG_ON(sizeof(struct bmap_dentry_t
) != DENTRY_SIZE
);
3859 BUILD_BUG_ON(sizeof(struct case_dentry_t
) != DENTRY_SIZE
);
3860 BUILD_BUG_ON(sizeof(struct volm_dentry_t
) != DENTRY_SIZE
);
3862 pr_info("exFAT: Version %s\n", EXFAT_VERSION
);
3864 err
= exfat_init_inodecache();
3868 err
= register_filesystem(&exfat_fs_type
);
3875 static void __exit
exit_exfat(void)
3877 exfat_destroy_inodecache();
3878 unregister_filesystem(&exfat_fs_type
);
3881 module_init(init_exfat
);
3882 module_exit(exit_exfat
);
3884 MODULE_LICENSE("GPL");
3885 MODULE_DESCRIPTION("exFAT Filesystem Driver");
3886 MODULE_ALIAS_FS("exfat");