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)
135 switch (attr->type) {
138 asize < sizeof(struct ATTR_STD_INFO) + roff ||
139 rsize < sizeof(struct ATTR_STD_INFO))
145 std5 = Add2Ptr(attr, roff);
148 nt2kernel(std5->cr_time, &ni->i_crtime);
150 nt2kernel(std5->a_time, &inode->i_atime);
151 nt2kernel(std5->c_time, &inode->i_ctime);
152 nt2kernel(std5->m_time, &inode->i_mtime);
154 ni->std_fa = std5->fa;
156 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
157 rsize >= sizeof(struct ATTR_STD_INFO5))
158 ni->std_security_id = std5->security_id;
162 if (attr->name_len || le || ino == MFT_REC_LOG)
165 err = ntfs_load_attr_list(ni, attr);
174 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
175 rsize < SIZEOF_ATTRIBUTE_FILENAME)
178 fname = Add2Ptr(attr, roff);
179 if (fname->type == FILE_NAME_DOS)
183 if (name && name->len == fname->name_len &&
184 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
192 /* Ignore data attribute in dir record. */
196 if (ino == MFT_REC_BADCLUST && !attr->non_res)
199 if (attr->name_len &&
200 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
201 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
202 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
203 (ino != MFT_REC_SECURE || !attr->non_res ||
204 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
205 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
206 /* File contains stream attribute. Ignore it. */
210 if (is_attr_sparsed(attr))
211 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
213 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
215 if (is_attr_compressed(attr))
216 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
218 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
220 if (is_attr_encrypted(attr))
221 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
223 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
225 if (!attr->non_res) {
226 ni->i_valid = inode->i_size = rsize;
227 inode_set_bytes(inode, rsize);
230 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
232 if (!attr->non_res) {
233 ni->ni_flags |= NI_FLAG_RESIDENT;
237 inode_set_bytes(inode, attr_ondisk_size(attr));
239 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
240 inode->i_size = le64_to_cpu(attr->nres.data_size);
241 if (!attr->nres.alloc_size)
244 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
252 root = Add2Ptr(attr, roff);
255 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
256 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
259 if (root->type != ATTR_NAME ||
260 root->rule != NTFS_COLLATION_TYPE_FILENAME)
266 ni->ni_flags |= NI_FLAG_DIR;
268 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
273 ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
278 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
279 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
282 inode->i_size = le64_to_cpu(attr->nres.data_size);
283 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
284 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
286 run = &ni->dir.alloc_run;
290 if (ino == MFT_REC_MFT) {
293 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
294 /* 0x20000000 = 2^32 / 8 */
295 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
298 run = &sbi->mft.bitmap.run;
300 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
301 !memcmp(attr_name(attr), I30_NAME,
304 run = &ni->dir.bitmap_run;
313 rp_fa = ni_parse_reparse(ni, attr, &rp);
318 * Assume one unicode symbol == one utf8.
320 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
324 ni->i_valid = inode->i_size;
326 /* Clear directory bit. */
327 if (ni->ni_flags & NI_FLAG_DIR) {
328 indx_clear(&ni->dir);
329 memset(&ni->dir, 0, sizeof(ni->dir));
330 ni->ni_flags &= ~NI_FLAG_DIR;
332 run_close(&ni->file.run);
334 mode = S_IFLNK | 0777;
338 goto attr_unpack_run; // Double break.
342 case REPARSE_COMPRESSED:
345 case REPARSE_DEDUPLICATED:
351 if (!attr->name_len &&
352 resident_data_ex(attr, sizeof(struct EA_INFO))) {
353 ni->ni_flags |= NI_FLAG_EA;
355 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
357 inode->i_mode = mode;
358 ntfs_get_wsl_perm(inode);
359 mode = inode->i_mode;
368 roff = le16_to_cpu(attr->nres.run_off);
375 t64 = le64_to_cpu(attr->nres.svcn);
377 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
378 t64, Add2Ptr(attr, roff), asize - roff);
389 if (!is_match && name) {
390 /* Reuse rec as buffer for ascii name. */
395 if (std5->fa & FILE_ATTRIBUTE_READONLY)
403 if (names != le16_to_cpu(rec->hard_links)) {
404 /* Correct minor error on the fly. Do not mark inode as dirty. */
405 rec->hard_links = cpu_to_le16(names);
409 set_nlink(inode, names);
412 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
415 * Dot and dot-dot should be included in count but was not
416 * included in enumeration.
417 * Usually a hard links to directories are disabled.
419 inode->i_op = &ntfs_dir_inode_operations;
420 inode->i_fop = &ntfs_dir_operations;
422 } else if (S_ISLNK(mode)) {
423 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
424 inode->i_op = &ntfs_link_inode_operations;
426 inode_nohighmem(inode);
427 } else if (S_ISREG(mode)) {
428 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
429 inode->i_op = &ntfs_file_inode_operations;
430 inode->i_fop = &ntfs_file_operations;
431 inode->i_mapping->a_ops =
432 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
433 if (ino != MFT_REC_MFT)
434 init_rwsem(&ni->file.run_lock);
435 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
437 inode->i_op = &ntfs_special_inode_operations;
438 init_special_inode(inode, mode, inode->i_rdev);
439 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
440 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
441 /* Records in $Extend are not a files or general directories. */
442 inode->i_op = &ntfs_file_inode_operations;
448 if ((sbi->options->sys_immutable &&
449 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
450 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
451 inode->i_flags |= S_IMMUTABLE;
453 inode->i_flags &= ~S_IMMUTABLE;
456 inode->i_mode = mode;
457 if (!(ni->ni_flags & NI_FLAG_EA)) {
458 /* If no xattr then no security (stored in xattr). */
459 inode->i_flags |= S_NOSEC;
463 if (ino == MFT_REC_MFT && !sb->s_root)
466 unlock_new_inode(inode);
471 if (ino == MFT_REC_MFT && !sb->s_root)
481 * Return: 1 if match.
483 static int ntfs_test_inode(struct inode *inode, void *data)
485 struct MFT_REF *ref = data;
487 return ino_get(ref) == inode->i_ino;
490 static int ntfs_set_inode(struct inode *inode, void *data)
492 const struct MFT_REF *ref = data;
494 inode->i_ino = ino_get(ref);
498 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
499 const struct cpu_str *name)
503 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
505 if (unlikely(!inode))
506 return ERR_PTR(-ENOMEM);
508 /* If this is a freshly allocated inode, need to read it now. */
509 if (inode->i_state & I_NEW)
510 inode = ntfs_read_mft(inode, name, ref);
511 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
512 /* Inode overlaps? */
513 _ntfs_bad_inode(inode);
520 GET_BLOCK_GENERAL = 0,
521 GET_BLOCK_WRITE_BEGIN = 1,
522 GET_BLOCK_DIRECT_IO_R = 2,
523 GET_BLOCK_DIRECT_IO_W = 3,
527 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
528 struct buffer_head *bh, int create,
529 enum get_block_ctx ctx)
531 struct super_block *sb = inode->i_sb;
532 struct ntfs_sb_info *sbi = sb->s_fs_info;
533 struct ntfs_inode *ni = ntfs_i(inode);
534 struct page *page = bh->b_page;
535 u8 cluster_bits = sbi->cluster_bits;
536 u32 block_size = sb->s_blocksize;
537 u64 bytes, lbo, valid;
543 /* Clear previous state. */
544 clear_buffer_new(bh);
545 clear_buffer_uptodate(bh);
547 /* Direct write uses 'create=0'. */
548 if (!create && vbo >= ni->i_valid) {
553 if (vbo >= inode->i_size) {
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);
580 bytes = ((u64)len << cluster_bits) - off;
582 if (lcn == SPARSE_LCN) {
584 if (bh->b_size > bytes)
593 if ((len << cluster_bits) > block_size)
594 ntfs_sparse_cluster(inode, page, vcn, len);
597 lbo = ((u64)lcn << cluster_bits) + off;
599 set_buffer_mapped(bh);
600 bh->b_bdev = sb->s_bdev;
601 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
605 if (ctx == GET_BLOCK_DIRECT_IO_W) {
606 /* ntfs_direct_IO will update ni->i_valid. */
611 if (bytes > bh->b_size)
617 if (vbo + bytes > valid) {
618 ni->i_valid = vbo + bytes;
619 mark_inode_dirty(inode);
621 } else if (vbo >= valid) {
622 /* Read out of valid data. */
623 /* Should never be here 'cause already checked. */
624 clear_buffer_mapped(bh);
625 } else if (vbo + bytes <= valid) {
627 } else if (vbo + block_size <= valid) {
628 /* Normal short read. */
632 * Read across valid size: vbo < valid && valid < vbo + block_size
637 u32 voff = valid - vbo;
639 bh->b_size = block_size;
640 off = vbo & (PAGE_SIZE - 1);
641 set_bh_page(bh, page, off);
642 err = bh_read(bh, 0);
645 zero_user_segment(page, off + voff, off + block_size);
649 if (bh->b_size > bytes)
653 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
654 static_assert(sizeof(size_t) < sizeof(loff_t));
655 if (bytes > 0x40000000u)
656 bh->b_size = 0x40000000u;
666 int ntfs_get_block(struct inode *inode, sector_t vbn,
667 struct buffer_head *bh_result, int create)
669 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
670 bh_result, create, GET_BLOCK_GENERAL);
673 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
674 struct buffer_head *bh_result, int create)
676 return ntfs_get_block_vbo(inode,
677 (u64)vsn << inode->i_sb->s_blocksize_bits,
678 bh_result, create, GET_BLOCK_BMAP);
681 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
683 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
686 static int ntfs_read_folio(struct file *file, struct folio *folio)
688 struct page *page = &folio->page;
690 struct address_space *mapping = page->mapping;
691 struct inode *inode = mapping->host;
692 struct ntfs_inode *ni = ntfs_i(inode);
694 if (is_resident(ni)) {
696 err = attr_data_read_resident(ni, page);
698 if (err != E_NTFS_NONRESIDENT) {
704 if (is_compressed(ni)) {
706 err = ni_readpage_cmpr(ni, page);
711 /* Normal + sparse files. */
712 return mpage_read_folio(folio, ntfs_get_block);
715 static void ntfs_readahead(struct readahead_control *rac)
717 struct address_space *mapping = rac->mapping;
718 struct inode *inode = mapping->host;
719 struct ntfs_inode *ni = ntfs_i(inode);
723 if (is_resident(ni)) {
724 /* No readahead for resident. */
728 if (is_compressed(ni)) {
729 /* No readahead for compressed. */
734 pos = readahead_pos(rac);
736 if (valid < i_size_read(inode) && pos <= valid &&
737 valid < pos + readahead_length(rac)) {
738 /* Range cross 'valid'. Read it page by page. */
742 mpage_readahead(rac, ntfs_get_block);
745 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
746 struct buffer_head *bh_result, int create)
748 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
749 bh_result, create, GET_BLOCK_DIRECT_IO_R);
752 static int ntfs_get_block_direct_IO_W(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_W);
759 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
761 struct file *file = iocb->ki_filp;
762 struct address_space *mapping = file->f_mapping;
763 struct inode *inode = mapping->host;
764 struct ntfs_inode *ni = ntfs_i(inode);
765 loff_t vbo = iocb->ki_pos;
767 int wr = iov_iter_rw(iter) & WRITE;
768 size_t iter_count = iov_iter_count(iter);
772 if (is_resident(ni)) {
773 /* Switch to buffered write. */
778 ret = blockdev_direct_IO(iocb, inode, iter,
779 wr ? ntfs_get_block_direct_IO_W
780 : ntfs_get_block_direct_IO_R);
784 else if (wr && ret == -EIOCBQUEUED)
785 end = vbo + iter_count;
791 if (end > valid && !S_ISBLK(inode->i_mode)) {
793 mark_inode_dirty(inode);
795 } else if (vbo < valid && valid < end) {
797 iov_iter_revert(iter, end - valid);
798 iov_iter_zero(end - valid, iter);
805 int ntfs_set_size(struct inode *inode, u64 new_size)
807 struct super_block *sb = inode->i_sb;
808 struct ntfs_sb_info *sbi = sb->s_fs_info;
809 struct ntfs_inode *ni = ntfs_i(inode);
812 /* Check for maximum file size. */
813 if (is_sparsed(ni) || is_compressed(ni)) {
814 if (new_size > sbi->maxbytes_sparse) {
818 } else if (new_size > sbi->maxbytes) {
824 down_write(&ni->file.run_lock);
826 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
827 &ni->i_valid, true, NULL);
829 up_write(&ni->file.run_lock);
832 mark_inode_dirty(inode);
838 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
840 struct address_space *mapping = page->mapping;
841 struct inode *inode = mapping->host;
842 struct ntfs_inode *ni = ntfs_i(inode);
845 if (is_resident(ni)) {
847 err = attr_data_write_resident(ni, page);
849 if (err != E_NTFS_NONRESIDENT) {
855 return block_write_full_page(page, ntfs_get_block, wbc);
858 static int ntfs_writepages(struct address_space *mapping,
859 struct writeback_control *wbc)
861 /* Redirect call to 'ntfs_writepage' for resident files. */
862 if (is_resident(ntfs_i(mapping->host)))
863 return generic_writepages(mapping, wbc);
864 return mpage_writepages(mapping, wbc, ntfs_get_block);
867 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
868 struct buffer_head *bh_result, int create)
870 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
871 bh_result, create, GET_BLOCK_WRITE_BEGIN);
874 int ntfs_write_begin(struct file *file, struct address_space *mapping,
875 loff_t pos, u32 len, struct page **pagep, void **fsdata)
878 struct inode *inode = mapping->host;
879 struct ntfs_inode *ni = ntfs_i(inode);
882 if (is_resident(ni)) {
883 struct page *page = grab_cache_page_write_begin(
884 mapping, pos >> PAGE_SHIFT);
892 err = attr_data_read_resident(ni, page);
902 if (err != E_NTFS_NONRESIDENT)
906 err = block_write_begin(mapping, pos, len, pagep,
907 ntfs_get_block_write_begin);
914 * ntfs_write_end - Address_space_operations::write_end.
916 int ntfs_write_end(struct file *file, struct address_space *mapping,
917 loff_t pos, u32 len, u32 copied, struct page *page,
920 struct inode *inode = mapping->host;
921 struct ntfs_inode *ni = ntfs_i(inode);
922 u64 valid = ni->i_valid;
926 if (is_resident(ni)) {
928 err = attr_data_write_resident(ni, page);
932 /* Clear any buffers in page. */
933 if (page_has_buffers(page)) {
934 struct buffer_head *head, *bh;
936 bh = head = page_buffers(page);
938 clear_buffer_dirty(bh);
939 clear_buffer_mapped(bh);
940 set_buffer_uptodate(bh);
941 } while (head != (bh = bh->b_this_page));
943 SetPageUptodate(page);
949 err = generic_write_end(file, mapping, pos, len, copied, page,
954 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
955 inode->i_ctime = inode->i_mtime = current_time(inode);
956 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
960 if (valid != ni->i_valid) {
961 /* ni->i_valid is changed in ntfs_get_block_vbo. */
966 mark_inode_dirty(inode);
972 int reset_log_file(struct inode *inode)
976 u32 log_size = inode->i_size;
977 struct address_space *mapping = inode->i_mapping;
984 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
986 err = block_write_begin(mapping, pos, len, &page,
987 ntfs_get_block_write_begin);
991 kaddr = kmap_atomic(page);
992 memset(kaddr, -1, len);
993 kunmap_atomic(kaddr);
994 flush_dcache_page(page);
996 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
1001 if (pos >= log_size)
1003 balance_dirty_pages_ratelimited(mapping);
1006 mark_inode_dirty_sync(inode);
1011 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1013 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1016 int ntfs_sync_inode(struct inode *inode)
1018 return _ni_write_inode(inode, 1);
1022 * writeback_inode - Helper function for ntfs_flush_inodes().
1024 * This writes both the inode and the file data blocks, waiting
1025 * for in flight data blocks before the start of the call. It
1026 * does not wait for any io started during the call.
1028 static int writeback_inode(struct inode *inode)
1030 int ret = sync_inode_metadata(inode, 0);
1033 ret = filemap_fdatawrite(inode->i_mapping);
1040 * Write data and metadata corresponding to i1 and i2. The io is
1041 * started but we do not wait for any of it to finish.
1043 * filemap_flush() is used for the block device, so if there is a dirty
1044 * page for a block already in flight, we will not wait and start the
1047 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1053 ret = writeback_inode(i1);
1055 ret = writeback_inode(i2);
1057 ret = sync_blockdev_nowait(sb->s_bdev);
1061 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1065 /* Write non resident data. */
1066 for (idx = 0; bytes; idx++) {
1067 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1068 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1071 return PTR_ERR(page);
1074 WARN_ON(!PageUptodate(page));
1075 ClearPageUptodate(page);
1077 memcpy(page_address(page), data, op);
1079 flush_dcache_page(page);
1080 SetPageUptodate(page);
1083 ntfs_unmap_page(page);
1086 data = Add2Ptr(data, PAGE_SIZE);
1092 * ntfs_reparse_bytes
1094 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1095 * for unicode string of @uni_len length.
1097 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1099 /* Header + unicode string + decorated unicode string. */
1100 return sizeof(short) * (2 * uni_len + 4) +
1101 offsetof(struct REPARSE_DATA_BUFFER,
1102 SymbolicLinkReparseBuffer.PathBuffer);
1105 static struct REPARSE_DATA_BUFFER *
1106 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1107 u32 size, u16 *nsize)
1110 struct REPARSE_DATA_BUFFER *rp;
1112 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1114 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1116 return ERR_PTR(-ENOMEM);
1118 rs = &rp->SymbolicLinkReparseBuffer;
1119 rp_name = rs->PathBuffer;
1121 /* Convert link name to UTF-16. */
1122 err = ntfs_nls_to_utf16(sbi, symname, size,
1123 (struct cpu_str *)(rp_name - 1), 2 * size,
1124 UTF16_LITTLE_ENDIAN);
1128 /* err = the length of unicode name of symlink. */
1129 *nsize = ntfs_reparse_bytes(err);
1131 if (*nsize > sbi->reparse.max_size) {
1136 /* Translate Linux '/' into Windows '\'. */
1137 for (i = 0; i < err; i++) {
1138 if (rp_name[i] == cpu_to_le16('/'))
1139 rp_name[i] = cpu_to_le16('\\');
1142 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1143 rp->ReparseDataLength =
1144 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1145 SymbolicLinkReparseBuffer));
1147 /* PrintName + SubstituteName. */
1148 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1149 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1150 rs->PrintNameLength = rs->SubstituteNameOffset;
1153 * TODO: Use relative path if possible to allow Windows to
1155 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1159 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1161 /* Decorate SubstituteName. */
1163 rp_name[0] = cpu_to_le16('\\');
1164 rp_name[1] = cpu_to_le16('?');
1165 rp_name[2] = cpu_to_le16('?');
1166 rp_name[3] = cpu_to_le16('\\');
1171 return ERR_PTR(err);
1174 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1175 struct inode *dir, struct dentry *dentry,
1176 const struct cpu_str *uni, umode_t mode,
1177 dev_t dev, const char *symname, u32 size,
1178 struct ntfs_fnd *fnd)
1181 struct super_block *sb = dir->i_sb;
1182 struct ntfs_sb_info *sbi = sb->s_fs_info;
1183 const struct qstr *name = &dentry->d_name;
1185 struct ntfs_inode *dir_ni = ntfs_i(dir);
1186 struct ntfs_inode *ni = NULL;
1187 struct inode *inode = NULL;
1188 struct ATTRIB *attr;
1189 struct ATTR_STD_INFO5 *std5;
1190 struct ATTR_FILE_NAME *fname;
1191 struct MFT_REC *rec;
1192 u32 asize, dsize, sd_size;
1193 enum FILE_ATTRIBUTE fa;
1194 __le32 security_id = SECURITY_ID_INVALID;
1197 u16 t16, nsize = 0, aid = 0;
1198 struct INDEX_ROOT *root, *dir_root;
1199 struct NTFS_DE *e, *new_de = NULL;
1200 struct REPARSE_DATA_BUFFER *rp = NULL;
1201 bool rp_inserted = false;
1203 ni_lock_dir(dir_ni);
1205 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1211 if (S_ISDIR(mode)) {
1212 /* Use parent's directory attributes. */
1213 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1214 FILE_ATTRIBUTE_ARCHIVE;
1216 * By default child directory inherits parent attributes.
1217 * Root directory is hidden + system.
1218 * Make an exception for children in root.
1220 if (dir->i_ino == MFT_REC_ROOT)
1221 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1222 } else if (S_ISLNK(mode)) {
1223 /* It is good idea that link should be the same type (file/dir) as target */
1224 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1227 * Linux: there are dir/file/symlink and so on.
1228 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1229 * It is good idea to create:
1230 * dir + reparse if 'symname' points to directory
1232 * file + reparse if 'symname' points to file
1233 * Unfortunately kern_path hangs if symname contains 'dir'.
1239 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1240 * struct inode *target = d_inode(path.dentry);
1242 * if (S_ISDIR(target->i_mode))
1243 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1244 * // if ( target->i_sb == sb ){
1245 * // use relative path?
1250 } else if (S_ISREG(mode)) {
1251 if (sbi->options->sparse) {
1252 /* Sparsed regular file, cause option 'sparse'. */
1253 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1254 FILE_ATTRIBUTE_ARCHIVE;
1255 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1256 /* Compressed regular file, if parent is compressed. */
1257 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1259 /* Regular file, default attributes. */
1260 fa = FILE_ATTRIBUTE_ARCHIVE;
1263 fa = FILE_ATTRIBUTE_ARCHIVE;
1267 fa |= FILE_ATTRIBUTE_READONLY;
1269 /* Allocate PATH_MAX bytes. */
1270 new_de = __getname();
1276 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1277 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1279 /* Step 1: allocate and fill new mft record. */
1280 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1284 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1290 inode = &ni->vfs_inode;
1291 inode_init_owner(mnt_userns, inode, dir, mode);
1292 mode = inode->i_mode;
1294 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1295 current_time(inode);
1298 rec->hard_links = cpu_to_le16(1);
1299 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1301 /* Get default security id. */
1302 sd = s_default_security;
1303 sd_size = sizeof(s_default_security);
1305 if (is_ntfs3(sbi)) {
1306 security_id = dir_ni->std_security_id;
1307 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1308 security_id = sbi->security.def_security_id;
1310 if (security_id == SECURITY_ID_INVALID &&
1311 !ntfs_insert_security(sbi, sd, sd_size,
1312 &security_id, NULL))
1313 sbi->security.def_security_id = security_id;
1317 /* Insert standard info. */
1318 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1320 if (security_id == SECURITY_ID_INVALID) {
1321 dsize = sizeof(struct ATTR_STD_INFO);
1323 dsize = sizeof(struct ATTR_STD_INFO5);
1324 std5->security_id = security_id;
1325 ni->std_security_id = security_id;
1327 asize = SIZEOF_RESIDENT + dsize;
1329 attr->type = ATTR_STD;
1330 attr->size = cpu_to_le32(asize);
1331 attr->id = cpu_to_le16(aid++);
1332 attr->res.data_off = SIZEOF_RESIDENT_LE;
1333 attr->res.data_size = cpu_to_le32(dsize);
1335 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1336 kernel2nt(&inode->i_atime);
1341 attr = Add2Ptr(attr, asize);
1343 /* Insert file name. */
1344 err = fill_name_de(sbi, new_de, name, uni);
1348 mi_get_ref(&ni->mi, &new_de->ref);
1350 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1351 mi_get_ref(&dir_ni->mi, &fname->home);
1352 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1353 fname->dup.a_time = std5->cr_time;
1354 fname->dup.alloc_size = fname->dup.data_size = 0;
1355 fname->dup.fa = std5->fa;
1356 fname->dup.ea_size = fname->dup.reparse = 0;
1358 dsize = le16_to_cpu(new_de->key_size);
1359 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1361 attr->type = ATTR_NAME;
1362 attr->size = cpu_to_le32(asize);
1363 attr->res.data_off = SIZEOF_RESIDENT_LE;
1364 attr->res.flags = RESIDENT_FLAG_INDEXED;
1365 attr->id = cpu_to_le16(aid++);
1366 attr->res.data_size = cpu_to_le32(dsize);
1367 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1369 attr = Add2Ptr(attr, asize);
1371 if (security_id == SECURITY_ID_INVALID) {
1372 /* Insert security attribute. */
1373 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1375 attr->type = ATTR_SECURE;
1376 attr->size = cpu_to_le32(asize);
1377 attr->id = cpu_to_le16(aid++);
1378 attr->res.data_off = SIZEOF_RESIDENT_LE;
1379 attr->res.data_size = cpu_to_le32(sd_size);
1380 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1382 attr = Add2Ptr(attr, asize);
1385 attr->id = cpu_to_le16(aid++);
1386 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1388 * Regular directory or symlink to directory.
1389 * Create root attribute.
1391 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1392 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1394 attr->type = ATTR_ROOT;
1395 attr->size = cpu_to_le32(asize);
1397 attr->name_len = ARRAY_SIZE(I30_NAME);
1398 attr->name_off = SIZEOF_RESIDENT_LE;
1399 attr->res.data_off =
1400 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1401 attr->res.data_size = cpu_to_le32(dsize);
1402 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1405 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1406 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1408 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1409 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1410 sizeof(struct NTFS_DE));
1411 root->ihdr.total = root->ihdr.used;
1413 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1414 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1415 e->flags = NTFS_IE_LAST;
1416 } else if (S_ISLNK(mode)) {
1419 * Create empty resident data attribute.
1421 asize = SIZEOF_RESIDENT;
1423 /* Insert empty ATTR_DATA */
1424 attr->type = ATTR_DATA;
1425 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1426 attr->name_off = SIZEOF_RESIDENT_LE;
1427 attr->res.data_off = SIZEOF_RESIDENT_LE;
1428 } else if (S_ISREG(mode)) {
1430 * Regular file. Create empty non resident data attribute.
1432 attr->type = ATTR_DATA;
1434 attr->nres.evcn = cpu_to_le64(-1ll);
1435 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1436 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1437 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1438 attr->flags = ATTR_FLAG_SPARSED;
1439 asize = SIZEOF_NONRESIDENT_EX + 8;
1440 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1441 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1442 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1443 attr->flags = ATTR_FLAG_COMPRESSED;
1444 attr->nres.c_unit = COMPRESSION_UNIT;
1445 asize = SIZEOF_NONRESIDENT_EX + 8;
1447 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1448 attr->name_off = SIZEOF_NONRESIDENT_LE;
1449 asize = SIZEOF_NONRESIDENT + 8;
1451 attr->nres.run_off = attr->name_off;
1454 * Node. Create empty resident data attribute.
1456 attr->type = ATTR_DATA;
1457 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1458 attr->name_off = SIZEOF_RESIDENT_LE;
1459 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1460 attr->flags = ATTR_FLAG_SPARSED;
1461 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1462 attr->flags = ATTR_FLAG_COMPRESSED;
1463 attr->res.data_off = SIZEOF_RESIDENT_LE;
1464 asize = SIZEOF_RESIDENT;
1465 ni->ni_flags |= NI_FLAG_RESIDENT;
1468 if (S_ISDIR(mode)) {
1469 ni->ni_flags |= NI_FLAG_DIR;
1470 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1473 } else if (S_ISLNK(mode)) {
1474 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1483 * Insert ATTR_REPARSE.
1485 attr = Add2Ptr(attr, asize);
1486 attr->type = ATTR_REPARSE;
1487 attr->id = cpu_to_le16(aid++);
1489 /* Resident or non resident? */
1490 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1491 t16 = PtrOffset(rec, attr);
1494 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1495 * It is good idea to keep extened attributes resident.
1497 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1499 CLST clst = bytes_to_cluster(sbi, nsize);
1501 /* Bytes per runs. */
1502 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1505 attr->nres.evcn = cpu_to_le64(clst - 1);
1506 attr->name_off = SIZEOF_NONRESIDENT_LE;
1507 attr->nres.run_off = attr->name_off;
1508 attr->nres.data_size = cpu_to_le64(nsize);
1509 attr->nres.valid_size = attr->nres.data_size;
1510 attr->nres.alloc_size =
1511 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1513 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1514 clst, NULL, 0, &alen, 0,
1519 err = run_pack(&ni->file.run, 0, clst,
1520 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1530 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1532 attr->res.data_off = SIZEOF_RESIDENT_LE;
1533 attr->res.data_size = cpu_to_le32(nsize);
1534 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1537 /* Size of symlink equals the length of input string. */
1538 inode->i_size = size;
1540 attr->size = cpu_to_le32(asize);
1542 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1550 attr = Add2Ptr(attr, asize);
1551 attr->type = ATTR_END;
1553 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1554 rec->next_attr_id = cpu_to_le16(aid);
1556 /* Step 2: Add new name in index. */
1557 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1561 /* Unlock parent directory before ntfs_init_acl. */
1564 inode->i_generation = le16_to_cpu(rec->seq);
1566 dir->i_mtime = dir->i_ctime = inode->i_atime;
1568 if (S_ISDIR(mode)) {
1569 inode->i_op = &ntfs_dir_inode_operations;
1570 inode->i_fop = &ntfs_dir_operations;
1571 } else if (S_ISLNK(mode)) {
1572 inode->i_op = &ntfs_link_inode_operations;
1573 inode->i_fop = NULL;
1574 inode->i_mapping->a_ops = &ntfs_aops;
1575 inode->i_size = size;
1576 inode_nohighmem(inode);
1577 } else if (S_ISREG(mode)) {
1578 inode->i_op = &ntfs_file_inode_operations;
1579 inode->i_fop = &ntfs_file_operations;
1580 inode->i_mapping->a_ops =
1581 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1582 init_rwsem(&ni->file.run_lock);
1584 inode->i_op = &ntfs_special_inode_operations;
1585 init_special_inode(inode, mode, dev);
1588 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1589 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1590 err = ntfs_init_acl(mnt_userns, inode, dir);
1596 inode->i_flags |= S_NOSEC;
1599 /* Write non resident data. */
1601 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1607 * Call 'd_instantiate' after inode->i_op is set
1608 * but before finish_open.
1610 d_instantiate(dentry, inode);
1612 ntfs_save_wsl_perm(inode);
1613 mark_inode_dirty(dir);
1614 mark_inode_dirty(inode);
1621 /* Undo 'indx_insert_entry'. */
1622 ni_lock_dir(dir_ni);
1623 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1624 le16_to_cpu(new_de->key_size), sbi);
1625 /* ni_unlock(dir_ni); will be called later. */
1628 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1631 if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1634 run_deallocate(sbi, &ni->file.run, false);
1637 clear_rec_inuse(rec);
1639 ni->mi.dirty = false;
1640 discard_new_inode(inode);
1642 ntfs_mark_rec_free(sbi, ino, false);
1651 return ERR_PTR(err);
1654 unlock_new_inode(inode);
1659 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1662 struct ntfs_inode *ni = ntfs_i(inode);
1663 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1666 /* Allocate PATH_MAX bytes. */
1671 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1672 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1674 /* Construct 'de'. */
1675 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1679 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1688 * inode_operations::unlink
1689 * inode_operations::rmdir
1691 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1694 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1695 struct inode *inode = d_inode(dentry);
1696 struct ntfs_inode *ni = ntfs_i(inode);
1697 struct ntfs_inode *dir_ni = ntfs_i(dir);
1698 struct NTFS_DE *de, *de2 = NULL;
1701 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1704 /* Allocate PATH_MAX bytes. */
1711 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1716 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1721 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1725 dir->i_mtime = dir->i_ctime = current_time(dir);
1726 mark_inode_dirty(dir);
1727 inode->i_ctime = dir->i_ctime;
1729 mark_inode_dirty(inode);
1730 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1731 _ntfs_bad_inode(inode);
1733 if (ni_is_dirty(dir))
1734 mark_inode_dirty(dir);
1735 if (ni_is_dirty(inode))
1736 mark_inode_dirty(inode);
1745 void ntfs_evict_inode(struct inode *inode)
1747 truncate_inode_pages_final(&inode->i_data);
1750 _ni_write_inode(inode, inode_needs_sync(inode));
1752 invalidate_inode_buffers(inode);
1755 ni_clear(ntfs_i(inode));
1758 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1761 int i, err = -EINVAL;
1762 struct ntfs_inode *ni = ntfs_i(inode);
1763 struct super_block *sb = inode->i_sb;
1764 struct ntfs_sb_info *sbi = sb->s_fs_info;
1767 void *to_free = NULL;
1768 struct REPARSE_DATA_BUFFER *rp;
1769 const __le16 *uname;
1770 struct ATTRIB *attr;
1772 /* Reparse data present. Try to parse it. */
1773 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1774 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1778 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1782 if (!attr->non_res) {
1783 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1786 size = le32_to_cpu(attr->res.data_size);
1788 size = le64_to_cpu(attr->nres.data_size);
1792 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1796 rp = kmalloc(size, GFP_NOFS);
1802 /* Read into temporal buffer. */
1803 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1808 /* Microsoft Tag. */
1809 switch (rp->ReparseTag) {
1810 case IO_REPARSE_TAG_MOUNT_POINT:
1811 /* Mount points and junctions. */
1812 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1813 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1814 MountPointReparseBuffer.PathBuffer))
1817 offsetof(struct REPARSE_DATA_BUFFER,
1818 MountPointReparseBuffer.PathBuffer) +
1819 le16_to_cpu(rp->MountPointReparseBuffer
1821 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1824 case IO_REPARSE_TAG_SYMLINK:
1825 /* FolderSymbolicLink */
1826 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1827 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1828 SymbolicLinkReparseBuffer.PathBuffer))
1831 rp, offsetof(struct REPARSE_DATA_BUFFER,
1832 SymbolicLinkReparseBuffer.PathBuffer) +
1833 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1836 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1839 case IO_REPARSE_TAG_CLOUD:
1840 case IO_REPARSE_TAG_CLOUD_1:
1841 case IO_REPARSE_TAG_CLOUD_2:
1842 case IO_REPARSE_TAG_CLOUD_3:
1843 case IO_REPARSE_TAG_CLOUD_4:
1844 case IO_REPARSE_TAG_CLOUD_5:
1845 case IO_REPARSE_TAG_CLOUD_6:
1846 case IO_REPARSE_TAG_CLOUD_7:
1847 case IO_REPARSE_TAG_CLOUD_8:
1848 case IO_REPARSE_TAG_CLOUD_9:
1849 case IO_REPARSE_TAG_CLOUD_A:
1850 case IO_REPARSE_TAG_CLOUD_B:
1851 case IO_REPARSE_TAG_CLOUD_C:
1852 case IO_REPARSE_TAG_CLOUD_D:
1853 case IO_REPARSE_TAG_CLOUD_E:
1854 case IO_REPARSE_TAG_CLOUD_F:
1855 err = sizeof("OneDrive") - 1;
1858 memcpy(buffer, "OneDrive", err);
1862 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1863 /* Unknown Microsoft Tag. */
1866 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1867 size <= sizeof(struct REPARSE_POINT)) {
1872 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1873 ulen = le16_to_cpu(rp->ReparseDataLength) -
1874 sizeof(struct REPARSE_POINT);
1877 /* Convert nlen from bytes to UNICODE chars. */
1880 /* Check that name is available. */
1881 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1884 /* If name is already zero terminated then truncate it now. */
1885 if (!uname[ulen - 1])
1888 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1893 /* Translate Windows '\' into Linux '/'. */
1894 for (i = 0; i < err; i++) {
1895 if (buffer[i] == '\\')
1899 /* Always set last zero. */
1906 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1907 struct delayed_call *done)
1913 return ERR_PTR(-ECHILD);
1915 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1917 return ERR_PTR(-ENOMEM);
1919 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1922 return ERR_PTR(err);
1925 set_delayed_call(done, kfree_link, ret);
1931 const struct inode_operations ntfs_link_inode_operations = {
1932 .get_link = ntfs_get_link,
1933 .setattr = ntfs3_setattr,
1934 .listxattr = ntfs_listxattr,
1935 .permission = ntfs_permission,
1938 const struct address_space_operations ntfs_aops = {
1939 .read_folio = ntfs_read_folio,
1940 .readahead = ntfs_readahead,
1941 .writepage = ntfs_writepage,
1942 .writepages = ntfs_writepages,
1943 .write_begin = ntfs_write_begin,
1944 .write_end = ntfs_write_end,
1945 .direct_IO = ntfs_direct_IO,
1947 .dirty_folio = block_dirty_folio,
1948 .invalidate_folio = block_invalidate_folio,
1951 const struct address_space_operations ntfs_aops_cmpr = {
1952 .read_folio = ntfs_read_folio,
1953 .readahead = ntfs_readahead,