2 * linux/fs/lockd/clnt4xdr.c
4 * XDR functions to encode/decode NLM version 4 RPC arguments and results.
6 * NLM client-side only.
8 * Copyright (C) 2010, Oracle. All rights reserved.
11 #include <linux/types.h>
12 #include <linux/sunrpc/xdr.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/sunrpc/stats.h>
15 #include <linux/lockd/lockd.h>
17 #define NLMDBG_FACILITY NLMDBG_XDR
19 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
20 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
23 #if (NLMCLNT_OHSIZE > NLM_MAXSTRLEN)
24 # error "NLM host name cannot be larger than NLM's maximum string length!"
28 * Declare the space requirements for NLM arguments and replies as
29 * number of 32bit-words
31 #define NLM4_void_sz (0)
32 #define NLM4_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
33 #define NLM4_caller_sz (1+(NLMCLNT_OHSIZE>>2))
34 #define NLM4_owner_sz (1+(NLMCLNT_OHSIZE>>2))
35 #define NLM4_fhandle_sz (1+(NFS3_FHSIZE>>2))
36 #define NLM4_lock_sz (5+NLM4_caller_sz+NLM4_owner_sz+NLM4_fhandle_sz)
37 #define NLM4_holder_sz (6+NLM4_owner_sz)
39 #define NLM4_testargs_sz (NLM4_cookie_sz+1+NLM4_lock_sz)
40 #define NLM4_lockargs_sz (NLM4_cookie_sz+4+NLM4_lock_sz)
41 #define NLM4_cancargs_sz (NLM4_cookie_sz+2+NLM4_lock_sz)
42 #define NLM4_unlockargs_sz (NLM4_cookie_sz+NLM4_lock_sz)
44 #define NLM4_testres_sz (NLM4_cookie_sz+1+NLM4_holder_sz)
45 #define NLM4_res_sz (NLM4_cookie_sz+1)
46 #define NLM4_norep_sz (0)
49 static s64
loff_t_to_s64(loff_t offset
)
53 if (offset
>= NLM4_OFFSET_MAX
)
54 res
= NLM4_OFFSET_MAX
;
55 else if (offset
<= -NLM4_OFFSET_MAX
)
56 res
= -NLM4_OFFSET_MAX
;
62 static void nlm4_compute_offsets(const struct nlm_lock
*lock
,
63 u64
*l_offset
, u64
*l_len
)
65 const struct file_lock
*fl
= &lock
->fl
;
67 *l_offset
= loff_t_to_s64(fl
->fl_start
);
68 if (fl
->fl_end
== OFFSET_MAX
)
71 *l_len
= loff_t_to_s64(fl
->fl_end
- fl
->fl_start
+ 1);
75 * Handle decode buffer overflows out-of-line.
77 static void print_overflow_msg(const char *func
, const struct xdr_stream
*xdr
)
79 dprintk("lockd: %s prematurely hit the end of our receive buffer. "
80 "Remaining buffer length is %tu words.\n",
81 func
, xdr
->end
- xdr
->p
);
86 * Encode/decode NLMv4 basic data types
88 * Basic NLMv4 data types are defined in Appendix II, section 6.1.4
89 * of RFC 1813: "NFS Version 3 Protocol Specification" and in Chapter
90 * 10 of X/Open's "Protocols for Interworking: XNFS, Version 3W".
92 * Not all basic data types have their own encoding and decoding
93 * functions. For run-time efficiency, some data types are encoded
97 static void encode_bool(struct xdr_stream
*xdr
, const int value
)
101 p
= xdr_reserve_space(xdr
, 4);
102 *p
= value
? xdr_one
: xdr_zero
;
105 static void encode_int32(struct xdr_stream
*xdr
, const s32 value
)
109 p
= xdr_reserve_space(xdr
, 4);
110 *p
= cpu_to_be32(value
);
114 * typedef opaque netobj<MAXNETOBJ_SZ>
116 static void encode_netobj(struct xdr_stream
*xdr
,
117 const u8
*data
, const unsigned int length
)
121 p
= xdr_reserve_space(xdr
, 4 + length
);
122 xdr_encode_opaque(p
, data
, length
);
125 static int decode_netobj(struct xdr_stream
*xdr
,
126 struct xdr_netobj
*obj
)
131 p
= xdr_inline_decode(xdr
, 4);
132 if (unlikely(p
== NULL
))
134 length
= be32_to_cpup(p
++);
135 if (unlikely(length
> XDR_MAX_NETOBJ
))
141 dprintk("NFS: returned netobj was too long: %u\n", length
);
144 print_overflow_msg(__func__
, xdr
);
151 static void encode_cookie(struct xdr_stream
*xdr
,
152 const struct nlm_cookie
*cookie
)
154 encode_netobj(xdr
, (u8
*)&cookie
->data
, cookie
->len
);
157 static int decode_cookie(struct xdr_stream
*xdr
,
158 struct nlm_cookie
*cookie
)
163 p
= xdr_inline_decode(xdr
, 4);
164 if (unlikely(p
== NULL
))
166 length
= be32_to_cpup(p
++);
167 /* apparently HPUX can return empty cookies */
170 if (length
> NLM_MAXCOOKIELEN
)
172 p
= xdr_inline_decode(xdr
, length
);
173 if (unlikely(p
== NULL
))
175 cookie
->len
= length
;
176 memcpy(cookie
->data
, p
, length
);
180 memset(cookie
->data
, 0, 4);
183 dprintk("NFS: returned cookie was too long: %u\n", length
);
186 print_overflow_msg(__func__
, xdr
);
193 static void encode_fh(struct xdr_stream
*xdr
, const struct nfs_fh
*fh
)
195 encode_netobj(xdr
, (u8
*)&fh
->data
, fh
->size
);
202 * NLM4_DENIED_NOLOCKS = 2,
204 * NLM4_DENIED_GRACE_PERIOD = 4,
216 * NB: we don't swap bytes for the NLM status values. The upper
217 * layers deal directly with the status value in network byte
220 static void encode_nlm4_stat(struct xdr_stream
*xdr
,
225 BUG_ON(be32_to_cpu(stat
) > NLM_FAILED
);
226 p
= xdr_reserve_space(xdr
, 4);
230 static int decode_nlm4_stat(struct xdr_stream
*xdr
, __be32
*stat
)
234 p
= xdr_inline_decode(xdr
, 4);
235 if (unlikely(p
== NULL
))
237 if (unlikely(ntohl(*p
) > ntohl(nlm4_failed
)))
242 dprintk("%s: server returned invalid nlm4_stats value: %u\n",
243 __func__
, be32_to_cpup(p
));
246 print_overflow_msg(__func__
, xdr
);
251 * struct nlm4_holder {
259 static void encode_nlm4_holder(struct xdr_stream
*xdr
,
260 const struct nlm_res
*result
)
262 const struct nlm_lock
*lock
= &result
->lock
;
266 encode_bool(xdr
, lock
->fl
.fl_type
== F_RDLCK
);
267 encode_int32(xdr
, lock
->svid
);
268 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
270 p
= xdr_reserve_space(xdr
, 4 + 4);
271 nlm4_compute_offsets(lock
, &l_offset
, &l_len
);
272 p
= xdr_encode_hyper(p
, l_offset
);
273 xdr_encode_hyper(p
, l_len
);
276 static int decode_nlm4_holder(struct xdr_stream
*xdr
, struct nlm_res
*result
)
278 struct nlm_lock
*lock
= &result
->lock
;
279 struct file_lock
*fl
= &lock
->fl
;
286 memset(lock
, 0, sizeof(*lock
));
289 p
= xdr_inline_decode(xdr
, 4 + 4);
290 if (unlikely(p
== NULL
))
292 exclusive
= be32_to_cpup(p
++);
293 lock
->svid
= be32_to_cpup(p
);
294 fl
->fl_pid
= (pid_t
)lock
->svid
;
296 error
= decode_netobj(xdr
, &lock
->oh
);
300 p
= xdr_inline_decode(xdr
, 8 + 8);
301 if (unlikely(p
== NULL
))
304 fl
->fl_flags
= FL_POSIX
;
305 fl
->fl_type
= exclusive
!= 0 ? F_WRLCK
: F_RDLCK
;
306 p
= xdr_decode_hyper(p
, &l_offset
);
307 xdr_decode_hyper(p
, &l_len
);
308 end
= l_offset
+ l_len
- 1;
310 fl
->fl_start
= (loff_t
)l_offset
;
311 if (l_len
== 0 || end
< 0)
312 fl
->fl_end
= OFFSET_MAX
;
314 fl
->fl_end
= (loff_t
)end
;
319 print_overflow_msg(__func__
, xdr
);
324 * string caller_name<LM_MAXSTRLEN>;
326 static void encode_caller_name(struct xdr_stream
*xdr
, const char *name
)
328 /* NB: client-side does not set lock->len */
329 u32 length
= strlen(name
);
332 p
= xdr_reserve_space(xdr
, 4 + length
);
333 xdr_encode_opaque(p
, name
, length
);
338 * string caller_name<LM_MAXSTRLEN>;
346 static void encode_nlm4_lock(struct xdr_stream
*xdr
,
347 const struct nlm_lock
*lock
)
352 encode_caller_name(xdr
, lock
->caller
);
353 encode_fh(xdr
, &lock
->fh
);
354 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
356 p
= xdr_reserve_space(xdr
, 4 + 8 + 8);
357 *p
++ = cpu_to_be32(lock
->svid
);
359 nlm4_compute_offsets(lock
, &l_offset
, &l_len
);
360 p
= xdr_encode_hyper(p
, l_offset
);
361 xdr_encode_hyper(p
, l_len
);
366 * NLMv4 XDR encode functions
368 * NLMv4 argument types are defined in Appendix II of RFC 1813:
369 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
370 * "Protocols for Interworking: XNFS, Version 3W".
374 * struct nlm4_testargs {
377 * struct nlm4_lock alock;
380 static void nlm4_xdr_enc_testargs(struct rpc_rqst
*req
,
381 struct xdr_stream
*xdr
,
382 const struct nlm_args
*args
)
384 const struct nlm_lock
*lock
= &args
->lock
;
386 encode_cookie(xdr
, &args
->cookie
);
387 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
388 encode_nlm4_lock(xdr
, lock
);
392 * struct nlm4_lockargs {
396 * struct nlm4_lock alock;
401 static void nlm4_xdr_enc_lockargs(struct rpc_rqst
*req
,
402 struct xdr_stream
*xdr
,
403 const struct nlm_args
*args
)
405 const struct nlm_lock
*lock
= &args
->lock
;
407 encode_cookie(xdr
, &args
->cookie
);
408 encode_bool(xdr
, args
->block
);
409 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
410 encode_nlm4_lock(xdr
, lock
);
411 encode_bool(xdr
, args
->reclaim
);
412 encode_int32(xdr
, args
->state
);
416 * struct nlm4_cancargs {
420 * struct nlm4_lock alock;
423 static void nlm4_xdr_enc_cancargs(struct rpc_rqst
*req
,
424 struct xdr_stream
*xdr
,
425 const struct nlm_args
*args
)
427 const struct nlm_lock
*lock
= &args
->lock
;
429 encode_cookie(xdr
, &args
->cookie
);
430 encode_bool(xdr
, args
->block
);
431 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
432 encode_nlm4_lock(xdr
, lock
);
436 * struct nlm4_unlockargs {
438 * struct nlm4_lock alock;
441 static void nlm4_xdr_enc_unlockargs(struct rpc_rqst
*req
,
442 struct xdr_stream
*xdr
,
443 const struct nlm_args
*args
)
445 const struct nlm_lock
*lock
= &args
->lock
;
447 encode_cookie(xdr
, &args
->cookie
);
448 encode_nlm4_lock(xdr
, lock
);
457 static void nlm4_xdr_enc_res(struct rpc_rqst
*req
,
458 struct xdr_stream
*xdr
,
459 const struct nlm_res
*result
)
461 encode_cookie(xdr
, &result
->cookie
);
462 encode_nlm4_stat(xdr
, result
->status
);
466 * union nlm4_testrply switch (nlm4_stats stat) {
468 * struct nlm4_holder holder;
473 * struct nlm4_testres {
475 * nlm4_testrply test_stat;
478 static void nlm4_xdr_enc_testres(struct rpc_rqst
*req
,
479 struct xdr_stream
*xdr
,
480 const struct nlm_res
*result
)
482 encode_cookie(xdr
, &result
->cookie
);
483 encode_nlm4_stat(xdr
, result
->status
);
484 if (result
->status
== nlm_lck_denied
)
485 encode_nlm4_holder(xdr
, result
);
490 * NLMv4 XDR decode functions
492 * NLMv4 argument types are defined in Appendix II of RFC 1813:
493 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
494 * "Protocols for Interworking: XNFS, Version 3W".
498 * union nlm4_testrply switch (nlm4_stats stat) {
500 * struct nlm4_holder holder;
505 * struct nlm4_testres {
507 * nlm4_testrply test_stat;
510 static int decode_nlm4_testrply(struct xdr_stream
*xdr
,
511 struct nlm_res
*result
)
515 error
= decode_nlm4_stat(xdr
, &result
->status
);
518 if (result
->status
== nlm_lck_denied
)
519 error
= decode_nlm4_holder(xdr
, result
);
524 static int nlm4_xdr_dec_testres(struct rpc_rqst
*req
,
525 struct xdr_stream
*xdr
,
526 struct nlm_res
*result
)
530 error
= decode_cookie(xdr
, &result
->cookie
);
533 error
= decode_nlm4_testrply(xdr
, result
);
544 static int nlm4_xdr_dec_res(struct rpc_rqst
*req
,
545 struct xdr_stream
*xdr
,
546 struct nlm_res
*result
)
550 error
= decode_cookie(xdr
, &result
->cookie
);
553 error
= decode_nlm4_stat(xdr
, &result
->status
);
560 * For NLM, a void procedure really returns nothing
562 #define nlm4_xdr_dec_norep NULL
564 #define PROC(proc, argtype, restype) \
565 [NLMPROC_##proc] = { \
566 .p_proc = NLMPROC_##proc, \
567 .p_encode = (kxdreproc_t)nlm4_xdr_enc_##argtype, \
568 .p_decode = (kxdrdproc_t)nlm4_xdr_dec_##restype, \
569 .p_arglen = NLM4_##argtype##_sz, \
570 .p_replen = NLM4_##restype##_sz, \
571 .p_statidx = NLMPROC_##proc, \
575 static struct rpc_procinfo nlm4_procedures
[] = {
576 PROC(TEST
, testargs
, testres
),
577 PROC(LOCK
, lockargs
, res
),
578 PROC(CANCEL
, cancargs
, res
),
579 PROC(UNLOCK
, unlockargs
, res
),
580 PROC(GRANTED
, testargs
, res
),
581 PROC(TEST_MSG
, testargs
, norep
),
582 PROC(LOCK_MSG
, lockargs
, norep
),
583 PROC(CANCEL_MSG
, cancargs
, norep
),
584 PROC(UNLOCK_MSG
, unlockargs
, norep
),
585 PROC(GRANTED_MSG
, testargs
, norep
),
586 PROC(TEST_RES
, testres
, norep
),
587 PROC(LOCK_RES
, res
, norep
),
588 PROC(CANCEL_RES
, res
, norep
),
589 PROC(UNLOCK_RES
, res
, norep
),
590 PROC(GRANTED_RES
, res
, norep
),
593 const struct rpc_version nlm_version4
= {
595 .nrprocs
= ARRAY_SIZE(nlm4_procedures
),
596 .procs
= nlm4_procedures
,