acl.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 (fuse_is_bad(inode))
  19. return ERR_PTR(-EIO);
  20. if (!fc->posix_acl || fc->no_getxattr)
  21. return NULL;
  22. if (type == ACL_TYPE_ACCESS)
  23. name = XATTR_NAME_POSIX_ACL_ACCESS;
  24. else if (type == ACL_TYPE_DEFAULT)
  25. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  26. else
  27. return ERR_PTR(-EOPNOTSUPP);
  28. value = kmalloc(PAGE_SIZE, GFP_KERNEL);
  29. if (!value)
  30. return ERR_PTR(-ENOMEM);
  31. size = fuse_getxattr(inode, name, value, PAGE_SIZE);
  32. if (size > 0)
  33. acl = posix_acl_from_xattr(fc->user_ns, value, size);
  34. else if ((size == 0) || (size == -ENODATA) ||
  35. (size == -EOPNOTSUPP && fc->no_getxattr))
  36. acl = NULL;
  37. else if (size == -ERANGE)
  38. acl = ERR_PTR(-E2BIG);
  39. else
  40. acl = ERR_PTR(size);
  41. kfree(value);
  42. return acl;
  43. }
  44. int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  45. {
  46. struct fuse_conn *fc = get_fuse_conn(inode);
  47. const char *name;
  48. int ret;
  49. if (fuse_is_bad(inode))
  50. return -EIO;
  51. if (!fc->posix_acl || fc->no_setxattr)
  52. return -EOPNOTSUPP;
  53. if (type == ACL_TYPE_ACCESS)
  54. name = XATTR_NAME_POSIX_ACL_ACCESS;
  55. else if (type == ACL_TYPE_DEFAULT)
  56. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  57. else
  58. return -EINVAL;
  59. if (acl) {
  60. /*
  61. * Fuse userspace is responsible for updating access
  62. * permissions in the inode, if needed. fuse_setxattr
  63. * invalidates the inode attributes, which will force
  64. * them to be refreshed the next time they are used,
  65. * and it also updates i_ctime.
  66. */
  67. size_t size = posix_acl_xattr_size(acl->a_count);
  68. void *value;
  69. if (size > PAGE_SIZE)
  70. return -E2BIG;
  71. value = kmalloc(size, GFP_KERNEL);
  72. if (!value)
  73. return -ENOMEM;
  74. ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
  75. if (ret < 0) {
  76. kfree(value);
  77. return ret;
  78. }
  79. ret = fuse_setxattr(inode, name, value, size, 0);
  80. kfree(value);
  81. } else {
  82. ret = fuse_removexattr(inode, name);
  83. }
  84. forget_all_cached_acls(inode);
  85. fuse_invalidate_attr(inode);
  86. return ret;
  87. }