super.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include <linux/ctype.h>
  10. #include <linux/efi.h>
  11. #include <linux/fs.h>
  12. #include <linux/module.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/ucs2_string.h>
  15. #include <linux/slab.h>
  16. #include <linux/magic.h>
  17. #include "internal.h"
  18. LIST_HEAD(efivarfs_list);
  19. static void efivarfs_evict_inode(struct inode *inode)
  20. {
  21. clear_inode(inode);
  22. }
  23. static const struct super_operations efivarfs_ops = {
  24. .statfs = simple_statfs,
  25. .drop_inode = generic_delete_inode,
  26. .evict_inode = efivarfs_evict_inode,
  27. };
  28. static struct super_block *efivarfs_sb;
  29. /*
  30. * Compare two efivarfs file names.
  31. *
  32. * An efivarfs filename is composed of two parts,
  33. *
  34. * 1. A case-sensitive variable name
  35. * 2. A case-insensitive GUID
  36. *
  37. * So we need to perform a case-sensitive match on part 1 and a
  38. * case-insensitive match on part 2.
  39. */
  40. static int efivarfs_d_compare(const struct dentry *dentry,
  41. unsigned int len, const char *str,
  42. const struct qstr *name)
  43. {
  44. int guid = len - EFI_VARIABLE_GUID_LEN;
  45. if (name->len != len)
  46. return 1;
  47. /* Case-sensitive compare for the variable name */
  48. if (memcmp(str, name->name, guid))
  49. return 1;
  50. /* Case-insensitive compare for the GUID */
  51. return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
  52. }
  53. static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
  54. {
  55. unsigned long hash = init_name_hash(dentry);
  56. const unsigned char *s = qstr->name;
  57. unsigned int len = qstr->len;
  58. if (!efivarfs_valid_name(s, len))
  59. return -EINVAL;
  60. while (len-- > EFI_VARIABLE_GUID_LEN)
  61. hash = partial_name_hash(*s++, hash);
  62. /* GUID is case-insensitive. */
  63. while (len--)
  64. hash = partial_name_hash(tolower(*s++), hash);
  65. qstr->hash = end_name_hash(hash);
  66. return 0;
  67. }
  68. static const struct dentry_operations efivarfs_d_ops = {
  69. .d_compare = efivarfs_d_compare,
  70. .d_hash = efivarfs_d_hash,
  71. .d_delete = always_delete_dentry,
  72. };
  73. static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
  74. {
  75. struct dentry *d;
  76. struct qstr q;
  77. int err;
  78. q.name = name;
  79. q.len = strlen(name);
  80. err = efivarfs_d_hash(parent, &q);
  81. if (err)
  82. return ERR_PTR(err);
  83. d = d_alloc(parent, &q);
  84. if (d)
  85. return d;
  86. return ERR_PTR(-ENOMEM);
  87. }
  88. static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
  89. unsigned long name_size, void *data)
  90. {
  91. struct super_block *sb = (struct super_block *)data;
  92. struct efivar_entry *entry;
  93. struct inode *inode = NULL;
  94. struct dentry *dentry, *root = sb->s_root;
  95. unsigned long size = 0;
  96. char *name;
  97. int len;
  98. int err = -ENOMEM;
  99. bool is_removable = false;
  100. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  101. if (!entry)
  102. return err;
  103. memcpy(entry->var.VariableName, name16, name_size);
  104. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  105. len = ucs2_utf8size(entry->var.VariableName);
  106. /* name, plus '-', plus GUID, plus NUL*/
  107. name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
  108. if (!name)
  109. goto fail;
  110. ucs2_as_utf8(name, entry->var.VariableName, len);
  111. if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
  112. is_removable = true;
  113. name[len] = '-';
  114. efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
  115. name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
  116. /* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
  117. strreplace(name, '/', '!');
  118. inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
  119. is_removable);
  120. if (!inode)
  121. goto fail_name;
  122. dentry = efivarfs_alloc_dentry(root, name);
  123. if (IS_ERR(dentry)) {
  124. err = PTR_ERR(dentry);
  125. goto fail_inode;
  126. }
  127. efivar_entry_size(entry, &size);
  128. err = efivar_entry_add(entry, &efivarfs_list);
  129. if (err)
  130. goto fail_inode;
  131. /* copied by the above to local storage in the dentry. */
  132. kfree(name);
  133. inode_lock(inode);
  134. inode->i_private = entry;
  135. i_size_write(inode, size + sizeof(entry->var.Attributes));
  136. inode_unlock(inode);
  137. d_add(dentry, inode);
  138. return 0;
  139. fail_inode:
  140. iput(inode);
  141. fail_name:
  142. kfree(name);
  143. fail:
  144. kfree(entry);
  145. return err;
  146. }
  147. static int efivarfs_destroy(struct efivar_entry *entry, void *data)
  148. {
  149. int err = efivar_entry_remove(entry);
  150. if (err)
  151. return err;
  152. kfree(entry);
  153. return 0;
  154. }
  155. static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  156. {
  157. struct inode *inode = NULL;
  158. struct dentry *root;
  159. int err;
  160. efivarfs_sb = sb;
  161. sb->s_maxbytes = MAX_LFS_FILESIZE;
  162. sb->s_blocksize = PAGE_SIZE;
  163. sb->s_blocksize_bits = PAGE_SHIFT;
  164. sb->s_magic = EFIVARFS_MAGIC;
  165. sb->s_op = &efivarfs_ops;
  166. sb->s_d_op = &efivarfs_d_ops;
  167. sb->s_time_gran = 1;
  168. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
  169. if (!inode)
  170. return -ENOMEM;
  171. inode->i_op = &efivarfs_dir_inode_operations;
  172. root = d_make_root(inode);
  173. sb->s_root = root;
  174. if (!root)
  175. return -ENOMEM;
  176. INIT_LIST_HEAD(&efivarfs_list);
  177. err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
  178. if (err)
  179. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  180. return err;
  181. }
  182. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  183. int flags, const char *dev_name, void *data)
  184. {
  185. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  186. }
  187. static void efivarfs_kill_sb(struct super_block *sb)
  188. {
  189. kill_litter_super(sb);
  190. efivarfs_sb = NULL;
  191. /* Remove all entries and destroy */
  192. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  193. }
  194. static struct file_system_type efivarfs_type = {
  195. .owner = THIS_MODULE,
  196. .name = "efivarfs",
  197. .mount = efivarfs_mount,
  198. .kill_sb = efivarfs_kill_sb,
  199. };
  200. static __init int efivarfs_init(void)
  201. {
  202. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  203. return -ENODEV;
  204. if (!efivars_kobject())
  205. return -ENODEV;
  206. return register_filesystem(&efivarfs_type);
  207. }
  208. static __exit void efivarfs_exit(void)
  209. {
  210. unregister_filesystem(&efivarfs_type);
  211. }
  212. MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
  213. MODULE_DESCRIPTION("EFI Variable Filesystem");
  214. MODULE_LICENSE("GPL");
  215. MODULE_ALIAS_FS("efivarfs");
  216. module_init(efivarfs_init);
  217. module_exit(efivarfs_exit);