SMB2 CHANGE_NOTIFY feature

中文分析过程请点击这里查看

1 Requirements description

Please see GitHub issue.

2 KSMBD development environment

Install ksmbd-tools from source:

apt install -y git gcc pkgconf autoconf automake libtool make meson ninja-build gawk libnl-3-dev libnl-genl-3-dev libglib2.0-dev # debian
dnf install -y git gcc pkgconf autoconf automake libtool make meson ninja-build gawk libnl3-devel glib2-devel # fedora
git clone https://github.com/cifsd-team/ksmbd-tools.git
cd ksmbd-tools
./autogen.sh
./configure --with-rundir=/run # --prefix=/usr/local/sbin --sysconfdir=/usr/local/etc
make -j`nproc`
make install -j`nproc`

create smb user:

# We are testing in a virtual machine, so we just use the root user.
sudo ksmbd.adduser --add root

Then create config file /usr/local/etc/ksmbd/ksmbd.conf:

[global]
        writeable = yes
        public = yes

[test]
        comment = test dir
        ; Note: there should not be a space after the path.
        path = /tmp/s_test

Start ksmbd:

mkdir /tmp/s_test
systemctl stop smbd.service # stop samba on debian
systemctl stop smb.service # stop samba on fedora
chmod 777 /tmp/s_test # just for test
systemctl restart ksmbd

3 Samba development environment

Install samba from source:

git clone https://gitlab.com/samba-team/devel/samba.git 
cd samba/bootstrap/generated-dists/fedora41/ # You can replace fedora41 with your own distribution
./bootstrap.sh # It may take some time to install the dependencies
cd ../../../
./configure --with-systemd --with-libunwind
make -j`nproc`
make install -j`nproc`
export PATH=/usr/local/samba/bin/:/usr/local/samba/sbin/:$PATH

When you only want to build and install smbd and smbclient:

make -j`nproc` bin/smbd && rm -rf /usr/local/samba/sbin/smbd; cp bin/smbd /usr/local/samba/sbin/smbd
make -j`nproc` bin/smbclient && rm -rf /usr/local/samba/bin/smbclient; cp bin/smbclient /usr/local/samba/bin/smbclient

Copy smb.service:

cp ./bin/default/packaging/systemd/smb.service /usr/lib/systemd/system/smb.service

Create config file /usr/local/samba/etc/smb.conf:

[test]
    comment = test dir
    path = /tmp/s_test
    public = yes
    read only = no
    writeable = yes

Create smb user:

# We are testing in a virtual machine, so we just use the root user.
pdbedit -a -u root

Start samba:

# stop the ksmbd service before starting samba
systemctl stop ksmbd.service

mkdir /tmp/s_test
chmod 777 /tmp/s_test # just for test

systemctl restart smbd.service # debian
systemctl restart smb.service # fedora

4 Linux client environment

Please refer to Questions about SMB2 CHANGE_NOTIFY.

4.1 smbclient

Use smbclient to connect to SMB server:

# smbclient //${server_ip}/share -U USERNAME%PASSWORD
smbclient //192.168.53.210/test -U root%1

Query change notifications of root directory.

smb: \> notify /
smb: \> notify \

Query change notifications of dir/:

smb: \> notify dir
smb: \> notify /dir
smb: \> notify ./dir

Or you can enter dir/, and then query change notifications:

smb: \> cd dir
smb: \dir\> notify /

4.2 smbinfo

It is part of cifs-utils.

dnf install libtalloc-devel -y
git clone https://git.samba.org/cifs-utils.git
autoreconf -i
./configure
make -j`nproc`
# make install

4.3 libsmb2

It is unrelated to Samba or cifs.ko.

git clone https://github.com/sahlberg/libsmb2.git

4.4 ioctl

Please refer to the following two patches:

d26c2ddd3356 cifs: add SMB3 change notification support
e3e9463414f6 smb3: improve SMB3 change notification support

Build and run new-inotify-ioctl-test.c (LKML):

gcc -o new-inotify-ioctl-test new-inotify-ioctl-test.c
./new-inotify-ioctl-test /mnt
./new-inotify-ioctl-test /mnt/dir

5 Windows client environment

10.42.20.210 is the IP address of the SMB server.

Enter the following path in “File Explorer”:

# Windows is case-insensitive, so you can use either "TEST" or "test"
\\10.42.20.210\test

When switching between samba and ksmbd, Windows may fail to mount. In that case, open PowerShell on Windows and run the following commands:

# View existing connections
net use
# Delete a specific connection
net use \\10.42.20.210\IPC$ /delete
net use \\10.42.20.210\test /delete
# Delete all connections (not recommended)
net use * /delete

6 Analysis of packets captured by tcpdump

6.1 samba 20251110-2016

The full capture file can be found on GitHub (or on gitee).

Windows “File Explorer” enter root directory of smb server:

Windows “File Explorer” enter dir/:

Samba server executes touch dir/file1: