nfs2xdr.c 26 KB

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