nfsxdr.c 12 KB

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