callback_xdr.c 27 KB

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