callback_xdr.c 25 KB

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