blob: 38c883d48b02430c52dcf74262775efd04d9633c [file] [log] [blame]
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/fs/nfsd/nfs2acl.c
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00003 *
4 * Process version 2 NFSACL requests.
5 *
6 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
7 */
8
9#include <linux/sunrpc/svc.h>
10#include <linux/nfs.h>
11#include <linux/nfsd/nfsd.h>
12#include <linux/nfsd/cache.h>
13#include <linux/nfsd/xdr.h>
14#include <linux/nfsd/xdr3.h>
15#include <linux/posix_acl.h>
16#include <linux/nfsacl.h>
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050017#include "vfs.h"
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000018
19#define NFSDDBG_FACILITY NFSDDBG_PROC
20#define RETURN_STATUS(st) { resp->status = (st); return (st); }
21
22/*
23 * NULL call.
24 */
Al Viro7111c662006-10-19 23:28:45 -070025static __be32
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000026nfsacld_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
27{
28 return nfs_ok;
29}
30
31/*
32 * Get the Access and/or Default ACL of a file.
33 */
Al Viro7111c662006-10-19 23:28:45 -070034static __be32 nfsacld_proc_getacl(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000035 struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
36{
37 svc_fh *fh;
38 struct posix_acl *acl;
Al Viroc4d987b2006-10-19 23:29:00 -070039 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000040
41 dprintk("nfsd: GETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
42
43 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +020044 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
45 if (nfserr)
J. Bruce Fieldsac8587d2007-11-12 16:05:02 -050046 RETURN_STATUS(nfserr);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000047
48 if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
49 RETURN_STATUS(nfserr_inval);
50 resp->mask = argp->mask;
51
52 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
53 acl = nfsd_get_posix_acl(fh, ACL_TYPE_ACCESS);
54 if (IS_ERR(acl)) {
55 int err = PTR_ERR(acl);
56
57 if (err == -ENODATA || err == -EOPNOTSUPP)
58 acl = NULL;
59 else {
60 nfserr = nfserrno(err);
61 goto fail;
62 }
63 }
64 if (acl == NULL) {
65 /* Solaris returns the inode's minimum ACL. */
66
67 struct inode *inode = fh->fh_dentry->d_inode;
68 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
69 }
70 resp->acl_access = acl;
71 }
72 if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
73 /* Check how Solaris handles requests for the Default ACL
74 of a non-directory! */
75
76 acl = nfsd_get_posix_acl(fh, ACL_TYPE_DEFAULT);
77 if (IS_ERR(acl)) {
78 int err = PTR_ERR(acl);
79
80 if (err == -ENODATA || err == -EOPNOTSUPP)
81 acl = NULL;
82 else {
83 nfserr = nfserrno(err);
84 goto fail;
85 }
86 }
87 resp->acl_default = acl;
88 }
89
90 /* resp->acl_{access,default} are released in nfssvc_release_getacl. */
91 RETURN_STATUS(0);
92
93fail:
94 posix_acl_release(resp->acl_access);
95 posix_acl_release(resp->acl_default);
96 RETURN_STATUS(nfserr);
97}
98
99/*
100 * Set the Access and/or Default ACL of a file.
101 */
Al Viro7111c662006-10-19 23:28:45 -0700102static __be32 nfsacld_proc_setacl(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000103 struct nfsd3_setaclargs *argp,
104 struct nfsd_attrstat *resp)
105{
106 svc_fh *fh;
Al Viroc4d987b2006-10-19 23:29:00 -0700107 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000108
109 dprintk("nfsd: SETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
110
111 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200112 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000113
114 if (!nfserr) {
115 nfserr = nfserrno( nfsd_set_posix_acl(
116 fh, ACL_TYPE_ACCESS, argp->acl_access) );
117 }
118 if (!nfserr) {
119 nfserr = nfserrno( nfsd_set_posix_acl(
120 fh, ACL_TYPE_DEFAULT, argp->acl_default) );
121 }
122
123 /* argp->acl_{access,default} may have been allocated in
124 nfssvc_decode_setaclargs. */
125 posix_acl_release(argp->acl_access);
126 posix_acl_release(argp->acl_default);
127 return nfserr;
128}
129
130/*
131 * Check file attributes
132 */
Al Viro7111c662006-10-19 23:28:45 -0700133static __be32 nfsacld_proc_getattr(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000134 struct nfsd_fhandle *argp, struct nfsd_attrstat *resp)
135{
136 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
137
138 fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200139 return fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000140}
141
142/*
143 * Check file access
144 */
Al Viro7111c662006-10-19 23:28:45 -0700145static __be32 nfsacld_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000146 struct nfsd3_accessres *resp)
147{
Al Viroc4d987b2006-10-19 23:29:00 -0700148 __be32 nfserr;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000149
150 dprintk("nfsd: ACCESS(2acl) %s 0x%x\n",
151 SVCFH_fmt(&argp->fh),
152 argp->access);
153
154 fh_copy(&resp->fh, &argp->fh);
155 resp->access = argp->access;
156 nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
157 return nfserr;
158}
159
160/*
161 * XDR decode functions
162 */
Al Viro131a21c2006-10-19 23:28:56 -0700163static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000164 struct nfsd3_getaclargs *argp)
165{
166 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
167 return 0;
168 argp->mask = ntohl(*p); p++;
169
170 return xdr_argsize_check(rqstp, p);
171}
172
173
Al Viro131a21c2006-10-19 23:28:56 -0700174static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000175 struct nfsd3_setaclargs *argp)
176{
177 struct kvec *head = rqstp->rq_arg.head;
178 unsigned int base;
179 int n;
180
181 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
182 return 0;
183 argp->mask = ntohl(*p++);
184 if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT) ||
185 !xdr_argsize_check(rqstp, p))
186 return 0;
187
188 base = (char *)p - (char *)head->iov_base;
189 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
190 (argp->mask & NFS_ACL) ?
191 &argp->acl_access : NULL);
192 if (n > 0)
193 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
194 (argp->mask & NFS_DFACL) ?
195 &argp->acl_default : NULL);
196 return (n > 0);
197}
198
Al Viro131a21c2006-10-19 23:28:56 -0700199static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000200 struct nfsd_fhandle *argp)
201{
202 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
203 return 0;
204 return xdr_argsize_check(rqstp, p);
205}
206
Al Viro131a21c2006-10-19 23:28:56 -0700207static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000208 struct nfsd3_accessargs *argp)
209{
210 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
211 return 0;
212 argp->access = ntohl(*p++);
213
214 return xdr_argsize_check(rqstp, p);
215}
216
217/*
218 * XDR encode functions
219 */
220
Peter Staubach1b7e0402009-11-02 16:59:07 -0500221/*
222 * There must be an encoding function for void results so svc_process
223 * will work properly.
224 */
225int
226nfsaclsvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
227{
228 return xdr_ressize_check(rqstp, p);
229}
230
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000231/* GETACL */
Al Viro131a21c2006-10-19 23:28:56 -0700232static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000233 struct nfsd3_getaclres *resp)
234{
235 struct dentry *dentry = resp->fh.fh_dentry;
Prasad Paefa89d2007-10-24 15:14:32 -0500236 struct inode *inode;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000237 struct kvec *head = rqstp->rq_res.head;
238 unsigned int base;
239 int n;
Jesper Juhlcb65a5b2006-12-08 02:39:39 -0800240 int w;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000241
Prasad Paefa89d2007-10-24 15:14:32 -0500242 /*
243 * Since this is version 2, the check for nfserr in
244 * nfsd_dispatch actually ensures the following cannot happen.
245 * However, it seems fragile to depend on that.
246 */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000247 if (dentry == NULL || dentry->d_inode == NULL)
248 return 0;
249 inode = dentry->d_inode;
250
251 p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
252 *p++ = htonl(resp->mask);
253 if (!xdr_ressize_check(rqstp, p))
254 return 0;
255 base = (char *)p - (char *)head->iov_base;
256
Jesper Juhlcb65a5b2006-12-08 02:39:39 -0800257 rqstp->rq_res.page_len = w = nfsacl_size(
258 (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
259 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000260 while (w > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700261 if (!rqstp->rq_respages[rqstp->rq_resused++])
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000262 return 0;
263 w -= PAGE_SIZE;
264 }
265
266 n = nfsacl_encode(&rqstp->rq_res, base, inode,
267 resp->acl_access,
268 resp->mask & NFS_ACL, 0);
269 if (n > 0)
270 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
271 resp->acl_default,
272 resp->mask & NFS_DFACL,
273 NFS_ACL_DEFAULT);
274 if (n <= 0)
275 return 0;
276 return 1;
277}
278
Al Viro131a21c2006-10-19 23:28:56 -0700279static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000280 struct nfsd_attrstat *resp)
281{
282 p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
283 return xdr_ressize_check(rqstp, p);
284}
285
286/* ACCESS */
Al Viro131a21c2006-10-19 23:28:56 -0700287static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000288 struct nfsd3_accessres *resp)
289{
290 p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
291 *p++ = htonl(resp->access);
292 return xdr_ressize_check(rqstp, p);
293}
294
295/*
296 * XDR release functions
297 */
Al Viro131a21c2006-10-19 23:28:56 -0700298static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000299 struct nfsd3_getaclres *resp)
300{
301 fh_put(&resp->fh);
302 posix_acl_release(resp->acl_access);
303 posix_acl_release(resp->acl_default);
304 return 1;
305}
306
Greg Banksc9ce2282007-02-20 10:12:34 +1100307static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
308 struct nfsd_attrstat *resp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000309{
310 fh_put(&resp->fh);
311 return 1;
312}
313
Greg Banksc9ce2282007-02-20 10:12:34 +1100314static int nfsaclsvc_release_access(struct svc_rqst *rqstp, __be32 *p,
315 struct nfsd3_accessres *resp)
316{
317 fh_put(&resp->fh);
318 return 1;
319}
320
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000321#define nfsaclsvc_decode_voidargs NULL
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000322#define nfsaclsvc_release_void NULL
323#define nfsd3_fhandleargs nfsd_fhandle
324#define nfsd3_attrstatres nfsd_attrstat
325#define nfsd3_voidres nfsd3_voidargs
326struct nfsd3_voidargs { int dummy; };
327
328#define PROC(name, argt, rest, relt, cache, respsize) \
329 { (svc_procfunc) nfsacld_proc_##name, \
330 (kxdrproc_t) nfsaclsvc_decode_##argt##args, \
331 (kxdrproc_t) nfsaclsvc_encode_##rest##res, \
332 (kxdrproc_t) nfsaclsvc_release_##relt, \
333 sizeof(struct nfsd3_##argt##args), \
334 sizeof(struct nfsd3_##rest##res), \
335 0, \
336 cache, \
337 respsize, \
338 }
339
340#define ST 1 /* status*/
341#define AT 21 /* attributes */
342#define pAT (1+AT) /* post attributes - conditional */
343#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
344
345static struct svc_procedure nfsd_acl_procedures2[] = {
346 PROC(null, void, void, void, RC_NOCACHE, ST),
347 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
Greg Banksc9ce2282007-02-20 10:12:34 +1100348 PROC(setacl, setacl, attrstat, attrstat, RC_NOCACHE, ST+AT),
349 PROC(getattr, fhandle, attrstat, attrstat, RC_NOCACHE, ST+AT),
350 PROC(access, access, access, access, RC_NOCACHE, ST+AT+1),
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000351};
352
353struct svc_version nfsd_acl_version2 = {
354 .vs_vers = 2,
355 .vs_nproc = 5,
356 .vs_proc = nfsd_acl_procedures2,
357 .vs_dispatch = nfsd_dispatch,
358 .vs_xdrsize = NFS3_SVC_XDRSIZE,
Peter Staubach1b7e0402009-11-02 16:59:07 -0500359 .vs_hidden = 0,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000360};