xref: /linux/fs/ntfs/inode.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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_Encrypted,
193 	NI_Sparse,
194 	NI_SparseDisabled,
195 	NI_FullyMapped,
196 	NI_FileNameDirty,
197 	NI_BeingDeleted,
198 	NI_BeingCreated,
199 	NI_HasEA,
200 	NI_RunlistDirty,
201 };
202 
203 /*
204  * NOTE: We should be adding dirty mft records to a list somewhere and they
205  * should be independent of the (ntfs/vfs) inode structure so that an inode can
206  * be removed but the record can be left dirty for syncing later.
207  */
208 
209 /*
210  * Macro tricks to expand the NInoFoo(), NInoSetFoo(), and NInoClearFoo()
211  * functions.
212  */
213 #define NINO_FNS(flag)						\
214 static inline int NIno##flag(struct ntfs_inode *ni)		\
215 {								\
216 	return test_bit(NI_##flag, &(ni)->state);		\
217 }								\
218 static inline void NInoSet##flag(struct ntfs_inode *ni)		\
219 {								\
220 	set_bit(NI_##flag, &(ni)->state);			\
221 }								\
222 static inline void NInoClear##flag(struct ntfs_inode *ni)	\
223 {								\
224 	clear_bit(NI_##flag, &(ni)->state);			\
225 }
226 
227 /*
228  * As above for NInoTestSetFoo() and NInoTestClearFoo().
229  */
230 #define TAS_NINO_FNS(flag)						\
231 static inline int NInoTestSet##flag(struct ntfs_inode *ni)		\
232 {									\
233 	return test_and_set_bit(NI_##flag, &(ni)->state);		\
234 }									\
235 static inline int NInoTestClear##flag(struct ntfs_inode *ni)		\
236 {									\
237 	return test_and_clear_bit(NI_##flag, &(ni)->state);		\
238 }
239 
240 /* Emit the ntfs inode bitops functions. */
241 NINO_FNS(Dirty)
242 TAS_NINO_FNS(Dirty)
243 NINO_FNS(AttrList)
244 NINO_FNS(AttrListDirty)
245 NINO_FNS(AttrListNonResident)
246 NINO_FNS(Attr)
247 NINO_FNS(MstProtected)
248 NINO_FNS(NonResident)
249 NINO_FNS(IndexAllocPresent)
250 NINO_FNS(Compressed)
251 NINO_FNS(Encrypted)
252 NINO_FNS(Sparse)
253 NINO_FNS(SparseDisabled)
254 NINO_FNS(FullyMapped)
255 NINO_FNS(FileNameDirty)
256 TAS_NINO_FNS(FileNameDirty)
257 NINO_FNS(BeingDeleted)
258 NINO_FNS(HasEA)
259 NINO_FNS(RunlistDirty)
260 
261 /*
262  * The full structure containing a ntfs_inode and a vfs struct inode. Used for
263  * all real and fake inodes but not for extent inodes which lack the vfs struct
264  * inode.
265  */
266 struct big_ntfs_inode {
267 	struct ntfs_inode ntfs_inode;
268 	struct inode vfs_inode;		/* The vfs inode structure. */
269 };
270 
271 /*
272  * NTFS_I - return the ntfs inode given a vfs inode
273  * @inode:	VFS inode
274  *
275  * NTFS_I() returns the ntfs inode associated with the VFS @inode.
276  */
277 static inline struct ntfs_inode *NTFS_I(struct inode *inode)
278 {
279 	return &container_of(inode, struct big_ntfs_inode, vfs_inode)->ntfs_inode;
280 }
281 
282 static inline struct inode *VFS_I(struct ntfs_inode *ni)
283 {
284 	return &container_of(ni, struct big_ntfs_inode, ntfs_inode)->vfs_inode;
285 }
286 
287 /*
288  * ntfs_attr - ntfs in memory attribute structure
289  *
290  * This structure exists only to provide a small structure for the
291  * ntfs_{attr_}iget()/ntfs_test_inode()/ntfs_init_locked_inode() mechanism.
292  *
293  * NOTE: Elements are ordered by size to make the structure as compact as
294  * possible on all architectures.
295  */
296 struct ntfs_attr {
297 	u64 mft_no;
298 	__le16 *name;
299 	u32 name_len;
300 	__le32 type;
301 	unsigned long state;
302 };
303 
304 int ntfs_test_inode(struct inode *vi, void *data);
305 struct inode *ntfs_iget(struct super_block *sb, u64 mft_no);
306 struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,
307 		__le16 *name, u32 name_len);
308 struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name,
309 		u32 name_len);
310 struct inode *ntfs_alloc_big_inode(struct super_block *sb);
311 void ntfs_free_big_inode(struct inode *inode);
312 int ntfs_drop_big_inode(struct inode *inode);
313 void ntfs_evict_big_inode(struct inode *vi);
314 void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni);
315 
316 static inline void ntfs_init_big_inode(struct inode *vi)
317 {
318 	struct ntfs_inode *ni = NTFS_I(vi);
319 
320 	ntfs_debug("Entering.");
321 	__ntfs_init_inode(vi->i_sb, ni);
322 	ni->mft_no = vi->i_ino;
323 }
324 
325 struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, u64 mft_no);
326 void ntfs_clear_extent_inode(struct ntfs_inode *ni);
327 int ntfs_read_inode_mount(struct inode *vi);
328 int ntfs_show_options(struct seq_file *sf, struct dentry *root);
329 int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size);
330 
331 int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
332 		 struct iattr *attr);
333 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
334 		struct kstat *stat, unsigned int request_mask,
335 		unsigned int query_flags);
336 
337 int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni);
338 int __ntfs_write_inode(struct inode *vi, int sync);
339 int ntfs_inode_attach_all_extents(struct ntfs_inode *ni);
340 int ntfs_inode_add_attrlist(struct ntfs_inode *ni);
341 void ntfs_destroy_ext_inode(struct ntfs_inode *ni);
342 int ntfs_inode_free_space(struct ntfs_inode *ni, int size);
343 s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf);
344 s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf,
345 		bool sync);
346 int ntfs_inode_close(struct ntfs_inode *ni);
347 
348 static inline void ntfs_commit_inode(struct inode *vi)
349 {
350 	__ntfs_write_inode(vi, 1);
351 }
352 
353 int ntfs_inode_sync_filename(struct ntfs_inode *ni);
354 int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
355 		const loff_t new_size, bool bsync);
356 void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev);
357 struct folio *ntfs_get_locked_folio(struct address_space *mapping,
358 		pgoff_t index, pgoff_t end_index, struct file_ra_state *ra);
359 
360 #endif /* _LINUX_NTFS_INODE_H */
361