nfs3xdr.c 28 KB

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