Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / fs / lockd / xdr.c
blob062191b9a333ce2f84d589dbb0172be51d31dbc2
1 /*
2 * linux/fs/lockd/xdr.c
4 * XDR support for lockd and the lock client.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/utsname.h>
13 #include <linux/nfs.h>
15 #include <linux/sunrpc/xdr.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/svc.h>
18 #include <linux/sunrpc/stats.h>
19 #include <linux/lockd/lockd.h>
20 #include <linux/lockd/sm_inter.h>
22 #define NLMDBG_FACILITY NLMDBG_XDR
25 static inline loff_t
26 s32_to_loff_t(__s32 offset)
28 return (loff_t)offset;
31 static inline __s32
32 loff_t_to_s32(loff_t offset)
34 __s32 res;
35 if (offset >= NLM_OFFSET_MAX)
36 res = NLM_OFFSET_MAX;
37 else if (offset <= -NLM_OFFSET_MAX)
38 res = -NLM_OFFSET_MAX;
39 else
40 res = offset;
41 return res;
45 * XDR functions for basic NLM types
47 static inline u32 *nlm_decode_cookie(u32 *p, struct nlm_cookie *c)
49 unsigned int len;
51 len = ntohl(*p++);
53 if(len==0)
55 c->len=4;
56 memset(c->data, 0, 4); /* hockeypux brain damage */
58 else if(len<=8)
60 c->len=len;
61 memcpy(c->data, p, len);
62 p+=(len+3)>>2;
64 else
66 printk(KERN_NOTICE
67 "lockd: bad cookie size %d (only cookies under 8 bytes are supported.)\n", len);
68 return NULL;
70 return p;
73 static inline u32 *
74 nlm_encode_cookie(u32 *p, struct nlm_cookie *c)
76 *p++ = htonl(c->len);
77 memcpy(p, c->data, c->len);
78 p+=(c->len+3)>>2;
79 return p;
82 static inline u32 *
83 nlm_decode_fh(u32 *p, struct nfs_fh *f)
85 unsigned int len;
87 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
88 printk(KERN_NOTICE
89 "lockd: bad fhandle size %x (should be %d)\n",
90 len, NFS2_FHSIZE);
91 return NULL;
93 f->size = NFS2_FHSIZE;
94 memcpy(f->data, p, NFS2_FHSIZE);
95 return p + XDR_QUADLEN(NFS2_FHSIZE);
98 static inline u32 *
99 nlm_encode_fh(u32 *p, struct nfs_fh *f)
101 *p++ = htonl(NFS2_FHSIZE);
102 memcpy(p, f->data, NFS2_FHSIZE);
103 return p + XDR_QUADLEN(NFS2_FHSIZE);
107 * Encode and decode owner handle
109 static inline u32 *
110 nlm_decode_oh(u32 *p, struct xdr_netobj *oh)
112 return xdr_decode_netobj(p, oh);
115 static inline u32 *
116 nlm_encode_oh(u32 *p, struct xdr_netobj *oh)
118 return xdr_encode_netobj(p, oh);
121 static inline u32 *
122 nlm_decode_lock(u32 *p, struct nlm_lock *lock)
124 struct file_lock *fl = &lock->fl;
125 s32 start, len, end;
127 if (!(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN))
128 || !(p = nlm_decode_fh(p, &lock->fh))
129 || !(p = nlm_decode_oh(p, &lock->oh)))
130 return NULL;
132 locks_init_lock(fl);
133 fl->fl_owner = current->files;
134 fl->fl_pid = ntohl(*p++);
135 fl->fl_flags = FL_POSIX;
136 fl->fl_type = F_RDLCK; /* as good as anything else */
137 start = ntohl(*p++);
138 len = ntohl(*p++);
139 end = start + len - 1;
141 fl->fl_start = s32_to_loff_t(start);
143 if (len == 0 || end < 0)
144 fl->fl_end = OFFSET_MAX;
145 else
146 fl->fl_end = s32_to_loff_t(end);
147 return p;
151 * Encode a lock as part of an NLM call
153 static u32 *
154 nlm_encode_lock(u32 *p, struct nlm_lock *lock)
156 struct file_lock *fl = &lock->fl;
157 __s32 start, len;
159 if (!(p = xdr_encode_string(p, lock->caller))
160 || !(p = nlm_encode_fh(p, &lock->fh))
161 || !(p = nlm_encode_oh(p, &lock->oh)))
162 return NULL;
164 if (fl->fl_start > NLM_OFFSET_MAX
165 || (fl->fl_end > NLM_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
166 return NULL;
168 start = loff_t_to_s32(fl->fl_start);
169 if (fl->fl_end == OFFSET_MAX)
170 len = 0;
171 else
172 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
174 *p++ = htonl(fl->fl_pid);
175 *p++ = htonl(start);
176 *p++ = htonl(len);
178 return p;
182 * Encode result of a TEST/TEST_MSG call
184 static u32 *
185 nlm_encode_testres(u32 *p, struct nlm_res *resp)
187 s32 start, len;
189 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
190 return 0;
191 *p++ = resp->status;
193 if (resp->status == nlm_lck_denied) {
194 struct file_lock *fl = &resp->lock.fl;
196 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
197 *p++ = htonl(fl->fl_pid);
199 /* Encode owner handle. */
200 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
201 return 0;
203 start = loff_t_to_s32(fl->fl_start);
204 if (fl->fl_end == OFFSET_MAX)
205 len = 0;
206 else
207 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
209 *p++ = htonl(start);
210 *p++ = htonl(len);
213 return p;
217 * Check buffer bounds after decoding arguments
219 static inline int
220 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
222 struct svc_buf *buf = &rqstp->rq_argbuf;
224 return p - buf->base <= buf->buflen;
227 static inline int
228 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
230 struct svc_buf *buf = &rqstp->rq_resbuf;
232 buf->len = p - buf->base;
233 return (buf->len <= buf->buflen);
237 * First, the server side XDR functions
240 nlmsvc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
242 u32 exclusive;
244 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
245 return 0;
247 exclusive = ntohl(*p++);
248 if (!(p = nlm_decode_lock(p, &argp->lock)))
249 return 0;
250 if (exclusive)
251 argp->lock.fl.fl_type = F_WRLCK;
253 return xdr_argsize_check(rqstp, p);
257 nlmsvc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
259 if (!(p = nlm_encode_testres(p, resp)))
260 return 0;
261 return xdr_ressize_check(rqstp, p);
265 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
267 u32 exclusive;
269 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
270 return 0;
271 argp->block = ntohl(*p++);
272 exclusive = ntohl(*p++);
273 if (!(p = nlm_decode_lock(p, &argp->lock)))
274 return 0;
275 if (exclusive)
276 argp->lock.fl.fl_type = F_WRLCK;
277 argp->reclaim = ntohl(*p++);
278 argp->state = ntohl(*p++);
279 argp->monitor = 1; /* monitor client by default */
281 return xdr_argsize_check(rqstp, p);
285 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
287 u32 exclusive;
289 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
290 return 0;
291 argp->block = ntohl(*p++);
292 exclusive = ntohl(*p++);
293 if (!(p = nlm_decode_lock(p, &argp->lock)))
294 return 0;
295 if (exclusive)
296 argp->lock.fl.fl_type = F_WRLCK;
297 return xdr_argsize_check(rqstp, p);
301 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
303 if (!(p = nlm_decode_cookie(p, &argp->cookie))
304 || !(p = nlm_decode_lock(p, &argp->lock)))
305 return 0;
306 argp->lock.fl.fl_type = F_UNLCK;
307 return xdr_argsize_check(rqstp, p);
311 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
313 struct nlm_lock *lock = &argp->lock;
314 int len;
316 memset(lock, 0, sizeof(*lock));
317 locks_init_lock(&lock->fl);
318 lock->fl.fl_pid = ~(u32) 0;
320 if (!(p = nlm_decode_cookie(p, &argp->cookie))
321 || !(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN))
322 || !(p = nlm_decode_fh(p, &lock->fh))
323 || !(p = nlm_decode_oh(p, &lock->oh)))
324 return 0;
325 argp->fsm_mode = ntohl(*p++);
326 argp->fsm_access = ntohl(*p++);
327 return xdr_argsize_check(rqstp, p);
331 nlmsvc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
333 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
334 return 0;
335 *p++ = resp->status;
336 *p++ = xdr_zero; /* sequence argument */
337 return xdr_ressize_check(rqstp, p);
341 nlmsvc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
343 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
344 return 0;
345 *p++ = resp->status;
346 return xdr_ressize_check(rqstp, p);
350 nlmsvc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
352 struct nlm_lock *lock = &argp->lock;
353 int len;
355 if (!(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN)))
356 return 0;
357 argp->state = ntohl(*p++);
358 return xdr_argsize_check(rqstp, p);
362 nlmsvc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
364 if (!(p = xdr_decode_string(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
365 return 0;
366 argp->state = ntohl(*p++);
367 argp->addr = ntohl(*p++);
368 return xdr_argsize_check(rqstp, p);
372 nlmsvc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
374 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
375 return 0;
376 resp->status = ntohl(*p++);
377 return xdr_argsize_check(rqstp, p);
381 nlmsvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
383 return xdr_argsize_check(rqstp, p);
387 nlmsvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
389 return xdr_ressize_check(rqstp, p);
393 * Now, the client side XDR functions
395 static int
396 nlmclt_encode_void(struct rpc_rqst *req, u32 *p, void *ptr)
398 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
399 return 0;
402 static int
403 nlmclt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
405 return 0;
408 static int
409 nlmclt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
411 struct nlm_lock *lock = &argp->lock;
413 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
414 return -EIO;
415 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
416 if (!(p = nlm_encode_lock(p, lock)))
417 return -EIO;
418 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
419 return 0;
422 static int
423 nlmclt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
425 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
426 return -EIO;
427 resp->status = ntohl(*p++);
428 if (resp->status == NLM_LCK_DENIED) {
429 struct file_lock *fl = &resp->lock.fl;
430 u32 excl;
431 s32 start, len, end;
433 memset(&resp->lock, 0, sizeof(resp->lock));
434 locks_init_lock(fl);
435 excl = ntohl(*p++);
436 fl->fl_pid = ntohl(*p++);
437 if (!(p = nlm_decode_oh(p, &resp->lock.oh)))
438 return -EIO;
440 fl->fl_flags = FL_POSIX;
441 fl->fl_type = excl? F_WRLCK : F_RDLCK;
442 start = ntohl(*p++);
443 len = ntohl(*p++);
444 end = start + len - 1;
446 fl->fl_start = s32_to_loff_t(start);
447 if (len == 0 || end < 0)
448 fl->fl_end = OFFSET_MAX;
449 else
450 fl->fl_end = s32_to_loff_t(end);
452 return 0;
456 static int
457 nlmclt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
459 struct nlm_lock *lock = &argp->lock;
461 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
462 return -EIO;
463 *p++ = argp->block? xdr_one : xdr_zero;
464 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
465 if (!(p = nlm_encode_lock(p, lock)))
466 return -EIO;
467 *p++ = argp->reclaim? xdr_one : xdr_zero;
468 *p++ = htonl(argp->state);
469 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
470 return 0;
473 static int
474 nlmclt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
476 struct nlm_lock *lock = &argp->lock;
478 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
479 return -EIO;
480 *p++ = argp->block? xdr_one : xdr_zero;
481 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
482 if (!(p = nlm_encode_lock(p, lock)))
483 return -EIO;
484 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
485 return 0;
488 static int
489 nlmclt_encode_unlockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
491 struct nlm_lock *lock = &argp->lock;
493 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
494 return -EIO;
495 if (!(p = nlm_encode_lock(p, lock)))
496 return -EIO;
497 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
498 return 0;
501 static int
502 nlmclt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
504 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
505 return -EIO;
506 *p++ = resp->status;
507 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
508 return 0;
511 static int
512 nlmclt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
514 if (!(p = nlm_encode_testres(p, resp)))
515 return -EIO;
516 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
517 return 0;
520 static int
521 nlmclt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
523 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
524 return -EIO;
525 resp->status = ntohl(*p++);
526 return 0;
530 * Buffer requirements for NLM
532 #define NLM_void_sz 0
533 #define NLM_cookie_sz 3 /* 1 len , 2 data */
534 #define NLM_caller_sz 1+QUADLEN(sizeof(system_utsname.nodename))
535 #define NLM_netobj_sz 1+QUADLEN(XDR_MAX_NETOBJ)
536 /* #define NLM_owner_sz 1+QUADLEN(NLM_MAXOWNER) */
537 #define NLM_fhandle_sz 1+QUADLEN(NFS2_FHSIZE)
538 #define NLM_lock_sz 3+NLM_caller_sz+NLM_netobj_sz+NLM_fhandle_sz
539 #define NLM_holder_sz 4+NLM_netobj_sz
541 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
542 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
543 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
544 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
546 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
547 #define NLM_res_sz NLM_cookie_sz+1
548 #define NLM_norep_sz 0
550 #ifndef MAX
551 # define MAX(a, b) (((a) > (b))? (a) : (b))
552 #endif
555 * For NLM, a void procedure really returns nothing
557 #define nlmclt_decode_norep NULL
559 #define PROC(proc, argtype, restype) \
560 { "nlm_" #proc, \
561 (kxdrproc_t) nlmclt_encode_##argtype, \
562 (kxdrproc_t) nlmclt_decode_##restype, \
563 MAX(NLM_##argtype##_sz, NLM_##restype##_sz) << 2, \
567 static struct rpc_procinfo nlm_procedures[] = {
568 PROC(null, void, void),
569 PROC(test, testargs, testres),
570 PROC(lock, lockargs, res),
571 PROC(canc, cancargs, res),
572 PROC(unlock, unlockargs, res),
573 PROC(granted, testargs, res),
574 PROC(test_msg, testargs, norep),
575 PROC(lock_msg, lockargs, norep),
576 PROC(canc_msg, cancargs, norep),
577 PROC(unlock_msg, unlockargs, norep),
578 PROC(granted_msg, testargs, norep),
579 PROC(test_res, testres, norep),
580 PROC(lock_res, res, norep),
581 PROC(canc_res, res, norep),
582 PROC(unlock_res, res, norep),
583 PROC(granted_res, res, norep),
584 PROC(undef, void, void),
585 PROC(undef, void, void),
586 PROC(undef, void, void),
587 PROC(undef, void, void),
588 #ifdef NLMCLNT_SUPPORT_SHARES
589 PROC(share, shareargs, shareres),
590 PROC(unshare, shareargs, shareres),
591 PROC(nm_lock, lockargs, res),
592 PROC(free_all, notify, void),
593 #else
594 PROC(undef, void, void),
595 PROC(undef, void, void),
596 PROC(undef, void, void),
597 PROC(undef, void, void),
598 #endif
601 static struct rpc_version nlm_version1 = {
602 1, 16, nlm_procedures,
605 static struct rpc_version nlm_version3 = {
606 3, 24, nlm_procedures,
609 #ifdef CONFIG_LOCKD_V4
610 extern struct rpc_version nlm_version4;
611 #endif
613 static struct rpc_version * nlm_versions[] = {
614 NULL,
615 &nlm_version1,
616 NULL,
617 &nlm_version3,
618 #ifdef CONFIG_LOCKD_V4
619 &nlm_version4,
620 #endif
623 static struct rpc_stat nlm_stats;
625 struct rpc_program nlm_program = {
626 "lockd",
627 NLM_PROGRAM,
628 sizeof(nlm_versions) / sizeof(nlm_versions[0]),
629 nlm_versions,
630 &nlm_stats,
633 #ifdef LOCKD_DEBUG
634 char *
635 nlm_procname(u32 proc)
637 if (proc < sizeof(nlm_procedures)/sizeof(nlm_procedures[0]))
638 return nlm_procedures[proc].p_procname;
639 return "unknown";
641 #endif