aboutsummaryrefslogtreecommitdiffstats
path: root/fs/sysfs/dir.c
diff options
context:
space:
mode:
authorTejun Heo <htejun@gmail.com>2007-06-11 01:04:01 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2007-06-12 19:08:47 -0400
commitdd14cbc994709a1c5a64ed3621f583c49a27e521 (patch)
treee48d38b7450661907c7b75490504c7f70b04d6cc /fs/sysfs/dir.c
parent6aa054aadfea613a437ad0b15d38eca2b963fc0a (diff)
sysfs: fix race condition around sd->s_dentry, take#2
Allowing attribute and symlink dentries to be reclaimed means sd->s_dentry can change dynamically. However, updates to the field are unsynchronized leading to race conditions. This patch adds sysfs_lock and use it to synchronize updates to sd->s_dentry. Due to the locking around ->d_iput, the check in sysfs_drop_dentry() is complex. sysfs_lock only protect sd->s_dentry pointer itself. The validity of the dentry is protected by dcache_lock, so whether dentry is alive or not can only be tested while holding both locks. This is minimal backport of sysfs_drop_dentry() rewrite in devel branch. Signed-off-by: Tejun Heo <htejun@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'fs/sysfs/dir.c')
-rw-r--r--fs/sysfs/dir.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 17a819151b91..c4342a019972 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -13,14 +13,26 @@
13#include "sysfs.h" 13#include "sysfs.h"
14 14
15DECLARE_RWSEM(sysfs_rename_sem); 15DECLARE_RWSEM(sysfs_rename_sem);
16spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
16 17
17static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) 18static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
18{ 19{
19 struct sysfs_dirent * sd = dentry->d_fsdata; 20 struct sysfs_dirent * sd = dentry->d_fsdata;
20 21
21 if (sd) { 22 if (sd) {
22 BUG_ON(sd->s_dentry != dentry); 23 /* sd->s_dentry is protected with sysfs_lock. This
23 sd->s_dentry = NULL; 24 * allows sysfs_drop_dentry() to dereference it.
25 */
26 spin_lock(&sysfs_lock);
27
28 /* The dentry might have been deleted or another
29 * lookup could have happened updating sd->s_dentry to
30 * point the new dentry. Ignore if it isn't pointing
31 * to this dentry.
32 */
33 if (sd->s_dentry == dentry)
34 sd->s_dentry = NULL;
35 spin_unlock(&sysfs_lock);
24 sysfs_put(sd); 36 sysfs_put(sd);
25 } 37 }
26 iput(inode); 38 iput(inode);
@@ -247,7 +259,10 @@ static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
247 } 259 }
248 260
249 dentry->d_fsdata = sysfs_get(sd); 261 dentry->d_fsdata = sysfs_get(sd);
262 /* protect sd->s_dentry against sysfs_d_iput */
263 spin_lock(&sysfs_lock);
250 sd->s_dentry = dentry; 264 sd->s_dentry = dentry;
265 spin_unlock(&sysfs_lock);
251 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init); 266 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
252 if (error) { 267 if (error) {
253 sysfs_put(sd); 268 sysfs_put(sd);
@@ -269,7 +284,10 @@ static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
269 int err = 0; 284 int err = 0;
270 285
271 dentry->d_fsdata = sysfs_get(sd); 286 dentry->d_fsdata = sysfs_get(sd);
287 /* protect sd->s_dentry against sysfs_d_iput */
288 spin_lock(&sysfs_lock);
272 sd->s_dentry = dentry; 289 sd->s_dentry = dentry;
290 spin_unlock(&sysfs_lock);
273 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink); 291 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
274 if (!err) { 292 if (!err) {
275 dentry->d_op = &sysfs_dentry_ops; 293 dentry->d_op = &sysfs_dentry_ops;