nfs3xdr.c 28 KB

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