blob: 91eac6c55e07dd5aa7b95ddd0c8c3b4d36e729a1 [file] [log] [blame]
Thomas Gleixner328970d2019-05-24 12:04:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joel Becker7063fbf2005-12-15 14:29:43 -08002/* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * symlink.c - operations for configfs symlinks.
6 *
Joel Becker7063fbf2005-12-15 14:29:43 -08007 * Based on sysfs:
8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
9 *
10 * configfs Copyright (C) 2005 Oracle. All rights reserved.
11 */
12
13#include <linux/fs.h>
14#include <linux/module.h>
15#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Joel Becker7063fbf2005-12-15 14:29:43 -080017
18#include <linux/configfs.h>
19#include "configfs_internal.h"
20
Louis Rilling9a73d782008-06-20 14:09:22 +020021/* Protects attachments of new symlinks */
22DEFINE_MUTEX(configfs_symlink_mutex);
23
Joel Becker7063fbf2005-12-15 14:29:43 -080024static int item_depth(struct config_item * item)
25{
26 struct config_item * p = item;
27 int depth = 0;
28 do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
29 return depth;
30}
31
32static int item_path_length(struct config_item * item)
33{
34 struct config_item * p = item;
35 int length = 1;
36 do {
37 length += strlen(config_item_name(p)) + 1;
38 p = p->ci_parent;
39 } while (p && !configfs_is_root(p));
40 return length;
41}
42
43static void fill_item_path(struct config_item * item, char * buffer, int length)
44{
45 struct config_item * p;
46
47 --length;
48 for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
49 int cur = strlen(config_item_name(p));
50
51 /* back up enough to print this bus id with '/' */
52 length -= cur;
Guenter Roeck18233422018-07-01 13:56:54 -070053 memcpy(buffer + length, config_item_name(p), cur);
Joel Becker7063fbf2005-12-15 14:29:43 -080054 *(buffer + --length) = '/';
55 }
56}
57
58static int create_link(struct config_item *parent_item,
Joel Beckere7515d02006-03-10 11:42:30 -080059 struct config_item *item,
Joel Becker7063fbf2005-12-15 14:29:43 -080060 struct dentry *dentry)
61{
62 struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
63 struct configfs_symlink *sl;
64 int ret;
65
Louis Rilling2a109f22008-07-04 16:56:05 +020066 ret = -ENOENT;
67 if (!configfs_dirent_is_ready(target_sd))
68 goto out;
Joel Becker7063fbf2005-12-15 14:29:43 -080069 ret = -ENOMEM;
70 sl = kmalloc(sizeof(struct configfs_symlink), GFP_KERNEL);
71 if (sl) {
Louis Rilling5301a772008-06-16 19:00:59 +020072 spin_lock(&configfs_dirent_lock);
Louis Rilling4768e9b2008-06-23 14:16:17 +020073 if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
74 spin_unlock(&configfs_dirent_lock);
Louis Rilling4768e9b2008-06-23 14:16:17 +020075 kfree(sl);
76 return -ENOENT;
77 }
Nicholas Bellingerba80aa92017-06-08 04:51:54 +000078 sl->sl_target = config_item_get(item);
Joel Becker7063fbf2005-12-15 14:29:43 -080079 list_add(&sl->sl_list, &target_sd->s_links);
Louis Rilling5301a772008-06-16 19:00:59 +020080 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080081 ret = configfs_create_link(sl, parent_item->ci_dentry,
82 dentry);
83 if (ret) {
Louis Rilling5301a772008-06-16 19:00:59 +020084 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080085 list_del_init(&sl->sl_list);
Louis Rilling5301a772008-06-16 19:00:59 +020086 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -080087 config_item_put(item);
88 kfree(sl);
89 }
90 }
91
Louis Rilling2a109f22008-07-04 16:56:05 +020092out:
Joel Becker7063fbf2005-12-15 14:29:43 -080093 return ret;
94}
95
96
Al Viro421748e2008-08-02 01:04:36 -040097static int get_target(const char *symname, struct path *path,
Al Virob7c177f2012-03-17 16:24:54 -040098 struct config_item **target, struct super_block *sb)
Joel Becker7063fbf2005-12-15 14:29:43 -080099{
100 int ret;
101
Al Viro421748e2008-08-02 01:04:36 -0400102 ret = kern_path(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, path);
Joel Becker7063fbf2005-12-15 14:29:43 -0800103 if (!ret) {
Al Virob7c177f2012-03-17 16:24:54 -0400104 if (path->dentry->d_sb == sb) {
Al Viro421748e2008-08-02 01:04:36 -0400105 *target = configfs_get_config_item(path->dentry);
Joel Becker7063fbf2005-12-15 14:29:43 -0800106 if (!*target) {
107 ret = -ENOENT;
Al Viro421748e2008-08-02 01:04:36 -0400108 path_put(path);
Joel Becker7063fbf2005-12-15 14:29:43 -0800109 }
Al Viro9b6e3102010-01-13 22:10:57 -0500110 } else {
Joel Becker7063fbf2005-12-15 14:29:43 -0800111 ret = -EPERM;
Al Viro9b6e3102010-01-13 22:10:57 -0500112 path_put(path);
113 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800114 }
115
116 return ret;
117}
118
119
120int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
121{
122 int ret;
Al Viro421748e2008-08-02 01:04:36 -0400123 struct path path;
Louis Rilling2a109f22008-07-04 16:56:05 +0200124 struct configfs_dirent *sd;
Joel Becker7063fbf2005-12-15 14:29:43 -0800125 struct config_item *parent_item;
Subrata Modak3c48f232009-04-19 01:10:03 +0530126 struct config_item *target_item = NULL;
Bhumika Goyalaa293582017-10-16 17:18:40 +0200127 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800128
Louis Rilling2a109f22008-07-04 16:56:05 +0200129 sd = dentry->d_parent->d_fsdata;
130 /*
131 * Fake invisibility if dir belongs to a group/default groups hierarchy
132 * being attached
133 */
134 ret = -ENOENT;
135 if (!configfs_dirent_is_ready(sd))
136 goto out;
137
Joel Becker7063fbf2005-12-15 14:29:43 -0800138 parent_item = configfs_get_config_item(dentry->d_parent);
139 type = parent_item->ci_type;
140
Louis Rilling2a109f22008-07-04 16:56:05 +0200141 ret = -EPERM;
Joel Becker7063fbf2005-12-15 14:29:43 -0800142 if (!type || !type->ct_item_ops ||
143 !type->ct_item_ops->allow_link)
144 goto out_put;
145
Al Virob7c177f2012-03-17 16:24:54 -0400146 ret = get_target(symname, &path, &target_item, dentry->d_sb);
Joel Becker7063fbf2005-12-15 14:29:43 -0800147 if (ret)
148 goto out_put;
149
150 ret = type->ct_item_ops->allow_link(parent_item, target_item);
Louis Rillinge7520652008-06-12 17:26:47 +0200151 if (!ret) {
Louis Rilling9a73d782008-06-20 14:09:22 +0200152 mutex_lock(&configfs_symlink_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800153 ret = create_link(parent_item, target_item, dentry);
Louis Rilling9a73d782008-06-20 14:09:22 +0200154 mutex_unlock(&configfs_symlink_mutex);
Louis Rillinge7520652008-06-12 17:26:47 +0200155 if (ret && type->ct_item_ops->drop_link)
156 type->ct_item_ops->drop_link(parent_item,
157 target_item);
158 }
Joel Becker7063fbf2005-12-15 14:29:43 -0800159
160 config_item_put(target_item);
Al Viro421748e2008-08-02 01:04:36 -0400161 path_put(&path);
Joel Becker7063fbf2005-12-15 14:29:43 -0800162
163out_put:
164 config_item_put(parent_item);
165
166out:
167 return ret;
168}
169
170int configfs_unlink(struct inode *dir, struct dentry *dentry)
171{
172 struct configfs_dirent *sd = dentry->d_fsdata;
173 struct configfs_symlink *sl;
174 struct config_item *parent_item;
Bhumika Goyalaa293582017-10-16 17:18:40 +0200175 const struct config_item_type *type;
Joel Becker7063fbf2005-12-15 14:29:43 -0800176 int ret;
177
178 ret = -EPERM; /* What lack-of-symlink returns */
179 if (!(sd->s_type & CONFIGFS_ITEM_LINK))
180 goto out;
181
Joel Becker7063fbf2005-12-15 14:29:43 -0800182 sl = sd->s_element;
183
184 parent_item = configfs_get_config_item(dentry->d_parent);
185 type = parent_item->ci_type;
186
Louis Rilling6f610762008-06-16 19:00:58 +0200187 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800188 list_del_init(&sd->s_sibling);
Louis Rilling6f610762008-06-16 19:00:58 +0200189 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800190 configfs_drop_dentry(sd, dentry->d_parent);
191 dput(dentry);
192 configfs_put(sd);
193
194 /*
195 * drop_link() must be called before
196 * list_del_init(&sl->sl_list), so that the order of
197 * drop_link(this, target) and drop_item(target) is preserved.
198 */
199 if (type && type->ct_item_ops &&
200 type->ct_item_ops->drop_link)
201 type->ct_item_ops->drop_link(parent_item,
202 sl->sl_target);
203
Louis Rilling5301a772008-06-16 19:00:59 +0200204 spin_lock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800205 list_del_init(&sl->sl_list);
Louis Rilling5301a772008-06-16 19:00:59 +0200206 spin_unlock(&configfs_dirent_lock);
Joel Becker7063fbf2005-12-15 14:29:43 -0800207
208 /* Put reference from create_link() */
209 config_item_put(sl->sl_target);
210 kfree(sl);
211
212 config_item_put(parent_item);
213
214 ret = 0;
215
216out:
217 return ret;
218}
219
220static int configfs_get_target_path(struct config_item * item, struct config_item * target,
221 char *path)
222{
223 char * s;
224 int depth, size;
225
226 depth = item_depth(item);
227 size = item_path_length(target) + depth * 3 - 1;
228 if (size > PATH_MAX)
229 return -ENAMETOOLONG;
230
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700231 pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
Joel Becker7063fbf2005-12-15 14:29:43 -0800232
233 for (s = path; depth--; s += 3)
234 strcpy(s,"../");
235
236 fill_item_path(target, path, size);
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700237 pr_debug("%s: path = '%s'\n", __func__, path);
Joel Becker7063fbf2005-12-15 14:29:43 -0800238
239 return 0;
240}
241
242static int configfs_getlink(struct dentry *dentry, char * path)
243{
244 struct config_item *item, *target_item;
245 int error = 0;
246
247 item = configfs_get_config_item(dentry->d_parent);
248 if (!item)
249 return -EINVAL;
250
251 target_item = configfs_get_config_item(dentry);
252 if (!target_item) {
253 config_item_put(item);
254 return -EINVAL;
255 }
256
257 down_read(&configfs_rename_sem);
258 error = configfs_get_target_path(item, target_item, path);
259 up_read(&configfs_rename_sem);
260
261 config_item_put(item);
262 config_item_put(target_item);
263 return error;
264
265}
266
Al Viro6b255392015-11-17 10:20:54 -0500267static const char *configfs_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500268 struct inode *inode,
269 struct delayed_call *done)
Joel Becker7063fbf2005-12-15 14:29:43 -0800270{
Al Virofceef392015-12-29 15:58:39 -0500271 char *body;
Al Viro680baac2015-05-02 13:32:22 -0400272 int error;
Joel Becker7063fbf2005-12-15 14:29:43 -0800273
Al Viro6b255392015-11-17 10:20:54 -0500274 if (!dentry)
275 return ERR_PTR(-ECHILD);
276
Al Virofceef392015-12-29 15:58:39 -0500277 body = kzalloc(PAGE_SIZE, GFP_KERNEL);
278 if (!body)
Al Viro680baac2015-05-02 13:32:22 -0400279 return ERR_PTR(-ENOMEM);
280
Al Virofceef392015-12-29 15:58:39 -0500281 error = configfs_getlink(dentry, body);
Al Viro680baac2015-05-02 13:32:22 -0400282 if (!error) {
Al Virofceef392015-12-29 15:58:39 -0500283 set_delayed_call(done, kfree_link, body);
284 return body;
Joel Becker7063fbf2005-12-15 14:29:43 -0800285 }
286
Al Virofceef392015-12-29 15:58:39 -0500287 kfree(body);
Al Viro680baac2015-05-02 13:32:22 -0400288 return ERR_PTR(error);
Joel Becker7063fbf2005-12-15 14:29:43 -0800289}
290
Arjan van de Ven754661f2007-02-12 00:55:38 -0800291const struct inode_operations configfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -0500292 .get_link = configfs_get_link,
Joel Becker3d0f89b2006-01-25 13:31:07 -0800293 .setattr = configfs_setattr,
Joel Becker7063fbf2005-12-15 14:29:43 -0800294};
295