inode.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/efi.h>
  10. #include <linux/fs.h>
  11. #include <linux/ctype.h>
  12. #include <linux/kmemleak.h>
  13. #include <linux/slab.h>
  14. #include <linux/uuid.h>
  15. #include "internal.h"
  16. struct inode *efivarfs_get_inode(struct super_block *sb,
  17. const struct inode *dir, int mode,
  18. dev_t dev, bool is_removable)
  19. {
  20. struct inode *inode = new_inode(sb);
  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(inode);
  25. inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
  26. switch (mode & S_IFMT) {
  27. case S_IFREG:
  28. inode->i_fop = &efivarfs_file_operations;
  29. break;
  30. case S_IFDIR:
  31. inode->i_op = &efivarfs_dir_inode_operations;
  32. inode->i_fop = &simple_dir_operations;
  33. inc_nlink(inode);
  34. break;
  35. }
  36. }
  37. return inode;
  38. }
  39. /*
  40. * Return true if 'str' is a valid efivarfs filename of the form,
  41. *
  42. * VariableName-12345678-1234-1234-1234-1234567891bc
  43. */
  44. bool efivarfs_valid_name(const char *str, int len)
  45. {
  46. const char *s = str + len - EFI_VARIABLE_GUID_LEN;
  47. /*
  48. * We need a GUID, plus at least one letter for the variable name,
  49. * plus the '-' separator
  50. */
  51. if (len < EFI_VARIABLE_GUID_LEN + 2)
  52. return false;
  53. /* GUID must be preceded by a '-' */
  54. if (*(s - 1) != '-')
  55. return false;
  56. /*
  57. * Validate that 's' is of the correct format, e.g.
  58. *
  59. * 12345678-1234-1234-1234-123456789abc
  60. */
  61. return uuid_is_valid(s);
  62. }
  63. static int efivarfs_create(struct inode *dir, struct dentry *dentry,
  64. umode_t mode, bool excl)
  65. {
  66. struct inode *inode = NULL;
  67. struct efivar_entry *var;
  68. int namelen, i = 0, err = 0;
  69. bool is_removable = false;
  70. if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
  71. return -EINVAL;
  72. var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
  73. if (!var)
  74. return -ENOMEM;
  75. /* length of the variable name itself: remove GUID and separator */
  76. namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
  77. err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
  78. if (err)
  79. goto out;
  80. if (efivar_variable_is_removable(var->var.VendorGuid,
  81. dentry->d_name.name, namelen))
  82. is_removable = true;
  83. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
  84. if (!inode) {
  85. err = -ENOMEM;
  86. goto out;
  87. }
  88. for (i = 0; i < namelen; i++)
  89. var->var.VariableName[i] = dentry->d_name.name[i];
  90. var->var.VariableName[i] = '\0';
  91. inode->i_private = var;
  92. kmemleak_ignore(var);
  93. err = efivar_entry_add(var, &efivarfs_list);
  94. if (err)
  95. goto out;
  96. d_instantiate(dentry, inode);
  97. dget(dentry);
  98. out:
  99. if (err) {
  100. kfree(var);
  101. if (inode)
  102. iput(inode);
  103. }
  104. return err;
  105. }
  106. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  107. {
  108. struct efivar_entry *var = d_inode(dentry)->i_private;
  109. if (efivar_entry_delete(var))
  110. return -EINVAL;
  111. drop_nlink(d_inode(dentry));
  112. dput(dentry);
  113. return 0;
  114. };
  115. const struct inode_operations efivarfs_dir_inode_operations = {
  116. .lookup = simple_lookup,
  117. .unlink = efivarfs_unlink,
  118. .create = efivarfs_create,
  119. };