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 u32 *nlm_decode_cookie(u32 *p, struct nlm_cookie *c)
56 memset(c->data, 0, 4); /* hockeypux brain damage */
58 else if(len<=NLM_MAXCOOKIELEN)
61 memcpy(c->data, p, len);
67 "lockd: bad cookie size %d (only cookies under %d bytes are supported.)\n", len, NLM_MAXCOOKIELEN);
74 nlm_encode_cookie(u32 *p, struct nlm_cookie *c)
77 memcpy(p, c->data, c->len);
78 p+=XDR_QUADLEN(c->len);
83 nlm_decode_fh(u32 *p, struct nfs_fh *f)
87 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
89 "lockd: bad fhandle size %d (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)))
134 lock->svid = ntohl(*p++);
137 fl->fl_owner = current->files;
138 fl->fl_pid = (pid_t)lock->svid;
139 fl->fl_flags = FL_POSIX;
140 fl->fl_type = F_RDLCK; /* as good as anything else */
143 end = start + len - 1;
145 fl->fl_start = s32_to_loff_t(start);
147 if (len == 0 || end < 0)
148 fl->fl_end = OFFSET_MAX;
150 fl->fl_end = s32_to_loff_t(end);
155 * Encode a lock as part of an NLM call
158 nlm_encode_lock(u32 *p, struct nlm_lock *lock)
160 struct file_lock *fl = &lock->fl;
163 if (!(p = xdr_encode_string(p, lock->caller))
164 || !(p = nlm_encode_fh(p, &lock->fh))
165 || !(p = nlm_encode_oh(p, &lock->oh)))
168 if (fl->fl_start > NLM_OFFSET_MAX
169 || (fl->fl_end > NLM_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
172 start = loff_t_to_s32(fl->fl_start);
173 if (fl->fl_end == OFFSET_MAX)
176 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
178 *p++ = htonl(lock->svid);
186 * Encode result of a TEST/TEST_MSG call
189 nlm_encode_testres(u32 *p, struct nlm_res *resp)
193 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
197 if (resp->status == nlm_lck_denied) {
198 struct file_lock *fl = &resp->lock.fl;
200 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
201 *p++ = htonl(resp->lock.svid);
203 /* Encode owner handle. */
204 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
207 start = loff_t_to_s32(fl->fl_start);
208 if (fl->fl_end == OFFSET_MAX)
211 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
222 * First, the server side XDR functions
225 nlmsvc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
229 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
232 exclusive = ntohl(*p++);
233 if (!(p = nlm_decode_lock(p, &argp->lock)))
236 argp->lock.fl.fl_type = F_WRLCK;
238 return xdr_argsize_check(rqstp, p);
242 nlmsvc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
244 if (!(p = nlm_encode_testres(p, resp)))
246 return xdr_ressize_check(rqstp, p);
250 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
254 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
256 argp->block = ntohl(*p++);
257 exclusive = ntohl(*p++);
258 if (!(p = nlm_decode_lock(p, &argp->lock)))
261 argp->lock.fl.fl_type = F_WRLCK;
262 argp->reclaim = ntohl(*p++);
263 argp->state = ntohl(*p++);
264 argp->monitor = 1; /* monitor client by default */
266 return xdr_argsize_check(rqstp, p);
270 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
274 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
276 argp->block = ntohl(*p++);
277 exclusive = ntohl(*p++);
278 if (!(p = nlm_decode_lock(p, &argp->lock)))
281 argp->lock.fl.fl_type = F_WRLCK;
282 return xdr_argsize_check(rqstp, p);
286 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
288 if (!(p = nlm_decode_cookie(p, &argp->cookie))
289 || !(p = nlm_decode_lock(p, &argp->lock)))
291 argp->lock.fl.fl_type = F_UNLCK;
292 return xdr_argsize_check(rqstp, p);
296 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
298 struct nlm_lock *lock = &argp->lock;
300 memset(lock, 0, sizeof(*lock));
301 locks_init_lock(&lock->fl);
302 lock->svid = ~(u32) 0;
303 lock->fl.fl_pid = (pid_t)lock->svid;
305 if (!(p = nlm_decode_cookie(p, &argp->cookie))
306 || !(p = xdr_decode_string_inplace(p, &lock->caller,
307 &lock->len, NLM_MAXSTRLEN))
308 || !(p = nlm_decode_fh(p, &lock->fh))
309 || !(p = nlm_decode_oh(p, &lock->oh)))
311 argp->fsm_mode = ntohl(*p++);
312 argp->fsm_access = ntohl(*p++);
313 return xdr_argsize_check(rqstp, p);
317 nlmsvc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
319 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
322 *p++ = xdr_zero; /* sequence argument */
323 return xdr_ressize_check(rqstp, p);
327 nlmsvc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
329 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
332 return xdr_ressize_check(rqstp, p);
336 nlmsvc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
338 struct nlm_lock *lock = &argp->lock;
340 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
341 &lock->len, NLM_MAXSTRLEN)))
343 argp->state = ntohl(*p++);
344 return xdr_argsize_check(rqstp, p);
348 nlmsvc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
350 if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
352 argp->state = ntohl(*p++);
353 /* Preserve the address in network byte order */
357 return xdr_argsize_check(rqstp, p);
361 nlmsvc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
363 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
365 resp->status = ntohl(*p++);
366 return xdr_argsize_check(rqstp, p);
370 nlmsvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
372 return xdr_argsize_check(rqstp, p);
376 nlmsvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
378 return xdr_ressize_check(rqstp, p);
382 * Now, the client side XDR functions
384 #ifdef NLMCLNT_SUPPORT_SHARES
386 nlmclt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
393 nlmclt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
395 struct nlm_lock *lock = &argp->lock;
397 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
399 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
400 if (!(p = nlm_encode_lock(p, lock)))
402 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
407 nlmclt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
409 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
411 resp->status = ntohl(*p++);
412 if (resp->status == NLM_LCK_DENIED) {
413 struct file_lock *fl = &resp->lock.fl;
417 memset(&resp->lock, 0, sizeof(resp->lock));
420 resp->lock.svid = ntohl(*p++);
421 fl->fl_pid = (pid_t)resp->lock.svid;
422 if (!(p = nlm_decode_oh(p, &resp->lock.oh)))
425 fl->fl_flags = FL_POSIX;
426 fl->fl_type = excl? F_WRLCK : F_RDLCK;
429 end = start + len - 1;
431 fl->fl_start = s32_to_loff_t(start);
432 if (len == 0 || end < 0)
433 fl->fl_end = OFFSET_MAX;
435 fl->fl_end = s32_to_loff_t(end);
442 nlmclt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
444 struct nlm_lock *lock = &argp->lock;
446 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
448 *p++ = argp->block? xdr_one : xdr_zero;
449 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
450 if (!(p = nlm_encode_lock(p, lock)))
452 *p++ = argp->reclaim? xdr_one : xdr_zero;
453 *p++ = htonl(argp->state);
454 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
459 nlmclt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
461 struct nlm_lock *lock = &argp->lock;
463 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
465 *p++ = argp->block? xdr_one : xdr_zero;
466 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
467 if (!(p = nlm_encode_lock(p, lock)))
469 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
474 nlmclt_encode_unlockargs(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)))
480 if (!(p = nlm_encode_lock(p, lock)))
482 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
487 nlmclt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
489 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
492 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
497 nlmclt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
499 if (!(p = nlm_encode_testres(p, resp)))
501 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
506 nlmclt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
508 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
510 resp->status = ntohl(*p++);
515 * Buffer requirements for NLM
517 #define NLM_void_sz 0
518 #define NLM_cookie_sz 1+XDR_QUADLEN(NLM_MAXCOOKIELEN)
519 #define NLM_caller_sz 1+XDR_QUADLEN(sizeof(system_utsname.nodename))
520 #define NLM_netobj_sz 1+XDR_QUADLEN(XDR_MAX_NETOBJ)
521 /* #define NLM_owner_sz 1+XDR_QUADLEN(NLM_MAXOWNER) */
522 #define NLM_fhandle_sz 1+XDR_QUADLEN(NFS2_FHSIZE)
523 #define NLM_lock_sz 3+NLM_caller_sz+NLM_netobj_sz+NLM_fhandle_sz
524 #define NLM_holder_sz 4+NLM_netobj_sz
526 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
527 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
528 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
529 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
531 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
532 #define NLM_res_sz NLM_cookie_sz+1
533 #define NLM_norep_sz 0
536 # define MAX(a, b) (((a) > (b))? (a) : (b))
540 * For NLM, a void procedure really returns nothing
542 #define nlmclt_decode_norep NULL
544 #define PROC(proc, argtype, restype) \
545 [NLMPROC_##proc] = { \
546 .p_proc = NLMPROC_##proc, \
547 .p_encode = (kxdrproc_t) nlmclt_encode_##argtype, \
548 .p_decode = (kxdrproc_t) nlmclt_decode_##restype, \
549 .p_bufsiz = MAX(NLM_##argtype##_sz, NLM_##restype##_sz) << 2, \
550 .p_statidx = NLMPROC_##proc, \
554 static struct rpc_procinfo nlm_procedures[] = {
555 PROC(TEST, testargs, testres),
556 PROC(LOCK, lockargs, res),
557 PROC(CANCEL, cancargs, res),
558 PROC(UNLOCK, unlockargs, res),
559 PROC(GRANTED, testargs, res),
560 PROC(TEST_MSG, testargs, norep),
561 PROC(LOCK_MSG, lockargs, norep),
562 PROC(CANCEL_MSG, cancargs, norep),
563 PROC(UNLOCK_MSG, unlockargs, norep),
564 PROC(GRANTED_MSG, testargs, norep),
565 PROC(TEST_RES, testres, norep),
566 PROC(LOCK_RES, res, norep),
567 PROC(CANCEL_RES, res, norep),
568 PROC(UNLOCK_RES, res, norep),
569 PROC(GRANTED_RES, res, norep),
570 #ifdef NLMCLNT_SUPPORT_SHARES
571 PROC(SHARE, shareargs, shareres),
572 PROC(UNSHARE, shareargs, shareres),
573 PROC(NM_LOCK, lockargs, res),
574 PROC(FREE_ALL, notify, void),
578 static struct rpc_version nlm_version1 = {
581 .procs = nlm_procedures,
584 static struct rpc_version nlm_version3 = {
587 .procs = nlm_procedures,
590 #ifdef CONFIG_LOCKD_V4
591 extern struct rpc_version nlm_version4;
594 static struct rpc_version * nlm_versions[] = {
597 #ifdef CONFIG_LOCKD_V4
602 static struct rpc_stat nlm_stats;
604 struct rpc_program nlm_program = {
606 .number = NLM_PROGRAM,
607 .nrvers = ARRAY_SIZE(nlm_versions),
608 .version = nlm_versions,
613 const char *nlmdbg_cookie2a(const struct nlm_cookie *cookie)
616 * We can get away with a static buffer because we're only
617 * called with BKL held.
619 static char buf[2*NLM_MAXCOOKIELEN+1];
621 int len = sizeof(buf);
624 len--; /* allow for trailing \0 */
627 for (i = 0 ; i < cookie->len ; i++) {
632 sprintf(p, "%02x", cookie->data[i]);