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
)
130 ret
= xdr_stream_decode_opaque_inline(xdr
, (void *)&obj
->data
,
132 if (unlikely(ret
< 0))
141 static void encode_cookie(struct xdr_stream
*xdr
,
142 const struct nlm_cookie
*cookie
)
144 encode_netobj(xdr
, (u8
*)&cookie
->data
, cookie
->len
);
147 static int decode_cookie(struct xdr_stream
*xdr
,
148 struct nlm_cookie
*cookie
)
153 p
= xdr_inline_decode(xdr
, 4);
154 if (unlikely(p
== NULL
))
156 length
= be32_to_cpup(p
++);
157 /* apparently HPUX can return empty cookies */
160 if (length
> NLM_MAXCOOKIELEN
)
162 p
= xdr_inline_decode(xdr
, length
);
163 if (unlikely(p
== NULL
))
165 cookie
->len
= length
;
166 memcpy(cookie
->data
, p
, length
);
170 memset(cookie
->data
, 0, 4);
173 dprintk("NFS: returned cookie was too long: %u\n", length
);
176 print_overflow_msg(__func__
, xdr
);
183 static void encode_fh(struct xdr_stream
*xdr
, const struct nfs_fh
*fh
)
185 encode_netobj(xdr
, (u8
*)&fh
->data
, NFS2_FHSIZE
);
192 * LCK_DENIED_NOLOCKS = 2,
194 * LCK_DENIED_GRACE_PERIOD = 4
202 * NB: we don't swap bytes for the NLM status values. The upper
203 * layers deal directly with the status value in network byte
207 static void encode_nlm_stat(struct xdr_stream
*xdr
,
212 WARN_ON_ONCE(be32_to_cpu(stat
) > NLM_LCK_DENIED_GRACE_PERIOD
);
213 p
= xdr_reserve_space(xdr
, 4);
217 static int decode_nlm_stat(struct xdr_stream
*xdr
,
222 p
= xdr_inline_decode(xdr
, 4);
223 if (unlikely(p
== NULL
))
225 if (unlikely(ntohl(*p
) > ntohl(nlm_lck_denied_grace_period
)))
230 dprintk("%s: server returned invalid nlm_stats value: %u\n",
231 __func__
, be32_to_cpup(p
));
234 print_overflow_msg(__func__
, xdr
);
239 * struct nlm_holder {
247 static void encode_nlm_holder(struct xdr_stream
*xdr
,
248 const struct nlm_res
*result
)
250 const struct nlm_lock
*lock
= &result
->lock
;
254 encode_bool(xdr
, lock
->fl
.fl_type
== F_RDLCK
);
255 encode_int32(xdr
, lock
->svid
);
256 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
258 p
= xdr_reserve_space(xdr
, 4 + 4);
259 nlm_compute_offsets(lock
, &l_offset
, &l_len
);
260 *p
++ = cpu_to_be32(l_offset
);
261 *p
= cpu_to_be32(l_len
);
264 static int decode_nlm_holder(struct xdr_stream
*xdr
, struct nlm_res
*result
)
266 struct nlm_lock
*lock
= &result
->lock
;
267 struct file_lock
*fl
= &lock
->fl
;
268 u32 exclusive
, l_offset
, l_len
;
273 memset(lock
, 0, sizeof(*lock
));
276 p
= xdr_inline_decode(xdr
, 4 + 4);
277 if (unlikely(p
== NULL
))
279 exclusive
= be32_to_cpup(p
++);
280 lock
->svid
= be32_to_cpup(p
);
281 fl
->fl_pid
= (pid_t
)lock
->svid
;
283 error
= decode_netobj(xdr
, &lock
->oh
);
287 p
= xdr_inline_decode(xdr
, 4 + 4);
288 if (unlikely(p
== NULL
))
291 fl
->fl_flags
= FL_POSIX
;
292 fl
->fl_type
= exclusive
!= 0 ? F_WRLCK
: F_RDLCK
;
293 l_offset
= be32_to_cpup(p
++);
294 l_len
= be32_to_cpup(p
);
295 end
= l_offset
+ l_len
- 1;
297 fl
->fl_start
= (loff_t
)l_offset
;
298 if (l_len
== 0 || end
< 0)
299 fl
->fl_end
= OFFSET_MAX
;
301 fl
->fl_end
= (loff_t
)end
;
306 print_overflow_msg(__func__
, xdr
);
311 * string caller_name<LM_MAXSTRLEN>;
313 static void encode_caller_name(struct xdr_stream
*xdr
, const char *name
)
315 /* NB: client-side does not set lock->len */
316 u32 length
= strlen(name
);
319 p
= xdr_reserve_space(xdr
, 4 + length
);
320 xdr_encode_opaque(p
, name
, length
);
325 * string caller_name<LM_MAXSTRLEN>;
333 static void encode_nlm_lock(struct xdr_stream
*xdr
,
334 const struct nlm_lock
*lock
)
339 encode_caller_name(xdr
, lock
->caller
);
340 encode_fh(xdr
, &lock
->fh
);
341 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
343 p
= xdr_reserve_space(xdr
, 4 + 4 + 4);
344 *p
++ = cpu_to_be32(lock
->svid
);
346 nlm_compute_offsets(lock
, &l_offset
, &l_len
);
347 *p
++ = cpu_to_be32(l_offset
);
348 *p
= cpu_to_be32(l_len
);
353 * NLMv3 XDR encode functions
355 * NLMv3 argument types are defined in Chapter 10 of The Open Group's
356 * "Protocols for Interworking: XNFS, Version 3W".
360 * struct nlm_testargs {
363 * struct nlm_lock alock;
366 static void nlm_xdr_enc_testargs(struct rpc_rqst
*req
,
367 struct xdr_stream
*xdr
,
370 const struct nlm_args
*args
= data
;
371 const struct nlm_lock
*lock
= &args
->lock
;
373 encode_cookie(xdr
, &args
->cookie
);
374 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
375 encode_nlm_lock(xdr
, lock
);
379 * struct nlm_lockargs {
383 * struct nlm_lock alock;
388 static void nlm_xdr_enc_lockargs(struct rpc_rqst
*req
,
389 struct xdr_stream
*xdr
,
392 const struct nlm_args
*args
= data
;
393 const struct nlm_lock
*lock
= &args
->lock
;
395 encode_cookie(xdr
, &args
->cookie
);
396 encode_bool(xdr
, args
->block
);
397 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
398 encode_nlm_lock(xdr
, lock
);
399 encode_bool(xdr
, args
->reclaim
);
400 encode_int32(xdr
, args
->state
);
404 * struct nlm_cancargs {
408 * struct nlm_lock alock;
411 static void nlm_xdr_enc_cancargs(struct rpc_rqst
*req
,
412 struct xdr_stream
*xdr
,
415 const struct nlm_args
*args
= data
;
416 const struct nlm_lock
*lock
= &args
->lock
;
418 encode_cookie(xdr
, &args
->cookie
);
419 encode_bool(xdr
, args
->block
);
420 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
421 encode_nlm_lock(xdr
, lock
);
425 * struct nlm_unlockargs {
427 * struct nlm_lock alock;
430 static void nlm_xdr_enc_unlockargs(struct rpc_rqst
*req
,
431 struct xdr_stream
*xdr
,
434 const struct nlm_args
*args
= data
;
435 const struct nlm_lock
*lock
= &args
->lock
;
437 encode_cookie(xdr
, &args
->cookie
);
438 encode_nlm_lock(xdr
, lock
);
447 static void nlm_xdr_enc_res(struct rpc_rqst
*req
,
448 struct xdr_stream
*xdr
,
451 const struct nlm_res
*result
= data
;
453 encode_cookie(xdr
, &result
->cookie
);
454 encode_nlm_stat(xdr
, result
->status
);
458 * union nlm_testrply switch (nlm_stats stat) {
460 * struct nlm_holder holder;
465 * struct nlm_testres {
467 * nlm_testrply test_stat;
470 static void encode_nlm_testrply(struct xdr_stream
*xdr
,
471 const struct nlm_res
*result
)
473 if (result
->status
== nlm_lck_denied
)
474 encode_nlm_holder(xdr
, result
);
477 static void nlm_xdr_enc_testres(struct rpc_rqst
*req
,
478 struct xdr_stream
*xdr
,
481 const struct nlm_res
*result
= data
;
483 encode_cookie(xdr
, &result
->cookie
);
484 encode_nlm_stat(xdr
, result
->status
);
485 encode_nlm_testrply(xdr
, result
);
490 * NLMv3 XDR decode functions
492 * NLMv3 result types are defined in Chapter 10 of The Open Group's
493 * "Protocols for Interworking: XNFS, Version 3W".
497 * union nlm_testrply switch (nlm_stats stat) {
499 * struct nlm_holder holder;
504 * struct nlm_testres {
506 * nlm_testrply test_stat;
509 static int decode_nlm_testrply(struct xdr_stream
*xdr
,
510 struct nlm_res
*result
)
514 error
= decode_nlm_stat(xdr
, &result
->status
);
517 if (result
->status
== nlm_lck_denied
)
518 error
= decode_nlm_holder(xdr
, result
);
523 static int nlm_xdr_dec_testres(struct rpc_rqst
*req
,
524 struct xdr_stream
*xdr
,
527 struct nlm_res
*result
= data
;
530 error
= decode_cookie(xdr
, &result
->cookie
);
533 error
= decode_nlm_testrply(xdr
, result
);
544 static int nlm_xdr_dec_res(struct rpc_rqst
*req
,
545 struct xdr_stream
*xdr
,
548 struct nlm_res
*result
= data
;
551 error
= decode_cookie(xdr
, &result
->cookie
);
554 error
= decode_nlm_stat(xdr
, &result
->status
);
561 * For NLM, a void procedure really returns nothing
563 #define nlm_xdr_dec_norep NULL
565 #define PROC(proc, argtype, restype) \
566 [NLMPROC_##proc] = { \
567 .p_proc = NLMPROC_##proc, \
568 .p_encode = nlm_xdr_enc_##argtype, \
569 .p_decode = nlm_xdr_dec_##restype, \
570 .p_arglen = NLM_##argtype##_sz, \
571 .p_replen = NLM_##restype##_sz, \
572 .p_statidx = NLMPROC_##proc, \
576 static const struct rpc_procinfo nlm_procedures
[] = {
577 PROC(TEST
, testargs
, testres
),
578 PROC(LOCK
, lockargs
, res
),
579 PROC(CANCEL
, cancargs
, res
),
580 PROC(UNLOCK
, unlockargs
, res
),
581 PROC(GRANTED
, testargs
, res
),
582 PROC(TEST_MSG
, testargs
, norep
),
583 PROC(LOCK_MSG
, lockargs
, norep
),
584 PROC(CANCEL_MSG
, cancargs
, norep
),
585 PROC(UNLOCK_MSG
, unlockargs
, norep
),
586 PROC(GRANTED_MSG
, testargs
, norep
),
587 PROC(TEST_RES
, testres
, norep
),
588 PROC(LOCK_RES
, res
, norep
),
589 PROC(CANCEL_RES
, res
, norep
),
590 PROC(UNLOCK_RES
, res
, norep
),
591 PROC(GRANTED_RES
, res
, norep
),
594 static unsigned int nlm_version1_counts
[ARRAY_SIZE(nlm_procedures
)];
595 static const struct rpc_version nlm_version1
= {
597 .nrprocs
= ARRAY_SIZE(nlm_procedures
),
598 .procs
= nlm_procedures
,
599 .counts
= nlm_version1_counts
,
602 static unsigned int nlm_version3_counts
[ARRAY_SIZE(nlm_procedures
)];
603 static const struct rpc_version nlm_version3
= {
605 .nrprocs
= ARRAY_SIZE(nlm_procedures
),
606 .procs
= nlm_procedures
,
607 .counts
= nlm_version3_counts
,
610 static const struct rpc_version
*nlm_versions
[] = {
613 #ifdef CONFIG_LOCKD_V4
618 static struct rpc_stat nlm_rpc_stats
;
620 const struct rpc_program nlm_program
= {
622 .number
= NLM_PROGRAM
,
623 .nrvers
= ARRAY_SIZE(nlm_versions
),
624 .version
= nlm_versions
,
625 .stats
= &nlm_rpc_stats
,