blob: 1247f544a440e41303637a252d864eeb97b990fb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09003#include <linux/gfp.h>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00004#include <linux/nfs.h>
5#include <linux/nfs3.h>
6#include <linux/nfs_fs.h>
Christoph Hellwig334a13e2005-06-28 20:44:58 -07007#include <linux/posix_acl_xattr.h>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00008#include <linux/nfsacl.h>
9
Trond Myklebustf41f7412008-06-11 17:39:04 -040010#include "internal.h"
Trond Myklebust3fc3edf2014-09-25 16:28:53 -040011#include "nfs3_fs.h"
Trond Myklebustf41f7412008-06-11 17:39:04 -040012
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000013#define NFSDBG_FACILITY NFSDBG_PROC
14
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010015/*
16 * nfs3_prepare_get_acl, nfs3_complete_get_acl, nfs3_abort_get_acl: Helpers for
17 * caching get_acl results in a race-free way. See fs/posix_acl.c:get_acl()
18 * for explanations.
19 */
20static void nfs3_prepare_get_acl(struct posix_acl **p)
21{
22 struct posix_acl *sentinel = uncached_acl_sentinel(current);
23
24 if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED) {
25 /* Not the first reader or sentinel already in place. */
26 }
27}
28
29static void nfs3_complete_get_acl(struct posix_acl **p, struct posix_acl *acl)
30{
31 struct posix_acl *sentinel = uncached_acl_sentinel(current);
32
33 /* Only cache the ACL if our sentinel is still in place. */
34 posix_acl_dup(acl);
35 if (cmpxchg(p, sentinel, acl) != sentinel)
36 posix_acl_release(acl);
37}
38
39static void nfs3_abort_get_acl(struct posix_acl **p)
40{
41 struct posix_acl *sentinel = uncached_acl_sentinel(current);
42
43 /* Remove our sentinel upon failure. */
44 cmpxchg(p, sentinel, ACL_NOT_CACHED);
45}
46
Miklos Szeredi0cad6242021-08-18 22:08:24 +020047struct posix_acl *nfs3_get_acl(struct inode *inode, int type, bool rcu)
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000048{
49 struct nfs_server *server = NFS_SERVER(inode);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000050 struct page *pages[NFSACL_MAXPAGES] = { };
51 struct nfs3_getaclargs args = {
52 .fh = NFS_FH(inode),
53 /* The xdr layer may allocate pages here. */
54 .pages = pages,
55 };
56 struct nfs3_getaclres res = {
Trond Myklebust17280172012-03-11 13:11:00 -040057 NULL,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000058 };
Chuck Leverdead28d2006-03-20 13:44:23 -050059 struct rpc_message msg = {
60 .rpc_argp = &args,
61 .rpc_resp = &res,
62 };
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000063 int status, count;
64
Miklos Szeredi0cad6242021-08-18 22:08:24 +020065 if (rcu)
66 return ERR_PTR(-ECHILD);
67
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000068 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
69 return ERR_PTR(-EOPNOTSUPP);
70
Trond Myklebust1f3208b2021-03-25 11:04:34 -040071 status = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +000072 if (status < 0)
73 return ERR_PTR(status);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000074
Andreas Gruenbacher5c6a9f72005-06-22 17:16:27 +000075 /*
76 * Only get the access acl when explicitly requested: We don't
77 * need it for access decisions, and only some applications use
78 * it. Applications which request the access acl first are not
79 * penalized from this optimization.
80 */
81 if (type == ACL_TYPE_ACCESS)
82 args.mask |= NFS_ACLCNT|NFS_ACL;
83 if (S_ISDIR(inode->i_mode))
84 args.mask |= NFS_DFACLCNT|NFS_DFACL;
85 if (args.mask == 0)
86 return NULL;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000087
88 dprintk("NFS call getacl\n");
Chuck Leverdead28d2006-03-20 13:44:23 -050089 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_GETACL];
Trond Myklebust6e94d622010-04-16 16:22:52 -040090 res.fattr = nfs_alloc_fattr();
91 if (res.fattr == NULL)
92 return ERR_PTR(-ENOMEM);
93
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +010094 if (args.mask & NFS_ACL)
95 nfs3_prepare_get_acl(&inode->i_acl);
96 if (args.mask & NFS_DFACL)
97 nfs3_prepare_get_acl(&inode->i_default_acl);
98
Chuck Leverdead28d2006-03-20 13:44:23 -050099 status = rpc_call_sync(server->client_acl, &msg, 0);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000100 dprintk("NFS reply getacl: %d\n", status);
101
102 /* pages may have been allocated at the xdr layer. */
103 for (count = 0; count < NFSACL_MAXPAGES && args.pages[count]; count++)
104 __free_page(args.pages[count]);
105
106 switch (status) {
107 case 0:
Trond Myklebust6e94d622010-04-16 16:22:52 -0400108 status = nfs_refresh_inode(inode, res.fattr);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000109 break;
110 case -EPFNOSUPPORT:
111 case -EPROTONOSUPPORT:
112 dprintk("NFS_V3_ACL extension not supported; disabling\n");
113 server->caps &= ~NFS_CAP_ACLS;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500114 fallthrough;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000115 case -ENOTSUPP:
116 status = -EOPNOTSUPP;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -0600117 goto getout;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000118 default:
119 goto getout;
120 }
121 if ((args.mask & res.mask) != args.mask) {
122 status = -EIO;
123 goto getout;
124 }
125
126 if (res.acl_access != NULL) {
Noah Massey718360c2014-01-30 21:31:12 -0500127 if ((posix_acl_equiv_mode(res.acl_access, NULL) == 0) ||
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800128 res.acl_access->a_count == 0) {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000129 posix_acl_release(res.acl_access);
130 res.acl_access = NULL;
131 }
132 }
133
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800134 if (res.mask & NFS_ACL)
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100135 nfs3_complete_get_acl(&inode->i_acl, res.acl_access);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800136 else
137 forget_cached_acl(inode, ACL_TYPE_ACCESS);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000138
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800139 if (res.mask & NFS_DFACL)
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100140 nfs3_complete_get_acl(&inode->i_default_acl, res.acl_default);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800141 else
142 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
143
144 nfs_free_fattr(res.fattr);
145 if (type == ACL_TYPE_ACCESS) {
146 posix_acl_release(res.acl_default);
147 return res.acl_access;
148 } else {
149 posix_acl_release(res.acl_access);
150 return res.acl_default;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000151 }
152
153getout:
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100154 nfs3_abort_get_acl(&inode->i_acl);
155 nfs3_abort_get_acl(&inode->i_default_acl);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000156 posix_acl_release(res.acl_access);
157 posix_acl_release(res.acl_default);
Trond Myklebust6e94d622010-04-16 16:22:52 -0400158 nfs_free_fattr(res.fattr);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800159 return ERR_PTR(status);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000160}
161
Trond Myklebust8f493b92014-02-02 14:36:42 -0500162static int __nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800163 struct posix_acl *dfacl)
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000164{
165 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust6e94d622010-04-16 16:22:52 -0400166 struct nfs_fattr *fattr;
Trond Myklebustae46141f2009-03-10 20:33:18 -0400167 struct page *pages[NFSACL_MAXPAGES];
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000168 struct nfs3_setaclargs args = {
169 .inode = inode,
170 .mask = NFS_ACL,
171 .acl_access = acl,
172 .pages = pages,
173 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500174 struct rpc_message msg = {
175 .rpc_argp = &args,
176 .rpc_resp = &fattr,
177 };
Trond Myklebustf87d928f2014-08-24 14:46:48 -0400178 int status = 0;
179
180 if (acl == NULL && (!S_ISDIR(inode->i_mode) || dfacl == NULL))
181 goto out;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000182
183 status = -EOPNOTSUPP;
184 if (!nfs_server_capable(inode, NFS_CAP_ACLS))
185 goto out;
186
Chuck Leverf61f6da2011-01-21 03:05:38 +0000187 /* We are doing this here because XDR marshalling does not
188 * return any results, it BUGs. */
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000189 status = -ENOSPC;
190 if (acl != NULL && acl->a_count > NFS_ACL_MAX_ENTRIES)
191 goto out;
192 if (dfacl != NULL && dfacl->a_count > NFS_ACL_MAX_ENTRIES)
193 goto out;
194 if (S_ISDIR(inode->i_mode)) {
195 args.mask |= NFS_DFACL;
196 args.acl_default = dfacl;
Trond Myklebustae46141f2009-03-10 20:33:18 -0400197 args.len = nfsacl_size(acl, dfacl);
198 } else
199 args.len = nfsacl_size(acl, NULL);
200
201 if (args.len > NFS_ACL_INLINE_BUFSIZE) {
202 unsigned int npages = 1 + ((args.len - 1) >> PAGE_SHIFT);
203
204 status = -ENOMEM;
205 do {
206 args.pages[args.npages] = alloc_page(GFP_KERNEL);
207 if (args.pages[args.npages] == NULL)
208 goto out_freepages;
209 args.npages++;
210 } while (args.npages < npages);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000211 }
212
213 dprintk("NFS call setacl\n");
Trond Myklebust6e94d622010-04-16 16:22:52 -0400214 status = -ENOMEM;
215 fattr = nfs_alloc_fattr();
216 if (fattr == NULL)
217 goto out_freepages;
218
Chuck Leverdead28d2006-03-20 13:44:23 -0500219 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
Trond Myklebust6e94d622010-04-16 16:22:52 -0400220 msg.rpc_resp = fattr;
Chuck Leverdead28d2006-03-20 13:44:23 -0500221 status = rpc_call_sync(server->client_acl, &msg, 0);
Trond Myklebustf41f7412008-06-11 17:39:04 -0400222 nfs_access_zap_cache(inode);
223 nfs_zap_acl_cache(inode);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000224 dprintk("NFS reply setacl: %d\n", status);
225
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000226 switch (status) {
227 case 0:
Trond Myklebust6e94d622010-04-16 16:22:52 -0400228 status = nfs_refresh_inode(inode, fattr);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000229 break;
230 case -EPFNOSUPPORT:
231 case -EPROTONOSUPPORT:
232 dprintk("NFS_V3_ACL SETACL RPC not supported"
233 "(will not retry)\n");
234 server->caps &= ~NFS_CAP_ACLS;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500235 fallthrough;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000236 case -ENOTSUPP:
237 status = -EOPNOTSUPP;
238 }
Trond Myklebust6e94d622010-04-16 16:22:52 -0400239 nfs_free_fattr(fattr);
Trond Myklebustae46141f2009-03-10 20:33:18 -0400240out_freepages:
241 while (args.npages != 0) {
242 args.npages--;
243 __free_page(args.pages[args.npages]);
244 }
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000245out:
246 return status;
247}
248
Trond Myklebust8f493b92014-02-02 14:36:42 -0500249int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
250 struct posix_acl *dfacl)
251{
252 int ret;
253 ret = __nfs3_proc_setacls(inode, acl, dfacl);
254 return (ret == -EOPNOTSUPP) ? 0 : ret;
255
256}
257
Christian Brauner13e83a42023-01-13 12:49:20 +0100258int nfs3_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
Christian Brauner549c7292021-01-21 14:19:43 +0100259 struct posix_acl *acl, int type)
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000260{
Andreas Gruenbacher7648f932020-04-20 15:51:47 +0200261 struct posix_acl *orig = acl, *dfacl = NULL, *alloc;
Christian Brauner138060b2022-09-23 10:29:39 +0200262 struct inode *inode = d_inode(dentry);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000263 int status;
264
265 if (S_ISDIR(inode->i_mode)) {
266 switch(type) {
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800267 case ACL_TYPE_ACCESS:
Christian Braunercac2f8b2022-09-22 17:17:00 +0200268 alloc = get_inode_acl(inode, ACL_TYPE_DEFAULT);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800269 if (IS_ERR(alloc))
270 goto fail;
Andreas Gruenbacher7648f932020-04-20 15:51:47 +0200271 dfacl = alloc;
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800272 break;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000273
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800274 case ACL_TYPE_DEFAULT:
Christian Braunercac2f8b2022-09-22 17:17:00 +0200275 alloc = get_inode_acl(inode, ACL_TYPE_ACCESS);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800276 if (IS_ERR(alloc))
277 goto fail;
Andreas Gruenbacher7648f932020-04-20 15:51:47 +0200278 dfacl = acl;
279 acl = alloc;
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800280 break;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000281 }
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800282 }
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000283
284 if (acl == NULL) {
Andreas Gruenbacher7648f932020-04-20 15:51:47 +0200285 alloc = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000286 if (IS_ERR(alloc))
287 goto fail;
Andreas Gruenbacher7648f932020-04-20 15:51:47 +0200288 acl = alloc;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000289 }
Trond Myklebust8f493b92014-02-02 14:36:42 -0500290 status = __nfs3_proc_setacls(inode, acl, dfacl);
Andreas Gruenbacher7648f932020-04-20 15:51:47 +0200291out:
292 if (acl != orig)
293 posix_acl_release(acl);
294 if (dfacl != orig)
295 posix_acl_release(dfacl);
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000296 return status;
297
298fail:
Andreas Gruenbacher7648f932020-04-20 15:51:47 +0200299 status = PTR_ERR(alloc);
300 goto out;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000301}
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000302
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800303const struct xattr_handler *nfs3_xattr_handlers[] = {
304 &posix_acl_access_xattr_handler,
305 &posix_acl_default_xattr_handler,
306 NULL,
307};
Christoph Hellwig74adf832014-06-18 11:07:03 +0200308
309static int
310nfs3_list_one_acl(struct inode *inode, int type, const char *name, void *data,
311 size_t size, ssize_t *result)
312{
313 struct posix_acl *acl;
314 char *p = data + *result;
315
Christian Braunercac2f8b2022-09-22 17:17:00 +0200316 acl = get_inode_acl(inode, type);
Andrey Utkin7a9e75a2014-07-26 14:58:01 +0300317 if (IS_ERR_OR_NULL(acl))
Christoph Hellwig74adf832014-06-18 11:07:03 +0200318 return 0;
319
320 posix_acl_release(acl);
321
322 *result += strlen(name);
323 *result += 1;
324 if (!size)
325 return 0;
326 if (*result > size)
327 return -ERANGE;
328
329 strcpy(p, name);
330 return 0;
331}
332
333ssize_t
334nfs3_listxattr(struct dentry *dentry, char *data, size_t size)
335{
David Howells2b0143b2015-03-17 22:25:59 +0000336 struct inode *inode = d_inode(dentry);
Christoph Hellwig74adf832014-06-18 11:07:03 +0200337 ssize_t result = 0;
338 int error;
339
340 error = nfs3_list_one_acl(inode, ACL_TYPE_ACCESS,
Andreas Gruenbacher97d79292015-12-02 14:44:35 +0100341 XATTR_NAME_POSIX_ACL_ACCESS, data, size, &result);
Christoph Hellwig74adf832014-06-18 11:07:03 +0200342 if (error)
343 return error;
344
345 error = nfs3_list_one_acl(inode, ACL_TYPE_DEFAULT,
Andreas Gruenbacher97d79292015-12-02 14:44:35 +0100346 XATTR_NAME_POSIX_ACL_DEFAULT, data, size, &result);
Christoph Hellwig74adf832014-06-18 11:07:03 +0200347 if (error)
348 return error;
349 return result;
350}