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 * Encode/decode NLMv4 basic data types
80 * Basic NLMv4 data types are defined in Appendix II, section 6.1.4
81 * of RFC 1813: "NFS Version 3 Protocol Specification" and in Chapter
82 * 10 of X/Open's "Protocols for Interworking: XNFS, Version 3W".
84 * Not all basic data types have their own encoding and decoding
85 * functions. For run-time efficiency, some data types are encoded
89 static void encode_bool(struct xdr_stream *xdr, const int value)
93 p = xdr_reserve_space(xdr, 4);
94 *p = value ? xdr_one : xdr_zero;
97 static void encode_int32(struct xdr_stream *xdr, const s32 value)
101 p = xdr_reserve_space(xdr, 4);
102 *p = cpu_to_be32(value);
106 * typedef opaque netobj<MAXNETOBJ_SZ>
108 static void encode_netobj(struct xdr_stream *xdr,
109 const u8 *data, const unsigned int length)
113 p = xdr_reserve_space(xdr, 4 + length);
114 xdr_encode_opaque(p, data, length);
117 static int decode_netobj(struct xdr_stream *xdr,
118 struct xdr_netobj *obj)
122 ret = xdr_stream_decode_opaque_inline(xdr, (void *)&obj->data,
124 if (unlikely(ret < 0))
133 static void encode_cookie(struct xdr_stream *xdr,
134 const struct nlm_cookie *cookie)
136 encode_netobj(xdr, (u8 *)&cookie->data, cookie->len);
139 static int decode_cookie(struct xdr_stream *xdr,
140 struct nlm_cookie *cookie)
145 p = xdr_inline_decode(xdr, 4);
146 if (unlikely(p == NULL))
148 length = be32_to_cpup(p++);
149 /* apparently HPUX can return empty cookies */
152 if (length > NLM_MAXCOOKIELEN)
154 p = xdr_inline_decode(xdr, length);
155 if (unlikely(p == NULL))
157 cookie->len = length;
158 memcpy(cookie->data, p, length);
162 memset(cookie->data, 0, 4);
165 dprintk("NFS: returned cookie was too long: %u\n", length);
174 static void encode_fh(struct xdr_stream *xdr, const struct nfs_fh *fh)
176 encode_netobj(xdr, (u8 *)&fh->data, fh->size);
183 * NLM4_DENIED_NOLOCKS = 2,
185 * NLM4_DENIED_GRACE_PERIOD = 4,
197 * NB: we don't swap bytes for the NLM status values. The upper
198 * layers deal directly with the status value in network byte
201 static void encode_nlm4_stat(struct xdr_stream *xdr,
206 BUG_ON(be32_to_cpu(stat) > NLM_FAILED);
207 p = xdr_reserve_space(xdr, 4);
211 static int decode_nlm4_stat(struct xdr_stream *xdr, __be32 *stat)
215 p = xdr_inline_decode(xdr, 4);
216 if (unlikely(p == NULL))
218 if (unlikely(ntohl(*p) > ntohl(nlm4_failed)))
223 dprintk("%s: server returned invalid nlm4_stats value: %u\n",
224 __func__, be32_to_cpup(p));
231 * struct nlm4_holder {
239 static void encode_nlm4_holder(struct xdr_stream *xdr,
240 const struct nlm_res *result)
242 const struct nlm_lock *lock = &result->lock;
246 encode_bool(xdr, lock->fl.fl_type == F_RDLCK);
247 encode_int32(xdr, lock->svid);
248 encode_netobj(xdr, lock->oh.data, lock->oh.len);
250 p = xdr_reserve_space(xdr, 4 + 4);
251 nlm4_compute_offsets(lock, &l_offset, &l_len);
252 p = xdr_encode_hyper(p, l_offset);
253 xdr_encode_hyper(p, l_len);
256 static int decode_nlm4_holder(struct xdr_stream *xdr, struct nlm_res *result)
258 struct nlm_lock *lock = &result->lock;
259 struct file_lock *fl = &lock->fl;
266 memset(lock, 0, sizeof(*lock));
269 p = xdr_inline_decode(xdr, 4 + 4);
270 if (unlikely(p == NULL))
272 exclusive = be32_to_cpup(p++);
273 lock->svid = be32_to_cpup(p);
274 fl->fl_pid = (pid_t)lock->svid;
276 error = decode_netobj(xdr, &lock->oh);
280 p = xdr_inline_decode(xdr, 8 + 8);
281 if (unlikely(p == NULL))
284 fl->fl_flags = FL_POSIX;
285 fl->fl_type = exclusive != 0 ? F_WRLCK : F_RDLCK;
286 p = xdr_decode_hyper(p, &l_offset);
287 xdr_decode_hyper(p, &l_len);
288 end = l_offset + l_len - 1;
290 fl->fl_start = (loff_t)l_offset;
291 if (l_len == 0 || end < 0)
292 fl->fl_end = OFFSET_MAX;
294 fl->fl_end = (loff_t)end;
303 * string caller_name<LM_MAXSTRLEN>;
305 static void encode_caller_name(struct xdr_stream *xdr, const char *name)
307 /* NB: client-side does not set lock->len */
308 u32 length = strlen(name);
311 p = xdr_reserve_space(xdr, 4 + length);
312 xdr_encode_opaque(p, name, length);
317 * string caller_name<LM_MAXSTRLEN>;
325 static void encode_nlm4_lock(struct xdr_stream *xdr,
326 const struct nlm_lock *lock)
331 encode_caller_name(xdr, lock->caller);
332 encode_fh(xdr, &lock->fh);
333 encode_netobj(xdr, lock->oh.data, lock->oh.len);
335 p = xdr_reserve_space(xdr, 4 + 8 + 8);
336 *p++ = cpu_to_be32(lock->svid);
338 nlm4_compute_offsets(lock, &l_offset, &l_len);
339 p = xdr_encode_hyper(p, l_offset);
340 xdr_encode_hyper(p, l_len);
345 * NLMv4 XDR encode functions
347 * NLMv4 argument types are defined in Appendix II of RFC 1813:
348 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
349 * "Protocols for Interworking: XNFS, Version 3W".
353 * struct nlm4_testargs {
356 * struct nlm4_lock alock;
359 static void nlm4_xdr_enc_testargs(struct rpc_rqst *req,
360 struct xdr_stream *xdr,
363 const struct nlm_args *args = data;
364 const struct nlm_lock *lock = &args->lock;
366 encode_cookie(xdr, &args->cookie);
367 encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
368 encode_nlm4_lock(xdr, lock);
372 * struct nlm4_lockargs {
376 * struct nlm4_lock alock;
381 static void nlm4_xdr_enc_lockargs(struct rpc_rqst *req,
382 struct xdr_stream *xdr,
385 const struct nlm_args *args = data;
386 const struct nlm_lock *lock = &args->lock;
388 encode_cookie(xdr, &args->cookie);
389 encode_bool(xdr, args->block);
390 encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
391 encode_nlm4_lock(xdr, lock);
392 encode_bool(xdr, args->reclaim);
393 encode_int32(xdr, args->state);
397 * struct nlm4_cancargs {
401 * struct nlm4_lock alock;
404 static void nlm4_xdr_enc_cancargs(struct rpc_rqst *req,
405 struct xdr_stream *xdr,
408 const struct nlm_args *args = data;
409 const struct nlm_lock *lock = &args->lock;
411 encode_cookie(xdr, &args->cookie);
412 encode_bool(xdr, args->block);
413 encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
414 encode_nlm4_lock(xdr, lock);
418 * struct nlm4_unlockargs {
420 * struct nlm4_lock alock;
423 static void nlm4_xdr_enc_unlockargs(struct rpc_rqst *req,
424 struct xdr_stream *xdr,
427 const struct nlm_args *args = data;
428 const struct nlm_lock *lock = &args->lock;
430 encode_cookie(xdr, &args->cookie);
431 encode_nlm4_lock(xdr, lock);
440 static void nlm4_xdr_enc_res(struct rpc_rqst *req,
441 struct xdr_stream *xdr,
444 const struct nlm_res *result = data;
446 encode_cookie(xdr, &result->cookie);
447 encode_nlm4_stat(xdr, result->status);
451 * union nlm4_testrply switch (nlm4_stats stat) {
453 * struct nlm4_holder holder;
458 * struct nlm4_testres {
460 * nlm4_testrply test_stat;
463 static void nlm4_xdr_enc_testres(struct rpc_rqst *req,
464 struct xdr_stream *xdr,
467 const struct nlm_res *result = data;
469 encode_cookie(xdr, &result->cookie);
470 encode_nlm4_stat(xdr, result->status);
471 if (result->status == nlm_lck_denied)
472 encode_nlm4_holder(xdr, result);
477 * NLMv4 XDR decode functions
479 * NLMv4 argument types are defined in Appendix II of RFC 1813:
480 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
481 * "Protocols for Interworking: XNFS, Version 3W".
485 * union nlm4_testrply switch (nlm4_stats stat) {
487 * struct nlm4_holder holder;
492 * struct nlm4_testres {
494 * nlm4_testrply test_stat;
497 static int decode_nlm4_testrply(struct xdr_stream *xdr,
498 struct nlm_res *result)
502 error = decode_nlm4_stat(xdr, &result->status);
505 if (result->status == nlm_lck_denied)
506 error = decode_nlm4_holder(xdr, result);
511 static int nlm4_xdr_dec_testres(struct rpc_rqst *req,
512 struct xdr_stream *xdr,
515 struct nlm_res *result = data;
518 error = decode_cookie(xdr, &result->cookie);
521 error = decode_nlm4_testrply(xdr, result);
532 static int nlm4_xdr_dec_res(struct rpc_rqst *req,
533 struct xdr_stream *xdr,
536 struct nlm_res *result = data;
539 error = decode_cookie(xdr, &result->cookie);
542 error = decode_nlm4_stat(xdr, &result->status);
549 * For NLM, a void procedure really returns nothing
551 #define nlm4_xdr_dec_norep NULL
553 #define PROC(proc, argtype, restype) \
554 [NLMPROC_##proc] = { \
555 .p_proc = NLMPROC_##proc, \
556 .p_encode = nlm4_xdr_enc_##argtype, \
557 .p_decode = nlm4_xdr_dec_##restype, \
558 .p_arglen = NLM4_##argtype##_sz, \
559 .p_replen = NLM4_##restype##_sz, \
560 .p_statidx = NLMPROC_##proc, \
564 static const struct rpc_procinfo nlm4_procedures[] = {
565 PROC(TEST, testargs, testres),
566 PROC(LOCK, lockargs, res),
567 PROC(CANCEL, cancargs, res),
568 PROC(UNLOCK, unlockargs, res),
569 PROC(GRANTED, testargs, res),
570 PROC(TEST_MSG, testargs, norep),
571 PROC(LOCK_MSG, lockargs, norep),
572 PROC(CANCEL_MSG, cancargs, norep),
573 PROC(UNLOCK_MSG, unlockargs, norep),
574 PROC(GRANTED_MSG, testargs, norep),
575 PROC(TEST_RES, testres, norep),
576 PROC(LOCK_RES, res, norep),
577 PROC(CANCEL_RES, res, norep),
578 PROC(UNLOCK_RES, res, norep),
579 PROC(GRANTED_RES, res, norep),
582 static unsigned int nlm_version4_counts[ARRAY_SIZE(nlm4_procedures)];
583 const struct rpc_version nlm_version4 = {
585 .nrprocs = ARRAY_SIZE(nlm4_procedures),
586 .procs = nlm4_procedures,
587 .counts = nlm_version4_counts,