2 * Directory notifications for Linux.
4 * Copyright (C) 2000 Stephen Rothwell
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
17 #include <linux/sched.h>
18 #include <linux/dnotify.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
23 extern void send_sigio(struct fown_struct
*fown
, int fd
, int band
);
25 int dir_notify_enable
= 1;
27 static rwlock_t dn_lock
= RW_LOCK_UNLOCKED
;
28 static kmem_cache_t
*dn_cache
;
30 static void redo_inode_mask(struct inode
*inode
)
32 unsigned long new_mask
;
33 struct dnotify_struct
*dn
;
36 for (dn
= inode
->i_dnotify
; dn
!= NULL
; dn
= dn
->dn_next
)
37 new_mask
|= dn
->dn_mask
& ~DN_MULTISHOT
;
38 inode
->i_dnotify_mask
= new_mask
;
41 int fcntl_dirnotify(int fd
, struct file
*filp
, unsigned long arg
)
43 struct dnotify_struct
*dn
= NULL
;
44 struct dnotify_struct
*odn
;
45 struct dnotify_struct
**prev
;
47 int turning_off
= (arg
& ~DN_MULTISHOT
) == 0;
49 if (!turning_off
&& !dir_notify_enable
)
51 inode
= filp
->f_dentry
->d_inode
;
52 if (!S_ISDIR(inode
->i_mode
))
55 dn
= kmem_cache_alloc(dn_cache
, SLAB_KERNEL
);
60 prev
= &inode
->i_dnotify
;
61 for (odn
= *prev
; odn
!= NULL
; prev
= &odn
->dn_next
, odn
= *prev
)
62 if (odn
->dn_filp
== filp
)
67 redo_inode_mask(inode
);
73 inode
->i_dnotify_mask
|= arg
& ~DN_MULTISHOT
;
78 filp
->f_owner
.pid
= current
->pid
;
79 filp
->f_owner
.uid
= current
->uid
;
80 filp
->f_owner
.euid
= current
->euid
;
81 dn
->dn_magic
= DNOTIFY_MAGIC
;
85 inode
->i_dnotify_mask
|= arg
& ~DN_MULTISHOT
;
86 dn
->dn_next
= inode
->i_dnotify
;
87 inode
->i_dnotify
= dn
;
89 write_unlock(&dn_lock
);
92 kmem_cache_free(dn_cache
, dn
);
96 void __inode_dir_notify(struct inode
*inode
, unsigned long event
)
98 struct dnotify_struct
* dn
;
99 struct dnotify_struct
**prev
;
100 struct fown_struct
* fown
;
103 write_lock(&dn_lock
);
104 prev
= &inode
->i_dnotify
;
105 while ((dn
= *prev
) != NULL
) {
106 if (dn
->dn_magic
!= DNOTIFY_MAGIC
) {
107 printk(KERN_ERR
"__inode_dir_notify: bad magic "
108 "number in dnotify_struct!\n");
111 if ((dn
->dn_mask
& event
) == 0) {
115 fown
= &dn
->dn_filp
->f_owner
;
117 send_sigio(fown
, dn
->dn_fd
, POLL_MSG
);
118 if (dn
->dn_mask
& DN_MULTISHOT
)
123 kmem_cache_free(dn_cache
, dn
);
127 redo_inode_mask(inode
);
129 write_unlock(&dn_lock
);
132 static int __init
dnotify_init(void)
134 dn_cache
= kmem_cache_create("dnotify cache",
135 sizeof(struct dnotify_struct
), 0, 0, NULL
, NULL
);
137 panic("cannot create dnotify slab cache");
141 module_init(dnotify_init
)