Debian Codegraph @

key.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018 Ernesto A. Fernández <[email protected]>
4 */
5
6 #include <linux/crc32c.h>
7 #include "apfs.h"
8 #include "unicode.h"
9
10 /**
11 * apfs_filename_cmp - Normalize and compare two APFS filenames
12 * @sb: filesystem superblock
13 * @name1: first name to compare
14 * @len1: length of @name1
15 * @name2: second name to compare
16 * @len2: length of the @name2
17 *
18 * Returns 0 if @name1 and @name2 are equal, or non-zero otherwise.
19 */
20 int apfs_filename_cmp(struct super_block *sb,
21 const char *name1, unsigned int len1,
22 const char *name2, unsigned int len2)
23 {
24 struct apfs_unicursor cursor1, cursor2;
25 bool case_fold = apfs_is_case_insensitive(sb);
26
27 if (!apfs_is_normalization_insensitive(sb)) {
28 if (len1 != len2)
29 return -1;
30 return memcmp(name1, name2, len1);
31 }
32
33 apfs_init_unicursor(&cursor1, name1, len1);
34 apfs_init_unicursor(&cursor2, name2, len2);
35
36 while (1) {
37 unicode_t uni1, uni2;
38
39 uni1 = apfs_normalize_next(&cursor1, case_fold);
40 uni2 = apfs_normalize_next(&cursor2, case_fold);
41
42 if (uni1 != uni2)
43 return uni1 < uni2 ? -1 : 1;
44 if (!uni1)
45 return 0;
46 }
47 }
48
49 /**
50 * apfs_keycmp - Compare two keys
51 * @k1: first key to compare
52 * @k2: second key to compare
53 *
54 * returns 0 if @k1 and @k2 are equal
55 * < 0 if @k1 comes before @k2 in the btree
56 * > 0 if @k1 comes after @k2 in the btree
57 */
58 int apfs_keycmp(struct apfs_key *k1, struct apfs_key *k2)
59 {
60 if (k1->id != k2->id)
61 return k1->id < k2->id ? -1 : 1;
62 if (k1->type != k2->type)
63 return k1->type < k2->type ? -1 : 1;
64 if (k1->number != k2->number)
65 return k1->number < k2->number ? -1 : 1;
66 if (!k1->name || !k2->name)
67 return 0;
68
69 /* Normalization seems to be ignored here, even for directory records */
70 return strcmp(k1->name, k2->name);
71 }
72
73 /**
74 * apfs_read_cat_key - Parse an on-disk catalog key
75 * @raw: pointer to the raw key
76 * @size: size of the raw key
77 * @key: apfs_key structure to store the result
78 * @hashed: are the directory records hashed?
79 *
80 * Returns 0 on success, or a negative error code otherwise.
81 */
82 int apfs_read_cat_key(void *raw, int size, struct apfs_key *key, bool hashed)
83 {
84 if (size < sizeof(struct apfs_key_header)) {
85 apfs_err(NULL, "bad key length (%d)", size);
86 return -EFSCORRUPTED;
87 }
88 key->id = apfs_cat_cnid((struct apfs_key_header *)raw);
89 key->type = apfs_cat_type((struct apfs_key_header *)raw);
90
91 switch (key->type) {
92 case APFS_TYPE_DIR_REC:
93 if (hashed) {
94 if (size < sizeof(struct apfs_drec_hashed_key) + 1 ||
95 *((char *)raw + size - 1) != 0) {
96 /* Filename must have NULL-termination */
97 apfs_err(NULL, "invalid drec key (%d)", size);
98 return -EFSCORRUPTED;
99 }
100 /* Name length is not used in key comparisons, only the hash */
101 key->number = le32_to_cpu(
102 ((struct apfs_drec_hashed_key *)raw)->name_len_and_hash) &
103 APFS_DREC_HASH_MASK;
104 key->name = ((struct apfs_drec_hashed_key *)raw)->name;
105 } else {
106 if (size < sizeof(struct apfs_drec_key) + 1 ||
107 *((char *)raw + size - 1) != 0) {
108 /* Filename must have NULL-termination */
109 apfs_err(NULL, "invalid drec key (%d)", size);
110 return -EFSCORRUPTED;
111 }
112 /* There's no hash */
113 key->number = 0;
114 key->name = ((struct apfs_drec_key *)raw)->name;
115 }
116 break;
117 case APFS_TYPE_XATTR:
118 if (size < sizeof(struct apfs_xattr_key) + 1 ||
119 *((char *)raw + size - 1) != 0) {
120 /* xattr name must have NULL-termination */
121 apfs_err(NULL, "invalid xattr key (%d)", size);
122 return -EFSCORRUPTED;
123 }
124 key->number = 0;
125 key->name = ((struct apfs_xattr_key *)raw)->name;
126 break;
127 case APFS_TYPE_FILE_EXTENT:
128 if (size != sizeof(struct apfs_file_extent_key)) {
129 apfs_err(NULL, "bad key length (%d)", size);
130 return -EFSCORRUPTED;
131 }
132 key->number = le64_to_cpu(
133 ((struct apfs_file_extent_key *)raw)->logical_addr);
134 key->name = NULL;
135 break;
136 case APFS_TYPE_SIBLING_LINK:
137 if (size != sizeof(struct apfs_sibling_link_key)) {
138 apfs_err(NULL, "bad key length (%d)", size);
139 return -EFSCORRUPTED;
140 }
141 key->number = le64_to_cpu(
142 ((struct apfs_sibling_link_key *)raw)->sibling_id);
143 key->name = NULL;
144 break;
145 default:
146 key->number = 0;
147 key->name = NULL;
148 break;
149 }
150
151 return 0;
152 }
153
154 int apfs_read_fext_key(void *raw, int size, struct apfs_key *key)
155 {
156 struct apfs_fext_tree_key *raw_key;
157
158 if (size != sizeof(*raw_key)) {
159 apfs_err(NULL, "bad key length (%d)", size);
160 return -EFSCORRUPTED;
161 }
162 raw_key = raw;
163
164 key->id = le64_to_cpu(raw_key->private_id);
165 key->type = 0;
166 key->number = le64_to_cpu(raw_key->logical_addr);
167 key->name = NULL;
168 return 0;
169 }
170
171 /**
172 * apfs_read_free_queue_key - Parse an on-disk free queue key
173 * @raw: pointer to the raw key
174 * @size: size of the raw key
175 * @key: apfs_key structure to store the result
176 *
177 * Returns 0 on success, or a negative error code otherwise.
178 */
179 int apfs_read_free_queue_key(void *raw, int size, struct apfs_key *key)
180 {
181 struct apfs_spaceman_free_queue_key *raw_key;
182
183 if (size != sizeof(*raw_key)) {
184 apfs_err(NULL, "bad key length (%d)", size);
185 return -EFSCORRUPTED;
186 }
187 raw_key = raw;
188
189 key->id = le64_to_cpu(raw_key->sfqk_xid);
190 key->type = 0;
191 key->number = le64_to_cpu(raw_key->sfqk_paddr);
192 key->name = NULL;
193
194 return 0;
195 }
196
197 /**
198 * apfs_read_omap_key - Parse an on-disk object map key
199 * @raw: pointer to the raw key
200 * @size: size of the raw key
201 * @key: apfs_key structure to store the result
202 *
203 * Returns 0 on success, or a negative error code otherwise.
204 */
205 int apfs_read_omap_key(void *raw, int size, struct apfs_key *key)
206 {
207 struct apfs_omap_key *raw_key;
208
209 if (size != sizeof(*raw_key)) {
210 apfs_err(NULL, "bad key length (%d)", size);
211 return -EFSCORRUPTED;
212 }
213 raw_key = raw;
214
215 key->id = le64_to_cpu(raw_key->ok_oid);
216 key->type = 0;
217 key->number = le64_to_cpu(raw_key->ok_xid);
218 key->name = NULL;
219
220 return 0;
221 }
222
223 /**
224 * apfs_read_extentref_key - Parse an on-disk extent reference tree key
225 * @raw: pointer to the raw key
226 * @size: size of the raw key
227 * @key: apfs_key structure to store the result
228 *
229 * Returns 0 on success, or a negative error code otherwise.
230 */
231 int apfs_read_extentref_key(void *raw, int size, struct apfs_key *key)
232 {
233 if (size != sizeof(struct apfs_phys_ext_key)) {
234 apfs_err(NULL, "bad key length (%d)", size);
235 return -EFSCORRUPTED;
236 }
237 key->id = apfs_cat_cnid((struct apfs_key_header *)raw);
238 key->type = apfs_cat_type((struct apfs_key_header *)raw);
239 key->number = 0;
240 key->name = NULL;
241 return 0;
242 }
243
244 int apfs_read_snap_meta_key(void *raw, int size, struct apfs_key *key)
245 {
246 if (size < sizeof(struct apfs_key_header)) {
247 apfs_err(NULL, "bad key length (%d)", size);
248 return -EFSCORRUPTED;
249 }
250 key->id = apfs_cat_cnid((struct apfs_key_header *)raw);
251 key->type = apfs_cat_type((struct apfs_key_header *)raw);
252 key->number = 0;
253
254 switch (key->type) {
255 case APFS_TYPE_SNAP_METADATA:
256 if (size != sizeof(struct apfs_snap_metadata_key)) {
257 apfs_err(NULL, "bad key length (%d)", size);
258 return -EFSCORRUPTED;
259 }
260 key->name = NULL;
261 return 0;
262 case APFS_TYPE_SNAP_NAME:
263 if (size < sizeof(struct apfs_snap_name_key) + 1 || *((char *)raw + size - 1) != 0) {
264 /* snapshot name must have NULL-termination */
265 apfs_err(NULL, "invalid snap name key (%d)", size);
266 return -EFSCORRUPTED;
267 }
268 key->name = ((struct apfs_snap_name_key *)raw)->name;
269 return 0;
270 default:
271 return -EFSCORRUPTED;
272 }
273 }
274
275 int apfs_read_omap_snap_key(void *raw, int size, struct apfs_key *key)
276 {
277 __le64 *xid = NULL;
278
279 if (size != sizeof(*xid)) {
280 apfs_err(NULL, "bad key length (%d)", size);
281 return -EFSCORRUPTED;
282 }
283 xid = raw;
284
285 key->id = le64_to_cpup(xid);
286 key->number = 0;
287 key->name = NULL;
288 key->type = 0;
289 return 0;
290 }
291
292 /**
293 * apfs_init_drec_key - Initialize an in-memory key for a dentry query
294 * @sb: filesystem superblock
295 * @ino: inode number of the parent directory
296 * @name: filename (NULL for a multiple query)
297 * @name_len: filename length (0 if NULL)
298 * @key: apfs_key structure to initialize
299 */
300 void apfs_init_drec_key(struct super_block *sb, u64 ino, const char *name,
301 unsigned int name_len, struct apfs_key *key)
302 {
303 struct apfs_unicursor cursor;
304 bool case_fold = apfs_is_case_insensitive(sb);
305 u32 hash = 0xFFFFFFFF;
306
307 key->id = ino;
308 key->type = APFS_TYPE_DIR_REC;
309 if (!apfs_is_normalization_insensitive(sb)) {
310 key->name = name;
311 key->number = 0;
312 return;
313 }
314
315 /* To respect normalization, queries can only consider the hash */
316 key->name = NULL;
317
318 if (!name) {
319 key->number = 0;
320 return;
321 }
322
323 apfs_init_unicursor(&cursor, name, name_len);
324
325 while (1) {
326 unicode_t utf32;
327
328 utf32 = apfs_normalize_next(&cursor, case_fold);
329 if (!utf32)
330 break;
331
332 hash = crc32c(hash, &utf32, sizeof(utf32));
333 }
334
335 /* The filename length doesn't matter, so it's left as zero */
336 key->number = hash << APFS_DREC_HASH_SHIFT;
337 }