clntxdr.c 13 KB

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