1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Pocessing of EA's 4 * 5 * Part of this file is based on code from the NTFS-3G. 6 * 7 * Copyright (c) 2014-2021 Jean-Pierre Andre 8 * Copyright (c) 2025 LG Electronics Co., Ltd. 9 */ 10 11 #include <linux/fs.h> 12 #include <linux/posix_acl.h> 13 #include <linux/posix_acl_xattr.h> 14 #include <linux/xattr.h> 15 16 #include "layout.h" 17 #include "attrib.h" 18 #include "index.h" 19 #include "dir.h" 20 #include "ea.h" 21 22 static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 ea_off, 23 s64 ea_size, bool need_truncate) 24 { 25 struct inode *ea_vi; 26 int err = 0; 27 s64 written; 28 29 ea_vi = ntfs_attr_iget(VFS_I(ni), type, AT_UNNAMED, 0); 30 if (IS_ERR(ea_vi)) 31 return PTR_ERR(ea_vi); 32 33 written = ntfs_inode_attr_pwrite(ea_vi, ea_off, ea_size, value, false); 34 if (written != ea_size) 35 err = -EIO; 36 else { 37 struct ntfs_inode *ea_ni = NTFS_I(ea_vi); 38 39 if (need_truncate && ea_ni->data_size > ea_off + ea_size) 40 ntfs_attr_truncate(ea_ni, ea_off + ea_size); 41 mark_mft_record_dirty(ni); 42 } 43 44 iput(ea_vi); 45 return err; 46 } 47 48 static int ntfs_ea_lookup(char *ea_buf, s64 ea_buf_size, const char *name, 49 int name_len, s64 *ea_offset, s64 *ea_size) 50 { 51 const struct ea_attr *p_ea; 52 size_t actual_size; 53 loff_t offset, p_ea_size; 54 unsigned int next; 55 56 offset = 0; 57 do { 58 if (ea_buf_size - offset < sizeof(struct ea_attr)) 59 break; 60 61 p_ea = (const struct ea_attr *)&ea_buf[offset]; 62 next = le32_to_cpu(p_ea->next_entry_offset); 63 p_ea_size = next ? next : (ea_buf_size - offset); 64 65 if (p_ea_size < sizeof(struct ea_attr) || 66 offset + p_ea_size > ea_buf_size) 67 break; 68 69 if ((s64)p_ea->ea_name_length + 1 > 70 p_ea_size - offsetof(struct ea_attr, ea_name)) 71 break; 72 73 actual_size = ALIGN(struct_size(p_ea, ea_name, 1 + p_ea->ea_name_length + 74 le16_to_cpu(p_ea->ea_value_length)), 4); 75 if (actual_size > p_ea_size) 76 break; 77 78 if (p_ea->ea_name_length == name_len && 79 !memcmp(p_ea->ea_name, name, name_len)) { 80 *ea_offset = offset; 81 *ea_size = next ? next : actual_size; 82 83 if (ea_buf_size < *ea_offset + *ea_size) 84 goto out; 85 86 return 0; 87 } 88 offset += next; 89 } while (next > 0 && offset < ea_buf_size); 90 91 out: 92 return -ENOENT; 93 } 94 95 /* 96 * Return the existing EA 97 * 98 * The EA_INFORMATION is not examined and the consistency of the 99 * existing EA is not checked. 100 * 101 * If successful, the full attribute is returned unchanged 102 * and its size is returned. 103 * If the designated buffer is too small, the needed size is 104 * returned, and the buffer is left unchanged. 105 * If there is an error, a negative value is returned and errno 106 * is set according to the error. 107 */ 108 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len, 109 void *buffer, size_t size) 110 { 111 struct ntfs_inode *ni = NTFS_I(inode); 112 const struct ea_attr *p_ea; 113 char *ea_buf; 114 s64 ea_off, ea_size, all_ea_size, ea_info_size; 115 int err; 116 u32 ea_info_qlen; 117 u16 ea_value_len; 118 struct ea_information *p_ea_info; 119 120 if (!NInoHasEA(ni)) 121 return -ENODATA; 122 123 p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 124 &ea_info_size); 125 if (IS_ERR(p_ea_info)) 126 return PTR_ERR(p_ea_info); 127 if (ea_info_size != sizeof(struct ea_information)) { 128 kvfree(p_ea_info); 129 return -EIO; 130 } 131 132 ea_info_qlen = le32_to_cpu(p_ea_info->ea_query_length); 133 kvfree(p_ea_info); 134 135 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size); 136 if (IS_ERR(ea_buf)) 137 return PTR_ERR(ea_buf); 138 139 if (ea_info_qlen > all_ea_size) { 140 err = -EIO; 141 goto free_ea_buf; 142 } 143 144 err = ntfs_ea_lookup(ea_buf, ea_info_qlen, name, name_len, &ea_off, 145 &ea_size); 146 if (!err) { 147 p_ea = (struct ea_attr *)&ea_buf[ea_off]; 148 ea_value_len = le16_to_cpu(p_ea->ea_value_length); 149 if (!buffer) { 150 kvfree(ea_buf); 151 return ea_value_len; 152 } 153 154 if (ea_value_len > size) { 155 err = -ERANGE; 156 goto free_ea_buf; 157 } 158 159 memcpy(buffer, &p_ea->ea_name[p_ea->ea_name_length + 1], 160 ea_value_len); 161 kvfree(ea_buf); 162 return ea_value_len; 163 } 164 165 err = -ENODATA; 166 free_ea_buf: 167 kvfree(ea_buf); 168 return err; 169 } 170 171 static inline int ea_packed_size(const struct ea_attr *p_ea) 172 { 173 /* 174 * 4 bytes for header (flags and lengths) + name length + 1 + 175 * value length. 176 */ 177 return 5 + p_ea->ea_name_length + le16_to_cpu(p_ea->ea_value_length); 178 } 179 180 /* 181 * Set a new EA, and set EA_INFORMATION accordingly 182 * 183 * This is roughly the same as ZwSetEaFile() on Windows, however 184 * the "offset to next" of the last EA should not be cleared. 185 * 186 * Consistency of the new EA is first checked. 187 * 188 * EA_INFORMATION is set first, and it is restored to its former 189 * state if setting EA fails. 190 */ 191 static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len, 192 const void *value, size_t val_size, int flags, 193 __le16 *packed_ea_size) 194 { 195 struct ntfs_inode *ni = NTFS_I(inode); 196 struct ea_information *p_ea_info = NULL; 197 int ea_packed, err = 0; 198 struct ea_attr *p_ea; 199 u32 ea_info_qsize = 0; 200 char *ea_buf = NULL; 201 char *new_ea_buf; 202 char *old_ea_buf = NULL; 203 struct ea_information old_ea_info; 204 size_t new_ea_size = ALIGN(struct_size(p_ea, ea_name, 1 + name_len + val_size), 4); 205 s64 ea_off, ea_info_size, all_ea_size, ea_size; 206 207 if (name_len > 255) 208 return -ENAMETOOLONG; 209 210 if (ntfs_attr_exist(ni, AT_EA_INFORMATION, AT_UNNAMED, 0)) { 211 p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 212 &ea_info_size); 213 if (IS_ERR(p_ea_info)) { 214 err = PTR_ERR(p_ea_info); 215 p_ea_info = NULL; 216 goto out; 217 } 218 if (ea_info_size != sizeof(struct ea_information)) { 219 err = -EIO; 220 goto out; 221 } 222 223 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size); 224 if (IS_ERR(ea_buf)) { 225 err = PTR_ERR(ea_buf); 226 ea_buf = NULL; 227 goto out; 228 } 229 if (!ea_buf) { 230 ea_info_qsize = 0; 231 kvfree(p_ea_info); 232 goto create_ea_info; 233 } 234 235 ea_info_qsize = le32_to_cpu(p_ea_info->ea_query_length); 236 } else { 237 create_ea_info: 238 p_ea_info = kzalloc(sizeof(struct ea_information), GFP_NOFS); 239 if (!p_ea_info) 240 return -ENOMEM; 241 242 ea_info_qsize = 0; 243 err = ntfs_attr_add(ni, AT_EA_INFORMATION, AT_UNNAMED, 0, 244 (char *)p_ea_info, sizeof(struct ea_information)); 245 if (err) 246 goto out; 247 248 if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) { 249 err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0); 250 if (err) 251 goto out; 252 } 253 254 goto alloc_new_ea; 255 } 256 257 if (ea_info_qsize > all_ea_size) { 258 err = -EIO; 259 goto out; 260 } 261 262 err = ntfs_ea_lookup(ea_buf, ea_info_qsize, name, name_len, &ea_off, 263 &ea_size); 264 if (ea_info_qsize && !err) { 265 if (flags & XATTR_CREATE) { 266 err = -EEXIST; 267 goto out; 268 } 269 if ((flags & XATTR_REPLACE) && !val_size) { 270 old_ea_info = *p_ea_info; 271 old_ea_buf = kvmemdup(ea_buf, all_ea_size, GFP_NOFS); 272 if (!old_ea_buf) { 273 err = -ENOMEM; 274 goto out; 275 } 276 } 277 278 /* Check the final $EA size before removing the old entry. */ 279 if (val_size && 280 ntfs_attr_size_bounds_check(ni->vol, AT_EA, 281 ea_info_qsize - ea_size + new_ea_size)) { 282 err = -EFBIG; 283 goto out; 284 } 285 286 p_ea = (struct ea_attr *)(ea_buf + ea_off); 287 288 if (val_size && 289 le16_to_cpu(p_ea->ea_value_length) == val_size && 290 !memcmp(p_ea->ea_name + p_ea->ea_name_length + 1, value, 291 val_size)) 292 goto out; 293 294 le16_add_cpu(&p_ea_info->ea_length, 0 - ea_packed_size(p_ea)); 295 296 if (p_ea->flags & NEED_EA) 297 le16_add_cpu(&p_ea_info->need_ea_count, -1); 298 299 memmove((char *)p_ea, (char *)p_ea + ea_size, ea_info_qsize - (ea_off + ea_size)); 300 ea_info_qsize -= ea_size; 301 p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize); 302 303 if ((flags & XATTR_REPLACE) && !val_size && !ea_info_qsize) { 304 err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0); 305 if (err) 306 goto out; 307 308 err = ntfs_attr_remove(ni, AT_EA_INFORMATION, AT_UNNAMED, 0); 309 if (err) { 310 /* Restore the original $EA if $EA_INFORMATION removal failed. */ 311 ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, old_ea_buf, 312 all_ea_size); 313 ea_info_qsize = le32_to_cpu(old_ea_info.ea_query_length); 314 } 315 goto out; 316 } 317 318 if ((flags & XATTR_REPLACE) && !val_size) { 319 err = ntfs_write_ea(ni, AT_EA, ea_buf, 0, ea_info_qsize, 320 true); 321 if (err) { 322 ntfs_write_ea(ni, AT_EA, old_ea_buf, 0, 323 all_ea_size, false); 324 goto out; 325 } 326 327 err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 328 0, sizeof(struct ea_information), false); 329 if (err) { 330 ntfs_write_ea(ni, AT_EA, old_ea_buf, 0, 331 all_ea_size, false); 332 ntfs_write_ea(ni, AT_EA_INFORMATION, 333 (char *)&old_ea_info, 0, 334 sizeof(old_ea_info), false); 335 } 336 goto out; 337 } 338 } else { 339 if (flags & XATTR_REPLACE) { 340 err = -ENODATA; 341 goto out; 342 } 343 344 if (ntfs_attr_size_bounds_check(ni->vol, AT_EA, 345 ea_info_qsize + new_ea_size)) { 346 err = -EFBIG; 347 goto out; 348 } 349 } 350 alloc_new_ea: 351 new_ea_buf = kvzalloc(ea_info_qsize + new_ea_size, GFP_NOFS); 352 if (!new_ea_buf) { 353 err = -ENOMEM; 354 goto out; 355 } 356 if (ea_info_qsize) 357 memcpy(new_ea_buf, ea_buf, ea_info_qsize); 358 kvfree(ea_buf); 359 ea_buf = new_ea_buf; 360 p_ea = (struct ea_attr *)(ea_buf + ea_info_qsize); 361 362 /* 363 * EA and REPARSE_POINT compatibility not checked any more, 364 * required by Windows 10, but having both may lead to 365 * problems with earlier versions. 366 */ 367 memcpy(p_ea->ea_name, name, name_len); 368 p_ea->ea_name_length = name_len; 369 p_ea->ea_name[name_len] = 0; 370 memcpy(p_ea->ea_name + name_len + 1, value, val_size); 371 p_ea->ea_value_length = cpu_to_le16(val_size); 372 p_ea->next_entry_offset = cpu_to_le32(new_ea_size); 373 374 ea_packed = le16_to_cpu(p_ea_info->ea_length) + ea_packed_size(p_ea); 375 p_ea_info->ea_length = cpu_to_le16(ea_packed); 376 p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize + new_ea_size); 377 378 if (ea_packed > 0xffff) { 379 err = -EFBIG; 380 goto out; 381 } 382 383 /* 384 * no EA or EA_INFORMATION : add them 385 */ 386 if (!ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) { 387 err = ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, ea_buf, 388 ea_info_qsize + new_ea_size); 389 if (err) 390 goto out; 391 } else { 392 err = ntfs_write_ea(ni, AT_EA, ea_buf, 0, 393 ea_info_qsize + new_ea_size, true); 394 if (err) 395 goto out; 396 } 397 398 err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0, 399 sizeof(struct ea_information), false); 400 if (err) 401 goto out; 402 403 if (packed_ea_size) 404 *packed_ea_size = p_ea_info->ea_length; 405 mark_mft_record_dirty(ni); 406 out: 407 if (ea_info_qsize > 0) 408 NInoSetHasEA(ni); 409 else 410 NInoClearHasEA(ni); 411 412 kvfree(ea_buf); 413 kvfree(old_ea_buf); 414 kvfree(p_ea_info); 415 416 return err; 417 } 418 419 /* 420 * Check for the presence of an EA "$LXDEV" (used by WSL) 421 * and return its value as a device address 422 */ 423 int ntfs_ea_get_wsl_inode(struct inode *inode, dev_t *rdevp, unsigned int flags, 424 bool *has_lxmod) 425 { 426 int err; 427 __le32 v; 428 429 *has_lxmod = false; 430 431 if (!(flags & NTFS_VOL_UID)) { 432 /* Load uid to lxuid EA */ 433 err = ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v, 434 sizeof(v)); 435 if (err == sizeof(v)) 436 i_uid_write(inode, le32_to_cpu(v)); 437 } 438 439 if (!(flags & NTFS_VOL_GID)) { 440 /* Load gid to lxgid EA */ 441 err = ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v, 442 sizeof(v)); 443 if (err == sizeof(v)) 444 i_gid_write(inode, le32_to_cpu(v)); 445 } 446 447 /* Load mode to lxmod EA */ 448 err = ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, sizeof(v)); 449 if (err == sizeof(v)) { 450 inode->i_mode = le32_to_cpu(v); 451 *has_lxmod = true; 452 } else { 453 /* Everyone gets all permissions. */ 454 inode->i_mode |= 0777; 455 } 456 457 /* Load mode to lxdev EA */ 458 err = ntfs_get_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v)); 459 if (err == sizeof(v)) 460 *rdevp = le32_to_cpu(v); 461 err = 0; 462 463 return err; 464 } 465 466 int ntfs_ea_set_wsl_inode(struct inode *inode, dev_t rdev, __le16 *ea_size, 467 unsigned int flags) 468 { 469 __le32 v; 470 int err = 0; 471 472 if (ea_size) 473 *ea_size = 0; 474 475 if (flags & NTFS_EA_UID) { 476 /* Store uid to lxuid EA */ 477 v = cpu_to_le32(i_uid_read(inode)); 478 err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v, 479 sizeof(v), 0, ea_size); 480 if (err) 481 return err; 482 } 483 484 if (flags & NTFS_EA_GID) { 485 /* Store gid to lxgid EA */ 486 v = cpu_to_le32(i_gid_read(inode)); 487 err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v, 488 sizeof(v), 0, ea_size); 489 if (err) 490 return err; 491 } 492 493 if (flags & NTFS_EA_MODE) { 494 /* Store mode to lxmod EA */ 495 v = cpu_to_le32(inode->i_mode); 496 err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, 497 sizeof(v), 0, ea_size); 498 if (err) 499 return err; 500 } 501 502 if (rdev) { 503 v = cpu_to_le32(rdev); 504 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v), 505 0, ea_size); 506 } 507 508 return err; 509 } 510 511 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 512 { 513 struct inode *inode = d_inode(dentry); 514 struct ntfs_inode *ni = NTFS_I(inode); 515 const struct ea_attr *p_ea; 516 s64 offset, ea_buf_size, ea_info_size; 517 s64 ea_size; 518 u32 next; 519 int err = 0; 520 u32 ea_info_qsize; 521 char *ea_buf = NULL; 522 ssize_t ret = 0; 523 struct ea_information *ea_info; 524 525 if (!NInoHasEA(ni)) 526 return 0; 527 528 mutex_lock(&NTFS_I(inode)->mrec_lock); 529 ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 530 &ea_info_size); 531 if (IS_ERR(ea_info)) { 532 err = PTR_ERR(ea_info); 533 ea_info = NULL; 534 goto out; 535 } 536 if (ea_info_size != sizeof(struct ea_information)) { 537 err = -EIO; 538 goto out; 539 } 540 541 ea_info_qsize = le32_to_cpu(ea_info->ea_query_length); 542 543 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &ea_buf_size); 544 if (IS_ERR(ea_buf)) { 545 err = PTR_ERR(ea_buf); 546 ea_buf = NULL; 547 goto out; 548 } 549 550 if (ea_info_qsize > ea_buf_size || ea_info_qsize == 0) 551 goto out; 552 553 offset = 0; 554 do { 555 if (ea_info_qsize - offset < sizeof(struct ea_attr)) { 556 err = -EIO; 557 goto out; 558 } 559 560 p_ea = (const struct ea_attr *)&ea_buf[offset]; 561 next = le32_to_cpu(p_ea->next_entry_offset); 562 ea_size = next ? next : (ea_info_qsize - offset); 563 564 if (ea_size < sizeof(struct ea_attr) || 565 offset + ea_size > ea_info_qsize) { 566 err = -EIO; 567 goto out; 568 } 569 570 if ((int)p_ea->ea_name_length + 1 > 571 ea_size - offsetof(struct ea_attr, ea_name)) { 572 err = -EIO; 573 goto out; 574 } 575 576 if (buffer) { 577 if (ret + p_ea->ea_name_length + 1 > size) { 578 err = -ERANGE; 579 goto out; 580 } 581 582 memcpy(buffer + ret, p_ea->ea_name, p_ea->ea_name_length); 583 buffer[ret + p_ea->ea_name_length] = 0; 584 } 585 586 ret += p_ea->ea_name_length + 1; 587 offset += ea_size; 588 } while (next > 0 && offset < ea_info_qsize); 589 590 out: 591 mutex_unlock(&NTFS_I(inode)->mrec_lock); 592 kvfree(ea_info); 593 kvfree(ea_buf); 594 595 return err ? err : ret; 596 } 597 598 // clang-format off 599 #define SYSTEM_DOS_ATTRIB "system.dos_attrib" 600 #define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib" 601 #define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be" 602 // clang-format on 603 604 static int ntfs_getxattr(const struct xattr_handler *handler, 605 struct dentry *unused, struct inode *inode, const char *name, 606 void *buffer, size_t size) 607 { 608 struct ntfs_inode *ni = NTFS_I(inode); 609 int err; 610 611 if (NVolShutdown(ni->vol)) 612 return -EIO; 613 614 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 615 if (!buffer) { 616 err = sizeof(u8); 617 } else if (size < sizeof(u8)) { 618 err = -ENODATA; 619 } else { 620 err = sizeof(u8); 621 *(u8 *)buffer = (u8)(le32_to_cpu(ni->flags) & 0x3F); 622 } 623 goto out; 624 } 625 626 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 627 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 628 if (!buffer) { 629 err = sizeof(u32); 630 } else if (size < sizeof(u32)) { 631 err = -ENODATA; 632 } else { 633 err = sizeof(u32); 634 *(u32 *)buffer = le32_to_cpu(ni->flags); 635 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 636 *(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer); 637 } 638 goto out; 639 } 640 641 mutex_lock(&ni->mrec_lock); 642 err = ntfs_get_ea(inode, name, strlen(name), buffer, size); 643 mutex_unlock(&ni->mrec_lock); 644 645 out: 646 return err; 647 } 648 649 static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr) 650 { 651 struct ntfs_attr_search_ctx *ctx; 652 struct mft_record *m; 653 struct attr_record *a; 654 __le16 new_aflags; 655 u16 old_name_ofs, old_mp_ofs; 656 int mp_size, mp_ofs, name_ofs, old_arec_size, arec_size, err; 657 658 m = map_mft_record(ni); 659 if (IS_ERR(m)) 660 return PTR_ERR(m); 661 662 ctx = ntfs_attr_get_search_ctx(ni, m); 663 if (!ctx) { 664 err = -ENOMEM; 665 goto err_out; 666 } 667 668 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 669 CASE_SENSITIVE, 0, NULL, 0, ctx); 670 if (err) { 671 err = -EINVAL; 672 goto err_out; 673 } 674 675 a = ctx->attr; 676 new_aflags = ctx->attr->flags; 677 678 if (fattr & FILE_ATTR_SPARSE_FILE) 679 new_aflags |= ATTR_IS_SPARSE; 680 else 681 new_aflags &= ~ATTR_IS_SPARSE; 682 683 if (fattr & FILE_ATTR_COMPRESSED) 684 new_aflags |= ATTR_IS_COMPRESSED; 685 else 686 new_aflags &= ~ATTR_IS_COMPRESSED; 687 688 if (new_aflags == a->flags) { 689 err = 0; 690 goto err_out; 691 } 692 693 if ((new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) == 694 (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) { 695 pr_err("file can't be sparsed and compressed\n"); 696 err = -EOPNOTSUPP; 697 goto err_out; 698 } 699 700 if (!a->non_resident) { 701 if (!(new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))) { 702 err = 0; 703 goto err_out; 704 } 705 706 if (le32_to_cpu(a->data.resident.value_length)) { 707 pr_err("Can't change sparse/compressed for non-empty file"); 708 err = -EOPNOTSUPP; 709 goto err_out; 710 } 711 712 err = ntfs_attr_make_non_resident(ni, 0); 713 if (err) 714 goto err_out; 715 716 ntfs_attr_reinit_search_ctx(ctx); 717 err = ntfs_attr_lookup(ni->type, ni->name, 718 ni->name_len, CASE_SENSITIVE, 719 0, NULL, 0, ctx); 720 if (err) { 721 err = -EINVAL; 722 goto err_out; 723 } 724 a = ctx->attr; 725 } else { 726 if (a->data.non_resident.data_size) { 727 pr_err("Can't change sparsed/compressed for non-empty file"); 728 err = -EOPNOTSUPP; 729 goto err_out; 730 } 731 } 732 733 old_name_ofs = le16_to_cpu(a->name_offset); 734 old_mp_ofs = le16_to_cpu(a->data.non_resident.mapping_pairs_offset); 735 736 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) 737 name_ofs = (offsetof(struct attr_record, 738 data.non_resident.compressed_size) + 739 sizeof(a->data.non_resident.compressed_size) + 7) & ~7; 740 else 741 name_ofs = (offsetof(struct attr_record, 742 data.non_resident.compressed_size) + 7) & ~7; 743 744 mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, ni->runlist.rl, 0, -1, -1); 745 if (unlikely(mp_size < 0)) { 746 err = mp_size; 747 ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err); 748 goto err_out; 749 } 750 751 mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7; 752 arec_size = (mp_ofs + mp_size + 7) & ~7; 753 old_arec_size = le32_to_cpu(a->length); 754 755 /* 756 * Move payloads before shrinking the record. Otherwise resizing moves 757 * the following attribute over the old payload before it can be copied. 758 */ 759 if (arec_size < old_arec_size) { 760 if (a->name_length && name_ofs != old_name_ofs) 761 memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs, 762 a->name_length * sizeof(__le16)); 763 if (mp_ofs != old_mp_ofs) 764 memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size); 765 } 766 767 err = ntfs_attr_record_resize(m, a, arec_size); 768 if (unlikely(err)) 769 goto err_out; 770 771 /* 772 * When compressed/sparse state changes, the non-resident header grows or 773 * shrinks by the compressed_size field. Update the in-record payload layout 774 * to match the new offsets before exposing the new mapping_pairs_offset. 775 */ 776 if (arec_size > old_arec_size) { 777 if (mp_ofs != old_mp_ofs) 778 memmove((u8 *)a + mp_ofs, (u8 *)a + old_mp_ofs, mp_size); 779 if (a->name_length) 780 memmove((u8 *)a + name_ofs, (u8 *)a + old_name_ofs, 781 a->name_length * sizeof(__le16)); 782 } 783 784 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) { 785 a->data.non_resident.compression_unit = 0; 786 if (new_aflags & ATTR_IS_COMPRESSED || ni->vol->major_ver < 3) 787 a->data.non_resident.compression_unit = 4; 788 a->data.non_resident.compressed_size = 0; 789 ni->itype.compressed.size = 0; 790 if (a->data.non_resident.compression_unit) { 791 ni->itype.compressed.block_size = 1U << 792 (a->data.non_resident.compression_unit + 793 ni->vol->cluster_size_bits); 794 ni->itype.compressed.block_size_bits = 795 ffs(ni->itype.compressed.block_size) - 796 1; 797 ni->itype.compressed.block_clusters = 1U << 798 a->data.non_resident.compression_unit; 799 } else { 800 ni->itype.compressed.block_size = 0; 801 ni->itype.compressed.block_size_bits = 0; 802 ni->itype.compressed.block_clusters = 0; 803 } 804 } else { 805 a->data.non_resident.compression_unit = 0; 806 } 807 808 a->name_offset = cpu_to_le16(name_ofs); 809 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs); 810 811 a->flags = new_aflags; 812 813 if (new_aflags & ATTR_IS_SPARSE) { 814 NInoSetSparse(ni); 815 ni->flags |= FILE_ATTR_SPARSE_FILE; 816 } else { 817 NInoClearSparse(ni); 818 ni->flags &= ~FILE_ATTR_SPARSE_FILE; 819 } 820 821 if (new_aflags & ATTR_IS_COMPRESSED) { 822 NInoSetCompressed(ni); 823 ni->flags |= FILE_ATTR_COMPRESSED; 824 } else { 825 NInoClearCompressed(ni); 826 ni->flags &= ~FILE_ATTR_COMPRESSED; 827 } 828 829 mark_mft_record_dirty(ctx->ntfs_ino); 830 err_out: 831 if (ctx) 832 ntfs_attr_put_search_ctx(ctx); 833 unmap_mft_record(ni); 834 return err; 835 } 836 837 static bool ntfs_is_reserved_lxattr(const char *name) 838 { 839 return !strcmp(name, "$LXUID") || !strcmp(name, "$LXGID") || 840 !strcmp(name, "$LXMOD") || !strcmp(name, "$LXDEV"); 841 } 842 843 static int ntfs_validate_fattr(struct ntfs_inode *ni, __le32 fattr) 844 { 845 const __le32 wof_flags = FILE_ATTR_SPARSE_FILE | 846 FILE_ATTR_REPARSE_POINT; 847 848 if (NInoWofCompressed(ni) && ((ni->flags ^ fattr) & wof_flags)) 849 return -EPERM; 850 851 return 0; 852 } 853 854 static int ntfs_setxattr(const struct xattr_handler *handler, 855 struct mnt_idmap *idmap, struct dentry *unused, 856 struct inode *inode, const char *name, const void *value, 857 size_t size, int flags) 858 { 859 struct ntfs_inode *ni = NTFS_I(inode); 860 int err; 861 __le32 fattr; 862 863 if (NVolShutdown(ni->vol)) 864 return -EIO; 865 866 if (ntfs_is_reserved_lxattr(name) && !capable(CAP_SYS_ADMIN)) 867 return -EPERM; 868 869 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 870 if (sizeof(u8) != size) { 871 err = -EINVAL; 872 goto out; 873 } 874 fattr = cpu_to_le32((le32_to_cpu(ni->flags) & ~0xffU) | 875 *(u8 *)value); 876 goto set_fattr; 877 } 878 879 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 880 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 881 if (size != sizeof(u32)) { 882 err = -EINVAL; 883 goto out; 884 } 885 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 886 fattr = cpu_to_le32(be32_to_cpu(*(__be32 *)value)); 887 else 888 fattr = cpu_to_le32(*(u32 *)value); 889 890 err = ntfs_validate_fattr(ni, fattr); 891 if (err) 892 goto out; 893 894 if (S_ISREG(inode->i_mode)) { 895 mutex_lock(&ni->mrec_lock); 896 err = ntfs_new_attr_flags(ni, fattr); 897 mutex_unlock(&ni->mrec_lock); 898 if (err) 899 goto out; 900 } 901 902 set_fattr: 903 if (S_ISDIR(inode->i_mode)) 904 fattr |= FILE_ATTR_DIRECTORY; 905 else 906 fattr &= ~FILE_ATTR_DIRECTORY; 907 908 err = ntfs_validate_fattr(ni, fattr); 909 if (err) 910 goto out; 911 912 if (ni->flags != fattr) { 913 ni->flags = fattr; 914 if (fattr & FILE_ATTR_READONLY) 915 inode->i_mode &= ~0222; 916 else 917 inode->i_mode |= 0222; 918 NInoSetFileNameDirty(ni); 919 mark_inode_dirty(inode); 920 } 921 err = 0; 922 goto out; 923 } 924 925 mutex_lock(&ni->mrec_lock); 926 err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, NULL); 927 mutex_unlock(&ni->mrec_lock); 928 929 out: 930 if (!err) { 931 inode_set_ctime_current(inode); 932 mark_inode_dirty(inode); 933 } 934 return err; 935 } 936 937 static bool ntfs_xattr_user_list(struct dentry *dentry) 938 { 939 return true; 940 } 941 942 // clang-format off 943 static const struct xattr_handler ntfs_other_xattr_handler = { 944 .prefix = "", 945 .get = ntfs_getxattr, 946 .set = ntfs_setxattr, 947 .list = ntfs_xattr_user_list, 948 }; 949 950 const struct xattr_handler * const ntfs_xattr_handlers[] = { 951 &ntfs_other_xattr_handler, 952 NULL, 953 }; 954 // clang-format on 955 956 #ifdef CONFIG_NTFS_FS_POSIX_ACL 957 struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry, 958 int type) 959 { 960 struct inode *inode = d_inode(dentry); 961 struct ntfs_inode *ni = NTFS_I(inode); 962 const char *name; 963 size_t name_len; 964 struct posix_acl *acl; 965 int err; 966 void *buf; 967 968 buf = kmalloc(PATH_MAX, GFP_KERNEL); 969 if (!buf) 970 return ERR_PTR(-ENOMEM); 971 972 /* Possible values of 'type' was already checked above. */ 973 if (type == ACL_TYPE_ACCESS) { 974 name = XATTR_NAME_POSIX_ACL_ACCESS; 975 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 976 } else { 977 name = XATTR_NAME_POSIX_ACL_DEFAULT; 978 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 979 } 980 981 mutex_lock(&ni->mrec_lock); 982 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX); 983 mutex_unlock(&ni->mrec_lock); 984 985 /* Translate extended attribute to acl. */ 986 if (err >= 0) 987 acl = posix_acl_from_xattr(&init_user_ns, buf, err); 988 else if (err == -ENODATA) 989 acl = NULL; 990 else 991 acl = ERR_PTR(err); 992 993 if (!IS_ERR(acl)) 994 set_cached_acl(inode, type, acl); 995 996 kfree(buf); 997 998 return acl; 999 } 1000 1001 static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap, 1002 struct inode *inode, struct posix_acl *acl, 1003 int type, bool init_acl) 1004 { 1005 const char *name; 1006 size_t size, name_len; 1007 void *value; 1008 int err; 1009 int flags; 1010 umode_t mode; 1011 1012 if (S_ISLNK(inode->i_mode)) 1013 return -EOPNOTSUPP; 1014 1015 mode = inode->i_mode; 1016 switch (type) { 1017 case ACL_TYPE_ACCESS: 1018 /* Do not change i_mode if we are in init_acl */ 1019 if (acl && !init_acl) { 1020 err = posix_acl_update_mode(idmap, inode, &mode, &acl); 1021 if (err) 1022 return err; 1023 } 1024 name = XATTR_NAME_POSIX_ACL_ACCESS; 1025 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 1026 break; 1027 1028 case ACL_TYPE_DEFAULT: 1029 if (!S_ISDIR(inode->i_mode)) 1030 return acl ? -EACCES : 0; 1031 name = XATTR_NAME_POSIX_ACL_DEFAULT; 1032 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 1033 break; 1034 1035 default: 1036 return -EINVAL; 1037 } 1038 1039 if (!acl) { 1040 /* Remove xattr if it can be presented via mode. */ 1041 size = 0; 1042 value = NULL; 1043 flags = XATTR_REPLACE; 1044 } else { 1045 value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 1046 if (!value) 1047 return -ENOMEM; 1048 flags = 0; 1049 } 1050 1051 mutex_lock(&NTFS_I(inode)->mrec_lock); 1052 err = ntfs_set_ea(inode, name, name_len, value, size, flags, NULL); 1053 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1054 if (err == -ENODATA && !size) 1055 err = 0; /* Removing non existed xattr. */ 1056 if (!err) { 1057 __le16 ea_size = 0; 1058 umode_t old_mode = inode->i_mode; 1059 1060 inode->i_mode = mode; 1061 mutex_lock(&NTFS_I(inode)->mrec_lock); 1062 err = ntfs_ea_set_wsl_inode(inode, 0, &ea_size, NTFS_EA_MODE); 1063 if (err) { 1064 ntfs_set_ea(inode, name, name_len, NULL, 0, 1065 XATTR_REPLACE, NULL); 1066 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1067 inode->i_mode = old_mode; 1068 goto out; 1069 } 1070 mutex_unlock(&NTFS_I(inode)->mrec_lock); 1071 1072 set_cached_acl(inode, type, acl); 1073 inode_set_ctime_current(inode); 1074 mark_inode_dirty(inode); 1075 } 1076 1077 out: 1078 kfree(value); 1079 1080 return err; 1081 } 1082 1083 int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 1084 struct posix_acl *acl, int type) 1085 { 1086 return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false); 1087 } 1088 1089 int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode, 1090 struct inode *dir) 1091 { 1092 struct posix_acl *default_acl, *acl; 1093 int err; 1094 1095 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 1096 if (err) 1097 return err; 1098 1099 if (default_acl) { 1100 err = ntfs_set_acl_ex(idmap, inode, default_acl, 1101 ACL_TYPE_DEFAULT, true); 1102 posix_acl_release(default_acl); 1103 } else { 1104 inode->i_default_acl = NULL; 1105 } 1106 1107 if (acl) { 1108 if (!err) 1109 err = ntfs_set_acl_ex(idmap, inode, acl, 1110 ACL_TYPE_ACCESS, true); 1111 posix_acl_release(acl); 1112 } else { 1113 inode->i_acl = NULL; 1114 } 1115 1116 return err; 1117 } 1118 #endif 1119