callback_xdr.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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(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(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. goto out_free;
  410. }
  411. }
  412. status = 0;
  413. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
  414. "highestslotid %u cachethis %d nrclists %u\n",
  415. __func__,
  416. ((u32 *)&args->csa_sessionid)[0],
  417. ((u32 *)&args->csa_sessionid)[1],
  418. ((u32 *)&args->csa_sessionid)[2],
  419. ((u32 *)&args->csa_sessionid)[3],
  420. args->csa_sequenceid, args->csa_slotid,
  421. args->csa_highestslotid, args->csa_cachethis,
  422. args->csa_nrclists);
  423. out:
  424. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  425. return status;
  426. out_free:
  427. for (i = 0; i < args->csa_nrclists; i++)
  428. kfree(args->csa_rclists[i].rcl_refcalls);
  429. kfree(args->csa_rclists);
  430. goto out;
  431. }
  432. static __be32 decode_recallany_args(struct svc_rqst *rqstp,
  433. struct xdr_stream *xdr,
  434. struct cb_recallanyargs *args)
  435. {
  436. uint32_t bitmap[2];
  437. __be32 *p, status;
  438. args->craa_addr = svc_addr(rqstp);
  439. p = read_buf(xdr, 4);
  440. if (unlikely(p == NULL))
  441. return htonl(NFS4ERR_BADXDR);
  442. args->craa_objs_to_keep = ntohl(*p++);
  443. status = decode_bitmap(xdr, bitmap);
  444. if (unlikely(status))
  445. return status;
  446. args->craa_type_mask = bitmap[0];
  447. return 0;
  448. }
  449. static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
  450. struct xdr_stream *xdr,
  451. struct cb_recallslotargs *args)
  452. {
  453. __be32 *p;
  454. args->crsa_addr = svc_addr(rqstp);
  455. p = read_buf(xdr, 4);
  456. if (unlikely(p == NULL))
  457. return htonl(NFS4ERR_BADXDR);
  458. args->crsa_target_highest_slotid = ntohl(*p++);
  459. return 0;
  460. }
  461. #endif /* CONFIG_NFS_V4_1 */
  462. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  463. {
  464. __be32 *p;
  465. p = xdr_reserve_space(xdr, 4 + len);
  466. if (unlikely(p == NULL))
  467. return htonl(NFS4ERR_RESOURCE);
  468. xdr_encode_opaque(p, str, len);
  469. return 0;
  470. }
  471. #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
  472. #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
  473. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
  474. {
  475. __be32 bm[2];
  476. __be32 *p;
  477. bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
  478. bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
  479. if (bm[1] != 0) {
  480. p = xdr_reserve_space(xdr, 16);
  481. if (unlikely(p == NULL))
  482. return htonl(NFS4ERR_RESOURCE);
  483. *p++ = htonl(2);
  484. *p++ = bm[0];
  485. *p++ = bm[1];
  486. } else if (bm[0] != 0) {
  487. p = xdr_reserve_space(xdr, 12);
  488. if (unlikely(p == NULL))
  489. return htonl(NFS4ERR_RESOURCE);
  490. *p++ = htonl(1);
  491. *p++ = bm[0];
  492. } else {
  493. p = xdr_reserve_space(xdr, 8);
  494. if (unlikely(p == NULL))
  495. return htonl(NFS4ERR_RESOURCE);
  496. *p++ = htonl(0);
  497. }
  498. *savep = p;
  499. return 0;
  500. }
  501. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  502. {
  503. __be32 *p;
  504. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  505. return 0;
  506. p = xdr_reserve_space(xdr, 8);
  507. if (unlikely(!p))
  508. return htonl(NFS4ERR_RESOURCE);
  509. p = xdr_encode_hyper(p, change);
  510. return 0;
  511. }
  512. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  513. {
  514. __be32 *p;
  515. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  516. return 0;
  517. p = xdr_reserve_space(xdr, 8);
  518. if (unlikely(!p))
  519. return htonl(NFS4ERR_RESOURCE);
  520. p = xdr_encode_hyper(p, size);
  521. return 0;
  522. }
  523. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
  524. {
  525. __be32 *p;
  526. p = xdr_reserve_space(xdr, 12);
  527. if (unlikely(!p))
  528. return htonl(NFS4ERR_RESOURCE);
  529. p = xdr_encode_hyper(p, time->tv_sec);
  530. *p = htonl(time->tv_nsec);
  531. return 0;
  532. }
  533. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  534. {
  535. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  536. return 0;
  537. return encode_attr_time(xdr,time);
  538. }
  539. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  540. {
  541. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  542. return 0;
  543. return encode_attr_time(xdr,time);
  544. }
  545. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  546. {
  547. __be32 status;
  548. hdr->status = xdr_reserve_space(xdr, 4);
  549. if (unlikely(hdr->status == NULL))
  550. return htonl(NFS4ERR_RESOURCE);
  551. status = encode_string(xdr, hdr->taglen, hdr->tag);
  552. if (unlikely(status != 0))
  553. return status;
  554. hdr->nops = xdr_reserve_space(xdr, 4);
  555. if (unlikely(hdr->nops == NULL))
  556. return htonl(NFS4ERR_RESOURCE);
  557. return 0;
  558. }
  559. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  560. {
  561. __be32 *p;
  562. p = xdr_reserve_space(xdr, 8);
  563. if (unlikely(p == NULL))
  564. return htonl(NFS4ERR_RESOURCE_HDR);
  565. *p++ = htonl(op);
  566. *p = res;
  567. return 0;
  568. }
  569. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
  570. {
  571. __be32 *savep = NULL;
  572. __be32 status = res->status;
  573. if (unlikely(status != 0))
  574. goto out;
  575. status = encode_attr_bitmap(xdr, res->bitmap, &savep);
  576. if (unlikely(status != 0))
  577. goto out;
  578. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  579. if (unlikely(status != 0))
  580. goto out;
  581. status = encode_attr_size(xdr, res->bitmap, res->size);
  582. if (unlikely(status != 0))
  583. goto out;
  584. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  585. if (unlikely(status != 0))
  586. goto out;
  587. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  588. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  589. out:
  590. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  591. return status;
  592. }
  593. #if defined(CONFIG_NFS_V4_1)
  594. static __be32 encode_sessionid(struct xdr_stream *xdr,
  595. const struct nfs4_sessionid *sid)
  596. {
  597. __be32 *p;
  598. int len = NFS4_MAX_SESSIONID_LEN;
  599. p = xdr_reserve_space(xdr, len);
  600. if (unlikely(p == NULL))
  601. return htonl(NFS4ERR_RESOURCE);
  602. memcpy(p, sid, len);
  603. return 0;
  604. }
  605. static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
  606. struct xdr_stream *xdr,
  607. const struct cb_sequenceres *res)
  608. {
  609. __be32 *p;
  610. __be32 status = res->csr_status;
  611. if (unlikely(status != 0))
  612. goto out;
  613. encode_sessionid(xdr, &res->csr_sessionid);
  614. p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
  615. if (unlikely(p == NULL))
  616. return htonl(NFS4ERR_RESOURCE);
  617. *p++ = htonl(res->csr_sequenceid);
  618. *p++ = htonl(res->csr_slotid);
  619. *p++ = htonl(res->csr_highestslotid);
  620. *p++ = htonl(res->csr_target_highestslotid);
  621. out:
  622. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  623. return status;
  624. }
  625. static __be32
  626. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  627. {
  628. if (op_nr == OP_CB_SEQUENCE) {
  629. if (nop != 0)
  630. return htonl(NFS4ERR_SEQUENCE_POS);
  631. } else {
  632. if (nop == 0)
  633. return htonl(NFS4ERR_OP_NOT_IN_SESSION);
  634. }
  635. switch (op_nr) {
  636. case OP_CB_GETATTR:
  637. case OP_CB_RECALL:
  638. case OP_CB_SEQUENCE:
  639. case OP_CB_RECALL_ANY:
  640. case OP_CB_RECALL_SLOT:
  641. case OP_CB_LAYOUTRECALL:
  642. case OP_CB_NOTIFY_DEVICEID:
  643. *op = &callback_ops[op_nr];
  644. break;
  645. case OP_CB_NOTIFY:
  646. case OP_CB_PUSH_DELEG:
  647. case OP_CB_RECALLABLE_OBJ_AVAIL:
  648. case OP_CB_WANTS_CANCELLED:
  649. case OP_CB_NOTIFY_LOCK:
  650. return htonl(NFS4ERR_NOTSUPP);
  651. default:
  652. return htonl(NFS4ERR_OP_ILLEGAL);
  653. }
  654. return htonl(NFS_OK);
  655. }
  656. static void nfs4_callback_free_slot(struct nfs4_session *session)
  657. {
  658. struct nfs4_slot_table *tbl = &session->bc_slot_table;
  659. spin_lock(&tbl->slot_tbl_lock);
  660. /*
  661. * Let the state manager know callback processing done.
  662. * A single slot, so highest used slotid is either 0 or -1
  663. */
  664. tbl->highest_used_slotid = NFS4_NO_SLOT;
  665. nfs4_slot_tbl_drain_complete(tbl);
  666. spin_unlock(&tbl->slot_tbl_lock);
  667. }
  668. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  669. {
  670. if (cps->slotid != NFS4_NO_SLOT)
  671. nfs4_callback_free_slot(cps->clp->cl_session);
  672. }
  673. #else /* CONFIG_NFS_V4_1 */
  674. static __be32
  675. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  676. {
  677. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  678. }
  679. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  680. {
  681. }
  682. #endif /* CONFIG_NFS_V4_1 */
  683. #ifdef CONFIG_NFS_V4_2
  684. static __be32
  685. preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
  686. {
  687. __be32 status = preprocess_nfs41_op(nop, op_nr, op);
  688. if (status != htonl(NFS4ERR_OP_ILLEGAL))
  689. return status;
  690. if (op_nr == OP_CB_OFFLOAD)
  691. return htonl(NFS4ERR_NOTSUPP);
  692. return htonl(NFS4ERR_OP_ILLEGAL);
  693. }
  694. #else /* CONFIG_NFS_V4_2 */
  695. static __be32
  696. preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
  697. {
  698. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  699. }
  700. #endif /* CONFIG_NFS_V4_2 */
  701. static __be32
  702. preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
  703. {
  704. switch (op_nr) {
  705. case OP_CB_GETATTR:
  706. case OP_CB_RECALL:
  707. *op = &callback_ops[op_nr];
  708. break;
  709. default:
  710. return htonl(NFS4ERR_OP_ILLEGAL);
  711. }
  712. return htonl(NFS_OK);
  713. }
  714. static __be32 process_op(int nop, struct svc_rqst *rqstp,
  715. struct xdr_stream *xdr_in, void *argp,
  716. struct xdr_stream *xdr_out, void *resp,
  717. struct cb_process_state *cps)
  718. {
  719. struct callback_op *op = &callback_ops[0];
  720. unsigned int op_nr;
  721. __be32 status;
  722. long maxlen;
  723. __be32 res;
  724. dprintk("%s: start\n", __func__);
  725. status = decode_op_hdr(xdr_in, &op_nr);
  726. if (unlikely(status))
  727. return status;
  728. dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
  729. __func__, cps->minorversion, nop, op_nr);
  730. switch (cps->minorversion) {
  731. case 0:
  732. status = preprocess_nfs4_op(op_nr, &op);
  733. break;
  734. case 1:
  735. status = preprocess_nfs41_op(nop, op_nr, &op);
  736. break;
  737. case 2:
  738. status = preprocess_nfs42_op(nop, op_nr, &op);
  739. break;
  740. default:
  741. status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  742. }
  743. if (status == htonl(NFS4ERR_OP_ILLEGAL))
  744. op_nr = OP_CB_ILLEGAL;
  745. if (status)
  746. goto encode_hdr;
  747. if (cps->drc_status) {
  748. status = cps->drc_status;
  749. goto encode_hdr;
  750. }
  751. maxlen = xdr_out->end - xdr_out->p;
  752. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  753. status = op->decode_args(rqstp, xdr_in, argp);
  754. if (likely(status == 0))
  755. status = op->process_op(argp, resp, cps);
  756. } else
  757. status = htonl(NFS4ERR_RESOURCE);
  758. encode_hdr:
  759. res = encode_op_hdr(xdr_out, op_nr, status);
  760. if (unlikely(res))
  761. return res;
  762. if (op->encode_res != NULL && status == 0)
  763. status = op->encode_res(rqstp, xdr_out, resp);
  764. dprintk("%s: done, status = %d\n", __func__, ntohl(status));
  765. return status;
  766. }
  767. /*
  768. * Decode, process and encode a COMPOUND
  769. */
  770. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
  771. {
  772. struct cb_compound_hdr_arg hdr_arg = { 0 };
  773. struct cb_compound_hdr_res hdr_res = { NULL };
  774. struct xdr_stream xdr_in, xdr_out;
  775. __be32 *p, status;
  776. struct cb_process_state cps = {
  777. .drc_status = 0,
  778. .clp = NULL,
  779. .slotid = NFS4_NO_SLOT,
  780. .net = SVC_NET(rqstp),
  781. };
  782. unsigned int nops = 0;
  783. dprintk("%s: start\n", __func__);
  784. xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
  785. p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
  786. xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
  787. status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
  788. if (status == __constant_htonl(NFS4ERR_RESOURCE))
  789. return rpc_garbage_args;
  790. if (hdr_arg.minorversion == 0) {
  791. cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
  792. if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
  793. return rpc_drop_reply;
  794. }
  795. cps.minorversion = hdr_arg.minorversion;
  796. hdr_res.taglen = hdr_arg.taglen;
  797. hdr_res.tag = hdr_arg.tag;
  798. if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
  799. return rpc_system_err;
  800. while (status == 0 && nops != hdr_arg.nops) {
  801. status = process_op(nops, rqstp, &xdr_in,
  802. argp, &xdr_out, resp, &cps);
  803. nops++;
  804. }
  805. /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
  806. * resource error in cb_compound status without returning op */
  807. if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
  808. status = htonl(NFS4ERR_RESOURCE);
  809. nops--;
  810. }
  811. *hdr_res.status = status;
  812. *hdr_res.nops = htonl(nops);
  813. nfs4_cb_free_slot(&cps);
  814. nfs_put_client(cps.clp);
  815. dprintk("%s: done, status = %u\n", __func__, ntohl(status));
  816. return rpc_success;
  817. }
  818. /*
  819. * Define NFS4 callback COMPOUND ops.
  820. */
  821. static struct callback_op callback_ops[] = {
  822. [0] = {
  823. .res_maxsize = CB_OP_HDR_RES_MAXSZ,
  824. },
  825. [OP_CB_GETATTR] = {
  826. .process_op = (callback_process_op_t)nfs4_callback_getattr,
  827. .decode_args = (callback_decode_arg_t)decode_getattr_args,
  828. .encode_res = (callback_encode_res_t)encode_getattr_res,
  829. .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
  830. },
  831. [OP_CB_RECALL] = {
  832. .process_op = (callback_process_op_t)nfs4_callback_recall,
  833. .decode_args = (callback_decode_arg_t)decode_recall_args,
  834. .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
  835. },
  836. #if defined(CONFIG_NFS_V4_1)
  837. [OP_CB_LAYOUTRECALL] = {
  838. .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
  839. .decode_args =
  840. (callback_decode_arg_t)decode_layoutrecall_args,
  841. .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
  842. },
  843. [OP_CB_NOTIFY_DEVICEID] = {
  844. .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
  845. .decode_args =
  846. (callback_decode_arg_t)decode_devicenotify_args,
  847. .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
  848. },
  849. [OP_CB_SEQUENCE] = {
  850. .process_op = (callback_process_op_t)nfs4_callback_sequence,
  851. .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
  852. .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
  853. .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
  854. },
  855. [OP_CB_RECALL_ANY] = {
  856. .process_op = (callback_process_op_t)nfs4_callback_recallany,
  857. .decode_args = (callback_decode_arg_t)decode_recallany_args,
  858. .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
  859. },
  860. [OP_CB_RECALL_SLOT] = {
  861. .process_op = (callback_process_op_t)nfs4_callback_recallslot,
  862. .decode_args = (callback_decode_arg_t)decode_recallslot_args,
  863. .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
  864. },
  865. #endif /* CONFIG_NFS_V4_1 */
  866. };
  867. /*
  868. * Define NFS4 callback procedures
  869. */
  870. static struct svc_procedure nfs4_callback_procedures1[] = {
  871. [CB_NULL] = {
  872. .pc_func = nfs4_callback_null,
  873. .pc_decode = (kxdrproc_t)nfs4_decode_void,
  874. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  875. .pc_xdrressize = 1,
  876. },
  877. [CB_COMPOUND] = {
  878. .pc_func = nfs4_callback_compound,
  879. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  880. .pc_argsize = 256,
  881. .pc_ressize = 256,
  882. .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
  883. }
  884. };
  885. struct svc_version nfs4_callback_version1 = {
  886. .vs_vers = 1,
  887. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  888. .vs_proc = nfs4_callback_procedures1,
  889. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  890. .vs_dispatch = NULL,
  891. .vs_hidden = 1,
  892. };
  893. struct svc_version nfs4_callback_version4 = {
  894. .vs_vers = 4,
  895. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  896. .vs_proc = nfs4_callback_procedures1,
  897. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  898. .vs_dispatch = NULL,
  899. .vs_hidden = 1,
  900. };