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