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 * XDR functions for basic NLM types
49 nlm4_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);
77 nlm4_encode_cookie(__be32 *p, struct nlm_cookie *c)
80 memcpy(p, c->data, c->len);
81 p+=XDR_QUADLEN(c->len);
86 nlm4_decode_fh(__be32 *p, struct nfs_fh *f)
88 memset(f->data, 0, sizeof(f->data));
89 f->size = ntohl(*p++);
90 if (f->size > NFS_MAXFHSIZE) {
91 dprintk("lockd: bad fhandle size %d (should be <=%d)\n",
92 f->size, NFS_MAXFHSIZE);
95 memcpy(f->data, p, f->size);
96 return p + XDR_QUADLEN(f->size);
100 * NLM file handles are defined by specification to be a variable-length
101 * XDR opaque no longer than 1024 bytes. However, this implementation
102 * limits their length to the size of an NFSv3 file handle.
105 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
110 if (xdr_stream_decode_u32(xdr, &len) < 0)
112 if (len > NFS_MAXFHSIZE)
115 p = xdr_inline_decode(xdr, len);
119 memcpy(fh->data, p, len);
120 memset(fh->data + len, 0, sizeof(fh->data) - len);
126 * Encode and decode owner handle
129 nlm4_decode_oh(__be32 *p, struct xdr_netobj *oh)
131 return xdr_decode_netobj(p, oh);
135 nlm4_decode_lock(__be32 *p, struct nlm_lock *lock)
137 struct file_lock *fl = &lock->fl;
141 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
142 &lock->len, NLM_MAXSTRLEN))
143 || !(p = nlm4_decode_fh(p, &lock->fh))
144 || !(p = nlm4_decode_oh(p, &lock->oh)))
146 lock->svid = ntohl(*p++);
149 fl->fl_flags = FL_POSIX;
150 fl->fl_type = F_RDLCK; /* as good as anything else */
151 p = xdr_decode_hyper(p, &start);
152 p = xdr_decode_hyper(p, &len);
153 end = start + len - 1;
155 fl->fl_start = s64_to_loff_t(start);
157 if (len == 0 || end < 0)
158 fl->fl_end = OFFSET_MAX;
160 fl->fl_end = s64_to_loff_t(end);
165 svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
167 struct file_lock *fl = &lock->fl;
171 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
173 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
175 if (!svcxdr_decode_owner(xdr, &lock->oh))
177 if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
179 if (xdr_stream_decode_u64(xdr, &start) < 0)
181 if (xdr_stream_decode_u64(xdr, &len) < 0)
185 fl->fl_flags = FL_POSIX;
186 fl->fl_type = F_RDLCK;
187 end = start + len - 1;
188 fl->fl_start = s64_to_loff_t(start);
189 if (len == 0 || end < 0)
190 fl->fl_end = OFFSET_MAX;
192 fl->fl_end = s64_to_loff_t(end);
198 * Encode result of a TEST/TEST_MSG call
201 nlm4_encode_testres(__be32 *p, struct nlm_res *resp)
205 dprintk("xdr: before encode_testres (p %p resp %p)\n", p, resp);
206 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
210 if (resp->status == nlm_lck_denied) {
211 struct file_lock *fl = &resp->lock.fl;
213 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
214 *p++ = htonl(resp->lock.svid);
216 /* Encode owner handle. */
217 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
220 start = loff_t_to_s64(fl->fl_start);
221 if (fl->fl_end == OFFSET_MAX)
224 len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
226 p = xdr_encode_hyper(p, start);
227 p = xdr_encode_hyper(p, len);
228 dprintk("xdr: encode_testres (status %u pid %d type %d start %Ld end %Ld)\n",
229 resp->status, (int)resp->lock.svid, fl->fl_type,
230 (long long)fl->fl_start, (long long)fl->fl_end);
233 dprintk("xdr: after encode_testres (p %p resp %p)\n", p, resp);
239 * Decode Call arguments
243 nlm4svc_decode_void(struct svc_rqst *rqstp, __be32 *p)
249 nlm4svc_decode_testargs(struct svc_rqst *rqstp, __be32 *p)
251 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
252 struct nlm_args *argp = rqstp->rq_argp;
255 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
257 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
259 if (!svcxdr_decode_lock(xdr, &argp->lock))
262 argp->lock.fl.fl_type = F_WRLCK;
268 nlm4svc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p)
270 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
271 struct nlm_args *argp = rqstp->rq_argp;
274 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
276 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
278 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
280 if (!svcxdr_decode_lock(xdr, &argp->lock))
283 argp->lock.fl.fl_type = F_WRLCK;
284 if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
286 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
288 argp->monitor = 1; /* monitor client by default */
294 nlm4svc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p)
296 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
297 struct nlm_args *argp = rqstp->rq_argp;
300 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
302 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
304 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
306 if (!svcxdr_decode_lock(xdr, &argp->lock))
309 argp->lock.fl.fl_type = F_WRLCK;
314 nlm4svc_encode_testres(struct svc_rqst *rqstp, __be32 *p)
316 struct nlm_res *resp = rqstp->rq_resp;
318 if (!(p = nlm4_encode_testres(p, resp)))
320 return xdr_ressize_check(rqstp, p);
324 nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p)
326 struct nlm_args *argp = rqstp->rq_argp;
328 if (!(p = nlm4_decode_cookie(p, &argp->cookie))
329 || !(p = nlm4_decode_lock(p, &argp->lock)))
331 argp->lock.fl.fl_type = F_UNLCK;
332 return xdr_argsize_check(rqstp, p);
336 nlm4svc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p)
338 struct nlm_args *argp = rqstp->rq_argp;
339 struct nlm_lock *lock = &argp->lock;
341 memset(lock, 0, sizeof(*lock));
342 locks_init_lock(&lock->fl);
343 lock->svid = ~(u32) 0;
345 if (!(p = nlm4_decode_cookie(p, &argp->cookie))
346 || !(p = xdr_decode_string_inplace(p, &lock->caller,
347 &lock->len, NLM_MAXSTRLEN))
348 || !(p = nlm4_decode_fh(p, &lock->fh))
349 || !(p = nlm4_decode_oh(p, &lock->oh)))
351 argp->fsm_mode = ntohl(*p++);
352 argp->fsm_access = ntohl(*p++);
353 return xdr_argsize_check(rqstp, p);
357 nlm4svc_encode_shareres(struct svc_rqst *rqstp, __be32 *p)
359 struct nlm_res *resp = rqstp->rq_resp;
361 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
364 *p++ = xdr_zero; /* sequence argument */
365 return xdr_ressize_check(rqstp, p);
369 nlm4svc_encode_res(struct svc_rqst *rqstp, __be32 *p)
371 struct nlm_res *resp = rqstp->rq_resp;
373 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
376 return xdr_ressize_check(rqstp, p);
380 nlm4svc_decode_notify(struct svc_rqst *rqstp, __be32 *p)
382 struct nlm_args *argp = rqstp->rq_argp;
383 struct nlm_lock *lock = &argp->lock;
385 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
386 &lock->len, NLM_MAXSTRLEN)))
388 argp->state = ntohl(*p++);
389 return xdr_argsize_check(rqstp, p);
393 nlm4svc_decode_reboot(struct svc_rqst *rqstp, __be32 *p)
395 struct nlm_reboot *argp = rqstp->rq_argp;
397 if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
399 argp->state = ntohl(*p++);
400 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
401 p += XDR_QUADLEN(SM_PRIV_SIZE);
402 return xdr_argsize_check(rqstp, p);
406 nlm4svc_decode_res(struct svc_rqst *rqstp, __be32 *p)
408 struct nlm_res *resp = rqstp->rq_argp;
410 if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
413 return xdr_argsize_check(rqstp, p);
417 nlm4svc_encode_void(struct svc_rqst *rqstp, __be32 *p)
419 return xdr_ressize_check(rqstp, p);