acl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright IBM Corporation, 2010
  3. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2.1 of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/fs.h>
  16. #include <net/9p/9p.h>
  17. #include <net/9p/client.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/posix_acl_xattr.h>
  21. #include "xattr.h"
  22. #include "acl.h"
  23. #include "v9fs_vfs.h"
  24. static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
  25. {
  26. ssize_t size;
  27. void *value = NULL;
  28. struct posix_acl *acl = NULL;;
  29. size = v9fs_fid_xattr_get(fid, name, NULL, 0);
  30. if (size > 0) {
  31. value = kzalloc(size, GFP_NOFS);
  32. if (!value)
  33. return ERR_PTR(-ENOMEM);
  34. size = v9fs_fid_xattr_get(fid, name, value, size);
  35. if (size > 0) {
  36. acl = posix_acl_from_xattr(value, size);
  37. if (IS_ERR(acl))
  38. goto err_out;
  39. }
  40. } else if (size == -ENODATA || size == 0 ||
  41. size == -ENOSYS || size == -EOPNOTSUPP) {
  42. acl = NULL;
  43. } else
  44. acl = ERR_PTR(-EIO);
  45. err_out:
  46. kfree(value);
  47. return acl;
  48. }
  49. int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
  50. {
  51. int retval = 0;
  52. struct posix_acl *pacl, *dacl;
  53. /* get the default/access acl values and cache them */
  54. dacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_DEFAULT);
  55. pacl = __v9fs_get_acl(fid, POSIX_ACL_XATTR_ACCESS);
  56. if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
  57. set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
  58. set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
  59. posix_acl_release(dacl);
  60. posix_acl_release(pacl);
  61. } else
  62. retval = -EIO;
  63. return retval;
  64. }
  65. static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
  66. {
  67. struct posix_acl *acl;
  68. /*
  69. * 9p Always cache the acl value when
  70. * instantiating the inode (v9fs_inode_from_fid)
  71. */
  72. acl = get_cached_acl(inode, type);
  73. BUG_ON(acl == ACL_NOT_CACHED);
  74. return acl;
  75. }
  76. int v9fs_check_acl(struct inode *inode, int mask)
  77. {
  78. struct posix_acl *acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
  79. if (IS_ERR(acl))
  80. return PTR_ERR(acl);
  81. if (acl) {
  82. int error = posix_acl_permission(inode, acl, mask);
  83. posix_acl_release(acl);
  84. return error;
  85. }
  86. return -EAGAIN;
  87. }
  88. static int v9fs_set_acl(struct dentry *dentry, int type, struct posix_acl *acl)
  89. {
  90. int retval;
  91. char *name;
  92. size_t size;
  93. void *buffer;
  94. struct inode *inode = dentry->d_inode;
  95. set_cached_acl(inode, type, acl);
  96. /* Set a setxattr request to server */
  97. size = posix_acl_xattr_size(acl->a_count);
  98. buffer = kmalloc(size, GFP_KERNEL);
  99. if (!buffer)
  100. return -ENOMEM;
  101. retval = posix_acl_to_xattr(acl, buffer, size);
  102. if (retval < 0)
  103. goto err_free_out;
  104. switch (type) {
  105. case ACL_TYPE_ACCESS:
  106. name = POSIX_ACL_XATTR_ACCESS;
  107. break;
  108. case ACL_TYPE_DEFAULT:
  109. name = POSIX_ACL_XATTR_DEFAULT;
  110. break;
  111. default:
  112. BUG();
  113. }
  114. retval = v9fs_xattr_set(dentry, name, buffer, size, 0);
  115. err_free_out:
  116. kfree(buffer);
  117. return retval;
  118. }
  119. int v9fs_acl_chmod(struct dentry *dentry)
  120. {
  121. int retval = 0;
  122. struct posix_acl *acl, *clone;
  123. struct inode *inode = dentry->d_inode;
  124. if (S_ISLNK(inode->i_mode))
  125. return -EOPNOTSUPP;
  126. acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
  127. if (acl) {
  128. clone = posix_acl_clone(acl, GFP_KERNEL);
  129. posix_acl_release(acl);
  130. if (!clone)
  131. return -ENOMEM;
  132. retval = posix_acl_chmod_masq(clone, inode->i_mode);
  133. if (!retval)
  134. retval = v9fs_set_acl(dentry, ACL_TYPE_ACCESS, clone);
  135. posix_acl_release(clone);
  136. }
  137. return retval;
  138. }
  139. static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
  140. void *buffer, size_t size, int type)
  141. {
  142. struct posix_acl *acl;
  143. int error;
  144. if (strcmp(name, "") != 0)
  145. return -EINVAL;
  146. acl = v9fs_get_cached_acl(dentry->d_inode, type);
  147. if (IS_ERR(acl))
  148. return PTR_ERR(acl);
  149. if (acl == NULL)
  150. return -ENODATA;
  151. error = posix_acl_to_xattr(acl, buffer, size);
  152. posix_acl_release(acl);
  153. return error;
  154. }
  155. static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
  156. const void *value, size_t size,
  157. int flags, int type)
  158. {
  159. int retval;
  160. struct posix_acl *acl;
  161. struct inode *inode = dentry->d_inode;
  162. if (strcmp(name, "") != 0)
  163. return -EINVAL;
  164. if (S_ISLNK(inode->i_mode))
  165. return -EOPNOTSUPP;
  166. if (!is_owner_or_cap(inode))
  167. return -EPERM;
  168. if (value) {
  169. /* update the cached acl value */
  170. acl = posix_acl_from_xattr(value, size);
  171. if (IS_ERR(acl))
  172. return PTR_ERR(acl);
  173. else if (acl) {
  174. retval = posix_acl_valid(acl);
  175. if (retval)
  176. goto err_out;
  177. }
  178. } else
  179. acl = NULL;
  180. switch (type) {
  181. case ACL_TYPE_ACCESS:
  182. name = POSIX_ACL_XATTR_ACCESS;
  183. if (acl) {
  184. mode_t mode = inode->i_mode;
  185. retval = posix_acl_equiv_mode(acl, &mode);
  186. if (retval < 0)
  187. goto err_out;
  188. else {
  189. struct iattr iattr;
  190. if (retval == 0) {
  191. /*
  192. * ACL can be represented
  193. * by the mode bits. So don't
  194. * update ACL.
  195. */
  196. acl = NULL;
  197. value = NULL;
  198. size = 0;
  199. }
  200. /* Updte the mode bits */
  201. iattr.ia_mode = ((mode & S_IALLUGO) |
  202. (inode->i_mode & ~S_IALLUGO));
  203. iattr.ia_valid = ATTR_MODE;
  204. /* FIXME should we update ctime ?
  205. * What is the following setxattr update the
  206. * mode ?
  207. */
  208. v9fs_vfs_setattr_dotl(dentry, &iattr);
  209. }
  210. }
  211. break;
  212. case ACL_TYPE_DEFAULT:
  213. name = POSIX_ACL_XATTR_DEFAULT;
  214. if (!S_ISDIR(inode->i_mode)) {
  215. retval = -EINVAL;
  216. goto err_out;
  217. }
  218. break;
  219. default:
  220. BUG();
  221. }
  222. retval = v9fs_xattr_set(dentry, name, value, size, flags);
  223. if (!retval)
  224. set_cached_acl(inode, type, acl);
  225. err_out:
  226. posix_acl_release(acl);
  227. return retval;
  228. }
  229. const struct xattr_handler v9fs_xattr_acl_access_handler = {
  230. .prefix = POSIX_ACL_XATTR_ACCESS,
  231. .flags = ACL_TYPE_ACCESS,
  232. .get = v9fs_xattr_get_acl,
  233. .set = v9fs_xattr_set_acl,
  234. };
  235. const struct xattr_handler v9fs_xattr_acl_default_handler = {
  236. .prefix = POSIX_ACL_XATTR_DEFAULT,
  237. .flags = ACL_TYPE_DEFAULT,
  238. .get = v9fs_xattr_get_acl,
  239. .set = v9fs_xattr_set_acl,
  240. };