callback_xdr.c 26 KB

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