1
2
3
4
5
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/time.h>
9#include <linux/slab.h>
10#include <linux/mm.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>
34
35#define EXFAT_VERSION "1.3.0"
36
37#include "exfat.h"
38
39static struct kmem_cache *exfat_inode_cachep;
40
41static int exfat_default_codepage = CONFIG_STAGING_EXFAT_DEFAULT_CODEPAGE;
42static char exfat_default_iocharset[] = CONFIG_STAGING_EXFAT_DEFAULT_IOCHARSET;
43
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))
47
48static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
49static int exfat_sync_inode(struct inode *inode);
50static struct inode *exfat_build_inode(struct super_block *sb,
51 struct file_id_t *fid, loff_t i_pos);
52static int exfat_write_inode(struct inode *inode,
53 struct writeback_control *wbc);
54static void exfat_write_super(struct super_block *sb);
55
56#define UNIX_SECS_1980 315532800L
57#define UNIX_SECS_2108 4354819200L
58
59
60static void exfat_time_fat2unix(struct timespec64 *ts, struct date_time_t *tp)
61{
62 ts->tv_sec = mktime64(tp->Year + 1980, tp->Month + 1, tp->Day,
63 tp->Hour, tp->Minute, tp->Second);
64
65 ts->tv_nsec = tp->MilliSecond * NSEC_PER_MSEC;
66}
67
68
69static void exfat_time_unix2fat(struct timespec64 *ts, struct date_time_t *tp)
70{
71 time64_t second = ts->tv_sec;
72 struct tm tm;
73
74 time64_to_tm(second, 0, &tm);
75
76 if (second < UNIX_SECS_1980) {
77 tp->MilliSecond = 0;
78 tp->Second = 0;
79 tp->Minute = 0;
80 tp->Hour = 0;
81 tp->Day = 1;
82 tp->Month = 1;
83 tp->Year = 0;
84 return;
85 }
86
87 if (second >= UNIX_SECS_2108) {
88 tp->MilliSecond = 999;
89 tp->Second = 59;
90 tp->Minute = 59;
91 tp->Hour = 23;
92 tp->Day = 31;
93 tp->Month = 12;
94 tp->Year = 127;
95 return;
96 }
97
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;
105}
106
107struct timestamp_t *tm_current(struct timestamp_t *tp)
108{
109 time64_t second = ktime_get_real_seconds();
110 struct tm tm;
111
112 time64_to_tm(second, 0, &tm);
113
114 if (second < UNIX_SECS_1980) {
115 tp->sec = 0;
116 tp->min = 0;
117 tp->hour = 0;
118 tp->day = 1;
119 tp->mon = 1;
120 tp->year = 0;
121 return tp;
122 }
123
124 if (second >= UNIX_SECS_2108) {
125 tp->sec = 59;
126 tp->min = 59;
127 tp->hour = 23;
128 tp->day = 31;
129 tp->mon = 12;
130 tp->year = 127;
131 return tp;
132 }
133
134 tp->sec = tm.tm_sec;
135 tp->min = tm.tm_min;
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;
140
141 return tp;
142}
143
144static void __lock_super(struct super_block *sb)
145{
146 struct exfat_sb_info *sbi = EXFAT_SB(sb);
147
148 mutex_lock(&sbi->s_lock);
149}
150
151static void __unlock_super(struct super_block *sb)
152{
153 struct exfat_sb_info *sbi = EXFAT_SB(sb);
154
155 mutex_unlock(&sbi->s_lock);
156}
157
158static int __is_sb_dirty(struct super_block *sb)
159{
160 struct exfat_sb_info *sbi = EXFAT_SB(sb);
161
162 return sbi->s_dirt;
163}
164
165static void __set_sb_clean(struct super_block *sb)
166{
167 struct exfat_sb_info *sbi = EXFAT_SB(sb);
168
169 sbi->s_dirt = 0;
170}
171
172static int __exfat_revalidate(struct dentry *dentry)
173{
174 return 0;
175}
176
177static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
178{
179 if (flags & LOOKUP_RCU)
180 return -ECHILD;
181
182 if (dentry->d_inode)
183 return 1;
184 return __exfat_revalidate(dentry);
185}
186
187static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
188{
189 if (flags & LOOKUP_RCU)
190 return -ECHILD;
191
192 if (dentry->d_inode)
193 return 1;
194
195 if (!flags)
196 return 0;
197
198 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
199 return 0;
200
201 return __exfat_revalidate(dentry);
202}
203
204static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
205{
206 while (len && name[len - 1] == '.')
207 len--;
208 return len;
209}
210
211static unsigned int exfat_striptail_len(const struct qstr *qstr)
212{
213 return __exfat_striptail_len(qstr->len, qstr->name);
214}
215
216static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
217{
218 qstr->hash = full_name_hash(dentry, qstr->name,
219 exfat_striptail_len(qstr));
220 return 0;
221}
222
223static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
224{
225 struct super_block *sb = dentry->d_sb;
226 const unsigned char *name;
227 unsigned int len;
228 unsigned long hash;
229
230 name = qstr->name;
231 len = exfat_striptail_len(qstr);
232
233 hash = init_name_hash(dentry);
234 while (len--)
235 hash = partial_name_hash(nls_upper(sb, *name++), hash);
236 qstr->hash = end_name_hash(hash);
237
238 return 0;
239}
240
241static int exfat_cmpi(const struct dentry *dentry, unsigned int len,
242 const char *str, const struct qstr *name)
243{
244 struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
245 unsigned int alen, blen;
246
247 alen = exfat_striptail_len(name);
248 blen = __exfat_striptail_len(len, str);
249 if (alen == blen) {
250 if (!t) {
251 if (strncasecmp(name->name, str, alen) == 0)
252 return 0;
253 } else {
254 if (nls_strnicmp(t, name->name, str, alen) == 0)
255 return 0;
256 }
257 }
258 return 1;
259}
260
261static int exfat_cmp(const struct dentry *dentry, unsigned int len,
262 const char *str, const struct qstr *name)
263{
264 unsigned int alen, blen;
265
266 alen = exfat_striptail_len(name);
267 blen = __exfat_striptail_len(len, str);
268 if (alen == blen) {
269 if (strncmp(name->name, str, alen) == 0)
270 return 0;
271 }
272 return 1;
273}
274
275static 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,
279};
280
281static const struct dentry_operations exfat_dentry_ops = {
282 .d_revalidate = exfat_revalidate,
283 .d_hash = exfat_d_hash,
284 .d_compare = exfat_cmp,
285};
286
287static DEFINE_MUTEX(z_mutex);
288
289static inline void fs_sync(struct super_block *sb, bool do_sync)
290{
291 if (do_sync)
292 exfat_bdev_sync(sb);
293}
294
295
296
297
298
299
300
301
302static inline int exfat_mode_can_hold_ro(struct inode *inode)
303{
304 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
305
306 if (S_ISDIR(inode->i_mode))
307 return 0;
308
309 if ((~sbi->options.fs_fmask) & 0222)
310 return 1;
311 return 0;
312}
313
314
315static inline mode_t exfat_make_mode(struct exfat_sb_info *sbi, u32 attr,
316 mode_t mode)
317{
318 if ((attr & ATTR_READONLY) && !(attr & ATTR_SUBDIR))
319 mode &= ~0222;
320
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;
325 else
326 return (mode & ~sbi->options.fs_fmask) | S_IFREG;
327}
328
329
330static inline u32 exfat_make_attr(struct inode *inode)
331{
332 if (exfat_mode_can_hold_ro(inode) && !(inode->i_mode & 0222))
333 return (EXFAT_I(inode)->fid.attr) | ATTR_READONLY;
334 else
335 return EXFAT_I(inode)->fid.attr;
336}
337
338static inline void exfat_save_attr(struct inode *inode, u32 attr)
339{
340 if (exfat_mode_can_hold_ro(inode))
341 EXFAT_I(inode)->fid.attr = attr & ATTR_RWMASK;
342 else
343 EXFAT_I(inode)->fid.attr = attr & (ATTR_RWMASK | ATTR_READONLY);
344}
345
346static int ffsMountVol(struct super_block *sb)
347{
348 int i, ret;
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);
353
354 pr_info("[EXFAT] trying to mount...\n");
355
356 mutex_lock(&z_mutex);
357
358 exfat_buf_init(sb);
359
360 mutex_init(&p_fs->v_mutex);
361 p_fs->dev_ejected = 0;
362
363
364 exfat_bdev_open(sb);
365
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);
369 ret = -EINVAL;
370 goto out;
371 }
372 if (p_bd->sector_size > sb->s_blocksize)
373 sb_set_blocksize(sb, p_bd->sector_size);
374
375
376 if (sector_read(sb, 0, &tmp_bh, 1) != 0) {
377 ret = -EIO;
378 goto out;
379 }
380
381 p_fs->PBR_sector = 0;
382
383 p_pbr = (struct pbr_sector_t *)tmp_bh->b_data;
384
385
386 if (GET16_A(p_pbr->signature) != PBR_SIGNATURE) {
387 brelse(tmp_bh);
388 exfat_bdev_close(sb);
389 ret = -EFSCORRUPTED;
390 goto out;
391 }
392
393
394 for (i = 0; i < 53; i++)
395 if (p_pbr->bpb[i])
396 break;
397
398 if (i < 53) {
399
400 ret = -EINVAL;
401 pr_info("EXFAT: Attempted to mount VFAT filesystem\n");
402 goto out;
403 } else {
404 ret = exfat_mount(sb, p_pbr);
405 }
406
407 brelse(tmp_bh);
408
409 if (ret) {
410 exfat_bdev_close(sb);
411 goto out;
412 }
413
414 ret = load_alloc_bitmap(sb);
415 if (ret) {
416 exfat_bdev_close(sb);
417 goto out;
418 }
419 ret = load_upcase_table(sb);
420 if (ret) {
421 free_alloc_bitmap(sb);
422 exfat_bdev_close(sb);
423 goto out;
424 }
425
426 if (p_fs->dev_ejected) {
427 free_upcase_table(sb);
428 free_alloc_bitmap(sb);
429 exfat_bdev_close(sb);
430 ret = -EIO;
431 goto out;
432 }
433
434 pr_info("[EXFAT] mounted successfully\n");
435
436out:
437 mutex_unlock(&z_mutex);
438
439 return ret;
440}
441
442static int ffsUmountVol(struct super_block *sb)
443{
444 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
445 int err = 0;
446
447 pr_info("[EXFAT] trying to unmount...\n");
448
449 mutex_lock(&z_mutex);
450
451
452 mutex_lock(&p_fs->v_mutex);
453
454 fs_sync(sb, true);
455 fs_set_vol_flags(sb, VOL_CLEAN);
456
457 free_upcase_table(sb);
458 free_alloc_bitmap(sb);
459
460 exfat_fat_release_all(sb);
461 exfat_buf_release_all(sb);
462
463
464 exfat_bdev_close(sb);
465
466 if (p_fs->dev_ejected) {
467 pr_info("[EXFAT] unmounted with media errors. Device is already ejected.\n");
468 err = -EIO;
469 }
470
471 exfat_buf_shutdown(sb);
472
473
474 mutex_unlock(&p_fs->v_mutex);
475 mutex_unlock(&z_mutex);
476
477 pr_info("[EXFAT] unmounted successfully\n");
478
479 return err;
480}
481
482static int ffsGetVolInfo(struct super_block *sb, struct vol_info_t *info)
483{
484 int err = 0;
485 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
486
487
488 if (!info)
489 return -EINVAL;
490
491
492 mutex_lock(&p_fs->v_mutex);
493
494 if (p_fs->used_clusters == UINT_MAX)
495 p_fs->used_clusters = exfat_count_used_clusters(sb);
496
497 info->FatType = p_fs->vol_type;
498 info->ClusterSize = p_fs->cluster_size;
499 info->NumClusters = p_fs->num_clusters - 2;
500 info->UsedClusters = p_fs->used_clusters;
501 info->FreeClusters = info->NumClusters - info->UsedClusters;
502
503 if (p_fs->dev_ejected)
504 err = -EIO;
505
506
507 mutex_unlock(&p_fs->v_mutex);
508
509 return err;
510}
511
512static int ffsSyncVol(struct super_block *sb, bool do_sync)
513{
514 int err = 0;
515 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
516
517
518 mutex_lock(&p_fs->v_mutex);
519
520
521 fs_sync(sb, do_sync);
522 fs_set_vol_flags(sb, VOL_CLEAN);
523
524 if (p_fs->dev_ejected)
525 err = -EIO;
526
527
528 mutex_unlock(&p_fs->v_mutex);
529
530 return err;
531}
532
533
534
535
536
537static int ffsLookupFile(struct inode *inode, char *path, struct file_id_t *fid)
538{
539 int ret, dentry, num_entries;
540 struct chain_t dir;
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);
547
548 pr_debug("%s entered\n", __func__);
549
550
551 if (!fid || !path || (*path == '\0'))
552 return -EINVAL;
553
554
555 mutex_lock(&p_fs->v_mutex);
556
557
558 ret = resolve_path(inode, path, &dir, &uni_name);
559 if (ret)
560 goto out;
561
562 ret = get_num_entries_and_dos_name(sb, &dir, &uni_name, &num_entries,
563 &dos_name);
564 if (ret)
565 goto out;
566
567
568 dentry = exfat_find_dir_entry(sb, &dir, &uni_name, num_entries,
569 &dos_name, TYPE_ALL);
570 if (dentry < -1) {
571 ret = -ENOENT;
572 goto out;
573 }
574
575 fid->dir.dir = dir.dir;
576 fid->dir.size = dir.size;
577 fid->dir.flags = dir.flags;
578 fid->entry = dentry;
579
580 if (dentry == -1) {
581 fid->type = TYPE_DIR;
582 fid->rwoffset = 0;
583 fid->hint_last_off = -1;
584
585 fid->attr = ATTR_SUBDIR;
586 fid->flags = 0x01;
587 fid->size = 0;
588 fid->start_clu = p_fs->root_dir;
589 } else {
590 es = get_entry_set_in_dir(sb, &dir, dentry,
591 ES_2_ENTRIES, &ep);
592 if (!es) {
593 ret = -ENOENT;
594 goto out;
595 }
596 ep2 = ep + 1;
597
598 fid->type = exfat_get_entry_type(ep);
599 fid->rwoffset = 0;
600 fid->hint_last_off = -1;
601 fid->attr = exfat_get_entry_attr(ep);
602
603 fid->size = exfat_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);
607 } else {
608 fid->flags = exfat_get_entry_flag(ep2);
609 fid->start_clu = exfat_get_entry_clu0(ep2);
610 }
611
612 release_entry_set(es);
613 }
614
615 if (p_fs->dev_ejected)
616 ret = -EIO;
617out:
618
619 mutex_unlock(&p_fs->v_mutex);
620
621 return ret;
622}
623
624static int ffsCreateFile(struct inode *inode, char *path, u8 mode,
625 struct file_id_t *fid)
626{
627 struct chain_t dir;
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);
631 int ret = 0;
632
633
634 if (!fid || !path || (*path == '\0'))
635 return -EINVAL;
636
637
638 mutex_lock(&p_fs->v_mutex);
639
640
641 ret = resolve_path(inode, path, &dir, &uni_name);
642 if (ret)
643 goto out;
644
645 fs_set_vol_flags(sb, VOL_DIRTY);
646
647
648 ret = create_file(inode, &dir, &uni_name, mode, fid);
649
650#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
651 fs_sync(sb, true);
652 fs_set_vol_flags(sb, VOL_CLEAN);
653#endif
654
655 if (p_fs->dev_ejected)
656 ret = -EIO;
657
658out:
659
660 mutex_unlock(&p_fs->v_mutex);
661
662 return ret;
663}
664
665static int ffsReadFile(struct inode *inode, struct file_id_t *fid, void *buffer,
666 u64 count, u64 *rcount)
667{
668 s32 offset, sec_offset, clu_offset;
669 u32 clu;
670 int ret = 0;
671 sector_t LogSector;
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);
677
678
679 if (!fid)
680 return -EINVAL;
681
682
683 if (!buffer)
684 return -EINVAL;
685
686
687 mutex_lock(&p_fs->v_mutex);
688
689
690 if (fid->type != TYPE_FILE) {
691 ret = -EPERM;
692 goto out;
693 }
694
695 if (fid->rwoffset > fid->size)
696 fid->rwoffset = fid->size;
697
698 if (count > (fid->size - fid->rwoffset))
699 count = fid->size - fid->rwoffset;
700
701 if (count == 0) {
702 if (rcount)
703 *rcount = 0;
704 ret = 0;
705 goto out;
706 }
707
708 read_bytes = 0;
709
710 while (count > 0) {
711 clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
712 clu = fid->start_clu;
713
714 if (fid->flags == 0x03) {
715 clu += clu_offset;
716 } else {
717
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;
722 }
723
724 while (clu_offset > 0) {
725
726 if (exfat_fat_read(sb, clu, &clu) == -1) {
727 ret = -EIO;
728 goto out;
729 }
730
731 clu_offset--;
732 }
733 }
734
735
736 fid->hint_last_off = (s32)(fid->rwoffset >>
737 p_fs->cluster_size_bits);
738 fid->hint_last_clu = clu;
739
740
741 offset = (s32)(fid->rwoffset & (p_fs->cluster_size - 1));
742
743
744 sec_offset = offset >> p_bd->sector_size_bits;
745
746
747 offset &= p_bd->sector_size_mask;
748
749 LogSector = START_SECTOR(clu) + sec_offset;
750
751 oneblkread = (u64)(p_bd->sector_size - offset);
752 if (oneblkread > count)
753 oneblkread = count;
754
755 if ((offset == 0) && (oneblkread == p_bd->sector_size)) {
756 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
757 0)
758 goto err_out;
759 memcpy((char *)buffer + read_bytes,
760 (char *)tmp_bh->b_data, (s32)oneblkread);
761 } else {
762 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
763 0)
764 goto err_out;
765 memcpy((char *)buffer + read_bytes,
766 (char *)tmp_bh->b_data + offset,
767 (s32)oneblkread);
768 }
769 count -= oneblkread;
770 read_bytes += oneblkread;
771 fid->rwoffset += oneblkread;
772 }
773 brelse(tmp_bh);
774
775
776err_out:
777
778 if (rcount)
779 *rcount = read_bytes;
780
781 if (p_fs->dev_ejected)
782 ret = -EIO;
783
784out:
785
786 mutex_unlock(&p_fs->v_mutex);
787
788 return ret;
789}
790
791static int ffsWriteFile(struct inode *inode, struct file_id_t *fid,
792 void *buffer, u64 count, u64 *wcount)
793{
794 bool modified = false;
795 s32 offset, sec_offset, clu_offset;
796 s32 num_clusters, num_alloc, num_alloced = (s32)~0;
797 int ret = 0;
798 u32 clu, last_clu;
799 sector_t LogSector;
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);
809
810
811 if (!fid)
812 return -EINVAL;
813
814
815 if (!buffer)
816 return -EINVAL;
817
818
819 mutex_lock(&p_fs->v_mutex);
820
821
822 if (fid->type != TYPE_FILE) {
823 ret = -EPERM;
824 goto out;
825 }
826
827 if (fid->rwoffset > fid->size)
828 fid->rwoffset = fid->size;
829
830 if (count == 0) {
831 if (wcount)
832 *wcount = 0;
833 ret = 0;
834 goto out;
835 }
836
837 fs_set_vol_flags(sb, VOL_DIRTY);
838
839 if (fid->size == 0)
840 num_clusters = 0;
841 else
842 num_clusters = (s32)((fid->size - 1) >>
843 p_fs->cluster_size_bits) + 1;
844
845 write_bytes = 0;
846
847 while (count > 0) {
848 clu_offset = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
849 clu = fid->start_clu;
850 last_clu = fid->start_clu;
851
852 if (fid->flags == 0x03) {
853 if ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
854 last_clu += clu_offset - 1;
855
856 if (clu_offset == num_clusters)
857 clu = CLUSTER_32(~0);
858 else
859 clu += clu_offset;
860 }
861 } else {
862
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;
867 }
868
869 while ((clu_offset > 0) && (clu != CLUSTER_32(~0))) {
870 last_clu = clu;
871
872 if (exfat_fat_read(sb, clu, &clu) == -1) {
873 ret = -EIO;
874 goto out;
875 }
876 clu_offset--;
877 }
878 }
879
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;
885 new_clu.size = 0;
886 new_clu.flags = fid->flags;
887
888
889 num_alloced = exfat_alloc_cluster(sb,
890 num_alloc,
891 &new_clu);
892 if (num_alloced == 0)
893 break;
894 if (num_alloced < 0) {
895 ret = num_alloced;
896 goto out;
897 }
898
899
900 if (last_clu == CLUSTER_32(~0)) {
901 if (new_clu.flags == 0x01)
902 fid->flags = 0x01;
903 fid->start_clu = new_clu.dir;
904 modified = true;
905 } else {
906 if (new_clu.flags != fid->flags) {
907 exfat_chain_cont_cluster(sb,
908 fid->start_clu,
909 num_clusters);
910 fid->flags = 0x01;
911 modified = true;
912 }
913 if (new_clu.flags == 0x01)
914 exfat_fat_write(sb, last_clu, new_clu.dir);
915 }
916
917 num_clusters += num_alloced;
918 clu = new_clu.dir;
919 }
920
921
922 fid->hint_last_off = (s32)(fid->rwoffset >>
923 p_fs->cluster_size_bits);
924 fid->hint_last_clu = clu;
925
926
927 offset = (s32)(fid->rwoffset & (p_fs->cluster_size - 1));
928
929
930 sec_offset = offset >> p_bd->sector_size_bits;
931
932
933 offset &= p_bd->sector_size_mask;
934
935 LogSector = START_SECTOR(clu) + sec_offset;
936
937 oneblkwrite = (u64)(p_bd->sector_size - offset);
938 if (oneblkwrite > count)
939 oneblkwrite = count;
940
941 if ((offset == 0) && (oneblkwrite == p_bd->sector_size)) {
942 if (sector_read(sb, LogSector, &tmp_bh, 0) !=
943 0)
944 goto err_out;
945 memcpy((char *)tmp_bh->b_data,
946 (char *)buffer + write_bytes, (s32)oneblkwrite);
947 if (sector_write(sb, LogSector, tmp_bh, 0) !=
948 0) {
949 brelse(tmp_bh);
950 goto err_out;
951 }
952 } else {
953 if ((offset > 0) ||
954 ((fid->rwoffset + oneblkwrite) < fid->size)) {
955 if (sector_read(sb, LogSector, &tmp_bh, 1) !=
956 0)
957 goto err_out;
958 } else {
959 if (sector_read(sb, LogSector, &tmp_bh, 0) !=
960 0)
961 goto err_out;
962 }
963
964 memcpy((char *)tmp_bh->b_data + offset,
965 (char *)buffer + write_bytes, (s32)oneblkwrite);
966 if (sector_write(sb, LogSector, tmp_bh, 0) !=
967 0) {
968 brelse(tmp_bh);
969 goto err_out;
970 }
971 }
972
973 count -= oneblkwrite;
974 write_bytes += oneblkwrite;
975 fid->rwoffset += oneblkwrite;
976
977 fid->attr |= ATTR_ARCHIVE;
978
979 if (fid->size < fid->rwoffset) {
980 fid->size = fid->rwoffset;
981 modified = true;
982 }
983 }
984
985 brelse(tmp_bh);
986
987
988 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
989 ES_ALL_ENTRIES, &ep);
990 if (!es)
991 goto err_out;
992 ep2 = ep + 1;
993
994 exfat_set_entry_time(ep, tm_current(&tm), TM_MODIFY);
995 exfat_set_entry_attr(ep, fid->attr);
996
997 if (modified) {
998 if (exfat_get_entry_flag(ep2) != fid->flags)
999 exfat_set_entry_flag(ep2, fid->flags);
1000
1001 if (exfat_get_entry_size(ep2) != fid->size)
1002 exfat_set_entry_size(ep2, fid->size);
1003
1004 if (exfat_get_entry_clu0(ep2) != fid->start_clu)
1005 exfat_set_entry_clu0(ep2, fid->start_clu);
1006 }
1007
1008 update_dir_checksum_with_entry_set(sb, es);
1009 release_entry_set(es);
1010
1011#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
1012 fs_sync(sb, true);
1013 fs_set_vol_flags(sb, VOL_CLEAN);
1014#endif
1015
1016err_out:
1017
1018 if (wcount)
1019 *wcount = write_bytes;
1020
1021 if (num_alloced == 0)
1022 ret = -ENOSPC;
1023
1024 else if (p_fs->dev_ejected)
1025 ret = -EIO;
1026
1027out:
1028
1029 mutex_unlock(&p_fs->v_mutex);
1030
1031 return ret;
1032}
1033
1034static int ffsTruncateFile(struct inode *inode, u64 old_size, u64 new_size)
1035{
1036 s32 num_clusters;
1037 u32 last_clu = CLUSTER_32(0);
1038 int ret = 0;
1039 struct chain_t clu;
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;
1046
1047 pr_debug("%s entered (inode %p size %llu)\n", __func__, inode,
1048 new_size);
1049
1050
1051 mutex_lock(&p_fs->v_mutex);
1052
1053
1054 if (fid->type != TYPE_FILE) {
1055 ret = -EPERM;
1056 goto out;
1057 }
1058
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);
1062 }
1063
1064 if (old_size <= new_size) {
1065 ret = 0;
1066 goto out;
1067 }
1068
1069 fs_set_vol_flags(sb, VOL_DIRTY);
1070
1071 clu.dir = fid->start_clu;
1072 clu.size = (s32)((old_size - 1) >> p_fs->cluster_size_bits) + 1;
1073 clu.flags = fid->flags;
1074
1075 if (new_size > 0) {
1076 num_clusters = (s32)((new_size - 1) >>
1077 p_fs->cluster_size_bits) + 1;
1078
1079 if (clu.flags == 0x03) {
1080 clu.dir += num_clusters;
1081 } else {
1082 while (num_clusters > 0) {
1083 last_clu = clu.dir;
1084 if (exfat_fat_read(sb, clu.dir, &clu.dir) == -1) {
1085 ret = -EIO;
1086 goto out;
1087 }
1088 num_clusters--;
1089 }
1090 }
1091
1092 clu.size -= num_clusters;
1093 }
1094
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);
1100 }
1101
1102
1103 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1104 ES_ALL_ENTRIES, &ep);
1105 if (!es) {
1106 ret = -ENOENT;
1107 goto out;
1108 }
1109 ep2 = ep + 1;
1110
1111 exfat_set_entry_time(ep, tm_current(&tm), TM_MODIFY);
1112 exfat_set_entry_attr(ep, fid->attr);
1113
1114 exfat_set_entry_size(ep2, new_size);
1115 if (new_size == 0) {
1116 exfat_set_entry_flag(ep2, 0x01);
1117 exfat_set_entry_clu0(ep2, CLUSTER_32(0));
1118 }
1119
1120 update_dir_checksum_with_entry_set(sb, es);
1121 release_entry_set(es);
1122
1123
1124 if (last_clu != CLUSTER_32(0)) {
1125 if (fid->flags == 0x01)
1126 exfat_fat_write(sb, last_clu, CLUSTER_32(~0));
1127 }
1128
1129
1130 exfat_free_cluster(sb, &clu, 0);
1131
1132
1133 fid->hint_last_off = -1;
1134 if (fid->rwoffset > fid->size)
1135 fid->rwoffset = fid->size;
1136
1137#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
1138 fs_sync(sb, true);
1139 fs_set_vol_flags(sb, VOL_CLEAN);
1140#endif
1141
1142 if (p_fs->dev_ejected)
1143 ret = -EIO;
1144
1145out:
1146 pr_debug("%s exited (%d)\n", __func__, ret);
1147
1148 mutex_unlock(&p_fs->v_mutex);
1149
1150 return ret;
1151}
1152
1153static void update_parent_info(struct file_id_t *fid,
1154 struct inode *parent_inode)
1155{
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);
1158
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);
1167 }
1168}
1169
1170static int ffsMoveFile(struct inode *old_parent_inode, struct file_id_t *fid,
1171 struct inode *new_parent_inode, struct dentry *new_dentry)
1172{
1173 s32 ret;
1174 s32 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;
1183 int num_entries;
1184 struct file_id_t *new_fid = NULL;
1185 s32 new_entry = 0;
1186
1187
1188 if (!fid)
1189 return -EINVAL;
1190
1191
1192 if (!new_path || (*new_path == '\0'))
1193 return -EINVAL;
1194
1195
1196 mutex_lock(&p_fs->v_mutex);
1197
1198 update_parent_info(fid, old_parent_inode);
1199
1200 olddir.dir = fid->dir.dir;
1201 olddir.size = fid->dir.size;
1202 olddir.flags = fid->dir.flags;
1203
1204 dentry = fid->entry;
1205
1206
1207 if (p_fs->vol_type != EXFAT) {
1208 if ((olddir.dir != p_fs->root_dir) && (dentry < 2)) {
1209 ret = -EPERM;
1210 goto out2;
1211 }
1212 }
1213
1214 ep = get_entry_in_dir(sb, &olddir, dentry, NULL);
1215 if (!ep) {
1216 ret = -ENOENT;
1217 goto out2;
1218 }
1219
1220 if (exfat_get_entry_attr(ep) & ATTR_READONLY) {
1221 ret = -EPERM;
1222 goto out2;
1223 }
1224
1225
1226 if (new_inode) {
1227 u32 entry_type;
1228
1229 ret = -ENOENT;
1230 new_fid = &EXFAT_I(new_inode)->fid;
1231
1232 update_parent_info(new_fid, new_parent_inode);
1233
1234 p_dir = &new_fid->dir;
1235 new_entry = new_fid->entry;
1236 ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
1237 if (!ep)
1238 goto out;
1239
1240 entry_type = exfat_get_entry_type(ep);
1241
1242 if (entry_type == TYPE_DIR) {
1243 struct chain_t new_clu;
1244
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;
1249
1250 if (!is_dir_empty(sb, &new_clu)) {
1251 ret = -EEXIST;
1252 goto out;
1253 }
1254 }
1255 }
1256
1257
1258 ret = resolve_path(new_parent_inode, new_path, &newdir, &uni_name);
1259 if (ret)
1260 goto out2;
1261
1262 fs_set_vol_flags(sb, VOL_DIRTY);
1263
1264 if (olddir.dir == newdir.dir)
1265 ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
1266 &uni_name, fid);
1267 else
1268 ret = move_file(new_parent_inode, &olddir, dentry, &newdir,
1269 &uni_name, fid);
1270
1271 if ((ret == 0) && new_inode) {
1272
1273 ep = get_entry_in_dir(sb, p_dir, new_entry, NULL);
1274 if (!ep)
1275 goto out;
1276
1277 num_entries = exfat_count_ext_entries(sb, p_dir,
1278 new_entry, ep);
1279 if (num_entries < 0)
1280 goto out;
1281 exfat_delete_dir_entry(sb, p_dir, new_entry, 0,
1282 num_entries + 1);
1283 }
1284out:
1285#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
1286 fs_sync(sb, true);
1287 fs_set_vol_flags(sb, VOL_CLEAN);
1288#endif
1289
1290 if (p_fs->dev_ejected)
1291 ret = -EIO;
1292out2:
1293
1294 mutex_unlock(&p_fs->v_mutex);
1295
1296 return ret;
1297}
1298
1299static int ffsRemoveFile(struct inode *inode, struct file_id_t *fid)
1300{
1301 s32 dentry;
1302 int ret = 0;
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);
1307
1308
1309 if (!fid)
1310 return -EINVAL;
1311
1312
1313 mutex_lock(&p_fs->v_mutex);
1314
1315 dir.dir = fid->dir.dir;
1316 dir.size = fid->dir.size;
1317 dir.flags = fid->dir.flags;
1318
1319 dentry = fid->entry;
1320
1321 ep = get_entry_in_dir(sb, &dir, dentry, NULL);
1322 if (!ep) {
1323 ret = -ENOENT;
1324 goto out;
1325 }
1326
1327 if (exfat_get_entry_attr(ep) & ATTR_READONLY) {
1328 ret = -EPERM;
1329 goto out;
1330 }
1331 fs_set_vol_flags(sb, VOL_DIRTY);
1332
1333
1334 remove_file(inode, &dir, dentry);
1335
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;
1339
1340
1341 exfat_free_cluster(sb, &clu_to_free, 0);
1342
1343 fid->size = 0;
1344 fid->start_clu = CLUSTER_32(~0);
1345 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
1346
1347#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
1348 fs_sync(sb, true);
1349 fs_set_vol_flags(sb, VOL_CLEAN);
1350#endif
1351
1352 if (p_fs->dev_ejected)
1353 ret = -EIO;
1354out:
1355
1356 mutex_unlock(&p_fs->v_mutex);
1357
1358 return ret;
1359}
1360
1361#if 0
1362
1363static int ffsSetAttr(struct inode *inode, u32 attr)
1364{
1365 u32 type;
1366 int ret = 0;
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;
1374
1375 if (fid->attr == attr) {
1376 if (p_fs->dev_ejected)
1377 return -EIO;
1378 return 0;
1379 }
1380
1381 if (is_dir) {
1382 if ((fid->dir.dir == p_fs->root_dir) &&
1383 (fid->entry == -1)) {
1384 if (p_fs->dev_ejected)
1385 return -EIO;
1386 return 0;
1387 }
1388 }
1389
1390
1391 mutex_lock(&p_fs->v_mutex);
1392
1393
1394 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1395 ES_ALL_ENTRIES, &ep);
1396 if (!es) {
1397 ret = -ENOENT;
1398 goto out;
1399 }
1400
1401 type = exfat_get_entry_type(ep);
1402
1403 if (((type == TYPE_FILE) && (attr & ATTR_SUBDIR)) ||
1404 ((type == TYPE_DIR) && (!(attr & ATTR_SUBDIR)))) {
1405 if (p_fs->dev_ejected)
1406 ret = -EIO;
1407 else
1408 ret = -EINVAL;
1409
1410 release_entry_set(es);
1411 goto out;
1412 }
1413
1414 fs_set_vol_flags(sb, VOL_DIRTY);
1415
1416
1417 fid->attr = attr;
1418 exfat_set_entry_attr(ep, attr);
1419
1420 update_dir_checksum_with_entry_set(sb, es);
1421 release_entry_set(es);
1422
1423#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
1424 fs_sync(sb, true);
1425 fs_set_vol_flags(sb, VOL_CLEAN);
1426#endif
1427
1428 if (p_fs->dev_ejected)
1429 ret = -EIO;
1430out:
1431
1432 mutex_unlock(&p_fs->v_mutex);
1433
1434 return ret;
1435}
1436#endif
1437
1438static int ffsReadStat(struct inode *inode, struct dir_entry_t *info)
1439{
1440 s32 count;
1441 int ret = 0;
1442 struct chain_t dir;
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;
1451
1452 pr_debug("%s entered\n", __func__);
1453
1454
1455 mutex_lock(&p_fs->v_mutex);
1456
1457 if (is_dir) {
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, ".");
1469
1470 dir.dir = p_fs->root_dir;
1471 dir.flags = 0x01;
1472
1473 if (p_fs->root_dir == CLUSTER_32(0)) {
1474
1475 info->Size = p_fs->dentries_in_root <<
1476 DENTRY_SIZE_BITS;
1477 } else {
1478 info->Size = count_num_clusters(sb, &dir) <<
1479 p_fs->cluster_size_bits;
1480 }
1481
1482 count = count_dos_name_entries(sb, &dir, TYPE_DIR);
1483 if (count < 0) {
1484 ret = count;
1485 goto out;
1486 }
1487 info->NumSubdirs = count;
1488
1489 if (p_fs->dev_ejected)
1490 ret = -EIO;
1491 goto out;
1492 }
1493 }
1494
1495
1496 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1497 ES_2_ENTRIES, &ep);
1498 if (!es) {
1499 ret = -ENOENT;
1500 goto out;
1501 }
1502 ep2 = ep + 1;
1503
1504
1505 info->Attr = exfat_get_entry_attr(ep);
1506
1507 exfat_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;
1515
1516 exfat_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;
1524
1525 memset((char *)&info->AccessTimestamp, 0, sizeof(struct date_time_t));
1526
1527 *uni_name.name = 0x0;
1528
1529
1530
1531 exfat_get_uni_name_from_ext_entry(sb, &fid->dir, fid->entry,
1532 uni_name.name);
1533 nls_uniname_to_cstring(sb, info->Name, &uni_name);
1534
1535 info->NumSubdirs = 2;
1536
1537 info->Size = exfat_get_entry_size(ep2);
1538
1539 release_entry_set(es);
1540
1541 if (is_dir) {
1542 dir.dir = fid->start_clu;
1543 dir.flags = 0x01;
1544
1545 if (info->Size == 0)
1546 info->Size = (u64)count_num_clusters(sb, &dir) <<
1547 p_fs->cluster_size_bits;
1548
1549 count = count_dos_name_entries(sb, &dir, TYPE_DIR);
1550 if (count < 0) {
1551 ret = count;
1552 goto out;
1553 }
1554 info->NumSubdirs += count;
1555 }
1556
1557 if (p_fs->dev_ejected)
1558 ret = -EIO;
1559
1560out:
1561
1562 mutex_unlock(&p_fs->v_mutex);
1563
1564 pr_debug("%s exited successfully\n", __func__);
1565 return ret;
1566}
1567
1568static int ffsWriteStat(struct inode *inode, struct dir_entry_t *info)
1569{
1570 int ret = 0;
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;
1578
1579 pr_debug("%s entered (inode %p info %p\n", __func__, inode, info);
1580
1581
1582 mutex_lock(&p_fs->v_mutex);
1583
1584 if (is_dir) {
1585 if ((fid->dir.dir == p_fs->root_dir) &&
1586 (fid->entry == -1)) {
1587 if (p_fs->dev_ejected)
1588 ret = -EIO;
1589 ret = 0;
1590 goto out;
1591 }
1592 }
1593
1594 fs_set_vol_flags(sb, VOL_DIRTY);
1595
1596
1597 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1598 ES_ALL_ENTRIES, &ep);
1599 if (!es) {
1600 ret = -ENOENT;
1601 goto out;
1602 }
1603 ep2 = ep + 1;
1604
1605 exfat_set_entry_attr(ep, info->Attr);
1606
1607
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 exfat_set_entry_time(ep, &tm, TM_CREATE);
1615
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 exfat_set_entry_time(ep, &tm, TM_MODIFY);
1623
1624 exfat_set_entry_size(ep2, info->Size);
1625
1626 update_dir_checksum_with_entry_set(sb, es);
1627 release_entry_set(es);
1628
1629 if (p_fs->dev_ejected)
1630 ret = -EIO;
1631
1632out:
1633
1634 mutex_unlock(&p_fs->v_mutex);
1635
1636 pr_debug("%s exited (%d)\n", __func__, ret);
1637
1638 return ret;
1639}
1640
1641static int ffsMapCluster(struct inode *inode, s32 clu_offset, u32 *clu)
1642{
1643 s32 num_clusters, num_alloced;
1644 bool modified = false;
1645 u32 last_clu;
1646 int ret = 0;
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);
1653
1654
1655 if (!clu)
1656 return -EINVAL;
1657
1658
1659 mutex_lock(&p_fs->v_mutex);
1660
1661 fid->rwoffset = (s64)(clu_offset) << p_fs->cluster_size_bits;
1662
1663 if (EXFAT_I(inode)->mmu_private == 0)
1664 num_clusters = 0;
1665 else
1666 num_clusters = (s32)((EXFAT_I(inode)->mmu_private - 1) >>
1667 p_fs->cluster_size_bits) + 1;
1668
1669 *clu = last_clu = fid->start_clu;
1670
1671 if (fid->flags == 0x03) {
1672 if ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
1673 last_clu += clu_offset - 1;
1674
1675 if (clu_offset == num_clusters)
1676 *clu = CLUSTER_32(~0);
1677 else
1678 *clu += clu_offset;
1679 }
1680 } else {
1681
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;
1686 }
1687
1688 while ((clu_offset > 0) && (*clu != CLUSTER_32(~0))) {
1689 last_clu = *clu;
1690 if (exfat_fat_read(sb, *clu, clu) == -1) {
1691 ret = -EIO;
1692 goto out;
1693 }
1694 clu_offset--;
1695 }
1696 }
1697
1698 if (*clu == CLUSTER_32(~0)) {
1699 fs_set_vol_flags(sb, VOL_DIRTY);
1700
1701 new_clu.dir = (last_clu == CLUSTER_32(~0)) ? CLUSTER_32(~0) :
1702 last_clu + 1;
1703 new_clu.size = 0;
1704 new_clu.flags = fid->flags;
1705
1706
1707 num_alloced = exfat_alloc_cluster(sb, 1, &new_clu);
1708 if (num_alloced < 0) {
1709 ret = -EIO;
1710 goto out;
1711 } else if (num_alloced == 0) {
1712 ret = -ENOSPC;
1713 goto out;
1714 }
1715
1716
1717 if (last_clu == CLUSTER_32(~0)) {
1718 if (new_clu.flags == 0x01)
1719 fid->flags = 0x01;
1720 fid->start_clu = new_clu.dir;
1721 modified = true;
1722 } else {
1723 if (new_clu.flags != fid->flags) {
1724 exfat_chain_cont_cluster(sb, fid->start_clu,
1725 num_clusters);
1726 fid->flags = 0x01;
1727 modified = true;
1728 }
1729 if (new_clu.flags == 0x01)
1730 exfat_fat_write(sb, last_clu, new_clu.dir);
1731 }
1732
1733 num_clusters += num_alloced;
1734 *clu = new_clu.dir;
1735
1736 es = get_entry_set_in_dir(sb, &fid->dir, fid->entry,
1737 ES_ALL_ENTRIES, &ep);
1738 if (!es) {
1739 ret = -ENOENT;
1740 goto out;
1741 }
1742
1743 ep++;
1744
1745
1746 if (modified) {
1747 if (exfat_get_entry_flag(ep) != fid->flags)
1748 exfat_set_entry_flag(ep, fid->flags);
1749
1750 if (exfat_get_entry_clu0(ep) != fid->start_clu)
1751 exfat_set_entry_clu0(ep, fid->start_clu);
1752 }
1753
1754 update_dir_checksum_with_entry_set(sb, es);
1755 release_entry_set(es);
1756
1757
1758 inode->i_blocks += num_alloced << (p_fs->cluster_size_bits - 9);
1759 }
1760
1761
1762 fid->hint_last_off = (s32)(fid->rwoffset >> p_fs->cluster_size_bits);
1763 fid->hint_last_clu = *clu;
1764
1765 if (p_fs->dev_ejected)
1766 ret = -EIO;
1767
1768out:
1769
1770 mutex_unlock(&p_fs->v_mutex);
1771
1772 return ret;
1773}
1774
1775
1776
1777
1778
1779static int ffsCreateDir(struct inode *inode, char *path, struct file_id_t *fid)
1780{
1781 int ret = 0;
1782 struct chain_t dir;
1783 struct uni_name_t uni_name;
1784 struct super_block *sb = inode->i_sb;
1785 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1786
1787 pr_debug("%s entered\n", __func__);
1788
1789
1790 if (!fid || !path || (*path == '\0'))
1791 return -EINVAL;
1792
1793
1794 mutex_lock(&p_fs->v_mutex);
1795
1796
1797 ret = resolve_path(inode, path, &dir, &uni_name);
1798 if (ret)
1799 goto out;
1800
1801 fs_set_vol_flags(sb, VOL_DIRTY);
1802
1803 ret = create_dir(inode, &dir, &uni_name, fid);
1804
1805#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
1806 fs_sync(sb, true);
1807 fs_set_vol_flags(sb, VOL_CLEAN);
1808#endif
1809
1810 if (p_fs->dev_ejected)
1811 ret = -EIO;
1812out:
1813
1814 mutex_unlock(&p_fs->v_mutex);
1815
1816 return ret;
1817}
1818
1819static int ffsReadDir(struct inode *inode, struct dir_entry_t *dir_entry)
1820{
1821 int i, dentry, clu_offset;
1822 int ret = 0;
1823 s32 dentries_per_clu, dentries_per_clu_bits = 0;
1824 u32 type;
1825 sector_t sector;
1826 struct chain_t dir, clu;
1827 struct uni_name_t uni_name;
1828 struct timestamp_t tm;
1829 struct dentry_t *ep;
1830 struct super_block *sb = inode->i_sb;
1831 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1832 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1833
1834
1835 if (!dir_entry)
1836 return -EINVAL;
1837
1838
1839 if (fid->type != TYPE_DIR)
1840 return -ENOTDIR;
1841
1842
1843 mutex_lock(&p_fs->v_mutex);
1844
1845 if (fid->entry == -1) {
1846 dir.dir = p_fs->root_dir;
1847 dir.flags = 0x01;
1848 } else {
1849 dir.dir = fid->start_clu;
1850 dir.size = (s32)(fid->size >> p_fs->cluster_size_bits);
1851 dir.flags = fid->flags;
1852 }
1853
1854 dentry = (s32)fid->rwoffset;
1855
1856 if (dir.dir == CLUSTER_32(0)) {
1857
1858 dentries_per_clu = p_fs->dentries_in_root;
1859
1860 if (dentry == dentries_per_clu) {
1861 clu.dir = CLUSTER_32(~0);
1862 } else {
1863 clu.dir = dir.dir;
1864 clu.size = dir.size;
1865 clu.flags = dir.flags;
1866 }
1867 } else {
1868 dentries_per_clu = p_fs->dentries_per_clu;
1869 dentries_per_clu_bits = ilog2(dentries_per_clu);
1870
1871 clu_offset = dentry >> dentries_per_clu_bits;
1872 clu.dir = dir.dir;
1873 clu.size = dir.size;
1874 clu.flags = dir.flags;
1875
1876 if (clu.flags == 0x03) {
1877 clu.dir += clu_offset;
1878 clu.size -= clu_offset;
1879 } else {
1880
1881 if ((clu_offset > 0) && (fid->hint_last_off > 0) &&
1882 (clu_offset >= fid->hint_last_off)) {
1883 clu_offset -= fid->hint_last_off;
1884 clu.dir = fid->hint_last_clu;
1885 }
1886
1887 while (clu_offset > 0) {
1888
1889 if (exfat_fat_read(sb, clu.dir, &clu.dir) == -1) {
1890 ret = -EIO;
1891 goto out;
1892 }
1893 clu_offset--;
1894 }
1895 }
1896 }
1897
1898 while (clu.dir != CLUSTER_32(~0)) {
1899 if (p_fs->dev_ejected)
1900 break;
1901
1902 if (dir.dir == CLUSTER_32(0))
1903 i = dentry % dentries_per_clu;
1904 else
1905 i = dentry & (dentries_per_clu - 1);
1906
1907 for ( ; i < dentries_per_clu; i++, dentry++) {
1908 ep = get_entry_in_dir(sb, &clu, i, §or);
1909 if (!ep) {
1910 ret = -ENOENT;
1911 goto out;
1912 }
1913 type = exfat_get_entry_type(ep);
1914
1915 if (type == TYPE_UNUSED)
1916 break;
1917
1918 if ((type != TYPE_FILE) && (type != TYPE_DIR))
1919 continue;
1920
1921 exfat_buf_lock(sb, sector);
1922 dir_entry->Attr = exfat_get_entry_attr(ep);
1923
1924 exfat_get_entry_time(ep, &tm, TM_CREATE);
1925 dir_entry->CreateTimestamp.Year = tm.year;
1926 dir_entry->CreateTimestamp.Month = tm.mon;
1927 dir_entry->CreateTimestamp.Day = tm.day;
1928 dir_entry->CreateTimestamp.Hour = tm.hour;
1929 dir_entry->CreateTimestamp.Minute = tm.min;
1930 dir_entry->CreateTimestamp.Second = tm.sec;
1931 dir_entry->CreateTimestamp.MilliSecond = 0;
1932
1933 exfat_get_entry_time(ep, &tm, TM_MODIFY);
1934 dir_entry->ModifyTimestamp.Year = tm.year;
1935 dir_entry->ModifyTimestamp.Month = tm.mon;
1936 dir_entry->ModifyTimestamp.Day = tm.day;
1937 dir_entry->ModifyTimestamp.Hour = tm.hour;
1938 dir_entry->ModifyTimestamp.Minute = tm.min;
1939 dir_entry->ModifyTimestamp.Second = tm.sec;
1940 dir_entry->ModifyTimestamp.MilliSecond = 0;
1941
1942 memset((char *)&dir_entry->AccessTimestamp, 0,
1943 sizeof(struct date_time_t));
1944
1945 *uni_name.name = 0x0;
1946 exfat_get_uni_name_from_ext_entry(sb, &dir, dentry,
1947 uni_name.name);
1948 nls_uniname_to_cstring(sb, dir_entry->Name, &uni_name);
1949 exfat_buf_unlock(sb, sector);
1950
1951 ep = get_entry_in_dir(sb, &clu, i + 1, NULL);
1952 if (!ep) {
1953 ret = -ENOENT;
1954 goto out;
1955 }
1956
1957 dir_entry->Size = exfat_get_entry_size(ep);
1958
1959
1960 if (dir.dir == CLUSTER_32(0)) {
1961 } else {
1962 fid->hint_last_off = dentry >>
1963 dentries_per_clu_bits;
1964 fid->hint_last_clu = clu.dir;
1965 }
1966
1967 fid->rwoffset = (s64)(++dentry);
1968
1969 if (p_fs->dev_ejected)
1970 ret = -EIO;
1971 goto out;
1972 }
1973
1974 if (dir.dir == CLUSTER_32(0))
1975 break;
1976
1977 if (clu.flags == 0x03) {
1978 if ((--clu.size) > 0)
1979 clu.dir++;
1980 else
1981 clu.dir = CLUSTER_32(~0);
1982 } else {
1983
1984 if (exfat_fat_read(sb, clu.dir, &clu.dir) == -1) {
1985 ret = -EIO;
1986 goto out;
1987 }
1988 }
1989 }
1990
1991 *dir_entry->Name = '\0';
1992
1993 fid->rwoffset = (s64)(++dentry);
1994
1995 if (p_fs->dev_ejected)
1996 ret = -EIO;
1997
1998out:
1999
2000 mutex_unlock(&p_fs->v_mutex);
2001
2002 return ret;
2003}
2004
2005static int ffsRemoveDir(struct inode *inode, struct file_id_t *fid)
2006{
2007 s32 dentry;
2008 int ret = 0;
2009 struct chain_t dir, clu_to_free;
2010 struct super_block *sb = inode->i_sb;
2011 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2012
2013
2014 if (!fid)
2015 return -EINVAL;
2016
2017 dir.dir = fid->dir.dir;
2018 dir.size = fid->dir.size;
2019 dir.flags = fid->dir.flags;
2020
2021 dentry = fid->entry;
2022
2023
2024 if (p_fs->vol_type != EXFAT) {
2025 if ((dir.dir != p_fs->root_dir) && (dentry < 2))
2026 return -EPERM;
2027 }
2028
2029
2030 mutex_lock(&p_fs->v_mutex);
2031
2032 clu_to_free.dir = fid->start_clu;
2033 clu_to_free.size = (s32)((fid->size - 1) >> p_fs->cluster_size_bits) + 1;
2034 clu_to_free.flags = fid->flags;
2035
2036 if (!is_dir_empty(sb, &clu_to_free)) {
2037 ret = -ENOTEMPTY;
2038 goto out;
2039 }
2040
2041 fs_set_vol_flags(sb, VOL_DIRTY);
2042
2043
2044 remove_file(inode, &dir, dentry);
2045
2046
2047 exfat_free_cluster(sb, &clu_to_free, 1);
2048
2049 fid->size = 0;
2050 fid->start_clu = CLUSTER_32(~0);
2051 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2052
2053#ifndef CONFIG_STAGING_EXFAT_DELAYED_SYNC
2054 fs_sync(sb, true);
2055 fs_set_vol_flags(sb, VOL_CLEAN);
2056#endif
2057
2058 if (p_fs->dev_ejected)
2059 ret = -EIO;
2060
2061out:
2062
2063 mutex_unlock(&p_fs->v_mutex);
2064
2065 return ret;
2066}
2067
2068
2069
2070
2071
2072static int exfat_readdir(struct file *filp, struct dir_context *ctx)
2073{
2074 struct inode *inode = file_inode(filp);
2075 struct super_block *sb = inode->i_sb;
2076 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2077 struct fs_info_t *p_fs = &sbi->fs_info;
2078 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
2079 struct dir_entry_t de;
2080 unsigned long inum;
2081 loff_t cpos;
2082 int err = 0;
2083
2084 __lock_super(sb);
2085
2086 cpos = ctx->pos;
2087
2088 if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
2089 while (cpos < 2) {
2090 if (inode->i_ino == EXFAT_ROOT_INO)
2091 inum = EXFAT_ROOT_INO;
2092 else if (cpos == 0)
2093 inum = inode->i_ino;
2094 else
2095 inum = parent_ino(filp->f_path.dentry);
2096
2097 if (!dir_emit_dots(filp, ctx))
2098 goto out;
2099 cpos++;
2100 ctx->pos++;
2101 }
2102 if (cpos == 2)
2103 cpos = 0;
2104 }
2105 if (cpos & (DENTRY_SIZE - 1)) {
2106 err = -ENOENT;
2107 goto out;
2108 }
2109
2110get_new:
2111 EXFAT_I(inode)->fid.size = i_size_read(inode);
2112 EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
2113
2114 err = ffsReadDir(inode, &de);
2115 if (err) {
2116
2117
2118
2119 if (err == -EIO) {
2120 cpos += 1 << p_bd->sector_size_bits;
2121 cpos &= ~((1 << p_bd->sector_size_bits) - 1);
2122 }
2123
2124 goto end_of_dir;
2125 }
2126
2127 cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
2128
2129 if (!de.Name[0])
2130 goto end_of_dir;
2131
2132 if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
2133 inum = inode->i_ino;
2134 } else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
2135 inum = parent_ino(filp->f_path.dentry);
2136 } else {
2137 loff_t i_pos = ((loff_t)EXFAT_I(inode)->fid.start_clu << 32) |
2138 ((EXFAT_I(inode)->fid.rwoffset - 1) & 0xffffffff);
2139 struct inode *tmp = exfat_iget(sb, i_pos);
2140
2141 if (tmp) {
2142 inum = tmp->i_ino;
2143 iput(tmp);
2144 } else {
2145 inum = iunique(sb, EXFAT_ROOT_INO);
2146 }
2147 }
2148
2149 if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
2150 (de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
2151 goto out;
2152
2153 ctx->pos = cpos;
2154 goto get_new;
2155
2156end_of_dir:
2157 ctx->pos = cpos;
2158out:
2159 __unlock_super(sb);
2160 return err;
2161}
2162
2163static int exfat_ioctl_volume_id(struct inode *dir)
2164{
2165 struct super_block *sb = dir->i_sb;
2166 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2167 struct fs_info_t *p_fs = &sbi->fs_info;
2168
2169 return p_fs->vol_id;
2170}
2171
2172static long exfat_generic_ioctl(struct file *filp, unsigned int cmd,
2173 unsigned long arg)
2174{
2175 struct inode *inode = filp->f_path.dentry->d_inode;
2176#ifdef CONFIG_STAGING_EXFAT_KERNEL_DEBUG
2177 unsigned int flags;
2178#endif
2179
2180 switch (cmd) {
2181 case EXFAT_IOCTL_GET_VOLUME_ID:
2182 return exfat_ioctl_volume_id(inode);
2183#ifdef CONFIG_STAGING_EXFAT_KERNEL_DEBUG
2184 case EXFAT_IOC_GET_DEBUGFLAGS: {
2185 struct super_block *sb = inode->i_sb;
2186 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2187
2188 flags = sbi->debug_flags;
2189 return put_user(flags, (int __user *)arg);
2190 }
2191 case EXFAT_IOC_SET_DEBUGFLAGS: {
2192 struct super_block *sb = inode->i_sb;
2193 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2194
2195 if (!capable(CAP_SYS_ADMIN))
2196 return -EPERM;
2197
2198 if (get_user(flags, (int __user *)arg))
2199 return -EFAULT;
2200
2201 __lock_super(sb);
2202 sbi->debug_flags = flags;
2203 __unlock_super(sb);
2204
2205 return 0;
2206 }
2207#endif
2208 default:
2209 return -ENOTTY;
2210 }
2211}
2212
2213static const struct file_operations exfat_dir_operations = {
2214 .llseek = generic_file_llseek,
2215 .read = generic_read_dir,
2216 .iterate = exfat_readdir,
2217 .unlocked_ioctl = exfat_generic_ioctl,
2218 .fsync = generic_file_fsync,
2219};
2220
2221static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2222 bool excl)
2223{
2224 struct super_block *sb = dir->i_sb;
2225 struct timespec64 curtime;
2226 struct inode *inode;
2227 struct file_id_t fid;
2228 loff_t i_pos;
2229 int err;
2230
2231 __lock_super(sb);
2232
2233 pr_debug("%s entered\n", __func__);
2234
2235 err = ffsCreateFile(dir, (u8 *)dentry->d_name.name, FM_REGULAR, &fid);
2236 if (err)
2237 goto out;
2238
2239 INC_IVERSION(dir);
2240 curtime = current_time(dir);
2241 dir->i_ctime = curtime;
2242 dir->i_mtime = curtime;
2243 dir->i_atime = curtime;
2244 if (IS_DIRSYNC(dir))
2245 (void)exfat_sync_inode(dir);
2246 else
2247 mark_inode_dirty(dir);
2248
2249 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2250
2251 inode = exfat_build_inode(sb, &fid, i_pos);
2252 if (IS_ERR(inode)) {
2253 err = PTR_ERR(inode);
2254 goto out;
2255 }
2256 INC_IVERSION(inode);
2257 curtime = current_time(inode);
2258 inode->i_mtime = curtime;
2259 inode->i_atime = curtime;
2260 inode->i_ctime = curtime;
2261
2262
2263
2264
2265 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2266 d_instantiate(dentry, inode);
2267
2268out:
2269 __unlock_super(sb);
2270 pr_debug("%s exited\n", __func__);
2271 return err;
2272}
2273
2274static int exfat_find(struct inode *dir, struct qstr *qname,
2275 struct file_id_t *fid)
2276{
2277 int err;
2278
2279 if (qname->len == 0)
2280 return -ENOENT;
2281
2282 err = ffsLookupFile(dir, (u8 *)qname->name, fid);
2283 if (err)
2284 return -ENOENT;
2285
2286 return 0;
2287}
2288
2289static int exfat_d_anon_disconn(struct dentry *dentry)
2290{
2291 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
2292}
2293
2294static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
2295 unsigned int flags)
2296{
2297 struct super_block *sb = dir->i_sb;
2298 struct inode *inode;
2299 struct dentry *alias;
2300 int err;
2301 struct file_id_t fid;
2302 loff_t i_pos;
2303 u64 ret;
2304 mode_t i_mode;
2305
2306 __lock_super(sb);
2307 pr_debug("%s entered\n", __func__);
2308 err = exfat_find(dir, &dentry->d_name, &fid);
2309 if (err) {
2310 if (err == -ENOENT) {
2311 inode = NULL;
2312 goto out;
2313 }
2314 goto error;
2315 }
2316
2317 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2318 inode = exfat_build_inode(sb, &fid, i_pos);
2319 if (IS_ERR(inode)) {
2320 err = PTR_ERR(inode);
2321 goto error;
2322 }
2323
2324 i_mode = inode->i_mode;
2325 if (S_ISLNK(i_mode) && !EXFAT_I(inode)->target) {
2326 EXFAT_I(inode)->target = kmalloc(i_size_read(inode) + 1,
2327 GFP_KERNEL);
2328 if (!EXFAT_I(inode)->target) {
2329 err = -ENOMEM;
2330 goto error;
2331 }
2332 ffsReadFile(dir, &fid, EXFAT_I(inode)->target,
2333 i_size_read(inode), &ret);
2334 *(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
2335 }
2336
2337 alias = d_find_alias(inode);
2338 if (alias && !exfat_d_anon_disconn(alias)) {
2339 BUG_ON(d_unhashed(alias));
2340 if (!S_ISDIR(i_mode))
2341 d_move(alias, dentry);
2342 iput(inode);
2343 __unlock_super(sb);
2344 pr_debug("%s exited 1\n", __func__);
2345 return alias;
2346 }
2347 dput(alias);
2348out:
2349 __unlock_super(sb);
2350 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2351 dentry = d_splice_alias(inode, dentry);
2352 if (dentry)
2353 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2354 pr_debug("%s exited 2\n", __func__);
2355 return dentry;
2356
2357error:
2358 __unlock_super(sb);
2359 pr_debug("%s exited 3\n", __func__);
2360 return ERR_PTR(err);
2361}
2362
2363static inline unsigned long exfat_hash(loff_t i_pos)
2364{
2365 return hash_32(i_pos, EXFAT_HASH_BITS);
2366}
2367
2368static void exfat_attach(struct inode *inode, loff_t i_pos)
2369{
2370 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
2371 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
2372
2373 spin_lock(&sbi->inode_hash_lock);
2374 EXFAT_I(inode)->i_pos = i_pos;
2375 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
2376 spin_unlock(&sbi->inode_hash_lock);
2377}
2378
2379static void exfat_detach(struct inode *inode)
2380{
2381 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
2382
2383 spin_lock(&sbi->inode_hash_lock);
2384 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
2385 EXFAT_I(inode)->i_pos = 0;
2386 spin_unlock(&sbi->inode_hash_lock);
2387}
2388
2389static int exfat_unlink(struct inode *dir, struct dentry *dentry)
2390{
2391 struct inode *inode = dentry->d_inode;
2392 struct super_block *sb = dir->i_sb;
2393 struct timespec64 curtime;
2394 int err;
2395
2396 __lock_super(sb);
2397
2398 pr_debug("%s entered\n", __func__);
2399
2400 EXFAT_I(inode)->fid.size = i_size_read(inode);
2401
2402 err = ffsRemoveFile(dir, &(EXFAT_I(inode)->fid));
2403 if (err)
2404 goto out;
2405
2406 INC_IVERSION(dir);
2407 curtime = current_time(dir);
2408 dir->i_mtime = curtime;
2409 dir->i_atime = curtime;
2410 if (IS_DIRSYNC(dir))
2411 (void)exfat_sync_inode(dir);
2412 else
2413 mark_inode_dirty(dir);
2414
2415 clear_nlink(inode);
2416 curtime = current_time(inode);
2417 inode->i_mtime = curtime;
2418 inode->i_atime = curtime;
2419 exfat_detach(inode);
2420 remove_inode_hash(inode);
2421
2422out:
2423 __unlock_super(sb);
2424 pr_debug("%s exited\n", __func__);
2425 return err;
2426}
2427
2428static int exfat_symlink(struct inode *dir, struct dentry *dentry,
2429 const char *target)
2430{
2431 struct super_block *sb = dir->i_sb;
2432 struct timespec64 curtime;
2433 struct inode *inode;
2434 struct file_id_t fid;
2435 loff_t i_pos;
2436 int err;
2437 u64 len = (u64)strlen(target);
2438 u64 ret;
2439
2440 __lock_super(sb);
2441
2442 pr_debug("%s entered\n", __func__);
2443
2444 err = ffsCreateFile(dir, (u8 *)dentry->d_name.name, FM_SYMLINK, &fid);
2445 if (err)
2446 goto out;
2447
2448
2449 err = ffsWriteFile(dir, &fid, (char *)target, len, &ret);
2450
2451 if (err) {
2452 ffsRemoveFile(dir, &fid);
2453 goto out;
2454 }
2455
2456 INC_IVERSION(dir);
2457 curtime = current_time(dir);
2458 dir->i_ctime = curtime;
2459 dir->i_mtime = curtime;
2460 dir->i_atime = curtime;
2461 if (IS_DIRSYNC(dir))
2462 (void)exfat_sync_inode(dir);
2463 else
2464 mark_inode_dirty(dir);
2465
2466 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2467
2468 inode = exfat_build_inode(sb, &fid, i_pos);
2469 if (IS_ERR(inode)) {
2470 err = PTR_ERR(inode);
2471 goto out;
2472 }
2473 INC_IVERSION(inode);
2474 curtime = current_time(inode);
2475 inode->i_mtime = curtime;
2476 inode->i_atime = curtime;
2477 inode->i_ctime = curtime;
2478
2479
2480 EXFAT_I(inode)->target = kmemdup(target, len + 1, GFP_KERNEL);
2481 if (!EXFAT_I(inode)->target) {
2482 err = -ENOMEM;
2483 goto out;
2484 }
2485
2486 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2487 d_instantiate(dentry, inode);
2488
2489out:
2490 __unlock_super(sb);
2491 pr_debug("%s exited\n", __func__);
2492 return err;
2493}
2494
2495static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2496{
2497 struct super_block *sb = dir->i_sb;
2498 struct timespec64 curtime;
2499 struct inode *inode;
2500 struct file_id_t fid;
2501 loff_t i_pos;
2502 int err;
2503
2504 __lock_super(sb);
2505
2506 pr_debug("%s entered\n", __func__);
2507
2508 err = ffsCreateDir(dir, (u8 *)dentry->d_name.name, &fid);
2509 if (err)
2510 goto out;
2511
2512 INC_IVERSION(dir);
2513 curtime = current_time(dir);
2514 dir->i_ctime = curtime;
2515 dir->i_mtime = curtime;
2516 dir->i_atime = curtime;
2517 if (IS_DIRSYNC(dir))
2518 (void)exfat_sync_inode(dir);
2519 else
2520 mark_inode_dirty(dir);
2521 inc_nlink(dir);
2522
2523 i_pos = ((loff_t)fid.dir.dir << 32) | (fid.entry & 0xffffffff);
2524
2525 inode = exfat_build_inode(sb, &fid, i_pos);
2526 if (IS_ERR(inode)) {
2527 err = PTR_ERR(inode);
2528 goto out;
2529 }
2530 INC_IVERSION(inode);
2531 curtime = current_time(inode);
2532 inode->i_mtime = curtime;
2533 inode->i_atime = curtime;
2534 inode->i_ctime = curtime;
2535
2536
2537 dentry->d_time = GET_IVERSION(dentry->d_parent->d_inode);
2538 d_instantiate(dentry, inode);
2539
2540out:
2541 __unlock_super(sb);
2542 pr_debug("%s exited\n", __func__);
2543 return err;
2544}
2545
2546static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
2547{
2548 struct inode *inode = dentry->d_inode;
2549 struct super_block *sb = dir->i_sb;
2550 struct timespec64 curtime;
2551 int err;
2552
2553 __lock_super(sb);
2554
2555 pr_debug("%s entered\n", __func__);
2556
2557 EXFAT_I(inode)->fid.size = i_size_read(inode);
2558
2559 err = ffsRemoveDir(dir, &(EXFAT_I(inode)->fid));
2560 if (err)
2561 goto out;
2562
2563 INC_IVERSION(dir);
2564 curtime = current_time(dir);
2565 dir->i_mtime = curtime;
2566 dir->i_atime = curtime;
2567 if (IS_DIRSYNC(dir))
2568 (void)exfat_sync_inode(dir);
2569 else
2570 mark_inode_dirty(dir);
2571 drop_nlink(dir);
2572
2573 clear_nlink(inode);
2574 curtime = current_time(inode);
2575 inode->i_mtime = curtime;
2576 inode->i_atime = curtime;
2577 exfat_detach(inode);
2578 remove_inode_hash(inode);
2579
2580out:
2581 __unlock_super(sb);
2582 pr_debug("%s exited\n", __func__);
2583 return err;
2584}
2585
2586static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
2587 struct inode *new_dir, struct dentry *new_dentry,
2588 unsigned int flags)
2589{
2590 struct inode *old_inode, *new_inode;
2591 struct super_block *sb = old_dir->i_sb;
2592 struct timespec64 curtime;
2593 loff_t i_pos;
2594 int err;
2595
2596 if (flags)
2597 return -EINVAL;
2598
2599 __lock_super(sb);
2600
2601 pr_debug("%s entered\n", __func__);
2602
2603 old_inode = old_dentry->d_inode;
2604 new_inode = new_dentry->d_inode;
2605
2606 EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
2607
2608 err = ffsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir,
2609 new_dentry);
2610 if (err)
2611 goto out;
2612
2613 INC_IVERSION(new_dir);
2614 curtime = current_time(new_dir);
2615 new_dir->i_ctime = curtime;
2616 new_dir->i_mtime = curtime;
2617 new_dir->i_atime = curtime;
2618
2619 if (IS_DIRSYNC(new_dir))
2620 (void)exfat_sync_inode(new_dir);
2621 else
2622 mark_inode_dirty(new_dir);
2623
2624 i_pos = ((loff_t)EXFAT_I(old_inode)->fid.dir.dir << 32) |
2625 (EXFAT_I(old_inode)->fid.entry & 0xffffffff);
2626
2627 exfat_detach(old_inode);
2628 exfat_attach(old_inode, i_pos);
2629 if (IS_DIRSYNC(new_dir))
2630 (void)exfat_sync_inode(old_inode);
2631 else
2632 mark_inode_dirty(old_inode);
2633
2634 if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
2635 drop_nlink(old_dir);
2636 if (!new_inode)
2637 inc_nlink(new_dir);
2638 }
2639 INC_IVERSION(old_dir);
2640 curtime = current_time(old_dir);
2641 old_dir->i_ctime = curtime;
2642 old_dir->i_mtime = curtime;
2643 if (IS_DIRSYNC(old_dir))
2644 (void)exfat_sync_inode(old_dir);
2645 else
2646 mark_inode_dirty(old_dir);
2647
2648 if (new_inode) {
2649 exfat_detach(new_inode);
2650 drop_nlink(new_inode);
2651 if (S_ISDIR(new_inode->i_mode))
2652 drop_nlink(new_inode);
2653 new_inode->i_ctime = current_time(new_inode);
2654 }
2655
2656out:
2657 __unlock_super(sb);
2658 pr_debug("%s exited\n", __func__);
2659 return err;
2660}
2661
2662static int exfat_cont_expand(struct inode *inode, loff_t size)
2663{
2664 struct address_space *mapping = inode->i_mapping;
2665 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
2666 struct timespec64 curtime;
2667 int err, err2;
2668
2669 err = generic_cont_expand_simple(inode, size);
2670 if (err != 0)
2671 return err;
2672
2673 curtime = current_time(inode);
2674 inode->i_ctime = curtime;
2675 inode->i_mtime = curtime;
2676 mark_inode_dirty(inode);
2677
2678 if (IS_SYNC(inode)) {
2679 err = filemap_fdatawrite_range(mapping, start,
2680 start + count - 1);
2681 err2 = sync_mapping_buffers(mapping);
2682 err = (err) ? (err) : (err2);
2683 err2 = write_inode_now(inode, 1);
2684 err = (err) ? (err) : (err2);
2685 if (!err)
2686 err = filemap_fdatawait_range(mapping, start,
2687 start + count - 1);
2688 }
2689 return err;
2690}
2691
2692static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
2693{
2694 mode_t allow_utime = sbi->options.allow_utime;
2695
2696 if (!uid_eq(current_fsuid(), inode->i_uid)) {
2697 if (in_group_p(inode->i_gid))
2698 allow_utime >>= 3;
2699 if (allow_utime & MAY_WRITE)
2700 return 1;
2701 }
2702
2703
2704 return 0;
2705}
2706
2707static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
2708 struct inode *inode, umode_t *mode_ptr)
2709{
2710 mode_t i_mode, mask, perm;
2711
2712 i_mode = inode->i_mode;
2713
2714 if (S_ISREG(i_mode) || S_ISLNK(i_mode))
2715 mask = sbi->options.fs_fmask;
2716 else
2717 mask = sbi->options.fs_dmask;
2718
2719 perm = *mode_ptr & ~(S_IFMT | mask);
2720
2721
2722 if ((perm & 0555) != (i_mode & 0555))
2723 return -EPERM;
2724
2725 if (exfat_mode_can_hold_ro(inode)) {
2726
2727
2728
2729
2730 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
2731 return -EPERM;
2732 } else {
2733
2734
2735
2736
2737 if ((perm & 0222) != (0222 & ~mask))
2738 return -EPERM;
2739 }
2740
2741 *mode_ptr &= S_IFMT | perm;
2742
2743 return 0;
2744}
2745
2746static void exfat_truncate(struct inode *inode, loff_t old_size)
2747{
2748 struct super_block *sb = inode->i_sb;
2749 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2750 struct fs_info_t *p_fs = &sbi->fs_info;
2751 struct timespec64 curtime;
2752 int err;
2753
2754 __lock_super(sb);
2755
2756
2757
2758
2759
2760 if (EXFAT_I(inode)->mmu_private > i_size_read(inode))
2761 EXFAT_I(inode)->mmu_private = i_size_read(inode);
2762
2763 if (EXFAT_I(inode)->fid.start_clu == 0)
2764 goto out;
2765
2766 err = ffsTruncateFile(inode, old_size, i_size_read(inode));
2767 if (err)
2768 goto out;
2769
2770 curtime = current_time(inode);
2771 inode->i_ctime = curtime;
2772 inode->i_mtime = curtime;
2773 if (IS_DIRSYNC(inode))
2774 (void)exfat_sync_inode(inode);
2775 else
2776 mark_inode_dirty(inode);
2777
2778 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1)) &
2779 ~((loff_t)p_fs->cluster_size - 1)) >> 9;
2780out:
2781 __unlock_super(sb);
2782}
2783
2784static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
2785{
2786 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
2787 struct inode *inode = dentry->d_inode;
2788 unsigned int ia_valid;
2789 int error;
2790 loff_t old_size;
2791
2792 pr_debug("%s entered\n", __func__);
2793
2794 if ((attr->ia_valid & ATTR_SIZE) &&
2795 attr->ia_size > i_size_read(inode)) {
2796 error = exfat_cont_expand(inode, attr->ia_size);
2797 if (error || attr->ia_valid == ATTR_SIZE)
2798 return error;
2799 attr->ia_valid &= ~ATTR_SIZE;
2800 }
2801
2802 ia_valid = attr->ia_valid;
2803
2804 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
2805 exfat_allow_set_time(sbi, inode)) {
2806 attr->ia_valid &= ~(ATTR_MTIME_SET |
2807 ATTR_ATIME_SET |
2808 ATTR_TIMES_SET);
2809 }
2810
2811 error = setattr_prepare(dentry, attr);
2812 attr->ia_valid = ia_valid;
2813 if (error)
2814 return error;
2815
2816 if (((attr->ia_valid & ATTR_UID) &&
2817 (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
2818 ((attr->ia_valid & ATTR_GID) &&
2819 (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
2820 ((attr->ia_valid & ATTR_MODE) &&
2821 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
2822 return -EPERM;
2823 }
2824
2825
2826
2827
2828
2829 if (attr->ia_valid & ATTR_MODE) {
2830 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
2831 attr->ia_valid &= ~ATTR_MODE;
2832 }
2833
2834 EXFAT_I(inode)->fid.size = i_size_read(inode);
2835
2836 if (attr->ia_valid & ATTR_SIZE) {
2837 old_size = i_size_read(inode);
2838 down_write(&EXFAT_I(inode)->truncate_lock);
2839 truncate_setsize(inode, attr->ia_size);
2840 exfat_truncate(inode, old_size);
2841 up_write(&EXFAT_I(inode)->truncate_lock);
2842 }
2843 setattr_copy(inode, attr);
2844 mark_inode_dirty(inode);
2845
2846 pr_debug("%s exited\n", __func__);
2847 return error;
2848}
2849
2850static int exfat_getattr(const struct path *path, struct kstat *stat,
2851 u32 request_mask, unsigned int flags)
2852{
2853 struct inode *inode = path->dentry->d_inode;
2854
2855 pr_debug("%s entered\n", __func__);
2856
2857 generic_fillattr(inode, stat);
2858 stat->blksize = EXFAT_SB(inode->i_sb)->fs_info.cluster_size;
2859
2860 pr_debug("%s exited\n", __func__);
2861 return 0;
2862}
2863
2864static const struct inode_operations exfat_dir_inode_operations = {
2865 .create = exfat_create,
2866 .lookup = exfat_lookup,
2867 .unlink = exfat_unlink,
2868 .symlink = exfat_symlink,
2869 .mkdir = exfat_mkdir,
2870 .rmdir = exfat_rmdir,
2871 .rename = exfat_rename,
2872 .setattr = exfat_setattr,
2873 .getattr = exfat_getattr,
2874};
2875
2876
2877
2878
2879static const char *exfat_get_link(struct dentry *dentry, struct inode *inode,
2880 struct delayed_call *done)
2881{
2882 struct exfat_inode_info *ei = EXFAT_I(inode);
2883
2884 if (ei->target) {
2885 char *cookie = ei->target;
2886
2887 if (cookie)
2888 return (char *)(ei->target);
2889 }
2890 return NULL;
2891}
2892
2893static const struct inode_operations exfat_symlink_inode_operations = {
2894 .get_link = exfat_get_link,
2895};
2896
2897static int exfat_file_release(struct inode *inode, struct file *filp)
2898{
2899 struct super_block *sb = inode->i_sb;
2900
2901 EXFAT_I(inode)->fid.size = i_size_read(inode);
2902 ffsSyncVol(sb, false);
2903 return 0;
2904}
2905
2906static const struct file_operations exfat_file_operations = {
2907 .llseek = generic_file_llseek,
2908 .read_iter = generic_file_read_iter,
2909 .write_iter = generic_file_write_iter,
2910 .mmap = generic_file_mmap,
2911 .release = exfat_file_release,
2912 .unlocked_ioctl = exfat_generic_ioctl,
2913 .fsync = generic_file_fsync,
2914 .splice_read = generic_file_splice_read,
2915};
2916
2917static const struct inode_operations exfat_file_inode_operations = {
2918 .setattr = exfat_setattr,
2919 .getattr = exfat_getattr,
2920};
2921
2922
2923
2924
2925
2926static int exfat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
2927 unsigned long *mapped_blocks, int *create)
2928{
2929 struct super_block *sb = inode->i_sb;
2930 struct exfat_sb_info *sbi = EXFAT_SB(sb);
2931 struct fs_info_t *p_fs = &sbi->fs_info;
2932 const unsigned long blocksize = sb->s_blocksize;
2933 const unsigned char blocksize_bits = sb->s_blocksize_bits;
2934 sector_t last_block;
2935 int err, clu_offset, sec_offset;
2936 unsigned int cluster;
2937
2938 *phys = 0;
2939 *mapped_blocks = 0;
2940
2941 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
2942 if (sector >= last_block) {
2943 if (*create == 0)
2944 return 0;
2945 } else {
2946 *create = 0;
2947 }
2948
2949
2950 clu_offset = sector >> p_fs->sectors_per_clu_bits;
2951
2952
2953 sec_offset = sector & (p_fs->sectors_per_clu - 1);
2954
2955 EXFAT_I(inode)->fid.size = i_size_read(inode);
2956
2957 err = ffsMapCluster(inode, clu_offset, &cluster);
2958
2959 if (!err && (cluster != CLUSTER_32(~0))) {
2960 *phys = START_SECTOR(cluster) + sec_offset;
2961 *mapped_blocks = p_fs->sectors_per_clu - sec_offset;
2962 }
2963
2964 return 0;
2965}
2966
2967static int exfat_get_block(struct inode *inode, sector_t iblock,
2968 struct buffer_head *bh_result, int create)
2969{
2970 struct super_block *sb = inode->i_sb;
2971 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
2972 int err;
2973 unsigned long mapped_blocks;
2974 sector_t phys;
2975
2976 __lock_super(sb);
2977
2978 err = exfat_bmap(inode, iblock, &phys, &mapped_blocks, &create);
2979 if (err) {
2980 __unlock_super(sb);
2981 return err;
2982 }
2983
2984 if (phys) {
2985 max_blocks = min(mapped_blocks, max_blocks);
2986 if (create) {
2987 EXFAT_I(inode)->mmu_private += max_blocks <<
2988 sb->s_blocksize_bits;
2989 set_buffer_new(bh_result);
2990 }
2991 map_bh(bh_result, sb, phys);
2992 }
2993
2994 bh_result->b_size = max_blocks << sb->s_blocksize_bits;
2995 __unlock_super(sb);
2996
2997 return 0;
2998}
2999
3000static int exfat_readpage(struct file *file, struct page *page)
3001{
3002 return mpage_readpage(page, exfat_get_block);
3003}
3004
3005static int exfat_readpages(struct file *file, struct address_space *mapping,
3006 struct list_head *pages, unsigned int nr_pages)
3007{
3008 return mpage_readpages(mapping, pages, nr_pages, exfat_get_block);
3009}
3010
3011static int exfat_writepage(struct page *page, struct writeback_control *wbc)
3012{
3013 return block_write_full_page(page, exfat_get_block, wbc);
3014}
3015
3016static int exfat_writepages(struct address_space *mapping,
3017 struct writeback_control *wbc)
3018{
3019 return mpage_writepages(mapping, wbc, exfat_get_block);
3020}
3021
3022static void exfat_write_failed(struct address_space *mapping, loff_t to)
3023{
3024 struct inode *inode = mapping->host;
3025
3026 if (to > i_size_read(inode)) {
3027 truncate_pagecache(inode, i_size_read(inode));
3028 EXFAT_I(inode)->fid.size = i_size_read(inode);
3029 exfat_truncate(inode, i_size_read(inode));
3030 }
3031}
3032
3033static int exfat_write_begin(struct file *file, struct address_space *mapping,
3034 loff_t pos, unsigned int len, unsigned int flags,
3035 struct page **pagep, void **fsdata)
3036{
3037 int ret;
3038
3039 *pagep = NULL;
3040 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
3041 exfat_get_block,
3042 &EXFAT_I(mapping->host)->mmu_private);
3043
3044 if (ret < 0)
3045 exfat_write_failed(mapping, pos + len);
3046 return ret;
3047}
3048
3049static int exfat_write_end(struct file *file, struct address_space *mapping,
3050 loff_t pos, unsigned int len, unsigned int copied,
3051 struct page *pagep, void *fsdata)
3052{
3053 struct inode *inode = mapping->host;
3054 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
3055 struct timespec64 curtime;
3056 int err;
3057
3058 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
3059
3060 if (err < len)
3061 exfat_write_failed(mapping, pos + len);
3062
3063 if (!(err < 0) && !(fid->attr & ATTR_ARCHIVE)) {
3064 curtime = current_time(inode);
3065 inode->i_mtime = curtime;
3066 inode->i_ctime = curtime;
3067 fid->attr |= ATTR_ARCHIVE;
3068 mark_inode_dirty(inode);
3069 }
3070 return err;
3071}
3072
3073static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3074{
3075 struct inode *inode = iocb->ki_filp->f_mapping->host;
3076 struct address_space *mapping = iocb->ki_filp->f_mapping;
3077 ssize_t ret;
3078 int rw;
3079
3080 rw = iov_iter_rw(iter);
3081
3082 if (rw == WRITE) {
3083 if (EXFAT_I(inode)->mmu_private < iov_iter_count(iter))
3084 return 0;
3085 }
3086 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
3087
3088 if ((ret < 0) && (rw & WRITE))
3089 exfat_write_failed(mapping, iov_iter_count(iter));
3090 return ret;
3091}
3092
3093static sector_t _exfat_bmap(struct address_space *mapping, sector_t block)
3094{
3095 sector_t blocknr;
3096
3097
3098 down_read(&EXFAT_I(mapping->host)->truncate_lock);
3099 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
3100 up_read(&EXFAT_I(mapping->host)->truncate_lock);
3101
3102 return blocknr;
3103}
3104
3105static const struct address_space_operations exfat_aops = {
3106 .readpage = exfat_readpage,
3107 .readpages = exfat_readpages,
3108 .writepage = exfat_writepage,
3109 .writepages = exfat_writepages,
3110 .write_begin = exfat_write_begin,
3111 .write_end = exfat_write_end,
3112 .direct_IO = exfat_direct_IO,
3113 .bmap = _exfat_bmap
3114};
3115
3116
3117
3118
3119
3120static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
3121{
3122 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3123 struct exfat_inode_info *info;
3124 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
3125 struct inode *inode = NULL;
3126
3127 spin_lock(&sbi->inode_hash_lock);
3128 hlist_for_each_entry(info, head, i_hash_fat) {
3129 BUG_ON(info->vfs_inode.i_sb != sb);
3130
3131 if (i_pos != info->i_pos)
3132 continue;
3133 inode = igrab(&info->vfs_inode);
3134 if (inode)
3135 break;
3136 }
3137 spin_unlock(&sbi->inode_hash_lock);
3138 return inode;
3139}
3140
3141
3142static int exfat_fill_inode(struct inode *inode, struct file_id_t *fid)
3143{
3144 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
3145 struct fs_info_t *p_fs = &sbi->fs_info;
3146 struct dir_entry_t info;
3147
3148 memcpy(&(EXFAT_I(inode)->fid), fid, sizeof(struct file_id_t));
3149
3150 ffsReadStat(inode, &info);
3151
3152 EXFAT_I(inode)->i_pos = 0;
3153 EXFAT_I(inode)->target = NULL;
3154 inode->i_uid = sbi->options.fs_uid;
3155 inode->i_gid = sbi->options.fs_gid;
3156 INC_IVERSION(inode);
3157 inode->i_generation = prandom_u32();
3158
3159 if (info.Attr & ATTR_SUBDIR) {
3160 inode->i_generation &= ~1;
3161 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3162 inode->i_op = &exfat_dir_inode_operations;
3163 inode->i_fop = &exfat_dir_operations;
3164
3165 i_size_write(inode, info.Size);
3166 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3167 set_nlink(inode, info.NumSubdirs);
3168 } else if (info.Attr & ATTR_SYMLINK) {
3169 inode->i_generation |= 1;
3170 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3171 inode->i_op = &exfat_symlink_inode_operations;
3172
3173 i_size_write(inode, info.Size);
3174 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3175 } else {
3176 inode->i_generation |= 1;
3177 inode->i_mode = exfat_make_mode(sbi, info.Attr, 0777);
3178 inode->i_op = &exfat_file_inode_operations;
3179 inode->i_fop = &exfat_file_operations;
3180 inode->i_mapping->a_ops = &exfat_aops;
3181 inode->i_mapping->nrpages = 0;
3182
3183 i_size_write(inode, info.Size);
3184 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3185 }
3186 exfat_save_attr(inode, info.Attr);
3187
3188 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
3189 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
3190
3191 exfat_time_fat2unix(&inode->i_mtime, &info.ModifyTimestamp);
3192 exfat_time_fat2unix(&inode->i_ctime, &info.CreateTimestamp);
3193 exfat_time_fat2unix(&inode->i_atime, &info.AccessTimestamp);
3194
3195 return 0;
3196}
3197
3198static struct inode *exfat_build_inode(struct super_block *sb,
3199 struct file_id_t *fid, loff_t i_pos)
3200{
3201 struct inode *inode;
3202 int err;
3203
3204 inode = exfat_iget(sb, i_pos);
3205 if (inode)
3206 goto out;
3207 inode = new_inode(sb);
3208 if (!inode) {
3209 inode = ERR_PTR(-ENOMEM);
3210 goto out;
3211 }
3212 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
3213 SET_IVERSION(inode, 1);
3214 err = exfat_fill_inode(inode, fid);
3215 if (err) {
3216 iput(inode);
3217 inode = ERR_PTR(err);
3218 goto out;
3219 }
3220 exfat_attach(inode, i_pos);
3221 insert_inode_hash(inode);
3222out:
3223 return inode;
3224}
3225
3226static int exfat_sync_inode(struct inode *inode)
3227{
3228 return exfat_write_inode(inode, NULL);
3229}
3230
3231static struct inode *exfat_alloc_inode(struct super_block *sb)
3232{
3233 struct exfat_inode_info *ei;
3234
3235 ei = kmem_cache_alloc(exfat_inode_cachep, GFP_NOFS);
3236 if (!ei)
3237 return NULL;
3238
3239 init_rwsem(&ei->truncate_lock);
3240
3241 return &ei->vfs_inode;
3242}
3243
3244static void exfat_destroy_inode(struct inode *inode)
3245{
3246 kfree(EXFAT_I(inode)->target);
3247 EXFAT_I(inode)->target = NULL;
3248
3249 kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
3250}
3251
3252static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
3253{
3254 struct dir_entry_t info;
3255
3256 if (inode->i_ino == EXFAT_ROOT_INO)
3257 return 0;
3258
3259 info.Attr = exfat_make_attr(inode);
3260 info.Size = i_size_read(inode);
3261
3262 exfat_time_unix2fat(&inode->i_mtime, &info.ModifyTimestamp);
3263 exfat_time_unix2fat(&inode->i_ctime, &info.CreateTimestamp);
3264 exfat_time_unix2fat(&inode->i_atime, &info.AccessTimestamp);
3265
3266 ffsWriteStat(inode, &info);
3267
3268 return 0;
3269}
3270
3271static void exfat_evict_inode(struct inode *inode)
3272{
3273 truncate_inode_pages(&inode->i_data, 0);
3274
3275 if (!inode->i_nlink)
3276 i_size_write(inode, 0);
3277 invalidate_inode_buffers(inode);
3278 clear_inode(inode);
3279 exfat_detach(inode);
3280
3281 remove_inode_hash(inode);
3282}
3283
3284static void exfat_free_super(struct exfat_sb_info *sbi)
3285{
3286 if (sbi->nls_disk)
3287 unload_nls(sbi->nls_disk);
3288 if (sbi->nls_io)
3289 unload_nls(sbi->nls_io);
3290 if (sbi->options.iocharset != exfat_default_iocharset)
3291 kfree(sbi->options.iocharset);
3292
3293 mutex_destroy(&sbi->s_lock);
3294 kvfree(sbi);
3295}
3296
3297static void exfat_put_super(struct super_block *sb)
3298{
3299 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3300
3301 if (__is_sb_dirty(sb))
3302 exfat_write_super(sb);
3303
3304 ffsUmountVol(sb);
3305
3306 sb->s_fs_info = NULL;
3307 exfat_free_super(sbi);
3308}
3309
3310static void exfat_write_super(struct super_block *sb)
3311{
3312 __lock_super(sb);
3313
3314 __set_sb_clean(sb);
3315
3316 if (!sb_rdonly(sb))
3317 ffsSyncVol(sb, true);
3318
3319 __unlock_super(sb);
3320}
3321
3322static int exfat_sync_fs(struct super_block *sb, int wait)
3323{
3324 int err = 0;
3325
3326 if (__is_sb_dirty(sb)) {
3327 __lock_super(sb);
3328 __set_sb_clean(sb);
3329 err = ffsSyncVol(sb, true);
3330 __unlock_super(sb);
3331 }
3332
3333 return err;
3334}
3335
3336static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
3337{
3338 struct super_block *sb = dentry->d_sb;
3339 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
3340 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3341 struct vol_info_t info;
3342
3343 if (p_fs->used_clusters == UINT_MAX) {
3344 if (ffsGetVolInfo(sb, &info) == -EIO)
3345 return -EIO;
3346
3347 } else {
3348 info.FatType = p_fs->vol_type;
3349 info.ClusterSize = p_fs->cluster_size;
3350 info.NumClusters = p_fs->num_clusters - 2;
3351 info.UsedClusters = p_fs->used_clusters;
3352 info.FreeClusters = info.NumClusters - info.UsedClusters;
3353
3354 if (p_fs->dev_ejected)
3355 pr_info("[EXFAT] statfs on device that is ejected\n");
3356 }
3357
3358 buf->f_type = sb->s_magic;
3359 buf->f_bsize = info.ClusterSize;
3360 buf->f_blocks = info.NumClusters;
3361 buf->f_bfree = info.FreeClusters;
3362 buf->f_bavail = info.FreeClusters;
3363 buf->f_fsid.val[0] = (u32)id;
3364 buf->f_fsid.val[1] = (u32)(id >> 32);
3365 buf->f_namelen = 260;
3366
3367 return 0;
3368}
3369
3370static int exfat_remount(struct super_block *sb, int *flags, char *data)
3371{
3372 *flags |= SB_NODIRATIME;
3373 return 0;
3374}
3375
3376static int exfat_show_options(struct seq_file *m, struct dentry *root)
3377{
3378 struct exfat_sb_info *sbi = EXFAT_SB(root->d_sb);
3379 struct exfat_mount_options *opts = &sbi->options;
3380
3381 if (__kuid_val(opts->fs_uid))
3382 seq_printf(m, ",uid=%u", __kuid_val(opts->fs_uid));
3383 if (__kgid_val(opts->fs_gid))
3384 seq_printf(m, ",gid=%u", __kgid_val(opts->fs_gid));
3385 seq_printf(m, ",fmask=%04o", opts->fs_fmask);
3386 seq_printf(m, ",dmask=%04o", opts->fs_dmask);
3387 if (opts->allow_utime)
3388 seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
3389 if (sbi->nls_disk)
3390 seq_printf(m, ",codepage=%s", sbi->nls_disk->charset);
3391 if (sbi->nls_io)
3392 seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
3393 seq_printf(m, ",namecase=%u", opts->casesensitive);
3394 if (opts->errors == EXFAT_ERRORS_CONT)
3395 seq_puts(m, ",errors=continue");
3396 else if (opts->errors == EXFAT_ERRORS_PANIC)
3397 seq_puts(m, ",errors=panic");
3398 else
3399 seq_puts(m, ",errors=remount-ro");
3400#ifdef CONFIG_STAGING_EXFAT_DISCARD
3401 if (opts->discard)
3402 seq_puts(m, ",discard");
3403#endif
3404 return 0;
3405}
3406
3407static const struct super_operations exfat_sops = {
3408 .alloc_inode = exfat_alloc_inode,
3409 .destroy_inode = exfat_destroy_inode,
3410 .write_inode = exfat_write_inode,
3411 .evict_inode = exfat_evict_inode,
3412 .put_super = exfat_put_super,
3413 .sync_fs = exfat_sync_fs,
3414 .statfs = exfat_statfs,
3415 .remount_fs = exfat_remount,
3416 .show_options = exfat_show_options,
3417};
3418
3419
3420
3421
3422
3423static struct inode *exfat_nfs_get_inode(struct super_block *sb, u64 ino,
3424 u32 generation)
3425{
3426 struct inode *inode = NULL;
3427
3428 if (ino < EXFAT_ROOT_INO)
3429 return inode;
3430 inode = ilookup(sb, ino);
3431
3432 if (inode && generation && (inode->i_generation != generation)) {
3433 iput(inode);
3434 inode = NULL;
3435 }
3436
3437 return inode;
3438}
3439
3440static struct dentry *exfat_fh_to_dentry(struct super_block *sb,
3441 struct fid *fid, int fh_len,
3442 int fh_type)
3443{
3444 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
3445 exfat_nfs_get_inode);
3446}
3447
3448static struct dentry *exfat_fh_to_parent(struct super_block *sb,
3449 struct fid *fid, int fh_len,
3450 int fh_type)
3451{
3452 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
3453 exfat_nfs_get_inode);
3454}
3455
3456static const struct export_operations exfat_export_ops = {
3457 .fh_to_dentry = exfat_fh_to_dentry,
3458 .fh_to_parent = exfat_fh_to_parent,
3459};
3460
3461
3462
3463
3464
3465enum {
3466 Opt_uid,
3467 Opt_gid,
3468 Opt_umask,
3469 Opt_dmask,
3470 Opt_fmask,
3471 Opt_allow_utime,
3472 Opt_codepage,
3473 Opt_charset,
3474 Opt_namecase,
3475 Opt_debug,
3476 Opt_err_cont,
3477 Opt_err_panic,
3478 Opt_err_ro,
3479 Opt_utf8_hack,
3480 Opt_err,
3481#ifdef CONFIG_STAGING_EXFAT_DISCARD
3482 Opt_discard,
3483#endif
3484};
3485
3486static const match_table_t exfat_tokens = {
3487 {Opt_uid, "uid=%u"},
3488 {Opt_gid, "gid=%u"},
3489 {Opt_umask, "umask=%o"},
3490 {Opt_dmask, "dmask=%o"},
3491 {Opt_fmask, "fmask=%o"},
3492 {Opt_allow_utime, "allow_utime=%o"},
3493 {Opt_codepage, "codepage=%u"},
3494 {Opt_charset, "iocharset=%s"},
3495 {Opt_namecase, "namecase=%u"},
3496 {Opt_debug, "debug"},
3497 {Opt_err_cont, "errors=continue"},
3498 {Opt_err_panic, "errors=panic"},
3499 {Opt_err_ro, "errors=remount-ro"},
3500 {Opt_utf8_hack, "utf8"},
3501#ifdef CONFIG_STAGING_EXFAT_DISCARD
3502 {Opt_discard, "discard"},
3503#endif
3504 {Opt_err, NULL}
3505};
3506
3507static int parse_options(char *options, int silent, int *debug,
3508 struct exfat_mount_options *opts)
3509{
3510 char *p;
3511 substring_t args[MAX_OPT_ARGS];
3512 int option;
3513 char *iocharset;
3514
3515 opts->fs_uid = current_uid();
3516 opts->fs_gid = current_gid();
3517 opts->fs_fmask = current->fs->umask;
3518 opts->fs_dmask = current->fs->umask;
3519 opts->allow_utime = U16_MAX;
3520 opts->codepage = exfat_default_codepage;
3521 opts->iocharset = exfat_default_iocharset;
3522 opts->casesensitive = 0;
3523 opts->errors = EXFAT_ERRORS_RO;
3524#ifdef CONFIG_STAGING_EXFAT_DISCARD
3525 opts->discard = 0;
3526#endif
3527 *debug = 0;
3528
3529 if (!options)
3530 goto out;
3531
3532 while ((p = strsep(&options, ","))) {
3533 int token;
3534
3535 if (!*p)
3536 continue;
3537
3538 token = match_token(p, exfat_tokens, args);
3539 switch (token) {
3540 case Opt_uid:
3541 if (match_int(&args[0], &option))
3542 return 0;
3543 opts->fs_uid = KUIDT_INIT(option);
3544 break;
3545 case Opt_gid:
3546 if (match_int(&args[0], &option))
3547 return 0;
3548 opts->fs_gid = KGIDT_INIT(option);
3549 break;
3550 case Opt_umask:
3551 case Opt_dmask:
3552 case Opt_fmask:
3553 if (match_octal(&args[0], &option))
3554 return 0;
3555 if (token != Opt_dmask)
3556 opts->fs_fmask = option;
3557 if (token != Opt_fmask)
3558 opts->fs_dmask = option;
3559 break;
3560 case Opt_allow_utime:
3561 if (match_octal(&args[0], &option))
3562 return 0;
3563 opts->allow_utime = option & 0022;
3564 break;
3565 case Opt_codepage:
3566 if (match_int(&args[0], &option))
3567 return 0;
3568 opts->codepage = option;
3569 break;
3570 case Opt_charset:
3571 if (opts->iocharset != exfat_default_iocharset)
3572 kfree(opts->iocharset);
3573 iocharset = match_strdup(&args[0]);
3574 if (!iocharset)
3575 return -ENOMEM;
3576 opts->iocharset = iocharset;
3577 break;
3578 case Opt_namecase:
3579 if (match_int(&args[0], &option))
3580 return 0;
3581 opts->casesensitive = option;
3582 break;
3583 case Opt_err_cont:
3584 opts->errors = EXFAT_ERRORS_CONT;
3585 break;
3586 case Opt_err_panic:
3587 opts->errors = EXFAT_ERRORS_PANIC;
3588 break;
3589 case Opt_err_ro:
3590 opts->errors = EXFAT_ERRORS_RO;
3591 break;
3592 case Opt_debug:
3593 *debug = 1;
3594 break;
3595#ifdef CONFIG_STAGING_EXFAT_DISCARD
3596 case Opt_discard:
3597 opts->discard = 1;
3598 break;
3599#endif
3600 case Opt_utf8_hack:
3601 break;
3602 default:
3603 if (!silent)
3604 pr_err("[EXFAT] Unrecognized mount option %s or missing value\n",
3605 p);
3606 return -EINVAL;
3607 }
3608 }
3609
3610out:
3611 if (opts->allow_utime == U16_MAX)
3612 opts->allow_utime = ~opts->fs_dmask & 0022;
3613
3614 return 0;
3615}
3616
3617static void exfat_hash_init(struct super_block *sb)
3618{
3619 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3620 int i;
3621
3622 spin_lock_init(&sbi->inode_hash_lock);
3623 for (i = 0; i < EXFAT_HASH_SIZE; i++)
3624 INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
3625}
3626
3627static int exfat_read_root(struct inode *inode)
3628{
3629 struct super_block *sb = inode->i_sb;
3630 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3631 struct fs_info_t *p_fs = &sbi->fs_info;
3632 struct timespec64 curtime;
3633 struct dir_entry_t info;
3634
3635 EXFAT_I(inode)->fid.dir.dir = p_fs->root_dir;
3636 EXFAT_I(inode)->fid.dir.flags = 0x01;
3637 EXFAT_I(inode)->fid.entry = -1;
3638 EXFAT_I(inode)->fid.start_clu = p_fs->root_dir;
3639 EXFAT_I(inode)->fid.flags = 0x01;
3640 EXFAT_I(inode)->fid.type = TYPE_DIR;
3641 EXFAT_I(inode)->fid.rwoffset = 0;
3642 EXFAT_I(inode)->fid.hint_last_off = -1;
3643
3644 EXFAT_I(inode)->target = NULL;
3645
3646 ffsReadStat(inode, &info);
3647
3648 inode->i_uid = sbi->options.fs_uid;
3649 inode->i_gid = sbi->options.fs_gid;
3650 INC_IVERSION(inode);
3651 inode->i_generation = 0;
3652 inode->i_mode = exfat_make_mode(sbi, ATTR_SUBDIR, 0777);
3653 inode->i_op = &exfat_dir_inode_operations;
3654 inode->i_fop = &exfat_dir_operations;
3655
3656 i_size_write(inode, info.Size);
3657 inode->i_blocks = ((i_size_read(inode) + (p_fs->cluster_size - 1))
3658 & ~((loff_t)p_fs->cluster_size - 1)) >> 9;
3659 EXFAT_I(inode)->i_pos = ((loff_t)p_fs->root_dir << 32) | 0xffffffff;
3660 EXFAT_I(inode)->mmu_private = i_size_read(inode);
3661
3662 exfat_save_attr(inode, ATTR_SUBDIR);
3663 curtime = current_time(inode);
3664 inode->i_mtime = curtime;
3665 inode->i_atime = curtime;
3666 inode->i_ctime = curtime;
3667 set_nlink(inode, info.NumSubdirs + 2);
3668
3669 return 0;
3670}
3671
3672static void setup_dops(struct super_block *sb)
3673{
3674 if (EXFAT_SB(sb)->options.casesensitive == 0)
3675 sb->s_d_op = &exfat_ci_dentry_ops;
3676 else
3677 sb->s_d_op = &exfat_dentry_ops;
3678}
3679
3680static int exfat_fill_super(struct super_block *sb, void *data, int silent)
3681{
3682 struct inode *root_inode = NULL;
3683 struct exfat_sb_info *sbi;
3684 int debug, ret;
3685 long error;
3686
3687
3688
3689
3690
3691
3692
3693 sbi = kvzalloc(sizeof(*sbi), GFP_KERNEL);
3694 if (!sbi)
3695 return -ENOMEM;
3696 mutex_init(&sbi->s_lock);
3697 sb->s_fs_info = sbi;
3698 sb->s_flags |= SB_NODIRATIME;
3699 sb->s_magic = EXFAT_SUPER_MAGIC;
3700 sb->s_op = &exfat_sops;
3701 sb->s_export_op = &exfat_export_ops;
3702
3703 error = parse_options(data, silent, &debug, &sbi->options);
3704 if (error)
3705 goto out_fail;
3706
3707 setup_dops(sb);
3708
3709 error = -EIO;
3710 sb_min_blocksize(sb, 512);
3711 sb->s_maxbytes = 0x7fffffffffffffffLL;
3712
3713 ret = ffsMountVol(sb);
3714 if (ret) {
3715 if (!silent)
3716 pr_err("[EXFAT] ffsMountVol failed\n");
3717
3718 goto out_fail;
3719 }
3720
3721
3722 exfat_hash_init(sb);
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732 sbi->nls_io = load_nls(sbi->options.iocharset);
3733
3734 error = -ENOMEM;
3735 root_inode = new_inode(sb);
3736 if (!root_inode)
3737 goto out_fail2;
3738 root_inode->i_ino = EXFAT_ROOT_INO;
3739 SET_IVERSION(root_inode, 1);
3740
3741 error = exfat_read_root(root_inode);
3742 if (error < 0)
3743 goto out_fail2;
3744 error = -ENOMEM;
3745 exfat_attach(root_inode, EXFAT_I(root_inode)->i_pos);
3746 insert_inode_hash(root_inode);
3747 sb->s_root = d_make_root(root_inode);
3748 if (!sb->s_root) {
3749 pr_err("[EXFAT] Getting the root inode failed\n");
3750 goto out_fail2;
3751 }
3752
3753 return 0;
3754
3755out_fail2:
3756 ffsUmountVol(sb);
3757out_fail:
3758 if (root_inode)
3759 iput(root_inode);
3760 sb->s_fs_info = NULL;
3761 exfat_free_super(sbi);
3762 return error;
3763}
3764
3765static struct dentry *exfat_fs_mount(struct file_system_type *fs_type,
3766 int flags, const char *dev_name,
3767 void *data)
3768{
3769 return mount_bdev(fs_type, flags, dev_name, data, exfat_fill_super);
3770}
3771
3772static void init_once(void *foo)
3773{
3774 struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
3775
3776 INIT_HLIST_NODE(&ei->i_hash_fat);
3777 inode_init_once(&ei->vfs_inode);
3778}
3779
3780static int __init exfat_init_inodecache(void)
3781{
3782 exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
3783 sizeof(struct exfat_inode_info),
3784 0,
3785 (SLAB_RECLAIM_ACCOUNT |
3786 SLAB_MEM_SPREAD),
3787 init_once);
3788 if (!exfat_inode_cachep)
3789 return -ENOMEM;
3790 return 0;
3791}
3792
3793static void __exit exfat_destroy_inodecache(void)
3794{
3795
3796
3797
3798
3799 rcu_barrier();
3800 kmem_cache_destroy(exfat_inode_cachep);
3801}
3802
3803#ifdef CONFIG_STAGING_EXFAT_KERNEL_DEBUG
3804static void exfat_debug_kill_sb(struct super_block *sb)
3805{
3806 struct exfat_sb_info *sbi = EXFAT_SB(sb);
3807 struct block_device *bdev = sb->s_bdev;
3808 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3809
3810 long flags;
3811
3812 if (sbi) {
3813 flags = sbi->debug_flags;
3814
3815 if (flags & EXFAT_DEBUGFLAGS_INVALID_UMOUNT) {
3816
3817
3818
3819
3820 mutex_lock(&p_fs->v_mutex);
3821 exfat_fat_release_all(sb);
3822 exfat_buf_release_all(sb);
3823 mutex_unlock(&p_fs->v_mutex);
3824
3825 invalidate_bdev(bdev);
3826 }
3827 }
3828
3829 kill_block_super(sb);
3830}
3831#endif
3832
3833static struct file_system_type exfat_fs_type = {
3834 .owner = THIS_MODULE,
3835 .name = "exfat",
3836 .mount = exfat_fs_mount,
3837#ifdef CONFIG_STAGING_EXFAT_KERNEL_DEBUG
3838 .kill_sb = exfat_debug_kill_sb,
3839#else
3840 .kill_sb = kill_block_super,
3841#endif
3842 .fs_flags = FS_REQUIRES_DEV,
3843};
3844
3845static int __init init_exfat(void)
3846{
3847 int err;
3848
3849 BUILD_BUG_ON(sizeof(struct dentry_t) != DENTRY_SIZE);
3850 BUILD_BUG_ON(sizeof(struct dos_dentry_t) != DENTRY_SIZE);
3851 BUILD_BUG_ON(sizeof(struct ext_dentry_t) != DENTRY_SIZE);
3852 BUILD_BUG_ON(sizeof(struct file_dentry_t) != DENTRY_SIZE);
3853 BUILD_BUG_ON(sizeof(struct strm_dentry_t) != DENTRY_SIZE);
3854 BUILD_BUG_ON(sizeof(struct name_dentry_t) != DENTRY_SIZE);
3855 BUILD_BUG_ON(sizeof(struct bmap_dentry_t) != DENTRY_SIZE);
3856 BUILD_BUG_ON(sizeof(struct case_dentry_t) != DENTRY_SIZE);
3857 BUILD_BUG_ON(sizeof(struct volm_dentry_t) != DENTRY_SIZE);
3858
3859 pr_info("exFAT: Version %s\n", EXFAT_VERSION);
3860
3861 err = exfat_init_inodecache();
3862 if (err)
3863 return err;
3864
3865 err = register_filesystem(&exfat_fs_type);
3866 if (err)
3867 return err;
3868
3869 return 0;
3870}
3871
3872static void __exit exit_exfat(void)
3873{
3874 exfat_destroy_inodecache();
3875 unregister_filesystem(&exfat_fs_type);
3876}
3877
3878module_init(init_exfat);
3879module_exit(exit_exfat);
3880
3881MODULE_LICENSE("GPL");
3882MODULE_DESCRIPTION("exFAT Filesystem Driver");
3883MODULE_ALIAS_FS("exfat");
3884