4 * XDR support for lockd and the lock client.
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 #include <uapi/linux/nfs2.h>
21 #define NLMDBG_FACILITY NLMDBG_XDR
25 s32_to_loff_t(__s32 offset)
27 return (loff_t)offset;
31 loff_t_to_s32(loff_t offset)
34 if (offset >= NLM_OFFSET_MAX)
36 else if (offset <= -NLM_OFFSET_MAX)
37 res = -NLM_OFFSET_MAX;
44 * XDR functions for basic NLM types
46 static __be32 *nlm_decode_cookie(__be32 *p, struct nlm_cookie *c)
55 memset(c->data, 0, 4); /* hockeypux brain damage */
57 else if(len<=NLM_MAXCOOKIELEN)
60 memcpy(c->data, p, len);
65 dprintk("lockd: bad cookie size %d (only cookies under "
66 "%d bytes are supported.)\n",
67 len, NLM_MAXCOOKIELEN);
73 static inline __be32 *
74 nlm_encode_cookie(__be32 *p, struct nlm_cookie *c)
77 memcpy(p, c->data, c->len);
78 p+=XDR_QUADLEN(c->len);
83 nlm_decode_fh(__be32 *p, struct nfs_fh *f)
87 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
88 dprintk("lockd: bad fhandle size %d (should be %d)\n",
92 f->size = NFS2_FHSIZE;
93 memset(f->data, 0, sizeof(f->data));
94 memcpy(f->data, p, NFS2_FHSIZE);
95 return p + XDR_QUADLEN(NFS2_FHSIZE);
99 * Encode and decode owner handle
101 static inline __be32 *
102 nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
104 return xdr_decode_netobj(p, oh);
107 static inline __be32 *
108 nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
110 return xdr_encode_netobj(p, oh);
114 nlm_decode_lock(__be32 *p, struct nlm_lock *lock)
116 struct file_lock *fl = &lock->fl;
119 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
122 || !(p = nlm_decode_fh(p, &lock->fh))
123 || !(p = nlm_decode_oh(p, &lock->oh)))
125 lock->svid = ntohl(*p++);
128 fl->fl_owner = current->files;
129 fl->fl_pid = (pid_t)lock->svid;
130 fl->fl_flags = FL_POSIX;
131 fl->fl_type = F_RDLCK; /* as good as anything else */
134 end = start + len - 1;
136 fl->fl_start = s32_to_loff_t(start);
138 if (len == 0 || end < 0)
139 fl->fl_end = OFFSET_MAX;
141 fl->fl_end = s32_to_loff_t(end);
146 * Encode result of a TEST/TEST_MSG call
149 nlm_encode_testres(__be32 *p, struct nlm_res *resp)
153 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
157 if (resp->status == nlm_lck_denied) {
158 struct file_lock *fl = &resp->lock.fl;
160 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
161 *p++ = htonl(resp->lock.svid);
163 /* Encode owner handle. */
164 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
167 start = loff_t_to_s32(fl->fl_start);
168 if (fl->fl_end == OFFSET_MAX)
171 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
182 * First, the server side XDR functions
185 nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
189 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
192 exclusive = ntohl(*p++);
193 if (!(p = nlm_decode_lock(p, &argp->lock)))
196 argp->lock.fl.fl_type = F_WRLCK;
198 return xdr_argsize_check(rqstp, p);
202 nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
204 if (!(p = nlm_encode_testres(p, resp)))
206 return xdr_ressize_check(rqstp, p);
210 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
214 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
216 argp->block = ntohl(*p++);
217 exclusive = ntohl(*p++);
218 if (!(p = nlm_decode_lock(p, &argp->lock)))
221 argp->lock.fl.fl_type = F_WRLCK;
222 argp->reclaim = ntohl(*p++);
223 argp->state = ntohl(*p++);
224 argp->monitor = 1; /* monitor client by default */
226 return xdr_argsize_check(rqstp, p);
230 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
234 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
236 argp->block = ntohl(*p++);
237 exclusive = ntohl(*p++);
238 if (!(p = nlm_decode_lock(p, &argp->lock)))
241 argp->lock.fl.fl_type = F_WRLCK;
242 return xdr_argsize_check(rqstp, p);
246 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
248 if (!(p = nlm_decode_cookie(p, &argp->cookie))
249 || !(p = nlm_decode_lock(p, &argp->lock)))
251 argp->lock.fl.fl_type = F_UNLCK;
252 return xdr_argsize_check(rqstp, p);
256 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
258 struct nlm_lock *lock = &argp->lock;
260 memset(lock, 0, sizeof(*lock));
261 locks_init_lock(&lock->fl);
262 lock->svid = ~(u32) 0;
263 lock->fl.fl_pid = (pid_t)lock->svid;
265 if (!(p = nlm_decode_cookie(p, &argp->cookie))
266 || !(p = xdr_decode_string_inplace(p, &lock->caller,
267 &lock->len, NLM_MAXSTRLEN))
268 || !(p = nlm_decode_fh(p, &lock->fh))
269 || !(p = nlm_decode_oh(p, &lock->oh)))
271 argp->fsm_mode = ntohl(*p++);
272 argp->fsm_access = ntohl(*p++);
273 return xdr_argsize_check(rqstp, p);
277 nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
279 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
282 *p++ = xdr_zero; /* sequence argument */
283 return xdr_ressize_check(rqstp, p);
287 nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
289 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
292 return xdr_ressize_check(rqstp, p);
296 nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
298 struct nlm_lock *lock = &argp->lock;
300 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
301 &lock->len, NLM_MAXSTRLEN)))
303 argp->state = ntohl(*p++);
304 return xdr_argsize_check(rqstp, p);
308 nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
310 if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
312 argp->state = ntohl(*p++);
313 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
314 p += XDR_QUADLEN(SM_PRIV_SIZE);
315 return xdr_argsize_check(rqstp, p);
319 nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
321 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
324 return xdr_argsize_check(rqstp, p);
328 nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
330 return xdr_argsize_check(rqstp, p);
334 nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
336 return xdr_ressize_check(rqstp, p);