1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5 * 6 */ 7 8 #include <linux/buffer_head.h> 9 #include <linux/fs.h> 10 #include <linux/mpage.h> 11 #include <linux/namei.h> 12 #include <linux/nls.h> 13 #include <linux/uio.h> 14 #include <linux/writeback.h> 15 #include <linux/iomap.h> 16 17 #include "debug.h" 18 #include "ntfs.h" 19 #include "ntfs_fs.h" 20 #include <trace/events/ntfs3.h> 21 22 struct IGET5_PARAM { 23 const struct MFT_REF *ref; 24 const struct cpu_str *name; 25 }; 26 27 /* 28 * ntfs_read_mft - Read record and parse MFT. 29 */ 30 static int ntfs_read_mft(struct inode *inode, const struct cpu_str *name, 31 const struct MFT_REF *ref) 32 { 33 int err = 0; 34 struct ntfs_inode *ni = ntfs_i(inode); 35 struct super_block *sb = inode->i_sb; 36 struct ntfs_sb_info *sbi = sb->s_fs_info; 37 mode_t mode = 0; 38 struct ATTR_STD_INFO5 *std5 = NULL; 39 struct ATTR_LIST_ENTRY *le; 40 struct ATTRIB *attr; 41 bool is_match = false; 42 bool is_root = false; 43 bool is_dir; 44 u64 ino = inode->i_ino; 45 u32 rp_fa = 0, asize, t32; 46 u16 roff, rsize, names = 0, links = 0; 47 const struct ATTR_FILE_NAME *fname = NULL; 48 const struct INDEX_ROOT *root = NULL; 49 struct REPARSE_DATA_BUFFER rp; // 0x18 bytes 50 u64 t64; 51 struct MFT_REC *rec; 52 struct runs_tree *run; 53 struct timespec64 ts; 54 const __le16 *aname; 55 56 inode->i_op = NULL; 57 /* Setup 'uid' and 'gid' */ 58 inode->i_uid = sbi->options->fs_uid; 59 inode->i_gid = sbi->options->fs_gid; 60 61 err = mi_init(&ni->mi, sbi, ino); 62 if (err) 63 goto out; 64 65 if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) { 66 t64 = sbi->mft.lbo >> sbi->cluster_bits; 67 t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size); 68 sbi->mft.ni = ni; 69 init_rwsem(&ni->file.run_lock); 70 71 if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) { 72 err = -ENOMEM; 73 goto out; 74 } 75 } 76 77 err = mi_read(&ni->mi, ino == MFT_REC_MFT); 78 79 if (err) 80 goto out; 81 82 rec = ni->mi.mrec; 83 84 if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) { 85 ; 86 } else if (ref->seq != rec->seq) { 87 err = -EINVAL; 88 ntfs_err(sb, "MFT: r=%llx, expect seq=%x instead of %x!", ino, 89 le16_to_cpu(ref->seq), le16_to_cpu(rec->seq)); 90 goto out; 91 } else if (!is_rec_inuse(rec)) { 92 err = -ESTALE; 93 ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino); 94 goto out; 95 } 96 97 if (le32_to_cpu(rec->total) != sbi->record_size) { 98 /* Bad inode? */ 99 err = -EINVAL; 100 goto out; 101 } 102 103 if (!is_rec_base(rec)) { 104 err = -EINVAL; 105 goto out; 106 } 107 108 /* Record should contain $I30 root. */ 109 is_dir = rec->flags & RECORD_FLAG_DIR; 110 111 /* MFT_REC_MFT is not a dir */ 112 if (is_dir && ino == MFT_REC_MFT) { 113 err = -EINVAL; 114 goto out; 115 } 116 117 inode->i_generation = le16_to_cpu(rec->seq); 118 119 /* Enumerate all struct Attributes MFT. */ 120 le = NULL; 121 attr = NULL; 122 123 /* 124 * To reduce tab pressure use goto instead of 125 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) )) 126 */ 127 next_attr: 128 run = NULL; 129 err = -EINVAL; 130 attr = ni_enum_attr_ex(ni, attr, &le, NULL); 131 if (!attr) 132 goto end_enum; 133 134 if (le && le->vcn) { 135 /* This is non primary attribute segment. Ignore if not MFT. */ 136 if (ino != MFT_REC_MFT) 137 goto next_attr; 138 139 if (attr->type == ATTR_DATA) 140 run = &ni->file.run; 141 else if (attr->type == ATTR_BITMAP) 142 run = &sbi->mft.bitmap.run; 143 else 144 goto next_attr; 145 146 asize = le32_to_cpu(attr->size); 147 goto attr_unpack_run; 148 } 149 150 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off); 151 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size); 152 asize = le32_to_cpu(attr->size); 153 aname = attr_name(attr); 154 155 /* 156 * Really this check was done in 'ni_enum_attr_ex' -> ... 'mi_enum_attr'. 157 * There not critical to check this case again 158 */ 159 if (attr->name_len && 160 sizeof(short) * attr->name_len + le16_to_cpu(attr->name_off) > 161 asize) 162 goto out; 163 164 if (attr->non_res) { 165 t64 = le64_to_cpu(attr->nres.alloc_size); 166 if (le64_to_cpu(attr->nres.data_size) > t64 || 167 le64_to_cpu(attr->nres.valid_size) > t64) 168 goto out; 169 } 170 171 switch (attr->type) { 172 case ATTR_STD: 173 if (attr->non_res || 174 asize < sizeof(struct ATTR_STD_INFO) + roff || 175 rsize < sizeof(struct ATTR_STD_INFO)) 176 goto out; 177 178 if (std5) 179 goto next_attr; 180 181 std5 = Add2Ptr(attr, roff); 182 183 nt2kernel(std5->cr_time, &ni->i_crtime); 184 nt2kernel(std5->a_time, &ts); 185 inode_set_atime_to_ts(inode, ts); 186 nt2kernel(std5->c_time, &ts); 187 inode_set_ctime_to_ts(inode, ts); 188 nt2kernel(std5->m_time, &ts); 189 inode_set_mtime_to_ts(inode, ts); 190 191 ni->std_fa = std5->fa; 192 193 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff && 194 rsize >= sizeof(struct ATTR_STD_INFO5)) 195 ni->std_security_id = std5->security_id; 196 goto next_attr; 197 198 case ATTR_LIST: 199 if (attr->name_len || le || ino == MFT_REC_LOG) 200 goto out; 201 202 err = ntfs_load_attr_list(ni, attr); 203 if (err) 204 goto out; 205 206 le = NULL; 207 attr = NULL; 208 goto next_attr; 209 210 case ATTR_NAME: 211 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff || 212 rsize < SIZEOF_ATTRIBUTE_FILENAME) 213 goto out; 214 215 names += 1; 216 fname = Add2Ptr(attr, roff); 217 218 /* Make sure the full name fits in the resident data. */ 219 if (rsize < fname_full_size(fname)) 220 goto out; 221 222 if (fname->type == FILE_NAME_DOS) 223 goto next_attr; 224 225 links += 1; 226 if (name && name->len == fname->name_len && 227 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len, 228 NULL, false)) 229 is_match = true; 230 231 goto next_attr; 232 233 case ATTR_DATA: 234 if (is_dir) { 235 /* Ignore data attribute in dir record. */ 236 goto next_attr; 237 } 238 239 if (ino == MFT_REC_BADCLUST && !attr->non_res) 240 goto next_attr; 241 242 if (attr->name_len && 243 ((ino != MFT_REC_BADCLUST || !attr->non_res || 244 attr->name_len != ARRAY_SIZE(BAD_NAME) || 245 memcmp(aname, BAD_NAME, sizeof(BAD_NAME))) && 246 (ino != MFT_REC_SECURE || !attr->non_res || 247 attr->name_len != ARRAY_SIZE(SDS_NAME) || 248 memcmp(aname, SDS_NAME, sizeof(SDS_NAME))))) { 249 /* File contains stream attribute. Ignore it. */ 250 goto next_attr; 251 } 252 253 if (is_attr_sparsed(attr)) 254 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE; 255 else 256 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE; 257 258 if (is_attr_compressed(attr)) 259 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED; 260 else 261 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED; 262 263 if (is_attr_encrypted(attr)) 264 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED; 265 else 266 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED; 267 268 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv); 269 270 if (!attr->non_res) { 271 ni->i_valid = inode->i_size = rsize; 272 inode_set_bytes(inode, rsize); 273 ni->ni_flags |= NI_FLAG_RESIDENT; 274 goto next_attr; 275 } 276 277 inode_set_bytes(inode, attr_ondisk_size(attr)); 278 279 ni->i_valid = le64_to_cpu(attr->nres.valid_size); 280 inode->i_size = le64_to_cpu(attr->nres.data_size); 281 if (!attr->nres.alloc_size) 282 goto next_attr; 283 284 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run : 285 &ni->file.run; 286 break; 287 288 case ATTR_ROOT: 289 if (attr->non_res || asize < sizeof(struct INDEX_ROOT) + roff || 290 rsize < sizeof(struct INDEX_ROOT)) 291 goto out; 292 293 root = Add2Ptr(attr, roff); 294 295 if (attr->name_len != ARRAY_SIZE(I30_NAME) || 296 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME))) 297 goto next_attr; 298 299 if (root->type != ATTR_NAME || 300 root->rule != NTFS_COLLATION_TYPE_FILENAME) 301 goto out; 302 303 if (!is_dir) 304 goto next_attr; 305 306 is_root = true; 307 ni->ni_flags |= NI_FLAG_DIR; 308 309 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30); 310 if (err) 311 goto out; 312 313 mode = sb->s_root ? 314 (S_IFDIR | (0777 & sbi->options->fs_dmask_inv)) : 315 (S_IFDIR | 0777); 316 goto next_attr; 317 318 case ATTR_ALLOC: 319 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) || 320 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME))) 321 goto next_attr; 322 323 inode->i_size = le64_to_cpu(attr->nres.data_size); 324 ni->i_valid = le64_to_cpu(attr->nres.valid_size); 325 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size)); 326 327 run = &ni->dir.alloc_run; 328 break; 329 330 case ATTR_BITMAP: 331 if (ino == MFT_REC_MFT) { 332 if (!attr->non_res) 333 goto out; 334 #ifndef CONFIG_NTFS3_64BIT_CLUSTER 335 /* 0x20000000 = 2^32 / 8 */ 336 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000) 337 goto out; 338 #endif 339 run = &sbi->mft.bitmap.run; 340 break; 341 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) && 342 !memcmp(attr_name(attr), I30_NAME, 343 sizeof(I30_NAME)) && 344 attr->non_res) { 345 run = &ni->dir.bitmap_run; 346 break; 347 } 348 goto next_attr; 349 350 case ATTR_REPARSE: 351 if (attr->name_len) 352 goto next_attr; 353 354 rp_fa = ni_parse_reparse(ni, attr, &rp); 355 switch (rp_fa) { 356 case REPARSE_LINK: 357 /* 358 * Normal symlink. 359 * Assume one unicode symbol == one utf8. 360 */ 361 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer 362 .PrintNameLength) / 363 sizeof(u16); 364 ni->i_valid = inode->i_size; 365 /* Clear directory bit. */ 366 if (ni->ni_flags & NI_FLAG_DIR) { 367 indx_clear(&ni->dir); 368 memset(&ni->dir, 0, sizeof(ni->dir)); 369 ni->ni_flags &= ~NI_FLAG_DIR; 370 } else { 371 run_close(&ni->file.run); 372 } 373 mode = S_IFLNK | 0777; 374 is_dir = false; 375 if (attr->non_res) { 376 run = &ni->file.run; 377 goto attr_unpack_run; // Double break. 378 } 379 break; 380 381 case REPARSE_COMPRESSED: 382 break; 383 384 case REPARSE_DEDUPLICATED: 385 break; 386 } 387 goto next_attr; 388 389 case ATTR_EA_INFO: 390 if (!attr->name_len && 391 resident_data_ex(attr, sizeof(struct EA_INFO))) { 392 ni->ni_flags |= NI_FLAG_EA; 393 /* 394 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode 395 */ 396 inode->i_mode = mode; 397 ntfs_get_wsl_perm(inode); 398 mode = inode->i_mode; 399 } 400 goto next_attr; 401 402 default: 403 goto next_attr; 404 } 405 406 attr_unpack_run: 407 roff = le16_to_cpu(attr->nres.run_off); 408 409 if (roff > asize) { 410 err = -EINVAL; 411 goto out; 412 } 413 414 t64 = le64_to_cpu(attr->nres.svcn); 415 416 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn), 417 t64, Add2Ptr(attr, roff), asize - roff); 418 if (err < 0) 419 goto out; 420 err = 0; 421 goto next_attr; 422 423 end_enum: 424 425 if (!std5) 426 goto out; 427 428 if (is_bad_inode(inode)) 429 goto out; 430 431 if (!is_match && name) { 432 err = -ENOENT; 433 goto out; 434 } 435 436 if (std5->fa & FILE_ATTRIBUTE_READONLY) 437 mode &= ~0222; 438 439 if (!names) { 440 err = -EINVAL; 441 goto out; 442 } 443 444 if (names != le16_to_cpu(rec->hard_links)) { 445 /* Correct minor error on the fly. Do not mark inode as dirty. */ 446 ntfs_inode_warn(inode, "Correct links count -> %u.", names); 447 rec->hard_links = cpu_to_le16(names); 448 ni->mi.dirty = true; 449 } 450 451 if (!links) { 452 err = -EINVAL; 453 goto out; 454 } 455 456 set_nlink(inode, links); 457 458 if (S_ISDIR(mode)) { 459 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY; 460 461 /* 462 * Dot and dot-dot should be included in count but was not 463 * included in enumeration. 464 * Usually a hard links to directories are disabled. 465 */ 466 inode->i_op = &ntfs_dir_inode_operations; 467 inode->i_fop = &ntfs_dir_operations; 468 ni->i_valid = 0; 469 } else if (S_ISLNK(mode)) { 470 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY; 471 inode->i_op = &ntfs_link_inode_operations; 472 inode->i_fop = NULL; 473 inode_nohighmem(inode); 474 } else if (S_ISREG(mode)) { 475 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY; 476 inode->i_op = &ntfs_file_inode_operations; 477 inode->i_fop = &ntfs_file_operations; 478 inode->i_mapping->a_ops = is_compressed(ni) ? &ntfs_aops_cmpr : 479 &ntfs_aops; 480 if (ino != MFT_REC_MFT) 481 init_rwsem(&ni->file.run_lock); 482 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) || 483 S_ISSOCK(mode)) { 484 inode->i_op = &ntfs_special_inode_operations; 485 init_special_inode(inode, mode, inode->i_rdev); 486 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) && 487 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) { 488 /* Records in $Extend are not a files or general directories. */ 489 inode->i_op = &ntfs_file_inode_operations; 490 mode = S_IFREG; 491 init_rwsem(&ni->file.run_lock); 492 } else { 493 err = -EINVAL; 494 goto out; 495 } 496 497 if ((sbi->options->sys_immutable && 498 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) && 499 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) { 500 inode->i_flags |= S_IMMUTABLE; 501 } else { 502 inode->i_flags &= ~S_IMMUTABLE; 503 } 504 505 inode->i_mode = mode; 506 if (!(ni->ni_flags & NI_FLAG_EA)) { 507 /* If no xattr then no security (stored in xattr). */ 508 inode->i_flags |= S_NOSEC; 509 } 510 511 if (ino == MFT_REC_MFT && !sb->s_root) 512 sbi->mft.ni = NULL; 513 514 return 0; 515 516 out: 517 if (ino == MFT_REC_MFT && !sb->s_root) 518 sbi->mft.ni = NULL; 519 520 return err; 521 } 522 523 /* 524 * ntfs_init_ads_node 525 * 526 * This function scans base inode for given ADS. 527 * And init inode associated with this ADS 528 */ 529 static int ntfs_init_ads_node(struct inode *inode, const __le16 *ads_name, 530 u8 ads_len, u32 flags) 531 { 532 int err = -EINVAL; 533 struct ntfs_inode *ni = ntfs_i(inode); 534 struct ntfs_inode *nb = ni->base; 535 struct ntfs_sb_info *sbi = nb->mi.sbi; 536 struct ATTR_LIST_ENTRY *le = NULL; 537 struct ATTRIB *attr = NULL; 538 u16 roff, asize; 539 u64 svcn; 540 541 if (nb->ni_flags & NI_FLAG_DIR) 542 return -EINVAL; /* no ADS for directories. */ 543 544 ni->mi.sbi = sbi; 545 ni->mi.rno = inode->i_ino; 546 547 if (ads_len == ARRAY_SIZE(QUERY_STREAMS) && 548 !memcmp(ads_name, QUERY_STREAMS, sizeof(QUERY_STREAMS))) { 549 goto ok; /* use goto to reduce tab pressure. */ 550 } 551 552 /* Enumerate all attributes in record. */ 553 while ((attr = ni_enum_attr_ex(nb, attr, &le, NULL))) { 554 if (attr->type == ATTR_DATA && attr->name_len && 555 ads_len == attr->name_len && 556 !memcmp(ads_name, attr_name(attr), ads_len * sizeof(u16))) { 557 /* We have found the ADS to open. */ 558 break; 559 } 560 } 561 562 if (!attr) { 563 if (!(flags & LOOKUP_CREATE)) { 564 /* Do not create ADS. */ 565 return -ENOENT; 566 } 567 568 /* Create new ADS. */ 569 err = ni_insert_resident(nb, 0, ATTR_DATA, ads_name, ads_len, 570 &attr, NULL, NULL); 571 if (err) { 572 /* Looks like the only reasons: ENOSPC/ENOMEM .*/ 573 return err; 574 } 575 } 576 577 if (is_attr_sparsed(attr)) 578 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE; 579 else 580 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE; 581 582 if (is_attr_compressed(attr)) 583 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED; 584 else 585 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED; 586 587 if (is_attr_encrypted(attr)) 588 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED; 589 else 590 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED; 591 592 if (!attr->non_res) { 593 ni->ni_flags |= NI_FLAG_RESIDENT; 594 ni->i_valid = inode->i_size = le32_to_cpu(attr->res.data_size); 595 inode_set_bytes(inode, inode->i_size); 596 goto ok; 597 } 598 599 inode_set_bytes(inode, attr_ondisk_size(attr)); 600 ni->i_valid = le64_to_cpu(attr->nres.valid_size); 601 inode->i_size = le64_to_cpu(attr->nres.data_size); 602 603 if (!attr->nres.alloc_size) 604 goto ok; 605 606 roff = le16_to_cpu(attr->nres.run_off); 607 asize = le32_to_cpu(attr->size); 608 609 if (roff > asize) { 610 /* This case should be checked in mi_enum_attr */ 611 return -EINVAL; 612 } 613 614 svcn = le64_to_cpu(attr->nres.svcn); 615 err = run_unpack_ex(&ni->file.run, sbi, ni->mi.rno, svcn, 616 le64_to_cpu(attr->nres.evcn), svcn, 617 Add2Ptr(attr, roff), asize - roff); 618 if (err < 0) { 619 /* run_unpack_ex marks volume dirty, if logical error. */ 620 return err; 621 } 622 623 ok: 624 /* Keep ADS name (little endian). */ 625 ni->file.ads.name = kmemdup(ads_name, ads_len * sizeof(u16), GFP_NOFS); 626 if (!ni->file.ads.name) 627 return -ENOMEM; 628 ni->file.ads.len = ads_len; 629 630 set_nlink(inode, 1); 631 632 init_rwsem(&ni->file.run_lock); 633 /* Most fields are the same as the base's? */ 634 inode->i_op = nb->vfs_inode.i_op; 635 inode->i_fop = nb->vfs_inode.i_fop; 636 inode->i_mapping->a_ops = nb->vfs_inode.i_mapping->a_ops; 637 inode->i_flags = nb->vfs_inode.i_flags; 638 inode->i_mode = nb->vfs_inode.i_mode; 639 inode->i_uid = nb->vfs_inode.i_uid; 640 inode->i_gid = nb->vfs_inode.i_gid; 641 inode->i_generation = nb->vfs_inode.i_generation; 642 643 return 0; 644 } 645 646 /* 647 * ntfs_test_inode 648 * 649 * Return: 1 if match. 650 */ 651 static int ntfs_test_inode(struct inode *inode, void *data) 652 { 653 const struct IGET5_PARAM *ig5 = data; 654 struct ntfs_inode *ni; 655 const struct cpu_str *name; 656 657 if (ino_get(ig5->ref) != inode->i_ino) 658 return 0; 659 660 ni = ntfs_i(inode); 661 662 if (ni->ni_flags & NI_FLAG_DIR) { 663 /* No ads for directories. */ 664 return 1; 665 } 666 667 name = ig5->name; 668 if (!name || !name->ads_len) { 669 if (!ni->file.ads.len) { 670 /* default file (not ads) match. */ 671 return 1; 672 } 673 } else if (ni->file.ads.len == name->ads_len && 674 !memcmp(ni->file.ads.name, &name->name[name->len + 1], 675 name->ads_len * sizeof(u16))) { 676 /* ads name match. */ 677 return 1; 678 } 679 680 return 0; 681 } 682 683 static int ntfs_set_inode(struct inode *inode, void *data) 684 { 685 const struct IGET5_PARAM *ig5 = data; 686 687 inode->i_ino = ino_get(ig5->ref); 688 return 0; 689 } 690 691 struct inode *ntfs_iget5_flags(struct super_block *sb, 692 const struct MFT_REF *ref, 693 const struct cpu_str *name, u32 flags) 694 { 695 int err; 696 /* Pack params to pass in iget5_locked. */ 697 struct IGET5_PARAM ig5 = { ref, name }; 698 u64 ino = ino_get(ref); 699 struct inode *inode, *base = NULL; 700 bool ads = name && name->ads_len; 701 struct ntfs_inode *ni; 702 703 if (ads) { 704 /* First get base inode */ 705 base = ntfs_iget5_flags(sb, ref, NULL, 0); 706 if (IS_ERR(base)) 707 return base; 708 } 709 710 inode = iget5_locked(sb, ino, ntfs_test_inode, ntfs_set_inode, &ig5); 711 if (unlikely(!inode)) { 712 err = -ENOMEM; 713 goto out; 714 } 715 716 ni = ntfs_i(inode); 717 718 /* If this is a freshly allocated inode, need to read it now. */ 719 if (inode_state_read_once(inode) & I_NEW) { 720 if (!base) { 721 /* default inode. generic file/dir. */ 722 ni->base = ni; 723 } else { 724 /* inode + ads */ 725 ni->base = ntfs_i(base); 726 base = NULL; /* keep reference incremented (instead of ihold). */ 727 } 728 729 if (ads) { 730 /* base record is loaded. Init ads node. */ 731 err = ntfs_init_ads_node( 732 inode, (__le16 *)&name->name[name->len + 1], 733 name->ads_len, flags); 734 } else { 735 err = ntfs_read_mft(inode, name, ref); 736 } 737 738 if (!err) { 739 unlock_new_inode(inode); 740 } else { 741 iget_failed(inode); 742 /* Do not mark volume dirty if ADS not found. */ 743 if (ads) 744 goto out; 745 } 746 } else if (!ads && ref->seq != ni->mi.mrec->seq) { 747 /* 748 * Sequence number is not expected. 749 * Looks like inode was reused but caller uses the old reference 750 */ 751 iput(inode); 752 err = -ESTALE; 753 } else { 754 err = 0; 755 } 756 757 if (err) 758 ntfs_set_state(sb->s_fs_info, NTFS_DIRTY_ERROR); 759 760 out: 761 if (base) 762 iput(base); 763 764 if (err) 765 return ERR_PTR(err); 766 767 return inode; 768 } 769 770 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block) 771 { 772 struct inode *inode = mapping->host; 773 struct ntfs_inode *ni = ntfs_i(inode); 774 775 /* 776 * We can get here for an inline file via the FIBMAP ioctl 777 */ 778 if (is_resident(ni)) 779 return 0; 780 781 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 782 !run_is_empty(&ni->file.run_da)) { 783 /* 784 * With delalloc data we want to sync the file so 785 * that we can make sure we allocate blocks for file and data 786 * is in place for the user to see it 787 */ 788 ni_allocate_da_blocks(ni); 789 } 790 791 return iomap_bmap(mapping, block, ntfs_iomap_next); 792 } 793 794 static void ntfs_iomap_read_end_io(struct bio *bio) 795 { 796 int error = blk_status_to_errno(bio->bi_status); 797 struct folio_iter fi; 798 799 bio_for_each_folio_all(fi, bio) { 800 struct folio *folio = fi.folio; 801 struct inode *inode = folio->mapping->host; 802 struct ntfs_inode *ni = ntfs_i(inode); 803 u64 valid = ni->i_valid; 804 u32 f_size = folio_size(folio); 805 loff_t f_pos = folio_pos(folio); 806 807 if (valid < f_pos + f_size) { 808 u32 z_from = valid <= f_pos ? 809 0 : 810 offset_in_folio(folio, valid); 811 /* The only thing ntfs_iomap_read_end_io used for. */ 812 folio_zero_segment(folio, z_from, f_size); 813 } 814 815 iomap_finish_folio_read(folio, fi.offset, fi.length, error); 816 } 817 bio_put(bio); 818 } 819 820 static void ntfs_iomap_bio_submit_read(const struct iomap_iter *iter, 821 struct iomap_read_folio_ctx *ctx) 822 { 823 iomap_bio_submit_read_endio(iter, ctx, ntfs_iomap_read_end_io); 824 } 825 826 // clang-format off 827 static const struct iomap_read_ops ntfs_iomap_bio_read_ops = { 828 .read_folio_range = iomap_bio_read_folio_range, 829 .submit_read = ntfs_iomap_bio_submit_read, 830 }; 831 // clang-format on 832 833 static int ntfs_read_folio(struct file *file, struct folio *folio) 834 { 835 int err; 836 struct address_space *mapping = folio->mapping; 837 struct inode *inode = mapping->host; 838 struct ntfs_inode *ni = ntfs_i(inode); 839 loff_t vbo = folio_pos(folio); 840 struct iomap_read_folio_ctx ctx = { 841 .cur_folio = folio, 842 .ops = &ntfs_iomap_bio_read_ops, 843 }; 844 845 if (unlikely(is_bad_ni(ni))) { 846 folio_unlock(folio); 847 return -EIO; 848 } 849 850 if (ni->i_valid <= vbo) { 851 folio_zero_range(folio, 0, folio_size(folio)); 852 folio_mark_uptodate(folio); 853 folio_unlock(folio); 854 return 0; 855 } 856 857 if (is_compressed(ni)) { 858 /* ni_lock is taken inside ni_read_folio_cmpr after page locks */ 859 err = ni_read_folio_cmpr(ni, folio); 860 return err; 861 } 862 863 iomap_read_folio(ntfs_iomap_next, &ctx, NULL); 864 return 0; 865 } 866 867 static void ntfs_readahead(struct readahead_control *rac) 868 { 869 struct address_space *mapping = rac->mapping; 870 struct inode *inode = mapping->host; 871 struct ntfs_inode *ni = ntfs_i(inode); 872 struct iomap_read_folio_ctx ctx = { 873 .ops = &ntfs_iomap_bio_read_ops, 874 .rac = rac, 875 }; 876 877 if (is_resident(ni)) { 878 /* No readahead for resident. */ 879 return; 880 } 881 882 if (is_compressed(ni)) { 883 /* No readahead for compressed. */ 884 return; 885 } 886 887 iomap_readahead(ntfs_iomap_next, &ctx, NULL); 888 } 889 890 int ntfs_set_size(struct inode *inode, u64 new_size) 891 { 892 struct super_block *sb = inode->i_sb; 893 struct ntfs_sb_info *sbi = sb->s_fs_info; 894 struct ntfs_inode *ni = ntfs_i(inode); 895 int err; 896 897 /* Check for maximum file size. */ 898 if (is_sparsed(ni) || is_compressed(ni)) { 899 if (new_size > sbi->maxbytes_sparse) { 900 return -EFBIG; 901 } 902 } else if (new_size > sbi->maxbytes) { 903 return -EFBIG; 904 } 905 906 /* Mark rw ntfs as dirty. It will be cleared at umount. */ 907 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 908 909 ni_lock(ni); 910 down_write(&ni->file.run_lock); 911 if (new_size < ni->i_valid) 912 ni->i_valid = new_size; 913 914 /* last 'true' means keep preallocated. */ 915 err = attr_set_size(ni, ATTR_DATA, ni->file.ads.name, ni->file.ads.len, 916 &ni->file.run, new_size, &ni->i_valid, true); 917 918 up_write(&ni->file.run_lock); 919 ni_unlock(ni); 920 921 return err; 922 } 923 924 /* 925 * Special value to detect ntfs_writeback_range call 926 */ 927 #define WB_NO_DA (struct iomap *)1 928 /* 929 * Function to get mapping vbo -> lbo. 930 * used with: 931 * - iomap_zero_range 932 * - iomap_truncate_page 933 * - iomap_dio_rw 934 * - iomap_file_buffered_write 935 * - iomap_bmap 936 * - iomap_fiemap 937 * - iomap_bio_read_folio 938 * - iomap_bio_readahead 939 */ 940 static int ntfs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 941 unsigned int flags, struct iomap *iomap, 942 struct iomap *srcmap) 943 { 944 struct ntfs_inode *ni = ntfs_i(inode); 945 struct ntfs_sb_info *sbi = ni->mi.sbi; 946 u8 cluster_bits = sbi->cluster_bits; 947 CLST vcn = offset >> cluster_bits; 948 u32 off = offset & sbi->cluster_mask; 949 bool rw = flags & IOMAP_WRITE; 950 loff_t endbyte = offset + length; 951 void *res = NULL; 952 int err; 953 CLST lcn, clen, clen_max = 1; 954 bool new_clst = false; 955 bool no_da; 956 bool zero = false; 957 958 trace_ntfs3_iomap_begin(inode, offset, length, flags); 959 if (unlikely(ntfs3_forced_shutdown(sbi->sb))) 960 return -EIO; 961 962 if (flags & IOMAP_REPORT) { 963 if (offset > ntfs_get_maxbytes(ni)) { 964 /* called from fiemap/bmap. */ 965 return -EINVAL; 966 } 967 968 if (offset >= inode->i_size) { 969 /* special code for report. */ 970 return -ENOENT; 971 } 972 } 973 974 if (rw) { 975 clen_max = bytes_to_cluster(sbi, endbyte) - vcn; 976 } 977 978 /* 979 * Force to allocate clusters if directIO(write) or writeback_range. 980 * NOTE: attr_data_get_block allocates clusters only for sparse file. 981 * Normal file allocates clusters in attr_set_size. 982 */ 983 no_da = flags == (IOMAP_DIRECT | IOMAP_WRITE) || srcmap == WB_NO_DA; 984 985 err = attr_data_get_block(ni, vcn, clen_max, &lcn, &clen, 986 rw ? &new_clst : NULL, zero, &res, no_da); 987 988 if (err) { 989 return err; 990 } 991 992 if (lcn == EOF_LCN) { 993 /* request out of file. */ 994 if (flags & IOMAP_REPORT) { 995 /* special code for report. */ 996 return -ENOENT; 997 } 998 999 if (rw) { 1000 /* should never be here. */ 1001 return -EINVAL; 1002 } 1003 lcn = SPARSE_LCN; 1004 } 1005 1006 iomap->flags = new_clst ? IOMAP_F_NEW : 0; 1007 1008 if (lcn == RESIDENT_LCN) { 1009 if (offset >= clen) { 1010 if (res) 1011 __free_page(virt_to_page(res)); 1012 if (flags & IOMAP_REPORT) { 1013 /* special code for report. */ 1014 return -ENOENT; 1015 } 1016 return -EFAULT; 1017 } 1018 1019 iomap->private = iomap->inline_data = res; 1020 iomap->type = IOMAP_INLINE; 1021 iomap->offset = 0; 1022 iomap->length = clen; /* resident size in bytes. */ 1023 return 0; 1024 } 1025 1026 if (!clen) { 1027 /* Looks like file is broken.*/ 1028 return -EINVAL; 1029 } 1030 1031 iomap->bdev = inode->i_sb->s_bdev; 1032 iomap->offset = offset; 1033 iomap->length = ((loff_t)clen << cluster_bits) - off; 1034 1035 if (lcn == COMPRESSED_LCN) { 1036 /* should never be here. */ 1037 return -EOPNOTSUPP; 1038 } 1039 1040 if (lcn == DELALLOC_LCN) { 1041 iomap->type = IOMAP_DELALLOC; 1042 iomap->addr = IOMAP_NULL_ADDR; 1043 } else { 1044 /* Translate clusters into bytes. */ 1045 iomap->addr = ((loff_t)lcn << cluster_bits) + off; 1046 if (length && iomap->length > length) 1047 iomap->length = length; 1048 else 1049 endbyte = offset + iomap->length; 1050 1051 if (lcn == SPARSE_LCN) { 1052 iomap->addr = IOMAP_NULL_ADDR; 1053 iomap->type = IOMAP_HOLE; 1054 // if (IOMAP_ZERO == flags && !off) { 1055 // iomap->length = (endbyte - offset) & 1056 // sbi->cluster_mask_inv; 1057 // } 1058 } else if (endbyte <= ni->i_valid) { 1059 iomap->type = IOMAP_MAPPED; 1060 } else if (offset < ni->i_valid) { 1061 iomap->type = IOMAP_MAPPED; 1062 if (flags & IOMAP_REPORT) 1063 iomap->length = ni->i_valid - offset; 1064 } else if (rw || (flags & IOMAP_ZERO)) { 1065 iomap->type = IOMAP_MAPPED; 1066 } else { 1067 iomap->type = IOMAP_UNWRITTEN; 1068 } 1069 } 1070 1071 if ((flags & IOMAP_ZERO) && 1072 (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) { 1073 /* Avoid too large requests. */ 1074 u32 tail; 1075 u32 off_a = offset & (PAGE_SIZE - 1); 1076 if (off_a) 1077 tail = PAGE_SIZE - off_a; 1078 else 1079 tail = PAGE_SIZE; 1080 1081 if (iomap->length > tail) 1082 iomap->length = tail; 1083 } 1084 1085 return 0; 1086 } 1087 1088 static int ntfs_iomap_end(struct inode *inode, loff_t pos, loff_t length, 1089 ssize_t written, unsigned int flags, 1090 struct iomap *iomap) 1091 { 1092 int err = 0; 1093 struct ntfs_inode *ni = ntfs_i(inode); 1094 loff_t endbyte = pos + written; 1095 1096 trace_ntfs3_iomap_end(inode, pos, length, written, flags); 1097 1098 if ((flags & IOMAP_WRITE) || (flags & IOMAP_ZERO)) { 1099 if (iomap->type == IOMAP_INLINE) { 1100 u32 data_size; 1101 struct ATTRIB *attr; 1102 struct mft_inode *mi; 1103 1104 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, 1105 ni->file.ads.name, ni->file.ads.len, 1106 NULL, &mi); 1107 if (!attr || attr->non_res) { 1108 err = -EINVAL; 1109 goto out; 1110 } 1111 1112 data_size = le32_to_cpu(attr->res.data_size); 1113 if (!(pos < data_size && endbyte <= data_size)) { 1114 err = -EINVAL; 1115 goto out; 1116 } 1117 1118 /* Update resident data. */ 1119 memcpy(resident_data(attr) + pos, 1120 iomap_inline_data(iomap, pos), written); 1121 mi->dirty = true; 1122 ni->i_valid = data_size; 1123 } else if (ni->i_valid < endbyte) { 1124 ni->i_valid = endbyte; 1125 mark_inode_dirty(inode); 1126 } 1127 } 1128 1129 if ((flags & IOMAP_ZERO) && 1130 (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) { 1131 /* Pair for code in ntfs_iomap_begin. */ 1132 balance_dirty_pages_ratelimited(inode->i_mapping); 1133 cond_resched(); 1134 } 1135 1136 out: 1137 if (iomap->type == IOMAP_INLINE) { 1138 __free_page(virt_to_page(iomap->private)); 1139 iomap->private = NULL; 1140 } 1141 1142 return err; 1143 } 1144 1145 /* 1146 * write_begin + put_folio + write_end. 1147 * iomap_zero_range 1148 * iomap_truncate_page 1149 * iomap_file_buffered_write 1150 */ 1151 static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos, 1152 unsigned int len, struct folio *folio) 1153 { 1154 struct ntfs_inode *ni = ntfs_i(inode); 1155 loff_t end = pos + len; 1156 u32 f_size = folio_size(folio); 1157 loff_t f_pos = folio_pos(folio); 1158 loff_t f_end = f_pos + f_size; 1159 1160 if (ni->i_valid <= end && end < f_end) { 1161 /* zero range [end - f_end). */ 1162 /* The only thing ntfs_iomap_put_folio used for. */ 1163 folio_zero_segment(folio, offset_in_folio(folio, end), f_size); 1164 } 1165 folio_unlock(folio); 1166 folio_put(folio); 1167 } 1168 1169 /* 1170 * iomap_writeback_ops::writeback_range 1171 */ 1172 static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc, 1173 struct folio *folio, u64 offset, 1174 unsigned int len, u64 end_pos) 1175 { 1176 struct iomap *iomap = &wpc->iomap; 1177 /* Check iomap position. */ 1178 if (iomap->offset + iomap->length <= offset || offset < iomap->offset) { 1179 int err; 1180 struct inode *inode = wpc->inode; 1181 struct ntfs_inode *ni = ntfs_i(inode); 1182 struct ntfs_sb_info *sbi = ntfs_sb(inode->i_sb); 1183 loff_t i_size_up = ntfs_up_cluster(sbi, inode->i_size); 1184 loff_t len_max = i_size_up - offset; 1185 1186 err = ni->file.run_da.count ? ni_allocate_da_blocks(ni) : 0; 1187 1188 if (!err) { 1189 /* Use local special value 'WB_NO_DA' to disable delalloc. */ 1190 err = ntfs_iomap_begin(inode, offset, len_max, 1191 IOMAP_WRITE, iomap, WB_NO_DA); 1192 } 1193 1194 if (err) { 1195 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 1196 return err; 1197 } 1198 } 1199 1200 return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); 1201 } 1202 1203 static const struct iomap_writeback_ops ntfs_writeback_ops = { 1204 .writeback_range = ntfs_writeback_range, 1205 .writeback_submit = iomap_ioend_writeback_submit, 1206 }; 1207 1208 static int ntfs_writepages(struct address_space *mapping, 1209 struct writeback_control *wbc) 1210 { 1211 int err; 1212 struct inode *inode = mapping->host; 1213 struct ntfs_inode *ni = ntfs_i(inode); 1214 struct iomap_writepage_ctx wpc = { 1215 .inode = inode, 1216 .wbc = wbc, 1217 .ops = &ntfs_writeback_ops, 1218 }; 1219 1220 /* Avoid any operation if inode is bad. */ 1221 if (unlikely(is_bad_ni(ni))) 1222 return -EINVAL; 1223 1224 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1225 return -EIO; 1226 1227 if (is_resident(ni)) { 1228 struct folio *folio = NULL; 1229 err = 0; 1230 1231 while ((folio = writeback_iter(mapping, wbc, folio, &err))) { 1232 int err2; 1233 1234 ni_lock(ni); 1235 err2 = attr_data_write_resident(ni, folio); 1236 ni_unlock(ni); 1237 1238 folio_unlock(folio); 1239 if (err2) { 1240 /* 1241 * attr_data_write_resident() can return the 1242 * positive internal code E_NTFS_NONRESIDENT; 1243 * both mapping_set_error() and writeback_iter() 1244 * need a negative errno and warn on a positive 1245 * value. 1246 */ 1247 if (err2 > 0) 1248 err2 = -EIO; 1249 mapping_set_error(mapping, err2); 1250 if (!err) 1251 err = err2; 1252 } 1253 } 1254 1255 return err; 1256 } 1257 1258 return iomap_writepages(&wpc); 1259 } 1260 1261 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc) 1262 { 1263 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 1264 } 1265 1266 int ntfs_sync_inode(struct inode *inode) 1267 { 1268 return _ni_write_inode(inode, 1); 1269 } 1270 1271 /* 1272 * Helper function to read file. 1273 * Used to read $AttrDef and $UpCase 1274 */ 1275 int inode_read_data(struct inode *inode, void *data, size_t bytes) 1276 { 1277 pgoff_t idx; 1278 struct address_space *mapping = inode->i_mapping; 1279 1280 for (idx = 0; bytes; idx++) { 1281 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes; 1282 struct page *page = read_mapping_page(mapping, idx, NULL); 1283 void *kaddr; 1284 1285 if (IS_ERR(page)) 1286 return PTR_ERR(page); 1287 1288 kaddr = kmap_atomic(page); 1289 memcpy(data, kaddr, op); 1290 kunmap_atomic(kaddr); 1291 1292 put_page(page); 1293 1294 bytes -= op; 1295 data = Add2Ptr(data, PAGE_SIZE); 1296 } 1297 return 0; 1298 } 1299 1300 /* 1301 * ntfs_reparse_bytes 1302 * 1303 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK) 1304 * for unicode string of @uni_len length. 1305 */ 1306 static inline u32 ntfs_reparse_bytes(u32 uni_len, bool is_absolute) 1307 { 1308 /* Header + unicode string + decorated unicode string. */ 1309 return sizeof(short) * (2 * uni_len + (is_absolute ? 4 : 0)) + 1310 offsetof(struct REPARSE_DATA_BUFFER, 1311 SymbolicLinkReparseBuffer.PathBuffer); 1312 } 1313 1314 static struct REPARSE_DATA_BUFFER * 1315 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname, 1316 u32 size, u16 *nsize) 1317 { 1318 int i, err; 1319 struct REPARSE_DATA_BUFFER *rp; 1320 __le16 *rp_name; 1321 typeof(rp->SymbolicLinkReparseBuffer) *rs; 1322 bool is_absolute; 1323 1324 is_absolute = symname[0] && symname[1] == ':'; 1325 1326 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2, is_absolute), GFP_NOFS); 1327 if (!rp) 1328 return ERR_PTR(-ENOMEM); 1329 1330 rs = &rp->SymbolicLinkReparseBuffer; 1331 rp_name = rs->PathBuffer; 1332 1333 /* Convert link name to UTF-16. */ 1334 err = ntfs_nls_to_utf16(sbi, symname, size, 1335 (struct cpu_str *)(rp_name - 1), 2 * size, 1336 UTF16_LITTLE_ENDIAN); 1337 if (err < 0) 1338 goto out; 1339 1340 /* err = the length of unicode name of symlink. */ 1341 *nsize = ntfs_reparse_bytes(err, is_absolute); 1342 1343 if (*nsize > sbi->reparse.max_size) { 1344 err = -EFBIG; 1345 goto out; 1346 } 1347 1348 /* Translate Linux '/' into Windows '\'. */ 1349 for (i = 0; i < err; i++) { 1350 if (rp_name[i] == cpu_to_le16('/')) 1351 rp_name[i] = cpu_to_le16('\\'); 1352 } 1353 1354 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK; 1355 rp->ReparseDataLength = 1356 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER, 1357 SymbolicLinkReparseBuffer)); 1358 1359 /* PrintName + SubstituteName. */ 1360 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err); 1361 rs->SubstituteNameLength = 1362 cpu_to_le16(sizeof(short) * err + (is_absolute ? 8 : 0)); 1363 rs->PrintNameLength = rs->SubstituteNameOffset; 1364 1365 /* 1366 * TODO: Use relative path if possible to allow Windows to 1367 * parse this path. 1368 * 0-absolute path, 1- relative path (SYMLINK_FLAG_RELATIVE). 1369 */ 1370 rs->Flags = cpu_to_le32(is_absolute ? 0 : SYMLINK_FLAG_RELATIVE); 1371 1372 memmove(rp_name + err + (is_absolute ? 4 : 0), rp_name, 1373 sizeof(short) * err); 1374 1375 if (is_absolute) { 1376 /* Decorate SubstituteName. */ 1377 rp_name += err; 1378 rp_name[0] = cpu_to_le16('\\'); 1379 rp_name[1] = cpu_to_le16('?'); 1380 rp_name[2] = cpu_to_le16('?'); 1381 rp_name[3] = cpu_to_le16('\\'); 1382 } 1383 1384 return rp; 1385 out: 1386 kfree(rp); 1387 return ERR_PTR(err); 1388 } 1389 1390 /* 1391 * ntfs_create_inode 1392 * 1393 * Helper function for: 1394 * - ntfs_create 1395 * - ntfs_mknod 1396 * - ntfs_symlink 1397 * - ntfs_mkdir 1398 * - ntfs_atomic_open 1399 * 1400 * NOTE: if fnd != NULL (ntfs_atomic_open) then @dir is locked 1401 */ 1402 int ntfs_create_inode(const struct mnt_idmap *idmap, struct inode *dir, 1403 struct dentry *dentry, const struct cpu_str *uni, 1404 umode_t mode, dev_t dev, const char *symname, u32 size, 1405 struct ntfs_fnd *fnd) 1406 { 1407 int err; 1408 struct super_block *sb = dir->i_sb; 1409 struct ntfs_sb_info *sbi = sb->s_fs_info; 1410 const struct qstr *name = &dentry->d_name; 1411 CLST ino = 0; 1412 struct ntfs_inode *dir_ni = ntfs_i(dir); 1413 struct ntfs_inode *ni = NULL; 1414 struct inode *inode = NULL; 1415 struct ATTRIB *attr; 1416 struct ATTR_STD_INFO5 *std5; 1417 struct ATTR_FILE_NAME *fname; 1418 struct MFT_REC *rec; 1419 u32 asize, dsize, sd_size; 1420 enum FILE_ATTRIBUTE fa; 1421 __le32 security_id = SECURITY_ID_INVALID; 1422 CLST vcn; 1423 const void *sd; 1424 u16 t16, nsize = 0, aid = 0; 1425 struct INDEX_ROOT *root, *dir_root; 1426 struct NTFS_DE *e, *new_de = NULL; 1427 struct REPARSE_DATA_BUFFER *rp = NULL; 1428 bool rp_inserted = false; 1429 1430 /* New file will be resident or non resident. */ 1431 const bool new_file_resident = 1; 1432 1433 trace_ntfs3_create_inode(dir, dentry); 1434 1435 if (!fnd) 1436 ni_lock_dir(dir_ni); 1437 1438 if (sbi->options->ads) { 1439 const char *ads = strchr(name->name + 1, ':'); 1440 if (ads && ads[1]) { 1441 ntfs_warn(sb, "failed to create ads"); 1442 err = -EINVAL; 1443 goto out1; 1444 } 1445 } 1446 1447 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL); 1448 if (!dir_root) { 1449 err = -EINVAL; 1450 goto out1; 1451 } 1452 1453 if (S_ISDIR(mode)) { 1454 /* Use parent's directory attributes. */ 1455 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY | 1456 FILE_ATTRIBUTE_ARCHIVE; 1457 /* 1458 * By default child directory inherits parent attributes. 1459 * Root directory is hidden + system. 1460 * Make an exception for children in root. 1461 */ 1462 if (dir->i_ino == MFT_REC_ROOT) 1463 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM); 1464 } else if (S_ISLNK(mode)) { 1465 /* It is good idea that link should be the same type (file/dir) as target */ 1466 fa = FILE_ATTRIBUTE_REPARSE_POINT; 1467 1468 /* 1469 * Linux: there are dir/file/symlink and so on. 1470 * NTFS: symlinks are "dir + reparse" or "file + reparse" 1471 * It is good idea to create: 1472 * dir + reparse if 'symname' points to directory 1473 * or 1474 * file + reparse if 'symname' points to file 1475 * Unfortunately kern_path hangs if symname contains 'dir'. 1476 */ 1477 1478 /* 1479 * struct path path; 1480 * 1481 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){ 1482 * struct inode *target = d_inode(path.dentry); 1483 * 1484 * if (S_ISDIR(target->i_mode)) 1485 * fa |= FILE_ATTRIBUTE_DIRECTORY; 1486 * // if ( target->i_sb == sb ){ 1487 * // use relative path? 1488 * // } 1489 * path_put(&path); 1490 * } 1491 */ 1492 } else if (S_ISREG(mode)) { 1493 if (sbi->options->sparse) { 1494 /* Sparsed regular file, cause option 'sparse'. */ 1495 fa = FILE_ATTRIBUTE_SPARSE_FILE | 1496 FILE_ATTRIBUTE_ARCHIVE; 1497 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) { 1498 /* Compressed regular file, if parent is compressed. */ 1499 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE; 1500 } else { 1501 /* Regular file, default attributes. */ 1502 fa = FILE_ATTRIBUTE_ARCHIVE; 1503 } 1504 } else { 1505 fa = FILE_ATTRIBUTE_ARCHIVE; 1506 } 1507 1508 /* If option "hide_dot_files" then set hidden attribute for dot files. */ 1509 if (sbi->options->hide_dot_files && name->name[0] == '.') 1510 fa |= FILE_ATTRIBUTE_HIDDEN; 1511 1512 if (!(mode & 0222)) 1513 fa |= FILE_ATTRIBUTE_READONLY; 1514 1515 new_de = kzalloc(PATH_MAX, GFP_KERNEL); 1516 if (!new_de) { 1517 err = -ENOMEM; 1518 goto out1; 1519 } 1520 1521 /* Avoid any operation if inode is bad. */ 1522 if (unlikely(is_bad_ni(dir_ni))) { 1523 err = -EINVAL; 1524 goto out2; 1525 } 1526 1527 if (unlikely(ntfs3_forced_shutdown(sb))) { 1528 err = -EIO; 1529 goto out2; 1530 } 1531 1532 /* Mark rw ntfs as dirty. it will be cleared at umount. */ 1533 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 1534 1535 /* Step 1: allocate and fill new mft record. */ 1536 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL); 1537 if (err) 1538 goto out2; 1539 1540 ni = ntfs_new_inode(sbi, ino, S_ISDIR(mode) ? RECORD_FLAG_DIR : 0); 1541 if (IS_ERR(ni)) { 1542 err = PTR_ERR(ni); 1543 ni = NULL; 1544 goto out3; 1545 } 1546 inode = &ni->vfs_inode; 1547 inode_init_owner(idmap, inode, dir, mode); 1548 mode = inode->i_mode; 1549 1550 ni->i_crtime = current_time(inode); 1551 1552 rec = ni->mi.mrec; 1553 rec->hard_links = cpu_to_le16(1); 1554 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off)); 1555 1556 /* Get default security id. */ 1557 sd = s_default_security; 1558 sd_size = sizeof(s_default_security); 1559 1560 if (is_ntfs3(sbi)) { 1561 security_id = dir_ni->std_security_id; 1562 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) { 1563 security_id = sbi->security.def_security_id; 1564 1565 if (security_id == SECURITY_ID_INVALID && 1566 !ntfs_insert_security(sbi, sd, sd_size, 1567 &security_id, NULL)) 1568 sbi->security.def_security_id = security_id; 1569 } 1570 } 1571 1572 /* Insert standard info. */ 1573 std5 = Add2Ptr(attr, SIZEOF_RESIDENT); 1574 1575 if (security_id == SECURITY_ID_INVALID) { 1576 dsize = sizeof(struct ATTR_STD_INFO); 1577 } else { 1578 dsize = sizeof(struct ATTR_STD_INFO5); 1579 std5->security_id = security_id; 1580 ni->std_security_id = security_id; 1581 } 1582 asize = SIZEOF_RESIDENT + dsize; 1583 1584 attr->type = ATTR_STD; 1585 attr->size = cpu_to_le32(asize); 1586 attr->id = cpu_to_le16(aid++); 1587 attr->res.data_off = SIZEOF_RESIDENT_LE; 1588 attr->res.data_size = cpu_to_le32(dsize); 1589 1590 std5->cr_time = std5->m_time = std5->c_time = std5->a_time = 1591 kernel2nt(&ni->i_crtime); 1592 1593 std5->fa = ni->std_fa = fa; 1594 1595 attr = Add2Ptr(attr, asize); 1596 1597 /* Insert file name. */ 1598 err = fill_name_de(sbi, new_de, name, uni); 1599 if (err) 1600 goto out4; 1601 1602 mi_get_ref(&ni->mi, &new_de->ref); 1603 1604 fname = (struct ATTR_FILE_NAME *)(new_de + 1); 1605 1606 if (sbi->options->windows_names && 1607 !valid_windows_name(sbi, (struct le_str *)&fname->name_len)) { 1608 err = -EINVAL; 1609 goto out4; 1610 } 1611 1612 mi_get_ref(&dir_ni->mi, &fname->home); 1613 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time = 1614 fname->dup.a_time = std5->cr_time; 1615 fname->dup.alloc_size = fname->dup.data_size = 0; 1616 fname->dup.fa = std5->fa; 1617 fname->dup.extend_data = S_ISLNK(mode) ? IO_REPARSE_TAG_SYMLINK : 0; 1618 1619 dsize = le16_to_cpu(new_de->key_size); 1620 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8); 1621 1622 attr->type = ATTR_NAME; 1623 attr->size = cpu_to_le32(asize); 1624 attr->res.data_off = SIZEOF_RESIDENT_LE; 1625 attr->res.flags = RESIDENT_FLAG_INDEXED; 1626 attr->id = cpu_to_le16(aid++); 1627 attr->res.data_size = cpu_to_le32(dsize); 1628 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize); 1629 1630 attr = Add2Ptr(attr, asize); 1631 1632 if (security_id == SECURITY_ID_INVALID) { 1633 /* Insert security attribute. */ 1634 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8); 1635 1636 attr->type = ATTR_SECURE; 1637 attr->size = cpu_to_le32(asize); 1638 attr->id = cpu_to_le16(aid++); 1639 attr->res.data_off = SIZEOF_RESIDENT_LE; 1640 attr->res.data_size = cpu_to_le32(sd_size); 1641 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size); 1642 1643 attr = Add2Ptr(attr, asize); 1644 } 1645 1646 attr->id = cpu_to_le16(aid++); 1647 if (fa & FILE_ATTRIBUTE_DIRECTORY) { 1648 /* 1649 * Regular directory or symlink to directory. 1650 * Create root attribute. 1651 */ 1652 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE); 1653 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize; 1654 1655 attr->type = ATTR_ROOT; 1656 attr->size = cpu_to_le32(asize); 1657 1658 attr->name_len = ARRAY_SIZE(I30_NAME); 1659 attr->name_off = SIZEOF_RESIDENT_LE; 1660 attr->res.data_off = 1661 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT); 1662 attr->res.data_size = cpu_to_le32(dsize); 1663 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME, 1664 sizeof(I30_NAME)); 1665 1666 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT); 1667 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr)); 1668 root->ihdr.de_off = cpu_to_le32(sizeof(struct INDEX_HDR)); 1669 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) + 1670 sizeof(struct NTFS_DE)); 1671 root->ihdr.total = root->ihdr.used; 1672 1673 e = Add2Ptr(root, sizeof(struct INDEX_ROOT)); 1674 e->size = cpu_to_le16(sizeof(struct NTFS_DE)); 1675 e->flags = NTFS_IE_LAST; 1676 } else if (S_ISLNK(mode)) { 1677 /* 1678 * Symlink to file. 1679 * Create empty resident data attribute. 1680 */ 1681 asize = SIZEOF_RESIDENT; 1682 1683 /* Insert empty ATTR_DATA */ 1684 attr->type = ATTR_DATA; 1685 attr->size = cpu_to_le32(SIZEOF_RESIDENT); 1686 attr->name_off = SIZEOF_RESIDENT_LE; 1687 attr->res.data_off = SIZEOF_RESIDENT_LE; 1688 } else if (!new_file_resident && S_ISREG(mode)) { 1689 /* 1690 * Regular file. Create empty non resident data attribute. 1691 */ 1692 attr->type = ATTR_DATA; 1693 attr->non_res = 1; 1694 attr->nres.evcn = cpu_to_le64(-1ll); 1695 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) { 1696 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8); 1697 attr->name_off = SIZEOF_NONRESIDENT_EX_LE; 1698 attr->flags = ATTR_FLAG_SPARSED; 1699 asize = SIZEOF_NONRESIDENT_EX + 8; 1700 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) { 1701 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8); 1702 attr->name_off = SIZEOF_NONRESIDENT_EX_LE; 1703 attr->flags = ATTR_FLAG_COMPRESSED; 1704 attr->nres.c_unit = NTFS_LZNT_CUNIT; 1705 asize = SIZEOF_NONRESIDENT_EX + 8; 1706 } else { 1707 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8); 1708 attr->name_off = SIZEOF_NONRESIDENT_LE; 1709 asize = SIZEOF_NONRESIDENT + 8; 1710 } 1711 attr->nres.run_off = attr->name_off; 1712 } else { 1713 /* 1714 * Node. Create empty resident data attribute. 1715 */ 1716 attr->type = ATTR_DATA; 1717 attr->size = cpu_to_le32(SIZEOF_RESIDENT); 1718 attr->name_off = SIZEOF_RESIDENT_LE; 1719 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) 1720 attr->flags = ATTR_FLAG_SPARSED; 1721 else if (fa & FILE_ATTRIBUTE_COMPRESSED) 1722 attr->flags = ATTR_FLAG_COMPRESSED; 1723 attr->res.data_off = SIZEOF_RESIDENT_LE; 1724 asize = SIZEOF_RESIDENT; 1725 ni->ni_flags |= NI_FLAG_RESIDENT; 1726 } 1727 1728 if (S_ISDIR(mode)) { 1729 ni->ni_flags |= NI_FLAG_DIR; 1730 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30); 1731 if (err) 1732 goto out4; 1733 } else if (S_ISLNK(mode)) { 1734 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize); 1735 1736 if (IS_ERR(rp)) { 1737 err = PTR_ERR(rp); 1738 rp = NULL; 1739 goto out4; 1740 } 1741 1742 /* 1743 * Insert ATTR_REPARSE. 1744 */ 1745 attr = Add2Ptr(attr, asize); 1746 attr->type = ATTR_REPARSE; 1747 attr->id = cpu_to_le16(aid++); 1748 1749 /* Resident or non resident? */ 1750 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8); 1751 t16 = PtrOffset(rec, attr); 1752 1753 /* 1754 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes. 1755 * It is good idea to keep extended attributes resident. 1756 */ 1757 if (asize + t16 + 0x78 + 8 > sbi->record_size) { 1758 CLST alen; 1759 CLST clst = bytes_to_cluster(sbi, nsize); 1760 1761 /* Bytes per runs. */ 1762 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT; 1763 1764 attr->non_res = 1; 1765 attr->nres.evcn = cpu_to_le64(clst - 1); 1766 attr->name_off = SIZEOF_NONRESIDENT_LE; 1767 attr->nres.run_off = attr->name_off; 1768 attr->nres.data_size = cpu_to_le64(nsize); 1769 attr->nres.valid_size = attr->nres.data_size; 1770 attr->nres.alloc_size = 1771 cpu_to_le64(ntfs_up_cluster(sbi, nsize)); 1772 1773 err = attr_allocate_clusters(sbi, &ni->file.run, NULL, 1774 0, 0, clst, NULL, 1775 ALLOCATE_DEF, &alen, 0, 1776 NULL, NULL); 1777 if (err) 1778 goto out5; 1779 1780 err = run_pack(&ni->file.run, 0, clst, 1781 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16, 1782 &vcn); 1783 if (err < 0) 1784 goto out5; 1785 1786 if (vcn != clst) { 1787 err = -EINVAL; 1788 goto out5; 1789 } 1790 1791 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8); 1792 /* Write non resident data. */ 1793 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, 1794 nsize, 0); 1795 if (err) 1796 goto out5; 1797 } else { 1798 attr->res.data_off = SIZEOF_RESIDENT_LE; 1799 attr->res.data_size = cpu_to_le32(nsize); 1800 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize); 1801 } 1802 /* Size of symlink equals the length of input string. */ 1803 inode->i_size = size; 1804 1805 attr->size = cpu_to_le32(asize); 1806 1807 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK, 1808 &new_de->ref); 1809 if (err) 1810 goto out5; 1811 1812 rp_inserted = true; 1813 } 1814 1815 attr = Add2Ptr(attr, asize); 1816 attr->type = ATTR_END; 1817 1818 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8); 1819 rec->next_attr_id = cpu_to_le16(aid); 1820 1821 inode->i_generation = le16_to_cpu(rec->seq); 1822 1823 if (S_ISDIR(mode)) { 1824 inode->i_op = &ntfs_dir_inode_operations; 1825 inode->i_fop = &ntfs_dir_operations; 1826 } else if (S_ISLNK(mode)) { 1827 inode->i_op = &ntfs_link_inode_operations; 1828 inode->i_fop = NULL; 1829 inode->i_mapping->a_ops = &ntfs_aops; 1830 inode->i_size = size; 1831 inode_nohighmem(inode); 1832 } else if (S_ISREG(mode)) { 1833 inode->i_op = &ntfs_file_inode_operations; 1834 inode->i_fop = &ntfs_file_operations; 1835 inode->i_mapping->a_ops = is_compressed(ni) ? &ntfs_aops_cmpr : 1836 &ntfs_aops; 1837 init_rwsem(&ni->file.run_lock); 1838 } else { 1839 inode->i_op = &ntfs_special_inode_operations; 1840 init_special_inode(inode, mode, dev); 1841 } 1842 1843 #ifdef CONFIG_NTFS3_FS_POSIX_ACL 1844 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) { 1845 err = ntfs_init_acl(idmap, inode, dir); 1846 if (err) 1847 goto out5; 1848 } else 1849 #endif 1850 { 1851 inode->i_flags |= S_NOSEC; 1852 } 1853 1854 if (!S_ISLNK(mode) && sbi->options->ugm) { 1855 /* 1856 * ntfs_init_acl and ntfs_save_wsl_perm update extended attribute. 1857 * The packed size of extended attribute is stored in direntry too. 1858 * 'fname' here points to inside new_de. 1859 */ 1860 err = ntfs_save_wsl_perm(inode, &fname->dup.extend_data); 1861 if (err) 1862 goto out6; 1863 1864 /* 1865 * update ea_size in file_name attribute too. 1866 * Use ni_find_attr cause layout of MFT record may be changed 1867 * in ntfs_init_acl and ntfs_save_wsl_perm. 1868 */ 1869 attr = ni_find_attr(ni, NULL, NULL, ATTR_NAME, NULL, 0, NULL, 1870 NULL); 1871 if (attr) { 1872 struct ATTR_FILE_NAME *fn; 1873 1874 fn = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); 1875 if (fn) 1876 fn->dup.extend_data = fname->dup.extend_data; 1877 } 1878 } 1879 1880 /* We do not need to update parent directory later */ 1881 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT; 1882 1883 /* Step 2: Add new name in index. */ 1884 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0); 1885 if (err) 1886 goto out6; 1887 1888 /* 1889 * Call 'd_instantiate_new' after inode->i_op is set 1890 * but before finish_open. 1891 */ 1892 d_instantiate_new(dentry, inode); 1893 1894 /* Set original time. inode times (i_ctime) may be changed in ntfs_init_acl. */ 1895 inode_set_atime_to_ts(inode, ni->i_crtime); 1896 inode_set_ctime_to_ts(inode, ni->i_crtime); 1897 inode_set_mtime_to_ts(inode, ni->i_crtime); 1898 inode_set_mtime_to_ts(dir, ni->i_crtime); 1899 inode_set_ctime_to_ts(dir, ni->i_crtime); 1900 1901 mark_inode_dirty(dir); 1902 mark_inode_dirty(inode); 1903 1904 /* Normal exit. */ 1905 goto out2; 1906 1907 out6: 1908 attr = ni_find_attr(ni, NULL, NULL, ATTR_EA, NULL, 0, NULL, NULL); 1909 if (attr && attr->non_res) { 1910 /* Delete ATTR_EA, if non-resident. */ 1911 struct runs_tree run; 1912 run_init(&run); 1913 attr_set_size(ni, ATTR_EA, NULL, 0, &run, 0, NULL, false); 1914 run_close(&run); 1915 } 1916 1917 if (rp_inserted) 1918 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref); 1919 1920 out5: 1921 if (!S_ISDIR(mode)) 1922 run_deallocate(sbi, &ni->file.run, false); 1923 1924 out4: 1925 clear_rec_inuse(rec); 1926 clear_nlink(inode); 1927 ni->mi.dirty = false; 1928 discard_new_inode(inode); 1929 out3: 1930 ntfs_mark_rec_free(sbi, ino, false); 1931 1932 out2: 1933 kfree(new_de); 1934 kfree(rp); 1935 1936 out1: 1937 if (!fnd) 1938 ni_unlock(dir_ni); 1939 1940 return err; 1941 } 1942 1943 int ntfs_link_inode(struct inode *inode, struct dentry *dentry) 1944 { 1945 int err; 1946 struct ntfs_inode *ni = ntfs_i(inode); 1947 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 1948 struct NTFS_DE *de; 1949 1950 de = kzalloc(PATH_MAX, GFP_KERNEL); 1951 if (!de) 1952 return -ENOMEM; 1953 1954 /* Mark rw ntfs as dirty. It will be cleared at umount. */ 1955 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 1956 1957 /* Construct 'de'. */ 1958 err = fill_name_de(sbi, de, &dentry->d_name, NULL); 1959 if (err) 1960 goto out; 1961 1962 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de); 1963 out: 1964 kfree(de); 1965 return err; 1966 } 1967 1968 /* 1969 * ntfs_unlink_inode 1970 * 1971 * inode_operations::unlink 1972 * inode_operations::rmdir 1973 */ 1974 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry) 1975 { 1976 int err; 1977 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info; 1978 struct inode *inode = d_inode(dentry); 1979 struct ntfs_inode *ni = ntfs_i(inode); 1980 struct ntfs_inode *dir_ni = ntfs_i(dir); 1981 struct NTFS_DE *de, *de2 = NULL; 1982 int undo_remove; 1983 1984 if (ntfs_is_meta_file(sbi, ni->mi.rno)) 1985 return -EINVAL; 1986 1987 de = kzalloc(PATH_MAX, GFP_KERNEL); 1988 if (!de) 1989 return -ENOMEM; 1990 1991 ni_lock(ni); 1992 1993 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) { 1994 err = -ENOTEMPTY; 1995 goto out; 1996 } 1997 1998 err = fill_name_de(sbi, de, &dentry->d_name, NULL); 1999 if (err < 0) 2000 goto out; 2001 2002 undo_remove = 0; 2003 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove); 2004 2005 if (!err) { 2006 drop_nlink(inode); 2007 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 2008 mark_inode_dirty(dir); 2009 inode_set_ctime_to_ts(inode, inode_get_ctime(dir)); 2010 if (inode->i_nlink) 2011 mark_inode_dirty(inode); 2012 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) { 2013 _ntfs_bad_inode(inode); 2014 } else { 2015 if (ni_is_dirty(dir)) 2016 mark_inode_dirty(dir); 2017 if (ni_is_dirty(inode)) 2018 mark_inode_dirty(inode); 2019 } 2020 2021 out: 2022 ni_unlock(ni); 2023 kfree(de); 2024 return err; 2025 } 2026 2027 void ntfs_evict_inode(struct inode *inode) 2028 { 2029 truncate_inode_pages_final(&inode->i_data); 2030 2031 clear_inode(inode); 2032 2033 ni_clear(ntfs_i(inode)); 2034 } 2035 2036 /* 2037 * ntfs_translate_junction 2038 * 2039 * Translate a Windows junction target to the Linux equivalent. 2040 * On junctions, targets are always absolute (they include the drive 2041 * letter). We have no way of knowing if the target is for the current 2042 * mounted device or not so we just assume it is. 2043 */ 2044 static int ntfs_translate_junction(const struct super_block *sb, 2045 const struct dentry *link_de, char *target, 2046 int target_len, int target_max) 2047 { 2048 int tl_len, err = target_len; 2049 char *link_path_buffer = NULL, *link_path; 2050 char *translated = NULL; 2051 char *target_start; 2052 int copy_len; 2053 2054 link_path_buffer = kmalloc(PATH_MAX, GFP_NOFS); 2055 if (!link_path_buffer) { 2056 err = -ENOMEM; 2057 goto out; 2058 } 2059 /* Get link path, relative to mount point */ 2060 link_path = dentry_path_raw(link_de, link_path_buffer, PATH_MAX); 2061 if (IS_ERR(link_path)) { 2062 ntfs_err(sb, "Error getting link path"); 2063 err = -EINVAL; 2064 goto out; 2065 } 2066 2067 translated = kmalloc(PATH_MAX, GFP_NOFS); 2068 if (!translated) { 2069 err = -ENOMEM; 2070 goto out; 2071 } 2072 2073 /* Make translated path a relative path to mount point */ 2074 strcpy(translated, "./"); 2075 ++link_path; /* Skip leading / */ 2076 for (tl_len = sizeof("./") - 1; *link_path; ++link_path) { 2077 if (*link_path == '/') { 2078 if (PATH_MAX - tl_len < sizeof("../")) { 2079 ntfs_err(sb, 2080 "Link path %s has too many components", 2081 link_path); 2082 err = -EINVAL; 2083 goto out; 2084 } 2085 strcpy(translated + tl_len, "../"); 2086 tl_len += sizeof("../") - 1; 2087 } 2088 } 2089 2090 /* Skip drive letter */ 2091 target_start = target; 2092 while (*target_start && *target_start != ':') 2093 ++target_start; 2094 2095 if (!*target_start) { 2096 ntfs_err(sb, "Link target (%s) missing drive separator", 2097 target); 2098 err = -EINVAL; 2099 goto out; 2100 } 2101 2102 /* Skip drive separator and leading /, if exists */ 2103 target_start += 1 + (target_start[1] == '/'); 2104 copy_len = target_len - (target_start - target); 2105 2106 if (PATH_MAX - tl_len <= copy_len) { 2107 ntfs_err(sb, "Link target %s too large for buffer (%d <= %d)", 2108 target_start, PATH_MAX - tl_len, copy_len); 2109 err = -EINVAL; 2110 goto out; 2111 } 2112 2113 /* translated path has a trailing / and target_start does not */ 2114 strcpy(translated + tl_len, target_start); 2115 tl_len += copy_len; 2116 if (target_max <= tl_len) { 2117 ntfs_err(sb, "Target path %s too large for buffer (%d <= %d)", 2118 translated, target_max, tl_len); 2119 err = -EINVAL; 2120 goto out; 2121 } 2122 strcpy(target, translated); 2123 err = tl_len; 2124 2125 out: 2126 kfree(link_path_buffer); 2127 kfree(translated); 2128 return err; 2129 } 2130 2131 static noinline int ntfs_readlink_hlp(const struct dentry *link_de, 2132 struct inode *inode, char *buffer, 2133 int buflen) 2134 { 2135 int i, err = -EINVAL; 2136 struct ntfs_inode *ni = ntfs_i(inode); 2137 struct super_block *sb = inode->i_sb; 2138 struct ntfs_sb_info *sbi = sb->s_fs_info; 2139 u64 size; 2140 u16 ulen = 0; 2141 void *to_free = NULL; 2142 struct REPARSE_DATA_BUFFER *rp; 2143 const __le16 *uname; 2144 struct ATTRIB *attr; 2145 2146 /* Reparse data present. Try to parse it. */ 2147 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag)); 2148 static_assert(sizeof(u32) == sizeof(rp->ReparseTag)); 2149 2150 *buffer = 0; 2151 2152 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL); 2153 if (!attr) 2154 goto out; 2155 2156 if (!attr->non_res) { 2157 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER)); 2158 if (!rp) 2159 goto out; 2160 size = le32_to_cpu(attr->res.data_size); 2161 } else { 2162 size = le64_to_cpu(attr->nres.data_size); 2163 rp = NULL; 2164 } 2165 2166 if (size > sbi->reparse.max_size || size <= sizeof(u32)) 2167 goto out; 2168 2169 if (!rp) { 2170 rp = kmalloc(size, GFP_NOFS); 2171 if (!rp) { 2172 err = -ENOMEM; 2173 goto out; 2174 } 2175 to_free = rp; 2176 /* Read into temporal buffer. */ 2177 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL); 2178 if (err) 2179 goto out; 2180 } 2181 2182 /* Microsoft Tag. */ 2183 switch (rp->ReparseTag) { 2184 case IO_REPARSE_TAG_MOUNT_POINT: 2185 /* Mount points and junctions. */ 2186 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */ 2187 if (size <= offsetof(struct REPARSE_DATA_BUFFER, 2188 MountPointReparseBuffer.PathBuffer)) 2189 goto out; 2190 uname = Add2Ptr(rp, 2191 offsetof(struct REPARSE_DATA_BUFFER, 2192 MountPointReparseBuffer.PathBuffer) + 2193 le16_to_cpu(rp->MountPointReparseBuffer 2194 .PrintNameOffset)); 2195 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength); 2196 break; 2197 2198 case IO_REPARSE_TAG_SYMLINK: 2199 /* FolderSymbolicLink */ 2200 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */ 2201 if (size <= offsetof(struct REPARSE_DATA_BUFFER, 2202 SymbolicLinkReparseBuffer.PathBuffer)) 2203 goto out; 2204 uname = Add2Ptr( 2205 rp, offsetof(struct REPARSE_DATA_BUFFER, 2206 SymbolicLinkReparseBuffer.PathBuffer) + 2207 le16_to_cpu(rp->SymbolicLinkReparseBuffer 2208 .PrintNameOffset)); 2209 ulen = le16_to_cpu( 2210 rp->SymbolicLinkReparseBuffer.PrintNameLength); 2211 break; 2212 2213 case IO_REPARSE_TAG_CLOUD: 2214 case IO_REPARSE_TAG_CLOUD_1: 2215 case IO_REPARSE_TAG_CLOUD_2: 2216 case IO_REPARSE_TAG_CLOUD_3: 2217 case IO_REPARSE_TAG_CLOUD_4: 2218 case IO_REPARSE_TAG_CLOUD_5: 2219 case IO_REPARSE_TAG_CLOUD_6: 2220 case IO_REPARSE_TAG_CLOUD_7: 2221 case IO_REPARSE_TAG_CLOUD_8: 2222 case IO_REPARSE_TAG_CLOUD_9: 2223 case IO_REPARSE_TAG_CLOUD_A: 2224 case IO_REPARSE_TAG_CLOUD_B: 2225 case IO_REPARSE_TAG_CLOUD_C: 2226 case IO_REPARSE_TAG_CLOUD_D: 2227 case IO_REPARSE_TAG_CLOUD_E: 2228 case IO_REPARSE_TAG_CLOUD_F: 2229 err = sizeof("OneDrive") - 1; 2230 if (err > buflen) 2231 err = buflen; 2232 memcpy(buffer, "OneDrive", err); 2233 goto out; 2234 2235 default: 2236 if (IsReparseTagMicrosoft(rp->ReparseTag)) { 2237 /* Unknown Microsoft Tag. */ 2238 goto out; 2239 } 2240 if (!IsReparseTagNameSurrogate(rp->ReparseTag) || 2241 size <= sizeof(struct REPARSE_POINT)) { 2242 goto out; 2243 } 2244 2245 /* Users tag. */ 2246 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT)); 2247 ulen = le16_to_cpu(rp->ReparseDataLength) - 2248 sizeof(struct REPARSE_POINT); 2249 } 2250 2251 /* Convert nlen from bytes to UNICODE chars. */ 2252 ulen >>= 1; 2253 2254 /* Check that name is available. */ 2255 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size)) 2256 goto out; 2257 2258 /* If name is already zero terminated then truncate it now. */ 2259 if (!uname[ulen - 1]) 2260 ulen -= 1; 2261 2262 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen); 2263 2264 if (err < 0) 2265 goto out; 2266 2267 if (err >= buflen) 2268 err = buflen - 1; 2269 2270 /* Translate Windows '\' into Linux '/'. */ 2271 for (i = 0; i < err; i++) { 2272 if (buffer[i] == '\\') 2273 buffer[i] = '/'; 2274 } 2275 2276 /* Always set last zero. */ 2277 buffer[err] = 0; 2278 2279 /* If this is a junction, translate the link target. */ 2280 if (rp->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) 2281 err = ntfs_translate_junction(sb, link_de, buffer, err, buflen); 2282 2283 out: 2284 kfree(to_free); 2285 return err; 2286 } 2287 2288 static const char *ntfs_get_link(struct dentry *de, struct inode *inode, 2289 struct delayed_call *done) 2290 { 2291 int err; 2292 char *ret; 2293 2294 if (!de) 2295 return ERR_PTR(-ECHILD); 2296 2297 ret = kmalloc(PAGE_SIZE, GFP_NOFS); 2298 if (!ret) 2299 return ERR_PTR(-ENOMEM); 2300 2301 err = ntfs_readlink_hlp(de, inode, ret, PAGE_SIZE); 2302 if (err < 0) { 2303 kfree(ret); 2304 return ERR_PTR(err); 2305 } 2306 2307 set_delayed_call(done, kfree_link, ret); 2308 2309 return ret; 2310 } 2311 2312 // clang-format off 2313 const struct inode_operations ntfs_link_inode_operations = { 2314 .get_link = ntfs_get_link, 2315 .setattr = ntfs_setattr, 2316 .listxattr = ntfs_listxattr, 2317 .fileattr_get = ntfs_fileattr_get, 2318 .fileattr_set = ntfs_fileattr_set, 2319 }; 2320 2321 const struct address_space_operations ntfs_aops = { 2322 .read_folio = ntfs_read_folio, 2323 .readahead = ntfs_readahead, 2324 .writepages = ntfs_writepages, 2325 .bmap = ntfs_bmap, 2326 .dirty_folio = iomap_dirty_folio, 2327 .migrate_folio = filemap_migrate_folio, 2328 .release_folio = iomap_release_folio, 2329 .invalidate_folio = iomap_invalidate_folio, 2330 }; 2331 2332 const struct address_space_operations ntfs_aops_cmpr = { 2333 .read_folio = ntfs_read_folio, 2334 .dirty_folio = iomap_dirty_folio, 2335 .release_folio = iomap_release_folio, 2336 .invalidate_folio = iomap_invalidate_folio, 2337 }; 2338 2339 DEFINE_IOMAP_ITER_NEXT_END(ntfs_iomap_next, ntfs_iomap_begin, ntfs_iomap_end); 2340 2341 const struct iomap_write_ops ntfs_iomap_folio_ops = { 2342 .put_folio = ntfs_iomap_put_folio, 2343 }; 2344 // clang-format on 2345