1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/lockd/clntxdr.c
5 * XDR functions to encode/decode NLM version 3 RPC arguments and results.
6 * NLM version 3 is backwards compatible with NLM versions 1 and 2.
8 * NLM client-side only.
10 * Copyright (C) 2010, Oracle. All rights reserved.
13 #include <linux/types.h>
14 #include <linux/sunrpc/xdr.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/stats.h>
17 #include <linux/lockd/lockd.h>
19 #include <uapi/linux/nfs2.h>
21 #define NLMDBG_FACILITY NLMDBG_XDR
23 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
24 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
28 * Declare the space requirements for NLM arguments and replies as
29 * number of 32bit-words
31 #define NLM_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
32 #define NLM_caller_sz (1+(NLMCLNT_OHSIZE>>2))
33 #define NLM_owner_sz (1+(NLMCLNT_OHSIZE>>2))
34 #define NLM_fhandle_sz (1+(NFS2_FHSIZE>>2))
35 #define NLM_lock_sz (3+NLM_caller_sz+NLM_owner_sz+NLM_fhandle_sz)
36 #define NLM_holder_sz (4+NLM_owner_sz)
38 #define NLM_testargs_sz (NLM_cookie_sz+1+NLM_lock_sz)
39 #define NLM_lockargs_sz (NLM_cookie_sz+4+NLM_lock_sz)
40 #define NLM_cancargs_sz (NLM_cookie_sz+2+NLM_lock_sz)
41 #define NLM_unlockargs_sz (NLM_cookie_sz+NLM_lock_sz)
43 #define NLM_testres_sz (NLM_cookie_sz+1+NLM_holder_sz)
44 #define NLM_res_sz (NLM_cookie_sz+1)
45 #define NLM_norep_sz (0)
48 static s32
loff_t_to_s32(loff_t offset
)
52 if (offset
>= NLM_OFFSET_MAX
)
54 else if (offset
<= -NLM_OFFSET_MAX
)
55 res
= -NLM_OFFSET_MAX
;
61 static void nlm_compute_offsets(const struct nlm_lock
*lock
,
62 u32
*l_offset
, u32
*l_len
)
64 const struct file_lock
*fl
= &lock
->fl
;
66 *l_offset
= loff_t_to_s32(fl
->fl_start
);
67 if (fl
->fl_end
== OFFSET_MAX
)
70 *l_len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
74 * Handle decode buffer overflows out-of-line.
76 static void print_overflow_msg(const char *func
, const struct xdr_stream
*xdr
)
78 dprintk("lockd: %s prematurely hit the end of our receive buffer. "
79 "Remaining buffer length is %tu words.\n",
80 func
, xdr
->end
- xdr
->p
);
85 * Encode/decode NLMv3 basic data types
87 * Basic NLMv3 data types are not defined in an IETF standards
88 * document. X/Open has a description of these data types that
89 * is useful. See Chapter 10 of "Protocols for Interworking:
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
, NFS2_FHSIZE
);
202 * LCK_DENIED_NOLOCKS = 2,
204 * LCK_DENIED_GRACE_PERIOD = 4
212 * NB: we don't swap bytes for the NLM status values. The upper
213 * layers deal directly with the status value in network byte
217 static void encode_nlm_stat(struct xdr_stream
*xdr
,
222 WARN_ON_ONCE(be32_to_cpu(stat
) > NLM_LCK_DENIED_GRACE_PERIOD
);
223 p
= xdr_reserve_space(xdr
, 4);
227 static int decode_nlm_stat(struct xdr_stream
*xdr
,
232 p
= xdr_inline_decode(xdr
, 4);
233 if (unlikely(p
== NULL
))
235 if (unlikely(ntohl(*p
) > ntohl(nlm_lck_denied_grace_period
)))
240 dprintk("%s: server returned invalid nlm_stats value: %u\n",
241 __func__
, be32_to_cpup(p
));
244 print_overflow_msg(__func__
, xdr
);
249 * struct nlm_holder {
257 static void encode_nlm_holder(struct xdr_stream
*xdr
,
258 const struct nlm_res
*result
)
260 const struct nlm_lock
*lock
= &result
->lock
;
264 encode_bool(xdr
, lock
->fl
.fl_type
== F_RDLCK
);
265 encode_int32(xdr
, lock
->svid
);
266 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
268 p
= xdr_reserve_space(xdr
, 4 + 4);
269 nlm_compute_offsets(lock
, &l_offset
, &l_len
);
270 *p
++ = cpu_to_be32(l_offset
);
271 *p
= cpu_to_be32(l_len
);
274 static int decode_nlm_holder(struct xdr_stream
*xdr
, struct nlm_res
*result
)
276 struct nlm_lock
*lock
= &result
->lock
;
277 struct file_lock
*fl
= &lock
->fl
;
278 u32 exclusive
, l_offset
, l_len
;
283 memset(lock
, 0, sizeof(*lock
));
286 p
= xdr_inline_decode(xdr
, 4 + 4);
287 if (unlikely(p
== NULL
))
289 exclusive
= be32_to_cpup(p
++);
290 lock
->svid
= be32_to_cpup(p
);
291 fl
->fl_pid
= (pid_t
)lock
->svid
;
293 error
= decode_netobj(xdr
, &lock
->oh
);
297 p
= xdr_inline_decode(xdr
, 4 + 4);
298 if (unlikely(p
== NULL
))
301 fl
->fl_flags
= FL_POSIX
;
302 fl
->fl_type
= exclusive
!= 0 ? F_WRLCK
: F_RDLCK
;
303 l_offset
= be32_to_cpup(p
++);
304 l_len
= be32_to_cpup(p
);
305 end
= l_offset
+ l_len
- 1;
307 fl
->fl_start
= (loff_t
)l_offset
;
308 if (l_len
== 0 || end
< 0)
309 fl
->fl_end
= OFFSET_MAX
;
311 fl
->fl_end
= (loff_t
)end
;
316 print_overflow_msg(__func__
, xdr
);
321 * string caller_name<LM_MAXSTRLEN>;
323 static void encode_caller_name(struct xdr_stream
*xdr
, const char *name
)
325 /* NB: client-side does not set lock->len */
326 u32 length
= strlen(name
);
329 p
= xdr_reserve_space(xdr
, 4 + length
);
330 xdr_encode_opaque(p
, name
, length
);
335 * string caller_name<LM_MAXSTRLEN>;
343 static void encode_nlm_lock(struct xdr_stream
*xdr
,
344 const struct nlm_lock
*lock
)
349 encode_caller_name(xdr
, lock
->caller
);
350 encode_fh(xdr
, &lock
->fh
);
351 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
353 p
= xdr_reserve_space(xdr
, 4 + 4 + 4);
354 *p
++ = cpu_to_be32(lock
->svid
);
356 nlm_compute_offsets(lock
, &l_offset
, &l_len
);
357 *p
++ = cpu_to_be32(l_offset
);
358 *p
= cpu_to_be32(l_len
);
363 * NLMv3 XDR encode functions
365 * NLMv3 argument types are defined in Chapter 10 of The Open Group's
366 * "Protocols for Interworking: XNFS, Version 3W".
370 * struct nlm_testargs {
373 * struct nlm_lock alock;
376 static void nlm_xdr_enc_testargs(struct rpc_rqst
*req
,
377 struct xdr_stream
*xdr
,
380 const struct nlm_args
*args
= data
;
381 const struct nlm_lock
*lock
= &args
->lock
;
383 encode_cookie(xdr
, &args
->cookie
);
384 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
385 encode_nlm_lock(xdr
, lock
);
389 * struct nlm_lockargs {
393 * struct nlm_lock alock;
398 static void nlm_xdr_enc_lockargs(struct rpc_rqst
*req
,
399 struct xdr_stream
*xdr
,
402 const struct nlm_args
*args
= data
;
403 const struct nlm_lock
*lock
= &args
->lock
;
405 encode_cookie(xdr
, &args
->cookie
);
406 encode_bool(xdr
, args
->block
);
407 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
408 encode_nlm_lock(xdr
, lock
);
409 encode_bool(xdr
, args
->reclaim
);
410 encode_int32(xdr
, args
->state
);
414 * struct nlm_cancargs {
418 * struct nlm_lock alock;
421 static void nlm_xdr_enc_cancargs(struct rpc_rqst
*req
,
422 struct xdr_stream
*xdr
,
425 const struct nlm_args
*args
= data
;
426 const struct nlm_lock
*lock
= &args
->lock
;
428 encode_cookie(xdr
, &args
->cookie
);
429 encode_bool(xdr
, args
->block
);
430 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
431 encode_nlm_lock(xdr
, lock
);
435 * struct nlm_unlockargs {
437 * struct nlm_lock alock;
440 static void nlm_xdr_enc_unlockargs(struct rpc_rqst
*req
,
441 struct xdr_stream
*xdr
,
444 const struct nlm_args
*args
= data
;
445 const struct nlm_lock
*lock
= &args
->lock
;
447 encode_cookie(xdr
, &args
->cookie
);
448 encode_nlm_lock(xdr
, lock
);
457 static void nlm_xdr_enc_res(struct rpc_rqst
*req
,
458 struct xdr_stream
*xdr
,
461 const struct nlm_res
*result
= data
;
463 encode_cookie(xdr
, &result
->cookie
);
464 encode_nlm_stat(xdr
, result
->status
);
468 * union nlm_testrply switch (nlm_stats stat) {
470 * struct nlm_holder holder;
475 * struct nlm_testres {
477 * nlm_testrply test_stat;
480 static void encode_nlm_testrply(struct xdr_stream
*xdr
,
481 const struct nlm_res
*result
)
483 if (result
->status
== nlm_lck_denied
)
484 encode_nlm_holder(xdr
, result
);
487 static void nlm_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_nlm_stat(xdr
, result
->status
);
495 encode_nlm_testrply(xdr
, result
);
500 * NLMv3 XDR decode functions
502 * NLMv3 result types are defined in Chapter 10 of The Open Group's
503 * "Protocols for Interworking: XNFS, Version 3W".
507 * union nlm_testrply switch (nlm_stats stat) {
509 * struct nlm_holder holder;
514 * struct nlm_testres {
516 * nlm_testrply test_stat;
519 static int decode_nlm_testrply(struct xdr_stream
*xdr
,
520 struct nlm_res
*result
)
524 error
= decode_nlm_stat(xdr
, &result
->status
);
527 if (result
->status
== nlm_lck_denied
)
528 error
= decode_nlm_holder(xdr
, result
);
533 static int nlm_xdr_dec_testres(struct rpc_rqst
*req
,
534 struct xdr_stream
*xdr
,
537 struct nlm_res
*result
= data
;
540 error
= decode_cookie(xdr
, &result
->cookie
);
543 error
= decode_nlm_testrply(xdr
, result
);
554 static int nlm_xdr_dec_res(struct rpc_rqst
*req
,
555 struct xdr_stream
*xdr
,
558 struct nlm_res
*result
= data
;
561 error
= decode_cookie(xdr
, &result
->cookie
);
564 error
= decode_nlm_stat(xdr
, &result
->status
);
571 * For NLM, a void procedure really returns nothing
573 #define nlm_xdr_dec_norep NULL
575 #define PROC(proc, argtype, restype) \
576 [NLMPROC_##proc] = { \
577 .p_proc = NLMPROC_##proc, \
578 .p_encode = nlm_xdr_enc_##argtype, \
579 .p_decode = nlm_xdr_dec_##restype, \
580 .p_arglen = NLM_##argtype##_sz, \
581 .p_replen = NLM_##restype##_sz, \
582 .p_statidx = NLMPROC_##proc, \
586 static const struct rpc_procinfo nlm_procedures
[] = {
587 PROC(TEST
, testargs
, testres
),
588 PROC(LOCK
, lockargs
, res
),
589 PROC(CANCEL
, cancargs
, res
),
590 PROC(UNLOCK
, unlockargs
, res
),
591 PROC(GRANTED
, testargs
, res
),
592 PROC(TEST_MSG
, testargs
, norep
),
593 PROC(LOCK_MSG
, lockargs
, norep
),
594 PROC(CANCEL_MSG
, cancargs
, norep
),
595 PROC(UNLOCK_MSG
, unlockargs
, norep
),
596 PROC(GRANTED_MSG
, testargs
, norep
),
597 PROC(TEST_RES
, testres
, norep
),
598 PROC(LOCK_RES
, res
, norep
),
599 PROC(CANCEL_RES
, res
, norep
),
600 PROC(UNLOCK_RES
, res
, norep
),
601 PROC(GRANTED_RES
, res
, norep
),
604 static unsigned int nlm_version1_counts
[ARRAY_SIZE(nlm_procedures
)];
605 static const struct rpc_version nlm_version1
= {
607 .nrprocs
= ARRAY_SIZE(nlm_procedures
),
608 .procs
= nlm_procedures
,
609 .counts
= nlm_version1_counts
,
612 static unsigned int nlm_version3_counts
[ARRAY_SIZE(nlm_procedures
)];
613 static const struct rpc_version nlm_version3
= {
615 .nrprocs
= ARRAY_SIZE(nlm_procedures
),
616 .procs
= nlm_procedures
,
617 .counts
= nlm_version3_counts
,
620 static const struct rpc_version
*nlm_versions
[] = {
623 #ifdef CONFIG_LOCKD_V4
628 static struct rpc_stat nlm_rpc_stats
;
630 const struct rpc_program nlm_program
= {
632 .number
= NLM_PROGRAM
,
633 .nrvers
= ARRAY_SIZE(nlm_versions
),
634 .version
= nlm_versions
,
635 .stats
= &nlm_rpc_stats
,