nfs2xdr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * linux/fs/nfs/nfs2xdr.c
  3. *
  4. * XDR functions to encode/decode NFS RPC arguments and results.
  5. *
  6. * Copyright (C) 1992, 1993, 1994 Rick Sladkey
  7. * Copyright (C) 1996 Olaf Kirch
  8. * 04 Aug 1998 Ion Badulescu <ionut@cs.columbia.edu>
  9. * FIFO's need special handling in NFSv2
  10. */
  11. #include <linux/param.h>
  12. #include <linux/time.h>
  13. #include <linux/mm.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/in.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/sunrpc/clnt.h>
  20. #include <linux/nfs.h>
  21. #include <linux/nfs2.h>
  22. #include <linux/nfs_fs.h>
  23. #include "internal.h"
  24. #define NFSDBG_FACILITY NFSDBG_XDR
  25. /* Mapping from NFS error code to "errno" error code. */
  26. #define errno_NFSERR_IO EIO
  27. /*
  28. * Declare the space requirements for NFS arguments and replies as
  29. * number of 32bit-words
  30. */
  31. #define NFS_fhandle_sz (8)
  32. #define NFS_sattr_sz (8)
  33. #define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
  34. #define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
  35. #define NFS_fattr_sz (17)
  36. #define NFS_info_sz (5)
  37. #define NFS_entry_sz (NFS_filename_sz+3)
  38. #define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
  39. #define NFS_removeargs_sz (NFS_fhandle_sz+NFS_filename_sz)
  40. #define NFS_sattrargs_sz (NFS_fhandle_sz+NFS_sattr_sz)
  41. #define NFS_readlinkargs_sz (NFS_fhandle_sz)
  42. #define NFS_readargs_sz (NFS_fhandle_sz+3)
  43. #define NFS_writeargs_sz (NFS_fhandle_sz+4)
  44. #define NFS_createargs_sz (NFS_diropargs_sz+NFS_sattr_sz)
  45. #define NFS_renameargs_sz (NFS_diropargs_sz+NFS_diropargs_sz)
  46. #define NFS_linkargs_sz (NFS_fhandle_sz+NFS_diropargs_sz)
  47. #define NFS_symlinkargs_sz (NFS_diropargs_sz+1+NFS_sattr_sz)
  48. #define NFS_readdirargs_sz (NFS_fhandle_sz+2)
  49. #define NFS_attrstat_sz (1+NFS_fattr_sz)
  50. #define NFS_diropres_sz (1+NFS_fhandle_sz+NFS_fattr_sz)
  51. #define NFS_readlinkres_sz (2)
  52. #define NFS_readres_sz (1+NFS_fattr_sz+1)
  53. #define NFS_writeres_sz (NFS_attrstat_sz)
  54. #define NFS_stat_sz (1)
  55. #define NFS_readdirres_sz (1)
  56. #define NFS_statfsres_sz (1+NFS_info_sz)
  57. /*
  58. * Common NFS XDR functions as inlines
  59. */
  60. static inline __be32 *
  61. xdr_encode_fhandle(__be32 *p, const struct nfs_fh *fhandle)
  62. {
  63. memcpy(p, fhandle->data, NFS2_FHSIZE);
  64. return p + XDR_QUADLEN(NFS2_FHSIZE);
  65. }
  66. static inline __be32 *
  67. xdr_decode_fhandle(__be32 *p, struct nfs_fh *fhandle)
  68. {
  69. /* NFSv2 handles have a fixed length */
  70. fhandle->size = NFS2_FHSIZE;
  71. memcpy(fhandle->data, p, NFS2_FHSIZE);
  72. return p + XDR_QUADLEN(NFS2_FHSIZE);
  73. }
  74. static inline __be32*
  75. xdr_encode_time(__be32 *p, struct timespec *timep)
  76. {
  77. *p++ = htonl(timep->tv_sec);
  78. /* Convert nanoseconds into microseconds */
  79. *p++ = htonl(timep->tv_nsec ? timep->tv_nsec / 1000 : 0);
  80. return p;
  81. }
  82. static inline __be32*
  83. xdr_encode_current_server_time(__be32 *p, struct timespec *timep)
  84. {
  85. /*
  86. * Passing the invalid value useconds=1000000 is a
  87. * Sun convention for "set to current server time".
  88. * It's needed to make permissions checks for the
  89. * "touch" program across v2 mounts to Solaris and
  90. * Irix boxes work correctly. See description of
  91. * sattr in section 6.1 of "NFS Illustrated" by
  92. * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
  93. */
  94. *p++ = htonl(timep->tv_sec);
  95. *p++ = htonl(1000000);
  96. return p;
  97. }
  98. static inline __be32*
  99. xdr_decode_time(__be32 *p, struct timespec *timep)
  100. {
  101. timep->tv_sec = ntohl(*p++);
  102. /* Convert microseconds into nanoseconds */
  103. timep->tv_nsec = ntohl(*p++) * 1000;
  104. return p;
  105. }
  106. static __be32 *
  107. xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
  108. {
  109. u32 rdev, type;
  110. type = ntohl(*p++);
  111. fattr->mode = ntohl(*p++);
  112. fattr->nlink = ntohl(*p++);
  113. fattr->uid = ntohl(*p++);
  114. fattr->gid = ntohl(*p++);
  115. fattr->size = ntohl(*p++);
  116. fattr->du.nfs2.blocksize = ntohl(*p++);
  117. rdev = ntohl(*p++);
  118. fattr->du.nfs2.blocks = ntohl(*p++);
  119. fattr->fsid.major = ntohl(*p++);
  120. fattr->fsid.minor = 0;
  121. fattr->fileid = ntohl(*p++);
  122. p = xdr_decode_time(p, &fattr->atime);
  123. p = xdr_decode_time(p, &fattr->mtime);
  124. p = xdr_decode_time(p, &fattr->ctime);
  125. fattr->valid |= NFS_ATTR_FATTR_V2;
  126. fattr->rdev = new_decode_dev(rdev);
  127. if (type == NFCHR && rdev == NFS2_FIFO_DEV) {
  128. fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
  129. fattr->rdev = 0;
  130. }
  131. return p;
  132. }
  133. static inline __be32 *
  134. xdr_encode_sattr(__be32 *p, struct iattr *attr)
  135. {
  136. const __be32 not_set = __constant_htonl(0xFFFFFFFF);
  137. *p++ = (attr->ia_valid & ATTR_MODE) ? htonl(attr->ia_mode) : not_set;
  138. *p++ = (attr->ia_valid & ATTR_UID) ? htonl(attr->ia_uid) : not_set;
  139. *p++ = (attr->ia_valid & ATTR_GID) ? htonl(attr->ia_gid) : not_set;
  140. *p++ = (attr->ia_valid & ATTR_SIZE) ? htonl(attr->ia_size) : not_set;
  141. if (attr->ia_valid & ATTR_ATIME_SET) {
  142. p = xdr_encode_time(p, &attr->ia_atime);
  143. } else if (attr->ia_valid & ATTR_ATIME) {
  144. p = xdr_encode_current_server_time(p, &attr->ia_atime);
  145. } else {
  146. *p++ = not_set;
  147. *p++ = not_set;
  148. }
  149. if (attr->ia_valid & ATTR_MTIME_SET) {
  150. p = xdr_encode_time(p, &attr->ia_mtime);
  151. } else if (attr->ia_valid & ATTR_MTIME) {
  152. p = xdr_encode_current_server_time(p, &attr->ia_mtime);
  153. } else {
  154. *p++ = not_set;
  155. *p++ = not_set;
  156. }
  157. return p;
  158. }
  159. /*
  160. * NFS encode functions
  161. */
  162. /*
  163. * Encode file handle argument
  164. * GETATTR, READLINK, STATFS
  165. */
  166. static int
  167. nfs_xdr_fhandle(struct rpc_rqst *req, __be32 *p, struct nfs_fh *fh)
  168. {
  169. p = xdr_encode_fhandle(p, fh);
  170. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  171. return 0;
  172. }
  173. /*
  174. * Encode SETATTR arguments
  175. */
  176. static int
  177. nfs_xdr_sattrargs(struct rpc_rqst *req, __be32 *p, struct nfs_sattrargs *args)
  178. {
  179. p = xdr_encode_fhandle(p, args->fh);
  180. p = xdr_encode_sattr(p, args->sattr);
  181. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  182. return 0;
  183. }
  184. /*
  185. * Encode directory ops argument
  186. * LOOKUP, RMDIR
  187. */
  188. static int
  189. nfs_xdr_diropargs(struct rpc_rqst *req, __be32 *p, struct nfs_diropargs *args)
  190. {
  191. p = xdr_encode_fhandle(p, args->fh);
  192. p = xdr_encode_array(p, args->name, args->len);
  193. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  194. return 0;
  195. }
  196. /*
  197. * Encode REMOVE argument
  198. */
  199. static int
  200. nfs_xdr_removeargs(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
  201. {
  202. p = xdr_encode_fhandle(p, args->fh);
  203. p = xdr_encode_array(p, args->name.name, args->name.len);
  204. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  205. return 0;
  206. }
  207. /*
  208. * Arguments to a READ call. Since we read data directly into the page
  209. * cache, we also set up the reply iovec here so that iov[1] points
  210. * exactly to the page we want to fetch.
  211. */
  212. static int
  213. nfs_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
  214. {
  215. struct rpc_auth *auth = req->rq_cred->cr_auth;
  216. unsigned int replen;
  217. u32 offset = (u32)args->offset;
  218. u32 count = args->count;
  219. p = xdr_encode_fhandle(p, args->fh);
  220. *p++ = htonl(offset);
  221. *p++ = htonl(count);
  222. *p++ = htonl(count);
  223. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  224. /* Inline the page array */
  225. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readres_sz) << 2;
  226. xdr_inline_pages(&req->rq_rcv_buf, replen,
  227. args->pages, args->pgbase, count);
  228. req->rq_rcv_buf.flags |= XDRBUF_READ;
  229. return 0;
  230. }
  231. /*
  232. * Decode READ reply
  233. */
  234. static int
  235. nfs_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
  236. {
  237. struct kvec *iov = req->rq_rcv_buf.head;
  238. size_t hdrlen;
  239. u32 count, recvd;
  240. int status;
  241. if ((status = ntohl(*p++)))
  242. return nfs_stat_to_errno(status);
  243. p = xdr_decode_fattr(p, res->fattr);
  244. count = ntohl(*p++);
  245. res->eof = 0;
  246. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  247. if (iov->iov_len < hdrlen) {
  248. dprintk("NFS: READ reply header overflowed:"
  249. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  250. return -errno_NFSERR_IO;
  251. } else if (iov->iov_len != hdrlen) {
  252. dprintk("NFS: READ header is short. iovec will be shifted.\n");
  253. xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
  254. }
  255. recvd = req->rq_rcv_buf.len - hdrlen;
  256. if (count > recvd) {
  257. dprintk("NFS: server cheating in read reply: "
  258. "count %u > recvd %u\n", count, recvd);
  259. count = recvd;
  260. }
  261. dprintk("RPC: readres OK count %u\n", count);
  262. if (count < res->count)
  263. res->count = count;
  264. return count;
  265. }
  266. /*
  267. * Write arguments. Splice the buffer to be written into the iovec.
  268. */
  269. static int
  270. nfs_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
  271. {
  272. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  273. u32 offset = (u32)args->offset;
  274. u32 count = args->count;
  275. p = xdr_encode_fhandle(p, args->fh);
  276. *p++ = htonl(offset);
  277. *p++ = htonl(offset);
  278. *p++ = htonl(count);
  279. *p++ = htonl(count);
  280. sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
  281. /* Copy the page array */
  282. xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
  283. sndbuf->flags |= XDRBUF_WRITE;
  284. return 0;
  285. }
  286. /*
  287. * Encode create arguments
  288. * CREATE, MKDIR
  289. */
  290. static int
  291. nfs_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs_createargs *args)
  292. {
  293. p = xdr_encode_fhandle(p, args->fh);
  294. p = xdr_encode_array(p, args->name, args->len);
  295. p = xdr_encode_sattr(p, args->sattr);
  296. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  297. return 0;
  298. }
  299. /*
  300. * Encode RENAME arguments
  301. */
  302. static int
  303. nfs_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs_renameargs *args)
  304. {
  305. p = xdr_encode_fhandle(p, args->fromfh);
  306. p = xdr_encode_array(p, args->fromname, args->fromlen);
  307. p = xdr_encode_fhandle(p, args->tofh);
  308. p = xdr_encode_array(p, args->toname, args->tolen);
  309. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  310. return 0;
  311. }
  312. /*
  313. * Encode LINK arguments
  314. */
  315. static int
  316. nfs_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs_linkargs *args)
  317. {
  318. p = xdr_encode_fhandle(p, args->fromfh);
  319. p = xdr_encode_fhandle(p, args->tofh);
  320. p = xdr_encode_array(p, args->toname, args->tolen);
  321. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  322. return 0;
  323. }
  324. /*
  325. * Encode SYMLINK arguments
  326. */
  327. static int
  328. nfs_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_symlinkargs *args)
  329. {
  330. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  331. size_t pad;
  332. p = xdr_encode_fhandle(p, args->fromfh);
  333. p = xdr_encode_array(p, args->fromname, args->fromlen);
  334. *p++ = htonl(args->pathlen);
  335. sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
  336. xdr_encode_pages(sndbuf, args->pages, 0, args->pathlen);
  337. /*
  338. * xdr_encode_pages may have added a few bytes to ensure the
  339. * pathname ends on a 4-byte boundary. Start encoding the
  340. * attributes after the pad bytes.
  341. */
  342. pad = sndbuf->tail->iov_len;
  343. if (pad > 0)
  344. p++;
  345. p = xdr_encode_sattr(p, args->sattr);
  346. sndbuf->len += xdr_adjust_iovec(sndbuf->tail, p) - pad;
  347. return 0;
  348. }
  349. /*
  350. * Encode arguments to readdir call
  351. */
  352. static int
  353. nfs_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs_readdirargs *args)
  354. {
  355. struct rpc_auth *auth = req->rq_cred->cr_auth;
  356. unsigned int replen;
  357. u32 count = args->count;
  358. p = xdr_encode_fhandle(p, args->fh);
  359. *p++ = htonl(args->cookie);
  360. *p++ = htonl(count); /* see above */
  361. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  362. /* Inline the page array */
  363. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
  364. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
  365. return 0;
  366. }
  367. /*
  368. * Decode the result of a readdir call.
  369. * We're not really decoding anymore, we just leave the buffer untouched
  370. * and only check that it is syntactically correct.
  371. * The real decoding happens in nfs_decode_entry below, called directly
  372. * from nfs_readdir for each entry.
  373. */
  374. static int
  375. nfs_xdr_readdirres(struct rpc_rqst *req, __be32 *p, void *dummy)
  376. {
  377. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  378. struct kvec *iov = rcvbuf->head;
  379. struct page **page;
  380. size_t hdrlen;
  381. unsigned int pglen, recvd;
  382. u32 len;
  383. int status, nr = 0;
  384. __be32 *end, *entry, *kaddr;
  385. if ((status = ntohl(*p++)))
  386. return nfs_stat_to_errno(status);
  387. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  388. if (iov->iov_len < hdrlen) {
  389. dprintk("NFS: READDIR reply header overflowed:"
  390. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  391. return -errno_NFSERR_IO;
  392. } else if (iov->iov_len != hdrlen) {
  393. dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
  394. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  395. }
  396. pglen = rcvbuf->page_len;
  397. recvd = rcvbuf->len - hdrlen;
  398. if (pglen > recvd)
  399. pglen = recvd;
  400. page = rcvbuf->pages;
  401. kaddr = p = kmap_atomic(*page, KM_USER0);
  402. end = (__be32 *)((char *)p + pglen);
  403. entry = p;
  404. /* Make sure the packet actually has a value_follows and EOF entry */
  405. if ((entry + 1) > end)
  406. goto short_pkt;
  407. for (; *p++; nr++) {
  408. if (p + 2 > end)
  409. goto short_pkt;
  410. p++; /* fileid */
  411. len = ntohl(*p++);
  412. p += XDR_QUADLEN(len) + 1; /* name plus cookie */
  413. if (len > NFS2_MAXNAMLEN) {
  414. dprintk("NFS: giant filename in readdir (len 0x%x)!\n",
  415. len);
  416. goto err_unmap;
  417. }
  418. if (p + 2 > end)
  419. goto short_pkt;
  420. entry = p;
  421. }
  422. /*
  423. * Apparently some server sends responses that are a valid size, but
  424. * contain no entries, and have value_follows==0 and EOF==0. For
  425. * those, just set the EOF marker.
  426. */
  427. if (!nr && entry[1] == 0) {
  428. dprintk("NFS: readdir reply truncated!\n");
  429. entry[1] = 1;
  430. }
  431. out:
  432. kunmap_atomic(kaddr, KM_USER0);
  433. return nr;
  434. short_pkt:
  435. /*
  436. * When we get a short packet there are 2 possibilities. We can
  437. * return an error, or fix up the response to look like a valid
  438. * response and return what we have so far. If there are no
  439. * entries and the packet was short, then return -EIO. If there
  440. * are valid entries in the response, return them and pretend that
  441. * the call was successful, but incomplete. The caller can retry the
  442. * readdir starting at the last cookie.
  443. */
  444. entry[0] = entry[1] = 0;
  445. if (!nr)
  446. nr = -errno_NFSERR_IO;
  447. goto out;
  448. err_unmap:
  449. nr = -errno_NFSERR_IO;
  450. goto out;
  451. }
  452. __be32 *
  453. nfs_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
  454. {
  455. if (!*p++) {
  456. if (!*p)
  457. return ERR_PTR(-EAGAIN);
  458. entry->eof = 1;
  459. return ERR_PTR(-EBADCOOKIE);
  460. }
  461. entry->ino = ntohl(*p++);
  462. entry->len = ntohl(*p++);
  463. entry->name = (const char *) p;
  464. p += XDR_QUADLEN(entry->len);
  465. entry->prev_cookie = entry->cookie;
  466. entry->cookie = ntohl(*p++);
  467. entry->eof = !p[0] && p[1];
  468. return p;
  469. }
  470. /*
  471. * NFS XDR decode functions
  472. */
  473. /*
  474. * Decode simple status reply
  475. */
  476. static int
  477. nfs_xdr_stat(struct rpc_rqst *req, __be32 *p, void *dummy)
  478. {
  479. int status;
  480. if ((status = ntohl(*p++)) != 0)
  481. status = nfs_stat_to_errno(status);
  482. return status;
  483. }
  484. /*
  485. * Decode attrstat reply
  486. * GETATTR, SETATTR, WRITE
  487. */
  488. static int
  489. nfs_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
  490. {
  491. int status;
  492. if ((status = ntohl(*p++)))
  493. return nfs_stat_to_errno(status);
  494. xdr_decode_fattr(p, fattr);
  495. return 0;
  496. }
  497. /*
  498. * Decode diropres reply
  499. * LOOKUP, CREATE, MKDIR
  500. */
  501. static int
  502. nfs_xdr_diropres(struct rpc_rqst *req, __be32 *p, struct nfs_diropok *res)
  503. {
  504. int status;
  505. if ((status = ntohl(*p++)))
  506. return nfs_stat_to_errno(status);
  507. p = xdr_decode_fhandle(p, res->fh);
  508. xdr_decode_fattr(p, res->fattr);
  509. return 0;
  510. }
  511. /*
  512. * Encode READLINK args
  513. */
  514. static int
  515. nfs_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_readlinkargs *args)
  516. {
  517. struct rpc_auth *auth = req->rq_cred->cr_auth;
  518. unsigned int replen;
  519. p = xdr_encode_fhandle(p, args->fh);
  520. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  521. /* Inline the page array */
  522. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
  523. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
  524. return 0;
  525. }
  526. /*
  527. * Decode READLINK reply
  528. */
  529. static int
  530. nfs_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, void *dummy)
  531. {
  532. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  533. struct kvec *iov = rcvbuf->head;
  534. size_t hdrlen;
  535. u32 len, recvd;
  536. char *kaddr;
  537. int status;
  538. if ((status = ntohl(*p++)))
  539. return nfs_stat_to_errno(status);
  540. /* Convert length of symlink */
  541. len = ntohl(*p++);
  542. if (len >= rcvbuf->page_len) {
  543. dprintk("nfs: server returned giant symlink!\n");
  544. return -ENAMETOOLONG;
  545. }
  546. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  547. if (iov->iov_len < hdrlen) {
  548. dprintk("NFS: READLINK reply header overflowed:"
  549. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  550. return -errno_NFSERR_IO;
  551. } else if (iov->iov_len != hdrlen) {
  552. dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
  553. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  554. }
  555. recvd = req->rq_rcv_buf.len - hdrlen;
  556. if (recvd < len) {
  557. dprintk("NFS: server cheating in readlink reply: "
  558. "count %u > recvd %u\n", len, recvd);
  559. return -EIO;
  560. }
  561. /* NULL terminate the string we got */
  562. kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
  563. kaddr[len+rcvbuf->page_base] = '\0';
  564. kunmap_atomic(kaddr, KM_USER0);
  565. return 0;
  566. }
  567. /*
  568. * Decode WRITE reply
  569. */
  570. static int
  571. nfs_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
  572. {
  573. res->verf->committed = NFS_FILE_SYNC;
  574. return nfs_xdr_attrstat(req, p, res->fattr);
  575. }
  576. /*
  577. * Decode STATFS reply
  578. */
  579. static int
  580. nfs_xdr_statfsres(struct rpc_rqst *req, __be32 *p, struct nfs2_fsstat *res)
  581. {
  582. int status;
  583. if ((status = ntohl(*p++)))
  584. return nfs_stat_to_errno(status);
  585. res->tsize = ntohl(*p++);
  586. res->bsize = ntohl(*p++);
  587. res->blocks = ntohl(*p++);
  588. res->bfree = ntohl(*p++);
  589. res->bavail = ntohl(*p++);
  590. return 0;
  591. }
  592. /*
  593. * We need to translate between nfs status return values and
  594. * the local errno values which may not be the same.
  595. */
  596. static struct {
  597. int stat;
  598. int errno;
  599. } nfs_errtbl[] = {
  600. { NFS_OK, 0 },
  601. { NFSERR_PERM, -EPERM },
  602. { NFSERR_NOENT, -ENOENT },
  603. { NFSERR_IO, -errno_NFSERR_IO},
  604. { NFSERR_NXIO, -ENXIO },
  605. /* { NFSERR_EAGAIN, -EAGAIN }, */
  606. { NFSERR_ACCES, -EACCES },
  607. { NFSERR_EXIST, -EEXIST },
  608. { NFSERR_XDEV, -EXDEV },
  609. { NFSERR_NODEV, -ENODEV },
  610. { NFSERR_NOTDIR, -ENOTDIR },
  611. { NFSERR_ISDIR, -EISDIR },
  612. { NFSERR_INVAL, -EINVAL },
  613. { NFSERR_FBIG, -EFBIG },
  614. { NFSERR_NOSPC, -ENOSPC },
  615. { NFSERR_ROFS, -EROFS },
  616. { NFSERR_MLINK, -EMLINK },
  617. { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
  618. { NFSERR_NOTEMPTY, -ENOTEMPTY },
  619. { NFSERR_DQUOT, -EDQUOT },
  620. { NFSERR_STALE, -ESTALE },
  621. { NFSERR_REMOTE, -EREMOTE },
  622. #ifdef EWFLUSH
  623. { NFSERR_WFLUSH, -EWFLUSH },
  624. #endif
  625. { NFSERR_BADHANDLE, -EBADHANDLE },
  626. { NFSERR_NOT_SYNC, -ENOTSYNC },
  627. { NFSERR_BAD_COOKIE, -EBADCOOKIE },
  628. { NFSERR_NOTSUPP, -ENOTSUPP },
  629. { NFSERR_TOOSMALL, -ETOOSMALL },
  630. { NFSERR_SERVERFAULT, -EREMOTEIO },
  631. { NFSERR_BADTYPE, -EBADTYPE },
  632. { NFSERR_JUKEBOX, -EJUKEBOX },
  633. { -1, -EIO }
  634. };
  635. /*
  636. * Convert an NFS error code to a local one.
  637. * This one is used jointly by NFSv2 and NFSv3.
  638. */
  639. int
  640. nfs_stat_to_errno(int stat)
  641. {
  642. int i;
  643. for (i = 0; nfs_errtbl[i].stat != -1; i++) {
  644. if (nfs_errtbl[i].stat == stat)
  645. return nfs_errtbl[i].errno;
  646. }
  647. dprintk("nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
  648. return nfs_errtbl[i].errno;
  649. }
  650. #define PROC(proc, argtype, restype, timer) \
  651. [NFSPROC_##proc] = { \
  652. .p_proc = NFSPROC_##proc, \
  653. .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
  654. .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
  655. .p_arglen = NFS_##argtype##_sz, \
  656. .p_replen = NFS_##restype##_sz, \
  657. .p_timer = timer, \
  658. .p_statidx = NFSPROC_##proc, \
  659. .p_name = #proc, \
  660. }
  661. struct rpc_procinfo nfs_procedures[] = {
  662. PROC(GETATTR, fhandle, attrstat, 1),
  663. PROC(SETATTR, sattrargs, attrstat, 0),
  664. PROC(LOOKUP, diropargs, diropres, 2),
  665. PROC(READLINK, readlinkargs, readlinkres, 3),
  666. PROC(READ, readargs, readres, 3),
  667. PROC(WRITE, writeargs, writeres, 4),
  668. PROC(CREATE, createargs, diropres, 0),
  669. PROC(REMOVE, removeargs, stat, 0),
  670. PROC(RENAME, renameargs, stat, 0),
  671. PROC(LINK, linkargs, stat, 0),
  672. PROC(SYMLINK, symlinkargs, stat, 0),
  673. PROC(MKDIR, createargs, diropres, 0),
  674. PROC(RMDIR, diropargs, stat, 0),
  675. PROC(READDIR, readdirargs, readdirres, 3),
  676. PROC(STATFS, fhandle, statfsres, 0),
  677. };
  678. struct rpc_version nfs_version2 = {
  679. .number = 2,
  680. .nrprocs = ARRAY_SIZE(nfs_procedures),
  681. .procs = nfs_procedures
  682. };