nfs3xdr.c 27 KB

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