[PATCH 3/8] sysfs: chain symlinks to their targets

From: Tejun Heo
Date: Thu Sep 20 2007 - 04:33:17 EST


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@xxxxxxxxx>
---
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


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/