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))
98 /* Record should contain $I30 root. */
99 is_dir = rec->flags & RECORD_FLAG_DIR;
101 inode->i_generation = le16_to_cpu(rec->seq);
103 /* Enumerate all struct Attributes MFT. */
108 * To reduce tab pressure use goto instead of
109 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
114 attr = ni_enum_attr_ex(ni, attr, &le, NULL);
119 /* This is non primary attribute segment. Ignore if not MFT. */
120 if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
124 asize = le32_to_cpu(attr->size);
125 goto attr_unpack_run;
128 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
129 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
130 asize = le32_to_cpu(attr->size);
132 if (le16_to_cpu(attr->name_off) + attr->name_len > asize)
136 t64 = le64_to_cpu(attr->nres.alloc_size);
137 if (le64_to_cpu(attr->nres.data_size) > t64 ||
138 le64_to_cpu(attr->nres.valid_size) > t64)
142 switch (attr->type) {
145 asize < sizeof(struct ATTR_STD_INFO) + roff ||
146 rsize < sizeof(struct ATTR_STD_INFO))
152 std5 = Add2Ptr(attr, roff);
155 nt2kernel(std5->cr_time, &ni->i_crtime);
157 nt2kernel(std5->a_time, &inode->i_atime);
158 nt2kernel(std5->c_time, &inode->i_ctime);
159 nt2kernel(std5->m_time, &inode->i_mtime);
161 ni->std_fa = std5->fa;
163 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
164 rsize >= sizeof(struct ATTR_STD_INFO5))
165 ni->std_security_id = std5->security_id;
169 if (attr->name_len || le || ino == MFT_REC_LOG)
172 err = ntfs_load_attr_list(ni, attr);
181 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
182 rsize < SIZEOF_ATTRIBUTE_FILENAME)
185 fname = Add2Ptr(attr, roff);
186 if (fname->type == FILE_NAME_DOS)
190 if (name && name->len == fname->name_len &&
191 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
199 /* Ignore data attribute in dir record. */
203 if (ino == MFT_REC_BADCLUST && !attr->non_res)
206 if (attr->name_len &&
207 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
208 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
209 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
210 (ino != MFT_REC_SECURE || !attr->non_res ||
211 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
212 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
213 /* File contains stream attribute. Ignore it. */
217 if (is_attr_sparsed(attr))
218 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
220 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
222 if (is_attr_compressed(attr))
223 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
225 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
227 if (is_attr_encrypted(attr))
228 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
230 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
232 if (!attr->non_res) {
233 ni->i_valid = inode->i_size = rsize;
234 inode_set_bytes(inode, rsize);
237 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
239 if (!attr->non_res) {
240 ni->ni_flags |= NI_FLAG_RESIDENT;
244 inode_set_bytes(inode, attr_ondisk_size(attr));
246 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
247 inode->i_size = le64_to_cpu(attr->nres.data_size);
248 if (!attr->nres.alloc_size)
251 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
259 root = Add2Ptr(attr, roff);
262 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
263 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
266 if (root->type != ATTR_NAME ||
267 root->rule != NTFS_COLLATION_TYPE_FILENAME)
273 ni->ni_flags |= NI_FLAG_DIR;
275 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
280 ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
285 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
286 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
289 inode->i_size = le64_to_cpu(attr->nres.data_size);
290 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
291 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
293 run = &ni->dir.alloc_run;
297 if (ino == MFT_REC_MFT) {
300 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
301 /* 0x20000000 = 2^32 / 8 */
302 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
305 run = &sbi->mft.bitmap.run;
307 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
308 !memcmp(attr_name(attr), I30_NAME,
311 run = &ni->dir.bitmap_run;
320 rp_fa = ni_parse_reparse(ni, attr, &rp);
325 * Assume one unicode symbol == one utf8.
327 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
331 ni->i_valid = inode->i_size;
333 /* Clear directory bit. */
334 if (ni->ni_flags & NI_FLAG_DIR) {
335 indx_clear(&ni->dir);
336 memset(&ni->dir, 0, sizeof(ni->dir));
337 ni->ni_flags &= ~NI_FLAG_DIR;
339 run_close(&ni->file.run);
341 mode = S_IFLNK | 0777;
345 goto attr_unpack_run; // Double break.
349 case REPARSE_COMPRESSED:
352 case REPARSE_DEDUPLICATED:
358 if (!attr->name_len &&
359 resident_data_ex(attr, sizeof(struct EA_INFO))) {
360 ni->ni_flags |= NI_FLAG_EA;
362 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
364 inode->i_mode = mode;
365 ntfs_get_wsl_perm(inode);
366 mode = inode->i_mode;
375 roff = le16_to_cpu(attr->nres.run_off);
382 t64 = le64_to_cpu(attr->nres.svcn);
384 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
385 t64, Add2Ptr(attr, roff), asize - roff);
396 if (!is_match && name) {
397 /* Reuse rec as buffer for ascii name. */
402 if (std5->fa & FILE_ATTRIBUTE_READONLY)
410 if (names != le16_to_cpu(rec->hard_links)) {
411 /* Correct minor error on the fly. Do not mark inode as dirty. */
412 rec->hard_links = cpu_to_le16(names);
416 set_nlink(inode, names);
419 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
422 * Dot and dot-dot should be included in count but was not
423 * included in enumeration.
424 * Usually a hard links to directories are disabled.
426 inode->i_op = &ntfs_dir_inode_operations;
427 inode->i_fop = &ntfs_dir_operations;
429 } else if (S_ISLNK(mode)) {
430 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
431 inode->i_op = &ntfs_link_inode_operations;
433 inode_nohighmem(inode);
434 } else if (S_ISREG(mode)) {
435 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
436 inode->i_op = &ntfs_file_inode_operations;
437 inode->i_fop = &ntfs_file_operations;
438 inode->i_mapping->a_ops =
439 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
440 if (ino != MFT_REC_MFT)
441 init_rwsem(&ni->file.run_lock);
442 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
444 inode->i_op = &ntfs_special_inode_operations;
445 init_special_inode(inode, mode, inode->i_rdev);
446 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
447 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
448 /* Records in $Extend are not a files or general directories. */
449 inode->i_op = &ntfs_file_inode_operations;
455 if ((sbi->options->sys_immutable &&
456 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
457 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
458 inode->i_flags |= S_IMMUTABLE;
460 inode->i_flags &= ~S_IMMUTABLE;
463 inode->i_mode = mode;
464 if (!(ni->ni_flags & NI_FLAG_EA)) {
465 /* If no xattr then no security (stored in xattr). */
466 inode->i_flags |= S_NOSEC;
470 if (ino == MFT_REC_MFT && !sb->s_root)
473 unlock_new_inode(inode);
478 if (ino == MFT_REC_MFT && !sb->s_root)
488 * Return: 1 if match.
490 static int ntfs_test_inode(struct inode *inode, void *data)
492 struct MFT_REF *ref = data;
494 return ino_get(ref) == inode->i_ino;
497 static int ntfs_set_inode(struct inode *inode, void *data)
499 const struct MFT_REF *ref = data;
501 inode->i_ino = ino_get(ref);
505 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
506 const struct cpu_str *name)
510 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
512 if (unlikely(!inode))
513 return ERR_PTR(-ENOMEM);
515 /* If this is a freshly allocated inode, need to read it now. */
516 if (inode->i_state & I_NEW)
517 inode = ntfs_read_mft(inode, name, ref);
518 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
519 /* Inode overlaps? */
520 _ntfs_bad_inode(inode);
527 GET_BLOCK_GENERAL = 0,
528 GET_BLOCK_WRITE_BEGIN = 1,
529 GET_BLOCK_DIRECT_IO_R = 2,
530 GET_BLOCK_DIRECT_IO_W = 3,
534 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
535 struct buffer_head *bh, int create,
536 enum get_block_ctx ctx)
538 struct super_block *sb = inode->i_sb;
539 struct ntfs_sb_info *sbi = sb->s_fs_info;
540 struct ntfs_inode *ni = ntfs_i(inode);
541 struct page *page = bh->b_page;
542 u8 cluster_bits = sbi->cluster_bits;
543 u32 block_size = sb->s_blocksize;
544 u64 bytes, lbo, valid;
550 /* Clear previous state. */
551 clear_buffer_new(bh);
552 clear_buffer_uptodate(bh);
554 /* Direct write uses 'create=0'. */
555 if (!create && vbo >= ni->i_valid) {
560 if (vbo >= inode->i_size) {
565 if (is_resident(ni)) {
567 err = attr_data_read_resident(ni, page);
571 set_buffer_uptodate(bh);
572 bh->b_size = block_size;
576 vcn = vbo >> cluster_bits;
577 off = vbo & sbi->cluster_mask;
580 err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
587 bytes = ((u64)len << cluster_bits) - off;
589 if (lcn == SPARSE_LCN) {
591 if (bh->b_size > bytes)
600 if ((len << cluster_bits) > block_size)
601 ntfs_sparse_cluster(inode, page, vcn, len);
604 lbo = ((u64)lcn << cluster_bits) + off;
606 set_buffer_mapped(bh);
607 bh->b_bdev = sb->s_bdev;
608 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
612 if (ctx == GET_BLOCK_DIRECT_IO_W) {
613 /* ntfs_direct_IO will update ni->i_valid. */
618 if (bytes > bh->b_size)
624 if (vbo + bytes > valid) {
625 ni->i_valid = vbo + bytes;
626 mark_inode_dirty(inode);
628 } else if (vbo >= valid) {
629 /* Read out of valid data. */
630 /* Should never be here 'cause already checked. */
631 clear_buffer_mapped(bh);
632 } else if (vbo + bytes <= valid) {
634 } else if (vbo + block_size <= valid) {
635 /* Normal short read. */
639 * Read across valid size: vbo < valid && valid < vbo + block_size
644 u32 voff = valid - vbo;
646 bh->b_size = block_size;
647 off = vbo & (PAGE_SIZE - 1);
648 set_bh_page(bh, page, off);
649 err = bh_read(bh, 0);
652 zero_user_segment(page, off + voff, off + block_size);
656 if (bh->b_size > bytes)
660 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
661 static_assert(sizeof(size_t) < sizeof(loff_t));
662 if (bytes > 0x40000000u)
663 bh->b_size = 0x40000000u;
673 int ntfs_get_block(struct inode *inode, sector_t vbn,
674 struct buffer_head *bh_result, int create)
676 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
677 bh_result, create, GET_BLOCK_GENERAL);
680 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
681 struct buffer_head *bh_result, int create)
683 return ntfs_get_block_vbo(inode,
684 (u64)vsn << inode->i_sb->s_blocksize_bits,
685 bh_result, create, GET_BLOCK_BMAP);
688 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
690 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
693 static int ntfs_read_folio(struct file *file, struct folio *folio)
695 struct page *page = &folio->page;
697 struct address_space *mapping = page->mapping;
698 struct inode *inode = mapping->host;
699 struct ntfs_inode *ni = ntfs_i(inode);
701 if (is_resident(ni)) {
703 err = attr_data_read_resident(ni, page);
705 if (err != E_NTFS_NONRESIDENT) {
711 if (is_compressed(ni)) {
713 err = ni_readpage_cmpr(ni, page);
718 /* Normal + sparse files. */
719 return mpage_read_folio(folio, ntfs_get_block);
722 static void ntfs_readahead(struct readahead_control *rac)
724 struct address_space *mapping = rac->mapping;
725 struct inode *inode = mapping->host;
726 struct ntfs_inode *ni = ntfs_i(inode);
730 if (is_resident(ni)) {
731 /* No readahead for resident. */
735 if (is_compressed(ni)) {
736 /* No readahead for compressed. */
741 pos = readahead_pos(rac);
743 if (valid < i_size_read(inode) && pos <= valid &&
744 valid < pos + readahead_length(rac)) {
745 /* Range cross 'valid'. Read it page by page. */
749 mpage_readahead(rac, ntfs_get_block);
752 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
753 struct buffer_head *bh_result, int create)
755 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
756 bh_result, create, GET_BLOCK_DIRECT_IO_R);
759 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
760 struct buffer_head *bh_result, int create)
762 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
763 bh_result, create, GET_BLOCK_DIRECT_IO_W);
766 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
768 struct file *file = iocb->ki_filp;
769 struct address_space *mapping = file->f_mapping;
770 struct inode *inode = mapping->host;
771 struct ntfs_inode *ni = ntfs_i(inode);
772 loff_t vbo = iocb->ki_pos;
774 int wr = iov_iter_rw(iter) & WRITE;
775 size_t iter_count = iov_iter_count(iter);
779 if (is_resident(ni)) {
780 /* Switch to buffered write. */
785 ret = blockdev_direct_IO(iocb, inode, iter,
786 wr ? ntfs_get_block_direct_IO_W
787 : ntfs_get_block_direct_IO_R);
791 else if (wr && ret == -EIOCBQUEUED)
792 end = vbo + iter_count;
798 if (end > valid && !S_ISBLK(inode->i_mode)) {
800 mark_inode_dirty(inode);
802 } else if (vbo < valid && valid < end) {
804 iov_iter_revert(iter, end - valid);
805 iov_iter_zero(end - valid, iter);
812 int ntfs_set_size(struct inode *inode, u64 new_size)
814 struct super_block *sb = inode->i_sb;
815 struct ntfs_sb_info *sbi = sb->s_fs_info;
816 struct ntfs_inode *ni = ntfs_i(inode);
819 /* Check for maximum file size. */
820 if (is_sparsed(ni) || is_compressed(ni)) {
821 if (new_size > sbi->maxbytes_sparse) {
825 } else if (new_size > sbi->maxbytes) {
831 down_write(&ni->file.run_lock);
833 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
834 &ni->i_valid, true, NULL);
836 up_write(&ni->file.run_lock);
839 mark_inode_dirty(inode);
845 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
847 struct address_space *mapping = page->mapping;
848 struct inode *inode = mapping->host;
849 struct ntfs_inode *ni = ntfs_i(inode);
852 if (is_resident(ni)) {
854 err = attr_data_write_resident(ni, page);
856 if (err != E_NTFS_NONRESIDENT) {
862 return block_write_full_page(page, ntfs_get_block, wbc);
865 static int ntfs_writepages(struct address_space *mapping,
866 struct writeback_control *wbc)
868 /* Redirect call to 'ntfs_writepage' for resident files. */
869 if (is_resident(ntfs_i(mapping->host)))
870 return generic_writepages(mapping, wbc);
871 return mpage_writepages(mapping, wbc, ntfs_get_block);
874 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
875 struct buffer_head *bh_result, int create)
877 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
878 bh_result, create, GET_BLOCK_WRITE_BEGIN);
881 int ntfs_write_begin(struct file *file, struct address_space *mapping,
882 loff_t pos, u32 len, struct page **pagep, void **fsdata)
885 struct inode *inode = mapping->host;
886 struct ntfs_inode *ni = ntfs_i(inode);
889 if (is_resident(ni)) {
890 struct page *page = grab_cache_page_write_begin(
891 mapping, pos >> PAGE_SHIFT);
899 err = attr_data_read_resident(ni, page);
909 if (err != E_NTFS_NONRESIDENT)
913 err = block_write_begin(mapping, pos, len, pagep,
914 ntfs_get_block_write_begin);
921 * ntfs_write_end - Address_space_operations::write_end.
923 int ntfs_write_end(struct file *file, struct address_space *mapping,
924 loff_t pos, u32 len, u32 copied, struct page *page,
927 struct inode *inode = mapping->host;
928 struct ntfs_inode *ni = ntfs_i(inode);
929 u64 valid = ni->i_valid;
933 if (is_resident(ni)) {
935 err = attr_data_write_resident(ni, page);
939 /* Clear any buffers in page. */
940 if (page_has_buffers(page)) {
941 struct buffer_head *head, *bh;
943 bh = head = page_buffers(page);
945 clear_buffer_dirty(bh);
946 clear_buffer_mapped(bh);
947 set_buffer_uptodate(bh);
948 } while (head != (bh = bh->b_this_page));
950 SetPageUptodate(page);
956 err = generic_write_end(file, mapping, pos, len, copied, page,
961 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
962 inode->i_ctime = inode->i_mtime = current_time(inode);
963 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
967 if (valid != ni->i_valid) {
968 /* ni->i_valid is changed in ntfs_get_block_vbo. */
973 mark_inode_dirty(inode);
979 int reset_log_file(struct inode *inode)
983 u32 log_size = inode->i_size;
984 struct address_space *mapping = inode->i_mapping;
991 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
993 err = block_write_begin(mapping, pos, len, &page,
994 ntfs_get_block_write_begin);
998 kaddr = kmap_atomic(page);
999 memset(kaddr, -1, len);
1000 kunmap_atomic(kaddr);
1001 flush_dcache_page(page);
1003 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
1008 if (pos >= log_size)
1010 balance_dirty_pages_ratelimited(mapping);
1013 mark_inode_dirty_sync(inode);
1018 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1020 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1023 int ntfs_sync_inode(struct inode *inode)
1025 return _ni_write_inode(inode, 1);
1029 * writeback_inode - Helper function for ntfs_flush_inodes().
1031 * This writes both the inode and the file data blocks, waiting
1032 * for in flight data blocks before the start of the call. It
1033 * does not wait for any io started during the call.
1035 static int writeback_inode(struct inode *inode)
1037 int ret = sync_inode_metadata(inode, 0);
1040 ret = filemap_fdatawrite(inode->i_mapping);
1047 * Write data and metadata corresponding to i1 and i2. The io is
1048 * started but we do not wait for any of it to finish.
1050 * filemap_flush() is used for the block device, so if there is a dirty
1051 * page for a block already in flight, we will not wait and start the
1054 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1060 ret = writeback_inode(i1);
1062 ret = writeback_inode(i2);
1064 ret = sync_blockdev_nowait(sb->s_bdev);
1068 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1072 /* Write non resident data. */
1073 for (idx = 0; bytes; idx++) {
1074 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1075 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1078 return PTR_ERR(page);
1081 WARN_ON(!PageUptodate(page));
1082 ClearPageUptodate(page);
1084 memcpy(page_address(page), data, op);
1086 flush_dcache_page(page);
1087 SetPageUptodate(page);
1090 ntfs_unmap_page(page);
1093 data = Add2Ptr(data, PAGE_SIZE);
1099 * ntfs_reparse_bytes
1101 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1102 * for unicode string of @uni_len length.
1104 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1106 /* Header + unicode string + decorated unicode string. */
1107 return sizeof(short) * (2 * uni_len + 4) +
1108 offsetof(struct REPARSE_DATA_BUFFER,
1109 SymbolicLinkReparseBuffer.PathBuffer);
1112 static struct REPARSE_DATA_BUFFER *
1113 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1114 u32 size, u16 *nsize)
1117 struct REPARSE_DATA_BUFFER *rp;
1119 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1121 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1123 return ERR_PTR(-ENOMEM);
1125 rs = &rp->SymbolicLinkReparseBuffer;
1126 rp_name = rs->PathBuffer;
1128 /* Convert link name to UTF-16. */
1129 err = ntfs_nls_to_utf16(sbi, symname, size,
1130 (struct cpu_str *)(rp_name - 1), 2 * size,
1131 UTF16_LITTLE_ENDIAN);
1135 /* err = the length of unicode name of symlink. */
1136 *nsize = ntfs_reparse_bytes(err);
1138 if (*nsize > sbi->reparse.max_size) {
1143 /* Translate Linux '/' into Windows '\'. */
1144 for (i = 0; i < err; i++) {
1145 if (rp_name[i] == cpu_to_le16('/'))
1146 rp_name[i] = cpu_to_le16('\\');
1149 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1150 rp->ReparseDataLength =
1151 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1152 SymbolicLinkReparseBuffer));
1154 /* PrintName + SubstituteName. */
1155 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1156 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1157 rs->PrintNameLength = rs->SubstituteNameOffset;
1160 * TODO: Use relative path if possible to allow Windows to
1162 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1166 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1168 /* Decorate SubstituteName. */
1170 rp_name[0] = cpu_to_le16('\\');
1171 rp_name[1] = cpu_to_le16('?');
1172 rp_name[2] = cpu_to_le16('?');
1173 rp_name[3] = cpu_to_le16('\\');
1178 return ERR_PTR(err);
1181 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1182 struct inode *dir, struct dentry *dentry,
1183 const struct cpu_str *uni, umode_t mode,
1184 dev_t dev, const char *symname, u32 size,
1185 struct ntfs_fnd *fnd)
1188 struct super_block *sb = dir->i_sb;
1189 struct ntfs_sb_info *sbi = sb->s_fs_info;
1190 const struct qstr *name = &dentry->d_name;
1192 struct ntfs_inode *dir_ni = ntfs_i(dir);
1193 struct ntfs_inode *ni = NULL;
1194 struct inode *inode = NULL;
1195 struct ATTRIB *attr;
1196 struct ATTR_STD_INFO5 *std5;
1197 struct ATTR_FILE_NAME *fname;
1198 struct MFT_REC *rec;
1199 u32 asize, dsize, sd_size;
1200 enum FILE_ATTRIBUTE fa;
1201 __le32 security_id = SECURITY_ID_INVALID;
1204 u16 t16, nsize = 0, aid = 0;
1205 struct INDEX_ROOT *root, *dir_root;
1206 struct NTFS_DE *e, *new_de = NULL;
1207 struct REPARSE_DATA_BUFFER *rp = NULL;
1208 bool rp_inserted = false;
1210 ni_lock_dir(dir_ni);
1212 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1218 if (S_ISDIR(mode)) {
1219 /* Use parent's directory attributes. */
1220 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1221 FILE_ATTRIBUTE_ARCHIVE;
1223 * By default child directory inherits parent attributes.
1224 * Root directory is hidden + system.
1225 * Make an exception for children in root.
1227 if (dir->i_ino == MFT_REC_ROOT)
1228 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1229 } else if (S_ISLNK(mode)) {
1230 /* It is good idea that link should be the same type (file/dir) as target */
1231 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1234 * Linux: there are dir/file/symlink and so on.
1235 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1236 * It is good idea to create:
1237 * dir + reparse if 'symname' points to directory
1239 * file + reparse if 'symname' points to file
1240 * Unfortunately kern_path hangs if symname contains 'dir'.
1246 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1247 * struct inode *target = d_inode(path.dentry);
1249 * if (S_ISDIR(target->i_mode))
1250 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1251 * // if ( target->i_sb == sb ){
1252 * // use relative path?
1257 } else if (S_ISREG(mode)) {
1258 if (sbi->options->sparse) {
1259 /* Sparsed regular file, cause option 'sparse'. */
1260 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1261 FILE_ATTRIBUTE_ARCHIVE;
1262 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1263 /* Compressed regular file, if parent is compressed. */
1264 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1266 /* Regular file, default attributes. */
1267 fa = FILE_ATTRIBUTE_ARCHIVE;
1270 fa = FILE_ATTRIBUTE_ARCHIVE;
1274 fa |= FILE_ATTRIBUTE_READONLY;
1276 /* Allocate PATH_MAX bytes. */
1277 new_de = __getname();
1283 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1284 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1286 /* Step 1: allocate and fill new mft record. */
1287 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1291 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1297 inode = &ni->vfs_inode;
1298 inode_init_owner(mnt_userns, inode, dir, mode);
1299 mode = inode->i_mode;
1301 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1302 current_time(inode);
1305 rec->hard_links = cpu_to_le16(1);
1306 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1308 /* Get default security id. */
1309 sd = s_default_security;
1310 sd_size = sizeof(s_default_security);
1312 if (is_ntfs3(sbi)) {
1313 security_id = dir_ni->std_security_id;
1314 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1315 security_id = sbi->security.def_security_id;
1317 if (security_id == SECURITY_ID_INVALID &&
1318 !ntfs_insert_security(sbi, sd, sd_size,
1319 &security_id, NULL))
1320 sbi->security.def_security_id = security_id;
1324 /* Insert standard info. */
1325 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1327 if (security_id == SECURITY_ID_INVALID) {
1328 dsize = sizeof(struct ATTR_STD_INFO);
1330 dsize = sizeof(struct ATTR_STD_INFO5);
1331 std5->security_id = security_id;
1332 ni->std_security_id = security_id;
1334 asize = SIZEOF_RESIDENT + dsize;
1336 attr->type = ATTR_STD;
1337 attr->size = cpu_to_le32(asize);
1338 attr->id = cpu_to_le16(aid++);
1339 attr->res.data_off = SIZEOF_RESIDENT_LE;
1340 attr->res.data_size = cpu_to_le32(dsize);
1342 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1343 kernel2nt(&inode->i_atime);
1348 attr = Add2Ptr(attr, asize);
1350 /* Insert file name. */
1351 err = fill_name_de(sbi, new_de, name, uni);
1355 mi_get_ref(&ni->mi, &new_de->ref);
1357 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1358 mi_get_ref(&dir_ni->mi, &fname->home);
1359 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1360 fname->dup.a_time = std5->cr_time;
1361 fname->dup.alloc_size = fname->dup.data_size = 0;
1362 fname->dup.fa = std5->fa;
1363 fname->dup.ea_size = fname->dup.reparse = 0;
1365 dsize = le16_to_cpu(new_de->key_size);
1366 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1368 attr->type = ATTR_NAME;
1369 attr->size = cpu_to_le32(asize);
1370 attr->res.data_off = SIZEOF_RESIDENT_LE;
1371 attr->res.flags = RESIDENT_FLAG_INDEXED;
1372 attr->id = cpu_to_le16(aid++);
1373 attr->res.data_size = cpu_to_le32(dsize);
1374 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1376 attr = Add2Ptr(attr, asize);
1378 if (security_id == SECURITY_ID_INVALID) {
1379 /* Insert security attribute. */
1380 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1382 attr->type = ATTR_SECURE;
1383 attr->size = cpu_to_le32(asize);
1384 attr->id = cpu_to_le16(aid++);
1385 attr->res.data_off = SIZEOF_RESIDENT_LE;
1386 attr->res.data_size = cpu_to_le32(sd_size);
1387 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1389 attr = Add2Ptr(attr, asize);
1392 attr->id = cpu_to_le16(aid++);
1393 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1395 * Regular directory or symlink to directory.
1396 * Create root attribute.
1398 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1399 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1401 attr->type = ATTR_ROOT;
1402 attr->size = cpu_to_le32(asize);
1404 attr->name_len = ARRAY_SIZE(I30_NAME);
1405 attr->name_off = SIZEOF_RESIDENT_LE;
1406 attr->res.data_off =
1407 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1408 attr->res.data_size = cpu_to_le32(dsize);
1409 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1412 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1413 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1415 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1416 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1417 sizeof(struct NTFS_DE));
1418 root->ihdr.total = root->ihdr.used;
1420 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1421 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1422 e->flags = NTFS_IE_LAST;
1423 } else if (S_ISLNK(mode)) {
1426 * Create empty resident data attribute.
1428 asize = SIZEOF_RESIDENT;
1430 /* Insert empty ATTR_DATA */
1431 attr->type = ATTR_DATA;
1432 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1433 attr->name_off = SIZEOF_RESIDENT_LE;
1434 attr->res.data_off = SIZEOF_RESIDENT_LE;
1435 } else if (S_ISREG(mode)) {
1437 * Regular file. Create empty non resident data attribute.
1439 attr->type = ATTR_DATA;
1441 attr->nres.evcn = cpu_to_le64(-1ll);
1442 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1443 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1444 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1445 attr->flags = ATTR_FLAG_SPARSED;
1446 asize = SIZEOF_NONRESIDENT_EX + 8;
1447 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1448 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1449 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1450 attr->flags = ATTR_FLAG_COMPRESSED;
1451 attr->nres.c_unit = COMPRESSION_UNIT;
1452 asize = SIZEOF_NONRESIDENT_EX + 8;
1454 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1455 attr->name_off = SIZEOF_NONRESIDENT_LE;
1456 asize = SIZEOF_NONRESIDENT + 8;
1458 attr->nres.run_off = attr->name_off;
1461 * Node. Create empty resident data attribute.
1463 attr->type = ATTR_DATA;
1464 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1465 attr->name_off = SIZEOF_RESIDENT_LE;
1466 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1467 attr->flags = ATTR_FLAG_SPARSED;
1468 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1469 attr->flags = ATTR_FLAG_COMPRESSED;
1470 attr->res.data_off = SIZEOF_RESIDENT_LE;
1471 asize = SIZEOF_RESIDENT;
1472 ni->ni_flags |= NI_FLAG_RESIDENT;
1475 if (S_ISDIR(mode)) {
1476 ni->ni_flags |= NI_FLAG_DIR;
1477 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1480 } else if (S_ISLNK(mode)) {
1481 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1490 * Insert ATTR_REPARSE.
1492 attr = Add2Ptr(attr, asize);
1493 attr->type = ATTR_REPARSE;
1494 attr->id = cpu_to_le16(aid++);
1496 /* Resident or non resident? */
1497 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1498 t16 = PtrOffset(rec, attr);
1501 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1502 * It is good idea to keep extened attributes resident.
1504 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1506 CLST clst = bytes_to_cluster(sbi, nsize);
1508 /* Bytes per runs. */
1509 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1512 attr->nres.evcn = cpu_to_le64(clst - 1);
1513 attr->name_off = SIZEOF_NONRESIDENT_LE;
1514 attr->nres.run_off = attr->name_off;
1515 attr->nres.data_size = cpu_to_le64(nsize);
1516 attr->nres.valid_size = attr->nres.data_size;
1517 attr->nres.alloc_size =
1518 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1520 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1521 clst, NULL, 0, &alen, 0,
1526 err = run_pack(&ni->file.run, 0, clst,
1527 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1537 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1539 attr->res.data_off = SIZEOF_RESIDENT_LE;
1540 attr->res.data_size = cpu_to_le32(nsize);
1541 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1544 /* Size of symlink equals the length of input string. */
1545 inode->i_size = size;
1547 attr->size = cpu_to_le32(asize);
1549 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1557 attr = Add2Ptr(attr, asize);
1558 attr->type = ATTR_END;
1560 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1561 rec->next_attr_id = cpu_to_le16(aid);
1563 /* Step 2: Add new name in index. */
1564 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1568 /* Unlock parent directory before ntfs_init_acl. */
1571 inode->i_generation = le16_to_cpu(rec->seq);
1573 dir->i_mtime = dir->i_ctime = inode->i_atime;
1575 if (S_ISDIR(mode)) {
1576 inode->i_op = &ntfs_dir_inode_operations;
1577 inode->i_fop = &ntfs_dir_operations;
1578 } else if (S_ISLNK(mode)) {
1579 inode->i_op = &ntfs_link_inode_operations;
1580 inode->i_fop = NULL;
1581 inode->i_mapping->a_ops = &ntfs_aops;
1582 inode->i_size = size;
1583 inode_nohighmem(inode);
1584 } else if (S_ISREG(mode)) {
1585 inode->i_op = &ntfs_file_inode_operations;
1586 inode->i_fop = &ntfs_file_operations;
1587 inode->i_mapping->a_ops =
1588 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1589 init_rwsem(&ni->file.run_lock);
1591 inode->i_op = &ntfs_special_inode_operations;
1592 init_special_inode(inode, mode, dev);
1595 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1596 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1597 err = ntfs_init_acl(mnt_userns, inode, dir);
1603 inode->i_flags |= S_NOSEC;
1606 /* Write non resident data. */
1608 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1614 * Call 'd_instantiate' after inode->i_op is set
1615 * but before finish_open.
1617 d_instantiate(dentry, inode);
1619 ntfs_save_wsl_perm(inode);
1620 mark_inode_dirty(dir);
1621 mark_inode_dirty(inode);
1628 /* Undo 'indx_insert_entry'. */
1629 ni_lock_dir(dir_ni);
1630 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1631 le16_to_cpu(new_de->key_size), sbi);
1632 /* ni_unlock(dir_ni); will be called later. */
1635 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1638 if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1641 run_deallocate(sbi, &ni->file.run, false);
1644 clear_rec_inuse(rec);
1646 ni->mi.dirty = false;
1647 discard_new_inode(inode);
1649 ntfs_mark_rec_free(sbi, ino, false);
1658 return ERR_PTR(err);
1661 unlock_new_inode(inode);
1666 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1669 struct ntfs_inode *ni = ntfs_i(inode);
1670 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1673 /* Allocate PATH_MAX bytes. */
1678 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1679 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1681 /* Construct 'de'. */
1682 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1686 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1695 * inode_operations::unlink
1696 * inode_operations::rmdir
1698 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1701 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1702 struct inode *inode = d_inode(dentry);
1703 struct ntfs_inode *ni = ntfs_i(inode);
1704 struct ntfs_inode *dir_ni = ntfs_i(dir);
1705 struct NTFS_DE *de, *de2 = NULL;
1708 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1711 /* Allocate PATH_MAX bytes. */
1718 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1723 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1728 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1732 dir->i_mtime = dir->i_ctime = current_time(dir);
1733 mark_inode_dirty(dir);
1734 inode->i_ctime = dir->i_ctime;
1736 mark_inode_dirty(inode);
1737 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1738 _ntfs_bad_inode(inode);
1740 if (ni_is_dirty(dir))
1741 mark_inode_dirty(dir);
1742 if (ni_is_dirty(inode))
1743 mark_inode_dirty(inode);
1752 void ntfs_evict_inode(struct inode *inode)
1754 truncate_inode_pages_final(&inode->i_data);
1757 _ni_write_inode(inode, inode_needs_sync(inode));
1759 invalidate_inode_buffers(inode);
1762 ni_clear(ntfs_i(inode));
1765 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1768 int i, err = -EINVAL;
1769 struct ntfs_inode *ni = ntfs_i(inode);
1770 struct super_block *sb = inode->i_sb;
1771 struct ntfs_sb_info *sbi = sb->s_fs_info;
1774 void *to_free = NULL;
1775 struct REPARSE_DATA_BUFFER *rp;
1776 const __le16 *uname;
1777 struct ATTRIB *attr;
1779 /* Reparse data present. Try to parse it. */
1780 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1781 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1785 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1789 if (!attr->non_res) {
1790 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1793 size = le32_to_cpu(attr->res.data_size);
1795 size = le64_to_cpu(attr->nres.data_size);
1799 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1803 rp = kmalloc(size, GFP_NOFS);
1809 /* Read into temporal buffer. */
1810 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1815 /* Microsoft Tag. */
1816 switch (rp->ReparseTag) {
1817 case IO_REPARSE_TAG_MOUNT_POINT:
1818 /* Mount points and junctions. */
1819 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1820 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1821 MountPointReparseBuffer.PathBuffer))
1824 offsetof(struct REPARSE_DATA_BUFFER,
1825 MountPointReparseBuffer.PathBuffer) +
1826 le16_to_cpu(rp->MountPointReparseBuffer
1828 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1831 case IO_REPARSE_TAG_SYMLINK:
1832 /* FolderSymbolicLink */
1833 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1834 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1835 SymbolicLinkReparseBuffer.PathBuffer))
1838 rp, offsetof(struct REPARSE_DATA_BUFFER,
1839 SymbolicLinkReparseBuffer.PathBuffer) +
1840 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1843 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1846 case IO_REPARSE_TAG_CLOUD:
1847 case IO_REPARSE_TAG_CLOUD_1:
1848 case IO_REPARSE_TAG_CLOUD_2:
1849 case IO_REPARSE_TAG_CLOUD_3:
1850 case IO_REPARSE_TAG_CLOUD_4:
1851 case IO_REPARSE_TAG_CLOUD_5:
1852 case IO_REPARSE_TAG_CLOUD_6:
1853 case IO_REPARSE_TAG_CLOUD_7:
1854 case IO_REPARSE_TAG_CLOUD_8:
1855 case IO_REPARSE_TAG_CLOUD_9:
1856 case IO_REPARSE_TAG_CLOUD_A:
1857 case IO_REPARSE_TAG_CLOUD_B:
1858 case IO_REPARSE_TAG_CLOUD_C:
1859 case IO_REPARSE_TAG_CLOUD_D:
1860 case IO_REPARSE_TAG_CLOUD_E:
1861 case IO_REPARSE_TAG_CLOUD_F:
1862 err = sizeof("OneDrive") - 1;
1865 memcpy(buffer, "OneDrive", err);
1869 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1870 /* Unknown Microsoft Tag. */
1873 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1874 size <= sizeof(struct REPARSE_POINT)) {
1879 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1880 ulen = le16_to_cpu(rp->ReparseDataLength) -
1881 sizeof(struct REPARSE_POINT);
1884 /* Convert nlen from bytes to UNICODE chars. */
1887 /* Check that name is available. */
1888 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1891 /* If name is already zero terminated then truncate it now. */
1892 if (!uname[ulen - 1])
1895 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1900 /* Translate Windows '\' into Linux '/'. */
1901 for (i = 0; i < err; i++) {
1902 if (buffer[i] == '\\')
1906 /* Always set last zero. */
1913 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1914 struct delayed_call *done)
1920 return ERR_PTR(-ECHILD);
1922 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1924 return ERR_PTR(-ENOMEM);
1926 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1929 return ERR_PTR(err);
1932 set_delayed_call(done, kfree_link, ret);
1938 const struct inode_operations ntfs_link_inode_operations = {
1939 .get_link = ntfs_get_link,
1940 .setattr = ntfs3_setattr,
1941 .listxattr = ntfs_listxattr,
1942 .permission = ntfs_permission,
1945 const struct address_space_operations ntfs_aops = {
1946 .read_folio = ntfs_read_folio,
1947 .readahead = ntfs_readahead,
1948 .writepage = ntfs_writepage,
1949 .writepages = ntfs_writepages,
1950 .write_begin = ntfs_write_begin,
1951 .write_end = ntfs_write_end,
1952 .direct_IO = ntfs_direct_IO,
1954 .dirty_folio = block_dirty_folio,
1955 .invalidate_folio = block_invalidate_folio,
1958 const struct address_space_operations ntfs_aops_cmpr = {
1959 .read_folio = ntfs_read_folio,
1960 .readahead = ntfs_readahead,