Skip to content

Commit e9c03af

Browse files
Al ViroChristoph Hellwig
authored andcommitted
configfs: calculate the symlink target only once
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 2743c51 commit e9c03af

4 files changed

Lines changed: 73 additions & 171 deletions

File tree

‎fs/configfs/configfs_internal.h‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct configfs_dirent {
3434
int s_dependent_count;
3535
struct list_head s_sibling;
3636
struct list_head s_children;
37-
struct list_head s_links;
37+
int s_links;
3838
void * s_element;
3939
int s_type;
4040
umode_t s_mode;
@@ -84,7 +84,6 @@ extern int configfs_setattr(struct dentry *dentry, struct iattr *iattr);
8484
extern struct dentry *configfs_pin_fs(void);
8585
extern void configfs_release_fs(void);
8686

87-
extern struct rw_semaphore configfs_rename_sem;
8887
extern const struct file_operations configfs_dir_operations;
8988
extern const struct file_operations configfs_file_operations;
9089
extern const struct file_operations configfs_bin_file_operations;
@@ -97,14 +96,8 @@ extern int configfs_symlink(struct inode *dir, struct dentry *dentry,
9796
const char *symname);
9897
extern int configfs_unlink(struct inode *dir, struct dentry *dentry);
9998

100-
struct configfs_symlink {
101-
struct list_head sl_list;
102-
struct config_item *sl_target;
103-
};
104-
105-
extern int configfs_create_link(struct configfs_symlink *sl,
106-
struct dentry *parent,
107-
struct dentry *dentry);
99+
int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
100+
struct dentry *dentry, char *body);
108101

109102
static inline struct config_item * to_item(struct dentry * dentry)
110103
{
@@ -132,11 +125,7 @@ static inline struct config_item *configfs_get_config_item(struct dentry *dentry
132125
spin_lock(&dentry->d_lock);
133126
if (!d_unhashed(dentry)) {
134127
struct configfs_dirent * sd = dentry->d_fsdata;
135-
if (sd->s_type & CONFIGFS_ITEM_LINK) {
136-
struct configfs_symlink * sl = sd->s_element;
137-
item = config_item_get(sl->sl_target);
138-
} else
139-
item = config_item_get(sd->s_element);
128+
item = config_item_get(sd->s_element);
140129
}
141130
spin_unlock(&dentry->d_lock);
142131

‎fs/configfs/dir.c‎

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <linux/configfs.h>
2323
#include "configfs_internal.h"
2424

25-
DECLARE_RWSEM(configfs_rename_sem);
2625
/*
2726
* Protects mutations of configfs_dirent linkage together with proper i_mutex
2827
* Also protects mutations of symlinks linkage to target configfs_dirent
@@ -191,7 +190,6 @@ static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *paren
191190
return ERR_PTR(-ENOMEM);
192191

193192
atomic_set(&sd->s_count, 1);
194-
INIT_LIST_HEAD(&sd->s_links);
195193
INIT_LIST_HEAD(&sd->s_children);
196194
sd->s_element = element;
197195
sd->s_type = type;
@@ -353,24 +351,24 @@ int configfs_dirent_is_ready(struct configfs_dirent *sd)
353351
return ret;
354352
}
355353

356-
int configfs_create_link(struct configfs_symlink *sl,
357-
struct dentry *parent,
358-
struct dentry *dentry)
354+
int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
355+
struct dentry *dentry, char *body)
359356
{
360357
int err = 0;
361358
umode_t mode = S_IFLNK | S_IRWXUGO;
362359
struct configfs_dirent *p = parent->d_fsdata;
363360
struct inode *inode;
364361

365-
err = configfs_make_dirent(p, dentry, sl, mode,
366-
CONFIGFS_ITEM_LINK, p->s_frag);
362+
err = configfs_make_dirent(p, dentry, target, mode, CONFIGFS_ITEM_LINK,
363+
p->s_frag);
367364
if (err)
368365
return err;
369366

370367
inode = configfs_create(dentry, mode);
371368
if (IS_ERR(inode))
372369
goto out_remove;
373370

371+
inode->i_link = body;
374372
inode->i_op = &configfs_symlink_inode_operations;
375373
d_instantiate(dentry, inode);
376374
dget(dentry); /* pin link dentries in core */
@@ -517,7 +515,7 @@ static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
517515
parent_sd->s_type |= CONFIGFS_USET_DROPPING;
518516

519517
ret = -EBUSY;
520-
if (!list_empty(&parent_sd->s_links))
518+
if (parent_sd->s_links)
521519
goto out;
522520

523521
ret = 0;
@@ -1575,44 +1573,6 @@ const struct inode_operations configfs_root_inode_operations = {
15751573
.setattr = configfs_setattr,
15761574
};
15771575

1578-
#if 0
1579-
int configfs_rename_dir(struct config_item * item, const char *new_name)
1580-
{
1581-
int error = 0;
1582-
struct dentry * new_dentry, * parent;
1583-
1584-
if (!strcmp(config_item_name(item), new_name))
1585-
return -EINVAL;
1586-
1587-
if (!item->parent)
1588-
return -EINVAL;
1589-
1590-
down_write(&configfs_rename_sem);
1591-
parent = item->parent->dentry;
1592-
1593-
inode_lock(d_inode(parent));
1594-
1595-
new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1596-
if (!IS_ERR(new_dentry)) {
1597-
if (d_really_is_negative(new_dentry)) {
1598-
error = config_item_set_name(item, "%s", new_name);
1599-
if (!error) {
1600-
d_add(new_dentry, NULL);
1601-
d_move(item->dentry, new_dentry);
1602-
}
1603-
else
1604-
d_delete(new_dentry);
1605-
} else
1606-
error = -EEXIST;
1607-
dput(new_dentry);
1608-
}
1609-
inode_unlock(d_inode(parent));
1610-
up_write(&configfs_rename_sem);
1611-
1612-
return error;
1613-
}
1614-
#endif
1615-
16161576
static int configfs_dir_open(struct inode *inode, struct file *file)
16171577
{
16181578
struct dentry * dentry = file->f_path.dentry;

‎fs/configfs/mount.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ static struct vfsmount *configfs_mount = NULL;
2828
struct kmem_cache *configfs_dir_cachep;
2929
static int configfs_mnt_count = 0;
3030

31+
32+
static void configfs_free_inode(struct inode *inode)
33+
{
34+
if (S_ISLNK(inode->i_mode))
35+
kfree(inode->i_link);
36+
free_inode_nonrcu(inode);
37+
}
38+
3139
static const struct super_operations configfs_ops = {
3240
.statfs = simple_statfs,
3341
.drop_inode = generic_delete_inode,
42+
.free_inode = configfs_free_inode,
3443
};
3544

3645
static struct config_group configfs_root_group = {

‎fs/configfs/symlink.c‎

Lines changed: 54 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,63 @@ static void fill_item_path(struct config_item * item, char * buffer, int length)
5555
}
5656
}
5757

58+
static int configfs_get_target_path(struct config_item *item,
59+
struct config_item *target, char *path)
60+
{
61+
int depth, size;
62+
char *s;
63+
64+
depth = item_depth(item);
65+
size = item_path_length(target) + depth * 3 - 1;
66+
if (size > PATH_MAX)
67+
return -ENAMETOOLONG;
68+
69+
pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
70+
71+
for (s = path; depth--; s += 3)
72+
strcpy(s,"../");
73+
74+
fill_item_path(target, path, size);
75+
pr_debug("%s: path = '%s'\n", __func__, path);
76+
return 0;
77+
}
78+
5879
static int create_link(struct config_item *parent_item,
5980
struct config_item *item,
6081
struct dentry *dentry)
6182
{
6283
struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
63-
struct configfs_symlink *sl;
84+
char *body;
6485
int ret;
6586

66-
ret = -ENOENT;
6787
if (!configfs_dirent_is_ready(target_sd))
68-
goto out;
69-
ret = -ENOMEM;
70-
sl = kmalloc(sizeof(struct configfs_symlink), GFP_KERNEL);
71-
if (sl) {
88+
return -ENOENT;
89+
90+
body = kzalloc(PAGE_SIZE, GFP_KERNEL);
91+
if (!body)
92+
return -ENOMEM;
93+
94+
configfs_get(target_sd);
95+
spin_lock(&configfs_dirent_lock);
96+
if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
97+
spin_unlock(&configfs_dirent_lock);
98+
configfs_put(target_sd);
99+
kfree(body);
100+
return -ENOENT;
101+
}
102+
target_sd->s_links++;
103+
spin_unlock(&configfs_dirent_lock);
104+
ret = configfs_get_target_path(item, item, body);
105+
if (!ret)
106+
ret = configfs_create_link(target_sd, parent_item->ci_dentry,
107+
dentry, body);
108+
if (ret) {
72109
spin_lock(&configfs_dirent_lock);
73-
if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
74-
spin_unlock(&configfs_dirent_lock);
75-
kfree(sl);
76-
return -ENOENT;
77-
}
78-
sl->sl_target = config_item_get(item);
79-
list_add(&sl->sl_list, &target_sd->s_links);
110+
target_sd->s_links--;
80111
spin_unlock(&configfs_dirent_lock);
81-
ret = configfs_create_link(sl, parent_item->ci_dentry,
82-
dentry);
83-
if (ret) {
84-
spin_lock(&configfs_dirent_lock);
85-
list_del_init(&sl->sl_list);
86-
spin_unlock(&configfs_dirent_lock);
87-
config_item_put(item);
88-
kfree(sl);
89-
}
112+
configfs_put(target_sd);
113+
kfree(body);
90114
}
91-
92-
out:
93115
return ret;
94116
}
95117

@@ -131,9 +153,8 @@ int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symna
131153
* Fake invisibility if dir belongs to a group/default groups hierarchy
132154
* being attached
133155
*/
134-
ret = -ENOENT;
135156
if (!configfs_dirent_is_ready(sd))
136-
goto out;
157+
return -ENOENT;
137158

138159
parent_item = configfs_get_config_item(dentry->d_parent);
139160
type = parent_item->ci_type;
@@ -193,15 +214,12 @@ int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symna
193214

194215
out_put:
195216
config_item_put(parent_item);
196-
197-
out:
198217
return ret;
199218
}
200219

201220
int configfs_unlink(struct inode *dir, struct dentry *dentry)
202221
{
203-
struct configfs_dirent *sd = dentry->d_fsdata;
204-
struct configfs_symlink *sl;
222+
struct configfs_dirent *sd = dentry->d_fsdata, *target_sd;
205223
struct config_item *parent_item;
206224
const struct config_item_type *type;
207225
int ret;
@@ -210,7 +228,7 @@ int configfs_unlink(struct inode *dir, struct dentry *dentry)
210228
if (!(sd->s_type & CONFIGFS_ITEM_LINK))
211229
goto out;
212230

213-
sl = sd->s_element;
231+
target_sd = sd->s_element;
214232

215233
parent_item = configfs_get_config_item(dentry->d_parent);
216234
type = parent_item->ci_type;
@@ -224,21 +242,18 @@ int configfs_unlink(struct inode *dir, struct dentry *dentry)
224242

225243
/*
226244
* drop_link() must be called before
227-
* list_del_init(&sl->sl_list), so that the order of
245+
* decrementing target's ->s_links, so that the order of
228246
* drop_link(this, target) and drop_item(target) is preserved.
229247
*/
230248
if (type && type->ct_item_ops &&
231249
type->ct_item_ops->drop_link)
232250
type->ct_item_ops->drop_link(parent_item,
233-
sl->sl_target);
251+
target_sd->s_element);
234252

235253
spin_lock(&configfs_dirent_lock);
236-
list_del_init(&sl->sl_list);
254+
target_sd->s_links--;
237255
spin_unlock(&configfs_dirent_lock);
238-
239-
/* Put reference from create_link() */
240-
config_item_put(sl->sl_target);
241-
kfree(sl);
256+
configfs_put(target_sd);
242257

243258
config_item_put(parent_item);
244259

@@ -248,79 +263,8 @@ int configfs_unlink(struct inode *dir, struct dentry *dentry)
248263
return ret;
249264
}
250265

251-
static int configfs_get_target_path(struct config_item * item, struct config_item * target,
252-
char *path)
253-
{
254-
char * s;
255-
int depth, size;
256-
257-
depth = item_depth(item);
258-
size = item_path_length(target) + depth * 3 - 1;
259-
if (size > PATH_MAX)
260-
return -ENAMETOOLONG;
261-
262-
pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
263-
264-
for (s = path; depth--; s += 3)
265-
strcpy(s,"../");
266-
267-
fill_item_path(target, path, size);
268-
pr_debug("%s: path = '%s'\n", __func__, path);
269-
270-
return 0;
271-
}
272-
273-
static int configfs_getlink(struct dentry *dentry, char * path)
274-
{
275-
struct config_item *item, *target_item;
276-
int error = 0;
277-
278-
item = configfs_get_config_item(dentry->d_parent);
279-
if (!item)
280-
return -EINVAL;
281-
282-
target_item = configfs_get_config_item(dentry);
283-
if (!target_item) {
284-
config_item_put(item);
285-
return -EINVAL;
286-
}
287-
288-
down_read(&configfs_rename_sem);
289-
error = configfs_get_target_path(item, target_item, path);
290-
up_read(&configfs_rename_sem);
291-
292-
config_item_put(item);
293-
config_item_put(target_item);
294-
return error;
295-
296-
}
297-
298-
static const char *configfs_get_link(struct dentry *dentry,
299-
struct inode *inode,
300-
struct delayed_call *done)
301-
{
302-
char *body;
303-
int error;
304-
305-
if (!dentry)
306-
return ERR_PTR(-ECHILD);
307-
308-
body = kzalloc(PAGE_SIZE, GFP_KERNEL);
309-
if (!body)
310-
return ERR_PTR(-ENOMEM);
311-
312-
error = configfs_getlink(dentry, body);
313-
if (!error) {
314-
set_delayed_call(done, kfree_link, body);
315-
return body;
316-
}
317-
318-
kfree(body);
319-
return ERR_PTR(error);
320-
}
321-
322266
const struct inode_operations configfs_symlink_inode_operations = {
323-
.get_link = configfs_get_link,
267+
.get_link = simple_get_link,
324268
.setattr = configfs_setattr,
325269
};
326270

0 commit comments

Comments
 (0)