nfs2xdr.c 24 KB

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