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