1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Defines for inode structures NTFS Linux kernel driver. 4 * 5 * Copyright (c) 2001-2007 Anton Altaparmakov 6 * Copyright (c) 2002 Richard Russon 7 * Copyright (c) 2025 LG Electronics Co., Ltd. 8 */ 9 10 #ifndef _LINUX_NTFS_INODE_H 11 #define _LINUX_NTFS_INODE_H 12 13 #include "debug.h" 14 15 enum ntfs_inode_mutex_lock_class { 16 NTFS_INODE_MUTEX_PARENT, 17 NTFS_INODE_MUTEX_NORMAL, 18 NTFS_INODE_MUTEX_NORMAL_CHILD, 19 NTFS_INODE_MUTEX_PARENT_2, 20 NTFS_INODE_MUTEX_NORMAL_2, 21 NTFS_EXTEND_MUTEX_PARENT, 22 NTFS_EA_MUTEX_NORMAL 23 }; 24 25 /* 26 * The NTFS in-memory inode structure. It is just used as an extension to the 27 * fields already provided in the VFS inode. 28 * @size_lock: Lock serializing access to inode sizes. 29 * @state: NTFS specific flags describing this inode. 30 * @flags: Flags describing the file. (Copy from STANDARD_INFORMATION). 31 * @mft_no: Number of the mft record / inode. 32 * @seq_no: Sequence number of the mft record. 33 * @count: Inode reference count for book keeping. 34 * @vol: Pointer to the ntfs volume of this inode. 35 * 36 * If NInoAttr() is true, the below fields describe the attribute which 37 * this fake inode belongs to. The actual inode of this attribute is 38 * pointed to by base_ntfs_ino and nr_extents is always set to -1 (see 39 * below). For real inodes, we also set the type (AT_DATA for files and 40 * AT_INDEX_ALLOCATION for directories), with the name = NULL and 41 * name_len = 0 for files and name = I30 (global constant) and 42 * name_len = 4 for directories. 43 * @type: Attribute type of this fake inode. 44 * @name: Attribute name of this fake inode. 45 * @name_len: Attribute name length of this fake inode. 46 * @runlist: If state has the NI_NonResident bit set, the runlist of 47 * the unnamed data attribute (if a file) or of the index allocation 48 * attribute (directory) or of the attribute described by the fake inode 49 * (if NInoAttr()). If runlist.rl is NULL, the runlist has not been read 50 * in yet or has been unmapped. If NI_NonResident is clear, the attribute 51 * is resident (file and fake inode) or there is no $I30 index allocation 52 * attribute (small directory). In the latter case runlist.rl is always 53 * NULL. 54 * @data_size: Copy from the attribute record. 55 * @initialized_size: Copy from the attribute record. 56 * @allocated_size: Copy from the attribute record. 57 * @i_crtime: File Creation time. 58 * @mrec: MFT record 59 * @mrec_lock: Lock for serializing access to the mft record belonging to 60 * this inode. 61 * @folio: The folio containing the mft record of the inode. 62 * @folio_ofs: Offset into the folio at which the mft record begins. 63 * @mft_lcn: Number containing the mft record. 64 * @mft_lcn_count: Number of clusters per mft record. 65 * 66 * Attribute list support (only for use by the attribute lookup 67 * functions). Setup during read_inode for all inodes with attribute 68 * lists. Only valid if NI_AttrList is set in state. 69 * @attr_list_size: Length of attribute list value in bytes. 70 * @attr_list: Attribute list value itself. 71 * 72 * It is a directory, $MFT, or an index inode. 73 * @block_size: Size of an index block. 74 * @vcn_size: Size of a vcn in this index. 75 * @collation_rule: The collation rule for the index. 76 * @block_size_bits: Log2 of the above. 77 * @vcn_size_bits: Log2 of the above. 78 * 79 * It is a compressed/sparse file/attribute inode. 80 * @size: Copy of compressed_size from $DATA. 81 * @block_size: Size of a compression block (cb). 82 * @block_size_bits: Log2 of the size of a cb. 83 * @block_clusters: Number of clusters per cb. 84 * @extent_lock: Lock for accessing/modifying the below. 85 * @nr_extents: For a base mft record, the number of attached extent inodes 86 * (0 if none), for extent records and for fake inodes describing an 87 * attribute this is -1. 88 * 89 * This union is only used if nr_extents != 0. 90 * @extent_ntfs_inos: For nr_extents > 0, array of the ntfs inodes of 91 * the extent mft records belonging to this base inode which have been 92 * loaded. 93 * @base_ntfs_ino: For nr_extents == -1, the ntfs inode of the base mft 94 * record. For fake inodes, the real (base) inode to which the attribute 95 * belongs. 96 * @i_dealloc_clusters: delayed allocated clusters. 97 * @target: symlink buffer. 98 */ 99 struct ntfs_inode { 100 rwlock_t size_lock; 101 unsigned long state; 102 __le32 flags; 103 u64 mft_no; 104 u16 seq_no; 105 atomic_t count; 106 struct ntfs_volume *vol; 107 __le32 type; 108 __le16 *name; 109 u32 name_len; 110 struct runlist runlist; 111 s64 data_size; 112 s64 initialized_size; 113 s64 allocated_size; 114 struct timespec64 i_crtime; 115 void *mrec; 116 struct mutex mrec_lock; 117 struct folio *folio; 118 int folio_ofs; 119 s64 mft_lcn[2]; 120 unsigned int mft_lcn_count; 121 u32 attr_list_size; 122 u8 *attr_list; 123 union { 124 struct { 125 u32 block_size; 126 u32 vcn_size; 127 __le32 collation_rule; 128 u8 block_size_bits; 129 u8 vcn_size_bits; 130 } index; 131 struct { 132 s64 size; 133 u32 block_size; 134 u8 block_size_bits; 135 u8 block_clusters; 136 } compressed; 137 } itype; 138 struct mutex extent_lock; 139 s32 nr_extents; 140 union { 141 struct ntfs_inode **extent_ntfs_inos; 142 struct ntfs_inode *base_ntfs_ino; 143 } ext; 144 unsigned int i_dealloc_clusters; 145 __le32 reparse_tag; 146 __le32 reparse_flags; 147 char *target; 148 }; 149 150 /* 151 * Defined bits for the state field in the ntfs_inode structure. 152 * (f) = files only, (d) = directories only, (a) = attributes/fake inodes only 153 * 154 * NI_Dirty Mft record needs to be written to disk. 155 * NI_AttrListDirty Mft record contains an attribute list. 156 * NI_AttrList Mft record contains an attribute list. 157 * NI_AttrListNonResident Attribute list is non-resident. Implies 158 * NI_AttrList is set. 159 * NI_Attr 1: Fake inode for attribute i/o. 160 * 0: Real inode or extent inode. 161 * NI_MstProtected Attribute is protected by MST fixups. 162 * NI_NonResident Unnamed data attr is non-resident (f) 163 * Attribute is non-resident (a). 164 * NI_IndexAllocPresent $I30 index alloc attr is present (d). 165 * NI_Compressed Unnamed data attr is compressed (f). 166 * Create compressed files by default (d). 167 * Attribute is compressed (a). 168 * NI_Encrypted Unnamed data attr is encrypted (f). 169 * Create encrypted files by default (d). 170 * Attribute is encrypted (a). 171 * NI_Sparse Unnamed data attr is sparse (f). 172 * Create sparse files by default (d). 173 * Attribute is sparse (a). 174 * NI_SparseDisabled May not create sparse regions. 175 * NI_FullyMapped Runlist is fully mapped. 176 * NI_FileNameDirty FILE_NAME attributes need to be updated. 177 * NI_BeingDeleted ntfs inode is being delated. 178 * NI_BeingCreated ntfs inode is being created. 179 * NI_HasEA ntfs inode has EA attribute. 180 * NI_RunlistDirty runlist need to be updated. 181 */ 182 enum { 183 NI_Dirty, 184 NI_AttrListDirty, 185 NI_AttrList, 186 NI_AttrListNonResident, 187 NI_Attr, 188 NI_MstProtected, 189 NI_NonResident, 190 NI_IndexAllocPresent, 191 NI_Compressed, 192 NI_WofCompressed, 193 NI_Encrypted, 194 NI_Sparse, 195 NI_SparseDisabled, 196 NI_FullyMapped, 197 NI_FileNameDirty, 198 NI_BeingDeleted, 199 NI_BeingCreated, 200 NI_HasEA, 201 NI_RunlistDirty, 202 }; 203 204 /* 205 * NOTE: We should be adding dirty mft records to a list somewhere and they 206 * should be independent of the (ntfs/vfs) inode structure so that an inode can 207 * be removed but the record can be left dirty for syncing later. 208 */ 209 210 /* 211 * Macro tricks to expand the NInoFoo(), NInoSetFoo(), and NInoClearFoo() 212 * functions. 213 */ 214 #define NINO_FNS(flag) \ 215 static inline int NIno##flag(struct ntfs_inode *ni) \ 216 { \ 217 return test_bit(NI_##flag, &(ni)->state); \ 218 } \ 219 static inline void NInoSet##flag(struct ntfs_inode *ni) \ 220 { \ 221 set_bit(NI_##flag, &(ni)->state); \ 222 } \ 223 static inline void NInoClear##flag(struct ntfs_inode *ni) \ 224 { \ 225 clear_bit(NI_##flag, &(ni)->state); \ 226 } 227 228 /* 229 * As above for NInoTestSetFoo() and NInoTestClearFoo(). 230 */ 231 #define TAS_NINO_FNS(flag) \ 232 static inline int NInoTestSet##flag(struct ntfs_inode *ni) \ 233 { \ 234 return test_and_set_bit(NI_##flag, &(ni)->state); \ 235 } \ 236 static inline int NInoTestClear##flag(struct ntfs_inode *ni) \ 237 { \ 238 return test_and_clear_bit(NI_##flag, &(ni)->state); \ 239 } 240 241 /* Emit the ntfs inode bitops functions. */ 242 NINO_FNS(Dirty) 243 TAS_NINO_FNS(Dirty) 244 NINO_FNS(AttrList) 245 NINO_FNS(AttrListDirty) 246 NINO_FNS(AttrListNonResident) 247 NINO_FNS(Attr) 248 NINO_FNS(MstProtected) 249 NINO_FNS(NonResident) 250 NINO_FNS(IndexAllocPresent) 251 NINO_FNS(Compressed) 252 NINO_FNS(WofCompressed) 253 NINO_FNS(Encrypted) 254 NINO_FNS(Sparse) 255 NINO_FNS(SparseDisabled) 256 NINO_FNS(FullyMapped) 257 NINO_FNS(FileNameDirty) 258 TAS_NINO_FNS(FileNameDirty) 259 NINO_FNS(BeingDeleted) 260 NINO_FNS(HasEA) 261 NINO_FNS(RunlistDirty) 262 263 /* 264 * The full structure containing a ntfs_inode and a vfs struct inode. Used for 265 * all real and fake inodes but not for extent inodes which lack the vfs struct 266 * inode. 267 */ 268 struct big_ntfs_inode { 269 struct ntfs_inode ntfs_inode; 270 struct inode vfs_inode; /* The vfs inode structure. */ 271 }; 272 273 /* 274 * NTFS_I - return the ntfs inode given a vfs inode 275 * @inode: VFS inode 276 * 277 * NTFS_I() returns the ntfs inode associated with the VFS @inode. 278 */ 279 static inline struct ntfs_inode *NTFS_I(struct inode *inode) 280 { 281 return &container_of(inode, struct big_ntfs_inode, vfs_inode)->ntfs_inode; 282 } 283 284 static inline struct inode *VFS_I(struct ntfs_inode *ni) 285 { 286 return &container_of(ni, struct big_ntfs_inode, ntfs_inode)->vfs_inode; 287 } 288 289 /* 290 * ntfs_attr - ntfs in memory attribute structure 291 * 292 * This structure exists only to provide a small structure for the 293 * ntfs_{attr_}iget()/ntfs_test_inode()/ntfs_init_locked_inode() mechanism. 294 * 295 * NOTE: Elements are ordered by size to make the structure as compact as 296 * possible on all architectures. 297 */ 298 struct ntfs_attr { 299 u64 mft_no; 300 __le16 *name; 301 u32 name_len; 302 __le32 type; 303 unsigned long state; 304 }; 305 306 int ntfs_test_inode(struct inode *vi, void *data); 307 struct inode *ntfs_iget(struct super_block *sb, u64 mft_no); 308 struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type, 309 __le16 *name, u32 name_len); 310 struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name, 311 u32 name_len); 312 struct inode *ntfs_alloc_big_inode(struct super_block *sb); 313 void ntfs_free_big_inode(struct inode *inode); 314 int ntfs_drop_big_inode(struct inode *inode); 315 void ntfs_evict_big_inode(struct inode *vi); 316 void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni); 317 318 static inline void ntfs_init_big_inode(struct inode *vi) 319 { 320 struct ntfs_inode *ni = NTFS_I(vi); 321 322 ntfs_debug("Entering."); 323 __ntfs_init_inode(vi->i_sb, ni); 324 ni->mft_no = vi->i_ino; 325 } 326 327 struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, u64 mft_no); 328 void ntfs_clear_extent_inode(struct ntfs_inode *ni); 329 int ntfs_read_inode_mount(struct inode *vi); 330 int ntfs_show_options(struct seq_file *sf, struct dentry *root); 331 int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size); 332 333 int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, 334 struct iattr *attr); 335 int ntfs_getattr(const struct mnt_idmap *idmap, const struct path *path, 336 struct kstat *stat, unsigned int request_mask, 337 unsigned int query_flags); 338 339 int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni); 340 int __ntfs_write_inode(struct inode *vi, int sync); 341 int ntfs_inode_attach_all_extents(struct ntfs_inode *ni); 342 int ntfs_inode_add_attrlist(struct ntfs_inode *ni); 343 int ntfs_inode_free_empty_extents(struct ntfs_inode *ni); 344 void ntfs_destroy_ext_inode(struct ntfs_inode *ni); 345 int ntfs_inode_free_space(struct ntfs_inode *ni, int size); 346 s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf); 347 s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, 348 bool sync); 349 int ntfs_inode_close(struct ntfs_inode *ni); 350 351 static inline void ntfs_commit_inode(struct inode *vi) 352 { 353 __ntfs_write_inode(vi, 1); 354 } 355 356 int ntfs_inode_sync_filename(struct ntfs_inode *ni); 357 int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, 358 const loff_t new_size); 359 void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev); 360 struct folio *ntfs_get_locked_folio(struct address_space *mapping, 361 pgoff_t index, pgoff_t end_index, struct file_ra_state *ra); 362 363 #endif /* _LINUX_NTFS_INODE_H */ 364