xdr4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 __be32 *
  41. nlm4_decode_cookie(__be32 *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 __be32 *
  65. nlm4_encode_cookie(__be32 *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 __be32 *
  73. nlm4_decode_fh(__be32 *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 __be32 *
  87. nlm4_encode_fh(__be32 *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 __be32 *
  98. nlm4_decode_oh(__be32 *p, struct xdr_netobj *oh)
  99. {
  100. return xdr_decode_netobj(p, oh);
  101. }
  102. static __be32 *
  103. nlm4_encode_oh(__be32 *p, struct xdr_netobj *oh)
  104. {
  105. return xdr_encode_netobj(p, oh);
  106. }
  107. static __be32 *
  108. nlm4_decode_lock(__be32 *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. lock->svid = ntohl(*p++);
  118. locks_init_lock(fl);
  119. fl->fl_owner = current->files;
  120. fl->fl_pid = (pid_t)lock->svid;
  121. fl->fl_flags = FL_POSIX;
  122. fl->fl_type = F_RDLCK; /* as good as anything else */
  123. p = xdr_decode_hyper(p, &start);
  124. p = xdr_decode_hyper(p, &len);
  125. end = start + len - 1;
  126. fl->fl_start = s64_to_loff_t(start);
  127. if (len == 0 || end < 0)
  128. fl->fl_end = OFFSET_MAX;
  129. else
  130. fl->fl_end = s64_to_loff_t(end);
  131. return p;
  132. }
  133. /*
  134. * Encode a lock as part of an NLM call
  135. */
  136. static __be32 *
  137. nlm4_encode_lock(__be32 *p, struct nlm_lock *lock)
  138. {
  139. struct file_lock *fl = &lock->fl;
  140. __s64 start, len;
  141. if (!(p = xdr_encode_string(p, lock->caller))
  142. || !(p = nlm4_encode_fh(p, &lock->fh))
  143. || !(p = nlm4_encode_oh(p, &lock->oh)))
  144. return NULL;
  145. if (fl->fl_start > NLM4_OFFSET_MAX
  146. || (fl->fl_end > NLM4_OFFSET_MAX && fl->fl_end != OFFSET_MAX))
  147. return NULL;
  148. *p++ = htonl(lock->svid);
  149. start = loff_t_to_s64(fl->fl_start);
  150. if (fl->fl_end == OFFSET_MAX)
  151. len = 0;
  152. else
  153. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  154. p = xdr_encode_hyper(p, start);
  155. p = xdr_encode_hyper(p, len);
  156. return p;
  157. }
  158. /*
  159. * Encode result of a TEST/TEST_MSG call
  160. */
  161. static __be32 *
  162. nlm4_encode_testres(__be32 *p, struct nlm_res *resp)
  163. {
  164. s64 start, len;
  165. dprintk("xdr: before encode_testres (p %p resp %p)\n", p, resp);
  166. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  167. return NULL;
  168. *p++ = resp->status;
  169. if (resp->status == nlm_lck_denied) {
  170. struct file_lock *fl = &resp->lock.fl;
  171. *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
  172. *p++ = htonl(resp->lock.svid);
  173. /* Encode owner handle. */
  174. if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
  175. return NULL;
  176. start = loff_t_to_s64(fl->fl_start);
  177. if (fl->fl_end == OFFSET_MAX)
  178. len = 0;
  179. else
  180. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  181. p = xdr_encode_hyper(p, start);
  182. p = xdr_encode_hyper(p, len);
  183. dprintk("xdr: encode_testres (status %u pid %d type %d start %Ld end %Ld)\n",
  184. resp->status, (int)resp->lock.svid, fl->fl_type,
  185. (long long)fl->fl_start, (long long)fl->fl_end);
  186. }
  187. dprintk("xdr: after encode_testres (p %p resp %p)\n", p, resp);
  188. return p;
  189. }
  190. /*
  191. * First, the server side XDR functions
  192. */
  193. int
  194. nlm4svc_decode_testargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  195. {
  196. u32 exclusive;
  197. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  198. return 0;
  199. exclusive = ntohl(*p++);
  200. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  201. return 0;
  202. if (exclusive)
  203. argp->lock.fl.fl_type = F_WRLCK;
  204. return xdr_argsize_check(rqstp, p);
  205. }
  206. int
  207. nlm4svc_encode_testres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  208. {
  209. if (!(p = nlm4_encode_testres(p, resp)))
  210. return 0;
  211. return xdr_ressize_check(rqstp, p);
  212. }
  213. int
  214. nlm4svc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  215. {
  216. u32 exclusive;
  217. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  218. return 0;
  219. argp->block = ntohl(*p++);
  220. exclusive = ntohl(*p++);
  221. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  222. return 0;
  223. if (exclusive)
  224. argp->lock.fl.fl_type = F_WRLCK;
  225. argp->reclaim = ntohl(*p++);
  226. argp->state = ntohl(*p++);
  227. argp->monitor = 1; /* monitor client by default */
  228. return xdr_argsize_check(rqstp, p);
  229. }
  230. int
  231. nlm4svc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  232. {
  233. u32 exclusive;
  234. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  235. return 0;
  236. argp->block = ntohl(*p++);
  237. exclusive = ntohl(*p++);
  238. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  239. return 0;
  240. if (exclusive)
  241. argp->lock.fl.fl_type = F_WRLCK;
  242. return xdr_argsize_check(rqstp, p);
  243. }
  244. int
  245. nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  246. {
  247. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  248. || !(p = nlm4_decode_lock(p, &argp->lock)))
  249. return 0;
  250. argp->lock.fl.fl_type = F_UNLCK;
  251. return xdr_argsize_check(rqstp, p);
  252. }
  253. int
  254. nlm4svc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  255. {
  256. struct nlm_lock *lock = &argp->lock;
  257. memset(lock, 0, sizeof(*lock));
  258. locks_init_lock(&lock->fl);
  259. lock->svid = ~(u32) 0;
  260. lock->fl.fl_pid = (pid_t)lock->svid;
  261. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  262. || !(p = xdr_decode_string_inplace(p, &lock->caller,
  263. &lock->len, NLM_MAXSTRLEN))
  264. || !(p = nlm4_decode_fh(p, &lock->fh))
  265. || !(p = nlm4_decode_oh(p, &lock->oh)))
  266. return 0;
  267. argp->fsm_mode = ntohl(*p++);
  268. argp->fsm_access = ntohl(*p++);
  269. return xdr_argsize_check(rqstp, p);
  270. }
  271. int
  272. nlm4svc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  273. {
  274. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  275. return 0;
  276. *p++ = resp->status;
  277. *p++ = xdr_zero; /* sequence argument */
  278. return xdr_ressize_check(rqstp, p);
  279. }
  280. int
  281. nlm4svc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  282. {
  283. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  284. return 0;
  285. *p++ = resp->status;
  286. return xdr_ressize_check(rqstp, p);
  287. }
  288. int
  289. nlm4svc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
  290. {
  291. struct nlm_lock *lock = &argp->lock;
  292. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  293. &lock->len, NLM_MAXSTRLEN)))
  294. return 0;
  295. argp->state = ntohl(*p++);
  296. return xdr_argsize_check(rqstp, p);
  297. }
  298. int
  299. nlm4svc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
  300. {
  301. if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
  302. return 0;
  303. argp->state = ntohl(*p++);
  304. /* Preserve the address in network byte order */
  305. argp->addr = *p++;
  306. argp->vers = *p++;
  307. argp->proto = *p++;
  308. return xdr_argsize_check(rqstp, p);
  309. }
  310. int
  311. nlm4svc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  312. {
  313. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  314. return 0;
  315. resp->status = *p++;
  316. return xdr_argsize_check(rqstp, p);
  317. }
  318. int
  319. nlm4svc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  320. {
  321. return xdr_argsize_check(rqstp, p);
  322. }
  323. int
  324. nlm4svc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  325. {
  326. return xdr_ressize_check(rqstp, p);
  327. }
  328. /*
  329. * Now, the client side XDR functions
  330. */
  331. #ifdef NLMCLNT_SUPPORT_SHARES
  332. static int
  333. nlm4clt_decode_void(struct rpc_rqst *req, __be32 *p, void *ptr)
  334. {
  335. return 0;
  336. }
  337. #endif
  338. static int
  339. nlm4clt_encode_testargs(struct rpc_rqst *req, __be32 *p, nlm_args *argp)
  340. {
  341. struct nlm_lock *lock = &argp->lock;
  342. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  343. return -EIO;
  344. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  345. if (!(p = nlm4_encode_lock(p, lock)))
  346. return -EIO;
  347. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  348. return 0;
  349. }
  350. static int
  351. nlm4clt_decode_testres(struct rpc_rqst *req, __be32 *p, struct nlm_res *resp)
  352. {
  353. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  354. return -EIO;
  355. resp->status = *p++;
  356. if (resp->status == nlm_lck_denied) {
  357. struct file_lock *fl = &resp->lock.fl;
  358. u32 excl;
  359. s64 start, end, len;
  360. memset(&resp->lock, 0, sizeof(resp->lock));
  361. locks_init_lock(fl);
  362. excl = ntohl(*p++);
  363. resp->lock.svid = ntohl(*p++);
  364. fl->fl_pid = (pid_t)resp->lock.svid;
  365. if (!(p = nlm4_decode_oh(p, &resp->lock.oh)))
  366. return -EIO;
  367. fl->fl_flags = FL_POSIX;
  368. fl->fl_type = excl? F_WRLCK : F_RDLCK;
  369. p = xdr_decode_hyper(p, &start);
  370. p = xdr_decode_hyper(p, &len);
  371. end = start + len - 1;
  372. fl->fl_start = s64_to_loff_t(start);
  373. if (len == 0 || end < 0)
  374. fl->fl_end = OFFSET_MAX;
  375. else
  376. fl->fl_end = s64_to_loff_t(end);
  377. }
  378. return 0;
  379. }
  380. static int
  381. nlm4clt_encode_lockargs(struct rpc_rqst *req, __be32 *p, nlm_args *argp)
  382. {
  383. struct nlm_lock *lock = &argp->lock;
  384. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  385. return -EIO;
  386. *p++ = argp->block? xdr_one : xdr_zero;
  387. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  388. if (!(p = nlm4_encode_lock(p, lock)))
  389. return -EIO;
  390. *p++ = argp->reclaim? xdr_one : xdr_zero;
  391. *p++ = htonl(argp->state);
  392. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  393. return 0;
  394. }
  395. static int
  396. nlm4clt_encode_cancargs(struct rpc_rqst *req, __be32 *p, nlm_args *argp)
  397. {
  398. struct nlm_lock *lock = &argp->lock;
  399. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  400. return -EIO;
  401. *p++ = argp->block? xdr_one : xdr_zero;
  402. *p++ = (lock->fl.fl_type == F_WRLCK)? xdr_one : xdr_zero;
  403. if (!(p = nlm4_encode_lock(p, lock)))
  404. return -EIO;
  405. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  406. return 0;
  407. }
  408. static int
  409. nlm4clt_encode_unlockargs(struct rpc_rqst *req, __be32 *p, nlm_args *argp)
  410. {
  411. struct nlm_lock *lock = &argp->lock;
  412. if (!(p = nlm4_encode_cookie(p, &argp->cookie)))
  413. return -EIO;
  414. if (!(p = nlm4_encode_lock(p, lock)))
  415. return -EIO;
  416. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  417. return 0;
  418. }
  419. static int
  420. nlm4clt_encode_res(struct rpc_rqst *req, __be32 *p, struct nlm_res *resp)
  421. {
  422. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  423. return -EIO;
  424. *p++ = resp->status;
  425. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  426. return 0;
  427. }
  428. static int
  429. nlm4clt_encode_testres(struct rpc_rqst *req, __be32 *p, struct nlm_res *resp)
  430. {
  431. if (!(p = nlm4_encode_testres(p, resp)))
  432. return -EIO;
  433. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  434. return 0;
  435. }
  436. static int
  437. nlm4clt_decode_res(struct rpc_rqst *req, __be32 *p, struct nlm_res *resp)
  438. {
  439. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  440. return -EIO;
  441. resp->status = *p++;
  442. return 0;
  443. }
  444. /*
  445. * Buffer requirements for NLM
  446. */
  447. #define NLM4_void_sz 0
  448. #define NLM4_cookie_sz 1+XDR_QUADLEN(NLM_MAXCOOKIELEN)
  449. #define NLM4_caller_sz 1+XDR_QUADLEN(NLM_MAXSTRLEN)
  450. #define NLM4_netobj_sz 1+XDR_QUADLEN(XDR_MAX_NETOBJ)
  451. /* #define NLM4_owner_sz 1+XDR_QUADLEN(NLM4_MAXOWNER) */
  452. #define NLM4_fhandle_sz 1+XDR_QUADLEN(NFS3_FHSIZE)
  453. #define NLM4_lock_sz 5+NLM4_caller_sz+NLM4_netobj_sz+NLM4_fhandle_sz
  454. #define NLM4_holder_sz 6+NLM4_netobj_sz
  455. #define NLM4_testargs_sz NLM4_cookie_sz+1+NLM4_lock_sz
  456. #define NLM4_lockargs_sz NLM4_cookie_sz+4+NLM4_lock_sz
  457. #define NLM4_cancargs_sz NLM4_cookie_sz+2+NLM4_lock_sz
  458. #define NLM4_unlockargs_sz NLM4_cookie_sz+NLM4_lock_sz
  459. #define NLM4_testres_sz NLM4_cookie_sz+1+NLM4_holder_sz
  460. #define NLM4_res_sz NLM4_cookie_sz+1
  461. #define NLM4_norep_sz 0
  462. #ifndef MAX
  463. # define MAX(a,b) (((a) > (b))? (a) : (b))
  464. #endif
  465. /*
  466. * For NLM, a void procedure really returns nothing
  467. */
  468. #define nlm4clt_decode_norep NULL
  469. #define PROC(proc, argtype, restype) \
  470. [NLMPROC_##proc] = { \
  471. .p_proc = NLMPROC_##proc, \
  472. .p_encode = (kxdrproc_t) nlm4clt_encode_##argtype, \
  473. .p_decode = (kxdrproc_t) nlm4clt_decode_##restype, \
  474. .p_bufsiz = MAX(NLM4_##argtype##_sz, NLM4_##restype##_sz) << 2, \
  475. .p_statidx = NLMPROC_##proc, \
  476. .p_name = #proc, \
  477. }
  478. static struct rpc_procinfo nlm4_procedures[] = {
  479. PROC(TEST, testargs, testres),
  480. PROC(LOCK, lockargs, res),
  481. PROC(CANCEL, cancargs, res),
  482. PROC(UNLOCK, unlockargs, res),
  483. PROC(GRANTED, testargs, res),
  484. PROC(TEST_MSG, testargs, norep),
  485. PROC(LOCK_MSG, lockargs, norep),
  486. PROC(CANCEL_MSG, cancargs, norep),
  487. PROC(UNLOCK_MSG, unlockargs, norep),
  488. PROC(GRANTED_MSG, testargs, norep),
  489. PROC(TEST_RES, testres, norep),
  490. PROC(LOCK_RES, res, norep),
  491. PROC(CANCEL_RES, res, norep),
  492. PROC(UNLOCK_RES, res, norep),
  493. PROC(GRANTED_RES, res, norep),
  494. #ifdef NLMCLNT_SUPPORT_SHARES
  495. PROC(SHARE, shareargs, shareres),
  496. PROC(UNSHARE, shareargs, shareres),
  497. PROC(NM_LOCK, lockargs, res),
  498. PROC(FREE_ALL, notify, void),
  499. #endif
  500. };
  501. struct rpc_version nlm_version4 = {
  502. .number = 4,
  503. .nrprocs = 24,
  504. .procs = nlm4_procedures,
  505. };