nfsxdr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * linux/fs/nfsd/nfsxdr.c
  3. *
  4. * XDR support for nfsd
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/time.h>
  10. #include <linux/nfs.h>
  11. #include <linux/vfs.h>
  12. #include <linux/sunrpc/xdr.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/nfsd/nfsd.h>
  15. #include <linux/nfsd/xdr.h>
  16. #include <linux/mm.h>
  17. #include "auth.h"
  18. #define NFSDDBG_FACILITY NFSDDBG_XDR
  19. /*
  20. * Mapping of S_IF* types to NFS file types
  21. */
  22. static u32 nfs_ftypes[] = {
  23. NFNON, NFCHR, NFCHR, NFBAD,
  24. NFDIR, NFBAD, NFBLK, NFBAD,
  25. NFREG, NFBAD, NFLNK, NFBAD,
  26. NFSOCK, NFBAD, NFLNK, NFBAD,
  27. };
  28. /*
  29. * XDR functions for basic NFS types
  30. */
  31. static __be32 *
  32. decode_fh(__be32 *p, struct svc_fh *fhp)
  33. {
  34. fh_init(fhp, NFS_FHSIZE);
  35. memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
  36. fhp->fh_handle.fh_size = NFS_FHSIZE;
  37. /* FIXME: Look up export pointer here and verify
  38. * Sun Secure RPC if requested */
  39. return p + (NFS_FHSIZE >> 2);
  40. }
  41. /* Helper function for NFSv2 ACL code */
  42. __be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp)
  43. {
  44. return decode_fh(p, fhp);
  45. }
  46. static __be32 *
  47. encode_fh(__be32 *p, struct svc_fh *fhp)
  48. {
  49. memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
  50. return p + (NFS_FHSIZE>> 2);
  51. }
  52. /*
  53. * Decode a file name and make sure that the path contains
  54. * no slashes or null bytes.
  55. */
  56. static __be32 *
  57. decode_filename(__be32 *p, char **namp, unsigned int *lenp)
  58. {
  59. char *name;
  60. unsigned int i;
  61. if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
  62. for (i = 0, name = *namp; i < *lenp; i++, name++) {
  63. if (*name == '\0' || *name == '/')
  64. return NULL;
  65. }
  66. }
  67. return p;
  68. }
  69. static __be32 *
  70. decode_pathname(__be32 *p, char **namp, unsigned int *lenp)
  71. {
  72. char *name;
  73. unsigned int i;
  74. if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) {
  75. for (i = 0, name = *namp; i < *lenp; i++, name++) {
  76. if (*name == '\0')
  77. return NULL;
  78. }
  79. }
  80. return p;
  81. }
  82. static __be32 *
  83. decode_sattr(__be32 *p, struct iattr *iap)
  84. {
  85. u32 tmp, tmp1;
  86. iap->ia_valid = 0;
  87. /* Sun client bug compatibility check: some sun clients seem to
  88. * put 0xffff in the mode field when they mean 0xffffffff.
  89. * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah.
  90. */
  91. if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
  92. iap->ia_valid |= ATTR_MODE;
  93. iap->ia_mode = tmp;
  94. }
  95. if ((tmp = ntohl(*p++)) != (u32)-1) {
  96. iap->ia_valid |= ATTR_UID;
  97. iap->ia_uid = tmp;
  98. }
  99. if ((tmp = ntohl(*p++)) != (u32)-1) {
  100. iap->ia_valid |= ATTR_GID;
  101. iap->ia_gid = tmp;
  102. }
  103. if ((tmp = ntohl(*p++)) != (u32)-1) {
  104. iap->ia_valid |= ATTR_SIZE;
  105. iap->ia_size = tmp;
  106. }
  107. tmp = ntohl(*p++); tmp1 = ntohl(*p++);
  108. if (tmp != (u32)-1 && tmp1 != (u32)-1) {
  109. iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
  110. iap->ia_atime.tv_sec = tmp;
  111. iap->ia_atime.tv_nsec = tmp1 * 1000;
  112. }
  113. tmp = ntohl(*p++); tmp1 = ntohl(*p++);
  114. if (tmp != (u32)-1 && tmp1 != (u32)-1) {
  115. iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
  116. iap->ia_mtime.tv_sec = tmp;
  117. iap->ia_mtime.tv_nsec = tmp1 * 1000;
  118. /*
  119. * Passing the invalid value useconds=1000000 for mtime
  120. * is a Sun convention for "set both mtime and atime to
  121. * current server time". It's needed to make permissions
  122. * checks for the "touch" program across v2 mounts to
  123. * Solaris and Irix boxes work correctly. See description of
  124. * sattr in section 6.1 of "NFS Illustrated" by
  125. * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
  126. */
  127. if (tmp1 == 1000000)
  128. iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
  129. }
  130. return p;
  131. }
  132. static __be32 *
  133. encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
  134. struct kstat *stat)
  135. {
  136. struct dentry *dentry = fhp->fh_dentry;
  137. int type;
  138. struct timespec time;
  139. u32 f;
  140. type = (stat->mode & S_IFMT);
  141. *p++ = htonl(nfs_ftypes[type >> 12]);
  142. *p++ = htonl((u32) stat->mode);
  143. *p++ = htonl((u32) stat->nlink);
  144. *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
  145. *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
  146. if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) {
  147. *p++ = htonl(NFS_MAXPATHLEN);
  148. } else {
  149. *p++ = htonl((u32) stat->size);
  150. }
  151. *p++ = htonl((u32) stat->blksize);
  152. if (S_ISCHR(type) || S_ISBLK(type))
  153. *p++ = htonl(new_encode_dev(stat->rdev));
  154. else
  155. *p++ = htonl(0xffffffff);
  156. *p++ = htonl((u32) stat->blocks);
  157. switch (fsid_source(fhp)) {
  158. default:
  159. case FSIDSOURCE_DEV:
  160. *p++ = htonl(new_encode_dev(stat->dev));
  161. break;
  162. case FSIDSOURCE_FSID:
  163. *p++ = htonl((u32) fhp->fh_export->ex_fsid);
  164. break;
  165. case FSIDSOURCE_UUID:
  166. f = ((u32*)fhp->fh_export->ex_uuid)[0];
  167. f ^= ((u32*)fhp->fh_export->ex_uuid)[1];
  168. f ^= ((u32*)fhp->fh_export->ex_uuid)[2];
  169. f ^= ((u32*)fhp->fh_export->ex_uuid)[3];
  170. *p++ = htonl(f);
  171. break;
  172. }
  173. *p++ = htonl((u32) stat->ino);
  174. *p++ = htonl((u32) stat->atime.tv_sec);
  175. *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0);
  176. lease_get_mtime(dentry->d_inode, &time);
  177. *p++ = htonl((u32) time.tv_sec);
  178. *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0);
  179. *p++ = htonl((u32) stat->ctime.tv_sec);
  180. *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0);
  181. return p;
  182. }
  183. /* Helper function for NFSv2 ACL code */
  184. __be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  185. {
  186. struct kstat stat;
  187. vfs_getattr(fhp->fh_export->ex_path.mnt, fhp->fh_dentry, &stat);
  188. return encode_fattr(rqstp, p, fhp, &stat);
  189. }
  190. /*
  191. * XDR decode functions
  192. */
  193. int
  194. nfssvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  195. {
  196. return xdr_argsize_check(rqstp, p);
  197. }
  198. int
  199. nfssvc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
  200. {
  201. if (!(p = decode_fh(p, &args->fh)))
  202. return 0;
  203. return xdr_argsize_check(rqstp, p);
  204. }
  205. int
  206. nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
  207. struct nfsd_sattrargs *args)
  208. {
  209. p = decode_fh(p, &args->fh);
  210. if (!p)
  211. return 0;
  212. p = decode_sattr(p, &args->attrs);
  213. return xdr_argsize_check(rqstp, p);
  214. }
  215. int
  216. nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
  217. struct nfsd_diropargs *args)
  218. {
  219. if (!(p = decode_fh(p, &args->fh))
  220. || !(p = decode_filename(p, &args->name, &args->len)))
  221. return 0;
  222. return xdr_argsize_check(rqstp, p);
  223. }
  224. int
  225. nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
  226. struct nfsd_readargs *args)
  227. {
  228. unsigned int len;
  229. int v,pn;
  230. if (!(p = decode_fh(p, &args->fh)))
  231. return 0;
  232. args->offset = ntohl(*p++);
  233. len = args->count = ntohl(*p++);
  234. p++; /* totalcount - unused */
  235. if (len > NFSSVC_MAXBLKSIZE_V2)
  236. len = NFSSVC_MAXBLKSIZE_V2;
  237. /* set up somewhere to store response.
  238. * We take pages, put them on reslist and include in iovec
  239. */
  240. v=0;
  241. while (len > 0) {
  242. pn = rqstp->rq_resused++;
  243. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
  244. rqstp->rq_vec[v].iov_len = len < PAGE_SIZE?len:PAGE_SIZE;
  245. len -= rqstp->rq_vec[v].iov_len;
  246. v++;
  247. }
  248. args->vlen = v;
  249. return xdr_argsize_check(rqstp, p);
  250. }
  251. int
  252. nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
  253. struct nfsd_writeargs *args)
  254. {
  255. unsigned int len, hdr, dlen;
  256. int v;
  257. if (!(p = decode_fh(p, &args->fh)))
  258. return 0;
  259. p++; /* beginoffset */
  260. args->offset = ntohl(*p++); /* offset */
  261. p++; /* totalcount */
  262. len = args->len = ntohl(*p++);
  263. /*
  264. * The protocol specifies a maximum of 8192 bytes.
  265. */
  266. if (len > NFSSVC_MAXBLKSIZE_V2)
  267. return 0;
  268. /*
  269. * Check to make sure that we got the right number of
  270. * bytes.
  271. */
  272. hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
  273. dlen = rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len
  274. - hdr;
  275. /*
  276. * Round the length of the data which was specified up to
  277. * the next multiple of XDR units and then compare that
  278. * against the length which was actually received.
  279. * Note that when RPCSEC/GSS (for example) is used, the
  280. * data buffer can be padded so dlen might be larger
  281. * than required. It must never be smaller.
  282. */
  283. if (dlen < XDR_QUADLEN(len)*4)
  284. return 0;
  285. rqstp->rq_vec[0].iov_base = (void*)p;
  286. rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
  287. v = 0;
  288. while (len > rqstp->rq_vec[v].iov_len) {
  289. len -= rqstp->rq_vec[v].iov_len;
  290. v++;
  291. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
  292. rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  293. }
  294. rqstp->rq_vec[v].iov_len = len;
  295. args->vlen = v + 1;
  296. return 1;
  297. }
  298. int
  299. nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
  300. struct nfsd_createargs *args)
  301. {
  302. if ( !(p = decode_fh(p, &args->fh))
  303. || !(p = decode_filename(p, &args->name, &args->len)))
  304. return 0;
  305. p = decode_sattr(p, &args->attrs);
  306. return xdr_argsize_check(rqstp, p);
  307. }
  308. int
  309. nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
  310. struct nfsd_renameargs *args)
  311. {
  312. if (!(p = decode_fh(p, &args->ffh))
  313. || !(p = decode_filename(p, &args->fname, &args->flen))
  314. || !(p = decode_fh(p, &args->tfh))
  315. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  316. return 0;
  317. return xdr_argsize_check(rqstp, p);
  318. }
  319. int
  320. nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd_readlinkargs *args)
  321. {
  322. if (!(p = decode_fh(p, &args->fh)))
  323. return 0;
  324. args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  325. return xdr_argsize_check(rqstp, p);
  326. }
  327. int
  328. nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
  329. struct nfsd_linkargs *args)
  330. {
  331. if (!(p = decode_fh(p, &args->ffh))
  332. || !(p = decode_fh(p, &args->tfh))
  333. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  334. return 0;
  335. return xdr_argsize_check(rqstp, p);
  336. }
  337. int
  338. nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
  339. struct nfsd_symlinkargs *args)
  340. {
  341. if ( !(p = decode_fh(p, &args->ffh))
  342. || !(p = decode_filename(p, &args->fname, &args->flen))
  343. || !(p = decode_pathname(p, &args->tname, &args->tlen)))
  344. return 0;
  345. p = decode_sattr(p, &args->attrs);
  346. return xdr_argsize_check(rqstp, p);
  347. }
  348. int
  349. nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
  350. struct nfsd_readdirargs *args)
  351. {
  352. if (!(p = decode_fh(p, &args->fh)))
  353. return 0;
  354. args->cookie = ntohl(*p++);
  355. args->count = ntohl(*p++);
  356. if (args->count > PAGE_SIZE)
  357. args->count = PAGE_SIZE;
  358. args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  359. return xdr_argsize_check(rqstp, p);
  360. }
  361. /*
  362. * XDR encode functions
  363. */
  364. int
  365. nfssvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  366. {
  367. return xdr_ressize_check(rqstp, p);
  368. }
  369. int
  370. nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
  371. struct nfsd_attrstat *resp)
  372. {
  373. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  374. return xdr_ressize_check(rqstp, p);
  375. }
  376. int
  377. nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
  378. struct nfsd_diropres *resp)
  379. {
  380. p = encode_fh(p, &resp->fh);
  381. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  382. return xdr_ressize_check(rqstp, p);
  383. }
  384. int
  385. nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
  386. struct nfsd_readlinkres *resp)
  387. {
  388. *p++ = htonl(resp->len);
  389. xdr_ressize_check(rqstp, p);
  390. rqstp->rq_res.page_len = resp->len;
  391. if (resp->len & 3) {
  392. /* need to pad the tail */
  393. rqstp->rq_res.tail[0].iov_base = p;
  394. *p = 0;
  395. rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
  396. }
  397. return 1;
  398. }
  399. int
  400. nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
  401. struct nfsd_readres *resp)
  402. {
  403. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  404. *p++ = htonl(resp->count);
  405. xdr_ressize_check(rqstp, p);
  406. /* now update rqstp->rq_res to reflect data aswell */
  407. rqstp->rq_res.page_len = resp->count;
  408. if (resp->count & 3) {
  409. /* need to pad the tail */
  410. rqstp->rq_res.tail[0].iov_base = p;
  411. *p = 0;
  412. rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
  413. }
  414. return 1;
  415. }
  416. int
  417. nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
  418. struct nfsd_readdirres *resp)
  419. {
  420. xdr_ressize_check(rqstp, p);
  421. p = resp->buffer;
  422. *p++ = 0; /* no more entries */
  423. *p++ = htonl((resp->common.err == nfserr_eof));
  424. rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
  425. return 1;
  426. }
  427. int
  428. nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p,
  429. struct nfsd_statfsres *resp)
  430. {
  431. struct kstatfs *stat = &resp->stats;
  432. *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */
  433. *p++ = htonl(stat->f_bsize);
  434. *p++ = htonl(stat->f_blocks);
  435. *p++ = htonl(stat->f_bfree);
  436. *p++ = htonl(stat->f_bavail);
  437. return xdr_ressize_check(rqstp, p);
  438. }
  439. int
  440. nfssvc_encode_entry(void *ccdv, const char *name,
  441. int namlen, loff_t offset, u64 ino, unsigned int d_type)
  442. {
  443. struct readdir_cd *ccd = ccdv;
  444. struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
  445. __be32 *p = cd->buffer;
  446. int buflen, slen;
  447. /*
  448. dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
  449. namlen, name, offset, ino);
  450. */
  451. if (offset > ~((u32) 0)) {
  452. cd->common.err = nfserr_fbig;
  453. return -EINVAL;
  454. }
  455. if (cd->offset)
  456. *cd->offset = htonl(offset);
  457. if (namlen > NFS2_MAXNAMLEN)
  458. namlen = NFS2_MAXNAMLEN;/* truncate filename */
  459. slen = XDR_QUADLEN(namlen);
  460. if ((buflen = cd->buflen - slen - 4) < 0) {
  461. cd->common.err = nfserr_toosmall;
  462. return -EINVAL;
  463. }
  464. if (ino > ~((u32) 0)) {
  465. cd->common.err = nfserr_fbig;
  466. return -EINVAL;
  467. }
  468. *p++ = xdr_one; /* mark entry present */
  469. *p++ = htonl((u32) ino); /* file id */
  470. p = xdr_encode_array(p, name, namlen);/* name length & name */
  471. cd->offset = p; /* remember pointer */
  472. *p++ = htonl(~0U); /* offset of next entry */
  473. cd->buflen = buflen;
  474. cd->buffer = p;
  475. cd->common.err = nfs_ok;
  476. return 0;
  477. }
  478. /*
  479. * XDR release functions
  480. */
  481. int
  482. nfssvc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
  483. struct nfsd_fhandle *resp)
  484. {
  485. fh_put(&resp->fh);
  486. return 1;
  487. }