1 /*
2 * linux/fs/lockd/xdr.c
3 *
4 * XDR support for lockd and the lock client.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/utsname.h>
13 #include <linux/nfs.h>
14
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>
21
22 #define NLMDBG_FACILITY NLMDBG_XDR
23
24
25 static inline loff_t
26 s32_to_loff_t(__s32 offset)
27 {
28 return (loff_t)offset;
29 }
30
31 static inline __s32
32 loff_t_to_s32(loff_t offset)
33 {
34 __s32 res;
35 if (offset >= NLM_OFFSET_MAX)
36 res = NLM_OFFSET_MAX;
37 else if (offset <= -NLM_OFFSET_MAX)
38 res = -NLM_OFFSET_MAX;
39 else
40 res = offset;
41 return res;
42 }
43
44 /*
45 * XDR functions for basic NLM types
46 */
47 static inline u32 *nlm_decode_cookie(u32 *p, struct nlm_cookie *c)
48 {
49 unsigned int len;
50
51 len = ntohl(*p++);
52
53 if(len==0)
54 {
55 c->len=4;
56 memset(c->data, 0, 4); /* hockeypux brain damage */
57 }
58 else if(len<=8)
59 {
60 c->len=len;
61 memcpy(c->data, p, len);
62 p+=(len+3)>>2;
63 }
64 else
65 {
66 printk(KERN_NOTICE
67 "lockd: bad cookie size %d (only cookies under 8 bytes are supported.)\n", len);
68 return NULL;
69 }
70 return p;
71 }
72
73 static inline u32 *
74 nlm_encode_cookie(u32 *p, struct nlm_cookie *c)
75 {
76 *p++ = htonl(c->len);
77 memcpy(p, c->data, c->len);
78 p+=(c->len+3)>>2;
79 return p;
80 }
81
82 static inline u32 *
83 nlm_decode_fh(u32 *p, struct nfs_fh *f)
84 {
85 unsigned int len;
86
87 if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
88 printk(KERN_NOTICE
89 "lockd: bad fhandle size %x (should be %d)\n",
90 len, NFS2_FHSIZE);
91 return NULL;
92 }
93 f->size = NFS2_FHSIZE;
94 memcpy(f->data, p, NFS2_FHSIZE);
95 return p + XDR_QUADLEN(NFS2_FHSIZE);
96 }
97
98 static inline u32 *
99 nlm_encode_fh(u32 *p, struct nfs_fh *f)
100 {
101 *p++ = htonl(NFS2_FHSIZE);
102 memcpy(p, f->data, NFS2_FHSIZE);
103 return p + XDR_QUADLEN(NFS2_FHSIZE);
104 }
105
106 /*
107 * Encode and decode owner handle
108 */
109 static inline u32 *
110 nlm_decode_oh(u32 *p, struct xdr_netobj *oh)
111 {
112 return xdr_decode_netobj(p, oh);
113 }
114
115 static inline u32 *
116 nlm_encode_oh(u32 *p, struct xdr_netobj *oh)
117 {
118 return xdr_encode_netobj(p, oh);
119 }
120
121 static inline u32 *
122 nlm_decode_lock(u32 *p, struct nlm_lock *lock)
123 {
124 struct file_lock *fl = &lock->fl;
125 s32 start, len, end;
126
127 if (!(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN))
128 || !(p = nlm_decode_fh(p, &lock->fh))
129 || !(p = nlm_decode_oh(p, &lock->oh)))
130 return NULL;
131
132 locks_init_lock(fl);
133 fl->fl_owner = current->files;
134 fl->fl_pid = ntohl(*p++);
135 fl->fl_flags = FL_POSIX;
136 fl->fl_type = F_RDLCK; /* as good as anything else */
137 start = ntohl(*p++);
138 len = ntohl(*p++);
139 end = start + len - 1;
140
141 fl->fl_start = s32_to_loff_t(start);
142
143 if (len == 0 || end < 0)
144 fl->fl_end = OFFSET_MAX;
145 else
146 fl->fl_end = s32_to_loff_t(end);
147 return p;
148 }
149
150 /*
151 * Encode a lock as part of an NLM call
152 */
153 static u32 *
154 nlm_encode_lock(u32 *p, struct nlm_lock *lock)
155 {
156 struct file_lock *fl = &lock->fl;
157 __s32 start, len;
158
159 if (!(p = xdr_encode_string(p, lock->caller))
160 || !(p = nlm_encode_fh(p, &lock->fh))
161 || !(p = nlm_encode_oh(p, &lock->oh)))
162 return NULL;
163
164 if (fl->fl_start > NLM_OFFSET_MAX
165 || (fl->fl_end > NLM_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
166 return NULL;
167
168 start = loff_t_to_s32(fl->fl_start);
169 if (fl->fl_end == OFFSET_MAX)
170 len = 0;
171 else
172 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
173
174 *p++ = htonl(fl->fl_pid);
175 *p++ = htonl(start);
176 *p++ = htonl(len);
177
178 return p;
179 }
180
181 /*
182 * Encode result of a TEST/TEST_MSG call
183 */
184 static u32 *
185 nlm_encode_testres(u32 *p, struct nlm_res *resp)
186 {
187 s32 start, len;
188
189 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
190 return 0;
191 *p++ = resp->status;
192
193 if (resp->status == nlm_lck_denied) {
194 struct file_lock *fl = &resp->lock.fl;
195
196 *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
197 *p++ = htonl(fl->fl_pid);
198
199 /* Encode owner handle. */
200 if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
201 return 0;
202
203 start = loff_t_to_s32(fl->fl_start);
204 if (fl->fl_end == OFFSET_MAX)
205 len = 0;
206 else
207 len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
208
209 *p++ = htonl(start);
210 *p++ = htonl(len);
211 }
212
213 return p;
214 }
215
216 /*
217 * Check buffer bounds after decoding arguments
218 */
219 static inline int
220 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
221 {
222 struct svc_buf *buf = &rqstp->rq_argbuf;
223
224 return p - buf->base <= buf->buflen;
225 }
226
227 static inline int
228 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
229 {
230 struct svc_buf *buf = &rqstp->rq_resbuf;
231
232 buf->len = p - buf->base;
233 return (buf->len <= buf->buflen);
234 }
235
236 /*
237 * First, the server side XDR functions
238 */
239 int
240 nlmsvc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
241 {
242 u32 exclusive;
243
244 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
245 return 0;
246
247 exclusive = ntohl(*p++);
248 if (!(p = nlm_decode_lock(p, &argp->lock)))
249 return 0;
250 if (exclusive)
251 argp->lock.fl.fl_type = F_WRLCK;
252
253 return xdr_argsize_check(rqstp, p);
254 }
255
256 int
257 nlmsvc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
258 {
259 if (!(p = nlm_encode_testres(p, resp)))
260 return 0;
261 return xdr_ressize_check(rqstp, p);
262 }
263
264 int
265 nlmsvc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
266 {
267 u32 exclusive;
268
269 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
270 return 0;
271 argp->block = ntohl(*p++);
272 exclusive = ntohl(*p++);
273 if (!(p = nlm_decode_lock(p, &argp->lock)))
274 return 0;
275 if (exclusive)
276 argp->lock.fl.fl_type = F_WRLCK;
277 argp->reclaim = ntohl(*p++);
278 argp->state = ntohl(*p++);
279 argp->monitor = 1; /* monitor client by default */
280
281 return xdr_argsize_check(rqstp, p);
282 }
283
284 int
285 nlmsvc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
286 {
287 u32 exclusive;
288
289 if (!(p = nlm_decode_cookie(p, &argp->cookie)))
290 return 0;
291 argp->block = ntohl(*p++);
292 exclusive = ntohl(*p++);
293 if (!(p = nlm_decode_lock(p, &argp->lock)))
294 return 0;
295 if (exclusive)
296 argp->lock.fl.fl_type = F_WRLCK;
297 return xdr_argsize_check(rqstp, p);
298 }
299
300 int
301 nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
302 {
303 if (!(p = nlm_decode_cookie(p, &argp->cookie))
304 || !(p = nlm_decode_lock(p, &argp->lock)))
305 return 0;
306 argp->lock.fl.fl_type = F_UNLCK;
307 return xdr_argsize_check(rqstp, p);
308 }
309
310 int
311 nlmsvc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
312 {
313 struct nlm_lock *lock = &argp->lock;
314 int len;
315
316 memset(lock, 0, sizeof(*lock));
317 locks_init_lock(&lock->fl);
318 lock->fl.fl_pid = ~(u32) 0;
319
320 if (!(p = nlm_decode_cookie(p, &argp->cookie))
321 || !(p = xdr_decode_string(p, &lock->caller, &len, NLM_MAXSTRLEN))
322 || !(p = nlm_decode_fh(p, &lock->fh))
323 || !(p = nlm_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);
328 }
329
330 int
331 nlmsvc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
332 {
333 if (!(p = nlm_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);
338 }
339
340 int
341 nlmsvc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
342 {
343 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
344 return 0;
345 *p++ = resp->status;
346 return xdr_ressize_check(rqstp, p);
347 }
348
349 int
350 nlmsvc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
351 {
352 struct nlm_lock *lock = &argp->lock;
353 int len;
354
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);
359 }
360
361 int
362 nlmsvc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
363 {
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);
369 }
370
371 int
372 nlmsvc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
373 {
374 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
375 return 0;
376 resp->status = ntohl(*p++);
377 return xdr_argsize_check(rqstp, p);
378 }
379
380 int
381 nlmsvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
382 {
383 return xdr_argsize_check(rqstp, p);
384 }
385
386 int
387 nlmsvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
388 {
389 return xdr_ressize_check(rqstp, p);
390 }
391
392 /*
393 * Now, the client side XDR functions
394 */
395 static int
396 nlmclt_encode_void(struct rpc_rqst *req, u32 *p, void *ptr)
397 {
398 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
399 return 0;
400 }
401
402 static int
403 nlmclt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
404 {
405 return 0;
406 }
407
408 static int
409 nlmclt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
410 {
411 struct nlm_lock *lock = &argp->lock;
412
413 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
414 return -EIO;
415 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
416 if (!(p = nlm_encode_lock(p, lock)))
417 return -EIO;
418 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
419 return 0;
420 }
421
422 static int
423 nlmclt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
424 {
425 if (!(p = nlm_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 s32 start, len, end;
432
433 memset(&resp->lock, 0, sizeof(resp->lock));
434 locks_init_lock(fl);
435 excl = ntohl(*p++);
436 fl->fl_pid = ntohl(*p++);
437 if (!(p = nlm_decode_oh(p, &resp->lock.oh)))
438 return -EIO;
439
440 fl->fl_flags = FL_POSIX;
441 fl->fl_type = excl? F_WRLCK : F_RDLCK;
442 start = ntohl(*p++);
443 len = ntohl(*p++);
444 end = start + len - 1;
445
446 fl->fl_start = s32_to_loff_t(start);
447 if (len == 0 || end < 0)
448 fl->fl_end = OFFSET_MAX;
449 else
450 fl->fl_end = s32_to_loff_t(end);
451 }
452 return 0;
453 }
454
455
456 static int
457 nlmclt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
458 {
459 struct nlm_lock *lock = &argp->lock;
460
461 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
462 return -EIO;
463 *p++ = argp->block? xdr_one : xdr_zero;
464 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
465 if (!(p = nlm_encode_lock(p, lock)))
466 return -EIO;
467 *p++ = argp->reclaim? xdr_one : xdr_zero;
468 *p++ = htonl(argp->state);
469 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
470 return 0;
471 }
472
473 static int
474 nlmclt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
475 {
476 struct nlm_lock *lock = &argp->lock;
477
478 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
479 return -EIO;
480 *p++ = argp->block? xdr_one : xdr_zero;
481 *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
482 if (!(p = nlm_encode_lock(p, lock)))
483 return -EIO;
484 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
485 return 0;
486 }
487
488 static int
489 nlmclt_encode_unlockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
490 {
491 struct nlm_lock *lock = &argp->lock;
492
493 if (!(p = nlm_encode_cookie(p, &argp->cookie)))
494 return -EIO;
495 if (!(p = nlm_encode_lock(p, lock)))
496 return -EIO;
497 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
498 return 0;
499 }
500
501 static int
502 nlmclt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
503 {
504 if (!(p = nlm_encode_cookie(p, &resp->cookie)))
505 return -EIO;
506 *p++ = resp->status;
507 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
508 return 0;
509 }
510
511 static int
512 nlmclt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
513 {
514 if (!(p = nlm_encode_testres(p, resp)))
515 return -EIO;
516 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
517 return 0;
518 }
519
520 static int
521 nlmclt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
522 {
523 if (!(p = nlm_decode_cookie(p, &resp->cookie)))
524 return -EIO;
525 resp->status = ntohl(*p++);
526 return 0;
527 }
528
529 /*
530 * Buffer requirements for NLM
531 */
532 #define NLM_void_sz 0
533 #define NLM_cookie_sz 3 /* 1 len , 2 data */
534 #define NLM_caller_sz 1+QUADLEN(sizeof(system_utsname.nodename))
535 #define NLM_netobj_sz 1+QUADLEN(XDR_MAX_NETOBJ)
536 /* #define NLM_owner_sz 1+QUADLEN(NLM_MAXOWNER) */
537 #define NLM_fhandle_sz 1+QUADLEN(NFS2_FHSIZE)
538 #define NLM_lock_sz 3+NLM_caller_sz+NLM_netobj_sz+NLM_fhandle_sz
539 #define NLM_holder_sz 4+NLM_netobj_sz
540
541 #define NLM_testargs_sz NLM_cookie_sz+1+NLM_lock_sz
542 #define NLM_lockargs_sz NLM_cookie_sz+4+NLM_lock_sz
543 #define NLM_cancargs_sz NLM_cookie_sz+2+NLM_lock_sz
544 #define NLM_unlockargs_sz NLM_cookie_sz+NLM_lock_sz
545
546 #define NLM_testres_sz NLM_cookie_sz+1+NLM_holder_sz
547 #define NLM_res_sz NLM_cookie_sz+1
548 #define NLM_norep_sz 0
549
550 #ifndef MAX
551 # define MAX(a, b) (((a) > (b))? (a) : (b))
552 #endif
553
554 /*
555 * For NLM, a void procedure really returns nothing
556 */
557 #define nlmclt_decode_norep NULL
558
559 #define PROC(proc, argtype, restype) \
560 { "nlm_" #proc, \
561 (kxdrproc_t) nlmclt_encode_##argtype, \
562 (kxdrproc_t) nlmclt_decode_##restype, \
563 MAX(NLM_##argtype##_sz, NLM_##restype##_sz) << 2, \
564 0 \
565 }
566
567 static struct rpc_procinfo nlm_procedures[] = {
568 PROC(null, void, void),
569 PROC(test, testargs, testres),
570 PROC(lock, lockargs, res),
571 PROC(canc, cancargs, res),
572 PROC(unlock, unlockargs, res),
573 PROC(granted, testargs, res),
574 PROC(test_msg, testargs, norep),
575 PROC(lock_msg, lockargs, norep),
576 PROC(canc_msg, cancargs, norep),
577 PROC(unlock_msg, unlockargs, norep),
578 PROC(granted_msg, testargs, norep),
579 PROC(test_res, testres, norep),
580 PROC(lock_res, res, norep),
581 PROC(canc_res, res, norep),
582 PROC(unlock_res, res, norep),
583 PROC(granted_res, res, norep),
584 PROC(undef, void, void),
585 PROC(undef, void, void),
586 PROC(undef, void, void),
587 PROC(undef, void, void),
588 #ifdef NLMCLNT_SUPPORT_SHARES
589 PROC(share, shareargs, shareres),
590 PROC(unshare, shareargs, shareres),
591 PROC(nm_lock, lockargs, res),
592 PROC(free_all, notify, void),
593 #else
594 PROC(undef, void, void),
595 PROC(undef, void, void),
596 PROC(undef, void, void),
597 PROC(undef, void, void),
598 #endif
599 };
600
601 static struct rpc_version nlm_version1 = {
602 1, 16, nlm_procedures,
603 };
604
605 static struct rpc_version nlm_version3 = {
606 3, 24, nlm_procedures,
607 };
608
609 #ifdef CONFIG_LOCKD_V4
610 extern struct rpc_version nlm_version4;
611 #endif
612
613 static struct rpc_version * nlm_versions[] = {
614 NULL,
615 &nlm_version1,
616 NULL,
617 &nlm_version3,
618 #ifdef CONFIG_LOCKD_V4
619 &nlm_version4,
620 #endif
621 };
622
623 static struct rpc_stat nlm_stats;
624
625 struct rpc_program nlm_program = {
626 "lockd",
627 NLM_PROGRAM,
628 sizeof(nlm_versions) / sizeof(nlm_versions[0]),
629 nlm_versions,
630 &nlm_stats,
631 };
632
633 #ifdef LOCKD_DEBUG
634 char *
635 nlm_procname(u32 proc)
636 {
637 if (proc < sizeof(nlm_procedures)/sizeof(nlm_procedures[0]))
638 return nlm_procedures[proc].p_procname;
639 return "unknown";
640 }
641 #endif
642
643
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.