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 parses 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;
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
;
49 /* Setup 'uid' and 'gid' */
50 inode
->i_uid
= sbi
->options
->fs_uid
;
51 inode
->i_gid
= sbi
->options
->fs_gid
;
53 err
= mi_init(&ni
->mi
, sbi
, ino
);
57 if (!sbi
->mft
.ni
&& ino
== MFT_REC_MFT
&& !sb
->s_root
) {
58 t64
= sbi
->mft
.lbo
>> sbi
->cluster_bits
;
59 t32
= bytes_to_cluster(sbi
, MFT_REC_VOL
* sbi
->record_size
);
61 init_rwsem(&ni
->file
.run_lock
);
63 if (!run_add_entry(&ni
->file
.run
, 0, t64
, t32
, true)) {
69 err
= mi_read(&ni
->mi
, ino
== MFT_REC_MFT
);
76 if (sbi
->flags
& NTFS_FLAGS_LOG_REPLAYING
) {
78 } else if (ref
->seq
!= rec
->seq
) {
80 ntfs_err(sb
, "MFT: r=%lx, expect seq=%x instead of %x!", ino
,
81 le16_to_cpu(ref
->seq
), le16_to_cpu(rec
->seq
));
83 } else if (!is_rec_inuse(rec
)) {
85 ntfs_err(sb
, "Inode r=%x is not in use!", (u32
)ino
);
89 if (le32_to_cpu(rec
->total
) != sbi
->record_size
) {
95 if (!is_rec_base(rec
)) {
100 /* Record should contain $I30 root. */
101 is_dir
= rec
->flags
& RECORD_FLAG_DIR
;
103 inode
->i_generation
= le16_to_cpu(rec
->seq
);
105 /* Enumerate all struct Attributes MFT. */
110 * To reduce tab pressure use goto instead of
111 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
116 attr
= ni_enum_attr_ex(ni
, attr
, &le
, NULL
);
121 /* This is non primary attribute segment. Ignore if not MFT. */
122 if (ino
!= MFT_REC_MFT
|| attr
->type
!= ATTR_DATA
)
126 asize
= le32_to_cpu(attr
->size
);
127 goto attr_unpack_run
;
130 roff
= attr
->non_res
? 0 : le16_to_cpu(attr
->res
.data_off
);
131 rsize
= attr
->non_res
? 0 : le32_to_cpu(attr
->res
.data_size
);
132 asize
= le32_to_cpu(attr
->size
);
134 if (le16_to_cpu(attr
->name_off
) + attr
->name_len
> asize
)
138 t64
= le64_to_cpu(attr
->nres
.alloc_size
);
139 if (le64_to_cpu(attr
->nres
.data_size
) > t64
||
140 le64_to_cpu(attr
->nres
.valid_size
) > t64
)
144 switch (attr
->type
) {
147 asize
< sizeof(struct ATTR_STD_INFO
) + roff
||
148 rsize
< sizeof(struct ATTR_STD_INFO
))
154 std5
= Add2Ptr(attr
, roff
);
157 nt2kernel(std5
->cr_time
, &ni
->i_crtime
);
159 nt2kernel(std5
->a_time
, &inode
->i_atime
);
160 nt2kernel(std5
->c_time
, &inode
->i_ctime
);
161 nt2kernel(std5
->m_time
, &inode
->i_mtime
);
163 ni
->std_fa
= std5
->fa
;
165 if (asize
>= sizeof(struct ATTR_STD_INFO5
) + roff
&&
166 rsize
>= sizeof(struct ATTR_STD_INFO5
))
167 ni
->std_security_id
= std5
->security_id
;
171 if (attr
->name_len
|| le
|| ino
== MFT_REC_LOG
)
174 err
= ntfs_load_attr_list(ni
, attr
);
183 if (attr
->non_res
|| asize
< SIZEOF_ATTRIBUTE_FILENAME
+ roff
||
184 rsize
< SIZEOF_ATTRIBUTE_FILENAME
)
187 fname
= Add2Ptr(attr
, roff
);
188 if (fname
->type
== FILE_NAME_DOS
)
192 if (name
&& name
->len
== fname
->name_len
&&
193 !ntfs_cmp_names_cpu(name
, (struct le_str
*)&fname
->name_len
,
201 /* Ignore data attribute in dir record. */
205 if (ino
== MFT_REC_BADCLUST
&& !attr
->non_res
)
208 if (attr
->name_len
&&
209 ((ino
!= MFT_REC_BADCLUST
|| !attr
->non_res
||
210 attr
->name_len
!= ARRAY_SIZE(BAD_NAME
) ||
211 memcmp(attr_name(attr
), BAD_NAME
, sizeof(BAD_NAME
))) &&
212 (ino
!= MFT_REC_SECURE
|| !attr
->non_res
||
213 attr
->name_len
!= ARRAY_SIZE(SDS_NAME
) ||
214 memcmp(attr_name(attr
), SDS_NAME
, sizeof(SDS_NAME
))))) {
215 /* File contains stream attribute. Ignore it. */
219 if (is_attr_sparsed(attr
))
220 ni
->std_fa
|= FILE_ATTRIBUTE_SPARSE_FILE
;
222 ni
->std_fa
&= ~FILE_ATTRIBUTE_SPARSE_FILE
;
224 if (is_attr_compressed(attr
))
225 ni
->std_fa
|= FILE_ATTRIBUTE_COMPRESSED
;
227 ni
->std_fa
&= ~FILE_ATTRIBUTE_COMPRESSED
;
229 if (is_attr_encrypted(attr
))
230 ni
->std_fa
|= FILE_ATTRIBUTE_ENCRYPTED
;
232 ni
->std_fa
&= ~FILE_ATTRIBUTE_ENCRYPTED
;
234 if (!attr
->non_res
) {
235 ni
->i_valid
= inode
->i_size
= rsize
;
236 inode_set_bytes(inode
, rsize
);
239 mode
= S_IFREG
| (0777 & sbi
->options
->fs_fmask_inv
);
241 if (!attr
->non_res
) {
242 ni
->ni_flags
|= NI_FLAG_RESIDENT
;
246 inode_set_bytes(inode
, attr_ondisk_size(attr
));
248 ni
->i_valid
= le64_to_cpu(attr
->nres
.valid_size
);
249 inode
->i_size
= le64_to_cpu(attr
->nres
.data_size
);
250 if (!attr
->nres
.alloc_size
)
253 run
= ino
== MFT_REC_BITMAP
? &sbi
->used
.bitmap
.run
261 root
= Add2Ptr(attr
, roff
);
264 if (attr
->name_len
!= ARRAY_SIZE(I30_NAME
) ||
265 memcmp(attr_name(attr
), I30_NAME
, sizeof(I30_NAME
)))
268 if (root
->type
!= ATTR_NAME
||
269 root
->rule
!= NTFS_COLLATION_TYPE_FILENAME
)
275 ni
->ni_flags
|= NI_FLAG_DIR
;
277 err
= indx_init(&ni
->dir
, sbi
, attr
, INDEX_MUTEX_I30
);
282 ? (S_IFDIR
| (0777 & sbi
->options
->fs_dmask_inv
))
287 if (!is_root
|| attr
->name_len
!= ARRAY_SIZE(I30_NAME
) ||
288 memcmp(attr_name(attr
), I30_NAME
, sizeof(I30_NAME
)))
291 inode
->i_size
= le64_to_cpu(attr
->nres
.data_size
);
292 ni
->i_valid
= le64_to_cpu(attr
->nres
.valid_size
);
293 inode_set_bytes(inode
, le64_to_cpu(attr
->nres
.alloc_size
));
295 run
= &ni
->dir
.alloc_run
;
299 if (ino
== MFT_REC_MFT
) {
302 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
303 /* 0x20000000 = 2^32 / 8 */
304 if (le64_to_cpu(attr
->nres
.alloc_size
) >= 0x20000000)
307 run
= &sbi
->mft
.bitmap
.run
;
309 } else if (is_dir
&& attr
->name_len
== ARRAY_SIZE(I30_NAME
) &&
310 !memcmp(attr_name(attr
), I30_NAME
,
313 run
= &ni
->dir
.bitmap_run
;
322 rp_fa
= ni_parse_reparse(ni
, attr
, &rp
);
327 * Assume one unicode symbol == one utf8.
329 inode
->i_size
= le16_to_cpu(rp
.SymbolicLinkReparseBuffer
333 ni
->i_valid
= inode
->i_size
;
335 /* Clear directory bit. */
336 if (ni
->ni_flags
& NI_FLAG_DIR
) {
337 indx_clear(&ni
->dir
);
338 memset(&ni
->dir
, 0, sizeof(ni
->dir
));
339 ni
->ni_flags
&= ~NI_FLAG_DIR
;
341 run_close(&ni
->file
.run
);
343 mode
= S_IFLNK
| 0777;
347 goto attr_unpack_run
; // Double break.
351 case REPARSE_COMPRESSED
:
354 case REPARSE_DEDUPLICATED
:
360 if (!attr
->name_len
&&
361 resident_data_ex(attr
, sizeof(struct EA_INFO
))) {
362 ni
->ni_flags
|= NI_FLAG_EA
;
364 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
366 inode
->i_mode
= mode
;
367 ntfs_get_wsl_perm(inode
);
368 mode
= inode
->i_mode
;
377 roff
= le16_to_cpu(attr
->nres
.run_off
);
384 t64
= le64_to_cpu(attr
->nres
.svcn
);
386 err
= run_unpack_ex(run
, sbi
, ino
, t64
, le64_to_cpu(attr
->nres
.evcn
),
387 t64
, Add2Ptr(attr
, roff
), asize
- roff
);
398 if (!is_match
&& name
) {
399 /* Reuse rec as buffer for ascii name. */
404 if (std5
->fa
& FILE_ATTRIBUTE_READONLY
)
412 if (names
!= le16_to_cpu(rec
->hard_links
)) {
413 /* Correct minor error on the fly. Do not mark inode as dirty. */
414 rec
->hard_links
= cpu_to_le16(names
);
418 set_nlink(inode
, names
);
421 ni
->std_fa
|= FILE_ATTRIBUTE_DIRECTORY
;
424 * Dot and dot-dot should be included in count but was not
425 * included in enumeration.
426 * Usually a hard links to directories are disabled.
428 inode
->i_op
= &ntfs_dir_inode_operations
;
429 inode
->i_fop
= &ntfs_dir_operations
;
431 } else if (S_ISLNK(mode
)) {
432 ni
->std_fa
&= ~FILE_ATTRIBUTE_DIRECTORY
;
433 inode
->i_op
= &ntfs_link_inode_operations
;
435 inode_nohighmem(inode
);
436 } else if (S_ISREG(mode
)) {
437 ni
->std_fa
&= ~FILE_ATTRIBUTE_DIRECTORY
;
438 inode
->i_op
= &ntfs_file_inode_operations
;
439 inode
->i_fop
= &ntfs_file_operations
;
440 inode
->i_mapping
->a_ops
=
441 is_compressed(ni
) ? &ntfs_aops_cmpr
: &ntfs_aops
;
442 if (ino
!= MFT_REC_MFT
)
443 init_rwsem(&ni
->file
.run_lock
);
444 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) || S_ISFIFO(mode
) ||
446 inode
->i_op
= &ntfs_special_inode_operations
;
447 init_special_inode(inode
, mode
, inode
->i_rdev
);
448 } else if (fname
&& fname
->home
.low
== cpu_to_le32(MFT_REC_EXTEND
) &&
449 fname
->home
.seq
== cpu_to_le16(MFT_REC_EXTEND
)) {
450 /* Records in $Extend are not a files or general directories. */
451 inode
->i_op
= &ntfs_file_inode_operations
;
457 if ((sbi
->options
->sys_immutable
&&
458 (std5
->fa
& FILE_ATTRIBUTE_SYSTEM
)) &&
459 !S_ISFIFO(mode
) && !S_ISSOCK(mode
) && !S_ISLNK(mode
)) {
460 inode
->i_flags
|= S_IMMUTABLE
;
462 inode
->i_flags
&= ~S_IMMUTABLE
;
465 inode
->i_mode
= mode
;
466 if (!(ni
->ni_flags
& NI_FLAG_EA
)) {
467 /* If no xattr then no security (stored in xattr). */
468 inode
->i_flags
|= S_NOSEC
;
471 if (ino
== MFT_REC_MFT
&& !sb
->s_root
)
474 unlock_new_inode(inode
);
479 if (ino
== MFT_REC_MFT
&& !sb
->s_root
)
489 * Return: 1 if match.
491 static int ntfs_test_inode(struct inode
*inode
, void *data
)
493 struct MFT_REF
*ref
= data
;
495 return ino_get(ref
) == inode
->i_ino
;
498 static int ntfs_set_inode(struct inode
*inode
, void *data
)
500 const struct MFT_REF
*ref
= data
;
502 inode
->i_ino
= ino_get(ref
);
506 struct inode
*ntfs_iget5(struct super_block
*sb
, const struct MFT_REF
*ref
,
507 const struct cpu_str
*name
)
511 inode
= iget5_locked(sb
, ino_get(ref
), ntfs_test_inode
, ntfs_set_inode
,
513 if (unlikely(!inode
))
514 return ERR_PTR(-ENOMEM
);
516 /* If this is a freshly allocated inode, need to read it now. */
517 if (inode
->i_state
& I_NEW
)
518 inode
= ntfs_read_mft(inode
, name
, ref
);
519 else if (ref
->seq
!= ntfs_i(inode
)->mi
.mrec
->seq
) {
520 /* Inode overlaps? */
521 _ntfs_bad_inode(inode
);
524 if (IS_ERR(inode
) && name
)
525 ntfs_set_state(sb
->s_fs_info
, NTFS_DIRTY_ERROR
);
531 GET_BLOCK_GENERAL
= 0,
532 GET_BLOCK_WRITE_BEGIN
= 1,
533 GET_BLOCK_DIRECT_IO_R
= 2,
534 GET_BLOCK_DIRECT_IO_W
= 3,
538 static noinline
int ntfs_get_block_vbo(struct inode
*inode
, u64 vbo
,
539 struct buffer_head
*bh
, int create
,
540 enum get_block_ctx ctx
)
542 struct super_block
*sb
= inode
->i_sb
;
543 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
544 struct ntfs_inode
*ni
= ntfs_i(inode
);
545 struct page
*page
= bh
->b_page
;
546 u8 cluster_bits
= sbi
->cluster_bits
;
547 u32 block_size
= sb
->s_blocksize
;
548 u64 bytes
, lbo
, valid
;
554 /* Clear previous state. */
555 clear_buffer_new(bh
);
556 clear_buffer_uptodate(bh
);
558 if (is_resident(ni
)) {
560 err
= attr_data_read_resident(ni
, page
);
564 set_buffer_uptodate(bh
);
565 bh
->b_size
= block_size
;
569 vcn
= vbo
>> cluster_bits
;
570 off
= vbo
& sbi
->cluster_mask
;
573 err
= attr_data_get_block(ni
, vcn
, 1, &lcn
, &len
, create
? &new : NULL
,
574 create
&& sbi
->cluster_size
> PAGE_SIZE
);
581 bytes
= ((u64
)len
<< cluster_bits
) - off
;
583 if (lcn
== SPARSE_LCN
) {
585 if (bh
->b_size
> bytes
)
595 lbo
= ((u64
)lcn
<< cluster_bits
) + off
;
597 set_buffer_mapped(bh
);
598 bh
->b_bdev
= sb
->s_bdev
;
599 bh
->b_blocknr
= lbo
>> sb
->s_blocksize_bits
;
603 if (ctx
== GET_BLOCK_DIRECT_IO_W
) {
604 /* ntfs_direct_IO will update ni->i_valid. */
609 if (bytes
> bh
->b_size
)
615 if (vbo
+ bytes
> valid
) {
616 ni
->i_valid
= vbo
+ bytes
;
617 mark_inode_dirty(inode
);
619 } else if (vbo
>= valid
) {
620 /* Read out of valid data. */
621 clear_buffer_mapped(bh
);
622 } else if (vbo
+ bytes
<= valid
) {
624 } else if (vbo
+ block_size
<= valid
) {
625 /* Normal short read. */
629 * Read across valid size: vbo < valid && valid < vbo + block_size
634 u32 voff
= valid
- vbo
;
636 bh
->b_size
= block_size
;
637 off
= vbo
& (PAGE_SIZE
- 1);
638 set_bh_page(bh
, page
, off
);
639 err
= bh_read(bh
, 0);
642 zero_user_segment(page
, off
+ voff
, off
+ block_size
);
646 if (bh
->b_size
> bytes
)
650 if (ctx
== GET_BLOCK_DIRECT_IO_W
|| ctx
== GET_BLOCK_DIRECT_IO_R
) {
651 static_assert(sizeof(size_t) < sizeof(loff_t
));
652 if (bytes
> 0x40000000u
)
653 bh
->b_size
= 0x40000000u
;
663 int ntfs_get_block(struct inode
*inode
, sector_t vbn
,
664 struct buffer_head
*bh_result
, int create
)
666 return ntfs_get_block_vbo(inode
, (u64
)vbn
<< inode
->i_blkbits
,
667 bh_result
, create
, GET_BLOCK_GENERAL
);
670 static int ntfs_get_block_bmap(struct inode
*inode
, sector_t vsn
,
671 struct buffer_head
*bh_result
, int create
)
673 return ntfs_get_block_vbo(inode
,
674 (u64
)vsn
<< inode
->i_sb
->s_blocksize_bits
,
675 bh_result
, create
, GET_BLOCK_BMAP
);
678 static sector_t
ntfs_bmap(struct address_space
*mapping
, sector_t block
)
680 return generic_block_bmap(mapping
, block
, ntfs_get_block_bmap
);
683 static int ntfs_read_folio(struct file
*file
, struct folio
*folio
)
685 struct page
*page
= &folio
->page
;
687 struct address_space
*mapping
= page
->mapping
;
688 struct inode
*inode
= mapping
->host
;
689 struct ntfs_inode
*ni
= ntfs_i(inode
);
691 if (is_resident(ni
)) {
693 err
= attr_data_read_resident(ni
, page
);
695 if (err
!= E_NTFS_NONRESIDENT
) {
701 if (is_compressed(ni
)) {
703 err
= ni_readpage_cmpr(ni
, page
);
708 /* Normal + sparse files. */
709 return mpage_read_folio(folio
, ntfs_get_block
);
712 static void ntfs_readahead(struct readahead_control
*rac
)
714 struct address_space
*mapping
= rac
->mapping
;
715 struct inode
*inode
= mapping
->host
;
716 struct ntfs_inode
*ni
= ntfs_i(inode
);
720 if (is_resident(ni
)) {
721 /* No readahead for resident. */
725 if (is_compressed(ni
)) {
726 /* No readahead for compressed. */
731 pos
= readahead_pos(rac
);
733 if (valid
< i_size_read(inode
) && pos
<= valid
&&
734 valid
< pos
+ readahead_length(rac
)) {
735 /* Range cross 'valid'. Read it page by page. */
739 mpage_readahead(rac
, ntfs_get_block
);
742 static int ntfs_get_block_direct_IO_R(struct inode
*inode
, sector_t iblock
,
743 struct buffer_head
*bh_result
, int create
)
745 return ntfs_get_block_vbo(inode
, (u64
)iblock
<< inode
->i_blkbits
,
746 bh_result
, create
, GET_BLOCK_DIRECT_IO_R
);
749 static int ntfs_get_block_direct_IO_W(struct inode
*inode
, sector_t iblock
,
750 struct buffer_head
*bh_result
, int create
)
752 return ntfs_get_block_vbo(inode
, (u64
)iblock
<< inode
->i_blkbits
,
753 bh_result
, create
, GET_BLOCK_DIRECT_IO_W
);
756 static ssize_t
ntfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
758 struct file
*file
= iocb
->ki_filp
;
759 struct address_space
*mapping
= file
->f_mapping
;
760 struct inode
*inode
= mapping
->host
;
761 struct ntfs_inode
*ni
= ntfs_i(inode
);
762 loff_t vbo
= iocb
->ki_pos
;
764 int wr
= iov_iter_rw(iter
) & WRITE
;
765 size_t iter_count
= iov_iter_count(iter
);
769 if (is_resident(ni
)) {
770 /* Switch to buffered write. */
775 ret
= blockdev_direct_IO(iocb
, inode
, iter
,
776 wr
? ntfs_get_block_direct_IO_W
777 : ntfs_get_block_direct_IO_R
);
781 else if (wr
&& ret
== -EIOCBQUEUED
)
782 end
= vbo
+ iter_count
;
788 if (end
> valid
&& !S_ISBLK(inode
->i_mode
)) {
790 mark_inode_dirty(inode
);
792 } else if (vbo
< valid
&& valid
< end
) {
794 iov_iter_revert(iter
, end
- valid
);
795 iov_iter_zero(end
- valid
, iter
);
802 int ntfs_set_size(struct inode
*inode
, u64 new_size
)
804 struct super_block
*sb
= inode
->i_sb
;
805 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
806 struct ntfs_inode
*ni
= ntfs_i(inode
);
809 /* Check for maximum file size. */
810 if (is_sparsed(ni
) || is_compressed(ni
)) {
811 if (new_size
> sbi
->maxbytes_sparse
) {
815 } else if (new_size
> sbi
->maxbytes
) {
821 down_write(&ni
->file
.run_lock
);
823 err
= attr_set_size(ni
, ATTR_DATA
, NULL
, 0, &ni
->file
.run
, new_size
,
824 &ni
->i_valid
, true, NULL
);
826 up_write(&ni
->file
.run_lock
);
829 mark_inode_dirty(inode
);
835 static int ntfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
837 struct address_space
*mapping
= page
->mapping
;
838 struct inode
*inode
= mapping
->host
;
839 struct ntfs_inode
*ni
= ntfs_i(inode
);
842 if (is_resident(ni
)) {
844 err
= attr_data_write_resident(ni
, page
);
846 if (err
!= E_NTFS_NONRESIDENT
) {
852 return block_write_full_page(page
, ntfs_get_block
, wbc
);
855 static int ntfs_resident_writepage(struct page
*page
,
856 struct writeback_control
*wbc
, void *data
)
858 struct address_space
*mapping
= data
;
859 struct ntfs_inode
*ni
= ntfs_i(mapping
->host
);
863 ret
= attr_data_write_resident(ni
, page
);
866 if (ret
!= E_NTFS_NONRESIDENT
)
868 mapping_set_error(mapping
, ret
);
872 static int ntfs_writepages(struct address_space
*mapping
,
873 struct writeback_control
*wbc
)
875 if (is_resident(ntfs_i(mapping
->host
)))
876 return write_cache_pages(mapping
, wbc
, ntfs_resident_writepage
,
878 return mpage_writepages(mapping
, wbc
, ntfs_get_block
);
881 static int ntfs_get_block_write_begin(struct inode
*inode
, sector_t vbn
,
882 struct buffer_head
*bh_result
, int create
)
884 return ntfs_get_block_vbo(inode
, (u64
)vbn
<< inode
->i_blkbits
,
885 bh_result
, create
, GET_BLOCK_WRITE_BEGIN
);
888 int ntfs_write_begin(struct file
*file
, struct address_space
*mapping
,
889 loff_t pos
, u32 len
, struct page
**pagep
, void **fsdata
)
892 struct inode
*inode
= mapping
->host
;
893 struct ntfs_inode
*ni
= ntfs_i(inode
);
896 if (is_resident(ni
)) {
897 struct page
*page
= grab_cache_page_write_begin(
898 mapping
, pos
>> PAGE_SHIFT
);
906 err
= attr_data_read_resident(ni
, page
);
916 if (err
!= E_NTFS_NONRESIDENT
)
920 err
= block_write_begin(mapping
, pos
, len
, pagep
,
921 ntfs_get_block_write_begin
);
928 * ntfs_write_end - Address_space_operations::write_end.
930 int ntfs_write_end(struct file
*file
, struct address_space
*mapping
,
931 loff_t pos
, u32 len
, u32 copied
, struct page
*page
,
934 struct inode
*inode
= mapping
->host
;
935 struct ntfs_inode
*ni
= ntfs_i(inode
);
936 u64 valid
= ni
->i_valid
;
940 if (is_resident(ni
)) {
942 err
= attr_data_write_resident(ni
, page
);
946 /* Clear any buffers in page. */
947 if (page_has_buffers(page
)) {
948 struct buffer_head
*head
, *bh
;
950 bh
= head
= page_buffers(page
);
952 clear_buffer_dirty(bh
);
953 clear_buffer_mapped(bh
);
954 set_buffer_uptodate(bh
);
955 } while (head
!= (bh
= bh
->b_this_page
));
957 SetPageUptodate(page
);
963 err
= generic_write_end(file
, mapping
, pos
, len
, copied
, page
,
968 if (!(ni
->std_fa
& FILE_ATTRIBUTE_ARCHIVE
)) {
969 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
970 ni
->std_fa
|= FILE_ATTRIBUTE_ARCHIVE
;
974 if (valid
!= ni
->i_valid
) {
975 /* ni->i_valid is changed in ntfs_get_block_vbo. */
979 if (pos
+ err
> inode
->i_size
) {
980 inode
->i_size
= pos
+ err
;
985 mark_inode_dirty(inode
);
991 int reset_log_file(struct inode
*inode
)
995 u32 log_size
= inode
->i_size
;
996 struct address_space
*mapping
= inode
->i_mapping
;
1003 len
= pos
+ PAGE_SIZE
> log_size
? (log_size
- pos
) : PAGE_SIZE
;
1005 err
= block_write_begin(mapping
, pos
, len
, &page
,
1006 ntfs_get_block_write_begin
);
1010 kaddr
= kmap_atomic(page
);
1011 memset(kaddr
, -1, len
);
1012 kunmap_atomic(kaddr
);
1013 flush_dcache_page(page
);
1015 err
= block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
1020 if (pos
>= log_size
)
1022 balance_dirty_pages_ratelimited(mapping
);
1025 mark_inode_dirty_sync(inode
);
1030 int ntfs3_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1032 return _ni_write_inode(inode
, wbc
->sync_mode
== WB_SYNC_ALL
);
1035 int ntfs_sync_inode(struct inode
*inode
)
1037 return _ni_write_inode(inode
, 1);
1041 * writeback_inode - Helper function for ntfs_flush_inodes().
1043 * This writes both the inode and the file data blocks, waiting
1044 * for in flight data blocks before the start of the call. It
1045 * does not wait for any io started during the call.
1047 static int writeback_inode(struct inode
*inode
)
1049 int ret
= sync_inode_metadata(inode
, 0);
1052 ret
= filemap_fdatawrite(inode
->i_mapping
);
1059 * Write data and metadata corresponding to i1 and i2. The io is
1060 * started but we do not wait for any of it to finish.
1062 * filemap_flush() is used for the block device, so if there is a dirty
1063 * page for a block already in flight, we will not wait and start the
1066 int ntfs_flush_inodes(struct super_block
*sb
, struct inode
*i1
,
1072 ret
= writeback_inode(i1
);
1074 ret
= writeback_inode(i2
);
1076 ret
= sync_blockdev_nowait(sb
->s_bdev
);
1080 int inode_write_data(struct inode
*inode
, const void *data
, size_t bytes
)
1084 /* Write non resident data. */
1085 for (idx
= 0; bytes
; idx
++) {
1086 size_t op
= bytes
> PAGE_SIZE
? PAGE_SIZE
: bytes
;
1087 struct page
*page
= ntfs_map_page(inode
->i_mapping
, idx
);
1090 return PTR_ERR(page
);
1093 WARN_ON(!PageUptodate(page
));
1094 ClearPageUptodate(page
);
1096 memcpy(page_address(page
), data
, op
);
1098 flush_dcache_page(page
);
1099 SetPageUptodate(page
);
1102 ntfs_unmap_page(page
);
1105 data
= Add2Ptr(data
, PAGE_SIZE
);
1111 * ntfs_reparse_bytes
1113 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1114 * for unicode string of @uni_len length.
1116 static inline u32
ntfs_reparse_bytes(u32 uni_len
)
1118 /* Header + unicode string + decorated unicode string. */
1119 return sizeof(short) * (2 * uni_len
+ 4) +
1120 offsetof(struct REPARSE_DATA_BUFFER
,
1121 SymbolicLinkReparseBuffer
.PathBuffer
);
1124 static struct REPARSE_DATA_BUFFER
*
1125 ntfs_create_reparse_buffer(struct ntfs_sb_info
*sbi
, const char *symname
,
1126 u32 size
, u16
*nsize
)
1129 struct REPARSE_DATA_BUFFER
*rp
;
1131 typeof(rp
->SymbolicLinkReparseBuffer
) *rs
;
1133 rp
= kzalloc(ntfs_reparse_bytes(2 * size
+ 2), GFP_NOFS
);
1135 return ERR_PTR(-ENOMEM
);
1137 rs
= &rp
->SymbolicLinkReparseBuffer
;
1138 rp_name
= rs
->PathBuffer
;
1140 /* Convert link name to UTF-16. */
1141 err
= ntfs_nls_to_utf16(sbi
, symname
, size
,
1142 (struct cpu_str
*)(rp_name
- 1), 2 * size
,
1143 UTF16_LITTLE_ENDIAN
);
1147 /* err = the length of unicode name of symlink. */
1148 *nsize
= ntfs_reparse_bytes(err
);
1150 if (*nsize
> sbi
->reparse
.max_size
) {
1155 /* Translate Linux '/' into Windows '\'. */
1156 for (i
= 0; i
< err
; i
++) {
1157 if (rp_name
[i
] == cpu_to_le16('/'))
1158 rp_name
[i
] = cpu_to_le16('\\');
1161 rp
->ReparseTag
= IO_REPARSE_TAG_SYMLINK
;
1162 rp
->ReparseDataLength
=
1163 cpu_to_le16(*nsize
- offsetof(struct REPARSE_DATA_BUFFER
,
1164 SymbolicLinkReparseBuffer
));
1166 /* PrintName + SubstituteName. */
1167 rs
->SubstituteNameOffset
= cpu_to_le16(sizeof(short) * err
);
1168 rs
->SubstituteNameLength
= cpu_to_le16(sizeof(short) * err
+ 8);
1169 rs
->PrintNameLength
= rs
->SubstituteNameOffset
;
1172 * TODO: Use relative path if possible to allow Windows to
1174 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1178 memmove(rp_name
+ err
+ 4, rp_name
, sizeof(short) * err
);
1180 /* Decorate SubstituteName. */
1182 rp_name
[0] = cpu_to_le16('\\');
1183 rp_name
[1] = cpu_to_le16('?');
1184 rp_name
[2] = cpu_to_le16('?');
1185 rp_name
[3] = cpu_to_le16('\\');
1190 return ERR_PTR(err
);
1196 * Helper function for:
1201 * - ntfs_atomic_open
1203 * NOTE: if fnd != NULL (ntfs_atomic_open) then @dir is locked
1205 struct inode
*ntfs_create_inode(struct user_namespace
*mnt_userns
,
1206 struct inode
*dir
, struct dentry
*dentry
,
1207 const struct cpu_str
*uni
, umode_t mode
,
1208 dev_t dev
, const char *symname
, u32 size
,
1209 struct ntfs_fnd
*fnd
)
1212 struct super_block
*sb
= dir
->i_sb
;
1213 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
1214 const struct qstr
*name
= &dentry
->d_name
;
1216 struct ntfs_inode
*dir_ni
= ntfs_i(dir
);
1217 struct ntfs_inode
*ni
= NULL
;
1218 struct inode
*inode
= NULL
;
1219 struct ATTRIB
*attr
;
1220 struct ATTR_STD_INFO5
*std5
;
1221 struct ATTR_FILE_NAME
*fname
;
1222 struct MFT_REC
*rec
;
1223 u32 asize
, dsize
, sd_size
;
1224 enum FILE_ATTRIBUTE fa
;
1225 __le32 security_id
= SECURITY_ID_INVALID
;
1228 u16 t16
, nsize
= 0, aid
= 0;
1229 struct INDEX_ROOT
*root
, *dir_root
;
1230 struct NTFS_DE
*e
, *new_de
= NULL
;
1231 struct REPARSE_DATA_BUFFER
*rp
= NULL
;
1232 bool rp_inserted
= false;
1235 ni_lock_dir(dir_ni
);
1237 dir_root
= indx_get_root(&dir_ni
->dir
, dir_ni
, NULL
, NULL
);
1243 if (S_ISDIR(mode
)) {
1244 /* Use parent's directory attributes. */
1245 fa
= dir_ni
->std_fa
| FILE_ATTRIBUTE_DIRECTORY
|
1246 FILE_ATTRIBUTE_ARCHIVE
;
1248 * By default child directory inherits parent attributes.
1249 * Root directory is hidden + system.
1250 * Make an exception for children in root.
1252 if (dir
->i_ino
== MFT_REC_ROOT
)
1253 fa
&= ~(FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
);
1254 } else if (S_ISLNK(mode
)) {
1255 /* It is good idea that link should be the same type (file/dir) as target */
1256 fa
= FILE_ATTRIBUTE_REPARSE_POINT
;
1259 * Linux: there are dir/file/symlink and so on.
1260 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1261 * It is good idea to create:
1262 * dir + reparse if 'symname' points to directory
1264 * file + reparse if 'symname' points to file
1265 * Unfortunately kern_path hangs if symname contains 'dir'.
1271 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1272 * struct inode *target = d_inode(path.dentry);
1274 * if (S_ISDIR(target->i_mode))
1275 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1276 * // if ( target->i_sb == sb ){
1277 * // use relative path?
1282 } else if (S_ISREG(mode
)) {
1283 if (sbi
->options
->sparse
) {
1284 /* Sparsed regular file, cause option 'sparse'. */
1285 fa
= FILE_ATTRIBUTE_SPARSE_FILE
|
1286 FILE_ATTRIBUTE_ARCHIVE
;
1287 } else if (dir_ni
->std_fa
& FILE_ATTRIBUTE_COMPRESSED
) {
1288 /* Compressed regular file, if parent is compressed. */
1289 fa
= FILE_ATTRIBUTE_COMPRESSED
| FILE_ATTRIBUTE_ARCHIVE
;
1291 /* Regular file, default attributes. */
1292 fa
= FILE_ATTRIBUTE_ARCHIVE
;
1295 fa
= FILE_ATTRIBUTE_ARCHIVE
;
1298 /* If option "hide_dot_files" then set hidden attribute for dot files. */
1299 if (sbi
->options
->hide_dot_files
&& name
->name
[0] == '.')
1300 fa
|= FILE_ATTRIBUTE_HIDDEN
;
1303 fa
|= FILE_ATTRIBUTE_READONLY
;
1305 /* Allocate PATH_MAX bytes. */
1306 new_de
= __getname();
1312 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1313 ntfs_set_state(sbi
, NTFS_DIRTY_DIRTY
);
1315 /* Step 1: allocate and fill new mft record. */
1316 err
= ntfs_look_free_mft(sbi
, &ino
, false, NULL
, NULL
);
1320 ni
= ntfs_new_inode(sbi
, ino
, fa
& FILE_ATTRIBUTE_DIRECTORY
);
1326 inode
= &ni
->vfs_inode
;
1327 inode_init_owner(mnt_userns
, inode
, dir
, mode
);
1328 mode
= inode
->i_mode
;
1330 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= ni
->i_crtime
=
1331 current_time(inode
);
1334 rec
->hard_links
= cpu_to_le16(1);
1335 attr
= Add2Ptr(rec
, le16_to_cpu(rec
->attr_off
));
1337 /* Get default security id. */
1338 sd
= s_default_security
;
1339 sd_size
= sizeof(s_default_security
);
1341 if (is_ntfs3(sbi
)) {
1342 security_id
= dir_ni
->std_security_id
;
1343 if (le32_to_cpu(security_id
) < SECURITY_ID_FIRST
) {
1344 security_id
= sbi
->security
.def_security_id
;
1346 if (security_id
== SECURITY_ID_INVALID
&&
1347 !ntfs_insert_security(sbi
, sd
, sd_size
,
1348 &security_id
, NULL
))
1349 sbi
->security
.def_security_id
= security_id
;
1353 /* Insert standard info. */
1354 std5
= Add2Ptr(attr
, SIZEOF_RESIDENT
);
1356 if (security_id
== SECURITY_ID_INVALID
) {
1357 dsize
= sizeof(struct ATTR_STD_INFO
);
1359 dsize
= sizeof(struct ATTR_STD_INFO5
);
1360 std5
->security_id
= security_id
;
1361 ni
->std_security_id
= security_id
;
1363 asize
= SIZEOF_RESIDENT
+ dsize
;
1365 attr
->type
= ATTR_STD
;
1366 attr
->size
= cpu_to_le32(asize
);
1367 attr
->id
= cpu_to_le16(aid
++);
1368 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1369 attr
->res
.data_size
= cpu_to_le32(dsize
);
1371 std5
->cr_time
= std5
->m_time
= std5
->c_time
= std5
->a_time
=
1372 kernel2nt(&inode
->i_atime
);
1377 attr
= Add2Ptr(attr
, asize
);
1379 /* Insert file name. */
1380 err
= fill_name_de(sbi
, new_de
, name
, uni
);
1384 mi_get_ref(&ni
->mi
, &new_de
->ref
);
1386 fname
= (struct ATTR_FILE_NAME
*)(new_de
+ 1);
1388 if (sbi
->options
->windows_names
&&
1389 !valid_windows_name(sbi
, (struct le_str
*)&fname
->name_len
)) {
1394 mi_get_ref(&dir_ni
->mi
, &fname
->home
);
1395 fname
->dup
.cr_time
= fname
->dup
.m_time
= fname
->dup
.c_time
=
1396 fname
->dup
.a_time
= std5
->cr_time
;
1397 fname
->dup
.alloc_size
= fname
->dup
.data_size
= 0;
1398 fname
->dup
.fa
= std5
->fa
;
1399 fname
->dup
.ea_size
= fname
->dup
.reparse
= 0;
1401 dsize
= le16_to_cpu(new_de
->key_size
);
1402 asize
= ALIGN(SIZEOF_RESIDENT
+ dsize
, 8);
1404 attr
->type
= ATTR_NAME
;
1405 attr
->size
= cpu_to_le32(asize
);
1406 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1407 attr
->res
.flags
= RESIDENT_FLAG_INDEXED
;
1408 attr
->id
= cpu_to_le16(aid
++);
1409 attr
->res
.data_size
= cpu_to_le32(dsize
);
1410 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), fname
, dsize
);
1412 attr
= Add2Ptr(attr
, asize
);
1414 if (security_id
== SECURITY_ID_INVALID
) {
1415 /* Insert security attribute. */
1416 asize
= SIZEOF_RESIDENT
+ ALIGN(sd_size
, 8);
1418 attr
->type
= ATTR_SECURE
;
1419 attr
->size
= cpu_to_le32(asize
);
1420 attr
->id
= cpu_to_le16(aid
++);
1421 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1422 attr
->res
.data_size
= cpu_to_le32(sd_size
);
1423 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), sd
, sd_size
);
1425 attr
= Add2Ptr(attr
, asize
);
1428 attr
->id
= cpu_to_le16(aid
++);
1429 if (fa
& FILE_ATTRIBUTE_DIRECTORY
) {
1431 * Regular directory or symlink to directory.
1432 * Create root attribute.
1434 dsize
= sizeof(struct INDEX_ROOT
) + sizeof(struct NTFS_DE
);
1435 asize
= sizeof(I30_NAME
) + SIZEOF_RESIDENT
+ dsize
;
1437 attr
->type
= ATTR_ROOT
;
1438 attr
->size
= cpu_to_le32(asize
);
1440 attr
->name_len
= ARRAY_SIZE(I30_NAME
);
1441 attr
->name_off
= SIZEOF_RESIDENT_LE
;
1442 attr
->res
.data_off
=
1443 cpu_to_le16(sizeof(I30_NAME
) + SIZEOF_RESIDENT
);
1444 attr
->res
.data_size
= cpu_to_le32(dsize
);
1445 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), I30_NAME
,
1448 root
= Add2Ptr(attr
, sizeof(I30_NAME
) + SIZEOF_RESIDENT
);
1449 memcpy(root
, dir_root
, offsetof(struct INDEX_ROOT
, ihdr
));
1451 cpu_to_le32(sizeof(struct INDEX_HDR
)); // 0x10
1452 root
->ihdr
.used
= cpu_to_le32(sizeof(struct INDEX_HDR
) +
1453 sizeof(struct NTFS_DE
));
1454 root
->ihdr
.total
= root
->ihdr
.used
;
1456 e
= Add2Ptr(root
, sizeof(struct INDEX_ROOT
));
1457 e
->size
= cpu_to_le16(sizeof(struct NTFS_DE
));
1458 e
->flags
= NTFS_IE_LAST
;
1459 } else if (S_ISLNK(mode
)) {
1462 * Create empty resident data attribute.
1464 asize
= SIZEOF_RESIDENT
;
1466 /* Insert empty ATTR_DATA */
1467 attr
->type
= ATTR_DATA
;
1468 attr
->size
= cpu_to_le32(SIZEOF_RESIDENT
);
1469 attr
->name_off
= SIZEOF_RESIDENT_LE
;
1470 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1471 } else if (S_ISREG(mode
)) {
1473 * Regular file. Create empty non resident data attribute.
1475 attr
->type
= ATTR_DATA
;
1477 attr
->nres
.evcn
= cpu_to_le64(-1ll);
1478 if (fa
& FILE_ATTRIBUTE_SPARSE_FILE
) {
1479 attr
->size
= cpu_to_le32(SIZEOF_NONRESIDENT_EX
+ 8);
1480 attr
->name_off
= SIZEOF_NONRESIDENT_EX_LE
;
1481 attr
->flags
= ATTR_FLAG_SPARSED
;
1482 asize
= SIZEOF_NONRESIDENT_EX
+ 8;
1483 } else if (fa
& FILE_ATTRIBUTE_COMPRESSED
) {
1484 attr
->size
= cpu_to_le32(SIZEOF_NONRESIDENT_EX
+ 8);
1485 attr
->name_off
= SIZEOF_NONRESIDENT_EX_LE
;
1486 attr
->flags
= ATTR_FLAG_COMPRESSED
;
1487 attr
->nres
.c_unit
= COMPRESSION_UNIT
;
1488 asize
= SIZEOF_NONRESIDENT_EX
+ 8;
1490 attr
->size
= cpu_to_le32(SIZEOF_NONRESIDENT
+ 8);
1491 attr
->name_off
= SIZEOF_NONRESIDENT_LE
;
1492 asize
= SIZEOF_NONRESIDENT
+ 8;
1494 attr
->nres
.run_off
= attr
->name_off
;
1497 * Node. Create empty resident data attribute.
1499 attr
->type
= ATTR_DATA
;
1500 attr
->size
= cpu_to_le32(SIZEOF_RESIDENT
);
1501 attr
->name_off
= SIZEOF_RESIDENT_LE
;
1502 if (fa
& FILE_ATTRIBUTE_SPARSE_FILE
)
1503 attr
->flags
= ATTR_FLAG_SPARSED
;
1504 else if (fa
& FILE_ATTRIBUTE_COMPRESSED
)
1505 attr
->flags
= ATTR_FLAG_COMPRESSED
;
1506 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1507 asize
= SIZEOF_RESIDENT
;
1508 ni
->ni_flags
|= NI_FLAG_RESIDENT
;
1511 if (S_ISDIR(mode
)) {
1512 ni
->ni_flags
|= NI_FLAG_DIR
;
1513 err
= indx_init(&ni
->dir
, sbi
, attr
, INDEX_MUTEX_I30
);
1516 } else if (S_ISLNK(mode
)) {
1517 rp
= ntfs_create_reparse_buffer(sbi
, symname
, size
, &nsize
);
1526 * Insert ATTR_REPARSE.
1528 attr
= Add2Ptr(attr
, asize
);
1529 attr
->type
= ATTR_REPARSE
;
1530 attr
->id
= cpu_to_le16(aid
++);
1532 /* Resident or non resident? */
1533 asize
= ALIGN(SIZEOF_RESIDENT
+ nsize
, 8);
1534 t16
= PtrOffset(rec
, attr
);
1537 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1538 * It is good idea to keep extened attributes resident.
1540 if (asize
+ t16
+ 0x78 + 8 > sbi
->record_size
) {
1542 CLST clst
= bytes_to_cluster(sbi
, nsize
);
1544 /* Bytes per runs. */
1545 t16
= sbi
->record_size
- t16
- SIZEOF_NONRESIDENT
;
1548 attr
->nres
.evcn
= cpu_to_le64(clst
- 1);
1549 attr
->name_off
= SIZEOF_NONRESIDENT_LE
;
1550 attr
->nres
.run_off
= attr
->name_off
;
1551 attr
->nres
.data_size
= cpu_to_le64(nsize
);
1552 attr
->nres
.valid_size
= attr
->nres
.data_size
;
1553 attr
->nres
.alloc_size
=
1554 cpu_to_le64(ntfs_up_cluster(sbi
, nsize
));
1556 err
= attr_allocate_clusters(sbi
, &ni
->file
.run
, 0, 0,
1557 clst
, NULL
, ALLOCATE_DEF
,
1558 &alen
, 0, NULL
, NULL
);
1562 err
= run_pack(&ni
->file
.run
, 0, clst
,
1563 Add2Ptr(attr
, SIZEOF_NONRESIDENT
), t16
,
1573 asize
= SIZEOF_NONRESIDENT
+ ALIGN(err
, 8);
1575 attr
->res
.data_off
= SIZEOF_RESIDENT_LE
;
1576 attr
->res
.data_size
= cpu_to_le32(nsize
);
1577 memcpy(Add2Ptr(attr
, SIZEOF_RESIDENT
), rp
, nsize
);
1580 /* Size of symlink equals the length of input string. */
1581 inode
->i_size
= size
;
1583 attr
->size
= cpu_to_le32(asize
);
1585 err
= ntfs_insert_reparse(sbi
, IO_REPARSE_TAG_SYMLINK
,
1593 attr
= Add2Ptr(attr
, asize
);
1594 attr
->type
= ATTR_END
;
1596 rec
->used
= cpu_to_le32(PtrOffset(rec
, attr
) + 8);
1597 rec
->next_attr_id
= cpu_to_le16(aid
);
1599 /* Step 2: Add new name in index. */
1600 err
= indx_insert_entry(&dir_ni
->dir
, dir_ni
, new_de
, sbi
, fnd
, 0);
1604 /* Unlock parent directory before ntfs_init_acl. */
1608 inode
->i_generation
= le16_to_cpu(rec
->seq
);
1610 dir
->i_mtime
= dir
->i_ctime
= inode
->i_atime
;
1612 if (S_ISDIR(mode
)) {
1613 inode
->i_op
= &ntfs_dir_inode_operations
;
1614 inode
->i_fop
= &ntfs_dir_operations
;
1615 } else if (S_ISLNK(mode
)) {
1616 inode
->i_op
= &ntfs_link_inode_operations
;
1617 inode
->i_fop
= NULL
;
1618 inode
->i_mapping
->a_ops
= &ntfs_aops
;
1619 inode
->i_size
= size
;
1620 inode_nohighmem(inode
);
1621 } else if (S_ISREG(mode
)) {
1622 inode
->i_op
= &ntfs_file_inode_operations
;
1623 inode
->i_fop
= &ntfs_file_operations
;
1624 inode
->i_mapping
->a_ops
=
1625 is_compressed(ni
) ? &ntfs_aops_cmpr
: &ntfs_aops
;
1626 init_rwsem(&ni
->file
.run_lock
);
1628 inode
->i_op
= &ntfs_special_inode_operations
;
1629 init_special_inode(inode
, mode
, dev
);
1632 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1633 if (!S_ISLNK(mode
) && (sb
->s_flags
& SB_POSIXACL
)) {
1634 err
= ntfs_init_acl(mnt_userns
, inode
, dir
);
1640 inode
->i_flags
|= S_NOSEC
;
1643 /* Write non resident data. */
1645 err
= ntfs_sb_write_run(sbi
, &ni
->file
.run
, 0, rp
, nsize
, 0);
1651 * Call 'd_instantiate' after inode->i_op is set
1652 * but before finish_open.
1654 d_instantiate(dentry
, inode
);
1656 ntfs_save_wsl_perm(inode
);
1657 mark_inode_dirty(dir
);
1658 mark_inode_dirty(inode
);
1665 /* Undo 'indx_insert_entry'. */
1667 ni_lock_dir(dir_ni
);
1668 indx_delete_entry(&dir_ni
->dir
, dir_ni
, new_de
+ 1,
1669 le16_to_cpu(new_de
->key_size
), sbi
);
1670 /* ni_unlock(dir_ni); will be called later. */
1673 ntfs_remove_reparse(sbi
, IO_REPARSE_TAG_SYMLINK
, &new_de
->ref
);
1677 run_deallocate(sbi
, &ni
->file
.run
, false);
1680 clear_rec_inuse(rec
);
1682 ni
->mi
.dirty
= false;
1683 discard_new_inode(inode
);
1685 ntfs_mark_rec_free(sbi
, ino
, false);
1695 return ERR_PTR(err
);
1698 unlock_new_inode(inode
);
1703 int ntfs_link_inode(struct inode
*inode
, struct dentry
*dentry
)
1706 struct ntfs_inode
*ni
= ntfs_i(inode
);
1707 struct ntfs_sb_info
*sbi
= inode
->i_sb
->s_fs_info
;
1710 /* Allocate PATH_MAX bytes. */
1715 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1716 ntfs_set_state(sbi
, NTFS_DIRTY_DIRTY
);
1718 /* Construct 'de'. */
1719 err
= fill_name_de(sbi
, de
, &dentry
->d_name
, NULL
);
1723 err
= ni_add_name(ntfs_i(d_inode(dentry
->d_parent
)), ni
, de
);
1732 * inode_operations::unlink
1733 * inode_operations::rmdir
1735 int ntfs_unlink_inode(struct inode
*dir
, const struct dentry
*dentry
)
1738 struct ntfs_sb_info
*sbi
= dir
->i_sb
->s_fs_info
;
1739 struct inode
*inode
= d_inode(dentry
);
1740 struct ntfs_inode
*ni
= ntfs_i(inode
);
1741 struct ntfs_inode
*dir_ni
= ntfs_i(dir
);
1742 struct NTFS_DE
*de
, *de2
= NULL
;
1745 if (ntfs_is_meta_file(sbi
, ni
->mi
.rno
))
1748 /* Allocate PATH_MAX bytes. */
1755 if (S_ISDIR(inode
->i_mode
) && !dir_is_empty(inode
)) {
1760 err
= fill_name_de(sbi
, de
, &dentry
->d_name
, NULL
);
1765 err
= ni_remove_name(dir_ni
, ni
, de
, &de2
, &undo_remove
);
1769 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
1770 mark_inode_dirty(dir
);
1771 inode
->i_ctime
= dir
->i_ctime
;
1773 mark_inode_dirty(inode
);
1774 } else if (!ni_remove_name_undo(dir_ni
, ni
, de
, de2
, undo_remove
)) {
1775 _ntfs_bad_inode(inode
);
1777 if (ni_is_dirty(dir
))
1778 mark_inode_dirty(dir
);
1779 if (ni_is_dirty(inode
))
1780 mark_inode_dirty(inode
);
1789 void ntfs_evict_inode(struct inode
*inode
)
1791 truncate_inode_pages_final(&inode
->i_data
);
1794 _ni_write_inode(inode
, inode_needs_sync(inode
));
1796 invalidate_inode_buffers(inode
);
1799 ni_clear(ntfs_i(inode
));
1803 * ntfs_translate_junction
1805 * Translate a Windows junction target to the Linux equivalent.
1806 * On junctions, targets are always absolute (they include the drive
1807 * letter). We have no way of knowing if the target is for the current
1808 * mounted device or not so we just assume it is.
1810 static int ntfs_translate_junction(const struct super_block
*sb
,
1811 const struct dentry
*link_de
, char *target
,
1812 int target_len
, int target_max
)
1814 int tl_len
, err
= target_len
;
1815 char *link_path_buffer
= NULL
, *link_path
;
1816 char *translated
= NULL
;
1820 link_path_buffer
= kmalloc(PATH_MAX
, GFP_NOFS
);
1821 if (!link_path_buffer
) {
1825 /* Get link path, relative to mount point */
1826 link_path
= dentry_path_raw(link_de
, link_path_buffer
, PATH_MAX
);
1827 if (IS_ERR(link_path
)) {
1828 ntfs_err(sb
, "Error getting link path");
1833 translated
= kmalloc(PATH_MAX
, GFP_NOFS
);
1839 /* Make translated path a relative path to mount point */
1840 strcpy(translated
, "./");
1841 ++link_path
; /* Skip leading / */
1842 for (tl_len
= sizeof("./") - 1; *link_path
; ++link_path
) {
1843 if (*link_path
== '/') {
1844 if (PATH_MAX
- tl_len
< sizeof("../")) {
1846 "Link path %s has too many components",
1851 strcpy(translated
+ tl_len
, "../");
1852 tl_len
+= sizeof("../") - 1;
1856 /* Skip drive letter */
1857 target_start
= target
;
1858 while (*target_start
&& *target_start
!= ':')
1861 if (!*target_start
) {
1862 ntfs_err(sb
, "Link target (%s) missing drive separator",
1868 /* Skip drive separator and leading /, if exists */
1869 target_start
+= 1 + (target_start
[1] == '/');
1870 copy_len
= target_len
- (target_start
- target
);
1872 if (PATH_MAX
- tl_len
<= copy_len
) {
1873 ntfs_err(sb
, "Link target %s too large for buffer (%d <= %d)",
1874 target_start
, PATH_MAX
- tl_len
, copy_len
);
1879 /* translated path has a trailing / and target_start does not */
1880 strcpy(translated
+ tl_len
, target_start
);
1882 if (target_max
<= tl_len
) {
1883 ntfs_err(sb
, "Target path %s too large for buffer (%d <= %d)",
1884 translated
, target_max
, tl_len
);
1888 strcpy(target
, translated
);
1892 kfree(link_path_buffer
);
1897 static noinline
int ntfs_readlink_hlp(const struct dentry
*link_de
,
1898 struct inode
*inode
, char *buffer
,
1901 int i
, err
= -EINVAL
;
1902 struct ntfs_inode
*ni
= ntfs_i(inode
);
1903 struct super_block
*sb
= inode
->i_sb
;
1904 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
1907 void *to_free
= NULL
;
1908 struct REPARSE_DATA_BUFFER
*rp
;
1909 const __le16
*uname
;
1910 struct ATTRIB
*attr
;
1912 /* Reparse data present. Try to parse it. */
1913 static_assert(!offsetof(struct REPARSE_DATA_BUFFER
, ReparseTag
));
1914 static_assert(sizeof(u32
) == sizeof(rp
->ReparseTag
));
1918 attr
= ni_find_attr(ni
, NULL
, NULL
, ATTR_REPARSE
, NULL
, 0, NULL
, NULL
);
1922 if (!attr
->non_res
) {
1923 rp
= resident_data_ex(attr
, sizeof(struct REPARSE_DATA_BUFFER
));
1926 size
= le32_to_cpu(attr
->res
.data_size
);
1928 size
= le64_to_cpu(attr
->nres
.data_size
);
1932 if (size
> sbi
->reparse
.max_size
|| size
<= sizeof(u32
))
1936 rp
= kmalloc(size
, GFP_NOFS
);
1942 /* Read into temporal buffer. */
1943 err
= ntfs_read_run_nb(sbi
, &ni
->file
.run
, 0, rp
, size
, NULL
);
1948 /* Microsoft Tag. */
1949 switch (rp
->ReparseTag
) {
1950 case IO_REPARSE_TAG_MOUNT_POINT
:
1951 /* Mount points and junctions. */
1952 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1953 if (size
<= offsetof(struct REPARSE_DATA_BUFFER
,
1954 MountPointReparseBuffer
.PathBuffer
))
1957 offsetof(struct REPARSE_DATA_BUFFER
,
1958 MountPointReparseBuffer
.PathBuffer
) +
1959 le16_to_cpu(rp
->MountPointReparseBuffer
1961 ulen
= le16_to_cpu(rp
->MountPointReparseBuffer
.PrintNameLength
);
1964 case IO_REPARSE_TAG_SYMLINK
:
1965 /* FolderSymbolicLink */
1966 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1967 if (size
<= offsetof(struct REPARSE_DATA_BUFFER
,
1968 SymbolicLinkReparseBuffer
.PathBuffer
))
1971 rp
, offsetof(struct REPARSE_DATA_BUFFER
,
1972 SymbolicLinkReparseBuffer
.PathBuffer
) +
1973 le16_to_cpu(rp
->SymbolicLinkReparseBuffer
1976 rp
->SymbolicLinkReparseBuffer
.PrintNameLength
);
1979 case IO_REPARSE_TAG_CLOUD
:
1980 case IO_REPARSE_TAG_CLOUD_1
:
1981 case IO_REPARSE_TAG_CLOUD_2
:
1982 case IO_REPARSE_TAG_CLOUD_3
:
1983 case IO_REPARSE_TAG_CLOUD_4
:
1984 case IO_REPARSE_TAG_CLOUD_5
:
1985 case IO_REPARSE_TAG_CLOUD_6
:
1986 case IO_REPARSE_TAG_CLOUD_7
:
1987 case IO_REPARSE_TAG_CLOUD_8
:
1988 case IO_REPARSE_TAG_CLOUD_9
:
1989 case IO_REPARSE_TAG_CLOUD_A
:
1990 case IO_REPARSE_TAG_CLOUD_B
:
1991 case IO_REPARSE_TAG_CLOUD_C
:
1992 case IO_REPARSE_TAG_CLOUD_D
:
1993 case IO_REPARSE_TAG_CLOUD_E
:
1994 case IO_REPARSE_TAG_CLOUD_F
:
1995 err
= sizeof("OneDrive") - 1;
1998 memcpy(buffer
, "OneDrive", err
);
2002 if (IsReparseTagMicrosoft(rp
->ReparseTag
)) {
2003 /* Unknown Microsoft Tag. */
2006 if (!IsReparseTagNameSurrogate(rp
->ReparseTag
) ||
2007 size
<= sizeof(struct REPARSE_POINT
)) {
2012 uname
= Add2Ptr(rp
, sizeof(struct REPARSE_POINT
));
2013 ulen
= le16_to_cpu(rp
->ReparseDataLength
) -
2014 sizeof(struct REPARSE_POINT
);
2017 /* Convert nlen from bytes to UNICODE chars. */
2020 /* Check that name is available. */
2021 if (!ulen
|| uname
+ ulen
> (__le16
*)Add2Ptr(rp
, size
))
2024 /* If name is already zero terminated then truncate it now. */
2025 if (!uname
[ulen
- 1])
2028 err
= ntfs_utf16_to_nls(sbi
, uname
, ulen
, buffer
, buflen
);
2033 /* Translate Windows '\' into Linux '/'. */
2034 for (i
= 0; i
< err
; i
++) {
2035 if (buffer
[i
] == '\\')
2039 /* Always set last zero. */
2042 /* If this is a junction, translate the link target. */
2043 if (rp
->ReparseTag
== IO_REPARSE_TAG_MOUNT_POINT
)
2044 err
= ntfs_translate_junction(sb
, link_de
, buffer
, err
, buflen
);
2051 static const char *ntfs_get_link(struct dentry
*de
, struct inode
*inode
,
2052 struct delayed_call
*done
)
2058 return ERR_PTR(-ECHILD
);
2060 ret
= kmalloc(PAGE_SIZE
, GFP_NOFS
);
2062 return ERR_PTR(-ENOMEM
);
2064 err
= ntfs_readlink_hlp(de
, inode
, ret
, PAGE_SIZE
);
2067 return ERR_PTR(err
);
2070 set_delayed_call(done
, kfree_link
, ret
);
2076 const struct inode_operations ntfs_link_inode_operations
= {
2077 .get_link
= ntfs_get_link
,
2078 .setattr
= ntfs3_setattr
,
2079 .listxattr
= ntfs_listxattr
,
2080 .permission
= ntfs_permission
,
2083 const struct address_space_operations ntfs_aops
= {
2084 .read_folio
= ntfs_read_folio
,
2085 .readahead
= ntfs_readahead
,
2086 .writepage
= ntfs_writepage
,
2087 .writepages
= ntfs_writepages
,
2088 .write_begin
= ntfs_write_begin
,
2089 .write_end
= ntfs_write_end
,
2090 .direct_IO
= ntfs_direct_IO
,
2092 .dirty_folio
= block_dirty_folio
,
2093 .invalidate_folio
= block_invalidate_folio
,
2096 const struct address_space_operations ntfs_aops_cmpr
= {
2097 .read_folio
= ntfs_read_folio
,
2098 .readahead
= ntfs_readahead
,