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