Skip to content

Commit d701ea2

Browse files
author
Miklos Szeredi
committed
efivars: convert to fileattr
Use the fileattr API to let the VFS handle locking, permission checking and conversion. Signed-off-by: Miklos Szeredi <mszeredi@redhat.com> Cc: Matthew Garrett <mjg59@srcf.ucam.org>
1 parent 9fefd5d commit d701ea2

2 files changed

Lines changed: 44 additions & 77 deletions

File tree

fs/efivarfs/file.c

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -106,86 +106,9 @@ static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
106106
return size;
107107
}
108108

109-
static inline unsigned int efivarfs_getflags(struct inode *inode)
110-
{
111-
unsigned int i_flags;
112-
unsigned int flags = 0;
113-
114-
i_flags = inode->i_flags;
115-
if (i_flags & S_IMMUTABLE)
116-
flags |= FS_IMMUTABLE_FL;
117-
return flags;
118-
}
119-
120-
static int
121-
efivarfs_ioc_getxflags(struct file *file, void __user *arg)
122-
{
123-
struct inode *inode = file->f_mapping->host;
124-
unsigned int flags = efivarfs_getflags(inode);
125-
126-
if (copy_to_user(arg, &flags, sizeof(flags)))
127-
return -EFAULT;
128-
return 0;
129-
}
130-
131-
static int
132-
efivarfs_ioc_setxflags(struct file *file, void __user *arg)
133-
{
134-
struct inode *inode = file->f_mapping->host;
135-
unsigned int flags;
136-
unsigned int i_flags = 0;
137-
unsigned int oldflags = efivarfs_getflags(inode);
138-
int error;
139-
140-
if (!inode_owner_or_capable(&init_user_ns, inode))
141-
return -EACCES;
142-
143-
if (copy_from_user(&flags, arg, sizeof(flags)))
144-
return -EFAULT;
145-
146-
if (flags & ~FS_IMMUTABLE_FL)
147-
return -EOPNOTSUPP;
148-
149-
if (flags & FS_IMMUTABLE_FL)
150-
i_flags |= S_IMMUTABLE;
151-
152-
153-
error = mnt_want_write_file(file);
154-
if (error)
155-
return error;
156-
157-
inode_lock(inode);
158-
159-
error = vfs_ioc_setflags_prepare(inode, oldflags, flags);
160-
if (error)
161-
goto out;
162-
163-
inode_set_flags(inode, i_flags, S_IMMUTABLE);
164-
out:
165-
inode_unlock(inode);
166-
mnt_drop_write_file(file);
167-
return error;
168-
}
169-
170-
static long
171-
efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p)
172-
{
173-
void __user *arg = (void __user *)p;
174-
175-
switch (cmd) {
176-
case FS_IOC_GETFLAGS:
177-
return efivarfs_ioc_getxflags(file, arg);
178-
case FS_IOC_SETFLAGS:
179-
return efivarfs_ioc_setxflags(file, arg);
180-
}
181-
182-
return -ENOTTY;
183-
}
184-
185109
const struct file_operations efivarfs_file_operations = {
186110
.open = simple_open,
187111
.read = efivarfs_file_read,
188112
.write = efivarfs_file_write,
189113
.llseek = no_llseek,
190-
.unlocked_ioctl = efivarfs_file_ioctl,
191114
};

fs/efivarfs/inode.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#include <linux/kmemleak.h>
1111
#include <linux/slab.h>
1212
#include <linux/uuid.h>
13+
#include <linux/fileattr.h>
1314

1415
#include "internal.h"
1516

17+
static const struct inode_operations efivarfs_file_inode_operations;
18+
1619
struct inode *efivarfs_get_inode(struct super_block *sb,
1720
const struct inode *dir, int mode,
1821
dev_t dev, bool is_removable)
@@ -26,6 +29,7 @@ struct inode *efivarfs_get_inode(struct super_block *sb,
2629
inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
2730
switch (mode & S_IFMT) {
2831
case S_IFREG:
32+
inode->i_op = &efivarfs_file_inode_operations;
2933
inode->i_fop = &efivarfs_file_operations;
3034
break;
3135
case S_IFDIR:
@@ -138,3 +142,43 @@ const struct inode_operations efivarfs_dir_inode_operations = {
138142
.unlink = efivarfs_unlink,
139143
.create = efivarfs_create,
140144
};
145+
146+
static int
147+
efivarfs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
148+
{
149+
unsigned int i_flags;
150+
unsigned int flags = 0;
151+
152+
i_flags = d_inode(dentry)->i_flags;
153+
if (i_flags & S_IMMUTABLE)
154+
flags |= FS_IMMUTABLE_FL;
155+
156+
fileattr_fill_flags(fa, flags);
157+
158+
return 0;
159+
}
160+
161+
static int
162+
efivarfs_fileattr_set(struct user_namespace *mnt_userns,
163+
struct dentry *dentry, struct fileattr *fa)
164+
{
165+
unsigned int i_flags = 0;
166+
167+
if (fileattr_has_fsx(fa))
168+
return -EOPNOTSUPP;
169+
170+
if (fa->flags & ~FS_IMMUTABLE_FL)
171+
return -EOPNOTSUPP;
172+
173+
if (fa->flags & FS_IMMUTABLE_FL)
174+
i_flags |= S_IMMUTABLE;
175+
176+
inode_set_flags(d_inode(dentry), i_flags, S_IMMUTABLE);
177+
178+
return 0;
179+
}
180+
181+
static const struct inode_operations efivarfs_file_inode_operations = {
182+
.fileattr_get = efivarfs_fileattr_get,
183+
.fileattr_set = efivarfs_fileattr_set,
184+
};

0 commit comments

Comments
 (0)