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