2 * linux/fs/lockd/clnt4xdr.c
4 * XDR functions to encode/decode NLM version 4 RPC arguments and results.
6 * NLM client-side only.
8 * Copyright (C) 2010, Oracle. All rights reserved.
11 #include <linux/types.h>
12 #include <linux/sunrpc/xdr.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/sunrpc/stats.h>
15 #include <linux/lockd/lockd.h>
17 #include <uapi/linux/nfs3.h>
19 #define NLMDBG_FACILITY NLMDBG_XDR
21 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
22 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
25 #if (NLMCLNT_OHSIZE > NLM_MAXSTRLEN)
26 # error "NLM host name cannot be larger than NLM's maximum string length!"
30 * Declare the space requirements for NLM arguments and replies as
31 * number of 32bit-words
33 #define NLM4_void_sz (0)
34 #define NLM4_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
35 #define NLM4_caller_sz (1+(NLMCLNT_OHSIZE>>2))
36 #define NLM4_owner_sz (1+(NLMCLNT_OHSIZE>>2))
37 #define NLM4_fhandle_sz (1+(NFS3_FHSIZE>>2))
38 #define NLM4_lock_sz (5+NLM4_caller_sz+NLM4_owner_sz+NLM4_fhandle_sz)
39 #define NLM4_holder_sz (6+NLM4_owner_sz)
41 #define NLM4_testargs_sz (NLM4_cookie_sz+1+NLM4_lock_sz)
42 #define NLM4_lockargs_sz (NLM4_cookie_sz+4+NLM4_lock_sz)
43 #define NLM4_cancargs_sz (NLM4_cookie_sz+2+NLM4_lock_sz)
44 #define NLM4_unlockargs_sz (NLM4_cookie_sz+NLM4_lock_sz)
46 #define NLM4_testres_sz (NLM4_cookie_sz+1+NLM4_holder_sz)
47 #define NLM4_res_sz (NLM4_cookie_sz+1)
48 #define NLM4_norep_sz (0)
51 static s64 loff_t_to_s64(loff_t offset)
55 if (offset >= NLM4_OFFSET_MAX)
56 res = NLM4_OFFSET_MAX;
57 else if (offset <= -NLM4_OFFSET_MAX)
58 res = -NLM4_OFFSET_MAX;
64 static void nlm4_compute_offsets(const struct nlm_lock *lock,
65 u64 *l_offset, u64 *l_len)
67 const struct file_lock *fl = &lock->fl;
69 *l_offset = loff_t_to_s64(fl->fl_start);
70 if (fl->fl_end == OFFSET_MAX)
73 *l_len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
77 * Handle decode buffer overflows out-of-line.
79 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
81 dprintk("lockd: %s prematurely hit the end of our receive buffer. "
82 "Remaining buffer length is %tu words.\n",
83 func, xdr->end - xdr->p);
88 * Encode/decode NLMv4 basic data types
90 * Basic NLMv4 data types are defined in Appendix II, section 6.1.4
91 * of RFC 1813: "NFS Version 3 Protocol Specification" and in Chapter
92 * 10 of X/Open's "Protocols for Interworking: XNFS, Version 3W".
94 * Not all basic data types have their own encoding and decoding
95 * functions. For run-time efficiency, some data types are encoded
99 static void encode_bool(struct xdr_stream *xdr, const int value)
103 p = xdr_reserve_space(xdr, 4);
104 *p = value ? xdr_one : xdr_zero;
107 static void encode_int32(struct xdr_stream *xdr, const s32 value)
111 p = xdr_reserve_space(xdr, 4);
112 *p = cpu_to_be32(value);
116 * typedef opaque netobj<MAXNETOBJ_SZ>
118 static void encode_netobj(struct xdr_stream *xdr,
119 const u8 *data, const unsigned int length)
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 encode_netobj(xdr, (u8 *)&cookie->data, cookie->len);
159 static int decode_cookie(struct xdr_stream *xdr,
160 struct nlm_cookie *cookie)
165 p = xdr_inline_decode(xdr, 4);
166 if (unlikely(p == NULL))
168 length = be32_to_cpup(p++);
169 /* apparently HPUX can return empty cookies */
172 if (length > NLM_MAXCOOKIELEN)
174 p = xdr_inline_decode(xdr, length);
175 if (unlikely(p == NULL))
177 cookie->len = length;
178 memcpy(cookie->data, p, length);
182 memset(cookie->data, 0, 4);
185 dprintk("NFS: returned cookie was too long: %u\n", length);
188 print_overflow_msg(__func__, xdr);
195 static void encode_fh(struct xdr_stream *xdr, const struct nfs_fh *fh)
197 encode_netobj(xdr, (u8 *)&fh->data, fh->size);
204 * NLM4_DENIED_NOLOCKS = 2,
206 * NLM4_DENIED_GRACE_PERIOD = 4,
218 * NB: we don't swap bytes for the NLM status values. The upper
219 * layers deal directly with the status value in network byte
222 static void encode_nlm4_stat(struct xdr_stream *xdr,
227 BUG_ON(be32_to_cpu(stat) > NLM_FAILED);
228 p = xdr_reserve_space(xdr, 4);
232 static int decode_nlm4_stat(struct xdr_stream *xdr, __be32 *stat)
236 p = xdr_inline_decode(xdr, 4);
237 if (unlikely(p == NULL))
239 if (unlikely(ntohl(*p) > ntohl(nlm4_failed)))
244 dprintk("%s: server returned invalid nlm4_stats value: %u\n",
245 __func__, be32_to_cpup(p));
248 print_overflow_msg(__func__, xdr);
253 * struct nlm4_holder {
261 static void encode_nlm4_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 nlm4_compute_offsets(lock, &l_offset, &l_len);
274 p = xdr_encode_hyper(p, l_offset);
275 xdr_encode_hyper(p, l_len);
278 static int decode_nlm4_holder(struct xdr_stream *xdr, struct nlm_res *result)
280 struct nlm_lock *lock = &result->lock;
281 struct file_lock *fl = &lock->fl;
288 memset(lock, 0, sizeof(*lock));
291 p = xdr_inline_decode(xdr, 4 + 4);
292 if (unlikely(p == NULL))
294 exclusive = be32_to_cpup(p++);
295 lock->svid = be32_to_cpup(p);
296 fl->fl_pid = (pid_t)lock->svid;
298 error = decode_netobj(xdr, &lock->oh);
302 p = xdr_inline_decode(xdr, 8 + 8);
303 if (unlikely(p == NULL))
306 fl->fl_flags = FL_POSIX;
307 fl->fl_type = exclusive != 0 ? F_WRLCK : F_RDLCK;
308 p = xdr_decode_hyper(p, &l_offset);
309 xdr_decode_hyper(p, &l_len);
310 end = l_offset + l_len - 1;
312 fl->fl_start = (loff_t)l_offset;
313 if (l_len == 0 || end < 0)
314 fl->fl_end = OFFSET_MAX;
316 fl->fl_end = (loff_t)end;
321 print_overflow_msg(__func__, xdr);
326 * string caller_name<LM_MAXSTRLEN>;
328 static void encode_caller_name(struct xdr_stream *xdr, const char *name)
330 /* NB: client-side does not set lock->len */
331 u32 length = strlen(name);
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_nlm4_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 + 8 + 8);
359 *p++ = cpu_to_be32(lock->svid);
361 nlm4_compute_offsets(lock, &l_offset, &l_len);
362 p = xdr_encode_hyper(p, l_offset);
363 xdr_encode_hyper(p, l_len);
368 * NLMv4 XDR encode functions
370 * NLMv4 argument types are defined in Appendix II of RFC 1813:
371 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
372 * "Protocols for Interworking: XNFS, Version 3W".
376 * struct nlm4_testargs {
379 * struct nlm4_lock alock;
382 static void nlm4_xdr_enc_testargs(struct rpc_rqst *req,
383 struct xdr_stream *xdr,
386 const struct nlm_args *args = data;
387 const struct nlm_lock *lock = &args->lock;
389 encode_cookie(xdr, &args->cookie);
390 encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
391 encode_nlm4_lock(xdr, lock);
395 * struct nlm4_lockargs {
399 * struct nlm4_lock alock;
404 static void nlm4_xdr_enc_lockargs(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);
415 encode_bool(xdr, args->reclaim);
416 encode_int32(xdr, args->state);
420 * struct nlm4_cancargs {
424 * struct nlm4_lock alock;
427 static void nlm4_xdr_enc_cancargs(struct rpc_rqst *req,
428 struct xdr_stream *xdr,
431 const struct nlm_args *args = data;
432 const struct nlm_lock *lock = &args->lock;
434 encode_cookie(xdr, &args->cookie);
435 encode_bool(xdr, args->block);
436 encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
437 encode_nlm4_lock(xdr, lock);
441 * struct nlm4_unlockargs {
443 * struct nlm4_lock alock;
446 static void nlm4_xdr_enc_unlockargs(struct rpc_rqst *req,
447 struct xdr_stream *xdr,
450 const struct nlm_args *args = data;
451 const struct nlm_lock *lock = &args->lock;
453 encode_cookie(xdr, &args->cookie);
454 encode_nlm4_lock(xdr, lock);
463 static void nlm4_xdr_enc_res(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);
474 * union nlm4_testrply switch (nlm4_stats stat) {
476 * struct nlm4_holder holder;
481 * struct nlm4_testres {
483 * nlm4_testrply test_stat;
486 static void nlm4_xdr_enc_testres(struct rpc_rqst *req,
487 struct xdr_stream *xdr,
490 const struct nlm_res *result = data;
492 encode_cookie(xdr, &result->cookie);
493 encode_nlm4_stat(xdr, result->status);
494 if (result->status == nlm_lck_denied)
495 encode_nlm4_holder(xdr, result);
500 * NLMv4 XDR decode functions
502 * NLMv4 argument types are defined in Appendix II of RFC 1813:
503 * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
504 * "Protocols for Interworking: XNFS, Version 3W".
508 * union nlm4_testrply switch (nlm4_stats stat) {
510 * struct nlm4_holder holder;
515 * struct nlm4_testres {
517 * nlm4_testrply test_stat;
520 static int decode_nlm4_testrply(struct xdr_stream *xdr,
521 struct nlm_res *result)
525 error = decode_nlm4_stat(xdr, &result->status);
528 if (result->status == nlm_lck_denied)
529 error = decode_nlm4_holder(xdr, result);
534 static int nlm4_xdr_dec_testres(struct rpc_rqst *req,
535 struct xdr_stream *xdr,
538 struct nlm_res *result = data;
541 error = decode_cookie(xdr, &result->cookie);
544 error = decode_nlm4_testrply(xdr, result);
555 static int nlm4_xdr_dec_res(struct rpc_rqst *req,
556 struct xdr_stream *xdr,
559 struct nlm_res *result = data;
562 error = decode_cookie(xdr, &result->cookie);
565 error = decode_nlm4_stat(xdr, &result->status);
572 * For NLM, a void procedure really returns nothing
574 #define nlm4_xdr_dec_norep NULL
576 #define PROC(proc, argtype, restype) \
577 [NLMPROC_##proc] = { \
578 .p_proc = NLMPROC_##proc, \
579 .p_encode = nlm4_xdr_enc_##argtype, \
580 .p_decode = nlm4_xdr_dec_##restype, \
581 .p_arglen = NLM4_##argtype##_sz, \
582 .p_replen = NLM4_##restype##_sz, \
583 .p_statidx = NLMPROC_##proc, \
587 static const struct rpc_procinfo nlm4_procedures[] = {
588 PROC(TEST, testargs, testres),
589 PROC(LOCK, lockargs, res),
590 PROC(CANCEL, cancargs, res),
591 PROC(UNLOCK, unlockargs, res),
592 PROC(GRANTED, testargs, res),
593 PROC(TEST_MSG, testargs, norep),
594 PROC(LOCK_MSG, lockargs, norep),
595 PROC(CANCEL_MSG, cancargs, norep),
596 PROC(UNLOCK_MSG, unlockargs, norep),
597 PROC(GRANTED_MSG, testargs, norep),
598 PROC(TEST_RES, testres, norep),
599 PROC(LOCK_RES, res, norep),
600 PROC(CANCEL_RES, res, norep),
601 PROC(UNLOCK_RES, res, norep),
602 PROC(GRANTED_RES, res, norep),
605 static unsigned int nlm_version4_counts[ARRAY_SIZE(nlm4_procedures)];
606 const struct rpc_version nlm_version4 = {
608 .nrprocs = ARRAY_SIZE(nlm4_procedures),
609 .procs = nlm4_procedures,
610 .counts = nlm_version4_counts,