nfs2xdr.c 19 KB

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