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 iomap_bio_submit_read_endio(iter, ctx, ntfs_iomap_read_end_io); 612 } 613 614 static const struct iomap_read_ops ntfs_iomap_bio_read_ops = { 615 .read_folio_range = iomap_bio_read_folio_range, 616 .submit_read = ntfs_iomap_bio_submit_read, 617 }; 618 619 static int ntfs_read_folio(struct file *file, struct folio *folio) 620 { 621 int err; 622 struct address_space *mapping = folio->mapping; 623 struct inode *inode = mapping->host; 624 struct ntfs_inode *ni = ntfs_i(inode); 625 loff_t vbo = folio_pos(folio); 626 struct iomap_read_folio_ctx ctx = { 627 .cur_folio = folio, 628 .ops = &ntfs_iomap_bio_read_ops, 629 }; 630 631 if (unlikely(is_bad_ni(ni))) { 632 folio_unlock(folio); 633 return -EIO; 634 } 635 636 if (ni->i_valid <= vbo) { 637 folio_zero_range(folio, 0, folio_size(folio)); 638 folio_mark_uptodate(folio); 639 folio_unlock(folio); 640 return 0; 641 } 642 643 if (is_compressed(ni)) { 644 /* ni_lock is taken inside ni_read_folio_cmpr after page locks */ 645 err = ni_read_folio_cmpr(ni, folio); 646 return err; 647 } 648 649 iomap_read_folio(&ntfs_iomap_ops, &ctx, NULL); 650 return 0; 651 } 652 653 static void ntfs_readahead(struct readahead_control *rac) 654 { 655 struct address_space *mapping = rac->mapping; 656 struct inode *inode = mapping->host; 657 struct ntfs_inode *ni = ntfs_i(inode); 658 struct iomap_read_folio_ctx ctx = { 659 .ops = &ntfs_iomap_bio_read_ops, 660 .rac = rac, 661 }; 662 663 if (is_resident(ni)) { 664 /* No readahead for resident. */ 665 return; 666 } 667 668 if (is_compressed(ni)) { 669 /* No readahead for compressed. */ 670 return; 671 } 672 673 iomap_readahead(&ntfs_iomap_ops, &ctx, NULL); 674 } 675 676 int ntfs_set_size(struct inode *inode, u64 new_size) 677 { 678 struct super_block *sb = inode->i_sb; 679 struct ntfs_sb_info *sbi = sb->s_fs_info; 680 struct ntfs_inode *ni = ntfs_i(inode); 681 int err; 682 683 /* Check for maximum file size. */ 684 if (is_sparsed(ni) || is_compressed(ni)) { 685 if (new_size > sbi->maxbytes_sparse) { 686 return -EFBIG; 687 } 688 } else if (new_size > sbi->maxbytes) { 689 return -EFBIG; 690 } 691 692 /* Mark rw ntfs as dirty. It will be cleared at umount. */ 693 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 694 695 ni_lock(ni); 696 down_write(&ni->file.run_lock); 697 if (new_size < ni->i_valid) 698 ni->i_valid = new_size; 699 700 /* last 'true' means keep preallocated. */ 701 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, 702 &ni->i_valid, true); 703 704 up_write(&ni->file.run_lock); 705 ni_unlock(ni); 706 707 return err; 708 } 709 710 /* 711 * Special value to detect ntfs_writeback_range call 712 */ 713 #define WB_NO_DA (struct iomap *)1 714 /* 715 * Function to get mapping vbo -> lbo. 716 * used with: 717 * - iomap_zero_range 718 * - iomap_truncate_page 719 * - iomap_dio_rw 720 * - iomap_file_buffered_write 721 * - iomap_bmap 722 * - iomap_fiemap 723 * - iomap_bio_read_folio 724 * - iomap_bio_readahead 725 */ 726 static int ntfs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 727 unsigned int flags, struct iomap *iomap, 728 struct iomap *srcmap) 729 { 730 struct ntfs_inode *ni = ntfs_i(inode); 731 struct ntfs_sb_info *sbi = ni->mi.sbi; 732 u8 cluster_bits = sbi->cluster_bits; 733 CLST vcn = offset >> cluster_bits; 734 u32 off = offset & sbi->cluster_mask; 735 bool rw = flags & IOMAP_WRITE; 736 loff_t endbyte = offset + length; 737 void *res = NULL; 738 int err; 739 CLST lcn, clen, clen_max = 1; 740 bool new_clst = false; 741 bool no_da; 742 bool zero = false; 743 if (unlikely(ntfs3_forced_shutdown(sbi->sb))) 744 return -EIO; 745 746 if (flags & IOMAP_REPORT) { 747 if (offset > ntfs_get_maxbytes(ni)) { 748 /* called from fiemap/bmap. */ 749 return -EINVAL; 750 } 751 752 if (offset >= inode->i_size) { 753 /* special code for report. */ 754 return -ENOENT; 755 } 756 } 757 758 if (IOMAP_ZERO == flags && (endbyte & sbi->cluster_mask)) { 759 rw = true; 760 } else if (rw) { 761 clen_max = bytes_to_cluster(sbi, endbyte) - vcn; 762 } 763 764 /* 765 * Force to allocate clusters if directIO(write) or writeback_range. 766 * NOTE: attr_data_get_block allocates clusters only for sparse file. 767 * Normal file allocates clusters in attr_set_size. 768 */ 769 no_da = flags == (IOMAP_DIRECT | IOMAP_WRITE) || srcmap == WB_NO_DA; 770 771 err = attr_data_get_block(ni, vcn, clen_max, &lcn, &clen, 772 rw ? &new_clst : NULL, zero, &res, no_da); 773 774 if (err) { 775 return err; 776 } 777 778 if (lcn == EOF_LCN) { 779 /* request out of file. */ 780 if (flags & IOMAP_REPORT) { 781 /* special code for report. */ 782 return -ENOENT; 783 } 784 785 if (rw) { 786 /* should never be here. */ 787 return -EINVAL; 788 } 789 lcn = SPARSE_LCN; 790 } 791 792 iomap->flags = new_clst ? IOMAP_F_NEW : 0; 793 794 if (lcn == RESIDENT_LCN) { 795 if (offset >= clen) { 796 __free_page(virt_to_page(res)); 797 if (flags & IOMAP_REPORT) { 798 /* special code for report. */ 799 return -ENOENT; 800 } 801 return -EFAULT; 802 } 803 804 iomap->private = iomap->inline_data = res; 805 iomap->type = IOMAP_INLINE; 806 iomap->offset = 0; 807 iomap->length = clen; /* resident size in bytes. */ 808 return 0; 809 } 810 811 if (!clen) { 812 /* broken file? */ 813 return -EINVAL; 814 } 815 816 iomap->bdev = inode->i_sb->s_bdev; 817 iomap->offset = offset; 818 iomap->length = ((loff_t)clen << cluster_bits) - off; 819 820 if (lcn == COMPRESSED_LCN) { 821 /* should never be here. */ 822 return -EOPNOTSUPP; 823 } 824 825 if (lcn == DELALLOC_LCN) { 826 iomap->type = IOMAP_DELALLOC; 827 iomap->addr = IOMAP_NULL_ADDR; 828 } else { 829 /* Translate clusters into bytes. */ 830 iomap->addr = ((loff_t)lcn << cluster_bits) + off; 831 if (length && iomap->length > length) 832 iomap->length = length; 833 else 834 endbyte = offset + iomap->length; 835 836 if (lcn == SPARSE_LCN) { 837 iomap->addr = IOMAP_NULL_ADDR; 838 iomap->type = IOMAP_HOLE; 839 // if (IOMAP_ZERO == flags && !off) { 840 // iomap->length = (endbyte - offset) & 841 // sbi->cluster_mask_inv; 842 // } 843 } else if (endbyte <= ni->i_valid) { 844 iomap->type = IOMAP_MAPPED; 845 } else if (offset < ni->i_valid) { 846 iomap->type = IOMAP_MAPPED; 847 if (flags & IOMAP_REPORT) 848 iomap->length = ni->i_valid - offset; 849 } else if (rw || (flags & IOMAP_ZERO)) { 850 iomap->type = IOMAP_MAPPED; 851 } else { 852 iomap->type = IOMAP_UNWRITTEN; 853 } 854 } 855 856 if ((flags & IOMAP_ZERO) && 857 (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) { 858 /* Avoid too large requests. */ 859 u32 tail; 860 u32 off_a = offset & (PAGE_SIZE - 1); 861 if (off_a) 862 tail = PAGE_SIZE - off_a; 863 else 864 tail = PAGE_SIZE; 865 866 if (iomap->length > tail) 867 iomap->length = tail; 868 } 869 870 return 0; 871 } 872 873 static int ntfs_iomap_end(struct inode *inode, loff_t pos, loff_t length, 874 ssize_t written, unsigned int flags, 875 struct iomap *iomap) 876 { 877 int err = 0; 878 struct ntfs_inode *ni = ntfs_i(inode); 879 loff_t endbyte = pos + written; 880 881 if ((flags & IOMAP_WRITE) || (flags & IOMAP_ZERO)) { 882 if (iomap->type == IOMAP_INLINE) { 883 u32 data_size; 884 struct ATTRIB *attr; 885 struct mft_inode *mi; 886 887 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, 888 NULL, &mi); 889 if (!attr || attr->non_res) { 890 err = -EINVAL; 891 goto out; 892 } 893 894 data_size = le32_to_cpu(attr->res.data_size); 895 if (!(pos < data_size && endbyte <= data_size)) { 896 err = -EINVAL; 897 goto out; 898 } 899 900 /* Update resident data. */ 901 memcpy(resident_data(attr) + pos, 902 iomap_inline_data(iomap, pos), written); 903 mi->dirty = true; 904 ni->i_valid = data_size; 905 } else if (ni->i_valid < endbyte) { 906 ni->i_valid = endbyte; 907 mark_inode_dirty(inode); 908 } 909 } 910 911 if ((flags & IOMAP_ZERO) && 912 (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) { 913 /* Pair for code in ntfs_iomap_begin. */ 914 balance_dirty_pages_ratelimited(inode->i_mapping); 915 cond_resched(); 916 } 917 918 out: 919 if (iomap->type == IOMAP_INLINE) { 920 __free_page(virt_to_page(iomap->private)); 921 iomap->private = NULL; 922 } 923 924 return err; 925 } 926 927 /* 928 * write_begin + put_folio + write_end. 929 * iomap_zero_range 930 * iomap_truncate_page 931 * iomap_file_buffered_write 932 */ 933 static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos, 934 unsigned int len, struct folio *folio) 935 { 936 struct ntfs_inode *ni = ntfs_i(inode); 937 loff_t end = pos + len; 938 u32 f_size = folio_size(folio); 939 loff_t f_pos = folio_pos(folio); 940 loff_t f_end = f_pos + f_size; 941 942 if (ni->i_valid <= end && end < f_end) { 943 /* zero range [end - f_end). */ 944 /* The only thing ntfs_iomap_put_folio used for. */ 945 folio_zero_segment(folio, offset_in_folio(folio, end), f_size); 946 } 947 folio_unlock(folio); 948 folio_put(folio); 949 } 950 951 /* 952 * iomap_writeback_ops::writeback_range 953 */ 954 static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc, 955 struct folio *folio, u64 offset, 956 unsigned int len, u64 end_pos) 957 { 958 struct iomap *iomap = &wpc->iomap; 959 /* Check iomap position. */ 960 if (iomap->offset + iomap->length <= offset || offset < iomap->offset) { 961 int err; 962 struct inode *inode = wpc->inode; 963 struct ntfs_inode *ni = ntfs_i(inode); 964 struct ntfs_sb_info *sbi = ntfs_sb(inode->i_sb); 965 loff_t i_size_up = ntfs_up_cluster(sbi, inode->i_size); 966 loff_t len_max = i_size_up - offset; 967 968 err = ni->file.run_da.count ? ni_allocate_da_blocks(ni) : 0; 969 970 if (!err) { 971 /* Use local special value 'WB_NO_DA' to disable delalloc. */ 972 err = ntfs_iomap_begin(inode, offset, len_max, 973 IOMAP_WRITE, iomap, WB_NO_DA); 974 } 975 976 if (err) { 977 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 978 return err; 979 } 980 } 981 982 return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); 983 } 984 985 static const struct iomap_writeback_ops ntfs_writeback_ops = { 986 .writeback_range = ntfs_writeback_range, 987 .writeback_submit = iomap_ioend_writeback_submit, 988 }; 989 990 static int ntfs_writepages(struct address_space *mapping, 991 struct writeback_control *wbc) 992 { 993 int err; 994 struct inode *inode = mapping->host; 995 struct ntfs_inode *ni = ntfs_i(inode); 996 struct iomap_writepage_ctx wpc = { 997 .inode = inode, 998 .wbc = wbc, 999 .ops = &ntfs_writeback_ops, 1000 }; 1001 1002 /* Avoid any operation if inode is bad. */ 1003 if (unlikely(is_bad_ni(ni))) 1004 return -EINVAL; 1005 1006 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1007 return -EIO; 1008 1009 if (is_resident(ni)) { 1010 struct folio *folio = NULL; 1011 err = 0; 1012 1013 while ((folio = writeback_iter(mapping, wbc, folio, &err))) { 1014 int err2; 1015 1016 ni_lock(ni); 1017 err2 = attr_data_write_resident(ni, folio); 1018 ni_unlock(ni); 1019 1020 folio_unlock(folio); 1021 if (err2) { 1022 mapping_set_error(mapping, err2); 1023 if (!err) 1024 err = err2; 1025 } 1026 } 1027 1028 return err; 1029 } 1030 1031 return iomap_writepages(&wpc); 1032 } 1033 1034 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc) 1035 { 1036 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 1037 } 1038 1039 int ntfs_sync_inode(struct inode *inode) 1040 { 1041 return _ni_write_inode(inode, 1); 1042 } 1043 1044 /* 1045 * Helper function to read file. 1046 * Used to read $AttrDef and $UpCase 1047 */ 1048 int inode_read_data(struct inode *inode, void *data, size_t bytes) 1049 { 1050 pgoff_t idx; 1051 struct address_space *mapping = inode->i_mapping; 1052 1053 for (idx = 0; bytes; idx++) { 1054 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes; 1055 struct page *page = read_mapping_page(mapping, idx, NULL); 1056 void *kaddr; 1057 1058 if (IS_ERR(page)) 1059 return PTR_ERR(page); 1060 1061 kaddr = kmap_atomic(page); 1062 memcpy(data, kaddr, op); 1063 kunmap_atomic(kaddr); 1064 1065 put_page(page); 1066 1067 bytes -= op; 1068 data = Add2Ptr(data, PAGE_SIZE); 1069 } 1070 return 0; 1071 } 1072 1073 /* 1074 * ntfs_reparse_bytes 1075 * 1076 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK) 1077 * for unicode string of @uni_len length. 1078 */ 1079 static inline u32 ntfs_reparse_bytes(u32 uni_len, bool is_absolute) 1080 { 1081 /* Header + unicode string + decorated unicode string. */ 1082 return sizeof(short) * (2 * uni_len + (is_absolute ? 4 : 0)) + 1083 offsetof(struct REPARSE_DATA_BUFFER, 1084 SymbolicLinkReparseBuffer.PathBuffer); 1085 } 1086 1087 static struct REPARSE_DATA_BUFFER * 1088 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname, 1089 u32 size, u16 *nsize) 1090 { 1091 int i, err; 1092 struct REPARSE_DATA_BUFFER *rp; 1093 __le16 *rp_name; 1094 typeof(rp->SymbolicLinkReparseBuffer) *rs; 1095 bool is_absolute; 1096 1097 is_absolute = symname[0] && symname[1] == ':'; 1098 1099 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2, is_absolute), GFP_NOFS); 1100 if (!rp) 1101 return ERR_PTR(-ENOMEM); 1102 1103 rs = &rp->SymbolicLinkReparseBuffer; 1104 rp_name = rs->PathBuffer; 1105 1106 /* Convert link name to UTF-16. */ 1107 err = ntfs_nls_to_utf16(sbi, symname, size, 1108 (struct cpu_str *)(rp_name - 1), 2 * size, 1109 UTF16_LITTLE_ENDIAN); 1110 if (err < 0) 1111 goto out; 1112 1113 /* err = the length of unicode name of symlink. */ 1114 *nsize = ntfs_reparse_bytes(err, is_absolute); 1115 1116 if (*nsize > sbi->reparse.max_size) { 1117 err = -EFBIG; 1118 goto out; 1119 } 1120 1121 /* Translate Linux '/' into Windows '\'. */ 1122 for (i = 0; i < err; i++) { 1123 if (rp_name[i] == cpu_to_le16('/')) 1124 rp_name[i] = cpu_to_le16('\\'); 1125 } 1126 1127 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK; 1128 rp->ReparseDataLength = 1129 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER, 1130 SymbolicLinkReparseBuffer)); 1131 1132 /* PrintName + SubstituteName. */ 1133 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err); 1134 rs->SubstituteNameLength = 1135 cpu_to_le16(sizeof(short) * err + (is_absolute ? 8 : 0)); 1136 rs->PrintNameLength = rs->SubstituteNameOffset; 1137 1138 /* 1139 * TODO: Use relative path if possible to allow Windows to 1140 * parse this path. 1141 * 0-absolute path, 1- relative path (SYMLINK_FLAG_RELATIVE). 1142 */ 1143 rs->Flags = cpu_to_le32(is_absolute ? 0 : SYMLINK_FLAG_RELATIVE); 1144 1145 memmove(rp_name + err + (is_absolute ? 4 : 0), rp_name, 1146 sizeof(short) * err); 1147 1148 if (is_absolute) { 1149 /* Decorate SubstituteName. */ 1150 rp_name += err; 1151 rp_name[0] = cpu_to_le16('\\'); 1152 rp_name[1] = cpu_to_le16('?'); 1153 rp_name[2] = cpu_to_le16('?'); 1154 rp_name[3] = cpu_to_le16('\\'); 1155 } 1156 1157 return rp; 1158 out: 1159 kfree(rp); 1160 return ERR_PTR(err); 1161 } 1162 1163 /* 1164 * ntfs_create_inode 1165 * 1166 * Helper function for: 1167 * - ntfs_create 1168 * - ntfs_mknod 1169 * - ntfs_symlink 1170 * - ntfs_mkdir 1171 * - ntfs_atomic_open 1172 * 1173 * NOTE: if fnd != NULL (ntfs_atomic_open) then @dir is locked 1174 */ 1175 int ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir, 1176 struct dentry *dentry, const struct cpu_str *uni, 1177 umode_t mode, dev_t dev, const char *symname, u32 size, 1178 struct ntfs_fnd *fnd) 1179 { 1180 int err; 1181 struct super_block *sb = dir->i_sb; 1182 struct ntfs_sb_info *sbi = sb->s_fs_info; 1183 const struct qstr *name = &dentry->d_name; 1184 CLST ino = 0; 1185 struct ntfs_inode *dir_ni = ntfs_i(dir); 1186 struct ntfs_inode *ni = NULL; 1187 struct inode *inode = NULL; 1188 struct ATTRIB *attr; 1189 struct ATTR_STD_INFO5 *std5; 1190 struct ATTR_FILE_NAME *fname; 1191 struct MFT_REC *rec; 1192 u32 asize, dsize, sd_size; 1193 enum FILE_ATTRIBUTE fa; 1194 __le32 security_id = SECURITY_ID_INVALID; 1195 CLST vcn; 1196 const void *sd; 1197 u16 t16, nsize = 0, aid = 0; 1198 struct INDEX_ROOT *root, *dir_root; 1199 struct NTFS_DE *e, *new_de = NULL; 1200 struct REPARSE_DATA_BUFFER *rp = NULL; 1201 bool rp_inserted = false; 1202 1203 /* New file will be resident or non resident. */ 1204 const bool new_file_resident = 1; 1205 1206 if (!fnd) 1207 ni_lock_dir(dir_ni); 1208 1209 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL); 1210 if (!dir_root) { 1211 err = -EINVAL; 1212 goto out1; 1213 } 1214 1215 if (S_ISDIR(mode)) { 1216 /* Use parent's directory attributes. */ 1217 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY | 1218 FILE_ATTRIBUTE_ARCHIVE; 1219 /* 1220 * By default child directory inherits parent attributes. 1221 * Root directory is hidden + system. 1222 * Make an exception for children in root. 1223 */ 1224 if (dir->i_ino == MFT_REC_ROOT) 1225 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM); 1226 } else if (S_ISLNK(mode)) { 1227 /* It is good idea that link should be the same type (file/dir) as target */ 1228 fa = FILE_ATTRIBUTE_REPARSE_POINT; 1229 1230 /* 1231 * Linux: there are dir/file/symlink and so on. 1232 * NTFS: symlinks are "dir + reparse" or "file + reparse" 1233 * It is good idea to create: 1234 * dir + reparse if 'symname' points to directory 1235 * or 1236 * file + reparse if 'symname' points to file 1237 * Unfortunately kern_path hangs if symname contains 'dir'. 1238 */ 1239 1240 /* 1241 * struct path path; 1242 * 1243 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){ 1244 * struct inode *target = d_inode(path.dentry); 1245 * 1246 * if (S_ISDIR(target->i_mode)) 1247 * fa |= FILE_ATTRIBUTE_DIRECTORY; 1248 * // if ( target->i_sb == sb ){ 1249 * // use relative path? 1250 * // } 1251 * path_put(&path); 1252 * } 1253 */ 1254 } else if (S_ISREG(mode)) { 1255 if (sbi->options->sparse) { 1256 /* Sparsed regular file, cause option 'sparse'. */ 1257 fa = FILE_ATTRIBUTE_SPARSE_FILE | 1258 FILE_ATTRIBUTE_ARCHIVE; 1259 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) { 1260 /* Compressed regular file, if parent is compressed. */ 1261 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE; 1262 } else { 1263 /* Regular file, default attributes. */ 1264 fa = FILE_ATTRIBUTE_ARCHIVE; 1265 } 1266 } else { 1267 fa = FILE_ATTRIBUTE_ARCHIVE; 1268 } 1269 1270 /* If option "hide_dot_files" then set hidden attribute for dot files. */ 1271 if (sbi->options->hide_dot_files && name->name[0] == '.') 1272 fa |= FILE_ATTRIBUTE_HIDDEN; 1273 1274 if (!(mode & 0222)) 1275 fa |= FILE_ATTRIBUTE_READONLY; 1276 1277 new_de = kzalloc(PATH_MAX, GFP_KERNEL); 1278 if (!new_de) { 1279 err = -ENOMEM; 1280 goto out1; 1281 } 1282 1283 /* Avoid any operation if inode is bad. */ 1284 if (unlikely(is_bad_ni(dir_ni))) { 1285 err = -EINVAL; 1286 goto out2; 1287 } 1288 1289 if (unlikely(ntfs3_forced_shutdown(sb))) { 1290 err = -EIO; 1291 goto out2; 1292 } 1293 1294 /* Mark rw ntfs as dirty. it will be cleared at umount. */ 1295 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 1296 1297 /* Step 1: allocate and fill new mft record. */ 1298 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL); 1299 if (err) 1300 goto out2; 1301 1302 ni = ntfs_new_inode(sbi, ino, S_ISDIR(mode) ? RECORD_FLAG_DIR : 0); 1303 if (IS_ERR(ni)) { 1304 err = PTR_ERR(ni); 1305 ni = NULL; 1306 goto out3; 1307 } 1308 inode = &ni->vfs_inode; 1309 inode_init_owner(idmap, inode, dir, mode); 1310 mode = inode->i_mode; 1311 1312 ni->i_crtime = current_time(inode); 1313 1314 rec = ni->mi.mrec; 1315 rec->hard_links = cpu_to_le16(1); 1316 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off)); 1317 1318 /* Get default security id. */ 1319 sd = s_default_security; 1320 sd_size = sizeof(s_default_security); 1321 1322 if (is_ntfs3(sbi)) { 1323 security_id = dir_ni->std_security_id; 1324 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) { 1325 security_id = sbi->security.def_security_id; 1326 1327 if (security_id == SECURITY_ID_INVALID && 1328 !ntfs_insert_security(sbi, sd, sd_size, 1329 &security_id, NULL)) 1330 sbi->security.def_security_id = security_id; 1331 } 1332 } 1333 1334 /* Insert standard info. */ 1335 std5 = Add2Ptr(attr, SIZEOF_RESIDENT); 1336 1337 if (security_id == SECURITY_ID_INVALID) { 1338 dsize = sizeof(struct ATTR_STD_INFO); 1339 } else { 1340 dsize = sizeof(struct ATTR_STD_INFO5); 1341 std5->security_id = security_id; 1342 ni->std_security_id = security_id; 1343 } 1344 asize = SIZEOF_RESIDENT + dsize; 1345 1346 attr->type = ATTR_STD; 1347 attr->size = cpu_to_le32(asize); 1348 attr->id = cpu_to_le16(aid++); 1349 attr->res.data_off = SIZEOF_RESIDENT_LE; 1350 attr->res.data_size = cpu_to_le32(dsize); 1351 1352 std5->cr_time = std5->m_time = std5->c_time = std5->a_time = 1353 kernel2nt(&ni->i_crtime); 1354 1355 std5->fa = ni->std_fa = fa; 1356 1357 attr = Add2Ptr(attr, asize); 1358 1359 /* Insert file name. */ 1360 err = fill_name_de(sbi, new_de, name, uni); 1361 if (err) 1362 goto out4; 1363 1364 mi_get_ref(&ni->mi, &new_de->ref); 1365 1366 fname = (struct ATTR_FILE_NAME *)(new_de + 1); 1367 1368 if (sbi->options->windows_names && 1369 !valid_windows_name(sbi, (struct le_str *)&fname->name_len)) { 1370 err = -EINVAL; 1371 goto out4; 1372 } 1373 1374 mi_get_ref(&dir_ni->mi, &fname->home); 1375 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time = 1376 fname->dup.a_time = std5->cr_time; 1377 fname->dup.alloc_size = fname->dup.data_size = 0; 1378 fname->dup.fa = std5->fa; 1379 fname->dup.extend_data = S_ISLNK(mode) ? IO_REPARSE_TAG_SYMLINK : 0; 1380 1381 dsize = le16_to_cpu(new_de->key_size); 1382 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8); 1383 1384 attr->type = ATTR_NAME; 1385 attr->size = cpu_to_le32(asize); 1386 attr->res.data_off = SIZEOF_RESIDENT_LE; 1387 attr->res.flags = RESIDENT_FLAG_INDEXED; 1388 attr->id = cpu_to_le16(aid++); 1389 attr->res.data_size = cpu_to_le32(dsize); 1390 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize); 1391 1392 attr = Add2Ptr(attr, asize); 1393 1394 if (security_id == SECURITY_ID_INVALID) { 1395 /* Insert security attribute. */ 1396 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8); 1397 1398 attr->type = ATTR_SECURE; 1399 attr->size = cpu_to_le32(asize); 1400 attr->id = cpu_to_le16(aid++); 1401 attr->res.data_off = SIZEOF_RESIDENT_LE; 1402 attr->res.data_size = cpu_to_le32(sd_size); 1403 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size); 1404 1405 attr = Add2Ptr(attr, asize); 1406 } 1407 1408 attr->id = cpu_to_le16(aid++); 1409 if (fa & FILE_ATTRIBUTE_DIRECTORY) { 1410 /* 1411 * Regular directory or symlink to directory. 1412 * Create root attribute. 1413 */ 1414 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE); 1415 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize; 1416 1417 attr->type = ATTR_ROOT; 1418 attr->size = cpu_to_le32(asize); 1419 1420 attr->name_len = ARRAY_SIZE(I30_NAME); 1421 attr->name_off = SIZEOF_RESIDENT_LE; 1422 attr->res.data_off = 1423 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT); 1424 attr->res.data_size = cpu_to_le32(dsize); 1425 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME, 1426 sizeof(I30_NAME)); 1427 1428 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT); 1429 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr)); 1430 root->ihdr.de_off = cpu_to_le32(sizeof(struct INDEX_HDR)); 1431 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) + 1432 sizeof(struct NTFS_DE)); 1433 root->ihdr.total = root->ihdr.used; 1434 1435 e = Add2Ptr(root, sizeof(struct INDEX_ROOT)); 1436 e->size = cpu_to_le16(sizeof(struct NTFS_DE)); 1437 e->flags = NTFS_IE_LAST; 1438 } else if (S_ISLNK(mode)) { 1439 /* 1440 * Symlink to file. 1441 * Create empty resident data attribute. 1442 */ 1443 asize = SIZEOF_RESIDENT; 1444 1445 /* Insert empty ATTR_DATA */ 1446 attr->type = ATTR_DATA; 1447 attr->size = cpu_to_le32(SIZEOF_RESIDENT); 1448 attr->name_off = SIZEOF_RESIDENT_LE; 1449 attr->res.data_off = SIZEOF_RESIDENT_LE; 1450 } else if (!new_file_resident && S_ISREG(mode)) { 1451 /* 1452 * Regular file. Create empty non resident data attribute. 1453 */ 1454 attr->type = ATTR_DATA; 1455 attr->non_res = 1; 1456 attr->nres.evcn = cpu_to_le64(-1ll); 1457 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) { 1458 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8); 1459 attr->name_off = SIZEOF_NONRESIDENT_EX_LE; 1460 attr->flags = ATTR_FLAG_SPARSED; 1461 asize = SIZEOF_NONRESIDENT_EX + 8; 1462 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) { 1463 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8); 1464 attr->name_off = SIZEOF_NONRESIDENT_EX_LE; 1465 attr->flags = ATTR_FLAG_COMPRESSED; 1466 attr->nres.c_unit = NTFS_LZNT_CUNIT; 1467 asize = SIZEOF_NONRESIDENT_EX + 8; 1468 } else { 1469 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8); 1470 attr->name_off = SIZEOF_NONRESIDENT_LE; 1471 asize = SIZEOF_NONRESIDENT + 8; 1472 } 1473 attr->nres.run_off = attr->name_off; 1474 } else { 1475 /* 1476 * Node. Create empty resident data attribute. 1477 */ 1478 attr->type = ATTR_DATA; 1479 attr->size = cpu_to_le32(SIZEOF_RESIDENT); 1480 attr->name_off = SIZEOF_RESIDENT_LE; 1481 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) 1482 attr->flags = ATTR_FLAG_SPARSED; 1483 else if (fa & FILE_ATTRIBUTE_COMPRESSED) 1484 attr->flags = ATTR_FLAG_COMPRESSED; 1485 attr->res.data_off = SIZEOF_RESIDENT_LE; 1486 asize = SIZEOF_RESIDENT; 1487 ni->ni_flags |= NI_FLAG_RESIDENT; 1488 } 1489 1490 if (S_ISDIR(mode)) { 1491 ni->ni_flags |= NI_FLAG_DIR; 1492 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30); 1493 if (err) 1494 goto out4; 1495 } else if (S_ISLNK(mode)) { 1496 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize); 1497 1498 if (IS_ERR(rp)) { 1499 err = PTR_ERR(rp); 1500 rp = NULL; 1501 goto out4; 1502 } 1503 1504 /* 1505 * Insert ATTR_REPARSE. 1506 */ 1507 attr = Add2Ptr(attr, asize); 1508 attr->type = ATTR_REPARSE; 1509 attr->id = cpu_to_le16(aid++); 1510 1511 /* Resident or non resident? */ 1512 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8); 1513 t16 = PtrOffset(rec, attr); 1514 1515 /* 1516 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes. 1517 * It is good idea to keep extended attributes resident. 1518 */ 1519 if (asize + t16 + 0x78 + 8 > sbi->record_size) { 1520 CLST alen; 1521 CLST clst = bytes_to_cluster(sbi, nsize); 1522 1523 /* Bytes per runs. */ 1524 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT; 1525 1526 attr->non_res = 1; 1527 attr->nres.evcn = cpu_to_le64(clst - 1); 1528 attr->name_off = SIZEOF_NONRESIDENT_LE; 1529 attr->nres.run_off = attr->name_off; 1530 attr->nres.data_size = cpu_to_le64(nsize); 1531 attr->nres.valid_size = attr->nres.data_size; 1532 attr->nres.alloc_size = 1533 cpu_to_le64(ntfs_up_cluster(sbi, nsize)); 1534 1535 err = attr_allocate_clusters(sbi, &ni->file.run, NULL, 1536 0, 0, clst, NULL, 1537 ALLOCATE_DEF, &alen, 0, 1538 NULL, NULL); 1539 if (err) 1540 goto out5; 1541 1542 err = run_pack(&ni->file.run, 0, clst, 1543 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16, 1544 &vcn); 1545 if (err < 0) 1546 goto out5; 1547 1548 if (vcn != clst) { 1549 err = -EINVAL; 1550 goto out5; 1551 } 1552 1553 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8); 1554 /* Write non resident data. */ 1555 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, 1556 nsize, 0); 1557 if (err) 1558 goto out5; 1559 } else { 1560 attr->res.data_off = SIZEOF_RESIDENT_LE; 1561 attr->res.data_size = cpu_to_le32(nsize); 1562 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize); 1563 } 1564 /* Size of symlink equals the length of input string. */ 1565 inode->i_size = size; 1566 1567 attr->size = cpu_to_le32(asize); 1568 1569 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK, 1570 &new_de->ref); 1571 if (err) 1572 goto out5; 1573 1574 rp_inserted = true; 1575 } 1576 1577 attr = Add2Ptr(attr, asize); 1578 attr->type = ATTR_END; 1579 1580 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8); 1581 rec->next_attr_id = cpu_to_le16(aid); 1582 1583 inode->i_generation = le16_to_cpu(rec->seq); 1584 1585 if (S_ISDIR(mode)) { 1586 inode->i_op = &ntfs_dir_inode_operations; 1587 inode->i_fop = &ntfs_dir_operations; 1588 } else if (S_ISLNK(mode)) { 1589 inode->i_op = &ntfs_link_inode_operations; 1590 inode->i_fop = NULL; 1591 inode->i_mapping->a_ops = &ntfs_aops; 1592 inode->i_size = size; 1593 inode_nohighmem(inode); 1594 } else if (S_ISREG(mode)) { 1595 inode->i_op = &ntfs_file_inode_operations; 1596 inode->i_fop = &ntfs_file_operations; 1597 inode->i_mapping->a_ops = is_compressed(ni) ? &ntfs_aops_cmpr : 1598 &ntfs_aops; 1599 init_rwsem(&ni->file.run_lock); 1600 } else { 1601 inode->i_op = &ntfs_special_inode_operations; 1602 init_special_inode(inode, mode, dev); 1603 } 1604 1605 #ifdef CONFIG_NTFS3_FS_POSIX_ACL 1606 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) { 1607 err = ntfs_init_acl(idmap, inode, dir); 1608 if (err) 1609 goto out5; 1610 } else 1611 #endif 1612 { 1613 inode->i_flags |= S_NOSEC; 1614 } 1615 1616 if (!S_ISLNK(mode)) { 1617 /* 1618 * ntfs_init_acl and ntfs_save_wsl_perm update extended attribute. 1619 * The packed size of extended attribute is stored in direntry too. 1620 * 'fname' here points to inside new_de. 1621 */ 1622 err = ntfs_save_wsl_perm(inode, &fname->dup.extend_data); 1623 if (err) 1624 goto out6; 1625 1626 /* 1627 * update ea_size in file_name attribute too. 1628 * Use ni_find_attr cause layout of MFT record may be changed 1629 * in ntfs_init_acl and ntfs_save_wsl_perm. 1630 */ 1631 attr = ni_find_attr(ni, NULL, NULL, ATTR_NAME, NULL, 0, NULL, 1632 NULL); 1633 if (attr) { 1634 struct ATTR_FILE_NAME *fn; 1635 1636 fn = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); 1637 if (fn) 1638 fn->dup.extend_data = fname->dup.extend_data; 1639 } 1640 } 1641 1642 /* We do not need to update parent directory later */ 1643 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT; 1644 1645 /* Step 2: Add new name in index. */ 1646 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0); 1647 if (err) 1648 goto out6; 1649 1650 /* 1651 * Call 'd_instantiate' after inode->i_op is set 1652 * but before finish_open. 1653 */ 1654 d_instantiate(dentry, inode); 1655 1656 /* Set original time. inode times (i_ctime) may be changed in ntfs_init_acl. */ 1657 inode_set_atime_to_ts(inode, ni->i_crtime); 1658 inode_set_ctime_to_ts(inode, ni->i_crtime); 1659 inode_set_mtime_to_ts(inode, ni->i_crtime); 1660 inode_set_mtime_to_ts(dir, ni->i_crtime); 1661 inode_set_ctime_to_ts(dir, ni->i_crtime); 1662 1663 mark_inode_dirty(dir); 1664 mark_inode_dirty(inode); 1665 1666 /* Normal exit. */ 1667 goto out2; 1668 1669 out6: 1670 attr = ni_find_attr(ni, NULL, NULL, ATTR_EA, NULL, 0, NULL, NULL); 1671 if (attr && attr->non_res) { 1672 /* Delete ATTR_EA, if non-resident. */ 1673 struct runs_tree run; 1674 run_init(&run); 1675 attr_set_size(ni, ATTR_EA, NULL, 0, &run, 0, NULL, false); 1676 run_close(&run); 1677 } 1678 1679 if (rp_inserted) 1680 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref); 1681 1682 out5: 1683 if (!S_ISDIR(mode)) 1684 run_deallocate(sbi, &ni->file.run, false); 1685 1686 out4: 1687 clear_rec_inuse(rec); 1688 clear_nlink(inode); 1689 ni->mi.dirty = false; 1690 discard_new_inode(inode); 1691 out3: 1692 ntfs_mark_rec_free(sbi, ino, false); 1693 1694 out2: 1695 kfree(new_de); 1696 kfree(rp); 1697 1698 out1: 1699 if (!fnd) 1700 ni_unlock(dir_ni); 1701 1702 if (!err) 1703 unlock_new_inode(inode); 1704 1705 return err; 1706 } 1707 1708 int ntfs_link_inode(struct inode *inode, struct dentry *dentry) 1709 { 1710 int err; 1711 struct ntfs_inode *ni = ntfs_i(inode); 1712 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 1713 struct NTFS_DE *de; 1714 1715 de = kzalloc(PATH_MAX, GFP_KERNEL); 1716 if (!de) 1717 return -ENOMEM; 1718 1719 /* Mark rw ntfs as dirty. It will be cleared at umount. */ 1720 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 1721 1722 /* Construct 'de'. */ 1723 err = fill_name_de(sbi, de, &dentry->d_name, NULL); 1724 if (err) 1725 goto out; 1726 1727 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de); 1728 out: 1729 kfree(de); 1730 return err; 1731 } 1732 1733 /* 1734 * ntfs_unlink_inode 1735 * 1736 * inode_operations::unlink 1737 * inode_operations::rmdir 1738 */ 1739 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry) 1740 { 1741 int err; 1742 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info; 1743 struct inode *inode = d_inode(dentry); 1744 struct ntfs_inode *ni = ntfs_i(inode); 1745 struct ntfs_inode *dir_ni = ntfs_i(dir); 1746 struct NTFS_DE *de, *de2 = NULL; 1747 int undo_remove; 1748 1749 if (ntfs_is_meta_file(sbi, ni->mi.rno)) 1750 return -EINVAL; 1751 1752 de = kzalloc(PATH_MAX, GFP_KERNEL); 1753 if (!de) 1754 return -ENOMEM; 1755 1756 ni_lock(ni); 1757 1758 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) { 1759 err = -ENOTEMPTY; 1760 goto out; 1761 } 1762 1763 err = fill_name_de(sbi, de, &dentry->d_name, NULL); 1764 if (err < 0) 1765 goto out; 1766 1767 undo_remove = 0; 1768 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove); 1769 1770 if (!err) { 1771 drop_nlink(inode); 1772 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 1773 mark_inode_dirty(dir); 1774 inode_set_ctime_to_ts(inode, inode_get_ctime(dir)); 1775 if (inode->i_nlink) 1776 mark_inode_dirty(inode); 1777 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) { 1778 _ntfs_bad_inode(inode); 1779 } else { 1780 if (ni_is_dirty(dir)) 1781 mark_inode_dirty(dir); 1782 if (ni_is_dirty(inode)) 1783 mark_inode_dirty(inode); 1784 } 1785 1786 out: 1787 ni_unlock(ni); 1788 kfree(de); 1789 return err; 1790 } 1791 1792 void ntfs_evict_inode(struct inode *inode) 1793 { 1794 truncate_inode_pages_final(&inode->i_data); 1795 1796 clear_inode(inode); 1797 1798 ni_clear(ntfs_i(inode)); 1799 } 1800 1801 /* 1802 * ntfs_translate_junction 1803 * 1804 * Translate a Windows junction target to the Linux equivalent. 1805 * On junctions, targets are always absolute (they include the drive 1806 * letter). We have no way of knowing if the target is for the current 1807 * mounted device or not so we just assume it is. 1808 */ 1809 static int ntfs_translate_junction(const struct super_block *sb, 1810 const struct dentry *link_de, char *target, 1811 int target_len, int target_max) 1812 { 1813 int tl_len, err = target_len; 1814 char *link_path_buffer = NULL, *link_path; 1815 char *translated = NULL; 1816 char *target_start; 1817 int copy_len; 1818 1819 link_path_buffer = kmalloc(PATH_MAX, GFP_NOFS); 1820 if (!link_path_buffer) { 1821 err = -ENOMEM; 1822 goto out; 1823 } 1824 /* Get link path, relative to mount point */ 1825 link_path = dentry_path_raw(link_de, link_path_buffer, PATH_MAX); 1826 if (IS_ERR(link_path)) { 1827 ntfs_err(sb, "Error getting link path"); 1828 err = -EINVAL; 1829 goto out; 1830 } 1831 1832 translated = kmalloc(PATH_MAX, GFP_NOFS); 1833 if (!translated) { 1834 err = -ENOMEM; 1835 goto out; 1836 } 1837 1838 /* Make translated path a relative path to mount point */ 1839 strcpy(translated, "./"); 1840 ++link_path; /* Skip leading / */ 1841 for (tl_len = sizeof("./") - 1; *link_path; ++link_path) { 1842 if (*link_path == '/') { 1843 if (PATH_MAX - tl_len < sizeof("../")) { 1844 ntfs_err(sb, 1845 "Link path %s has too many components", 1846 link_path); 1847 err = -EINVAL; 1848 goto out; 1849 } 1850 strcpy(translated + tl_len, "../"); 1851 tl_len += sizeof("../") - 1; 1852 } 1853 } 1854 1855 /* Skip drive letter */ 1856 target_start = target; 1857 while (*target_start && *target_start != ':') 1858 ++target_start; 1859 1860 if (!*target_start) { 1861 ntfs_err(sb, "Link target (%s) missing drive separator", 1862 target); 1863 err = -EINVAL; 1864 goto out; 1865 } 1866 1867 /* Skip drive separator and leading /, if exists */ 1868 target_start += 1 + (target_start[1] == '/'); 1869 copy_len = target_len - (target_start - target); 1870 1871 if (PATH_MAX - tl_len <= copy_len) { 1872 ntfs_err(sb, "Link target %s too large for buffer (%d <= %d)", 1873 target_start, PATH_MAX - tl_len, copy_len); 1874 err = -EINVAL; 1875 goto out; 1876 } 1877 1878 /* translated path has a trailing / and target_start does not */ 1879 strcpy(translated + tl_len, target_start); 1880 tl_len += copy_len; 1881 if (target_max <= tl_len) { 1882 ntfs_err(sb, "Target path %s too large for buffer (%d <= %d)", 1883 translated, target_max, tl_len); 1884 err = -EINVAL; 1885 goto out; 1886 } 1887 strcpy(target, translated); 1888 err = tl_len; 1889 1890 out: 1891 kfree(link_path_buffer); 1892 kfree(translated); 1893 return err; 1894 } 1895 1896 static noinline int ntfs_readlink_hlp(const struct dentry *link_de, 1897 struct inode *inode, char *buffer, 1898 int buflen) 1899 { 1900 int i, err = -EINVAL; 1901 struct ntfs_inode *ni = ntfs_i(inode); 1902 struct super_block *sb = inode->i_sb; 1903 struct ntfs_sb_info *sbi = sb->s_fs_info; 1904 u64 size; 1905 u16 ulen = 0; 1906 void *to_free = NULL; 1907 struct REPARSE_DATA_BUFFER *rp; 1908 const __le16 *uname; 1909 struct ATTRIB *attr; 1910 1911 /* Reparse data present. Try to parse it. */ 1912 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag)); 1913 static_assert(sizeof(u32) == sizeof(rp->ReparseTag)); 1914 1915 *buffer = 0; 1916 1917 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL); 1918 if (!attr) 1919 goto out; 1920 1921 if (!attr->non_res) { 1922 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER)); 1923 if (!rp) 1924 goto out; 1925 size = le32_to_cpu(attr->res.data_size); 1926 } else { 1927 size = le64_to_cpu(attr->nres.data_size); 1928 rp = NULL; 1929 } 1930 1931 if (size > sbi->reparse.max_size || size <= sizeof(u32)) 1932 goto out; 1933 1934 if (!rp) { 1935 rp = kmalloc(size, GFP_NOFS); 1936 if (!rp) { 1937 err = -ENOMEM; 1938 goto out; 1939 } 1940 to_free = rp; 1941 /* Read into temporal buffer. */ 1942 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL); 1943 if (err) 1944 goto out; 1945 } 1946 1947 /* Microsoft Tag. */ 1948 switch (rp->ReparseTag) { 1949 case IO_REPARSE_TAG_MOUNT_POINT: 1950 /* Mount points and junctions. */ 1951 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */ 1952 if (size <= offsetof(struct REPARSE_DATA_BUFFER, 1953 MountPointReparseBuffer.PathBuffer)) 1954 goto out; 1955 uname = Add2Ptr(rp, 1956 offsetof(struct REPARSE_DATA_BUFFER, 1957 MountPointReparseBuffer.PathBuffer) + 1958 le16_to_cpu(rp->MountPointReparseBuffer 1959 .PrintNameOffset)); 1960 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength); 1961 break; 1962 1963 case IO_REPARSE_TAG_SYMLINK: 1964 /* FolderSymbolicLink */ 1965 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */ 1966 if (size <= offsetof(struct REPARSE_DATA_BUFFER, 1967 SymbolicLinkReparseBuffer.PathBuffer)) 1968 goto out; 1969 uname = Add2Ptr( 1970 rp, offsetof(struct REPARSE_DATA_BUFFER, 1971 SymbolicLinkReparseBuffer.PathBuffer) + 1972 le16_to_cpu(rp->SymbolicLinkReparseBuffer 1973 .PrintNameOffset)); 1974 ulen = le16_to_cpu( 1975 rp->SymbolicLinkReparseBuffer.PrintNameLength); 1976 break; 1977 1978 case IO_REPARSE_TAG_CLOUD: 1979 case IO_REPARSE_TAG_CLOUD_1: 1980 case IO_REPARSE_TAG_CLOUD_2: 1981 case IO_REPARSE_TAG_CLOUD_3: 1982 case IO_REPARSE_TAG_CLOUD_4: 1983 case IO_REPARSE_TAG_CLOUD_5: 1984 case IO_REPARSE_TAG_CLOUD_6: 1985 case IO_REPARSE_TAG_CLOUD_7: 1986 case IO_REPARSE_TAG_CLOUD_8: 1987 case IO_REPARSE_TAG_CLOUD_9: 1988 case IO_REPARSE_TAG_CLOUD_A: 1989 case IO_REPARSE_TAG_CLOUD_B: 1990 case IO_REPARSE_TAG_CLOUD_C: 1991 case IO_REPARSE_TAG_CLOUD_D: 1992 case IO_REPARSE_TAG_CLOUD_E: 1993 case IO_REPARSE_TAG_CLOUD_F: 1994 err = sizeof("OneDrive") - 1; 1995 if (err > buflen) 1996 err = buflen; 1997 memcpy(buffer, "OneDrive", err); 1998 goto out; 1999 2000 default: 2001 if (IsReparseTagMicrosoft(rp->ReparseTag)) { 2002 /* Unknown Microsoft Tag. */ 2003 goto out; 2004 } 2005 if (!IsReparseTagNameSurrogate(rp->ReparseTag) || 2006 size <= sizeof(struct REPARSE_POINT)) { 2007 goto out; 2008 } 2009 2010 /* Users tag. */ 2011 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT)); 2012 ulen = le16_to_cpu(rp->ReparseDataLength) - 2013 sizeof(struct REPARSE_POINT); 2014 } 2015 2016 /* Convert nlen from bytes to UNICODE chars. */ 2017 ulen >>= 1; 2018 2019 /* Check that name is available. */ 2020 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size)) 2021 goto out; 2022 2023 /* If name is already zero terminated then truncate it now. */ 2024 if (!uname[ulen - 1]) 2025 ulen -= 1; 2026 2027 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen); 2028 2029 if (err < 0) 2030 goto out; 2031 2032 /* Translate Windows '\' into Linux '/'. */ 2033 for (i = 0; i < err; i++) { 2034 if (buffer[i] == '\\') 2035 buffer[i] = '/'; 2036 } 2037 2038 /* Always set last zero. */ 2039 buffer[err] = 0; 2040 2041 /* If this is a junction, translate the link target. */ 2042 if (rp->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) 2043 err = ntfs_translate_junction(sb, link_de, buffer, err, buflen); 2044 2045 out: 2046 kfree(to_free); 2047 return err; 2048 } 2049 2050 static const char *ntfs_get_link(struct dentry *de, struct inode *inode, 2051 struct delayed_call *done) 2052 { 2053 int err; 2054 char *ret; 2055 2056 if (!de) 2057 return ERR_PTR(-ECHILD); 2058 2059 ret = kmalloc(PAGE_SIZE, GFP_NOFS); 2060 if (!ret) 2061 return ERR_PTR(-ENOMEM); 2062 2063 err = ntfs_readlink_hlp(de, inode, ret, PAGE_SIZE); 2064 if (err < 0) { 2065 kfree(ret); 2066 return ERR_PTR(err); 2067 } 2068 2069 set_delayed_call(done, kfree_link, ret); 2070 2071 return ret; 2072 } 2073 2074 // clang-format off 2075 const struct inode_operations ntfs_link_inode_operations = { 2076 .get_link = ntfs_get_link, 2077 .setattr = ntfs_setattr, 2078 .listxattr = ntfs_listxattr, 2079 .fileattr_get = ntfs_fileattr_get, 2080 .fileattr_set = ntfs_fileattr_set, 2081 }; 2082 2083 const struct address_space_operations ntfs_aops = { 2084 .read_folio = ntfs_read_folio, 2085 .readahead = ntfs_readahead, 2086 .writepages = ntfs_writepages, 2087 .bmap = ntfs_bmap, 2088 .dirty_folio = iomap_dirty_folio, 2089 .migrate_folio = filemap_migrate_folio, 2090 .release_folio = iomap_release_folio, 2091 .invalidate_folio = iomap_invalidate_folio, 2092 }; 2093 2094 const struct address_space_operations ntfs_aops_cmpr = { 2095 .read_folio = ntfs_read_folio, 2096 .dirty_folio = iomap_dirty_folio, 2097 .release_folio = iomap_release_folio, 2098 .invalidate_folio = iomap_invalidate_folio, 2099 }; 2100 2101 const struct iomap_ops ntfs_iomap_ops = { 2102 .iomap_begin = ntfs_iomap_begin, 2103 .iomap_end = ntfs_iomap_end, 2104 }; 2105 2106 const struct iomap_write_ops ntfs_iomap_folio_ops = { 2107 .put_folio = ntfs_iomap_put_folio, 2108 }; 2109 // clang-format on 2110