callback_xdr.c 23 KB

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