callback_xdr.c 25 KB

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