nfsd: make fs/nfsd/vfs.h for common includes
[safe/jmp/linux-2.6] / fs / nfsd / nfs3acl.c
1 /*
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>
16 #include "vfs.h"
17
18 #define RETURN_STATUS(st)       { resp->status = (st); return (st); }
19
20 /*
21  * NULL call.
22  */
23 static __be32
24 nfsd3_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  */
32 static __be32 nfsd3_proc_getacl(struct svc_rqst * rqstp,
33                 struct nfsd3_getaclargs *argp, struct nfsd3_getaclres *resp)
34 {
35         svc_fh *fh;
36         struct posix_acl *acl;
37         __be32 nfserr = 0;
38
39         fh = fh_copy(&resp->fh, &argp->fh);
40         nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP);
41         if (nfserr)
42                 RETURN_STATUS(nfserr);
43
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
89 fail:
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  */
98 static __be32 nfsd3_proc_setacl(struct svc_rqst * rqstp,
99                 struct nfsd3_setaclargs *argp,
100                 struct nfsd3_attrstat *resp)
101 {
102         svc_fh *fh;
103         __be32 nfserr = 0;
104
105         fh = fh_copy(&resp->fh, &argp->fh);
106         nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR);
107
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  */
127 static int nfs3svc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p,
128                 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
138 static int nfs3svc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p,
139                 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 */
168 static int nfs3svc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p,
169                 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;
176                 struct kvec *head = rqstp->rq_res.head;
177                 unsigned int base;
178                 int n;
179                 int w;
180
181                 *p++ = htonl(resp->mask);
182                 if (!xdr_ressize_check(rqstp, p))
183                         return 0;
184                 base = (char *)p - (char *)head->iov_base;
185
186                 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);
189                 while (w > 0) {
190                         if (!rqstp->rq_respages[rqstp->rq_resused++])
191                                 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 */
213 static int nfs3svc_encode_setaclres(struct svc_rqst *rqstp, __be32 *p,
214                 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  */
224 static int nfs3svc_release_getacl(struct svc_rqst *rqstp, __be32 *p,
225                 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
237 struct 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
256 static 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
262 struct 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,
268                 .vs_hidden      = 0,
269 };
270