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
));
65 static ssize_t
efivarfs_file_read(struct file
*file
, char __user
*userbuf
,
66 size_t count
, loff_t
*ppos
)
68 struct efivar_entry
*var
= file
->private_data
;
69 unsigned long datasize
= 0;
75 while (!__ratelimit(&file
->f_cred
->user
->ratelimit
)) {
76 if (!msleep_interruptible(50))
80 err
= efivar_entry_size(var
, &datasize
);
83 * efivarfs represents uncommitted variables with
84 * zero-length files. Reading them should return EOF.
91 data
= kmalloc(datasize
+ sizeof(attributes
), GFP_KERNEL
);
96 size
= efivar_entry_get(var
, &attributes
, &datasize
,
97 data
+ sizeof(attributes
));
101 memcpy(data
, &attributes
, sizeof(attributes
));
102 size
= simple_read_from_buffer(userbuf
, count
, ppos
,
103 data
, datasize
+ sizeof(attributes
));
110 static inline unsigned int efivarfs_getflags(struct inode
*inode
)
112 unsigned int i_flags
;
113 unsigned int flags
= 0;
115 i_flags
= inode
->i_flags
;
116 if (i_flags
& S_IMMUTABLE
)
117 flags
|= FS_IMMUTABLE_FL
;
122 efivarfs_ioc_getxflags(struct file
*file
, void __user
*arg
)
124 struct inode
*inode
= file
->f_mapping
->host
;
125 unsigned int flags
= efivarfs_getflags(inode
);
127 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
133 efivarfs_ioc_setxflags(struct file
*file
, void __user
*arg
)
135 struct inode
*inode
= file
->f_mapping
->host
;
137 unsigned int i_flags
= 0;
138 unsigned int oldflags
= efivarfs_getflags(inode
);
141 if (!inode_owner_or_capable(inode
))
144 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
147 if (flags
& ~FS_IMMUTABLE_FL
)
150 if (flags
& FS_IMMUTABLE_FL
)
151 i_flags
|= S_IMMUTABLE
;
154 error
= mnt_want_write_file(file
);
160 error
= vfs_ioc_setflags_prepare(inode
, oldflags
, flags
);
164 inode_set_flags(inode
, i_flags
, S_IMMUTABLE
);
167 mnt_drop_write_file(file
);
172 efivarfs_file_ioctl(struct file
*file
, unsigned int cmd
, unsigned long p
)
174 void __user
*arg
= (void __user
*)p
;
177 case FS_IOC_GETFLAGS
:
178 return efivarfs_ioc_getxflags(file
, arg
);
179 case FS_IOC_SETFLAGS
:
180 return efivarfs_ioc_setxflags(file
, arg
);
186 const struct file_operations efivarfs_file_operations
= {
188 .read
= efivarfs_file_read
,
189 .write
= efivarfs_file_write
,
191 .unlocked_ioctl
= efivarfs_file_ioctl
,