Debian Codegraph @

dir.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2018 Ernesto A. Fernández <[email protected]>
4 */
5
6 #include <linux/slab.h>
7 #include <linux/buffer_head.h>
8 #include "apfs.h"
9
10 /**
11 * apfs_drec_from_query - Read the directory record found by a successful query
12 * @query: the query that found the record
13 * @drec: Return parameter. The directory record found.
14 * @hashed: is this record hashed?
15 *
16 * Reads the directory record into @drec and performs some basic sanity checks
17 * as a protection against crafted filesystems. Returns 0 on success or
18 * -EFSCORRUPTED otherwise.
19 *
20 * The caller must not free @query while @drec is in use, because @drec->name
21 * points to data on disk.
22 */
23 static int apfs_drec_from_query(struct apfs_query *query, struct apfs_drec *drec, bool hashed)
24 {
25 struct super_block *sb = query->node->object.sb;
26 char *raw = query->node->object.data;
27 struct apfs_drec_hashed_key *de_hkey = NULL;
28 struct apfs_drec_key *de_ukey = NULL;
29 struct apfs_drec_val *de;
30 int namelen, xlen;
31 char *xval = NULL, *name;
32
33 namelen = query->key_len - (hashed ? sizeof(*de_hkey) : sizeof(*de_ukey));
34 if (namelen < 1) {
35 apfs_err(sb, "key is too small (%d)", query->key_len);
36 return -EFSCORRUPTED;
37 }
38 if (query->len < sizeof(*de)) {
39 apfs_err(sb, "value is too small (%d)", query->len);
40 return -EFSCORRUPTED;
41 }
42
43 de = (struct apfs_drec_val *)(raw + query->off);
44 if (hashed) {
45 de_hkey = (struct apfs_drec_hashed_key *)(raw + query->key_off);
46 if (namelen != (le32_to_cpu(de_hkey->name_len_and_hash) & APFS_DREC_LEN_MASK)) {
47 apfs_err(sb, "inconsistent name length");
48 return -EFSCORRUPTED;
49 }
50 name = de_hkey->name;
51 } else {
52 de_ukey = (struct apfs_drec_key *)(raw + query->key_off);
53 if (namelen != le16_to_cpu(de_ukey->name_len)) {
54 apfs_err(sb, "inconsistent name length");
55 return -EFSCORRUPTED;
56 }
57 name = de_ukey->name;
58 }
59
60 /* Filename must be NULL-terminated */
61 if (name[namelen - 1] != 0) {
62 apfs_err(sb, "null termination missing");
63 return -EFSCORRUPTED;
64 }
65
66 /* The dentry may have at most one xfield: the sibling id */
67 drec->sibling_id = 0;
68 xlen = apfs_find_xfield(de->xfields, query->len - sizeof(*de),
69 APFS_DREC_EXT_TYPE_SIBLING_ID, &xval);
70 if (xlen >= sizeof(__le64)) {
71 __le64 *sib_id = (__le64 *)xval;
72
73 drec->sibling_id = le64_to_cpup(sib_id);
74 }
75
76 drec->name = name;
77 drec->name_len = namelen - 1; /* Don't count the NULL termination */
78 drec->ino = le64_to_cpu(de->file_id);
79
80 drec->type = le16_to_cpu(de->flags) & APFS_DREC_TYPE_MASK;
81 if (drec->type != DT_FIFO && drec->type & 1) /* Invalid file type */
82 drec->type = DT_UNKNOWN;
83 return 0;
84 }
85
86 /**
87 * apfs_dentry_lookup - Lookup a dentry record in the catalog b-tree
88 * @dir: parent directory
89 * @child: filename
90 * @drec: on return, the directory record found
91 *
92 * Runs a catalog query for @name in the @dir directory. On success, sets
93 * @drec and returns a pointer to the query structure. On failure, returns
94 * an appropriate error pointer.
95 */
96 static struct apfs_query *apfs_dentry_lookup(struct inode *dir,
97 const struct qstr *child,
98 struct apfs_drec *drec)
99 {
100 struct super_block *sb = dir->i_sb;
101 struct apfs_sb_info *sbi = APFS_SB(sb);
102 struct apfs_query *query;
103 u64 cnid = apfs_ino(dir);
104 bool hashed = apfs_is_normalization_insensitive(sb);
105 int err;
106
107 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
108 if (!query)
109 return ERR_PTR(-ENOMEM);
110 apfs_init_drec_key(sb, cnid, child->name, child->len, &query->key);
111
112 /*
113 * Distinct filenames in the same directory may (rarely) share the same
114 * hash. The query code cannot handle that because their order in the
115 * b-tree would depend on their unnormalized original names. Just get
116 * all the candidates and check them one by one.
117 *
118 * This is very wasteful for normalization-sensitive filesystems: there
119 * are no hashes so we just check every single file in the directory for
120 * no reason. This would be easy to avoid but does it matter? (TODO)
121 */
122 query->flags |= APFS_QUERY_CAT | APFS_QUERY_ANY_NAME | APFS_QUERY_EXACT;
123 do {
124 err = apfs_btree_query(sb, &query);
125 if (err)
126 goto fail;
127 err = apfs_drec_from_query(query, drec, hashed);
128 if (err)
129 goto fail;
130 } while (unlikely(apfs_filename_cmp(sb, child->name, child->len, drec->name, drec->name_len)));
131
132 /*
133 * We may need to refresh the query later, but the refresh code doesn't
134 * know how to deal with hash collisions. Instead set the key to the
135 * unnormalized name and pretend that this was never a multiple query
136 * in the first place.
137 */
138 query->key.name = drec->name;
139 query->flags &= ~(APFS_QUERY_MULTIPLE | APFS_QUERY_DONE | APFS_QUERY_NEXT);
140 return query;
141
142 fail:
143 if (err != -ENODATA)
144 apfs_err(sb, "query failed in dir 0x%llx", cnid);
145 apfs_free_query(query);
146 return ERR_PTR(err);
147 }
148
149 /**
150 * apfs_inode_by_name - Find the cnid for a given filename
151 * @dir: parent directory
152 * @child: filename
153 * @ino: on return, the inode number found
154 *
155 * Returns 0 and the inode number (which is the cnid of the file
156 * record); otherwise, return the appropriate error code.
157 */
158 int apfs_inode_by_name(struct inode *dir, const struct qstr *child, u64 *ino)
159 {
160 struct super_block *sb = dir->i_sb;
161 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
162 struct apfs_query *query;
163 struct apfs_drec drec;
164 int err = 0;
165
166 down_read(&nxi->nx_big_sem);
167 query = apfs_dentry_lookup(dir, child, &drec);
168 if (IS_ERR(query)) {
169 err = PTR_ERR(query);
170 goto out;
171 }
172 *ino = drec.ino;
173 apfs_free_query(query);
174 out:
175 up_read(&nxi->nx_big_sem);
176 return err;
177 }
178
179 static int apfs_readdir(struct file *file, struct dir_context *ctx)
180 {
181 struct inode *inode = file_inode(file);
182 struct super_block *sb = inode->i_sb;
183 struct apfs_sb_info *sbi = APFS_SB(sb);
184 struct apfs_nxsb_info *nxi = APFS_NXI(sb);
185 struct apfs_query *query;
186 u64 cnid = apfs_ino(inode);
187 loff_t pos;
188 bool hashed = apfs_is_normalization_insensitive(sb);
189 int err = 0;
190
191 down_read(&nxi->nx_big_sem);
192
193 /* Inode numbers might overflow here; follow btrfs in ignoring that */
194 if (!dir_emit_dots(file, ctx))
195 goto out;
196
197 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
198 if (!query) {
199 err = -ENOMEM;
200 goto out;
201 }
202
203 /* We want all the children for the cnid, regardless of the name */
204 apfs_init_drec_key(sb, cnid, NULL /* name */, 0 /* name_len */, &query->key);
205 query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT;
206
207 pos = ctx->pos - 2;
208 while (1) {
209 struct apfs_drec drec;
210 /*
211 * We query for the matching records, one by one. After we
212 * pass ctx->pos we begin to emit them.
213 *
214 * TODO: Faster approach for large directories?
215 */
216
217 err = apfs_btree_query(sb, &query);
218 if (err == -ENODATA) { /* Got all the records */
219 err = 0;
220 break;
221 }
222 if (err)
223 break;
224
225 err = apfs_drec_from_query(query, &drec, hashed);
226 if (err) {
227 apfs_alert(sb, "bad dentry record in directory 0x%llx",
228 cnid);
229 break;
230 }
231
232 err = 0;
233 if (pos <= 0) {
234 if (!dir_emit(ctx, drec.name, drec.name_len,
235 drec.ino, drec.type))
236 break;
237 ++ctx->pos;
238 }
239 pos--;
240 }
241 apfs_free_query(query);
242
243 out:
244 up_read(&nxi->nx_big_sem);
245 return err;
246 }
247
248 const struct file_operations apfs_dir_operations = {
249 .llseek = generic_file_llseek,
250 .read = generic_read_dir,
251 .iterate_shared = apfs_readdir,
252 .fsync = apfs_fsync,
253 .unlocked_ioctl = apfs_dir_ioctl,
254 };
255
256 /**
257 * apfs_build_dentry_unhashed_key - Allocate and initialize the key for an unhashed dentry record
258 * @qname: filename
259 * @parent_id: inode number for the parent of the dentry
260 * @key_p: on return, a pointer to the new on-disk key structure
261 *
262 * Returns the length of the key, or a negative error code in case of failure.
263 */
264 static int apfs_build_dentry_unhashed_key(const struct qstr *qname, u64 parent_id,
265 struct apfs_drec_key **key_p)
266 {
267 struct apfs_drec_key *key;
268 u16 namelen = qname->len + 1; /* We count the null-termination */
269 int key_len;
270
271 key_len = sizeof(*key) + namelen;
272 key = kmalloc(key_len, GFP_KERNEL);
273 if (!key)
274 return -ENOMEM;
275
276 apfs_key_set_hdr(APFS_TYPE_DIR_REC, parent_id, key);
277 key->name_len = cpu_to_le16(namelen);
278 strscpy(key->name, qname->name, namelen);
279
280 *key_p = key;
281 return key_len;
282 }
283
284 /**
285 * apfs_build_dentry_hashed_key - Allocate and initialize the key for a hashed dentry record
286 * @qname: filename
287 * @hash: filename hash
288 * @parent_id: inode number for the parent of the dentry
289 * @key_p: on return, a pointer to the new on-disk key structure
290 *
291 * Returns the length of the key, or a negative error code in case of failure.
292 */
293 static int apfs_build_dentry_hashed_key(const struct qstr *qname, u64 hash, u64 parent_id,
294 struct apfs_drec_hashed_key **key_p)
295 {
296 struct apfs_drec_hashed_key *key;
297 u16 namelen = qname->len + 1; /* We count the null-termination */
298 int key_len;
299
300 key_len = sizeof(*key) + namelen;
301 key = kmalloc(key_len, GFP_KERNEL);
302 if (!key)
303 return -ENOMEM;
304
305 apfs_key_set_hdr(APFS_TYPE_DIR_REC, parent_id, key);
306 key->name_len_and_hash = cpu_to_le32(namelen | hash);
307 strscpy(key->name, qname->name, namelen);
308
309 *key_p = key;
310 return key_len;
311 }
312
313 /**
314 * apfs_build_dentry_val - Allocate and initialize the value for a dentry record
315 * @inode: vfs inode for the dentry
316 * @sibling_id: sibling id for this hardlink (0 for none)
317 * @val_p: on return, a pointer to the new on-disk value structure
318 *
319 * Returns the length of the value, or a negative error code in case of failure.
320 */
321 static int apfs_build_dentry_val(struct inode *inode, u64 sibling_id,
322 struct apfs_drec_val **val_p)
323 {
324 struct apfs_drec_val *val;
325 struct apfs_x_field xkey;
326 int total_xlen = 0, val_len;
327 __le64 raw_sibling_id = cpu_to_le64(sibling_id);
328 struct timespec64 now = current_time(inode);
329
330 /* The dentry record may have one xfield: the sibling id */
331 if (sibling_id)
332 total_xlen += sizeof(struct apfs_xf_blob) +
333 sizeof(xkey) + sizeof(raw_sibling_id);
334
335 val_len = sizeof(*val) + total_xlen;
336 val = kmalloc(val_len, GFP_KERNEL);
337 if (!val)
338 return -ENOMEM;
339 *val_p = val;
340
341 val->file_id = cpu_to_le64(apfs_ino(inode));
342 val->date_added = cpu_to_le64(timespec64_to_ns(&now));
343 val->flags = cpu_to_le16((inode->i_mode >> 12) & 15); /* File type */
344
345 if (!sibling_id)
346 return val_len;
347
348 /* The buffer was just allocated: none of these functions should fail */
349 apfs_init_xfields(val->xfields, val_len - sizeof(*val));
350 xkey.x_type = APFS_DREC_EXT_TYPE_SIBLING_ID;
351 xkey.x_flags = 0; /* TODO: proper flags here? */
352 xkey.x_size = cpu_to_le16(sizeof(raw_sibling_id));
353 apfs_insert_xfield(val->xfields, total_xlen, &xkey, &raw_sibling_id);
354 return val_len;
355 }
356
357 /**
358 * apfs_create_dentry_rec - Create a dentry record in the catalog b-tree
359 * @inode: vfs inode for the dentry
360 * @qname: filename
361 * @parent_id: inode number for the parent of the dentry
362 * @sibling_id: sibling id for this hardlink (0 for none)
363 *
364 * Returns 0 on success or a negative error code in case of failure.
365 */
366 static int apfs_create_dentry_rec(struct inode *inode, const struct qstr *qname,
367 u64 parent_id, u64 sibling_id)
368 {
369 struct super_block *sb = inode->i_sb;
370 struct apfs_sb_info *sbi = APFS_SB(sb);
371 struct apfs_query *query;
372 void *raw_key = NULL;
373 struct apfs_drec_val *raw_val = NULL;
374 int key_len, val_len;
375 bool hashed = apfs_is_normalization_insensitive(sb);
376 int ret;
377
378 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
379 if (!query)
380 return -ENOMEM;
381 apfs_init_drec_key(sb, parent_id, qname->name, qname->len, &query->key);
382 query->flags |= APFS_QUERY_CAT;
383
384 ret = apfs_btree_query(sb, &query);
385 if (ret && ret != -ENODATA) {
386 apfs_err(sb, "query failed in dir 0x%llx (hash 0x%llx)", parent_id, query->key.number);
387 goto fail;
388 }
389
390 if (hashed)
391 key_len = apfs_build_dentry_hashed_key(qname, query->key.number, parent_id,
392 (struct apfs_drec_hashed_key **)&raw_key);
393 else
394 key_len = apfs_build_dentry_unhashed_key(qname, parent_id,
395 (struct apfs_drec_key **)&raw_key);
396 if (key_len < 0) {
397 ret = key_len;
398 goto fail;
399 }
400
401 val_len = apfs_build_dentry_val(inode, sibling_id, &raw_val);
402 if (val_len < 0) {
403 ret = val_len;
404 goto fail;
405 }
406 /* TODO: deal with hash collisions */
407 ret = apfs_btree_insert(query, raw_key, key_len, raw_val, val_len);
408 if (ret)
409 apfs_err(sb, "insertion failed in dir 0x%llx (hash 0x%llx)", parent_id, query->key.number);
410
411 fail:
412 kfree(raw_val);
413 kfree(raw_key);
414 apfs_free_query(query);
415 return ret;
416 }
417
418 /**
419 * apfs_build_sibling_val - Allocate and initialize a sibling link's value
420 * @dentry: in-memory dentry for this hardlink
421 * @val_p: on return, a pointer to the new on-disk value structure
422 *
423 * Returns the length of the value, or a negative error code in case of failure.
424 */
425 static int apfs_build_sibling_val(struct dentry *dentry,
426 struct apfs_sibling_val **val_p)
427 {
428 struct apfs_sibling_val *val;
429 const struct qstr *qname = &dentry->d_name;
430 u16 namelen = qname->len + 1; /* We count the null-termination */
431 struct inode *parent = d_inode(dentry->d_parent);
432 int val_len;
433
434 val_len = sizeof(*val) + namelen;
435 val = kmalloc(val_len, GFP_KERNEL);
436 if (!val)
437 return -ENOMEM;
438
439 val->parent_id = cpu_to_le64(apfs_ino(parent));
440 val->name_len = cpu_to_le16(namelen);
441 strscpy(val->name, qname->name, namelen);
442
443 *val_p = val;
444 return val_len;
445 }
446
447 /**
448 * apfs_create_sibling_link_rec - Create a sibling link record for a dentry
449 * @dentry: the in-memory dentry
450 * @inode: vfs inode for the dentry
451 * @sibling_id: sibling id for this hardlink
452 *
453 * Returns 0 on success or a negative error code in case of failure.
454 */
455 static int apfs_create_sibling_link_rec(struct dentry *dentry,
456 struct inode *inode, u64 sibling_id)
457 {
458 struct super_block *sb = dentry->d_sb;
459 struct apfs_sb_info *sbi = APFS_SB(sb);
460 struct apfs_query *query = NULL;
461 struct apfs_sibling_link_key raw_key;
462 struct apfs_sibling_val *raw_val;
463 int val_len;
464 int ret;
465
466 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
467 if (!query)
468 return -ENOMEM;
469 apfs_init_sibling_link_key(apfs_ino(inode), sibling_id, &query->key);
470 query->flags |= APFS_QUERY_CAT;
471
472 ret = apfs_btree_query(sb, &query);
473 if (ret && ret != -ENODATA) {
474 apfs_err(sb, "query failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
475 goto fail;
476 }
477
478 apfs_key_set_hdr(APFS_TYPE_SIBLING_LINK, apfs_ino(inode), &raw_key);
479 raw_key.sibling_id = cpu_to_le64(sibling_id);
480 val_len = apfs_build_sibling_val(dentry, &raw_val);
481 if (val_len < 0)
482 goto fail;
483
484 ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), raw_val, val_len);
485 if (ret)
486 apfs_err(sb, "insertion failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
487 kfree(raw_val);
488
489 fail:
490 apfs_free_query(query);
491 return ret;
492 }
493
494 /**
495 * apfs_create_sibling_map_rec - Create a sibling map record for a dentry
496 * @dentry: the in-memory dentry
497 * @inode: vfs inode for the dentry
498 * @sibling_id: sibling id for this hardlink
499 *
500 * Returns 0 on success or a negative error code in case of failure.
501 */
502 static int apfs_create_sibling_map_rec(struct dentry *dentry,
503 struct inode *inode, u64 sibling_id)
504 {
505 struct super_block *sb = dentry->d_sb;
506 struct apfs_sb_info *sbi = APFS_SB(sb);
507 struct apfs_query *query = NULL;
508 struct apfs_sibling_map_key raw_key;
509 struct apfs_sibling_map_val raw_val;
510 int ret;
511
512 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
513 if (!query)
514 return -ENOMEM;
515 apfs_init_sibling_map_key(sibling_id, &query->key);
516 query->flags |= APFS_QUERY_CAT;
517
518 ret = apfs_btree_query(sb, &query);
519 if (ret && ret != -ENODATA) {
520 apfs_err(sb, "query failed for sibling 0x%llx", sibling_id);
521 goto fail;
522 }
523
524 apfs_key_set_hdr(APFS_TYPE_SIBLING_MAP, sibling_id, &raw_key);
525 raw_val.file_id = cpu_to_le64(apfs_ino(inode));
526
527 ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val));
528 if (ret)
529 apfs_err(sb, "insertion failed for sibling 0x%llx", sibling_id);
530
531 fail:
532 apfs_free_query(query);
533 return ret;
534 }
535
536 /**
537 * apfs_create_sibling_recs - Create sibling link and map records for a dentry
538 * @dentry: the in-memory dentry
539 * @inode: vfs inode for the dentry
540 * @sibling_id: on return, the sibling id for this hardlink
541 *
542 * Returns 0 on success or a negative error code in case of failure.
543 */
544 static int apfs_create_sibling_recs(struct dentry *dentry,
545 struct inode *inode, u64 *sibling_id)
546 {
547 struct super_block *sb = dentry->d_sb;
548 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
549 u64 cnid;
550 int ret;
551
552 /* Sibling ids come from the same pool as the inode numbers */
553 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
554 cnid = le64_to_cpu(vsb_raw->apfs_next_obj_id);
555 le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1);
556
557 ret = apfs_create_sibling_link_rec(dentry, inode, cnid);
558 if (ret)
559 return ret;
560 ret = apfs_create_sibling_map_rec(dentry, inode, cnid);
561 if (ret)
562 return ret;
563
564 *sibling_id = cnid;
565 return 0;
566 }
567
568 /**
569 * apfs_create_dentry - Create all records for a new dentry
570 * @dentry: the in-memory dentry
571 * @inode: vfs inode for the dentry
572 *
573 * Creates the dentry record itself, as well as the sibling records if needed;
574 * also updates the child count for the parent inode. Returns 0 on success or
575 * a negative error code in case of failure.
576 */
577 static int apfs_create_dentry(struct dentry *dentry, struct inode *inode)
578 {
579 struct super_block *sb = inode->i_sb;
580 struct inode *parent = d_inode(dentry->d_parent);
581 u64 sibling_id = 0;
582 int err;
583
584 if (inode->i_nlink > 1) {
585 /* This is optional for a single link, so don't waste space */
586 err = apfs_create_sibling_recs(dentry, inode, &sibling_id);
587 if (err) {
588 apfs_err(sb, "failed to create sibling recs for ino 0x%llx", apfs_ino(inode));
589 return err;
590 }
591 }
592
593 err = apfs_create_dentry_rec(inode, &dentry->d_name, apfs_ino(parent), sibling_id);
594 if (err) {
595 apfs_err(sb, "failed to create drec for ino 0x%llx", apfs_ino(inode));
596 return err;
597 }
598
599 /* Now update the parent inode */
600 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
601 parent->i_mtime = parent->i_ctime = current_time(inode);
602 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
603 parent->i_mtime = inode_set_ctime_current(parent);
604 #else
605 inode_set_mtime_to_ts(parent, inode_set_ctime_current(parent));
606 #endif
607 ++APFS_I(parent)->i_nchildren;
608 apfs_inode_join_transaction(parent->i_sb, parent);
609 return 0;
610 }
611
612 /**
613 * apfs_undo_create_dentry - Clean up apfs_create_dentry()
614 * @dentry: the in-memory dentry
615 */
616 static void apfs_undo_create_dentry(struct dentry *dentry)
617 {
618 struct inode *parent = d_inode(dentry->d_parent);
619
620 --APFS_I(parent)->i_nchildren;
621 }
622
623 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 18, 13)
624
625 static inline void discard_new_inode(struct inode *inode)
626 {
627 unlock_new_inode(inode);
628 iput(inode);
629 }
630
•631 #endif
632
633 int apfs_mkany(struct inode *dir, struct dentry *dentry, umode_t mode,
634 dev_t rdev, const char *symname)
635 {
636 struct super_block *sb = dir->i_sb;
637 struct inode *inode;
638 int err;
639
640 err = apfs_transaction_start(sb, APFS_TRANS_REG);
641 if (err)
642 return err;
643
644 inode = apfs_new_inode(dir, mode, rdev);
645 if (IS_ERR(inode)) {
646 err = PTR_ERR(inode);
647 goto out_abort;
648 }
649
650 err = apfs_create_inode_rec(sb, inode, dentry);
651 if (err) {
652 apfs_err(sb, "failed to create inode rec for ino 0x%llx", apfs_ino(inode));
653 goto out_discard_inode;
654 }
655
656 err = apfs_create_dentry(dentry, inode);
657 if (err) {
658 apfs_err(sb, "failed to create dentry recs for ino 0x%llx", apfs_ino(inode));
659 goto out_discard_inode;
660 }
661
662 if (symname) {
663 err = apfs_xattr_set(inode, APFS_XATTR_NAME_SYMLINK, symname,
664 strlen(symname) + 1, 0 /* flags */);
665 if (err == -ERANGE) {
666 err = -ENAMETOOLONG;
667 goto out_undo_create;
668 }
669 if (err) {
670 apfs_err(sb, "failed to set symlink xattr for ino 0x%llx", apfs_ino(inode));
671 goto out_undo_create;
672 }
673 }
674
675 err = apfs_transaction_commit(sb);
676 if (err)
677 goto out_undo_create;
678
679 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
680 /* Apparently there is a lockdep bug here, but it doesn't matter */
681 unlock_new_inode(inode);
682 d_instantiate(dentry, inode);
683 #else
684 d_instantiate_new(dentry, inode);
685 #endif
686 return 0;
687
688 out_undo_create:
689 apfs_undo_create_dentry(dentry);
690 out_discard_inode:
691 /* Don't reset nlink: on-disk cleanup is unneeded and would deadlock */
692 discard_new_inode(inode);
693 out_abort:
694 apfs_transaction_abort(sb);
695 return err;
696 }
697
698 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
699 int apfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
700 dev_t rdev)
701 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
702 int apfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
703 struct dentry *dentry, umode_t mode, dev_t rdev)
704 #else
705 int apfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
706 struct dentry *dentry, umode_t mode, dev_t rdev)
707 #endif
708 {
709 return apfs_mkany(dir, dentry, mode, rdev, NULL /* symname */);
710 }
711
712 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
713
714 int apfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
715 {
716 return apfs_mknod(dir, dentry, mode | S_IFDIR, 0 /* rdev */);
717 }
718
719 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
720
721 int apfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
722 struct dentry *dentry, umode_t mode)
723 {
724 return apfs_mknod(mnt_userns, dir, dentry, mode | S_IFDIR, 0 /* rdev */);
725 }
726
727 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 15, 0)
728
729 int apfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
730 struct dentry *dentry, umode_t mode)
731 {
732 return apfs_mknod(idmap, dir, dentry, mode | S_IFDIR, 0 /* rdev */);
733 }
734
735 #else
736
737 struct dentry *apfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, struct dentry *dentry, umode_t mode)
738 {
739 return ERR_PTR(apfs_mknod(idmap, dir, dentry, mode | S_IFDIR, 0 /* rdev */));
740 }
741
742 #endif
743
744
745 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
746
747 int apfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
748 bool excl)
749 {
750 return apfs_mknod(dir, dentry, mode, 0 /* rdev */);
751 }
752
753 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
754
755 int apfs_create(struct user_namespace *mnt_userns, struct inode *dir,
756 struct dentry *dentry, umode_t mode, bool excl)
757 {
758 return apfs_mknod(mnt_userns, dir, dentry, mode, 0 /* rdev */);
759 }
760
761 #else
762
763 int apfs_create(struct mnt_idmap *idmap, struct inode *dir,
764 struct dentry *dentry, umode_t mode, bool excl)
765 {
766 return apfs_mknod(idmap, dir, dentry, mode, 0 /* rdev */);
767 }
768
769 #endif
770
771
772 /**
773 * apfs_prepare_dentry_for_link - Assign a sibling id and records to a dentry
774 * @dentry: the in-memory dentry (should be for a primary link)
775 *
776 * Returns 0 on success, or a negative error code in case of failure.
777 */
778 static int apfs_prepare_dentry_for_link(struct dentry *dentry)
779 {
780 struct inode *parent = d_inode(dentry->d_parent);
781 struct super_block *sb = parent->i_sb;
782 struct apfs_query *query;
783 struct apfs_drec drec;
784 u64 sibling_id;
785 int ret;
786
787 query = apfs_dentry_lookup(parent, &dentry->d_name, &drec);
788 if (IS_ERR(query)) {
789 apfs_err(sb, "lookup failed in dir 0x%llx", apfs_ino(parent));
790 return PTR_ERR(query);
791 }
792 if (drec.sibling_id) {
793 /* This dentry already has a sibling id xfield */
794 apfs_free_query(query);
795 return 0;
796 }
797
798 /* Don't modify the dentry record, just delete it to make a new one */
799 ret = apfs_btree_remove(query);
800 apfs_free_query(query);
801 if (ret) {
802 apfs_err(sb, "removal failed in dir 0x%llx", apfs_ino(parent));
803 return ret;
804 }
805
806 ret = apfs_create_sibling_recs(dentry, d_inode(dentry), &sibling_id);
807 if (ret) {
808 apfs_err(sb, "failed to create sibling recs in dir 0x%llx", apfs_ino(parent));
809 return ret;
810 }
811 return apfs_create_dentry_rec(d_inode(dentry), &dentry->d_name,
812 apfs_ino(parent), sibling_id);
813 }
814
815 /**
816 * __apfs_undo_link - Clean up __apfs_link()
817 * @dentry: the in-memory dentry
818 * @inode: target inode
819 */
820 static void __apfs_undo_link(struct dentry *dentry, struct inode *inode)
821 {
822 apfs_undo_create_dentry(dentry);
823 drop_nlink(inode);
824 }
825
826 /**
827 * __apfs_link - Link a dentry
828 * @old_dentry: dentry for the old link
829 * @dentry: new dentry to link
830 *
831 * Does the same as apfs_link(), but without starting a transaction, taking a
832 * new reference to @old_dentry->d_inode, or instantiating @dentry.
833 */
834 static int __apfs_link(struct dentry *old_dentry, struct dentry *dentry)
835 {
836 struct inode *inode = d_inode(old_dentry);
837 struct super_block *sb = inode->i_sb;
838 int err;
839
840 /* First update the inode's link count */
841 inc_nlink(inode);
842 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
843 inode->i_ctime = current_time(inode);
844 #else
845 inode_set_ctime_current(inode);
846 #endif
847 apfs_inode_join_transaction(inode->i_sb, inode);
848
849 if (inode->i_nlink == 2) {
850 /* A single link may lack sibling records, so create them now */
851 err = apfs_prepare_dentry_for_link(old_dentry);
852 if (err) {
853 apfs_err(sb, "failed to prepare original dentry");
854 goto fail;
855 }
856 }
857
858 err = apfs_create_dentry(dentry, inode);
859 if (err) {
860 apfs_err(sb, "failed to create new dentry");
861 goto fail;
862 }
863 return 0;
864
865 fail:
866 drop_nlink(inode);
867 return err;
868 }
869
870 int apfs_link(struct dentry *old_dentry, struct inode *dir,
871 struct dentry *dentry)
872 {
873 struct super_block *sb = dir->i_sb;
874 struct inode *inode = d_inode(old_dentry);
875 int err;
876
877 err = apfs_transaction_start(sb, APFS_TRANS_REG);
878 if (err)
879 return err;
880
881 err = __apfs_link(old_dentry, dentry);
882 if (err)
883 goto out_abort;
884 ihold(inode);
885
886 err = apfs_transaction_commit(sb);
887 if (err)
888 goto out_undo_link;
889
890 d_instantiate(dentry, inode);
891 return 0;
892
893 out_undo_link:
894 iput(inode);
895 __apfs_undo_link(dentry, inode);
896 out_abort:
897 apfs_transaction_abort(sb);
898 return err;
899 }
900
901 /**
902 * apfs_delete_sibling_link_rec - Delete the sibling link record for a dentry
903 * @dentry: the in-memory dentry
904 * @sibling_id: sibling id for this hardlink
905 *
906 * Returns 0 on success or a negative error code in case of failure.
907 */
908 static int apfs_delete_sibling_link_rec(struct dentry *dentry, u64 sibling_id)
909 {
910 struct super_block *sb = dentry->d_sb;
911 struct apfs_sb_info *sbi = APFS_SB(sb);
912 struct inode *inode = d_inode(dentry);
913 struct apfs_query *query = NULL;
914 int ret;
915
916 ASSERT(sibling_id);
917
918 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
919 if (!query)
920 return -ENOMEM;
921 apfs_init_sibling_link_key(apfs_ino(inode), sibling_id, &query->key);
922 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
923
924 ret = apfs_btree_query(sb, &query);
925 if (ret == -ENODATA) {
926 /* A dentry with a sibling id must have sibling records */
927 ret = -EFSCORRUPTED;
928 }
929 if (ret) {
930 apfs_err(sb, "query failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
931 goto fail;
932 }
933 ret = apfs_btree_remove(query);
934 if (ret)
935 apfs_err(sb, "removal failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
936
937 fail:
938 apfs_free_query(query);
939 return ret;
940 }
941
942 /**
943 * apfs_delete_sibling_map_rec - Delete the sibling map record for a dentry
944 * @dentry: the in-memory dentry
945 * @sibling_id: sibling id for this hardlink
946 *
947 * Returns 0 on success or a negative error code in case of failure.
948 */
949 static int apfs_delete_sibling_map_rec(struct dentry *dentry, u64 sibling_id)
950 {
951 struct super_block *sb = dentry->d_sb;
952 struct apfs_sb_info *sbi = APFS_SB(sb);
953 struct apfs_query *query = NULL;
954 int ret;
955
956 ASSERT(sibling_id);
957
958 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
959 if (!query)
960 return -ENOMEM;
961 apfs_init_sibling_map_key(sibling_id, &query->key);
962 query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
963
964 ret = apfs_btree_query(sb, &query);
965 if (ret == -ENODATA) {
966 /* A dentry with a sibling id must have sibling records */
967 ret = -EFSCORRUPTED;
968 }
969 if (ret) {
970 apfs_err(sb, "query failed for sibling 0x%llx", sibling_id);
971 goto fail;
972 }
973 ret = apfs_btree_remove(query);
974 if (ret)
975 apfs_err(sb, "removal failed for sibling 0x%llx", sibling_id);
976
977 fail:
978 apfs_free_query(query);
979 return ret;
980 }
981
982 /**
983 * apfs_delete_dentry - Delete all records for a dentry
984 * @dentry: the in-memory dentry
985 *
986 * Deletes the dentry record itself, as well as the sibling records if they
987 * exist; also updates the child count for the parent inode. Returns 0 on
988 * success or a negative error code in case of failure.
989 */
990 static int apfs_delete_dentry(struct dentry *dentry)
991 {
992 struct super_block *sb = dentry->d_sb;
993 struct inode *parent = d_inode(dentry->d_parent);
994 struct apfs_query *query;
995 struct apfs_drec drec;
996 int err;
997
998 query = apfs_dentry_lookup(parent, &dentry->d_name, &drec);
999 if (IS_ERR(query))
1000 return PTR_ERR(query);
1001 err = apfs_btree_remove(query);
1002 apfs_free_query(query);
1003 if (err) {
1004 apfs_err(sb, "drec removal failed");
1005 return err;
1006 }
1007
1008 if (drec.sibling_id) {
1009 err = apfs_delete_sibling_link_rec(dentry, drec.sibling_id);
1010 if (err) {
1011 apfs_err(sb, "sibling link removal failed");
1012 return err;
1013 }
1014 err = apfs_delete_sibling_map_rec(dentry, drec.sibling_id);
1015 if (err) {
1016 apfs_err(sb, "sibling map removal failed");
1017 return err;
1018 }
1019 }
1020
1021 /* Now update the parent inode */
1022 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1023 parent->i_mtime = parent->i_ctime = current_time(parent);
1024 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
1025 parent->i_mtime = inode_set_ctime_current(parent);
1026 #else
1027 inode_set_mtime_to_ts(parent, inode_set_ctime_current(parent));
1028 #endif
1029 --APFS_I(parent)->i_nchildren;
1030 apfs_inode_join_transaction(sb, parent);
1031 return err;
1032 }
1033
1034 /**
1035 * apfs_undo_delete_dentry - Clean up apfs_delete_dentry()
1036 * @dentry: the in-memory dentry
1037 */
1038 static inline void apfs_undo_delete_dentry(struct dentry *dentry)
1039 {
1040 struct inode *parent = d_inode(dentry->d_parent);
1041
1042 /* Cleanup for the on-disk changes will happen on transaction abort */
1043 ++APFS_I(parent)->i_nchildren;
1044 }
1045
1046 /**
1047 * apfs_sibling_link_from_query - Read the sibling link record found by a query
1048 * @query: the query that found the record
1049 * @name: on return, the name of link
1050 * @parent: on return, the inode number for the link's parent
1051 *
1052 * Reads the sibling link information into @parent and @name, and performs some
1053 * basic sanity checks as a protection against crafted filesystems. The caller
1054 * must free @name after use. Returns 0 on success or a negative error code in
1055 * case of failure.
1056 */
1057 static int apfs_sibling_link_from_query(struct apfs_query *query,
1058 char **name, u64 *parent)
1059 {
1060 struct super_block *sb = query->node->object.sb;
1061 char *raw = query->node->object.data;
1062 struct apfs_sibling_val *siblink;
1063 int namelen = query->len - sizeof(*siblink);
1064
1065 if (namelen < 1) {
1066 apfs_err(sb, "value is too small (%d)", query->len);
1067 return -EFSCORRUPTED;
1068 }
1069 siblink = (struct apfs_sibling_val *)(raw + query->off);
1070
1071 if (namelen != le16_to_cpu(siblink->name_len)) {
1072 apfs_err(sb, "inconsistent name length");
1073 return -EFSCORRUPTED;
1074 }
1075 /* Filename must be NULL-terminated */
1076 if (siblink->name[namelen - 1] != 0) {
1077 apfs_err(sb, "null termination missing");
1078 return -EFSCORRUPTED;
1079 }
1080
1081 *name = kmalloc(namelen, GFP_KERNEL);
1082 if (!*name)
1083 return -ENOMEM;
1084 strscpy(*name, siblink->name, namelen);
1085 *parent = le64_to_cpu(siblink->parent_id);
1086 return 0;
1087 }
1088
1089 /**
1090 * apfs_find_primary_link - Find the primary link for an inode
1091 * @inode: the vfs inode
1092 * @name: on return, the name of the primary link
1093 * @parent: on return, the inode number for the primary parent
1094 *
1095 * On success, returns 0 and sets @parent and @name; the second must be freed
1096 * by the caller after use. Returns a negative error code in case of failure.
1097 */
1098 static int apfs_find_primary_link(struct inode *inode, char **name, u64 *parent)
1099 {
1100 struct super_block *sb = inode->i_sb;
1101 struct apfs_sb_info *sbi = APFS_SB(sb);
1102 struct apfs_query *query;
1103 int err;
1104
1105 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
1106 if (!query)
1107 return -ENOMEM;
1108 apfs_init_sibling_link_key(apfs_ino(inode), 0 /* sibling_id */, &query->key);
1109 query->flags |= APFS_QUERY_CAT | APFS_QUERY_ANY_NUMBER | APFS_QUERY_EXACT;
1110
1111 /* The primary link is the one with the lowest sibling id */
1112 *name = NULL;
1113 while (1) {
1114 err = apfs_btree_query(sb, &query);
1115 if (err == -ENODATA) /* No more link records */
1116 break;
1117 kfree(*name);
1118 if (err) {
1119 apfs_err(sb, "query failed for ino 0x%llx", apfs_ino(inode));
1120 goto fail;
1121 }
1122
1123 err = apfs_sibling_link_from_query(query, name, parent);
1124 if (err) {
1125 apfs_err(sb, "bad sibling link record for ino 0x%llx", apfs_ino(inode));
1126 goto fail;
1127 }
1128 }
1129 err = *name ? 0 : -EFSCORRUPTED; /* Sibling records must exist */
1130 if (err)
1131 apfs_err(sb, "query failed for ino 0x%llx", apfs_ino(inode));
1132
1133 fail:
1134 apfs_free_query(query);
1135 return err;
1136 }
1137
1138 /**
1139 * apfs_orphan_name - Get the name for an orphan inode's invisible link
1140 * @ino: the inode number
1141 * @qname: on return, the name assigned to the link
1142 *
1143 * Returns 0 on success; the caller must remember to free @qname->name after
1144 * use. Returns a negative error code in case of failure.
1145 */
1146 static int apfs_orphan_name(u64 ino, struct qstr *qname)
1147 {
1148 int max_len;
1149 char *name;
1150
1151 /* The name is the inode number in hex, with '-dead' suffix */
1152 max_len = 2 + 16 + 5 + 1;
1153 name = kmalloc(max_len, GFP_KERNEL);
1154 if (!name)
1155 return -ENOMEM;
1156 qname->len = snprintf(name, max_len, "0x%llx-dead", ino);
1157 qname->name = name;
1158 return 0;
1159 }
1160
1161 /**
1162 * apfs_create_orphan_link - Create a link for an orphan inode under private-dir
1163 * @inode: the vfs inode
1164 *
1165 * On success, returns 0. Returns a negative error code in case of failure.
1166 */
1167 static int apfs_create_orphan_link(struct inode *inode)
1168 {
1169 struct super_block *sb = inode->i_sb;
1170 struct apfs_sb_info *sbi = APFS_SB(sb);
1171 struct inode *priv_dir = sbi->s_private_dir;
1172 struct qstr qname;
1173 int err = 0;
1174
1175 err = apfs_orphan_name(apfs_ino(inode), &qname);
1176 if (err)
1177 return err;
1178 err = apfs_create_dentry_rec(inode, &qname, apfs_ino(priv_dir), 0 /* sibling_id */);
1179 if (err) {
1180 apfs_err(sb, "failed to create drec for ino 0x%llx", apfs_ino(inode));
1181 goto fail;
1182 }
1183
1184 /* Now update the child count for private-dir */
1185 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1186 priv_dir->i_mtime = priv_dir->i_ctime = current_time(priv_dir);
1187 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
1188 priv_dir->i_mtime = inode_set_ctime_current(priv_dir);
1189 #else
1190 inode_set_mtime_to_ts(priv_dir, inode_set_ctime_current(priv_dir));
1191 #endif
1192 ++APFS_I(priv_dir)->i_nchildren;
1193 apfs_inode_join_transaction(sb, priv_dir);
1194
1195 fail:
1196 kfree(qname.name);
1197 return err;
1198 }
1199
1200 /**
1201 * apfs_delete_orphan_link - Delete the link for an orphan inode
1202 * @inode: the vfs inode
1203 *
1204 * Returns 0 on success or a negative error code in case of failure.
1205 */
1206 int apfs_delete_orphan_link(struct inode *inode)
1207 {
1208 struct super_block *sb = inode->i_sb;
1209 struct apfs_sb_info *sbi = APFS_SB(sb);
1210 struct inode *priv_dir = sbi->s_private_dir;
1211 struct apfs_query *query;
1212 struct qstr qname;
1213 struct apfs_drec drec;
1214 int err;
1215
1216 err = apfs_orphan_name(apfs_ino(inode), &qname);
1217 if (err)
1218 return err;
1219
1220 query = apfs_dentry_lookup(priv_dir, &qname, &drec);
1221 if (IS_ERR(query)) {
1222 apfs_err(sb, "dentry lookup failed");
1223 err = PTR_ERR(query);
1224 query = NULL;
1225 goto fail;
1226 }
1227 err = apfs_btree_remove(query);
1228 if (err) {
1229 apfs_err(sb, "dentry removal failed");
1230 goto fail;
1231 }
1232
1233 /* Now update the child count for private-dir */
1234 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1235 priv_dir->i_mtime = priv_dir->i_ctime = current_time(priv_dir);
1236 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
1237 priv_dir->i_mtime = inode_set_ctime_current(priv_dir);
1238 #else
1239 inode_set_mtime_to_ts(priv_dir, inode_set_ctime_current(priv_dir));
1240 #endif
1241 --APFS_I(priv_dir)->i_nchildren;
1242 apfs_inode_join_transaction(sb, priv_dir);
1243
1244 fail:
1245 apfs_free_query(query);
1246 kfree(qname.name);
1247 return err;
1248 }
1249
1250 /**
1251 * __apfs_undo_unlink - Clean up __apfs_unlink()
1252 * @dentry: dentry to unlink
1253 */
1254 static void __apfs_undo_unlink(struct dentry *dentry)
1255 {
1256 struct inode *inode = d_inode(dentry);
1257
1258 /* Silence warning about nlink 0->1 */
1259 apfs_inode_state_set_raw(inode, I_LINKABLE);
1260 inc_nlink(inode);
1261 apfs_inode_state_clear_raw(inode, I_LINKABLE);
1262
1263 apfs_undo_delete_dentry(dentry);
1264 }
1265
1266 /**
1267 * apfs_vol_filecnt_dec - Update the volume file count after a new orphaning
1268 * @orphan: the new orphan
1269 */
1270 static void apfs_vol_filecnt_dec(struct inode *orphan)
1271 {
1272 struct super_block *sb = orphan->i_sb;
1273 struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
1274
1275 apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
1276
1277 switch (orphan->i_mode & S_IFMT) {
1278 case S_IFREG:
1279 le64_add_cpu(&vsb_raw->apfs_num_files, -1);
1280 break;
1281 case S_IFDIR:
1282 le64_add_cpu(&vsb_raw->apfs_num_directories, -1);
1283 break;
1284 case S_IFLNK:
1285 le64_add_cpu(&vsb_raw->apfs_num_symlinks, -1);
1286 break;
1287 default:
1288 le64_add_cpu(&vsb_raw->apfs_num_other_fsobjects, -1);
1289 break;
1290 }
1291 }
1292
1293 /**
1294 * __apfs_unlink - Unlink a dentry
1295 * @dir: parent directory
1296 * @dentry: dentry to unlink
1297 *
1298 * Does the same as apfs_unlink(), but without starting a transaction.
1299 */
1300 static int __apfs_unlink(struct inode *dir, struct dentry *dentry)
1301 {
1302 struct inode *inode = d_inode(dentry);
1303 struct super_block *sb = inode->i_sb;
1304 struct apfs_inode_info *ai = APFS_I(inode);
1305 char *primary_name = NULL;
1306 int err;
1307
1308 err = apfs_delete_dentry(dentry);
1309 if (err) {
1310 apfs_err(sb, "failed to delete dentry recs for ino 0x%llx", apfs_ino(inode));
1311 return err;
1312 }
1313
1314 drop_nlink(inode);
1315 if (!inode->i_nlink) {
1316 /* Orphaned inodes continue to report their old location */
1317 err = apfs_create_orphan_link(inode);
1318 /* Orphans are not included in the volume file counts */
1319 apfs_vol_filecnt_dec(inode);
1320 } else {
1321 /* We may have deleted the primary link, so get the new one */
1322 err = apfs_find_primary_link(inode, &primary_name,
1323 &ai->i_parent_id);
1324 }
1325 if (err)
1326 goto fail;
1327
1328 #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
1329 inode->i_ctime = dir->i_ctime;
1330 #else
1331 inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
1332 #endif
1333 /* TODO: defer write of the primary name? */
1334 err = apfs_update_inode(inode, primary_name);
1335 if (err)
1336 apfs_err(sb, "inode update failed for 0x%llx", apfs_ino(inode));
1337
1338 fail:
1339 kfree(primary_name);
1340 primary_name = NULL;
1341 if (err)
1342 __apfs_undo_unlink(dentry);
1343 return err;
1344 }
1345
1346 int apfs_unlink(struct inode *dir, struct dentry *dentry)
1347 {
1348 struct super_block *sb = dir->i_sb;
1349 int err;
1350
1351 err = apfs_transaction_start(sb, APFS_TRANS_DEL);
1352 if (err)
1353 return err;
1354
1355 err = __apfs_unlink(dir, dentry);
1356 if (err)
1357 goto out_abort;
1358
1359 err = apfs_transaction_commit(sb);
1360 if (err)
1361 goto out_undo_unlink;
1362 return 0;
1363
1364 out_undo_unlink:
1365 __apfs_undo_unlink(dentry);
1366 out_abort:
1367 apfs_transaction_abort(sb);
1368 return err;
1369 }
1370
1371 int apfs_rmdir(struct inode *dir, struct dentry *dentry)
1372 {
1373 struct inode *inode = d_inode(dentry);
1374
1375 if (APFS_I(inode)->i_nchildren)
1376 return -ENOTEMPTY;
1377 return apfs_unlink(dir, dentry);
1378 }
1379
1380 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
1381 int apfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1382 struct inode *new_dir, struct dentry *new_dentry,
1383 unsigned int flags)
1384 #elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6)
1385 int apfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
1386 struct dentry *old_dentry, struct inode *new_dir,
1387 struct dentry *new_dentry, unsigned int flags)
1388 #else
1389 int apfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
1390 struct dentry *old_dentry, struct inode *new_dir,
1391 struct dentry *new_dentry, unsigned int flags)
1392 #endif
1393 {
1394 struct super_block *sb = old_dir->i_sb;
1395 struct inode *old_inode = d_inode(old_dentry);
1396 struct inode *new_inode = d_inode(new_dentry);
1397 int err;
1398
1399 if (new_inode && APFS_I(new_inode)->i_nchildren)
1400 return -ENOTEMPTY;
1401
1402 if (flags & ~RENAME_NOREPLACE) /* TODO: support RENAME_EXCHANGE */
1403 return -EINVAL;
1404
1405 err = apfs_transaction_start(sb, APFS_TRANS_REG);
1406 if (err)
1407 return err;
1408
1409 if (new_inode) {
1410 err = __apfs_unlink(new_dir, new_dentry);
1411 if (err) {
1412 apfs_err(sb, "unlink failed for replaced dentry");
1413 goto out_abort;
1414 }
1415 }
1416
1417 err = __apfs_link(old_dentry, new_dentry);
1418 if (err) {
1419 apfs_err(sb, "link failed for new dentry");
1420 goto out_undo_unlink_new;
1421 }
1422
1423 err = __apfs_unlink(old_dir, old_dentry);
1424 if (err) {
1425 apfs_err(sb, "unlink failed for old dentry");
1426 goto out_undo_link;
1427 }
1428
1429 err = apfs_transaction_commit(sb);
1430 if (err)
1431 goto out_undo_unlink_old;
1432 return 0;
1433
1434 out_undo_unlink_old:
1435 __apfs_undo_unlink(old_dentry);
1436 out_undo_link:
1437 __apfs_undo_link(new_dentry, old_inode);
1438 out_undo_unlink_new:
1439 if (new_inode)
1440 __apfs_undo_unlink(new_dentry);
1441 out_abort:
1442 apfs_transaction_abort(sb);
1443 return err;
1444 }
1445
1446 /**
1447 * apfs_any_orphan_ino - Find the inode number for any orphaned regular file
1448 * @sb: filesytem superblock
1449 * @ino_p: on return, the inode number found
1450 *
1451 * Returns 0 on success, or a negative error code in case of failure, which may
1452 * be -ENODATA if there are no orphan files.
1453 */
1454 u64 apfs_any_orphan_ino(struct super_block *sb, u64 *ino_p)
1455 {
1456 struct apfs_sb_info *sbi = APFS_SB(sb);
1457 struct apfs_query *query = NULL;
1458 struct apfs_drec drec = {0};
1459 struct qstr qname = {0};
1460 bool hashed = apfs_is_normalization_insensitive(sb);
1461 bool found = false;
1462 int err;
1463
1464 query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
1465 if (!query)
1466 return -ENOMEM;
1467 apfs_init_drec_key(sb, APFS_PRIV_DIR_INO_NUM, NULL /* name */, 0 /* name_len */, &query->key);
1468 query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT;
1469
1470 while (!found) {
1471 err = apfs_btree_query(sb, &query);
1472 if (err) {
1473 if (err == -ENODATA)
1474 goto out;
1475 apfs_err(sb, "drec query failed for private dir");
1476 goto out;
1477 }
1478 err = apfs_drec_from_query(query, &drec, hashed);
1479 if (err) {
1480 apfs_alert(sb, "bad dentry record in private dir");
1481 goto out;
1482 }
1483
1484 /* These files are deleted immediately by ->evict_inode() */
1485 if (drec.type != DT_REG)
1486 continue;
1487
1488 /*
1489 * Confirm that this is an orphan file, because the official
1490 * reference allows other uses for the private directory.
1491 */
1492 err = apfs_orphan_name(drec.ino, &qname);
1493 if (err)
1494 goto out;
1495 found = strcmp(drec.name, qname.name) == 0;
1496 kfree(qname.name);
1497 qname.name = NULL;
1498 }
1499 *ino_p = drec.ino;
1500
1501 out:
1502 apfs_free_query(query);
1503 query = NULL;
1504 return err;
1505 }