| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
#include <linux/buffer_head.h> |
| 7 |
#include <linux/slab.h> |
| 8 |
#include <linux/blk_types.h> |
| 9 |
#include "apfs.h" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
static inline bool apfs_ext_is_hole(struct apfs_file_extent *extent) |
| 16 |
{ |
| 17 |
return extent->phys_block_num == 0; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
static inline u64 apfs_size_to_blocks(struct super_block *sb, u64 size) |
| 28 |
{ |
| 29 |
return (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
int apfs_extent_from_query(struct apfs_query *query, |
| 42 |
struct apfs_file_extent *extent) |
| 43 |
{ |
| 44 |
struct super_block *sb = query->node->object.sb; |
| 45 |
char *raw = query->node->object.data; |
| 46 |
u64 ext_len; |
| 47 |
|
| 48 |
if (!apfs_is_sealed(sb)) { |
| 49 |
struct apfs_file_extent_val *ext = NULL; |
| 50 |
struct apfs_file_extent_key *ext_key = NULL; |
| 51 |
|
| 52 |
if (query->len != sizeof(*ext) || query->key_len != sizeof(*ext_key)) { |
| 53 |
apfs_err(sb, "bad length of key (%d) or value (%d)", query->key_len, query->len); |
| 54 |
return -EFSCORRUPTED; |
| 55 |
} |
| 56 |
|
| 57 |
ext = (struct apfs_file_extent_val *)(raw + query->off); |
| 58 |
ext_key = (struct apfs_file_extent_key *)(raw + query->key_off); |
| 59 |
ext_len = le64_to_cpu(ext->len_and_flags) & APFS_FILE_EXTENT_LEN_MASK; |
| 60 |
|
| 61 |
extent->logical_addr = le64_to_cpu(ext_key->logical_addr); |
| 62 |
extent->phys_block_num = le64_to_cpu(ext->phys_block_num); |
| 63 |
extent->crypto_id = le64_to_cpu(ext->crypto_id); |
| 64 |
} else { |
| 65 |
struct apfs_fext_tree_val *fext_val = NULL; |
| 66 |
struct apfs_fext_tree_key *fext_key = NULL; |
| 67 |
|
| 68 |
if (query->len != sizeof(*fext_val) || query->key_len != sizeof(*fext_key)) { |
| 69 |
apfs_err(sb, "bad length of sealed key (%d) or value (%d)", query->key_len, query->len); |
| 70 |
return -EFSCORRUPTED; |
| 71 |
} |
| 72 |
|
| 73 |
fext_val = (struct apfs_fext_tree_val *)(raw + query->off); |
| 74 |
fext_key = (struct apfs_fext_tree_key *)(raw + query->key_off); |
| 75 |
ext_len = le64_to_cpu(fext_val->len_and_flags) & APFS_FILE_EXTENT_LEN_MASK; |
| 76 |
|
| 77 |
extent->logical_addr = le64_to_cpu(fext_key->logical_addr); |
| 78 |
extent->phys_block_num = le64_to_cpu(fext_val->phys_block_num); |
| 79 |
extent->crypto_id = 0; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
if (ext_len & (sb->s_blocksize - 1)) { |
| 84 |
apfs_err(sb, "invalid length (0x%llx)", ext_len); |
| 85 |
return -EFSCORRUPTED; |
| 86 |
} |
| 87 |
extent->len = ext_len; |
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
static int apfs_extent_read(struct apfs_dstream_info *dstream, sector_t dsblock, |
| 101 |
struct apfs_file_extent *extent) |
| 102 |
{ |
| 103 |
struct super_block *sb = dstream->ds_sb; |
| 104 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 105 |
struct apfs_superblock *vsb_raw = sbi->s_vsb_raw; |
| 106 |
struct apfs_key key; |
| 107 |
struct apfs_query *query; |
| 108 |
struct apfs_file_extent *cache = &dstream->ds_cached_ext; |
| 109 |
u64 iaddr = dsblock << sb->s_blocksize_bits; |
| 110 |
struct apfs_node *root = NULL; |
| 111 |
int ret = 0; |
| 112 |
|
| 113 |
spin_lock(&dstream->ds_ext_lock); |
| 114 |
if (iaddr >= cache->logical_addr && |
| 115 |
iaddr < cache->logical_addr + cache->len) { |
| 116 |
*extent = *cache; |
| 117 |
spin_unlock(&dstream->ds_ext_lock); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
spin_unlock(&dstream->ds_ext_lock); |
| 121 |
|
| 122 |
|
| 123 |
if (!apfs_is_sealed(sb)) { |
| 124 |
apfs_init_file_extent_key(dstream->ds_id, iaddr, &key); |
| 125 |
root = sbi->s_cat_root; |
| 126 |
} else { |
| 127 |
apfs_init_fext_key(dstream->ds_id, iaddr, &key); |
| 128 |
root = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_fext_tree_oid), APFS_OBJ_PHYSICAL, false ); |
| 129 |
if (IS_ERR(root)) { |
| 130 |
apfs_err(sb, "failed to read fext root 0x%llx", le64_to_cpu(vsb_raw->apfs_fext_tree_oid)); |
| 131 |
return PTR_ERR(root); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
query = apfs_alloc_query(root, NULL ); |
| 136 |
if (!query) { |
| 137 |
ret = -ENOMEM; |
| 138 |
goto done; |
| 139 |
} |
| 140 |
query->key = key; |
| 141 |
query->flags = apfs_is_sealed(sb) ? APFS_QUERY_FEXT : APFS_QUERY_CAT; |
| 142 |
|
| 143 |
ret = apfs_btree_query(sb, &query); |
| 144 |
if (ret) { |
| 145 |
apfs_err(sb, "query failed for id 0x%llx, addr 0x%llx", dstream->ds_id, iaddr); |
| 146 |
if (ret == -ENODATA) |
| 147 |
ret = -EFSCORRUPTED; |
| 148 |
goto done; |
| 149 |
} |
| 150 |
|
| 151 |
ret = apfs_extent_from_query(query, extent); |
| 152 |
if (ret) { |
| 153 |
apfs_err(sb, "bad extent record for dstream 0x%llx", dstream->ds_id); |
| 154 |
goto done; |
| 155 |
} |
| 156 |
if (iaddr < extent->logical_addr || iaddr >= extent->logical_addr + extent->len) { |
| 157 |
apfs_err(sb, "no extent for addr 0x%llx in dstream 0x%llx", iaddr, dstream->ds_id); |
| 158 |
ret = -EFSCORRUPTED; |
| 159 |
goto done; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
if (!dstream->ds_ext_dirty) { |
| 167 |
spin_lock(&dstream->ds_ext_lock); |
| 168 |
*cache = *extent; |
| 169 |
spin_unlock(&dstream->ds_ext_lock); |
| 170 |
} |
| 171 |
|
| 172 |
done: |
| 173 |
apfs_free_query(query); |
| 174 |
if (apfs_is_sealed(sb)) |
| 175 |
apfs_node_free(root); |
| 176 |
return ret; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
int apfs_logic_to_phys_bno(struct apfs_dstream_info *dstream, sector_t dsblock, u64 *bno) |
| 188 |
{ |
| 189 |
struct super_block *sb = dstream->ds_sb; |
| 190 |
struct apfs_file_extent ext; |
| 191 |
u64 blk_off; |
| 192 |
int ret; |
| 193 |
|
| 194 |
ret = apfs_extent_read(dstream, dsblock, &ext); |
| 195 |
if (ret) |
| 196 |
return ret; |
| 197 |
|
| 198 |
if (apfs_ext_is_hole(&ext)) { |
| 199 |
*bno = 0; |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
blk_off = dsblock - (ext.logical_addr >> sb->s_blocksize_bits); |
| 205 |
*bno = ext.phys_block_num + blk_off; |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
int __apfs_get_block(struct apfs_dstream_info *dstream, sector_t dsblock, |
| 211 |
struct buffer_head *bh_result, int create) |
| 212 |
{ |
| 213 |
struct super_block *sb = dstream->ds_sb; |
| 214 |
struct apfs_file_extent ext; |
| 215 |
u64 blk_off, bno, map_len; |
| 216 |
int ret; |
| 217 |
|
| 218 |
ASSERT(!create); |
| 219 |
|
| 220 |
if (dsblock >= apfs_size_to_blocks(sb, dstream->ds_size)) |
| 221 |
return 0; |
| 222 |
|
| 223 |
ret = apfs_extent_read(dstream, dsblock, &ext); |
| 224 |
if (ret) { |
| 225 |
apfs_err(sb, "extent read failed"); |
| 226 |
return ret; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
blk_off = dsblock - (ext.logical_addr >> sb->s_blocksize_bits); |
| 231 |
|
| 232 |
|
| 233 |
map_len = ext.len - (blk_off << sb->s_blocksize_bits); |
| 234 |
if (bh_result->b_size > map_len) |
| 235 |
bh_result->b_size = map_len; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
map_len = bh_result->b_size; |
| 242 |
|
| 243 |
if (!apfs_ext_is_hole(&ext)) { |
| 244 |
|
| 245 |
bno = ext.phys_block_num + blk_off; |
| 246 |
apfs_map_bh(bh_result, sb, bno); |
| 247 |
} |
| 248 |
bh_result->b_size = map_len; |
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
int apfs_get_block(struct inode *inode, sector_t iblock, |
| 253 |
struct buffer_head *bh_result, int create) |
| 254 |
{ |
| 255 |
struct apfs_nxsb_info *nxi = APFS_NXI(inode->i_sb); |
| 256 |
struct apfs_inode_info *ai = APFS_I(inode); |
| 257 |
int ret; |
| 258 |
|
| 259 |
down_read(&nxi->nx_big_sem); |
| 260 |
ret = __apfs_get_block(&ai->i_dstream, iblock, bh_result, create); |
| 261 |
up_read(&nxi->nx_big_sem); |
| 262 |
return ret; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static inline void apfs_set_extent_length(struct apfs_file_extent_val *ext, u64 len) |
| 274 |
{ |
| 275 |
u64 len_and_flags = le64_to_cpu(ext->len_and_flags); |
| 276 |
u64 flags = len_and_flags & APFS_FILE_EXTENT_FLAG_MASK; |
| 277 |
|
| 278 |
ext->len_and_flags = cpu_to_le64(flags | len); |
| 279 |
} |
| 280 |
|
| 281 |
static int apfs_range_put_reference(struct super_block *sb, u64 paddr, u64 length); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
static int apfs_shrink_extent_head(struct apfs_query *query, struct apfs_dstream_info *dstream, u64 start) |
| 293 |
{ |
| 294 |
struct super_block *sb = query->node->object.sb; |
| 295 |
struct apfs_file_extent_key key; |
| 296 |
struct apfs_file_extent_val val; |
| 297 |
struct apfs_file_extent extent; |
| 298 |
u64 new_len, head_len; |
| 299 |
void *raw = NULL; |
| 300 |
int err = 0; |
| 301 |
|
| 302 |
err = apfs_extent_from_query(query, &extent); |
| 303 |
if (err) { |
| 304 |
apfs_err(sb, "bad extent record for dstream 0x%llx", dstream->ds_id); |
| 305 |
return err; |
| 306 |
} |
| 307 |
raw = query->node->object.data; |
| 308 |
key = *(struct apfs_file_extent_key *)(raw + query->key_off); |
| 309 |
val = *(struct apfs_file_extent_val *)(raw + query->off); |
| 310 |
|
| 311 |
new_len = extent.logical_addr + extent.len - start; |
| 312 |
head_len = extent.len - new_len; |
| 313 |
|
| 314 |
|
| 315 |
if (!apfs_ext_is_hole(&extent)) { |
| 316 |
err = apfs_range_put_reference(sb, extent.phys_block_num, head_len); |
| 317 |
if (err) { |
| 318 |
apfs_err(sb, "failed to put range 0x%llx-0x%llx", extent.phys_block_num, head_len); |
| 319 |
return err; |
| 320 |
} |
| 321 |
} else { |
| 322 |
dstream->ds_sparse_bytes -= head_len; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
key.logical_addr = cpu_to_le64(start); |
| 327 |
apfs_set_extent_length(&val, new_len); |
| 328 |
if (!apfs_ext_is_hole(&extent)) |
| 329 |
le64_add_cpu(&val.phys_block_num, head_len >> sb->s_blocksize_bits); |
| 330 |
return apfs_btree_replace(query, &key, sizeof(key), &val, sizeof(val)); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static int apfs_shrink_extent_tail(struct apfs_query *query, struct apfs_dstream_info *dstream, u64 end) |
| 343 |
{ |
| 344 |
struct super_block *sb = query->node->object.sb; |
| 345 |
struct apfs_file_extent_val *val; |
| 346 |
struct apfs_file_extent extent; |
| 347 |
u64 new_len, new_blkcount, tail_len; |
| 348 |
void *raw; |
| 349 |
int err = 0; |
| 350 |
|
| 351 |
ASSERT((end & (sb->s_blocksize - 1)) == 0); |
| 352 |
|
| 353 |
err = apfs_query_join_transaction(query); |
| 354 |
if (err) { |
| 355 |
apfs_err(sb, "query join failed"); |
| 356 |
return err; |
| 357 |
} |
| 358 |
raw = query->node->object.data; |
| 359 |
|
| 360 |
err = apfs_extent_from_query(query, &extent); |
| 361 |
if (err) { |
| 362 |
apfs_err(sb, "bad extent record for dstream 0x%llx", dstream->ds_id); |
| 363 |
return err; |
| 364 |
} |
| 365 |
val = raw + query->off; |
| 366 |
|
| 367 |
new_len = end - extent.logical_addr; |
| 368 |
new_blkcount = new_len >> sb->s_blocksize_bits; |
| 369 |
tail_len = extent.len - new_len; |
| 370 |
|
| 371 |
|
| 372 |
if (!apfs_ext_is_hole(&extent)) { |
| 373 |
err = apfs_range_put_reference(sb, extent.phys_block_num + new_blkcount, tail_len); |
| 374 |
if (err) { |
| 375 |
apfs_err(sb, "failed to put range 0x%llx-0x%llx", extent.phys_block_num + new_blkcount, tail_len); |
| 376 |
return err; |
| 377 |
} |
| 378 |
} else { |
| 379 |
dstream->ds_sparse_bytes -= tail_len; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
apfs_set_extent_length(val, new_len); |
| 384 |
return err; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
static inline bool apfs_query_found_extent(struct apfs_query *query) |
| 392 |
{ |
| 393 |
void *raw = query->node->object.data; |
| 394 |
struct apfs_key_header *hdr; |
| 395 |
|
| 396 |
if (query->key_len < sizeof(*hdr)) |
| 397 |
return false; |
| 398 |
hdr = raw + query->key_off; |
| 399 |
|
| 400 |
if (apfs_cat_type(hdr) != APFS_TYPE_FILE_EXTENT) |
| 401 |
return false; |
| 402 |
if (apfs_cat_cnid(hdr) != query->key.id) |
| 403 |
return false; |
| 404 |
return true; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
static int apfs_update_tail_extent(struct apfs_dstream_info *dstream, const struct apfs_file_extent *extent) |
| 416 |
{ |
| 417 |
struct super_block *sb = dstream->ds_sb; |
| 418 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 419 |
struct apfs_query *query; |
| 420 |
struct apfs_file_extent_key raw_key; |
| 421 |
struct apfs_file_extent_val raw_val; |
| 422 |
u64 extent_id = dstream->ds_id; |
| 423 |
int ret; |
| 424 |
u64 new_crypto; |
| 425 |
|
| 426 |
apfs_key_set_hdr(APFS_TYPE_FILE_EXTENT, extent_id, &raw_key); |
| 427 |
raw_key.logical_addr = cpu_to_le64(extent->logical_addr); |
| 428 |
raw_val.len_and_flags = cpu_to_le64(extent->len); |
| 429 |
raw_val.phys_block_num = cpu_to_le64(extent->phys_block_num); |
| 430 |
if (apfs_vol_is_encrypted(sb)) |
| 431 |
new_crypto = extent_id; |
| 432 |
else |
| 433 |
new_crypto = 0; |
| 434 |
raw_val.crypto_id = cpu_to_le64(new_crypto); |
| 435 |
|
| 436 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 437 |
if (!query) |
| 438 |
return -ENOMEM; |
| 439 |
|
| 440 |
apfs_init_file_extent_key(extent_id, -1, &query->key); |
| 441 |
query->flags = APFS_QUERY_CAT; |
| 442 |
|
| 443 |
ret = apfs_btree_query(sb, &query); |
| 444 |
if (ret && ret != -ENODATA) { |
| 445 |
apfs_err(sb, "query failed for last extent of id 0x%llx", extent_id); |
| 446 |
goto out; |
| 447 |
} |
| 448 |
|
| 449 |
if (ret == -ENODATA || !apfs_query_found_extent(query)) { |
| 450 |
|
| 451 |
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 452 |
if (ret) { |
| 453 |
apfs_err(sb, "insertion failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 454 |
goto out; |
| 455 |
} |
| 456 |
} else { |
| 457 |
struct apfs_file_extent tail; |
| 458 |
|
| 459 |
ret = apfs_extent_from_query(query, &tail); |
| 460 |
if (ret) { |
| 461 |
apfs_err(sb, "bad extent record for dstream 0x%llx", dstream->ds_id); |
| 462 |
goto out; |
| 463 |
} |
| 464 |
|
| 465 |
if (tail.logical_addr > extent->logical_addr) { |
| 466 |
apfs_alert(sb, "extent is not tail - bug!"); |
| 467 |
ret = -EOPNOTSUPP; |
| 468 |
goto out; |
| 469 |
} else if (tail.logical_addr == extent->logical_addr) { |
| 470 |
ret = apfs_btree_replace(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 471 |
if (ret) { |
| 472 |
apfs_err(sb, "update failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 473 |
goto out; |
| 474 |
} |
| 475 |
if (apfs_ext_is_hole(&tail)) { |
| 476 |
dstream->ds_sparse_bytes -= tail.len; |
| 477 |
} else if (tail.phys_block_num != extent->phys_block_num) { |
| 478 |
ret = apfs_range_put_reference(sb, tail.phys_block_num, tail.len); |
| 479 |
if (ret) { |
| 480 |
apfs_err(sb, "failed to put range 0x%llx-0x%llx", tail.phys_block_num, tail.len); |
| 481 |
goto out; |
| 482 |
} |
| 483 |
} |
| 484 |
if (new_crypto == tail.crypto_id) |
| 485 |
goto out; |
| 486 |
ret = apfs_crypto_adj_refcnt(sb, tail.crypto_id, -1); |
| 487 |
if (ret) { |
| 488 |
apfs_err(sb, "failed to put crypto id 0x%llx", tail.crypto_id); |
| 489 |
goto out; |
| 490 |
} |
| 491 |
} else { |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
if (extent->logical_addr < tail.logical_addr + tail.len) { |
| 500 |
ret = apfs_shrink_extent_tail(query, dstream, extent->logical_addr); |
| 501 |
if (ret) { |
| 502 |
apfs_err(sb, "failed to shrink tail of dstream 0x%llx", extent_id); |
| 503 |
goto out; |
| 504 |
} |
| 505 |
} |
| 506 |
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 507 |
if (ret) { |
| 508 |
apfs_err(sb, "insertion failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 509 |
goto out; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
ret = apfs_crypto_adj_refcnt(sb, new_crypto, 1); |
| 515 |
if (ret) |
| 516 |
apfs_err(sb, "failed to take crypto id 0x%llx", new_crypto); |
| 517 |
|
| 518 |
out: |
| 519 |
apfs_free_query(query); |
| 520 |
return ret; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static int apfs_split_extent(struct apfs_query *query, u64 div) |
| 531 |
{ |
| 532 |
struct super_block *sb = query->node->object.sb; |
| 533 |
struct apfs_file_extent_val *val1; |
| 534 |
struct apfs_file_extent_key key2; |
| 535 |
struct apfs_file_extent_val val2; |
| 536 |
struct apfs_file_extent extent; |
| 537 |
u64 len1, len2, blkcount1; |
| 538 |
void *raw; |
| 539 |
int err = 0; |
| 540 |
|
| 541 |
err = apfs_query_join_transaction(query); |
| 542 |
if (err) { |
| 543 |
apfs_err(sb, "query join failed"); |
| 544 |
return err; |
| 545 |
} |
| 546 |
raw = query->node->object.data; |
| 547 |
|
| 548 |
err = apfs_extent_from_query(query, &extent); |
| 549 |
if (err) { |
| 550 |
apfs_err(sb, "bad extent record"); |
| 551 |
return err; |
| 552 |
} |
| 553 |
val1 = raw + query->off; |
| 554 |
val2 = *(struct apfs_file_extent_val *)(raw + query->off); |
| 555 |
key2 = *(struct apfs_file_extent_key *)(raw + query->key_off); |
| 556 |
|
| 557 |
len1 = div - extent.logical_addr; |
| 558 |
blkcount1 = len1 >> sb->s_blocksize_bits; |
| 559 |
len2 = extent.len - len1; |
| 560 |
|
| 561 |
|
| 562 |
apfs_set_extent_length(val1, len1); |
| 563 |
|
| 564 |
|
| 565 |
key2.logical_addr = cpu_to_le64(div); |
| 566 |
if (!apfs_ext_is_hole(&extent)) |
| 567 |
val2.phys_block_num = cpu_to_le64(extent.phys_block_num + blkcount1); |
| 568 |
apfs_set_extent_length(&val2, len2); |
| 569 |
err = apfs_btree_insert(query, &key2, sizeof(key2), &val2, sizeof(val2)); |
| 570 |
if (err) { |
| 571 |
apfs_err(sb, "insertion failed in division 0x%llx", div); |
| 572 |
return err; |
| 573 |
} |
| 574 |
|
| 575 |
return apfs_crypto_adj_refcnt(sb, extent.crypto_id, 1); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
static int apfs_update_mid_extent(struct apfs_dstream_info *dstream, const struct apfs_file_extent *extent) |
| 587 |
{ |
| 588 |
struct super_block *sb = dstream->ds_sb; |
| 589 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 590 |
struct apfs_key key; |
| 591 |
struct apfs_query *query; |
| 592 |
struct apfs_file_extent_key raw_key; |
| 593 |
struct apfs_file_extent_val raw_val; |
| 594 |
struct apfs_file_extent prev_ext; |
| 595 |
u64 extent_id = dstream->ds_id; |
| 596 |
u64 prev_crypto, new_crypto; |
| 597 |
u64 prev_start, prev_end; |
| 598 |
bool second_run = false; |
| 599 |
int ret; |
| 600 |
|
| 601 |
apfs_key_set_hdr(APFS_TYPE_FILE_EXTENT, extent_id, &raw_key); |
| 602 |
raw_key.logical_addr = cpu_to_le64(extent->logical_addr); |
| 603 |
raw_val.len_and_flags = cpu_to_le64(extent->len); |
| 604 |
raw_val.phys_block_num = cpu_to_le64(extent->phys_block_num); |
| 605 |
if (apfs_vol_is_encrypted(sb)) |
| 606 |
new_crypto = extent_id; |
| 607 |
else |
| 608 |
new_crypto = 0; |
| 609 |
raw_val.crypto_id = cpu_to_le64(new_crypto); |
| 610 |
|
| 611 |
apfs_init_file_extent_key(extent_id, extent->logical_addr, &key); |
| 612 |
|
| 613 |
search_and_insert: |
| 614 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 615 |
if (!query) |
| 616 |
return -ENOMEM; |
| 617 |
query->key = key; |
| 618 |
query->flags = APFS_QUERY_CAT; |
| 619 |
|
| 620 |
ret = apfs_btree_query(sb, &query); |
| 621 |
if (ret && ret != -ENODATA) { |
| 622 |
apfs_err(sb, "query failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 623 |
goto out; |
| 624 |
} |
| 625 |
|
| 626 |
if (ret == -ENODATA || !apfs_query_found_extent(query)) { |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
if (!second_run) { |
| 632 |
apfs_err(sb, "missing extent on dstream 0x%llx", extent_id); |
| 633 |
ret = -EFSCORRUPTED; |
| 634 |
} else { |
| 635 |
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 636 |
if (ret) |
| 637 |
apfs_err(sb, "insertion failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 638 |
} |
| 639 |
goto out; |
| 640 |
} |
| 641 |
|
| 642 |
if (apfs_extent_from_query(query, &prev_ext)) { |
| 643 |
apfs_err(sb, "bad mid extent record on dstream 0x%llx", extent_id); |
| 644 |
ret = -EFSCORRUPTED; |
| 645 |
goto out; |
| 646 |
} |
| 647 |
prev_crypto = prev_ext.crypto_id; |
| 648 |
prev_start = prev_ext.logical_addr; |
| 649 |
prev_end = prev_ext.logical_addr + prev_ext.len; |
| 650 |
|
| 651 |
if (prev_end == extent->logical_addr && second_run) { |
| 652 |
|
| 653 |
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 654 |
if (ret) { |
| 655 |
apfs_err(sb, "insertion failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 656 |
goto out; |
| 657 |
} |
| 658 |
} else if (prev_start == extent->logical_addr && prev_ext.len == extent->len) { |
| 659 |
|
| 660 |
ret = apfs_btree_replace(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 661 |
if (ret) { |
| 662 |
apfs_err(sb, "update failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 663 |
goto out; |
| 664 |
} |
| 665 |
if (apfs_ext_is_hole(&prev_ext)) { |
| 666 |
dstream->ds_sparse_bytes -= prev_ext.len; |
| 667 |
} else if (prev_ext.phys_block_num != extent->phys_block_num) { |
| 668 |
ret = apfs_range_put_reference(sb, prev_ext.phys_block_num, prev_ext.len); |
| 669 |
if (ret) { |
| 670 |
apfs_err(sb, "failed to put range 0x%llx-0x%llx", prev_ext.phys_block_num, prev_ext.len); |
| 671 |
goto out; |
| 672 |
} |
| 673 |
} |
| 674 |
ret = apfs_crypto_adj_refcnt(sb, prev_crypto, -1); |
| 675 |
if (ret) { |
| 676 |
apfs_err(sb, "failed to put crypto id 0x%llx", prev_crypto); |
| 677 |
goto out; |
| 678 |
} |
| 679 |
} else if (prev_start == extent->logical_addr) { |
| 680 |
|
| 681 |
if (second_run) { |
| 682 |
|
| 683 |
apfs_alert(sb, "recursion shrinking extent head for dstream 0x%llx", extent_id); |
| 684 |
ret = -EFSCORRUPTED; |
| 685 |
goto out; |
| 686 |
} |
| 687 |
ret = apfs_shrink_extent_head(query, dstream, extent->logical_addr + extent->len); |
| 688 |
if (ret) { |
| 689 |
apfs_err(sb, "failed to shrink extent in dstream 0x%llx", extent_id); |
| 690 |
goto out; |
| 691 |
} |
| 692 |
|
| 693 |
apfs_free_query(query); |
| 694 |
second_run = true; |
| 695 |
goto search_and_insert; |
| 696 |
} else if (prev_end == extent->logical_addr + extent->len) { |
| 697 |
|
| 698 |
ret = apfs_shrink_extent_tail(query, dstream, extent->logical_addr); |
| 699 |
if (ret) { |
| 700 |
apfs_err(sb, "failed to shrink extent in dstream 0x%llx", extent_id); |
| 701 |
goto out; |
| 702 |
} |
| 703 |
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 704 |
if (ret) { |
| 705 |
apfs_err(sb, "insertion failed for id 0x%llx, addr 0x%llx", extent_id, extent->logical_addr); |
| 706 |
goto out; |
| 707 |
} |
| 708 |
} else if (prev_start < extent->logical_addr && prev_end > extent->logical_addr + extent->len) { |
| 709 |
|
| 710 |
if (second_run) { |
| 711 |
|
| 712 |
apfs_alert(sb, "recursion when splitting extents for dstream 0x%llx", extent_id); |
| 713 |
ret = -EFSCORRUPTED; |
| 714 |
goto out; |
| 715 |
} |
| 716 |
ret = apfs_split_extent(query, extent->logical_addr + extent->len); |
| 717 |
if (ret) { |
| 718 |
apfs_err(sb, "failed to split extent in dstream 0x%llx", extent_id); |
| 719 |
goto out; |
| 720 |
} |
| 721 |
|
| 722 |
apfs_free_query(query); |
| 723 |
second_run = true; |
| 724 |
goto search_and_insert; |
| 725 |
} else { |
| 726 |
|
| 727 |
apfs_alert(sb, "strange extents for dstream 0x%llx", extent_id); |
| 728 |
ret = -EFSCORRUPTED; |
| 729 |
goto out; |
| 730 |
} |
| 731 |
|
| 732 |
ret = apfs_crypto_adj_refcnt(sb, new_crypto, 1); |
| 733 |
if (ret) |
| 734 |
apfs_err(sb, "failed to take crypto id 0x%llx", new_crypto); |
| 735 |
|
| 736 |
out: |
| 737 |
apfs_free_query(query); |
| 738 |
return ret; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
static int apfs_update_extent(struct apfs_dstream_info *dstream, const struct apfs_file_extent *extent) |
| 750 |
{ |
| 751 |
struct super_block *sb = dstream->ds_sb; |
| 752 |
|
| 753 |
if (extent->logical_addr + extent->len >= dstream->ds_size) |
| 754 |
return apfs_update_tail_extent(dstream, extent); |
| 755 |
if (extent->len > sb->s_blocksize) { |
| 756 |
apfs_err(sb, "can't create mid extents of length 0x%llx", extent->len); |
| 757 |
return -EOPNOTSUPP; |
| 758 |
} |
| 759 |
return apfs_update_mid_extent(dstream, extent); |
| 760 |
} |
| 761 |
|
| 762 |
static int apfs_extend_phys_extent(struct apfs_query *query, u64 bno, u64 blkcnt, u64 dstream_id) |
| 763 |
{ |
| 764 |
struct apfs_phys_ext_key raw_key; |
| 765 |
struct apfs_phys_ext_val raw_val; |
| 766 |
u64 kind = (u64)APFS_KIND_NEW << APFS_PEXT_KIND_SHIFT; |
| 767 |
|
| 768 |
apfs_key_set_hdr(APFS_TYPE_EXTENT, bno, &raw_key); |
| 769 |
raw_val.len_and_kind = cpu_to_le64(kind | blkcnt); |
| 770 |
raw_val.owning_obj_id = cpu_to_le64(dstream_id); |
| 771 |
raw_val.refcnt = cpu_to_le32(1); |
| 772 |
return apfs_btree_replace(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 773 |
} |
| 774 |
|
| 775 |
static int apfs_insert_new_phys_extent(struct apfs_query *query, u64 bno, u64 blkcnt, u64 dstream_id) |
| 776 |
{ |
| 777 |
struct apfs_phys_ext_key raw_key; |
| 778 |
struct apfs_phys_ext_val raw_val; |
| 779 |
u64 kind = (u64)APFS_KIND_NEW << APFS_PEXT_KIND_SHIFT; |
| 780 |
|
| 781 |
apfs_key_set_hdr(APFS_TYPE_EXTENT, bno, &raw_key); |
| 782 |
raw_val.len_and_kind = cpu_to_le64(kind | blkcnt); |
| 783 |
raw_val.owning_obj_id = cpu_to_le64(dstream_id); |
| 784 |
raw_val.refcnt = cpu_to_le32(1); |
| 785 |
return apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 786 |
} |
| 787 |
|
| 788 |
static int apfs_phys_ext_from_query(struct apfs_query *query, struct apfs_phys_extent *pext); |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
static int apfs_insert_phys_extent(struct apfs_dstream_info *dstream, const struct apfs_file_extent *extent) |
| 799 |
{ |
| 800 |
struct super_block *sb = dstream->ds_sb; |
| 801 |
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 802 |
struct apfs_node *extref_root; |
| 803 |
struct apfs_query *query = NULL; |
| 804 |
struct apfs_phys_extent pext; |
| 805 |
u64 blkcnt = extent->len >> sb->s_blocksize_bits; |
| 806 |
u64 last_bno, new_base, new_blkcnt; |
| 807 |
int ret; |
| 808 |
|
| 809 |
extref_root = apfs_read_node(sb, |
| 810 |
le64_to_cpu(vsb_raw->apfs_extentref_tree_oid), |
| 811 |
APFS_OBJ_PHYSICAL, true ); |
| 812 |
if (IS_ERR(extref_root)) { |
| 813 |
apfs_err(sb, "failed to read extref root 0x%llx", le64_to_cpu(vsb_raw->apfs_extentref_tree_oid)); |
| 814 |
return PTR_ERR(extref_root); |
| 815 |
} |
| 816 |
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o); |
| 817 |
vsb_raw->apfs_extentref_tree_oid = cpu_to_le64(extref_root->object.oid); |
| 818 |
|
| 819 |
query = apfs_alloc_query(extref_root, NULL ); |
| 820 |
if (!query) { |
| 821 |
ret = -ENOMEM; |
| 822 |
goto out; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
last_bno = extent->phys_block_num + blkcnt - 1; |
| 831 |
apfs_init_extent_key(last_bno, &query->key); |
| 832 |
query->flags = APFS_QUERY_EXTENTREF; |
| 833 |
|
| 834 |
ret = apfs_btree_query(sb, &query); |
| 835 |
if (ret && ret != -ENODATA) { |
| 836 |
apfs_err(sb, "query failed for paddr 0x%llx", last_bno); |
| 837 |
goto out; |
| 838 |
} |
| 839 |
|
| 840 |
if (ret == -ENODATA) { |
| 841 |
|
| 842 |
ret = apfs_insert_new_phys_extent(query, extent->phys_block_num, blkcnt, dstream->ds_id); |
| 843 |
if (ret) |
| 844 |
apfs_err(sb, "insertion failed for paddr 0x%llx", extent->phys_block_num); |
| 845 |
goto out; |
| 846 |
} |
| 847 |
|
| 848 |
ret = apfs_phys_ext_from_query(query, &pext); |
| 849 |
if (ret) { |
| 850 |
apfs_err(sb, "bad pext record for bno 0x%llx", last_bno); |
| 851 |
goto out; |
| 852 |
} |
| 853 |
if (pext.bno + pext.blkcount <= extent->phys_block_num) { |
| 854 |
|
| 855 |
ret = apfs_insert_new_phys_extent(query, extent->phys_block_num, blkcnt, dstream->ds_id); |
| 856 |
if (ret) |
| 857 |
apfs_err(sb, "insertion failed for paddr 0x%llx", extent->phys_block_num); |
| 858 |
goto out; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
if (pext.bno + pext.blkcount >= extent->phys_block_num + blkcnt) { |
| 866 |
apfs_err(sb, "dirty cache tail covered by existing physical extent 0x%llx-0x%llx", pext.bno, pext.blkcount); |
| 867 |
ret = -EFSCORRUPTED; |
| 868 |
goto out; |
| 869 |
} |
| 870 |
if (pext.refcnt == 1) { |
| 871 |
new_base = pext.bno; |
| 872 |
new_blkcnt = extent->phys_block_num + blkcnt - new_base; |
| 873 |
ret = apfs_extend_phys_extent(query, new_base, new_blkcnt, dstream->ds_id); |
| 874 |
if (ret) |
| 875 |
apfs_err(sb, "update failed for paddr 0x%llx", new_base); |
| 876 |
} else { |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
new_base = pext.bno + pext.blkcount; |
| 882 |
new_blkcnt = extent->phys_block_num + blkcnt - new_base; |
| 883 |
ret = apfs_insert_new_phys_extent(query, new_base, new_blkcnt, dstream->ds_id); |
| 884 |
if (ret) |
| 885 |
apfs_err(sb, "insertion failed for paddr 0x%llx", new_base); |
| 886 |
} |
| 887 |
|
| 888 |
out: |
| 889 |
apfs_free_query(query); |
| 890 |
apfs_node_free(extref_root); |
| 891 |
return ret; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
static int apfs_phys_ext_from_query(struct apfs_query *query, struct apfs_phys_extent *pext) |
| 904 |
{ |
| 905 |
struct super_block *sb = query->node->object.sb; |
| 906 |
struct apfs_phys_ext_key *key; |
| 907 |
struct apfs_phys_ext_val *val; |
| 908 |
char *raw = query->node->object.data; |
| 909 |
|
| 910 |
if (query->len != sizeof(*val) || query->key_len != sizeof(*key)) { |
| 911 |
apfs_err(sb, "bad length of key (%d) or value (%d)", query->key_len, query->len); |
| 912 |
return -EFSCORRUPTED; |
| 913 |
} |
| 914 |
|
| 915 |
key = (struct apfs_phys_ext_key *)(raw + query->key_off); |
| 916 |
val = (struct apfs_phys_ext_val *)(raw + query->off); |
| 917 |
|
| 918 |
pext->bno = apfs_cat_cnid(&key->hdr); |
| 919 |
pext->blkcount = le64_to_cpu(val->len_and_kind) & APFS_PEXT_LEN_MASK; |
| 920 |
pext->len = pext->blkcount << sb->s_blocksize_bits; |
| 921 |
pext->refcnt = le32_to_cpu(val->refcnt); |
| 922 |
pext->kind = le64_to_cpu(val->len_and_kind) >> APFS_PEXT_KIND_SHIFT; |
| 923 |
return 0; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
static int apfs_free_phys_ext(struct super_block *sb, struct apfs_phys_extent *pext) |
| 934 |
{ |
| 935 |
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 936 |
|
| 937 |
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o); |
| 938 |
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, -pext->blkcount); |
| 939 |
le64_add_cpu(&vsb_raw->apfs_total_blocks_freed, pext->blkcount); |
| 940 |
|
| 941 |
return apfs_free_queue_insert(sb, pext->bno, pext->blkcount); |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
static int apfs_put_phys_extent(struct apfs_phys_extent *pext, struct apfs_query *query) |
| 952 |
{ |
| 953 |
struct super_block *sb = query->node->object.sb; |
| 954 |
struct apfs_phys_ext_val *val; |
| 955 |
void *raw; |
| 956 |
int err; |
| 957 |
|
| 958 |
if (--pext->refcnt == 0) { |
| 959 |
err = apfs_btree_remove(query); |
| 960 |
if (err) { |
| 961 |
apfs_err(sb, "removal failed for paddr 0x%llx", pext->bno); |
| 962 |
return err; |
| 963 |
} |
| 964 |
return pext->kind == APFS_KIND_NEW ? apfs_free_phys_ext(sb, pext) : 0; |
| 965 |
} |
| 966 |
|
| 967 |
err = apfs_query_join_transaction(query); |
| 968 |
if (err) { |
| 969 |
apfs_err(sb, "query join failed"); |
| 970 |
return err; |
| 971 |
} |
| 972 |
raw = query->node->object.data; |
| 973 |
val = raw + query->off; |
| 974 |
val->refcnt = cpu_to_le32(pext->refcnt); |
| 975 |
return 0; |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
static int apfs_take_phys_extent(struct apfs_phys_extent *pext, struct apfs_query *query) |
| 986 |
{ |
| 987 |
struct super_block *sb = query->node->object.sb; |
| 988 |
struct apfs_phys_ext_val *val; |
| 989 |
void *raw; |
| 990 |
int err; |
| 991 |
|
| 992 |
|
| 993 |
if (++pext->refcnt == 0) |
| 994 |
return apfs_btree_remove(query); |
| 995 |
|
| 996 |
err = apfs_query_join_transaction(query); |
| 997 |
if (err) { |
| 998 |
apfs_err(sb, "query join failed"); |
| 999 |
return err; |
| 1000 |
} |
| 1001 |
raw = query->node->object.data; |
| 1002 |
val = raw + query->off; |
| 1003 |
val->refcnt = cpu_to_le32(pext->refcnt); |
| 1004 |
return 0; |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
static inline void apfs_set_phys_ext_length(struct apfs_phys_ext_val *pext, u64 len) |
| 1015 |
{ |
| 1016 |
u64 len_and_kind = le64_to_cpu(pext->len_and_kind); |
| 1017 |
u64 kind = len_and_kind & APFS_PEXT_KIND_MASK; |
| 1018 |
|
| 1019 |
pext->len_and_kind = cpu_to_le64(kind | len); |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
static int apfs_split_phys_ext(struct apfs_query *query, u64 div) |
| 1030 |
{ |
| 1031 |
struct super_block *sb = query->node->object.sb; |
| 1032 |
struct apfs_phys_ext_val *val1; |
| 1033 |
struct apfs_phys_ext_key key2; |
| 1034 |
struct apfs_phys_ext_val val2; |
| 1035 |
struct apfs_phys_extent pextent; |
| 1036 |
u64 blkcount1, blkcount2; |
| 1037 |
void *raw; |
| 1038 |
int err = 0; |
| 1039 |
|
| 1040 |
err = apfs_query_join_transaction(query); |
| 1041 |
if (err) { |
| 1042 |
apfs_err(sb, "query join failed"); |
| 1043 |
return err; |
| 1044 |
} |
| 1045 |
raw = query->node->object.data; |
| 1046 |
|
| 1047 |
err = apfs_phys_ext_from_query(query, &pextent); |
| 1048 |
if (err) { |
| 1049 |
apfs_err(sb, "bad pext record over div 0x%llx", div); |
| 1050 |
return err; |
| 1051 |
} |
| 1052 |
val1 = raw + query->off; |
| 1053 |
val2 = *(struct apfs_phys_ext_val *)(raw + query->off); |
| 1054 |
key2 = *(struct apfs_phys_ext_key *)(raw + query->key_off); |
| 1055 |
|
| 1056 |
blkcount1 = div - pextent.bno; |
| 1057 |
blkcount2 = pextent.blkcount - blkcount1; |
| 1058 |
|
| 1059 |
|
| 1060 |
apfs_set_phys_ext_length(val1, blkcount1); |
| 1061 |
|
| 1062 |
|
| 1063 |
apfs_key_set_hdr(APFS_TYPE_EXTENT, div, &key2); |
| 1064 |
apfs_set_phys_ext_length(&val2, blkcount2); |
| 1065 |
return apfs_btree_insert(query, &key2, sizeof(key2), &val2, sizeof(val2)); |
| 1066 |
} |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
static int apfs_create_update_pext(struct apfs_query *query, const struct apfs_file_extent *extent, u32 diff) |
| 1077 |
{ |
| 1078 |
struct super_block *sb = query->node->object.sb; |
| 1079 |
struct apfs_phys_ext_key key = {0}; |
| 1080 |
struct apfs_phys_ext_val val = {0}; |
| 1081 |
|
| 1082 |
apfs_key_set_hdr(APFS_TYPE_EXTENT, extent->phys_block_num, &key); |
| 1083 |
val.len_and_kind = cpu_to_le64((u64)APFS_KIND_UPDATE << APFS_PEXT_KIND_SHIFT | extent->len >> sb->s_blocksize_bits); |
| 1084 |
val.owning_obj_id = cpu_to_le64(APFS_OWNING_OBJ_ID_INVALID); |
| 1085 |
val.refcnt = cpu_to_le32(diff); |
| 1086 |
return apfs_btree_insert(query, &key, sizeof(key), &val, sizeof(val)); |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
static inline bool apfs_dstream_cache_is_tail(struct apfs_dstream_info *dstream) |
| 1094 |
{ |
| 1095 |
struct apfs_file_extent *cache = &dstream->ds_cached_ext; |
| 1096 |
|
| 1097 |
|
| 1098 |
return cache->len && (dstream->ds_size <= cache->logical_addr + cache->len); |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
int apfs_flush_extent_cache(struct apfs_dstream_info *dstream) |
| 1108 |
{ |
| 1109 |
struct super_block *sb = dstream->ds_sb; |
| 1110 |
struct apfs_file_extent *ext = &dstream->ds_cached_ext; |
| 1111 |
int err; |
| 1112 |
|
| 1113 |
if (!dstream->ds_ext_dirty) |
| 1114 |
return 0; |
| 1115 |
ASSERT(ext->len > 0); |
| 1116 |
|
| 1117 |
err = apfs_update_extent(dstream, ext); |
| 1118 |
if (err) { |
| 1119 |
apfs_err(sb, "extent update failed"); |
| 1120 |
return err; |
| 1121 |
} |
| 1122 |
err = apfs_insert_phys_extent(dstream, ext); |
| 1123 |
if (err) { |
| 1124 |
apfs_err(sb, "pext insertion failed"); |
| 1125 |
return err; |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
dstream->ds_ext_dirty = false; |
| 1136 |
return 0; |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
static int apfs_create_hole(struct apfs_dstream_info *dstream, u64 start, u64 end) |
| 1149 |
{ |
| 1150 |
struct super_block *sb = dstream->ds_sb; |
| 1151 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 1152 |
struct apfs_query *query; |
| 1153 |
struct apfs_file_extent_key raw_key; |
| 1154 |
struct apfs_file_extent_val raw_val; |
| 1155 |
u64 extent_id = dstream->ds_id; |
| 1156 |
int ret; |
| 1157 |
|
| 1158 |
if (start == end) |
| 1159 |
return 0; |
| 1160 |
|
| 1161 |
|
| 1162 |
start <<= sb->s_blocksize_bits; |
| 1163 |
end <<= sb->s_blocksize_bits; |
| 1164 |
|
| 1165 |
apfs_key_set_hdr(APFS_TYPE_FILE_EXTENT, extent_id, &raw_key); |
| 1166 |
raw_key.logical_addr = cpu_to_le64(start); |
| 1167 |
raw_val.len_and_flags = cpu_to_le64(end - start); |
| 1168 |
raw_val.phys_block_num = cpu_to_le64(0); |
| 1169 |
raw_val.crypto_id = cpu_to_le64(apfs_vol_is_encrypted(sb) ? extent_id : 0); |
| 1170 |
|
| 1171 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 1172 |
if (!query) |
| 1173 |
return -ENOMEM; |
| 1174 |
apfs_init_file_extent_key(extent_id, start, &query->key); |
| 1175 |
query->flags = APFS_QUERY_CAT; |
| 1176 |
|
| 1177 |
ret = apfs_btree_query(sb, &query); |
| 1178 |
if (ret && ret != -ENODATA) { |
| 1179 |
apfs_err(sb, "query failed for id 0x%llx, addr 0x%llx", extent_id, start); |
| 1180 |
goto out; |
| 1181 |
} |
| 1182 |
|
| 1183 |
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 1184 |
if (ret) |
| 1185 |
apfs_err(sb, "insertion failed for id 0x%llx, addr 0x%llx", extent_id, start); |
| 1186 |
dstream->ds_sparse_bytes += end - start; |
| 1187 |
|
| 1188 |
out: |
| 1189 |
apfs_free_query(query); |
| 1190 |
return ret; |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
static int apfs_zero_dstream_tail(struct apfs_dstream_info *dstream) |
| 1200 |
{ |
| 1201 |
struct super_block *sb = dstream->ds_sb; |
| 1202 |
struct inode *inode = NULL; |
| 1203 |
struct page *page = NULL; |
| 1204 |
void *fsdata = NULL; |
| 1205 |
int valid_length; |
| 1206 |
int err; |
| 1207 |
|
| 1208 |
|
| 1209 |
if (dstream->ds_size <= dstream->ds_sparse_bytes) |
| 1210 |
return 0; |
| 1211 |
|
| 1212 |
|
| 1213 |
valid_length = dstream->ds_size & (sb->s_blocksize - 1); |
| 1214 |
if (valid_length == 0) |
| 1215 |
return 0; |
| 1216 |
|
| 1217 |
inode = dstream->ds_inode; |
| 1218 |
if (!inode) { |
| 1219 |
|
| 1220 |
apfs_alert(sb, "attempt to zero the tail of xattr dstream 0x%llx", dstream->ds_id); |
| 1221 |
return -EFSCORRUPTED; |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
err = __apfs_write_begin(NULL, inode->i_mapping, inode->i_size, 0, 0, &page, &fsdata); |
| 1226 |
if (err) |
| 1227 |
return err; |
| 1228 |
return __apfs_write_end(NULL, inode->i_mapping, inode->i_size, 0, 0, page, fsdata); |
| 1229 |
} |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
static void apfs_zero_bh_tail(struct super_block *sb, struct buffer_head *bh, u64 length) |
| 1238 |
{ |
| 1239 |
ASSERT(buffer_trans(bh)); |
| 1240 |
if (length < sb->s_blocksize) |
| 1241 |
memset(bh->b_data + length, 0, sb->s_blocksize - length); |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
static int apfs_range_in_snap(struct super_block *sb, u64 bno, u64 blkcnt, bool *in_snap) |
| 1254 |
{ |
| 1255 |
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 1256 |
struct apfs_node *extref_root = NULL; |
| 1257 |
struct apfs_query *query = NULL; |
| 1258 |
struct apfs_phys_extent pext = {0}; |
| 1259 |
int ret; |
| 1260 |
|
| 1261 |
|
| 1262 |
if (vsb_raw->apfs_num_snapshots == 0) { |
| 1263 |
*in_snap = false; |
| 1264 |
return 0; |
| 1265 |
} |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
extref_root = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_extentref_tree_oid), APFS_OBJ_PHYSICAL, false ); |
| 1272 |
if (IS_ERR(extref_root)) { |
| 1273 |
apfs_err(sb, "failed to read extref root 0x%llx", le64_to_cpu(vsb_raw->apfs_extentref_tree_oid)); |
| 1274 |
return PTR_ERR(extref_root); |
| 1275 |
} |
| 1276 |
|
| 1277 |
query = apfs_alloc_query(extref_root, NULL ); |
| 1278 |
if (!query) { |
| 1279 |
ret = -ENOMEM; |
| 1280 |
goto out; |
| 1281 |
} |
| 1282 |
|
| 1283 |
apfs_init_extent_key(bno, &query->key); |
| 1284 |
query->flags = APFS_QUERY_EXTENTREF; |
| 1285 |
|
| 1286 |
ret = apfs_btree_query(sb, &query); |
| 1287 |
if (ret && ret != -ENODATA) { |
| 1288 |
apfs_err(sb, "query failed for paddr 0x%llx", bno); |
| 1289 |
goto out; |
| 1290 |
} |
| 1291 |
if (ret == -ENODATA) { |
| 1292 |
*in_snap = false; |
| 1293 |
ret = 0; |
| 1294 |
goto out; |
| 1295 |
} |
| 1296 |
|
| 1297 |
ret = apfs_phys_ext_from_query(query, &pext); |
| 1298 |
if (ret) { |
| 1299 |
apfs_err(sb, "bad pext record for paddr 0x%llx", bno); |
| 1300 |
goto out; |
| 1301 |
} |
| 1302 |
|
| 1303 |
if (pext.bno <= bno && pext.bno + pext.blkcount >= bno + blkcnt) { |
| 1304 |
if (pext.kind == APFS_KIND_NEW) { |
| 1305 |
*in_snap = false; |
| 1306 |
goto out; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
*in_snap = true; |
| 1316 |
|
| 1317 |
out: |
| 1318 |
apfs_free_query(query); |
| 1319 |
apfs_node_free(extref_root); |
| 1320 |
return ret; |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
static int apfs_dstream_cache_in_snap(struct apfs_dstream_info *dstream, bool *in_snap) |
| 1331 |
{ |
| 1332 |
struct super_block *sb = dstream->ds_sb; |
| 1333 |
struct apfs_file_extent *cache = NULL; |
| 1334 |
|
| 1335 |
|
| 1336 |
if (dstream->ds_ext_dirty) { |
| 1337 |
*in_snap = false; |
| 1338 |
return 0; |
| 1339 |
} |
| 1340 |
|
| 1341 |
cache = &dstream->ds_cached_ext; |
| 1342 |
return apfs_range_in_snap(sb, cache->phys_block_num, cache->len >> sb->s_blocksize_bits, in_snap); |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
static int apfs_dstream_get_new_block(struct apfs_dstream_info *dstream, u64 dsblock, struct buffer_head *bh_result, u64 *bno) |
| 1355 |
{ |
| 1356 |
struct super_block *sb = dstream->ds_sb; |
| 1357 |
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 1358 |
struct apfs_file_extent *cache = NULL; |
| 1359 |
u64 phys_bno, logical_addr, cache_blks, dstream_blks; |
| 1360 |
bool in_snap = true; |
| 1361 |
int err; |
| 1362 |
|
| 1363 |
|
| 1364 |
logical_addr = dsblock << sb->s_blocksize_bits; |
| 1365 |
|
| 1366 |
err = apfs_spaceman_allocate_block(sb, &phys_bno, false ); |
| 1367 |
if (err) { |
| 1368 |
apfs_err(sb, "block allocation failed"); |
| 1369 |
return err; |
| 1370 |
} |
| 1371 |
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o); |
| 1372 |
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, 1); |
| 1373 |
le64_add_cpu(&vsb_raw->apfs_total_blocks_alloced, 1); |
| 1374 |
if (bno) |
| 1375 |
*bno = phys_bno; |
| 1376 |
|
| 1377 |
if (bh_result) { |
| 1378 |
apfs_map_bh(bh_result, sb, phys_bno); |
| 1379 |
err = apfs_transaction_join(sb, bh_result); |
| 1380 |
if (err) |
| 1381 |
return err; |
| 1382 |
|
| 1383 |
if (!buffer_uptodate(bh_result)) { |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
set_buffer_new(bh_result); |
| 1389 |
} else if (dstream->ds_size > logical_addr) { |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
apfs_zero_bh_tail(sb, bh_result, dstream->ds_size - logical_addr); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
dstream_blks = apfs_size_to_blocks(sb, dstream->ds_size); |
| 1399 |
if (dstream_blks < dsblock) { |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
err = apfs_zero_dstream_tail(dstream); |
| 1405 |
if (err) { |
| 1406 |
apfs_err(sb, "failed to zero tail for dstream 0x%llx", dstream->ds_id); |
| 1407 |
return err; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
err = apfs_dstream_cache_in_snap(dstream, &in_snap); |
| 1412 |
if (err) |
| 1413 |
return err; |
| 1414 |
|
| 1415 |
cache = &dstream->ds_cached_ext; |
| 1416 |
cache_blks = apfs_size_to_blocks(sb, cache->len); |
| 1417 |
|
| 1418 |
|
| 1419 |
if (!in_snap && apfs_dstream_cache_is_tail(dstream) && |
| 1420 |
logical_addr == cache->logical_addr + cache->len && |
| 1421 |
phys_bno == cache->phys_block_num + cache_blks) { |
| 1422 |
cache->len += sb->s_blocksize; |
| 1423 |
dstream->ds_ext_dirty = true; |
| 1424 |
return 0; |
| 1425 |
} |
| 1426 |
|
| 1427 |
err = apfs_flush_extent_cache(dstream); |
| 1428 |
if (err) { |
| 1429 |
apfs_err(sb, "extent cache flush failed for dstream 0x%llx", dstream->ds_id); |
| 1430 |
return err; |
| 1431 |
} |
| 1432 |
|
| 1433 |
if (dstream_blks < dsblock) { |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
err = apfs_create_hole(dstream, dstream_blks, dsblock); |
| 1440 |
if (err) { |
| 1441 |
apfs_err(sb, "hole creation failed for dstream 0x%llx", dstream->ds_id); |
| 1442 |
return err; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
cache->logical_addr = logical_addr; |
| 1447 |
cache->phys_block_num = phys_bno; |
| 1448 |
cache->len = sb->s_blocksize; |
| 1449 |
dstream->ds_ext_dirty = true; |
| 1450 |
return 0; |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
int apfs_dstream_get_new_bno(struct apfs_dstream_info *dstream, u64 dsblock, u64 *bno) |
| 1462 |
{ |
| 1463 |
return apfs_dstream_get_new_block(dstream, dsblock, NULL , bno); |
| 1464 |
} |
| 1465 |
|
| 1466 |
int apfs_get_new_block(struct inode *inode, sector_t iblock, |
| 1467 |
struct buffer_head *bh_result, int create) |
| 1468 |
{ |
| 1469 |
struct apfs_inode_info *ai = APFS_I(inode); |
| 1470 |
|
| 1471 |
ASSERT(create); |
| 1472 |
return apfs_dstream_get_new_block(&ai->i_dstream, iblock, bh_result, NULL ); |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
static int apfs_shrink_dstream_last_extent(struct apfs_dstream_info *dstream, loff_t new_size) |
| 1488 |
{ |
| 1489 |
struct super_block *sb = dstream->ds_sb; |
| 1490 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 1491 |
struct apfs_query *query; |
| 1492 |
struct apfs_file_extent tail; |
| 1493 |
u64 extent_id = dstream->ds_id; |
| 1494 |
int ret = 0; |
| 1495 |
|
| 1496 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 1497 |
if (!query) |
| 1498 |
return -ENOMEM; |
| 1499 |
apfs_init_file_extent_key(extent_id, -1, &query->key); |
| 1500 |
query->flags = APFS_QUERY_CAT; |
| 1501 |
|
| 1502 |
ret = apfs_btree_query(sb, &query); |
| 1503 |
if (ret && ret != -ENODATA) { |
| 1504 |
apfs_err(sb, "query failed for last extent of id 0x%llx", extent_id); |
| 1505 |
goto out; |
| 1506 |
} |
| 1507 |
|
| 1508 |
if (!apfs_query_found_extent(query)) { |
| 1509 |
|
| 1510 |
if (new_size) { |
| 1511 |
apfs_err(sb, "missing extent for dstream 0x%llx", extent_id); |
| 1512 |
ret = -EFSCORRUPTED; |
| 1513 |
} else { |
| 1514 |
ret = 0; |
| 1515 |
} |
| 1516 |
goto out; |
| 1517 |
} |
| 1518 |
|
| 1519 |
ret = apfs_extent_from_query(query, &tail); |
| 1520 |
if (ret) { |
| 1521 |
apfs_err(sb, "bad tail extent record on dstream 0x%llx", extent_id); |
| 1522 |
goto out; |
| 1523 |
} |
| 1524 |
|
| 1525 |
if (tail.logical_addr + tail.len < new_size) { |
| 1526 |
apfs_err(sb, "missing extent for dstream 0x%llx", extent_id); |
| 1527 |
ret = -EFSCORRUPTED; |
| 1528 |
} else if (tail.logical_addr + tail.len == new_size) { |
| 1529 |
ret = 0; |
| 1530 |
} else if (tail.logical_addr >= new_size) { |
| 1531 |
|
| 1532 |
ret = apfs_btree_remove(query); |
| 1533 |
if (ret) { |
| 1534 |
apfs_err(sb, "removal failed for id 0x%llx, addr 0x%llx", dstream->ds_id, tail.logical_addr); |
| 1535 |
goto out; |
| 1536 |
} |
| 1537 |
if (apfs_ext_is_hole(&tail)) { |
| 1538 |
dstream->ds_sparse_bytes -= tail.len; |
| 1539 |
} else { |
| 1540 |
ret = apfs_range_put_reference(sb, tail.phys_block_num, tail.len); |
| 1541 |
if (ret) { |
| 1542 |
apfs_err(sb, "failed to put range 0x%llx-0x%llx", tail.phys_block_num, tail.len); |
| 1543 |
goto out; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
ret = apfs_crypto_adj_refcnt(sb, tail.crypto_id, -1); |
| 1547 |
if (ret) { |
| 1548 |
apfs_err(sb, "failed to take crypto id 0x%llx", tail.crypto_id); |
| 1549 |
goto out; |
| 1550 |
} |
| 1551 |
ret = tail.logical_addr == new_size ? 0 : -EAGAIN; |
| 1552 |
} else { |
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
new_size = apfs_size_to_blocks(sb, new_size) << sb->s_blocksize_bits; |
| 1558 |
ret = apfs_shrink_extent_tail(query, dstream, new_size); |
| 1559 |
if (ret) |
| 1560 |
apfs_err(sb, "failed to shrink tail of dstream 0x%llx", extent_id); |
| 1561 |
} |
| 1562 |
|
| 1563 |
out: |
| 1564 |
apfs_free_query(query); |
| 1565 |
return ret; |
| 1566 |
} |
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
static int apfs_shrink_dstream(struct apfs_dstream_info *dstream, loff_t new_size) |
| 1576 |
{ |
| 1577 |
int ret; |
| 1578 |
|
| 1579 |
do { |
| 1580 |
ret = apfs_shrink_dstream_last_extent(dstream, new_size); |
| 1581 |
} while (ret == -EAGAIN); |
| 1582 |
|
| 1583 |
return ret; |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
int apfs_truncate(struct apfs_dstream_info *dstream, loff_t new_size) |
| 1594 |
{ |
| 1595 |
struct super_block *sb = dstream->ds_sb; |
| 1596 |
u64 old_blks, new_blks; |
| 1597 |
struct apfs_file_extent *cache = &dstream->ds_cached_ext; |
| 1598 |
int err; |
| 1599 |
|
| 1600 |
|
| 1601 |
err = apfs_flush_extent_cache(dstream); |
| 1602 |
if (err) { |
| 1603 |
apfs_err(sb, "extent cache flush failed for dstream 0x%llx", dstream->ds_id); |
| 1604 |
return err; |
| 1605 |
} |
| 1606 |
dstream->ds_ext_dirty = false; |
| 1607 |
|
| 1608 |
|
| 1609 |
cache->len = 0; |
| 1610 |
|
| 1611 |
|
| 1612 |
if (new_size <= dstream->ds_size) |
| 1613 |
return apfs_shrink_dstream(dstream, new_size); |
| 1614 |
|
| 1615 |
err = apfs_zero_dstream_tail(dstream); |
| 1616 |
if (err) { |
| 1617 |
apfs_err(sb, "failed to zero tail for dstream 0x%llx", dstream->ds_id); |
| 1618 |
return err; |
| 1619 |
} |
| 1620 |
new_blks = apfs_size_to_blocks(sb, new_size); |
| 1621 |
old_blks = apfs_size_to_blocks(sb, dstream->ds_size); |
| 1622 |
return apfs_create_hole(dstream, old_blks, new_blks); |
| 1623 |
} |
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
static int apfs_dstream_delete_front(struct super_block *sb, u64 ds_id) |
| 1635 |
{ |
| 1636 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 1637 |
struct apfs_spaceman *sm = APFS_SM(sb); |
| 1638 |
struct apfs_spaceman_phys *sm_raw = sm->sm_raw; |
| 1639 |
struct apfs_spaceman_free_queue *fq = NULL; |
| 1640 |
struct apfs_query *query = NULL; |
| 1641 |
struct apfs_file_extent head; |
| 1642 |
bool first_match = true; |
| 1643 |
int ret; |
| 1644 |
|
| 1645 |
fq = &sm_raw->sm_fq[APFS_SFQ_MAIN]; |
| 1646 |
if (le64_to_cpu(fq->sfq_count) > APFS_TRANS_MAIN_QUEUE_MAX) |
| 1647 |
return -EAGAIN; |
| 1648 |
|
| 1649 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 1650 |
if (!query) |
| 1651 |
return -ENOMEM; |
| 1652 |
apfs_init_file_extent_key(ds_id, 0, &query->key); |
| 1653 |
query->flags = APFS_QUERY_CAT; |
| 1654 |
|
| 1655 |
next_extent: |
| 1656 |
ret = apfs_btree_query(sb, &query); |
| 1657 |
if (ret && ret != -ENODATA) { |
| 1658 |
apfs_err(sb, "query failed for first extent of id 0x%llx", ds_id); |
| 1659 |
goto out; |
| 1660 |
} |
| 1661 |
apfs_query_direct_forward(query); |
| 1662 |
if (!apfs_query_found_extent(query)) { |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
if (first_match) { |
| 1669 |
first_match = false; |
| 1670 |
goto next_extent; |
| 1671 |
} |
| 1672 |
ret = -ENODATA; |
| 1673 |
goto out; |
| 1674 |
} |
| 1675 |
first_match = false; |
| 1676 |
|
| 1677 |
ret = apfs_extent_from_query(query, &head); |
| 1678 |
if (ret) { |
| 1679 |
apfs_err(sb, "bad head extent record on dstream 0x%llx", ds_id); |
| 1680 |
goto out; |
| 1681 |
} |
| 1682 |
ret = apfs_btree_remove(query); |
| 1683 |
if (ret) { |
| 1684 |
apfs_err(sb, "removal failed for id 0x%llx, addr 0x%llx", ds_id, head.logical_addr); |
| 1685 |
goto out; |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
if (!apfs_ext_is_hole(&head)) { |
| 1693 |
ret = apfs_range_put_reference(sb, head.phys_block_num, head.len); |
| 1694 |
if (ret) { |
| 1695 |
apfs_err(sb, "failed to put range 0x%llx-0x%llx", head.phys_block_num, head.len); |
| 1696 |
goto out; |
| 1697 |
} |
| 1698 |
ret = apfs_crypto_adj_refcnt(sb, head.crypto_id, -1); |
| 1699 |
if (ret) { |
| 1700 |
apfs_err(sb, "failed to take crypto id 0x%llx", head.crypto_id); |
| 1701 |
goto out; |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (le64_to_cpu(fq->sfq_count) <= APFS_TRANS_MAIN_QUEUE_MAX) |
| 1706 |
goto next_extent; |
| 1707 |
ret = -EAGAIN; |
| 1708 |
out: |
| 1709 |
apfs_free_query(query); |
| 1710 |
return ret; |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
int apfs_inode_delete_front(struct inode *inode) |
| 1722 |
{ |
| 1723 |
struct super_block *sb = inode->i_sb; |
| 1724 |
struct apfs_dstream_info *dstream = NULL; |
| 1725 |
struct apfs_inode_info *ai = APFS_I(inode); |
| 1726 |
int ret; |
| 1727 |
|
| 1728 |
if (!ai->i_has_dstream) |
| 1729 |
return 0; |
| 1730 |
|
| 1731 |
dstream = &ai->i_dstream; |
| 1732 |
ret = apfs_flush_extent_cache(dstream); |
| 1733 |
if (ret) { |
| 1734 |
apfs_err(sb, "extent cache flush failed for dstream 0x%llx", dstream->ds_id); |
| 1735 |
return ret; |
| 1736 |
} |
| 1737 |
|
| 1738 |
ret = apfs_dstream_delete_front(sb, dstream->ds_id); |
| 1739 |
if (ret == -ENODATA) |
| 1740 |
return 0; |
| 1741 |
return ret; |
| 1742 |
} |
| 1743 |
|
| 1744 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) |
| 1745 |
loff_t apfs_remap_file_range(struct file *src_file, loff_t off, struct file *dst_file, loff_t destoff, loff_t len, unsigned int remap_flags) |
| 1746 |
#else |
| 1747 |
int apfs_clone_file_range(struct file *src_file, loff_t off, struct file *dst_file, loff_t destoff, u64 len) |
| 1748 |
#endif |
| 1749 |
{ |
| 1750 |
struct inode *src_inode = file_inode(src_file); |
| 1751 |
struct inode *dst_inode = file_inode(dst_file); |
| 1752 |
struct apfs_inode_info *src_ai = APFS_I(src_inode); |
| 1753 |
struct apfs_inode_info *dst_ai = APFS_I(dst_inode); |
| 1754 |
struct apfs_dstream_info *src_ds = &src_ai->i_dstream; |
| 1755 |
struct apfs_dstream_info *dst_ds = &dst_ai->i_dstream; |
| 1756 |
struct super_block *sb = src_inode->i_sb; |
| 1757 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 1758 |
const u64 xfield_flags = APFS_INODE_MAINTAIN_DIR_STATS | APFS_INODE_IS_SPARSE | APFS_INODE_HAS_PURGEABLE_FLAGS; |
| 1759 |
int err; |
| 1760 |
|
| 1761 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) |
| 1762 |
if (remap_flags & ~(REMAP_FILE_ADVISORY)) |
| 1763 |
return -EINVAL; |
| 1764 |
#endif |
| 1765 |
if (src_inode == dst_inode) |
| 1766 |
return -EINVAL; |
| 1767 |
|
| 1768 |
|
| 1769 |
if (off != 0 || destoff != 0 || len != 0) |
| 1770 |
return -EINVAL; |
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
if (dst_ai->i_has_dstream || dst_ai->i_bsd_flags & APFS_INOBSD_COMPRESSED) { |
| 1778 |
apfs_warn(sb, "clones can only replace freshly created files"); |
| 1779 |
return -EOPNOTSUPP; |
| 1780 |
} |
| 1781 |
if (dst_ai->i_int_flags & xfield_flags) { |
| 1782 |
apfs_warn(sb, "clone can't replace a file that has xfields"); |
| 1783 |
return -EOPNOTSUPP; |
| 1784 |
} |
| 1785 |
|
| 1786 |
if (!src_ai->i_has_dstream) { |
| 1787 |
apfs_warn(sb, "can't clone a file with no dstream"); |
| 1788 |
return -EOPNOTSUPP; |
| 1789 |
} |
| 1790 |
|
| 1791 |
err = apfs_transaction_start(sb, APFS_TRANS_REG); |
| 1792 |
if (err) |
| 1793 |
return err; |
| 1794 |
apfs_inode_join_transaction(sb, src_inode); |
| 1795 |
apfs_inode_join_transaction(sb, dst_inode); |
| 1796 |
|
| 1797 |
err = apfs_flush_extent_cache(src_ds); |
| 1798 |
if (err) { |
| 1799 |
apfs_err(sb, "extent cache flush failed for dstream 0x%llx", src_ds->ds_id); |
| 1800 |
goto fail; |
| 1801 |
} |
| 1802 |
err = apfs_dstream_adj_refcnt(src_ds, +1); |
| 1803 |
if (err) { |
| 1804 |
apfs_err(sb, "failed to take dstream id 0x%llx", src_ds->ds_id); |
| 1805 |
goto fail; |
| 1806 |
} |
| 1807 |
src_ds->ds_shared = true; |
| 1808 |
|
| 1809 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0) |
| 1810 |
dst_inode->i_mtime = dst_inode->i_ctime = current_time(dst_inode); |
| 1811 |
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0) |
| 1812 |
dst_inode->i_mtime = inode_set_ctime_current(dst_inode); |
| 1813 |
#else |
| 1814 |
inode_set_mtime_to_ts(dst_inode, inode_set_ctime_current(dst_inode)); |
| 1815 |
#endif |
| 1816 |
dst_inode->i_size = src_inode->i_size; |
| 1817 |
dst_ai->i_key_class = src_ai->i_key_class; |
| 1818 |
dst_ai->i_int_flags = src_ai->i_int_flags; |
| 1819 |
dst_ai->i_bsd_flags = src_ai->i_bsd_flags; |
| 1820 |
dst_ai->i_has_dstream = true; |
| 1821 |
|
| 1822 |
dst_ds->ds_sb = src_ds->ds_sb; |
| 1823 |
dst_ds->ds_inode = dst_inode; |
| 1824 |
dst_ds->ds_id = src_ds->ds_id; |
| 1825 |
dst_ds->ds_size = src_ds->ds_size; |
| 1826 |
dst_ds->ds_sparse_bytes = src_ds->ds_sparse_bytes; |
| 1827 |
dst_ds->ds_cached_ext = src_ds->ds_cached_ext; |
| 1828 |
dst_ds->ds_ext_dirty = false; |
| 1829 |
dst_ds->ds_shared = true; |
| 1830 |
|
| 1831 |
dst_ai->i_int_flags |= APFS_INODE_WAS_EVER_CLONED | APFS_INODE_WAS_CLONED; |
| 1832 |
src_ai->i_int_flags |= APFS_INODE_WAS_EVER_CLONED; |
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
dst_ai->i_int_flags &= ~xfield_flags; |
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
sbi->s_nxi->nx_transaction.t_state |= APFS_NX_TRANS_FORCE_COMMIT; |
| 1846 |
err = apfs_transaction_commit(sb); |
| 1847 |
if (err) |
| 1848 |
goto fail; |
| 1849 |
return dst_ds->ds_size; |
| 1850 |
|
| 1851 |
fail: |
| 1852 |
apfs_transaction_abort(sb); |
| 1853 |
return err; |
| 1854 |
} |
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
static int apfs_extent_create_record(struct super_block *sb, u64 dstream_id, struct apfs_file_extent *extent) |
| 1865 |
{ |
| 1866 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 1867 |
struct apfs_query *query = NULL; |
| 1868 |
struct apfs_file_extent_val raw_val; |
| 1869 |
struct apfs_file_extent_key raw_key; |
| 1870 |
int ret = 0; |
| 1871 |
|
| 1872 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 1873 |
if (!query) |
| 1874 |
return -ENOMEM; |
| 1875 |
apfs_init_file_extent_key(dstream_id, extent->logical_addr, &query->key); |
| 1876 |
query->flags = APFS_QUERY_CAT | APFS_QUERY_EXACT; |
| 1877 |
|
| 1878 |
ret = apfs_btree_query(sb, &query); |
| 1879 |
if (ret && ret != -ENODATA) { |
| 1880 |
apfs_err(sb, "query failed for id 0x%llx, addr 0x%llx", dstream_id, extent->logical_addr); |
| 1881 |
goto out; |
| 1882 |
} |
| 1883 |
|
| 1884 |
apfs_key_set_hdr(APFS_TYPE_FILE_EXTENT, dstream_id, &raw_key); |
| 1885 |
raw_key.logical_addr = cpu_to_le64(extent->logical_addr); |
| 1886 |
raw_val.len_and_flags = cpu_to_le64(extent->len); |
| 1887 |
raw_val.phys_block_num = cpu_to_le64(extent->phys_block_num); |
| 1888 |
raw_val.crypto_id = cpu_to_le64(apfs_vol_is_encrypted(sb) ? dstream_id : 0); |
| 1889 |
|
| 1890 |
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val)); |
| 1891 |
if (ret) |
| 1892 |
apfs_err(sb, "insertion failed for id 0x%llx, addr 0x%llx", dstream_id, extent->logical_addr); |
| 1893 |
out: |
| 1894 |
apfs_free_query(query); |
| 1895 |
return ret; |
| 1896 |
} |
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
static int apfs_put_single_extent(struct super_block *sb, u64 *paddr_end, u64 paddr_min) |
| 1912 |
{ |
| 1913 |
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 1914 |
struct apfs_node *extref_root = NULL; |
| 1915 |
struct apfs_key key; |
| 1916 |
struct apfs_query *query = NULL; |
| 1917 |
struct apfs_phys_extent prev_ext; |
| 1918 |
u64 prev_start, prev_end; |
| 1919 |
bool cropped_head = false, cropped_tail = false; |
| 1920 |
struct apfs_file_extent tmp = {0}; |
| 1921 |
int ret; |
| 1922 |
|
| 1923 |
extref_root = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_extentref_tree_oid), APFS_OBJ_PHYSICAL, true ); |
| 1924 |
if (IS_ERR(extref_root)) { |
| 1925 |
apfs_err(sb, "failed to read extref root 0x%llx", le64_to_cpu(vsb_raw->apfs_extentref_tree_oid)); |
| 1926 |
return PTR_ERR(extref_root); |
| 1927 |
} |
| 1928 |
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o); |
| 1929 |
vsb_raw->apfs_extentref_tree_oid = cpu_to_le64(extref_root->object.oid); |
| 1930 |
|
| 1931 |
apfs_init_extent_key(*paddr_end - 1, &key); |
| 1932 |
|
| 1933 |
restart: |
| 1934 |
query = apfs_alloc_query(extref_root, NULL ); |
| 1935 |
if (!query) { |
| 1936 |
ret = -ENOMEM; |
| 1937 |
goto out; |
| 1938 |
} |
| 1939 |
query->key = key; |
| 1940 |
query->flags = APFS_QUERY_EXTENTREF; |
| 1941 |
|
| 1942 |
ret = apfs_btree_query(sb, &query); |
| 1943 |
if (ret && ret != -ENODATA) { |
| 1944 |
apfs_err(sb, "query failed for paddr 0x%llx", *paddr_end - 1); |
| 1945 |
goto out; |
| 1946 |
} |
| 1947 |
|
| 1948 |
if (ret == -ENODATA) { |
| 1949 |
|
| 1950 |
tmp.phys_block_num = paddr_min; |
| 1951 |
tmp.len = (*paddr_end - paddr_min) << sb->s_blocksize_bits; |
| 1952 |
ret = apfs_create_update_pext(query, &tmp, -1); |
| 1953 |
*paddr_end = paddr_min; |
| 1954 |
goto out; |
| 1955 |
} |
| 1956 |
|
| 1957 |
ret = apfs_phys_ext_from_query(query, &prev_ext); |
| 1958 |
if (ret) { |
| 1959 |
apfs_err(sb, "bad pext record over paddr 0x%llx", *paddr_end - 1); |
| 1960 |
goto out; |
| 1961 |
} |
| 1962 |
prev_start = prev_ext.bno; |
| 1963 |
prev_end = prev_ext.bno + prev_ext.blkcount; |
| 1964 |
if (prev_end < *paddr_end) { |
| 1965 |
|
| 1966 |
tmp.phys_block_num = max(prev_end, paddr_min); |
| 1967 |
tmp.len = (*paddr_end - tmp.phys_block_num) << sb->s_blocksize_bits; |
| 1968 |
ret = apfs_create_update_pext(query, &tmp, -1); |
| 1969 |
*paddr_end = tmp.phys_block_num; |
| 1970 |
goto out; |
| 1971 |
} |
| 1972 |
|
| 1973 |
if ((cropped_tail && prev_end > *paddr_end) || (cropped_head && prev_start < paddr_min)) { |
| 1974 |
|
| 1975 |
apfs_alert(sb, "recursion cropping physical extent 0x%llx-0x%llx", prev_start, prev_end); |
| 1976 |
ret = -EFSCORRUPTED; |
| 1977 |
goto out; |
| 1978 |
} |
| 1979 |
|
| 1980 |
if (prev_end > *paddr_end) { |
| 1981 |
ret = apfs_split_phys_ext(query, *paddr_end); |
| 1982 |
if (ret) { |
| 1983 |
apfs_err(sb, "failed to split pext at 0x%llx", *paddr_end); |
| 1984 |
goto out; |
| 1985 |
} |
| 1986 |
|
| 1987 |
apfs_free_query(query); |
| 1988 |
cropped_tail = true; |
| 1989 |
goto restart; |
| 1990 |
} |
| 1991 |
|
| 1992 |
if (prev_start < paddr_min) { |
| 1993 |
ret = apfs_split_phys_ext(query, paddr_min); |
| 1994 |
if (ret) { |
| 1995 |
apfs_err(sb, "failed to split pext at 0x%llx", paddr_min); |
| 1996 |
goto out; |
| 1997 |
} |
| 1998 |
|
| 1999 |
apfs_free_query(query); |
| 2000 |
cropped_head = true; |
| 2001 |
goto restart; |
| 2002 |
} |
| 2003 |
|
| 2004 |
|
| 2005 |
ret = apfs_put_phys_extent(&prev_ext, query); |
| 2006 |
if (ret) |
| 2007 |
apfs_err(sb, "failed to put pext at 0x%llx", prev_ext.bno); |
| 2008 |
*paddr_end = prev_start; |
| 2009 |
|
| 2010 |
out: |
| 2011 |
apfs_free_query(query); |
| 2012 |
apfs_node_free(extref_root); |
| 2013 |
return ret; |
| 2014 |
} |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
static int apfs_range_put_reference(struct super_block *sb, u64 paddr, u64 length) |
| 2025 |
{ |
| 2026 |
u64 extent_end; |
| 2027 |
int err; |
| 2028 |
|
| 2029 |
ASSERT(paddr); |
| 2030 |
|
| 2031 |
extent_end = paddr + (length >> sb->s_blocksize_bits); |
| 2032 |
while (extent_end > paddr) { |
| 2033 |
err = apfs_put_single_extent(sb, &extent_end, paddr); |
| 2034 |
if (err) |
| 2035 |
return err; |
| 2036 |
} |
| 2037 |
return 0; |
| 2038 |
} |
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
static int apfs_take_single_extent(struct super_block *sb, u64 *paddr_end, u64 paddr_min) |
| 2052 |
{ |
| 2053 |
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 2054 |
struct apfs_node *extref_root = NULL; |
| 2055 |
struct apfs_key key; |
| 2056 |
struct apfs_query *query = NULL; |
| 2057 |
struct apfs_phys_extent prev_ext; |
| 2058 |
u64 prev_start, prev_end; |
| 2059 |
bool cropped_head = false, cropped_tail = false; |
| 2060 |
struct apfs_file_extent tmp = {0}; |
| 2061 |
int ret; |
| 2062 |
|
| 2063 |
extref_root = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_extentref_tree_oid), APFS_OBJ_PHYSICAL, true ); |
| 2064 |
if (IS_ERR(extref_root)) { |
| 2065 |
apfs_err(sb, "failed to read extref root 0x%llx", le64_to_cpu(vsb_raw->apfs_extentref_tree_oid)); |
| 2066 |
return PTR_ERR(extref_root); |
| 2067 |
} |
| 2068 |
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o); |
| 2069 |
vsb_raw->apfs_extentref_tree_oid = cpu_to_le64(extref_root->object.oid); |
| 2070 |
|
| 2071 |
apfs_init_extent_key(*paddr_end - 1, &key); |
| 2072 |
|
| 2073 |
restart: |
| 2074 |
query = apfs_alloc_query(extref_root, NULL ); |
| 2075 |
if (!query) { |
| 2076 |
ret = -ENOMEM; |
| 2077 |
goto out; |
| 2078 |
} |
| 2079 |
query->key = key; |
| 2080 |
query->flags = APFS_QUERY_EXTENTREF; |
| 2081 |
|
| 2082 |
ret = apfs_btree_query(sb, &query); |
| 2083 |
if (ret && ret != -ENODATA) { |
| 2084 |
apfs_err(sb, "query failed for paddr 0x%llx", *paddr_end - 1); |
| 2085 |
goto out; |
| 2086 |
} |
| 2087 |
|
| 2088 |
if (ret == -ENODATA) { |
| 2089 |
|
| 2090 |
tmp.phys_block_num = paddr_min; |
| 2091 |
tmp.len = (*paddr_end - paddr_min) << sb->s_blocksize_bits; |
| 2092 |
ret = apfs_create_update_pext(query, &tmp, +1); |
| 2093 |
*paddr_end = paddr_min; |
| 2094 |
goto out; |
| 2095 |
} |
| 2096 |
|
| 2097 |
ret = apfs_phys_ext_from_query(query, &prev_ext); |
| 2098 |
if (ret) { |
| 2099 |
apfs_err(sb, "bad pext record over paddr 0x%llx", *paddr_end - 1); |
| 2100 |
goto out; |
| 2101 |
} |
| 2102 |
prev_start = prev_ext.bno; |
| 2103 |
prev_end = prev_ext.bno + prev_ext.blkcount; |
| 2104 |
if (prev_end < *paddr_end) { |
| 2105 |
|
| 2106 |
tmp.phys_block_num = max(prev_end, paddr_min); |
| 2107 |
tmp.len = (*paddr_end - tmp.phys_block_num) << sb->s_blocksize_bits; |
| 2108 |
ret = apfs_create_update_pext(query, &tmp, +1); |
| 2109 |
*paddr_end = tmp.phys_block_num; |
| 2110 |
goto out; |
| 2111 |
} |
| 2112 |
|
| 2113 |
if ((cropped_tail && prev_end > *paddr_end) || (cropped_head && prev_start < paddr_min)) { |
| 2114 |
|
| 2115 |
apfs_alert(sb, "recursion cropping physical extent 0x%llx-0x%llx", prev_start, prev_end); |
| 2116 |
ret = -EFSCORRUPTED; |
| 2117 |
goto out; |
| 2118 |
} |
| 2119 |
|
| 2120 |
if (prev_end > *paddr_end) { |
| 2121 |
ret = apfs_split_phys_ext(query, *paddr_end); |
| 2122 |
if (ret) { |
| 2123 |
apfs_err(sb, "failed to split pext at 0x%llx", *paddr_end); |
| 2124 |
goto out; |
| 2125 |
} |
| 2126 |
|
| 2127 |
apfs_free_query(query); |
| 2128 |
cropped_tail = true; |
| 2129 |
goto restart; |
| 2130 |
} |
| 2131 |
|
| 2132 |
if (prev_start < paddr_min) { |
| 2133 |
ret = apfs_split_phys_ext(query, paddr_min); |
| 2134 |
if (ret) { |
| 2135 |
apfs_err(sb, "failed to split pext at 0x%llx", paddr_min); |
| 2136 |
goto out; |
| 2137 |
} |
| 2138 |
|
| 2139 |
apfs_free_query(query); |
| 2140 |
cropped_head = true; |
| 2141 |
goto restart; |
| 2142 |
} |
| 2143 |
|
| 2144 |
|
| 2145 |
ret = apfs_take_phys_extent(&prev_ext, query); |
| 2146 |
if (ret) |
| 2147 |
apfs_err(sb, "failed to take pext at 0x%llx", prev_ext.bno); |
| 2148 |
*paddr_end = prev_start; |
| 2149 |
|
| 2150 |
out: |
| 2151 |
apfs_free_query(query); |
| 2152 |
apfs_node_free(extref_root); |
| 2153 |
return ret; |
| 2154 |
} |
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
static int apfs_range_take_reference(struct super_block *sb, u64 paddr, u64 length) |
| 2165 |
{ |
| 2166 |
u64 extent_end; |
| 2167 |
int err; |
| 2168 |
|
| 2169 |
ASSERT(paddr); |
| 2170 |
|
| 2171 |
extent_end = paddr + (length >> sb->s_blocksize_bits); |
| 2172 |
while (extent_end > paddr) { |
| 2173 |
err = apfs_take_single_extent(sb, &extent_end, paddr); |
| 2174 |
if (err) |
| 2175 |
return err; |
| 2176 |
} |
| 2177 |
return 0; |
| 2178 |
} |
| 2179 |
|
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
static int apfs_clone_single_extent(struct apfs_dstream_info *dstream, u64 new_id, u64 *log_addr) |
| 2192 |
{ |
| 2193 |
struct super_block *sb = dstream->ds_sb; |
| 2194 |
struct apfs_file_extent extent; |
| 2195 |
int err; |
| 2196 |
|
| 2197 |
err = apfs_extent_read(dstream, *log_addr >> sb->s_blocksize_bits, &extent); |
| 2198 |
if (err) { |
| 2199 |
apfs_err(sb, "failed to read an extent to clone for dstream 0x%llx", dstream->ds_id); |
| 2200 |
return err; |
| 2201 |
} |
| 2202 |
err = apfs_extent_create_record(sb, new_id, &extent); |
| 2203 |
if (err) { |
| 2204 |
apfs_err(sb, "failed to create extent record for clone of dstream 0x%llx", dstream->ds_id); |
| 2205 |
return err; |
| 2206 |
} |
| 2207 |
|
| 2208 |
if (!apfs_ext_is_hole(&extent)) { |
| 2209 |
err = apfs_range_take_reference(sb, extent.phys_block_num, extent.len); |
| 2210 |
if (err) { |
| 2211 |
apfs_err(sb, "failed to take a reference to physical range 0x%llx-0x%llx", extent.phys_block_num, extent.len); |
| 2212 |
return err; |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
*log_addr += extent.len; |
| 2217 |
return 0; |
| 2218 |
} |
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
int apfs_clone_extents(struct apfs_dstream_info *dstream, u64 new_id) |
| 2228 |
{ |
| 2229 |
u64 next = 0; |
| 2230 |
int err; |
| 2231 |
|
| 2232 |
while (next < dstream->ds_size) { |
| 2233 |
err = apfs_clone_single_extent(dstream, new_id, &next); |
| 2234 |
if (err) |
| 2235 |
return err; |
| 2236 |
} |
| 2237 |
return 0; |
| 2238 |
} |
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
int apfs_nonsparse_dstream_read(struct apfs_dstream_info *dstream, void *buf, size_t count, u64 offset) |
| 2250 |
{ |
| 2251 |
struct super_block *sb = dstream->ds_sb; |
| 2252 |
u64 logical_start_block, logical_end_block, log_bno, blkcnt, idx; |
| 2253 |
struct buffer_head **bhs = NULL; |
| 2254 |
int ret = 0; |
| 2255 |
|
| 2256 |
|
| 2257 |
if (count >= APFS_MAX_FILE_SIZE || offset >= APFS_MAX_FILE_SIZE) { |
| 2258 |
apfs_err(sb, "dstream read overflow (0x%llx-0x%llx)", offset, (unsigned long long)count); |
| 2259 |
return -EFBIG; |
| 2260 |
} |
| 2261 |
|
| 2262 |
if (offset + count > dstream->ds_size) { |
| 2263 |
apfs_err(sb, "reading past the end (0x%llx-0x%llx)", offset, (unsigned long long)count); |
| 2264 |
|
| 2265 |
return -EFSCORRUPTED; |
| 2266 |
} |
| 2267 |
|
| 2268 |
logical_start_block = offset >> sb->s_blocksize_bits; |
| 2269 |
logical_end_block = (offset + count + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 2270 |
blkcnt = logical_end_block - logical_start_block; |
| 2271 |
bhs = kcalloc(blkcnt, sizeof(*bhs), GFP_KERNEL); |
| 2272 |
if (!bhs) |
| 2273 |
return -ENOMEM; |
| 2274 |
|
| 2275 |
for (log_bno = logical_start_block; log_bno < logical_end_block; log_bno++) { |
| 2276 |
struct buffer_head *bh = NULL; |
| 2277 |
u64 bno = 0; |
| 2278 |
|
| 2279 |
idx = log_bno - logical_start_block; |
| 2280 |
|
| 2281 |
ret = apfs_logic_to_phys_bno(dstream, log_bno, &bno); |
| 2282 |
if (ret) |
| 2283 |
goto out; |
| 2284 |
if (bno == 0) { |
| 2285 |
apfs_err(sb, "nonsparse dstream has a hole"); |
| 2286 |
ret = -EFSCORRUPTED; |
| 2287 |
goto out; |
| 2288 |
} |
| 2289 |
|
| 2290 |
bhs[idx] = __apfs_getblk(sb, bno); |
| 2291 |
if (!bhs[idx]) { |
| 2292 |
apfs_err(sb, "failed to map block 0x%llx", bno); |
| 2293 |
ret = -EIO; |
| 2294 |
goto out; |
| 2295 |
} |
| 2296 |
|
| 2297 |
bh = bhs[idx]; |
| 2298 |
if (!buffer_uptodate(bh)) { |
| 2299 |
get_bh(bh); |
| 2300 |
lock_buffer(bh); |
| 2301 |
apfs_submit_bh(REQ_OP_READ, 0, bh); |
| 2302 |
} |
| 2303 |
} |
| 2304 |
|
| 2305 |
for (log_bno = logical_start_block; log_bno < logical_end_block; log_bno++) { |
| 2306 |
int off_in_block, left_in_block; |
| 2307 |
|
| 2308 |
idx = log_bno - logical_start_block; |
| 2309 |
wait_on_buffer(bhs[idx]); |
| 2310 |
if (!buffer_uptodate(bhs[idx])) { |
| 2311 |
apfs_err(sb, "failed to read a block"); |
| 2312 |
ret = -EIO; |
| 2313 |
goto out; |
| 2314 |
} |
| 2315 |
|
| 2316 |
if (log_bno == logical_start_block) |
| 2317 |
off_in_block = offset & (sb->s_blocksize - 1); |
| 2318 |
else |
| 2319 |
off_in_block = 0; |
| 2320 |
|
| 2321 |
if (log_bno == logical_end_block - 1) |
| 2322 |
left_in_block = count + offset - (log_bno << sb->s_blocksize_bits) - off_in_block; |
| 2323 |
else |
| 2324 |
left_in_block = sb->s_blocksize - off_in_block; |
| 2325 |
|
| 2326 |
memcpy(buf, bhs[idx]->b_data + off_in_block, left_in_block); |
| 2327 |
buf += left_in_block; |
| 2328 |
} |
| 2329 |
|
| 2330 |
out: |
| 2331 |
if (bhs) { |
| 2332 |
for (idx = 0; idx < blkcnt; idx++) |
| 2333 |
brelse(bhs[idx]); |
| 2334 |
kfree(bhs); |
| 2335 |
} |
| 2336 |
return ret; |
| 2337 |
} |
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
void apfs_nonsparse_dstream_preread(struct apfs_dstream_info *dstream) |
| 2346 |
{ |
| 2347 |
struct super_block *sb = dstream->ds_sb; |
| 2348 |
u64 logical_end_block, log_bno; |
| 2349 |
|
| 2350 |
logical_end_block = (dstream->ds_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 2351 |
|
| 2352 |
for (log_bno = 0; log_bno < logical_end_block; log_bno++) { |
| 2353 |
struct buffer_head *bh = NULL; |
| 2354 |
u64 bno = 0; |
| 2355 |
int ret; |
| 2356 |
|
| 2357 |
ret = apfs_logic_to_phys_bno(dstream, log_bno, &bno); |
| 2358 |
if (ret || bno == 0) |
| 2359 |
return; |
| 2360 |
|
| 2361 |
bh = __apfs_getblk(sb, bno); |
| 2362 |
if (!bh) |
| 2363 |
return; |
| 2364 |
if (!buffer_uptodate(bh)) { |
| 2365 |
get_bh(bh); |
| 2366 |
lock_buffer(bh); |
| 2367 |
apfs_submit_bh(REQ_OP_READ, 0, bh); |
| 2368 |
} |
| 2369 |
brelse(bh); |
| 2370 |
bh = NULL; |
| 2371 |
} |
| 2372 |
} |