nfs2xdr.c 19 KB

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