blob: 85e4b4a233f9d9b3f0bcab413c92458844c558e3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/nfs/nfs2xdr.c
4 *
5 * XDR functions to encode/decode NFS RPC arguments and results.
6 *
7 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
8 * Copyright (C) 1996 Olaf Kirch
9 * 04 Aug 1998 Ion Badulescu <ionut@cs.columbia.edu>
10 * FIFO's need special handling in NFSv2
11 */
12
13#include <linux/param.h>
14#include <linux/time.h>
15#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/string.h>
18#include <linux/in.h>
19#include <linux/pagemap.h>
20#include <linux/proc_fs.h>
21#include <linux/sunrpc/clnt.h>
22#include <linux/nfs.h>
23#include <linux/nfs2.h>
24#include <linux/nfs_fs.h>
Trond Myklebust816724e2006-06-24 08:41:41 -040025#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define NFSDBG_FACILITY NFSDBG_XDR
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Mapping from NFS error code to "errno" error code. */
30#define errno_NFSERR_IO EIO
31
32/*
33 * Declare the space requirements for NFS arguments and replies as
34 * number of 32bit-words
35 */
36#define NFS_fhandle_sz (8)
37#define NFS_sattr_sz (8)
38#define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
39#define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
40#define NFS_fattr_sz (17)
41#define NFS_info_sz (5)
42#define NFS_entry_sz (NFS_filename_sz+3)
43
44#define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
Trond Myklebust4fdc17b2007-07-14 15:39:57 -040045#define NFS_removeargs_sz (NFS_fhandle_sz+NFS_filename_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define NFS_sattrargs_sz (NFS_fhandle_sz+NFS_sattr_sz)
47#define NFS_readlinkargs_sz (NFS_fhandle_sz)
48#define NFS_readargs_sz (NFS_fhandle_sz+3)
49#define NFS_writeargs_sz (NFS_fhandle_sz+4)
50#define NFS_createargs_sz (NFS_diropargs_sz+NFS_sattr_sz)
51#define NFS_renameargs_sz (NFS_diropargs_sz+NFS_diropargs_sz)
52#define NFS_linkargs_sz (NFS_fhandle_sz+NFS_diropargs_sz)
Chuck Lever94a6d752006-08-22 20:06:23 -040053#define NFS_symlinkargs_sz (NFS_diropargs_sz+1+NFS_sattr_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define NFS_readdirargs_sz (NFS_fhandle_sz+2)
55
56#define NFS_attrstat_sz (1+NFS_fattr_sz)
57#define NFS_diropres_sz (1+NFS_fhandle_sz+NFS_fattr_sz)
58#define NFS_readlinkres_sz (2)
59#define NFS_readres_sz (1+NFS_fattr_sz+1)
60#define NFS_writeres_sz (NFS_attrstat_sz)
61#define NFS_stat_sz (1)
62#define NFS_readdirres_sz (1)
63#define NFS_statfsres_sz (1+NFS_info_sz)
64
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -040065static int nfs_stat_to_errno(enum nfs_stat);
Chuck Lever25a08662010-12-14 14:54:30 +000066
67/*
68 * While encoding arguments, set up the reply buffer in advance to
69 * receive reply data directly into the page cache.
70 */
71static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
72 unsigned int base, unsigned int len,
73 unsigned int bufsize)
74{
75 struct rpc_auth *auth = req->rq_cred->cr_auth;
76 unsigned int replen;
77
78 replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
79 xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
80}
81
Chuck Leverf796f8b2010-12-14 14:55:10 +000082/*
83 * Handle decode buffer overflows out-of-line.
84 */
85static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
86{
87 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
88 "Remaining buffer length is %tu words.\n",
89 func, xdr->end - xdr->p);
90}
91
Chuck Lever25a08662010-12-14 14:54:30 +000092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
Chuck Lever25a08662010-12-14 14:54:30 +000094 * Encode/decode NFSv2 basic data types
95 *
96 * Basic NFSv2 data types are defined in section 2.3 of RFC 1094:
97 * "NFS: Network File System Protocol Specification".
98 *
99 * Not all basic data types have their own encoding and decoding
100 * functions. For run-time efficiency, some data types are encoded
101 * or decoded inline.
102 */
103
104/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000105 * typedef opaque nfsdata<>;
106 */
Anna Schumaker9137bdf2014-05-06 09:12:25 -0400107static int decode_nfsdata(struct xdr_stream *xdr, struct nfs_pgio_res *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000108{
109 u32 recvd, count;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000110 __be32 *p;
111
112 p = xdr_inline_decode(xdr, 4);
113 if (unlikely(p == NULL))
114 goto out_overflow;
115 count = be32_to_cpup(p);
Trond Myklebust64bd5772012-06-20 22:35:05 -0400116 recvd = xdr_read_pages(xdr, count);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000117 if (unlikely(count > recvd))
118 goto out_cheating;
119out:
Chuck Leverf796f8b2010-12-14 14:55:10 +0000120 result->eof = 0; /* NFSv2 does not pass EOF flag on the wire. */
121 result->count = count;
122 return count;
123out_cheating:
124 dprintk("NFS: server cheating in read result: "
125 "count %u > recvd %u\n", count, recvd);
126 count = recvd;
127 goto out;
128out_overflow:
129 print_overflow_msg(__func__, xdr);
130 return -EIO;
131}
132
133/*
134 * enum stat {
135 * NFS_OK = 0,
136 * NFSERR_PERM = 1,
137 * NFSERR_NOENT = 2,
138 * NFSERR_IO = 5,
139 * NFSERR_NXIO = 6,
140 * NFSERR_ACCES = 13,
141 * NFSERR_EXIST = 17,
142 * NFSERR_NODEV = 19,
143 * NFSERR_NOTDIR = 20,
144 * NFSERR_ISDIR = 21,
145 * NFSERR_FBIG = 27,
146 * NFSERR_NOSPC = 28,
147 * NFSERR_ROFS = 30,
148 * NFSERR_NAMETOOLONG = 63,
149 * NFSERR_NOTEMPTY = 66,
150 * NFSERR_DQUOT = 69,
151 * NFSERR_STALE = 70,
152 * NFSERR_WFLUSH = 99
153 * };
154 */
155static int decode_stat(struct xdr_stream *xdr, enum nfs_stat *status)
156{
157 __be32 *p;
158
159 p = xdr_inline_decode(xdr, 4);
160 if (unlikely(p == NULL))
161 goto out_overflow;
162 *status = be32_to_cpup(p);
163 return 0;
164out_overflow:
165 print_overflow_msg(__func__, xdr);
166 return -EIO;
167}
168
169/*
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000170 * 2.3.2. ftype
171 *
172 * enum ftype {
173 * NFNON = 0,
174 * NFREG = 1,
175 * NFDIR = 2,
176 * NFBLK = 3,
177 * NFCHR = 4,
178 * NFLNK = 5
179 * };
180 *
181 */
182static __be32 *xdr_decode_ftype(__be32 *p, u32 *type)
183{
184 *type = be32_to_cpup(p++);
185 if (unlikely(*type > NF2FIFO))
186 *type = NFBAD;
187 return p;
188}
189
190/*
Chuck Lever25a08662010-12-14 14:54:30 +0000191 * 2.3.3. fhandle
192 *
193 * typedef opaque fhandle[FHSIZE];
194 */
195static void encode_fhandle(struct xdr_stream *xdr, const struct nfs_fh *fh)
196{
197 __be32 *p;
198
Chuck Lever25a08662010-12-14 14:54:30 +0000199 p = xdr_reserve_space(xdr, NFS2_FHSIZE);
200 memcpy(p, fh->data, NFS2_FHSIZE);
201}
202
Chuck Leverf796f8b2010-12-14 14:55:10 +0000203static int decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
204{
205 __be32 *p;
206
207 p = xdr_inline_decode(xdr, NFS2_FHSIZE);
208 if (unlikely(p == NULL))
209 goto out_overflow;
210 fh->size = NFS2_FHSIZE;
211 memcpy(fh->data, p, NFS2_FHSIZE);
212 return 0;
213out_overflow:
214 print_overflow_msg(__func__, xdr);
215 return -EIO;
216}
217
Chuck Lever25a08662010-12-14 14:54:30 +0000218/*
Chuck Lever282ac2a2010-12-14 14:54:50 +0000219 * 2.3.4. timeval
220 *
221 * struct timeval {
222 * unsigned int seconds;
223 * unsigned int useconds;
224 * };
225 */
226static __be32 *xdr_encode_time(__be32 *p, const struct timespec *timep)
227{
228 *p++ = cpu_to_be32(timep->tv_sec);
229 if (timep->tv_nsec != 0)
230 *p++ = cpu_to_be32(timep->tv_nsec / NSEC_PER_USEC);
231 else
232 *p++ = cpu_to_be32(0);
233 return p;
234}
235
236/*
237 * Passing the invalid value useconds=1000000 is a Sun convention for
238 * "set to current server time". It's needed to make permissions checks
239 * for the "touch" program across v2 mounts to Solaris and Irix servers
240 * work correctly. See description of sattr in section 6.1 of "NFS
241 * Illustrated" by Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5.
242 */
243static __be32 *xdr_encode_current_server_time(__be32 *p,
244 const struct timespec *timep)
245{
246 *p++ = cpu_to_be32(timep->tv_sec);
247 *p++ = cpu_to_be32(1000000);
248 return p;
249}
250
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000251static __be32 *xdr_decode_time(__be32 *p, struct timespec *timep)
252{
253 timep->tv_sec = be32_to_cpup(p++);
254 timep->tv_nsec = be32_to_cpup(p++) * NSEC_PER_USEC;
255 return p;
256}
257
Chuck Lever282ac2a2010-12-14 14:54:50 +0000258/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000259 * 2.3.5. fattr
260 *
261 * struct fattr {
262 * ftype type;
263 * unsigned int mode;
264 * unsigned int nlink;
265 * unsigned int uid;
266 * unsigned int gid;
267 * unsigned int size;
268 * unsigned int blocksize;
269 * unsigned int rdev;
270 * unsigned int blocks;
271 * unsigned int fsid;
272 * unsigned int fileid;
273 * timeval atime;
274 * timeval mtime;
275 * timeval ctime;
276 * };
277 *
278 */
279static int decode_fattr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
280{
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000281 u32 rdev, type;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000282 __be32 *p;
283
284 p = xdr_inline_decode(xdr, NFS_fattr_sz << 2);
285 if (unlikely(p == NULL))
286 goto out_overflow;
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000287
288 fattr->valid |= NFS_ATTR_FATTR_V2;
289
290 p = xdr_decode_ftype(p, &type);
291
292 fattr->mode = be32_to_cpup(p++);
293 fattr->nlink = be32_to_cpup(p++);
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800294 fattr->uid = make_kuid(&init_user_ns, be32_to_cpup(p++));
295 if (!uid_valid(fattr->uid))
296 goto out_uid;
297 fattr->gid = make_kgid(&init_user_ns, be32_to_cpup(p++));
298 if (!gid_valid(fattr->gid))
299 goto out_gid;
300
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000301 fattr->size = be32_to_cpup(p++);
302 fattr->du.nfs2.blocksize = be32_to_cpup(p++);
303
304 rdev = be32_to_cpup(p++);
305 fattr->rdev = new_decode_dev(rdev);
306 if (type == (u32)NFCHR && rdev == (u32)NFS2_FIFO_DEV) {
307 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
308 fattr->rdev = 0;
309 }
310
311 fattr->du.nfs2.blocks = be32_to_cpup(p++);
312 fattr->fsid.major = be32_to_cpup(p++);
313 fattr->fsid.minor = 0;
314 fattr->fileid = be32_to_cpup(p++);
315
316 p = xdr_decode_time(p, &fattr->atime);
317 p = xdr_decode_time(p, &fattr->mtime);
318 xdr_decode_time(p, &fattr->ctime);
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400319 fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
320
Chuck Leverf796f8b2010-12-14 14:55:10 +0000321 return 0;
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800322out_uid:
323 dprintk("NFS: returned invalid uid\n");
324 return -EINVAL;
325out_gid:
326 dprintk("NFS: returned invalid gid\n");
327 return -EINVAL;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000328out_overflow:
329 print_overflow_msg(__func__, xdr);
330 return -EIO;
331}
332
333/*
Chuck Lever25a08662010-12-14 14:54:30 +0000334 * 2.3.6. sattr
335 *
336 * struct sattr {
337 * unsigned int mode;
338 * unsigned int uid;
339 * unsigned int gid;
340 * unsigned int size;
341 * timeval atime;
342 * timeval mtime;
343 * };
344 */
345
346#define NFS2_SATTR_NOT_SET (0xffffffff)
347
348static __be32 *xdr_time_not_set(__be32 *p)
349{
350 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
351 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
352 return p;
353}
354
355static void encode_sattr(struct xdr_stream *xdr, const struct iattr *attr)
356{
357 __be32 *p;
358
359 p = xdr_reserve_space(xdr, NFS_sattr_sz << 2);
360
361 if (attr->ia_valid & ATTR_MODE)
362 *p++ = cpu_to_be32(attr->ia_mode);
363 else
364 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
365 if (attr->ia_valid & ATTR_UID)
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800366 *p++ = cpu_to_be32(from_kuid(&init_user_ns, attr->ia_uid));
Chuck Lever25a08662010-12-14 14:54:30 +0000367 else
368 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
369 if (attr->ia_valid & ATTR_GID)
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800370 *p++ = cpu_to_be32(from_kgid(&init_user_ns, attr->ia_gid));
Chuck Lever25a08662010-12-14 14:54:30 +0000371 else
372 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
373 if (attr->ia_valid & ATTR_SIZE)
374 *p++ = cpu_to_be32((u32)attr->ia_size);
375 else
376 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
377
378 if (attr->ia_valid & ATTR_ATIME_SET)
379 p = xdr_encode_time(p, &attr->ia_atime);
380 else if (attr->ia_valid & ATTR_ATIME)
381 p = xdr_encode_current_server_time(p, &attr->ia_atime);
382 else
383 p = xdr_time_not_set(p);
384 if (attr->ia_valid & ATTR_MTIME_SET)
385 xdr_encode_time(p, &attr->ia_mtime);
386 else if (attr->ia_valid & ATTR_MTIME)
387 xdr_encode_current_server_time(p, &attr->ia_mtime);
388 else
389 xdr_time_not_set(p);
390}
391
392/*
393 * 2.3.7. filename
394 *
395 * typedef string filename<MAXNAMLEN>;
396 */
397static void encode_filename(struct xdr_stream *xdr,
398 const char *name, u32 length)
399{
400 __be32 *p;
401
Trond Myklebust7fc38842012-10-15 11:51:21 -0400402 WARN_ON_ONCE(length > NFS2_MAXNAMLEN);
Chuck Lever25a08662010-12-14 14:54:30 +0000403 p = xdr_reserve_space(xdr, 4 + length);
404 xdr_encode_opaque(p, name, length);
405}
406
Chuck Leverf796f8b2010-12-14 14:55:10 +0000407static int decode_filename_inline(struct xdr_stream *xdr,
408 const char **name, u32 *length)
409{
410 __be32 *p;
411 u32 count;
412
413 p = xdr_inline_decode(xdr, 4);
414 if (unlikely(p == NULL))
415 goto out_overflow;
416 count = be32_to_cpup(p);
417 if (count > NFS3_MAXNAMLEN)
418 goto out_nametoolong;
419 p = xdr_inline_decode(xdr, count);
420 if (unlikely(p == NULL))
421 goto out_overflow;
422 *name = (const char *)p;
423 *length = count;
424 return 0;
425out_nametoolong:
426 dprintk("NFS: returned filename too long: %u\n", count);
427 return -ENAMETOOLONG;
428out_overflow:
429 print_overflow_msg(__func__, xdr);
430 return -EIO;
431}
432
Chuck Lever25a08662010-12-14 14:54:30 +0000433/*
434 * 2.3.8. path
435 *
436 * typedef string path<MAXPATHLEN>;
437 */
438static void encode_path(struct xdr_stream *xdr, struct page **pages, u32 length)
439{
440 __be32 *p;
441
Chuck Lever25a08662010-12-14 14:54:30 +0000442 p = xdr_reserve_space(xdr, 4);
443 *p = cpu_to_be32(length);
444 xdr_write_pages(xdr, pages, 0, length);
445}
446
Chuck Leverf796f8b2010-12-14 14:55:10 +0000447static int decode_path(struct xdr_stream *xdr)
448{
449 u32 length, recvd;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000450 __be32 *p;
451
452 p = xdr_inline_decode(xdr, 4);
453 if (unlikely(p == NULL))
454 goto out_overflow;
455 length = be32_to_cpup(p);
456 if (unlikely(length >= xdr->buf->page_len || length > NFS_MAXPATHLEN))
457 goto out_size;
Trond Myklebust64bd5772012-06-20 22:35:05 -0400458 recvd = xdr_read_pages(xdr, length);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000459 if (unlikely(length > recvd))
460 goto out_cheating;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000461 xdr_terminate_string(xdr->buf, length);
462 return 0;
463out_size:
464 dprintk("NFS: returned pathname too long: %u\n", length);
465 return -ENAMETOOLONG;
466out_cheating:
467 dprintk("NFS: server cheating in pathname result: "
468 "length %u > received %u\n", length, recvd);
469 return -EIO;
470out_overflow:
471 print_overflow_msg(__func__, xdr);
472 return -EIO;
473}
474
475/*
476 * 2.3.9. attrstat
477 *
478 * union attrstat switch (stat status) {
479 * case NFS_OK:
480 * fattr attributes;
481 * default:
482 * void;
483 * };
484 */
Peng Taoaabff4d2014-08-27 10:47:14 +0800485static int decode_attrstat(struct xdr_stream *xdr, struct nfs_fattr *result,
486 __u32 *op_status)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000487{
488 enum nfs_stat status;
489 int error;
490
491 error = decode_stat(xdr, &status);
492 if (unlikely(error))
493 goto out;
Peng Taoaabff4d2014-08-27 10:47:14 +0800494 if (op_status)
495 *op_status = status;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000496 if (status != NFS_OK)
497 goto out_default;
498 error = decode_fattr(xdr, result);
499out:
500 return error;
501out_default:
502 return nfs_stat_to_errno(status);
503}
504
Chuck Lever25a08662010-12-14 14:54:30 +0000505/*
506 * 2.3.10. diropargs
507 *
508 * struct diropargs {
509 * fhandle dir;
510 * filename name;
511 * };
512 */
513static void encode_diropargs(struct xdr_stream *xdr, const struct nfs_fh *fh,
514 const char *name, u32 length)
515{
516 encode_fhandle(xdr, fh);
517 encode_filename(xdr, name, length);
518}
519
Chuck Leverf796f8b2010-12-14 14:55:10 +0000520/*
521 * 2.3.11. diropres
522 *
523 * union diropres switch (stat status) {
524 * case NFS_OK:
525 * struct {
526 * fhandle file;
527 * fattr attributes;
528 * } diropok;
529 * default:
530 * void;
531 * };
532 */
533static int decode_diropok(struct xdr_stream *xdr, struct nfs_diropok *result)
534{
535 int error;
536
537 error = decode_fhandle(xdr, result->fh);
538 if (unlikely(error))
539 goto out;
540 error = decode_fattr(xdr, result->fattr);
541out:
542 return error;
543}
544
545static int decode_diropres(struct xdr_stream *xdr, struct nfs_diropok *result)
546{
547 enum nfs_stat status;
548 int error;
549
550 error = decode_stat(xdr, &status);
551 if (unlikely(error))
552 goto out;
553 if (status != NFS_OK)
554 goto out_default;
555 error = decode_diropok(xdr, result);
556out:
557 return error;
558out_default:
559 return nfs_stat_to_errno(status);
560}
561
Chuck Lever25a08662010-12-14 14:54:30 +0000562
563/*
Chuck Lever2d70f532010-12-14 14:54:40 +0000564 * NFSv2 XDR encode functions
565 *
566 * NFSv2 argument types are defined in section 2.2 of RFC 1094:
567 * "NFS: Network File System Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Chuck Lever9f06c712010-12-14 14:59:18 +0000570static void nfs2_xdr_enc_fhandle(struct rpc_rqst *req,
571 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200572 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000573{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200574 const struct nfs_fh *fh = data;
575
Chuck Lever9f06c712010-12-14 14:59:18 +0000576 encode_fhandle(xdr, fh);
Chuck Lever25a08662010-12-14 14:54:30 +0000577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
Chuck Lever25a08662010-12-14 14:54:30 +0000580 * 2.2.3. sattrargs
581 *
582 * struct sattrargs {
583 * fhandle file;
584 * sattr attributes;
585 * };
586 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000587static void nfs2_xdr_enc_sattrargs(struct rpc_rqst *req,
588 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200589 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000590{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200591 const struct nfs_sattrargs *args = data;
592
Chuck Lever9f06c712010-12-14 14:59:18 +0000593 encode_fhandle(xdr, args->fh);
594 encode_sattr(xdr, args->sattr);
Chuck Lever25a08662010-12-14 14:54:30 +0000595}
596
Chuck Lever9f06c712010-12-14 14:59:18 +0000597static void nfs2_xdr_enc_diropargs(struct rpc_rqst *req,
598 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200599 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000600{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200601 const struct nfs_diropargs *args = data;
602
Chuck Lever9f06c712010-12-14 14:59:18 +0000603 encode_diropargs(xdr, args->fh, args->name, args->len);
Chuck Lever25a08662010-12-14 14:54:30 +0000604}
605
Chuck Lever9f06c712010-12-14 14:59:18 +0000606static void nfs2_xdr_enc_readlinkargs(struct rpc_rqst *req,
607 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200608 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000609{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200610 const struct nfs_readlinkargs *args = data;
611
Chuck Lever9f06c712010-12-14 14:59:18 +0000612 encode_fhandle(xdr, args->fh);
Chuck Lever25a08662010-12-14 14:54:30 +0000613 prepare_reply_buffer(req, args->pages, args->pgbase,
614 args->pglen, NFS_readlinkres_sz);
Chuck Lever25a08662010-12-14 14:54:30 +0000615}
616
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400617/*
Chuck Lever25a08662010-12-14 14:54:30 +0000618 * 2.2.7. readargs
619 *
620 * struct readargs {
621 * fhandle file;
622 * unsigned offset;
623 * unsigned count;
624 * unsigned totalcount;
625 * };
626 */
627static void encode_readargs(struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400628 const struct nfs_pgio_args *args)
Chuck Lever25a08662010-12-14 14:54:30 +0000629{
630 u32 offset = args->offset;
631 u32 count = args->count;
632 __be32 *p;
633
634 encode_fhandle(xdr, args->fh);
635
636 p = xdr_reserve_space(xdr, 4 + 4 + 4);
637 *p++ = cpu_to_be32(offset);
638 *p++ = cpu_to_be32(count);
639 *p = cpu_to_be32(count);
640}
641
Chuck Lever9f06c712010-12-14 14:59:18 +0000642static void nfs2_xdr_enc_readargs(struct rpc_rqst *req,
643 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200644 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000645{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200646 const struct nfs_pgio_args *args = data;
647
Chuck Lever9f06c712010-12-14 14:59:18 +0000648 encode_readargs(xdr, args);
Chuck Lever25a08662010-12-14 14:54:30 +0000649 prepare_reply_buffer(req, args->pages, args->pgbase,
650 args->count, NFS_readres_sz);
651 req->rq_rcv_buf.flags |= XDRBUF_READ;
Chuck Lever25a08662010-12-14 14:54:30 +0000652}
653
654/*
Chuck Lever25a08662010-12-14 14:54:30 +0000655 * 2.2.9. writeargs
656 *
657 * struct writeargs {
658 * fhandle file;
659 * unsigned beginoffset;
660 * unsigned offset;
661 * unsigned totalcount;
662 * nfsdata data;
663 * };
664 */
665static void encode_writeargs(struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400666 const struct nfs_pgio_args *args)
Chuck Lever25a08662010-12-14 14:54:30 +0000667{
668 u32 offset = args->offset;
669 u32 count = args->count;
670 __be32 *p;
671
672 encode_fhandle(xdr, args->fh);
673
674 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
675 *p++ = cpu_to_be32(offset);
676 *p++ = cpu_to_be32(offset);
677 *p++ = cpu_to_be32(count);
678
679 /* nfsdata */
680 *p = cpu_to_be32(count);
681 xdr_write_pages(xdr, args->pages, args->pgbase, count);
682}
683
Chuck Lever9f06c712010-12-14 14:59:18 +0000684static void nfs2_xdr_enc_writeargs(struct rpc_rqst *req,
685 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200686 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000687{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200688 const struct nfs_pgio_args *args = data;
689
Chuck Lever9f06c712010-12-14 14:59:18 +0000690 encode_writeargs(xdr, args);
691 xdr->buf->flags |= XDRBUF_WRITE;
Chuck Lever25a08662010-12-14 14:54:30 +0000692}
693
694/*
Chuck Lever25a08662010-12-14 14:54:30 +0000695 * 2.2.10. createargs
696 *
697 * struct createargs {
698 * diropargs where;
699 * sattr attributes;
700 * };
701 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000702static void nfs2_xdr_enc_createargs(struct rpc_rqst *req,
703 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200704 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000705{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200706 const struct nfs_createargs *args = data;
707
Chuck Lever9f06c712010-12-14 14:59:18 +0000708 encode_diropargs(xdr, args->fh, args->name, args->len);
709 encode_sattr(xdr, args->sattr);
Chuck Lever25a08662010-12-14 14:54:30 +0000710}
711
Chuck Lever9f06c712010-12-14 14:59:18 +0000712static void nfs2_xdr_enc_removeargs(struct rpc_rqst *req,
713 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200714 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000715{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200716 const struct nfs_removeargs *args = data;
717
Chuck Lever9f06c712010-12-14 14:59:18 +0000718 encode_diropargs(xdr, args->fh, args->name.name, args->name.len);
Chuck Lever25a08662010-12-14 14:54:30 +0000719}
720
721/*
Chuck Lever25a08662010-12-14 14:54:30 +0000722 * 2.2.12. renameargs
723 *
724 * struct renameargs {
725 * diropargs from;
726 * diropargs to;
727 * };
728 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000729static void nfs2_xdr_enc_renameargs(struct rpc_rqst *req,
730 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200731 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000732{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200733 const struct nfs_renameargs *args = data;
Chuck Lever25a08662010-12-14 14:54:30 +0000734 const struct qstr *old = args->old_name;
735 const struct qstr *new = args->new_name;
Chuck Lever25a08662010-12-14 14:54:30 +0000736
Chuck Lever9f06c712010-12-14 14:59:18 +0000737 encode_diropargs(xdr, args->old_dir, old->name, old->len);
738 encode_diropargs(xdr, args->new_dir, new->name, new->len);
Chuck Lever25a08662010-12-14 14:54:30 +0000739}
740
741/*
Chuck Lever25a08662010-12-14 14:54:30 +0000742 * 2.2.13. linkargs
743 *
744 * struct linkargs {
745 * fhandle from;
746 * diropargs to;
747 * };
748 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000749static void nfs2_xdr_enc_linkargs(struct rpc_rqst *req,
750 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200751 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000752{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200753 const struct nfs_linkargs *args = data;
754
Chuck Lever9f06c712010-12-14 14:59:18 +0000755 encode_fhandle(xdr, args->fromfh);
756 encode_diropargs(xdr, args->tofh, args->toname, args->tolen);
Chuck Lever25a08662010-12-14 14:54:30 +0000757}
758
759/*
Chuck Lever25a08662010-12-14 14:54:30 +0000760 * 2.2.14. symlinkargs
761 *
762 * struct symlinkargs {
763 * diropargs from;
764 * path to;
765 * sattr attributes;
766 * };
767 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000768static void nfs2_xdr_enc_symlinkargs(struct rpc_rqst *req,
769 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200770 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000771{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200772 const struct nfs_symlinkargs *args = data;
773
Chuck Lever9f06c712010-12-14 14:59:18 +0000774 encode_diropargs(xdr, args->fromfh, args->fromname, args->fromlen);
775 encode_path(xdr, args->pages, args->pathlen);
776 encode_sattr(xdr, args->sattr);
Chuck Lever25a08662010-12-14 14:54:30 +0000777}
778
779/*
Chuck Lever25a08662010-12-14 14:54:30 +0000780 * 2.2.17. readdirargs
781 *
782 * struct readdirargs {
783 * fhandle dir;
784 * nfscookie cookie;
785 * unsigned count;
786 * };
787 */
788static void encode_readdirargs(struct xdr_stream *xdr,
789 const struct nfs_readdirargs *args)
790{
791 __be32 *p;
792
793 encode_fhandle(xdr, args->fh);
794
795 p = xdr_reserve_space(xdr, 4 + 4);
796 *p++ = cpu_to_be32(args->cookie);
797 *p = cpu_to_be32(args->count);
798}
799
Chuck Lever9f06c712010-12-14 14:59:18 +0000800static void nfs2_xdr_enc_readdirargs(struct rpc_rqst *req,
801 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200802 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000803{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200804 const struct nfs_readdirargs *args = data;
805
Chuck Lever9f06c712010-12-14 14:59:18 +0000806 encode_readdirargs(xdr, args);
Chuck Lever25a08662010-12-14 14:54:30 +0000807 prepare_reply_buffer(req, args->pages, 0,
808 args->count, NFS_readdirres_sz);
Chuck Lever25a08662010-12-14 14:54:30 +0000809}
810
811/*
Chuck Lever661ad422010-12-14 14:55:20 +0000812 * NFSv2 XDR decode functions
813 *
814 * NFSv2 result types are defined in section 2.2 of RFC 1094:
815 * "NFS: Network File System Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Chuck Leverbf269552010-12-14 14:59:29 +0000818static int nfs2_xdr_dec_stat(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Leverf796f8b2010-12-14 14:55:10 +0000819 void *__unused)
820{
Chuck Leverf796f8b2010-12-14 14:55:10 +0000821 enum nfs_stat status;
822 int error;
823
Chuck Leverbf269552010-12-14 14:59:29 +0000824 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000825 if (unlikely(error))
826 goto out;
827 if (status != NFS_OK)
828 goto out_default;
829out:
830 return error;
831out_default:
832 return nfs_stat_to_errno(status);
833}
834
Chuck Leverbf269552010-12-14 14:59:29 +0000835static int nfs2_xdr_dec_attrstat(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200836 void *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000837{
Peng Taoaabff4d2014-08-27 10:47:14 +0800838 return decode_attrstat(xdr, result, NULL);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000839}
840
Chuck Leverbf269552010-12-14 14:59:29 +0000841static int nfs2_xdr_dec_diropres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200842 void *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000843{
Chuck Leverbf269552010-12-14 14:59:29 +0000844 return decode_diropres(xdr, result);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000848 * 2.2.6. readlinkres
849 *
850 * union readlinkres switch (stat status) {
851 * case NFS_OK:
852 * path data;
853 * default:
854 * void;
855 * };
856 */
Chuck Leverbf269552010-12-14 14:59:29 +0000857static int nfs2_xdr_dec_readlinkres(struct rpc_rqst *req,
858 struct xdr_stream *xdr, void *__unused)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000859{
Chuck Leverf796f8b2010-12-14 14:55:10 +0000860 enum nfs_stat status;
861 int error;
862
Chuck Leverbf269552010-12-14 14:59:29 +0000863 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000864 if (unlikely(error))
865 goto out;
866 if (status != NFS_OK)
867 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +0000868 error = decode_path(xdr);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000869out:
870 return error;
871out_default:
872 return nfs_stat_to_errno(status);
873}
874
875/*
876 * 2.2.7. readres
877 *
878 * union readres switch (stat status) {
879 * case NFS_OK:
880 * fattr attributes;
881 * nfsdata data;
882 * default:
883 * void;
884 * };
885 */
Chuck Leverbf269552010-12-14 14:59:29 +0000886static int nfs2_xdr_dec_readres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200887 void *data)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000888{
Christoph Hellwigfc016482017-05-08 15:09:02 +0200889 struct nfs_pgio_res *result = data;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000890 enum nfs_stat status;
891 int error;
892
Chuck Leverbf269552010-12-14 14:59:29 +0000893 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000894 if (unlikely(error))
895 goto out;
Peng Taoaabff4d2014-08-27 10:47:14 +0800896 result->op_status = status;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000897 if (status != NFS_OK)
898 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +0000899 error = decode_fattr(xdr, result->fattr);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000900 if (unlikely(error))
901 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +0000902 error = decode_nfsdata(xdr, result);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000903out:
904 return error;
905out_default:
906 return nfs_stat_to_errno(status);
907}
908
Chuck Leverbf269552010-12-14 14:59:29 +0000909static int nfs2_xdr_dec_writeres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200910 void *data)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000911{
Christoph Hellwigfc016482017-05-08 15:09:02 +0200912 struct nfs_pgio_res *result = data;
913
Chuck Leverf796f8b2010-12-14 14:55:10 +0000914 /* All NFSv2 writes are "file sync" writes */
915 result->verf->committed = NFS_FILE_SYNC;
Peng Taoaabff4d2014-08-27 10:47:14 +0800916 return decode_attrstat(xdr, result->fattr, &result->op_status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000917}
918
919/**
920 * nfs2_decode_dirent - Decode a single NFSv2 directory entry stored in
921 * the local page cache.
922 * @xdr: XDR stream where entry resides
923 * @entry: buffer to fill in with entry data
Chuck Leverf796f8b2010-12-14 14:55:10 +0000924 * @plus: boolean indicating whether this should be a readdirplus entry
925 *
Chuck Lever573c4e12010-12-14 14:58:11 +0000926 * Returns zero if successful, otherwise a negative errno value is
927 * returned.
Chuck Leverf796f8b2010-12-14 14:55:10 +0000928 *
929 * This function is not invoked during READDIR reply decoding, but
930 * rather whenever an application invokes the getdents(2) system call
931 * on a directory already in our cache.
932 *
933 * 2.2.17. entry
934 *
935 * struct entry {
936 * unsigned fileid;
937 * filename name;
938 * nfscookie cookie;
939 * entry *nextentry;
940 * };
941 */
Chuck Lever573c4e12010-12-14 14:58:11 +0000942int nfs2_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400943 bool plus)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000944{
945 __be32 *p;
946 int error;
947
948 p = xdr_inline_decode(xdr, 4);
949 if (unlikely(p == NULL))
950 goto out_overflow;
951 if (*p++ == xdr_zero) {
952 p = xdr_inline_decode(xdr, 4);
953 if (unlikely(p == NULL))
954 goto out_overflow;
955 if (*p++ == xdr_zero)
Chuck Lever573c4e12010-12-14 14:58:11 +0000956 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000957 entry->eof = 1;
Chuck Lever573c4e12010-12-14 14:58:11 +0000958 return -EBADCOOKIE;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000959 }
960
961 p = xdr_inline_decode(xdr, 4);
962 if (unlikely(p == NULL))
963 goto out_overflow;
964 entry->ino = be32_to_cpup(p);
965
966 error = decode_filename_inline(xdr, &entry->name, &entry->len);
967 if (unlikely(error))
Chuck Lever573c4e12010-12-14 14:58:11 +0000968 return error;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000969
970 /*
971 * The type (size and byte order) of nfscookie isn't defined in
972 * RFC 1094. This implementation assumes that it's an XDR uint32.
973 */
974 entry->prev_cookie = entry->cookie;
975 p = xdr_inline_decode(xdr, 4);
976 if (unlikely(p == NULL))
977 goto out_overflow;
978 entry->cookie = be32_to_cpup(p);
979
980 entry->d_type = DT_UNKNOWN;
981
Chuck Lever573c4e12010-12-14 14:58:11 +0000982 return 0;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000983
984out_overflow:
985 print_overflow_msg(__func__, xdr);
Chuck Lever573c4e12010-12-14 14:58:11 +0000986 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000987}
988
989/*
990 * 2.2.17. readdirres
991 *
992 * union readdirres switch (stat status) {
993 * case NFS_OK:
994 * struct {
995 * entry *entries;
996 * bool eof;
997 * } readdirok;
998 * default:
999 * void;
1000 * };
1001 *
1002 * Read the directory contents into the page cache, but don't
1003 * touch them. The actual decoding is done by nfs2_decode_dirent()
1004 * during subsequent nfs_readdir() calls.
1005 */
1006static int decode_readdirok(struct xdr_stream *xdr)
1007{
Trond Myklebust64bd5772012-06-20 22:35:05 -04001008 return xdr_read_pages(xdr, xdr->buf->page_len);
Chuck Leverf796f8b2010-12-14 14:55:10 +00001009}
1010
Chuck Leverbf269552010-12-14 14:59:29 +00001011static int nfs2_xdr_dec_readdirres(struct rpc_rqst *req,
1012 struct xdr_stream *xdr, void *__unused)
Chuck Leverf796f8b2010-12-14 14:55:10 +00001013{
Chuck Leverf796f8b2010-12-14 14:55:10 +00001014 enum nfs_stat status;
1015 int error;
1016
Chuck Leverbf269552010-12-14 14:59:29 +00001017 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +00001018 if (unlikely(error))
1019 goto out;
1020 if (status != NFS_OK)
1021 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001022 error = decode_readdirok(xdr);
Chuck Leverf796f8b2010-12-14 14:55:10 +00001023out:
1024 return error;
1025out_default:
1026 return nfs_stat_to_errno(status);
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
Chuck Leverf796f8b2010-12-14 14:55:10 +00001030 * 2.2.18. statfsres
1031 *
1032 * union statfsres (stat status) {
1033 * case NFS_OK:
1034 * struct {
1035 * unsigned tsize;
1036 * unsigned bsize;
1037 * unsigned blocks;
1038 * unsigned bfree;
1039 * unsigned bavail;
1040 * } info;
1041 * default:
1042 * void;
1043 * };
1044 */
1045static int decode_info(struct xdr_stream *xdr, struct nfs2_fsstat *result)
1046{
1047 __be32 *p;
1048
1049 p = xdr_inline_decode(xdr, NFS_info_sz << 2);
1050 if (unlikely(p == NULL))
1051 goto out_overflow;
1052 result->tsize = be32_to_cpup(p++);
1053 result->bsize = be32_to_cpup(p++);
1054 result->blocks = be32_to_cpup(p++);
1055 result->bfree = be32_to_cpup(p++);
1056 result->bavail = be32_to_cpup(p);
1057 return 0;
1058out_overflow:
1059 print_overflow_msg(__func__, xdr);
1060 return -EIO;
1061}
1062
Chuck Leverbf269552010-12-14 14:59:29 +00001063static int nfs2_xdr_dec_statfsres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +02001064 void *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +00001065{
Chuck Leverf796f8b2010-12-14 14:55:10 +00001066 enum nfs_stat status;
1067 int error;
1068
Chuck Leverbf269552010-12-14 14:59:29 +00001069 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +00001070 if (unlikely(error))
1071 goto out;
1072 if (status != NFS_OK)
1073 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001074 error = decode_info(xdr, result);
Chuck Leverf796f8b2010-12-14 14:55:10 +00001075out:
1076 return error;
1077out_default:
1078 return nfs_stat_to_errno(status);
1079}
1080
1081
1082/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * We need to translate between nfs status return values and
1084 * the local errno values which may not be the same.
1085 */
Chuck Lever85828492010-12-14 14:55:00 +00001086static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int stat;
1088 int errno;
1089} nfs_errtbl[] = {
1090 { NFS_OK, 0 },
Benny Halevy856dff32008-03-31 17:39:06 +03001091 { NFSERR_PERM, -EPERM },
1092 { NFSERR_NOENT, -ENOENT },
1093 { NFSERR_IO, -errno_NFSERR_IO},
1094 { NFSERR_NXIO, -ENXIO },
1095/* { NFSERR_EAGAIN, -EAGAIN }, */
1096 { NFSERR_ACCES, -EACCES },
1097 { NFSERR_EXIST, -EEXIST },
1098 { NFSERR_XDEV, -EXDEV },
1099 { NFSERR_NODEV, -ENODEV },
1100 { NFSERR_NOTDIR, -ENOTDIR },
1101 { NFSERR_ISDIR, -EISDIR },
1102 { NFSERR_INVAL, -EINVAL },
1103 { NFSERR_FBIG, -EFBIG },
1104 { NFSERR_NOSPC, -ENOSPC },
1105 { NFSERR_ROFS, -EROFS },
1106 { NFSERR_MLINK, -EMLINK },
1107 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
1108 { NFSERR_NOTEMPTY, -ENOTEMPTY },
1109 { NFSERR_DQUOT, -EDQUOT },
1110 { NFSERR_STALE, -ESTALE },
1111 { NFSERR_REMOTE, -EREMOTE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112#ifdef EWFLUSH
Benny Halevy856dff32008-03-31 17:39:06 +03001113 { NFSERR_WFLUSH, -EWFLUSH },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114#endif
Benny Halevy856dff32008-03-31 17:39:06 +03001115 { NFSERR_BADHANDLE, -EBADHANDLE },
1116 { NFSERR_NOT_SYNC, -ENOTSYNC },
1117 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
1118 { NFSERR_NOTSUPP, -ENOTSUPP },
1119 { NFSERR_TOOSMALL, -ETOOSMALL },
Trond Myklebustfdcb4572010-02-08 09:32:40 -05001120 { NFSERR_SERVERFAULT, -EREMOTEIO },
Benny Halevy856dff32008-03-31 17:39:06 +03001121 { NFSERR_BADTYPE, -EBADTYPE },
1122 { NFSERR_JUKEBOX, -EJUKEBOX },
1123 { -1, -EIO }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124};
1125
Chuck Lever85828492010-12-14 14:55:00 +00001126/**
1127 * nfs_stat_to_errno - convert an NFS status code to a local errno
1128 * @status: NFS status code to convert
1129 *
1130 * Returns a local errno value, or -EIO if the NFS status code is
1131 * not recognized. This function is used jointly by NFSv2 and NFSv3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 */
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001133static int nfs_stat_to_errno(enum nfs_stat status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int i;
1136
1137 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
Chuck Lever85828492010-12-14 14:55:00 +00001138 if (nfs_errtbl[i].stat == (int)status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return nfs_errtbl[i].errno;
1140 }
Chuck Lever85828492010-12-14 14:55:00 +00001141 dprintk("NFS: Unrecognized nfs status value: %u\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return nfs_errtbl[i].errno;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145#define PROC(proc, argtype, restype, timer) \
1146[NFSPROC_##proc] = { \
1147 .p_proc = NFSPROC_##proc, \
Christoph Hellwigfcc85812017-05-08 10:01:49 +02001148 .p_encode = nfs2_xdr_enc_##argtype, \
Christoph Hellwigfc016482017-05-08 15:09:02 +02001149 .p_decode = nfs2_xdr_dec_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -04001150 .p_arglen = NFS_##argtype##_sz, \
1151 .p_replen = NFS_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05001152 .p_timer = timer, \
1153 .p_statidx = NFSPROC_##proc, \
1154 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Christoph Hellwig511e9362017-05-12 15:36:49 +02001156const struct rpc_procinfo nfs_procedures[] = {
Chuck Lever7d93bd712010-12-14 14:57:42 +00001157 PROC(GETATTR, fhandle, attrstat, 1),
1158 PROC(SETATTR, sattrargs, attrstat, 0),
1159 PROC(LOOKUP, diropargs, diropres, 2),
1160 PROC(READLINK, readlinkargs, readlinkres, 3),
1161 PROC(READ, readargs, readres, 3),
1162 PROC(WRITE, writeargs, writeres, 4),
1163 PROC(CREATE, createargs, diropres, 0),
1164 PROC(REMOVE, removeargs, stat, 0),
1165 PROC(RENAME, renameargs, stat, 0),
1166 PROC(LINK, linkargs, stat, 0),
1167 PROC(SYMLINK, symlinkargs, stat, 0),
1168 PROC(MKDIR, createargs, diropres, 0),
1169 PROC(RMDIR, diropargs, stat, 0),
1170 PROC(READDIR, readdirargs, readdirres, 3),
1171 PROC(STATFS, fhandle, statfsres, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172};
1173
Christoph Hellwigc5518582017-05-08 23:27:10 +02001174static unsigned int nfs_version2_counts[ARRAY_SIZE(nfs_procedures)];
Trond Myklebusta613fa12012-01-20 13:53:56 -05001175const struct rpc_version nfs_version2 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 .number = 2,
Tobias Klausere8c96f82006-03-24 03:15:34 -08001177 .nrprocs = ARRAY_SIZE(nfs_procedures),
Christoph Hellwigc5518582017-05-08 23:27:10 +02001178 .procs = nfs_procedures,
1179 .counts = nfs_version2_counts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180};