file.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/slab.h>
  12. #include "internal.h"
  13. static ssize_t efivarfs_file_write(struct file *file,
  14. const char __user *userbuf, size_t count, loff_t *ppos)
  15. {
  16. struct efivar_entry *var = file->private_data;
  17. void *data;
  18. u32 attributes;
  19. struct inode *inode = file->f_mapping->host;
  20. unsigned long datasize = count - sizeof(attributes);
  21. ssize_t bytes;
  22. bool set = false;
  23. if (count < sizeof(attributes))
  24. return -EINVAL;
  25. if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
  26. return -EFAULT;
  27. if (attributes & ~(EFI_VARIABLE_MASK))
  28. return -EINVAL;
  29. data = memdup_user(userbuf + sizeof(attributes), datasize);
  30. if (IS_ERR(data))
  31. return PTR_ERR(data);
  32. bytes = efivar_entry_set_get_size(var, attributes, &datasize,
  33. data, &set);
  34. if (!set && bytes) {
  35. if (bytes == -ENOENT)
  36. bytes = -EIO;
  37. goto out;
  38. }
  39. if (bytes == -ENOENT) {
  40. drop_nlink(inode);
  41. d_delete(file->f_path.dentry);
  42. dput(file->f_path.dentry);
  43. } else {
  44. mutex_lock(&inode->i_mutex);
  45. i_size_write(inode, datasize + sizeof(attributes));
  46. mutex_unlock(&inode->i_mutex);
  47. }
  48. bytes = count;
  49. out:
  50. kfree(data);
  51. return bytes;
  52. }
  53. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  54. size_t count, loff_t *ppos)
  55. {
  56. struct efivar_entry *var = file->private_data;
  57. unsigned long datasize = 0;
  58. u32 attributes;
  59. void *data;
  60. ssize_t size = 0;
  61. int err;
  62. err = efivar_entry_size(var, &datasize);
  63. /*
  64. * efivarfs represents uncommitted variables with
  65. * zero-length files. Reading them should return EOF.
  66. */
  67. if (err == -ENOENT)
  68. return 0;
  69. else if (err)
  70. return err;
  71. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  72. if (!data)
  73. return -ENOMEM;
  74. size = efivar_entry_get(var, &attributes, &datasize,
  75. data + sizeof(attributes));
  76. if (size)
  77. goto out_free;
  78. memcpy(data, &attributes, sizeof(attributes));
  79. size = simple_read_from_buffer(userbuf, count, ppos,
  80. data, datasize + sizeof(attributes));
  81. out_free:
  82. kfree(data);
  83. return size;
  84. }
  85. const struct file_operations efivarfs_file_operations = {
  86. .open = simple_open,
  87. .read = efivarfs_file_read,
  88. .write = efivarfs_file_write,
  89. .llseek = no_llseek,
  90. };