Import 2.3.47pre4
[davej-history.git] / fs / lockd / xdr4.c
blob7cedcd849771978db6829aed2eefd591c5ee010b
1 /*
2 * linux/fs/lockd/xdr4.c
4 * XDR support for lockd and the lock client.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 * Copyright (C) 1999, Trond Myklebust <trond.myklebust@fys.uio.no>
8 */
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/utsname.h>
13 #include <linux/nfs.h>
15 #include <linux/sunrpc/xdr.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/svc.h>
18 #include <linux/sunrpc/stats.h>
19 #include <linux/lockd/lockd.h>
20 #include <linux/lockd/sm_inter.h>
22 #define NLMDBG_FACILITY NLMDBG_XDR
23 #define NLM_MAXSTRLEN 1024
24 #define OFFSET_MAX ((off_t)LONG_MAX)
26 #define QUADLEN(len) (((len) + 3) >> 2)
28 u32 nlm4_deadlock, nlm4_rofs, nlm4_stale_fh, nlm4_fbig,
29 nlm4_failed;
32 typedef struct nlm_args nlm_args;
34 static inline off_t
35 size_to_off_t(__s64 size)
37 size = (size > (__s64)LONG_MAX) ? (off_t)LONG_MAX : (off_t) size;
38 return (size < (__s64)-LONG_MAX) ? (off_t)-LONG_MAX : (off_t) size;
42 * XDR functions for basic NLM types
44 static u32 *
45 nlm4_decode_cookie(u32 *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<=8)
58 c->len=len;
59 memcpy(c->data, p, len);
60 p+=(len+3)>>2;
62 else
64 printk(KERN_NOTICE
65 "lockd: bad cookie size %d (only cookies under 8 bytes are supported.)\n", len);
66 return NULL;
68 return p;
71 static u32 *
72 nlm4_encode_cookie(u32 *p, struct nlm_cookie *c)
74 *p++ = htonl(c->len);
75 memcpy(p, c->data, c->len);
76 p+=(c->len+3)>>2;
77 return p;
80 static u32 *
81 nlm4_decode_fh(u32 *p, struct nfs_fh *f)
83 memset(f->data, 0, sizeof(f->data));
84 #ifdef NFS_MAXFHSIZE
85 f->size = ntohl(*p++);
86 if (f->size > NFS_MAXFHSIZE) {
87 printk(KERN_NOTICE
88 "lockd: bad fhandle size %x (should be %d)\n",
89 f->size, NFS_MAXFHSIZE);
90 return NULL;
92 memcpy(f->data, p, f->size);
93 return p + XDR_QUADLEN(f->size);
94 #else
95 if (ntohl(*p++) != NFS_FHSIZE)
96 return NULL; /* for now, all filehandles are 32 bytes */
97 memcpy(f->data, p, NFS_FHSIZE);
98 return p + XDR_QUADLEN(NFS_FHSIZE);
99 #endif
102 static u32 *
103 nlm4_encode_fh(u32 *p, struct nfs_fh *f)
105 #ifdef NFS_MAXFHSIZE
106 *p++ = htonl(f->size);
107 memcpy(p, f->data, f->size);
108 return p + XDR_QUADLEN(f->size);
109 #else
110 *p++ = htonl(NFS_FHSIZE);
111 memcpy(p, f->data, NFS_FHSIZE);
112 return p + XDR_QUADLEN(NFS_FHSIZE);
113 #endif
117 * Encode and decode owner handle
119 static u32 *
120 nlm4_decode_oh(u32 *p, struct xdr_netobj *oh)
122 return xdr_decode_netobj(p, oh);
125 static u32 *
126 nlm4_encode_oh(u32 *p, struct xdr_netobj *oh)
128 return xdr_encode_netobj(p, oh);
131 static u32 *
132 nlm4_decode_lock(u32 *p, struct nlm_lock *lock)
134 struct file_lock *fl = &lock->fl;
135 __s64 len, start, end;
136 int tmp;
138 if (!(p = xdr_decode_string(p, &lock->caller, &tmp, NLM_MAXSTRLEN))
139 || !(p = nlm4_decode_fh(p, &lock->fh))
140 || !(p = nlm4_decode_oh(p, &lock->oh)))
141 return NULL;
143 memset(fl, 0, sizeof(*fl));
144 fl->fl_owner = current->files;
145 fl->fl_pid = ntohl(*p++);
146 fl->fl_flags = FL_POSIX;
147 fl->fl_type = F_RDLCK; /* as good as anything else */
148 p = xdr_decode_hyper(p, &start);
149 p = xdr_decode_hyper(p, &len);
150 end = start + len - 1;
152 fl->fl_start = size_to_off_t(start);
153 fl->fl_end = size_to_off_t(end);
155 if (len == 0 || fl->fl_end < 0)
156 fl->fl_end = OFFSET_MAX;
157 return p;
161 * Encode a lock as part of an NLM call
163 static u32 *
164 nlm4_encode_lock(u32 *p, struct nlm_lock *lock)
166 struct file_lock *fl = &lock->fl;
168 if (!(p = xdr_encode_string(p, lock->caller))
169 || !(p = nlm4_encode_fh(p, &lock->fh))
170 || !(p = nlm4_encode_oh(p, &lock->oh)))
171 return NULL;
173 *p++ = htonl(fl->fl_pid);
174 p = xdr_encode_hyper(p, fl->fl_start);
175 if (fl->fl_end == OFFSET_MAX)
176 p = xdr_encode_hyper(p, 0);
177 else
178 p = xdr_encode_hyper(p, fl->fl_end - fl->fl_start + 1);
180 return p;
184 * Encode result of a TEST/TEST_MSG call
186 static u32 *
187 nlm4_encode_testres(u32 *p, struct nlm_res *resp)
189 dprintk("xdr: before encode_testres (p %p resp %p)\n", p, resp);
190 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
191 return 0;
192 *p++ = resp->status;
194 if (resp->status == nlm_lck_denied) {
195 struct file_lock *fl = &resp->lock.fl;
197 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
198 *p++ = htonl(fl->fl_pid);
200 /* Encode owner handle. */
201 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
202 return 0;
204 p = xdr_encode_hyper(p, fl->fl_start);
205 if (fl->fl_end == OFFSET_MAX)
206 p = xdr_encode_hyper(p, 0);
207 else
208 p = xdr_encode_hyper(p, fl->fl_end - fl->fl_start + 1);
209 dprintk("xdr: encode_testres (status %d pid %d type %d start %ld end %ld)\n", resp->status, fl->fl_pid, fl->fl_type, fl->fl_start, fl->fl_end);
212 dprintk("xdr: after encode_testres (p %p resp %p)\n", p, resp);
213 return p;
218 * Check buffer bounds after decoding arguments
220 static int
221 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
223 struct svc_buf *buf = &rqstp->rq_argbuf;
225 return p - buf->base <= buf->buflen;
228 static int
229 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
231 struct svc_buf *buf = &rqstp->rq_resbuf;
233 buf->len = p - buf->base;
234 return (buf->len <= buf->buflen);
238 * First, the server side XDR functions
241 nlm4svc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
243 u32 exclusive;
245 if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
246 return 0;
248 exclusive = ntohl(*p++);
249 if (!(p = nlm4_decode_lock(p, &argp->lock)))
250 return 0;
251 if (exclusive)
252 argp->lock.fl.fl_type = F_WRLCK;
254 return xdr_argsize_check(rqstp, p);
258 nlm4svc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
260 if (!(p = nlm4_encode_testres(p, resp)))
261 return 0;
262 return xdr_ressize_check(rqstp, p);
266 nlm4svc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
268 u32 exclusive;
270 if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
271 return 0;
272 argp->block = ntohl(*p++);
273 exclusive = ntohl(*p++);
274 if (!(p = nlm4_decode_lock(p, &argp->lock)))
275 return 0;
276 if (exclusive)
277 argp->lock.fl.fl_type = F_WRLCK;
278 argp->reclaim = ntohl(*p++);
279 argp->state = ntohl(*p++);
280 argp->monitor = 1; /* monitor client by default */
282 return xdr_argsize_check(rqstp, p);
286 nlm4svc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
288 u32 exclusive;
290 if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
291 return 0;
292 argp->block = ntohl(*p++);
293 exclusive = ntohl(*p++);
294 if (!(p = nlm4_decode_lock(p, &argp->lock)))
295 return 0;
296 if (exclusive)
297 argp->lock.fl.fl_type = F_WRLCK;
298 return xdr_argsize_check(rqstp, p);
302 nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
304 if (!(p = nlm4_decode_cookie(p, &argp->cookie))
305 || !(p = nlm4_decode_lock(p, &argp->lock)))
306 return 0;
307 argp->lock.fl.fl_type = F_UNLCK;
308 return xdr_argsize_check(rqstp, p);
312 nlm4svc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
314 struct nlm_lock *lock = &argp->lock;
315 int len;
317 memset(lock, 0, sizeof(*lock));
318 lock->fl.fl_pid = ~(u32) 0;
320 if (!(p = nlm4_decode_cookie(p, &argp->cookie))
321 || !(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN))
322 || !(p = nlm4_decode_fh(p, &lock->fh))
323 || !(p = nlm4_decode_oh(p, &lock->oh)))
324 return 0;
325 argp->fsm_mode = ntohl(*p++);
326 argp->fsm_access = ntohl(*p++);
327 return xdr_argsize_check(rqstp, p);
331 nlm4svc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
333 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
334 return 0;
335 *p++ = resp->status;
336 *p++ = xdr_zero; /* sequence argument */
337 return xdr_ressize_check(rqstp, p);
341 nlm4svc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
343 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
344 return 0;
345 *p++ = resp->status;
346 return xdr_ressize_check(rqstp, p);
350 nlm4svc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
352 struct nlm_lock *lock = &argp->lock;
353 int len;
355 if (!(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN)))
356 return 0;
357 argp->state = ntohl(*p++);
358 return xdr_argsize_check(rqstp, p);
362 nlm4svc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
364 if (!(p = xdr_decode_string(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
365 return 0;
366 argp->state = ntohl(*p++);
367 argp->addr = ntohl(*p++);
368 return xdr_argsize_check(rqstp, p);
372 nlm4svc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
374 if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
375 return 0;
376 resp->status = ntohl(*p++);
377 return xdr_argsize_check(rqstp, p);
381 nlm4svc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
383 return xdr_argsize_check(rqstp, p);
387 nlm4svc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
389 return xdr_ressize_check(rqstp, p);
393 * Now, the client side XDR functions
395 static int
396 nlm4clt_encode_void(struct rpc_rqst *req, u32 *p, void *ptr)
398 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
399 return 0;
402 static int
403 nlm4clt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
405 return 0;
408 static int
409 nlm4clt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
411 struct nlm_lock *lock = &argp->lock;
413 if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
414 return -EIO;
415 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
416 if (!(p = nlm4_encode_lock(p, lock)))
417 return -EIO;
418 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
419 return 0;
422 static int
423 nlm4clt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
425 if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
426 return -EIO;
427 resp->status = ntohl(*p++);
428 if (resp->status == NLM_LCK_DENIED) {
429 struct file_lock *fl = &resp->lock.fl;
430 u32 excl;
431 s64 start, end, len;
433 memset(&resp->lock, 0, sizeof(resp->lock));
434 excl = ntohl(*p++);
435 fl->fl_pid = ntohl(*p++);
436 if (!(p = nlm4_decode_oh(p, &resp->lock.oh)))
437 return -EIO;
439 fl->fl_flags = FL_POSIX;
440 fl->fl_type = excl? F_WRLCK : F_RDLCK;
441 p = xdr_decode_hyper(p, &start);
442 p = xdr_decode_hyper(p, &len);
443 end = start + len - 1;
445 fl->fl_start = size_to_off_t(start);
446 fl->fl_end = size_to_off_t(end);
447 if (len == 0 || fl->fl_end < 0)
448 fl->fl_end = OFFSET_MAX;
450 return 0;
454 static int
455 nlm4clt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
457 struct nlm_lock *lock = &argp->lock;
459 if (!(p = nlm4_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 = nlm4_encode_lock(p, lock)))
464 return -EIO;
465 *p++ = argp->reclaim? xdr_one : xdr_zero;
466 *p++ = htonl(argp->state);
467 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
468 return 0;
471 static int
472 nlm4clt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
474 struct nlm_lock *lock = &argp->lock;
476 if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
477 return -EIO;
478 *p++ = argp->block? xdr_one : xdr_zero;
479 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
480 if (!(p = nlm4_encode_lock(p, lock)))
481 return -EIO;
482 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
483 return 0;
486 static int
487 nlm4clt_encode_unlockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
489 struct nlm_lock *lock = &argp->lock;
491 if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
492 return -EIO;
493 if (!(p = nlm4_encode_lock(p, lock)))
494 return -EIO;
495 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
496 return 0;
499 static int
500 nlm4clt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
502 if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
503 return -EIO;
504 *p++ = resp->status;
505 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
506 return 0;
509 static int
510 nlm4clt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
512 if (!(p = nlm4_encode_testres(p, resp)))
513 return -EIO;
514 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
515 return 0;
518 static int
519 nlm4clt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
521 if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
522 return -EIO;
523 resp->status = ntohl(*p++);
524 return 0;
528 * Buffer requirements for NLM
530 #define NLM4_void_sz 0
531 #define NLM4_cookie_sz 3 /* 1 len , 2 data */
532 #define NLM4_caller_sz 1+XDR_QUADLEN(NLM_MAXSTRLEN)
533 #define NLM4_netobj_sz 1+XDR_QUADLEN(XDR_MAX_NETOBJ)
534 /* #define NLM4_owner_sz 1+XDR_QUADLEN(NLM4_MAXOWNER) */
535 #define NLM4_fhandle_sz 1+XDR_QUADLEN(NFS3_FHSIZE)
536 #define NLM4_lock_sz 5+NLM4_caller_sz+NLM4_netobj_sz+NLM4_fhandle_sz
537 #define NLM4_holder_sz 6+NLM4_netobj_sz
539 #define NLM4_testargs_sz NLM4_cookie_sz+1+NLM4_lock_sz
540 #define NLM4_lockargs_sz NLM4_cookie_sz+4+NLM4_lock_sz
541 #define NLM4_cancargs_sz NLM4_cookie_sz+2+NLM4_lock_sz
542 #define NLM4_unlockargs_sz NLM4_cookie_sz+NLM4_lock_sz
544 #define NLM4_testres_sz NLM4_cookie_sz+1+NLM4_holder_sz
545 #define NLM4_res_sz NLM4_cookie_sz+1
546 #define NLM4_norep_sz 0
548 #ifndef MAX
549 # define MAX(a,b) (((a) > (b))? (a) : (b))
550 #endif
553 * For NLM, a void procedure really returns nothing
555 #define nlm4clt_decode_norep NULL
557 #define PROC(proc, argtype, restype) \
558 { "nlm4_" #proc, \
559 (kxdrproc_t) nlm4clt_encode_##argtype, \
560 (kxdrproc_t) nlm4clt_decode_##restype, \
561 MAX(NLM4_##argtype##_sz, NLM4_##restype##_sz) << 2, \
565 static struct rpc_procinfo nlm4_procedures[] = {
566 PROC(null, void, void),
567 PROC(test, testargs, testres),
568 PROC(lock, lockargs, res),
569 PROC(canc, cancargs, res),
570 PROC(unlock, unlockargs, res),
571 PROC(granted, testargs, res),
572 PROC(test_msg, testargs, norep),
573 PROC(lock_msg, lockargs, norep),
574 PROC(canc_msg, cancargs, norep),
575 PROC(unlock_msg, unlockargs, norep),
576 PROC(granted_msg, testargs, norep),
577 PROC(test_res, testres, norep),
578 PROC(lock_res, res, norep),
579 PROC(canc_res, res, norep),
580 PROC(unlock_res, res, norep),
581 PROC(granted_res, res, norep),
582 PROC(undef, void, void),
583 PROC(undef, void, void),
584 PROC(undef, void, void),
585 PROC(undef, void, void),
586 #ifdef NLMCLNT_SUPPORT_SHARES
587 PROC(share, shareargs, shareres),
588 PROC(unshare, shareargs, shareres),
589 PROC(nm_lock, lockargs, res),
590 PROC(free_all, notify, void),
591 #else
592 PROC(undef, void, void),
593 PROC(undef, void, void),
594 PROC(undef, void, void),
595 PROC(undef, void, void),
596 #endif
599 struct rpc_version nlm_version4 = {
600 4, 24, nlm4_procedures,