nfs3xdr.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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 != NULL) {
  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. || !(p = decode_sattr3(p, &args->attrs)))
  272. return 0;
  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. || !(p = xdr_decode_hyper(p, &args->offset)))
  307. return 0;
  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. || !(p = xdr_decode_hyper(p, &args->offset)))
  331. return 0;
  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. * If more than one page was used, then compute the length
  345. * of the data in the request as the total size of the
  346. * request minus the transport protocol headers minus the
  347. * RPC protocol headers minus the NFS protocol fields
  348. * already consumed. If the request fits into a single
  349. * page, then compete the length of the data as the size
  350. * of the NFS portion of the request minus the NFS
  351. * protocol fields already consumed.
  352. */
  353. hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
  354. if (rqstp->rq_respages != rqstp->rq_pages + 1) {
  355. dlen = rqstp->rq_arg.len -
  356. (PAGE_SIZE - rqstp->rq_arg.head[0].iov_len) - hdr;
  357. } else {
  358. dlen = rqstp->rq_arg.head[0].iov_len - hdr;
  359. }
  360. /*
  361. * Round the length of the data which was specified up to
  362. * the next multiple of XDR units and then compare that
  363. * against the length which was actually received.
  364. */
  365. if (dlen != ((len + 3) & ~0x3))
  366. return 0;
  367. if (args->count > max_blocksize) {
  368. args->count = max_blocksize;
  369. len = args->len = max_blocksize;
  370. }
  371. rqstp->rq_vec[0].iov_base = (void*)p;
  372. rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
  373. v = 0;
  374. while (len > rqstp->rq_vec[v].iov_len) {
  375. len -= rqstp->rq_vec[v].iov_len;
  376. v++;
  377. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
  378. rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  379. }
  380. rqstp->rq_vec[v].iov_len = len;
  381. args->vlen = v + 1;
  382. return 1;
  383. }
  384. int
  385. nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
  386. struct nfsd3_createargs *args)
  387. {
  388. if (!(p = decode_fh(p, &args->fh))
  389. || !(p = decode_filename(p, &args->name, &args->len)))
  390. return 0;
  391. switch (args->createmode = ntohl(*p++)) {
  392. case NFS3_CREATE_UNCHECKED:
  393. case NFS3_CREATE_GUARDED:
  394. if (!(p = decode_sattr3(p, &args->attrs)))
  395. return 0;
  396. break;
  397. case NFS3_CREATE_EXCLUSIVE:
  398. args->verf = p;
  399. p += 2;
  400. break;
  401. default:
  402. return 0;
  403. }
  404. return xdr_argsize_check(rqstp, p);
  405. }
  406. int
  407. nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
  408. struct nfsd3_createargs *args)
  409. {
  410. if (!(p = decode_fh(p, &args->fh))
  411. || !(p = decode_filename(p, &args->name, &args->len))
  412. || !(p = decode_sattr3(p, &args->attrs)))
  413. return 0;
  414. return xdr_argsize_check(rqstp, p);
  415. }
  416. int
  417. nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
  418. struct nfsd3_symlinkargs *args)
  419. {
  420. unsigned int len;
  421. int avail;
  422. char *old, *new;
  423. struct kvec *vec;
  424. if (!(p = decode_fh(p, &args->ffh))
  425. || !(p = decode_filename(p, &args->fname, &args->flen))
  426. || !(p = decode_sattr3(p, &args->attrs))
  427. )
  428. return 0;
  429. /* now decode the pathname, which might be larger than the first page.
  430. * As we have to check for nul's anyway, we copy it into a new page
  431. * This page appears in the rq_res.pages list, but as pages_len is always
  432. * 0, it won't get in the way
  433. */
  434. len = ntohl(*p++);
  435. if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
  436. return 0;
  437. args->tname = new =
  438. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  439. args->tlen = len;
  440. /* first copy and check from the first page */
  441. old = (char*)p;
  442. vec = &rqstp->rq_arg.head[0];
  443. avail = vec->iov_len - (old - (char*)vec->iov_base);
  444. while (len && avail && *old) {
  445. *new++ = *old++;
  446. len--;
  447. avail--;
  448. }
  449. /* now copy next page if there is one */
  450. if (len && !avail && rqstp->rq_arg.page_len) {
  451. avail = rqstp->rq_arg.page_len;
  452. if (avail > PAGE_SIZE) avail = PAGE_SIZE;
  453. old = page_address(rqstp->rq_arg.pages[0]);
  454. }
  455. while (len && avail && *old) {
  456. *new++ = *old++;
  457. len--;
  458. avail--;
  459. }
  460. *new = '\0';
  461. if (len)
  462. return 0;
  463. return 1;
  464. }
  465. int
  466. nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
  467. struct nfsd3_mknodargs *args)
  468. {
  469. if (!(p = decode_fh(p, &args->fh))
  470. || !(p = decode_filename(p, &args->name, &args->len)))
  471. return 0;
  472. args->ftype = ntohl(*p++);
  473. if (args->ftype == NF3BLK || args->ftype == NF3CHR
  474. || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
  475. if (!(p = decode_sattr3(p, &args->attrs)))
  476. return 0;
  477. }
  478. if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
  479. args->major = ntohl(*p++);
  480. args->minor = ntohl(*p++);
  481. }
  482. return xdr_argsize_check(rqstp, p);
  483. }
  484. int
  485. nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
  486. struct nfsd3_renameargs *args)
  487. {
  488. if (!(p = decode_fh(p, &args->ffh))
  489. || !(p = decode_filename(p, &args->fname, &args->flen))
  490. || !(p = decode_fh(p, &args->tfh))
  491. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  492. return 0;
  493. return xdr_argsize_check(rqstp, p);
  494. }
  495. int
  496. nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
  497. struct nfsd3_readlinkargs *args)
  498. {
  499. if (!(p = decode_fh(p, &args->fh)))
  500. return 0;
  501. args->buffer =
  502. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  503. return xdr_argsize_check(rqstp, p);
  504. }
  505. int
  506. nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
  507. struct nfsd3_linkargs *args)
  508. {
  509. if (!(p = decode_fh(p, &args->ffh))
  510. || !(p = decode_fh(p, &args->tfh))
  511. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  512. return 0;
  513. return xdr_argsize_check(rqstp, p);
  514. }
  515. int
  516. nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
  517. struct nfsd3_readdirargs *args)
  518. {
  519. if (!(p = decode_fh(p, &args->fh)))
  520. return 0;
  521. p = xdr_decode_hyper(p, &args->cookie);
  522. args->verf = p; p += 2;
  523. args->dircount = ~0;
  524. args->count = ntohl(*p++);
  525. if (args->count > PAGE_SIZE)
  526. args->count = PAGE_SIZE;
  527. args->buffer =
  528. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  529. return xdr_argsize_check(rqstp, p);
  530. }
  531. int
  532. nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
  533. struct nfsd3_readdirargs *args)
  534. {
  535. int len, pn;
  536. u32 max_blocksize = svc_max_payload(rqstp);
  537. if (!(p = decode_fh(p, &args->fh)))
  538. return 0;
  539. p = xdr_decode_hyper(p, &args->cookie);
  540. args->verf = p; p += 2;
  541. args->dircount = ntohl(*p++);
  542. args->count = ntohl(*p++);
  543. len = (args->count > max_blocksize) ? max_blocksize :
  544. args->count;
  545. args->count = len;
  546. while (len > 0) {
  547. pn = rqstp->rq_resused++;
  548. if (!args->buffer)
  549. args->buffer = page_address(rqstp->rq_respages[pn]);
  550. len -= PAGE_SIZE;
  551. }
  552. return xdr_argsize_check(rqstp, p);
  553. }
  554. int
  555. nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
  556. struct nfsd3_commitargs *args)
  557. {
  558. if (!(p = decode_fh(p, &args->fh)))
  559. return 0;
  560. p = xdr_decode_hyper(p, &args->offset);
  561. args->count = ntohl(*p++);
  562. return xdr_argsize_check(rqstp, p);
  563. }
  564. /*
  565. * XDR encode functions
  566. */
  567. /*
  568. * There must be an encoding function for void results so svc_process
  569. * will work properly.
  570. */
  571. int
  572. nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  573. {
  574. return xdr_ressize_check(rqstp, p);
  575. }
  576. /* GETATTR */
  577. int
  578. nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
  579. struct nfsd3_attrstat *resp)
  580. {
  581. if (resp->status == 0)
  582. p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
  583. return xdr_ressize_check(rqstp, p);
  584. }
  585. /* SETATTR, REMOVE, RMDIR */
  586. int
  587. nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
  588. struct nfsd3_attrstat *resp)
  589. {
  590. p = encode_wcc_data(rqstp, p, &resp->fh);
  591. return xdr_ressize_check(rqstp, p);
  592. }
  593. /* LOOKUP */
  594. int
  595. nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
  596. struct nfsd3_diropres *resp)
  597. {
  598. if (resp->status == 0) {
  599. p = encode_fh(p, &resp->fh);
  600. p = encode_post_op_attr(rqstp, p, &resp->fh);
  601. }
  602. p = encode_post_op_attr(rqstp, p, &resp->dirfh);
  603. return xdr_ressize_check(rqstp, p);
  604. }
  605. /* ACCESS */
  606. int
  607. nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
  608. struct nfsd3_accessres *resp)
  609. {
  610. p = encode_post_op_attr(rqstp, p, &resp->fh);
  611. if (resp->status == 0)
  612. *p++ = htonl(resp->access);
  613. return xdr_ressize_check(rqstp, p);
  614. }
  615. /* READLINK */
  616. int
  617. nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
  618. struct nfsd3_readlinkres *resp)
  619. {
  620. p = encode_post_op_attr(rqstp, p, &resp->fh);
  621. if (resp->status == 0) {
  622. *p++ = htonl(resp->len);
  623. xdr_ressize_check(rqstp, p);
  624. rqstp->rq_res.page_len = resp->len;
  625. if (resp->len & 3) {
  626. /* need to pad the tail */
  627. rqstp->rq_res.tail[0].iov_base = p;
  628. *p = 0;
  629. rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
  630. }
  631. return 1;
  632. } else
  633. return xdr_ressize_check(rqstp, p);
  634. }
  635. /* READ */
  636. int
  637. nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
  638. struct nfsd3_readres *resp)
  639. {
  640. p = encode_post_op_attr(rqstp, p, &resp->fh);
  641. if (resp->status == 0) {
  642. *p++ = htonl(resp->count);
  643. *p++ = htonl(resp->eof);
  644. *p++ = htonl(resp->count); /* xdr opaque count */
  645. xdr_ressize_check(rqstp, p);
  646. /* now update rqstp->rq_res to reflect data aswell */
  647. rqstp->rq_res.page_len = resp->count;
  648. if (resp->count & 3) {
  649. /* need to pad the tail */
  650. rqstp->rq_res.tail[0].iov_base = p;
  651. *p = 0;
  652. rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
  653. }
  654. return 1;
  655. } else
  656. return xdr_ressize_check(rqstp, p);
  657. }
  658. /* WRITE */
  659. int
  660. nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
  661. struct nfsd3_writeres *resp)
  662. {
  663. p = encode_wcc_data(rqstp, p, &resp->fh);
  664. if (resp->status == 0) {
  665. *p++ = htonl(resp->count);
  666. *p++ = htonl(resp->committed);
  667. *p++ = htonl(nfssvc_boot.tv_sec);
  668. *p++ = htonl(nfssvc_boot.tv_usec);
  669. }
  670. return xdr_ressize_check(rqstp, p);
  671. }
  672. /* CREATE, MKDIR, SYMLINK, MKNOD */
  673. int
  674. nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
  675. struct nfsd3_diropres *resp)
  676. {
  677. if (resp->status == 0) {
  678. *p++ = xdr_one;
  679. p = encode_fh(p, &resp->fh);
  680. p = encode_post_op_attr(rqstp, p, &resp->fh);
  681. }
  682. p = encode_wcc_data(rqstp, p, &resp->dirfh);
  683. return xdr_ressize_check(rqstp, p);
  684. }
  685. /* RENAME */
  686. int
  687. nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
  688. struct nfsd3_renameres *resp)
  689. {
  690. p = encode_wcc_data(rqstp, p, &resp->ffh);
  691. p = encode_wcc_data(rqstp, p, &resp->tfh);
  692. return xdr_ressize_check(rqstp, p);
  693. }
  694. /* LINK */
  695. int
  696. nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
  697. struct nfsd3_linkres *resp)
  698. {
  699. p = encode_post_op_attr(rqstp, p, &resp->fh);
  700. p = encode_wcc_data(rqstp, p, &resp->tfh);
  701. return xdr_ressize_check(rqstp, p);
  702. }
  703. /* READDIR */
  704. int
  705. nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
  706. struct nfsd3_readdirres *resp)
  707. {
  708. p = encode_post_op_attr(rqstp, p, &resp->fh);
  709. if (resp->status == 0) {
  710. /* stupid readdir cookie */
  711. memcpy(p, resp->verf, 8); p += 2;
  712. xdr_ressize_check(rqstp, p);
  713. if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
  714. return 1; /*No room for trailer */
  715. rqstp->rq_res.page_len = (resp->count) << 2;
  716. /* add the 'tail' to the end of the 'head' page - page 0. */
  717. rqstp->rq_res.tail[0].iov_base = p;
  718. *p++ = 0; /* no more entries */
  719. *p++ = htonl(resp->common.err == nfserr_eof);
  720. rqstp->rq_res.tail[0].iov_len = 2<<2;
  721. return 1;
  722. } else
  723. return xdr_ressize_check(rqstp, p);
  724. }
  725. static __be32 *
  726. encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
  727. int namlen, ino_t ino)
  728. {
  729. *p++ = xdr_one; /* mark entry present */
  730. p = xdr_encode_hyper(p, ino); /* file id */
  731. p = xdr_encode_array(p, name, namlen);/* name length & name */
  732. cd->offset = p; /* remember pointer */
  733. p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
  734. return p;
  735. }
  736. static __be32 *
  737. encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
  738. struct svc_fh *fhp)
  739. {
  740. p = encode_post_op_attr(cd->rqstp, p, fhp);
  741. *p++ = xdr_one; /* yes, a file handle follows */
  742. p = encode_fh(p, fhp);
  743. fh_put(fhp);
  744. return p;
  745. }
  746. static int
  747. compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
  748. const char *name, int namlen)
  749. {
  750. struct svc_export *exp;
  751. struct dentry *dparent, *dchild;
  752. int rv = 0;
  753. dparent = cd->fh.fh_dentry;
  754. exp = cd->fh.fh_export;
  755. fh_init(fhp, NFS3_FHSIZE);
  756. if (isdotent(name, namlen)) {
  757. if (namlen == 2) {
  758. dchild = dget_parent(dparent);
  759. if (dchild == dparent) {
  760. /* filesystem root - cannot return filehandle for ".." */
  761. dput(dchild);
  762. return 1;
  763. }
  764. } else
  765. dchild = dget(dparent);
  766. } else
  767. dchild = lookup_one_len(name, dparent, namlen);
  768. if (IS_ERR(dchild))
  769. return 1;
  770. if (d_mountpoint(dchild) ||
  771. fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
  772. !dchild->d_inode)
  773. rv = 1;
  774. dput(dchild);
  775. return rv;
  776. }
  777. /*
  778. * Encode a directory entry. This one works for both normal readdir
  779. * and readdirplus.
  780. * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
  781. * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
  782. *
  783. * The readdirplus baggage is 1+21 words for post_op_attr, plus the
  784. * file handle.
  785. */
  786. #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
  787. #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
  788. static int
  789. encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
  790. loff_t offset, ino_t ino, unsigned int d_type, int plus)
  791. {
  792. struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
  793. common);
  794. __be32 *p = cd->buffer;
  795. caddr_t curr_page_addr = NULL;
  796. int pn; /* current page number */
  797. int slen; /* string (name) length */
  798. int elen; /* estimated entry length in words */
  799. int num_entry_words = 0; /* actual number of words */
  800. if (cd->offset) {
  801. u64 offset64 = offset;
  802. if (unlikely(cd->offset1)) {
  803. /* we ended up with offset on a page boundary */
  804. *cd->offset = htonl(offset64 >> 32);
  805. *cd->offset1 = htonl(offset64 & 0xffffffff);
  806. cd->offset1 = NULL;
  807. } else {
  808. xdr_encode_hyper(cd->offset, offset64);
  809. }
  810. }
  811. /*
  812. dprintk("encode_entry(%.*s @%ld%s)\n",
  813. namlen, name, (long) offset, plus? " plus" : "");
  814. */
  815. /* truncate filename if too long */
  816. if (namlen > NFS3_MAXNAMLEN)
  817. namlen = NFS3_MAXNAMLEN;
  818. slen = XDR_QUADLEN(namlen);
  819. elen = slen + NFS3_ENTRY_BAGGAGE
  820. + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
  821. if (cd->buflen < elen) {
  822. cd->common.err = nfserr_toosmall;
  823. return -EINVAL;
  824. }
  825. /* determine which page in rq_respages[] we are currently filling */
  826. for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
  827. curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
  828. if (((caddr_t)cd->buffer >= curr_page_addr) &&
  829. ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
  830. break;
  831. }
  832. if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
  833. /* encode entry in current page */
  834. p = encode_entry_baggage(cd, p, name, namlen, ino);
  835. /* throw in readdirplus baggage */
  836. if (plus) {
  837. struct svc_fh fh;
  838. if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
  839. *p++ = 0;
  840. *p++ = 0;
  841. } else
  842. p = encode_entryplus_baggage(cd, p, &fh);
  843. }
  844. num_entry_words = p - cd->buffer;
  845. } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
  846. /* temporarily encode entry into next page, then move back to
  847. * current and next page in rq_respages[] */
  848. __be32 *p1, *tmp;
  849. int len1, len2;
  850. /* grab next page for temporary storage of entry */
  851. p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
  852. p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
  853. /* throw in readdirplus baggage */
  854. if (plus) {
  855. struct svc_fh fh;
  856. if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
  857. /* zero out the filehandle */
  858. *p1++ = 0;
  859. *p1++ = 0;
  860. } else
  861. p1 = encode_entryplus_baggage(cd, p1, &fh);
  862. }
  863. /* determine entry word length and lengths to go in pages */
  864. num_entry_words = p1 - tmp;
  865. len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
  866. if ((num_entry_words << 2) < len1) {
  867. /* the actual number of words in the entry is less
  868. * than elen and can still fit in the current page
  869. */
  870. memmove(p, tmp, num_entry_words << 2);
  871. p += num_entry_words;
  872. /* update offset */
  873. cd->offset = cd->buffer + (cd->offset - tmp);
  874. } else {
  875. unsigned int offset_r = (cd->offset - tmp) << 2;
  876. /* update pointer to offset location.
  877. * This is a 64bit quantity, so we need to
  878. * deal with 3 cases:
  879. * - entirely in first page
  880. * - entirely in second page
  881. * - 4 bytes in each page
  882. */
  883. if (offset_r + 8 <= len1) {
  884. cd->offset = p + (cd->offset - tmp);
  885. } else if (offset_r >= len1) {
  886. cd->offset -= len1 >> 2;
  887. } else {
  888. /* sitting on the fence */
  889. BUG_ON(offset_r != len1 - 4);
  890. cd->offset = p + (cd->offset - tmp);
  891. cd->offset1 = tmp;
  892. }
  893. len2 = (num_entry_words << 2) - len1;
  894. /* move from temp page to current and next pages */
  895. memmove(p, tmp, len1);
  896. memmove(tmp, (caddr_t)tmp+len1, len2);
  897. p = tmp + (len2 >> 2);
  898. }
  899. }
  900. else {
  901. cd->common.err = nfserr_toosmall;
  902. return -EINVAL;
  903. }
  904. cd->buflen -= num_entry_words;
  905. cd->buffer = p;
  906. cd->common.err = nfs_ok;
  907. return 0;
  908. }
  909. int
  910. nfs3svc_encode_entry(void *cd, const char *name,
  911. int namlen, loff_t offset, u64 ino, unsigned int d_type)
  912. {
  913. return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
  914. }
  915. int
  916. nfs3svc_encode_entry_plus(void *cd, const char *name,
  917. int namlen, loff_t offset, u64 ino,
  918. unsigned int d_type)
  919. {
  920. return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
  921. }
  922. /* FSSTAT */
  923. int
  924. nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
  925. struct nfsd3_fsstatres *resp)
  926. {
  927. struct kstatfs *s = &resp->stats;
  928. u64 bs = s->f_bsize;
  929. *p++ = xdr_zero; /* no post_op_attr */
  930. if (resp->status == 0) {
  931. p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
  932. p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
  933. p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
  934. p = xdr_encode_hyper(p, s->f_files); /* total inodes */
  935. p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
  936. p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
  937. *p++ = htonl(resp->invarsec); /* mean unchanged time */
  938. }
  939. return xdr_ressize_check(rqstp, p);
  940. }
  941. /* FSINFO */
  942. int
  943. nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
  944. struct nfsd3_fsinfores *resp)
  945. {
  946. *p++ = xdr_zero; /* no post_op_attr */
  947. if (resp->status == 0) {
  948. *p++ = htonl(resp->f_rtmax);
  949. *p++ = htonl(resp->f_rtpref);
  950. *p++ = htonl(resp->f_rtmult);
  951. *p++ = htonl(resp->f_wtmax);
  952. *p++ = htonl(resp->f_wtpref);
  953. *p++ = htonl(resp->f_wtmult);
  954. *p++ = htonl(resp->f_dtpref);
  955. p = xdr_encode_hyper(p, resp->f_maxfilesize);
  956. *p++ = xdr_one;
  957. *p++ = xdr_zero;
  958. *p++ = htonl(resp->f_properties);
  959. }
  960. return xdr_ressize_check(rqstp, p);
  961. }
  962. /* PATHCONF */
  963. int
  964. nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
  965. struct nfsd3_pathconfres *resp)
  966. {
  967. *p++ = xdr_zero; /* no post_op_attr */
  968. if (resp->status == 0) {
  969. *p++ = htonl(resp->p_link_max);
  970. *p++ = htonl(resp->p_name_max);
  971. *p++ = htonl(resp->p_no_trunc);
  972. *p++ = htonl(resp->p_chown_restricted);
  973. *p++ = htonl(resp->p_case_insensitive);
  974. *p++ = htonl(resp->p_case_preserving);
  975. }
  976. return xdr_ressize_check(rqstp, p);
  977. }
  978. /* COMMIT */
  979. int
  980. nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
  981. struct nfsd3_commitres *resp)
  982. {
  983. p = encode_wcc_data(rqstp, p, &resp->fh);
  984. /* Write verifier */
  985. if (resp->status == 0) {
  986. *p++ = htonl(nfssvc_boot.tv_sec);
  987. *p++ = htonl(nfssvc_boot.tv_usec);
  988. }
  989. return xdr_ressize_check(rqstp, p);
  990. }
  991. /*
  992. * XDR release functions
  993. */
  994. int
  995. nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
  996. struct nfsd3_attrstat *resp)
  997. {
  998. fh_put(&resp->fh);
  999. return 1;
  1000. }
  1001. int
  1002. nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
  1003. struct nfsd3_fhandle_pair *resp)
  1004. {
  1005. fh_put(&resp->fh1);
  1006. fh_put(&resp->fh2);
  1007. return 1;
  1008. }