nfsxdr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. if (!(p = decode_fh(p, &args->fh))
  209. || !(p = decode_sattr(p, &args->attrs)))
  210. return 0;
  211. return xdr_argsize_check(rqstp, p);
  212. }
  213. int
  214. nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
  215. struct nfsd_diropargs *args)
  216. {
  217. if (!(p = decode_fh(p, &args->fh))
  218. || !(p = decode_filename(p, &args->name, &args->len)))
  219. return 0;
  220. return xdr_argsize_check(rqstp, p);
  221. }
  222. int
  223. nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
  224. struct nfsd_readargs *args)
  225. {
  226. unsigned int len;
  227. int v,pn;
  228. if (!(p = decode_fh(p, &args->fh)))
  229. return 0;
  230. args->offset = ntohl(*p++);
  231. len = args->count = ntohl(*p++);
  232. p++; /* totalcount - unused */
  233. if (len > NFSSVC_MAXBLKSIZE_V2)
  234. len = NFSSVC_MAXBLKSIZE_V2;
  235. /* set up somewhere to store response.
  236. * We take pages, put them on reslist and include in iovec
  237. */
  238. v=0;
  239. while (len > 0) {
  240. pn = rqstp->rq_resused++;
  241. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
  242. rqstp->rq_vec[v].iov_len = len < PAGE_SIZE?len:PAGE_SIZE;
  243. len -= rqstp->rq_vec[v].iov_len;
  244. v++;
  245. }
  246. args->vlen = v;
  247. return xdr_argsize_check(rqstp, p);
  248. }
  249. int
  250. nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
  251. struct nfsd_writeargs *args)
  252. {
  253. unsigned int len;
  254. int v;
  255. if (!(p = decode_fh(p, &args->fh)))
  256. return 0;
  257. p++; /* beginoffset */
  258. args->offset = ntohl(*p++); /* offset */
  259. p++; /* totalcount */
  260. len = args->len = ntohl(*p++);
  261. rqstp->rq_vec[0].iov_base = (void*)p;
  262. rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len -
  263. (((void*)p) - rqstp->rq_arg.head[0].iov_base);
  264. if (len > NFSSVC_MAXBLKSIZE_V2)
  265. len = NFSSVC_MAXBLKSIZE_V2;
  266. v = 0;
  267. while (len > rqstp->rq_vec[v].iov_len) {
  268. len -= rqstp->rq_vec[v].iov_len;
  269. v++;
  270. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
  271. rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  272. }
  273. rqstp->rq_vec[v].iov_len = len;
  274. args->vlen = v+1;
  275. return rqstp->rq_vec[0].iov_len > 0;
  276. }
  277. int
  278. nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
  279. struct nfsd_createargs *args)
  280. {
  281. if (!(p = decode_fh(p, &args->fh))
  282. || !(p = decode_filename(p, &args->name, &args->len))
  283. || !(p = decode_sattr(p, &args->attrs)))
  284. return 0;
  285. return xdr_argsize_check(rqstp, p);
  286. }
  287. int
  288. nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
  289. struct nfsd_renameargs *args)
  290. {
  291. if (!(p = decode_fh(p, &args->ffh))
  292. || !(p = decode_filename(p, &args->fname, &args->flen))
  293. || !(p = decode_fh(p, &args->tfh))
  294. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  295. return 0;
  296. return xdr_argsize_check(rqstp, p);
  297. }
  298. int
  299. nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd_readlinkargs *args)
  300. {
  301. if (!(p = decode_fh(p, &args->fh)))
  302. return 0;
  303. args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  304. return xdr_argsize_check(rqstp, p);
  305. }
  306. int
  307. nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
  308. struct nfsd_linkargs *args)
  309. {
  310. if (!(p = decode_fh(p, &args->ffh))
  311. || !(p = decode_fh(p, &args->tfh))
  312. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  313. return 0;
  314. return xdr_argsize_check(rqstp, p);
  315. }
  316. int
  317. nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
  318. struct nfsd_symlinkargs *args)
  319. {
  320. if (!(p = decode_fh(p, &args->ffh))
  321. || !(p = decode_filename(p, &args->fname, &args->flen))
  322. || !(p = decode_pathname(p, &args->tname, &args->tlen))
  323. || !(p = decode_sattr(p, &args->attrs)))
  324. return 0;
  325. return xdr_argsize_check(rqstp, p);
  326. }
  327. int
  328. nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
  329. struct nfsd_readdirargs *args)
  330. {
  331. if (!(p = decode_fh(p, &args->fh)))
  332. return 0;
  333. args->cookie = ntohl(*p++);
  334. args->count = ntohl(*p++);
  335. if (args->count > PAGE_SIZE)
  336. args->count = PAGE_SIZE;
  337. args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  338. return xdr_argsize_check(rqstp, p);
  339. }
  340. /*
  341. * XDR encode functions
  342. */
  343. int
  344. nfssvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  345. {
  346. return xdr_ressize_check(rqstp, p);
  347. }
  348. int
  349. nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
  350. struct nfsd_attrstat *resp)
  351. {
  352. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  353. return xdr_ressize_check(rqstp, p);
  354. }
  355. int
  356. nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
  357. struct nfsd_diropres *resp)
  358. {
  359. p = encode_fh(p, &resp->fh);
  360. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  361. return xdr_ressize_check(rqstp, p);
  362. }
  363. int
  364. nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
  365. struct nfsd_readlinkres *resp)
  366. {
  367. *p++ = htonl(resp->len);
  368. xdr_ressize_check(rqstp, p);
  369. rqstp->rq_res.page_len = resp->len;
  370. if (resp->len & 3) {
  371. /* need to pad the tail */
  372. rqstp->rq_res.tail[0].iov_base = p;
  373. *p = 0;
  374. rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
  375. }
  376. return 1;
  377. }
  378. int
  379. nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
  380. struct nfsd_readres *resp)
  381. {
  382. p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
  383. *p++ = htonl(resp->count);
  384. xdr_ressize_check(rqstp, p);
  385. /* now update rqstp->rq_res to reflect data aswell */
  386. rqstp->rq_res.page_len = resp->count;
  387. if (resp->count & 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->count&3);
  392. }
  393. return 1;
  394. }
  395. int
  396. nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
  397. struct nfsd_readdirres *resp)
  398. {
  399. xdr_ressize_check(rqstp, p);
  400. p = resp->buffer;
  401. *p++ = 0; /* no more entries */
  402. *p++ = htonl((resp->common.err == nfserr_eof));
  403. rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
  404. return 1;
  405. }
  406. int
  407. nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p,
  408. struct nfsd_statfsres *resp)
  409. {
  410. struct kstatfs *stat = &resp->stats;
  411. *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */
  412. *p++ = htonl(stat->f_bsize);
  413. *p++ = htonl(stat->f_blocks);
  414. *p++ = htonl(stat->f_bfree);
  415. *p++ = htonl(stat->f_bavail);
  416. return xdr_ressize_check(rqstp, p);
  417. }
  418. int
  419. nfssvc_encode_entry(void *ccdv, const char *name,
  420. int namlen, loff_t offset, u64 ino, unsigned int d_type)
  421. {
  422. struct readdir_cd *ccd = ccdv;
  423. struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
  424. __be32 *p = cd->buffer;
  425. int buflen, slen;
  426. /*
  427. dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
  428. namlen, name, offset, ino);
  429. */
  430. if (offset > ~((u32) 0)) {
  431. cd->common.err = nfserr_fbig;
  432. return -EINVAL;
  433. }
  434. if (cd->offset)
  435. *cd->offset = htonl(offset);
  436. if (namlen > NFS2_MAXNAMLEN)
  437. namlen = NFS2_MAXNAMLEN;/* truncate filename */
  438. slen = XDR_QUADLEN(namlen);
  439. if ((buflen = cd->buflen - slen - 4) < 0) {
  440. cd->common.err = nfserr_toosmall;
  441. return -EINVAL;
  442. }
  443. *p++ = xdr_one; /* mark entry present */
  444. *p++ = htonl((u32) ino); /* file id */
  445. p = xdr_encode_array(p, name, namlen);/* name length & name */
  446. cd->offset = p; /* remember pointer */
  447. *p++ = htonl(~0U); /* offset of next entry */
  448. cd->buflen = buflen;
  449. cd->buffer = p;
  450. cd->common.err = nfs_ok;
  451. return 0;
  452. }
  453. /*
  454. * XDR release functions
  455. */
  456. int
  457. nfssvc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
  458. struct nfsd_fhandle *resp)
  459. {
  460. fh_put(&resp->fh);
  461. return 1;
  462. }