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. int flags)
  196. {
  197. struct v9fs_session_info *v9ses;
  198. struct posix_acl *acl;
  199. int error;
  200. v9ses = v9fs_dentry2v9ses(dentry);
  201. /*
  202. * We allow set/get/list of acl when access=client is not specified
  203. */
  204. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
  205. return v9fs_xattr_get(dentry, handler->name, buffer, size);
  206. acl = v9fs_get_cached_acl(inode, handler->flags);
  207. if (IS_ERR(acl))
  208. return PTR_ERR(acl);
  209. if (acl == NULL)
  210. return -ENODATA;
  211. error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  212. posix_acl_release(acl);
  213. return error;
  214. }
  215. static int v9fs_xattr_set_acl(const struct xattr_handler *handler,
  216. struct dentry *dentry, struct inode *inode,
  217. const char *name, const void *value,
  218. size_t size, int flags)
  219. {
  220. int retval;
  221. struct posix_acl *acl;
  222. struct v9fs_session_info *v9ses;
  223. v9ses = v9fs_dentry2v9ses(dentry);
  224. /*
  225. * set the attribute on the remote. Without even looking at the
  226. * xattr value. We leave it to the server to validate
  227. */
  228. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
  229. return v9fs_xattr_set(dentry, handler->name, value, size,
  230. flags);
  231. if (S_ISLNK(inode->i_mode))
  232. return -EOPNOTSUPP;
  233. if (!inode_owner_or_capable(inode))
  234. return -EPERM;
  235. if (value) {
  236. /* update the cached acl value */
  237. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  238. if (IS_ERR(acl))
  239. return PTR_ERR(acl);
  240. else if (acl) {
  241. retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
  242. if (retval)
  243. goto err_out;
  244. }
  245. } else
  246. acl = NULL;
  247. switch (handler->flags) {
  248. case ACL_TYPE_ACCESS:
  249. if (acl) {
  250. struct iattr iattr = { 0 };
  251. struct posix_acl *old_acl = acl;
  252. retval = posix_acl_update_mode(inode, &iattr.ia_mode, &acl);
  253. if (retval)
  254. goto err_out;
  255. if (!acl) {
  256. /*
  257. * ACL can be represented
  258. * by the mode bits. So don't
  259. * update ACL.
  260. */
  261. posix_acl_release(old_acl);
  262. value = NULL;
  263. size = 0;
  264. }
  265. iattr.ia_valid = ATTR_MODE;
  266. /* FIXME should we update ctime ?
  267. * What is the following setxattr update the
  268. * mode ?
  269. */
  270. v9fs_vfs_setattr_dotl(dentry, &iattr);
  271. }
  272. break;
  273. case ACL_TYPE_DEFAULT:
  274. if (!S_ISDIR(inode->i_mode)) {
  275. retval = acl ? -EINVAL : 0;
  276. goto err_out;
  277. }
  278. break;
  279. default:
  280. BUG();
  281. }
  282. retval = v9fs_xattr_set(dentry, handler->name, value, size, flags);
  283. if (!retval)
  284. set_cached_acl(inode, handler->flags, acl);
  285. err_out:
  286. posix_acl_release(acl);
  287. return retval;
  288. }
  289. const struct xattr_handler v9fs_xattr_acl_access_handler = {
  290. .name = XATTR_NAME_POSIX_ACL_ACCESS,
  291. .flags = ACL_TYPE_ACCESS,
  292. .get = v9fs_xattr_get_acl,
  293. .set = v9fs_xattr_set_acl,
  294. };
  295. const struct xattr_handler v9fs_xattr_acl_default_handler = {
  296. .name = XATTR_NAME_POSIX_ACL_DEFAULT,
  297. .flags = ACL_TYPE_DEFAULT,
  298. .get = v9fs_xattr_get_acl,
  299. .set = v9fs_xattr_set_acl,
  300. };