1 // SPDX-License-Identifier: GPL-2.0
5 * XDR support for lockd and the lock client.
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/nfs.h>
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>
20 #include <uapi/linux/nfs2.h>
22 #define NLMDBG_FACILITY NLMDBG_XDR
26 s32_to_loff_t(__s32 offset
)
28 return (loff_t
)offset
;
32 loff_t_to_s32(loff_t offset
)
35 if (offset
>= NLM_OFFSET_MAX
)
37 else if (offset
<= -NLM_OFFSET_MAX
)
38 res
= -NLM_OFFSET_MAX
;
45 * XDR functions for basic NLM types
47 static __be32
*nlm_decode_cookie(__be32
*p
, struct nlm_cookie
*c
)
56 memset(c
->data
, 0, 4); /* hockeypux brain damage */
58 else if(len
<=NLM_MAXCOOKIELEN
)
61 memcpy(c
->data
, p
, len
);
66 dprintk("lockd: bad cookie size %d (only cookies under "
67 "%d bytes are supported.)\n",
68 len
, NLM_MAXCOOKIELEN
);
74 static inline __be32
*
75 nlm_encode_cookie(__be32
*p
, struct nlm_cookie
*c
)
78 memcpy(p
, c
->data
, c
->len
);
79 p
+=XDR_QUADLEN(c
->len
);
84 nlm_decode_fh(__be32
*p
, struct nfs_fh
*f
)
88 if ((len
= ntohl(*p
++)) != NFS2_FHSIZE
) {
89 dprintk("lockd: bad fhandle size %d (should be %d)\n",
93 f
->size
= NFS2_FHSIZE
;
94 memset(f
->data
, 0, sizeof(f
->data
));
95 memcpy(f
->data
, p
, NFS2_FHSIZE
);
96 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
100 * Encode and decode owner handle
102 static inline __be32
*
103 nlm_decode_oh(__be32
*p
, struct xdr_netobj
*oh
)
105 return xdr_decode_netobj(p
, oh
);
108 static inline __be32
*
109 nlm_encode_oh(__be32
*p
, struct xdr_netobj
*oh
)
111 return xdr_encode_netobj(p
, oh
);
115 nlm_decode_lock(__be32
*p
, struct nlm_lock
*lock
)
117 struct file_lock
*fl
= &lock
->fl
;
120 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
123 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
124 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
126 lock
->svid
= ntohl(*p
++);
129 fl
->fl_owner
= current
->files
;
130 fl
->fl_pid
= (pid_t
)lock
->svid
;
131 fl
->fl_flags
= FL_POSIX
;
132 fl
->fl_type
= F_RDLCK
; /* as good as anything else */
135 end
= start
+ len
- 1;
137 fl
->fl_start
= s32_to_loff_t(start
);
139 if (len
== 0 || end
< 0)
140 fl
->fl_end
= OFFSET_MAX
;
142 fl
->fl_end
= s32_to_loff_t(end
);
147 * Encode result of a TEST/TEST_MSG call
150 nlm_encode_testres(__be32
*p
, struct nlm_res
*resp
)
154 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
158 if (resp
->status
== nlm_lck_denied
) {
159 struct file_lock
*fl
= &resp
->lock
.fl
;
161 *p
++ = (fl
->fl_type
== F_RDLCK
)? xdr_zero
: xdr_one
;
162 *p
++ = htonl(resp
->lock
.svid
);
164 /* Encode owner handle. */
165 if (!(p
= xdr_encode_netobj(p
, &resp
->lock
.oh
)))
168 start
= loff_t_to_s32(fl
->fl_start
);
169 if (fl
->fl_end
== OFFSET_MAX
)
172 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
183 * First, the server side XDR functions
186 nlmsvc_decode_testargs(struct svc_rqst
*rqstp
, __be32
*p
)
188 struct nlm_args
*argp
= rqstp
->rq_argp
;
191 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
194 exclusive
= ntohl(*p
++);
195 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
198 argp
->lock
.fl
.fl_type
= F_WRLCK
;
200 return xdr_argsize_check(rqstp
, p
);
204 nlmsvc_encode_testres(struct svc_rqst
*rqstp
, __be32
*p
)
206 struct nlm_res
*resp
= rqstp
->rq_resp
;
208 if (!(p
= nlm_encode_testres(p
, resp
)))
210 return xdr_ressize_check(rqstp
, p
);
214 nlmsvc_decode_lockargs(struct svc_rqst
*rqstp
, __be32
*p
)
216 struct nlm_args
*argp
= rqstp
->rq_argp
;
219 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
221 argp
->block
= ntohl(*p
++);
222 exclusive
= ntohl(*p
++);
223 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
226 argp
->lock
.fl
.fl_type
= F_WRLCK
;
227 argp
->reclaim
= ntohl(*p
++);
228 argp
->state
= ntohl(*p
++);
229 argp
->monitor
= 1; /* monitor client by default */
231 return xdr_argsize_check(rqstp
, p
);
235 nlmsvc_decode_cancargs(struct svc_rqst
*rqstp
, __be32
*p
)
237 struct nlm_args
*argp
= rqstp
->rq_argp
;
240 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
242 argp
->block
= ntohl(*p
++);
243 exclusive
= ntohl(*p
++);
244 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
247 argp
->lock
.fl
.fl_type
= F_WRLCK
;
248 return xdr_argsize_check(rqstp
, p
);
252 nlmsvc_decode_unlockargs(struct svc_rqst
*rqstp
, __be32
*p
)
254 struct nlm_args
*argp
= rqstp
->rq_argp
;
256 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
257 || !(p
= nlm_decode_lock(p
, &argp
->lock
)))
259 argp
->lock
.fl
.fl_type
= F_UNLCK
;
260 return xdr_argsize_check(rqstp
, p
);
264 nlmsvc_decode_shareargs(struct svc_rqst
*rqstp
, __be32
*p
)
266 struct nlm_args
*argp
= rqstp
->rq_argp
;
267 struct nlm_lock
*lock
= &argp
->lock
;
269 memset(lock
, 0, sizeof(*lock
));
270 locks_init_lock(&lock
->fl
);
271 lock
->svid
= ~(u32
) 0;
272 lock
->fl
.fl_pid
= (pid_t
)lock
->svid
;
274 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
275 || !(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
276 &lock
->len
, NLM_MAXSTRLEN
))
277 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
278 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
280 argp
->fsm_mode
= ntohl(*p
++);
281 argp
->fsm_access
= ntohl(*p
++);
282 return xdr_argsize_check(rqstp
, p
);
286 nlmsvc_encode_shareres(struct svc_rqst
*rqstp
, __be32
*p
)
288 struct nlm_res
*resp
= rqstp
->rq_resp
;
290 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
293 *p
++ = xdr_zero
; /* sequence argument */
294 return xdr_ressize_check(rqstp
, p
);
298 nlmsvc_encode_res(struct svc_rqst
*rqstp
, __be32
*p
)
300 struct nlm_res
*resp
= rqstp
->rq_resp
;
302 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
305 return xdr_ressize_check(rqstp
, p
);
309 nlmsvc_decode_notify(struct svc_rqst
*rqstp
, __be32
*p
)
311 struct nlm_args
*argp
= rqstp
->rq_argp
;
312 struct nlm_lock
*lock
= &argp
->lock
;
314 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
315 &lock
->len
, NLM_MAXSTRLEN
)))
317 argp
->state
= ntohl(*p
++);
318 return xdr_argsize_check(rqstp
, p
);
322 nlmsvc_decode_reboot(struct svc_rqst
*rqstp
, __be32
*p
)
324 struct nlm_reboot
*argp
= rqstp
->rq_argp
;
326 if (!(p
= xdr_decode_string_inplace(p
, &argp
->mon
, &argp
->len
, SM_MAXSTRLEN
)))
328 argp
->state
= ntohl(*p
++);
329 memcpy(&argp
->priv
.data
, p
, sizeof(argp
->priv
.data
));
330 p
+= XDR_QUADLEN(SM_PRIV_SIZE
);
331 return xdr_argsize_check(rqstp
, p
);
335 nlmsvc_decode_res(struct svc_rqst
*rqstp
, __be32
*p
)
337 struct nlm_res
*resp
= rqstp
->rq_argp
;
339 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
342 return xdr_argsize_check(rqstp
, p
);
346 nlmsvc_decode_void(struct svc_rqst
*rqstp
, __be32
*p
)
348 return xdr_argsize_check(rqstp
, p
);
352 nlmsvc_encode_void(struct svc_rqst
*rqstp
, __be32
*p
)
354 return xdr_ressize_check(rqstp
, p
);