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;
47 struct timespec64 ctime;
50 /* Setup 'uid' and 'gid' */
51 inode->i_uid = sbi->options->fs_uid;
52 inode->i_gid = sbi->options->fs_gid;
54 err = mi_init(&ni->mi, sbi, ino);
58 if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
59 t64 = sbi->mft.lbo >> sbi->cluster_bits;
60 t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
62 init_rwsem(&ni->file.run_lock);
64 if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
70 err = mi_read(&ni->mi, ino == MFT_REC_MFT);
77 if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
79 } else if (ref->seq != rec->seq) {
81 ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
82 le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
84 } else if (!is_rec_inuse(rec)) {
86 ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
90 if (le32_to_cpu(rec->total) != sbi->record_size) {
96 if (!is_rec_base(rec)) {
101 /* Record should contain $I30 root. */
102 is_dir = rec->flags & RECORD_FLAG_DIR;
104 /* MFT_REC_MFT is not a dir */
105 if (is_dir && ino == MFT_REC_MFT) {
110 inode->i_generation = le16_to_cpu(rec->seq);
112 /* Enumerate all struct Attributes MFT. */
117 * To reduce tab pressure use goto instead of
118 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
123 attr = ni_enum_attr_ex(ni, attr, &le, NULL);
128 /* This is non primary attribute segment. Ignore if not MFT. */
129 if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
133 asize = le32_to_cpu(attr->size);
134 goto attr_unpack_run;
137 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
138 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
139 asize = le32_to_cpu(attr->size);
142 * Really this check was done in 'ni_enum_attr_ex' -> ... 'mi_enum_attr'.
143 * There not critical to check this case again
145 if (attr->name_len &&
146 sizeof(short) * attr->name_len + le16_to_cpu(attr->name_off) >
151 t64 = le64_to_cpu(attr->nres.alloc_size);
152 if (le64_to_cpu(attr->nres.data_size) > t64 ||
153 le64_to_cpu(attr->nres.valid_size) > t64)
157 switch (attr->type) {
160 asize < sizeof(struct ATTR_STD_INFO) + roff ||
161 rsize < sizeof(struct ATTR_STD_INFO))
167 std5 = Add2Ptr(attr, roff);
170 nt2kernel(std5->cr_time, &ni->i_crtime);
172 nt2kernel(std5->a_time, &inode->i_atime);
173 ctime = inode_get_ctime(inode);
174 nt2kernel(std5->c_time, &ctime);
175 nt2kernel(std5->m_time, &inode->i_mtime);
177 ni->std_fa = std5->fa;
179 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
180 rsize >= sizeof(struct ATTR_STD_INFO5))
181 ni->std_security_id = std5->security_id;
185 if (attr->name_len || le || ino == MFT_REC_LOG)
188 err = ntfs_load_attr_list(ni, attr);
197 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
198 rsize < SIZEOF_ATTRIBUTE_FILENAME)
201 fname = Add2Ptr(attr, roff);
202 if (fname->type == FILE_NAME_DOS)
206 if (name && name->len == fname->name_len &&
207 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
215 /* Ignore data attribute in dir record. */
219 if (ino == MFT_REC_BADCLUST && !attr->non_res)
222 if (attr->name_len &&
223 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
224 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
225 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
226 (ino != MFT_REC_SECURE || !attr->non_res ||
227 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
228 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
229 /* File contains stream attribute. Ignore it. */
233 if (is_attr_sparsed(attr))
234 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
236 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
238 if (is_attr_compressed(attr))
239 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
241 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
243 if (is_attr_encrypted(attr))
244 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
246 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
248 if (!attr->non_res) {
249 ni->i_valid = inode->i_size = rsize;
250 inode_set_bytes(inode, rsize);
253 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
255 if (!attr->non_res) {
256 ni->ni_flags |= NI_FLAG_RESIDENT;
260 inode_set_bytes(inode, attr_ondisk_size(attr));
262 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
263 inode->i_size = le64_to_cpu(attr->nres.data_size);
264 if (!attr->nres.alloc_size)
267 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run :
275 root = Add2Ptr(attr, roff);
277 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
278 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
281 if (root->type != ATTR_NAME ||
282 root->rule != NTFS_COLLATION_TYPE_FILENAME)
289 ni->ni_flags |= NI_FLAG_DIR;
291 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
296 (S_IFDIR | (0777 & sbi->options->fs_dmask_inv)) :
301 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
302 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
305 inode->i_size = le64_to_cpu(attr->nres.data_size);
306 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
307 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
309 run = &ni->dir.alloc_run;
313 if (ino == MFT_REC_MFT) {
316 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
317 /* 0x20000000 = 2^32 / 8 */
318 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
321 run = &sbi->mft.bitmap.run;
323 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
324 !memcmp(attr_name(attr), I30_NAME,
327 run = &ni->dir.bitmap_run;
336 rp_fa = ni_parse_reparse(ni, attr, &rp);
341 * Assume one unicode symbol == one utf8.
343 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
347 ni->i_valid = inode->i_size;
349 /* Clear directory bit. */
350 if (ni->ni_flags & NI_FLAG_DIR) {
351 indx_clear(&ni->dir);
352 memset(&ni->dir, 0, sizeof(ni->dir));
353 ni->ni_flags &= ~NI_FLAG_DIR;
355 run_close(&ni->file.run);
357 mode = S_IFLNK | 0777;
361 goto attr_unpack_run; // Double break.
365 case REPARSE_COMPRESSED:
368 case REPARSE_DEDUPLICATED:
374 if (!attr->name_len &&
375 resident_data_ex(attr, sizeof(struct EA_INFO))) {
376 ni->ni_flags |= NI_FLAG_EA;
378 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
380 inode->i_mode = mode;
381 ntfs_get_wsl_perm(inode);
382 mode = inode->i_mode;
391 roff = le16_to_cpu(attr->nres.run_off);
398 t64 = le64_to_cpu(attr->nres.svcn);
400 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
401 t64, Add2Ptr(attr, roff), asize - roff);
412 if (!is_match && name) {
413 /* Reuse rec as buffer for ascii name. */
418 if (std5->fa & FILE_ATTRIBUTE_READONLY)
426 if (names != le16_to_cpu(rec->hard_links)) {
427 /* Correct minor error on the fly. Do not mark inode as dirty. */
428 rec->hard_links = cpu_to_le16(names);
432 set_nlink(inode, names);
435 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
438 * Dot and dot-dot should be included in count but was not
439 * included in enumeration.
440 * Usually a hard links to directories are disabled.
442 inode->i_op = &ntfs_dir_inode_operations;
443 inode->i_fop = &ntfs_dir_operations;
445 } else if (S_ISLNK(mode)) {
446 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
447 inode->i_op = &ntfs_link_inode_operations;
449 inode_nohighmem(inode);
450 } else if (S_ISREG(mode)) {
451 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
452 inode->i_op = &ntfs_file_inode_operations;
453 inode->i_fop = &ntfs_file_operations;
454 inode->i_mapping->a_ops = is_compressed(ni) ? &ntfs_aops_cmpr :
456 if (ino != MFT_REC_MFT)
457 init_rwsem(&ni->file.run_lock);
458 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
460 inode->i_op = &ntfs_special_inode_operations;
461 init_special_inode(inode, mode, inode->i_rdev);
462 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
463 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
464 /* Records in $Extend are not a files or general directories. */
465 inode->i_op = &ntfs_file_inode_operations;
471 if ((sbi->options->sys_immutable &&
472 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
473 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
474 inode->i_flags |= S_IMMUTABLE;
476 inode->i_flags &= ~S_IMMUTABLE;
479 inode->i_mode = mode;
480 if (!(ni->ni_flags & NI_FLAG_EA)) {
481 /* If no xattr then no security (stored in xattr). */
482 inode->i_flags |= S_NOSEC;
485 if (ino == MFT_REC_MFT && !sb->s_root)
488 unlock_new_inode(inode);
493 if (ino == MFT_REC_MFT && !sb->s_root)
503 * Return: 1 if match.
505 static int ntfs_test_inode(struct inode *inode, void *data)
507 struct MFT_REF *ref = data;
509 return ino_get(ref) == inode->i_ino;
512 static int ntfs_set_inode(struct inode *inode, void *data)
514 const struct MFT_REF *ref = data;
516 inode->i_ino = ino_get(ref);
520 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
521 const struct cpu_str *name)
525 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
527 if (unlikely(!inode))
528 return ERR_PTR(-ENOMEM);
530 /* If this is a freshly allocated inode, need to read it now. */
531 if (inode->i_state & I_NEW)
532 inode = ntfs_read_mft(inode, name, ref);
533 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
534 /* Inode overlaps? */
535 _ntfs_bad_inode(inode);
538 if (IS_ERR(inode) && name)
539 ntfs_set_state(sb->s_fs_info, NTFS_DIRTY_ERROR);
545 GET_BLOCK_GENERAL = 0,
546 GET_BLOCK_WRITE_BEGIN = 1,
547 GET_BLOCK_DIRECT_IO_R = 2,
548 GET_BLOCK_DIRECT_IO_W = 3,
552 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
553 struct buffer_head *bh, int create,
554 enum get_block_ctx ctx)
556 struct super_block *sb = inode->i_sb;
557 struct ntfs_sb_info *sbi = sb->s_fs_info;
558 struct ntfs_inode *ni = ntfs_i(inode);
559 struct folio *folio = bh->b_folio;
560 u8 cluster_bits = sbi->cluster_bits;
561 u32 block_size = sb->s_blocksize;
562 u64 bytes, lbo, valid;
568 /* Clear previous state. */
569 clear_buffer_new(bh);
570 clear_buffer_uptodate(bh);
572 if (is_resident(ni)) {
574 err = attr_data_read_resident(ni, &folio->page);
578 set_buffer_uptodate(bh);
579 bh->b_size = block_size;
583 vcn = vbo >> cluster_bits;
584 off = vbo & sbi->cluster_mask;
587 err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL,
588 create && sbi->cluster_size > PAGE_SIZE);
595 bytes = ((u64)len << cluster_bits) - off;
597 if (lcn == SPARSE_LCN) {
599 if (bh->b_size > bytes)
609 lbo = ((u64)lcn << cluster_bits) + off;
611 set_buffer_mapped(bh);
612 bh->b_bdev = sb->s_bdev;
613 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
617 if (ctx == GET_BLOCK_DIRECT_IO_W) {
618 /* ntfs_direct_IO will update ni->i_valid. */
623 if (bytes > bh->b_size)
629 if (vbo + bytes > valid) {
630 ni->i_valid = vbo + bytes;
631 mark_inode_dirty(inode);
633 } else if (vbo >= valid) {
634 /* Read out of valid data. */
635 clear_buffer_mapped(bh);
636 } else if (vbo + bytes <= valid) {
638 } else if (vbo + block_size <= valid) {
639 /* Normal short read. */
643 * Read across valid size: vbo < valid && valid < vbo + block_size
648 u32 voff = valid - vbo;
650 bh->b_size = block_size;
651 off = vbo & (PAGE_SIZE - 1);
652 folio_set_bh(bh, folio, off);
654 err = bh_read(bh, 0);
657 folio_zero_segment(folio, off + voff, off + block_size);
661 if (bh->b_size > bytes)
665 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
666 static_assert(sizeof(size_t) < sizeof(loff_t));
667 if (bytes > 0x40000000u)
668 bh->b_size = 0x40000000u;
678 int ntfs_get_block(struct inode *inode, sector_t vbn,
679 struct buffer_head *bh_result, int create)
681 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
682 bh_result, create, GET_BLOCK_GENERAL);
685 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
686 struct buffer_head *bh_result, int create)
688 return ntfs_get_block_vbo(inode,
689 (u64)vsn << inode->i_sb->s_blocksize_bits,
690 bh_result, create, GET_BLOCK_BMAP);
693 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
695 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
698 static int ntfs_read_folio(struct file *file, struct folio *folio)
700 struct page *page = &folio->page;
702 struct address_space *mapping = page->mapping;
703 struct inode *inode = mapping->host;
704 struct ntfs_inode *ni = ntfs_i(inode);
706 if (is_resident(ni)) {
708 err = attr_data_read_resident(ni, page);
710 if (err != E_NTFS_NONRESIDENT) {
716 if (is_compressed(ni)) {
718 err = ni_readpage_cmpr(ni, page);
723 /* Normal + sparse files. */
724 return mpage_read_folio(folio, ntfs_get_block);
727 static void ntfs_readahead(struct readahead_control *rac)
729 struct address_space *mapping = rac->mapping;
730 struct inode *inode = mapping->host;
731 struct ntfs_inode *ni = ntfs_i(inode);
735 if (is_resident(ni)) {
736 /* No readahead for resident. */
740 if (is_compressed(ni)) {
741 /* No readahead for compressed. */
746 pos = readahead_pos(rac);
748 if (valid < i_size_read(inode) && pos <= valid &&
749 valid < pos + readahead_length(rac)) {
750 /* Range cross 'valid'. Read it page by page. */
754 mpage_readahead(rac, ntfs_get_block);
757 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
758 struct buffer_head *bh_result, int create)
760 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
761 bh_result, create, GET_BLOCK_DIRECT_IO_R);
764 static int ntfs_get_block_direct_IO_W(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_W);
771 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
773 struct file *file = iocb->ki_filp;
774 struct address_space *mapping = file->f_mapping;
775 struct inode *inode = mapping->host;
776 struct ntfs_inode *ni = ntfs_i(inode);
777 loff_t vbo = iocb->ki_pos;
779 int wr = iov_iter_rw(iter) & WRITE;
780 size_t iter_count = iov_iter_count(iter);
784 if (is_resident(ni)) {
785 /* Switch to buffered write. */
790 ret = blockdev_direct_IO(iocb, inode, iter,
791 wr ? ntfs_get_block_direct_IO_W :
792 ntfs_get_block_direct_IO_R);
796 else if (wr && ret == -EIOCBQUEUED)
797 end = vbo + iter_count;
803 if (end > valid && !S_ISBLK(inode->i_mode)) {
805 mark_inode_dirty(inode);
807 } else if (vbo < valid && valid < end) {
809 iov_iter_revert(iter, end - valid);
810 iov_iter_zero(end - valid, iter);
817 int ntfs_set_size(struct inode *inode, u64 new_size)
819 struct super_block *sb = inode->i_sb;
820 struct ntfs_sb_info *sbi = sb->s_fs_info;
821 struct ntfs_inode *ni = ntfs_i(inode);
824 /* Check for maximum file size. */
825 if (is_sparsed(ni) || is_compressed(ni)) {
826 if (new_size > sbi->maxbytes_sparse) {
830 } else if (new_size > sbi->maxbytes) {
836 down_write(&ni->file.run_lock);
838 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
839 &ni->i_valid, true, NULL);
841 up_write(&ni->file.run_lock);
844 mark_inode_dirty(inode);
850 static int ntfs_resident_writepage(struct folio *folio,
851 struct writeback_control *wbc, void *data)
853 struct address_space *mapping = data;
854 struct ntfs_inode *ni = ntfs_i(mapping->host);
858 ret = attr_data_write_resident(ni, &folio->page);
861 if (ret != E_NTFS_NONRESIDENT)
863 mapping_set_error(mapping, ret);
867 static int ntfs_writepages(struct address_space *mapping,
868 struct writeback_control *wbc)
870 if (is_resident(ntfs_i(mapping->host)))
871 return write_cache_pages(mapping, wbc, ntfs_resident_writepage,
873 return mpage_writepages(mapping, wbc, ntfs_get_block);
876 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
877 struct buffer_head *bh_result, int create)
879 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
880 bh_result, create, GET_BLOCK_WRITE_BEGIN);
883 int ntfs_write_begin(struct file *file, struct address_space *mapping,
884 loff_t pos, u32 len, struct page **pagep, void **fsdata)
887 struct inode *inode = mapping->host;
888 struct ntfs_inode *ni = ntfs_i(inode);
891 if (is_resident(ni)) {
893 grab_cache_page_write_begin(mapping, pos >> PAGE_SHIFT);
901 err = attr_data_read_resident(ni, page);
911 if (err != E_NTFS_NONRESIDENT)
915 err = block_write_begin(mapping, pos, len, pagep,
916 ntfs_get_block_write_begin);
923 * ntfs_write_end - Address_space_operations::write_end.
925 int ntfs_write_end(struct file *file, struct address_space *mapping, loff_t pos,
926 u32 len, u32 copied, struct page *page, void *fsdata)
928 struct inode *inode = mapping->host;
929 struct ntfs_inode *ni = ntfs_i(inode);
930 u64 valid = ni->i_valid;
934 if (is_resident(ni)) {
936 err = attr_data_write_resident(ni, page);
940 /* Clear any buffers in page. */
941 if (page_has_buffers(page)) {
942 struct buffer_head *head, *bh;
944 bh = head = page_buffers(page);
946 clear_buffer_dirty(bh);
947 clear_buffer_mapped(bh);
948 set_buffer_uptodate(bh);
949 } while (head != (bh = bh->b_this_page));
951 SetPageUptodate(page);
957 err = generic_write_end(file, mapping, pos, len, copied, page,
962 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
963 inode->i_mtime = inode_set_ctime_current(inode);
964 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
968 if (valid != ni->i_valid) {
969 /* ni->i_valid is changed in ntfs_get_block_vbo. */
973 if (pos + err > inode->i_size) {
974 inode->i_size = pos + err;
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);
1190 * Helper function for:
1195 * - ntfs_atomic_open
1197 * NOTE: if fnd != NULL (ntfs_atomic_open) then @dir is locked
1199 struct inode *ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir,
1200 struct dentry *dentry,
1201 const struct cpu_str *uni, umode_t mode,
1202 dev_t dev, const char *symname, u32 size,
1203 struct ntfs_fnd *fnd)
1206 struct super_block *sb = dir->i_sb;
1207 struct ntfs_sb_info *sbi = sb->s_fs_info;
1208 const struct qstr *name = &dentry->d_name;
1210 struct ntfs_inode *dir_ni = ntfs_i(dir);
1211 struct ntfs_inode *ni = NULL;
1212 struct inode *inode = NULL;
1213 struct ATTRIB *attr;
1214 struct ATTR_STD_INFO5 *std5;
1215 struct ATTR_FILE_NAME *fname;
1216 struct MFT_REC *rec;
1217 u32 asize, dsize, sd_size;
1218 enum FILE_ATTRIBUTE fa;
1219 __le32 security_id = SECURITY_ID_INVALID;
1222 u16 t16, nsize = 0, aid = 0;
1223 struct INDEX_ROOT *root, *dir_root;
1224 struct NTFS_DE *e, *new_de = NULL;
1225 struct REPARSE_DATA_BUFFER *rp = NULL;
1226 bool rp_inserted = false;
1229 ni_lock_dir(dir_ni);
1231 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1237 if (S_ISDIR(mode)) {
1238 /* Use parent's directory attributes. */
1239 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1240 FILE_ATTRIBUTE_ARCHIVE;
1242 * By default child directory inherits parent attributes.
1243 * Root directory is hidden + system.
1244 * Make an exception for children in root.
1246 if (dir->i_ino == MFT_REC_ROOT)
1247 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1248 } else if (S_ISLNK(mode)) {
1249 /* It is good idea that link should be the same type (file/dir) as target */
1250 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1253 * Linux: there are dir/file/symlink and so on.
1254 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1255 * It is good idea to create:
1256 * dir + reparse if 'symname' points to directory
1258 * file + reparse if 'symname' points to file
1259 * Unfortunately kern_path hangs if symname contains 'dir'.
1265 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1266 * struct inode *target = d_inode(path.dentry);
1268 * if (S_ISDIR(target->i_mode))
1269 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1270 * // if ( target->i_sb == sb ){
1271 * // use relative path?
1276 } else if (S_ISREG(mode)) {
1277 if (sbi->options->sparse) {
1278 /* Sparsed regular file, cause option 'sparse'. */
1279 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1280 FILE_ATTRIBUTE_ARCHIVE;
1281 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1282 /* Compressed regular file, if parent is compressed. */
1283 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1285 /* Regular file, default attributes. */
1286 fa = FILE_ATTRIBUTE_ARCHIVE;
1289 fa = FILE_ATTRIBUTE_ARCHIVE;
1292 /* If option "hide_dot_files" then set hidden attribute for dot files. */
1293 if (sbi->options->hide_dot_files && name->name[0] == '.')
1294 fa |= FILE_ATTRIBUTE_HIDDEN;
1297 fa |= FILE_ATTRIBUTE_READONLY;
1299 /* Allocate PATH_MAX bytes. */
1300 new_de = __getname();
1306 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1307 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1309 /* Step 1: allocate and fill new mft record. */
1310 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1314 ni = ntfs_new_inode(sbi, ino, S_ISDIR(mode) ? RECORD_FLAG_DIR : 0);
1320 inode = &ni->vfs_inode;
1321 inode_init_owner(idmap, inode, dir, mode);
1322 mode = inode->i_mode;
1324 ni->i_crtime = current_time(inode);
1327 rec->hard_links = cpu_to_le16(1);
1328 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1330 /* Get default security id. */
1331 sd = s_default_security;
1332 sd_size = sizeof(s_default_security);
1334 if (is_ntfs3(sbi)) {
1335 security_id = dir_ni->std_security_id;
1336 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1337 security_id = sbi->security.def_security_id;
1339 if (security_id == SECURITY_ID_INVALID &&
1340 !ntfs_insert_security(sbi, sd, sd_size,
1341 &security_id, NULL))
1342 sbi->security.def_security_id = security_id;
1346 /* Insert standard info. */
1347 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1349 if (security_id == SECURITY_ID_INVALID) {
1350 dsize = sizeof(struct ATTR_STD_INFO);
1352 dsize = sizeof(struct ATTR_STD_INFO5);
1353 std5->security_id = security_id;
1354 ni->std_security_id = security_id;
1356 asize = SIZEOF_RESIDENT + dsize;
1358 attr->type = ATTR_STD;
1359 attr->size = cpu_to_le32(asize);
1360 attr->id = cpu_to_le16(aid++);
1361 attr->res.data_off = SIZEOF_RESIDENT_LE;
1362 attr->res.data_size = cpu_to_le32(dsize);
1364 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1365 kernel2nt(&ni->i_crtime);
1367 std5->fa = ni->std_fa = fa;
1369 attr = Add2Ptr(attr, asize);
1371 /* Insert file name. */
1372 err = fill_name_de(sbi, new_de, name, uni);
1376 mi_get_ref(&ni->mi, &new_de->ref);
1378 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1380 if (sbi->options->windows_names &&
1381 !valid_windows_name(sbi, (struct le_str *)&fname->name_len)) {
1386 mi_get_ref(&dir_ni->mi, &fname->home);
1387 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1388 fname->dup.a_time = std5->cr_time;
1389 fname->dup.alloc_size = fname->dup.data_size = 0;
1390 fname->dup.fa = std5->fa;
1391 fname->dup.ea_size = fname->dup.reparse = 0;
1393 dsize = le16_to_cpu(new_de->key_size);
1394 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1396 attr->type = ATTR_NAME;
1397 attr->size = cpu_to_le32(asize);
1398 attr->res.data_off = SIZEOF_RESIDENT_LE;
1399 attr->res.flags = RESIDENT_FLAG_INDEXED;
1400 attr->id = cpu_to_le16(aid++);
1401 attr->res.data_size = cpu_to_le32(dsize);
1402 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1404 attr = Add2Ptr(attr, asize);
1406 if (security_id == SECURITY_ID_INVALID) {
1407 /* Insert security attribute. */
1408 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1410 attr->type = ATTR_SECURE;
1411 attr->size = cpu_to_le32(asize);
1412 attr->id = cpu_to_le16(aid++);
1413 attr->res.data_off = SIZEOF_RESIDENT_LE;
1414 attr->res.data_size = cpu_to_le32(sd_size);
1415 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1417 attr = Add2Ptr(attr, asize);
1420 attr->id = cpu_to_le16(aid++);
1421 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1423 * Regular directory or symlink to directory.
1424 * Create root attribute.
1426 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1427 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1429 attr->type = ATTR_ROOT;
1430 attr->size = cpu_to_le32(asize);
1432 attr->name_len = ARRAY_SIZE(I30_NAME);
1433 attr->name_off = SIZEOF_RESIDENT_LE;
1434 attr->res.data_off =
1435 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1436 attr->res.data_size = cpu_to_le32(dsize);
1437 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1440 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1441 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1442 root->ihdr.de_off = cpu_to_le32(sizeof(struct INDEX_HDR));
1443 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1444 sizeof(struct NTFS_DE));
1445 root->ihdr.total = root->ihdr.used;
1447 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1448 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1449 e->flags = NTFS_IE_LAST;
1450 } else if (S_ISLNK(mode)) {
1453 * Create empty resident data attribute.
1455 asize = SIZEOF_RESIDENT;
1457 /* Insert empty ATTR_DATA */
1458 attr->type = ATTR_DATA;
1459 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1460 attr->name_off = SIZEOF_RESIDENT_LE;
1461 attr->res.data_off = SIZEOF_RESIDENT_LE;
1462 } else if (S_ISREG(mode)) {
1464 * Regular file. Create empty non resident data attribute.
1466 attr->type = ATTR_DATA;
1468 attr->nres.evcn = cpu_to_le64(-1ll);
1469 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1470 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1471 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1472 attr->flags = ATTR_FLAG_SPARSED;
1473 asize = SIZEOF_NONRESIDENT_EX + 8;
1474 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1475 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1476 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1477 attr->flags = ATTR_FLAG_COMPRESSED;
1478 attr->nres.c_unit = COMPRESSION_UNIT;
1479 asize = SIZEOF_NONRESIDENT_EX + 8;
1481 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1482 attr->name_off = SIZEOF_NONRESIDENT_LE;
1483 asize = SIZEOF_NONRESIDENT + 8;
1485 attr->nres.run_off = attr->name_off;
1488 * Node. Create empty resident data attribute.
1490 attr->type = ATTR_DATA;
1491 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1492 attr->name_off = SIZEOF_RESIDENT_LE;
1493 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1494 attr->flags = ATTR_FLAG_SPARSED;
1495 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1496 attr->flags = ATTR_FLAG_COMPRESSED;
1497 attr->res.data_off = SIZEOF_RESIDENT_LE;
1498 asize = SIZEOF_RESIDENT;
1499 ni->ni_flags |= NI_FLAG_RESIDENT;
1502 if (S_ISDIR(mode)) {
1503 ni->ni_flags |= NI_FLAG_DIR;
1504 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1507 } else if (S_ISLNK(mode)) {
1508 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1517 * Insert ATTR_REPARSE.
1519 attr = Add2Ptr(attr, asize);
1520 attr->type = ATTR_REPARSE;
1521 attr->id = cpu_to_le16(aid++);
1523 /* Resident or non resident? */
1524 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1525 t16 = PtrOffset(rec, attr);
1528 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1529 * It is good idea to keep extened attributes resident.
1531 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1533 CLST clst = bytes_to_cluster(sbi, nsize);
1535 /* Bytes per runs. */
1536 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1539 attr->nres.evcn = cpu_to_le64(clst - 1);
1540 attr->name_off = SIZEOF_NONRESIDENT_LE;
1541 attr->nres.run_off = attr->name_off;
1542 attr->nres.data_size = cpu_to_le64(nsize);
1543 attr->nres.valid_size = attr->nres.data_size;
1544 attr->nres.alloc_size =
1545 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1547 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1548 clst, NULL, ALLOCATE_DEF,
1549 &alen, 0, NULL, NULL);
1553 err = run_pack(&ni->file.run, 0, clst,
1554 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1564 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1565 /* Write non resident data. */
1566 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp,
1571 attr->res.data_off = SIZEOF_RESIDENT_LE;
1572 attr->res.data_size = cpu_to_le32(nsize);
1573 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1575 /* Size of symlink equals the length of input string. */
1576 inode->i_size = size;
1578 attr->size = cpu_to_le32(asize);
1580 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1588 attr = Add2Ptr(attr, asize);
1589 attr->type = ATTR_END;
1591 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1592 rec->next_attr_id = cpu_to_le16(aid);
1594 inode->i_generation = le16_to_cpu(rec->seq);
1596 if (S_ISDIR(mode)) {
1597 inode->i_op = &ntfs_dir_inode_operations;
1598 inode->i_fop = &ntfs_dir_operations;
1599 } else if (S_ISLNK(mode)) {
1600 inode->i_op = &ntfs_link_inode_operations;
1601 inode->i_fop = NULL;
1602 inode->i_mapping->a_ops = &ntfs_aops;
1603 inode->i_size = size;
1604 inode_nohighmem(inode);
1605 } else if (S_ISREG(mode)) {
1606 inode->i_op = &ntfs_file_inode_operations;
1607 inode->i_fop = &ntfs_file_operations;
1608 inode->i_mapping->a_ops = is_compressed(ni) ? &ntfs_aops_cmpr :
1610 init_rwsem(&ni->file.run_lock);
1612 inode->i_op = &ntfs_special_inode_operations;
1613 init_special_inode(inode, mode, dev);
1616 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1617 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1618 err = ntfs_init_acl(idmap, inode, dir);
1624 inode->i_flags |= S_NOSEC;
1628 * ntfs_init_acl and ntfs_save_wsl_perm update extended attribute.
1629 * The packed size of extended attribute is stored in direntry too.
1630 * 'fname' here points to inside new_de.
1632 ntfs_save_wsl_perm(inode, &fname->dup.ea_size);
1635 * update ea_size in file_name attribute too.
1636 * Use ni_find_attr cause layout of MFT record may be changed
1637 * in ntfs_init_acl and ntfs_save_wsl_perm.
1639 attr = ni_find_attr(ni, NULL, NULL, ATTR_NAME, NULL, 0, NULL, NULL);
1641 struct ATTR_FILE_NAME *fn;
1643 fn = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1645 fn->dup.ea_size = fname->dup.ea_size;
1648 /* We do not need to update parent directory later */
1649 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
1651 /* Step 2: Add new name in index. */
1652 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1657 * Call 'd_instantiate' after inode->i_op is set
1658 * but before finish_open.
1660 d_instantiate(dentry, inode);
1662 /* Set original time. inode times (i_ctime) may be changed in ntfs_init_acl. */
1663 inode->i_atime = inode->i_mtime = inode_set_ctime_to_ts(inode, ni->i_crtime);
1664 dir->i_mtime = inode_set_ctime_to_ts(dir, ni->i_crtime);
1666 mark_inode_dirty(dir);
1667 mark_inode_dirty(inode);
1674 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1678 run_deallocate(sbi, &ni->file.run, false);
1681 clear_rec_inuse(rec);
1683 ni->mi.dirty = false;
1684 discard_new_inode(inode);
1686 ntfs_mark_rec_free(sbi, ino, false);
1697 return ERR_PTR(err);
1699 unlock_new_inode(inode);
1704 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1707 struct ntfs_inode *ni = ntfs_i(inode);
1708 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1711 /* Allocate PATH_MAX bytes. */
1716 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1717 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1719 /* Construct 'de'. */
1720 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1724 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1733 * inode_operations::unlink
1734 * inode_operations::rmdir
1736 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1739 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1740 struct inode *inode = d_inode(dentry);
1741 struct ntfs_inode *ni = ntfs_i(inode);
1742 struct ntfs_inode *dir_ni = ntfs_i(dir);
1743 struct NTFS_DE *de, *de2 = NULL;
1746 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1749 /* Allocate PATH_MAX bytes. */
1756 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1761 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1766 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1770 dir->i_mtime = inode_set_ctime_current(dir);
1771 mark_inode_dirty(dir);
1772 inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
1774 mark_inode_dirty(inode);
1775 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1776 _ntfs_bad_inode(inode);
1778 if (ni_is_dirty(dir))
1779 mark_inode_dirty(dir);
1780 if (ni_is_dirty(inode))
1781 mark_inode_dirty(inode);
1790 void ntfs_evict_inode(struct inode *inode)
1792 truncate_inode_pages_final(&inode->i_data);
1794 invalidate_inode_buffers(inode);
1797 ni_clear(ntfs_i(inode));
1801 * ntfs_translate_junction
1803 * Translate a Windows junction target to the Linux equivalent.
1804 * On junctions, targets are always absolute (they include the drive
1805 * letter). We have no way of knowing if the target is for the current
1806 * mounted device or not so we just assume it is.
1808 static int ntfs_translate_junction(const struct super_block *sb,
1809 const struct dentry *link_de, char *target,
1810 int target_len, int target_max)
1812 int tl_len, err = target_len;
1813 char *link_path_buffer = NULL, *link_path;
1814 char *translated = NULL;
1818 link_path_buffer = kmalloc(PATH_MAX, GFP_NOFS);
1819 if (!link_path_buffer) {
1823 /* Get link path, relative to mount point */
1824 link_path = dentry_path_raw(link_de, link_path_buffer, PATH_MAX);
1825 if (IS_ERR(link_path)) {
1826 ntfs_err(sb, "Error getting link path");
1831 translated = kmalloc(PATH_MAX, GFP_NOFS);
1837 /* Make translated path a relative path to mount point */
1838 strcpy(translated, "./");
1839 ++link_path; /* Skip leading / */
1840 for (tl_len = sizeof("./") - 1; *link_path; ++link_path) {
1841 if (*link_path == '/') {
1842 if (PATH_MAX - tl_len < sizeof("../")) {
1844 "Link path %s has too many components",
1849 strcpy(translated + tl_len, "../");
1850 tl_len += sizeof("../") - 1;
1854 /* Skip drive letter */
1855 target_start = target;
1856 while (*target_start && *target_start != ':')
1859 if (!*target_start) {
1860 ntfs_err(sb, "Link target (%s) missing drive separator",
1866 /* Skip drive separator and leading /, if exists */
1867 target_start += 1 + (target_start[1] == '/');
1868 copy_len = target_len - (target_start - target);
1870 if (PATH_MAX - tl_len <= copy_len) {
1871 ntfs_err(sb, "Link target %s too large for buffer (%d <= %d)",
1872 target_start, PATH_MAX - tl_len, copy_len);
1877 /* translated path has a trailing / and target_start does not */
1878 strcpy(translated + tl_len, target_start);
1880 if (target_max <= tl_len) {
1881 ntfs_err(sb, "Target path %s too large for buffer (%d <= %d)",
1882 translated, target_max, tl_len);
1886 strcpy(target, translated);
1890 kfree(link_path_buffer);
1895 static noinline int ntfs_readlink_hlp(const struct dentry *link_de,
1896 struct inode *inode, char *buffer,
1899 int i, err = -EINVAL;
1900 struct ntfs_inode *ni = ntfs_i(inode);
1901 struct super_block *sb = inode->i_sb;
1902 struct ntfs_sb_info *sbi = sb->s_fs_info;
1905 void *to_free = NULL;
1906 struct REPARSE_DATA_BUFFER *rp;
1907 const __le16 *uname;
1908 struct ATTRIB *attr;
1910 /* Reparse data present. Try to parse it. */
1911 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1912 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1916 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1920 if (!attr->non_res) {
1921 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1924 size = le32_to_cpu(attr->res.data_size);
1926 size = le64_to_cpu(attr->nres.data_size);
1930 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1934 rp = kmalloc(size, GFP_NOFS);
1940 /* Read into temporal buffer. */
1941 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1946 /* Microsoft Tag. */
1947 switch (rp->ReparseTag) {
1948 case IO_REPARSE_TAG_MOUNT_POINT:
1949 /* Mount points and junctions. */
1950 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1951 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1952 MountPointReparseBuffer.PathBuffer))
1955 offsetof(struct REPARSE_DATA_BUFFER,
1956 MountPointReparseBuffer.PathBuffer) +
1957 le16_to_cpu(rp->MountPointReparseBuffer
1959 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1962 case IO_REPARSE_TAG_SYMLINK:
1963 /* FolderSymbolicLink */
1964 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1965 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1966 SymbolicLinkReparseBuffer.PathBuffer))
1969 rp, offsetof(struct REPARSE_DATA_BUFFER,
1970 SymbolicLinkReparseBuffer.PathBuffer) +
1971 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1974 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1977 case IO_REPARSE_TAG_CLOUD:
1978 case IO_REPARSE_TAG_CLOUD_1:
1979 case IO_REPARSE_TAG_CLOUD_2:
1980 case IO_REPARSE_TAG_CLOUD_3:
1981 case IO_REPARSE_TAG_CLOUD_4:
1982 case IO_REPARSE_TAG_CLOUD_5:
1983 case IO_REPARSE_TAG_CLOUD_6:
1984 case IO_REPARSE_TAG_CLOUD_7:
1985 case IO_REPARSE_TAG_CLOUD_8:
1986 case IO_REPARSE_TAG_CLOUD_9:
1987 case IO_REPARSE_TAG_CLOUD_A:
1988 case IO_REPARSE_TAG_CLOUD_B:
1989 case IO_REPARSE_TAG_CLOUD_C:
1990 case IO_REPARSE_TAG_CLOUD_D:
1991 case IO_REPARSE_TAG_CLOUD_E:
1992 case IO_REPARSE_TAG_CLOUD_F:
1993 err = sizeof("OneDrive") - 1;
1996 memcpy(buffer, "OneDrive", err);
2000 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
2001 /* Unknown Microsoft Tag. */
2004 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
2005 size <= sizeof(struct REPARSE_POINT)) {
2010 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
2011 ulen = le16_to_cpu(rp->ReparseDataLength) -
2012 sizeof(struct REPARSE_POINT);
2015 /* Convert nlen from bytes to UNICODE chars. */
2018 /* Check that name is available. */
2019 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
2022 /* If name is already zero terminated then truncate it now. */
2023 if (!uname[ulen - 1])
2026 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
2031 /* Translate Windows '\' into Linux '/'. */
2032 for (i = 0; i < err; i++) {
2033 if (buffer[i] == '\\')
2037 /* Always set last zero. */
2040 /* If this is a junction, translate the link target. */
2041 if (rp->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
2042 err = ntfs_translate_junction(sb, link_de, buffer, err, buflen);
2049 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
2050 struct delayed_call *done)
2056 return ERR_PTR(-ECHILD);
2058 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
2060 return ERR_PTR(-ENOMEM);
2062 err = ntfs_readlink_hlp(de, inode, ret, PAGE_SIZE);
2065 return ERR_PTR(err);
2068 set_delayed_call(done, kfree_link, ret);
2074 const struct inode_operations ntfs_link_inode_operations = {
2075 .get_link = ntfs_get_link,
2076 .setattr = ntfs3_setattr,
2077 .listxattr = ntfs_listxattr,
2080 const struct address_space_operations ntfs_aops = {
2081 .read_folio = ntfs_read_folio,
2082 .readahead = ntfs_readahead,
2083 .writepages = ntfs_writepages,
2084 .write_begin = ntfs_write_begin,
2085 .write_end = ntfs_write_end,
2086 .direct_IO = ntfs_direct_IO,
2088 .dirty_folio = block_dirty_folio,
2089 .migrate_folio = buffer_migrate_folio,
2090 .invalidate_folio = block_invalidate_folio,
2093 const struct address_space_operations ntfs_aops_cmpr = {
2094 .read_folio = ntfs_read_folio,
2095 .readahead = ntfs_readahead,