Skip to contentSkip to conversation

cifs: open files should not hold ref on superblock

PATCH In review [email protected] 4 participants 9 messages Mar 4 – May 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cifs: open files should not hold ref on superblock

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")
---
 fs/smb/client/cifsfs.c    |  7 +++++--
 fs/smb/client/cifsproto.h |  1 +
 fs/smb/client/file.c      | 11 ----------
 fs/smb/client/misc.c      | 42 +++++++++++++++++++++++++++++++++++++++
 fs/smb/client/trace.h     |  2 ++
 5 files changed, 50 insertions(+), 13 deletions(-)
fs/smb/client/cifsfs.c +5 -2
index 99b04234a08e6..fcc56481d6cf2 100644
@@ -330,10 +330,14 @@ static void cifs_kill_sb(struct super_block *sb)
 
 	/*
 	 * We need to release all dentries for the cached directories
-	 * before we kill the sb.
+	 * and close all deferred file handles before we kill the sb.
 	 */
 	if (cifs_sb->root) {
 		close_all_cached_dirs(cifs_sb);
+		cifs_close_all_deferred_files_sb(cifs_sb);
+
+		/* Wait for all pending oplock breaks to complete */
+		flush_workqueue(cifsoplockd_wq);
 
 		/* finally release root dentry */
 		dput(cifs_sb->root);
@@ -864,7 +868,6 @@ static void cifs_umount_begin(struct super_block *sb)
 	spin_unlock(&tcon->tc_lock);
 	spin_unlock(&cifs_tcp_ses_lock);
 
-	cifs_close_all_deferred_files(tcon);
 	/* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
 	/* cancel_notify_requests(tcon); */
 	if (tcon->ses && tcon->ses->server) {
fs/smb/client/cifsproto.h +1 -0
index 96d6b5325aa33..800a7e418c326 100644
@@ -261,6 +261,7 @@ void cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode);
 
 void cifs_close_all_deferred_files(struct cifs_tcon *tcon);
 
+void cifs_close_all_deferred_files_sb(struct cifs_sb_info *cifs_sb);
 void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
 					   struct dentry *dentry);
 
fs/smb/client/file.c +0 -11
index 18f31d4eb98de..fb4f9aafe1386 100644
@@ -711,8 +711,6 @@ struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
 	mutex_init(&cfile->fh_mutex);
 	spin_lock_init(&cfile->file_info_lock);
 
-	cifs_sb_active(inode->i_sb);
-
 	/*
 	 * If the server returned a read oplock and we have mandatory brlocks,
 	 * set oplock level to None.
@@ -767,7 +765,6 @@ static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
 	struct inode *inode = d_inode(cifs_file->dentry);
 	struct cifsInodeInfo *cifsi = CIFS_I(inode);
 	struct cifsLockInfo *li, *tmp;
-	struct super_block *sb = inode->i_sb;
 
 	/*
 	 * Delete any outstanding lock records. We'll lose them when the file
@@ -785,7 +782,6 @@ static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
 
 	cifs_put_tlink(cifs_file->tlink);
 	dput(cifs_file->dentry);
-	cifs_sb_deactive(sb);
 	kfree(cifs_file->symlink_target);
 	kfree(cifs_file);
 }
@@ -3165,12 +3161,6 @@ void cifs_oplock_break(struct work_struct *work)
 	__u64 persistent_fid, volatile_fid;
 	__u16 net_fid;
 
-	/*
-	 * Hold a reference to the superblock to prevent it and its inodes from
-	 * being freed while we are accessing cinode. Otherwise, _cifsFileInfo_put()
-	 * may release the last reference to the sb and trigger inode eviction.
-	 */
-	cifs_sb_active(sb);
 	wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
 			TASK_UNINTERRUPTIBLE);
 
@@ -3255,7 +3245,6 @@ void cifs_oplock_break(struct work_struct *work)
 	cifs_put_tlink(tlink);
 out:
 	cifs_done_oplock_break(cinode);
-	cifs_sb_deactive(sb);
 }
 
 static int cifs_swap_activate(struct swap_info_struct *sis,
fs/smb/client/misc.c +42 -0

54 lines not on this page · raw message

fs/smb/client/trace.h +2 -0
index 9228f95cae2bd..acfbb63086ea2 100644
@@ -176,6 +176,7 @@
 	EM(netfs_trace_tcon_ref_get_cached_laundromat,	"GET Ch-Lau") \
 	EM(netfs_trace_tcon_ref_get_cached_lease_break,	"GET Ch-Lea") \
 	EM(netfs_trace_tcon_ref_get_cancelled_close,	"GET Cn-Cls") \
+	EM(netfs_trace_tcon_ref_get_close_defer_files,	"GET Cl-Def") \
 	EM(netfs_trace_tcon_ref_get_dfs_refer,		"GET DfsRef") \
 	EM(netfs_trace_tcon_ref_get_find,		"GET Find  ") \
 	EM(netfs_trace_tcon_ref_get_find_sess_tcon,	"GET FndSes") \
@@ -187,6 +188,7 @@
 	EM(netfs_trace_tcon_ref_put_cancelled_close,	"PUT Cn-Cls") \
 	EM(netfs_trace_tcon_ref_put_cancelled_close_fid, "PUT Cn-Fid") \
 	EM(netfs_trace_tcon_ref_put_cancelled_mid,	"PUT Cn-Mid") \
+	EM(netfs_trace_tcon_ref_put_close_defer_files,	"PUT Cl-Def") \
 	EM(netfs_trace_tcon_ref_put_mnt_ctx,		"PUT MntCtx") \
 	EM(netfs_trace_tcon_ref_put_dfs_refer,		"PUT DfsRfr") \
 	EM(netfs_trace_tcon_ref_put_reconnect_server,	"PUT Reconn") \
Signature · 1 line
2.43.0
#1 lore raw

merged into cifs-2.6.git for-next pending more review and testing

Quoted 217 lines · On Wed, Mar 4, 2026 at 6:46 AM <[email protected]> wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")
---
 fs/smb/client/cifsfs.c    |  7 +++++--
 fs/smb/client/cifsproto.h |  1 +
 fs/smb/client/file.c      | 11 ----------
 fs/smb/client/misc.c      | 42 +++++++++++++++++++++++++++++++++++++++
 fs/smb/client/trace.h     |  2 ++
 5 files changed, 50 insertions(+), 13 deletions(-)
fs/smb/client/cifsfs.c +5 -2
index 99b04234a08e6..fcc56481d6cf2 100644
@@ -330,10 +330,14 @@ static void cifs_kill_sb(struct super_block *sb)
 
        /*
         * We need to release all dentries for the cached directories
-        * before we kill the sb.
+        * and close all deferred file handles before we kill the sb.
         */
        if (cifs_sb->root) {
                close_all_cached_dirs(cifs_sb);
+               cifs_close_all_deferred_files_sb(cifs_sb);
+
+               /* Wait for all pending oplock breaks to complete */
+               flush_workqueue(cifsoplockd_wq);
 
                /* finally release root dentry */
                dput(cifs_sb->root);
@@ -864,7 +868,6 @@ static void cifs_umount_begin(struct super_block *sb)
        spin_unlock(&tcon->tc_lock);
        spin_unlock(&cifs_tcp_ses_lock);
 
-       cifs_close_all_deferred_files(tcon);
        /* cancel_brl_requests(tcon); */ /* BB mark all brl mids as exiting */
        /* cancel_notify_requests(tcon); */
        if (tcon->ses && tcon->ses->server) {
fs/smb/client/cifsproto.h +1 -0
index 96d6b5325aa33..800a7e418c326 100644
@@ -261,6 +261,7 @@ void cifs_close_deferred_file(struct cifsInodeInfo *cifs_inode);
 
 void cifs_close_all_deferred_files(struct cifs_tcon *tcon);
 
+void cifs_close_all_deferred_files_sb(struct cifs_sb_info *cifs_sb);
 void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
                                           struct dentry *dentry);
 
fs/smb/client/file.c +0 -11
index 18f31d4eb98de..fb4f9aafe1386 100644
@@ -711,8 +711,6 @@ struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
        mutex_init(&cfile->fh_mutex);
        spin_lock_init(&cfile->file_info_lock);
 
-       cifs_sb_active(inode->i_sb);
-
        /*
         * If the server returned a read oplock and we have mandatory brlocks,
         * set oplock level to None.
@@ -767,7 +765,6 @@ static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
        struct inode *inode = d_inode(cifs_file->dentry);
        struct cifsInodeInfo *cifsi = CIFS_I(inode);
        struct cifsLockInfo *li, *tmp;
-       struct super_block *sb = inode->i_sb;
 
        /*
         * Delete any outstanding lock records. We'll lose them when the file
@@ -785,7 +782,6 @@ static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
 
        cifs_put_tlink(cifs_file->tlink);
        dput(cifs_file->dentry);
-       cifs_sb_deactive(sb);
        kfree(cifs_file->symlink_target);
        kfree(cifs_file);
 }
@@ -3165,12 +3161,6 @@ void cifs_oplock_break(struct work_struct *work)
        __u64 persistent_fid, volatile_fid;
        __u16 net_fid;
 
-       /*
-        * Hold a reference to the superblock to prevent it and its inodes from
-        * being freed while we are accessing cinode. Otherwise, _cifsFileInfo_put()
-        * may release the last reference to the sb and trigger inode eviction.
-        */
-       cifs_sb_active(sb);
        wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
                        TASK_UNINTERRUPTIBLE);
 
@@ -3255,7 +3245,6 @@ void cifs_oplock_break(struct work_struct *work)
        cifs_put_tlink(tlink);
 out:
        cifs_done_oplock_break(cinode);
-       cifs_sb_deactive(sb);
 }
 
 static int cifs_swap_activate(struct swap_info_struct *sis,
fs/smb/client/misc.c +42 -0
index 22cde46309fe0..318533210648d 100644
@@ -28,6 +28,11 @@
 #include "fs_context.h"
 #include "cached_dir.h"
 
+struct tcon_list {
+       struct list_head entry;
+       struct cifs_tcon *tcon;
+};
+
 /* The xid serves as a useful identifier for each incoming vfs request,
    in a similar way to the mid which is useful to track each sent smb,
    and CurrentXid can also provide a running counter (although it
@@ -550,6 +555,43 @@ cifs_close_all_deferred_files(struct cifs_tcon *tcon)
        }
 }
 
+void cifs_close_all_deferred_files_sb(struct cifs_sb_info *cifs_sb)
+{
+       struct rb_root *root = &cifs_sb->tlink_tree;
+       struct rb_node *node;
+       struct cifs_tcon *tcon;
+       struct tcon_link *tlink;
+       struct tcon_list *tmp_list, *q;
+       LIST_HEAD(tcon_head);
+
+       spin_lock(&cifs_sb->tlink_tree_lock);
+       for (node = rb_first(root); node; node = rb_next(node)) {
+               tlink = rb_entry(node, struct tcon_link, tl_rbnode);
+               tcon = tlink_tcon(tlink);
+               if (IS_ERR(tcon))
+                       continue;
+               tmp_list = kmalloc_obj(struct tcon_list, GFP_ATOMIC);
+               if (tmp_list == NULL)
+                       break;
+               tmp_list->tcon = tcon;
+               /* Take a reference on tcon to prevent it from being freed */
+               spin_lock(&tcon->tc_lock);
+               ++tcon->tc_count;
+               trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
+                                   netfs_trace_tcon_ref_get_close_defer_files);
+               spin_unlock(&tcon->tc_lock);
+               list_add_tail(&tmp_list->entry, &tcon_head);
+       }
+       spin_unlock(&cifs_sb->tlink_tree_lock);
+
+       list_for_each_entry_safe(tmp_list, q, &tcon_head, entry) {
+               cifs_close_all_deferred_files(tmp_list->tcon);
+               list_del(&tmp_list->entry);
+               cifs_put_tcon(tmp_list->tcon, netfs_trace_tcon_ref_put_close_defer_files);
+               kfree(tmp_list);
+       }
+}
+
 void cifs_close_deferred_file_under_dentry(struct cifs_tcon *tcon,
                                           struct dentry *dentry)
 {
fs/smb/client/trace.h +2 -1
index 9228f95cae2bd..acfbb63086ea2 100644
@@ -176,6 +176,7 @@
        EM(netfs_trace_tcon_ref_get_cached_laundromat,  "GET Ch-Lau") \
        EM(netfs_trace_tcon_ref_get_cached_lease_break, "GET Ch-Lea") \
        EM(netfs_trace_tcon_ref_get_cancelled_close,    "GET Cn-Cls") \
+       EM(netfs_trace_tcon_ref_get_close_defer_files,  "GET Cl-Def") \
        EM(netfs_trace_tcon_ref_get_dfs_refer,          "GET DfsRef") \
        EM(netfs_trace_tcon_ref_get_find,               "GET Find  ") \
        EM(netfs_trace_tcon_ref_get_find_sess_tcon,     "GET FndSes") \
@@ -187,6 +188,7 @@
        EM(netfs_trace_tcon_ref_put_cancelled_close,    "PUT Cn-Cls") \
        EM(netfs_trace_tcon_ref_put_cancelled_close_fid, "PUT Cn-Fid") \
        EM(netfs_trace_tcon_ref_put_cancelled_mid,      "PUT Cn-Mid") \
+       EM(netfs_trace_tcon_ref_put_close_defer_files,  "PUT Cl-Def") \
        EM(netfs_trace_tcon_ref_put_mnt_ctx,            "PUT MntCtx") \
        EM(netfs_trace_tcon_ref_put_dfs_refer,          "PUT DfsRfr") \
        EM(netfs_trace_tcon_ref_put_reconnect_server,   "PUT Reconn") \
--

2.43.0

Signature · 3 lines
Thanks,

Steve
#2 lore raw
In reply to [email protected]
Quoted 31 lines · On Wed, Mar 04, 2026 at 06:15:53PM +0530, [email protected] wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Quoted 5 lines
Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")

---

Hi Shyam,

So the side effect of the previous code is that the umount hangs until all the files are closed?

Thanks,

Signature · 2 lines
Henrique
SUSE Labs
#3 lore raw
Quoted 39 lines · Henrique Carvalho
Quoted 31 lines · On Wed, Mar 04, 2026 at 06:15:53PM +0530, [email protected] wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Quoted 5 lines
Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")

---

Quoted 4 lines

Hi Shyam,

So the side effect of the previous code is that the umount hangs until all the files are closed?

Hi Henrique
Umount works. All it does is decrement refcount on sb.
When the last file is closed (or when the last cifs_oplock_break processing completes) that's when cifs_kill_sb would get called. Before that if there's another mount of the same share, it will reuse the same session, tcon and open handles. As a result, an attempt to delete files on the mount point may fail (which is one of first things done by many xfstests).

Quoted 6 lines

Thanks,

--
Henrique
SUSE Labs

Signature · 2 lines
Regards,
Shyam
#4 lore raw
Quoted 46 lines · Shyam Prasad N
Quoted 39 lines · Henrique Carvalho
Quoted 31 lines · On Wed, Mar 04, 2026 at 06:15:53PM +0530, [email protected] wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Quoted 5 lines
Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")

---

Quoted 4 lines

Hi Shyam,

So the side effect of the previous code is that the umount hangs until all the files are closed?

Quoted 9 lines

Hi Henrique
Umount works. All it does is decrement refcount on sb.
When the last file is closed (or when the last cifs_oplock_break processing completes) that's when cifs_kill_sb would get called. Before that if there's another mount of the same share, it will reuse the same session, tcon and open handles. As a result, an attempt to delete files on the mount point may fail (which is one of first things done by many xfstests).

Thank you for the explanation.

I will wait for your v2.

#5 lore raw
Quoted 58 lines · Henrique Carvalho
Quoted 46 lines · Shyam Prasad N
Quoted 39 lines · Henrique Carvalho
Quoted 31 lines · On Wed, Mar 04, 2026 at 06:15:53PM +0530, [email protected] wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Quoted 5 lines
Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")

---

Quoted 4 lines

Hi Shyam,

So the side effect of the previous code is that the umount hangs until all the files are closed?

Quoted 9 lines

Hi Henrique
Umount works. All it does is decrement refcount on sb.
When the last file is closed (or when the last cifs_oplock_break processing completes) that's when cifs_kill_sb would get called. Before that if there's another mount of the same share, it will reuse the same session, tcon and open handles. As a result, an attempt to delete files on the mount point may fail (which is one of first things done by many xfstests).

Quoted 3 lines

Thank you for the explanation.

I will wait for your v2.

Hi Steve,

I ran generic/694 to understand why it is failing with this change. I think that this fix has just exposed a problem rather than caused it.

The test does the following:
1. either fallocates a file to 4G or pwrites to it
2. calls sync
3. runs stat to get number of blocks allocated for the file
4. umounts the share
5. mounts the share again
6. runs stat to get number of blocks allocated for the file
7. compares output of steps 3 and 6

Without this change, both step 3 and 6 would return 0, since even through umount/mount, the same file would remain open (since superblocks will be shared).
With this change, step 3 would return 0. Step 6 would return the right value.

If you use nosharesock even after reverting this change, you'll see the test failing.
Or even with this change if actimeo=0, then this test passes.

The real question to ask is why aren't we updating i_blocks even after sync succeeds?
My guess is that this has something to do with attribute caching when the handle is kept open.

Signature · 2 lines
Regards,
Shyam
#6 lore raw
Quoted 70 lines · Shyam Prasad N
Quoted 58 lines · Henrique Carvalho
Quoted 46 lines · Shyam Prasad N
Quoted 39 lines · Henrique Carvalho
Quoted 31 lines · On Wed, Mar 04, 2026 at 06:15:53PM +0530, [email protected] wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Quoted 5 lines
Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")

---

Quoted 4 lines

Hi Shyam,

So the side effect of the previous code is that the umount hangs until all the files are closed?

Quoted 9 lines

Hi Henrique
Umount works. All it does is decrement refcount on sb.
When the last file is closed (or when the last cifs_oplock_break processing completes) that's when cifs_kill_sb would get called. Before that if there's another mount of the same share, it will reuse the same session, tcon and open handles. As a result, an attempt to delete files on the mount point may fail (which is one of first things done by many xfstests).

Quoted 3 lines

Thank you for the explanation.

I will wait for your v2.

Hi Steve,

I ran generic/694 to understand why it is failing with this change. I think that this fix has just exposed a problem rather than caused it.

Quoted 8 lines

The test does the following:
1. either fallocates a file to 4G or pwrites to it
2. calls sync
3. runs stat to get number of blocks allocated for the file
4. umounts the share
5. mounts the share again
6. runs stat to get number of blocks allocated for the file
7. compares output of steps 3 and 6

Any chance of creating a small repro script for this that is easier to debug han the xfstest was?

Quoted 5 lines

Without this change, both step 3 and 6 would return 0, since even through umount/mount, the same file would remain open (since superblocks will be shared).
With this change, step 3 would return 0. Step 6 would return the right value.

Quoted 8 lines

If you use nosharesock even after reverting this change, you'll see the test failing.
Or even with this change if actimeo=0, then this test passes.

The real question to ask is why aren't we updating i_blocks even after sync succeeds?
My guess is that this has something to do with attribute caching when the handle is kept open.

That sounds an important bug to fix. Glad this test showed it.

Signature · 3 lines
Thanks,

Steve
#7 lore raw

Hi!

Quoted 80 lines · Steve French
Quoted 70 lines · Shyam Prasad N
Quoted 58 lines · Henrique Carvalho
Quoted 46 lines · Shyam Prasad N
Quoted 39 lines · Henrique Carvalho
Quoted 31 lines · On Wed, Mar 04, 2026 at 06:15:53PM +0530, [email protected] wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Quoted 5 lines
Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")

---

Quoted 4 lines

Hi Shyam,

So the side effect of the previous code is that the umount hangs until all the files are closed?

Quoted 9 lines

Hi Henrique
Umount works. All it does is decrement refcount on sb.
When the last file is closed (or when the last cifs_oplock_break processing completes) that's when cifs_kill_sb would get called. Before that if there's another mount of the same share, it will reuse the same session, tcon and open handles. As a result, an attempt to delete files on the mount point may fail (which is one of first things done by many xfstests).

Quoted 3 lines

Thank you for the explanation.

I will wait for your v2.

Hi Steve,

I ran generic/694 to understand why it is failing with this change. I think that this fix has just exposed a problem rather than caused it.

Quoted 8 lines

The test does the following:
1. either fallocates a file to 4G or pwrites to it
2. calls sync
3. runs stat to get number of blocks allocated for the file
4. umounts the share
5. mounts the share again
6. runs stat to get number of blocks allocated for the file
7. compares output of steps 3 and 6

Quoted 2 lines

Any chance of creating a small repro script for this that is easier to debug han the xfstest was?

I analyzed this issue and performed a simple local reproduction using smb3/smb21:

mount /dev/sda /mnt
mount -t cifs //127.0.0.1/share /mnt/test_mnt -o vers=3.0,guest

# smb versions below 3 do not support fallocate;
# generic/694 replaces it with pwrite.
# xfs_io -ft -c "pwrite 0 4G" /mnt/test_mnt/aaa
xfs_io -ft -c "falloc 0 4M" /mnt/test_mnt/aaa
stat -c "%b" /mnt/test_mnt/aaa
umount /mnt/test_mnt
mount -t cifs //127.0.0.1/share /mnt/test_mnt -o vers=3.0,guest
stat -c "%b" /mnt/test_mnt/aaa
Quoted 10 lines
Quoted 10 lines

Without this change, both step 3 and 6 would return 0, since even through umount/mount, the same file would remain open (since superblocks will be shared).
With this change, step 3 would return 0. Step 6 would return the right value.

If you use nosharesock even after reverting this change, you'll see the test failing.
Or even with this change if actimeo=0, then this test passes.

Quoted 9 lines
Quoted 4 lines

The real question to ask is why aren't we updating i_blocks even after sync succeeds?
My guess is that this has something to do with attribute caching when the handle is kept open.

That sounds an important bug to fix. Glad this test showed it.

xfs_io -ft -c "falloc 0 4M" /mnt/test_mnt/aaa opens the file for writing, which causes stat -> ... -> update_inode_info to skip updating inode->i_blocks, because is_size_safe_to_change() returns false.

The file is only removed from the list in _cifsFileInfo_put(), but that call is deferred, so the value obtained in step 3 doesn't match the expected value. Step 6, however, does match — because after the remount the file has not been opened for writing.

Before the patch was merged, both runs returned 0, because the second mount reused the sb instance and the inode instance from the first mount, and the interval between the two mounts was too short, causing cifs_dentry_needs_reval() to return false.

SMB 2.1 behaves similarly: pwrite writes 4 GB and the size shows as 8388608. If the server-side backing filesystem is ext4, it also initially shows 8388608(see ext4_file_getattr()), and only during the writeback path — when it's discovered that the extent count is too large and an additional ETB(extent tree block) is required — does it become 8388616(ext4_do_writepages -> ... -> ext4_ext_new_meta_block).

On the client side, cifs_write_end() updates inode->i_blocks but doesn't account for that metadata block. Although generic/694 issues a sync before stat, and the server flushes to disk upon receiving it, so by rights it should return 8388616 — it doesn't. This is because cifs_revalidate_dentry_attr() -> cifs_dentry_needs_reval() returns false directly, since CIFS_CACHE_READ evaluates to true; as a result, it doesn't even go to the server to fetch stat.

The oplock is only cleared to 0 in _cifsFileInfo_put(), and that itself is a deferred release, so the stat call never picks up the actual data from the remote server before _cifsFileInfo_put() called.

On the other hand, why is there no problem with cifs/smb2, and the issue only appears on smb21/smb3?

The difference is that prior to smb21, there was no concept of leases — only oplocks:

smb2_set_oplock_level
cinode->lease_granted = false

smb21_set_oplock_level
cinode->lease_granted = true

This means that on cifs/smb2, cifs_close() invokes _cifsFileInfo_put() immediately (rather than going through the deferred-close path), so the problem described above doesn't occur.

I'm new to SMB, so this is very much a beginner's analysis. I hope it's useful to others, and I'd really appreciate it if anyone could point out mistakes or things I've gotten wrong.

Thanks,
Zizhi Wo

#8 lore raw
Quoted 141 lines · Zizhi Wo

Hi!

Quoted 80 lines · Steve French
Quoted 70 lines · Shyam Prasad N
Quoted 58 lines · Henrique Carvalho
Quoted 46 lines · Shyam Prasad N
Quoted 39 lines · Henrique Carvalho
Quoted 31 lines · On Wed, Mar 04, 2026 at 06:15:53PM +0530, [email protected] wrote:

From: Shyam Prasad N <[email protected]>

Today whenever we deal with a file, in addition to holding a reference on the dentry, we also get a reference on the superblock. This happens in two cases:
1. when a new cinode is allocated
2. when an oplock break is being processed

The reasoning for holding the superblock ref was to make sure that when umount happens, if there are users of inodes and dentries, it does not try to clean them up and wait for the last ref to superblock to be dropped by last of such users.

But the side effect of doing that is that umount silently drops a ref on the superblock and we could have deferred closes and lease breaks still holding these refs.

Ideally, we should ensure that all of these users of inodes and dentries are cleaned up at the time of umount, which is what this code is doing.

This code change allows these code paths to use a ref on the dentry (and hence the inode). That way, umount is ensured to clean up SMB client resources when it's the last ref on the superblock (For ex: when same objects are shared).

The code change also moves the call to close all the files in deferred close list to the umount code path. It also waits for oplock_break workers to be flushed before calling kill_anon_super (which eventually frees up those objects).

Quoted 5 lines
Fixes 24261fc23db9 ("cifs: delay super block destruction until all cifsFileInfo objects are gone")
Fixes 705c79101ccf ("smb: client: fix use-after-free in cifs_oplock_break")

---

Quoted 4 lines

Hi Shyam,

So the side effect of the previous code is that the umount hangs until all the files are closed?

Quoted 9 lines

Hi Henrique
Umount works. All it does is decrement refcount on sb.
When the last file is closed (or when the last cifs_oplock_break processing completes) that's when cifs_kill_sb would get called. Before that if there's another mount of the same share, it will reuse the same session, tcon and open handles. As a result, an attempt to delete files on the mount point may fail (which is one of first things done by many xfstests).

Quoted 3 lines

Thank you for the explanation.

I will wait for your v2.

Hi Steve,

I ran generic/694 to understand why it is failing with this change. I think that this fix has just exposed a problem rather than caused it.

Quoted 8 lines

The test does the following:
1. either fallocates a file to 4G or pwrites to it
2. calls sync
3. runs stat to get number of blocks allocated for the file
4. umounts the share
5. mounts the share again
6. runs stat to get number of blocks allocated for the file
7. compares output of steps 3 and 6

Quoted 2 lines

Any chance of creating a small repro script for this that is easier to debug han the xfstest was?

I analyzed this issue and performed a simple local reproduction using smb3/smb21:

mount /dev/sda /mnt
mount -t cifs //127.0.0.1/share /mnt/test_mnt -o vers=3.0,guest

# smb versions below 3 do not support fallocate;
# generic/694 replaces it with pwrite.
# xfs_io -ft -c "pwrite 0 4G" /mnt/test_mnt/aaa
xfs_io -ft -c "falloc 0 4M" /mnt/test_mnt/aaa
stat -c "%b" /mnt/test_mnt/aaa
umount /mnt/test_mnt
mount -t cifs //127.0.0.1/share /mnt/test_mnt -o vers=3.0,guest
stat -c "%b" /mnt/test_mnt/aaa
Quoted 10 lines
Quoted 10 lines

Without this change, both step 3 and 6 would return 0, since even through umount/mount, the same file would remain open (since superblocks will be shared).
With this change, step 3 would return 0. Step 6 would return the right value.

If you use nosharesock even after reverting this change, you'll see the test failing.
Or even with this change if actimeo=0, then this test passes.

Quoted 9 lines
Quoted 4 lines

The real question to ask is why aren't we updating i_blocks even after sync succeeds?
My guess is that this has something to do with attribute caching when the handle is kept open.

That sounds an important bug to fix.  Glad this test showed it.

xfs_io -ft -c "falloc 0 4M" /mnt/test_mnt/aaa opens the file for writing, which causes stat -> ... -> update_inode_info to skip updating inode->i_blocks, because is_size_safe_to_change() returns false.

The file is only removed from the list in _cifsFileInfo_put(), but that call is deferred, so the value obtained in step 3 doesn't match the expected value. Step 6, however, does match — because after the remount the file has not been opened for writing.

Before the patch was merged, both runs returned 0, because the second mount reused the sb instance and the inode instance from the first mount, and the interval between the two mounts was too short, causing cifs_dentry_needs_reval() to return false.

SMB 2.1 behaves similarly: pwrite writes 4 GB and the size shows as 8388608. If the server-side backing filesystem is ext4, it also initially shows 8388608(see ext4_file_getattr()), and only during the writeback path — when it's discovered that the extent count is too large and an additional ETB(extent tree block) is required — does it become 8388616(ext4_do_writepages -> ... -> ext4_ext_new_meta_block).

Quoted 7 lines

On the client side, cifs_write_end() updates inode->i_blocks but doesn't account for that metadata block. Although generic/694 issues a sync before stat, and the server flushes to disk upon receiving it, so by rights it should return 8388616 — it doesn't. This is because cifs_revalidate_dentry_attr() -> cifs_dentry_needs_reval() returns false directly, since CIFS_CACHE_READ evaluates to true; as a result, it doesn't even go to the server to fetch stat.

Sorry, on the stable branch, cifs_write_end() is gone. It looks like the update now happens in netfs_perform_write -> netfs_update_i_size instead.

Quoted 37 lines

The oplock is only cleared to 0 in _cifsFileInfo_put(), and that itself is a deferred release, so the stat call never picks up the actual data from the remote server before _cifsFileInfo_put() called.

On the other hand, why is there no problem with cifs/smb2, and the issue only appears on smb21/smb3?

The difference is that prior to smb21, there was no concept of leases — only oplocks:

smb2_set_oplock_level
  cinode->lease_granted = false

smb21_set_oplock_level
  cinode->lease_granted = true

This means that on cifs/smb2, cifs_close() invokes _cifsFileInfo_put() immediately (rather than going through the deferred-close path), so the problem described above doesn't occur.

I'm new to SMB, so this is very much a beginner's analysis. I hope it's useful to others, and I'd really appreciate it if anyone could point out mistakes or things I've gotten wrong.

Thanks,
Zizhi Wo