1 // SPDX-License-Identifier: GPL-2.0
5 * XDR support for lockd and the lock client.
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/nfs.h>
14 #include <linux/sunrpc/xdr.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/svc.h>
17 #include <linux/sunrpc/stats.h>
18 #include <linux/lockd/lockd.h>
20 #include <uapi/linux/nfs2.h>
26 s32_to_loff_t(__s32 offset)
28 return (loff_t)offset;
32 loff_t_to_s32(loff_t offset)
35 if (offset >= NLM_OFFSET_MAX)
37 else if (offset <= -NLM_OFFSET_MAX)
38 res = -NLM_OFFSET_MAX;
45 * NLM file handles are defined by specification to be a variable-length
46 * XDR opaque no longer than 1024 bytes. However, this implementation
47 * constrains their length to exactly the length of an NFSv2 file
51 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
56 if (xdr_stream_decode_u32(xdr, &len) < 0)
58 if (len != NFS2_FHSIZE)
61 p = xdr_inline_decode(xdr, len);
64 fh->size = NFS2_FHSIZE;
65 memcpy(fh->data, p, len);
66 memset(fh->data + NFS2_FHSIZE, 0, sizeof(fh->data) - NFS2_FHSIZE);
72 svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
74 struct file_lock *fl = &lock->fl;
77 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
79 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
81 if (!svcxdr_decode_owner(xdr, &lock->oh))
83 if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
85 if (xdr_stream_decode_u32(xdr, &start) < 0)
87 if (xdr_stream_decode_u32(xdr, &len) < 0)
91 fl->fl_flags = FL_POSIX;
92 fl->fl_type = F_RDLCK;
93 end = start + len - 1;
94 fl->fl_start = s32_to_loff_t(start);
95 if (len == 0 || end < 0)
96 fl->fl_end = OFFSET_MAX;
98 fl->fl_end = s32_to_loff_t(end);
104 svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
106 const struct file_lock *fl = &lock->fl;
110 if (xdr_stream_encode_bool(xdr, fl->fl_type != F_RDLCK) < 0)
112 if (xdr_stream_encode_u32(xdr, lock->svid) < 0)
114 if (!svcxdr_encode_owner(xdr, &lock->oh))
116 start = loff_t_to_s32(fl->fl_start);
117 if (fl->fl_end == OFFSET_MAX)
120 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
121 if (xdr_stream_encode_u32(xdr, start) < 0)
123 if (xdr_stream_encode_u32(xdr, len) < 0)
130 svcxdr_encode_testrply(struct xdr_stream *xdr, const struct nlm_res *resp)
132 if (!svcxdr_encode_stats(xdr, resp->status))
134 switch (resp->status) {
136 if (!svcxdr_encode_holder(xdr, &resp->lock))
145 * Decode Call arguments
149 nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p)
155 nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p)
157 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
158 struct nlm_args *argp = rqstp->rq_argp;
161 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
163 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
165 if (!svcxdr_decode_lock(xdr, &argp->lock))
168 argp->lock.fl.fl_type = F_WRLCK;
174 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p)
176 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
177 struct nlm_args *argp = rqstp->rq_argp;
180 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
182 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
184 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
186 if (!svcxdr_decode_lock(xdr, &argp->lock))
189 argp->lock.fl.fl_type = F_WRLCK;
190 if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
192 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
194 argp->monitor = 1; /* monitor client by default */
200 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p)
202 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
203 struct nlm_args *argp = rqstp->rq_argp;
206 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
208 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
210 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
212 if (!svcxdr_decode_lock(xdr, &argp->lock))
215 argp->lock.fl.fl_type = F_WRLCK;
221 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p)
223 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
224 struct nlm_args *argp = rqstp->rq_argp;
226 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
228 if (!svcxdr_decode_lock(xdr, &argp->lock))
230 argp->lock.fl.fl_type = F_UNLCK;
236 nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p)
238 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
239 struct nlm_res *resp = rqstp->rq_argp;
241 if (!svcxdr_decode_cookie(xdr, &resp->cookie))
243 if (!svcxdr_decode_stats(xdr, &resp->status))
250 nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p)
252 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
253 struct nlm_reboot *argp = rqstp->rq_argp;
256 if (xdr_stream_decode_u32(xdr, &len) < 0)
258 if (len > SM_MAXSTRLEN)
260 p = xdr_inline_decode(xdr, len);
264 argp->mon = (char *)p;
265 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
267 p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
270 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
276 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p)
278 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
279 struct nlm_args *argp = rqstp->rq_argp;
280 struct nlm_lock *lock = &argp->lock;
282 memset(lock, 0, sizeof(*lock));
283 locks_init_lock(&lock->fl);
284 lock->svid = ~(u32)0;
286 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
288 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
290 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
292 if (!svcxdr_decode_owner(xdr, &lock->oh))
294 /* XXX: Range checks are missing in the original code */
295 if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
297 if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
304 nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p)
306 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
307 struct nlm_args *argp = rqstp->rq_argp;
308 struct nlm_lock *lock = &argp->lock;
310 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
312 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
320 * Encode Reply results
324 nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p)
330 nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p)
332 struct xdr_stream *xdr = &rqstp->rq_res_stream;
333 struct nlm_res *resp = rqstp->rq_resp;
335 return svcxdr_encode_cookie(xdr, &resp->cookie) &&
336 svcxdr_encode_testrply(xdr, resp);
340 nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p)
342 struct xdr_stream *xdr = &rqstp->rq_res_stream;
343 struct nlm_res *resp = rqstp->rq_resp;
345 return svcxdr_encode_cookie(xdr, &resp->cookie) &&
346 svcxdr_encode_stats(xdr, resp->status);
350 nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p)
352 struct xdr_stream *xdr = &rqstp->rq_res_stream;
353 struct nlm_res *resp = rqstp->rq_resp;
355 if (!svcxdr_encode_cookie(xdr, &resp->cookie))
357 if (!svcxdr_encode_stats(xdr, resp->status))
360 if (xdr_stream_encode_u32(xdr, 0) < 0)