-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.c
More file actions
1124 lines (953 loc) · 28.5 KB
/
Copy pathfile.c
File metadata and controls
1124 lines (953 loc) · 28.5 KB
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
/*
* NTFS kernel file operations. Part of the Linux-NTFS project.
*
* Copyright (c) 2001-2015 Anton Altaparmakov and Tuxera Inc.
* Copyright (c) 2025 LG Electronics Co., Ltd.
*/
#include <linux/writeback.h>
#include <linux/blkdev.h>
#include <linux/fs.h>
#include <linux/iomap.h>
#include <linux/uio.h>
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
#include <linux/compat.h>
#include <linux/falloc.h>
#include <uapi/linux/ntfs.h>
#include "lcnalloc.h"
#include "ntfs.h"
#include "aops.h"
#include "reparse.h"
#include "ea.h"
#include "ntfs_iomap.h"
#include "misc.h"
#include "bitmap.h"
/**
* ntfs_file_open - called when an inode is about to be opened
* @vi: inode to be opened
* @filp: file structure describing the inode
*
* Limit file size to the page cache limit on architectures where unsigned long
* is 32-bits. This is the most we can do for now without overflowing the page
* cache page index. Doing it this way means we don't run into problems because
* of existing too large files. It would be better to allow the user to read
* the beginning of the file but I doubt very much anyone is going to hit this
* check on a 32-bit architecture, so there is no point in adding the extra
* complexity required to support this.
*
* On 64-bit architectures, the check is hopefully optimized away by the
* compiler.
*
* After the check passes, just call generic_file_open() to do its work.
*/
static int ntfs_file_open(struct inode *vi, struct file *filp)
{
struct ntfs_inode *ni = NTFS_I(vi);
if (NVolShutdown(ni->vol))
return -EIO;
if (sizeof(unsigned long) < 8) {
if (i_size_read(vi) > MAX_LFS_FILESIZE)
return -EOVERFLOW;
}
if (filp->f_flags & O_TRUNC && NInoNonResident(ni)) {
int err;
mutex_lock(&ni->mrec_lock);
down_read(&ni->runlist.lock);
if (!ni->runlist.rl) {
err = ntfs_attr_map_whole_runlist(ni);
if (err) {
up_read(&ni->runlist.lock);
mutex_unlock(&ni->mrec_lock);
return err;
}
}
ni->lcn_seek_trunc = ni->runlist.rl->lcn;
up_read(&ni->runlist.lock);
mutex_unlock(&ni->mrec_lock);
}
filp->f_mode |= FMODE_NOWAIT;
return generic_file_open(vi, filp);
}
static int ntfs_file_release(struct inode *vi, struct file *filp)
{
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_volume *vol = ni->vol;
s64 aligned_data_size = round_up(ni->data_size, vol->cluster_size);
if (NInoCompressed(ni))
return 0;
inode_lock(vi);
mutex_lock(&ni->mrec_lock);
down_write(&ni->runlist.lock);
if (aligned_data_size < ni->allocated_size) {
int err;
s64 vcn_ds = aligned_data_size >> vol->cluster_size_bits;
s64 vcn_tr = -1;
struct runlist_element *rl = ni->runlist.rl;
ssize_t rc = ni->runlist.count - 2;
while (rc >= 0 && rl[rc].lcn == LCN_HOLE && vcn_ds <= rl[rc].vcn) {
vcn_tr = rl[rc].vcn;
rc--;
}
if (vcn_tr >= 0) {
err = ntfs_rl_truncate_nolock(vol, &ni->runlist, vcn_tr);
if (err) {
ntfs_free(ni->runlist.rl);
ni->runlist.rl = NULL;
ntfs_error(vol->sb, "Preallocated block rollback failed");
} else {
ni->allocated_size = vcn_tr << vol->cluster_size_bits;
err = ntfs_attr_update_mapping_pairs(ni, 0);
if (err)
ntfs_error(vol->sb,
"Failed to rollback mapping pairs for prealloc");
}
}
}
up_write(&ni->runlist.lock);
mutex_unlock(&ni->mrec_lock);
inode_unlock(vi);
return 0;
}
/**
* ntfs_file_fsync - sync a file to disk
* @filp: file to be synced
* @start: start offset to be synced
* @end: end offset to be synced
* @datasync: if non-zero only flush user data and not metadata
*
* Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync
* system calls. This function is inspired by fs/buffer.c::file_fsync().
*
* If @datasync is false, write the mft record and all associated extent mft
* records as well as the $DATA attribute and then sync the block device.
*
* If @datasync is true and the attribute is non-resident, we skip the writing
* of the mft record and all associated extent mft records (this might still
* happen due to the write_inode_now() call).
*
* Also, if @datasync is true, we do not wait on the inode to be written out
* but we always wait on the page cache pages to be written out.
*/
static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
int datasync)
{
struct inode *vi = filp->f_mapping->host;
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_volume *vol = ni->vol;
int err, ret = 0;
struct inode *parent_vi, *ia_vi;
struct ntfs_attr_search_ctx *ctx;
ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
if (NVolShutdown(vol))
return -EIO;
err = file_write_and_wait_range(filp, start, end);
if (err)
return err;
if (!datasync || !NInoNonResident(NTFS_I(vi)))
ret = __ntfs_write_inode(vi, 1);
write_inode_now(vi, !datasync);
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx)
return -ENOMEM;
mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_2);
while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) {
if (ctx->attr->type == AT_FILE_NAME) {
struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr +
le16_to_cpu(ctx->attr->data.resident.value_offset));
parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory));
if (IS_ERR(parent_vi))
continue;
mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_PARENT_2);
ia_vi = ntfs_index_iget(parent_vi, I30, 4);
mutex_unlock(&NTFS_I(parent_vi)->mrec_lock);
if (IS_ERR(ia_vi)) {
iput(parent_vi);
continue;
}
write_inode_now(ia_vi, 1);
iput(ia_vi);
write_inode_now(parent_vi, 1);
iput(parent_vi);
} else if (ctx->attr->non_resident) {
struct inode *attr_vi;
__le16 *name;
name = (__le16 *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->name_offset));
if (ctx->attr->type == AT_DATA && ctx->attr->name_length == 0)
continue;
attr_vi = ntfs_attr_iget(vi, ctx->attr->type,
name, ctx->attr->name_length);
if (IS_ERR(attr_vi))
continue;
spin_lock(&attr_vi->i_lock);
if (attr_vi->i_state & I_DIRTY_PAGES) {
spin_unlock(&attr_vi->i_lock);
filemap_write_and_wait(attr_vi->i_mapping);
} else
spin_unlock(&attr_vi->i_lock);
iput(attr_vi);
}
}
mutex_unlock(&ni->mrec_lock);
ntfs_attr_put_search_ctx(ctx);
write_inode_now(vol->mftbmp_ino, 1);
down_write(&vol->lcnbmp_lock);
write_inode_now(vol->lcnbmp_ino, 1);
up_write(&vol->lcnbmp_lock);
write_inode_now(vol->mft_ino, 1);
/*
* NOTE: If we were to use mapping->private_list (see ext2 and
* fs/buffer.c) for dirty blocks then we could optimize the below to be
* sync_mapping_buffers(vi->i_mapping).
*/
err = sync_blockdev(vi->i_sb->s_bdev);
if (unlikely(err && !ret))
ret = err;
if (likely(!ret))
ntfs_debug("Done.");
else
ntfs_warning(vi->i_sb,
"Failed to f%ssync inode 0x%lx. Error %u.",
datasync ? "data" : "", vi->i_ino, -ret);
if (!ret)
blkdev_issue_flush(vi->i_sb->s_bdev);
return ret;
}
/**
* ntfsp_setattr - called from notify_change() when an attribute is being changed
* @idmap: idmap of the mount the inode was found from
* @dentry: dentry whose attributes to change
* @attr: structure describing the attributes and the changes
*
* We have to trap VFS attempts to truncate the file described by @dentry as
* soon as possible, because we do not implement changes in i_size yet. So we
* abort all i_size changes here.
*
* We also abort all changes of user, group, and mode as we do not implement
* the NTFS ACLs yet.
*/
int ntfsp_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
struct iattr *attr)
{
struct inode *vi = d_inode(dentry);
int err;
unsigned int ia_valid = attr->ia_valid;
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_volume *vol = ni->vol;
if (NVolShutdown(vol))
return -EIO;
err = setattr_prepare(idmap, dentry, attr);
if (err)
goto out;
if (!(vol->vol_flags & VOLUME_IS_DIRTY))
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
if (ia_valid & ATTR_SIZE) {
if (NInoCompressed(ni) || NInoEncrypted(ni)) {
ntfs_warning(vi->i_sb,
"Changes in inode size are not supported yet for %s files, ignoring.",
NInoCompressed(ni) ? "compressed" : "encrypted");
err = -EOPNOTSUPP;
} else {
loff_t old_size = vi->i_size;
err = inode_newsize_ok(vi, attr->ia_size);
if (err)
goto out;
inode_dio_wait(vi);
/* Serialize against page faults */
if (NInoNonResident(NTFS_I(vi)) &&
attr->ia_size < old_size) {
err = iomap_truncate_page(vi, attr->ia_size, NULL,
&ntfs_read_iomap_ops);
if (err)
goto out;
}
truncate_setsize(vi, attr->ia_size);
err = ntfs_truncate_vfs(vi, attr->ia_size, old_size);
if (err) {
i_size_write(vi, old_size);
goto out;
}
if (NInoNonResident(ni) && attr->ia_size > old_size &&
old_size % PAGE_SIZE != 0) {
loff_t len = min_t(loff_t,
round_up(old_size, PAGE_SIZE) - old_size,
attr->ia_size - old_size);
err = iomap_zero_range(vi, old_size, len, NULL,
&ntfs_read_iomap_ops);
}
}
if (ia_valid == ATTR_SIZE)
goto out;
ia_valid |= ATTR_MTIME | ATTR_CTIME;
}
setattr_copy(idmap, vi, attr);
if (vol->sb->s_flags & SB_POSIXACL && !S_ISLNK(vi->i_mode)) {
err = posix_acl_chmod(idmap, dentry, vi->i_mode);
if (err)
goto out;
}
if (0222 & vi->i_mode)
ni->flags &= ~FILE_ATTR_READONLY;
else
ni->flags |= FILE_ATTR_READONLY;
if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
unsigned int flags = 0;
if (ia_valid & ATTR_UID)
flags |= NTFS_EA_UID;
if (ia_valid & ATTR_GID)
flags |= NTFS_EA_GID;
if (ia_valid & ATTR_MODE)
flags |= NTFS_EA_MODE;
if (S_ISDIR(vi->i_mode))
vi->i_mode &= ~vol->dmask;
else
vi->i_mode &= ~vol->fmask;
mutex_lock(&ni->mrec_lock);
ntfs_ea_set_wsl_inode(vi, 0, NULL, flags);
mutex_unlock(&ni->mrec_lock);
}
mark_inode_dirty(vi);
out:
return err;
}
int ntfsp_getattr(struct mnt_idmap *idmap, const struct path *path,
struct kstat *stat, unsigned int request_mask,
unsigned int query_flags)
{
struct inode *inode = d_backing_inode(path->dentry);
generic_fillattr(idmap, request_mask, inode, stat);
stat->blksize = NTFS_SB(inode->i_sb)->cluster_size;
stat->blocks = (((u64)NTFS_I(inode)->i_dealloc_clusters <<
NTFS_SB(inode->i_sb)->cluster_size_bits) >> 9) + inode->i_blocks;
stat->result_mask |= STATX_BTIME;
stat->btime = NTFS_I(inode)->i_crtime;
return 0;
}
static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
{
struct inode *vi = file->f_mapping->host;
if (whence == SEEK_DATA || whence == SEEK_HOLE) {
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_volume *vol = ni->vol;
struct runlist_element *rl;
s64 vcn;
unsigned int vcn_off;
loff_t end_off;
unsigned long flags;
int i;
inode_lock_shared(vi);
if (NInoCompressed(ni) || NInoEncrypted(ni))
goto error;
read_lock_irqsave(&ni->size_lock, flags);
end_off = ni->data_size;
read_unlock_irqrestore(&ni->size_lock, flags);
if (offset < 0 || offset >= end_off)
goto error;
if (!NInoNonResident(ni)) {
if (whence == SEEK_HOLE)
offset = end_off;
goto found_no_runlist_lock;
}
vcn = offset >> vol->cluster_size_bits;
vcn_off = offset & vol->cluster_size_mask;
down_read(&ni->runlist.lock);
rl = ni->runlist.rl;
i = 0;
#ifdef DEBUG
ntfs_debug("init:");
ntfs_debug_dump_runlist(rl);
#endif
while (1) {
if (!rl || !NInoFullyMapped(ni) || rl[i].lcn == LCN_RL_NOT_MAPPED) {
int ret;
up_read(&ni->runlist.lock);
ret = ntfs_map_runlist(ni, rl ? rl[i].vcn : 0);
if (ret)
goto error;
down_read(&ni->runlist.lock);
rl = ni->runlist.rl;
#ifdef DEBUG
ntfs_debug("mapped:");
ntfs_debug_dump_runlist(ni->runlist.rl);
#endif
continue;
} else if (rl[i].lcn == LCN_ENOENT) {
if (whence == SEEK_DATA) {
up_read(&ni->runlist.lock);
goto error;
} else {
offset = end_off;
goto found;
}
} else if (rl[i + 1].vcn > vcn) {
if ((whence == SEEK_DATA && (rl[i].lcn >= 0 ||
rl[i].lcn == LCN_DELALLOC)) ||
(whence == SEEK_HOLE && rl[i].lcn == LCN_HOLE)) {
offset = (vcn << vol->cluster_size_bits) + vcn_off;
if (offset < ni->data_size)
goto found;
}
vcn = rl[i + 1].vcn;
vcn_off = 0;
}
i++;
}
up_read(&ni->runlist.lock);
inode_unlock_shared(vi);
return -EIO;
found:
up_read(&ni->runlist.lock);
found_no_runlist_lock:
inode_unlock_shared(vi);
return vfs_setpos(file, offset, vi->i_sb->s_maxbytes);
error:
inode_unlock_shared(vi);
return -ENXIO;
} else {
return generic_file_llseek_size(file, offset, whence,
vi->i_sb->s_maxbytes,
i_size_read(vi));
}
}
static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
struct inode *vi = file_inode(iocb->ki_filp);
struct super_block *sb = vi->i_sb;
ssize_t ret;
if (NVolShutdown(NTFS_SB(sb)))
return -EIO;
if (NInoCompressed(NTFS_I(vi)) && iocb->ki_flags & IOCB_DIRECT)
return -EOPNOTSUPP;
inode_lock_shared(vi);
if (iocb->ki_flags & IOCB_DIRECT) {
size_t count = iov_iter_count(to);
if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
ret = -EINVAL;
goto inode_unlock;
}
file_accessed(iocb->ki_filp);
ret = iomap_dio_rw(iocb, to, &ntfs_read_iomap_ops, NULL, IOMAP_DIO_PARTIAL,
NULL, 0);
} else {
ret = generic_file_read_iter(iocb, to);
}
inode_unlock:
inode_unlock_shared(vi);
return ret;
}
static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
int error, unsigned int flags)
{
struct inode *inode = file_inode(iocb->ki_filp);
if (error)
return error;
if (size) {
if (i_size_read(inode) < iocb->ki_pos + size) {
i_size_write(inode, iocb->ki_pos + size);
mark_inode_dirty(inode);
}
}
return 0;
}
static const struct iomap_dio_ops ntfs_write_dio_ops = {
.end_io = ntfs_file_write_dio_end_io,
};
static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct file *file = iocb->ki_filp;
struct inode *vi = file->f_mapping->host;
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_volume *vol = ni->vol;
ssize_t ret;
ssize_t count;
loff_t pos;
int err;
loff_t old_data_size, old_init_size;
if (NVolShutdown(vol))
return -EIO;
if (NInoEncrypted(ni)) {
ntfs_error(vi->i_sb, "Writing for %s files is not supported yet",
NInoCompressed(ni) ? "Compressed" : "Encrypted");
return -EOPNOTSUPP;
}
if (NInoCompressed(ni) && iocb->ki_flags & IOCB_DIRECT)
return -EOPNOTSUPP;
if (iocb->ki_flags & IOCB_NOWAIT) {
if (!inode_trylock(vi))
return -EAGAIN;
} else
inode_lock(vi);
ret = generic_write_checks(iocb, from);
if (ret <= 0)
goto out_lock;
if (NInoNonResident(ni) && (iocb->ki_flags & IOCB_DIRECT) &&
((iocb->ki_pos | ret) & (vi->i_sb->s_blocksize - 1))) {
ret = -EINVAL;
goto out_lock;
}
err = file_modified(iocb->ki_filp);
if (err) {
ret = err;
goto out_lock;
}
if (!(vol->vol_flags & VOLUME_IS_DIRTY))
ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
pos = iocb->ki_pos;
count = ret;
old_data_size = ni->data_size;
old_init_size = ni->initialized_size;
if (iocb->ki_pos + ret > old_data_size) {
mutex_lock(&ni->mrec_lock);
if (!NInoCompressed(ni) && iocb->ki_pos + ret > ni->allocated_size &&
iocb->ki_pos + ret < ni->allocated_size + vol->preallocated_size)
ret = ntfs_attr_expand(ni, iocb->ki_pos + ret,
ni->allocated_size + vol->preallocated_size);
else if (NInoCompressed(ni) && iocb->ki_pos + ret > ni->allocated_size)
ret = ntfs_attr_expand(ni, iocb->ki_pos + ret,
round_up(iocb->ki_pos + ret, ni->itype.compressed.block_size));
else
ret = ntfs_attr_expand(ni, iocb->ki_pos + ret, 0);
mutex_unlock(&ni->mrec_lock);
if (ret < 0)
goto out;
}
if (NInoNonResident(ni) && iocb->ki_pos + count > old_init_size) {
ret = ntfs_extend_initialized_size(vi, iocb->ki_pos,
iocb->ki_pos + count);
if (ret < 0)
goto out;
}
if (NInoNonResident(ni) && NInoCompressed(ni)) {
ret = ntfs_compress_write(ni, pos, count, from);
if (ret > 0)
iocb->ki_pos += ret;
goto out;
}
if (NInoNonResident(ni) && iocb->ki_flags & IOCB_DIRECT) {
ret = iomap_dio_rw(iocb, from, &ntfs_dio_iomap_ops,
&ntfs_write_dio_ops, 0, NULL, 0);
if (ret == -ENOTBLK)
ret = 0;
else if (ret < 0)
goto out;
if (iov_iter_count(from)) {
loff_t offset, end;
ssize_t written;
int ret2;
offset = iocb->ki_pos;
iocb->ki_flags &= ~IOCB_DIRECT;
written = iomap_file_buffered_write(iocb, from,
&ntfs_write_iomap_ops, NULL);
if (written < 0) {
err = written;
goto out;
}
ret += written;
end = iocb->ki_pos + written - 1;
ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,
offset, end);
if (ret2)
goto out_err;
if (!ret2)
invalidate_mapping_pages(iocb->ki_filp->f_mapping,
offset >> PAGE_SHIFT,
end >> PAGE_SHIFT);
}
} else {
ret = iomap_file_buffered_write(iocb, from, &ntfs_write_iomap_ops, NULL);
}
out:
if (ret < 0 && ret != -EIOCBQUEUED) {
out_err:
if (ni->initialized_size != old_init_size) {
mutex_lock(&ni->mrec_lock);
ntfs_attr_set_initialized_size(ni, old_init_size);
mutex_unlock(&ni->mrec_lock);
}
if (ni->data_size != old_data_size) {
truncate_setsize(vi, old_data_size);
ntfs_attr_truncate(ni, old_data_size);
}
}
out_lock:
inode_unlock(vi);
if (ret > 0)
ret = generic_write_sync(iocb, ret);
return ret;
}
static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
{
struct inode *inode = file_inode(vmf->vma->vm_file);
vm_fault_t ret;
if (unlikely(IS_IMMUTABLE(inode)))
return VM_FAULT_SIGBUS;
sb_start_pagefault(inode->i_sb);
file_update_time(vmf->vma->vm_file);
ret = iomap_page_mkwrite(vmf, &ntfs_page_mkwrite_iomap_ops);
sb_end_pagefault(inode->i_sb);
return ret;
}
static const struct vm_operations_struct ntfs_file_vm_ops = {
.fault = filemap_fault,
.map_pages = filemap_map_pages,
.page_mkwrite = ntfs_filemap_page_mkwrite,
};
static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
{
struct inode *inode = file_inode(file);
if (NVolShutdown(NTFS_SB(file->f_mapping->host->i_sb)))
return -EIO;
if (NInoCompressed(NTFS_I(inode)))
return -EOPNOTSUPP;
if (vma->vm_flags & VM_WRITE) {
struct inode *inode = file_inode(file);
loff_t from, to;
int err;
from = ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
to = min_t(loff_t, i_size_read(inode),
from + vma->vm_end - vma->vm_start);
if (NTFS_I(inode)->initialized_size < to) {
err = ntfs_extend_initialized_size(inode, to, to);
if (err)
return err;
}
}
file_accessed(file);
vma->vm_ops = &ntfs_file_vm_ops;
return 0;
}
static int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
u64 start, u64 len)
{
return iomap_fiemap(inode, fieinfo, start, len, &ntfs_read_iomap_ops);
}
static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode,
struct delayed_call *done)
{
if (!NTFS_I(inode)->target)
return ERR_PTR(-EINVAL);
return NTFS_I(inode)->target;
}
static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
struct pipe_inode_info *pipe, size_t len, unsigned int flags)
{
if (NVolShutdown(NTFS_SB(in->f_mapping->host->i_sb)))
return -EIO;
return filemap_splice_read(in, ppos, pipe, len, flags);
}
static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg)
{
u32 flags;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (get_user(flags, (__u32 __user *)arg))
return -EFAULT;
return ntfs_force_shutdown(sb, flags);
}
static int ntfs_ioctl_get_volume_label(struct file *filp, unsigned long arg)
{
struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb);
char __user *buf = (char __user *)arg;
if (!vol->volume_label) {
if (copy_to_user(buf, "", 1))
return -EFAULT;
} else if (copy_to_user(buf, vol->volume_label,
MIN(FSLABEL_MAX, strlen(vol->volume_label) + 1)))
return -EFAULT;
return 0;
}
static int ntfs_ioctl_set_volume_label(struct file *filp, unsigned long arg)
{
struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb);
char *label;
int ret;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
label = strndup_user((const char __user *)arg, FSLABEL_MAX);
if (IS_ERR(label))
return PTR_ERR(label);
ret = mnt_want_write_file(filp);
if (ret)
goto out;
ret = ntfs_write_volume_label(vol, label);
mnt_drop_write_file(filp);
out:
kfree(label);
return ret;
}
static int ntfs_ioctl_fitrim(struct ntfs_volume *vol, unsigned long arg)
{
struct fstrim_range __user *user_range;
struct fstrim_range range;
struct block_device *dev;
int err;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
dev = vol->sb->s_bdev;
if (!bdev_max_discard_sectors(dev))
return -EOPNOTSUPP;
user_range = (struct fstrim_range __user *)arg;
if (copy_from_user(&range, user_range, sizeof(range)))
return -EFAULT;
if (range.len == 0)
return -EINVAL;
if (range.len < vol->cluster_size)
return -EINVAL;
range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev));
err = ntfsp_trim_fs(vol, &range);
if (err < 0)
return err;
if (copy_to_user(user_range, &range, sizeof(range)))
return -EFAULT;
return 0;
}
long ntfsp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case NTFS_IOC_SHUTDOWN:
return ntfs_ioctl_shutdown(file_inode(filp)->i_sb, arg);
case FS_IOC_GETFSLABEL:
return ntfs_ioctl_get_volume_label(filp, arg);
case FS_IOC_SETFSLABEL:
return ntfs_ioctl_set_volume_label(filp, arg);
case FITRIM:
return ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)->i_sb), arg);
default:
return -ENOTTY;
}
}
#ifdef CONFIG_COMPAT
long ntfsp_compat_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
return ntfsp_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
}
#endif
static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
{
struct inode *vi = file_inode(file);
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_volume *vol = ni->vol;
int err = 0;
loff_t end_offset = offset + len;
loff_t old_size, new_size;
s64 start_vcn, end_vcn;
bool map_locked = false;
if (!S_ISREG(vi->i_mode))
return -EOPNOTSUPP;
if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_INSERT_RANGE |
FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE))
return -EOPNOTSUPP;
if (!NVolFreeClusterKnown(vol))
wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
if ((ni->vol->mft_zone_end - ni->vol->mft_zone_start) == 0)
return -ENOSPC;
if (NInoNonResident(ni) && !NInoFullyMapped(ni)) {
down_write(&ni->runlist.lock);
err = ntfs_attr_map_whole_runlist(ni);
up_write(&ni->runlist.lock);
if (err)
return err;
}
if (!(vol->vol_flags & VOLUME_IS_DIRTY)) {
err = ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
if (err)
return err;
}
old_size = i_size_read(vi);
new_size = max_t(loff_t, old_size, end_offset);
start_vcn = offset >> vol->cluster_size_bits;
end_vcn = ((end_offset - 1) >> vol->cluster_size_bits) + 1;
inode_lock(vi);
if (NInoCompressed(ni) || NInoEncrypted(ni)) {
err = -EOPNOTSUPP;
goto out;
}
inode_dio_wait(vi);
if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
FALLOC_FL_INSERT_RANGE)) {
filemap_invalidate_lock(vi->i_mapping);
map_locked = true;
}
if (mode & FALLOC_FL_INSERT_RANGE) {
loff_t offset_down = round_down(offset,
max_t(unsigned long, vol->cluster_size, PAGE_SIZE));
loff_t alloc_size;
if ((offset & vol->cluster_size_mask) ||
(len & vol->cluster_size_mask) ||
offset >= ni->allocated_size) {
err = -EINVAL;
goto out;
}
new_size = old_size +
((end_vcn - start_vcn) << vol->cluster_size_bits);
alloc_size = ni->allocated_size +
((end_vcn - start_vcn) << vol->cluster_size_bits);
if (alloc_size < 0) {
err = -EFBIG;
goto out;
}
err = inode_newsize_ok(vi, alloc_size);
if (err)
goto out;
err = filemap_write_and_wait_range(vi->i_mapping,
offset_down, LLONG_MAX);
if (err)
goto out;
truncate_pagecache(vi, offset_down);
mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_insert_range(ni, start_vcn,
end_vcn - start_vcn);
mutex_unlock(&ni->mrec_lock);
if (err)
goto out;
} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
loff_t offset_down = round_down(offset,
max_t(unsigned long, vol->cluster_size, PAGE_SIZE));
if ((offset & vol->cluster_size_mask) ||
(len & vol->cluster_size_mask) ||
offset >= ni->allocated_size) {
err = -EINVAL;
goto out;
}
if ((end_vcn << vol->cluster_size_bits) > ni->allocated_size)
end_vcn = DIV_ROUND_UP(ni->allocated_size - 1,
vol->cluster_size) + 1;
new_size = old_size -
((end_vcn - start_vcn) << vol->cluster_size_bits);
if (new_size < 0)
new_size = 0;
err = filemap_write_and_wait_range(vi->i_mapping,
offset_down, LLONG_MAX);
if (err)
goto out;
truncate_pagecache(vi, offset_down);
mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
err = ntfs_non_resident_attr_collapse_range(ni, start_vcn,
end_vcn - start_vcn);
mutex_unlock(&ni->mrec_lock);
if (err)
goto out;
} else if (mode & FALLOC_FL_PUNCH_HOLE) {
loff_t offset_down = round_down(offset, max_t(unsigned int,
vol->cluster_size, PAGE_SIZE));
if (!(mode & FALLOC_FL_KEEP_SIZE)) {
err = -EINVAL;
goto out;
}
if (offset >= ni->data_size)
goto out;
if (offset + len > ni->data_size) {
end_offset = ni->data_size;
end_vcn = ((end_offset - 1) >> vol->cluster_size_bits) + 1;
}
err = filemap_write_and_wait_range(vi->i_mapping, offset_down, LLONG_MAX);
if (err)