4 * XDR support for lockd and the lock client.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/utsname.h>
12 #include <linux/nfs.h>
14 #include <linux/sunrpc/xdr.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/svc.h>
17 #include <linux/sunrpc/stats.h>
18 #include <linux/lockd/lockd.h>
20 #define NLMDBG_FACILITY NLMDBG_XDR
24 s32_to_loff_t(__s32 offset
)
26 return (loff_t
)offset
;
30 loff_t_to_s32(loff_t offset
)
33 if (offset
>= NLM_OFFSET_MAX
)
35 else if (offset
<= -NLM_OFFSET_MAX
)
36 res
= -NLM_OFFSET_MAX
;
43 * XDR functions for basic NLM types
45 static __be32
*nlm_decode_cookie(__be32
*p
, struct nlm_cookie
*c
)
54 memset(c
->data
, 0, 4); /* hockeypux brain damage */
56 else if(len
<=NLM_MAXCOOKIELEN
)
59 memcpy(c
->data
, p
, len
);
64 dprintk("lockd: bad cookie size %d (only cookies under "
65 "%d bytes are supported.)\n",
66 len
, NLM_MAXCOOKIELEN
);
72 static inline __be32
*
73 nlm_encode_cookie(__be32
*p
, struct nlm_cookie
*c
)
76 memcpy(p
, c
->data
, c
->len
);
77 p
+=XDR_QUADLEN(c
->len
);
82 nlm_decode_fh(__be32
*p
, struct nfs_fh
*f
)
86 if ((len
= ntohl(*p
++)) != NFS2_FHSIZE
) {
87 dprintk("lockd: bad fhandle size %d (should be %d)\n",
91 f
->size
= NFS2_FHSIZE
;
92 memset(f
->data
, 0, sizeof(f
->data
));
93 memcpy(f
->data
, p
, NFS2_FHSIZE
);
94 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
97 static inline __be32
*
98 nlm_encode_fh(__be32
*p
, struct nfs_fh
*f
)
100 *p
++ = htonl(NFS2_FHSIZE
);
101 memcpy(p
, f
->data
, NFS2_FHSIZE
);
102 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
106 * Encode and decode owner handle
108 static inline __be32
*
109 nlm_decode_oh(__be32
*p
, struct xdr_netobj
*oh
)
111 return xdr_decode_netobj(p
, oh
);
114 static inline __be32
*
115 nlm_encode_oh(__be32
*p
, struct xdr_netobj
*oh
)
117 return xdr_encode_netobj(p
, oh
);
121 nlm_decode_lock(__be32
*p
, struct nlm_lock
*lock
)
123 struct file_lock
*fl
= &lock
->fl
;
126 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
129 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
130 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
132 lock
->svid
= ntohl(*p
++);
135 fl
->fl_owner
= current
->files
;
136 fl
->fl_pid
= (pid_t
)lock
->svid
;
137 fl
->fl_flags
= FL_POSIX
;
138 fl
->fl_type
= F_RDLCK
; /* as good as anything else */
141 end
= start
+ len
- 1;
143 fl
->fl_start
= s32_to_loff_t(start
);
145 if (len
== 0 || end
< 0)
146 fl
->fl_end
= OFFSET_MAX
;
148 fl
->fl_end
= s32_to_loff_t(end
);
153 * Encode a lock as part of an NLM call
156 nlm_encode_lock(__be32
*p
, struct nlm_lock
*lock
)
158 struct file_lock
*fl
= &lock
->fl
;
161 if (!(p
= xdr_encode_string(p
, lock
->caller
))
162 || !(p
= nlm_encode_fh(p
, &lock
->fh
))
163 || !(p
= nlm_encode_oh(p
, &lock
->oh
)))
166 if (fl
->fl_start
> NLM_OFFSET_MAX
167 || (fl
->fl_end
> NLM_OFFSET_MAX
&& fl
->fl_end
!= OFFSET_MAX
))
170 start
= loff_t_to_s32(fl
->fl_start
);
171 if (fl
->fl_end
== OFFSET_MAX
)
174 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
176 *p
++ = htonl(lock
->svid
);
184 * Encode result of a TEST/TEST_MSG call
187 nlm_encode_testres(__be32
*p
, struct nlm_res
*resp
)
191 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
195 if (resp
->status
== nlm_lck_denied
) {
196 struct file_lock
*fl
= &resp
->lock
.fl
;
198 *p
++ = (fl
->fl_type
== F_RDLCK
)? xdr_zero
: xdr_one
;
199 *p
++ = htonl(resp
->lock
.svid
);
201 /* Encode owner handle. */
202 if (!(p
= xdr_encode_netobj(p
, &resp
->lock
.oh
)))
205 start
= loff_t_to_s32(fl
->fl_start
);
206 if (fl
->fl_end
== OFFSET_MAX
)
209 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
220 * First, the server side XDR functions
223 nlmsvc_decode_testargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
227 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
230 exclusive
= ntohl(*p
++);
231 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
234 argp
->lock
.fl
.fl_type
= F_WRLCK
;
236 return xdr_argsize_check(rqstp
, p
);
240 nlmsvc_encode_testres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
242 if (!(p
= nlm_encode_testres(p
, resp
)))
244 return xdr_ressize_check(rqstp
, p
);
248 nlmsvc_decode_lockargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
252 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
254 argp
->block
= ntohl(*p
++);
255 exclusive
= ntohl(*p
++);
256 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
259 argp
->lock
.fl
.fl_type
= F_WRLCK
;
260 argp
->reclaim
= ntohl(*p
++);
261 argp
->state
= ntohl(*p
++);
262 argp
->monitor
= 1; /* monitor client by default */
264 return xdr_argsize_check(rqstp
, p
);
268 nlmsvc_decode_cancargs(struct svc_rqst
*rqstp
, __be32
*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 return xdr_argsize_check(rqstp
, p
);
284 nlmsvc_decode_unlockargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
286 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
287 || !(p
= nlm_decode_lock(p
, &argp
->lock
)))
289 argp
->lock
.fl
.fl_type
= F_UNLCK
;
290 return xdr_argsize_check(rqstp
, p
);
294 nlmsvc_decode_shareargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
296 struct nlm_lock
*lock
= &argp
->lock
;
298 memset(lock
, 0, sizeof(*lock
));
299 locks_init_lock(&lock
->fl
);
300 lock
->svid
= ~(u32
) 0;
301 lock
->fl
.fl_pid
= (pid_t
)lock
->svid
;
303 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
304 || !(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
305 &lock
->len
, NLM_MAXSTRLEN
))
306 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
307 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
309 argp
->fsm_mode
= ntohl(*p
++);
310 argp
->fsm_access
= ntohl(*p
++);
311 return xdr_argsize_check(rqstp
, p
);
315 nlmsvc_encode_shareres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
317 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
320 *p
++ = xdr_zero
; /* sequence argument */
321 return xdr_ressize_check(rqstp
, p
);
325 nlmsvc_encode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
327 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
330 return xdr_ressize_check(rqstp
, p
);
334 nlmsvc_decode_notify(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_args
*argp
)
336 struct nlm_lock
*lock
= &argp
->lock
;
338 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
339 &lock
->len
, NLM_MAXSTRLEN
)))
341 argp
->state
= ntohl(*p
++);
342 return xdr_argsize_check(rqstp
, p
);
346 nlmsvc_decode_reboot(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_reboot
*argp
)
348 if (!(p
= xdr_decode_string_inplace(p
, &argp
->mon
, &argp
->len
, SM_MAXSTRLEN
)))
350 argp
->state
= ntohl(*p
++);
351 memcpy(&argp
->priv
.data
, p
, sizeof(argp
->priv
.data
));
352 p
+= XDR_QUADLEN(SM_PRIV_SIZE
);
353 return xdr_argsize_check(rqstp
, p
);
357 nlmsvc_decode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
359 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
362 return xdr_argsize_check(rqstp
, p
);
366 nlmsvc_decode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
368 return xdr_argsize_check(rqstp
, p
);
372 nlmsvc_encode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
374 return xdr_ressize_check(rqstp
, p
);
378 * Now, the client side XDR functions
380 #ifdef NLMCLNT_SUPPORT_SHARES
382 nlmclt_decode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
389 nlmclt_encode_testargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
391 struct nlm_lock
*lock
= &argp
->lock
;
393 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
395 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
396 if (!(p
= nlm_encode_lock(p
, lock
)))
398 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
403 nlmclt_decode_testres(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
405 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
408 if (resp
->status
== nlm_lck_denied
) {
409 struct file_lock
*fl
= &resp
->lock
.fl
;
413 memset(&resp
->lock
, 0, sizeof(resp
->lock
));
416 resp
->lock
.svid
= ntohl(*p
++);
417 fl
->fl_pid
= (pid_t
)resp
->lock
.svid
;
418 if (!(p
= nlm_decode_oh(p
, &resp
->lock
.oh
)))
421 fl
->fl_flags
= FL_POSIX
;
422 fl
->fl_type
= excl
? F_WRLCK
: F_RDLCK
;
425 end
= start
+ len
- 1;
427 fl
->fl_start
= s32_to_loff_t(start
);
428 if (len
== 0 || end
< 0)
429 fl
->fl_end
= OFFSET_MAX
;
431 fl
->fl_end
= s32_to_loff_t(end
);
438 nlmclt_encode_lockargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
440 struct nlm_lock
*lock
= &argp
->lock
;
442 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
444 *p
++ = argp
->block
? xdr_one
: xdr_zero
;
445 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
446 if (!(p
= nlm_encode_lock(p
, lock
)))
448 *p
++ = argp
->reclaim
? xdr_one
: xdr_zero
;
449 *p
++ = htonl(argp
->state
);
450 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
455 nlmclt_encode_cancargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
457 struct nlm_lock
*lock
= &argp
->lock
;
459 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
461 *p
++ = argp
->block
? xdr_one
: xdr_zero
;
462 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
463 if (!(p
= nlm_encode_lock(p
, lock
)))
465 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
470 nlmclt_encode_unlockargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
472 struct nlm_lock
*lock
= &argp
->lock
;
474 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
476 if (!(p
= nlm_encode_lock(p
, lock
)))
478 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
483 nlmclt_encode_res(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
485 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
488 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
493 nlmclt_encode_testres(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
495 if (!(p
= nlm_encode_testres(p
, resp
)))
497 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
502 nlmclt_decode_res(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
504 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
510 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
511 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
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(NLMCLNT_OHSIZE)
520 #define NLM_owner_sz 1+XDR_QUADLEN(NLMCLNT_OHSIZE)
521 #define NLM_fhandle_sz 1+XDR_QUADLEN(NFS2_FHSIZE)
522 #define NLM_lock_sz 3+NLM_caller_sz+NLM_owner_sz+NLM_fhandle_sz
523 #define NLM_holder_sz 4+NLM_owner_sz
525 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
526 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
527 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
528 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
530 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
531 #define NLM_res_sz NLM_cookie_sz+1
532 #define NLM_norep_sz 0
535 * For NLM, a void procedure really returns nothing
537 #define nlmclt_decode_norep NULL
539 #define PROC(proc, argtype, restype) \
540 [NLMPROC_##proc] = { \
541 .p_proc = NLMPROC_##proc, \
542 .p_encode = (kxdrproc_t) nlmclt_encode_##argtype, \
543 .p_decode = (kxdrproc_t) nlmclt_decode_##restype, \
544 .p_arglen = NLM_##argtype##_sz, \
545 .p_replen = NLM_##restype##_sz, \
546 .p_statidx = NLMPROC_##proc, \
550 static struct rpc_procinfo nlm_procedures
[] = {
551 PROC(TEST
, testargs
, testres
),
552 PROC(LOCK
, lockargs
, res
),
553 PROC(CANCEL
, cancargs
, res
),
554 PROC(UNLOCK
, unlockargs
, res
),
555 PROC(GRANTED
, testargs
, res
),
556 PROC(TEST_MSG
, testargs
, norep
),
557 PROC(LOCK_MSG
, lockargs
, norep
),
558 PROC(CANCEL_MSG
, cancargs
, norep
),
559 PROC(UNLOCK_MSG
, unlockargs
, norep
),
560 PROC(GRANTED_MSG
, testargs
, norep
),
561 PROC(TEST_RES
, testres
, norep
),
562 PROC(LOCK_RES
, res
, norep
),
563 PROC(CANCEL_RES
, res
, norep
),
564 PROC(UNLOCK_RES
, res
, norep
),
565 PROC(GRANTED_RES
, res
, norep
),
566 #ifdef NLMCLNT_SUPPORT_SHARES
567 PROC(SHARE
, shareargs
, shareres
),
568 PROC(UNSHARE
, shareargs
, shareres
),
569 PROC(NM_LOCK
, lockargs
, res
),
570 PROC(FREE_ALL
, notify
, void),
574 static struct rpc_version nlm_version1
= {
577 .procs
= nlm_procedures
,
580 static struct rpc_version nlm_version3
= {
583 .procs
= nlm_procedures
,
586 static struct rpc_version
* nlm_versions
[] = {
589 #ifdef CONFIG_LOCKD_V4
594 static struct rpc_stat nlm_stats
;
596 struct rpc_program nlm_program
= {
598 .number
= NLM_PROGRAM
,
599 .nrvers
= ARRAY_SIZE(nlm_versions
),
600 .version
= nlm_versions
,
605 const char *nlmdbg_cookie2a(const struct nlm_cookie
*cookie
)
608 * We can get away with a static buffer because we're only
609 * called with BKL held.
611 static char buf
[2*NLM_MAXCOOKIELEN
+1];
612 unsigned int i
, len
= sizeof(buf
);
615 len
--; /* allow for trailing \0 */
618 for (i
= 0 ; i
< cookie
->len
; i
++) {
623 sprintf(p
, "%02x", cookie
->data
[i
]);