1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/buffer_head.h>
10 #include <linux/mpage.h>
11 #include <linux/namei.h>
12 #include <linux/nls.h>
13 #include <linux/uio.h>
14 #include <linux/writeback.h>
21 * ntfs_read_mft - Read record and parses MFT.
23 static struct inode *ntfs_read_mft(struct inode *inode,
24 const struct cpu_str *name,
25 const struct MFT_REF *ref)
28 struct ntfs_inode *ni = ntfs_i(inode);
29 struct super_block *sb = inode->i_sb;
30 struct ntfs_sb_info *sbi = sb->s_fs_info;
32 struct ATTR_STD_INFO5 *std5 = NULL;
33 struct ATTR_LIST_ENTRY *le;
35 bool is_match = false;
38 unsigned long ino = inode->i_ino;
39 u32 rp_fa = 0, asize, t32;
40 u16 roff, rsize, names = 0;
41 const struct ATTR_FILE_NAME *fname = NULL;
42 const struct INDEX_ROOT *root;
43 struct REPARSE_DATA_BUFFER rp; // 0x18 bytes
46 struct runs_tree *run;
49 /* Setup 'uid' and 'gid' */
50 inode->i_uid = sbi->options->fs_uid;
51 inode->i_gid = sbi->options->fs_gid;
53 err = mi_init(&ni->mi, sbi, ino);
57 if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
58 t64 = sbi->mft.lbo >> sbi->cluster_bits;
59 t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
61 init_rwsem(&ni->file.run_lock);
63 if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
69 err = mi_read(&ni->mi, ino == MFT_REC_MFT);
76 if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
78 } else if (ref->seq != rec->seq) {
80 ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
81 le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
83 } else if (!is_rec_inuse(rec)) {
85 ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
89 if (le32_to_cpu(rec->total) != sbi->record_size) {
95 if (!is_rec_base(rec))
98 /* Record should contain $I30 root. */
99 is_dir = rec->flags & RECORD_FLAG_DIR;
101 inode->i_generation = le16_to_cpu(rec->seq);
103 /* Enumerate all struct Attributes MFT. */
108 * To reduce tab pressure use goto instead of
109 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
114 attr = ni_enum_attr_ex(ni, attr, &le, NULL);
119 /* This is non primary attribute segment. Ignore if not MFT. */
120 if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
124 asize = le32_to_cpu(attr->size);
125 goto attr_unpack_run;
128 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
129 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
130 asize = le32_to_cpu(attr->size);
132 switch (attr->type) {
135 asize < sizeof(struct ATTR_STD_INFO) + roff ||
136 rsize < sizeof(struct ATTR_STD_INFO))
142 std5 = Add2Ptr(attr, roff);
145 nt2kernel(std5->cr_time, &ni->i_crtime);
147 nt2kernel(std5->a_time, &inode->i_atime);
148 nt2kernel(std5->c_time, &inode->i_ctime);
149 nt2kernel(std5->m_time, &inode->i_mtime);
151 ni->std_fa = std5->fa;
153 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
154 rsize >= sizeof(struct ATTR_STD_INFO5))
155 ni->std_security_id = std5->security_id;
159 if (attr->name_len || le || ino == MFT_REC_LOG)
162 err = ntfs_load_attr_list(ni, attr);
171 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
172 rsize < SIZEOF_ATTRIBUTE_FILENAME)
175 fname = Add2Ptr(attr, roff);
176 if (fname->type == FILE_NAME_DOS)
180 if (name && name->len == fname->name_len &&
181 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
189 /* Ignore data attribute in dir record. */
193 if (ino == MFT_REC_BADCLUST && !attr->non_res)
196 if (attr->name_len &&
197 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
198 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
199 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
200 (ino != MFT_REC_SECURE || !attr->non_res ||
201 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
202 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
203 /* File contains stream attribute. Ignore it. */
207 if (is_attr_sparsed(attr))
208 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
210 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
212 if (is_attr_compressed(attr))
213 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
215 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
217 if (is_attr_encrypted(attr))
218 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
220 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
222 if (!attr->non_res) {
223 ni->i_valid = inode->i_size = rsize;
224 inode_set_bytes(inode, rsize);
227 t32 = le16_to_cpu(attr->nres.run_off);
230 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
232 if (!attr->non_res) {
233 ni->ni_flags |= NI_FLAG_RESIDENT;
237 inode_set_bytes(inode, attr_ondisk_size(attr));
239 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
240 inode->i_size = le64_to_cpu(attr->nres.data_size);
241 if (!attr->nres.alloc_size)
244 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
252 root = Add2Ptr(attr, roff);
255 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
256 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
259 if (root->type != ATTR_NAME ||
260 root->rule != NTFS_COLLATION_TYPE_FILENAME)
266 ni->ni_flags |= NI_FLAG_DIR;
268 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
273 ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
278 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
279 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
282 inode->i_size = le64_to_cpu(attr->nres.data_size);
283 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
284 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
286 run = &ni->dir.alloc_run;
290 if (ino == MFT_REC_MFT) {
293 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
294 /* 0x20000000 = 2^32 / 8 */
295 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
298 run = &sbi->mft.bitmap.run;
300 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
301 !memcmp(attr_name(attr), I30_NAME,
304 run = &ni->dir.bitmap_run;
313 rp_fa = ni_parse_reparse(ni, attr, &rp);
316 if (!attr->non_res) {
317 inode->i_size = rsize;
318 inode_set_bytes(inode, rsize);
322 le64_to_cpu(attr->nres.data_size);
323 t32 = le16_to_cpu(attr->nres.run_off);
326 /* Looks like normal symlink. */
327 ni->i_valid = inode->i_size;
329 /* Clear directory bit. */
330 if (ni->ni_flags & NI_FLAG_DIR) {
331 indx_clear(&ni->dir);
332 memset(&ni->dir, 0, sizeof(ni->dir));
333 ni->ni_flags &= ~NI_FLAG_DIR;
335 run_close(&ni->file.run);
337 mode = S_IFLNK | 0777;
341 goto attr_unpack_run; // Double break.
345 case REPARSE_COMPRESSED:
348 case REPARSE_DEDUPLICATED:
354 if (!attr->name_len &&
355 resident_data_ex(attr, sizeof(struct EA_INFO))) {
356 ni->ni_flags |= NI_FLAG_EA;
358 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
360 inode->i_mode = mode;
361 ntfs_get_wsl_perm(inode);
362 mode = inode->i_mode;
371 roff = le16_to_cpu(attr->nres.run_off);
373 t64 = le64_to_cpu(attr->nres.svcn);
374 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
375 t64, Add2Ptr(attr, roff), asize - roff);
386 if (!is_match && name) {
387 /* Reuse rec as buffer for ascii name. */
392 if (std5->fa & FILE_ATTRIBUTE_READONLY)
400 if (names != le16_to_cpu(rec->hard_links)) {
401 /* Correct minor error on the fly. Do not mark inode as dirty. */
402 rec->hard_links = cpu_to_le16(names);
406 set_nlink(inode, names);
409 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
412 * Dot and dot-dot should be included in count but was not
413 * included in enumeration.
414 * Usually a hard links to directories are disabled.
416 inode->i_op = &ntfs_dir_inode_operations;
417 inode->i_fop = &ntfs_dir_operations;
419 } else if (S_ISLNK(mode)) {
420 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
421 inode->i_op = &ntfs_link_inode_operations;
423 inode_nohighmem(inode); // ??
424 } else if (S_ISREG(mode)) {
425 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
426 inode->i_op = &ntfs_file_inode_operations;
427 inode->i_fop = &ntfs_file_operations;
428 inode->i_mapping->a_ops =
429 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
430 if (ino != MFT_REC_MFT)
431 init_rwsem(&ni->file.run_lock);
432 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
434 inode->i_op = &ntfs_special_inode_operations;
435 init_special_inode(inode, mode, inode->i_rdev);
436 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
437 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
438 /* Records in $Extend are not a files or general directories. */
444 if ((sbi->options->sys_immutable &&
445 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
446 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
447 inode->i_flags |= S_IMMUTABLE;
449 inode->i_flags &= ~S_IMMUTABLE;
452 inode->i_mode = mode;
453 if (!(ni->ni_flags & NI_FLAG_EA)) {
454 /* If no xattr then no security (stored in xattr). */
455 inode->i_flags |= S_NOSEC;
459 if (ino == MFT_REC_MFT && !sb->s_root)
462 unlock_new_inode(inode);
467 if (ino == MFT_REC_MFT && !sb->s_root)
477 * Return: 1 if match.
479 static int ntfs_test_inode(struct inode *inode, void *data)
481 struct MFT_REF *ref = data;
483 return ino_get(ref) == inode->i_ino;
486 static int ntfs_set_inode(struct inode *inode, void *data)
488 const struct MFT_REF *ref = data;
490 inode->i_ino = ino_get(ref);
494 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
495 const struct cpu_str *name)
499 inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
501 if (unlikely(!inode))
502 return ERR_PTR(-ENOMEM);
504 /* If this is a freshly allocated inode, need to read it now. */
505 if (inode->i_state & I_NEW)
506 inode = ntfs_read_mft(inode, name, ref);
507 else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
508 /* Inode overlaps? */
509 make_bad_inode(inode);
516 GET_BLOCK_GENERAL = 0,
517 GET_BLOCK_WRITE_BEGIN = 1,
518 GET_BLOCK_DIRECT_IO_R = 2,
519 GET_BLOCK_DIRECT_IO_W = 3,
523 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
524 struct buffer_head *bh, int create,
525 enum get_block_ctx ctx)
527 struct super_block *sb = inode->i_sb;
528 struct ntfs_sb_info *sbi = sb->s_fs_info;
529 struct ntfs_inode *ni = ntfs_i(inode);
530 struct page *page = bh->b_page;
531 u8 cluster_bits = sbi->cluster_bits;
532 u32 block_size = sb->s_blocksize;
533 u64 bytes, lbo, valid;
539 /* Clear previous state. */
540 clear_buffer_new(bh);
541 clear_buffer_uptodate(bh);
543 /* Direct write uses 'create=0'. */
544 if (!create && vbo >= ni->i_valid) {
549 if (vbo >= inode->i_size) {
554 if (is_resident(ni)) {
556 err = attr_data_read_resident(ni, page);
560 set_buffer_uptodate(bh);
561 bh->b_size = block_size;
565 vcn = vbo >> cluster_bits;
566 off = vbo & sbi->cluster_mask;
569 err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
576 bytes = ((u64)len << cluster_bits) - off;
578 if (lcn == SPARSE_LCN) {
580 if (bh->b_size > bytes)
589 if ((len << cluster_bits) > block_size)
590 ntfs_sparse_cluster(inode, page, vcn, len);
593 lbo = ((u64)lcn << cluster_bits) + off;
595 set_buffer_mapped(bh);
596 bh->b_bdev = sb->s_bdev;
597 bh->b_blocknr = lbo >> sb->s_blocksize_bits;
601 if (ctx == GET_BLOCK_DIRECT_IO_W) {
602 /* ntfs_direct_IO will update ni->i_valid. */
607 if (bytes > bh->b_size)
613 if (vbo + bytes > valid) {
614 ni->i_valid = vbo + bytes;
615 mark_inode_dirty(inode);
617 } else if (vbo >= valid) {
618 /* Read out of valid data. */
619 /* Should never be here 'cause already checked. */
620 clear_buffer_mapped(bh);
621 } else if (vbo + bytes <= valid) {
623 } else if (vbo + block_size <= valid) {
624 /* Normal short read. */
628 * Read across valid size: vbo < valid && valid < vbo + block_size
633 u32 voff = valid - vbo;
635 bh->b_size = block_size;
636 off = vbo & (PAGE_SIZE - 1);
637 set_bh_page(bh, page, off);
638 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
640 if (!buffer_uptodate(bh)) {
644 zero_user_segment(page, off + voff, off + block_size);
648 if (bh->b_size > bytes)
652 if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
653 static_assert(sizeof(size_t) < sizeof(loff_t));
654 if (bytes > 0x40000000u)
655 bh->b_size = 0x40000000u;
665 int ntfs_get_block(struct inode *inode, sector_t vbn,
666 struct buffer_head *bh_result, int create)
668 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
669 bh_result, create, GET_BLOCK_GENERAL);
672 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
673 struct buffer_head *bh_result, int create)
675 return ntfs_get_block_vbo(inode,
676 (u64)vsn << inode->i_sb->s_blocksize_bits,
677 bh_result, create, GET_BLOCK_BMAP);
680 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
682 return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
685 static int ntfs_readpage(struct file *file, struct page *page)
688 struct address_space *mapping = page->mapping;
689 struct inode *inode = mapping->host;
690 struct ntfs_inode *ni = ntfs_i(inode);
692 if (is_resident(ni)) {
694 err = attr_data_read_resident(ni, page);
696 if (err != E_NTFS_NONRESIDENT) {
702 if (is_compressed(ni)) {
704 err = ni_readpage_cmpr(ni, page);
709 /* Normal + sparse files. */
710 return mpage_readpage(page, ntfs_get_block);
713 static void ntfs_readahead(struct readahead_control *rac)
715 struct address_space *mapping = rac->mapping;
716 struct inode *inode = mapping->host;
717 struct ntfs_inode *ni = ntfs_i(inode);
721 if (is_resident(ni)) {
722 /* No readahead for resident. */
726 if (is_compressed(ni)) {
727 /* No readahead for compressed. */
732 pos = readahead_pos(rac);
734 if (valid < i_size_read(inode) && pos <= valid &&
735 valid < pos + readahead_length(rac)) {
736 /* Range cross 'valid'. Read it page by page. */
740 mpage_readahead(rac, ntfs_get_block);
743 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
744 struct buffer_head *bh_result, int create)
746 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
747 bh_result, create, GET_BLOCK_DIRECT_IO_R);
750 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
751 struct buffer_head *bh_result, int create)
753 return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
754 bh_result, create, GET_BLOCK_DIRECT_IO_W);
757 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
759 struct file *file = iocb->ki_filp;
760 struct address_space *mapping = file->f_mapping;
761 struct inode *inode = mapping->host;
762 struct ntfs_inode *ni = ntfs_i(inode);
763 loff_t vbo = iocb->ki_pos;
765 int wr = iov_iter_rw(iter) & WRITE;
769 if (is_resident(ni)) {
770 /* Switch to buffered write. */
775 ret = blockdev_direct_IO(iocb, inode, iter,
776 wr ? ntfs_get_block_direct_IO_W
777 : ntfs_get_block_direct_IO_R);
785 if (end > valid && !S_ISBLK(inode->i_mode)) {
787 mark_inode_dirty(inode);
789 } else if (vbo < valid && valid < end) {
791 iov_iter_revert(iter, end - valid);
792 iov_iter_zero(end - valid, iter);
799 int ntfs_set_size(struct inode *inode, u64 new_size)
801 struct super_block *sb = inode->i_sb;
802 struct ntfs_sb_info *sbi = sb->s_fs_info;
803 struct ntfs_inode *ni = ntfs_i(inode);
806 /* Check for maximum file size. */
807 if (is_sparsed(ni) || is_compressed(ni)) {
808 if (new_size > sbi->maxbytes_sparse) {
812 } else if (new_size > sbi->maxbytes) {
818 down_write(&ni->file.run_lock);
820 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
821 &ni->i_valid, true, NULL);
823 up_write(&ni->file.run_lock);
826 mark_inode_dirty(inode);
832 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
834 struct address_space *mapping = page->mapping;
835 struct inode *inode = mapping->host;
836 struct ntfs_inode *ni = ntfs_i(inode);
839 if (is_resident(ni)) {
841 err = attr_data_write_resident(ni, page);
843 if (err != E_NTFS_NONRESIDENT) {
849 return block_write_full_page(page, ntfs_get_block, wbc);
852 static int ntfs_writepages(struct address_space *mapping,
853 struct writeback_control *wbc)
855 struct inode *inode = mapping->host;
856 struct ntfs_inode *ni = ntfs_i(inode);
857 /* Redirect call to 'ntfs_writepage' for resident files. */
858 get_block_t *get_block = is_resident(ni) ? NULL : &ntfs_get_block;
860 return mpage_writepages(mapping, wbc, get_block);
863 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
864 struct buffer_head *bh_result, int create)
866 return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
867 bh_result, create, GET_BLOCK_WRITE_BEGIN);
870 static int ntfs_write_begin(struct file *file, struct address_space *mapping,
871 loff_t pos, u32 len, u32 flags, struct page **pagep,
875 struct inode *inode = mapping->host;
876 struct ntfs_inode *ni = ntfs_i(inode);
879 if (is_resident(ni)) {
880 struct page *page = grab_cache_page_write_begin(
881 mapping, pos >> PAGE_SHIFT, flags);
889 err = attr_data_read_resident(ni, page);
899 if (err != E_NTFS_NONRESIDENT)
903 err = block_write_begin(mapping, pos, len, flags, pagep,
904 ntfs_get_block_write_begin);
911 * ntfs_write_end - Address_space_operations::write_end.
913 static int ntfs_write_end(struct file *file, struct address_space *mapping,
914 loff_t pos, u32 len, u32 copied, struct page *page,
918 struct inode *inode = mapping->host;
919 struct ntfs_inode *ni = ntfs_i(inode);
920 u64 valid = ni->i_valid;
924 if (is_resident(ni)) {
926 err = attr_data_write_resident(ni, page);
930 /* Clear any buffers in page. */
931 if (page_has_buffers(page)) {
932 struct buffer_head *head, *bh;
934 bh = head = page_buffers(page);
936 clear_buffer_dirty(bh);
937 clear_buffer_mapped(bh);
938 set_buffer_uptodate(bh);
939 } while (head != (bh = bh->b_this_page));
941 SetPageUptodate(page);
947 err = generic_write_end(file, mapping, pos, len, copied, page,
952 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
953 inode->i_ctime = inode->i_mtime = current_time(inode);
954 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
958 if (valid != ni->i_valid) {
959 /* ni->i_valid is changed in ntfs_get_block_vbo. */
964 mark_inode_dirty(inode);
970 int reset_log_file(struct inode *inode)
974 u32 log_size = inode->i_size;
975 struct address_space *mapping = inode->i_mapping;
982 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
984 err = block_write_begin(mapping, pos, len, 0, &page,
985 ntfs_get_block_write_begin);
989 kaddr = kmap_atomic(page);
990 memset(kaddr, -1, len);
991 kunmap_atomic(kaddr);
992 flush_dcache_page(page);
994 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
1001 balance_dirty_pages_ratelimited(mapping);
1004 mark_inode_dirty_sync(inode);
1009 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1011 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1014 int ntfs_sync_inode(struct inode *inode)
1016 return _ni_write_inode(inode, 1);
1020 * writeback_inode - Helper function for ntfs_flush_inodes().
1022 * This writes both the inode and the file data blocks, waiting
1023 * for in flight data blocks before the start of the call. It
1024 * does not wait for any io started during the call.
1026 static int writeback_inode(struct inode *inode)
1028 int ret = sync_inode_metadata(inode, 0);
1031 ret = filemap_fdatawrite(inode->i_mapping);
1038 * Write data and metadata corresponding to i1 and i2. The io is
1039 * started but we do not wait for any of it to finish.
1041 * filemap_flush() is used for the block device, so if there is a dirty
1042 * page for a block already in flight, we will not wait and start the
1045 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1051 ret = writeback_inode(i1);
1053 ret = writeback_inode(i2);
1055 ret = filemap_flush(sb->s_bdev->bd_inode->i_mapping);
1059 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1063 /* Write non resident data. */
1064 for (idx = 0; bytes; idx++) {
1065 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1066 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1069 return PTR_ERR(page);
1072 WARN_ON(!PageUptodate(page));
1073 ClearPageUptodate(page);
1075 memcpy(page_address(page), data, op);
1077 flush_dcache_page(page);
1078 SetPageUptodate(page);
1081 ntfs_unmap_page(page);
1084 data = Add2Ptr(data, PAGE_SIZE);
1090 * ntfs_reparse_bytes
1092 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1093 * for unicode string of @uni_len length.
1095 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1097 /* Header + unicode string + decorated unicode string. */
1098 return sizeof(short) * (2 * uni_len + 4) +
1099 offsetof(struct REPARSE_DATA_BUFFER,
1100 SymbolicLinkReparseBuffer.PathBuffer);
1103 static struct REPARSE_DATA_BUFFER *
1104 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1105 u32 size, u16 *nsize)
1108 struct REPARSE_DATA_BUFFER *rp;
1110 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1112 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1114 return ERR_PTR(-ENOMEM);
1116 rs = &rp->SymbolicLinkReparseBuffer;
1117 rp_name = rs->PathBuffer;
1119 /* Convert link name to UTF-16. */
1120 err = ntfs_nls_to_utf16(sbi, symname, size,
1121 (struct cpu_str *)(rp_name - 1), 2 * size,
1122 UTF16_LITTLE_ENDIAN);
1126 /* err = the length of unicode name of symlink. */
1127 *nsize = ntfs_reparse_bytes(err);
1129 if (*nsize > sbi->reparse.max_size) {
1134 /* Translate Linux '/' into Windows '\'. */
1135 for (i = 0; i < err; i++) {
1136 if (rp_name[i] == cpu_to_le16('/'))
1137 rp_name[i] = cpu_to_le16('\\');
1140 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1141 rp->ReparseDataLength =
1142 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1143 SymbolicLinkReparseBuffer));
1145 /* PrintName + SubstituteName. */
1146 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1147 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1148 rs->PrintNameLength = rs->SubstituteNameOffset;
1151 * TODO: Use relative path if possible to allow Windows to
1153 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1157 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1159 /* Decorate SubstituteName. */
1161 rp_name[0] = cpu_to_le16('\\');
1162 rp_name[1] = cpu_to_le16('?');
1163 rp_name[2] = cpu_to_le16('?');
1164 rp_name[3] = cpu_to_le16('\\');
1169 return ERR_PTR(err);
1172 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1173 struct inode *dir, struct dentry *dentry,
1174 const struct cpu_str *uni, umode_t mode,
1175 dev_t dev, const char *symname, u32 size,
1176 struct ntfs_fnd *fnd)
1179 struct super_block *sb = dir->i_sb;
1180 struct ntfs_sb_info *sbi = sb->s_fs_info;
1181 const struct qstr *name = &dentry->d_name;
1183 struct ntfs_inode *dir_ni = ntfs_i(dir);
1184 struct ntfs_inode *ni = NULL;
1185 struct inode *inode = NULL;
1186 struct ATTRIB *attr;
1187 struct ATTR_STD_INFO5 *std5;
1188 struct ATTR_FILE_NAME *fname;
1189 struct MFT_REC *rec;
1190 u32 asize, dsize, sd_size;
1191 enum FILE_ATTRIBUTE fa;
1192 __le32 security_id = SECURITY_ID_INVALID;
1195 u16 t16, nsize = 0, aid = 0;
1196 struct INDEX_ROOT *root, *dir_root;
1197 struct NTFS_DE *e, *new_de = NULL;
1198 struct REPARSE_DATA_BUFFER *rp = NULL;
1199 bool rp_inserted = false;
1201 ni_lock_dir(dir_ni);
1203 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1209 if (S_ISDIR(mode)) {
1210 /* Use parent's directory attributes. */
1211 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1212 FILE_ATTRIBUTE_ARCHIVE;
1214 * By default child directory inherits parent attributes.
1215 * Root directory is hidden + system.
1216 * Make an exception for children in root.
1218 if (dir->i_ino == MFT_REC_ROOT)
1219 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1220 } else if (S_ISLNK(mode)) {
1221 /* It is good idea that link should be the same type (file/dir) as target */
1222 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1225 * Linux: there are dir/file/symlink and so on.
1226 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1227 * It is good idea to create:
1228 * dir + reparse if 'symname' points to directory
1230 * file + reparse if 'symname' points to file
1231 * Unfortunately kern_path hangs if symname contains 'dir'.
1237 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1238 * struct inode *target = d_inode(path.dentry);
1240 * if (S_ISDIR(target->i_mode))
1241 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1242 * // if ( target->i_sb == sb ){
1243 * // use relative path?
1248 } else if (S_ISREG(mode)) {
1249 if (sbi->options->sparse) {
1250 /* Sparsed regular file, cause option 'sparse'. */
1251 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1252 FILE_ATTRIBUTE_ARCHIVE;
1253 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1254 /* Compressed regular file, if parent is compressed. */
1255 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1257 /* Regular file, default attributes. */
1258 fa = FILE_ATTRIBUTE_ARCHIVE;
1261 fa = FILE_ATTRIBUTE_ARCHIVE;
1265 fa |= FILE_ATTRIBUTE_READONLY;
1267 /* Allocate PATH_MAX bytes. */
1268 new_de = __getname();
1274 /* Mark rw ntfs as dirty. it will be cleared at umount. */
1275 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1277 /* Step 1: allocate and fill new mft record. */
1278 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1282 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1288 inode = &ni->vfs_inode;
1289 inode_init_owner(mnt_userns, inode, dir, mode);
1290 mode = inode->i_mode;
1292 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1293 current_time(inode);
1296 rec->hard_links = cpu_to_le16(1);
1297 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1299 /* Get default security id. */
1300 sd = s_default_security;
1301 sd_size = sizeof(s_default_security);
1303 if (is_ntfs3(sbi)) {
1304 security_id = dir_ni->std_security_id;
1305 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1306 security_id = sbi->security.def_security_id;
1308 if (security_id == SECURITY_ID_INVALID &&
1309 !ntfs_insert_security(sbi, sd, sd_size,
1310 &security_id, NULL))
1311 sbi->security.def_security_id = security_id;
1315 /* Insert standard info. */
1316 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1318 if (security_id == SECURITY_ID_INVALID) {
1319 dsize = sizeof(struct ATTR_STD_INFO);
1321 dsize = sizeof(struct ATTR_STD_INFO5);
1322 std5->security_id = security_id;
1323 ni->std_security_id = security_id;
1325 asize = SIZEOF_RESIDENT + dsize;
1327 attr->type = ATTR_STD;
1328 attr->size = cpu_to_le32(asize);
1329 attr->id = cpu_to_le16(aid++);
1330 attr->res.data_off = SIZEOF_RESIDENT_LE;
1331 attr->res.data_size = cpu_to_le32(dsize);
1333 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1334 kernel2nt(&inode->i_atime);
1339 attr = Add2Ptr(attr, asize);
1341 /* Insert file name. */
1342 err = fill_name_de(sbi, new_de, name, uni);
1346 mi_get_ref(&ni->mi, &new_de->ref);
1348 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1349 mi_get_ref(&dir_ni->mi, &fname->home);
1350 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1351 fname->dup.a_time = std5->cr_time;
1352 fname->dup.alloc_size = fname->dup.data_size = 0;
1353 fname->dup.fa = std5->fa;
1354 fname->dup.ea_size = fname->dup.reparse = 0;
1356 dsize = le16_to_cpu(new_de->key_size);
1357 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1359 attr->type = ATTR_NAME;
1360 attr->size = cpu_to_le32(asize);
1361 attr->res.data_off = SIZEOF_RESIDENT_LE;
1362 attr->res.flags = RESIDENT_FLAG_INDEXED;
1363 attr->id = cpu_to_le16(aid++);
1364 attr->res.data_size = cpu_to_le32(dsize);
1365 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1367 attr = Add2Ptr(attr, asize);
1369 if (security_id == SECURITY_ID_INVALID) {
1370 /* Insert security attribute. */
1371 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1373 attr->type = ATTR_SECURE;
1374 attr->size = cpu_to_le32(asize);
1375 attr->id = cpu_to_le16(aid++);
1376 attr->res.data_off = SIZEOF_RESIDENT_LE;
1377 attr->res.data_size = cpu_to_le32(sd_size);
1378 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1380 attr = Add2Ptr(attr, asize);
1383 attr->id = cpu_to_le16(aid++);
1384 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1386 * Regular directory or symlink to directory.
1387 * Create root attribute.
1389 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1390 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1392 attr->type = ATTR_ROOT;
1393 attr->size = cpu_to_le32(asize);
1395 attr->name_len = ARRAY_SIZE(I30_NAME);
1396 attr->name_off = SIZEOF_RESIDENT_LE;
1397 attr->res.data_off =
1398 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1399 attr->res.data_size = cpu_to_le32(dsize);
1400 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1403 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1404 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1406 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1407 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1408 sizeof(struct NTFS_DE));
1409 root->ihdr.total = root->ihdr.used;
1411 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1412 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1413 e->flags = NTFS_IE_LAST;
1414 } else if (S_ISLNK(mode)) {
1417 * Create empty resident data attribute.
1419 asize = SIZEOF_RESIDENT;
1421 /* Insert empty ATTR_DATA */
1422 attr->type = ATTR_DATA;
1423 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1424 attr->name_off = SIZEOF_RESIDENT_LE;
1425 attr->res.data_off = SIZEOF_RESIDENT_LE;
1426 } else if (S_ISREG(mode)) {
1428 * Regular file. Create empty non resident data attribute.
1430 attr->type = ATTR_DATA;
1432 attr->nres.evcn = cpu_to_le64(-1ll);
1433 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1434 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1435 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1436 attr->flags = ATTR_FLAG_SPARSED;
1437 asize = SIZEOF_NONRESIDENT_EX + 8;
1438 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1439 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1440 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1441 attr->flags = ATTR_FLAG_COMPRESSED;
1442 attr->nres.c_unit = COMPRESSION_UNIT;
1443 asize = SIZEOF_NONRESIDENT_EX + 8;
1445 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1446 attr->name_off = SIZEOF_NONRESIDENT_LE;
1447 asize = SIZEOF_NONRESIDENT + 8;
1449 attr->nres.run_off = attr->name_off;
1452 * Node. Create empty resident data attribute.
1454 attr->type = ATTR_DATA;
1455 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1456 attr->name_off = SIZEOF_RESIDENT_LE;
1457 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1458 attr->flags = ATTR_FLAG_SPARSED;
1459 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1460 attr->flags = ATTR_FLAG_COMPRESSED;
1461 attr->res.data_off = SIZEOF_RESIDENT_LE;
1462 asize = SIZEOF_RESIDENT;
1463 ni->ni_flags |= NI_FLAG_RESIDENT;
1466 if (S_ISDIR(mode)) {
1467 ni->ni_flags |= NI_FLAG_DIR;
1468 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1471 } else if (S_ISLNK(mode)) {
1472 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1481 * Insert ATTR_REPARSE.
1483 attr = Add2Ptr(attr, asize);
1484 attr->type = ATTR_REPARSE;
1485 attr->id = cpu_to_le16(aid++);
1487 /* Resident or non resident? */
1488 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1489 t16 = PtrOffset(rec, attr);
1492 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1493 * It is good idea to keep extened attributes resident.
1495 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1497 CLST clst = bytes_to_cluster(sbi, nsize);
1499 /* Bytes per runs. */
1500 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1503 attr->nres.evcn = cpu_to_le64(clst - 1);
1504 attr->name_off = SIZEOF_NONRESIDENT_LE;
1505 attr->nres.run_off = attr->name_off;
1506 attr->nres.data_size = cpu_to_le64(nsize);
1507 attr->nres.valid_size = attr->nres.data_size;
1508 attr->nres.alloc_size =
1509 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1511 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1512 clst, NULL, 0, &alen, 0,
1517 err = run_pack(&ni->file.run, 0, clst,
1518 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1528 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1530 attr->res.data_off = SIZEOF_RESIDENT_LE;
1531 attr->res.data_size = cpu_to_le32(nsize);
1532 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1535 /* Size of symlink equals the length of input string. */
1536 inode->i_size = size;
1538 attr->size = cpu_to_le32(asize);
1540 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1548 attr = Add2Ptr(attr, asize);
1549 attr->type = ATTR_END;
1551 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1552 rec->next_attr_id = cpu_to_le16(aid);
1554 /* Step 2: Add new name in index. */
1555 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1559 /* Unlock parent directory before ntfs_init_acl. */
1562 inode->i_generation = le16_to_cpu(rec->seq);
1564 dir->i_mtime = dir->i_ctime = inode->i_atime;
1566 if (S_ISDIR(mode)) {
1567 inode->i_op = &ntfs_dir_inode_operations;
1568 inode->i_fop = &ntfs_dir_operations;
1569 } else if (S_ISLNK(mode)) {
1570 inode->i_op = &ntfs_link_inode_operations;
1571 inode->i_fop = NULL;
1572 inode->i_mapping->a_ops = &ntfs_aops;
1573 inode->i_size = size;
1574 inode_nohighmem(inode);
1575 } else if (S_ISREG(mode)) {
1576 inode->i_op = &ntfs_file_inode_operations;
1577 inode->i_fop = &ntfs_file_operations;
1578 inode->i_mapping->a_ops =
1579 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1580 init_rwsem(&ni->file.run_lock);
1582 inode->i_op = &ntfs_special_inode_operations;
1583 init_special_inode(inode, mode, dev);
1586 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1587 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1588 err = ntfs_init_acl(mnt_userns, inode, dir);
1594 inode->i_flags |= S_NOSEC;
1597 /* Write non resident data. */
1599 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1605 * Call 'd_instantiate' after inode->i_op is set
1606 * but before finish_open.
1608 d_instantiate(dentry, inode);
1610 ntfs_save_wsl_perm(inode);
1611 mark_inode_dirty(dir);
1612 mark_inode_dirty(inode);
1619 /* Undo 'indx_insert_entry'. */
1620 ni_lock_dir(dir_ni);
1621 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1622 le16_to_cpu(new_de->key_size), sbi);
1623 /* ni_unlock(dir_ni); will be called later. */
1626 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1629 if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1632 run_deallocate(sbi, &ni->file.run, false);
1635 clear_rec_inuse(rec);
1637 ni->mi.dirty = false;
1638 discard_new_inode(inode);
1640 ntfs_mark_rec_free(sbi, ino);
1649 return ERR_PTR(err);
1652 unlock_new_inode(inode);
1657 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1660 struct ntfs_inode *ni = ntfs_i(inode);
1661 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1663 struct ATTR_FILE_NAME *de_name;
1665 /* Allocate PATH_MAX bytes. */
1670 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1671 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1673 /* Construct 'de'. */
1674 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1678 de_name = (struct ATTR_FILE_NAME *)(de + 1);
1679 /* Fill duplicate info. */
1680 de_name->dup.cr_time = de_name->dup.m_time = de_name->dup.c_time =
1681 de_name->dup.a_time = kernel2nt(&inode->i_ctime);
1682 de_name->dup.alloc_size = de_name->dup.data_size =
1683 cpu_to_le64(inode->i_size);
1684 de_name->dup.fa = ni->std_fa;
1685 de_name->dup.ea_size = de_name->dup.reparse = 0;
1687 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1696 * inode_operations::unlink
1697 * inode_operations::rmdir
1699 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1702 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1703 struct inode *inode = d_inode(dentry);
1704 struct ntfs_inode *ni = ntfs_i(inode);
1705 struct ntfs_inode *dir_ni = ntfs_i(dir);
1706 struct NTFS_DE *de, *de2 = NULL;
1709 if (ntfs_is_meta_file(sbi, ni->mi.rno))
1712 /* Allocate PATH_MAX bytes. */
1719 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1724 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1729 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1733 dir->i_mtime = dir->i_ctime = current_time(dir);
1734 mark_inode_dirty(dir);
1735 inode->i_ctime = dir->i_ctime;
1737 mark_inode_dirty(inode);
1738 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1739 make_bad_inode(inode);
1740 ntfs_inode_err(inode, "failed to undo unlink");
1741 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1743 if (ni_is_dirty(dir))
1744 mark_inode_dirty(dir);
1745 if (ni_is_dirty(inode))
1746 mark_inode_dirty(inode);
1755 void ntfs_evict_inode(struct inode *inode)
1757 truncate_inode_pages_final(&inode->i_data);
1760 _ni_write_inode(inode, inode_needs_sync(inode));
1762 invalidate_inode_buffers(inode);
1765 ni_clear(ntfs_i(inode));
1768 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1771 int i, err = -EINVAL;
1772 struct ntfs_inode *ni = ntfs_i(inode);
1773 struct super_block *sb = inode->i_sb;
1774 struct ntfs_sb_info *sbi = sb->s_fs_info;
1777 void *to_free = NULL;
1778 struct REPARSE_DATA_BUFFER *rp;
1779 const __le16 *uname;
1780 struct ATTRIB *attr;
1782 /* Reparse data present. Try to parse it. */
1783 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1784 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1788 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1792 if (!attr->non_res) {
1793 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1796 size = le32_to_cpu(attr->res.data_size);
1798 size = le64_to_cpu(attr->nres.data_size);
1802 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1806 rp = kmalloc(size, GFP_NOFS);
1812 /* Read into temporal buffer. */
1813 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1818 /* Microsoft Tag. */
1819 switch (rp->ReparseTag) {
1820 case IO_REPARSE_TAG_MOUNT_POINT:
1821 /* Mount points and junctions. */
1822 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1823 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1824 MountPointReparseBuffer.PathBuffer))
1827 offsetof(struct REPARSE_DATA_BUFFER,
1828 MountPointReparseBuffer.PathBuffer) +
1829 le16_to_cpu(rp->MountPointReparseBuffer
1831 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1834 case IO_REPARSE_TAG_SYMLINK:
1835 /* FolderSymbolicLink */
1836 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1837 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1838 SymbolicLinkReparseBuffer.PathBuffer))
1841 rp, offsetof(struct REPARSE_DATA_BUFFER,
1842 SymbolicLinkReparseBuffer.PathBuffer) +
1843 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1846 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1849 case IO_REPARSE_TAG_CLOUD:
1850 case IO_REPARSE_TAG_CLOUD_1:
1851 case IO_REPARSE_TAG_CLOUD_2:
1852 case IO_REPARSE_TAG_CLOUD_3:
1853 case IO_REPARSE_TAG_CLOUD_4:
1854 case IO_REPARSE_TAG_CLOUD_5:
1855 case IO_REPARSE_TAG_CLOUD_6:
1856 case IO_REPARSE_TAG_CLOUD_7:
1857 case IO_REPARSE_TAG_CLOUD_8:
1858 case IO_REPARSE_TAG_CLOUD_9:
1859 case IO_REPARSE_TAG_CLOUD_A:
1860 case IO_REPARSE_TAG_CLOUD_B:
1861 case IO_REPARSE_TAG_CLOUD_C:
1862 case IO_REPARSE_TAG_CLOUD_D:
1863 case IO_REPARSE_TAG_CLOUD_E:
1864 case IO_REPARSE_TAG_CLOUD_F:
1865 err = sizeof("OneDrive") - 1;
1868 memcpy(buffer, "OneDrive", err);
1872 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1873 /* Unknown Microsoft Tag. */
1876 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1877 size <= sizeof(struct REPARSE_POINT)) {
1882 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1883 ulen = le16_to_cpu(rp->ReparseDataLength) -
1884 sizeof(struct REPARSE_POINT);
1887 /* Convert nlen from bytes to UNICODE chars. */
1890 /* Check that name is available. */
1891 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1894 /* If name is already zero terminated then truncate it now. */
1895 if (!uname[ulen - 1])
1898 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1903 /* Translate Windows '\' into Linux '/'. */
1904 for (i = 0; i < err; i++) {
1905 if (buffer[i] == '\\')
1909 /* Always set last zero. */
1916 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1917 struct delayed_call *done)
1923 return ERR_PTR(-ECHILD);
1925 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1927 return ERR_PTR(-ENOMEM);
1929 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1932 return ERR_PTR(err);
1935 set_delayed_call(done, kfree_link, ret);
1941 const struct inode_operations ntfs_link_inode_operations = {
1942 .get_link = ntfs_get_link,
1943 .setattr = ntfs3_setattr,
1944 .listxattr = ntfs_listxattr,
1945 .permission = ntfs_permission,
1946 .get_acl = ntfs_get_acl,
1947 .set_acl = ntfs_set_acl,
1950 const struct address_space_operations ntfs_aops = {
1951 .readpage = ntfs_readpage,
1952 .readahead = ntfs_readahead,
1953 .writepage = ntfs_writepage,
1954 .writepages = ntfs_writepages,
1955 .write_begin = ntfs_write_begin,
1956 .write_end = ntfs_write_end,
1957 .direct_IO = ntfs_direct_IO,
1959 .set_page_dirty = __set_page_dirty_buffers,
1962 const struct address_space_operations ntfs_aops_cmpr = {
1963 .readpage = ntfs_readpage,
1964 .readahead = ntfs_readahead,