Import 2.3.26pre2
[davej-history.git] / fs / lockd / xdr.c
blob85fb7c7290909289437fffe4e97b8824c135c073
1 /*
2 * linux/fs/lockd/xdr.c
4 * XDR support for lockd and the lock client.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/utsname.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>
19 #include <linux/lockd/sm_inter.h>
21 #define NLMDBG_FACILITY NLMDBG_XDR
22 #define NLM_MAXSTRLEN 1024
24 #define QUADLEN(len) (((len) + 3) >> 2)
27 u32 nlm_granted, nlm_lck_denied, nlm_lck_denied_nolocks,
28 nlm_lck_blocked, nlm_lck_denied_grace_period;
31 typedef struct nlm_args nlm_args;
34 * Initialization of NFS status variables
36 void
37 nlmxdr_init(void)
39 static int inited = 0;
41 if (inited)
42 return;
44 nlm_granted = htonl(NLM_LCK_GRANTED);
45 nlm_lck_denied = htonl(NLM_LCK_DENIED);
46 nlm_lck_denied_nolocks = htonl(NLM_LCK_DENIED_NOLOCKS);
47 nlm_lck_blocked = htonl(NLM_LCK_BLOCKED);
48 nlm_lck_denied_grace_period = htonl(NLM_LCK_DENIED_GRACE_PERIOD);
50 inited = 1;
54 * XDR functions for basic NLM types
56 static inline u32 *nlm_decode_cookie(u32 *p, struct nlm_cookie *c)
58 unsigned int len;
60 len = ntohl(*p++);
62 if(len==0)
64 c->len=4;
65 memset(c->data, 0, 4); /* hockeypux brain damage */
67 else if(len<=8)
69 c->len=len;
70 memcpy(c->data, p, len);
71 p+=(len+3)>>2;
73 else
75 printk(KERN_NOTICE
76 "lockd: bad cookie size %d (only cookies under 8 bytes are supported.)\n", len);
77 return NULL;
79 return p;
82 static inline u32 *
83 nlm_encode_cookie(u32 *p, struct nlm_cookie *c)
85 *p++ = htonl(c->len);
86 memcpy(p, c->data, c->len);
87 p+=(c->len+3)>>2;
88 return p;
91 static inline u32 *
92 nlm_decode_fh(u32 *p, struct nfs_fh *f)
94 unsigned int len;
96 if ((len = ntohl(*p++)) != sizeof(*f)) {
97 printk(KERN_NOTICE
98 "lockd: bad fhandle size %x (should be %d)\n",
99 len, sizeof(*f));
100 return NULL;
102 memcpy(f, p, sizeof(*f));
103 return p + XDR_QUADLEN(sizeof(*f));
106 static inline u32 *
107 nlm_encode_fh(u32 *p, struct nfs_fh *f)
109 *p++ = htonl(sizeof(*f));
110 memcpy(p, f, sizeof(*f));
111 return p + XDR_QUADLEN(sizeof(*f));
115 * Encode and decode owner handle
117 static inline u32 *
118 nlm_decode_oh(u32 *p, struct xdr_netobj *oh)
120 return xdr_decode_netobj(p, oh);
123 static inline u32 *
124 nlm_encode_oh(u32 *p, struct xdr_netobj *oh)
126 return xdr_encode_netobj(p, oh);
129 static inline u32 *
130 nlm_decode_lock(u32 *p, struct nlm_lock *lock)
132 struct file_lock *fl = &lock->fl;
133 int len;
135 if (!(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN))
136 || !(p = nlm_decode_fh(p, &lock->fh))
137 || !(p = nlm_decode_oh(p, &lock->oh)))
138 return NULL;
140 memset(fl, 0, sizeof(*fl));
141 fl->fl_owner = current->files;
142 fl->fl_pid = ntohl(*p++);
143 fl->fl_flags = FL_POSIX;
144 fl->fl_type = F_RDLCK; /* as good as anything else */
145 fl->fl_start = ntohl(*p++);
146 len = ntohl(*p++);
147 if (len == 0 || (fl->fl_end = fl->fl_start + len - 1) < 0)
148 fl->fl_end = NLM_OFFSET_MAX;
149 return p;
153 * Encode a lock as part of an NLM call
155 static u32 *
156 nlm_encode_lock(u32 *p, struct nlm_lock *lock)
158 struct file_lock *fl = &lock->fl;
160 if (!(p = xdr_encode_string(p, lock->caller))
161 || !(p = nlm_encode_fh(p, &lock->fh))
162 || !(p = nlm_encode_oh(p, &lock->oh)))
163 return NULL;
165 *p++ = htonl(fl->fl_pid);
166 *p++ = htonl(lock->fl.fl_start);
167 if (lock->fl.fl_end == NLM_OFFSET_MAX)
168 *p++ = xdr_zero;
169 else
170 *p++ = htonl(lock->fl.fl_end - lock->fl.fl_start + 1);
172 return p;
176 * Encode result of a TEST/TEST_MSG call
178 static u32 *
179 nlm_encode_testres(u32 *p, struct nlm_res *resp)
181 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
182 return 0;
183 *p++ = resp->status;
185 if (resp->status == nlm_lck_denied) {
186 struct file_lock *fl = &resp->lock.fl;
188 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
189 *p++ = htonl(fl->fl_pid);
191 /* Encode owner handle. */
192 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
193 return 0;
195 *p++ = htonl(fl->fl_start);
196 if (fl->fl_end == NLM_OFFSET_MAX)
197 *p++ = xdr_zero;
198 else
199 *p++ = htonl(fl->fl_end - fl->fl_start + 1);
202 return p;
206 * Check buffer bounds after decoding arguments
208 static inline int
209 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
211 struct svc_buf *buf = &rqstp->rq_argbuf;
213 return p - buf->base <= buf->buflen;
216 static inline int
217 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
219 struct svc_buf *buf = &rqstp->rq_resbuf;
221 buf->len = p - buf->base;
222 return (buf->len <= buf->buflen);
226 * First, the server side XDR functions
229 nlmsvc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
231 u32 exclusive;
233 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
234 return 0;
236 exclusive = ntohl(*p++);
237 if (!(p = nlm_decode_lock(p, &argp->lock)))
238 return 0;
239 if (exclusive)
240 argp->lock.fl.fl_type = F_WRLCK;
242 return xdr_argsize_check(rqstp, p);
246 nlmsvc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
248 if (!(p = nlm_encode_testres(p, resp)))
249 return 0;
250 return xdr_ressize_check(rqstp, p);
254 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
256 u32 exclusive;
258 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
259 return 0;
260 argp->block = ntohl(*p++);
261 exclusive = ntohl(*p++);
262 if (!(p = nlm_decode_lock(p, &argp->lock)))
263 return 0;
264 if (exclusive)
265 argp->lock.fl.fl_type = F_WRLCK;
266 argp->reclaim = ntohl(*p++);
267 argp->state = ntohl(*p++);
268 argp->monitor = 1; /* monitor client by default */
270 return xdr_argsize_check(rqstp, p);
274 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
276 u32 exclusive;
278 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
279 return 0;
280 argp->block = ntohl(*p++);
281 exclusive = ntohl(*p++);
282 if (!(p = nlm_decode_lock(p, &argp->lock)))
283 return 0;
284 if (exclusive)
285 argp->lock.fl.fl_type = F_WRLCK;
286 return xdr_argsize_check(rqstp, p);
290 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
292 if (!(p = nlm_decode_cookie(p, &argp->cookie))
293 || !(p = nlm_decode_lock(p, &argp->lock)))
294 return 0;
295 argp->lock.fl.fl_type = F_UNLCK;
296 return xdr_argsize_check(rqstp, p);
300 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
302 struct nlm_lock *lock = &argp->lock;
303 int len;
305 memset(lock, 0, sizeof(*lock));
306 lock->fl.fl_pid = ~(u32) 0;
308 if (!(p = nlm_decode_cookie(p, &argp->cookie))
309 || !(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN))
310 || !(p = nlm_decode_fh(p, &lock->fh))
311 || !(p = nlm_decode_oh(p, &lock->oh)))
312 return 0;
313 argp->fsm_mode = ntohl(*p++);
314 argp->fsm_access = ntohl(*p++);
315 return xdr_argsize_check(rqstp, p);
319 nlmsvc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
321 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
322 return 0;
323 *p++ = resp->status;
324 *p++ = xdr_zero; /* sequence argument */
325 return xdr_ressize_check(rqstp, p);
329 nlmsvc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
331 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
332 return 0;
333 *p++ = resp->status;
334 return xdr_ressize_check(rqstp, p);
338 nlmsvc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
340 struct nlm_lock *lock = &argp->lock;
341 int len;
343 if (!(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN)))
344 return 0;
345 argp->state = ntohl(*p++);
346 return xdr_argsize_check(rqstp, p);
350 nlmsvc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
352 if (!(p = xdr_decode_string(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
353 return 0;
354 argp->state = ntohl(*p++);
355 argp->addr = ntohl(*p++);
356 return xdr_argsize_check(rqstp, p);
360 nlmsvc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
362 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
363 return 0;
364 resp->status = ntohl(*p++);
365 return xdr_argsize_check(rqstp, p);
369 nlmsvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
371 return xdr_argsize_check(rqstp, p);
375 nlmsvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
377 return xdr_ressize_check(rqstp, p);
381 * Now, the client side XDR functions
383 static int
384 nlmclt_encode_void(struct rpc_rqst *req, u32 *p, void *ptr)
386 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
387 return 0;
390 static int
391 nlmclt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
393 return 0;
396 static int
397 nlmclt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
399 struct nlm_lock *lock = &argp->lock;
401 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
402 return -EIO;
403 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
404 if (!(p = nlm_encode_lock(p, lock)))
405 return -EIO;
406 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
407 return 0;
410 static int
411 nlmclt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
413 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
414 return -EIO;
415 resp->status = ntohl(*p++);
416 if (resp->status == NLM_LCK_DENIED) {
417 struct file_lock *fl = &resp->lock.fl;
418 u32 excl, len;
420 memset(&resp->lock, 0, sizeof(resp->lock));
421 excl = ntohl(*p++);
422 fl->fl_pid = ntohl(*p++);
423 if (!(p = nlm_decode_oh(p, &resp->lock.oh)))
424 return -EIO;
426 fl->fl_flags = FL_POSIX;
427 fl->fl_type = excl? F_WRLCK : F_RDLCK;
428 fl->fl_start = ntohl(*p++);
429 len = ntohl(*p++);
430 if (len == 0 || (fl->fl_end = fl->fl_start + len - 1) < 0)
431 fl->fl_end = NLM_OFFSET_MAX;
433 return 0;
437 static int
438 nlmclt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
440 struct nlm_lock *lock = &argp->lock;
442 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
443 return -EIO;
444 *p++ = argp->block? xdr_one : xdr_zero;
445 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
446 if (!(p = nlm_encode_lock(p, lock)))
447 return -EIO;
448 *p++ = argp->reclaim? xdr_one : xdr_zero;
449 *p++ = htonl(argp->state);
450 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
451 return 0;
454 static int
455 nlmclt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
457 struct nlm_lock *lock = &argp->lock;
459 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
460 return -EIO;
461 *p++ = argp->block? xdr_one : xdr_zero;
462 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
463 if (!(p = nlm_encode_lock(p, lock)))
464 return -EIO;
465 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
466 return 0;
469 static int
470 nlmclt_encode_unlockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
472 struct nlm_lock *lock = &argp->lock;
474 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
475 return -EIO;
476 if (!(p = nlm_encode_lock(p, lock)))
477 return -EIO;
478 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
479 return 0;
482 static int
483 nlmclt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
485 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
486 return -EIO;
487 *p++ = resp->status;
488 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
489 return 0;
492 static int
493 nlmclt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
495 if (!(p = nlm_encode_testres(p, resp)))
496 return -EIO;
497 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
498 return 0;
501 static int
502 nlmclt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
504 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
505 return -EIO;
506 resp->status = ntohl(*p++);
507 return 0;
511 * Buffer requirements for NLM
513 #define NLM_void_sz 0
514 #define NLM_cookie_sz 3 /* 1 len , 2 data */
515 #define NLM_caller_sz 1+QUADLEN(sizeof(system_utsname.nodename))
516 #define NLM_netobj_sz 1+QUADLEN(XDR_MAX_NETOBJ)
517 /* #define NLM_owner_sz 1+QUADLEN(NLM_MAXOWNER) */
518 #define NLM_fhandle_sz 1+QUADLEN(NFS_FHSIZE)
519 #define NLM_lock_sz 3+NLM_caller_sz+NLM_netobj_sz+NLM_fhandle_sz
520 #define NLM_holder_sz 4+NLM_netobj_sz
522 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
523 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
524 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
525 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
527 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
528 #define NLM_res_sz NLM_cookie_sz+1
529 #define NLM_norep_sz 0
531 #ifndef MAX
532 # define MAX(a, b) (((a) > (b))? (a) : (b))
533 #endif
536 * For NLM, a void procedure really returns nothing
538 #define nlmclt_decode_norep NULL
540 #define PROC(proc, argtype, restype) \
541 { "nlm_" #proc, \
542 (kxdrproc_t) nlmclt_encode_##argtype, \
543 (kxdrproc_t) nlmclt_decode_##restype, \
544 MAX(NLM_##argtype##_sz, NLM_##restype##_sz) << 2 \
547 static struct rpc_procinfo nlm_procedures[] = {
548 PROC(null, void, void),
549 PROC(test, testargs, testres),
550 PROC(lock, lockargs, res),
551 PROC(canc, cancargs, res),
552 PROC(unlock, unlockargs, res),
553 PROC(granted, testargs, res),
554 PROC(test_msg, testargs, norep),
555 PROC(lock_msg, lockargs, norep),
556 PROC(canc_msg, cancargs, norep),
557 PROC(unlock_msg, unlockargs, norep),
558 PROC(granted_msg, testargs, norep),
559 PROC(test_res, testres, norep),
560 PROC(lock_res, res, norep),
561 PROC(canc_res, res, norep),
562 PROC(unlock_res, res, norep),
563 PROC(granted_res, res, norep),
564 PROC(undef, void, void),
565 PROC(undef, void, void),
566 PROC(undef, void, void),
567 PROC(undef, void, void),
568 #ifdef NLMCLNT_SUPPORT_SHARES
569 PROC(share, shareargs, shareres),
570 PROC(unshare, shareargs, shareres),
571 PROC(nm_lock, lockargs, res),
572 PROC(free_all, notify, void),
573 #else
574 PROC(undef, void, void),
575 PROC(undef, void, void),
576 PROC(undef, void, void),
577 PROC(undef, void, void),
578 #endif
581 static struct rpc_version nlm_version1 = {
582 1, 16, nlm_procedures,
585 static struct rpc_version nlm_version3 = {
586 3, 24, nlm_procedures,
589 static struct rpc_version * nlm_versions[] = {
590 NULL,
591 &nlm_version1,
592 NULL,
593 &nlm_version3,
596 static struct rpc_stat nlm_stats;
598 struct rpc_program nlm_program = {
599 "lockd",
600 NLM_PROGRAM,
601 sizeof(nlm_versions) / sizeof(nlm_versions[0]),
602 nlm_versions,
603 &nlm_stats,
606 #ifdef LOCKD_DEBUG
607 char *
608 nlm_procname(u32 proc)
610 if (proc < sizeof(nlm_procedures)/sizeof(nlm_procedures[0]))
611 return nlm_procedures[proc].p_procname;
612 return "unknown";
614 #endif