clnt4xdr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 struct nlm_args *args)
  334. {
  335. const struct nlm_lock *lock = &args->lock;
  336. encode_cookie(xdr, &args->cookie);
  337. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  338. encode_nlm4_lock(xdr, lock);
  339. }
  340. /*
  341. * struct nlm4_lockargs {
  342. * netobj cookie;
  343. * bool block;
  344. * bool exclusive;
  345. * struct nlm4_lock alock;
  346. * bool reclaim;
  347. * int state;
  348. * };
  349. */
  350. static void nlm4_xdr_enc_lockargs(struct rpc_rqst *req,
  351. struct xdr_stream *xdr,
  352. const struct nlm_args *args)
  353. {
  354. const struct nlm_lock *lock = &args->lock;
  355. encode_cookie(xdr, &args->cookie);
  356. encode_bool(xdr, args->block);
  357. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  358. encode_nlm4_lock(xdr, lock);
  359. encode_bool(xdr, args->reclaim);
  360. encode_int32(xdr, args->state);
  361. }
  362. /*
  363. * struct nlm4_cancargs {
  364. * netobj cookie;
  365. * bool block;
  366. * bool exclusive;
  367. * struct nlm4_lock alock;
  368. * };
  369. */
  370. static void nlm4_xdr_enc_cancargs(struct rpc_rqst *req,
  371. struct xdr_stream *xdr,
  372. const struct nlm_args *args)
  373. {
  374. const struct nlm_lock *lock = &args->lock;
  375. encode_cookie(xdr, &args->cookie);
  376. encode_bool(xdr, args->block);
  377. encode_bool(xdr, lock->fl.fl_type == F_WRLCK);
  378. encode_nlm4_lock(xdr, lock);
  379. }
  380. /*
  381. * struct nlm4_unlockargs {
  382. * netobj cookie;
  383. * struct nlm4_lock alock;
  384. * };
  385. */
  386. static void nlm4_xdr_enc_unlockargs(struct rpc_rqst *req,
  387. struct xdr_stream *xdr,
  388. const struct nlm_args *args)
  389. {
  390. const struct nlm_lock *lock = &args->lock;
  391. encode_cookie(xdr, &args->cookie);
  392. encode_nlm4_lock(xdr, lock);
  393. }
  394. /*
  395. * struct nlm4_res {
  396. * netobj cookie;
  397. * nlm4_stat stat;
  398. * };
  399. */
  400. static void nlm4_xdr_enc_res(struct rpc_rqst *req,
  401. struct xdr_stream *xdr,
  402. const struct nlm_res *result)
  403. {
  404. encode_cookie(xdr, &result->cookie);
  405. encode_nlm4_stat(xdr, result->status);
  406. }
  407. /*
  408. * union nlm4_testrply switch (nlm4_stats stat) {
  409. * case NLM4_DENIED:
  410. * struct nlm4_holder holder;
  411. * default:
  412. * void;
  413. * };
  414. *
  415. * struct nlm4_testres {
  416. * netobj cookie;
  417. * nlm4_testrply test_stat;
  418. * };
  419. */
  420. static void nlm4_xdr_enc_testres(struct rpc_rqst *req,
  421. struct xdr_stream *xdr,
  422. const struct nlm_res *result)
  423. {
  424. encode_cookie(xdr, &result->cookie);
  425. encode_nlm4_stat(xdr, result->status);
  426. if (result->status == nlm_lck_denied)
  427. encode_nlm4_holder(xdr, result);
  428. }
  429. /*
  430. * NLMv4 XDR decode functions
  431. *
  432. * NLMv4 argument types are defined in Appendix II of RFC 1813:
  433. * "NFS Version 3 Protocol Specification" and Chapter 10 of X/Open's
  434. * "Protocols for Interworking: XNFS, Version 3W".
  435. */
  436. /*
  437. * union nlm4_testrply switch (nlm4_stats stat) {
  438. * case NLM4_DENIED:
  439. * struct nlm4_holder holder;
  440. * default:
  441. * void;
  442. * };
  443. *
  444. * struct nlm4_testres {
  445. * netobj cookie;
  446. * nlm4_testrply test_stat;
  447. * };
  448. */
  449. static int decode_nlm4_testrply(struct xdr_stream *xdr,
  450. struct nlm_res *result)
  451. {
  452. int error;
  453. error = decode_nlm4_stat(xdr, &result->status);
  454. if (unlikely(error))
  455. goto out;
  456. if (result->status == nlm_lck_denied)
  457. error = decode_nlm4_holder(xdr, result);
  458. out:
  459. return error;
  460. }
  461. static int nlm4_xdr_dec_testres(struct rpc_rqst *req,
  462. struct xdr_stream *xdr,
  463. struct nlm_res *result)
  464. {
  465. int error;
  466. error = decode_cookie(xdr, &result->cookie);
  467. if (unlikely(error))
  468. goto out;
  469. error = decode_nlm4_testrply(xdr, result);
  470. out:
  471. return error;
  472. }
  473. /*
  474. * struct nlm4_res {
  475. * netobj cookie;
  476. * nlm4_stat stat;
  477. * };
  478. */
  479. static int nlm4_xdr_dec_res(struct rpc_rqst *req,
  480. struct xdr_stream *xdr,
  481. struct nlm_res *result)
  482. {
  483. int error;
  484. error = decode_cookie(xdr, &result->cookie);
  485. if (unlikely(error))
  486. goto out;
  487. error = decode_nlm4_stat(xdr, &result->status);
  488. out:
  489. return error;
  490. }
  491. /*
  492. * For NLM, a void procedure really returns nothing
  493. */
  494. #define nlm4_xdr_dec_norep NULL
  495. #define PROC(proc, argtype, restype) \
  496. [NLMPROC_##proc] = { \
  497. .p_proc = NLMPROC_##proc, \
  498. .p_encode = (kxdreproc_t)nlm4_xdr_enc_##argtype, \
  499. .p_decode = (kxdrdproc_t)nlm4_xdr_dec_##restype, \
  500. .p_arglen = NLM4_##argtype##_sz, \
  501. .p_replen = NLM4_##restype##_sz, \
  502. .p_statidx = NLMPROC_##proc, \
  503. .p_name = #proc, \
  504. }
  505. static struct rpc_procinfo nlm4_procedures[] = {
  506. PROC(TEST, testargs, testres),
  507. PROC(LOCK, lockargs, res),
  508. PROC(CANCEL, cancargs, res),
  509. PROC(UNLOCK, unlockargs, res),
  510. PROC(GRANTED, testargs, res),
  511. PROC(TEST_MSG, testargs, norep),
  512. PROC(LOCK_MSG, lockargs, norep),
  513. PROC(CANCEL_MSG, cancargs, norep),
  514. PROC(UNLOCK_MSG, unlockargs, norep),
  515. PROC(GRANTED_MSG, testargs, norep),
  516. PROC(TEST_RES, testres, norep),
  517. PROC(LOCK_RES, res, norep),
  518. PROC(CANCEL_RES, res, norep),
  519. PROC(UNLOCK_RES, res, norep),
  520. PROC(GRANTED_RES, res, norep),
  521. };
  522. const struct rpc_version nlm_version4 = {
  523. .number = 4,
  524. .nrprocs = ARRAY_SIZE(nlm4_procedures),
  525. .procs = nlm4_procedures,
  526. };