xref: /linux/fs/ntfs/ntfs.h (revision 546b928da0427b0d6c663cbb992bd7bfa9ac7971)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Defines for NTFS Linux kernel driver.
4  *
5  * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.
6  * Copyright (C) 2002 Richard Russon
7  * Copyright (c) 2025 LG Electronics Co., Ltd.
8  */
9 
10 #ifndef _LINUX_NTFS_H
11 #define _LINUX_NTFS_H
12 
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/hex.h>
16 #include <linux/module.h>
17 #include <linux/compiler.h>
18 #include <linux/fs.h>
19 #include <linux/nls.h>
20 #include <linux/smp.h>
21 #include <linux/pagemap.h>
22 #include <linux/blk_types.h>
23 #include <linux/uidgid.h>
24 
25 #include "volume.h"
26 #include "layout.h"
27 #include "inode.h"
28 
29 #ifdef pr_fmt
30 #undef pr_fmt
31 #endif
32 
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 
35 /*
36  * Default pre-allocation size is optimize runlist merge overhead
37  * with small chunk size.
38  */
39 #define NTFS_DEF_PREALLOC_SIZE		65536
40 
41 /*
42  * The log2 of the standard number of clusters per compression block.
43  * A value of 4 corresponds to 16 clusters (1 << 4), which is the
44  * default chunk size used by NTFS LZNT1 compression.
45  */
46 #define STANDARD_COMPRESSION_UNIT	4
47 
48 /*
49  * The maximum cluster size (4KB) allowed for compression to be enabled.
50  * By design, NTFS does not support compression on volumes where the
51  * cluster size exceeds 4096 bytes.
52  */
53 #define MAX_COMPRESSION_CLUSTER_SIZE 4096
54 
55 #define NTFS_B_TO_CLU(vol, b) ((b) >> (vol)->cluster_size_bits)
56 #define NTFS_CLU_TO_B(vol, clu) ((u64)(clu) << (vol)->cluster_size_bits)
57 #define NTFS_B_TO_CLU_OFS(vol, clu) ((u64)(clu) & (vol)->cluster_size_mask)
58 
59 #define NTFS_MFT_NR_TO_CLU(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) >> \
60 					 (vol)->cluster_size_bits)
61 #define NTFS_MFT_NR_TO_PIDX(vol, mft_no) (mft_no >> (PAGE_SHIFT - \
62 					  (vol)->mft_record_size_bits))
63 #define NTFS_MFT_NR_TO_POFS(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) & \
64 					  ~PAGE_MASK)
65 
66 #define NTFS_PIDX_TO_BLK(vol, idx) (((u64)idx << PAGE_SHIFT) >> \
67 				    ((vol)->sb)->s_blocksize_bits)
68 #define NTFS_PIDX_TO_CLU(vol, idx) (((u64)idx << PAGE_SHIFT) >> \
69 				    (vol)->cluster_size_bits)
70 #define NTFS_CLU_TO_PIDX(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) >> \
71 				    PAGE_SHIFT)
72 #define NTFS_CLU_TO_POFS(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) & \
73 				    ~PAGE_MASK)
74 
75 enum {
76 	NTFS_BLOCK_SIZE		= 512,
77 	NTFS_BLOCK_SIZE_BITS	= 9,
78 	NTFS_SB_MAGIC		= 0x5346544e,	/* 'NTFS' */
79 	NTFS_MAX_NAME_LEN	= 255,
80 	NTFS_MAX_LABEL_LEN	= 128,
81 };
82 
83 enum {
84 	CASE_SENSITIVE = 0,
85 	IGNORE_CASE = 1,
86 };
87 
88 /*
89  * Conversion helpers for NTFS units.
90  */
91 
92 /* Convert bytes to cluster count */
ntfs_bytes_to_cluster(const struct ntfs_volume * vol,s64 bytes)93 static inline u64 ntfs_bytes_to_cluster(const struct ntfs_volume *vol,
94 		s64 bytes)
95 {
96 	return bytes >> vol->cluster_size_bits;
97 }
98 
99 /* Convert cluster count to bytes */
ntfs_cluster_to_bytes(const struct ntfs_volume * vol,u64 clusters)100 static inline u64 ntfs_cluster_to_bytes(const struct ntfs_volume *vol,
101 		u64 clusters)
102 {
103 	return clusters << vol->cluster_size_bits;
104 }
105 
106 /* Get the byte offset within a cluster from a linear byte address */
ntfs_bytes_to_cluster_off(const struct ntfs_volume * vol,u64 bytes)107 static inline u64 ntfs_bytes_to_cluster_off(const struct ntfs_volume *vol,
108 		u64 bytes)
109 {
110 	return bytes & vol->cluster_size_mask;
111 }
112 
113 /* Calculate the physical cluster number containing a specific MFT record. */
ntfs_mft_no_to_cluster(const struct ntfs_volume * vol,unsigned long mft_no)114 static inline u64 ntfs_mft_no_to_cluster(const struct ntfs_volume *vol,
115 		unsigned long mft_no)
116 {
117 	return ((u64)mft_no << vol->mft_record_size_bits) >>
118 		vol->cluster_size_bits;
119 }
120 
121 /* Calculate the folio index where the MFT record resides. */
ntfs_mft_no_to_pidx(const struct ntfs_volume * vol,unsigned long mft_no)122 static inline pgoff_t ntfs_mft_no_to_pidx(const struct ntfs_volume *vol,
123 		unsigned long mft_no)
124 {
125 	return mft_no >> (PAGE_SHIFT - vol->mft_record_size_bits);
126 }
127 
128 /* Calculate the byte offset within a folio for an MFT record. */
ntfs_mft_no_to_poff(const struct ntfs_volume * vol,unsigned long mft_no)129 static inline u64 ntfs_mft_no_to_poff(const struct ntfs_volume *vol,
130 		unsigned long mft_no)
131 {
132 	return ((u64)mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
133 }
134 
135 /* Convert folio index to cluster number. */
ntfs_pidx_to_cluster(const struct ntfs_volume * vol,pgoff_t idx)136 static inline u64 ntfs_pidx_to_cluster(const struct ntfs_volume *vol,
137 		pgoff_t idx)
138 {
139 	return ((u64)idx << PAGE_SHIFT) >> vol->cluster_size_bits;
140 }
141 
142 /* Convert cluster number to folio index. */
ntfs_cluster_to_pidx(const struct ntfs_volume * vol,u64 clu)143 static inline pgoff_t ntfs_cluster_to_pidx(const struct ntfs_volume *vol,
144 		u64 clu)
145 {
146 	return (clu << vol->cluster_size_bits) >> PAGE_SHIFT;
147 }
148 
149 /* Get the byte offset within a folio from a cluster number */
ntfs_cluster_to_poff(const struct ntfs_volume * vol,u64 clu)150 static inline u64 ntfs_cluster_to_poff(const struct ntfs_volume *vol,
151 		u64 clu)
152 {
153 	return (clu << vol->cluster_size_bits) & ~PAGE_MASK;
154 }
155 
156 /* Convert a byte offset on the volume to a bio sector number. */
ntfs_bytes_to_bio_sector(u64 bytes)157 static inline sector_t ntfs_bytes_to_bio_sector(u64 bytes)
158 {
159 	return bytes >> SECTOR_SHIFT;
160 }
161 
162 /* Global variables. */
163 
164 /* Slab caches (from super.c). */
165 extern struct kmem_cache *ntfs_name_cache;
166 extern struct kmem_cache *ntfs_inode_cache;
167 extern struct kmem_cache *ntfs_big_inode_cache;
168 extern struct kmem_cache *ntfs_attr_ctx_cache;
169 extern struct kmem_cache *ntfs_index_ctx_cache;
170 
171 /* The various operations structs defined throughout the driver files. */
172 extern const struct address_space_operations ntfs_aops;
173 extern const struct address_space_operations ntfs_mft_aops;
174 
175 extern const struct  file_operations ntfs_file_ops;
176 extern const struct inode_operations ntfs_file_inode_ops;
177 extern const  struct inode_operations ntfs_symlink_inode_operations;
178 extern const struct inode_operations ntfs_special_inode_operations;
179 
180 extern const struct  file_operations ntfs_dir_ops;
181 extern const struct inode_operations ntfs_dir_inode_ops;
182 
183 extern const struct  file_operations ntfs_empty_file_ops;
184 extern const struct inode_operations ntfs_empty_inode_ops;
185 
186 extern const struct export_operations ntfs_export_ops;
187 
188 /*
189  * NTFS_SB - return the ntfs volume given a vfs super block
190  * @sb:		VFS super block
191  *
192  * NTFS_SB() returns the ntfs volume associated with the VFS super block @sb.
193  */
NTFS_SB(struct super_block * sb)194 static inline struct ntfs_volume *NTFS_SB(struct super_block *sb)
195 {
196 	return sb->s_fs_info;
197 }
198 
199 /* Declarations of functions and global variables. */
200 
201 /* From fs/ntfs/compress.c */
202 int ntfs_read_compressed_block(struct folio *folio);
203 #ifdef CONFIG_NTFS_FS_WOF_COMPRESSION
204 int ntfs_read_wof_compressed_block(struct folio *folio);
205 void ntfs_wof_free_workspaces(void);
206 #endif
207 int allocate_compression_buffers(void);
208 void free_compression_buffers(void);
209 int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count,
210 		struct iov_iter *from);
211 
212 /* From fs/ntfs/super.c */
213 #define default_upcase_len 0x10000
214 extern struct mutex ntfs_lock;
215 
216 struct option_t {
217 	int val;
218 	char *str;
219 };
220 extern const struct option_t on_errors_arr[];
221 int ntfs_set_volume_flags(struct ntfs_volume *vol, __le16 flags);
222 int ntfs_clear_volume_flags(struct ntfs_volume *vol, __le16 flags);
223 int ntfs_write_volume_label(struct ntfs_volume *vol, char *label);
224 
225 /* From fs/ntfs/mst.c */
226 int post_read_mst_fixup(struct ntfs_record *b, const u32 size);
227 int pre_write_mst_fixup(struct ntfs_record *b, const u32 size);
228 void post_write_mst_fixup(struct ntfs_record *b);
229 
230 /* From fs/ntfs/unistr.c */
231 bool ntfs_are_names_equal(const __le16 *s1, size_t s1_len,
232 		const __le16 *s2, size_t s2_len,
233 		const u32 ic,
234 		const __le16 *upcase, const u32 upcase_size);
235 int ntfs_collate_names(const __le16 *name1, const u32 name1_len,
236 		const __le16 *name2, const u32 name2_len,
237 		const int err_val, const u32 ic,
238 		const __le16 *upcase, const u32 upcase_len);
239 int ntfs_ucsncmp(const __le16 *s1, const __le16 *s2, size_t n);
240 int ntfs_ucsncasecmp(const __le16 *s1, const __le16 *s2, size_t n,
241 		const __le16 *upcase, const u32 upcase_size);
242 int ntfs_file_compare_values(const struct file_name_attr *file_name_attr1,
243 		const struct file_name_attr *file_name_attr2,
244 		const int err_val, const u32 ic,
245 		const __le16 *upcase, const u32 upcase_len);
246 int ntfs_nlstoucs(const struct ntfs_volume *vol, const char *ins,
247 		const int ins_len, __le16 **outs, int max_name_len);
248 int ntfs_ucstonls(const struct ntfs_volume *vol, const __le16 *ins,
249 		const int ins_len, unsigned char **outs, int outs_len);
250 __le16 *ntfs_ucsndup(const __le16 *s, u32 maxlen);
251 bool ntfs_names_are_equal(const __le16 *s1, size_t s1_len,
252 		const __le16 *s2, size_t s2_len,
253 		const u32 ic,
254 		const __le16 *upcase, const u32 upcase_size);
255 int ntfs_force_shutdown(struct super_block *sb, u32 flags);
256 long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
257 #ifdef CONFIG_COMPAT
258 long ntfs_compat_ioctl(struct file *filp, unsigned int cmd,
259 		unsigned long arg);
260 #endif
261 
262 /* From fs/ntfs/upcase.c */
263 __le16 *generate_default_upcase(void);
264 
ntfs_ffs(int x)265 static inline int ntfs_ffs(int x)
266 {
267 	int r = 1;
268 
269 	if (!x)
270 		return 0;
271 	if (!(x & 0xffff)) {
272 		x >>= 16;
273 		r += 16;
274 	}
275 	if (!(x & 0xff)) {
276 		x >>= 8;
277 		r += 8;
278 	}
279 	if (!(x & 0xf)) {
280 		x >>= 4;
281 		r += 4;
282 	}
283 	if (!(x & 3)) {
284 		x >>= 2;
285 		r += 2;
286 	}
287 	if (!(x & 1))
288 		r += 1;
289 	return r;
290 }
291 
292 /* From fs/ntfs/bdev-io.c */
293 int ntfs_bdev_read(struct block_device *bdev, char *data, loff_t start, size_t size);
294 int ntfs_bdev_write(struct super_block *sb, void *buf, loff_t start, size_t size);
295 
296 #endif /* _LINUX_NTFS_H */
297