dir.c 3.1 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
T
Tejun Heo 已提交
2 3 4 5 6 7 8 9 10
 * 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.
L
Linus Torvalds 已提交
11 12 13 14 15 16
 */

#undef DEBUG

#include <linux/fs.h>
#include <linux/kobject.h>
17
#include <linux/slab.h>
L
Linus Torvalds 已提交
18 19
#include "sysfs.h"

20
DEFINE_SPINLOCK(sysfs_symlink_target_lock);
L
Linus Torvalds 已提交
21

22
void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
23
{
24
	char *buf, *path = NULL;
25

26 27 28
	buf = kzalloc(PATH_MAX, GFP_KERNEL);
	if (buf)
		path = kernfs_path(parent, buf, PATH_MAX);
29

30 31
	WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s/%s'\n",
	     path, name);
32

33
	kfree(buf);
34 35
}

L
Linus Torvalds 已提交
36
/**
37 38 39
 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
 * @kobj: object we're creating directory for
 * @ns: the namespace tag to use
L
Linus Torvalds 已提交
40
 */
41
int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
L
Linus Torvalds 已提交
42
{
43
	struct kernfs_node *parent, *kn;
L
Linus Torvalds 已提交
44 45 46

	BUG_ON(!kobj);

47
	if (kobj->parent)
48
		parent = kobj->parent->sd;
L
Linus Torvalds 已提交
49
	else
50
		parent = sysfs_root_kn;
L
Linus Torvalds 已提交
51

52
	if (!parent)
53 54
		return -ENOENT;

55 56
	kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
				  S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns);
57 58 59 60
	if (IS_ERR(kn)) {
		if (PTR_ERR(kn) == -EEXIST)
			sysfs_warn_dup(parent, kobject_name(kobj));
		return PTR_ERR(kn);
61 62
	}

63
	kobj->sd = kn;
64
	return 0;
L
Linus Torvalds 已提交
65 66
}

67 68 69 70 71 72 73 74
/**
 *	sysfs_remove_dir - remove an object's directory.
 *	@kobj:	object.
 *
 *	The only thing special about this is that we remove any files in
 *	the directory before we remove the directory, and we've inlined
 *	what used to be sysfs_rmdir() below, instead of calling separately.
 */
75
void sysfs_remove_dir(struct kobject *kobj)
76
{
77
	struct kernfs_node *kn = kobj->sd;
78

79 80 81 82 83
	/*
	 * In general, kboject owner is responsible for ensuring removal
	 * doesn't race with other operations and sysfs doesn't provide any
	 * protection; however, when @kobj is used as a symlink target, the
	 * symlinking entity usually doesn't own @kobj and thus has no
84 85
	 * control over removal.  @kobj->sd may be removed anytime
	 * and symlink code may end up dereferencing an already freed node.
86
	 *
87 88 89
	 * sysfs_symlink_target_lock synchronizes @kobj->sd
	 * disassociation against symlink operations so that symlink code
	 * can safely dereference @kobj->sd.
90 91
	 */
	spin_lock(&sysfs_symlink_target_lock);
92
	kobj->sd = NULL;
93
	spin_unlock(&sysfs_symlink_target_lock);
94

95
	if (kn) {
T
Tejun Heo 已提交
96
		WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
97
		kernfs_remove(kn);
T
Tejun Heo 已提交
98
	}
L
Linus Torvalds 已提交
99 100
}

101 102
int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
			const void *new_ns)
103
{
104 105
	struct kernfs_node *parent;
	int ret;
106

107 108 109 110
	parent = kernfs_get_parent(kobj->sd);
	ret = kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
	kernfs_put(parent);
	return ret;
111 112
}

113 114
int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
		      const void *new_ns)
115
{
116 117
	struct kernfs_node *kn = kobj->sd;
	struct kernfs_node *new_parent;
118

119 120
	new_parent = new_parent_kobj && new_parent_kobj->sd ?
		new_parent_kobj->sd : sysfs_root_kn;
121

122
	return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
123
}