1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/lockd/clntxdr.c
5 * XDR functions to encode/decode NLM version 1 and 3 RPC
6 * arguments and results. NLM version 2 is not specified
7 * by a standard, thus it is not implemented.
9 * NLM client-side only.
11 * Copyright (C) 2010, Oracle. All rights reserved.
14 #include <linux/types.h>
15 #include <linux/sunrpc/xdr.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/stats.h>
18 #include <linux/lockd/lockd.h>
20 #include <uapi/linux/nfs2.h>
22 #define NLMDBG_FACILITY NLMDBG_XDR
24 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
25 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
29 * Declare the space requirements for NLM arguments and replies as
30 * number of 32bit-words
32 #define NLM_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
33 #define NLM_caller_sz (1+(NLMCLNT_OHSIZE>>2))
34 #define NLM_owner_sz (1+(NLMCLNT_OHSIZE>>2))
35 #define NLM_fhandle_sz (1+(NFS2_FHSIZE>>2))
36 #define NLM_lock_sz (3+NLM_caller_sz+NLM_owner_sz+NLM_fhandle_sz)
37 #define NLM_holder_sz (4+NLM_owner_sz)
39 #define NLM_testargs_sz (NLM_cookie_sz+1+NLM_lock_sz)
40 #define NLM_lockargs_sz (NLM_cookie_sz+4+NLM_lock_sz)
41 #define NLM_cancargs_sz (NLM_cookie_sz+2+NLM_lock_sz)
42 #define NLM_unlockargs_sz (NLM_cookie_sz+NLM_lock_sz)
44 #define NLM_testres_sz (NLM_cookie_sz+1+NLM_holder_sz)
45 #define NLM_res_sz (NLM_cookie_sz+1)
46 #define NLM_norep_sz (0)
49 static s32
loff_t_to_s32(loff_t offset
)
53 if (offset
>= NLM_OFFSET_MAX
)
55 else if (offset
<= -NLM_OFFSET_MAX
)
56 res
= -NLM_OFFSET_MAX
;
62 static void nlm_compute_offsets(const struct nlm_lock
*lock
,
63 u32
*l_offset
, u32
*l_len
)
65 const struct file_lock
*fl
= &lock
->fl
;
67 *l_offset
= loff_t_to_s32(fl
->fl_start
);
68 if (fl
->fl_end
== OFFSET_MAX
)
71 *l_len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
75 * Encode/decode NLMv3 basic data types
77 * Basic NLMv3 data types are not defined in an IETF standards
78 * document. X/Open has a description of these data types that
79 * is useful. See Chapter 10 of "Protocols for Interworking:
82 * Not all basic data types have their own encoding and decoding
83 * functions. For run-time efficiency, some data types are encoded
87 static void encode_bool(struct xdr_stream
*xdr
, const int value
)
91 p
= xdr_reserve_space(xdr
, 4);
92 *p
= value
? xdr_one
: xdr_zero
;
95 static void encode_int32(struct xdr_stream
*xdr
, const s32 value
)
99 p
= xdr_reserve_space(xdr
, 4);
100 *p
= cpu_to_be32(value
);
104 * typedef opaque netobj<MAXNETOBJ_SZ>
106 static void encode_netobj(struct xdr_stream
*xdr
,
107 const u8
*data
, const unsigned int length
)
111 p
= xdr_reserve_space(xdr
, 4 + length
);
112 xdr_encode_opaque(p
, data
, length
);
115 static int decode_netobj(struct xdr_stream
*xdr
,
116 struct xdr_netobj
*obj
)
120 ret
= xdr_stream_decode_opaque_inline(xdr
, (void *)&obj
->data
,
122 if (unlikely(ret
< 0))
131 static void encode_cookie(struct xdr_stream
*xdr
,
132 const struct nlm_cookie
*cookie
)
134 encode_netobj(xdr
, (u8
*)&cookie
->data
, cookie
->len
);
137 static int decode_cookie(struct xdr_stream
*xdr
,
138 struct nlm_cookie
*cookie
)
143 p
= xdr_inline_decode(xdr
, 4);
144 if (unlikely(p
== NULL
))
146 length
= be32_to_cpup(p
++);
147 /* apparently HPUX can return empty cookies */
150 if (length
> NLM_MAXCOOKIELEN
)
152 p
= xdr_inline_decode(xdr
, length
);
153 if (unlikely(p
== NULL
))
155 cookie
->len
= length
;
156 memcpy(cookie
->data
, p
, length
);
160 memset(cookie
->data
, 0, 4);
163 dprintk("NFS: returned cookie was too long: %u\n", length
);
172 static void encode_fh(struct xdr_stream
*xdr
, const struct nfs_fh
*fh
)
174 encode_netobj(xdr
, (u8
*)&fh
->data
, NFS2_FHSIZE
);
181 * LCK_DENIED_NOLOCKS = 2,
183 * LCK_DENIED_GRACE_PERIOD = 4
191 * NB: we don't swap bytes for the NLM status values. The upper
192 * layers deal directly with the status value in network byte
196 static void encode_nlm_stat(struct xdr_stream
*xdr
,
201 WARN_ON_ONCE(be32_to_cpu(stat
) > NLM_LCK_DENIED_GRACE_PERIOD
);
202 p
= xdr_reserve_space(xdr
, 4);
206 static int decode_nlm_stat(struct xdr_stream
*xdr
,
211 p
= xdr_inline_decode(xdr
, 4);
212 if (unlikely(p
== NULL
))
214 if (unlikely(ntohl(*p
) > ntohl(nlm_lck_denied_grace_period
)))
219 dprintk("%s: server returned invalid nlm_stats value: %u\n",
220 __func__
, be32_to_cpup(p
));
227 * struct nlm_holder {
235 static void encode_nlm_holder(struct xdr_stream
*xdr
,
236 const struct nlm_res
*result
)
238 const struct nlm_lock
*lock
= &result
->lock
;
242 encode_bool(xdr
, lock
->fl
.c
.flc_type
== F_RDLCK
);
243 encode_int32(xdr
, lock
->svid
);
244 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
246 p
= xdr_reserve_space(xdr
, 4 + 4);
247 nlm_compute_offsets(lock
, &l_offset
, &l_len
);
248 *p
++ = cpu_to_be32(l_offset
);
249 *p
= cpu_to_be32(l_len
);
252 static int decode_nlm_holder(struct xdr_stream
*xdr
, struct nlm_res
*result
)
254 struct nlm_lock
*lock
= &result
->lock
;
255 struct file_lock
*fl
= &lock
->fl
;
256 u32 exclusive
, l_offset
, l_len
;
261 memset(lock
, 0, sizeof(*lock
));
264 p
= xdr_inline_decode(xdr
, 4 + 4);
265 if (unlikely(p
== NULL
))
267 exclusive
= be32_to_cpup(p
++);
268 lock
->svid
= be32_to_cpup(p
);
269 fl
->c
.flc_pid
= (pid_t
)lock
->svid
;
271 error
= decode_netobj(xdr
, &lock
->oh
);
275 p
= xdr_inline_decode(xdr
, 4 + 4);
276 if (unlikely(p
== NULL
))
279 fl
->c
.flc_flags
= FL_POSIX
;
280 fl
->c
.flc_type
= exclusive
!= 0 ? F_WRLCK
: F_RDLCK
;
281 l_offset
= be32_to_cpup(p
++);
282 l_len
= be32_to_cpup(p
);
283 end
= l_offset
+ l_len
- 1;
285 fl
->fl_start
= (loff_t
)l_offset
;
286 if (l_len
== 0 || end
< 0)
287 fl
->fl_end
= OFFSET_MAX
;
289 fl
->fl_end
= (loff_t
)end
;
298 * string caller_name<LM_MAXSTRLEN>;
300 static void encode_caller_name(struct xdr_stream
*xdr
, const char *name
)
302 /* NB: client-side does not set lock->len */
303 u32 length
= strlen(name
);
306 p
= xdr_reserve_space(xdr
, 4 + length
);
307 xdr_encode_opaque(p
, name
, length
);
312 * string caller_name<LM_MAXSTRLEN>;
320 static void encode_nlm_lock(struct xdr_stream
*xdr
,
321 const struct nlm_lock
*lock
)
326 encode_caller_name(xdr
, lock
->caller
);
327 encode_fh(xdr
, &lock
->fh
);
328 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
330 p
= xdr_reserve_space(xdr
, 4 + 4 + 4);
331 *p
++ = cpu_to_be32(lock
->svid
);
333 nlm_compute_offsets(lock
, &l_offset
, &l_len
);
334 *p
++ = cpu_to_be32(l_offset
);
335 *p
= cpu_to_be32(l_len
);
340 * NLMv3 XDR encode functions
342 * NLMv3 argument types are defined in Chapter 10 of The Open Group's
343 * "Protocols for Interworking: XNFS, Version 3W".
347 * struct nlm_testargs {
350 * struct nlm_lock alock;
353 static void nlm_xdr_enc_testargs(struct rpc_rqst
*req
,
354 struct xdr_stream
*xdr
,
357 const struct nlm_args
*args
= data
;
358 const struct nlm_lock
*lock
= &args
->lock
;
360 encode_cookie(xdr
, &args
->cookie
);
361 encode_bool(xdr
, lock
->fl
.c
.flc_type
== F_WRLCK
);
362 encode_nlm_lock(xdr
, lock
);
366 * struct nlm_lockargs {
370 * struct nlm_lock alock;
375 static void nlm_xdr_enc_lockargs(struct rpc_rqst
*req
,
376 struct xdr_stream
*xdr
,
379 const struct nlm_args
*args
= data
;
380 const struct nlm_lock
*lock
= &args
->lock
;
382 encode_cookie(xdr
, &args
->cookie
);
383 encode_bool(xdr
, args
->block
);
384 encode_bool(xdr
, lock
->fl
.c
.flc_type
== F_WRLCK
);
385 encode_nlm_lock(xdr
, lock
);
386 encode_bool(xdr
, args
->reclaim
);
387 encode_int32(xdr
, args
->state
);
391 * struct nlm_cancargs {
395 * struct nlm_lock alock;
398 static void nlm_xdr_enc_cancargs(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
.c
.flc_type
== F_WRLCK
);
408 encode_nlm_lock(xdr
, lock
);
412 * struct nlm_unlockargs {
414 * struct nlm_lock alock;
417 static void nlm_xdr_enc_unlockargs(struct rpc_rqst
*req
,
418 struct xdr_stream
*xdr
,
421 const struct nlm_args
*args
= data
;
422 const struct nlm_lock
*lock
= &args
->lock
;
424 encode_cookie(xdr
, &args
->cookie
);
425 encode_nlm_lock(xdr
, lock
);
434 static void nlm_xdr_enc_res(struct rpc_rqst
*req
,
435 struct xdr_stream
*xdr
,
438 const struct nlm_res
*result
= data
;
440 encode_cookie(xdr
, &result
->cookie
);
441 encode_nlm_stat(xdr
, result
->status
);
445 * union nlm_testrply switch (nlm_stats stat) {
447 * struct nlm_holder holder;
452 * struct nlm_testres {
454 * nlm_testrply test_stat;
457 static void encode_nlm_testrply(struct xdr_stream
*xdr
,
458 const struct nlm_res
*result
)
460 if (result
->status
== nlm_lck_denied
)
461 encode_nlm_holder(xdr
, result
);
464 static void nlm_xdr_enc_testres(struct rpc_rqst
*req
,
465 struct xdr_stream
*xdr
,
468 const struct nlm_res
*result
= data
;
470 encode_cookie(xdr
, &result
->cookie
);
471 encode_nlm_stat(xdr
, result
->status
);
472 encode_nlm_testrply(xdr
, result
);
477 * NLMv3 XDR decode functions
479 * NLMv3 result types are defined in Chapter 10 of The Open Group's
480 * "Protocols for Interworking: XNFS, Version 3W".
484 * union nlm_testrply switch (nlm_stats stat) {
486 * struct nlm_holder holder;
491 * struct nlm_testres {
493 * nlm_testrply test_stat;
496 static int decode_nlm_testrply(struct xdr_stream
*xdr
,
497 struct nlm_res
*result
)
501 error
= decode_nlm_stat(xdr
, &result
->status
);
504 if (result
->status
== nlm_lck_denied
)
505 error
= decode_nlm_holder(xdr
, result
);
510 static int nlm_xdr_dec_testres(struct rpc_rqst
*req
,
511 struct xdr_stream
*xdr
,
514 struct nlm_res
*result
= data
;
517 error
= decode_cookie(xdr
, &result
->cookie
);
520 error
= decode_nlm_testrply(xdr
, result
);
531 static int nlm_xdr_dec_res(struct rpc_rqst
*req
,
532 struct xdr_stream
*xdr
,
535 struct nlm_res
*result
= data
;
538 error
= decode_cookie(xdr
, &result
->cookie
);
541 error
= decode_nlm_stat(xdr
, &result
->status
);
548 * For NLM, a void procedure really returns nothing
550 #define nlm_xdr_dec_norep NULL
552 #define PROC(proc, argtype, restype) \
553 [NLMPROC_##proc] = { \
554 .p_proc = NLMPROC_##proc, \
555 .p_encode = nlm_xdr_enc_##argtype, \
556 .p_decode = nlm_xdr_dec_##restype, \
557 .p_arglen = NLM_##argtype##_sz, \
558 .p_replen = NLM_##restype##_sz, \
559 .p_statidx = NLMPROC_##proc, \
563 static const struct rpc_procinfo nlm_procedures
[] = {
564 PROC(TEST
, testargs
, testres
),
565 PROC(LOCK
, lockargs
, res
),
566 PROC(CANCEL
, cancargs
, res
),
567 PROC(UNLOCK
, unlockargs
, res
),
568 PROC(GRANTED
, testargs
, res
),
569 PROC(TEST_MSG
, testargs
, norep
),
570 PROC(LOCK_MSG
, lockargs
, norep
),
571 PROC(CANCEL_MSG
, cancargs
, norep
),
572 PROC(UNLOCK_MSG
, unlockargs
, norep
),
573 PROC(GRANTED_MSG
, testargs
, norep
),
574 PROC(TEST_RES
, testres
, norep
),
575 PROC(LOCK_RES
, res
, norep
),
576 PROC(CANCEL_RES
, res
, norep
),
577 PROC(UNLOCK_RES
, res
, norep
),
578 PROC(GRANTED_RES
, res
, norep
),
581 static unsigned int nlm_version1_counts
[ARRAY_SIZE(nlm_procedures
)];
582 static const struct rpc_version nlm_version1
= {
584 .nrprocs
= ARRAY_SIZE(nlm_procedures
),
585 .procs
= nlm_procedures
,
586 .counts
= nlm_version1_counts
,
589 static unsigned int nlm_version3_counts
[ARRAY_SIZE(nlm_procedures
)];
590 static const struct rpc_version nlm_version3
= {
592 .nrprocs
= ARRAY_SIZE(nlm_procedures
),
593 .procs
= nlm_procedures
,
594 .counts
= nlm_version3_counts
,
597 static const struct rpc_version
*nlm_versions
[] = {
600 #ifdef CONFIG_LOCKD_V4
605 static struct rpc_stat nlm_rpc_stats
;
607 const struct rpc_program nlm_program
= {
609 .number
= NLM_PROGRAM
,
610 .nrvers
= ARRAY_SIZE(nlm_versions
),
611 .version
= nlm_versions
,
612 .stats
= &nlm_rpc_stats
,