blob: 2d116d2298f8e3aec23982bc04795dd9daf69ca2 [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.
391 */
NeilBrown072f62e2007-05-09 02:34:57 -0700392 if (dlen != XDR_QUADLEN(len)*4)
Peter Staubachf34b9562007-05-09 02:34:48 -0700393 return 0;
394
395 if (args->count > max_blocksize) {
396 args->count = max_blocksize;
397 len = args->len = max_blocksize;
398 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700399 rqstp->rq_vec[0].iov_base = (void*)p;
400 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
Peter Staubachf34b9562007-05-09 02:34:48 -0700401 v = 0;
NeilBrown3cc03b12006-10-04 02:15:47 -0700402 while (len > rqstp->rq_vec[v].iov_len) {
403 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700405 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
406 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700408 rqstp->rq_vec[v].iov_len = len;
Peter Staubachf34b9562007-05-09 02:34:48 -0700409 args->vlen = v + 1;
410 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413int
Al Viro91f07162006-10-19 23:28:57 -0700414nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct nfsd3_createargs *args)
416{
417 if (!(p = decode_fh(p, &args->fh))
418 || !(p = decode_filename(p, &args->name, &args->len)))
419 return 0;
420
421 switch (args->createmode = ntohl(*p++)) {
422 case NFS3_CREATE_UNCHECKED:
423 case NFS3_CREATE_GUARDED:
NeilBrown072f62e2007-05-09 02:34:57 -0700424 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 case NFS3_CREATE_EXCLUSIVE:
427 args->verf = p;
428 p += 2;
429 break;
430 default:
431 return 0;
432 }
433
434 return xdr_argsize_check(rqstp, p);
435}
436int
Al Viro91f07162006-10-19 23:28:57 -0700437nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct nfsd3_createargs *args)
439{
NeilBrown072f62e2007-05-09 02:34:57 -0700440 if (!(p = decode_fh(p, &args->fh)) ||
441 !(p = decode_filename(p, &args->name, &args->len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700443 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return xdr_argsize_check(rqstp, p);
446}
447
448int
Al Viro91f07162006-10-19 23:28:57 -0700449nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct nfsd3_symlinkargs *args)
451{
452 unsigned int len;
453 int avail;
454 char *old, *new;
455 struct kvec *vec;
456
NeilBrown072f62e2007-05-09 02:34:57 -0700457 if (!(p = decode_fh(p, &args->ffh)) ||
458 !(p = decode_filename(p, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 )
460 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700461 p = decode_sattr3(p, &args->attrs);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* now decode the pathname, which might be larger than the first page.
464 * As we have to check for nul's anyway, we copy it into a new page
465 * This page appears in the rq_res.pages list, but as pages_len is always
466 * 0, it won't get in the way
467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 len = ntohl(*p++);
469 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
470 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700471 args->tname = new =
472 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 args->tlen = len;
474 /* first copy and check from the first page */
475 old = (char*)p;
476 vec = &rqstp->rq_arg.head[0];
477 avail = vec->iov_len - (old - (char*)vec->iov_base);
478 while (len && avail && *old) {
479 *new++ = *old++;
480 len--;
481 avail--;
482 }
483 /* now copy next page if there is one */
484 if (len && !avail && rqstp->rq_arg.page_len) {
485 avail = rqstp->rq_arg.page_len;
486 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
487 old = page_address(rqstp->rq_arg.pages[0]);
488 }
489 while (len && avail && *old) {
490 *new++ = *old++;
491 len--;
492 avail--;
493 }
494 *new = '\0';
495 if (len)
496 return 0;
497
498 return 1;
499}
500
501int
Al Viro91f07162006-10-19 23:28:57 -0700502nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct nfsd3_mknodargs *args)
504{
505 if (!(p = decode_fh(p, &args->fh))
506 || !(p = decode_filename(p, &args->name, &args->len)))
507 return 0;
508
509 args->ftype = ntohl(*p++);
510
511 if (args->ftype == NF3BLK || args->ftype == NF3CHR
NeilBrown072f62e2007-05-09 02:34:57 -0700512 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
513 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
516 args->major = ntohl(*p++);
517 args->minor = ntohl(*p++);
518 }
519
520 return xdr_argsize_check(rqstp, p);
521}
522
523int
Al Viro91f07162006-10-19 23:28:57 -0700524nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct nfsd3_renameargs *args)
526{
527 if (!(p = decode_fh(p, &args->ffh))
528 || !(p = decode_filename(p, &args->fname, &args->flen))
529 || !(p = decode_fh(p, &args->tfh))
530 || !(p = decode_filename(p, &args->tname, &args->tlen)))
531 return 0;
532
533 return xdr_argsize_check(rqstp, p);
534}
535
536int
Al Viro91f07162006-10-19 23:28:57 -0700537nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct nfsd3_readlinkargs *args)
539{
540 if (!(p = decode_fh(p, &args->fh)))
541 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700542 args->buffer =
543 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return xdr_argsize_check(rqstp, p);
546}
547
548int
Al Viro91f07162006-10-19 23:28:57 -0700549nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct nfsd3_linkargs *args)
551{
552 if (!(p = decode_fh(p, &args->ffh))
553 || !(p = decode_fh(p, &args->tfh))
554 || !(p = decode_filename(p, &args->tname, &args->tlen)))
555 return 0;
556
557 return xdr_argsize_check(rqstp, p);
558}
559
560int
Al Viro91f07162006-10-19 23:28:57 -0700561nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct nfsd3_readdirargs *args)
563{
564 if (!(p = decode_fh(p, &args->fh)))
565 return 0;
566 p = xdr_decode_hyper(p, &args->cookie);
567 args->verf = p; p += 2;
568 args->dircount = ~0;
569 args->count = ntohl(*p++);
570
571 if (args->count > PAGE_SIZE)
572 args->count = PAGE_SIZE;
573
NeilBrown44524352006-10-04 02:15:46 -0700574 args->buffer =
575 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return xdr_argsize_check(rqstp, p);
578}
579
580int
Al Viro91f07162006-10-19 23:28:57 -0700581nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct nfsd3_readdirargs *args)
583{
584 int len, pn;
Greg Banks7adae482006-10-04 02:15:47 -0700585 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (!(p = decode_fh(p, &args->fh)))
588 return 0;
589 p = xdr_decode_hyper(p, &args->cookie);
590 args->verf = p; p += 2;
591 args->dircount = ntohl(*p++);
592 args->count = ntohl(*p++);
593
Greg Banks7adae482006-10-04 02:15:47 -0700594 len = (args->count > max_blocksize) ? max_blocksize :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 args->count;
596 args->count = len;
597
598 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700599 pn = rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!args->buffer)
601 args->buffer = page_address(rqstp->rq_respages[pn]);
602 len -= PAGE_SIZE;
603 }
604
605 return xdr_argsize_check(rqstp, p);
606}
607
608int
Al Viro91f07162006-10-19 23:28:57 -0700609nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct nfsd3_commitargs *args)
611{
612 if (!(p = decode_fh(p, &args->fh)))
613 return 0;
614 p = xdr_decode_hyper(p, &args->offset);
615 args->count = ntohl(*p++);
616
617 return xdr_argsize_check(rqstp, p);
618}
619
620/*
621 * XDR encode functions
622 */
623/*
624 * There must be an encoding function for void results so svc_process
625 * will work properly.
626 */
627int
Al Viro91f07162006-10-19 23:28:57 -0700628nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 return xdr_ressize_check(rqstp, p);
631}
632
633/* GETATTR */
634int
Al Viro91f07162006-10-19 23:28:57 -0700635nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct nfsd3_attrstat *resp)
637{
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400638 if (resp->status == 0) {
639 lease_get_mtime(resp->fh.fh_dentry->d_inode,
640 &resp->stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800641 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return xdr_ressize_check(rqstp, p);
644}
645
646/* SETATTR, REMOVE, RMDIR */
647int
Al Viro91f07162006-10-19 23:28:57 -0700648nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct nfsd3_attrstat *resp)
650{
651 p = encode_wcc_data(rqstp, p, &resp->fh);
652 return xdr_ressize_check(rqstp, p);
653}
654
655/* LOOKUP */
656int
Al Viro91f07162006-10-19 23:28:57 -0700657nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct nfsd3_diropres *resp)
659{
660 if (resp->status == 0) {
661 p = encode_fh(p, &resp->fh);
662 p = encode_post_op_attr(rqstp, p, &resp->fh);
663 }
664 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
665 return xdr_ressize_check(rqstp, p);
666}
667
668/* ACCESS */
669int
Al Viro91f07162006-10-19 23:28:57 -0700670nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct nfsd3_accessres *resp)
672{
673 p = encode_post_op_attr(rqstp, p, &resp->fh);
674 if (resp->status == 0)
675 *p++ = htonl(resp->access);
676 return xdr_ressize_check(rqstp, p);
677}
678
679/* READLINK */
680int
Al Viro91f07162006-10-19 23:28:57 -0700681nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct nfsd3_readlinkres *resp)
683{
684 p = encode_post_op_attr(rqstp, p, &resp->fh);
685 if (resp->status == 0) {
686 *p++ = htonl(resp->len);
687 xdr_ressize_check(rqstp, p);
688 rqstp->rq_res.page_len = resp->len;
689 if (resp->len & 3) {
690 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 rqstp->rq_res.tail[0].iov_base = p;
692 *p = 0;
693 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
694 }
695 return 1;
696 } else
697 return xdr_ressize_check(rqstp, p);
698}
699
700/* READ */
701int
Al Viro91f07162006-10-19 23:28:57 -0700702nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct nfsd3_readres *resp)
704{
705 p = encode_post_op_attr(rqstp, p, &resp->fh);
706 if (resp->status == 0) {
707 *p++ = htonl(resp->count);
708 *p++ = htonl(resp->eof);
709 *p++ = htonl(resp->count); /* xdr opaque count */
710 xdr_ressize_check(rqstp, p);
711 /* now update rqstp->rq_res to reflect data aswell */
712 rqstp->rq_res.page_len = resp->count;
713 if (resp->count & 3) {
714 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 rqstp->rq_res.tail[0].iov_base = p;
716 *p = 0;
717 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
718 }
719 return 1;
720 } else
721 return xdr_ressize_check(rqstp, p);
722}
723
724/* WRITE */
725int
Al Viro91f07162006-10-19 23:28:57 -0700726nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct nfsd3_writeres *resp)
728{
729 p = encode_wcc_data(rqstp, p, &resp->fh);
730 if (resp->status == 0) {
731 *p++ = htonl(resp->count);
732 *p++ = htonl(resp->committed);
733 *p++ = htonl(nfssvc_boot.tv_sec);
734 *p++ = htonl(nfssvc_boot.tv_usec);
735 }
736 return xdr_ressize_check(rqstp, p);
737}
738
739/* CREATE, MKDIR, SYMLINK, MKNOD */
740int
Al Viro91f07162006-10-19 23:28:57 -0700741nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct nfsd3_diropres *resp)
743{
744 if (resp->status == 0) {
745 *p++ = xdr_one;
746 p = encode_fh(p, &resp->fh);
747 p = encode_post_op_attr(rqstp, p, &resp->fh);
748 }
749 p = encode_wcc_data(rqstp, p, &resp->dirfh);
750 return xdr_ressize_check(rqstp, p);
751}
752
753/* RENAME */
754int
Al Viro91f07162006-10-19 23:28:57 -0700755nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct nfsd3_renameres *resp)
757{
758 p = encode_wcc_data(rqstp, p, &resp->ffh);
759 p = encode_wcc_data(rqstp, p, &resp->tfh);
760 return xdr_ressize_check(rqstp, p);
761}
762
763/* LINK */
764int
Al Viro91f07162006-10-19 23:28:57 -0700765nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct nfsd3_linkres *resp)
767{
768 p = encode_post_op_attr(rqstp, p, &resp->fh);
769 p = encode_wcc_data(rqstp, p, &resp->tfh);
770 return xdr_ressize_check(rqstp, p);
771}
772
773/* READDIR */
774int
Al Viro91f07162006-10-19 23:28:57 -0700775nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct nfsd3_readdirres *resp)
777{
778 p = encode_post_op_attr(rqstp, p, &resp->fh);
779
780 if (resp->status == 0) {
781 /* stupid readdir cookie */
782 memcpy(p, resp->verf, 8); p += 2;
783 xdr_ressize_check(rqstp, p);
784 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
785 return 1; /*No room for trailer */
786 rqstp->rq_res.page_len = (resp->count) << 2;
787
788 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 rqstp->rq_res.tail[0].iov_base = p;
790 *p++ = 0; /* no more entries */
791 *p++ = htonl(resp->common.err == nfserr_eof);
792 rqstp->rq_res.tail[0].iov_len = 2<<2;
793 return 1;
794 } else
795 return xdr_ressize_check(rqstp, p);
796}
797
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800798static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700799encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400800 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 *p++ = xdr_one; /* mark entry present */
803 p = xdr_encode_hyper(p, ino); /* file id */
804 p = xdr_encode_array(p, name, namlen);/* name length & name */
805
806 cd->offset = p; /* remember pointer */
807 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
808
809 return p;
810}
811
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800812static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700813encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct svc_fh *fhp)
815{
816 p = encode_post_op_attr(cd->rqstp, p, fhp);
817 *p++ = xdr_one; /* yes, a file handle follows */
818 p = encode_fh(p, fhp);
819 fh_put(fhp);
820 return p;
821}
822
823static int
824compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
825 const char *name, int namlen)
826{
827 struct svc_export *exp;
828 struct dentry *dparent, *dchild;
829 int rv = 0;
830
831 dparent = cd->fh.fh_dentry;
832 exp = cd->fh.fh_export;
833
834 fh_init(fhp, NFS3_FHSIZE);
835 if (isdotent(name, namlen)) {
836 if (namlen == 2) {
837 dchild = dget_parent(dparent);
838 if (dchild == dparent) {
839 /* filesystem root - cannot return filehandle for ".." */
840 dput(dchild);
841 return 1;
842 }
843 } else
844 dchild = dget(dparent);
845 } else
846 dchild = lookup_one_len(name, dparent, namlen);
847 if (IS_ERR(dchild))
848 return 1;
849 if (d_mountpoint(dchild) ||
850 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
851 !dchild->d_inode)
852 rv = 1;
853 dput(dchild);
854 return rv;
855}
856
857/*
858 * Encode a directory entry. This one works for both normal readdir
859 * and readdirplus.
860 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
861 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
862 *
863 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
864 * file handle.
865 */
866
867#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
868#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
869static int
NeilBrown598b9a52007-03-26 21:32:08 -0800870encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400871 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
874 common);
Al Viro91f07162006-10-19 23:28:57 -0700875 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 caddr_t curr_page_addr = NULL;
877 int pn; /* current page number */
878 int slen; /* string (name) length */
879 int elen; /* estimated entry length in words */
880 int num_entry_words = 0; /* actual number of words */
881
882 if (cd->offset) {
883 u64 offset64 = offset;
884
885 if (unlikely(cd->offset1)) {
886 /* we ended up with offset on a page boundary */
887 *cd->offset = htonl(offset64 >> 32);
888 *cd->offset1 = htonl(offset64 & 0xffffffff);
889 cd->offset1 = NULL;
890 } else {
NeilBrown598b9a52007-03-26 21:32:08 -0800891 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 }
894
895 /*
896 dprintk("encode_entry(%.*s @%ld%s)\n",
897 namlen, name, (long) offset, plus? " plus" : "");
898 */
899
900 /* truncate filename if too long */
901 if (namlen > NFS3_MAXNAMLEN)
902 namlen = NFS3_MAXNAMLEN;
903
904 slen = XDR_QUADLEN(namlen);
905 elen = slen + NFS3_ENTRY_BAGGAGE
906 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
907
908 if (cd->buflen < elen) {
909 cd->common.err = nfserr_toosmall;
910 return -EINVAL;
911 }
912
913 /* determine which page in rq_respages[] we are currently filling */
914 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
915 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
916
917 if (((caddr_t)cd->buffer >= curr_page_addr) &&
918 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
919 break;
920 }
921
922 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
923 /* encode entry in current page */
924
925 p = encode_entry_baggage(cd, p, name, namlen, ino);
926
927 /* throw in readdirplus baggage */
928 if (plus) {
929 struct svc_fh fh;
930
931 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
932 *p++ = 0;
933 *p++ = 0;
934 } else
935 p = encode_entryplus_baggage(cd, p, &fh);
936 }
937 num_entry_words = p - cd->buffer;
938 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
939 /* temporarily encode entry into next page, then move back to
940 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -0700941 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 int len1, len2;
943
944 /* grab next page for temporary storage of entry */
945 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
946
947 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
948
949 /* throw in readdirplus baggage */
950 if (plus) {
951 struct svc_fh fh;
952
953 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
954 /* zero out the filehandle */
955 *p1++ = 0;
956 *p1++ = 0;
957 } else
958 p1 = encode_entryplus_baggage(cd, p1, &fh);
959 }
960
961 /* determine entry word length and lengths to go in pages */
962 num_entry_words = p1 - tmp;
963 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
964 if ((num_entry_words << 2) < len1) {
965 /* the actual number of words in the entry is less
966 * than elen and can still fit in the current page
967 */
968 memmove(p, tmp, num_entry_words << 2);
969 p += num_entry_words;
970
971 /* update offset */
972 cd->offset = cd->buffer + (cd->offset - tmp);
973 } else {
974 unsigned int offset_r = (cd->offset - tmp) << 2;
975
976 /* update pointer to offset location.
977 * This is a 64bit quantity, so we need to
978 * deal with 3 cases:
979 * - entirely in first page
980 * - entirely in second page
981 * - 4 bytes in each page
982 */
983 if (offset_r + 8 <= len1) {
984 cd->offset = p + (cd->offset - tmp);
985 } else if (offset_r >= len1) {
986 cd->offset -= len1 >> 2;
987 } else {
988 /* sitting on the fence */
989 BUG_ON(offset_r != len1 - 4);
990 cd->offset = p + (cd->offset - tmp);
991 cd->offset1 = tmp;
992 }
993
994 len2 = (num_entry_words << 2) - len1;
995
996 /* move from temp page to current and next pages */
997 memmove(p, tmp, len1);
998 memmove(tmp, (caddr_t)tmp+len1, len2);
999
1000 p = tmp + (len2 >> 2);
1001 }
1002 }
1003 else {
1004 cd->common.err = nfserr_toosmall;
1005 return -EINVAL;
1006 }
1007
1008 cd->buflen -= num_entry_words;
1009 cd->buffer = p;
1010 cd->common.err = nfs_ok;
1011 return 0;
1012
1013}
1014
1015int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001016nfs3svc_encode_entry(void *cd, const char *name,
1017 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1020}
1021
1022int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001023nfs3svc_encode_entry_plus(void *cd, const char *name,
1024 int namlen, loff_t offset, u64 ino,
1025 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1028}
1029
1030/* FSSTAT */
1031int
Al Viro91f07162006-10-19 23:28:57 -07001032nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct nfsd3_fsstatres *resp)
1034{
1035 struct kstatfs *s = &resp->stats;
1036 u64 bs = s->f_bsize;
1037
1038 *p++ = xdr_zero; /* no post_op_attr */
1039
1040 if (resp->status == 0) {
1041 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1042 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1043 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1044 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1045 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1046 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1047 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1048 }
1049 return xdr_ressize_check(rqstp, p);
1050}
1051
1052/* FSINFO */
1053int
Al Viro91f07162006-10-19 23:28:57 -07001054nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct nfsd3_fsinfores *resp)
1056{
1057 *p++ = xdr_zero; /* no post_op_attr */
1058
1059 if (resp->status == 0) {
1060 *p++ = htonl(resp->f_rtmax);
1061 *p++ = htonl(resp->f_rtpref);
1062 *p++ = htonl(resp->f_rtmult);
1063 *p++ = htonl(resp->f_wtmax);
1064 *p++ = htonl(resp->f_wtpref);
1065 *p++ = htonl(resp->f_wtmult);
1066 *p++ = htonl(resp->f_dtpref);
1067 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1068 *p++ = xdr_one;
1069 *p++ = xdr_zero;
1070 *p++ = htonl(resp->f_properties);
1071 }
1072
1073 return xdr_ressize_check(rqstp, p);
1074}
1075
1076/* PATHCONF */
1077int
Al Viro91f07162006-10-19 23:28:57 -07001078nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct nfsd3_pathconfres *resp)
1080{
1081 *p++ = xdr_zero; /* no post_op_attr */
1082
1083 if (resp->status == 0) {
1084 *p++ = htonl(resp->p_link_max);
1085 *p++ = htonl(resp->p_name_max);
1086 *p++ = htonl(resp->p_no_trunc);
1087 *p++ = htonl(resp->p_chown_restricted);
1088 *p++ = htonl(resp->p_case_insensitive);
1089 *p++ = htonl(resp->p_case_preserving);
1090 }
1091
1092 return xdr_ressize_check(rqstp, p);
1093}
1094
1095/* COMMIT */
1096int
Al Viro91f07162006-10-19 23:28:57 -07001097nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct nfsd3_commitres *resp)
1099{
1100 p = encode_wcc_data(rqstp, p, &resp->fh);
1101 /* Write verifier */
1102 if (resp->status == 0) {
1103 *p++ = htonl(nfssvc_boot.tv_sec);
1104 *p++ = htonl(nfssvc_boot.tv_usec);
1105 }
1106 return xdr_ressize_check(rqstp, p);
1107}
1108
1109/*
1110 * XDR release functions
1111 */
1112int
Al Viro91f07162006-10-19 23:28:57 -07001113nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct nfsd3_attrstat *resp)
1115{
1116 fh_put(&resp->fh);
1117 return 1;
1118}
1119
1120int
Al Viro91f07162006-10-19 23:28:57 -07001121nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct nfsd3_fhandle_pair *resp)
1123{
1124 fh_put(&resp->fh1);
1125 fh_put(&resp->fh2);
1126 return 1;
1127}