1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/buffer_head.h>
10 #include <linux/mpage.h>
11 #include <linux/namei.h>
12 #include <linux/nls.h>
13 #include <linux/uio.h>
14 #include <linux/writeback.h>
21 * ntfs_read_mft - Read record and parses MFT.
23 static struct inode *ntfs_read_mft(struct inode *inode,
24 const struct cpu_str *name,
25 const struct MFT_REF *ref)
28 struct ntfs_inode *ni = ntfs_i(inode);
29 struct super_block *sb = inode->i_sb;
30 struct ntfs_sb_info *sbi = sb->s_fs_info;
32 struct ATTR_STD_INFO5 *std5 = NULL;
33 struct ATTR_LIST_ENTRY *le;
35 bool is_match = false;
38 unsigned long ino = inode->i_ino;
39 u32 rp_fa = 0, asize, t32;
40 u16 roff, rsize, names = 0;
41 const struct ATTR_FILE_NAME *fname = NULL;
42 const struct INDEX_ROOT *root;
43 struct REPARSE_DATA_BUFFER rp; // 0x18 bytes
46 struct runs_tree *run;
49 /* Setup 'uid' and 'gid' */
50 inode->i_uid = sbi->options->fs_uid;
51 inode->i_gid = sbi->options->fs_gid;
53 err = mi_init(&ni->mi, sbi, ino);
57 if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
58 t64 = sbi->mft.lbo >> sbi->cluster_bits;
59 t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
61 init_rwsem(&ni->file.run_lock);
63 if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
69 err = mi_read(&ni->mi, ino == MFT_REC_MFT);
76 if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
78 } else if (ref->seq != rec->seq) {
80 ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
81 le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
83 } else if (!is_rec_inuse(rec)) {
85 ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
89 if (le32_to_cpu(rec->total) != sbi->record_size) {
95 if (!is_rec_base(rec))
98 /* Record should contain $I30 root. */
99 is_dir = rec->flags & RECORD_FLAG_DIR;
101 inode->i_generation = le16_to_cpu(rec->seq);
103 /* Enumerate all struct Attributes MFT. */
108 * To reduce tab pressure use goto instead of
109 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
114 attr = ni_enum_attr_ex(ni, attr, &le, NULL);
119 /* This is non primary attribute segment. Ignore if not MFT. */
120 if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
124 asize = le32_to_cpu(attr->size);
125 goto attr_unpack_run;
128 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
129 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
130 asize = le32_to_cpu(attr->size);
132 if (le16_to_cpu(attr->name_off) + attr->name_len > asize)
136 t64 = le64_to_cpu(attr->nres.alloc_size);
137 if (le64_to_cpu(attr->nres.data_size) > t64 ||
138 le64_to_cpu(attr->nres.valid_size) > t64)
142 switch (attr->type) {
145 asize < sizeof(struct ATTR_STD_INFO) + roff ||
146 rsize < sizeof(struct ATTR_STD_INFO))
152 std5 = Add2Ptr(attr, roff);
155 nt2kernel(std5->cr_time, &ni->i_crtime);
157 nt2kernel(std5->a_time, &inode->i_atime);
158 nt2kernel(std5->c_time, &inode->i_ctime);
159 nt2kernel(std5->m_time, &inode->i_mtime);
161 ni->std_fa = std5->fa;
163 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
164 rsize >= sizeof(struct ATTR_STD_INFO5))
165 ni->std_security_id = std5->security_id;
169 if (attr->name_len || le || ino == MFT_REC_LOG)
172 err = ntfs_load_attr_list(ni, attr);
181 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
182 rsize < SIZEOF_ATTRIBUTE_FILENAME)
185 fname = Add2Ptr(attr, roff);
186 if (fname->type == FILE_NAME_DOS)
190 if (name && name->len == fname->name_len &&
191 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
199 /* Ignore data attribute in dir record. */
203 if (ino == MFT_REC_BADCLUST && !attr->non_res)
206 if (attr->name_len &&
207 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
208 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
209 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
210 (ino != MFT_REC_SECURE || !attr->non_res ||
211 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
212 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
213 /* File contains stream attribute. Ignore it. */
217 if (is_attr_sparsed(attr))
218 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
220 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
222 if (is_attr_compressed(attr))
223 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
225 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
227 if (is_attr_encrypted(attr))
228 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
230 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
232 if (!attr->non_res) {
233 ni->i_valid = inode->i_size = rsize;
234 inode_set_bytes(inode, rsize);
237 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
239 if (!attr->non_res) {
240 ni->ni_flags |= NI_FLAG_RESIDENT;
244 inode_set_bytes(inode, attr_ondisk_size(attr));
246 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
247 inode->i_size = le64_to_cpu(attr->nres.data_size);
248 if (!attr->nres.alloc_size)
251 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
259 root = Add2Ptr(attr, roff);
262 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
263 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
266 if (root->type != ATTR_NAME ||
267 root->rule != NTFS_COLLATION_TYPE_FILENAME)
273 ni->ni_flags |= NI_FLAG_DIR;
275 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
280 ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
285 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
286 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
289 inode->i_size = le64_to_cpu(attr->nres.data_size);
290 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
291 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
293 run = &ni->dir.alloc_run;
297 if (ino == MFT_REC_MFT) {
300 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
301 /* 0x20000000 = 2^32 / 8 */
302 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
305 run = &sbi->mft.bitmap.run;
307 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
308 !memcmp(attr_name(attr), I30_NAME,
311 run = &ni->dir.bitmap_run;
320 rp_fa = ni_parse_reparse(ni, attr, &rp);
325 * Assume one unicode symbol == one utf8.
327 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
331 ni->i_valid = inode->i_size;
333 /* Clear directory bit. */
334 if (ni->ni_flags & NI_FLAG_DIR) {
335 indx_clear(&ni->dir);
336 memset(&ni->dir, 0, sizeof(ni->dir));
337 ni->ni_flags &= ~NI_FLAG_DIR;
339 run_close(&ni->file.run);
341 mode = S_IFLNK | 0777;
345 goto attr_unpack_run; // Double break.
349 case REPARSE_COMPRESSED:
352 case REPARSE_DEDUPLICATED:
358 if (!attr->name_len &&
359 resident_data_ex(attr, sizeof(struct EA_INFO))) {
360 ni->ni_flags |= NI_FLAG_EA;
362 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
364 inode->i_mode = mode;
365 ntfs_get_wsl_perm(inode);
366 mode = inode->i_mode;
375 roff = le16_to_cpu(attr->nres.run_off);
382 t64 = le64_to_cpu(attr->nres.svcn);
384 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
385 t64, Add2Ptr(attr, roff), asize - roff);
396 if (!is_match && name) {
397 /* Reuse rec as buffer for ascii name. */
402 if (std5->fa & FILE_ATTRIBUTE_READONLY)
410 if (names != le16_to_cpu(rec->hard_links)) {
411 /* Correct minor error on the fly. Do not mark inode as dirty. */
412 rec->hard_links = cpu_to_le16(names);
416 set_nlink(inode, names);
419 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
422 * Dot and dot-dot should be included in count but was not
423 * included in enumeration.
424 * Usually a hard links to directories are disabled.
426 inode->i_op = &ntfs_dir_inode_operations;
427 inode->i_fop = &ntfs_dir_operations;
429 } else if (S_ISLNK(mode)) {
430 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
431 inode->i_op = &ntfs_link_inode_operations;
433 inode_nohighmem(inode);
434 } else if (S_ISREG(mode)) {
435 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
436 inode->i_op = &ntfs_file_inode_operations;
437 inode->i_fop = &ntfs_file_operations;
438 inode->i_mapping->a_ops =
439 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
440 if (ino != MFT_REC_MFT)
441 init_rwsem(&ni->file.run_lock);
442 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
444 inode->i_op = &ntfs_special_inode_operations;
445 init_special_inode(inode, mode, inode->i_rdev);
446 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
447 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
448 /* Records in $Extend are not a files or general directories. */
449 inode->i_op = &ntfs_file_inode_operations;
455 if ((sbi->options->sys_immutable &&
456 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
457 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
458 inode->i_flags |= S_IMMUTABLE;
460 inode->i_flags &= ~S_IMMUTABLE;
463 inode->i_mode = mode;
464 if (!(ni->ni_flags & NI_FLAG_EA)) {
465 /* If no xattr then no security (stored in xattr). */
466 inode->i_flags |= S_NOSEC;
470 if (ino == MFT_REC_MFT && !sb->s_root)
473 unlock_new_inode(inode);
478 if (ino == MFT_REC_MFT && !sb->s_root)
488 * Return: 1 if match.
490 static int ntfs_test_inode(struct inode *inode, void *data)
492 struct MFT_REF *ref = data;
494 return ino_get(ref) == inode->i_ino;
497 static int ntfs_set_inode(struct inode *inode, void *data)
499 const struct MFT_REF *ref = data;
501 inode->i_ino = ino_get(ref);
505 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
506 const struct cpu_str *name)
510 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
512 if (unlikely(!inode))
513 return ERR_PTR(-ENOMEM);
515 /* If this is a freshly allocated inode, need to read it now. */
516 if (inode->i_state & I_NEW)
517 inode = ntfs_read_mft(inode, name, ref);
518 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
519 /* Inode overlaps? */
520 _ntfs_bad_inode(inode);
527 GET_BLOCK_GENERAL = 0,
528 GET_BLOCK_WRITE_BEGIN = 1,
529 GET_BLOCK_DIRECT_IO_R = 2,
530 GET_BLOCK_DIRECT_IO_W = 3,
534 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
535 struct buffer_head *bh, int create,
536 enum get_block_ctx ctx)
538 struct super_block *sb = inode->i_sb;
539 struct ntfs_sb_info *sbi = sb->s_fs_info;
540 struct ntfs_inode *ni = ntfs_i(inode);
541 struct page *page = bh->b_page;
542 u8 cluster_bits = sbi->cluster_bits;
543 u32 block_size = sb->s_blocksize;
544 u64 bytes, lbo, valid;
550 /* Clear previous state. */
551 clear_buffer_new(bh);
552 clear_buffer_uptodate(bh);
554 /* Direct write uses 'create=0'. */
555 if (!create && vbo >= ni->i_valid) {
560 if (vbo >= inode->i_size) {
565 if (is_resident(ni)) {
567 err = attr_data_read_resident(ni, page);
571 set_buffer_uptodate(bh);
572 bh->b_size = block_size;
576 vcn = vbo >> cluster_bits;
577 off = vbo & sbi->cluster_mask;
580 err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL,
581 create && sbi->cluster_size > PAGE_SIZE);
588 bytes = ((u64)len << cluster_bits) - off;
590 if (lcn == SPARSE_LCN) {
592 if (bh->b_size > bytes)
602 lbo = ((u64)lcn << cluster_bits) + off;
604 set_buffer_mapped(bh);
605 bh->b_bdev = sb->s_bdev;
606 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
610 if (ctx == GET_BLOCK_DIRECT_IO_W) {
611 /* ntfs_direct_IO will update ni->i_valid. */
616 if (bytes > bh->b_size)
622 if (vbo + bytes > valid) {
623 ni->i_valid = vbo + bytes;
624 mark_inode_dirty(inode);
626 } else if (vbo >= valid) {
627 /* Read out of valid data. */
628 /* Should never be here 'cause already checked. */
629 clear_buffer_mapped(bh);
630 } else if (vbo + bytes <= valid) {
632 } else if (vbo + block_size <= valid) {
633 /* Normal short read. */
637 * Read across valid size: vbo < valid && valid < vbo + block_size
642 u32 voff = valid - vbo;
644 bh->b_size = block_size;
645 off = vbo & (PAGE_SIZE - 1);
646 set_bh_page(bh, page, off);
649 bh->b_end_io = end_buffer_read_sync;
651 submit_bh(REQ_OP_READ, bh);
654 if (!buffer_uptodate(bh)) {
658 zero_user_segment(page, off + voff, off + block_size);
662 if (bh->b_size > bytes)
666 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
667 static_assert(sizeof(size_t) < sizeof(loff_t));
668 if (bytes > 0x40000000u)
669 bh->b_size = 0x40000000u;
679 int ntfs_get_block(struct inode *inode, sector_t vbn,
680 struct buffer_head *bh_result, int create)
682 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
683 bh_result, create, GET_BLOCK_GENERAL);
686 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
687 struct buffer_head *bh_result, int create)
689 return ntfs_get_block_vbo(inode,
690 (u64)vsn << inode->i_sb->s_blocksize_bits,
691 bh_result, create, GET_BLOCK_BMAP);
694 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
696 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
699 static int ntfs_read_folio(struct file *file, struct folio *folio)
701 struct page *page = &folio->page;
703 struct address_space *mapping = page->mapping;
704 struct inode *inode = mapping->host;
705 struct ntfs_inode *ni = ntfs_i(inode);
707 if (is_resident(ni)) {
709 err = attr_data_read_resident(ni, page);
711 if (err != E_NTFS_NONRESIDENT) {
717 if (is_compressed(ni)) {
719 err = ni_readpage_cmpr(ni, page);
724 /* Normal + sparse files. */
725 return mpage_read_folio(folio, ntfs_get_block);
728 static void ntfs_readahead(struct readahead_control *rac)
730 struct address_space *mapping = rac->mapping;
731 struct inode *inode = mapping->host;
732 struct ntfs_inode *ni = ntfs_i(inode);
736 if (is_resident(ni)) {
737 /* No readahead for resident. */
741 if (is_compressed(ni)) {
742 /* No readahead for compressed. */
747 pos = readahead_pos(rac);
749 if (valid < i_size_read(inode) && pos <= valid &&
750 valid < pos + readahead_length(rac)) {
751 /* Range cross 'valid'. Read it page by page. */
755 mpage_readahead(rac, ntfs_get_block);
758 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
759 struct buffer_head *bh_result, int create)
761 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
762 bh_result, create, GET_BLOCK_DIRECT_IO_R);
765 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
766 struct buffer_head *bh_result, int create)
768 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
769 bh_result, create, GET_BLOCK_DIRECT_IO_W);
772 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
774 struct file *file = iocb->ki_filp;
775 struct address_space *mapping = file->f_mapping;
776 struct inode *inode = mapping->host;
777 struct ntfs_inode *ni = ntfs_i(inode);
778 loff_t vbo = iocb->ki_pos;
780 int wr = iov_iter_rw(iter) & WRITE;
781 size_t iter_count = iov_iter_count(iter);
785 if (is_resident(ni)) {
786 /* Switch to buffered write. */
791 ret = blockdev_direct_IO(iocb, inode, iter,
792 wr ? ntfs_get_block_direct_IO_W
793 : ntfs_get_block_direct_IO_R);
797 else if (wr && ret == -EIOCBQUEUED)
798 end = vbo + iter_count;
804 if (end > valid && !S_ISBLK(inode->i_mode)) {
806 mark_inode_dirty(inode);
808 } else if (vbo < valid && valid < end) {
810 iov_iter_revert(iter, end - valid);
811 iov_iter_zero(end - valid, iter);
818 int ntfs_set_size(struct inode *inode, u64 new_size)
820 struct super_block *sb = inode->i_sb;
821 struct ntfs_sb_info *sbi = sb->s_fs_info;
822 struct ntfs_inode *ni = ntfs_i(inode);
825 /* Check for maximum file size. */
826 if (is_sparsed(ni) || is_compressed(ni)) {
827 if (new_size > sbi->maxbytes_sparse) {
831 } else if (new_size > sbi->maxbytes) {
837 down_write(&ni->file.run_lock);
839 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
840 &ni->i_valid, true, NULL);
842 up_write(&ni->file.run_lock);
845 mark_inode_dirty(inode);
851 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
853 struct address_space *mapping = page->mapping;
854 struct inode *inode = mapping->host;
855 struct ntfs_inode *ni = ntfs_i(inode);
858 if (is_resident(ni)) {
860 err = attr_data_write_resident(ni, page);
862 if (err != E_NTFS_NONRESIDENT) {
868 return block_write_full_page(page, ntfs_get_block, wbc);
871 static int ntfs_writepages(struct address_space *mapping,
872 struct writeback_control *wbc)
874 /* Redirect call to 'ntfs_writepage' for resident files. */
875 if (is_resident(ntfs_i(mapping->host)))
876 return generic_writepages(mapping, wbc);
877 return mpage_writepages(mapping, wbc, ntfs_get_block);
880 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
881 struct buffer_head *bh_result, int create)
883 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
884 bh_result, create, GET_BLOCK_WRITE_BEGIN);
887 int ntfs_write_begin(struct file *file, struct address_space *mapping,
888 loff_t pos, u32 len, struct page **pagep, void **fsdata)
891 struct inode *inode = mapping->host;
892 struct ntfs_inode *ni = ntfs_i(inode);
895 if (is_resident(ni)) {
896 struct page *page = grab_cache_page_write_begin(
897 mapping, pos >> PAGE_SHIFT);
905 err = attr_data_read_resident(ni, page);
915 if (err != E_NTFS_NONRESIDENT)
919 err = block_write_begin(mapping, pos, len, pagep,
920 ntfs_get_block_write_begin);
927 * ntfs_write_end - Address_space_operations::write_end.
929 int ntfs_write_end(struct file *file, struct address_space *mapping,
930 loff_t pos, u32 len, u32 copied, struct page *page,
933 struct inode *inode = mapping->host;
934 struct ntfs_inode *ni = ntfs_i(inode);
935 u64 valid = ni->i_valid;
939 if (is_resident(ni)) {
941 err = attr_data_write_resident(ni, page);
945 /* Clear any buffers in page. */
946 if (page_has_buffers(page)) {
947 struct buffer_head *head, *bh;
949 bh = head = page_buffers(page);
951 clear_buffer_dirty(bh);
952 clear_buffer_mapped(bh);
953 set_buffer_uptodate(bh);
954 } while (head != (bh = bh->b_this_page));
956 SetPageUptodate(page);
962 err = generic_write_end(file, mapping, pos, len, copied, page,
967 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
968 inode->i_ctime = inode->i_mtime = current_time(inode);
969 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
973 if (valid != ni->i_valid) {
974 /* ni->i_valid is changed in ntfs_get_block_vbo. */
979 mark_inode_dirty(inode);
985 int reset_log_file(struct inode *inode)
989 u32 log_size = inode->i_size;
990 struct address_space *mapping = inode->i_mapping;
997 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
999 err = block_write_begin(mapping, pos, len, &page,
1000 ntfs_get_block_write_begin);
1004 kaddr = kmap_atomic(page);
1005 memset(kaddr, -1, len);
1006 kunmap_atomic(kaddr);
1007 flush_dcache_page(page);
1009 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
1014 if (pos >= log_size)
1016 balance_dirty_pages_ratelimited(mapping);
1019 mark_inode_dirty_sync(inode);
1024 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1026 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1029 int ntfs_sync_inode(struct inode *inode)
1031 return _ni_write_inode(inode, 1);
1035 * writeback_inode - Helper function for ntfs_flush_inodes().
1037 * This writes both the inode and the file data blocks, waiting
1038 * for in flight data blocks before the start of the call. It
1039 * does not wait for any io started during the call.
1041 static int writeback_inode(struct inode *inode)
1043 int ret = sync_inode_metadata(inode, 0);
1046 ret = filemap_fdatawrite(inode->i_mapping);
1053 * Write data and metadata corresponding to i1 and i2. The io is
1054 * started but we do not wait for any of it to finish.
1056 * filemap_flush() is used for the block device, so if there is a dirty
1057 * page for a block already in flight, we will not wait and start the
1060 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1066 ret = writeback_inode(i1);
1068 ret = writeback_inode(i2);
1070 ret = sync_blockdev_nowait(sb->s_bdev);
1074 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1078 /* Write non resident data. */
1079 for (idx = 0; bytes; idx++) {
1080 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1081 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1084 return PTR_ERR(page);
1087 WARN_ON(!PageUptodate(page));
1088 ClearPageUptodate(page);
1090 memcpy(page_address(page), data, op);
1092 flush_dcache_page(page);
1093 SetPageUptodate(page);
1096 ntfs_unmap_page(page);
1099 data = Add2Ptr(data, PAGE_SIZE);
1105 * ntfs_reparse_bytes
1107 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1108 * for unicode string of @uni_len length.
1110 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1112 /* Header + unicode string + decorated unicode string. */
1113 return sizeof(short) * (2 * uni_len + 4) +
1114 offsetof(struct REPARSE_DATA_BUFFER,
1115 SymbolicLinkReparseBuffer.PathBuffer);
1118 static struct REPARSE_DATA_BUFFER *
1119 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1120 u32 size, u16 *nsize)
1123 struct REPARSE_DATA_BUFFER *rp;
1125 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1127 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1129 return ERR_PTR(-ENOMEM);
1131 rs = &rp->SymbolicLinkReparseBuffer;
1132 rp_name = rs->PathBuffer;
1134 /* Convert link name to UTF-16. */
1135 err = ntfs_nls_to_utf16(sbi, symname, size,
1136 (struct cpu_str *)(rp_name - 1), 2 * size,
1137 UTF16_LITTLE_ENDIAN);
1141 /* err = the length of unicode name of symlink. */
1142 *nsize = ntfs_reparse_bytes(err);
1144 if (*nsize > sbi->reparse.max_size) {
1149 /* Translate Linux '/' into Windows '\'. */
1150 for (i = 0; i < err; i++) {
1151 if (rp_name[i] == cpu_to_le16('/'))
1152 rp_name[i] = cpu_to_le16('\\');
1155 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1156 rp->ReparseDataLength =
1157 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1158 SymbolicLinkReparseBuffer));
1160 /* PrintName + SubstituteName. */
1161 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1162 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1163 rs->PrintNameLength = rs->SubstituteNameOffset;
1166 * TODO: Use relative path if possible to allow Windows to
1168 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1172 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1174 /* Decorate SubstituteName. */
1176 rp_name[0] = cpu_to_le16('\\');
1177 rp_name[1] = cpu_to_le16('?');
1178 rp_name[2] = cpu_to_le16('?');
1179 rp_name[3] = cpu_to_le16('\\');
1184 return ERR_PTR(err);
1187 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1188 struct inode *dir, struct dentry *dentry,
1189 const struct cpu_str *uni, umode_t mode,
1190 dev_t dev, const char *symname, u32 size,
1191 struct ntfs_fnd *fnd)
1194 struct super_block *sb = dir->i_sb;
1195 struct ntfs_sb_info *sbi = sb->s_fs_info;
1196 const struct qstr *name = &dentry->d_name;
1198 struct ntfs_inode *dir_ni = ntfs_i(dir);
1199 struct ntfs_inode *ni = NULL;
1200 struct inode *inode = NULL;
1201 struct ATTRIB *attr;
1202 struct ATTR_STD_INFO5 *std5;
1203 struct ATTR_FILE_NAME *fname;
1204 struct MFT_REC *rec;
1205 u32 asize, dsize, sd_size;
1206 enum FILE_ATTRIBUTE fa;
1207 __le32 security_id = SECURITY_ID_INVALID;
1210 u16 t16, nsize = 0, aid = 0;
1211 struct INDEX_ROOT *root, *dir_root;
1212 struct NTFS_DE *e, *new_de = NULL;
1213 struct REPARSE_DATA_BUFFER *rp = NULL;
1214 bool rp_inserted = false;
1216 ni_lock_dir(dir_ni);
1218 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1224 if (S_ISDIR(mode)) {
1225 /* Use parent's directory attributes. */
1226 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1227 FILE_ATTRIBUTE_ARCHIVE;
1229 * By default child directory inherits parent attributes.
1230 * Root directory is hidden + system.
1231 * Make an exception for children in root.
1233 if (dir->i_ino == MFT_REC_ROOT)
1234 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1235 } else if (S_ISLNK(mode)) {
1236 /* It is good idea that link should be the same type (file/dir) as target */
1237 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1240 * Linux: there are dir/file/symlink and so on.
1241 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1242 * It is good idea to create:
1243 * dir + reparse if 'symname' points to directory
1245 * file + reparse if 'symname' points to file
1246 * Unfortunately kern_path hangs if symname contains 'dir'.
1252 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1253 * struct inode *target = d_inode(path.dentry);
1255 * if (S_ISDIR(target->i_mode))
1256 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1257 * // if ( target->i_sb == sb ){
1258 * // use relative path?
1263 } else if (S_ISREG(mode)) {
1264 if (sbi->options->sparse) {
1265 /* Sparsed regular file, cause option 'sparse'. */
1266 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1267 FILE_ATTRIBUTE_ARCHIVE;
1268 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1269 /* Compressed regular file, if parent is compressed. */
1270 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1272 /* Regular file, default attributes. */
1273 fa = FILE_ATTRIBUTE_ARCHIVE;
1276 fa = FILE_ATTRIBUTE_ARCHIVE;
1279 /* If option "hide_dot_files" then set hidden attribute for dot files. */
1280 if (sbi->options->hide_dot_files && name->name[0] == '.')
1281 fa |= FILE_ATTRIBUTE_HIDDEN;
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);
1369 if (sbi->options->windows_names &&
1370 !valid_windows_name(sbi, (struct le_str *)&fname->name_len)) {
1375 mi_get_ref(&dir_ni->mi, &fname->home);
1376 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1377 fname->dup.a_time = std5->cr_time;
1378 fname->dup.alloc_size = fname->dup.data_size = 0;
1379 fname->dup.fa = std5->fa;
1380 fname->dup.ea_size = fname->dup.reparse = 0;
1382 dsize = le16_to_cpu(new_de->key_size);
1383 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1385 attr->type = ATTR_NAME;
1386 attr->size = cpu_to_le32(asize);
1387 attr->res.data_off = SIZEOF_RESIDENT_LE;
1388 attr->res.flags = RESIDENT_FLAG_INDEXED;
1389 attr->id = cpu_to_le16(aid++);
1390 attr->res.data_size = cpu_to_le32(dsize);
1391 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1393 attr = Add2Ptr(attr, asize);
1395 if (security_id == SECURITY_ID_INVALID) {
1396 /* Insert security attribute. */
1397 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1399 attr->type = ATTR_SECURE;
1400 attr->size = cpu_to_le32(asize);
1401 attr->id = cpu_to_le16(aid++);
1402 attr->res.data_off = SIZEOF_RESIDENT_LE;
1403 attr->res.data_size = cpu_to_le32(sd_size);
1404 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1406 attr = Add2Ptr(attr, asize);
1409 attr->id = cpu_to_le16(aid++);
1410 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1412 * Regular directory or symlink to directory.
1413 * Create root attribute.
1415 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1416 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1418 attr->type = ATTR_ROOT;
1419 attr->size = cpu_to_le32(asize);
1421 attr->name_len = ARRAY_SIZE(I30_NAME);
1422 attr->name_off = SIZEOF_RESIDENT_LE;
1423 attr->res.data_off =
1424 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1425 attr->res.data_size = cpu_to_le32(dsize);
1426 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1429 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1430 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1432 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1433 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1434 sizeof(struct NTFS_DE));
1435 root->ihdr.total = root->ihdr.used;
1437 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1438 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1439 e->flags = NTFS_IE_LAST;
1440 } else if (S_ISLNK(mode)) {
1443 * Create empty resident data attribute.
1445 asize = SIZEOF_RESIDENT;
1447 /* Insert empty ATTR_DATA */
1448 attr->type = ATTR_DATA;
1449 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1450 attr->name_off = SIZEOF_RESIDENT_LE;
1451 attr->res.data_off = SIZEOF_RESIDENT_LE;
1452 } else if (S_ISREG(mode)) {
1454 * Regular file. Create empty non resident data attribute.
1456 attr->type = ATTR_DATA;
1458 attr->nres.evcn = cpu_to_le64(-1ll);
1459 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1460 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1461 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1462 attr->flags = ATTR_FLAG_SPARSED;
1463 asize = SIZEOF_NONRESIDENT_EX + 8;
1464 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1465 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1466 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1467 attr->flags = ATTR_FLAG_COMPRESSED;
1468 attr->nres.c_unit = COMPRESSION_UNIT;
1469 asize = SIZEOF_NONRESIDENT_EX + 8;
1471 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1472 attr->name_off = SIZEOF_NONRESIDENT_LE;
1473 asize = SIZEOF_NONRESIDENT + 8;
1475 attr->nres.run_off = attr->name_off;
1478 * Node. Create empty resident data attribute.
1480 attr->type = ATTR_DATA;
1481 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1482 attr->name_off = SIZEOF_RESIDENT_LE;
1483 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1484 attr->flags = ATTR_FLAG_SPARSED;
1485 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1486 attr->flags = ATTR_FLAG_COMPRESSED;
1487 attr->res.data_off = SIZEOF_RESIDENT_LE;
1488 asize = SIZEOF_RESIDENT;
1489 ni->ni_flags |= NI_FLAG_RESIDENT;
1492 if (S_ISDIR(mode)) {
1493 ni->ni_flags |= NI_FLAG_DIR;
1494 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1497 } else if (S_ISLNK(mode)) {
1498 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1507 * Insert ATTR_REPARSE.
1509 attr = Add2Ptr(attr, asize);
1510 attr->type = ATTR_REPARSE;
1511 attr->id = cpu_to_le16(aid++);
1513 /* Resident or non resident? */
1514 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1515 t16 = PtrOffset(rec, attr);
1518 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1519 * It is good idea to keep extened attributes resident.
1521 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1523 CLST clst = bytes_to_cluster(sbi, nsize);
1525 /* Bytes per runs. */
1526 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1529 attr->nres.evcn = cpu_to_le64(clst - 1);
1530 attr->name_off = SIZEOF_NONRESIDENT_LE;
1531 attr->nres.run_off = attr->name_off;
1532 attr->nres.data_size = cpu_to_le64(nsize);
1533 attr->nres.valid_size = attr->nres.data_size;
1534 attr->nres.alloc_size =
1535 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1537 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1538 clst, NULL, ALLOCATE_DEF,
1539 &alen, 0, NULL, NULL);
1543 err = run_pack(&ni->file.run, 0, clst,
1544 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1554 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1556 attr->res.data_off = SIZEOF_RESIDENT_LE;
1557 attr->res.data_size = cpu_to_le32(nsize);
1558 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1561 /* Size of symlink equals the length of input string. */
1562 inode->i_size = size;
1564 attr->size = cpu_to_le32(asize);
1566 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1574 attr = Add2Ptr(attr, asize);
1575 attr->type = ATTR_END;
1577 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1578 rec->next_attr_id = cpu_to_le16(aid);
1580 /* Step 2: Add new name in index. */
1581 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1585 /* Unlock parent directory before ntfs_init_acl. */
1588 inode->i_generation = le16_to_cpu(rec->seq);
1590 dir->i_mtime = dir->i_ctime = inode->i_atime;
1592 if (S_ISDIR(mode)) {
1593 inode->i_op = &ntfs_dir_inode_operations;
1594 inode->i_fop = &ntfs_dir_operations;
1595 } else if (S_ISLNK(mode)) {
1596 inode->i_op = &ntfs_link_inode_operations;
1597 inode->i_fop = NULL;
1598 inode->i_mapping->a_ops = &ntfs_aops;
1599 inode->i_size = size;
1600 inode_nohighmem(inode);
1601 } else if (S_ISREG(mode)) {
1602 inode->i_op = &ntfs_file_inode_operations;
1603 inode->i_fop = &ntfs_file_operations;
1604 inode->i_mapping->a_ops =
1605 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1606 init_rwsem(&ni->file.run_lock);
1608 inode->i_op = &ntfs_special_inode_operations;
1609 init_special_inode(inode, mode, dev);
1612 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1613 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1614 err = ntfs_init_acl(mnt_userns, inode, dir);
1620 inode->i_flags |= S_NOSEC;
1623 /* Write non resident data. */
1625 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1631 * Call 'd_instantiate' after inode->i_op is set
1632 * but before finish_open.
1634 d_instantiate(dentry, inode);
1636 ntfs_save_wsl_perm(inode);
1637 mark_inode_dirty(dir);
1638 mark_inode_dirty(inode);
1645 /* Undo 'indx_insert_entry'. */
1646 ni_lock_dir(dir_ni);
1647 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1648 le16_to_cpu(new_de->key_size), sbi);
1649 /* ni_unlock(dir_ni); will be called later. */
1652 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1655 if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1658 run_deallocate(sbi, &ni->file.run, false);
1661 clear_rec_inuse(rec);
1663 ni->mi.dirty = false;
1664 discard_new_inode(inode);
1666 ntfs_mark_rec_free(sbi, ino, false);
1675 return ERR_PTR(err);
1678 unlock_new_inode(inode);
1683 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1686 struct ntfs_inode *ni = ntfs_i(inode);
1687 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1690 /* Allocate PATH_MAX bytes. */
1695 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1696 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1698 /* Construct 'de'. */
1699 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1703 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1712 * inode_operations::unlink
1713 * inode_operations::rmdir
1715 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1718 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1719 struct inode *inode = d_inode(dentry);
1720 struct ntfs_inode *ni = ntfs_i(inode);
1721 struct ntfs_inode *dir_ni = ntfs_i(dir);
1722 struct NTFS_DE *de, *de2 = NULL;
1725 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1728 /* Allocate PATH_MAX bytes. */
1735 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1740 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1745 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1749 dir->i_mtime = dir->i_ctime = current_time(dir);
1750 mark_inode_dirty(dir);
1751 inode->i_ctime = dir->i_ctime;
1753 mark_inode_dirty(inode);
1754 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1755 _ntfs_bad_inode(inode);
1757 if (ni_is_dirty(dir))
1758 mark_inode_dirty(dir);
1759 if (ni_is_dirty(inode))
1760 mark_inode_dirty(inode);
1769 void ntfs_evict_inode(struct inode *inode)
1771 truncate_inode_pages_final(&inode->i_data);
1774 _ni_write_inode(inode, inode_needs_sync(inode));
1776 invalidate_inode_buffers(inode);
1779 ni_clear(ntfs_i(inode));
1783 * ntfs_translate_junction
1785 * Translate a Windows junction target to the Linux equivalent.
1786 * On junctions, targets are always absolute (they include the drive
1787 * letter). We have no way of knowing if the target is for the current
1788 * mounted device or not so we just assume it is.
1790 static int ntfs_translate_junction(const struct super_block *sb,
1791 const struct dentry *link_de, char *target,
1792 int target_len, int target_max)
1794 int tl_len, err = target_len;
1795 char *link_path_buffer = NULL, *link_path;
1796 char *translated = NULL;
1800 link_path_buffer = kmalloc(PATH_MAX, GFP_NOFS);
1801 if (!link_path_buffer) {
1805 /* Get link path, relative to mount point */
1806 link_path = dentry_path_raw(link_de, link_path_buffer, PATH_MAX);
1807 if (IS_ERR(link_path)) {
1808 ntfs_err(sb, "Error getting link path");
1813 translated = kmalloc(PATH_MAX, GFP_NOFS);
1819 /* Make translated path a relative path to mount point */
1820 strcpy(translated, "./");
1821 ++link_path; /* Skip leading / */
1822 for (tl_len = sizeof("./") - 1; *link_path; ++link_path) {
1823 if (*link_path == '/') {
1824 if (PATH_MAX - tl_len < sizeof("../")) {
1825 ntfs_err(sb, "Link path %s has too many components",
1830 strcpy(translated + tl_len, "../");
1831 tl_len += sizeof("../") - 1;
1835 /* Skip drive letter */
1836 target_start = target;
1837 while (*target_start && *target_start != ':')
1840 if (!*target_start) {
1841 ntfs_err(sb, "Link target (%s) missing drive separator", target);
1846 /* Skip drive separator and leading /, if exists */
1847 target_start += 1 + (target_start[1] == '/');
1848 copy_len = target_len - (target_start - target);
1850 if (PATH_MAX - tl_len <= copy_len) {
1851 ntfs_err(sb, "Link target %s too large for buffer (%d <= %d)",
1852 target_start, PATH_MAX - tl_len, copy_len);
1857 /* translated path has a trailing / and target_start does not */
1858 strcpy(translated + tl_len, target_start);
1860 if (target_max <= tl_len) {
1861 ntfs_err(sb, "Target path %s too large for buffer (%d <= %d)",
1862 translated, target_max, tl_len);
1866 strcpy(target, translated);
1870 kfree(link_path_buffer);
1875 static noinline int ntfs_readlink_hlp(const struct dentry *link_de,
1876 struct inode *inode, char *buffer,
1879 int i, err = -EINVAL;
1880 struct ntfs_inode *ni = ntfs_i(inode);
1881 struct super_block *sb = inode->i_sb;
1882 struct ntfs_sb_info *sbi = sb->s_fs_info;
1885 void *to_free = NULL;
1886 struct REPARSE_DATA_BUFFER *rp;
1887 const __le16 *uname;
1888 struct ATTRIB *attr;
1890 /* Reparse data present. Try to parse it. */
1891 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1892 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1896 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1900 if (!attr->non_res) {
1901 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1904 size = le32_to_cpu(attr->res.data_size);
1906 size = le64_to_cpu(attr->nres.data_size);
1910 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1914 rp = kmalloc(size, GFP_NOFS);
1920 /* Read into temporal buffer. */
1921 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1926 /* Microsoft Tag. */
1927 switch (rp->ReparseTag) {
1928 case IO_REPARSE_TAG_MOUNT_POINT:
1929 /* Mount points and junctions. */
1930 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1931 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1932 MountPointReparseBuffer.PathBuffer))
1935 offsetof(struct REPARSE_DATA_BUFFER,
1936 MountPointReparseBuffer.PathBuffer) +
1937 le16_to_cpu(rp->MountPointReparseBuffer
1939 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1942 case IO_REPARSE_TAG_SYMLINK:
1943 /* FolderSymbolicLink */
1944 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1945 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1946 SymbolicLinkReparseBuffer.PathBuffer))
1949 rp, offsetof(struct REPARSE_DATA_BUFFER,
1950 SymbolicLinkReparseBuffer.PathBuffer) +
1951 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1954 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1957 case IO_REPARSE_TAG_CLOUD:
1958 case IO_REPARSE_TAG_CLOUD_1:
1959 case IO_REPARSE_TAG_CLOUD_2:
1960 case IO_REPARSE_TAG_CLOUD_3:
1961 case IO_REPARSE_TAG_CLOUD_4:
1962 case IO_REPARSE_TAG_CLOUD_5:
1963 case IO_REPARSE_TAG_CLOUD_6:
1964 case IO_REPARSE_TAG_CLOUD_7:
1965 case IO_REPARSE_TAG_CLOUD_8:
1966 case IO_REPARSE_TAG_CLOUD_9:
1967 case IO_REPARSE_TAG_CLOUD_A:
1968 case IO_REPARSE_TAG_CLOUD_B:
1969 case IO_REPARSE_TAG_CLOUD_C:
1970 case IO_REPARSE_TAG_CLOUD_D:
1971 case IO_REPARSE_TAG_CLOUD_E:
1972 case IO_REPARSE_TAG_CLOUD_F:
1973 err = sizeof("OneDrive") - 1;
1976 memcpy(buffer, "OneDrive", err);
1980 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1981 /* Unknown Microsoft Tag. */
1984 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1985 size <= sizeof(struct REPARSE_POINT)) {
1990 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1991 ulen = le16_to_cpu(rp->ReparseDataLength) -
1992 sizeof(struct REPARSE_POINT);
1995 /* Convert nlen from bytes to UNICODE chars. */
1998 /* Check that name is available. */
1999 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
2002 /* If name is already zero terminated then truncate it now. */
2003 if (!uname[ulen - 1])
2006 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
2011 /* Translate Windows '\' into Linux '/'. */
2012 for (i = 0; i < err; i++) {
2013 if (buffer[i] == '\\')
2017 /* Always set last zero. */
2020 /* If this is a junction, translate the link target. */
2021 if (rp->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
2022 err = ntfs_translate_junction(sb, link_de, buffer, err, buflen);
2029 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
2030 struct delayed_call *done)
2036 return ERR_PTR(-ECHILD);
2038 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
2040 return ERR_PTR(-ENOMEM);
2042 err = ntfs_readlink_hlp(de, inode, ret, PAGE_SIZE);
2045 return ERR_PTR(err);
2048 set_delayed_call(done, kfree_link, ret);
2054 const struct inode_operations ntfs_link_inode_operations = {
2055 .get_link = ntfs_get_link,
2056 .setattr = ntfs3_setattr,
2057 .listxattr = ntfs_listxattr,
2058 .permission = ntfs_permission,
2059 .get_acl = ntfs_get_acl,
2060 .set_acl = ntfs_set_acl,
2063 const struct address_space_operations ntfs_aops = {
2064 .read_folio = ntfs_read_folio,
2065 .readahead = ntfs_readahead,
2066 .writepage = ntfs_writepage,
2067 .writepages = ntfs_writepages,
2068 .write_begin = ntfs_write_begin,
2069 .write_end = ntfs_write_end,
2070 .direct_IO = ntfs_direct_IO,
2072 .dirty_folio = block_dirty_folio,
2073 .invalidate_folio = block_invalidate_folio,
2076 const struct address_space_operations ntfs_aops_cmpr = {
2077 .read_folio = ntfs_read_folio,
2078 .readahead = ntfs_readahead,