callback_xdr.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. __be32 *p;
  430. args->craa_addr = svc_addr(rqstp);
  431. p = read_buf(xdr, 4);
  432. if (unlikely(p == NULL))
  433. return htonl(NFS4ERR_BADXDR);
  434. args->craa_objs_to_keep = ntohl(*p++);
  435. p = read_buf(xdr, 4);
  436. if (unlikely(p == NULL))
  437. return htonl(NFS4ERR_BADXDR);
  438. args->craa_type_mask = ntohl(*p);
  439. return 0;
  440. }
  441. static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
  442. struct xdr_stream *xdr,
  443. struct cb_recallslotargs *args)
  444. {
  445. __be32 *p;
  446. args->crsa_addr = svc_addr(rqstp);
  447. p = read_buf(xdr, 4);
  448. if (unlikely(p == NULL))
  449. return htonl(NFS4ERR_BADXDR);
  450. args->crsa_target_max_slots = ntohl(*p++);
  451. return 0;
  452. }
  453. #endif /* CONFIG_NFS_V4_1 */
  454. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  455. {
  456. __be32 *p;
  457. p = xdr_reserve_space(xdr, 4 + len);
  458. if (unlikely(p == NULL))
  459. return htonl(NFS4ERR_RESOURCE);
  460. xdr_encode_opaque(p, str, len);
  461. return 0;
  462. }
  463. #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
  464. #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
  465. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
  466. {
  467. __be32 bm[2];
  468. __be32 *p;
  469. bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
  470. bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
  471. if (bm[1] != 0) {
  472. p = xdr_reserve_space(xdr, 16);
  473. if (unlikely(p == NULL))
  474. return htonl(NFS4ERR_RESOURCE);
  475. *p++ = htonl(2);
  476. *p++ = bm[0];
  477. *p++ = bm[1];
  478. } else if (bm[0] != 0) {
  479. p = xdr_reserve_space(xdr, 12);
  480. if (unlikely(p == NULL))
  481. return htonl(NFS4ERR_RESOURCE);
  482. *p++ = htonl(1);
  483. *p++ = bm[0];
  484. } else {
  485. p = xdr_reserve_space(xdr, 8);
  486. if (unlikely(p == NULL))
  487. return htonl(NFS4ERR_RESOURCE);
  488. *p++ = htonl(0);
  489. }
  490. *savep = p;
  491. return 0;
  492. }
  493. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  494. {
  495. __be32 *p;
  496. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  497. return 0;
  498. p = xdr_reserve_space(xdr, 8);
  499. if (unlikely(!p))
  500. return htonl(NFS4ERR_RESOURCE);
  501. p = xdr_encode_hyper(p, change);
  502. return 0;
  503. }
  504. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  505. {
  506. __be32 *p;
  507. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  508. return 0;
  509. p = xdr_reserve_space(xdr, 8);
  510. if (unlikely(!p))
  511. return htonl(NFS4ERR_RESOURCE);
  512. p = xdr_encode_hyper(p, size);
  513. return 0;
  514. }
  515. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
  516. {
  517. __be32 *p;
  518. p = xdr_reserve_space(xdr, 12);
  519. if (unlikely(!p))
  520. return htonl(NFS4ERR_RESOURCE);
  521. p = xdr_encode_hyper(p, time->tv_sec);
  522. *p = htonl(time->tv_nsec);
  523. return 0;
  524. }
  525. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  526. {
  527. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  528. return 0;
  529. return encode_attr_time(xdr,time);
  530. }
  531. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  532. {
  533. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  534. return 0;
  535. return encode_attr_time(xdr,time);
  536. }
  537. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  538. {
  539. __be32 status;
  540. hdr->status = xdr_reserve_space(xdr, 4);
  541. if (unlikely(hdr->status == NULL))
  542. return htonl(NFS4ERR_RESOURCE);
  543. status = encode_string(xdr, hdr->taglen, hdr->tag);
  544. if (unlikely(status != 0))
  545. return status;
  546. hdr->nops = xdr_reserve_space(xdr, 4);
  547. if (unlikely(hdr->nops == NULL))
  548. return htonl(NFS4ERR_RESOURCE);
  549. return 0;
  550. }
  551. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  552. {
  553. __be32 *p;
  554. p = xdr_reserve_space(xdr, 8);
  555. if (unlikely(p == NULL))
  556. return htonl(NFS4ERR_RESOURCE_HDR);
  557. *p++ = htonl(op);
  558. *p = res;
  559. return 0;
  560. }
  561. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
  562. {
  563. __be32 *savep = NULL;
  564. __be32 status = res->status;
  565. if (unlikely(status != 0))
  566. goto out;
  567. status = encode_attr_bitmap(xdr, res->bitmap, &savep);
  568. if (unlikely(status != 0))
  569. goto out;
  570. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  571. if (unlikely(status != 0))
  572. goto out;
  573. status = encode_attr_size(xdr, res->bitmap, res->size);
  574. if (unlikely(status != 0))
  575. goto out;
  576. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  577. if (unlikely(status != 0))
  578. goto out;
  579. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  580. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  581. out:
  582. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  583. return status;
  584. }
  585. #if defined(CONFIG_NFS_V4_1)
  586. static __be32 encode_sessionid(struct xdr_stream *xdr,
  587. const struct nfs4_sessionid *sid)
  588. {
  589. __be32 *p;
  590. int len = NFS4_MAX_SESSIONID_LEN;
  591. p = xdr_reserve_space(xdr, len);
  592. if (unlikely(p == NULL))
  593. return htonl(NFS4ERR_RESOURCE);
  594. memcpy(p, sid, len);
  595. return 0;
  596. }
  597. static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
  598. struct xdr_stream *xdr,
  599. const struct cb_sequenceres *res)
  600. {
  601. __be32 *p;
  602. unsigned status = res->csr_status;
  603. if (unlikely(status != 0))
  604. goto out;
  605. encode_sessionid(xdr, &res->csr_sessionid);
  606. p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
  607. if (unlikely(p == NULL))
  608. return htonl(NFS4ERR_RESOURCE);
  609. *p++ = htonl(res->csr_sequenceid);
  610. *p++ = htonl(res->csr_slotid);
  611. *p++ = htonl(res->csr_highestslotid);
  612. *p++ = htonl(res->csr_target_highestslotid);
  613. out:
  614. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  615. return status;
  616. }
  617. static __be32
  618. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  619. {
  620. if (op_nr == OP_CB_SEQUENCE) {
  621. if (nop != 0)
  622. return htonl(NFS4ERR_SEQUENCE_POS);
  623. } else {
  624. if (nop == 0)
  625. return htonl(NFS4ERR_OP_NOT_IN_SESSION);
  626. }
  627. switch (op_nr) {
  628. case OP_CB_GETATTR:
  629. case OP_CB_RECALL:
  630. case OP_CB_SEQUENCE:
  631. case OP_CB_RECALL_ANY:
  632. case OP_CB_RECALL_SLOT:
  633. case OP_CB_LAYOUTRECALL:
  634. case OP_CB_NOTIFY_DEVICEID:
  635. *op = &callback_ops[op_nr];
  636. break;
  637. case OP_CB_NOTIFY:
  638. case OP_CB_PUSH_DELEG:
  639. case OP_CB_RECALLABLE_OBJ_AVAIL:
  640. case OP_CB_WANTS_CANCELLED:
  641. case OP_CB_NOTIFY_LOCK:
  642. return htonl(NFS4ERR_NOTSUPP);
  643. default:
  644. return htonl(NFS4ERR_OP_ILLEGAL);
  645. }
  646. return htonl(NFS_OK);
  647. }
  648. static void nfs4_callback_free_slot(struct nfs4_session *session)
  649. {
  650. struct nfs4_slot_table *tbl = &session->bc_slot_table;
  651. spin_lock(&tbl->slot_tbl_lock);
  652. /*
  653. * Let the state manager know callback processing done.
  654. * A single slot, so highest used slotid is either 0 or -1
  655. */
  656. tbl->highest_used_slotid = -1;
  657. nfs4_check_drain_bc_complete(session);
  658. spin_unlock(&tbl->slot_tbl_lock);
  659. }
  660. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  661. {
  662. if (cps->slotid != -1)
  663. nfs4_callback_free_slot(cps->clp->cl_session);
  664. }
  665. #else /* CONFIG_NFS_V4_1 */
  666. static __be32
  667. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  668. {
  669. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  670. }
  671. static void nfs4_cb_free_slot(struct cb_process_state *cps)
  672. {
  673. }
  674. #endif /* CONFIG_NFS_V4_1 */
  675. static __be32
  676. preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
  677. {
  678. switch (op_nr) {
  679. case OP_CB_GETATTR:
  680. case OP_CB_RECALL:
  681. *op = &callback_ops[op_nr];
  682. break;
  683. default:
  684. return htonl(NFS4ERR_OP_ILLEGAL);
  685. }
  686. return htonl(NFS_OK);
  687. }
  688. static __be32 process_op(uint32_t minorversion, int nop,
  689. struct svc_rqst *rqstp,
  690. struct xdr_stream *xdr_in, void *argp,
  691. struct xdr_stream *xdr_out, void *resp,
  692. struct cb_process_state *cps)
  693. {
  694. struct callback_op *op = &callback_ops[0];
  695. unsigned int op_nr;
  696. __be32 status;
  697. long maxlen;
  698. __be32 res;
  699. dprintk("%s: start\n", __func__);
  700. status = decode_op_hdr(xdr_in, &op_nr);
  701. if (unlikely(status))
  702. return status;
  703. dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
  704. __func__, minorversion, nop, op_nr);
  705. status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
  706. preprocess_nfs4_op(op_nr, &op);
  707. if (status == htonl(NFS4ERR_OP_ILLEGAL))
  708. op_nr = OP_CB_ILLEGAL;
  709. if (status)
  710. goto encode_hdr;
  711. if (cps->drc_status) {
  712. status = cps->drc_status;
  713. goto encode_hdr;
  714. }
  715. maxlen = xdr_out->end - xdr_out->p;
  716. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  717. status = op->decode_args(rqstp, xdr_in, argp);
  718. if (likely(status == 0))
  719. status = op->process_op(argp, resp, cps);
  720. } else
  721. status = htonl(NFS4ERR_RESOURCE);
  722. encode_hdr:
  723. res = encode_op_hdr(xdr_out, op_nr, status);
  724. if (unlikely(res))
  725. return res;
  726. if (op->encode_res != NULL && status == 0)
  727. status = op->encode_res(rqstp, xdr_out, resp);
  728. dprintk("%s: done, status = %d\n", __func__, ntohl(status));
  729. return status;
  730. }
  731. /*
  732. * Decode, process and encode a COMPOUND
  733. */
  734. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
  735. {
  736. struct cb_compound_hdr_arg hdr_arg = { 0 };
  737. struct cb_compound_hdr_res hdr_res = { NULL };
  738. struct xdr_stream xdr_in, xdr_out;
  739. __be32 *p, status;
  740. struct cb_process_state cps = {
  741. .drc_status = 0,
  742. .clp = NULL,
  743. .slotid = -1,
  744. };
  745. unsigned int nops = 0;
  746. dprintk("%s: start\n", __func__);
  747. xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
  748. p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
  749. xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
  750. status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
  751. if (status == __constant_htonl(NFS4ERR_RESOURCE))
  752. return rpc_garbage_args;
  753. if (hdr_arg.minorversion == 0) {
  754. cps.clp = nfs4_find_client_ident(hdr_arg.cb_ident);
  755. if (!cps.clp || !check_gss_callback_principal(cps.clp, rqstp))
  756. return rpc_drop_reply;
  757. }
  758. hdr_res.taglen = hdr_arg.taglen;
  759. hdr_res.tag = hdr_arg.tag;
  760. if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
  761. return rpc_system_err;
  762. while (status == 0 && nops != hdr_arg.nops) {
  763. status = process_op(hdr_arg.minorversion, nops, rqstp,
  764. &xdr_in, argp, &xdr_out, resp, &cps);
  765. nops++;
  766. }
  767. /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
  768. * resource error in cb_compound status without returning op */
  769. if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
  770. status = htonl(NFS4ERR_RESOURCE);
  771. nops--;
  772. }
  773. *hdr_res.status = status;
  774. *hdr_res.nops = htonl(nops);
  775. nfs4_cb_free_slot(&cps);
  776. nfs_put_client(cps.clp);
  777. dprintk("%s: done, status = %u\n", __func__, ntohl(status));
  778. return rpc_success;
  779. }
  780. /*
  781. * Define NFS4 callback COMPOUND ops.
  782. */
  783. static struct callback_op callback_ops[] = {
  784. [0] = {
  785. .res_maxsize = CB_OP_HDR_RES_MAXSZ,
  786. },
  787. [OP_CB_GETATTR] = {
  788. .process_op = (callback_process_op_t)nfs4_callback_getattr,
  789. .decode_args = (callback_decode_arg_t)decode_getattr_args,
  790. .encode_res = (callback_encode_res_t)encode_getattr_res,
  791. .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
  792. },
  793. [OP_CB_RECALL] = {
  794. .process_op = (callback_process_op_t)nfs4_callback_recall,
  795. .decode_args = (callback_decode_arg_t)decode_recall_args,
  796. .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
  797. },
  798. #if defined(CONFIG_NFS_V4_1)
  799. [OP_CB_LAYOUTRECALL] = {
  800. .process_op = (callback_process_op_t)nfs4_callback_layoutrecall,
  801. .decode_args =
  802. (callback_decode_arg_t)decode_layoutrecall_args,
  803. .res_maxsize = CB_OP_LAYOUTRECALL_RES_MAXSZ,
  804. },
  805. [OP_CB_NOTIFY_DEVICEID] = {
  806. .process_op = (callback_process_op_t)nfs4_callback_devicenotify,
  807. .decode_args =
  808. (callback_decode_arg_t)decode_devicenotify_args,
  809. .res_maxsize = CB_OP_DEVICENOTIFY_RES_MAXSZ,
  810. },
  811. [OP_CB_SEQUENCE] = {
  812. .process_op = (callback_process_op_t)nfs4_callback_sequence,
  813. .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
  814. .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
  815. .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
  816. },
  817. [OP_CB_RECALL_ANY] = {
  818. .process_op = (callback_process_op_t)nfs4_callback_recallany,
  819. .decode_args = (callback_decode_arg_t)decode_recallany_args,
  820. .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
  821. },
  822. [OP_CB_RECALL_SLOT] = {
  823. .process_op = (callback_process_op_t)nfs4_callback_recallslot,
  824. .decode_args = (callback_decode_arg_t)decode_recallslot_args,
  825. .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
  826. },
  827. #endif /* CONFIG_NFS_V4_1 */
  828. };
  829. /*
  830. * Define NFS4 callback procedures
  831. */
  832. static struct svc_procedure nfs4_callback_procedures1[] = {
  833. [CB_NULL] = {
  834. .pc_func = nfs4_callback_null,
  835. .pc_decode = (kxdrproc_t)nfs4_decode_void,
  836. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  837. .pc_xdrressize = 1,
  838. },
  839. [CB_COMPOUND] = {
  840. .pc_func = nfs4_callback_compound,
  841. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  842. .pc_argsize = 256,
  843. .pc_ressize = 256,
  844. .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
  845. }
  846. };
  847. struct svc_version nfs4_callback_version1 = {
  848. .vs_vers = 1,
  849. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  850. .vs_proc = nfs4_callback_procedures1,
  851. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  852. .vs_dispatch = NULL,
  853. .vs_hidden = 1,
  854. };
  855. struct svc_version nfs4_callback_version4 = {
  856. .vs_vers = 4,
  857. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  858. .vs_proc = nfs4_callback_procedures1,
  859. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  860. .vs_dispatch = NULL,
  861. };