blob: 874e2a94bf4f4539889118e89669cf53cc63386b [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
Boaz Harrosh9a74af22009-12-03 20:30:56 +02009#include "nfsd.h"
10/* FIXME: nfsacl.h is a broken header */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000011#include <linux/nfsacl.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020012#include "cache.h"
13#include "xdr3.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050014#include "vfs.h"
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000015
16#define NFSDDBG_FACILITY NFSDDBG_PROC
17#define RETURN_STATUS(st) { resp->status = (st); return (st); }
18
19/*
20 * NULL call.
21 */
Al Viro7111c662006-10-19 23:28:45 -070022static __be32
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000023nfsacld_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
24{
25 return nfs_ok;
26}
27
28/*
29 * Get the Access and/or Default ACL of a file.
30 */
Al Viro7111c662006-10-19 23:28:45 -070031static __be32 nfsacld_proc_getacl(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000032 struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
33{
34 svc_fh *fh;
35 struct posix_acl *acl;
Al Viroc4d987b2006-10-19 23:29:00 -070036 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000037
38 dprintk("nfsd: GETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
39
40 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +020041 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
42 if (nfserr)
J. Bruce Fieldsac8587d2007-11-12 16:05:02 -050043 RETURN_STATUS(nfserr);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000044
45 if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
46 RETURN_STATUS(nfserr_inval);
47 resp->mask = argp->mask;
48
49 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
50 acl = nfsd_get_posix_acl(fh, ACL_TYPE_ACCESS);
51 if (IS_ERR(acl)) {
52 int err = PTR_ERR(acl);
53
54 if (err == -ENODATA || err == -EOPNOTSUPP)
55 acl = NULL;
56 else {
57 nfserr = nfserrno(err);
58 goto fail;
59 }
60 }
61 if (acl == NULL) {
62 /* Solaris returns the inode's minimum ACL. */
63
64 struct inode *inode = fh->fh_dentry->d_inode;
65 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
66 }
67 resp->acl_access = acl;
68 }
69 if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
70 /* Check how Solaris handles requests for the Default ACL
71 of a non-directory! */
72
73 acl = nfsd_get_posix_acl(fh, ACL_TYPE_DEFAULT);
74 if (IS_ERR(acl)) {
75 int err = PTR_ERR(acl);
76
77 if (err == -ENODATA || err == -EOPNOTSUPP)
78 acl = NULL;
79 else {
80 nfserr = nfserrno(err);
81 goto fail;
82 }
83 }
84 resp->acl_default = acl;
85 }
86
87 /* resp->acl_{access,default} are released in nfssvc_release_getacl. */
88 RETURN_STATUS(0);
89
90fail:
91 posix_acl_release(resp->acl_access);
92 posix_acl_release(resp->acl_default);
93 RETURN_STATUS(nfserr);
94}
95
96/*
97 * Set the Access and/or Default ACL of a file.
98 */
Al Viro7111c662006-10-19 23:28:45 -070099static __be32 nfsacld_proc_setacl(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000100 struct nfsd3_setaclargs *argp,
101 struct nfsd_attrstat *resp)
102{
103 svc_fh *fh;
Al Viroc4d987b2006-10-19 23:29:00 -0700104 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000105
106 dprintk("nfsd: SETACL(2acl) %s\n", SVCFH_fmt(&argp->fh));
107
108 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200109 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000110
111 if (!nfserr) {
112 nfserr = nfserrno( nfsd_set_posix_acl(
113 fh, ACL_TYPE_ACCESS, argp->acl_access) );
114 }
115 if (!nfserr) {
116 nfserr = nfserrno( nfsd_set_posix_acl(
117 fh, ACL_TYPE_DEFAULT, argp->acl_default) );
118 }
119
120 /* argp->acl_{access,default} may have been allocated in
121 nfssvc_decode_setaclargs. */
122 posix_acl_release(argp->acl_access);
123 posix_acl_release(argp->acl_default);
124 return nfserr;
125}
126
127/*
128 * Check file attributes
129 */
Al Viro7111c662006-10-19 23:28:45 -0700130static __be32 nfsacld_proc_getattr(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000131 struct nfsd_fhandle *argp, struct nfsd_attrstat *resp)
132{
133 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
134
135 fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200136 return fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000137}
138
139/*
140 * Check file access
141 */
Al Viro7111c662006-10-19 23:28:45 -0700142static __be32 nfsacld_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000143 struct nfsd3_accessres *resp)
144{
Al Viroc4d987b2006-10-19 23:29:00 -0700145 __be32 nfserr;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000146
147 dprintk("nfsd: ACCESS(2acl) %s 0x%x\n",
148 SVCFH_fmt(&argp->fh),
149 argp->access);
150
151 fh_copy(&resp->fh, &argp->fh);
152 resp->access = argp->access;
153 nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
154 return nfserr;
155}
156
157/*
158 * XDR decode functions
159 */
Al Viro131a21c2006-10-19 23:28:56 -0700160static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000161 struct nfsd3_getaclargs *argp)
162{
163 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
164 return 0;
165 argp->mask = ntohl(*p); p++;
166
167 return xdr_argsize_check(rqstp, p);
168}
169
170
Al Viro131a21c2006-10-19 23:28:56 -0700171static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000172 struct nfsd3_setaclargs *argp)
173{
174 struct kvec *head = rqstp->rq_arg.head;
175 unsigned int base;
176 int n;
177
178 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
179 return 0;
180 argp->mask = ntohl(*p++);
181 if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT) ||
182 !xdr_argsize_check(rqstp, p))
183 return 0;
184
185 base = (char *)p - (char *)head->iov_base;
186 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
187 (argp->mask & NFS_ACL) ?
188 &argp->acl_access : NULL);
189 if (n > 0)
190 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
191 (argp->mask & NFS_DFACL) ?
192 &argp->acl_default : NULL);
193 return (n > 0);
194}
195
Al Viro131a21c2006-10-19 23:28:56 -0700196static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000197 struct nfsd_fhandle *argp)
198{
199 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
200 return 0;
201 return xdr_argsize_check(rqstp, p);
202}
203
Al Viro131a21c2006-10-19 23:28:56 -0700204static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000205 struct nfsd3_accessargs *argp)
206{
207 if (!(p = nfs2svc_decode_fh(p, &argp->fh)))
208 return 0;
209 argp->access = ntohl(*p++);
210
211 return xdr_argsize_check(rqstp, p);
212}
213
214/*
215 * XDR encode functions
216 */
217
Peter Staubach1b7e0402009-11-02 16:59:07 -0500218/*
219 * There must be an encoding function for void results so svc_process
220 * will work properly.
221 */
222int
223nfsaclsvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
224{
225 return xdr_ressize_check(rqstp, p);
226}
227
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000228/* GETACL */
Al Viro131a21c2006-10-19 23:28:56 -0700229static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000230 struct nfsd3_getaclres *resp)
231{
232 struct dentry *dentry = resp->fh.fh_dentry;
Prasad Paefa89d2007-10-24 15:14:32 -0500233 struct inode *inode;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000234 struct kvec *head = rqstp->rq_res.head;
235 unsigned int base;
236 int n;
Jesper Juhlcb65a5b2006-12-08 02:39:39 -0800237 int w;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000238
Prasad Paefa89d2007-10-24 15:14:32 -0500239 /*
240 * Since this is version 2, the check for nfserr in
241 * nfsd_dispatch actually ensures the following cannot happen.
242 * However, it seems fragile to depend on that.
243 */
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000244 if (dentry == NULL || dentry->d_inode == NULL)
245 return 0;
246 inode = dentry->d_inode;
247
248 p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
249 *p++ = htonl(resp->mask);
250 if (!xdr_ressize_check(rqstp, p))
251 return 0;
252 base = (char *)p - (char *)head->iov_base;
253
Jesper Juhlcb65a5b2006-12-08 02:39:39 -0800254 rqstp->rq_res.page_len = w = nfsacl_size(
255 (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
256 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000257 while (w > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700258 if (!rqstp->rq_respages[rqstp->rq_resused++])
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000259 return 0;
260 w -= PAGE_SIZE;
261 }
262
263 n = nfsacl_encode(&rqstp->rq_res, base, inode,
264 resp->acl_access,
265 resp->mask & NFS_ACL, 0);
266 if (n > 0)
267 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
268 resp->acl_default,
269 resp->mask & NFS_DFACL,
270 NFS_ACL_DEFAULT);
271 if (n <= 0)
272 return 0;
273 return 1;
274}
275
Al Viro131a21c2006-10-19 23:28:56 -0700276static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000277 struct nfsd_attrstat *resp)
278{
279 p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
280 return xdr_ressize_check(rqstp, p);
281}
282
283/* ACCESS */
Al Viro131a21c2006-10-19 23:28:56 -0700284static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000285 struct nfsd3_accessres *resp)
286{
287 p = nfs2svc_encode_fattr(rqstp, p, &resp->fh);
288 *p++ = htonl(resp->access);
289 return xdr_ressize_check(rqstp, p);
290}
291
292/*
293 * XDR release functions
294 */
Al Viro131a21c2006-10-19 23:28:56 -0700295static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000296 struct nfsd3_getaclres *resp)
297{
298 fh_put(&resp->fh);
299 posix_acl_release(resp->acl_access);
300 posix_acl_release(resp->acl_default);
301 return 1;
302}
303
Greg Banksc9ce2282007-02-20 10:12:34 +1100304static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
305 struct nfsd_attrstat *resp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000306{
307 fh_put(&resp->fh);
308 return 1;
309}
310
Greg Banksc9ce2282007-02-20 10:12:34 +1100311static int nfsaclsvc_release_access(struct svc_rqst *rqstp, __be32 *p,
312 struct nfsd3_accessres *resp)
313{
314 fh_put(&resp->fh);
315 return 1;
316}
317
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000318#define nfsaclsvc_decode_voidargs NULL
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000319#define nfsaclsvc_release_void NULL
320#define nfsd3_fhandleargs nfsd_fhandle
321#define nfsd3_attrstatres nfsd_attrstat
322#define nfsd3_voidres nfsd3_voidargs
323struct nfsd3_voidargs { int dummy; };
324
325#define PROC(name, argt, rest, relt, cache, respsize) \
326 { (svc_procfunc) nfsacld_proc_##name, \
327 (kxdrproc_t) nfsaclsvc_decode_##argt##args, \
328 (kxdrproc_t) nfsaclsvc_encode_##rest##res, \
329 (kxdrproc_t) nfsaclsvc_release_##relt, \
330 sizeof(struct nfsd3_##argt##args), \
331 sizeof(struct nfsd3_##rest##res), \
332 0, \
333 cache, \
334 respsize, \
335 }
336
337#define ST 1 /* status*/
338#define AT 21 /* attributes */
339#define pAT (1+AT) /* post attributes - conditional */
340#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
341
342static struct svc_procedure nfsd_acl_procedures2[] = {
343 PROC(null, void, void, void, RC_NOCACHE, ST),
344 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
Greg Banksc9ce2282007-02-20 10:12:34 +1100345 PROC(setacl, setacl, attrstat, attrstat, RC_NOCACHE, ST+AT),
346 PROC(getattr, fhandle, attrstat, attrstat, RC_NOCACHE, ST+AT),
347 PROC(access, access, access, access, RC_NOCACHE, ST+AT+1),
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000348};
349
350struct svc_version nfsd_acl_version2 = {
351 .vs_vers = 2,
352 .vs_nproc = 5,
353 .vs_proc = nfsd_acl_procedures2,
354 .vs_dispatch = nfsd_dispatch,
355 .vs_xdrsize = NFS3_SVC_XDRSIZE,
Peter Staubach1b7e0402009-11-02 16:59:07 -0500356 .vs_hidden = 0,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000357};