All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <htejun@gmail.com>
To: ebiederm@xmission.com, cornelia.huck@de.ibm.com, greg@kroah.com,
	stern@rowland.harvard.edu, kay.sievers@vrfy.org,
	linux-kernel@vger.kernel.org, htejun@gmail.com
Cc: Tejun Heo <htejun@gmail.com>
Subject: [PATCH 3/8] sysfs: chain symlinks to their targets
Date: Thu, 20 Sep 2007 17:31:38 +0900	[thread overview]
Message-ID: <11902770981121-git-send-email-htejun@gmail.com> (raw)
In-Reply-To: <11902770971822-git-send-email-htejun@gmail.com>

Add sd->s_dir.links and sd->s_link.next and chain symlinks to their
targets.  This will be used to implement auto-removal and auto-rename
of symlinks.

Symlinks created with kobject-based sysfs_create_symlink() won't be
chained to keep backward compatibility.

Signed-off-by: Tejun Heo <htejun@gmail.com>
---
 fs/sysfs/dir.c     |   26 ++++++++++++++++++++++++--
 fs/sysfs/symlink.c |    3 +++
 fs/sysfs/sysfs.h   |    4 ++++
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index b042a2e..bb9e87e 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -495,7 +495,8 @@ static struct inode *sysfs_addrm_get_parent_inode(struct sysfs_addrm_cxt *acxt,
  *
  *	Get @parent and set sd->s_parent to it and increment nlink of
  *	parent inode if @sd is a directory and link into the children
- *	list of the parent.
+ *	list of the parent.  If LINK_CHAINED flag is set, @sd is also
+ *	chained into the parent's s_dir.links list.
  *
  *	This function should be called between calls to
  *	sysfs_addrm_start() and sysfs_addrm_finish() and should be
@@ -522,6 +523,11 @@ int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *parent,
 
 	sysfs_link_sibling(sd);
 
+	if (sd->s_flags & SYSFS_FLAG_LINK_CHAINED) {
+		sd->s_link.next = sd->s_link.target->s_dir.links;
+		sd->s_link.target->s_dir.links = sd;
+	}
+
 	if (parent_inode) {
 		parent_inode->i_ctime = parent_inode->i_mtime = CURRENT_TIME;
 		if (sysfs_type(sd) == SYSFS_DIR)
@@ -537,7 +543,8 @@ int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *parent,
  *	@sd: sysfs_dirent to be added
  *
  *	Mark @sd removed and drop nlink of parent inode if @sd is a
- *	directory.  @sd is unlinked from the children list.
+ *	directory.  @sd is unlinked from the children list and the
+ *	s_dir.links list if applicable.
  *
  *	This function should be called between calls to
  *	sysfs_addrm_start() and sysfs_addrm_finish() and should be
@@ -554,6 +561,21 @@ void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
 
 	parent_inode = sysfs_addrm_get_parent_inode(acxt, sd->s_parent);
 
+	if (sd->s_flags & SYSFS_FLAG_LINK_CHAINED) {
+		struct sysfs_dirent *target = sd->s_link.target;
+		struct sysfs_dirent **p;
+		int removed = 0;
+
+		for (p = &target->s_dir.links; *p; p = &(*p)->s_link.next) {
+			if (*p == sd) {
+				*p = sd->s_link.next;
+				removed = 1;
+				break;
+			}
+		}
+		BUG_ON(!removed);
+	}
+
 	sysfs_unlink_sibling(sd);
 
 	sd->s_flags |= SYSFS_FLAG_REMOVED;
diff --git a/fs/sysfs/symlink.c b/fs/sysfs/symlink.c
index 296fef5..53cc8a6 100644
--- a/fs/sysfs/symlink.c
+++ b/fs/sysfs/symlink.c
@@ -231,6 +231,9 @@ struct sysfs_dirent *__sysfs_add_link(struct sysfs_dirent *parent,
 		goto err;
 
 	if (format) {
+		/* chain into the target's links list */
+		flags |= SYSFS_FLAG_LINK_CHAINED;
+
 		/* SYSFS_COPY_NAME means 'copy the format string' */
 		rc = -ENOMEM;
 		if (mode & SYSFS_COPY_NAME) {
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index 9167032..f704279 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -5,10 +5,13 @@ struct sysfs_elem_dir {
 	void			*data;
 	/* children list starts here and goes through sd->s_sibling */
 	struct sysfs_dirent	*children;
+	/* symlinks list starts here and goes through sd->s_link.next */
+	struct sysfs_dirent	*links;
 };
 
 struct sysfs_elem_link {
 	struct sysfs_dirent	*target;
+	struct sysfs_dirent	*next;
 	const char		*name_fmt;
 };
 
@@ -64,6 +67,7 @@ struct sysfs_dirent {
 #define SYSFS_FLAG_REMOVED		0x0200
 #define SYSFS_FLAG_NAME_COPIED		0x0400
 #define SYSFS_FLAG_LINK_NAME_FMT_COPIED	0x0800
+#define SYSFS_FLAG_LINK_CHAINED		0x1000
 
 static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
 {
-- 
1.5.0.3



  parent reply	other threads:[~2007-09-20  8:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-20  8:31 [PATCHSET 4/4] sysfs: implement new features Tejun Heo
2007-09-20  8:31 ` [PATCH 1/8] sysfs: notify file on deactivation Tejun Heo
2007-09-20  8:31 ` [PATCH 2/8] sysfs: add name formatting support for symlinks Tejun Heo
2007-09-20  8:31 ` [PATCH 4/8] sysfs: implement symlink auto-removal Tejun Heo
2007-09-20  8:31 ` [PATCH 5/8] sysfs: implement symlink auto-rename Tejun Heo
2007-09-20  8:31 ` [PATCH 6/8] sysfs: implement plugged creation of sysfs nodes Tejun Heo
2007-09-20  8:31 ` Tejun Heo [this message]
2007-09-20  8:31 ` [PATCH 7/8] sysfs: implement batch error handling Tejun Heo
2007-09-20  8:31 ` [PATCH 8/8] sysfs: add copyrights Tejun Heo
2007-09-25 22:50 ` [PATCHSET 4/4] sysfs: implement new features Greg KH
2007-09-27 12:24   ` Tejun Heo
2007-09-27 22:38   ` Kyle Moffett

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=11902770981121-git-send-email-htejun@gmail.com \
    --to=htejun@gmail.com \
    --cc=cornelia.huck@de.ibm.com \
    --cc=ebiederm@xmission.com \
    --cc=greg@kroah.com \
    --cc=kay.sievers@vrfy.org \
    --cc=linux-kernel@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.