clntxdr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * linux/fs/lockd/clntxdr.c
  3. *
  4. * XDR functions to encode/decode NLM version 3 RPC arguments and results.
  5. * NLM version 3 is backwards compatible with NLM versions 1 and 2.
  6. *
  7. * NLM client-side only.
  8. *
  9. * Copyright (C) 2010, Oracle. All rights reserved.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/sunrpc/xdr.h>
  13. #include <linux/sunrpc/clnt.h>
  14. #include <linux/sunrpc/stats.h>
  15. #include <linux/lockd/lockd.h>
  16. #define NLMDBG_FACILITY NLMDBG_XDR
  17. #if (NLMCLNT_OHSIZE > XDR_MAX_NETOBJ)
  18. # error "NLM host name cannot be larger than XDR_MAX_NETOBJ!"
  19. #endif
  20. /*
  21. * Declare the space requirements for NLM arguments and replies as
  22. * number of 32bit-words
  23. */
  24. #define NLM_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
  25. #define NLM_caller_sz (1+(NLMCLNT_OHSIZE>>2))
  26. #define NLM_owner_sz (1+(NLMCLNT_OHSIZE>>2))
  27. #define NLM_fhandle_sz (1+(NFS2_FHSIZE>>2))
  28. #define NLM_lock_sz (3+NLM_caller_sz+NLM_owner_sz+NLM_fhandle_sz)
  29. #define NLM_holder_sz (4+NLM_owner_sz)
  30. #define NLM_testargs_sz (NLM_cookie_sz+1+NLM_lock_sz)
  31. #define NLM_lockargs_sz (NLM_cookie_sz+4+NLM_lock_sz)
  32. #define NLM_cancargs_sz (NLM_cookie_sz+2+NLM_lock_sz)
  33. #define NLM_unlockargs_sz (NLM_cookie_sz+NLM_lock_sz)
  34. #define NLM_testres_sz (NLM_cookie_sz+1+NLM_holder_sz)
  35. #define NLM_res_sz (NLM_cookie_sz+1)
  36. #define NLM_norep_sz (0)
  37. static s32 loff_t_to_s32(loff_t offset)
  38. {
  39. s32 res;
  40. if (offset >= NLM_OFFSET_MAX)
  41. res = NLM_OFFSET_MAX;
  42. else if (offset <= -NLM_OFFSET_MAX)
  43. res = -NLM_OFFSET_MAX;
  44. else
  45. res = offset;
  46. return res;
  47. }
  48. static void nlm_compute_offsets(const struct nlm_lock *lock,
  49. u32 *l_offset, u32 *l_len)
  50. {
  51. const struct file_lock *fl = &lock->fl;
  52. BUG_ON(fl->fl_start > NLM_OFFSET_MAX);
  53. BUG_ON(fl->fl_end > NLM_OFFSET_MAX &&
  54. fl->fl_end != OFFSET_MAX);
  55. *l_offset = loff_t_to_s32(fl->fl_start);
  56. if (fl->fl_end == OFFSET_MAX)
  57. *l_len = 0;
  58. else
  59. *l_len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
  60. }
  61. /*
  62. * Handle decode buffer overflows out-of-line.
  63. */
  64. static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
  65. {
  66. dprintk("lockd: %s prematurely hit the end of our receive buffer. "
  67. "Remaining buffer length is %tu words.\n",
  68. func, xdr->end - xdr->p);
  69. }
  70. /*
  71. * Encode/decode NLMv3 basic data types
  72. *
  73. * Basic NLMv3 data types are not defined in an IETF standards
  74. * document. X/Open has a description of these data types that
  75. * is useful. See Chapter 10 of "Protocols for Interworking:
  76. * XNFS, Version 3W".
  77. *
  78. * Not all basic data types have their own encoding and decoding
  79. * functions. For run-time efficiency, some data types are encoded
  80. * or decoded inline.
  81. */
  82. static void encode_bool(struct xdr_stream *xdr, const int value)
  83. {
  84. __be32 *p;
  85. p = xdr_reserve_space(xdr, 4);
  86. *p = value ? xdr_one : xdr_zero;
  87. }
  88. static void encode_int32(struct xdr_stream *xdr, const s32 value)
  89. {
  90. __be32 *p;
  91. p = xdr_reserve_space(xdr, 4);
  92. *p = cpu_to_be32(value);
  93. }
  94. /*
  95. * typedef opaque netobj<MAXNETOBJ_SZ>
  96. */
  97. static void encode_netobj(struct xdr_stream *xdr,
  98. const u8 *data, const unsigned int length)
  99. {
  100. __be32 *p;
  101. BUG_ON(length > XDR_MAX_NETOBJ);
  102. p = xdr_reserve_space(xdr, 4 + length);
  103. xdr_encode_opaque(p, data, length);
  104. }
  105. static int decode_netobj(struct xdr_stream *xdr,
  106. struct xdr_netobj *obj)
  107. {
  108. u32 length;
  109. __be32 *p;
  110. p = xdr_inline_decode(xdr, 4);
  111. if (unlikely(p == NULL))
  112. goto out_overflow;
  113. length = be32_to_cpup(p++);
  114. if (unlikely(length > XDR_MAX_NETOBJ))
  115. goto out_size;
  116. obj->len = length;
  117. obj->data = (u8 *)p;
  118. return 0;
  119. out_size:
  120. dprintk("NFS: returned netobj was too long: %u\n", length);
  121. return -EIO;
  122. out_overflow:
  123. print_overflow_msg(__func__, xdr);
  124. return -EIO;
  125. }
  126. /*
  127. * netobj cookie;
  128. */
  129. static void encode_cookie(struct xdr_stream *xdr,
  130. const struct nlm_cookie *cookie)
  131. {
  132. BUG_ON(cookie->len > NLM_MAXCOOKIELEN);
  133. encode_netobj(xdr, (u8 *)&cookie->data, cookie->len);
  134. }
  135. static int decode_cookie(struct xdr_stream *xdr,
  136. struct nlm_cookie *cookie)
  137. {
  138. u32 length;
  139. __be32 *p;
  140. p = xdr_inline_decode(xdr, 4);
  141. if (unlikely(p == NULL))
  142. goto out_overflow;
  143. length = be32_to_cpup(p++);
  144. /* apparently HPUX can return empty cookies */
  145. if (length == 0)
  146. goto out_hpux;
  147. if (length > NLM_MAXCOOKIELEN)
  148. goto out_size;
  149. p = xdr_inline_decode(xdr, length);
  150. if (unlikely(p == NULL))
  151. goto out_overflow;
  152. cookie->len = length;
  153. memcpy(cookie->data, p, length);
  154. return 0;
  155. out_hpux:
  156. cookie->len = 4;
  157. memset(cookie->data, 0, 4);
  158. return 0;
  159. out_size:
  160. dprintk("NFS: returned cookie was too long: %u\n", length);
  161. return -EIO;
  162. out_overflow:
  163. print_overflow_msg(__func__, xdr);
  164. return -EIO;
  165. }
  166. /*
  167. * netobj fh;
  168. */
  169. static void encode_fh(struct xdr_stream *xdr, const struct nfs_fh *fh)
  170. {
  171. BUG_ON(fh->size != NFS2_FHSIZE);
  172. encode_netobj(xdr, (u8 *)&fh->data, NFS2_FHSIZE);
  173. }
  174. /*
  175. * enum nlm_stats {
  176. * LCK_GRANTED = 0,
  177. * LCK_DENIED = 1,
  178. * LCK_DENIED_NOLOCKS = 2,
  179. * LCK_BLOCKED = 3,
  180. * LCK_DENIED_GRACE_PERIOD = 4
  181. * };
  182. *
  183. *
  184. * struct nlm_stat {
  185. * nlm_stats stat;
  186. * };
  187. *
  188. * NB: we don't swap bytes for the NLM status values. The upper
  189. * layers deal directly with the status value in network byte
  190. * order.
  191. */
  192. static void encode_nlm_stat(struct xdr_stream *xdr,
  193. const __be32 stat)
  194. {
  195. __be32 *p;
  196. BUG_ON(be32_to_cpu(stat) > NLM_LCK_DENIED_GRACE_PERIOD);
  197. p = xdr_reserve_space(xdr, 4);
  198. *p = stat;
  199. }
  200. static int decode_nlm_stat(struct xdr_stream *xdr,
  201. __be32 *stat)
  202. {
  203. __be32 *p;
  204. p = xdr_inline_decode(xdr, 4);
  205. if (unlikely(p == NULL))
  206. goto out_overflow;
  207. if (unlikely(*p > nlm_lck_denied_grace_period))
  208. goto out_enum;
  209. *stat = *p;
  210. return 0;
  211. out_enum:
  212. dprintk("%s: server returned invalid nlm_stats value: %u\n",
  213. __func__, be32_to_cpup(p));
  214. return -EIO;
  215. out_overflow:
  216. print_overflow_msg(__func__, xdr);
  217. return -EIO;
  218. }
  219. /*
  220. * struct nlm_holder {
  221. * bool exclusive;
  222. * int uppid;
  223. * netobj oh;
  224. * unsigned l_offset;
  225. * unsigned l_len;
  226. * };
  227. */
  228. static void encode_nlm_holder(struct xdr_stream *xdr,
  229. const struct nlm_res *result)
  230. {
  231. const struct nlm_lock *lock = &result->lock;
  232. u32 l_offset, l_len;
  233. __be32 *p;
  234. encode_bool(xdr, lock->fl.fl_type == F_RDLCK);
  235. encode_int32(xdr, lock->svid);
  236. encode_netobj(xdr, lock->oh.data, lock->oh.len);
  237. p = xdr_reserve_space(xdr, 4 + 4);
  238. nlm_compute_offsets(lock, &l_offset, &l_len);
  239. *p++ = cpu_to_be32(l_offset);
  240. *p = cpu_to_be32(l_len);
  241. }
  242. static int decode_nlm_holder(struct xdr_stream *xdr, struct nlm_res *result)
  243. {
  244. struct nlm_lock *lock = &result->lock;
  245. struct file_lock *fl = &lock->fl;
  246. u32 exclusive, l_offset, l_len;
  247. int error;
  248. __be32 *p;
  249. s32 end;
  250. memset(lock, 0, sizeof(*lock));
  251. locks_init_lock(fl);
  252. p = xdr_inline_decode(xdr, 4 + 4);
  253. if (unlikely(p == NULL))
  254. goto out_overflow;
  255. exclusive = be32_to_cpup(p++);
  256. lock->svid = be32_to_cpup(p);
  257. fl->fl_pid = (pid_t)lock->svid;
  258. error = decode_netobj(xdr, &lock->oh);
  259. if (unlikely(error))
  260. goto out;
  261. p = xdr_inline_decode(xdr, 4 + 4);
  262. if (unlikely(p == NULL))
  263. goto out_overflow;
  264. fl->fl_flags = FL_POSIX;
  265. fl->fl_type = exclusive != 0 ? F_WRLCK : F_RDLCK;
  266. l_offset = be32_to_cpup(p++);
  267. l_len = be32_to_cpup(p);
  268. end = l_offset + l_len - 1;
  269. fl->fl_start = (loff_t)l_offset;
  270. if (l_len == 0 || end < 0)
  271. fl->fl_end = OFFSET_MAX;
  272. else
  273. fl->fl_end = (loff_t)end;
  274. error = 0;
  275. out:
  276. return error;
  277. out_overflow:
  278. print_overflow_msg(__func__, xdr);
  279. return -EIO;
  280. }
  281. /*
  282. * string caller_name<LM_MAXSTRLEN>;
  283. */
  284. static void encode_caller_name(struct xdr_stream *xdr, const char *name)
  285. {
  286. /* NB: client-side does not set lock->len */
  287. u32 length = strlen(name);
  288. __be32 *p;
  289. BUG_ON(length > NLM_MAXSTRLEN);
  290. p = xdr_reserve_space(xdr, 4 + length);
  291. xdr_encode_opaque(p, name, length);
  292. }
  293. /*
  294. * struct nlm_lock {
  295. * string caller_name<LM_MAXSTRLEN>;
  296. * netobj fh;
  297. * netobj oh;
  298. * int uppid;
  299. * unsigned l_offset;
  300. * unsigned l_len;
  301. * };
  302. */
  303. static void encode_nlm_lock(struct xdr_stream *xdr,
  304. const struct nlm_lock *lock)
  305. {
  306. u32 l_offset, l_len;
  307. __be32 *p;
  308. encode_caller_name(xdr, lock->caller);
  309. encode_fh(xdr, &lock->fh);
  310. encode_netobj(xdr, lock->oh.data, lock->oh.len);
  311. p = xdr_reserve_space(xdr, 4 + 4 + 4);
  312. *p++ = cpu_to_be32(lock->svid);
  313. nlm_compute_offsets(lock, &l_offset, &l_len);
  314. *p++ = cpu_to_be32(l_offset);
  315. *p = cpu_to_be32(l_len);
  316. }
  317. /*
  318. * NLMv3 XDR encode functions
  319. *
  320. * NLMv3 argument types are defined in Chapter 10 of The Open Group's
  321. * "Protocols for Interworking: XNFS, Version 3W".
  322. */
  323. /*
  324. * struct nlm_testargs {
  325. * netobj cookie;
  326. * bool exclusive;
  327. * struct nlm_lock alock;
  328. * };
  329. */
  330. static int nlm_xdr_enc_testargs(struct rpc_rqst *req, __be32 *p,
  331. const struct nlm_args *args)
  332. {
  333. const struct nlm_lock *lock = &args->lock;
  334. struct xdr_stream xdr;
  335. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  336. encode_cookie(&xdr, &args->cookie);
  337. encode_bool(&xdr, lock->fl.fl_type == F_WRLCK);
  338. encode_nlm_lock(&xdr, lock);
  339. return 0;
  340. }
  341. /*
  342. * struct nlm_lockargs {
  343. * netobj cookie;
  344. * bool block;
  345. * bool exclusive;
  346. * struct nlm_lock alock;
  347. * bool reclaim;
  348. * int state;
  349. * };
  350. */
  351. static int nlm_xdr_enc_lockargs(struct rpc_rqst *req, __be32 *p,
  352. const struct nlm_args *args)
  353. {
  354. const struct nlm_lock *lock = &args->lock;
  355. struct xdr_stream xdr;
  356. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  357. encode_cookie(&xdr, &args->cookie);
  358. encode_bool(&xdr, args->block);
  359. encode_bool(&xdr, lock->fl.fl_type == F_WRLCK);
  360. encode_nlm_lock(&xdr, lock);
  361. encode_bool(&xdr, args->reclaim);
  362. encode_int32(&xdr, args->state);
  363. return 0;
  364. }
  365. /*
  366. * struct nlm_cancargs {
  367. * netobj cookie;
  368. * bool block;
  369. * bool exclusive;
  370. * struct nlm_lock alock;
  371. * };
  372. */
  373. static int nlm_xdr_enc_cancargs(struct rpc_rqst *req, __be32 *p,
  374. const struct nlm_args *args)
  375. {
  376. const struct nlm_lock *lock = &args->lock;
  377. struct xdr_stream xdr;
  378. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  379. encode_cookie(&xdr, &args->cookie);
  380. encode_bool(&xdr, args->block);
  381. encode_bool(&xdr, lock->fl.fl_type == F_WRLCK);
  382. encode_nlm_lock(&xdr, lock);
  383. return 0;
  384. }
  385. /*
  386. * struct nlm_unlockargs {
  387. * netobj cookie;
  388. * struct nlm_lock alock;
  389. * };
  390. */
  391. static int nlm_xdr_enc_unlockargs(struct rpc_rqst *req, __be32 *p,
  392. const struct nlm_args *args)
  393. {
  394. const struct nlm_lock *lock = &args->lock;
  395. struct xdr_stream xdr;
  396. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  397. encode_cookie(&xdr, &args->cookie);
  398. encode_nlm_lock(&xdr, lock);
  399. return 0;
  400. }
  401. /*
  402. * struct nlm_res {
  403. * netobj cookie;
  404. * nlm_stat stat;
  405. * };
  406. */
  407. static int nlm_xdr_enc_res(struct rpc_rqst *req, __be32 *p,
  408. const struct nlm_res *result)
  409. {
  410. struct xdr_stream xdr;
  411. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  412. encode_cookie(&xdr, &result->cookie);
  413. encode_nlm_stat(&xdr, result->status);
  414. return 0;
  415. }
  416. /*
  417. * union nlm_testrply switch (nlm_stats stat) {
  418. * case LCK_DENIED:
  419. * struct nlm_holder holder;
  420. * default:
  421. * void;
  422. * };
  423. *
  424. * struct nlm_testres {
  425. * netobj cookie;
  426. * nlm_testrply test_stat;
  427. * };
  428. */
  429. static void encode_nlm_testrply(struct xdr_stream *xdr,
  430. const struct nlm_res *result)
  431. {
  432. if (result->status == nlm_lck_denied)
  433. encode_nlm_holder(xdr, result);
  434. }
  435. static int nlm_xdr_enc_testres(struct rpc_rqst *req, __be32 *p,
  436. const struct nlm_res *result)
  437. {
  438. struct xdr_stream xdr;
  439. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  440. encode_cookie(&xdr, &result->cookie);
  441. encode_nlm_stat(&xdr, result->status);
  442. encode_nlm_testrply(&xdr, result);
  443. return 0;
  444. }
  445. /*
  446. * NLMv3 XDR decode functions
  447. *
  448. * NLMv3 result types are defined in Chapter 10 of The Open Group's
  449. * "Protocols for Interworking: XNFS, Version 3W".
  450. */
  451. /*
  452. * union nlm_testrply switch (nlm_stats stat) {
  453. * case LCK_DENIED:
  454. * struct nlm_holder holder;
  455. * default:
  456. * void;
  457. * };
  458. *
  459. * struct nlm_testres {
  460. * netobj cookie;
  461. * nlm_testrply test_stat;
  462. * };
  463. */
  464. static int decode_nlm_testrply(struct xdr_stream *xdr,
  465. struct nlm_res *result)
  466. {
  467. int error;
  468. error = decode_nlm_stat(xdr, &result->status);
  469. if (unlikely(error))
  470. goto out;
  471. if (result->status == nlm_lck_denied)
  472. error = decode_nlm_holder(xdr, result);
  473. out:
  474. return error;
  475. }
  476. static int nlm_xdr_dec_testres(struct rpc_rqst *req, __be32 *p,
  477. struct nlm_res *result)
  478. {
  479. struct xdr_stream xdr;
  480. int error;
  481. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  482. error = decode_cookie(&xdr, &result->cookie);
  483. if (unlikely(error))
  484. goto out;
  485. error = decode_nlm_testrply(&xdr, result);
  486. out:
  487. return error;
  488. }
  489. /*
  490. * struct nlm_res {
  491. * netobj cookie;
  492. * nlm_stat stat;
  493. * };
  494. */
  495. static int nlm_xdr_dec_res(struct rpc_rqst *req, __be32 *p,
  496. struct nlm_res *result)
  497. {
  498. struct xdr_stream xdr;
  499. int error;
  500. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  501. error = decode_cookie(&xdr, &result->cookie);
  502. if (unlikely(error))
  503. goto out;
  504. error = decode_nlm_stat(&xdr, &result->status);
  505. out:
  506. return error;
  507. }
  508. /*
  509. * For NLM, a void procedure really returns nothing
  510. */
  511. #define nlm_xdr_dec_norep NULL
  512. #define PROC(proc, argtype, restype) \
  513. [NLMPROC_##proc] = { \
  514. .p_proc = NLMPROC_##proc, \
  515. .p_encode = (kxdrproc_t)nlm_xdr_enc_##argtype, \
  516. .p_decode = (kxdrproc_t)nlm_xdr_dec_##restype, \
  517. .p_arglen = NLM_##argtype##_sz, \
  518. .p_replen = NLM_##restype##_sz, \
  519. .p_statidx = NLMPROC_##proc, \
  520. .p_name = #proc, \
  521. }
  522. static struct rpc_procinfo nlm_procedures[] = {
  523. PROC(TEST, testargs, testres),
  524. PROC(LOCK, lockargs, res),
  525. PROC(CANCEL, cancargs, res),
  526. PROC(UNLOCK, unlockargs, res),
  527. PROC(GRANTED, testargs, res),
  528. PROC(TEST_MSG, testargs, norep),
  529. PROC(LOCK_MSG, lockargs, norep),
  530. PROC(CANCEL_MSG, cancargs, norep),
  531. PROC(UNLOCK_MSG, unlockargs, norep),
  532. PROC(GRANTED_MSG, testargs, norep),
  533. PROC(TEST_RES, testres, norep),
  534. PROC(LOCK_RES, res, norep),
  535. PROC(CANCEL_RES, res, norep),
  536. PROC(UNLOCK_RES, res, norep),
  537. PROC(GRANTED_RES, res, norep),
  538. };
  539. static struct rpc_version nlm_version1 = {
  540. .number = 1,
  541. .nrprocs = ARRAY_SIZE(nlm_procedures),
  542. .procs = nlm_procedures,
  543. };
  544. static struct rpc_version nlm_version3 = {
  545. .number = 3,
  546. .nrprocs = ARRAY_SIZE(nlm_procedures),
  547. .procs = nlm_procedures,
  548. };
  549. static struct rpc_version *nlm_versions[] = {
  550. [1] = &nlm_version1,
  551. [3] = &nlm_version3,
  552. #ifdef CONFIG_LOCKD_V4
  553. [4] = &nlm_version4,
  554. #endif
  555. };
  556. static struct rpc_stat nlm_rpc_stats;
  557. struct rpc_program nlm_program = {
  558. .name = "lockd",
  559. .number = NLM_PROGRAM,
  560. .nrvers = ARRAY_SIZE(nlm_versions),
  561. .version = nlm_versions,
  562. .stats = &nlm_rpc_stats,
  563. };