xref: /linux/fs/ntfs3/ntfs_fs.h (revision 9e8fc16dc6b474b9a87843c3d3ee12fc83f2bdcc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  */
7 
8 // clang-format off
9 #ifndef _LINUX_NTFS3_NTFS_FS_H
10 #define _LINUX_NTFS3_NTFS_FS_H
11 
12 #include <linux/blkdev.h>
13 #include <linux/buffer_head.h>
14 #include <linux/fs.h>
15 #include <linux/iomap.h>
16 #include <linux/highmem.h>
17 #include <linux/kernel.h>
18 #include <linux/hex.h>
19 #include <linux/mm.h>
20 #include <linux/mutex.h>
21 #include <linux/page-flags.h>
22 #include <linux/pagemap.h>
23 #include <linux/rbtree.h>
24 #include <linux/rwsem.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/time64.h>
28 #include <linux/types.h>
29 #include <linux/uidgid.h>
30 #include <asm/div64.h>
31 #include <asm/page.h>
32 
33 #include "debug.h"
34 #include "ntfs.h"
35 
36 struct dentry;
37 struct fiemap_extent_info;
38 struct user_namespace;
39 struct page;
40 struct writeback_control;
41 enum utf16_endian;
42 
43 
44 #define MINUS_ONE_T			((size_t)(-1))
45 /* Biggest MFT / smallest cluster */
46 #define MAXIMUM_BYTES_PER_MFT		4096
47 #define MAXIMUM_SHIFT_BYTES_PER_MFT	12
48 #define NTFS_BLOCKS_PER_MFT_RECORD	(MAXIMUM_BYTES_PER_MFT / 512)
49 
50 #define MAXIMUM_BYTES_PER_INDEX		4096
51 #define MAXIMUM_SHIFT_BYTES_PER_INDEX	12
52 #define NTFS_BLOCKS_PER_INODE		(MAXIMUM_BYTES_PER_INDEX / 512)
53 
54 /* NTFS specific error code when fixup failed. */
55 #define E_NTFS_FIXUP			555
56 /* NTFS specific error code about resident->nonresident. */
57 #define E_NTFS_NONRESIDENT		556
58 /* NTFS specific error code about punch hole. */
59 #define E_NTFS_NOTALIGNED		557
60 /* NTFS specific error code when on-disk struct is corrupted. */
61 #define E_NTFS_CORRUPT			558
62 
63 
64 /* sbi->flags */
65 #define NTFS_FLAGS_NODISCARD		0x00000001
66 /* ntfs in shutdown state. */
67 #define NTFS_FLAGS_SHUTDOWN_BIT		0x00000002  /* == 4*/
68 /* Set when LogFile is replaying. */
69 #define NTFS_FLAGS_LOG_REPLAYING	0x00000008
70 /* Set when we changed first MFT's which copy must be updated in $MftMirr. */
71 #define NTFS_FLAGS_MFTMIRR		0x00001000
72 #define NTFS_FLAGS_NEED_REPLAY		0x04000000
73 
74 
75 /* ni->ni_flags */
76 /*
77  * Data attribute is external compressed (LZX/Xpress)
78  * 1 - WOF_COMPRESSION_XPRESS4K
79  * 2 - WOF_COMPRESSION_XPRESS8K
80  * 3 - WOF_COMPRESSION_XPRESS16K
81  * 4 - WOF_COMPRESSION_LZX32K
82  */
83 #define NI_FLAG_COMPRESSED_MASK		0x0000000f
84 /* Data attribute is deduplicated. */
85 #define NI_FLAG_DEDUPLICATED		0x00000010
86 #define NI_FLAG_EA			0x00000020
87 #define NI_FLAG_DIR			0x00000040
88 #define NI_FLAG_RESIDENT		0x00000080
89 #define NI_FLAG_UPDATE_PARENT		0x00000100
90 // clang-format on
91 
92 struct ntfs_mount_options {
93 	char *nls_name;
94 	struct nls_table *nls;
95 	/* ->d_hash() and ->d_compare() read the options in rcu pathwalk */
96 	struct rcu_head rcu;
97 
98 	kuid_t fs_uid;
99 	kgid_t fs_gid;
100 	u16 fs_fmask_inv;
101 	u16 fs_dmask_inv;
102 
103 	unsigned fmask : 1; /* fmask was set. */
104 	unsigned dmask : 1; /*dmask was set. */
105 	unsigned sys_immutable : 1; /* Immutable system files. */
106 	unsigned discard : 1; /* Issue discard requests on deletions. */
107 	unsigned sparse : 1; /* Create sparse files. */
108 	unsigned showmeta : 1; /* Show meta files. */
109 	unsigned nohidden : 1; /* Do not show hidden files. */
110 	unsigned hide_dot_files : 1; /* Set hidden flag on dot files. */
111 	unsigned windows_names : 1; /* Disallow names forbidden by Windows. */
112 	unsigned force : 1; /* RW mount dirty volume. */
113 	unsigned prealloc : 1; /* Preallocate space when file is growing. */
114 	unsigned nocase : 1; /* case insensitive. */
115 	unsigned delalloc : 1; /* delay allocation. */
116 	unsigned ads : 1; /* ads support. */
117 	unsigned ugm : 1; /* store user/group/mode in xattr. */
118 };
119 
120 /* Special value to unpack and deallocate. */
121 #define RUN_DEALLOCATE ((struct runs_tree *)(size_t)1)
122 
123 /* TODO: Use rb tree instead of array. */
124 struct runs_tree {
125 	struct ntfs_run *runs;
126 	size_t count; /* Currently used size a ntfs_run storage. */
127 	size_t allocated; /* Currently allocated ntfs_run storage size. */
128 };
129 
130 struct ntfs_buffers {
131 	/* Biggest MFT / smallest cluster = 4096 / 512 = 8 */
132 	/* Biggest index / smallest cluster = 4096 / 512 = 8 */
133 	struct buffer_head *bh[PAGE_SIZE >> SECTOR_SHIFT];
134 	u32 bytes;
135 	u32 nbufs;
136 	u32 off;
137 };
138 
139 enum ALLOCATE_OPT {
140 	ALLOCATE_DEF = 0, // Allocate all clusters.
141 	ALLOCATE_MFT = 1, // Allocate for MFT.
142 	ALLOCATE_ZERO = 2, // Zeroout new allocated clusters.
143 	ALLOCATE_ONE_FR = 4, // Allocate one fragment only.
144 };
145 
146 enum bitmap_mutex_classes {
147 	BITMAP_MUTEX_CLUSTERS = 0,
148 	BITMAP_MUTEX_MFT = 1,
149 };
150 
151 struct wnd_bitmap {
152 	struct super_block *sb;
153 	struct rw_semaphore rw_lock;
154 
155 	struct runs_tree run;
156 	size_t nbits;
157 
158 	size_t total_zeroes; // Total number of free bits.
159 	u16 *free_bits; // Free bits in each window.
160 	size_t nwnd;
161 	u32 bits_last; // Bits in last window.
162 
163 	struct rb_root start_tree; // Extents, sorted by 'start'.
164 	struct rb_root count_tree; // Extents, sorted by 'count + start'.
165 	size_t count; // Extents count.
166 
167 	/*
168 	 * -1 Tree is activated but not updated (too many fragments).
169 	 * 0 - Tree is not activated.
170 	 * 1 - Tree is activated and updated.
171 	 */
172 	int uptodated;
173 	size_t extent_min; // Minimal extent used while building.
174 	size_t extent_max; // Upper estimate of biggest free block.
175 
176 	/* Zone [bit, end) */
177 	size_t zone_bit;
178 	size_t zone_end;
179 
180 	bool inited;
181 };
182 
183 typedef int (*NTFS_CMP_FUNC)(const void *key1, size_t len1, const void *key2,
184 			     size_t len2, const void *param);
185 
186 enum index_mutex_classed {
187 	INDEX_MUTEX_I30 = 0,
188 	INDEX_MUTEX_SII = 1,
189 	INDEX_MUTEX_SDH = 2,
190 	INDEX_MUTEX_SO = 3,
191 	INDEX_MUTEX_SQ = 4,
192 	INDEX_MUTEX_SR = 5,
193 	INDEX_MUTEX_TOTAL
194 };
195 
196 /* ntfs_index - Allocation unit inside directory. */
197 struct ntfs_index {
198 	struct runs_tree bitmap_run;
199 	struct runs_tree alloc_run;
200 	/* read/write access to 'bitmap_run'/'alloc_run' while ntfs_readdir */
201 	struct rw_semaphore run_lock;
202 	size_t version; /* increment each change */
203 
204 	u8 index_bits; // log2(root->index_block_size)
205 	u8 idx2vbn_bits; // log2(root->index_block_clst)
206 	u8 vbn2vbo_bits; // index_block_size < cluster? 9 : cluster_bits
207 	u8 type; // index_mutex_classed
208 };
209 
210 /* Minimum MFT zone. */
211 #define NTFS_MIN_MFT_ZONE 100
212 /* Step to increase the MFT. */
213 #define NTFS_MFT_INCREASE_STEP 1024
214 
215 /* Ntfs file system in-core superblock data. */
216 struct ntfs_sb_info {
217 	struct super_block *sb;
218 	struct rcu_head rcu;
219 
220 	u32 discard_granularity;
221 	u64 discard_granularity_mask_inv; // ~(discard_granularity_mask_inv-1)
222 	u32 bdev_blocksize; // bdev_logical_block_size(bdev)
223 
224 	u32 cluster_size; // bytes per cluster
225 	u32 cluster_mask; // == cluster_size - 1
226 	u64 cluster_mask_inv; // ~(cluster_size - 1)
227 	u32 block_mask; // sb->s_blocksize - 1
228 	u32 blocks_per_cluster; // cluster_size / sb->s_blocksize
229 
230 	u32 record_size;
231 	u32 index_size;
232 
233 	u8 cluster_bits;
234 	u8 record_bits;
235 
236 	u64 maxbytes; // Maximum size for normal files.
237 	u64 maxbytes_sparse; // Maximum size for sparse file.
238 
239 	unsigned long flags; // See NTFS_FLAGS_
240 
241 	CLST zone_max; // Maximum MFT zone length in clusters
242 	CLST bad_clusters; // The count of marked bad clusters.
243 
244 	u16 max_bytes_per_attr; // Maximum attribute size in record.
245 	u16 attr_size_tr; // Attribute size threshold (320 bytes).
246 
247 	/* Records in $Extend. */
248 	CLST objid_no;
249 	CLST quota_no;
250 	CLST reparse_no;
251 	CLST usn_jrnl_no;
252 
253 	struct ATTR_DEF_ENTRY *def_table; // Attribute definition table.
254 	u32 def_entries;
255 	u32 ea_max_size;
256 
257 	struct MFT_REC *new_rec;
258 
259 	u16 *upcase;
260 
261 	struct {
262 		u64 lbo, lbo2;
263 		struct ntfs_inode *ni;
264 		struct wnd_bitmap bitmap; // $MFT::Bitmap
265 		/*
266 		 * MFT records [11-24) used to expand MFT itself.
267 		 * They always marked as used in $MFT::Bitmap
268 		 * 'reserved_bitmap' contains real bitmap of these records.
269 		 */
270 		ulong reserved_bitmap; // Bitmap of used records [11 - 24)
271 		size_t next_free; // The next record to allocate from
272 		size_t used; // MFT valid size in records.
273 		u32 recs_mirr; // Number of records in MFTMirr
274 		u8 next_reserved;
275 		u8 reserved_bitmap_inited;
276 	} mft;
277 
278 	struct {
279 		struct wnd_bitmap bitmap; // $Bitmap::Data
280 		CLST next_free_lcn;
281 		/* Total sum of delay allocated clusters in all files. */
282 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
283 		atomic64_t da;
284 #else
285 		atomic_t da;
286 #endif
287 	} used;
288 
289 	struct {
290 		u64 size; // In bytes.
291 		u64 blocks; // In blocks.
292 		u64 ser_num;
293 		struct ntfs_inode *ni;
294 		__le16 flags; // Cached current VOLUME_INFO::flags, VOLUME_FLAG_DIRTY.
295 		u8 major_ver;
296 		u8 minor_ver;
297 		char label[FSLABEL_MAX];
298 		bool real_dirty; // Real fs state.
299 	} volume;
300 
301 	struct {
302 		struct ntfs_index index_sii;
303 		struct ntfs_index index_sdh;
304 		struct ntfs_inode *ni;
305 		u32 next_id;
306 		u64 next_off;
307 		__le32 def_security_id;
308 	} security;
309 
310 	struct {
311 		struct ntfs_index index_r;
312 		struct ntfs_inode *ni;
313 		u64 max_size; // 16K
314 	} reparse;
315 
316 	struct {
317 		struct ntfs_index index_o;
318 		struct ntfs_inode *ni;
319 	} objid;
320 
321 	struct {
322 		struct mutex mtx_lznt;
323 		struct lznt *lznt;
324 #ifdef CONFIG_NTFS3_LZX_XPRESS
325 		struct mutex mtx_xpress;
326 		struct xpress_decompressor *xpress;
327 		struct mutex mtx_lzx;
328 		struct lzx_decompressor *lzx;
329 #endif
330 	} compress;
331 
332 	struct ntfs_mount_options *options;
333 	struct ratelimit_state msg_ratelimit;
334 	struct proc_dir_entry *procdir;
335 };
336 
337 /* One MFT record(usually 1024 bytes), consists of attributes. */
338 struct mft_inode {
339 	struct rb_node node;
340 	struct ntfs_sb_info *sbi;
341 
342 	struct MFT_REC *mrec;
343 	struct ntfs_buffers nb;
344 
345 	CLST rno;
346 	bool dirty;
347 };
348 
349 /* Nested class for ntfs_inode::ni_lock. */
350 enum ntfs_inode_mutex_lock_class {
351 	NTFS_INODE_MUTEX_DIRTY = 1,
352 	NTFS_INODE_MUTEX_SECURITY,
353 	NTFS_INODE_MUTEX_OBJID,
354 	NTFS_INODE_MUTEX_REPARSE,
355 	NTFS_INODE_MUTEX_NORMAL,
356 	NTFS_INODE_MUTEX_PARENT,
357 	NTFS_INODE_MUTEX_PARENT2,
358 };
359 
360 /*
361  * struct ntfs_inode
362  *
363  * Ntfs inode - extends linux inode. consists of one or more MFT inodes.
364  */
365 struct ntfs_inode {
366 	struct mft_inode mi; // base record
367 
368 	/*
369 	 * Valid size: [0 - i_valid) - these range in file contains valid data.
370 	 * Range [i_valid - inode->i_size) - contains 0.
371 	 * Usually i_valid <= inode->i_size.
372 	 */
373 	u64 i_valid;
374 	struct timespec64 i_crtime;
375 
376 	struct mutex ni_lock;
377 
378 	/* File attributes from std. */
379 	enum FILE_ATTRIBUTE std_fa;
380 	__le32 std_security_id;
381 
382 	/*
383 	 * Tree of mft_inode.
384 	 * Not empty when primary MFT record (usually 1024 bytes) can't save all attributes
385 	 * e.g. file becomes too fragmented or contains a lot of names.
386 	 */
387 	struct rb_root mi_tree;
388 
389 	/*
390 	 * This member is used in ntfs_readdir to ensure that all subrecords are loaded
391 	 */
392 	u8 mi_loaded;
393 
394 	/*
395 	 * Use this field to avoid any write(s).
396 	 * If inode is bad during initialization - use make_bad_inode
397 	 * If inode is bad during operations - use this field
398 	 */
399 	u8 ni_bad;
400 
401 	/* Keep track of FS_NODUMP_FL. */
402 	u8 nodump;
403 
404 	union {
405 		struct ntfs_index dir;
406 		struct {
407 			struct rw_semaphore run_lock;
408 			/* Unpacked runs from just one record. */
409 			struct runs_tree run;
410 			/*
411 			 * Pairs [vcn, len] for all delay allocated clusters.
412 			 * Normal file always contains delayed clusters in one fragment.
413 			 * TODO: use 2 CLST per pair instead of 3.
414 			 */
415 			struct runs_tree run_da;
416 #ifdef CONFIG_NTFS3_LZX_XPRESS
417 			struct folio *offs_folio;
418 #endif
419 			/* Alternative data stream */
420 			struct {
421 				__le16 *name;
422 				u8 len;
423 			} ads;
424 		} file;
425 	};
426 
427 	struct {
428 		struct runs_tree run;
429 		struct ATTR_LIST_ENTRY *le; // 1K aligned memory.
430 		size_t size;
431 		bool dirty;
432 	} attr_list;
433 
434 	size_t ni_flags; // NI_FLAG_XXX
435 	struct ntfs_inode *base; /* ADS: points to base inode. Other: this. */
436 
437 	struct inode vfs_inode;
438 };
439 
440 struct indx_node {
441 	struct ntfs_buffers nb;
442 	struct INDEX_BUFFER *index;
443 };
444 
445 struct ntfs_fnd {
446 	int level;
447 	struct indx_node *nodes[20];
448 	struct NTFS_DE *de[20];
449 	struct NTFS_DE *root_de;
450 };
451 
452 enum REPARSE_SIGN {
453 	REPARSE_NONE = 0,
454 	REPARSE_COMPRESSED = 1,
455 	REPARSE_DEDUPLICATED = 2,
456 	REPARSE_LINK = 3
457 };
458 
459 static inline bool is_ni_base(const struct ntfs_inode *ni)
460 {
461 	return ni == ni->base;
462 }
463 
464 /* Functions from attrib.c */
465 int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
466 			   struct runs_tree *run_da, CLST vcn, CLST lcn,
467 			   CLST len, CLST *pre_alloc, enum ALLOCATE_OPT opt,
468 			   CLST *alen, const size_t fr, CLST *new_lcn,
469 			   CLST *new_len);
470 int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
471 			  struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
472 			  struct runs_tree *run, struct ATTRIB **ins_attr,
473 			  struct page *page);
474 int attr_set_size_ex(struct ntfs_inode *ni, enum ATTR_TYPE type,
475 		     const __le16 *name, u8 name_len, struct runs_tree *run,
476 		     u64 new_size, const u64 *new_valid, bool keep_prealloc,
477 		     struct ATTRIB **ret, bool no_da);
478 static inline int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
479 				const __le16 *name, u8 name_len,
480 				struct runs_tree *run, u64 new_size,
481 				const u64 *new_valid, bool keep_prealloc)
482 {
483 	return attr_set_size_ex(ni, type, name, name_len, run, new_size,
484 				new_valid, keep_prealloc, NULL, false);
485 }
486 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
487 			CLST *len, bool *new, bool zero, void **res,
488 			bool no_da);
489 int attr_data_get_block_locked(struct ntfs_inode *ni, CLST vcn, CLST clen,
490 			       CLST *lcn, CLST *len, bool *new, bool zero,
491 			       void **res, bool no_da);
492 int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio);
493 int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
494 		       const __le16 *name, u8 name_len, struct runs_tree *run,
495 		       CLST vcn);
496 int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
497 			 const __le16 *name, u8 name_len, struct runs_tree *run,
498 			 u64 from, u64 to);
499 int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
500 			struct runs_tree *run, u64 frame, u64 frames,
501 			u8 frame_bits, u32 *ondisk_size, u64 *vbo_data);
502 int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
503 			     CLST frame, CLST *clst_data,
504 			     struct runs_tree *run);
505 int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
506 			u64 new_valid);
507 int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes);
508 int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes);
509 int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size);
510 int attr_force_nonresident(struct ntfs_inode *ni);
511 
512 /* Functions from attrlist.c */
513 void al_destroy(struct ntfs_inode *ni);
514 bool al_verify(struct ntfs_inode *ni);
515 int ntfs_load_attr_list(struct ntfs_inode *ni, struct ATTRIB *attr);
516 struct ATTR_LIST_ENTRY *al_enumerate(struct ntfs_inode *ni,
517 				     struct ATTR_LIST_ENTRY *le);
518 struct ATTR_LIST_ENTRY *al_find_le(struct ntfs_inode *ni,
519 				   struct ATTR_LIST_ENTRY *le,
520 				   const struct ATTRIB *attr);
521 struct ATTR_LIST_ENTRY *al_find_ex(struct ntfs_inode *ni,
522 				   struct ATTR_LIST_ENTRY *le,
523 				   enum ATTR_TYPE type, const __le16 *name,
524 				   u8 name_len, const CLST *vcn);
525 int al_add_le(struct ntfs_inode *ni, enum ATTR_TYPE type, const __le16 *name,
526 	      u8 name_len, CLST svcn, __le16 id, const struct MFT_REF *ref,
527 	      struct ATTR_LIST_ENTRY **new_le);
528 bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le);
529 int al_update(struct ntfs_inode *ni, int sync);
530 static inline size_t al_aligned(size_t size)
531 {
532 	return size_add(size, 1023) & ~(size_t)1023;
533 }
534 
535 /* Globals from bitfunc.c */
536 bool are_bits_clear(const void *map, size_t bit, size_t nbits);
537 bool are_bits_set(const void *map, size_t bit, size_t nbits);
538 size_t get_set_bits_ex(const void *map, size_t bit, size_t nbits);
539 
540 /* Globals from dir.c */
541 int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const __le16 *name, u32 len,
542 		      u8 *buf, int buf_len);
543 int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
544 		      struct cpu_str *uni, u32 max_ulen,
545 		      enum utf16_endian endian);
546 struct inode *dir_search_flags(struct inode *dir, const struct cpu_str *uni,
547 			       struct ntfs_fnd *fnd, u32 flags);
548 static inline struct inode *dir_search(struct inode *dir,
549 				       const struct cpu_str *uni)
550 {
551 	return dir_search_flags(dir, uni, NULL, 0);
552 }
553 
554 bool dir_is_empty(struct inode *dir);
555 extern const struct file_operations ntfs_dir_operations;
556 
557 /* Globals from file.c */
558 int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
559 int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
560 		      struct file_kattr *fa);
561 int ntfs_getattr(const struct mnt_idmap *idmap, const struct path *path,
562 		 struct kstat *stat, u32 request_mask, u32 flags);
563 int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
564 		 struct iattr *attr);
565 int ntfs_file_open(struct inode *inode, struct file *file);
566 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
567 		__u64 start, __u64 len);
568 int ntfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync);
569 long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg);
570 long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg);
571 extern const struct inode_operations ntfs_special_inode_operations;
572 extern const struct inode_operations ntfs_file_inode_operations;
573 extern const struct file_operations ntfs_file_operations;
574 
575 /* Globals from frecord.c */
576 void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi);
577 struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni);
578 struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni);
579 void ni_clear(struct ntfs_inode *ni);
580 int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi);
581 int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
582 	       struct mft_inode **mi);
583 struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
584 			    struct ATTR_LIST_ENTRY **entry_o,
585 			    enum ATTR_TYPE type, const __le16 *name,
586 			    u8 name_len, const CLST *vcn,
587 			    struct mft_inode **mi);
588 struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
589 			       struct ATTR_LIST_ENTRY **le,
590 			       struct mft_inode **mi);
591 int ni_load_all_mi(struct ntfs_inode *ni);
592 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi);
593 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
594 		   const __le16 *name, u8 name_len, bool base_only,
595 		   const __le16 *id);
596 int ni_create_attr_list(struct ntfs_inode *ni);
597 int ni_expand_list(struct ntfs_inode *ni);
598 int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
599 			  const __le16 *name, u8 name_len,
600 			  const struct runs_tree *run, CLST svcn, CLST len,
601 			  __le16 flags, struct ATTRIB **new_attr,
602 			  struct mft_inode **mi, struct ATTR_LIST_ENTRY **le);
603 int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
604 		       enum ATTR_TYPE type, const __le16 *name, u8 name_len,
605 		       struct ATTRIB **new_attr, struct mft_inode **mi,
606 		       struct ATTR_LIST_ENTRY **le);
607 void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
608 		       struct mft_inode *mi, struct ATTR_LIST_ENTRY *le);
609 int ni_delete_all(struct ntfs_inode *ni);
610 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
611 				     const struct le_str *uni,
612 				     const struct MFT_REF *home,
613 				     struct mft_inode **mi,
614 				     struct ATTR_LIST_ENTRY **entry);
615 struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
616 				     struct mft_inode **mi,
617 				     struct ATTR_LIST_ENTRY **entry);
618 int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa);
619 enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
620 				   struct REPARSE_DATA_BUFFER *buffer);
621 int ni_write_inode(struct inode *inode, int sync, const char *hint);
622 #define _ni_write_inode(i, w) ni_write_inode(i, w, __func__)
623 int ni_read_folio_cmpr(struct ntfs_inode *ni, struct folio *folio);
624 int ni_decompress_file(struct ntfs_inode *ni);
625 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
626 		  u32 pages_per_frame, int copy);
627 int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
628 		   u32 pages_per_frame);
629 int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
630 		   struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step);
631 
632 bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
633 			 struct NTFS_DE *de, struct NTFS_DE *de2,
634 			 int undo_step);
635 
636 int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
637 		struct NTFS_DE *de);
638 
639 int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
640 	      struct ntfs_inode *ni, struct NTFS_DE *de,
641 	      struct NTFS_DE *new_de);
642 
643 bool ni_is_dirty(struct inode *inode);
644 loff_t ni_seek_data_or_hole(struct ntfs_inode *ni, loff_t offset, bool data);
645 int ni_write_parents(struct ntfs_inode *ni, int sync);
646 int ni_allocate_da_blocks(struct ntfs_inode *ni);
647 int ni_allocate_da_blocks_locked(struct ntfs_inode *ni);
648 ssize_t ni_query_ads(struct ntfs_inode *ni, loff_t *pos, struct iov_iter *iter);
649 
650 /* Globals from fslog.c */
651 bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes);
652 int log_replay(struct ntfs_inode *ni, bool *initialized);
653 
654 /* Globals from fsntfs.c */
655 struct buffer_head *ntfs_bread(struct super_block *sb, sector_t block);
656 bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes);
657 int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes,
658 		       bool simple);
659 int ntfs_extend_init(struct ntfs_sb_info *sbi);
660 int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi);
661 int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
662 			     CLST *new_lcn, CLST *new_len,
663 			     enum ALLOCATE_OPT opt);
664 bool ntfs_check_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen,
665 			   bool da);
666 int ntfs_look_free_mft(struct ntfs_sb_info *sbi, CLST *rno, bool mft,
667 		       struct ntfs_inode *ni, struct mft_inode **mi);
668 void ntfs_mark_rec_free(struct ntfs_sb_info *sbi, CLST rno, bool is_mft);
669 int ntfs_clear_mft_tail(struct ntfs_sb_info *sbi, size_t from, size_t to);
670 int ntfs_refresh_zone(struct ntfs_sb_info *sbi);
671 void ntfs_update_mftmirr(struct ntfs_sb_info *sbi);
672 void ntfs_bad_inode(struct inode *inode, const char *hint);
673 #define _ntfs_bad_inode(i) ntfs_bad_inode(i, __func__)
674 enum NTFS_DIRTY_FLAGS {
675 	NTFS_DIRTY_CLEAR = 0,
676 	NTFS_DIRTY_DIRTY = 1,
677 	NTFS_DIRTY_ERROR = 2,
678 };
679 int ntfs_set_state(struct ntfs_sb_info *sbi, enum NTFS_DIRTY_FLAGS dirty);
680 int ntfs_sb_write(struct super_block *sb, u64 lbo, size_t bytes,
681 		  const void *buffer, int wait);
682 int ntfs_sb_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
683 		      u64 vbo, const void *buf, size_t bytes, int sync);
684 struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
685 				   const struct runs_tree *run, u64 vbo);
686 int ntfs_read_run_nb_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run,
687 			u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb,
688 			struct file_ra_state *ra);
689 static inline int ntfs_read_run_nb(struct ntfs_sb_info *sbi,
690 				   const struct runs_tree *run, u64 vbo,
691 				   void *buf, u32 bytes,
692 				   struct ntfs_buffers *nb)
693 {
694 	return ntfs_read_run_nb_ra(sbi, run, vbo, buf, bytes, nb, NULL);
695 }
696 int ntfs_read_bh_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run,
697 		    u64 vbo, struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
698 		    struct ntfs_buffers *nb, struct file_ra_state *ra);
699 static inline int ntfs_read_bh(struct ntfs_sb_info *sbi,
700 			       const struct runs_tree *run, u64 vbo,
701 			       struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
702 			       struct ntfs_buffers *nb)
703 {
704 	return ntfs_read_bh_ra(sbi, run, vbo, rhdr, bytes, nb, NULL);
705 }
706 int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
707 		u32 bytes, struct ntfs_buffers *nb);
708 int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
709 		  struct ntfs_buffers *nb, int sync);
710 int ntfs_read_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
711 			void *buf, u64 vbo, size_t bytes, int wr);
712 static inline int ntfs_read_run(struct ntfs_sb_info *sbi,
713 				const struct runs_tree *run, void *buf, u64 vbo,
714 				size_t bytes)
715 {
716 	return ntfs_read_write_run(sbi, run, buf, vbo, bytes, 0);
717 }
718 static inline int ntfs_write_run(struct ntfs_sb_info *sbi,
719 				 const struct runs_tree *run, void *buf,
720 				 u64 vbo, size_t bytes)
721 {
722 	return ntfs_read_write_run(sbi, run, buf, vbo, bytes, 1);
723 }
724 
725 int ntfs_bio_fill_1(struct ntfs_sb_info *sbi, const struct runs_tree *run);
726 int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
727 		    u64 vbo, u64 *lbo, u64 *bytes);
728 struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST nRec,
729 				  enum RECORD_FLAG flag);
730 extern const u8 s_default_security[0x50];
731 bool is_sd_valid(const struct SECURITY_DESCRIPTOR_RELATIVE *sd, u32 len);
732 int ntfs_security_init(struct ntfs_sb_info *sbi);
733 int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
734 			    struct SECURITY_DESCRIPTOR_RELATIVE **sd,
735 			    size_t *size);
736 int ntfs_insert_security(struct ntfs_sb_info *sbi,
737 			 const struct SECURITY_DESCRIPTOR_RELATIVE *sd,
738 			 u32 size, __le32 *security_id, bool *inserted);
739 int ntfs_reparse_init(struct ntfs_sb_info *sbi);
740 int ntfs_objid_init(struct ntfs_sb_info *sbi);
741 int ntfs_objid_remove(struct ntfs_sb_info *sbi, struct GUID *guid);
742 int ntfs_insert_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
743 			const struct MFT_REF *ref);
744 int ntfs_remove_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
745 			const struct MFT_REF *ref);
746 void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len, bool trim);
747 int run_deallocate(struct ntfs_sb_info *sbi, const struct runs_tree *run,
748 		   bool trim);
749 bool valid_windows_name(struct ntfs_sb_info *sbi, const struct le_str *name);
750 int ntfs_set_label(struct ntfs_sb_info *sbi, u8 *label, int len);
751 
752 /* Globals from index.c */
753 int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit);
754 void fnd_clear(struct ntfs_fnd *fnd);
755 static inline struct ntfs_fnd *fnd_get(void)
756 {
757 	return kzalloc_obj(struct ntfs_fnd, GFP_NOFS);
758 }
759 static inline void fnd_put(struct ntfs_fnd *fnd)
760 {
761 	if (fnd) {
762 		fnd_clear(fnd);
763 		kfree(fnd);
764 	}
765 }
766 void indx_clear(struct ntfs_index *idx);
767 int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
768 	      const struct ATTRIB *attr, enum index_mutex_classed type);
769 struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
770 				 struct ATTRIB **attr, struct mft_inode **mi);
771 int indx_read_ra(struct ntfs_index *idx, struct ntfs_inode *ni, CLST vbn,
772 		 struct indx_node **node, struct file_ra_state *ra);
773 static inline int indx_read(struct ntfs_index *idx, struct ntfs_inode *ni,
774 			    CLST vbn, struct indx_node **node)
775 {
776 	return indx_read_ra(idx, ni, vbn, node, NULL);
777 }
778 int indx_find(struct ntfs_index *indx, struct ntfs_inode *dir,
779 	      const struct INDEX_ROOT *root, const void *Key, size_t KeyLen,
780 	      const void *param, int *diff, struct NTFS_DE **entry,
781 	      struct ntfs_fnd *fnd);
782 int indx_find_sort(struct ntfs_index *indx, struct ntfs_inode *ni,
783 		   const struct INDEX_ROOT *root, struct NTFS_DE **entry,
784 		   struct ntfs_fnd *fnd);
785 int indx_find_raw(struct ntfs_index *indx, struct ntfs_inode *ni,
786 		  const struct INDEX_ROOT *root, struct NTFS_DE **entry,
787 		  size_t *off, struct ntfs_fnd *fnd);
788 int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
789 		      const struct NTFS_DE *new_de, const void *param,
790 		      struct ntfs_fnd *fnd, bool undo);
791 int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
792 		      const void *key, u32 key_len, const void *param);
793 int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
794 		    const struct ATTR_FILE_NAME *fname,
795 		    const struct NTFS_DUP_INFO *dup, int sync);
796 
797 /* Globals from inode.c */
798 struct inode *ntfs_iget5_flags(struct super_block *sb,
799 			       const struct MFT_REF *ref,
800 			       const struct cpu_str *name, u32 flags);
801 static inline struct inode *ntfs_iget5(struct super_block *sb,
802 				       const struct MFT_REF *ref,
803 				       const struct cpu_str *name)
804 {
805 	return ntfs_iget5_flags(sb, ref, name, 0);
806 }
807 int ntfs_set_size(struct inode *inode, u64 new_size);
808 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc);
809 int ntfs_sync_inode(struct inode *inode);
810 int inode_read_data(struct inode *inode, void *data, size_t bytes);
811 int ntfs_create_inode(const struct mnt_idmap *idmap, struct inode *dir,
812 		      struct dentry *dentry, const struct cpu_str *uni,
813 		      umode_t mode, dev_t dev, const char *symname, u32 size,
814 		      struct ntfs_fnd *fnd);
815 int ntfs_link_inode(struct inode *inode, struct dentry *dentry);
816 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry);
817 void ntfs_evict_inode(struct inode *inode);
818 DECLARE_IOMAP_ITER_NEXT(ntfs_iomap_next);
819 extern const struct iomap_write_ops ntfs_iomap_folio_ops;
820 extern const struct inode_operations ntfs_link_inode_operations;
821 extern const struct address_space_operations ntfs_aops;
822 extern const struct address_space_operations ntfs_aops_cmpr;
823 
824 /* Globals from name_i.c */
825 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
826 		 const struct cpu_str *uni);
827 struct dentry *ntfs3_get_parent(struct dentry *child);
828 
829 extern const struct inode_operations ntfs_dir_inode_operations;
830 extern const struct inode_operations ntfs_special_inode_operations;
831 extern const struct dentry_operations ntfs_dentry_ops;
832 
833 /* Globals from record.c */
834 int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi);
835 void mi_put(struct mft_inode *mi);
836 int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno);
837 int mi_read(struct mft_inode *mi, bool is_mft);
838 struct ATTRIB *mi_enum_attr(struct ntfs_inode *ni, struct mft_inode *mi,
839 			    struct ATTRIB *attr);
840 struct ATTRIB *mi_find_attr(struct ntfs_inode *ni, struct mft_inode *mi,
841 			    struct ATTRIB *attr, enum ATTR_TYPE type,
842 			    const __le16 *name, u8 name_len, const __le16 *id);
843 static inline struct ATTRIB *rec_find_attr_le(struct ntfs_inode *ni,
844 					      struct mft_inode *rec,
845 					      struct ATTR_LIST_ENTRY *le)
846 {
847 	return mi_find_attr(ni, rec, NULL, le->type, le_name(le), le->name_len,
848 			    &le->id);
849 }
850 int mi_write(struct mft_inode *mi, int wait);
851 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
852 		  __le16 flags, bool is_mft);
853 struct ATTRIB *mi_insert_attr(struct ntfs_inode *ni, struct mft_inode *mi,
854 			      enum ATTR_TYPE type, const __le16 *name,
855 			      u8 name_len, u32 asize, u16 name_off);
856 
857 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
858 		    struct ATTRIB *attr);
859 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes);
860 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
861 		 const struct runs_tree *run, CLST len);
862 static inline bool mi_is_ref(const struct mft_inode *mi,
863 			     const struct MFT_REF *ref)
864 {
865 	if (le32_to_cpu(ref->low) != mi->rno)
866 		return false;
867 	if (ref->seq != mi->mrec->seq)
868 		return false;
869 
870 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
871 	return le16_to_cpu(ref->high) == (mi->rno >> 32);
872 #else
873 	return !ref->high;
874 #endif
875 }
876 
877 static inline void mi_get_ref(const struct mft_inode *mi, struct MFT_REF *ref)
878 {
879 	ref->low = cpu_to_le32(mi->rno);
880 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
881 	ref->high = cpu_to_le16(mi->rno >> 32);
882 #else
883 	ref->high = 0;
884 #endif
885 	ref->seq = mi->mrec->seq;
886 }
887 
888 /* Globals from run.c */
889 bool run_lookup_entry(const struct runs_tree *run, CLST vcn, CLST *lcn,
890 		      CLST *len, size_t *index);
891 bool run_lookup_entry_da(const struct runs_tree *run,
892 			 const struct runs_tree *run_da, CLST vcn, CLST *lcn,
893 			 CLST *len);
894 void run_truncate(struct runs_tree *run, CLST vcn);
895 void run_truncate_head(struct runs_tree *run, CLST vcn);
896 void run_truncate_around(struct runs_tree *run, CLST vcn);
897 bool run_add_entry(struct runs_tree *run, CLST vcn, CLST lcn, CLST len,
898 		   bool is_mft);
899 bool run_collapse_range(struct runs_tree *run, CLST vcn, CLST len, CLST sub);
900 int run_insert_range(struct runs_tree *run, CLST vcn, CLST len);
901 int run_insert_range_da(struct runs_tree *run, CLST vcn, CLST len);
902 bool run_get_entry(const struct runs_tree *run, size_t index, CLST *vcn,
903 		   CLST *lcn, CLST *len);
904 bool run_is_mapped_full(const struct runs_tree *run, CLST svcn, CLST evcn);
905 
906 int run_pack(const struct runs_tree *run, CLST svcn, CLST len, u8 *run_buf,
907 	     u32 run_buf_size, CLST *packed_vcns);
908 int run_unpack(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
909 	       CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
910 	       int run_buf_size);
911 
912 #ifdef NTFS3_CHECK_FREE_CLST
913 int run_unpack_ex(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
914 		  CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
915 		  int run_buf_size);
916 #else
917 #define run_unpack_ex run_unpack
918 #endif
919 int run_get_highest_vcn(CLST vcn, const u8 *run_buf, size_t run_buf_size,
920 			u64 *highest_vcn);
921 int run_clone(const struct runs_tree *run, struct runs_tree *new_run);
922 bool run_remove_range(struct runs_tree *run, CLST vcn, CLST len, CLST *done);
923 CLST run_len(const struct runs_tree *run);
924 CLST run_get_max_vcn(const struct runs_tree *run);
925 
926 /* Globals from super.c */
927 void *ntfs_set_shared(void *ptr, u32 bytes);
928 void *ntfs_put_shared(void *ptr);
929 void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len);
930 int ntfs_discard(struct ntfs_sb_info *sbi, CLST Lcn, CLST Len);
931 
932 /* Globals from bitmap.c*/
933 int __init ntfs3_init_bitmap(void);
934 void ntfs3_exit_bitmap(void);
935 void wnd_close(struct wnd_bitmap *wnd);
936 static inline size_t wnd_zeroes(const struct wnd_bitmap *wnd)
937 {
938 	return wnd->total_zeroes;
939 }
940 int wnd_init(struct wnd_bitmap *wnd, struct super_block *sb, size_t nbits);
941 int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits);
942 int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits);
943 int wnd_set_used_safe(struct wnd_bitmap *wnd, size_t bit, size_t bits,
944 		      size_t *done);
945 bool wnd_is_free(struct wnd_bitmap *wnd, size_t bit, size_t bits);
946 bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits);
947 
948 /* Possible values for 'flags' 'wnd_find'. */
949 #define BITMAP_FIND_MARK_AS_USED 0x01
950 #define BITMAP_FIND_FULL 0x02
951 size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
952 		size_t flags, size_t *allocated);
953 int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits);
954 void wnd_zone_set(struct wnd_bitmap *wnd, size_t Lcn, size_t Len);
955 int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range);
956 
957 void ntfs_bitmap_set_le(void *map, unsigned int start, int len);
958 void ntfs_bitmap_clear_le(void *map, unsigned int start, int len);
959 unsigned int ntfs_bitmap_weight_le(const void *bitmap, int bits);
960 
961 /* Globals from upcase.c */
962 int ntfs_cmp_names(const __le16 *s1, size_t l1, const __le16 *s2, size_t l2,
963 		   const u16 *upcase, bool bothcase);
964 int ntfs_cmp_names_cpu(const struct cpu_str *uni1, const struct le_str *uni2,
965 		       const u16 *upcase, bool bothcase);
966 unsigned long ntfs_names_hash(const u16 *name, size_t len, const u16 *upcase,
967 			      unsigned long hash);
968 
969 /* globals from xattr.c */
970 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
971 struct posix_acl *ntfs_get_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
972 			       int type);
973 int ntfs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
974 		 struct posix_acl *acl, int type);
975 int ntfs_init_acl(const struct mnt_idmap *idmap, struct inode *inode,
976 		  struct inode *dir);
977 #else
978 #define ntfs_get_acl NULL
979 #define ntfs_set_acl NULL
980 #endif
981 
982 int ntfs_acl_chmod(const struct mnt_idmap *idmap, struct dentry *dentry);
983 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
984 extern const struct xattr_handler *const ntfs_xattr_handlers[];
985 
986 int ntfs_save_wsl_perm(struct inode *inode, __le32 *ea_size);
987 void ntfs_get_wsl_perm(struct inode *inode);
988 
989 /* globals from lznt.c */
990 struct lznt *get_lznt_ctx(int level);
991 size_t compress_lznt(const void *uncompressed, size_t uncompressed_size,
992 		     void *compressed, size_t compressed_size,
993 		     struct lznt *ctx);
994 ssize_t decompress_lznt(const void *compressed, size_t compressed_size,
995 			void *uncompressed, size_t uncompressed_size);
996 
997 static inline bool is_ntfs3(struct ntfs_sb_info *sbi)
998 {
999 	return sbi->volume.major_ver >= 3;
1000 }
1001 
1002 /* (sb->s_flags & SB_ACTIVE) */
1003 static inline bool is_mounted(struct ntfs_sb_info *sbi)
1004 {
1005 	return !!sbi->sb->s_root;
1006 }
1007 
1008 static inline bool ntfs_is_meta_file(struct ntfs_sb_info *sbi, CLST rno)
1009 {
1010 	return rno < MFT_REC_FREE || rno == sbi->objid_no ||
1011 	       rno == sbi->quota_no || rno == sbi->reparse_no ||
1012 	       rno == sbi->usn_jrnl_no;
1013 }
1014 
1015 static inline size_t wnd_zone_bit(const struct wnd_bitmap *wnd)
1016 {
1017 	return wnd->zone_bit;
1018 }
1019 
1020 static inline size_t wnd_zone_len(const struct wnd_bitmap *wnd)
1021 {
1022 	return wnd->zone_end - wnd->zone_bit;
1023 }
1024 
1025 static inline void run_init(struct runs_tree *run)
1026 {
1027 	run->runs = NULL;
1028 	run->count = 0;
1029 	run->allocated = 0;
1030 }
1031 
1032 static inline struct runs_tree *run_alloc(void)
1033 {
1034 	return kzalloc_obj(struct runs_tree, GFP_NOFS);
1035 }
1036 
1037 static inline void run_close(struct runs_tree *run)
1038 {
1039 	kvfree(run->runs);
1040 	memset(run, 0, sizeof(*run));
1041 }
1042 
1043 static inline void run_free(struct runs_tree *run)
1044 {
1045 	if (run) {
1046 		kvfree(run->runs);
1047 		kfree(run);
1048 	}
1049 }
1050 
1051 static inline bool run_is_empty(struct runs_tree *run)
1052 {
1053 	return !run->count;
1054 }
1055 
1056 /* NTFS uses quad aligned bitmaps. */
1057 static inline size_t ntfs3_bitmap_size(size_t bits)
1058 {
1059 	return BITS_TO_U64(bits) * sizeof(u64);
1060 }
1061 
1062 #define _100ns2seconds 10000000
1063 #define SecondsToStartOf1970 0x00000002B6109100
1064 
1065 #define NTFS_TIME_GRAN 100
1066 
1067 /*
1068  * kernel2nt - Converts in-memory kernel timestamp into nt time.
1069  */
1070 static inline __le64 kernel2nt(const struct timespec64 *ts)
1071 {
1072 	// 10^7 units of 100 nanoseconds one second
1073 	return cpu_to_le64(_100ns2seconds *
1074 				   (ts->tv_sec + SecondsToStartOf1970) +
1075 			   ts->tv_nsec / NTFS_TIME_GRAN);
1076 }
1077 
1078 /*
1079  * nt2kernel - Converts on-disk nt time into kernel timestamp.
1080  */
1081 static inline void nt2kernel(const __le64 tm, struct timespec64 *ts)
1082 {
1083 	s32 t32;
1084 	/* use signed 64 bit to support timestamps prior to epoch. xfstest 258. */
1085 	s64 t = le64_to_cpu(tm) - _100ns2seconds * SecondsToStartOf1970;
1086 
1087 	ts->tv_sec = div_s64_rem(t, _100ns2seconds, &t32);
1088 	ts->tv_nsec = t32 * 100;
1089 }
1090 
1091 static inline struct ntfs_sb_info *ntfs_sb(struct super_block *sb)
1092 {
1093 	return sb->s_fs_info;
1094 }
1095 
1096 static inline int ntfs3_forced_shutdown(struct super_block *sb)
1097 {
1098 	return test_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags);
1099 }
1100 
1101 /* Returns total sum of delay allocated clusters in all files. */
1102 static inline CLST ntfs_get_da(struct ntfs_sb_info *sbi)
1103 {
1104 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1105 	return atomic64_read(&sbi->used.da);
1106 #else
1107 	return atomic_read(&sbi->used.da);
1108 #endif
1109 }
1110 
1111 /* Update total count of delay allocated clusters. */
1112 static inline void ntfs_add_da(struct ntfs_sb_info *sbi, CLST da)
1113 {
1114 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1115 	atomic64_add(da, &sbi->used.da);
1116 #else
1117 	atomic_add(da, &sbi->used.da);
1118 #endif
1119 }
1120 
1121 /* Update total count of delay allocated clusters. */
1122 static inline void ntfs_sub_da(struct ntfs_sb_info *sbi, CLST da)
1123 {
1124 #ifdef CONFIG_NTFS3_64BIT_CLUSTER
1125 	atomic64_sub(da, &sbi->used.da);
1126 #else
1127 	atomic_sub(da, &sbi->used.da);
1128 #endif
1129 }
1130 
1131 /*
1132  * ntfs_up_cluster - Align up on cluster boundary.
1133  */
1134 static inline u64 ntfs_up_cluster(const struct ntfs_sb_info *sbi, u64 size)
1135 {
1136 	return (size + sbi->cluster_mask) & sbi->cluster_mask_inv;
1137 }
1138 
1139 /*
1140  * ntfs_up_block - Align up on cluster boundary.
1141  */
1142 static inline u64 ntfs_up_block(const struct super_block *sb, u64 size)
1143 {
1144 	return (size + sb->s_blocksize - 1) & ~(u64)(sb->s_blocksize - 1);
1145 }
1146 
1147 static inline CLST bytes_to_cluster(const struct ntfs_sb_info *sbi, u64 size)
1148 {
1149 	return (size + sbi->cluster_mask) >> sbi->cluster_bits;
1150 }
1151 
1152 static inline u64 bytes_to_block(const struct super_block *sb, u64 size)
1153 {
1154 	return (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1155 }
1156 
1157 static inline struct ntfs_inode *ntfs_i(struct inode *inode)
1158 {
1159 	return container_of(inode, struct ntfs_inode, vfs_inode);
1160 }
1161 
1162 static inline bool is_compressed(const struct ntfs_inode *ni)
1163 {
1164 	return (ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) ||
1165 	       (ni->ni_flags & NI_FLAG_COMPRESSED_MASK);
1166 }
1167 
1168 static inline bool is_bad_ni(const struct ntfs_inode *ni)
1169 {
1170 	return ni->ni_bad;
1171 }
1172 
1173 static inline int ni_ext_compress_bits(const struct ntfs_inode *ni)
1174 {
1175 	return 0xb + (ni->ni_flags & NI_FLAG_COMPRESSED_MASK);
1176 }
1177 
1178 /* Bits - 0xc, 0xd, 0xe, 0xf, 0x10 */
1179 static inline void ni_set_ext_compress_bits(struct ntfs_inode *ni, u8 bits)
1180 {
1181 	ni->ni_flags |= (bits - 0xb) & NI_FLAG_COMPRESSED_MASK;
1182 }
1183 
1184 static inline bool is_dedup(const struct ntfs_inode *ni)
1185 {
1186 	return ni->ni_flags & NI_FLAG_DEDUPLICATED;
1187 }
1188 
1189 static inline bool is_encrypted(const struct ntfs_inode *ni)
1190 {
1191 	return ni->std_fa & FILE_ATTRIBUTE_ENCRYPTED;
1192 }
1193 
1194 static inline bool is_sparsed(const struct ntfs_inode *ni)
1195 {
1196 	return ni->std_fa & FILE_ATTRIBUTE_SPARSE_FILE;
1197 }
1198 
1199 static inline int is_resident(struct ntfs_inode *ni)
1200 {
1201 	return ni->ni_flags & NI_FLAG_RESIDENT;
1202 }
1203 
1204 static inline loff_t ntfs_get_maxbytes(struct ntfs_inode *ni)
1205 {
1206 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1207 	return is_sparsed(ni) || is_compressed(ni) ? sbi->maxbytes_sparse :
1208 						     sbi->maxbytes;
1209 }
1210 
1211 static inline void le16_sub_cpu(__le16 *var, u16 val)
1212 {
1213 	*var = cpu_to_le16(le16_to_cpu(*var) - val);
1214 }
1215 
1216 static inline void le32_sub_cpu(__le32 *var, u32 val)
1217 {
1218 	*var = cpu_to_le32(le32_to_cpu(*var) - val);
1219 }
1220 
1221 static inline void nb_put(struct ntfs_buffers *nb)
1222 {
1223 	u32 i, nbufs = nb->nbufs;
1224 
1225 	if (!nbufs)
1226 		return;
1227 
1228 	for (i = 0; i < nbufs; i++)
1229 		put_bh(nb->bh[i]);
1230 	nb->nbufs = 0;
1231 }
1232 
1233 static inline void put_indx_node(struct indx_node *in)
1234 {
1235 	if (!in)
1236 		return;
1237 
1238 	kfree(in->index);
1239 	nb_put(&in->nb);
1240 	kfree(in);
1241 }
1242 
1243 static inline void mi_clear(struct mft_inode *mi)
1244 {
1245 	nb_put(&mi->nb);
1246 	kfree(mi->mrec);
1247 	mi->mrec = NULL;
1248 }
1249 
1250 static inline void ni_lock(struct ntfs_inode *ni)
1251 {
1252 	mutex_lock_nested(&ni->base->ni_lock, NTFS_INODE_MUTEX_NORMAL);
1253 }
1254 
1255 static inline void ni_lock_dir(struct ntfs_inode *ni)
1256 {
1257 	mutex_lock_nested(&ni->base->ni_lock, NTFS_INODE_MUTEX_PARENT);
1258 }
1259 
1260 static inline void ni_lock_dir2(struct ntfs_inode *ni)
1261 {
1262 	mutex_lock_nested(&ni->base->ni_lock, NTFS_INODE_MUTEX_PARENT2);
1263 }
1264 
1265 static inline void ni_unlock(struct ntfs_inode *ni)
1266 {
1267 	mutex_unlock(&ni->base->ni_lock);
1268 }
1269 
1270 static inline int ni_trylock(struct ntfs_inode *ni)
1271 {
1272 	return mutex_trylock(&ni->base->ni_lock);
1273 }
1274 
1275 static inline int attr_load_runs_attr(struct ntfs_inode *ni,
1276 				      struct ATTRIB *attr,
1277 				      struct runs_tree *run, CLST vcn)
1278 {
1279 	return attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1280 				  attr->name_len, run, vcn);
1281 }
1282 
1283 static inline void le64_sub_cpu(__le64 *var, u64 val)
1284 {
1285 	*var = cpu_to_le64(le64_to_cpu(*var) - val);
1286 }
1287 
1288 #endif /* _LINUX_NTFS3_NTFS_FS_H */
1289