xref: /linux/fs/ntfs/inode.h (revision cdd4dc3aebeab43a72ce0bc2b5bab6f0a80b97a5)
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 	char *target;
146 };
147 
148 /*
149  * Defined bits for the state field in the ntfs_inode structure.
150  * (f) = files only, (d) = directories only, (a) = attributes/fake inodes only
151  *
152  * NI_Dirty			Mft record needs to be written to disk.
153  * NI_AttrListDirty		Mft record contains an attribute list.
154  * NI_AttrList			Mft record contains an attribute list.
155  * NI_AttrListNonResident	Attribute list is non-resident. Implies
156  *				NI_AttrList is set.
157  * NI_Attr			1: Fake inode for attribute i/o.
158  *				0: Real inode or extent inode.
159  * NI_MstProtected		Attribute is protected by MST fixups.
160  * NI_NonResident		Unnamed data attr is non-resident (f)
161  *				Attribute is non-resident (a).
162  * NI_IndexAllocPresent		$I30 index alloc attr is present (d).
163  * NI_Compressed		Unnamed data attr is compressed (f).
164  *				Create compressed files by default (d).
165  *				Attribute is compressed (a).
166  * NI_Encrypted			Unnamed data attr is encrypted (f).
167  *				Create encrypted files by default (d).
168  *				Attribute is encrypted (a).
169  * NI_Sparse			Unnamed data attr is sparse (f).
170  *				Create sparse files by default (d).
171  *				Attribute is sparse (a).
172  * NI_SparseDisabled		May not create sparse regions.
173  * NI_FullyMapped		Runlist is fully mapped.
174  * NI_FileNameDirty		FILE_NAME attributes need to be updated.
175  * NI_BeingDeleted		ntfs inode is being delated.
176  * NI_BeingCreated		ntfs inode is being created.
177  * NI_HasEA			ntfs inode has EA attribute.
178  * NI_RunlistDirty		runlist need to be updated.
179  */
180 enum {
181 	NI_Dirty,
182 	NI_AttrListDirty,
183 	NI_AttrList,
184 	NI_AttrListNonResident,
185 	NI_Attr,
186 	NI_MstProtected,
187 	NI_NonResident,
188 	NI_IndexAllocPresent,
189 	NI_Compressed,
190 	NI_Encrypted,
191 	NI_Sparse,
192 	NI_SparseDisabled,
193 	NI_FullyMapped,
194 	NI_FileNameDirty,
195 	NI_BeingDeleted,
196 	NI_BeingCreated,
197 	NI_HasEA,
198 	NI_RunlistDirty,
199 };
200 
201 /*
202  * NOTE: We should be adding dirty mft records to a list somewhere and they
203  * should be independent of the (ntfs/vfs) inode structure so that an inode can
204  * be removed but the record can be left dirty for syncing later.
205  */
206 
207 /*
208  * Macro tricks to expand the NInoFoo(), NInoSetFoo(), and NInoClearFoo()
209  * functions.
210  */
211 #define NINO_FNS(flag)						\
212 static inline int NIno##flag(struct ntfs_inode *ni)		\
213 {								\
214 	return test_bit(NI_##flag, &(ni)->state);		\
215 }								\
216 static inline void NInoSet##flag(struct ntfs_inode *ni)		\
217 {								\
218 	set_bit(NI_##flag, &(ni)->state);			\
219 }								\
220 static inline void NInoClear##flag(struct ntfs_inode *ni)	\
221 {								\
222 	clear_bit(NI_##flag, &(ni)->state);			\
223 }
224 
225 /*
226  * As above for NInoTestSetFoo() and NInoTestClearFoo().
227  */
228 #define TAS_NINO_FNS(flag)						\
229 static inline int NInoTestSet##flag(struct ntfs_inode *ni)		\
230 {									\
231 	return test_and_set_bit(NI_##flag, &(ni)->state);		\
232 }									\
233 static inline int NInoTestClear##flag(struct ntfs_inode *ni)		\
234 {									\
235 	return test_and_clear_bit(NI_##flag, &(ni)->state);		\
236 }
237 
238 /* Emit the ntfs inode bitops functions. */
239 NINO_FNS(Dirty)
240 TAS_NINO_FNS(Dirty)
241 NINO_FNS(AttrList)
242 NINO_FNS(AttrListDirty)
243 NINO_FNS(AttrListNonResident)
244 NINO_FNS(Attr)
245 NINO_FNS(MstProtected)
246 NINO_FNS(NonResident)
247 NINO_FNS(IndexAllocPresent)
248 NINO_FNS(Compressed)
249 NINO_FNS(Encrypted)
250 NINO_FNS(Sparse)
251 NINO_FNS(SparseDisabled)
252 NINO_FNS(FullyMapped)
253 NINO_FNS(FileNameDirty)
254 TAS_NINO_FNS(FileNameDirty)
255 NINO_FNS(BeingDeleted)
256 NINO_FNS(HasEA)
257 NINO_FNS(RunlistDirty)
258 
259 /*
260  * The full structure containing a ntfs_inode and a vfs struct inode. Used for
261  * all real and fake inodes but not for extent inodes which lack the vfs struct
262  * inode.
263  */
264 struct big_ntfs_inode {
265 	struct ntfs_inode ntfs_inode;
266 	struct inode vfs_inode;		/* The vfs inode structure. */
267 };
268 
269 /*
270  * NTFS_I - return the ntfs inode given a vfs inode
271  * @inode:	VFS inode
272  *
273  * NTFS_I() returns the ntfs inode associated with the VFS @inode.
274  */
275 static inline struct ntfs_inode *NTFS_I(struct inode *inode)
276 {
277 	return &container_of(inode, struct big_ntfs_inode, vfs_inode)->ntfs_inode;
278 }
279 
280 static inline struct inode *VFS_I(struct ntfs_inode *ni)
281 {
282 	return &container_of(ni, struct big_ntfs_inode, ntfs_inode)->vfs_inode;
283 }
284 
285 /*
286  * ntfs_attr - ntfs in memory attribute structure
287  *
288  * This structure exists only to provide a small structure for the
289  * ntfs_{attr_}iget()/ntfs_test_inode()/ntfs_init_locked_inode() mechanism.
290  *
291  * NOTE: Elements are ordered by size to make the structure as compact as
292  * possible on all architectures.
293  */
294 struct ntfs_attr {
295 	u64 mft_no;
296 	__le16 *name;
297 	u32 name_len;
298 	__le32 type;
299 	unsigned long state;
300 };
301 
302 int ntfs_test_inode(struct inode *vi, void *data);
303 struct inode *ntfs_iget(struct super_block *sb, u64 mft_no);
304 struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,
305 		__le16 *name, u32 name_len);
306 struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name,
307 		u32 name_len);
308 struct inode *ntfs_alloc_big_inode(struct super_block *sb);
309 void ntfs_free_big_inode(struct inode *inode);
310 int ntfs_drop_big_inode(struct inode *inode);
311 void ntfs_evict_big_inode(struct inode *vi);
312 void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni);
313 
314 static inline void ntfs_init_big_inode(struct inode *vi)
315 {
316 	struct ntfs_inode *ni = NTFS_I(vi);
317 
318 	ntfs_debug("Entering.");
319 	__ntfs_init_inode(vi->i_sb, ni);
320 	ni->mft_no = vi->i_ino;
321 }
322 
323 struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, u64 mft_no);
324 void ntfs_clear_extent_inode(struct ntfs_inode *ni);
325 int ntfs_read_inode_mount(struct inode *vi);
326 int ntfs_show_options(struct seq_file *sf, struct dentry *root);
327 int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size);
328 
329 int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
330 		 struct iattr *attr);
331 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
332 		struct kstat *stat, unsigned int request_mask,
333 		unsigned int query_flags);
334 
335 int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni);
336 int __ntfs_write_inode(struct inode *vi, int sync);
337 int ntfs_inode_attach_all_extents(struct ntfs_inode *ni);
338 int ntfs_inode_add_attrlist(struct ntfs_inode *ni);
339 void ntfs_destroy_ext_inode(struct ntfs_inode *ni);
340 int ntfs_inode_free_space(struct ntfs_inode *ni, int size);
341 s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf);
342 s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf,
343 		bool sync);
344 int ntfs_inode_close(struct ntfs_inode *ni);
345 
346 static inline void ntfs_commit_inode(struct inode *vi)
347 {
348 	__ntfs_write_inode(vi, 1);
349 }
350 
351 int ntfs_inode_sync_filename(struct ntfs_inode *ni);
352 int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
353 		const loff_t new_size, bool bsync);
354 void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev);
355 struct folio *ntfs_get_locked_folio(struct address_space *mapping,
356 		pgoff_t index, pgoff_t end_index, struct file_ra_state *ra);
357 
358 #endif /* _LINUX_NTFS_INODE_H */
359