acl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. if (dentry) {
  169. v9fs_set_acl(dentry, ACL_TYPE_DEFAULT, *dpacl);
  170. v9fs_set_acl(dentry, ACL_TYPE_ACCESS, *pacl);
  171. }
  172. posix_acl_release(*dpacl);
  173. posix_acl_release(*pacl);
  174. *dpacl = *pacl = NULL;
  175. return 0;
  176. }
  177. int v9fs_acl_mode(struct inode *dir, mode_t *modep,
  178. struct posix_acl **dpacl, struct posix_acl **pacl)
  179. {
  180. int retval = 0;
  181. mode_t mode = *modep;
  182. struct posix_acl *acl = NULL;
  183. if (!S_ISLNK(mode)) {
  184. acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
  185. if (IS_ERR(acl))
  186. return PTR_ERR(acl);
  187. if (!acl)
  188. mode &= ~current_umask();
  189. }
  190. if (acl) {
  191. struct posix_acl *clone;
  192. if (S_ISDIR(mode))
  193. *dpacl = posix_acl_dup(acl);
  194. clone = posix_acl_clone(acl, GFP_NOFS);
  195. posix_acl_release(acl);
  196. if (!clone)
  197. return -ENOMEM;
  198. retval = posix_acl_create_masq(clone, &mode);
  199. if (retval < 0) {
  200. posix_acl_release(clone);
  201. goto cleanup;
  202. }
  203. if (retval > 0)
  204. *pacl = clone;
  205. else
  206. posix_acl_release(clone);
  207. }
  208. *modep = mode;
  209. return 0;
  210. cleanup:
  211. return retval;
  212. }
  213. static int v9fs_remote_get_acl(struct dentry *dentry, const char *name,
  214. void *buffer, size_t size, int type)
  215. {
  216. char *full_name;
  217. switch (type) {
  218. case ACL_TYPE_ACCESS:
  219. full_name = POSIX_ACL_XATTR_ACCESS;
  220. break;
  221. case ACL_TYPE_DEFAULT:
  222. full_name = POSIX_ACL_XATTR_DEFAULT;
  223. break;
  224. default:
  225. BUG();
  226. }
  227. return v9fs_xattr_get(dentry, full_name, buffer, size);
  228. }
  229. static int v9fs_xattr_get_acl(struct dentry *dentry, const char *name,
  230. void *buffer, size_t size, int type)
  231. {
  232. struct v9fs_session_info *v9ses;
  233. struct posix_acl *acl;
  234. int error;
  235. if (strcmp(name, "") != 0)
  236. return -EINVAL;
  237. v9ses = v9fs_dentry2v9ses(dentry);
  238. /*
  239. * We allow set/get/list of acl when access=client is not specified
  240. */
  241. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
  242. return v9fs_remote_get_acl(dentry, name, buffer, size, type);
  243. acl = v9fs_get_cached_acl(dentry->d_inode, type);
  244. if (IS_ERR(acl))
  245. return PTR_ERR(acl);
  246. if (acl == NULL)
  247. return -ENODATA;
  248. error = posix_acl_to_xattr(acl, buffer, size);
  249. posix_acl_release(acl);
  250. return error;
  251. }
  252. static int v9fs_remote_set_acl(struct dentry *dentry, const char *name,
  253. const void *value, size_t size,
  254. int flags, int type)
  255. {
  256. char *full_name;
  257. switch (type) {
  258. case ACL_TYPE_ACCESS:
  259. full_name = POSIX_ACL_XATTR_ACCESS;
  260. break;
  261. case ACL_TYPE_DEFAULT:
  262. full_name = POSIX_ACL_XATTR_DEFAULT;
  263. break;
  264. default:
  265. BUG();
  266. }
  267. return v9fs_xattr_set(dentry, full_name, value, size, flags);
  268. }
  269. static int v9fs_xattr_set_acl(struct dentry *dentry, const char *name,
  270. const void *value, size_t size,
  271. int flags, int type)
  272. {
  273. int retval;
  274. struct posix_acl *acl;
  275. struct v9fs_session_info *v9ses;
  276. struct inode *inode = dentry->d_inode;
  277. if (strcmp(name, "") != 0)
  278. return -EINVAL;
  279. v9ses = v9fs_dentry2v9ses(dentry);
  280. /*
  281. * set the attribute on the remote. Without even looking at the
  282. * xattr value. We leave it to the server to validate
  283. */
  284. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
  285. return v9fs_remote_set_acl(dentry, name,
  286. value, size, flags, type);
  287. if (S_ISLNK(inode->i_mode))
  288. return -EOPNOTSUPP;
  289. if (!inode_owner_or_capable(inode))
  290. return -EPERM;
  291. if (value) {
  292. /* update the cached acl value */
  293. acl = posix_acl_from_xattr(value, size);
  294. if (IS_ERR(acl))
  295. return PTR_ERR(acl);
  296. else if (acl) {
  297. retval = posix_acl_valid(acl);
  298. if (retval)
  299. goto err_out;
  300. }
  301. } else
  302. acl = NULL;
  303. switch (type) {
  304. case ACL_TYPE_ACCESS:
  305. name = POSIX_ACL_XATTR_ACCESS;
  306. if (acl) {
  307. mode_t mode = inode->i_mode;
  308. retval = posix_acl_equiv_mode(acl, &mode);
  309. if (retval < 0)
  310. goto err_out;
  311. else {
  312. struct iattr iattr;
  313. if (retval == 0) {
  314. /*
  315. * ACL can be represented
  316. * by the mode bits. So don't
  317. * update ACL.
  318. */
  319. acl = NULL;
  320. value = NULL;
  321. size = 0;
  322. }
  323. /* Updte the mode bits */
  324. iattr.ia_mode = ((mode & S_IALLUGO) |
  325. (inode->i_mode & ~S_IALLUGO));
  326. iattr.ia_valid = ATTR_MODE;
  327. /* FIXME should we update ctime ?
  328. * What is the following setxattr update the
  329. * mode ?
  330. */
  331. v9fs_vfs_setattr_dotl(dentry, &iattr);
  332. }
  333. }
  334. break;
  335. case ACL_TYPE_DEFAULT:
  336. name = POSIX_ACL_XATTR_DEFAULT;
  337. if (!S_ISDIR(inode->i_mode)) {
  338. retval = acl ? -EINVAL : 0;
  339. goto err_out;
  340. }
  341. break;
  342. default:
  343. BUG();
  344. }
  345. retval = v9fs_xattr_set(dentry, name, value, size, flags);
  346. if (!retval)
  347. set_cached_acl(inode, type, acl);
  348. err_out:
  349. posix_acl_release(acl);
  350. return retval;
  351. }
  352. const struct xattr_handler v9fs_xattr_acl_access_handler = {
  353. .prefix = POSIX_ACL_XATTR_ACCESS,
  354. .flags = ACL_TYPE_ACCESS,
  355. .get = v9fs_xattr_get_acl,
  356. .set = v9fs_xattr_set_acl,
  357. };
  358. const struct xattr_handler v9fs_xattr_acl_default_handler = {
  359. .prefix = POSIX_ACL_XATTR_DEFAULT,
  360. .flags = ACL_TYPE_DEFAULT,
  361. .get = v9fs_xattr_get_acl,
  362. .set = v9fs_xattr_set_acl,
  363. };