callback_xdr.c 25 KB

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