blob: 4a800017558dc02ee9b53e666974b5a2e516f9c7 [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
Cornelia Huck36ce6da2008-06-10 11:09:08 +020022/**
Alex Chiang425cb022009-02-12 10:56:59 -070023 * sysfs_pathname - return full path to sysfs dirent
Tejun Heo324a56e2013-12-11 14:11:53 -050024 * @kn: kernfs_node whose path we want
Geert Uytterhoeven66081a72012-09-29 22:23:19 +020025 * @path: caller allocated buffer of size PATH_MAX
Alex Chiang425cb022009-02-12 10:56:59 -070026 *
27 * Gives the name "/" to the sysfs_root entry; any path returned
28 * is relative to wherever sysfs is mounted.
Alex Chiang425cb022009-02-12 10:56:59 -070029 */
Tejun Heo324a56e2013-12-11 14:11:53 -050030static char *sysfs_pathname(struct kernfs_node *kn, char *path)
Alex Chiang425cb022009-02-12 10:56:59 -070031{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050032 if (kn->parent) {
33 sysfs_pathname(kn->parent, path);
Geert Uytterhoeven66081a72012-09-29 22:23:19 +020034 strlcat(path, "/", PATH_MAX);
Alex Chiang425cb022009-02-12 10:56:59 -070035 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -050036 strlcat(path, kn->name, PATH_MAX);
Alex Chiang425cb022009-02-12 10:56:59 -070037 return path;
38}
39
Tejun Heo324a56e2013-12-11 14:11:53 -050040void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
Tejun Heod1c14592013-10-24 11:49:11 -040041{
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
Alex Chiang425cb022009-02-12 10:56:59 -070057/**
Tejun Heoe34ff492013-09-11 22:29:05 -040058 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
Tejun Heoe34ff492013-09-11 22:29:05 -040062int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Tejun Heo324a56e2013-12-11 14:11:53 -050064 struct kernfs_node *parent, *kn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 BUG_ON(!kobj);
67
Eric W. Biederman90bc6132007-07-31 19:15:08 +090068 if (kobj->parent)
Tejun Heo324a56e2013-12-11 14:11:53 -050069 parent = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 else
Tejun Heo324a56e2013-12-11 14:11:53 -050071 parent = sysfs_root_kn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Tejun Heo324a56e2013-12-11 14:11:53 -050073 if (!parent)
Dan Williams3a198882012-04-06 13:41:06 -070074 return -ENOENT;
75
Tejun Heo324a56e2013-12-11 14:11:53 -050076 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);
Tejun Heo93b2b8e2013-11-28 14:54:15 -050081 }
82
Tejun Heo324a56e2013-12-11 14:11:53 -050083 kobj->sd = kn;
Tejun Heo93b2b8e2013-11-28 14:54:15 -050084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Tejun Heo7eed6ec2013-10-24 11:49:10 -040087/**
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070088 * 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 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070095void sysfs_remove_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070096{
Tejun Heo324a56e2013-12-11 14:11:53 -050097 struct kernfs_node *kn = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +090098
Tejun Heo0cae60f2013-10-30 10:28:36 -040099 /*
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
Tejun Heo324a56e2013-12-11 14:11:53 -0500104 * control over removal. @kobj->sd may be removed anytime
105 * and symlink code may end up dereferencing an already freed node.
Tejun Heo0cae60f2013-10-30 10:28:36 -0400106 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500107 * sysfs_symlink_target_lock synchronizes @kobj->sd
108 * disassociation against symlink operations so that symlink code
109 * can safely dereference @kobj->sd.
Tejun Heo0cae60f2013-10-30 10:28:36 -0400110 */
111 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900112 kobj->sd = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400113 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900114
Tejun Heo324a56e2013-12-11 14:11:53 -0500115 if (kn) {
116 WARN_ON_ONCE(sysfs_type(kn) != SYSFS_DIR);
117 kernfs_remove(kn);
Tejun Heo250f7c32013-09-18 17:15:38 -0400118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Tejun Heoe34ff492013-09-11 22:29:05 -0400121int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
122 const void *new_ns)
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800123{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500124 struct kernfs_node *parent = kobj->sd->parent;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700125
Tejun Heo324a56e2013-12-11 14:11:53 -0500126 return kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800127}
128
Tejun Heoe34ff492013-09-11 22:29:05 -0400129int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
130 const void *new_ns)
Cornelia Huck8a824722006-11-20 17:07:51 +0100131{
Tejun Heo324a56e2013-12-11 14:11:53 -0500132 struct kernfs_node *kn = kobj->sd;
133 struct kernfs_node *new_parent;
Cornelia Huck8a824722006-11-20 17:07:51 +0100134
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500135 BUG_ON(!kn->parent);
Tejun Heo324a56e2013-12-11 14:11:53 -0500136 new_parent = new_parent_kobj && new_parent_kobj->sd ?
137 new_parent_kobj->sd : sysfs_root_kn;
Cornelia Huck8a824722006-11-20 17:07:51 +0100138
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500139 return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
Cornelia Huck8a824722006-11-20 17:07:51 +0100140}