perf_counter: Rename 'event' to event_id/hw_event
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / lockd / xdr.c
blob0336f2beacdee7792bf4155be5d279bfd30ca756
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>
20 #define NLMDBG_FACILITY NLMDBG_XDR
23 static inline loff_t
24 s32_to_loff_t(__s32 offset)
26 return (loff_t)offset;
29 static inline __s32
30 loff_t_to_s32(loff_t offset)
32 __s32 res;
33 if (offset >= NLM_OFFSET_MAX)
34 res = NLM_OFFSET_MAX;
35 else if (offset <= -NLM_OFFSET_MAX)
36 res = -NLM_OFFSET_MAX;
37 else
38 res = offset;
39 return res;
43 * XDR functions for basic NLM types
45 static __be32 *nlm_decode_cookie(__be32 *p, struct nlm_cookie *c)
47 unsigned int len;
49 len = ntohl(*p++);
51 if(len==0)
53 c->len=4;
54 memset(c->data, 0, 4); /* hockeypux brain damage */
56 else if(len<=NLM_MAXCOOKIELEN)
58 c->len=len;
59 memcpy(c->data, p, len);
60 p+=XDR_QUADLEN(len);
62 else
64 dprintk("lockd: bad cookie size %d (only cookies under "
65 "%d bytes are supported.)\n",
66 len, NLM_MAXCOOKIELEN);
67 return NULL;
69 return p;
72 static inline __be32 *
73 nlm_encode_cookie(__be32 *p, struct nlm_cookie *c)
75 *p++ = htonl(c->len);
76 memcpy(p, c->data, c->len);
77 p+=XDR_QUADLEN(c->len);
78 return p;
81 static __be32 *
82 nlm_decode_fh(__be32 *p, struct nfs_fh *f)
84 unsigned int len;
86 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
87 dprintk("lockd: bad fhandle size %d (should be %d)\n",
88 len, NFS2_FHSIZE);
89 return NULL;
91 f->size = NFS2_FHSIZE;
92 memset(f->data, 0, sizeof(f->data));
93 memcpy(f->data, p, NFS2_FHSIZE);
94 return p + XDR_QUADLEN(NFS2_FHSIZE);
97 static inline __be32 *
98 nlm_encode_fh(__be32 *p, struct nfs_fh *f)
100 *p++ = htonl(NFS2_FHSIZE);
101 memcpy(p, f->data, NFS2_FHSIZE);
102 return p + XDR_QUADLEN(NFS2_FHSIZE);
106 * Encode and decode owner handle
108 static inline __be32 *
109 nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
111 return xdr_decode_netobj(p, oh);
114 static inline __be32 *
115 nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
117 return xdr_encode_netobj(p, oh);
120 static __be32 *
121 nlm_decode_lock(__be32 *p, struct nlm_lock *lock)
123 struct file_lock *fl = &lock->fl;
124 s32 start, len, end;
126 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
127 &lock->len,
128 NLM_MAXSTRLEN))
129 || !(p = nlm_decode_fh(p, &lock->fh))
130 || !(p = nlm_decode_oh(p, &lock->oh)))
131 return NULL;
132 lock->svid = ntohl(*p++);
134 locks_init_lock(fl);
135 fl->fl_owner = current->files;
136 fl->fl_pid = (pid_t)lock->svid;
137 fl->fl_flags = FL_POSIX;
138 fl->fl_type = F_RDLCK; /* as good as anything else */
139 start = ntohl(*p++);
140 len = ntohl(*p++);
141 end = start + len - 1;
143 fl->fl_start = s32_to_loff_t(start);
145 if (len == 0 || end < 0)
146 fl->fl_end = OFFSET_MAX;
147 else
148 fl->fl_end = s32_to_loff_t(end);
149 return p;
153 * Encode a lock as part of an NLM call
155 static __be32 *
156 nlm_encode_lock(__be32 *p, struct nlm_lock *lock)
158 struct file_lock *fl = &lock->fl;
159 __s32 start, len;
161 if (!(p = xdr_encode_string(p, lock->caller))
162 || !(p = nlm_encode_fh(p, &lock->fh))
163 || !(p = nlm_encode_oh(p, &lock->oh)))
164 return NULL;
166 if (fl->fl_start > NLM_OFFSET_MAX
167 || (fl->fl_end > NLM_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
168 return NULL;
170 start = loff_t_to_s32(fl->fl_start);
171 if (fl->fl_end == OFFSET_MAX)
172 len = 0;
173 else
174 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
176 *p++ = htonl(lock->svid);
177 *p++ = htonl(start);
178 *p++ = htonl(len);
180 return p;
184 * Encode result of a TEST/TEST_MSG call
186 static __be32 *
187 nlm_encode_testres(__be32 *p, struct nlm_res *resp)
189 s32 start, len;
191 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
192 return NULL;
193 *p++ = resp->status;
195 if (resp->status == nlm_lck_denied) {
196 struct file_lock *fl = &resp->lock.fl;
198 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
199 *p++ = htonl(resp->lock.svid);
201 /* Encode owner handle. */
202 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
203 return NULL;
205 start = loff_t_to_s32(fl->fl_start);
206 if (fl->fl_end == OFFSET_MAX)
207 len = 0;
208 else
209 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
211 *p++ = htonl(start);
212 *p++ = htonl(len);
215 return p;
220 * First, the server side XDR functions
223 nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
225 u32 exclusive;
227 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
228 return 0;
230 exclusive = ntohl(*p++);
231 if (!(p = nlm_decode_lock(p, &argp->lock)))
232 return 0;
233 if (exclusive)
234 argp->lock.fl.fl_type = F_WRLCK;
236 return xdr_argsize_check(rqstp, p);
240 nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
242 if (!(p = nlm_encode_testres(p, resp)))
243 return 0;
244 return xdr_ressize_check(rqstp, p);
248 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
250 u32 exclusive;
252 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
253 return 0;
254 argp->block = ntohl(*p++);
255 exclusive = ntohl(*p++);
256 if (!(p = nlm_decode_lock(p, &argp->lock)))
257 return 0;
258 if (exclusive)
259 argp->lock.fl.fl_type = F_WRLCK;
260 argp->reclaim = ntohl(*p++);
261 argp->state = ntohl(*p++);
262 argp->monitor = 1; /* monitor client by default */
264 return xdr_argsize_check(rqstp, p);
268 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
270 u32 exclusive;
272 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
273 return 0;
274 argp->block = ntohl(*p++);
275 exclusive = ntohl(*p++);
276 if (!(p = nlm_decode_lock(p, &argp->lock)))
277 return 0;
278 if (exclusive)
279 argp->lock.fl.fl_type = F_WRLCK;
280 return xdr_argsize_check(rqstp, p);
284 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
286 if (!(p = nlm_decode_cookie(p, &argp->cookie))
287 || !(p = nlm_decode_lock(p, &argp->lock)))
288 return 0;
289 argp->lock.fl.fl_type = F_UNLCK;
290 return xdr_argsize_check(rqstp, p);
294 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
296 struct nlm_lock *lock = &argp->lock;
298 memset(lock, 0, sizeof(*lock));
299 locks_init_lock(&lock->fl);
300 lock->svid = ~(u32) 0;
301 lock->fl.fl_pid = (pid_t)lock->svid;
303 if (!(p = nlm_decode_cookie(p, &argp->cookie))
304 || !(p = xdr_decode_string_inplace(p, &lock->caller,
305 &lock->len, NLM_MAXSTRLEN))
306 || !(p = nlm_decode_fh(p, &lock->fh))
307 || !(p = nlm_decode_oh(p, &lock->oh)))
308 return 0;
309 argp->fsm_mode = ntohl(*p++);
310 argp->fsm_access = ntohl(*p++);
311 return xdr_argsize_check(rqstp, p);
315 nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
317 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
318 return 0;
319 *p++ = resp->status;
320 *p++ = xdr_zero; /* sequence argument */
321 return xdr_ressize_check(rqstp, p);
325 nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
327 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
328 return 0;
329 *p++ = resp->status;
330 return xdr_ressize_check(rqstp, p);
334 nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
336 struct nlm_lock *lock = &argp->lock;
338 if (!(p = xdr_decode_string_inplace(p, &lock->caller,
339 &lock->len, NLM_MAXSTRLEN)))
340 return 0;
341 argp->state = ntohl(*p++);
342 return xdr_argsize_check(rqstp, p);
346 nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
348 if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
349 return 0;
350 argp->state = ntohl(*p++);
351 memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
352 p += XDR_QUADLEN(SM_PRIV_SIZE);
353 return xdr_argsize_check(rqstp, p);
357 nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
359 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
360 return 0;
361 resp->status = *p++;
362 return xdr_argsize_check(rqstp, p);
366 nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
368 return xdr_argsize_check(rqstp, p);
372 nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
374 return xdr_ressize_check(rqstp, p);
378 * Now, the client side XDR functions
380 #ifdef NLMCLNT_SUPPORT_SHARES
381 static int
382 nlmclt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
384 return 0;
386 #endif
388 static int
389 nlmclt_encode_testargs(struct rpc_rqst *req, __be32 *p, nlm_args *argp)
391 struct nlm_lock *lock = &argp->lock;
393 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
394 return -EIO;
395 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
396 if (!(p = nlm_encode_lock(p, lock)))
397 return -EIO;
398 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
399 return 0;
402 static int
403 nlmclt_decode_testres(struct rpc_rqst *req, __be32 *p, struct nlm_res *resp)
405 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
406 return -EIO;
407 resp->status = *p++;
408 if (resp->status == nlm_lck_denied) {
409 struct file_lock *fl = &resp->lock.fl;
410 u32 excl;
411 s32 start, len, end;
413 memset(&resp->lock, 0, sizeof(resp->lock));
414 locks_init_lock(fl);
415 excl = ntohl(*p++);
416 resp->lock.svid = ntohl(*p++);
417 fl->fl_pid = (pid_t)resp->lock.svid;
418 if (!(p = nlm_decode_oh(p, &resp->lock.oh)))
419 return -EIO;
421 fl->fl_flags = FL_POSIX;
422 fl->fl_type = excl? F_WRLCK : F_RDLCK;
423 start = ntohl(*p++);
424 len = ntohl(*p++);
425 end = start + len - 1;
427 fl->fl_start = s32_to_loff_t(start);
428 if (len == 0 || end < 0)
429 fl->fl_end = OFFSET_MAX;
430 else
431 fl->fl_end = s32_to_loff_t(end);
433 return 0;
437 static int
438 nlmclt_encode_lockargs(struct rpc_rqst *req, __be32 *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, __be32 *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, __be32 *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, __be32 *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, __be32 *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, __be32 *p, struct nlm_res *resp)
504 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
505 return -EIO;
506 resp->status = *p++;
507 return 0;
510 #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
511 # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
512 #endif
515 * Buffer requirements for NLM
517 #define NLM_void_sz 0
518 #define NLM_cookie_sz 1+XDR_QUADLEN(NLM_MAXCOOKIELEN)
519 #define NLM_caller_sz 1+XDR_QUADLEN(NLMCLNT_OHSIZE)
520 #define NLM_owner_sz 1+XDR_QUADLEN(NLMCLNT_OHSIZE)
521 #define NLM_fhandle_sz 1+XDR_QUADLEN(NFS2_FHSIZE)
522 #define NLM_lock_sz 3+NLM_caller_sz+NLM_owner_sz+NLM_fhandle_sz
523 #define NLM_holder_sz 4+NLM_owner_sz
525 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
526 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
527 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
528 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
530 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
531 #define NLM_res_sz NLM_cookie_sz+1
532 #define NLM_norep_sz 0
535 * For NLM, a void procedure really returns nothing
537 #define nlmclt_decode_norep NULL
539 #define PROC(proc, argtype, restype) \
540 [NLMPROC_##proc] = { \
541 .p_proc = NLMPROC_##proc, \
542 .p_encode = (kxdrproc_t) nlmclt_encode_##argtype, \
543 .p_decode = (kxdrproc_t) nlmclt_decode_##restype, \
544 .p_arglen = NLM_##argtype##_sz, \
545 .p_replen = NLM_##restype##_sz, \
546 .p_statidx = NLMPROC_##proc, \
547 .p_name = #proc, \
550 static struct rpc_procinfo nlm_procedures[] = {
551 PROC(TEST, testargs, testres),
552 PROC(LOCK, lockargs, res),
553 PROC(CANCEL, cancargs, res),
554 PROC(UNLOCK, unlockargs, res),
555 PROC(GRANTED, testargs, res),
556 PROC(TEST_MSG, testargs, norep),
557 PROC(LOCK_MSG, lockargs, norep),
558 PROC(CANCEL_MSG, cancargs, norep),
559 PROC(UNLOCK_MSG, unlockargs, norep),
560 PROC(GRANTED_MSG, testargs, norep),
561 PROC(TEST_RES, testres, norep),
562 PROC(LOCK_RES, res, norep),
563 PROC(CANCEL_RES, res, norep),
564 PROC(UNLOCK_RES, res, norep),
565 PROC(GRANTED_RES, res, norep),
566 #ifdef NLMCLNT_SUPPORT_SHARES
567 PROC(SHARE, shareargs, shareres),
568 PROC(UNSHARE, shareargs, shareres),
569 PROC(NM_LOCK, lockargs, res),
570 PROC(FREE_ALL, notify, void),
571 #endif
574 static struct rpc_version nlm_version1 = {
575 .number = 1,
576 .nrprocs = 16,
577 .procs = nlm_procedures,
580 static struct rpc_version nlm_version3 = {
581 .number = 3,
582 .nrprocs = 24,
583 .procs = nlm_procedures,
586 static struct rpc_version * nlm_versions[] = {
587 [1] = &nlm_version1,
588 [3] = &nlm_version3,
589 #ifdef CONFIG_LOCKD_V4
590 [4] = &nlm_version4,
591 #endif
594 static struct rpc_stat nlm_stats;
596 struct rpc_program nlm_program = {
597 .name = "lockd",
598 .number = NLM_PROGRAM,
599 .nrvers = ARRAY_SIZE(nlm_versions),
600 .version = nlm_versions,
601 .stats = &nlm_stats,
604 #ifdef RPC_DEBUG
605 const char *nlmdbg_cookie2a(const struct nlm_cookie *cookie)
608 * We can get away with a static buffer because we're only
609 * called with BKL held.
611 static char buf[2*NLM_MAXCOOKIELEN+1];
612 unsigned int i, len = sizeof(buf);
613 char *p = buf;
615 len--; /* allow for trailing \0 */
616 if (len < 3)
617 return "???";
618 for (i = 0 ; i < cookie->len ; i++) {
619 if (len < 2) {
620 strcpy(p-3, "...");
621 break;
623 sprintf(p, "%02x", cookie->data[i]);
624 p += 2;
625 len -= 2;
627 *p = '\0';
629 return buf;
631 #endif