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 switch (attr->type) {
135 asize < sizeof(struct ATTR_STD_INFO) + roff ||
136 rsize < sizeof(struct ATTR_STD_INFO))
142 std5 = Add2Ptr(attr, roff);
145 nt2kernel(std5->cr_time, &ni->i_crtime);
147 nt2kernel(std5->a_time, &inode->i_atime);
148 nt2kernel(std5->c_time, &inode->i_ctime);
149 nt2kernel(std5->m_time, &inode->i_mtime);
151 ni->std_fa = std5->fa;
153 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
154 rsize >= sizeof(struct ATTR_STD_INFO5))
155 ni->std_security_id = std5->security_id;
159 if (attr->name_len || le || ino == MFT_REC_LOG)
162 err = ntfs_load_attr_list(ni, attr);
171 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
172 rsize < SIZEOF_ATTRIBUTE_FILENAME)
175 fname = Add2Ptr(attr, roff);
176 if (fname->type == FILE_NAME_DOS)
180 if (name && name->len == fname->name_len &&
181 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
189 /* Ignore data attribute in dir record. */
193 if (ino == MFT_REC_BADCLUST && !attr->non_res)
196 if (attr->name_len &&
197 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
198 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
199 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
200 (ino != MFT_REC_SECURE || !attr->non_res ||
201 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
202 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
203 /* File contains stream attribute. Ignore it. */
207 if (is_attr_sparsed(attr))
208 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
210 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
212 if (is_attr_compressed(attr))
213 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
215 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
217 if (is_attr_encrypted(attr))
218 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
220 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
222 if (!attr->non_res) {
223 ni->i_valid = inode->i_size = rsize;
224 inode_set_bytes(inode, rsize);
227 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
229 if (!attr->non_res) {
230 ni->ni_flags |= NI_FLAG_RESIDENT;
234 inode_set_bytes(inode, attr_ondisk_size(attr));
236 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
237 inode->i_size = le64_to_cpu(attr->nres.data_size);
238 if (!attr->nres.alloc_size)
241 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
249 root = Add2Ptr(attr, roff);
252 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
253 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
256 if (root->type != ATTR_NAME ||
257 root->rule != NTFS_COLLATION_TYPE_FILENAME)
263 ni->ni_flags |= NI_FLAG_DIR;
265 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
270 ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
275 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
276 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
279 inode->i_size = le64_to_cpu(attr->nres.data_size);
280 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
281 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
283 run = &ni->dir.alloc_run;
287 if (ino == MFT_REC_MFT) {
290 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
291 /* 0x20000000 = 2^32 / 8 */
292 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
295 run = &sbi->mft.bitmap.run;
297 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
298 !memcmp(attr_name(attr), I30_NAME,
301 run = &ni->dir.bitmap_run;
310 rp_fa = ni_parse_reparse(ni, attr, &rp);
315 * Assume one unicode symbol == one utf8.
317 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
321 ni->i_valid = inode->i_size;
323 /* Clear directory bit. */
324 if (ni->ni_flags & NI_FLAG_DIR) {
325 indx_clear(&ni->dir);
326 memset(&ni->dir, 0, sizeof(ni->dir));
327 ni->ni_flags &= ~NI_FLAG_DIR;
329 run_close(&ni->file.run);
331 mode = S_IFLNK | 0777;
335 goto attr_unpack_run; // Double break.
339 case REPARSE_COMPRESSED:
342 case REPARSE_DEDUPLICATED:
348 if (!attr->name_len &&
349 resident_data_ex(attr, sizeof(struct EA_INFO))) {
350 ni->ni_flags |= NI_FLAG_EA;
352 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
354 inode->i_mode = mode;
355 ntfs_get_wsl_perm(inode);
356 mode = inode->i_mode;
365 roff = le16_to_cpu(attr->nres.run_off);
367 t64 = le64_to_cpu(attr->nres.svcn);
368 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
369 t64, Add2Ptr(attr, roff), asize - roff);
380 if (!is_match && name) {
381 /* Reuse rec as buffer for ascii name. */
386 if (std5->fa & FILE_ATTRIBUTE_READONLY)
394 if (names != le16_to_cpu(rec->hard_links)) {
395 /* Correct minor error on the fly. Do not mark inode as dirty. */
396 rec->hard_links = cpu_to_le16(names);
400 set_nlink(inode, names);
403 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
406 * Dot and dot-dot should be included in count but was not
407 * included in enumeration.
408 * Usually a hard links to directories are disabled.
410 inode->i_op = &ntfs_dir_inode_operations;
411 inode->i_fop = &ntfs_dir_operations;
413 } else if (S_ISLNK(mode)) {
414 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
415 inode->i_op = &ntfs_link_inode_operations;
417 inode_nohighmem(inode);
418 } else if (S_ISREG(mode)) {
419 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
420 inode->i_op = &ntfs_file_inode_operations;
421 inode->i_fop = &ntfs_file_operations;
422 inode->i_mapping->a_ops =
423 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
424 if (ino != MFT_REC_MFT)
425 init_rwsem(&ni->file.run_lock);
426 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
428 inode->i_op = &ntfs_special_inode_operations;
429 init_special_inode(inode, mode, inode->i_rdev);
430 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
431 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
432 /* Records in $Extend are not a files or general directories. */
438 if ((sbi->options->sys_immutable &&
439 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
440 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
441 inode->i_flags |= S_IMMUTABLE;
443 inode->i_flags &= ~S_IMMUTABLE;
446 inode->i_mode = mode;
447 if (!(ni->ni_flags & NI_FLAG_EA)) {
448 /* If no xattr then no security (stored in xattr). */
449 inode->i_flags |= S_NOSEC;
453 if (ino == MFT_REC_MFT && !sb->s_root)
456 unlock_new_inode(inode);
461 if (ino == MFT_REC_MFT && !sb->s_root)
471 * Return: 1 if match.
473 static int ntfs_test_inode(struct inode *inode, void *data)
475 struct MFT_REF *ref = data;
477 return ino_get(ref) == inode->i_ino;
480 static int ntfs_set_inode(struct inode *inode, void *data)
482 const struct MFT_REF *ref = data;
484 inode->i_ino = ino_get(ref);
488 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
489 const struct cpu_str *name)
493 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
495 if (unlikely(!inode))
496 return ERR_PTR(-ENOMEM);
498 /* If this is a freshly allocated inode, need to read it now. */
499 if (inode->i_state & I_NEW)
500 inode = ntfs_read_mft(inode, name, ref);
501 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
502 /* Inode overlaps? */
503 make_bad_inode(inode);
510 GET_BLOCK_GENERAL = 0,
511 GET_BLOCK_WRITE_BEGIN = 1,
512 GET_BLOCK_DIRECT_IO_R = 2,
513 GET_BLOCK_DIRECT_IO_W = 3,
517 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
518 struct buffer_head *bh, int create,
519 enum get_block_ctx ctx)
521 struct super_block *sb = inode->i_sb;
522 struct ntfs_sb_info *sbi = sb->s_fs_info;
523 struct ntfs_inode *ni = ntfs_i(inode);
524 struct page *page = bh->b_page;
525 u8 cluster_bits = sbi->cluster_bits;
526 u32 block_size = sb->s_blocksize;
527 u64 bytes, lbo, valid;
533 /* Clear previous state. */
534 clear_buffer_new(bh);
535 clear_buffer_uptodate(bh);
537 /* Direct write uses 'create=0'. */
538 if (!create && vbo >= ni->i_valid) {
543 if (vbo >= inode->i_size) {
548 if (is_resident(ni)) {
550 err = attr_data_read_resident(ni, page);
554 set_buffer_uptodate(bh);
555 bh->b_size = block_size;
559 vcn = vbo >> cluster_bits;
560 off = vbo & sbi->cluster_mask;
563 err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
570 bytes = ((u64)len << cluster_bits) - off;
572 if (lcn == SPARSE_LCN) {
574 if (bh->b_size > bytes)
583 if ((len << cluster_bits) > block_size)
584 ntfs_sparse_cluster(inode, page, vcn, len);
587 lbo = ((u64)lcn << cluster_bits) + off;
589 set_buffer_mapped(bh);
590 bh->b_bdev = sb->s_bdev;
591 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
595 if (ctx == GET_BLOCK_DIRECT_IO_W) {
596 /* ntfs_direct_IO will update ni->i_valid. */
601 if (bytes > bh->b_size)
607 if (vbo + bytes > valid) {
608 ni->i_valid = vbo + bytes;
609 mark_inode_dirty(inode);
611 } else if (vbo >= valid) {
612 /* Read out of valid data. */
613 /* Should never be here 'cause already checked. */
614 clear_buffer_mapped(bh);
615 } else if (vbo + bytes <= valid) {
617 } else if (vbo + block_size <= valid) {
618 /* Normal short read. */
622 * Read across valid size: vbo < valid && valid < vbo + block_size
627 u32 voff = valid - vbo;
629 bh->b_size = block_size;
630 off = vbo & (PAGE_SIZE - 1);
631 set_bh_page(bh, page, off);
632 ll_rw_block(REQ_OP_READ, 1, &bh);
634 if (!buffer_uptodate(bh)) {
638 zero_user_segment(page, off + voff, off + block_size);
642 if (bh->b_size > bytes)
646 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
647 static_assert(sizeof(size_t) < sizeof(loff_t));
648 if (bytes > 0x40000000u)
649 bh->b_size = 0x40000000u;
659 int ntfs_get_block(struct inode *inode, sector_t vbn,
660 struct buffer_head *bh_result, int create)
662 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
663 bh_result, create, GET_BLOCK_GENERAL);
666 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
667 struct buffer_head *bh_result, int create)
669 return ntfs_get_block_vbo(inode,
670 (u64)vsn << inode->i_sb->s_blocksize_bits,
671 bh_result, create, GET_BLOCK_BMAP);
674 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
676 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
679 static int ntfs_read_folio(struct file *file, struct folio *folio)
681 struct page *page = &folio->page;
683 struct address_space *mapping = page->mapping;
684 struct inode *inode = mapping->host;
685 struct ntfs_inode *ni = ntfs_i(inode);
687 if (is_resident(ni)) {
689 err = attr_data_read_resident(ni, page);
691 if (err != E_NTFS_NONRESIDENT) {
697 if (is_compressed(ni)) {
699 err = ni_readpage_cmpr(ni, page);
704 /* Normal + sparse files. */
705 return mpage_read_folio(folio, ntfs_get_block);
708 static void ntfs_readahead(struct readahead_control *rac)
710 struct address_space *mapping = rac->mapping;
711 struct inode *inode = mapping->host;
712 struct ntfs_inode *ni = ntfs_i(inode);
716 if (is_resident(ni)) {
717 /* No readahead for resident. */
721 if (is_compressed(ni)) {
722 /* No readahead for compressed. */
727 pos = readahead_pos(rac);
729 if (valid < i_size_read(inode) && pos <= valid &&
730 valid < pos + readahead_length(rac)) {
731 /* Range cross 'valid'. Read it page by page. */
735 mpage_readahead(rac, ntfs_get_block);
738 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
739 struct buffer_head *bh_result, int create)
741 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
742 bh_result, create, GET_BLOCK_DIRECT_IO_R);
745 static int ntfs_get_block_direct_IO_W(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_W);
752 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
754 struct file *file = iocb->ki_filp;
755 struct address_space *mapping = file->f_mapping;
756 struct inode *inode = mapping->host;
757 struct ntfs_inode *ni = ntfs_i(inode);
758 loff_t vbo = iocb->ki_pos;
760 int wr = iov_iter_rw(iter) & WRITE;
761 size_t iter_count = iov_iter_count(iter);
765 if (is_resident(ni)) {
766 /* Switch to buffered write. */
771 ret = blockdev_direct_IO(iocb, inode, iter,
772 wr ? ntfs_get_block_direct_IO_W
773 : ntfs_get_block_direct_IO_R);
777 else if (wr && ret == -EIOCBQUEUED)
778 end = vbo + iter_count;
784 if (end > valid && !S_ISBLK(inode->i_mode)) {
786 mark_inode_dirty(inode);
788 } else if (vbo < valid && valid < end) {
790 iov_iter_revert(iter, end - valid);
791 iov_iter_zero(end - valid, iter);
798 int ntfs_set_size(struct inode *inode, u64 new_size)
800 struct super_block *sb = inode->i_sb;
801 struct ntfs_sb_info *sbi = sb->s_fs_info;
802 struct ntfs_inode *ni = ntfs_i(inode);
805 /* Check for maximum file size. */
806 if (is_sparsed(ni) || is_compressed(ni)) {
807 if (new_size > sbi->maxbytes_sparse) {
811 } else if (new_size > sbi->maxbytes) {
817 down_write(&ni->file.run_lock);
819 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
820 &ni->i_valid, true, NULL);
822 up_write(&ni->file.run_lock);
825 mark_inode_dirty(inode);
831 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
833 struct address_space *mapping = page->mapping;
834 struct inode *inode = mapping->host;
835 struct ntfs_inode *ni = ntfs_i(inode);
838 if (is_resident(ni)) {
840 err = attr_data_write_resident(ni, page);
842 if (err != E_NTFS_NONRESIDENT) {
848 return block_write_full_page(page, ntfs_get_block, wbc);
851 static int ntfs_writepages(struct address_space *mapping,
852 struct writeback_control *wbc)
854 /* Redirect call to 'ntfs_writepage' for resident files. */
855 if (is_resident(ntfs_i(mapping->host)))
856 return generic_writepages(mapping, wbc);
857 return mpage_writepages(mapping, wbc, ntfs_get_block);
860 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
861 struct buffer_head *bh_result, int create)
863 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
864 bh_result, create, GET_BLOCK_WRITE_BEGIN);
867 int ntfs_write_begin(struct file *file, struct address_space *mapping,
868 loff_t pos, u32 len, struct page **pagep, void **fsdata)
871 struct inode *inode = mapping->host;
872 struct ntfs_inode *ni = ntfs_i(inode);
875 if (is_resident(ni)) {
876 struct page *page = grab_cache_page_write_begin(
877 mapping, pos >> PAGE_SHIFT);
885 err = attr_data_read_resident(ni, page);
895 if (err != E_NTFS_NONRESIDENT)
899 err = block_write_begin(mapping, pos, len, pagep,
900 ntfs_get_block_write_begin);
907 * ntfs_write_end - Address_space_operations::write_end.
909 int ntfs_write_end(struct file *file, struct address_space *mapping,
910 loff_t pos, u32 len, u32 copied, struct page *page,
913 struct inode *inode = mapping->host;
914 struct ntfs_inode *ni = ntfs_i(inode);
915 u64 valid = ni->i_valid;
919 if (is_resident(ni)) {
921 err = attr_data_write_resident(ni, page);
925 /* Clear any buffers in page. */
926 if (page_has_buffers(page)) {
927 struct buffer_head *head, *bh;
929 bh = head = page_buffers(page);
931 clear_buffer_dirty(bh);
932 clear_buffer_mapped(bh);
933 set_buffer_uptodate(bh);
934 } while (head != (bh = bh->b_this_page));
936 SetPageUptodate(page);
942 err = generic_write_end(file, mapping, pos, len, copied, page,
947 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
948 inode->i_ctime = inode->i_mtime = current_time(inode);
949 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
953 if (valid != ni->i_valid) {
954 /* ni->i_valid is changed in ntfs_get_block_vbo. */
959 mark_inode_dirty(inode);
965 int reset_log_file(struct inode *inode)
969 u32 log_size = inode->i_size;
970 struct address_space *mapping = inode->i_mapping;
977 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
979 err = block_write_begin(mapping, pos, len, &page,
980 ntfs_get_block_write_begin);
984 kaddr = kmap_atomic(page);
985 memset(kaddr, -1, len);
986 kunmap_atomic(kaddr);
987 flush_dcache_page(page);
989 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
996 balance_dirty_pages_ratelimited(mapping);
999 mark_inode_dirty_sync(inode);
1004 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1006 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1009 int ntfs_sync_inode(struct inode *inode)
1011 return _ni_write_inode(inode, 1);
1015 * writeback_inode - Helper function for ntfs_flush_inodes().
1017 * This writes both the inode and the file data blocks, waiting
1018 * for in flight data blocks before the start of the call. It
1019 * does not wait for any io started during the call.
1021 static int writeback_inode(struct inode *inode)
1023 int ret = sync_inode_metadata(inode, 0);
1026 ret = filemap_fdatawrite(inode->i_mapping);
1033 * Write data and metadata corresponding to i1 and i2. The io is
1034 * started but we do not wait for any of it to finish.
1036 * filemap_flush() is used for the block device, so if there is a dirty
1037 * page for a block already in flight, we will not wait and start the
1040 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1046 ret = writeback_inode(i1);
1048 ret = writeback_inode(i2);
1050 ret = sync_blockdev_nowait(sb->s_bdev);
1054 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1058 /* Write non resident data. */
1059 for (idx = 0; bytes; idx++) {
1060 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1061 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1064 return PTR_ERR(page);
1067 WARN_ON(!PageUptodate(page));
1068 ClearPageUptodate(page);
1070 memcpy(page_address(page), data, op);
1072 flush_dcache_page(page);
1073 SetPageUptodate(page);
1076 ntfs_unmap_page(page);
1079 data = Add2Ptr(data, PAGE_SIZE);
1085 * ntfs_reparse_bytes
1087 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1088 * for unicode string of @uni_len length.
1090 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1092 /* Header + unicode string + decorated unicode string. */
1093 return sizeof(short) * (2 * uni_len + 4) +
1094 offsetof(struct REPARSE_DATA_BUFFER,
1095 SymbolicLinkReparseBuffer.PathBuffer);
1098 static struct REPARSE_DATA_BUFFER *
1099 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1100 u32 size, u16 *nsize)
1103 struct REPARSE_DATA_BUFFER *rp;
1105 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1107 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1109 return ERR_PTR(-ENOMEM);
1111 rs = &rp->SymbolicLinkReparseBuffer;
1112 rp_name = rs->PathBuffer;
1114 /* Convert link name to UTF-16. */
1115 err = ntfs_nls_to_utf16(sbi, symname, size,
1116 (struct cpu_str *)(rp_name - 1), 2 * size,
1117 UTF16_LITTLE_ENDIAN);
1121 /* err = the length of unicode name of symlink. */
1122 *nsize = ntfs_reparse_bytes(err);
1124 if (*nsize > sbi->reparse.max_size) {
1129 /* Translate Linux '/' into Windows '\'. */
1130 for (i = 0; i < err; i++) {
1131 if (rp_name[i] == cpu_to_le16('/'))
1132 rp_name[i] = cpu_to_le16('\\');
1135 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1136 rp->ReparseDataLength =
1137 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1138 SymbolicLinkReparseBuffer));
1140 /* PrintName + SubstituteName. */
1141 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1142 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1143 rs->PrintNameLength = rs->SubstituteNameOffset;
1146 * TODO: Use relative path if possible to allow Windows to
1148 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1152 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1154 /* Decorate SubstituteName. */
1156 rp_name[0] = cpu_to_le16('\\');
1157 rp_name[1] = cpu_to_le16('?');
1158 rp_name[2] = cpu_to_le16('?');
1159 rp_name[3] = cpu_to_le16('\\');
1164 return ERR_PTR(err);
1167 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1168 struct inode *dir, struct dentry *dentry,
1169 const struct cpu_str *uni, umode_t mode,
1170 dev_t dev, const char *symname, u32 size,
1171 struct ntfs_fnd *fnd)
1174 struct super_block *sb = dir->i_sb;
1175 struct ntfs_sb_info *sbi = sb->s_fs_info;
1176 const struct qstr *name = &dentry->d_name;
1178 struct ntfs_inode *dir_ni = ntfs_i(dir);
1179 struct ntfs_inode *ni = NULL;
1180 struct inode *inode = NULL;
1181 struct ATTRIB *attr;
1182 struct ATTR_STD_INFO5 *std5;
1183 struct ATTR_FILE_NAME *fname;
1184 struct MFT_REC *rec;
1185 u32 asize, dsize, sd_size;
1186 enum FILE_ATTRIBUTE fa;
1187 __le32 security_id = SECURITY_ID_INVALID;
1190 u16 t16, nsize = 0, aid = 0;
1191 struct INDEX_ROOT *root, *dir_root;
1192 struct NTFS_DE *e, *new_de = NULL;
1193 struct REPARSE_DATA_BUFFER *rp = NULL;
1194 bool rp_inserted = false;
1196 ni_lock_dir(dir_ni);
1198 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1204 if (S_ISDIR(mode)) {
1205 /* Use parent's directory attributes. */
1206 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1207 FILE_ATTRIBUTE_ARCHIVE;
1209 * By default child directory inherits parent attributes.
1210 * Root directory is hidden + system.
1211 * Make an exception for children in root.
1213 if (dir->i_ino == MFT_REC_ROOT)
1214 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1215 } else if (S_ISLNK(mode)) {
1216 /* It is good idea that link should be the same type (file/dir) as target */
1217 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1220 * Linux: there are dir/file/symlink and so on.
1221 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1222 * It is good idea to create:
1223 * dir + reparse if 'symname' points to directory
1225 * file + reparse if 'symname' points to file
1226 * Unfortunately kern_path hangs if symname contains 'dir'.
1232 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1233 * struct inode *target = d_inode(path.dentry);
1235 * if (S_ISDIR(target->i_mode))
1236 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1237 * // if ( target->i_sb == sb ){
1238 * // use relative path?
1243 } else if (S_ISREG(mode)) {
1244 if (sbi->options->sparse) {
1245 /* Sparsed regular file, cause option 'sparse'. */
1246 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1247 FILE_ATTRIBUTE_ARCHIVE;
1248 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1249 /* Compressed regular file, if parent is compressed. */
1250 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1252 /* Regular file, default attributes. */
1253 fa = FILE_ATTRIBUTE_ARCHIVE;
1256 fa = FILE_ATTRIBUTE_ARCHIVE;
1260 fa |= FILE_ATTRIBUTE_READONLY;
1262 /* Allocate PATH_MAX bytes. */
1263 new_de = __getname();
1269 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1270 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1272 /* Step 1: allocate and fill new mft record. */
1273 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1277 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1283 inode = &ni->vfs_inode;
1284 inode_init_owner(mnt_userns, inode, dir, mode);
1285 mode = inode->i_mode;
1287 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1288 current_time(inode);
1291 rec->hard_links = cpu_to_le16(1);
1292 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1294 /* Get default security id. */
1295 sd = s_default_security;
1296 sd_size = sizeof(s_default_security);
1298 if (is_ntfs3(sbi)) {
1299 security_id = dir_ni->std_security_id;
1300 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1301 security_id = sbi->security.def_security_id;
1303 if (security_id == SECURITY_ID_INVALID &&
1304 !ntfs_insert_security(sbi, sd, sd_size,
1305 &security_id, NULL))
1306 sbi->security.def_security_id = security_id;
1310 /* Insert standard info. */
1311 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1313 if (security_id == SECURITY_ID_INVALID) {
1314 dsize = sizeof(struct ATTR_STD_INFO);
1316 dsize = sizeof(struct ATTR_STD_INFO5);
1317 std5->security_id = security_id;
1318 ni->std_security_id = security_id;
1320 asize = SIZEOF_RESIDENT + dsize;
1322 attr->type = ATTR_STD;
1323 attr->size = cpu_to_le32(asize);
1324 attr->id = cpu_to_le16(aid++);
1325 attr->res.data_off = SIZEOF_RESIDENT_LE;
1326 attr->res.data_size = cpu_to_le32(dsize);
1328 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1329 kernel2nt(&inode->i_atime);
1334 attr = Add2Ptr(attr, asize);
1336 /* Insert file name. */
1337 err = fill_name_de(sbi, new_de, name, uni);
1341 mi_get_ref(&ni->mi, &new_de->ref);
1343 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1344 mi_get_ref(&dir_ni->mi, &fname->home);
1345 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1346 fname->dup.a_time = std5->cr_time;
1347 fname->dup.alloc_size = fname->dup.data_size = 0;
1348 fname->dup.fa = std5->fa;
1349 fname->dup.ea_size = fname->dup.reparse = 0;
1351 dsize = le16_to_cpu(new_de->key_size);
1352 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1354 attr->type = ATTR_NAME;
1355 attr->size = cpu_to_le32(asize);
1356 attr->res.data_off = SIZEOF_RESIDENT_LE;
1357 attr->res.flags = RESIDENT_FLAG_INDEXED;
1358 attr->id = cpu_to_le16(aid++);
1359 attr->res.data_size = cpu_to_le32(dsize);
1360 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1362 attr = Add2Ptr(attr, asize);
1364 if (security_id == SECURITY_ID_INVALID) {
1365 /* Insert security attribute. */
1366 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1368 attr->type = ATTR_SECURE;
1369 attr->size = cpu_to_le32(asize);
1370 attr->id = cpu_to_le16(aid++);
1371 attr->res.data_off = SIZEOF_RESIDENT_LE;
1372 attr->res.data_size = cpu_to_le32(sd_size);
1373 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1375 attr = Add2Ptr(attr, asize);
1378 attr->id = cpu_to_le16(aid++);
1379 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1381 * Regular directory or symlink to directory.
1382 * Create root attribute.
1384 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1385 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1387 attr->type = ATTR_ROOT;
1388 attr->size = cpu_to_le32(asize);
1390 attr->name_len = ARRAY_SIZE(I30_NAME);
1391 attr->name_off = SIZEOF_RESIDENT_LE;
1392 attr->res.data_off =
1393 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1394 attr->res.data_size = cpu_to_le32(dsize);
1395 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1398 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1399 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1401 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1402 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1403 sizeof(struct NTFS_DE));
1404 root->ihdr.total = root->ihdr.used;
1406 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1407 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1408 e->flags = NTFS_IE_LAST;
1409 } else if (S_ISLNK(mode)) {
1412 * Create empty resident data attribute.
1414 asize = SIZEOF_RESIDENT;
1416 /* Insert empty ATTR_DATA */
1417 attr->type = ATTR_DATA;
1418 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1419 attr->name_off = SIZEOF_RESIDENT_LE;
1420 attr->res.data_off = SIZEOF_RESIDENT_LE;
1421 } else if (S_ISREG(mode)) {
1423 * Regular file. Create empty non resident data attribute.
1425 attr->type = ATTR_DATA;
1427 attr->nres.evcn = cpu_to_le64(-1ll);
1428 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1429 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1430 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1431 attr->flags = ATTR_FLAG_SPARSED;
1432 asize = SIZEOF_NONRESIDENT_EX + 8;
1433 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1434 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1435 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1436 attr->flags = ATTR_FLAG_COMPRESSED;
1437 attr->nres.c_unit = COMPRESSION_UNIT;
1438 asize = SIZEOF_NONRESIDENT_EX + 8;
1440 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1441 attr->name_off = SIZEOF_NONRESIDENT_LE;
1442 asize = SIZEOF_NONRESIDENT + 8;
1444 attr->nres.run_off = attr->name_off;
1447 * Node. Create empty resident data attribute.
1449 attr->type = ATTR_DATA;
1450 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1451 attr->name_off = SIZEOF_RESIDENT_LE;
1452 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1453 attr->flags = ATTR_FLAG_SPARSED;
1454 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1455 attr->flags = ATTR_FLAG_COMPRESSED;
1456 attr->res.data_off = SIZEOF_RESIDENT_LE;
1457 asize = SIZEOF_RESIDENT;
1458 ni->ni_flags |= NI_FLAG_RESIDENT;
1461 if (S_ISDIR(mode)) {
1462 ni->ni_flags |= NI_FLAG_DIR;
1463 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1466 } else if (S_ISLNK(mode)) {
1467 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1476 * Insert ATTR_REPARSE.
1478 attr = Add2Ptr(attr, asize);
1479 attr->type = ATTR_REPARSE;
1480 attr->id = cpu_to_le16(aid++);
1482 /* Resident or non resident? */
1483 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1484 t16 = PtrOffset(rec, attr);
1487 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1488 * It is good idea to keep extened attributes resident.
1490 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1492 CLST clst = bytes_to_cluster(sbi, nsize);
1494 /* Bytes per runs. */
1495 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1498 attr->nres.evcn = cpu_to_le64(clst - 1);
1499 attr->name_off = SIZEOF_NONRESIDENT_LE;
1500 attr->nres.run_off = attr->name_off;
1501 attr->nres.data_size = cpu_to_le64(nsize);
1502 attr->nres.valid_size = attr->nres.data_size;
1503 attr->nres.alloc_size =
1504 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1506 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1507 clst, NULL, 0, &alen, 0,
1512 err = run_pack(&ni->file.run, 0, clst,
1513 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1523 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1525 attr->res.data_off = SIZEOF_RESIDENT_LE;
1526 attr->res.data_size = cpu_to_le32(nsize);
1527 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1530 /* Size of symlink equals the length of input string. */
1531 inode->i_size = size;
1533 attr->size = cpu_to_le32(asize);
1535 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1543 attr = Add2Ptr(attr, asize);
1544 attr->type = ATTR_END;
1546 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1547 rec->next_attr_id = cpu_to_le16(aid);
1549 /* Step 2: Add new name in index. */
1550 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1554 /* Unlock parent directory before ntfs_init_acl. */
1557 inode->i_generation = le16_to_cpu(rec->seq);
1559 dir->i_mtime = dir->i_ctime = inode->i_atime;
1561 if (S_ISDIR(mode)) {
1562 inode->i_op = &ntfs_dir_inode_operations;
1563 inode->i_fop = &ntfs_dir_operations;
1564 } else if (S_ISLNK(mode)) {
1565 inode->i_op = &ntfs_link_inode_operations;
1566 inode->i_fop = NULL;
1567 inode->i_mapping->a_ops = &ntfs_aops;
1568 inode->i_size = size;
1569 inode_nohighmem(inode);
1570 } else if (S_ISREG(mode)) {
1571 inode->i_op = &ntfs_file_inode_operations;
1572 inode->i_fop = &ntfs_file_operations;
1573 inode->i_mapping->a_ops =
1574 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1575 init_rwsem(&ni->file.run_lock);
1577 inode->i_op = &ntfs_special_inode_operations;
1578 init_special_inode(inode, mode, dev);
1581 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1582 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1583 err = ntfs_init_acl(mnt_userns, inode, dir);
1589 inode->i_flags |= S_NOSEC;
1592 /* Write non resident data. */
1594 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1600 * Call 'd_instantiate' after inode->i_op is set
1601 * but before finish_open.
1603 d_instantiate(dentry, inode);
1605 ntfs_save_wsl_perm(inode);
1606 mark_inode_dirty(dir);
1607 mark_inode_dirty(inode);
1614 /* Undo 'indx_insert_entry'. */
1615 ni_lock_dir(dir_ni);
1616 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1617 le16_to_cpu(new_de->key_size), sbi);
1618 /* ni_unlock(dir_ni); will be called later. */
1621 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1624 if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1627 run_deallocate(sbi, &ni->file.run, false);
1630 clear_rec_inuse(rec);
1632 ni->mi.dirty = false;
1633 discard_new_inode(inode);
1635 ntfs_mark_rec_free(sbi, ino);
1644 return ERR_PTR(err);
1647 unlock_new_inode(inode);
1652 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1655 struct ntfs_inode *ni = ntfs_i(inode);
1656 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1658 struct ATTR_FILE_NAME *de_name;
1660 /* Allocate PATH_MAX bytes. */
1665 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1666 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1668 /* Construct 'de'. */
1669 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1673 de_name = (struct ATTR_FILE_NAME *)(de + 1);
1674 /* Fill duplicate info. */
1675 de_name->dup.cr_time = de_name->dup.m_time = de_name->dup.c_time =
1676 de_name->dup.a_time = kernel2nt(&inode->i_ctime);
1677 de_name->dup.alloc_size = de_name->dup.data_size =
1678 cpu_to_le64(inode->i_size);
1679 de_name->dup.fa = ni->std_fa;
1680 de_name->dup.ea_size = de_name->dup.reparse = 0;
1682 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1691 * inode_operations::unlink
1692 * inode_operations::rmdir
1694 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1697 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1698 struct inode *inode = d_inode(dentry);
1699 struct ntfs_inode *ni = ntfs_i(inode);
1700 struct ntfs_inode *dir_ni = ntfs_i(dir);
1701 struct NTFS_DE *de, *de2 = NULL;
1704 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1707 /* Allocate PATH_MAX bytes. */
1714 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1719 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1724 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1728 dir->i_mtime = dir->i_ctime = current_time(dir);
1729 mark_inode_dirty(dir);
1730 inode->i_ctime = dir->i_ctime;
1732 mark_inode_dirty(inode);
1733 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1734 make_bad_inode(inode);
1735 ntfs_inode_err(inode, "failed to undo unlink");
1736 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1738 if (ni_is_dirty(dir))
1739 mark_inode_dirty(dir);
1740 if (ni_is_dirty(inode))
1741 mark_inode_dirty(inode);
1750 void ntfs_evict_inode(struct inode *inode)
1752 truncate_inode_pages_final(&inode->i_data);
1755 _ni_write_inode(inode, inode_needs_sync(inode));
1757 invalidate_inode_buffers(inode);
1760 ni_clear(ntfs_i(inode));
1763 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1766 int i, err = -EINVAL;
1767 struct ntfs_inode *ni = ntfs_i(inode);
1768 struct super_block *sb = inode->i_sb;
1769 struct ntfs_sb_info *sbi = sb->s_fs_info;
1772 void *to_free = NULL;
1773 struct REPARSE_DATA_BUFFER *rp;
1774 const __le16 *uname;
1775 struct ATTRIB *attr;
1777 /* Reparse data present. Try to parse it. */
1778 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1779 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1783 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1787 if (!attr->non_res) {
1788 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1791 size = le32_to_cpu(attr->res.data_size);
1793 size = le64_to_cpu(attr->nres.data_size);
1797 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1801 rp = kmalloc(size, GFP_NOFS);
1807 /* Read into temporal buffer. */
1808 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1813 /* Microsoft Tag. */
1814 switch (rp->ReparseTag) {
1815 case IO_REPARSE_TAG_MOUNT_POINT:
1816 /* Mount points and junctions. */
1817 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1818 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1819 MountPointReparseBuffer.PathBuffer))
1822 offsetof(struct REPARSE_DATA_BUFFER,
1823 MountPointReparseBuffer.PathBuffer) +
1824 le16_to_cpu(rp->MountPointReparseBuffer
1826 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1829 case IO_REPARSE_TAG_SYMLINK:
1830 /* FolderSymbolicLink */
1831 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1832 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1833 SymbolicLinkReparseBuffer.PathBuffer))
1836 rp, offsetof(struct REPARSE_DATA_BUFFER,
1837 SymbolicLinkReparseBuffer.PathBuffer) +
1838 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1841 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1844 case IO_REPARSE_TAG_CLOUD:
1845 case IO_REPARSE_TAG_CLOUD_1:
1846 case IO_REPARSE_TAG_CLOUD_2:
1847 case IO_REPARSE_TAG_CLOUD_3:
1848 case IO_REPARSE_TAG_CLOUD_4:
1849 case IO_REPARSE_TAG_CLOUD_5:
1850 case IO_REPARSE_TAG_CLOUD_6:
1851 case IO_REPARSE_TAG_CLOUD_7:
1852 case IO_REPARSE_TAG_CLOUD_8:
1853 case IO_REPARSE_TAG_CLOUD_9:
1854 case IO_REPARSE_TAG_CLOUD_A:
1855 case IO_REPARSE_TAG_CLOUD_B:
1856 case IO_REPARSE_TAG_CLOUD_C:
1857 case IO_REPARSE_TAG_CLOUD_D:
1858 case IO_REPARSE_TAG_CLOUD_E:
1859 case IO_REPARSE_TAG_CLOUD_F:
1860 err = sizeof("OneDrive") - 1;
1863 memcpy(buffer, "OneDrive", err);
1867 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1868 /* Unknown Microsoft Tag. */
1871 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1872 size <= sizeof(struct REPARSE_POINT)) {
1877 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1878 ulen = le16_to_cpu(rp->ReparseDataLength) -
1879 sizeof(struct REPARSE_POINT);
1882 /* Convert nlen from bytes to UNICODE chars. */
1885 /* Check that name is available. */
1886 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1889 /* If name is already zero terminated then truncate it now. */
1890 if (!uname[ulen - 1])
1893 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1898 /* Translate Windows '\' into Linux '/'. */
1899 for (i = 0; i < err; i++) {
1900 if (buffer[i] == '\\')
1904 /* Always set last zero. */
1911 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1912 struct delayed_call *done)
1918 return ERR_PTR(-ECHILD);
1920 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1922 return ERR_PTR(-ENOMEM);
1924 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1927 return ERR_PTR(err);
1930 set_delayed_call(done, kfree_link, ret);
1936 const struct inode_operations ntfs_link_inode_operations = {
1937 .get_link = ntfs_get_link,
1938 .setattr = ntfs3_setattr,
1939 .listxattr = ntfs_listxattr,
1940 .permission = ntfs_permission,
1941 .get_acl = ntfs_get_acl,
1942 .set_acl = ntfs_set_acl,
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,