Skip to contentSkip to conversation

ntfs: update iomap and address space operations

bug report Dan Carpenter 2 participants 2 messages Apr 10 VFS

lore
Share & tools
lore
#0 lore raw

Hello Namjae Jeon,

Commit b041ca562526 ("ntfs: update iomap and address space operations") from Feb 13, 2026 (linux-next), leads to the following Smatch static checker warning:

fs/ntfs/iomap.c:517 ntfs_write_simple_iomap_begin_non_resident()
error: uninitialized symbol 'err'.

fs/ntfs/iomap.c

    379 static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_t offset,
    380                                                       loff_t length, struct iomap *iomap)
    381 {
    382         struct ntfs_inode *ni = NTFS_I(inode);
    383         struct ntfs_volume *vol = ni->vol;
    384         loff_t vcn_ofs, rl_length;
    385         struct runlist_element *rl, *rlc;
    386         bool is_retry = false;
    387         int err;

I think the intent was to initialize err to zero.

    388         s64 vcn, lcn;
    389         s64 max_clu_count =
    390                 ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size));
    391 
    392         vcn = ntfs_bytes_to_cluster(vol, offset);
    393         vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset);
    394 
    395         down_read(&ni->runlist.lock);
    396         rl = ni->runlist.rl;
    397         if (!rl) {
    398                 up_read(&ni->runlist.lock);
    399                 err = ntfs_map_runlist(ni, vcn);
    400                 if (err) {
    401                         mutex_unlock(&ni->mrec_lock);
    402                         return -ENOENT;
    403                 }

Otherwise if rl is true

    404                 down_read(&ni->runlist.lock);
    405                 rl = ni->runlist.rl;
    406         }
    407         up_read(&ni->runlist.lock);
    408 
    409         down_write(&ni->runlist.lock);
    410 remap_rl:
    411         /* Seek to element containing target vcn. */
    412         rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn);
    413         if (IS_ERR(rl)) {
    414                 up_write(&ni->runlist.lock);
    415                 mutex_unlock(&ni->mrec_lock);
    416                 return -EIO;
    417         }
    418         lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
    419 
    420         if (lcn <= LCN_RL_NOT_MAPPED && is_retry == false) {
    421                 is_retry = true;
    422                 if (!ntfs_map_runlist_nolock(ni, vcn, NULL)) {
    423                         rl = ni->runlist.rl;
    424                         goto remap_rl;
    425                 }
    426         }
    427 
    428         max_clu_count = min(max_clu_count, rl->length - (vcn - rl->vcn));
    429         if (max_clu_count == 0) {
    430                 ntfs_error(inode->i_sb,
    431                                 "runlist(vcn : %lld, length : %lld) is corrupted\n",
    432                                 rl->vcn, rl->length);
    433                 up_write(&ni->runlist.lock);
    434                 mutex_unlock(&ni->mrec_lock);
    435                 return -EIO;
    436         }
    437 
    438         iomap->bdev = inode->i_sb->s_bdev;
    439         iomap->offset = offset;
    440 
    441         if (lcn <= LCN_DELALLOC) {
    442                 if (lcn < LCN_DELALLOC) {
    443                         max_clu_count =
    444                                 ntfs_available_clusters_count(vol, max_clu_count);
    445                         if (max_clu_count < 0) {
    446                                 err = max_clu_count;
    447                                 up_write(&ni->runlist.lock);
    448                                 mutex_unlock(&ni->mrec_lock);
    449                                 return err;
    450                         }
    451                 }
    452 
    453                 iomap->type = IOMAP_DELALLOC;
    454                 iomap->addr = IOMAP_NULL_ADDR;
    455 
    456                 if (lcn <= LCN_HOLE) {
    457                         size_t new_rl_count;
    458 
    459                         rlc = kmalloc(sizeof(struct runlist_element) * 2,
    460                                         GFP_NOFS);
    461                         if (!rlc) {
    462                                 up_write(&ni->runlist.lock);
    463                                 mutex_unlock(&ni->mrec_lock);
    464                                 return -ENOMEM;
    465                         }
    466 
    467                         rlc->vcn = vcn;
    468                         rlc->lcn = LCN_DELALLOC;
    469                         rlc->length = max_clu_count;
    470 
    471                         rlc[1].vcn = vcn + max_clu_count;
    472                         rlc[1].lcn = LCN_RL_NOT_MAPPED;
    473                         rlc[1].length = 0;
    474 
    475                         rl = ntfs_runlists_merge(&ni->runlist, rlc, 0,
    476                                         &new_rl_count);
    477                         if (IS_ERR(rl)) {
    478                                 ntfs_error(vol->sb, "Failed to merge runlists");
    479                                 up_write(&ni->runlist.lock);
    480                                 mutex_unlock(&ni->mrec_lock);
    481                                 kvfree(rlc);
    482                                 return PTR_ERR(rl);
    483                         }
    484 
    485                         ni->runlist.rl = rl;
    486                         ni->runlist.count = new_rl_count;
    487                         ni->i_dealloc_clusters += max_clu_count;
    488                 }
    489                 up_write(&ni->runlist.lock);
    490                 mutex_unlock(&ni->mrec_lock);
    491 
    492                 if (lcn < LCN_DELALLOC)
    493                         ntfs_hold_dirty_clusters(vol, max_clu_count);
    494 
    495                 rl_length = ntfs_cluster_to_bytes(vol, max_clu_count);
    496                 if (length > rl_length - vcn_ofs)
    497                         iomap->length = rl_length - vcn_ofs;
    498                 else
    499                         iomap->length = length;
    500 
    501                 iomap->flags = IOMAP_F_NEW;
    502                 if (lcn <= LCN_HOLE) {
    503                         loff_t end = offset + length;
    504 
    505                         if (vcn_ofs || ((vol->cluster_size > iomap->length) &&
    506                                         end < ni->initialized_size)) {
    507                                 loff_t z_start, z_end;
    508 
    509                                 z_start = vcn << vol->cluster_size_bits;
    510                                 z_end = min_t(loff_t, z_start + vol->cluster_size,
    511                                               i_size_read(inode));
    512                                 if (z_end > z_start)
    513                                         err = ntfs_zero_range(inode,
    514                                                               z_start,
    515                                                               z_end - z_start);

And we don't hit this assignment

    516                         }
--> 517                         if ((!err || err == -EPERM) &&
                                      ^^^

then err is uninitialized

    518                             max_clu_count > 1 &&
    519                             (iomap->length & vol->cluster_size_mask &&
    520                              end < ni->initialized_size)) {
    521                                 loff_t z_start, z_end;
    522 
    523                                 z_start = (vcn + max_clu_count - 1) <<
    524                                         vol->cluster_size_bits;
    525                                 z_end = min_t(loff_t, z_start + vol->cluster_size,
    526                                               i_size_read(inode));
    527                                 if (z_end > z_start)
    528                                         err = ntfs_zero_range(inode,
    529                                                               z_start,
    530                                                               z_end - z_start);
    531                         }
    532 
    533                         if (err == -EPERM)
    534                                 err = 0;
    535                         if (err) {
    536                                 ntfs_release_dirty_clusters(vol, max_clu_count);
    537                                 return err;
    538                         }
    539                 }
    540         } else {
    541                 up_write(&ni->runlist.lock);
    542                 mutex_unlock(&ni->mrec_lock);
    543 
    544                 iomap->type = IOMAP_MAPPED;
    545                 iomap->addr = ntfs_cluster_to_bytes(vol, lcn) + vcn_ofs;
    546 
    547                 rl_length = ntfs_cluster_to_bytes(vol, max_clu_count);
    548                 if (length > rl_length - vcn_ofs)
    549                         iomap->length = rl_length - vcn_ofs;
    550                 else
    551                         iomap->length = length;
    552         }
    553 
    554         return 0;
    555 }

This email is a free service from the Smatch-CI project [smatch.sf.net].

regards,
dan carpenter

#1 lore raw
Quoted 2 lines · Dan Carpenter

Hello Namjae Jeon,

Hi Dan,

I will check your reports.
Thank you for the reports!