forked from dorimanx/exfat-nofuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exfat_super.c
2116 lines (1784 loc) · 51.4 KB
/
exfat_super.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/slab.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
#include <linux/smp_lock.h>
#endif
#include <linux/seq_file.h>
#include <linux/pagemap.h>
#include <linux/mpage.h>
#include <linux/buffer_head.h>
#include <linux/exportfs.h>
#include <linux/mount.h>
#include <linux/vfs.h>
#include <linux/parser.h>
#include <linux/uio.h>
#include <linux/writeback.h>
#include <linux/log2.h>
#include <linux/hash.h>
#include <linux/backing-dev.h>
#include <linux/sched.h>
#include <linux/fs_struct.h>
#include <linux/namei.h>
#include <asm/current.h>
#include <asm/unaligned.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(3,9,9)
#include <linux/aio.h>
#endif
#include "exfat_version.h"
#include "exfat_config.h"
#include "exfat_global.h"
#include "exfat_data.h"
#include "exfat_oal.h"
#include "exfat_blkdev.h"
#include "exfat_cache.h"
#include "exfat_part.h"
#include "exfat_nls.h"
#include "exfat_api.h"
#include "exfat.h"
#include "exfat_super.h"
static struct kmem_cache *exfat_inode_cachep;
static int exfat_default_codepage = DEFAULT_CODEPAGE;
static char exfat_default_iocharset[] = DEFAULT_IOCHARSET;
extern struct timezone sys_tz;
#define ELAPSED_TIME 0
#if (ELAPSED_TIME == 1)
#include <linux/time.h>
static UINT32 __t1, __t2;
static UINT32 get_current_msec(void)
{
struct timeval tm;
do_gettimeofday(&tm);
return((UINT32)(tm.tv_sec*1000000 + tm.tv_usec));
}
#define TIME_START() do {__t1 = get_current_msec();} while (0)
#define TIME_END() do {__t2 = get_current_msec();} while (0)
#define PRINT_TIME(n) do {printk("[EXFAT] Elapsed time %d = %d (usec)\n", n, (__t2 - __t1));} while (0)
#else
#define TIME_START()
#define TIME_END()
#define PRINT_TIME(n)
#endif
#define SECS_PER_MIN (60)
#define SECS_PER_HOUR (SECS_PER_MIN * 60)
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
#define UNIX_SECS_1980 315532800L
#if BITS_PER_LONG == 64
#define UNIX_SECS_2108 4354819200L
#endif
/* days between 1.1.70 and 1.1.80 (2 leap days) */
#define DAYS_DELTA (365 * 10 + 2)
/* 120 (2100 - 1980) isn't leap year */
#define YEAR_2100 120
#define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
/* Linear day numbers of the respective 1sts in non-leap years. */
static time_t days_in_year[] = {
/* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
};
static void exfat_truncate(struct inode *inode);
/* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
void exfat_time_fat2unix(struct exfat_sb_info *sbi, struct timespec *ts,
DATE_TIME_T *tp)
{
time_t leap_day, year = tp->Year;
leap_day = (year + 3) / 4;
if (year > YEAR_2100) /* 2100 isn't leap year */
leap_day = ((year + 3) / 4) - 1;
else
leap_day = ((year + 3) / 4);
if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
leap_day++;
ts->tv_sec = tp->Second
+ tp->Minute * SECS_PER_MIN
+ tp->Hour * SECS_PER_HOUR
+ (year * 365 + leap_day + days_in_year[(tp->Month)] + (tp->Day - 1) + DAYS_DELTA) * SECS_PER_DAY
+ sys_tz.tz_minuteswest * SECS_PER_MIN;
ts->tv_nsec = 0;
}
/* Convert linear UNIX date to a FAT time/date pair. */
void exfat_time_unix2fat(struct exfat_sb_info *sbi, struct timespec *ts,
DATE_TIME_T *tp)
{
time_t second = ts->tv_sec;
time_t day, leap_day, month, year;
second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
if (second < UNIX_SECS_1980) {
tp->Second = 0;
tp->Minute = 0;
tp->Hour = 0;
tp->Day = 1;
tp->Month = 1;
tp->Year = 0;
return;
}
#if BITS_PER_LONG == 64
if (second >= UNIX_SECS_2108) {
tp->Second = 59;
tp->Minute = 59;
tp->Hour = 23;
tp->Day = 31;
tp->Month = 12;
tp->Year = 127;
return;
}
#endif
day = second / SECS_PER_DAY - DAYS_DELTA;
year = day / 365;
leap_day = (year + 3) / 4;
if (year > YEAR_2100) /* 2100 isn't leap year */
leap_day--;
if (year * 365 + leap_day > day)
year--;
leap_day = (year + 3) / 4;
if (year > YEAR_2100) /* 2100 isn't leap year */
leap_day--;
day -= year * 365 + leap_day;
if (IS_LEAP_YEAR(year) && day == days_in_year[3]) {
month = 2;
} else {
if (IS_LEAP_YEAR(year) && day > days_in_year[3])
day--;
for (month = 1; month < 12; month++) {
if (days_in_year[month + 1] > day)
break;
}
}
day -= days_in_year[month];
tp->Second = second % SECS_PER_MIN;
tp->Minute = (second / SECS_PER_MIN) % 60;
tp->Hour = (second / SECS_PER_HOUR) % 24;
tp->Day = day + 1;
tp->Month = month;
tp->Year = year;
}
static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
static int exfat_generic_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
#else
static long exfat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
#endif
static int exfat_sync_inode(struct inode *inode);
static struct inode *exfat_build_inode(struct super_block *sb, FILE_ID_T *fid, loff_t i_pos);
static void exfat_detach(struct inode *inode);
static void exfat_attach(struct inode *inode, loff_t i_pos);
static inline unsigned long exfat_hash(loff_t i_pos);
static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
static void exfat_write_super(struct super_block *sb);
/*======================================================================*/
/* Directory Entry Operations */
/*======================================================================*/
static int exfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
{
struct inode *inode = filp->f_path.dentry->d_inode;
struct super_block *sb = inode->i_sb;
struct exfat_sb_info *sbi = EXFAT_SB(sb);
FS_INFO_T *p_fs = &(sbi->fs_info);
BD_INFO_T *p_bd = &(EXFAT_SB(sb)->bd_info);
DIR_ENTRY_T de;
unsigned long inum;
loff_t cpos;
int err = 0;
mutex_lock(&sbi->s_lock);
cpos = filp->f_pos;
/* Fake . and .. for the root directory. */
if ((p_fs->vol_type == EXFAT) || (inode->i_ino == EXFAT_ROOT_INO)) {
while (cpos < 2) {
if (inode->i_ino == EXFAT_ROOT_INO)
inum = EXFAT_ROOT_INO;
else if (cpos == 0)
inum = inode->i_ino;
else /* (cpos == 1) */
inum = parent_ino(filp->f_path.dentry);
if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
goto out;
cpos++;
filp->f_pos++;
}
if (cpos == 2) {
cpos = 0;
}
}
if (cpos & (DENTRY_SIZE - 1)) {
err = -ENOENT;
goto out;
}
get_new:
EXFAT_I(inode)->fid.size = i_size_read(inode);
EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
err = FsReadDir(inode, &de);
if (err) {
// at least we tried to read a sector
// move cpos to next sector position (should be aligned)
if (err == FFS_MEDIAERR) {
cpos += 1 << p_bd->sector_size_bits;
cpos &= ~((1 << p_bd->sector_size_bits)-1);
}
err = -EIO;
goto end_of_dir;
}
cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
if (!de.Name[0])
goto end_of_dir;
if (!memcmp(de.ShortName, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
inum = inode->i_ino;
} else if (!memcmp(de.ShortName, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH)) {
inum = parent_ino(filp->f_path.dentry);
} else {
loff_t i_pos = ((loff_t) EXFAT_I(inode)->fid.start_clu << 32) |
((EXFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
struct inode *tmp = exfat_iget(sb, i_pos);
if (tmp) {
inum = tmp->i_ino;
iput(tmp);
} else {
inum = iunique(sb, EXFAT_ROOT_INO);
}
}
if (filldir(dirent, de.Name, strlen(de.Name), cpos-1, inum,
(de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
goto out;
filp->f_pos = cpos;
goto get_new;
end_of_dir:
filp->f_pos = cpos;
out:
mutex_unlock(&sbi->s_lock);
return err;
}
static int exfat_ioctl_volume_id(struct inode *dir)
{
struct super_block *sb = dir->i_sb;
struct exfat_sb_info *sbi = EXFAT_SB(sb);
FS_INFO_T *p_fs = &(sbi->fs_info);
return p_fs->vol_id;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
static int exfat_generic_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
#else
static long exfat_generic_ioctl(struct file *filp,
unsigned int cmd, unsigned long arg)
#endif
{
#if EXFAT_CONFIG_KERNEL_DEBUG
#if !(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
struct inode *inode = filp->f_dentry->d_inode;
#endif
unsigned int flags;
#endif /* EXFAT_CONFIG_KERNEL_DEBUG */
switch (cmd) {
case EXFAT_IOCTL_GET_VOLUME_ID:
return exfat_ioctl_volume_id(inode);
#if EXFAT_CONFIG_KERNEL_DEBUG
case EXFAT_IOC_GET_DEBUGFLAGS: {
struct super_block *sb = inode->i_sb;
struct exfat_sb_info *sbi = EXFAT_SB(sb);
flags = sbi->debug_flags;
return put_user(flags, (int __user *)arg);
}
case EXFAT_IOC_SET_DEBUGFLAGS: {
struct super_block *sb = inode->i_sb;
struct exfat_sb_info *sbi = EXFAT_SB(sb);
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (get_user(flags, (int __user *) arg))
return -EFAULT;
mutex_lock(&sbi->s_lock);
sbi->debug_flags = flags;
mutex_unlock(&sbi->s_lock);
return 0;
}
#endif /* EXFAT_CONFIG_KERNEL_DEBUG */
default:
return -ENOTTY; /* Inappropriate ioctl for device */
}
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
static int exfat_file_fsync(struct file *filp, int datasync)
{
struct inode *inode = filp->f_mapping->host;
struct super_block *sb = inode->i_sb;
int res, err;
res = generic_file_fsync(filp, datasync);
err = FsSyncVol(sb, 1);
return res ? res : err;
}
#endif
const struct file_operations exfat_dir_operations = {
.llseek = generic_file_llseek,
.read = generic_read_dir,
.readdir = exfat_readdir,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
.ioctl = exfat_generic_ioctl,
.fsync = exfat_file_fsync,
#else
.unlocked_ioctl = exfat_generic_ioctl,
.fsync = generic_file_fsync,
#endif
};
static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
bool excl)
{
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct timespec ts;
FILE_ID_T fid;
loff_t i_pos;
int err;
mutex_lock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_create entered\n");
ts = CURRENT_TIME_SEC;
err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_REGULAR, &fid);
if (err) {
if (err == FFS_INVALIDPATH)
err = -EINVAL;
else if (err == FFS_FILEEXIST)
err = -EEXIST;
else if (err == FFS_FULL)
err = -ENOSPC;
else if (err == FFS_NAMETOOLONG)
err = -ENAMETOOLONG;
else
err = -EIO;
goto out;
}
dir->i_version++;
dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
if (IS_DIRSYNC(dir))
(void) exfat_sync_inode(dir);
else
mark_inode_dirty(dir);
i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
inode = exfat_build_inode(sb, &fid, i_pos);
if (IS_ERR(inode)) {
err = PTR_ERR(inode);
goto out;
}
inode->i_version++;
inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
/* timestamp is already written, so mark_inode_dirty() is unnecessary. */
dentry->d_time = dentry->d_parent->d_inode->i_version;
d_instantiate(dentry, inode);
out:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_create exited\n");
return err;
}
static int exfat_find(struct inode *dir, struct qstr *qname,
FILE_ID_T *fid)
{
int err;
if (qname->len == 0)
return -ENOENT;
err = FsLookupFile(dir, (UINT8 *) qname->name, fid);
if (err)
return -ENOENT;
return 0;
}
static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags) {
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct dentry *alias;
FILE_ID_T fid;
loff_t i_pos;
int err;
UINT64 ret;
mode_t i_mode;
mutex_lock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_lookup entered\n");
err = exfat_find(dir, &dentry->d_name, &fid);
if (err) {
if (err == -ENOENT) {
inode = NULL;
goto out;
}
goto error;
}
i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
inode = exfat_build_inode(sb, &fid, i_pos);
if (IS_ERR(inode)) {
err = PTR_ERR(inode);
goto error;
}
i_mode = inode->i_mode;
if (S_ISLNK(i_mode)) {
EXFAT_I(inode)->target = MALLOC(i_size_read(inode)+1);
if (!EXFAT_I(inode)->target) {
err = -ENOMEM;
goto error;
}
FsReadFile(dir, &fid, EXFAT_I(inode)->target, i_size_read(inode), &ret);
*(EXFAT_I(inode)->target + i_size_read(inode)) = '\0';
}
alias = d_find_alias(inode);
if (!alias)
goto out;
if (!(alias->d_flags &DCACHE_DISCONNECTED)) {
BUG_ON(d_unhashed(alias));
if (!S_ISDIR(i_mode))
d_move(alias, dentry);
iput(inode);
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_lookup exited 1\n");
return alias;
}
out:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
dentry->d_time = dentry->d_parent->d_inode->i_version;
dentry->d_op = sb->s_root->d_op;
dentry = d_splice_alias(inode, dentry);
if (dentry) {
dentry->d_op = sb->s_root->d_op;
dentry->d_time = dentry->d_parent->d_inode->i_version;
}
PRINTK("exfat_lookup exited 2\n");
return dentry;
error:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_lookup exited 3\n");
return ERR_PTR(err);
}
static int exfat_unlink(struct inode *dir, struct dentry *dentry)
{
struct inode *inode = dentry->d_inode;
struct super_block *sb = dir->i_sb;
struct timespec ts;
int err;
mutex_lock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_unlink entered\n");
ts = CURRENT_TIME_SEC;
EXFAT_I(inode)->fid.size = i_size_read(inode);
err = FsRemoveFile(dir, &(EXFAT_I(inode)->fid));
if (err) {
if (err == FFS_PERMISSIONERR)
err = -EPERM;
else
err = -EIO;
goto out;
}
dir->i_version++;
dir->i_mtime = dir->i_atime = ts;
if (IS_DIRSYNC(dir))
(void) exfat_sync_inode(dir);
else
mark_inode_dirty(dir);
clear_nlink(inode);
inode->i_mtime = inode->i_atime = ts;
exfat_detach(inode);
/*
{
struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
spin_lock(&sbi->inode_hash_lock);
EXFAT_I(inode)->i_pos = 0;
hlist_del_init(&EXFAT_I(inode)->i_fat_hash);
spin_unlock(&sbi->inode_hash_lock);
}
*/
remove_inode_hash(inode);
out:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_unlink exited\n");
return err;
}
static int exfat_symlink(struct inode *dir, struct dentry *dentry, const char *target)
{
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct timespec ts;
FILE_ID_T fid;
loff_t i_pos;
int err;
UINT64 len = (UINT64) strlen(target);
UINT64 ret;
mutex_lock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_symlink entered\n");
ts = CURRENT_TIME_SEC;
err = FsCreateFile(dir, (UINT8 *) dentry->d_name.name, FM_SYMLINK, &fid);
if (err) {
if (err == FFS_INVALIDPATH)
err = -EINVAL;
else if (err == FFS_FILEEXIST)
err = -EEXIST;
else if (err == FFS_FULL)
err = -ENOSPC;
else
err = -EIO;
goto out;
}
err = FsWriteFile(dir, &fid, (char *) target, len, &ret);
if (err) {
FsRemoveFile(dir, &fid);
if (err == FFS_FULL)
err = -ENOSPC;
else
err = -EIO;
goto out;
}
dir->i_version++;
dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
if (IS_DIRSYNC(dir))
(void) exfat_sync_inode(dir);
else
mark_inode_dirty(dir);
i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
inode = exfat_build_inode(sb, &fid, i_pos);
if (IS_ERR(inode)) {
err = PTR_ERR(inode);
goto out;
}
inode->i_version++;
inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
/* timestamp is already written, so mark_inode_dirty() is unneeded. */
EXFAT_I(inode)->target = MALLOC(len+1);
if (!EXFAT_I(inode)->target) {
err = -ENOMEM;
goto out;
}
MEMCPY(EXFAT_I(inode)->target, target, len+1);
dentry->d_time = dentry->d_parent->d_inode->i_version;
d_instantiate(dentry, inode);
out:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_symlink exited\n");
return err;
}
static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct timespec ts;
FILE_ID_T fid;
loff_t i_pos;
int err;
mutex_lock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_mkdir entered\n");
ts = CURRENT_TIME_SEC;
err = FsCreateDir(dir, (UINT8 *) dentry->d_name.name, &fid);
if (err) {
if (err == FFS_INVALIDPATH)
err = -EINVAL;
else if (err == FFS_FILEEXIST)
err = -EEXIST;
else if (err == FFS_FULL)
err = -ENOSPC;
else if (err == FFS_NAMETOOLONG)
err = -ENAMETOOLONG;
else
err = -EIO;
goto out;
}
dir->i_version++;
dir->i_ctime = dir->i_mtime = dir->i_atime = ts;
if (IS_DIRSYNC(dir))
(void) exfat_sync_inode(dir);
else
mark_inode_dirty(dir);
inc_nlink(dir);
i_pos = ((loff_t) fid.dir.dir << 32) | (fid.entry & 0xffffffff);
inode = exfat_build_inode(sb, &fid, i_pos);
if (IS_ERR(inode)) {
err = PTR_ERR(inode);
goto out;
}
inode->i_version++;
inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
/* timestamp is already written, so mark_inode_dirty() is unneeded. */
dentry->d_time = dentry->d_parent->d_inode->i_version;
d_instantiate(dentry, inode);
out:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_mkdir exited\n");
return err;
}
static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
{
struct inode *inode = dentry->d_inode;
struct super_block *sb = dir->i_sb;
struct timespec ts;
int err;
mutex_lock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_rmdir entered\n");
ts = CURRENT_TIME_SEC;
EXFAT_I(inode)->fid.size = i_size_read(inode);
err = FsRemoveDir(dir, &(EXFAT_I(inode)->fid));
if (err) {
if (err == FFS_INVALIDPATH)
err = -EINVAL;
else if (err == FFS_FILEEXIST)
err = -ENOTEMPTY;
else if (err == FFS_NOTFOUND)
err = -ENOENT;
else if (err == FFS_DIRBUSY)
err = -EBUSY;
else
err = -EIO;
goto out;
}
dir->i_version++;
dir->i_mtime = dir->i_atime = ts;
if (IS_DIRSYNC(dir))
(void) exfat_sync_inode(dir);
else
mark_inode_dirty(dir);
drop_nlink(dir);
clear_nlink(inode);
inode->i_mtime = inode->i_atime = ts;
exfat_detach(inode);
/*
{
struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
spin_lock(&sbi->inode_hash_lock);
EXFAT_I(inode)->i_pos = 0;
hlist_del_init(&EXFAT_I(inode)->i_fat_hash);
spin_unlock(&sbi->inode_hash_lock);
}
*/
remove_inode_hash(inode);
out:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_rmdir exited\n");
return err;
}
static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
struct inode *old_inode, *new_inode;
struct super_block *sb = old_dir->i_sb;
struct timespec ts;
loff_t i_pos;
int err;
mutex_lock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_rename entered\n");
old_inode = old_dentry->d_inode;
new_inode = new_dentry->d_inode;
ts = CURRENT_TIME_SEC;
EXFAT_I(old_inode)->fid.size = i_size_read(old_inode);
err = FsMoveFile(old_dir, &(EXFAT_I(old_inode)->fid), new_dir, new_dentry);
if (err) {
if (err == FFS_PERMISSIONERR)
err = -EPERM;
else if (err == FFS_INVALIDPATH)
err = -EINVAL;
else if (err == FFS_FILEEXIST)
err = -EEXIST;
else if (err == FFS_NOTFOUND)
err = -ENOENT;
else if (err == FFS_FULL)
err = -ENOSPC;
else
err = -EIO;
goto out;
}
new_dir->i_version++;
new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime = ts;
if (IS_DIRSYNC(new_dir))
(void) exfat_sync_inode(new_dir);
else
mark_inode_dirty(new_dir);
i_pos = ((loff_t) EXFAT_I(old_inode)->fid.dir.dir << 32) |
(EXFAT_I(old_inode)->fid.entry & 0xffffffff);
exfat_detach(old_inode);
/*
{
struct exfat_sb_info *sbi = EXFAT_SB(old_inode->i_sb);
spin_lock(&sbi->inode_hash_lock);
EXFAT_I(old_inode)->i_pos = 0;
hlist_del_init(&EXFAT_I(old_inode)->i_fat_hash);
spin_unlock(&sbi->inode_hash_lock);
}
*/
exfat_attach(old_inode, i_pos);
/*
{
struct exfat_sb_info *sbi = EXFAT_SB(old_inode->i_sb);
struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
spin_lock(&sbi->inode_hash_lock);
EXFAT_I(old_inode)->i_pos = i_pos;
hlist_add_head(&EXFAT_I(old_inode)->i_fat_hash, head);
spin_unlock(&sbi->inode_hash_lock);
}
*/
if (IS_DIRSYNC(new_dir))
(void) exfat_sync_inode(old_inode);
else
mark_inode_dirty(old_inode);
if ((S_ISDIR(old_inode->i_mode)) && (old_dir != new_dir)) {
drop_nlink(old_dir);
if (!new_inode) inc_nlink(new_dir);
}
old_dir->i_version++;
old_dir->i_ctime = old_dir->i_mtime = ts;
if (IS_DIRSYNC(old_dir))
(void) exfat_sync_inode(old_dir);
else
mark_inode_dirty(old_dir);
if (new_inode) {
exfat_detach(new_inode);
/*
{
struct exfat_sb_info *sbi = EXFAT_SB(new_inode->i_sb);
spin_lock(&sbi->inode_hash_lock);
EXFAT_I(new_inode)->i_pos = 0;
hlist_del_init(&EXFAT_I(new_inode)->i_fat_hash);
spin_unlock(&sbi->inode_hash_lock);
}
*/
drop_nlink(new_inode);
if (S_ISDIR(new_inode->i_mode))
drop_nlink(new_inode);
new_inode->i_ctime = ts;
//(void) exfat_sync_inode(new_inode);
}
out:
mutex_unlock(&EXFAT_SB(sb)->s_lock);
PRINTK("exfat_rename exited\n");
return err;
}
static int exfat_cont_expand(struct inode *inode, loff_t size)
{
struct address_space *mapping = inode->i_mapping;
loff_t start = i_size_read(inode), count = size - i_size_read(inode);
int err, err2;
if ((err = generic_cont_expand_simple(inode, size)) != 0)
return err;
inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
if (IS_SYNC(inode)) {
err = filemap_fdatawrite_range(mapping, start, start + count - 1);
err2 = sync_mapping_buffers(mapping);
err = (err)?(err):(err2);
err2 = write_inode_now(inode, 1);
err = (err)?(err):(err2);
if (!err) {
err = filemap_fdatawait_range(mapping, start, start + count - 1);
}
}
return err;
}
static int exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
{
mode_t allow_utime = sbi->options.allow_utime;
if (current_fsuid() != inode->i_uid) {
if (in_group_p(inode->i_gid))
allow_utime >>= 3;
if (allow_utime & MAY_WRITE)
return 1;
}
/* use a default check */
return 0;
}
static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
struct inode *inode, umode_t *mode_ptr)
{
mode_t i_mode, mask, perm;
i_mode = inode->i_mode;
if (S_ISREG(i_mode) || S_ISLNK(i_mode))
mask = sbi->options.fs_fmask;
else
mask = sbi->options.fs_dmask;
perm = *mode_ptr & ~(S_IFMT | mask);
/* Of the r and x bits, all (subject to umask) must be present.*/
if ((perm & (S_IRUGO | S_IXUGO)) != (i_mode & (S_IRUGO|S_IXUGO)))
return -EPERM;
if (exfat_mode_can_hold_ro(inode)) {
/* Of the w bits, either all (subject to umask) or none must be present. */
if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
return -EPERM;
} else {
/* If exfat_mode_can_hold_ro(inode) is false, can't change w bits. */
if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
return -EPERM;
}
*mode_ptr &= S_IFMT | perm;
return 0;
}
static int exfat_setattr(struct dentry *dentry, struct iattr *attr)
{
struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
struct inode *inode = dentry->d_inode;
unsigned int ia_valid;
int error;
PRINTK("exfat_setattr entered\n");
if ((attr->ia_valid & ATTR_SIZE)
&& (attr->ia_size > i_size_read(inode))) {
error = exfat_cont_expand(inode, attr->ia_size);
if (error || attr->ia_valid == ATTR_SIZE)
return error;
attr->ia_valid &= ~ATTR_SIZE;
}
ia_valid = attr->ia_valid;
if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET))
&& exfat_allow_set_time(sbi, inode)) {
attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET);
}
error = inode_change_ok(inode, attr);
attr->ia_valid = ia_valid;
if (error) {
return error;
}
if (((attr->ia_valid & ATTR_UID) &&
(attr->ia_uid != sbi->options.fs_uid)) ||
((attr->ia_valid & ATTR_GID) &&
(attr->ia_gid != sbi->options.fs_gid)) ||
((attr->ia_valid & ATTR_MODE) &&
(attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | S_IRWXUGO)))) {
return -EPERM;
}
/*
* We don't return -EPERM here. Yes, strange, but this is too
* old behavior.
*/
if (attr->ia_valid & ATTR_MODE) {
if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
attr->ia_valid &= ~ATTR_MODE;
}
EXFAT_I(inode)->fid.size = i_size_read(inode);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
if (attr->ia_valid)