xdr4.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * linux/fs/lockd/xdr4.c
  3. *
  4. * XDR support for lockd and the lock client.
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. * Copyright (C) 1999, Trond Myklebust <trond.myklebust@fys.uio.no>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/utsname.h>
  12. #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>
  18. #include <linux/lockd/sm_inter.h>
  19. #define NLMDBG_FACILITY NLMDBG_XDR
  20. static inline loff_t
  21. s64_to_loff_t(__s64 offset)
  22. {
  23. return (loff_t)offset;
  24. }
  25. static inline s64
  26. loff_t_to_s64(loff_t offset)
  27. {
  28. s64 res;
  29. if (offset > NLM4_OFFSET_MAX)
  30. res = NLM4_OFFSET_MAX;
  31. else if (offset < -NLM4_OFFSET_MAX)
  32. res = -NLM4_OFFSET_MAX;
  33. else
  34. res = offset;
  35. return res;
  36. }
  37. /*
  38. * XDR functions for basic NLM types
  39. */
  40. static u32 *
  41. nlm4_decode_cookie(u32 *p, struct nlm_cookie *c)
  42. {
  43. unsigned int len;
  44. len = ntohl(*p++);
  45. if(len==0)
  46. {
  47. c->len=4;
  48. memset(c->data, 0, 4); /* hockeypux brain damage */
  49. }
  50. else if(len<=NLM_MAXCOOKIELEN)
  51. {
  52. c->len=len;
  53. memcpy(c->data, p, len);
  54. p+=XDR_QUADLEN(len);
  55. }
  56. else
  57. {
  58. printk(KERN_NOTICE
  59. "lockd: bad cookie size %d (only cookies under %d bytes are supported.)\n", len, NLM_MAXCOOKIELEN);
  60. return NULL;
  61. }
  62. return p;
  63. }
  64. static u32 *
  65. nlm4_encode_cookie(u32 *p, struct nlm_cookie *c)
  66. {
  67. *p++ = htonl(c->len);
  68. memcpy(p, c->data, c->len);
  69. p+=XDR_QUADLEN(c->len);
  70. return p;
  71. }
  72. static u32 *
  73. nlm4_decode_fh(u32 *p, struct nfs_fh *f)
  74. {
  75. memset(f->data, 0, sizeof(f->data));
  76. f->size = ntohl(*p++);
  77. if (f->size > NFS_MAXFHSIZE) {
  78. printk(KERN_NOTICE
  79. "lockd: bad fhandle size %d (should be <=%d)\n",
  80. f->size, NFS_MAXFHSIZE);
  81. return NULL;
  82. }
  83. memcpy(f->data, p, f->size);
  84. return p + XDR_QUADLEN(f->size);
  85. }
  86. static u32 *
  87. nlm4_encode_fh(u32 *p, struct nfs_fh *f)
  88. {
  89. *p++ = htonl(f->size);
  90. if (f->size) p[XDR_QUADLEN(f->size)-1] = 0; /* don't leak anything */
  91. memcpy(p, f->data, f->size);
  92. return p + XDR_QUADLEN(f->size);
  93. }
  94. /*
  95. * Encode and decode owner handle
  96. */
  97. static u32 *
  98. nlm4_decode_oh(u32 *p, struct xdr_netobj *oh)
  99. {
  100. return xdr_decode_netobj(p, oh);
  101. }
  102. static u32 *
  103. nlm4_encode_oh(u32 *p, struct xdr_netobj *oh)
  104. {
  105. return xdr_encode_netobj(p, oh);
  106. }
  107. static u32 *
  108. nlm4_decode_lock(u32 *p, struct nlm_lock *lock)
  109. {
  110. struct file_lock *fl = &lock->fl;
  111. __s64 len, start, end;
  112. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  113. &lock->len, NLM_MAXSTRLEN))
  114. || !(p = nlm4_decode_fh(p, &lock->fh))
  115. || !(p = nlm4_decode_oh(p, &lock->oh)))
  116. return NULL;
  117. locks_init_lock(fl);
  118. fl->fl_owner = current->files;
  119. fl->fl_pid = ntohl(*p++);
  120. fl->fl_flags = FL_POSIX;
  121. fl->fl_type = F_RDLCK; /* as good as anything else */
  122. p = xdr_decode_hyper(p, &start);
  123. p = xdr_decode_hyper(p, &len);
  124. end = start + len - 1;
  125. fl->fl_start = s64_to_loff_t(start);
  126. if (len == 0 || end < 0)
  127. fl->fl_end = OFFSET_MAX;
  128. else
  129. fl->fl_end = s64_to_loff_t(end);
  130. return p;
  131. }
  132. /*
  133. * Encode a lock as part of an NLM call
  134. */
  135. static u32 *
  136. nlm4_encode_lock(u32 *p, struct nlm_lock *lock)
  137. {
  138. struct file_lock *fl = &lock->fl;
  139. __s64 start, len;
  140. if (!(p = xdr_encode_string(p, lock->caller))
  141. || !(p = nlm4_encode_fh(p, &lock->fh))
  142. || !(p = nlm4_encode_oh(p, &lock->oh)))
  143. return NULL;
  144. if (fl->fl_start > NLM4_OFFSET_MAX
  145. || (fl->fl_end > NLM4_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
  146. return NULL;
  147. *p++ = htonl(fl->fl_pid);
  148. start = loff_t_to_s64(fl->fl_start);
  149. if (fl->fl_end == OFFSET_MAX)
  150. len = 0;
  151. else
  152. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  153. p = xdr_encode_hyper(p, start);
  154. p = xdr_encode_hyper(p, len);
  155. return p;
  156. }
  157. /*
  158. * Encode result of a TEST/TEST_MSG call
  159. */
  160. static u32 *
  161. nlm4_encode_testres(u32 *p, struct nlm_res *resp)
  162. {
  163. s64 start, len;
  164. dprintk("xdr: before encode_testres (p %p resp %p)\n", p, resp);
  165. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  166. return NULL;
  167. *p++ = resp->status;
  168. if (resp->status == nlm_lck_denied) {
  169. struct file_lock *fl = &resp->lock.fl;
  170. *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
  171. *p++ = htonl(fl->fl_pid);
  172. /* Encode owner handle. */
  173. if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
  174. return NULL;
  175. start = loff_t_to_s64(fl->fl_start);
  176. if (fl->fl_end == OFFSET_MAX)
  177. len = 0;
  178. else
  179. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  180. p = xdr_encode_hyper(p, start);
  181. p = xdr_encode_hyper(p, len);
  182. dprintk("xdr: encode_testres (status %d pid %d type %d start %Ld end %Ld)\n",
  183. resp->status, fl->fl_pid, fl->fl_type,
  184. (long long)fl->fl_start, (long long)fl->fl_end);
  185. }
  186. dprintk("xdr: after encode_testres (p %p resp %p)\n", p, resp);
  187. return p;
  188. }
  189. /*
  190. * First, the server side XDR functions
  191. */
  192. int
  193. nlm4svc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  194. {
  195. u32 exclusive;
  196. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  197. return 0;
  198. exclusive = ntohl(*p++);
  199. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  200. return 0;
  201. if (exclusive)
  202. argp->lock.fl.fl_type = F_WRLCK;
  203. return xdr_argsize_check(rqstp, p);
  204. }
  205. int
  206. nlm4svc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  207. {
  208. if (!(p = nlm4_encode_testres(p, resp)))
  209. return 0;
  210. return xdr_ressize_check(rqstp, p);
  211. }
  212. int
  213. nlm4svc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  214. {
  215. u32 exclusive;
  216. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  217. return 0;
  218. argp->block = ntohl(*p++);
  219. exclusive = ntohl(*p++);
  220. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  221. return 0;
  222. if (exclusive)
  223. argp->lock.fl.fl_type = F_WRLCK;
  224. argp->reclaim = ntohl(*p++);
  225. argp->state = ntohl(*p++);
  226. argp->monitor = 1; /* monitor client by default */
  227. return xdr_argsize_check(rqstp, p);
  228. }
  229. int
  230. nlm4svc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  231. {
  232. u32 exclusive;
  233. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  234. return 0;
  235. argp->block = ntohl(*p++);
  236. exclusive = ntohl(*p++);
  237. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  238. return 0;
  239. if (exclusive)
  240. argp->lock.fl.fl_type = F_WRLCK;
  241. return xdr_argsize_check(rqstp, p);
  242. }
  243. int
  244. nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  245. {
  246. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  247. || !(p = nlm4_decode_lock(p, &argp->lock)))
  248. return 0;
  249. argp->lock.fl.fl_type = F_UNLCK;
  250. return xdr_argsize_check(rqstp, p);
  251. }
  252. int
  253. nlm4svc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  254. {
  255. struct nlm_lock *lock = &argp->lock;
  256. memset(lock, 0, sizeof(*lock));
  257. locks_init_lock(&lock->fl);
  258. lock->fl.fl_pid = ~(u32) 0;
  259. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  260. || !(p = xdr_decode_string_inplace(p, &lock->caller,
  261. &lock->len, NLM_MAXSTRLEN))
  262. || !(p = nlm4_decode_fh(p, &lock->fh))
  263. || !(p = nlm4_decode_oh(p, &lock->oh)))
  264. return 0;
  265. argp->fsm_mode = ntohl(*p++);
  266. argp->fsm_access = ntohl(*p++);
  267. return xdr_argsize_check(rqstp, p);
  268. }
  269. int
  270. nlm4svc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  271. {
  272. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  273. return 0;
  274. *p++ = resp->status;
  275. *p++ = xdr_zero; /* sequence argument */
  276. return xdr_ressize_check(rqstp, p);
  277. }
  278. int
  279. nlm4svc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  280. {
  281. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  282. return 0;
  283. *p++ = resp->status;
  284. return xdr_ressize_check(rqstp, p);
  285. }
  286. int
  287. nlm4svc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
  288. {
  289. struct nlm_lock *lock = &argp->lock;
  290. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  291. &lock->len, NLM_MAXSTRLEN)))
  292. return 0;
  293. argp->state = ntohl(*p++);
  294. return xdr_argsize_check(rqstp, p);
  295. }
  296. int
  297. nlm4svc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
  298. {
  299. if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
  300. return 0;
  301. argp->state = ntohl(*p++);
  302. /* Preserve the address in network byte order */
  303. argp->addr = *p++;
  304. return xdr_argsize_check(rqstp, p);
  305. }
  306. int
  307. nlm4svc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  308. {
  309. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  310. return 0;
  311. resp->status = ntohl(*p++);
  312. return xdr_argsize_check(rqstp, p);
  313. }
  314. int
  315. nlm4svc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
  316. {
  317. return xdr_argsize_check(rqstp, p);
  318. }
  319. int
  320. nlm4svc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
  321. {
  322. return xdr_ressize_check(rqstp, p);
  323. }
  324. /*
  325. * Now, the client side XDR functions
  326. */
  327. #ifdef NLMCLNT_SUPPORT_SHARES
  328. static int
  329. nlm4clt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
  330. {
  331. return 0;
  332. }
  333. #endif
  334. static int
  335. nlm4clt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  336. {
  337. struct nlm_lock *lock = &argp->lock;
  338. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  339. return -EIO;
  340. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  341. if (!(p = nlm4_encode_lock(p, lock)))
  342. return -EIO;
  343. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  344. return 0;
  345. }
  346. static int
  347. nlm4clt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  348. {
  349. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  350. return -EIO;
  351. resp->status = ntohl(*p++);
  352. if (resp->status == NLM_LCK_DENIED) {
  353. struct file_lock *fl = &resp->lock.fl;
  354. u32 excl;
  355. s64 start, end, len;
  356. memset(&resp->lock, 0, sizeof(resp->lock));
  357. locks_init_lock(fl);
  358. excl = ntohl(*p++);
  359. fl->fl_pid = ntohl(*p++);
  360. if (!(p = nlm4_decode_oh(p, &resp->lock.oh)))
  361. return -EIO;
  362. fl->fl_flags = FL_POSIX;
  363. fl->fl_type = excl? F_WRLCK : F_RDLCK;
  364. p = xdr_decode_hyper(p, &start);
  365. p = xdr_decode_hyper(p, &len);
  366. end = start + len - 1;
  367. fl->fl_start = s64_to_loff_t(start);
  368. if (len == 0 || end < 0)
  369. fl->fl_end = OFFSET_MAX;
  370. else
  371. fl->fl_end = s64_to_loff_t(end);
  372. }
  373. return 0;
  374. }
  375. static int
  376. nlm4clt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  377. {
  378. struct nlm_lock *lock = &argp->lock;
  379. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  380. return -EIO;
  381. *p++ = argp->block? xdr_one : xdr_zero;
  382. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  383. if (!(p = nlm4_encode_lock(p, lock)))
  384. return -EIO;
  385. *p++ = argp->reclaim? xdr_one : xdr_zero;
  386. *p++ = htonl(argp->state);
  387. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  388. return 0;
  389. }
  390. static int
  391. nlm4clt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  392. {
  393. struct nlm_lock *lock = &argp->lock;
  394. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  395. return -EIO;
  396. *p++ = argp->block? xdr_one : xdr_zero;
  397. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  398. if (!(p = nlm4_encode_lock(p, lock)))
  399. return -EIO;
  400. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  401. return 0;
  402. }
  403. static int
  404. nlm4clt_encode_unlockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  405. {
  406. struct nlm_lock *lock = &argp->lock;
  407. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  408. return -EIO;
  409. if (!(p = nlm4_encode_lock(p, lock)))
  410. return -EIO;
  411. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  412. return 0;
  413. }
  414. static int
  415. nlm4clt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  416. {
  417. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  418. return -EIO;
  419. *p++ = resp->status;
  420. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  421. return 0;
  422. }
  423. static int
  424. nlm4clt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  425. {
  426. if (!(p = nlm4_encode_testres(p, resp)))
  427. return -EIO;
  428. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  429. return 0;
  430. }
  431. static int
  432. nlm4clt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  433. {
  434. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  435. return -EIO;
  436. resp->status = ntohl(*p++);
  437. return 0;
  438. }
  439. /*
  440. * Buffer requirements for NLM
  441. */
  442. #define NLM4_void_sz 0
  443. #define NLM4_cookie_sz 1+XDR_QUADLEN(NLM_MAXCOOKIELEN)
  444. #define NLM4_caller_sz 1+XDR_QUADLEN(NLM_MAXSTRLEN)
  445. #define NLM4_netobj_sz 1+XDR_QUADLEN(XDR_MAX_NETOBJ)
  446. /* #define NLM4_owner_sz 1+XDR_QUADLEN(NLM4_MAXOWNER) */
  447. #define NLM4_fhandle_sz 1+XDR_QUADLEN(NFS3_FHSIZE)
  448. #define NLM4_lock_sz 5+NLM4_caller_sz+NLM4_netobj_sz+NLM4_fhandle_sz
  449. #define NLM4_holder_sz 6+NLM4_netobj_sz
  450. #define NLM4_testargs_sz NLM4_cookie_sz+1+NLM4_lock_sz
  451. #define NLM4_lockargs_sz NLM4_cookie_sz+4+NLM4_lock_sz
  452. #define NLM4_cancargs_sz NLM4_cookie_sz+2+NLM4_lock_sz
  453. #define NLM4_unlockargs_sz NLM4_cookie_sz+NLM4_lock_sz
  454. #define NLM4_testres_sz NLM4_cookie_sz+1+NLM4_holder_sz
  455. #define NLM4_res_sz NLM4_cookie_sz+1
  456. #define NLM4_norep_sz 0
  457. #ifndef MAX
  458. # define MAX(a,b) (((a) > (b))? (a) : (b))
  459. #endif
  460. /*
  461. * For NLM, a void procedure really returns nothing
  462. */
  463. #define nlm4clt_decode_norep NULL
  464. #define PROC(proc, argtype, restype) \
  465. [NLMPROC_##proc] = { \
  466. .p_proc = NLMPROC_##proc, \
  467. .p_encode = (kxdrproc_t) nlm4clt_encode_##argtype, \
  468. .p_decode = (kxdrproc_t) nlm4clt_decode_##restype, \
  469. .p_bufsiz = MAX(NLM4_##argtype##_sz, NLM4_##restype##_sz) << 2 \
  470. }
  471. static struct rpc_procinfo nlm4_procedures[] = {
  472. PROC(TEST, testargs, testres),
  473. PROC(LOCK, lockargs, res),
  474. PROC(CANCEL, cancargs, res),
  475. PROC(UNLOCK, unlockargs, res),
  476. PROC(GRANTED, testargs, res),
  477. PROC(TEST_MSG, testargs, norep),
  478. PROC(LOCK_MSG, lockargs, norep),
  479. PROC(CANCEL_MSG, cancargs, norep),
  480. PROC(UNLOCK_MSG, unlockargs, norep),
  481. PROC(GRANTED_MSG, testargs, norep),
  482. PROC(TEST_RES, testres, norep),
  483. PROC(LOCK_RES, res, norep),
  484. PROC(CANCEL_RES, res, norep),
  485. PROC(UNLOCK_RES, res, norep),
  486. PROC(GRANTED_RES, res, norep),
  487. #ifdef NLMCLNT_SUPPORT_SHARES
  488. PROC(SHARE, shareargs, shareres),
  489. PROC(UNSHARE, shareargs, shareres),
  490. PROC(NM_LOCK, lockargs, res),
  491. PROC(FREE_ALL, notify, void),
  492. #endif
  493. };
  494. struct rpc_version nlm_version4 = {
  495. .number = 4,
  496. .nrprocs = 24,
  497. .procs = nlm4_procedures,
  498. };