nfs2xdr.c 18 KB

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