blob: 526d85a65f76696c315c9e7f8657395527b7c412 [file] [log] [blame]
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +00001/*
2 * linux/fs/nfsd/nfs3acl.c
3 *
4 * Process version 3 NFSACL requests.
5 *
6 * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
7 */
8
9#include <linux/sunrpc/svc.h>
10#include <linux/nfs3.h>
11#include <linux/nfsd/nfsd.h>
12#include <linux/nfsd/cache.h>
13#include <linux/nfsd/xdr3.h>
14#include <linux/posix_acl.h>
15#include <linux/nfsacl.h>
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050016#include "vfs.h"
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000017
18#define RETURN_STATUS(st) { resp->status = (st); return (st); }
19
20/*
21 * NULL call.
22 */
Al Viro7111c662006-10-19 23:28:45 -070023static __be32
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000024nfsd3_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
25{
26 return nfs_ok;
27}
28
29/*
30 * Get the Access and/or Default ACL of a file.
31 */
Al Viro7111c662006-10-19 23:28:45 -070032static __be32 nfsd3_proc_getacl(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000033 struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
34{
35 svc_fh *fh;
36 struct posix_acl *acl;
Al Viroc4d987b2006-10-19 23:29:00 -070037 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000038
39 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +020040 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
41 if (nfserr)
J. Bruce Fieldsac8587d2007-11-12 16:05:02 -050042 RETURN_STATUS(nfserr);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000043
44 if (argp->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
45 RETURN_STATUS(nfserr_inval);
46 resp->mask = argp->mask;
47
48 if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
49 acl = nfsd_get_posix_acl(fh, ACL_TYPE_ACCESS);
50 if (IS_ERR(acl)) {
51 int err = PTR_ERR(acl);
52
53 if (err == -ENODATA || err == -EOPNOTSUPP)
54 acl = NULL;
55 else {
56 nfserr = nfserrno(err);
57 goto fail;
58 }
59 }
60 if (acl == NULL) {
61 /* Solaris returns the inode's minimum ACL. */
62
63 struct inode *inode = fh->fh_dentry->d_inode;
64 acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
65 }
66 resp->acl_access = acl;
67 }
68 if (resp->mask & (NFS_DFACL|NFS_DFACLCNT)) {
69 /* Check how Solaris handles requests for the Default ACL
70 of a non-directory! */
71
72 acl = nfsd_get_posix_acl(fh, ACL_TYPE_DEFAULT);
73 if (IS_ERR(acl)) {
74 int err = PTR_ERR(acl);
75
76 if (err == -ENODATA || err == -EOPNOTSUPP)
77 acl = NULL;
78 else {
79 nfserr = nfserrno(err);
80 goto fail;
81 }
82 }
83 resp->acl_default = acl;
84 }
85
86 /* resp->acl_{access,default} are released in nfs3svc_release_getacl. */
87 RETURN_STATUS(0);
88
89fail:
90 posix_acl_release(resp->acl_access);
91 posix_acl_release(resp->acl_default);
92 RETURN_STATUS(nfserr);
93}
94
95/*
96 * Set the Access and/or Default ACL of a file.
97 */
Al Viro7111c662006-10-19 23:28:45 -070098static __be32 nfsd3_proc_setacl(struct svc_rqst * rqstp,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000099 struct nfsd3_setaclargs *argp,
100 struct nfsd3_attrstat *resp)
101{
102 svc_fh *fh;
Al Viroc4d987b2006-10-19 23:29:00 -0700103 __be32 nfserr = 0;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000104
105 fh = fh_copy(&resp->fh, &argp->fh);
Miklos Szeredi8837abc2008-06-16 13:20:29 +0200106 nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000107
108 if (!nfserr) {
109 nfserr = nfserrno( nfsd_set_posix_acl(
110 fh, ACL_TYPE_ACCESS, argp->acl_access) );
111 }
112 if (!nfserr) {
113 nfserr = nfserrno( nfsd_set_posix_acl(
114 fh, ACL_TYPE_DEFAULT, argp->acl_default) );
115 }
116
117 /* 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{
130 if (!(p = nfs3svc_decode_fh(p, &args->fh)))
131 return 0;
132 args->mask = ntohl(*p); p++;
133
134 return xdr_argsize_check(rqstp, p);
135}
136
137
Al Viro91f07162006-10-19 23:28:57 -0700138static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000139 struct nfsd3_setaclargs *args)
140{
141 struct kvec *head = rqstp->rq_arg.head;
142 unsigned int base;
143 int n;
144
145 if (!(p = nfs3svc_decode_fh(p, &args->fh)))
146 return 0;
147 args->mask = ntohl(*p++);
148 if (args->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT) ||
149 !xdr_argsize_check(rqstp, p))
150 return 0;
151
152 base = (char *)p - (char *)head->iov_base;
153 n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
154 (args->mask & NFS_ACL) ?
155 &args->acl_access : NULL);
156 if (n > 0)
157 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
158 (args->mask & NFS_DFACL) ?
159 &args->acl_default : NULL);
160 return (n > 0);
161}
162
163/*
164 * XDR encode functions
165 */
166
167/* GETACL */
Al Viro91f07162006-10-19 23:28:57 -0700168static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000169 struct nfsd3_getaclres *resp)
170{
171 struct dentry *dentry = resp->fh.fh_dentry;
172
173 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
174 if (resp->status == 0 && dentry && dentry->d_inode) {
175 struct inode *inode = dentry->d_inode;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000176 struct kvec *head = rqstp->rq_res.head;
177 unsigned int base;
178 int n;
Jesper Juhl14d2b592006-12-08 02:39:40 -0800179 int w;
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000180
181 *p++ = htonl(resp->mask);
182 if (!xdr_ressize_check(rqstp, p))
183 return 0;
184 base = (char *)p - (char *)head->iov_base;
185
Jesper Juhl14d2b592006-12-08 02:39:40 -0800186 rqstp->rq_res.page_len = w = nfsacl_size(
187 (resp->mask & NFS_ACL) ? resp->acl_access : NULL,
188 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000189 while (w > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700190 if (!rqstp->rq_respages[rqstp->rq_resused++])
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000191 return 0;
192 w -= PAGE_SIZE;
193 }
194
195 n = nfsacl_encode(&rqstp->rq_res, base, inode,
196 resp->acl_access,
197 resp->mask & NFS_ACL, 0);
198 if (n > 0)
199 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
200 resp->acl_default,
201 resp->mask & NFS_DFACL,
202 NFS_ACL_DEFAULT);
203 if (n <= 0)
204 return 0;
205 } else
206 if (!xdr_ressize_check(rqstp, p))
207 return 0;
208
209 return 1;
210}
211
212/* SETACL */
Al Viro91f07162006-10-19 23:28:57 -0700213static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000214 struct nfsd3_attrstat *resp)
215{
216 p = nfs3svc_encode_post_op_attr(rqstp, p, &resp->fh);
217
218 return xdr_ressize_check(rqstp, p);
219}
220
221/*
222 * XDR release functions
223 */
Al Viro91f07162006-10-19 23:28:57 -0700224static int nfs3svc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000225 struct nfsd3_getaclres *resp)
226{
227 fh_put(&resp->fh);
228 posix_acl_release(resp->acl_access);
229 posix_acl_release(resp->acl_default);
230 return 1;
231}
232
233#define nfs3svc_decode_voidargs NULL
234#define nfs3svc_release_void NULL
235#define nfsd3_setaclres nfsd3_attrstat
236#define nfsd3_voidres nfsd3_voidargs
237struct nfsd3_voidargs { int dummy; };
238
239#define PROC(name, argt, rest, relt, cache, respsize) \
240 { (svc_procfunc) nfsd3_proc_##name, \
241 (kxdrproc_t) nfs3svc_decode_##argt##args, \
242 (kxdrproc_t) nfs3svc_encode_##rest##res, \
243 (kxdrproc_t) nfs3svc_release_##relt, \
244 sizeof(struct nfsd3_##argt##args), \
245 sizeof(struct nfsd3_##rest##res), \
246 0, \
247 cache, \
248 respsize, \
249 }
250
251#define ST 1 /* status*/
252#define AT 21 /* attributes */
253#define pAT (1+AT) /* post attributes - conditional */
254#define ACL (1+NFS_ACL_MAX_ENTRIES*3) /* Access Control List */
255
256static struct svc_procedure nfsd_acl_procedures3[] = {
257 PROC(null, void, void, void, RC_NOCACHE, ST),
258 PROC(getacl, getacl, getacl, getacl, RC_NOCACHE, ST+1+2*(1+ACL)),
259 PROC(setacl, setacl, setacl, fhandle, RC_NOCACHE, ST+pAT),
260};
261
262struct svc_version nfsd_acl_version3 = {
263 .vs_vers = 3,
264 .vs_nproc = 3,
265 .vs_proc = nfsd_acl_procedures3,
266 .vs_dispatch = nfsd_dispatch,
267 .vs_xdrsize = NFS3_SVC_XDRSIZE,
Peter Staubach1b7e0402009-11-02 16:59:07 -0500268 .vs_hidden = 0,
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000269};
270