file.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 = 0;
  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 = kmalloc(datasize, GFP_KERNEL);
  30. if (!data)
  31. return -ENOMEM;
  32. if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
  33. bytes = -EFAULT;
  34. goto out;
  35. }
  36. bytes = efivar_entry_set_get_size(var, attributes, &datasize,
  37. data, &set);
  38. if (!set && bytes) {
  39. if (bytes == -ENOENT)
  40. bytes = -EIO;
  41. goto out;
  42. }
  43. if (bytes == -ENOENT) {
  44. drop_nlink(inode);
  45. d_delete(file->f_dentry);
  46. dput(file->f_dentry);
  47. } else {
  48. mutex_lock(&inode->i_mutex);
  49. i_size_write(inode, datasize + sizeof(attributes));
  50. mutex_unlock(&inode->i_mutex);
  51. }
  52. bytes = count;
  53. out:
  54. kfree(data);
  55. return bytes;
  56. }
  57. static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
  58. size_t count, loff_t *ppos)
  59. {
  60. struct efivar_entry *var = file->private_data;
  61. unsigned long datasize = 0;
  62. u32 attributes;
  63. void *data;
  64. ssize_t size = 0;
  65. int err;
  66. err = efivar_entry_size(var, &datasize);
  67. /*
  68. * efivarfs represents uncommitted variables with
  69. * zero-length files. Reading them should return EOF.
  70. */
  71. if (err == -ENOENT)
  72. return 0;
  73. else if (err)
  74. return err;
  75. data = kmalloc(datasize + sizeof(attributes), GFP_KERNEL);
  76. if (!data)
  77. return -ENOMEM;
  78. size = efivar_entry_get(var, &attributes, &datasize,
  79. data + sizeof(attributes));
  80. if (size)
  81. goto out_free;
  82. memcpy(data, &attributes, sizeof(attributes));
  83. size = simple_read_from_buffer(userbuf, count, ppos,
  84. data, datasize + sizeof(attributes));
  85. out_free:
  86. kfree(data);
  87. return size;
  88. }
  89. const struct file_operations efivarfs_file_operations = {
  90. .open = simple_open,
  91. .read = efivarfs_file_read,
  92. .write = efivarfs_file_write,
  93. .llseek = no_llseek,
  94. };