kernfs: s/sysfs/kernfs/ in various data structures
[deliverable/linux.git] / fs / sysfs / dir.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
2 * fs/sysfs/dir.c - sysfs core and dir operation implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
1da177e4
LT
11 */
12
13#undef DEBUG
14
15#include <linux/fs.h>
1da177e4 16#include <linux/kobject.h>
c6f87733 17#include <linux/slab.h>
1da177e4
LT
18#include "sysfs.h"
19
0cae60f9 20DEFINE_SPINLOCK(sysfs_symlink_target_lock);
1da177e4 21
425cb029
AC
22/**
23 * sysfs_pathname - return full path to sysfs dirent
324a56e1 24 * @kn: kernfs_node whose path we want
66081a72 25 * @path: caller allocated buffer of size PATH_MAX
425cb029
AC
26 *
27 * Gives the name "/" to the sysfs_root entry; any path returned
28 * is relative to wherever sysfs is mounted.
425cb029 29 */
324a56e1 30static char *sysfs_pathname(struct kernfs_node *kn, char *path)
425cb029 31{
adc5e8b5
TH
32 if (kn->parent) {
33 sysfs_pathname(kn->parent, path);
66081a72 34 strlcat(path, "/", PATH_MAX);
425cb029 35 }
adc5e8b5 36 strlcat(path, kn->name, PATH_MAX);
425cb029
AC
37 return path;
38}
39
324a56e1 40void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
d1c1459e
TH
41{
42 char *path;
43
44 path = kzalloc(PATH_MAX, GFP_KERNEL);
45 if (path) {
46 sysfs_pathname(parent, path);
47 strlcat(path, "/", PATH_MAX);
48 strlcat(path, name, PATH_MAX);
49 }
50
51 WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n",
52 path ? path : name);
53
54 kfree(path);
55}
56
1da177e4 57/**
e34ff490
TH
58 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
59 * @kobj: object we're creating directory for
60 * @ns: the namespace tag to use
1da177e4 61 */
e34ff490 62int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
1da177e4 63{
324a56e1 64 struct kernfs_node *parent, *kn;
1da177e4
LT
65
66 BUG_ON(!kobj);
67
90bc6135 68 if (kobj->parent)
324a56e1 69 parent = kobj->parent->sd;
1da177e4 70 else
324a56e1 71 parent = sysfs_root_kn;
1da177e4 72
324a56e1 73 if (!parent)
3a198886
DW
74 return -ENOENT;
75
324a56e1
TH
76 kn = kernfs_create_dir_ns(parent, kobject_name(kobj), kobj, ns);
77 if (IS_ERR(kn)) {
78 if (PTR_ERR(kn) == -EEXIST)
79 sysfs_warn_dup(parent, kobject_name(kobj));
80 return PTR_ERR(kn);
93b2b8e4
TH
81 }
82
324a56e1 83 kobj->sd = kn;
93b2b8e4 84 return 0;
1da177e4
LT
85}
86
b592fcfe
EB
87/**
88 * sysfs_remove_dir - remove an object's directory.
89 * @kobj: object.
90 *
91 * The only thing special about this is that we remove any files in
92 * the directory before we remove the directory, and we've inlined
93 * what used to be sysfs_rmdir() below, instead of calling separately.
94 */
1b18dc2b 95void sysfs_remove_dir(struct kobject *kobj)
b592fcfe 96{
324a56e1 97 struct kernfs_node *kn = kobj->sd;
aecdceda 98
0cae60f9
TH
99 /*
100 * In general, kboject owner is responsible for ensuring removal
101 * doesn't race with other operations and sysfs doesn't provide any
102 * protection; however, when @kobj is used as a symlink target, the
103 * symlinking entity usually doesn't own @kobj and thus has no
324a56e1
TH
104 * control over removal. @kobj->sd may be removed anytime
105 * and symlink code may end up dereferencing an already freed node.
0cae60f9 106 *
324a56e1
TH
107 * sysfs_symlink_target_lock synchronizes @kobj->sd
108 * disassociation against symlink operations so that symlink code
109 * can safely dereference @kobj->sd.
0cae60f9
TH
110 */
111 spin_lock(&sysfs_symlink_target_lock);
608e266a 112 kobj->sd = NULL;
0cae60f9 113 spin_unlock(&sysfs_symlink_target_lock);
aecdceda 114
324a56e1
TH
115 if (kn) {
116 WARN_ON_ONCE(sysfs_type(kn) != SYSFS_DIR);
117 kernfs_remove(kn);
250f7c3f 118 }
1da177e4
LT
119}
120
e34ff490
TH
121int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
122 const void *new_ns)
ca1bab38 123{
adc5e8b5 124 struct kernfs_node *parent = kobj->sd->parent;
3ff195b0 125
324a56e1 126 return kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
ca1bab38
EB
127}
128
e34ff490
TH
129int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
130 const void *new_ns)
8a82472f 131{
324a56e1
TH
132 struct kernfs_node *kn = kobj->sd;
133 struct kernfs_node *new_parent;
8a82472f 134
adc5e8b5 135 BUG_ON(!kn->parent);
324a56e1
TH
136 new_parent = new_parent_kobj && new_parent_kobj->sd ?
137 new_parent_kobj->sd : sysfs_root_kn;
51225039 138
adc5e8b5 139 return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
8a82472f 140}
This page took 0.857399 seconds and 5 git commands to generate.