clntxdr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. WARN_ON_ONCE(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(ntohl(*p) > ntohl(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 void nlm_xdr_enc_testargs(struct rpc_rqst *req,
  331. struct xdr_stream *xdr,
  332. const struct nlm_args *args)
  333. {
  334. const struct nlm_lock *lock = &args->lock;
  335. encode_cookie(xdr, &args->cookie);
  336. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  337. encode_nlm_lock(xdr, lock);
  338. }
  339. /*
  340. * struct nlm_lockargs {
  341. * netobj cookie;
  342. * bool block;
  343. * bool exclusive;
  344. * struct nlm_lock alock;
  345. * bool reclaim;
  346. * int state;
  347. * };
  348. */
  349. static void nlm_xdr_enc_lockargs(struct rpc_rqst *req,
  350. struct xdr_stream *xdr,
  351. const struct nlm_args *args)
  352. {
  353. const struct nlm_lock *lock = &args->lock;
  354. encode_cookie(xdr, &args->cookie);
  355. encode_bool(xdr, args->block);
  356. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  357. encode_nlm_lock(xdr, lock);
  358. encode_bool(xdr, args->reclaim);
  359. encode_int32(xdr, args->state);
  360. }
  361. /*
  362. * struct nlm_cancargs {
  363. * netobj cookie;
  364. * bool block;
  365. * bool exclusive;
  366. * struct nlm_lock alock;
  367. * };
  368. */
  369. static void nlm_xdr_enc_cancargs(struct rpc_rqst *req,
  370. struct xdr_stream *xdr,
  371. const struct nlm_args *args)
  372. {
  373. const struct nlm_lock *lock = &args->lock;
  374. encode_cookie(xdr, &args->cookie);
  375. encode_bool(xdr, args->block);
  376. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  377. encode_nlm_lock(xdr, lock);
  378. }
  379. /*
  380. * struct nlm_unlockargs {
  381. * netobj cookie;
  382. * struct nlm_lock alock;
  383. * };
  384. */
  385. static void nlm_xdr_enc_unlockargs(struct rpc_rqst *req,
  386. struct xdr_stream *xdr,
  387. const struct nlm_args *args)
  388. {
  389. const struct nlm_lock *lock = &args->lock;
  390. encode_cookie(xdr, &args->cookie);
  391. encode_nlm_lock(xdr, lock);
  392. }
  393. /*
  394. * struct nlm_res {
  395. * netobj cookie;
  396. * nlm_stat stat;
  397. * };
  398. */
  399. static void nlm_xdr_enc_res(struct rpc_rqst *req,
  400. struct xdr_stream *xdr,
  401. const struct nlm_res *result)
  402. {
  403. encode_cookie(xdr, &result->cookie);
  404. encode_nlm_stat(xdr, result->status);
  405. }
  406. /*
  407. * union nlm_testrply switch (nlm_stats stat) {
  408. * case LCK_DENIED:
  409. * struct nlm_holder holder;
  410. * default:
  411. * void;
  412. * };
  413. *
  414. * struct nlm_testres {
  415. * netobj cookie;
  416. * nlm_testrply test_stat;
  417. * };
  418. */
  419. static void encode_nlm_testrply(struct xdr_stream *xdr,
  420. const struct nlm_res *result)
  421. {
  422. if (result->status == nlm_lck_denied)
  423. encode_nlm_holder(xdr, result);
  424. }
  425. static void nlm_xdr_enc_testres(struct rpc_rqst *req,
  426. struct xdr_stream *xdr,
  427. const struct nlm_res *result)
  428. {
  429. encode_cookie(xdr, &result->cookie);
  430. encode_nlm_stat(xdr, result->status);
  431. encode_nlm_testrply(xdr, result);
  432. }
  433. /*
  434. * NLMv3 XDR decode functions
  435. *
  436. * NLMv3 result types are defined in Chapter 10 of The Open Group's
  437. * "Protocols for Interworking: XNFS, Version 3W".
  438. */
  439. /*
  440. * union nlm_testrply switch (nlm_stats stat) {
  441. * case LCK_DENIED:
  442. * struct nlm_holder holder;
  443. * default:
  444. * void;
  445. * };
  446. *
  447. * struct nlm_testres {
  448. * netobj cookie;
  449. * nlm_testrply test_stat;
  450. * };
  451. */
  452. static int decode_nlm_testrply(struct xdr_stream *xdr,
  453. struct nlm_res *result)
  454. {
  455. int error;
  456. error = decode_nlm_stat(xdr, &result->status);
  457. if (unlikely(error))
  458. goto out;
  459. if (result->status == nlm_lck_denied)
  460. error = decode_nlm_holder(xdr, result);
  461. out:
  462. return error;
  463. }
  464. static int nlm_xdr_dec_testres(struct rpc_rqst *req,
  465. struct xdr_stream *xdr,
  466. struct nlm_res *result)
  467. {
  468. int error;
  469. error = decode_cookie(xdr, &result->cookie);
  470. if (unlikely(error))
  471. goto out;
  472. error = decode_nlm_testrply(xdr, result);
  473. out:
  474. return error;
  475. }
  476. /*
  477. * struct nlm_res {
  478. * netobj cookie;
  479. * nlm_stat stat;
  480. * };
  481. */
  482. static int nlm_xdr_dec_res(struct rpc_rqst *req,
  483. struct xdr_stream *xdr,
  484. struct nlm_res *result)
  485. {
  486. int error;
  487. error = decode_cookie(xdr, &result->cookie);
  488. if (unlikely(error))
  489. goto out;
  490. error = decode_nlm_stat(xdr, &result->status);
  491. out:
  492. return error;
  493. }
  494. /*
  495. * For NLM, a void procedure really returns nothing
  496. */
  497. #define nlm_xdr_dec_norep NULL
  498. #define PROC(proc, argtype, restype) \
  499. [NLMPROC_##proc] = { \
  500. .p_proc = NLMPROC_##proc, \
  501. .p_encode = (kxdreproc_t)nlm_xdr_enc_##argtype, \
  502. .p_decode = (kxdrdproc_t)nlm_xdr_dec_##restype, \
  503. .p_arglen = NLM_##argtype##_sz, \
  504. .p_replen = NLM_##restype##_sz, \
  505. .p_statidx = NLMPROC_##proc, \
  506. .p_name = #proc, \
  507. }
  508. static struct rpc_procinfo nlm_procedures[] = {
  509. PROC(TEST, testargs, testres),
  510. PROC(LOCK, lockargs, res),
  511. PROC(CANCEL, cancargs, res),
  512. PROC(UNLOCK, unlockargs, res),
  513. PROC(GRANTED, testargs, res),
  514. PROC(TEST_MSG, testargs, norep),
  515. PROC(LOCK_MSG, lockargs, norep),
  516. PROC(CANCEL_MSG, cancargs, norep),
  517. PROC(UNLOCK_MSG, unlockargs, norep),
  518. PROC(GRANTED_MSG, testargs, norep),
  519. PROC(TEST_RES, testres, norep),
  520. PROC(LOCK_RES, res, norep),
  521. PROC(CANCEL_RES, res, norep),
  522. PROC(UNLOCK_RES, res, norep),
  523. PROC(GRANTED_RES, res, norep),
  524. };
  525. static const struct rpc_version nlm_version1 = {
  526. .number = 1,
  527. .nrprocs = ARRAY_SIZE(nlm_procedures),
  528. .procs = nlm_procedures,
  529. };
  530. static const struct rpc_version nlm_version3 = {
  531. .number = 3,
  532. .nrprocs = ARRAY_SIZE(nlm_procedures),
  533. .procs = nlm_procedures,
  534. };
  535. static const struct rpc_version *nlm_versions[] = {
  536. [1] = &nlm_version1,
  537. [3] = &nlm_version3,
  538. #ifdef CONFIG_LOCKD_V4
  539. [4] = &nlm_version4,
  540. #endif
  541. };
  542. static struct rpc_stat nlm_rpc_stats;
  543. const struct rpc_program nlm_program = {
  544. .name = "lockd",
  545. .number = NLM_PROGRAM,
  546. .nrvers = ARRAY_SIZE(nlm_versions),
  547. .version = nlm_versions,
  548. .stats = &nlm_rpc_stats,
  549. };