acl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. int v9fs_set_create_acl(struct dentry *dentry,
  140. struct posix_acl *dpacl, struct posix_acl *pacl)
  141. {
  142. if (dpacl)
  143. v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, dpacl);
  144. if (pacl)
  145. v9fs_set_acl(dentry, ACL_TYPE_ACCESS, pacl);
  146. posix_acl_release(dpacl);
  147. posix_acl_release(pacl);
  148. return 0;
  149. }
  150. int v9fs_acl_mode(struct inode *dir, mode_t *modep,
  151. struct posix_acl **dpacl, struct posix_acl **pacl)
  152. {
  153. int retval = 0;
  154. mode_t mode = *modep;
  155. struct posix_acl *acl = NULL;
  156. if (!S_ISLNK(mode)) {
  157. acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
  158. if (IS_ERR(acl))
  159. return PTR_ERR(acl);
  160. if (!acl)
  161. mode &= ~current_umask();
  162. }
  163. if (acl) {
  164. struct posix_acl *clone;
  165. if (S_ISDIR(mode))
  166. *dpacl = acl;
  167. clone = posix_acl_clone(acl, GFP_NOFS);
  168. retval = -ENOMEM;
  169. if (!clone)
  170. goto cleanup;
  171. retval = posix_acl_create_masq(clone, &mode);
  172. if (retval < 0) {
  173. posix_acl_release(clone);
  174. goto cleanup;
  175. }
  176. if (retval > 0)
  177. *pacl = clone;
  178. }
  179. *modep = mode;
  180. return 0;
  181. cleanup:
  182. posix_acl_release(acl);
  183. return retval;
  184. }
  185. static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
  186. void *buffer, size_t size, int type)
  187. {
  188. struct posix_acl *acl;
  189. int error;
  190. if (strcmp(name, "") != 0)
  191. return -EINVAL;
  192. acl = v9fs_get_cached_acl(dentry->d_inode, type);
  193. if (IS_ERR(acl))
  194. return PTR_ERR(acl);
  195. if (acl == NULL)
  196. return -ENODATA;
  197. error = posix_acl_to_xattr(acl, buffer, size);
  198. posix_acl_release(acl);
  199. return error;
  200. }
  201. static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
  202. const void *value, size_t size,
  203. int flags, int type)
  204. {
  205. int retval;
  206. struct posix_acl *acl;
  207. struct inode *inode = dentry->d_inode;
  208. if (strcmp(name, "") != 0)
  209. return -EINVAL;
  210. if (S_ISLNK(inode->i_mode))
  211. return -EOPNOTSUPP;
  212. if (!is_owner_or_cap(inode))
  213. return -EPERM;
  214. if (value) {
  215. /* update the cached acl value */
  216. acl = posix_acl_from_xattr(value, size);
  217. if (IS_ERR(acl))
  218. return PTR_ERR(acl);
  219. else if (acl) {
  220. retval = posix_acl_valid(acl);
  221. if (retval)
  222. goto err_out;
  223. }
  224. } else
  225. acl = NULL;
  226. switch (type) {
  227. case ACL_TYPE_ACCESS:
  228. name = POSIX_ACL_XATTR_ACCESS;
  229. if (acl) {
  230. mode_t mode = inode->i_mode;
  231. retval = posix_acl_equiv_mode(acl, &mode);
  232. if (retval < 0)
  233. goto err_out;
  234. else {
  235. struct iattr iattr;
  236. if (retval == 0) {
  237. /*
  238. * ACL can be represented
  239. * by the mode bits. So don't
  240. * update ACL.
  241. */
  242. acl = NULL;
  243. value = NULL;
  244. size = 0;
  245. }
  246. /* Updte the mode bits */
  247. iattr.ia_mode = ((mode & S_IALLUGO) |
  248. (inode->i_mode & ~S_IALLUGO));
  249. iattr.ia_valid = ATTR_MODE;
  250. /* FIXME should we update ctime ?
  251. * What is the following setxattr update the
  252. * mode ?
  253. */
  254. v9fs_vfs_setattr_dotl(dentry, &iattr);
  255. }
  256. }
  257. break;
  258. case ACL_TYPE_DEFAULT:
  259. name = POSIX_ACL_XATTR_DEFAULT;
  260. if (!S_ISDIR(inode->i_mode)) {
  261. retval = -EINVAL;
  262. goto err_out;
  263. }
  264. break;
  265. default:
  266. BUG();
  267. }
  268. retval = v9fs_xattr_set(dentry, name, value, size, flags);
  269. if (!retval)
  270. set_cached_acl(inode, type, acl);
  271. err_out:
  272. posix_acl_release(acl);
  273. return retval;
  274. }
  275. const struct xattr_handler v9fs_xattr_acl_access_handler = {
  276. .prefix = POSIX_ACL_XATTR_ACCESS,
  277. .flags = ACL_TYPE_ACCESS,
  278. .get = v9fs_xattr_get_acl,
  279. .set = v9fs_xattr_set_acl,
  280. };
  281. const struct xattr_handler v9fs_xattr_acl_default_handler = {
  282. .prefix = POSIX_ACL_XATTR_DEFAULT,
  283. .flags = ACL_TYPE_DEFAULT,
  284. .get = v9fs_xattr_get_acl,
  285. .set = v9fs_xattr_set_acl,
  286. };