acl.c 9.0 KB

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