1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/buffer_head.h>
10 #include <linux/mpage.h>
11 #include <linux/namei.h>
12 #include <linux/nls.h>
13 #include <linux/uio.h>
14 #include <linux/writeback.h>
21 * ntfs_read_mft - Read record and parse MFT.
23 static struct inode
*ntfs_read_mft(struct inode
*inode
,
24 const struct cpu_str
*name
,
25 const struct MFT_REF
*ref
)
28 struct ntfs_inode
*ni
= ntfs_i(inode
);
29 struct super_block
*sb
= inode
->i_sb
;
30 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
32 struct ATTR_STD_INFO5
*std5
= NULL
;
33 struct ATTR_LIST_ENTRY
*le
;
35 bool is_match
= false;
38 unsigned long ino
= inode
->i_ino
;
39 u32 rp_fa
= 0, asize
, t32
;
40 u16 roff
, rsize
, names
= 0, links
= 0;
41 const struct ATTR_FILE_NAME
*fname
= NULL
;
42 const struct INDEX_ROOT
*root
;
43 struct REPARSE_DATA_BUFFER rp
; // 0x18 bytes
46 struct runs_tree
*run
;
50 /* Setup 'uid' and 'gid' */
51 inode
->i_uid
= sbi
->options
->fs_uid
;
52 inode
->i_gid
= sbi
->options
->fs_gid
;
54 err
= mi_init(&ni
->mi
, sbi
, ino
);
58 if (!sbi
->mft
.ni
&& ino
== MFT_REC_MFT
&& !sb
->s_root
) {
59 t64
= sbi
->mft
.lbo
>> sbi
->cluster_bits
;
60 t32
= bytes_to_cluster(sbi
, MFT_REC_VOL
* sbi
->record_size
);
62 init_rwsem(&ni
->file
.run_lock
);
64 if (!run_add_entry(&ni
->file
.run
, 0, t64
, t32
, true)) {
70 err
= mi_read(&ni
->mi
, ino
== MFT_REC_MFT
);
77 if (sbi
->flags
& NTFS_FLAGS_LOG_REPLAYING
) {
79 } else if (ref
->seq
!= rec
->seq
) {
81 ntfs_err(sb
, "MFT: r=%lx, expect seq=%x instead of %x!", ino
,
82 le16_to_cpu(ref
->seq
), le16_to_cpu(rec
->seq
));
84 } else if (!is_rec_inuse(rec
)) {
86 ntfs_err(sb
, "Inode r=%x is not in use!", (u32
)ino
);
90 if (le32_to_cpu(rec
->total
) != sbi
->record_size
) {
96 if (!is_rec_base(rec
)) {
101 /* Record should contain $I30 root. */
102 is_dir
= rec
->flags
& RECORD_FLAG_DIR
;
104 /* MFT_REC_MFT is not a dir */
105 if (is_dir
&& ino
== MFT_REC_MFT
) {
110 inode
->i_generation
= le16_to_cpu(rec
->seq
);
112 /* Enumerate all struct Attributes MFT. */
117 * To reduce tab pressure use goto instead of
118 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
123 attr
= ni_enum_attr_ex(ni
, attr
, &le
, NULL
);
128 /* This is non primary attribute segment. Ignore if not MFT. */
129 if (ino
!= MFT_REC_MFT
|| attr
->type
!= ATTR_DATA
)
133 asize
= le32_to_cpu(attr
->size
);
134 goto attr_unpack_run
;
137 roff
= attr
->non_res
? 0 : le16_to_cpu(attr
->res
.data_off
);
138 rsize
= attr
->non_res
? 0 : le32_to_cpu(attr
->res
.data_size
);
139 asize
= le32_to_cpu(attr
->size
);
142 * Really this check was done in 'ni_enum_attr_ex' -> ... 'mi_enum_attr'.
143 * There not critical to check this case again
145 if (attr
->name_len
&&
146 sizeof(short) * attr
->name_len
+ le16_to_cpu(attr
->name_off
) >
151 t64
= le64_to_cpu(attr
->nres
.alloc_size
);
152 if (le64_to_cpu(attr
->nres
.data_size
) > t64
||
153 le64_to_cpu(attr
->nres
.valid_size
) > t64
)
157 switch (attr
->type
) {
160 asize
< sizeof(struct ATTR_STD_INFO
) + roff
||
161 rsize
< sizeof(struct ATTR_STD_INFO
))
167 std5
= Add2Ptr(attr
, roff
);
170 nt2kernel(std5
->cr_time
, &ni
->i_crtime
);
172 nt2kernel(std5
->a_time
, &ts
);
173 inode_set_atime_to_ts(inode
, ts
);
174 nt2kernel(std5
->c_time
, &ts
);
175 inode_set_ctime_to_ts(inode
, ts
);
176 nt2kernel(std5
->m_time
, &ts
);
177 inode_set_mtime_to_ts(inode
, ts
);
179 ni
->std_fa
= std5
->fa
;
181 if (asize
>= sizeof(struct ATTR_STD_INFO5
) + roff
&&
182 rsize
>= sizeof(struct ATTR_STD_INFO5
))
183 ni
->std_security_id
= std5
->security_id
;
187 if (attr
->name_len
|| le
|| ino
== MFT_REC_LOG
)
190 err
= ntfs_load_attr_list(ni
, attr
);
199 if (attr
->non_res
|| asize
< SIZEOF_ATTRIBUTE_FILENAME
+ roff
||
200 rsize
< SIZEOF_ATTRIBUTE_FILENAME
)
204 fname
= Add2Ptr(attr
, roff
);
205 if (fname
->type
== FILE_NAME_DOS
)
209 if (name
&& name
->len
== fname
->name_len
&&
210 !ntfs_cmp_names_cpu(name
, (struct le_str
*)&fname
->name_len
,
218 /* Ignore data attribute in dir record. */
222 if (ino
== MFT_REC_BADCLUST
&& !attr
->non_res
)
225 if (attr
->name_len
&&
226 ((ino
!= MFT_REC_BADCLUST
|| !attr
->non_res
||
227 attr
->name_len
!= ARRAY_SIZE(BAD_NAME
) ||
228 memcmp(attr_name(attr
), BAD_NAME
, sizeof(BAD_NAME
))) &&
229 (ino
!= MFT_REC_SECURE
|| !attr
->non_res
||
230 attr
->name_len
!= ARRAY_SIZE(SDS_NAME
) ||
231 memcmp(attr_name(attr
), SDS_NAME
, sizeof(SDS_NAME
))))) {
232 /* File contains stream attribute. Ignore it. */
236 if (is_attr_sparsed(attr
))
237 ni
->std_fa
|= FILE_ATTRIBUTE_SPARSE_FILE
;
239 ni
->std_fa
&= ~FILE_ATTRIBUTE_SPARSE_FILE
;
241 if (is_attr_compressed(attr
))
242 ni
->std_fa
|= FILE_ATTRIBUTE_COMPRESSED
;
244 ni
->std_fa
&= ~FILE_ATTRIBUTE_COMPRESSED
;
246 if (is_attr_encrypted(attr
))
247 ni
->std_fa
|= FILE_ATTRIBUTE_ENCRYPTED
;
249 ni
->std_fa
&= ~FILE_ATTRIBUTE_ENCRYPTED
;
251 if (!attr
->non_res
) {
252 ni
->i_valid
= inode
->i_size
= rsize
;
253 inode_set_bytes(inode
, rsize
);
256 mode
= S_IFREG
| (0777 & sbi
->options
->fs_fmask_inv
);
258 if (!attr
->non_res
) {
259 ni
->ni_flags
|= NI_FLAG_RESIDENT
;
263 inode_set_bytes(inode
, attr_ondisk_size(attr
));
265 ni
->i_valid
= le64_to_cpu(attr
->nres
.valid_size
);
266 inode
->i_size
= le64_to_cpu(attr
->nres
.data_size
);
267 if (!attr
->nres
.alloc_size
)
270 run
= ino
== MFT_REC_BITMAP
? &sbi
->used
.bitmap
.run
:
278 root
= Add2Ptr(attr
, roff
);
280 if (attr
->name_len
!= ARRAY_SIZE(I30_NAME
) ||
281 memcmp(attr_name(attr
), I30_NAME
, sizeof(I30_NAME
)))
284 if (root
->type
!= ATTR_NAME
||
285 root
->rule
!= NTFS_COLLATION_TYPE_FILENAME
)
292 ni
->ni_flags
|= NI_FLAG_DIR
;
294 err
= indx_init(&ni
->dir
, sbi
, attr
, INDEX_MUTEX_I30
);
299 (S_IFDIR
| (0777 & sbi
->options
->fs_dmask_inv
)) :
304 if (!is_root
|| attr
->name_len
!= ARRAY_SIZE(I30_NAME
) ||
305 memcmp(attr_name(attr
), I30_NAME
, sizeof(I30_NAME
)))
308 inode
->i_size
= le64_to_cpu(attr
->nres
.data_size
);
309 ni
->i_valid
= le64_to_cpu(attr
->nres
.valid_size
);
310 inode_set_bytes(inode
, le64_to_cpu(attr
->nres
.alloc_size
));
312 run
= &ni
->dir
.alloc_run
;
316 if (ino
== MFT_REC_MFT
) {
319 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
320 /* 0x20000000 = 2^32 / 8 */
321 if (le64_to_cpu(attr
->nres
.alloc_size
) >= 0x20000000)
324 run
= &sbi
->mft
.bitmap
.run
;
326 } else if (is_dir
&& attr
->name_len
== ARRAY_SIZE(I30_NAME
) &&
327 !memcmp(attr_name(attr
), I30_NAME
,
330 run
= &ni
->dir
.bitmap_run
;
339 rp_fa
= ni_parse_reparse(ni
, attr
, &rp
);
344 * Assume one unicode symbol == one utf8.
346 inode
->i_size
= le16_to_cpu(rp
.SymbolicLinkReparseBuffer
349 ni
->i_valid
= inode
->i_size
;
350 /* Clear directory bit. */
351 if (ni
->ni_flags
& NI_FLAG_DIR
) {
352 indx_clear(&ni
->dir
);
353 memset(&ni
->dir
, 0, sizeof(ni
->dir
));
354 ni
->ni_flags
&= ~NI_FLAG_DIR
;
356 run_close(&ni
->file
.run
);
358 mode
= S_IFLNK
| 0777;
362 goto attr_unpack_run
; // Double break.
366 case REPARSE_COMPRESSED
:
369 case REPARSE_DEDUPLICATED
:
375 if (!attr
->name_len
&&
376 resident_data_ex(attr
, sizeof(struct EA_INFO
))) {
377 ni
->ni_flags
|= NI_FLAG_EA
;
379 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
381 inode
->i_mode
= mode
;
382 ntfs_get_wsl_perm(inode
);
383 mode
= inode
->i_mode
;
392 roff
= le16_to_cpu(attr
->nres
.run_off
);
399 t64
= le64_to_cpu(attr
->nres
.svcn
);
401 err
= run_unpack_ex(run
, sbi
, ino
, t64
, le64_to_cpu(attr
->nres
.evcn
),
402 t64
, Add2Ptr(attr
, roff
), asize
- roff
);
413 if (is_bad_inode(inode
))
416 if (!is_match
&& name
) {
421 if (std5
->fa
& FILE_ATTRIBUTE_READONLY
)
429 if (names
!= le16_to_cpu(rec
->hard_links
)) {
430 /* Correct minor error on the fly. Do not mark inode as dirty. */
431 ntfs_inode_warn(inode
, "Correct links count -> %u.", names
);
432 rec
->hard_links
= cpu_to_le16(names
);
436 set_nlink(inode
, links
);
439 ni
->std_fa
|= FILE_ATTRIBUTE_DIRECTORY
;
442 * Dot and dot-dot should be included in count but was not
443 * included in enumeration.
444 * Usually a hard links to directories are disabled.
446 inode
->i_op
= &ntfs_dir_inode_operations
;
447 inode
->i_fop
= unlikely(is_legacy_ntfs(sb
)) ?
448 &ntfs_legacy_dir_operations
:
449 &ntfs_dir_operations
;
451 } else if (S_ISLNK(mode
)) {
452 ni
->std_fa
&= ~FILE_ATTRIBUTE_DIRECTORY
;
453 inode
->i_op
= &ntfs_link_inode_operations
;
455 inode_nohighmem(inode
);
456 } else if (S_ISREG(mode
)) {
457 ni
->std_fa
&= ~FILE_ATTRIBUTE_DIRECTORY
;
458 inode
->i_op
= &ntfs_file_inode_operations
;
459 inode
->i_fop
= unlikely(is_legacy_ntfs(sb
)) ?
460 &ntfs_legacy_file_operations
:
461 &ntfs_file_operations
;
462 inode
->i_mapping
->a_ops
= is_compressed(ni
) ? &ntfs_aops_cmpr
:
464 if (ino
!= MFT_REC_MFT
)
465 init_rwsem(&ni
->file
.run_lock
);
466 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) || S_ISFIFO(mode
) ||
468 inode
->i_op
= &ntfs_special_inode_operations
;
469 init_special_inode(inode
, mode
, inode
->i_rdev
);
470 } else if (fname
&& fname
->home
.low
== cpu_to_le32(MFT_REC_EXTEND
) &&
471 fname
->home
.seq
== cpu_to_le16(MFT_REC_EXTEND
)) {
472 /* Records in $Extend are not a files or general directories. */
473 inode
->i_op
= &ntfs_file_inode_operations
;
479 if ((sbi
->options
->sys_immutable
&&
480 (std5
->fa
& FILE_ATTRIBUTE_SYSTEM
)) &&
481 !S_ISFIFO(mode
) && !S_ISSOCK(mode
) && !S_ISLNK(mode
)) {
482 inode
->i_flags
|= S_IMMUTABLE
;
484 inode
->i_flags
&= ~S_IMMUTABLE
;
487 inode
->i_mode
= mode
;
488 if (!(ni
->ni_flags
& NI_FLAG_EA
)) {
489 /* If no xattr then no security (stored in xattr). */
490 inode
->i_flags
|= S_NOSEC
;
493 if (ino
== MFT_REC_MFT
&& !sb
->s_root
)
496 unlock_new_inode(inode
);
501 if (ino
== MFT_REC_MFT
&& !sb
->s_root
)
511 * Return: 1 if match.
513 static int ntfs_test_inode(struct inode
*inode
, void *data
)
515 struct MFT_REF
*ref
= data
;
517 return ino_get(ref
) == inode
->i_ino
;
520 static int ntfs_set_inode(struct inode
*inode
, void *data
)
522 const struct MFT_REF
*ref
= data
;
524 inode
->i_ino
= ino_get(ref
);
528 struct inode
*ntfs_iget5(struct super_block
*sb
, const struct MFT_REF
*ref
,
529 const struct cpu_str
*name
)
533 inode
= iget5_locked(sb
, ino_get(ref
), ntfs_test_inode
, ntfs_set_inode
,
535 if (unlikely(!inode
))
536 return ERR_PTR(-ENOMEM
);
538 /* If this is a freshly allocated inode, need to read it now. */
539 if (inode
->i_state
& I_NEW
)
540 inode
= ntfs_read_mft(inode
, name
, ref
);
541 else if (ref
->seq
!= ntfs_i(inode
)->mi
.mrec
->seq
) {
543 * Sequence number is not expected.
544 * Looks like inode was reused but caller uses the old reference
547 inode
= ERR_PTR(-ESTALE
);
551 ntfs_set_state(sb
->s_fs_info
, NTFS_DIRTY_ERROR
);
557 GET_BLOCK_GENERAL
= 0,
558 GET_BLOCK_WRITE_BEGIN
= 1,
559 GET_BLOCK_DIRECT_IO_R
= 2,
560 GET_BLOCK_DIRECT_IO_W
= 3,
564 static noinline
int ntfs_get_block_vbo(struct inode
*inode
, u64 vbo
,
565 struct buffer_head
*bh
, int create
,
566 enum get_block_ctx ctx
)
568 struct super_block
*sb
= inode
->i_sb
;
569 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
570 struct ntfs_inode
*ni
= ntfs_i(inode
);
571 struct folio
*folio
= bh
->b_folio
;
572 u8 cluster_bits
= sbi
->cluster_bits
;
573 u32 block_size
= sb
->s_blocksize
;
574 u64 bytes
, lbo
, valid
;
580 /* Clear previous state. */
581 clear_buffer_new(bh
);
582 clear_buffer_uptodate(bh
);
584 if (is_resident(ni
)) {
585 bh
->b_blocknr
= RESIDENT_LCN
;
586 bh
->b_size
= block_size
;
588 /* direct io (read) or bmap call */
592 err
= attr_data_read_resident(ni
, folio
);
596 set_buffer_uptodate(bh
);
601 vcn
= vbo
>> cluster_bits
;
602 off
= vbo
& sbi
->cluster_mask
;
605 err
= attr_data_get_block(ni
, vcn
, 1, &lcn
, &len
, create
? &new : NULL
,
606 create
&& sbi
->cluster_size
> PAGE_SIZE
);
613 bytes
= ((u64
)len
<< cluster_bits
) - off
;
615 if (lcn
>= sbi
->used
.bitmap
.nbits
) {
616 /* This case includes resident/compressed/sparse. */
618 if (bh
->b_size
> bytes
)
628 lbo
= ((u64
)lcn
<< cluster_bits
) + off
;
630 set_buffer_mapped(bh
);
631 bh
->b_bdev
= sb
->s_bdev
;
632 bh
->b_blocknr
= lbo
>> sb
->s_blocksize_bits
;
636 if (ctx
== GET_BLOCK_DIRECT_IO_W
) {
637 /* ntfs_direct_IO will update ni->i_valid. */
642 if (bytes
> bh
->b_size
)
648 if (vbo
+ bytes
> valid
) {
649 ni
->i_valid
= vbo
+ bytes
;
650 mark_inode_dirty(inode
);
652 } else if (vbo
>= valid
) {
653 /* Read out of valid data. */
654 clear_buffer_mapped(bh
);
655 } else if (vbo
+ bytes
<= valid
) {
657 } else if (vbo
+ block_size
<= valid
) {
658 /* Normal short read. */
662 * Read across valid size: vbo < valid && valid < vbo + block_size
667 u32 voff
= valid
- vbo
;
669 bh
->b_size
= block_size
;
670 off
= vbo
& (PAGE_SIZE
- 1);
671 folio_set_bh(bh
, folio
, off
);
673 if (bh_read(bh
, 0) < 0) {
677 folio_zero_segment(folio
, off
+ voff
, off
+ block_size
);
681 if (bh
->b_size
> bytes
)
685 if (ctx
== GET_BLOCK_DIRECT_IO_W
|| ctx
== GET_BLOCK_DIRECT_IO_R
) {
686 static_assert(sizeof(size_t) < sizeof(loff_t
));
687 if (bytes
> 0x40000000u
)
688 bh
->b_size
= 0x40000000u
;
698 int ntfs_get_block(struct inode
*inode
, sector_t vbn
,
699 struct buffer_head
*bh_result
, int create
)
701 return ntfs_get_block_vbo(inode
, (u64
)vbn
<< inode
->i_blkbits
,
702 bh_result
, create
, GET_BLOCK_GENERAL
);
705 static int ntfs_get_block_bmap(struct inode
*inode
, sector_t vsn
,
706 struct buffer_head
*bh_result
, int create
)
708 return ntfs_get_block_vbo(inode
,
709 (u64
)vsn
<< inode
->i_sb
->s_blocksize_bits
,
710 bh_result
, create
, GET_BLOCK_BMAP
);
713 static sector_t
ntfs_bmap(struct address_space
*mapping
, sector_t block
)
715 return generic_block_bmap(mapping
, block
, ntfs_get_block_bmap
);
718 static int ntfs_read_folio(struct file
*file
, struct folio
*folio
)
721 struct address_space
*mapping
= folio
->mapping
;
722 struct inode
*inode
= mapping
->host
;
723 struct ntfs_inode
*ni
= ntfs_i(inode
);
725 if (is_resident(ni
)) {
727 err
= attr_data_read_resident(ni
, folio
);
729 if (err
!= E_NTFS_NONRESIDENT
) {
735 if (is_compressed(ni
)) {
737 err
= ni_readpage_cmpr(ni
, folio
);
742 /* Normal + sparse files. */
743 return mpage_read_folio(folio
, ntfs_get_block
);
746 static void ntfs_readahead(struct readahead_control
*rac
)
748 struct address_space
*mapping
= rac
->mapping
;
749 struct inode
*inode
= mapping
->host
;
750 struct ntfs_inode
*ni
= ntfs_i(inode
);
754 if (is_resident(ni
)) {
755 /* No readahead for resident. */
759 if (is_compressed(ni
)) {
760 /* No readahead for compressed. */
765 pos
= readahead_pos(rac
);
767 if (valid
< i_size_read(inode
) && pos
<= valid
&&
768 valid
< pos
+ readahead_length(rac
)) {
769 /* Range cross 'valid'. Read it page by page. */
773 mpage_readahead(rac
, ntfs_get_block
);
776 static int ntfs_get_block_direct_IO_R(struct inode
*inode
, sector_t iblock
,
777 struct buffer_head
*bh_result
, int create
)
779 return ntfs_get_block_vbo(inode
, (u64
)iblock
<< inode
->i_blkbits
,
780 bh_result
, create
, GET_BLOCK_DIRECT_IO_R
);
783 static int ntfs_get_block_direct_IO_W(struct inode
*inode
, sector_t iblock
,
784 struct buffer_head
*bh_result
, int create
)
786 return ntfs_get_block_vbo(inode
, (u64
)iblock
<< inode
->i_blkbits
,
787 bh_result
, create
, GET_BLOCK_DIRECT_IO_W
);
790 static ssize_t
ntfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
792 struct file
*file
= iocb
->ki_filp
;
793 struct address_space
*mapping
= file
->f_mapping
;
794 struct inode
*inode
= mapping
->host
;
795 struct ntfs_inode
*ni
= ntfs_i(inode
);
796 loff_t vbo
= iocb
->ki_pos
;
798 int wr
= iov_iter_rw(iter
) & WRITE
;
799 size_t iter_count
= iov_iter_count(iter
);
803 if (is_resident(ni
)) {
804 /* Switch to buffered write. */
809 ret
= blockdev_direct_IO(iocb
, inode
, iter
,
810 wr
? ntfs_get_block_direct_IO_W
:
811 ntfs_get_block_direct_IO_R
);
815 else if (wr
&& ret
== -EIOCBQUEUED
)
816 end
= vbo
+ iter_count
;
822 if (end
> valid
&& !S_ISBLK(inode
->i_mode
)) {
824 mark_inode_dirty(inode
);
826 } else if (vbo
< valid
&& valid
< end
) {
828 iov_iter_revert(iter
, end
- valid
);
829 iov_iter_zero(end
- valid
, iter
);
836 int ntfs_set_size(struct inode
*inode
, u64 new_size
)
838 struct super_block
*sb
= inode
->i_sb
;
839 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
840 struct ntfs_inode
*ni
= ntfs_i(inode
);
843 /* Check for maximum file size. */
844 if (is_sparsed(ni
) || is_compressed(ni
)) {
845 if (new_size
> sbi
->maxbytes_sparse
) {
849 } else if (new_size
> sbi
->maxbytes
) {
855 down_write(&ni
->file
.run_lock
);
857 err
= attr_set_size(ni
, ATTR_DATA
, NULL
, 0, &ni
->file
.run
, new_size
,
858 &ni
->i_valid
, true, NULL
);
860 up_write(&ni
->file
.run_lock
);
863 mark_inode_dirty(inode
);
869 static int ntfs_resident_writepage(struct folio
*folio
,
870 struct writeback_control
*wbc
, void *data
)
872 struct address_space
*mapping
= data
;
873 struct inode
*inode
= mapping
->host
;
874 struct ntfs_inode
*ni
= ntfs_i(inode
);
877 if (unlikely(ntfs3_forced_shutdown(inode
->i_sb
)))
881 ret
= attr_data_write_resident(ni
, folio
);
884 if (ret
!= E_NTFS_NONRESIDENT
)
886 mapping_set_error(mapping
, ret
);
890 static int ntfs_writepages(struct address_space
*mapping
,
891 struct writeback_control
*wbc
)
893 struct inode
*inode
= mapping
->host
;
895 if (unlikely(ntfs3_forced_shutdown(inode
->i_sb
)))
898 if (is_resident(ntfs_i(inode
)))
899 return write_cache_pages(mapping
, wbc
, ntfs_resident_writepage
,
901 return mpage_writepages(mapping
, wbc
, ntfs_get_block
);
904 static int ntfs_get_block_write_begin(struct inode
*inode
, sector_t vbn
,
905 struct buffer_head
*bh_result
, int create
)
907 return ntfs_get_block_vbo(inode
, (u64
)vbn
<< inode
->i_blkbits
,
908 bh_result
, create
, GET_BLOCK_WRITE_BEGIN
);
911 int ntfs_write_begin(struct file
*file
, struct address_space
*mapping
,
912 loff_t pos
, u32 len
, struct folio
**foliop
, void **fsdata
)
915 struct inode
*inode
= mapping
->host
;
916 struct ntfs_inode
*ni
= ntfs_i(inode
);
918 if (unlikely(ntfs3_forced_shutdown(inode
->i_sb
)))
921 if (is_resident(ni
)) {
922 struct folio
*folio
= __filemap_get_folio(
923 mapping
, pos
>> PAGE_SHIFT
, FGP_WRITEBEGIN
,
924 mapping_gfp_mask(mapping
));
927 err
= PTR_ERR(folio
);
932 err
= attr_data_read_resident(ni
, folio
);
942 if (err
!= E_NTFS_NONRESIDENT
)
946 err
= block_write_begin(mapping
, pos
, len
, foliop
,
947 ntfs_get_block_write_begin
);
954 * ntfs_write_end - Address_space_operations::write_end.
956 int ntfs_write_end(struct file
*file
, struct address_space
*mapping
, loff_t pos
,
957 u32 len
, u32 copied
, struct folio
*folio
, void *fsdata
)
959 struct inode
*inode
= mapping
->host
;
960 struct ntfs_inode
*ni
= ntfs_i(inode
);
961 u64 valid
= ni
->i_valid
;
965 if (is_resident(ni
)) {
967 err
= attr_data_write_resident(ni
, folio
);
970 struct buffer_head
*head
= folio_buffers(folio
);
972 /* Clear any buffers in folio. */
974 struct buffer_head
*bh
= head
;
977 clear_buffer_dirty(bh
);
978 clear_buffer_mapped(bh
);
979 set_buffer_uptodate(bh
);
980 } while (head
!= (bh
= bh
->b_this_page
));
982 folio_mark_uptodate(folio
);
988 err
= generic_write_end(file
, mapping
, pos
, len
, copied
, folio
,
993 if (!(ni
->std_fa
& FILE_ATTRIBUTE_ARCHIVE
)) {
994 inode_set_mtime_to_ts(inode
,
995 inode_set_ctime_current(inode
));
996 ni
->std_fa
|= FILE_ATTRIBUTE_ARCHIVE
;
1000 if (valid
!= ni
->i_valid
) {
1001 /* ni->i_valid is changed in ntfs_get_block_vbo. */
1005 if (pos
+ err
> inode
->i_size
) {
1006 i_size_write(inode
, pos
+ err
);
1011 mark_inode_dirty(inode
);
1017 int ntfs3_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1019 return _ni_write_inode(inode
, wbc
->sync_mode
== WB_SYNC_ALL
);
1022 int ntfs_sync_inode(struct inode
*inode
)
1024 return _ni_write_inode(inode
, 1);
1028 * writeback_inode - Helper function for ntfs_flush_inodes().
1030 * This writes both the inode and the file data blocks, waiting
1031 * for in flight data blocks before the start of the call. It
1032 * does not wait for any io started during the call.
1034 static int writeback_inode(struct inode
*inode
)
1036 int ret
= sync_inode_metadata(inode
, 0);
1039 ret
= filemap_fdatawrite(inode
->i_mapping
);
1046 * Write data and metadata corresponding to i1 and i2. The io is
1047 * started but we do not wait for any of it to finish.
1049 * filemap_flush() is used for the block device, so if there is a dirty
1050 * page for a block already in flight, we will not wait and start the
1053 int ntfs_flush_inodes(struct super_block
*sb
, struct inode
*i1
,
1059 ret
= writeback_inode(i1
);
1061 ret
= writeback_inode(i2
);
1063 ret
= filemap_flush(sb
->s_bdev_file
->f_mapping
);
1068 * Helper function to read file.
1070 int inode_read_data(struct inode
*inode
, void *data
, size_t bytes
)
1073 struct address_space
*mapping
= inode
->i_mapping
;
1075 for (idx
= 0; bytes
; idx
++) {
1076 size_t op
= bytes
> PAGE_SIZE
? PAGE_SIZE
: bytes
;
1077 struct page
*page
= read_mapping_page(mapping
, idx
, NULL
);
1081 return PTR_ERR(page
);
1083 kaddr
= kmap_atomic(page
);
1084 memcpy(data
, kaddr
, op
);
1085 kunmap_atomic(kaddr
);
1090 data
= Add2Ptr(data
, PAGE_SIZE
);
1096 * ntfs_reparse_bytes
1098 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1099 * for unicode string of @uni_len length.
1101 static inline u32
ntfs_reparse_bytes(u32 uni_len
)
1103 /* Header + unicode string + decorated unicode string. */
1104 return sizeof(short) * (2 * uni_len
+ 4) +
1105 offsetof(struct REPARSE_DATA_BUFFER
,
1106 SymbolicLinkReparseBuffer
.PathBuffer
);
1109 static struct REPARSE_DATA_BUFFER
*
1110 ntfs_create_reparse_buffer(struct ntfs_sb_info
*sbi
, const char *symname
,
1111 u32 size
, u16
*nsize
)
1114 struct REPARSE_DATA_BUFFER
*rp
;
1116 typeof(rp
->SymbolicLinkReparseBuffer
) *rs
;
1118 rp
= kzalloc(ntfs_reparse_bytes(2 * size
+ 2), GFP_NOFS
);
1120 return ERR_PTR(-ENOMEM
);
1122 rs
= &rp
->SymbolicLinkReparseBuffer
;
1123 rp_name
= rs
->PathBuffer
;
1125 /* Convert link name to UTF-16. */
1126 err
= ntfs_nls_to_utf16(sbi
, symname
, size
,
1127 (struct cpu_str
*)(rp_name
- 1), 2 * size
,
1128 UTF16_LITTLE_ENDIAN
);
1132 /* err = the length of unicode name of symlink. */
1133 *nsize
= ntfs_reparse_bytes(err
);
1135 if (*nsize
> sbi
->reparse
.max_size
) {
1140 /* Translate Linux '/' into Windows '\'. */
1141 for (i
= 0; i
< err
; i
++) {
1142 if (rp_name
[i
] == cpu_to_le16('/'))
1143 rp_name
[i
] = cpu_to_le16('\\');
1146 rp
->ReparseTag
= IO_REPARSE_TAG_SYMLINK
;
1147 rp
->ReparseDataLength
=
1148 cpu_to_le16(*nsize
- offsetof(struct REPARSE_DATA_BUFFER
,
1149 SymbolicLinkReparseBuffer
));
1151 /* PrintName + SubstituteName. */
1152 rs
->SubstituteNameOffset
= cpu_to_le16(sizeof(short) * err
);
1153 rs
->SubstituteNameLength
= cpu_to_le16(sizeof(short) * err
+ 8);
1154 rs
->PrintNameLength
= rs
->SubstituteNameOffset
;
1157 * TODO: Use relative path if possible to allow Windows to
1159 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1163 memmove(rp_name
+ err
+ 4, rp_name
, sizeof(short) * err
);
1165 /* Decorate SubstituteName. */
1167 rp_name
[0] = cpu_to_le16('\\');
1168 rp_name
[1] = cpu_to_le16('?');
1169 rp_name
[2] = cpu_to_le16('?');
1170 rp_name
[3] = cpu_to_le16('\\');
1175 return ERR_PTR(err
);
1181 * Helper function for:
1186 * - ntfs_atomic_open
1188 * NOTE: if fnd != NULL (ntfs_atomic_open) then @dir is locked
1190 int ntfs_create_inode(struct mnt_idmap
*idmap
, struct inode
*dir
,
1191 struct dentry
*dentry
, const struct cpu_str
*uni
,
1192 umode_t mode
, dev_t dev
, const char *symname
, u32 size
,
1193 struct ntfs_fnd
*fnd
)
1196 struct super_block
*sb
= dir
->i_sb
;
1197 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
1198 const struct qstr
*name
= &dentry
->d_name
;
1200 struct ntfs_inode
*dir_ni
= ntfs_i(dir
);
1201 struct ntfs_inode
*ni
= NULL
;
1202 struct inode
*inode
= NULL
;
1203 struct ATTRIB
*attr
;
1204 struct ATTR_STD_INFO5
*std5
;
1205 struct ATTR_FILE_NAME
*fname
;
1206 struct MFT_REC
*rec
;
1207 u32 asize
, dsize
, sd_size
;
1208 enum FILE_ATTRIBUTE fa
;
1209 __le32 security_id
= SECURITY_ID_INVALID
;
1212 u16 t16
, nsize
= 0, aid
= 0;
1213 struct INDEX_ROOT
*root
, *dir_root
;
1214 struct NTFS_DE
*e
, *new_de
= NULL
;
1215 struct REPARSE_DATA_BUFFER
*rp
= NULL
;
1216 bool rp_inserted
= false;
1218 /* New file will be resident or non resident. */
1219 const bool new_file_resident
= 1;
1222 ni_lock_dir(dir_ni
);
1224 dir_root
= indx_get_root(&dir_ni
->dir
, dir_ni
, NULL
, NULL
);
1230 if (S_ISDIR(mode
)) {
1231 /* Use parent's directory attributes. */
1232 fa
= dir_ni
->std_fa
| FILE_ATTRIBUTE_DIRECTORY
|
1233 FILE_ATTRIBUTE_ARCHIVE
;
1235 * By default child directory inherits parent attributes.
1236 * Root directory is hidden + system.
1237 * Make an exception for children in root.
1239 if (dir
->i_ino
== MFT_REC_ROOT
)
1240 fa
&= ~(FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
);
1241 } else if (S_ISLNK(mode
)) {
1242 /* It is good idea that link should be the same type (file/dir) as target */
1243 fa
= FILE_ATTRIBUTE_REPARSE_POINT
;
1246 * Linux: there are dir/file/symlink and so on.
1247 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1248 * It is good idea to create:
1249 * dir + reparse if 'symname' points to directory
1251 * file + reparse if 'symname' points to file
1252 * Unfortunately kern_path hangs if symname contains 'dir'.
1258 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1259 * struct inode *target = d_inode(path.dentry);
1261 * if (S_ISDIR(target->i_mode))
1262 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1263 * // if ( target->i_sb == sb ){
1264 * // use relative path?
1269 } else if (S_ISREG(mode
)) {
1270 if (sbi
->options
->sparse
) {
1271 /* Sparsed regular file, cause option 'sparse'. */
1272 fa
= FILE_ATTRIBUTE_SPARSE_FILE
|
1273 FILE_ATTRIBUTE_ARCHIVE
;
1274 } else if (dir_ni
->std_fa
& FILE_ATTRIBUTE_COMPRESSED
) {
1275 /* Compressed regular file, if parent is compressed. */
1276 fa
= FILE_ATTRIBUTE_COMPRESSED
| FILE_ATTRIBUTE_ARCHIVE
;
1278 /* Regular file, default attributes. */
1279 fa
= FILE_ATTRIBUTE_ARCHIVE
;
1282 fa
= FILE_ATTRIBUTE_ARCHIVE
;
1285 /* If option "hide_dot_files" then set hidden attribute for dot files. */
1286 if (sbi
->options
->hide_dot_files
&& name
->name
[0] == '.')
1287 fa
|= FILE_ATTRIBUTE_HIDDEN
;
1290 fa
|= FILE_ATTRIBUTE_READONLY
;
1292 /* Allocate PATH_MAX bytes. */
1293 new_de
= __getname();
1299 if (unlikely(ntfs3_forced_shutdown(sb
))) {
1304 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1305 ntfs_set_state(sbi
, NTFS_DIRTY_DIRTY
);
1307 /* Step 1: allocate and fill new mft record. */
1308 err
= ntfs_look_free_mft(sbi
, &ino
, false, NULL
, NULL
);
1312 ni
= ntfs_new_inode(sbi
, ino
, S_ISDIR(mode
) ? RECORD_FLAG_DIR
: 0);
1318 inode
= &ni
->vfs_inode
;
1319 inode_init_owner(idmap
, inode
, dir
, mode
);
1320 mode
= inode
->i_mode
;
1322 ni
->i_crtime
= current_time(inode
);
1325 rec
->hard_links
= cpu_to_le16(1);
1326 attr
= Add2Ptr(rec
, le16_to_cpu(rec
->attr_off
));
1328 /* Get default security id. */
1329 sd
= s_default_security
;
1330 sd_size
= sizeof(s_default_security
);
1332 if (is_ntfs3(sbi
)) {
1333 security_id
= dir_ni
->std_security_id
;
1334 if (le32_to_cpu(security_id
) < SECURITY_ID_FIRST
) {
1335 security_id
= sbi
->security
.def_security_id
;
1337 if (security_id
== SECURITY_ID_INVALID
&&
1338 !ntfs_insert_security(sbi
, sd
, sd_size
,
1339 &security_id
, NULL
))
1340 sbi
->security
.def_security_id
= security_id
;
1344 /* Insert standard info. */
1345 std5
= Add2Ptr(attr
, SIZEOF_RESIDENT
);
1347 if (security_id
== SECURITY_ID_INVALID
) {
1348 dsize
= sizeof(struct ATTR_STD_INFO
);
1350 dsize
= sizeof(struct ATTR_STD_INFO5
);
1351 std5
->security_id
= security_id
;
1352 ni
->std_security_id
= security_id
;
1354 asize
= SIZEOF_RESIDENT
+ dsize
;
1356 attr
->type
= ATTR_STD
;
1357 attr
->size
= cpu_to_le32(asize
);
1358 attr
->id
= cpu_to_le16(aid
++);
1359 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1360 attr
->res
.data_size
= cpu_to_le32(dsize
);
1362 std5
->cr_time
= std5
->m_time
= std5
->c_time
= std5
->a_time
=
1363 kernel2nt(&ni
->i_crtime
);
1365 std5
->fa
= ni
->std_fa
= fa
;
1367 attr
= Add2Ptr(attr
, asize
);
1369 /* Insert file name. */
1370 err
= fill_name_de(sbi
, new_de
, name
, uni
);
1374 mi_get_ref(&ni
->mi
, &new_de
->ref
);
1376 fname
= (struct ATTR_FILE_NAME
*)(new_de
+ 1);
1378 if (sbi
->options
->windows_names
&&
1379 !valid_windows_name(sbi
, (struct le_str
*)&fname
->name_len
)) {
1384 mi_get_ref(&dir_ni
->mi
, &fname
->home
);
1385 fname
->dup
.cr_time
= fname
->dup
.m_time
= fname
->dup
.c_time
=
1386 fname
->dup
.a_time
= std5
->cr_time
;
1387 fname
->dup
.alloc_size
= fname
->dup
.data_size
= 0;
1388 fname
->dup
.fa
= std5
->fa
;
1389 fname
->dup
.ea_size
= fname
->dup
.reparse
= 0;
1391 dsize
= le16_to_cpu(new_de
->key_size
);
1392 asize
= ALIGN(SIZEOF_RESIDENT
+ dsize
, 8);
1394 attr
->type
= ATTR_NAME
;
1395 attr
->size
= cpu_to_le32(asize
);
1396 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1397 attr
->res
.flags
= RESIDENT_FLAG_INDEXED
;
1398 attr
->id
= cpu_to_le16(aid
++);
1399 attr
->res
.data_size
= cpu_to_le32(dsize
);
1400 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), fname
, dsize
);
1402 attr
= Add2Ptr(attr
, asize
);
1404 if (security_id
== SECURITY_ID_INVALID
) {
1405 /* Insert security attribute. */
1406 asize
= SIZEOF_RESIDENT
+ ALIGN(sd_size
, 8);
1408 attr
->type
= ATTR_SECURE
;
1409 attr
->size
= cpu_to_le32(asize
);
1410 attr
->id
= cpu_to_le16(aid
++);
1411 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1412 attr
->res
.data_size
= cpu_to_le32(sd_size
);
1413 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), sd
, sd_size
);
1415 attr
= Add2Ptr(attr
, asize
);
1418 attr
->id
= cpu_to_le16(aid
++);
1419 if (fa
& FILE_ATTRIBUTE_DIRECTORY
) {
1421 * Regular directory or symlink to directory.
1422 * Create root attribute.
1424 dsize
= sizeof(struct INDEX_ROOT
) + sizeof(struct NTFS_DE
);
1425 asize
= sizeof(I30_NAME
) + SIZEOF_RESIDENT
+ dsize
;
1427 attr
->type
= ATTR_ROOT
;
1428 attr
->size
= cpu_to_le32(asize
);
1430 attr
->name_len
= ARRAY_SIZE(I30_NAME
);
1431 attr
->name_off
= SIZEOF_RESIDENT_LE
;
1432 attr
->res
.data_off
=
1433 cpu_to_le16(sizeof(I30_NAME
) + SIZEOF_RESIDENT
);
1434 attr
->res
.data_size
= cpu_to_le32(dsize
);
1435 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), I30_NAME
,
1438 root
= Add2Ptr(attr
, sizeof(I30_NAME
) + SIZEOF_RESIDENT
);
1439 memcpy(root
, dir_root
, offsetof(struct INDEX_ROOT
, ihdr
));
1440 root
->ihdr
.de_off
= cpu_to_le32(sizeof(struct INDEX_HDR
));
1441 root
->ihdr
.used
= cpu_to_le32(sizeof(struct INDEX_HDR
) +
1442 sizeof(struct NTFS_DE
));
1443 root
->ihdr
.total
= root
->ihdr
.used
;
1445 e
= Add2Ptr(root
, sizeof(struct INDEX_ROOT
));
1446 e
->size
= cpu_to_le16(sizeof(struct NTFS_DE
));
1447 e
->flags
= NTFS_IE_LAST
;
1448 } else if (S_ISLNK(mode
)) {
1451 * Create empty resident data attribute.
1453 asize
= SIZEOF_RESIDENT
;
1455 /* Insert empty ATTR_DATA */
1456 attr
->type
= ATTR_DATA
;
1457 attr
->size
= cpu_to_le32(SIZEOF_RESIDENT
);
1458 attr
->name_off
= SIZEOF_RESIDENT_LE
;
1459 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1460 } else if (!new_file_resident
&& S_ISREG(mode
)) {
1462 * Regular file. Create empty non resident data attribute.
1464 attr
->type
= ATTR_DATA
;
1466 attr
->nres
.evcn
= cpu_to_le64(-1ll);
1467 if (fa
& FILE_ATTRIBUTE_SPARSE_FILE
) {
1468 attr
->size
= cpu_to_le32(SIZEOF_NONRESIDENT_EX
+ 8);
1469 attr
->name_off
= SIZEOF_NONRESIDENT_EX_LE
;
1470 attr
->flags
= ATTR_FLAG_SPARSED
;
1471 asize
= SIZEOF_NONRESIDENT_EX
+ 8;
1472 } else if (fa
& FILE_ATTRIBUTE_COMPRESSED
) {
1473 attr
->size
= cpu_to_le32(SIZEOF_NONRESIDENT_EX
+ 8);
1474 attr
->name_off
= SIZEOF_NONRESIDENT_EX_LE
;
1475 attr
->flags
= ATTR_FLAG_COMPRESSED
;
1476 attr
->nres
.c_unit
= NTFS_LZNT_CUNIT
;
1477 asize
= SIZEOF_NONRESIDENT_EX
+ 8;
1479 attr
->size
= cpu_to_le32(SIZEOF_NONRESIDENT
+ 8);
1480 attr
->name_off
= SIZEOF_NONRESIDENT_LE
;
1481 asize
= SIZEOF_NONRESIDENT
+ 8;
1483 attr
->nres
.run_off
= attr
->name_off
;
1486 * Node. Create empty resident data attribute.
1488 attr
->type
= ATTR_DATA
;
1489 attr
->size
= cpu_to_le32(SIZEOF_RESIDENT
);
1490 attr
->name_off
= SIZEOF_RESIDENT_LE
;
1491 if (fa
& FILE_ATTRIBUTE_SPARSE_FILE
)
1492 attr
->flags
= ATTR_FLAG_SPARSED
;
1493 else if (fa
& FILE_ATTRIBUTE_COMPRESSED
)
1494 attr
->flags
= ATTR_FLAG_COMPRESSED
;
1495 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1496 asize
= SIZEOF_RESIDENT
;
1497 ni
->ni_flags
|= NI_FLAG_RESIDENT
;
1500 if (S_ISDIR(mode
)) {
1501 ni
->ni_flags
|= NI_FLAG_DIR
;
1502 err
= indx_init(&ni
->dir
, sbi
, attr
, INDEX_MUTEX_I30
);
1505 } else if (S_ISLNK(mode
)) {
1506 rp
= ntfs_create_reparse_buffer(sbi
, symname
, size
, &nsize
);
1515 * Insert ATTR_REPARSE.
1517 attr
= Add2Ptr(attr
, asize
);
1518 attr
->type
= ATTR_REPARSE
;
1519 attr
->id
= cpu_to_le16(aid
++);
1521 /* Resident or non resident? */
1522 asize
= ALIGN(SIZEOF_RESIDENT
+ nsize
, 8);
1523 t16
= PtrOffset(rec
, attr
);
1526 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1527 * It is good idea to keep extended attributes resident.
1529 if (asize
+ t16
+ 0x78 + 8 > sbi
->record_size
) {
1531 CLST clst
= bytes_to_cluster(sbi
, nsize
);
1533 /* Bytes per runs. */
1534 t16
= sbi
->record_size
- t16
- SIZEOF_NONRESIDENT
;
1537 attr
->nres
.evcn
= cpu_to_le64(clst
- 1);
1538 attr
->name_off
= SIZEOF_NONRESIDENT_LE
;
1539 attr
->nres
.run_off
= attr
->name_off
;
1540 attr
->nres
.data_size
= cpu_to_le64(nsize
);
1541 attr
->nres
.valid_size
= attr
->nres
.data_size
;
1542 attr
->nres
.alloc_size
=
1543 cpu_to_le64(ntfs_up_cluster(sbi
, nsize
));
1545 err
= attr_allocate_clusters(sbi
, &ni
->file
.run
, 0, 0,
1546 clst
, NULL
, ALLOCATE_DEF
,
1547 &alen
, 0, NULL
, NULL
);
1551 err
= run_pack(&ni
->file
.run
, 0, clst
,
1552 Add2Ptr(attr
, SIZEOF_NONRESIDENT
), t16
,
1562 asize
= SIZEOF_NONRESIDENT
+ ALIGN(err
, 8);
1563 /* Write non resident data. */
1564 err
= ntfs_sb_write_run(sbi
, &ni
->file
.run
, 0, rp
,
1569 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1570 attr
->res
.data_size
= cpu_to_le32(nsize
);
1571 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), rp
, nsize
);
1573 /* Size of symlink equals the length of input string. */
1574 inode
->i_size
= size
;
1576 attr
->size
= cpu_to_le32(asize
);
1578 err
= ntfs_insert_reparse(sbi
, IO_REPARSE_TAG_SYMLINK
,
1586 attr
= Add2Ptr(attr
, asize
);
1587 attr
->type
= ATTR_END
;
1589 rec
->used
= cpu_to_le32(PtrOffset(rec
, attr
) + 8);
1590 rec
->next_attr_id
= cpu_to_le16(aid
);
1592 inode
->i_generation
= le16_to_cpu(rec
->seq
);
1594 if (S_ISDIR(mode
)) {
1595 inode
->i_op
= &ntfs_dir_inode_operations
;
1596 inode
->i_fop
= unlikely(is_legacy_ntfs(sb
)) ?
1597 &ntfs_legacy_dir_operations
:
1598 &ntfs_dir_operations
;
1599 } else if (S_ISLNK(mode
)) {
1600 inode
->i_op
= &ntfs_link_inode_operations
;
1601 inode
->i_fop
= NULL
;
1602 inode
->i_mapping
->a_ops
= &ntfs_aops
;
1603 inode
->i_size
= size
;
1604 inode_nohighmem(inode
);
1605 } else if (S_ISREG(mode
)) {
1606 inode
->i_op
= &ntfs_file_inode_operations
;
1607 inode
->i_fop
= unlikely(is_legacy_ntfs(sb
)) ?
1608 &ntfs_legacy_file_operations
:
1609 &ntfs_file_operations
;
1610 inode
->i_mapping
->a_ops
= is_compressed(ni
) ? &ntfs_aops_cmpr
:
1612 init_rwsem(&ni
->file
.run_lock
);
1614 inode
->i_op
= &ntfs_special_inode_operations
;
1615 init_special_inode(inode
, mode
, dev
);
1618 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1619 if (!S_ISLNK(mode
) && (sb
->s_flags
& SB_POSIXACL
)) {
1620 err
= ntfs_init_acl(idmap
, inode
, dir
);
1626 inode
->i_flags
|= S_NOSEC
;
1630 * ntfs_init_acl and ntfs_save_wsl_perm update extended attribute.
1631 * The packed size of extended attribute is stored in direntry too.
1632 * 'fname' here points to inside new_de.
1634 err
= ntfs_save_wsl_perm(inode
, &fname
->dup
.ea_size
);
1639 * update ea_size in file_name attribute too.
1640 * Use ni_find_attr cause layout of MFT record may be changed
1641 * in ntfs_init_acl and ntfs_save_wsl_perm.
1643 attr
= ni_find_attr(ni
, NULL
, NULL
, ATTR_NAME
, NULL
, 0, NULL
, NULL
);
1645 struct ATTR_FILE_NAME
*fn
;
1647 fn
= resident_data_ex(attr
, SIZEOF_ATTRIBUTE_FILENAME
);
1649 fn
->dup
.ea_size
= fname
->dup
.ea_size
;
1652 /* We do not need to update parent directory later */
1653 ni
->ni_flags
&= ~NI_FLAG_UPDATE_PARENT
;
1655 /* Step 2: Add new name in index. */
1656 err
= indx_insert_entry(&dir_ni
->dir
, dir_ni
, new_de
, sbi
, fnd
, 0);
1661 * Call 'd_instantiate' after inode->i_op is set
1662 * but before finish_open.
1664 d_instantiate(dentry
, inode
);
1666 /* Set original time. inode times (i_ctime) may be changed in ntfs_init_acl. */
1667 inode_set_atime_to_ts(inode
, ni
->i_crtime
);
1668 inode_set_ctime_to_ts(inode
, ni
->i_crtime
);
1669 inode_set_mtime_to_ts(inode
, ni
->i_crtime
);
1670 inode_set_mtime_to_ts(dir
, ni
->i_crtime
);
1671 inode_set_ctime_to_ts(dir
, ni
->i_crtime
);
1673 mark_inode_dirty(dir
);
1674 mark_inode_dirty(inode
);
1680 attr
= ni_find_attr(ni
, NULL
, NULL
, ATTR_EA
, NULL
, 0, NULL
, NULL
);
1681 if (attr
&& attr
->non_res
) {
1682 /* Delete ATTR_EA, if non-resident. */
1683 struct runs_tree run
;
1685 attr_set_size(ni
, ATTR_EA
, NULL
, 0, &run
, 0, NULL
, false, NULL
);
1690 ntfs_remove_reparse(sbi
, IO_REPARSE_TAG_SYMLINK
, &new_de
->ref
);
1694 run_deallocate(sbi
, &ni
->file
.run
, false);
1697 clear_rec_inuse(rec
);
1699 ni
->mi
.dirty
= false;
1700 discard_new_inode(inode
);
1702 ntfs_mark_rec_free(sbi
, ino
, false);
1713 unlock_new_inode(inode
);
1718 int ntfs_link_inode(struct inode
*inode
, struct dentry
*dentry
)
1721 struct ntfs_inode
*ni
= ntfs_i(inode
);
1722 struct ntfs_sb_info
*sbi
= inode
->i_sb
->s_fs_info
;
1725 /* Allocate PATH_MAX bytes. */
1730 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1731 ntfs_set_state(sbi
, NTFS_DIRTY_DIRTY
);
1733 /* Construct 'de'. */
1734 err
= fill_name_de(sbi
, de
, &dentry
->d_name
, NULL
);
1738 err
= ni_add_name(ntfs_i(d_inode(dentry
->d_parent
)), ni
, de
);
1747 * inode_operations::unlink
1748 * inode_operations::rmdir
1750 int ntfs_unlink_inode(struct inode
*dir
, const struct dentry
*dentry
)
1753 struct ntfs_sb_info
*sbi
= dir
->i_sb
->s_fs_info
;
1754 struct inode
*inode
= d_inode(dentry
);
1755 struct ntfs_inode
*ni
= ntfs_i(inode
);
1756 struct ntfs_inode
*dir_ni
= ntfs_i(dir
);
1757 struct NTFS_DE
*de
, *de2
= NULL
;
1760 if (ntfs_is_meta_file(sbi
, ni
->mi
.rno
))
1763 /* Allocate PATH_MAX bytes. */
1770 if (S_ISDIR(inode
->i_mode
) && !dir_is_empty(inode
)) {
1775 err
= fill_name_de(sbi
, de
, &dentry
->d_name
, NULL
);
1780 err
= ni_remove_name(dir_ni
, ni
, de
, &de2
, &undo_remove
);
1784 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
1785 mark_inode_dirty(dir
);
1786 inode_set_ctime_to_ts(inode
, inode_get_ctime(dir
));
1788 mark_inode_dirty(inode
);
1789 } else if (!ni_remove_name_undo(dir_ni
, ni
, de
, de2
, undo_remove
)) {
1790 _ntfs_bad_inode(inode
);
1792 if (ni_is_dirty(dir
))
1793 mark_inode_dirty(dir
);
1794 if (ni_is_dirty(inode
))
1795 mark_inode_dirty(inode
);
1804 void ntfs_evict_inode(struct inode
*inode
)
1806 truncate_inode_pages_final(&inode
->i_data
);
1808 invalidate_inode_buffers(inode
);
1811 ni_clear(ntfs_i(inode
));
1815 * ntfs_translate_junction
1817 * Translate a Windows junction target to the Linux equivalent.
1818 * On junctions, targets are always absolute (they include the drive
1819 * letter). We have no way of knowing if the target is for the current
1820 * mounted device or not so we just assume it is.
1822 static int ntfs_translate_junction(const struct super_block
*sb
,
1823 const struct dentry
*link_de
, char *target
,
1824 int target_len
, int target_max
)
1826 int tl_len
, err
= target_len
;
1827 char *link_path_buffer
= NULL
, *link_path
;
1828 char *translated
= NULL
;
1832 link_path_buffer
= kmalloc(PATH_MAX
, GFP_NOFS
);
1833 if (!link_path_buffer
) {
1837 /* Get link path, relative to mount point */
1838 link_path
= dentry_path_raw(link_de
, link_path_buffer
, PATH_MAX
);
1839 if (IS_ERR(link_path
)) {
1840 ntfs_err(sb
, "Error getting link path");
1845 translated
= kmalloc(PATH_MAX
, GFP_NOFS
);
1851 /* Make translated path a relative path to mount point */
1852 strcpy(translated
, "./");
1853 ++link_path
; /* Skip leading / */
1854 for (tl_len
= sizeof("./") - 1; *link_path
; ++link_path
) {
1855 if (*link_path
== '/') {
1856 if (PATH_MAX
- tl_len
< sizeof("../")) {
1858 "Link path %s has too many components",
1863 strcpy(translated
+ tl_len
, "../");
1864 tl_len
+= sizeof("../") - 1;
1868 /* Skip drive letter */
1869 target_start
= target
;
1870 while (*target_start
&& *target_start
!= ':')
1873 if (!*target_start
) {
1874 ntfs_err(sb
, "Link target (%s) missing drive separator",
1880 /* Skip drive separator and leading /, if exists */
1881 target_start
+= 1 + (target_start
[1] == '/');
1882 copy_len
= target_len
- (target_start
- target
);
1884 if (PATH_MAX
- tl_len
<= copy_len
) {
1885 ntfs_err(sb
, "Link target %s too large for buffer (%d <= %d)",
1886 target_start
, PATH_MAX
- tl_len
, copy_len
);
1891 /* translated path has a trailing / and target_start does not */
1892 strcpy(translated
+ tl_len
, target_start
);
1894 if (target_max
<= tl_len
) {
1895 ntfs_err(sb
, "Target path %s too large for buffer (%d <= %d)",
1896 translated
, target_max
, tl_len
);
1900 strcpy(target
, translated
);
1904 kfree(link_path_buffer
);
1909 static noinline
int ntfs_readlink_hlp(const struct dentry
*link_de
,
1910 struct inode
*inode
, char *buffer
,
1913 int i
, err
= -EINVAL
;
1914 struct ntfs_inode
*ni
= ntfs_i(inode
);
1915 struct super_block
*sb
= inode
->i_sb
;
1916 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
1919 void *to_free
= NULL
;
1920 struct REPARSE_DATA_BUFFER
*rp
;
1921 const __le16
*uname
;
1922 struct ATTRIB
*attr
;
1924 /* Reparse data present. Try to parse it. */
1925 static_assert(!offsetof(struct REPARSE_DATA_BUFFER
, ReparseTag
));
1926 static_assert(sizeof(u32
) == sizeof(rp
->ReparseTag
));
1930 attr
= ni_find_attr(ni
, NULL
, NULL
, ATTR_REPARSE
, NULL
, 0, NULL
, NULL
);
1934 if (!attr
->non_res
) {
1935 rp
= resident_data_ex(attr
, sizeof(struct REPARSE_DATA_BUFFER
));
1938 size
= le32_to_cpu(attr
->res
.data_size
);
1940 size
= le64_to_cpu(attr
->nres
.data_size
);
1944 if (size
> sbi
->reparse
.max_size
|| size
<= sizeof(u32
))
1948 rp
= kmalloc(size
, GFP_NOFS
);
1954 /* Read into temporal buffer. */
1955 err
= ntfs_read_run_nb(sbi
, &ni
->file
.run
, 0, rp
, size
, NULL
);
1960 /* Microsoft Tag. */
1961 switch (rp
->ReparseTag
) {
1962 case IO_REPARSE_TAG_MOUNT_POINT
:
1963 /* Mount points and junctions. */
1964 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1965 if (size
<= offsetof(struct REPARSE_DATA_BUFFER
,
1966 MountPointReparseBuffer
.PathBuffer
))
1969 offsetof(struct REPARSE_DATA_BUFFER
,
1970 MountPointReparseBuffer
.PathBuffer
) +
1971 le16_to_cpu(rp
->MountPointReparseBuffer
1973 ulen
= le16_to_cpu(rp
->MountPointReparseBuffer
.PrintNameLength
);
1976 case IO_REPARSE_TAG_SYMLINK
:
1977 /* FolderSymbolicLink */
1978 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1979 if (size
<= offsetof(struct REPARSE_DATA_BUFFER
,
1980 SymbolicLinkReparseBuffer
.PathBuffer
))
1983 rp
, offsetof(struct REPARSE_DATA_BUFFER
,
1984 SymbolicLinkReparseBuffer
.PathBuffer
) +
1985 le16_to_cpu(rp
->SymbolicLinkReparseBuffer
1988 rp
->SymbolicLinkReparseBuffer
.PrintNameLength
);
1991 case IO_REPARSE_TAG_CLOUD
:
1992 case IO_REPARSE_TAG_CLOUD_1
:
1993 case IO_REPARSE_TAG_CLOUD_2
:
1994 case IO_REPARSE_TAG_CLOUD_3
:
1995 case IO_REPARSE_TAG_CLOUD_4
:
1996 case IO_REPARSE_TAG_CLOUD_5
:
1997 case IO_REPARSE_TAG_CLOUD_6
:
1998 case IO_REPARSE_TAG_CLOUD_7
:
1999 case IO_REPARSE_TAG_CLOUD_8
:
2000 case IO_REPARSE_TAG_CLOUD_9
:
2001 case IO_REPARSE_TAG_CLOUD_A
:
2002 case IO_REPARSE_TAG_CLOUD_B
:
2003 case IO_REPARSE_TAG_CLOUD_C
:
2004 case IO_REPARSE_TAG_CLOUD_D
:
2005 case IO_REPARSE_TAG_CLOUD_E
:
2006 case IO_REPARSE_TAG_CLOUD_F
:
2007 err
= sizeof("OneDrive") - 1;
2010 memcpy(buffer
, "OneDrive", err
);
2014 if (IsReparseTagMicrosoft(rp
->ReparseTag
)) {
2015 /* Unknown Microsoft Tag. */
2018 if (!IsReparseTagNameSurrogate(rp
->ReparseTag
) ||
2019 size
<= sizeof(struct REPARSE_POINT
)) {
2024 uname
= Add2Ptr(rp
, sizeof(struct REPARSE_POINT
));
2025 ulen
= le16_to_cpu(rp
->ReparseDataLength
) -
2026 sizeof(struct REPARSE_POINT
);
2029 /* Convert nlen from bytes to UNICODE chars. */
2032 /* Check that name is available. */
2033 if (!ulen
|| uname
+ ulen
> (__le16
*)Add2Ptr(rp
, size
))
2036 /* If name is already zero terminated then truncate it now. */
2037 if (!uname
[ulen
- 1])
2040 err
= ntfs_utf16_to_nls(sbi
, uname
, ulen
, buffer
, buflen
);
2045 /* Translate Windows '\' into Linux '/'. */
2046 for (i
= 0; i
< err
; i
++) {
2047 if (buffer
[i
] == '\\')
2051 /* Always set last zero. */
2054 /* If this is a junction, translate the link target. */
2055 if (rp
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
2056 err
= ntfs_translate_junction(sb
, link_de
, buffer
, err
, buflen
);
2063 static const char *ntfs_get_link(struct dentry
*de
, struct inode
*inode
,
2064 struct delayed_call
*done
)
2070 return ERR_PTR(-ECHILD
);
2072 ret
= kmalloc(PAGE_SIZE
, GFP_NOFS
);
2074 return ERR_PTR(-ENOMEM
);
2076 err
= ntfs_readlink_hlp(de
, inode
, ret
, PAGE_SIZE
);
2079 return ERR_PTR(err
);
2082 set_delayed_call(done
, kfree_link
, ret
);
2088 const struct inode_operations ntfs_link_inode_operations
= {
2089 .get_link
= ntfs_get_link
,
2090 .setattr
= ntfs_setattr
,
2091 .listxattr
= ntfs_listxattr
,
2094 const struct address_space_operations ntfs_aops
= {
2095 .read_folio
= ntfs_read_folio
,
2096 .readahead
= ntfs_readahead
,
2097 .writepages
= ntfs_writepages
,
2098 .write_begin
= ntfs_write_begin
,
2099 .write_end
= ntfs_write_end
,
2100 .direct_IO
= ntfs_direct_IO
,
2102 .dirty_folio
= block_dirty_folio
,
2103 .migrate_folio
= buffer_migrate_folio
,
2104 .invalidate_folio
= block_invalidate_folio
,
2107 const struct address_space_operations ntfs_aops_cmpr
= {
2108 .read_folio
= ntfs_read_folio
,
2109 .readahead
= ntfs_readahead
,
2110 .dirty_folio
= block_dirty_folio
,