Skip to contentSkip to conversation

Questions about SMB2 CHANGE_NOTIFY

DISCUSSION ChenXiaoSong 2 participants 4 messages Dec 15, 2025 – Dec 16, 2025

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

Hi,

I am currently using the following two methods to query a directory for change notifications on the client side:

- smbclient:
https://chenxiaosong.com/en/smb2-change-notify.html#linux-client-env
- Windows "File Explorer":
https://chenxiaosong.com/en/smb2-change-notify.html#win-client-env

Are there any better tools to print detailed info about change notifications on the client side, or any userspace C program examples (using ioctl) available?

Thanks,
ChenXiaoSong.

#1 lore raw

Steve has already told me that I can use smbinfo (which is part of cifs-utils) and libsmb2 (which is unrelated to Samba or cifs.ko).”

Thanks,
ChenXiaoSong.

Quoted 14 lines · ChenXiaoSong

Hi,

I am currently using the following two methods to query a directory for change notifications on the client side:

  - smbclient: https://chenxiaosong.com/en/smb2-change-
notify.html#linux-client-env
  - Windows "File Explorer": https://chenxiaosong.com/en/smb2-change-
notify.html#win-client-env

Are there any better tools to print detailed info about change notifications on the client side, or any userspace C program examples (using ioctl) available?

Thanks,
ChenXiaoSong.

#2 lore raw

I had posted a sample program for using the change notify ioctl on an smb3.1.1 mount (see attached). It is fairly easy to write test programs to use these - see e.g. the older simply notify on Linux cifs.ko - IIRC something like this should work. We could really use someone to add a notify tool in cifs-utils (e.g. in smbinfo tool).

import fcntl

# constant for sizeof(int)
INT = 4

CIFS_IOCTL_MAGIC = 0xCF
CIFS_IOC_NOTIFY = 0x4005cf09

print("CIFS_IOCTL_MAGIC:", CIFS_IOCTL_MAGIC)
print("CIFS_IOC_NOTIFY:", CIFS_IOC_NOTIFY)

# then change some files you don't mind destroying
fd = open('/mnt1/somefile','rb')

ret = fcntl.ioctl(fd.fileno(), CIFS_IOC_NOTIFY, fd.fileno()) print("Exiting: ", ret);

e.g. notice the original commit message, and then the second one which was added later to allow returning what was changed not just that there was a change to that directory:

Author: Steve French <[email protected]>
Date:   Thu Feb 6 06:00:14 2020 -0600

cifs: add SMB3 change notification support

A commonly used SMB3 feature is change notification, allowing an app to be notified about changes to a directory. The SMB3 Notify request blocks until the server detects a change to that directory or its contents that matches the completion flags that were passed in and the "watch_tree" flag (which indicates whether subdirectories under this directory should be also included). See MS-SMB2 2.2.35 for additional detail.

To use this simply pass in the following structure to ioctl:

struct __attribute__((__packed__)) smb3_notify {
uint32_t completion_filter;
bool watch_tree;
} __packed;

using CIFS_IOC_NOTIFY 0x4005cf09
or equivalently _IOW(CIFS_IOCTL_MAGIC, 9, struct smb3_notify)

SMB3 change notification is supported by all major servers. The ioctl will block until the server detects a change to that directory or its subdirectories (if watch_tree is set).

commit e3e9463414f610e91528f2b920b8cb655f4bae33 (tag:
6.1-rc-smb3-client-fixes-part2)

Author: Steve French <[email protected]>
Date:   Sat Oct 15 00:43:22 2022 -0500

smb3: improve SMB3 change notification support

Change notification is a commonly supported feature by most servers, but the current ioctl to request notification when a directory is changed does not return the information about what changed (even though it is returned by the server in the SMB3 change notify response), it simply returns when there is a change.

This ioctl improves upon CIFS_IOC_NOTIFY by returning the notify information structure which includes the name of the file(s) that changed and why. See MS-SMB2 2.2.35 for details on the individual filter flags and the file_notify_information structure returned.

To use this simply pass in the following (with enough space to fit at least one file_notify_information structure)

struct __attribute__((__packed__)) smb3_notify {
uint32_t completion_filter;
bool watch_tree;
uint32_t data_len;
uint8_t data[];
} __packed;

using CIFS_IOC_NOTIFY_INFO 0xc009cf0b
or equivalently _IOWR(CIFS_IOCTL_MAGIC, 11, struct smb3_notify_info)

The ioctl will block until the server detects a change to that directory or its subdirectories (if watch_tree is set).

Quoted 26 lines · ChenXiaoSong

Steve has already told me that I can use smbinfo (which is part of cifs-utils) and libsmb2 (which is unrelated to Samba or cifs.ko).”

Thanks,
ChenXiaoSong.

Quoted 17 lines · ChenXiaoSong

Hi,

I am currently using the following two methods to query a directory for change notifications on the client side:

- smbclient: https://chenxiaosong.com/en/smb2-change-
notify.html#linux-client-env
- Windows "File Explorer": https://chenxiaosong.com/en/smb2-change-
notify.html#win-client-env

Are there any better tools to print detailed info about change notifications on the client side, or any userspace C program examples (using ioctl) available?

Thanks,
ChenXiaoSong.

Signature · 3 lines
Thanks,

Steve

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdbool.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

void dumpmem(FILE *out, const void *ptr, const size_t size)

{
    const size_t BYTES_PER_LINE = 16;
    size_t offset, read;

    uint8_t *p = (uint8_t *)ptr;
    const uint8_t *maxp = (p + size);

    if (out == NULL || ptr == NULL || size == 0)
    {
        return;
    }

    for (offset = read = 0; offset != size; offset += read)
    {
        uint8_t buf[BYTES_PER_LINE];

        for (read = 0; read != BYTES_PER_LINE && (&p[offset + read]) < maxp; read++)
        {
            buf[read] = p[offset + read];
        }

        if (read == 0)
            return;

        fprintf(out, "%.8x: ", (unsigned int)offset);

        /* raw data */
        for (size_t i = 0; i < read; i++)
        {
            fprintf(out, " %.2x", buf[i]);
            if (BYTES_PER_LINE > 8 && BYTES_PER_LINE % 2 == 0 && i == (BYTES_PER_LINE / 2 - 1))
                fprintf(out, " ");
        }

        /* ASCII */
        if (read < BYTES_PER_LINE)
        {
            for (size_t i = read; i < BYTES_PER_LINE; i++)
            {
                fprintf(out, "  ");
                fprintf(out, " ");
                if (BYTES_PER_LINE > 8 && BYTES_PER_LINE % 2 == 0 && i == (BYTES_PER_LINE / 2 - 1))
                    fprintf(out, " ");
            }
        }
        fprintf(out, " ");
        for (size_t i = 0; i < read; i++)
        {
            if (buf[i] <= 31 || buf[i] >= 127) /* ignore control and non-ASCII characters */
                fprintf(out, ".");
            else
                fprintf(out, "%c", buf[i]);
        }

        fprintf(out, "\n");
    }
}

/* See MS-SMB2 2.2.35 for a definition of the individual filter flags */

struct __attribute__((__packed__)) smb3_notify {
       uint32_t completion_filter;
       bool	watch_tree;
       uint32_t data_len;
       uint8_t	data[];
} __packed;

#define CIFS_IOC_NOTIFY 0x4005cf09 /* previous ioctl which simply returns when changes occur */ #define CIFS_IOC_NOTIFY_INFO 0xc009cf0b /* new ioctl for change notification */ int main(int argc, char **argv)

{
	struct smb3_notify *pnotify;
	int f, g;

	if ((f = open(argv[1], O_RDONLY)) < 0) {
		fprintf(stderr, "Failed to open %s\n", argv[1]);
		exit(1);
	}

	pnotify = malloc(sizeof(struct smb3_notify) + 200);
	memset(pnotify, 0, sizeof(struct smb3_notify) + 200);

	pnotify->watch_tree = false;
	pnotify->completion_filter = 0xFFF;
	pnotify->data_len = 200;

	if (ioctl(f, CIFS_IOC_NOTIFY_INFO, pnotify) < 0)
		printf("Error %d returned from ioctl\n", errno);
	else {
		printf("notify completed. returned data size is %d\n", pnotify->data_len);
		dumpmem(stdout, pnotify->data, pnotify->data_len);
	}
}
#3 lore raw

Thanks for your reply. This is very helpful for my development of ksmbd change notify.

I will also try to add a notify tool to cifs-utils.

Thanks,
ChenXiaoSong.

Quoted 121 lines · Steve French

I had posted a sample program for using the change notify ioctl on an smb3.1.1 mount (see attached). It is fairly easy to write test programs to use these - see e.g. the older simply notify on Linux cifs.ko - IIRC something like this should work. We could really use someone to add a notify tool in cifs-utils (e.g. in smbinfo tool).

import fcntl

# constant for sizeof(int)
INT = 4

CIFS_IOCTL_MAGIC = 0xCF
CIFS_IOC_NOTIFY = 0x4005cf09

print("CIFS_IOCTL_MAGIC:", CIFS_IOCTL_MAGIC)
print("CIFS_IOC_NOTIFY:", CIFS_IOC_NOTIFY)

# then change some files you don't mind destroying
fd = open('/mnt1/somefile','rb')

ret = fcntl.ioctl(fd.fileno(), CIFS_IOC_NOTIFY, fd.fileno()) print("Exiting: ", ret);

e.g. notice the original commit message, and then the second one which was added later to allow returning what was changed not just that there was a change to that directory:

Author: Steve French <[email protected]>
Date:   Thu Feb 6 06:00:14 2020 -0600

cifs: add SMB3 change notification support

A commonly used SMB3 feature is change notification, allowing an app to be notified about changes to a directory. The SMB3 Notify request blocks until the server detects a change to that directory or its contents that matches the completion flags that were passed in and the "watch_tree" flag (which indicates whether subdirectories under this directory should be also included). See MS-SMB2 2.2.35 for additional detail.

To use this simply pass in the following structure to ioctl:

struct __attribute__((__packed__)) smb3_notify {
uint32_t completion_filter;
bool watch_tree;
} __packed;

using CIFS_IOC_NOTIFY 0x4005cf09
or equivalently _IOW(CIFS_IOCTL_MAGIC, 9, struct smb3_notify)

SMB3 change notification is supported by all major servers. The ioctl will block until the server detects a change to that directory or its subdirectories (if watch_tree is set).

commit e3e9463414f610e91528f2b920b8cb655f4bae33 (tag:
6.1-rc-smb3-client-fixes-part2)

Author: Steve French <[email protected]>
Date:   Sat Oct 15 00:43:22 2022 -0500

smb3: improve SMB3 change notification support

Change notification is a commonly supported feature by most servers, but the current ioctl to request notification when a directory is changed does not return the information about what changed (even though it is returned by the server in the SMB3 change notify response), it simply returns when there is a change.

This ioctl improves upon CIFS_IOC_NOTIFY by returning the notify information structure which includes the name of the file(s) that changed and why. See MS-SMB2 2.2.35 for details on the individual filter flags and the file_notify_information structure returned.

To use this simply pass in the following (with enough space to fit at least one file_notify_information structure)

struct __attribute__((__packed__)) smb3_notify {
uint32_t completion_filter;
bool watch_tree;
uint32_t data_len;
uint8_t data[];
} __packed;

using CIFS_IOC_NOTIFY_INFO 0xc009cf0b
or equivalently _IOWR(CIFS_IOCTL_MAGIC, 11, struct smb3_notify_info)

The ioctl will block until the server detects a change to that directory or its subdirectories (if watch_tree is set).

Quoted 26 lines · ChenXiaoSong

Steve has already told me that I can use smbinfo (which is part of cifs-utils) and libsmb2 (which is unrelated to Samba or cifs.ko).”

Thanks,
ChenXiaoSong.

Quoted 17 lines · ChenXiaoSong

Hi,

I am currently using the following two methods to query a directory for change notifications on the client side:

- smbclient: https://chenxiaosong.com/en/smb2-change-
notify.html#linux-client-env
- Windows "File Explorer": https://chenxiaosong.com/en/smb2-change-
notify.html#win-client-env

Are there any better tools to print detailed info about change notifications on the client side, or any userspace C program examples (using ioctl) available?

Thanks,
ChenXiaoSong.