^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fs/sysfs/symlink.c - sysfs symlink implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2001-3 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2007 SUSE Linux Products GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Please see Documentation/filesystems/sysfs.rst for more information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int sysfs_do_create_link_sd(struct kernfs_node *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct kobject *target_kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) const char *name, int warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct kernfs_node *kn, *target = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (WARN_ON(!name || !parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * We don't own @target_kobj and it may be removed at any time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Synchronize using sysfs_symlink_target_lock. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * sysfs_remove_dir() for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) spin_lock(&sysfs_symlink_target_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (target_kobj->sd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) target = target_kobj->sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) kernfs_get(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) spin_unlock(&sysfs_symlink_target_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) kn = kernfs_create_link(parent, name, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) kernfs_put(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!IS_ERR(kn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (warn && PTR_ERR(kn) == -EEXIST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sysfs_warn_dup(parent, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return PTR_ERR(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * sysfs_create_link_sd - create symlink to a given object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @kn: directory we're creating the link in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @target: object we're pointing to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @name: name of the symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int sysfs_create_link_sd(struct kernfs_node *kn, struct kobject *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return sysfs_do_create_link_sd(kn, target, name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) const char *name, int warn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct kernfs_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) parent = sysfs_root_kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) parent = kobj->sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return sysfs_do_create_link_sd(parent, target, name, warn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * sysfs_create_link - create symlink between two objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @kobj: object whose directory we're creating the link in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @target: object we're pointing to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @name: name of the symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int sysfs_create_link(struct kobject *kobj, struct kobject *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return sysfs_do_create_link(kobj, target, name, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) EXPORT_SYMBOL_GPL(sysfs_create_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * sysfs_create_link_nowarn - create symlink between two objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * @kobj: object whose directory we're creating the link in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @target: object we're pointing to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * @name: name of the symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * This function does the same as sysfs_create_link(), but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * doesn't warn if the link already exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return sysfs_do_create_link(kobj, target, name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) EXPORT_SYMBOL_GPL(sysfs_create_link_nowarn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * sysfs_delete_link - remove symlink in object's directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @kobj: object we're acting for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @targ: object we're pointing to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @name: name of the symlink to remove.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * Unlike sysfs_remove_link sysfs_delete_link has enough information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * to successfully delete symlinks in tagged directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) const void *ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * We don't own @target and it may be removed at any time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Synchronize using sysfs_symlink_target_lock. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * sysfs_remove_dir() for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) spin_lock(&sysfs_symlink_target_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (targ->sd && kernfs_ns_enabled(kobj->sd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ns = targ->sd->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) spin_unlock(&sysfs_symlink_target_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) kernfs_remove_by_name_ns(kobj->sd, name, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * sysfs_remove_link - remove symlink in object's directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @kobj: object we're acting for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * @name: name of the symlink to remove.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void sysfs_remove_link(struct kobject *kobj, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct kernfs_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) parent = sysfs_root_kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) parent = kobj->sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) kernfs_remove_by_name(parent, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) EXPORT_SYMBOL_GPL(sysfs_remove_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * sysfs_rename_link_ns - rename symlink in object's directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @kobj: object we're acting for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * @targ: object we're pointing to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @old: previous name of the symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @new: new name of the symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * @new_ns: new namespace of the symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * A helper function for the common rename symlink idiom.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) const char *old, const char *new, const void *new_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct kernfs_node *parent, *kn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const void *old_ns = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (!kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) parent = sysfs_root_kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) parent = kobj->sd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (targ->sd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) old_ns = targ->sd->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) result = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) kn = kernfs_find_and_get_ns(parent, old, old_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (kernfs_type(kn) != KERNFS_LINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (kn->symlink.target_kn->priv != targ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) result = kernfs_rename_ns(kn, parent, new, new_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) kernfs_put(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);