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
)
133 ret
= xdr_stream_decode_opaque_inline(xdr
, (void *)&obj
->data
,
135 if (unlikely(ret
< 0))
144 static void encode_cookie(struct xdr_stream
*xdr
,
145 const struct nlm_cookie
*cookie
)
147 encode_netobj(xdr
, (u8
*)&cookie
->data
, cookie
->len
);
150 static int decode_cookie(struct xdr_stream
*xdr
,
151 struct nlm_cookie
*cookie
)
156 p
= xdr_inline_decode(xdr
, 4);
157 if (unlikely(p
== NULL
))
159 length
= be32_to_cpup(p
++);
160 /* apparently HPUX can return empty cookies */
163 if (length
> NLM_MAXCOOKIELEN
)
165 p
= xdr_inline_decode(xdr
, length
);
166 if (unlikely(p
== NULL
))
168 cookie
->len
= length
;
169 memcpy(cookie
->data
, p
, length
);
173 memset(cookie
->data
, 0, 4);
176 dprintk("NFS: returned cookie was too long: %u\n", length
);
179 print_overflow_msg(__func__
, xdr
);
186 static void encode_fh(struct xdr_stream
*xdr
, const struct nfs_fh
*fh
)
188 encode_netobj(xdr
, (u8
*)&fh
->data
, fh
->size
);
195 * NLM4_DENIED_NOLOCKS = 2,
197 * NLM4_DENIED_GRACE_PERIOD = 4,
209 * NB: we don't swap bytes for the NLM status values. The upper
210 * layers deal directly with the status value in network byte
213 static void encode_nlm4_stat(struct xdr_stream
*xdr
,
218 BUG_ON(be32_to_cpu(stat
) > NLM_FAILED
);
219 p
= xdr_reserve_space(xdr
, 4);
223 static int decode_nlm4_stat(struct xdr_stream
*xdr
, __be32
*stat
)
227 p
= xdr_inline_decode(xdr
, 4);
228 if (unlikely(p
== NULL
))
230 if (unlikely(ntohl(*p
) > ntohl(nlm4_failed
)))
235 dprintk("%s: server returned invalid nlm4_stats value: %u\n",
236 __func__
, be32_to_cpup(p
));
239 print_overflow_msg(__func__
, xdr
);
244 * struct nlm4_holder {
252 static void encode_nlm4_holder(struct xdr_stream
*xdr
,
253 const struct nlm_res
*result
)
255 const struct nlm_lock
*lock
= &result
->lock
;
259 encode_bool(xdr
, lock
->fl
.fl_type
== F_RDLCK
);
260 encode_int32(xdr
, lock
->svid
);
261 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
263 p
= xdr_reserve_space(xdr
, 4 + 4);
264 nlm4_compute_offsets(lock
, &l_offset
, &l_len
);
265 p
= xdr_encode_hyper(p
, l_offset
);
266 xdr_encode_hyper(p
, l_len
);
269 static int decode_nlm4_holder(struct xdr_stream
*xdr
, struct nlm_res
*result
)
271 struct nlm_lock
*lock
= &result
->lock
;
272 struct file_lock
*fl
= &lock
->fl
;
279 memset(lock
, 0, sizeof(*lock
));
282 p
= xdr_inline_decode(xdr
, 4 + 4);
283 if (unlikely(p
== NULL
))
285 exclusive
= be32_to_cpup(p
++);
286 lock
->svid
= be32_to_cpup(p
);
287 fl
->fl_pid
= (pid_t
)lock
->svid
;
289 error
= decode_netobj(xdr
, &lock
->oh
);
293 p
= xdr_inline_decode(xdr
, 8 + 8);
294 if (unlikely(p
== NULL
))
297 fl
->fl_flags
= FL_POSIX
;
298 fl
->fl_type
= exclusive
!= 0 ? F_WRLCK
: F_RDLCK
;
299 p
= xdr_decode_hyper(p
, &l_offset
);
300 xdr_decode_hyper(p
, &l_len
);
301 end
= l_offset
+ l_len
- 1;
303 fl
->fl_start
= (loff_t
)l_offset
;
304 if (l_len
== 0 || end
< 0)
305 fl
->fl_end
= OFFSET_MAX
;
307 fl
->fl_end
= (loff_t
)end
;
312 print_overflow_msg(__func__
, xdr
);
317 * string caller_name<LM_MAXSTRLEN>;
319 static void encode_caller_name(struct xdr_stream
*xdr
, const char *name
)
321 /* NB: client-side does not set lock->len */
322 u32 length
= strlen(name
);
325 p
= xdr_reserve_space(xdr
, 4 + length
);
326 xdr_encode_opaque(p
, name
, length
);
331 * string caller_name<LM_MAXSTRLEN>;
339 static void encode_nlm4_lock(struct xdr_stream
*xdr
,
340 const struct nlm_lock
*lock
)
345 encode_caller_name(xdr
, lock
->caller
);
346 encode_fh(xdr
, &lock
->fh
);
347 encode_netobj(xdr
, lock
->oh
.data
, lock
->oh
.len
);
349 p
= xdr_reserve_space(xdr
, 4 + 8 + 8);
350 *p
++ = cpu_to_be32(lock
->svid
);
352 nlm4_compute_offsets(lock
, &l_offset
, &l_len
);
353 p
= xdr_encode_hyper(p
, l_offset
);
354 xdr_encode_hyper(p
, l_len
);
359 * NLMv4 XDR encode functions
361 * NLMv4 argument types are defined in Appendix II of RFC 1813:
362 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
363 * "Protocols for Interworking: XNFS, Version 3W".
367 * struct nlm4_testargs {
370 * struct nlm4_lock alock;
373 static void nlm4_xdr_enc_testargs(struct rpc_rqst
*req
,
374 struct xdr_stream
*xdr
,
377 const struct nlm_args
*args
= data
;
378 const struct nlm_lock
*lock
= &args
->lock
;
380 encode_cookie(xdr
, &args
->cookie
);
381 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
382 encode_nlm4_lock(xdr
, lock
);
386 * struct nlm4_lockargs {
390 * struct nlm4_lock alock;
395 static void nlm4_xdr_enc_lockargs(struct rpc_rqst
*req
,
396 struct xdr_stream
*xdr
,
399 const struct nlm_args
*args
= data
;
400 const struct nlm_lock
*lock
= &args
->lock
;
402 encode_cookie(xdr
, &args
->cookie
);
403 encode_bool(xdr
, args
->block
);
404 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
405 encode_nlm4_lock(xdr
, lock
);
406 encode_bool(xdr
, args
->reclaim
);
407 encode_int32(xdr
, args
->state
);
411 * struct nlm4_cancargs {
415 * struct nlm4_lock alock;
418 static void nlm4_xdr_enc_cancargs(struct rpc_rqst
*req
,
419 struct xdr_stream
*xdr
,
422 const struct nlm_args
*args
= data
;
423 const struct nlm_lock
*lock
= &args
->lock
;
425 encode_cookie(xdr
, &args
->cookie
);
426 encode_bool(xdr
, args
->block
);
427 encode_bool(xdr
, lock
->fl
.fl_type
== F_WRLCK
);
428 encode_nlm4_lock(xdr
, lock
);
432 * struct nlm4_unlockargs {
434 * struct nlm4_lock alock;
437 static void nlm4_xdr_enc_unlockargs(struct rpc_rqst
*req
,
438 struct xdr_stream
*xdr
,
441 const struct nlm_args
*args
= data
;
442 const struct nlm_lock
*lock
= &args
->lock
;
444 encode_cookie(xdr
, &args
->cookie
);
445 encode_nlm4_lock(xdr
, lock
);
454 static void nlm4_xdr_enc_res(struct rpc_rqst
*req
,
455 struct xdr_stream
*xdr
,
458 const struct nlm_res
*result
= data
;
460 encode_cookie(xdr
, &result
->cookie
);
461 encode_nlm4_stat(xdr
, result
->status
);
465 * union nlm4_testrply switch (nlm4_stats stat) {
467 * struct nlm4_holder holder;
472 * struct nlm4_testres {
474 * nlm4_testrply test_stat;
477 static void nlm4_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_nlm4_stat(xdr
, result
->status
);
485 if (result
->status
== nlm_lck_denied
)
486 encode_nlm4_holder(xdr
, result
);
491 * NLMv4 XDR decode functions
493 * NLMv4 argument types are defined in Appendix II of RFC 1813:
494 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
495 * "Protocols for Interworking: XNFS, Version 3W".
499 * union nlm4_testrply switch (nlm4_stats stat) {
501 * struct nlm4_holder holder;
506 * struct nlm4_testres {
508 * nlm4_testrply test_stat;
511 static int decode_nlm4_testrply(struct xdr_stream
*xdr
,
512 struct nlm_res
*result
)
516 error
= decode_nlm4_stat(xdr
, &result
->status
);
519 if (result
->status
== nlm_lck_denied
)
520 error
= decode_nlm4_holder(xdr
, result
);
525 static int nlm4_xdr_dec_testres(struct rpc_rqst
*req
,
526 struct xdr_stream
*xdr
,
529 struct nlm_res
*result
= data
;
532 error
= decode_cookie(xdr
, &result
->cookie
);
535 error
= decode_nlm4_testrply(xdr
, result
);
546 static int nlm4_xdr_dec_res(struct rpc_rqst
*req
,
547 struct xdr_stream
*xdr
,
550 struct nlm_res
*result
= data
;
553 error
= decode_cookie(xdr
, &result
->cookie
);
556 error
= decode_nlm4_stat(xdr
, &result
->status
);
563 * For NLM, a void procedure really returns nothing
565 #define nlm4_xdr_dec_norep NULL
567 #define PROC(proc, argtype, restype) \
568 [NLMPROC_##proc] = { \
569 .p_proc = NLMPROC_##proc, \
570 .p_encode = nlm4_xdr_enc_##argtype, \
571 .p_decode = nlm4_xdr_dec_##restype, \
572 .p_arglen = NLM4_##argtype##_sz, \
573 .p_replen = NLM4_##restype##_sz, \
574 .p_statidx = NLMPROC_##proc, \
578 static const struct rpc_procinfo nlm4_procedures
[] = {
579 PROC(TEST
, testargs
, testres
),
580 PROC(LOCK
, lockargs
, res
),
581 PROC(CANCEL
, cancargs
, res
),
582 PROC(UNLOCK
, unlockargs
, res
),
583 PROC(GRANTED
, testargs
, res
),
584 PROC(TEST_MSG
, testargs
, norep
),
585 PROC(LOCK_MSG
, lockargs
, norep
),
586 PROC(CANCEL_MSG
, cancargs
, norep
),
587 PROC(UNLOCK_MSG
, unlockargs
, norep
),
588 PROC(GRANTED_MSG
, testargs
, norep
),
589 PROC(TEST_RES
, testres
, norep
),
590 PROC(LOCK_RES
, res
, norep
),
591 PROC(CANCEL_RES
, res
, norep
),
592 PROC(UNLOCK_RES
, res
, norep
),
593 PROC(GRANTED_RES
, res
, norep
),
596 static unsigned int nlm_version4_counts
[ARRAY_SIZE(nlm4_procedures
)];
597 const struct rpc_version nlm_version4
= {
599 .nrprocs
= ARRAY_SIZE(nlm4_procedures
),
600 .procs
= nlm4_procedures
,
601 .counts
= nlm_version4_counts
,