nfs3xdr.c 27 KB

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