acl.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright IBM Corporation, 2010
  4. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/fs.h>
  8. #include <net/9p/9p.h>
  9. #include <net/9p/client.h>
  10. #include <linux/slab.h>
  11. #include <linux/sched.h>
  12. #include <linux/posix_acl_xattr.h>
  13. #include "xattr.h"
  14. #include "acl.h"
  15. #include "v9fs.h"
  16. #include "v9fs_vfs.h"
  17. #include "fid.h"
  18. static struct posix_acl *v9fs_fid_get_acl(struct p9_fid *fid, const char *name)
  19. {
  20. ssize_t size;
  21. void *value = NULL;
  22. struct posix_acl *acl = NULL;
  23. size = v9fs_fid_xattr_get(fid, name, NULL, 0);
  24. if (size < 0)
  25. return ERR_PTR(size);
  26. if (size == 0)
  27. return ERR_PTR(-ENODATA);
  28. value = kzalloc(size, GFP_NOFS);
  29. if (!value)
  30. return ERR_PTR(-ENOMEM);
  31. size = v9fs_fid_xattr_get(fid, name, value, size);
  32. if (size < 0)
  33. acl = ERR_PTR(size);
  34. else if (size == 0)
  35. acl = ERR_PTR(-ENODATA);
  36. else
  37. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  38. kfree(value);
  39. return acl;
  40. }
  41. static struct posix_acl *v9fs_acl_get(struct dentry *dentry, const char *name)
  42. {
  43. struct p9_fid *fid;
  44. struct posix_acl *acl = NULL;
  45. fid = v9fs_fid_lookup(dentry);
  46. if (IS_ERR(fid))
  47. return ERR_CAST(fid);
  48. acl = v9fs_fid_get_acl(fid, name);
  49. p9_fid_put(fid);
  50. return acl;
  51. }
  52. static struct posix_acl *__v9fs_get_acl(struct p9_fid *fid, const char *name)
  53. {
  54. int retval;
  55. struct posix_acl *acl = NULL;
  56. acl = v9fs_fid_get_acl(fid, name);
  57. if (!IS_ERR(acl))
  58. return acl;
  59. retval = PTR_ERR(acl);
  60. if (retval == -ENODATA || retval == -ENOSYS || retval == -EOPNOTSUPP)
  61. return NULL;
  62. /* map everything else to -EIO */
  63. return ERR_PTR(-EIO);
  64. }
  65. int v9fs_get_acl(struct inode *inode, struct p9_fid *fid)
  66. {
  67. int retval = 0;
  68. struct posix_acl *pacl, *dacl;
  69. struct v9fs_session_info *v9ses;
  70. v9ses = v9fs_inode2v9ses(inode);
  71. if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
  72. ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
  73. set_cached_acl(inode, ACL_TYPE_DEFAULT, NULL);
  74. set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
  75. return 0;
  76. }
  77. /* get the default/access acl values and cache them */
  78. dacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_DEFAULT);
  79. pacl = __v9fs_get_acl(fid, XATTR_NAME_POSIX_ACL_ACCESS);
  80. if (!IS_ERR(dacl) && !IS_ERR(pacl)) {
  81. set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
  82. set_cached_acl(inode, ACL_TYPE_ACCESS, pacl);
  83. } else
  84. retval = -EIO;
  85. if (!IS_ERR(dacl))
  86. posix_acl_release(dacl);
  87. if (!IS_ERR(pacl))
  88. posix_acl_release(pacl);
  89. return retval;
  90. }
  91. static struct posix_acl *v9fs_get_cached_acl(struct inode *inode, int type)
  92. {
  93. struct posix_acl *acl;
  94. /*
  95. * 9p Always cache the acl value when
  96. * instantiating the inode (v9fs_inode_from_fid)
  97. */
  98. acl = get_cached_acl(inode, type);
  99. BUG_ON(is_uncached_acl(acl));
  100. return acl;
  101. }
  102. struct posix_acl *v9fs_iop_get_inode_acl(struct inode *inode, int type, bool rcu)
  103. {
  104. struct v9fs_session_info *v9ses;
  105. if (rcu)
  106. return ERR_PTR(-ECHILD);
  107. v9ses = v9fs_inode2v9ses(inode);
  108. if (((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) ||
  109. ((v9ses->flags & V9FS_ACL_MASK) != V9FS_POSIX_ACL)) {
  110. /*
  111. * On access = client and acl = on mode get the acl
  112. * values from the server
  113. */
  114. return NULL;
  115. }
  116. return v9fs_get_cached_acl(inode, type);
  117. }
  118. struct posix_acl *v9fs_iop_get_acl(struct mnt_idmap *idmap,
  119. struct dentry *dentry, int type)
  120. {
  121. struct v9fs_session_info *v9ses;
  122. v9ses = v9fs_dentry2v9ses(dentry);
  123. /* We allow set/get/list of acl when access=client is not specified. */
  124. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT)
  125. return v9fs_acl_get(dentry, posix_acl_xattr_name(type));
  126. return v9fs_get_cached_acl(d_inode(dentry), type);
  127. }
  128. int v9fs_iop_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  129. struct posix_acl *acl, int type)
  130. {
  131. int retval;
  132. size_t size = 0;
  133. void *value = NULL;
  134. const char *acl_name;
  135. struct v9fs_session_info *v9ses;
  136. struct inode *inode = d_inode(dentry);
  137. if (acl) {
  138. retval = posix_acl_valid(inode->i_sb->s_user_ns, acl);
  139. if (retval)
  140. goto err_out;
  141. size = posix_acl_xattr_size(acl->a_count);
  142. value = kzalloc(size, GFP_NOFS);
  143. if (!value) {
  144. retval = -ENOMEM;
  145. goto err_out;
  146. }
  147. retval = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  148. if (retval < 0)
  149. goto err_out;
  150. }
  151. /*
  152. * set the attribute on the remote. Without even looking at the
  153. * xattr value. We leave it to the server to validate
  154. */
  155. acl_name = posix_acl_xattr_name(type);
  156. v9ses = v9fs_dentry2v9ses(dentry);
  157. if ((v9ses->flags & V9FS_ACCESS_MASK) != V9FS_ACCESS_CLIENT) {
  158. retval = v9fs_xattr_set(dentry, acl_name, value, size, 0);
  159. goto err_out;
  160. }
  161. if (S_ISLNK(inode->i_mode)) {
  162. retval = -EOPNOTSUPP;
  163. goto err_out;
  164. }
  165. if (!inode_owner_or_capable(&nop_mnt_idmap, inode)) {
  166. retval = -EPERM;
  167. goto err_out;
  168. }
  169. switch (type) {
  170. case ACL_TYPE_ACCESS:
  171. if (acl) {
  172. struct iattr iattr = {};
  173. struct posix_acl *acl_mode = acl;
  174. retval = posix_acl_update_mode(&nop_mnt_idmap, inode,
  175. &iattr.ia_mode,
  176. &acl_mode);
  177. if (retval)
  178. goto err_out;
  179. if (!acl_mode) {
  180. /*
  181. * ACL can be represented by the mode bits.
  182. * So don't update ACL below.
  183. */
  184. kfree(value);
  185. value = NULL;
  186. size = 0;
  187. }
  188. iattr.ia_valid = ATTR_MODE;
  189. /*
  190. * FIXME should we update ctime ?
  191. * What is the following setxattr update the mode ?
  192. */
  193. v9fs_vfs_setattr_dotl(&nop_mnt_idmap, dentry, &iattr);
  194. }
  195. break;
  196. case ACL_TYPE_DEFAULT:
  197. if (!S_ISDIR(inode->i_mode)) {
  198. retval = acl ? -EINVAL : 0;
  199. goto err_out;
  200. }
  201. break;
  202. }
  203. retval = v9fs_xattr_set(dentry, acl_name, value, size, 0);
  204. if (!retval)
  205. set_cached_acl(inode, type, acl);
  206. err_out:
  207. kfree(value);
  208. return retval;
  209. }
  210. static int v9fs_set_acl(struct p9_fid *fid, int type, struct posix_acl *acl)
  211. {
  212. int retval;
  213. char *name;
  214. size_t size;
  215. void *buffer;
  216. if (!acl)
  217. return 0;
  218. /* Set a setxattr request to server */
  219. size = posix_acl_xattr_size(acl->a_count);
  220. buffer = kmalloc(size, GFP_KERNEL);
  221. if (!buffer)
  222. return -ENOMEM;
  223. retval = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  224. if (retval < 0)
  225. goto err_free_out;
  226. switch (type) {
  227. case ACL_TYPE_ACCESS:
  228. name = XATTR_NAME_POSIX_ACL_ACCESS;
  229. break;
  230. case ACL_TYPE_DEFAULT:
  231. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  232. break;
  233. default:
  234. BUG();
  235. }
  236. retval = v9fs_fid_xattr_set(fid, name, buffer, size, 0);
  237. err_free_out:
  238. kfree(buffer);
  239. return retval;
  240. }
  241. int v9fs_acl_chmod(struct inode *inode, struct p9_fid *fid)
  242. {
  243. int retval = 0;
  244. struct posix_acl *acl;
  245. if (S_ISLNK(inode->i_mode))
  246. return -EOPNOTSUPP;
  247. acl = v9fs_get_cached_acl(inode, ACL_TYPE_ACCESS);
  248. if (acl) {
  249. retval = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  250. if (retval)
  251. return retval;
  252. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  253. retval = v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
  254. posix_acl_release(acl);
  255. }
  256. return retval;
  257. }
  258. int v9fs_set_create_acl(struct inode *inode, struct p9_fid *fid,
  259. struct posix_acl *dacl, struct posix_acl *acl)
  260. {
  261. set_cached_acl(inode, ACL_TYPE_DEFAULT, dacl);
  262. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  263. v9fs_set_acl(fid, ACL_TYPE_DEFAULT, dacl);
  264. v9fs_set_acl(fid, ACL_TYPE_ACCESS, acl);
  265. return 0;
  266. }
  267. void v9fs_put_acl(struct posix_acl *dacl,
  268. struct posix_acl *acl)
  269. {
  270. posix_acl_release(dacl);
  271. posix_acl_release(acl);
  272. }
  273. int v9fs_acl_mode(struct inode *dir, umode_t *modep,
  274. struct posix_acl **dpacl, struct posix_acl **pacl)
  275. {
  276. int retval = 0;
  277. umode_t mode = *modep;
  278. struct posix_acl *acl = NULL;
  279. if (!S_ISLNK(mode)) {
  280. acl = v9fs_get_cached_acl(dir, ACL_TYPE_DEFAULT);
  281. if (IS_ERR(acl))
  282. return PTR_ERR(acl);
  283. if (!acl)
  284. mode &= ~current_umask();
  285. }
  286. if (acl) {
  287. if (S_ISDIR(mode))
  288. *dpacl = posix_acl_dup(acl);
  289. retval = __posix_acl_create(&acl, GFP_NOFS, &mode);
  290. if (retval < 0)
  291. return retval;
  292. if (retval > 0)
  293. *pacl = acl;
  294. else
  295. posix_acl_release(acl);
  296. }
  297. *modep = mode;
  298. return 0;
  299. }