diff options
| author | Matt Fleming <matt.fleming@intel.com> | 2013-02-08 11:27:24 -0500 |
|---|---|---|
| committer | Matt Fleming <matt.fleming@intel.com> | 2013-04-17 08:25:09 -0400 |
| commit | d68772b7c83f4b518be15ae96f4827c8ed02f684 (patch) | |
| tree | b3408a750976edf6e0b62bfce3e26d1c9685a907 /fs/efivarfs/file.c | |
| parent | 048517722cde2595a7366d0c3c72b8b1ec142a9c (diff) | |
efivarfs: Move to fs/efivarfs
Now that efivarfs uses the efivar API, move it out of efivars.c and
into fs/efivarfs where it belongs. This move will eventually allow us
to enable the efivarfs code without having to also enable
CONFIG_EFI_VARS built, and vice versa.
Furthermore, things like,
mount -t efivarfs none /sys/firmware/efi/efivars
will now work if efivarfs is built as a module without requiring the
use of MODULE_ALIAS(), which would have been necessary when the
efivarfs code was part of efivars.c.
Cc: Matthew Garrett <matthew.garrett@nebula.com>
Cc: Jeremy Kerr <jk@ozlabs.org>
Reviewed-by: Tom Gundersen <teg@jklm.no>
Tested-by: Tom Gundersen <teg@jklm.no>
Signed-off-by: Matt Fleming <matt.fleming@intel.com>
Diffstat (limited to 'fs/efivarfs/file.c')
| -rw-r--r-- | fs/efivarfs/file.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/fs/efivarfs/file.c b/fs/efivarfs/file.c new file mode 100644 index 000000000000..aeb0368dace2 --- /dev/null +++ b/fs/efivarfs/file.c | |||
| @@ -0,0 +1,111 @@ | |||
| 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 | |||
| 15 | static int efivarfs_file_open(struct inode *inode, struct file *file) | ||
| 16 | { | ||
| 17 | file->private_data = inode->i_private; | ||
| 18 | return 0; | ||
| 19 | } | ||
| 20 | |||
| 21 | static ssize_t efivarfs_file_write(struct file *file, | ||
| 22 | const char __user *userbuf, size_t count, loff_t *ppos) | ||
| 23 | { | ||
| 24 | struct efivar_entry *var = file->private_data; | ||
| 25 | void *data; | ||
| 26 | u32 attributes; | ||
| 27 | struct inode *inode = file->f_mapping->host; | ||
| 28 | unsigned long datasize = count - sizeof(attributes); | ||
| 29 | ssize_t bytes = 0; | ||
| 30 | bool set = false; | ||
| 31 | |||
| 32 | if (count < sizeof(attributes)) | ||
| 33 | return -EINVAL; | ||
| 34 | |||
| 35 | if (copy_from_user(&attributes, userbuf, sizeof(attributes))) | ||
| 36 | return -EFAULT; | ||
| 37 | |||
| 38 | if (attributes & ~(EFI_VARIABLE_MASK)) | ||
| 39 | return -EINVAL; | ||
| 40 | |||
| 41 | data = kmalloc(datasize, GFP_KERNEL); | ||
| 42 | if (!data) | ||
| 43 | return -ENOMEM; | ||
| 44 | |||
| 45 | if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) { | ||
| 46 | bytes = -EFAULT; | ||
| 47 | goto out; | ||
| 48 | } | ||
| 49 | |||
| 50 | bytes = efivar_entry_set_get_size(var, attributes, &datasize, | ||
| 51 | data, &set); | ||
| 52 | if (!set && bytes) | ||
| 53 | goto out; | ||
| 54 | |||
| 55 | if (bytes == -ENOENT) { | ||
| 56 | drop_nlink(inode); | ||
| 57 | d_delete(file->f_dentry); | ||
| 58 | dput(file->f_dentry); | ||
| 59 | } else { | ||
| 60 | mutex_lock(&inode->i_mutex); | ||
| 61 | i_size_write(inode, datasize + sizeof(attributes)); | ||
| 62 | mutex_unlock(&inode->i_mutex); | ||
| 63 | } | ||
| 64 | |||
| 65 | bytes = count; | ||
| 66 | |||
| 67 | out: | ||
| 68 | kfree(data); | ||
| 69 | |||
| 70 | return bytes; | ||
| 71 | } | ||
| 72 | |||
| 73 | static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf, | ||
| 74 | size_t count, loff_t *ppos) | ||
| 75 | { | ||
| 76 | struct efivar_entry *var = file->private_data; | ||
| 77 | unsigned long datasize = 0; | ||
| 78 | u32 attributes; | ||
| 79 | void *data; | ||
| 80 | ssize_t size = 0; | ||
| 81 | int err; | ||
| 82 | |||
| 83 | err = efivar_entry_size(var, &datasize); | ||
| 84 | if (err) | ||
| 85 | return err; | ||
| 86 | |||
| 87 | data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL); | ||
| 88 | |||
| 89 | if (!data) | ||
| 90 | return -ENOMEM; | ||
| 91 | |||
| 92 | size = efivar_entry_get(var, &attributes, &datasize, | ||
| 93 | data + sizeof(attributes)); | ||
| 94 | if (size) | ||
| 95 | goto out_free; | ||
| 96 | |||
| 97 | memcpy(data, &attributes, sizeof(attributes)); | ||
| 98 | size = simple_read_from_buffer(userbuf, count, ppos, | ||
| 99 | data, datasize + sizeof(attributes)); | ||
| 100 | out_free: | ||
| 101 | kfree(data); | ||
| 102 | |||
| 103 | return size; | ||
| 104 | } | ||
| 105 | |||
| 106 | const struct file_operations efivarfs_file_operations = { | ||
| 107 | .open = efivarfs_file_open, | ||
| 108 | .read = efivarfs_file_read, | ||
| 109 | .write = efivarfs_file_write, | ||
| 110 | .llseek = no_llseek, | ||
| 111 | }; | ||
