Merge branch 'x86-efi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / fs / efivarfs / file.c
CommitLineData
d68772b7
MF
1/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
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>
11#include <linux/fs.h>
12
13#include "internal.h"
14
d68772b7
MF
15static ssize_t efivarfs_file_write(struct file *file,
16 const char __user *userbuf, size_t count, loff_t *ppos)
17{
18 struct efivar_entry *var = file->private_data;
19 void *data;
20 u32 attributes;
21 struct inode *inode = file->f_mapping->host;
22 unsigned long datasize = count - sizeof(attributes);
23 ssize_t bytes = 0;
24 bool set = false;
25
26 if (count < sizeof(attributes))
27 return -EINVAL;
28
29 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
30 return -EFAULT;
31
32 if (attributes & ~(EFI_VARIABLE_MASK))
33 return -EINVAL;
34
35 data = kmalloc(datasize, GFP_KERNEL);
36 if (!data)
37 return -ENOMEM;
38
39 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
40 bytes = -EFAULT;
41 goto out;
42 }
43
44 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
45 data, &set);
46 if (!set && bytes)
47 goto out;
48
49 if (bytes == -ENOENT) {
50 drop_nlink(inode);
51 d_delete(file->f_dentry);
52 dput(file->f_dentry);
53 } else {
54 mutex_lock(&inode->i_mutex);
55 i_size_write(inode, datasize + sizeof(attributes));
56 mutex_unlock(&inode->i_mutex);
57 }
58
59 bytes = count;
60
61out:
62 kfree(data);
63
64 return bytes;
65}
66
67static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
68 size_t count, loff_t *ppos)
69{
70 struct efivar_entry *var = file->private_data;
71 unsigned long datasize = 0;
72 u32 attributes;
73 void *data;
74 ssize_t size = 0;
75 int err;
76
77 err = efivar_entry_size(var, &datasize);
78 if (err)
79 return err;
80
81 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
82
83 if (!data)
84 return -ENOMEM;
85
86 size = efivar_entry_get(var, &attributes, &datasize,
87 data + sizeof(attributes));
88 if (size)
89 goto out_free;
90
91 memcpy(data, &attributes, sizeof(attributes));
92 size = simple_read_from_buffer(userbuf, count, ppos,
93 data, datasize + sizeof(attributes));
94out_free:
95 kfree(data);
96
97 return size;
98}
99
100const struct file_operations efivarfs_file_operations = {
f53f292e 101 .open = simple_open,
d68772b7
MF
102 .read = efivarfs_file_read,
103 .write = efivarfs_file_write,
104 .llseek = no_llseek,
105};