Skip to contentSkip to conversation

smb/client: fix incorrect nlink returned by fstat()

PATCH Superseded ChenXiaoSong 1 participant 2 messages Jul 1

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
author reviewer
#1 lore raw

PATCH v3 1/1 smb/client: fix incorrect nlink returned by fstat()

From: ChenXiaoSong <[email protected]>

Reproducer:

1. mount -t cifs //${server_ip}/export /mnt
2. touch /mnt/file1; ln /mnt/file1 /mnt/file2; ln /mnt/file1 /mnt/file3
3. C program: int fd = open("/mnt/file1", O_RDONLY);
4. C program: struct stat stbuf; fstat(fd, &stbuf); stbuf.st_nlink is always 1, should be 3

cifs_atomic_open() already obtains the correct nlink. Setting NumberOfLinks to 0 in SMB2_open() triggers the CIFS_FATTR_UNKNOWN_NLINK flag in cifs_open_info_to_fattr(), which safely preserves the existing i_nlink in cifs_nlink_fattr_to_inode().

Refer to the detailed procedure below:

path_openat

    open_last_lookups
      lookup_open
        atomic_open
          cifs_atomic_open // dir->i_op->atomic_open
            cifs_lookup
              cifs_get_inode_info
                cifs_get_fattr
                  smb2_query_path_info // server->ops->query_path_info
                    smb2_compound_op
                      SMB2_open_init
                      case SMB2_OP_QUERY_INFO
                      SMB2_query_info_init(FILE_ALL_INFORMATION,)
                  cifs_open_info_to_fattr
                    fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks)
                update_inode_info
                  cifs_iget
                    cifs_fattr_to_inode
                      cifs_nlink_fattr_to_inode
                        set_nlink(inode, fattr->cf_nlink)
    do_open
      vfs_open
        do_dentry_open
          cifs_open
            cifs_nt_open
              smb2_open_file // server->ops->open
                SMB2_open
                  buf->NumberOfLinks = 0
              cifs_get_inode_info
                cifs_get_fattr
                  cifs_open_info_to_fattr
                    if (fattr->cf_nlink < 1) // true
                    fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK
                update_inode_info
                  cifs_fattr_to_inode
                    cifs_nlink_fattr_to_inode
                      if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) // true
                      return // do not modify nlink
---
 fs/smb/client/smb2pdu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/smb2pdu.c +1 -1
index 56caf8cbdb0b..0801674f49c6 100644
@@ -3378,7 +3378,7 @@ SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
 		buf->AllocationSize = rsp->AllocationSize;
 		buf->EndOfFile = rsp->EndofFile;
 		buf->Attributes = rsp->FileAttributes;
-		buf->NumberOfLinks = cpu_to_le32(1);
+		buf->NumberOfLinks = cpu_to_le32(0); /* invalid nlink */
 		buf->DeletePending = 0; /* successful open = not delete pending */
 	}
 
Signature · 1 line
2.54.0