2 * linux/fs/lockd/clntxdr.c
4 * XDR functions to encode/decode NLM version 3 RPC arguments and results.
5 * NLM version 3 is backwards compatible with NLM versions 1 and 2.
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 #define NLMDBG_FACILITY NLMDBG_XDR
20 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
21 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
25 * Declare the space requirements for NLM arguments and replies as
26 * number of 32bit-words
28 #define NLM_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
29 #define NLM_caller_sz (1+(NLMCLNT_OHSIZE>>2))
30 #define NLM_owner_sz (1+(NLMCLNT_OHSIZE>>2))
31 #define NLM_fhandle_sz (1+(NFS2_FHSIZE>>2))
32 #define NLM_lock_sz (3+NLM_caller_sz+NLM_owner_sz+NLM_fhandle_sz)
33 #define NLM_holder_sz (4+NLM_owner_sz)
35 #define NLM_testargs_sz (NLM_cookie_sz+1+NLM_lock_sz)
36 #define NLM_lockargs_sz (NLM_cookie_sz+4+NLM_lock_sz)
37 #define NLM_cancargs_sz (NLM_cookie_sz+2+NLM_lock_sz)
38 #define NLM_unlockargs_sz (NLM_cookie_sz+NLM_lock_sz)
40 #define NLM_testres_sz (NLM_cookie_sz+1+NLM_holder_sz)
41 #define NLM_res_sz (NLM_cookie_sz+1)
42 #define NLM_norep_sz (0)
45 static s32 loff_t_to_s32(loff_t offset)
49 if (offset >= NLM_OFFSET_MAX)
51 else if (offset <= -NLM_OFFSET_MAX)
52 res = -NLM_OFFSET_MAX;
58 static void nlm_compute_offsets(const struct nlm_lock *lock,
59 u32 *l_offset, u32 *l_len)
61 const struct file_lock *fl = &lock->fl;
63 BUG_ON(fl->fl_start > NLM_OFFSET_MAX);
64 BUG_ON(fl->fl_end > NLM_OFFSET_MAX &&
65 fl->fl_end != OFFSET_MAX);
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 * Handle decode buffer overflows out-of-line.
77 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
79 dprintk("lockd: %s prematurely hit the end of our receive buffer. "
80 "Remaining buffer length is %tu words.\n",
81 func, xdr->end - xdr->p);
86 * Encode/decode NLMv3 basic data types
88 * Basic NLMv3 data types are not defined in an IETF standards
89 * document. X/Open has a description of these data types that
90 * is useful. See Chapter 10 of "Protocols for Interworking:
93 * Not all basic data types have their own encoding and decoding
94 * functions. For run-time efficiency, some data types are encoded
98 static void encode_bool(struct xdr_stream *xdr, const int value)
102 p = xdr_reserve_space(xdr, 4);
103 *p = value ? xdr_one : xdr_zero;
106 static void encode_int32(struct xdr_stream *xdr, const s32 value)
110 p = xdr_reserve_space(xdr, 4);
111 *p = cpu_to_be32(value);
115 * typedef opaque netobj<MAXNETOBJ_SZ>
117 static void encode_netobj(struct xdr_stream *xdr,
118 const u8 *data, const unsigned int length)
122 BUG_ON(length > XDR_MAX_NETOBJ);
123 p = xdr_reserve_space(xdr, 4 + length);
124 xdr_encode_opaque(p, data, length);
127 static int decode_netobj(struct xdr_stream *xdr,
128 struct xdr_netobj *obj)
133 p = xdr_inline_decode(xdr, 4);
134 if (unlikely(p == NULL))
136 length = be32_to_cpup(p++);
137 if (unlikely(length > XDR_MAX_NETOBJ))
143 dprintk("NFS: returned netobj was too long: %u\n", length);
146 print_overflow_msg(__func__, xdr);
153 static void encode_cookie(struct xdr_stream *xdr,
154 const struct nlm_cookie *cookie)
156 BUG_ON(cookie->len > NLM_MAXCOOKIELEN);
157 encode_netobj(xdr, (u8 *)&cookie->data, cookie->len);
160 static int decode_cookie(struct xdr_stream *xdr,
161 struct nlm_cookie *cookie)
166 p = xdr_inline_decode(xdr, 4);
167 if (unlikely(p == NULL))
169 length = be32_to_cpup(p++);
170 /* apparently HPUX can return empty cookies */
173 if (length > NLM_MAXCOOKIELEN)
175 p = xdr_inline_decode(xdr, length);
176 if (unlikely(p == NULL))
178 cookie->len = length;
179 memcpy(cookie->data, p, length);
183 memset(cookie->data, 0, 4);
186 dprintk("NFS: returned cookie was too long: %u\n", length);
189 print_overflow_msg(__func__, xdr);
196 static void encode_fh(struct xdr_stream *xdr, const struct nfs_fh *fh)
198 BUG_ON(fh->size != NFS2_FHSIZE);
199 encode_netobj(xdr, (u8 *)&fh->data, NFS2_FHSIZE);
206 * LCK_DENIED_NOLOCKS = 2,
208 * LCK_DENIED_GRACE_PERIOD = 4
216 * NB: we don't swap bytes for the NLM status values. The upper
217 * layers deal directly with the status value in network byte
221 static void encode_nlm_stat(struct xdr_stream *xdr,
226 BUG_ON(be32_to_cpu(stat) > NLM_LCK_DENIED_GRACE_PERIOD);
227 p = xdr_reserve_space(xdr, 4);
231 static int decode_nlm_stat(struct xdr_stream *xdr,
236 p = xdr_inline_decode(xdr, 4);
237 if (unlikely(p == NULL))
239 if (unlikely(*p > nlm_lck_denied_grace_period))
244 dprintk("%s: server returned invalid nlm_stats value: %u\n",
245 __func__, be32_to_cpup(p));
248 print_overflow_msg(__func__, xdr);
253 * struct nlm_holder {
261 static void encode_nlm_holder(struct xdr_stream *xdr,
262 const struct nlm_res *result)
264 const struct nlm_lock *lock = &result->lock;
268 encode_bool(xdr, lock->fl.fl_type == F_RDLCK);
269 encode_int32(xdr, lock->svid);
270 encode_netobj(xdr, lock->oh.data, lock->oh.len);
272 p = xdr_reserve_space(xdr, 4 + 4);
273 nlm_compute_offsets(lock, &l_offset, &l_len);
274 *p++ = cpu_to_be32(l_offset);
275 *p = cpu_to_be32(l_len);
278 static int decode_nlm_holder(struct xdr_stream *xdr, struct nlm_res *result)
280 struct nlm_lock *lock = &result->lock;
281 struct file_lock *fl = &lock->fl;
282 u32 exclusive, l_offset, l_len;
287 memset(lock, 0, sizeof(*lock));
290 p = xdr_inline_decode(xdr, 4 + 4);
291 if (unlikely(p == NULL))
293 exclusive = be32_to_cpup(p++);
294 lock->svid = be32_to_cpup(p);
295 fl->fl_pid = (pid_t)lock->svid;
297 error = decode_netobj(xdr, &lock->oh);
301 p = xdr_inline_decode(xdr, 4 + 4);
302 if (unlikely(p == NULL))
305 fl->fl_flags = FL_POSIX;
306 fl->fl_type = exclusive != 0 ? F_WRLCK : F_RDLCK;
307 l_offset = be32_to_cpup(p++);
308 l_len = be32_to_cpup(p);
309 end = l_offset + l_len - 1;
311 fl->fl_start = (loff_t)l_offset;
312 if (l_len == 0 || end < 0)
313 fl->fl_end = OFFSET_MAX;
315 fl->fl_end = (loff_t)end;
320 print_overflow_msg(__func__, xdr);
325 * string caller_name<LM_MAXSTRLEN>;
327 static void encode_caller_name(struct xdr_stream *xdr, const char *name)
329 /* NB: client-side does not set lock->len */
330 u32 length = strlen(name);
333 BUG_ON(length > NLM_MAXSTRLEN);
334 p = xdr_reserve_space(xdr, 4 + length);
335 xdr_encode_opaque(p, name, length);
340 * string caller_name<LM_MAXSTRLEN>;
348 static void encode_nlm_lock(struct xdr_stream *xdr,
349 const struct nlm_lock *lock)
354 encode_caller_name(xdr, lock->caller);
355 encode_fh(xdr, &lock->fh);
356 encode_netobj(xdr, lock->oh.data, lock->oh.len);
358 p = xdr_reserve_space(xdr, 4 + 4 + 4);
359 *p++ = cpu_to_be32(lock->svid);
361 nlm_compute_offsets(lock, &l_offset, &l_len);
362 *p++ = cpu_to_be32(l_offset);
363 *p = cpu_to_be32(l_len);
368 * NLMv3 XDR encode functions
370 * NLMv3 argument types are defined in Chapter 10 of The Open Group's
371 * "Protocols for Interworking: XNFS, Version 3W".
375 * struct nlm_testargs {
378 * struct nlm_lock alock;
381 static int nlm_xdr_enc_testargs(struct rpc_rqst *req, __be32 *p,
382 const struct nlm_args *args)
384 const struct nlm_lock *lock = &args->lock;
385 struct xdr_stream xdr;
387 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
388 encode_cookie(&xdr, &args->cookie);
389 encode_bool(&xdr, lock->fl.fl_type == F_WRLCK);
390 encode_nlm_lock(&xdr, lock);
395 * struct nlm_lockargs {
399 * struct nlm_lock alock;
404 static int nlm_xdr_enc_lockargs(struct rpc_rqst *req, __be32 *p,
405 const struct nlm_args *args)
407 const struct nlm_lock *lock = &args->lock;
408 struct xdr_stream xdr;
410 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
411 encode_cookie(&xdr, &args->cookie);
412 encode_bool(&xdr, args->block);
413 encode_bool(&xdr, lock->fl.fl_type == F_WRLCK);
414 encode_nlm_lock(&xdr, lock);
415 encode_bool(&xdr, args->reclaim);
416 encode_int32(&xdr, args->state);
421 * struct nlm_cancargs {
425 * struct nlm_lock alock;
428 static int nlm_xdr_enc_cancargs(struct rpc_rqst *req, __be32 *p,
429 const struct nlm_args *args)
431 const struct nlm_lock *lock = &args->lock;
432 struct xdr_stream xdr;
434 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
435 encode_cookie(&xdr, &args->cookie);
436 encode_bool(&xdr, args->block);
437 encode_bool(&xdr, lock->fl.fl_type == F_WRLCK);
438 encode_nlm_lock(&xdr, lock);
443 * struct nlm_unlockargs {
445 * struct nlm_lock alock;
448 static int nlm_xdr_enc_unlockargs(struct rpc_rqst *req, __be32 *p,
449 const struct nlm_args *args)
451 const struct nlm_lock *lock = &args->lock;
452 struct xdr_stream xdr;
454 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
455 encode_cookie(&xdr, &args->cookie);
456 encode_nlm_lock(&xdr, lock);
466 static int nlm_xdr_enc_res(struct rpc_rqst *req, __be32 *p,
467 const struct nlm_res *result)
469 struct xdr_stream xdr;
471 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
472 encode_cookie(&xdr, &result->cookie);
473 encode_nlm_stat(&xdr, result->status);
478 * union nlm_testrply switch (nlm_stats stat) {
480 * struct nlm_holder holder;
485 * struct nlm_testres {
487 * nlm_testrply test_stat;
490 static void encode_nlm_testrply(struct xdr_stream *xdr,
491 const struct nlm_res *result)
493 if (result->status == nlm_lck_denied)
494 encode_nlm_holder(xdr, result);
497 static int nlm_xdr_enc_testres(struct rpc_rqst *req, __be32 *p,
498 const struct nlm_res *result)
500 struct xdr_stream xdr;
502 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
503 encode_cookie(&xdr, &result->cookie);
504 encode_nlm_stat(&xdr, result->status);
505 encode_nlm_testrply(&xdr, result);
511 * NLMv3 XDR decode functions
513 * NLMv3 result types are defined in Chapter 10 of The Open Group's
514 * "Protocols for Interworking: XNFS, Version 3W".
518 * union nlm_testrply switch (nlm_stats stat) {
520 * struct nlm_holder holder;
525 * struct nlm_testres {
527 * nlm_testrply test_stat;
530 static int decode_nlm_testrply(struct xdr_stream *xdr,
531 struct nlm_res *result)
535 error = decode_nlm_stat(xdr, &result->status);
538 if (result->status == nlm_lck_denied)
539 error = decode_nlm_holder(xdr, result);
544 static int nlm_xdr_dec_testres(struct rpc_rqst *req, __be32 *p,
545 struct nlm_res *result)
547 struct xdr_stream xdr;
550 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
551 error = decode_cookie(&xdr, &result->cookie);
554 error = decode_nlm_testrply(&xdr, result);
565 static int nlm_xdr_dec_res(struct rpc_rqst *req, __be32 *p,
566 struct nlm_res *result)
568 struct xdr_stream xdr;
571 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
572 error = decode_cookie(&xdr, &result->cookie);
575 error = decode_nlm_stat(&xdr, &result->status);
582 * For NLM, a void procedure really returns nothing
584 #define nlm_xdr_dec_norep NULL
586 #define PROC(proc, argtype, restype) \
587 [NLMPROC_##proc] = { \
588 .p_proc = NLMPROC_##proc, \
589 .p_encode = (kxdrproc_t)nlm_xdr_enc_##argtype, \
590 .p_decode = (kxdrproc_t)nlm_xdr_dec_##restype, \
591 .p_arglen = NLM_##argtype##_sz, \
592 .p_replen = NLM_##restype##_sz, \
593 .p_statidx = NLMPROC_##proc, \
597 static struct rpc_procinfo nlm_procedures[] = {
598 PROC(TEST, testargs, testres),
599 PROC(LOCK, lockargs, res),
600 PROC(CANCEL, cancargs, res),
601 PROC(UNLOCK, unlockargs, res),
602 PROC(GRANTED, testargs, res),
603 PROC(TEST_MSG, testargs, norep),
604 PROC(LOCK_MSG, lockargs, norep),
605 PROC(CANCEL_MSG, cancargs, norep),
606 PROC(UNLOCK_MSG, unlockargs, norep),
607 PROC(GRANTED_MSG, testargs, norep),
608 PROC(TEST_RES, testres, norep),
609 PROC(LOCK_RES, res, norep),
610 PROC(CANCEL_RES, res, norep),
611 PROC(UNLOCK_RES, res, norep),
612 PROC(GRANTED_RES, res, norep),
615 static struct rpc_version nlm_version1 = {
617 .nrprocs = ARRAY_SIZE(nlm_procedures),
618 .procs = nlm_procedures,
621 static struct rpc_version nlm_version3 = {
623 .nrprocs = ARRAY_SIZE(nlm_procedures),
624 .procs = nlm_procedures,
627 static struct rpc_version *nlm_versions[] = {
630 #ifdef CONFIG_LOCKD_V4
635 static struct rpc_stat nlm_rpc_stats;
637 struct rpc_program nlm_program = {
639 .number = NLM_PROGRAM,
640 .nrvers = ARRAY_SIZE(nlm_versions),
641 .version = nlm_versions,
642 .stats = &nlm_rpc_stats,