nfs2xdr.c 26 KB

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