nfs2xdr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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->old_dir);
  306. p = xdr_encode_array(p, args->old_name->name, args->old_name->len);
  307. p = xdr_encode_fhandle(p, args->new_dir);
  308. p = xdr_encode_array(p, args->new_name->name, args->new_name->len);
  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. int status;
  383. if ((status = ntohl(*p++)))
  384. return nfs_stat_to_errno(status);
  385. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  386. if (iov->iov_len < hdrlen) {
  387. dprintk("NFS: READDIR reply header overflowed:"
  388. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  389. return -errno_NFSERR_IO;
  390. } else if (iov->iov_len != hdrlen) {
  391. dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
  392. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  393. }
  394. pglen = rcvbuf->page_len;
  395. recvd = rcvbuf->len - hdrlen;
  396. if (pglen > recvd)
  397. pglen = recvd;
  398. page = rcvbuf->pages;
  399. return pglen;
  400. }
  401. static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
  402. {
  403. dprintk("nfs: %s: prematurely hit end of receive buffer. "
  404. "Remaining buffer length is %tu words.\n",
  405. func, xdr->end - xdr->p);
  406. }
  407. __be32 *
  408. nfs_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry, struct nfs_server *server, int plus)
  409. {
  410. __be32 *p;
  411. p = xdr_inline_decode(xdr, 4);
  412. if (unlikely(!p))
  413. goto out_overflow;
  414. if (!ntohl(*p++)) {
  415. p = xdr_inline_decode(xdr, 4);
  416. if (unlikely(!p))
  417. goto out_overflow;
  418. if (!ntohl(*p++))
  419. return ERR_PTR(-EAGAIN);
  420. entry->eof = 1;
  421. return ERR_PTR(-EBADCOOKIE);
  422. }
  423. p = xdr_inline_decode(xdr, 8);
  424. if (unlikely(!p))
  425. goto out_overflow;
  426. entry->ino = ntohl(*p++);
  427. entry->len = ntohl(*p++);
  428. p = xdr_inline_decode(xdr, entry->len + 4);
  429. if (unlikely(!p))
  430. goto out_overflow;
  431. entry->name = (const char *) p;
  432. p += XDR_QUADLEN(entry->len);
  433. entry->prev_cookie = entry->cookie;
  434. entry->cookie = ntohl(*p++);
  435. entry->d_type = DT_UNKNOWN;
  436. p = xdr_inline_peek(xdr, 8);
  437. if (p != NULL)
  438. entry->eof = !p[0] && p[1];
  439. else
  440. entry->eof = 0;
  441. return p;
  442. out_overflow:
  443. print_overflow_msg(__func__, xdr);
  444. return ERR_PTR(-EAGAIN);
  445. }
  446. /*
  447. * NFS XDR decode functions
  448. */
  449. /*
  450. * Decode simple status reply
  451. */
  452. static int
  453. nfs_xdr_stat(struct rpc_rqst *req, __be32 *p, void *dummy)
  454. {
  455. int status;
  456. if ((status = ntohl(*p++)) != 0)
  457. status = nfs_stat_to_errno(status);
  458. return status;
  459. }
  460. /*
  461. * Decode attrstat reply
  462. * GETATTR, SETATTR, WRITE
  463. */
  464. static int
  465. nfs_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
  466. {
  467. int status;
  468. if ((status = ntohl(*p++)))
  469. return nfs_stat_to_errno(status);
  470. xdr_decode_fattr(p, fattr);
  471. return 0;
  472. }
  473. /*
  474. * Decode diropres reply
  475. * LOOKUP, CREATE, MKDIR
  476. */
  477. static int
  478. nfs_xdr_diropres(struct rpc_rqst *req, __be32 *p, struct nfs_diropok *res)
  479. {
  480. int status;
  481. if ((status = ntohl(*p++)))
  482. return nfs_stat_to_errno(status);
  483. p = xdr_decode_fhandle(p, res->fh);
  484. xdr_decode_fattr(p, res->fattr);
  485. return 0;
  486. }
  487. /*
  488. * Encode READLINK args
  489. */
  490. static int
  491. nfs_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_readlinkargs *args)
  492. {
  493. struct rpc_auth *auth = req->rq_cred->cr_auth;
  494. unsigned int replen;
  495. p = xdr_encode_fhandle(p, args->fh);
  496. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  497. /* Inline the page array */
  498. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
  499. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
  500. return 0;
  501. }
  502. /*
  503. * Decode READLINK reply
  504. */
  505. static int
  506. nfs_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, void *dummy)
  507. {
  508. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  509. struct kvec *iov = rcvbuf->head;
  510. size_t hdrlen;
  511. u32 len, recvd;
  512. int status;
  513. if ((status = ntohl(*p++)))
  514. return nfs_stat_to_errno(status);
  515. /* Convert length of symlink */
  516. len = ntohl(*p++);
  517. if (len >= rcvbuf->page_len) {
  518. dprintk("nfs: server returned giant symlink!\n");
  519. return -ENAMETOOLONG;
  520. }
  521. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  522. if (iov->iov_len < hdrlen) {
  523. dprintk("NFS: READLINK reply header overflowed:"
  524. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  525. return -errno_NFSERR_IO;
  526. } else if (iov->iov_len != hdrlen) {
  527. dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
  528. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  529. }
  530. recvd = req->rq_rcv_buf.len - hdrlen;
  531. if (recvd < len) {
  532. dprintk("NFS: server cheating in readlink reply: "
  533. "count %u > recvd %u\n", len, recvd);
  534. return -EIO;
  535. }
  536. xdr_terminate_string(rcvbuf, len);
  537. return 0;
  538. }
  539. /*
  540. * Decode WRITE reply
  541. */
  542. static int
  543. nfs_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
  544. {
  545. res->verf->committed = NFS_FILE_SYNC;
  546. return nfs_xdr_attrstat(req, p, res->fattr);
  547. }
  548. /*
  549. * Decode STATFS reply
  550. */
  551. static int
  552. nfs_xdr_statfsres(struct rpc_rqst *req, __be32 *p, struct nfs2_fsstat *res)
  553. {
  554. int status;
  555. if ((status = ntohl(*p++)))
  556. return nfs_stat_to_errno(status);
  557. res->tsize = ntohl(*p++);
  558. res->bsize = ntohl(*p++);
  559. res->blocks = ntohl(*p++);
  560. res->bfree = ntohl(*p++);
  561. res->bavail = ntohl(*p++);
  562. return 0;
  563. }
  564. /*
  565. * We need to translate between nfs status return values and
  566. * the local errno values which may not be the same.
  567. */
  568. static struct {
  569. int stat;
  570. int errno;
  571. } nfs_errtbl[] = {
  572. { NFS_OK, 0 },
  573. { NFSERR_PERM, -EPERM },
  574. { NFSERR_NOENT, -ENOENT },
  575. { NFSERR_IO, -errno_NFSERR_IO},
  576. { NFSERR_NXIO, -ENXIO },
  577. /* { NFSERR_EAGAIN, -EAGAIN }, */
  578. { NFSERR_ACCES, -EACCES },
  579. { NFSERR_EXIST, -EEXIST },
  580. { NFSERR_XDEV, -EXDEV },
  581. { NFSERR_NODEV, -ENODEV },
  582. { NFSERR_NOTDIR, -ENOTDIR },
  583. { NFSERR_ISDIR, -EISDIR },
  584. { NFSERR_INVAL, -EINVAL },
  585. { NFSERR_FBIG, -EFBIG },
  586. { NFSERR_NOSPC, -ENOSPC },
  587. { NFSERR_ROFS, -EROFS },
  588. { NFSERR_MLINK, -EMLINK },
  589. { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
  590. { NFSERR_NOTEMPTY, -ENOTEMPTY },
  591. { NFSERR_DQUOT, -EDQUOT },
  592. { NFSERR_STALE, -ESTALE },
  593. { NFSERR_REMOTE, -EREMOTE },
  594. #ifdef EWFLUSH
  595. { NFSERR_WFLUSH, -EWFLUSH },
  596. #endif
  597. { NFSERR_BADHANDLE, -EBADHANDLE },
  598. { NFSERR_NOT_SYNC, -ENOTSYNC },
  599. { NFSERR_BAD_COOKIE, -EBADCOOKIE },
  600. { NFSERR_NOTSUPP, -ENOTSUPP },
  601. { NFSERR_TOOSMALL, -ETOOSMALL },
  602. { NFSERR_SERVERFAULT, -EREMOTEIO },
  603. { NFSERR_BADTYPE, -EBADTYPE },
  604. { NFSERR_JUKEBOX, -EJUKEBOX },
  605. { -1, -EIO }
  606. };
  607. /*
  608. * Convert an NFS error code to a local one.
  609. * This one is used jointly by NFSv2 and NFSv3.
  610. */
  611. int
  612. nfs_stat_to_errno(int stat)
  613. {
  614. int i;
  615. for (i = 0; nfs_errtbl[i].stat != -1; i++) {
  616. if (nfs_errtbl[i].stat == stat)
  617. return nfs_errtbl[i].errno;
  618. }
  619. dprintk("nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
  620. return nfs_errtbl[i].errno;
  621. }
  622. #define PROC(proc, argtype, restype, timer) \
  623. [NFSPROC_##proc] = { \
  624. .p_proc = NFSPROC_##proc, \
  625. .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
  626. .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
  627. .p_arglen = NFS_##argtype##_sz, \
  628. .p_replen = NFS_##restype##_sz, \
  629. .p_timer = timer, \
  630. .p_statidx = NFSPROC_##proc, \
  631. .p_name = #proc, \
  632. }
  633. struct rpc_procinfo nfs_procedures[] = {
  634. PROC(GETATTR, fhandle, attrstat, 1),
  635. PROC(SETATTR, sattrargs, attrstat, 0),
  636. PROC(LOOKUP, diropargs, diropres, 2),
  637. PROC(READLINK, readlinkargs, readlinkres, 3),
  638. PROC(READ, readargs, readres, 3),
  639. PROC(WRITE, writeargs, writeres, 4),
  640. PROC(CREATE, createargs, diropres, 0),
  641. PROC(REMOVE, removeargs, stat, 0),
  642. PROC(RENAME, renameargs, stat, 0),
  643. PROC(LINK, linkargs, stat, 0),
  644. PROC(SYMLINK, symlinkargs, stat, 0),
  645. PROC(MKDIR, createargs, diropres, 0),
  646. PROC(RMDIR, diropargs, stat, 0),
  647. PROC(READDIR, readdirargs, readdirres, 3),
  648. PROC(STATFS, fhandle, statfsres, 0),
  649. };
  650. struct rpc_version nfs_version2 = {
  651. .number = 2,
  652. .nrprocs = ARRAY_SIZE(nfs_procedures),
  653. .procs = nfs_procedures
  654. };