Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'ntfs3_for_6.8' of https://github.com/Paragon-Software-Group/linux-ntfs3

Pull ntfs3 fixes from Konstantin Komarov:
"Fixed:
- size update for compressed file
- some logic errors, overflows
- memory leak
- some code was refactored

Added:
- implement super_operations::shutdown

Improved:
- alternative boot processing
- reduced stack usage"

* tag 'ntfs3_for_6.8' of https://github.com/Paragon-Software-Group/linux-ntfs3: (28 commits)
fs/ntfs3: Slightly simplify ntfs_inode_printk()
fs/ntfs3: Add ioctl operation for directories (FITRIM)
fs/ntfs3: Fix oob in ntfs_listxattr
fs/ntfs3: Fix an NULL dereference bug
fs/ntfs3: Update inode->i_size after success write into compressed file
fs/ntfs3: Fixed overflow check in mi_enum_attr()
fs/ntfs3: Correct function is_rst_area_valid
fs/ntfs3: Use i_size_read and i_size_write
fs/ntfs3: Prevent generic message "attempt to access beyond end of device"
fs/ntfs3: use non-movable memory for ntfs3 MFT buffer cache
fs/ntfs3: Use kvfree to free memory allocated by kvmalloc
fs/ntfs3: Disable ATTR_LIST_ENTRY size check
fs/ntfs3: Fix c/mtime typo
fs/ntfs3: Add NULL ptr dereference checking at the end of attr_allocate_frame()
fs/ntfs3: Add and fix comments
fs/ntfs3: ntfs3_forced_shutdown use int instead of bool
fs/ntfs3: Implement super_operations::shutdown
fs/ntfs3: Drop suid and sgid bits as a part of fpunch
fs/ntfs3: Add file_modified
fs/ntfs3: Correct use bh_read
...

+379 -245
+28 -17
fs/ntfs3/attrib.c
··· 886 886 struct runs_tree *run = &ni->file.run; 887 887 struct ntfs_sb_info *sbi; 888 888 u8 cluster_bits; 889 - struct ATTRIB *attr = NULL, *attr_b; 889 + struct ATTRIB *attr, *attr_b; 890 890 struct ATTR_LIST_ENTRY *le, *le_b; 891 891 struct mft_inode *mi, *mi_b; 892 892 CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen; ··· 904 904 *len = 0; 905 905 up_read(&ni->file.run_lock); 906 906 907 - if (*len) { 908 - if (*lcn != SPARSE_LCN || !new) 909 - return 0; /* Fast normal way without allocation. */ 910 - else if (clen > *len) 911 - clen = *len; 912 - } 907 + if (*len && (*lcn != SPARSE_LCN || !new)) 908 + return 0; /* Fast normal way without allocation. */ 913 909 914 910 /* No cluster in cache or we need to allocate cluster in hole. */ 915 911 sbi = ni->mi.sbi; ··· 913 917 914 918 ni_lock(ni); 915 919 down_write(&ni->file.run_lock); 920 + 921 + /* Repeat the code above (under write lock). */ 922 + if (!run_lookup_entry(run, vcn, lcn, len, NULL)) 923 + *len = 0; 924 + 925 + if (*len) { 926 + if (*lcn != SPARSE_LCN || !new) 927 + goto out; /* normal way without allocation. */ 928 + if (clen > *len) 929 + clen = *len; 930 + } 916 931 917 932 le_b = NULL; 918 933 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b); ··· 1743 1736 le_b = NULL; 1744 1737 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 1745 1738 0, NULL, &mi_b); 1746 - if (!attr_b) 1747 - return -ENOENT; 1739 + if (!attr_b) { 1740 + err = -ENOENT; 1741 + goto out; 1742 + } 1748 1743 1749 1744 attr = attr_b; 1750 1745 le = le_b; ··· 1827 1818 ok: 1828 1819 run_truncate_around(run, vcn); 1829 1820 out: 1830 - if (new_valid > data_size) 1831 - new_valid = data_size; 1821 + if (attr_b) { 1822 + if (new_valid > data_size) 1823 + new_valid = data_size; 1832 1824 1833 - valid_size = le64_to_cpu(attr_b->nres.valid_size); 1834 - if (new_valid != valid_size) { 1835 - attr_b->nres.valid_size = cpu_to_le64(valid_size); 1836 - mi_b->dirty = true; 1825 + valid_size = le64_to_cpu(attr_b->nres.valid_size); 1826 + if (new_valid != valid_size) { 1827 + attr_b->nres.valid_size = cpu_to_le64(valid_size); 1828 + mi_b->dirty = true; 1829 + } 1837 1830 } 1838 1831 1839 1832 return err; ··· 2084 2073 2085 2074 /* Update inode size. */ 2086 2075 ni->i_valid = valid_size; 2087 - ni->vfs_inode.i_size = data_size; 2076 + i_size_write(&ni->vfs_inode, data_size); 2088 2077 inode_set_bytes(&ni->vfs_inode, total_size); 2089 2078 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 2090 2079 mark_inode_dirty(&ni->vfs_inode); ··· 2499 2488 mi_b->dirty = true; 2500 2489 2501 2490 done: 2502 - ni->vfs_inode.i_size += bytes; 2491 + i_size_write(&ni->vfs_inode, ni->vfs_inode.i_size + bytes); 2503 2492 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 2504 2493 mark_inode_dirty(&ni->vfs_inode); 2505 2494
+6 -6
fs/ntfs3/attrlist.c
··· 29 29 void al_destroy(struct ntfs_inode *ni) 30 30 { 31 31 run_close(&ni->attr_list.run); 32 - kfree(ni->attr_list.le); 32 + kvfree(ni->attr_list.le); 33 33 ni->attr_list.le = NULL; 34 34 ni->attr_list.size = 0; 35 35 ni->attr_list.dirty = false; ··· 127 127 { 128 128 size_t off; 129 129 u16 sz; 130 + const unsigned le_min_size = le_size(0); 130 131 131 132 if (!le) { 132 133 le = ni->attr_list.le; 133 134 } else { 134 135 sz = le16_to_cpu(le->size); 135 - if (sz < sizeof(struct ATTR_LIST_ENTRY)) { 136 + if (sz < le_min_size) { 136 137 /* Impossible 'cause we should not return such le. */ 137 138 return NULL; 138 139 } ··· 142 141 143 142 /* Check boundary. */ 144 143 off = PtrOffset(ni->attr_list.le, le); 145 - if (off + sizeof(struct ATTR_LIST_ENTRY) > ni->attr_list.size) { 144 + if (off + le_min_size > ni->attr_list.size) { 146 145 /* The regular end of list. */ 147 146 return NULL; 148 147 } ··· 150 149 sz = le16_to_cpu(le->size); 151 150 152 151 /* Check le for errors. */ 153 - if (sz < sizeof(struct ATTR_LIST_ENTRY) || 154 - off + sz > ni->attr_list.size || 152 + if (sz < le_min_size || off + sz > ni->attr_list.size || 155 153 sz < le->name_off + le->name_len * sizeof(short)) { 156 154 return NULL; 157 155 } ··· 318 318 memcpy(ptr, al->le, off); 319 319 memcpy(Add2Ptr(ptr, off + sz), le, old_size - off); 320 320 le = Add2Ptr(ptr, off); 321 - kfree(al->le); 321 + kvfree(al->le); 322 322 al->le = ptr; 323 323 } else { 324 324 memmove(Add2Ptr(le, sz), le, old_size - off);
+2 -2
fs/ntfs3/bitmap.c
··· 124 124 { 125 125 struct rb_node *node, *next; 126 126 127 - kfree(wnd->free_bits); 127 + kvfree(wnd->free_bits); 128 128 wnd->free_bits = NULL; 129 129 run_close(&wnd->run); 130 130 ··· 1360 1360 memcpy(new_free, wnd->free_bits, wnd->nwnd * sizeof(short)); 1361 1361 memset(new_free + wnd->nwnd, 0, 1362 1362 (new_wnd - wnd->nwnd) * sizeof(short)); 1363 - kfree(wnd->free_bits); 1363 + kvfree(wnd->free_bits); 1364 1364 wnd->free_bits = new_free; 1365 1365 } 1366 1366
+35 -13
fs/ntfs3/dir.c
··· 309 309 return 0; 310 310 } 311 311 312 - /* NTFS: symlinks are "dir + reparse" or "file + reparse" */ 313 - if (fname->dup.fa & FILE_ATTRIBUTE_REPARSE_POINT) 314 - dt_type = DT_LNK; 315 - else 316 - dt_type = (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG; 312 + /* 313 + * NTFS: symlinks are "dir + reparse" or "file + reparse" 314 + * Unfortunately reparse attribute is used for many purposes (several dozens). 315 + * It is not possible here to know is this name symlink or not. 316 + * To get exactly the type of name we should to open inode (read mft). 317 + * getattr for opened file (fstat) correctly returns symlink. 318 + */ 319 + dt_type = (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG; 320 + 321 + /* 322 + * It is not reliable to detect the type of name using duplicated information 323 + * stored in parent directory. 324 + * The only correct way to get the type of name - read MFT record and find ATTR_STD. 325 + * The code below is not good idea. 326 + * It does additional locks/reads just to get the type of name. 327 + * Should we use additional mount option to enable branch below? 328 + */ 329 + if ((fname->dup.fa & FILE_ATTRIBUTE_REPARSE_POINT) && 330 + ino != ni->mi.rno) { 331 + struct inode *inode = ntfs_iget5(sbi->sb, &e->ref, NULL); 332 + if (!IS_ERR_OR_NULL(inode)) { 333 + dt_type = fs_umode_to_dtype(inode->i_mode); 334 + iput(inode); 335 + } 336 + } 317 337 318 338 return !dir_emit(ctx, (s8 *)name, name_len, ino, dt_type); 319 339 } ··· 515 495 struct INDEX_HDR *hdr; 516 496 const struct ATTR_FILE_NAME *fname; 517 497 u32 e_size, off, end; 518 - u64 vbo = 0; 519 498 size_t drs = 0, fles = 0, bit = 0; 520 - loff_t i_size = ni->vfs_inode.i_size; 521 499 struct indx_node *node = NULL; 522 - u8 index_bits = ni->dir.index_bits; 500 + size_t max_indx = i_size_read(&ni->vfs_inode) >> ni->dir.index_bits; 523 501 524 502 if (is_empty) 525 503 *is_empty = true; ··· 536 518 e = Add2Ptr(hdr, off); 537 519 e_size = le16_to_cpu(e->size); 538 520 if (e_size < sizeof(struct NTFS_DE) || 539 - off + e_size > end) 521 + off + e_size > end) { 522 + /* Looks like corruption. */ 540 523 break; 524 + } 541 525 542 526 if (de_is_last(e)) 543 527 break; ··· 563 543 fles += 1; 564 544 } 565 545 566 - if (vbo >= i_size) 546 + if (bit >= max_indx) 567 547 goto out; 568 548 569 549 err = indx_used_bit(&ni->dir, ni, &bit); ··· 573 553 if (bit == MINUS_ONE_T) 574 554 goto out; 575 555 576 - vbo = (u64)bit << index_bits; 577 - if (vbo >= i_size) 556 + if (bit >= max_indx) 578 557 goto out; 579 558 580 559 err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits, ··· 583 564 584 565 hdr = &node->index->ihdr; 585 566 bit += 1; 586 - vbo = (u64)bit << ni->dir.idx2vbn_bits; 587 567 } 588 568 589 569 out: ··· 611 593 .iterate_shared = ntfs_readdir, 612 594 .fsync = generic_file_fsync, 613 595 .open = ntfs_file_open, 596 + .unlocked_ioctl = ntfs_ioctl, 597 + #ifdef CONFIG_COMPAT 598 + .compat_ioctl = ntfs_compat_ioctl, 599 + #endif 614 600 }; 615 601 // clang-format on
+59 -17
fs/ntfs3/file.c
··· 48 48 return 0; 49 49 } 50 50 51 - static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg) 51 + long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg) 52 52 { 53 53 struct inode *inode = file_inode(filp); 54 54 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; ··· 61 61 } 62 62 63 63 #ifdef CONFIG_COMPAT 64 - static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg) 64 + long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg) 65 65 66 66 { 67 67 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); ··· 188 188 u32 bh_next, bh_off, to; 189 189 sector_t iblock; 190 190 struct folio *folio; 191 + bool dirty = false; 191 192 192 193 for (; idx < idx_end; idx += 1, from = 0) { 193 194 page_off = (loff_t)idx << PAGE_SHIFT; ··· 224 223 /* Ok, it's mapped. Make sure it's up-to-date. */ 225 224 if (folio_test_uptodate(folio)) 226 225 set_buffer_uptodate(bh); 227 - 228 - if (!buffer_uptodate(bh)) { 229 - err = bh_read(bh, 0); 230 - if (err < 0) { 231 - folio_unlock(folio); 232 - folio_put(folio); 233 - goto out; 234 - } 226 + else if (bh_read(bh, 0) < 0) { 227 + err = -EIO; 228 + folio_unlock(folio); 229 + folio_put(folio); 230 + goto out; 235 231 } 236 232 237 233 mark_buffer_dirty(bh); 238 - 239 234 } while (bh_off = bh_next, iblock += 1, 240 235 head != (bh = bh->b_this_page)); 241 236 242 237 folio_zero_segment(folio, from, to); 238 + dirty = true; 243 239 244 240 folio_unlock(folio); 245 241 folio_put(folio); 246 242 cond_resched(); 247 243 } 248 244 out: 249 - mark_inode_dirty(inode); 245 + if (dirty) 246 + mark_inode_dirty(inode); 250 247 return err; 251 248 } 252 249 ··· 259 260 u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT); 260 261 bool rw = vma->vm_flags & VM_WRITE; 261 262 int err; 263 + 264 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 265 + return -EIO; 262 266 263 267 if (is_encrypted(ni)) { 264 268 ntfs_inode_warn(inode, "mmap encrypted not supported"); ··· 501 499 ni_lock(ni); 502 500 err = attr_punch_hole(ni, vbo, len, &frame_size); 503 501 ni_unlock(ni); 502 + if (!err) 503 + goto ok; 504 + 504 505 if (err != E_NTFS_NOTALIGNED) 505 506 goto out; 506 507 507 508 /* Process not aligned punch. */ 509 + err = 0; 508 510 mask = frame_size - 1; 509 511 vbo_a = (vbo + mask) & ~mask; 510 512 end_a = end & ~mask; ··· 531 525 ni_lock(ni); 532 526 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL); 533 527 ni_unlock(ni); 528 + if (err) 529 + goto out; 534 530 } 535 531 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 536 532 /* ··· 572 564 ni_lock(ni); 573 565 err = attr_insert_range(ni, vbo, len); 574 566 ni_unlock(ni); 567 + if (err) 568 + goto out; 575 569 } else { 576 570 /* Check new size. */ 577 571 u8 cluster_bits = sbi->cluster_bits; ··· 643 633 &ni->file.run, i_size, &ni->i_valid, 644 634 true, NULL); 645 635 ni_unlock(ni); 636 + if (err) 637 + goto out; 646 638 } else if (new_size > i_size) { 647 - inode->i_size = new_size; 639 + i_size_write(inode, new_size); 648 640 } 649 641 } 642 + 643 + ok: 644 + err = file_modified(file); 645 + if (err) 646 + goto out; 650 647 651 648 out: 652 649 if (map_locked) ··· 680 663 umode_t mode = inode->i_mode; 681 664 int err; 682 665 666 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 667 + return -EIO; 668 + 683 669 err = setattr_prepare(idmap, dentry, attr); 684 670 if (err) 685 671 goto out; ··· 696 676 goto out; 697 677 } 698 678 inode_dio_wait(inode); 699 - oldsize = inode->i_size; 679 + oldsize = i_size_read(inode); 700 680 newsize = attr->ia_size; 701 681 702 682 if (newsize <= oldsize) ··· 708 688 goto out; 709 689 710 690 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 711 - inode->i_size = newsize; 691 + i_size_write(inode, newsize); 712 692 } 713 693 714 694 setattr_copy(idmap, inode, attr); ··· 737 717 struct file *file = iocb->ki_filp; 738 718 struct inode *inode = file->f_mapping->host; 739 719 struct ntfs_inode *ni = ntfs_i(inode); 720 + 721 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 722 + return -EIO; 740 723 741 724 if (is_encrypted(ni)) { 742 725 ntfs_inode_warn(inode, "encrypted i/o not supported"); ··· 774 751 { 775 752 struct inode *inode = in->f_mapping->host; 776 753 struct ntfs_inode *ni = ntfs_i(inode); 754 + 755 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 756 + return -EIO; 777 757 778 758 if (is_encrypted(ni)) { 779 759 ntfs_inode_warn(inode, "encrypted i/o not supported"); ··· 847 821 size_t count = iov_iter_count(from); 848 822 loff_t pos = iocb->ki_pos; 849 823 struct inode *inode = file_inode(file); 850 - loff_t i_size = inode->i_size; 824 + loff_t i_size = i_size_read(inode); 851 825 struct address_space *mapping = inode->i_mapping; 852 826 struct ntfs_inode *ni = ntfs_i(inode); 853 827 u64 valid = ni->i_valid; ··· 1054 1028 iocb->ki_pos += written; 1055 1029 if (iocb->ki_pos > ni->i_valid) 1056 1030 ni->i_valid = iocb->ki_pos; 1031 + if (iocb->ki_pos > i_size) 1032 + i_size_write(inode, iocb->ki_pos); 1057 1033 1058 1034 return written; 1059 1035 } ··· 1069 1041 struct address_space *mapping = file->f_mapping; 1070 1042 struct inode *inode = mapping->host; 1071 1043 ssize_t ret; 1044 + int err; 1072 1045 struct ntfs_inode *ni = ntfs_i(inode); 1046 + 1047 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1048 + return -EIO; 1073 1049 1074 1050 if (is_encrypted(ni)) { 1075 1051 ntfs_inode_warn(inode, "encrypted i/o not supported"); ··· 1099 1067 ret = generic_write_checks(iocb, from); 1100 1068 if (ret <= 0) 1101 1069 goto out; 1070 + 1071 + err = file_modified(iocb->ki_filp); 1072 + if (err) { 1073 + ret = err; 1074 + goto out; 1075 + } 1102 1076 1103 1077 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { 1104 1078 /* Should never be here, see ntfs_file_open(). */ ··· 1134 1096 int ntfs_file_open(struct inode *inode, struct file *file) 1135 1097 { 1136 1098 struct ntfs_inode *ni = ntfs_i(inode); 1099 + 1100 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1101 + return -EIO; 1137 1102 1138 1103 if (unlikely((is_compressed(ni) || is_encrypted(ni)) && 1139 1104 (file->f_flags & O_DIRECT))) { ··· 1179 1138 down_write(&ni->file.run_lock); 1180 1139 1181 1140 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, 1182 - inode->i_size, &ni->i_valid, false, NULL); 1141 + i_size_read(inode), &ni->i_valid, false, 1142 + NULL); 1183 1143 1184 1144 up_write(&ni->file.run_lock); 1185 1145 ni_unlock(ni);
+11 -8
fs/ntfs3/frecord.c
··· 778 778 run_deallocate(sbi, &ni->attr_list.run, true); 779 779 run_close(&ni->attr_list.run); 780 780 ni->attr_list.size = 0; 781 - kfree(ni->attr_list.le); 781 + kvfree(ni->attr_list.le); 782 782 ni->attr_list.le = NULL; 783 783 ni->attr_list.dirty = false; 784 784 ··· 927 927 return 0; 928 928 929 929 out: 930 - kfree(ni->attr_list.le); 930 + kvfree(ni->attr_list.le); 931 931 ni->attr_list.le = NULL; 932 932 ni->attr_list.size = 0; 933 933 return err; ··· 2099 2099 gfp_t gfp_mask; 2100 2100 struct page *pg; 2101 2101 2102 - if (vbo >= ni->vfs_inode.i_size) { 2102 + if (vbo >= i_size_read(&ni->vfs_inode)) { 2103 2103 SetPageUptodate(page); 2104 2104 err = 0; 2105 2105 goto out; ··· 2173 2173 { 2174 2174 struct ntfs_sb_info *sbi = ni->mi.sbi; 2175 2175 struct inode *inode = &ni->vfs_inode; 2176 - loff_t i_size = inode->i_size; 2176 + loff_t i_size = i_size_read(inode); 2177 2177 struct address_space *mapping = inode->i_mapping; 2178 2178 gfp_t gfp_mask = mapping_gfp_mask(mapping); 2179 2179 struct page **pages = NULL; ··· 2457 2457 struct ATTR_LIST_ENTRY *le = NULL; 2458 2458 struct runs_tree *run = &ni->file.run; 2459 2459 u64 valid_size = ni->i_valid; 2460 + loff_t i_size = i_size_read(&ni->vfs_inode); 2460 2461 u64 vbo_disk; 2461 2462 size_t unc_size; 2462 2463 u32 frame_size, i, npages_disk, ondisk_size; ··· 2549 2548 } 2550 2549 } 2551 2550 2552 - frames = (ni->vfs_inode.i_size - 1) >> frame_bits; 2551 + frames = (i_size - 1) >> frame_bits; 2553 2552 2554 2553 err = attr_wof_frame_info(ni, attr, run, frame64, frames, 2555 2554 frame_bits, &ondisk_size, &vbo_data); ··· 2557 2556 goto out2; 2558 2557 2559 2558 if (frame64 == frames) { 2560 - unc_size = 1 + ((ni->vfs_inode.i_size - 1) & 2561 - (frame_size - 1)); 2559 + unc_size = 1 + ((i_size - 1) & (frame_size - 1)); 2562 2560 ondisk_size = attr_size(attr) - vbo_data; 2563 2561 } else { 2564 2562 unc_size = frame_size; ··· 3259 3259 if (is_bad_inode(inode) || sb_rdonly(sb)) 3260 3260 return 0; 3261 3261 3262 + if (unlikely(ntfs3_forced_shutdown(sb))) 3263 + return -EIO; 3264 + 3262 3265 if (!ni_trylock(ni)) { 3263 3266 /* 'ni' is under modification, skip for now. */ 3264 3267 mark_inode_dirty_sync(inode); ··· 3291 3288 modified = true; 3292 3289 } 3293 3290 3294 - ts = inode_get_mtime(inode); 3291 + ts = inode_get_ctime(inode); 3295 3292 dup.c_time = kernel2nt(&ts); 3296 3293 if (std->c_time != dup.c_time) { 3297 3294 std->c_time = dup.c_time;
+104 -124
fs/ntfs3/fslog.c
··· 465 465 { 466 466 const struct RESTART_AREA *ra; 467 467 u16 cl, fl, ul; 468 - u32 off, l_size, file_dat_bits, file_size_round; 468 + u32 off, l_size, seq_bits; 469 469 u16 ro = le16_to_cpu(rhdr->ra_off); 470 470 u32 sys_page = le32_to_cpu(rhdr->sys_page_size); 471 471 ··· 511 511 /* Make sure the sequence number bits match the log file size. */ 512 512 l_size = le64_to_cpu(ra->l_size); 513 513 514 - file_dat_bits = sizeof(u64) * 8 - le32_to_cpu(ra->seq_num_bits); 515 - file_size_round = 1u << (file_dat_bits + 3); 516 - if (file_size_round != l_size && 517 - (file_size_round < l_size || (file_size_round / 2) > l_size)) { 518 - return false; 514 + seq_bits = sizeof(u64) * 8 + 3; 515 + while (l_size) { 516 + l_size >>= 1; 517 + seq_bits -= 1; 519 518 } 519 + 520 + if (seq_bits != ra->seq_num_bits) 521 + return false; 520 522 521 523 /* The log page data offset and record header length must be quad-aligned. */ 522 524 if (!IS_ALIGNED(le16_to_cpu(ra->data_off), 8) || ··· 976 974 return e; 977 975 } 978 976 977 + struct restart_info { 978 + u64 last_lsn; 979 + struct RESTART_HDR *r_page; 980 + u32 vbo; 981 + bool chkdsk_was_run; 982 + bool valid_page; 983 + bool initialized; 984 + bool restart; 985 + }; 986 + 979 987 #define RESTART_SINGLE_PAGE_IO cpu_to_le16(0x0001) 980 988 981 989 #define NTFSLOG_WRAPPED 0x00000001 ··· 999 987 struct ntfs_inode *ni; 1000 988 1001 989 u32 l_size; 990 + u32 orig_file_size; 1002 991 u32 sys_page_size; 1003 992 u32 sys_page_mask; 1004 993 u32 page_size; ··· 1053 1040 1054 1041 struct CLIENT_ID client_id; 1055 1042 u32 client_undo_commit; 1043 + 1044 + struct restart_info rst_info, rst_info2; 1056 1045 }; 1057 1046 1058 1047 static inline u32 lsn_to_vbo(struct ntfs_log *log, const u64 lsn) ··· 1120 1105 lsn <= le64_to_cpu(log->ra->current_lsn) && lsn; 1121 1106 } 1122 1107 1123 - struct restart_info { 1124 - u64 last_lsn; 1125 - struct RESTART_HDR *r_page; 1126 - u32 vbo; 1127 - bool chkdsk_was_run; 1128 - bool valid_page; 1129 - bool initialized; 1130 - bool restart; 1131 - }; 1132 - 1133 1108 static int read_log_page(struct ntfs_log *log, u32 vbo, 1134 1109 struct RECORD_PAGE_HDR **buffer, bool *usa_error) 1135 1110 { ··· 1181 1176 * restart page header. It will stop the first time we find a 1182 1177 * valid page header. 1183 1178 */ 1184 - static int log_read_rst(struct ntfs_log *log, u32 l_size, bool first, 1179 + static int log_read_rst(struct ntfs_log *log, bool first, 1185 1180 struct restart_info *info) 1186 1181 { 1187 1182 u32 skip, vbo; ··· 1197 1192 } 1198 1193 1199 1194 /* Loop continuously until we succeed. */ 1200 - for (; vbo < l_size; vbo = 2 * vbo + skip, skip = 0) { 1195 + for (; vbo < log->l_size; vbo = 2 * vbo + skip, skip = 0) { 1201 1196 bool usa_error; 1202 1197 bool brst, bchk; 1203 1198 struct RESTART_AREA *ra; ··· 1290 1285 /* 1291 1286 * Ilog_init_pg_hdr - Init @log from restart page header. 1292 1287 */ 1293 - static void log_init_pg_hdr(struct ntfs_log *log, u32 sys_page_size, 1294 - u32 page_size, u16 major_ver, u16 minor_ver) 1288 + static void log_init_pg_hdr(struct ntfs_log *log, u16 major_ver, u16 minor_ver) 1295 1289 { 1296 - log->sys_page_size = sys_page_size; 1297 - log->sys_page_mask = sys_page_size - 1; 1298 - log->page_size = page_size; 1299 - log->page_mask = page_size - 1; 1300 - log->page_bits = blksize_bits(page_size); 1290 + log->sys_page_size = log->page_size; 1291 + log->sys_page_mask = log->page_mask; 1301 1292 1302 1293 log->clst_per_page = log->page_size >> log->ni->mi.sbi->cluster_bits; 1303 1294 if (!log->clst_per_page) 1304 1295 log->clst_per_page = 1; 1305 1296 1306 - log->first_page = major_ver >= 2 ? 1307 - 0x22 * page_size : 1308 - ((sys_page_size << 1) + (page_size << 1)); 1297 + log->first_page = major_ver >= 2 ? 0x22 * log->page_size : 1298 + 4 * log->page_size; 1309 1299 log->major_ver = major_ver; 1310 1300 log->minor_ver = minor_ver; 1311 1301 } ··· 1308 1308 /* 1309 1309 * log_create - Init @log in cases when we don't have a restart area to use. 1310 1310 */ 1311 - static void log_create(struct ntfs_log *log, u32 l_size, const u64 last_lsn, 1311 + static void log_create(struct ntfs_log *log, const u64 last_lsn, 1312 1312 u32 open_log_count, bool wrapped, bool use_multi_page) 1313 1313 { 1314 - log->l_size = l_size; 1315 1314 /* All file offsets must be quadword aligned. */ 1316 - log->file_data_bits = blksize_bits(l_size) - 3; 1315 + log->file_data_bits = blksize_bits(log->l_size) - 3; 1317 1316 log->seq_num_mask = (8 << log->file_data_bits) - 1; 1318 1317 log->seq_num_bits = sizeof(u64) * 8 - log->file_data_bits; 1319 1318 log->seq_num = (last_lsn >> log->file_data_bits) + 2; ··· 3719 3720 struct ntfs_sb_info *sbi = ni->mi.sbi; 3720 3721 struct ntfs_log *log; 3721 3722 3722 - struct restart_info rst_info, rst_info2; 3723 - u64 rec_lsn, ra_lsn, checkpt_lsn = 0, rlsn = 0; 3723 + u64 rec_lsn, checkpt_lsn = 0, rlsn = 0; 3724 3724 struct ATTR_NAME_ENTRY *attr_names = NULL; 3725 - struct ATTR_NAME_ENTRY *ane; 3726 3725 struct RESTART_TABLE *dptbl = NULL; 3727 3726 struct RESTART_TABLE *trtbl = NULL; 3728 3727 const struct RESTART_TABLE *rt; ··· 3738 3741 struct TRANSACTION_ENTRY *tr; 3739 3742 struct DIR_PAGE_ENTRY *dp; 3740 3743 u32 i, bytes_per_attr_entry; 3741 - u32 l_size = ni->vfs_inode.i_size; 3742 - u32 orig_file_size = l_size; 3743 - u32 page_size, vbo, tail, off, dlen; 3744 + u32 vbo, tail, off, dlen; 3744 3745 u32 saved_len, rec_len, transact_id; 3745 3746 bool use_second_page; 3746 3747 struct RESTART_AREA *ra2, *ra = NULL; ··· 3753 3758 u16 t16; 3754 3759 u32 t32; 3755 3760 3756 - /* Get the size of page. NOTE: To replay we can use default page. */ 3757 - #if PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <= DefaultLogPageSize * 2 3758 - page_size = norm_file_page(PAGE_SIZE, &l_size, true); 3759 - #else 3760 - page_size = norm_file_page(PAGE_SIZE, &l_size, false); 3761 - #endif 3762 - if (!page_size) 3763 - return -EINVAL; 3764 - 3765 3761 log = kzalloc(sizeof(struct ntfs_log), GFP_NOFS); 3766 3762 if (!log) 3767 3763 return -ENOMEM; 3768 3764 3769 3765 log->ni = ni; 3770 - log->l_size = l_size; 3771 - log->one_page_buf = kmalloc(page_size, GFP_NOFS); 3766 + log->l_size = log->orig_file_size = ni->vfs_inode.i_size; 3772 3767 3768 + /* Get the size of page. NOTE: To replay we can use default page. */ 3769 + #if PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <= DefaultLogPageSize * 2 3770 + log->page_size = norm_file_page(PAGE_SIZE, &log->l_size, true); 3771 + #else 3772 + log->page_size = norm_file_page(PAGE_SIZE, &log->l_size, false); 3773 + #endif 3774 + if (!log->page_size) { 3775 + err = -EINVAL; 3776 + goto out; 3777 + } 3778 + 3779 + log->one_page_buf = kmalloc(log->page_size, GFP_NOFS); 3773 3780 if (!log->one_page_buf) { 3774 3781 err = -ENOMEM; 3775 3782 goto out; 3776 3783 } 3777 3784 3778 - log->page_size = page_size; 3779 - log->page_mask = page_size - 1; 3780 - log->page_bits = blksize_bits(page_size); 3785 + log->page_mask = log->page_size - 1; 3786 + log->page_bits = blksize_bits(log->page_size); 3781 3787 3782 3788 /* Look for a restart area on the disk. */ 3783 - memset(&rst_info, 0, sizeof(struct restart_info)); 3784 - err = log_read_rst(log, l_size, true, &rst_info); 3789 + err = log_read_rst(log, true, &log->rst_info); 3785 3790 if (err) 3786 3791 goto out; 3787 3792 3788 3793 /* remember 'initialized' */ 3789 - *initialized = rst_info.initialized; 3794 + *initialized = log->rst_info.initialized; 3790 3795 3791 - if (!rst_info.restart) { 3792 - if (rst_info.initialized) { 3796 + if (!log->rst_info.restart) { 3797 + if (log->rst_info.initialized) { 3793 3798 /* No restart area but the file is not initialized. */ 3794 3799 err = -EINVAL; 3795 3800 goto out; 3796 3801 } 3797 3802 3798 - log_init_pg_hdr(log, page_size, page_size, 1, 1); 3799 - log_create(log, l_size, 0, get_random_u32(), false, false); 3800 - 3801 - log->ra = ra; 3803 + log_init_pg_hdr(log, 1, 1); 3804 + log_create(log, 0, get_random_u32(), false, false); 3802 3805 3803 3806 ra = log_create_ra(log); 3804 3807 if (!ra) { ··· 3813 3820 * If the restart offset above wasn't zero then we won't 3814 3821 * look for a second restart. 3815 3822 */ 3816 - if (rst_info.vbo) 3823 + if (log->rst_info.vbo) 3817 3824 goto check_restart_area; 3818 3825 3819 - memset(&rst_info2, 0, sizeof(struct restart_info)); 3820 - err = log_read_rst(log, l_size, false, &rst_info2); 3826 + err = log_read_rst(log, false, &log->rst_info2); 3821 3827 if (err) 3822 3828 goto out; 3823 3829 3824 3830 /* Determine which restart area to use. */ 3825 - if (!rst_info2.restart || rst_info2.last_lsn <= rst_info.last_lsn) 3831 + if (!log->rst_info2.restart || 3832 + log->rst_info2.last_lsn <= log->rst_info.last_lsn) 3826 3833 goto use_first_page; 3827 3834 3828 3835 use_second_page = true; 3829 3836 3830 - if (rst_info.chkdsk_was_run && page_size != rst_info.vbo) { 3837 + if (log->rst_info.chkdsk_was_run && 3838 + log->page_size != log->rst_info.vbo) { 3831 3839 struct RECORD_PAGE_HDR *sp = NULL; 3832 3840 bool usa_error; 3833 3841 3834 - if (!read_log_page(log, page_size, &sp, &usa_error) && 3842 + if (!read_log_page(log, log->page_size, &sp, &usa_error) && 3835 3843 sp->rhdr.sign == NTFS_CHKD_SIGNATURE) { 3836 3844 use_second_page = false; 3837 3845 } ··· 3840 3846 } 3841 3847 3842 3848 if (use_second_page) { 3843 - kfree(rst_info.r_page); 3844 - memcpy(&rst_info, &rst_info2, sizeof(struct restart_info)); 3845 - rst_info2.r_page = NULL; 3849 + kfree(log->rst_info.r_page); 3850 + memcpy(&log->rst_info, &log->rst_info2, 3851 + sizeof(struct restart_info)); 3852 + log->rst_info2.r_page = NULL; 3846 3853 } 3847 3854 3848 3855 use_first_page: 3849 - kfree(rst_info2.r_page); 3856 + kfree(log->rst_info2.r_page); 3850 3857 3851 3858 check_restart_area: 3852 3859 /* 3853 3860 * If the restart area is at offset 0, we want 3854 3861 * to write the second restart area first. 3855 3862 */ 3856 - log->init_ra = !!rst_info.vbo; 3863 + log->init_ra = !!log->rst_info.vbo; 3857 3864 3858 3865 /* If we have a valid page then grab a pointer to the restart area. */ 3859 - ra2 = rst_info.valid_page ? 3860 - Add2Ptr(rst_info.r_page, 3861 - le16_to_cpu(rst_info.r_page->ra_off)) : 3866 + ra2 = log->rst_info.valid_page ? 3867 + Add2Ptr(log->rst_info.r_page, 3868 + le16_to_cpu(log->rst_info.r_page->ra_off)) : 3862 3869 NULL; 3863 3870 3864 - if (rst_info.chkdsk_was_run || 3871 + if (log->rst_info.chkdsk_was_run || 3865 3872 (ra2 && ra2->client_idx[1] == LFS_NO_CLIENT_LE)) { 3866 3873 bool wrapped = false; 3867 3874 bool use_multi_page = false; 3868 3875 u32 open_log_count; 3869 3876 3870 3877 /* Do some checks based on whether we have a valid log page. */ 3871 - if (!rst_info.valid_page) { 3872 - open_log_count = get_random_u32(); 3873 - goto init_log_instance; 3874 - } 3875 - open_log_count = le32_to_cpu(ra2->open_log_count); 3878 + open_log_count = log->rst_info.valid_page ? 3879 + le32_to_cpu(ra2->open_log_count) : 3880 + get_random_u32(); 3876 3881 3877 - /* 3878 - * If the restart page size isn't changing then we want to 3879 - * check how much work we need to do. 3880 - */ 3881 - if (page_size != le32_to_cpu(rst_info.r_page->sys_page_size)) 3882 - goto init_log_instance; 3882 + log_init_pg_hdr(log, 1, 1); 3883 3883 3884 - init_log_instance: 3885 - log_init_pg_hdr(log, page_size, page_size, 1, 1); 3886 - 3887 - log_create(log, l_size, rst_info.last_lsn, open_log_count, 3888 - wrapped, use_multi_page); 3884 + log_create(log, log->rst_info.last_lsn, open_log_count, wrapped, 3885 + use_multi_page); 3889 3886 3890 3887 ra = log_create_ra(log); 3891 3888 if (!ra) { ··· 3901 3916 * use the log file. We must use the system page size instead of the 3902 3917 * default size if there is not a clean shutdown. 3903 3918 */ 3904 - t32 = le32_to_cpu(rst_info.r_page->sys_page_size); 3905 - if (page_size != t32) { 3906 - l_size = orig_file_size; 3907 - page_size = 3908 - norm_file_page(t32, &l_size, t32 == DefaultLogPageSize); 3919 + t32 = le32_to_cpu(log->rst_info.r_page->sys_page_size); 3920 + if (log->page_size != t32) { 3921 + log->l_size = log->orig_file_size; 3922 + log->page_size = norm_file_page(t32, &log->l_size, 3923 + t32 == DefaultLogPageSize); 3909 3924 } 3910 3925 3911 - if (page_size != t32 || 3912 - page_size != le32_to_cpu(rst_info.r_page->page_size)) { 3926 + if (log->page_size != t32 || 3927 + log->page_size != le32_to_cpu(log->rst_info.r_page->page_size)) { 3913 3928 err = -EINVAL; 3914 3929 goto out; 3915 3930 } 3916 3931 3917 3932 /* If the file size has shrunk then we won't mount it. */ 3918 - if (l_size < le64_to_cpu(ra2->l_size)) { 3933 + if (log->l_size < le64_to_cpu(ra2->l_size)) { 3919 3934 err = -EINVAL; 3920 3935 goto out; 3921 3936 } 3922 3937 3923 - log_init_pg_hdr(log, page_size, page_size, 3924 - le16_to_cpu(rst_info.r_page->major_ver), 3925 - le16_to_cpu(rst_info.r_page->minor_ver)); 3938 + log_init_pg_hdr(log, le16_to_cpu(log->rst_info.r_page->major_ver), 3939 + le16_to_cpu(log->rst_info.r_page->minor_ver)); 3926 3940 3927 3941 log->l_size = le64_to_cpu(ra2->l_size); 3928 3942 log->seq_num_bits = le32_to_cpu(ra2->seq_num_bits); ··· 3929 3945 log->seq_num_mask = (8 << log->file_data_bits) - 1; 3930 3946 log->last_lsn = le64_to_cpu(ra2->current_lsn); 3931 3947 log->seq_num = log->last_lsn >> log->file_data_bits; 3932 - log->ra_off = le16_to_cpu(rst_info.r_page->ra_off); 3948 + log->ra_off = le16_to_cpu(log->rst_info.r_page->ra_off); 3933 3949 log->restart_size = log->sys_page_size - log->ra_off; 3934 3950 log->record_header_len = le16_to_cpu(ra2->rec_hdr_len); 3935 3951 log->ra_size = le16_to_cpu(ra2->ra_len); ··· 4029 4045 log->current_avail = current_log_avail(log); 4030 4046 4031 4047 /* Remember which restart area to write first. */ 4032 - log->init_ra = rst_info.vbo; 4048 + log->init_ra = log->rst_info.vbo; 4033 4049 4034 4050 process_log: 4035 4051 /* 1.0, 1.1, 2.0 log->major_ver/minor_ver - short values. */ ··· 4089 4105 log->client_id.seq_num = cr->seq_num; 4090 4106 log->client_id.client_idx = client; 4091 4107 4092 - err = read_rst_area(log, &rst, &ra_lsn); 4108 + err = read_rst_area(log, &rst, &checkpt_lsn); 4093 4109 if (err) 4094 4110 goto out; 4095 4111 ··· 4098 4114 4099 4115 bytes_per_attr_entry = !rst->major_ver ? 0x2C : 0x28; 4100 4116 4101 - checkpt_lsn = le64_to_cpu(rst->check_point_start); 4102 - if (!checkpt_lsn) 4103 - checkpt_lsn = ra_lsn; 4117 + if (rst->check_point_start) 4118 + checkpt_lsn = le64_to_cpu(rst->check_point_start); 4104 4119 4105 4120 /* Allocate and Read the Transaction Table. */ 4106 4121 if (!rst->transact_table_len) ··· 4313 4330 lcb = NULL; 4314 4331 4315 4332 check_attribute_names2: 4316 - if (!rst->attr_names_len) 4317 - goto trace_attribute_table; 4318 - 4319 - ane = attr_names; 4320 - if (!oatbl) 4321 - goto trace_attribute_table; 4322 - while (ane->off) { 4323 - /* TODO: Clear table on exit! */ 4324 - oe = Add2Ptr(oatbl, le16_to_cpu(ane->off)); 4325 - t16 = le16_to_cpu(ane->name_bytes); 4326 - oe->name_len = t16 / sizeof(short); 4327 - oe->ptr = ane->name; 4328 - oe->is_attr_name = 2; 4329 - ane = Add2Ptr(ane, sizeof(struct ATTR_NAME_ENTRY) + t16); 4333 + if (rst->attr_names_len && oatbl) { 4334 + struct ATTR_NAME_ENTRY *ane = attr_names; 4335 + while (ane->off) { 4336 + /* TODO: Clear table on exit! */ 4337 + oe = Add2Ptr(oatbl, le16_to_cpu(ane->off)); 4338 + t16 = le16_to_cpu(ane->name_bytes); 4339 + oe->name_len = t16 / sizeof(short); 4340 + oe->ptr = ane->name; 4341 + oe->is_attr_name = 2; 4342 + ane = Add2Ptr(ane, 4343 + sizeof(struct ATTR_NAME_ENTRY) + t16); 4344 + } 4330 4345 } 4331 4346 4332 - trace_attribute_table: 4333 4347 /* 4334 4348 * If the checkpt_lsn is zero, then this is a freshly 4335 4349 * formatted disk and we have no work to do. ··· 5169 5189 kfree(oatbl); 5170 5190 kfree(dptbl); 5171 5191 kfree(attr_names); 5172 - kfree(rst_info.r_page); 5192 + kfree(log->rst_info.r_page); 5173 5193 5174 5194 kfree(ra); 5175 5195 kfree(log->one_page_buf);
+27 -2
fs/ntfs3/fsntfs.c
··· 853 853 /* 854 854 * sb can be NULL here. In this case sbi->flags should be 0 too. 855 855 */ 856 - if (!sb || !(sbi->flags & NTFS_FLAGS_MFTMIRR)) 856 + if (!sb || !(sbi->flags & NTFS_FLAGS_MFTMIRR) || 857 + unlikely(ntfs3_forced_shutdown(sb))) 857 858 return; 858 859 859 860 blocksize = sb->s_blocksize; ··· 1005 1004 while (bytes--) 1006 1005 hash = ((hash >> 0x1D) | (hash << 3)) + le32_to_cpu(*ptr++); 1007 1006 return cpu_to_le32(hash); 1007 + } 1008 + 1009 + /* 1010 + * simple wrapper for sb_bread_unmovable. 1011 + */ 1012 + struct buffer_head *ntfs_bread(struct super_block *sb, sector_t block) 1013 + { 1014 + struct ntfs_sb_info *sbi = sb->s_fs_info; 1015 + struct buffer_head *bh; 1016 + 1017 + if (unlikely(block >= sbi->volume.blocks)) { 1018 + /* prevent generic message "attempt to access beyond end of device" */ 1019 + ntfs_err(sb, "try to read out of volume at offset 0x%llx", 1020 + (u64)block << sb->s_blocksize_bits); 1021 + return NULL; 1022 + } 1023 + 1024 + bh = sb_bread_unmovable(sb, block); 1025 + if (bh) 1026 + return bh; 1027 + 1028 + ntfs_err(sb, "failed to read volume at offset 0x%llx", 1029 + (u64)block << sb->s_blocksize_bits); 1030 + return NULL; 1008 1031 } 1009 1032 1010 1033 int ntfs_sb_read(struct super_block *sb, u64 lbo, size_t bytes, void *buffer) ··· 2153 2128 if (le32_to_cpu(d_security->size) == new_sec_size && 2154 2129 d_security->key.hash == hash_key.hash && 2155 2130 !memcmp(d_security + 1, sd, size_sd)) { 2156 - *security_id = d_security->key.sec_id; 2157 2131 /* Such security already exists. */ 2132 + *security_id = d_security->key.sec_id; 2158 2133 err = 0; 2159 2134 goto out; 2160 2135 }
+4 -4
fs/ntfs3/index.c
··· 1462 1462 goto out2; 1463 1463 1464 1464 if (in->name == I30_NAME) { 1465 - ni->vfs_inode.i_size = data_size; 1465 + i_size_write(&ni->vfs_inode, data_size); 1466 1466 inode_set_bytes(&ni->vfs_inode, alloc_size); 1467 1467 } 1468 1468 ··· 1544 1544 } 1545 1545 1546 1546 if (in->name == I30_NAME) 1547 - ni->vfs_inode.i_size = data_size; 1547 + i_size_write(&ni->vfs_inode, data_size); 1548 1548 1549 1549 *vbn = bit << indx->idx2vbn_bits; 1550 1550 ··· 2090 2090 return err; 2091 2091 2092 2092 if (in->name == I30_NAME) 2093 - ni->vfs_inode.i_size = new_data; 2093 + i_size_write(&ni->vfs_inode, new_data); 2094 2094 2095 2095 bpb = bitmap_size(bit); 2096 2096 if (bpb * 8 == nbits) ··· 2576 2576 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len, 2577 2577 &indx->alloc_run, 0, NULL, false, NULL); 2578 2578 if (in->name == I30_NAME) 2579 - ni->vfs_inode.i_size = 0; 2579 + i_size_write(&ni->vfs_inode, 0); 2580 2580 2581 2581 err = ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len, 2582 2582 false, NULL);
+24 -8
fs/ntfs3/inode.c
··· 345 345 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer 346 346 .PrintNameLength) / 347 347 sizeof(u16); 348 - 349 348 ni->i_valid = inode->i_size; 350 - 351 349 /* Clear directory bit. */ 352 350 if (ni->ni_flags & NI_FLAG_DIR) { 353 351 indx_clear(&ni->dir); ··· 410 412 goto out; 411 413 412 414 if (!is_match && name) { 413 - /* Reuse rec as buffer for ascii name. */ 414 415 err = -ENOENT; 415 416 goto out; 416 417 } ··· 424 427 425 428 if (names != le16_to_cpu(rec->hard_links)) { 426 429 /* Correct minor error on the fly. Do not mark inode as dirty. */ 430 + ntfs_inode_warn(inode, "Correct links count -> %u.", names); 427 431 rec->hard_links = cpu_to_le16(names); 428 432 ni->mi.dirty = true; 429 433 } ··· 651 653 off = vbo & (PAGE_SIZE - 1); 652 654 folio_set_bh(bh, folio, off); 653 655 654 - err = bh_read(bh, 0); 655 - if (err < 0) 656 + if (bh_read(bh, 0) < 0) { 657 + err = -EIO; 656 658 goto out; 659 + } 657 660 folio_zero_segment(folio, off + voff, off + block_size); 658 661 } 659 662 } ··· 852 853 struct writeback_control *wbc, void *data) 853 854 { 854 855 struct address_space *mapping = data; 855 - struct ntfs_inode *ni = ntfs_i(mapping->host); 856 + struct inode *inode = mapping->host; 857 + struct ntfs_inode *ni = ntfs_i(inode); 856 858 int ret; 859 + 860 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 861 + return -EIO; 857 862 858 863 ni_lock(ni); 859 864 ret = attr_data_write_resident(ni, &folio->page); ··· 872 869 static int ntfs_writepages(struct address_space *mapping, 873 870 struct writeback_control *wbc) 874 871 { 875 - if (is_resident(ntfs_i(mapping->host))) 872 + struct inode *inode = mapping->host; 873 + 874 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 875 + return -EIO; 876 + 877 + if (is_resident(ntfs_i(inode))) 876 878 return write_cache_pages(mapping, wbc, ntfs_resident_writepage, 877 879 mapping); 878 880 return mpage_writepages(mapping, wbc, ntfs_get_block); ··· 896 888 int err; 897 889 struct inode *inode = mapping->host; 898 890 struct ntfs_inode *ni = ntfs_i(inode); 891 + 892 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 893 + return -EIO; 899 894 900 895 *pagep = NULL; 901 896 if (is_resident(ni)) { ··· 985 974 } 986 975 987 976 if (pos + err > inode->i_size) { 988 - inode->i_size = pos + err; 977 + i_size_write(inode, pos + err); 989 978 dirty = true; 990 979 } 991 980 ··· 1315 1304 if (!new_de) { 1316 1305 err = -ENOMEM; 1317 1306 goto out1; 1307 + } 1308 + 1309 + if (unlikely(ntfs3_forced_shutdown(sb))) { 1310 + err = -EIO; 1311 + goto out2; 1318 1312 } 1319 1313 1320 1314 /* Mark rw ntfs as dirty. it will be cleared at umount. */
+12
fs/ntfs3/namei.c
··· 181 181 struct ntfs_inode *ni = ntfs_i(dir); 182 182 int err; 183 183 184 + if (unlikely(ntfs3_forced_shutdown(dir->i_sb))) 185 + return -EIO; 186 + 184 187 ni_lock_dir(ni); 185 188 186 189 err = ntfs_unlink_inode(dir, dentry); ··· 201 198 { 202 199 u32 size = strlen(symname); 203 200 struct inode *inode; 201 + 202 + if (unlikely(ntfs3_forced_shutdown(dir->i_sb))) 203 + return -EIO; 204 204 205 205 inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0, 206 206 symname, size, NULL); ··· 232 226 { 233 227 struct ntfs_inode *ni = ntfs_i(dir); 234 228 int err; 229 + 230 + if (unlikely(ntfs3_forced_shutdown(dir->i_sb))) 231 + return -EIO; 235 232 236 233 ni_lock_dir(ni); 237 234 ··· 272 263 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) < 273 264 1024); 274 265 static_assert(PATH_MAX >= 4 * 1024); 266 + 267 + if (unlikely(ntfs3_forced_shutdown(sb))) 268 + return -EIO; 275 269 276 270 if (flags & ~RENAME_NOREPLACE) 277 271 return -EINVAL;
+1 -3
fs/ntfs3/ntfs.h
··· 523 523 __le64 vcn; // 0x08: Starting VCN of this attribute. 524 524 struct MFT_REF ref; // 0x10: MFT record number with attribute. 525 525 __le16 id; // 0x18: struct ATTRIB ID. 526 - __le16 name[3]; // 0x1A: Just to align. To get real name can use bNameOffset. 526 + __le16 name[]; // 0x1A: To get real name use name_off. 527 527 528 528 }; // sizeof(0x20) 529 - 530 - static_assert(sizeof(struct ATTR_LIST_ENTRY) == 0x20); 531 529 532 530 static inline u32 le_size(u8 name_len) 533 531 {
+13 -16
fs/ntfs3/ntfs_fs.h
··· 61 61 62 62 /* sbi->flags */ 63 63 #define NTFS_FLAGS_NODISCARD 0x00000001 64 + /* ntfs in shutdown state. */ 65 + #define NTFS_FLAGS_SHUTDOWN_BIT 0x00000002 /* == 4*/ 64 66 /* Set when LogFile is replaying. */ 65 67 #define NTFS_FLAGS_LOG_REPLAYING 0x00000008 66 68 /* Set when we changed first MFT's which copy must be updated in $MftMirr. */ ··· 228 226 u64 maxbytes; // Maximum size for normal files. 229 227 u64 maxbytes_sparse; // Maximum size for sparse file. 230 228 231 - u32 flags; // See NTFS_FLAGS_XXX. 229 + unsigned long flags; // See NTFS_FLAGS_ 232 230 233 231 CLST zone_max; // Maximum MFT zone length in clusters 234 232 CLST bad_clusters; // The count of marked bad clusters. ··· 475 473 int al_update(struct ntfs_inode *ni, int sync); 476 474 static inline size_t al_aligned(size_t size) 477 475 { 478 - return (size + 1023) & ~(size_t)1023; 476 + return size_add(size, 1023) & ~(size_t)1023; 479 477 } 480 478 481 479 /* Globals from bitfunc.c */ ··· 502 500 int ntfs_file_open(struct inode *inode, struct file *file); 503 501 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 504 502 __u64 start, __u64 len); 503 + long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg); 504 + long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg); 505 505 extern const struct inode_operations ntfs_special_inode_operations; 506 506 extern const struct inode_operations ntfs_file_inode_operations; 507 507 extern const struct file_operations ntfs_file_operations; ··· 588 584 int log_replay(struct ntfs_inode *ni, bool *initialized); 589 585 590 586 /* Globals from fsntfs.c */ 587 + struct buffer_head *ntfs_bread(struct super_block *sb, sector_t block); 591 588 bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes); 592 589 int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes, 593 590 bool simple); ··· 877 872 878 873 int ntfs_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry); 879 874 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size); 880 - extern const struct xattr_handler * const ntfs_xattr_handlers[]; 875 + extern const struct xattr_handler *const ntfs_xattr_handlers[]; 881 876 882 877 int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size); 883 878 void ntfs_get_wsl_perm(struct inode *inode); ··· 1004 999 return sb->s_fs_info; 1005 1000 } 1006 1001 1002 + static inline int ntfs3_forced_shutdown(struct super_block *sb) 1003 + { 1004 + return test_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags); 1005 + } 1006 + 1007 1007 /* 1008 1008 * ntfs_up_cluster - Align up on cluster boundary. 1009 1009 */ ··· 1033 1023 static inline u64 bytes_to_block(const struct super_block *sb, u64 size) 1034 1024 { 1035 1025 return (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; 1036 - } 1037 - 1038 - static inline struct buffer_head *ntfs_bread(struct super_block *sb, 1039 - sector_t block) 1040 - { 1041 - struct buffer_head *bh = sb_bread(sb, block); 1042 - 1043 - if (bh) 1044 - return bh; 1045 - 1046 - ntfs_err(sb, "failed to read volume at offset 0x%llx", 1047 - (u64)block << sb->s_blocksize_bits); 1048 - return NULL; 1049 1026 } 1050 1027 1051 1028 static inline struct ntfs_inode *ntfs_i(struct inode *inode)
+15 -3
fs/ntfs3/record.c
··· 279 279 if (t16 > asize) 280 280 return NULL; 281 281 282 - if (t16 + le32_to_cpu(attr->res.data_size) > asize) 282 + if (le32_to_cpu(attr->res.data_size) > asize - t16) 283 283 return NULL; 284 284 285 285 t32 = sizeof(short) * attr->name_len; ··· 535 535 return false; 536 536 537 537 if (ni && is_attr_indexed(attr)) { 538 - le16_add_cpu(&ni->mi.mrec->hard_links, -1); 539 - ni->mi.dirty = true; 538 + u16 links = le16_to_cpu(ni->mi.mrec->hard_links); 539 + struct ATTR_FILE_NAME *fname = 540 + attr->type != ATTR_NAME ? 541 + NULL : 542 + resident_data_ex(attr, 543 + SIZEOF_ATTRIBUTE_FILENAME); 544 + if (fname && fname->type == FILE_NAME_DOS) { 545 + /* Do not decrease links count deleting DOS name. */ 546 + } else if (!links) { 547 + /* minor error. Not critical. */ 548 + } else { 549 + ni->mi.mrec->hard_links = cpu_to_le16(links - 1); 550 + ni->mi.dirty = true; 551 + } 540 552 } 541 553 542 554 used -= asize;
+32 -22
fs/ntfs3/super.c
··· 122 122 123 123 if (name) { 124 124 struct dentry *de = d_find_alias(inode); 125 - const u32 name_len = ARRAY_SIZE(s_name_buf) - 1; 126 125 127 126 if (de) { 128 127 spin_lock(&de->d_lock); 129 - snprintf(name, name_len, " \"%s\"", de->d_name.name); 128 + snprintf(name, sizeof(s_name_buf), " \"%s\"", 129 + de->d_name.name); 130 130 spin_unlock(&de->d_lock); 131 - name[name_len] = 0; /* To be sure. */ 132 131 } else { 133 132 name[0] = 0; 134 133 } ··· 624 625 { 625 626 kfree(sbi->new_rec); 626 627 kvfree(ntfs_put_shared(sbi->upcase)); 627 - kfree(sbi->def_table); 628 + kvfree(sbi->def_table); 628 629 kfree(sbi->compress.lznt); 629 630 #ifdef CONFIG_NTFS3_LZX_XPRESS 630 631 xpress_free_decompressor(sbi->compress.xpress); ··· 714 715 } 715 716 716 717 /* 718 + * ntfs_shutdown - super_operations::shutdown 719 + */ 720 + static void ntfs_shutdown(struct super_block *sb) 721 + { 722 + set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags); 723 + } 724 + 725 + /* 717 726 * ntfs_sync_fs - super_operations::sync_fs 718 727 */ 719 728 static int ntfs_sync_fs(struct super_block *sb, int wait) ··· 730 723 struct ntfs_sb_info *sbi = sb->s_fs_info; 731 724 struct ntfs_inode *ni; 732 725 struct inode *inode; 726 + 727 + if (unlikely(ntfs3_forced_shutdown(sb))) 728 + return -EIO; 733 729 734 730 ni = sbi->security.ni; 735 731 if (ni) { ··· 773 763 .put_super = ntfs_put_super, 774 764 .statfs = ntfs_statfs, 775 765 .show_options = ntfs_show_options, 766 + .shutdown = ntfs_shutdown, 776 767 .sync_fs = ntfs_sync_fs, 777 768 .write_inode = ntfs3_write_inode, 778 769 }; ··· 877 866 u16 fn, ao; 878 867 u8 cluster_bits; 879 868 u32 boot_off = 0; 869 + sector_t boot_block = 0; 880 870 const char *hint = "Primary boot"; 881 871 882 872 /* Save original dev_size. Used with alternative boot. */ ··· 885 873 886 874 sbi->volume.blocks = dev_size >> PAGE_SHIFT; 887 875 888 - bh = ntfs_bread(sb, 0); 876 + read_boot: 877 + bh = ntfs_bread(sb, boot_block); 889 878 if (!bh) 890 - return -EIO; 879 + return boot_block ? -EINVAL : -EIO; 891 880 892 - check_boot: 893 881 err = -EINVAL; 894 882 895 883 /* Corrupted image; do not read OOB */ ··· 1120 1108 } 1121 1109 1122 1110 out: 1123 - if (err == -EINVAL && !bh->b_blocknr && dev_size0 > PAGE_SHIFT) { 1111 + brelse(bh); 1112 + 1113 + if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) { 1124 1114 u32 block_size = min_t(u32, sector_size, PAGE_SIZE); 1125 1115 u64 lbo = dev_size0 - sizeof(*boot); 1126 1116 1127 - /* 1128 - * Try alternative boot (last sector) 1129 - */ 1130 - brelse(bh); 1131 - 1132 - sb_set_blocksize(sb, block_size); 1133 - bh = ntfs_bread(sb, lbo >> blksize_bits(block_size)); 1134 - if (!bh) 1135 - return -EINVAL; 1136 - 1117 + boot_block = lbo >> blksize_bits(block_size); 1137 1118 boot_off = lbo & (block_size - 1); 1138 - hint = "Alternative boot"; 1139 - dev_size = dev_size0; /* restore original size. */ 1140 - goto check_boot; 1119 + if (boot_block && block_size >= boot_off + sizeof(*boot)) { 1120 + /* 1121 + * Try alternative boot (last sector) 1122 + */ 1123 + sb_set_blocksize(sb, block_size); 1124 + hint = "Alternative boot"; 1125 + dev_size = dev_size0; /* restore original size. */ 1126 + goto read_boot; 1127 + } 1141 1128 } 1142 - brelse(bh); 1143 1129 1144 1130 return err; 1145 1131 }
+6
fs/ntfs3/xattr.c
··· 219 219 if (!ea->name_len) 220 220 break; 221 221 222 + if (ea->name_len > ea_size) 223 + break; 224 + 222 225 if (buffer) { 223 226 /* Check if we can use field ea->name */ 224 227 if (off + ea_size > size) ··· 746 743 { 747 744 int err; 748 745 struct ntfs_inode *ni = ntfs_i(inode); 746 + 747 + if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 748 + return -EIO; 749 749 750 750 /* Dispatch request. */ 751 751 if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {