NFS: decode_dirent should use an xdr_stream
[deliverable/linux.git] / fs / nfs / nfs2xdr.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/nfs2xdr.c
3 *
4 * XDR functions to encode/decode NFS RPC arguments and results.
5 *
6 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
7 * Copyright (C) 1996 Olaf Kirch
8 * 04 Aug 1998 Ion Badulescu <ionut@cs.columbia.edu>
9 * FIFO's need special handling in NFSv2
10 */
11
12#include <linux/param.h>
13#include <linux/time.h>
14#include <linux/mm.h>
1da177e4
LT
15#include <linux/errno.h>
16#include <linux/string.h>
17#include <linux/in.h>
18#include <linux/pagemap.h>
19#include <linux/proc_fs.h>
20#include <linux/sunrpc/clnt.h>
21#include <linux/nfs.h>
22#include <linux/nfs2.h>
23#include <linux/nfs_fs.h>
816724e6 24#include "internal.h"
1da177e4
LT
25
26#define NFSDBG_FACILITY NFSDBG_XDR
1da177e4 27
1da177e4
LT
28/* Mapping from NFS error code to "errno" error code. */
29#define errno_NFSERR_IO EIO
30
31/*
32 * Declare the space requirements for NFS arguments and replies as
33 * number of 32bit-words
34 */
35#define NFS_fhandle_sz (8)
36#define NFS_sattr_sz (8)
37#define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
38#define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
39#define NFS_fattr_sz (17)
40#define NFS_info_sz (5)
41#define NFS_entry_sz (NFS_filename_sz+3)
42
43#define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
4fdc17b2 44#define NFS_removeargs_sz (NFS_fhandle_sz+NFS_filename_sz)
1da177e4
LT
45#define NFS_sattrargs_sz (NFS_fhandle_sz+NFS_sattr_sz)
46#define NFS_readlinkargs_sz (NFS_fhandle_sz)
47#define NFS_readargs_sz (NFS_fhandle_sz+3)
48#define NFS_writeargs_sz (NFS_fhandle_sz+4)
49#define NFS_createargs_sz (NFS_diropargs_sz+NFS_sattr_sz)
50#define NFS_renameargs_sz (NFS_diropargs_sz+NFS_diropargs_sz)
51#define NFS_linkargs_sz (NFS_fhandle_sz+NFS_diropargs_sz)
94a6d753 52#define NFS_symlinkargs_sz (NFS_diropargs_sz+1+NFS_sattr_sz)
1da177e4
LT
53#define NFS_readdirargs_sz (NFS_fhandle_sz+2)
54
55#define NFS_attrstat_sz (1+NFS_fattr_sz)
56#define NFS_diropres_sz (1+NFS_fhandle_sz+NFS_fattr_sz)
57#define NFS_readlinkres_sz (2)
58#define NFS_readres_sz (1+NFS_fattr_sz+1)
59#define NFS_writeres_sz (NFS_attrstat_sz)
60#define NFS_stat_sz (1)
61#define NFS_readdirres_sz (1)
62#define NFS_statfsres_sz (1+NFS_info_sz)
63
64/*
65 * Common NFS XDR functions as inlines
66 */
9d787a75 67static inline __be32 *
4fdc17b2 68xdr_encode_fhandle(__be32 *p, const struct nfs_fh *fhandle)
1da177e4
LT
69{
70 memcpy(p, fhandle->data, NFS2_FHSIZE);
71 return p + XDR_QUADLEN(NFS2_FHSIZE);
72}
73
9d787a75
AV
74static inline __be32 *
75xdr_decode_fhandle(__be32 *p, struct nfs_fh *fhandle)
1da177e4
LT
76{
77 /* NFSv2 handles have a fixed length */
78 fhandle->size = NFS2_FHSIZE;
79 memcpy(fhandle->data, p, NFS2_FHSIZE);
80 return p + XDR_QUADLEN(NFS2_FHSIZE);
81}
82
9d787a75
AV
83static inline __be32*
84xdr_encode_time(__be32 *p, struct timespec *timep)
1da177e4
LT
85{
86 *p++ = htonl(timep->tv_sec);
87 /* Convert nanoseconds into microseconds */
88 *p++ = htonl(timep->tv_nsec ? timep->tv_nsec / 1000 : 0);
89 return p;
90}
91
9d787a75
AV
92static inline __be32*
93xdr_encode_current_server_time(__be32 *p, struct timespec *timep)
1da177e4
LT
94{
95 /*
96 * Passing the invalid value useconds=1000000 is a
97 * Sun convention for "set to current server time".
98 * It's needed to make permissions checks for the
99 * "touch" program across v2 mounts to Solaris and
100 * Irix boxes work correctly. See description of
101 * sattr in section 6.1 of "NFS Illustrated" by
102 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
103 */
104 *p++ = htonl(timep->tv_sec);
105 *p++ = htonl(1000000);
106 return p;
107}
108
9d787a75
AV
109static inline __be32*
110xdr_decode_time(__be32 *p, struct timespec *timep)
1da177e4
LT
111{
112 timep->tv_sec = ntohl(*p++);
113 /* Convert microseconds into nanoseconds */
114 timep->tv_nsec = ntohl(*p++) * 1000;
115 return p;
116}
117
9d787a75
AV
118static __be32 *
119xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
1da177e4 120{
bca79478
TM
121 u32 rdev, type;
122 type = ntohl(*p++);
1da177e4
LT
123 fattr->mode = ntohl(*p++);
124 fattr->nlink = ntohl(*p++);
125 fattr->uid = ntohl(*p++);
126 fattr->gid = ntohl(*p++);
127 fattr->size = ntohl(*p++);
128 fattr->du.nfs2.blocksize = ntohl(*p++);
129 rdev = ntohl(*p++);
130 fattr->du.nfs2.blocks = ntohl(*p++);
8b4bdcf8
TM
131 fattr->fsid.major = ntohl(*p++);
132 fattr->fsid.minor = 0;
1da177e4
LT
133 fattr->fileid = ntohl(*p++);
134 p = xdr_decode_time(p, &fattr->atime);
135 p = xdr_decode_time(p, &fattr->mtime);
136 p = xdr_decode_time(p, &fattr->ctime);
9e6e70f8 137 fattr->valid |= NFS_ATTR_FATTR_V2;
1da177e4 138 fattr->rdev = new_decode_dev(rdev);
bca79478 139 if (type == NFCHR && rdev == NFS2_FIFO_DEV) {
1da177e4
LT
140 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
141 fattr->rdev = 0;
142 }
1da177e4
LT
143 return p;
144}
145
9d787a75
AV
146static inline __be32 *
147xdr_encode_sattr(__be32 *p, struct iattr *attr)
1da177e4 148{
9d787a75 149 const __be32 not_set = __constant_htonl(0xFFFFFFFF);
eadb8c14
TM
150
151 *p++ = (attr->ia_valid & ATTR_MODE) ? htonl(attr->ia_mode) : not_set;
152 *p++ = (attr->ia_valid & ATTR_UID) ? htonl(attr->ia_uid) : not_set;
153 *p++ = (attr->ia_valid & ATTR_GID) ? htonl(attr->ia_gid) : not_set;
154 *p++ = (attr->ia_valid & ATTR_SIZE) ? htonl(attr->ia_size) : not_set;
1da177e4
LT
155
156 if (attr->ia_valid & ATTR_ATIME_SET) {
157 p = xdr_encode_time(p, &attr->ia_atime);
158 } else if (attr->ia_valid & ATTR_ATIME) {
159 p = xdr_encode_current_server_time(p, &attr->ia_atime);
160 } else {
eadb8c14
TM
161 *p++ = not_set;
162 *p++ = not_set;
1da177e4
LT
163 }
164
165 if (attr->ia_valid & ATTR_MTIME_SET) {
166 p = xdr_encode_time(p, &attr->ia_mtime);
167 } else if (attr->ia_valid & ATTR_MTIME) {
168 p = xdr_encode_current_server_time(p, &attr->ia_mtime);
169 } else {
eadb8c14
TM
170 *p++ = not_set;
171 *p++ = not_set;
1da177e4
LT
172 }
173 return p;
174}
1da177e4
LT
175
176/*
177 * NFS encode functions
178 */
179/*
180 * Encode file handle argument
181 * GETATTR, READLINK, STATFS
182 */
183static int
9d787a75 184nfs_xdr_fhandle(struct rpc_rqst *req, __be32 *p, struct nfs_fh *fh)
1da177e4
LT
185{
186 p = xdr_encode_fhandle(p, fh);
187 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
188 return 0;
189}
190
191/*
192 * Encode SETATTR arguments
193 */
194static int
9d787a75 195nfs_xdr_sattrargs(struct rpc_rqst *req, __be32 *p, struct nfs_sattrargs *args)
1da177e4
LT
196{
197 p = xdr_encode_fhandle(p, args->fh);
198 p = xdr_encode_sattr(p, args->sattr);
199 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
200 return 0;
201}
202
203/*
204 * Encode directory ops argument
4fdc17b2 205 * LOOKUP, RMDIR
1da177e4
LT
206 */
207static int
9d787a75 208nfs_xdr_diropargs(struct rpc_rqst *req, __be32 *p, struct nfs_diropargs *args)
1da177e4
LT
209{
210 p = xdr_encode_fhandle(p, args->fh);
211 p = xdr_encode_array(p, args->name, args->len);
212 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
213 return 0;
214}
215
4fdc17b2
TM
216/*
217 * Encode REMOVE argument
218 */
219static int
220nfs_xdr_removeargs(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
221{
222 p = xdr_encode_fhandle(p, args->fh);
223 p = xdr_encode_array(p, args->name.name, args->name.len);
224 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
225 return 0;
226}
227
1da177e4
LT
228/*
229 * Arguments to a READ call. Since we read data directly into the page
230 * cache, we also set up the reply iovec here so that iov[1] points
231 * exactly to the page we want to fetch.
232 */
233static int
9d787a75 234nfs_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
1da177e4 235{
a17c2153 236 struct rpc_auth *auth = req->rq_cred->cr_auth;
1da177e4
LT
237 unsigned int replen;
238 u32 offset = (u32)args->offset;
239 u32 count = args->count;
240
241 p = xdr_encode_fhandle(p, args->fh);
242 *p++ = htonl(offset);
243 *p++ = htonl(count);
244 *p++ = htonl(count);
245 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
246
247 /* Inline the page array */
248 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readres_sz) << 2;
249 xdr_inline_pages(&req->rq_rcv_buf, replen,
250 args->pages, args->pgbase, count);
4f22ccc3 251 req->rq_rcv_buf.flags |= XDRBUF_READ;
1da177e4
LT
252 return 0;
253}
254
255/*
256 * Decode READ reply
257 */
258static int
9d787a75 259nfs_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
1da177e4
LT
260{
261 struct kvec *iov = req->rq_rcv_buf.head;
6232dbbc
CL
262 size_t hdrlen;
263 u32 count, recvd;
264 int status;
1da177e4
LT
265
266 if ((status = ntohl(*p++)))
856dff3d 267 return nfs_stat_to_errno(status);
1da177e4
LT
268 p = xdr_decode_fattr(p, res->fattr);
269
270 count = ntohl(*p++);
271 res->eof = 0;
272 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
273 if (iov->iov_len < hdrlen) {
fe82a183 274 dprintk("NFS: READ reply header overflowed:"
6232dbbc 275 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
1da177e4
LT
276 return -errno_NFSERR_IO;
277 } else if (iov->iov_len != hdrlen) {
278 dprintk("NFS: READ header is short. iovec will be shifted.\n");
279 xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
280 }
281
282 recvd = req->rq_rcv_buf.len - hdrlen;
283 if (count > recvd) {
fe82a183 284 dprintk("NFS: server cheating in read reply: "
6232dbbc 285 "count %u > recvd %u\n", count, recvd);
1da177e4
LT
286 count = recvd;
287 }
288
6232dbbc 289 dprintk("RPC: readres OK count %u\n", count);
1da177e4
LT
290 if (count < res->count)
291 res->count = count;
292
293 return count;
294}
295
296
297/*
298 * Write arguments. Splice the buffer to be written into the iovec.
299 */
300static int
9d787a75 301nfs_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
1da177e4
LT
302{
303 struct xdr_buf *sndbuf = &req->rq_snd_buf;
304 u32 offset = (u32)args->offset;
305 u32 count = args->count;
306
307 p = xdr_encode_fhandle(p, args->fh);
308 *p++ = htonl(offset);
309 *p++ = htonl(offset);
310 *p++ = htonl(count);
311 *p++ = htonl(count);
312 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
313
314 /* Copy the page array */
315 xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
4f22ccc3 316 sndbuf->flags |= XDRBUF_WRITE;
1da177e4
LT
317 return 0;
318}
319
320/*
321 * Encode create arguments
322 * CREATE, MKDIR
323 */
324static int
9d787a75 325nfs_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs_createargs *args)
1da177e4
LT
326{
327 p = xdr_encode_fhandle(p, args->fh);
328 p = xdr_encode_array(p, args->name, args->len);
329 p = xdr_encode_sattr(p, args->sattr);
330 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
331 return 0;
332}
333
334/*
335 * Encode RENAME arguments
336 */
337static int
9d787a75 338nfs_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs_renameargs *args)
1da177e4 339{
920769f0
JL
340 p = xdr_encode_fhandle(p, args->old_dir);
341 p = xdr_encode_array(p, args->old_name->name, args->old_name->len);
342 p = xdr_encode_fhandle(p, args->new_dir);
343 p = xdr_encode_array(p, args->new_name->name, args->new_name->len);
1da177e4
LT
344 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
345 return 0;
346}
347
348/*
349 * Encode LINK arguments
350 */
351static int
9d787a75 352nfs_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs_linkargs *args)
1da177e4
LT
353{
354 p = xdr_encode_fhandle(p, args->fromfh);
355 p = xdr_encode_fhandle(p, args->tofh);
356 p = xdr_encode_array(p, args->toname, args->tolen);
357 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
358 return 0;
359}
360
361/*
362 * Encode SYMLINK arguments
363 */
364static int
9d787a75 365nfs_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_symlinkargs *args)
1da177e4 366{
94a6d753
CL
367 struct xdr_buf *sndbuf = &req->rq_snd_buf;
368 size_t pad;
369
1da177e4
LT
370 p = xdr_encode_fhandle(p, args->fromfh);
371 p = xdr_encode_array(p, args->fromname, args->fromlen);
94a6d753
CL
372 *p++ = htonl(args->pathlen);
373 sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
374
375 xdr_encode_pages(sndbuf, args->pages, 0, args->pathlen);
376
377 /*
378 * xdr_encode_pages may have added a few bytes to ensure the
379 * pathname ends on a 4-byte boundary. Start encoding the
380 * attributes after the pad bytes.
381 */
382 pad = sndbuf->tail->iov_len;
383 if (pad > 0)
384 p++;
1da177e4 385 p = xdr_encode_sattr(p, args->sattr);
94a6d753 386 sndbuf->len += xdr_adjust_iovec(sndbuf->tail, p) - pad;
1da177e4
LT
387 return 0;
388}
389
390/*
391 * Encode arguments to readdir call
392 */
393static int
9d787a75 394nfs_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs_readdirargs *args)
1da177e4 395{
a17c2153 396 struct rpc_auth *auth = req->rq_cred->cr_auth;
1da177e4
LT
397 unsigned int replen;
398 u32 count = args->count;
399
400 p = xdr_encode_fhandle(p, args->fh);
401 *p++ = htonl(args->cookie);
402 *p++ = htonl(count); /* see above */
403 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
404
405 /* Inline the page array */
406 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
407 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
408 return 0;
409}
410
411/*
412 * Decode the result of a readdir call.
413 * We're not really decoding anymore, we just leave the buffer untouched
414 * and only check that it is syntactically correct.
415 * The real decoding happens in nfs_decode_entry below, called directly
416 * from nfs_readdir for each entry.
417 */
418static int
9d787a75 419nfs_xdr_readdirres(struct rpc_rqst *req, __be32 *p, void *dummy)
1da177e4
LT
420{
421 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
422 struct kvec *iov = rcvbuf->head;
423 struct page **page;
6232dbbc
CL
424 size_t hdrlen;
425 unsigned int pglen, recvd;
426 u32 len;
caa02bd5 427 int status, nr = 0;
9d787a75 428 __be32 *end, *entry, *kaddr;
1da177e4
LT
429
430 if ((status = ntohl(*p++)))
856dff3d 431 return nfs_stat_to_errno(status);
1da177e4
LT
432
433 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
434 if (iov->iov_len < hdrlen) {
fe82a183 435 dprintk("NFS: READDIR reply header overflowed:"
6232dbbc 436 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
1da177e4
LT
437 return -errno_NFSERR_IO;
438 } else if (iov->iov_len != hdrlen) {
439 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
440 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
441 }
442
443 pglen = rcvbuf->page_len;
444 recvd = rcvbuf->len - hdrlen;
445 if (pglen > recvd)
446 pglen = recvd;
447 page = rcvbuf->pages;
9d787a75
AV
448 kaddr = p = kmap_atomic(*page, KM_USER0);
449 end = (__be32 *)((char *)p + pglen);
1da177e4 450 entry = p;
caa02bd5
JL
451
452 /* Make sure the packet actually has a value_follows and EOF entry */
453 if ((entry + 1) > end)
454 goto short_pkt;
455
456 for (; *p++; nr++) {
1da177e4
LT
457 if (p + 2 > end)
458 goto short_pkt;
459 p++; /* fileid */
460 len = ntohl(*p++);
461 p += XDR_QUADLEN(len) + 1; /* name plus cookie */
462 if (len > NFS2_MAXNAMLEN) {
fe82a183 463 dprintk("NFS: giant filename in readdir (len 0x%x)!\n",
1da177e4
LT
464 len);
465 goto err_unmap;
466 }
467 if (p + 2 > end)
468 goto short_pkt;
469 entry = p;
470 }
caa02bd5
JL
471
472 /*
473 * Apparently some server sends responses that are a valid size, but
474 * contain no entries, and have value_follows==0 and EOF==0. For
475 * those, just set the EOF marker.
476 */
477 if (!nr && entry[1] == 0) {
478 dprintk("NFS: readdir reply truncated!\n");
479 entry[1] = 1;
480 }
1da177e4
LT
481 out:
482 kunmap_atomic(kaddr, KM_USER0);
483 return nr;
484 short_pkt:
caa02bd5
JL
485 /*
486 * When we get a short packet there are 2 possibilities. We can
487 * return an error, or fix up the response to look like a valid
488 * response and return what we have so far. If there are no
489 * entries and the packet was short, then return -EIO. If there
490 * are valid entries in the response, return them and pretend that
491 * the call was successful, but incomplete. The caller can retry the
492 * readdir starting at the last cookie.
493 */
1da177e4 494 entry[0] = entry[1] = 0;
caa02bd5
JL
495 if (!nr)
496 nr = -errno_NFSERR_IO;
1da177e4
LT
497 goto out;
498err_unmap:
499 nr = -errno_NFSERR_IO;
500 goto out;
501}
502
babddc72
BS
503static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
504{
505 dprintk("nfs: %s: prematurely hit end of receive buffer. "
506 "Remaining buffer length is %tu words.\n",
507 func, xdr->end - xdr->p);
508}
509
0dbb4c67 510__be32 *
babddc72 511nfs_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry, int plus)
1da177e4 512{
babddc72
BS
513 __be32 *p;
514 p = xdr_inline_decode(xdr, 4);
515 if (unlikely(!p))
516 goto out_overflow;
517 if (!ntohl(*p++)) {
518 p = xdr_inline_decode(xdr, 4);
519 if (unlikely(!p))
520 goto out_overflow;
521 if (!ntohl(*p++))
1da177e4
LT
522 return ERR_PTR(-EAGAIN);
523 entry->eof = 1;
524 return ERR_PTR(-EBADCOOKIE);
525 }
526
babddc72
BS
527 p = xdr_inline_decode(xdr, 8);
528 if (unlikely(!p))
529 goto out_overflow;
530
1da177e4
LT
531 entry->ino = ntohl(*p++);
532 entry->len = ntohl(*p++);
babddc72
BS
533
534 p = xdr_inline_decode(xdr, entry->len + 4);
535 if (unlikely(!p))
536 goto out_overflow;
1da177e4
LT
537 entry->name = (const char *) p;
538 p += XDR_QUADLEN(entry->len);
539 entry->prev_cookie = entry->cookie;
540 entry->cookie = ntohl(*p++);
babddc72
BS
541
542 p = xdr_inline_peek(xdr, 8);
543 if (p != NULL)
544 entry->eof = !p[0] && p[1];
545 else
546 entry->eof = 0;
1da177e4
LT
547
548 return p;
babddc72
BS
549
550out_overflow:
551 print_overflow_msg(__func__, xdr);
552 return ERR_PTR(-EIO);
1da177e4
LT
553}
554
555/*
556 * NFS XDR decode functions
557 */
558/*
559 * Decode simple status reply
560 */
561static int
9d787a75 562nfs_xdr_stat(struct rpc_rqst *req, __be32 *p, void *dummy)
1da177e4
LT
563{
564 int status;
565
566 if ((status = ntohl(*p++)) != 0)
856dff3d 567 status = nfs_stat_to_errno(status);
1da177e4
LT
568 return status;
569}
570
571/*
572 * Decode attrstat reply
573 * GETATTR, SETATTR, WRITE
574 */
575static int
9d787a75 576nfs_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
1da177e4
LT
577{
578 int status;
579
580 if ((status = ntohl(*p++)))
856dff3d 581 return nfs_stat_to_errno(status);
1da177e4
LT
582 xdr_decode_fattr(p, fattr);
583 return 0;
584}
585
586/*
587 * Decode diropres reply
588 * LOOKUP, CREATE, MKDIR
589 */
590static int
9d787a75 591nfs_xdr_diropres(struct rpc_rqst *req, __be32 *p, struct nfs_diropok *res)
1da177e4
LT
592{
593 int status;
594
595 if ((status = ntohl(*p++)))
856dff3d 596 return nfs_stat_to_errno(status);
1da177e4
LT
597 p = xdr_decode_fhandle(p, res->fh);
598 xdr_decode_fattr(p, res->fattr);
599 return 0;
600}
601
602/*
603 * Encode READLINK args
604 */
605static int
9d787a75 606nfs_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_readlinkargs *args)
1da177e4 607{
a17c2153 608 struct rpc_auth *auth = req->rq_cred->cr_auth;
1da177e4
LT
609 unsigned int replen;
610
611 p = xdr_encode_fhandle(p, args->fh);
612 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
613
614 /* Inline the page array */
615 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
616 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
617 return 0;
618}
619
620/*
621 * Decode READLINK reply
622 */
623static int
9d787a75 624nfs_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, void *dummy)
1da177e4
LT
625{
626 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
627 struct kvec *iov = rcvbuf->head;
6232dbbc
CL
628 size_t hdrlen;
629 u32 len, recvd;
1da177e4
LT
630 int status;
631
632 if ((status = ntohl(*p++)))
856dff3d 633 return nfs_stat_to_errno(status);
1da177e4
LT
634 /* Convert length of symlink */
635 len = ntohl(*p++);
6232dbbc 636 if (len >= rcvbuf->page_len) {
fe82a183 637 dprintk("nfs: server returned giant symlink!\n");
1da177e4
LT
638 return -ENAMETOOLONG;
639 }
640 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
641 if (iov->iov_len < hdrlen) {
fe82a183 642 dprintk("NFS: READLINK reply header overflowed:"
6232dbbc 643 "length %Zu > %Zu\n", hdrlen, iov->iov_len);
1da177e4
LT
644 return -errno_NFSERR_IO;
645 } else if (iov->iov_len != hdrlen) {
646 dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
647 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
648 }
649 recvd = req->rq_rcv_buf.len - hdrlen;
650 if (recvd < len) {
fe82a183 651 dprintk("NFS: server cheating in readlink reply: "
1da177e4
LT
652 "count %u > recvd %u\n", len, recvd);
653 return -EIO;
654 }
655
b4687da7 656 xdr_terminate_string(rcvbuf, len);
1da177e4
LT
657 return 0;
658}
659
660/*
661 * Decode WRITE reply
662 */
663static int
9d787a75 664nfs_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
1da177e4
LT
665{
666 res->verf->committed = NFS_FILE_SYNC;
667 return nfs_xdr_attrstat(req, p, res->fattr);
668}
669
670/*
671 * Decode STATFS reply
672 */
673static int
9d787a75 674nfs_xdr_statfsres(struct rpc_rqst *req, __be32 *p, struct nfs2_fsstat *res)
1da177e4
LT
675{
676 int status;
677
678 if ((status = ntohl(*p++)))
856dff3d 679 return nfs_stat_to_errno(status);
1da177e4
LT
680
681 res->tsize = ntohl(*p++);
682 res->bsize = ntohl(*p++);
683 res->blocks = ntohl(*p++);
684 res->bfree = ntohl(*p++);
685 res->bavail = ntohl(*p++);
686 return 0;
687}
688
689/*
690 * We need to translate between nfs status return values and
691 * the local errno values which may not be the same.
692 */
693static struct {
694 int stat;
695 int errno;
696} nfs_errtbl[] = {
697 { NFS_OK, 0 },
856dff3d
BH
698 { NFSERR_PERM, -EPERM },
699 { NFSERR_NOENT, -ENOENT },
700 { NFSERR_IO, -errno_NFSERR_IO},
701 { NFSERR_NXIO, -ENXIO },
702/* { NFSERR_EAGAIN, -EAGAIN }, */
703 { NFSERR_ACCES, -EACCES },
704 { NFSERR_EXIST, -EEXIST },
705 { NFSERR_XDEV, -EXDEV },
706 { NFSERR_NODEV, -ENODEV },
707 { NFSERR_NOTDIR, -ENOTDIR },
708 { NFSERR_ISDIR, -EISDIR },
709 { NFSERR_INVAL, -EINVAL },
710 { NFSERR_FBIG, -EFBIG },
711 { NFSERR_NOSPC, -ENOSPC },
712 { NFSERR_ROFS, -EROFS },
713 { NFSERR_MLINK, -EMLINK },
714 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
715 { NFSERR_NOTEMPTY, -ENOTEMPTY },
716 { NFSERR_DQUOT, -EDQUOT },
717 { NFSERR_STALE, -ESTALE },
718 { NFSERR_REMOTE, -EREMOTE },
1da177e4 719#ifdef EWFLUSH
856dff3d 720 { NFSERR_WFLUSH, -EWFLUSH },
1da177e4 721#endif
856dff3d
BH
722 { NFSERR_BADHANDLE, -EBADHANDLE },
723 { NFSERR_NOT_SYNC, -ENOTSYNC },
724 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
725 { NFSERR_NOTSUPP, -ENOTSUPP },
726 { NFSERR_TOOSMALL, -ETOOSMALL },
fdcb4577 727 { NFSERR_SERVERFAULT, -EREMOTEIO },
856dff3d
BH
728 { NFSERR_BADTYPE, -EBADTYPE },
729 { NFSERR_JUKEBOX, -EJUKEBOX },
730 { -1, -EIO }
1da177e4
LT
731};
732
733/*
734 * Convert an NFS error code to a local one.
735 * This one is used jointly by NFSv2 and NFSv3.
736 */
737int
738nfs_stat_to_errno(int stat)
739{
740 int i;
741
742 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
743 if (nfs_errtbl[i].stat == stat)
744 return nfs_errtbl[i].errno;
745 }
fe82a183 746 dprintk("nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
1da177e4
LT
747 return nfs_errtbl[i].errno;
748}
749
1da177e4
LT
750#define PROC(proc, argtype, restype, timer) \
751[NFSPROC_##proc] = { \
752 .p_proc = NFSPROC_##proc, \
753 .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
754 .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
2bea90d4
CL
755 .p_arglen = NFS_##argtype##_sz, \
756 .p_replen = NFS_##restype##_sz, \
cc0175c1
CL
757 .p_timer = timer, \
758 .p_statidx = NFSPROC_##proc, \
759 .p_name = #proc, \
1da177e4
LT
760 }
761struct rpc_procinfo nfs_procedures[] = {
762 PROC(GETATTR, fhandle, attrstat, 1),
763 PROC(SETATTR, sattrargs, attrstat, 0),
764 PROC(LOOKUP, diropargs, diropres, 2),
765 PROC(READLINK, readlinkargs, readlinkres, 3),
766 PROC(READ, readargs, readres, 3),
767 PROC(WRITE, writeargs, writeres, 4),
768 PROC(CREATE, createargs, diropres, 0),
4fdc17b2 769 PROC(REMOVE, removeargs, stat, 0),
1da177e4
LT
770 PROC(RENAME, renameargs, stat, 0),
771 PROC(LINK, linkargs, stat, 0),
772 PROC(SYMLINK, symlinkargs, stat, 0),
773 PROC(MKDIR, createargs, diropres, 0),
774 PROC(RMDIR, diropargs, stat, 0),
775 PROC(READDIR, readdirargs, readdirres, 3),
776 PROC(STATFS, fhandle, statfsres, 0),
777};
778
779struct rpc_version nfs_version2 = {
780 .number = 2,
e8c96f8c 781 .nrprocs = ARRAY_SIZE(nfs_procedures),
1da177e4
LT
782 .procs = nfs_procedures
783};
This page took 0.501786 seconds and 5 git commands to generate.