xref: /linux/fs/ntfs/runlist.h (revision cdd4dc3aebeab43a72ce0bc2b5bab6f0a80b97a5)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Defines for runlist handling in NTFS Linux kernel driver.
4  *
5  * Copyright (c) 2001-2005 Anton Altaparmakov
6  * Copyright (c) 2002 Richard Russon
7  * Copyright (c) 2025 LG Electronics Co., Ltd.
8  */
9 
10 #ifndef _LINUX_NTFS_RUNLIST_H
11 #define _LINUX_NTFS_RUNLIST_H
12 
13 #include "volume.h"
14 
15 /*
16  * runlist_element - in memory vcn to lcn mapping array element
17  * @vcn:	starting vcn of the current array element
18  * @lcn:	starting lcn of the current array element
19  * @length:	length in clusters of the current array element
20  *
21  * The last vcn (in fact the last vcn + 1) is reached when length == 0.
22  *
23  * When lcn == -1 this means that the count vcns starting at vcn are not
24  * physically allocated (i.e. this is a hole / data is sparse).
25  *
26  * In memory vcn to lcn mapping structure element.
27  * @vcn: vcn = Starting virtual cluster number.
28  * @lcn: lcn = Starting logical cluster number.
29  * @length: Run length in clusters.
30  */
31 struct runlist_element {
32 	s64 vcn;
33 	s64 lcn;
34 	s64 length;
35 };
36 
37 /*
38  * runlist - in memory vcn to lcn mapping array including a read/write lock
39  * @rl:		pointer to an array of runlist elements
40  * @lock:	read/write spinlock for serializing access to @rl
41  * @rl_hint:	hint/cache pointing to the last accessed runlist element
42  */
43 struct runlist {
44 	struct runlist_element *rl;
45 	struct rw_semaphore lock;
46 	size_t count;
47 	int rl_hint;
48 };
49 
50 static inline void ntfs_init_runlist(struct runlist *rl)
51 {
52 	rl->rl = NULL;
53 	init_rwsem(&rl->lock);
54 	rl->count = 0;
55 	rl->rl_hint = -1;
56 }
57 
58 enum {
59 	LCN_DELALLOC		= -1,
60 	LCN_HOLE		= -2,
61 	LCN_RL_NOT_MAPPED	= -3,
62 	LCN_ENOENT		= -4,
63 	LCN_ENOMEM		= -5,
64 	LCN_EIO			= -6,
65 	LCN_EINVAL		= -7,
66 };
67 
68 struct runlist_element *ntfs_runlists_merge(struct runlist *d_runlist,
69 		struct runlist_element *srl, size_t s_rl_count,
70 		size_t *new_rl_count);
71 struct runlist_element *ntfs_mapping_pairs_decompress(const struct ntfs_volume *vol,
72 		const struct attr_record *attr, struct runlist *old_runlist,
73 		size_t *new_rl_count);
74 s64 ntfs_rl_vcn_to_lcn(const struct runlist_element *rl, const s64 vcn);
75 struct runlist_element *ntfs_rl_find_vcn_nolock(struct runlist_element *rl, const s64 vcn);
76 int ntfs_get_size_for_mapping_pairs(const struct ntfs_volume *vol,
77 		const struct runlist_element *rl, const s64 first_vcn,
78 		const s64 last_vcn, int max_mp_size);
79 int ntfs_mapping_pairs_build(const struct ntfs_volume *vol, s8 *dst,
80 		const int dst_len, const struct runlist_element *rl,
81 		const s64 first_vcn, const s64 last_vcn, s64 *const stop_vcn,
82 		struct runlist_element **stop_rl, unsigned int *de_cluster_count);
83 int ntfs_rl_truncate_nolock(const struct ntfs_volume *vol,
84 		struct runlist *const runlist, const s64 new_length);
85 int ntfs_rl_sparse(struct runlist_element *rl);
86 s64 ntfs_rl_get_compressed_size(struct ntfs_volume *vol, struct runlist_element *rl);
87 struct runlist_element *ntfs_rl_insert_range(struct runlist_element *dst_rl, int dst_cnt,
88 		struct runlist_element *src_rl, int src_cnt, size_t *new_cnt);
89 struct runlist_element *ntfs_rl_punch_hole(struct runlist_element *dst_rl, int dst_cnt,
90 		s64 start_vcn, s64 len, struct runlist_element **punch_rl,
91 		size_t *new_rl_cnt);
92 struct runlist_element *ntfs_rl_collapse_range(struct runlist_element *dst_rl, int dst_cnt,
93 		s64 start_vcn, s64 len, struct runlist_element **punch_rl,
94 		size_t *new_rl_cnt);
95 struct runlist_element *ntfs_rl_realloc(struct runlist_element *rl, int old_size,
96 		int new_size);
97 #endif /* _LINUX_NTFS_RUNLIST_H */
98