nfs2xdr.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/in.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/sunrpc/clnt.h>
  20. #include <linux/nfs.h>
  21. #include <linux/nfs2.h>
  22. #include <linux/nfs_fs.h>
  23. #include "internal.h"
  24. #define NFSDBG_FACILITY NFSDBG_XDR
  25. /* Mapping from NFS error code to "errno" error code. */
  26. #define errno_NFSERR_IO EIO
  27. /*
  28. * Declare the space requirements for NFS arguments and replies as
  29. * number of 32bit-words
  30. */
  31. #define NFS_fhandle_sz (8)
  32. #define NFS_sattr_sz (8)
  33. #define NFS_filename_sz (1+(NFS2_MAXNAMLEN>>2))
  34. #define NFS_path_sz (1+(NFS2_MAXPATHLEN>>2))
  35. #define NFS_fattr_sz (17)
  36. #define NFS_info_sz (5)
  37. #define NFS_entry_sz (NFS_filename_sz+3)
  38. #define NFS_diropargs_sz (NFS_fhandle_sz+NFS_filename_sz)
  39. #define NFS_removeargs_sz (NFS_fhandle_sz+NFS_filename_sz)
  40. #define NFS_sattrargs_sz (NFS_fhandle_sz+NFS_sattr_sz)
  41. #define NFS_readlinkargs_sz (NFS_fhandle_sz)
  42. #define NFS_readargs_sz (NFS_fhandle_sz+3)
  43. #define NFS_writeargs_sz (NFS_fhandle_sz+4)
  44. #define NFS_createargs_sz (NFS_diropargs_sz+NFS_sattr_sz)
  45. #define NFS_renameargs_sz (NFS_diropargs_sz+NFS_diropargs_sz)
  46. #define NFS_linkargs_sz (NFS_fhandle_sz+NFS_diropargs_sz)
  47. #define NFS_symlinkargs_sz (NFS_diropargs_sz+1+NFS_sattr_sz)
  48. #define NFS_readdirargs_sz (NFS_fhandle_sz+2)
  49. #define NFS_attrstat_sz (1+NFS_fattr_sz)
  50. #define NFS_diropres_sz (1+NFS_fhandle_sz+NFS_fattr_sz)
  51. #define NFS_readlinkres_sz (2)
  52. #define NFS_readres_sz (1+NFS_fattr_sz+1)
  53. #define NFS_writeres_sz (NFS_attrstat_sz)
  54. #define NFS_stat_sz (1)
  55. #define NFS_readdirres_sz (1)
  56. #define NFS_statfsres_sz (1+NFS_info_sz)
  57. /*
  58. * While encoding arguments, set up the reply buffer in advance to
  59. * receive reply data directly into the page cache.
  60. */
  61. static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
  62. unsigned int base, unsigned int len,
  63. unsigned int bufsize)
  64. {
  65. struct rpc_auth *auth = req->rq_cred->cr_auth;
  66. unsigned int replen;
  67. replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
  68. xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
  69. }
  70. /*
  71. * Handle decode buffer overflows out-of-line.
  72. */
  73. static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
  74. {
  75. dprintk("NFS: %s prematurely hit the end of our receive buffer. "
  76. "Remaining buffer length is %tu words.\n",
  77. func, xdr->end - xdr->p);
  78. }
  79. /*
  80. * Common NFS XDR functions as inlines
  81. */
  82. static inline __be32*
  83. xdr_decode_time(__be32 *p, struct timespec *timep)
  84. {
  85. timep->tv_sec = ntohl(*p++);
  86. /* Convert microseconds into nanoseconds */
  87. timep->tv_nsec = ntohl(*p++) * 1000;
  88. return p;
  89. }
  90. static __be32 *
  91. xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
  92. {
  93. u32 rdev, type;
  94. type = ntohl(*p++);
  95. fattr->mode = ntohl(*p++);
  96. fattr->nlink = ntohl(*p++);
  97. fattr->uid = ntohl(*p++);
  98. fattr->gid = ntohl(*p++);
  99. fattr->size = ntohl(*p++);
  100. fattr->du.nfs2.blocksize = ntohl(*p++);
  101. rdev = ntohl(*p++);
  102. fattr->du.nfs2.blocks = ntohl(*p++);
  103. fattr->fsid.major = ntohl(*p++);
  104. fattr->fsid.minor = 0;
  105. fattr->fileid = ntohl(*p++);
  106. p = xdr_decode_time(p, &fattr->atime);
  107. p = xdr_decode_time(p, &fattr->mtime);
  108. p = xdr_decode_time(p, &fattr->ctime);
  109. fattr->valid |= NFS_ATTR_FATTR_V2;
  110. fattr->rdev = new_decode_dev(rdev);
  111. if (type == NFCHR && rdev == NFS2_FIFO_DEV) {
  112. fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
  113. fattr->rdev = 0;
  114. }
  115. return p;
  116. }
  117. /*
  118. * Encode/decode NFSv2 basic data types
  119. *
  120. * Basic NFSv2 data types are defined in section 2.3 of RFC 1094:
  121. * "NFS: Network File System Protocol Specification".
  122. *
  123. * Not all basic data types have their own encoding and decoding
  124. * functions. For run-time efficiency, some data types are encoded
  125. * or decoded inline.
  126. */
  127. /*
  128. * typedef opaque nfsdata<>;
  129. */
  130. static int decode_nfsdata(struct xdr_stream *xdr, struct nfs_readres *result)
  131. {
  132. u32 recvd, count;
  133. size_t hdrlen;
  134. __be32 *p;
  135. p = xdr_inline_decode(xdr, 4);
  136. if (unlikely(p == NULL))
  137. goto out_overflow;
  138. count = be32_to_cpup(p);
  139. hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
  140. recvd = xdr->buf->len - hdrlen;
  141. if (unlikely(count > recvd))
  142. goto out_cheating;
  143. out:
  144. xdr_read_pages(xdr, count);
  145. result->eof = 0; /* NFSv2 does not pass EOF flag on the wire. */
  146. result->count = count;
  147. return count;
  148. out_cheating:
  149. dprintk("NFS: server cheating in read result: "
  150. "count %u > recvd %u\n", count, recvd);
  151. count = recvd;
  152. goto out;
  153. out_overflow:
  154. print_overflow_msg(__func__, xdr);
  155. return -EIO;
  156. }
  157. /*
  158. * enum stat {
  159. * NFS_OK = 0,
  160. * NFSERR_PERM = 1,
  161. * NFSERR_NOENT = 2,
  162. * NFSERR_IO = 5,
  163. * NFSERR_NXIO = 6,
  164. * NFSERR_ACCES = 13,
  165. * NFSERR_EXIST = 17,
  166. * NFSERR_NODEV = 19,
  167. * NFSERR_NOTDIR = 20,
  168. * NFSERR_ISDIR = 21,
  169. * NFSERR_FBIG = 27,
  170. * NFSERR_NOSPC = 28,
  171. * NFSERR_ROFS = 30,
  172. * NFSERR_NAMETOOLONG = 63,
  173. * NFSERR_NOTEMPTY = 66,
  174. * NFSERR_DQUOT = 69,
  175. * NFSERR_STALE = 70,
  176. * NFSERR_WFLUSH = 99
  177. * };
  178. */
  179. static int decode_stat(struct xdr_stream *xdr, enum nfs_stat *status)
  180. {
  181. __be32 *p;
  182. p = xdr_inline_decode(xdr, 4);
  183. if (unlikely(p == NULL))
  184. goto out_overflow;
  185. *status = be32_to_cpup(p);
  186. return 0;
  187. out_overflow:
  188. print_overflow_msg(__func__, xdr);
  189. return -EIO;
  190. }
  191. /*
  192. * 2.3.3. fhandle
  193. *
  194. * typedef opaque fhandle[FHSIZE];
  195. */
  196. static void encode_fhandle(struct xdr_stream *xdr, const struct nfs_fh *fh)
  197. {
  198. __be32 *p;
  199. BUG_ON(fh->size != NFS2_FHSIZE);
  200. p = xdr_reserve_space(xdr, NFS2_FHSIZE);
  201. memcpy(p, fh->data, NFS2_FHSIZE);
  202. }
  203. static int decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
  204. {
  205. __be32 *p;
  206. p = xdr_inline_decode(xdr, NFS2_FHSIZE);
  207. if (unlikely(p == NULL))
  208. goto out_overflow;
  209. fh->size = NFS2_FHSIZE;
  210. memcpy(fh->data, p, NFS2_FHSIZE);
  211. return 0;
  212. out_overflow:
  213. print_overflow_msg(__func__, xdr);
  214. return -EIO;
  215. }
  216. /*
  217. * 2.3.4. timeval
  218. *
  219. * struct timeval {
  220. * unsigned int seconds;
  221. * unsigned int useconds;
  222. * };
  223. */
  224. static __be32 *xdr_encode_time(__be32 *p, const struct timespec *timep)
  225. {
  226. *p++ = cpu_to_be32(timep->tv_sec);
  227. if (timep->tv_nsec != 0)
  228. *p++ = cpu_to_be32(timep->tv_nsec / NSEC_PER_USEC);
  229. else
  230. *p++ = cpu_to_be32(0);
  231. return p;
  232. }
  233. /*
  234. * Passing the invalid value useconds=1000000 is a Sun convention for
  235. * "set to current server time". It's needed to make permissions checks
  236. * for the "touch" program across v2 mounts to Solaris and Irix servers
  237. * work correctly. See description of sattr in section 6.1 of "NFS
  238. * Illustrated" by Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5.
  239. */
  240. static __be32 *xdr_encode_current_server_time(__be32 *p,
  241. const struct timespec *timep)
  242. {
  243. *p++ = cpu_to_be32(timep->tv_sec);
  244. *p++ = cpu_to_be32(1000000);
  245. return p;
  246. }
  247. /*
  248. * 2.3.5. fattr
  249. *
  250. * struct fattr {
  251. * ftype type;
  252. * unsigned int mode;
  253. * unsigned int nlink;
  254. * unsigned int uid;
  255. * unsigned int gid;
  256. * unsigned int size;
  257. * unsigned int blocksize;
  258. * unsigned int rdev;
  259. * unsigned int blocks;
  260. * unsigned int fsid;
  261. * unsigned int fileid;
  262. * timeval atime;
  263. * timeval mtime;
  264. * timeval ctime;
  265. * };
  266. *
  267. */
  268. static int decode_fattr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  269. {
  270. __be32 *p;
  271. p = xdr_inline_decode(xdr, NFS_fattr_sz << 2);
  272. if (unlikely(p == NULL))
  273. goto out_overflow;
  274. xdr_decode_fattr(p, fattr);
  275. return 0;
  276. out_overflow:
  277. print_overflow_msg(__func__, xdr);
  278. return -EIO;
  279. }
  280. /*
  281. * 2.3.6. sattr
  282. *
  283. * struct sattr {
  284. * unsigned int mode;
  285. * unsigned int uid;
  286. * unsigned int gid;
  287. * unsigned int size;
  288. * timeval atime;
  289. * timeval mtime;
  290. * };
  291. */
  292. #define NFS2_SATTR_NOT_SET (0xffffffff)
  293. static __be32 *xdr_time_not_set(__be32 *p)
  294. {
  295. *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
  296. *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
  297. return p;
  298. }
  299. static void encode_sattr(struct xdr_stream *xdr, const struct iattr *attr)
  300. {
  301. __be32 *p;
  302. p = xdr_reserve_space(xdr, NFS_sattr_sz << 2);
  303. if (attr->ia_valid & ATTR_MODE)
  304. *p++ = cpu_to_be32(attr->ia_mode);
  305. else
  306. *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
  307. if (attr->ia_valid & ATTR_UID)
  308. *p++ = cpu_to_be32(attr->ia_uid);
  309. else
  310. *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
  311. if (attr->ia_valid & ATTR_GID)
  312. *p++ = cpu_to_be32(attr->ia_gid);
  313. else
  314. *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
  315. if (attr->ia_valid & ATTR_SIZE)
  316. *p++ = cpu_to_be32((u32)attr->ia_size);
  317. else
  318. *p++ = cpu_to_be32(NFS2_SATTR_NOT_SET);
  319. if (attr->ia_valid & ATTR_ATIME_SET)
  320. p = xdr_encode_time(p, &attr->ia_atime);
  321. else if (attr->ia_valid & ATTR_ATIME)
  322. p = xdr_encode_current_server_time(p, &attr->ia_atime);
  323. else
  324. p = xdr_time_not_set(p);
  325. if (attr->ia_valid & ATTR_MTIME_SET)
  326. xdr_encode_time(p, &attr->ia_mtime);
  327. else if (attr->ia_valid & ATTR_MTIME)
  328. xdr_encode_current_server_time(p, &attr->ia_mtime);
  329. else
  330. xdr_time_not_set(p);
  331. }
  332. /*
  333. * 2.3.7. filename
  334. *
  335. * typedef string filename<MAXNAMLEN>;
  336. */
  337. static void encode_filename(struct xdr_stream *xdr,
  338. const char *name, u32 length)
  339. {
  340. __be32 *p;
  341. BUG_ON(length > NFS2_MAXNAMLEN);
  342. p = xdr_reserve_space(xdr, 4 + length);
  343. xdr_encode_opaque(p, name, length);
  344. }
  345. static int decode_filename_inline(struct xdr_stream *xdr,
  346. const char **name, u32 *length)
  347. {
  348. __be32 *p;
  349. u32 count;
  350. p = xdr_inline_decode(xdr, 4);
  351. if (unlikely(p == NULL))
  352. goto out_overflow;
  353. count = be32_to_cpup(p);
  354. if (count > NFS3_MAXNAMLEN)
  355. goto out_nametoolong;
  356. p = xdr_inline_decode(xdr, count);
  357. if (unlikely(p == NULL))
  358. goto out_overflow;
  359. *name = (const char *)p;
  360. *length = count;
  361. return 0;
  362. out_nametoolong:
  363. dprintk("NFS: returned filename too long: %u\n", count);
  364. return -ENAMETOOLONG;
  365. out_overflow:
  366. print_overflow_msg(__func__, xdr);
  367. return -EIO;
  368. }
  369. /*
  370. * 2.3.8. path
  371. *
  372. * typedef string path<MAXPATHLEN>;
  373. */
  374. static void encode_path(struct xdr_stream *xdr, struct page **pages, u32 length)
  375. {
  376. __be32 *p;
  377. BUG_ON(length > NFS2_MAXPATHLEN);
  378. p = xdr_reserve_space(xdr, 4);
  379. *p = cpu_to_be32(length);
  380. xdr_write_pages(xdr, pages, 0, length);
  381. }
  382. static int decode_path(struct xdr_stream *xdr)
  383. {
  384. u32 length, recvd;
  385. size_t hdrlen;
  386. __be32 *p;
  387. p = xdr_inline_decode(xdr, 4);
  388. if (unlikely(p == NULL))
  389. goto out_overflow;
  390. length = be32_to_cpup(p);
  391. if (unlikely(length >= xdr->buf->page_len || length > NFS_MAXPATHLEN))
  392. goto out_size;
  393. hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
  394. recvd = xdr->buf->len - hdrlen;
  395. if (unlikely(length > recvd))
  396. goto out_cheating;
  397. xdr_read_pages(xdr, length);
  398. xdr_terminate_string(xdr->buf, length);
  399. return 0;
  400. out_size:
  401. dprintk("NFS: returned pathname too long: %u\n", length);
  402. return -ENAMETOOLONG;
  403. out_cheating:
  404. dprintk("NFS: server cheating in pathname result: "
  405. "length %u > received %u\n", length, recvd);
  406. return -EIO;
  407. out_overflow:
  408. print_overflow_msg(__func__, xdr);
  409. return -EIO;
  410. }
  411. /*
  412. * 2.3.9. attrstat
  413. *
  414. * union attrstat switch (stat status) {
  415. * case NFS_OK:
  416. * fattr attributes;
  417. * default:
  418. * void;
  419. * };
  420. */
  421. static int decode_attrstat(struct xdr_stream *xdr, struct nfs_fattr *result)
  422. {
  423. enum nfs_stat status;
  424. int error;
  425. error = decode_stat(xdr, &status);
  426. if (unlikely(error))
  427. goto out;
  428. if (status != NFS_OK)
  429. goto out_default;
  430. error = decode_fattr(xdr, result);
  431. out:
  432. return error;
  433. out_default:
  434. return nfs_stat_to_errno(status);
  435. }
  436. /*
  437. * 2.3.10. diropargs
  438. *
  439. * struct diropargs {
  440. * fhandle dir;
  441. * filename name;
  442. * };
  443. */
  444. static void encode_diropargs(struct xdr_stream *xdr, const struct nfs_fh *fh,
  445. const char *name, u32 length)
  446. {
  447. encode_fhandle(xdr, fh);
  448. encode_filename(xdr, name, length);
  449. }
  450. /*
  451. * 2.3.11. diropres
  452. *
  453. * union diropres switch (stat status) {
  454. * case NFS_OK:
  455. * struct {
  456. * fhandle file;
  457. * fattr attributes;
  458. * } diropok;
  459. * default:
  460. * void;
  461. * };
  462. */
  463. static int decode_diropok(struct xdr_stream *xdr, struct nfs_diropok *result)
  464. {
  465. int error;
  466. error = decode_fhandle(xdr, result->fh);
  467. if (unlikely(error))
  468. goto out;
  469. error = decode_fattr(xdr, result->fattr);
  470. out:
  471. return error;
  472. }
  473. static int decode_diropres(struct xdr_stream *xdr, struct nfs_diropok *result)
  474. {
  475. enum nfs_stat status;
  476. int error;
  477. error = decode_stat(xdr, &status);
  478. if (unlikely(error))
  479. goto out;
  480. if (status != NFS_OK)
  481. goto out_default;
  482. error = decode_diropok(xdr, result);
  483. out:
  484. return error;
  485. out_default:
  486. return nfs_stat_to_errno(status);
  487. }
  488. /*
  489. * NFSv2 XDR encode functions
  490. *
  491. * NFSv2 argument types are defined in section 2.2 of RFC 1094:
  492. * "NFS: Network File System Protocol Specification".
  493. */
  494. static int nfs2_xdr_enc_fhandle(struct rpc_rqst *req, __be32 *p,
  495. const struct nfs_fh *fh)
  496. {
  497. struct xdr_stream xdr;
  498. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  499. encode_fhandle(&xdr, fh);
  500. return 0;
  501. }
  502. /*
  503. * 2.2.3. sattrargs
  504. *
  505. * struct sattrargs {
  506. * fhandle file;
  507. * sattr attributes;
  508. * };
  509. */
  510. static int nfs2_xdr_enc_sattrargs(struct rpc_rqst *req, __be32 *p,
  511. const struct nfs_sattrargs *args)
  512. {
  513. struct xdr_stream xdr;
  514. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  515. encode_fhandle(&xdr, args->fh);
  516. encode_sattr(&xdr, args->sattr);
  517. return 0;
  518. }
  519. static int nfs2_xdr_enc_diropargs(struct rpc_rqst *req, __be32 *p,
  520. const struct nfs_diropargs *args)
  521. {
  522. struct xdr_stream xdr;
  523. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  524. encode_diropargs(&xdr, args->fh, args->name, args->len);
  525. return 0;
  526. }
  527. static int nfs2_xdr_enc_readlinkargs(struct rpc_rqst *req, __be32 *p,
  528. const struct nfs_readlinkargs *args)
  529. {
  530. struct xdr_stream xdr;
  531. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  532. encode_fhandle(&xdr, args->fh);
  533. prepare_reply_buffer(req, args->pages, args->pgbase,
  534. args->pglen, NFS_readlinkres_sz);
  535. return 0;
  536. }
  537. /*
  538. * 2.2.7. readargs
  539. *
  540. * struct readargs {
  541. * fhandle file;
  542. * unsigned offset;
  543. * unsigned count;
  544. * unsigned totalcount;
  545. * };
  546. */
  547. static void encode_readargs(struct xdr_stream *xdr,
  548. const struct nfs_readargs *args)
  549. {
  550. u32 offset = args->offset;
  551. u32 count = args->count;
  552. __be32 *p;
  553. encode_fhandle(xdr, args->fh);
  554. p = xdr_reserve_space(xdr, 4 + 4 + 4);
  555. *p++ = cpu_to_be32(offset);
  556. *p++ = cpu_to_be32(count);
  557. *p = cpu_to_be32(count);
  558. }
  559. static int nfs2_xdr_enc_readargs(struct rpc_rqst *req, __be32 *p,
  560. const struct nfs_readargs *args)
  561. {
  562. struct xdr_stream xdr;
  563. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  564. encode_readargs(&xdr, args);
  565. prepare_reply_buffer(req, args->pages, args->pgbase,
  566. args->count, NFS_readres_sz);
  567. req->rq_rcv_buf.flags |= XDRBUF_READ;
  568. return 0;
  569. }
  570. /*
  571. * 2.2.9. writeargs
  572. *
  573. * struct writeargs {
  574. * fhandle file;
  575. * unsigned beginoffset;
  576. * unsigned offset;
  577. * unsigned totalcount;
  578. * nfsdata data;
  579. * };
  580. */
  581. static void encode_writeargs(struct xdr_stream *xdr,
  582. const struct nfs_writeargs *args)
  583. {
  584. u32 offset = args->offset;
  585. u32 count = args->count;
  586. __be32 *p;
  587. encode_fhandle(xdr, args->fh);
  588. p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
  589. *p++ = cpu_to_be32(offset);
  590. *p++ = cpu_to_be32(offset);
  591. *p++ = cpu_to_be32(count);
  592. /* nfsdata */
  593. *p = cpu_to_be32(count);
  594. xdr_write_pages(xdr, args->pages, args->pgbase, count);
  595. }
  596. static int nfs2_xdr_enc_writeargs(struct rpc_rqst *req, __be32 *p,
  597. const struct nfs_writeargs *args)
  598. {
  599. struct xdr_stream xdr;
  600. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  601. encode_writeargs(&xdr, args);
  602. xdr.buf->flags |= XDRBUF_WRITE;
  603. return 0;
  604. }
  605. /*
  606. * 2.2.10. createargs
  607. *
  608. * struct createargs {
  609. * diropargs where;
  610. * sattr attributes;
  611. * };
  612. */
  613. static int nfs2_xdr_enc_createargs(struct rpc_rqst *req, __be32 *p,
  614. const struct nfs_createargs *args)
  615. {
  616. struct xdr_stream xdr;
  617. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  618. encode_diropargs(&xdr, args->fh, args->name, args->len);
  619. encode_sattr(&xdr, args->sattr);
  620. return 0;
  621. }
  622. static int nfs2_xdr_enc_removeargs(struct rpc_rqst *req, __be32 *p,
  623. const struct nfs_removeargs *args)
  624. {
  625. struct xdr_stream xdr;
  626. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  627. encode_diropargs(&xdr, args->fh, args->name.name, args->name.len);
  628. return 0;
  629. }
  630. /*
  631. * 2.2.12. renameargs
  632. *
  633. * struct renameargs {
  634. * diropargs from;
  635. * diropargs to;
  636. * };
  637. */
  638. static int nfs2_xdr_enc_renameargs(struct rpc_rqst *req, __be32 *p,
  639. const struct nfs_renameargs *args)
  640. {
  641. const struct qstr *old = args->old_name;
  642. const struct qstr *new = args->new_name;
  643. struct xdr_stream xdr;
  644. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  645. encode_diropargs(&xdr, args->old_dir, old->name, old->len);
  646. encode_diropargs(&xdr, args->new_dir, new->name, new->len);
  647. return 0;
  648. }
  649. /*
  650. * 2.2.13. linkargs
  651. *
  652. * struct linkargs {
  653. * fhandle from;
  654. * diropargs to;
  655. * };
  656. */
  657. static int nfs2_xdr_enc_linkargs(struct rpc_rqst *req, __be32 *p,
  658. const struct nfs_linkargs *args)
  659. {
  660. struct xdr_stream xdr;
  661. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  662. encode_fhandle(&xdr, args->fromfh);
  663. encode_diropargs(&xdr, args->tofh, args->toname, args->tolen);
  664. return 0;
  665. }
  666. /*
  667. * 2.2.14. symlinkargs
  668. *
  669. * struct symlinkargs {
  670. * diropargs from;
  671. * path to;
  672. * sattr attributes;
  673. * };
  674. */
  675. static int nfs2_xdr_enc_symlinkargs(struct rpc_rqst *req, __be32 *p,
  676. const struct nfs_symlinkargs *args)
  677. {
  678. struct xdr_stream xdr;
  679. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  680. encode_diropargs(&xdr, args->fromfh, args->fromname, args->fromlen);
  681. encode_path(&xdr, args->pages, args->pathlen);
  682. encode_sattr(&xdr, args->sattr);
  683. return 0;
  684. }
  685. /*
  686. * 2.2.17. readdirargs
  687. *
  688. * struct readdirargs {
  689. * fhandle dir;
  690. * nfscookie cookie;
  691. * unsigned count;
  692. * };
  693. */
  694. static void encode_readdirargs(struct xdr_stream *xdr,
  695. const struct nfs_readdirargs *args)
  696. {
  697. __be32 *p;
  698. encode_fhandle(xdr, args->fh);
  699. p = xdr_reserve_space(xdr, 4 + 4);
  700. *p++ = cpu_to_be32(args->cookie);
  701. *p = cpu_to_be32(args->count);
  702. }
  703. static int nfs2_xdr_enc_readdirargs(struct rpc_rqst *req, __be32 *p,
  704. const struct nfs_readdirargs *args)
  705. {
  706. struct xdr_stream xdr;
  707. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  708. encode_readdirargs(&xdr, args);
  709. prepare_reply_buffer(req, args->pages, 0,
  710. args->count, NFS_readdirres_sz);
  711. return 0;
  712. }
  713. /*
  714. * NFSv2 XDR decode functions
  715. *
  716. * NFSv2 result types are defined in section 2.2 of RFC 1094:
  717. * "NFS: Network File System Protocol Specification".
  718. */
  719. static int nfs2_xdr_dec_stat(struct rpc_rqst *req, __be32 *p,
  720. void *__unused)
  721. {
  722. struct xdr_stream xdr;
  723. enum nfs_stat status;
  724. int error;
  725. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  726. error = decode_stat(&xdr, &status);
  727. if (unlikely(error))
  728. goto out;
  729. if (status != NFS_OK)
  730. goto out_default;
  731. out:
  732. return error;
  733. out_default:
  734. return nfs_stat_to_errno(status);
  735. }
  736. static int nfs2_xdr_dec_attrstat(struct rpc_rqst *req, __be32 *p,
  737. struct nfs_fattr *result)
  738. {
  739. struct xdr_stream xdr;
  740. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  741. return decode_attrstat(&xdr, result);
  742. }
  743. static int nfs2_xdr_dec_diropres(struct rpc_rqst *req, __be32 *p,
  744. struct nfs_diropok *result)
  745. {
  746. struct xdr_stream xdr;
  747. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  748. return decode_diropres(&xdr, result);
  749. }
  750. /*
  751. * 2.2.6. readlinkres
  752. *
  753. * union readlinkres switch (stat status) {
  754. * case NFS_OK:
  755. * path data;
  756. * default:
  757. * void;
  758. * };
  759. */
  760. static int nfs2_xdr_dec_readlinkres(struct rpc_rqst *req, __be32 *p,
  761. void *__unused)
  762. {
  763. struct xdr_stream xdr;
  764. enum nfs_stat status;
  765. int error;
  766. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  767. error = decode_stat(&xdr, &status);
  768. if (unlikely(error))
  769. goto out;
  770. if (status != NFS_OK)
  771. goto out_default;
  772. error = decode_path(&xdr);
  773. out:
  774. return error;
  775. out_default:
  776. return nfs_stat_to_errno(status);
  777. }
  778. /*
  779. * 2.2.7. readres
  780. *
  781. * union readres switch (stat status) {
  782. * case NFS_OK:
  783. * fattr attributes;
  784. * nfsdata data;
  785. * default:
  786. * void;
  787. * };
  788. */
  789. static int nfs2_xdr_dec_readres(struct rpc_rqst *req, __be32 *p,
  790. struct nfs_readres *result)
  791. {
  792. struct xdr_stream xdr;
  793. enum nfs_stat status;
  794. int error;
  795. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  796. error = decode_stat(&xdr, &status);
  797. if (unlikely(error))
  798. goto out;
  799. if (status != NFS_OK)
  800. goto out_default;
  801. error = decode_fattr(&xdr, result->fattr);
  802. if (unlikely(error))
  803. goto out;
  804. error = decode_nfsdata(&xdr, result);
  805. out:
  806. return error;
  807. out_default:
  808. return nfs_stat_to_errno(status);
  809. }
  810. static int nfs2_xdr_dec_writeres(struct rpc_rqst *req, __be32 *p,
  811. struct nfs_writeres *result)
  812. {
  813. struct xdr_stream xdr;
  814. /* All NFSv2 writes are "file sync" writes */
  815. result->verf->committed = NFS_FILE_SYNC;
  816. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  817. return decode_attrstat(&xdr, result->fattr);
  818. }
  819. /**
  820. * nfs2_decode_dirent - Decode a single NFSv2 directory entry stored in
  821. * the local page cache.
  822. * @xdr: XDR stream where entry resides
  823. * @entry: buffer to fill in with entry data
  824. * @server: nfs_server data for this directory
  825. * @plus: boolean indicating whether this should be a readdirplus entry
  826. *
  827. * Returns the position of the next item in the buffer, or an ERR_PTR.
  828. *
  829. * This function is not invoked during READDIR reply decoding, but
  830. * rather whenever an application invokes the getdents(2) system call
  831. * on a directory already in our cache.
  832. *
  833. * 2.2.17. entry
  834. *
  835. * struct entry {
  836. * unsigned fileid;
  837. * filename name;
  838. * nfscookie cookie;
  839. * entry *nextentry;
  840. * };
  841. */
  842. __be32 *nfs2_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
  843. struct nfs_server *server, int plus)
  844. {
  845. __be32 *p;
  846. int error;
  847. p = xdr_inline_decode(xdr, 4);
  848. if (unlikely(p == NULL))
  849. goto out_overflow;
  850. if (*p++ == xdr_zero) {
  851. p = xdr_inline_decode(xdr, 4);
  852. if (unlikely(p == NULL))
  853. goto out_overflow;
  854. if (*p++ == xdr_zero)
  855. return ERR_PTR(-EAGAIN);
  856. entry->eof = 1;
  857. return ERR_PTR(-EBADCOOKIE);
  858. }
  859. p = xdr_inline_decode(xdr, 4);
  860. if (unlikely(p == NULL))
  861. goto out_overflow;
  862. entry->ino = be32_to_cpup(p);
  863. error = decode_filename_inline(xdr, &entry->name, &entry->len);
  864. if (unlikely(error))
  865. return ERR_PTR(error);
  866. /*
  867. * The type (size and byte order) of nfscookie isn't defined in
  868. * RFC 1094. This implementation assumes that it's an XDR uint32.
  869. */
  870. entry->prev_cookie = entry->cookie;
  871. p = xdr_inline_decode(xdr, 4);
  872. if (unlikely(p == NULL))
  873. goto out_overflow;
  874. entry->cookie = be32_to_cpup(p);
  875. entry->d_type = DT_UNKNOWN;
  876. /* Peek at the next entry to see if we're at EOD */
  877. p = xdr_inline_peek(xdr, 4 + 4);
  878. entry->eof = 0;
  879. if (p != NULL)
  880. entry->eof = (p[0] == xdr_zero) && (p[1] != xdr_zero);
  881. return p;
  882. out_overflow:
  883. print_overflow_msg(__func__, xdr);
  884. return ERR_PTR(-EAGAIN);
  885. }
  886. /*
  887. * 2.2.17. readdirres
  888. *
  889. * union readdirres switch (stat status) {
  890. * case NFS_OK:
  891. * struct {
  892. * entry *entries;
  893. * bool eof;
  894. * } readdirok;
  895. * default:
  896. * void;
  897. * };
  898. *
  899. * Read the directory contents into the page cache, but don't
  900. * touch them. The actual decoding is done by nfs2_decode_dirent()
  901. * during subsequent nfs_readdir() calls.
  902. */
  903. static int decode_readdirok(struct xdr_stream *xdr)
  904. {
  905. u32 recvd, pglen;
  906. size_t hdrlen;
  907. pglen = xdr->buf->page_len;
  908. hdrlen = (u8 *)xdr->p - (u8 *)xdr->iov->iov_base;
  909. recvd = xdr->buf->len - hdrlen;
  910. if (unlikely(pglen > recvd))
  911. goto out_cheating;
  912. out:
  913. xdr_read_pages(xdr, pglen);
  914. return pglen;
  915. out_cheating:
  916. dprintk("NFS: server cheating in readdir result: "
  917. "pglen %u > recvd %u\n", pglen, recvd);
  918. pglen = recvd;
  919. goto out;
  920. }
  921. static int nfs2_xdr_dec_readdirres(struct rpc_rqst *req, __be32 *p,
  922. void *__unused)
  923. {
  924. struct xdr_stream xdr;
  925. enum nfs_stat status;
  926. int error;
  927. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  928. error = decode_stat(&xdr, &status);
  929. if (unlikely(error))
  930. goto out;
  931. if (status != NFS_OK)
  932. goto out_default;
  933. error = decode_readdirok(&xdr);
  934. out:
  935. return error;
  936. out_default:
  937. return nfs_stat_to_errno(status);
  938. }
  939. /*
  940. * 2.2.18. statfsres
  941. *
  942. * union statfsres (stat status) {
  943. * case NFS_OK:
  944. * struct {
  945. * unsigned tsize;
  946. * unsigned bsize;
  947. * unsigned blocks;
  948. * unsigned bfree;
  949. * unsigned bavail;
  950. * } info;
  951. * default:
  952. * void;
  953. * };
  954. */
  955. static int decode_info(struct xdr_stream *xdr, struct nfs2_fsstat *result)
  956. {
  957. __be32 *p;
  958. p = xdr_inline_decode(xdr, NFS_info_sz << 2);
  959. if (unlikely(p == NULL))
  960. goto out_overflow;
  961. result->tsize = be32_to_cpup(p++);
  962. result->bsize = be32_to_cpup(p++);
  963. result->blocks = be32_to_cpup(p++);
  964. result->bfree = be32_to_cpup(p++);
  965. result->bavail = be32_to_cpup(p);
  966. return 0;
  967. out_overflow:
  968. print_overflow_msg(__func__, xdr);
  969. return -EIO;
  970. }
  971. static int nfs2_xdr_dec_statfsres(struct rpc_rqst *req, __be32 *p,
  972. struct nfs2_fsstat *result)
  973. {
  974. struct xdr_stream xdr;
  975. enum nfs_stat status;
  976. int error;
  977. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  978. error = decode_stat(&xdr, &status);
  979. if (unlikely(error))
  980. goto out;
  981. if (status != NFS_OK)
  982. goto out_default;
  983. error = decode_info(&xdr, result);
  984. out:
  985. return error;
  986. out_default:
  987. return nfs_stat_to_errno(status);
  988. }
  989. /*
  990. * We need to translate between nfs status return values and
  991. * the local errno values which may not be the same.
  992. */
  993. static const struct {
  994. int stat;
  995. int errno;
  996. } nfs_errtbl[] = {
  997. { NFS_OK, 0 },
  998. { NFSERR_PERM, -EPERM },
  999. { NFSERR_NOENT, -ENOENT },
  1000. { NFSERR_IO, -errno_NFSERR_IO},
  1001. { NFSERR_NXIO, -ENXIO },
  1002. /* { NFSERR_EAGAIN, -EAGAIN }, */
  1003. { NFSERR_ACCES, -EACCES },
  1004. { NFSERR_EXIST, -EEXIST },
  1005. { NFSERR_XDEV, -EXDEV },
  1006. { NFSERR_NODEV, -ENODEV },
  1007. { NFSERR_NOTDIR, -ENOTDIR },
  1008. { NFSERR_ISDIR, -EISDIR },
  1009. { NFSERR_INVAL, -EINVAL },
  1010. { NFSERR_FBIG, -EFBIG },
  1011. { NFSERR_NOSPC, -ENOSPC },
  1012. { NFSERR_ROFS, -EROFS },
  1013. { NFSERR_MLINK, -EMLINK },
  1014. { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
  1015. { NFSERR_NOTEMPTY, -ENOTEMPTY },
  1016. { NFSERR_DQUOT, -EDQUOT },
  1017. { NFSERR_STALE, -ESTALE },
  1018. { NFSERR_REMOTE, -EREMOTE },
  1019. #ifdef EWFLUSH
  1020. { NFSERR_WFLUSH, -EWFLUSH },
  1021. #endif
  1022. { NFSERR_BADHANDLE, -EBADHANDLE },
  1023. { NFSERR_NOT_SYNC, -ENOTSYNC },
  1024. { NFSERR_BAD_COOKIE, -EBADCOOKIE },
  1025. { NFSERR_NOTSUPP, -ENOTSUPP },
  1026. { NFSERR_TOOSMALL, -ETOOSMALL },
  1027. { NFSERR_SERVERFAULT, -EREMOTEIO },
  1028. { NFSERR_BADTYPE, -EBADTYPE },
  1029. { NFSERR_JUKEBOX, -EJUKEBOX },
  1030. { -1, -EIO }
  1031. };
  1032. /**
  1033. * nfs_stat_to_errno - convert an NFS status code to a local errno
  1034. * @status: NFS status code to convert
  1035. *
  1036. * Returns a local errno value, or -EIO if the NFS status code is
  1037. * not recognized. This function is used jointly by NFSv2 and NFSv3.
  1038. */
  1039. int nfs_stat_to_errno(enum nfs_stat status)
  1040. {
  1041. int i;
  1042. for (i = 0; nfs_errtbl[i].stat != -1; i++) {
  1043. if (nfs_errtbl[i].stat == (int)status)
  1044. return nfs_errtbl[i].errno;
  1045. }
  1046. dprintk("NFS: Unrecognized nfs status value: %u\n", status);
  1047. return nfs_errtbl[i].errno;
  1048. }
  1049. #define PROC(proc, argtype, restype, timer) \
  1050. [NFSPROC_##proc] = { \
  1051. .p_proc = NFSPROC_##proc, \
  1052. .p_encode = (kxdrproc_t)nfs2_xdr_enc_##argtype, \
  1053. .p_decode = (kxdrproc_t)nfs2_xdr_dec_##restype, \
  1054. .p_arglen = NFS_##argtype##_sz, \
  1055. .p_replen = NFS_##restype##_sz, \
  1056. .p_timer = timer, \
  1057. .p_statidx = NFSPROC_##proc, \
  1058. .p_name = #proc, \
  1059. }
  1060. struct rpc_procinfo nfs_procedures[] = {
  1061. PROC(GETATTR, fhandle, attrstat, 1),
  1062. PROC(SETATTR, sattrargs, attrstat, 0),
  1063. PROC(LOOKUP, diropargs, diropres, 2),
  1064. PROC(READLINK, readlinkargs, readlinkres, 3),
  1065. PROC(READ, readargs, readres, 3),
  1066. PROC(WRITE, writeargs, writeres, 4),
  1067. PROC(CREATE, createargs, diropres, 0),
  1068. PROC(REMOVE, removeargs, stat, 0),
  1069. PROC(RENAME, renameargs, stat, 0),
  1070. PROC(LINK, linkargs, stat, 0),
  1071. PROC(SYMLINK, symlinkargs, stat, 0),
  1072. PROC(MKDIR, createargs, diropres, 0),
  1073. PROC(RMDIR, diropargs, stat, 0),
  1074. PROC(READDIR, readdirargs, readdirres, 3),
  1075. PROC(STATFS, fhandle, statfsres, 0),
  1076. };
  1077. struct rpc_version nfs_version2 = {
  1078. .number = 2,
  1079. .nrprocs = ARRAY_SIZE(nfs_procedures),
  1080. .procs = nfs_procedures
  1081. };