inode.c 3.2 KB

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