acl.c 9.2 KB

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