]>
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 <okir@monad.swb.de> | |
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 | { |
026fec7e CH |
384 | struct nfsd_readdirargs *args = rqstp->rq_argp; |
385 | ||
d40aa337 BT |
386 | p = decode_fh(p, &args->fh); |
387 | if (!p) | |
1da177e4 LT |
388 | return 0; |
389 | args->cookie = ntohl(*p++); | |
390 | args->count = ntohl(*p++); | |
1da177e4 | 391 | |
9512a16b | 392 | return xdr_argsize_check(rqstp, p); |
1da177e4 LT |
393 | } |
394 | ||
395 | /* | |
396 | * XDR encode functions | |
397 | */ | |
1da177e4 | 398 | |
cc028a10 CL |
399 | int |
400 | nfssvc_encode_stat(struct svc_rqst *rqstp, __be32 *p) | |
401 | { | |
402 | struct nfsd_stat *resp = rqstp->rq_resp; | |
403 | ||
404 | *p++ = resp->status; | |
405 | return xdr_ressize_check(rqstp, p); | |
406 | } | |
407 | ||
1da177e4 | 408 | int |
63f8de37 | 409 | nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 410 | { |
63f8de37 CH |
411 | struct nfsd_attrstat *resp = rqstp->rq_resp; |
412 | ||
cc028a10 | 413 | *p++ = resp->status; |
f0af2210 | 414 | if (resp->status != nfs_ok) |
cc028a10 | 415 | goto out; |
a334de28 | 416 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
cc028a10 | 417 | out: |
1da177e4 LT |
418 | return xdr_ressize_check(rqstp, p); |
419 | } | |
420 | ||
421 | int | |
63f8de37 | 422 | nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 423 | { |
63f8de37 CH |
424 | struct nfsd_diropres *resp = rqstp->rq_resp; |
425 | ||
cc028a10 | 426 | *p++ = resp->status; |
f0af2210 | 427 | if (resp->status != nfs_ok) |
cc028a10 | 428 | goto out; |
1da177e4 | 429 | p = encode_fh(p, &resp->fh); |
a334de28 | 430 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
cc028a10 | 431 | out: |
1da177e4 LT |
432 | return xdr_ressize_check(rqstp, p); |
433 | } | |
434 | ||
435 | int | |
63f8de37 | 436 | nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 437 | { |
63f8de37 | 438 | struct nfsd_readlinkres *resp = rqstp->rq_resp; |
76e5492b | 439 | struct kvec *head = rqstp->rq_res.head; |
63f8de37 | 440 | |
cc028a10 | 441 | *p++ = resp->status; |
f0af2210 CL |
442 | if (resp->status != nfs_ok) |
443 | return xdr_ressize_check(rqstp, p); | |
444 | ||
1da177e4 LT |
445 | *p++ = htonl(resp->len); |
446 | xdr_ressize_check(rqstp, p); | |
447 | rqstp->rq_res.page_len = resp->len; | |
448 | if (resp->len & 3) { | |
449 | /* need to pad the tail */ | |
1da177e4 LT |
450 | rqstp->rq_res.tail[0].iov_base = p; |
451 | *p = 0; | |
452 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3); | |
453 | } | |
76e5492b CL |
454 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->len)) |
455 | return 0; | |
1da177e4 LT |
456 | return 1; |
457 | } | |
458 | ||
459 | int | |
63f8de37 | 460 | nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 461 | { |
63f8de37 | 462 | struct nfsd_readres *resp = rqstp->rq_resp; |
76e5492b | 463 | struct kvec *head = rqstp->rq_res.head; |
63f8de37 | 464 | |
cc028a10 | 465 | *p++ = resp->status; |
f0af2210 CL |
466 | if (resp->status != nfs_ok) |
467 | return xdr_ressize_check(rqstp, p); | |
468 | ||
a334de28 | 469 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
1da177e4 LT |
470 | *p++ = htonl(resp->count); |
471 | xdr_ressize_check(rqstp, p); | |
472 | ||
25985edc | 473 | /* now update rqstp->rq_res to reflect data as well */ |
1da177e4 LT |
474 | rqstp->rq_res.page_len = resp->count; |
475 | if (resp->count & 3) { | |
476 | /* need to pad the tail */ | |
1da177e4 LT |
477 | rqstp->rq_res.tail[0].iov_base = p; |
478 | *p = 0; | |
479 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3); | |
480 | } | |
76e5492b CL |
481 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->count)) |
482 | return 0; | |
1da177e4 LT |
483 | return 1; |
484 | } | |
485 | ||
486 | int | |
63f8de37 | 487 | nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 488 | { |
63f8de37 CH |
489 | struct nfsd_readdirres *resp = rqstp->rq_resp; |
490 | ||
cc028a10 | 491 | *p++ = resp->status; |
f0af2210 CL |
492 | if (resp->status != nfs_ok) |
493 | return xdr_ressize_check(rqstp, p); | |
494 | ||
1da177e4 LT |
495 | xdr_ressize_check(rqstp, p); |
496 | p = resp->buffer; | |
497 | *p++ = 0; /* no more entries */ | |
498 | *p++ = htonl((resp->common.err == nfserr_eof)); | |
499 | rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1; | |
500 | ||
501 | return 1; | |
502 | } | |
503 | ||
504 | int | |
63f8de37 | 505 | nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p) |
1da177e4 | 506 | { |
63f8de37 | 507 | struct nfsd_statfsres *resp = rqstp->rq_resp; |
1da177e4 LT |
508 | struct kstatfs *stat = &resp->stats; |
509 | ||
cc028a10 | 510 | *p++ = resp->status; |
f0af2210 CL |
511 | if (resp->status != nfs_ok) |
512 | return xdr_ressize_check(rqstp, p); | |
513 | ||
7adae489 | 514 | *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */ |
1da177e4 LT |
515 | *p++ = htonl(stat->f_bsize); |
516 | *p++ = htonl(stat->f_blocks); | |
517 | *p++ = htonl(stat->f_bfree); | |
518 | *p++ = htonl(stat->f_bavail); | |
519 | return xdr_ressize_check(rqstp, p); | |
520 | } | |
521 | ||
522 | int | |
a0ad13ef N |
523 | nfssvc_encode_entry(void *ccdv, const char *name, |
524 | int namlen, loff_t offset, u64 ino, unsigned int d_type) | |
1da177e4 | 525 | { |
a0ad13ef | 526 | struct readdir_cd *ccd = ccdv; |
1da177e4 | 527 | struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common); |
131a21c2 | 528 | __be32 *p = cd->buffer; |
1da177e4 LT |
529 | int buflen, slen; |
530 | ||
531 | /* | |
532 | dprintk("nfsd: entry(%.*s off %ld ino %ld)\n", | |
533 | namlen, name, offset, ino); | |
534 | */ | |
535 | ||
536 | if (offset > ~((u32) 0)) { | |
537 | cd->common.err = nfserr_fbig; | |
538 | return -EINVAL; | |
539 | } | |
540 | if (cd->offset) | |
541 | *cd->offset = htonl(offset); | |
1da177e4 | 542 | |
3c7aa15d KM |
543 | /* truncate filename */ |
544 | namlen = min(namlen, NFS2_MAXNAMLEN); | |
1da177e4 | 545 | slen = XDR_QUADLEN(namlen); |
3c7aa15d | 546 | |
1da177e4 LT |
547 | if ((buflen = cd->buflen - slen - 4) < 0) { |
548 | cd->common.err = nfserr_toosmall; | |
549 | return -EINVAL; | |
550 | } | |
40ee5dc6 PS |
551 | if (ino > ~((u32) 0)) { |
552 | cd->common.err = nfserr_fbig; | |
553 | return -EINVAL; | |
554 | } | |
1da177e4 LT |
555 | *p++ = xdr_one; /* mark entry present */ |
556 | *p++ = htonl((u32) ino); /* file id */ | |
557 | p = xdr_encode_array(p, name, namlen);/* name length & name */ | |
558 | cd->offset = p; /* remember pointer */ | |
131a21c2 | 559 | *p++ = htonl(~0U); /* offset of next entry */ |
1da177e4 LT |
560 | |
561 | cd->buflen = buflen; | |
562 | cd->buffer = p; | |
563 | cd->common.err = nfs_ok; | |
564 | return 0; | |
565 | } | |
566 | ||
567 | /* | |
568 | * XDR release functions | |
569 | */ | |
1841b9b6 | 570 | void nfssvc_release_attrstat(struct svc_rqst *rqstp) |
1da177e4 | 571 | { |
1841b9b6 CL |
572 | struct nfsd_attrstat *resp = rqstp->rq_resp; |
573 | ||
574 | fh_put(&resp->fh); | |
575 | } | |
576 | ||
577 | void nfssvc_release_diropres(struct svc_rqst *rqstp) | |
578 | { | |
579 | struct nfsd_diropres *resp = rqstp->rq_resp; | |
580 | ||
581 | fh_put(&resp->fh); | |
582 | } | |
583 | ||
584 | void nfssvc_release_readres(struct svc_rqst *rqstp) | |
585 | { | |
586 | struct nfsd_readres *resp = rqstp->rq_resp; | |
8537488b | 587 | |
1da177e4 | 588 | fh_put(&resp->fh); |
1da177e4 | 589 | } |