acl.c 8.6 KB

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