callback_xdr.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. args->devs = kmalloc(n * sizeof(*args->devs), GFP_KERNEL);
  271. if (!args->devs) {
  272. status = htonl(NFS4ERR_DELAY);
  273. goto out;
  274. }
  275. /* Decode each dev notification */
  276. for (i = 0; i < n; i++) {
  277. struct cb_devicenotifyitem *dev = &args->devs[i];
  278. p = read_buf(xdr, (4 * sizeof(uint32_t)) + NFS4_DEVICEID4_SIZE);
  279. if (unlikely(p == NULL)) {
  280. status = htonl(NFS4ERR_BADXDR);
  281. goto err;
  282. }
  283. tmp = ntohl(*p++); /* bitmap size */
  284. if (tmp != 1) {
  285. status = htonl(NFS4ERR_INVAL);
  286. goto err;
  287. }
  288. dev->cbd_notify_type = ntohl(*p++);
  289. if (dev->cbd_notify_type != NOTIFY_DEVICEID4_CHANGE &&
  290. dev->cbd_notify_type != NOTIFY_DEVICEID4_DELETE) {
  291. status = htonl(NFS4ERR_INVAL);
  292. goto err;
  293. }
  294. tmp = ntohl(*p++); /* opaque size */
  295. if (((dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE) &&
  296. (tmp != NFS4_DEVICEID4_SIZE + 8)) ||
  297. ((dev->cbd_notify_type == NOTIFY_DEVICEID4_DELETE) &&
  298. (tmp != NFS4_DEVICEID4_SIZE + 4))) {
  299. status = htonl(NFS4ERR_INVAL);
  300. goto err;
  301. }
  302. dev->cbd_layout_type = ntohl(*p++);
  303. memcpy(dev->cbd_dev_id.data, p, NFS4_DEVICEID4_SIZE);
  304. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  305. if (dev->cbd_layout_type == NOTIFY_DEVICEID4_CHANGE) {
  306. p = read_buf(xdr, sizeof(uint32_t));
  307. if (unlikely(p == NULL)) {
  308. status = htonl(NFS4ERR_BADXDR);
  309. goto err;
  310. }
  311. dev->cbd_immediate = ntohl(*p++);
  312. } else {
  313. dev->cbd_immediate = 0;
  314. }
  315. args->ndevs++;
  316. dprintk("%s: type %d layout 0x%x immediate %d\n",
  317. __func__, dev->cbd_notify_type, dev->cbd_layout_type,
  318. dev->cbd_immediate);
  319. }
  320. out:
  321. dprintk("%s: status %d ndevs %d\n",
  322. __func__, ntohl(status), args->ndevs);
  323. return status;
  324. err:
  325. kfree(args->devs);
  326. goto out;
  327. }
  328. static __be32 decode_sessionid(struct xdr_stream *xdr,
  329. struct nfs4_sessionid *sid)
  330. {
  331. __be32 *p;
  332. int len = NFS4_MAX_SESSIONID_LEN;
  333. p = read_buf(xdr, len);
  334. if (unlikely(p == NULL))
  335. return htonl(NFS4ERR_RESOURCE);
  336. memcpy(sid->data, p, len);
  337. return 0;
  338. }
  339. static __be32 decode_rc_list(struct xdr_stream *xdr,
  340. struct referring_call_list *rc_list)
  341. {
  342. __be32 *p;
  343. int i;
  344. __be32 status;
  345. status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
  346. if (status)
  347. goto out;
  348. status = htonl(NFS4ERR_RESOURCE);
  349. p = read_buf(xdr, sizeof(uint32_t));
  350. if (unlikely(p == NULL))
  351. goto out;
  352. rc_list->rcl_nrefcalls = ntohl(*p++);
  353. if (rc_list->rcl_nrefcalls) {
  354. p = read_buf(xdr,
  355. rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
  356. if (unlikely(p == NULL))
  357. goto out;
  358. rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
  359. sizeof(*rc_list->rcl_refcalls),
  360. GFP_KERNEL);
  361. if (unlikely(rc_list->rcl_refcalls == NULL))
  362. goto out;
  363. for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
  364. rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
  365. rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
  366. }
  367. }
  368. status = 0;
  369. out:
  370. return status;
  371. }
  372. static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
  373. struct xdr_stream *xdr,
  374. struct cb_sequenceargs *args)
  375. {
  376. __be32 *p;
  377. int i;
  378. __be32 status;
  379. status = decode_sessionid(xdr, &args->csa_sessionid);
  380. if (status)
  381. goto out;
  382. status = htonl(NFS4ERR_RESOURCE);
  383. p = read_buf(xdr, 5 * sizeof(uint32_t));
  384. if (unlikely(p == NULL))
  385. goto out;
  386. args->csa_addr = svc_addr(rqstp);
  387. args->csa_sequenceid = ntohl(*p++);
  388. args->csa_slotid = ntohl(*p++);
  389. args->csa_highestslotid = ntohl(*p++);
  390. args->csa_cachethis = ntohl(*p++);
  391. args->csa_nrclists = ntohl(*p++);
  392. args->csa_rclists = NULL;
  393. if (args->csa_nrclists) {
  394. args->csa_rclists = kmalloc(args->csa_nrclists *
  395. sizeof(*args->csa_rclists),
  396. GFP_KERNEL);
  397. if (unlikely(args->csa_rclists == NULL))
  398. goto out;
  399. for (i = 0; i < args->csa_nrclists; i++) {
  400. status = decode_rc_list(xdr, &args->csa_rclists[i]);
  401. if (status)
  402. goto out_free;
  403. }
  404. }
  405. status = 0;
  406. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
  407. "highestslotid %u cachethis %d nrclists %u\n",
  408. __func__,
  409. ((u32 *)&args->csa_sessionid)[0],
  410. ((u32 *)&args->csa_sessionid)[1],
  411. ((u32 *)&args->csa_sessionid)[2],
  412. ((u32 *)&args->csa_sessionid)[3],
  413. args->csa_sequenceid, args->csa_slotid,
  414. args->csa_highestslotid, args->csa_cachethis,
  415. args->csa_nrclists);
  416. out:
  417. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  418. return status;
  419. out_free:
  420. for (i = 0; i < args->csa_nrclists; i++)
  421. kfree(args->csa_rclists[i].rcl_refcalls);
  422. kfree(args->csa_rclists);
  423. goto out;
  424. }
  425. static __be32 decode_recallany_args(struct svc_rqst *rqstp,
  426. struct xdr_stream *xdr,
  427. struct cb_recallanyargs *args)
  428. {
  429. uint32_t bitmap[2];
  430. __be32 *p, status;
  431. args->craa_addr = svc_addr(rqstp);
  432. p = read_buf(xdr, 4);
  433. if (unlikely(p == NULL))
  434. return htonl(NFS4ERR_BADXDR);
  435. args->craa_objs_to_keep = ntohl(*p++);
  436. status = decode_bitmap(xdr, bitmap);
  437. if (unlikely(status))
  438. return status;
  439. args->craa_type_mask = bitmap[0];
  440. return 0;
  441. }
  442. static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
  443. struct xdr_stream *xdr,
  444. struct cb_recallslotargs *args)
  445. {
  446. __be32 *p;
  447. args->crsa_addr = svc_addr(rqstp);
  448. p = read_buf(xdr, 4);
  449. if (unlikely(p == NULL))
  450. return htonl(NFS4ERR_BADXDR);
  451. args->crsa_target_max_slots = ntohl(*p++);
  452. return 0;
  453. }
  454. #endif /* CONFIG_NFS_V4_1 */
  455. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  456. {
  457. __be32 *p;
  458. p = xdr_reserve_space(xdr, 4 + len);
  459. if (unlikely(p == NULL))
  460. return htonl(NFS4ERR_RESOURCE);
  461. xdr_encode_opaque(p, str, len);
  462. return 0;
  463. }
  464. #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
  465. #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
  466. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
  467. {
  468. __be32 bm[2];
  469. __be32 *p;
  470. bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
  471. bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
  472. if (bm[1] != 0) {
  473. p = xdr_reserve_space(xdr, 16);
  474. if (unlikely(p == NULL))
  475. return htonl(NFS4ERR_RESOURCE);
  476. *p++ = htonl(2);
  477. *p++ = bm[0];
  478. *p++ = bm[1];
  479. } else if (bm[0] != 0) {
  480. p = xdr_reserve_space(xdr, 12);
  481. if (unlikely(p == NULL))
  482. return htonl(NFS4ERR_RESOURCE);
  483. *p++ = htonl(1);
  484. *p++ = bm[0];
  485. } else {
  486. p = xdr_reserve_space(xdr, 8);
  487. if (unlikely(p == NULL))
  488. return htonl(NFS4ERR_RESOURCE);
  489. *p++ = htonl(0);
  490. }
  491. *savep = p;
  492. return 0;
  493. }
  494. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  495. {
  496. __be32 *p;
  497. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  498. return 0;
  499. p = xdr_reserve_space(xdr, 8);
  500. if (unlikely(!p))
  501. return htonl(NFS4ERR_RESOURCE);
  502. p = xdr_encode_hyper(p, change);
  503. return 0;
  504. }
  505. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  506. {
  507. __be32 *p;
  508. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  509. return 0;
  510. p = xdr_reserve_space(xdr, 8);
  511. if (unlikely(!p))
  512. return htonl(NFS4ERR_RESOURCE);
  513. p = xdr_encode_hyper(p, size);
  514. return 0;
  515. }
  516. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
  517. {
  518. __be32 *p;
  519. p = xdr_reserve_space(xdr, 12);
  520. if (unlikely(!p))
  521. return htonl(NFS4ERR_RESOURCE);
  522. p = xdr_encode_hyper(p, time->tv_sec);
  523. *p = htonl(time->tv_nsec);
  524. return 0;
  525. }
  526. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  527. {
  528. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  529. return 0;
  530. return encode_attr_time(xdr,time);
  531. }
  532. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  533. {
  534. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  535. return 0;
  536. return encode_attr_time(xdr,time);
  537. }
  538. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  539. {
  540. __be32 status;
  541. hdr->status = xdr_reserve_space(xdr, 4);
  542. if (unlikely(hdr->status == NULL))
  543. return htonl(NFS4ERR_RESOURCE);
  544. status = encode_string(xdr, hdr->taglen, hdr->tag);
  545. if (unlikely(status != 0))
  546. return status;
  547. hdr->nops = xdr_reserve_space(xdr, 4);
  548. if (unlikely(hdr->nops == NULL))
  549. return htonl(NFS4ERR_RESOURCE);
  550. return 0;
  551. }
  552. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  553. {
  554. __be32 *p;
  555. p = xdr_reserve_space(xdr, 8);
  556. if (unlikely(p == NULL))
  557. return htonl(NFS4ERR_RESOURCE_HDR);
  558. *p++ = htonl(op);
  559. *p = res;
  560. return 0;
  561. }
  562. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
  563. {
  564. __be32 *savep = NULL;
  565. __be32 status = res->status;
  566. if (unlikely(status != 0))
  567. goto out;
  568. status = encode_attr_bitmap(xdr, res->bitmap, &savep);
  569. if (unlikely(status != 0))
  570. goto out;
  571. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  572. if (unlikely(status != 0))
  573. goto out;
  574. status = encode_attr_size(xdr, res->bitmap, res->size);
  575. if (unlikely(status != 0))
  576. goto out;
  577. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  578. if (unlikely(status != 0))
  579. goto out;
  580. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  581. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  582. out:
  583. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  584. return status;
  585. }
  586. #if defined(CONFIG_NFS_V4_1)
  587. static __be32 encode_sessionid(struct xdr_stream *xdr,
  588. const struct nfs4_sessionid *sid)
  589. {
  590. __be32 *p;
  591. int len = NFS4_MAX_SESSIONID_LEN;
  592. p = xdr_reserve_space(xdr, len);
  593. if (unlikely(p == NULL))
  594. return htonl(NFS4ERR_RESOURCE);
  595. memcpy(p, sid, len);
  596. return 0;
  597. }
  598. static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
  599. struct xdr_stream *xdr,
  600. const struct cb_sequenceres *res)
  601. {
  602. __be32 *p;
  603. unsigned status = res->csr_status;
  604. if (unlikely(status != 0))
  605. goto out;
  606. encode_sessionid(xdr, &res->csr_sessionid);
  607. p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
  608. if (unlikely(p == NULL))
  609. return htonl(NFS4ERR_RESOURCE);
  610. *p++ = htonl(res->csr_sequenceid);
  611. *p++ = htonl(res->csr_slotid);
  612. *p++ = htonl(res->csr_highestslotid);
  613. *p++ = htonl(res->csr_target_highestslotid);
  614. out:
  615. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  616. return status;
  617. }
  618. static __be32
  619. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  620. {
  621. if (op_nr == OP_CB_SEQUENCE) {
  622. if (nop != 0)
  623. return htonl(NFS4ERR_SEQUENCE_POS);
  624. } else {
  625. if (nop == 0)
  626. return htonl(NFS4ERR_OP_NOT_IN_SESSION);
  627. }
  628. switch (op_nr) {
  629. case OP_CB_GETATTR:
  630. case OP_CB_RECALL:
  631. case OP_CB_SEQUENCE:
  632. case OP_CB_RECALL_ANY:
  633. case OP_CB_RECALL_SLOT:
  634. case OP_CB_LAYOUTRECALL:
  635. case OP_CB_NOTIFY_DEVICEID:
  636. *op = &callback_ops[op_nr];
  637. break;
  638. case OP_CB_NOTIFY:
  639. case OP_CB_PUSH_DELEG:
  640. case OP_CB_RECALLABLE_OBJ_AVAIL:
  641. case OP_CB_WANTS_CANCELLED:
  642. case OP_CB_NOTIFY_LOCK:
  643. return htonl(NFS4ERR_NOTSUPP);
  644. default:
  645. return htonl(NFS4ERR_OP_ILLEGAL);
  646. }
  647. return htonl(NFS_OK);
  648. }
  649. static void nfs4_callback_free_slot(struct nfs4_session *session)
  650. {
  651. struct nfs4_slot_table *tbl = &session->bc_slot_table;
  652. spin_lock(&tbl->slot_tbl_lock);
  653. /*
  654. * Let the state manager know callback processing done.
  655. * A single slot, so highest used slotid is either 0 or -1
  656. */
  657. tbl->highest_used_slotid = -1;
  658. nfs4_check_drain_bc_complete(session);
  659. spin_unlock(&tbl->slot_tbl_lock);
  660. }
  661. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  662. {
  663. if (cps->slotid != -1)
  664. nfs4_callback_free_slot(cps->clp->cl_session);
  665. }
  666. #else /* CONFIG_NFS_V4_1 */
  667. static __be32
  668. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  669. {
  670. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  671. }
  672. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  673. {
  674. }
  675. #endif /* CONFIG_NFS_V4_1 */
  676. static __be32
  677. preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
  678. {
  679. switch (op_nr) {
  680. case OP_CB_GETATTR:
  681. case OP_CB_RECALL:
  682. *op = &callback_ops[op_nr];
  683. break;
  684. default:
  685. return htonl(NFS4ERR_OP_ILLEGAL);
  686. }
  687. return htonl(NFS_OK);
  688. }
  689. static __be32 process_op(uint32_t minorversion, int nop,
  690. struct svc_rqst *rqstp,
  691. struct xdr_stream *xdr_in, void *argp,
  692. struct xdr_stream *xdr_out, void *resp,
  693. struct cb_process_state *cps)
  694. {
  695. struct callback_op *op = &callback_ops[0];
  696. unsigned int op_nr;
  697. __be32 status;
  698. long maxlen;
  699. __be32 res;
  700. dprintk("%s: start\n", __func__);
  701. status = decode_op_hdr(xdr_in, &op_nr);
  702. if (unlikely(status))
  703. return status;
  704. dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
  705. __func__, minorversion, nop, op_nr);
  706. status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
  707. preprocess_nfs4_op(op_nr, &op);
  708. if (status == htonl(NFS4ERR_OP_ILLEGAL))
  709. op_nr = OP_CB_ILLEGAL;
  710. if (status)
  711. goto encode_hdr;
  712. if (cps->drc_status) {
  713. status = cps->drc_status;
  714. goto encode_hdr;
  715. }
  716. maxlen = xdr_out->end - xdr_out->p;
  717. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  718. status = op->decode_args(rqstp, xdr_in, argp);
  719. if (likely(status == 0))
  720. status = op->process_op(argp, resp, cps);
  721. } else
  722. status = htonl(NFS4ERR_RESOURCE);
  723. encode_hdr:
  724. res = encode_op_hdr(xdr_out, op_nr, status);
  725. if (unlikely(res))
  726. return res;
  727. if (op->encode_res != NULL && status == 0)
  728. status = op->encode_res(rqstp, xdr_out, resp);
  729. dprintk("%s: done, status = %d\n", __func__, ntohl(status));
  730. return status;
  731. }
  732. /*
  733. * Decode, process and encode a COMPOUND
  734. */
  735. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
  736. {
  737. struct cb_compound_hdr_arg hdr_arg = { 0 };
  738. struct cb_compound_hdr_res hdr_res = { NULL };
  739. struct xdr_stream xdr_in, xdr_out;
  740. __be32 *p, status;
  741. struct cb_process_state cps = {
  742. .drc_status = 0,
  743. .clp = NULL,
  744. .slotid = -1,
  745. };
  746. unsigned int nops = 0;
  747. dprintk("%s: start\n", __func__);
  748. xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
  749. p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
  750. xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
  751. status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
  752. if (status == __constant_htonl(NFS4ERR_RESOURCE))
  753. return rpc_garbage_args;
  754. if (hdr_arg.minorversion == 0) {
  755. cps.clp = nfs4_find_client_ident(hdr_arg.cb_ident);
  756. if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
  757. return rpc_drop_reply;
  758. }
  759. hdr_res.taglen = hdr_arg.taglen;
  760. hdr_res.tag = hdr_arg.tag;
  761. if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
  762. return rpc_system_err;
  763. while (status == 0 && nops != hdr_arg.nops) {
  764. status = process_op(hdr_arg.minorversion, nops, rqstp,
  765. &xdr_in, argp, &xdr_out, resp, &cps);
  766. nops++;
  767. }
  768. /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
  769. * resource error in cb_compound status without returning op */
  770. if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
  771. status = htonl(NFS4ERR_RESOURCE);
  772. nops--;
  773. }
  774. *hdr_res.status = status;
  775. *hdr_res.nops = htonl(nops);
  776. nfs4_cb_free_slot(&cps);
  777. nfs_put_client(cps.clp);
  778. dprintk("%s: done, status = %u\n", __func__, ntohl(status));
  779. return rpc_success;
  780. }
  781. /*
  782. * Define NFS4 callback COMPOUND ops.
  783. */
  784. static struct callback_op callback_ops[] = {
  785. [0] = {
  786. .res_maxsize = CB_OP_HDR_RES_MAXSZ,
  787. },
  788. [OP_CB_GETATTR] = {
  789. .process_op = (callback_process_op_t)nfs4_callback_getattr,
  790. .decode_args = (callback_decode_arg_t)decode_getattr_args,
  791. .encode_res = (callback_encode_res_t)encode_getattr_res,
  792. .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
  793. },
  794. [OP_CB_RECALL] = {
  795. .process_op = (callback_process_op_t)nfs4_callback_recall,
  796. .decode_args = (callback_decode_arg_t)decode_recall_args,
  797. .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
  798. },
  799. #if defined(CONFIG_NFS_V4_1)
  800. [OP_CB_LAYOUTRECALL] = {
  801. .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
  802. .decode_args =
  803. (callback_decode_arg_t)decode_layoutrecall_args,
  804. .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
  805. },
  806. [OP_CB_NOTIFY_DEVICEID] = {
  807. .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
  808. .decode_args =
  809. (callback_decode_arg_t)decode_devicenotify_args,
  810. .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
  811. },
  812. [OP_CB_SEQUENCE] = {
  813. .process_op = (callback_process_op_t)nfs4_callback_sequence,
  814. .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
  815. .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
  816. .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
  817. },
  818. [OP_CB_RECALL_ANY] = {
  819. .process_op = (callback_process_op_t)nfs4_callback_recallany,
  820. .decode_args = (callback_decode_arg_t)decode_recallany_args,
  821. .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
  822. },
  823. [OP_CB_RECALL_SLOT] = {
  824. .process_op = (callback_process_op_t)nfs4_callback_recallslot,
  825. .decode_args = (callback_decode_arg_t)decode_recallslot_args,
  826. .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
  827. },
  828. #endif /* CONFIG_NFS_V4_1 */
  829. };
  830. /*
  831. * Define NFS4 callback procedures
  832. */
  833. static struct svc_procedure nfs4_callback_procedures1[] = {
  834. [CB_NULL] = {
  835. .pc_func = nfs4_callback_null,
  836. .pc_decode = (kxdrproc_t)nfs4_decode_void,
  837. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  838. .pc_xdrressize = 1,
  839. },
  840. [CB_COMPOUND] = {
  841. .pc_func = nfs4_callback_compound,
  842. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  843. .pc_argsize = 256,
  844. .pc_ressize = 256,
  845. .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
  846. }
  847. };
  848. struct svc_version nfs4_callback_version1 = {
  849. .vs_vers = 1,
  850. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  851. .vs_proc = nfs4_callback_procedures1,
  852. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  853. .vs_dispatch = NULL,
  854. .vs_hidden = 1,
  855. };
  856. struct svc_version nfs4_callback_version4 = {
  857. .vs_vers = 4,
  858. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  859. .vs_proc = nfs4_callback_procedures1,
  860. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  861. .vs_dispatch = NULL,
  862. .vs_hidden = 1,
  863. };