Skip to content

Commit 3d0f89b

Browse files
Joel BeckerMark Fasheh
authored andcommitted
configfs: Add permission and ownership to configfs objects.
configfs always made item and attribute ownership root.root and permissions based on a umask of 022. Add ->setattr() to allow chown(2)/chmod(2), and persist the changes for the lifetime of the items and attributes. Signed-off-by: Joel Becker <joel.becker@oracle.com> Signed-off-by: Mark Fasheh <mark.fasheh@oracle.com>
1 parent 62ca3d2 commit 3d0f89b

8 files changed

Lines changed: 179 additions & 37 deletions

File tree

‎Documentation/filesystems/configfs/configfs_example.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ static struct config_item_type simple_children_type = {
320320
.ct_item_ops = &simple_children_item_ops,
321321
.ct_group_ops = &simple_children_group_ops,
322322
.ct_attrs = simple_children_attrs,
323+
.ct_owner = THIS_MODULE,
323324
};
324325

325326
static struct configfs_subsystem simple_children_subsys = {
@@ -403,6 +404,7 @@ static struct config_item_type group_children_type = {
403404
.ct_item_ops = &group_children_item_ops,
404405
.ct_group_ops = &group_children_group_ops,
405406
.ct_attrs = group_children_attrs,
407+
.ct_owner = THIS_MODULE,
406408
};
407409

408410
static struct configfs_subsystem group_children_subsys = {

‎fs/configfs/configfs_internal.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct configfs_dirent {
3636
int s_type;
3737
umode_t s_mode;
3838
struct dentry * s_dentry;
39+
struct iattr * s_iattr;
3940
};
4041

4142
#define CONFIGFS_ROOT 0x0001
@@ -48,10 +49,11 @@ struct configfs_dirent {
4849
#define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR)
4950

5051
extern struct vfsmount * configfs_mount;
52+
extern kmem_cache_t *configfs_dir_cachep;
5153

5254
extern int configfs_is_root(struct config_item *item);
5355

54-
extern struct inode * configfs_new_inode(mode_t mode);
56+
extern struct inode * configfs_new_inode(mode_t mode, struct configfs_dirent *);
5557
extern int configfs_create(struct dentry *, int mode, int (*init)(struct inode *));
5658

5759
extern int configfs_create_file(struct config_item *, const struct configfs_attribute *);
@@ -63,6 +65,7 @@ extern void configfs_hash_and_remove(struct dentry * dir, const char * name);
6365

6466
extern const unsigned char * configfs_get_name(struct configfs_dirent *sd);
6567
extern void configfs_drop_dentry(struct configfs_dirent *sd, struct dentry *parent);
68+
extern int configfs_setattr(struct dentry *dentry, struct iattr *iattr);
6669

6770
extern int configfs_pin_fs(void);
6871
extern void configfs_release_fs(void);
@@ -120,8 +123,10 @@ static inline struct config_item *configfs_get_config_item(struct dentry *dentry
120123

121124
static inline void release_configfs_dirent(struct configfs_dirent * sd)
122125
{
123-
if (!(sd->s_type & CONFIGFS_ROOT))
124-
kfree(sd);
126+
if (!(sd->s_type & CONFIGFS_ROOT)) {
127+
kfree(sd->s_iattr);
128+
kmem_cache_free(configfs_dir_cachep, sd);
129+
}
125130
}
126131

127132
static inline struct configfs_dirent * configfs_get(struct configfs_dirent * sd)

‎fs/configfs/dir.c‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent * pare
7272
{
7373
struct configfs_dirent * sd;
7474

75-
sd = kmalloc(sizeof(*sd), GFP_KERNEL);
75+
sd = kmem_cache_alloc(configfs_dir_cachep, GFP_KERNEL);
7676
if (!sd)
7777
return NULL;
7878

@@ -136,13 +136,19 @@ static int create_dir(struct config_item * k, struct dentry * p,
136136
int error;
137137
umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
138138

139-
error = configfs_create(d, mode, init_dir);
139+
error = configfs_make_dirent(p->d_fsdata, d, k, mode,
140+
CONFIGFS_DIR);
140141
if (!error) {
141-
error = configfs_make_dirent(p->d_fsdata, d, k, mode,
142-
CONFIGFS_DIR);
142+
error = configfs_create(d, mode, init_dir);
143143
if (!error) {
144144
p->d_inode->i_nlink++;
145145
(d)->d_op = &configfs_dentry_ops;
146+
} else {
147+
struct configfs_dirent *sd = d->d_fsdata;
148+
if (sd) {
149+
list_del_init(&sd->s_sibling);
150+
configfs_put(sd);
151+
}
146152
}
147153
}
148154
return error;
@@ -182,12 +188,19 @@ int configfs_create_link(struct configfs_symlink *sl,
182188
int err = 0;
183189
umode_t mode = S_IFLNK | S_IRWXUGO;
184190

185-
err = configfs_create(dentry, mode, init_symlink);
191+
err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
192+
CONFIGFS_ITEM_LINK);
186193
if (!err) {
187-
err = configfs_make_dirent(parent->d_fsdata, dentry, sl,
188-
mode, CONFIGFS_ITEM_LINK);
194+
err = configfs_create(dentry, mode, init_symlink);
189195
if (!err)
190196
dentry->d_op = &configfs_dentry_ops;
197+
else {
198+
struct configfs_dirent *sd = dentry->d_fsdata;
199+
if (sd) {
200+
list_del_init(&sd->s_sibling);
201+
configfs_put(sd);
202+
}
203+
}
191204
}
192205
return err;
193206
}
@@ -241,13 +254,15 @@ static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * den
241254
struct configfs_attribute * attr = sd->s_element;
242255
int error;
243256

257+
dentry->d_fsdata = configfs_get(sd);
258+
sd->s_dentry = dentry;
244259
error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, init_file);
245-
if (error)
260+
if (error) {
261+
configfs_put(sd);
246262
return error;
263+
}
247264

248265
dentry->d_op = &configfs_dentry_ops;
249-
dentry->d_fsdata = configfs_get(sd);
250-
sd->s_dentry = dentry;
251266
d_rehash(dentry);
252267

253268
return 0;
@@ -839,6 +854,7 @@ struct inode_operations configfs_dir_inode_operations = {
839854
.symlink = configfs_symlink,
840855
.unlink = configfs_unlink,
841856
.lookup = configfs_lookup,
857+
.setattr = configfs_setattr,
842858
};
843859

844860
#if 0

‎fs/configfs/file.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#include <linux/fs.h>
2828
#include <linux/module.h>
29-
#include <linux/dnotify.h>
3029
#include <linux/slab.h>
3130
#include <asm/uaccess.h>
3231
#include <asm/semaphore.h>
@@ -150,7 +149,7 @@ configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *pp
150149
/**
151150
* fill_write_buffer - copy buffer from userspace.
152151
* @buffer: data buffer for file.
153-
* @userbuf: data from user.
152+
* @buf: data from user.
154153
* @count: number of bytes in @userbuf.
155154
*
156155
* Allocate @buffer->page if it hasn't been already, then
@@ -177,8 +176,9 @@ fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size
177176

178177
/**
179178
* flush_write_buffer - push buffer to config_item.
180-
* @file: file pointer.
179+
* @dentry: dentry to the attribute
181180
* @buffer: data buffer for file.
181+
* @count: number of bytes
182182
*
183183
* Get the correct pointers for the config_item and the attribute we're
184184
* dealing with, then call the store() method for the attribute,
@@ -217,15 +217,16 @@ static ssize_t
217217
configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
218218
{
219219
struct configfs_buffer * buffer = file->private_data;
220+
ssize_t len;
220221

221222
down(&buffer->sem);
222-
count = fill_write_buffer(buffer,buf,count);
223-
if (count > 0)
224-
count = flush_write_buffer(file->f_dentry,buffer,count);
225-
if (count > 0)
226-
*ppos += count;
223+
len = fill_write_buffer(buffer, buf, count);
224+
if (len > 0)
225+
len = flush_write_buffer(file->f_dentry, buffer, count);
226+
if (len > 0)
227+
*ppos += len;
227228
up(&buffer->sem);
228-
return count;
229+
return len;
229230
}
230231

231232
static int check_perm(struct inode * inode, struct file * file)

‎fs/configfs/inode.c‎

Lines changed: 108 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/pagemap.h>
3232
#include <linux/namei.h>
3333
#include <linux/backing-dev.h>
34+
#include <linux/capability.h>
3435

3536
#include <linux/configfs.h>
3637
#include "configfs_internal.h"
@@ -48,18 +49,107 @@ static struct backing_dev_info configfs_backing_dev_info = {
4849
.capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
4950
};
5051

51-
struct inode * configfs_new_inode(mode_t mode)
52+
static struct inode_operations configfs_inode_operations ={
53+
.setattr = configfs_setattr,
54+
};
55+
56+
int configfs_setattr(struct dentry * dentry, struct iattr * iattr)
57+
{
58+
struct inode * inode = dentry->d_inode;
59+
struct configfs_dirent * sd = dentry->d_fsdata;
60+
struct iattr * sd_iattr;
61+
unsigned int ia_valid = iattr->ia_valid;
62+
int error;
63+
64+
if (!sd)
65+
return -EINVAL;
66+
67+
sd_iattr = sd->s_iattr;
68+
69+
error = inode_change_ok(inode, iattr);
70+
if (error)
71+
return error;
72+
73+
error = inode_setattr(inode, iattr);
74+
if (error)
75+
return error;
76+
77+
if (!sd_iattr) {
78+
/* setting attributes for the first time, allocate now */
79+
sd_iattr = kmalloc(sizeof(struct iattr), GFP_KERNEL);
80+
if (!sd_iattr)
81+
return -ENOMEM;
82+
/* assign default attributes */
83+
memset(sd_iattr, 0, sizeof(struct iattr));
84+
sd_iattr->ia_mode = sd->s_mode;
85+
sd_iattr->ia_uid = 0;
86+
sd_iattr->ia_gid = 0;
87+
sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
88+
sd->s_iattr = sd_iattr;
89+
}
90+
91+
/* attributes were changed atleast once in past */
92+
93+
if (ia_valid & ATTR_UID)
94+
sd_iattr->ia_uid = iattr->ia_uid;
95+
if (ia_valid & ATTR_GID)
96+
sd_iattr->ia_gid = iattr->ia_gid;
97+
if (ia_valid & ATTR_ATIME)
98+
sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
99+
inode->i_sb->s_time_gran);
100+
if (ia_valid & ATTR_MTIME)
101+
sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
102+
inode->i_sb->s_time_gran);
103+
if (ia_valid & ATTR_CTIME)
104+
sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
105+
inode->i_sb->s_time_gran);
106+
if (ia_valid & ATTR_MODE) {
107+
umode_t mode = iattr->ia_mode;
108+
109+
if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
110+
mode &= ~S_ISGID;
111+
sd_iattr->ia_mode = sd->s_mode = mode;
112+
}
113+
114+
return error;
115+
}
116+
117+
static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
118+
{
119+
inode->i_mode = mode;
120+
inode->i_uid = 0;
121+
inode->i_gid = 0;
122+
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
123+
}
124+
125+
static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
126+
{
127+
inode->i_mode = iattr->ia_mode;
128+
inode->i_uid = iattr->ia_uid;
129+
inode->i_gid = iattr->ia_gid;
130+
inode->i_atime = iattr->ia_atime;
131+
inode->i_mtime = iattr->ia_mtime;
132+
inode->i_ctime = iattr->ia_ctime;
133+
}
134+
135+
struct inode * configfs_new_inode(mode_t mode, struct configfs_dirent * sd)
52136
{
53137
struct inode * inode = new_inode(configfs_sb);
54138
if (inode) {
55-
inode->i_mode = mode;
56-
inode->i_uid = 0;
57-
inode->i_gid = 0;
58139
inode->i_blksize = PAGE_CACHE_SIZE;
59140
inode->i_blocks = 0;
60-
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
61141
inode->i_mapping->a_ops = &configfs_aops;
62142
inode->i_mapping->backing_dev_info = &configfs_backing_dev_info;
143+
inode->i_op = &configfs_inode_operations;
144+
145+
if (sd->s_iattr) {
146+
/* sysfs_dirent has non-default attributes
147+
* get them for the new inode from persistent copy
148+
* in sysfs_dirent
149+
*/
150+
set_inode_attr(inode, sd->s_iattr);
151+
} else
152+
set_default_inode_attr(inode, mode);
63153
}
64154
return inode;
65155
}
@@ -70,7 +160,8 @@ int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *
70160
struct inode * inode = NULL;
71161
if (dentry) {
72162
if (!dentry->d_inode) {
73-
if ((inode = configfs_new_inode(mode))) {
163+
struct configfs_dirent *sd = dentry->d_fsdata;
164+
if ((inode = configfs_new_inode(mode, sd))) {
74165
if (dentry->d_parent && dentry->d_parent->d_inode) {
75166
struct inode *p_inode = dentry->d_parent->d_inode;
76167
p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
@@ -103,7 +194,7 @@ int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *
103194
*/
104195
const unsigned char * configfs_get_name(struct configfs_dirent *sd)
105196
{
106-
struct attribute * attr;
197+
struct configfs_attribute *attr;
107198

108199
if (!sd || !sd->s_element)
109200
BUG();
@@ -114,7 +205,7 @@ const unsigned char * configfs_get_name(struct configfs_dirent *sd)
114205

115206
if (sd->s_type & CONFIGFS_ITEM_ATTR) {
116207
attr = sd->s_element;
117-
return attr->name;
208+
return attr->ca_name;
118209
}
119210
return NULL;
120211
}
@@ -130,13 +221,17 @@ void configfs_drop_dentry(struct configfs_dirent * sd, struct dentry * parent)
130221

131222
if (dentry) {
132223
spin_lock(&dcache_lock);
224+
spin_lock(&dentry->d_lock);
133225
if (!(d_unhashed(dentry) && dentry->d_inode)) {
134226
dget_locked(dentry);
135227
__d_drop(dentry);
228+
spin_unlock(&dentry->d_lock);
136229
spin_unlock(&dcache_lock);
137230
simple_unlink(parent->d_inode, dentry);
138-
} else
231+
} else {
232+
spin_unlock(&dentry->d_lock);
139233
spin_unlock(&dcache_lock);
234+
}
140235
}
141236
}
142237

@@ -145,6 +240,10 @@ void configfs_hash_and_remove(struct dentry * dir, const char * name)
145240
struct configfs_dirent * sd;
146241
struct configfs_dirent * parent_sd = dir->d_fsdata;
147242

243+
if (dir->d_inode == NULL)
244+
/* no inode means this hasn't been made visible yet */
245+
return;
246+
148247
mutex_lock(&dir->d_inode->i_mutex);
149248
list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
150249
if (!sd->s_element)

0 commit comments

Comments
 (0)