acl.c 8.7 KB

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