Commit | Line | Data |
---|---|---|
a257cdd0 | 1 | /* |
a257cdd0 AG |
2 | * Process version 2 NFSACL requests. |
3 | * | |
4 | * Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de> | |
5 | */ | |
6 | ||
9a74af21 BH |
7 | #include "nfsd.h" |
8 | /* FIXME: nfsacl.h is a broken header */ | |
a257cdd0 | 9 | #include <linux/nfsacl.h> |
5a0e3ad6 | 10 | #include <linux/gfp.h> |
9a74af21 BH |
11 | #include "cache.h" |
12 | #include "xdr3.h" | |
0a3adade | 13 | #include "vfs.h" |
a257cdd0 AG |
14 | |
15 | #define NFSDDBG_FACILITY NFSDDBG_PROC | |
16 | #define RETURN_STATUS(st) { resp->status = (st); return (st); } | |
17 | ||
18 | /* | |
19 | * NULL call. | |
20 | */ | |
7111c66e | 21 | static __be32 |
a6beb732 | 22 | nfsacld_proc_null(struct svc_rqst *rqstp) |
a257cdd0 AG |
23 | { |
24 | return nfs_ok; | |
25 | } | |
26 | ||
27 | /* | |
28 | * Get the Access and/or Default ACL of a file. | |
29 | */ | |
a6beb732 | 30 | static __be32 nfsacld_proc_getacl(struct svc_rqst *rqstp) |
a257cdd0 | 31 | { |
a6beb732 CH |
32 | struct nfsd3_getaclargs *argp = rqstp->rq_argp; |
33 | struct nfsd3_getaclres *resp = rqstp->rq_resp; | |
a257cdd0 | 34 | struct posix_acl *acl; |
4ac7249e CH |
35 | struct inode *inode; |
36 | svc_fh *fh; | |
c4d987ba | 37 | __be32 nfserr = 0; |
a257cdd0 AG |
38 | |
39 | dprintk("nfsd: GETACL(2acl) %s\n", SVCFH_fmt(&argp->fh)); | |
40 | ||
41 | fh = fh_copy(&resp->fh, &argp->fh); | |
8837abca MS |
42 | nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_NOP); |
43 | if (nfserr) | |
ac8587dc | 44 | RETURN_STATUS(nfserr); |
a257cdd0 | 45 | |
2b0143b5 | 46 | inode = d_inode(fh->fh_dentry); |
4ac7249e | 47 | |
7b8f4586 | 48 | if (argp->mask & ~NFS_ACL_MASK) |
a257cdd0 AG |
49 | RETURN_STATUS(nfserr_inval); |
50 | resp->mask = argp->mask; | |
51 | ||
4f4a4fad BF |
52 | nfserr = fh_getattr(fh, &resp->stat); |
53 | if (nfserr) | |
7b8f4586 | 54 | RETURN_STATUS(nfserr); |
4f4a4fad | 55 | |
a257cdd0 | 56 | if (resp->mask & (NFS_ACL|NFS_ACLCNT)) { |
4ac7249e | 57 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
a257cdd0 AG |
58 | if (acl == NULL) { |
59 | /* Solaris returns the inode's minimum ACL. */ | |
a257cdd0 AG |
60 | acl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL); |
61 | } | |
35e634b8 KM |
62 | if (IS_ERR(acl)) { |
63 | nfserr = nfserrno(PTR_ERR(acl)); | |
64 | goto fail; | |
65 | } | |
a257cdd0 AG |
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! */ | |
4ac7249e | 71 | acl = get_acl(inode, ACL_TYPE_DEFAULT); |
a257cdd0 | 72 | if (IS_ERR(acl)) { |
4ac7249e CH |
73 | nfserr = nfserrno(PTR_ERR(acl)); |
74 | goto fail; | |
a257cdd0 AG |
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 | */ | |
a6beb732 | 91 | static __be32 nfsacld_proc_setacl(struct svc_rqst *rqstp) |
a257cdd0 | 92 | { |
a6beb732 CH |
93 | struct nfsd3_setaclargs *argp = rqstp->rq_argp; |
94 | struct nfsd_attrstat *resp = rqstp->rq_resp; | |
4ac7249e | 95 | struct inode *inode; |
a257cdd0 | 96 | svc_fh *fh; |
c4d987ba | 97 | __be32 nfserr = 0; |
4ac7249e | 98 | int error; |
a257cdd0 AG |
99 | |
100 | dprintk("nfsd: SETACL(2acl) %s\n", SVCFH_fmt(&argp->fh)); | |
101 | ||
102 | fh = fh_copy(&resp->fh, &argp->fh); | |
8837abca | 103 | nfserr = fh_verify(rqstp, &resp->fh, 0, NFSD_MAY_SATTR); |
4ac7249e CH |
104 | if (nfserr) |
105 | goto out; | |
a257cdd0 | 106 | |
2b0143b5 | 107 | inode = d_inode(fh->fh_dentry); |
a257cdd0 | 108 | |
4ac7249e CH |
109 | error = fh_want_write(fh); |
110 | if (error) | |
111 | goto out_errno; | |
112 | ||
99965378 BH |
113 | fh_lock(fh); |
114 | ||
115 | error = set_posix_acl(inode, ACL_TYPE_ACCESS, argp->acl_access); | |
4ac7249e | 116 | if (error) |
99965378 BH |
117 | goto out_drop_lock; |
118 | error = set_posix_acl(inode, ACL_TYPE_DEFAULT, argp->acl_default); | |
4ac7249e | 119 | if (error) |
99965378 BH |
120 | goto out_drop_lock; |
121 | ||
122 | fh_unlock(fh); | |
4ac7249e CH |
123 | |
124 | fh_drop_write(fh); | |
125 | ||
126 | nfserr = fh_getattr(fh, &resp->stat); | |
127 | ||
128 | out: | |
a257cdd0 AG |
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; | |
99965378 BH |
134 | out_drop_lock: |
135 | fh_unlock(fh); | |
4ac7249e CH |
136 | fh_drop_write(fh); |
137 | out_errno: | |
138 | nfserr = nfserrno(error); | |
139 | goto out; | |
a257cdd0 AG |
140 | } |
141 | ||
142 | /* | |
143 | * Check file attributes | |
144 | */ | |
a6beb732 | 145 | static __be32 nfsacld_proc_getattr(struct svc_rqst *rqstp) |
a257cdd0 | 146 | { |
a6beb732 CH |
147 | struct nfsd_fhandle *argp = rqstp->rq_argp; |
148 | struct nfsd_attrstat *resp = rqstp->rq_resp; | |
4f4a4fad | 149 | __be32 nfserr; |
a257cdd0 AG |
150 | dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh)); |
151 | ||
152 | fh_copy(&resp->fh, &argp->fh); | |
4f4a4fad BF |
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; | |
a257cdd0 AG |
158 | } |
159 | ||
160 | /* | |
161 | * Check file access | |
162 | */ | |
a6beb732 | 163 | static __be32 nfsacld_proc_access(struct svc_rqst *rqstp) |
a257cdd0 | 164 | { |
a6beb732 CH |
165 | struct nfsd3_accessargs *argp = rqstp->rq_argp; |
166 | struct nfsd3_accessres *resp = rqstp->rq_resp; | |
c4d987ba | 167 | __be32 nfserr; |
a257cdd0 AG |
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); | |
4f4a4fad BF |
176 | if (nfserr) |
177 | return nfserr; | |
178 | nfserr = fh_getattr(&resp->fh, &resp->stat); | |
a257cdd0 AG |
179 | return nfserr; |
180 | } | |
181 | ||
182 | /* | |
183 | * XDR decode functions | |
184 | */ | |
131a21c2 | 185 | static int nfsaclsvc_decode_getaclargs(struct svc_rqst *rqstp, __be32 *p, |
a257cdd0 AG |
186 | struct nfsd3_getaclargs *argp) |
187 | { | |
d40aa337 BT |
188 | p = nfs2svc_decode_fh(p, &argp->fh); |
189 | if (!p) | |
a257cdd0 AG |
190 | return 0; |
191 | argp->mask = ntohl(*p); p++; | |
192 | ||
193 | return xdr_argsize_check(rqstp, p); | |
194 | } | |
195 | ||
196 | ||
131a21c2 | 197 | static int nfsaclsvc_decode_setaclargs(struct svc_rqst *rqstp, __be32 *p, |
a257cdd0 AG |
198 | struct nfsd3_setaclargs *argp) |
199 | { | |
200 | struct kvec *head = rqstp->rq_arg.head; | |
201 | unsigned int base; | |
202 | int n; | |
203 | ||
d40aa337 BT |
204 | p = nfs2svc_decode_fh(p, &argp->fh); |
205 | if (!p) | |
a257cdd0 AG |
206 | return 0; |
207 | argp->mask = ntohl(*p++); | |
7b8f4586 | 208 | if (argp->mask & ~NFS_ACL_MASK || |
a257cdd0 AG |
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 | ||
131a21c2 | 223 | static int nfsaclsvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p, |
a257cdd0 AG |
224 | struct nfsd_fhandle *argp) |
225 | { | |
d40aa337 BT |
226 | p = nfs2svc_decode_fh(p, &argp->fh); |
227 | if (!p) | |
a257cdd0 AG |
228 | return 0; |
229 | return xdr_argsize_check(rqstp, p); | |
230 | } | |
231 | ||
131a21c2 | 232 | static int nfsaclsvc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p, |
a257cdd0 AG |
233 | struct nfsd3_accessargs *argp) |
234 | { | |
d40aa337 BT |
235 | p = nfs2svc_decode_fh(p, &argp->fh); |
236 | if (!p) | |
a257cdd0 AG |
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 | ||
1b7e0403 PS |
247 | /* |
248 | * There must be an encoding function for void results so svc_process | |
249 | * will work properly. | |
250 | */ | |
9c0b0ff7 | 251 | static int nfsaclsvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy) |
1b7e0403 PS |
252 | { |
253 | return xdr_ressize_check(rqstp, p); | |
254 | } | |
255 | ||
a257cdd0 | 256 | /* GETACL */ |
131a21c2 | 257 | static int nfsaclsvc_encode_getaclres(struct svc_rqst *rqstp, __be32 *p, |
a257cdd0 AG |
258 | struct nfsd3_getaclres *resp) |
259 | { | |
260 | struct dentry *dentry = resp->fh.fh_dentry; | |
aefa89d1 | 261 | struct inode *inode; |
a257cdd0 AG |
262 | struct kvec *head = rqstp->rq_res.head; |
263 | unsigned int base; | |
264 | int n; | |
cb65a5ba | 265 | int w; |
a257cdd0 | 266 | |
aefa89d1 P |
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 | */ | |
2b0143b5 | 272 | if (dentry == NULL || d_really_is_negative(dentry)) |
a257cdd0 | 273 | return 0; |
2b0143b5 | 274 | inode = d_inode(dentry); |
a257cdd0 | 275 | |
4f4a4fad | 276 | p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
a257cdd0 AG |
277 | *p++ = htonl(resp->mask); |
278 | if (!xdr_ressize_check(rqstp, p)) | |
279 | return 0; | |
280 | base = (char *)p - (char *)head->iov_base; | |
281 | ||
cb65a5ba JJ |
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); | |
a257cdd0 | 285 | while (w > 0) { |
afc59400 | 286 | if (!*(rqstp->rq_next_page++)) |
a257cdd0 AG |
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); | |
7b8f4586 | 299 | return (n > 0); |
a257cdd0 AG |
300 | } |
301 | ||
131a21c2 | 302 | static int nfsaclsvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p, |
a257cdd0 AG |
303 | struct nfsd_attrstat *resp) |
304 | { | |
4f4a4fad | 305 | p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
a257cdd0 AG |
306 | return xdr_ressize_check(rqstp, p); |
307 | } | |
308 | ||
309 | /* ACCESS */ | |
131a21c2 | 310 | static int nfsaclsvc_encode_accessres(struct svc_rqst *rqstp, __be32 *p, |
a257cdd0 AG |
311 | struct nfsd3_accessres *resp) |
312 | { | |
4f4a4fad | 313 | p = nfs2svc_encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
a257cdd0 AG |
314 | *p++ = htonl(resp->access); |
315 | return xdr_ressize_check(rqstp, p); | |
316 | } | |
317 | ||
318 | /* | |
319 | * XDR release functions | |
320 | */ | |
8537488b | 321 | static void nfsaclsvc_release_getacl(struct svc_rqst *rqstp) |
a257cdd0 | 322 | { |
8537488b CH |
323 | struct nfsd3_getaclres *resp = rqstp->rq_resp; |
324 | ||
a257cdd0 AG |
325 | fh_put(&resp->fh); |
326 | posix_acl_release(resp->acl_access); | |
327 | posix_acl_release(resp->acl_default); | |
a257cdd0 AG |
328 | } |
329 | ||
8537488b | 330 | static void nfsaclsvc_release_attrstat(struct svc_rqst *rqstp) |
a257cdd0 | 331 | { |
8537488b CH |
332 | struct nfsd_attrstat *resp = rqstp->rq_resp; |
333 | ||
a257cdd0 | 334 | fh_put(&resp->fh); |
a257cdd0 AG |
335 | } |
336 | ||
8537488b | 337 | static void nfsaclsvc_release_access(struct svc_rqst *rqstp) |
c9ce2283 | 338 | { |
8537488b CH |
339 | struct nfsd3_accessres *resp = rqstp->rq_resp; |
340 | ||
341 | fh_put(&resp->fh); | |
c9ce2283 GB |
342 | } |
343 | ||
a257cdd0 | 344 | #define nfsaclsvc_decode_voidargs NULL |
a257cdd0 AG |
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 | ||
f7235b6b CH |
351 | #define PROC(name, argt, rest, relt, cache, respsize) \ |
352 | { \ | |
a6beb732 | 353 | .pc_func = nfsacld_proc_##name, \ |
f7235b6b CH |
354 | .pc_decode = (kxdrproc_t) nfsaclsvc_decode_##argt##args, \ |
355 | .pc_encode = (kxdrproc_t) nfsaclsvc_encode_##rest##res, \ | |
8537488b | 356 | .pc_release = nfsaclsvc_release_##relt, \ |
f7235b6b CH |
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 | } | |
a257cdd0 AG |
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)), | |
c9ce2283 GB |
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), | |
a257cdd0 AG |
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 | }; |