blob: 2fb5748dae0c808bfc55f1435cf6e79350357cd9 [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/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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/types.h>
11#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Christoph Hellwig9c69de42014-05-06 19:37:13 +020020#include <uapi/linux/nfs2.h>
21
Chuck Levercc1029b2021-06-03 16:50:58 -040022#include "svcxdr.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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/*
Chuck Lever2fd0c672021-06-03 16:51:04 -040045 * 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
Chuck Lever2fd0c672021-06-03 16:51:04 -040071static 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->fl_flags = FL_POSIX;
92 fl->fl_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
Chuck Leveradf98a42021-06-03 16:51:58 -0400103static bool
104svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Chuck Leveradf98a42021-06-03 16:51:58 -0400106 const struct file_lock *fl = &lock->fl;
107 s32 start, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Chuck Leveradf98a42021-06-03 16:51:58 -0400109 /* exclusive */
110 if (xdr_stream_encode_bool(xdr, fl->fl_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Chuck Leveradf98a42021-06-03 16:51:58 -0400126 return true;
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Chuck Leveradf98a42021-06-03 16:51:58 -0400129static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Chuck Leveradf98a42021-06-03 16:51:58 -0400140 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143
144/*
Chuck Levercc1029b2021-06-03 16:50:58 -0400145 * Decode Call arguments
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
Chuck Levercc1029b2021-06-03 16:50:58 -0400147
Chuck Leverc44b31c2021-10-12 11:57:28 -0400148bool
Chuck Lever16c66362021-10-12 11:57:22 -0400149nlmsvc_decode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Levercc1029b2021-06-03 16:50:58 -0400150{
Chuck Leverc44b31c2021-10-12 11:57:28 -0400151 return true;
Chuck Levercc1029b2021-06-03 16:50:58 -0400152}
153
Chuck Leverc44b31c2021-10-12 11:57:28 -0400154bool
Chuck Lever16c66362021-10-12 11:57:22 -0400155nlmsvc_decode_testargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200157 struct nlm_args *argp = rqstp->rq_argp;
Chuck Lever2fd0c672021-06-03 16:51:04 -0400158 u32 exclusive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Chuck Lever2fd0c672021-06-03 16:51:04 -0400160 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400161 return false;
Chuck Lever2fd0c672021-06-03 16:51:04 -0400162 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400163 return false;
Chuck Lever2fd0c672021-06-03 16:51:04 -0400164 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400165 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (exclusive)
167 argp->lock.fl.fl_type = F_WRLCK;
168
Chuck Leverc44b31c2021-10-12 11:57:28 -0400169 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Chuck Leverc44b31c2021-10-12 11:57:28 -0400172bool
Chuck Lever16c66362021-10-12 11:57:22 -0400173nlmsvc_decode_lockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400174{
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400175 struct nlm_args *argp = rqstp->rq_argp;
176 u32 exclusive;
177
178 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400179 return false;
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400180 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400181 return false;
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400182 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400183 return false;
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400184 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400185 return false;
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400186 if (exclusive)
187 argp->lock.fl.fl_type = F_WRLCK;
188 if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400189 return false;
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400190 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400191 return false;
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400192 argp->monitor = 1; /* monitor client by default */
193
Chuck Leverc44b31c2021-10-12 11:57:28 -0400194 return true;
Chuck Leverc1adb8c2021-06-03 16:51:10 -0400195}
196
Chuck Leverc44b31c2021-10-12 11:57:28 -0400197bool
Chuck Lever16c66362021-10-12 11:57:22 -0400198nlmsvc_decode_cancargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Leverf4e08f32021-06-03 16:51:16 -0400199{
Chuck Leverf4e08f32021-06-03 16:51:16 -0400200 struct nlm_args *argp = rqstp->rq_argp;
201 u32 exclusive;
202
203 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400204 return false;
Chuck Leverf4e08f32021-06-03 16:51:16 -0400205 if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400206 return false;
Chuck Leverf4e08f32021-06-03 16:51:16 -0400207 if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400208 return false;
Chuck Leverf4e08f32021-06-03 16:51:16 -0400209 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400210 return false;
Chuck Leverf4e08f32021-06-03 16:51:16 -0400211 if (exclusive)
212 argp->lock.fl.fl_type = F_WRLCK;
213
Chuck Leverc44b31c2021-10-12 11:57:28 -0400214 return true;
Chuck Leverf4e08f32021-06-03 16:51:16 -0400215}
216
Chuck Leverc44b31c2021-10-12 11:57:28 -0400217bool
Chuck Lever16c66362021-10-12 11:57:22 -0400218nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Leverc27045d2021-06-03 16:51:22 -0400219{
Chuck Leverc27045d2021-06-03 16:51:22 -0400220 struct nlm_args *argp = rqstp->rq_argp;
221
222 if (!svcxdr_decode_cookie(xdr, &argp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400223 return false;
Chuck Leverc27045d2021-06-03 16:51:22 -0400224 if (!svcxdr_decode_lock(xdr, &argp->lock))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400225 return false;
Chuck Leverc27045d2021-06-03 16:51:22 -0400226 argp->lock.fl.fl_type = F_UNLCK;
227
Chuck Leverc44b31c2021-10-12 11:57:28 -0400228 return true;
Chuck Leverc27045d2021-06-03 16:51:22 -0400229}
230
Chuck Leverc44b31c2021-10-12 11:57:28 -0400231bool
Chuck Lever16c66362021-10-12 11:57:22 -0400232nlmsvc_decode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever16ddcab2021-06-03 16:51:28 -0400233{
Chuck Lever16ddcab2021-06-03 16:51:28 -0400234 struct nlm_res *resp = rqstp->rq_argp;
235
236 if (!svcxdr_decode_cookie(xdr, &resp->cookie))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400237 return false;
Chuck Lever16ddcab2021-06-03 16:51:28 -0400238 if (!svcxdr_decode_stats(xdr, &resp->status))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400239 return false;
Chuck Lever16ddcab2021-06-03 16:51:28 -0400240
Chuck Leverc44b31c2021-10-12 11:57:28 -0400241 return true;
Chuck Lever16ddcab2021-06-03 16:51:28 -0400242}
243
Chuck Leverc44b31c2021-10-12 11:57:28 -0400244bool
Chuck Lever16c66362021-10-12 11:57:22 -0400245nlmsvc_decode_reboot(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever137e05e2021-06-03 16:51:34 -0400246{
Chuck Lever137e05e2021-06-03 16:51:34 -0400247 struct nlm_reboot *argp = rqstp->rq_argp;
Chuck Lever16c66362021-10-12 11:57:22 -0400248 __be32 *p;
Chuck Lever137e05e2021-06-03 16:51:34 -0400249 u32 len;
250
251 if (xdr_stream_decode_u32(xdr, &len) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400252 return false;
Chuck Lever137e05e2021-06-03 16:51:34 -0400253 if (len > SM_MAXSTRLEN)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400254 return false;
Chuck Lever137e05e2021-06-03 16:51:34 -0400255 p = xdr_inline_decode(xdr, len);
256 if (!p)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400257 return false;
Chuck Lever137e05e2021-06-03 16:51:34 -0400258 argp->len = len;
259 argp->mon = (char *)p;
260 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400261 return false;
Chuck Lever137e05e2021-06-03 16:51:34 -0400262 p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
263 if (!p)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400264 return false;
Chuck Lever137e05e2021-06-03 16:51:34 -0400265 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
266
Chuck Leverc44b31c2021-10-12 11:57:28 -0400267 return true;
Chuck Lever137e05e2021-06-03 16:51:34 -0400268}
269
Chuck Leverc44b31c2021-10-12 11:57:28 -0400270bool
Chuck Lever16c66362021-10-12 11:57:22 -0400271nlmsvc_decode_shareargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever890939e2021-06-03 16:51:40 -0400272{
Chuck Lever890939e2021-06-03 16:51:40 -0400273 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))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400281 return false;
Chuck Lever890939e2021-06-03 16:51:40 -0400282 if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400283 return false;
Chuck Lever890939e2021-06-03 16:51:40 -0400284 if (!svcxdr_decode_fhandle(xdr, &lock->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400285 return false;
Chuck Lever890939e2021-06-03 16:51:40 -0400286 if (!svcxdr_decode_owner(xdr, &lock->oh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400287 return false;
Chuck Lever890939e2021-06-03 16:51:40 -0400288 /* XXX: Range checks are missing in the original code */
289 if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400290 return false;
Chuck Lever890939e2021-06-03 16:51:40 -0400291 if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400292 return false;
Chuck Lever890939e2021-06-03 16:51:40 -0400293
Chuck Leverc44b31c2021-10-12 11:57:28 -0400294 return true;
Chuck Lever890939e2021-06-03 16:51:40 -0400295}
296
Chuck Leverc44b31c2021-10-12 11:57:28 -0400297bool
Chuck Lever16c66362021-10-12 11:57:22 -0400298nlmsvc_decode_notify(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Lever14e10522021-06-03 16:51:46 -0400299{
Chuck Lever14e10522021-06-03 16:51:46 -0400300 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))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400304 return false;
Chuck Lever14e10522021-06-03 16:51:46 -0400305 if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400306 return false;
Chuck Lever14e10522021-06-03 16:51:46 -0400307
Chuck Leverc44b31c2021-10-12 11:57:28 -0400308 return true;
Chuck Lever14e10522021-06-03 16:51:46 -0400309}
310
Chuck Levere26ec892021-06-03 16:51:52 -0400311
312/*
313 * Encode Reply results
314 */
315
Chuck Lever130e2052021-10-13 10:41:13 -0400316bool
Chuck Leverfda49442021-10-13 10:41:06 -0400317nlmsvc_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Levere26ec892021-06-03 16:51:52 -0400318{
Chuck Lever130e2052021-10-13 10:41:13 -0400319 return true;
Chuck Levere26ec892021-06-03 16:51:52 -0400320}
321
Chuck Lever130e2052021-10-13 10:41:13 -0400322bool
Chuck Leverfda49442021-10-13 10:41:06 -0400323nlmsvc_encode_testres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200325 struct nlm_res *resp = rqstp->rq_resp;
326
Chuck Leveradf98a42021-06-03 16:51:58 -0400327 return svcxdr_encode_cookie(xdr, &resp->cookie) &&
328 svcxdr_encode_testrply(xdr, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Chuck Lever130e2052021-10-13 10:41:13 -0400331bool
Chuck Leverfda49442021-10-13 10:41:06 -0400332nlmsvc_encode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Chuck Levere96735a2021-06-03 16:52:04 -0400333{
Chuck Levere96735a2021-06-03 16:52:04 -0400334 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
Chuck Lever130e2052021-10-13 10:41:13 -0400340bool
Chuck Leverfda49442021-10-13 10:41:06 -0400341nlmsvc_encode_shareres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200343 struct nlm_res *resp = rqstp->rq_resp;
344
Chuck Lever529ca3a2021-06-03 16:52:10 -0400345 if (!svcxdr_encode_cookie(xdr, &resp->cookie))
Chuck Lever130e2052021-10-13 10:41:13 -0400346 return false;
Chuck Lever529ca3a2021-06-03 16:52:10 -0400347 if (!svcxdr_encode_stats(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400348 return false;
Chuck Lever529ca3a2021-06-03 16:52:10 -0400349 /* sequence */
350 if (xdr_stream_encode_u32(xdr, 0) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400351 return false;
Chuck Lever529ca3a2021-06-03 16:52:10 -0400352
Chuck Lever130e2052021-10-13 10:41:13 -0400353 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}