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