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 // clang-format off
0009 #ifndef _LINUX_NTFS3_NTFS_FS_H
0010 #define _LINUX_NTFS3_NTFS_FS_H
0011 
0012 #include <linux/blkdev.h>
0013 #include <linux/buffer_head.h>
0014 #include <linux/fs.h>
0015 #include <linux/highmem.h>
0016 #include <linux/kernel.h>
0017 #include <linux/mm.h>
0018 #include <linux/mutex.h>
0019 #include <linux/page-flags.h>
0020 #include <linux/pagemap.h>
0021 #include <linux/rbtree.h>
0022 #include <linux/rwsem.h>
0023 #include <linux/slab.h>
0024 #include <linux/string.h>
0025 #include <linux/time64.h>
0026 #include <linux/types.h>
0027 #include <linux/uidgid.h>
0028 #include <asm/div64.h>
0029 #include <asm/page.h>
0030 
0031 #include "debug.h"
0032 #include "ntfs.h"
0033 
0034 struct dentry;
0035 struct fiemap_extent_info;
0036 struct user_namespace;
0037 struct page;
0038 struct writeback_control;
0039 enum utf16_endian;
0040 
0041 
0042 #define MINUS_ONE_T         ((size_t)(-1))
0043 /* Biggest MFT / smallest cluster */
0044 #define MAXIMUM_BYTES_PER_MFT       4096
0045 #define NTFS_BLOCKS_PER_MFT_RECORD  (MAXIMUM_BYTES_PER_MFT / 512)
0046 
0047 #define MAXIMUM_BYTES_PER_INDEX     4096
0048 #define NTFS_BLOCKS_PER_INODE       (MAXIMUM_BYTES_PER_INDEX / 512)
0049 
0050 /* NTFS specific error code when fixup failed. */
0051 #define E_NTFS_FIXUP            555
0052 /* NTFS specific error code about resident->nonresident. */
0053 #define E_NTFS_NONRESIDENT      556
0054 /* NTFS specific error code about punch hole. */
0055 #define E_NTFS_NOTALIGNED       557
0056 
0057 
0058 /* sbi->flags */
0059 #define NTFS_FLAGS_NODISCARD        0x00000001
0060 /* Set when LogFile is replaying. */
0061 #define NTFS_FLAGS_LOG_REPLAYING    0x00000008
0062 /* Set when we changed first MFT's which copy must be updated in $MftMirr. */
0063 #define NTFS_FLAGS_MFTMIRR      0x00001000
0064 #define NTFS_FLAGS_NEED_REPLAY      0x04000000
0065 
0066 
0067 /* ni->ni_flags */
0068 /*
0069  * Data attribute is external compressed (LZX/Xpress)
0070  * 1 - WOF_COMPRESSION_XPRESS4K
0071  * 2 - WOF_COMPRESSION_XPRESS8K
0072  * 3 - WOF_COMPRESSION_XPRESS16K
0073  * 4 - WOF_COMPRESSION_LZX32K
0074  */
0075 #define NI_FLAG_COMPRESSED_MASK     0x0000000f
0076 /* Data attribute is deduplicated. */
0077 #define NI_FLAG_DEDUPLICATED        0x00000010
0078 #define NI_FLAG_EA          0x00000020
0079 #define NI_FLAG_DIR         0x00000040
0080 #define NI_FLAG_RESIDENT        0x00000080
0081 #define NI_FLAG_UPDATE_PARENT       0x00000100
0082 // clang-format on
0083 
0084 struct ntfs_mount_options {
0085     char *nls_name;
0086     struct nls_table *nls;
0087 
0088     kuid_t fs_uid;
0089     kgid_t fs_gid;
0090     u16 fs_fmask_inv;
0091     u16 fs_dmask_inv;
0092 
0093     unsigned fmask : 1; /* fmask was set. */
0094     unsigned dmask : 1; /*dmask was set. */
0095     unsigned sys_immutable : 1; /* Immutable system files. */
0096     unsigned discard : 1; /* Issue discard requests on deletions. */
0097     unsigned sparse : 1; /* Create sparse files. */
0098     unsigned showmeta : 1; /* Show meta files. */
0099     unsigned nohidden : 1; /* Do not show hidden files. */
0100     unsigned force : 1; /* RW mount dirty volume. */
0101     unsigned noacsrules : 1; /* Exclude acs rules. */
0102     unsigned prealloc : 1; /* Preallocate space when file is growing. */
0103 };
0104 
0105 /* Special value to unpack and deallocate. */
0106 #define RUN_DEALLOCATE ((struct runs_tree *)(size_t)1)
0107 
0108 /* TODO: Use rb tree instead of array. */
0109 struct runs_tree {
0110     struct ntfs_run *runs;
0111     size_t count; /* Currently used size a ntfs_run storage. */
0112     size_t allocated; /* Currently allocated ntfs_run storage size. */
0113 };
0114 
0115 struct ntfs_buffers {
0116     /* Biggest MFT / smallest cluster = 4096 / 512 = 8 */
0117     /* Biggest index / smallest cluster = 4096 / 512 = 8 */
0118     struct buffer_head *bh[PAGE_SIZE >> SECTOR_SHIFT];
0119     u32 bytes;
0120     u32 nbufs;
0121     u32 off;
0122 };
0123 
0124 enum ALLOCATE_OPT {
0125     ALLOCATE_DEF = 0, // Allocate all clusters.
0126     ALLOCATE_MFT = 1, // Allocate for MFT.
0127 };
0128 
0129 enum bitmap_mutex_classes {
0130     BITMAP_MUTEX_CLUSTERS = 0,
0131     BITMAP_MUTEX_MFT = 1,
0132 };
0133 
0134 struct wnd_bitmap {
0135     struct super_block *sb;
0136     struct rw_semaphore rw_lock;
0137 
0138     struct runs_tree run;
0139     size_t nbits;
0140 
0141     size_t total_zeroes; // Total number of free bits.
0142     u16 *free_bits; // Free bits in each window.
0143     size_t nwnd;
0144     u32 bits_last; // Bits in last window.
0145 
0146     struct rb_root start_tree; // Extents, sorted by 'start'.
0147     struct rb_root count_tree; // Extents, sorted by 'count + start'.
0148     size_t count; // Extents count.
0149 
0150     /*
0151      * -1 Tree is activated but not updated (too many fragments).
0152      * 0 - Tree is not activated.
0153      * 1 - Tree is activated and updated.
0154      */
0155     int uptodated;
0156     size_t extent_min; // Minimal extent used while building.
0157     size_t extent_max; // Upper estimate of biggest free block.
0158 
0159     /* Zone [bit, end) */
0160     size_t zone_bit;
0161     size_t zone_end;
0162 
0163     bool set_tail; // Not necessary in driver.
0164     bool inited;
0165 };
0166 
0167 typedef int (*NTFS_CMP_FUNC)(const void *key1, size_t len1, const void *key2,
0168                  size_t len2, const void *param);
0169 
0170 enum index_mutex_classed {
0171     INDEX_MUTEX_I30 = 0,
0172     INDEX_MUTEX_SII = 1,
0173     INDEX_MUTEX_SDH = 2,
0174     INDEX_MUTEX_SO = 3,
0175     INDEX_MUTEX_SQ = 4,
0176     INDEX_MUTEX_SR = 5,
0177     INDEX_MUTEX_TOTAL
0178 };
0179 
0180 /* ntfs_index - Allocation unit inside directory. */
0181 struct ntfs_index {
0182     struct runs_tree bitmap_run;
0183     struct runs_tree alloc_run;
0184     /* read/write access to 'bitmap_run'/'alloc_run' while ntfs_readdir */
0185     struct rw_semaphore run_lock;
0186 
0187     /*TODO: Remove 'cmp'. */
0188     NTFS_CMP_FUNC cmp;
0189 
0190     u8 index_bits; // log2(root->index_block_size)
0191     u8 idx2vbn_bits; // log2(root->index_block_clst)
0192     u8 vbn2vbo_bits; // index_block_size < cluster? 9 : cluster_bits
0193     u8 type; // index_mutex_classed
0194 };
0195 
0196 /* Minimum MFT zone. */
0197 #define NTFS_MIN_MFT_ZONE 100
0198 
0199 /* Ntfs file system in-core superblock data. */
0200 struct ntfs_sb_info {
0201     struct super_block *sb;
0202 
0203     u32 discard_granularity;
0204     u64 discard_granularity_mask_inv; // ~(discard_granularity_mask_inv-1)
0205 
0206     u32 cluster_size; // bytes per cluster
0207     u32 cluster_mask; // == cluster_size - 1
0208     u64 cluster_mask_inv; // ~(cluster_size - 1)
0209     u32 block_mask; // sb->s_blocksize - 1
0210     u32 blocks_per_cluster; // cluster_size / sb->s_blocksize
0211 
0212     u32 record_size;
0213     u32 index_size;
0214 
0215     u8 cluster_bits;
0216     u8 record_bits;
0217 
0218     u64 maxbytes; // Maximum size for normal files.
0219     u64 maxbytes_sparse; // Maximum size for sparse file.
0220 
0221     u32 flags; // See NTFS_FLAGS_XXX.
0222 
0223     CLST zone_max; // Maximum MFT zone length in clusters
0224     CLST bad_clusters; // The count of marked bad clusters.
0225 
0226     u16 max_bytes_per_attr; // Maximum attribute size in record.
0227     u16 attr_size_tr; // Attribute size threshold (320 bytes).
0228 
0229     /* Records in $Extend. */
0230     CLST objid_no;
0231     CLST quota_no;
0232     CLST reparse_no;
0233     CLST usn_jrnl_no;
0234 
0235     struct ATTR_DEF_ENTRY *def_table; // Attribute definition table.
0236     u32 def_entries;
0237     u32 ea_max_size;
0238 
0239     struct MFT_REC *new_rec;
0240 
0241     u16 *upcase;
0242 
0243     struct {
0244         u64 lbo, lbo2;
0245         struct ntfs_inode *ni;
0246         struct wnd_bitmap bitmap; // $MFT::Bitmap
0247         /*
0248          * MFT records [11-24) used to expand MFT itself.
0249          * They always marked as used in $MFT::Bitmap
0250          * 'reserved_bitmap' contains real bitmap of these records.
0251          */
0252         ulong reserved_bitmap; // Bitmap of used records [11 - 24)
0253         size_t next_free; // The next record to allocate from
0254         size_t used; // MFT valid size in records.
0255         u32 recs_mirr; // Number of records in MFTMirr
0256         u8 next_reserved;
0257         u8 reserved_bitmap_inited;
0258     } mft;
0259 
0260     struct {
0261         struct wnd_bitmap bitmap; // $Bitmap::Data
0262         CLST next_free_lcn;
0263     } used;
0264 
0265     struct {
0266         u64 size; // In bytes.
0267         u64 blocks; // In blocks.
0268         u64 ser_num;
0269         struct ntfs_inode *ni;
0270         __le16 flags; // Cached current VOLUME_INFO::flags, VOLUME_FLAG_DIRTY.
0271         u8 major_ver;
0272         u8 minor_ver;
0273         char label[65];
0274         bool real_dirty; // Real fs state.
0275     } volume;
0276 
0277     struct {
0278         struct ntfs_index index_sii;
0279         struct ntfs_index index_sdh;
0280         struct ntfs_inode *ni;
0281         u32 next_id;
0282         u64 next_off;
0283 
0284         __le32 def_security_id;
0285     } security;
0286 
0287     struct {
0288         struct ntfs_index index_r;
0289         struct ntfs_inode *ni;
0290         u64 max_size; // 16K
0291     } reparse;
0292 
0293     struct {
0294         struct ntfs_index index_o;
0295         struct ntfs_inode *ni;
0296     } objid;
0297 
0298     struct {
0299         struct mutex mtx_lznt;
0300         struct lznt *lznt;
0301 #ifdef CONFIG_NTFS3_LZX_XPRESS
0302         struct mutex mtx_xpress;
0303         struct xpress_decompressor *xpress;
0304         struct mutex mtx_lzx;
0305         struct lzx_decompressor *lzx;
0306 #endif
0307     } compress;
0308 
0309     struct ntfs_mount_options *options;
0310     struct ratelimit_state msg_ratelimit;
0311 };
0312 
0313 /* One MFT record(usually 1024 bytes), consists of attributes. */
0314 struct mft_inode {
0315     struct rb_node node;
0316     struct ntfs_sb_info *sbi;
0317 
0318     struct MFT_REC *mrec;
0319     struct ntfs_buffers nb;
0320 
0321     CLST rno;
0322     bool dirty;
0323 };
0324 
0325 /* Nested class for ntfs_inode::ni_lock. */
0326 enum ntfs_inode_mutex_lock_class {
0327     NTFS_INODE_MUTEX_DIRTY,
0328     NTFS_INODE_MUTEX_SECURITY,
0329     NTFS_INODE_MUTEX_OBJID,
0330     NTFS_INODE_MUTEX_REPARSE,
0331     NTFS_INODE_MUTEX_NORMAL,
0332     NTFS_INODE_MUTEX_PARENT,
0333 };
0334 
0335 /*
0336  * sturct ntfs_inode
0337  *
0338  * Ntfs inode - extends linux inode. consists of one or more MFT inodes.
0339  */
0340 struct ntfs_inode {
0341     struct mft_inode mi; // base record
0342 
0343     /*
0344      * Valid size: [0 - i_valid) - these range in file contains valid data.
0345      * Range [i_valid - inode->i_size) - contains 0.
0346      * Usually i_valid <= inode->i_size.
0347      */
0348     u64 i_valid;
0349     struct timespec64 i_crtime;
0350 
0351     struct mutex ni_lock;
0352 
0353     /* File attributes from std. */
0354     enum FILE_ATTRIBUTE std_fa;
0355     __le32 std_security_id;
0356 
0357     /*
0358      * Tree of mft_inode.
0359      * Not empty when primary MFT record (usually 1024 bytes) can't save all attributes
0360      * e.g. file becomes too fragmented or contains a lot of names.
0361      */
0362     struct rb_root mi_tree;
0363 
0364     /*
0365      * This member is used in ntfs_readdir to ensure that all subrecords are loaded
0366      */
0367     u8 mi_loaded;
0368 
0369     union {
0370         struct ntfs_index dir;
0371         struct {
0372             struct rw_semaphore run_lock;
0373             struct runs_tree run;
0374 #ifdef CONFIG_NTFS3_LZX_XPRESS
0375             struct page *offs_page;
0376 #endif
0377         } file;
0378     };
0379 
0380     struct {
0381         struct runs_tree run;
0382         struct ATTR_LIST_ENTRY *le; // 1K aligned memory.
0383         size_t size;
0384         bool dirty;
0385     } attr_list;
0386 
0387     size_t ni_flags; // NI_FLAG_XXX
0388 
0389     struct inode vfs_inode;
0390 };
0391 
0392 struct indx_node {
0393     struct ntfs_buffers nb;
0394     struct INDEX_BUFFER *index;
0395 };
0396 
0397 struct ntfs_fnd {
0398     int level;
0399     struct indx_node *nodes[20];
0400     struct NTFS_DE *de[20];
0401     struct NTFS_DE *root_de;
0402 };
0403 
0404 enum REPARSE_SIGN {
0405     REPARSE_NONE = 0,
0406     REPARSE_COMPRESSED = 1,
0407     REPARSE_DEDUPLICATED = 2,
0408     REPARSE_LINK = 3
0409 };
0410 
0411 /* Functions from attrib.c */
0412 int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
0413                CLST vcn, CLST lcn, CLST len, CLST *pre_alloc,
0414                enum ALLOCATE_OPT opt, CLST *alen, const size_t fr,
0415                CLST *new_lcn);
0416 int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
0417               struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
0418               u64 new_size, struct runs_tree *run,
0419               struct ATTRIB **ins_attr, struct page *page);
0420 int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
0421           const __le16 *name, u8 name_len, struct runs_tree *run,
0422           u64 new_size, const u64 *new_valid, bool keep_prealloc,
0423           struct ATTRIB **ret);
0424 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
0425             CLST *len, bool *new);
0426 int attr_data_read_resident(struct ntfs_inode *ni, struct page *page);
0427 int attr_data_write_resident(struct ntfs_inode *ni, struct page *page);
0428 int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
0429                const __le16 *name, u8 name_len, struct runs_tree *run,
0430                CLST vcn);
0431 int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
0432              const __le16 *name, u8 name_len, struct runs_tree *run,
0433              u64 from, u64 to);
0434 int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
0435             struct runs_tree *run, u64 frame, u64 frames,
0436             u8 frame_bits, u32 *ondisk_size, u64 *vbo_data);
0437 int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
0438                  CLST frame, CLST *clst_data);
0439 int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
0440             u64 new_valid);
0441 int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes);
0442 int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes);
0443 int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size);
0444 
0445 /* Functions from attrlist.c */
0446 void al_destroy(struct ntfs_inode *ni);
0447 bool al_verify(struct ntfs_inode *ni);
0448 int ntfs_load_attr_list(struct ntfs_inode *ni, struct ATTRIB *attr);
0449 struct ATTR_LIST_ENTRY *al_enumerate(struct ntfs_inode *ni,
0450                      struct ATTR_LIST_ENTRY *le);
0451 struct ATTR_LIST_ENTRY *al_find_le(struct ntfs_inode *ni,
0452                    struct ATTR_LIST_ENTRY *le,
0453                    const struct ATTRIB *attr);
0454 struct ATTR_LIST_ENTRY *al_find_ex(struct ntfs_inode *ni,
0455                    struct ATTR_LIST_ENTRY *le,
0456                    enum ATTR_TYPE type, const __le16 *name,
0457                    u8 name_len, const CLST *vcn);
0458 int al_add_le(struct ntfs_inode *ni, enum ATTR_TYPE type, const __le16 *name,
0459           u8 name_len, CLST svcn, __le16 id, const struct MFT_REF *ref,
0460           struct ATTR_LIST_ENTRY **new_le);
0461 bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le);
0462 bool al_delete_le(struct ntfs_inode *ni, enum ATTR_TYPE type, CLST vcn,
0463           const __le16 *name, size_t name_len,
0464           const struct MFT_REF *ref);
0465 int al_update(struct ntfs_inode *ni, int sync);
0466 static inline size_t al_aligned(size_t size)
0467 {
0468     return (size + 1023) & ~(size_t)1023;
0469 }
0470 
0471 /* Globals from bitfunc.c */
0472 bool are_bits_clear(const ulong *map, size_t bit, size_t nbits);
0473 bool are_bits_set(const ulong *map, size_t bit, size_t nbits);
0474 size_t get_set_bits_ex(const ulong *map, size_t bit, size_t nbits);
0475 
0476 /* Globals from dir.c */
0477 int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const __le16 *name, u32 len,
0478               u8 *buf, int buf_len);
0479 int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
0480               struct cpu_str *uni, u32 max_ulen,
0481               enum utf16_endian endian);
0482 struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni,
0483                struct ntfs_fnd *fnd);
0484 bool dir_is_empty(struct inode *dir);
0485 extern const struct file_operations ntfs_dir_operations;
0486 
0487 /* Globals from file.c */
0488 int ntfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
0489          struct kstat *stat, u32 request_mask, u32 flags);
0490 void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
0491              CLST len);
0492 int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0493           struct iattr *attr);
0494 int ntfs_file_open(struct inode *inode, struct file *file);
0495 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
0496         __u64 start, __u64 len);
0497 extern const struct inode_operations ntfs_special_inode_operations;
0498 extern const struct inode_operations ntfs_file_inode_operations;
0499 extern const struct file_operations ntfs_file_operations;
0500 
0501 /* Globals from frecord.c */
0502 void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi);
0503 struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni);
0504 struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni);
0505 void ni_clear(struct ntfs_inode *ni);
0506 int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi);
0507 int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
0508            struct mft_inode **mi);
0509 struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
0510                 struct ATTR_LIST_ENTRY **entry_o,
0511                 enum ATTR_TYPE type, const __le16 *name,
0512                 u8 name_len, const CLST *vcn,
0513                 struct mft_inode **mi);
0514 struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
0515                    struct ATTR_LIST_ENTRY **le,
0516                    struct mft_inode **mi);
0517 struct ATTRIB *ni_load_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
0518                 const __le16 *name, u8 name_len, CLST vcn,
0519                 struct mft_inode **pmi);
0520 int ni_load_all_mi(struct ntfs_inode *ni);
0521 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi);
0522 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
0523            const __le16 *name, size_t name_len, bool base_only,
0524            const __le16 *id);
0525 int ni_create_attr_list(struct ntfs_inode *ni);
0526 int ni_expand_list(struct ntfs_inode *ni);
0527 int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
0528               const __le16 *name, u8 name_len,
0529               const struct runs_tree *run, CLST svcn, CLST len,
0530               __le16 flags, struct ATTRIB **new_attr,
0531               struct mft_inode **mi, struct ATTR_LIST_ENTRY **le);
0532 int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
0533                enum ATTR_TYPE type, const __le16 *name, u8 name_len,
0534                struct ATTRIB **new_attr, struct mft_inode **mi,
0535                struct ATTR_LIST_ENTRY **le);
0536 void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
0537                struct mft_inode *mi, struct ATTR_LIST_ENTRY *le);
0538 int ni_delete_all(struct ntfs_inode *ni);
0539 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
0540                      const struct cpu_str *uni,
0541                      const struct MFT_REF *home,
0542                      struct mft_inode **mi,
0543                      struct ATTR_LIST_ENTRY **entry);
0544 struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
0545                      struct mft_inode **mi,
0546                      struct ATTR_LIST_ENTRY **entry);
0547 int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa);
0548 enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
0549                    struct REPARSE_DATA_BUFFER *buffer);
0550 int ni_write_inode(struct inode *inode, int sync, const char *hint);
0551 #define _ni_write_inode(i, w) ni_write_inode(i, w, __func__)
0552 int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
0553           __u64 vbo, __u64 len);
0554 int ni_readpage_cmpr(struct ntfs_inode *ni, struct page *page);
0555 int ni_decompress_file(struct ntfs_inode *ni);
0556 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
0557           u32 pages_per_frame);
0558 int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
0559            u32 pages_per_frame);
0560 int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
0561            struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step);
0562 
0563 bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
0564              struct NTFS_DE *de, struct NTFS_DE *de2,
0565              int undo_step);
0566 
0567 int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
0568         struct NTFS_DE *de);
0569 
0570 int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
0571           struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de,
0572           bool *is_bad);
0573 
0574 bool ni_is_dirty(struct inode *inode);
0575 
0576 /* Globals from fslog.c */
0577 int log_replay(struct ntfs_inode *ni, bool *initialized);
0578 
0579 /* Globals from fsntfs.c */
0580 bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes);
0581 int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes,
0582                bool simple);
0583 int ntfs_extend_init(struct ntfs_sb_info *sbi);
0584 int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi);
0585 const struct ATTR_DEF_ENTRY *ntfs_query_def(struct ntfs_sb_info *sbi,
0586                         enum ATTR_TYPE Type);
0587 int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
0588                  CLST *new_lcn, CLST *new_len,
0589                  enum ALLOCATE_OPT opt);
0590 int ntfs_look_free_mft(struct ntfs_sb_info *sbi, CLST *rno, bool mft,
0591                struct ntfs_inode *ni, struct mft_inode **mi);
0592 void ntfs_mark_rec_free(struct ntfs_sb_info *sbi, CLST rno, bool is_mft);
0593 int ntfs_clear_mft_tail(struct ntfs_sb_info *sbi, size_t from, size_t to);
0594 int ntfs_refresh_zone(struct ntfs_sb_info *sbi);
0595 void ntfs_update_mftmirr(struct ntfs_sb_info *sbi, int wait);
0596 void ntfs_bad_inode(struct inode *inode, const char *hint);
0597 #define _ntfs_bad_inode(i) ntfs_bad_inode(i, __func__)
0598 enum NTFS_DIRTY_FLAGS {
0599     NTFS_DIRTY_CLEAR = 0,
0600     NTFS_DIRTY_DIRTY = 1,
0601     NTFS_DIRTY_ERROR = 2,
0602 };
0603 int ntfs_set_state(struct ntfs_sb_info *sbi, enum NTFS_DIRTY_FLAGS dirty);
0604 int ntfs_sb_read(struct super_block *sb, u64 lbo, size_t bytes, void *buffer);
0605 int ntfs_sb_write(struct super_block *sb, u64 lbo, size_t bytes,
0606           const void *buffer, int wait);
0607 int ntfs_sb_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
0608               u64 vbo, const void *buf, size_t bytes, int sync);
0609 struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
0610                    const struct runs_tree *run, u64 vbo);
0611 int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
0612              u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb);
0613 int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
0614          struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
0615          struct ntfs_buffers *nb);
0616 int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
0617         u32 bytes, struct ntfs_buffers *nb);
0618 int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
0619           struct ntfs_buffers *nb, int sync);
0620 int ntfs_bio_pages(struct ntfs_sb_info *sbi, const struct runs_tree *run,
0621            struct page **pages, u32 nr_pages, u64 vbo, u32 bytes,
0622            enum req_op op);
0623 int ntfs_bio_fill_1(struct ntfs_sb_info *sbi, const struct runs_tree *run);
0624 int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
0625             u64 vbo, u64 *lbo, u64 *bytes);
0626 struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST nRec,
0627                   bool dir);
0628 extern const u8 s_default_security[0x50];
0629 bool is_sd_valid(const struct SECURITY_DESCRIPTOR_RELATIVE *sd, u32 len);
0630 int ntfs_security_init(struct ntfs_sb_info *sbi);
0631 int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
0632                 struct SECURITY_DESCRIPTOR_RELATIVE **sd,
0633                 size_t *size);
0634 int ntfs_insert_security(struct ntfs_sb_info *sbi,
0635              const struct SECURITY_DESCRIPTOR_RELATIVE *sd,
0636              u32 size, __le32 *security_id, bool *inserted);
0637 int ntfs_reparse_init(struct ntfs_sb_info *sbi);
0638 int ntfs_objid_init(struct ntfs_sb_info *sbi);
0639 int ntfs_objid_remove(struct ntfs_sb_info *sbi, struct GUID *guid);
0640 int ntfs_insert_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
0641             const struct MFT_REF *ref);
0642 int ntfs_remove_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
0643             const struct MFT_REF *ref);
0644 void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len, bool trim);
0645 int run_deallocate(struct ntfs_sb_info *sbi, struct runs_tree *run, bool trim);
0646 
0647 /* Globals from index.c */
0648 int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit);
0649 void fnd_clear(struct ntfs_fnd *fnd);
0650 static inline struct ntfs_fnd *fnd_get(void)
0651 {
0652     return kzalloc(sizeof(struct ntfs_fnd), GFP_NOFS);
0653 }
0654 static inline void fnd_put(struct ntfs_fnd *fnd)
0655 {
0656     if (fnd) {
0657         fnd_clear(fnd);
0658         kfree(fnd);
0659     }
0660 }
0661 void indx_clear(struct ntfs_index *idx);
0662 int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
0663           const struct ATTRIB *attr, enum index_mutex_classed type);
0664 struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
0665                  struct ATTRIB **attr, struct mft_inode **mi);
0666 int indx_read(struct ntfs_index *idx, struct ntfs_inode *ni, CLST vbn,
0667           struct indx_node **node);
0668 int indx_find(struct ntfs_index *indx, struct ntfs_inode *dir,
0669           const struct INDEX_ROOT *root, const void *Key, size_t KeyLen,
0670           const void *param, int *diff, struct NTFS_DE **entry,
0671           struct ntfs_fnd *fnd);
0672 int indx_find_sort(struct ntfs_index *indx, struct ntfs_inode *ni,
0673            const struct INDEX_ROOT *root, struct NTFS_DE **entry,
0674            struct ntfs_fnd *fnd);
0675 int indx_find_raw(struct ntfs_index *indx, struct ntfs_inode *ni,
0676           const struct INDEX_ROOT *root, struct NTFS_DE **entry,
0677           size_t *off, struct ntfs_fnd *fnd);
0678 int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
0679               const struct NTFS_DE *new_de, const void *param,
0680               struct ntfs_fnd *fnd, bool undo);
0681 int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
0682               const void *key, u32 key_len, const void *param);
0683 int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
0684             const struct ATTR_FILE_NAME *fname,
0685             const struct NTFS_DUP_INFO *dup, int sync);
0686 
0687 /* Globals from inode.c */
0688 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
0689              const struct cpu_str *name);
0690 int ntfs_set_size(struct inode *inode, u64 new_size);
0691 int reset_log_file(struct inode *inode);
0692 int ntfs_get_block(struct inode *inode, sector_t vbn,
0693            struct buffer_head *bh_result, int create);
0694 int ntfs_write_begin(struct file *file, struct address_space *mapping,
0695              loff_t pos, u32 len, struct page **pagep, void **fsdata);
0696 int ntfs_write_end(struct file *file, struct address_space *mapping,
0697            loff_t pos, u32 len, u32 copied, struct page *page,
0698            void *fsdata);
0699 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc);
0700 int ntfs_sync_inode(struct inode *inode);
0701 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
0702               struct inode *i2);
0703 int inode_write_data(struct inode *inode, const void *data, size_t bytes);
0704 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
0705                 struct inode *dir, struct dentry *dentry,
0706                 const struct cpu_str *uni, umode_t mode,
0707                 dev_t dev, const char *symname, u32 size,
0708                 struct ntfs_fnd *fnd);
0709 int ntfs_link_inode(struct inode *inode, struct dentry *dentry);
0710 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry);
0711 void ntfs_evict_inode(struct inode *inode);
0712 extern const struct inode_operations ntfs_link_inode_operations;
0713 extern const struct address_space_operations ntfs_aops;
0714 extern const struct address_space_operations ntfs_aops_cmpr;
0715 
0716 /* Globals from name_i.c */
0717 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
0718          const struct cpu_str *uni);
0719 struct dentry *ntfs3_get_parent(struct dentry *child);
0720 
0721 extern const struct inode_operations ntfs_dir_inode_operations;
0722 extern const struct inode_operations ntfs_special_inode_operations;
0723 
0724 /* Globals from record.c */
0725 int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi);
0726 void mi_put(struct mft_inode *mi);
0727 int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno);
0728 int mi_read(struct mft_inode *mi, bool is_mft);
0729 struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr);
0730 // TODO: id?
0731 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
0732                 enum ATTR_TYPE type, const __le16 *name,
0733                 size_t name_len, const __le16 *id);
0734 static inline struct ATTRIB *rec_find_attr_le(struct mft_inode *rec,
0735                           struct ATTR_LIST_ENTRY *le)
0736 {
0737     return mi_find_attr(rec, NULL, le->type, le_name(le), le->name_len,
0738                 &le->id);
0739 }
0740 int mi_write(struct mft_inode *mi, int wait);
0741 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
0742           __le16 flags, bool is_mft);
0743 struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
0744                   const __le16 *name, u8 name_len, u32 asize,
0745                   u16 name_off);
0746 
0747 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
0748             struct ATTRIB *attr);
0749 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes);
0750 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
0751          struct runs_tree *run, CLST len);
0752 static inline bool mi_is_ref(const struct mft_inode *mi,
0753                  const struct MFT_REF *ref)
0754 {
0755     if (le32_to_cpu(ref->low) != mi->rno)
0756         return false;
0757     if (ref->seq != mi->mrec->seq)
0758         return false;
0759 
0760 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
0761     return le16_to_cpu(ref->high) == (mi->rno >> 32);
0762 #else
0763     return !ref->high;
0764 #endif
0765 }
0766 
0767 static inline void mi_get_ref(const struct mft_inode *mi, struct MFT_REF *ref)
0768 {
0769     ref->low = cpu_to_le32(mi->rno);
0770 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
0771     ref->high = cpu_to_le16(mi->rno >> 32);
0772 #else
0773     ref->high = 0;
0774 #endif
0775     ref->seq = mi->mrec->seq;
0776 }
0777 
0778 /* Globals from run.c */
0779 bool run_lookup_entry(const struct runs_tree *run, CLST vcn, CLST *lcn,
0780               CLST *len, size_t *index);
0781 void run_truncate(struct runs_tree *run, CLST vcn);
0782 void run_truncate_head(struct runs_tree *run, CLST vcn);
0783 void run_truncate_around(struct runs_tree *run, CLST vcn);
0784 bool run_add_entry(struct runs_tree *run, CLST vcn, CLST lcn, CLST len,
0785            bool is_mft);
0786 bool run_collapse_range(struct runs_tree *run, CLST vcn, CLST len);
0787 bool run_insert_range(struct runs_tree *run, CLST vcn, CLST len);
0788 bool run_get_entry(const struct runs_tree *run, size_t index, CLST *vcn,
0789            CLST *lcn, CLST *len);
0790 bool run_is_mapped_full(const struct runs_tree *run, CLST svcn, CLST evcn);
0791 
0792 int run_pack(const struct runs_tree *run, CLST svcn, CLST len, u8 *run_buf,
0793          u32 run_buf_size, CLST *packed_vcns);
0794 int run_unpack(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
0795            CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
0796            u32 run_buf_size);
0797 
0798 #ifdef NTFS3_CHECK_FREE_CLST
0799 int run_unpack_ex(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
0800           CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
0801           u32 run_buf_size);
0802 #else
0803 #define run_unpack_ex run_unpack
0804 #endif
0805 int run_get_highest_vcn(CLST vcn, const u8 *run_buf, u64 *highest_vcn);
0806 int run_clone(const struct runs_tree *run, struct runs_tree *new_run);
0807 
0808 /* Globals from super.c */
0809 void *ntfs_set_shared(void *ptr, u32 bytes);
0810 void *ntfs_put_shared(void *ptr);
0811 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len);
0812 int ntfs_discard(struct ntfs_sb_info *sbi, CLST Lcn, CLST Len);
0813 
0814 /* Globals from bitmap.c*/
0815 int __init ntfs3_init_bitmap(void);
0816 void ntfs3_exit_bitmap(void);
0817 void wnd_close(struct wnd_bitmap *wnd);
0818 static inline size_t wnd_zeroes(const struct wnd_bitmap *wnd)
0819 {
0820     return wnd->total_zeroes;
0821 }
0822 int wnd_init(struct wnd_bitmap *wnd, struct super_block *sb, size_t nbits);
0823 int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits);
0824 int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits);
0825 bool wnd_is_free(struct wnd_bitmap *wnd, size_t bit, size_t bits);
0826 bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits);
0827 
0828 /* Possible values for 'flags' 'wnd_find'. */
0829 #define BITMAP_FIND_MARK_AS_USED 0x01
0830 #define BITMAP_FIND_FULL 0x02
0831 size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
0832         size_t flags, size_t *allocated);
0833 int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits);
0834 void wnd_zone_set(struct wnd_bitmap *wnd, size_t Lcn, size_t Len);
0835 int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range);
0836 
0837 /* Globals from upcase.c */
0838 int ntfs_cmp_names(const __le16 *s1, size_t l1, const __le16 *s2, size_t l2,
0839            const u16 *upcase, bool bothcase);
0840 int ntfs_cmp_names_cpu(const struct cpu_str *uni1, const struct le_str *uni2,
0841                const u16 *upcase, bool bothcase);
0842 
0843 /* globals from xattr.c */
0844 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
0845 struct posix_acl *ntfs_get_acl(struct inode *inode, int type, bool rcu);
0846 int ntfs_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
0847          struct posix_acl *acl, int type);
0848 int ntfs_init_acl(struct user_namespace *mnt_userns, struct inode *inode,
0849           struct inode *dir);
0850 #else
0851 #define ntfs_get_acl NULL
0852 #define ntfs_set_acl NULL
0853 #endif
0854 
0855 int ntfs_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode);
0856 int ntfs_permission(struct user_namespace *mnt_userns, struct inode *inode,
0857             int mask);
0858 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
0859 extern const struct xattr_handler *ntfs_xattr_handlers[];
0860 
0861 int ntfs_save_wsl_perm(struct inode *inode);
0862 void ntfs_get_wsl_perm(struct inode *inode);
0863 
0864 /* globals from lznt.c */
0865 struct lznt *get_lznt_ctx(int level);
0866 size_t compress_lznt(const void *uncompressed, size_t uncompressed_size,
0867              void *compressed, size_t compressed_size,
0868              struct lznt *ctx);
0869 ssize_t decompress_lznt(const void *compressed, size_t compressed_size,
0870             void *uncompressed, size_t uncompressed_size);
0871 
0872 static inline bool is_ntfs3(struct ntfs_sb_info *sbi)
0873 {
0874     return sbi->volume.major_ver >= 3;
0875 }
0876 
0877 /* (sb->s_flags & SB_ACTIVE) */
0878 static inline bool is_mounted(struct ntfs_sb_info *sbi)
0879 {
0880     return !!sbi->sb->s_root;
0881 }
0882 
0883 static inline bool ntfs_is_meta_file(struct ntfs_sb_info *sbi, CLST rno)
0884 {
0885     return rno < MFT_REC_FREE || rno == sbi->objid_no ||
0886            rno == sbi->quota_no || rno == sbi->reparse_no ||
0887            rno == sbi->usn_jrnl_no;
0888 }
0889 
0890 static inline void ntfs_unmap_page(struct page *page)
0891 {
0892     kunmap(page);
0893     put_page(page);
0894 }
0895 
0896 static inline struct page *ntfs_map_page(struct address_space *mapping,
0897                      unsigned long index)
0898 {
0899     struct page *page = read_mapping_page(mapping, index, NULL);
0900 
0901     if (!IS_ERR(page))
0902         kmap(page);
0903     return page;
0904 }
0905 
0906 static inline size_t wnd_zone_bit(const struct wnd_bitmap *wnd)
0907 {
0908     return wnd->zone_bit;
0909 }
0910 
0911 static inline size_t wnd_zone_len(const struct wnd_bitmap *wnd)
0912 {
0913     return wnd->zone_end - wnd->zone_bit;
0914 }
0915 
0916 static inline void run_init(struct runs_tree *run)
0917 {
0918     run->runs = NULL;
0919     run->count = 0;
0920     run->allocated = 0;
0921 }
0922 
0923 static inline struct runs_tree *run_alloc(void)
0924 {
0925     return kzalloc(sizeof(struct runs_tree), GFP_NOFS);
0926 }
0927 
0928 static inline void run_close(struct runs_tree *run)
0929 {
0930     kvfree(run->runs);
0931     memset(run, 0, sizeof(*run));
0932 }
0933 
0934 static inline void run_free(struct runs_tree *run)
0935 {
0936     if (run) {
0937         kvfree(run->runs);
0938         kfree(run);
0939     }
0940 }
0941 
0942 static inline bool run_is_empty(struct runs_tree *run)
0943 {
0944     return !run->count;
0945 }
0946 
0947 /* NTFS uses quad aligned bitmaps. */
0948 static inline size_t bitmap_size(size_t bits)
0949 {
0950     return ALIGN((bits + 7) >> 3, 8);
0951 }
0952 
0953 #define _100ns2seconds 10000000
0954 #define SecondsToStartOf1970 0x00000002B6109100
0955 
0956 #define NTFS_TIME_GRAN 100
0957 
0958 /*
0959  * kernel2nt - Converts in-memory kernel timestamp into nt time.
0960  */
0961 static inline __le64 kernel2nt(const struct timespec64 *ts)
0962 {
0963     // 10^7 units of 100 nanoseconds one second
0964     return cpu_to_le64(_100ns2seconds *
0965                    (ts->tv_sec + SecondsToStartOf1970) +
0966                ts->tv_nsec / NTFS_TIME_GRAN);
0967 }
0968 
0969 /*
0970  * nt2kernel - Converts on-disk nt time into kernel timestamp.
0971  */
0972 static inline void nt2kernel(const __le64 tm, struct timespec64 *ts)
0973 {
0974     u64 t = le64_to_cpu(tm) - _100ns2seconds * SecondsToStartOf1970;
0975 
0976     // WARNING: do_div changes its first argument(!)
0977     ts->tv_nsec = do_div(t, _100ns2seconds) * 100;
0978     ts->tv_sec = t;
0979 }
0980 
0981 static inline struct ntfs_sb_info *ntfs_sb(struct super_block *sb)
0982 {
0983     return sb->s_fs_info;
0984 }
0985 
0986 /*
0987  * ntfs_up_cluster - Align up on cluster boundary.
0988  */
0989 static inline u64 ntfs_up_cluster(const struct ntfs_sb_info *sbi, u64 size)
0990 {
0991     return (size + sbi->cluster_mask) & sbi->cluster_mask_inv;
0992 }
0993 
0994 /*
0995  * ntfs_up_block - Align up on cluster boundary.
0996  */
0997 static inline u64 ntfs_up_block(const struct super_block *sb, u64 size)
0998 {
0999     return (size + sb->s_blocksize - 1) & ~(u64)(sb->s_blocksize - 1);
1000 }
1001 
1002 static inline CLST bytes_to_cluster(const struct ntfs_sb_info *sbi, u64 size)
1003 {
1004     return (size + sbi->cluster_mask) >> sbi->cluster_bits;
1005 }
1006 
1007 static inline u64 bytes_to_block(const struct super_block *sb, u64 size)
1008 {
1009     return (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1010 }
1011 
1012 static inline struct buffer_head *ntfs_bread(struct super_block *sb,
1013                          sector_t block)
1014 {
1015     struct buffer_head *bh = sb_bread(sb, block);
1016 
1017     if (bh)
1018         return bh;
1019 
1020     ntfs_err(sb, "failed to read volume at offset 0x%llx",
1021          (u64)block << sb->s_blocksize_bits);
1022     return NULL;
1023 }
1024 
1025 static inline struct ntfs_inode *ntfs_i(struct inode *inode)
1026 {
1027     return container_of(inode, struct ntfs_inode, vfs_inode);
1028 }
1029 
1030 static inline bool is_compressed(const struct ntfs_inode *ni)
1031 {
1032     return (ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) ||
1033            (ni->ni_flags & NI_FLAG_COMPRESSED_MASK);
1034 }
1035 
1036 static inline int ni_ext_compress_bits(const struct ntfs_inode *ni)
1037 {
1038     return 0xb + (ni->ni_flags & NI_FLAG_COMPRESSED_MASK);
1039 }
1040 
1041 /* Bits - 0xc, 0xd, 0xe, 0xf, 0x10 */
1042 static inline void ni_set_ext_compress_bits(struct ntfs_inode *ni, u8 bits)
1043 {
1044     ni->ni_flags |= (bits - 0xb) & NI_FLAG_COMPRESSED_MASK;
1045 }
1046 
1047 static inline bool is_dedup(const struct ntfs_inode *ni)
1048 {
1049     return ni->ni_flags & NI_FLAG_DEDUPLICATED;
1050 }
1051 
1052 static inline bool is_encrypted(const struct ntfs_inode *ni)
1053 {
1054     return ni->std_fa & FILE_ATTRIBUTE_ENCRYPTED;
1055 }
1056 
1057 static inline bool is_sparsed(const struct ntfs_inode *ni)
1058 {
1059     return ni->std_fa & FILE_ATTRIBUTE_SPARSE_FILE;
1060 }
1061 
1062 static inline int is_resident(struct ntfs_inode *ni)
1063 {
1064     return ni->ni_flags & NI_FLAG_RESIDENT;
1065 }
1066 
1067 static inline void le16_sub_cpu(__le16 *var, u16 val)
1068 {
1069     *var = cpu_to_le16(le16_to_cpu(*var) - val);
1070 }
1071 
1072 static inline void le32_sub_cpu(__le32 *var, u32 val)
1073 {
1074     *var = cpu_to_le32(le32_to_cpu(*var) - val);
1075 }
1076 
1077 static inline void nb_put(struct ntfs_buffers *nb)
1078 {
1079     u32 i, nbufs = nb->nbufs;
1080 
1081     if (!nbufs)
1082         return;
1083 
1084     for (i = 0; i < nbufs; i++)
1085         put_bh(nb->bh[i]);
1086     nb->nbufs = 0;
1087 }
1088 
1089 static inline void put_indx_node(struct indx_node *in)
1090 {
1091     if (!in)
1092         return;
1093 
1094     kfree(in->index);
1095     nb_put(&in->nb);
1096     kfree(in);
1097 }
1098 
1099 static inline void mi_clear(struct mft_inode *mi)
1100 {
1101     nb_put(&mi->nb);
1102     kfree(mi->mrec);
1103     mi->mrec = NULL;
1104 }
1105 
1106 static inline void ni_lock(struct ntfs_inode *ni)
1107 {
1108     mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_NORMAL);
1109 }
1110 
1111 static inline void ni_lock_dir(struct ntfs_inode *ni)
1112 {
1113     mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_PARENT);
1114 }
1115 
1116 static inline void ni_unlock(struct ntfs_inode *ni)
1117 {
1118     mutex_unlock(&ni->ni_lock);
1119 }
1120 
1121 static inline int ni_trylock(struct ntfs_inode *ni)
1122 {
1123     return mutex_trylock(&ni->ni_lock);
1124 }
1125 
1126 static inline int attr_load_runs_attr(struct ntfs_inode *ni,
1127                       struct ATTRIB *attr,
1128                       struct runs_tree *run, CLST vcn)
1129 {
1130     return attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1131                   attr->name_len, run, vcn);
1132 }
1133 
1134 static inline void le64_sub_cpu(__le64 *var, u64 val)
1135 {
1136     *var = cpu_to_le64(le64_to_cpu(*var) - val);
1137 }
1138 
1139 #endif /* _LINUX_NTFS3_NTFS_FS_H */