1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/blkdev.h>
9 #include <linux/buffer_head.h>
11 #include <linux/iversion.h>
12 #include <linux/mpage.h>
13 #include <linux/namei.h>
14 #include <linux/nls.h>
15 #include <linux/uio.h>
16 #include <linux/writeback.h>
25 * reads record and parses MFT
27 static struct inode *ntfs_read_mft(struct inode *inode,
28 const struct cpu_str *name,
29 const struct MFT_REF *ref)
32 struct ntfs_inode *ni = ntfs_i(inode);
33 struct super_block *sb = inode->i_sb;
34 struct ntfs_sb_info *sbi = sb->s_fs_info;
36 struct ATTR_STD_INFO5 *std5 = NULL;
37 struct ATTR_LIST_ENTRY *le;
39 bool is_match = false;
42 unsigned long ino = inode->i_ino;
43 u32 rp_fa = 0, asize, t32;
44 u16 roff, rsize, names = 0;
45 const struct ATTR_FILE_NAME *fname = NULL;
46 const struct INDEX_ROOT *root;
47 struct REPARSE_DATA_BUFFER rp; // 0x18 bytes
50 struct runs_tree *run;
53 /* Setup 'uid' and 'gid' */
54 inode->i_uid = sbi->options.fs_uid;
55 inode->i_gid = sbi->options.fs_gid;
57 err = mi_init(&ni->mi, sbi, ino);
61 if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
62 t64 = sbi->mft.lbo >> sbi->cluster_bits;
63 t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
65 init_rwsem(&ni->file.run_lock);
67 if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
73 err = mi_read(&ni->mi, ino == MFT_REC_MFT);
80 if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
82 } else if (ref->seq != rec->seq) {
84 ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
85 le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
87 } else if (!is_rec_inuse(rec)) {
89 ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
93 if (le32_to_cpu(rec->total) != sbi->record_size) {
99 if (!is_rec_base(rec))
102 /* record should contain $I30 root */
103 is_dir = rec->flags & RECORD_FLAG_DIR;
105 inode->i_generation = le16_to_cpu(rec->seq);
107 /* Enumerate all struct Attributes MFT */
112 * to reduce tab pressure use goto instead of
113 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
118 attr = ni_enum_attr_ex(ni, attr, &le, NULL);
123 /* This is non primary attribute segment. Ignore if not MFT */
124 if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
128 asize = le32_to_cpu(attr->size);
129 goto attr_unpack_run;
132 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
133 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
134 asize = le32_to_cpu(attr->size);
136 switch (attr->type) {
139 asize < sizeof(struct ATTR_STD_INFO) + roff ||
140 rsize < sizeof(struct ATTR_STD_INFO))
146 std5 = Add2Ptr(attr, roff);
149 nt2kernel(std5->cr_time, &ni->i_crtime);
151 nt2kernel(std5->a_time, &inode->i_atime);
152 nt2kernel(std5->c_time, &inode->i_ctime);
153 nt2kernel(std5->m_time, &inode->i_mtime);
155 ni->std_fa = std5->fa;
157 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
158 rsize >= sizeof(struct ATTR_STD_INFO5))
159 ni->std_security_id = std5->security_id;
163 if (attr->name_len || le || ino == MFT_REC_LOG)
166 err = ntfs_load_attr_list(ni, attr);
175 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
176 rsize < SIZEOF_ATTRIBUTE_FILENAME)
179 fname = Add2Ptr(attr, roff);
180 if (fname->type == FILE_NAME_DOS)
184 if (name && name->len == fname->name_len &&
185 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
193 /* ignore data attribute in dir record */
197 if (ino == MFT_REC_BADCLUST && !attr->non_res)
200 if (attr->name_len &&
201 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
202 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
203 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
204 (ino != MFT_REC_SECURE || !attr->non_res ||
205 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
206 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
207 /* file contains stream attribute. ignore it */
211 if (is_attr_sparsed(attr))
212 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
214 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
216 if (is_attr_compressed(attr))
217 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
219 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
221 if (is_attr_encrypted(attr))
222 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
224 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
226 if (!attr->non_res) {
227 ni->i_valid = inode->i_size = rsize;
228 inode_set_bytes(inode, rsize);
231 t32 = le16_to_cpu(attr->nres.run_off);
234 mode = S_IFREG | (0777 & sbi->options.fs_fmask_inv);
236 if (!attr->non_res) {
237 ni->ni_flags |= NI_FLAG_RESIDENT;
241 inode_set_bytes(inode, attr_ondisk_size(attr));
243 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
244 inode->i_size = le64_to_cpu(attr->nres.data_size);
245 if (!attr->nres.alloc_size)
248 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
256 root = Add2Ptr(attr, roff);
259 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
260 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
263 if (root->type != ATTR_NAME ||
264 root->rule != NTFS_COLLATION_TYPE_FILENAME)
270 ni->ni_flags |= NI_FLAG_DIR;
272 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
277 ? (S_IFDIR | (0777 & sbi->options.fs_dmask_inv))
282 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
283 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
286 inode->i_size = le64_to_cpu(attr->nres.data_size);
287 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
288 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
290 run = &ni->dir.alloc_run;
294 if (ino == MFT_REC_MFT) {
297 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
298 /* 0x20000000 = 2^32 / 8 */
299 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
302 run = &sbi->mft.bitmap.run;
304 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
305 !memcmp(attr_name(attr), I30_NAME,
308 run = &ni->dir.bitmap_run;
317 rp_fa = ni_parse_reparse(ni, attr, &rp);
320 if (!attr->non_res) {
321 inode->i_size = rsize;
322 inode_set_bytes(inode, rsize);
326 le64_to_cpu(attr->nres.data_size);
327 t32 = le16_to_cpu(attr->nres.run_off);
330 /* Looks like normal symlink */
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);
377 t64 = le64_to_cpu(attr->nres.svcn);
378 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
379 t64, Add2Ptr(attr, roff), asize - roff);
390 if (!is_match && name) {
391 /* reuse rec as buffer for ascii name */
396 if (std5->fa & FILE_ATTRIBUTE_READONLY)
404 set_nlink(inode, names);
407 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
410 * dot and dot-dot should be included in count but was not
411 * included in enumeration.
412 * Usually a hard links to directories are disabled
414 inode->i_op = &ntfs_dir_inode_operations;
415 inode->i_fop = &ntfs_dir_operations;
417 } else if (S_ISLNK(mode)) {
418 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
419 inode->i_op = &ntfs_link_inode_operations;
421 inode_nohighmem(inode); // ??
422 } else if (S_ISREG(mode)) {
423 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
424 inode->i_op = &ntfs_file_inode_operations;
425 inode->i_fop = &ntfs_file_operations;
426 inode->i_mapping->a_ops =
427 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
428 if (ino != MFT_REC_MFT)
429 init_rwsem(&ni->file.run_lock);
430 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
432 inode->i_op = &ntfs_special_inode_operations;
433 init_special_inode(inode, mode, inode->i_rdev);
434 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
435 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
436 /* Records in $Extend are not a files or general directories */
442 if ((sbi->options.sys_immutable &&
443 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
444 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
445 inode->i_flags |= S_IMMUTABLE;
447 inode->i_flags &= ~S_IMMUTABLE;
450 inode->i_mode = mode;
451 if (!(ni->ni_flags & NI_FLAG_EA)) {
452 /* if no xattr then no security (stored in xattr) */
453 inode->i_flags |= S_NOSEC;
457 if (ino == MFT_REC_MFT && !sb->s_root)
460 unlock_new_inode(inode);
465 if (ino == MFT_REC_MFT && !sb->s_root)
472 /* returns 1 if match */
473 static int ntfs_test_inode(struct inode *inode, void *data)
475 struct MFT_REF *ref = data;
477 return ino_get(ref) == inode->i_ino;
480 static int ntfs_set_inode(struct inode *inode, void *data)
482 const struct MFT_REF *ref = data;
484 inode->i_ino = ino_get(ref);
488 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
489 const struct cpu_str *name)
493 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
495 if (unlikely(!inode))
496 return ERR_PTR(-ENOMEM);
498 /* If this is a freshly allocated inode, need to read it now. */
499 if (inode->i_state & I_NEW)
500 inode = ntfs_read_mft(inode, name, ref);
501 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
502 /* inode overlaps? */
503 make_bad_inode(inode);
510 GET_BLOCK_GENERAL = 0,
511 GET_BLOCK_WRITE_BEGIN = 1,
512 GET_BLOCK_DIRECT_IO_R = 2,
513 GET_BLOCK_DIRECT_IO_W = 3,
517 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
518 struct buffer_head *bh, int create,
519 enum get_block_ctx ctx)
521 struct super_block *sb = inode->i_sb;
522 struct ntfs_sb_info *sbi = sb->s_fs_info;
523 struct ntfs_inode *ni = ntfs_i(inode);
524 struct page *page = bh->b_page;
525 u8 cluster_bits = sbi->cluster_bits;
526 u32 block_size = sb->s_blocksize;
527 u64 bytes, lbo, valid;
533 /*clear previous state*/
534 clear_buffer_new(bh);
535 clear_buffer_uptodate(bh);
537 /* direct write uses 'create=0'*/
538 if (!create && vbo >= ni->i_valid) {
543 if (vbo >= inode->i_size) {
548 if (is_resident(ni)) {
550 err = attr_data_read_resident(ni, page);
554 set_buffer_uptodate(bh);
555 bh->b_size = block_size;
559 vcn = vbo >> cluster_bits;
560 off = vbo & sbi->cluster_mask;
563 err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
570 bytes = ((u64)len << cluster_bits) - off;
572 if (lcn == SPARSE_LCN) {
574 if (bh->b_size > bytes)
583 if ((len << cluster_bits) > block_size)
584 ntfs_sparse_cluster(inode, page, vcn, len);
587 lbo = ((u64)lcn << cluster_bits) + off;
589 set_buffer_mapped(bh);
590 bh->b_bdev = sb->s_bdev;
591 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
595 if (ctx == GET_BLOCK_DIRECT_IO_W) {
596 /*ntfs_direct_IO will update ni->i_valid */
601 if (bytes > bh->b_size)
607 if (vbo + bytes > valid) {
608 ni->i_valid = vbo + bytes;
609 mark_inode_dirty(inode);
611 } else if (vbo >= valid) {
612 /* read out of valid data*/
613 /* should never be here 'cause already checked */
614 clear_buffer_mapped(bh);
615 } else if (vbo + bytes <= valid) {
617 } else if (vbo + block_size <= valid) {
618 /* normal short read */
622 * read across valid size: vbo < valid && valid < vbo + block_size
627 u32 voff = valid - vbo;
629 bh->b_size = block_size;
630 off = vbo & (PAGE_SIZE - 1);
631 set_bh_page(bh, page, off);
632 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
634 if (!buffer_uptodate(bh)) {
638 zero_user_segment(page, off + voff, off + block_size);
642 if (bh->b_size > bytes)
646 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
647 static_assert(sizeof(size_t) < sizeof(loff_t));
648 if (bytes > 0x40000000u)
649 bh->b_size = 0x40000000u;
659 int ntfs_get_block(struct inode *inode, sector_t vbn,
660 struct buffer_head *bh_result, int create)
662 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
663 bh_result, create, GET_BLOCK_GENERAL);
666 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
667 struct buffer_head *bh_result, int create)
669 return ntfs_get_block_vbo(inode,
670 (u64)vsn << inode->i_sb->s_blocksize_bits,
671 bh_result, create, GET_BLOCK_BMAP);
674 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
676 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
679 static int ntfs_readpage(struct file *file, struct page *page)
682 struct address_space *mapping = page->mapping;
683 struct inode *inode = mapping->host;
684 struct ntfs_inode *ni = ntfs_i(inode);
686 if (is_resident(ni)) {
688 err = attr_data_read_resident(ni, page);
690 if (err != E_NTFS_NONRESIDENT) {
696 if (is_compressed(ni)) {
698 err = ni_readpage_cmpr(ni, page);
703 /* normal + sparse files */
704 return mpage_readpage(page, ntfs_get_block);
707 static void ntfs_readahead(struct readahead_control *rac)
709 struct address_space *mapping = rac->mapping;
710 struct inode *inode = mapping->host;
711 struct ntfs_inode *ni = ntfs_i(inode);
715 if (is_resident(ni)) {
716 /* no readahead for resident */
720 if (is_compressed(ni)) {
721 /* no readahead for compressed */
726 pos = readahead_pos(rac);
728 if (valid < i_size_read(inode) && pos <= valid &&
729 valid < pos + readahead_length(rac)) {
730 /* range cross 'valid'. read it page by page */
734 mpage_readahead(rac, ntfs_get_block);
737 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
738 struct buffer_head *bh_result, int create)
740 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
741 bh_result, create, GET_BLOCK_DIRECT_IO_R);
744 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
745 struct buffer_head *bh_result, int create)
747 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
748 bh_result, create, GET_BLOCK_DIRECT_IO_W);
751 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
753 struct file *file = iocb->ki_filp;
754 struct address_space *mapping = file->f_mapping;
755 struct inode *inode = mapping->host;
756 struct ntfs_inode *ni = ntfs_i(inode);
757 loff_t vbo = iocb->ki_pos;
759 int wr = iov_iter_rw(iter) & WRITE;
763 if (is_resident(ni)) {
764 /*switch to buffered write*/
769 ret = blockdev_direct_IO(iocb, inode, iter,
770 wr ? ntfs_get_block_direct_IO_W
771 : ntfs_get_block_direct_IO_R);
779 if (end > valid && !S_ISBLK(inode->i_mode)) {
781 mark_inode_dirty(inode);
783 } else if (vbo < valid && valid < end) {
785 iov_iter_revert(iter, end - valid);
786 iov_iter_zero(end - valid, iter);
793 int ntfs_set_size(struct inode *inode, u64 new_size)
795 struct super_block *sb = inode->i_sb;
796 struct ntfs_sb_info *sbi = sb->s_fs_info;
797 struct ntfs_inode *ni = ntfs_i(inode);
800 /* Check for maximum file size */
801 if (is_sparsed(ni) || is_compressed(ni)) {
802 if (new_size > sbi->maxbytes_sparse) {
806 } else if (new_size > sbi->maxbytes) {
812 down_write(&ni->file.run_lock);
814 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
815 &ni->i_valid, true, NULL);
817 up_write(&ni->file.run_lock);
820 mark_inode_dirty(inode);
826 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
828 struct address_space *mapping = page->mapping;
829 struct inode *inode = mapping->host;
830 struct ntfs_inode *ni = ntfs_i(inode);
833 if (is_resident(ni)) {
835 err = attr_data_write_resident(ni, page);
837 if (err != E_NTFS_NONRESIDENT) {
843 return block_write_full_page(page, ntfs_get_block, wbc);
846 static int ntfs_writepages(struct address_space *mapping,
847 struct writeback_control *wbc)
849 struct inode *inode = mapping->host;
850 struct ntfs_inode *ni = ntfs_i(inode);
851 /* redirect call to 'ntfs_writepage' for resident files*/
852 get_block_t *get_block = is_resident(ni) ? NULL : &ntfs_get_block;
854 return mpage_writepages(mapping, wbc, get_block);
857 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
858 struct buffer_head *bh_result, int create)
860 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
861 bh_result, create, GET_BLOCK_WRITE_BEGIN);
864 static int ntfs_write_begin(struct file *file, struct address_space *mapping,
865 loff_t pos, u32 len, u32 flags, struct page **pagep,
869 struct inode *inode = mapping->host;
870 struct ntfs_inode *ni = ntfs_i(inode);
873 if (is_resident(ni)) {
874 struct page *page = grab_cache_page_write_begin(
875 mapping, pos >> PAGE_SHIFT, flags);
883 err = attr_data_read_resident(ni, page);
893 if (err != E_NTFS_NONRESIDENT)
897 err = block_write_begin(mapping, pos, len, flags, pagep,
898 ntfs_get_block_write_begin);
904 /* address_space_operations::write_end */
905 static int ntfs_write_end(struct file *file, struct address_space *mapping,
906 loff_t pos, u32 len, u32 copied, struct page *page,
910 struct inode *inode = mapping->host;
911 struct ntfs_inode *ni = ntfs_i(inode);
912 u64 valid = ni->i_valid;
916 if (is_resident(ni)) {
918 err = attr_data_write_resident(ni, page);
922 /* clear any buffers in page*/
923 if (page_has_buffers(page)) {
924 struct buffer_head *head, *bh;
926 bh = head = page_buffers(page);
928 clear_buffer_dirty(bh);
929 clear_buffer_mapped(bh);
930 set_buffer_uptodate(bh);
931 } while (head != (bh = bh->b_this_page));
933 SetPageUptodate(page);
939 err = generic_write_end(file, mapping, pos, len, copied, page,
944 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
945 inode->i_ctime = inode->i_mtime = current_time(inode);
946 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
950 if (valid != ni->i_valid) {
951 /* ni->i_valid is changed in ntfs_get_block_vbo */
956 mark_inode_dirty(inode);
962 int reset_log_file(struct inode *inode)
966 u32 log_size = inode->i_size;
967 struct address_space *mapping = inode->i_mapping;
974 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
976 err = block_write_begin(mapping, pos, len, 0, &page,
977 ntfs_get_block_write_begin);
981 kaddr = kmap_atomic(page);
982 memset(kaddr, -1, len);
983 kunmap_atomic(kaddr);
984 flush_dcache_page(page);
986 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
993 balance_dirty_pages_ratelimited(mapping);
996 mark_inode_dirty_sync(inode);
1001 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1003 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1006 int ntfs_sync_inode(struct inode *inode)
1008 return _ni_write_inode(inode, 1);
1012 * helper function for ntfs_flush_inodes. This writes both the inode
1013 * and the file data blocks, waiting for in flight data blocks before
1014 * the start of the call. It does not wait for any io started
1017 static int writeback_inode(struct inode *inode)
1019 int ret = sync_inode_metadata(inode, 0);
1022 ret = filemap_fdatawrite(inode->i_mapping);
1027 * write data and metadata corresponding to i1 and i2. The io is
1028 * started but we do not wait for any of it to finish.
1030 * filemap_flush is used for the block device, so if there is a dirty
1031 * page for a block already in flight, we will not wait and start the
1034 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1040 ret = writeback_inode(i1);
1042 ret = writeback_inode(i2);
1044 ret = filemap_flush(sb->s_bdev->bd_inode->i_mapping);
1048 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1052 /* Write non resident data */
1053 for (idx = 0; bytes; idx++) {
1054 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1055 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1058 return PTR_ERR(page);
1061 WARN_ON(!PageUptodate(page));
1062 ClearPageUptodate(page);
1064 memcpy(page_address(page), data, op);
1066 flush_dcache_page(page);
1067 SetPageUptodate(page);
1070 ntfs_unmap_page(page);
1073 data = Add2Ptr(data, PAGE_SIZE);
1079 * number of bytes to for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1080 * for unicode string of 'uni_len' length
1082 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1084 /* header + unicode string + decorated unicode string */
1085 return sizeof(short) * (2 * uni_len + 4) +
1086 offsetof(struct REPARSE_DATA_BUFFER,
1087 SymbolicLinkReparseBuffer.PathBuffer);
1090 static struct REPARSE_DATA_BUFFER *
1091 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1092 u32 size, u16 *nsize)
1095 struct REPARSE_DATA_BUFFER *rp;
1097 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1099 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1101 return ERR_PTR(-ENOMEM);
1103 rs = &rp->SymbolicLinkReparseBuffer;
1104 rp_name = rs->PathBuffer;
1106 /* Convert link name to utf16 */
1107 err = ntfs_nls_to_utf16(sbi, symname, size,
1108 (struct cpu_str *)(rp_name - 1), 2 * size,
1109 UTF16_LITTLE_ENDIAN);
1113 /* err = the length of unicode name of symlink */
1114 *nsize = ntfs_reparse_bytes(err);
1116 if (*nsize > sbi->reparse.max_size) {
1121 /* translate linux '/' into windows '\' */
1122 for (i = 0; i < err; i++) {
1123 if (rp_name[i] == cpu_to_le16('/'))
1124 rp_name[i] = cpu_to_le16('\\');
1127 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1128 rp->ReparseDataLength =
1129 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1130 SymbolicLinkReparseBuffer));
1132 /* PrintName + SubstituteName */
1133 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1134 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1135 rs->PrintNameLength = rs->SubstituteNameOffset;
1138 * TODO: use relative path if possible to allow windows to parse this path
1139 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE)
1143 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1145 /* decorate SubstituteName */
1147 rp_name[0] = cpu_to_le16('\\');
1148 rp_name[1] = cpu_to_le16('?');
1149 rp_name[2] = cpu_to_le16('?');
1150 rp_name[3] = cpu_to_le16('\\');
1155 return ERR_PTR(err);
1158 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1159 struct inode *dir, struct dentry *dentry,
1160 const struct cpu_str *uni, umode_t mode,
1161 dev_t dev, const char *symname, u32 size,
1162 struct ntfs_fnd *fnd)
1165 struct super_block *sb = dir->i_sb;
1166 struct ntfs_sb_info *sbi = sb->s_fs_info;
1167 const struct qstr *name = &dentry->d_name;
1169 struct ntfs_inode *dir_ni = ntfs_i(dir);
1170 struct ntfs_inode *ni = NULL;
1171 struct inode *inode = NULL;
1172 struct ATTRIB *attr;
1173 struct ATTR_STD_INFO5 *std5;
1174 struct ATTR_FILE_NAME *fname;
1175 struct MFT_REC *rec;
1176 u32 asize, dsize, sd_size;
1177 enum FILE_ATTRIBUTE fa;
1178 __le32 security_id = SECURITY_ID_INVALID;
1181 u16 t16, nsize = 0, aid = 0;
1182 struct INDEX_ROOT *root, *dir_root;
1183 struct NTFS_DE *e, *new_de = NULL;
1184 struct REPARSE_DATA_BUFFER *rp = NULL;
1185 bool rp_inserted = false;
1187 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1189 return ERR_PTR(-EINVAL);
1191 if (S_ISDIR(mode)) {
1192 /* use parent's directory attributes */
1193 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1194 FILE_ATTRIBUTE_ARCHIVE;
1196 * By default child directory inherits parent attributes
1197 * root directory is hidden + system
1198 * Make an exception for children in root
1200 if (dir->i_ino == MFT_REC_ROOT)
1201 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1202 } else if (S_ISLNK(mode)) {
1203 /* It is good idea that link should be the same type (file/dir) as target */
1204 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1207 * linux: there are dir/file/symlink and so on
1208 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1209 * It is good idea to create:
1210 * dir + reparse if 'symname' points to directory
1212 * file + reparse if 'symname' points to file
1213 * Unfortunately kern_path hangs if symname contains 'dir'
1219 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1220 * struct inode *target = d_inode(path.dentry);
1222 * if (S_ISDIR(target->i_mode))
1223 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1224 * // if ( target->i_sb == sb ){
1225 * // use relative path?
1230 } else if (S_ISREG(mode)) {
1231 if (sbi->options.sparse) {
1232 /* sparsed regular file, cause option 'sparse' */
1233 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1234 FILE_ATTRIBUTE_ARCHIVE;
1235 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1236 /* compressed regular file, if parent is compressed */
1237 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1239 /* regular file, default attributes */
1240 fa = FILE_ATTRIBUTE_ARCHIVE;
1243 fa = FILE_ATTRIBUTE_ARCHIVE;
1247 fa |= FILE_ATTRIBUTE_READONLY;
1249 /* allocate PATH_MAX bytes */
1250 new_de = __getname();
1256 /*mark rw ntfs as dirty. it will be cleared at umount*/
1257 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1259 /* Step 1: allocate and fill new mft record */
1260 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1264 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1270 inode = &ni->vfs_inode;
1271 inode_init_owner(mnt_userns, inode, dir, mode);
1273 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1274 current_time(inode);
1277 rec->hard_links = cpu_to_le16(1);
1278 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1280 /* Get default security id */
1281 sd = s_default_security;
1282 sd_size = sizeof(s_default_security);
1284 if (is_ntfs3(sbi)) {
1285 security_id = dir_ni->std_security_id;
1286 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1287 security_id = sbi->security.def_security_id;
1289 if (security_id == SECURITY_ID_INVALID &&
1290 !ntfs_insert_security(sbi, sd, sd_size,
1291 &security_id, NULL))
1292 sbi->security.def_security_id = security_id;
1296 /* Insert standard info */
1297 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1299 if (security_id == SECURITY_ID_INVALID) {
1300 dsize = sizeof(struct ATTR_STD_INFO);
1302 dsize = sizeof(struct ATTR_STD_INFO5);
1303 std5->security_id = security_id;
1304 ni->std_security_id = security_id;
1306 asize = SIZEOF_RESIDENT + dsize;
1308 attr->type = ATTR_STD;
1309 attr->size = cpu_to_le32(asize);
1310 attr->id = cpu_to_le16(aid++);
1311 attr->res.data_off = SIZEOF_RESIDENT_LE;
1312 attr->res.data_size = cpu_to_le32(dsize);
1314 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1315 kernel2nt(&inode->i_atime);
1320 attr = Add2Ptr(attr, asize);
1322 /* Insert file name */
1323 err = fill_name_de(sbi, new_de, name, uni);
1327 mi_get_ref(&ni->mi, &new_de->ref);
1329 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1330 mi_get_ref(&dir_ni->mi, &fname->home);
1331 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1332 fname->dup.a_time = std5->cr_time;
1333 fname->dup.alloc_size = fname->dup.data_size = 0;
1334 fname->dup.fa = std5->fa;
1335 fname->dup.ea_size = fname->dup.reparse = 0;
1337 dsize = le16_to_cpu(new_de->key_size);
1338 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1340 attr->type = ATTR_NAME;
1341 attr->size = cpu_to_le32(asize);
1342 attr->res.data_off = SIZEOF_RESIDENT_LE;
1343 attr->res.flags = RESIDENT_FLAG_INDEXED;
1344 attr->id = cpu_to_le16(aid++);
1345 attr->res.data_size = cpu_to_le32(dsize);
1346 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1348 attr = Add2Ptr(attr, asize);
1350 if (security_id == SECURITY_ID_INVALID) {
1351 /* Insert security attribute */
1352 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1354 attr->type = ATTR_SECURE;
1355 attr->size = cpu_to_le32(asize);
1356 attr->id = cpu_to_le16(aid++);
1357 attr->res.data_off = SIZEOF_RESIDENT_LE;
1358 attr->res.data_size = cpu_to_le32(sd_size);
1359 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1361 attr = Add2Ptr(attr, asize);
1364 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1366 * regular directory or symlink to directory
1367 * Create root attribute
1369 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1370 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1372 attr->type = ATTR_ROOT;
1373 attr->size = cpu_to_le32(asize);
1374 attr->id = cpu_to_le16(aid++);
1376 attr->name_len = ARRAY_SIZE(I30_NAME);
1377 attr->name_off = SIZEOF_RESIDENT_LE;
1378 attr->res.data_off =
1379 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1380 attr->res.data_size = cpu_to_le32(dsize);
1381 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1384 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1385 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1387 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1388 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1389 sizeof(struct NTFS_DE));
1390 root->ihdr.total = root->ihdr.used;
1392 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1393 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1394 e->flags = NTFS_IE_LAST;
1395 } else if (S_ISLNK(mode)) {
1398 * Create empty resident data attribute
1400 asize = SIZEOF_RESIDENT;
1402 /* insert empty ATTR_DATA */
1403 attr->type = ATTR_DATA;
1404 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1405 attr->id = cpu_to_le16(aid++);
1406 attr->name_off = SIZEOF_RESIDENT_LE;
1407 attr->res.data_off = SIZEOF_RESIDENT_LE;
1410 * regular file or node
1412 attr->type = ATTR_DATA;
1413 attr->id = cpu_to_le16(aid++);
1415 if (S_ISREG(mode)) {
1416 /* Create empty non resident data attribute */
1418 attr->nres.evcn = cpu_to_le64(-1ll);
1419 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1421 cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1422 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1423 attr->flags = ATTR_FLAG_SPARSED;
1424 asize = SIZEOF_NONRESIDENT_EX + 8;
1425 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1427 cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1428 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1429 attr->flags = ATTR_FLAG_COMPRESSED;
1430 attr->nres.c_unit = COMPRESSION_UNIT;
1431 asize = SIZEOF_NONRESIDENT_EX + 8;
1434 cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1435 attr->name_off = SIZEOF_NONRESIDENT_LE;
1436 asize = SIZEOF_NONRESIDENT + 8;
1438 attr->nres.run_off = attr->name_off;
1440 /* Create empty resident data attribute */
1441 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1442 attr->name_off = SIZEOF_RESIDENT_LE;
1443 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1444 attr->flags = ATTR_FLAG_SPARSED;
1445 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1446 attr->flags = ATTR_FLAG_COMPRESSED;
1447 attr->res.data_off = SIZEOF_RESIDENT_LE;
1448 asize = SIZEOF_RESIDENT;
1449 ni->ni_flags |= NI_FLAG_RESIDENT;
1453 if (S_ISDIR(mode)) {
1454 ni->ni_flags |= NI_FLAG_DIR;
1455 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1458 } else if (S_ISLNK(mode)) {
1459 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1468 * Insert ATTR_REPARSE
1470 attr = Add2Ptr(attr, asize);
1471 attr->type = ATTR_REPARSE;
1472 attr->id = cpu_to_le16(aid++);
1474 /* resident or non resident? */
1475 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1476 t16 = PtrOffset(rec, attr);
1478 if (asize + t16 + 8 > sbi->record_size) {
1480 CLST clst = bytes_to_cluster(sbi, nsize);
1482 /* bytes per runs */
1483 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1486 attr->nres.evcn = cpu_to_le64(clst - 1);
1487 attr->name_off = SIZEOF_NONRESIDENT_LE;
1488 attr->nres.run_off = attr->name_off;
1489 attr->nres.data_size = cpu_to_le64(nsize);
1490 attr->nres.valid_size = attr->nres.data_size;
1491 attr->nres.alloc_size =
1492 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1494 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1495 clst, NULL, 0, &alen, 0,
1500 err = run_pack(&ni->file.run, 0, clst,
1501 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1511 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1512 inode->i_size = nsize;
1514 attr->res.data_off = SIZEOF_RESIDENT_LE;
1515 attr->res.data_size = cpu_to_le32(nsize);
1516 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1517 inode->i_size = nsize;
1521 attr->size = cpu_to_le32(asize);
1523 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1531 attr = Add2Ptr(attr, asize);
1532 attr->type = ATTR_END;
1534 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1535 rec->next_attr_id = cpu_to_le16(aid);
1537 /* Step 2: Add new name in index */
1538 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd);
1542 /* Update current directory record */
1543 mark_inode_dirty(dir);
1545 inode->i_generation = le16_to_cpu(rec->seq);
1547 dir->i_mtime = dir->i_ctime = inode->i_atime;
1549 if (S_ISDIR(mode)) {
1550 if (dir->i_mode & S_ISGID)
1552 inode->i_op = &ntfs_dir_inode_operations;
1553 inode->i_fop = &ntfs_dir_operations;
1554 } else if (S_ISLNK(mode)) {
1555 inode->i_op = &ntfs_link_inode_operations;
1556 inode->i_fop = NULL;
1557 inode->i_mapping->a_ops = &ntfs_aops;
1558 } else if (S_ISREG(mode)) {
1559 inode->i_op = &ntfs_file_inode_operations;
1560 inode->i_fop = &ntfs_file_operations;
1561 inode->i_mapping->a_ops =
1562 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1563 init_rwsem(&ni->file.run_lock);
1565 inode->i_op = &ntfs_special_inode_operations;
1566 init_special_inode(inode, mode, dev);
1569 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1570 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1571 err = ntfs_init_acl(mnt_userns, inode, dir);
1577 inode->i_flags |= S_NOSEC;
1580 /* Write non resident data */
1582 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize);
1587 /* call 'd_instantiate' after inode->i_op is set but before finish_open */
1588 d_instantiate(dentry, inode);
1590 ntfs_save_wsl_perm(inode);
1591 mark_inode_dirty(inode);
1592 mark_inode_dirty(dir);
1599 /* undo 'indx_insert_entry' */
1600 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1601 le16_to_cpu(new_de->key_size), sbi);
1604 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1607 if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1610 run_deallocate(sbi, &ni->file.run, false);
1613 clear_rec_inuse(rec);
1615 ni->mi.dirty = false;
1616 discard_new_inode(inode);
1618 ntfs_mark_rec_free(sbi, ino);
1626 return ERR_PTR(err);
1628 unlock_new_inode(inode);
1633 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1636 struct inode *dir = d_inode(dentry->d_parent);
1637 struct ntfs_inode *dir_ni = ntfs_i(dir);
1638 struct ntfs_inode *ni = ntfs_i(inode);
1639 struct super_block *sb = inode->i_sb;
1640 struct ntfs_sb_info *sbi = sb->s_fs_info;
1641 const struct qstr *name = &dentry->d_name;
1642 struct NTFS_DE *new_de = NULL;
1643 struct ATTR_FILE_NAME *fname;
1644 struct ATTRIB *attr;
1646 struct INDEX_ROOT *dir_root;
1648 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1652 /* allocate PATH_MAX bytes */
1653 new_de = __getname();
1657 /*mark rw ntfs as dirty. it will be cleared at umount*/
1658 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
1661 err = fill_name_de(sbi, new_de, name, NULL);
1665 key_size = le16_to_cpu(new_de->key_size);
1666 err = ni_insert_resident(ni, key_size, ATTR_NAME, NULL, 0, &attr, NULL);
1670 mi_get_ref(&ni->mi, &new_de->ref);
1672 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1673 mi_get_ref(&dir_ni->mi, &fname->home);
1674 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1675 fname->dup.a_time = kernel2nt(&inode->i_ctime);
1676 fname->dup.alloc_size = fname->dup.data_size = 0;
1677 fname->dup.fa = ni->std_fa;
1678 fname->dup.ea_size = fname->dup.reparse = 0;
1680 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, key_size);
1682 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, NULL);
1686 le16_add_cpu(&ni->mi.mrec->hard_links, 1);
1687 ni->mi.dirty = true;
1697 * inode_operations::unlink
1698 * inode_operations::rmdir
1700 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1703 struct super_block *sb = dir->i_sb;
1704 struct ntfs_sb_info *sbi = sb->s_fs_info;
1705 struct inode *inode = d_inode(dentry);
1706 struct ntfs_inode *ni = ntfs_i(inode);
1707 const struct qstr *name = &dentry->d_name;
1708 struct ntfs_inode *dir_ni = ntfs_i(dir);
1709 struct ntfs_index *indx = &dir_ni->dir;
1710 struct cpu_str *uni = NULL;
1711 struct ATTR_FILE_NAME *fname;
1713 struct ATTR_LIST_ENTRY *le;
1715 bool is_dir = S_ISDIR(inode->i_mode);
1716 struct INDEX_ROOT *dir_root;
1718 dir_root = indx_get_root(indx, dir_ni, NULL, NULL);
1724 if (is_dir && !dir_is_empty(inode)) {
1729 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
1734 /* allocate PATH_MAX bytes */
1741 /* Convert input string to unicode */
1742 err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
1747 /*mark rw ntfs as dirty. it will be cleared at umount*/
1748 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1750 /* find name in record */
1751 mi_get_ref(&dir_ni->mi, &ref);
1754 fname = ni_fname_name(ni, uni, &ref, &le);
1760 name_type = paired_name(fname->type);
1762 err = indx_delete_entry(indx, dir_ni, fname, fname_full_size(fname),
1767 /* Then remove name from mft */
1768 ni_remove_attr_le(ni, attr_from_name(fname), le);
1770 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
1771 ni->mi.dirty = true;
1773 if (name_type != FILE_NAME_POSIX) {
1774 /* Now we should delete name by type */
1775 fname = ni_fname_type(ni, name_type, &le);
1777 err = indx_delete_entry(indx, dir_ni, fname,
1778 fname_full_size(fname), sbi);
1782 ni_remove_attr_le(ni, attr_from_name(fname), le);
1784 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
1797 make_bad_inode(inode);
1800 dir->i_mtime = dir->i_ctime = current_time(dir);
1801 mark_inode_dirty(dir);
1802 inode->i_ctime = dir->i_ctime;
1804 mark_inode_dirty(inode);
1813 void ntfs_evict_inode(struct inode *inode)
1815 truncate_inode_pages_final(&inode->i_data);
1818 _ni_write_inode(inode, inode_needs_sync(inode));
1820 invalidate_inode_buffers(inode);
1823 ni_clear(ntfs_i(inode));
1826 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1830 struct ntfs_inode *ni = ntfs_i(inode);
1831 struct super_block *sb = inode->i_sb;
1832 struct ntfs_sb_info *sbi = sb->s_fs_info;
1833 u64 i_size = inode->i_size;
1835 void *to_free = NULL;
1836 struct REPARSE_DATA_BUFFER *rp;
1838 struct ATTRIB *attr;
1840 /* Reparse data present. Try to parse it */
1841 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1842 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1846 /* Read into temporal buffer */
1847 if (i_size > sbi->reparse.max_size || i_size <= sizeof(u32)) {
1852 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1858 if (!attr->non_res) {
1859 rp = resident_data_ex(attr, i_size);
1865 rp = kmalloc(i_size, GFP_NOFS);
1871 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, i_size, NULL);
1879 switch (rp->ReparseTag) {
1880 case IO_REPARSE_TAG_MOUNT_POINT:
1881 /* Mount points and junctions */
1882 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1883 if (i_size <= offsetof(struct REPARSE_DATA_BUFFER,
1884 MountPointReparseBuffer.PathBuffer))
1887 offsetof(struct REPARSE_DATA_BUFFER,
1888 MountPointReparseBuffer.PathBuffer) +
1889 le16_to_cpu(rp->MountPointReparseBuffer
1892 nlen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1895 case IO_REPARSE_TAG_SYMLINK:
1896 /* FolderSymbolicLink */
1897 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1898 if (i_size <= offsetof(struct REPARSE_DATA_BUFFER,
1899 SymbolicLinkReparseBuffer.PathBuffer))
1902 offsetof(struct REPARSE_DATA_BUFFER,
1903 SymbolicLinkReparseBuffer.PathBuffer) +
1904 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1908 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1911 case IO_REPARSE_TAG_CLOUD:
1912 case IO_REPARSE_TAG_CLOUD_1:
1913 case IO_REPARSE_TAG_CLOUD_2:
1914 case IO_REPARSE_TAG_CLOUD_3:
1915 case IO_REPARSE_TAG_CLOUD_4:
1916 case IO_REPARSE_TAG_CLOUD_5:
1917 case IO_REPARSE_TAG_CLOUD_6:
1918 case IO_REPARSE_TAG_CLOUD_7:
1919 case IO_REPARSE_TAG_CLOUD_8:
1920 case IO_REPARSE_TAG_CLOUD_9:
1921 case IO_REPARSE_TAG_CLOUD_A:
1922 case IO_REPARSE_TAG_CLOUD_B:
1923 case IO_REPARSE_TAG_CLOUD_C:
1924 case IO_REPARSE_TAG_CLOUD_D:
1925 case IO_REPARSE_TAG_CLOUD_E:
1926 case IO_REPARSE_TAG_CLOUD_F:
1927 err = sizeof("OneDrive") - 1;
1930 memcpy(buffer, "OneDrive", err);
1934 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1935 /* unknown Microsoft Tag */
1938 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1939 i_size <= sizeof(struct REPARSE_POINT)) {
1944 uni = Add2Ptr(rp, sizeof(struct REPARSE_POINT) - 2);
1945 nlen = le16_to_cpu(rp->ReparseDataLength) -
1946 sizeof(struct REPARSE_POINT);
1949 /* Convert nlen from bytes to UNICODE chars */
1952 /* Check that name is available */
1953 if (!nlen || &uni->name[nlen] > (__le16 *)Add2Ptr(rp, i_size))
1956 /* If name is already zero terminated then truncate it now */
1957 if (!uni->name[nlen - 1])
1961 err = ntfs_utf16_to_nls(sbi, uni, buffer, buflen);
1966 /* translate windows '\' into linux '/' */
1967 for (i = 0; i < err; i++) {
1968 if (buffer[i] == '\\')
1972 /* Always set last zero */
1979 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1980 struct delayed_call *done)
1986 return ERR_PTR(-ECHILD);
1988 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1990 return ERR_PTR(-ENOMEM);
1992 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1995 return ERR_PTR(err);
1998 set_delayed_call(done, kfree_link, ret);
2004 const struct inode_operations ntfs_link_inode_operations = {
2005 .get_link = ntfs_get_link,
2006 .setattr = ntfs3_setattr,
2007 .listxattr = ntfs_listxattr,
2008 .permission = ntfs_permission,
2009 .get_acl = ntfs_get_acl,
2010 .set_acl = ntfs_set_acl,
2013 const struct address_space_operations ntfs_aops = {
2014 .readpage = ntfs_readpage,
2015 .readahead = ntfs_readahead,
2016 .writepage = ntfs_writepage,
2017 .writepages = ntfs_writepages,
2018 .write_begin = ntfs_write_begin,
2019 .write_end = ntfs_write_end,
2020 .direct_IO = ntfs_direct_IO,
2022 .set_page_dirty = __set_page_dirty_buffers,
2025 const struct address_space_operations ntfs_aops_cmpr = {
2026 .readpage = ntfs_readpage,
2027 .readahead = ntfs_readahead,