nfsxdr.c 13 KB

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