clntxdr.c 13 KB

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