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