callback_xdr.c 25 KB

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