callback_xdr.c 25 KB

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