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