4 * XDR support for lockd and the lock client.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
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
26 s32_to_loff_t(__s32 offset)
28 return (loff_t)offset;
32 loff_t_to_s32(loff_t offset)
35 if (offset >= NLM_OFFSET_MAX)
37 else if (offset <= -NLM_OFFSET_MAX)
38 res = -NLM_OFFSET_MAX;
45 * XDR functions for basic NLM types
47 static inline u32 *nlm_decode_cookie(u32 *p, struct nlm_cookie *c)
56 memset(c->data, 0, 4); /* hockeypux brain damage */
61 memcpy(c->data, p, len);
67 "lockd: bad cookie size %d (only cookies under 8 bytes are supported.)\n", len);
74 nlm_encode_cookie(u32 *p, struct nlm_cookie *c)
77 memcpy(p, c->data, c->len);
83 nlm_decode_fh(u32 *p, struct nfs_fh *f)
87 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
89 "lockd: bad fhandle size %x (should be %d)\n",
93 f->size = NFS2_FHSIZE;
94 memset(f->data, 0, sizeof(f->data));
95 memcpy(f->data, p, NFS2_FHSIZE);
96 return p + XDR_QUADLEN(NFS2_FHSIZE);
100 nlm_encode_fh(u32 *p, struct nfs_fh *f)
102 *p++ = htonl(NFS2_FHSIZE);
103 memcpy(p, f->data, NFS2_FHSIZE);
104 return p + XDR_QUADLEN(NFS2_FHSIZE);
108 * Encode and decode owner handle
111 nlm_decode_oh(u32 *p, struct xdr_netobj *oh)
113 return xdr_decode_netobj(p, oh);
117 nlm_encode_oh(u32 *p, struct xdr_netobj *oh)
119 return xdr_encode_netobj(p, oh);
123 nlm_decode_lock(u32 *p, struct nlm_lock *lock)
125 struct file_lock *fl = &lock->fl;
128 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
131 || !(p = nlm_decode_fh(p, &lock->fh))
132 || !(p = nlm_decode_oh(p, &lock->oh)))
136 fl->fl_owner = current->files;
137 fl->fl_pid = ntohl(*p++);
138 fl->fl_flags = FL_POSIX;
139 fl->fl_type = F_RDLCK; /* as good as anything else */
142 end = start + len - 1;
144 fl->fl_start = s32_to_loff_t(start);
146 if (len == 0 || end < 0)
147 fl->fl_end = OFFSET_MAX;
149 fl->fl_end = s32_to_loff_t(end);
154 * Encode a lock as part of an NLM call
157 nlm_encode_lock(u32 *p, struct nlm_lock *lock)
159 struct file_lock *fl = &lock->fl;
162 if (!(p = xdr_encode_string(p, lock->caller))
163 || !(p = nlm_encode_fh(p, &lock->fh))
164 || !(p = nlm_encode_oh(p, &lock->oh)))
167 if (fl->fl_start > NLM_OFFSET_MAX
168 || (fl->fl_end > NLM_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
171 start = loff_t_to_s32(fl->fl_start);
172 if (fl->fl_end == OFFSET_MAX)
175 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
177 *p++ = htonl(fl->fl_pid);
185 * Encode result of a TEST/TEST_MSG call
188 nlm_encode_testres(u32 *p, struct nlm_res *resp)
192 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
196 if (resp->status == nlm_lck_denied) {
197 struct file_lock *fl = &resp->lock.fl;
199 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
200 *p++ = htonl(fl->fl_pid);
202 /* Encode owner handle. */
203 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
206 start = loff_t_to_s32(fl->fl_start);
207 if (fl->fl_end == OFFSET_MAX)
210 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
220 * Check buffer bounds after decoding arguments
223 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
225 struct svc_buf *buf = &rqstp->rq_argbuf;
227 return p - buf->base <= buf->buflen;
231 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
233 struct svc_buf *buf = &rqstp->rq_resbuf;
235 buf->len = p - buf->base;
236 return (buf->len <= buf->buflen);
240 * First, the server side XDR functions
243 nlmsvc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
247 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
250 exclusive = ntohl(*p++);
251 if (!(p = nlm_decode_lock(p, &argp->lock)))
254 argp->lock.fl.fl_type = F_WRLCK;
256 return xdr_argsize_check(rqstp, p);
260 nlmsvc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
262 if (!(p = nlm_encode_testres(p, resp)))
264 return xdr_ressize_check(rqstp, p);
268 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
272 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
274 argp->block = ntohl(*p++);
275 exclusive = ntohl(*p++);
276 if (!(p = nlm_decode_lock(p, &argp->lock)))
279 argp->lock.fl.fl_type = F_WRLCK;
280 argp->reclaim = ntohl(*p++);
281 argp->state = ntohl(*p++);
282 argp->monitor = 1; /* monitor client by default */
284 return xdr_argsize_check(rqstp, p);
288 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
292 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
294 argp->block = ntohl(*p++);
295 exclusive = ntohl(*p++);
296 if (!(p = nlm_decode_lock(p, &argp->lock)))
299 argp->lock.fl.fl_type = F_WRLCK;
300 return xdr_argsize_check(rqstp, p);
304 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
306 if (!(p = nlm_decode_cookie(p, &argp->cookie))
307 || !(p = nlm_decode_lock(p, &argp->lock)))
309 argp->lock.fl.fl_type = F_UNLCK;
310 return xdr_argsize_check(rqstp, p);
314 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
316 struct nlm_lock *lock = &argp->lock;
318 memset(lock, 0, sizeof(*lock));
319 locks_init_lock(&lock->fl);
320 lock->fl.fl_pid = ~(u32) 0;
322 if (!(p = nlm_decode_cookie(p, &argp->cookie))
323 || !(p = xdr_decode_string_inplace(p, &lock->caller,
324 &lock->len, NLM_MAXSTRLEN))
325 || !(p = nlm_decode_fh(p, &lock->fh))
326 || !(p = nlm_decode_oh(p, &lock->oh)))
328 argp->fsm_mode = ntohl(*p++);
329 argp->fsm_access = ntohl(*p++);
330 return xdr_argsize_check(rqstp, p);
334 nlmsvc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
336 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
339 *p++ = xdr_zero; /* sequence argument */
340 return xdr_ressize_check(rqstp, p);
344 nlmsvc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
346 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
349 return xdr_ressize_check(rqstp, p);
353 nlmsvc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
355 struct nlm_lock *lock = &argp->lock;
357 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
358 &lock->len, NLM_MAXSTRLEN)))
360 argp->state = ntohl(*p++);
361 return xdr_argsize_check(rqstp, p);
365 nlmsvc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
367 if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
369 argp->state = ntohl(*p++);
370 /* Preserve the address in network byte order */
372 return xdr_argsize_check(rqstp, p);
376 nlmsvc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
378 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
380 resp->status = ntohl(*p++);
381 return xdr_argsize_check(rqstp, p);
385 nlmsvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
387 return xdr_argsize_check(rqstp, p);
391 nlmsvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
393 return xdr_ressize_check(rqstp, p);
397 * Now, the client side XDR functions
400 nlmclt_encode_void(struct rpc_rqst *req, u32 *p, void *ptr)
402 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
407 nlmclt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
413 nlmclt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
415 struct nlm_lock *lock = &argp->lock;
417 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
419 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
420 if (!(p = nlm_encode_lock(p, lock)))
422 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
427 nlmclt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
429 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
431 resp->status = ntohl(*p++);
432 if (resp->status == NLM_LCK_DENIED) {
433 struct file_lock *fl = &resp->lock.fl;
437 memset(&resp->lock, 0, sizeof(resp->lock));
440 fl->fl_pid = ntohl(*p++);
441 if (!(p = nlm_decode_oh(p, &resp->lock.oh)))
444 fl->fl_flags = FL_POSIX;
445 fl->fl_type = excl? F_WRLCK : F_RDLCK;
448 end = start + len - 1;
450 fl->fl_start = s32_to_loff_t(start);
451 if (len == 0 || end < 0)
452 fl->fl_end = OFFSET_MAX;
454 fl->fl_end = s32_to_loff_t(end);
461 nlmclt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
463 struct nlm_lock *lock = &argp->lock;
465 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
467 *p++ = argp->block? xdr_one : xdr_zero;
468 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
469 if (!(p = nlm_encode_lock(p, lock)))
471 *p++ = argp->reclaim? xdr_one : xdr_zero;
472 *p++ = htonl(argp->state);
473 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
478 nlmclt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
480 struct nlm_lock *lock = &argp->lock;
482 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
484 *p++ = argp->block? xdr_one : xdr_zero;
485 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
486 if (!(p = nlm_encode_lock(p, lock)))
488 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
493 nlmclt_encode_unlockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
495 struct nlm_lock *lock = &argp->lock;
497 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
499 if (!(p = nlm_encode_lock(p, lock)))
501 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
506 nlmclt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
508 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
511 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
516 nlmclt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
518 if (!(p = nlm_encode_testres(p, resp)))
520 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
525 nlmclt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
527 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
529 resp->status = ntohl(*p++);
534 * Buffer requirements for NLM
536 #define NLM_void_sz 0
537 #define NLM_cookie_sz 3 /* 1 len , 2 data */
538 #define NLM_caller_sz 1+QUADLEN(sizeof(system_utsname.nodename))
539 #define NLM_netobj_sz 1+QUADLEN(XDR_MAX_NETOBJ)
540 /* #define NLM_owner_sz 1+QUADLEN(NLM_MAXOWNER) */
541 #define NLM_fhandle_sz 1+QUADLEN(NFS2_FHSIZE)
542 #define NLM_lock_sz 3+NLM_caller_sz+NLM_netobj_sz+NLM_fhandle_sz
543 #define NLM_holder_sz 4+NLM_netobj_sz
545 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
546 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
547 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
548 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
550 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
551 #define NLM_res_sz NLM_cookie_sz+1
552 #define NLM_norep_sz 0
555 # define MAX(a, b) (((a) > (b))? (a) : (b))
559 * For NLM, a void procedure really returns nothing
561 #define nlmclt_decode_norep NULL
563 #define PROC(proc, argtype, restype) \
564 { .p_procname = "nlm_" #proc, \
565 .p_encode = (kxdrproc_t) nlmclt_encode_##argtype, \
566 .p_decode = (kxdrproc_t) nlmclt_decode_##restype, \
567 .p_bufsiz = MAX(NLM_##argtype##_sz, NLM_##restype##_sz) << 2 \
570 static struct rpc_procinfo nlm_procedures[] = {
571 PROC(null, void, void),
572 PROC(test, testargs, testres),
573 PROC(lock, lockargs, res),
574 PROC(canc, cancargs, res),
575 PROC(unlock, unlockargs, res),
576 PROC(granted, testargs, res),
577 PROC(test_msg, testargs, norep),
578 PROC(lock_msg, lockargs, norep),
579 PROC(canc_msg, cancargs, norep),
580 PROC(unlock_msg, unlockargs, norep),
581 PROC(granted_msg, testargs, norep),
582 PROC(test_res, testres, norep),
583 PROC(lock_res, res, norep),
584 PROC(canc_res, res, norep),
585 PROC(unlock_res, res, norep),
586 PROC(granted_res, res, norep),
587 PROC(undef, void, void),
588 PROC(undef, void, void),
589 PROC(undef, void, void),
590 PROC(undef, void, void),
591 #ifdef NLMCLNT_SUPPORT_SHARES
592 PROC(share, shareargs, shareres),
593 PROC(unshare, shareargs, shareres),
594 PROC(nm_lock, lockargs, res),
595 PROC(free_all, notify, void),
597 PROC(undef, void, void),
598 PROC(undef, void, void),
599 PROC(undef, void, void),
600 PROC(undef, void, void),
604 static struct rpc_version nlm_version1 = {
605 1, 16, nlm_procedures,
608 static struct rpc_version nlm_version3 = {
609 3, 24, nlm_procedures,
612 #ifdef CONFIG_LOCKD_V4
613 extern struct rpc_version nlm_version4;
616 static struct rpc_version * nlm_versions[] = {
621 #ifdef CONFIG_LOCKD_V4
626 static struct rpc_stat nlm_stats;
628 struct rpc_program nlm_program = {
631 sizeof(nlm_versions) / sizeof(nlm_versions[0]),
638 nlm_procname(u32 proc)
640 if (proc < sizeof(nlm_procedures)/sizeof(nlm_procedures[0]))
641 return nlm_procedures[proc].p_procname;