Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/lockd/xdr.c |
| 3 | * |
| 4 | * XDR support for lockd and the lock client. |
| 5 | * |
| 6 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/nfs.h> |
| 12 | |
| 13 | #include <linux/sunrpc/xdr.h> |
| 14 | #include <linux/sunrpc/clnt.h> |
| 15 | #include <linux/sunrpc/svc.h> |
| 16 | #include <linux/sunrpc/stats.h> |
| 17 | #include <linux/lockd/lockd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Christoph Hellwig | 9c69de4 | 2014-05-06 19:37:13 +0200 | [diff] [blame] | 19 | #include <uapi/linux/nfs2.h> |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define NLMDBG_FACILITY NLMDBG_XDR |
| 22 | |
| 23 | |
| 24 | static inline loff_t |
| 25 | s32_to_loff_t(__s32 offset) |
| 26 | { |
| 27 | return (loff_t)offset; |
| 28 | } |
| 29 | |
| 30 | static inline __s32 |
| 31 | loff_t_to_s32(loff_t offset) |
| 32 | { |
| 33 | __s32 res; |
| 34 | if (offset >= NLM_OFFSET_MAX) |
| 35 | res = NLM_OFFSET_MAX; |
| 36 | else if (offset <= -NLM_OFFSET_MAX) |
| 37 | res = -NLM_OFFSET_MAX; |
| 38 | else |
| 39 | res = offset; |
| 40 | return res; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * XDR functions for basic NLM types |
| 45 | */ |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 46 | static __be32 *nlm_decode_cookie(__be32 *p, struct nlm_cookie *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | unsigned int len; |
| 49 | |
| 50 | len = ntohl(*p++); |
| 51 | |
| 52 | if(len==0) |
| 53 | { |
| 54 | c->len=4; |
| 55 | memset(c->data, 0, 4); /* hockeypux brain damage */ |
| 56 | } |
| 57 | else if(len<=NLM_MAXCOOKIELEN) |
| 58 | { |
| 59 | c->len=len; |
| 60 | memcpy(c->data, p, len); |
| 61 | p+=XDR_QUADLEN(len); |
| 62 | } |
| 63 | else |
| 64 | { |
Chuck Lever | e159a08 | 2007-09-11 18:01:15 -0400 | [diff] [blame] | 65 | dprintk("lockd: bad cookie size %d (only cookies under " |
| 66 | "%d bytes are supported.)\n", |
| 67 | len, NLM_MAXCOOKIELEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | return p; |
| 71 | } |
| 72 | |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 73 | static inline __be32 * |
| 74 | nlm_encode_cookie(__be32 *p, struct nlm_cookie *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | *p++ = htonl(c->len); |
| 77 | memcpy(p, c->data, c->len); |
| 78 | p+=XDR_QUADLEN(c->len); |
| 79 | return p; |
| 80 | } |
| 81 | |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 82 | static __be32 * |
| 83 | nlm_decode_fh(__be32 *p, struct nfs_fh *f) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | unsigned int len; |
| 86 | |
| 87 | if ((len = ntohl(*p++)) != NFS2_FHSIZE) { |
Chuck Lever | e159a08 | 2007-09-11 18:01:15 -0400 | [diff] [blame] | 88 | dprintk("lockd: bad fhandle size %d (should be %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | len, NFS2_FHSIZE); |
| 90 | return NULL; |
| 91 | } |
| 92 | f->size = NFS2_FHSIZE; |
| 93 | memset(f->data, 0, sizeof(f->data)); |
| 94 | memcpy(f->data, p, NFS2_FHSIZE); |
| 95 | return p + XDR_QUADLEN(NFS2_FHSIZE); |
| 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* |
| 99 | * Encode and decode owner handle |
| 100 | */ |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 101 | static inline __be32 * |
| 102 | nlm_decode_oh(__be32 *p, struct xdr_netobj *oh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | return xdr_decode_netobj(p, oh); |
| 105 | } |
| 106 | |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 107 | static inline __be32 * |
| 108 | nlm_encode_oh(__be32 *p, struct xdr_netobj *oh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | return xdr_encode_netobj(p, oh); |
| 111 | } |
| 112 | |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 113 | static __be32 * |
| 114 | nlm_decode_lock(__be32 *p, struct nlm_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | struct file_lock *fl = &lock->fl; |
| 117 | s32 start, len, end; |
| 118 | |
| 119 | if (!(p = xdr_decode_string_inplace(p, &lock->caller, |
| 120 | &lock->len, |
| 121 | NLM_MAXSTRLEN)) |
| 122 | || !(p = nlm_decode_fh(p, &lock->fh)) |
| 123 | || !(p = nlm_decode_oh(p, &lock->oh))) |
| 124 | return NULL; |
Trond Myklebust | 7bab377 | 2006-03-20 13:44:06 -0500 | [diff] [blame] | 125 | lock->svid = ntohl(*p++); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | locks_init_lock(fl); |
| 128 | fl->fl_owner = current->files; |
Trond Myklebust | 7bab377 | 2006-03-20 13:44:06 -0500 | [diff] [blame] | 129 | fl->fl_pid = (pid_t)lock->svid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | fl->fl_flags = FL_POSIX; |
| 131 | fl->fl_type = F_RDLCK; /* as good as anything else */ |
| 132 | start = ntohl(*p++); |
| 133 | len = ntohl(*p++); |
| 134 | end = start + len - 1; |
| 135 | |
| 136 | fl->fl_start = s32_to_loff_t(start); |
| 137 | |
| 138 | if (len == 0 || end < 0) |
| 139 | fl->fl_end = OFFSET_MAX; |
| 140 | else |
| 141 | fl->fl_end = s32_to_loff_t(end); |
| 142 | return p; |
| 143 | } |
| 144 | |
| 145 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | * Encode result of a TEST/TEST_MSG call |
| 147 | */ |
Al Viro | 52921e0 | 2006-10-19 23:28:46 -0700 | [diff] [blame] | 148 | static __be32 * |
| 149 | nlm_encode_testres(__be32 *p, struct nlm_res *resp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | s32 start, len; |
| 152 | |
| 153 | if (!(p = nlm_encode_cookie(p, &resp->cookie))) |
| 154 | return NULL; |
| 155 | *p++ = resp->status; |
| 156 | |
| 157 | if (resp->status == nlm_lck_denied) { |
| 158 | struct file_lock *fl = &resp->lock.fl; |
| 159 | |
| 160 | *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one; |
Trond Myklebust | 7bab377 | 2006-03-20 13:44:06 -0500 | [diff] [blame] | 161 | *p++ = htonl(resp->lock.svid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | /* Encode owner handle. */ |
| 164 | if (!(p = xdr_encode_netobj(p, &resp->lock.oh))) |
| 165 | return NULL; |
| 166 | |
| 167 | start = loff_t_to_s32(fl->fl_start); |
| 168 | if (fl->fl_end == OFFSET_MAX) |
| 169 | len = 0; |
| 170 | else |
| 171 | len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1); |
| 172 | |
| 173 | *p++ = htonl(start); |
| 174 | *p++ = htonl(len); |
| 175 | } |
| 176 | |
| 177 | return p; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * First, the server side XDR functions |
| 183 | */ |
| 184 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 185 | nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 187 | struct nlm_args *argp = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | u32 exclusive; |
| 189 | |
| 190 | if (!(p = nlm_decode_cookie(p, &argp->cookie))) |
| 191 | return 0; |
| 192 | |
| 193 | exclusive = ntohl(*p++); |
| 194 | if (!(p = nlm_decode_lock(p, &argp->lock))) |
| 195 | return 0; |
| 196 | if (exclusive) |
| 197 | argp->lock.fl.fl_type = F_WRLCK; |
| 198 | |
| 199 | return xdr_argsize_check(rqstp, p); |
| 200 | } |
| 201 | |
| 202 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 203 | nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 205 | struct nlm_res *resp = rqstp->rq_resp; |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (!(p = nlm_encode_testres(p, resp))) |
| 208 | return 0; |
| 209 | return xdr_ressize_check(rqstp, p); |
| 210 | } |
| 211 | |
| 212 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 213 | nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 215 | struct nlm_args *argp = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | u32 exclusive; |
| 217 | |
| 218 | if (!(p = nlm_decode_cookie(p, &argp->cookie))) |
| 219 | return 0; |
| 220 | argp->block = ntohl(*p++); |
| 221 | exclusive = ntohl(*p++); |
| 222 | if (!(p = nlm_decode_lock(p, &argp->lock))) |
| 223 | return 0; |
| 224 | if (exclusive) |
| 225 | argp->lock.fl.fl_type = F_WRLCK; |
| 226 | argp->reclaim = ntohl(*p++); |
| 227 | argp->state = ntohl(*p++); |
| 228 | argp->monitor = 1; /* monitor client by default */ |
| 229 | |
| 230 | return xdr_argsize_check(rqstp, p); |
| 231 | } |
| 232 | |
| 233 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 234 | nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 236 | struct nlm_args *argp = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | u32 exclusive; |
| 238 | |
| 239 | if (!(p = nlm_decode_cookie(p, &argp->cookie))) |
| 240 | return 0; |
| 241 | argp->block = ntohl(*p++); |
| 242 | exclusive = ntohl(*p++); |
| 243 | if (!(p = nlm_decode_lock(p, &argp->lock))) |
| 244 | return 0; |
| 245 | if (exclusive) |
| 246 | argp->lock.fl.fl_type = F_WRLCK; |
| 247 | return xdr_argsize_check(rqstp, p); |
| 248 | } |
| 249 | |
| 250 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 251 | nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 253 | struct nlm_args *argp = rqstp->rq_argp; |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (!(p = nlm_decode_cookie(p, &argp->cookie)) |
| 256 | || !(p = nlm_decode_lock(p, &argp->lock))) |
| 257 | return 0; |
| 258 | argp->lock.fl.fl_type = F_UNLCK; |
| 259 | return xdr_argsize_check(rqstp, p); |
| 260 | } |
| 261 | |
| 262 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 263 | nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 265 | struct nlm_args *argp = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | struct nlm_lock *lock = &argp->lock; |
| 267 | |
| 268 | memset(lock, 0, sizeof(*lock)); |
| 269 | locks_init_lock(&lock->fl); |
Trond Myklebust | 7bab377 | 2006-03-20 13:44:06 -0500 | [diff] [blame] | 270 | lock->svid = ~(u32) 0; |
| 271 | lock->fl.fl_pid = (pid_t)lock->svid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | if (!(p = nlm_decode_cookie(p, &argp->cookie)) |
| 274 | || !(p = xdr_decode_string_inplace(p, &lock->caller, |
| 275 | &lock->len, NLM_MAXSTRLEN)) |
| 276 | || !(p = nlm_decode_fh(p, &lock->fh)) |
| 277 | || !(p = nlm_decode_oh(p, &lock->oh))) |
| 278 | return 0; |
| 279 | argp->fsm_mode = ntohl(*p++); |
| 280 | argp->fsm_access = ntohl(*p++); |
| 281 | return xdr_argsize_check(rqstp, p); |
| 282 | } |
| 283 | |
| 284 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 285 | nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 287 | struct nlm_res *resp = rqstp->rq_resp; |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | if (!(p = nlm_encode_cookie(p, &resp->cookie))) |
| 290 | return 0; |
| 291 | *p++ = resp->status; |
| 292 | *p++ = xdr_zero; /* sequence argument */ |
| 293 | return xdr_ressize_check(rqstp, p); |
| 294 | } |
| 295 | |
| 296 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 297 | nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 299 | struct nlm_res *resp = rqstp->rq_resp; |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (!(p = nlm_encode_cookie(p, &resp->cookie))) |
| 302 | return 0; |
| 303 | *p++ = resp->status; |
| 304 | return xdr_ressize_check(rqstp, p); |
| 305 | } |
| 306 | |
| 307 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 308 | nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 310 | struct nlm_args *argp = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | struct nlm_lock *lock = &argp->lock; |
| 312 | |
| 313 | if (!(p = xdr_decode_string_inplace(p, &lock->caller, |
| 314 | &lock->len, NLM_MAXSTRLEN))) |
| 315 | return 0; |
| 316 | argp->state = ntohl(*p++); |
| 317 | return xdr_argsize_check(rqstp, p); |
| 318 | } |
| 319 | |
| 320 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 321 | nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 323 | struct nlm_reboot *argp = rqstp->rq_argp; |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN))) |
| 326 | return 0; |
| 327 | argp->state = ntohl(*p++); |
Chuck Lever | 576df46 | 2008-12-05 19:03:39 -0500 | [diff] [blame] | 328 | memcpy(&argp->priv.data, p, sizeof(argp->priv.data)); |
| 329 | p += XDR_QUADLEN(SM_PRIV_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return xdr_argsize_check(rqstp, p); |
| 331 | } |
| 332 | |
| 333 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 334 | nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 336 | struct nlm_res *resp = rqstp->rq_argp; |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (!(p = nlm_decode_cookie(p, &resp->cookie))) |
| 339 | return 0; |
Al Viro | e8c5c04 | 2006-12-13 00:35:03 -0800 | [diff] [blame] | 340 | resp->status = *p++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return xdr_argsize_check(rqstp, p); |
| 342 | } |
| 343 | |
| 344 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 345 | nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
| 347 | return xdr_argsize_check(rqstp, p); |
| 348 | } |
| 349 | |
| 350 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 351 | nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
| 353 | return xdr_ressize_check(rqstp, p); |
| 354 | } |