clnt4xdr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * linux/fs/lockd/clnt4xdr.c
  3. *
  4. * XDR functions to encode/decode NLM version 4 RPC arguments and results.
  5. *
  6. * NLM client-side only.
  7. *
  8. * Copyright (C) 2010, Oracle. All rights reserved.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/sunrpc/xdr.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/stats.h>
  14. #include <linux/lockd/lockd.h>
  15. #include <uapi/linux/nfs3.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. #if (NLMCLNT_OHSIZE > NLM_MAXSTRLEN)
  21. # error "NLM host name cannot be larger than NLM's maximum string length!"
  22. #endif
  23. /*
  24. * Declare the space requirements for NLM arguments and replies as
  25. * number of 32bit-words
  26. */
  27. #define NLM4_void_sz (0)
  28. #define NLM4_cookie_sz (1+(NLM_MAXCOOKIELEN>>2))
  29. #define NLM4_caller_sz (1+(NLMCLNT_OHSIZE>>2))
  30. #define NLM4_owner_sz (1+(NLMCLNT_OHSIZE>>2))
  31. #define NLM4_fhandle_sz (1+(NFS3_FHSIZE>>2))
  32. #define NLM4_lock_sz (5+NLM4_caller_sz+NLM4_owner_sz+NLM4_fhandle_sz)
  33. #define NLM4_holder_sz (6+NLM4_owner_sz)
  34. #define NLM4_testargs_sz (NLM4_cookie_sz+1+NLM4_lock_sz)
  35. #define NLM4_lockargs_sz (NLM4_cookie_sz+4+NLM4_lock_sz)
  36. #define NLM4_cancargs_sz (NLM4_cookie_sz+2+NLM4_lock_sz)
  37. #define NLM4_unlockargs_sz (NLM4_cookie_sz+NLM4_lock_sz)
  38. #define NLM4_testres_sz (NLM4_cookie_sz+1+NLM4_holder_sz)
  39. #define NLM4_res_sz (NLM4_cookie_sz+1)
  40. #define NLM4_norep_sz (0)
  41. static s64 loff_t_to_s64(loff_t offset)
  42. {
  43. s64 res;
  44. if (offset >= NLM4_OFFSET_MAX)
  45. res = NLM4_OFFSET_MAX;
  46. else if (offset <= -NLM4_OFFSET_MAX)
  47. res = -NLM4_OFFSET_MAX;
  48. else
  49. res = offset;
  50. return res;
  51. }
  52. static void nlm4_compute_offsets(const struct nlm_lock *lock,
  53. u64 *l_offset, u64 *l_len)
  54. {
  55. const struct file_lock *fl = &lock->fl;
  56. *l_offset = loff_t_to_s64(fl->fl_start);
  57. if (fl->fl_end == OFFSET_MAX)
  58. *l_len = 0;
  59. else
  60. *l_len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  61. }
  62. /*
  63. * Handle decode buffer overflows out-of-line.
  64. */
  65. static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
  66. {
  67. dprintk("lockd: %s prematurely hit the end of our receive buffer. "
  68. "Remaining buffer length is %tu words.\n",
  69. func, xdr->end - xdr->p);
  70. }
  71. /*
  72. * Encode/decode NLMv4 basic data types
  73. *
  74. * Basic NLMv4 data types are defined in Appendix II, section 6.1.4
  75. * of RFC 1813: "NFS Version 3 Protocol Specification" and in Chapter
  76. * 10 of X/Open's "Protocols for Interworking: 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. p = xdr_reserve_space(xdr, 4 + length);
  102. xdr_encode_opaque(p, data, length);
  103. }
  104. static int decode_netobj(struct xdr_stream *xdr,
  105. struct xdr_netobj *obj)
  106. {
  107. u32 length;
  108. __be32 *p;
  109. p = xdr_inline_decode(xdr, 4);
  110. if (unlikely(p == NULL))
  111. goto out_overflow;
  112. length = be32_to_cpup(p++);
  113. if (unlikely(length > XDR_MAX_NETOBJ))
  114. goto out_size;
  115. obj->len = length;
  116. obj->data = (u8 *)p;
  117. return 0;
  118. out_size:
  119. dprintk("NFS: returned netobj was too long: %u\n", length);
  120. return -EIO;
  121. out_overflow:
  122. print_overflow_msg(__func__, xdr);
  123. return -EIO;
  124. }
  125. /*
  126. * netobj cookie;
  127. */
  128. static void encode_cookie(struct xdr_stream *xdr,
  129. const struct nlm_cookie *cookie)
  130. {
  131. encode_netobj(xdr, (u8 *)&cookie->data, cookie->len);
  132. }
  133. static int decode_cookie(struct xdr_stream *xdr,
  134. struct nlm_cookie *cookie)
  135. {
  136. u32 length;
  137. __be32 *p;
  138. p = xdr_inline_decode(xdr, 4);
  139. if (unlikely(p == NULL))
  140. goto out_overflow;
  141. length = be32_to_cpup(p++);
  142. /* apparently HPUX can return empty cookies */
  143. if (length == 0)
  144. goto out_hpux;
  145. if (length > NLM_MAXCOOKIELEN)
  146. goto out_size;
  147. p = xdr_inline_decode(xdr, length);
  148. if (unlikely(p == NULL))
  149. goto out_overflow;
  150. cookie->len = length;
  151. memcpy(cookie->data, p, length);
  152. return 0;
  153. out_hpux:
  154. cookie->len = 4;
  155. memset(cookie->data, 0, 4);
  156. return 0;
  157. out_size:
  158. dprintk("NFS: returned cookie was too long: %u\n", length);
  159. return -EIO;
  160. out_overflow:
  161. print_overflow_msg(__func__, xdr);
  162. return -EIO;
  163. }
  164. /*
  165. * netobj fh;
  166. */
  167. static void encode_fh(struct xdr_stream *xdr, const struct nfs_fh *fh)
  168. {
  169. encode_netobj(xdr, (u8 *)&fh->data, fh->size);
  170. }
  171. /*
  172. * enum nlm4_stats {
  173. * NLM4_GRANTED = 0,
  174. * NLM4_DENIED = 1,
  175. * NLM4_DENIED_NOLOCKS = 2,
  176. * NLM4_BLOCKED = 3,
  177. * NLM4_DENIED_GRACE_PERIOD = 4,
  178. * NLM4_DEADLCK = 5,
  179. * NLM4_ROFS = 6,
  180. * NLM4_STALE_FH = 7,
  181. * NLM4_FBIG = 8,
  182. * NLM4_FAILED = 9
  183. * };
  184. *
  185. * struct nlm4_stat {
  186. * nlm4_stats stat;
  187. * };
  188. *
  189. * NB: we don't swap bytes for the NLM status values. The upper
  190. * layers deal directly with the status value in network byte
  191. * order.
  192. */
  193. static void encode_nlm4_stat(struct xdr_stream *xdr,
  194. const __be32 stat)
  195. {
  196. __be32 *p;
  197. BUG_ON(be32_to_cpu(stat) > NLM_FAILED);
  198. p = xdr_reserve_space(xdr, 4);
  199. *p = stat;
  200. }
  201. static int decode_nlm4_stat(struct xdr_stream *xdr, __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(nlm4_failed)))
  208. goto out_bad_xdr;
  209. *stat = *p;
  210. return 0;
  211. out_bad_xdr:
  212. dprintk("%s: server returned invalid nlm4_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 nlm4_holder {
  221. * bool exclusive;
  222. * int32 svid;
  223. * netobj oh;
  224. * uint64 l_offset;
  225. * uint64 l_len;
  226. * };
  227. */
  228. static void encode_nlm4_holder(struct xdr_stream *xdr,
  229. const struct nlm_res *result)
  230. {
  231. const struct nlm_lock *lock = &result->lock;
  232. u64 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. nlm4_compute_offsets(lock, &l_offset, &l_len);
  239. p = xdr_encode_hyper(p, l_offset);
  240. xdr_encode_hyper(p, l_len);
  241. }
  242. static int decode_nlm4_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. u64 l_offset, l_len;
  247. u32 exclusive;
  248. int error;
  249. __be32 *p;
  250. s32 end;
  251. memset(lock, 0, sizeof(*lock));
  252. locks_init_lock(fl);
  253. p = xdr_inline_decode(xdr, 4 + 4);
  254. if (unlikely(p == NULL))
  255. goto out_overflow;
  256. exclusive = be32_to_cpup(p++);
  257. lock->svid = be32_to_cpup(p);
  258. fl->fl_pid = (pid_t)lock->svid;
  259. error = decode_netobj(xdr, &lock->oh);
  260. if (unlikely(error))
  261. goto out;
  262. p = xdr_inline_decode(xdr, 8 + 8);
  263. if (unlikely(p == NULL))
  264. goto out_overflow;
  265. fl->fl_flags = FL_POSIX;
  266. fl->fl_type = exclusive != 0 ? F_WRLCK : F_RDLCK;
  267. p = xdr_decode_hyper(p, &l_offset);
  268. xdr_decode_hyper(p, &l_len);
  269. end = l_offset + l_len - 1;
  270. fl->fl_start = (loff_t)l_offset;
  271. if (l_len == 0 || end < 0)
  272. fl->fl_end = OFFSET_MAX;
  273. else
  274. fl->fl_end = (loff_t)end;
  275. error = 0;
  276. out:
  277. return error;
  278. out_overflow:
  279. print_overflow_msg(__func__, xdr);
  280. return -EIO;
  281. }
  282. /*
  283. * string caller_name<LM_MAXSTRLEN>;
  284. */
  285. static void encode_caller_name(struct xdr_stream *xdr, const char *name)
  286. {
  287. /* NB: client-side does not set lock->len */
  288. u32 length = strlen(name);
  289. __be32 *p;
  290. p = xdr_reserve_space(xdr, 4 + length);
  291. xdr_encode_opaque(p, name, length);
  292. }
  293. /*
  294. * struct nlm4_lock {
  295. * string caller_name<LM_MAXSTRLEN>;
  296. * netobj fh;
  297. * netobj oh;
  298. * int32 svid;
  299. * uint64 l_offset;
  300. * uint64 l_len;
  301. * };
  302. */
  303. static void encode_nlm4_lock(struct xdr_stream *xdr,
  304. const struct nlm_lock *lock)
  305. {
  306. u64 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 + 8 + 8);
  312. *p++ = cpu_to_be32(lock->svid);
  313. nlm4_compute_offsets(lock, &l_offset, &l_len);
  314. p = xdr_encode_hyper(p, l_offset);
  315. xdr_encode_hyper(p, l_len);
  316. }
  317. /*
  318. * NLMv4 XDR encode functions
  319. *
  320. * NLMv4 argument types are defined in Appendix II of RFC 1813:
  321. * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
  322. * "Protocols for Interworking: XNFS, Version 3W".
  323. */
  324. /*
  325. * struct nlm4_testargs {
  326. * netobj cookie;
  327. * bool exclusive;
  328. * struct nlm4_lock alock;
  329. * };
  330. */
  331. static void nlm4_xdr_enc_testargs(struct rpc_rqst *req,
  332. struct xdr_stream *xdr,
  333. const void *data)
  334. {
  335. const struct nlm_args *args = data;
  336. const struct nlm_lock *lock = &args->lock;
  337. encode_cookie(xdr, &args->cookie);
  338. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  339. encode_nlm4_lock(xdr, lock);
  340. }
  341. /*
  342. * struct nlm4_lockargs {
  343. * netobj cookie;
  344. * bool block;
  345. * bool exclusive;
  346. * struct nlm4_lock alock;
  347. * bool reclaim;
  348. * int state;
  349. * };
  350. */
  351. static void nlm4_xdr_enc_lockargs(struct rpc_rqst *req,
  352. struct xdr_stream *xdr,
  353. const void *data)
  354. {
  355. const struct nlm_args *args = data;
  356. const struct nlm_lock *lock = &args->lock;
  357. encode_cookie(xdr, &args->cookie);
  358. encode_bool(xdr, args->block);
  359. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  360. encode_nlm4_lock(xdr, lock);
  361. encode_bool(xdr, args->reclaim);
  362. encode_int32(xdr, args->state);
  363. }
  364. /*
  365. * struct nlm4_cancargs {
  366. * netobj cookie;
  367. * bool block;
  368. * bool exclusive;
  369. * struct nlm4_lock alock;
  370. * };
  371. */
  372. static void nlm4_xdr_enc_cancargs(struct rpc_rqst *req,
  373. struct xdr_stream *xdr,
  374. const void *data)
  375. {
  376. const struct nlm_args *args = data;
  377. const struct nlm_lock *lock = &args->lock;
  378. encode_cookie(xdr, &args->cookie);
  379. encode_bool(xdr, args->block);
  380. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  381. encode_nlm4_lock(xdr, lock);
  382. }
  383. /*
  384. * struct nlm4_unlockargs {
  385. * netobj cookie;
  386. * struct nlm4_lock alock;
  387. * };
  388. */
  389. static void nlm4_xdr_enc_unlockargs(struct rpc_rqst *req,
  390. struct xdr_stream *xdr,
  391. const void *data)
  392. {
  393. const struct nlm_args *args = data;
  394. const struct nlm_lock *lock = &args->lock;
  395. encode_cookie(xdr, &args->cookie);
  396. encode_nlm4_lock(xdr, lock);
  397. }
  398. /*
  399. * struct nlm4_res {
  400. * netobj cookie;
  401. * nlm4_stat stat;
  402. * };
  403. */
  404. static void nlm4_xdr_enc_res(struct rpc_rqst *req,
  405. struct xdr_stream *xdr,
  406. const void *data)
  407. {
  408. const struct nlm_res *result = data;
  409. encode_cookie(xdr, &result->cookie);
  410. encode_nlm4_stat(xdr, result->status);
  411. }
  412. /*
  413. * union nlm4_testrply switch (nlm4_stats stat) {
  414. * case NLM4_DENIED:
  415. * struct nlm4_holder holder;
  416. * default:
  417. * void;
  418. * };
  419. *
  420. * struct nlm4_testres {
  421. * netobj cookie;
  422. * nlm4_testrply test_stat;
  423. * };
  424. */
  425. static void nlm4_xdr_enc_testres(struct rpc_rqst *req,
  426. struct xdr_stream *xdr,
  427. const void *data)
  428. {
  429. const struct nlm_res *result = data;
  430. encode_cookie(xdr, &result->cookie);
  431. encode_nlm4_stat(xdr, result->status);
  432. if (result->status == nlm_lck_denied)
  433. encode_nlm4_holder(xdr, result);
  434. }
  435. /*
  436. * NLMv4 XDR decode functions
  437. *
  438. * NLMv4 argument types are defined in Appendix II of RFC 1813:
  439. * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
  440. * "Protocols for Interworking: XNFS, Version 3W".
  441. */
  442. /*
  443. * union nlm4_testrply switch (nlm4_stats stat) {
  444. * case NLM4_DENIED:
  445. * struct nlm4_holder holder;
  446. * default:
  447. * void;
  448. * };
  449. *
  450. * struct nlm4_testres {
  451. * netobj cookie;
  452. * nlm4_testrply test_stat;
  453. * };
  454. */
  455. static int decode_nlm4_testrply(struct xdr_stream *xdr,
  456. struct nlm_res *result)
  457. {
  458. int error;
  459. error = decode_nlm4_stat(xdr, &result->status);
  460. if (unlikely(error))
  461. goto out;
  462. if (result->status == nlm_lck_denied)
  463. error = decode_nlm4_holder(xdr, result);
  464. out:
  465. return error;
  466. }
  467. static int nlm4_xdr_dec_testres(struct rpc_rqst *req,
  468. struct xdr_stream *xdr,
  469. void *data)
  470. {
  471. struct nlm_res *result = data;
  472. int error;
  473. error = decode_cookie(xdr, &result->cookie);
  474. if (unlikely(error))
  475. goto out;
  476. error = decode_nlm4_testrply(xdr, result);
  477. out:
  478. return error;
  479. }
  480. /*
  481. * struct nlm4_res {
  482. * netobj cookie;
  483. * nlm4_stat stat;
  484. * };
  485. */
  486. static int nlm4_xdr_dec_res(struct rpc_rqst *req,
  487. struct xdr_stream *xdr,
  488. void *data)
  489. {
  490. struct nlm_res *result = data;
  491. int error;
  492. error = decode_cookie(xdr, &result->cookie);
  493. if (unlikely(error))
  494. goto out;
  495. error = decode_nlm4_stat(xdr, &result->status);
  496. out:
  497. return error;
  498. }
  499. /*
  500. * For NLM, a void procedure really returns nothing
  501. */
  502. #define nlm4_xdr_dec_norep NULL
  503. #define PROC(proc, argtype, restype) \
  504. [NLMPROC_##proc] = { \
  505. .p_proc = NLMPROC_##proc, \
  506. .p_encode = nlm4_xdr_enc_##argtype, \
  507. .p_decode = nlm4_xdr_dec_##restype, \
  508. .p_arglen = NLM4_##argtype##_sz, \
  509. .p_replen = NLM4_##restype##_sz, \
  510. .p_statidx = NLMPROC_##proc, \
  511. .p_name = #proc, \
  512. }
  513. static const struct rpc_procinfo nlm4_procedures[] = {
  514. PROC(TEST, testargs, testres),
  515. PROC(LOCK, lockargs, res),
  516. PROC(CANCEL, cancargs, res),
  517. PROC(UNLOCK, unlockargs, res),
  518. PROC(GRANTED, testargs, res),
  519. PROC(TEST_MSG, testargs, norep),
  520. PROC(LOCK_MSG, lockargs, norep),
  521. PROC(CANCEL_MSG, cancargs, norep),
  522. PROC(UNLOCK_MSG, unlockargs, norep),
  523. PROC(GRANTED_MSG, testargs, norep),
  524. PROC(TEST_RES, testres, norep),
  525. PROC(LOCK_RES, res, norep),
  526. PROC(CANCEL_RES, res, norep),
  527. PROC(UNLOCK_RES, res, norep),
  528. PROC(GRANTED_RES, res, norep),
  529. };
  530. static unsigned int nlm_version4_counts[ARRAY_SIZE(nlm4_procedures)];
  531. const struct rpc_version nlm_version4 = {
  532. .number = 4,
  533. .nrprocs = ARRAY_SIZE(nlm4_procedures),
  534. .procs = nlm4_procedures,
  535. .counts = nlm_version4_counts,
  536. };