2 * Copyright (C) 2012 Red Hat, Inc.
3 * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
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.
10 #include <linux/ctype.h>
11 #include <linux/efi.h>
13 #include <linux/module.h>
14 #include <linux/pagemap.h>
15 #include <linux/ucs2_string.h>
19 LIST_HEAD(efivarfs_list);
21 static void efivarfs_evict_inode(struct inode *inode)
26 static const struct super_operations efivarfs_ops = {
27 .statfs = simple_statfs,
28 .drop_inode = generic_delete_inode,
29 .evict_inode = efivarfs_evict_inode,
30 .show_options = generic_show_options,
33 static struct super_block *efivarfs_sb;
36 * Compare two efivarfs file names.
38 * An efivarfs filename is composed of two parts,
40 * 1. A case-sensitive variable name
41 * 2. A case-insensitive GUID
43 * So we need to perform a case-sensitive match on part 1 and a
44 * case-insensitive match on part 2.
46 static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
47 const struct dentry *dentry, const struct inode *inode,
48 unsigned int len, const char *str,
49 const struct qstr *name)
51 int guid = len - EFI_VARIABLE_GUID_LEN;
56 /* Case-sensitive compare for the variable name */
57 if (memcmp(str, name->name, guid))
60 /* Case-insensitive compare for the GUID */
61 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
64 static int efivarfs_d_hash(const struct dentry *dentry,
65 const struct inode *inode, struct qstr *qstr)
67 unsigned long hash = init_name_hash();
68 const unsigned char *s = qstr->name;
69 unsigned int len = qstr->len;
71 if (!efivarfs_valid_name(s, len))
74 while (len-- > EFI_VARIABLE_GUID_LEN)
75 hash = partial_name_hash(*s++, hash);
77 /* GUID is case-insensitive. */
79 hash = partial_name_hash(tolower(*s++), hash);
81 qstr->hash = end_name_hash(hash);
86 * Retaining negative dentries for an in-memory filesystem just wastes
87 * memory and lookup time: arrange for them to be deleted immediately.
89 static int efivarfs_delete_dentry(const struct dentry *dentry)
94 static struct dentry_operations efivarfs_d_ops = {
95 .d_compare = efivarfs_d_compare,
96 .d_hash = efivarfs_d_hash,
97 .d_delete = efivarfs_delete_dentry,
100 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
107 q.len = strlen(name);
109 err = efivarfs_d_hash(NULL, NULL, &q);
113 d = d_alloc(parent, &q);
117 return ERR_PTR(-ENOMEM);
120 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
121 unsigned long name_size, void *data)
123 struct super_block *sb = (struct super_block *)data;
124 struct efivar_entry *entry;
125 struct inode *inode = NULL;
126 struct dentry *dentry, *root = sb->s_root;
127 unsigned long size = 0;
132 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
136 memcpy(entry->var.VariableName, name16, name_size);
137 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
139 len = ucs2_strlen(entry->var.VariableName);
141 /* name, plus '-', plus GUID, plus NUL*/
142 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
146 for (i = 0; i < len; i++)
147 name[i] = entry->var.VariableName[i] & 0xFF;
151 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
153 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
155 inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
159 dentry = efivarfs_alloc_dentry(root, name);
160 if (IS_ERR(dentry)) {
161 err = PTR_ERR(dentry);
165 /* copied by the above to local storage in the dentry. */
168 efivar_entry_size(entry, &size);
169 efivar_entry_add(entry, &efivarfs_list);
171 mutex_lock(&inode->i_mutex);
172 inode->i_private = entry;
173 i_size_write(inode, size + sizeof(entry->var.Attributes));
174 mutex_unlock(&inode->i_mutex);
175 d_add(dentry, inode);
188 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
190 efivar_entry_remove(entry);
195 static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
197 struct inode *inode = NULL;
203 sb->s_maxbytes = MAX_LFS_FILESIZE;
204 sb->s_blocksize = PAGE_CACHE_SIZE;
205 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
206 sb->s_magic = EFIVARFS_MAGIC;
207 sb->s_op = &efivarfs_ops;
208 sb->s_d_op = &efivarfs_d_ops;
211 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
214 inode->i_op = &efivarfs_dir_inode_operations;
216 root = d_make_root(inode);
221 INIT_LIST_HEAD(&efivarfs_list);
223 err = efivar_init(efivarfs_callback, (void *)sb, false,
224 true, &efivarfs_list);
226 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
231 static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
232 int flags, const char *dev_name, void *data)
234 return mount_single(fs_type, flags, data, efivarfs_fill_super);
237 static void efivarfs_kill_sb(struct super_block *sb)
239 kill_litter_super(sb);
242 /* Remove all entries and destroy */
243 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
246 static struct file_system_type efivarfs_type = {
248 .mount = efivarfs_mount,
249 .kill_sb = efivarfs_kill_sb,
252 static __init int efivarfs_init(void)
254 if (!efi_enabled(EFI_RUNTIME_SERVICES))
257 if (!efivars_kobject())
260 return register_filesystem(&efivarfs_type);
263 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
264 MODULE_DESCRIPTION("EFI Variable Filesystem");
265 MODULE_LICENSE("GPL");
266 MODULE_ALIAS_FS("efivarfs");
268 module_init(efivarfs_init);