callback_xdr.c 25 KB

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