forked from dorimanx/exfat-nofuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exfat_super.c
2658 lines (2308 loc) · 66.7 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
/* Some of the source code in this file came from "linux/fs/fat/file.c","linux/fs/fat/inode.c" and "linux/fs/fat/misc.c". */
/*
* linux/fs/fat/file.c
*
* Written 1992,1993 by Werner Almesberger
*
* regular file handling primitives for fat-based filesystems
*/
/*
* linux/fs/fat/inode.c
*
* Written 1992,1993 by Werner Almesberger
* VFAT extensions by Gordon Chaffee, merged with msdos fs by Henrik Storner
* Rewritten for the constant inumbers support by Al Viro
*
* Fixes:
*
* Max Cohan: Fixed invalid FSINFO offset when info_sector is 0
*/
/*
* linux/fs/fat/misc.c
*
* Written 1992,1993 by Werner Almesberger
* 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
* and date_dos2unix for date==0 by Igor Zhbanov([email protected])
*/
/*
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#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>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
#include <linux/aio.h>
#endif
#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>
#include "exfat_version.h"
#include "exfat_config.h"
#include "exfat_data.h"
#include "exfat_oal.h"
#include "exfat_blkdev.h"
#include "exfat_cache.h"
#include "exfat_nls.h"
#include "exfat_api.h"
#include "exfat_core.h"
#include "exfat_super.h"
static struct kmem_cache *exfat_inode_cachep;
static int exfat_default_codepage = CONFIG_EXFAT_DEFAULT_CODEPAGE;
static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
extern struct timezone sys_tz;
#define CHECK_ERR(x) BUG_ON(x)
#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_DECADE (365 * 10 + 2)
/* 120 (2100 - 1980) isn't leap year */
#define NO_LEAP_YEAR_2100 (120)
#define IS_LEAP_YEAR(y) (!((y) & 0x3) && (y) != NO_LEAP_YEAR_2100)
#define SECS_PER_MIN (60)
#define SECS_PER_HOUR (60 * SECS_PER_MIN)
#define SECS_PER_DAY (24 * SECS_PER_HOUR)
#define MAKE_LEAP_YEAR(leap_year, year) \
do { \
if (unlikely(year > NO_LEAP_YEAR_2100)) \
leap_year = ((year + 3) / 4) - 1; \
else \
leap_year = ((year + 3) / 4); \
} while (0)
/* Linear day numbers of the respective 1sts in non-leap years. */
static time_t accum_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, loff_t old_size);
/* 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 year = tp->Year;
time_t ld;
MAKE_LEAP_YEAR(ld, year);
if (IS_LEAP_YEAR(year) && (tp->Month) > 2)
ld++;
ts->tv_sec = tp->Second + tp->Minute * SECS_PER_MIN
+ tp->Hour * SECS_PER_HOUR
+ (year * 365 + ld + accum_days_in_year[(tp->Month)] + (tp->Day - 1) + DAYS_DELTA_DECADE) * 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, month, year;
time_t ld;
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_DECADE;
year = day / 365;
MAKE_LEAP_YEAR(ld, year);
if (year * 365 + ld > day)
year--;
MAKE_LEAP_YEAR(ld, year);
day -= year * 365 + ld;
if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
month = 2;
} else {
if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
day--;
for (month = 1; month < 12; month++) {
if (accum_days_in_year[month + 1] > day)
break;
}
}
day -= accum_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);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
static int exfat_write_inode(struct inode *inode, int wait);
#else
static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
#endif
static void exfat_write_super(struct super_block *sb);
static void __lock_super(struct super_block *sb)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
lock_super(sb);
#else
struct exfat_sb_info *sbi = EXFAT_SB(sb);
mutex_lock(&sbi->s_lock);
#endif
}
static void __unlock_super(struct super_block *sb)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
unlock_super(sb);
#else
struct exfat_sb_info *sbi = EXFAT_SB(sb);
mutex_unlock(&sbi->s_lock);
#endif
}
static int __is_sb_dirty(struct super_block *sb)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
return sb->s_dirt;
#else
struct exfat_sb_info *sbi = EXFAT_SB(sb);
return sbi->s_dirt;
#endif
}
static void __set_sb_clean(struct super_block *sb)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
sb->s_dirt = 0;
#else
struct exfat_sb_info *sbi = EXFAT_SB(sb);
sbi->s_dirt = 0;
#endif
}
static int __exfat_revalidate(struct dentry *dentry)
{
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
#else
static int exfat_revalidate(struct dentry *dentry, struct nameidata *nd)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
if (flags & LOOKUP_RCU)
return -ECHILD;
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
if (nd && nd->flags & LOOKUP_RCU)
return -ECHILD;
#endif
if (dentry->d_inode)
return 1;
return __exfat_revalidate(dentry);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
#else
static int exfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
if (flags & LOOKUP_RCU)
return -ECHILD;
#else
unsigned int flags;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
if (nd && nd->flags & LOOKUP_RCU)
return -ECHILD;
#endif
flags = nd ? nd->flags : 0;
#endif
if (dentry->d_inode)
return 1;
if (!flags)
return 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,00)
if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
return 0;
#else
if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) {
if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
return 0;
}
#endif
return __exfat_revalidate(dentry);
}
static unsigned int __exfat_striptail_len(unsigned int len, const char *name)
{
while (len && name[len - 1] == '.')
len--;
return len;
}
static unsigned int exfat_striptail_len(const struct qstr *qstr)
{
return __exfat_striptail_len(qstr->len, qstr->name);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
static int exfat_d_hash(struct dentry *dentry, struct qstr *qstr)
#else
static int exfat_d_hash(const struct dentry *dentry, const struct inode *inode,
struct qstr *qstr)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
qstr->hash = full_name_hash(dentry, qstr->name, exfat_striptail_len(qstr));
#else
qstr->hash = full_name_hash(qstr->name, exfat_striptail_len(qstr));
#endif
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
static int exfat_d_hashi(struct dentry *dentry, struct qstr *qstr)
#else
static int exfat_d_hashi(const struct dentry *dentry, const struct inode *inode,
struct qstr *qstr)
#endif
{
struct super_block *sb = dentry->d_sb;
const unsigned char *name;
unsigned int len;
unsigned long hash;
name = qstr->name;
len = exfat_striptail_len(qstr);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
hash = init_name_hash(dentry);
#else
hash = init_name_hash();
#endif
while (len--)
hash = partial_name_hash(nls_upper(sb, *name++), hash);
qstr->hash = end_name_hash(hash);
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
static int exfat_cmpi(const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
static int exfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
static int exfat_cmpi(struct dentry *parent, struct qstr *a, struct qstr *b)
#else
static int exfat_cmpi(const struct dentry *parent, const struct inode *pinode,
const struct dentry *dentry, const struct inode *inode,
unsigned int len, const char *str, const struct qstr *name)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
struct nls_table *t = EXFAT_SB(dentry->d_sb)->nls_io;
#else
struct nls_table *t = EXFAT_SB(parent->d_sb)->nls_io;
#endif
unsigned int alen, blen;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
alen = exfat_striptail_len(a);
blen = exfat_striptail_len(b);
#else
alen = exfat_striptail_len(name);
blen = __exfat_striptail_len(len, str);
#endif
if (alen == blen) {
if (t == NULL) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
if (strncasecmp(a->name, b->name, alen) == 0)
#else
if (strncasecmp(name->name, str, alen) == 0)
#endif
return 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
} else if (nls_strnicmp(t, a->name, b->name, alen) == 0)
#else
} else if (nls_strnicmp(t, name->name, str, alen) == 0)
#endif
return 0;
}
return 1;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,8,0)
static int exfat_cmp(const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
static int exfat_cmp(const struct dentry *parent, const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
static int exfat_cmp(struct dentry *parent, struct qstr *a,
struct qstr *b)
#else
static int exfat_cmp(const struct dentry *parent, const struct inode *pinode,
const struct dentry *dentry, const struct inode *inode,
unsigned int len, const char *str, const struct qstr *name)
#endif
{
unsigned int alen, blen;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
alen = exfat_striptail_len(a);
blen = exfat_striptail_len(b);
#else
alen = exfat_striptail_len(name);
blen = __exfat_striptail_len(len, str);
#endif
if (alen == blen) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
if (strncmp(a->name, b->name, alen) == 0)
#else
if (strncmp(name->name, str, alen) == 0)
#endif
return 0;
}
return 1;
}
static const struct dentry_operations exfat_ci_dentry_ops = {
.d_revalidate = exfat_revalidate_ci,
.d_hash = exfat_d_hashi,
.d_compare = exfat_cmpi,
};
static const struct dentry_operations exfat_dentry_ops = {
.d_revalidate = exfat_revalidate,
.d_hash = exfat_d_hash,
.d_compare = exfat_cmp,
};
/*======================================================================*/
/* Directory Entry Operations */
/*======================================================================*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
static int exfat_readdir(struct file *filp, struct dir_context *ctx)
#else
static int exfat_readdir(struct file *filp, void *dirent, filldir_t filldir)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
struct inode *inode = file_inode(filp);
#else
struct inode *inode = filp->f_path.dentry->d_inode;
#endif
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;
__lock_super(sb);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
cpos = ctx->pos;
#else
cpos = filp->f_pos;
#endif
/* 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 LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
if (!dir_emit_dots(filp, ctx))
#else
if (filldir(dirent, "..", cpos+1, cpos, inum, DT_DIR) < 0)
#endif
goto out;
cpos++;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
ctx->pos++;
#else
filp->f_pos++;
#endif
}
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 LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
if (!dir_emit(ctx, de.Name, strlen(de.Name), inum,
(de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
#else
if (filldir(dirent, de.Name, strlen(de.Name), cpos-1, inum,
(de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
#endif
goto out;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
ctx->pos = cpos;
#else
filp->f_pos = cpos;
#endif
goto get_new;
end_of_dir:
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
ctx->pos = cpos;
#else
filp->f_pos = cpos;
#endif
out:
__unlock_super(sb);
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 !(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36))
#if !(LINUX_VERSION_CODE < KERNEL_VERSION(3,18,3))
struct inode *inode = filp->f_path.dentry->d_inode;
#else
struct inode *inode = filp->f_dentry->d_inode;
#endif
#endif
#ifdef CONFIG_EXFAT_KERNEL_DEBUG
unsigned int flags;
#endif /* CONFIG_EXFAT_KERNEL_DEBUG */
switch (cmd) {
case EXFAT_IOCTL_GET_VOLUME_ID:
return exfat_ioctl_volume_id(inode);
#ifdef CONFIG_EXFAT_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;
__lock_super(sb);
sbi->debug_flags = flags;
__unlock_super(sb);
return 0;
}
#endif /* CONFIG_EXFAT_KERNEL_DEBUG */
default:
return -ENOTTY; /* Inappropriate ioctl for device */
}
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
static int exfat_file_fsync(struct file *filp, struct dentry *dentry,
int datasync)
#else
static int exfat_file_fsync(struct file *filp, int datasync)
#endif
{
struct inode *inode = filp->f_mapping->host;
struct super_block *sb = inode->i_sb;
int res, err;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
res = simple_fsync(filp, dentry, datasync);
#else
res = generic_file_fsync(filp, datasync);
#endif
err = FsSyncVol(sb, 1);
return res ? res : err;
}
#endif
const struct file_operations exfat_dir_operations = {
.llseek = generic_file_llseek,
.read = generic_read_dir,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,11,0)
.iterate = exfat_readdir,
#else
.readdir = exfat_readdir,
#endif
#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
};
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
bool excl)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0)
static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
struct nameidata *nd)
#else
static int exfat_create(struct inode *dir, struct dentry *dentry, int mode,
struct nameidata *nd)
#endif
{
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct timespec ts;
FILE_ID_T fid;
loff_t i_pos;
int err;
__lock_super(sb);
DPRINTK("exfat_create entered\n");
ts = CURRENT_TIME_SEC;
err = FsCreateFile(dir, (u8 *) 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:
__unlock_super(sb);
DPRINTK("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, (u8 *) qname->name, fid);
if (err)
return -ENOENT;
return 0;
}
static int exfat_d_anon_disconn(struct dentry *dentry)
{
return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,00)
static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags)
#else
static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *nd)
#endif
{
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct dentry *alias;
int err;
FILE_ID_T fid;
loff_t i_pos;
u64 ret;
mode_t i_mode;
__lock_super(sb);
DPRINTK("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 = kmalloc(i_size_read(inode)+1, GFP_KERNEL);
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 && !exfat_d_anon_disconn(alias)) {
CHECK_ERR(d_unhashed(alias));
if (!S_ISDIR(i_mode))
d_move(alias, dentry);
iput(inode);
__unlock_super(sb);
DPRINTK("exfat_lookup exited 1\n");
return alias;
} else {
dput(alias);
}
out:
__unlock_super(sb);
dentry->d_time = dentry->d_parent->d_inode->i_version;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38)
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;
}
#else
dentry = d_splice_alias(inode, dentry);
if (dentry)
dentry->d_time = dentry->d_parent->d_inode->i_version;
#endif
DPRINTK("exfat_lookup exited 2\n");
return dentry;
error:
__unlock_super(sb);
DPRINTK("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;
__lock_super(sb);
DPRINTK("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);
remove_inode_hash(inode);
out:
__unlock_super(sb);
DPRINTK("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;
u64 len = (u64) strlen(target);
u64 ret;
__lock_super(sb);
DPRINTK("exfat_symlink entered\n");
ts = CURRENT_TIME_SEC;
err = FsCreateFile(dir, (u8 *) 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 = kmalloc(len+1, GFP_KERNEL);
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:
__unlock_super(sb);
DPRINTK("exfat_symlink exited\n");
return err;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,3,0)
static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
#else
static int exfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
#endif
{