blob: 56cdff4e954c5f43e6ba3709edc070feb8759cbc [file] [log] [blame]
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00001/*
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00002 * Process version 3 NFSACL requests.
3 *
4 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
5 */
6
Boaz Harrosh9a74af22009-12-03 20:30:56 +02007#include "nfsd.h"
8/* FIXME: nfsacl.h is a broken header */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00009#include <linux/nfsacl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/gfp.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020011#include "cache.h"
12#include "xdr3.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050013#include "vfs.h"
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000014
15#define RETURN_STATUS(st) { resp->status = (st); return (st); }
16
17/*
18 * NULL call.
19 */
Al Viro7111c662006-10-19 23:28:45 -070020static __be32
Christoph Hellwiga6beb732017-05-08 17:35:49 +020021nfsd3_proc_null(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000022{
23 return nfs_ok;
24}
25
26/*
27 * Get the Access and/or Default ACL of a file.
28 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +020029static __be32 nfsd3_proc_getacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000030{
Christoph Hellwiga6beb732017-05-08 17:35:49 +020031 struct nfsd3_getaclargs *argp = rqstp->rq_argp;
32 struct nfsd3_getaclres *resp = rqstp->rq_resp;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000033 struct posix_acl *acl;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080034 struct inode *inode;
35 svc_fh *fh;
Al Viroc4d987b2006-10-19 23:29:00 -070036 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000037
38 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +020039 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
40 if (nfserr)
J. Bruce Fieldsac8587d2007-11-12 16:05:02 -050041 RETURN_STATUS(nfserr);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000042
David Howells2b0143b2015-03-17 22:25:59 +000043 inode = d_inode(fh->fh_dentry);
Christoph Hellwig4ac72492013-12-20 05:16:55 -080044
Kinglong Mee7b8f4582015-07-03 19:39:02 +080045 if (argp->mask & ~NFS_ACL_MASK)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000046 RETURN_STATUS(nfserr_inval);
47 resp->mask = argp->mask;
48
49 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
Christoph Hellwig4ac72492013-12-20 05:16:55 -080050 acl = get_acl(inode, ACL_TYPE_ACCESS);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000051 if (acl == NULL) {
52 /* Solaris returns the inode's minimum ACL. */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000053 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
54 }
Kinglong Mee35e634b2014-07-09 21:54:16 +080055 if (IS_ERR(acl)) {
56 nfserr = nfserrno(PTR_ERR(acl));
57 goto fail;
58 }
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000059 resp->acl_access = acl;
60 }
61 if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
62 /* Check how Solaris handles requests for the Default ACL
63 of a non-directory! */
Christoph Hellwig4ac72492013-12-20 05:16:55 -080064 acl = get_acl(inode, ACL_TYPE_DEFAULT);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000065 if (IS_ERR(acl)) {
Christoph Hellwig4ac72492013-12-20 05:16:55 -080066 nfserr = nfserrno(PTR_ERR(acl));
67 goto fail;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000068 }
69 resp->acl_default = acl;
70 }
71
72 /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
73 RETURN_STATUS(0);
74
75fail:
76 posix_acl_release(resp->acl_access);
77 posix_acl_release(resp->acl_default);
78 RETURN_STATUS(nfserr);
79}
80
81/*
82 * Set the Access and/or Default ACL of a file.
83 */
Christoph Hellwiga6beb732017-05-08 17:35:49 +020084static __be32 nfsd3_proc_setacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000085{
Christoph Hellwiga6beb732017-05-08 17:35:49 +020086 struct nfsd3_setaclargs *argp = rqstp->rq_argp;
87 struct nfsd3_attrstat *resp = rqstp->rq_resp;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080088 struct inode *inode;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000089 svc_fh *fh;
Al Viroc4d987b2006-10-19 23:29:00 -070090 __be32 nfserr = 0;
Christoph Hellwig4ac72492013-12-20 05:16:55 -080091 int error;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000092
93 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +020094 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
Christoph Hellwig4ac72492013-12-20 05:16:55 -080095 if (nfserr)
96 goto out;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000097
David Howells2b0143b2015-03-17 22:25:59 +000098 inode = d_inode(fh->fh_dentry);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000099
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800100 error = fh_want_write(fh);
101 if (error)
102 goto out_errno;
103
Ben Hutchings99965372016-06-22 19:43:35 +0100104 fh_lock(fh);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800105
Ben Hutchings99965372016-06-22 19:43:35 +0100106 error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access);
107 if (error)
108 goto out_drop_lock;
109 error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default);
110
111out_drop_lock:
112 fh_unlock(fh);
Christoph Hellwig4ac72492013-12-20 05:16:55 -0800113 fh_drop_write(fh);
114out_errno:
115 nfserr = nfserrno(error);
116out:
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000117 /* argp->acl_{access,default} may have been allocated in
118 nfs3svc_decode_setaclargs. */
119 posix_acl_release(argp->acl_access);
120 posix_acl_release(argp->acl_default);
121 RETURN_STATUS(nfserr);
122}
123
124/*
125 * XDR decode functions
126 */
Al Viro91f07162006-10-19 23:28:57 -0700127static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000128 struct nfsd3_getaclargs *args)
129{
Benoit Tained40aa332014-05-22 16:32:30 +0200130 p = nfs3svc_decode_fh(p, &args->fh);
131 if (!p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000132 return 0;
133 args->mask = ntohl(*p); p++;
134
135 return xdr_argsize_check(rqstp, p);
136}
137
138
Al Viro91f07162006-10-19 23:28:57 -0700139static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000140 struct nfsd3_setaclargs *args)
141{
142 struct kvec *head = rqstp->rq_arg.head;
143 unsigned int base;
144 int n;
145
Benoit Tained40aa332014-05-22 16:32:30 +0200146 p = nfs3svc_decode_fh(p, &args->fh);
147 if (!p)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000148 return 0;
149 args->mask = ntohl(*p++);
Kinglong Mee7b8f4582015-07-03 19:39:02 +0800150 if (args->mask & ~NFS_ACL_MASK ||
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000151 !xdr_argsize_check(rqstp, p))
152 return 0;
153
154 base = (char *)p - (char *)head->iov_base;
155 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
156 (args->mask & NFS_ACL) ?
157 &args->acl_access : NULL);
158 if (n > 0)
159 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
160 (args->mask & NFS_DFACL) ?
161 &args->acl_default : NULL);
162 return (n > 0);
163}
164
165/*
166 * XDR encode functions
167 */
168
169/* GETACL */
Al Viro91f07162006-10-19 23:28:57 -0700170static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000171 struct nfsd3_getaclres *resp)
172{
173 struct dentry *dentry = resp->fh.fh_dentry;
174
175 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
David Howells2b0143b2015-03-17 22:25:59 +0000176 if (resp->status == 0 && dentry && d_really_is_positive(dentry)) {
177 struct inode *inode = d_inode(dentry);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000178 struct kvec *head = rqstp->rq_res.head;
179 unsigned int base;
180 int n;
Jesper Juhl14d2b592006-12-08 02:39:40 -0800181 int w;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000182
183 *p++ = htonl(resp->mask);
184 if (!xdr_ressize_check(rqstp, p))
185 return 0;
186 base = (char *)p - (char *)head->iov_base;
187
Jesper Juhl14d2b592006-12-08 02:39:40 -0800188 rqstp->rq_res.page_len = w = nfsacl_size(
189 (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
190 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000191 while (w > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500192 if (!*(rqstp->rq_next_page++))
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000193 return 0;
194 w -= PAGE_SIZE;
195 }
196
197 n = nfsacl_encode(&rqstp->rq_res, base, inode,
198 resp->acl_access,
199 resp->mask & NFS_ACL, 0);
200 if (n > 0)
201 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
202 resp->acl_default,
203 resp->mask & NFS_DFACL,
204 NFS_ACL_DEFAULT);
205 if (n <= 0)
206 return 0;
207 } else
208 if (!xdr_ressize_check(rqstp, p))
209 return 0;
210
211 return 1;
212}
213
214/* SETACL */
Al Viro91f07162006-10-19 23:28:57 -0700215static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000216 struct nfsd3_attrstat *resp)
217{
218 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
219
220 return xdr_ressize_check(rqstp, p);
221}
222
223/*
224 * XDR release functions
225 */
Christoph Hellwig85374882017-05-08 18:48:24 +0200226static void nfs3svc_release_getacl(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000227{
Christoph Hellwig85374882017-05-08 18:48:24 +0200228 struct nfsd3_getaclres *resp = rqstp->rq_resp;
229
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000230 fh_put(&resp->fh);
231 posix_acl_release(resp->acl_access);
232 posix_acl_release(resp->acl_default);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000233}
234
235#define nfs3svc_decode_voidargs NULL
236#define nfs3svc_release_void NULL
237#define nfsd3_setaclres nfsd3_attrstat
238#define nfsd3_voidres nfsd3_voidargs
239struct nfsd3_voidargs { int dummy; };
240
Christoph Hellwigf7235b62017-05-08 17:59:13 +0200241#define PROC(name, argt, rest, relt, cache, respsize) \
242{ \
Christoph Hellwiga6beb732017-05-08 17:35:49 +0200243 .pc_func = nfsd3_proc_##name, \
Christoph Hellwigf7235b62017-05-08 17:59:13 +0200244 .pc_decode = (kxdrproc_t) nfs3svc_decode_##argt##args, \
245 .pc_encode = (kxdrproc_t) nfs3svc_encode_##rest##res, \
Christoph Hellwig85374882017-05-08 18:48:24 +0200246 .pc_release = nfs3svc_release_##relt, \
Christoph Hellwigf7235b62017-05-08 17:59:13 +0200247 .pc_argsize = sizeof(struct nfsd3_##argt##args), \
248 .pc_ressize = sizeof(struct nfsd3_##rest##res), \
249 .pc_cachetype = cache, \
250 .pc_xdrressize = respsize, \
251}
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000252
253#define ST 1 /* status*/
254#define AT 21 /* attributes */
255#define pAT (1+AT) /* post attributes - conditional */
256#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
257
258static struct svc_procedure nfsd_acl_procedures3[] = {
259 PROC(null, void, void, void, RC_NOCACHE, ST),
260 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
261 PROC(setacl, setacl, setacl, fhandle, RC_NOCACHE, ST+pAT),
262};
263
264struct svc_version nfsd_acl_version3 = {
265 .vs_vers = 3,
266 .vs_nproc = 3,
267 .vs_proc = nfsd_acl_procedures3,
268 .vs_dispatch = nfsd_dispatch,
269 .vs_xdrsize = NFS3_SVC_XDRSIZE,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000270};
271