blob: 856267c0864bdef39fbf63963864ebf5f33f89f7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Chuck Lever79565212021-06-03 16:52:16 -040021#include "svcxdr.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static inline loff_t
24s64_to_loff_t(__s64 offset)
25{
26 return (loff_t)offset;
27}
28
29
30static inline s64
31loff_t_to_s64(loff_t offset)
32{
33 s64 res;
34 if (offset > NLM4_OFFSET_MAX)
35 res = NLM4_OFFSET_MAX;
36 else if (offset < -NLM4_OFFSET_MAX)
37 res = -NLM4_OFFSET_MAX;
38 else
39 res = offset;
40 return res;
41}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
Chuck Lever345b4152021-06-03 16:52:22 -040044 * NLM file handles are defined by specification to be a variable-length
45 * XDR opaque no longer than 1024 bytes. However, this implementation
46 * limits their length to the size of an NFSv3 file handle.
47 */
48static bool
49svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
50{
51 __be32 *p;
52 u32 len;
53
54 if (xdr_stream_decode_u32(xdr, &len) < 0)
55 return false;
56 if (len > NFS_MAXFHSIZE)
57 return false;
58
59 p = xdr_inline_decode(xdr, len);
60 if (!p)
61 return false;
62 fh->size = len;
63 memcpy(fh->data, p, len);
64 memset(fh->data + len, 0, sizeof(fh->data) - len);
65
66 return true;
67}
68
Chuck Lever345b4152021-06-03 16:52:22 -040069static bool
70svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
71{
72 struct file_lock *fl = &lock->fl;
73 u64 len, start;
74 s64 end;
75
76 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
77 return false;
78 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
79 return false;
80 if (!svcxdr_decode_owner(xdr, &lock->oh))
81 return false;
82 if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
83 return false;
84 if (xdr_stream_decode_u64(xdr, &start) < 0)
85 return false;
86 if (xdr_stream_decode_u64(xdr, &len) < 0)
87 return false;
88
89 locks_init_lock(fl);
90 fl->fl_flags = FL_POSIX;
91 fl->fl_type = F_RDLCK;
92 end = start + len - 1;
93 fl->fl_start = s64_to_loff_t(start);
94 if (len == 0 || end < 0)
95 fl->fl_end = OFFSET_MAX;
96 else
97 fl->fl_end = s64_to_loff_t(end);
98
99 return true;
100}
101
Chuck Lever1beef142021-06-03 16:53:17 -0400102static bool
103svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Chuck Lever1beef142021-06-03 16:53:17 -0400105 const struct file_lock *fl = &lock->fl;
106 s64 start, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Chuck Lever1beef142021-06-03 16:53:17 -0400108 /* exclusive */
109 if (xdr_stream_encode_bool(xdr, fl->fl_type != F_RDLCK) < 0)
110 return false;
111 if (xdr_stream_encode_u32(xdr, lock->svid) < 0)
112 return false;
113 if (!svcxdr_encode_owner(xdr, &lock->oh))
114 return false;
115 start = loff_t_to_s64(fl->fl_start);
116 if (fl->fl_end == OFFSET_MAX)
117 len = 0;
118 else
119 len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
120 if (xdr_stream_encode_u64(xdr, start) < 0)
121 return false;
122 if (xdr_stream_encode_u64(xdr, len) < 0)
123 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Chuck Lever1beef142021-06-03 16:53:17 -0400125 return true;
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Chuck Lever1beef142021-06-03 16:53:17 -0400128static bool
129svcxdr_encode_testrply(struct xdr_stream *xdr, const struct nlm_res *resp)
130{
131 if (!svcxdr_encode_stats(xdr, resp->status))
132 return false;
133 switch (resp->status) {
134 case nlm_lck_denied:
135 if (!svcxdr_encode_holder(xdr, &resp->lock))
136 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
Chuck Lever1beef142021-06-03 16:53:17 -0400139 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142
143/*
Chuck Lever79565212021-06-03 16:52:16 -0400144 * Decode Call arguments
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Chuck Lever79565212021-06-03 16:52:16 -0400146
Chuck Leverc44b31c2021-10-12 11:57:28 -0400147bool
Chuck Lever16c66362021-10-12 11:57:22 -0400148nlm4svc_decode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever79565212021-06-03 16:52:16 -0400149{
Chuck Leverc44b31c2021-10-12 11:57:28 -0400150 return true;
Chuck Lever79565212021-06-03 16:52:16 -0400151}
152
Chuck Leverc44b31c2021-10-12 11:57:28 -0400153bool
Chuck Lever16c66362021-10-12 11:57:22 -0400154nlm4svc_decode_testargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200156 struct nlm_args *argp = rqstp->rq_argp;
Chuck Lever345b4152021-06-03 16:52:22 -0400157 u32 exclusive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Chuck Lever345b4152021-06-03 16:52:22 -0400159 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400160 return false;
Chuck Lever345b4152021-06-03 16:52:22 -0400161 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400162 return false;
Chuck Lever345b4152021-06-03 16:52:22 -0400163 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400164 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (exclusive)
166 argp->lock.fl.fl_type = F_WRLCK;
167
Chuck Leverc44b31c2021-10-12 11:57:28 -0400168 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Chuck Leverc44b31c2021-10-12 11:57:28 -0400171bool
Chuck Lever16c66362021-10-12 11:57:22 -0400172nlm4svc_decode_lockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever0e5977a2021-06-03 16:52:28 -0400173{
Chuck Lever0e5977a2021-06-03 16:52:28 -0400174 struct nlm_args *argp = rqstp->rq_argp;
175 u32 exclusive;
176
177 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400178 return false;
Chuck Lever0e5977a2021-06-03 16:52:28 -0400179 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400180 return false;
Chuck Lever0e5977a2021-06-03 16:52:28 -0400181 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400182 return false;
Chuck Lever0e5977a2021-06-03 16:52:28 -0400183 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400184 return false;
Chuck Lever0e5977a2021-06-03 16:52:28 -0400185 if (exclusive)
186 argp->lock.fl.fl_type = F_WRLCK;
187 if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400188 return false;
Chuck Lever0e5977a2021-06-03 16:52:28 -0400189 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400190 return false;
Chuck Lever0e5977a2021-06-03 16:52:28 -0400191 argp->monitor = 1; /* monitor client by default */
192
Chuck Leverc44b31c2021-10-12 11:57:28 -0400193 return true;
Chuck Lever0e5977a2021-06-03 16:52:28 -0400194}
195
Chuck Leverc44b31c2021-10-12 11:57:28 -0400196bool
Chuck Lever16c66362021-10-12 11:57:22 -0400197nlm4svc_decode_cancargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever1e1f38d2021-06-03 16:52:34 -0400198{
Chuck Lever1e1f38d2021-06-03 16:52:34 -0400199 struct nlm_args *argp = rqstp->rq_argp;
200 u32 exclusive;
201
202 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400203 return false;
Chuck Lever1e1f38d2021-06-03 16:52:34 -0400204 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400205 return false;
Chuck Lever1e1f38d2021-06-03 16:52:34 -0400206 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400207 return false;
Chuck Lever1e1f38d2021-06-03 16:52:34 -0400208 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400209 return false;
Chuck Lever1e1f38d2021-06-03 16:52:34 -0400210 if (exclusive)
211 argp->lock.fl.fl_type = F_WRLCK;
Chuck Leverc44b31c2021-10-12 11:57:28 -0400212
213 return true;
Chuck Lever1e1f38d2021-06-03 16:52:34 -0400214}
215
Chuck Leverc44b31c2021-10-12 11:57:28 -0400216bool
Chuck Lever16c66362021-10-12 11:57:22 -0400217nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Leverd76d8c22021-06-03 16:52:40 -0400218{
Chuck Leverd76d8c22021-06-03 16:52:40 -0400219 struct nlm_args *argp = rqstp->rq_argp;
220
221 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400222 return false;
Chuck Leverd76d8c22021-06-03 16:52:40 -0400223 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400224 return false;
Chuck Leverd76d8c22021-06-03 16:52:40 -0400225 argp->lock.fl.fl_type = F_UNLCK;
226
Chuck Leverc44b31c2021-10-12 11:57:28 -0400227 return true;
Chuck Leverd76d8c22021-06-03 16:52:40 -0400228}
229
Chuck Leverc44b31c2021-10-12 11:57:28 -0400230bool
Chuck Lever16c66362021-10-12 11:57:22 -0400231nlm4svc_decode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Leverb4c24b52021-06-03 16:52:46 -0400232{
Chuck Leverb4c24b52021-06-03 16:52:46 -0400233 struct nlm_res *resp = rqstp->rq_argp;
234
235 if (!svcxdr_decode_cookie(xdr, &resp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400236 return false;
Chuck Leverb4c24b52021-06-03 16:52:46 -0400237 if (!svcxdr_decode_stats(xdr, &resp->status))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400238 return false;
Chuck Leverb4c24b52021-06-03 16:52:46 -0400239
Chuck Leverc44b31c2021-10-12 11:57:28 -0400240 return true;
Chuck Leverb4c24b52021-06-03 16:52:46 -0400241}
242
Chuck Leverc44b31c2021-10-12 11:57:28 -0400243bool
Chuck Lever16c66362021-10-12 11:57:22 -0400244nlm4svc_decode_reboot(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Leverbc3665f2021-06-03 16:52:52 -0400245{
Chuck Leverbc3665f2021-06-03 16:52:52 -0400246 struct nlm_reboot *argp = rqstp->rq_argp;
Chuck Lever16c66362021-10-12 11:57:22 -0400247 __be32 *p;
Chuck Leverbc3665f2021-06-03 16:52:52 -0400248 u32 len;
249
250 if (xdr_stream_decode_u32(xdr, &len) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400251 return false;
Chuck Leverbc3665f2021-06-03 16:52:52 -0400252 if (len > SM_MAXSTRLEN)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400253 return false;
Chuck Leverbc3665f2021-06-03 16:52:52 -0400254 p = xdr_inline_decode(xdr, len);
255 if (!p)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400256 return false;
Chuck Leverbc3665f2021-06-03 16:52:52 -0400257 argp->len = len;
258 argp->mon = (char *)p;
259 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400260 return false;
Chuck Leverbc3665f2021-06-03 16:52:52 -0400261 p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
262 if (!p)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400263 return false;
Chuck Leverbc3665f2021-06-03 16:52:52 -0400264 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
265
Chuck Leverc44b31c2021-10-12 11:57:28 -0400266 return true;
Chuck Leverbc3665f2021-06-03 16:52:52 -0400267}
268
Chuck Leverc44b31c2021-10-12 11:57:28 -0400269bool
Chuck Lever16c66362021-10-12 11:57:22 -0400270nlm4svc_decode_shareargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever7cf96b62021-06-03 16:52:58 -0400271{
Chuck Lever7cf96b62021-06-03 16:52:58 -0400272 struct nlm_args *argp = rqstp->rq_argp;
273 struct nlm_lock *lock = &argp->lock;
274
275 memset(lock, 0, sizeof(*lock));
276 locks_init_lock(&lock->fl);
277 lock->svid = ~(u32)0;
278
279 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400280 return false;
Chuck Lever7cf96b62021-06-03 16:52:58 -0400281 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400282 return false;
Chuck Lever7cf96b62021-06-03 16:52:58 -0400283 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400284 return false;
Chuck Lever7cf96b62021-06-03 16:52:58 -0400285 if (!svcxdr_decode_owner(xdr, &lock->oh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400286 return false;
Chuck Lever7cf96b62021-06-03 16:52:58 -0400287 /* XXX: Range checks are missing in the original code */
288 if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400289 return false;
Chuck Lever7cf96b62021-06-03 16:52:58 -0400290 if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400291 return false;
Chuck Lever7cf96b62021-06-03 16:52:58 -0400292
Chuck Leverc44b31c2021-10-12 11:57:28 -0400293 return true;
Chuck Lever7cf96b62021-06-03 16:52:58 -0400294}
295
Chuck Leverc44b31c2021-10-12 11:57:28 -0400296bool
Chuck Lever16c66362021-10-12 11:57:22 -0400297nlm4svc_decode_notify(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever3049e972021-06-03 16:53:04 -0400298{
Chuck Lever3049e972021-06-03 16:53:04 -0400299 struct nlm_args *argp = rqstp->rq_argp;
300 struct nlm_lock *lock = &argp->lock;
301
302 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400303 return false;
Chuck Lever3049e972021-06-03 16:53:04 -0400304 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400305 return false;
Chuck Lever3049e972021-06-03 16:53:04 -0400306
Chuck Leverc44b31c2021-10-12 11:57:28 -0400307 return true;
Chuck Lever3049e972021-06-03 16:53:04 -0400308}
309
Chuck Leverec757e42021-06-03 16:53:11 -0400310
311/*
312 * Encode Reply results
313 */
314
Chuck Lever130e2052021-10-13 10:41:13 -0400315bool
Chuck Leverfda49442021-10-13 10:41:06 -0400316nlm4svc_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Leverec757e42021-06-03 16:53:11 -0400317{
Chuck Lever130e2052021-10-13 10:41:13 -0400318 return true;
Chuck Leverec757e42021-06-03 16:53:11 -0400319}
320
Chuck Lever130e2052021-10-13 10:41:13 -0400321bool
Chuck Leverfda49442021-10-13 10:41:06 -0400322nlm4svc_encode_testres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200324 struct nlm_res *resp = rqstp->rq_resp;
325
Chuck Lever1beef142021-06-03 16:53:17 -0400326 return svcxdr_encode_cookie(xdr, &resp->cookie) &&
327 svcxdr_encode_testrply(xdr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Chuck Lever130e2052021-10-13 10:41:13 -0400330bool
Chuck Leverfda49442021-10-13 10:41:06 -0400331nlm4svc_encode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever447c14d2021-06-03 16:53:23 -0400332{
Chuck Lever447c14d2021-06-03 16:53:23 -0400333 struct nlm_res *resp = rqstp->rq_resp;
334
335 return svcxdr_encode_cookie(xdr, &resp->cookie) &&
336 svcxdr_encode_stats(xdr, resp->status);
337}
338
Chuck Lever130e2052021-10-13 10:41:13 -0400339bool
Chuck Leverfda49442021-10-13 10:41:06 -0400340nlm4svc_encode_shareres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200342 struct nlm_res *resp = rqstp->rq_resp;
343
Chuck Lever0ff5b502021-06-03 16:53:29 -0400344 if (!svcxdr_encode_cookie(xdr, &resp->cookie))
Chuck Lever130e2052021-10-13 10:41:13 -0400345 return false;
Chuck Lever0ff5b502021-06-03 16:53:29 -0400346 if (!svcxdr_encode_stats(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400347 return false;
Chuck Lever0ff5b502021-06-03 16:53:29 -0400348 /* sequence */
349 if (xdr_stream_encode_u32(xdr, 0) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400350 return false;
Chuck Lever0ff5b502021-06-03 16:53:29 -0400351
Chuck Lever130e2052021-10-13 10:41:13 -0400352 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}