linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ioannis Zarkadas <iz2175@columbia.edu>
To: linux-fsdevel@vger.kernel.org
Subject: fsnotify: Question on proper use
Date: Fri, 17 Mar 2023 01:31:24 -0400	[thread overview]
Message-ID: <CADjSRtSFavkO4W7wbWiqmiyef82NZ=33e7rx=yGWt+ja830TPQ@mail.gmail.com> (raw)

Hi everyone!

I'm developing a kernel module and trying to setup a directory watch
with fsnotify.
I mainly copied existing code paths in the kernel that I found,
because I couldn't
find any usage documentation.

My issue is the following:
- Setting up the watch works initially.
- If I remove and reinsert the kernel module, fsnotify_add_inode_mark
fails with EEXIST.

So I must be doing something wrong. I am using the put/destroy methods
for the mark
and the put method for the group when unloading the module.

Here is how I setup the watch:

> static struct fsnotify_group *group;
> static struct fsnotify_mark mark;
>
> static int setup_sync_dir_watch(char *sync_dir) {
>     int ret;
>     struct fsnotify_mark *old_mark;
>     struct path sync_dir_path;
>
>     pr_info("%s: Syncing extents for files under '%s'\n", MODULE_NAME,
>             sync_dir);
>
>     group = fsnotify_alloc_group(&nvmeof_xrp_fsnotify_ops);
>     if (IS_ERR(group)) {
>         pr_err("%s: Error allocating fsnotify group!\n", MODULE_NAME);
>         return -1;
>     }
>     ret = kern_path(sync_dir, LOOKUP_FOLLOW, &sync_dir_path);
>     if (ret) {
>         pr_err("%s: Error getting kernel path: %d!\n", MODULE_NAME, ret);
>         goto release_group;
>     }
>     fsnotify_init_mark(&mark, group);
>     mark.mask = FS_CREATE | FS_DELETE | FS_MODIFY |
>                                  FS_CLOSE_WRITE | FS_EVENT_ON_CHILD;
>     ret = fsnotify_add_inode_mark(&mark,
>                                   sync_dir_path.dentry->d_inode, 0);
>     path_put(&sync_dir_path);
>     if (ret) {
>         pr_err("%s: Error adding fsnotify mark! Error code: %d\n", MODULE_NAME,
>                ret);
>         goto release_mark;
>     }
>     return 0;
> release_mark:
>     fsnotify_destroy_mark(&mark, group);
>     fsnotify_put_mark(&mark);
> release_group:
>     fsnotify_put_group(group);
>     return ret;
> }

And here is how I clear it when exiting the module:

> static void __exit module_exit(void) {
>     fsnotify_destroy_mark(&mark, group);
>     fsnotify_put_mark(&mark);
>     fsnotify_put_group(group);

I also tried to find the mark and delete it, but it returns NULL even though
fsnotify_add_inode_mark returns EEXISTS:

> mutex_lock(&group->mark_mutex);
> old_mark = fsnotify_find_mark(
>     &sync_dir_path.dentry->d_inode->i_fsnotify_marks,
>     group);
> if (old_mark != NULL) {
>     pr_info("%s: Found old mark, destroying it...\n", MODULE_NAME);
>     fsnotify_destroy_mark(old_mark, group);
>     fsnotify_put_mark(old_mark);
> }
> mutex_unlock(&group->mark_mutex);

Thanks in advance,
Yannis

                 reply	other threads:[~2023-03-17  5:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADjSRtSFavkO4W7wbWiqmiyef82NZ=33e7rx=yGWt+ja830TPQ@mail.gmail.com' \
    --to=iz2175@columbia.edu \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).