callback_xdr.c 26 KB

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