xref: /linux/fs/ntfs/inode.h (revision ea04295b4f1f4c40b955e85f432ddde72f659ee3)
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  * @linuxflags: Linux-specific file attributes loaded from the $LINUXFLAGS EA.
99  */
100 struct ntfs_inode {
101 	rwlock_t size_lock;
102 	unsigned long state;
103 	__le32 flags;
104 	u64 mft_no;
105 	u16 seq_no;
106 	atomic_t count;
107 	struct ntfs_volume *vol;
108 	__le32 type;
109 	__le16 *name;
110 	u32 name_len;
111 	atomic_t stream_open_count;
112 	struct runlist runlist;
113 	s64 data_size;
114 	s64 initialized_size;
115 	s64 allocated_size;
116 	struct timespec64 i_crtime;
117 	void *mrec;
118 	struct mutex mrec_lock;
119 	struct folio *folio;
120 	int folio_ofs;
121 	s64 mft_lcn[2];
122 	unsigned int mft_lcn_count;
123 	u32 attr_list_size;
124 	u8 *attr_list;
125 	union {
126 		struct {
127 			u32 block_size;
128 			u32 vcn_size;
129 			__le32 collation_rule;
130 			u8 block_size_bits;
131 			u8 vcn_size_bits;
132 		} index;
133 		struct {
134 			s64 size;
135 			u32 block_size;
136 			u8 block_size_bits;
137 			u8 block_clusters;
138 		} compressed;
139 	} itype;
140 	struct mutex extent_lock;
141 	s32 nr_extents;
142 	union {
143 		struct ntfs_inode **extent_ntfs_inos;
144 		struct ntfs_inode *base_ntfs_ino;
145 	} ext;
146 	unsigned int i_dealloc_clusters;
147 	__le32 reparse_tag;
148 	__le32 reparse_flags;
149 	char *target;
150 	u32 linuxflags;
151 };
152 
153 /*
154  * Defined bits for the state field in the ntfs_inode structure.
155  * (f) = files only, (d) = directories only, (a) = attributes/fake inodes only
156  *
157  * NI_Dirty			Mft record needs to be written to disk.
158  * NI_AttrListDirty		Mft record contains an attribute list.
159  * NI_AttrList			Mft record contains an attribute list.
160  * NI_AttrListNonResident	Attribute list is non-resident. Implies
161  *				NI_AttrList is set.
162  * NI_Attr			1: Fake inode for attribute i/o.
163  *				0: Real inode or extent inode.
164  * NI_MstProtected		Attribute is protected by MST fixups.
165  * NI_NonResident		Unnamed data attr is non-resident (f)
166  *				Attribute is non-resident (a).
167  * NI_IndexAllocPresent		$I30 index alloc attr is present (d).
168  * NI_Compressed		Unnamed data attr is compressed (f).
169  *				Create compressed files by default (d).
170  *				Attribute is compressed (a).
171  * NI_Encrypted			Unnamed data attr is encrypted (f).
172  *				Create encrypted files by default (d).
173  *				Attribute is encrypted (a).
174  * NI_Sparse			Unnamed data attr is sparse (f).
175  *				Create sparse files by default (d).
176  *				Attribute is sparse (a).
177  * NI_SparseDisabled		May not create sparse regions.
178  * NI_FullyMapped		Runlist is fully mapped.
179  * NI_FileNameDirty		FILE_NAME attributes need to be updated.
180  * NI_BeingDeleted		ntfs inode is being delated.
181  * NI_BeingCreated		ntfs inode is being created.
182  * NI_HasEA			ntfs inode has EA attribute.
183  * NI_RunlistDirty		runlist need to be updated.
184  * NI_StreamUnlinked		Named stream is unlinked but still open.
185  */
186 enum {
187 	NI_Dirty,
188 	NI_AttrListDirty,
189 	NI_AttrList,
190 	NI_AttrListNonResident,
191 	NI_Attr,
192 	NI_MstProtected,
193 	NI_NonResident,
194 	NI_IndexAllocPresent,
195 	NI_Compressed,
196 	NI_WofCompressed,
197 	NI_Encrypted,
198 	NI_Sparse,
199 	NI_SparseDisabled,
200 	NI_FullyMapped,
201 	NI_FileNameDirty,
202 	NI_BeingDeleted,
203 	NI_BeingCreated,
204 	NI_HasEA,
205 	NI_RunlistDirty,
206 	NI_StreamUnlinked,
207 };
208 
209 /*
210  * NOTE: We should be adding dirty mft records to a list somewhere and they
211  * should be independent of the (ntfs/vfs) inode structure so that an inode can
212  * be removed but the record can be left dirty for syncing later.
213  */
214 
215 /*
216  * Macro tricks to expand the NInoFoo(), NInoSetFoo(), and NInoClearFoo()
217  * functions.
218  */
219 #define NINO_FNS(flag)						\
220 static inline int NIno##flag(struct ntfs_inode *ni)		\
221 {								\
222 	return test_bit(NI_##flag, &(ni)->state);		\
223 }								\
224 static inline void NInoSet##flag(struct ntfs_inode *ni)		\
225 {								\
226 	set_bit(NI_##flag, &(ni)->state);			\
227 }								\
228 static inline void NInoClear##flag(struct ntfs_inode *ni)	\
229 {								\
230 	clear_bit(NI_##flag, &(ni)->state);			\
231 }
232 
233 /*
234  * As above for NInoTestSetFoo() and NInoTestClearFoo().
235  */
236 #define TAS_NINO_FNS(flag)						\
237 static inline int NInoTestSet##flag(struct ntfs_inode *ni)		\
238 {									\
239 	return test_and_set_bit(NI_##flag, &(ni)->state);		\
240 }									\
241 static inline int NInoTestClear##flag(struct ntfs_inode *ni)		\
242 {									\
243 	return test_and_clear_bit(NI_##flag, &(ni)->state);		\
244 }
245 
246 /* Emit the ntfs inode bitops functions. */
247 NINO_FNS(Dirty)
248 TAS_NINO_FNS(Dirty)
249 NINO_FNS(AttrList)
250 NINO_FNS(AttrListDirty)
251 NINO_FNS(AttrListNonResident)
252 NINO_FNS(Attr)
253 NINO_FNS(MstProtected)
254 NINO_FNS(NonResident)
255 NINO_FNS(IndexAllocPresent)
256 NINO_FNS(Compressed)
257 NINO_FNS(WofCompressed)
258 NINO_FNS(Encrypted)
259 NINO_FNS(Sparse)
260 NINO_FNS(SparseDisabled)
261 NINO_FNS(FullyMapped)
262 NINO_FNS(FileNameDirty)
263 TAS_NINO_FNS(FileNameDirty)
264 NINO_FNS(BeingDeleted)
265 NINO_FNS(HasEA)
266 NINO_FNS(RunlistDirty)
267 NINO_FNS(StreamUnlinked)
268 
269 /*
270  * The full structure containing a ntfs_inode and a vfs struct inode. Used for
271  * all real and fake inodes but not for extent inodes which lack the vfs struct
272  * inode.
273  */
274 struct big_ntfs_inode {
275 	struct ntfs_inode ntfs_inode;
276 	struct inode vfs_inode;		/* The vfs inode structure. */
277 };
278 
279 /*
280  * NTFS_I - return the ntfs inode given a vfs inode
281  * @inode:	VFS inode
282  *
283  * NTFS_I() returns the ntfs inode associated with the VFS @inode.
284  */
285 static inline struct ntfs_inode *NTFS_I(struct inode *inode)
286 {
287 	return &container_of(inode, struct big_ntfs_inode, vfs_inode)->ntfs_inode;
288 }
289 
290 static inline struct inode *VFS_I(struct ntfs_inode *ni)
291 {
292 	return &container_of(ni, struct big_ntfs_inode, ntfs_inode)->vfs_inode;
293 }
294 
295 /* Return true when @ni represents a named $DATA attribute inode. */
296 static inline bool ntfs_inode_is_named_stream(struct ntfs_inode *ni)
297 {
298 	return NInoAttr(ni) && ni->type == AT_DATA && ni->name_len;
299 }
300 
301 /* Return the base MFT inode for an attribute inode, or @ni itself. */
302 static inline struct ntfs_inode *ntfs_base_inode(struct ntfs_inode *ni)
303 {
304 	if (NInoAttr(ni) && ni->nr_extents == -1 && ni->ext.base_ntfs_ino)
305 		return ni->ext.base_ntfs_ino;
306 	return ni;
307 }
308 
309 /*
310  * ntfs_attr - ntfs in memory attribute structure
311  *
312  * This structure exists only to provide a small structure for the
313  * ntfs_{attr_}iget()/ntfs_test_inode()/ntfs_init_locked_inode() mechanism.
314  *
315  * NOTE: Elements are ordered by size to make the structure as compact as
316  * possible on all architectures.
317  */
318 struct ntfs_attr {
319 	u64 mft_no;
320 	__le16 *name;
321 	u32 name_len;
322 	__le32 type;
323 	unsigned long state;
324 };
325 
326 int ntfs_test_inode(struct inode *vi, void *data);
327 int ntfs_test_inode_rcu(struct inode *vi, void *data);
328 struct inode *ntfs_iget(struct super_block *sb, u64 mft_no);
329 struct inode *ntfs_attr_ilookup(struct inode *base_vi, __le32 type,
330 		__le16 *name, u32 name_len);
331 struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type,
332 		__le16 *name, u32 name_len);
333 struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name,
334 		u32 name_len);
335 struct inode *ntfs_alloc_big_inode(struct super_block *sb);
336 void ntfs_free_big_inode(struct inode *inode);
337 int ntfs_drop_big_inode(struct inode *inode);
338 void ntfs_evict_big_inode(struct inode *vi);
339 void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni);
340 
341 static inline void ntfs_init_big_inode(struct inode *vi)
342 {
343 	struct ntfs_inode *ni = NTFS_I(vi);
344 
345 	ntfs_debug("Entering.");
346 	__ntfs_init_inode(vi->i_sb, ni);
347 	ni->mft_no = vi->i_ino;
348 }
349 
350 struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, u64 mft_no);
351 void ntfs_clear_extent_inode(struct ntfs_inode *ni);
352 int ntfs_read_inode_mount(struct inode *vi);
353 int ntfs_show_options(struct seq_file *sf, struct dentry *root);
354 int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size);
355 
356 int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
357 		 struct iattr *attr);
358 int ntfs_getattr(const struct mnt_idmap *idmap, const struct path *path,
359 		struct kstat *stat, unsigned int request_mask,
360 		unsigned int query_flags);
361 
362 int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni);
363 int __ntfs_write_inode(struct inode *vi, int sync);
364 int ntfs_inode_attach_all_extents(struct ntfs_inode *ni);
365 int ntfs_inode_add_attrlist(struct ntfs_inode *ni);
366 int ntfs_inode_free_empty_extents(struct ntfs_inode *ni);
367 void ntfs_destroy_ext_inode(struct ntfs_inode *ni);
368 int ntfs_inode_free_space(struct ntfs_inode *ni, int size);
369 s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf);
370 s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf,
371 		bool sync);
372 int ntfs_inode_close(struct ntfs_inode *ni);
373 
374 static inline void ntfs_commit_inode(struct inode *vi)
375 {
376 	__ntfs_write_inode(vi, 1);
377 }
378 
379 int ntfs_inode_sync_filename(struct ntfs_inode *ni);
380 int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
381 		const loff_t new_size);
382 void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev);
383 struct folio *ntfs_get_locked_folio(struct address_space *mapping,
384 		pgoff_t index, pgoff_t end_index, struct file_ra_state *ra);
385 
386 #endif /* _LINUX_NTFS_INODE_H */
387