callback_xdr.c 27 KB

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