]>
Commit | Line | Data |
---|---|---|
d68772b7 MF |
1 | /* |
2 | * Copyright (C) 2012 Red Hat, Inc. | |
3 | * Copyright (C) 2012 Jeremy Kerr <[email protected]> | |
4 | * | |
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 | */ | |
9 | ||
10 | #include <linux/efi.h> | |
bef3efbe | 11 | #include <linux/delay.h> |
d68772b7 | 12 | #include <linux/fs.h> |
20b4fb48 | 13 | #include <linux/slab.h> |
ed8b0de5 | 14 | #include <linux/mount.h> |
d68772b7 MF |
15 | |
16 | #include "internal.h" | |
17 | ||
d68772b7 MF |
18 | static ssize_t efivarfs_file_write(struct file *file, |
19 | const char __user *userbuf, size_t count, loff_t *ppos) | |
20 | { | |
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); | |
aca32b57 | 26 | ssize_t bytes; |
d68772b7 MF |
27 | bool set = false; |
28 | ||
29 | if (count < sizeof(attributes)) | |
30 | return -EINVAL; | |
31 | ||
32 | if (copy_from_user(&attributes, userbuf, sizeof(attributes))) | |
33 | return -EFAULT; | |
34 | ||
35 | if (attributes & ~(EFI_VARIABLE_MASK)) | |
36 | return -EINVAL; | |
37 | ||
aca32b57 GB |
38 | data = memdup_user(userbuf + sizeof(attributes), datasize); |
39 | if (IS_ERR(data)) | |
40 | return PTR_ERR(data); | |
d68772b7 MF |
41 | |
42 | bytes = efivar_entry_set_get_size(var, attributes, &datasize, | |
43 | data, &set); | |
3fab70c1 LX |
44 | if (!set && bytes) { |
45 | if (bytes == -ENOENT) | |
46 | bytes = -EIO; | |
d68772b7 | 47 | goto out; |
3fab70c1 | 48 | } |
d68772b7 MF |
49 | |
50 | if (bytes == -ENOENT) { | |
51 | drop_nlink(inode); | |
b583043e AV |
52 | d_delete(file->f_path.dentry); |
53 | dput(file->f_path.dentry); | |
d68772b7 | 54 | } else { |
5955102c | 55 | inode_lock(inode); |
d68772b7 | 56 | i_size_write(inode, datasize + sizeof(attributes)); |
5955102c | 57 | inode_unlock(inode); |
d68772b7 MF |
58 | } |
59 | ||
60 | bytes = count; | |
61 | ||
62 | out: | |
63 | kfree(data); | |
64 | ||
65 | return bytes; | |
66 | } | |
67 | ||
68 | static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf, | |
69 | size_t count, loff_t *ppos) | |
70 | { | |
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; | |
77 | ||
bef3efbe TL |
78 | while (!__ratelimit(&file->f_cred->user->ratelimit)) { |
79 | if (!msleep_interruptible(50)) | |
80 | return -EINTR; | |
81 | } | |
82 | ||
d68772b7 | 83 | err = efivar_entry_size(var, &datasize); |
3fab70c1 LX |
84 | |
85 | /* | |
86 | * efivarfs represents uncommitted variables with | |
87 | * zero-length files. Reading them should return EOF. | |
88 | */ | |
89 | if (err == -ENOENT) | |
90 | return 0; | |
91 | else if (err) | |
d68772b7 MF |
92 | return err; |
93 | ||
94 | data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL); | |
95 | ||
96 | if (!data) | |
97 | return -ENOMEM; | |
98 | ||
99 | size = efivar_entry_get(var, &attributes, &datasize, | |
100 | data + sizeof(attributes)); | |
101 | if (size) | |
102 | goto out_free; | |
103 | ||
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); | |
109 | ||
110 | return size; | |
111 | } | |
112 | ||
5aca2842 | 113 | static inline unsigned int efivarfs_getflags(struct inode *inode) |
ed8b0de5 | 114 | { |
ed8b0de5 PJ |
115 | unsigned int i_flags; |
116 | unsigned int flags = 0; | |
117 | ||
118 | i_flags = inode->i_flags; | |
119 | if (i_flags & S_IMMUTABLE) | |
120 | flags |= FS_IMMUTABLE_FL; | |
5aca2842 DW |
121 | return flags; |
122 | } | |
123 | ||
124 | static int | |
125 | efivarfs_ioc_getxflags(struct file *file, void __user *arg) | |
126 | { | |
127 | struct inode *inode = file->f_mapping->host; | |
128 | unsigned int flags = efivarfs_getflags(inode); | |
ed8b0de5 PJ |
129 | |
130 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
131 | return -EFAULT; | |
132 | return 0; | |
133 | } | |
134 | ||
135 | static int | |
136 | efivarfs_ioc_setxflags(struct file *file, void __user *arg) | |
137 | { | |
138 | struct inode *inode = file->f_mapping->host; | |
139 | unsigned int flags; | |
140 | unsigned int i_flags = 0; | |
5aca2842 | 141 | unsigned int oldflags = efivarfs_getflags(inode); |
ed8b0de5 PJ |
142 | int error; |
143 | ||
144 | if (!inode_owner_or_capable(inode)) | |
145 | return -EACCES; | |
146 | ||
147 | if (copy_from_user(&flags, arg, sizeof(flags))) | |
148 | return -EFAULT; | |
149 | ||
150 | if (flags & ~FS_IMMUTABLE_FL) | |
151 | return -EOPNOTSUPP; | |
152 | ||
ed8b0de5 PJ |
153 | if (flags & FS_IMMUTABLE_FL) |
154 | i_flags |= S_IMMUTABLE; | |
155 | ||
156 | ||
157 | error = mnt_want_write_file(file); | |
158 | if (error) | |
159 | return error; | |
160 | ||
161 | inode_lock(inode); | |
5aca2842 DW |
162 | |
163 | error = vfs_ioc_setflags_prepare(inode, oldflags, flags); | |
164 | if (error) | |
165 | goto out; | |
166 | ||
ed8b0de5 | 167 | inode_set_flags(inode, i_flags, S_IMMUTABLE); |
5aca2842 | 168 | out: |
ed8b0de5 | 169 | inode_unlock(inode); |
ed8b0de5 | 170 | mnt_drop_write_file(file); |
5aca2842 | 171 | return error; |
ed8b0de5 PJ |
172 | } |
173 | ||
6c5450ef | 174 | static long |
ed8b0de5 PJ |
175 | efivarfs_file_ioctl(struct file *file, unsigned int cmd, unsigned long p) |
176 | { | |
177 | void __user *arg = (void __user *)p; | |
178 | ||
179 | switch (cmd) { | |
180 | case FS_IOC_GETFLAGS: | |
181 | return efivarfs_ioc_getxflags(file, arg); | |
182 | case FS_IOC_SETFLAGS: | |
183 | return efivarfs_ioc_setxflags(file, arg); | |
184 | } | |
185 | ||
186 | return -ENOTTY; | |
187 | } | |
188 | ||
d68772b7 | 189 | const struct file_operations efivarfs_file_operations = { |
f53f292e | 190 | .open = simple_open, |
d68772b7 MF |
191 | .read = efivarfs_file_read, |
192 | .write = efivarfs_file_write, | |
193 | .llseek = no_llseek, | |
ed8b0de5 | 194 | .unlocked_ioctl = efivarfs_file_ioctl, |
d68772b7 | 195 | }; |