callback_xdr.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /*
  2. * linux/fs/nfs/callback_xdr.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFSv4 callback encode/decode procedures
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/sunrpc/svc.h>
  10. #include <linux/nfs4.h>
  11. #include <linux/nfs_fs.h>
  12. #include <linux/ratelimit.h>
  13. #include <linux/printk.h>
  14. #include <linux/slab.h>
  15. #include <linux/sunrpc/bc_xprt.h>
  16. #include "nfs4_fs.h"
  17. #include "callback.h"
  18. #include "internal.h"
  19. #define CB_OP_TAGLEN_MAXSZ (512)
  20. #define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
  21. #define CB_OP_GETATTR_BITMAP_MAXSZ (4)
  22. #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
  23. CB_OP_GETATTR_BITMAP_MAXSZ + \
  24. 2 + 2 + 3 + 3)
  25. #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  26. #if defined(CONFIG_NFS_V4_1)
  27. #define CB_OP_LAYOUTRECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  28. #define CB_OP_DEVICENOTIFY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  29. #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
  30. 4 + 1 + 3)
  31. #define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  32. #define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  33. #endif /* CONFIG_NFS_V4_1 */
  34. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  35. /* Internal error code */
  36. #define NFS4ERR_RESOURCE_HDR 11050
  37. typedef __be32 (*callback_process_op_t)(void *, void *,
  38. struct cb_process_state *);
  39. typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
  40. typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
  41. struct callback_op {
  42. callback_process_op_t process_op;
  43. callback_decode_arg_t decode_args;
  44. callback_encode_res_t encode_res;
  45. long res_maxsize;
  46. };
  47. static struct callback_op callback_ops[];
  48. static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
  49. {
  50. return htonl(NFS4_OK);
  51. }
  52. static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  53. {
  54. return xdr_argsize_check(rqstp, p);
  55. }
  56. static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  57. {
  58. return xdr_ressize_check(rqstp, p);
  59. }
  60. static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
  61. {
  62. __be32 *p;
  63. p = xdr_inline_decode(xdr, nbytes);
  64. if (unlikely(p == NULL))
  65. printk(KERN_WARNING "NFS: NFSv4 callback reply buffer overflowed!\n");
  66. return p;
  67. }
  68. static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
  69. {
  70. __be32 *p;
  71. p = read_buf(xdr, 4);
  72. if (unlikely(p == NULL))
  73. return htonl(NFS4ERR_RESOURCE);
  74. *len = ntohl(*p);
  75. if (*len != 0) {
  76. p = read_buf(xdr, *len);
  77. if (unlikely(p == NULL))
  78. return htonl(NFS4ERR_RESOURCE);
  79. *str = (const char *)p;
  80. } else
  81. *str = NULL;
  82. return 0;
  83. }
  84. static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
  85. {
  86. __be32 *p;
  87. p = read_buf(xdr, 4);
  88. if (unlikely(p == NULL))
  89. return htonl(NFS4ERR_RESOURCE);
  90. fh->size = ntohl(*p);
  91. if (fh->size > NFS4_FHSIZE)
  92. return htonl(NFS4ERR_BADHANDLE);
  93. p = read_buf(xdr, fh->size);
  94. if (unlikely(p == NULL))
  95. return htonl(NFS4ERR_RESOURCE);
  96. memcpy(&fh->data[0], p, fh->size);
  97. memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
  98. return 0;
  99. }
  100. static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
  101. {
  102. __be32 *p;
  103. unsigned int attrlen;
  104. p = read_buf(xdr, 4);
  105. if (unlikely(p == NULL))
  106. return htonl(NFS4ERR_RESOURCE);
  107. attrlen = ntohl(*p);
  108. p = read_buf(xdr, attrlen << 2);
  109. if (unlikely(p == NULL))
  110. return htonl(NFS4ERR_RESOURCE);
  111. if (likely(attrlen > 0))
  112. bitmap[0] = ntohl(*p++);
  113. if (attrlen > 1)
  114. bitmap[1] = ntohl(*p);
  115. return 0;
  116. }
  117. static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
  118. {
  119. __be32 *p;
  120. p = read_buf(xdr, NFS4_STATEID_SIZE);
  121. if (unlikely(p == NULL))
  122. return htonl(NFS4ERR_RESOURCE);
  123. memcpy(stateid, p, NFS4_STATEID_SIZE);
  124. return 0;
  125. }
  126. static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
  127. {
  128. __be32 *p;
  129. __be32 status;
  130. status = decode_string(xdr, &hdr->taglen, &hdr->tag);
  131. if (unlikely(status != 0))
  132. return status;
  133. /* We do not like overly long tags! */
  134. if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
  135. printk("NFS: NFSv4 CALLBACK %s: client sent tag of length %u\n",
  136. __func__, hdr->taglen);
  137. return htonl(NFS4ERR_RESOURCE);
  138. }
  139. p = read_buf(xdr, 12);
  140. if (unlikely(p == NULL))
  141. return htonl(NFS4ERR_RESOURCE);
  142. hdr->minorversion = ntohl(*p++);
  143. /* Check minor version is zero or one. */
  144. if (hdr->minorversion <= 1) {
  145. hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 */
  146. } else {
  147. pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
  148. "illegal minor version %u!\n",
  149. __func__, hdr->minorversion);
  150. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  151. }
  152. hdr->nops = ntohl(*p);
  153. dprintk("%s: minorversion %d nops %d\n", __func__,
  154. hdr->minorversion, hdr->nops);
  155. return 0;
  156. }
  157. static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
  158. {
  159. __be32 *p;
  160. p = read_buf(xdr, 4);
  161. if (unlikely(p == NULL))
  162. return htonl(NFS4ERR_RESOURCE_HDR);
  163. *op = ntohl(*p);
  164. return 0;
  165. }
  166. static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
  167. {
  168. __be32 status;
  169. status = decode_fh(xdr, &args->fh);
  170. if (unlikely(status != 0))
  171. goto out;
  172. args->addr = svc_addr(rqstp);
  173. status = decode_bitmap(xdr, args->bitmap);
  174. out:
  175. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  176. return status;
  177. }
  178. static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
  179. {
  180. __be32 *p;
  181. __be32 status;
  182. args->addr = svc_addr(rqstp);
  183. status = decode_stateid(xdr, &args->stateid);
  184. if (unlikely(status != 0))
  185. goto out;
  186. p = read_buf(xdr, 4);
  187. if (unlikely(p == NULL)) {
  188. status = htonl(NFS4ERR_RESOURCE);
  189. goto out;
  190. }
  191. args->truncate = ntohl(*p);
  192. status = decode_fh(xdr, &args->fh);
  193. out:
  194. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  195. return status;
  196. }
  197. #if defined(CONFIG_NFS_V4_1)
  198. static __be32 decode_layoutrecall_args(struct svc_rqst *rqstp,
  199. struct xdr_stream *xdr,
  200. struct cb_layoutrecallargs *args)
  201. {
  202. __be32 *p;
  203. __be32 status = 0;
  204. uint32_t iomode;
  205. args->cbl_addr = svc_addr(rqstp);
  206. p = read_buf(xdr, 4 * sizeof(uint32_t));
  207. if (unlikely(p == NULL)) {
  208. status = htonl(NFS4ERR_BADXDR);
  209. goto out;
  210. }
  211. args->cbl_layout_type = ntohl(*p++);
  212. /* Depite the spec's xdr, iomode really belongs in the FILE switch,
  213. * as it is unusable and ignored with the other types.
  214. */
  215. iomode = ntohl(*p++);
  216. args->cbl_layoutchanged = ntohl(*p++);
  217. args->cbl_recall_type = ntohl(*p++);
  218. if (args->cbl_recall_type == RETURN_FILE) {
  219. args->cbl_range.iomode = iomode;
  220. status = decode_fh(xdr, &args->cbl_fh);
  221. if (unlikely(status != 0))
  222. goto out;
  223. p = read_buf(xdr, 2 * sizeof(uint64_t));
  224. if (unlikely(p == NULL)) {
  225. status = htonl(NFS4ERR_BADXDR);
  226. goto out;
  227. }
  228. p = xdr_decode_hyper(p, &args->cbl_range.offset);
  229. p = xdr_decode_hyper(p, &args->cbl_range.length);
  230. status = decode_stateid(xdr, &args->cbl_stateid);
  231. if (unlikely(status != 0))
  232. goto out;
  233. } else if (args->cbl_recall_type == RETURN_FSID) {
  234. p = read_buf(xdr, 2 * sizeof(uint64_t));
  235. if (unlikely(p == NULL)) {
  236. status = htonl(NFS4ERR_BADXDR);
  237. goto out;
  238. }
  239. p = xdr_decode_hyper(p, &args->cbl_fsid.major);
  240. p = xdr_decode_hyper(p, &args->cbl_fsid.minor);
  241. } else if (args->cbl_recall_type != RETURN_ALL) {
  242. status = htonl(NFS4ERR_BADXDR);
  243. goto out;
  244. }
  245. dprintk("%s: ltype 0x%x iomode %d changed %d recall_type %d\n",
  246. __func__,
  247. args->cbl_layout_type, iomode,
  248. args->cbl_layoutchanged, args->cbl_recall_type);
  249. out:
  250. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  251. return status;
  252. }
  253. static
  254. __be32 decode_devicenotify_args(struct svc_rqst *rqstp,
  255. struct xdr_stream *xdr,
  256. struct cb_devicenotifyargs *args)
  257. {
  258. __be32 *p;
  259. __be32 status = 0;
  260. u32 tmp;
  261. int n, i;
  262. args->ndevs = 0;
  263. /* Num of device notifications */
  264. p = read_buf(xdr, sizeof(uint32_t));
  265. if (unlikely(p == NULL)) {
  266. status = htonl(NFS4ERR_BADXDR);
  267. goto out;
  268. }
  269. n = ntohl(*p++);
  270. if (n <= 0)
  271. goto out;
  272. if (n > ULONG_MAX / sizeof(*args->devs)) {
  273. status = htonl(NFS4ERR_BADXDR);
  274. goto out;
  275. }
  276. args->devs = kmalloc(n * sizeof(*args->devs), GFP_KERNEL);
  277. if (!args->devs) {
  278. status = htonl(NFS4ERR_DELAY);
  279. goto out;
  280. }
  281. /* Decode each dev notification */
  282. for (i = 0; i < n; i++) {
  283. struct cb_devicenotifyitem *dev = &args->devs[i];
  284. p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
  285. if (unlikely(p == NULL)) {
  286. status = htonl(NFS4ERR_BADXDR);
  287. goto err;
  288. }
  289. tmp = ntohl(*p++); /* bitmap size */
  290. if (tmp != 1) {
  291. status = htonl(NFS4ERR_INVAL);
  292. goto err;
  293. }
  294. dev->cbd_notify_type = ntohl(*p++);
  295. if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
  296. dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
  297. status = htonl(NFS4ERR_INVAL);
  298. goto err;
  299. }
  300. tmp = ntohl(*p++); /* opaque size */
  301. if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
  302. (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
  303. ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
  304. (tmp != NFS4_DEVICEID4_SIZE + 4))) {
  305. status = htonl(NFS4ERR_INVAL);
  306. goto err;
  307. }
  308. dev->cbd_layout_type = ntohl(*p++);
  309. memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
  310. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  311. if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
  312. p = read_buf(xdr, sizeof(uint32_t));
  313. if (unlikely(p == NULL)) {
  314. status = htonl(NFS4ERR_BADXDR);
  315. goto err;
  316. }
  317. dev->cbd_immediate = ntohl(*p++);
  318. } else {
  319. dev->cbd_immediate = 0;
  320. }
  321. args->ndevs++;
  322. dprintk("%s: type %d layout 0x%x immediate %d\n",
  323. __func__, dev->cbd_notify_type, dev->cbd_layout_type,
  324. dev->cbd_immediate);
  325. }
  326. out:
  327. dprintk("%s: status %d ndevs %d\n",
  328. __func__, ntohl(status), args->ndevs);
  329. return status;
  330. err:
  331. kfree(args->devs);
  332. goto out;
  333. }
  334. static __be32 decode_sessionid(struct xdr_stream *xdr,
  335. struct nfs4_sessionid *sid)
  336. {
  337. __be32 *p;
  338. int len = NFS4_MAX_SESSIONID_LEN;
  339. p = read_buf(xdr, len);
  340. if (unlikely(p == NULL))
  341. return htonl(NFS4ERR_RESOURCE);
  342. memcpy(sid->data, p, len);
  343. return 0;
  344. }
  345. static __be32 decode_rc_list(struct xdr_stream *xdr,
  346. struct referring_call_list *rc_list)
  347. {
  348. __be32 *p;
  349. int i;
  350. __be32 status;
  351. status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
  352. if (status)
  353. goto out;
  354. status = htonl(NFS4ERR_RESOURCE);
  355. p = read_buf(xdr, sizeof(uint32_t));
  356. if (unlikely(p == NULL))
  357. goto out;
  358. rc_list->rcl_nrefcalls = ntohl(*p++);
  359. if (rc_list->rcl_nrefcalls) {
  360. p = read_buf(xdr,
  361. rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
  362. if (unlikely(p == NULL))
  363. goto out;
  364. rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
  365. sizeof(*rc_list->rcl_refcalls),
  366. GFP_KERNEL);
  367. if (unlikely(rc_list->rcl_refcalls == NULL))
  368. goto out;
  369. for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
  370. rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
  371. rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
  372. }
  373. }
  374. status = 0;
  375. out:
  376. return status;
  377. }
  378. static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
  379. struct xdr_stream *xdr,
  380. struct cb_sequenceargs *args)
  381. {
  382. __be32 *p;
  383. int i;
  384. __be32 status;
  385. status = decode_sessionid(xdr, &args->csa_sessionid);
  386. if (status)
  387. goto out;
  388. status = htonl(NFS4ERR_RESOURCE);
  389. p = read_buf(xdr, 5 * sizeof(uint32_t));
  390. if (unlikely(p == NULL))
  391. goto out;
  392. args->csa_addr = svc_addr(rqstp);
  393. args->csa_sequenceid = ntohl(*p++);
  394. args->csa_slotid = ntohl(*p++);
  395. args->csa_highestslotid = ntohl(*p++);
  396. args->csa_cachethis = ntohl(*p++);
  397. args->csa_nrclists = ntohl(*p++);
  398. args->csa_rclists = NULL;
  399. if (args->csa_nrclists) {
  400. args->csa_rclists = kmalloc_array(args->csa_nrclists,
  401. sizeof(*args->csa_rclists),
  402. GFP_KERNEL);
  403. if (unlikely(args->csa_rclists == NULL))
  404. goto out;
  405. for (i = 0; i < args->csa_nrclists; i++) {
  406. status = decode_rc_list(xdr, &args->csa_rclists[i]);
  407. if (status) {
  408. args->csa_nrclists = i;
  409. goto out_free;
  410. }
  411. }
  412. }
  413. status = 0;
  414. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
  415. "highestslotid %u cachethis %d nrclists %u\n",
  416. __func__,
  417. ((u32 *)&args->csa_sessionid)[0],
  418. ((u32 *)&args->csa_sessionid)[1],
  419. ((u32 *)&args->csa_sessionid)[2],
  420. ((u32 *)&args->csa_sessionid)[3],
  421. args->csa_sequenceid, args->csa_slotid,
  422. args->csa_highestslotid, args->csa_cachethis,
  423. args->csa_nrclists);
  424. out:
  425. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  426. return status;
  427. out_free:
  428. for (i = 0; i < args->csa_nrclists; i++)
  429. kfree(args->csa_rclists[i].rcl_refcalls);
  430. kfree(args->csa_rclists);
  431. goto out;
  432. }
  433. static __be32 decode_recallany_args(struct svc_rqst *rqstp,
  434. struct xdr_stream *xdr,
  435. struct cb_recallanyargs *args)
  436. {
  437. uint32_t bitmap[2];
  438. __be32 *p, status;
  439. args->craa_addr = svc_addr(rqstp);
  440. p = read_buf(xdr, 4);
  441. if (unlikely(p == NULL))
  442. return htonl(NFS4ERR_BADXDR);
  443. args->craa_objs_to_keep = ntohl(*p++);
  444. status = decode_bitmap(xdr, bitmap);
  445. if (unlikely(status))
  446. return status;
  447. args->craa_type_mask = bitmap[0];
  448. return 0;
  449. }
  450. static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
  451. struct xdr_stream *xdr,
  452. struct cb_recallslotargs *args)
  453. {
  454. __be32 *p;
  455. args->crsa_addr = svc_addr(rqstp);
  456. p = read_buf(xdr, 4);
  457. if (unlikely(p == NULL))
  458. return htonl(NFS4ERR_BADXDR);
  459. args->crsa_target_max_slots = ntohl(*p++);
  460. return 0;
  461. }
  462. #endif /* CONFIG_NFS_V4_1 */
  463. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  464. {
  465. __be32 *p;
  466. p = xdr_reserve_space(xdr, 4 + len);
  467. if (unlikely(p == NULL))
  468. return htonl(NFS4ERR_RESOURCE);
  469. xdr_encode_opaque(p, str, len);
  470. return 0;
  471. }
  472. #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
  473. #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
  474. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
  475. {
  476. __be32 bm[2];
  477. __be32 *p;
  478. bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
  479. bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
  480. if (bm[1] != 0) {
  481. p = xdr_reserve_space(xdr, 16);
  482. if (unlikely(p == NULL))
  483. return htonl(NFS4ERR_RESOURCE);
  484. *p++ = htonl(2);
  485. *p++ = bm[0];
  486. *p++ = bm[1];
  487. } else if (bm[0] != 0) {
  488. p = xdr_reserve_space(xdr, 12);
  489. if (unlikely(p == NULL))
  490. return htonl(NFS4ERR_RESOURCE);
  491. *p++ = htonl(1);
  492. *p++ = bm[0];
  493. } else {
  494. p = xdr_reserve_space(xdr, 8);
  495. if (unlikely(p == NULL))
  496. return htonl(NFS4ERR_RESOURCE);
  497. *p++ = htonl(0);
  498. }
  499. *savep = p;
  500. return 0;
  501. }
  502. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  503. {
  504. __be32 *p;
  505. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  506. return 0;
  507. p = xdr_reserve_space(xdr, 8);
  508. if (unlikely(!p))
  509. return htonl(NFS4ERR_RESOURCE);
  510. p = xdr_encode_hyper(p, change);
  511. return 0;
  512. }
  513. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  514. {
  515. __be32 *p;
  516. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  517. return 0;
  518. p = xdr_reserve_space(xdr, 8);
  519. if (unlikely(!p))
  520. return htonl(NFS4ERR_RESOURCE);
  521. p = xdr_encode_hyper(p, size);
  522. return 0;
  523. }
  524. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
  525. {
  526. __be32 *p;
  527. p = xdr_reserve_space(xdr, 12);
  528. if (unlikely(!p))
  529. return htonl(NFS4ERR_RESOURCE);
  530. p = xdr_encode_hyper(p, time->tv_sec);
  531. *p = htonl(time->tv_nsec);
  532. return 0;
  533. }
  534. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  535. {
  536. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  537. return 0;
  538. return encode_attr_time(xdr,time);
  539. }
  540. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  541. {
  542. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  543. return 0;
  544. return encode_attr_time(xdr,time);
  545. }
  546. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  547. {
  548. __be32 status;
  549. hdr->status = xdr_reserve_space(xdr, 4);
  550. if (unlikely(hdr->status == NULL))
  551. return htonl(NFS4ERR_RESOURCE);
  552. status = encode_string(xdr, hdr->taglen, hdr->tag);
  553. if (unlikely(status != 0))
  554. return status;
  555. hdr->nops = xdr_reserve_space(xdr, 4);
  556. if (unlikely(hdr->nops == NULL))
  557. return htonl(NFS4ERR_RESOURCE);
  558. return 0;
  559. }
  560. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  561. {
  562. __be32 *p;
  563. p = xdr_reserve_space(xdr, 8);
  564. if (unlikely(p == NULL))
  565. return htonl(NFS4ERR_RESOURCE_HDR);
  566. *p++ = htonl(op);
  567. *p = res;
  568. return 0;
  569. }
  570. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
  571. {
  572. __be32 *savep = NULL;
  573. __be32 status = res->status;
  574. if (unlikely(status != 0))
  575. goto out;
  576. status = encode_attr_bitmap(xdr, res->bitmap, &savep);
  577. if (unlikely(status != 0))
  578. goto out;
  579. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  580. if (unlikely(status != 0))
  581. goto out;
  582. status = encode_attr_size(xdr, res->bitmap, res->size);
  583. if (unlikely(status != 0))
  584. goto out;
  585. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  586. if (unlikely(status != 0))
  587. goto out;
  588. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  589. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  590. out:
  591. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  592. return status;
  593. }
  594. #if defined(CONFIG_NFS_V4_1)
  595. static __be32 encode_sessionid(struct xdr_stream *xdr,
  596. const struct nfs4_sessionid *sid)
  597. {
  598. __be32 *p;
  599. int len = NFS4_MAX_SESSIONID_LEN;
  600. p = xdr_reserve_space(xdr, len);
  601. if (unlikely(p == NULL))
  602. return htonl(NFS4ERR_RESOURCE);
  603. memcpy(p, sid, len);
  604. return 0;
  605. }
  606. static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
  607. struct xdr_stream *xdr,
  608. const struct cb_sequenceres *res)
  609. {
  610. __be32 *p;
  611. unsigned status = res->csr_status;
  612. if (unlikely(status != 0))
  613. goto out;
  614. encode_sessionid(xdr, &res->csr_sessionid);
  615. p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
  616. if (unlikely(p == NULL))
  617. return htonl(NFS4ERR_RESOURCE);
  618. *p++ = htonl(res->csr_sequenceid);
  619. *p++ = htonl(res->csr_slotid);
  620. *p++ = htonl(res->csr_highestslotid);
  621. *p++ = htonl(res->csr_target_highestslotid);
  622. out:
  623. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  624. return status;
  625. }
  626. static __be32
  627. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  628. {
  629. if (op_nr == OP_CB_SEQUENCE) {
  630. if (nop != 0)
  631. return htonl(NFS4ERR_SEQUENCE_POS);
  632. } else {
  633. if (nop == 0)
  634. return htonl(NFS4ERR_OP_NOT_IN_SESSION);
  635. }
  636. switch (op_nr) {
  637. case OP_CB_GETATTR:
  638. case OP_CB_RECALL:
  639. case OP_CB_SEQUENCE:
  640. case OP_CB_RECALL_ANY:
  641. case OP_CB_RECALL_SLOT:
  642. case OP_CB_LAYOUTRECALL:
  643. case OP_CB_NOTIFY_DEVICEID:
  644. *op = &callback_ops[op_nr];
  645. break;
  646. case OP_CB_NOTIFY:
  647. case OP_CB_PUSH_DELEG:
  648. case OP_CB_RECALLABLE_OBJ_AVAIL:
  649. case OP_CB_WANTS_CANCELLED:
  650. case OP_CB_NOTIFY_LOCK:
  651. return htonl(NFS4ERR_NOTSUPP);
  652. default:
  653. return htonl(NFS4ERR_OP_ILLEGAL);
  654. }
  655. return htonl(NFS_OK);
  656. }
  657. static void nfs4_callback_free_slot(struct nfs4_session *session)
  658. {
  659. struct nfs4_slot_table *tbl = &session->bc_slot_table;
  660. spin_lock(&tbl->slot_tbl_lock);
  661. /*
  662. * Let the state manager know callback processing done.
  663. * A single slot, so highest used slotid is either 0 or -1
  664. */
  665. tbl->highest_used_slotid = NFS4_NO_SLOT;
  666. nfs4_check_drain_bc_complete(session);
  667. spin_unlock(&tbl->slot_tbl_lock);
  668. }
  669. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  670. {
  671. if (cps->slotid != NFS4_NO_SLOT)
  672. nfs4_callback_free_slot(cps->clp->cl_session);
  673. }
  674. #else /* CONFIG_NFS_V4_1 */
  675. static __be32
  676. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  677. {
  678. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  679. }
  680. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  681. {
  682. }
  683. #endif /* CONFIG_NFS_V4_1 */
  684. static __be32
  685. preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
  686. {
  687. switch (op_nr) {
  688. case OP_CB_GETATTR:
  689. case OP_CB_RECALL:
  690. *op = &callback_ops[op_nr];
  691. break;
  692. default:
  693. return htonl(NFS4ERR_OP_ILLEGAL);
  694. }
  695. return htonl(NFS_OK);
  696. }
  697. static __be32 process_op(uint32_t minorversion, int nop,
  698. struct svc_rqst *rqstp,
  699. struct xdr_stream *xdr_in, void *argp,
  700. struct xdr_stream *xdr_out, void *resp,
  701. struct cb_process_state *cps)
  702. {
  703. struct callback_op *op = &callback_ops[0];
  704. unsigned int op_nr;
  705. __be32 status;
  706. long maxlen;
  707. __be32 res;
  708. dprintk("%s: start\n", __func__);
  709. status = decode_op_hdr(xdr_in, &op_nr);
  710. if (unlikely(status))
  711. return status;
  712. dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
  713. __func__, minorversion, nop, op_nr);
  714. status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
  715. preprocess_nfs4_op(op_nr, &op);
  716. if (status == htonl(NFS4ERR_OP_ILLEGAL))
  717. op_nr = OP_CB_ILLEGAL;
  718. if (status)
  719. goto encode_hdr;
  720. if (cps->drc_status) {
  721. status = cps->drc_status;
  722. goto encode_hdr;
  723. }
  724. maxlen = xdr_out->end - xdr_out->p;
  725. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  726. status = op->decode_args(rqstp, xdr_in, argp);
  727. if (likely(status == 0))
  728. status = op->process_op(argp, resp, cps);
  729. } else
  730. status = htonl(NFS4ERR_RESOURCE);
  731. encode_hdr:
  732. res = encode_op_hdr(xdr_out, op_nr, status);
  733. if (unlikely(res))
  734. return res;
  735. if (op->encode_res != NULL && status == 0)
  736. status = op->encode_res(rqstp, xdr_out, resp);
  737. dprintk("%s: done, status = %d\n", __func__, ntohl(status));
  738. return status;
  739. }
  740. /*
  741. * Decode, process and encode a COMPOUND
  742. */
  743. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
  744. {
  745. struct cb_compound_hdr_arg hdr_arg = { 0 };
  746. struct cb_compound_hdr_res hdr_res = { NULL };
  747. struct xdr_stream xdr_in, xdr_out;
  748. __be32 *p, status;
  749. struct cb_process_state cps = {
  750. .drc_status = 0,
  751. .clp = NULL,
  752. .slotid = NFS4_NO_SLOT,
  753. .net = rqstp->rq_xprt->xpt_net,
  754. };
  755. unsigned int nops = 0;
  756. dprintk("%s: start\n", __func__);
  757. xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
  758. p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
  759. xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
  760. status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
  761. if (status == __constant_htonl(NFS4ERR_RESOURCE))
  762. return rpc_garbage_args;
  763. if (hdr_arg.minorversion == 0) {
  764. cps.clp = nfs4_find_client_ident(rqstp->rq_xprt->xpt_net, hdr_arg.cb_ident);
  765. if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
  766. return rpc_drop_reply;
  767. }
  768. hdr_res.taglen = hdr_arg.taglen;
  769. hdr_res.tag = hdr_arg.tag;
  770. if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
  771. return rpc_system_err;
  772. while (status == 0 && nops != hdr_arg.nops) {
  773. status = process_op(hdr_arg.minorversion, nops, rqstp,
  774. &xdr_in, argp, &xdr_out, resp, &cps);
  775. nops++;
  776. }
  777. /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
  778. * resource error in cb_compound status without returning op */
  779. if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
  780. status = htonl(NFS4ERR_RESOURCE);
  781. nops--;
  782. }
  783. *hdr_res.status = status;
  784. *hdr_res.nops = htonl(nops);
  785. nfs4_cb_free_slot(&cps);
  786. nfs_put_client(cps.clp);
  787. dprintk("%s: done, status = %u\n", __func__, ntohl(status));
  788. return rpc_success;
  789. }
  790. /*
  791. * Define NFS4 callback COMPOUND ops.
  792. */
  793. static struct callback_op callback_ops[] = {
  794. [0] = {
  795. .res_maxsize = CB_OP_HDR_RES_MAXSZ,
  796. },
  797. [OP_CB_GETATTR] = {
  798. .process_op = (callback_process_op_t)nfs4_callback_getattr,
  799. .decode_args = (callback_decode_arg_t)decode_getattr_args,
  800. .encode_res = (callback_encode_res_t)encode_getattr_res,
  801. .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
  802. },
  803. [OP_CB_RECALL] = {
  804. .process_op = (callback_process_op_t)nfs4_callback_recall,
  805. .decode_args = (callback_decode_arg_t)decode_recall_args,
  806. .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
  807. },
  808. #if defined(CONFIG_NFS_V4_1)
  809. [OP_CB_LAYOUTRECALL] = {
  810. .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
  811. .decode_args =
  812. (callback_decode_arg_t)decode_layoutrecall_args,
  813. .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
  814. },
  815. [OP_CB_NOTIFY_DEVICEID] = {
  816. .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
  817. .decode_args =
  818. (callback_decode_arg_t)decode_devicenotify_args,
  819. .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
  820. },
  821. [OP_CB_SEQUENCE] = {
  822. .process_op = (callback_process_op_t)nfs4_callback_sequence,
  823. .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
  824. .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
  825. .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
  826. },
  827. [OP_CB_RECALL_ANY] = {
  828. .process_op = (callback_process_op_t)nfs4_callback_recallany,
  829. .decode_args = (callback_decode_arg_t)decode_recallany_args,
  830. .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
  831. },
  832. [OP_CB_RECALL_SLOT] = {
  833. .process_op = (callback_process_op_t)nfs4_callback_recallslot,
  834. .decode_args = (callback_decode_arg_t)decode_recallslot_args,
  835. .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
  836. },
  837. #endif /* CONFIG_NFS_V4_1 */
  838. };
  839. /*
  840. * Define NFS4 callback procedures
  841. */
  842. static struct svc_procedure nfs4_callback_procedures1[] = {
  843. [CB_NULL] = {
  844. .pc_func = nfs4_callback_null,
  845. .pc_decode = (kxdrproc_t)nfs4_decode_void,
  846. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  847. .pc_xdrressize = 1,
  848. },
  849. [CB_COMPOUND] = {
  850. .pc_func = nfs4_callback_compound,
  851. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  852. .pc_argsize = 256,
  853. .pc_ressize = 256,
  854. .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
  855. }
  856. };
  857. struct svc_version nfs4_callback_version1 = {
  858. .vs_vers = 1,
  859. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  860. .vs_proc = nfs4_callback_procedures1,
  861. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  862. .vs_dispatch = NULL,
  863. .vs_hidden = 1,
  864. };
  865. struct svc_version nfs4_callback_version4 = {
  866. .vs_vers = 4,
  867. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  868. .vs_proc = nfs4_callback_procedures1,
  869. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  870. .vs_dispatch = NULL,
  871. .vs_hidden = 1,
  872. };