1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/lockd/clnt4xdr.c
5 * XDR functions to encode/decode NLM version 4 RPC arguments and results.
7 * NLM client-side only.
9 * Copyright (C) 2010, Oracle. All rights reserved.
12 #include <linux/types.h>
13 #include <linux/sunrpc/xdr.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/stats.h>
16 #include <linux/lockd/lockd.h>
18 #include <uapi/linux/nfs3.h>
20 #define NLMDBG_FACILITY NLMDBG_XDR
22 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
23 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
26 #if (NLMCLNT_OHSIZE > NLM_MAXSTRLEN)
27 # error "NLM host name cannot be larger than NLM's maximum string length!"
31 * Declare the space requirements for NLM arguments and replies as
32 * number of 32bit-words
34 #define NLM4_void_sz (0)
35 #define NLM4_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
36 #define NLM4_caller_sz (1+(NLMCLNT_OHSIZE>>2))
37 #define NLM4_owner_sz (1+(NLMCLNT_OHSIZE>>2))
38 #define NLM4_fhandle_sz (1+(NFS3_FHSIZE>>2))
39 #define NLM4_lock_sz (5+NLM4_caller_sz+NLM4_owner_sz+NLM4_fhandle_sz)
40 #define NLM4_holder_sz (6+NLM4_owner_sz)
42 #define NLM4_testargs_sz (NLM4_cookie_sz+1+NLM4_lock_sz)
43 #define NLM4_lockargs_sz (NLM4_cookie_sz+4+NLM4_lock_sz)
44 #define NLM4_cancargs_sz (NLM4_cookie_sz+2+NLM4_lock_sz)
45 #define NLM4_unlockargs_sz (NLM4_cookie_sz+NLM4_lock_sz)
47 #define NLM4_testres_sz (NLM4_cookie_sz+1+NLM4_holder_sz)
48 #define NLM4_res_sz (NLM4_cookie_sz+1)
49 #define NLM4_norep_sz (0)
52 static s64
loff_t_to_s64(loff_t offset
)
56 if (offset
>= NLM4_OFFSET_MAX
)
57 res
= NLM4_OFFSET_MAX
;
58 else if (offset
<= -NLM4_OFFSET_MAX
)
59 res
= -NLM4_OFFSET_MAX
;
65 static void nlm4_compute_offsets(const struct nlm_lock
*lock
,
66 u64
*l_offset
, u64
*l_len
)
68 const struct file_lock
*fl
= &lock
->fl
;
70 *l_offset
= loff_t_to_s64(fl
->fl_start
);
71 if (fl
->fl_end
== OFFSET_MAX
)
74 *l_len
= loff_t_to_s64(fl
->fl_end
- fl
->fl_start
+ 1);
78 * Handle decode buffer overflows out-of-line.
80 static void print_overflow_msg(const char *func
, const struct xdr_stream
*xdr
)
82 dprintk("lockd: %s prematurely hit the end of our receive buffer. "
83 "Remaining buffer length is %tu words.\n",
84 func
, xdr
->end
- xdr
->p
);
89 * Encode/decode NLMv4 basic data types
91 * Basic NLMv4 data types are defined in Appendix II, section 6.1.4
92 * of RFC 1813: "NFS Version 3 Protocol Specification" and in Chapter
93 * 10 of X/Open's "Protocols for Interworking: XNFS, Version 3W".
95 * Not all basic data types have their own encoding and decoding
96 * functions. For run-time efficiency, some data types are encoded
100 static void encode_bool(struct xdr_stream
*xdr
, const int value
)
104 p
= xdr_reserve_space(xdr
, 4);
105 *p
= value
? xdr_one
: xdr_zero
;
108 static void encode_int32(struct xdr_stream
*xdr
, const s32 value
)
112 p
= xdr_reserve_space(xdr
, 4);
113 *p
= cpu_to_be32(value
);
117 * typedef opaque netobj<MAXNETOBJ_SZ>
119 static void encode_netobj(struct xdr_stream
*xdr
,
120 const u8
*data
, const unsigned int length
)
124 p
= xdr_reserve_space(xdr
, 4 + length
);
125 xdr_encode_opaque(p
, data
, length
);
128 static int decode_netobj(struct xdr_stream
*xdr
,
129 struct xdr_netobj
*obj
)
134 p
= xdr_inline_decode(xdr
, 4);
135 if (unlikely(p
== NULL
))
137 length
= be32_to_cpup(p
++);
138 if (unlikely(length
> XDR_MAX_NETOBJ
))
144 dprintk("NFS: returned netobj was too long: %u\n", length
);
147 print_overflow_msg(__func__
, xdr
);
154 static void encode_cookie(struct xdr_stream
*xdr
,
155 const struct nlm_cookie
*cookie
)
157 encode_netobj(xdr
, (u8
*)&cookie
->data
, cookie
->len
);
160 static int decode_cookie(struct xdr_stream
*xdr
,
161 struct nlm_cookie
*cookie
)
166 p
= xdr_inline_decode(xdr
, 4);
167 if (unlikely(p
== NULL
))
169 length
= be32_to_cpup(p
++);
170 /* apparently HPUX can return empty cookies */
173 if (length
> NLM_MAXCOOKIELEN
)
175 p
= xdr_inline_decode(xdr
, length
);
176 if (unlikely(p
== NULL
))
178 cookie
->len
= length
;
179 memcpy(cookie
->data
, p
, length
);
183 memset(cookie
->data
, 0, 4);
186 dprintk("NFS: returned cookie was too long: %u\n", length
);
189 print_overflow_msg(__func__
, xdr
);
196 static void encode_fh(struct xdr_stream
*xdr
, const struct nfs_fh
*fh
)
198 encode_netobj(xdr
, (u8
*)&fh
->data
, fh
->size
);
205 * NLM4_DENIED_NOLOCKS = 2,
207 * NLM4_DENIED_GRACE_PERIOD = 4,
219 * NB: we don't swap bytes for the NLM status values. The upper
220 * layers deal directly with the status value in network byte
223 static void encode_nlm4_stat(struct xdr_stream
*xdr
,
228 BUG_ON(be32_to_cpu(stat
) > NLM_FAILED
);
229 p
= xdr_reserve_space(xdr
, 4);
233 static int decode_nlm4_stat(struct xdr_stream
*xdr
, __be32
*stat
)
237 p
= xdr_inline_decode(xdr
, 4);
238 if (unlikely(p
== NULL
))
240 if (unlikely(ntohl(*p
) > ntohl(nlm4_failed
)))
245 dprintk("%s: server returned invalid nlm4_stats value: %u\n",
246 __func__
, be32_to_cpup(p
));
249 print_overflow_msg(__func__
, xdr
);
254 * struct nlm4_holder {
262 static void encode_nlm4_holder(struct xdr_stream
*xdr
,
263 const struct nlm_res
*result
)
265 const struct nlm_lock
*lock
= &result
->lock
;
269 encode_bool(xdr
, lock
->fl
.fl_type
== F_RDLCK
);
270 encode_int32(xdr
, lock
->svid
);
271 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
273 p
= xdr_reserve_space(xdr
, 4 + 4);
274 nlm4_compute_offsets(lock
, &l_offset
, &l_len
);
275 p
= xdr_encode_hyper(p
, l_offset
);
276 xdr_encode_hyper(p
, l_len
);
279 static int decode_nlm4_holder(struct xdr_stream
*xdr
, struct nlm_res
*result
)
281 struct nlm_lock
*lock
= &result
->lock
;
282 struct file_lock
*fl
= &lock
->fl
;
289 memset(lock
, 0, sizeof(*lock
));
292 p
= xdr_inline_decode(xdr
, 4 + 4);
293 if (unlikely(p
== NULL
))
295 exclusive
= be32_to_cpup(p
++);
296 lock
->svid
= be32_to_cpup(p
);
297 fl
->fl_pid
= (pid_t
)lock
->svid
;
299 error
= decode_netobj(xdr
, &lock
->oh
);
303 p
= xdr_inline_decode(xdr
, 8 + 8);
304 if (unlikely(p
== NULL
))
307 fl
->fl_flags
= FL_POSIX
;
308 fl
->fl_type
= exclusive
!= 0 ? F_WRLCK
: F_RDLCK
;
309 p
= xdr_decode_hyper(p
, &l_offset
);
310 xdr_decode_hyper(p
, &l_len
);
311 end
= l_offset
+ l_len
- 1;
313 fl
->fl_start
= (loff_t
)l_offset
;
314 if (l_len
== 0 || end
< 0)
315 fl
->fl_end
= OFFSET_MAX
;
317 fl
->fl_end
= (loff_t
)end
;
322 print_overflow_msg(__func__
, xdr
);
327 * string caller_name<LM_MAXSTRLEN>;
329 static void encode_caller_name(struct xdr_stream
*xdr
, const char *name
)
331 /* NB: client-side does not set lock->len */
332 u32 length
= strlen(name
);
335 p
= xdr_reserve_space(xdr
, 4 + length
);
336 xdr_encode_opaque(p
, name
, length
);
341 * string caller_name<LM_MAXSTRLEN>;
349 static void encode_nlm4_lock(struct xdr_stream
*xdr
,
350 const struct nlm_lock
*lock
)
355 encode_caller_name(xdr
, lock
->caller
);
356 encode_fh(xdr
, &lock
->fh
);
357 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
359 p
= xdr_reserve_space(xdr
, 4 + 8 + 8);
360 *p
++ = cpu_to_be32(lock
->svid
);
362 nlm4_compute_offsets(lock
, &l_offset
, &l_len
);
363 p
= xdr_encode_hyper(p
, l_offset
);
364 xdr_encode_hyper(p
, l_len
);
369 * NLMv4 XDR encode functions
371 * NLMv4 argument types are defined in Appendix II of RFC 1813:
372 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
373 * "Protocols for Interworking: XNFS, Version 3W".
377 * struct nlm4_testargs {
380 * struct nlm4_lock alock;
383 static void nlm4_xdr_enc_testargs(struct rpc_rqst
*req
,
384 struct xdr_stream
*xdr
,
387 const struct nlm_args
*args
= data
;
388 const struct nlm_lock
*lock
= &args
->lock
;
390 encode_cookie(xdr
, &args
->cookie
);
391 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
392 encode_nlm4_lock(xdr
, lock
);
396 * struct nlm4_lockargs {
400 * struct nlm4_lock alock;
405 static void nlm4_xdr_enc_lockargs(struct rpc_rqst
*req
,
406 struct xdr_stream
*xdr
,
409 const struct nlm_args
*args
= data
;
410 const struct nlm_lock
*lock
= &args
->lock
;
412 encode_cookie(xdr
, &args
->cookie
);
413 encode_bool(xdr
, args
->block
);
414 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
415 encode_nlm4_lock(xdr
, lock
);
416 encode_bool(xdr
, args
->reclaim
);
417 encode_int32(xdr
, args
->state
);
421 * struct nlm4_cancargs {
425 * struct nlm4_lock alock;
428 static void nlm4_xdr_enc_cancargs(struct rpc_rqst
*req
,
429 struct xdr_stream
*xdr
,
432 const struct nlm_args
*args
= data
;
433 const struct nlm_lock
*lock
= &args
->lock
;
435 encode_cookie(xdr
, &args
->cookie
);
436 encode_bool(xdr
, args
->block
);
437 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
438 encode_nlm4_lock(xdr
, lock
);
442 * struct nlm4_unlockargs {
444 * struct nlm4_lock alock;
447 static void nlm4_xdr_enc_unlockargs(struct rpc_rqst
*req
,
448 struct xdr_stream
*xdr
,
451 const struct nlm_args
*args
= data
;
452 const struct nlm_lock
*lock
= &args
->lock
;
454 encode_cookie(xdr
, &args
->cookie
);
455 encode_nlm4_lock(xdr
, lock
);
464 static void nlm4_xdr_enc_res(struct rpc_rqst
*req
,
465 struct xdr_stream
*xdr
,
468 const struct nlm_res
*result
= data
;
470 encode_cookie(xdr
, &result
->cookie
);
471 encode_nlm4_stat(xdr
, result
->status
);
475 * union nlm4_testrply switch (nlm4_stats stat) {
477 * struct nlm4_holder holder;
482 * struct nlm4_testres {
484 * nlm4_testrply test_stat;
487 static void nlm4_xdr_enc_testres(struct rpc_rqst
*req
,
488 struct xdr_stream
*xdr
,
491 const struct nlm_res
*result
= data
;
493 encode_cookie(xdr
, &result
->cookie
);
494 encode_nlm4_stat(xdr
, result
->status
);
495 if (result
->status
== nlm_lck_denied
)
496 encode_nlm4_holder(xdr
, result
);
501 * NLMv4 XDR decode functions
503 * NLMv4 argument types are defined in Appendix II of RFC 1813:
504 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
505 * "Protocols for Interworking: XNFS, Version 3W".
509 * union nlm4_testrply switch (nlm4_stats stat) {
511 * struct nlm4_holder holder;
516 * struct nlm4_testres {
518 * nlm4_testrply test_stat;
521 static int decode_nlm4_testrply(struct xdr_stream
*xdr
,
522 struct nlm_res
*result
)
526 error
= decode_nlm4_stat(xdr
, &result
->status
);
529 if (result
->status
== nlm_lck_denied
)
530 error
= decode_nlm4_holder(xdr
, result
);
535 static int nlm4_xdr_dec_testres(struct rpc_rqst
*req
,
536 struct xdr_stream
*xdr
,
539 struct nlm_res
*result
= data
;
542 error
= decode_cookie(xdr
, &result
->cookie
);
545 error
= decode_nlm4_testrply(xdr
, result
);
556 static int nlm4_xdr_dec_res(struct rpc_rqst
*req
,
557 struct xdr_stream
*xdr
,
560 struct nlm_res
*result
= data
;
563 error
= decode_cookie(xdr
, &result
->cookie
);
566 error
= decode_nlm4_stat(xdr
, &result
->status
);
573 * For NLM, a void procedure really returns nothing
575 #define nlm4_xdr_dec_norep NULL
577 #define PROC(proc, argtype, restype) \
578 [NLMPROC_##proc] = { \
579 .p_proc = NLMPROC_##proc, \
580 .p_encode = nlm4_xdr_enc_##argtype, \
581 .p_decode = nlm4_xdr_dec_##restype, \
582 .p_arglen = NLM4_##argtype##_sz, \
583 .p_replen = NLM4_##restype##_sz, \
584 .p_statidx = NLMPROC_##proc, \
588 static const struct rpc_procinfo nlm4_procedures
[] = {
589 PROC(TEST
, testargs
, testres
),
590 PROC(LOCK
, lockargs
, res
),
591 PROC(CANCEL
, cancargs
, res
),
592 PROC(UNLOCK
, unlockargs
, res
),
593 PROC(GRANTED
, testargs
, res
),
594 PROC(TEST_MSG
, testargs
, norep
),
595 PROC(LOCK_MSG
, lockargs
, norep
),
596 PROC(CANCEL_MSG
, cancargs
, norep
),
597 PROC(UNLOCK_MSG
, unlockargs
, norep
),
598 PROC(GRANTED_MSG
, testargs
, norep
),
599 PROC(TEST_RES
, testres
, norep
),
600 PROC(LOCK_RES
, res
, norep
),
601 PROC(CANCEL_RES
, res
, norep
),
602 PROC(UNLOCK_RES
, res
, norep
),
603 PROC(GRANTED_RES
, res
, norep
),
606 static unsigned int nlm_version4_counts
[ARRAY_SIZE(nlm4_procedures
)];
607 const struct rpc_version nlm_version4
= {
609 .nrprocs
= ARRAY_SIZE(nlm4_procedures
),
610 .procs
= nlm4_procedures
,
611 .counts
= nlm_version4_counts
,