aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Fleming <matt.fleming@intel.com>2013-02-08 11:27:24 -0500
committerMatt Fleming <matt.fleming@intel.com>2013-04-17 08:25:09 -0400
commitd68772b7c83f4b518be15ae96f4827c8ed02f684 (patch)
treeb3408a750976edf6e0b62bfce3e26d1c9685a907
parent048517722cde2595a7366d0c3c72b8b1ec142a9c (diff)
efivarfs: Move to fs/efivarfs
Now that efivarfs uses the efivar API, move it out of efivars.c and into fs/efivarfs where it belongs. This move will eventually allow us to enable the efivarfs code without having to also enable CONFIG_EFI_VARS built, and vice versa. Furthermore, things like, mount -t efivarfs none /sys/firmware/efi/efivars will now work if efivarfs is built as a module without requiring the use of MODULE_ALIAS(), which would have been necessary when the efivarfs code was part of efivars.c. Cc: Matthew Garrett <matthew.garrett@nebula.com> Cc: Jeremy Kerr <jk@ozlabs.org> Reviewed-by: Tom Gundersen <teg@jklm.no> Tested-by: Tom Gundersen <teg@jklm.no> Signed-off-by: Matt Fleming <matt.fleming@intel.com>
-rw-r--r--MAINTAINERS9
-rw-r--r--drivers/firmware/efivars.c496
-rw-r--r--fs/Kconfig1
-rw-r--r--fs/Makefile1
-rw-r--r--fs/efivarfs/Kconfig12
-rw-r--r--fs/efivarfs/Makefile7
-rw-r--r--fs/efivarfs/file.c111
-rw-r--r--fs/efivarfs/inode.c173
-rw-r--r--fs/efivarfs/internal.h22
-rw-r--r--fs/efivarfs/super.c267
10 files changed, 603 insertions, 496 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index 9e4862bf9961..0855f4450e91 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2990,6 +2990,15 @@ F: arch/x86/platform/efi/*
2990F: drivers/firmware/efivars.c 2990F: drivers/firmware/efivars.c
2991F: include/linux/efi*.h 2991F: include/linux/efi*.h
2992 2992
2993EFI VARIABLE FILESYSTEM
2994M: Matthew Garrett <matthew.garrett@nebula.com>
2995M: Jeremy Kerr <jk@ozlabs.org>
2996M: Matt Fleming <matt.fleming@intel.com>
2997T: git git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/efi.git
2998L: linux-efi@vger.kernel.org
2999S: Maintained
3000F: fs/efivarfs/
3001
2993EFIFB FRAMEBUFFER DRIVER 3002EFIFB FRAMEBUFFER DRIVER
2994L: linux-fbdev@vger.kernel.org 3003L: linux-fbdev@vger.kernel.org
2995M: Peter Jones <pjones@redhat.com> 3004M: Peter Jones <pjones@redhat.com>
diff --git a/drivers/firmware/efivars.c b/drivers/firmware/efivars.c
index 5e7c3b1acde9..af396758482f 100644
--- a/drivers/firmware/efivars.c
+++ b/drivers/firmware/efivars.c
@@ -94,7 +94,6 @@ MODULE_DESCRIPTION("sysfs interface to EFI Variables");
94MODULE_LICENSE("GPL"); 94MODULE_LICENSE("GPL");
95MODULE_VERSION(EFIVARS_VERSION); 95MODULE_VERSION(EFIVARS_VERSION);
96 96
97static LIST_HEAD(efivarfs_list);
98LIST_HEAD(efivar_sysfs_list); 97LIST_HEAD(efivar_sysfs_list);
99EXPORT_SYMBOL_GPL(efivar_sysfs_list); 98EXPORT_SYMBOL_GPL(efivar_sysfs_list);
100 99
@@ -558,12 +557,6 @@ static struct kobj_type efivar_ktype = {
558 .default_attrs = def_attrs, 557 .default_attrs = def_attrs,
559}; 558};
560 559
561static int efivarfs_file_open(struct inode *inode, struct file *file)
562{
563 file->private_data = inode->i_private;
564 return 0;
565}
566
567static int efi_status_to_err(efi_status_t status) 560static int efi_status_to_err(efi_status_t status)
568{ 561{
569 int err; 562 int err;
@@ -597,493 +590,6 @@ static int efi_status_to_err(efi_status_t status)
597 return err; 590 return err;
598} 591}
599 592
600static ssize_t efivarfs_file_write(struct file *file,
601 const char __user *userbuf, size_t count, loff_t *ppos)
602{
603 struct efivar_entry *var = file->private_data;
604 void *data;
605 u32 attributes;
606 struct inode *inode = file->f_mapping->host;
607 unsigned long datasize = count - sizeof(attributes);
608 ssize_t bytes = 0;
609 bool set = false;
610
611 if (count < sizeof(attributes))
612 return -EINVAL;
613
614 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
615 return -EFAULT;
616
617 if (attributes & ~(EFI_VARIABLE_MASK))
618 return -EINVAL;
619
620 data = kmalloc(datasize, GFP_KERNEL);
621 if (!data)
622 return -ENOMEM;
623
624 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
625 bytes = -EFAULT;
626 goto out;
627 }
628
629 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
630 data, &set);
631 if (!set && bytes)
632 goto out;
633
634 if (!bytes) {
635 mutex_lock(&inode->i_mutex);
636 i_size_write(inode, datasize + sizeof(attributes));
637 mutex_unlock(&inode->i_mutex);
638 } else if (bytes == -ENOENT) {
639 drop_nlink(inode);
640 d_delete(file->f_dentry);
641 dput(file->f_dentry);
642 } else
643 pr_warn("efivarfs: inconsistent EFI variable implementation? "
644 "status=%zu\n", bytes);
645
646 bytes = count;
647
648out:
649 kfree(data);
650
651 return bytes;
652}
653
654static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
655 size_t count, loff_t *ppos)
656{
657 struct efivar_entry *var = file->private_data;
658 unsigned long datasize = 0;
659 u32 attributes;
660 void *data;
661 ssize_t size = 0;
662 int err;
663
664 err = efivar_entry_size(var, &datasize);
665 if (err)
666 return err;
667
668 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
669
670 if (!data)
671 return -ENOMEM;
672
673 size = efivar_entry_get(var, &attributes, &datasize,
674 data + sizeof(attributes));
675 if (size)
676 goto out_free;
677
678 memcpy(data, &attributes, sizeof(attributes));
679 size = simple_read_from_buffer(userbuf, count, ppos,
680 data, datasize + sizeof(attributes));
681out_free:
682 kfree(data);
683
684 return size;
685}
686
687static void efivarfs_evict_inode(struct inode *inode)
688{
689 clear_inode(inode);
690}
691
692static const struct super_operations efivarfs_ops = {
693 .statfs = simple_statfs,
694 .drop_inode = generic_delete_inode,
695 .evict_inode = efivarfs_evict_inode,
696 .show_options = generic_show_options,
697};
698
699static struct super_block *efivarfs_sb;
700
701static const struct inode_operations efivarfs_dir_inode_operations;
702
703static const struct file_operations efivarfs_file_operations = {
704 .open = efivarfs_file_open,
705 .read = efivarfs_file_read,
706 .write = efivarfs_file_write,
707 .llseek = no_llseek,
708};
709
710static struct inode *efivarfs_get_inode(struct super_block *sb,
711 const struct inode *dir, int mode, dev_t dev)
712{
713 struct inode *inode = new_inode(sb);
714
715 if (inode) {
716 inode->i_ino = get_next_ino();
717 inode->i_mode = mode;
718 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
719 switch (mode & S_IFMT) {
720 case S_IFREG:
721 inode->i_fop = &efivarfs_file_operations;
722 break;
723 case S_IFDIR:
724 inode->i_op = &efivarfs_dir_inode_operations;
725 inode->i_fop = &simple_dir_operations;
726 inc_nlink(inode);
727 break;
728 }
729 }
730 return inode;
731}
732
733/*
734 * Return true if 'str' is a valid efivarfs filename of the form,
735 *
736 * VariableName-12345678-1234-1234-1234-1234567891bc
737 */
738static bool efivarfs_valid_name(const char *str, int len)
739{
740 static const char dashes[EFI_VARIABLE_GUID_LEN] = {
741 [8] = 1, [13] = 1, [18] = 1, [23] = 1
742 };
743 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
744 int i;
745
746 /*
747 * We need a GUID, plus at least one letter for the variable name,
748 * plus the '-' separator
749 */
750 if (len < EFI_VARIABLE_GUID_LEN + 2)
751 return false;
752
753 /* GUID must be preceded by a '-' */
754 if (*(s - 1) != '-')
755 return false;
756
757 /*
758 * Validate that 's' is of the correct format, e.g.
759 *
760 * 12345678-1234-1234-1234-123456789abc
761 */
762 for (i = 0; i < EFI_VARIABLE_GUID_LEN; i++) {
763 if (dashes[i]) {
764 if (*s++ != '-')
765 return false;
766 } else {
767 if (!isxdigit(*s++))
768 return false;
769 }
770 }
771
772 return true;
773}
774
775static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
776{
777 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
778 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
779 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
780 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
781 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
782 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
783 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
784 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
785 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
786 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
787 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
788 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
789 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
790 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
791 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
792 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
793}
794
795static int efivarfs_create(struct inode *dir, struct dentry *dentry,
796 umode_t mode, bool excl)
797{
798 struct inode *inode;
799 struct efivar_entry *var;
800 int namelen, i = 0, err = 0;
801
802 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
803 return -EINVAL;
804
805 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
806 if (!inode)
807 return -ENOMEM;
808
809 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
810 if (!var) {
811 err = -ENOMEM;
812 goto out;
813 }
814
815 /* length of the variable name itself: remove GUID and separator */
816 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
817
818 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
819 &var->var.VendorGuid);
820
821 for (i = 0; i < namelen; i++)
822 var->var.VariableName[i] = dentry->d_name.name[i];
823
824 var->var.VariableName[i] = '\0';
825
826 inode->i_private = var;
827
828 efivar_entry_add(var, &efivarfs_list);
829 d_instantiate(dentry, inode);
830 dget(dentry);
831out:
832 if (err) {
833 kfree(var);
834 iput(inode);
835 }
836 return err;
837}
838
839static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
840{
841 struct efivar_entry *var = dentry->d_inode->i_private;
842
843 if (efivar_entry_delete(var))
844 return -EINVAL;
845
846 drop_nlink(dentry->d_inode);
847 dput(dentry);
848 return 0;
849};
850
851/*
852 * Compare two efivarfs file names.
853 *
854 * An efivarfs filename is composed of two parts,
855 *
856 * 1. A case-sensitive variable name
857 * 2. A case-insensitive GUID
858 *
859 * So we need to perform a case-sensitive match on part 1 and a
860 * case-insensitive match on part 2.
861 */
862static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
863 const struct dentry *dentry, const struct inode *inode,
864 unsigned int len, const char *str,
865 const struct qstr *name)
866{
867 int guid = len - EFI_VARIABLE_GUID_LEN;
868
869 if (name->len != len)
870 return 1;
871
872 /* Case-sensitive compare for the variable name */
873 if (memcmp(str, name->name, guid))
874 return 1;
875
876 /* Case-insensitive compare for the GUID */
877 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
878}
879
880static int efivarfs_d_hash(const struct dentry *dentry,
881 const struct inode *inode, struct qstr *qstr)
882{
883 unsigned long hash = init_name_hash();
884 const unsigned char *s = qstr->name;
885 unsigned int len = qstr->len;
886
887 if (!efivarfs_valid_name(s, len))
888 return -EINVAL;
889
890 while (len-- > EFI_VARIABLE_GUID_LEN)
891 hash = partial_name_hash(*s++, hash);
892
893 /* GUID is case-insensitive. */
894 while (len--)
895 hash = partial_name_hash(tolower(*s++), hash);
896
897 qstr->hash = end_name_hash(hash);
898 return 0;
899}
900
901/*
902 * Retaining negative dentries for an in-memory filesystem just wastes
903 * memory and lookup time: arrange for them to be deleted immediately.
904 */
905static int efivarfs_delete_dentry(const struct dentry *dentry)
906{
907 return 1;
908}
909
910static struct dentry_operations efivarfs_d_ops = {
911 .d_compare = efivarfs_d_compare,
912 .d_hash = efivarfs_d_hash,
913 .d_delete = efivarfs_delete_dentry,
914};
915
916static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
917{
918 struct dentry *d;
919 struct qstr q;
920 int err;
921
922 q.name = name;
923 q.len = strlen(name);
924
925 err = efivarfs_d_hash(NULL, NULL, &q);
926 if (err)
927 return ERR_PTR(err);
928
929 d = d_alloc(parent, &q);
930 if (d)
931 return d;
932
933 return ERR_PTR(-ENOMEM);
934}
935
936static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
937 unsigned long name_size, void *data)
938{
939 struct super_block *sb = (struct super_block *)data;
940 struct efivar_entry *entry;
941 struct inode *inode = NULL;
942 struct dentry *dentry, *root = sb->s_root;
943 unsigned long size = 0;
944 char *name;
945 int len, i;
946 int err = -ENOMEM;
947
948 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
949 if (!entry)
950 return err;
951
952 memcpy(entry->var.VariableName, name16, name_size);
953 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
954
955 len = utf16_strlen(entry->var.VariableName);
956
957 /* name, plus '-', plus GUID, plus NUL*/
958 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
959 if (!name)
960 goto fail;
961
962 for (i = 0; i < len; i++)
963 name[i] = entry->var.VariableName[i] & 0xFF;
964
965 name[len] = '-';
966
967 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
968
969 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
970
971 inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
972 if (!inode)
973 goto fail_name;
974
975 dentry = efivarfs_alloc_dentry(root, name);
976 if (IS_ERR(dentry)) {
977 err = PTR_ERR(dentry);
978 goto fail_inode;
979 }
980
981 /* copied by the above to local storage in the dentry. */
982 kfree(name);
983
984 efivar_entry_size(entry, &size);
985 efivar_entry_add(entry, &efivarfs_list);
986
987 mutex_lock(&inode->i_mutex);
988 inode->i_private = entry;
989 i_size_write(inode, size + sizeof(entry->var.Attributes));
990 mutex_unlock(&inode->i_mutex);
991 d_add(dentry, inode);
992
993 return 0;
994
995fail_inode:
996 iput(inode);
997fail_name:
998 kfree(name);
999fail:
1000 kfree(entry);
1001 return err;
1002}
1003
1004static int efivarfs_destroy(struct efivar_entry *entry, void *data)
1005{
1006 efivar_entry_remove(entry);
1007 kfree(entry);
1008 return 0;
1009}
1010
1011static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
1012{
1013 struct inode *inode = NULL;
1014 struct dentry *root;
1015 int err;
1016
1017 efivarfs_sb = sb;
1018
1019 sb->s_maxbytes = MAX_LFS_FILESIZE;
1020 sb->s_blocksize = PAGE_CACHE_SIZE;
1021 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1022 sb->s_magic = EFIVARFS_MAGIC;
1023 sb->s_op = &efivarfs_ops;
1024 sb->s_d_op = &efivarfs_d_ops;
1025 sb->s_time_gran = 1;
1026
1027 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
1028 if (!inode)
1029 return -ENOMEM;
1030 inode->i_op = &efivarfs_dir_inode_operations;
1031
1032 root = d_make_root(inode);
1033 sb->s_root = root;
1034 if (!root)
1035 return -ENOMEM;
1036
1037 INIT_LIST_HEAD(&efivarfs_list);
1038
1039 err = efivar_init(efivarfs_callback, (void *)sb, false,
1040 true, &efivarfs_list);
1041 if (err)
1042 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
1043
1044 return err;
1045}
1046
1047static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
1048 int flags, const char *dev_name, void *data)
1049{
1050 return mount_single(fs_type, flags, data, efivarfs_fill_super);
1051}
1052
1053static void efivarfs_kill_sb(struct super_block *sb)
1054{
1055 kill_litter_super(sb);
1056 efivarfs_sb = NULL;
1057
1058 /* Remove all entries and destroy */
1059 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
1060}
1061
1062static struct file_system_type efivarfs_type = {
1063 .name = "efivarfs",
1064 .mount = efivarfs_mount,
1065 .kill_sb = efivarfs_kill_sb,
1066};
1067MODULE_ALIAS_FS("efivarfs");
1068
1069/*
1070 * Handle negative dentry.
1071 */
1072static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
1073 unsigned int flags)
1074{
1075 if (dentry->d_name.len > NAME_MAX)
1076 return ERR_PTR(-ENAMETOOLONG);
1077 d_add(dentry, NULL);
1078 return NULL;
1079}
1080
1081static const struct inode_operations efivarfs_dir_inode_operations = {
1082 .lookup = efivarfs_lookup,
1083 .unlink = efivarfs_unlink,
1084 .create = efivarfs_create,
1085};
1086
1087static ssize_t efivar_create(struct file *filp, struct kobject *kobj, 593static ssize_t efivar_create(struct file *filp, struct kobject *kobj,
1088 struct bin_attribute *bin_attr, 594 struct bin_attribute *bin_attr,
1089 char *buf, loff_t pos, size_t count) 595 char *buf, loff_t pos, size_t count)
@@ -2164,8 +1670,6 @@ int efivars_register(struct efivars *efivars,
2164 1670
2165 __efivars = efivars; 1671 __efivars = efivars;
2166 1672
2167 register_filesystem(&efivarfs_type);
2168
2169 return 0; 1673 return 0;
2170} 1674}
2171EXPORT_SYMBOL_GPL(efivars_register); 1675EXPORT_SYMBOL_GPL(efivars_register);
diff --git a/fs/Kconfig b/fs/Kconfig
index 780725a463b1..c229f828eb01 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -211,6 +211,7 @@ source "fs/sysv/Kconfig"
211source "fs/ufs/Kconfig" 211source "fs/ufs/Kconfig"
212source "fs/exofs/Kconfig" 212source "fs/exofs/Kconfig"
213source "fs/f2fs/Kconfig" 213source "fs/f2fs/Kconfig"
214source "fs/efivarfs/Kconfig"
214 215
215endif # MISC_FILESYSTEMS 216endif # MISC_FILESYSTEMS
216 217
diff --git a/fs/Makefile b/fs/Makefile
index 9d53192236fc..0fde6a369a72 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -127,3 +127,4 @@ obj-$(CONFIG_F2FS_FS) += f2fs/
127obj-y += exofs/ # Multiple modules 127obj-y += exofs/ # Multiple modules
128obj-$(CONFIG_CEPH_FS) += ceph/ 128obj-$(CONFIG_CEPH_FS) += ceph/
129obj-$(CONFIG_PSTORE) += pstore/ 129obj-$(CONFIG_PSTORE) += pstore/
130obj-$(CONFIG_EFIVAR_FS) += efivarfs/
diff --git a/fs/efivarfs/Kconfig b/fs/efivarfs/Kconfig
new file mode 100644
index 000000000000..1fb2b7f849f3
--- /dev/null
+++ b/fs/efivarfs/Kconfig
@@ -0,0 +1,12 @@
1config EFIVAR_FS
2 tristate "EFI Variable filesystem"
3 depends on EFI_VARS
4 help
5 efivarfs is a replacement filesystem for the old EFI
6 variable support via sysfs, as it doesn't suffer from the
7 same 1024-byte variable size limit.
8
9 To compile this file system support as a module, choose M
10 here. The module will be called efivarfs.
11
12 If unsure, say N.
diff --git a/fs/efivarfs/Makefile b/fs/efivarfs/Makefile
new file mode 100644
index 000000000000..955d478177d5
--- /dev/null
+++ b/fs/efivarfs/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the efivarfs filesystem
3#
4
5obj-$(CONFIG_EFIVAR_FS) += efivarfs.o
6
7efivarfs-objs := inode.o file.o super.o
diff --git a/fs/efivarfs/file.c b/fs/efivarfs/file.c
new file mode 100644
index 000000000000..aeb0368dace2
--- /dev/null
+++ b/fs/efivarfs/file.c
@@ -0,0 +1,111 @@
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/efi.h>
11#include <linux/fs.h>
12
13#include "internal.h"
14
15static int efivarfs_file_open(struct inode *inode, struct file *file)
16{
17 file->private_data = inode->i_private;
18 return 0;
19}
20
21static ssize_t efivarfs_file_write(struct file *file,
22 const char __user *userbuf, size_t count, loff_t *ppos)
23{
24 struct efivar_entry *var = file->private_data;
25 void *data;
26 u32 attributes;
27 struct inode *inode = file->f_mapping->host;
28 unsigned long datasize = count - sizeof(attributes);
29 ssize_t bytes = 0;
30 bool set = false;
31
32 if (count < sizeof(attributes))
33 return -EINVAL;
34
35 if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
36 return -EFAULT;
37
38 if (attributes & ~(EFI_VARIABLE_MASK))
39 return -EINVAL;
40
41 data = kmalloc(datasize, GFP_KERNEL);
42 if (!data)
43 return -ENOMEM;
44
45 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
46 bytes = -EFAULT;
47 goto out;
48 }
49
50 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
51 data, &set);
52 if (!set && bytes)
53 goto out;
54
55 if (bytes == -ENOENT) {
56 drop_nlink(inode);
57 d_delete(file->f_dentry);
58 dput(file->f_dentry);
59 } else {
60 mutex_lock(&inode->i_mutex);
61 i_size_write(inode, datasize + sizeof(attributes));
62 mutex_unlock(&inode->i_mutex);
63 }
64
65 bytes = count;
66
67out:
68 kfree(data);
69
70 return bytes;
71}
72
73static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
74 size_t count, loff_t *ppos)
75{
76 struct efivar_entry *var = file->private_data;
77 unsigned long datasize = 0;
78 u32 attributes;
79 void *data;
80 ssize_t size = 0;
81 int err;
82
83 err = efivar_entry_size(var, &datasize);
84 if (err)
85 return err;
86
87 data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
88
89 if (!data)
90 return -ENOMEM;
91
92 size = efivar_entry_get(var, &attributes, &datasize,
93 data + sizeof(attributes));
94 if (size)
95 goto out_free;
96
97 memcpy(data, &attributes, sizeof(attributes));
98 size = simple_read_from_buffer(userbuf, count, ppos,
99 data, datasize + sizeof(attributes));
100out_free:
101 kfree(data);
102
103 return size;
104}
105
106const struct file_operations efivarfs_file_operations = {
107 .open = efivarfs_file_open,
108 .read = efivarfs_file_read,
109 .write = efivarfs_file_write,
110 .llseek = no_llseek,
111};
diff --git a/fs/efivarfs/inode.c b/fs/efivarfs/inode.c
new file mode 100644
index 000000000000..640e289d522e
--- /dev/null
+++ b/fs/efivarfs/inode.c
@@ -0,0 +1,173 @@
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/efi.h>
11#include <linux/fs.h>
12#include <linux/ctype.h>
13
14#include "internal.h"
15
16struct inode *efivarfs_get_inode(struct super_block *sb,
17 const struct inode *dir, int mode, dev_t dev)
18{
19 struct inode *inode = new_inode(sb);
20
21 if (inode) {
22 inode->i_ino = get_next_ino();
23 inode->i_mode = mode;
24 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
25 switch (mode & S_IFMT) {
26 case S_IFREG:
27 inode->i_fop = &efivarfs_file_operations;
28 break;
29 case S_IFDIR:
30 inode->i_op = &efivarfs_dir_inode_operations;
31 inode->i_fop = &simple_dir_operations;
32 inc_nlink(inode);
33 break;
34 }
35 }
36 return inode;
37}
38
39/*
40 * Return true if 'str' is a valid efivarfs filename of the form,
41 *
42 * VariableName-12345678-1234-1234-1234-1234567891bc
43 */
44bool efivarfs_valid_name(const char *str, int len)
45{
46 static const char dashes[EFI_VARIABLE_GUID_LEN] = {
47 [8] = 1, [13] = 1, [18] = 1, [23] = 1
48 };
49 const char *s = str + len - EFI_VARIABLE_GUID_LEN;
50 int i;
51
52 /*
53 * We need a GUID, plus at least one letter for the variable name,
54 * plus the '-' separator
55 */
56 if (len < EFI_VARIABLE_GUID_LEN + 2)
57 return false;
58
59 /* GUID must be preceded by a '-' */
60 if (*(s - 1) != '-')
61 return false;
62
63 /*
64 * Validate that 's' is of the correct format, e.g.
65 *
66 * 12345678-1234-1234-1234-123456789abc
67 */
68 for (i = 0; i < EFI_VARIABLE_GUID_LEN; i++) {
69 if (dashes[i]) {
70 if (*s++ != '-')
71 return false;
72 } else {
73 if (!isxdigit(*s++))
74 return false;
75 }
76 }
77
78 return true;
79}
80
81static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)
82{
83 guid->b[0] = hex_to_bin(str[6]) << 4 | hex_to_bin(str[7]);
84 guid->b[1] = hex_to_bin(str[4]) << 4 | hex_to_bin(str[5]);
85 guid->b[2] = hex_to_bin(str[2]) << 4 | hex_to_bin(str[3]);
86 guid->b[3] = hex_to_bin(str[0]) << 4 | hex_to_bin(str[1]);
87 guid->b[4] = hex_to_bin(str[11]) << 4 | hex_to_bin(str[12]);
88 guid->b[5] = hex_to_bin(str[9]) << 4 | hex_to_bin(str[10]);
89 guid->b[6] = hex_to_bin(str[16]) << 4 | hex_to_bin(str[17]);
90 guid->b[7] = hex_to_bin(str[14]) << 4 | hex_to_bin(str[15]);
91 guid->b[8] = hex_to_bin(str[19]) << 4 | hex_to_bin(str[20]);
92 guid->b[9] = hex_to_bin(str[21]) << 4 | hex_to_bin(str[22]);
93 guid->b[10] = hex_to_bin(str[24]) << 4 | hex_to_bin(str[25]);
94 guid->b[11] = hex_to_bin(str[26]) << 4 | hex_to_bin(str[27]);
95 guid->b[12] = hex_to_bin(str[28]) << 4 | hex_to_bin(str[29]);
96 guid->b[13] = hex_to_bin(str[30]) << 4 | hex_to_bin(str[31]);
97 guid->b[14] = hex_to_bin(str[32]) << 4 | hex_to_bin(str[33]);
98 guid->b[15] = hex_to_bin(str[34]) << 4 | hex_to_bin(str[35]);
99}
100
101static int efivarfs_create(struct inode *dir, struct dentry *dentry,
102 umode_t mode, bool excl)
103{
104 struct inode *inode;
105 struct efivar_entry *var;
106 int namelen, i = 0, err = 0;
107
108 if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
109 return -EINVAL;
110
111 inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0);
112 if (!inode)
113 return -ENOMEM;
114
115 var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
116 if (!var) {
117 err = -ENOMEM;
118 goto out;
119 }
120
121 /* length of the variable name itself: remove GUID and separator */
122 namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
123
124 efivarfs_hex_to_guid(dentry->d_name.name + namelen + 1,
125 &var->var.VendorGuid);
126
127 for (i = 0; i < namelen; i++)
128 var->var.VariableName[i] = dentry->d_name.name[i];
129
130 var->var.VariableName[i] = '\0';
131
132 inode->i_private = var;
133
134 efivar_entry_add(var, &efivarfs_list);
135 d_instantiate(dentry, inode);
136 dget(dentry);
137out:
138 if (err) {
139 kfree(var);
140 iput(inode);
141 }
142 return err;
143}
144
145static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
146{
147 struct efivar_entry *var = dentry->d_inode->i_private;
148
149 if (efivar_entry_delete(var))
150 return -EINVAL;
151
152 drop_nlink(dentry->d_inode);
153 dput(dentry);
154 return 0;
155};
156
157/*
158 * Handle negative dentry.
159 */
160static struct dentry *efivarfs_lookup(struct inode *dir, struct dentry *dentry,
161 unsigned int flags)
162{
163 if (dentry->d_name.len > NAME_MAX)
164 return ERR_PTR(-ENAMETOOLONG);
165 d_add(dentry, NULL);
166 return NULL;
167}
168
169const struct inode_operations efivarfs_dir_inode_operations = {
170 .lookup = efivarfs_lookup,
171 .unlink = efivarfs_unlink,
172 .create = efivarfs_create,
173};
diff --git a/fs/efivarfs/internal.h b/fs/efivarfs/internal.h
new file mode 100644
index 000000000000..b5ff16addb7c
--- /dev/null
+++ b/fs/efivarfs/internal.h
@@ -0,0 +1,22 @@
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#ifndef EFIVAR_FS_INTERNAL_H
10#define EFIVAR_FS_INTERNAL_H
11
12#include <linux/list.h>
13
14extern const struct file_operations efivarfs_file_operations;
15extern const struct inode_operations efivarfs_dir_inode_operations;
16extern bool efivarfs_valid_name(const char *str, int len);
17extern struct inode *efivarfs_get_inode(struct super_block *sb,
18 const struct inode *dir, int mode, dev_t dev);
19
20extern struct list_head efivarfs_list;
21
22#endif /* EFIVAR_FS_INTERNAL_H */
diff --git a/fs/efivarfs/super.c b/fs/efivarfs/super.c
new file mode 100644
index 000000000000..34c48f1fcdbc
--- /dev/null
+++ b/fs/efivarfs/super.c
@@ -0,0 +1,267 @@
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>
15
16#include "internal.h"
17
18LIST_HEAD(efivarfs_list);
19
20static void efivarfs_evict_inode(struct inode *inode)
21{
22 clear_inode(inode);
23}
24
25static const struct super_operations efivarfs_ops = {
26 .statfs = simple_statfs,
27 .drop_inode = generic_delete_inode,
28 .evict_inode = efivarfs_evict_inode,
29 .show_options = generic_show_options,
30};
31
32static struct super_block *efivarfs_sb;
33
34/*
35 * Compare two efivarfs file names.
36 *
37 * An efivarfs filename is composed of two parts,
38 *
39 * 1. A case-sensitive variable name
40 * 2. A case-insensitive GUID
41 *
42 * So we need to perform a case-sensitive match on part 1 and a
43 * case-insensitive match on part 2.
44 */
45static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
46 const struct dentry *dentry, const struct inode *inode,
47 unsigned int len, const char *str,
48 const struct qstr *name)
49{
50 int guid = len - EFI_VARIABLE_GUID_LEN;
51
52 if (name->len != len)
53 return 1;
54
55 /* Case-sensitive compare for the variable name */
56 if (memcmp(str, name->name, guid))
57 return 1;
58
59 /* Case-insensitive compare for the GUID */
60 return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
61}
62
63static int efivarfs_d_hash(const struct dentry *dentry,
64 const struct inode *inode, struct qstr *qstr)
65{
66 unsigned long hash = init_name_hash();
67 const unsigned char *s = qstr->name;
68 unsigned int len = qstr->len;
69
70 if (!efivarfs_valid_name(s, len))
71 return -EINVAL;
72
73 while (len-- > EFI_VARIABLE_GUID_LEN)
74 hash = partial_name_hash(*s++, hash);
75
76 /* GUID is case-insensitive. */
77 while (len--)
78 hash = partial_name_hash(tolower(*s++), hash);
79
80 qstr->hash = end_name_hash(hash);
81 return 0;
82}
83
84/*
85 * Retaining negative dentries for an in-memory filesystem just wastes
86 * memory and lookup time: arrange for them to be deleted immediately.
87 */
88static int efivarfs_delete_dentry(const struct dentry *dentry)
89{
90 return 1;
91}
92
93static struct dentry_operations efivarfs_d_ops = {
94 .d_compare = efivarfs_d_compare,
95 .d_hash = efivarfs_d_hash,
96 .d_delete = efivarfs_delete_dentry,
97};
98
99static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
100{
101 struct dentry *d;
102 struct qstr q;
103 int err;
104
105 q.name = name;
106 q.len = strlen(name);
107
108 err = efivarfs_d_hash(NULL, NULL, &q);
109 if (err)
110 return ERR_PTR(err);
111
112 d = d_alloc(parent, &q);
113 if (d)
114 return d;
115
116 return ERR_PTR(-ENOMEM);
117}
118
119static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
120 unsigned long name_size, void *data)
121{
122 struct super_block *sb = (struct super_block *)data;
123 struct efivar_entry *entry;
124 struct inode *inode = NULL;
125 struct dentry *dentry, *root = sb->s_root;
126 unsigned long size = 0;
127 char *name;
128 int len, i;
129 int err = -ENOMEM;
130
131 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
132 if (!entry)
133 return err;
134
135 memcpy(entry->var.VariableName, name16, name_size);
136 memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
137
138 len = utf16_strlen(entry->var.VariableName);
139
140 /* name, plus '-', plus GUID, plus NUL*/
141 name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
142 if (!name)
143 goto fail;
144
145 for (i = 0; i < len; i++)
146 name[i] = entry->var.VariableName[i] & 0xFF;
147
148 name[len] = '-';
149
150 efi_guid_unparse(&entry->var.VendorGuid, name + len + 1);
151
152 name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
153
154 inode = efivarfs_get_inode(sb, root->d_inode, S_IFREG | 0644, 0);
155 if (!inode)
156 goto fail_name;
157
158 dentry = efivarfs_alloc_dentry(root, name);
159 if (IS_ERR(dentry)) {
160 err = PTR_ERR(dentry);
161 goto fail_inode;
162 }
163
164 /* copied by the above to local storage in the dentry. */
165 kfree(name);
166
167 efivar_entry_size(entry, &size);
168 efivar_entry_add(entry, &efivarfs_list);
169
170 mutex_lock(&inode->i_mutex);
171 inode->i_private = entry;
172 i_size_write(inode, size + sizeof(entry->var.Attributes));
173 mutex_unlock(&inode->i_mutex);
174 d_add(dentry, inode);
175
176 return 0;
177
178fail_inode:
179 iput(inode);
180fail_name:
181 kfree(name);
182fail:
183 kfree(entry);
184 return err;
185}
186
187static int efivarfs_destroy(struct efivar_entry *entry, void *data)
188{
189 efivar_entry_remove(entry);
190 kfree(entry);
191 return 0;
192}
193
194static int efivarfs_fill_super(struct super_block *sb, void *data, int silent)
195{
196 struct inode *inode = NULL;
197 struct dentry *root;
198 int err;
199
200 efivarfs_sb = sb;
201
202 sb->s_maxbytes = MAX_LFS_FILESIZE;
203 sb->s_blocksize = PAGE_CACHE_SIZE;
204 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
205 sb->s_magic = EFIVARFS_MAGIC;
206 sb->s_op = &efivarfs_ops;
207 sb->s_d_op = &efivarfs_d_ops;
208 sb->s_time_gran = 1;
209
210 inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0);
211 if (!inode)
212 return -ENOMEM;
213 inode->i_op = &efivarfs_dir_inode_operations;
214
215 root = d_make_root(inode);
216 sb->s_root = root;
217 if (!root)
218 return -ENOMEM;
219
220 INIT_LIST_HEAD(&efivarfs_list);
221
222 err = efivar_init(efivarfs_callback, (void *)sb, false,
223 true, &efivarfs_list);
224 if (err)
225 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
226
227 return err;
228}
229
230static struct dentry *efivarfs_mount(struct file_system_type *fs_type,
231 int flags, const char *dev_name, void *data)
232{
233 return mount_single(fs_type, flags, data, efivarfs_fill_super);
234}
235
236static void efivarfs_kill_sb(struct super_block *sb)
237{
238 kill_litter_super(sb);
239 efivarfs_sb = NULL;
240
241 /* Remove all entries and destroy */
242 __efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL, NULL);
243}
244
245static struct file_system_type efivarfs_type = {
246 .name = "efivarfs",
247 .mount = efivarfs_mount,
248 .kill_sb = efivarfs_kill_sb,
249};
250
251static __init int efivarfs_init(void)
252{
253 if (!efi_enabled(EFI_RUNTIME_SERVICES))
254 return 0;
255
256 if (!efivars_kobject())
257 return 0;
258
259 return register_filesystem(&efivarfs_type);
260}
261
262MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
263MODULE_DESCRIPTION("EFI Variable Filesystem");
264MODULE_LICENSE("GPL");
265MODULE_ALIAS_FS("efivarfs");
266
267module_init(efivarfs_init);