2 * Directory notifications for Linux.
4 * Copyright (C) 2000,2001,2002 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 void dnotify_flush(struct file
*filp
, fl_owner_t id
)
43 struct dnotify_struct
*dn
;
44 struct dnotify_struct
**prev
;
47 inode
= filp
->f_dentry
->d_inode
;
48 if (!S_ISDIR(inode
->i_mode
))
51 prev
= &inode
->i_dnotify
;
52 while ((dn
= *prev
) != NULL
) {
53 if ((dn
->dn_owner
== id
) && (dn
->dn_filp
== filp
)) {
55 redo_inode_mask(inode
);
56 kmem_cache_free(dn_cache
, dn
);
61 write_unlock(&dn_lock
);
64 int fcntl_dirnotify(int fd
, struct file
*filp
, unsigned long arg
)
66 struct dnotify_struct
*dn
;
67 struct dnotify_struct
*odn
;
68 struct dnotify_struct
**prev
;
70 fl_owner_t id
= current
->files
;
73 if ((arg
& ~DN_MULTISHOT
) == 0) {
74 dnotify_flush(filp
, id
);
77 if (!dir_notify_enable
)
79 inode
= filp
->f_dentry
->d_inode
;
80 if (!S_ISDIR(inode
->i_mode
))
82 dn
= kmem_cache_alloc(dn_cache
, SLAB_KERNEL
);
86 prev
= &inode
->i_dnotify
;
87 while ((odn
= *prev
) != NULL
) {
88 if ((odn
->dn_owner
== id
) && (odn
->dn_filp
== filp
)) {
91 inode
->i_dnotify_mask
|= arg
& ~DN_MULTISHOT
;
97 error
= f_setown(filp
, current
->pid
, 1);
105 inode
->i_dnotify_mask
|= arg
& ~DN_MULTISHOT
;
106 dn
->dn_next
= inode
->i_dnotify
;
107 inode
->i_dnotify
= dn
;
109 write_unlock(&dn_lock
);
112 kmem_cache_free(dn_cache
, dn
);
116 void __inode_dir_notify(struct inode
*inode
, unsigned long event
)
118 struct dnotify_struct
* dn
;
119 struct dnotify_struct
**prev
;
120 struct fown_struct
* fown
;
123 write_lock(&dn_lock
);
124 prev
= &inode
->i_dnotify
;
125 while ((dn
= *prev
) != NULL
) {
126 if ((dn
->dn_mask
& event
) == 0) {
130 fown
= &dn
->dn_filp
->f_owner
;
131 send_sigio(fown
, dn
->dn_fd
, POLL_MSG
);
132 if (dn
->dn_mask
& DN_MULTISHOT
)
137 kmem_cache_free(dn_cache
, dn
);
141 redo_inode_mask(inode
);
142 write_unlock(&dn_lock
);
146 * This is hopelessly wrong, but unfixable without API changes. At
147 * least it doesn't oops the kernel...
149 * To safely access ->d_parent we need to keep d_move away from it. Use the
150 * dentry's d_lock for this.
152 void dnotify_parent(struct dentry
*dentry
, unsigned long event
)
154 struct dentry
*parent
;
156 spin_lock(&dentry
->d_lock
);
157 parent
= dentry
->d_parent
;
158 if (parent
->d_inode
->i_dnotify_mask
& event
) {
160 spin_unlock(&dentry
->d_lock
);
161 __inode_dir_notify(parent
->d_inode
, event
);
164 spin_unlock(&dentry
->d_lock
);
168 static int __init
dnotify_init(void)
170 dn_cache
= kmem_cache_create("dnotify_cache",
171 sizeof(struct dnotify_struct
), 0, 0, NULL
, NULL
);
173 panic("cannot create dnotify slab cache");
177 module_init(dnotify_init
)