Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *
0004  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
0005  *
0006  */
0007 
0008 #include <linux/buffer_head.h>
0009 #include <linux/fs.h>
0010 #include <linux/mpage.h>
0011 #include <linux/namei.h>
0012 #include <linux/nls.h>
0013 #include <linux/uio.h>
0014 #include <linux/writeback.h>
0015 
0016 #include "debug.h"
0017 #include "ntfs.h"
0018 #include "ntfs_fs.h"
0019 
0020 /*
0021  * ntfs_read_mft - Read record and parses MFT.
0022  */
0023 static struct inode *ntfs_read_mft(struct inode *inode,
0024                    const struct cpu_str *name,
0025                    const struct MFT_REF *ref)
0026 {
0027     int err = 0;
0028     struct ntfs_inode *ni = ntfs_i(inode);
0029     struct super_block *sb = inode->i_sb;
0030     struct ntfs_sb_info *sbi = sb->s_fs_info;
0031     mode_t mode = 0;
0032     struct ATTR_STD_INFO5 *std5 = NULL;
0033     struct ATTR_LIST_ENTRY *le;
0034     struct ATTRIB *attr;
0035     bool is_match = false;
0036     bool is_root = false;
0037     bool is_dir;
0038     unsigned long ino = inode->i_ino;
0039     u32 rp_fa = 0, asize, t32;
0040     u16 roff, rsize, names = 0;
0041     const struct ATTR_FILE_NAME *fname = NULL;
0042     const struct INDEX_ROOT *root;
0043     struct REPARSE_DATA_BUFFER rp; // 0x18 bytes
0044     u64 t64;
0045     struct MFT_REC *rec;
0046     struct runs_tree *run;
0047 
0048     inode->i_op = NULL;
0049     /* Setup 'uid' and 'gid' */
0050     inode->i_uid = sbi->options->fs_uid;
0051     inode->i_gid = sbi->options->fs_gid;
0052 
0053     err = mi_init(&ni->mi, sbi, ino);
0054     if (err)
0055         goto out;
0056 
0057     if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
0058         t64 = sbi->mft.lbo >> sbi->cluster_bits;
0059         t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
0060         sbi->mft.ni = ni;
0061         init_rwsem(&ni->file.run_lock);
0062 
0063         if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
0064             err = -ENOMEM;
0065             goto out;
0066         }
0067     }
0068 
0069     err = mi_read(&ni->mi, ino == MFT_REC_MFT);
0070 
0071     if (err)
0072         goto out;
0073 
0074     rec = ni->mi.mrec;
0075 
0076     if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
0077         ;
0078     } else if (ref->seq != rec->seq) {
0079         err = -EINVAL;
0080         ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
0081              le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
0082         goto out;
0083     } else if (!is_rec_inuse(rec)) {
0084         err = -EINVAL;
0085         ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
0086         goto out;
0087     }
0088 
0089     if (le32_to_cpu(rec->total) != sbi->record_size) {
0090         /* Bad inode? */
0091         err = -EINVAL;
0092         goto out;
0093     }
0094 
0095     if (!is_rec_base(rec))
0096         goto Ok;
0097 
0098     /* Record should contain $I30 root. */
0099     is_dir = rec->flags & RECORD_FLAG_DIR;
0100 
0101     inode->i_generation = le16_to_cpu(rec->seq);
0102 
0103     /* Enumerate all struct Attributes MFT. */
0104     le = NULL;
0105     attr = NULL;
0106 
0107     /*
0108      * To reduce tab pressure use goto instead of
0109      * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
0110      */
0111 next_attr:
0112     run = NULL;
0113     err = -EINVAL;
0114     attr = ni_enum_attr_ex(ni, attr, &le, NULL);
0115     if (!attr)
0116         goto end_enum;
0117 
0118     if (le && le->vcn) {
0119         /* This is non primary attribute segment. Ignore if not MFT. */
0120         if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
0121             goto next_attr;
0122 
0123         run = &ni->file.run;
0124         asize = le32_to_cpu(attr->size);
0125         goto attr_unpack_run;
0126     }
0127 
0128     roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
0129     rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
0130     asize = le32_to_cpu(attr->size);
0131 
0132     switch (attr->type) {
0133     case ATTR_STD:
0134         if (attr->non_res ||
0135             asize < sizeof(struct ATTR_STD_INFO) + roff ||
0136             rsize < sizeof(struct ATTR_STD_INFO))
0137             goto out;
0138 
0139         if (std5)
0140             goto next_attr;
0141 
0142         std5 = Add2Ptr(attr, roff);
0143 
0144 #ifdef STATX_BTIME
0145         nt2kernel(std5->cr_time, &ni->i_crtime);
0146 #endif
0147         nt2kernel(std5->a_time, &inode->i_atime);
0148         nt2kernel(std5->c_time, &inode->i_ctime);
0149         nt2kernel(std5->m_time, &inode->i_mtime);
0150 
0151         ni->std_fa = std5->fa;
0152 
0153         if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
0154             rsize >= sizeof(struct ATTR_STD_INFO5))
0155             ni->std_security_id = std5->security_id;
0156         goto next_attr;
0157 
0158     case ATTR_LIST:
0159         if (attr->name_len || le || ino == MFT_REC_LOG)
0160             goto out;
0161 
0162         err = ntfs_load_attr_list(ni, attr);
0163         if (err)
0164             goto out;
0165 
0166         le = NULL;
0167         attr = NULL;
0168         goto next_attr;
0169 
0170     case ATTR_NAME:
0171         if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
0172             rsize < SIZEOF_ATTRIBUTE_FILENAME)
0173             goto out;
0174 
0175         fname = Add2Ptr(attr, roff);
0176         if (fname->type == FILE_NAME_DOS)
0177             goto next_attr;
0178 
0179         names += 1;
0180         if (name && name->len == fname->name_len &&
0181             !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
0182                     NULL, false))
0183             is_match = true;
0184 
0185         goto next_attr;
0186 
0187     case ATTR_DATA:
0188         if (is_dir) {
0189             /* Ignore data attribute in dir record. */
0190             goto next_attr;
0191         }
0192 
0193         if (ino == MFT_REC_BADCLUST && !attr->non_res)
0194             goto next_attr;
0195 
0196         if (attr->name_len &&
0197             ((ino != MFT_REC_BADCLUST || !attr->non_res ||
0198               attr->name_len != ARRAY_SIZE(BAD_NAME) ||
0199               memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
0200              (ino != MFT_REC_SECURE || !attr->non_res ||
0201               attr->name_len != ARRAY_SIZE(SDS_NAME) ||
0202               memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
0203             /* File contains stream attribute. Ignore it. */
0204             goto next_attr;
0205         }
0206 
0207         if (is_attr_sparsed(attr))
0208             ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
0209         else
0210             ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
0211 
0212         if (is_attr_compressed(attr))
0213             ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
0214         else
0215             ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
0216 
0217         if (is_attr_encrypted(attr))
0218             ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
0219         else
0220             ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
0221 
0222         if (!attr->non_res) {
0223             ni->i_valid = inode->i_size = rsize;
0224             inode_set_bytes(inode, rsize);
0225         }
0226 
0227         mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
0228 
0229         if (!attr->non_res) {
0230             ni->ni_flags |= NI_FLAG_RESIDENT;
0231             goto next_attr;
0232         }
0233 
0234         inode_set_bytes(inode, attr_ondisk_size(attr));
0235 
0236         ni->i_valid = le64_to_cpu(attr->nres.valid_size);
0237         inode->i_size = le64_to_cpu(attr->nres.data_size);
0238         if (!attr->nres.alloc_size)
0239             goto next_attr;
0240 
0241         run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
0242                         : &ni->file.run;
0243         break;
0244 
0245     case ATTR_ROOT:
0246         if (attr->non_res)
0247             goto out;
0248 
0249         root = Add2Ptr(attr, roff);
0250         is_root = true;
0251 
0252         if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
0253             memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
0254             goto next_attr;
0255 
0256         if (root->type != ATTR_NAME ||
0257             root->rule != NTFS_COLLATION_TYPE_FILENAME)
0258             goto out;
0259 
0260         if (!is_dir)
0261             goto next_attr;
0262 
0263         ni->ni_flags |= NI_FLAG_DIR;
0264 
0265         err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
0266         if (err)
0267             goto out;
0268 
0269         mode = sb->s_root
0270                    ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
0271                    : (S_IFDIR | 0777);
0272         goto next_attr;
0273 
0274     case ATTR_ALLOC:
0275         if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
0276             memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
0277             goto next_attr;
0278 
0279         inode->i_size = le64_to_cpu(attr->nres.data_size);
0280         ni->i_valid = le64_to_cpu(attr->nres.valid_size);
0281         inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
0282 
0283         run = &ni->dir.alloc_run;
0284         break;
0285 
0286     case ATTR_BITMAP:
0287         if (ino == MFT_REC_MFT) {
0288             if (!attr->non_res)
0289                 goto out;
0290 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
0291             /* 0x20000000 = 2^32 / 8 */
0292             if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
0293                 goto out;
0294 #endif
0295             run = &sbi->mft.bitmap.run;
0296             break;
0297         } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
0298                !memcmp(attr_name(attr), I30_NAME,
0299                    sizeof(I30_NAME)) &&
0300                attr->non_res) {
0301             run = &ni->dir.bitmap_run;
0302             break;
0303         }
0304         goto next_attr;
0305 
0306     case ATTR_REPARSE:
0307         if (attr->name_len)
0308             goto next_attr;
0309 
0310         rp_fa = ni_parse_reparse(ni, attr, &rp);
0311         switch (rp_fa) {
0312         case REPARSE_LINK:
0313             /*
0314              * Normal symlink.
0315              * Assume one unicode symbol == one utf8.
0316              */
0317             inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
0318                                 .PrintNameLength) /
0319                     sizeof(u16);
0320 
0321             ni->i_valid = inode->i_size;
0322 
0323             /* Clear directory bit. */
0324             if (ni->ni_flags & NI_FLAG_DIR) {
0325                 indx_clear(&ni->dir);
0326                 memset(&ni->dir, 0, sizeof(ni->dir));
0327                 ni->ni_flags &= ~NI_FLAG_DIR;
0328             } else {
0329                 run_close(&ni->file.run);
0330             }
0331             mode = S_IFLNK | 0777;
0332             is_dir = false;
0333             if (attr->non_res) {
0334                 run = &ni->file.run;
0335                 goto attr_unpack_run; // Double break.
0336             }
0337             break;
0338 
0339         case REPARSE_COMPRESSED:
0340             break;
0341 
0342         case REPARSE_DEDUPLICATED:
0343             break;
0344         }
0345         goto next_attr;
0346 
0347     case ATTR_EA_INFO:
0348         if (!attr->name_len &&
0349             resident_data_ex(attr, sizeof(struct EA_INFO))) {
0350             ni->ni_flags |= NI_FLAG_EA;
0351             /*
0352              * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
0353              */
0354             inode->i_mode = mode;
0355             ntfs_get_wsl_perm(inode);
0356             mode = inode->i_mode;
0357         }
0358         goto next_attr;
0359 
0360     default:
0361         goto next_attr;
0362     }
0363 
0364 attr_unpack_run:
0365     roff = le16_to_cpu(attr->nres.run_off);
0366 
0367     t64 = le64_to_cpu(attr->nres.svcn);
0368     err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
0369                 t64, Add2Ptr(attr, roff), asize - roff);
0370     if (err < 0)
0371         goto out;
0372     err = 0;
0373     goto next_attr;
0374 
0375 end_enum:
0376 
0377     if (!std5)
0378         goto out;
0379 
0380     if (!is_match && name) {
0381         /* Reuse rec as buffer for ascii name. */
0382         err = -ENOENT;
0383         goto out;
0384     }
0385 
0386     if (std5->fa & FILE_ATTRIBUTE_READONLY)
0387         mode &= ~0222;
0388 
0389     if (!names) {
0390         err = -EINVAL;
0391         goto out;
0392     }
0393 
0394     if (names != le16_to_cpu(rec->hard_links)) {
0395         /* Correct minor error on the fly. Do not mark inode as dirty. */
0396         rec->hard_links = cpu_to_le16(names);
0397         ni->mi.dirty = true;
0398     }
0399 
0400     set_nlink(inode, names);
0401 
0402     if (S_ISDIR(mode)) {
0403         ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
0404 
0405         /*
0406          * Dot and dot-dot should be included in count but was not
0407          * included in enumeration.
0408          * Usually a hard links to directories are disabled.
0409          */
0410         inode->i_op = &ntfs_dir_inode_operations;
0411         inode->i_fop = &ntfs_dir_operations;
0412         ni->i_valid = 0;
0413     } else if (S_ISLNK(mode)) {
0414         ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
0415         inode->i_op = &ntfs_link_inode_operations;
0416         inode->i_fop = NULL;
0417         inode_nohighmem(inode);
0418     } else if (S_ISREG(mode)) {
0419         ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
0420         inode->i_op = &ntfs_file_inode_operations;
0421         inode->i_fop = &ntfs_file_operations;
0422         inode->i_mapping->a_ops =
0423             is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
0424         if (ino != MFT_REC_MFT)
0425             init_rwsem(&ni->file.run_lock);
0426     } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
0427            S_ISSOCK(mode)) {
0428         inode->i_op = &ntfs_special_inode_operations;
0429         init_special_inode(inode, mode, inode->i_rdev);
0430     } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
0431            fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
0432         /* Records in $Extend are not a files or general directories. */
0433         inode->i_op = &ntfs_file_inode_operations;
0434     } else {
0435         err = -EINVAL;
0436         goto out;
0437     }
0438 
0439     if ((sbi->options->sys_immutable &&
0440          (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
0441         !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
0442         inode->i_flags |= S_IMMUTABLE;
0443     } else {
0444         inode->i_flags &= ~S_IMMUTABLE;
0445     }
0446 
0447     inode->i_mode = mode;
0448     if (!(ni->ni_flags & NI_FLAG_EA)) {
0449         /* If no xattr then no security (stored in xattr). */
0450         inode->i_flags |= S_NOSEC;
0451     }
0452 
0453 Ok:
0454     if (ino == MFT_REC_MFT && !sb->s_root)
0455         sbi->mft.ni = NULL;
0456 
0457     unlock_new_inode(inode);
0458 
0459     return inode;
0460 
0461 out:
0462     if (ino == MFT_REC_MFT && !sb->s_root)
0463         sbi->mft.ni = NULL;
0464 
0465     iget_failed(inode);
0466     return ERR_PTR(err);
0467 }
0468 
0469 /*
0470  * ntfs_test_inode
0471  *
0472  * Return: 1 if match.
0473  */
0474 static int ntfs_test_inode(struct inode *inode, void *data)
0475 {
0476     struct MFT_REF *ref = data;
0477 
0478     return ino_get(ref) == inode->i_ino;
0479 }
0480 
0481 static int ntfs_set_inode(struct inode *inode, void *data)
0482 {
0483     const struct MFT_REF *ref = data;
0484 
0485     inode->i_ino = ino_get(ref);
0486     return 0;
0487 }
0488 
0489 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
0490              const struct cpu_str *name)
0491 {
0492     struct inode *inode;
0493 
0494     inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
0495                  (void *)ref);
0496     if (unlikely(!inode))
0497         return ERR_PTR(-ENOMEM);
0498 
0499     /* If this is a freshly allocated inode, need to read it now. */
0500     if (inode->i_state & I_NEW)
0501         inode = ntfs_read_mft(inode, name, ref);
0502     else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
0503         /* Inode overlaps? */
0504         _ntfs_bad_inode(inode);
0505     }
0506 
0507     return inode;
0508 }
0509 
0510 enum get_block_ctx {
0511     GET_BLOCK_GENERAL = 0,
0512     GET_BLOCK_WRITE_BEGIN = 1,
0513     GET_BLOCK_DIRECT_IO_R = 2,
0514     GET_BLOCK_DIRECT_IO_W = 3,
0515     GET_BLOCK_BMAP = 4,
0516 };
0517 
0518 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
0519                        struct buffer_head *bh, int create,
0520                        enum get_block_ctx ctx)
0521 {
0522     struct super_block *sb = inode->i_sb;
0523     struct ntfs_sb_info *sbi = sb->s_fs_info;
0524     struct ntfs_inode *ni = ntfs_i(inode);
0525     struct page *page = bh->b_page;
0526     u8 cluster_bits = sbi->cluster_bits;
0527     u32 block_size = sb->s_blocksize;
0528     u64 bytes, lbo, valid;
0529     u32 off;
0530     int err;
0531     CLST vcn, lcn, len;
0532     bool new;
0533 
0534     /* Clear previous state. */
0535     clear_buffer_new(bh);
0536     clear_buffer_uptodate(bh);
0537 
0538     /* Direct write uses 'create=0'. */
0539     if (!create && vbo >= ni->i_valid) {
0540         /* Out of valid. */
0541         return 0;
0542     }
0543 
0544     if (vbo >= inode->i_size) {
0545         /* Out of size. */
0546         return 0;
0547     }
0548 
0549     if (is_resident(ni)) {
0550         ni_lock(ni);
0551         err = attr_data_read_resident(ni, page);
0552         ni_unlock(ni);
0553 
0554         if (!err)
0555             set_buffer_uptodate(bh);
0556         bh->b_size = block_size;
0557         return err;
0558     }
0559 
0560     vcn = vbo >> cluster_bits;
0561     off = vbo & sbi->cluster_mask;
0562     new = false;
0563 
0564     err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
0565     if (err)
0566         goto out;
0567 
0568     if (!len)
0569         return 0;
0570 
0571     bytes = ((u64)len << cluster_bits) - off;
0572 
0573     if (lcn == SPARSE_LCN) {
0574         if (!create) {
0575             if (bh->b_size > bytes)
0576                 bh->b_size = bytes;
0577             return 0;
0578         }
0579         WARN_ON(1);
0580     }
0581 
0582     if (new) {
0583         set_buffer_new(bh);
0584         if ((len << cluster_bits) > block_size)
0585             ntfs_sparse_cluster(inode, page, vcn, len);
0586     }
0587 
0588     lbo = ((u64)lcn << cluster_bits) + off;
0589 
0590     set_buffer_mapped(bh);
0591     bh->b_bdev = sb->s_bdev;
0592     bh->b_blocknr = lbo >> sb->s_blocksize_bits;
0593 
0594     valid = ni->i_valid;
0595 
0596     if (ctx == GET_BLOCK_DIRECT_IO_W) {
0597         /* ntfs_direct_IO will update ni->i_valid. */
0598         if (vbo >= valid)
0599             set_buffer_new(bh);
0600     } else if (create) {
0601         /* Normal write. */
0602         if (bytes > bh->b_size)
0603             bytes = bh->b_size;
0604 
0605         if (vbo >= valid)
0606             set_buffer_new(bh);
0607 
0608         if (vbo + bytes > valid) {
0609             ni->i_valid = vbo + bytes;
0610             mark_inode_dirty(inode);
0611         }
0612     } else if (vbo >= valid) {
0613         /* Read out of valid data. */
0614         /* Should never be here 'cause already checked. */
0615         clear_buffer_mapped(bh);
0616     } else if (vbo + bytes <= valid) {
0617         /* Normal read. */
0618     } else if (vbo + block_size <= valid) {
0619         /* Normal short read. */
0620         bytes = block_size;
0621     } else {
0622         /*
0623          * Read across valid size: vbo < valid && valid < vbo + block_size
0624          */
0625         bytes = block_size;
0626 
0627         if (page) {
0628             u32 voff = valid - vbo;
0629 
0630             bh->b_size = block_size;
0631             off = vbo & (PAGE_SIZE - 1);
0632             set_bh_page(bh, page, off);
0633             ll_rw_block(REQ_OP_READ, 1, &bh);
0634             wait_on_buffer(bh);
0635             if (!buffer_uptodate(bh)) {
0636                 err = -EIO;
0637                 goto out;
0638             }
0639             zero_user_segment(page, off + voff, off + block_size);
0640         }
0641     }
0642 
0643     if (bh->b_size > bytes)
0644         bh->b_size = bytes;
0645 
0646 #ifndef __LP64__
0647     if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
0648         static_assert(sizeof(size_t) < sizeof(loff_t));
0649         if (bytes > 0x40000000u)
0650             bh->b_size = 0x40000000u;
0651     }
0652 #endif
0653 
0654     return 0;
0655 
0656 out:
0657     return err;
0658 }
0659 
0660 int ntfs_get_block(struct inode *inode, sector_t vbn,
0661            struct buffer_head *bh_result, int create)
0662 {
0663     return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
0664                   bh_result, create, GET_BLOCK_GENERAL);
0665 }
0666 
0667 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
0668                    struct buffer_head *bh_result, int create)
0669 {
0670     return ntfs_get_block_vbo(inode,
0671                   (u64)vsn << inode->i_sb->s_blocksize_bits,
0672                   bh_result, create, GET_BLOCK_BMAP);
0673 }
0674 
0675 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
0676 {
0677     return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
0678 }
0679 
0680 static int ntfs_read_folio(struct file *file, struct folio *folio)
0681 {
0682     struct page *page = &folio->page;
0683     int err;
0684     struct address_space *mapping = page->mapping;
0685     struct inode *inode = mapping->host;
0686     struct ntfs_inode *ni = ntfs_i(inode);
0687 
0688     if (is_resident(ni)) {
0689         ni_lock(ni);
0690         err = attr_data_read_resident(ni, page);
0691         ni_unlock(ni);
0692         if (err != E_NTFS_NONRESIDENT) {
0693             unlock_page(page);
0694             return err;
0695         }
0696     }
0697 
0698     if (is_compressed(ni)) {
0699         ni_lock(ni);
0700         err = ni_readpage_cmpr(ni, page);
0701         ni_unlock(ni);
0702         return err;
0703     }
0704 
0705     /* Normal + sparse files. */
0706     return mpage_read_folio(folio, ntfs_get_block);
0707 }
0708 
0709 static void ntfs_readahead(struct readahead_control *rac)
0710 {
0711     struct address_space *mapping = rac->mapping;
0712     struct inode *inode = mapping->host;
0713     struct ntfs_inode *ni = ntfs_i(inode);
0714     u64 valid;
0715     loff_t pos;
0716 
0717     if (is_resident(ni)) {
0718         /* No readahead for resident. */
0719         return;
0720     }
0721 
0722     if (is_compressed(ni)) {
0723         /* No readahead for compressed. */
0724         return;
0725     }
0726 
0727     valid = ni->i_valid;
0728     pos = readahead_pos(rac);
0729 
0730     if (valid < i_size_read(inode) && pos <= valid &&
0731         valid < pos + readahead_length(rac)) {
0732         /* Range cross 'valid'. Read it page by page. */
0733         return;
0734     }
0735 
0736     mpage_readahead(rac, ntfs_get_block);
0737 }
0738 
0739 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
0740                       struct buffer_head *bh_result, int create)
0741 {
0742     return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
0743                   bh_result, create, GET_BLOCK_DIRECT_IO_R);
0744 }
0745 
0746 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
0747                       struct buffer_head *bh_result, int create)
0748 {
0749     return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
0750                   bh_result, create, GET_BLOCK_DIRECT_IO_W);
0751 }
0752 
0753 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
0754 {
0755     struct file *file = iocb->ki_filp;
0756     struct address_space *mapping = file->f_mapping;
0757     struct inode *inode = mapping->host;
0758     struct ntfs_inode *ni = ntfs_i(inode);
0759     loff_t vbo = iocb->ki_pos;
0760     loff_t end;
0761     int wr = iov_iter_rw(iter) & WRITE;
0762     size_t iter_count = iov_iter_count(iter);
0763     loff_t valid;
0764     ssize_t ret;
0765 
0766     if (is_resident(ni)) {
0767         /* Switch to buffered write. */
0768         ret = 0;
0769         goto out;
0770     }
0771 
0772     ret = blockdev_direct_IO(iocb, inode, iter,
0773                  wr ? ntfs_get_block_direct_IO_W
0774                     : ntfs_get_block_direct_IO_R);
0775 
0776     if (ret > 0)
0777         end = vbo + ret;
0778     else if (wr && ret == -EIOCBQUEUED)
0779         end = vbo + iter_count;
0780     else
0781         goto out;
0782 
0783     valid = ni->i_valid;
0784     if (wr) {
0785         if (end > valid && !S_ISBLK(inode->i_mode)) {
0786             ni->i_valid = end;
0787             mark_inode_dirty(inode);
0788         }
0789     } else if (vbo < valid && valid < end) {
0790         /* Fix page. */
0791         iov_iter_revert(iter, end - valid);
0792         iov_iter_zero(end - valid, iter);
0793     }
0794 
0795 out:
0796     return ret;
0797 }
0798 
0799 int ntfs_set_size(struct inode *inode, u64 new_size)
0800 {
0801     struct super_block *sb = inode->i_sb;
0802     struct ntfs_sb_info *sbi = sb->s_fs_info;
0803     struct ntfs_inode *ni = ntfs_i(inode);
0804     int err;
0805 
0806     /* Check for maximum file size. */
0807     if (is_sparsed(ni) || is_compressed(ni)) {
0808         if (new_size > sbi->maxbytes_sparse) {
0809             err = -EFBIG;
0810             goto out;
0811         }
0812     } else if (new_size > sbi->maxbytes) {
0813         err = -EFBIG;
0814         goto out;
0815     }
0816 
0817     ni_lock(ni);
0818     down_write(&ni->file.run_lock);
0819 
0820     err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
0821                 &ni->i_valid, true, NULL);
0822 
0823     up_write(&ni->file.run_lock);
0824     ni_unlock(ni);
0825 
0826     mark_inode_dirty(inode);
0827 
0828 out:
0829     return err;
0830 }
0831 
0832 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
0833 {
0834     struct address_space *mapping = page->mapping;
0835     struct inode *inode = mapping->host;
0836     struct ntfs_inode *ni = ntfs_i(inode);
0837     int err;
0838 
0839     if (is_resident(ni)) {
0840         ni_lock(ni);
0841         err = attr_data_write_resident(ni, page);
0842         ni_unlock(ni);
0843         if (err != E_NTFS_NONRESIDENT) {
0844             unlock_page(page);
0845             return err;
0846         }
0847     }
0848 
0849     return block_write_full_page(page, ntfs_get_block, wbc);
0850 }
0851 
0852 static int ntfs_writepages(struct address_space *mapping,
0853                struct writeback_control *wbc)
0854 {
0855     /* Redirect call to 'ntfs_writepage' for resident files. */
0856     if (is_resident(ntfs_i(mapping->host)))
0857         return generic_writepages(mapping, wbc);
0858     return mpage_writepages(mapping, wbc, ntfs_get_block);
0859 }
0860 
0861 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
0862                       struct buffer_head *bh_result, int create)
0863 {
0864     return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
0865                   bh_result, create, GET_BLOCK_WRITE_BEGIN);
0866 }
0867 
0868 int ntfs_write_begin(struct file *file, struct address_space *mapping,
0869              loff_t pos, u32 len, struct page **pagep, void **fsdata)
0870 {
0871     int err;
0872     struct inode *inode = mapping->host;
0873     struct ntfs_inode *ni = ntfs_i(inode);
0874 
0875     *pagep = NULL;
0876     if (is_resident(ni)) {
0877         struct page *page = grab_cache_page_write_begin(
0878             mapping, pos >> PAGE_SHIFT);
0879 
0880         if (!page) {
0881             err = -ENOMEM;
0882             goto out;
0883         }
0884 
0885         ni_lock(ni);
0886         err = attr_data_read_resident(ni, page);
0887         ni_unlock(ni);
0888 
0889         if (!err) {
0890             *pagep = page;
0891             goto out;
0892         }
0893         unlock_page(page);
0894         put_page(page);
0895 
0896         if (err != E_NTFS_NONRESIDENT)
0897             goto out;
0898     }
0899 
0900     err = block_write_begin(mapping, pos, len, pagep,
0901                 ntfs_get_block_write_begin);
0902 
0903 out:
0904     return err;
0905 }
0906 
0907 /*
0908  * ntfs_write_end - Address_space_operations::write_end.
0909  */
0910 int ntfs_write_end(struct file *file, struct address_space *mapping,
0911            loff_t pos, u32 len, u32 copied, struct page *page,
0912            void *fsdata)
0913 {
0914     struct inode *inode = mapping->host;
0915     struct ntfs_inode *ni = ntfs_i(inode);
0916     u64 valid = ni->i_valid;
0917     bool dirty = false;
0918     int err;
0919 
0920     if (is_resident(ni)) {
0921         ni_lock(ni);
0922         err = attr_data_write_resident(ni, page);
0923         ni_unlock(ni);
0924         if (!err) {
0925             dirty = true;
0926             /* Clear any buffers in page. */
0927             if (page_has_buffers(page)) {
0928                 struct buffer_head *head, *bh;
0929 
0930                 bh = head = page_buffers(page);
0931                 do {
0932                     clear_buffer_dirty(bh);
0933                     clear_buffer_mapped(bh);
0934                     set_buffer_uptodate(bh);
0935                 } while (head != (bh = bh->b_this_page));
0936             }
0937             SetPageUptodate(page);
0938             err = copied;
0939         }
0940         unlock_page(page);
0941         put_page(page);
0942     } else {
0943         err = generic_write_end(file, mapping, pos, len, copied, page,
0944                     fsdata);
0945     }
0946 
0947     if (err >= 0) {
0948         if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
0949             inode->i_ctime = inode->i_mtime = current_time(inode);
0950             ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
0951             dirty = true;
0952         }
0953 
0954         if (valid != ni->i_valid) {
0955             /* ni->i_valid is changed in ntfs_get_block_vbo. */
0956             dirty = true;
0957         }
0958 
0959         if (dirty)
0960             mark_inode_dirty(inode);
0961     }
0962 
0963     return err;
0964 }
0965 
0966 int reset_log_file(struct inode *inode)
0967 {
0968     int err;
0969     loff_t pos = 0;
0970     u32 log_size = inode->i_size;
0971     struct address_space *mapping = inode->i_mapping;
0972 
0973     for (;;) {
0974         u32 len;
0975         void *kaddr;
0976         struct page *page;
0977 
0978         len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
0979 
0980         err = block_write_begin(mapping, pos, len, &page,
0981                     ntfs_get_block_write_begin);
0982         if (err)
0983             goto out;
0984 
0985         kaddr = kmap_atomic(page);
0986         memset(kaddr, -1, len);
0987         kunmap_atomic(kaddr);
0988         flush_dcache_page(page);
0989 
0990         err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
0991         if (err < 0)
0992             goto out;
0993         pos += len;
0994 
0995         if (pos >= log_size)
0996             break;
0997         balance_dirty_pages_ratelimited(mapping);
0998     }
0999 out:
1000     mark_inode_dirty_sync(inode);
1001 
1002     return err;
1003 }
1004 
1005 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1006 {
1007     return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1008 }
1009 
1010 int ntfs_sync_inode(struct inode *inode)
1011 {
1012     return _ni_write_inode(inode, 1);
1013 }
1014 
1015 /*
1016  * writeback_inode - Helper function for ntfs_flush_inodes().
1017  *
1018  * This writes both the inode and the file data blocks, waiting
1019  * for in flight data blocks before the start of the call.  It
1020  * does not wait for any io started during the call.
1021  */
1022 static int writeback_inode(struct inode *inode)
1023 {
1024     int ret = sync_inode_metadata(inode, 0);
1025 
1026     if (!ret)
1027         ret = filemap_fdatawrite(inode->i_mapping);
1028     return ret;
1029 }
1030 
1031 /*
1032  * ntfs_flush_inodes
1033  *
1034  * Write data and metadata corresponding to i1 and i2.  The io is
1035  * started but we do not wait for any of it to finish.
1036  *
1037  * filemap_flush() is used for the block device, so if there is a dirty
1038  * page for a block already in flight, we will not wait and start the
1039  * io over again.
1040  */
1041 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1042               struct inode *i2)
1043 {
1044     int ret = 0;
1045 
1046     if (i1)
1047         ret = writeback_inode(i1);
1048     if (!ret && i2)
1049         ret = writeback_inode(i2);
1050     if (!ret)
1051         ret = sync_blockdev_nowait(sb->s_bdev);
1052     return ret;
1053 }
1054 
1055 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1056 {
1057     pgoff_t idx;
1058 
1059     /* Write non resident data. */
1060     for (idx = 0; bytes; idx++) {
1061         size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1062         struct page *page = ntfs_map_page(inode->i_mapping, idx);
1063 
1064         if (IS_ERR(page))
1065             return PTR_ERR(page);
1066 
1067         lock_page(page);
1068         WARN_ON(!PageUptodate(page));
1069         ClearPageUptodate(page);
1070 
1071         memcpy(page_address(page), data, op);
1072 
1073         flush_dcache_page(page);
1074         SetPageUptodate(page);
1075         unlock_page(page);
1076 
1077         ntfs_unmap_page(page);
1078 
1079         bytes -= op;
1080         data = Add2Ptr(data, PAGE_SIZE);
1081     }
1082     return 0;
1083 }
1084 
1085 /*
1086  * ntfs_reparse_bytes
1087  *
1088  * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1089  * for unicode string of @uni_len length.
1090  */
1091 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1092 {
1093     /* Header + unicode string + decorated unicode string. */
1094     return sizeof(short) * (2 * uni_len + 4) +
1095            offsetof(struct REPARSE_DATA_BUFFER,
1096             SymbolicLinkReparseBuffer.PathBuffer);
1097 }
1098 
1099 static struct REPARSE_DATA_BUFFER *
1100 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1101                u32 size, u16 *nsize)
1102 {
1103     int i, err;
1104     struct REPARSE_DATA_BUFFER *rp;
1105     __le16 *rp_name;
1106     typeof(rp->SymbolicLinkReparseBuffer) *rs;
1107 
1108     rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1109     if (!rp)
1110         return ERR_PTR(-ENOMEM);
1111 
1112     rs = &rp->SymbolicLinkReparseBuffer;
1113     rp_name = rs->PathBuffer;
1114 
1115     /* Convert link name to UTF-16. */
1116     err = ntfs_nls_to_utf16(sbi, symname, size,
1117                 (struct cpu_str *)(rp_name - 1), 2 * size,
1118                 UTF16_LITTLE_ENDIAN);
1119     if (err < 0)
1120         goto out;
1121 
1122     /* err = the length of unicode name of symlink. */
1123     *nsize = ntfs_reparse_bytes(err);
1124 
1125     if (*nsize > sbi->reparse.max_size) {
1126         err = -EFBIG;
1127         goto out;
1128     }
1129 
1130     /* Translate Linux '/' into Windows '\'. */
1131     for (i = 0; i < err; i++) {
1132         if (rp_name[i] == cpu_to_le16('/'))
1133             rp_name[i] = cpu_to_le16('\\');
1134     }
1135 
1136     rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1137     rp->ReparseDataLength =
1138         cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1139                           SymbolicLinkReparseBuffer));
1140 
1141     /* PrintName + SubstituteName. */
1142     rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1143     rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1144     rs->PrintNameLength = rs->SubstituteNameOffset;
1145 
1146     /*
1147      * TODO: Use relative path if possible to allow Windows to
1148      * parse this path.
1149      * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1150      */
1151     rs->Flags = 0;
1152 
1153     memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1154 
1155     /* Decorate SubstituteName. */
1156     rp_name += err;
1157     rp_name[0] = cpu_to_le16('\\');
1158     rp_name[1] = cpu_to_le16('?');
1159     rp_name[2] = cpu_to_le16('?');
1160     rp_name[3] = cpu_to_le16('\\');
1161 
1162     return rp;
1163 out:
1164     kfree(rp);
1165     return ERR_PTR(err);
1166 }
1167 
1168 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1169                 struct inode *dir, struct dentry *dentry,
1170                 const struct cpu_str *uni, umode_t mode,
1171                 dev_t dev, const char *symname, u32 size,
1172                 struct ntfs_fnd *fnd)
1173 {
1174     int err;
1175     struct super_block *sb = dir->i_sb;
1176     struct ntfs_sb_info *sbi = sb->s_fs_info;
1177     const struct qstr *name = &dentry->d_name;
1178     CLST ino = 0;
1179     struct ntfs_inode *dir_ni = ntfs_i(dir);
1180     struct ntfs_inode *ni = NULL;
1181     struct inode *inode = NULL;
1182     struct ATTRIB *attr;
1183     struct ATTR_STD_INFO5 *std5;
1184     struct ATTR_FILE_NAME *fname;
1185     struct MFT_REC *rec;
1186     u32 asize, dsize, sd_size;
1187     enum FILE_ATTRIBUTE fa;
1188     __le32 security_id = SECURITY_ID_INVALID;
1189     CLST vcn;
1190     const void *sd;
1191     u16 t16, nsize = 0, aid = 0;
1192     struct INDEX_ROOT *root, *dir_root;
1193     struct NTFS_DE *e, *new_de = NULL;
1194     struct REPARSE_DATA_BUFFER *rp = NULL;
1195     bool rp_inserted = false;
1196 
1197     ni_lock_dir(dir_ni);
1198 
1199     dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1200     if (!dir_root) {
1201         err = -EINVAL;
1202         goto out1;
1203     }
1204 
1205     if (S_ISDIR(mode)) {
1206         /* Use parent's directory attributes. */
1207         fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1208              FILE_ATTRIBUTE_ARCHIVE;
1209         /*
1210          * By default child directory inherits parent attributes.
1211          * Root directory is hidden + system.
1212          * Make an exception for children in root.
1213          */
1214         if (dir->i_ino == MFT_REC_ROOT)
1215             fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1216     } else if (S_ISLNK(mode)) {
1217         /* It is good idea that link should be the same type (file/dir) as target */
1218         fa = FILE_ATTRIBUTE_REPARSE_POINT;
1219 
1220         /*
1221          * Linux: there are dir/file/symlink and so on.
1222          * NTFS: symlinks are "dir + reparse" or "file + reparse"
1223          * It is good idea to create:
1224          * dir + reparse if 'symname' points to directory
1225          * or
1226          * file + reparse if 'symname' points to file
1227          * Unfortunately kern_path hangs if symname contains 'dir'.
1228          */
1229 
1230         /*
1231          *  struct path path;
1232          *
1233          *  if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1234          *      struct inode *target = d_inode(path.dentry);
1235          *
1236          *      if (S_ISDIR(target->i_mode))
1237          *          fa |= FILE_ATTRIBUTE_DIRECTORY;
1238          *      // if ( target->i_sb == sb ){
1239          *      //  use relative path?
1240          *      // }
1241          *      path_put(&path);
1242          *  }
1243          */
1244     } else if (S_ISREG(mode)) {
1245         if (sbi->options->sparse) {
1246             /* Sparsed regular file, cause option 'sparse'. */
1247             fa = FILE_ATTRIBUTE_SPARSE_FILE |
1248                  FILE_ATTRIBUTE_ARCHIVE;
1249         } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1250             /* Compressed regular file, if parent is compressed. */
1251             fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1252         } else {
1253             /* Regular file, default attributes. */
1254             fa = FILE_ATTRIBUTE_ARCHIVE;
1255         }
1256     } else {
1257         fa = FILE_ATTRIBUTE_ARCHIVE;
1258     }
1259 
1260     if (!(mode & 0222))
1261         fa |= FILE_ATTRIBUTE_READONLY;
1262 
1263     /* Allocate PATH_MAX bytes. */
1264     new_de = __getname();
1265     if (!new_de) {
1266         err = -ENOMEM;
1267         goto out1;
1268     }
1269 
1270     /* Mark rw ntfs as dirty. it will be cleared at umount. */
1271     ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1272 
1273     /* Step 1: allocate and fill new mft record. */
1274     err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1275     if (err)
1276         goto out2;
1277 
1278     ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1279     if (IS_ERR(ni)) {
1280         err = PTR_ERR(ni);
1281         ni = NULL;
1282         goto out3;
1283     }
1284     inode = &ni->vfs_inode;
1285     inode_init_owner(mnt_userns, inode, dir, mode);
1286     mode = inode->i_mode;
1287 
1288     inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1289         current_time(inode);
1290 
1291     rec = ni->mi.mrec;
1292     rec->hard_links = cpu_to_le16(1);
1293     attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1294 
1295     /* Get default security id. */
1296     sd = s_default_security;
1297     sd_size = sizeof(s_default_security);
1298 
1299     if (is_ntfs3(sbi)) {
1300         security_id = dir_ni->std_security_id;
1301         if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1302             security_id = sbi->security.def_security_id;
1303 
1304             if (security_id == SECURITY_ID_INVALID &&
1305                 !ntfs_insert_security(sbi, sd, sd_size,
1306                           &security_id, NULL))
1307                 sbi->security.def_security_id = security_id;
1308         }
1309     }
1310 
1311     /* Insert standard info. */
1312     std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1313 
1314     if (security_id == SECURITY_ID_INVALID) {
1315         dsize = sizeof(struct ATTR_STD_INFO);
1316     } else {
1317         dsize = sizeof(struct ATTR_STD_INFO5);
1318         std5->security_id = security_id;
1319         ni->std_security_id = security_id;
1320     }
1321     asize = SIZEOF_RESIDENT + dsize;
1322 
1323     attr->type = ATTR_STD;
1324     attr->size = cpu_to_le32(asize);
1325     attr->id = cpu_to_le16(aid++);
1326     attr->res.data_off = SIZEOF_RESIDENT_LE;
1327     attr->res.data_size = cpu_to_le32(dsize);
1328 
1329     std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1330         kernel2nt(&inode->i_atime);
1331 
1332     ni->std_fa = fa;
1333     std5->fa = fa;
1334 
1335     attr = Add2Ptr(attr, asize);
1336 
1337     /* Insert file name. */
1338     err = fill_name_de(sbi, new_de, name, uni);
1339     if (err)
1340         goto out4;
1341 
1342     mi_get_ref(&ni->mi, &new_de->ref);
1343 
1344     fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1345     mi_get_ref(&dir_ni->mi, &fname->home);
1346     fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1347         fname->dup.a_time = std5->cr_time;
1348     fname->dup.alloc_size = fname->dup.data_size = 0;
1349     fname->dup.fa = std5->fa;
1350     fname->dup.ea_size = fname->dup.reparse = 0;
1351 
1352     dsize = le16_to_cpu(new_de->key_size);
1353     asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1354 
1355     attr->type = ATTR_NAME;
1356     attr->size = cpu_to_le32(asize);
1357     attr->res.data_off = SIZEOF_RESIDENT_LE;
1358     attr->res.flags = RESIDENT_FLAG_INDEXED;
1359     attr->id = cpu_to_le16(aid++);
1360     attr->res.data_size = cpu_to_le32(dsize);
1361     memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1362 
1363     attr = Add2Ptr(attr, asize);
1364 
1365     if (security_id == SECURITY_ID_INVALID) {
1366         /* Insert security attribute. */
1367         asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1368 
1369         attr->type = ATTR_SECURE;
1370         attr->size = cpu_to_le32(asize);
1371         attr->id = cpu_to_le16(aid++);
1372         attr->res.data_off = SIZEOF_RESIDENT_LE;
1373         attr->res.data_size = cpu_to_le32(sd_size);
1374         memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1375 
1376         attr = Add2Ptr(attr, asize);
1377     }
1378 
1379     attr->id = cpu_to_le16(aid++);
1380     if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1381         /*
1382          * Regular directory or symlink to directory.
1383          * Create root attribute.
1384          */
1385         dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1386         asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1387 
1388         attr->type = ATTR_ROOT;
1389         attr->size = cpu_to_le32(asize);
1390 
1391         attr->name_len = ARRAY_SIZE(I30_NAME);
1392         attr->name_off = SIZEOF_RESIDENT_LE;
1393         attr->res.data_off =
1394             cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1395         attr->res.data_size = cpu_to_le32(dsize);
1396         memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1397                sizeof(I30_NAME));
1398 
1399         root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1400         memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1401         root->ihdr.de_off =
1402             cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1403         root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1404                           sizeof(struct NTFS_DE));
1405         root->ihdr.total = root->ihdr.used;
1406 
1407         e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1408         e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1409         e->flags = NTFS_IE_LAST;
1410     } else if (S_ISLNK(mode)) {
1411         /*
1412          * Symlink to file.
1413          * Create empty resident data attribute.
1414          */
1415         asize = SIZEOF_RESIDENT;
1416 
1417         /* Insert empty ATTR_DATA */
1418         attr->type = ATTR_DATA;
1419         attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1420         attr->name_off = SIZEOF_RESIDENT_LE;
1421         attr->res.data_off = SIZEOF_RESIDENT_LE;
1422     } else if (S_ISREG(mode)) {
1423         /*
1424          * Regular file. Create empty non resident data attribute.
1425          */
1426         attr->type = ATTR_DATA;
1427         attr->non_res = 1;
1428         attr->nres.evcn = cpu_to_le64(-1ll);
1429         if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1430             attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1431             attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1432             attr->flags = ATTR_FLAG_SPARSED;
1433             asize = SIZEOF_NONRESIDENT_EX + 8;
1434         } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1435             attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1436             attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1437             attr->flags = ATTR_FLAG_COMPRESSED;
1438             attr->nres.c_unit = COMPRESSION_UNIT;
1439             asize = SIZEOF_NONRESIDENT_EX + 8;
1440         } else {
1441             attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1442             attr->name_off = SIZEOF_NONRESIDENT_LE;
1443             asize = SIZEOF_NONRESIDENT + 8;
1444         }
1445         attr->nres.run_off = attr->name_off;
1446     } else {
1447         /*
1448          * Node. Create empty resident data attribute.
1449          */
1450         attr->type = ATTR_DATA;
1451         attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1452         attr->name_off = SIZEOF_RESIDENT_LE;
1453         if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1454             attr->flags = ATTR_FLAG_SPARSED;
1455         else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1456             attr->flags = ATTR_FLAG_COMPRESSED;
1457         attr->res.data_off = SIZEOF_RESIDENT_LE;
1458         asize = SIZEOF_RESIDENT;
1459         ni->ni_flags |= NI_FLAG_RESIDENT;
1460     }
1461 
1462     if (S_ISDIR(mode)) {
1463         ni->ni_flags |= NI_FLAG_DIR;
1464         err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1465         if (err)
1466             goto out4;
1467     } else if (S_ISLNK(mode)) {
1468         rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1469 
1470         if (IS_ERR(rp)) {
1471             err = PTR_ERR(rp);
1472             rp = NULL;
1473             goto out4;
1474         }
1475 
1476         /*
1477          * Insert ATTR_REPARSE.
1478          */
1479         attr = Add2Ptr(attr, asize);
1480         attr->type = ATTR_REPARSE;
1481         attr->id = cpu_to_le16(aid++);
1482 
1483         /* Resident or non resident? */
1484         asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1485         t16 = PtrOffset(rec, attr);
1486 
1487         /*
1488          * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1489          * It is good idea to keep extened attributes resident.
1490          */
1491         if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1492             CLST alen;
1493             CLST clst = bytes_to_cluster(sbi, nsize);
1494 
1495             /* Bytes per runs. */
1496             t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1497 
1498             attr->non_res = 1;
1499             attr->nres.evcn = cpu_to_le64(clst - 1);
1500             attr->name_off = SIZEOF_NONRESIDENT_LE;
1501             attr->nres.run_off = attr->name_off;
1502             attr->nres.data_size = cpu_to_le64(nsize);
1503             attr->nres.valid_size = attr->nres.data_size;
1504             attr->nres.alloc_size =
1505                 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1506 
1507             err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1508                              clst, NULL, 0, &alen, 0,
1509                              NULL);
1510             if (err)
1511                 goto out5;
1512 
1513             err = run_pack(&ni->file.run, 0, clst,
1514                        Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1515                        &vcn);
1516             if (err < 0)
1517                 goto out5;
1518 
1519             if (vcn != clst) {
1520                 err = -EINVAL;
1521                 goto out5;
1522             }
1523 
1524             asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1525         } else {
1526             attr->res.data_off = SIZEOF_RESIDENT_LE;
1527             attr->res.data_size = cpu_to_le32(nsize);
1528             memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1529             nsize = 0;
1530         }
1531         /* Size of symlink equals the length of input string. */
1532         inode->i_size = size;
1533 
1534         attr->size = cpu_to_le32(asize);
1535 
1536         err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1537                       &new_de->ref);
1538         if (err)
1539             goto out5;
1540 
1541         rp_inserted = true;
1542     }
1543 
1544     attr = Add2Ptr(attr, asize);
1545     attr->type = ATTR_END;
1546 
1547     rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1548     rec->next_attr_id = cpu_to_le16(aid);
1549 
1550     /* Step 2: Add new name in index. */
1551     err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1552     if (err)
1553         goto out6;
1554 
1555     /* Unlock parent directory before ntfs_init_acl. */
1556     ni_unlock(dir_ni);
1557 
1558     inode->i_generation = le16_to_cpu(rec->seq);
1559 
1560     dir->i_mtime = dir->i_ctime = inode->i_atime;
1561 
1562     if (S_ISDIR(mode)) {
1563         inode->i_op = &ntfs_dir_inode_operations;
1564         inode->i_fop = &ntfs_dir_operations;
1565     } else if (S_ISLNK(mode)) {
1566         inode->i_op = &ntfs_link_inode_operations;
1567         inode->i_fop = NULL;
1568         inode->i_mapping->a_ops = &ntfs_aops;
1569         inode->i_size = size;
1570         inode_nohighmem(inode);
1571     } else if (S_ISREG(mode)) {
1572         inode->i_op = &ntfs_file_inode_operations;
1573         inode->i_fop = &ntfs_file_operations;
1574         inode->i_mapping->a_ops =
1575             is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1576         init_rwsem(&ni->file.run_lock);
1577     } else {
1578         inode->i_op = &ntfs_special_inode_operations;
1579         init_special_inode(inode, mode, dev);
1580     }
1581 
1582 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1583     if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1584         err = ntfs_init_acl(mnt_userns, inode, dir);
1585         if (err)
1586             goto out7;
1587     } else
1588 #endif
1589     {
1590         inode->i_flags |= S_NOSEC;
1591     }
1592 
1593     /* Write non resident data. */
1594     if (nsize) {
1595         err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1596         if (err)
1597             goto out7;
1598     }
1599 
1600     /*
1601      * Call 'd_instantiate' after inode->i_op is set
1602      * but before finish_open.
1603      */
1604     d_instantiate(dentry, inode);
1605 
1606     ntfs_save_wsl_perm(inode);
1607     mark_inode_dirty(dir);
1608     mark_inode_dirty(inode);
1609 
1610     /* Normal exit. */
1611     goto out2;
1612 
1613 out7:
1614 
1615     /* Undo 'indx_insert_entry'. */
1616     ni_lock_dir(dir_ni);
1617     indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1618               le16_to_cpu(new_de->key_size), sbi);
1619     /* ni_unlock(dir_ni); will be called later. */
1620 out6:
1621     if (rp_inserted)
1622         ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1623 
1624 out5:
1625     if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1626         goto out4;
1627 
1628     run_deallocate(sbi, &ni->file.run, false);
1629 
1630 out4:
1631     clear_rec_inuse(rec);
1632     clear_nlink(inode);
1633     ni->mi.dirty = false;
1634     discard_new_inode(inode);
1635 out3:
1636     ntfs_mark_rec_free(sbi, ino, false);
1637 
1638 out2:
1639     __putname(new_de);
1640     kfree(rp);
1641 
1642 out1:
1643     if (err) {
1644         ni_unlock(dir_ni);
1645         return ERR_PTR(err);
1646     }
1647 
1648     unlock_new_inode(inode);
1649 
1650     return inode;
1651 }
1652 
1653 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1654 {
1655     int err;
1656     struct ntfs_inode *ni = ntfs_i(inode);
1657     struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1658     struct NTFS_DE *de;
1659 
1660     /* Allocate PATH_MAX bytes. */
1661     de = __getname();
1662     if (!de)
1663         return -ENOMEM;
1664 
1665     /* Mark rw ntfs as dirty. It will be cleared at umount. */
1666     ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1667 
1668     /* Construct 'de'. */
1669     err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1670     if (err)
1671         goto out;
1672 
1673     err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1674 out:
1675     __putname(de);
1676     return err;
1677 }
1678 
1679 /*
1680  * ntfs_unlink_inode
1681  *
1682  * inode_operations::unlink
1683  * inode_operations::rmdir
1684  */
1685 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1686 {
1687     int err;
1688     struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1689     struct inode *inode = d_inode(dentry);
1690     struct ntfs_inode *ni = ntfs_i(inode);
1691     struct ntfs_inode *dir_ni = ntfs_i(dir);
1692     struct NTFS_DE *de, *de2 = NULL;
1693     int undo_remove;
1694 
1695     if (ntfs_is_meta_file(sbi, ni->mi.rno))
1696         return -EINVAL;
1697 
1698     /* Allocate PATH_MAX bytes. */
1699     de = __getname();
1700     if (!de)
1701         return -ENOMEM;
1702 
1703     ni_lock(ni);
1704 
1705     if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1706         err = -ENOTEMPTY;
1707         goto out;
1708     }
1709 
1710     err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1711     if (err < 0)
1712         goto out;
1713 
1714     undo_remove = 0;
1715     err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1716 
1717     if (!err) {
1718         drop_nlink(inode);
1719         dir->i_mtime = dir->i_ctime = current_time(dir);
1720         mark_inode_dirty(dir);
1721         inode->i_ctime = dir->i_ctime;
1722         if (inode->i_nlink)
1723             mark_inode_dirty(inode);
1724     } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1725         _ntfs_bad_inode(inode);
1726     } else {
1727         if (ni_is_dirty(dir))
1728             mark_inode_dirty(dir);
1729         if (ni_is_dirty(inode))
1730             mark_inode_dirty(inode);
1731     }
1732 
1733 out:
1734     ni_unlock(ni);
1735     __putname(de);
1736     return err;
1737 }
1738 
1739 void ntfs_evict_inode(struct inode *inode)
1740 {
1741     truncate_inode_pages_final(&inode->i_data);
1742 
1743     if (inode->i_nlink)
1744         _ni_write_inode(inode, inode_needs_sync(inode));
1745 
1746     invalidate_inode_buffers(inode);
1747     clear_inode(inode);
1748 
1749     ni_clear(ntfs_i(inode));
1750 }
1751 
1752 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1753                       int buflen)
1754 {
1755     int i, err = -EINVAL;
1756     struct ntfs_inode *ni = ntfs_i(inode);
1757     struct super_block *sb = inode->i_sb;
1758     struct ntfs_sb_info *sbi = sb->s_fs_info;
1759     u64 size;
1760     u16 ulen = 0;
1761     void *to_free = NULL;
1762     struct REPARSE_DATA_BUFFER *rp;
1763     const __le16 *uname;
1764     struct ATTRIB *attr;
1765 
1766     /* Reparse data present. Try to parse it. */
1767     static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1768     static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1769 
1770     *buffer = 0;
1771 
1772     attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1773     if (!attr)
1774         goto out;
1775 
1776     if (!attr->non_res) {
1777         rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1778         if (!rp)
1779             goto out;
1780         size = le32_to_cpu(attr->res.data_size);
1781     } else {
1782         size = le64_to_cpu(attr->nres.data_size);
1783         rp = NULL;
1784     }
1785 
1786     if (size > sbi->reparse.max_size || size <= sizeof(u32))
1787         goto out;
1788 
1789     if (!rp) {
1790         rp = kmalloc(size, GFP_NOFS);
1791         if (!rp) {
1792             err = -ENOMEM;
1793             goto out;
1794         }
1795         to_free = rp;
1796         /* Read into temporal buffer. */
1797         err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1798         if (err)
1799             goto out;
1800     }
1801 
1802     /* Microsoft Tag. */
1803     switch (rp->ReparseTag) {
1804     case IO_REPARSE_TAG_MOUNT_POINT:
1805         /* Mount points and junctions. */
1806         /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1807         if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1808                      MountPointReparseBuffer.PathBuffer))
1809             goto out;
1810         uname = Add2Ptr(rp,
1811                 offsetof(struct REPARSE_DATA_BUFFER,
1812                      MountPointReparseBuffer.PathBuffer) +
1813                     le16_to_cpu(rp->MountPointReparseBuffer
1814                                 .PrintNameOffset));
1815         ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1816         break;
1817 
1818     case IO_REPARSE_TAG_SYMLINK:
1819         /* FolderSymbolicLink */
1820         /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1821         if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1822                      SymbolicLinkReparseBuffer.PathBuffer))
1823             goto out;
1824         uname = Add2Ptr(
1825             rp, offsetof(struct REPARSE_DATA_BUFFER,
1826                      SymbolicLinkReparseBuffer.PathBuffer) +
1827                     le16_to_cpu(rp->SymbolicLinkReparseBuffer
1828                             .PrintNameOffset));
1829         ulen = le16_to_cpu(
1830             rp->SymbolicLinkReparseBuffer.PrintNameLength);
1831         break;
1832 
1833     case IO_REPARSE_TAG_CLOUD:
1834     case IO_REPARSE_TAG_CLOUD_1:
1835     case IO_REPARSE_TAG_CLOUD_2:
1836     case IO_REPARSE_TAG_CLOUD_3:
1837     case IO_REPARSE_TAG_CLOUD_4:
1838     case IO_REPARSE_TAG_CLOUD_5:
1839     case IO_REPARSE_TAG_CLOUD_6:
1840     case IO_REPARSE_TAG_CLOUD_7:
1841     case IO_REPARSE_TAG_CLOUD_8:
1842     case IO_REPARSE_TAG_CLOUD_9:
1843     case IO_REPARSE_TAG_CLOUD_A:
1844     case IO_REPARSE_TAG_CLOUD_B:
1845     case IO_REPARSE_TAG_CLOUD_C:
1846     case IO_REPARSE_TAG_CLOUD_D:
1847     case IO_REPARSE_TAG_CLOUD_E:
1848     case IO_REPARSE_TAG_CLOUD_F:
1849         err = sizeof("OneDrive") - 1;
1850         if (err > buflen)
1851             err = buflen;
1852         memcpy(buffer, "OneDrive", err);
1853         goto out;
1854 
1855     default:
1856         if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1857             /* Unknown Microsoft Tag. */
1858             goto out;
1859         }
1860         if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1861             size <= sizeof(struct REPARSE_POINT)) {
1862             goto out;
1863         }
1864 
1865         /* Users tag. */
1866         uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1867         ulen = le16_to_cpu(rp->ReparseDataLength) -
1868                sizeof(struct REPARSE_POINT);
1869     }
1870 
1871     /* Convert nlen from bytes to UNICODE chars. */
1872     ulen >>= 1;
1873 
1874     /* Check that name is available. */
1875     if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1876         goto out;
1877 
1878     /* If name is already zero terminated then truncate it now. */
1879     if (!uname[ulen - 1])
1880         ulen -= 1;
1881 
1882     err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1883 
1884     if (err < 0)
1885         goto out;
1886 
1887     /* Translate Windows '\' into Linux '/'. */
1888     for (i = 0; i < err; i++) {
1889         if (buffer[i] == '\\')
1890             buffer[i] = '/';
1891     }
1892 
1893     /* Always set last zero. */
1894     buffer[err] = 0;
1895 out:
1896     kfree(to_free);
1897     return err;
1898 }
1899 
1900 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1901                  struct delayed_call *done)
1902 {
1903     int err;
1904     char *ret;
1905 
1906     if (!de)
1907         return ERR_PTR(-ECHILD);
1908 
1909     ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1910     if (!ret)
1911         return ERR_PTR(-ENOMEM);
1912 
1913     err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1914     if (err < 0) {
1915         kfree(ret);
1916         return ERR_PTR(err);
1917     }
1918 
1919     set_delayed_call(done, kfree_link, ret);
1920 
1921     return ret;
1922 }
1923 
1924 // clang-format off
1925 const struct inode_operations ntfs_link_inode_operations = {
1926     .get_link   = ntfs_get_link,
1927     .setattr    = ntfs3_setattr,
1928     .listxattr  = ntfs_listxattr,
1929     .permission = ntfs_permission,
1930     .get_acl    = ntfs_get_acl,
1931     .set_acl    = ntfs_set_acl,
1932 };
1933 
1934 const struct address_space_operations ntfs_aops = {
1935     .read_folio = ntfs_read_folio,
1936     .readahead  = ntfs_readahead,
1937     .writepage  = ntfs_writepage,
1938     .writepages = ntfs_writepages,
1939     .write_begin    = ntfs_write_begin,
1940     .write_end  = ntfs_write_end,
1941     .direct_IO  = ntfs_direct_IO,
1942     .bmap       = ntfs_bmap,
1943     .dirty_folio    = block_dirty_folio,
1944     .invalidate_folio = block_invalidate_folio,
1945 };
1946 
1947 const struct address_space_operations ntfs_aops_cmpr = {
1948     .read_folio = ntfs_read_folio,
1949     .readahead  = ntfs_readahead,
1950 };
1951 // clang-format on