callback_xdr.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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, atime, ctime,
  28. * mtime, deleg_atime, deleg_mtime */\
  29. (2 + 2 + 3 + 3 + 3 + 3 + 3) * 4)
  30. #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  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. #ifdef CONFIG_NFS_V4_2
  40. #define CB_OP_OFFLOAD_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  41. #endif /* CONFIG_NFS_V4_2 */
  42. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  43. /* Internal error code */
  44. #define NFS4ERR_RESOURCE_HDR 11050
  45. struct callback_op {
  46. __be32 (*process_op)(void *, void *, struct cb_process_state *);
  47. __be32 (*decode_args)(struct svc_rqst *, struct xdr_stream *, void *);
  48. __be32 (*encode_res)(struct svc_rqst *, struct xdr_stream *,
  49. const void *);
  50. long res_maxsize;
  51. };
  52. static struct callback_op callback_ops[];
  53. static __be32 nfs4_callback_null(struct svc_rqst *rqstp)
  54. {
  55. return htonl(NFS4_OK);
  56. }
  57. /*
  58. * svc_process_common() looks for an XDR encoder to know when
  59. * not to drop a Reply.
  60. */
  61. static bool nfs4_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  62. {
  63. return true;
  64. }
  65. static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len,
  66. const char **str, size_t maxlen)
  67. {
  68. ssize_t err;
  69. err = xdr_stream_decode_opaque_inline(xdr, (void **)str, maxlen);
  70. if (err < 0)
  71. return cpu_to_be32(NFS4ERR_RESOURCE);
  72. *len = err;
  73. return 0;
  74. }
  75. static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
  76. {
  77. __be32 *p;
  78. p = xdr_inline_decode(xdr, 4);
  79. if (unlikely(p == NULL))
  80. return htonl(NFS4ERR_RESOURCE);
  81. fh->size = ntohl(*p);
  82. if (fh->size > NFS4_FHSIZE)
  83. return htonl(NFS4ERR_BADHANDLE);
  84. p = xdr_inline_decode(xdr, fh->size);
  85. if (unlikely(p == NULL))
  86. return htonl(NFS4ERR_RESOURCE);
  87. memcpy(&fh->data[0], p, fh->size);
  88. memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
  89. return 0;
  90. }
  91. static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
  92. {
  93. __be32 *p;
  94. unsigned int attrlen;
  95. p = xdr_inline_decode(xdr, 4);
  96. if (unlikely(p == NULL))
  97. return htonl(NFS4ERR_RESOURCE);
  98. attrlen = ntohl(*p);
  99. p = xdr_inline_decode(xdr, attrlen << 2);
  100. if (unlikely(p == NULL))
  101. return htonl(NFS4ERR_RESOURCE);
  102. if (likely(attrlen > 0))
  103. bitmap[0] = ntohl(*p++);
  104. if (attrlen > 1)
  105. bitmap[1] = ntohl(*p++);
  106. if (attrlen > 2)
  107. bitmap[2] = ntohl(*p);
  108. return 0;
  109. }
  110. static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
  111. {
  112. __be32 *p;
  113. p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
  114. if (unlikely(p == NULL))
  115. return htonl(NFS4ERR_RESOURCE);
  116. memcpy(stateid->data, p, NFS4_STATEID_SIZE);
  117. return 0;
  118. }
  119. static __be32 decode_delegation_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
  120. {
  121. stateid->type = NFS4_DELEGATION_STATEID_TYPE;
  122. return decode_stateid(xdr, stateid);
  123. }
  124. static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
  125. {
  126. __be32 *p;
  127. __be32 status;
  128. status = decode_string(xdr, &hdr->taglen, &hdr->tag, CB_OP_TAGLEN_MAXSZ);
  129. if (unlikely(status != 0))
  130. return status;
  131. p = xdr_inline_decode(xdr, 12);
  132. if (unlikely(p == NULL))
  133. return htonl(NFS4ERR_RESOURCE);
  134. hdr->minorversion = ntohl(*p++);
  135. /* Check for minor version support */
  136. if (hdr->minorversion <= NFS4_MAX_MINOR_VERSION) {
  137. hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 and v4.2 */
  138. } else {
  139. pr_warn_ratelimited("NFS: %s: NFSv4 server callback with "
  140. "illegal minor version %u!\n",
  141. __func__, hdr->minorversion);
  142. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  143. }
  144. hdr->nops = ntohl(*p);
  145. return 0;
  146. }
  147. static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
  148. {
  149. __be32 *p;
  150. p = xdr_inline_decode(xdr, 4);
  151. if (unlikely(p == NULL))
  152. return htonl(NFS4ERR_RESOURCE_HDR);
  153. *op = ntohl(*p);
  154. return 0;
  155. }
  156. static __be32 decode_getattr_args(struct svc_rqst *rqstp,
  157. struct xdr_stream *xdr, void *argp)
  158. {
  159. struct cb_getattrargs *args = argp;
  160. __be32 status;
  161. status = decode_fh(xdr, &args->fh);
  162. if (unlikely(status != 0))
  163. return status;
  164. return decode_bitmap(xdr, args->bitmap);
  165. }
  166. static __be32 decode_recall_args(struct svc_rqst *rqstp,
  167. struct xdr_stream *xdr, void *argp)
  168. {
  169. struct cb_recallargs *args = argp;
  170. __be32 *p;
  171. __be32 status;
  172. status = decode_delegation_stateid(xdr, &args->stateid);
  173. if (unlikely(status != 0))
  174. return status;
  175. p = xdr_inline_decode(xdr, 4);
  176. if (unlikely(p == NULL))
  177. return htonl(NFS4ERR_RESOURCE);
  178. args->truncate = ntohl(*p);
  179. return decode_fh(xdr, &args->fh);
  180. }
  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_objs(*args->devs, n);
  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. if (unlikely(rc_list->rcl_nrefcalls > xdr->buf->len))
  330. goto out;
  331. p = xdr_inline_decode(xdr,
  332. rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
  333. if (unlikely(p == NULL))
  334. goto out;
  335. rc_list->rcl_refcalls = kmalloc_objs(*rc_list->rcl_refcalls,
  336. rc_list->rcl_nrefcalls);
  337. if (unlikely(rc_list->rcl_refcalls == NULL))
  338. goto out;
  339. for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
  340. rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
  341. rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
  342. }
  343. }
  344. status = 0;
  345. out:
  346. return status;
  347. }
  348. static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
  349. struct xdr_stream *xdr,
  350. void *argp)
  351. {
  352. struct cb_sequenceargs *args = argp;
  353. __be32 *p;
  354. int i;
  355. __be32 status;
  356. status = decode_sessionid(xdr, &args->csa_sessionid);
  357. if (status)
  358. return status;
  359. p = xdr_inline_decode(xdr, 5 * sizeof(uint32_t));
  360. if (unlikely(p == NULL))
  361. return htonl(NFS4ERR_RESOURCE);
  362. args->csa_addr = svc_addr(rqstp);
  363. args->csa_sequenceid = ntohl(*p++);
  364. args->csa_slotid = ntohl(*p++);
  365. args->csa_highestslotid = ntohl(*p++);
  366. args->csa_cachethis = ntohl(*p++);
  367. args->csa_nrclists = ntohl(*p++);
  368. args->csa_rclists = NULL;
  369. if (args->csa_nrclists) {
  370. args->csa_rclists = kmalloc_objs(*args->csa_rclists,
  371. args->csa_nrclists);
  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[3];
  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. #ifdef CONFIG_NFS_V4_2
  454. static __be32 decode_write_response(struct xdr_stream *xdr,
  455. struct cb_offloadargs *args)
  456. {
  457. __be32 *p;
  458. /* skip the always zero field */
  459. p = xdr_inline_decode(xdr, 4);
  460. if (unlikely(!p))
  461. goto out;
  462. p++;
  463. /* decode count, stable_how, verifier */
  464. p = xdr_inline_decode(xdr, 8 + 4);
  465. if (unlikely(!p))
  466. goto out;
  467. p = xdr_decode_hyper(p, &args->wr_count);
  468. args->wr_writeverf.committed = be32_to_cpup(p);
  469. p = xdr_inline_decode(xdr, NFS4_VERIFIER_SIZE);
  470. if (likely(p)) {
  471. memcpy(&args->wr_writeverf.verifier.data[0], p,
  472. NFS4_VERIFIER_SIZE);
  473. return 0;
  474. }
  475. out:
  476. return htonl(NFS4ERR_RESOURCE);
  477. }
  478. static __be32 decode_offload_args(struct svc_rqst *rqstp,
  479. struct xdr_stream *xdr,
  480. void *data)
  481. {
  482. struct cb_offloadargs *args = data;
  483. __be32 *p;
  484. __be32 status;
  485. /* decode fh */
  486. status = decode_fh(xdr, &args->coa_fh);
  487. if (unlikely(status != 0))
  488. return status;
  489. /* decode stateid */
  490. status = decode_stateid(xdr, &args->coa_stateid);
  491. if (unlikely(status != 0))
  492. return status;
  493. /* decode status */
  494. p = xdr_inline_decode(xdr, 4);
  495. if (unlikely(!p))
  496. goto out;
  497. args->error = ntohl(*p++);
  498. if (!args->error) {
  499. status = decode_write_response(xdr, args);
  500. if (unlikely(status != 0))
  501. return status;
  502. } else {
  503. p = xdr_inline_decode(xdr, 8);
  504. if (unlikely(!p))
  505. goto out;
  506. p = xdr_decode_hyper(p, &args->wr_count);
  507. }
  508. return 0;
  509. out:
  510. return htonl(NFS4ERR_RESOURCE);
  511. }
  512. #endif /* CONFIG_NFS_V4_2 */
  513. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  514. {
  515. if (unlikely(xdr_stream_encode_opaque(xdr, str, len) < 0))
  516. return cpu_to_be32(NFS4ERR_RESOURCE);
  517. return 0;
  518. }
  519. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, size_t sz)
  520. {
  521. if (xdr_stream_encode_uint32_array(xdr, bitmap, sz) < 0)
  522. return cpu_to_be32(NFS4ERR_RESOURCE);
  523. return 0;
  524. }
  525. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  526. {
  527. __be32 *p;
  528. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  529. return 0;
  530. p = xdr_reserve_space(xdr, 8);
  531. if (unlikely(!p))
  532. return htonl(NFS4ERR_RESOURCE);
  533. p = xdr_encode_hyper(p, change);
  534. return 0;
  535. }
  536. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  537. {
  538. __be32 *p;
  539. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  540. return 0;
  541. p = xdr_reserve_space(xdr, 8);
  542. if (unlikely(!p))
  543. return htonl(NFS4ERR_RESOURCE);
  544. p = xdr_encode_hyper(p, size);
  545. return 0;
  546. }
  547. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec64 *time)
  548. {
  549. __be32 *p;
  550. p = xdr_reserve_space(xdr, 12);
  551. if (unlikely(!p))
  552. return htonl(NFS4ERR_RESOURCE);
  553. p = xdr_encode_hyper(p, time->tv_sec);
  554. *p = htonl(time->tv_nsec);
  555. return 0;
  556. }
  557. static __be32 encode_attr_atime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time)
  558. {
  559. if (!(bitmap[1] & FATTR4_WORD1_TIME_ACCESS))
  560. return 0;
  561. return encode_attr_time(xdr,time);
  562. }
  563. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time)
  564. {
  565. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  566. return 0;
  567. return encode_attr_time(xdr,time);
  568. }
  569. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec64 *time)
  570. {
  571. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  572. return 0;
  573. return encode_attr_time(xdr,time);
  574. }
  575. static __be32 encode_attr_delegatime(struct xdr_stream *xdr,
  576. const uint32_t *bitmap,
  577. const struct timespec64 *time)
  578. {
  579. if (!(bitmap[2] & FATTR4_WORD2_TIME_DELEG_ACCESS))
  580. return 0;
  581. return encode_attr_time(xdr,time);
  582. }
  583. static __be32 encode_attr_delegmtime(struct xdr_stream *xdr,
  584. const uint32_t *bitmap,
  585. const struct timespec64 *time)
  586. {
  587. if (!(bitmap[2] & FATTR4_WORD2_TIME_DELEG_MODIFY))
  588. return 0;
  589. return encode_attr_time(xdr,time);
  590. }
  591. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  592. {
  593. __be32 status;
  594. hdr->status = xdr_reserve_space(xdr, 4);
  595. if (unlikely(hdr->status == NULL))
  596. return htonl(NFS4ERR_RESOURCE);
  597. status = encode_string(xdr, hdr->taglen, hdr->tag);
  598. if (unlikely(status != 0))
  599. return status;
  600. hdr->nops = xdr_reserve_space(xdr, 4);
  601. if (unlikely(hdr->nops == NULL))
  602. return htonl(NFS4ERR_RESOURCE);
  603. return 0;
  604. }
  605. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  606. {
  607. __be32 *p;
  608. p = xdr_reserve_space(xdr, 8);
  609. if (unlikely(p == NULL))
  610. return htonl(NFS4ERR_RESOURCE_HDR);
  611. *p++ = htonl(op);
  612. *p = res;
  613. return 0;
  614. }
  615. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr,
  616. const void *resp)
  617. {
  618. const struct cb_getattrres *res = resp;
  619. __be32 *savep = NULL;
  620. __be32 status = res->status;
  621. if (unlikely(status != 0))
  622. goto out;
  623. status = encode_attr_bitmap(xdr, res->bitmap, ARRAY_SIZE(res->bitmap));
  624. if (unlikely(status != 0))
  625. goto out;
  626. status = cpu_to_be32(NFS4ERR_RESOURCE);
  627. savep = xdr_reserve_space(xdr, sizeof(*savep));
  628. if (unlikely(!savep))
  629. goto out;
  630. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  631. if (unlikely(status != 0))
  632. goto out;
  633. status = encode_attr_size(xdr, res->bitmap, res->size);
  634. if (unlikely(status != 0))
  635. goto out;
  636. status = encode_attr_atime(xdr, res->bitmap, &res->atime);
  637. if (unlikely(status != 0))
  638. goto out;
  639. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  640. if (unlikely(status != 0))
  641. goto out;
  642. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  643. if (unlikely(status != 0))
  644. goto out;
  645. status = encode_attr_delegatime(xdr, res->bitmap, &res->atime);
  646. if (unlikely(status != 0))
  647. goto out;
  648. status = encode_attr_delegmtime(xdr, res->bitmap, &res->mtime);
  649. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  650. out:
  651. return status;
  652. }
  653. static __be32 encode_sessionid(struct xdr_stream *xdr,
  654. const struct nfs4_sessionid *sid)
  655. {
  656. __be32 *p;
  657. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
  658. if (unlikely(p == NULL))
  659. return htonl(NFS4ERR_RESOURCE);
  660. memcpy(p, sid, NFS4_MAX_SESSIONID_LEN);
  661. return 0;
  662. }
  663. static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
  664. struct xdr_stream *xdr,
  665. const void *resp)
  666. {
  667. const struct cb_sequenceres *res = resp;
  668. __be32 *p;
  669. __be32 status = res->csr_status;
  670. if (unlikely(status != 0))
  671. return status;
  672. status = encode_sessionid(xdr, &res->csr_sessionid);
  673. if (status)
  674. return status;
  675. p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
  676. if (unlikely(p == NULL))
  677. return htonl(NFS4ERR_RESOURCE);
  678. *p++ = htonl(res->csr_sequenceid);
  679. *p++ = htonl(res->csr_slotid);
  680. *p++ = htonl(res->csr_highestslotid);
  681. *p++ = htonl(res->csr_target_highestslotid);
  682. return 0;
  683. }
  684. static __be32
  685. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  686. {
  687. if (op_nr == OP_CB_SEQUENCE) {
  688. if (nop != 0)
  689. return htonl(NFS4ERR_SEQUENCE_POS);
  690. } else {
  691. if (nop == 0)
  692. return htonl(NFS4ERR_OP_NOT_IN_SESSION);
  693. }
  694. switch (op_nr) {
  695. case OP_CB_GETATTR:
  696. case OP_CB_RECALL:
  697. case OP_CB_SEQUENCE:
  698. case OP_CB_RECALL_ANY:
  699. case OP_CB_RECALL_SLOT:
  700. case OP_CB_LAYOUTRECALL:
  701. case OP_CB_NOTIFY_DEVICEID:
  702. case OP_CB_NOTIFY_LOCK:
  703. *op = &callback_ops[op_nr];
  704. break;
  705. case OP_CB_NOTIFY:
  706. case OP_CB_PUSH_DELEG:
  707. case OP_CB_RECALLABLE_OBJ_AVAIL:
  708. case OP_CB_WANTS_CANCELLED:
  709. return htonl(NFS4ERR_NOTSUPP);
  710. default:
  711. return htonl(NFS4ERR_OP_ILLEGAL);
  712. }
  713. return htonl(NFS_OK);
  714. }
  715. static void nfs4_callback_free_slot(struct nfs4_session *session,
  716. struct nfs4_slot *slot)
  717. {
  718. struct nfs4_slot_table *tbl = &session->bc_slot_table;
  719. spin_lock(&tbl->slot_tbl_lock);
  720. /*
  721. * Let the state manager know callback processing done.
  722. * A single slot, so highest used slotid is either 0 or -1
  723. */
  724. nfs4_free_slot(tbl, slot);
  725. spin_unlock(&tbl->slot_tbl_lock);
  726. }
  727. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  728. {
  729. if (cps->slot) {
  730. nfs4_callback_free_slot(cps->clp->cl_session, cps->slot);
  731. cps->slot = NULL;
  732. }
  733. }
  734. #ifdef CONFIG_NFS_V4_2
  735. static __be32
  736. preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
  737. {
  738. __be32 status = preprocess_nfs41_op(nop, op_nr, op);
  739. if (status != htonl(NFS4ERR_OP_ILLEGAL))
  740. return status;
  741. if (op_nr == OP_CB_OFFLOAD) {
  742. *op = &callback_ops[op_nr];
  743. return htonl(NFS_OK);
  744. } else
  745. return htonl(NFS4ERR_NOTSUPP);
  746. return htonl(NFS4ERR_OP_ILLEGAL);
  747. }
  748. #else /* CONFIG_NFS_V4_2 */
  749. static __be32
  750. preprocess_nfs42_op(int nop, unsigned int op_nr, struct callback_op **op)
  751. {
  752. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  753. }
  754. #endif /* CONFIG_NFS_V4_2 */
  755. static __be32
  756. preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
  757. {
  758. switch (op_nr) {
  759. case OP_CB_GETATTR:
  760. case OP_CB_RECALL:
  761. *op = &callback_ops[op_nr];
  762. break;
  763. default:
  764. return htonl(NFS4ERR_OP_ILLEGAL);
  765. }
  766. return htonl(NFS_OK);
  767. }
  768. static __be32 process_op(int nop, struct svc_rqst *rqstp,
  769. struct cb_process_state *cps)
  770. {
  771. struct xdr_stream *xdr_out = &rqstp->rq_res_stream;
  772. struct callback_op *op = &callback_ops[0];
  773. unsigned int op_nr;
  774. __be32 status;
  775. long maxlen;
  776. __be32 res;
  777. status = decode_op_hdr(&rqstp->rq_arg_stream, &op_nr);
  778. if (unlikely(status))
  779. return status;
  780. switch (cps->minorversion) {
  781. case 0:
  782. status = preprocess_nfs4_op(op_nr, &op);
  783. break;
  784. case 1:
  785. status = preprocess_nfs41_op(nop, op_nr, &op);
  786. break;
  787. case 2:
  788. status = preprocess_nfs42_op(nop, op_nr, &op);
  789. break;
  790. default:
  791. status = htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  792. }
  793. if (status == htonl(NFS4ERR_OP_ILLEGAL))
  794. op_nr = OP_CB_ILLEGAL;
  795. if (status)
  796. goto encode_hdr;
  797. if (cps->drc_status) {
  798. status = cps->drc_status;
  799. goto encode_hdr;
  800. }
  801. maxlen = xdr_out->end - xdr_out->p;
  802. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  803. status = op->decode_args(rqstp, &rqstp->rq_arg_stream,
  804. rqstp->rq_argp);
  805. if (likely(status == 0))
  806. status = op->process_op(rqstp->rq_argp, rqstp->rq_resp,
  807. cps);
  808. } else
  809. status = htonl(NFS4ERR_RESOURCE);
  810. encode_hdr:
  811. res = encode_op_hdr(xdr_out, op_nr, status);
  812. if (unlikely(res))
  813. return res;
  814. if (op->encode_res != NULL && status == 0)
  815. status = op->encode_res(rqstp, xdr_out, rqstp->rq_resp);
  816. return status;
  817. }
  818. /*
  819. * Decode, process and encode a COMPOUND
  820. */
  821. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp)
  822. {
  823. struct cb_compound_hdr_arg hdr_arg = { 0 };
  824. struct cb_compound_hdr_res hdr_res = { NULL };
  825. struct cb_process_state cps = {
  826. .drc_status = 0,
  827. .clp = NULL,
  828. .net = SVC_NET(rqstp),
  829. };
  830. unsigned int nops = 0;
  831. __be32 status;
  832. status = decode_compound_hdr_arg(&rqstp->rq_arg_stream, &hdr_arg);
  833. if (status == htonl(NFS4ERR_RESOURCE))
  834. return rpc_garbage_args;
  835. if (hdr_arg.minorversion == 0) {
  836. cps.clp = nfs4_find_client_ident(SVC_NET(rqstp), hdr_arg.cb_ident);
  837. if (!cps.clp) {
  838. trace_nfs_cb_no_clp(rqstp->rq_xid, hdr_arg.cb_ident);
  839. goto out_invalidcred;
  840. }
  841. if (!check_gss_callback_principal(cps.clp, rqstp)) {
  842. trace_nfs_cb_badprinc(rqstp->rq_xid, hdr_arg.cb_ident);
  843. nfs_put_client(cps.clp);
  844. goto out_invalidcred;
  845. }
  846. svc_xprt_set_valid(rqstp->rq_xprt);
  847. }
  848. cps.minorversion = hdr_arg.minorversion;
  849. hdr_res.taglen = hdr_arg.taglen;
  850. hdr_res.tag = hdr_arg.tag;
  851. if (encode_compound_hdr_res(&rqstp->rq_res_stream, &hdr_res) != 0) {
  852. if (cps.clp)
  853. nfs_put_client(cps.clp);
  854. return rpc_system_err;
  855. }
  856. while (status == 0 && nops != hdr_arg.nops) {
  857. status = process_op(nops, rqstp, &cps);
  858. nops++;
  859. }
  860. /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
  861. * resource error in cb_compound status without returning op */
  862. if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
  863. status = htonl(NFS4ERR_RESOURCE);
  864. nops--;
  865. }
  866. if (svc_is_backchannel(rqstp) && cps.clp) {
  867. rqstp->bc_to_initval = cps.clp->cl_rpcclient->cl_timeout->to_initval;
  868. rqstp->bc_to_retries = cps.clp->cl_rpcclient->cl_timeout->to_retries;
  869. }
  870. *hdr_res.status = status;
  871. *hdr_res.nops = htonl(nops);
  872. nfs4_cb_free_slot(&cps);
  873. nfs_put_client(cps.clp);
  874. return rpc_success;
  875. out_invalidcred:
  876. pr_warn_ratelimited("NFS: NFSv4 callback contains invalid cred\n");
  877. rqstp->rq_auth_stat = rpc_autherr_badcred;
  878. return rpc_success;
  879. }
  880. static int
  881. nfs_callback_dispatch(struct svc_rqst *rqstp)
  882. {
  883. const struct svc_procedure *procp = rqstp->rq_procinfo;
  884. *rqstp->rq_accept_statp = procp->pc_func(rqstp);
  885. return 1;
  886. }
  887. /*
  888. * Define NFS4 callback COMPOUND ops.
  889. */
  890. static struct callback_op callback_ops[] = {
  891. [0] = {
  892. .res_maxsize = CB_OP_HDR_RES_MAXSZ,
  893. },
  894. [OP_CB_GETATTR] = {
  895. .process_op = nfs4_callback_getattr,
  896. .decode_args = decode_getattr_args,
  897. .encode_res = encode_getattr_res,
  898. .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
  899. },
  900. [OP_CB_RECALL] = {
  901. .process_op = nfs4_callback_recall,
  902. .decode_args = decode_recall_args,
  903. .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
  904. },
  905. [OP_CB_LAYOUTRECALL] = {
  906. .process_op = nfs4_callback_layoutrecall,
  907. .decode_args = decode_layoutrecall_args,
  908. .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
  909. },
  910. [OP_CB_NOTIFY_DEVICEID] = {
  911. .process_op = nfs4_callback_devicenotify,
  912. .decode_args = decode_devicenotify_args,
  913. .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
  914. },
  915. [OP_CB_SEQUENCE] = {
  916. .process_op = nfs4_callback_sequence,
  917. .decode_args = decode_cb_sequence_args,
  918. .encode_res = encode_cb_sequence_res,
  919. .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
  920. },
  921. [OP_CB_RECALL_ANY] = {
  922. .process_op = nfs4_callback_recallany,
  923. .decode_args = decode_recallany_args,
  924. .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
  925. },
  926. [OP_CB_RECALL_SLOT] = {
  927. .process_op = nfs4_callback_recallslot,
  928. .decode_args = decode_recallslot_args,
  929. .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
  930. },
  931. [OP_CB_NOTIFY_LOCK] = {
  932. .process_op = nfs4_callback_notify_lock,
  933. .decode_args = decode_notify_lock_args,
  934. .res_maxsize = CB_OP_NOTIFY_LOCK_RES_MAXSZ,
  935. },
  936. #ifdef CONFIG_NFS_V4_2
  937. [OP_CB_OFFLOAD] = {
  938. .process_op = nfs4_callback_offload,
  939. .decode_args = decode_offload_args,
  940. .res_maxsize = CB_OP_OFFLOAD_RES_MAXSZ,
  941. },
  942. #endif /* CONFIG_NFS_V4_2 */
  943. };
  944. /*
  945. * Define NFS4 callback procedures
  946. */
  947. static const struct svc_procedure nfs4_callback_procedures1[] = {
  948. [CB_NULL] = {
  949. .pc_func = nfs4_callback_null,
  950. .pc_encode = nfs4_encode_void,
  951. .pc_xdrressize = 1,
  952. .pc_name = "NULL",
  953. },
  954. [CB_COMPOUND] = {
  955. .pc_func = nfs4_callback_compound,
  956. .pc_encode = nfs4_encode_void,
  957. .pc_argsize = 256,
  958. .pc_argzero = 256,
  959. .pc_ressize = 256,
  960. .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
  961. .pc_name = "COMPOUND",
  962. }
  963. };
  964. static DEFINE_PER_CPU_ALIGNED(unsigned long,
  965. nfs4_callback_count1[ARRAY_SIZE(nfs4_callback_procedures1)]);
  966. const struct svc_version nfs4_callback_version1 = {
  967. .vs_vers = 1,
  968. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  969. .vs_proc = nfs4_callback_procedures1,
  970. .vs_count = nfs4_callback_count1,
  971. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  972. .vs_dispatch = nfs_callback_dispatch,
  973. .vs_hidden = true,
  974. .vs_need_cong_ctrl = true,
  975. };
  976. static DEFINE_PER_CPU_ALIGNED(unsigned long,
  977. nfs4_callback_count4[ARRAY_SIZE(nfs4_callback_procedures1)]);
  978. const struct svc_version nfs4_callback_version4 = {
  979. .vs_vers = 4,
  980. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  981. .vs_proc = nfs4_callback_procedures1,
  982. .vs_count = nfs4_callback_count4,
  983. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  984. .vs_dispatch = nfs_callback_dispatch,
  985. .vs_hidden = true,
  986. .vs_need_cong_ctrl = true,
  987. };