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