nfs3xdr.c 26 KB

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