nfsxdr.c 13 KB

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