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>
19 #include <linux/lockd/sm_inter.h>
21 #define NLMDBG_FACILITY NLMDBG_XDR
25 s32_to_loff_t(__s32 offset
)
27 return (loff_t
)offset
;
31 loff_t_to_s32(loff_t offset
)
34 if (offset
>= NLM_OFFSET_MAX
)
36 else if (offset
<= -NLM_OFFSET_MAX
)
37 res
= -NLM_OFFSET_MAX
;
44 * XDR functions for basic NLM types
46 static __be32
*nlm_decode_cookie(__be32
*p
, struct nlm_cookie
*c
)
55 memset(c
->data
, 0, 4); /* hockeypux brain damage */
57 else if(len
<=NLM_MAXCOOKIELEN
)
60 memcpy(c
->data
, p
, len
);
66 "lockd: bad cookie size %d (only cookies under %d bytes are supported.)\n", 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
) {
88 "lockd: bad fhandle size %d (should be %d)\n",
92 f
->size
= NFS2_FHSIZE
;
93 memset(f
->data
, 0, sizeof(f
->data
));
94 memcpy(f
->data
, p
, NFS2_FHSIZE
);
95 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
98 static inline __be32
*
99 nlm_encode_fh(__be32
*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
109 static inline __be32
*
110 nlm_decode_oh(__be32
*p
, struct xdr_netobj
*oh
)
112 return xdr_decode_netobj(p
, oh
);
115 static inline __be32
*
116 nlm_encode_oh(__be32
*p
, struct xdr_netobj
*oh
)
118 return xdr_encode_netobj(p
, oh
);
122 nlm_decode_lock(__be32
*p
, struct nlm_lock
*lock
)
124 struct file_lock
*fl
= &lock
->fl
;
127 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
130 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
131 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
133 lock
->svid
= ntohl(*p
++);
136 fl
->fl_owner
= current
->files
;
137 fl
->fl_pid
= (pid_t
)lock
->svid
;
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(__be32
*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(lock
->svid
);
185 * Encode result of a TEST/TEST_MSG call
188 nlm_encode_testres(__be32
*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(resp
->lock
.svid
);
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);
221 * First, the server side XDR functions
224 nlmsvc_decode_testargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
228 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
231 exclusive
= ntohl(*p
++);
232 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
235 argp
->lock
.fl
.fl_type
= F_WRLCK
;
237 return xdr_argsize_check(rqstp
, p
);
241 nlmsvc_encode_testres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
243 if (!(p
= nlm_encode_testres(p
, resp
)))
245 return xdr_ressize_check(rqstp
, p
);
249 nlmsvc_decode_lockargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
253 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
255 argp
->block
= ntohl(*p
++);
256 exclusive
= ntohl(*p
++);
257 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
260 argp
->lock
.fl
.fl_type
= F_WRLCK
;
261 argp
->reclaim
= ntohl(*p
++);
262 argp
->state
= ntohl(*p
++);
263 argp
->monitor
= 1; /* monitor client by default */
265 return xdr_argsize_check(rqstp
, p
);
269 nlmsvc_decode_cancargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
273 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
275 argp
->block
= ntohl(*p
++);
276 exclusive
= ntohl(*p
++);
277 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
280 argp
->lock
.fl
.fl_type
= F_WRLCK
;
281 return xdr_argsize_check(rqstp
, p
);
285 nlmsvc_decode_unlockargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
287 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
288 || !(p
= nlm_decode_lock(p
, &argp
->lock
)))
290 argp
->lock
.fl
.fl_type
= F_UNLCK
;
291 return xdr_argsize_check(rqstp
, p
);
295 nlmsvc_decode_shareargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
297 struct nlm_lock
*lock
= &argp
->lock
;
299 memset(lock
, 0, sizeof(*lock
));
300 locks_init_lock(&lock
->fl
);
301 lock
->svid
= ~(u32
) 0;
302 lock
->fl
.fl_pid
= (pid_t
)lock
->svid
;
304 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
305 || !(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
306 &lock
->len
, NLM_MAXSTRLEN
))
307 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
308 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
310 argp
->fsm_mode
= ntohl(*p
++);
311 argp
->fsm_access
= ntohl(*p
++);
312 return xdr_argsize_check(rqstp
, p
);
316 nlmsvc_encode_shareres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
318 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
321 *p
++ = xdr_zero
; /* sequence argument */
322 return xdr_ressize_check(rqstp
, p
);
326 nlmsvc_encode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
328 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
331 return xdr_ressize_check(rqstp
, p
);
335 nlmsvc_decode_notify(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_args
*argp
)
337 struct nlm_lock
*lock
= &argp
->lock
;
339 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
340 &lock
->len
, NLM_MAXSTRLEN
)))
342 argp
->state
= ntohl(*p
++);
343 return xdr_argsize_check(rqstp
, p
);
347 nlmsvc_decode_reboot(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_reboot
*argp
)
349 if (!(p
= xdr_decode_string_inplace(p
, &argp
->mon
, &argp
->len
, SM_MAXSTRLEN
)))
351 argp
->state
= ntohl(*p
++);
352 /* Preserve the address in network byte order */
356 return xdr_argsize_check(rqstp
, p
);
360 nlmsvc_decode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
362 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
364 resp
->status
= ntohl(*p
++);
365 return xdr_argsize_check(rqstp
, p
);
369 nlmsvc_decode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
371 return xdr_argsize_check(rqstp
, p
);
375 nlmsvc_encode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
377 return xdr_ressize_check(rqstp
, p
);
381 * Now, the client side XDR functions
383 #ifdef NLMCLNT_SUPPORT_SHARES
385 nlmclt_decode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
392 nlmclt_encode_testargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
394 struct nlm_lock
*lock
= &argp
->lock
;
396 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
398 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
399 if (!(p
= nlm_encode_lock(p
, lock
)))
401 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
406 nlmclt_decode_testres(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
408 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
410 resp
->status
= ntohl(*p
++);
411 if (resp
->status
== NLM_LCK_DENIED
) {
412 struct file_lock
*fl
= &resp
->lock
.fl
;
416 memset(&resp
->lock
, 0, sizeof(resp
->lock
));
419 resp
->lock
.svid
= ntohl(*p
++);
420 fl
->fl_pid
= (pid_t
)resp
->lock
.svid
;
421 if (!(p
= nlm_decode_oh(p
, &resp
->lock
.oh
)))
424 fl
->fl_flags
= FL_POSIX
;
425 fl
->fl_type
= excl
? F_WRLCK
: F_RDLCK
;
428 end
= start
+ len
- 1;
430 fl
->fl_start
= s32_to_loff_t(start
);
431 if (len
== 0 || end
< 0)
432 fl
->fl_end
= OFFSET_MAX
;
434 fl
->fl_end
= s32_to_loff_t(end
);
441 nlmclt_encode_lockargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
443 struct nlm_lock
*lock
= &argp
->lock
;
445 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
447 *p
++ = argp
->block
? xdr_one
: xdr_zero
;
448 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
449 if (!(p
= nlm_encode_lock(p
, lock
)))
451 *p
++ = argp
->reclaim
? xdr_one
: xdr_zero
;
452 *p
++ = htonl(argp
->state
);
453 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
458 nlmclt_encode_cancargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
460 struct nlm_lock
*lock
= &argp
->lock
;
462 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
464 *p
++ = argp
->block
? xdr_one
: xdr_zero
;
465 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
466 if (!(p
= nlm_encode_lock(p
, lock
)))
468 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
473 nlmclt_encode_unlockargs(struct rpc_rqst
*req
, __be32
*p
, nlm_args
*argp
)
475 struct nlm_lock
*lock
= &argp
->lock
;
477 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
479 if (!(p
= nlm_encode_lock(p
, lock
)))
481 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
486 nlmclt_encode_res(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
488 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
491 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
496 nlmclt_encode_testres(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
498 if (!(p
= nlm_encode_testres(p
, resp
)))
500 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
505 nlmclt_decode_res(struct rpc_rqst
*req
, __be32
*p
, struct nlm_res
*resp
)
507 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
509 resp
->status
= ntohl(*p
++);
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(sizeof(utsname()->nodename))
519 #define NLM_netobj_sz 1+XDR_QUADLEN(XDR_MAX_NETOBJ)
520 /* #define NLM_owner_sz 1+XDR_QUADLEN(NLM_MAXOWNER) */
521 #define NLM_fhandle_sz 1+XDR_QUADLEN(NFS2_FHSIZE)
522 #define NLM_lock_sz 3+NLM_caller_sz+NLM_netobj_sz+NLM_fhandle_sz
523 #define NLM_holder_sz 4+NLM_netobj_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 # define MAX(a, b) (((a) > (b))? (a) : (b))
539 * For NLM, a void procedure really returns nothing
541 #define nlmclt_decode_norep NULL
543 #define PROC(proc, argtype, restype) \
544 [NLMPROC_##proc] = { \
545 .p_proc = NLMPROC_##proc, \
546 .p_encode = (kxdrproc_t) nlmclt_encode_##argtype, \
547 .p_decode = (kxdrproc_t) nlmclt_decode_##restype, \
548 .p_bufsiz = MAX(NLM_##argtype##_sz, NLM_##restype##_sz) << 2, \
549 .p_statidx = NLMPROC_##proc, \
553 static struct rpc_procinfo nlm_procedures
[] = {
554 PROC(TEST
, testargs
, testres
),
555 PROC(LOCK
, lockargs
, res
),
556 PROC(CANCEL
, cancargs
, res
),
557 PROC(UNLOCK
, unlockargs
, res
),
558 PROC(GRANTED
, testargs
, res
),
559 PROC(TEST_MSG
, testargs
, norep
),
560 PROC(LOCK_MSG
, lockargs
, norep
),
561 PROC(CANCEL_MSG
, cancargs
, norep
),
562 PROC(UNLOCK_MSG
, unlockargs
, norep
),
563 PROC(GRANTED_MSG
, testargs
, norep
),
564 PROC(TEST_RES
, testres
, norep
),
565 PROC(LOCK_RES
, res
, norep
),
566 PROC(CANCEL_RES
, res
, norep
),
567 PROC(UNLOCK_RES
, res
, norep
),
568 PROC(GRANTED_RES
, res
, norep
),
569 #ifdef NLMCLNT_SUPPORT_SHARES
570 PROC(SHARE
, shareargs
, shareres
),
571 PROC(UNSHARE
, shareargs
, shareres
),
572 PROC(NM_LOCK
, lockargs
, res
),
573 PROC(FREE_ALL
, notify
, void),
577 static struct rpc_version nlm_version1
= {
580 .procs
= nlm_procedures
,
583 static struct rpc_version nlm_version3
= {
586 .procs
= nlm_procedures
,
589 #ifdef CONFIG_LOCKD_V4
590 extern struct rpc_version nlm_version4
;
593 static struct rpc_version
* nlm_versions
[] = {
596 #ifdef CONFIG_LOCKD_V4
601 static struct rpc_stat nlm_stats
;
603 struct rpc_program nlm_program
= {
605 .number
= NLM_PROGRAM
,
606 .nrvers
= ARRAY_SIZE(nlm_versions
),
607 .version
= nlm_versions
,
612 const char *nlmdbg_cookie2a(const struct nlm_cookie
*cookie
)
615 * We can get away with a static buffer because we're only
616 * called with BKL held.
618 static char buf
[2*NLM_MAXCOOKIELEN
+1];
620 int len
= sizeof(buf
);
623 len
--; /* allow for trailing \0 */
626 for (i
= 0 ; i
< cookie
->len
; i
++) {
631 sprintf(p
, "%02x", cookie
->data
[i
]);