acl.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * FUSE: Filesystem in Userspace
  3. * Copyright (C) 2016 Canonical Ltd. <seth.forshee@canonical.com>
  4. *
  5. * This program can be distributed under the terms of the GNU GPL.
  6. * See the file COPYING.
  7. */
  8. #include "fuse_i.h"
  9. #include <linux/posix_acl.h>
  10. #include <linux/posix_acl_xattr.h>
  11. struct posix_acl *fuse_get_acl(struct inode *inode, int type)
  12. {
  13. struct fuse_conn *fc = get_fuse_conn(inode);
  14. int size;
  15. const char *name;
  16. void *value = NULL;
  17. struct posix_acl *acl;
  18. if (!fc->posix_acl || fc->no_getxattr)
  19. return NULL;
  20. if (type == ACL_TYPE_ACCESS)
  21. name = XATTR_NAME_POSIX_ACL_ACCESS;
  22. else if (type == ACL_TYPE_DEFAULT)
  23. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  24. else
  25. return ERR_PTR(-EOPNOTSUPP);
  26. value = kmalloc(PAGE_SIZE, GFP_KERNEL);
  27. if (!value)
  28. return ERR_PTR(-ENOMEM);
  29. size = fuse_getxattr(inode, name, value, PAGE_SIZE);
  30. if (size > 0)
  31. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  32. else if ((size == 0) || (size == -ENODATA) ||
  33. (size == -EOPNOTSUPP && fc->no_getxattr))
  34. acl = NULL;
  35. else if (size == -ERANGE)
  36. acl = ERR_PTR(-E2BIG);
  37. else
  38. acl = ERR_PTR(size);
  39. kfree(value);
  40. return acl;
  41. }
  42. int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  43. {
  44. struct fuse_conn *fc = get_fuse_conn(inode);
  45. const char *name;
  46. int ret;
  47. if (!fc->posix_acl || fc->no_setxattr)
  48. return -EOPNOTSUPP;
  49. if (type == ACL_TYPE_ACCESS)
  50. name = XATTR_NAME_POSIX_ACL_ACCESS;
  51. else if (type == ACL_TYPE_DEFAULT)
  52. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  53. else
  54. return -EINVAL;
  55. if (acl) {
  56. /*
  57. * Fuse userspace is responsible for updating access
  58. * permissions in the inode, if needed. fuse_setxattr
  59. * invalidates the inode attributes, which will force
  60. * them to be refreshed the next time they are used,
  61. * and it also updates i_ctime.
  62. */
  63. size_t size = posix_acl_xattr_size(acl->a_count);
  64. void *value;
  65. if (size > PAGE_SIZE)
  66. return -E2BIG;
  67. value = kmalloc(size, GFP_KERNEL);
  68. if (!value)
  69. return -ENOMEM;
  70. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  71. if (ret < 0) {
  72. kfree(value);
  73. return ret;
  74. }
  75. ret = fuse_setxattr(inode, name, value, size, 0);
  76. kfree(value);
  77. } else {
  78. ret = fuse_removexattr(inode, name);
  79. }
  80. forget_all_cached_acls(inode);
  81. fuse_invalidate_attr(inode);
  82. return ret;
  83. }