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 if (ea_buf_size < sizeof(struct ea_attr)) 57 goto out; 58 59 offset = 0; 60 do { 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 (!p_ea_info || ea_info_size != sizeof(struct ea_information)) { 126 kvfree(p_ea_info); 127 return -ENODATA; 128 } 129 130 ea_info_qlen = le32_to_cpu(p_ea_info->ea_query_length); 131 kvfree(p_ea_info); 132 133 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size); 134 if (!ea_buf) 135 return -ENODATA; 136 137 if (ea_info_qlen > all_ea_size) { 138 err = -EIO; 139 goto free_ea_buf; 140 } 141 142 err = ntfs_ea_lookup(ea_buf, ea_info_qlen, name, name_len, &ea_off, 143 &ea_size); 144 if (!err) { 145 p_ea = (struct ea_attr *)&ea_buf[ea_off]; 146 ea_value_len = le16_to_cpu(p_ea->ea_value_length); 147 if (!buffer) { 148 kvfree(ea_buf); 149 return ea_value_len; 150 } 151 152 if (ea_value_len > size) { 153 err = -ERANGE; 154 goto free_ea_buf; 155 } 156 157 memcpy(buffer, &p_ea->ea_name[p_ea->ea_name_length + 1], 158 ea_value_len); 159 kvfree(ea_buf); 160 return ea_value_len; 161 } 162 163 err = -ENODATA; 164 free_ea_buf: 165 kvfree(ea_buf); 166 return err; 167 } 168 169 static inline int ea_packed_size(const struct ea_attr *p_ea) 170 { 171 /* 172 * 4 bytes for header (flags and lengths) + name length + 1 + 173 * value length. 174 */ 175 return 5 + p_ea->ea_name_length + le16_to_cpu(p_ea->ea_value_length); 176 } 177 178 /* 179 * Set a new EA, and set EA_INFORMATION accordingly 180 * 181 * This is roughly the same as ZwSetEaFile() on Windows, however 182 * the "offset to next" of the last EA should not be cleared. 183 * 184 * Consistency of the new EA is first checked. 185 * 186 * EA_INFORMATION is set first, and it is restored to its former 187 * state if setting EA fails. 188 */ 189 static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len, 190 const void *value, size_t val_size, int flags, 191 __le16 *packed_ea_size) 192 { 193 struct ntfs_inode *ni = NTFS_I(inode); 194 struct ea_information *p_ea_info = NULL; 195 int ea_packed, err = 0; 196 struct ea_attr *p_ea; 197 u32 ea_info_qsize = 0; 198 char *ea_buf = NULL; 199 size_t new_ea_size = ALIGN(struct_size(p_ea, ea_name, 1 + name_len + val_size), 4); 200 s64 ea_off, ea_info_size, all_ea_size, ea_size; 201 202 if (name_len > 255) 203 return -ENAMETOOLONG; 204 205 if (ntfs_attr_exist(ni, AT_EA_INFORMATION, AT_UNNAMED, 0)) { 206 p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 207 &ea_info_size); 208 if (!p_ea_info || ea_info_size != sizeof(struct ea_information)) 209 goto out; 210 211 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size); 212 if (!ea_buf) { 213 ea_info_qsize = 0; 214 kvfree(p_ea_info); 215 goto create_ea_info; 216 } 217 218 ea_info_qsize = le32_to_cpu(p_ea_info->ea_query_length); 219 } else { 220 create_ea_info: 221 p_ea_info = kzalloc(sizeof(struct ea_information), GFP_NOFS); 222 if (!p_ea_info) 223 return -ENOMEM; 224 225 ea_info_qsize = 0; 226 err = ntfs_attr_add(ni, AT_EA_INFORMATION, AT_UNNAMED, 0, 227 (char *)p_ea_info, sizeof(struct ea_information)); 228 if (err) 229 goto out; 230 231 if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) { 232 err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0); 233 if (err) 234 goto out; 235 } 236 237 goto alloc_new_ea; 238 } 239 240 if (ea_info_qsize > all_ea_size) { 241 err = -EIO; 242 goto out; 243 } 244 245 err = ntfs_ea_lookup(ea_buf, ea_info_qsize, name, name_len, &ea_off, 246 &ea_size); 247 if (ea_info_qsize && !err) { 248 if (flags & XATTR_CREATE) { 249 err = -EEXIST; 250 goto out; 251 } 252 253 p_ea = (struct ea_attr *)(ea_buf + ea_off); 254 255 if (val_size && 256 le16_to_cpu(p_ea->ea_value_length) == val_size && 257 !memcmp(p_ea->ea_name + p_ea->ea_name_length + 1, value, 258 val_size)) 259 goto out; 260 261 le16_add_cpu(&p_ea_info->ea_length, 0 - ea_packed_size(p_ea)); 262 263 if (p_ea->flags & NEED_EA) 264 le16_add_cpu(&p_ea_info->need_ea_count, -1); 265 266 memmove((char *)p_ea, (char *)p_ea + ea_size, ea_info_qsize - (ea_off + ea_size)); 267 ea_info_qsize -= ea_size; 268 p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize); 269 270 err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0, 271 sizeof(struct ea_information), false); 272 if (err) 273 goto out; 274 275 err = ntfs_write_ea(ni, AT_EA, ea_buf, 0, ea_info_qsize, true); 276 if (err) 277 goto out; 278 279 if ((flags & XATTR_REPLACE) && !val_size) { 280 /* Remove xattr. */ 281 goto out; 282 } 283 } else { 284 if (flags & XATTR_REPLACE) { 285 err = -ENODATA; 286 goto out; 287 } 288 } 289 kvfree(ea_buf); 290 291 alloc_new_ea: 292 ea_buf = kzalloc(new_ea_size, GFP_NOFS); 293 if (!ea_buf) { 294 err = -ENOMEM; 295 goto out; 296 } 297 298 /* 299 * EA and REPARSE_POINT compatibility not checked any more, 300 * required by Windows 10, but having both may lead to 301 * problems with earlier versions. 302 */ 303 p_ea = (struct ea_attr *)ea_buf; 304 memcpy(p_ea->ea_name, name, name_len); 305 p_ea->ea_name_length = name_len; 306 p_ea->ea_name[name_len] = 0; 307 memcpy(p_ea->ea_name + name_len + 1, value, val_size); 308 p_ea->ea_value_length = cpu_to_le16(val_size); 309 p_ea->next_entry_offset = cpu_to_le32(new_ea_size); 310 311 ea_packed = le16_to_cpu(p_ea_info->ea_length) + ea_packed_size(p_ea); 312 p_ea_info->ea_length = cpu_to_le16(ea_packed); 313 p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize + new_ea_size); 314 315 if (ea_packed > 0xffff || 316 ntfs_attr_size_bounds_check(ni->vol, AT_EA, new_ea_size)) { 317 err = -EFBIG; 318 goto out; 319 } 320 321 /* 322 * no EA or EA_INFORMATION : add them 323 */ 324 if (!ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) { 325 err = ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, (char *)p_ea, 326 new_ea_size); 327 if (err) 328 goto out; 329 } else { 330 err = ntfs_write_ea(ni, AT_EA, (char *)p_ea, ea_info_qsize, 331 new_ea_size, false); 332 if (err) 333 goto out; 334 } 335 336 err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0, 337 sizeof(struct ea_information), false); 338 if (err) 339 goto out; 340 341 if (packed_ea_size) 342 *packed_ea_size = p_ea_info->ea_length; 343 mark_mft_record_dirty(ni); 344 out: 345 if (ea_info_qsize > 0) 346 NInoSetHasEA(ni); 347 else 348 NInoClearHasEA(ni); 349 350 kvfree(ea_buf); 351 kvfree(p_ea_info); 352 353 return err; 354 } 355 356 /* 357 * Check for the presence of an EA "$LXDEV" (used by WSL) 358 * and return its value as a device address 359 */ 360 int ntfs_ea_get_wsl_inode(struct inode *inode, dev_t *rdevp, unsigned int flags) 361 { 362 int err; 363 __le32 v; 364 365 if (!(flags & NTFS_VOL_UID)) { 366 /* Load uid to lxuid EA */ 367 err = ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v, 368 sizeof(v)); 369 if (err < 0) 370 return err; 371 if (err != sizeof(v)) 372 return -EIO; 373 i_uid_write(inode, le32_to_cpu(v)); 374 } 375 376 if (!(flags & NTFS_VOL_GID)) { 377 /* Load gid to lxgid EA */ 378 err = ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v, 379 sizeof(v)); 380 if (err < 0) 381 return err; 382 if (err != sizeof(v)) 383 return -EIO; 384 i_gid_write(inode, le32_to_cpu(v)); 385 } 386 387 /* Load mode to lxmod EA */ 388 err = ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, sizeof(v)); 389 if (err == sizeof(v)) { 390 inode->i_mode = le32_to_cpu(v); 391 } else { 392 /* Everyone gets all permissions. */ 393 inode->i_mode |= 0777; 394 } 395 396 /* Load mode to lxdev EA */ 397 err = ntfs_get_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v)); 398 if (err == sizeof(v)) 399 *rdevp = le32_to_cpu(v); 400 err = 0; 401 402 return err; 403 } 404 405 int ntfs_ea_set_wsl_inode(struct inode *inode, dev_t rdev, __le16 *ea_size, 406 unsigned int flags) 407 { 408 __le32 v; 409 int err; 410 411 if (flags & NTFS_EA_UID) { 412 /* Store uid to lxuid EA */ 413 v = cpu_to_le32(i_uid_read(inode)); 414 err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v, 415 sizeof(v), 0, ea_size); 416 if (err) 417 return err; 418 } 419 420 if (flags & NTFS_EA_GID) { 421 /* Store gid to lxgid EA */ 422 v = cpu_to_le32(i_gid_read(inode)); 423 err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v, 424 sizeof(v), 0, ea_size); 425 if (err) 426 return err; 427 } 428 429 if (flags & NTFS_EA_MODE) { 430 /* Store mode to lxmod EA */ 431 v = cpu_to_le32(inode->i_mode); 432 err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, 433 sizeof(v), 0, ea_size); 434 if (err) 435 return err; 436 } 437 438 if (rdev) { 439 v = cpu_to_le32(rdev); 440 err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v), 441 0, ea_size); 442 } 443 444 return err; 445 } 446 447 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size) 448 { 449 struct inode *inode = d_inode(dentry); 450 struct ntfs_inode *ni = NTFS_I(inode); 451 const struct ea_attr *p_ea; 452 s64 offset, ea_buf_size, ea_info_size; 453 int next, err = 0, ea_size; 454 u32 ea_info_qsize; 455 char *ea_buf = NULL; 456 ssize_t ret = 0; 457 struct ea_information *ea_info; 458 459 if (!NInoHasEA(ni)) 460 return 0; 461 462 mutex_lock(&NTFS_I(inode)->mrec_lock); 463 ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0, 464 &ea_info_size); 465 if (!ea_info || ea_info_size != sizeof(struct ea_information)) 466 goto out; 467 468 ea_info_qsize = le32_to_cpu(ea_info->ea_query_length); 469 470 ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &ea_buf_size); 471 if (!ea_buf) 472 goto out; 473 474 if (ea_info_qsize > ea_buf_size) 475 goto out; 476 477 if (ea_buf_size < sizeof(struct ea_attr)) 478 goto out; 479 480 offset = 0; 481 do { 482 p_ea = (const struct ea_attr *)&ea_buf[offset]; 483 next = le32_to_cpu(p_ea->next_entry_offset); 484 if (next) 485 ea_size = next; 486 else 487 ea_size = ALIGN(struct_size(p_ea, ea_name, 488 1 + p_ea->ea_name_length + 489 le16_to_cpu(p_ea->ea_value_length)), 490 4); 491 if (buffer) { 492 if (offset + ea_size > ea_info_qsize) 493 break; 494 495 if (ret + p_ea->ea_name_length + 1 > size) { 496 err = -ERANGE; 497 goto out; 498 } 499 500 if (p_ea->ea_name_length + 1 > (ea_info_qsize - offset)) 501 break; 502 503 memcpy(buffer + ret, p_ea->ea_name, p_ea->ea_name_length); 504 buffer[ret + p_ea->ea_name_length] = 0; 505 } 506 507 ret += p_ea->ea_name_length + 1; 508 offset += ea_size; 509 } while (next > 0 && offset < ea_info_qsize && 510 sizeof(struct ea_attr) < (ea_info_qsize - offset)); 511 512 out: 513 mutex_unlock(&NTFS_I(inode)->mrec_lock); 514 kvfree(ea_info); 515 kvfree(ea_buf); 516 517 return err ? err : ret; 518 } 519 520 // clang-format off 521 #define SYSTEM_DOS_ATTRIB "system.dos_attrib" 522 #define SYSTEM_NTFS_ATTRIB "system.ntfs_attrib" 523 #define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be" 524 // clang-format on 525 526 static int ntfs_getxattr(const struct xattr_handler *handler, 527 struct dentry *unused, struct inode *inode, const char *name, 528 void *buffer, size_t size) 529 { 530 struct ntfs_inode *ni = NTFS_I(inode); 531 int err; 532 533 if (NVolShutdown(ni->vol)) 534 return -EIO; 535 536 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 537 if (!buffer) { 538 err = sizeof(u8); 539 } else if (size < sizeof(u8)) { 540 err = -ENODATA; 541 } else { 542 err = sizeof(u8); 543 *(u8 *)buffer = (u8)(le32_to_cpu(ni->flags) & 0x3F); 544 } 545 goto out; 546 } 547 548 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 549 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 550 if (!buffer) { 551 err = sizeof(u32); 552 } else if (size < sizeof(u32)) { 553 err = -ENODATA; 554 } else { 555 err = sizeof(u32); 556 *(u32 *)buffer = le32_to_cpu(ni->flags); 557 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 558 *(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer); 559 } 560 goto out; 561 } 562 563 mutex_lock(&ni->mrec_lock); 564 err = ntfs_get_ea(inode, name, strlen(name), buffer, size); 565 mutex_unlock(&ni->mrec_lock); 566 567 out: 568 return err; 569 } 570 571 static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr) 572 { 573 struct ntfs_attr_search_ctx *ctx; 574 struct mft_record *m; 575 struct attr_record *a; 576 __le16 new_aflags; 577 int mp_size, mp_ofs, name_ofs, arec_size, err; 578 579 m = map_mft_record(ni); 580 if (IS_ERR(m)) 581 return PTR_ERR(m); 582 583 ctx = ntfs_attr_get_search_ctx(ni, m); 584 if (!ctx) { 585 err = -ENOMEM; 586 goto err_out; 587 } 588 589 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 590 CASE_SENSITIVE, 0, NULL, 0, ctx); 591 if (err) { 592 err = -EINVAL; 593 goto err_out; 594 } 595 596 a = ctx->attr; 597 new_aflags = ctx->attr->flags; 598 599 if (fattr & FILE_ATTR_SPARSE_FILE) 600 new_aflags |= ATTR_IS_SPARSE; 601 else 602 new_aflags &= ~ATTR_IS_SPARSE; 603 604 if (fattr & FILE_ATTR_COMPRESSED) 605 new_aflags |= ATTR_IS_COMPRESSED; 606 else 607 new_aflags &= ~ATTR_IS_COMPRESSED; 608 609 if (new_aflags == a->flags) 610 return 0; 611 612 if ((new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) == 613 (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) { 614 pr_err("file can't be sparsed and compressed\n"); 615 err = -EOPNOTSUPP; 616 goto err_out; 617 } 618 619 if (!a->non_resident) 620 goto out; 621 622 if (a->data.non_resident.data_size) { 623 pr_err("Can't change sparsed/compressed for non-empty file\n"); 624 err = -EOPNOTSUPP; 625 goto err_out; 626 } 627 628 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) 629 name_ofs = (offsetof(struct attr_record, 630 data.non_resident.compressed_size) + 631 sizeof(a->data.non_resident.compressed_size) + 7) & ~7; 632 else 633 name_ofs = (offsetof(struct attr_record, 634 data.non_resident.compressed_size) + 7) & ~7; 635 636 mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, ni->runlist.rl, 0, -1, -1); 637 if (unlikely(mp_size < 0)) { 638 err = mp_size; 639 ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err); 640 goto err_out; 641 } 642 643 mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7; 644 arec_size = (mp_ofs + mp_size + 7) & ~7; 645 646 err = ntfs_attr_record_resize(m, a, arec_size); 647 if (unlikely(err)) 648 goto err_out; 649 650 if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) { 651 a->data.non_resident.compression_unit = 0; 652 if (new_aflags & ATTR_IS_COMPRESSED || ni->vol->major_ver < 3) 653 a->data.non_resident.compression_unit = 4; 654 a->data.non_resident.compressed_size = 0; 655 ni->itype.compressed.size = 0; 656 if (a->data.non_resident.compression_unit) { 657 ni->itype.compressed.block_size = 1U << 658 (a->data.non_resident.compression_unit + 659 ni->vol->cluster_size_bits); 660 ni->itype.compressed.block_size_bits = 661 ffs(ni->itype.compressed.block_size) - 662 1; 663 ni->itype.compressed.block_clusters = 1U << 664 a->data.non_resident.compression_unit; 665 } else { 666 ni->itype.compressed.block_size = 0; 667 ni->itype.compressed.block_size_bits = 0; 668 ni->itype.compressed.block_clusters = 0; 669 } 670 671 if (new_aflags & ATTR_IS_SPARSE) { 672 NInoSetSparse(ni); 673 ni->flags |= FILE_ATTR_SPARSE_FILE; 674 } 675 676 if (new_aflags & ATTR_IS_COMPRESSED) { 677 NInoSetCompressed(ni); 678 ni->flags |= FILE_ATTR_COMPRESSED; 679 } 680 } else { 681 ni->flags &= ~(FILE_ATTR_SPARSE_FILE | FILE_ATTR_COMPRESSED); 682 a->data.non_resident.compression_unit = 0; 683 NInoClearSparse(ni); 684 NInoClearCompressed(ni); 685 } 686 687 a->name_offset = cpu_to_le16(name_ofs); 688 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs); 689 690 out: 691 a->flags = new_aflags; 692 mark_mft_record_dirty(ctx->ntfs_ino); 693 err_out: 694 if (ctx) 695 ntfs_attr_put_search_ctx(ctx); 696 unmap_mft_record(ni); 697 return err; 698 } 699 700 static int ntfs_setxattr(const struct xattr_handler *handler, 701 struct mnt_idmap *idmap, struct dentry *unused, 702 struct inode *inode, const char *name, const void *value, 703 size_t size, int flags) 704 { 705 struct ntfs_inode *ni = NTFS_I(inode); 706 int err; 707 __le32 fattr; 708 709 if (NVolShutdown(ni->vol)) 710 return -EIO; 711 712 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) { 713 if (sizeof(u8) != size) { 714 err = -EINVAL; 715 goto out; 716 } 717 fattr = cpu_to_le32(*(u8 *)value); 718 goto set_fattr; 719 } 720 721 if (!strcmp(name, SYSTEM_NTFS_ATTRIB) || 722 !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) { 723 if (size != sizeof(u32)) { 724 err = -EINVAL; 725 goto out; 726 } 727 if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) 728 fattr = cpu_to_le32(be32_to_cpu(*(__be32 *)value)); 729 else 730 fattr = cpu_to_le32(*(u32 *)value); 731 732 if (S_ISREG(inode->i_mode)) { 733 mutex_lock(&ni->mrec_lock); 734 err = ntfs_new_attr_flags(ni, fattr); 735 mutex_unlock(&ni->mrec_lock); 736 if (err) 737 goto out; 738 } 739 740 set_fattr: 741 if (S_ISDIR(inode->i_mode)) 742 fattr |= FILE_ATTR_DIRECTORY; 743 else 744 fattr &= ~FILE_ATTR_DIRECTORY; 745 746 if (ni->flags != fattr) { 747 ni->flags = fattr; 748 if (fattr & FILE_ATTR_READONLY) 749 inode->i_mode &= ~0222; 750 else 751 inode->i_mode |= 0222; 752 NInoSetFileNameDirty(ni); 753 mark_inode_dirty(inode); 754 } 755 err = 0; 756 goto out; 757 } 758 759 mutex_lock(&ni->mrec_lock); 760 err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, NULL); 761 mutex_unlock(&ni->mrec_lock); 762 763 out: 764 inode_set_ctime_current(inode); 765 mark_inode_dirty(inode); 766 return err; 767 } 768 769 static bool ntfs_xattr_user_list(struct dentry *dentry) 770 { 771 return true; 772 } 773 774 // clang-format off 775 static const struct xattr_handler ntfs_other_xattr_handler = { 776 .prefix = "", 777 .get = ntfs_getxattr, 778 .set = ntfs_setxattr, 779 .list = ntfs_xattr_user_list, 780 }; 781 782 const struct xattr_handler * const ntfs_xattr_handlers[] = { 783 &ntfs_other_xattr_handler, 784 NULL, 785 }; 786 // clang-format on 787 788 #ifdef CONFIG_NTFS_FS_POSIX_ACL 789 struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry, 790 int type) 791 { 792 struct inode *inode = d_inode(dentry); 793 struct ntfs_inode *ni = NTFS_I(inode); 794 const char *name; 795 size_t name_len; 796 struct posix_acl *acl; 797 int err; 798 void *buf; 799 800 buf = kmalloc(PATH_MAX, GFP_KERNEL); 801 if (!buf) 802 return ERR_PTR(-ENOMEM); 803 804 /* Possible values of 'type' was already checked above. */ 805 if (type == ACL_TYPE_ACCESS) { 806 name = XATTR_NAME_POSIX_ACL_ACCESS; 807 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 808 } else { 809 name = XATTR_NAME_POSIX_ACL_DEFAULT; 810 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 811 } 812 813 mutex_lock(&ni->mrec_lock); 814 err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX); 815 mutex_unlock(&ni->mrec_lock); 816 817 /* Translate extended attribute to acl. */ 818 if (err >= 0) 819 acl = posix_acl_from_xattr(&init_user_ns, buf, err); 820 else if (err == -ENODATA) 821 acl = NULL; 822 else 823 acl = ERR_PTR(err); 824 825 if (!IS_ERR(acl)) 826 set_cached_acl(inode, type, acl); 827 828 kfree(buf); 829 830 return acl; 831 } 832 833 static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap, 834 struct inode *inode, struct posix_acl *acl, 835 int type, bool init_acl) 836 { 837 const char *name; 838 size_t size, name_len; 839 void *value; 840 int err; 841 int flags; 842 umode_t mode; 843 844 if (S_ISLNK(inode->i_mode)) 845 return -EOPNOTSUPP; 846 847 mode = inode->i_mode; 848 switch (type) { 849 case ACL_TYPE_ACCESS: 850 /* Do not change i_mode if we are in init_acl */ 851 if (acl && !init_acl) { 852 err = posix_acl_update_mode(idmap, inode, &mode, &acl); 853 if (err) 854 return err; 855 } 856 name = XATTR_NAME_POSIX_ACL_ACCESS; 857 name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1; 858 break; 859 860 case ACL_TYPE_DEFAULT: 861 if (!S_ISDIR(inode->i_mode)) 862 return acl ? -EACCES : 0; 863 name = XATTR_NAME_POSIX_ACL_DEFAULT; 864 name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1; 865 break; 866 867 default: 868 return -EINVAL; 869 } 870 871 if (!acl) { 872 /* Remove xattr if it can be presented via mode. */ 873 size = 0; 874 value = NULL; 875 flags = XATTR_REPLACE; 876 } else { 877 value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS); 878 if (!value) 879 return -ENOMEM; 880 flags = 0; 881 } 882 883 mutex_lock(&NTFS_I(inode)->mrec_lock); 884 err = ntfs_set_ea(inode, name, name_len, value, size, flags, NULL); 885 mutex_unlock(&NTFS_I(inode)->mrec_lock); 886 if (err == -ENODATA && !size) 887 err = 0; /* Removing non existed xattr. */ 888 if (!err) { 889 __le16 ea_size = 0; 890 umode_t old_mode = inode->i_mode; 891 892 inode->i_mode = mode; 893 mutex_lock(&NTFS_I(inode)->mrec_lock); 894 err = ntfs_ea_set_wsl_inode(inode, 0, &ea_size, NTFS_EA_MODE); 895 if (err) { 896 ntfs_set_ea(inode, name, name_len, NULL, 0, 897 XATTR_REPLACE, NULL); 898 mutex_unlock(&NTFS_I(inode)->mrec_lock); 899 inode->i_mode = old_mode; 900 goto out; 901 } 902 mutex_unlock(&NTFS_I(inode)->mrec_lock); 903 904 set_cached_acl(inode, type, acl); 905 inode_set_ctime_current(inode); 906 mark_inode_dirty(inode); 907 } 908 909 out: 910 kfree(value); 911 912 return err; 913 } 914 915 int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 916 struct posix_acl *acl, int type) 917 { 918 return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false); 919 } 920 921 int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode, 922 struct inode *dir) 923 { 924 struct posix_acl *default_acl, *acl; 925 int err; 926 927 err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 928 if (err) 929 return err; 930 931 if (default_acl) { 932 err = ntfs_set_acl_ex(idmap, inode, default_acl, 933 ACL_TYPE_DEFAULT, true); 934 posix_acl_release(default_acl); 935 } else { 936 inode->i_default_acl = NULL; 937 } 938 939 if (acl) { 940 if (!err) 941 err = ntfs_set_acl_ex(idmap, inode, acl, 942 ACL_TYPE_ACCESS, true); 943 posix_acl_release(acl); 944 } else { 945 inode->i_acl = NULL; 946 } 947 948 return err; 949 } 950 #endif 951