nfs2xdr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. /* #define NFS_PARANOIA 1 */
  28. /* Mapping from NFS error code to "errno" error code. */
  29. #define errno_NFSERR_IO EIO
  30. /*
  31. * Declare the space requirements for NFS arguments and replies as
  32. * number of 32bit-words
  33. */
  34. #define NFS_fhandle_sz (8)
  35. #define NFS_sattr_sz (8)
  36. #define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
  37. #define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
  38. #define NFS_fattr_sz (17)
  39. #define NFS_info_sz (5)
  40. #define NFS_entry_sz (NFS_filename_sz+3)
  41. #define NFS_diropargs_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, 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, REMOVE, 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. * Arguments to a READ call. Since we read data directly into the page
  201. * cache, we also set up the reply iovec here so that iov[1] points
  202. * exactly to the page we want to fetch.
  203. */
  204. static int
  205. nfs_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
  206. {
  207. struct rpc_auth *auth = req->rq_task->tk_auth;
  208. unsigned int replen;
  209. u32 offset = (u32)args->offset;
  210. u32 count = args->count;
  211. p = xdr_encode_fhandle(p, args->fh);
  212. *p++ = htonl(offset);
  213. *p++ = htonl(count);
  214. *p++ = htonl(count);
  215. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  216. /* Inline the page array */
  217. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readres_sz) << 2;
  218. xdr_inline_pages(&req->rq_rcv_buf, replen,
  219. args->pages, args->pgbase, count);
  220. return 0;
  221. }
  222. /*
  223. * Decode READ reply
  224. */
  225. static int
  226. nfs_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
  227. {
  228. struct kvec *iov = req->rq_rcv_buf.head;
  229. int status, count, recvd, hdrlen;
  230. if ((status = ntohl(*p++)))
  231. return -nfs_stat_to_errno(status);
  232. p = xdr_decode_fattr(p, res->fattr);
  233. count = ntohl(*p++);
  234. res->eof = 0;
  235. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  236. if (iov->iov_len < hdrlen) {
  237. printk(KERN_WARNING "NFS: READ reply header overflowed:"
  238. "length %d > %Zu\n", hdrlen, iov->iov_len);
  239. return -errno_NFSERR_IO;
  240. } else if (iov->iov_len != hdrlen) {
  241. dprintk("NFS: READ header is short. iovec will be shifted.\n");
  242. xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
  243. }
  244. recvd = req->rq_rcv_buf.len - hdrlen;
  245. if (count > recvd) {
  246. printk(KERN_WARNING "NFS: server cheating in read reply: "
  247. "count %d > recvd %d\n", count, recvd);
  248. count = recvd;
  249. }
  250. dprintk("RPC: readres OK count %d\n", count);
  251. if (count < res->count)
  252. res->count = count;
  253. return count;
  254. }
  255. /*
  256. * Write arguments. Splice the buffer to be written into the iovec.
  257. */
  258. static int
  259. nfs_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
  260. {
  261. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  262. u32 offset = (u32)args->offset;
  263. u32 count = args->count;
  264. p = xdr_encode_fhandle(p, args->fh);
  265. *p++ = htonl(offset);
  266. *p++ = htonl(offset);
  267. *p++ = htonl(count);
  268. *p++ = htonl(count);
  269. sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
  270. /* Copy the page array */
  271. xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
  272. return 0;
  273. }
  274. /*
  275. * Encode create arguments
  276. * CREATE, MKDIR
  277. */
  278. static int
  279. nfs_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs_createargs *args)
  280. {
  281. p = xdr_encode_fhandle(p, args->fh);
  282. p = xdr_encode_array(p, args->name, args->len);
  283. p = xdr_encode_sattr(p, args->sattr);
  284. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  285. return 0;
  286. }
  287. /*
  288. * Encode RENAME arguments
  289. */
  290. static int
  291. nfs_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs_renameargs *args)
  292. {
  293. p = xdr_encode_fhandle(p, args->fromfh);
  294. p = xdr_encode_array(p, args->fromname, args->fromlen);
  295. p = xdr_encode_fhandle(p, args->tofh);
  296. p = xdr_encode_array(p, args->toname, args->tolen);
  297. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  298. return 0;
  299. }
  300. /*
  301. * Encode LINK arguments
  302. */
  303. static int
  304. nfs_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs_linkargs *args)
  305. {
  306. p = xdr_encode_fhandle(p, args->fromfh);
  307. p = xdr_encode_fhandle(p, args->tofh);
  308. p = xdr_encode_array(p, args->toname, args->tolen);
  309. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  310. return 0;
  311. }
  312. /*
  313. * Encode SYMLINK arguments
  314. */
  315. static int
  316. nfs_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_symlinkargs *args)
  317. {
  318. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  319. size_t pad;
  320. p = xdr_encode_fhandle(p, args->fromfh);
  321. p = xdr_encode_array(p, args->fromname, args->fromlen);
  322. *p++ = htonl(args->pathlen);
  323. sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
  324. xdr_encode_pages(sndbuf, args->pages, 0, args->pathlen);
  325. /*
  326. * xdr_encode_pages may have added a few bytes to ensure the
  327. * pathname ends on a 4-byte boundary. Start encoding the
  328. * attributes after the pad bytes.
  329. */
  330. pad = sndbuf->tail->iov_len;
  331. if (pad > 0)
  332. p++;
  333. p = xdr_encode_sattr(p, args->sattr);
  334. sndbuf->len += xdr_adjust_iovec(sndbuf->tail, p) - pad;
  335. return 0;
  336. }
  337. /*
  338. * Encode arguments to readdir call
  339. */
  340. static int
  341. nfs_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs_readdirargs *args)
  342. {
  343. struct rpc_task *task = req->rq_task;
  344. struct rpc_auth *auth = task->tk_auth;
  345. unsigned int replen;
  346. u32 count = args->count;
  347. p = xdr_encode_fhandle(p, args->fh);
  348. *p++ = htonl(args->cookie);
  349. *p++ = htonl(count); /* see above */
  350. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  351. /* Inline the page array */
  352. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
  353. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
  354. return 0;
  355. }
  356. /*
  357. * Decode the result of a readdir call.
  358. * We're not really decoding anymore, we just leave the buffer untouched
  359. * and only check that it is syntactically correct.
  360. * The real decoding happens in nfs_decode_entry below, called directly
  361. * from nfs_readdir for each entry.
  362. */
  363. static int
  364. nfs_xdr_readdirres(struct rpc_rqst *req, __be32 *p, void *dummy)
  365. {
  366. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  367. struct kvec *iov = rcvbuf->head;
  368. struct page **page;
  369. int hdrlen, recvd;
  370. int status, nr;
  371. unsigned int len, pglen;
  372. __be32 *end, *entry, *kaddr;
  373. if ((status = ntohl(*p++)))
  374. return -nfs_stat_to_errno(status);
  375. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  376. if (iov->iov_len < hdrlen) {
  377. printk(KERN_WARNING "NFS: READDIR reply header overflowed:"
  378. "length %d > %Zu\n", hdrlen, iov->iov_len);
  379. return -errno_NFSERR_IO;
  380. } else if (iov->iov_len != hdrlen) {
  381. dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
  382. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  383. }
  384. pglen = rcvbuf->page_len;
  385. recvd = rcvbuf->len - hdrlen;
  386. if (pglen > recvd)
  387. pglen = recvd;
  388. page = rcvbuf->pages;
  389. kaddr = p = kmap_atomic(*page, KM_USER0);
  390. end = (__be32 *)((char *)p + pglen);
  391. entry = p;
  392. for (nr = 0; *p++; nr++) {
  393. if (p + 2 > end)
  394. goto short_pkt;
  395. p++; /* fileid */
  396. len = ntohl(*p++);
  397. p += XDR_QUADLEN(len) + 1; /* name plus cookie */
  398. if (len > NFS2_MAXNAMLEN) {
  399. printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)!\n",
  400. len);
  401. goto err_unmap;
  402. }
  403. if (p + 2 > end)
  404. goto short_pkt;
  405. entry = p;
  406. }
  407. if (!nr && (entry[0] != 0 || entry[1] == 0))
  408. goto short_pkt;
  409. out:
  410. kunmap_atomic(kaddr, KM_USER0);
  411. return nr;
  412. short_pkt:
  413. entry[0] = entry[1] = 0;
  414. /* truncate listing ? */
  415. if (!nr) {
  416. printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
  417. entry[1] = 1;
  418. }
  419. goto out;
  420. err_unmap:
  421. nr = -errno_NFSERR_IO;
  422. goto out;
  423. }
  424. __be32 *
  425. nfs_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
  426. {
  427. if (!*p++) {
  428. if (!*p)
  429. return ERR_PTR(-EAGAIN);
  430. entry->eof = 1;
  431. return ERR_PTR(-EBADCOOKIE);
  432. }
  433. entry->ino = ntohl(*p++);
  434. entry->len = ntohl(*p++);
  435. entry->name = (const char *) p;
  436. p += XDR_QUADLEN(entry->len);
  437. entry->prev_cookie = entry->cookie;
  438. entry->cookie = ntohl(*p++);
  439. entry->eof = !p[0] && p[1];
  440. return p;
  441. }
  442. /*
  443. * NFS XDR decode functions
  444. */
  445. /*
  446. * Decode simple status reply
  447. */
  448. static int
  449. nfs_xdr_stat(struct rpc_rqst *req, __be32 *p, void *dummy)
  450. {
  451. int status;
  452. if ((status = ntohl(*p++)) != 0)
  453. status = -nfs_stat_to_errno(status);
  454. return status;
  455. }
  456. /*
  457. * Decode attrstat reply
  458. * GETATTR, SETATTR, WRITE
  459. */
  460. static int
  461. nfs_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
  462. {
  463. int status;
  464. if ((status = ntohl(*p++)))
  465. return -nfs_stat_to_errno(status);
  466. xdr_decode_fattr(p, fattr);
  467. return 0;
  468. }
  469. /*
  470. * Decode diropres reply
  471. * LOOKUP, CREATE, MKDIR
  472. */
  473. static int
  474. nfs_xdr_diropres(struct rpc_rqst *req, __be32 *p, struct nfs_diropok *res)
  475. {
  476. int status;
  477. if ((status = ntohl(*p++)))
  478. return -nfs_stat_to_errno(status);
  479. p = xdr_decode_fhandle(p, res->fh);
  480. xdr_decode_fattr(p, res->fattr);
  481. return 0;
  482. }
  483. /*
  484. * Encode READLINK args
  485. */
  486. static int
  487. nfs_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs_readlinkargs *args)
  488. {
  489. struct rpc_auth *auth = req->rq_task->tk_auth;
  490. unsigned int replen;
  491. p = xdr_encode_fhandle(p, args->fh);
  492. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  493. /* Inline the page array */
  494. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
  495. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
  496. return 0;
  497. }
  498. /*
  499. * Decode READLINK reply
  500. */
  501. static int
  502. nfs_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, void *dummy)
  503. {
  504. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  505. struct kvec *iov = rcvbuf->head;
  506. int hdrlen, len, recvd;
  507. char *kaddr;
  508. int status;
  509. if ((status = ntohl(*p++)))
  510. return -nfs_stat_to_errno(status);
  511. /* Convert length of symlink */
  512. len = ntohl(*p++);
  513. if (len >= rcvbuf->page_len || len <= 0) {
  514. dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
  515. return -ENAMETOOLONG;
  516. }
  517. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  518. if (iov->iov_len < hdrlen) {
  519. printk(KERN_WARNING "NFS: READLINK reply header overflowed:"
  520. "length %d > %Zu\n", hdrlen, iov->iov_len);
  521. return -errno_NFSERR_IO;
  522. } else if (iov->iov_len != hdrlen) {
  523. dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
  524. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  525. }
  526. recvd = req->rq_rcv_buf.len - hdrlen;
  527. if (recvd < len) {
  528. printk(KERN_WARNING "NFS: server cheating in readlink reply: "
  529. "count %u > recvd %u\n", len, recvd);
  530. return -EIO;
  531. }
  532. /* NULL terminate the string we got */
  533. kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
  534. kaddr[len+rcvbuf->page_base] = '\0';
  535. kunmap_atomic(kaddr, KM_USER0);
  536. return 0;
  537. }
  538. /*
  539. * Decode WRITE reply
  540. */
  541. static int
  542. nfs_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
  543. {
  544. res->verf->committed = NFS_FILE_SYNC;
  545. return nfs_xdr_attrstat(req, p, res->fattr);
  546. }
  547. /*
  548. * Decode STATFS reply
  549. */
  550. static int
  551. nfs_xdr_statfsres(struct rpc_rqst *req, __be32 *p, struct nfs2_fsstat *res)
  552. {
  553. int status;
  554. if ((status = ntohl(*p++)))
  555. return -nfs_stat_to_errno(status);
  556. res->tsize = ntohl(*p++);
  557. res->bsize = ntohl(*p++);
  558. res->blocks = ntohl(*p++);
  559. res->bfree = ntohl(*p++);
  560. res->bavail = ntohl(*p++);
  561. return 0;
  562. }
  563. /*
  564. * We need to translate between nfs status return values and
  565. * the local errno values which may not be the same.
  566. */
  567. static struct {
  568. int stat;
  569. int errno;
  570. } nfs_errtbl[] = {
  571. { NFS_OK, 0 },
  572. { NFSERR_PERM, EPERM },
  573. { NFSERR_NOENT, ENOENT },
  574. { NFSERR_IO, errno_NFSERR_IO },
  575. { NFSERR_NXIO, ENXIO },
  576. /* { NFSERR_EAGAIN, EAGAIN }, */
  577. { NFSERR_ACCES, EACCES },
  578. { NFSERR_EXIST, EEXIST },
  579. { NFSERR_XDEV, EXDEV },
  580. { NFSERR_NODEV, ENODEV },
  581. { NFSERR_NOTDIR, ENOTDIR },
  582. { NFSERR_ISDIR, EISDIR },
  583. { NFSERR_INVAL, EINVAL },
  584. { NFSERR_FBIG, EFBIG },
  585. { NFSERR_NOSPC, ENOSPC },
  586. { NFSERR_ROFS, EROFS },
  587. { NFSERR_MLINK, EMLINK },
  588. { NFSERR_NAMETOOLONG, ENAMETOOLONG },
  589. { NFSERR_NOTEMPTY, ENOTEMPTY },
  590. { NFSERR_DQUOT, EDQUOT },
  591. { NFSERR_STALE, ESTALE },
  592. { NFSERR_REMOTE, EREMOTE },
  593. #ifdef EWFLUSH
  594. { NFSERR_WFLUSH, EWFLUSH },
  595. #endif
  596. { NFSERR_BADHANDLE, EBADHANDLE },
  597. { NFSERR_NOT_SYNC, ENOTSYNC },
  598. { NFSERR_BAD_COOKIE, EBADCOOKIE },
  599. { NFSERR_NOTSUPP, ENOTSUPP },
  600. { NFSERR_TOOSMALL, ETOOSMALL },
  601. { NFSERR_SERVERFAULT, ESERVERFAULT },
  602. { NFSERR_BADTYPE, EBADTYPE },
  603. { NFSERR_JUKEBOX, EJUKEBOX },
  604. { -1, EIO }
  605. };
  606. /*
  607. * Convert an NFS error code to a local one.
  608. * This one is used jointly by NFSv2 and NFSv3.
  609. */
  610. int
  611. nfs_stat_to_errno(int stat)
  612. {
  613. int i;
  614. for (i = 0; nfs_errtbl[i].stat != -1; i++) {
  615. if (nfs_errtbl[i].stat == stat)
  616. return nfs_errtbl[i].errno;
  617. }
  618. printk(KERN_ERR "nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
  619. return nfs_errtbl[i].errno;
  620. }
  621. #ifndef MAX
  622. # define MAX(a, b) (((a) > (b))? (a) : (b))
  623. #endif
  624. #define PROC(proc, argtype, restype, timer) \
  625. [NFSPROC_##proc] = { \
  626. .p_proc = NFSPROC_##proc, \
  627. .p_encode = (kxdrproc_t) nfs_xdr_##argtype, \
  628. .p_decode = (kxdrproc_t) nfs_xdr_##restype, \
  629. .p_bufsiz = MAX(NFS_##argtype##_sz,NFS_##restype##_sz) << 2, \
  630. .p_timer = timer, \
  631. .p_statidx = NFSPROC_##proc, \
  632. .p_name = #proc, \
  633. }
  634. struct rpc_procinfo nfs_procedures[] = {
  635. PROC(GETATTR, fhandle, attrstat, 1),
  636. PROC(SETATTR, sattrargs, attrstat, 0),
  637. PROC(LOOKUP, diropargs, diropres, 2),
  638. PROC(READLINK, readlinkargs, readlinkres, 3),
  639. PROC(READ, readargs, readres, 3),
  640. PROC(WRITE, writeargs, writeres, 4),
  641. PROC(CREATE, createargs, diropres, 0),
  642. PROC(REMOVE, diropargs, stat, 0),
  643. PROC(RENAME, renameargs, stat, 0),
  644. PROC(LINK, linkargs, stat, 0),
  645. PROC(SYMLINK, symlinkargs, stat, 0),
  646. PROC(MKDIR, createargs, diropres, 0),
  647. PROC(RMDIR, diropargs, stat, 0),
  648. PROC(READDIR, readdirargs, readdirres, 3),
  649. PROC(STATFS, fhandle, statfsres, 0),
  650. };
  651. struct rpc_version nfs_version2 = {
  652. .number = 2,
  653. .nrprocs = ARRAY_SIZE(nfs_procedures),
  654. .procs = nfs_procedures
  655. };