Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 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/ctype.h> |
| 11 | #include <linux/efi.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/pagemap.h> |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame^] | 15 | #include <linux/ucs2_string.h> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 16 | |
| 17 | #include "internal.h" |
| 18 | |
| 19 | LIST_HEAD(efivarfs_list); |
| 20 | |
| 21 | static void efivarfs_evict_inode(struct inode *inode) |
| 22 | { |
| 23 | clear_inode(inode); |
| 24 | } |
| 25 | |
| 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, |
| 31 | }; |
| 32 | |
| 33 | static struct super_block *efivarfs_sb; |
| 34 | |
| 35 | /* |
| 36 | * Compare two efivarfs file names. |
| 37 | * |
| 38 | * An efivarfs filename is composed of two parts, |
| 39 | * |
| 40 | * 1. A case-sensitive variable name |
| 41 | * 2. A case-insensitive GUID |
| 42 | * |
| 43 | * So we need to perform a case-sensitive match on part 1 and a |
| 44 | * case-insensitive match on part 2. |
| 45 | */ |
| 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) |
| 50 | { |
| 51 | int guid = len - EFI_VARIABLE_GUID_LEN; |
| 52 | |
| 53 | if (name->len != len) |
| 54 | return 1; |
| 55 | |
| 56 | /* Case-sensitive compare for the variable name */ |
| 57 | if (memcmp(str, name->name, guid)) |
| 58 | return 1; |
| 59 | |
| 60 | /* Case-insensitive compare for the GUID */ |
| 61 | return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN); |
| 62 | } |
| 63 | |
| 64 | static int efivarfs_d_hash(const struct dentry *dentry, |
| 65 | const struct inode *inode, struct qstr *qstr) |
| 66 | { |
| 67 | unsigned long hash = init_name_hash(); |
| 68 | const unsigned char *s = qstr->name; |
| 69 | unsigned int len = qstr->len; |
| 70 | |
| 71 | if (!efivarfs_valid_name(s, len)) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | while (len-- > EFI_VARIABLE_GUID_LEN) |
| 75 | hash = partial_name_hash(*s++, hash); |
| 76 | |
| 77 | /* GUID is case-insensitive. */ |
| 78 | while (len--) |
| 79 | hash = partial_name_hash(tolower(*s++), hash); |
| 80 | |
| 81 | qstr->hash = end_name_hash(hash); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Retaining negative dentries for an in-memory filesystem just wastes |
| 87 | * memory and lookup time: arrange for them to be deleted immediately. |
| 88 | */ |
| 89 | static int efivarfs_delete_dentry(const struct dentry *dentry) |
| 90 | { |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 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, |
| 98 | }; |
| 99 | |
| 100 | static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name) |
| 101 | { |
| 102 | struct dentry *d; |
| 103 | struct qstr q; |
| 104 | int err; |
| 105 | |
| 106 | q.name = name; |
| 107 | q.len = strlen(name); |
| 108 | |
| 109 | err = efivarfs_d_hash(NULL, NULL, &q); |
| 110 | if (err) |
| 111 | return ERR_PTR(err); |
| 112 | |
| 113 | d = d_alloc(parent, &q); |
| 114 | if (d) |
| 115 | return d; |
| 116 | |
| 117 | return ERR_PTR(-ENOMEM); |
| 118 | } |
| 119 | |
| 120 | static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor, |
| 121 | unsigned long name_size, void *data) |
| 122 | { |
| 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; |
| 128 | char *name; |
| 129 | int len, i; |
| 130 | int err = -ENOMEM; |
| 131 | |
| 132 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 133 | if (!entry) |
| 134 | return err; |
| 135 | |
| 136 | memcpy(entry->var.VariableName, name16, name_size); |
| 137 | memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t)); |
| 138 | |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame^] | 139 | len = ucs2_strlen(entry->var.VariableName); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 140 | |
| 141 | /* name, plus '-', plus GUID, plus NUL*/ |
| 142 | name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL); |
| 143 | if (!name) |
| 144 | goto fail; |
| 145 | |
| 146 | for (i = 0; i < len; i++) |
| 147 | name[i] = entry->var.VariableName[i] & 0xFF; |
| 148 | |
| 149 | name[len] = '-'; |
| 150 | |
| 151 | efi_guid_unparse(&entry->var.VendorGuid, name + len + 1); |
| 152 | |
| 153 | name[len + EFI_VARIABLE_GUID_LEN+1] = '\0'; |
| 154 | |
| 155 | inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0); |
| 156 | if (!inode) |
| 157 | goto fail_name; |
| 158 | |
| 159 | dentry = efivarfs_alloc_dentry(root, name); |
| 160 | if (IS_ERR(dentry)) { |
| 161 | err = PTR_ERR(dentry); |
| 162 | goto fail_inode; |
| 163 | } |
| 164 | |
| 165 | /* copied by the above to local storage in the dentry. */ |
| 166 | kfree(name); |
| 167 | |
| 168 | efivar_entry_size(entry, &size); |
| 169 | efivar_entry_add(entry, &efivarfs_list); |
| 170 | |
| 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); |
| 176 | |
| 177 | return 0; |
| 178 | |
| 179 | fail_inode: |
| 180 | iput(inode); |
| 181 | fail_name: |
| 182 | kfree(name); |
| 183 | fail: |
| 184 | kfree(entry); |
| 185 | return err; |
| 186 | } |
| 187 | |
| 188 | static int efivarfs_destroy(struct efivar_entry *entry, void *data) |
| 189 | { |
| 190 | efivar_entry_remove(entry); |
| 191 | kfree(entry); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int efivarfs_fill_super(struct super_block *sb, void *data, int silent) |
| 196 | { |
| 197 | struct inode *inode = NULL; |
| 198 | struct dentry *root; |
| 199 | int err; |
| 200 | |
| 201 | efivarfs_sb = sb; |
| 202 | |
| 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; |
| 209 | sb->s_time_gran = 1; |
| 210 | |
| 211 | inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0); |
| 212 | if (!inode) |
| 213 | return -ENOMEM; |
| 214 | inode->i_op = &efivarfs_dir_inode_operations; |
| 215 | |
| 216 | root = d_make_root(inode); |
| 217 | sb->s_root = root; |
| 218 | if (!root) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | INIT_LIST_HEAD(&efivarfs_list); |
| 222 | |
| 223 | err = efivar_init(efivarfs_callback, (void *)sb, false, |
| 224 | true, &efivarfs_list); |
| 225 | if (err) |
| 226 | __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL); |
| 227 | |
| 228 | return err; |
| 229 | } |
| 230 | |
| 231 | static struct dentry *efivarfs_mount(struct file_system_type *fs_type, |
| 232 | int flags, const char *dev_name, void *data) |
| 233 | { |
| 234 | return mount_single(fs_type, flags, data, efivarfs_fill_super); |
| 235 | } |
| 236 | |
| 237 | static void efivarfs_kill_sb(struct super_block *sb) |
| 238 | { |
| 239 | kill_litter_super(sb); |
| 240 | efivarfs_sb = NULL; |
| 241 | |
| 242 | /* Remove all entries and destroy */ |
| 243 | __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL); |
| 244 | } |
| 245 | |
| 246 | static struct file_system_type efivarfs_type = { |
| 247 | .name = "efivarfs", |
| 248 | .mount = efivarfs_mount, |
| 249 | .kill_sb = efivarfs_kill_sb, |
| 250 | }; |
| 251 | |
| 252 | static __init int efivarfs_init(void) |
| 253 | { |
| 254 | if (!efi_enabled(EFI_RUNTIME_SERVICES)) |
| 255 | return 0; |
| 256 | |
| 257 | if (!efivars_kobject()) |
| 258 | return 0; |
| 259 | |
| 260 | return register_filesystem(&efivarfs_type); |
| 261 | } |
| 262 | |
| 263 | MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr"); |
| 264 | MODULE_DESCRIPTION("EFI Variable Filesystem"); |
| 265 | MODULE_LICENSE("GPL"); |
| 266 | MODULE_ALIAS_FS("efivarfs"); |
| 267 | |
| 268 | module_init(efivarfs_init); |