nfsxdr.c 12 KB

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