1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Red Hat, Inc.
4 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
8 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/mount.h>
15 static ssize_t efivarfs_file_write(struct file *file,
16 const char __user *userbuf, size_t count, loff_t *ppos)
18 struct efivar_entry *var = file->private_data;
21 struct inode *inode = file->f_mapping->host;
22 unsigned long datasize = count - sizeof(attributes);
26 if (count < sizeof(attributes))
29 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
32 if (attributes & ~(EFI_VARIABLE_MASK))
35 data = memdup_user(userbuf + sizeof(attributes), datasize);
39 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
47 if (bytes == -ENOENT) {
49 d_delete(file->f_path.dentry);
50 dput(file->f_path.dentry);
53 i_size_write(inode, datasize + sizeof(attributes));
54 inode->i_mtime = current_time(inode);
66 static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
67 size_t count, loff_t *ppos)
69 struct efivar_entry *var = file->private_data;
70 unsigned long datasize = 0;
76 while (!__ratelimit(&file->f_cred->user->ratelimit))
79 err = efivar_entry_size(var, &datasize);
82 * efivarfs represents uncommitted variables with
83 * zero-length files. Reading them should return EOF.
90 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
95 size = efivar_entry_get(var, &attributes, &datasize,
96 data + sizeof(attributes));
100 memcpy(data, &attributes, sizeof(attributes));
101 size = simple_read_from_buffer(userbuf, count, ppos,
102 data, datasize + sizeof(attributes));
109 const struct file_operations efivarfs_file_operations = {
111 .read = efivarfs_file_read,
112 .write = efivarfs_file_write,