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>
24 s64_to_loff_t(__s64 offset)
26 return (loff_t)offset;
31 loff_t_to_s64(loff_t offset)
34 if (offset > NLM4_OFFSET_MAX)
35 res = NLM4_OFFSET_MAX;
36 else if (offset < -NLM4_OFFSET_MAX)
37 res = -NLM4_OFFSET_MAX;
44 * NLM file handles are defined by specification to be a variable-length
45 * XDR opaque no longer than 1024 bytes. However, this implementation
46 * limits their length to the size of an NFSv3 file handle.
49 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
54 if (xdr_stream_decode_u32(xdr, &len) < 0)
56 if (len > NFS_MAXFHSIZE)
59 p = xdr_inline_decode(xdr, len);
63 memcpy(fh->data, p, len);
64 memset(fh->data + len, 0, sizeof(fh->data) - len);
70 svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
72 struct file_lock *fl = &lock->fl;
76 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
78 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
80 if (!svcxdr_decode_owner(xdr, &lock->oh))
82 if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
84 if (xdr_stream_decode_u64(xdr, &start) < 0)
86 if (xdr_stream_decode_u64(xdr, &len) < 0)
90 fl->fl_flags = FL_POSIX;
91 fl->fl_type = F_RDLCK;
92 end = start + len - 1;
93 fl->fl_start = s64_to_loff_t(start);
94 if (len == 0 || end < 0)
95 fl->fl_end = OFFSET_MAX;
97 fl->fl_end = s64_to_loff_t(end);
103 svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
105 const struct file_lock *fl = &lock->fl;
109 if (xdr_stream_encode_bool(xdr, fl->fl_type != F_RDLCK) < 0)
111 if (xdr_stream_encode_u32(xdr, lock->svid) < 0)
113 if (!svcxdr_encode_owner(xdr, &lock->oh))
115 start = loff_t_to_s64(fl->fl_start);
116 if (fl->fl_end == OFFSET_MAX)
119 len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
120 if (xdr_stream_encode_u64(xdr, start) < 0)
122 if (xdr_stream_encode_u64(xdr, len) < 0)
129 svcxdr_encode_testrply(struct xdr_stream *xdr, const struct nlm_res *resp)
131 if (!svcxdr_encode_stats(xdr, resp->status))
133 switch (resp->status) {
135 if (!svcxdr_encode_holder(xdr, &resp->lock))
144 * Decode Call arguments
148 nlm4svc_decode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
154 nlm4svc_decode_testargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
156 struct nlm_args *argp = rqstp->rq_argp;
159 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
161 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
163 if (!svcxdr_decode_lock(xdr, &argp->lock))
166 argp->lock.fl.fl_type = F_WRLCK;
172 nlm4svc_decode_lockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
174 struct nlm_args *argp = rqstp->rq_argp;
177 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
179 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
181 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
183 if (!svcxdr_decode_lock(xdr, &argp->lock))
186 argp->lock.fl.fl_type = F_WRLCK;
187 if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
189 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
191 argp->monitor = 1; /* monitor client by default */
197 nlm4svc_decode_cancargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
199 struct nlm_args *argp = rqstp->rq_argp;
202 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
204 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
206 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
208 if (!svcxdr_decode_lock(xdr, &argp->lock))
211 argp->lock.fl.fl_type = F_WRLCK;
217 nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
219 struct nlm_args *argp = rqstp->rq_argp;
221 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
223 if (!svcxdr_decode_lock(xdr, &argp->lock))
225 argp->lock.fl.fl_type = F_UNLCK;
231 nlm4svc_decode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
233 struct nlm_res *resp = rqstp->rq_argp;
235 if (!svcxdr_decode_cookie(xdr, &resp->cookie))
237 if (!svcxdr_decode_stats(xdr, &resp->status))
244 nlm4svc_decode_reboot(struct svc_rqst *rqstp, struct xdr_stream *xdr)
246 struct nlm_reboot *argp = rqstp->rq_argp;
250 if (xdr_stream_decode_u32(xdr, &len) < 0)
252 if (len > SM_MAXSTRLEN)
254 p = xdr_inline_decode(xdr, len);
258 argp->mon = (char *)p;
259 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
261 p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
264 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
270 nlm4svc_decode_shareargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
272 struct nlm_args *argp = rqstp->rq_argp;
273 struct nlm_lock *lock = &argp->lock;
275 memset(lock, 0, sizeof(*lock));
276 locks_init_lock(&lock->fl);
277 lock->svid = ~(u32)0;
279 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
281 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
283 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
285 if (!svcxdr_decode_owner(xdr, &lock->oh))
287 /* XXX: Range checks are missing in the original code */
288 if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
290 if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
297 nlm4svc_decode_notify(struct svc_rqst *rqstp, struct xdr_stream *xdr)
299 struct nlm_args *argp = rqstp->rq_argp;
300 struct nlm_lock *lock = &argp->lock;
302 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
304 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
312 * Encode Reply results
316 nlm4svc_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
322 nlm4svc_encode_testres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
324 struct nlm_res *resp = rqstp->rq_resp;
326 return svcxdr_encode_cookie(xdr, &resp->cookie) &&
327 svcxdr_encode_testrply(xdr, resp);
331 nlm4svc_encode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
333 struct nlm_res *resp = rqstp->rq_resp;
335 return svcxdr_encode_cookie(xdr, &resp->cookie) &&
336 svcxdr_encode_stats(xdr, resp->status);
340 nlm4svc_encode_shareres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
342 struct nlm_res *resp = rqstp->rq_resp;
344 if (!svcxdr_encode_cookie(xdr, &resp->cookie))
346 if (!svcxdr_encode_stats(xdr, resp->status))
349 if (xdr_stream_encode_u32(xdr, 0) < 0)