2 * Copyright (C) 2012 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/efi.h>
12 #include <linux/slab.h>
13 #include <linux/mount.h>
17 static ssize_t efivarfs_file_write(struct file *file,
18 const char __user *userbuf, size_t count, loff_t *ppos)
20 struct efivar_entry *var = file->private_data;
23 struct inode *inode = file->f_mapping->host;
24 unsigned long datasize = count - sizeof(attributes);
28 if (count < sizeof(attributes))
31 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
34 if (attributes & ~(EFI_VARIABLE_MASK))
37 data = memdup_user(userbuf + sizeof(attributes), datasize);
41 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
49 if (bytes == -ENOENT) {
51 d_delete(file->f_path.dentry);
52 dput(file->f_path.dentry);
55 i_size_write(inode, datasize + sizeof(attributes));
67 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
68 size_t count, loff_t *ppos)
70 struct efivar_entry *var = file->private_data;
71 unsigned long datasize = 0;
77 err = efivar_entry_size(var, &datasize);
80 * efivarfs represents uncommitted variables with
81 * zero-length files. Reading them should return EOF.
88 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
93 size = efivar_entry_get(var, &attributes, &datasize,
94 data + sizeof(attributes));
98 memcpy(data, &attributes, sizeof(attributes));
99 size = simple_read_from_buffer(userbuf, count, ppos,
100 data, datasize + sizeof(attributes));
108 efivarfs_ioc_getxflags(struct file *file, void __user *arg)
110 struct inode *inode = file->f_mapping->host;
111 unsigned int i_flags;
112 unsigned int flags = 0;
114 i_flags = inode->i_flags;
115 if (i_flags & S_IMMUTABLE)
116 flags |= FS_IMMUTABLE_FL;
118 if (copy_to_user(arg, &flags, sizeof(flags)))
124 efivarfs_ioc_setxflags(struct file *file, void __user *arg)
126 struct inode *inode = file->f_mapping->host;
128 unsigned int i_flags = 0;
131 if (!inode_owner_or_capable(inode))
134 if (copy_from_user(&flags, arg, sizeof(flags)))
137 if (flags & ~FS_IMMUTABLE_FL)
140 if (!capable(CAP_LINUX_IMMUTABLE))
143 if (flags & FS_IMMUTABLE_FL)
144 i_flags |= S_IMMUTABLE;
147 error = mnt_want_write_file(file);
152 inode_set_flags(inode, i_flags, S_IMMUTABLE);
155 mnt_drop_write_file(file);
161 efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p)
163 void __user *arg = (void __user *)p;
166 case FS_IOC_GETFLAGS:
167 return efivarfs_ioc_getxflags(file, arg);
168 case FS_IOC_SETFLAGS:
169 return efivarfs_ioc_setxflags(file, arg);
175 const struct file_operations efivarfs_file_operations = {
177 .read = efivarfs_file_read,
178 .write = efivarfs_file_write,
180 .unlocked_ioctl = efivarfs_file_ioctl,