1 // SPDX-License-Identifier: GPL-2.0
5 * XDR support for lockd and the lock client.
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
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>
24 #define NLMDBG_FACILITY NLMDBG_XDR
28 s32_to_loff_t(__s32 offset)
30 return (loff_t)offset;
34 loff_t_to_s32(loff_t offset)
37 if (offset >= NLM_OFFSET_MAX)
39 else if (offset <= -NLM_OFFSET_MAX)
40 res = -NLM_OFFSET_MAX;
47 * XDR functions for basic NLM types
49 static __be32 *nlm_decode_cookie(__be32 *p, struct nlm_cookie *c)
58 memset(c->data, 0, 4); /* hockeypux brain damage */
60 else if(len<=NLM_MAXCOOKIELEN)
63 memcpy(c->data, p, len);
68 dprintk("lockd: bad cookie size %d (only cookies under "
69 "%d bytes are supported.)\n",
70 len, NLM_MAXCOOKIELEN);
76 static inline __be32 *
77 nlm_encode_cookie(__be32 *p, struct nlm_cookie *c)
80 memcpy(p, c->data, c->len);
81 p+=XDR_QUADLEN(c->len);
86 nlm_decode_fh(__be32 *p, struct nfs_fh *f)
90 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
91 dprintk("lockd: bad fhandle size %d (should be %d)\n",
95 f->size = NFS2_FHSIZE;
96 memset(f->data, 0, sizeof(f->data));
97 memcpy(f->data, p, NFS2_FHSIZE);
98 return p + XDR_QUADLEN(NFS2_FHSIZE);
102 * NLM file handles are defined by specification to be a variable-length
103 * XDR opaque no longer than 1024 bytes. However, this implementation
104 * constrains their length to exactly the length of an NFSv2 file
108 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
113 if (xdr_stream_decode_u32(xdr, &len) < 0)
115 if (len != NFS2_FHSIZE)
118 p = xdr_inline_decode(xdr, len);
121 fh->size = NFS2_FHSIZE;
122 memcpy(fh->data, p, len);
123 memset(fh->data + NFS2_FHSIZE, 0, sizeof(fh->data) - NFS2_FHSIZE);
129 * Encode and decode owner handle
131 static inline __be32 *
132 nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
134 return xdr_decode_netobj(p, oh);
137 static inline __be32 *
138 nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
140 return xdr_encode_netobj(p, oh);
144 svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
146 struct file_lock *fl = &lock->fl;
149 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
151 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
153 if (!svcxdr_decode_owner(xdr, &lock->oh))
155 if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
157 if (xdr_stream_decode_u32(xdr, &start) < 0)
159 if (xdr_stream_decode_u32(xdr, &len) < 0)
163 fl->fl_flags = FL_POSIX;
164 fl->fl_type = F_RDLCK;
165 end = start + len - 1;
166 fl->fl_start = s32_to_loff_t(start);
167 if (len == 0 || end < 0)
168 fl->fl_end = OFFSET_MAX;
170 fl->fl_end = s32_to_loff_t(end);
176 * Encode result of a TEST/TEST_MSG call
179 nlm_encode_testres(__be32 *p, struct nlm_res *resp)
183 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
187 if (resp->status == nlm_lck_denied) {
188 struct file_lock *fl = &resp->lock.fl;
190 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
191 *p++ = htonl(resp->lock.svid);
193 /* Encode owner handle. */
194 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
197 start = loff_t_to_s32(fl->fl_start);
198 if (fl->fl_end == OFFSET_MAX)
201 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
212 * Decode Call arguments
216 nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p)
222 nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p)
224 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
225 struct nlm_args *argp = rqstp->rq_argp;
228 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
230 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
232 if (!svcxdr_decode_lock(xdr, &argp->lock))
235 argp->lock.fl.fl_type = F_WRLCK;
241 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p)
243 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
244 struct nlm_args *argp = rqstp->rq_argp;
247 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
249 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
251 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
253 if (!svcxdr_decode_lock(xdr, &argp->lock))
256 argp->lock.fl.fl_type = F_WRLCK;
257 if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
259 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
261 argp->monitor = 1; /* monitor client by default */
267 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p)
269 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
270 struct nlm_args *argp = rqstp->rq_argp;
273 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
275 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
277 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
279 if (!svcxdr_decode_lock(xdr, &argp->lock))
282 argp->lock.fl.fl_type = F_WRLCK;
288 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p)
290 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
291 struct nlm_args *argp = rqstp->rq_argp;
293 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
295 if (!svcxdr_decode_lock(xdr, &argp->lock))
297 argp->lock.fl.fl_type = F_UNLCK;
303 nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p)
305 struct nlm_res *resp = rqstp->rq_resp;
307 if (!(p = nlm_encode_testres(p, resp)))
309 return xdr_ressize_check(rqstp, p);
313 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p)
315 struct nlm_args *argp = rqstp->rq_argp;
316 struct nlm_lock *lock = &argp->lock;
318 memset(lock, 0, sizeof(*lock));
319 locks_init_lock(&lock->fl);
320 lock->svid = ~(u32) 0;
322 if (!(p = nlm_decode_cookie(p, &argp->cookie))
323 || !(p = xdr_decode_string_inplace(p, &lock->caller,
324 &lock->len, NLM_MAXSTRLEN))
325 || !(p = nlm_decode_fh(p, &lock->fh))
326 || !(p = nlm_decode_oh(p, &lock->oh)))
328 argp->fsm_mode = ntohl(*p++);
329 argp->fsm_access = ntohl(*p++);
330 return xdr_argsize_check(rqstp, p);
334 nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p)
336 struct nlm_res *resp = rqstp->rq_resp;
338 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
341 *p++ = xdr_zero; /* sequence argument */
342 return xdr_ressize_check(rqstp, p);
346 nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p)
348 struct nlm_res *resp = rqstp->rq_resp;
350 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
353 return xdr_ressize_check(rqstp, p);
357 nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p)
359 struct nlm_args *argp = rqstp->rq_argp;
360 struct nlm_lock *lock = &argp->lock;
362 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
363 &lock->len, NLM_MAXSTRLEN)))
365 argp->state = ntohl(*p++);
366 return xdr_argsize_check(rqstp, p);
370 nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p)
372 struct nlm_reboot *argp = rqstp->rq_argp;
374 if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
376 argp->state = ntohl(*p++);
377 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
378 p += XDR_QUADLEN(SM_PRIV_SIZE);
379 return xdr_argsize_check(rqstp, p);
383 nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p)
385 struct nlm_res *resp = rqstp->rq_argp;
387 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
390 return xdr_argsize_check(rqstp, p);
394 nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p)
396 return xdr_ressize_check(rqstp, p);