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 /* MFT_REC_MFT is not a dir */
104 if (is_dir && ino == MFT_REC_MFT) {
109 inode->i_generation = le16_to_cpu(rec->seq);
111 /* Enumerate all struct Attributes MFT. */
116 * To reduce tab pressure use goto instead of
117 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
122 attr = ni_enum_attr_ex(ni, attr, &le, NULL);
127 /* This is non primary attribute segment. Ignore if not MFT. */
128 if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
132 asize = le32_to_cpu(attr->size);
133 goto attr_unpack_run;
136 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
137 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
138 asize = le32_to_cpu(attr->size);
140 if (le16_to_cpu(attr->name_off) + attr->name_len > asize)
144 t64 = le64_to_cpu(attr->nres.alloc_size);
145 if (le64_to_cpu(attr->nres.data_size) > t64 ||
146 le64_to_cpu(attr->nres.valid_size) > t64)
150 switch (attr->type) {
153 asize < sizeof(struct ATTR_STD_INFO) + roff ||
154 rsize < sizeof(struct ATTR_STD_INFO))
160 std5 = Add2Ptr(attr, roff);
163 nt2kernel(std5->cr_time, &ni->i_crtime);
165 nt2kernel(std5->a_time, &inode->i_atime);
166 nt2kernel(std5->c_time, &inode->i_ctime);
167 nt2kernel(std5->m_time, &inode->i_mtime);
169 ni->std_fa = std5->fa;
171 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
172 rsize >= sizeof(struct ATTR_STD_INFO5))
173 ni->std_security_id = std5->security_id;
177 if (attr->name_len || le || ino == MFT_REC_LOG)
180 err = ntfs_load_attr_list(ni, attr);
189 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
190 rsize < SIZEOF_ATTRIBUTE_FILENAME)
193 fname = Add2Ptr(attr, roff);
194 if (fname->type == FILE_NAME_DOS)
198 if (name && name->len == fname->name_len &&
199 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
207 /* Ignore data attribute in dir record. */
211 if (ino == MFT_REC_BADCLUST && !attr->non_res)
214 if (attr->name_len &&
215 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
216 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
217 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
218 (ino != MFT_REC_SECURE || !attr->non_res ||
219 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
220 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
221 /* File contains stream attribute. Ignore it. */
225 if (is_attr_sparsed(attr))
226 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
228 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
230 if (is_attr_compressed(attr))
231 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
233 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
235 if (is_attr_encrypted(attr))
236 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
238 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
240 if (!attr->non_res) {
241 ni->i_valid = inode->i_size = rsize;
242 inode_set_bytes(inode, rsize);
245 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
247 if (!attr->non_res) {
248 ni->ni_flags |= NI_FLAG_RESIDENT;
252 inode_set_bytes(inode, attr_ondisk_size(attr));
254 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
255 inode->i_size = le64_to_cpu(attr->nres.data_size);
256 if (!attr->nres.alloc_size)
259 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
267 root = Add2Ptr(attr, roff);
269 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
270 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
273 if (root->type != ATTR_NAME ||
274 root->rule != NTFS_COLLATION_TYPE_FILENAME)
281 ni->ni_flags |= NI_FLAG_DIR;
283 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
288 ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
293 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
294 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
297 inode->i_size = le64_to_cpu(attr->nres.data_size);
298 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
299 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
301 run = &ni->dir.alloc_run;
305 if (ino == MFT_REC_MFT) {
308 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
309 /* 0x20000000 = 2^32 / 8 */
310 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
313 run = &sbi->mft.bitmap.run;
315 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
316 !memcmp(attr_name(attr), I30_NAME,
319 run = &ni->dir.bitmap_run;
328 rp_fa = ni_parse_reparse(ni, attr, &rp);
333 * Assume one unicode symbol == one utf8.
335 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
339 ni->i_valid = inode->i_size;
341 /* Clear directory bit. */
342 if (ni->ni_flags & NI_FLAG_DIR) {
343 indx_clear(&ni->dir);
344 memset(&ni->dir, 0, sizeof(ni->dir));
345 ni->ni_flags &= ~NI_FLAG_DIR;
347 run_close(&ni->file.run);
349 mode = S_IFLNK | 0777;
353 goto attr_unpack_run; // Double break.
357 case REPARSE_COMPRESSED:
360 case REPARSE_DEDUPLICATED:
366 if (!attr->name_len &&
367 resident_data_ex(attr, sizeof(struct EA_INFO))) {
368 ni->ni_flags |= NI_FLAG_EA;
370 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
372 inode->i_mode = mode;
373 ntfs_get_wsl_perm(inode);
374 mode = inode->i_mode;
383 roff = le16_to_cpu(attr->nres.run_off);
390 t64 = le64_to_cpu(attr->nres.svcn);
392 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
393 t64, Add2Ptr(attr, roff), asize - roff);
404 if (!is_match && name) {
409 if (std5->fa & FILE_ATTRIBUTE_READONLY)
417 if (names != le16_to_cpu(rec->hard_links)) {
418 /* Correct minor error on the fly. Do not mark inode as dirty. */
419 ntfs_inode_warn(inode, "Correct links count -> %u.", names);
420 rec->hard_links = cpu_to_le16(names);
424 set_nlink(inode, names);
427 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
430 * Dot and dot-dot should be included in count but was not
431 * included in enumeration.
432 * Usually a hard links to directories are disabled.
434 inode->i_op = &ntfs_dir_inode_operations;
435 inode->i_fop = &ntfs_dir_operations;
437 } else if (S_ISLNK(mode)) {
438 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
439 inode->i_op = &ntfs_link_inode_operations;
441 inode_nohighmem(inode);
442 } else if (S_ISREG(mode)) {
443 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
444 inode->i_op = &ntfs_file_inode_operations;
445 inode->i_fop = &ntfs_file_operations;
446 inode->i_mapping->a_ops =
447 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
448 if (ino != MFT_REC_MFT)
449 init_rwsem(&ni->file.run_lock);
450 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
452 inode->i_op = &ntfs_special_inode_operations;
453 init_special_inode(inode, mode, inode->i_rdev);
454 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
455 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
456 /* Records in $Extend are not a files or general directories. */
457 inode->i_op = &ntfs_file_inode_operations;
463 if ((sbi->options->sys_immutable &&
464 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
465 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
466 inode->i_flags |= S_IMMUTABLE;
468 inode->i_flags &= ~S_IMMUTABLE;
471 inode->i_mode = mode;
472 if (!(ni->ni_flags & NI_FLAG_EA)) {
473 /* If no xattr then no security (stored in xattr). */
474 inode->i_flags |= S_NOSEC;
477 if (ino == MFT_REC_MFT && !sb->s_root)
480 unlock_new_inode(inode);
485 if (ino == MFT_REC_MFT && !sb->s_root)
495 * Return: 1 if match.
497 static int ntfs_test_inode(struct inode *inode, void *data)
499 struct MFT_REF *ref = data;
501 return ino_get(ref) == inode->i_ino;
504 static int ntfs_set_inode(struct inode *inode, void *data)
506 const struct MFT_REF *ref = data;
508 inode->i_ino = ino_get(ref);
512 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
513 const struct cpu_str *name)
517 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
519 if (unlikely(!inode))
520 return ERR_PTR(-ENOMEM);
522 /* If this is a freshly allocated inode, need to read it now. */
523 if (inode->i_state & I_NEW)
524 inode = ntfs_read_mft(inode, name, ref);
525 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
526 /* Inode overlaps? */
527 _ntfs_bad_inode(inode);
530 if (IS_ERR(inode) && name)
531 ntfs_set_state(sb->s_fs_info, NTFS_DIRTY_ERROR);
537 GET_BLOCK_GENERAL = 0,
538 GET_BLOCK_WRITE_BEGIN = 1,
539 GET_BLOCK_DIRECT_IO_R = 2,
540 GET_BLOCK_DIRECT_IO_W = 3,
544 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
545 struct buffer_head *bh, int create,
546 enum get_block_ctx ctx)
548 struct super_block *sb = inode->i_sb;
549 struct ntfs_sb_info *sbi = sb->s_fs_info;
550 struct ntfs_inode *ni = ntfs_i(inode);
551 struct page *page = bh->b_page;
552 u8 cluster_bits = sbi->cluster_bits;
553 u32 block_size = sb->s_blocksize;
554 u64 bytes, lbo, valid;
560 /* Clear previous state. */
561 clear_buffer_new(bh);
562 clear_buffer_uptodate(bh);
564 /* Direct write uses 'create=0'. */
565 if (!create && vbo >= ni->i_valid) {
570 if (vbo >= inode->i_size) {
575 if (is_resident(ni)) {
577 err = attr_data_read_resident(ni, page);
581 set_buffer_uptodate(bh);
582 bh->b_size = block_size;
586 vcn = vbo >> cluster_bits;
587 off = vbo & sbi->cluster_mask;
590 err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
597 bytes = ((u64)len << cluster_bits) - off;
599 if (lcn == SPARSE_LCN) {
601 if (bh->b_size > bytes)
610 if ((len << cluster_bits) > block_size)
611 ntfs_sparse_cluster(inode, page, vcn, len);
614 lbo = ((u64)lcn << cluster_bits) + off;
616 set_buffer_mapped(bh);
617 bh->b_bdev = sb->s_bdev;
618 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
622 if (ctx == GET_BLOCK_DIRECT_IO_W) {
623 /* ntfs_direct_IO will update ni->i_valid. */
628 if (bytes > bh->b_size)
634 if (vbo + bytes > valid) {
635 ni->i_valid = vbo + bytes;
636 mark_inode_dirty(inode);
638 } else if (vbo >= valid) {
639 /* Read out of valid data. */
640 /* Should never be here 'cause already checked. */
641 clear_buffer_mapped(bh);
642 } else if (vbo + bytes <= valid) {
644 } else if (vbo + block_size <= valid) {
645 /* Normal short read. */
649 * Read across valid size: vbo < valid && valid < vbo + block_size
654 u32 voff = valid - vbo;
656 bh->b_size = block_size;
657 off = vbo & (PAGE_SIZE - 1);
658 set_bh_page(bh, page, off);
659 err = bh_read(bh, 0);
662 zero_user_segment(page, off + voff, off + block_size);
666 if (bh->b_size > bytes)
670 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
671 static_assert(sizeof(size_t) < sizeof(loff_t));
672 if (bytes > 0x40000000u)
673 bh->b_size = 0x40000000u;
683 int ntfs_get_block(struct inode *inode, sector_t vbn,
684 struct buffer_head *bh_result, int create)
686 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
687 bh_result, create, GET_BLOCK_GENERAL);
690 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
691 struct buffer_head *bh_result, int create)
693 return ntfs_get_block_vbo(inode,
694 (u64)vsn << inode->i_sb->s_blocksize_bits,
695 bh_result, create, GET_BLOCK_BMAP);
698 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
700 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
703 static int ntfs_read_folio(struct file *file, struct folio *folio)
705 struct page *page = &folio->page;
707 struct address_space *mapping = page->mapping;
708 struct inode *inode = mapping->host;
709 struct ntfs_inode *ni = ntfs_i(inode);
711 if (is_resident(ni)) {
713 err = attr_data_read_resident(ni, page);
715 if (err != E_NTFS_NONRESIDENT) {
721 if (is_compressed(ni)) {
723 err = ni_readpage_cmpr(ni, page);
728 /* Normal + sparse files. */
729 return mpage_read_folio(folio, ntfs_get_block);
732 static void ntfs_readahead(struct readahead_control *rac)
734 struct address_space *mapping = rac->mapping;
735 struct inode *inode = mapping->host;
736 struct ntfs_inode *ni = ntfs_i(inode);
740 if (is_resident(ni)) {
741 /* No readahead for resident. */
745 if (is_compressed(ni)) {
746 /* No readahead for compressed. */
751 pos = readahead_pos(rac);
753 if (valid < i_size_read(inode) && pos <= valid &&
754 valid < pos + readahead_length(rac)) {
755 /* Range cross 'valid'. Read it page by page. */
759 mpage_readahead(rac, ntfs_get_block);
762 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
763 struct buffer_head *bh_result, int create)
765 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
766 bh_result, create, GET_BLOCK_DIRECT_IO_R);
769 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
770 struct buffer_head *bh_result, int create)
772 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
773 bh_result, create, GET_BLOCK_DIRECT_IO_W);
776 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
778 struct file *file = iocb->ki_filp;
779 struct address_space *mapping = file->f_mapping;
780 struct inode *inode = mapping->host;
781 struct ntfs_inode *ni = ntfs_i(inode);
782 loff_t vbo = iocb->ki_pos;
784 int wr = iov_iter_rw(iter) & WRITE;
785 size_t iter_count = iov_iter_count(iter);
789 if (is_resident(ni)) {
790 /* Switch to buffered write. */
795 ret = blockdev_direct_IO(iocb, inode, iter,
796 wr ? ntfs_get_block_direct_IO_W
797 : ntfs_get_block_direct_IO_R);
801 else if (wr && ret == -EIOCBQUEUED)
802 end = vbo + iter_count;
808 if (end > valid && !S_ISBLK(inode->i_mode)) {
810 mark_inode_dirty(inode);
812 } else if (vbo < valid && valid < end) {
814 iov_iter_revert(iter, end - valid);
815 iov_iter_zero(end - valid, iter);
822 int ntfs_set_size(struct inode *inode, u64 new_size)
824 struct super_block *sb = inode->i_sb;
825 struct ntfs_sb_info *sbi = sb->s_fs_info;
826 struct ntfs_inode *ni = ntfs_i(inode);
829 /* Check for maximum file size. */
830 if (is_sparsed(ni) || is_compressed(ni)) {
831 if (new_size > sbi->maxbytes_sparse) {
835 } else if (new_size > sbi->maxbytes) {
841 down_write(&ni->file.run_lock);
843 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
844 &ni->i_valid, true, NULL);
846 up_write(&ni->file.run_lock);
849 mark_inode_dirty(inode);
855 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
857 struct address_space *mapping = page->mapping;
858 struct inode *inode = mapping->host;
859 struct ntfs_inode *ni = ntfs_i(inode);
862 if (is_resident(ni)) {
864 err = attr_data_write_resident(ni, page);
866 if (err != E_NTFS_NONRESIDENT) {
872 return block_write_full_page(page, ntfs_get_block, wbc);
875 static int ntfs_writepages(struct address_space *mapping,
876 struct writeback_control *wbc)
878 /* Redirect call to 'ntfs_writepage' for resident files. */
879 if (is_resident(ntfs_i(mapping->host)))
880 return generic_writepages(mapping, wbc);
881 return mpage_writepages(mapping, wbc, ntfs_get_block);
884 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
885 struct buffer_head *bh_result, int create)
887 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
888 bh_result, create, GET_BLOCK_WRITE_BEGIN);
891 int ntfs_write_begin(struct file *file, struct address_space *mapping,
892 loff_t pos, u32 len, struct page **pagep, void **fsdata)
895 struct inode *inode = mapping->host;
896 struct ntfs_inode *ni = ntfs_i(inode);
899 if (is_resident(ni)) {
900 struct page *page = grab_cache_page_write_begin(
901 mapping, pos >> PAGE_SHIFT);
909 err = attr_data_read_resident(ni, page);
919 if (err != E_NTFS_NONRESIDENT)
923 err = block_write_begin(mapping, pos, len, pagep,
924 ntfs_get_block_write_begin);
931 * ntfs_write_end - Address_space_operations::write_end.
933 int ntfs_write_end(struct file *file, struct address_space *mapping,
934 loff_t pos, u32 len, u32 copied, struct page *page,
937 struct inode *inode = mapping->host;
938 struct ntfs_inode *ni = ntfs_i(inode);
939 u64 valid = ni->i_valid;
943 if (is_resident(ni)) {
945 err = attr_data_write_resident(ni, page);
949 /* Clear any buffers in page. */
950 if (page_has_buffers(page)) {
951 struct buffer_head *head, *bh;
953 bh = head = page_buffers(page);
955 clear_buffer_dirty(bh);
956 clear_buffer_mapped(bh);
957 set_buffer_uptodate(bh);
958 } while (head != (bh = bh->b_this_page));
960 SetPageUptodate(page);
966 err = generic_write_end(file, mapping, pos, len, copied, page,
971 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
972 inode->i_ctime = inode->i_mtime = current_time(inode);
973 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
977 if (valid != ni->i_valid) {
978 /* ni->i_valid is changed in ntfs_get_block_vbo. */
983 mark_inode_dirty(inode);
989 int reset_log_file(struct inode *inode)
993 u32 log_size = inode->i_size;
994 struct address_space *mapping = inode->i_mapping;
1001 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
1003 err = block_write_begin(mapping, pos, len, &page,
1004 ntfs_get_block_write_begin);
1008 kaddr = kmap_atomic(page);
1009 memset(kaddr, -1, len);
1010 kunmap_atomic(kaddr);
1011 flush_dcache_page(page);
1013 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
1018 if (pos >= log_size)
1020 balance_dirty_pages_ratelimited(mapping);
1023 mark_inode_dirty_sync(inode);
1028 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1030 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1033 int ntfs_sync_inode(struct inode *inode)
1035 return _ni_write_inode(inode, 1);
1039 * writeback_inode - Helper function for ntfs_flush_inodes().
1041 * This writes both the inode and the file data blocks, waiting
1042 * for in flight data blocks before the start of the call. It
1043 * does not wait for any io started during the call.
1045 static int writeback_inode(struct inode *inode)
1047 int ret = sync_inode_metadata(inode, 0);
1050 ret = filemap_fdatawrite(inode->i_mapping);
1057 * Write data and metadata corresponding to i1 and i2. The io is
1058 * started but we do not wait for any of it to finish.
1060 * filemap_flush() is used for the block device, so if there is a dirty
1061 * page for a block already in flight, we will not wait and start the
1064 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1070 ret = writeback_inode(i1);
1072 ret = writeback_inode(i2);
1074 ret = sync_blockdev_nowait(sb->s_bdev);
1078 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1082 /* Write non resident data. */
1083 for (idx = 0; bytes; idx++) {
1084 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1085 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1088 return PTR_ERR(page);
1091 WARN_ON(!PageUptodate(page));
1092 ClearPageUptodate(page);
1094 memcpy(page_address(page), data, op);
1096 flush_dcache_page(page);
1097 SetPageUptodate(page);
1100 ntfs_unmap_page(page);
1103 data = Add2Ptr(data, PAGE_SIZE);
1109 * ntfs_reparse_bytes
1111 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1112 * for unicode string of @uni_len length.
1114 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1116 /* Header + unicode string + decorated unicode string. */
1117 return sizeof(short) * (2 * uni_len + 4) +
1118 offsetof(struct REPARSE_DATA_BUFFER,
1119 SymbolicLinkReparseBuffer.PathBuffer);
1122 static struct REPARSE_DATA_BUFFER *
1123 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1124 u32 size, u16 *nsize)
1127 struct REPARSE_DATA_BUFFER *rp;
1129 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1131 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1133 return ERR_PTR(-ENOMEM);
1135 rs = &rp->SymbolicLinkReparseBuffer;
1136 rp_name = rs->PathBuffer;
1138 /* Convert link name to UTF-16. */
1139 err = ntfs_nls_to_utf16(sbi, symname, size,
1140 (struct cpu_str *)(rp_name - 1), 2 * size,
1141 UTF16_LITTLE_ENDIAN);
1145 /* err = the length of unicode name of symlink. */
1146 *nsize = ntfs_reparse_bytes(err);
1148 if (*nsize > sbi->reparse.max_size) {
1153 /* Translate Linux '/' into Windows '\'. */
1154 for (i = 0; i < err; i++) {
1155 if (rp_name[i] == cpu_to_le16('/'))
1156 rp_name[i] = cpu_to_le16('\\');
1159 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1160 rp->ReparseDataLength =
1161 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1162 SymbolicLinkReparseBuffer));
1164 /* PrintName + SubstituteName. */
1165 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1166 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1167 rs->PrintNameLength = rs->SubstituteNameOffset;
1170 * TODO: Use relative path if possible to allow Windows to
1172 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1176 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1178 /* Decorate SubstituteName. */
1180 rp_name[0] = cpu_to_le16('\\');
1181 rp_name[1] = cpu_to_le16('?');
1182 rp_name[2] = cpu_to_le16('?');
1183 rp_name[3] = cpu_to_le16('\\');
1188 return ERR_PTR(err);
1191 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1192 struct inode *dir, struct dentry *dentry,
1193 const struct cpu_str *uni, umode_t mode,
1194 dev_t dev, const char *symname, u32 size,
1195 struct ntfs_fnd *fnd)
1198 struct super_block *sb = dir->i_sb;
1199 struct ntfs_sb_info *sbi = sb->s_fs_info;
1200 const struct qstr *name = &dentry->d_name;
1202 struct ntfs_inode *dir_ni = ntfs_i(dir);
1203 struct ntfs_inode *ni = NULL;
1204 struct inode *inode = NULL;
1205 struct ATTRIB *attr;
1206 struct ATTR_STD_INFO5 *std5;
1207 struct ATTR_FILE_NAME *fname;
1208 struct MFT_REC *rec;
1209 u32 asize, dsize, sd_size;
1210 enum FILE_ATTRIBUTE fa;
1211 __le32 security_id = SECURITY_ID_INVALID;
1214 u16 t16, nsize = 0, aid = 0;
1215 struct INDEX_ROOT *root, *dir_root;
1216 struct NTFS_DE *e, *new_de = NULL;
1217 struct REPARSE_DATA_BUFFER *rp = NULL;
1218 bool rp_inserted = false;
1220 ni_lock_dir(dir_ni);
1222 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1228 if (S_ISDIR(mode)) {
1229 /* Use parent's directory attributes. */
1230 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1231 FILE_ATTRIBUTE_ARCHIVE;
1233 * By default child directory inherits parent attributes.
1234 * Root directory is hidden + system.
1235 * Make an exception for children in root.
1237 if (dir->i_ino == MFT_REC_ROOT)
1238 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1239 } else if (S_ISLNK(mode)) {
1240 /* It is good idea that link should be the same type (file/dir) as target */
1241 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1244 * Linux: there are dir/file/symlink and so on.
1245 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1246 * It is good idea to create:
1247 * dir + reparse if 'symname' points to directory
1249 * file + reparse if 'symname' points to file
1250 * Unfortunately kern_path hangs if symname contains 'dir'.
1256 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1257 * struct inode *target = d_inode(path.dentry);
1259 * if (S_ISDIR(target->i_mode))
1260 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1261 * // if ( target->i_sb == sb ){
1262 * // use relative path?
1267 } else if (S_ISREG(mode)) {
1268 if (sbi->options->sparse) {
1269 /* Sparsed regular file, cause option 'sparse'. */
1270 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1271 FILE_ATTRIBUTE_ARCHIVE;
1272 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1273 /* Compressed regular file, if parent is compressed. */
1274 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1276 /* Regular file, default attributes. */
1277 fa = FILE_ATTRIBUTE_ARCHIVE;
1280 fa = FILE_ATTRIBUTE_ARCHIVE;
1284 fa |= FILE_ATTRIBUTE_READONLY;
1286 /* Allocate PATH_MAX bytes. */
1287 new_de = __getname();
1293 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1294 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1296 /* Step 1: allocate and fill new mft record. */
1297 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1301 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1307 inode = &ni->vfs_inode;
1308 inode_init_owner(mnt_userns, inode, dir, mode);
1309 mode = inode->i_mode;
1311 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1312 current_time(inode);
1315 rec->hard_links = cpu_to_le16(1);
1316 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1318 /* Get default security id. */
1319 sd = s_default_security;
1320 sd_size = sizeof(s_default_security);
1322 if (is_ntfs3(sbi)) {
1323 security_id = dir_ni->std_security_id;
1324 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1325 security_id = sbi->security.def_security_id;
1327 if (security_id == SECURITY_ID_INVALID &&
1328 !ntfs_insert_security(sbi, sd, sd_size,
1329 &security_id, NULL))
1330 sbi->security.def_security_id = security_id;
1334 /* Insert standard info. */
1335 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1337 if (security_id == SECURITY_ID_INVALID) {
1338 dsize = sizeof(struct ATTR_STD_INFO);
1340 dsize = sizeof(struct ATTR_STD_INFO5);
1341 std5->security_id = security_id;
1342 ni->std_security_id = security_id;
1344 asize = SIZEOF_RESIDENT + dsize;
1346 attr->type = ATTR_STD;
1347 attr->size = cpu_to_le32(asize);
1348 attr->id = cpu_to_le16(aid++);
1349 attr->res.data_off = SIZEOF_RESIDENT_LE;
1350 attr->res.data_size = cpu_to_le32(dsize);
1352 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1353 kernel2nt(&inode->i_atime);
1358 attr = Add2Ptr(attr, asize);
1360 /* Insert file name. */
1361 err = fill_name_de(sbi, new_de, name, uni);
1365 mi_get_ref(&ni->mi, &new_de->ref);
1367 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1368 mi_get_ref(&dir_ni->mi, &fname->home);
1369 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1370 fname->dup.a_time = std5->cr_time;
1371 fname->dup.alloc_size = fname->dup.data_size = 0;
1372 fname->dup.fa = std5->fa;
1373 fname->dup.ea_size = fname->dup.reparse = 0;
1375 dsize = le16_to_cpu(new_de->key_size);
1376 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1378 attr->type = ATTR_NAME;
1379 attr->size = cpu_to_le32(asize);
1380 attr->res.data_off = SIZEOF_RESIDENT_LE;
1381 attr->res.flags = RESIDENT_FLAG_INDEXED;
1382 attr->id = cpu_to_le16(aid++);
1383 attr->res.data_size = cpu_to_le32(dsize);
1384 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1386 attr = Add2Ptr(attr, asize);
1388 if (security_id == SECURITY_ID_INVALID) {
1389 /* Insert security attribute. */
1390 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1392 attr->type = ATTR_SECURE;
1393 attr->size = cpu_to_le32(asize);
1394 attr->id = cpu_to_le16(aid++);
1395 attr->res.data_off = SIZEOF_RESIDENT_LE;
1396 attr->res.data_size = cpu_to_le32(sd_size);
1397 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1399 attr = Add2Ptr(attr, asize);
1402 attr->id = cpu_to_le16(aid++);
1403 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1405 * Regular directory or symlink to directory.
1406 * Create root attribute.
1408 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1409 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1411 attr->type = ATTR_ROOT;
1412 attr->size = cpu_to_le32(asize);
1414 attr->name_len = ARRAY_SIZE(I30_NAME);
1415 attr->name_off = SIZEOF_RESIDENT_LE;
1416 attr->res.data_off =
1417 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1418 attr->res.data_size = cpu_to_le32(dsize);
1419 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1422 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1423 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1425 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1426 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1427 sizeof(struct NTFS_DE));
1428 root->ihdr.total = root->ihdr.used;
1430 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1431 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1432 e->flags = NTFS_IE_LAST;
1433 } else if (S_ISLNK(mode)) {
1436 * Create empty resident data attribute.
1438 asize = SIZEOF_RESIDENT;
1440 /* Insert empty ATTR_DATA */
1441 attr->type = ATTR_DATA;
1442 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1443 attr->name_off = SIZEOF_RESIDENT_LE;
1444 attr->res.data_off = SIZEOF_RESIDENT_LE;
1445 } else if (S_ISREG(mode)) {
1447 * Regular file. Create empty non resident data attribute.
1449 attr->type = ATTR_DATA;
1451 attr->nres.evcn = cpu_to_le64(-1ll);
1452 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1453 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1454 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1455 attr->flags = ATTR_FLAG_SPARSED;
1456 asize = SIZEOF_NONRESIDENT_EX + 8;
1457 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1458 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1459 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1460 attr->flags = ATTR_FLAG_COMPRESSED;
1461 attr->nres.c_unit = COMPRESSION_UNIT;
1462 asize = SIZEOF_NONRESIDENT_EX + 8;
1464 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1465 attr->name_off = SIZEOF_NONRESIDENT_LE;
1466 asize = SIZEOF_NONRESIDENT + 8;
1468 attr->nres.run_off = attr->name_off;
1471 * Node. Create empty resident data attribute.
1473 attr->type = ATTR_DATA;
1474 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1475 attr->name_off = SIZEOF_RESIDENT_LE;
1476 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1477 attr->flags = ATTR_FLAG_SPARSED;
1478 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1479 attr->flags = ATTR_FLAG_COMPRESSED;
1480 attr->res.data_off = SIZEOF_RESIDENT_LE;
1481 asize = SIZEOF_RESIDENT;
1482 ni->ni_flags |= NI_FLAG_RESIDENT;
1485 if (S_ISDIR(mode)) {
1486 ni->ni_flags |= NI_FLAG_DIR;
1487 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1490 } else if (S_ISLNK(mode)) {
1491 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1500 * Insert ATTR_REPARSE.
1502 attr = Add2Ptr(attr, asize);
1503 attr->type = ATTR_REPARSE;
1504 attr->id = cpu_to_le16(aid++);
1506 /* Resident or non resident? */
1507 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1508 t16 = PtrOffset(rec, attr);
1511 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1512 * It is good idea to keep extened attributes resident.
1514 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1516 CLST clst = bytes_to_cluster(sbi, nsize);
1518 /* Bytes per runs. */
1519 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1522 attr->nres.evcn = cpu_to_le64(clst - 1);
1523 attr->name_off = SIZEOF_NONRESIDENT_LE;
1524 attr->nres.run_off = attr->name_off;
1525 attr->nres.data_size = cpu_to_le64(nsize);
1526 attr->nres.valid_size = attr->nres.data_size;
1527 attr->nres.alloc_size =
1528 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1530 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1531 clst, NULL, 0, &alen, 0,
1536 err = run_pack(&ni->file.run, 0, clst,
1537 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1547 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1549 attr->res.data_off = SIZEOF_RESIDENT_LE;
1550 attr->res.data_size = cpu_to_le32(nsize);
1551 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1554 /* Size of symlink equals the length of input string. */
1555 inode->i_size = size;
1557 attr->size = cpu_to_le32(asize);
1559 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1567 attr = Add2Ptr(attr, asize);
1568 attr->type = ATTR_END;
1570 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1571 rec->next_attr_id = cpu_to_le16(aid);
1573 /* Step 2: Add new name in index. */
1574 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1578 /* Unlock parent directory before ntfs_init_acl. */
1581 inode->i_generation = le16_to_cpu(rec->seq);
1583 dir->i_mtime = dir->i_ctime = inode->i_atime;
1585 if (S_ISDIR(mode)) {
1586 inode->i_op = &ntfs_dir_inode_operations;
1587 inode->i_fop = &ntfs_dir_operations;
1588 } else if (S_ISLNK(mode)) {
1589 inode->i_op = &ntfs_link_inode_operations;
1590 inode->i_fop = NULL;
1591 inode->i_mapping->a_ops = &ntfs_aops;
1592 inode->i_size = size;
1593 inode_nohighmem(inode);
1594 } else if (S_ISREG(mode)) {
1595 inode->i_op = &ntfs_file_inode_operations;
1596 inode->i_fop = &ntfs_file_operations;
1597 inode->i_mapping->a_ops =
1598 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1599 init_rwsem(&ni->file.run_lock);
1601 inode->i_op = &ntfs_special_inode_operations;
1602 init_special_inode(inode, mode, dev);
1605 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1606 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1607 err = ntfs_init_acl(mnt_userns, inode, dir);
1613 inode->i_flags |= S_NOSEC;
1616 /* Write non resident data. */
1618 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1624 * Call 'd_instantiate' after inode->i_op is set
1625 * but before finish_open.
1627 d_instantiate(dentry, inode);
1629 ntfs_save_wsl_perm(inode);
1630 mark_inode_dirty(dir);
1631 mark_inode_dirty(inode);
1638 /* Undo 'indx_insert_entry'. */
1639 ni_lock_dir(dir_ni);
1640 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1641 le16_to_cpu(new_de->key_size), sbi);
1642 /* ni_unlock(dir_ni); will be called later. */
1645 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1649 run_deallocate(sbi, &ni->file.run, false);
1652 clear_rec_inuse(rec);
1654 ni->mi.dirty = false;
1655 discard_new_inode(inode);
1657 ntfs_mark_rec_free(sbi, ino, false);
1666 return ERR_PTR(err);
1669 unlock_new_inode(inode);
1674 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1677 struct ntfs_inode *ni = ntfs_i(inode);
1678 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1681 /* Allocate PATH_MAX bytes. */
1686 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1687 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1689 /* Construct 'de'. */
1690 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1694 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1703 * inode_operations::unlink
1704 * inode_operations::rmdir
1706 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1709 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1710 struct inode *inode = d_inode(dentry);
1711 struct ntfs_inode *ni = ntfs_i(inode);
1712 struct ntfs_inode *dir_ni = ntfs_i(dir);
1713 struct NTFS_DE *de, *de2 = NULL;
1716 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1719 /* Allocate PATH_MAX bytes. */
1726 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1731 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1736 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1740 dir->i_mtime = dir->i_ctime = current_time(dir);
1741 mark_inode_dirty(dir);
1742 inode->i_ctime = dir->i_ctime;
1744 mark_inode_dirty(inode);
1745 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1746 _ntfs_bad_inode(inode);
1748 if (ni_is_dirty(dir))
1749 mark_inode_dirty(dir);
1750 if (ni_is_dirty(inode))
1751 mark_inode_dirty(inode);
1760 void ntfs_evict_inode(struct inode *inode)
1762 truncate_inode_pages_final(&inode->i_data);
1765 _ni_write_inode(inode, inode_needs_sync(inode));
1767 invalidate_inode_buffers(inode);
1770 ni_clear(ntfs_i(inode));
1773 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1776 int i, err = -EINVAL;
1777 struct ntfs_inode *ni = ntfs_i(inode);
1778 struct super_block *sb = inode->i_sb;
1779 struct ntfs_sb_info *sbi = sb->s_fs_info;
1782 void *to_free = NULL;
1783 struct REPARSE_DATA_BUFFER *rp;
1784 const __le16 *uname;
1785 struct ATTRIB *attr;
1787 /* Reparse data present. Try to parse it. */
1788 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1789 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1793 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1797 if (!attr->non_res) {
1798 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1801 size = le32_to_cpu(attr->res.data_size);
1803 size = le64_to_cpu(attr->nres.data_size);
1807 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1811 rp = kmalloc(size, GFP_NOFS);
1817 /* Read into temporal buffer. */
1818 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1823 /* Microsoft Tag. */
1824 switch (rp->ReparseTag) {
1825 case IO_REPARSE_TAG_MOUNT_POINT:
1826 /* Mount points and junctions. */
1827 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1828 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1829 MountPointReparseBuffer.PathBuffer))
1832 offsetof(struct REPARSE_DATA_BUFFER,
1833 MountPointReparseBuffer.PathBuffer) +
1834 le16_to_cpu(rp->MountPointReparseBuffer
1836 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1839 case IO_REPARSE_TAG_SYMLINK:
1840 /* FolderSymbolicLink */
1841 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1842 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1843 SymbolicLinkReparseBuffer.PathBuffer))
1846 rp, offsetof(struct REPARSE_DATA_BUFFER,
1847 SymbolicLinkReparseBuffer.PathBuffer) +
1848 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1851 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1854 case IO_REPARSE_TAG_CLOUD:
1855 case IO_REPARSE_TAG_CLOUD_1:
1856 case IO_REPARSE_TAG_CLOUD_2:
1857 case IO_REPARSE_TAG_CLOUD_3:
1858 case IO_REPARSE_TAG_CLOUD_4:
1859 case IO_REPARSE_TAG_CLOUD_5:
1860 case IO_REPARSE_TAG_CLOUD_6:
1861 case IO_REPARSE_TAG_CLOUD_7:
1862 case IO_REPARSE_TAG_CLOUD_8:
1863 case IO_REPARSE_TAG_CLOUD_9:
1864 case IO_REPARSE_TAG_CLOUD_A:
1865 case IO_REPARSE_TAG_CLOUD_B:
1866 case IO_REPARSE_TAG_CLOUD_C:
1867 case IO_REPARSE_TAG_CLOUD_D:
1868 case IO_REPARSE_TAG_CLOUD_E:
1869 case IO_REPARSE_TAG_CLOUD_F:
1870 err = sizeof("OneDrive") - 1;
1873 memcpy(buffer, "OneDrive", err);
1877 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1878 /* Unknown Microsoft Tag. */
1881 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1882 size <= sizeof(struct REPARSE_POINT)) {
1887 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1888 ulen = le16_to_cpu(rp->ReparseDataLength) -
1889 sizeof(struct REPARSE_POINT);
1892 /* Convert nlen from bytes to UNICODE chars. */
1895 /* Check that name is available. */
1896 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1899 /* If name is already zero terminated then truncate it now. */
1900 if (!uname[ulen - 1])
1903 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1908 /* Translate Windows '\' into Linux '/'. */
1909 for (i = 0; i < err; i++) {
1910 if (buffer[i] == '\\')
1914 /* Always set last zero. */
1921 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1922 struct delayed_call *done)
1928 return ERR_PTR(-ECHILD);
1930 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1932 return ERR_PTR(-ENOMEM);
1934 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1937 return ERR_PTR(err);
1940 set_delayed_call(done, kfree_link, ret);
1946 const struct inode_operations ntfs_link_inode_operations = {
1947 .get_link = ntfs_get_link,
1948 .setattr = ntfs3_setattr,
1949 .listxattr = ntfs_listxattr,
1950 .permission = ntfs_permission,
1953 const struct address_space_operations ntfs_aops = {
1954 .read_folio = ntfs_read_folio,
1955 .readahead = ntfs_readahead,
1956 .writepage = ntfs_writepage,
1957 .writepages = ntfs_writepages,
1958 .write_begin = ntfs_write_begin,
1959 .write_end = ntfs_write_end,
1960 .direct_IO = ntfs_direct_IO,
1962 .dirty_folio = block_dirty_folio,
1963 .invalidate_folio = block_invalidate_folio,
1966 const struct address_space_operations ntfs_aops_cmpr = {
1967 .read_folio = ntfs_read_folio,
1968 .readahead = ntfs_readahead,