| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
#include <linux/slab.h> |
| 7 |
#include <linux/buffer_head.h> |
| 8 |
#include "apfs.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 61 |
if (name[namelen - 1] != 0) { |
| 62 |
apfs_err(sb, "null termination missing"); |
| 63 |
return -EFSCORRUPTED; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 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; |
| 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) |
| 82 |
drec->type = DT_UNKNOWN; |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 ); |
| 108 |
if (!query) |
| 109 |
return ERR_PTR(-ENOMEM); |
| 110 |
apfs_init_drec_key(sb, cnid, child->name, child->len, &query->key); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 194 |
if (!dir_emit_dots(file, ctx)) |
| 195 |
goto out; |
| 196 |
|
| 197 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 198 |
if (!query) { |
| 199 |
err = -ENOMEM; |
| 200 |
goto out; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
apfs_init_drec_key(sb, cnid, NULL , 0 , &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 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
err = apfs_btree_query(sb, &query); |
| 218 |
if (err == -ENODATA) { |
| 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 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 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; |
| 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 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 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; |
| 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 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 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 |
|
| 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); |
| 344 |
|
| 345 |
if (!sibling_id) |
| 346 |
return val_len; |
| 347 |
|
| 348 |
|
| 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; |
| 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 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 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 ); |
| 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 |
|
| 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 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 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; |
| 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 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 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 ); |
| 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 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 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 ); |
| 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 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 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 |
|
| 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 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 614 |
|
| 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 ); |
| 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 |
|
| 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 |
|
| 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 ); |
| 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 ); |
| 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 ); |
| 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 ); |
| 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 )); |
| 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 ); |
| 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 ); |
| 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 ); |
| 767 |
} |
| 768 |
|
| 769 |
#endif |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 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 |
|
| 794 |
apfs_free_query(query); |
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 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 |
|
| 817 |
|
| 818 |
|
| 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 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 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 ); |
| 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 |
|
| 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 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 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 ); |
| 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 |
|
| 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 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 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 |
|
| 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 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
static inline void apfs_undo_delete_dentry(struct dentry *dentry) |
| 1039 |
{ |
| 1040 |
struct inode *parent = d_inode(dentry->d_parent); |
| 1041 |
|
| 1042 |
|
| 1043 |
++APFS_I(parent)->i_nchildren; |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 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 |
|
| 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 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 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 ); |
| 1106 |
if (!query) |
| 1107 |
return -ENOMEM; |
| 1108 |
apfs_init_sibling_link_key(apfs_ino(inode), 0 , &query->key); |
| 1109 |
query->flags |= APFS_QUERY_CAT | APFS_QUERY_ANY_NUMBER | APFS_QUERY_EXACT; |
| 1110 |
|
| 1111 |
|
| 1112 |
*name = NULL; |
| 1113 |
while (1) { |
| 1114 |
err = apfs_btree_query(sb, &query); |
| 1115 |
if (err == -ENODATA) |
| 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; |
| 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 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
static int apfs_orphan_name(u64 ino, struct qstr *qname) |
| 1147 |
{ |
| 1148 |
int max_len; |
| 1149 |
char *name; |
| 1150 |
|
| 1151 |
|
| 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 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 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 ); |
| 1179 |
if (err) { |
| 1180 |
apfs_err(sb, "failed to create drec for ino 0x%llx", apfs_ino(inode)); |
| 1181 |
goto fail; |
| 1182 |
} |
| 1183 |
|
| 1184 |
|
| 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 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 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 |
|
| 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 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
static void __apfs_undo_unlink(struct dentry *dentry) |
| 1255 |
{ |
| 1256 |
struct inode *inode = d_inode(dentry); |
| 1257 |
|
| 1258 |
|
| 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 |
|
| 1268 |
|
| 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 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 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 |
|
| 1317 |
err = apfs_create_orphan_link(inode); |
| 1318 |
|
| 1319 |
apfs_vol_filecnt_dec(inode); |
| 1320 |
} else { |
| 1321 |
|
| 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 |
|
| 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) |
| 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 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 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 ); |
| 1465 |
if (!query) |
| 1466 |
return -ENOMEM; |
| 1467 |
apfs_init_drec_key(sb, APFS_PRIV_DIR_INO_NUM, NULL , 0 , &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 |
|
| 1485 |
if (drec.type != DT_REG) |
| 1486 |
continue; |
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 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 |
} |