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) {
405 /* Reuse rec as buffer for ascii name. */
410 if (std5->fa & FILE_ATTRIBUTE_READONLY)
418 if (names != le16_to_cpu(rec->hard_links)) {
419 /* Correct minor error on the fly. Do not mark inode as dirty. */
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 make_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 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
661 if (!buffer_uptodate(bh)) {
665 zero_user_segment(page, off + voff, off + block_size);
669 if (bh->b_size > bytes)
673 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
674 static_assert(sizeof(size_t) < sizeof(loff_t));
675 if (bytes > 0x40000000u)
676 bh->b_size = 0x40000000u;
686 int ntfs_get_block(struct inode *inode, sector_t vbn,
687 struct buffer_head *bh_result, int create)
689 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
690 bh_result, create, GET_BLOCK_GENERAL);
693 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
694 struct buffer_head *bh_result, int create)
696 return ntfs_get_block_vbo(inode,
697 (u64)vsn << inode->i_sb->s_blocksize_bits,
698 bh_result, create, GET_BLOCK_BMAP);
701 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
703 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
706 static int ntfs_readpage(struct file *file, struct page *page)
709 struct address_space *mapping = page->mapping;
710 struct inode *inode = mapping->host;
711 struct ntfs_inode *ni = ntfs_i(inode);
713 if (is_resident(ni)) {
715 err = attr_data_read_resident(ni, page);
717 if (err != E_NTFS_NONRESIDENT) {
723 if (is_compressed(ni)) {
725 err = ni_readpage_cmpr(ni, page);
730 /* Normal + sparse files. */
731 return mpage_readpage(page, ntfs_get_block);
734 static void ntfs_readahead(struct readahead_control *rac)
736 struct address_space *mapping = rac->mapping;
737 struct inode *inode = mapping->host;
738 struct ntfs_inode *ni = ntfs_i(inode);
742 if (is_resident(ni)) {
743 /* No readahead for resident. */
747 if (is_compressed(ni)) {
748 /* No readahead for compressed. */
753 pos = readahead_pos(rac);
755 if (valid < i_size_read(inode) && pos <= valid &&
756 valid < pos + readahead_length(rac)) {
757 /* Range cross 'valid'. Read it page by page. */
761 mpage_readahead(rac, ntfs_get_block);
764 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
765 struct buffer_head *bh_result, int create)
767 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
768 bh_result, create, GET_BLOCK_DIRECT_IO_R);
771 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
772 struct buffer_head *bh_result, int create)
774 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
775 bh_result, create, GET_BLOCK_DIRECT_IO_W);
778 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
780 struct file *file = iocb->ki_filp;
781 struct address_space *mapping = file->f_mapping;
782 struct inode *inode = mapping->host;
783 struct ntfs_inode *ni = ntfs_i(inode);
784 loff_t vbo = iocb->ki_pos;
786 int wr = iov_iter_rw(iter) & WRITE;
787 size_t iter_count = iov_iter_count(iter);
791 if (is_resident(ni)) {
792 /* Switch to buffered write. */
797 ret = blockdev_direct_IO(iocb, inode, iter,
798 wr ? ntfs_get_block_direct_IO_W
799 : ntfs_get_block_direct_IO_R);
803 else if (wr && ret == -EIOCBQUEUED)
804 end = vbo + iter_count;
810 if (end > valid && !S_ISBLK(inode->i_mode)) {
812 mark_inode_dirty(inode);
814 } else if (vbo < valid && valid < end) {
816 iov_iter_revert(iter, end - valid);
817 iov_iter_zero(end - valid, iter);
824 int ntfs_set_size(struct inode *inode, u64 new_size)
826 struct super_block *sb = inode->i_sb;
827 struct ntfs_sb_info *sbi = sb->s_fs_info;
828 struct ntfs_inode *ni = ntfs_i(inode);
831 /* Check for maximum file size. */
832 if (is_sparsed(ni) || is_compressed(ni)) {
833 if (new_size > sbi->maxbytes_sparse) {
837 } else if (new_size > sbi->maxbytes) {
843 down_write(&ni->file.run_lock);
845 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
846 &ni->i_valid, true, NULL);
848 up_write(&ni->file.run_lock);
851 mark_inode_dirty(inode);
857 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
859 struct address_space *mapping = page->mapping;
860 struct inode *inode = mapping->host;
861 struct ntfs_inode *ni = ntfs_i(inode);
864 if (is_resident(ni)) {
866 err = attr_data_write_resident(ni, page);
868 if (err != E_NTFS_NONRESIDENT) {
874 return block_write_full_page(page, ntfs_get_block, wbc);
877 static int ntfs_writepages(struct address_space *mapping,
878 struct writeback_control *wbc)
880 struct inode *inode = mapping->host;
881 struct ntfs_inode *ni = ntfs_i(inode);
882 /* Redirect call to 'ntfs_writepage' for resident files. */
883 get_block_t *get_block = is_resident(ni) ? NULL : &ntfs_get_block;
885 return mpage_writepages(mapping, wbc, get_block);
888 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
889 struct buffer_head *bh_result, int create)
891 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
892 bh_result, create, GET_BLOCK_WRITE_BEGIN);
895 static int ntfs_write_begin(struct file *file, struct address_space *mapping,
896 loff_t pos, u32 len, u32 flags, struct page **pagep,
900 struct inode *inode = mapping->host;
901 struct ntfs_inode *ni = ntfs_i(inode);
904 if (is_resident(ni)) {
905 struct page *page = grab_cache_page_write_begin(
906 mapping, pos >> PAGE_SHIFT, flags);
914 err = attr_data_read_resident(ni, page);
924 if (err != E_NTFS_NONRESIDENT)
928 err = block_write_begin(mapping, pos, len, flags, pagep,
929 ntfs_get_block_write_begin);
936 * ntfs_write_end - Address_space_operations::write_end.
938 static int ntfs_write_end(struct file *file, struct address_space *mapping,
939 loff_t pos, u32 len, u32 copied, struct page *page,
943 struct inode *inode = mapping->host;
944 struct ntfs_inode *ni = ntfs_i(inode);
945 u64 valid = ni->i_valid;
949 if (is_resident(ni)) {
951 err = attr_data_write_resident(ni, page);
955 /* Clear any buffers in page. */
956 if (page_has_buffers(page)) {
957 struct buffer_head *head, *bh;
959 bh = head = page_buffers(page);
961 clear_buffer_dirty(bh);
962 clear_buffer_mapped(bh);
963 set_buffer_uptodate(bh);
964 } while (head != (bh = bh->b_this_page));
966 SetPageUptodate(page);
972 err = generic_write_end(file, mapping, pos, len, copied, page,
977 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
978 inode->i_ctime = inode->i_mtime = current_time(inode);
979 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
983 if (valid != ni->i_valid) {
984 /* ni->i_valid is changed in ntfs_get_block_vbo. */
989 mark_inode_dirty(inode);
995 int reset_log_file(struct inode *inode)
999 u32 log_size = inode->i_size;
1000 struct address_space *mapping = inode->i_mapping;
1007 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
1009 err = block_write_begin(mapping, pos, len, 0, &page,
1010 ntfs_get_block_write_begin);
1014 kaddr = kmap_atomic(page);
1015 memset(kaddr, -1, len);
1016 kunmap_atomic(kaddr);
1017 flush_dcache_page(page);
1019 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
1024 if (pos >= log_size)
1026 balance_dirty_pages_ratelimited(mapping);
1029 mark_inode_dirty_sync(inode);
1034 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1036 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1039 int ntfs_sync_inode(struct inode *inode)
1041 return _ni_write_inode(inode, 1);
1045 * writeback_inode - Helper function for ntfs_flush_inodes().
1047 * This writes both the inode and the file data blocks, waiting
1048 * for in flight data blocks before the start of the call. It
1049 * does not wait for any io started during the call.
1051 static int writeback_inode(struct inode *inode)
1053 int ret = sync_inode_metadata(inode, 0);
1056 ret = filemap_fdatawrite(inode->i_mapping);
1063 * Write data and metadata corresponding to i1 and i2. The io is
1064 * started but we do not wait for any of it to finish.
1066 * filemap_flush() is used for the block device, so if there is a dirty
1067 * page for a block already in flight, we will not wait and start the
1070 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1076 ret = writeback_inode(i1);
1078 ret = writeback_inode(i2);
1080 ret = filemap_flush(sb->s_bdev->bd_inode->i_mapping);
1084 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1088 /* Write non resident data. */
1089 for (idx = 0; bytes; idx++) {
1090 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1091 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1094 return PTR_ERR(page);
1097 WARN_ON(!PageUptodate(page));
1098 ClearPageUptodate(page);
1100 memcpy(page_address(page), data, op);
1102 flush_dcache_page(page);
1103 SetPageUptodate(page);
1106 ntfs_unmap_page(page);
1109 data = Add2Ptr(data, PAGE_SIZE);
1115 * ntfs_reparse_bytes
1117 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1118 * for unicode string of @uni_len length.
1120 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1122 /* Header + unicode string + decorated unicode string. */
1123 return sizeof(short) * (2 * uni_len + 4) +
1124 offsetof(struct REPARSE_DATA_BUFFER,
1125 SymbolicLinkReparseBuffer.PathBuffer);
1128 static struct REPARSE_DATA_BUFFER *
1129 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1130 u32 size, u16 *nsize)
1133 struct REPARSE_DATA_BUFFER *rp;
1135 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1137 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1139 return ERR_PTR(-ENOMEM);
1141 rs = &rp->SymbolicLinkReparseBuffer;
1142 rp_name = rs->PathBuffer;
1144 /* Convert link name to UTF-16. */
1145 err = ntfs_nls_to_utf16(sbi, symname, size,
1146 (struct cpu_str *)(rp_name - 1), 2 * size,
1147 UTF16_LITTLE_ENDIAN);
1151 /* err = the length of unicode name of symlink. */
1152 *nsize = ntfs_reparse_bytes(err);
1154 if (*nsize > sbi->reparse.max_size) {
1159 /* Translate Linux '/' into Windows '\'. */
1160 for (i = 0; i < err; i++) {
1161 if (rp_name[i] == cpu_to_le16('/'))
1162 rp_name[i] = cpu_to_le16('\\');
1165 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1166 rp->ReparseDataLength =
1167 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1168 SymbolicLinkReparseBuffer));
1170 /* PrintName + SubstituteName. */
1171 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1172 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1173 rs->PrintNameLength = rs->SubstituteNameOffset;
1176 * TODO: Use relative path if possible to allow Windows to
1178 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1182 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1184 /* Decorate SubstituteName. */
1186 rp_name[0] = cpu_to_le16('\\');
1187 rp_name[1] = cpu_to_le16('?');
1188 rp_name[2] = cpu_to_le16('?');
1189 rp_name[3] = cpu_to_le16('\\');
1194 return ERR_PTR(err);
1197 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1198 struct inode *dir, struct dentry *dentry,
1199 const struct cpu_str *uni, umode_t mode,
1200 dev_t dev, const char *symname, u32 size,
1201 struct ntfs_fnd *fnd)
1204 struct super_block *sb = dir->i_sb;
1205 struct ntfs_sb_info *sbi = sb->s_fs_info;
1206 const struct qstr *name = &dentry->d_name;
1208 struct ntfs_inode *dir_ni = ntfs_i(dir);
1209 struct ntfs_inode *ni = NULL;
1210 struct inode *inode = NULL;
1211 struct ATTRIB *attr;
1212 struct ATTR_STD_INFO5 *std5;
1213 struct ATTR_FILE_NAME *fname;
1214 struct MFT_REC *rec;
1215 u32 asize, dsize, sd_size;
1216 enum FILE_ATTRIBUTE fa;
1217 __le32 security_id = SECURITY_ID_INVALID;
1220 u16 t16, nsize = 0, aid = 0;
1221 struct INDEX_ROOT *root, *dir_root;
1222 struct NTFS_DE *e, *new_de = NULL;
1223 struct REPARSE_DATA_BUFFER *rp = NULL;
1224 bool rp_inserted = false;
1226 ni_lock_dir(dir_ni);
1228 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1234 if (S_ISDIR(mode)) {
1235 /* Use parent's directory attributes. */
1236 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1237 FILE_ATTRIBUTE_ARCHIVE;
1239 * By default child directory inherits parent attributes.
1240 * Root directory is hidden + system.
1241 * Make an exception for children in root.
1243 if (dir->i_ino == MFT_REC_ROOT)
1244 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1245 } else if (S_ISLNK(mode)) {
1246 /* It is good idea that link should be the same type (file/dir) as target */
1247 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1250 * Linux: there are dir/file/symlink and so on.
1251 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1252 * It is good idea to create:
1253 * dir + reparse if 'symname' points to directory
1255 * file + reparse if 'symname' points to file
1256 * Unfortunately kern_path hangs if symname contains 'dir'.
1262 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1263 * struct inode *target = d_inode(path.dentry);
1265 * if (S_ISDIR(target->i_mode))
1266 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1267 * // if ( target->i_sb == sb ){
1268 * // use relative path?
1273 } else if (S_ISREG(mode)) {
1274 if (sbi->options->sparse) {
1275 /* Sparsed regular file, cause option 'sparse'. */
1276 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1277 FILE_ATTRIBUTE_ARCHIVE;
1278 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1279 /* Compressed regular file, if parent is compressed. */
1280 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1282 /* Regular file, default attributes. */
1283 fa = FILE_ATTRIBUTE_ARCHIVE;
1286 fa = FILE_ATTRIBUTE_ARCHIVE;
1290 fa |= FILE_ATTRIBUTE_READONLY;
1292 /* Allocate PATH_MAX bytes. */
1293 new_de = __getname();
1299 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1300 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1302 /* Step 1: allocate and fill new mft record. */
1303 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1307 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1313 inode = &ni->vfs_inode;
1314 inode_init_owner(mnt_userns, inode, dir, mode);
1315 mode = inode->i_mode;
1317 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1318 current_time(inode);
1321 rec->hard_links = cpu_to_le16(1);
1322 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1324 /* Get default security id. */
1325 sd = s_default_security;
1326 sd_size = sizeof(s_default_security);
1328 if (is_ntfs3(sbi)) {
1329 security_id = dir_ni->std_security_id;
1330 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1331 security_id = sbi->security.def_security_id;
1333 if (security_id == SECURITY_ID_INVALID &&
1334 !ntfs_insert_security(sbi, sd, sd_size,
1335 &security_id, NULL))
1336 sbi->security.def_security_id = security_id;
1340 /* Insert standard info. */
1341 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1343 if (security_id == SECURITY_ID_INVALID) {
1344 dsize = sizeof(struct ATTR_STD_INFO);
1346 dsize = sizeof(struct ATTR_STD_INFO5);
1347 std5->security_id = security_id;
1348 ni->std_security_id = security_id;
1350 asize = SIZEOF_RESIDENT + dsize;
1352 attr->type = ATTR_STD;
1353 attr->size = cpu_to_le32(asize);
1354 attr->id = cpu_to_le16(aid++);
1355 attr->res.data_off = SIZEOF_RESIDENT_LE;
1356 attr->res.data_size = cpu_to_le32(dsize);
1358 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1359 kernel2nt(&inode->i_atime);
1364 attr = Add2Ptr(attr, asize);
1366 /* Insert file name. */
1367 err = fill_name_de(sbi, new_de, name, uni);
1371 mi_get_ref(&ni->mi, &new_de->ref);
1373 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1374 mi_get_ref(&dir_ni->mi, &fname->home);
1375 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1376 fname->dup.a_time = std5->cr_time;
1377 fname->dup.alloc_size = fname->dup.data_size = 0;
1378 fname->dup.fa = std5->fa;
1379 fname->dup.ea_size = fname->dup.reparse = 0;
1381 dsize = le16_to_cpu(new_de->key_size);
1382 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1384 attr->type = ATTR_NAME;
1385 attr->size = cpu_to_le32(asize);
1386 attr->res.data_off = SIZEOF_RESIDENT_LE;
1387 attr->res.flags = RESIDENT_FLAG_INDEXED;
1388 attr->id = cpu_to_le16(aid++);
1389 attr->res.data_size = cpu_to_le32(dsize);
1390 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1392 attr = Add2Ptr(attr, asize);
1394 if (security_id == SECURITY_ID_INVALID) {
1395 /* Insert security attribute. */
1396 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1398 attr->type = ATTR_SECURE;
1399 attr->size = cpu_to_le32(asize);
1400 attr->id = cpu_to_le16(aid++);
1401 attr->res.data_off = SIZEOF_RESIDENT_LE;
1402 attr->res.data_size = cpu_to_le32(sd_size);
1403 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1405 attr = Add2Ptr(attr, asize);
1408 attr->id = cpu_to_le16(aid++);
1409 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1411 * Regular directory or symlink to directory.
1412 * Create root attribute.
1414 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1415 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1417 attr->type = ATTR_ROOT;
1418 attr->size = cpu_to_le32(asize);
1420 attr->name_len = ARRAY_SIZE(I30_NAME);
1421 attr->name_off = SIZEOF_RESIDENT_LE;
1422 attr->res.data_off =
1423 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1424 attr->res.data_size = cpu_to_le32(dsize);
1425 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1428 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1429 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1431 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1432 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1433 sizeof(struct NTFS_DE));
1434 root->ihdr.total = root->ihdr.used;
1436 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1437 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1438 e->flags = NTFS_IE_LAST;
1439 } else if (S_ISLNK(mode)) {
1442 * Create empty resident data attribute.
1444 asize = SIZEOF_RESIDENT;
1446 /* Insert empty ATTR_DATA */
1447 attr->type = ATTR_DATA;
1448 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1449 attr->name_off = SIZEOF_RESIDENT_LE;
1450 attr->res.data_off = SIZEOF_RESIDENT_LE;
1451 } else if (S_ISREG(mode)) {
1453 * Regular file. Create empty non resident data attribute.
1455 attr->type = ATTR_DATA;
1457 attr->nres.evcn = cpu_to_le64(-1ll);
1458 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1459 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1460 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1461 attr->flags = ATTR_FLAG_SPARSED;
1462 asize = SIZEOF_NONRESIDENT_EX + 8;
1463 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1464 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1465 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1466 attr->flags = ATTR_FLAG_COMPRESSED;
1467 attr->nres.c_unit = COMPRESSION_UNIT;
1468 asize = SIZEOF_NONRESIDENT_EX + 8;
1470 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1471 attr->name_off = SIZEOF_NONRESIDENT_LE;
1472 asize = SIZEOF_NONRESIDENT + 8;
1474 attr->nres.run_off = attr->name_off;
1477 * Node. Create empty resident data attribute.
1479 attr->type = ATTR_DATA;
1480 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1481 attr->name_off = SIZEOF_RESIDENT_LE;
1482 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1483 attr->flags = ATTR_FLAG_SPARSED;
1484 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1485 attr->flags = ATTR_FLAG_COMPRESSED;
1486 attr->res.data_off = SIZEOF_RESIDENT_LE;
1487 asize = SIZEOF_RESIDENT;
1488 ni->ni_flags |= NI_FLAG_RESIDENT;
1491 if (S_ISDIR(mode)) {
1492 ni->ni_flags |= NI_FLAG_DIR;
1493 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1496 } else if (S_ISLNK(mode)) {
1497 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1506 * Insert ATTR_REPARSE.
1508 attr = Add2Ptr(attr, asize);
1509 attr->type = ATTR_REPARSE;
1510 attr->id = cpu_to_le16(aid++);
1512 /* Resident or non resident? */
1513 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1514 t16 = PtrOffset(rec, attr);
1517 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1518 * It is good idea to keep extened attributes resident.
1520 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1522 CLST clst = bytes_to_cluster(sbi, nsize);
1524 /* Bytes per runs. */
1525 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1528 attr->nres.evcn = cpu_to_le64(clst - 1);
1529 attr->name_off = SIZEOF_NONRESIDENT_LE;
1530 attr->nres.run_off = attr->name_off;
1531 attr->nres.data_size = cpu_to_le64(nsize);
1532 attr->nres.valid_size = attr->nres.data_size;
1533 attr->nres.alloc_size =
1534 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1536 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1537 clst, NULL, 0, &alen, 0,
1542 err = run_pack(&ni->file.run, 0, clst,
1543 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1553 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1555 attr->res.data_off = SIZEOF_RESIDENT_LE;
1556 attr->res.data_size = cpu_to_le32(nsize);
1557 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1560 /* Size of symlink equals the length of input string. */
1561 inode->i_size = size;
1563 attr->size = cpu_to_le32(asize);
1565 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1573 attr = Add2Ptr(attr, asize);
1574 attr->type = ATTR_END;
1576 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1577 rec->next_attr_id = cpu_to_le16(aid);
1579 /* Step 2: Add new name in index. */
1580 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1584 /* Unlock parent directory before ntfs_init_acl. */
1587 inode->i_generation = le16_to_cpu(rec->seq);
1589 dir->i_mtime = dir->i_ctime = inode->i_atime;
1591 if (S_ISDIR(mode)) {
1592 inode->i_op = &ntfs_dir_inode_operations;
1593 inode->i_fop = &ntfs_dir_operations;
1594 } else if (S_ISLNK(mode)) {
1595 inode->i_op = &ntfs_link_inode_operations;
1596 inode->i_fop = NULL;
1597 inode->i_mapping->a_ops = &ntfs_aops;
1598 inode->i_size = size;
1599 inode_nohighmem(inode);
1600 } else if (S_ISREG(mode)) {
1601 inode->i_op = &ntfs_file_inode_operations;
1602 inode->i_fop = &ntfs_file_operations;
1603 inode->i_mapping->a_ops =
1604 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1605 init_rwsem(&ni->file.run_lock);
1607 inode->i_op = &ntfs_special_inode_operations;
1608 init_special_inode(inode, mode, dev);
1611 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1612 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1613 err = ntfs_init_acl(mnt_userns, inode, dir);
1619 inode->i_flags |= S_NOSEC;
1622 /* Write non resident data. */
1624 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1630 * Call 'd_instantiate' after inode->i_op is set
1631 * but before finish_open.
1633 d_instantiate(dentry, inode);
1635 ntfs_save_wsl_perm(inode);
1636 mark_inode_dirty(dir);
1637 mark_inode_dirty(inode);
1644 /* Undo 'indx_insert_entry'. */
1645 ni_lock_dir(dir_ni);
1646 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1647 le16_to_cpu(new_de->key_size), sbi);
1648 /* ni_unlock(dir_ni); will be called later. */
1651 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1655 run_deallocate(sbi, &ni->file.run, false);
1658 clear_rec_inuse(rec);
1660 ni->mi.dirty = false;
1661 discard_new_inode(inode);
1663 ntfs_mark_rec_free(sbi, ino);
1672 return ERR_PTR(err);
1675 unlock_new_inode(inode);
1680 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1683 struct ntfs_inode *ni = ntfs_i(inode);
1684 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1686 struct ATTR_FILE_NAME *de_name;
1688 /* Allocate PATH_MAX bytes. */
1693 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1694 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1696 /* Construct 'de'. */
1697 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1701 de_name = (struct ATTR_FILE_NAME *)(de + 1);
1702 /* Fill duplicate info. */
1703 de_name->dup.cr_time = de_name->dup.m_time = de_name->dup.c_time =
1704 de_name->dup.a_time = kernel2nt(&inode->i_ctime);
1705 de_name->dup.alloc_size = de_name->dup.data_size =
1706 cpu_to_le64(inode->i_size);
1707 de_name->dup.fa = ni->std_fa;
1708 de_name->dup.ea_size = de_name->dup.reparse = 0;
1710 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1719 * inode_operations::unlink
1720 * inode_operations::rmdir
1722 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1725 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1726 struct inode *inode = d_inode(dentry);
1727 struct ntfs_inode *ni = ntfs_i(inode);
1728 struct ntfs_inode *dir_ni = ntfs_i(dir);
1729 struct NTFS_DE *de, *de2 = NULL;
1732 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1735 /* Allocate PATH_MAX bytes. */
1742 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1747 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1752 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1756 dir->i_mtime = dir->i_ctime = current_time(dir);
1757 mark_inode_dirty(dir);
1758 inode->i_ctime = dir->i_ctime;
1760 mark_inode_dirty(inode);
1761 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1762 make_bad_inode(inode);
1763 ntfs_inode_err(inode, "failed to undo unlink");
1764 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1766 if (ni_is_dirty(dir))
1767 mark_inode_dirty(dir);
1768 if (ni_is_dirty(inode))
1769 mark_inode_dirty(inode);
1778 void ntfs_evict_inode(struct inode *inode)
1780 truncate_inode_pages_final(&inode->i_data);
1783 _ni_write_inode(inode, inode_needs_sync(inode));
1785 invalidate_inode_buffers(inode);
1788 ni_clear(ntfs_i(inode));
1791 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1794 int i, err = -EINVAL;
1795 struct ntfs_inode *ni = ntfs_i(inode);
1796 struct super_block *sb = inode->i_sb;
1797 struct ntfs_sb_info *sbi = sb->s_fs_info;
1800 void *to_free = NULL;
1801 struct REPARSE_DATA_BUFFER *rp;
1802 const __le16 *uname;
1803 struct ATTRIB *attr;
1805 /* Reparse data present. Try to parse it. */
1806 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1807 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1811 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1815 if (!attr->non_res) {
1816 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1819 size = le32_to_cpu(attr->res.data_size);
1821 size = le64_to_cpu(attr->nres.data_size);
1825 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1829 rp = kmalloc(size, GFP_NOFS);
1835 /* Read into temporal buffer. */
1836 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1841 /* Microsoft Tag. */
1842 switch (rp->ReparseTag) {
1843 case IO_REPARSE_TAG_MOUNT_POINT:
1844 /* Mount points and junctions. */
1845 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1846 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1847 MountPointReparseBuffer.PathBuffer))
1850 offsetof(struct REPARSE_DATA_BUFFER,
1851 MountPointReparseBuffer.PathBuffer) +
1852 le16_to_cpu(rp->MountPointReparseBuffer
1854 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1857 case IO_REPARSE_TAG_SYMLINK:
1858 /* FolderSymbolicLink */
1859 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1860 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1861 SymbolicLinkReparseBuffer.PathBuffer))
1864 rp, offsetof(struct REPARSE_DATA_BUFFER,
1865 SymbolicLinkReparseBuffer.PathBuffer) +
1866 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1869 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1872 case IO_REPARSE_TAG_CLOUD:
1873 case IO_REPARSE_TAG_CLOUD_1:
1874 case IO_REPARSE_TAG_CLOUD_2:
1875 case IO_REPARSE_TAG_CLOUD_3:
1876 case IO_REPARSE_TAG_CLOUD_4:
1877 case IO_REPARSE_TAG_CLOUD_5:
1878 case IO_REPARSE_TAG_CLOUD_6:
1879 case IO_REPARSE_TAG_CLOUD_7:
1880 case IO_REPARSE_TAG_CLOUD_8:
1881 case IO_REPARSE_TAG_CLOUD_9:
1882 case IO_REPARSE_TAG_CLOUD_A:
1883 case IO_REPARSE_TAG_CLOUD_B:
1884 case IO_REPARSE_TAG_CLOUD_C:
1885 case IO_REPARSE_TAG_CLOUD_D:
1886 case IO_REPARSE_TAG_CLOUD_E:
1887 case IO_REPARSE_TAG_CLOUD_F:
1888 err = sizeof("OneDrive") - 1;
1891 memcpy(buffer, "OneDrive", err);
1895 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1896 /* Unknown Microsoft Tag. */
1899 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1900 size <= sizeof(struct REPARSE_POINT)) {
1905 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1906 ulen = le16_to_cpu(rp->ReparseDataLength) -
1907 sizeof(struct REPARSE_POINT);
1910 /* Convert nlen from bytes to UNICODE chars. */
1913 /* Check that name is available. */
1914 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1917 /* If name is already zero terminated then truncate it now. */
1918 if (!uname[ulen - 1])
1921 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1926 /* Translate Windows '\' into Linux '/'. */
1927 for (i = 0; i < err; i++) {
1928 if (buffer[i] == '\\')
1932 /* Always set last zero. */
1939 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1940 struct delayed_call *done)
1946 return ERR_PTR(-ECHILD);
1948 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1950 return ERR_PTR(-ENOMEM);
1952 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1955 return ERR_PTR(err);
1958 set_delayed_call(done, kfree_link, ret);
1964 const struct inode_operations ntfs_link_inode_operations = {
1965 .get_link = ntfs_get_link,
1966 .setattr = ntfs3_setattr,
1967 .listxattr = ntfs_listxattr,
1968 .permission = ntfs_permission,
1971 const struct address_space_operations ntfs_aops = {
1972 .readpage = ntfs_readpage,
1973 .readahead = ntfs_readahead,
1974 .writepage = ntfs_writepage,
1975 .writepages = ntfs_writepages,
1976 .write_begin = ntfs_write_begin,
1977 .write_end = ntfs_write_end,
1978 .direct_IO = ntfs_direct_IO,
1980 .set_page_dirty = __set_page_dirty_buffers,
1983 const struct address_space_operations ntfs_aops_cmpr = {
1984 .readpage = ntfs_readpage,
1985 .readahead = ntfs_readahead,