acl.c 8.9 KB

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