acl.c 9.0 KB

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