callback_xdr.c 28 KB

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