| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
#include <linux/buffer_head.h> |
| 7 |
#include <linux/xattr.h> |
| 8 |
#include <linux/blk_types.h> |
| 9 |
#include "apfs.h" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
static int apfs_xattr_from_query(struct apfs_query *query, |
| 24 |
struct apfs_xattr *xattr) |
| 25 |
{ |
| 26 |
struct super_block *sb = query->node->object.sb; |
| 27 |
struct apfs_xattr_val *xattr_val; |
| 28 |
struct apfs_xattr_key *xattr_key; |
| 29 |
char *raw = query->node->object.data; |
| 30 |
int datalen = query->len - sizeof(*xattr_val); |
| 31 |
int namelen = query->key_len - sizeof(*xattr_key); |
| 32 |
|
| 33 |
if (namelen < 1 || datalen < 0) { |
| 34 |
apfs_err(sb, "bad length of name (%d) or data (%d)", namelen, datalen); |
| 35 |
return -EFSCORRUPTED; |
| 36 |
} |
| 37 |
|
| 38 |
xattr_val = (struct apfs_xattr_val *)(raw + query->off); |
| 39 |
xattr_key = (struct apfs_xattr_key *)(raw + query->key_off); |
| 40 |
|
| 41 |
if (namelen != le16_to_cpu(xattr_key->name_len)) { |
| 42 |
apfs_err(sb, "inconsistent name length (%d vs %d)", namelen, le16_to_cpu(xattr_key->name_len)); |
| 43 |
return -EFSCORRUPTED; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
if (xattr_key->name[namelen - 1] != 0) { |
| 48 |
apfs_err(sb, "null termination missing"); |
| 49 |
return -EFSCORRUPTED; |
| 50 |
} |
| 51 |
|
| 52 |
xattr->has_dstream = le16_to_cpu(xattr_val->flags) & |
| 53 |
APFS_XATTR_DATA_STREAM; |
| 54 |
|
| 55 |
if (xattr->has_dstream && datalen != sizeof(struct apfs_xattr_dstream)) { |
| 56 |
apfs_err(sb, "bad data length (%d)", datalen); |
| 57 |
return -EFSCORRUPTED; |
| 58 |
} |
| 59 |
if (!xattr->has_dstream && datalen != le16_to_cpu(xattr_val->xdata_len)) { |
| 60 |
apfs_err(sb, "inconsistent data length (%d vs %d)", datalen, le16_to_cpu(xattr_val->xdata_len)); |
| 61 |
return -EFSCORRUPTED; |
| 62 |
} |
| 63 |
|
| 64 |
xattr->name = xattr_key->name; |
| 65 |
xattr->name_len = namelen - 1; |
| 66 |
xattr->xdata = xattr_val->xdata; |
| 67 |
xattr->xdata_len = datalen; |
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static void apfs_dstream_from_xattr(struct super_block *sb, struct apfs_xattr *xattr, struct apfs_dstream_info *dstream) |
| 78 |
{ |
| 79 |
struct apfs_xattr_dstream *xdata = (void *)xattr->xdata; |
| 80 |
|
| 81 |
dstream->ds_sb = sb; |
| 82 |
dstream->ds_inode = NULL; |
| 83 |
dstream->ds_id = le64_to_cpu(xdata->xattr_obj_id); |
| 84 |
dstream->ds_size = le64_to_cpu(xdata->dstream.size); |
| 85 |
dstream->ds_sparse_bytes = 0; |
| 86 |
|
| 87 |
dstream->ds_cached_ext.len = 0; |
| 88 |
dstream->ds_ext_dirty = false; |
| 89 |
spin_lock_init(&dstream->ds_ext_lock); |
| 90 |
|
| 91 |
|
| 92 |
dstream->ds_shared = false; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static int apfs_xattr_extents_read(struct inode *parent, |
| 110 |
struct apfs_xattr *xattr, |
| 111 |
void *buffer, size_t size, bool only_whole) |
| 112 |
{ |
| 113 |
struct super_block *sb = parent->i_sb; |
| 114 |
struct apfs_dstream_info *dstream; |
| 115 |
int length, ret; |
| 116 |
|
| 117 |
dstream = kzalloc(sizeof(*dstream), GFP_KERNEL); |
| 118 |
if (!dstream) |
| 119 |
return -ENOMEM; |
| 120 |
apfs_dstream_from_xattr(sb, xattr, dstream); |
| 121 |
|
| 122 |
length = dstream->ds_size; |
| 123 |
if (length < 0 || length < dstream->ds_size) { |
| 124 |
|
| 125 |
apfs_warn(sb, "xattr is too big to read on linux (0x%llx)", dstream->ds_size); |
| 126 |
ret = -E2BIG; |
| 127 |
goto out; |
| 128 |
} |
| 129 |
|
| 130 |
if (!buffer) { |
| 131 |
|
| 132 |
ret = length; |
| 133 |
goto out; |
| 134 |
} |
| 135 |
|
| 136 |
if (only_whole) { |
| 137 |
if (length > size) { |
| 138 |
|
| 139 |
ret = -ERANGE; |
| 140 |
goto out; |
| 141 |
} |
| 142 |
} else { |
| 143 |
if (length > size) |
| 144 |
length = size; |
| 145 |
} |
| 146 |
|
| 147 |
ret = apfs_nonsparse_dstream_read(dstream, buffer, length, 0 ); |
| 148 |
if (ret == 0) |
| 149 |
ret = length; |
| 150 |
|
| 151 |
out: |
| 152 |
kfree(dstream); |
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
static int apfs_xattr_inline_read(struct apfs_xattr *xattr, void *buffer, size_t size, bool only_whole) |
| 170 |
{ |
| 171 |
int length = xattr->xdata_len; |
| 172 |
|
| 173 |
if (!buffer) |
| 174 |
return length; |
| 175 |
if (only_whole) { |
| 176 |
if (length > size) |
| 177 |
return -ERANGE; |
| 178 |
} else { |
| 179 |
if (length > size) |
| 180 |
length = size; |
| 181 |
} |
| 182 |
memcpy(buffer, xattr->xdata, length); |
| 183 |
return length; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
int apfs_xattr_get_compressed_data(struct inode *inode, const char *name, struct apfs_compressed_data *cdata) |
| 195 |
{ |
| 196 |
struct super_block *sb = inode->i_sb; |
| 197 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 198 |
struct apfs_query *query; |
| 199 |
struct apfs_xattr xattr; |
| 200 |
u64 cnid = apfs_ino(inode); |
| 201 |
int ret; |
| 202 |
|
| 203 |
|
| 204 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 205 |
if (!query) |
| 206 |
return -ENOMEM; |
| 207 |
apfs_init_xattr_key(cnid, name, &query->key); |
| 208 |
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT; |
| 209 |
|
| 210 |
ret = apfs_btree_query(sb, &query); |
| 211 |
if (ret) { |
| 212 |
apfs_err(sb, "query failed for id 0x%llx (%s)", cnid, name); |
| 213 |
goto done; |
| 214 |
} |
| 215 |
|
| 216 |
ret = apfs_xattr_from_query(query, &xattr); |
| 217 |
if (ret) { |
| 218 |
apfs_err(sb, "bad xattr record in inode 0x%llx", cnid); |
| 219 |
goto done; |
| 220 |
} |
| 221 |
|
| 222 |
cdata->has_dstream = xattr.has_dstream; |
| 223 |
if (cdata->has_dstream) { |
| 224 |
struct apfs_dstream_info *dstream = NULL; |
| 225 |
|
| 226 |
dstream = kzalloc(sizeof(*dstream), GFP_KERNEL); |
| 227 |
if (!dstream) { |
| 228 |
ret = -ENOMEM; |
| 229 |
goto done; |
| 230 |
} |
| 231 |
apfs_dstream_from_xattr(sb, &xattr, dstream); |
| 232 |
|
| 233 |
cdata->dstream = dstream; |
| 234 |
cdata->size = dstream->ds_size; |
| 235 |
} else { |
| 236 |
void *data = NULL; |
| 237 |
int len; |
| 238 |
|
| 239 |
len = xattr.xdata_len; |
| 240 |
if (len > APFS_XATTR_MAX_EMBEDDED_SIZE) { |
| 241 |
apfs_err(sb, "inline xattr too big"); |
| 242 |
ret = -EFSCORRUPTED; |
| 243 |
goto done; |
| 244 |
} |
| 245 |
data = kzalloc(len, GFP_KERNEL); |
| 246 |
if (!data) { |
| 247 |
ret = -ENOMEM; |
| 248 |
goto done; |
| 249 |
} |
| 250 |
memcpy(data, xattr.xdata, len); |
| 251 |
|
| 252 |
cdata->data = data; |
| 253 |
cdata->size = len; |
| 254 |
} |
| 255 |
ret = 0; |
| 256 |
|
| 257 |
done: |
| 258 |
apfs_free_query(query); |
| 259 |
return ret; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
void apfs_release_compressed_data(struct apfs_compressed_data *cdata) |
| 267 |
{ |
| 268 |
if (!cdata) |
| 269 |
return; |
| 270 |
|
| 271 |
if (cdata->has_dstream) { |
| 272 |
kfree(cdata->dstream); |
| 273 |
cdata->dstream = NULL; |
| 274 |
} else { |
| 275 |
kfree(cdata->data); |
| 276 |
cdata->data = NULL; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
int apfs_compressed_data_read(struct apfs_compressed_data *cdata, void *buf, size_t count, u64 offset) |
| 290 |
{ |
| 291 |
if (cdata->has_dstream) |
| 292 |
return apfs_nonsparse_dstream_read(cdata->dstream, buf, count, offset); |
| 293 |
|
| 294 |
if (offset > cdata->size || count > cdata->size - offset) { |
| 295 |
apfs_err(NULL, "reading past the end (0x%llx-0x%llx)", offset, (unsigned long long)count); |
| 296 |
|
| 297 |
return -EFSCORRUPTED; |
| 298 |
} |
| 299 |
memcpy(buf, cdata->data + offset, count); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
int __apfs_xattr_get(struct inode *inode, const char *name, void *buffer, |
| 313 |
size_t size) |
| 314 |
{ |
| 315 |
return ____apfs_xattr_get(inode, name, buffer, size, true ); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
int ____apfs_xattr_get(struct inode *inode, const char *name, void *buffer, |
| 327 |
size_t size, bool only_whole) |
| 328 |
{ |
| 329 |
struct super_block *sb = inode->i_sb; |
| 330 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 331 |
struct apfs_query *query; |
| 332 |
struct apfs_xattr xattr; |
| 333 |
u64 cnid = apfs_ino(inode); |
| 334 |
int ret; |
| 335 |
|
| 336 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 337 |
if (!query) |
| 338 |
return -ENOMEM; |
| 339 |
apfs_init_xattr_key(cnid, name, &query->key); |
| 340 |
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT; |
| 341 |
|
| 342 |
ret = apfs_btree_query(sb, &query); |
| 343 |
if (ret) { |
| 344 |
if (ret != -ENODATA) |
| 345 |
apfs_err(sb, "query failed for id 0x%llx (%s)", cnid, name); |
| 346 |
goto done; |
| 347 |
} |
| 348 |
|
| 349 |
ret = apfs_xattr_from_query(query, &xattr); |
| 350 |
if (ret) { |
| 351 |
apfs_err(sb, "bad xattr record in inode 0x%llx", cnid); |
| 352 |
goto done; |
| 353 |
} |
| 354 |
|
| 355 |
if (xattr.has_dstream) |
| 356 |
ret = apfs_xattr_extents_read(inode, &xattr, buffer, size, only_whole); |
| 357 |
else |
| 358 |
ret = apfs_xattr_inline_read(&xattr, buffer, size, only_whole); |
| 359 |
|
| 360 |
done: |
| 361 |
apfs_free_query(query); |
| 362 |
return ret; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
static int apfs_xattr_get(struct inode *inode, const char *name, void *buffer, size_t size) |
| 379 |
{ |
| 380 |
struct apfs_nxsb_info *nxi = APFS_NXI(inode->i_sb); |
| 381 |
int ret; |
| 382 |
|
| 383 |
down_read(&nxi->nx_big_sem); |
| 384 |
ret = __apfs_xattr_get(inode, name, buffer, size); |
| 385 |
up_read(&nxi->nx_big_sem); |
| 386 |
if (ret > XATTR_SIZE_MAX) { |
| 387 |
apfs_warn(inode->i_sb, "xattr is too big to read on linux (%d)", ret); |
| 388 |
return -E2BIG; |
| 389 |
} |
| 390 |
return ret; |
| 391 |
} |
| 392 |
|
| 393 |
static int apfs_xattr_osx_get(const struct xattr_handler *handler, |
| 394 |
struct dentry *unused, struct inode *inode, |
| 395 |
const char *name, void *buffer, size_t size) |
| 396 |
{ |
| 397 |
|
| 398 |
return apfs_xattr_get(inode, name, buffer, size); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
static int apfs_delete_xattr(struct apfs_query *query) |
| 408 |
{ |
| 409 |
struct super_block *sb = query->node->object.sb; |
| 410 |
struct apfs_xattr xattr; |
| 411 |
struct apfs_dstream_info *dstream; |
| 412 |
int err; |
| 413 |
|
| 414 |
err = apfs_xattr_from_query(query, &xattr); |
| 415 |
if (err) { |
| 416 |
apfs_err(sb, "bad xattr record"); |
| 417 |
return err; |
| 418 |
} |
| 419 |
|
| 420 |
if (!xattr.has_dstream) |
| 421 |
return apfs_btree_remove(query); |
| 422 |
|
| 423 |
dstream = kzalloc(sizeof(*dstream), GFP_KERNEL); |
| 424 |
if (!dstream) |
| 425 |
return -ENOMEM; |
| 426 |
apfs_dstream_from_xattr(sb, &xattr, dstream); |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
err = apfs_btree_remove(query); |
| 434 |
if (err) { |
| 435 |
apfs_err(sb, "removal failed"); |
| 436 |
goto fail; |
| 437 |
} |
| 438 |
err = apfs_truncate(dstream, 0); |
| 439 |
if (err) |
| 440 |
apfs_err(sb, "truncation failed for dstream 0x%llx", dstream->ds_id); |
| 441 |
|
| 442 |
fail: |
| 443 |
kfree(dstream); |
| 444 |
return err; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
static int apfs_delete_any_xattr(struct inode *inode) |
| 456 |
{ |
| 457 |
struct super_block *sb = inode->i_sb; |
| 458 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 459 |
struct apfs_query *query; |
| 460 |
int ret; |
| 461 |
|
| 462 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 463 |
if (!query) |
| 464 |
return -ENOMEM; |
| 465 |
apfs_init_xattr_key(apfs_ino(inode), NULL , &query->key); |
| 466 |
query->flags = APFS_QUERY_CAT | APFS_QUERY_ANY_NAME | APFS_QUERY_EXACT; |
| 467 |
|
| 468 |
ret = apfs_btree_query(sb, &query); |
| 469 |
if (ret) { |
| 470 |
if (ret == -ENODATA) |
| 471 |
ret = 0; |
| 472 |
else |
| 473 |
apfs_err(sb, "query failed for ino 0x%llx", apfs_ino(inode)); |
| 474 |
goto out; |
| 475 |
} |
| 476 |
|
| 477 |
ret = apfs_delete_xattr(query); |
| 478 |
if (!ret) |
| 479 |
ret = -EAGAIN; |
| 480 |
else |
| 481 |
apfs_err(sb, "xattr deletion failed"); |
| 482 |
|
| 483 |
out: |
| 484 |
apfs_free_query(query); |
| 485 |
return ret; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
int apfs_delete_all_xattrs(struct inode *inode) |
| 495 |
{ |
| 496 |
struct apfs_nxsb_info *nxi = APFS_NXI(inode->i_sb); |
| 497 |
int ret; |
| 498 |
|
| 499 |
lockdep_assert_held_write(&nxi->nx_big_sem); |
| 500 |
|
| 501 |
do { |
| 502 |
ret = apfs_delete_any_xattr(inode); |
| 503 |
} while (ret == -EAGAIN); |
| 504 |
|
| 505 |
return ret; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
static int apfs_build_xattr_key(const char *name, u64 ino, struct apfs_xattr_key **key_p) |
| 517 |
{ |
| 518 |
struct apfs_xattr_key *key; |
| 519 |
u16 namelen = strlen(name) + 1; |
| 520 |
int key_len; |
| 521 |
|
| 522 |
key_len = sizeof(*key) + namelen; |
| 523 |
key = kmalloc(key_len, GFP_KERNEL); |
| 524 |
if (!key) |
| 525 |
return -ENOMEM; |
| 526 |
|
| 527 |
apfs_key_set_hdr(APFS_TYPE_XATTR, ino, key); |
| 528 |
key->name_len = cpu_to_le16(namelen); |
| 529 |
strscpy(key->name, name, namelen); |
| 530 |
|
| 531 |
*key_p = key; |
| 532 |
return key_len; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
static int apfs_build_dstream_xattr_val(struct apfs_dstream_info *dstream, struct apfs_xattr_val **val_p) |
| 543 |
{ |
| 544 |
struct apfs_xattr_val *val; |
| 545 |
struct apfs_xattr_dstream *dstream_raw; |
| 546 |
int val_len; |
| 547 |
|
| 548 |
val_len = sizeof(*val) + sizeof(*dstream_raw); |
| 549 |
val = kzalloc(val_len, GFP_KERNEL); |
| 550 |
if (!val) |
| 551 |
return -ENOMEM; |
| 552 |
|
| 553 |
val->flags = cpu_to_le16(APFS_XATTR_DATA_STREAM); |
| 554 |
val->xdata_len = cpu_to_le16(sizeof(*dstream_raw)); |
| 555 |
|
| 556 |
dstream_raw = (void *)val->xdata; |
| 557 |
dstream_raw->xattr_obj_id = cpu_to_le64(dstream->ds_id); |
| 558 |
dstream_raw->dstream.size = cpu_to_le64(dstream->ds_size); |
| 559 |
dstream_raw->dstream.alloced_size = cpu_to_le64(apfs_alloced_size(dstream)); |
| 560 |
if (apfs_vol_is_encrypted(dstream->ds_sb)) |
| 561 |
dstream_raw->dstream.default_crypto_id = cpu_to_le64(dstream->ds_id); |
| 562 |
|
| 563 |
*val_p = val; |
| 564 |
return val_len; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
static int apfs_build_inline_xattr_val(const void *value, size_t size, struct apfs_xattr_val **val_p) |
| 576 |
{ |
| 577 |
struct apfs_xattr_val *val; |
| 578 |
int val_len; |
| 579 |
|
| 580 |
val_len = sizeof(*val) + size; |
| 581 |
val = kmalloc(val_len, GFP_KERNEL); |
| 582 |
if (!val) |
| 583 |
return -ENOMEM; |
| 584 |
|
| 585 |
val->flags = cpu_to_le16(APFS_XATTR_DATA_EMBEDDED); |
| 586 |
val->xdata_len = cpu_to_le16(size); |
| 587 |
memcpy(val->xdata, value, size); |
| 588 |
|
| 589 |
*val_p = val; |
| 590 |
return val_len; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
static struct apfs_dstream_info *apfs_create_xattr_dstream(struct super_block *sb, const void *value, size_t size) |
| 603 |
{ |
| 604 |
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 605 |
struct apfs_dstream_info *dstream; |
| 606 |
int blkcnt, i; |
| 607 |
int err; |
| 608 |
|
| 609 |
dstream = kzalloc(sizeof(*dstream), GFP_KERNEL); |
| 610 |
if (!dstream) |
| 611 |
return ERR_PTR(-ENOMEM); |
| 612 |
dstream->ds_sb = sb; |
| 613 |
spin_lock_init(&dstream->ds_ext_lock); |
| 614 |
|
| 615 |
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o); |
| 616 |
dstream->ds_id = le64_to_cpu(vsb_raw->apfs_next_obj_id); |
| 617 |
le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1); |
| 618 |
|
| 619 |
blkcnt = (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 620 |
for (i = 0; i < blkcnt; i++) { |
| 621 |
struct buffer_head *bh; |
| 622 |
int off, tocopy; |
| 623 |
u64 bno; |
| 624 |
|
| 625 |
err = apfs_dstream_get_new_bno(dstream, i, &bno); |
| 626 |
if (err) { |
| 627 |
apfs_err(sb, "failed to get new block in dstream 0x%llx", dstream->ds_id); |
| 628 |
goto fail; |
| 629 |
} |
| 630 |
bh = apfs_sb_bread(sb, bno); |
| 631 |
if (!bh) { |
| 632 |
apfs_err(sb, "failed to read new block"); |
| 633 |
err = -EIO; |
| 634 |
goto fail; |
| 635 |
} |
| 636 |
|
| 637 |
err = apfs_transaction_join(sb, bh); |
| 638 |
if (err) { |
| 639 |
brelse(bh); |
| 640 |
goto fail; |
| 641 |
} |
| 642 |
|
| 643 |
off = i << sb->s_blocksize_bits; |
| 644 |
tocopy = min(sb->s_blocksize, (unsigned long)(size - off)); |
| 645 |
memcpy(bh->b_data, value + off, tocopy); |
| 646 |
if (tocopy < sb->s_blocksize) |
| 647 |
memset(bh->b_data + tocopy, 0, sb->s_blocksize - tocopy); |
| 648 |
brelse(bh); |
| 649 |
|
| 650 |
dstream->ds_size += tocopy; |
| 651 |
} |
| 652 |
|
| 653 |
err = apfs_flush_extent_cache(dstream); |
| 654 |
if (err) { |
| 655 |
apfs_err(sb, "extent cache flush failed for dstream 0x%llx", dstream->ds_id); |
| 656 |
goto fail; |
| 657 |
} |
| 658 |
return dstream; |
| 659 |
|
| 660 |
fail: |
| 661 |
kfree(dstream); |
| 662 |
return ERR_PTR(err); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
static int apfs_xattr_dstream_from_query(struct apfs_query *query, struct apfs_dstream_info **dstream_p) |
| 673 |
{ |
| 674 |
struct super_block *sb = query->node->object.sb; |
| 675 |
struct apfs_dstream_info *dstream = NULL; |
| 676 |
struct apfs_xattr xattr; |
| 677 |
int err; |
| 678 |
|
| 679 |
err = apfs_xattr_from_query(query, &xattr); |
| 680 |
if (err) { |
| 681 |
apfs_err(sb, "bad xattr record"); |
| 682 |
return err; |
| 683 |
} |
| 684 |
|
| 685 |
if (xattr.has_dstream) { |
| 686 |
dstream = kzalloc(sizeof(*dstream), GFP_KERNEL); |
| 687 |
if (!dstream) |
| 688 |
return -ENOMEM; |
| 689 |
apfs_dstream_from_xattr(sb, &xattr, dstream); |
| 690 |
} |
| 691 |
*dstream_p = dstream; |
| 692 |
return 0; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
int apfs_xattr_set(struct inode *inode, const char *name, const void *value, |
| 706 |
size_t size, int flags) |
| 707 |
{ |
| 708 |
struct super_block *sb = inode->i_sb; |
| 709 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 710 |
struct apfs_query *query = NULL; |
| 711 |
u64 cnid = apfs_ino(inode); |
| 712 |
int key_len, val_len; |
| 713 |
struct apfs_xattr_key *raw_key = NULL; |
| 714 |
struct apfs_xattr_val *raw_val = NULL; |
| 715 |
struct apfs_dstream_info *dstream = NULL; |
| 716 |
struct apfs_dstream_info *old_dstream = NULL; |
| 717 |
int ret; |
| 718 |
|
| 719 |
if (size > APFS_XATTR_MAX_EMBEDDED_SIZE) { |
| 720 |
dstream = apfs_create_xattr_dstream(sb, value, size); |
| 721 |
if (IS_ERR(dstream)) { |
| 722 |
apfs_err(sb, "failed to set xattr dstream for ino 0x%llx", apfs_ino(inode)); |
| 723 |
return PTR_ERR(dstream); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 728 |
if (!query) { |
| 729 |
ret = -ENOMEM; |
| 730 |
goto done; |
| 731 |
} |
| 732 |
apfs_init_xattr_key(cnid, name, &query->key); |
| 733 |
query->flags = APFS_QUERY_CAT | APFS_QUERY_EXACT; |
| 734 |
|
| 735 |
ret = apfs_btree_query(sb, &query); |
| 736 |
if (ret) { |
| 737 |
if (ret != -ENODATA) { |
| 738 |
apfs_err(sb, "query failed for id 0x%llx (%s)", cnid, name); |
| 739 |
goto done; |
| 740 |
} else if (flags & XATTR_REPLACE) { |
| 741 |
goto done; |
| 742 |
} |
| 743 |
} else if (flags & XATTR_CREATE) { |
| 744 |
ret = -EEXIST; |
| 745 |
goto done; |
| 746 |
} else if (!value) { |
| 747 |
ret = apfs_delete_xattr(query); |
| 748 |
if (ret) |
| 749 |
apfs_err(sb, "xattr deletion failed"); |
| 750 |
goto done; |
| 751 |
} else { |
| 752 |
|
| 753 |
ret = apfs_xattr_dstream_from_query(query, &old_dstream); |
| 754 |
if (ret) { |
| 755 |
apfs_err(sb, "failed to get the old dstream"); |
| 756 |
goto done; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
key_len = apfs_build_xattr_key(name, cnid, &raw_key); |
| 761 |
if (key_len < 0) { |
| 762 |
ret = key_len; |
| 763 |
goto done; |
| 764 |
} |
| 765 |
|
| 766 |
if (dstream) |
| 767 |
val_len = apfs_build_dstream_xattr_val(dstream, &raw_val); |
| 768 |
else |
| 769 |
val_len = apfs_build_inline_xattr_val(value, size, &raw_val); |
| 770 |
if (val_len < 0) { |
| 771 |
ret = val_len; |
| 772 |
goto done; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
if (strcmp(name, APFS_XATTR_NAME_SYMLINK) == 0) |
| 777 |
raw_val->flags |= cpu_to_le16(APFS_XATTR_FILE_SYSTEM_OWNED); |
| 778 |
|
| 779 |
if (ret) |
| 780 |
ret = apfs_btree_insert(query, raw_key, key_len, raw_val, val_len); |
| 781 |
else |
| 782 |
ret = apfs_btree_replace(query, raw_key, key_len, raw_val, val_len); |
| 783 |
if (ret) { |
| 784 |
apfs_err(sb, "insertion/update failed for id 0x%llx (%s)", cnid, name); |
| 785 |
goto done; |
| 786 |
} |
| 787 |
|
| 788 |
if (old_dstream) { |
| 789 |
ret = apfs_truncate(old_dstream, 0); |
| 790 |
if (ret) |
| 791 |
apfs_err(sb, "truncation failed for dstream 0x%llx", old_dstream->ds_id); |
| 792 |
} |
| 793 |
|
| 794 |
done: |
| 795 |
kfree(dstream); |
| 796 |
kfree(old_dstream); |
| 797 |
kfree(raw_val); |
| 798 |
kfree(raw_key); |
| 799 |
apfs_free_query(query); |
| 800 |
return ret; |
| 801 |
} |
| 802 |
|
| 803 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0) |
| 804 |
static int apfs_xattr_osx_set(const struct xattr_handler *handler, |
| 805 |
struct dentry *unused, struct inode *inode, const char *name, |
| 806 |
const void *value, size_t size, int flags) |
| 807 |
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0) && !RHEL_VERSION_GE(9, 6) |
| 808 |
static int apfs_xattr_osx_set(const struct xattr_handler *handler, |
| 809 |
struct user_namespace *mnt_userns, struct dentry *unused, |
| 810 |
struct inode *inode, const char *name, const void *value, |
| 811 |
size_t size, int flags) |
| 812 |
#else |
| 813 |
static int apfs_xattr_osx_set(const struct xattr_handler *handler, |
| 814 |
struct mnt_idmap *idmap, struct dentry *unused, |
| 815 |
struct inode *inode, const char *name, const void *value, |
| 816 |
size_t size, int flags) |
| 817 |
#endif |
| 818 |
{ |
| 819 |
struct super_block *sb = inode->i_sb; |
| 820 |
int err; |
| 821 |
|
| 822 |
err = apfs_transaction_start(sb, value ? APFS_TRANS_REG : APFS_TRANS_DEL); |
| 823 |
if (err) |
| 824 |
return err; |
| 825 |
|
| 826 |
|
| 827 |
err = apfs_xattr_set(inode, name, value, size, flags); |
| 828 |
if (err) |
| 829 |
goto fail; |
| 830 |
|
| 831 |
err = apfs_transaction_commit(sb); |
| 832 |
if (!err) |
| 833 |
return 0; |
| 834 |
|
| 835 |
fail: |
| 836 |
apfs_transaction_abort(sb); |
| 837 |
return err; |
| 838 |
} |
| 839 |
|
| 840 |
static const struct xattr_handler apfs_xattr_osx_handler = { |
| 841 |
.prefix = XATTR_MAC_OSX_PREFIX, |
| 842 |
.get = apfs_xattr_osx_get, |
| 843 |
.set = apfs_xattr_osx_set, |
| 844 |
}; |
| 845 |
|
| 846 |
|
| 847 |
const struct xattr_handler *apfs_xattr_handlers[] = { |
| 848 |
&apfs_xattr_osx_handler, |
| 849 |
NULL |
| 850 |
}; |
| 851 |
|
| 852 |
ssize_t apfs_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 853 |
{ |
| 854 |
struct inode *inode = d_inode(dentry); |
| 855 |
struct super_block *sb = inode->i_sb; |
| 856 |
struct apfs_sb_info *sbi = APFS_SB(sb); |
| 857 |
struct apfs_nxsb_info *nxi = APFS_NXI(sb); |
| 858 |
struct apfs_query *query; |
| 859 |
u64 cnid = apfs_ino(inode); |
| 860 |
size_t free = size; |
| 861 |
ssize_t ret; |
| 862 |
|
| 863 |
down_read(&nxi->nx_big_sem); |
| 864 |
|
| 865 |
query = apfs_alloc_query(sbi->s_cat_root, NULL ); |
| 866 |
if (!query) { |
| 867 |
ret = -ENOMEM; |
| 868 |
goto fail; |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
apfs_init_xattr_key(cnid, NULL , &query->key); |
| 873 |
query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT; |
| 874 |
|
| 875 |
while (1) { |
| 876 |
struct apfs_xattr xattr; |
| 877 |
|
| 878 |
ret = apfs_btree_query(sb, &query); |
| 879 |
if (ret == -ENODATA) { |
| 880 |
ret = size - free; |
| 881 |
break; |
| 882 |
} |
| 883 |
if (ret) { |
| 884 |
apfs_err(sb, "query failed for id 0x%llx", cnid); |
| 885 |
break; |
| 886 |
} |
| 887 |
|
| 888 |
ret = apfs_xattr_from_query(query, &xattr); |
| 889 |
if (ret) { |
| 890 |
apfs_err(sb, "bad xattr key in inode %llx", cnid); |
| 891 |
break; |
| 892 |
} |
| 893 |
|
| 894 |
if (buffer) { |
| 895 |
|
| 896 |
if (xattr.name_len + XATTR_MAC_OSX_PREFIX_LEN + 1 > |
| 897 |
free) { |
| 898 |
ret = -ERANGE; |
| 899 |
break; |
| 900 |
} |
| 901 |
memcpy(buffer, XATTR_MAC_OSX_PREFIX, |
| 902 |
XATTR_MAC_OSX_PREFIX_LEN); |
| 903 |
buffer += XATTR_MAC_OSX_PREFIX_LEN; |
| 904 |
memcpy(buffer, xattr.name, xattr.name_len + 1); |
| 905 |
buffer += xattr.name_len + 1; |
| 906 |
} |
| 907 |
free -= xattr.name_len + XATTR_MAC_OSX_PREFIX_LEN + 1; |
| 908 |
} |
| 909 |
|
| 910 |
fail: |
| 911 |
apfs_free_query(query); |
| 912 |
up_read(&nxi->nx_big_sem); |
| 913 |
return ret; |
| 914 |
} |