4 * XDR support for lockd and the lock client.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/nfs.h>
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>
19 #define NLMDBG_FACILITY NLMDBG_XDR
23 s32_to_loff_t(__s32 offset
)
25 return (loff_t
)offset
;
29 loff_t_to_s32(loff_t offset
)
32 if (offset
>= NLM_OFFSET_MAX
)
34 else if (offset
<= -NLM_OFFSET_MAX
)
35 res
= -NLM_OFFSET_MAX
;
42 * XDR functions for basic NLM types
44 static __be32
*nlm_decode_cookie(__be32
*p
, struct nlm_cookie
*c
)
53 memset(c
->data
, 0, 4); /* hockeypux brain damage */
55 else if(len
<=NLM_MAXCOOKIELEN
)
58 memcpy(c
->data
, p
, len
);
63 dprintk("lockd: bad cookie size %d (only cookies under "
64 "%d bytes are supported.)\n",
65 len
, NLM_MAXCOOKIELEN
);
71 static inline __be32
*
72 nlm_encode_cookie(__be32
*p
, struct nlm_cookie
*c
)
75 memcpy(p
, c
->data
, c
->len
);
76 p
+=XDR_QUADLEN(c
->len
);
81 nlm_decode_fh(__be32
*p
, struct nfs_fh
*f
)
85 if ((len
= ntohl(*p
++)) != NFS2_FHSIZE
) {
86 dprintk("lockd: bad fhandle size %d (should be %d)\n",
90 f
->size
= NFS2_FHSIZE
;
91 memset(f
->data
, 0, sizeof(f
->data
));
92 memcpy(f
->data
, p
, NFS2_FHSIZE
);
93 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
96 static inline __be32
*
97 nlm_encode_fh(__be32
*p
, struct nfs_fh
*f
)
99 *p
++ = htonl(NFS2_FHSIZE
);
100 memcpy(p
, f
->data
, NFS2_FHSIZE
);
101 return p
+ XDR_QUADLEN(NFS2_FHSIZE
);
105 * Encode and decode owner handle
107 static inline __be32
*
108 nlm_decode_oh(__be32
*p
, struct xdr_netobj
*oh
)
110 return xdr_decode_netobj(p
, oh
);
113 static inline __be32
*
114 nlm_encode_oh(__be32
*p
, struct xdr_netobj
*oh
)
116 return xdr_encode_netobj(p
, oh
);
120 nlm_decode_lock(__be32
*p
, struct nlm_lock
*lock
)
122 struct file_lock
*fl
= &lock
->fl
;
125 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
128 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
129 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
131 lock
->svid
= ntohl(*p
++);
134 fl
->fl_owner
= current
->files
;
135 fl
->fl_pid
= (pid_t
)lock
->svid
;
136 fl
->fl_flags
= FL_POSIX
;
137 fl
->fl_type
= F_RDLCK
; /* as good as anything else */
140 end
= start
+ len
- 1;
142 fl
->fl_start
= s32_to_loff_t(start
);
144 if (len
== 0 || end
< 0)
145 fl
->fl_end
= OFFSET_MAX
;
147 fl
->fl_end
= s32_to_loff_t(end
);
152 * Encode result of a TEST/TEST_MSG call
155 nlm_encode_testres(__be32
*p
, struct nlm_res
*resp
)
159 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
163 if (resp
->status
== nlm_lck_denied
) {
164 struct file_lock
*fl
= &resp
->lock
.fl
;
166 *p
++ = (fl
->fl_type
== F_RDLCK
)? xdr_zero
: xdr_one
;
167 *p
++ = htonl(resp
->lock
.svid
);
169 /* Encode owner handle. */
170 if (!(p
= xdr_encode_netobj(p
, &resp
->lock
.oh
)))
173 start
= loff_t_to_s32(fl
->fl_start
);
174 if (fl
->fl_end
== OFFSET_MAX
)
177 len
= loff_t_to_s32(fl
->fl_end
- fl
->fl_start
+ 1);
188 * First, the server side XDR functions
191 nlmsvc_decode_testargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
195 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
198 exclusive
= ntohl(*p
++);
199 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
202 argp
->lock
.fl
.fl_type
= F_WRLCK
;
204 return xdr_argsize_check(rqstp
, p
);
208 nlmsvc_encode_testres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
210 if (!(p
= nlm_encode_testres(p
, resp
)))
212 return xdr_ressize_check(rqstp
, p
);
216 nlmsvc_decode_lockargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
220 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
)))
222 argp
->block
= ntohl(*p
++);
223 exclusive
= ntohl(*p
++);
224 if (!(p
= nlm_decode_lock(p
, &argp
->lock
)))
227 argp
->lock
.fl
.fl_type
= F_WRLCK
;
228 argp
->reclaim
= ntohl(*p
++);
229 argp
->state
= ntohl(*p
++);
230 argp
->monitor
= 1; /* monitor client by default */
232 return xdr_argsize_check(rqstp
, p
);
236 nlmsvc_decode_cancargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*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
, nlm_args
*argp
)
254 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
255 || !(p
= nlm_decode_lock(p
, &argp
->lock
)))
257 argp
->lock
.fl
.fl_type
= F_UNLCK
;
258 return xdr_argsize_check(rqstp
, p
);
262 nlmsvc_decode_shareargs(struct svc_rqst
*rqstp
, __be32
*p
, nlm_args
*argp
)
264 struct nlm_lock
*lock
= &argp
->lock
;
266 memset(lock
, 0, sizeof(*lock
));
267 locks_init_lock(&lock
->fl
);
268 lock
->svid
= ~(u32
) 0;
269 lock
->fl
.fl_pid
= (pid_t
)lock
->svid
;
271 if (!(p
= nlm_decode_cookie(p
, &argp
->cookie
))
272 || !(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
273 &lock
->len
, NLM_MAXSTRLEN
))
274 || !(p
= nlm_decode_fh(p
, &lock
->fh
))
275 || !(p
= nlm_decode_oh(p
, &lock
->oh
)))
277 argp
->fsm_mode
= ntohl(*p
++);
278 argp
->fsm_access
= ntohl(*p
++);
279 return xdr_argsize_check(rqstp
, p
);
283 nlmsvc_encode_shareres(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
285 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
288 *p
++ = xdr_zero
; /* sequence argument */
289 return xdr_ressize_check(rqstp
, p
);
293 nlmsvc_encode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
295 if (!(p
= nlm_encode_cookie(p
, &resp
->cookie
)))
298 return xdr_ressize_check(rqstp
, p
);
302 nlmsvc_decode_notify(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_args
*argp
)
304 struct nlm_lock
*lock
= &argp
->lock
;
306 if (!(p
= xdr_decode_string_inplace(p
, &lock
->caller
,
307 &lock
->len
, NLM_MAXSTRLEN
)))
309 argp
->state
= ntohl(*p
++);
310 return xdr_argsize_check(rqstp
, p
);
314 nlmsvc_decode_reboot(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_reboot
*argp
)
316 if (!(p
= xdr_decode_string_inplace(p
, &argp
->mon
, &argp
->len
, SM_MAXSTRLEN
)))
318 argp
->state
= ntohl(*p
++);
319 memcpy(&argp
->priv
.data
, p
, sizeof(argp
->priv
.data
));
320 p
+= XDR_QUADLEN(SM_PRIV_SIZE
);
321 return xdr_argsize_check(rqstp
, p
);
325 nlmsvc_decode_res(struct svc_rqst
*rqstp
, __be32
*p
, struct nlm_res
*resp
)
327 if (!(p
= nlm_decode_cookie(p
, &resp
->cookie
)))
330 return xdr_argsize_check(rqstp
, p
);
334 nlmsvc_decode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
336 return xdr_argsize_check(rqstp
, p
);
340 nlmsvc_encode_void(struct svc_rqst
*rqstp
, __be32
*p
, void *dummy
)
342 return xdr_ressize_check(rqstp
, p
);