blob: f917fd25858af81a4edaf712fffa3480ee3a8e6b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfsd/nfs3xdr.c
3 *
4 * XDR support for nfsd/protocol version 3.
5 *
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7 *
8 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
9 */
10
11#include <linux/types.h>
12#include <linux/time.h>
13#include <linux/nfs3.h>
14#include <linux/list.h>
15#include <linux/spinlock.h>
16#include <linux/dcache.h>
17#include <linux/namei.h>
18#include <linux/mm.h>
19#include <linux/vfs.h>
20#include <linux/sunrpc/xdr.h>
21#include <linux/sunrpc/svc.h>
22#include <linux/nfsd/nfsd.h>
23#include <linux/nfsd/xdr3.h>
24
25#define NFSDDBG_FACILITY NFSDDBG_XDR
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Mapping of S_IF* types to NFS file types
30 */
31static u32 nfs3_ftypes[] = {
32 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
33 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
34 NF3REG, NF3BAD, NF3LNK, NF3BAD,
35 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
36};
37
38/*
39 * XDR functions for basic NFS types
40 */
Adrian Bunk3ee6f612006-12-06 20:40:23 -080041static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070042encode_time3(__be32 *p, struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
45 return p;
46}
47
Adrian Bunk3ee6f612006-12-06 20:40:23 -080048static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070049decode_time3(__be32 *p, struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 time->tv_sec = ntohl(*p++);
52 time->tv_nsec = ntohl(*p++);
53 return p;
54}
55
Adrian Bunk3ee6f612006-12-06 20:40:23 -080056static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070057decode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 unsigned int size;
60 fh_init(fhp, NFS3_FHSIZE);
61 size = ntohl(*p++);
62 if (size > NFS3_FHSIZE)
63 return NULL;
64
65 memcpy(&fhp->fh_handle.fh_base, p, size);
66 fhp->fh_handle.fh_size = size;
67 return p + XDR_QUADLEN(size);
68}
69
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000070/* Helper function for NFSv3 ACL code */
Al Viro91f07162006-10-19 23:28:57 -070071__be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000072{
73 return decode_fh(p, fhp);
74}
75
Adrian Bunk3ee6f612006-12-06 20:40:23 -080076static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070077encode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 unsigned int size = fhp->fh_handle.fh_size;
80 *p++ = htonl(size);
81 if (size) p[XDR_QUADLEN(size)-1]=0;
82 memcpy(p, &fhp->fh_handle.fh_base, size);
83 return p + XDR_QUADLEN(size);
84}
85
86/*
87 * Decode a file name and make sure that the path contains
88 * no slashes or null bytes.
89 */
Adrian Bunk3ee6f612006-12-06 20:40:23 -080090static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070091decode_filename(__be32 *p, char **namp, int *lenp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 char *name;
94 int i;
95
96 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
97 for (i = 0, name = *namp; i < *lenp; i++, name++) {
98 if (*name == '\0' || *name == '/')
99 return NULL;
100 }
101 }
102
103 return p;
104}
105
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800106static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700107decode_sattr3(__be32 *p, struct iattr *iap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 u32 tmp;
110
111 iap->ia_valid = 0;
112
113 if (*p++) {
114 iap->ia_valid |= ATTR_MODE;
115 iap->ia_mode = ntohl(*p++);
116 }
117 if (*p++) {
118 iap->ia_valid |= ATTR_UID;
119 iap->ia_uid = ntohl(*p++);
120 }
121 if (*p++) {
122 iap->ia_valid |= ATTR_GID;
123 iap->ia_gid = ntohl(*p++);
124 }
125 if (*p++) {
126 u64 newsize;
127
128 iap->ia_valid |= ATTR_SIZE;
129 p = xdr_decode_hyper(p, &newsize);
130 if (newsize <= NFS_OFFSET_MAX)
131 iap->ia_size = newsize;
132 else
133 iap->ia_size = NFS_OFFSET_MAX;
134 }
135 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
136 iap->ia_valid |= ATTR_ATIME;
137 } else if (tmp == 2) { /* set to client time */
138 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
139 iap->ia_atime.tv_sec = ntohl(*p++);
140 iap->ia_atime.tv_nsec = ntohl(*p++);
141 }
142 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
143 iap->ia_valid |= ATTR_MTIME;
144 } else if (tmp == 2) { /* set to client time */
145 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
146 iap->ia_mtime.tv_sec = ntohl(*p++);
147 iap->ia_mtime.tv_nsec = ntohl(*p++);
148 }
149 return p;
150}
151
NeilBrownaf6a4e22007-02-14 00:33:12 -0800152static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
153{
154 u64 f;
155 switch(fsid_source(fhp)) {
156 default:
157 case FSIDSOURCE_DEV:
158 p = xdr_encode_hyper(p, (u64)huge_encode_dev
159 (fhp->fh_dentry->d_inode->i_sb->s_dev));
160 break;
161 case FSIDSOURCE_FSID:
162 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
163 break;
164 case FSIDSOURCE_UUID:
165 f = ((u64*)fhp->fh_export->ex_uuid)[0];
166 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
167 p = xdr_encode_hyper(p, f);
168 break;
169 }
170 return p;
171}
172
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800173static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700174encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800175 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
David Shawa334de22006-01-06 00:19:58 -0800177 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
178 *p++ = htonl((u32) stat->mode);
179 *p++ = htonl((u32) stat->nlink);
180 *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
181 *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
182 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
184 } else {
David Shawa334de22006-01-06 00:19:58 -0800185 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
David Shawa334de22006-01-06 00:19:58 -0800187 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
188 *p++ = htonl((u32) MAJOR(stat->rdev));
189 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800190 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400191 p = xdr_encode_hyper(p, stat->ino);
David Shawa334de22006-01-06 00:19:58 -0800192 p = encode_time3(p, &stat->atime);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400193 p = encode_time3(p, &stat->mtime);
David Shawa334de22006-01-06 00:19:58 -0800194 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 return p;
197}
198
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800199static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700200encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Attributes to follow */
203 *p++ = xdr_one;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400204 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207/*
208 * Encode post-operation attributes.
209 * The inode may be NULL if the call failed because of a stale file
210 * handle. In this case, no attributes are returned.
211 */
Al Viro91f07162006-10-19 23:28:57 -0700212static __be32 *
213encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct dentry *dentry = fhp->fh_dentry;
NeilBrown072f62e2007-05-09 02:34:57 -0700216 if (dentry && dentry->d_inode) {
David Shawa334de22006-01-06 00:19:58 -0800217 int err;
218 struct kstat stat;
219
220 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
221 if (!err) {
222 *p++ = xdr_one; /* attributes follow */
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400223 lease_get_mtime(dentry->d_inode, &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800224 return encode_fattr3(rqstp, p, fhp, &stat);
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 *p++ = xdr_zero;
228 return p;
229}
230
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000231/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700232__be32 *
233nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000234{
235 return encode_post_op_attr(rqstp, p, fhp);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * Enocde weak cache consistency data
240 */
Al Viro91f07162006-10-19 23:28:57 -0700241static __be32 *
242encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct dentry *dentry = fhp->fh_dentry;
245
246 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
247 if (fhp->fh_pre_saved) {
248 *p++ = xdr_one;
249 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
250 p = encode_time3(p, &fhp->fh_pre_mtime);
251 p = encode_time3(p, &fhp->fh_pre_ctime);
252 } else {
253 *p++ = xdr_zero;
254 }
255 return encode_saved_post_attr(rqstp, p, fhp);
256 }
257 /* no pre- or post-attrs */
258 *p++ = xdr_zero;
259 return encode_post_op_attr(rqstp, p, fhp);
260}
261
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400262/*
263 * Fill in the post_op attr for the wcc data
264 */
265void fill_post_wcc(struct svc_fh *fhp)
266{
267 int err;
268
269 if (fhp->fh_post_saved)
270 printk("nfsd: inode locked twice during operation.\n");
271
272 err = vfs_getattr(fhp->fh_export->ex_mnt, fhp->fh_dentry,
273 &fhp->fh_post_attr);
274 if (err)
275 fhp->fh_post_saved = 0;
276 else
277 fhp->fh_post_saved = 1;
278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/*
281 * XDR decode functions
282 */
283int
Al Viro91f07162006-10-19 23:28:57 -0700284nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 if (!(p = decode_fh(p, &args->fh)))
287 return 0;
288 return xdr_argsize_check(rqstp, p);
289}
290
291int
Al Viro91f07162006-10-19 23:28:57 -0700292nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct nfsd3_sattrargs *args)
294{
NeilBrown072f62e2007-05-09 02:34:57 -0700295 if (!(p = decode_fh(p, &args->fh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700297 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 if ((args->check_guard = ntohl(*p++)) != 0) {
300 struct timespec time;
301 p = decode_time3(p, &time);
302 args->guardtime = time.tv_sec;
303 }
304
305 return xdr_argsize_check(rqstp, p);
306}
307
308int
Al Viro91f07162006-10-19 23:28:57 -0700309nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct nfsd3_diropargs *args)
311{
312 if (!(p = decode_fh(p, &args->fh))
313 || !(p = decode_filename(p, &args->name, &args->len)))
314 return 0;
315
316 return xdr_argsize_check(rqstp, p);
317}
318
319int
Al Viro91f07162006-10-19 23:28:57 -0700320nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct nfsd3_accessargs *args)
322{
323 if (!(p = decode_fh(p, &args->fh)))
324 return 0;
325 args->access = ntohl(*p++);
326
327 return xdr_argsize_check(rqstp, p);
328}
329
330int
Al Viro91f07162006-10-19 23:28:57 -0700331nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct nfsd3_readargs *args)
333{
334 unsigned int len;
335 int v,pn;
Greg Banks7adae482006-10-04 02:15:47 -0700336 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
NeilBrown072f62e2007-05-09 02:34:57 -0700338 if (!(p = decode_fh(p, &args->fh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700340 p = xdr_decode_hyper(p, &args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 len = args->count = ntohl(*p++);
343
Greg Banks7adae482006-10-04 02:15:47 -0700344 if (len > max_blocksize)
345 len = max_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* set up the kvec */
348 v=0;
349 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700350 pn = rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700351 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
352 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
353 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 v++;
355 }
356 args->vlen = v;
357 return xdr_argsize_check(rqstp, p);
358}
359
360int
Al Viro91f07162006-10-19 23:28:57 -0700361nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct nfsd3_writeargs *args)
363{
Peter Staubachf34b9562007-05-09 02:34:48 -0700364 unsigned int len, v, hdr, dlen;
Greg Banks7adae482006-10-04 02:15:47 -0700365 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
NeilBrown072f62e2007-05-09 02:34:57 -0700367 if (!(p = decode_fh(p, &args->fh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700369 p = xdr_decode_hyper(p, &args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 args->count = ntohl(*p++);
372 args->stable = ntohl(*p++);
373 len = args->len = ntohl(*p++);
Peter Staubachf34b9562007-05-09 02:34:48 -0700374 /*
375 * The count must equal the amount of data passed.
376 */
377 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379
Peter Staubachf34b9562007-05-09 02:34:48 -0700380 /*
381 * Check to make sure that we got the right number of
382 * bytes.
Peter Staubachf34b9562007-05-09 02:34:48 -0700383 */
384 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
NeilBrown072f62e2007-05-09 02:34:57 -0700385 dlen = rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len
386 - hdr;
Peter Staubachf34b9562007-05-09 02:34:48 -0700387 /*
388 * Round the length of the data which was specified up to
389 * the next multiple of XDR units and then compare that
390 * against the length which was actually received.
NeilBrownba67a392008-01-11 17:06:52 -0500391 * Note that when RPCSEC/GSS (for example) is used, the
392 * data buffer can be padded so dlen might be larger
393 * than required. It must never be smaller.
Peter Staubachf34b9562007-05-09 02:34:48 -0700394 */
NeilBrownba67a392008-01-11 17:06:52 -0500395 if (dlen < XDR_QUADLEN(len)*4)
Peter Staubachf34b9562007-05-09 02:34:48 -0700396 return 0;
397
398 if (args->count > max_blocksize) {
399 args->count = max_blocksize;
400 len = args->len = max_blocksize;
401 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700402 rqstp->rq_vec[0].iov_base = (void*)p;
403 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
Peter Staubachf34b9562007-05-09 02:34:48 -0700404 v = 0;
NeilBrown3cc03b12006-10-04 02:15:47 -0700405 while (len > rqstp->rq_vec[v].iov_len) {
406 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700408 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
409 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700411 rqstp->rq_vec[v].iov_len = len;
Peter Staubachf34b9562007-05-09 02:34:48 -0700412 args->vlen = v + 1;
413 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416int
Al Viro91f07162006-10-19 23:28:57 -0700417nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct nfsd3_createargs *args)
419{
420 if (!(p = decode_fh(p, &args->fh))
421 || !(p = decode_filename(p, &args->name, &args->len)))
422 return 0;
423
424 switch (args->createmode = ntohl(*p++)) {
425 case NFS3_CREATE_UNCHECKED:
426 case NFS3_CREATE_GUARDED:
NeilBrown072f62e2007-05-09 02:34:57 -0700427 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429 case NFS3_CREATE_EXCLUSIVE:
430 args->verf = p;
431 p += 2;
432 break;
433 default:
434 return 0;
435 }
436
437 return xdr_argsize_check(rqstp, p);
438}
439int
Al Viro91f07162006-10-19 23:28:57 -0700440nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct nfsd3_createargs *args)
442{
NeilBrown072f62e2007-05-09 02:34:57 -0700443 if (!(p = decode_fh(p, &args->fh)) ||
444 !(p = decode_filename(p, &args->name, &args->len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700446 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 return xdr_argsize_check(rqstp, p);
449}
450
451int
Al Viro91f07162006-10-19 23:28:57 -0700452nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct nfsd3_symlinkargs *args)
454{
455 unsigned int len;
456 int avail;
457 char *old, *new;
458 struct kvec *vec;
459
NeilBrown072f62e2007-05-09 02:34:57 -0700460 if (!(p = decode_fh(p, &args->ffh)) ||
461 !(p = decode_filename(p, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 )
463 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700464 p = decode_sattr3(p, &args->attrs);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* now decode the pathname, which might be larger than the first page.
467 * As we have to check for nul's anyway, we copy it into a new page
468 * This page appears in the rq_res.pages list, but as pages_len is always
469 * 0, it won't get in the way
470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 len = ntohl(*p++);
472 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
473 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700474 args->tname = new =
475 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 args->tlen = len;
477 /* first copy and check from the first page */
478 old = (char*)p;
479 vec = &rqstp->rq_arg.head[0];
480 avail = vec->iov_len - (old - (char*)vec->iov_base);
481 while (len && avail && *old) {
482 *new++ = *old++;
483 len--;
484 avail--;
485 }
486 /* now copy next page if there is one */
487 if (len && !avail && rqstp->rq_arg.page_len) {
488 avail = rqstp->rq_arg.page_len;
489 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
490 old = page_address(rqstp->rq_arg.pages[0]);
491 }
492 while (len && avail && *old) {
493 *new++ = *old++;
494 len--;
495 avail--;
496 }
497 *new = '\0';
498 if (len)
499 return 0;
500
501 return 1;
502}
503
504int
Al Viro91f07162006-10-19 23:28:57 -0700505nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct nfsd3_mknodargs *args)
507{
508 if (!(p = decode_fh(p, &args->fh))
509 || !(p = decode_filename(p, &args->name, &args->len)))
510 return 0;
511
512 args->ftype = ntohl(*p++);
513
514 if (args->ftype == NF3BLK || args->ftype == NF3CHR
NeilBrown072f62e2007-05-09 02:34:57 -0700515 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
516 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
519 args->major = ntohl(*p++);
520 args->minor = ntohl(*p++);
521 }
522
523 return xdr_argsize_check(rqstp, p);
524}
525
526int
Al Viro91f07162006-10-19 23:28:57 -0700527nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct nfsd3_renameargs *args)
529{
530 if (!(p = decode_fh(p, &args->ffh))
531 || !(p = decode_filename(p, &args->fname, &args->flen))
532 || !(p = decode_fh(p, &args->tfh))
533 || !(p = decode_filename(p, &args->tname, &args->tlen)))
534 return 0;
535
536 return xdr_argsize_check(rqstp, p);
537}
538
539int
Al Viro91f07162006-10-19 23:28:57 -0700540nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct nfsd3_readlinkargs *args)
542{
543 if (!(p = decode_fh(p, &args->fh)))
544 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700545 args->buffer =
546 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 return xdr_argsize_check(rqstp, p);
549}
550
551int
Al Viro91f07162006-10-19 23:28:57 -0700552nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct nfsd3_linkargs *args)
554{
555 if (!(p = decode_fh(p, &args->ffh))
556 || !(p = decode_fh(p, &args->tfh))
557 || !(p = decode_filename(p, &args->tname, &args->tlen)))
558 return 0;
559
560 return xdr_argsize_check(rqstp, p);
561}
562
563int
Al Viro91f07162006-10-19 23:28:57 -0700564nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct nfsd3_readdirargs *args)
566{
567 if (!(p = decode_fh(p, &args->fh)))
568 return 0;
569 p = xdr_decode_hyper(p, &args->cookie);
570 args->verf = p; p += 2;
571 args->dircount = ~0;
572 args->count = ntohl(*p++);
573
574 if (args->count > PAGE_SIZE)
575 args->count = PAGE_SIZE;
576
NeilBrown44524352006-10-04 02:15:46 -0700577 args->buffer =
578 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return xdr_argsize_check(rqstp, p);
581}
582
583int
Al Viro91f07162006-10-19 23:28:57 -0700584nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct nfsd3_readdirargs *args)
586{
587 int len, pn;
Greg Banks7adae482006-10-04 02:15:47 -0700588 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (!(p = decode_fh(p, &args->fh)))
591 return 0;
592 p = xdr_decode_hyper(p, &args->cookie);
593 args->verf = p; p += 2;
594 args->dircount = ntohl(*p++);
595 args->count = ntohl(*p++);
596
Greg Banks7adae482006-10-04 02:15:47 -0700597 len = (args->count > max_blocksize) ? max_blocksize :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 args->count;
599 args->count = len;
600
601 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700602 pn = rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!args->buffer)
604 args->buffer = page_address(rqstp->rq_respages[pn]);
605 len -= PAGE_SIZE;
606 }
607
608 return xdr_argsize_check(rqstp, p);
609}
610
611int
Al Viro91f07162006-10-19 23:28:57 -0700612nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct nfsd3_commitargs *args)
614{
615 if (!(p = decode_fh(p, &args->fh)))
616 return 0;
617 p = xdr_decode_hyper(p, &args->offset);
618 args->count = ntohl(*p++);
619
620 return xdr_argsize_check(rqstp, p);
621}
622
623/*
624 * XDR encode functions
625 */
626/*
627 * There must be an encoding function for void results so svc_process
628 * will work properly.
629 */
630int
Al Viro91f07162006-10-19 23:28:57 -0700631nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 return xdr_ressize_check(rqstp, p);
634}
635
636/* GETATTR */
637int
Al Viro91f07162006-10-19 23:28:57 -0700638nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct nfsd3_attrstat *resp)
640{
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400641 if (resp->status == 0) {
642 lease_get_mtime(resp->fh.fh_dentry->d_inode,
643 &resp->stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800644 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return xdr_ressize_check(rqstp, p);
647}
648
649/* SETATTR, REMOVE, RMDIR */
650int
Al Viro91f07162006-10-19 23:28:57 -0700651nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct nfsd3_attrstat *resp)
653{
654 p = encode_wcc_data(rqstp, p, &resp->fh);
655 return xdr_ressize_check(rqstp, p);
656}
657
658/* LOOKUP */
659int
Al Viro91f07162006-10-19 23:28:57 -0700660nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct nfsd3_diropres *resp)
662{
663 if (resp->status == 0) {
664 p = encode_fh(p, &resp->fh);
665 p = encode_post_op_attr(rqstp, p, &resp->fh);
666 }
667 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
668 return xdr_ressize_check(rqstp, p);
669}
670
671/* ACCESS */
672int
Al Viro91f07162006-10-19 23:28:57 -0700673nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct nfsd3_accessres *resp)
675{
676 p = encode_post_op_attr(rqstp, p, &resp->fh);
677 if (resp->status == 0)
678 *p++ = htonl(resp->access);
679 return xdr_ressize_check(rqstp, p);
680}
681
682/* READLINK */
683int
Al Viro91f07162006-10-19 23:28:57 -0700684nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct nfsd3_readlinkres *resp)
686{
687 p = encode_post_op_attr(rqstp, p, &resp->fh);
688 if (resp->status == 0) {
689 *p++ = htonl(resp->len);
690 xdr_ressize_check(rqstp, p);
691 rqstp->rq_res.page_len = resp->len;
692 if (resp->len & 3) {
693 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 rqstp->rq_res.tail[0].iov_base = p;
695 *p = 0;
696 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
697 }
698 return 1;
699 } else
700 return xdr_ressize_check(rqstp, p);
701}
702
703/* READ */
704int
Al Viro91f07162006-10-19 23:28:57 -0700705nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct nfsd3_readres *resp)
707{
708 p = encode_post_op_attr(rqstp, p, &resp->fh);
709 if (resp->status == 0) {
710 *p++ = htonl(resp->count);
711 *p++ = htonl(resp->eof);
712 *p++ = htonl(resp->count); /* xdr opaque count */
713 xdr_ressize_check(rqstp, p);
714 /* now update rqstp->rq_res to reflect data aswell */
715 rqstp->rq_res.page_len = resp->count;
716 if (resp->count & 3) {
717 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 rqstp->rq_res.tail[0].iov_base = p;
719 *p = 0;
720 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
721 }
722 return 1;
723 } else
724 return xdr_ressize_check(rqstp, p);
725}
726
727/* WRITE */
728int
Al Viro91f07162006-10-19 23:28:57 -0700729nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct nfsd3_writeres *resp)
731{
732 p = encode_wcc_data(rqstp, p, &resp->fh);
733 if (resp->status == 0) {
734 *p++ = htonl(resp->count);
735 *p++ = htonl(resp->committed);
736 *p++ = htonl(nfssvc_boot.tv_sec);
737 *p++ = htonl(nfssvc_boot.tv_usec);
738 }
739 return xdr_ressize_check(rqstp, p);
740}
741
742/* CREATE, MKDIR, SYMLINK, MKNOD */
743int
Al Viro91f07162006-10-19 23:28:57 -0700744nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct nfsd3_diropres *resp)
746{
747 if (resp->status == 0) {
748 *p++ = xdr_one;
749 p = encode_fh(p, &resp->fh);
750 p = encode_post_op_attr(rqstp, p, &resp->fh);
751 }
752 p = encode_wcc_data(rqstp, p, &resp->dirfh);
753 return xdr_ressize_check(rqstp, p);
754}
755
756/* RENAME */
757int
Al Viro91f07162006-10-19 23:28:57 -0700758nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct nfsd3_renameres *resp)
760{
761 p = encode_wcc_data(rqstp, p, &resp->ffh);
762 p = encode_wcc_data(rqstp, p, &resp->tfh);
763 return xdr_ressize_check(rqstp, p);
764}
765
766/* LINK */
767int
Al Viro91f07162006-10-19 23:28:57 -0700768nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct nfsd3_linkres *resp)
770{
771 p = encode_post_op_attr(rqstp, p, &resp->fh);
772 p = encode_wcc_data(rqstp, p, &resp->tfh);
773 return xdr_ressize_check(rqstp, p);
774}
775
776/* READDIR */
777int
Al Viro91f07162006-10-19 23:28:57 -0700778nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct nfsd3_readdirres *resp)
780{
781 p = encode_post_op_attr(rqstp, p, &resp->fh);
782
783 if (resp->status == 0) {
784 /* stupid readdir cookie */
785 memcpy(p, resp->verf, 8); p += 2;
786 xdr_ressize_check(rqstp, p);
787 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
788 return 1; /*No room for trailer */
789 rqstp->rq_res.page_len = (resp->count) << 2;
790
791 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 rqstp->rq_res.tail[0].iov_base = p;
793 *p++ = 0; /* no more entries */
794 *p++ = htonl(resp->common.err == nfserr_eof);
795 rqstp->rq_res.tail[0].iov_len = 2<<2;
796 return 1;
797 } else
798 return xdr_ressize_check(rqstp, p);
799}
800
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800801static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700802encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400803 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 *p++ = xdr_one; /* mark entry present */
806 p = xdr_encode_hyper(p, ino); /* file id */
807 p = xdr_encode_array(p, name, namlen);/* name length & name */
808
809 cd->offset = p; /* remember pointer */
810 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
811
812 return p;
813}
814
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800815static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700816encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct svc_fh *fhp)
818{
819 p = encode_post_op_attr(cd->rqstp, p, fhp);
820 *p++ = xdr_one; /* yes, a file handle follows */
821 p = encode_fh(p, fhp);
822 fh_put(fhp);
823 return p;
824}
825
826static int
827compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
828 const char *name, int namlen)
829{
830 struct svc_export *exp;
831 struct dentry *dparent, *dchild;
832 int rv = 0;
833
834 dparent = cd->fh.fh_dentry;
835 exp = cd->fh.fh_export;
836
837 fh_init(fhp, NFS3_FHSIZE);
838 if (isdotent(name, namlen)) {
839 if (namlen == 2) {
840 dchild = dget_parent(dparent);
841 if (dchild == dparent) {
842 /* filesystem root - cannot return filehandle for ".." */
843 dput(dchild);
844 return 1;
845 }
846 } else
847 dchild = dget(dparent);
848 } else
849 dchild = lookup_one_len(name, dparent, namlen);
850 if (IS_ERR(dchild))
851 return 1;
852 if (d_mountpoint(dchild) ||
853 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
854 !dchild->d_inode)
855 rv = 1;
856 dput(dchild);
857 return rv;
858}
859
860/*
861 * Encode a directory entry. This one works for both normal readdir
862 * and readdirplus.
863 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
864 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
865 *
866 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
867 * file handle.
868 */
869
870#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
871#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
872static int
NeilBrown598b9a52007-03-26 21:32:08 -0800873encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400874 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
877 common);
Al Viro91f07162006-10-19 23:28:57 -0700878 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 caddr_t curr_page_addr = NULL;
880 int pn; /* current page number */
881 int slen; /* string (name) length */
882 int elen; /* estimated entry length in words */
883 int num_entry_words = 0; /* actual number of words */
884
885 if (cd->offset) {
886 u64 offset64 = offset;
887
888 if (unlikely(cd->offset1)) {
889 /* we ended up with offset on a page boundary */
890 *cd->offset = htonl(offset64 >> 32);
891 *cd->offset1 = htonl(offset64 & 0xffffffff);
892 cd->offset1 = NULL;
893 } else {
NeilBrown598b9a52007-03-26 21:32:08 -0800894 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 }
897
898 /*
899 dprintk("encode_entry(%.*s @%ld%s)\n",
900 namlen, name, (long) offset, plus? " plus" : "");
901 */
902
903 /* truncate filename if too long */
904 if (namlen > NFS3_MAXNAMLEN)
905 namlen = NFS3_MAXNAMLEN;
906
907 slen = XDR_QUADLEN(namlen);
908 elen = slen + NFS3_ENTRY_BAGGAGE
909 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
910
911 if (cd->buflen < elen) {
912 cd->common.err = nfserr_toosmall;
913 return -EINVAL;
914 }
915
916 /* determine which page in rq_respages[] we are currently filling */
917 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
918 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
919
920 if (((caddr_t)cd->buffer >= curr_page_addr) &&
921 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
922 break;
923 }
924
925 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
926 /* encode entry in current page */
927
928 p = encode_entry_baggage(cd, p, name, namlen, ino);
929
930 /* throw in readdirplus baggage */
931 if (plus) {
932 struct svc_fh fh;
933
934 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
935 *p++ = 0;
936 *p++ = 0;
937 } else
938 p = encode_entryplus_baggage(cd, p, &fh);
939 }
940 num_entry_words = p - cd->buffer;
941 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
942 /* temporarily encode entry into next page, then move back to
943 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -0700944 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int len1, len2;
946
947 /* grab next page for temporary storage of entry */
948 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
949
950 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
951
952 /* throw in readdirplus baggage */
953 if (plus) {
954 struct svc_fh fh;
955
956 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
957 /* zero out the filehandle */
958 *p1++ = 0;
959 *p1++ = 0;
960 } else
961 p1 = encode_entryplus_baggage(cd, p1, &fh);
962 }
963
964 /* determine entry word length and lengths to go in pages */
965 num_entry_words = p1 - tmp;
966 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
967 if ((num_entry_words << 2) < len1) {
968 /* the actual number of words in the entry is less
969 * than elen and can still fit in the current page
970 */
971 memmove(p, tmp, num_entry_words << 2);
972 p += num_entry_words;
973
974 /* update offset */
975 cd->offset = cd->buffer + (cd->offset - tmp);
976 } else {
977 unsigned int offset_r = (cd->offset - tmp) << 2;
978
979 /* update pointer to offset location.
980 * This is a 64bit quantity, so we need to
981 * deal with 3 cases:
982 * - entirely in first page
983 * - entirely in second page
984 * - 4 bytes in each page
985 */
986 if (offset_r + 8 <= len1) {
987 cd->offset = p + (cd->offset - tmp);
988 } else if (offset_r >= len1) {
989 cd->offset -= len1 >> 2;
990 } else {
991 /* sitting on the fence */
992 BUG_ON(offset_r != len1 - 4);
993 cd->offset = p + (cd->offset - tmp);
994 cd->offset1 = tmp;
995 }
996
997 len2 = (num_entry_words << 2) - len1;
998
999 /* move from temp page to current and next pages */
1000 memmove(p, tmp, len1);
1001 memmove(tmp, (caddr_t)tmp+len1, len2);
1002
1003 p = tmp + (len2 >> 2);
1004 }
1005 }
1006 else {
1007 cd->common.err = nfserr_toosmall;
1008 return -EINVAL;
1009 }
1010
1011 cd->buflen -= num_entry_words;
1012 cd->buffer = p;
1013 cd->common.err = nfs_ok;
1014 return 0;
1015
1016}
1017
1018int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001019nfs3svc_encode_entry(void *cd, const char *name,
1020 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1023}
1024
1025int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001026nfs3svc_encode_entry_plus(void *cd, const char *name,
1027 int namlen, loff_t offset, u64 ino,
1028 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1031}
1032
1033/* FSSTAT */
1034int
Al Viro91f07162006-10-19 23:28:57 -07001035nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct nfsd3_fsstatres *resp)
1037{
1038 struct kstatfs *s = &resp->stats;
1039 u64 bs = s->f_bsize;
1040
1041 *p++ = xdr_zero; /* no post_op_attr */
1042
1043 if (resp->status == 0) {
1044 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1045 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1046 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1047 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1048 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1049 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1050 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1051 }
1052 return xdr_ressize_check(rqstp, p);
1053}
1054
1055/* FSINFO */
1056int
Al Viro91f07162006-10-19 23:28:57 -07001057nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct nfsd3_fsinfores *resp)
1059{
1060 *p++ = xdr_zero; /* no post_op_attr */
1061
1062 if (resp->status == 0) {
1063 *p++ = htonl(resp->f_rtmax);
1064 *p++ = htonl(resp->f_rtpref);
1065 *p++ = htonl(resp->f_rtmult);
1066 *p++ = htonl(resp->f_wtmax);
1067 *p++ = htonl(resp->f_wtpref);
1068 *p++ = htonl(resp->f_wtmult);
1069 *p++ = htonl(resp->f_dtpref);
1070 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1071 *p++ = xdr_one;
1072 *p++ = xdr_zero;
1073 *p++ = htonl(resp->f_properties);
1074 }
1075
1076 return xdr_ressize_check(rqstp, p);
1077}
1078
1079/* PATHCONF */
1080int
Al Viro91f07162006-10-19 23:28:57 -07001081nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct nfsd3_pathconfres *resp)
1083{
1084 *p++ = xdr_zero; /* no post_op_attr */
1085
1086 if (resp->status == 0) {
1087 *p++ = htonl(resp->p_link_max);
1088 *p++ = htonl(resp->p_name_max);
1089 *p++ = htonl(resp->p_no_trunc);
1090 *p++ = htonl(resp->p_chown_restricted);
1091 *p++ = htonl(resp->p_case_insensitive);
1092 *p++ = htonl(resp->p_case_preserving);
1093 }
1094
1095 return xdr_ressize_check(rqstp, p);
1096}
1097
1098/* COMMIT */
1099int
Al Viro91f07162006-10-19 23:28:57 -07001100nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct nfsd3_commitres *resp)
1102{
1103 p = encode_wcc_data(rqstp, p, &resp->fh);
1104 /* Write verifier */
1105 if (resp->status == 0) {
1106 *p++ = htonl(nfssvc_boot.tv_sec);
1107 *p++ = htonl(nfssvc_boot.tv_usec);
1108 }
1109 return xdr_ressize_check(rqstp, p);
1110}
1111
1112/*
1113 * XDR release functions
1114 */
1115int
Al Viro91f07162006-10-19 23:28:57 -07001116nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct nfsd3_attrstat *resp)
1118{
1119 fh_put(&resp->fh);
1120 return 1;
1121}
1122
1123int
Al Viro91f07162006-10-19 23:28:57 -07001124nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 struct nfsd3_fhandle_pair *resp)
1126{
1127 fh_put(&resp->fh1);
1128 fh_put(&resp->fh2);
1129 return 1;
1130}