nfsd: Move private headers to source directory
[safe/jmp/linux-2.6] / fs / nfsd / nfs2acl.c
1 /*
2  * linux/fs/nfsd/nfs2acl.c
3  *
4  * Process version 2 NFSACL requests.
5  *
6  * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
7  */
8
9 #include "nfsd.h"
10 /* FIXME: nfsacl.h is a broken header */
11 #include <linux/nfsacl.h>
12 #include "cache.h"
13 #include "xdr3.h"
14 #include "vfs.h"
15
16 #define NFSDDBG_FACILITY                NFSDDBG_PROC
17 #define RETURN_STATUS(st)       { resp->status = (st); return (st); }
18
19 /*
20  * NULL call.
21  */
22 static __be32
23 nfsacld_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  */
31 static __be32 nfsacld_proc_getacl(struct svc_rqst * rqstp,
32                 struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
33 {
34         svc_fh *fh;
35         struct posix_acl *acl;
36         __be32 nfserr = 0;
37
38         dprintk("nfsd: GETACL(2acl)   %s\n", SVCFH_fmt(&argp->fh));
39
40         fh = fh_copy(&resp->fh, &argp->fh);
41         nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
42         if (nfserr)
43                 RETURN_STATUS(nfserr);
44
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
90 fail:
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  */
99 static __be32 nfsacld_proc_setacl(struct svc_rqst * rqstp,
100                 struct nfsd3_setaclargs *argp,
101                 struct nfsd_attrstat *resp)
102 {
103         svc_fh *fh;
104         __be32 nfserr = 0;
105
106         dprintk("nfsd: SETACL(2acl)   %s\n", SVCFH_fmt(&argp->fh));
107
108         fh = fh_copy(&resp->fh, &argp->fh);
109         nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
110
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  */
130 static __be32 nfsacld_proc_getattr(struct svc_rqst * rqstp,
131                 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);
136         return fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
137 }
138
139 /*
140  * Check file access
141  */
142 static __be32 nfsacld_proc_access(struct svc_rqst *rqstp, struct nfsd3_accessargs *argp,
143                 struct nfsd3_accessres *resp)
144 {
145         __be32 nfserr;
146
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  */
160 static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
161                 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
171 static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
172                 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
196 static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p,
197                 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
204 static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
205                 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
218 /*
219  * There must be an encoding function for void results so svc_process
220  * will work properly.
221  */
222 int
223 nfsaclsvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
224 {
225         return xdr_ressize_check(rqstp, p);
226 }
227
228 /* GETACL */
229 static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
230                 struct nfsd3_getaclres *resp)
231 {
232         struct dentry *dentry = resp->fh.fh_dentry;
233         struct inode *inode;
234         struct kvec *head = rqstp->rq_res.head;
235         unsigned int base;
236         int n;
237         int w;
238
239         /*
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          */
244         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
254         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);
257         while (w > 0) {
258                 if (!rqstp->rq_respages[rqstp->rq_resused++])
259                         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
276 static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p,
277                 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 */
284 static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
285                 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  */
295 static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
296                 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
304 static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
305                 struct nfsd_attrstat *resp)
306 {
307         fh_put(&resp->fh);
308         return 1;
309 }
310
311 static 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
318 #define nfsaclsvc_decode_voidargs       NULL
319 #define nfsaclsvc_release_void          NULL
320 #define nfsd3_fhandleargs       nfsd_fhandle
321 #define nfsd3_attrstatres       nfsd_attrstat
322 #define nfsd3_voidres           nfsd3_voidargs
323 struct 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
342 static 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)),
345   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),
348 };
349
350 struct 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,
356                 .vs_hidden      = 0,
357 };