Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * linux/fs/lockd/xdr.c
  4 *
  5 * XDR support for lockd and the lock client.
  6 *
  7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  8 */
  9
 10#include <linux/types.h>
 11#include <linux/sched.h>
 12#include <linux/nfs.h>
 13
 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>
 19
 20#include <uapi/linux/nfs2.h>
 21
 22#include "svcxdr.h"
 23
 24
 25static inline loff_t
 26s32_to_loff_t(__s32 offset)
 27{
 28	return (loff_t)offset;
 29}
 30
 31static inline __s32
 32loff_t_to_s32(loff_t offset)
 33{
 34	__s32 res;
 35	if (offset >= NLM_OFFSET_MAX)
 36		res = NLM_OFFSET_MAX;
 37	else if (offset <= -NLM_OFFSET_MAX)
 38		res = -NLM_OFFSET_MAX;
 39	else
 40		res = offset;
 41	return res;
 42}
 43
 44/*
 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
 48 * handle.
 49 */
 50static bool
 51svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
 52{
 53	__be32 *p;
 54	u32 len;
 55
 56	if (xdr_stream_decode_u32(xdr, &len) < 0)
 57		return false;
 58	if (len != NFS2_FHSIZE)
 59		return false;
 60
 61	p = xdr_inline_decode(xdr, len);
 62	if (!p)
 63		return false;
 64	fh->size = NFS2_FHSIZE;
 65	memcpy(fh->data, p, len);
 66	memset(fh->data + NFS2_FHSIZE, 0, sizeof(fh->data) - NFS2_FHSIZE);
 67
 68	return true;
 69}
 70
 71static bool
 72svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
 73{
 74	struct file_lock *fl = &lock->fl;
 75	s32 start, len, end;
 76
 77	if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
 78		return false;
 79	if (!svcxdr_decode_fhandle(xdr, &lock->fh))
 80		return false;
 81	if (!svcxdr_decode_owner(xdr, &lock->oh))
 82		return false;
 83	if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
 84		return false;
 85	if (xdr_stream_decode_u32(xdr, &start) < 0)
 86		return false;
 87	if (xdr_stream_decode_u32(xdr, &len) < 0)
 88		return false;
 89
 90	locks_init_lock(fl);
 91	fl->c.flc_flags = FL_POSIX;
 92	fl->c.flc_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;
 97	else
 98		fl->fl_end = s32_to_loff_t(end);
 99
100	return true;
 
 
 
 
 
 
101}
102
103static bool
104svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
105{
106	const struct file_lock *fl = &lock->fl;
107	s32 start, len;
108
109	/* exclusive */
110	if (xdr_stream_encode_bool(xdr, fl->c.flc_type != F_RDLCK) < 0)
111		return false;
112	if (xdr_stream_encode_u32(xdr, lock->svid) < 0)
113		return false;
114	if (!svcxdr_encode_owner(xdr, &lock->oh))
115		return false;
116	start = loff_t_to_s32(fl->fl_start);
117	if (fl->fl_end == OFFSET_MAX)
118		len = 0;
119	else
120		len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
121	if (xdr_stream_encode_u32(xdr, start) < 0)
122		return false;
123	if (xdr_stream_encode_u32(xdr, len) < 0)
124		return false;
125
126	return true;
127}
128
129static bool
130svcxdr_encode_testrply(struct xdr_stream *xdr, const struct nlm_res *resp)
131{
132	if (!svcxdr_encode_stats(xdr, resp->status))
133		return false;
134	switch (resp->status) {
135	case nlm_lck_denied:
136		if (!svcxdr_encode_holder(xdr, &resp->lock))
137			return false;
138	}
139
140	return true;
 
 
141}
142
143
144/*
145 * Decode Call arguments
146 */
 
 
 
 
 
147
148bool
149nlmsvc_decode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
150{
151	return true;
152}
153
154bool
155nlmsvc_decode_testargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
156{
157	struct nlm_args *argp = rqstp->rq_argp;
158	u32 exclusive;
159
160	if (!svcxdr_decode_cookie(xdr, &argp->cookie))
161		return false;
162	if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
163		return false;
164	if (!svcxdr_decode_lock(xdr, &argp->lock))
165		return false;
166	if (exclusive)
167		argp->lock.fl.c.flc_type = F_WRLCK;
 
 
 
 
 
 
 
 
 
 
168
169	return true;
 
 
 
 
170}
171
172bool
173nlmsvc_decode_lockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
 
 
 
174{
175	struct nlm_args *argp = rqstp->rq_argp;
176	u32 exclusive;
177
178	if (!svcxdr_decode_cookie(xdr, &argp->cookie))
179		return false;
180	if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
181		return false;
182	if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
183		return false;
184	if (!svcxdr_decode_lock(xdr, &argp->lock))
185		return false;
186	if (exclusive)
187		argp->lock.fl.c.flc_type = F_WRLCK;
188	if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
189		return false;
190	if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
191		return false;
192	argp->monitor = 1;		/* monitor client by default */
193
194	return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195}
196
197bool
198nlmsvc_decode_cancargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
 
 
 
 
199{
200	struct nlm_args *argp = rqstp->rq_argp;
201	u32 exclusive;
 
 
202
203	if (!svcxdr_decode_cookie(xdr, &argp->cookie))
204		return false;
205	if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
206		return false;
207	if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
208		return false;
209	if (!svcxdr_decode_lock(xdr, &argp->lock))
210		return false;
211	if (exclusive)
212		argp->lock.fl.c.flc_type = F_WRLCK;
213
214	return true;
215}
216
217bool
218nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
219{
220	struct nlm_args *argp = rqstp->rq_argp;
221
222	if (!svcxdr_decode_cookie(xdr, &argp->cookie))
223		return false;
224	if (!svcxdr_decode_lock(xdr, &argp->lock))
225		return false;
226	argp->lock.fl.c.flc_type = F_UNLCK;
227
228	return true;
229}
230
231bool
232nlmsvc_decode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
233{
234	struct nlm_res *resp = rqstp->rq_argp;
235
236	if (!svcxdr_decode_cookie(xdr, &resp->cookie))
237		return false;
238	if (!svcxdr_decode_stats(xdr, &resp->status))
239		return false;
 
 
 
 
 
 
 
240
241	return true;
242}
243
244bool
245nlmsvc_decode_reboot(struct svc_rqst *rqstp, struct xdr_stream *xdr)
246{
247	struct nlm_reboot *argp = rqstp->rq_argp;
248	__be32 *p;
249	u32 len;
250
251	if (xdr_stream_decode_u32(xdr, &len) < 0)
252		return false;
253	if (len > SM_MAXSTRLEN)
254		return false;
255	p = xdr_inline_decode(xdr, len);
256	if (!p)
257		return false;
258	argp->len = len;
259	argp->mon = (char *)p;
260	if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
261		return false;
262	p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
263	if (!p)
264		return false;
265	memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
266
267	return true;
 
 
 
 
 
 
 
268}
269
270bool
271nlmsvc_decode_shareargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
272{
273	struct nlm_args *argp = rqstp->rq_argp;
274	struct nlm_lock	*lock = &argp->lock;
275
276	memset(lock, 0, sizeof(*lock));
277	locks_init_lock(&lock->fl);
278	lock->svid = ~(u32)0;
 
279
280	if (!svcxdr_decode_cookie(xdr, &argp->cookie))
281		return false;
282	if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
283		return false;
284	if (!svcxdr_decode_fhandle(xdr, &lock->fh))
285		return false;
286	if (!svcxdr_decode_owner(xdr, &lock->oh))
287		return false;
288	/* XXX: Range checks are missing in the original code */
289	if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
290		return false;
291	if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
292		return false;
293
294	return true;
295}
296
297bool
298nlmsvc_decode_notify(struct svc_rqst *rqstp, struct xdr_stream *xdr)
299{
300	struct nlm_args *argp = rqstp->rq_argp;
301	struct nlm_lock	*lock = &argp->lock;
302
303	if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
304		return false;
305	if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
306		return false;
307
308	return true;
309}
310
311
312/*
313 * Encode Reply results
314 */
315
316bool
317nlmsvc_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
318{
319	return true;
 
 
 
320}
321
322bool
323nlmsvc_encode_testres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
324{
325	struct nlm_res *resp = rqstp->rq_resp;
326
327	return svcxdr_encode_cookie(xdr, &resp->cookie) &&
328		svcxdr_encode_testrply(xdr, resp);
 
 
 
329}
330
331bool
332nlmsvc_encode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
333{
334	struct nlm_res *resp = rqstp->rq_resp;
335
336	return svcxdr_encode_cookie(xdr, &resp->cookie) &&
337		svcxdr_encode_stats(xdr, resp->status);
 
 
338}
339
340bool
341nlmsvc_encode_shareres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
342{
343	struct nlm_res *resp = rqstp->rq_resp;
 
 
 
 
344
345	if (!svcxdr_encode_cookie(xdr, &resp->cookie))
346		return false;
347	if (!svcxdr_encode_stats(xdr, resp->status))
348		return false;
349	/* sequence */
350	if (xdr_stream_encode_u32(xdr, 0) < 0)
351		return false;
352
353	return true;
 
 
 
354}
v4.6
 
  1/*
  2 * linux/fs/lockd/xdr.c
  3 *
  4 * XDR support for lockd and the lock client.
  5 *
  6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7 */
  8
  9#include <linux/types.h>
 10#include <linux/sched.h>
 11#include <linux/nfs.h>
 12
 13#include <linux/sunrpc/xdr.h>
 14#include <linux/sunrpc/clnt.h>
 15#include <linux/sunrpc/svc.h>
 16#include <linux/sunrpc/stats.h>
 17#include <linux/lockd/lockd.h>
 18
 19#include <uapi/linux/nfs2.h>
 20
 21#define NLMDBG_FACILITY		NLMDBG_XDR
 22
 23
 24static inline loff_t
 25s32_to_loff_t(__s32 offset)
 26{
 27	return (loff_t)offset;
 28}
 29
 30static inline __s32
 31loff_t_to_s32(loff_t offset)
 32{
 33	__s32 res;
 34	if (offset >= NLM_OFFSET_MAX)
 35		res = NLM_OFFSET_MAX;
 36	else if (offset <= -NLM_OFFSET_MAX)
 37		res = -NLM_OFFSET_MAX;
 38	else
 39		res = offset;
 40	return res;
 41}
 42
 43/*
 44 * XDR functions for basic NLM types
 
 
 
 45 */
 46static __be32 *nlm_decode_cookie(__be32 *p, struct nlm_cookie *c)
 
 47{
 48	unsigned int	len;
 
 49
 50	len = ntohl(*p++);
 51	
 52	if(len==0)
 53	{
 54		c->len=4;
 55		memset(c->data, 0, 4);	/* hockeypux brain damage */
 56	}
 57	else if(len<=NLM_MAXCOOKIELEN)
 58	{
 59		c->len=len;
 60		memcpy(c->data, p, len);
 61		p+=XDR_QUADLEN(len);
 62	}
 63	else 
 64	{
 65		dprintk("lockd: bad cookie size %d (only cookies under "
 66			"%d bytes are supported.)\n",
 67				len, NLM_MAXCOOKIELEN);
 68		return NULL;
 69	}
 70	return p;
 71}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 72
 73static inline __be32 *
 74nlm_encode_cookie(__be32 *p, struct nlm_cookie *c)
 75{
 76	*p++ = htonl(c->len);
 77	memcpy(p, c->data, c->len);
 78	p+=XDR_QUADLEN(c->len);
 79	return p;
 80}
 81
 82static __be32 *
 83nlm_decode_fh(__be32 *p, struct nfs_fh *f)
 84{
 85	unsigned int	len;
 
 86
 87	if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
 88		dprintk("lockd: bad fhandle size %d (should be %d)\n",
 89			len, NFS2_FHSIZE);
 90		return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 91	}
 92	f->size = NFS2_FHSIZE;
 93	memset(f->data, 0, sizeof(f->data));
 94	memcpy(f->data, p, NFS2_FHSIZE);
 95	return p + XDR_QUADLEN(NFS2_FHSIZE);
 96}
 97
 
 98/*
 99 * Encode and decode owner handle
100 */
101static inline __be32 *
102nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
103{
104	return xdr_decode_netobj(p, oh);
105}
106
107static inline __be32 *
108nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
109{
110	return xdr_encode_netobj(p, oh);
111}
112
113static __be32 *
114nlm_decode_lock(__be32 *p, struct nlm_lock *lock)
115{
116	struct file_lock	*fl = &lock->fl;
117	s32			start, len, end;
118
119	if (!(p = xdr_decode_string_inplace(p, &lock->caller,
120					    &lock->len,
121					    NLM_MAXSTRLEN))
122	 || !(p = nlm_decode_fh(p, &lock->fh))
123	 || !(p = nlm_decode_oh(p, &lock->oh)))
124		return NULL;
125	lock->svid  = ntohl(*p++);
126
127	locks_init_lock(fl);
128	fl->fl_owner = current->files;
129	fl->fl_pid   = (pid_t)lock->svid;
130	fl->fl_flags = FL_POSIX;
131	fl->fl_type  = F_RDLCK;		/* as good as anything else */
132	start = ntohl(*p++);
133	len = ntohl(*p++);
134	end = start + len - 1;
135
136	fl->fl_start = s32_to_loff_t(start);
137
138	if (len == 0 || end < 0)
139		fl->fl_end = OFFSET_MAX;
140	else
141		fl->fl_end = s32_to_loff_t(end);
142	return p;
143}
144
145/*
146 * Encode result of a TEST/TEST_MSG call
147 */
148static __be32 *
149nlm_encode_testres(__be32 *p, struct nlm_res *resp)
150{
151	s32		start, len;
 
152
153	if (!(p = nlm_encode_cookie(p, &resp->cookie)))
154		return NULL;
155	*p++ = resp->status;
156
157	if (resp->status == nlm_lck_denied) {
158		struct file_lock	*fl = &resp->lock.fl;
 
 
 
 
 
 
 
 
 
159
160		*p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
161		*p++ = htonl(resp->lock.svid);
162
163		/* Encode owner handle. */
164		if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
165			return NULL;
166
167		start = loff_t_to_s32(fl->fl_start);
168		if (fl->fl_end == OFFSET_MAX)
169			len = 0;
170		else
171			len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
172
173		*p++ = htonl(start);
174		*p++ = htonl(len);
175	}
176
177	return p;
178}
179
180
181/*
182 * First, the server side XDR functions
183 */
184int
185nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
186{
187	u32	exclusive;
188
189	if (!(p = nlm_decode_cookie(p, &argp->cookie)))
190		return 0;
191
192	exclusive = ntohl(*p++);
193	if (!(p = nlm_decode_lock(p, &argp->lock)))
194		return 0;
 
 
 
 
 
195	if (exclusive)
196		argp->lock.fl.fl_type = F_WRLCK;
197
198	return xdr_argsize_check(rqstp, p);
199}
200
201int
202nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
203{
204	if (!(p = nlm_encode_testres(p, resp)))
205		return 0;
206	return xdr_ressize_check(rqstp, p);
 
 
 
 
 
 
207}
208
209int
210nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
211{
212	u32	exclusive;
213
214	if (!(p = nlm_decode_cookie(p, &argp->cookie)))
215		return 0;
216	argp->block  = ntohl(*p++);
217	exclusive    = ntohl(*p++);
218	if (!(p = nlm_decode_lock(p, &argp->lock)))
219		return 0;
220	if (exclusive)
221		argp->lock.fl.fl_type = F_WRLCK;
222	argp->reclaim = ntohl(*p++);
223	argp->state   = ntohl(*p++);
224	argp->monitor = 1;		/* monitor client by default */
225
226	return xdr_argsize_check(rqstp, p);
227}
228
229int
230nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
231{
232	u32	exclusive;
 
 
233
234	if (!(p = nlm_decode_cookie(p, &argp->cookie)))
235		return 0;
236	argp->block = ntohl(*p++);
237	exclusive = ntohl(*p++);
238	if (!(p = nlm_decode_lock(p, &argp->lock)))
239		return 0;
240	if (exclusive)
241		argp->lock.fl.fl_type = F_WRLCK;
242	return xdr_argsize_check(rqstp, p);
243}
 
 
 
 
 
244
245int
246nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
247{
248	if (!(p = nlm_decode_cookie(p, &argp->cookie))
249	 || !(p = nlm_decode_lock(p, &argp->lock)))
250		return 0;
251	argp->lock.fl.fl_type = F_UNLCK;
252	return xdr_argsize_check(rqstp, p);
253}
254
255int
256nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
257{
 
258	struct nlm_lock	*lock = &argp->lock;
259
260	memset(lock, 0, sizeof(*lock));
261	locks_init_lock(&lock->fl);
262	lock->svid = ~(u32) 0;
263	lock->fl.fl_pid = (pid_t)lock->svid;
264
265	if (!(p = nlm_decode_cookie(p, &argp->cookie))
266	 || !(p = xdr_decode_string_inplace(p, &lock->caller,
267					    &lock->len, NLM_MAXSTRLEN))
268	 || !(p = nlm_decode_fh(p, &lock->fh))
269	 || !(p = nlm_decode_oh(p, &lock->oh)))
270		return 0;
271	argp->fsm_mode = ntohl(*p++);
272	argp->fsm_access = ntohl(*p++);
273	return xdr_argsize_check(rqstp, p);
 
 
 
 
 
 
274}
275
276int
277nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
278{
279	if (!(p = nlm_encode_cookie(p, &resp->cookie)))
280		return 0;
281	*p++ = resp->status;
282	*p++ = xdr_zero;		/* sequence argument */
283	return xdr_ressize_check(rqstp, p);
 
 
 
 
284}
285
286int
287nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
 
 
 
 
 
288{
289	if (!(p = nlm_encode_cookie(p, &resp->cookie)))
290		return 0;
291	*p++ = resp->status;
292	return xdr_ressize_check(rqstp, p);
293}
294
295int
296nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
297{
298	struct nlm_lock	*lock = &argp->lock;
299
300	if (!(p = xdr_decode_string_inplace(p, &lock->caller,
301					    &lock->len, NLM_MAXSTRLEN)))
302		return 0;
303	argp->state = ntohl(*p++);
304	return xdr_argsize_check(rqstp, p);
305}
306
307int
308nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
309{
310	if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
311		return 0;
312	argp->state = ntohl(*p++);
313	memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
314	p += XDR_QUADLEN(SM_PRIV_SIZE);
315	return xdr_argsize_check(rqstp, p);
316}
317
318int
319nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
320{
321	if (!(p = nlm_decode_cookie(p, &resp->cookie)))
322		return 0;
323	resp->status = *p++;
324	return xdr_argsize_check(rqstp, p);
325}
326
327int
328nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
329{
330	return xdr_argsize_check(rqstp, p);
331}
 
 
332
333int
334nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
335{
336	return xdr_ressize_check(rqstp, p);
337}