This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
super.c
3873 lines (3233 loc) · 97.5 KB
/
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
*
* super.c: exFAT glue layer for supporting VFS
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/slab.h>
#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 <linux/bio.h>
#include <linux/blkdev.h>
#include <linux/swap.h> /* for mark_page_accessed() */
#include <linux/vmalloc.h>
#include <linux/mutex.h>
#include <asm/current.h>
#include <asm/unaligned.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
#include <linux/aio.h>
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)
#error EXFAT only supports linux kernel version 3.0 or higher
#endif
#include "version.h"
#include "config.h"
#include "exfat.h"
#include "core.h"
/* skip iterating emit_dots when dir is empty */
#define ITER_POS_FILLED_DOTS (2)
static struct kset *exfat_kset;
static struct kmem_cache *exfat_inode_cachep;
static DEFINE_MUTEX(_lock_core);
static int exfat_default_codepage = CONFIG_EXFAT_DEFAULT_CODEPAGE;
static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
static const char exfat_iocharset_with_utf8[] = "iso8859-1";
static void exfat_truncate(struct inode *inode, loff_t old_size);
static int exfat_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create);
static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos);
static struct inode *exfat_build_inode(struct super_block *sb, const 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 s32 __exfat_sync_fs(struct super_block *sb, s32 do_sync);
static int __exfat_write_inode(struct inode *inode, int sync);
static int exfat_sync_inode(struct inode *inode);
static int exfat_write_inode(struct inode *inode, struct writeback_control *wbc);
static void exfat_write_super(struct super_block *sb);
static void exfat_write_failed(struct address_space *mapping, loff_t to);
static void exfat_init_namebuf(DENTRY_NAMEBUF_T *nb);
static int exfat_alloc_namebuf(DENTRY_NAMEBUF_T *nb);
static void exfat_free_namebuf(DENTRY_NAMEBUF_T *nb);
static int __exfat_getattr(struct inode *inode, struct kstat *stat);
static void __exfat_writepage_end_io(struct bio *bio, int err);
static inline void lock_super(struct super_block *sb);
static inline void unlock_super(struct super_block *sb);
static int exfat_create_compat(struct inode *dir, struct dentry *dentry);
static int __exfat_revalidate(struct dentry *dentry);
static int __exfat_revalidate_ci(struct dentry *dentry, unsigned int flags);
static int __exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync);
static struct dentry *__exfat_lookup(struct inode *dir, struct dentry *dentry);
static int __exfat_mkdir(struct inode *dir, struct dentry *dentry);
static int __exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry);
static int __exfat_show_options(struct seq_file *m, struct super_block *sb);
static inline ssize_t __exfat_blkdev_direct_IO(int rw, struct kiocb *iocb,
struct inode *inode, void *iov_u, loff_t offset,
unsigned long nr_segs);
static inline ssize_t __exfat_direct_IO(int rw, struct kiocb *iocb,
struct inode *inode, void *iov_u, loff_t offset,
loff_t count, unsigned long nr_segs);
static int __exfat_d_hash(const struct dentry *dentry, struct qstr *qstr);
static int __exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr);
static int __exfat_cmp(const struct dentry *dentry, unsigned int len,
const char *str, const struct qstr *name);
static int __exfat_cmpi(const struct dentry *dentry, unsigned int len,
const char *str, const struct qstr *name);
/* mount the file system volume */
static s32 exfat_mount(struct super_block *sb)
{
s32 err;
/* acquire the core lock for file system ccritical section */
mutex_lock(&_lock_core);
err = exfat_meta_cache_init(sb);
if (err)
goto out;
err = exfat_fscore_mount(sb);
out:
if (err)
exfat_meta_cache_shutdown(sb);
/* release the core lock for file system critical section */
mutex_unlock(&_lock_core);
return err;
}
/* unmount the file system volume */
static s32 exfat_umount(struct super_block *sb)
{
s32 err;
/* acquire the core lock for file system ccritical section */
mutex_lock(&_lock_core);
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_umount(sb);
exfat_meta_cache_shutdown(sb);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
/* release the core lock for file system critical section */
mutex_unlock(&_lock_core);
return err;
}
static s32 exfat_set_vol_flags(struct super_block *sb, u16 new_flag, s32 always_sync)
{
s32 err;
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_set_vol_flags(sb, new_flag, always_sync);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return err;
}
/* read the target string of symlink */
static s32 exfat_read_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *rcount)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid && buffer);
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_read_link(inode, fid, buffer, count, rcount);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return err;
}
/* write the target string of symlink */
static s32 exfat_write_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *wcount)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid && buffer);
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_write_link(inode, fid, buffer, count, wcount);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return err;
}
/* remove a file */
static s32 exfat_remove(struct inode *inode, FILE_ID_T *fid)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid);
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_remove(inode, fid);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return err;
}
/* get the information of a given file */
static s32 exfat_read_inode(struct inode *inode, DIR_ENTRY_T *info)
{
s32 err;
struct super_block *sb = inode->i_sb;
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_read_inode(inode, info);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return err;
}
/* return the cluster number in the given cluster offset */
static s32 exfat_map_clus(struct inode *inode, u32 clu_offset, u32 *clu, int dest)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(clu);
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_map_clus(inode, clu_offset, clu, dest);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return err;
}
/* read a directory entry from the opened directory */
static s32 __exfat_readdir(struct inode *inode, DIR_ENTRY_T *dir_entry)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(dir_entry);
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
err = exfat_fscore_readdir(inode, dir_entry);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return err;
}
/* reflect the internal dirty flags to VFS bh dirty flags */
static s32 exfat_cache_flush(struct super_block *sb, int do_sync)
{
mutex_lock(&(EXFAT_SB(sb)->s_vlock));
exfat_fcache_flush(sb, do_sync);
exfat_dcache_flush(sb, do_sync);
mutex_unlock(&(EXFAT_SB(sb)->s_vlock));
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
/* EMPTY */
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0) */
static inline void bio_set_dev(struct bio *bio, struct block_device *bdev)
{
bio->bi_bdev = bdev;
}
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,9,0)
#define current_time(x) CURRENT_TIME_SEC
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
static int exfat_getattr(const struct path *path, struct kstat *stat,
u32 request_mask, unsigned int query_flags)
{
struct inode *inode = d_backing_inode(path->dentry);
return __exfat_getattr(inode, stat);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) */
static int exfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
{
struct inode *inode = dentry->d_inode;
return __exfat_getattr(inode, stat);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
static inline void __exfat_clean_bdev_aliases(struct block_device *bdev, sector_t block)
{
clean_bdev_aliases(bdev, block, 1);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0) */
static inline void __exfat_clean_bdev_aliases(struct block_device *bdev, sector_t block)
{
unmap_underlying_metadata(bdev, block);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry,
unsigned int flags)
{
/*
* The VFS already checks for existence, so for local filesystems
* the RENAME_NOREPLACE implementation is equivalent to plain rename.
* Don't support any other flags
*/
if (flags & ~RENAME_NOREPLACE)
return -EINVAL;
return __exfat_rename(old_dir, old_dentry, new_dir, new_dentry);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) */
static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
return __exfat_rename(old_dir, old_dentry, new_dir, new_dentry);
}
// setattr_prepare() was backported to several LTS kernels
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 39) && \
LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
#define SETATTR_PREPARE_AVAILABLE
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 37) && \
LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
#define SETATTR_PREPARE_AVAILABLE
#endif
#ifndef SETATTR_PREPARE_AVAILABLE
static int setattr_prepare(struct dentry *dentry, struct iattr *attr)
{
struct inode *inode = dentry->d_inode;
return inode_change_ok(inode, attr);
}
#endif
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
static inline void __exfat_submit_bio_write(struct bio *bio)
{
bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
submit_bio(bio);
}
static inline unsigned int __exfat_full_name_hash(const struct dentry *dentry, const char *name, unsigned int len)
{
return full_name_hash(dentry, name, len);
}
static inline unsigned long __exfat_init_name_hash(const struct dentry *dentry)
{
return init_name_hash(dentry);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0) */
static inline void __exfat_submit_bio_write(struct bio *bio)
{
submit_bio(WRITE, bio);
}
static inline unsigned int __exfat_full_name_hash(const struct dentry *unused, const char *name, unsigned int len)
{
return full_name_hash(name, len);
}
static inline unsigned long __exfat_init_name_hash(const struct dentry *unused)
{
return init_name_hash();
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 21)
/* EMPTY */
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 21) */
static inline void inode_lock(struct inode *inode)
{
mutex_lock(&inode->i_mutex);
}
static inline void inode_unlock(struct inode *inode)
{
mutex_unlock(&inode->i_mutex);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
static inline int exfat_remount_syncfs(struct super_block *sb)
{
sync_filesystem(sb);
return 0;
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0) */
static inline int exfat_remount_syncfs(struct super_block *sb)
{
/*
* We don`t need to call sync_filesystem(sb),
* Because VFS calls it.
*/
return 0;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
static inline sector_t __exfat_bio_sector(struct bio *bio)
{
return bio->bi_iter.bi_sector;
}
static inline void __exfat_set_bio_iterate(struct bio *bio, sector_t sector,
unsigned int size, unsigned int idx, unsigned int done)
{
struct bvec_iter *iter = &(bio->bi_iter);
iter->bi_sector = sector;
iter->bi_size = size;
iter->bi_idx = idx;
iter->bi_bvec_done = done;
}
static void __exfat_truncate_pagecache(struct inode *inode,
loff_t to, loff_t newsize)
{
truncate_pagecache(inode, newsize);
}
static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
{
return __exfat_d_hash(dentry, qstr);
}
static int exfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
{
return __exfat_d_hashi(dentry, qstr);
}
//instead of exfat_readdir
static int exfat_iterate(struct file *filp, struct dir_context *ctx)
{
struct inode *inode = filp->f_path.dentry->d_inode;
struct super_block *sb = inode->i_sb;
DIR_ENTRY_T de;
DENTRY_NAMEBUF_T *nb = &(de.NameBuf);
unsigned long inum;
loff_t cpos;
int err = 0, fake_offset = 0;
exfat_init_namebuf(nb);
lock_super(sb);
cpos = ctx->pos;
if (!dir_emit_dots(filp, ctx))
goto out;
if (ctx->pos == ITER_POS_FILLED_DOTS) {
cpos = 0;
fake_offset = 1;
}
if (cpos & (DENTRY_SIZE - 1)) {
err = -ENOENT;
goto out;
}
/* name buffer should be allocated before use */
err = exfat_alloc_namebuf(nb);
if (err)
goto out;
get_new:
EXFAT_I(inode)->fid.size = i_size_read(inode);
EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
if (cpos >= EXFAT_I(inode)->fid.size)
goto end_of_dir;
err = __exfat_readdir(inode, &de);
if (err) {
// at least we tried to read a sector
// move cpos to next sector position (should be aligned)
if (err == -EIO) {
cpos += 1 << (sb->s_blocksize_bits);
cpos &= ~((u32)sb->s_blocksize-1);
}
err = -EIO;
goto end_of_dir;
}
cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
if (!nb->lfn[0])
goto end_of_dir;
if (!memcmp(nb->sfn, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
inum = inode->i_ino;
} else if (!memcmp(nb->sfn, 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);
}
}
/* Before calling dir_emit(), sb_lock should be released.
* Because page fault can occur in dir_emit() when the size of buffer given
* from user is larger than one page size
*/
unlock_super(sb);
if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
(de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
goto out_unlocked;
lock_super(sb);
ctx->pos = cpos;
goto get_new;
end_of_dir:
if (!cpos && fake_offset)
cpos = ITER_POS_FILLED_DOTS;
ctx->pos = cpos;
out:
unlock_super(sb);
out_unlocked:
/*
* To improve performance, free namebuf after unlock sb_lock.
* If namebuf is not allocated, this function do nothing
*/
exfat_free_namebuf(nb);
return err;
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) */
static inline sector_t __exfat_bio_sector(struct bio *bio)
{
return bio->bi_sector;
}
static inline void __exfat_set_bio_iterate(struct bio *bio, sector_t sector,
unsigned int size, unsigned int idx, unsigned int done)
{
bio->bi_sector = sector;
bio->bi_idx = idx;
bio->bi_size = size; //PAGE_SIZE;
}
static void __exfat_truncate_pagecache(struct inode *inode,
loff_t to, loff_t newsize)
{
truncate_pagecache(inode, to, newsize);
}
static int exfat_d_hash(const struct dentry *dentry,
const struct inode *inode, struct qstr *qstr)
{
return __exfat_d_hash(dentry, qstr);
}
static int exfat_d_hashi(const struct dentry *dentry,
const struct inode *inode, struct qstr *qstr)
{
return __exfat_d_hashi(dentry, qstr);
}
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 *fsi = &(sbi->fsi);
DIR_ENTRY_T de;
DENTRY_NAMEBUF_T *nb = &(de.NameBuf);
unsigned long inum;
loff_t cpos;
int err = 0, fake_offset = 0;
exfat_init_namebuf(nb);
lock_super(sb);
cpos = filp->f_pos;
/* Fake . and .. for the root directory. */
while (cpos < ITER_POS_FILLED_DOTS) {
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 == ITER_POS_FILLED_DOTS) {
cpos = 0;
fake_offset = 1;
}
if (cpos & (DENTRY_SIZE - 1)) {
err = -ENOENT;
goto out;
}
/* name buffer should be allocated before use */
err = exfat_alloc_namebuf(nb);
if (err)
goto out;
get_new:
EXFAT_I(inode)->fid.size = i_size_read(inode);
EXFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
if (cpos >= EXFAT_I(inode)->fid.size)
goto end_of_dir;
err = __exfat_readdir(inode, &de);
if (err) {
// at least we tried to read a sector
// move cpos to next sector position (should be aligned)
if (err == -EIO) {
cpos += 1 << (sb->s_blocksize_bits);
cpos &= ~((u32)sb->s_blocksize-1);
}
err = -EIO;
goto end_of_dir;
}
cpos = EXFAT_I(inode)->fid.rwoffset << DENTRY_SIZE_BITS;
if (!nb->lfn[0])
goto end_of_dir;
if (!memcmp(nb->sfn, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH)) {
inum = inode->i_ino;
} else if (!memcmp(nb->sfn, 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);
}
}
/* Before calling dir_emit(), sb_lock should be released.
* Because page fault can occur in dir_emit() when the size of buffer given
* from user is larger than one page size
*/
unlock_super(sb);
if (filldir(dirent, nb->lfn, strlen(nb->lfn), cpos, inum,
(de.Attr & ATTR_SUBDIR) ? DT_DIR : DT_REG) < 0)
goto out_unlocked;
lock_super(sb);
filp->f_pos = cpos;
goto get_new;
end_of_dir:
if (!cpos && fake_offset)
cpos = ITER_POS_FILLED_DOTS;
filp->f_pos = cpos;
out:
unlock_super(sb);
out_unlocked:
/*
* To improve performance, free namebuf after unlock sb_lock.
* If namebuf is not allocated, this function do nothing
*/
exfat_free_namebuf(nb);
return err;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
/* EMPTY */
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) */
static inline struct inode *file_inode(const struct file *f)
{
return f->f_dentry->d_inode;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
static inline int __is_sb_dirty(struct super_block *sb)
{
return EXFAT_SB(sb)->s_dirt;
}
static inline void __set_sb_clean(struct super_block *sb)
{
EXFAT_SB(sb)->s_dirt = 0;
}
/* Workqueue wrapper for exfat_write_super () */
static void __write_super_delayed(struct work_struct *work)
{
struct exfat_sb_info *sbi;
struct super_block *sb;
sbi = container_of(work, struct exfat_sb_info, write_super_work.work);
sb = sbi->host_sb;
/* XXX: Is this needed? */
if (!sb || !down_read_trylock(&sb->s_umount)) {
DMSG("%s: skip delayed work(write_super).\n", __func__);
return;
}
DMSG("%s: do delayed_work(write_super).\n", __func__);
spin_lock(&sbi->work_lock);
sbi->write_super_queued = 0;
spin_unlock(&sbi->work_lock);
exfat_write_super(sb);
up_read(&sb->s_umount);
}
static void setup_exfat_sync_super_wq(struct super_block *sb)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
mutex_init(&sbi->s_lock);
spin_lock_init(&sbi->work_lock);
INIT_DELAYED_WORK(&sbi->write_super_work, __write_super_delayed);
sbi->host_sb = sb;
}
static inline bool __cancel_delayed_work_sync(struct exfat_sb_info *sbi)
{
return cancel_delayed_work_sync(&sbi->write_super_work);
}
static inline void lock_super(struct super_block *sb)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
mutex_lock(&sbi->s_lock);
}
static inline void unlock_super(struct super_block *sb)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
mutex_unlock(&sbi->s_lock);
}
static int exfat_revalidate(struct dentry *dentry, unsigned int flags)
{
if (flags & LOOKUP_RCU)
return -ECHILD;
return __exfat_revalidate(dentry);
}
static int exfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
{
if (flags & LOOKUP_RCU)
return -ECHILD;
return __exfat_revalidate_ci(dentry, flags);
}
static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
struct exfat_inode_info *info;
struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
struct inode *inode = NULL;
spin_lock(&sbi->inode_hash_lock);
hlist_for_each_entry(info, head, i_hash_fat) {
BUG_ON(info->vfs_inode.i_sb != sb);
if (i_pos != info->i_pos)
continue;
inode = igrab(&info->vfs_inode);
if (inode)
break;
}
spin_unlock(&sbi->inode_hash_lock);
return inode;
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) */
static inline int __is_sb_dirty(struct super_block *sb)
{
return sb->s_dirt;
}
static inline void __set_sb_clean(struct super_block *sb)
{
sb->s_dirt = 0;
}
static void setup_exfat_sync_super_wq(struct super_block *sb)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
sbi->host_sb = sb;
}
static inline bool __cancel_delayed_work_sync(struct exfat_sb_info *sbi)
{
/* DO NOTHING */
return 0;
}
static inline void clear_inode(struct inode *inode)
{
end_writeback(inode);
}
static int exfat_revalidate(struct dentry *dentry, struct nameidata *nd)
{
if (nd && nd->flags & LOOKUP_RCU)
return -ECHILD;
return __exfat_revalidate(dentry);
}
static int exfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
{
if (nd && nd->flags & LOOKUP_RCU)
return -ECHILD;
return __exfat_revalidate_ci(dentry, nd ? nd->flags : 0);
}
static struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
{
struct exfat_sb_info *sbi = EXFAT_SB(sb);
struct exfat_inode_info *info;
struct hlist_node *node;
struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
struct inode *inode = NULL;
spin_lock(&sbi->inode_hash_lock);
hlist_for_each_entry(info, node, head, i_hash_fat) {
BUG_ON(info->vfs_inode.i_sb != sb);
if (i_pos != info->i_pos)
continue;
inode = igrab(&info->vfs_inode);
if (inode)
break;
}
spin_unlock(&sbi->inode_hash_lock);
return inode;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags)
{
return __exfat_lookup(dir, dentry);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0) */
static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *nd)
{
return __exfat_lookup(dir, dentry);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
/* NOTHING NOW */
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0) */
#define GLOBAL_ROOT_UID (0)
#define GLOBAL_ROOT_GID (0)
static inline bool uid_eq(uid_t left, uid_t right)
{
return left == right;
}
static inline bool gid_eq(gid_t left, gid_t right)
{
return left == right;
}
static inline uid_t from_kuid_munged(struct user_namespace *to, uid_t kuid)
{
return kuid;
}
static inline gid_t from_kgid_munged(struct user_namespace *to, gid_t kgid)
{
return kgid;
}
static inline uid_t make_kuid(struct user_namespace *from, uid_t uid)
{
return uid;
}
static inline gid_t make_kgid(struct user_namespace *from, gid_t gid)
{
return gid;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
static struct dentry *__d_make_root(struct inode *root_inode)
{
return d_make_root(root_inode);
}
static void __exfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
{
down_write(&EXFAT_I(inode)->truncate_lock);
truncate_setsize(inode, new);
exfat_truncate(inode, old);
up_write(&EXFAT_I(inode)->truncate_lock);
}
static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
{
sector_t blocknr;
/* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
down_read(&EXFAT_I(mapping->host)->truncate_lock);
blocknr = generic_block_bmap(mapping, block, exfat_get_block);
up_read(&EXFAT_I(mapping->host)->truncate_lock);
return blocknr;
}
static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
return __exfat_mkdir(dir, dentry);
}
static int exfat_show_options(struct seq_file *m, struct dentry *root)
{
return __exfat_show_options(m, root->d_sb);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0) */
static inline void set_nlink(struct inode *inode, unsigned int nlink)
{
inode->i_nlink = nlink;
}
static struct dentry *__d_make_root(struct inode *root_inode)
{
return d_alloc_root(root_inode);
}
static void __exfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
{
truncate_setsize(inode, new);
exfat_truncate(inode, old);
}
static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
{
sector_t blocknr;
/* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
down_read(&mapping->host->i_alloc_sem);
blocknr = generic_block_bmap(mapping, block, exfat_get_block);
up_read(&mapping->host->i_alloc_sem);
return blocknr;
}
static int exfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
return __exfat_mkdir(dir, dentry);
}
static int exfat_show_options(struct seq_file *m, struct vfsmount *mnt)
{
return __exfat_show_options(m, mnt->mnt_sb);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
#define __exfat_generic_file_fsync(filp, start, end, datasync) \
generic_file_fsync(filp, start, end, datasync)