Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/fs/nfsd/nfsxdr.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * XDR support for nfsd |
| 5 | * |
| 6 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/time.h> |
| 11 | #include <linux/nfs.h> |
| 12 | #include <linux/vfs.h> |
| 13 | #include <linux/sunrpc/xdr.h> |
| 14 | #include <linux/sunrpc/svc.h> |
| 15 | #include <linux/nfsd/nfsd.h> |
| 16 | #include <linux/nfsd/xdr.h> |
| 17 | #include <linux/mm.h> |
| 18 | |
| 19 | #define NFSDDBG_FACILITY NFSDDBG_XDR |
| 20 | |
| 21 | |
| 22 | #ifdef NFSD_OPTIMIZE_SPACE |
| 23 | # define inline |
| 24 | #endif |
| 25 | |
| 26 | /* |
| 27 | * Mapping of S_IF* types to NFS file types |
| 28 | */ |
| 29 | static u32 nfs_ftypes[] = { |
| 30 | NFNON, NFCHR, NFCHR, NFBAD, |
| 31 | NFDIR, NFBAD, NFBLK, NFBAD, |
| 32 | NFREG, NFBAD, NFLNK, NFBAD, |
| 33 | NFSOCK, NFBAD, NFLNK, NFBAD, |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | /* |
| 38 | * XDR functions for basic NFS types |
| 39 | */ |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 40 | static __be32 * |
| 41 | decode_fh(__be32 *p, struct svc_fh *fhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | fh_init(fhp, NFS_FHSIZE); |
| 44 | memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE); |
| 45 | fhp->fh_handle.fh_size = NFS_FHSIZE; |
| 46 | |
| 47 | /* FIXME: Look up export pointer here and verify |
| 48 | * Sun Secure RPC if requested */ |
| 49 | return p + (NFS_FHSIZE >> 2); |
| 50 | } |
| 51 | |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 52 | /* Helper function for NFSv2 ACL code */ |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 53 | __be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 54 | { |
| 55 | return decode_fh(p, fhp); |
| 56 | } |
| 57 | |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 58 | static inline __be32 * |
| 59 | encode_fh(__be32 *p, struct svc_fh *fhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE); |
| 62 | return p + (NFS_FHSIZE>> 2); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Decode a file name and make sure that the path contains |
| 67 | * no slashes or null bytes. |
| 68 | */ |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 69 | static inline __be32 * |
| 70 | decode_filename(__be32 *p, char **namp, int *lenp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | char *name; |
| 73 | int i; |
| 74 | |
| 75 | if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) { |
| 76 | for (i = 0, name = *namp; i < *lenp; i++, name++) { |
| 77 | if (*name == '\0' || *name == '/') |
| 78 | return NULL; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return p; |
| 83 | } |
| 84 | |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 85 | static inline __be32 * |
| 86 | decode_pathname(__be32 *p, char **namp, int *lenp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | char *name; |
| 89 | int i; |
| 90 | |
| 91 | if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) { |
| 92 | for (i = 0, name = *namp; i < *lenp; i++, name++) { |
| 93 | if (*name == '\0') |
| 94 | return NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return p; |
| 99 | } |
| 100 | |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 101 | static inline __be32 * |
| 102 | decode_sattr(__be32 *p, struct iattr *iap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | u32 tmp, tmp1; |
| 105 | |
| 106 | iap->ia_valid = 0; |
| 107 | |
| 108 | /* Sun client bug compatibility check: some sun clients seem to |
| 109 | * put 0xffff in the mode field when they mean 0xffffffff. |
| 110 | * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah. |
| 111 | */ |
| 112 | if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) { |
| 113 | iap->ia_valid |= ATTR_MODE; |
| 114 | iap->ia_mode = tmp; |
| 115 | } |
| 116 | if ((tmp = ntohl(*p++)) != (u32)-1) { |
| 117 | iap->ia_valid |= ATTR_UID; |
| 118 | iap->ia_uid = tmp; |
| 119 | } |
| 120 | if ((tmp = ntohl(*p++)) != (u32)-1) { |
| 121 | iap->ia_valid |= ATTR_GID; |
| 122 | iap->ia_gid = tmp; |
| 123 | } |
| 124 | if ((tmp = ntohl(*p++)) != (u32)-1) { |
| 125 | iap->ia_valid |= ATTR_SIZE; |
| 126 | iap->ia_size = tmp; |
| 127 | } |
| 128 | tmp = ntohl(*p++); tmp1 = ntohl(*p++); |
| 129 | if (tmp != (u32)-1 && tmp1 != (u32)-1) { |
| 130 | iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET; |
| 131 | iap->ia_atime.tv_sec = tmp; |
| 132 | iap->ia_atime.tv_nsec = tmp1 * 1000; |
| 133 | } |
| 134 | tmp = ntohl(*p++); tmp1 = ntohl(*p++); |
| 135 | if (tmp != (u32)-1 && tmp1 != (u32)-1) { |
| 136 | iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET; |
| 137 | iap->ia_mtime.tv_sec = tmp; |
| 138 | iap->ia_mtime.tv_nsec = tmp1 * 1000; |
| 139 | /* |
| 140 | * Passing the invalid value useconds=1000000 for mtime |
| 141 | * is a Sun convention for "set both mtime and atime to |
| 142 | * current server time". It's needed to make permissions |
| 143 | * checks for the "touch" program across v2 mounts to |
| 144 | * Solaris and Irix boxes work correctly. See description of |
| 145 | * sattr in section 6.1 of "NFS Illustrated" by |
| 146 | * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5 |
| 147 | */ |
| 148 | if (tmp1 == 1000000) |
| 149 | iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET); |
| 150 | } |
| 151 | return p; |
| 152 | } |
| 153 | |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 154 | static __be32 * |
| 155 | encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 156 | struct kstat *stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | struct dentry *dentry = fhp->fh_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | int type; |
| 160 | struct timespec time; |
| 161 | |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 162 | type = (stat->mode & S_IFMT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | *p++ = htonl(nfs_ftypes[type >> 12]); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 165 | *p++ = htonl((u32) stat->mode); |
| 166 | *p++ = htonl((u32) stat->nlink); |
| 167 | *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid)); |
| 168 | *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 170 | if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | *p++ = htonl(NFS_MAXPATHLEN); |
| 172 | } else { |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 173 | *p++ = htonl((u32) stat->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 175 | *p++ = htonl((u32) stat->blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (S_ISCHR(type) || S_ISBLK(type)) |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 177 | *p++ = htonl(new_encode_dev(stat->rdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | else |
| 179 | *p++ = htonl(0xffffffff); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 180 | *p++ = htonl((u32) stat->blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (is_fsid(fhp, rqstp->rq_reffh)) |
| 182 | *p++ = htonl((u32) fhp->fh_export->ex_fsid); |
| 183 | else |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 184 | *p++ = htonl(new_encode_dev(stat->dev)); |
| 185 | *p++ = htonl((u32) stat->ino); |
| 186 | *p++ = htonl((u32) stat->atime.tv_sec); |
| 187 | *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | lease_get_mtime(dentry->d_inode, &time); |
| 189 | *p++ = htonl((u32) time.tv_sec); |
| 190 | *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 191 | *p++ = htonl((u32) stat->ctime.tv_sec); |
| 192 | *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | return p; |
| 195 | } |
| 196 | |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 197 | /* Helper function for NFSv2 ACL code */ |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 198 | __be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 199 | { |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 200 | struct kstat stat; |
| 201 | vfs_getattr(fhp->fh_export->ex_mnt, fhp->fh_dentry, &stat); |
| 202 | return encode_fattr(rqstp, p, fhp, &stat); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * XDR decode functions |
| 207 | */ |
| 208 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 209 | nfssvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | return xdr_argsize_check(rqstp, p); |
| 212 | } |
| 213 | |
| 214 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 215 | nfssvc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | if (!(p = decode_fh(p, &args->fh))) |
| 218 | return 0; |
| 219 | return xdr_argsize_check(rqstp, p); |
| 220 | } |
| 221 | |
| 222 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 223 | nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | struct nfsd_sattrargs *args) |
| 225 | { |
| 226 | if (!(p = decode_fh(p, &args->fh)) |
| 227 | || !(p = decode_sattr(p, &args->attrs))) |
| 228 | return 0; |
| 229 | |
| 230 | return xdr_argsize_check(rqstp, p); |
| 231 | } |
| 232 | |
| 233 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 234 | nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | struct nfsd_diropargs *args) |
| 236 | { |
| 237 | if (!(p = decode_fh(p, &args->fh)) |
| 238 | || !(p = decode_filename(p, &args->name, &args->len))) |
| 239 | return 0; |
| 240 | |
| 241 | return xdr_argsize_check(rqstp, p); |
| 242 | } |
| 243 | |
| 244 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 245 | nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | struct nfsd_readargs *args) |
| 247 | { |
| 248 | unsigned int len; |
| 249 | int v,pn; |
| 250 | if (!(p = decode_fh(p, &args->fh))) |
| 251 | return 0; |
| 252 | |
| 253 | args->offset = ntohl(*p++); |
| 254 | len = args->count = ntohl(*p++); |
| 255 | p++; /* totalcount - unused */ |
| 256 | |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 257 | if (len > NFSSVC_MAXBLKSIZE_V2) |
| 258 | len = NFSSVC_MAXBLKSIZE_V2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | /* set up somewhere to store response. |
| 261 | * We take pages, put them on reslist and include in iovec |
| 262 | */ |
| 263 | v=0; |
| 264 | while (len > 0) { |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 265 | pn = rqstp->rq_resused++; |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 266 | rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]); |
| 267 | rqstp->rq_vec[v].iov_len = len < PAGE_SIZE?len:PAGE_SIZE; |
| 268 | len -= rqstp->rq_vec[v].iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | v++; |
| 270 | } |
| 271 | args->vlen = v; |
| 272 | return xdr_argsize_check(rqstp, p); |
| 273 | } |
| 274 | |
| 275 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 276 | nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | struct nfsd_writeargs *args) |
| 278 | { |
| 279 | unsigned int len; |
| 280 | int v; |
| 281 | if (!(p = decode_fh(p, &args->fh))) |
| 282 | return 0; |
| 283 | |
| 284 | p++; /* beginoffset */ |
| 285 | args->offset = ntohl(*p++); /* offset */ |
| 286 | p++; /* totalcount */ |
| 287 | len = args->len = ntohl(*p++); |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 288 | rqstp->rq_vec[0].iov_base = (void*)p; |
| 289 | rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | (((void*)p) - rqstp->rq_arg.head[0].iov_base); |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 291 | if (len > NFSSVC_MAXBLKSIZE_V2) |
| 292 | len = NFSSVC_MAXBLKSIZE_V2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | v = 0; |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 294 | while (len > rqstp->rq_vec[v].iov_len) { |
| 295 | len -= rqstp->rq_vec[v].iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | v++; |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 297 | rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]); |
| 298 | rqstp->rq_vec[v].iov_len = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 300 | rqstp->rq_vec[v].iov_len = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | args->vlen = v+1; |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 302 | return rqstp->rq_vec[0].iov_len > 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 306 | nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | struct nfsd_createargs *args) |
| 308 | { |
| 309 | if (!(p = decode_fh(p, &args->fh)) |
| 310 | || !(p = decode_filename(p, &args->name, &args->len)) |
| 311 | || !(p = decode_sattr(p, &args->attrs))) |
| 312 | return 0; |
| 313 | |
| 314 | return xdr_argsize_check(rqstp, p); |
| 315 | } |
| 316 | |
| 317 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 318 | nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | struct nfsd_renameargs *args) |
| 320 | { |
| 321 | if (!(p = decode_fh(p, &args->ffh)) |
| 322 | || !(p = decode_filename(p, &args->fname, &args->flen)) |
| 323 | || !(p = decode_fh(p, &args->tfh)) |
| 324 | || !(p = decode_filename(p, &args->tname, &args->tlen))) |
| 325 | return 0; |
| 326 | |
| 327 | return xdr_argsize_check(rqstp, p); |
| 328 | } |
| 329 | |
| 330 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 331 | nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd_readlinkargs *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | if (!(p = decode_fh(p, &args->fh))) |
| 334 | return 0; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 335 | args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | return xdr_argsize_check(rqstp, p); |
| 338 | } |
| 339 | |
| 340 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 341 | nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | struct nfsd_linkargs *args) |
| 343 | { |
| 344 | if (!(p = decode_fh(p, &args->ffh)) |
| 345 | || !(p = decode_fh(p, &args->tfh)) |
| 346 | || !(p = decode_filename(p, &args->tname, &args->tlen))) |
| 347 | return 0; |
| 348 | |
| 349 | return xdr_argsize_check(rqstp, p); |
| 350 | } |
| 351 | |
| 352 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 353 | nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | struct nfsd_symlinkargs *args) |
| 355 | { |
| 356 | if (!(p = decode_fh(p, &args->ffh)) |
| 357 | || !(p = decode_filename(p, &args->fname, &args->flen)) |
| 358 | || !(p = decode_pathname(p, &args->tname, &args->tlen)) |
| 359 | || !(p = decode_sattr(p, &args->attrs))) |
| 360 | return 0; |
| 361 | |
| 362 | return xdr_argsize_check(rqstp, p); |
| 363 | } |
| 364 | |
| 365 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 366 | nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | struct nfsd_readdirargs *args) |
| 368 | { |
| 369 | if (!(p = decode_fh(p, &args->fh))) |
| 370 | return 0; |
| 371 | args->cookie = ntohl(*p++); |
| 372 | args->count = ntohl(*p++); |
| 373 | if (args->count > PAGE_SIZE) |
| 374 | args->count = PAGE_SIZE; |
| 375 | |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 376 | args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
| 378 | return xdr_argsize_check(rqstp, p); |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * XDR encode functions |
| 383 | */ |
| 384 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 385 | nfssvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | return xdr_ressize_check(rqstp, p); |
| 388 | } |
| 389 | |
| 390 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 391 | nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | struct nfsd_attrstat *resp) |
| 393 | { |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 394 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return xdr_ressize_check(rqstp, p); |
| 396 | } |
| 397 | |
| 398 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 399 | nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | struct nfsd_diropres *resp) |
| 401 | { |
| 402 | p = encode_fh(p, &resp->fh); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 403 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return xdr_ressize_check(rqstp, p); |
| 405 | } |
| 406 | |
| 407 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 408 | nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | struct nfsd_readlinkres *resp) |
| 410 | { |
| 411 | *p++ = htonl(resp->len); |
| 412 | xdr_ressize_check(rqstp, p); |
| 413 | rqstp->rq_res.page_len = resp->len; |
| 414 | if (resp->len & 3) { |
| 415 | /* need to pad the tail */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | rqstp->rq_res.tail[0].iov_base = p; |
| 417 | *p = 0; |
| 418 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3); |
| 419 | } |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 424 | nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | struct nfsd_readres *resp) |
| 426 | { |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 427 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | *p++ = htonl(resp->count); |
| 429 | xdr_ressize_check(rqstp, p); |
| 430 | |
| 431 | /* now update rqstp->rq_res to reflect data aswell */ |
| 432 | rqstp->rq_res.page_len = resp->count; |
| 433 | if (resp->count & 3) { |
| 434 | /* need to pad the tail */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | rqstp->rq_res.tail[0].iov_base = p; |
| 436 | *p = 0; |
| 437 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3); |
| 438 | } |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 443 | nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | struct nfsd_readdirres *resp) |
| 445 | { |
| 446 | xdr_ressize_check(rqstp, p); |
| 447 | p = resp->buffer; |
| 448 | *p++ = 0; /* no more entries */ |
| 449 | *p++ = htonl((resp->common.err == nfserr_eof)); |
| 450 | rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1; |
| 451 | |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 456 | nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | struct nfsd_statfsres *resp) |
| 458 | { |
| 459 | struct kstatfs *stat = &resp->stats; |
| 460 | |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 461 | *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | *p++ = htonl(stat->f_bsize); |
| 463 | *p++ = htonl(stat->f_blocks); |
| 464 | *p++ = htonl(stat->f_bfree); |
| 465 | *p++ = htonl(stat->f_bavail); |
| 466 | return xdr_ressize_check(rqstp, p); |
| 467 | } |
| 468 | |
| 469 | int |
| 470 | nfssvc_encode_entry(struct readdir_cd *ccd, const char *name, |
| 471 | int namlen, loff_t offset, ino_t ino, unsigned int d_type) |
| 472 | { |
| 473 | struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common); |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 474 | __be32 *p = cd->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | int buflen, slen; |
| 476 | |
| 477 | /* |
| 478 | dprintk("nfsd: entry(%.*s off %ld ino %ld)\n", |
| 479 | namlen, name, offset, ino); |
| 480 | */ |
| 481 | |
| 482 | if (offset > ~((u32) 0)) { |
| 483 | cd->common.err = nfserr_fbig; |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | if (cd->offset) |
| 487 | *cd->offset = htonl(offset); |
| 488 | if (namlen > NFS2_MAXNAMLEN) |
| 489 | namlen = NFS2_MAXNAMLEN;/* truncate filename */ |
| 490 | |
| 491 | slen = XDR_QUADLEN(namlen); |
| 492 | if ((buflen = cd->buflen - slen - 4) < 0) { |
| 493 | cd->common.err = nfserr_toosmall; |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | *p++ = xdr_one; /* mark entry present */ |
| 497 | *p++ = htonl((u32) ino); /* file id */ |
| 498 | p = xdr_encode_array(p, name, namlen);/* name length & name */ |
| 499 | cd->offset = p; /* remember pointer */ |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 500 | *p++ = htonl(~0U); /* offset of next entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | cd->buflen = buflen; |
| 503 | cd->buffer = p; |
| 504 | cd->common.err = nfs_ok; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * XDR release functions |
| 510 | */ |
| 511 | int |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 512 | nfssvc_release_fhandle(struct svc_rqst *rqstp, __be32 *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | struct nfsd_fhandle *resp) |
| 514 | { |
| 515 | fh_put(&resp->fh); |
| 516 | return 1; |
| 517 | } |