acl.c 7.5 KB

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