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 memcpy(f
->data
, p
, NFS2_FHSIZE
);
95 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
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
110 nlm_decode_oh(u32
*p
, struct xdr_netobj
*oh
)
112 return xdr_decode_netobj(p
, oh
);
116 nlm_encode_oh(u32
*p
, struct xdr_netobj
*oh
)
118 return xdr_encode_netobj(p
, oh
);
122 nlm_decode_lock(u32
*p
, struct nlm_lock
*lock
)
124 struct file_lock
*fl
= &lock
->fl
;
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
)))
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 */
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
;
146 fl
->fl_end
= s32_to_loff_t(end
);
151 * Encode a lock as part of an NLM call
154 nlm_encode_lock(u32
*p
, struct nlm_lock
*lock
)
156 struct file_lock
*fl
= &lock
->fl
;
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
)))
164 if (fl
->fl_start
> NLM_OFFSET_MAX
165 || (fl
->fl_end
> NLM_OFFSET_MAX
&& fl
->fl_end
!= OFFSET_MAX
))
168 start
= loff_t_to_s32(fl
->fl_start
);
169 if (fl
->fl_end
== OFFSET_MAX
)
172 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
174 *p
++ = htonl(fl
->fl_pid
);
182 * Encode result of a TEST/TEST_MSG call
185 nlm_encode_testres(u32
*p
, struct nlm_res
*resp
)
189 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
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
)))
203 start
= loff_t_to_s32(fl
->fl_start
);
204 if (fl
->fl_end
== OFFSET_MAX
)
207 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
217 * Check buffer bounds after decoding arguments
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
;
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
)
244 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
247 exclusive
= ntohl(*p
++);
248 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
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
)))
261 return xdr_ressize_check(rqstp
, p
);
265 nlmsvc_decode_lockargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
269 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
271 argp
->block
= ntohl(*p
++);
272 exclusive
= ntohl(*p
++);
273 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
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
)
289 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
291 argp
->block
= ntohl(*p
++);
292 exclusive
= ntohl(*p
++);
293 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
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
)))
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
;
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
)))
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
)))
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
)))
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
;
355 if (!(p
= xdr_decode_string(p
, &lock
->caller
, &len
, NLM_MAXSTRLEN
)))
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
)))
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
)))
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
396 nlmclt_encode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
398 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
403 nlmclt_decode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
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
)))
415 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
416 if (!(p
= nlm_encode_lock(p
, lock
)))
418 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
423 nlmclt_decode_testres(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
425 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
427 resp
->status
= ntohl(*p
++);
428 if (resp
->status
== NLM_LCK_DENIED
) {
429 struct file_lock
*fl
= &resp
->lock
.fl
;
433 memset(&resp
->lock
, 0, sizeof(resp
->lock
));
436 fl
->fl_pid
= ntohl(*p
++);
437 if (!(p
= nlm_decode_oh(p
, &resp
->lock
.oh
)))
440 fl
->fl_flags
= FL_POSIX
;
441 fl
->fl_type
= excl
? F_WRLCK
: F_RDLCK
;
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
;
450 fl
->fl_end
= s32_to_loff_t(end
);
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
)))
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
)))
467 *p
++ = argp
->reclaim
? xdr_one
: xdr_zero
;
468 *p
++ = htonl(argp
->state
);
469 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
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
)))
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
)))
484 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
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
)))
495 if (!(p
= nlm_encode_lock(p
, lock
)))
497 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
502 nlmclt_encode_res(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
504 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
507 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
512 nlmclt_encode_testres(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
514 if (!(p
= nlm_encode_testres(p
, resp
)))
516 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
521 nlmclt_decode_res(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
523 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
525 resp
->status
= ntohl(*p
++);
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
551 # define MAX(a, b) (((a) > (b))? (a) : (b))
555 * For NLM, a void procedure really returns nothing
557 #define nlmclt_decode_norep NULL
559 #define PROC(proc, argtype, restype) \
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),
594 PROC(undef
, void, void),
595 PROC(undef
, void, void),
596 PROC(undef
, void, void),
597 PROC(undef
, void, void),
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
;
613 static struct rpc_version
* nlm_versions
[] = {
618 #ifdef CONFIG_LOCKD_V4
623 static struct rpc_stat nlm_stats
;
625 struct rpc_program nlm_program
= {
628 sizeof(nlm_versions
) / sizeof(nlm_versions
[0]),
635 nlm_procname(u32 proc
)
637 if (proc
< sizeof(nlm_procedures
)/sizeof(nlm_procedures
[0]))
638 return nlm_procedures
[proc
].p_procname
;