nfs2xdr.c 26 KB

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