2 * linux/fs/lockd/xdr4.c
4 * XDR support for lockd and the lock client.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 * Copyright (C) 1999, Trond Myklebust <trond.myklebust@fys.uio.no>
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
23 #define NLM_MAXSTRLEN 1024
24 #define OFFSET_MAX ((off_t)LONG_MAX)
26 #define QUADLEN(len) (((len) + 3) >> 2)
28 u32 nlm4_deadlock
, nlm4_rofs
, nlm4_stale_fh
, nlm4_fbig
,
32 typedef struct nlm_args nlm_args
;
35 size_to_off_t(__s64 size
)
37 size
= (size
> (__s64
)LONG_MAX
) ? (off_t
)LONG_MAX
: (off_t
) size
;
38 return (size
< (__s64
)-LONG_MAX
) ? (off_t
)-LONG_MAX
: (off_t
) size
;
42 * XDR functions for basic NLM types
45 nlm4_decode_cookie(u32
*p
, struct nlm_cookie
*c
)
54 memset(c
->data
, 0, 4); /* hockeypux brain damage */
59 memcpy(c
->data
, p
, len
);
65 "lockd: bad cookie size %d (only cookies under 8 bytes are supported.)\n", len
);
72 nlm4_encode_cookie(u32
*p
, struct nlm_cookie
*c
)
75 memcpy(p
, c
->data
, c
->len
);
81 nlm4_decode_fh(u32
*p
, struct nfs_fh
*f
)
83 memset(f
->data
, 0, sizeof(f
->data
));
85 f
->size
= ntohl(*p
++);
86 if (f
->size
> NFS_MAXFHSIZE
) {
88 "lockd: bad fhandle size %x (should be %d)\n",
89 f
->size
, NFS_MAXFHSIZE
);
92 memcpy(f
->data
, p
, f
->size
);
93 return p
+ XDR_QUADLEN(f
->size
);
95 if (ntohl(*p
++) != NFS_FHSIZE
)
96 return NULL
; /* for now, all filehandles are 32 bytes */
97 memcpy(f
->data
, p
, NFS_FHSIZE
);
98 return p
+ XDR_QUADLEN(NFS_FHSIZE
);
103 nlm4_encode_fh(u32
*p
, struct nfs_fh
*f
)
106 *p
++ = htonl(f
->size
);
107 memcpy(p
, f
->data
, f
->size
);
108 return p
+ XDR_QUADLEN(f
->size
);
110 *p
++ = htonl(NFS_FHSIZE
);
111 memcpy(p
, f
->data
, NFS_FHSIZE
);
112 return p
+ XDR_QUADLEN(NFS_FHSIZE
);
117 * Encode and decode owner handle
120 nlm4_decode_oh(u32
*p
, struct xdr_netobj
*oh
)
122 return xdr_decode_netobj(p
, oh
);
126 nlm4_encode_oh(u32
*p
, struct xdr_netobj
*oh
)
128 return xdr_encode_netobj(p
, oh
);
132 nlm4_decode_lock(u32
*p
, struct nlm_lock
*lock
)
134 struct file_lock
*fl
= &lock
->fl
;
135 __s64 len
, start
, end
;
138 if (!(p
= xdr_decode_string(p
, &lock
->caller
, &tmp
, NLM_MAXSTRLEN
))
139 || !(p
= nlm4_decode_fh(p
, &lock
->fh
))
140 || !(p
= nlm4_decode_oh(p
, &lock
->oh
)))
143 memset(fl
, 0, sizeof(*fl
));
144 fl
->fl_owner
= current
->files
;
145 fl
->fl_pid
= ntohl(*p
++);
146 fl
->fl_flags
= FL_POSIX
;
147 fl
->fl_type
= F_RDLCK
; /* as good as anything else */
148 p
= xdr_decode_hyper(p
, &start
);
149 p
= xdr_decode_hyper(p
, &len
);
150 end
= start
+ len
- 1;
152 fl
->fl_start
= size_to_off_t(start
);
153 fl
->fl_end
= size_to_off_t(end
);
155 if (len
== 0 || fl
->fl_end
< 0)
156 fl
->fl_end
= OFFSET_MAX
;
161 * Encode a lock as part of an NLM call
164 nlm4_encode_lock(u32
*p
, struct nlm_lock
*lock
)
166 struct file_lock
*fl
= &lock
->fl
;
168 if (!(p
= xdr_encode_string(p
, lock
->caller
))
169 || !(p
= nlm4_encode_fh(p
, &lock
->fh
))
170 || !(p
= nlm4_encode_oh(p
, &lock
->oh
)))
173 *p
++ = htonl(fl
->fl_pid
);
174 p
= xdr_encode_hyper(p
, fl
->fl_start
);
175 if (fl
->fl_end
== OFFSET_MAX
)
176 p
= xdr_encode_hyper(p
, 0);
178 p
= xdr_encode_hyper(p
, fl
->fl_end
- fl
->fl_start
+ 1);
184 * Encode result of a TEST/TEST_MSG call
187 nlm4_encode_testres(u32
*p
, struct nlm_res
*resp
)
189 dprintk("xdr: before encode_testres (p %p resp %p)\n", p
, resp
);
190 if (!(p
= nlm4_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(fl
->fl_pid
);
200 /* Encode owner handle. */
201 if (!(p
= xdr_encode_netobj(p
, &resp
->lock
.oh
)))
204 p
= xdr_encode_hyper(p
, fl
->fl_start
);
205 if (fl
->fl_end
== OFFSET_MAX
)
206 p
= xdr_encode_hyper(p
, 0);
208 p
= xdr_encode_hyper(p
, fl
->fl_end
- fl
->fl_start
+ 1);
209 dprintk("xdr: encode_testres (status %d pid %d type %d start %ld end %ld)\n", resp
->status
, fl
->fl_pid
, fl
->fl_type
, fl
->fl_start
, fl
->fl_end
);
212 dprintk("xdr: after encode_testres (p %p resp %p)\n", p
, resp
);
218 * Check buffer bounds after decoding arguments
221 xdr_argsize_check(struct svc_rqst
*rqstp
, u32
*p
)
223 struct svc_buf
*buf
= &rqstp
->rq_argbuf
;
225 return p
- buf
->base
<= buf
->buflen
;
229 xdr_ressize_check(struct svc_rqst
*rqstp
, u32
*p
)
231 struct svc_buf
*buf
= &rqstp
->rq_resbuf
;
233 buf
->len
= p
- buf
->base
;
234 return (buf
->len
<= buf
->buflen
);
238 * First, the server side XDR functions
241 nlm4svc_decode_testargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
245 if (!(p
= nlm4_decode_cookie(p
, &argp
->cookie
)))
248 exclusive
= ntohl(*p
++);
249 if (!(p
= nlm4_decode_lock(p
, &argp
->lock
)))
252 argp
->lock
.fl
.fl_type
= F_WRLCK
;
254 return xdr_argsize_check(rqstp
, p
);
258 nlm4svc_encode_testres(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_res
*resp
)
260 if (!(p
= nlm4_encode_testres(p
, resp
)))
262 return xdr_ressize_check(rqstp
, p
);
266 nlm4svc_decode_lockargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
270 if (!(p
= nlm4_decode_cookie(p
, &argp
->cookie
)))
272 argp
->block
= ntohl(*p
++);
273 exclusive
= ntohl(*p
++);
274 if (!(p
= nlm4_decode_lock(p
, &argp
->lock
)))
277 argp
->lock
.fl
.fl_type
= F_WRLCK
;
278 argp
->reclaim
= ntohl(*p
++);
279 argp
->state
= ntohl(*p
++);
280 argp
->monitor
= 1; /* monitor client by default */
282 return xdr_argsize_check(rqstp
, p
);
286 nlm4svc_decode_cancargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
290 if (!(p
= nlm4_decode_cookie(p
, &argp
->cookie
)))
292 argp
->block
= ntohl(*p
++);
293 exclusive
= ntohl(*p
++);
294 if (!(p
= nlm4_decode_lock(p
, &argp
->lock
)))
297 argp
->lock
.fl
.fl_type
= F_WRLCK
;
298 return xdr_argsize_check(rqstp
, p
);
302 nlm4svc_decode_unlockargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
304 if (!(p
= nlm4_decode_cookie(p
, &argp
->cookie
))
305 || !(p
= nlm4_decode_lock(p
, &argp
->lock
)))
307 argp
->lock
.fl
.fl_type
= F_UNLCK
;
308 return xdr_argsize_check(rqstp
, p
);
312 nlm4svc_decode_shareargs(struct svc_rqst
*rqstp
, u32
*p
, nlm_args
*argp
)
314 struct nlm_lock
*lock
= &argp
->lock
;
317 memset(lock
, 0, sizeof(*lock
));
318 lock
->fl
.fl_pid
= ~(u32
) 0;
320 if (!(p
= nlm4_decode_cookie(p
, &argp
->cookie
))
321 || !(p
= xdr_decode_string(p
, &lock
->caller
, &len
, NLM_MAXSTRLEN
))
322 || !(p
= nlm4_decode_fh(p
, &lock
->fh
))
323 || !(p
= nlm4_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 nlm4svc_encode_shareres(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_res
*resp
)
333 if (!(p
= nlm4_encode_cookie(p
, &resp
->cookie
)))
336 *p
++ = xdr_zero
; /* sequence argument */
337 return xdr_ressize_check(rqstp
, p
);
341 nlm4svc_encode_res(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_res
*resp
)
343 if (!(p
= nlm4_encode_cookie(p
, &resp
->cookie
)))
346 return xdr_ressize_check(rqstp
, p
);
350 nlm4svc_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 nlm4svc_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 nlm4svc_decode_res(struct svc_rqst
*rqstp
, u32
*p
, struct nlm_res
*resp
)
374 if (!(p
= nlm4_decode_cookie(p
, &resp
->cookie
)))
376 resp
->status
= ntohl(*p
++);
377 return xdr_argsize_check(rqstp
, p
);
381 nlm4svc_decode_void(struct svc_rqst
*rqstp
, u32
*p
, void *dummy
)
383 return xdr_argsize_check(rqstp
, p
);
387 nlm4svc_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 nlm4clt_encode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
398 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
403 nlm4clt_decode_void(struct rpc_rqst
*req
, u32
*p
, void *ptr
)
409 nlm4clt_encode_testargs(struct rpc_rqst
*req
, u32
*p
, nlm_args
*argp
)
411 struct nlm_lock
*lock
= &argp
->lock
;
413 if (!(p
= nlm4_encode_cookie(p
, &argp
->cookie
)))
415 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
416 if (!(p
= nlm4_encode_lock(p
, lock
)))
418 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
423 nlm4clt_decode_testres(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
425 if (!(p
= nlm4_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
));
435 fl
->fl_pid
= ntohl(*p
++);
436 if (!(p
= nlm4_decode_oh(p
, &resp
->lock
.oh
)))
439 fl
->fl_flags
= FL_POSIX
;
440 fl
->fl_type
= excl
? F_WRLCK
: F_RDLCK
;
441 p
= xdr_decode_hyper(p
, &start
);
442 p
= xdr_decode_hyper(p
, &len
);
443 end
= start
+ len
- 1;
445 fl
->fl_start
= size_to_off_t(start
);
446 fl
->fl_end
= size_to_off_t(end
);
447 if (len
== 0 || fl
->fl_end
< 0)
448 fl
->fl_end
= OFFSET_MAX
;
455 nlm4clt_encode_lockargs(struct rpc_rqst
*req
, u32
*p
, nlm_args
*argp
)
457 struct nlm_lock
*lock
= &argp
->lock
;
459 if (!(p
= nlm4_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
= nlm4_encode_lock(p
, lock
)))
465 *p
++ = argp
->reclaim
? xdr_one
: xdr_zero
;
466 *p
++ = htonl(argp
->state
);
467 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
472 nlm4clt_encode_cancargs(struct rpc_rqst
*req
, u32
*p
, nlm_args
*argp
)
474 struct nlm_lock
*lock
= &argp
->lock
;
476 if (!(p
= nlm4_encode_cookie(p
, &argp
->cookie
)))
478 *p
++ = argp
->block
? xdr_one
: xdr_zero
;
479 *p
++ = (lock
->fl
.fl_type
== F_WRLCK
)? xdr_one
: xdr_zero
;
480 if (!(p
= nlm4_encode_lock(p
, lock
)))
482 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
487 nlm4clt_encode_unlockargs(struct rpc_rqst
*req
, u32
*p
, nlm_args
*argp
)
489 struct nlm_lock
*lock
= &argp
->lock
;
491 if (!(p
= nlm4_encode_cookie(p
, &argp
->cookie
)))
493 if (!(p
= nlm4_encode_lock(p
, lock
)))
495 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
500 nlm4clt_encode_res(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
502 if (!(p
= nlm4_encode_cookie(p
, &resp
->cookie
)))
505 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
510 nlm4clt_encode_testres(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
512 if (!(p
= nlm4_encode_testres(p
, resp
)))
514 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
519 nlm4clt_decode_res(struct rpc_rqst
*req
, u32
*p
, struct nlm_res
*resp
)
521 if (!(p
= nlm4_decode_cookie(p
, &resp
->cookie
)))
523 resp
->status
= ntohl(*p
++);
528 * Buffer requirements for NLM
530 #define NLM4_void_sz 0
531 #define NLM4_cookie_sz 3 /* 1 len , 2 data */
532 #define NLM4_caller_sz 1+XDR_QUADLEN(NLM_MAXSTRLEN)
533 #define NLM4_netobj_sz 1+XDR_QUADLEN(XDR_MAX_NETOBJ)
534 /* #define NLM4_owner_sz 1+XDR_QUADLEN(NLM4_MAXOWNER) */
535 #define NLM4_fhandle_sz 1+XDR_QUADLEN(NFS3_FHSIZE)
536 #define NLM4_lock_sz 5+NLM4_caller_sz+NLM4_netobj_sz+NLM4_fhandle_sz
537 #define NLM4_holder_sz 6+NLM4_netobj_sz
539 #define NLM4_testargs_sz NLM4_cookie_sz+1+NLM4_lock_sz
540 #define NLM4_lockargs_sz NLM4_cookie_sz+4+NLM4_lock_sz
541 #define NLM4_cancargs_sz NLM4_cookie_sz+2+NLM4_lock_sz
542 #define NLM4_unlockargs_sz NLM4_cookie_sz+NLM4_lock_sz
544 #define NLM4_testres_sz NLM4_cookie_sz+1+NLM4_holder_sz
545 #define NLM4_res_sz NLM4_cookie_sz+1
546 #define NLM4_norep_sz 0
549 # define MAX(a,b) (((a) > (b))? (a) : (b))
553 * For NLM, a void procedure really returns nothing
555 #define nlm4clt_decode_norep NULL
557 #define PROC(proc, argtype, restype) \
559 (kxdrproc_t) nlm4clt_encode_##argtype, \
560 (kxdrproc_t) nlm4clt_decode_##restype, \
561 MAX(NLM4_##argtype##_sz, NLM4_##restype##_sz) << 2, \
565 static struct rpc_procinfo nlm4_procedures
[] = {
566 PROC(null
, void, void),
567 PROC(test
, testargs
, testres
),
568 PROC(lock
, lockargs
, res
),
569 PROC(canc
, cancargs
, res
),
570 PROC(unlock
, unlockargs
, res
),
571 PROC(granted
, testargs
, res
),
572 PROC(test_msg
, testargs
, norep
),
573 PROC(lock_msg
, lockargs
, norep
),
574 PROC(canc_msg
, cancargs
, norep
),
575 PROC(unlock_msg
, unlockargs
, norep
),
576 PROC(granted_msg
, testargs
, norep
),
577 PROC(test_res
, testres
, norep
),
578 PROC(lock_res
, res
, norep
),
579 PROC(canc_res
, res
, norep
),
580 PROC(unlock_res
, res
, norep
),
581 PROC(granted_res
, res
, norep
),
582 PROC(undef
, void, void),
583 PROC(undef
, void, void),
584 PROC(undef
, void, void),
585 PROC(undef
, void, void),
586 #ifdef NLMCLNT_SUPPORT_SHARES
587 PROC(share
, shareargs
, shareres
),
588 PROC(unshare
, shareargs
, shareres
),
589 PROC(nm_lock
, lockargs
, res
),
590 PROC(free_all
, notify
, void),
592 PROC(undef
, void, void),
593 PROC(undef
, void, void),
594 PROC(undef
, void, void),
595 PROC(undef
, void, void),
599 struct rpc_version nlm_version4
= {
600 4, 24, nlm4_procedures
,