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
22 #define NLM_MAXSTRLEN 1024
24 #define QUADLEN(len) (((len) + 3) >> 2)
27 u32 nlm_granted
, nlm_lck_denied
, nlm_lck_denied_nolocks
,
28 nlm_lck_blocked
, nlm_lck_denied_grace_period
;
31 typedef struct nlm_args nlm_args
;
34 * Initialization of NFS status variables
39 static int inited
= 0;
44 nlm_granted
= htonl(NLM_LCK_GRANTED
);
45 nlm_lck_denied
= htonl(NLM_LCK_DENIED
);
46 nlm_lck_denied_nolocks
= htonl(NLM_LCK_DENIED_NOLOCKS
);
47 nlm_lck_blocked
= htonl(NLM_LCK_BLOCKED
);
48 nlm_lck_denied_grace_period
= htonl(NLM_LCK_DENIED_GRACE_PERIOD
);
54 * XDR functions for basic NLM types
56 static inline u32
*nlm_decode_cookie(u32
*p
, struct nlm_cookie
*c
)
65 memset(c
->data
, 0, 4); /* hockeypux brain damage */
70 memcpy(c
->data
, p
, len
);
76 "lockd: bad cookie size %d (only cookies under 8 bytes are supported.)\n", len
);
83 nlm_encode_cookie(u32
*p
, struct nlm_cookie
*c
)
86 memcpy(p
, c
->data
, c
->len
);
92 nlm_decode_fh(u32
*p
, struct nfs_fh
*f
)
96 if ((len
= ntohl(*p
++)) != sizeof(*f
)) {
98 "lockd: bad fhandle size %x (should be %d)\n",
102 memcpy(f
, p
, sizeof(*f
));
103 return p
+ XDR_QUADLEN(sizeof(*f
));
107 nlm_encode_fh(u32
*p
, struct nfs_fh
*f
)
109 *p
++ = htonl(sizeof(*f
));
110 memcpy(p
, f
, sizeof(*f
));
111 return p
+ XDR_QUADLEN(sizeof(*f
));
115 * Encode and decode owner handle
118 nlm_decode_oh(u32
*p
, struct xdr_netobj
*oh
)
120 return xdr_decode_netobj(p
, oh
);
124 nlm_encode_oh(u32
*p
, struct xdr_netobj
*oh
)
126 return xdr_encode_netobj(p
, oh
);
130 nlm_decode_lock(u32
*p
, struct nlm_lock
*lock
)
132 struct file_lock
*fl
= &lock
->fl
;
135 if (!(p
= xdr_decode_string(p
, &lock
->caller
, &len
, NLM_MAXSTRLEN
))
136 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
137 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
140 memset(fl
, 0, sizeof(*fl
));
141 fl
->fl_owner
= current
->files
;
142 fl
->fl_pid
= ntohl(*p
++);
143 fl
->fl_flags
= FL_POSIX
;
144 fl
->fl_type
= F_RDLCK
; /* as good as anything else */
145 fl
->fl_start
= ntohl(*p
++);
147 if (len
== 0 || (fl
->fl_end
= fl
->fl_start
+ len
- 1) < 0)
148 fl
->fl_end
= NLM_OFFSET_MAX
;
153 * Encode a lock as part of an NLM call
156 nlm_encode_lock(u32
*p
, struct nlm_lock
*lock
)
158 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 *p
++ = htonl(fl
->fl_pid
);
166 *p
++ = htonl(lock
->fl
.fl_start
);
167 if (lock
->fl
.fl_end
== NLM_OFFSET_MAX
)
170 *p
++ = htonl(lock
->fl
.fl_end
- lock
->fl
.fl_start
+ 1);
176 * Encode result of a TEST/TEST_MSG call
179 nlm_encode_testres(u32
*p
, struct nlm_res
*resp
)
181 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
185 if (resp
->status
== nlm_lck_denied
) {
186 struct file_lock
*fl
= &resp
->lock
.fl
;
188 *p
++ = (fl
->fl_type
== F_RDLCK
)? xdr_zero
: xdr_one
;
189 *p
++ = htonl(fl
->fl_pid
);
191 /* Encode owner handle. */
192 if (!(p
= xdr_encode_netobj(p
, &resp
->lock
.oh
)))
195 *p
++ = htonl(fl
->fl_start
);
196 if (fl
->fl_end
== NLM_OFFSET_MAX
)
199 *p
++ = htonl(fl
->fl_end
- fl
->fl_start
+ 1);
206 * Check buffer bounds after decoding arguments
209 xdr_argsize_check(struct svc_rqst
*rqstp
, u32
*p
)
211 struct svc_buf
*buf
= &rqstp
->rq_argbuf
;
213 return p
- buf
->base
<= buf
->buflen
;
217 xdr_ressize_check(struct svc_rqst
*rqstp
, u32
*p
)
219 struct svc_buf
*buf
= &rqstp
->rq_resbuf
;
221 buf
->len
= p
- buf
->base
;
222 return (buf
->len
<= buf
->buflen
);
226 * First, the server side XDR functions
229 nlmsvc_decode_testargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
233 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
236 exclusive
= ntohl(*p
++);
237 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
240 argp
->lock
.fl
.fl_type
= F_WRLCK
;
242 return xdr_argsize_check(rqstp
, p
);
246 nlmsvc_encode_testres(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_res
*resp
)
248 if (!(p
= nlm_encode_testres(p
, resp
)))
250 return xdr_ressize_check(rqstp
, p
);
254 nlmsvc_decode_lockargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
258 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
260 argp
->block
= ntohl(*p
++);
261 exclusive
= ntohl(*p
++);
262 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
265 argp
->lock
.fl
.fl_type
= F_WRLCK
;
266 argp
->reclaim
= ntohl(*p
++);
267 argp
->state
= ntohl(*p
++);
268 argp
->monitor
= 1; /* monitor client by default */
270 return xdr_argsize_check(rqstp
, p
);
274 nlmsvc_decode_cancargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
278 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
280 argp
->block
= ntohl(*p
++);
281 exclusive
= ntohl(*p
++);
282 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
285 argp
->lock
.fl
.fl_type
= F_WRLCK
;
286 return xdr_argsize_check(rqstp
, p
);
290 nlmsvc_decode_unlockargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
292 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
293 || !(p
= nlm_decode_lock(p
, &argp
->lock
)))
295 argp
->lock
.fl
.fl_type
= F_UNLCK
;
296 return xdr_argsize_check(rqstp
, p
);
300 nlmsvc_decode_shareargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
302 struct nlm_lock
*lock
= &argp
->lock
;
305 memset(lock
, 0, sizeof(*lock
));
306 lock
->fl
.fl_pid
= ~(u32
) 0;
308 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
309 || !(p
= xdr_decode_string(p
, &lock
->caller
, &len
, NLM_MAXSTRLEN
))
310 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
311 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
313 argp
->fsm_mode
= ntohl(*p
++);
314 argp
->fsm_access
= ntohl(*p
++);
315 return xdr_argsize_check(rqstp
, p
);
319 nlmsvc_encode_shareres(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_res
*resp
)
321 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
324 *p
++ = xdr_zero
; /* sequence argument */
325 return xdr_ressize_check(rqstp
, p
);
329 nlmsvc_encode_res(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_res
*resp
)
331 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
334 return xdr_ressize_check(rqstp
, p
);
338 nlmsvc_decode_notify(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_args
*argp
)
340 struct nlm_lock
*lock
= &argp
->lock
;
343 if (!(p
= xdr_decode_string(p
, &lock
->caller
, &len
, NLM_MAXSTRLEN
)))
345 argp
->state
= ntohl(*p
++);
346 return xdr_argsize_check(rqstp
, p
);
350 nlmsvc_decode_reboot(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_reboot
*argp
)
352 if (!(p
= xdr_decode_string(p
, &argp
->mon
, &argp
->len
, SM_MAXSTRLEN
)))
354 argp
->state
= ntohl(*p
++);
355 argp
->addr
= ntohl(*p
++);
356 return xdr_argsize_check(rqstp
, p
);
360 nlmsvc_decode_res(struct svc_rqst
*rqstp
, u32
*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
, u32
*p
, void *dummy
)
371 return xdr_argsize_check(rqstp
, p
);
375 nlmsvc_encode_void(struct svc_rqst
*rqstp
, u32
*p
, void *dummy
)
377 return xdr_ressize_check(rqstp
, p
);
381 * Now, the client side XDR functions
384 nlmclt_encode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
386 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
391 nlmclt_decode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
397 nlmclt_encode_testargs(struct rpc_rqst
*req
, u32
*p
, nlm_args
*argp
)
399 struct nlm_lock
*lock
= &argp
->lock
;
401 if (!(p
= nlm_encode_cookie(p
, &argp
->cookie
)))
403 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
404 if (!(p
= nlm_encode_lock(p
, lock
)))
406 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
411 nlmclt_decode_testres(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
413 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
415 resp
->status
= ntohl(*p
++);
416 if (resp
->status
== NLM_LCK_DENIED
) {
417 struct file_lock
*fl
= &resp
->lock
.fl
;
420 memset(&resp
->lock
, 0, sizeof(resp
->lock
));
422 fl
->fl_pid
= ntohl(*p
++);
423 if (!(p
= nlm_decode_oh(p
, &resp
->lock
.oh
)))
426 fl
->fl_flags
= FL_POSIX
;
427 fl
->fl_type
= excl
? F_WRLCK
: F_RDLCK
;
428 fl
->fl_start
= ntohl(*p
++);
430 if (len
== 0 || (fl
->fl_end
= fl
->fl_start
+ len
- 1) < 0)
431 fl
->fl_end
= NLM_OFFSET_MAX
;
438 nlmclt_encode_lockargs(struct rpc_rqst
*req
, u32
*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
, u32
*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
, u32
*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
, u32
*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
, u32
*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
, u32
*p
, struct nlm_res
*resp
)
504 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
506 resp
->status
= ntohl(*p
++);
511 * Buffer requirements for NLM
513 #define NLM_void_sz 0
514 #define NLM_cookie_sz 3 /* 1 len , 2 data */
515 #define NLM_caller_sz 1+QUADLEN(sizeof(system_utsname.nodename))
516 #define NLM_netobj_sz 1+QUADLEN(XDR_MAX_NETOBJ)
517 /* #define NLM_owner_sz 1+QUADLEN(NLM_MAXOWNER) */
518 #define NLM_fhandle_sz 1+QUADLEN(NFS_FHSIZE)
519 #define NLM_lock_sz 3+NLM_caller_sz+NLM_netobj_sz+NLM_fhandle_sz
520 #define NLM_holder_sz 4+NLM_netobj_sz
522 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
523 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
524 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
525 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
527 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
528 #define NLM_res_sz NLM_cookie_sz+1
529 #define NLM_norep_sz 0
532 # define MAX(a, b) (((a) > (b))? (a) : (b))
536 * For NLM, a void procedure really returns nothing
538 #define nlmclt_decode_norep NULL
540 #define PROC(proc, argtype, restype) \
542 (kxdrproc_t) nlmclt_encode_##argtype, \
543 (kxdrproc_t) nlmclt_decode_##restype, \
544 MAX(NLM_##argtype##_sz, NLM_##restype##_sz) << 2 \
547 static struct rpc_procinfo nlm_procedures
[] = {
548 PROC(null
, void, void),
549 PROC(test
, testargs
, testres
),
550 PROC(lock
, lockargs
, res
),
551 PROC(canc
, cancargs
, res
),
552 PROC(unlock
, unlockargs
, res
),
553 PROC(granted
, testargs
, res
),
554 PROC(test_msg
, testargs
, norep
),
555 PROC(lock_msg
, lockargs
, norep
),
556 PROC(canc_msg
, cancargs
, norep
),
557 PROC(unlock_msg
, unlockargs
, norep
),
558 PROC(granted_msg
, testargs
, norep
),
559 PROC(test_res
, testres
, norep
),
560 PROC(lock_res
, res
, norep
),
561 PROC(canc_res
, res
, norep
),
562 PROC(unlock_res
, res
, norep
),
563 PROC(granted_res
, res
, norep
),
564 PROC(undef
, void, void),
565 PROC(undef
, void, void),
566 PROC(undef
, void, void),
567 PROC(undef
, void, void),
568 #ifdef NLMCLNT_SUPPORT_SHARES
569 PROC(share
, shareargs
, shareres
),
570 PROC(unshare
, shareargs
, shareres
),
571 PROC(nm_lock
, lockargs
, res
),
572 PROC(free_all
, notify
, void),
574 PROC(undef
, void, void),
575 PROC(undef
, void, void),
576 PROC(undef
, void, void),
577 PROC(undef
, void, void),
581 static struct rpc_version nlm_version1
= {
582 1, 16, nlm_procedures
,
585 static struct rpc_version nlm_version3
= {
586 3, 24, nlm_procedures
,
589 static struct rpc_version
* nlm_versions
[] = {
596 static struct rpc_stat nlm_stats
;
598 struct rpc_program nlm_program
= {
601 sizeof(nlm_versions
) / sizeof(nlm_versions
[0]),
608 nlm_procname(u32 proc
)
610 if (proc
< sizeof(nlm_procedures
)/sizeof(nlm_procedures
[0]))
611 return nlm_procedures
[proc
].p_procname
;