forked from LGA1150/ntfs3-oot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inode.c
2057 lines (1711 loc) · 47.9 KB
/
inode.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
/*
*
* Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
*
*/
#include <linux/blkdev.h>
#include <linux/buffer_head.h>
#include <linux/fs.h>
#include <linux/iversion.h>
#include <linux/mpage.h>
#include <linux/namei.h>
#include <linux/nls.h>
#include <linux/uio.h>
#include <linux/version.h>
#include <linux/writeback.h>
#include "debug.h"
#include "ntfs.h"
#include "ntfs_fs.h"
/*
* ntfs_read_mft
*
* reads record and parses MFT
*/
static struct inode *ntfs_read_mft(struct inode *inode,
const struct cpu_str *name,
const struct MFT_REF *ref)
{
int err = 0;
struct ntfs_inode *ni = ntfs_i(inode);
struct super_block *sb = inode->i_sb;
struct ntfs_sb_info *sbi = sb->s_fs_info;
mode_t mode = 0;
struct ATTR_STD_INFO5 *std5 = NULL;
struct ATTR_LIST_ENTRY *le;
struct ATTRIB *attr;
bool is_match = false;
bool is_root = false;
bool is_dir;
unsigned long ino = inode->i_ino;
u32 rp_fa = 0, asize, t32;
u16 roff, rsize, names = 0;
const struct ATTR_FILE_NAME *fname = NULL;
const struct INDEX_ROOT *root;
struct REPARSE_DATA_BUFFER rp; // 0x18 bytes
u64 t64;
struct MFT_REC *rec;
struct runs_tree *run;
inode->i_op = NULL;
err = mi_init(&ni->mi, sbi, ino);
if (err)
goto out;
if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
t64 = sbi->mft.lbo >> sbi->cluster_bits;
t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
sbi->mft.ni = ni;
init_rwsem(&ni->file.run_lock);
if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
err = -ENOMEM;
goto out;
}
}
err = mi_read(&ni->mi, ino == MFT_REC_MFT);
if (err)
goto out;
rec = ni->mi.mrec;
if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
;
} else if (ref->seq != rec->seq) {
err = -EINVAL;
ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
goto out;
} else if (!is_rec_inuse(rec)) {
err = -EINVAL;
ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
goto out;
}
if (le32_to_cpu(rec->total) != sbi->record_size) {
// bad inode?
err = -EINVAL;
goto out;
}
if (!is_rec_base(rec))
goto Ok;
/* record should contain $I30 root */
is_dir = rec->flags & RECORD_FLAG_DIR;
inode->i_generation = le16_to_cpu(rec->seq);
/* Enumerate all struct Attributes MFT */
le = NULL;
attr = NULL;
/*
* to reduce tab pressure use goto instead of
* while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
*/
next_attr:
run = NULL;
err = -EINVAL;
attr = ni_enum_attr_ex(ni, attr, &le, NULL);
if (!attr)
goto end_enum;
if (le && le->vcn) {
/* This is non primary attribute segment. Ignore if not MFT */
if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
goto next_attr;
run = &ni->file.run;
asize = le32_to_cpu(attr->size);
goto attr_unpack_run;
}
roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
asize = le32_to_cpu(attr->size);
switch (attr->type) {
case ATTR_STD:
if (attr->non_res ||
asize < sizeof(struct ATTR_STD_INFO) + roff ||
rsize < sizeof(struct ATTR_STD_INFO))
goto out;
if (std5)
goto next_attr;
std5 = Add2Ptr(attr, roff);
#ifdef STATX_BTIME
nt2kernel(std5->cr_time, &ni->i_crtime);
#endif
nt2kernel(std5->a_time, &inode->i_atime);
nt2kernel(std5->c_time, &inode->i_ctime);
nt2kernel(std5->m_time, &inode->i_mtime);
ni->std_fa = std5->fa;
if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
rsize >= sizeof(struct ATTR_STD_INFO5))
ni->std_security_id = std5->security_id;
goto next_attr;
case ATTR_LIST:
if (attr->name_len || le || ino == MFT_REC_LOG)
goto out;
err = ntfs_load_attr_list(ni, attr);
if (err)
goto out;
le = NULL;
attr = NULL;
goto next_attr;
case ATTR_NAME:
if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
rsize < SIZEOF_ATTRIBUTE_FILENAME)
goto out;
fname = Add2Ptr(attr, roff);
if (fname->type == FILE_NAME_DOS)
goto next_attr;
names += 1;
if (name && name->len == fname->name_len &&
!ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
NULL, false))
is_match = true;
goto next_attr;
case ATTR_DATA:
if (is_dir) {
/* ignore data attribute in dir record */
goto next_attr;
}
if (ino == MFT_REC_BADCLUST && !attr->non_res)
goto next_attr;
if (attr->name_len &&
((ino != MFT_REC_BADCLUST || !attr->non_res ||
attr->name_len != ARRAY_SIZE(BAD_NAME) ||
memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
(ino != MFT_REC_SECURE || !attr->non_res ||
attr->name_len != ARRAY_SIZE(SDS_NAME) ||
memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
/* file contains stream attribute. ignore it */
goto next_attr;
}
if (is_attr_sparsed(attr))
ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
else
ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
if (is_attr_compressed(attr))
ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
else
ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
if (is_attr_encrypted(attr))
ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
else
ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
if (!attr->non_res) {
ni->i_valid = inode->i_size = rsize;
inode_set_bytes(inode, rsize);
t32 = asize;
} else {
t32 = le16_to_cpu(attr->nres.run_off);
}
mode = S_IFREG | (0777 & sbi->options.fs_fmask_inv);
if (!attr->non_res) {
ni->ni_flags |= NI_FLAG_RESIDENT;
goto next_attr;
}
inode_set_bytes(inode, attr_ondisk_size(attr));
ni->i_valid = le64_to_cpu(attr->nres.valid_size);
inode->i_size = le64_to_cpu(attr->nres.data_size);
if (!attr->nres.alloc_size)
goto next_attr;
run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run :
&ni->file.run;
break;
case ATTR_ROOT:
if (attr->non_res)
goto out;
root = Add2Ptr(attr, roff);
is_root = true;
if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
goto next_attr;
if (root->type != ATTR_NAME ||
root->rule != NTFS_COLLATION_TYPE_FILENAME)
goto out;
if (!is_dir)
goto next_attr;
ni->ni_flags |= NI_FLAG_DIR;
err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
if (err)
goto out;
mode = sb->s_root ?
(S_IFDIR | (0777 & sbi->options.fs_dmask_inv)) :
(S_IFDIR | 0777);
goto next_attr;
case ATTR_ALLOC:
if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
goto next_attr;
inode->i_size = le64_to_cpu(attr->nres.data_size);
ni->i_valid = le64_to_cpu(attr->nres.valid_size);
inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
run = &ni->dir.alloc_run;
break;
case ATTR_BITMAP:
if (ino == MFT_REC_MFT) {
if (!attr->non_res)
goto out;
#ifndef NTFS3_64BIT_CLUSTER
/* 0x20000000 = 2^32 / 8 */
if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
goto out;
#endif
run = &sbi->mft.bitmap.run;
break;
} else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
!memcmp(attr_name(attr), I30_NAME,
sizeof(I30_NAME)) &&
attr->non_res) {
run = &ni->dir.bitmap_run;
break;
}
goto next_attr;
case ATTR_REPARSE:
if (attr->name_len)
goto next_attr;
rp_fa = ni_parse_reparse(ni, attr, &rp);
switch (rp_fa) {
case REPARSE_LINK:
if (!attr->non_res) {
inode->i_size = rsize;
inode_set_bytes(inode, rsize);
t32 = asize;
} else {
inode->i_size =
le64_to_cpu(attr->nres.data_size);
t32 = le16_to_cpu(attr->nres.run_off);
}
/* Looks like normal symlink */
ni->i_valid = inode->i_size;
/* Clear directory bit */
if (ni->ni_flags & NI_FLAG_DIR) {
indx_clear(&ni->dir);
memset(&ni->dir, 0, sizeof(ni->dir));
ni->ni_flags &= ~NI_FLAG_DIR;
} else {
run_close(&ni->file.run);
}
mode = S_IFLNK | 0777;
is_dir = false;
if (attr->non_res) {
run = &ni->file.run;
goto attr_unpack_run; // double break
}
break;
case REPARSE_COMPRESSED:
break;
case REPARSE_DEDUPLICATED:
break;
}
goto next_attr;
case ATTR_EA_INFO:
if (!attr->name_len &&
resident_data_ex(attr, sizeof(struct EA_INFO)))
ni->ni_flags |= NI_FLAG_EA;
goto next_attr;
default:
goto next_attr;
}
attr_unpack_run:
roff = le16_to_cpu(attr->nres.run_off);
t64 = le64_to_cpu(attr->nres.svcn);
err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
t64, Add2Ptr(attr, roff), asize - roff);
if (err < 0)
goto out;
err = 0;
goto next_attr;
end_enum:
if (!std5)
goto out;
if (!is_match && name) {
/* reuse rec as buffer for ascii name */
err = -ENOENT;
goto out;
}
if (std5->fa & FILE_ATTRIBUTE_READONLY)
mode &= ~0222;
/* Setup 'uid' and 'gid' */
inode->i_uid = sbi->options.fs_uid;
inode->i_gid = sbi->options.fs_gid;
if (!names) {
err = -EINVAL;
goto out;
}
if (S_ISDIR(mode)) {
ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
/*
* dot and dot-dot should be included in count but was not
* included in enumeration.
* Usually a hard links to directories are disabled
*/
set_nlink(inode, 1);
inode->i_op = &ntfs_dir_inode_operations;
inode->i_fop = &ntfs_dir_operations;
ni->i_valid = 0;
} else if (S_ISLNK(mode)) {
ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
inode->i_op = &ntfs_link_inode_operations;
inode->i_fop = NULL;
inode_nohighmem(inode); // ??
set_nlink(inode, names);
} else if (S_ISREG(mode)) {
ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
set_nlink(inode, names);
inode->i_op = &ntfs_file_inode_operations;
inode->i_fop = &ntfs_file_operations;
inode->i_mapping->a_ops =
is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
if (ino != MFT_REC_MFT)
init_rwsem(&ni->file.run_lock);
} else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
/* Records in $Extend are not a files or general directories */
} else {
err = -EINVAL;
goto out;
}
if ((sbi->options.sys_immutable &&
(std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
!S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
inode->i_flags |= S_IMMUTABLE;
} else {
inode->i_flags &= ~S_IMMUTABLE;
}
inode->i_mode = mode;
if (!(ni->ni_flags & NI_FLAG_EA)) {
/* if no xattr then no security (stored in xattr) */
inode->i_flags |= S_NOSEC;
}
Ok:
if (ino == MFT_REC_MFT && !sb->s_root)
sbi->mft.ni = NULL;
unlock_new_inode(inode);
return inode;
out:
if (ino == MFT_REC_MFT && !sb->s_root)
sbi->mft.ni = NULL;
iget_failed(inode);
return ERR_PTR(err);
}
/* returns 1 if match */
static int ntfs_test_inode(struct inode *inode, const struct MFT_REF *ref)
{
return ino_get(ref) == inode->i_ino;
}
static int ntfs_set_inode(struct inode *inode, const struct MFT_REF *ref)
{
inode->i_ino = ino_get(ref);
return 0;
}
struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
const struct cpu_str *name)
{
struct inode *inode;
inode = iget5_locked(sb, ino_get(ref),
(int (*)(struct inode *, void *))ntfs_test_inode,
(int (*)(struct inode *, void *))ntfs_set_inode,
(void *)ref);
if (unlikely(!inode))
return ERR_PTR(-ENOMEM);
/* If this is a freshly allocated inode, need to read it now. */
if (inode->i_state & I_NEW)
inode = ntfs_read_mft(inode, name, ref);
else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
/* inode overlaps? */
make_bad_inode(inode);
}
return inode;
}
enum get_block_ctx {
GET_BLOCK_GENERAL = 0,
GET_BLOCK_WRITE_BEGIN = 1,
GET_BLOCK_DIRECT_IO_R = 2,
GET_BLOCK_DIRECT_IO_W = 3,
GET_BLOCK_BMAP = 4,
};
static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
struct buffer_head *bh, int create,
enum get_block_ctx ctx)
{
struct super_block *sb = inode->i_sb;
struct ntfs_sb_info *sbi = sb->s_fs_info;
struct ntfs_inode *ni = ntfs_i(inode);
struct page *page = bh->b_page;
u8 cluster_bits = sbi->cluster_bits;
u32 block_size = sb->s_blocksize;
u64 bytes, lbo, valid;
u32 off;
int err;
CLST vcn, lcn, len;
bool new;
/*clear previous state*/
clear_buffer_new(bh);
clear_buffer_uptodate(bh);
/* direct write uses 'create=0'*/
if (!create && vbo >= ni->i_valid) {
/* out of valid */
return 0;
}
if (vbo >= inode->i_size) {
/* out of size */
return 0;
}
if (is_resident(ni)) {
ni_lock(ni);
err = attr_data_read_resident(ni, page);
ni_unlock(ni);
if (!err)
set_buffer_uptodate(bh);
bh->b_size = block_size;
return err;
}
vcn = vbo >> cluster_bits;
off = vbo & sbi->cluster_mask;
new = false;
err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
if (err)
goto out;
if (!len)
return 0;
bytes = ((u64)len << cluster_bits) - off;
if (lcn == SPARSE_LCN) {
if (!create) {
if (bh->b_size > bytes)
bh->b_size = bytes;
return 0;
}
WARN_ON(1);
}
if (new) {
set_buffer_new(bh);
if ((len << cluster_bits) > block_size)
ntfs_sparse_cluster(inode, page, vcn, len);
}
lbo = ((u64)lcn << cluster_bits) + off;
set_buffer_mapped(bh);
bh->b_bdev = sb->s_bdev;
bh->b_blocknr = lbo >> sb->s_blocksize_bits;
valid = ni->i_valid;
if (ctx == GET_BLOCK_DIRECT_IO_W) {
/*ntfs_direct_IO will update ni->i_valid */
if (vbo >= valid)
set_buffer_new(bh);
} else if (create) {
/*normal write*/
if (vbo >= valid) {
set_buffer_new(bh);
if (bytes > bh->b_size)
bytes = bh->b_size;
ni->i_valid = vbo + bytes;
mark_inode_dirty(inode);
}
} else if (valid >= inode->i_size) {
/* normal read of normal file*/
} else if (vbo >= valid) {
/* read out of valid data*/
/* should never be here 'cause already checked */
clear_buffer_mapped(bh);
} else if (vbo + bytes <= valid) {
/* normal read */
} else if (vbo + block_size <= valid) {
/* normal short read */
bytes = block_size;
} else {
/*
* read across valid size: vbo < valid && valid < vbo + block_size
*/
u32 voff = valid - vbo;
bh->b_size = bytes = block_size;
off = vbo & (PAGE_SIZE - 1);
set_bh_page(bh, page, off);
ll_rw_block(REQ_OP_READ, 0, 1, &bh);
wait_on_buffer(bh);
/* Uhhuh. Read error. Complain and punt. */
if (!buffer_uptodate(bh)) {
err = -EIO;
goto out;
}
zero_user_segment(page, off + voff, off + block_size);
}
if (bh->b_size > bytes)
bh->b_size = bytes;
#ifndef __LP64__
if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
static_assert(sizeof(size_t) < sizeof(loff_t));
if (bytes > 0x40000000u)
bh->b_size = 0x40000000u;
}
#endif
return 0;
out:
return err;
}
int ntfs_get_block(struct inode *inode, sector_t vbn,
struct buffer_head *bh_result, int create)
{
return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
bh_result, create, GET_BLOCK_GENERAL);
}
static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
struct buffer_head *bh_result, int create)
{
return ntfs_get_block_vbo(inode,
(u64)vsn << inode->i_sb->s_blocksize_bits,
bh_result, create, GET_BLOCK_BMAP);
}
static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
{
return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
}
static int ntfs_readpage(struct file *file, struct page *page)
{
int err;
struct address_space *mapping = page->mapping;
struct inode *inode = mapping->host;
struct ntfs_inode *ni = ntfs_i(inode);
if (is_resident(ni)) {
ni_lock(ni);
err = attr_data_read_resident(ni, page);
ni_unlock(ni);
if (err != E_NTFS_NONRESIDENT) {
unlock_page(page);
return err;
}
}
if (is_compressed(ni)) {
ni_lock(ni);
err = ni_readpage_cmpr(ni, page);
ni_unlock(ni);
return err;
}
/* normal + sparse files */
return mpage_readpage(page, ntfs_get_block);
}
static void ntfs_readahead(struct readahead_control *rac)
{
struct address_space *mapping = rac->mapping;
struct inode *inode = mapping->host;
struct ntfs_inode *ni = ntfs_i(inode);
u64 valid;
loff_t pos;
if (is_resident(ni)) {
/* no readahead for resident */
return;
}
if (is_compressed(ni)) {
/* no readahead for compressed */
return;
}
valid = ni->i_valid;
pos = readahead_pos(rac);
if (valid < i_size_read(inode) && pos <= valid &&
valid < pos + readahead_length(rac)) {
/* range cross 'valid'. read it page by page */
return;
}
mpage_readahead(rac, ntfs_get_block);
}
static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create)
{
return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
bh_result, create, GET_BLOCK_DIRECT_IO_R);
}
static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
struct buffer_head *bh_result, int create)
{
return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
bh_result, create, GET_BLOCK_DIRECT_IO_W);
}
static ssize_t ntfs_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;
struct ntfs_inode *ni = ntfs_i(inode);
size_t count = iov_iter_count(iter);
loff_t vbo = iocb->ki_pos;
loff_t end = vbo + count;
int wr = iov_iter_rw(iter) & WRITE;
const struct iovec *iov = iter->iov;
unsigned long nr_segs = iter->nr_segs;
loff_t valid;
ssize_t ret;
if (is_resident(ni)) {
/*switch to buffered write*/
ret = 0;
goto out;
}
ret = blockdev_direct_IO(iocb, inode, iter,
wr ? ntfs_get_block_direct_IO_W :
ntfs_get_block_direct_IO_R);
valid = ni->i_valid;
if (wr) {
if (ret <= 0)
goto out;
vbo += ret;
if (vbo > valid && !S_ISBLK(inode->i_mode)) {
ni->i_valid = vbo;
mark_inode_dirty(inode);
}
} else if (vbo < valid && valid < end) {
/* fix page */
unsigned long uaddr = ~0ul;
struct page *page;
long i, npages;
size_t dvbo = valid - vbo;
size_t off = 0;
/*Find user address*/
for (i = 0; i < nr_segs; i++) {
if (off <= dvbo && dvbo < off + iov[i].iov_len) {
uaddr = (unsigned long)iov[i].iov_base + dvbo -
off;
break;
}
off += iov[i].iov_len;
}
if (uaddr == ~0ul)
goto fix_error;
npages = get_user_pages_unlocked(uaddr, 1, &page, FOLL_WRITE);
if (npages <= 0)
goto fix_error;
zero_user_segment(page, valid & (PAGE_SIZE - 1), PAGE_SIZE);
put_page(page);
}
out:
return ret;
fix_error:
ntfs_inode_warn(inode, "file garbage at 0x%llx", valid);
goto out;
}
int ntfs_set_size(struct inode *inode, u64 new_size)
{
struct super_block *sb = inode->i_sb;
struct ntfs_sb_info *sbi = sb->s_fs_info;
struct ntfs_inode *ni = ntfs_i(inode);
int err;
/* Check for maximum file size */
if (is_sparsed(ni) || is_compressed(ni)) {
if (new_size > sbi->maxbytes_sparse) {
err = -EFBIG;
goto out;
}
} else if (new_size > sbi->maxbytes) {
err = -EFBIG;
goto out;
}
ni_lock(ni);
down_write(&ni->file.run_lock);
err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
&ni->i_valid, true, NULL);
up_write(&ni->file.run_lock);
ni_unlock(ni);
mark_inode_dirty(inode);
out:
return err;
}
static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
{
struct address_space *mapping = page->mapping;
struct inode *inode = mapping->host;
struct ntfs_inode *ni = ntfs_i(inode);
int err;
if (is_resident(ni)) {
ni_lock(ni);
err = attr_data_write_resident(ni, page);
ni_unlock(ni);
if (err != E_NTFS_NONRESIDENT) {
unlock_page(page);
return err;
}
}
return block_write_full_page(page, ntfs_get_block, wbc);
}
static int ntfs_writepages(struct address_space *mapping,
struct writeback_control *wbc)
{
struct inode *inode = mapping->host;
struct ntfs_inode *ni = ntfs_i(inode);
/* redirect call to 'ntfs_writepage' for resident files*/
get_block_t *get_block = is_resident(ni) ? NULL : &ntfs_get_block;
return mpage_writepages(mapping, wbc, get_block);
}
static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
struct buffer_head *bh_result, int create)
{
return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
bh_result, create, GET_BLOCK_WRITE_BEGIN);
}
static int ntfs_write_begin(struct file *file, struct address_space *mapping,
loff_t pos, u32 len, u32 flags, struct page **pagep,
void **fsdata)
{
int err;
struct inode *inode = mapping->host;
struct ntfs_inode *ni = ntfs_i(inode);
*pagep = NULL;
if (is_resident(ni)) {
struct page *page = grab_cache_page_write_begin(
mapping, pos >> PAGE_SHIFT, flags);
if (!page) {
err = -ENOMEM;
goto out;
}
ni_lock(ni);
err = attr_data_read_resident(ni, page);
ni_unlock(ni);
if (!err) {
*pagep = page;
goto out;
}
unlock_page(page);
put_page(page);
if (err != E_NTFS_NONRESIDENT)
goto out;
}
err = block_write_begin(mapping, pos, len, flags, pagep,
ntfs_get_block_write_begin);
out:
return err;
}
/* address_space_operations::write_end */
static int ntfs_write_end(struct file *file, struct address_space *mapping,
loff_t pos, u32 len, u32 copied, struct page *page,
void *fsdata)
{
struct inode *inode = mapping->host;
struct ntfs_inode *ni = ntfs_i(inode);
u64 valid = ni->i_valid;
bool dirty = false;
int err;
if (is_resident(ni)) {
ni_lock(ni);
err = attr_data_write_resident(ni, page);
ni_unlock(ni);
if (!err) {
dirty = true;
/* clear any buffers in page*/
if (page_has_buffers(page)) {
struct buffer_head *head, *bh;
bh = head = page_buffers(page);
do {
clear_buffer_dirty(bh);
clear_buffer_mapped(bh);
set_buffer_uptodate(bh);
} while (head != (bh = bh->b_this_page));
}
SetPageUptodate(page);
err = copied;
}
unlock_page(page);
put_page(page);
} else {
err = generic_write_end(file, mapping, pos, len, copied, page,
fsdata);
}
if (err >= 0) {
if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
inode->i_ctime = inode->i_mtime = current_time(inode);
ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
dirty = true;
}
if (valid != ni->i_valid) {
/* ni->i_valid is changed in ntfs_get_block_vbo */
dirty = true;
}
if (dirty)
mark_inode_dirty(inode);
}
return err;
}
int reset_log_file(struct inode *inode)
{
int err;
loff_t pos = 0;
u32 log_size = inode->i_size;
struct address_space *mapping = inode->i_mapping;
for (;;) {
u32 len;
void *kaddr;
struct page *page;
len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
err = block_write_begin(mapping, pos, len, 0, &page,
ntfs_get_block_write_begin);
if (err)
goto out;