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

Pull ntfs3 updates from Konstantin Komarov:
"Fixes:
- reusing of the file index (could cause the file to be trimmed)
- infinite dir enumeration
- taking DOS names into account during link counting
- le32_to_cpu conversion, 32 bit overflow, NULL check
- some code was refactored

Changes:
- removed max link count info display during driver init

Remove:
- atomic_open has been removed for lack of use"

* tag 'ntfs3_for_6.10' of https://github.com/Paragon-Software-Group/linux-ntfs3:
fs/ntfs3: Break dir enumeration if directory contents error
fs/ntfs3: Fix case when index is reused during tree transformation
fs/ntfs3: Mark volume as dirty if xattr is broken
fs/ntfs3: Always make file nonresident on fallocate call
fs/ntfs3: Redesign ntfs_create_inode to return error code instead of inode
fs/ntfs3: Use variable length array instead of fixed size
fs/ntfs3: Use 64 bit variable to avoid 32 bit overflow
fs/ntfs3: Check 'folio' pointer for NULL
fs/ntfs3: Missed le32_to_cpu conversion
fs/ntfs3: Remove max link count info display during driver init
fs/ntfs3: Taking DOS names into account during link counting
fs/ntfs3: remove atomic_open
fs/ntfs3: use kcalloc() instead of kzalloc()

+98 -154
+32
fs/ntfs3/attrib.c
··· 2558 2558 2559 2559 goto out; 2560 2560 } 2561 + 2562 + /* 2563 + * attr_force_nonresident 2564 + * 2565 + * Convert default data attribute into non resident form. 2566 + */ 2567 + int attr_force_nonresident(struct ntfs_inode *ni) 2568 + { 2569 + int err; 2570 + struct ATTRIB *attr; 2571 + struct ATTR_LIST_ENTRY *le = NULL; 2572 + struct mft_inode *mi; 2573 + 2574 + attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi); 2575 + if (!attr) { 2576 + ntfs_bad_inode(&ni->vfs_inode, "no data attribute"); 2577 + return -ENOENT; 2578 + } 2579 + 2580 + if (attr->non_res) { 2581 + /* Already non resident. */ 2582 + return 0; 2583 + } 2584 + 2585 + down_write(&ni->file.run_lock); 2586 + err = attr_make_nonresident(ni, attr, le, mi, 2587 + le32_to_cpu(attr->res.data_size), 2588 + &ni->file.run, &attr, NULL); 2589 + up_write(&ni->file.run_lock); 2590 + 2591 + return err; 2592 + }
+1
fs/ntfs3/dir.c
··· 475 475 vbo = (u64)bit << index_bits; 476 476 if (vbo >= i_size) { 477 477 ntfs_inode_err(dir, "Looks like your dir is corrupt"); 478 + ctx->pos = eod; 478 479 err = -EINVAL; 479 480 goto out; 480 481 }
+9
fs/ntfs3/file.c
··· 578 578 /* Check new size. */ 579 579 u8 cluster_bits = sbi->cluster_bits; 580 580 581 + /* Be sure file is non resident. */ 582 + if (is_resident(ni)) { 583 + ni_lock(ni); 584 + err = attr_force_nonresident(ni); 585 + ni_unlock(ni); 586 + if (err) 587 + goto out; 588 + } 589 + 581 590 /* generic/213: expected -ENOSPC instead of -EFBIG. */ 582 591 if (!is_supported_holes) { 583 592 loff_t to_alloc = new_size - inode_get_bytes(inode);
+1 -1
fs/ntfs3/frecord.c
··· 2636 2636 goto out1; 2637 2637 } 2638 2638 2639 - pages_disk = kzalloc(npages_disk * sizeof(struct page *), GFP_NOFS); 2639 + pages_disk = kcalloc(npages_disk, sizeof(*pages_disk), GFP_NOFS); 2640 2640 if (!pages_disk) { 2641 2641 err = -ENOMEM; 2642 2642 goto out2;
+3 -2
fs/ntfs3/fslog.c
··· 517 517 seq_bits -= 1; 518 518 } 519 519 520 - if (seq_bits != ra->seq_num_bits) 520 + if (seq_bits != le32_to_cpu(ra->seq_num_bits)) 521 521 return false; 522 522 523 523 /* The log page data offset and record header length must be quad-aligned. */ ··· 1184 1184 static int log_read_rst(struct ntfs_log *log, bool first, 1185 1185 struct restart_info *info) 1186 1186 { 1187 - u32 skip, vbo; 1187 + u32 skip; 1188 + u64 vbo; 1188 1189 struct RESTART_HDR *r_page = NULL; 1189 1190 1190 1191 /* Determine which restart area we are looking for. */
+6
fs/ntfs3/index.c
··· 1534 1534 goto out1; 1535 1535 } 1536 1536 1537 + if (data_size <= le64_to_cpu(alloc->nres.data_size)) { 1538 + /* Reuse index. */ 1539 + goto out; 1540 + } 1541 + 1537 1542 /* Increase allocation. */ 1538 1543 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len, 1539 1544 &indx->alloc_run, data_size, &data_size, true, ··· 1552 1547 if (in->name == I30_NAME) 1553 1548 i_size_write(&ni->vfs_inode, data_size); 1554 1549 1550 + out: 1555 1551 *vbn = bit << indx->idx2vbn_bits; 1556 1552 1557 1553 return 0;
+26 -20
fs/ntfs3/inode.c
··· 37 37 bool is_dir; 38 38 unsigned long ino = inode->i_ino; 39 39 u32 rp_fa = 0, asize, t32; 40 - u16 roff, rsize, names = 0; 40 + u16 roff, rsize, names = 0, links = 0; 41 41 const struct ATTR_FILE_NAME *fname = NULL; 42 42 const struct INDEX_ROOT *root; 43 43 struct REPARSE_DATA_BUFFER rp; // 0x18 bytes ··· 200 200 rsize < SIZEOF_ATTRIBUTE_FILENAME) 201 201 goto out; 202 202 203 + names += 1; 203 204 fname = Add2Ptr(attr, roff); 204 205 if (fname->type == FILE_NAME_DOS) 205 206 goto next_attr; 206 207 207 - names += 1; 208 + links += 1; 208 209 if (name && name->len == fname->name_len && 209 210 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len, 210 211 NULL, false)) ··· 430 429 ni->mi.dirty = true; 431 430 } 432 431 433 - set_nlink(inode, names); 432 + set_nlink(inode, links); 434 433 435 434 if (S_ISDIR(mode)) { 436 435 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY; ··· 577 576 clear_buffer_uptodate(bh); 578 577 579 578 if (is_resident(ni)) { 580 - ni_lock(ni); 581 - err = attr_data_read_resident(ni, &folio->page); 582 - ni_unlock(ni); 583 - 584 - if (!err) 585 - set_buffer_uptodate(bh); 579 + bh->b_blocknr = RESIDENT_LCN; 586 580 bh->b_size = block_size; 581 + if (!folio) { 582 + err = 0; 583 + } else { 584 + ni_lock(ni); 585 + err = attr_data_read_resident(ni, &folio->page); 586 + ni_unlock(ni); 587 + 588 + if (!err) 589 + set_buffer_uptodate(bh); 590 + } 587 591 return err; 588 592 } 589 593 ··· 1222 1216 * 1223 1217 * NOTE: if fnd != NULL (ntfs_atomic_open) then @dir is locked 1224 1218 */ 1225 - struct inode *ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir, 1226 - struct dentry *dentry, 1227 - const struct cpu_str *uni, umode_t mode, 1228 - dev_t dev, const char *symname, u32 size, 1229 - struct ntfs_fnd *fnd) 1219 + int ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir, 1220 + struct dentry *dentry, const struct cpu_str *uni, 1221 + umode_t mode, dev_t dev, const char *symname, u32 size, 1222 + struct ntfs_fnd *fnd) 1230 1223 { 1231 1224 int err; 1232 1225 struct super_block *sb = dir->i_sb; ··· 1249 1244 struct NTFS_DE *e, *new_de = NULL; 1250 1245 struct REPARSE_DATA_BUFFER *rp = NULL; 1251 1246 bool rp_inserted = false; 1247 + 1248 + /* New file will be resident or non resident. */ 1249 + const bool new_file_resident = 1; 1252 1250 1253 1251 if (!fnd) 1254 1252 ni_lock_dir(dir_ni); ··· 1492 1484 attr->size = cpu_to_le32(SIZEOF_RESIDENT); 1493 1485 attr->name_off = SIZEOF_RESIDENT_LE; 1494 1486 attr->res.data_off = SIZEOF_RESIDENT_LE; 1495 - } else if (S_ISREG(mode)) { 1487 + } else if (!new_file_resident && S_ISREG(mode)) { 1496 1488 /* 1497 1489 * Regular file. Create empty non resident data attribute. 1498 1490 */ ··· 1735 1727 if (!fnd) 1736 1728 ni_unlock(dir_ni); 1737 1729 1738 - if (err) 1739 - return ERR_PTR(err); 1730 + if (!err) 1731 + unlock_new_inode(inode); 1740 1732 1741 - unlock_new_inode(inode); 1742 - 1743 - return inode; 1733 + return err; 1744 1734 } 1745 1735 1746 1736 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
+8 -113
fs/ntfs3/namei.c
··· 107 107 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir, 108 108 struct dentry *dentry, umode_t mode, bool excl) 109 109 { 110 - struct inode *inode; 111 - 112 - inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0, 113 - NULL, 0, NULL); 114 - 115 - return IS_ERR(inode) ? PTR_ERR(inode) : 0; 110 + return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0, 111 + NULL, 0, NULL); 116 112 } 117 113 118 114 /* ··· 119 123 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 120 124 struct dentry *dentry, umode_t mode, dev_t rdev) 121 125 { 122 - struct inode *inode; 123 - 124 - inode = ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0, 125 - NULL); 126 - 127 - return IS_ERR(inode) ? PTR_ERR(inode) : 0; 126 + return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0, 127 + NULL); 128 128 } 129 129 130 130 /* ··· 192 200 struct dentry *dentry, const char *symname) 193 201 { 194 202 u32 size = strlen(symname); 195 - struct inode *inode; 196 203 197 204 if (unlikely(ntfs3_forced_shutdown(dir->i_sb))) 198 205 return -EIO; 199 206 200 - inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0, 201 - symname, size, NULL); 202 - 203 - return IS_ERR(inode) ? PTR_ERR(inode) : 0; 207 + return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0, 208 + symname, size, NULL); 204 209 } 205 210 206 211 /* ··· 206 217 static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 207 218 struct dentry *dentry, umode_t mode) 208 219 { 209 - struct inode *inode; 210 - 211 - inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0, 212 - NULL, 0, NULL); 213 - 214 - return IS_ERR(inode) ? PTR_ERR(inode) : 0; 220 + return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0, 221 + NULL, 0, NULL); 215 222 } 216 223 217 224 /* ··· 340 355 ni_unlock(dir_ni); 341 356 out: 342 357 __putname(de); 343 - return err; 344 - } 345 - 346 - /* 347 - * ntfs_atomic_open 348 - * 349 - * inode_operations::atomic_open 350 - */ 351 - static int ntfs_atomic_open(struct inode *dir, struct dentry *dentry, 352 - struct file *file, u32 flags, umode_t mode) 353 - { 354 - int err; 355 - struct inode *inode; 356 - struct ntfs_fnd *fnd = NULL; 357 - struct ntfs_inode *ni = ntfs_i(dir); 358 - struct dentry *d = NULL; 359 - struct cpu_str *uni = __getname(); 360 - bool locked = false; 361 - 362 - if (!uni) 363 - return -ENOMEM; 364 - 365 - err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name, 366 - dentry->d_name.len, uni, NTFS_NAME_LEN, 367 - UTF16_HOST_ENDIAN); 368 - if (err < 0) 369 - goto out; 370 - 371 - #ifdef CONFIG_NTFS3_FS_POSIX_ACL 372 - if (IS_POSIXACL(dir)) { 373 - /* 374 - * Load in cache current acl to avoid ni_lock(dir): 375 - * ntfs_create_inode -> ntfs_init_acl -> posix_acl_create -> 376 - * ntfs_get_acl -> ntfs_get_acl_ex -> ni_lock 377 - */ 378 - struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT); 379 - 380 - if (IS_ERR(p)) { 381 - err = PTR_ERR(p); 382 - goto out; 383 - } 384 - posix_acl_release(p); 385 - } 386 - #endif 387 - 388 - if (d_in_lookup(dentry)) { 389 - ni_lock_dir(ni); 390 - locked = true; 391 - fnd = fnd_get(); 392 - if (!fnd) { 393 - err = -ENOMEM; 394 - goto out1; 395 - } 396 - 397 - d = d_splice_alias(dir_search_u(dir, uni, fnd), dentry); 398 - if (IS_ERR(d)) { 399 - err = PTR_ERR(d); 400 - d = NULL; 401 - goto out2; 402 - } 403 - 404 - if (d) 405 - dentry = d; 406 - } 407 - 408 - if (!(flags & O_CREAT) || d_really_is_positive(dentry)) { 409 - err = finish_no_open(file, d); 410 - goto out2; 411 - } 412 - 413 - file->f_mode |= FMODE_CREATED; 414 - 415 - /* 416 - * fnd contains tree's path to insert to. 417 - * If fnd is not NULL then dir is locked. 418 - */ 419 - inode = ntfs_create_inode(file_mnt_idmap(file), dir, dentry, uni, 420 - mode, 0, NULL, 0, fnd); 421 - err = IS_ERR(inode) ? PTR_ERR(inode) : 422 - finish_open(file, dentry, ntfs_file_open); 423 - dput(d); 424 - 425 - out2: 426 - fnd_put(fnd); 427 - out1: 428 - if (locked) 429 - ni_unlock(ni); 430 - out: 431 - __putname(uni); 432 358 return err; 433 359 } 434 360 ··· 508 612 .setattr = ntfs3_setattr, 509 613 .getattr = ntfs_getattr, 510 614 .listxattr = ntfs_listxattr, 511 - .atomic_open = ntfs_atomic_open, 512 615 .fiemap = ntfs_fiemap, 513 616 }; 514 617
+1 -1
fs/ntfs3/ntfs.h
··· 59 59 struct cpu_str { 60 60 u8 len; 61 61 u8 unused; 62 - u16 name[10]; 62 + u16 name[]; 63 63 }; 64 64 65 65 struct le_str {
+5 -5
fs/ntfs3/ntfs_fs.h
··· 452 452 int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes); 453 453 int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes); 454 454 int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size); 455 + int attr_force_nonresident(struct ntfs_inode *ni); 455 456 456 457 /* Functions from attrlist.c */ 457 458 void al_destroy(struct ntfs_inode *ni); ··· 717 716 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1, 718 717 struct inode *i2); 719 718 int inode_write_data(struct inode *inode, const void *data, size_t bytes); 720 - struct inode *ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir, 721 - struct dentry *dentry, 722 - const struct cpu_str *uni, umode_t mode, 723 - dev_t dev, const char *symname, u32 size, 724 - struct ntfs_fnd *fnd); 719 + int ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir, 720 + struct dentry *dentry, const struct cpu_str *uni, 721 + umode_t mode, dev_t dev, const char *symname, u32 size, 722 + struct ntfs_fnd *fnd); 725 723 int ntfs_link_inode(struct inode *inode, struct dentry *dentry); 726 724 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry); 727 725 void ntfs_evict_inode(struct inode *inode);
+2 -9
fs/ntfs3/record.c
··· 534 534 if (aoff + asize > used) 535 535 return false; 536 536 537 - if (ni && is_attr_indexed(attr)) { 537 + if (ni && is_attr_indexed(attr) && attr->type == ATTR_NAME) { 538 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) { 539 + if (!links) { 547 540 /* minor error. Not critical. */ 548 541 } else { 549 542 ni->mi.mrec->hard_links = cpu_to_le16(links - 1);
-2
fs/ntfs3/super.c
··· 1861 1861 { 1862 1862 int err; 1863 1863 1864 - pr_info("ntfs3: Max link count %u\n", NTFS_LINK_MAX); 1865 - 1866 1864 if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL)) 1867 1865 pr_info("ntfs3: Enabled Linux POSIX ACLs support\n"); 1868 1866 if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
+4 -1
fs/ntfs3/xattr.c
··· 219 219 if (!ea->name_len) 220 220 break; 221 221 222 - if (ea->name_len > ea_size) 222 + if (ea->name_len > ea_size) { 223 + ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR); 224 + err = -EINVAL; /* corrupted fs */ 223 225 break; 226 + } 224 227 225 228 if (buffer) { 226 229 /* Check if we can use field ea->name */