1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/lockd/xdr4.c
5 * XDR support for lockd and the lock client.
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8 * Copyright (C) 1999, Trond Myklebust <trond.myklebust@fys.uio.no>
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/nfs.h>
15 #include <linux/sunrpc/xdr.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/svc.h>
18 #include <linux/sunrpc/stats.h>
19 #include <linux/lockd/lockd.h>
23 #define NLMDBG_FACILITY NLMDBG_XDR
26 s64_to_loff_t(__s64 offset)
28 return (loff_t)offset;
33 loff_t_to_s64(loff_t offset)
36 if (offset > NLM4_OFFSET_MAX)
37 res = NLM4_OFFSET_MAX;
38 else if (offset < -NLM4_OFFSET_MAX)
39 res = -NLM4_OFFSET_MAX;
46 nlm4_encode_cookie(__be32 *p, struct nlm_cookie *c)
49 memcpy(p, c->data, c->len);
50 p+=XDR_QUADLEN(c->len);
55 * NLM file handles are defined by specification to be a variable-length
56 * XDR opaque no longer than 1024 bytes. However, this implementation
57 * limits their length to the size of an NFSv3 file handle.
60 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
65 if (xdr_stream_decode_u32(xdr, &len) < 0)
67 if (len > NFS_MAXFHSIZE)
70 p = xdr_inline_decode(xdr, len);
74 memcpy(fh->data, p, len);
75 memset(fh->data + len, 0, sizeof(fh->data) - len);
81 svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
83 struct file_lock *fl = &lock->fl;
87 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
89 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
91 if (!svcxdr_decode_owner(xdr, &lock->oh))
93 if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
95 if (xdr_stream_decode_u64(xdr, &start) < 0)
97 if (xdr_stream_decode_u64(xdr, &len) < 0)
101 fl->fl_flags = FL_POSIX;
102 fl->fl_type = F_RDLCK;
103 end = start + len - 1;
104 fl->fl_start = s64_to_loff_t(start);
105 if (len == 0 || end < 0)
106 fl->fl_end = OFFSET_MAX;
108 fl->fl_end = s64_to_loff_t(end);
114 * Encode result of a TEST/TEST_MSG call
117 nlm4_encode_testres(__be32 *p, struct nlm_res *resp)
121 dprintk("xdr: before encode_testres (p %p resp %p)\n", p, resp);
122 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
126 if (resp->status == nlm_lck_denied) {
127 struct file_lock *fl = &resp->lock.fl;
129 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
130 *p++ = htonl(resp->lock.svid);
132 /* Encode owner handle. */
133 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
136 start = loff_t_to_s64(fl->fl_start);
137 if (fl->fl_end == OFFSET_MAX)
140 len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
142 p = xdr_encode_hyper(p, start);
143 p = xdr_encode_hyper(p, len);
144 dprintk("xdr: encode_testres (status %u pid %d type %d start %Ld end %Ld)\n",
145 resp->status, (int)resp->lock.svid, fl->fl_type,
146 (long long)fl->fl_start, (long long)fl->fl_end);
149 dprintk("xdr: after encode_testres (p %p resp %p)\n", p, resp);
155 * Decode Call arguments
159 nlm4svc_decode_void(struct svc_rqst *rqstp, __be32 *p)
165 nlm4svc_decode_testargs(struct svc_rqst *rqstp, __be32 *p)
167 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
168 struct nlm_args *argp = rqstp->rq_argp;
171 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
173 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
175 if (!svcxdr_decode_lock(xdr, &argp->lock))
178 argp->lock.fl.fl_type = F_WRLCK;
184 nlm4svc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p)
186 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
187 struct nlm_args *argp = rqstp->rq_argp;
190 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
192 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
194 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
196 if (!svcxdr_decode_lock(xdr, &argp->lock))
199 argp->lock.fl.fl_type = F_WRLCK;
200 if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
202 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
204 argp->monitor = 1; /* monitor client by default */
210 nlm4svc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p)
212 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
213 struct nlm_args *argp = rqstp->rq_argp;
216 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
218 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
220 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
222 if (!svcxdr_decode_lock(xdr, &argp->lock))
225 argp->lock.fl.fl_type = F_WRLCK;
230 nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p)
232 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
233 struct nlm_args *argp = rqstp->rq_argp;
235 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
237 if (!svcxdr_decode_lock(xdr, &argp->lock))
239 argp->lock.fl.fl_type = F_UNLCK;
245 nlm4svc_decode_res(struct svc_rqst *rqstp, __be32 *p)
247 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
248 struct nlm_res *resp = rqstp->rq_argp;
250 if (!svcxdr_decode_cookie(xdr, &resp->cookie))
252 if (!svcxdr_decode_stats(xdr, &resp->status))
259 nlm4svc_decode_reboot(struct svc_rqst *rqstp, __be32 *p)
261 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
262 struct nlm_reboot *argp = rqstp->rq_argp;
265 if (xdr_stream_decode_u32(xdr, &len) < 0)
267 if (len > SM_MAXSTRLEN)
269 p = xdr_inline_decode(xdr, len);
273 argp->mon = (char *)p;
274 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
276 p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
279 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
285 nlm4svc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p)
287 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
288 struct nlm_args *argp = rqstp->rq_argp;
289 struct nlm_lock *lock = &argp->lock;
291 memset(lock, 0, sizeof(*lock));
292 locks_init_lock(&lock->fl);
293 lock->svid = ~(u32)0;
295 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
297 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
299 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
301 if (!svcxdr_decode_owner(xdr, &lock->oh))
303 /* XXX: Range checks are missing in the original code */
304 if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
306 if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
313 nlm4svc_encode_testres(struct svc_rqst *rqstp, __be32 *p)
315 struct nlm_res *resp = rqstp->rq_resp;
317 if (!(p = nlm4_encode_testres(p, resp)))
319 return xdr_ressize_check(rqstp, p);
323 nlm4svc_encode_shareres(struct svc_rqst *rqstp, __be32 *p)
325 struct nlm_res *resp = rqstp->rq_resp;
327 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
330 *p++ = xdr_zero; /* sequence argument */
331 return xdr_ressize_check(rqstp, p);
335 nlm4svc_encode_res(struct svc_rqst *rqstp, __be32 *p)
337 struct nlm_res *resp = rqstp->rq_resp;
339 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
342 return xdr_ressize_check(rqstp, p);
346 nlm4svc_decode_notify(struct svc_rqst *rqstp, __be32 *p)
348 struct nlm_args *argp = rqstp->rq_argp;
349 struct nlm_lock *lock = &argp->lock;
351 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
352 &lock->len, NLM_MAXSTRLEN)))
354 argp->state = ntohl(*p++);
355 return xdr_argsize_check(rqstp, p);
359 nlm4svc_encode_void(struct svc_rqst *rqstp, __be32 *p)
361 return xdr_ressize_check(rqstp, p);