nfs2xdr.c 26 KB

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