xref: /linux/fs/ntfs/volume.h (revision 3f11c48d4f94bfa58eaff60588f81a5484c34ca3)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Defines for volume structures in NTFS Linux kernel driver.
4  *
5  * Copyright (c) 2001-2006 Anton Altaparmakov
6  * Copyright (c) 2002 Richard Russon
7  * Copyright (c) 2025 LG Electronics Co., Ltd.
8  */
9 
10 #ifndef _LINUX_NTFS_VOLUME_H
11 #define _LINUX_NTFS_VOLUME_H
12 
13 #include <linux/rwsem.h>
14 #include <linux/sched.h>
15 #include <linux/wait.h>
16 #include <linux/uidgid.h>
17 #include <linux/workqueue.h>
18 #include <linux/errseq.h>
19 
20 #include "layout.h"
21 
22 #define NTFS_VOL_UID	BIT(1)
23 #define NTFS_VOL_GID	BIT(2)
24 
25 /*
26  * The NTFS in memory super block structure.
27  *
28  * @sb: Pointer back to the super_block.
29  * @nr_blocks: Number of sb->s_blocksize bytes sized blocks on the device.
30  * @flags: Miscellaneous flags, see below.
31  * @uid: uid that files will be mounted as.
32  * @gid: gid that files will be mounted as.
33  * @fmask: The mask for file permissions.
34  * @dmask: The mask for directory permissions.
35  * @mft_zone_multiplier: Initial mft zone multiplier.
36  * @on_errors: What to do on filesystem errors.
37  * @wb_err: Writeback error tracking.
38  * @sector_size: in bytes
39  * @sector_size_bits: log2(sector_size)
40  * @cluster_size: in bytes
41  * @cluster_size_mask: cluster_size - 1
42  * @cluster_size_bits: log2(cluster_size)
43  * @mft_record_size: in bytes
44  * @mft_record_size_mask: mft_record_size - 1
45  * @mft_record_size_bits: log2(mft_record_size)
46  * @mft_write_supported: Whether the MFT write paths support the geometry.
47  * @index_record_size: in bytes
48  * @index_record_size_mask: index_record_size - 1
49  * @index_record_size_bits: log2(index_record_size)
50  * @nr_clusters: Volume size in clusters == number of bits in lcn bitmap.
51  * @mft_lcn: Cluster location of mft data.
52  * @mftmirr_lcn: Cluster location of copy of mft.
53  * @serial_no: The volume serial number.
54  * @upcase_len: Number of entries in upcase[].
55  * @upcase: The upcase table.
56  * @attrdef_size: Size of the attribute definition table in bytes.
57  * @attrdef: Table of attribute definitions. Obtained from FILE_AttrDef.
58  * @mft_data_pos: Mft record number at which to allocate the next mft record.
59  * @mft_record_reserve_pos: First record in the in-memory MFT metadata reserve
60  *                         (protected by mftbmp_lock).
61  * @mft_record_reserve_end: First record beyond the MFT metadata reserve
62  *                         (protected by mftbmp_lock).
63  * @mft_zone_start: First cluster of the mft zone.
64  * @mft_zone_end: First cluster beyond the mft zone.
65  * @mft_zone_pos: Current position in the mft zone.
66  * @data1_zone_pos: Current position in the first data zone.
67  * @data2_zone_pos: Current position in the second data zone.
68  * @mft_ino: The VFS inode of $MFT.
69  * @mftbmp_ino: Attribute inode for $MFT/$BITMAP.
70  * @mftbmp_lock: Lock for serializing accesses to the mft record bitmap.
71  * @mftmirr_ino: The VFS inode of $MFTMirr.
72  * @mftmirr_size: Size of mft mirror in mft records.
73  * @logfile_ino: The VFS inode of LogFile.
74  * @lcnbmp_ino: The VFS inode of $Bitmap.
75  * @lcnbmp_lock: Lock for serializing accesses to the cluster bitmap
76  * @vol_ino: The VFS inode of $Volume.
77  * @vol_flags: Volume flags.
78  * @major_ver: Ntfs major version of volume.
79  * @minor_ver: Ntfs minor version of volume.
80  * @volume_label_lock: protects @volume_label.
81  * @volume_label: volume label.
82  * @root_ino: The VFS inode of the root directory.
83  * @secure_ino: The VFS inode of $Secure (NTFS3.0+ only, otherwise NULL).
84  * @extend_ino: The VFS inode of $Extend (NTFS3.0+ only, otherwise NULL).
85  * @nls_map: NLS (National Language Support) table.
86  * @nls_utf8: NLS table for UTF-8.
87  * @free_waitq: Wait queue for threads waiting for free clusters or MFT records.
88  * @free_clusters: Track the number of free clusters.
89  * @bad_clusters: Number of clusters recorded in $BadClus:$Bad.
90  * @free_mft_records: Track the free mft records.
91  * @dirty_clusters: Number of clusters that are dirty.
92  * @sparse_compression_unit: Size of compression/sparse unit in clusters.
93  * @lcn_empty_bits_per_page: Number of empty bits per page in the LCN bitmap.
94  * @precalc_work: Work structure for background pre-calculation tasks.
95  * @preallocated_size: reallocation size (in bytes).
96  */
97 struct ntfs_volume {
98 	struct super_block *sb;
99 	s64 nr_blocks;
100 	unsigned long flags;
101 	kuid_t uid;
102 	kgid_t gid;
103 	umode_t fmask;
104 	umode_t dmask;
105 	u8 mft_zone_multiplier;
106 	u8 on_errors;
107 	errseq_t wb_err;
108 	u16 sector_size;
109 	u8 sector_size_bits;
110 	u32 cluster_size;
111 	u32 cluster_size_mask;
112 	u8 cluster_size_bits;
113 	u32 mft_record_size;
114 	u32 mft_record_size_mask;
115 	u8 mft_record_size_bits;
116 	bool mft_write_supported;
117 	/*
118 	 * Unit size used for MFT I/O. This is the MFT record size when
119 	 * it is at least as large as the device logical block, or the
120 	 * containing device logical block when the record is smaller.
121 	 */
122 	u32 mft_io_unit_size;
123 	u32 index_record_size;
124 	u32 index_record_size_mask;
125 	u8 index_record_size_bits;
126 	s64 nr_clusters;
127 	s64 bad_clusters;
128 	s64 mft_lcn;
129 	s64 mftmirr_lcn;
130 	u64 serial_no;
131 	u32 upcase_len;
132 	__le16 *upcase;
133 	s32 attrdef_size;
134 	struct attr_def *attrdef;
135 	s64 mft_data_pos;
136 	s64 mft_record_reserve_pos;
137 	s64 mft_record_reserve_end;
138 	s64 mft_zone_start;
139 	s64 mft_zone_end;
140 	s64 mft_zone_pos;
141 	s64 data1_zone_pos;
142 	s64 data2_zone_pos;
143 	struct inode *mft_ino;
144 	struct inode *mftbmp_ino;
145 	struct rw_semaphore mftbmp_lock;
146 	struct inode *mftmirr_ino;
147 	int mftmirr_size;
148 	struct inode *logfile_ino;
149 	struct inode *lcnbmp_ino;
150 	struct rw_semaphore lcnbmp_lock;
151 	struct mutex volume_label_lock;
152 	struct inode *vol_ino;
153 	__le16 vol_flags;
154 	u8 major_ver;
155 	u8 minor_ver;
156 	unsigned char *volume_label;
157 	struct inode *root_ino;
158 	struct inode *secure_ino;
159 	struct inode *extend_ino;
160 	struct nls_table *nls_map;
161 	bool nls_utf8;
162 	wait_queue_head_t free_waitq;
163 	atomic64_t free_clusters;
164 	atomic64_t free_mft_records;
165 	atomic64_t dirty_clusters;
166 	u8 sparse_compression_unit;
167 	unsigned int *lcn_empty_bits_per_page;
168 	struct work_struct precalc_work;
169 	loff_t preallocated_size;
170 };
171 
172 /*
173  * Defined bits for the flags field in the ntfs_volume structure.
174  *
175  * NV_Errors			Volume has errors, prevent remount rw.
176  * NV_ShowSystemFiles		Return system files in ntfs_readdir().
177  * NV_CaseSensitive		Treat file names as case sensitive and
178  *				create filenames in the POSIX namespace.
179  *				Otherwise be case insensitive but still
180  *				create file names in POSIX namespace.
181  * NV_LogFileEmpty		LogFile journal is empty.
182  * NV_UsnJrnlStamped		UsnJrnl has been stamped.
183  * NV_ReadOnly			Volume is mounted read-only.
184  * NV_Compression		Volume supports compression.
185  * NV_FreeClusterKnown		Free cluster count is known and up-to-date.
186  * NV_Shutdown			Volume is in shutdown state
187  * NV_SysImmutable		Protect system files from deletion.
188  * NV_ShowHiddenFiles		Return hidden files in ntfs_readdir().
189  * NV_HideDotFiles		Hide names beginning with a dot (".").
190  * NV_CheckWindowsNames		Refuse creation/rename of files with
191  *				Windows-reserved names (CON, AUX, NUL, COM1,
192  *				LPT1, etc.) or invalid characters.
193  *
194  * NV_Hibernated		Windows is hibernated on the volume; the sync
195  *				paths must not write the volume flags.
196  * NV_Discard			Issue discard/TRIM commands for freed clusters.
197  * NV_DisableSparse		Disable creation of sparse regions.
198  * NV_NativeSymlinkRel		Translate absolute Windows reparse targets (native_symlink=rel).
199  * NV_MftBootstrap		Mount is still assembling $MFT's own runlist.
200  * NV_StreamsWindows		Interpret the final path component as file:stream.
201  */
202 enum {
203 	NV_Errors,
204 	NV_ShowSystemFiles,
205 	NV_CaseSensitive,
206 	NV_LogFileEmpty,
207 	NV_UsnJrnlStamped,
208 	NV_ReadOnly,
209 	NV_Compression,
210 	NV_FreeClusterKnown,
211 	NV_Shutdown,
212 	NV_SysImmutable,
213 	NV_ShowHiddenFiles,
214 	NV_HideDotFiles,
215 	NV_CheckWindowsNames,
216 	NV_Hibernated,
217 	NV_Discard,
218 	NV_DisableSparse,
219 	NV_NativeSymlinkRel,
220 	NV_SymlinkNative,
221 	NV_MftBootstrap,
222 	NV_StreamsWindows,
223 };
224 
225 /*
226  * Macro tricks to expand the NVolFoo(), NVolSetFoo(), and NVolClearFoo()
227  * functions.
228  */
229 #define DEFINE_NVOL_BIT_OPS(flag)					\
230 static inline int NVol##flag(struct ntfs_volume *vol)		\
231 {								\
232 	return test_bit(NV_##flag, &(vol)->flags);		\
233 }								\
234 static inline void NVolSet##flag(struct ntfs_volume *vol)	\
235 {								\
236 	set_bit(NV_##flag, &(vol)->flags);			\
237 }								\
238 static inline void NVolClear##flag(struct ntfs_volume *vol)	\
239 {								\
240 	clear_bit(NV_##flag, &(vol)->flags);			\
241 }
242 
243 /* Emit the ntfs volume bitops functions. */
244 DEFINE_NVOL_BIT_OPS(Errors)
245 DEFINE_NVOL_BIT_OPS(ShowSystemFiles)
246 DEFINE_NVOL_BIT_OPS(CaseSensitive)
247 DEFINE_NVOL_BIT_OPS(LogFileEmpty)
248 DEFINE_NVOL_BIT_OPS(UsnJrnlStamped)
249 DEFINE_NVOL_BIT_OPS(ReadOnly)
250 DEFINE_NVOL_BIT_OPS(Compression)
251 DEFINE_NVOL_BIT_OPS(FreeClusterKnown)
252 DEFINE_NVOL_BIT_OPS(Shutdown)
253 DEFINE_NVOL_BIT_OPS(SysImmutable)
254 DEFINE_NVOL_BIT_OPS(ShowHiddenFiles)
255 DEFINE_NVOL_BIT_OPS(HideDotFiles)
256 DEFINE_NVOL_BIT_OPS(CheckWindowsNames)
257 DEFINE_NVOL_BIT_OPS(Hibernated)
258 DEFINE_NVOL_BIT_OPS(Discard)
259 DEFINE_NVOL_BIT_OPS(DisableSparse)
260 DEFINE_NVOL_BIT_OPS(NativeSymlinkRel)
261 DEFINE_NVOL_BIT_OPS(SymlinkNative)
262 DEFINE_NVOL_BIT_OPS(MftBootstrap)
263 DEFINE_NVOL_BIT_OPS(StreamsWindows)
264 
265 static inline void ntfs_inc_free_clusters(struct ntfs_volume *vol, s64 nr)
266 {
267 	if (!NVolFreeClusterKnown(vol))
268 		wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
269 	atomic64_add(nr, &vol->free_clusters);
270 }
271 
272 static inline void ntfs_dec_free_clusters(struct ntfs_volume *vol, s64 nr)
273 {
274 	if (!NVolFreeClusterKnown(vol))
275 		wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
276 	atomic64_sub(nr, &vol->free_clusters);
277 }
278 
279 static inline void ntfs_inc_free_mft_records(struct ntfs_volume *vol, s64 nr)
280 {
281 	atomic64_add(nr, &vol->free_mft_records);
282 }
283 
284 static inline void ntfs_dec_free_mft_records(struct ntfs_volume *vol, s64 nr)
285 {
286 	atomic64_sub(nr, &vol->free_mft_records);
287 }
288 
289 static inline void ntfs_set_lcn_empty_bits(struct ntfs_volume *vol, unsigned long index,
290 		u8 val, unsigned int count)
291 {
292 	if (!NVolFreeClusterKnown(vol))
293 		wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
294 
295 	if (val)
296 		vol->lcn_empty_bits_per_page[index] -= count;
297 	else
298 		vol->lcn_empty_bits_per_page[index] += count;
299 }
300 
301 static __always_inline void ntfs_hold_dirty_clusters(struct ntfs_volume *vol, s64 nr_clusters)
302 {
303 	atomic64_add(nr_clusters, &vol->dirty_clusters);
304 }
305 
306 static __always_inline void ntfs_release_dirty_clusters(struct ntfs_volume *vol, s64 nr_clusters)
307 {
308 	if (atomic64_read(&vol->dirty_clusters) < nr_clusters)
309 		atomic64_set(&vol->dirty_clusters, 0);
310 	else
311 		atomic64_sub(nr_clusters, &vol->dirty_clusters);
312 }
313 
314 s64 ntfs_available_clusters_count(struct ntfs_volume *vol, s64 nr_clusters);
315 s64 get_nr_free_clusters(struct ntfs_volume *vol);
316 #endif /* _LINUX_NTFS_VOLUME_H */
317