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++); |
Benny Halevy | 48a9e2d | 2009-04-01 09:23:20 -0400 | [diff] [blame] | 150 | /* Check minor version is zero or one. */ |
| 151 | if (hdr->minorversion <= 1) { |
| 152 | p++; /* skip callback_ident */ |
| 153 | } else { |
Benny Halevy | b8f2ef8 | 2009-04-01 09:23:19 -0400 | [diff] [blame] | 154 | printk(KERN_WARNING "%s: NFSv4 server callback with " |
| 155 | "illegal minor version %u!\n", |
| 156 | __func__, hdr->minorversion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return htonl(NFS4ERR_MINOR_VERS_MISMATCH); |
| 158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | hdr->nops = ntohl(*p); |
Benny Halevy | b8f2ef8 | 2009-04-01 09:23:19 -0400 | [diff] [blame] | 160 | dprintk("%s: minorversion %d nops %d\n", __func__, |
| 161 | hdr->minorversion, hdr->nops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 165 | static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 167 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | p = read_buf(xdr, 4); |
| 169 | if (unlikely(p == NULL)) |
| 170 | return htonl(NFS4ERR_RESOURCE); |
| 171 | *op = ntohl(*p); |
| 172 | return 0; |
| 173 | } |
| 174 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 175 | 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] | 176 | { |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 177 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | status = decode_fh(xdr, &args->fh); |
| 180 | if (unlikely(status != 0)) |
| 181 | goto out; |
Chuck Lever | 671beed | 2007-12-10 14:58:22 -0500 | [diff] [blame] | 182 | args->addr = svc_addr(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | status = decode_bitmap(xdr, args->bitmap); |
| 184 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 185 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return status; |
| 187 | } |
| 188 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 189 | 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] | 190 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 191 | __be32 *p; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 192 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Chuck Lever | c1d3586 | 2007-12-10 14:58:29 -0500 | [diff] [blame] | 194 | args->addr = svc_addr(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | status = decode_stateid(xdr, &args->stateid); |
| 196 | if (unlikely(status != 0)) |
| 197 | goto out; |
| 198 | p = read_buf(xdr, 4); |
| 199 | if (unlikely(p == NULL)) { |
| 200 | status = htonl(NFS4ERR_RESOURCE); |
| 201 | goto out; |
| 202 | } |
| 203 | args->truncate = ntohl(*p); |
| 204 | status = decode_fh(xdr, &args->fh); |
| 205 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 206 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
Alexey Dobriyan | 3873bc5 | 2006-05-27 03:31:12 +0400 | [diff] [blame] | 207 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 210 | 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] | 211 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 212 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | p = xdr_reserve_space(xdr, 4 + len); |
| 215 | if (unlikely(p == NULL)) |
| 216 | return htonl(NFS4ERR_RESOURCE); |
| 217 | xdr_encode_opaque(p, str, len); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) |
| 222 | #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 223 | 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] | 224 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 225 | __be32 bm[2]; |
| 226 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0); |
| 229 | bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1); |
| 230 | if (bm[1] != 0) { |
| 231 | p = xdr_reserve_space(xdr, 16); |
| 232 | if (unlikely(p == NULL)) |
| 233 | return htonl(NFS4ERR_RESOURCE); |
| 234 | *p++ = htonl(2); |
| 235 | *p++ = bm[0]; |
| 236 | *p++ = bm[1]; |
| 237 | } else if (bm[0] != 0) { |
| 238 | p = xdr_reserve_space(xdr, 12); |
| 239 | if (unlikely(p == NULL)) |
| 240 | return htonl(NFS4ERR_RESOURCE); |
| 241 | *p++ = htonl(1); |
| 242 | *p++ = bm[0]; |
| 243 | } else { |
| 244 | p = xdr_reserve_space(xdr, 8); |
| 245 | if (unlikely(p == NULL)) |
| 246 | return htonl(NFS4ERR_RESOURCE); |
| 247 | *p++ = htonl(0); |
| 248 | } |
| 249 | *savep = p; |
| 250 | return 0; |
| 251 | } |
| 252 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 253 | 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] | 254 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 255 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | if (!(bitmap[0] & FATTR4_WORD0_CHANGE)) |
| 258 | return 0; |
| 259 | p = xdr_reserve_space(xdr, 8); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 260 | if (unlikely(!p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | return htonl(NFS4ERR_RESOURCE); |
| 262 | p = xdr_encode_hyper(p, change); |
| 263 | return 0; |
| 264 | } |
| 265 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 266 | 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] | 267 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 268 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | if (!(bitmap[0] & FATTR4_WORD0_SIZE)) |
| 271 | return 0; |
| 272 | p = xdr_reserve_space(xdr, 8); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 273 | if (unlikely(!p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return htonl(NFS4ERR_RESOURCE); |
| 275 | p = xdr_encode_hyper(p, size); |
| 276 | return 0; |
| 277 | } |
| 278 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 279 | 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] | 280 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 281 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | p = xdr_reserve_space(xdr, 12); |
Harvey Harrison | 90dc7d2 | 2008-02-20 13:03:05 -0800 | [diff] [blame] | 284 | if (unlikely(!p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return htonl(NFS4ERR_RESOURCE); |
| 286 | p = xdr_encode_hyper(p, time->tv_sec); |
| 287 | *p = htonl(time->tv_nsec); |
| 288 | return 0; |
| 289 | } |
| 290 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) |
| 294 | return 0; |
| 295 | return encode_attr_time(xdr,time); |
| 296 | } |
| 297 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 298 | 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] | 299 | { |
| 300 | if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) |
| 301 | return 0; |
| 302 | return encode_attr_time(xdr,time); |
| 303 | } |
| 304 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 305 | 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] | 306 | { |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 307 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | hdr->status = xdr_reserve_space(xdr, 4); |
| 310 | if (unlikely(hdr->status == NULL)) |
| 311 | return htonl(NFS4ERR_RESOURCE); |
| 312 | status = encode_string(xdr, hdr->taglen, hdr->tag); |
| 313 | if (unlikely(status != 0)) |
| 314 | return status; |
| 315 | hdr->nops = xdr_reserve_space(xdr, 4); |
| 316 | if (unlikely(hdr->nops == NULL)) |
| 317 | return htonl(NFS4ERR_RESOURCE); |
| 318 | return 0; |
| 319 | } |
| 320 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 321 | 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] | 322 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 323 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
| 325 | p = xdr_reserve_space(xdr, 8); |
| 326 | if (unlikely(p == NULL)) |
| 327 | return htonl(NFS4ERR_RESOURCE); |
| 328 | *p++ = htonl(op); |
| 329 | *p = res; |
| 330 | return 0; |
| 331 | } |
| 332 | |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 333 | 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] | 334 | { |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 335 | __be32 *savep = NULL; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 336 | __be32 status = res->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
| 338 | if (unlikely(status != 0)) |
| 339 | goto out; |
| 340 | status = encode_attr_bitmap(xdr, res->bitmap, &savep); |
| 341 | if (unlikely(status != 0)) |
| 342 | goto out; |
| 343 | status = encode_attr_change(xdr, res->bitmap, res->change_attr); |
| 344 | if (unlikely(status != 0)) |
| 345 | goto out; |
| 346 | status = encode_attr_size(xdr, res->bitmap, res->size); |
| 347 | if (unlikely(status != 0)) |
| 348 | goto out; |
| 349 | status = encode_attr_ctime(xdr, res->bitmap, &res->ctime); |
| 350 | if (unlikely(status != 0)) |
| 351 | goto out; |
| 352 | status = encode_attr_mtime(xdr, res->bitmap, &res->mtime); |
| 353 | *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1))); |
| 354 | out: |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 355 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return status; |
| 357 | } |
| 358 | |
Benny Halevy | 34bc47c9 | 2009-04-01 09:23:22 -0400 | [diff] [blame^] | 359 | #if defined(CONFIG_NFS_V4_1) |
| 360 | |
| 361 | static __be32 |
| 362 | preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) |
| 363 | { |
| 364 | switch (op_nr) { |
| 365 | case OP_CB_GETATTR: |
| 366 | case OP_CB_RECALL: |
| 367 | *op = &callback_ops[op_nr]; |
| 368 | break; |
| 369 | |
| 370 | case OP_CB_LAYOUTRECALL: |
| 371 | case OP_CB_NOTIFY_DEVICEID: |
| 372 | case OP_CB_NOTIFY: |
| 373 | case OP_CB_PUSH_DELEG: |
| 374 | case OP_CB_RECALL_ANY: |
| 375 | case OP_CB_RECALLABLE_OBJ_AVAIL: |
| 376 | case OP_CB_RECALL_SLOT: |
| 377 | case OP_CB_SEQUENCE: |
| 378 | case OP_CB_WANTS_CANCELLED: |
| 379 | case OP_CB_NOTIFY_LOCK: |
| 380 | return htonl(NFS4ERR_NOTSUPP); |
| 381 | |
| 382 | default: |
| 383 | return htonl(NFS4ERR_OP_ILLEGAL); |
| 384 | } |
| 385 | |
| 386 | return htonl(NFS_OK); |
| 387 | } |
| 388 | |
| 389 | #else /* CONFIG_NFS_V4_1 */ |
| 390 | |
| 391 | static __be32 |
| 392 | preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) |
| 393 | { |
| 394 | return htonl(NFS4ERR_MINOR_VERS_MISMATCH); |
| 395 | } |
| 396 | |
| 397 | #endif /* CONFIG_NFS_V4_1 */ |
| 398 | |
| 399 | static __be32 |
| 400 | preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op) |
| 401 | { |
| 402 | switch (op_nr) { |
| 403 | case OP_CB_GETATTR: |
| 404 | case OP_CB_RECALL: |
| 405 | *op = &callback_ops[op_nr]; |
| 406 | break; |
| 407 | default: |
| 408 | return htonl(NFS4ERR_OP_ILLEGAL); |
| 409 | } |
| 410 | |
| 411 | return htonl(NFS_OK); |
| 412 | } |
| 413 | |
| 414 | static __be32 process_op(uint32_t minorversion, int nop, |
| 415 | struct svc_rqst *rqstp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | struct xdr_stream *xdr_in, void *argp, |
| 417 | struct xdr_stream *xdr_out, void *resp) |
| 418 | { |
Trond Myklebust | a162a6b | 2006-03-20 13:44:10 -0500 | [diff] [blame] | 419 | struct callback_op *op = &callback_ops[0]; |
| 420 | unsigned int op_nr = OP_CB_ILLEGAL; |
Benny Halevy | 34bc47c9 | 2009-04-01 09:23:22 -0400 | [diff] [blame^] | 421 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | long maxlen; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 423 | __be32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 425 | dprintk("%s: start\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | status = decode_op_hdr(xdr_in, &op_nr); |
Benny Halevy | 34bc47c9 | 2009-04-01 09:23:22 -0400 | [diff] [blame^] | 427 | if (unlikely(status)) { |
| 428 | status = htonl(NFS4ERR_OP_ILLEGAL); |
| 429 | goto out; |
Trond Myklebust | a162a6b | 2006-03-20 13:44:10 -0500 | [diff] [blame] | 430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Benny Halevy | 34bc47c9 | 2009-04-01 09:23:22 -0400 | [diff] [blame^] | 432 | dprintk("%s: minorversion=%d nop=%d op_nr=%u\n", |
| 433 | __func__, minorversion, nop, op_nr); |
| 434 | |
| 435 | status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) : |
| 436 | preprocess_nfs4_op(op_nr, &op); |
| 437 | if (status == htonl(NFS4ERR_OP_ILLEGAL)) |
| 438 | op_nr = OP_CB_ILLEGAL; |
| 439 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | maxlen = xdr_out->end - xdr_out->p; |
| 441 | if (maxlen > 0 && maxlen < PAGE_SIZE) { |
| 442 | if (likely(status == 0 && op->decode_args != NULL)) |
| 443 | status = op->decode_args(rqstp, xdr_in, argp); |
| 444 | if (likely(status == 0 && op->process_op != NULL)) |
| 445 | status = op->process_op(argp, resp); |
| 446 | } else |
| 447 | status = htonl(NFS4ERR_RESOURCE); |
| 448 | |
| 449 | res = encode_op_hdr(xdr_out, op_nr, status); |
| 450 | if (status == 0) |
| 451 | status = res; |
| 452 | if (op->encode_res != NULL && status == 0) |
| 453 | status = op->encode_res(rqstp, xdr_out, resp); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 454 | dprintk("%s: done, status = %d\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | return status; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Decode, process and encode a COMPOUND |
| 460 | */ |
Al Viro | 7111c66 | 2006-10-19 23:28:45 -0700 | [diff] [blame] | 461 | 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] | 462 | { |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 463 | struct cb_compound_hdr_arg hdr_arg = { 0 }; |
| 464 | struct cb_compound_hdr_res hdr_res = { NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | struct xdr_stream xdr_in, xdr_out; |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 466 | __be32 *p; |
Al Viro | e6f684f | 2006-10-19 23:28:50 -0700 | [diff] [blame] | 467 | __be32 status; |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 468 | unsigned int nops = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 470 | dprintk("%s: start\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
| 472 | xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base); |
| 473 | |
Al Viro | 5704fde | 2006-10-19 23:28:51 -0700 | [diff] [blame] | 474 | 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] | 475 | xdr_init_encode(&xdr_out, &rqstp->rq_res, p); |
| 476 | |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 477 | status = decode_compound_hdr_arg(&xdr_in, &hdr_arg); |
| 478 | if (status == __constant_htonl(NFS4ERR_RESOURCE)) |
| 479 | return rpc_garbage_args; |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | hdr_res.taglen = hdr_arg.taglen; |
| 482 | hdr_res.tag = hdr_arg.tag; |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 483 | if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) |
| 484 | return rpc_system_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 486 | while (status == 0 && nops != hdr_arg.nops) { |
Benny Halevy | 34bc47c9 | 2009-04-01 09:23:22 -0400 | [diff] [blame^] | 487 | status = process_op(hdr_arg.minorversion, nops, |
| 488 | rqstp, &xdr_in, argp, &xdr_out, resp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | nops++; |
| 490 | } |
Trond Myklebust | 3a6258e | 2008-05-06 13:32:40 -0400 | [diff] [blame] | 491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | *hdr_res.status = status; |
| 493 | *hdr_res.nops = htonl(nops); |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 494 | dprintk("%s: done, status = %u\n", __func__, ntohl(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return rpc_success; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Define NFS4 callback COMPOUND ops. |
| 500 | */ |
| 501 | static struct callback_op callback_ops[] = { |
| 502 | [0] = { |
| 503 | .res_maxsize = CB_OP_HDR_RES_MAXSZ, |
| 504 | }, |
| 505 | [OP_CB_GETATTR] = { |
| 506 | .process_op = (callback_process_op_t)nfs4_callback_getattr, |
| 507 | .decode_args = (callback_decode_arg_t)decode_getattr_args, |
| 508 | .encode_res = (callback_encode_res_t)encode_getattr_res, |
| 509 | .res_maxsize = CB_OP_GETATTR_RES_MAXSZ, |
| 510 | }, |
| 511 | [OP_CB_RECALL] = { |
| 512 | .process_op = (callback_process_op_t)nfs4_callback_recall, |
| 513 | .decode_args = (callback_decode_arg_t)decode_recall_args, |
| 514 | .res_maxsize = CB_OP_RECALL_RES_MAXSZ, |
| 515 | } |
| 516 | }; |
| 517 | |
| 518 | /* |
| 519 | * Define NFS4 callback procedures |
| 520 | */ |
| 521 | static struct svc_procedure nfs4_callback_procedures1[] = { |
| 522 | [CB_NULL] = { |
| 523 | .pc_func = nfs4_callback_null, |
| 524 | .pc_decode = (kxdrproc_t)nfs4_decode_void, |
| 525 | .pc_encode = (kxdrproc_t)nfs4_encode_void, |
| 526 | .pc_xdrressize = 1, |
| 527 | }, |
| 528 | [CB_COMPOUND] = { |
| 529 | .pc_func = nfs4_callback_compound, |
| 530 | .pc_encode = (kxdrproc_t)nfs4_encode_void, |
| 531 | .pc_argsize = 256, |
| 532 | .pc_ressize = 256, |
| 533 | .pc_xdrressize = NFS4_CALLBACK_BUFSIZE, |
| 534 | } |
| 535 | }; |
| 536 | |
| 537 | struct svc_version nfs4_callback_version1 = { |
| 538 | .vs_vers = 1, |
| 539 | .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1), |
| 540 | .vs_proc = nfs4_callback_procedures1, |
| 541 | .vs_xdrsize = NFS4_CALLBACK_XDRSIZE, |
| 542 | .vs_dispatch = NULL, |
| 543 | }; |
| 544 | |