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