acl.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.h"
  24. #include "v9fs_vfs.h"
  25. #include "fid.h"
  26. static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, char *name)
  27. {
  28. ssize_t size;
  29. void *value = NULL;
  30. struct posix_acl *acl = NULL;
  31. size = v9fs_fid_xattr_get(fid, name, NULL, 0);
  32. if (size > 0) {
  33. value = kzalloc(size, GFP_NOFS);
  34. if (!value)
  35. return ERR_PTR(-ENOMEM);
  36. size = v9fs_fid_xattr_get(fid, name, value, size);
  37. if (size > 0) {
  38. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  39. if (IS_ERR(acl))
  40. goto err_out;
  41. }
  42. } else if (size == -ENODATA || size == 0 ||
  43. size == -ENOSYS || size == -EOPNOTSUPP) {
  44. acl = NULL;
  45. } else
  46. acl = ERR_PTR(-EIO);
  47. err_out:
  48. kfree(value);
  49. return acl;
  50. }
  51. int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
  52. {
  53. int retval = 0;
  54. struct posix_acl *pacl, *dacl;
  55. struct v9fs_session_info *v9ses;
  56. v9ses = v9fs_inode2v9ses(inode);
  57. if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
  58. ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
  59. set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
  60. set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
  61. return 0;
  62. }
  63. /* get the default/access acl values and cache them */
  64. dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
  65. pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
  66. if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
  67. set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
  68. set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
  69. } else
  70. retval = -EIO;
  71. if (!IS_ERR(dacl))
  72. posix_acl_release(dacl);
  73. if (!IS_ERR(pacl))
  74. posix_acl_release(pacl);
  75. return retval;
  76. }
  77. static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
  78. {
  79. struct posix_acl *acl;
  80. /*
  81. * 9p Always cache the acl value when
  82. * instantiating the inode (v9fs_inode_from_fid)
  83. */
  84. acl = get_cached_acl(inode, type);
  85. BUG_ON(is_uncached_acl(acl));
  86. return acl;
  87. }
  88. struct posix_acl *v9fs_iop_get_acl(struct inode *inode, int type)
  89. {
  90. struct v9fs_session_info *v9ses;
  91. v9ses = v9fs_inode2v9ses(inode);
  92. if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
  93. ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
  94. /*
  95. * On access = client and acl = on mode get the acl
  96. * values from the server
  97. */
  98. return NULL;
  99. }
  100. return v9fs_get_cached_acl(inode, type);
  101. }
  102. static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
  103. {
  104. int retval;
  105. char *name;
  106. size_t size;
  107. void *buffer;
  108. if (!acl)
  109. return 0;
  110. /* Set a setxattr request to server */
  111. size = posix_acl_xattr_size(acl->a_count);
  112. buffer = kmalloc(size, GFP_KERNEL);
  113. if (!buffer)
  114. return -ENOMEM;
  115. retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  116. if (retval < 0)
  117. goto err_free_out;
  118. switch (type) {
  119. case ACL_TYPE_ACCESS:
  120. name = XATTR_NAME_POSIX_ACL_ACCESS;
  121. break;
  122. case ACL_TYPE_DEFAULT:
  123. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  124. break;
  125. default:
  126. BUG();
  127. }
  128. retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
  129. err_free_out:
  130. kfree(buffer);
  131. return retval;
  132. }
  133. int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
  134. {
  135. int retval = 0;
  136. struct posix_acl *acl;
  137. if (S_ISLNK(inode->i_mode))
  138. return -EOPNOTSUPP;
  139. acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
  140. if (acl) {
  141. retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  142. if (retval)
  143. return retval;
  144. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  145. retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
  146. posix_acl_release(acl);
  147. }
  148. return retval;
  149. }
  150. int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
  151. struct posix_acl *dacl, struct posix_acl *acl)
  152. {
  153. set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
  154. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  155. v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
  156. v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
  157. return 0;
  158. }
  159. void v9fs_put_acl(struct posix_acl *dacl,
  160. struct posix_acl *acl)
  161. {
  162. posix_acl_release(dacl);
  163. posix_acl_release(acl);
  164. }
  165. int v9fs_acl_mode(struct inode *dir, umode_t *modep,
  166. struct posix_acl **dpacl, struct posix_acl **pacl)
  167. {
  168. int retval = 0;
  169. umode_t mode = *modep;
  170. struct posix_acl *acl = NULL;
  171. if (!S_ISLNK(mode)) {
  172. acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
  173. if (IS_ERR(acl))
  174. return PTR_ERR(acl);
  175. if (!acl)
  176. mode &= ~current_umask();
  177. }
  178. if (acl) {
  179. if (S_ISDIR(mode))
  180. *dpacl = posix_acl_dup(acl);
  181. retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
  182. if (retval < 0)
  183. return retval;
  184. if (retval > 0)
  185. *pacl = acl;
  186. else
  187. posix_acl_release(acl);
  188. }
  189. *modep = mode;
  190. return 0;
  191. }
  192. static int v9fs_xattr_get_acl(const struct xattr_handler *handler,
  193. struct dentry *dentry, struct inode *inode,
  194. const char *name, void *buffer, size_t size)
  195. {
  196. struct v9fs_session_info *v9ses;
  197. struct posix_acl *acl;
  198. int error;
  199. v9ses = v9fs_dentry2v9ses(dentry);
  200. /*
  201. * We allow set/get/list of acl when access=client is not specified
  202. */
  203. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
  204. return v9fs_xattr_get(dentry, handler->name, buffer, size);
  205. acl = v9fs_get_cached_acl(inode, handler->flags);
  206. if (IS_ERR(acl))
  207. return PTR_ERR(acl);
  208. if (acl == NULL)
  209. return -ENODATA;
  210. error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  211. posix_acl_release(acl);
  212. return error;
  213. }
  214. static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
  215. struct dentry *dentry, struct inode *inode,
  216. const char *name, const void *value,
  217. size_t size, int flags)
  218. {
  219. int retval;
  220. struct posix_acl *acl;
  221. struct v9fs_session_info *v9ses;
  222. v9ses = v9fs_dentry2v9ses(dentry);
  223. /*
  224. * set the attribute on the remote. Without even looking at the
  225. * xattr value. We leave it to the server to validate
  226. */
  227. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
  228. return v9fs_xattr_set(dentry, handler->name, value, size,
  229. flags);
  230. if (S_ISLNK(inode->i_mode))
  231. return -EOPNOTSUPP;
  232. if (!inode_owner_or_capable(inode))
  233. return -EPERM;
  234. if (value) {
  235. /* update the cached acl value */
  236. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  237. if (IS_ERR(acl))
  238. return PTR_ERR(acl);
  239. else if (acl) {
  240. retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
  241. if (retval)
  242. goto err_out;
  243. }
  244. } else
  245. acl = NULL;
  246. switch (handler->flags) {
  247. case ACL_TYPE_ACCESS:
  248. if (acl) {
  249. struct iattr iattr;
  250. struct posix_acl *old_acl = acl;
  251. retval = posix_acl_update_mode(inode, &iattr.ia_mode, &acl);
  252. if (retval)
  253. goto err_out;
  254. if (!acl) {
  255. /*
  256. * ACL can be represented
  257. * by the mode bits. So don't
  258. * update ACL.
  259. */
  260. posix_acl_release(old_acl);
  261. value = NULL;
  262. size = 0;
  263. }
  264. iattr.ia_valid = ATTR_MODE;
  265. /* FIXME should we update ctime ?
  266. * What is the following setxattr update the
  267. * mode ?
  268. */
  269. v9fs_vfs_setattr_dotl(dentry, &iattr);
  270. }
  271. break;
  272. case ACL_TYPE_DEFAULT:
  273. if (!S_ISDIR(inode->i_mode)) {
  274. retval = acl ? -EINVAL : 0;
  275. goto err_out;
  276. }
  277. break;
  278. default:
  279. BUG();
  280. }
  281. retval = v9fs_xattr_set(dentry, handler->name, value, size, flags);
  282. if (!retval)
  283. set_cached_acl(inode, handler->flags, acl);
  284. err_out:
  285. posix_acl_release(acl);
  286. return retval;
  287. }
  288. const struct xattr_handler v9fs_xattr_acl_access_handler = {
  289. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  290. .flags = ACL_TYPE_ACCESS,
  291. .get = v9fs_xattr_get_acl,
  292. .set = v9fs_xattr_set_acl,
  293. };
  294. const struct xattr_handler v9fs_xattr_acl_default_handler = {
  295. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  296. .flags = ACL_TYPE_DEFAULT,
  297. .get = v9fs_xattr_get_acl,
  298. .set = v9fs_xattr_set_acl,
  299. };