nfsxdr.c 13 KB

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