]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfsd/nfs2acl.c
sunrpc: properly type pc_func callbacks
[karo-tx-linux.git] / fs / nfsd / nfs2acl.c
1 /*
2  * Process version 2 NFSACL requests.
3  *
4  * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
5  */
6
7 #include "nfsd.h"
8 /* FIXME: nfsacl.h is a broken header */
9 #include <linux/nfsacl.h>
10 #include <linux/gfp.h>
11 #include "cache.h"
12 #include "xdr3.h"
13 #include "vfs.h"
14
15 #define NFSDDBG_FACILITY                NFSDDBG_PROC
16 #define RETURN_STATUS(st)       { resp->status = (st); return (st); }
17
18 /*
19  * NULL call.
20  */
21 static __be32
22 nfsacld_proc_null(struct svc_rqst *rqstp)
23 {
24         return nfs_ok;
25 }
26
27 /*
28  * Get the Access and/or Default ACL of a file.
29  */
30 static __be32 nfsacld_proc_getacl(struct svc_rqst *rqstp)
31 {
32         struct nfsd3_getaclargs *argp = rqstp->rq_argp;
33         struct nfsd3_getaclres *resp = rqstp->rq_resp;
34         struct posix_acl *acl;
35         struct inode *inode;
36         svc_fh *fh;
37         __be32 nfserr = 0;
38
39         dprintk("nfsd: GETACL(2acl)   %s\n", SVCFH_fmt(&argp->fh));
40
41         fh = fh_copy(&resp->fh, &argp->fh);
42         nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
43         if (nfserr)
44                 RETURN_STATUS(nfserr);
45
46         inode = d_inode(fh->fh_dentry);
47
48         if (argp->mask & ~NFS_ACL_MASK)
49                 RETURN_STATUS(nfserr_inval);
50         resp->mask = argp->mask;
51
52         nfserr = fh_getattr(fh, &resp->stat);
53         if (nfserr)
54                 RETURN_STATUS(nfserr);
55
56         if (resp->mask & (NFS_ACL|NFS_ACLCNT)) {
57                 acl = get_acl(inode, ACL_TYPE_ACCESS);
58                 if (acl == NULL) {
59                         /* Solaris returns the inode's minimum ACL. */
60                         acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
61                 }
62                 if (IS_ERR(acl)) {
63                         nfserr = nfserrno(PTR_ERR(acl));
64                         goto fail;
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                 acl = get_acl(inode, ACL_TYPE_DEFAULT);
72                 if (IS_ERR(acl)) {
73                         nfserr = nfserrno(PTR_ERR(acl));
74                         goto fail;
75                 }
76                 resp->acl_default = acl;
77         }
78
79         /* resp->acl_{access,default} are released in nfssvc_release_getacl. */
80         RETURN_STATUS(0);
81
82 fail:
83         posix_acl_release(resp->acl_access);
84         posix_acl_release(resp->acl_default);
85         RETURN_STATUS(nfserr);
86 }
87
88 /*
89  * Set the Access and/or Default ACL of a file.
90  */
91 static __be32 nfsacld_proc_setacl(struct svc_rqst *rqstp)
92 {
93         struct nfsd3_setaclargs *argp = rqstp->rq_argp;
94         struct nfsd_attrstat *resp = rqstp->rq_resp;
95         struct inode *inode;
96         svc_fh *fh;
97         __be32 nfserr = 0;
98         int error;
99
100         dprintk("nfsd: SETACL(2acl)   %s\n", SVCFH_fmt(&argp->fh));
101
102         fh = fh_copy(&resp->fh, &argp->fh);
103         nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
104         if (nfserr)
105                 goto out;
106
107         inode = d_inode(fh->fh_dentry);
108
109         error = fh_want_write(fh);
110         if (error)
111                 goto out_errno;
112
113         fh_lock(fh);
114
115         error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access);
116         if (error)
117                 goto out_drop_lock;
118         error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default);
119         if (error)
120                 goto out_drop_lock;
121
122         fh_unlock(fh);
123
124         fh_drop_write(fh);
125
126         nfserr = fh_getattr(fh, &resp->stat);
127
128 out:
129         /* argp->acl_{access,default} may have been allocated in
130            nfssvc_decode_setaclargs. */
131         posix_acl_release(argp->acl_access);
132         posix_acl_release(argp->acl_default);
133         return nfserr;
134 out_drop_lock:
135         fh_unlock(fh);
136         fh_drop_write(fh);
137 out_errno:
138         nfserr = nfserrno(error);
139         goto out;
140 }
141
142 /*
143  * Check file attributes
144  */
145 static __be32 nfsacld_proc_getattr(struct svc_rqst *rqstp)
146 {
147         struct nfsd_fhandle *argp = rqstp->rq_argp;
148         struct nfsd_attrstat *resp = rqstp->rq_resp;
149         __be32 nfserr;
150         dprintk("nfsd: GETATTR  %s\n", SVCFH_fmt(&argp->fh));
151
152         fh_copy(&resp->fh, &argp->fh);
153         nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
154         if (nfserr)
155                 return nfserr;
156         nfserr = fh_getattr(&resp->fh, &resp->stat);
157         return nfserr;
158 }
159
160 /*
161  * Check file access
162  */
163 static __be32 nfsacld_proc_access(struct svc_rqst *rqstp)
164 {
165         struct nfsd3_accessargs *argp = rqstp->rq_argp;
166         struct nfsd3_accessres *resp = rqstp->rq_resp;
167         __be32 nfserr;
168
169         dprintk("nfsd: ACCESS(2acl)   %s 0x%x\n",
170                         SVCFH_fmt(&argp->fh),
171                         argp->access);
172
173         fh_copy(&resp->fh, &argp->fh);
174         resp->access = argp->access;
175         nfserr = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
176         if (nfserr)
177                 return nfserr;
178         nfserr = fh_getattr(&resp->fh, &resp->stat);
179         return nfserr;
180 }
181
182 /*
183  * XDR decode functions
184  */
185 static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
186                 struct nfsd3_getaclargs *argp)
187 {
188         p = nfs2svc_decode_fh(p, &argp->fh);
189         if (!p)
190                 return 0;
191         argp->mask = ntohl(*p); p++;
192
193         return xdr_argsize_check(rqstp, p);
194 }
195
196
197 static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
198                 struct nfsd3_setaclargs *argp)
199 {
200         struct kvec *head = rqstp->rq_arg.head;
201         unsigned int base;
202         int n;
203
204         p = nfs2svc_decode_fh(p, &argp->fh);
205         if (!p)
206                 return 0;
207         argp->mask = ntohl(*p++);
208         if (argp->mask & ~NFS_ACL_MASK ||
209             !xdr_argsize_check(rqstp, p))
210                 return 0;
211
212         base = (char *)p - (char *)head->iov_base;
213         n = nfsacl_decode(&rqstp->rq_arg, base, NULL,
214                           (argp->mask & NFS_ACL) ?
215                           &argp->acl_access : NULL);
216         if (n > 0)
217                 n = nfsacl_decode(&rqstp->rq_arg, base + n, NULL,
218                                   (argp->mask & NFS_DFACL) ?
219                                   &argp->acl_default : NULL);
220         return (n > 0);
221 }
222
223 static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p,
224                 struct nfsd_fhandle *argp)
225 {
226         p = nfs2svc_decode_fh(p, &argp->fh);
227         if (!p)
228                 return 0;
229         return xdr_argsize_check(rqstp, p);
230 }
231
232 static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
233                 struct nfsd3_accessargs *argp)
234 {
235         p = nfs2svc_decode_fh(p, &argp->fh);
236         if (!p)
237                 return 0;
238         argp->access = ntohl(*p++);
239
240         return xdr_argsize_check(rqstp, p);
241 }
242
243 /*
244  * XDR encode functions
245  */
246
247 /*
248  * There must be an encoding function for void results so svc_process
249  * will work properly.
250  */
251 static int nfsaclsvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
252 {
253         return xdr_ressize_check(rqstp, p);
254 }
255
256 /* GETACL */
257 static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
258                 struct nfsd3_getaclres *resp)
259 {
260         struct dentry *dentry = resp->fh.fh_dentry;
261         struct inode *inode;
262         struct kvec *head = rqstp->rq_res.head;
263         unsigned int base;
264         int n;
265         int w;
266
267         /*
268          * Since this is version 2, the check for nfserr in
269          * nfsd_dispatch actually ensures the following cannot happen.
270          * However, it seems fragile to depend on that.
271          */
272         if (dentry == NULL || d_really_is_negative(dentry))
273                 return 0;
274         inode = d_inode(dentry);
275
276         p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat);
277         *p++ = htonl(resp->mask);
278         if (!xdr_ressize_check(rqstp, p))
279                 return 0;
280         base = (char *)p - (char *)head->iov_base;
281
282         rqstp->rq_res.page_len = w = nfsacl_size(
283                 (resp->mask & NFS_ACL)   ? resp->acl_access  : NULL,
284                 (resp->mask & NFS_DFACL) ? resp->acl_default : NULL);
285         while (w > 0) {
286                 if (!*(rqstp->rq_next_page++))
287                         return 0;
288                 w -= PAGE_SIZE;
289         }
290
291         n = nfsacl_encode(&rqstp->rq_res, base, inode,
292                           resp->acl_access,
293                           resp->mask & NFS_ACL, 0);
294         if (n > 0)
295                 n = nfsacl_encode(&rqstp->rq_res, base + n, inode,
296                                   resp->acl_default,
297                                   resp->mask & NFS_DFACL,
298                                   NFS_ACL_DEFAULT);
299         return (n > 0);
300 }
301
302 static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p,
303                 struct nfsd_attrstat *resp)
304 {
305         p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat);
306         return xdr_ressize_check(rqstp, p);
307 }
308
309 /* ACCESS */
310 static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
311                 struct nfsd3_accessres *resp)
312 {
313         p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat);
314         *p++ = htonl(resp->access);
315         return xdr_ressize_check(rqstp, p);
316 }
317
318 /*
319  * XDR release functions
320  */
321 static int nfsaclsvc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
322                 struct nfsd3_getaclres *resp)
323 {
324         fh_put(&resp->fh);
325         posix_acl_release(resp->acl_access);
326         posix_acl_release(resp->acl_default);
327         return 1;
328 }
329
330 static int nfsaclsvc_release_attrstat(struct svc_rqst *rqstp, __be32 *p,
331                 struct nfsd_attrstat *resp)
332 {
333         fh_put(&resp->fh);
334         return 1;
335 }
336
337 static int nfsaclsvc_release_access(struct svc_rqst *rqstp, __be32 *p,
338                struct nfsd3_accessres *resp)
339 {
340        fh_put(&resp->fh);
341        return 1;
342 }
343
344 #define nfsaclsvc_decode_voidargs       NULL
345 #define nfsaclsvc_release_void          NULL
346 #define nfsd3_fhandleargs       nfsd_fhandle
347 #define nfsd3_attrstatres       nfsd_attrstat
348 #define nfsd3_voidres           nfsd3_voidargs
349 struct nfsd3_voidargs { int dummy; };
350
351 #define PROC(name, argt, rest, relt, cache, respsize)                   \
352 {                                                                       \
353         .pc_func        = nfsacld_proc_##name,                          \
354         .pc_decode      = (kxdrproc_t) nfsaclsvc_decode_##argt##args,   \
355         .pc_encode      = (kxdrproc_t) nfsaclsvc_encode_##rest##res,    \
356         .pc_release     = (kxdrproc_t) nfsaclsvc_release_##relt,        \
357         .pc_argsize     = sizeof(struct nfsd3_##argt##args),            \
358         .pc_ressize     = sizeof(struct nfsd3_##rest##res),             \
359         .pc_cachetype   = cache,                                        \
360         .pc_xdrressize  = respsize,                                     \
361 }
362
363 #define ST 1            /* status*/
364 #define AT 21           /* attributes */
365 #define pAT (1+AT)      /* post attributes - conditional */
366 #define ACL (1+NFS_ACL_MAX_ENTRIES*3)  /* Access Control List */
367
368 static struct svc_procedure             nfsd_acl_procedures2[] = {
369   PROC(null,    void,           void,           void,     RC_NOCACHE, ST),
370   PROC(getacl,  getacl,         getacl,         getacl,   RC_NOCACHE, ST+1+2*(1+ACL)),
371   PROC(setacl,  setacl,         attrstat,       attrstat, RC_NOCACHE, ST+AT),
372   PROC(getattr, fhandle,        attrstat,       attrstat, RC_NOCACHE, ST+AT),
373   PROC(access,  access,         access,         access,   RC_NOCACHE, ST+AT+1),
374 };
375
376 struct svc_version      nfsd_acl_version2 = {
377                 .vs_vers        = 2,
378                 .vs_nproc       = 5,
379                 .vs_proc        = nfsd_acl_procedures2,
380                 .vs_dispatch    = nfsd_dispatch,
381                 .vs_xdrsize     = NFS3_SVC_XDRSIZE,
382 };