nfs2xdr.c 18 KB

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