blob: e39b884f0867d32eb4ffb322121d59ba3df0981d [file] [log] [blame]
Greg Kroah-Hartman619daee2018-01-22 16:18:13 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09003 * fs/sysfs/dir.c - sysfs core and dir operation implementation
4 *
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 *
Tejun Heo6d66f5c2007-09-20 17:31:38 +09009 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Greg Kroah-Hartman5d54f942018-01-22 15:57:59 +010012#define pr_fmt(fmt) "sysfs: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kobject.h>
Robert P. J. Dayc6f87732008-03-13 22:41:52 -040016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "sysfs.h"
18
Tejun Heo0cae60f2013-10-30 10:28:36 -040019DEFINE_SPINLOCK(sysfs_symlink_target_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Tejun Heo324a56e2013-12-11 14:11:53 -050021void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
Tejun Heod1c14592013-10-24 11:49:11 -040022{
Tejun Heo3abb1d92016-08-10 11:23:44 -040023 char *buf;
Tejun Heod1c14592013-10-24 11:49:11 -040024
Tejun Heo3eef34a2014-02-07 13:32:07 -050025 buf = kzalloc(PATH_MAX, GFP_KERNEL);
26 if (buf)
Tejun Heo3abb1d92016-08-10 11:23:44 -040027 kernfs_path(parent, buf, PATH_MAX);
Tejun Heod1c14592013-10-24 11:49:11 -040028
Greg Kroah-Hartman5d54f942018-01-22 15:57:59 +010029 pr_warn("cannot create duplicate filename '%s/%s'\n", buf, name);
30 dump_stack();
Tejun Heod1c14592013-10-24 11:49:11 -040031
Tejun Heo3eef34a2014-02-07 13:32:07 -050032 kfree(buf);
Tejun Heod1c14592013-10-24 11:49:11 -040033}
34
Alex Chiang425cb022009-02-12 10:56:59 -070035/**
Tejun Heoe34ff492013-09-11 22:29:05 -040036 * sysfs_create_dir_ns - create a directory for an object with a namespace tag
37 * @kobj: object we're creating directory for
38 * @ns: the namespace tag to use
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
Tejun Heoe34ff492013-09-11 22:29:05 -040040int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Tejun Heo324a56e2013-12-11 14:11:53 -050042 struct kernfs_node *parent, *kn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 BUG_ON(!kobj);
45
Eric W. Biederman90bc6132007-07-31 19:15:08 +090046 if (kobj->parent)
Tejun Heo324a56e2013-12-11 14:11:53 -050047 parent = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 else
Tejun Heo324a56e2013-12-11 14:11:53 -050049 parent = sysfs_root_kn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Tejun Heo324a56e2013-12-11 14:11:53 -050051 if (!parent)
Dan Williams3a198882012-04-06 13:41:06 -070052 return -ENOENT;
53
Tejun Heobb8b9d02013-12-11 16:02:55 -050054 kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
Dmitry Torokhov488dee92018-07-20 21:56:47 +000055 S_IRWXU | S_IRUGO | S_IXUGO,
56 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
57 kobj, ns);
Tejun Heo324a56e2013-12-11 14:11:53 -050058 if (IS_ERR(kn)) {
59 if (PTR_ERR(kn) == -EEXIST)
60 sysfs_warn_dup(parent, kobject_name(kobj));
61 return PTR_ERR(kn);
Tejun Heo93b2b8e2013-11-28 14:54:15 -050062 }
63
Tejun Heo324a56e2013-12-11 14:11:53 -050064 kobj->sd = kn;
Tejun Heo93b2b8e2013-11-28 14:54:15 -050065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Tejun Heo7eed6ec2013-10-24 11:49:10 -040068/**
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070069 * sysfs_remove_dir - remove an object's directory.
70 * @kobj: object.
71 *
72 * The only thing special about this is that we remove any files in
73 * the directory before we remove the directory, and we've inlined
74 * what used to be sysfs_rmdir() below, instead of calling separately.
75 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070076void sysfs_remove_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070077{
Tejun Heo324a56e2013-12-11 14:11:53 -050078 struct kernfs_node *kn = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +090079
Tejun Heo0cae60f2013-10-30 10:28:36 -040080 /*
81 * In general, kboject owner is responsible for ensuring removal
82 * doesn't race with other operations and sysfs doesn't provide any
83 * protection; however, when @kobj is used as a symlink target, the
84 * symlinking entity usually doesn't own @kobj and thus has no
Tejun Heo324a56e2013-12-11 14:11:53 -050085 * control over removal. @kobj->sd may be removed anytime
86 * and symlink code may end up dereferencing an already freed node.
Tejun Heo0cae60f2013-10-30 10:28:36 -040087 *
Tejun Heo324a56e2013-12-11 14:11:53 -050088 * sysfs_symlink_target_lock synchronizes @kobj->sd
89 * disassociation against symlink operations so that symlink code
90 * can safely dereference @kobj->sd.
Tejun Heo0cae60f2013-10-30 10:28:36 -040091 */
92 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090093 kobj->sd = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -040094 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +090095
Tejun Heo324a56e2013-12-11 14:11:53 -050096 if (kn) {
Tejun Heodf23fc32013-12-11 14:11:56 -050097 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -050098 kernfs_remove(kn);
Tejun Heo250f7c32013-09-18 17:15:38 -040099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Tejun Heoe34ff492013-09-11 22:29:05 -0400102int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
103 const void *new_ns)
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800104{
Tejun Heo3eef34a2014-02-07 13:32:07 -0500105 struct kernfs_node *parent;
106 int ret;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700107
Tejun Heo3eef34a2014-02-07 13:32:07 -0500108 parent = kernfs_get_parent(kobj->sd);
109 ret = kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
110 kernfs_put(parent);
111 return ret;
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800112}
113
Tejun Heoe34ff492013-09-11 22:29:05 -0400114int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
115 const void *new_ns)
Cornelia Huck8a824722006-11-20 17:07:51 +0100116{
Tejun Heo324a56e2013-12-11 14:11:53 -0500117 struct kernfs_node *kn = kobj->sd;
118 struct kernfs_node *new_parent;
Cornelia Huck8a824722006-11-20 17:07:51 +0100119
Tejun Heo324a56e2013-12-11 14:11:53 -0500120 new_parent = new_parent_kobj && new_parent_kobj->sd ?
121 new_parent_kobj->sd : sysfs_root_kn;
Cornelia Huck8a824722006-11-20 17:07:51 +0100122
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500123 return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
Cornelia Huck8a824722006-11-20 17:07:51 +0100124}
Eric W. Biederman87d28462015-05-13 16:31:40 -0500125
126/**
127 * sysfs_create_mount_point - create an always empty directory
128 * @parent_kobj: kobject that will contain this always empty directory
129 * @name: The name of the always empty directory to add
130 */
131int sysfs_create_mount_point(struct kobject *parent_kobj, const char *name)
132{
133 struct kernfs_node *kn, *parent = parent_kobj->sd;
134
135 kn = kernfs_create_empty_dir(parent, name);
136 if (IS_ERR(kn)) {
137 if (PTR_ERR(kn) == -EEXIST)
138 sysfs_warn_dup(parent, name);
139 return PTR_ERR(kn);
140 }
141
142 return 0;
143}
144EXPORT_SYMBOL_GPL(sysfs_create_mount_point);
145
146/**
147 * sysfs_remove_mount_point - remove an always empty directory.
148 * @parent_kobj: kobject that will contain this always empty directory
149 * @name: The name of the always empty directory to remove
150 *
151 */
152void sysfs_remove_mount_point(struct kobject *parent_kobj, const char *name)
153{
154 struct kernfs_node *parent = parent_kobj->sd;
155
156 kernfs_remove_by_name_ns(parent, name, NULL);
157}
158EXPORT_SYMBOL_GPL(sysfs_remove_mount_point);