Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/callback_xdr.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Trond Myklebust |
| 5 | * |
| 6 | * NFSv4 callback encode/decode procedures |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/sunrpc/svc.h> |
| 10 | #include <linux/nfs4.h> |
| 11 | #include <linux/nfs_fs.h> |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 12 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "callback.h" |
| 14 | |
| 15 | #define CB_OP_TAGLEN_MAXSZ (512) |
| 16 | #define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ) |
| 17 | #define CB_OP_GETATTR_BITMAP_MAXSZ (4) |
| 18 | #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ |
| 19 | CB_OP_GETATTR_BITMAP_MAXSZ + \ |
| 20 | 2 + 2 + 3 + 3) |
| 21 | #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) |
| 22 | |
| 23 | #define NFSDBG_FACILITY NFSDBG_CALLBACK |
| 24 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 25 | typedef __be32 (*callback_process_op_t)(void *, void *); |
| 26 | typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *); |
| 27 | typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | struct callback_op { |
| 31 | callback_process_op_t process_op; |
| 32 | callback_decode_arg_t decode_args; |
| 33 | callback_encode_res_t encode_res; |
| 34 | long res_maxsize; |
| 35 | }; |
| 36 | |
| 37 | static struct callback_op callback_ops[]; |
| 38 | |
Al Viro | 7111c66 | 2006-10-19 23:28:45 -0700 | [diff] [blame] | 39 | static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | return htonl(NFS4_OK); |
| 42 | } |
| 43 | |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 44 | static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | return xdr_argsize_check(rqstp, p); |
| 47 | } |
| 48 | |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 49 | static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | return xdr_ressize_check(rqstp, p); |
| 52 | } |
| 53 | |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 54 | static __be32 *read_buf(struct xdr_stream *xdr, int nbytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 56 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | p = xdr_inline_decode(xdr, nbytes); |
| 59 | if (unlikely(p == NULL)) |
| 60 | printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n"); |
| 61 | return p; |
| 62 | } |
| 63 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 64 | static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 66 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | p = read_buf(xdr, 4); |
| 69 | if (unlikely(p == NULL)) |
| 70 | return htonl(NFS4ERR_RESOURCE); |
| 71 | *len = ntohl(*p); |
| 72 | |
| 73 | if (*len != 0) { |
| 74 | p = read_buf(xdr, *len); |
| 75 | if (unlikely(p == NULL)) |
| 76 | return htonl(NFS4ERR_RESOURCE); |
| 77 | *str = (const char *)p; |
| 78 | } else |
| 79 | *str = NULL; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 84 | static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 86 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | p = read_buf(xdr, 4); |
| 89 | if (unlikely(p == NULL)) |
| 90 | return htonl(NFS4ERR_RESOURCE); |
| 91 | fh->size = ntohl(*p); |
| 92 | if (fh->size > NFS4_FHSIZE) |
| 93 | return htonl(NFS4ERR_BADHANDLE); |
| 94 | p = read_buf(xdr, fh->size); |
| 95 | if (unlikely(p == NULL)) |
| 96 | return htonl(NFS4ERR_RESOURCE); |
| 97 | memcpy(&fh->data[0], p, fh->size); |
| 98 | memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size); |
| 99 | return 0; |
| 100 | } |
| 101 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 102 | static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 104 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | unsigned int attrlen; |
| 106 | |
| 107 | p = read_buf(xdr, 4); |
| 108 | if (unlikely(p == NULL)) |
| 109 | return htonl(NFS4ERR_RESOURCE); |
| 110 | attrlen = ntohl(*p); |
| 111 | p = read_buf(xdr, attrlen << 2); |
| 112 | if (unlikely(p == NULL)) |
| 113 | return htonl(NFS4ERR_RESOURCE); |
| 114 | if (likely(attrlen > 0)) |
| 115 | bitmap[0] = ntohl(*p++); |
| 116 | if (attrlen > 1) |
| 117 | bitmap[1] = ntohl(*p); |
| 118 | return 0; |
| 119 | } |
| 120 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 121 | static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 123 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | p = read_buf(xdr, 16); |
| 126 | if (unlikely(p == NULL)) |
| 127 | return htonl(NFS4ERR_RESOURCE); |
| 128 | memcpy(stateid->data, p, 16); |
| 129 | return 0; |
| 130 | } |
| 131 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 132 | static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 134 | __be32 *p; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 135 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | status = decode_string(xdr, &hdr->taglen, &hdr->tag); |
| 138 | if (unlikely(status != 0)) |
| 139 | return status; |
| 140 | /* We do not like overly long tags! */ |
Chuck Lever | 5cce428 | 2007-10-26 13:33:01 -0400 | [diff] [blame] | 141 | if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | printk("NFSv4 CALLBACK %s: client sent tag of length %u\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 143 | __func__, hdr->taglen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return htonl(NFS4ERR_RESOURCE); |
| 145 | } |
| 146 | p = read_buf(xdr, 12); |
| 147 | if (unlikely(p == NULL)) |
| 148 | return htonl(NFS4ERR_RESOURCE); |
Benny Halevy | b8f2ef8 | 2009-04-01 09:23:19 -0400 | [diff] [blame^] | 149 | hdr->minorversion = ntohl(*p++); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* Check minor version is zero. */ |
Benny Halevy | b8f2ef8 | 2009-04-01 09:23:19 -0400 | [diff] [blame^] | 151 | if (hdr->minorversion != 0) { |
| 152 | printk(KERN_WARNING "%s: NFSv4 server callback with " |
| 153 | "illegal minor version %u!\n", |
| 154 | __func__, hdr->minorversion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return htonl(NFS4ERR_MINOR_VERS_MISMATCH); |
| 156 | } |
| 157 | hdr->callback_ident = ntohl(*p++); |
| 158 | hdr->nops = ntohl(*p); |
Benny Halevy | b8f2ef8 | 2009-04-01 09:23:19 -0400 | [diff] [blame^] | 159 | dprintk("%s: minorversion %d nops %d\n", __func__, |
| 160 | hdr->minorversion, hdr->nops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 164 | static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 166 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | p = read_buf(xdr, 4); |
| 168 | if (unlikely(p == NULL)) |
| 169 | return htonl(NFS4ERR_RESOURCE); |
| 170 | *op = ntohl(*p); |
| 171 | return 0; |
| 172 | } |
| 173 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 174 | static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 176 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | status = decode_fh(xdr, &args->fh); |
| 179 | if (unlikely(status != 0)) |
| 180 | goto out; |
Chuck Lever | 671beed | 2007-12-10 14:58:22 -0500 | [diff] [blame] | 181 | args->addr = svc_addr(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | status = decode_bitmap(xdr, args->bitmap); |
| 183 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 184 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return status; |
| 186 | } |
| 187 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 188 | static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 190 | __be32 *p; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 191 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Chuck Lever | c1d3586 | 2007-12-10 14:58:29 -0500 | [diff] [blame] | 193 | args->addr = svc_addr(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | status = decode_stateid(xdr, &args->stateid); |
| 195 | if (unlikely(status != 0)) |
| 196 | goto out; |
| 197 | p = read_buf(xdr, 4); |
| 198 | if (unlikely(p == NULL)) { |
| 199 | status = htonl(NFS4ERR_RESOURCE); |
| 200 | goto out; |
| 201 | } |
| 202 | args->truncate = ntohl(*p); |
| 203 | status = decode_fh(xdr, &args->fh); |
| 204 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 205 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
Alexey Dobriyan | 3873bc5 | 2006-05-27 03:31:12 +0400 | [diff] [blame] | 206 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 209 | static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 211 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | p = xdr_reserve_space(xdr, 4 + len); |
| 214 | if (unlikely(p == NULL)) |
| 215 | return htonl(NFS4ERR_RESOURCE); |
| 216 | xdr_encode_opaque(p, str, len); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) |
| 221 | #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 222 | static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 224 | __be32 bm[2]; |
| 225 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0); |
| 228 | bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1); |
| 229 | if (bm[1] != 0) { |
| 230 | p = xdr_reserve_space(xdr, 16); |
| 231 | if (unlikely(p == NULL)) |
| 232 | return htonl(NFS4ERR_RESOURCE); |
| 233 | *p++ = htonl(2); |
| 234 | *p++ = bm[0]; |
| 235 | *p++ = bm[1]; |
| 236 | } else if (bm[0] != 0) { |
| 237 | p = xdr_reserve_space(xdr, 12); |
| 238 | if (unlikely(p == NULL)) |
| 239 | return htonl(NFS4ERR_RESOURCE); |
| 240 | *p++ = htonl(1); |
| 241 | *p++ = bm[0]; |
| 242 | } else { |
| 243 | p = xdr_reserve_space(xdr, 8); |
| 244 | if (unlikely(p == NULL)) |
| 245 | return htonl(NFS4ERR_RESOURCE); |
| 246 | *p++ = htonl(0); |
| 247 | } |
| 248 | *savep = p; |
| 249 | return 0; |
| 250 | } |
| 251 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 252 | static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 254 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | if (!(bitmap[0] & FATTR4_WORD0_CHANGE)) |
| 257 | return 0; |
| 258 | p = xdr_reserve_space(xdr, 8); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 259 | if (unlikely(!p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return htonl(NFS4ERR_RESOURCE); |
| 261 | p = xdr_encode_hyper(p, change); |
| 262 | return 0; |
| 263 | } |
| 264 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 265 | static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 267 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | if (!(bitmap[0] & FATTR4_WORD0_SIZE)) |
| 270 | return 0; |
| 271 | p = xdr_reserve_space(xdr, 8); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 272 | if (unlikely(!p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return htonl(NFS4ERR_RESOURCE); |
| 274 | p = xdr_encode_hyper(p, size); |
| 275 | return 0; |
| 276 | } |
| 277 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 278 | static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 280 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | p = xdr_reserve_space(xdr, 12); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 283 | if (unlikely(!p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return htonl(NFS4ERR_RESOURCE); |
| 285 | p = xdr_encode_hyper(p, time->tv_sec); |
| 286 | *p = htonl(time->tv_nsec); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 290 | static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) |
| 293 | return 0; |
| 294 | return encode_attr_time(xdr,time); |
| 295 | } |
| 296 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 297 | static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
| 299 | if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) |
| 300 | return 0; |
| 301 | return encode_attr_time(xdr,time); |
| 302 | } |
| 303 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 304 | static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 306 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
| 308 | hdr->status = xdr_reserve_space(xdr, 4); |
| 309 | if (unlikely(hdr->status == NULL)) |
| 310 | return htonl(NFS4ERR_RESOURCE); |
| 311 | status = encode_string(xdr, hdr->taglen, hdr->tag); |
| 312 | if (unlikely(status != 0)) |
| 313 | return status; |
| 314 | hdr->nops = xdr_reserve_space(xdr, 4); |
| 315 | if (unlikely(hdr->nops == NULL)) |
| 316 | return htonl(NFS4ERR_RESOURCE); |
| 317 | return 0; |
| 318 | } |
| 319 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 320 | static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 322 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
| 324 | p = xdr_reserve_space(xdr, 8); |
| 325 | if (unlikely(p == NULL)) |
| 326 | return htonl(NFS4ERR_RESOURCE); |
| 327 | *p++ = htonl(op); |
| 328 | *p = res; |
| 329 | return 0; |
| 330 | } |
| 331 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 332 | static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 334 | __be32 *savep = NULL; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 335 | __be32 status = res->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | if (unlikely(status != 0)) |
| 338 | goto out; |
| 339 | status = encode_attr_bitmap(xdr, res->bitmap, &savep); |
| 340 | if (unlikely(status != 0)) |
| 341 | goto out; |
| 342 | status = encode_attr_change(xdr, res->bitmap, res->change_attr); |
| 343 | if (unlikely(status != 0)) |
| 344 | goto out; |
| 345 | status = encode_attr_size(xdr, res->bitmap, res->size); |
| 346 | if (unlikely(status != 0)) |
| 347 | goto out; |
| 348 | status = encode_attr_ctime(xdr, res->bitmap, &res->ctime); |
| 349 | if (unlikely(status != 0)) |
| 350 | goto out; |
| 351 | status = encode_attr_mtime(xdr, res->bitmap, &res->mtime); |
| 352 | *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1))); |
| 353 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 354 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return status; |
| 356 | } |
| 357 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 358 | static __be32 process_op(struct svc_rqst *rqstp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | struct xdr_stream *xdr_in, void *argp, |
| 360 | struct xdr_stream *xdr_out, void *resp) |
| 361 | { |
Trond Myklebust | a162a6b | 2006-03-20 13:44:10 -0500 | [diff] [blame] | 362 | struct callback_op *op = &callback_ops[0]; |
| 363 | unsigned int op_nr = OP_CB_ILLEGAL; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 364 | __be32 status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | long maxlen; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 366 | __be32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 368 | dprintk("%s: start\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | status = decode_op_hdr(xdr_in, &op_nr); |
Trond Myklebust | a162a6b | 2006-03-20 13:44:10 -0500 | [diff] [blame] | 370 | if (likely(status == 0)) { |
| 371 | switch (op_nr) { |
| 372 | case OP_CB_GETATTR: |
| 373 | case OP_CB_RECALL: |
| 374 | op = &callback_ops[op_nr]; |
| 375 | break; |
| 376 | default: |
| 377 | op_nr = OP_CB_ILLEGAL; |
| 378 | op = &callback_ops[0]; |
| 379 | status = htonl(NFS4ERR_OP_ILLEGAL); |
| 380 | } |
| 381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | maxlen = xdr_out->end - xdr_out->p; |
| 384 | if (maxlen > 0 && maxlen < PAGE_SIZE) { |
| 385 | if (likely(status == 0 && op->decode_args != NULL)) |
| 386 | status = op->decode_args(rqstp, xdr_in, argp); |
| 387 | if (likely(status == 0 && op->process_op != NULL)) |
| 388 | status = op->process_op(argp, resp); |
| 389 | } else |
| 390 | status = htonl(NFS4ERR_RESOURCE); |
| 391 | |
| 392 | res = encode_op_hdr(xdr_out, op_nr, status); |
| 393 | if (status == 0) |
| 394 | status = res; |
| 395 | if (op->encode_res != NULL && status == 0) |
| 396 | status = op->encode_res(rqstp, xdr_out, resp); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 397 | dprintk("%s: done, status = %d\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | return status; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Decode, process and encode a COMPOUND |
| 403 | */ |
Al Viro | 7111c66 | 2006-10-19 23:28:45 -0700 | [diff] [blame] | 404 | static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 406 | struct cb_compound_hdr_arg hdr_arg = { 0 }; |
| 407 | struct cb_compound_hdr_res hdr_res = { NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | struct xdr_stream xdr_in, xdr_out; |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 409 | __be32 *p; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 410 | __be32 status; |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 411 | unsigned int nops = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 413 | dprintk("%s: start\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
| 415 | xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base); |
| 416 | |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 417 | p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | xdr_init_encode(&xdr_out, &rqstp->rq_res, p); |
| 419 | |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 420 | status = decode_compound_hdr_arg(&xdr_in, &hdr_arg); |
| 421 | if (status == __constant_htonl(NFS4ERR_RESOURCE)) |
| 422 | return rpc_garbage_args; |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | hdr_res.taglen = hdr_arg.taglen; |
| 425 | hdr_res.tag = hdr_arg.tag; |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 426 | if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) |
| 427 | return rpc_system_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 429 | while (status == 0 && nops != hdr_arg.nops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | status = process_op(rqstp, &xdr_in, argp, &xdr_out, resp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | nops++; |
| 432 | } |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | *hdr_res.status = status; |
| 435 | *hdr_res.nops = htonl(nops); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 436 | dprintk("%s: done, status = %u\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return rpc_success; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Define NFS4 callback COMPOUND ops. |
| 442 | */ |
| 443 | static struct callback_op callback_ops[] = { |
| 444 | [0] = { |
| 445 | .res_maxsize = CB_OP_HDR_RES_MAXSZ, |
| 446 | }, |
| 447 | [OP_CB_GETATTR] = { |
| 448 | .process_op = (callback_process_op_t)nfs4_callback_getattr, |
| 449 | .decode_args = (callback_decode_arg_t)decode_getattr_args, |
| 450 | .encode_res = (callback_encode_res_t)encode_getattr_res, |
| 451 | .res_maxsize = CB_OP_GETATTR_RES_MAXSZ, |
| 452 | }, |
| 453 | [OP_CB_RECALL] = { |
| 454 | .process_op = (callback_process_op_t)nfs4_callback_recall, |
| 455 | .decode_args = (callback_decode_arg_t)decode_recall_args, |
| 456 | .res_maxsize = CB_OP_RECALL_RES_MAXSZ, |
| 457 | } |
| 458 | }; |
| 459 | |
| 460 | /* |
| 461 | * Define NFS4 callback procedures |
| 462 | */ |
| 463 | static struct svc_procedure nfs4_callback_procedures1[] = { |
| 464 | [CB_NULL] = { |
| 465 | .pc_func = nfs4_callback_null, |
| 466 | .pc_decode = (kxdrproc_t)nfs4_decode_void, |
| 467 | .pc_encode = (kxdrproc_t)nfs4_encode_void, |
| 468 | .pc_xdrressize = 1, |
| 469 | }, |
| 470 | [CB_COMPOUND] = { |
| 471 | .pc_func = nfs4_callback_compound, |
| 472 | .pc_encode = (kxdrproc_t)nfs4_encode_void, |
| 473 | .pc_argsize = 256, |
| 474 | .pc_ressize = 256, |
| 475 | .pc_xdrressize = NFS4_CALLBACK_BUFSIZE, |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | struct svc_version nfs4_callback_version1 = { |
| 480 | .vs_vers = 1, |
| 481 | .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), |
| 482 | .vs_proc = nfs4_callback_procedures1, |
| 483 | .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, |
| 484 | .vs_dispatch = NULL, |
| 485 | }; |
| 486 | |