nfs2xdr.c 19 KB

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