]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * XDR support for nfsd |
4 | * | |
5 | * Copyright (C) 1995, 1996 Olaf Kirch <[email protected]> | |
6 | */ | |
7 | ||
3dadecce | 8 | #include "vfs.h" |
9a74af21 | 9 | #include "xdr.h" |
2e8138a2 | 10 | #include "auth.h" |
1da177e4 LT |
11 | |
12 | #define NFSDDBG_FACILITY NFSDDBG_XDR | |
13 | ||
1da177e4 LT |
14 | /* |
15 | * Mapping of S_IF* types to NFS file types | |
16 | */ | |
17 | static u32 nfs_ftypes[] = { | |
18 | NFNON, NFCHR, NFCHR, NFBAD, | |
19 | NFDIR, NFBAD, NFBLK, NFBAD, | |
20 | NFREG, NFBAD, NFLNK, NFBAD, | |
21 | NFSOCK, NFBAD, NFLNK, NFBAD, | |
22 | }; | |
23 | ||
24 | ||
25 | /* | |
ebcd8e8b | 26 | * Basic NFSv2 data types (RFC 1094 Section 2.3) |
1da177e4 | 27 | */ |
ebcd8e8b | 28 | |
131a21c2 AV |
29 | static __be32 * |
30 | decode_fh(__be32 *p, struct svc_fh *fhp) | |
1da177e4 LT |
31 | { |
32 | fh_init(fhp, NFS_FHSIZE); | |
33 | memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE); | |
34 | fhp->fh_handle.fh_size = NFS_FHSIZE; | |
35 | ||
36 | /* FIXME: Look up export pointer here and verify | |
37 | * Sun Secure RPC if requested */ | |
38 | return p + (NFS_FHSIZE >> 2); | |
39 | } | |
40 | ||
ebcd8e8b CL |
41 | static bool |
42 | svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp) | |
43 | { | |
44 | __be32 *p; | |
45 | ||
46 | p = xdr_inline_decode(xdr, NFS_FHSIZE); | |
47 | if (!p) | |
48 | return false; | |
49 | fh_init(fhp, NFS_FHSIZE); | |
50 | memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE); | |
51 | fhp->fh_handle.fh_size = NFS_FHSIZE; | |
52 | ||
53 | return true; | |
54 | } | |
55 | ||
a257cdd0 | 56 | /* Helper function for NFSv2 ACL code */ |
131a21c2 | 57 | __be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp) |
a257cdd0 AG |
58 | { |
59 | return decode_fh(p, fhp); | |
60 | } | |
61 | ||
3ee6f61c | 62 | static __be32 * |
131a21c2 | 63 | encode_fh(__be32 *p, struct svc_fh *fhp) |
1da177e4 LT |
64 | { |
65 | memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE); | |
66 | return p + (NFS_FHSIZE>> 2); | |
67 | } | |
68 | ||
69 | /* | |
70 | * Decode a file name and make sure that the path contains | |
71 | * no slashes or null bytes. | |
72 | */ | |
3ee6f61c | 73 | static __be32 * |
ee1a95b3 | 74 | decode_filename(__be32 *p, char **namp, unsigned int *lenp) |
1da177e4 LT |
75 | { |
76 | char *name; | |
ee1a95b3 | 77 | unsigned int i; |
1da177e4 LT |
78 | |
79 | if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) { | |
80 | for (i = 0, name = *namp; i < *lenp; i++, name++) { | |
81 | if (*name == '\0' || *name == '/') | |
82 | return NULL; | |
83 | } | |
84 | } | |
85 | ||
86 | return p; | |
87 | } | |
88 | ||
3ee6f61c | 89 | static __be32 * |
e45d1a18 | 90 | decode_sattr(__be32 *p, struct iattr *iap, struct user_namespace *userns) |
1da177e4 LT |
91 | { |
92 | u32 tmp, tmp1; | |
93 | ||
94 | iap->ia_valid = 0; | |
95 | ||
96 | /* Sun client bug compatibility check: some sun clients seem to | |
97 | * put 0xffff in the mode field when they mean 0xffffffff. | |
98 | * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah. | |
99 | */ | |
100 | if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) { | |
101 | iap->ia_valid |= ATTR_MODE; | |
102 | iap->ia_mode = tmp; | |
103 | } | |
104 | if ((tmp = ntohl(*p++)) != (u32)-1) { | |
e45d1a18 | 105 | iap->ia_uid = make_kuid(userns, tmp); |
7c19723e EB |
106 | if (uid_valid(iap->ia_uid)) |
107 | iap->ia_valid |= ATTR_UID; | |
1da177e4 LT |
108 | } |
109 | if ((tmp = ntohl(*p++)) != (u32)-1) { | |
e45d1a18 | 110 | iap->ia_gid = make_kgid(userns, tmp); |
7c19723e EB |
111 | if (gid_valid(iap->ia_gid)) |
112 | iap->ia_valid |= ATTR_GID; | |
1da177e4 LT |
113 | } |
114 | if ((tmp = ntohl(*p++)) != (u32)-1) { | |
115 | iap->ia_valid |= ATTR_SIZE; | |
116 | iap->ia_size = tmp; | |
117 | } | |
118 | tmp = ntohl(*p++); tmp1 = ntohl(*p++); | |
119 | if (tmp != (u32)-1 && tmp1 != (u32)-1) { | |
120 | iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET; | |
121 | iap->ia_atime.tv_sec = tmp; | |
122 | iap->ia_atime.tv_nsec = tmp1 * 1000; | |
123 | } | |
124 | tmp = ntohl(*p++); tmp1 = ntohl(*p++); | |
125 | if (tmp != (u32)-1 && tmp1 != (u32)-1) { | |
126 | iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET; | |
127 | iap->ia_mtime.tv_sec = tmp; | |
128 | iap->ia_mtime.tv_nsec = tmp1 * 1000; | |
129 | /* | |
130 | * Passing the invalid value useconds=1000000 for mtime | |
131 | * is a Sun convention for "set both mtime and atime to | |
132 | * current server time". It's needed to make permissions | |
133 | * checks for the "touch" program across v2 mounts to | |
134 | * Solaris and Irix boxes work correctly. See description of | |
135 | * sattr in section 6.1 of "NFS Illustrated" by | |
136 | * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5 | |
137 | */ | |
138 | if (tmp1 == 1000000) | |
139 | iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET); | |
140 | } | |
141 | return p; | |
142 | } | |
143 | ||
131a21c2 AV |
144 | static __be32 * |
145 | encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, | |
a334de28 | 146 | struct kstat *stat) |
1da177e4 | 147 | { |
e45d1a18 | 148 | struct user_namespace *userns = nfsd_user_namespace(rqstp); |
1da177e4 | 149 | struct dentry *dentry = fhp->fh_dentry; |
1da177e4 | 150 | int type; |
95582b00 | 151 | struct timespec64 time; |
af6a4e28 | 152 | u32 f; |
1da177e4 | 153 | |
a334de28 | 154 | type = (stat->mode & S_IFMT); |
1da177e4 LT |
155 | |
156 | *p++ = htonl(nfs_ftypes[type >> 12]); | |
082f31a2 | 157 | *p++ = htonl((u32) stat->mode); |
a334de28 | 158 | *p++ = htonl((u32) stat->nlink); |
e45d1a18 TM |
159 | *p++ = htonl((u32) from_kuid_munged(userns, stat->uid)); |
160 | *p++ = htonl((u32) from_kgid_munged(userns, stat->gid)); | |
1da177e4 | 161 | |
a334de28 | 162 | if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) { |
1da177e4 LT |
163 | *p++ = htonl(NFS_MAXPATHLEN); |
164 | } else { | |
a334de28 | 165 | *p++ = htonl((u32) stat->size); |
1da177e4 | 166 | } |
a334de28 | 167 | *p++ = htonl((u32) stat->blksize); |
1da177e4 | 168 | if (S_ISCHR(type) || S_ISBLK(type)) |
a334de28 | 169 | *p++ = htonl(new_encode_dev(stat->rdev)); |
1da177e4 LT |
170 | else |
171 | *p++ = htonl(0xffffffff); | |
a334de28 | 172 | *p++ = htonl((u32) stat->blocks); |
af6a4e28 N |
173 | switch (fsid_source(fhp)) { |
174 | default: | |
175 | case FSIDSOURCE_DEV: | |
a334de28 | 176 | *p++ = htonl(new_encode_dev(stat->dev)); |
af6a4e28 N |
177 | break; |
178 | case FSIDSOURCE_FSID: | |
179 | *p++ = htonl((u32) fhp->fh_export->ex_fsid); | |
180 | break; | |
181 | case FSIDSOURCE_UUID: | |
182 | f = ((u32*)fhp->fh_export->ex_uuid)[0]; | |
183 | f ^= ((u32*)fhp->fh_export->ex_uuid)[1]; | |
184 | f ^= ((u32*)fhp->fh_export->ex_uuid)[2]; | |
185 | f ^= ((u32*)fhp->fh_export->ex_uuid)[3]; | |
186 | *p++ = htonl(f); | |
187 | break; | |
188 | } | |
a334de28 DS |
189 | *p++ = htonl((u32) stat->ino); |
190 | *p++ = htonl((u32) stat->atime.tv_sec); | |
191 | *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0); | |
76c47948 | 192 | time = stat->mtime; |
2b0143b5 | 193 | lease_get_mtime(d_inode(dentry), &time); |
1da177e4 LT |
194 | *p++ = htonl((u32) time.tv_sec); |
195 | *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0); | |
a334de28 DS |
196 | *p++ = htonl((u32) stat->ctime.tv_sec); |
197 | *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0); | |
1da177e4 LT |
198 | |
199 | return p; | |
200 | } | |
201 | ||
a257cdd0 | 202 | /* Helper function for NFSv2 ACL code */ |
4f4a4fad | 203 | __be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, struct kstat *stat) |
a257cdd0 | 204 | { |
4f4a4fad | 205 | return encode_fattr(rqstp, p, fhp, stat); |
a257cdd0 | 206 | } |
1da177e4 LT |
207 | |
208 | /* | |
209 | * XDR decode functions | |
210 | */ | |
1da177e4 LT |
211 | |
212 | int | |
ebcd8e8b | 213 | nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 214 | { |
ebcd8e8b | 215 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
026fec7e CH |
216 | struct nfsd_fhandle *args = rqstp->rq_argp; |
217 | ||
ebcd8e8b | 218 | return svcxdr_decode_fhandle(xdr, &args->fh); |
1da177e4 LT |
219 | } |
220 | ||
221 | int | |
026fec7e | 222 | nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 223 | { |
026fec7e CH |
224 | struct nfsd_sattrargs *args = rqstp->rq_argp; |
225 | ||
072f62ed N |
226 | p = decode_fh(p, &args->fh); |
227 | if (!p) | |
1da177e4 | 228 | return 0; |
e45d1a18 | 229 | p = decode_sattr(p, &args->attrs, nfsd_user_namespace(rqstp)); |
1da177e4 LT |
230 | |
231 | return xdr_argsize_check(rqstp, p); | |
232 | } | |
233 | ||
234 | int | |
026fec7e | 235 | nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 236 | { |
026fec7e CH |
237 | struct nfsd_diropargs *args = rqstp->rq_argp; |
238 | ||
1da177e4 LT |
239 | if (!(p = decode_fh(p, &args->fh)) |
240 | || !(p = decode_filename(p, &args->name, &args->len))) | |
241 | return 0; | |
242 | ||
d28c442f | 243 | return xdr_argsize_check(rqstp, p); |
1da177e4 LT |
244 | } |
245 | ||
246 | int | |
026fec7e | 247 | nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 248 | { |
8c293ef9 | 249 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
026fec7e | 250 | struct nfsd_readargs *args = rqstp->rq_argp; |
8c293ef9 | 251 | u32 totalcount; |
1da177e4 | 252 | |
8c293ef9 CL |
253 | if (!svcxdr_decode_fhandle(xdr, &args->fh)) |
254 | return 0; | |
255 | if (xdr_stream_decode_u32(xdr, &args->offset) < 0) | |
256 | return 0; | |
257 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) | |
258 | return 0; | |
259 | /* totalcount is ignored */ | |
260 | if (xdr_stream_decode_u32(xdr, &totalcount) < 0) | |
261 | return 0; | |
1da177e4 | 262 | |
8c293ef9 | 263 | return 1; |
1da177e4 LT |
264 | } |
265 | ||
266 | int | |
026fec7e | 267 | nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 268 | { |
a51b5b73 | 269 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
026fec7e | 270 | struct nfsd_writeargs *args = rqstp->rq_argp; |
db44bac4 | 271 | struct kvec *head = rqstp->rq_arg.head; |
a51b5b73 CL |
272 | struct kvec *tail = rqstp->rq_arg.tail; |
273 | u32 beginoffset, totalcount; | |
274 | size_t remaining; | |
f34b9568 | 275 | |
a51b5b73 | 276 | if (!svcxdr_decode_fhandle(xdr, &args->fh)) |
1da177e4 | 277 | return 0; |
a51b5b73 CL |
278 | /* beginoffset is ignored */ |
279 | if (xdr_stream_decode_u32(xdr, &beginoffset) < 0) | |
280 | return 0; | |
281 | if (xdr_stream_decode_u32(xdr, &args->offset) < 0) | |
282 | return 0; | |
283 | /* totalcount is ignored */ | |
284 | if (xdr_stream_decode_u32(xdr, &totalcount) < 0) | |
f34b9568 PS |
285 | return 0; |
286 | ||
a51b5b73 CL |
287 | /* opaque data */ |
288 | if (xdr_stream_decode_u32(xdr, &args->len) < 0) | |
13bf9fbf | 289 | return 0; |
a51b5b73 CL |
290 | if (args->len > NFSSVC_MAXBLKSIZE_V2) |
291 | return 0; | |
292 | remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len; | |
293 | remaining -= xdr_stream_pos(xdr); | |
294 | if (remaining < xdr_align_size(args->len)) | |
f34b9568 | 295 | return 0; |
a51b5b73 CL |
296 | args->first.iov_base = xdr->p; |
297 | args->first.iov_len = head->iov_len - xdr_stream_pos(xdr); | |
f34b9568 | 298 | |
f34b9568 | 299 | return 1; |
1da177e4 LT |
300 | } |
301 | ||
302 | int | |
026fec7e | 303 | nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 304 | { |
026fec7e CH |
305 | struct nfsd_createargs *args = rqstp->rq_argp; |
306 | ||
072f62ed N |
307 | if ( !(p = decode_fh(p, &args->fh)) |
308 | || !(p = decode_filename(p, &args->name, &args->len))) | |
1da177e4 | 309 | return 0; |
e45d1a18 | 310 | p = decode_sattr(p, &args->attrs, nfsd_user_namespace(rqstp)); |
1da177e4 LT |
311 | |
312 | return xdr_argsize_check(rqstp, p); | |
313 | } | |
314 | ||
315 | int | |
026fec7e | 316 | nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 317 | { |
026fec7e CH |
318 | struct nfsd_renameargs *args = rqstp->rq_argp; |
319 | ||
1da177e4 LT |
320 | if (!(p = decode_fh(p, &args->ffh)) |
321 | || !(p = decode_filename(p, &args->fname, &args->flen)) | |
322 | || !(p = decode_fh(p, &args->tfh)) | |
323 | || !(p = decode_filename(p, &args->tname, &args->tlen))) | |
324 | return 0; | |
325 | ||
9512a16b | 326 | return xdr_argsize_check(rqstp, p); |
1da177e4 LT |
327 | } |
328 | ||
329 | int | |
026fec7e | 330 | nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 331 | { |
026fec7e CH |
332 | struct nfsd_linkargs *args = rqstp->rq_argp; |
333 | ||
1da177e4 LT |
334 | if (!(p = decode_fh(p, &args->ffh)) |
335 | || !(p = decode_fh(p, &args->tfh)) | |
336 | || !(p = decode_filename(p, &args->tname, &args->tlen))) | |
337 | return 0; | |
338 | ||
339 | return xdr_argsize_check(rqstp, p); | |
340 | } | |
341 | ||
342 | int | |
026fec7e | 343 | nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 344 | { |
026fec7e | 345 | struct nfsd_symlinkargs *args = rqstp->rq_argp; |
38a70315 CL |
346 | char *base = (char *)p; |
347 | size_t xdrlen; | |
026fec7e | 348 | |
072f62ed | 349 | if ( !(p = decode_fh(p, &args->ffh)) |
38a70315 | 350 | || !(p = decode_filename(p, &args->fname, &args->flen))) |
1da177e4 LT |
351 | return 0; |
352 | ||
38a70315 CL |
353 | args->tlen = ntohl(*p++); |
354 | if (args->tlen == 0) | |
355 | return 0; | |
356 | ||
357 | args->first.iov_base = p; | |
358 | args->first.iov_len = rqstp->rq_arg.head[0].iov_len; | |
359 | args->first.iov_len -= (char *)p - base; | |
360 | ||
361 | /* This request is never larger than a page. Therefore, | |
362 | * transport will deliver either: | |
363 | * 1. pathname in the pagelist -> sattr is in the tail. | |
364 | * 2. everything in the head buffer -> sattr is in the head. | |
365 | */ | |
366 | if (rqstp->rq_arg.page_len) { | |
367 | if (args->tlen != rqstp->rq_arg.page_len) | |
368 | return 0; | |
369 | p = rqstp->rq_arg.tail[0].iov_base; | |
370 | } else { | |
371 | xdrlen = XDR_QUADLEN(args->tlen); | |
372 | if (xdrlen > args->first.iov_len - (8 * sizeof(__be32))) | |
373 | return 0; | |
374 | p += xdrlen; | |
375 | } | |
e45d1a18 | 376 | decode_sattr(p, &args->attrs, nfsd_user_namespace(rqstp)); |
38a70315 CL |
377 | |
378 | return 1; | |
1da177e4 LT |
379 | } |
380 | ||
381 | int | |
026fec7e | 382 | nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 383 | { |
8688361a | 384 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
026fec7e CH |
385 | struct nfsd_readdirargs *args = rqstp->rq_argp; |
386 | ||
8688361a CL |
387 | if (!svcxdr_decode_fhandle(xdr, &args->fh)) |
388 | return 0; | |
389 | if (xdr_stream_decode_u32(xdr, &args->cookie) < 0) | |
390 | return 0; | |
391 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) | |
1da177e4 | 392 | return 0; |
1da177e4 | 393 | |
8688361a | 394 | return 1; |
1da177e4 LT |
395 | } |
396 | ||
397 | /* | |
398 | * XDR encode functions | |
399 | */ | |
1da177e4 | 400 | |
cc028a10 CL |
401 | int |
402 | nfssvc_encode_stat(struct svc_rqst *rqstp, __be32 *p) | |
403 | { | |
404 | struct nfsd_stat *resp = rqstp->rq_resp; | |
405 | ||
406 | *p++ = resp->status; | |
407 | return xdr_ressize_check(rqstp, p); | |
408 | } | |
409 | ||
1da177e4 | 410 | int |
63f8de37 | 411 | nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 412 | { |
63f8de37 CH |
413 | struct nfsd_attrstat *resp = rqstp->rq_resp; |
414 | ||
cc028a10 | 415 | *p++ = resp->status; |
f0af2210 | 416 | if (resp->status != nfs_ok) |
cc028a10 | 417 | goto out; |
a334de28 | 418 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
cc028a10 | 419 | out: |
1da177e4 LT |
420 | return xdr_ressize_check(rqstp, p); |
421 | } | |
422 | ||
423 | int | |
63f8de37 | 424 | nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 425 | { |
63f8de37 CH |
426 | struct nfsd_diropres *resp = rqstp->rq_resp; |
427 | ||
cc028a10 | 428 | *p++ = resp->status; |
f0af2210 | 429 | if (resp->status != nfs_ok) |
cc028a10 | 430 | goto out; |
1da177e4 | 431 | p = encode_fh(p, &resp->fh); |
a334de28 | 432 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
cc028a10 | 433 | out: |
1da177e4 LT |
434 | return xdr_ressize_check(rqstp, p); |
435 | } | |
436 | ||
437 | int | |
63f8de37 | 438 | nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 439 | { |
63f8de37 | 440 | struct nfsd_readlinkres *resp = rqstp->rq_resp; |
76e5492b | 441 | struct kvec *head = rqstp->rq_res.head; |
63f8de37 | 442 | |
cc028a10 | 443 | *p++ = resp->status; |
f0af2210 CL |
444 | if (resp->status != nfs_ok) |
445 | return xdr_ressize_check(rqstp, p); | |
446 | ||
1da177e4 LT |
447 | *p++ = htonl(resp->len); |
448 | xdr_ressize_check(rqstp, p); | |
449 | rqstp->rq_res.page_len = resp->len; | |
450 | if (resp->len & 3) { | |
451 | /* need to pad the tail */ | |
1da177e4 LT |
452 | rqstp->rq_res.tail[0].iov_base = p; |
453 | *p = 0; | |
454 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3); | |
455 | } | |
76e5492b CL |
456 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->len)) |
457 | return 0; | |
1da177e4 LT |
458 | return 1; |
459 | } | |
460 | ||
461 | int | |
63f8de37 | 462 | nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 463 | { |
63f8de37 | 464 | struct nfsd_readres *resp = rqstp->rq_resp; |
76e5492b | 465 | struct kvec *head = rqstp->rq_res.head; |
63f8de37 | 466 | |
cc028a10 | 467 | *p++ = resp->status; |
f0af2210 CL |
468 | if (resp->status != nfs_ok) |
469 | return xdr_ressize_check(rqstp, p); | |
470 | ||
a334de28 | 471 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
1da177e4 LT |
472 | *p++ = htonl(resp->count); |
473 | xdr_ressize_check(rqstp, p); | |
474 | ||
25985edc | 475 | /* now update rqstp->rq_res to reflect data as well */ |
1da177e4 LT |
476 | rqstp->rq_res.page_len = resp->count; |
477 | if (resp->count & 3) { | |
478 | /* need to pad the tail */ | |
1da177e4 LT |
479 | rqstp->rq_res.tail[0].iov_base = p; |
480 | *p = 0; | |
481 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3); | |
482 | } | |
76e5492b CL |
483 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->count)) |
484 | return 0; | |
1da177e4 LT |
485 | return 1; |
486 | } | |
487 | ||
488 | int | |
63f8de37 | 489 | nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 490 | { |
63f8de37 CH |
491 | struct nfsd_readdirres *resp = rqstp->rq_resp; |
492 | ||
cc028a10 | 493 | *p++ = resp->status; |
f0af2210 CL |
494 | if (resp->status != nfs_ok) |
495 | return xdr_ressize_check(rqstp, p); | |
496 | ||
1da177e4 LT |
497 | xdr_ressize_check(rqstp, p); |
498 | p = resp->buffer; | |
499 | *p++ = 0; /* no more entries */ | |
500 | *p++ = htonl((resp->common.err == nfserr_eof)); | |
501 | rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1; | |
502 | ||
503 | return 1; | |
504 | } | |
505 | ||
506 | int | |
63f8de37 | 507 | nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 508 | { |
63f8de37 | 509 | struct nfsd_statfsres *resp = rqstp->rq_resp; |
1da177e4 LT |
510 | struct kstatfs *stat = &resp->stats; |
511 | ||
cc028a10 | 512 | *p++ = resp->status; |
f0af2210 CL |
513 | if (resp->status != nfs_ok) |
514 | return xdr_ressize_check(rqstp, p); | |
515 | ||
7adae489 | 516 | *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */ |
1da177e4 LT |
517 | *p++ = htonl(stat->f_bsize); |
518 | *p++ = htonl(stat->f_blocks); | |
519 | *p++ = htonl(stat->f_bfree); | |
520 | *p++ = htonl(stat->f_bavail); | |
521 | return xdr_ressize_check(rqstp, p); | |
522 | } | |
523 | ||
524 | int | |
a0ad13ef N |
525 | nfssvc_encode_entry(void *ccdv, const char *name, |
526 | int namlen, loff_t offset, u64 ino, unsigned int d_type) | |
1da177e4 | 527 | { |
a0ad13ef | 528 | struct readdir_cd *ccd = ccdv; |
1da177e4 | 529 | struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common); |
131a21c2 | 530 | __be32 *p = cd->buffer; |
1da177e4 LT |
531 | int buflen, slen; |
532 | ||
533 | /* | |
534 | dprintk("nfsd: entry(%.*s off %ld ino %ld)\n", | |
535 | namlen, name, offset, ino); | |
536 | */ | |
537 | ||
538 | if (offset > ~((u32) 0)) { | |
539 | cd->common.err = nfserr_fbig; | |
540 | return -EINVAL; | |
541 | } | |
542 | if (cd->offset) | |
543 | *cd->offset = htonl(offset); | |
1da177e4 | 544 | |
3c7aa15d KM |
545 | /* truncate filename */ |
546 | namlen = min(namlen, NFS2_MAXNAMLEN); | |
1da177e4 | 547 | slen = XDR_QUADLEN(namlen); |
3c7aa15d | 548 | |
1da177e4 LT |
549 | if ((buflen = cd->buflen - slen - 4) < 0) { |
550 | cd->common.err = nfserr_toosmall; | |
551 | return -EINVAL; | |
552 | } | |
40ee5dc6 PS |
553 | if (ino > ~((u32) 0)) { |
554 | cd->common.err = nfserr_fbig; | |
555 | return -EINVAL; | |
556 | } | |
1da177e4 LT |
557 | *p++ = xdr_one; /* mark entry present */ |
558 | *p++ = htonl((u32) ino); /* file id */ | |
559 | p = xdr_encode_array(p, name, namlen);/* name length & name */ | |
560 | cd->offset = p; /* remember pointer */ | |
131a21c2 | 561 | *p++ = htonl(~0U); /* offset of next entry */ |
1da177e4 LT |
562 | |
563 | cd->buflen = buflen; | |
564 | cd->buffer = p; | |
565 | cd->common.err = nfs_ok; | |
566 | return 0; | |
567 | } | |
568 | ||
569 | /* | |
570 | * XDR release functions | |
571 | */ | |
1841b9b6 | 572 | void nfssvc_release_attrstat(struct svc_rqst *rqstp) |
1da177e4 | 573 | { |
1841b9b6 CL |
574 | struct nfsd_attrstat *resp = rqstp->rq_resp; |
575 | ||
576 | fh_put(&resp->fh); | |
577 | } | |
578 | ||
579 | void nfssvc_release_diropres(struct svc_rqst *rqstp) | |
580 | { | |
581 | struct nfsd_diropres *resp = rqstp->rq_resp; | |
582 | ||
583 | fh_put(&resp->fh); | |
584 | } | |
585 | ||
586 | void nfssvc_release_readres(struct svc_rqst *rqstp) | |
587 | { | |
588 | struct nfsd_readres *resp = rqstp->rq_resp; | |
8537488b | 589 | |
1da177e4 | 590 | fh_put(&resp->fh); |
1da177e4 | 591 | } |