Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / fs / efivarfs / file.c
blob8e568428c88be059299b5ada7eb48e7d289a04fe
1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
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.
8 */
10 #include <linux/efi.h>
11 #include <linux/delay.h>
12 #include <linux/fs.h>
13 #include <linux/slab.h>
14 #include <linux/mount.h>
16 #include "internal.h"
18 static ssize_t efivarfs_file_write(struct file *file,
19 const char __user *userbuf, size_t count, loff_t *ppos)
21 struct efivar_entry *var = file->private_data;
22 void *data;
23 u32 attributes;
24 struct inode *inode = file->f_mapping->host;
25 unsigned long datasize = count - sizeof(attributes);
26 ssize_t bytes;
27 bool set = false;
29 if (count < sizeof(attributes))
30 return -EINVAL;
32 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
33 return -EFAULT;
35 if (attributes & ~(EFI_VARIABLE_MASK))
36 return -EINVAL;
38 data = memdup_user(userbuf + sizeof(attributes), datasize);
39 if (IS_ERR(data))
40 return PTR_ERR(data);
42 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
43 data, &set);
44 if (!set && bytes) {
45 if (bytes == -ENOENT)
46 bytes = -EIO;
47 goto out;
50 if (bytes == -ENOENT) {
51 drop_nlink(inode);
52 d_delete(file->f_path.dentry);
53 dput(file->f_path.dentry);
54 } else {
55 inode_lock(inode);
56 i_size_write(inode, datasize + sizeof(attributes));
57 inode_unlock(inode);
60 bytes = count;
62 out:
63 kfree(data);
65 return bytes;
68 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
69 size_t count, loff_t *ppos)
71 struct efivar_entry *var = file->private_data;
72 unsigned long datasize = 0;
73 u32 attributes;
74 void *data;
75 ssize_t size = 0;
76 int err;
78 while (!__ratelimit(&file->f_cred->user->ratelimit)) {
79 if (!msleep_interruptible(50))
80 return -EINTR;
83 err = efivar_entry_size(var, &datasize);
86 * efivarfs represents uncommitted variables with
87 * zero-length files. Reading them should return EOF.
89 if (err == -ENOENT)
90 return 0;
91 else if (err)
92 return err;
94 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
96 if (!data)
97 return -ENOMEM;
99 size = efivar_entry_get(var, &attributes, &datasize,
100 data + sizeof(attributes));
101 if (size)
102 goto out_free;
104 memcpy(data, &attributes, sizeof(attributes));
105 size = simple_read_from_buffer(userbuf, count, ppos,
106 data, datasize + sizeof(attributes));
107 out_free:
108 kfree(data);
110 return size;
113 static int
114 efivarfs_ioc_getxflags(struct file *file, void __user *arg)
116 struct inode *inode = file->f_mapping->host;
117 unsigned int i_flags;
118 unsigned int flags = 0;
120 i_flags = inode->i_flags;
121 if (i_flags & S_IMMUTABLE)
122 flags |= FS_IMMUTABLE_FL;
124 if (copy_to_user(arg, &flags, sizeof(flags)))
125 return -EFAULT;
126 return 0;
129 static int
130 efivarfs_ioc_setxflags(struct file *file, void __user *arg)
132 struct inode *inode = file->f_mapping->host;
133 unsigned int flags;
134 unsigned int i_flags = 0;
135 int error;
137 if (!inode_owner_or_capable(inode))
138 return -EACCES;
140 if (copy_from_user(&flags, arg, sizeof(flags)))
141 return -EFAULT;
143 if (flags & ~FS_IMMUTABLE_FL)
144 return -EOPNOTSUPP;
146 if (!capable(CAP_LINUX_IMMUTABLE))
147 return -EPERM;
149 if (flags & FS_IMMUTABLE_FL)
150 i_flags |= S_IMMUTABLE;
153 error = mnt_want_write_file(file);
154 if (error)
155 return error;
157 inode_lock(inode);
158 inode_set_flags(inode, i_flags, S_IMMUTABLE);
159 inode_unlock(inode);
161 mnt_drop_write_file(file);
163 return 0;
166 static long
167 efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p)
169 void __user *arg = (void __user *)p;
171 switch (cmd) {
172 case FS_IOC_GETFLAGS:
173 return efivarfs_ioc_getxflags(file, arg);
174 case FS_IOC_SETFLAGS:
175 return efivarfs_ioc_setxflags(file, arg);
178 return -ENOTTY;
181 const struct file_operations efivarfs_file_operations = {
182 .open = simple_open,
183 .read = efivarfs_file_read,
184 .write = efivarfs_file_write,
185 .llseek = no_llseek,
186 .unlocked_ioctl = efivarfs_file_ioctl,