GNU Linux-libre 5.15.137-gnu
[releases.git] / fs / lockd / xdr4.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/lockd/xdr4.c
4  *
5  * XDR support for lockd and the lock client.
6  *
7  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8  * Copyright (C) 1999, Trond Myklebust <trond.myklebust@fys.uio.no>
9  */
10
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/nfs.h>
14
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>
20
21 #include "svcxdr.h"
22
23 static inline s64
24 loff_t_to_s64(loff_t offset)
25 {
26         s64 res;
27         if (offset > NLM4_OFFSET_MAX)
28                 res = NLM4_OFFSET_MAX;
29         else if (offset < -NLM4_OFFSET_MAX)
30                 res = -NLM4_OFFSET_MAX;
31         else
32                 res = offset;
33         return res;
34 }
35
36 void nlm4svc_set_file_lock_range(struct file_lock *fl, u64 off, u64 len)
37 {
38         s64 end = off + len - 1;
39
40         fl->fl_start = off;
41         if (len == 0 || end < 0)
42                 fl->fl_end = OFFSET_MAX;
43         else
44                 fl->fl_end = end;
45 }
46
47 /*
48  * NLM file handles are defined by specification to be a variable-length
49  * XDR opaque no longer than 1024 bytes. However, this implementation
50  * limits their length to the size of an NFSv3 file handle.
51  */
52 static bool
53 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
54 {
55         __be32 *p;
56         u32 len;
57
58         if (xdr_stream_decode_u32(xdr, &len) < 0)
59                 return false;
60         if (len > NFS_MAXFHSIZE)
61                 return false;
62
63         p = xdr_inline_decode(xdr, len);
64         if (!p)
65                 return false;
66         fh->size = len;
67         memcpy(fh->data, p, len);
68         memset(fh->data + len, 0, sizeof(fh->data) - len);
69
70         return true;
71 }
72
73 static bool
74 svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
75 {
76         struct file_lock *fl = &lock->fl;
77
78         if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
79                 return false;
80         if (!svcxdr_decode_fhandle(xdr, &lock->fh))
81                 return false;
82         if (!svcxdr_decode_owner(xdr, &lock->oh))
83                 return false;
84         if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
85                 return false;
86         if (xdr_stream_decode_u64(xdr, &lock->lock_start) < 0)
87                 return false;
88         if (xdr_stream_decode_u64(xdr, &lock->lock_len) < 0)
89                 return false;
90
91         locks_init_lock(fl);
92         fl->fl_flags = FL_POSIX;
93         fl->fl_type  = F_RDLCK;
94         nlm4svc_set_file_lock_range(fl, lock->lock_start, lock->lock_len);
95         return true;
96 }
97
98 static bool
99 svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
100 {
101         const struct file_lock *fl = &lock->fl;
102         s64 start, len;
103
104         /* exclusive */
105         if (xdr_stream_encode_bool(xdr, fl->fl_type != F_RDLCK) < 0)
106                 return false;
107         if (xdr_stream_encode_u32(xdr, lock->svid) < 0)
108                 return false;
109         if (!svcxdr_encode_owner(xdr, &lock->oh))
110                 return false;
111         start = loff_t_to_s64(fl->fl_start);
112         if (fl->fl_end == OFFSET_MAX)
113                 len = 0;
114         else
115                 len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
116         if (xdr_stream_encode_u64(xdr, start) < 0)
117                 return false;
118         if (xdr_stream_encode_u64(xdr, len) < 0)
119                 return false;
120
121         return true;
122 }
123
124 static bool
125 svcxdr_encode_testrply(struct xdr_stream *xdr, const struct nlm_res *resp)
126 {
127         if (!svcxdr_encode_stats(xdr, resp->status))
128                 return false;
129         switch (resp->status) {
130         case nlm_lck_denied:
131                 if (!svcxdr_encode_holder(xdr, &resp->lock))
132                         return false;
133         }
134
135         return true;
136 }
137
138
139 /*
140  * Decode Call arguments
141  */
142
143 int
144 nlm4svc_decode_void(struct svc_rqst *rqstp, __be32 *p)
145 {
146         return 1;
147 }
148
149 int
150 nlm4svc_decode_testargs(struct svc_rqst *rqstp, __be32 *p)
151 {
152         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
153         struct nlm_args *argp = rqstp->rq_argp;
154         u32 exclusive;
155
156         if (!svcxdr_decode_cookie(xdr, &argp->cookie))
157                 return 0;
158         if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
159                 return 0;
160         if (!svcxdr_decode_lock(xdr, &argp->lock))
161                 return 0;
162         if (exclusive)
163                 argp->lock.fl.fl_type = F_WRLCK;
164
165         return 1;
166 }
167
168 int
169 nlm4svc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p)
170 {
171         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
172         struct nlm_args *argp = rqstp->rq_argp;
173         u32 exclusive;
174
175         if (!svcxdr_decode_cookie(xdr, &argp->cookie))
176                 return 0;
177         if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
178                 return 0;
179         if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
180                 return 0;
181         if (!svcxdr_decode_lock(xdr, &argp->lock))
182                 return 0;
183         if (exclusive)
184                 argp->lock.fl.fl_type = F_WRLCK;
185         if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
186                 return 0;
187         if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
188                 return 0;
189         argp->monitor = 1;              /* monitor client by default */
190
191         return 1;
192 }
193
194 int
195 nlm4svc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p)
196 {
197         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
198         struct nlm_args *argp = rqstp->rq_argp;
199         u32 exclusive;
200
201         if (!svcxdr_decode_cookie(xdr, &argp->cookie))
202                 return 0;
203         if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
204                 return 0;
205         if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
206                 return 0;
207         if (!svcxdr_decode_lock(xdr, &argp->lock))
208                 return 0;
209         if (exclusive)
210                 argp->lock.fl.fl_type = F_WRLCK;
211         return 1;
212 }
213
214 int
215 nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p)
216 {
217         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
218         struct nlm_args *argp = rqstp->rq_argp;
219
220         if (!svcxdr_decode_cookie(xdr, &argp->cookie))
221                 return 0;
222         if (!svcxdr_decode_lock(xdr, &argp->lock))
223                 return 0;
224         argp->lock.fl.fl_type = F_UNLCK;
225
226         return 1;
227 }
228
229 int
230 nlm4svc_decode_res(struct svc_rqst *rqstp, __be32 *p)
231 {
232         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
233         struct nlm_res *resp = rqstp->rq_argp;
234
235         if (!svcxdr_decode_cookie(xdr, &resp->cookie))
236                 return 0;
237         if (!svcxdr_decode_stats(xdr, &resp->status))
238                 return 0;
239
240         return 1;
241 }
242
243 int
244 nlm4svc_decode_reboot(struct svc_rqst *rqstp, __be32 *p)
245 {
246         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
247         struct nlm_reboot *argp = rqstp->rq_argp;
248         u32 len;
249
250         if (xdr_stream_decode_u32(xdr, &len) < 0)
251                 return 0;
252         if (len > SM_MAXSTRLEN)
253                 return 0;
254         p = xdr_inline_decode(xdr, len);
255         if (!p)
256                 return 0;
257         argp->len = len;
258         argp->mon = (char *)p;
259         if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
260                 return 0;
261         p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
262         if (!p)
263                 return 0;
264         memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
265
266         return 1;
267 }
268
269 int
270 nlm4svc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p)
271 {
272         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
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 0;
282         if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
283                 return 0;
284         if (!svcxdr_decode_fhandle(xdr, &lock->fh))
285                 return 0;
286         if (!svcxdr_decode_owner(xdr, &lock->oh))
287                 return 0;
288         /* XXX: Range checks are missing in the original code */
289         if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
290                 return 0;
291         if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
292                 return 0;
293
294         return 1;
295 }
296
297 int
298 nlm4svc_decode_notify(struct svc_rqst *rqstp, __be32 *p)
299 {
300         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
301         struct nlm_args *argp = rqstp->rq_argp;
302         struct nlm_lock *lock = &argp->lock;
303
304         if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
305                 return 0;
306         if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
307                 return 0;
308
309         return 1;
310 }
311
312
313 /*
314  * Encode Reply results
315  */
316
317 int
318 nlm4svc_encode_void(struct svc_rqst *rqstp, __be32 *p)
319 {
320         return 1;
321 }
322
323 int
324 nlm4svc_encode_testres(struct svc_rqst *rqstp, __be32 *p)
325 {
326         struct xdr_stream *xdr = &rqstp->rq_res_stream;
327         struct nlm_res *resp = rqstp->rq_resp;
328
329         return svcxdr_encode_cookie(xdr, &resp->cookie) &&
330                 svcxdr_encode_testrply(xdr, resp);
331 }
332
333 int
334 nlm4svc_encode_res(struct svc_rqst *rqstp, __be32 *p)
335 {
336         struct xdr_stream *xdr = &rqstp->rq_res_stream;
337         struct nlm_res *resp = rqstp->rq_resp;
338
339         return svcxdr_encode_cookie(xdr, &resp->cookie) &&
340                 svcxdr_encode_stats(xdr, resp->status);
341 }
342
343 int
344 nlm4svc_encode_shareres(struct svc_rqst *rqstp, __be32 *p)
345 {
346         struct xdr_stream *xdr = &rqstp->rq_res_stream;
347         struct nlm_res *resp = rqstp->rq_resp;
348
349         if (!svcxdr_encode_cookie(xdr, &resp->cookie))
350                 return 0;
351         if (!svcxdr_encode_stats(xdr, resp->status))
352                 return 0;
353         /* sequence */
354         if (xdr_stream_encode_u32(xdr, 0) < 0)
355                 return 0;
356
357         return 1;
358 }