super.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. .show_options = generic_show_options,
  28. };
  29. static struct super_block *efivarfs_sb;
  30. /*
  31. * Compare two efivarfs file names.
  32. *
  33. * An efivarfs filename is composed of two parts,
  34. *
  35. * 1. A case-sensitive variable name
  36. * 2. A case-insensitive GUID
  37. *
  38. * So we need to perform a case-sensitive match on part 1 and a
  39. * case-insensitive match on part 2.
  40. */
  41. static int efivarfs_d_compare(const struct dentry *parent,
  42. const struct dentry *dentry,
  43. unsigned int len, const char *str,
  44. const struct qstr *name)
  45. {
  46. int guid = len - EFI_VARIABLE_GUID_LEN;
  47. if (name->len != len)
  48. return 1;
  49. /* Case-sensitive compare for the variable name */
  50. if (memcmp(str, name->name, guid))
  51. return 1;
  52. /* Case-insensitive compare for the GUID */
  53. return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
  54. }
  55. static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
  56. {
  57. unsigned long hash = init_name_hash();
  58. const unsigned char *s = qstr->name;
  59. unsigned int len = qstr->len;
  60. if (!efivarfs_valid_name(s, len))
  61. return -EINVAL;
  62. while (len-- > EFI_VARIABLE_GUID_LEN)
  63. hash = partial_name_hash(*s++, hash);
  64. /* GUID is case-insensitive. */
  65. while (len--)
  66. hash = partial_name_hash(tolower(*s++), hash);
  67. qstr->hash = end_name_hash(hash);
  68. return 0;
  69. }
  70. /*
  71. * Retaining negative dentries for an in-memory filesystem just wastes
  72. * memory and lookup time: arrange for them to be deleted immediately.
  73. */
  74. static int efivarfs_delete_dentry(const struct dentry *dentry)
  75. {
  76. return 1;
  77. }
  78. static struct dentry_operations efivarfs_d_ops = {
  79. .d_compare = efivarfs_d_compare,
  80. .d_hash = efivarfs_d_hash,
  81. .d_delete = efivarfs_delete_dentry,
  82. };
  83. static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
  84. {
  85. struct dentry *d;
  86. struct qstr q;
  87. int err;
  88. q.name = name;
  89. q.len = strlen(name);
  90. err = efivarfs_d_hash(NULL, &q);
  91. if (err)
  92. return ERR_PTR(err);
  93. d = d_alloc(parent, &q);
  94. if (d)
  95. return d;
  96. return ERR_PTR(-ENOMEM);
  97. }
  98. static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
  99. unsigned long name_size, void *data)
  100. {
  101. struct super_block *sb = (struct super_block *)data;
  102. struct efivar_entry *entry;
  103. struct inode *inode = NULL;
  104. struct dentry *dentry, *root = sb->s_root;
  105. unsigned long size = 0;
  106. char *name;
  107. int len, i;
  108. int err = -ENOMEM;
  109. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  110. if (!entry)
  111. return err;
  112. memcpy(entry->var.VariableName, name16, name_size);
  113. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  114. len = ucs2_strlen(entry->var.VariableName);
  115. /* name, plus '-', plus GUID, plus NUL*/
  116. name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
  117. if (!name)
  118. goto fail;
  119. for (i = 0; i < len; i++)
  120. name[i] = entry->var.VariableName[i] & 0xFF;
  121. name[len] = '-';
  122. efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
  123. name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
  124. inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
  125. if (!inode)
  126. goto fail_name;
  127. dentry = efivarfs_alloc_dentry(root, name);
  128. if (IS_ERR(dentry)) {
  129. err = PTR_ERR(dentry);
  130. goto fail_inode;
  131. }
  132. /* copied by the above to local storage in the dentry. */
  133. kfree(name);
  134. efivar_entry_size(entry, &size);
  135. efivar_entry_add(entry, &efivarfs_list);
  136. mutex_lock(&inode->i_mutex);
  137. inode->i_private = entry;
  138. i_size_write(inode, size + sizeof(entry->var.Attributes));
  139. mutex_unlock(&inode->i_mutex);
  140. d_add(dentry, inode);
  141. return 0;
  142. fail_inode:
  143. iput(inode);
  144. fail_name:
  145. kfree(name);
  146. fail:
  147. kfree(entry);
  148. return err;
  149. }
  150. static int efivarfs_destroy(struct efivar_entry *entry, void *data)
  151. {
  152. efivar_entry_remove(entry);
  153. kfree(entry);
  154. return 0;
  155. }
  156. static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
  157. {
  158. struct inode *inode = NULL;
  159. struct dentry *root;
  160. int err;
  161. efivarfs_sb = sb;
  162. sb->s_maxbytes = MAX_LFS_FILESIZE;
  163. sb->s_blocksize = PAGE_CACHE_SIZE;
  164. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  165. sb->s_magic = EFIVARFS_MAGIC;
  166. sb->s_op = &efivarfs_ops;
  167. sb->s_d_op = &efivarfs_d_ops;
  168. sb->s_time_gran = 1;
  169. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
  170. if (!inode)
  171. return -ENOMEM;
  172. inode->i_op = &efivarfs_dir_inode_operations;
  173. root = d_make_root(inode);
  174. sb->s_root = root;
  175. if (!root)
  176. return -ENOMEM;
  177. INIT_LIST_HEAD(&efivarfs_list);
  178. err = efivar_init(efivarfs_callback, (void *)sb, false,
  179. true, &efivarfs_list);
  180. if (err)
  181. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  182. return err;
  183. }
  184. static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
  185. int flags, const char *dev_name, void *data)
  186. {
  187. return mount_single(fs_type, flags, data, efivarfs_fill_super);
  188. }
  189. static void efivarfs_kill_sb(struct super_block *sb)
  190. {
  191. kill_litter_super(sb);
  192. efivarfs_sb = NULL;
  193. /* Remove all entries and destroy */
  194. __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
  195. }
  196. static struct file_system_type efivarfs_type = {
  197. .name = "efivarfs",
  198. .mount = efivarfs_mount,
  199. .kill_sb = efivarfs_kill_sb,
  200. };
  201. static __init int efivarfs_init(void)
  202. {
  203. if (!efi_enabled(EFI_RUNTIME_SERVICES))
  204. return 0;
  205. if (!efivars_kobject())
  206. return 0;
  207. return register_filesystem(&efivarfs_type);
  208. }
  209. MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
  210. MODULE_DESCRIPTION("EFI Variable Filesystem");
  211. MODULE_LICENSE("GPL");
  212. MODULE_ALIAS_FS("efivarfs");
  213. module_init(efivarfs_init);