callback_xdr.c 26 KB

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