blob: 2b67bda2021b9e2955ae66ba03e536dfdcefec2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#undef DEBUG
14
15#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kobject.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "sysfs.h"
19
Tejun Heo0cae60f2013-10-30 10:28:36 -040020DEFINE_SPINLOCK(sysfs_symlink_target_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Tejun Heo324a56e2013-12-11 14:11:53 -050022void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
Tejun Heod1c14592013-10-24 11:49:11 -040023{
Tejun Heo3abb1d92016-08-10 11:23:44 -040024 char *buf;
Tejun Heod1c14592013-10-24 11:49:11 -040025
Tejun Heo3eef34a2014-02-07 13:32:07 -050026 buf = kzalloc(PATH_MAX, GFP_KERNEL);
27 if (buf)
Tejun Heo3abb1d92016-08-10 11:23:44 -040028 kernfs_path(parent, buf, PATH_MAX);
Tejun Heod1c14592013-10-24 11:49:11 -040029
Tejun Heo3eef34a2014-02-07 13:32:07 -050030 WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s/%s'\n",
Tejun Heo3abb1d92016-08-10 11:23:44 -040031 buf, name);
Tejun Heod1c14592013-10-24 11:49:11 -040032
Tejun Heo3eef34a2014-02-07 13:32:07 -050033 kfree(buf);
Tejun Heod1c14592013-10-24 11:49:11 -040034}
35
Alex Chiang425cb022009-02-12 10:56:59 -070036/**
Tejun Heoe34ff492013-09-11 22:29:05 -040037 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
38 * @kobj: object we're creating directory for
39 * @ns: the namespace tag to use
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
Tejun Heoe34ff492013-09-11 22:29:05 -040041int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Tejun Heo324a56e2013-12-11 14:11:53 -050043 struct kernfs_node *parent, *kn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 BUG_ON(!kobj);
46
Eric W. Biederman90bc6132007-07-31 19:15:08 +090047 if (kobj->parent)
Tejun Heo324a56e2013-12-11 14:11:53 -050048 parent = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 else
Tejun Heo324a56e2013-12-11 14:11:53 -050050 parent = sysfs_root_kn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Tejun Heo324a56e2013-12-11 14:11:53 -050052 if (!parent)
Dan Williams3a198882012-04-06 13:41:06 -070053 return -ENOENT;
54
Tejun Heobb8b9d092013-12-11 16:02:55 -050055 kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
56 S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns);
Tejun Heo324a56e2013-12-11 14:11:53 -050057 if (IS_ERR(kn)) {
58 if (PTR_ERR(kn) == -EEXIST)
59 sysfs_warn_dup(parent, kobject_name(kobj));
60 return PTR_ERR(kn);
Tejun Heo93b2b8e2013-11-28 14:54:15 -050061 }
62
Tejun Heo324a56e2013-12-11 14:11:53 -050063 kobj->sd = kn;
Tejun Heo93b2b8e2013-11-28 14:54:15 -050064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Tejun Heo7eed6ec2013-10-24 11:49:10 -040067/**
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070068 * sysfs_remove_dir - remove an object's directory.
69 * @kobj: object.
70 *
71 * The only thing special about this is that we remove any files in
72 * the directory before we remove the directory, and we've inlined
73 * what used to be sysfs_rmdir() below, instead of calling separately.
74 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070075void sysfs_remove_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070076{
Tejun Heo324a56e2013-12-11 14:11:53 -050077 struct kernfs_node *kn = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +090078
Tejun Heo0cae60f2013-10-30 10:28:36 -040079 /*
80 * In general, kboject owner is responsible for ensuring removal
81 * doesn't race with other operations and sysfs doesn't provide any
82 * protection; however, when @kobj is used as a symlink target, the
83 * symlinking entity usually doesn't own @kobj and thus has no
Tejun Heo324a56e2013-12-11 14:11:53 -050084 * control over removal. @kobj->sd may be removed anytime
85 * and symlink code may end up dereferencing an already freed node.
Tejun Heo0cae60f2013-10-30 10:28:36 -040086 *
Tejun Heo324a56e2013-12-11 14:11:53 -050087 * sysfs_symlink_target_lock synchronizes @kobj->sd
88 * disassociation against symlink operations so that symlink code
89 * can safely dereference @kobj->sd.
Tejun Heo0cae60f2013-10-30 10:28:36 -040090 */
91 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090092 kobj->sd = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -040093 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +090094
Tejun Heo324a56e2013-12-11 14:11:53 -050095 if (kn) {
Tejun Heodf23fc32013-12-11 14:11:56 -050096 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -050097 kernfs_remove(kn);
Tejun Heo250f7c32013-09-18 17:15:38 -040098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Tejun Heoe34ff492013-09-11 22:29:05 -0400101int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
102 const void *new_ns)
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800103{
Tejun Heo3eef34a2014-02-07 13:32:07 -0500104 struct kernfs_node *parent;
105 int ret;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700106
Tejun Heo3eef34a2014-02-07 13:32:07 -0500107 parent = kernfs_get_parent(kobj->sd);
108 ret = kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
109 kernfs_put(parent);
110 return ret;
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800111}
112
Tejun Heoe34ff492013-09-11 22:29:05 -0400113int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
114 const void *new_ns)
Cornelia Huck8a824722006-11-20 17:07:51 +0100115{
Tejun Heo324a56e2013-12-11 14:11:53 -0500116 struct kernfs_node *kn = kobj->sd;
117 struct kernfs_node *new_parent;
Cornelia Huck8a824722006-11-20 17:07:51 +0100118
Tejun Heo324a56e2013-12-11 14:11:53 -0500119 new_parent = new_parent_kobj && new_parent_kobj->sd ?
120 new_parent_kobj->sd : sysfs_root_kn;
Cornelia Huck8a824722006-11-20 17:07:51 +0100121
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500122 return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
Cornelia Huck8a824722006-11-20 17:07:51 +0100123}
Eric W. Biederman87d28462015-05-13 16:31:40 -0500124
125/**
126 * sysfs_create_mount_point - create an always empty directory
127 * @parent_kobj: kobject that will contain this always empty directory
128 * @name: The name of the always empty directory to add
129 */
130int sysfs_create_mount_point(struct kobject *parent_kobj, const char *name)
131{
132 struct kernfs_node *kn, *parent = parent_kobj->sd;
133
134 kn = kernfs_create_empty_dir(parent, name);
135 if (IS_ERR(kn)) {
136 if (PTR_ERR(kn) == -EEXIST)
137 sysfs_warn_dup(parent, name);
138 return PTR_ERR(kn);
139 }
140
141 return 0;
142}
143EXPORT_SYMBOL_GPL(sysfs_create_mount_point);
144
145/**
146 * sysfs_remove_mount_point - remove an always empty directory.
147 * @parent_kobj: kobject that will contain this always empty directory
148 * @name: The name of the always empty directory to remove
149 *
150 */
151void sysfs_remove_mount_point(struct kobject *parent_kobj, const char *name)
152{
153 struct kernfs_node *parent = parent_kobj->sd;
154
155 kernfs_remove_by_name_ns(parent, name, NULL);
156}
157EXPORT_SYMBOL_GPL(sysfs_remove_mount_point);