blob: ee0d761c3179ca06efcdebe619eb107214079ba7 [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 Heobb8b9d092013-12-11 16:02:55 -050076 kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
77 S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns);
Tejun Heo324a56e2013-12-11 14:11:53 -050078 if (IS_ERR(kn)) {
79 if (PTR_ERR(kn) == -EEXIST)
80 sysfs_warn_dup(parent, kobject_name(kobj));
81 return PTR_ERR(kn);
Tejun Heo93b2b8e2013-11-28 14:54:15 -050082 }
83
Tejun Heo324a56e2013-12-11 14:11:53 -050084 kobj->sd = kn;
Tejun Heo93b2b8e2013-11-28 14:54:15 -050085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Tejun Heo7eed6ec2013-10-24 11:49:10 -040088/**
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070089 * sysfs_remove_dir - remove an object's directory.
90 * @kobj: object.
91 *
92 * The only thing special about this is that we remove any files in
93 * the directory before we remove the directory, and we've inlined
94 * what used to be sysfs_rmdir() below, instead of calling separately.
95 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070096void sysfs_remove_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070097{
Tejun Heo324a56e2013-12-11 14:11:53 -050098 struct kernfs_node *kn = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +090099
Tejun Heo0cae60f2013-10-30 10:28:36 -0400100 /*
101 * In general, kboject owner is responsible for ensuring removal
102 * doesn't race with other operations and sysfs doesn't provide any
103 * protection; however, when @kobj is used as a symlink target, the
104 * symlinking entity usually doesn't own @kobj and thus has no
Tejun Heo324a56e2013-12-11 14:11:53 -0500105 * control over removal. @kobj->sd may be removed anytime
106 * and symlink code may end up dereferencing an already freed node.
Tejun Heo0cae60f2013-10-30 10:28:36 -0400107 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500108 * sysfs_symlink_target_lock synchronizes @kobj->sd
109 * disassociation against symlink operations so that symlink code
110 * can safely dereference @kobj->sd.
Tejun Heo0cae60f2013-10-30 10:28:36 -0400111 */
112 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900113 kobj->sd = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400114 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900115
Tejun Heo324a56e2013-12-11 14:11:53 -0500116 if (kn) {
Tejun Heodf23fc32013-12-11 14:11:56 -0500117 WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500118 kernfs_remove(kn);
Tejun Heo250f7c32013-09-18 17:15:38 -0400119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Tejun Heoe34ff492013-09-11 22:29:05 -0400122int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
123 const void *new_ns)
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800124{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500125 struct kernfs_node *parent = kobj->sd->parent;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700126
Tejun Heo324a56e2013-12-11 14:11:53 -0500127 return kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
Eric W. Biedermanca1bab32009-11-20 16:08:57 -0800128}
129
Tejun Heoe34ff492013-09-11 22:29:05 -0400130int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
131 const void *new_ns)
Cornelia Huck8a824722006-11-20 17:07:51 +0100132{
Tejun Heo324a56e2013-12-11 14:11:53 -0500133 struct kernfs_node *kn = kobj->sd;
134 struct kernfs_node *new_parent;
Cornelia Huck8a824722006-11-20 17:07:51 +0100135
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500136 BUG_ON(!kn->parent);
Tejun Heo324a56e2013-12-11 14:11:53 -0500137 new_parent = new_parent_kobj && new_parent_kobj->sd ?
138 new_parent_kobj->sd : sysfs_root_kn;
Cornelia Huck8a824722006-11-20 17:07:51 +0100139
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500140 return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
Cornelia Huck8a824722006-11-20 17:07:51 +0100141}