-
Notifications
You must be signed in to change notification settings - Fork 20
/
sdfat.c
5347 lines (4484 loc) · 137 KB
/
sdfat.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
/*
* 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, see <http://www.gnu.org/licenses/>.
*/
/************************************************************************/
/* */
/* PROJECT : exFAT & FAT12/16/32 File System */
/* FILE : core.c */
/* PURPOSE : sdFAT glue layer for supporting VFS */
/* */
/*----------------------------------------------------------------------*/
/* NOTES */
/* */
/* */
/************************************************************************/
#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 <asm/current.h>
#include <asm/unaligned.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
#include <linux/iversion.h>
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
#include <linux/aio.h>
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 0, 0)
#error SDFAT only supports linux kernel version 3.0 or higher
#endif
#include "sdfat.h"
#include "version.h"
/* skip iterating emit_dots when dir is empty */
#define ITER_POS_FILLED_DOTS (2)
/* type index declare at sdfat.h */
const char *FS_TYPE_STR[] = {
"auto",
"exfat",
"vfat"
};
static struct kset *sdfat_kset;
static struct kmem_cache *sdfat_inode_cachep;
static int sdfat_default_codepage = CONFIG_SDFAT_DEFAULT_CODEPAGE;
static char sdfat_default_iocharset[] = CONFIG_SDFAT_DEFAULT_IOCHARSET;
static const char sdfat_iocharset_with_utf8[] = "iso8859-1";
#ifdef CONFIG_SDFAT_TRACE_SB_LOCK
static unsigned long __lock_jiffies;
#endif
static void sdfat_truncate(struct inode *inode, loff_t old_size);
static int sdfat_get_block(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create);
static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos);
static struct inode *sdfat_build_inode(struct super_block *sb, const FILE_ID_T *fid, loff_t i_pos);
static void sdfat_detach(struct inode *inode);
static void sdfat_attach(struct inode *inode, loff_t i_pos);
static inline unsigned long sdfat_hash(loff_t i_pos);
static int __sdfat_write_inode(struct inode *inode, int sync);
static int sdfat_sync_inode(struct inode *inode);
static int sdfat_write_inode(struct inode *inode, struct writeback_control *wbc);
static void sdfat_write_super(struct super_block *sb);
static void sdfat_write_failed(struct address_space *mapping, loff_t to);
static void sdfat_init_namebuf(DENTRY_NAMEBUF_T *nb);
static int sdfat_alloc_namebuf(DENTRY_NAMEBUF_T *nb);
static void sdfat_free_namebuf(DENTRY_NAMEBUF_T *nb);
/*************************************************************************
* INNER FUNCTIONS FOR FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
*************************************************************************/
static int __sdfat_getattr(struct inode *inode, struct kstat *stat);
static void __sdfat_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 __sdfat_create(struct inode *dir, struct dentry *dentry);
static int __sdfat_revalidate(struct dentry *dentry);
static int __sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags);
static int __sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync);
static struct dentry *__sdfat_lookup(struct inode *dir, struct dentry *dentry);
static int __sdfat_mkdir(struct inode *dir, struct dentry *dentry);
static int __sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry);
static int __sdfat_show_options(struct seq_file *m, struct super_block *sb);
static inline ssize_t __sdfat_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 __sdfat_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 __sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr);
static int __sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr);
static int __sdfat_cmp(const struct dentry *dentry, unsigned int len,
const char *str, const struct qstr *name);
static int __sdfat_cmpi(const struct dentry *dentry, unsigned int len,
const char *str, const struct qstr *name);
/*************************************************************************
* FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
*************************************************************************/
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)
static inline void inode_set_iversion(struct inode *inode, u64 val)
{
inode->i_version = val;
}
static inline u64 inode_peek_iversion(struct inode *inode)
{
return inode->i_version;
}
#endif
#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, 11, 0)
static int sdfat_getattr(const struct path *path, struct kstat *stat,
u32 request_mask, unsigned int query_flags)
{
struct inode *inode = d_backing_inode(path->dentry);
return __sdfat_getattr(inode, stat);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 11, 0) */
static int sdfat_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
{
struct inode *inode = dentry->d_inode;
return __sdfat_getattr(inode, stat);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
static inline void __sdfat_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 __sdfat_clean_bdev_aliases(struct block_device *bdev, sector_t block)
{
unmap_underlying_metadata(bdev, block);
}
static inline int wbc_to_write_flags(struct writeback_control *wbc)
{
if (wbc->sync_mode == WB_SYNC_ALL)
return WRITE_SYNC;
return 0;
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
static int sdfat_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 __sdfat_rename(old_dir, old_dentry, new_dir, new_dentry);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 0) */
static int sdfat_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
return __sdfat_rename(old_dir, old_dentry, new_dir, new_dentry);
}
static int setattr_prepare(struct dentry *dentry, struct iattr *attr)
{
struct inode *inode = dentry->d_inode;
return inode_change_ok(inode, attr);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
static inline void __sdfat_submit_bio_write(struct bio *bio,
struct writeback_control *wbc)
{
int write_flags = wbc_to_write_flags(wbc);
bio_set_op_attrs(bio, REQ_OP_WRITE, write_flags);
submit_bio(bio);
}
static inline unsigned int __sdfat_full_name_hash(const struct dentry *dentry, const char *name, unsigned int len)
{
return full_name_hash(dentry, name, len);
}
static inline unsigned long __sdfat_init_name_hash(const struct dentry *dentry)
{
return init_name_hash(dentry);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0) */
static inline void __sdfat_submit_bio_write(struct bio *bio,
struct writeback_control *wbc)
{
int write_flags = wbc_to_write_flags(wbc);
submit_bio(WRITE | write_flags, bio);
}
static inline unsigned int __sdfat_full_name_hash(const struct dentry *unused, const char *name, unsigned int len)
{
return full_name_hash(name, len);
}
static inline unsigned long __sdfat_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 sdfat_remount_syncfs(struct super_block *sb)
{
sync_filesystem(sb);
return 0;
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0) */
static inline int sdfat_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, 15, 0)
/* EMPTY */
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) */
static inline void truncate_inode_pages_final(struct address_space *mapping)
{
truncate_inode_pages(mapping, 0);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
static inline sector_t __sdfat_bio_sector(struct bio *bio)
{
return bio->bi_iter.bi_sector;
}
static inline void __sdfat_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 __sdfat_truncate_pagecache(struct inode *inode,
loff_t to, loff_t newsize)
{
truncate_pagecache(inode, newsize);
}
static int sdfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
{
return __sdfat_d_hash(dentry, qstr);
}
static int sdfat_d_hashi(const struct dentry *dentry, struct qstr *qstr)
{
return __sdfat_d_hashi(dentry, qstr);
}
//instead of sdfat_readdir
static int sdfat_iterate(struct file *filp, struct dir_context *ctx)
{
struct inode *inode = filp->f_path.dentry->d_inode;
struct super_block *sb = inode->i_sb;
struct sdfat_sb_info *sbi = SDFAT_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;
sdfat_init_namebuf(nb);
__lock_super(sb);
cpos = ctx->pos;
if ((fsi->vol_type == EXFAT) || (inode->i_ino == SDFAT_ROOT_INO)) {
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 = sdfat_alloc_namebuf(nb);
if (err)
goto out;
get_new:
SDFAT_I(inode)->fid.size = i_size_read(inode);
SDFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
if (cpos >= SDFAT_I(inode)->fid.size)
goto end_of_dir;
err = fsapi_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 = SDFAT_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) SDFAT_I(inode)->fid.start_clu << 32) |
((SDFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
struct inode *tmp = sdfat_iget(sb, i_pos);
if (tmp) {
inum = tmp->i_ino;
iput(tmp);
} else {
inum = iunique(sb, SDFAT_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
*/
sdfat_free_namebuf(nb);
return err;
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) */
static inline sector_t __sdfat_bio_sector(struct bio *bio)
{
return bio->bi_sector;
}
static inline void __sdfat_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 __sdfat_truncate_pagecache(struct inode *inode,
loff_t to, loff_t newsize)
{
truncate_pagecache(inode, to, newsize);
}
static int sdfat_d_hash(const struct dentry *dentry,
const struct inode *inode, struct qstr *qstr)
{
return __sdfat_d_hash(dentry, qstr);
}
static int sdfat_d_hashi(const struct dentry *dentry,
const struct inode *inode, struct qstr *qstr)
{
return __sdfat_d_hashi(dentry, qstr);
}
static int sdfat_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 sdfat_sb_info *sbi = SDFAT_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;
sdfat_init_namebuf(nb);
__lock_super(sb);
cpos = filp->f_pos;
/* Fake . and .. for the root directory. */
if ((fsi->vol_type == EXFAT) || (inode->i_ino == SDFAT_ROOT_INO)) {
while (cpos < ITER_POS_FILLED_DOTS) {
if (inode->i_ino == SDFAT_ROOT_INO)
inum = SDFAT_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 = sdfat_alloc_namebuf(nb);
if (err)
goto out;
get_new:
SDFAT_I(inode)->fid.size = i_size_read(inode);
SDFAT_I(inode)->fid.rwoffset = cpos >> DENTRY_SIZE_BITS;
if (cpos >= SDFAT_I(inode)->fid.size)
goto end_of_dir;
err = fsapi_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 = SDFAT_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) SDFAT_I(inode)->fid.start_clu << 32) |
((SDFAT_I(inode)->fid.rwoffset-1) & 0xffffffff);
struct inode *tmp = sdfat_iget(sb, i_pos);
if (tmp) {
inum = tmp->i_ino;
iput(tmp);
} else {
inum = iunique(sb, SDFAT_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
*/
sdfat_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 SDFAT_SB(sb)->s_dirt;
}
static inline void __set_sb_clean(struct super_block *sb)
{
SDFAT_SB(sb)->s_dirt = 0;
}
/* Workqueue wrapper for sdfat_write_super () */
static void __write_super_delayed(struct work_struct *work)
{
struct sdfat_sb_info *sbi;
struct super_block *sb;
sbi = container_of(work, struct sdfat_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);
sdfat_write_super(sb);
up_read(&sb->s_umount);
}
static void setup_sdfat_sync_super_wq(struct super_block *sb)
{
struct sdfat_sb_info *sbi = SDFAT_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 sdfat_sb_info *sbi)
{
return cancel_delayed_work_sync(&sbi->write_super_work);
}
static inline void lock_super(struct super_block *sb)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
mutex_lock(&sbi->s_lock);
}
static inline void unlock_super(struct super_block *sb)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
mutex_unlock(&sbi->s_lock);
}
static int sdfat_revalidate(struct dentry *dentry, unsigned int flags)
{
if (flags & LOOKUP_RCU)
return -ECHILD;
return __sdfat_revalidate(dentry);
}
static int sdfat_revalidate_ci(struct dentry *dentry, unsigned int flags)
{
if (flags & LOOKUP_RCU)
return -ECHILD;
return __sdfat_revalidate_ci(dentry, flags);
}
static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
struct sdfat_inode_info *info;
struct hlist_head *head = sbi->inode_hashtable + sdfat_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_sdfat_sync_super_wq(struct super_block *sb)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
sbi->host_sb = sb;
}
static inline bool __cancel_delayed_work_sync(struct sdfat_sb_info *sbi)
{
/* DO NOTHING */
return 0;
}
static inline void clear_inode(struct inode *inode)
{
end_writeback(inode);
}
static int sdfat_revalidate(struct dentry *dentry, struct nameidata *nd)
{
if (nd && nd->flags & LOOKUP_RCU)
return -ECHILD;
return __sdfat_revalidate(dentry);
}
static int sdfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
{
if (nd && nd->flags & LOOKUP_RCU)
return -ECHILD;
return __sdfat_revalidate_ci(dentry, nd ? nd->flags : 0);
}
static struct inode *sdfat_iget(struct super_block *sb, loff_t i_pos)
{
struct sdfat_sb_info *sbi = SDFAT_SB(sb);
struct sdfat_inode_info *info;
struct hlist_node *node;
struct hlist_head *head = sbi->inode_hashtable + sdfat_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 *sdfat_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags)
{
return __sdfat_lookup(dir, dentry);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0) */
static struct dentry *sdfat_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *nd)
{
return __sdfat_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 __sdfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
{
down_write(&SDFAT_I(inode)->truncate_lock);
truncate_setsize(inode, new);
sdfat_truncate(inode, old);
up_write(&SDFAT_I(inode)->truncate_lock);
}
static sector_t sdfat_aop_bmap(struct address_space *mapping, sector_t block)
{
sector_t blocknr;
/* sdfat_get_cluster() assumes the requested blocknr isn't truncated. */
down_read(&SDFAT_I(mapping->host)->truncate_lock);
blocknr = generic_block_bmap(mapping, block, sdfat_get_block);
up_read(&SDFAT_I(mapping->host)->truncate_lock);
return blocknr;
}
static int sdfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
return __sdfat_mkdir(dir, dentry);
}
static int sdfat_show_options(struct seq_file *m, struct dentry *root)
{
return __sdfat_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 __sdfat_do_truncate(struct inode *inode, loff_t old, loff_t new)
{
truncate_setsize(inode, new);
sdfat_truncate(inode, old);
}
static sector_t sdfat_aop_bmap(struct address_space *mapping, sector_t block)
{
sector_t blocknr;
/* sdfat_get_cluster() assumes the requested blocknr isn't truncated. */
down_read(&mapping->host->i_alloc_sem);
blocknr = generic_block_bmap(mapping, block, sdfat_get_block);
up_read(&mapping->host->i_alloc_sem);
return blocknr;
}
static int sdfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
return __sdfat_mkdir(dir, dentry);
}
static int sdfat_show_options(struct seq_file *m, struct vfsmount *mnt)
{
return __sdfat_show_options(m, mnt->mnt_sb);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 1, 0)
#define __sdfat_generic_file_fsync(filp, start, end, datasync) \
generic_file_fsync(filp, start, end, datasync)
static int sdfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
{
return __sdfat_file_fsync(filp, start, end, datasync);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0) */
#define __sdfat_generic_file_fsync(filp, start, end, datasync) \
generic_file_fsync(filp, datasync)
static int sdfat_file_fsync(struct file *filp, int datasync)
{
return __sdfat_file_fsync(filp, 0, 0, datasync);
}
#endif
/*************************************************************************
* MORE FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY
*************************************************************************/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0)
static void sdfat_writepage_end_io(struct bio *bio)
{
__sdfat_writepage_end_io(bio, blk_status_to_errno(bio->bi_status));
}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0)
static void sdfat_writepage_end_io(struct bio *bio)
{
__sdfat_writepage_end_io(bio, bio->bi_error);
}
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) */
static void sdfat_writepage_end_io(struct bio *bio, int err)
{
if (test_bit(BIO_UPTODATE, &bio->bi_flags))
err = 0;
__sdfat_writepage_end_io(bio, err);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
static int sdfat_cmp(const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
{
return __sdfat_cmp(dentry, len, str, name);
}
static int sdfat_cmpi(const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
{
return __sdfat_cmpi(dentry, len, str, name);
}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
static int sdfat_cmp(const struct dentry *parent, const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
{
return __sdfat_cmp(dentry, len, str, name);
}
static int sdfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
unsigned int len, const char *str, const struct qstr *name)
{
return __sdfat_cmpi(dentry, len, str, name);
}
#else
static int sdfat_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)
{
return __sdfat_cmp(dentry, len, str, name);
}
static int sdfat_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)
{
return __sdfat_cmpi(dentry, len, str, name);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)
static ssize_t sdfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
{
struct file *file = iocb->ki_filp;
struct address_space *mapping = file->f_mapping;
struct inode *inode = mapping->host;
size_t count = iov_iter_count(iter);
int rw = iov_iter_rw(iter);
loff_t offset = iocb->ki_pos;