xdr4.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * linux/fs/lockd/xdr4.c
  3. *
  4. * XDR support for lockd and the lock client.
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. * Copyright (C) 1999, Trond Myklebust <trond.myklebust@fys.uio.no>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/utsname.h>
  12. #include <linux/nfs.h>
  13. #include <linux/sunrpc/xdr.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/sunrpc/svc.h>
  16. #include <linux/sunrpc/stats.h>
  17. #include <linux/lockd/lockd.h>
  18. #include <linux/lockd/sm_inter.h>
  19. #define NLMDBG_FACILITY NLMDBG_XDR
  20. static inline loff_t
  21. s64_to_loff_t(__s64 offset)
  22. {
  23. return (loff_t)offset;
  24. }
  25. static inline s64
  26. loff_t_to_s64(loff_t offset)
  27. {
  28. s64 res;
  29. if (offset > NLM4_OFFSET_MAX)
  30. res = NLM4_OFFSET_MAX;
  31. else if (offset < -NLM4_OFFSET_MAX)
  32. res = -NLM4_OFFSET_MAX;
  33. else
  34. res = offset;
  35. return res;
  36. }
  37. /*
  38. * XDR functions for basic NLM types
  39. */
  40. static u32 *
  41. nlm4_decode_cookie(u32 *p, struct nlm_cookie *c)
  42. {
  43. unsigned int len;
  44. len = ntohl(*p++);
  45. if(len==0)
  46. {
  47. c->len=4;
  48. memset(c->data, 0, 4); /* hockeypux brain damage */
  49. }
  50. else if(len<=NLM_MAXCOOKIELEN)
  51. {
  52. c->len=len;
  53. memcpy(c->data, p, len);
  54. p+=XDR_QUADLEN(len);
  55. }
  56. else
  57. {
  58. printk(KERN_NOTICE
  59. "lockd: bad cookie size %d (only cookies under %d bytes are supported.)\n", len, NLM_MAXCOOKIELEN);
  60. return NULL;
  61. }
  62. return p;
  63. }
  64. static u32 *
  65. nlm4_encode_cookie(u32 *p, struct nlm_cookie *c)
  66. {
  67. *p++ = htonl(c->len);
  68. memcpy(p, c->data, c->len);
  69. p+=XDR_QUADLEN(c->len);
  70. return p;
  71. }
  72. static u32 *
  73. nlm4_decode_fh(u32 *p, struct nfs_fh *f)
  74. {
  75. memset(f->data, 0, sizeof(f->data));
  76. f->size = ntohl(*p++);
  77. if (f->size > NFS_MAXFHSIZE) {
  78. printk(KERN_NOTICE
  79. "lockd: bad fhandle size %d (should be <=%d)\n",
  80. f->size, NFS_MAXFHSIZE);
  81. return NULL;
  82. }
  83. memcpy(f->data, p, f->size);
  84. return p + XDR_QUADLEN(f->size);
  85. }
  86. static u32 *
  87. nlm4_encode_fh(u32 *p, struct nfs_fh *f)
  88. {
  89. *p++ = htonl(f->size);
  90. if (f->size) p[XDR_QUADLEN(f->size)-1] = 0; /* don't leak anything */
  91. memcpy(p, f->data, f->size);
  92. return p + XDR_QUADLEN(f->size);
  93. }
  94. /*
  95. * Encode and decode owner handle
  96. */
  97. static u32 *
  98. nlm4_decode_oh(u32 *p, struct xdr_netobj *oh)
  99. {
  100. return xdr_decode_netobj(p, oh);
  101. }
  102. static u32 *
  103. nlm4_encode_oh(u32 *p, struct xdr_netobj *oh)
  104. {
  105. return xdr_encode_netobj(p, oh);
  106. }
  107. static u32 *
  108. nlm4_decode_lock(u32 *p, struct nlm_lock *lock)
  109. {
  110. struct file_lock *fl = &lock->fl;
  111. __s64 len, start, end;
  112. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  113. &lock->len, NLM_MAXSTRLEN))
  114. || !(p = nlm4_decode_fh(p, &lock->fh))
  115. || !(p = nlm4_decode_oh(p, &lock->oh)))
  116. return NULL;
  117. locks_init_lock(fl);
  118. fl->fl_owner = current->files;
  119. fl->fl_pid = ntohl(*p++);
  120. fl->fl_flags = FL_POSIX;
  121. fl->fl_type = F_RDLCK; /* as good as anything else */
  122. p = xdr_decode_hyper(p, &start);
  123. p = xdr_decode_hyper(p, &len);
  124. end = start + len - 1;
  125. fl->fl_start = s64_to_loff_t(start);
  126. if (len == 0 || end < 0)
  127. fl->fl_end = OFFSET_MAX;
  128. else
  129. fl->fl_end = s64_to_loff_t(end);
  130. return p;
  131. }
  132. /*
  133. * Encode a lock as part of an NLM call
  134. */
  135. static u32 *
  136. nlm4_encode_lock(u32 *p, struct nlm_lock *lock)
  137. {
  138. struct file_lock *fl = &lock->fl;
  139. __s64 start, len;
  140. if (!(p = xdr_encode_string(p, lock->caller))
  141. || !(p = nlm4_encode_fh(p, &lock->fh))
  142. || !(p = nlm4_encode_oh(p, &lock->oh)))
  143. return NULL;
  144. if (fl->fl_start > NLM4_OFFSET_MAX
  145. || (fl->fl_end > NLM4_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
  146. return NULL;
  147. *p++ = htonl(fl->fl_pid);
  148. start = loff_t_to_s64(fl->fl_start);
  149. if (fl->fl_end == OFFSET_MAX)
  150. len = 0;
  151. else
  152. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  153. p = xdr_encode_hyper(p, start);
  154. p = xdr_encode_hyper(p, len);
  155. return p;
  156. }
  157. /*
  158. * Encode result of a TEST/TEST_MSG call
  159. */
  160. static u32 *
  161. nlm4_encode_testres(u32 *p, struct nlm_res *resp)
  162. {
  163. s64 start, len;
  164. dprintk("xdr: before encode_testres (p %p resp %p)\n", p, resp);
  165. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  166. return NULL;
  167. *p++ = resp->status;
  168. if (resp->status == nlm_lck_denied) {
  169. struct file_lock *fl = &resp->lock.fl;
  170. *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
  171. *p++ = htonl(fl->fl_pid);
  172. /* Encode owner handle. */
  173. if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
  174. return NULL;
  175. start = loff_t_to_s64(fl->fl_start);
  176. if (fl->fl_end == OFFSET_MAX)
  177. len = 0;
  178. else
  179. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  180. p = xdr_encode_hyper(p, start);
  181. p = xdr_encode_hyper(p, len);
  182. dprintk("xdr: encode_testres (status %d pid %d type %d start %Ld end %Ld)\n",
  183. resp->status, fl->fl_pid, fl->fl_type,
  184. (long long)fl->fl_start, (long long)fl->fl_end);
  185. }
  186. dprintk("xdr: after encode_testres (p %p resp %p)\n", p, resp);
  187. return p;
  188. }
  189. /*
  190. * First, the server side XDR functions
  191. */
  192. int
  193. nlm4svc_decode_testargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  194. {
  195. u32 exclusive;
  196. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  197. return 0;
  198. exclusive = ntohl(*p++);
  199. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  200. return 0;
  201. if (exclusive)
  202. argp->lock.fl.fl_type = F_WRLCK;
  203. return xdr_argsize_check(rqstp, p);
  204. }
  205. int
  206. nlm4svc_encode_testres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  207. {
  208. if (!(p = nlm4_encode_testres(p, resp)))
  209. return 0;
  210. return xdr_ressize_check(rqstp, p);
  211. }
  212. int
  213. nlm4svc_decode_lockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  214. {
  215. u32 exclusive;
  216. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  217. return 0;
  218. argp->block = ntohl(*p++);
  219. exclusive = ntohl(*p++);
  220. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  221. return 0;
  222. if (exclusive)
  223. argp->lock.fl.fl_type = F_WRLCK;
  224. argp->reclaim = ntohl(*p++);
  225. argp->state = ntohl(*p++);
  226. argp->monitor = 1; /* monitor client by default */
  227. return xdr_argsize_check(rqstp, p);
  228. }
  229. int
  230. nlm4svc_decode_cancargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  231. {
  232. u32 exclusive;
  233. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  234. return 0;
  235. argp->block = ntohl(*p++);
  236. exclusive = ntohl(*p++);
  237. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  238. return 0;
  239. if (exclusive)
  240. argp->lock.fl.fl_type = F_WRLCK;
  241. return xdr_argsize_check(rqstp, p);
  242. }
  243. int
  244. nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  245. {
  246. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  247. || !(p = nlm4_decode_lock(p, &argp->lock)))
  248. return 0;
  249. argp->lock.fl.fl_type = F_UNLCK;
  250. return xdr_argsize_check(rqstp, p);
  251. }
  252. int
  253. nlm4svc_decode_shareargs(struct svc_rqst *rqstp, u32 *p, nlm_args *argp)
  254. {
  255. struct nlm_lock *lock = &argp->lock;
  256. memset(lock, 0, sizeof(*lock));
  257. locks_init_lock(&lock->fl);
  258. lock->fl.fl_pid = ~(u32) 0;
  259. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  260. || !(p = xdr_decode_string_inplace(p, &lock->caller,
  261. &lock->len, NLM_MAXSTRLEN))
  262. || !(p = nlm4_decode_fh(p, &lock->fh))
  263. || !(p = nlm4_decode_oh(p, &lock->oh)))
  264. return 0;
  265. argp->fsm_mode = ntohl(*p++);
  266. argp->fsm_access = ntohl(*p++);
  267. return xdr_argsize_check(rqstp, p);
  268. }
  269. int
  270. nlm4svc_encode_shareres(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  271. {
  272. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  273. return 0;
  274. *p++ = resp->status;
  275. *p++ = xdr_zero; /* sequence argument */
  276. return xdr_ressize_check(rqstp, p);
  277. }
  278. int
  279. nlm4svc_encode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  280. {
  281. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  282. return 0;
  283. *p++ = resp->status;
  284. return xdr_ressize_check(rqstp, p);
  285. }
  286. int
  287. nlm4svc_decode_notify(struct svc_rqst *rqstp, u32 *p, struct nlm_args *argp)
  288. {
  289. struct nlm_lock *lock = &argp->lock;
  290. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  291. &lock->len, NLM_MAXSTRLEN)))
  292. return 0;
  293. argp->state = ntohl(*p++);
  294. return xdr_argsize_check(rqstp, p);
  295. }
  296. int
  297. nlm4svc_decode_reboot(struct svc_rqst *rqstp, u32 *p, struct nlm_reboot *argp)
  298. {
  299. if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
  300. return 0;
  301. argp->state = ntohl(*p++);
  302. /* Preserve the address in network byte order */
  303. argp->addr = *p++;
  304. argp->vers = *p++;
  305. argp->proto = *p++;
  306. return xdr_argsize_check(rqstp, p);
  307. }
  308. int
  309. nlm4svc_decode_res(struct svc_rqst *rqstp, u32 *p, struct nlm_res *resp)
  310. {
  311. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  312. return 0;
  313. resp->status = ntohl(*p++);
  314. return xdr_argsize_check(rqstp, p);
  315. }
  316. int
  317. nlm4svc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
  318. {
  319. return xdr_argsize_check(rqstp, p);
  320. }
  321. int
  322. nlm4svc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
  323. {
  324. return xdr_ressize_check(rqstp, p);
  325. }
  326. /*
  327. * Now, the client side XDR functions
  328. */
  329. #ifdef NLMCLNT_SUPPORT_SHARES
  330. static int
  331. nlm4clt_decode_void(struct rpc_rqst *req, u32 *p, void *ptr)
  332. {
  333. return 0;
  334. }
  335. #endif
  336. static int
  337. nlm4clt_encode_testargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  338. {
  339. struct nlm_lock *lock = &argp->lock;
  340. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  341. return -EIO;
  342. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  343. if (!(p = nlm4_encode_lock(p, lock)))
  344. return -EIO;
  345. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  346. return 0;
  347. }
  348. static int
  349. nlm4clt_decode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  350. {
  351. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  352. return -EIO;
  353. resp->status = ntohl(*p++);
  354. if (resp->status == NLM_LCK_DENIED) {
  355. struct file_lock *fl = &resp->lock.fl;
  356. u32 excl;
  357. s64 start, end, len;
  358. memset(&resp->lock, 0, sizeof(resp->lock));
  359. locks_init_lock(fl);
  360. excl = ntohl(*p++);
  361. fl->fl_pid = ntohl(*p++);
  362. if (!(p = nlm4_decode_oh(p, &resp->lock.oh)))
  363. return -EIO;
  364. fl->fl_flags = FL_POSIX;
  365. fl->fl_type = excl? F_WRLCK : F_RDLCK;
  366. p = xdr_decode_hyper(p, &start);
  367. p = xdr_decode_hyper(p, &len);
  368. end = start + len - 1;
  369. fl->fl_start = s64_to_loff_t(start);
  370. if (len == 0 || end < 0)
  371. fl->fl_end = OFFSET_MAX;
  372. else
  373. fl->fl_end = s64_to_loff_t(end);
  374. }
  375. return 0;
  376. }
  377. static int
  378. nlm4clt_encode_lockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  379. {
  380. struct nlm_lock *lock = &argp->lock;
  381. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  382. return -EIO;
  383. *p++ = argp->block? xdr_one : xdr_zero;
  384. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  385. if (!(p = nlm4_encode_lock(p, lock)))
  386. return -EIO;
  387. *p++ = argp->reclaim? xdr_one : xdr_zero;
  388. *p++ = htonl(argp->state);
  389. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  390. return 0;
  391. }
  392. static int
  393. nlm4clt_encode_cancargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  394. {
  395. struct nlm_lock *lock = &argp->lock;
  396. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  397. return -EIO;
  398. *p++ = argp->block? xdr_one : xdr_zero;
  399. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  400. if (!(p = nlm4_encode_lock(p, lock)))
  401. return -EIO;
  402. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  403. return 0;
  404. }
  405. static int
  406. nlm4clt_encode_unlockargs(struct rpc_rqst *req, u32 *p, nlm_args *argp)
  407. {
  408. struct nlm_lock *lock = &argp->lock;
  409. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  410. return -EIO;
  411. if (!(p = nlm4_encode_lock(p, lock)))
  412. return -EIO;
  413. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  414. return 0;
  415. }
  416. static int
  417. nlm4clt_encode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  418. {
  419. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  420. return -EIO;
  421. *p++ = resp->status;
  422. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  423. return 0;
  424. }
  425. static int
  426. nlm4clt_encode_testres(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  427. {
  428. if (!(p = nlm4_encode_testres(p, resp)))
  429. return -EIO;
  430. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  431. return 0;
  432. }
  433. static int
  434. nlm4clt_decode_res(struct rpc_rqst *req, u32 *p, struct nlm_res *resp)
  435. {
  436. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  437. return -EIO;
  438. resp->status = ntohl(*p++);
  439. return 0;
  440. }
  441. /*
  442. * Buffer requirements for NLM
  443. */
  444. #define NLM4_void_sz 0
  445. #define NLM4_cookie_sz 1+XDR_QUADLEN(NLM_MAXCOOKIELEN)
  446. #define NLM4_caller_sz 1+XDR_QUADLEN(NLM_MAXSTRLEN)
  447. #define NLM4_netobj_sz 1+XDR_QUADLEN(XDR_MAX_NETOBJ)
  448. /* #define NLM4_owner_sz 1+XDR_QUADLEN(NLM4_MAXOWNER) */
  449. #define NLM4_fhandle_sz 1+XDR_QUADLEN(NFS3_FHSIZE)
  450. #define NLM4_lock_sz 5+NLM4_caller_sz+NLM4_netobj_sz+NLM4_fhandle_sz
  451. #define NLM4_holder_sz 6+NLM4_netobj_sz
  452. #define NLM4_testargs_sz NLM4_cookie_sz+1+NLM4_lock_sz
  453. #define NLM4_lockargs_sz NLM4_cookie_sz+4+NLM4_lock_sz
  454. #define NLM4_cancargs_sz NLM4_cookie_sz+2+NLM4_lock_sz
  455. #define NLM4_unlockargs_sz NLM4_cookie_sz+NLM4_lock_sz
  456. #define NLM4_testres_sz NLM4_cookie_sz+1+NLM4_holder_sz
  457. #define NLM4_res_sz NLM4_cookie_sz+1
  458. #define NLM4_norep_sz 0
  459. #ifndef MAX
  460. # define MAX(a,b) (((a) > (b))? (a) : (b))
  461. #endif
  462. /*
  463. * For NLM, a void procedure really returns nothing
  464. */
  465. #define nlm4clt_decode_norep NULL
  466. #define PROC(proc, argtype, restype) \
  467. [NLMPROC_##proc] = { \
  468. .p_proc = NLMPROC_##proc, \
  469. .p_encode = (kxdrproc_t) nlm4clt_encode_##argtype, \
  470. .p_decode = (kxdrproc_t) nlm4clt_decode_##restype, \
  471. .p_bufsiz = MAX(NLM4_##argtype##_sz, NLM4_##restype##_sz) << 2 \
  472. }
  473. static struct rpc_procinfo nlm4_procedures[] = {
  474. PROC(TEST, testargs, testres),
  475. PROC(LOCK, lockargs, res),
  476. PROC(CANCEL, cancargs, res),
  477. PROC(UNLOCK, unlockargs, res),
  478. PROC(GRANTED, testargs, res),
  479. PROC(TEST_MSG, testargs, norep),
  480. PROC(LOCK_MSG, lockargs, norep),
  481. PROC(CANCEL_MSG, cancargs, norep),
  482. PROC(UNLOCK_MSG, unlockargs, norep),
  483. PROC(GRANTED_MSG, testargs, norep),
  484. PROC(TEST_RES, testres, norep),
  485. PROC(LOCK_RES, res, norep),
  486. PROC(CANCEL_RES, res, norep),
  487. PROC(UNLOCK_RES, res, norep),
  488. PROC(GRANTED_RES, res, norep),
  489. #ifdef NLMCLNT_SUPPORT_SHARES
  490. PROC(SHARE, shareargs, shareres),
  491. PROC(UNSHARE, shareargs, shareres),
  492. PROC(NM_LOCK, lockargs, res),
  493. PROC(FREE_ALL, notify, void),
  494. #endif
  495. };
  496. struct rpc_version nlm_version4 = {
  497. .number = 4,
  498. .nrprocs = 24,
  499. .procs = nlm4_procedures,
  500. };