📄 dir.c
字号:
/* * fs/sysfs/dir.c - sysfs core and dir operation implementation * * Copyright (c) 2001-3 Patrick Mochel * Copyright (c) 2007 SUSE Linux Products GmbH * Copyright (c) 2007 Tejun Heo <teheo@suse.de> * * This file is released under the GPLv2. * * Please see Documentation/filesystems/sysfs.txt for more information. */#undef DEBUG#include <linux/fs.h>#include <linux/mount.h>#include <linux/module.h>#include <linux/kobject.h>#include <linux/namei.h>#include <linux/idr.h>#include <linux/completion.h>#include <linux/mutex.h>#include "sysfs.h"DEFINE_MUTEX(sysfs_mutex);DEFINE_MUTEX(sysfs_rename_mutex);DEFINE_SPINLOCK(sysfs_assoc_lock);static DEFINE_SPINLOCK(sysfs_ino_lock);static DEFINE_IDA(sysfs_ino_ida);/** * sysfs_link_sibling - link sysfs_dirent into sibling list * @sd: sysfs_dirent of interest * * Link @sd into its sibling list which starts from * sd->s_parent->s_dir.children. * * Locking: * mutex_lock(sysfs_mutex) */static void sysfs_link_sibling(struct sysfs_dirent *sd){ struct sysfs_dirent *parent_sd = sd->s_parent; struct sysfs_dirent **pos; BUG_ON(sd->s_sibling); /* Store directory entries in order by ino. This allows * readdir to properly restart without having to add a * cursor into the s_dir.children list. */ for (pos = &parent_sd->s_dir.children; *pos; pos = &(*pos)->s_sibling) { if (sd->s_ino < (*pos)->s_ino) break; } sd->s_sibling = *pos; *pos = sd;}/** * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list * @sd: sysfs_dirent of interest * * Unlink @sd from its sibling list which starts from * sd->s_parent->s_dir.children. * * Locking: * mutex_lock(sysfs_mutex) */static void sysfs_unlink_sibling(struct sysfs_dirent *sd){ struct sysfs_dirent **pos; for (pos = &sd->s_parent->s_dir.children; *pos; pos = &(*pos)->s_sibling) { if (*pos == sd) { *pos = sd->s_sibling; sd->s_sibling = NULL; break; } }}/** * sysfs_get_dentry - get dentry for the given sysfs_dirent * @sd: sysfs_dirent of interest * * Get dentry for @sd. Dentry is looked up if currently not * present. This function descends from the root looking up * dentry for each step. * * LOCKING: * mutex_lock(sysfs_rename_mutex) * * RETURNS: * Pointer to found dentry on success, ERR_PTR() value on error. */struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd){ struct dentry *dentry = dget(sysfs_sb->s_root); while (dentry->d_fsdata != sd) { struct sysfs_dirent *cur; struct dentry *parent; /* find the first ancestor which hasn't been looked up */ cur = sd; while (cur->s_parent != dentry->d_fsdata) cur = cur->s_parent; /* look it up */ parent = dentry; mutex_lock(&parent->d_inode->i_mutex); dentry = lookup_one_noperm(cur->s_name, parent); mutex_unlock(&parent->d_inode->i_mutex); dput(parent); if (IS_ERR(dentry)) break; } return dentry;}/** * sysfs_get_active - get an active reference to sysfs_dirent * @sd: sysfs_dirent to get an active reference to * * Get an active reference of @sd. This function is noop if @sd * is NULL. * * RETURNS: * Pointer to @sd on success, NULL on failure. */static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd){ if (unlikely(!sd)) return NULL; while (1) { int v, t; v = atomic_read(&sd->s_active); if (unlikely(v < 0)) return NULL; t = atomic_cmpxchg(&sd->s_active, v, v + 1); if (likely(t == v)) return sd; if (t < 0) return NULL; cpu_relax(); }}/** * sysfs_put_active - put an active reference to sysfs_dirent * @sd: sysfs_dirent to put an active reference to * * Put an active reference to @sd. This function is noop if @sd * is NULL. */static void sysfs_put_active(struct sysfs_dirent *sd){ struct completion *cmpl; int v; if (unlikely(!sd)) return; v = atomic_dec_return(&sd->s_active); if (likely(v != SD_DEACTIVATED_BIAS)) return; /* atomic_dec_return() is a mb(), we'll always see the updated * sd->s_sibling. */ cmpl = (void *)sd->s_sibling; complete(cmpl);}/** * sysfs_get_active_two - get active references to sysfs_dirent and parent * @sd: sysfs_dirent of interest * * Get active reference to @sd and its parent. Parent's active * reference is grabbed first. This function is noop if @sd is * NULL. * * RETURNS: * Pointer to @sd on success, NULL on failure. */struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd){ if (sd) { if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent))) return NULL; if (unlikely(!sysfs_get_active(sd))) { sysfs_put_active(sd->s_parent); return NULL; } } return sd;}/** * sysfs_put_active_two - put active references to sysfs_dirent and parent * @sd: sysfs_dirent of interest * * Put active references to @sd and its parent. This function is * noop if @sd is NULL. */void sysfs_put_active_two(struct sysfs_dirent *sd){ if (sd) { sysfs_put_active(sd); sysfs_put_active(sd->s_parent); }}/** * sysfs_deactivate - deactivate sysfs_dirent * @sd: sysfs_dirent to deactivate * * Deny new active references and drain existing ones. */static void sysfs_deactivate(struct sysfs_dirent *sd){ DECLARE_COMPLETION_ONSTACK(wait); int v; BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED)); sd->s_sibling = (void *)&wait; /* atomic_add_return() is a mb(), put_active() will always see * the updated sd->s_sibling. */ v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); if (v != SD_DEACTIVATED_BIAS) wait_for_completion(&wait); sd->s_sibling = NULL;}static 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){ struct sysfs_dirent *parent_sd; repeat: /* Moving/renaming is always done while holding reference. * sd->s_parent won't change beneath us. */ parent_sd = sd->s_parent; if (sysfs_type(sd) == SYSFS_KOBJ_LINK) sysfs_put(sd->s_symlink.target_sd); if (sysfs_type(sd) & SYSFS_COPY_NAME) kfree(sd->s_name); kfree(sd->s_iattr); sysfs_free_ino(sd->s_ino); kmem_cache_free(sysfs_dir_cachep, sd); sd = parent_sd; if (sd && atomic_dec_and_test(&sd->s_count)) goto repeat;}static void sysfs_d_iput(struct dentry * dentry, struct inode * inode){ struct sysfs_dirent * sd = dentry->d_fsdata; sysfs_put(sd); iput(inode);}static struct dentry_operations sysfs_dentry_ops = { .d_iput = sysfs_d_iput,};struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type){ char *dup_name = NULL; struct sysfs_dirent *sd; if (type & SYSFS_COPY_NAME) { name = dup_name = kstrdup(name, GFP_KERNEL); if (!name) return NULL; } sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); if (!sd) goto err_out1; if (sysfs_alloc_ino(&sd->s_ino)) goto err_out2; atomic_set(&sd->s_count, 1); atomic_set(&sd->s_active, 0); sd->s_name = name; sd->s_mode = mode; sd->s_flags = type; return sd; err_out2: kmem_cache_free(sysfs_dir_cachep, sd); err_out1: kfree(dup_name); return NULL;}static int sysfs_ilookup_test(struct inode *inode, void *arg){ struct sysfs_dirent *sd = arg; return inode->i_ino == sd->s_ino;}/** * sysfs_addrm_start - prepare for sysfs_dirent add/remove * @acxt: pointer to sysfs_addrm_cxt to be used * @parent_sd: parent sysfs_dirent * * This function is called when the caller is about to add or * remove sysfs_dirent under @parent_sd. This function acquires * sysfs_mutex, grabs inode for @parent_sd if available and lock * i_mutex of it. @acxt is used to keep and pass context to * other addrm functions. * * LOCKING: * Kernel thread context (may sleep). sysfs_mutex is locked on * return. i_mutex of parent inode is locked on return if * available. */void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *parent_sd){ struct inode *inode; memset(acxt, 0, sizeof(*acxt)); acxt->parent_sd = parent_sd; /* Lookup parent inode. inode initialization and I_NEW * clearing are protected by sysfs_mutex. By grabbing it and * looking up with _nowait variant, inode state can be * determined reliably. */ mutex_lock(&sysfs_mutex); inode = ilookup5_nowait(sysfs_sb, parent_sd->s_ino, sysfs_ilookup_test, parent_sd); if (inode && !(inode->i_state & I_NEW)) { /* parent inode available */ acxt->parent_inode = inode; /* sysfs_mutex is below i_mutex in lock hierarchy. * First, trylock i_mutex. If fails, unlock * sysfs_mutex and lock them in order. */ if (!mutex_trylock(&inode->i_mutex)) { mutex_unlock(&sysfs_mutex); mutex_lock(&inode->i_mutex); mutex_lock(&sysfs_mutex); } } else iput(inode);}/** * sysfs_add_one - add sysfs_dirent to parent * @acxt: addrm context to use * @sd: sysfs_dirent to be added * * Get @acxt->parent_sd 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. * * This function should be called between calls to * sysfs_addrm_start() and sysfs_addrm_finish() and should be * passed the same @acxt as passed to sysfs_addrm_start(). * * LOCKING: * Determined by sysfs_addrm_start(). * * RETURNS: * 0 on success, -EEXIST if entry with the given name already * exists. */int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd){ if (sysfs_find_dirent(acxt->parent_sd, sd->s_name)) { printk(KERN_WARNING "sysfs: duplicate filename '%s' " "can not be created\n", sd->s_name); WARN_ON(1); return -EEXIST; } sd->s_parent = sysfs_get(acxt->parent_sd); if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) inc_nlink(acxt->parent_inode); acxt->cnt++; sysfs_link_sibling(sd); return 0;}/** * sysfs_remove_one - remove sysfs_dirent from parent * @acxt: addrm context to use * @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. * * This function should be called between calls to * sysfs_addrm_start() and sysfs_addrm_finish() and should be * passed the same @acxt as passed to sysfs_addrm_start(). * * LOCKING: * Determined by sysfs_addrm_start(). */void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd){ BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED); sysfs_unlink_sibling(sd); sd->s_flags |= SYSFS_FLAG_REMOVED; sd->s_sibling = acxt->removed; acxt->removed = sd; if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) drop_nlink(acxt->parent_inode); acxt->cnt++;}/** * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent * @sd: target sysfs_dirent * * Drop dentry for @sd. @sd must have been unlinked from its * parent on entry to this function such that it can't be looked * up anymore. */static void sysfs_drop_dentry(struct sysfs_dirent *sd){ struct inode *inode; struct dentry *dentry;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -