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/nfs.h>
13 #include <linux/sunrpc/xdr.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/svc.h>
16 #include <linux/sunrpc/stats.h>
17 #include <linux/lockd/lockd.h>
19 #define NLMDBG_FACILITY NLMDBG_XDR
23 s32_to_loff_t(__s32 offset
)
25 return (loff_t
)offset
;
29 loff_t_to_s32(loff_t offset
)
32 if (offset
>= NLM_OFFSET_MAX
)
34 else if (offset
<= -NLM_OFFSET_MAX
)
35 res
= -NLM_OFFSET_MAX
;
42 * XDR functions for basic NLM types
44 static __be32
*nlm_decode_cookie(__be32
*p
, struct nlm_cookie
*c
)
53 memset(c
->data
, 0, 4); /* hockeypux brain damage */
55 else if(len
<=NLM_MAXCOOKIELEN
)
58 memcpy(c
->data
, p
, len
);
63 dprintk("lockd: bad cookie size %d (only cookies under "
64 "%d bytes are supported.)\n",
65 len
, NLM_MAXCOOKIELEN
);
71 static inline __be32
*
72 nlm_encode_cookie(__be32
*p
, struct nlm_cookie
*c
)
75 memcpy(p
, c
->data
, c
->len
);
76 p
+=XDR_QUADLEN(c
->len
);
81 nlm_decode_fh(__be32
*p
, struct nfs_fh
*f
)
85 if ((len
= ntohl(*p
++)) != NFS2_FHSIZE
) {
86 dprintk("lockd: bad fhandle size %d (should be %d)\n",
90 f
->size
= NFS2_FHSIZE
;
91 memset(f
->data
, 0, sizeof(f
->data
));
92 memcpy(f
->data
, p
, NFS2_FHSIZE
);
93 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
96 static inline __be32
*
97 nlm_encode_fh(__be32
*p
, struct nfs_fh
*f
)
99 *p
++ = htonl(NFS2_FHSIZE
);
100 memcpy(p
, f
->data
, NFS2_FHSIZE
);
101 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
105 * Encode and decode owner handle
107 static inline __be32
*
108 nlm_decode_oh(__be32
*p
, struct xdr_netobj
*oh
)
110 return xdr_decode_netobj(p
, oh
);
113 static inline __be32
*
114 nlm_encode_oh(__be32
*p
, struct xdr_netobj
*oh
)
116 return xdr_encode_netobj(p
, oh
);
120 nlm_decode_lock(__be32
*p
, struct nlm_lock
*lock
)
122 struct file_lock
*fl
= &lock
->fl
;
125 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
128 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
129 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
131 lock
->svid
= ntohl(*p
++);
134 fl
->fl_owner
= current
->files
;
135 fl
->fl_pid
= (pid_t
)lock
->svid
;
136 fl
->fl_flags
= FL_POSIX
;
137 fl
->fl_type
= F_RDLCK
; /* as good as anything else */
140 end
= start
+ len
- 1;
142 fl
->fl_start
= s32_to_loff_t(start
);
144 if (len
== 0 || end
< 0)
145 fl
->fl_end
= OFFSET_MAX
;
147 fl
->fl_end
= s32_to_loff_t(end
);
152 * Encode a lock as part of an NLM call
155 nlm_encode_lock(__be32
*p
, struct nlm_lock
*lock
)
157 struct file_lock
*fl
= &lock
->fl
;
160 if (!(p
= xdr_encode_string(p
, lock
->caller
))
161 || !(p
= nlm_encode_fh(p
, &lock
->fh
))
162 || !(p
= nlm_encode_oh(p
, &lock
->oh
)))
165 if (fl
->fl_start
> NLM_OFFSET_MAX
166 || (fl
->fl_end
> NLM_OFFSET_MAX
&& fl
->fl_end
!= OFFSET_MAX
))
169 start
= loff_t_to_s32(fl
->fl_start
);
170 if (fl
->fl_end
== OFFSET_MAX
)
173 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
175 *p
++ = htonl(lock
->svid
);
183 * Encode result of a TEST/TEST_MSG call
186 nlm_encode_testres(__be32
*p
, struct nlm_res
*resp
)
190 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
194 if (resp
->status
== nlm_lck_denied
) {
195 struct file_lock
*fl
= &resp
->lock
.fl
;
197 *p
++ = (fl
->fl_type
== F_RDLCK
)? xdr_zero
: xdr_one
;
198 *p
++ = htonl(resp
->lock
.svid
);
200 /* Encode owner handle. */
201 if (!(p
= xdr_encode_netobj(p
, &resp
->lock
.oh
)))
204 start
= loff_t_to_s32(fl
->fl_start
);
205 if (fl
->fl_end
== OFFSET_MAX
)
208 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
219 * First, the server side XDR functions
222 nlmsvc_decode_testargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
226 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
229 exclusive
= ntohl(*p
++);
230 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
233 argp
->lock
.fl
.fl_type
= F_WRLCK
;
235 return xdr_argsize_check(rqstp
, p
);
239 nlmsvc_encode_testres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
241 if (!(p
= nlm_encode_testres(p
, resp
)))
243 return xdr_ressize_check(rqstp
, p
);
247 nlmsvc_decode_lockargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
251 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
253 argp
->block
= ntohl(*p
++);
254 exclusive
= ntohl(*p
++);
255 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
258 argp
->lock
.fl
.fl_type
= F_WRLCK
;
259 argp
->reclaim
= ntohl(*p
++);
260 argp
->state
= ntohl(*p
++);
261 argp
->monitor
= 1; /* monitor client by default */
263 return xdr_argsize_check(rqstp
, p
);
267 nlmsvc_decode_cancargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
271 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
273 argp
->block
= ntohl(*p
++);
274 exclusive
= ntohl(*p
++);
275 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
278 argp
->lock
.fl
.fl_type
= F_WRLCK
;
279 return xdr_argsize_check(rqstp
, p
);
283 nlmsvc_decode_unlockargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
285 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
286 || !(p
= nlm_decode_lock(p
, &argp
->lock
)))
288 argp
->lock
.fl
.fl_type
= F_UNLCK
;
289 return xdr_argsize_check(rqstp
, p
);
293 nlmsvc_decode_shareargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
295 struct nlm_lock
*lock
= &argp
->lock
;
297 memset(lock
, 0, sizeof(*lock
));
298 locks_init_lock(&lock
->fl
);
299 lock
->svid
= ~(u32
) 0;
300 lock
->fl
.fl_pid
= (pid_t
)lock
->svid
;
302 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
303 || !(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
304 &lock
->len
, NLM_MAXSTRLEN
))
305 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
306 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
308 argp
->fsm_mode
= ntohl(*p
++);
309 argp
->fsm_access
= ntohl(*p
++);
310 return xdr_argsize_check(rqstp
, p
);
314 nlmsvc_encode_shareres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
316 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
319 *p
++ = xdr_zero
; /* sequence argument */
320 return xdr_ressize_check(rqstp
, p
);
324 nlmsvc_encode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
326 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
329 return xdr_ressize_check(rqstp
, p
);
333 nlmsvc_decode_notify(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_args
*argp
)
335 struct nlm_lock
*lock
= &argp
->lock
;
337 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
338 &lock
->len
, NLM_MAXSTRLEN
)))
340 argp
->state
= ntohl(*p
++);
341 return xdr_argsize_check(rqstp
, p
);
345 nlmsvc_decode_reboot(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_reboot
*argp
)
347 if (!(p
= xdr_decode_string_inplace(p
, &argp
->mon
, &argp
->len
, SM_MAXSTRLEN
)))
349 argp
->state
= ntohl(*p
++);
350 memcpy(&argp
->priv
.data
, p
, sizeof(argp
->priv
.data
));
351 p
+= XDR_QUADLEN(SM_PRIV_SIZE
);
352 return xdr_argsize_check(rqstp
, p
);
356 nlmsvc_decode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
358 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
361 return xdr_argsize_check(rqstp
, p
);
365 nlmsvc_decode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
367 return xdr_argsize_check(rqstp
, p
);
371 nlmsvc_encode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
373 return xdr_ressize_check(rqstp
, p
);
377 * Now, the client side XDR functions
379 #ifdef NLMCLNT_SUPPORT_SHARES
381 nlmclt_decode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
388 nlmclt_encode_testargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
390 struct nlm_lock
*lock
= &argp
->lock
;
392 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
394 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
395 if (!(p
= nlm_encode_lock(p
, lock
)))
397 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
402 nlmclt_decode_testres(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
404 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
407 if (resp
->status
== nlm_lck_denied
) {
408 struct file_lock
*fl
= &resp
->lock
.fl
;
412 memset(&resp
->lock
, 0, sizeof(resp
->lock
));
415 resp
->lock
.svid
= ntohl(*p
++);
416 fl
->fl_pid
= (pid_t
)resp
->lock
.svid
;
417 if (!(p
= nlm_decode_oh(p
, &resp
->lock
.oh
)))
420 fl
->fl_flags
= FL_POSIX
;
421 fl
->fl_type
= excl
? F_WRLCK
: F_RDLCK
;
424 end
= start
+ len
- 1;
426 fl
->fl_start
= s32_to_loff_t(start
);
427 if (len
== 0 || end
< 0)
428 fl
->fl_end
= OFFSET_MAX
;
430 fl
->fl_end
= s32_to_loff_t(end
);
437 nlmclt_encode_lockargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
439 struct nlm_lock
*lock
= &argp
->lock
;
441 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
443 *p
++ = argp
->block
? xdr_one
: xdr_zero
;
444 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
445 if (!(p
= nlm_encode_lock(p
, lock
)))
447 *p
++ = argp
->reclaim
? xdr_one
: xdr_zero
;
448 *p
++ = htonl(argp
->state
);
449 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
454 nlmclt_encode_cancargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
456 struct nlm_lock
*lock
= &argp
->lock
;
458 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
460 *p
++ = argp
->block
? xdr_one
: xdr_zero
;
461 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
462 if (!(p
= nlm_encode_lock(p
, lock
)))
464 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
469 nlmclt_encode_unlockargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
471 struct nlm_lock
*lock
= &argp
->lock
;
473 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
475 if (!(p
= nlm_encode_lock(p
, lock
)))
477 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
482 nlmclt_encode_res(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
484 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
487 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
492 nlmclt_encode_testres(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
494 if (!(p
= nlm_encode_testres(p
, resp
)))
496 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
501 nlmclt_decode_res(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
503 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
509 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
510 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
514 * Buffer requirements for NLM
516 #define NLM_void_sz 0
517 #define NLM_cookie_sz 1+XDR_QUADLEN(NLM_MAXCOOKIELEN)
518 #define NLM_caller_sz 1+XDR_QUADLEN(NLMCLNT_OHSIZE)
519 #define NLM_owner_sz 1+XDR_QUADLEN(NLMCLNT_OHSIZE)
520 #define NLM_fhandle_sz 1+XDR_QUADLEN(NFS2_FHSIZE)
521 #define NLM_lock_sz 3+NLM_caller_sz+NLM_owner_sz+NLM_fhandle_sz
522 #define NLM_holder_sz 4+NLM_owner_sz
524 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
525 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
526 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
527 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
529 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
530 #define NLM_res_sz NLM_cookie_sz+1
531 #define NLM_norep_sz 0
534 * For NLM, a void procedure really returns nothing
536 #define nlmclt_decode_norep NULL
538 #define PROC(proc, argtype, restype) \
539 [NLMPROC_##proc] = { \
540 .p_proc = NLMPROC_##proc, \
541 .p_encode = (kxdrproc_t) nlmclt_encode_##argtype, \
542 .p_decode = (kxdrproc_t) nlmclt_decode_##restype, \
543 .p_arglen = NLM_##argtype##_sz, \
544 .p_replen = NLM_##restype##_sz, \
545 .p_statidx = NLMPROC_##proc, \
549 static struct rpc_procinfo nlm_procedures
[] = {
550 PROC(TEST
, testargs
, testres
),
551 PROC(LOCK
, lockargs
, res
),
552 PROC(CANCEL
, cancargs
, res
),
553 PROC(UNLOCK
, unlockargs
, res
),
554 PROC(GRANTED
, testargs
, res
),
555 PROC(TEST_MSG
, testargs
, norep
),
556 PROC(LOCK_MSG
, lockargs
, norep
),
557 PROC(CANCEL_MSG
, cancargs
, norep
),
558 PROC(UNLOCK_MSG
, unlockargs
, norep
),
559 PROC(GRANTED_MSG
, testargs
, norep
),
560 PROC(TEST_RES
, testres
, norep
),
561 PROC(LOCK_RES
, res
, norep
),
562 PROC(CANCEL_RES
, res
, norep
),
563 PROC(UNLOCK_RES
, res
, norep
),
564 PROC(GRANTED_RES
, res
, norep
),
565 #ifdef NLMCLNT_SUPPORT_SHARES
566 PROC(SHARE
, shareargs
, shareres
),
567 PROC(UNSHARE
, shareargs
, shareres
),
568 PROC(NM_LOCK
, lockargs
, res
),
569 PROC(FREE_ALL
, notify
, void),
573 static struct rpc_version nlm_version1
= {
576 .procs
= nlm_procedures
,
579 static struct rpc_version nlm_version3
= {
582 .procs
= nlm_procedures
,
585 static struct rpc_version
* nlm_versions
[] = {
588 #ifdef CONFIG_LOCKD_V4
593 static struct rpc_stat nlm_stats
;
595 struct rpc_program nlm_program
= {
597 .number
= NLM_PROGRAM
,
598 .nrvers
= ARRAY_SIZE(nlm_versions
),
599 .version
= nlm_versions
,
604 const char *nlmdbg_cookie2a(const struct nlm_cookie
*cookie
)
607 * We can get away with a static buffer because we're only
608 * called with BKL held.
610 static char buf
[2*NLM_MAXCOOKIELEN
+1];
611 unsigned int i
, len
= sizeof(buf
);
614 len
--; /* allow for trailing \0 */
617 for (i
= 0 ; i
< cookie
->len
; i
++) {
622 sprintf(p
, "%02x", cookie
->data
[i
]);