nfs2xdr.c 19 KB

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