public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <htejun@gmail.com>
To: gregkh@suse.de, dmitry.torokhov@gmail.com,
	cornelia.huck@de.ibm.com, oneukum@suse.de, rpurdie@rpsys.net,
	stern@rowland.harvard.edu, maneesh@in.ibm.com,
	akpm@linux-foundation.org, linux-kernel@vger.kernel.org,
	htejun@gmail.com
Cc: Tejun Heo <htejun@gmail.com>
Subject: [PATCH 05/21] sysfs: allocate inode number using ida
Date: Sat, 28 Apr 2007 22:39:40 +0900	[thread overview]
Message-ID: <117776758093-git-send-email-htejun@gmail.com> (raw)
In-Reply-To: <11777675753460-git-send-email-htejun@gmail.com>

Inode number handling was incorrect in two ways.

1. sysfs uses the inode number allocated by new_inode() and never
   hashes it.  When reporting the inode number, it uses iunique() if
   inode is inaccessible.  This is incorrect because iunique() assumes
   the inodes are hashed.  This can cause duplicate inode numbers and
   the condition is likely to happen because new_inode() and iunique()
   use separate increasing static counters to scan for empty slot.

2. sysfs_dirent->s_dentry can go away anytime and can't be referenced
   unless the caller knows the dentry is not and not going to be
   deleted.

This patch adds sysfs_dirent->s_ino and uses ida to give each sd a
unique inode number on allocation which is freed when the sd is
released.

Signed-off-by: Tejun Heo <htejun@gmail.com>
---
 fs/sysfs/dir.c   |   43 ++++++++++++++++++++++++++++++++++++++-----
 fs/sysfs/inode.c |    1 +
 fs/sysfs/mount.c |    1 +
 fs/sysfs/sysfs.h |    1 +
 4 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 4dc4322..ada8405 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -9,11 +9,41 @@
 #include <linux/module.h>
 #include <linux/kobject.h>
 #include <linux/namei.h>
+#include <linux/idr.h>
 #include <asm/semaphore.h>
 #include "sysfs.h"
 
 DECLARE_RWSEM(sysfs_rename_sem);
 
+static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_IDA(sysfs_ino_ida);
+
+int sysfs_alloc_ino(ino_t *pino)
+{
+	int ino, rc;
+
+ retry:
+	spin_lock(&sysfs_ino_lock);
+	rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
+	spin_unlock(&sysfs_ino_lock);
+
+	if (rc == -EAGAIN) {
+		if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
+			goto retry;
+		rc = -ENOMEM;
+	}
+
+	*pino = ino;
+	return rc;
+}
+
+static void sysfs_free_ino(ino_t ino)
+{
+	spin_lock(&sysfs_ino_lock);
+	ida_remove(&sysfs_ino_ida, ino);
+	spin_unlock(&sysfs_ino_lock);
+}
+
 void release_sysfs_dirent(struct sysfs_dirent * sd)
 {
 	if (sd->s_type & SYSFS_KOBJ_LINK) {
@@ -23,6 +53,7 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
 		kfree(sl);
 	}
 	kfree(sd->s_iattr);
+	sysfs_free_ino(sd->s_ino);
 	kmem_cache_free(sysfs_dir_cachep, sd);
 }
 
@@ -53,6 +84,11 @@ static struct sysfs_dirent * __sysfs_new_dirent(void * element)
 	if (!sd)
 		return NULL;
 
+	if (sysfs_alloc_ino(&sd->s_ino)) {
+		kmem_cache_free(sysfs_dir_cachep, sd);
+		return NULL;
+	}
+
 	atomic_set(&sd->s_count, 1);
 	atomic_set(&sd->s_event, 1);
 	INIT_LIST_HEAD(&sd->s_children);
@@ -521,7 +557,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 
 	switch (i) {
 		case 0:
-			ino = dentry->d_inode->i_ino;
+			ino = parent_sd->s_ino;
 			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
 				break;
 			filp->f_pos++;
@@ -550,10 +586,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 
 				name = sysfs_get_name(next);
 				len = strlen(name);
-				if (next->s_dentry)
-					ino = next->s_dentry->d_inode->i_ino;
-				else
-					ino = iunique(sysfs_sb, 2);
+				ino = next->s_ino;
 
 				if (filldir(dirent, name, len, filp->f_pos, ino,
 						 dt_type(next)) < 0)
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c
index 4de5c6b..7c1f4e8 100644
--- a/fs/sysfs/inode.c
+++ b/fs/sysfs/inode.c
@@ -140,6 +140,7 @@ struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd)
 		inode->i_mapping->a_ops = &sysfs_aops;
 		inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
 		inode->i_op = &sysfs_inode_operations;
+		inode->i_ino = sd->s_ino;
 		lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
 
 		if (sd->s_iattr) {
diff --git a/fs/sysfs/mount.c b/fs/sysfs/mount.c
index 23a48a3..c1bafef 100644
--- a/fs/sysfs/mount.c
+++ b/fs/sysfs/mount.c
@@ -32,6 +32,7 @@ static struct sysfs_dirent sysfs_root = {
 	.s_children	= LIST_HEAD_INIT(sysfs_root.s_children),
 	.s_element	= NULL,
 	.s_type		= SYSFS_ROOT,
+	.s_ino		= 1,
 	.s_iattr	= NULL,
 };
 
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index 3b8aae0..1d61027 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -5,6 +5,7 @@ struct sysfs_dirent {
 	void 			* s_element;
 	int			s_type;
 	umode_t			s_mode;
+	ino_t			s_ino;
 	struct dentry		* s_dentry;
 	struct iattr		* s_iattr;
 	atomic_t		s_event;
-- 
1.5.0.3



  parent reply	other threads:[~2007-04-28 13:43 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-28 13:39 [PATCHSET] sysfs: sysfs rework, take #2 Tejun Heo
2007-04-28 13:39 ` [PATCH 01/21] idr: fix obscure bug in allocation path Tejun Heo
2007-04-28 13:39 ` [PATCH 02/21] idr: separate out idr_mark_full() Tejun Heo
2007-04-28 13:39 ` Tejun Heo [this message]
2007-04-28 13:39 ` [PATCH 04/21] sysfs: move release_sysfs_dirent() to dir.c Tejun Heo
2007-04-28 13:39 ` [PATCH 07/21] sysfs: fix error handling in binattr write() Tejun Heo
2007-04-28 13:39 ` [PATCH 06/21] sysfs: make sysfs_put() ignore NULL sd Tejun Heo
2007-04-28 13:39 ` [PATCH 03/21] ida: implement idr based id allocator Tejun Heo
2007-04-28 13:39 ` [PATCH 10/21] sysfs: consolidate sysfs_dirent creation functions Tejun Heo
2007-04-28 13:39 ` [PATCH 09/21] sysfs: flatten and fix sysfs_rename_dir() error handling Tejun Heo
2007-04-28 13:39 ` [PATCH 12/21] sysfs: add sysfs_dirent->s_name Tejun Heo
2007-04-28 13:39 ` [PATCH 08/21] sysfs: flatten cleanup paths in sysfs_add_link() and create_dir() Tejun Heo
2007-04-28 13:39 ` [PATCH 11/21] sysfs: add sysfs_dirent->s_parent Tejun Heo
2007-04-28 13:39 ` [PATCH 17/21] sysfs: implement sysfs_dirent active reference and immediate disconnect Tejun Heo
2007-04-28 13:39 ` [PATCH 19/21] sysfs: kill unnecessary attribute->owner Tejun Heo
2007-04-28 13:39 ` [PATCH 18/21] sysfs: kill attribute file orphaning Tejun Heo
2007-04-28 13:39 ` [PATCH 15/21] sysfs: reimplement symlink using sysfs_dirent tree Tejun Heo
2007-04-28 13:39 ` [PATCH 13/21] sysfs: make sysfs_dirent->s_element a union Tejun Heo
2007-04-28 13:39 ` [PATCH 20/21] sysfs: separate out sysfs_attach_dentry() Tejun Heo
2007-04-28 13:39 ` [PATCH 14/21] sysfs: implement kobj_sysfs_assoc_lock Tejun Heo
2007-04-28 13:39 ` [PATCH 16/21] sysfs: implement bin_buffer Tejun Heo
2007-05-01 14:25   ` Satyam Sharma
     [not found]     ` <463753DB.2060101@gmail.com>
2007-05-01 19:37       ` Satyam Sharma
2007-05-01 20:04         ` Satyam Sharma
2007-05-02 11:29           ` Tejun Heo
2007-04-28 13:39 ` [PATCH 21/21] sysfs: reimplement syfs_drop_dentry() Tejun Heo
2007-04-28 14:28 ` [PATCHSET] sysfs: sysfs rework, take #2 Alan Stern
2007-04-28 16:06   ` Tejun Heo
2007-05-02 12:38 ` Cornelia Huck
2007-05-02 13:04   ` Tejun Heo

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=117776758093-git-send-email-htejun@gmail.com \
    --to=htejun@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=cornelia.huck@de.ibm.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=gregkh@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maneesh@in.ibm.com \
    --cc=oneukum@suse.de \
    --cc=rpurdie@rpsys.net \
    --cc=stern@rowland.harvard.edu \
    /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