nfs3xdr.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. * linux/fs/nfsd/nfs3xdr.c
  3. *
  4. * XDR support for nfsd/protocol version 3.
  5. *
  6. * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
  7. *
  8. * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
  9. */
  10. #include <linux/types.h>
  11. #include <linux/time.h>
  12. #include <linux/nfs3.h>
  13. #include <linux/list.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/dcache.h>
  16. #include <linux/namei.h>
  17. #include <linux/mm.h>
  18. #include <linux/vfs.h>
  19. #include <linux/sunrpc/xdr.h>
  20. #include <linux/sunrpc/svc.h>
  21. #include <linux/nfsd/nfsd.h>
  22. #include <linux/nfsd/xdr3.h>
  23. #define NFSDDBG_FACILITY NFSDDBG_XDR
  24. /*
  25. * Mapping of S_IF* types to NFS file types
  26. */
  27. static u32 nfs3_ftypes[] = {
  28. NF3NON, NF3FIFO, NF3CHR, NF3BAD,
  29. NF3DIR, NF3BAD, NF3BLK, NF3BAD,
  30. NF3REG, NF3BAD, NF3LNK, NF3BAD,
  31. NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
  32. };
  33. /*
  34. * XDR functions for basic NFS types
  35. */
  36. static __be32 *
  37. encode_time3(__be32 *p, struct timespec *time)
  38. {
  39. *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
  40. return p;
  41. }
  42. static __be32 *
  43. decode_time3(__be32 *p, struct timespec *time)
  44. {
  45. time->tv_sec = ntohl(*p++);
  46. time->tv_nsec = ntohl(*p++);
  47. return p;
  48. }
  49. static __be32 *
  50. decode_fh(__be32 *p, struct svc_fh *fhp)
  51. {
  52. unsigned int size;
  53. fh_init(fhp, NFS3_FHSIZE);
  54. size = ntohl(*p++);
  55. if (size > NFS3_FHSIZE)
  56. return NULL;
  57. memcpy(&fhp->fh_handle.fh_base, p, size);
  58. fhp->fh_handle.fh_size = size;
  59. return p + XDR_QUADLEN(size);
  60. }
  61. /* Helper function for NFSv3 ACL code */
  62. __be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
  63. {
  64. return decode_fh(p, fhp);
  65. }
  66. static __be32 *
  67. encode_fh(__be32 *p, struct svc_fh *fhp)
  68. {
  69. unsigned int size = fhp->fh_handle.fh_size;
  70. *p++ = htonl(size);
  71. if (size) p[XDR_QUADLEN(size)-1]=0;
  72. memcpy(p, &fhp->fh_handle.fh_base, size);
  73. return p + XDR_QUADLEN(size);
  74. }
  75. /*
  76. * Decode a file name and make sure that the path contains
  77. * no slashes or null bytes.
  78. */
  79. static __be32 *
  80. decode_filename(__be32 *p, char **namp, int *lenp)
  81. {
  82. char *name;
  83. int i;
  84. if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
  85. for (i = 0, name = *namp; i < *lenp; i++, name++) {
  86. if (*name == '\0' || *name == '/')
  87. return NULL;
  88. }
  89. }
  90. return p;
  91. }
  92. static __be32 *
  93. decode_sattr3(__be32 *p, struct iattr *iap)
  94. {
  95. u32 tmp;
  96. iap->ia_valid = 0;
  97. if (*p++) {
  98. iap->ia_valid |= ATTR_MODE;
  99. iap->ia_mode = ntohl(*p++);
  100. }
  101. if (*p++) {
  102. iap->ia_valid |= ATTR_UID;
  103. iap->ia_uid = ntohl(*p++);
  104. }
  105. if (*p++) {
  106. iap->ia_valid |= ATTR_GID;
  107. iap->ia_gid = ntohl(*p++);
  108. }
  109. if (*p++) {
  110. u64 newsize;
  111. iap->ia_valid |= ATTR_SIZE;
  112. p = xdr_decode_hyper(p, &newsize);
  113. if (newsize <= NFS_OFFSET_MAX)
  114. iap->ia_size = newsize;
  115. else
  116. iap->ia_size = NFS_OFFSET_MAX;
  117. }
  118. if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
  119. iap->ia_valid |= ATTR_ATIME;
  120. } else if (tmp == 2) { /* set to client time */
  121. iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
  122. iap->ia_atime.tv_sec = ntohl(*p++);
  123. iap->ia_atime.tv_nsec = ntohl(*p++);
  124. }
  125. if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
  126. iap->ia_valid |= ATTR_MTIME;
  127. } else if (tmp == 2) { /* set to client time */
  128. iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
  129. iap->ia_mtime.tv_sec = ntohl(*p++);
  130. iap->ia_mtime.tv_nsec = ntohl(*p++);
  131. }
  132. return p;
  133. }
  134. static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
  135. {
  136. u64 f;
  137. switch(fsid_source(fhp)) {
  138. default:
  139. case FSIDSOURCE_DEV:
  140. p = xdr_encode_hyper(p, (u64)huge_encode_dev
  141. (fhp->fh_dentry->d_inode->i_sb->s_dev));
  142. break;
  143. case FSIDSOURCE_FSID:
  144. p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
  145. break;
  146. case FSIDSOURCE_UUID:
  147. f = ((u64*)fhp->fh_export->ex_uuid)[0];
  148. f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
  149. p = xdr_encode_hyper(p, f);
  150. break;
  151. }
  152. return p;
  153. }
  154. static __be32 *
  155. encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
  156. struct kstat *stat)
  157. {
  158. struct dentry *dentry = fhp->fh_dentry;
  159. struct timespec time;
  160. *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
  161. *p++ = htonl((u32) stat->mode);
  162. *p++ = htonl((u32) stat->nlink);
  163. *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
  164. *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
  165. if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
  166. p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
  167. } else {
  168. p = xdr_encode_hyper(p, (u64) stat->size);
  169. }
  170. p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
  171. *p++ = htonl((u32) MAJOR(stat->rdev));
  172. *p++ = htonl((u32) MINOR(stat->rdev));
  173. p = encode_fsid(p, fhp);
  174. p = xdr_encode_hyper(p, (u64) stat->ino);
  175. p = encode_time3(p, &stat->atime);
  176. lease_get_mtime(dentry->d_inode, &time);
  177. p = encode_time3(p, &time);
  178. p = encode_time3(p, &stat->ctime);
  179. return p;
  180. }
  181. static __be32 *
  182. encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  183. {
  184. struct inode *inode = fhp->fh_dentry->d_inode;
  185. /* Attributes to follow */
  186. *p++ = xdr_one;
  187. *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
  188. *p++ = htonl((u32) fhp->fh_post_mode);
  189. *p++ = htonl((u32) fhp->fh_post_nlink);
  190. *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
  191. *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
  192. if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
  193. p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
  194. } else {
  195. p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
  196. }
  197. p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
  198. *p++ = fhp->fh_post_rdev[0];
  199. *p++ = fhp->fh_post_rdev[1];
  200. p = encode_fsid(p, fhp);
  201. p = xdr_encode_hyper(p, (u64) inode->i_ino);
  202. p = encode_time3(p, &fhp->fh_post_atime);
  203. p = encode_time3(p, &fhp->fh_post_mtime);
  204. p = encode_time3(p, &fhp->fh_post_ctime);
  205. return p;
  206. }
  207. /*
  208. * Encode post-operation attributes.
  209. * The inode may be NULL if the call failed because of a stale file
  210. * handle. In this case, no attributes are returned.
  211. */
  212. static __be32 *
  213. encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  214. {
  215. struct dentry *dentry = fhp->fh_dentry;
  216. if (dentry && dentry->d_inode) {
  217. int err;
  218. struct kstat stat;
  219. err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
  220. if (!err) {
  221. *p++ = xdr_one; /* attributes follow */
  222. return encode_fattr3(rqstp, p, fhp, &stat);
  223. }
  224. }
  225. *p++ = xdr_zero;
  226. return p;
  227. }
  228. /* Helper for NFSv3 ACLs */
  229. __be32 *
  230. nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  231. {
  232. return encode_post_op_attr(rqstp, p, fhp);
  233. }
  234. /*
  235. * Enocde weak cache consistency data
  236. */
  237. static __be32 *
  238. encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  239. {
  240. struct dentry *dentry = fhp->fh_dentry;
  241. if (dentry && dentry->d_inode && fhp->fh_post_saved) {
  242. if (fhp->fh_pre_saved) {
  243. *p++ = xdr_one;
  244. p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
  245. p = encode_time3(p, &fhp->fh_pre_mtime);
  246. p = encode_time3(p, &fhp->fh_pre_ctime);
  247. } else {
  248. *p++ = xdr_zero;
  249. }
  250. return encode_saved_post_attr(rqstp, p, fhp);
  251. }
  252. /* no pre- or post-attrs */
  253. *p++ = xdr_zero;
  254. return encode_post_op_attr(rqstp, p, fhp);
  255. }
  256. /*
  257. * XDR decode functions
  258. */
  259. int
  260. nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
  261. {
  262. if (!(p = decode_fh(p, &args->fh)))
  263. return 0;
  264. return xdr_argsize_check(rqstp, p);
  265. }
  266. int
  267. nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
  268. struct nfsd3_sattrargs *args)
  269. {
  270. if (!(p = decode_fh(p, &args->fh)))
  271. return 0;
  272. p = decode_sattr3(p, &args->attrs);
  273. if ((args->check_guard = ntohl(*p++)) != 0) {
  274. struct timespec time;
  275. p = decode_time3(p, &time);
  276. args->guardtime = time.tv_sec;
  277. }
  278. return xdr_argsize_check(rqstp, p);
  279. }
  280. int
  281. nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
  282. struct nfsd3_diropargs *args)
  283. {
  284. if (!(p = decode_fh(p, &args->fh))
  285. || !(p = decode_filename(p, &args->name, &args->len)))
  286. return 0;
  287. return xdr_argsize_check(rqstp, p);
  288. }
  289. int
  290. nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
  291. struct nfsd3_accessargs *args)
  292. {
  293. if (!(p = decode_fh(p, &args->fh)))
  294. return 0;
  295. args->access = ntohl(*p++);
  296. return xdr_argsize_check(rqstp, p);
  297. }
  298. int
  299. nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
  300. struct nfsd3_readargs *args)
  301. {
  302. unsigned int len;
  303. int v,pn;
  304. u32 max_blocksize = svc_max_payload(rqstp);
  305. if (!(p = decode_fh(p, &args->fh)))
  306. return 0;
  307. p = xdr_decode_hyper(p, &args->offset);
  308. len = args->count = ntohl(*p++);
  309. if (len > max_blocksize)
  310. len = max_blocksize;
  311. /* set up the kvec */
  312. v=0;
  313. while (len > 0) {
  314. pn = rqstp->rq_resused++;
  315. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
  316. rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
  317. len -= rqstp->rq_vec[v].iov_len;
  318. v++;
  319. }
  320. args->vlen = v;
  321. return xdr_argsize_check(rqstp, p);
  322. }
  323. int
  324. nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
  325. struct nfsd3_writeargs *args)
  326. {
  327. unsigned int len, v, hdr, dlen;
  328. u32 max_blocksize = svc_max_payload(rqstp);
  329. if (!(p = decode_fh(p, &args->fh)))
  330. return 0;
  331. p = xdr_decode_hyper(p, &args->offset);
  332. args->count = ntohl(*p++);
  333. args->stable = ntohl(*p++);
  334. len = args->len = ntohl(*p++);
  335. /*
  336. * The count must equal the amount of data passed.
  337. */
  338. if (args->count != args->len)
  339. return 0;
  340. /*
  341. * Check to make sure that we got the right number of
  342. * bytes.
  343. */
  344. hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
  345. dlen = rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len
  346. - hdr;
  347. /*
  348. * Round the length of the data which was specified up to
  349. * the next multiple of XDR units and then compare that
  350. * against the length which was actually received.
  351. */
  352. if (dlen != XDR_QUADLEN(len)*4)
  353. return 0;
  354. if (args->count > max_blocksize) {
  355. args->count = max_blocksize;
  356. len = args->len = max_blocksize;
  357. }
  358. rqstp->rq_vec[0].iov_base = (void*)p;
  359. rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
  360. v = 0;
  361. while (len > rqstp->rq_vec[v].iov_len) {
  362. len -= rqstp->rq_vec[v].iov_len;
  363. v++;
  364. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
  365. rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  366. }
  367. rqstp->rq_vec[v].iov_len = len;
  368. args->vlen = v + 1;
  369. return 1;
  370. }
  371. int
  372. nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
  373. struct nfsd3_createargs *args)
  374. {
  375. if (!(p = decode_fh(p, &args->fh))
  376. || !(p = decode_filename(p, &args->name, &args->len)))
  377. return 0;
  378. switch (args->createmode = ntohl(*p++)) {
  379. case NFS3_CREATE_UNCHECKED:
  380. case NFS3_CREATE_GUARDED:
  381. p = decode_sattr3(p, &args->attrs);
  382. break;
  383. case NFS3_CREATE_EXCLUSIVE:
  384. args->verf = p;
  385. p += 2;
  386. break;
  387. default:
  388. return 0;
  389. }
  390. return xdr_argsize_check(rqstp, p);
  391. }
  392. int
  393. nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
  394. struct nfsd3_createargs *args)
  395. {
  396. if (!(p = decode_fh(p, &args->fh)) ||
  397. !(p = decode_filename(p, &args->name, &args->len)))
  398. return 0;
  399. p = decode_sattr3(p, &args->attrs);
  400. return xdr_argsize_check(rqstp, p);
  401. }
  402. int
  403. nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
  404. struct nfsd3_symlinkargs *args)
  405. {
  406. unsigned int len;
  407. int avail;
  408. char *old, *new;
  409. struct kvec *vec;
  410. if (!(p = decode_fh(p, &args->ffh)) ||
  411. !(p = decode_filename(p, &args->fname, &args->flen))
  412. )
  413. return 0;
  414. p = decode_sattr3(p, &args->attrs);
  415. /* now decode the pathname, which might be larger than the first page.
  416. * As we have to check for nul's anyway, we copy it into a new page
  417. * This page appears in the rq_res.pages list, but as pages_len is always
  418. * 0, it won't get in the way
  419. */
  420. len = ntohl(*p++);
  421. if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
  422. return 0;
  423. args->tname = new =
  424. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  425. args->tlen = len;
  426. /* first copy and check from the first page */
  427. old = (char*)p;
  428. vec = &rqstp->rq_arg.head[0];
  429. avail = vec->iov_len - (old - (char*)vec->iov_base);
  430. while (len && avail && *old) {
  431. *new++ = *old++;
  432. len--;
  433. avail--;
  434. }
  435. /* now copy next page if there is one */
  436. if (len && !avail && rqstp->rq_arg.page_len) {
  437. avail = rqstp->rq_arg.page_len;
  438. if (avail > PAGE_SIZE) avail = PAGE_SIZE;
  439. old = page_address(rqstp->rq_arg.pages[0]);
  440. }
  441. while (len && avail && *old) {
  442. *new++ = *old++;
  443. len--;
  444. avail--;
  445. }
  446. *new = '\0';
  447. if (len)
  448. return 0;
  449. return 1;
  450. }
  451. int
  452. nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
  453. struct nfsd3_mknodargs *args)
  454. {
  455. if (!(p = decode_fh(p, &args->fh))
  456. || !(p = decode_filename(p, &args->name, &args->len)))
  457. return 0;
  458. args->ftype = ntohl(*p++);
  459. if (args->ftype == NF3BLK || args->ftype == NF3CHR
  460. || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
  461. p = decode_sattr3(p, &args->attrs);
  462. if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
  463. args->major = ntohl(*p++);
  464. args->minor = ntohl(*p++);
  465. }
  466. return xdr_argsize_check(rqstp, p);
  467. }
  468. int
  469. nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
  470. struct nfsd3_renameargs *args)
  471. {
  472. if (!(p = decode_fh(p, &args->ffh))
  473. || !(p = decode_filename(p, &args->fname, &args->flen))
  474. || !(p = decode_fh(p, &args->tfh))
  475. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  476. return 0;
  477. return xdr_argsize_check(rqstp, p);
  478. }
  479. int
  480. nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
  481. struct nfsd3_readlinkargs *args)
  482. {
  483. if (!(p = decode_fh(p, &args->fh)))
  484. return 0;
  485. args->buffer =
  486. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  487. return xdr_argsize_check(rqstp, p);
  488. }
  489. int
  490. nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
  491. struct nfsd3_linkargs *args)
  492. {
  493. if (!(p = decode_fh(p, &args->ffh))
  494. || !(p = decode_fh(p, &args->tfh))
  495. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  496. return 0;
  497. return xdr_argsize_check(rqstp, p);
  498. }
  499. int
  500. nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
  501. struct nfsd3_readdirargs *args)
  502. {
  503. if (!(p = decode_fh(p, &args->fh)))
  504. return 0;
  505. p = xdr_decode_hyper(p, &args->cookie);
  506. args->verf = p; p += 2;
  507. args->dircount = ~0;
  508. args->count = ntohl(*p++);
  509. if (args->count > PAGE_SIZE)
  510. args->count = PAGE_SIZE;
  511. args->buffer =
  512. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  513. return xdr_argsize_check(rqstp, p);
  514. }
  515. int
  516. nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
  517. struct nfsd3_readdirargs *args)
  518. {
  519. int len, pn;
  520. u32 max_blocksize = svc_max_payload(rqstp);
  521. if (!(p = decode_fh(p, &args->fh)))
  522. return 0;
  523. p = xdr_decode_hyper(p, &args->cookie);
  524. args->verf = p; p += 2;
  525. args->dircount = ntohl(*p++);
  526. args->count = ntohl(*p++);
  527. len = (args->count > max_blocksize) ? max_blocksize :
  528. args->count;
  529. args->count = len;
  530. while (len > 0) {
  531. pn = rqstp->rq_resused++;
  532. if (!args->buffer)
  533. args->buffer = page_address(rqstp->rq_respages[pn]);
  534. len -= PAGE_SIZE;
  535. }
  536. return xdr_argsize_check(rqstp, p);
  537. }
  538. int
  539. nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
  540. struct nfsd3_commitargs *args)
  541. {
  542. if (!(p = decode_fh(p, &args->fh)))
  543. return 0;
  544. p = xdr_decode_hyper(p, &args->offset);
  545. args->count = ntohl(*p++);
  546. return xdr_argsize_check(rqstp, p);
  547. }
  548. /*
  549. * XDR encode functions
  550. */
  551. /*
  552. * There must be an encoding function for void results so svc_process
  553. * will work properly.
  554. */
  555. int
  556. nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  557. {
  558. return xdr_ressize_check(rqstp, p);
  559. }
  560. /* GETATTR */
  561. int
  562. nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
  563. struct nfsd3_attrstat *resp)
  564. {
  565. if (resp->status == 0)
  566. p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
  567. return xdr_ressize_check(rqstp, p);
  568. }
  569. /* SETATTR, REMOVE, RMDIR */
  570. int
  571. nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
  572. struct nfsd3_attrstat *resp)
  573. {
  574. p = encode_wcc_data(rqstp, p, &resp->fh);
  575. return xdr_ressize_check(rqstp, p);
  576. }
  577. /* LOOKUP */
  578. int
  579. nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
  580. struct nfsd3_diropres *resp)
  581. {
  582. if (resp->status == 0) {
  583. p = encode_fh(p, &resp->fh);
  584. p = encode_post_op_attr(rqstp, p, &resp->fh);
  585. }
  586. p = encode_post_op_attr(rqstp, p, &resp->dirfh);
  587. return xdr_ressize_check(rqstp, p);
  588. }
  589. /* ACCESS */
  590. int
  591. nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
  592. struct nfsd3_accessres *resp)
  593. {
  594. p = encode_post_op_attr(rqstp, p, &resp->fh);
  595. if (resp->status == 0)
  596. *p++ = htonl(resp->access);
  597. return xdr_ressize_check(rqstp, p);
  598. }
  599. /* READLINK */
  600. int
  601. nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
  602. struct nfsd3_readlinkres *resp)
  603. {
  604. p = encode_post_op_attr(rqstp, p, &resp->fh);
  605. if (resp->status == 0) {
  606. *p++ = htonl(resp->len);
  607. xdr_ressize_check(rqstp, p);
  608. rqstp->rq_res.page_len = resp->len;
  609. if (resp->len & 3) {
  610. /* need to pad the tail */
  611. rqstp->rq_res.tail[0].iov_base = p;
  612. *p = 0;
  613. rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
  614. }
  615. return 1;
  616. } else
  617. return xdr_ressize_check(rqstp, p);
  618. }
  619. /* READ */
  620. int
  621. nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
  622. struct nfsd3_readres *resp)
  623. {
  624. p = encode_post_op_attr(rqstp, p, &resp->fh);
  625. if (resp->status == 0) {
  626. *p++ = htonl(resp->count);
  627. *p++ = htonl(resp->eof);
  628. *p++ = htonl(resp->count); /* xdr opaque count */
  629. xdr_ressize_check(rqstp, p);
  630. /* now update rqstp->rq_res to reflect data aswell */
  631. rqstp->rq_res.page_len = resp->count;
  632. if (resp->count & 3) {
  633. /* need to pad the tail */
  634. rqstp->rq_res.tail[0].iov_base = p;
  635. *p = 0;
  636. rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
  637. }
  638. return 1;
  639. } else
  640. return xdr_ressize_check(rqstp, p);
  641. }
  642. /* WRITE */
  643. int
  644. nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
  645. struct nfsd3_writeres *resp)
  646. {
  647. p = encode_wcc_data(rqstp, p, &resp->fh);
  648. if (resp->status == 0) {
  649. *p++ = htonl(resp->count);
  650. *p++ = htonl(resp->committed);
  651. *p++ = htonl(nfssvc_boot.tv_sec);
  652. *p++ = htonl(nfssvc_boot.tv_usec);
  653. }
  654. return xdr_ressize_check(rqstp, p);
  655. }
  656. /* CREATE, MKDIR, SYMLINK, MKNOD */
  657. int
  658. nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
  659. struct nfsd3_diropres *resp)
  660. {
  661. if (resp->status == 0) {
  662. *p++ = xdr_one;
  663. p = encode_fh(p, &resp->fh);
  664. p = encode_post_op_attr(rqstp, p, &resp->fh);
  665. }
  666. p = encode_wcc_data(rqstp, p, &resp->dirfh);
  667. return xdr_ressize_check(rqstp, p);
  668. }
  669. /* RENAME */
  670. int
  671. nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
  672. struct nfsd3_renameres *resp)
  673. {
  674. p = encode_wcc_data(rqstp, p, &resp->ffh);
  675. p = encode_wcc_data(rqstp, p, &resp->tfh);
  676. return xdr_ressize_check(rqstp, p);
  677. }
  678. /* LINK */
  679. int
  680. nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
  681. struct nfsd3_linkres *resp)
  682. {
  683. p = encode_post_op_attr(rqstp, p, &resp->fh);
  684. p = encode_wcc_data(rqstp, p, &resp->tfh);
  685. return xdr_ressize_check(rqstp, p);
  686. }
  687. /* READDIR */
  688. int
  689. nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
  690. struct nfsd3_readdirres *resp)
  691. {
  692. p = encode_post_op_attr(rqstp, p, &resp->fh);
  693. if (resp->status == 0) {
  694. /* stupid readdir cookie */
  695. memcpy(p, resp->verf, 8); p += 2;
  696. xdr_ressize_check(rqstp, p);
  697. if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
  698. return 1; /*No room for trailer */
  699. rqstp->rq_res.page_len = (resp->count) << 2;
  700. /* add the 'tail' to the end of the 'head' page - page 0. */
  701. rqstp->rq_res.tail[0].iov_base = p;
  702. *p++ = 0; /* no more entries */
  703. *p++ = htonl(resp->common.err == nfserr_eof);
  704. rqstp->rq_res.tail[0].iov_len = 2<<2;
  705. return 1;
  706. } else
  707. return xdr_ressize_check(rqstp, p);
  708. }
  709. static __be32 *
  710. encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
  711. int namlen, ino_t ino)
  712. {
  713. *p++ = xdr_one; /* mark entry present */
  714. p = xdr_encode_hyper(p, ino); /* file id */
  715. p = xdr_encode_array(p, name, namlen);/* name length & name */
  716. cd->offset = p; /* remember pointer */
  717. p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
  718. return p;
  719. }
  720. static __be32 *
  721. encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
  722. struct svc_fh *fhp)
  723. {
  724. p = encode_post_op_attr(cd->rqstp, p, fhp);
  725. *p++ = xdr_one; /* yes, a file handle follows */
  726. p = encode_fh(p, fhp);
  727. fh_put(fhp);
  728. return p;
  729. }
  730. static int
  731. compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
  732. const char *name, int namlen)
  733. {
  734. struct svc_export *exp;
  735. struct dentry *dparent, *dchild;
  736. int rv = 0;
  737. dparent = cd->fh.fh_dentry;
  738. exp = cd->fh.fh_export;
  739. fh_init(fhp, NFS3_FHSIZE);
  740. if (isdotent(name, namlen)) {
  741. if (namlen == 2) {
  742. dchild = dget_parent(dparent);
  743. if (dchild == dparent) {
  744. /* filesystem root - cannot return filehandle for ".." */
  745. dput(dchild);
  746. return 1;
  747. }
  748. } else
  749. dchild = dget(dparent);
  750. } else
  751. dchild = lookup_one_len(name, dparent, namlen);
  752. if (IS_ERR(dchild))
  753. return 1;
  754. if (d_mountpoint(dchild) ||
  755. fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
  756. !dchild->d_inode)
  757. rv = 1;
  758. dput(dchild);
  759. return rv;
  760. }
  761. /*
  762. * Encode a directory entry. This one works for both normal readdir
  763. * and readdirplus.
  764. * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
  765. * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
  766. *
  767. * The readdirplus baggage is 1+21 words for post_op_attr, plus the
  768. * file handle.
  769. */
  770. #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
  771. #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
  772. static int
  773. encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
  774. loff_t offset, ino_t ino, unsigned int d_type, int plus)
  775. {
  776. struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
  777. common);
  778. __be32 *p = cd->buffer;
  779. caddr_t curr_page_addr = NULL;
  780. int pn; /* current page number */
  781. int slen; /* string (name) length */
  782. int elen; /* estimated entry length in words */
  783. int num_entry_words = 0; /* actual number of words */
  784. if (cd->offset) {
  785. u64 offset64 = offset;
  786. if (unlikely(cd->offset1)) {
  787. /* we ended up with offset on a page boundary */
  788. *cd->offset = htonl(offset64 >> 32);
  789. *cd->offset1 = htonl(offset64 & 0xffffffff);
  790. cd->offset1 = NULL;
  791. } else {
  792. xdr_encode_hyper(cd->offset, offset64);
  793. }
  794. }
  795. /*
  796. dprintk("encode_entry(%.*s @%ld%s)\n",
  797. namlen, name, (long) offset, plus? " plus" : "");
  798. */
  799. /* truncate filename if too long */
  800. if (namlen > NFS3_MAXNAMLEN)
  801. namlen = NFS3_MAXNAMLEN;
  802. slen = XDR_QUADLEN(namlen);
  803. elen = slen + NFS3_ENTRY_BAGGAGE
  804. + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
  805. if (cd->buflen < elen) {
  806. cd->common.err = nfserr_toosmall;
  807. return -EINVAL;
  808. }
  809. /* determine which page in rq_respages[] we are currently filling */
  810. for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
  811. curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
  812. if (((caddr_t)cd->buffer >= curr_page_addr) &&
  813. ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
  814. break;
  815. }
  816. if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
  817. /* encode entry in current page */
  818. p = encode_entry_baggage(cd, p, name, namlen, ino);
  819. /* throw in readdirplus baggage */
  820. if (plus) {
  821. struct svc_fh fh;
  822. if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
  823. *p++ = 0;
  824. *p++ = 0;
  825. } else
  826. p = encode_entryplus_baggage(cd, p, &fh);
  827. }
  828. num_entry_words = p - cd->buffer;
  829. } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
  830. /* temporarily encode entry into next page, then move back to
  831. * current and next page in rq_respages[] */
  832. __be32 *p1, *tmp;
  833. int len1, len2;
  834. /* grab next page for temporary storage of entry */
  835. p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
  836. p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
  837. /* throw in readdirplus baggage */
  838. if (plus) {
  839. struct svc_fh fh;
  840. if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
  841. /* zero out the filehandle */
  842. *p1++ = 0;
  843. *p1++ = 0;
  844. } else
  845. p1 = encode_entryplus_baggage(cd, p1, &fh);
  846. }
  847. /* determine entry word length and lengths to go in pages */
  848. num_entry_words = p1 - tmp;
  849. len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
  850. if ((num_entry_words << 2) < len1) {
  851. /* the actual number of words in the entry is less
  852. * than elen and can still fit in the current page
  853. */
  854. memmove(p, tmp, num_entry_words << 2);
  855. p += num_entry_words;
  856. /* update offset */
  857. cd->offset = cd->buffer + (cd->offset - tmp);
  858. } else {
  859. unsigned int offset_r = (cd->offset - tmp) << 2;
  860. /* update pointer to offset location.
  861. * This is a 64bit quantity, so we need to
  862. * deal with 3 cases:
  863. * - entirely in first page
  864. * - entirely in second page
  865. * - 4 bytes in each page
  866. */
  867. if (offset_r + 8 <= len1) {
  868. cd->offset = p + (cd->offset - tmp);
  869. } else if (offset_r >= len1) {
  870. cd->offset -= len1 >> 2;
  871. } else {
  872. /* sitting on the fence */
  873. BUG_ON(offset_r != len1 - 4);
  874. cd->offset = p + (cd->offset - tmp);
  875. cd->offset1 = tmp;
  876. }
  877. len2 = (num_entry_words << 2) - len1;
  878. /* move from temp page to current and next pages */
  879. memmove(p, tmp, len1);
  880. memmove(tmp, (caddr_t)tmp+len1, len2);
  881. p = tmp + (len2 >> 2);
  882. }
  883. }
  884. else {
  885. cd->common.err = nfserr_toosmall;
  886. return -EINVAL;
  887. }
  888. cd->buflen -= num_entry_words;
  889. cd->buffer = p;
  890. cd->common.err = nfs_ok;
  891. return 0;
  892. }
  893. int
  894. nfs3svc_encode_entry(void *cd, const char *name,
  895. int namlen, loff_t offset, u64 ino, unsigned int d_type)
  896. {
  897. return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
  898. }
  899. int
  900. nfs3svc_encode_entry_plus(void *cd, const char *name,
  901. int namlen, loff_t offset, u64 ino,
  902. unsigned int d_type)
  903. {
  904. return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
  905. }
  906. /* FSSTAT */
  907. int
  908. nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
  909. struct nfsd3_fsstatres *resp)
  910. {
  911. struct kstatfs *s = &resp->stats;
  912. u64 bs = s->f_bsize;
  913. *p++ = xdr_zero; /* no post_op_attr */
  914. if (resp->status == 0) {
  915. p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
  916. p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
  917. p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
  918. p = xdr_encode_hyper(p, s->f_files); /* total inodes */
  919. p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
  920. p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
  921. *p++ = htonl(resp->invarsec); /* mean unchanged time */
  922. }
  923. return xdr_ressize_check(rqstp, p);
  924. }
  925. /* FSINFO */
  926. int
  927. nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
  928. struct nfsd3_fsinfores *resp)
  929. {
  930. *p++ = xdr_zero; /* no post_op_attr */
  931. if (resp->status == 0) {
  932. *p++ = htonl(resp->f_rtmax);
  933. *p++ = htonl(resp->f_rtpref);
  934. *p++ = htonl(resp->f_rtmult);
  935. *p++ = htonl(resp->f_wtmax);
  936. *p++ = htonl(resp->f_wtpref);
  937. *p++ = htonl(resp->f_wtmult);
  938. *p++ = htonl(resp->f_dtpref);
  939. p = xdr_encode_hyper(p, resp->f_maxfilesize);
  940. *p++ = xdr_one;
  941. *p++ = xdr_zero;
  942. *p++ = htonl(resp->f_properties);
  943. }
  944. return xdr_ressize_check(rqstp, p);
  945. }
  946. /* PATHCONF */
  947. int
  948. nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
  949. struct nfsd3_pathconfres *resp)
  950. {
  951. *p++ = xdr_zero; /* no post_op_attr */
  952. if (resp->status == 0) {
  953. *p++ = htonl(resp->p_link_max);
  954. *p++ = htonl(resp->p_name_max);
  955. *p++ = htonl(resp->p_no_trunc);
  956. *p++ = htonl(resp->p_chown_restricted);
  957. *p++ = htonl(resp->p_case_insensitive);
  958. *p++ = htonl(resp->p_case_preserving);
  959. }
  960. return xdr_ressize_check(rqstp, p);
  961. }
  962. /* COMMIT */
  963. int
  964. nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
  965. struct nfsd3_commitres *resp)
  966. {
  967. p = encode_wcc_data(rqstp, p, &resp->fh);
  968. /* Write verifier */
  969. if (resp->status == 0) {
  970. *p++ = htonl(nfssvc_boot.tv_sec);
  971. *p++ = htonl(nfssvc_boot.tv_usec);
  972. }
  973. return xdr_ressize_check(rqstp, p);
  974. }
  975. /*
  976. * XDR release functions
  977. */
  978. int
  979. nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
  980. struct nfsd3_attrstat *resp)
  981. {
  982. fh_put(&resp->fh);
  983. return 1;
  984. }
  985. int
  986. nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
  987. struct nfsd3_fhandle_pair *resp)
  988. {
  989. fh_put(&resp->fh1);
  990. fh_put(&resp->fh2);
  991. return 1;
  992. }