blob: 76614311f9571b42eb291ecc4edbf4b45395ac1f [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/*
Chuck Lever25a08662010-12-14 14:54:30 +000083 * Encode/decode NFSv2 basic data types
84 *
85 * Basic NFSv2 data types are defined in section 2.3 of RFC 1094:
86 * "NFS: Network File System Protocol Specification".
87 *
88 * Not all basic data types have their own encoding and decoding
89 * functions. For run-time efficiency, some data types are encoded
90 * or decoded inline.
91 */
92
93/*
Chuck Leverf796f8b2010-12-14 14:55:10 +000094 * typedef opaque nfsdata<>;
95 */
Anna Schumaker9137bdf2014-05-06 09:12:25 -040096static int decode_nfsdata(struct xdr_stream *xdr, struct nfs_pgio_res *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +000097{
98 u32 recvd, count;
Chuck Leverf796f8b2010-12-14 14:55:10 +000099 __be32 *p;
100
101 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500102 if (unlikely(!p))
103 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000104 count = be32_to_cpup(p);
Trond Myklebust64bd5772012-06-20 22:35:05 -0400105 recvd = xdr_read_pages(xdr, count);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000106 if (unlikely(count > recvd))
107 goto out_cheating;
108out:
Chuck Leverf796f8b2010-12-14 14:55:10 +0000109 result->eof = 0; /* NFSv2 does not pass EOF flag on the wire. */
110 result->count = count;
111 return count;
112out_cheating:
113 dprintk("NFS: server cheating in read result: "
114 "count %u > recvd %u\n", count, recvd);
115 count = recvd;
116 goto out;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000117}
118
119/*
120 * enum stat {
121 * NFS_OK = 0,
122 * NFSERR_PERM = 1,
123 * NFSERR_NOENT = 2,
124 * NFSERR_IO = 5,
125 * NFSERR_NXIO = 6,
126 * NFSERR_ACCES = 13,
127 * NFSERR_EXIST = 17,
128 * NFSERR_NODEV = 19,
129 * NFSERR_NOTDIR = 20,
130 * NFSERR_ISDIR = 21,
131 * NFSERR_FBIG = 27,
132 * NFSERR_NOSPC = 28,
133 * NFSERR_ROFS = 30,
134 * NFSERR_NAMETOOLONG = 63,
135 * NFSERR_NOTEMPTY = 66,
136 * NFSERR_DQUOT = 69,
137 * NFSERR_STALE = 70,
138 * NFSERR_WFLUSH = 99
139 * };
140 */
141static int decode_stat(struct xdr_stream *xdr, enum nfs_stat *status)
142{
143 __be32 *p;
144
145 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500146 if (unlikely(!p))
147 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000148 *status = be32_to_cpup(p);
149 return 0;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000150}
151
152/*
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000153 * 2.3.2. ftype
154 *
155 * enum ftype {
156 * NFNON = 0,
157 * NFREG = 1,
158 * NFDIR = 2,
159 * NFBLK = 3,
160 * NFCHR = 4,
161 * NFLNK = 5
162 * };
163 *
164 */
165static __be32 *xdr_decode_ftype(__be32 *p, u32 *type)
166{
167 *type = be32_to_cpup(p++);
168 if (unlikely(*type > NF2FIFO))
169 *type = NFBAD;
170 return p;
171}
172
173/*
Chuck Lever25a08662010-12-14 14:54:30 +0000174 * 2.3.3. fhandle
175 *
176 * typedef opaque fhandle[FHSIZE];
177 */
178static void encode_fhandle(struct xdr_stream *xdr, const struct nfs_fh *fh)
179{
180 __be32 *p;
181
Chuck Lever25a08662010-12-14 14:54:30 +0000182 p = xdr_reserve_space(xdr, NFS2_FHSIZE);
183 memcpy(p, fh->data, NFS2_FHSIZE);
184}
185
Chuck Leverf796f8b2010-12-14 14:55:10 +0000186static int decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
187{
188 __be32 *p;
189
190 p = xdr_inline_decode(xdr, NFS2_FHSIZE);
Chuck Levereb72f482019-02-11 11:24:21 -0500191 if (unlikely(!p))
192 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000193 fh->size = NFS2_FHSIZE;
194 memcpy(fh->data, p, NFS2_FHSIZE);
195 return 0;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000196}
197
Chuck Lever25a08662010-12-14 14:54:30 +0000198/*
Chuck Lever282ac2a2010-12-14 14:54:50 +0000199 * 2.3.4. timeval
200 *
201 * struct timeval {
202 * unsigned int seconds;
203 * unsigned int useconds;
204 * };
205 */
206static __be32 *xdr_encode_time(__be32 *p, const struct timespec *timep)
207{
208 *p++ = cpu_to_be32(timep->tv_sec);
209 if (timep->tv_nsec != 0)
210 *p++ = cpu_to_be32(timep->tv_nsec / NSEC_PER_USEC);
211 else
212 *p++ = cpu_to_be32(0);
213 return p;
214}
215
216/*
217 * Passing the invalid value useconds=1000000 is a Sun convention for
218 * "set to current server time". It's needed to make permissions checks
219 * for the "touch" program across v2 mounts to Solaris and Irix servers
220 * work correctly. See description of sattr in section 6.1 of "NFS
221 * Illustrated" by Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5.
222 */
223static __be32 *xdr_encode_current_server_time(__be32 *p,
224 const struct timespec *timep)
225{
226 *p++ = cpu_to_be32(timep->tv_sec);
227 *p++ = cpu_to_be32(1000000);
228 return p;
229}
230
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000231static __be32 *xdr_decode_time(__be32 *p, struct timespec *timep)
232{
233 timep->tv_sec = be32_to_cpup(p++);
234 timep->tv_nsec = be32_to_cpup(p++) * NSEC_PER_USEC;
235 return p;
236}
237
Chuck Lever282ac2a2010-12-14 14:54:50 +0000238/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000239 * 2.3.5. fattr
240 *
241 * struct fattr {
242 * ftype type;
243 * unsigned int mode;
244 * unsigned int nlink;
245 * unsigned int uid;
246 * unsigned int gid;
247 * unsigned int size;
248 * unsigned int blocksize;
249 * unsigned int rdev;
250 * unsigned int blocks;
251 * unsigned int fsid;
252 * unsigned int fileid;
253 * timeval atime;
254 * timeval mtime;
255 * timeval ctime;
256 * };
257 *
258 */
259static int decode_fattr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
260{
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000261 u32 rdev, type;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000262 __be32 *p;
263
264 p = xdr_inline_decode(xdr, NFS_fattr_sz << 2);
Chuck Levereb72f482019-02-11 11:24:21 -0500265 if (unlikely(!p))
266 return -EIO;
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000267
268 fattr->valid |= NFS_ATTR_FATTR_V2;
269
270 p = xdr_decode_ftype(p, &type);
271
272 fattr->mode = be32_to_cpup(p++);
273 fattr->nlink = be32_to_cpup(p++);
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800274 fattr->uid = make_kuid(&init_user_ns, be32_to_cpup(p++));
275 if (!uid_valid(fattr->uid))
276 goto out_uid;
277 fattr->gid = make_kgid(&init_user_ns, be32_to_cpup(p++));
278 if (!gid_valid(fattr->gid))
279 goto out_gid;
280
Chuck Lever5f96e5e2010-12-14 14:55:30 +0000281 fattr->size = be32_to_cpup(p++);
282 fattr->du.nfs2.blocksize = be32_to_cpup(p++);
283
284 rdev = be32_to_cpup(p++);
285 fattr->rdev = new_decode_dev(rdev);
286 if (type == (u32)NFCHR && rdev == (u32)NFS2_FIFO_DEV) {
287 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
288 fattr->rdev = 0;
289 }
290
291 fattr->du.nfs2.blocks = be32_to_cpup(p++);
292 fattr->fsid.major = be32_to_cpup(p++);
293 fattr->fsid.minor = 0;
294 fattr->fileid = be32_to_cpup(p++);
295
296 p = xdr_decode_time(p, &fattr->atime);
297 p = xdr_decode_time(p, &fattr->mtime);
298 xdr_decode_time(p, &fattr->ctime);
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400299 fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
300
Chuck Leverf796f8b2010-12-14 14:55:10 +0000301 return 0;
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800302out_uid:
303 dprintk("NFS: returned invalid uid\n");
304 return -EINVAL;
305out_gid:
306 dprintk("NFS: returned invalid gid\n");
307 return -EINVAL;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000308}
309
310/*
Chuck Lever25a08662010-12-14 14:54:30 +0000311 * 2.3.6. sattr
312 *
313 * struct sattr {
314 * unsigned int mode;
315 * unsigned int uid;
316 * unsigned int gid;
317 * unsigned int size;
318 * timeval atime;
319 * timeval mtime;
320 * };
321 */
322
323#define NFS2_SATTR_NOT_SET (0xffffffff)
324
325static __be32 *xdr_time_not_set(__be32 *p)
326{
327 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
328 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
329 return p;
330}
331
332static void encode_sattr(struct xdr_stream *xdr, const struct iattr *attr)
333{
Deepa Dinamani95582b02018-05-08 19:36:02 -0700334 struct timespec ts;
Chuck Lever25a08662010-12-14 14:54:30 +0000335 __be32 *p;
336
337 p = xdr_reserve_space(xdr, NFS_sattr_sz << 2);
338
339 if (attr->ia_valid & ATTR_MODE)
340 *p++ = cpu_to_be32(attr->ia_mode);
341 else
342 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
343 if (attr->ia_valid & ATTR_UID)
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800344 *p++ = cpu_to_be32(from_kuid(&init_user_ns, attr->ia_uid));
Chuck Lever25a08662010-12-14 14:54:30 +0000345 else
346 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
347 if (attr->ia_valid & ATTR_GID)
Eric W. Biedermancfa08982013-02-01 03:45:54 -0800348 *p++ = cpu_to_be32(from_kgid(&init_user_ns, attr->ia_gid));
Chuck Lever25a08662010-12-14 14:54:30 +0000349 else
350 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
351 if (attr->ia_valid & ATTR_SIZE)
352 *p++ = cpu_to_be32((u32)attr->ia_size);
353 else
354 *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
355
Deepa Dinamani95582b02018-05-08 19:36:02 -0700356 if (attr->ia_valid & ATTR_ATIME_SET) {
357 ts = timespec64_to_timespec(attr->ia_atime);
358 p = xdr_encode_time(p, &ts);
359 } else if (attr->ia_valid & ATTR_ATIME) {
360 ts = timespec64_to_timespec(attr->ia_atime);
361 p = xdr_encode_current_server_time(p, &ts);
362 } else
Chuck Lever25a08662010-12-14 14:54:30 +0000363 p = xdr_time_not_set(p);
Deepa Dinamani95582b02018-05-08 19:36:02 -0700364 if (attr->ia_valid & ATTR_MTIME_SET) {
365 ts = timespec64_to_timespec(attr->ia_atime);
366 xdr_encode_time(p, &ts);
367 } else if (attr->ia_valid & ATTR_MTIME) {
368 ts = timespec64_to_timespec(attr->ia_mtime);
369 xdr_encode_current_server_time(p, &ts);
370 } else
Chuck Lever25a08662010-12-14 14:54:30 +0000371 xdr_time_not_set(p);
372}
373
374/*
375 * 2.3.7. filename
376 *
377 * typedef string filename<MAXNAMLEN>;
378 */
379static void encode_filename(struct xdr_stream *xdr,
380 const char *name, u32 length)
381{
382 __be32 *p;
383
Trond Myklebust7fc38842012-10-15 11:51:21 -0400384 WARN_ON_ONCE(length > NFS2_MAXNAMLEN);
Chuck Lever25a08662010-12-14 14:54:30 +0000385 p = xdr_reserve_space(xdr, 4 + length);
386 xdr_encode_opaque(p, name, length);
387}
388
Chuck Leverf796f8b2010-12-14 14:55:10 +0000389static int decode_filename_inline(struct xdr_stream *xdr,
390 const char **name, u32 *length)
391{
392 __be32 *p;
393 u32 count;
394
395 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500396 if (unlikely(!p))
397 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000398 count = be32_to_cpup(p);
399 if (count > NFS3_MAXNAMLEN)
400 goto out_nametoolong;
401 p = xdr_inline_decode(xdr, count);
Chuck Levereb72f482019-02-11 11:24:21 -0500402 if (unlikely(!p))
403 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000404 *name = (const char *)p;
405 *length = count;
406 return 0;
407out_nametoolong:
408 dprintk("NFS: returned filename too long: %u\n", count);
409 return -ENAMETOOLONG;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000410}
411
Chuck Lever25a08662010-12-14 14:54:30 +0000412/*
413 * 2.3.8. path
414 *
415 * typedef string path<MAXPATHLEN>;
416 */
417static void encode_path(struct xdr_stream *xdr, struct page **pages, u32 length)
418{
419 __be32 *p;
420
Chuck Lever25a08662010-12-14 14:54:30 +0000421 p = xdr_reserve_space(xdr, 4);
422 *p = cpu_to_be32(length);
423 xdr_write_pages(xdr, pages, 0, length);
424}
425
Chuck Leverf796f8b2010-12-14 14:55:10 +0000426static int decode_path(struct xdr_stream *xdr)
427{
428 u32 length, recvd;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000429 __be32 *p;
430
431 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500432 if (unlikely(!p))
433 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000434 length = be32_to_cpup(p);
435 if (unlikely(length >= xdr->buf->page_len || length > NFS_MAXPATHLEN))
436 goto out_size;
Trond Myklebust64bd5772012-06-20 22:35:05 -0400437 recvd = xdr_read_pages(xdr, length);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000438 if (unlikely(length > recvd))
439 goto out_cheating;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000440 xdr_terminate_string(xdr->buf, length);
441 return 0;
442out_size:
443 dprintk("NFS: returned pathname too long: %u\n", length);
444 return -ENAMETOOLONG;
445out_cheating:
446 dprintk("NFS: server cheating in pathname result: "
447 "length %u > received %u\n", length, recvd);
448 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000449}
450
451/*
452 * 2.3.9. attrstat
453 *
454 * union attrstat switch (stat status) {
455 * case NFS_OK:
456 * fattr attributes;
457 * default:
458 * void;
459 * };
460 */
Peng Taoaabff4d2014-08-27 10:47:14 +0800461static int decode_attrstat(struct xdr_stream *xdr, struct nfs_fattr *result,
462 __u32 *op_status)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000463{
464 enum nfs_stat status;
465 int error;
466
467 error = decode_stat(xdr, &status);
468 if (unlikely(error))
469 goto out;
Peng Taoaabff4d2014-08-27 10:47:14 +0800470 if (op_status)
471 *op_status = status;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000472 if (status != NFS_OK)
473 goto out_default;
474 error = decode_fattr(xdr, result);
475out:
476 return error;
477out_default:
478 return nfs_stat_to_errno(status);
479}
480
Chuck Lever25a08662010-12-14 14:54:30 +0000481/*
482 * 2.3.10. diropargs
483 *
484 * struct diropargs {
485 * fhandle dir;
486 * filename name;
487 * };
488 */
489static void encode_diropargs(struct xdr_stream *xdr, const struct nfs_fh *fh,
490 const char *name, u32 length)
491{
492 encode_fhandle(xdr, fh);
493 encode_filename(xdr, name, length);
494}
495
Chuck Leverf796f8b2010-12-14 14:55:10 +0000496/*
497 * 2.3.11. diropres
498 *
499 * union diropres switch (stat status) {
500 * case NFS_OK:
501 * struct {
502 * fhandle file;
503 * fattr attributes;
504 * } diropok;
505 * default:
506 * void;
507 * };
508 */
509static int decode_diropok(struct xdr_stream *xdr, struct nfs_diropok *result)
510{
511 int error;
512
513 error = decode_fhandle(xdr, result->fh);
514 if (unlikely(error))
515 goto out;
516 error = decode_fattr(xdr, result->fattr);
517out:
518 return error;
519}
520
521static int decode_diropres(struct xdr_stream *xdr, struct nfs_diropok *result)
522{
523 enum nfs_stat status;
524 int error;
525
526 error = decode_stat(xdr, &status);
527 if (unlikely(error))
528 goto out;
529 if (status != NFS_OK)
530 goto out_default;
531 error = decode_diropok(xdr, result);
532out:
533 return error;
534out_default:
535 return nfs_stat_to_errno(status);
536}
537
Chuck Lever25a08662010-12-14 14:54:30 +0000538
539/*
Chuck Lever2d70f532010-12-14 14:54:40 +0000540 * NFSv2 XDR encode functions
541 *
542 * NFSv2 argument types are defined in section 2.2 of RFC 1094:
543 * "NFS: Network File System Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Chuck Lever9f06c712010-12-14 14:59:18 +0000546static void nfs2_xdr_enc_fhandle(struct rpc_rqst *req,
547 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200548 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000549{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200550 const struct nfs_fh *fh = data;
551
Chuck Lever9f06c712010-12-14 14:59:18 +0000552 encode_fhandle(xdr, fh);
Chuck Lever25a08662010-12-14 14:54:30 +0000553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/*
Chuck Lever25a08662010-12-14 14:54:30 +0000556 * 2.2.3. sattrargs
557 *
558 * struct sattrargs {
559 * fhandle file;
560 * sattr attributes;
561 * };
562 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000563static void nfs2_xdr_enc_sattrargs(struct rpc_rqst *req,
564 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200565 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000566{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200567 const struct nfs_sattrargs *args = data;
568
Chuck Lever9f06c712010-12-14 14:59:18 +0000569 encode_fhandle(xdr, args->fh);
570 encode_sattr(xdr, args->sattr);
Chuck Lever25a08662010-12-14 14:54:30 +0000571}
572
Chuck Lever9f06c712010-12-14 14:59:18 +0000573static void nfs2_xdr_enc_diropargs(struct rpc_rqst *req,
574 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200575 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000576{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200577 const struct nfs_diropargs *args = data;
578
Chuck Lever9f06c712010-12-14 14:59:18 +0000579 encode_diropargs(xdr, args->fh, args->name, args->len);
Chuck Lever25a08662010-12-14 14:54:30 +0000580}
581
Chuck Lever9f06c712010-12-14 14:59:18 +0000582static void nfs2_xdr_enc_readlinkargs(struct rpc_rqst *req,
583 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200584 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000585{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200586 const struct nfs_readlinkargs *args = data;
587
Chuck Lever9f06c712010-12-14 14:59:18 +0000588 encode_fhandle(xdr, args->fh);
Chuck Lever25a08662010-12-14 14:54:30 +0000589 prepare_reply_buffer(req, args->pages, args->pgbase,
590 args->pglen, NFS_readlinkres_sz);
Chuck Lever25a08662010-12-14 14:54:30 +0000591}
592
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400593/*
Chuck Lever25a08662010-12-14 14:54:30 +0000594 * 2.2.7. readargs
595 *
596 * struct readargs {
597 * fhandle file;
598 * unsigned offset;
599 * unsigned count;
600 * unsigned totalcount;
601 * };
602 */
603static void encode_readargs(struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400604 const struct nfs_pgio_args *args)
Chuck Lever25a08662010-12-14 14:54:30 +0000605{
606 u32 offset = args->offset;
607 u32 count = args->count;
608 __be32 *p;
609
610 encode_fhandle(xdr, args->fh);
611
612 p = xdr_reserve_space(xdr, 4 + 4 + 4);
613 *p++ = cpu_to_be32(offset);
614 *p++ = cpu_to_be32(count);
615 *p = cpu_to_be32(count);
616}
617
Chuck Lever9f06c712010-12-14 14:59:18 +0000618static void nfs2_xdr_enc_readargs(struct rpc_rqst *req,
619 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200620 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000621{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200622 const struct nfs_pgio_args *args = data;
623
Chuck Lever9f06c712010-12-14 14:59:18 +0000624 encode_readargs(xdr, args);
Chuck Lever25a08662010-12-14 14:54:30 +0000625 prepare_reply_buffer(req, args->pages, args->pgbase,
626 args->count, NFS_readres_sz);
627 req->rq_rcv_buf.flags |= XDRBUF_READ;
Chuck Lever25a08662010-12-14 14:54:30 +0000628}
629
630/*
Chuck Lever25a08662010-12-14 14:54:30 +0000631 * 2.2.9. writeargs
632 *
633 * struct writeargs {
634 * fhandle file;
635 * unsigned beginoffset;
636 * unsigned offset;
637 * unsigned totalcount;
638 * nfsdata data;
639 * };
640 */
641static void encode_writeargs(struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400642 const struct nfs_pgio_args *args)
Chuck Lever25a08662010-12-14 14:54:30 +0000643{
644 u32 offset = args->offset;
645 u32 count = args->count;
646 __be32 *p;
647
648 encode_fhandle(xdr, args->fh);
649
650 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
651 *p++ = cpu_to_be32(offset);
652 *p++ = cpu_to_be32(offset);
653 *p++ = cpu_to_be32(count);
654
655 /* nfsdata */
656 *p = cpu_to_be32(count);
657 xdr_write_pages(xdr, args->pages, args->pgbase, count);
658}
659
Chuck Lever9f06c712010-12-14 14:59:18 +0000660static void nfs2_xdr_enc_writeargs(struct rpc_rqst *req,
661 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200662 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000663{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200664 const struct nfs_pgio_args *args = data;
665
Chuck Lever9f06c712010-12-14 14:59:18 +0000666 encode_writeargs(xdr, args);
667 xdr->buf->flags |= XDRBUF_WRITE;
Chuck Lever25a08662010-12-14 14:54:30 +0000668}
669
670/*
Chuck Lever25a08662010-12-14 14:54:30 +0000671 * 2.2.10. createargs
672 *
673 * struct createargs {
674 * diropargs where;
675 * sattr attributes;
676 * };
677 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000678static void nfs2_xdr_enc_createargs(struct rpc_rqst *req,
679 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200680 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000681{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200682 const struct nfs_createargs *args = data;
683
Chuck Lever9f06c712010-12-14 14:59:18 +0000684 encode_diropargs(xdr, args->fh, args->name, args->len);
685 encode_sattr(xdr, args->sattr);
Chuck Lever25a08662010-12-14 14:54:30 +0000686}
687
Chuck Lever9f06c712010-12-14 14:59:18 +0000688static void nfs2_xdr_enc_removeargs(struct rpc_rqst *req,
689 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200690 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000691{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200692 const struct nfs_removeargs *args = data;
693
Chuck Lever9f06c712010-12-14 14:59:18 +0000694 encode_diropargs(xdr, args->fh, args->name.name, args->name.len);
Chuck Lever25a08662010-12-14 14:54:30 +0000695}
696
697/*
Chuck Lever25a08662010-12-14 14:54:30 +0000698 * 2.2.12. renameargs
699 *
700 * struct renameargs {
701 * diropargs from;
702 * diropargs to;
703 * };
704 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000705static void nfs2_xdr_enc_renameargs(struct rpc_rqst *req,
706 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200707 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000708{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200709 const struct nfs_renameargs *args = data;
Chuck Lever25a08662010-12-14 14:54:30 +0000710 const struct qstr *old = args->old_name;
711 const struct qstr *new = args->new_name;
Chuck Lever25a08662010-12-14 14:54:30 +0000712
Chuck Lever9f06c712010-12-14 14:59:18 +0000713 encode_diropargs(xdr, args->old_dir, old->name, old->len);
714 encode_diropargs(xdr, args->new_dir, new->name, new->len);
Chuck Lever25a08662010-12-14 14:54:30 +0000715}
716
717/*
Chuck Lever25a08662010-12-14 14:54:30 +0000718 * 2.2.13. linkargs
719 *
720 * struct linkargs {
721 * fhandle from;
722 * diropargs to;
723 * };
724 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000725static void nfs2_xdr_enc_linkargs(struct rpc_rqst *req,
726 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200727 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000728{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200729 const struct nfs_linkargs *args = data;
730
Chuck Lever9f06c712010-12-14 14:59:18 +0000731 encode_fhandle(xdr, args->fromfh);
732 encode_diropargs(xdr, args->tofh, args->toname, args->tolen);
Chuck Lever25a08662010-12-14 14:54:30 +0000733}
734
735/*
Chuck Lever25a08662010-12-14 14:54:30 +0000736 * 2.2.14. symlinkargs
737 *
738 * struct symlinkargs {
739 * diropargs from;
740 * path to;
741 * sattr attributes;
742 * };
743 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000744static void nfs2_xdr_enc_symlinkargs(struct rpc_rqst *req,
745 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200746 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000747{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200748 const struct nfs_symlinkargs *args = data;
749
Chuck Lever9f06c712010-12-14 14:59:18 +0000750 encode_diropargs(xdr, args->fromfh, args->fromname, args->fromlen);
751 encode_path(xdr, args->pages, args->pathlen);
752 encode_sattr(xdr, args->sattr);
Chuck Lever25a08662010-12-14 14:54:30 +0000753}
754
755/*
Chuck Lever25a08662010-12-14 14:54:30 +0000756 * 2.2.17. readdirargs
757 *
758 * struct readdirargs {
759 * fhandle dir;
760 * nfscookie cookie;
761 * unsigned count;
762 * };
763 */
764static void encode_readdirargs(struct xdr_stream *xdr,
765 const struct nfs_readdirargs *args)
766{
767 __be32 *p;
768
769 encode_fhandle(xdr, args->fh);
770
771 p = xdr_reserve_space(xdr, 4 + 4);
772 *p++ = cpu_to_be32(args->cookie);
773 *p = cpu_to_be32(args->count);
774}
775
Chuck Lever9f06c712010-12-14 14:59:18 +0000776static void nfs2_xdr_enc_readdirargs(struct rpc_rqst *req,
777 struct xdr_stream *xdr,
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200778 const void *data)
Chuck Lever25a08662010-12-14 14:54:30 +0000779{
Christoph Hellwigfcc85812017-05-08 10:01:49 +0200780 const struct nfs_readdirargs *args = data;
781
Chuck Lever9f06c712010-12-14 14:59:18 +0000782 encode_readdirargs(xdr, args);
Chuck Lever25a08662010-12-14 14:54:30 +0000783 prepare_reply_buffer(req, args->pages, 0,
784 args->count, NFS_readdirres_sz);
Chuck Lever25a08662010-12-14 14:54:30 +0000785}
786
787/*
Chuck Lever661ad422010-12-14 14:55:20 +0000788 * NFSv2 XDR decode functions
789 *
790 * NFSv2 result types are defined in section 2.2 of RFC 1094:
791 * "NFS: Network File System Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Chuck Leverbf269552010-12-14 14:59:29 +0000794static int nfs2_xdr_dec_stat(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Leverf796f8b2010-12-14 14:55:10 +0000795 void *__unused)
796{
Chuck Leverf796f8b2010-12-14 14:55:10 +0000797 enum nfs_stat status;
798 int error;
799
Chuck Leverbf269552010-12-14 14:59:29 +0000800 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000801 if (unlikely(error))
802 goto out;
803 if (status != NFS_OK)
804 goto out_default;
805out:
806 return error;
807out_default:
808 return nfs_stat_to_errno(status);
809}
810
Chuck Leverbf269552010-12-14 14:59:29 +0000811static int nfs2_xdr_dec_attrstat(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200812 void *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000813{
Peng Taoaabff4d2014-08-27 10:47:14 +0800814 return decode_attrstat(xdr, result, NULL);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000815}
816
Chuck Leverbf269552010-12-14 14:59:29 +0000817static int nfs2_xdr_dec_diropres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200818 void *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000819{
Chuck Leverbf269552010-12-14 14:59:29 +0000820 return decode_diropres(xdr, result);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/*
Chuck Leverf796f8b2010-12-14 14:55:10 +0000824 * 2.2.6. readlinkres
825 *
826 * union readlinkres switch (stat status) {
827 * case NFS_OK:
828 * path data;
829 * default:
830 * void;
831 * };
832 */
Chuck Leverbf269552010-12-14 14:59:29 +0000833static int nfs2_xdr_dec_readlinkres(struct rpc_rqst *req,
834 struct xdr_stream *xdr, void *__unused)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000835{
Chuck Leverf796f8b2010-12-14 14:55:10 +0000836 enum nfs_stat status;
837 int error;
838
Chuck Leverbf269552010-12-14 14:59:29 +0000839 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000840 if (unlikely(error))
841 goto out;
842 if (status != NFS_OK)
843 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +0000844 error = decode_path(xdr);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000845out:
846 return error;
847out_default:
848 return nfs_stat_to_errno(status);
849}
850
851/*
852 * 2.2.7. readres
853 *
854 * union readres switch (stat status) {
855 * case NFS_OK:
856 * fattr attributes;
857 * nfsdata data;
858 * default:
859 * void;
860 * };
861 */
Chuck Leverbf269552010-12-14 14:59:29 +0000862static int nfs2_xdr_dec_readres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200863 void *data)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000864{
Christoph Hellwigfc016482017-05-08 15:09:02 +0200865 struct nfs_pgio_res *result = data;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000866 enum nfs_stat status;
867 int error;
868
Chuck Leverbf269552010-12-14 14:59:29 +0000869 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000870 if (unlikely(error))
871 goto out;
Peng Taoaabff4d2014-08-27 10:47:14 +0800872 result->op_status = status;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000873 if (status != NFS_OK)
874 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +0000875 error = decode_fattr(xdr, result->fattr);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000876 if (unlikely(error))
877 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +0000878 error = decode_nfsdata(xdr, result);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000879out:
880 return error;
881out_default:
882 return nfs_stat_to_errno(status);
883}
884
Chuck Leverbf269552010-12-14 14:59:29 +0000885static int nfs2_xdr_dec_writeres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +0200886 void *data)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000887{
Christoph Hellwigfc016482017-05-08 15:09:02 +0200888 struct nfs_pgio_res *result = data;
889
Chuck Leverf796f8b2010-12-14 14:55:10 +0000890 /* All NFSv2 writes are "file sync" writes */
891 result->verf->committed = NFS_FILE_SYNC;
Peng Taoaabff4d2014-08-27 10:47:14 +0800892 return decode_attrstat(xdr, result->fattr, &result->op_status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000893}
894
895/**
896 * nfs2_decode_dirent - Decode a single NFSv2 directory entry stored in
897 * the local page cache.
898 * @xdr: XDR stream where entry resides
899 * @entry: buffer to fill in with entry data
Chuck Leverf796f8b2010-12-14 14:55:10 +0000900 * @plus: boolean indicating whether this should be a readdirplus entry
901 *
Chuck Lever573c4e12010-12-14 14:58:11 +0000902 * Returns zero if successful, otherwise a negative errno value is
903 * returned.
Chuck Leverf796f8b2010-12-14 14:55:10 +0000904 *
905 * This function is not invoked during READDIR reply decoding, but
906 * rather whenever an application invokes the getdents(2) system call
907 * on a directory already in our cache.
908 *
909 * 2.2.17. entry
910 *
911 * struct entry {
912 * unsigned fileid;
913 * filename name;
914 * nfscookie cookie;
915 * entry *nextentry;
916 * };
917 */
Chuck Lever573c4e12010-12-14 14:58:11 +0000918int nfs2_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400919 bool plus)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000920{
921 __be32 *p;
922 int error;
923
924 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500925 if (unlikely(!p))
926 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000927 if (*p++ == xdr_zero) {
928 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500929 if (unlikely(!p))
930 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000931 if (*p++ == xdr_zero)
Chuck Lever573c4e12010-12-14 14:58:11 +0000932 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000933 entry->eof = 1;
Chuck Lever573c4e12010-12-14 14:58:11 +0000934 return -EBADCOOKIE;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000935 }
936
937 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500938 if (unlikely(!p))
939 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000940 entry->ino = be32_to_cpup(p);
941
942 error = decode_filename_inline(xdr, &entry->name, &entry->len);
943 if (unlikely(error))
Chuck Lever573c4e12010-12-14 14:58:11 +0000944 return error;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000945
946 /*
947 * The type (size and byte order) of nfscookie isn't defined in
948 * RFC 1094. This implementation assumes that it's an XDR uint32.
949 */
950 entry->prev_cookie = entry->cookie;
951 p = xdr_inline_decode(xdr, 4);
Chuck Levereb72f482019-02-11 11:24:21 -0500952 if (unlikely(!p))
953 return -EAGAIN;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000954 entry->cookie = be32_to_cpup(p);
955
956 entry->d_type = DT_UNKNOWN;
957
Chuck Lever573c4e12010-12-14 14:58:11 +0000958 return 0;
Chuck Leverf796f8b2010-12-14 14:55:10 +0000959}
960
961/*
962 * 2.2.17. readdirres
963 *
964 * union readdirres switch (stat status) {
965 * case NFS_OK:
966 * struct {
967 * entry *entries;
968 * bool eof;
969 * } readdirok;
970 * default:
971 * void;
972 * };
973 *
974 * Read the directory contents into the page cache, but don't
975 * touch them. The actual decoding is done by nfs2_decode_dirent()
976 * during subsequent nfs_readdir() calls.
977 */
978static int decode_readdirok(struct xdr_stream *xdr)
979{
Trond Myklebust64bd5772012-06-20 22:35:05 -0400980 return xdr_read_pages(xdr, xdr->buf->page_len);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000981}
982
Chuck Leverbf269552010-12-14 14:59:29 +0000983static int nfs2_xdr_dec_readdirres(struct rpc_rqst *req,
984 struct xdr_stream *xdr, void *__unused)
Chuck Leverf796f8b2010-12-14 14:55:10 +0000985{
Chuck Leverf796f8b2010-12-14 14:55:10 +0000986 enum nfs_stat status;
987 int error;
988
Chuck Leverbf269552010-12-14 14:59:29 +0000989 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000990 if (unlikely(error))
991 goto out;
992 if (status != NFS_OK)
993 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +0000994 error = decode_readdirok(xdr);
Chuck Leverf796f8b2010-12-14 14:55:10 +0000995out:
996 return error;
997out_default:
998 return nfs_stat_to_errno(status);
999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/*
Chuck Leverf796f8b2010-12-14 14:55:10 +00001002 * 2.2.18. statfsres
1003 *
1004 * union statfsres (stat status) {
1005 * case NFS_OK:
1006 * struct {
1007 * unsigned tsize;
1008 * unsigned bsize;
1009 * unsigned blocks;
1010 * unsigned bfree;
1011 * unsigned bavail;
1012 * } info;
1013 * default:
1014 * void;
1015 * };
1016 */
1017static int decode_info(struct xdr_stream *xdr, struct nfs2_fsstat *result)
1018{
1019 __be32 *p;
1020
1021 p = xdr_inline_decode(xdr, NFS_info_sz << 2);
Chuck Levereb72f482019-02-11 11:24:21 -05001022 if (unlikely(!p))
1023 return -EIO;
Chuck Leverf796f8b2010-12-14 14:55:10 +00001024 result->tsize = be32_to_cpup(p++);
1025 result->bsize = be32_to_cpup(p++);
1026 result->blocks = be32_to_cpup(p++);
1027 result->bfree = be32_to_cpup(p++);
1028 result->bavail = be32_to_cpup(p);
1029 return 0;
Chuck Leverf796f8b2010-12-14 14:55:10 +00001030}
1031
Chuck Leverbf269552010-12-14 14:59:29 +00001032static int nfs2_xdr_dec_statfsres(struct rpc_rqst *req, struct xdr_stream *xdr,
Christoph Hellwigfc016482017-05-08 15:09:02 +02001033 void *result)
Chuck Leverf796f8b2010-12-14 14:55:10 +00001034{
Chuck Leverf796f8b2010-12-14 14:55:10 +00001035 enum nfs_stat status;
1036 int error;
1037
Chuck Leverbf269552010-12-14 14:59:29 +00001038 error = decode_stat(xdr, &status);
Chuck Leverf796f8b2010-12-14 14:55:10 +00001039 if (unlikely(error))
1040 goto out;
1041 if (status != NFS_OK)
1042 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001043 error = decode_info(xdr, result);
Chuck Leverf796f8b2010-12-14 14:55:10 +00001044out:
1045 return error;
1046out_default:
1047 return nfs_stat_to_errno(status);
1048}
1049
1050
1051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * We need to translate between nfs status return values and
1053 * the local errno values which may not be the same.
1054 */
Chuck Lever85828492010-12-14 14:55:00 +00001055static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int stat;
1057 int errno;
1058} nfs_errtbl[] = {
1059 { NFS_OK, 0 },
Benny Halevy856dff32008-03-31 17:39:06 +03001060 { NFSERR_PERM, -EPERM },
1061 { NFSERR_NOENT, -ENOENT },
1062 { NFSERR_IO, -errno_NFSERR_IO},
1063 { NFSERR_NXIO, -ENXIO },
1064/* { NFSERR_EAGAIN, -EAGAIN }, */
1065 { NFSERR_ACCES, -EACCES },
1066 { NFSERR_EXIST, -EEXIST },
1067 { NFSERR_XDEV, -EXDEV },
1068 { NFSERR_NODEV, -ENODEV },
1069 { NFSERR_NOTDIR, -ENOTDIR },
1070 { NFSERR_ISDIR, -EISDIR },
1071 { NFSERR_INVAL, -EINVAL },
1072 { NFSERR_FBIG, -EFBIG },
1073 { NFSERR_NOSPC, -ENOSPC },
1074 { NFSERR_ROFS, -EROFS },
1075 { NFSERR_MLINK, -EMLINK },
1076 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
1077 { NFSERR_NOTEMPTY, -ENOTEMPTY },
1078 { NFSERR_DQUOT, -EDQUOT },
1079 { NFSERR_STALE, -ESTALE },
1080 { NFSERR_REMOTE, -EREMOTE },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081#ifdef EWFLUSH
Benny Halevy856dff32008-03-31 17:39:06 +03001082 { NFSERR_WFLUSH, -EWFLUSH },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#endif
Benny Halevy856dff32008-03-31 17:39:06 +03001084 { NFSERR_BADHANDLE, -EBADHANDLE },
1085 { NFSERR_NOT_SYNC, -ENOTSYNC },
1086 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
1087 { NFSERR_NOTSUPP, -ENOTSUPP },
1088 { NFSERR_TOOSMALL, -ETOOSMALL },
Trond Myklebustfdcb4572010-02-08 09:32:40 -05001089 { NFSERR_SERVERFAULT, -EREMOTEIO },
Benny Halevy856dff32008-03-31 17:39:06 +03001090 { NFSERR_BADTYPE, -EBADTYPE },
1091 { NFSERR_JUKEBOX, -EJUKEBOX },
1092 { -1, -EIO }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093};
1094
Chuck Lever85828492010-12-14 14:55:00 +00001095/**
1096 * nfs_stat_to_errno - convert an NFS status code to a local errno
1097 * @status: NFS status code to convert
1098 *
1099 * Returns a local errno value, or -EIO if the NFS status code is
1100 * not recognized. This function is used jointly by NFSv2 and NFSv3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001102static int nfs_stat_to_errno(enum nfs_stat status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 int i;
1105
1106 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
Chuck Lever85828492010-12-14 14:55:00 +00001107 if (nfs_errtbl[i].stat == (int)status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return nfs_errtbl[i].errno;
1109 }
Chuck Lever85828492010-12-14 14:55:00 +00001110 dprintk("NFS: Unrecognized nfs status value: %u\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return nfs_errtbl[i].errno;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114#define PROC(proc, argtype, restype, timer) \
1115[NFSPROC_##proc] = { \
1116 .p_proc = NFSPROC_##proc, \
Christoph Hellwigfcc85812017-05-08 10:01:49 +02001117 .p_encode = nfs2_xdr_enc_##argtype, \
Christoph Hellwigfc016482017-05-08 15:09:02 +02001118 .p_decode = nfs2_xdr_dec_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -04001119 .p_arglen = NFS_##argtype##_sz, \
1120 .p_replen = NFS_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05001121 .p_timer = timer, \
1122 .p_statidx = NFSPROC_##proc, \
1123 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Christoph Hellwig511e9362017-05-12 15:36:49 +02001125const struct rpc_procinfo nfs_procedures[] = {
Chuck Lever7d93bd712010-12-14 14:57:42 +00001126 PROC(GETATTR, fhandle, attrstat, 1),
1127 PROC(SETATTR, sattrargs, attrstat, 0),
1128 PROC(LOOKUP, diropargs, diropres, 2),
1129 PROC(READLINK, readlinkargs, readlinkres, 3),
1130 PROC(READ, readargs, readres, 3),
1131 PROC(WRITE, writeargs, writeres, 4),
1132 PROC(CREATE, createargs, diropres, 0),
1133 PROC(REMOVE, removeargs, stat, 0),
1134 PROC(RENAME, renameargs, stat, 0),
1135 PROC(LINK, linkargs, stat, 0),
1136 PROC(SYMLINK, symlinkargs, stat, 0),
1137 PROC(MKDIR, createargs, diropres, 0),
1138 PROC(RMDIR, diropargs, stat, 0),
1139 PROC(READDIR, readdirargs, readdirres, 3),
1140 PROC(STATFS, fhandle, statfsres, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141};
1142
Christoph Hellwigc5518582017-05-08 23:27:10 +02001143static unsigned int nfs_version2_counts[ARRAY_SIZE(nfs_procedures)];
Trond Myklebusta613fa12012-01-20 13:53:56 -05001144const struct rpc_version nfs_version2 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 .number = 2,
Tobias Klausere8c96f82006-03-24 03:15:34 -08001146 .nrprocs = ARRAY_SIZE(nfs_procedures),
Christoph Hellwigc5518582017-05-08 23:27:10 +02001147 .procs = nfs_procedures,
1148 .counts = nfs_version2_counts,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149};