inode.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. uuid_le_to_bin(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
  77. if (efivar_variable_is_removable(var->var.VendorGuid,
  78. dentry->d_name.name, namelen))
  79. is_removable = true;
  80. inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
  81. if (!inode) {
  82. err = -ENOMEM;
  83. goto out;
  84. }
  85. for (i = 0; i < namelen; i++)
  86. var->var.VariableName[i] = dentry->d_name.name[i];
  87. var->var.VariableName[i] = '\0';
  88. inode->i_private = var;
  89. err = efivar_entry_add(var, &efivarfs_list);
  90. if (err)
  91. goto out;
  92. d_instantiate(dentry, inode);
  93. dget(dentry);
  94. out:
  95. if (err) {
  96. kfree(var);
  97. if (inode)
  98. iput(inode);
  99. }
  100. return err;
  101. }
  102. static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
  103. {
  104. struct efivar_entry *var = d_inode(dentry)->i_private;
  105. if (efivar_entry_delete(var))
  106. return -EINVAL;
  107. drop_nlink(d_inode(dentry));
  108. dput(dentry);
  109. return 0;
  110. };
  111. const struct inode_operations efivarfs_dir_inode_operations = {
  112. .lookup = simple_lookup,
  113. .unlink = efivarfs_unlink,
  114. .create = efivarfs_create,
  115. };