nfs3xdr.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*
  2. * XDR support for nfsd/protocol version 3.
  3. *
  4. * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
  5. *
  6. * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
  7. */
  8. #include <linux/namei.h>
  9. #include "xdr3.h"
  10. #include "auth.h"
  11. #define NFSDDBG_FACILITY NFSDDBG_XDR
  12. /*
  13. * Mapping of S_IF* types to NFS file types
  14. */
  15. static u32 nfs3_ftypes[] = {
  16. NF3NON, NF3FIFO, NF3CHR, NF3BAD,
  17. NF3DIR, NF3BAD, NF3BLK, NF3BAD,
  18. NF3REG, NF3BAD, NF3LNK, NF3BAD,
  19. NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
  20. };
  21. /*
  22. * XDR functions for basic NFS types
  23. */
  24. static __be32 *
  25. encode_time3(__be32 *p, struct timespec *time)
  26. {
  27. *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
  28. return p;
  29. }
  30. static __be32 *
  31. decode_time3(__be32 *p, struct timespec *time)
  32. {
  33. time->tv_sec = ntohl(*p++);
  34. time->tv_nsec = ntohl(*p++);
  35. return p;
  36. }
  37. static __be32 *
  38. decode_fh(__be32 *p, struct svc_fh *fhp)
  39. {
  40. unsigned int size;
  41. fh_init(fhp, NFS3_FHSIZE);
  42. size = ntohl(*p++);
  43. if (size > NFS3_FHSIZE)
  44. return NULL;
  45. memcpy(&fhp->fh_handle.fh_base, p, size);
  46. fhp->fh_handle.fh_size = size;
  47. return p + XDR_QUADLEN(size);
  48. }
  49. /* Helper function for NFSv3 ACL code */
  50. __be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
  51. {
  52. return decode_fh(p, fhp);
  53. }
  54. static __be32 *
  55. encode_fh(__be32 *p, struct svc_fh *fhp)
  56. {
  57. unsigned int size = fhp->fh_handle.fh_size;
  58. *p++ = htonl(size);
  59. if (size) p[XDR_QUADLEN(size)-1]=0;
  60. memcpy(p, &fhp->fh_handle.fh_base, size);
  61. return p + XDR_QUADLEN(size);
  62. }
  63. /*
  64. * Decode a file name and make sure that the path contains
  65. * no slashes or null bytes.
  66. */
  67. static __be32 *
  68. decode_filename(__be32 *p, char **namp, unsigned int *lenp)
  69. {
  70. char *name;
  71. unsigned int i;
  72. if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
  73. for (i = 0, name = *namp; i < *lenp; i++, name++) {
  74. if (*name == '\0' || *name == '/')
  75. return NULL;
  76. }
  77. }
  78. return p;
  79. }
  80. static __be32 *
  81. decode_sattr3(__be32 *p, struct iattr *iap)
  82. {
  83. u32 tmp;
  84. iap->ia_valid = 0;
  85. if (*p++) {
  86. iap->ia_valid |= ATTR_MODE;
  87. iap->ia_mode = ntohl(*p++);
  88. }
  89. if (*p++) {
  90. iap->ia_valid |= ATTR_UID;
  91. iap->ia_uid = ntohl(*p++);
  92. }
  93. if (*p++) {
  94. iap->ia_valid |= ATTR_GID;
  95. iap->ia_gid = ntohl(*p++);
  96. }
  97. if (*p++) {
  98. u64 newsize;
  99. iap->ia_valid |= ATTR_SIZE;
  100. p = xdr_decode_hyper(p, &newsize);
  101. if (newsize <= NFS_OFFSET_MAX)
  102. iap->ia_size = newsize;
  103. else
  104. iap->ia_size = NFS_OFFSET_MAX;
  105. }
  106. if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
  107. iap->ia_valid |= ATTR_ATIME;
  108. } else if (tmp == 2) { /* set to client time */
  109. iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
  110. iap->ia_atime.tv_sec = ntohl(*p++);
  111. iap->ia_atime.tv_nsec = ntohl(*p++);
  112. }
  113. if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
  114. iap->ia_valid |= ATTR_MTIME;
  115. } else if (tmp == 2) { /* set to client time */
  116. iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
  117. iap->ia_mtime.tv_sec = ntohl(*p++);
  118. iap->ia_mtime.tv_nsec = ntohl(*p++);
  119. }
  120. return p;
  121. }
  122. static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
  123. {
  124. u64 f;
  125. switch(fsid_source(fhp)) {
  126. default:
  127. case FSIDSOURCE_DEV:
  128. p = xdr_encode_hyper(p, (u64)huge_encode_dev
  129. (fhp->fh_dentry->d_inode->i_sb->s_dev));
  130. break;
  131. case FSIDSOURCE_FSID:
  132. p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
  133. break;
  134. case FSIDSOURCE_UUID:
  135. f = ((u64*)fhp->fh_export->ex_uuid)[0];
  136. f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
  137. p = xdr_encode_hyper(p, f);
  138. break;
  139. }
  140. return p;
  141. }
  142. static __be32 *
  143. encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
  144. struct kstat *stat)
  145. {
  146. *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
  147. *p++ = htonl((u32) stat->mode);
  148. *p++ = htonl((u32) stat->nlink);
  149. *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
  150. *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
  151. if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
  152. p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
  153. } else {
  154. p = xdr_encode_hyper(p, (u64) stat->size);
  155. }
  156. p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
  157. *p++ = htonl((u32) MAJOR(stat->rdev));
  158. *p++ = htonl((u32) MINOR(stat->rdev));
  159. p = encode_fsid(p, fhp);
  160. p = xdr_encode_hyper(p, stat->ino);
  161. p = encode_time3(p, &stat->atime);
  162. p = encode_time3(p, &stat->mtime);
  163. p = encode_time3(p, &stat->ctime);
  164. return p;
  165. }
  166. static __be32 *
  167. encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  168. {
  169. /* Attributes to follow */
  170. *p++ = xdr_one;
  171. return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
  172. }
  173. /*
  174. * Encode post-operation attributes.
  175. * The inode may be NULL if the call failed because of a stale file
  176. * handle. In this case, no attributes are returned.
  177. */
  178. static __be32 *
  179. encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  180. {
  181. struct dentry *dentry = fhp->fh_dentry;
  182. if (dentry && dentry->d_inode) {
  183. int err;
  184. struct kstat stat;
  185. err = vfs_getattr(fhp->fh_export->ex_path.mnt, dentry, &stat);
  186. if (!err) {
  187. *p++ = xdr_one; /* attributes follow */
  188. lease_get_mtime(dentry->d_inode, &stat.mtime);
  189. return encode_fattr3(rqstp, p, fhp, &stat);
  190. }
  191. }
  192. *p++ = xdr_zero;
  193. return p;
  194. }
  195. /* Helper for NFSv3 ACLs */
  196. __be32 *
  197. nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  198. {
  199. return encode_post_op_attr(rqstp, p, fhp);
  200. }
  201. /*
  202. * Enocde weak cache consistency data
  203. */
  204. static __be32 *
  205. encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
  206. {
  207. struct dentry *dentry = fhp->fh_dentry;
  208. if (dentry && dentry->d_inode && fhp->fh_post_saved) {
  209. if (fhp->fh_pre_saved) {
  210. *p++ = xdr_one;
  211. p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
  212. p = encode_time3(p, &fhp->fh_pre_mtime);
  213. p = encode_time3(p, &fhp->fh_pre_ctime);
  214. } else {
  215. *p++ = xdr_zero;
  216. }
  217. return encode_saved_post_attr(rqstp, p, fhp);
  218. }
  219. /* no pre- or post-attrs */
  220. *p++ = xdr_zero;
  221. return encode_post_op_attr(rqstp, p, fhp);
  222. }
  223. /*
  224. * Fill in the post_op attr for the wcc data
  225. */
  226. void fill_post_wcc(struct svc_fh *fhp)
  227. {
  228. int err;
  229. if (fhp->fh_post_saved)
  230. printk("nfsd: inode locked twice during operation.\n");
  231. err = vfs_getattr(fhp->fh_export->ex_path.mnt, fhp->fh_dentry,
  232. &fhp->fh_post_attr);
  233. fhp->fh_post_change = fhp->fh_dentry->d_inode->i_version;
  234. if (err) {
  235. fhp->fh_post_saved = 0;
  236. /* Grab the ctime anyway - set_change_info might use it */
  237. fhp->fh_post_attr.ctime = fhp->fh_dentry->d_inode->i_ctime;
  238. } else
  239. fhp->fh_post_saved = 1;
  240. }
  241. /*
  242. * XDR decode functions
  243. */
  244. int
  245. nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
  246. {
  247. if (!(p = decode_fh(p, &args->fh)))
  248. return 0;
  249. return xdr_argsize_check(rqstp, p);
  250. }
  251. int
  252. nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
  253. struct nfsd3_sattrargs *args)
  254. {
  255. if (!(p = decode_fh(p, &args->fh)))
  256. return 0;
  257. p = decode_sattr3(p, &args->attrs);
  258. if ((args->check_guard = ntohl(*p++)) != 0) {
  259. struct timespec time;
  260. p = decode_time3(p, &time);
  261. args->guardtime = time.tv_sec;
  262. }
  263. return xdr_argsize_check(rqstp, p);
  264. }
  265. int
  266. nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
  267. struct nfsd3_diropargs *args)
  268. {
  269. if (!(p = decode_fh(p, &args->fh))
  270. || !(p = decode_filename(p, &args->name, &args->len)))
  271. return 0;
  272. return xdr_argsize_check(rqstp, p);
  273. }
  274. int
  275. nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
  276. struct nfsd3_accessargs *args)
  277. {
  278. if (!(p = decode_fh(p, &args->fh)))
  279. return 0;
  280. args->access = ntohl(*p++);
  281. return xdr_argsize_check(rqstp, p);
  282. }
  283. int
  284. nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
  285. struct nfsd3_readargs *args)
  286. {
  287. unsigned int len;
  288. int v,pn;
  289. u32 max_blocksize = svc_max_payload(rqstp);
  290. if (!(p = decode_fh(p, &args->fh)))
  291. return 0;
  292. p = xdr_decode_hyper(p, &args->offset);
  293. len = args->count = ntohl(*p++);
  294. if (len > max_blocksize)
  295. len = max_blocksize;
  296. /* set up the kvec */
  297. v=0;
  298. while (len > 0) {
  299. pn = rqstp->rq_resused++;
  300. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
  301. rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
  302. len -= rqstp->rq_vec[v].iov_len;
  303. v++;
  304. }
  305. args->vlen = v;
  306. return xdr_argsize_check(rqstp, p);
  307. }
  308. int
  309. nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
  310. struct nfsd3_writeargs *args)
  311. {
  312. unsigned int len, v, hdr, dlen;
  313. u32 max_blocksize = svc_max_payload(rqstp);
  314. if (!(p = decode_fh(p, &args->fh)))
  315. return 0;
  316. p = xdr_decode_hyper(p, &args->offset);
  317. args->count = ntohl(*p++);
  318. args->stable = ntohl(*p++);
  319. len = args->len = ntohl(*p++);
  320. /*
  321. * The count must equal the amount of data passed.
  322. */
  323. if (args->count != args->len)
  324. return 0;
  325. /*
  326. * Check to make sure that we got the right number of
  327. * bytes.
  328. */
  329. hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
  330. dlen = rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len
  331. - hdr;
  332. /*
  333. * Round the length of the data which was specified up to
  334. * the next multiple of XDR units and then compare that
  335. * against the length which was actually received.
  336. * Note that when RPCSEC/GSS (for example) is used, the
  337. * data buffer can be padded so dlen might be larger
  338. * than required. It must never be smaller.
  339. */
  340. if (dlen < XDR_QUADLEN(len)*4)
  341. return 0;
  342. if (args->count > max_blocksize) {
  343. args->count = max_blocksize;
  344. len = args->len = max_blocksize;
  345. }
  346. rqstp->rq_vec[0].iov_base = (void*)p;
  347. rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
  348. v = 0;
  349. while (len > rqstp->rq_vec[v].iov_len) {
  350. len -= rqstp->rq_vec[v].iov_len;
  351. v++;
  352. rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
  353. rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  354. }
  355. rqstp->rq_vec[v].iov_len = len;
  356. args->vlen = v + 1;
  357. return 1;
  358. }
  359. int
  360. nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
  361. struct nfsd3_createargs *args)
  362. {
  363. if (!(p = decode_fh(p, &args->fh))
  364. || !(p = decode_filename(p, &args->name, &args->len)))
  365. return 0;
  366. switch (args->createmode = ntohl(*p++)) {
  367. case NFS3_CREATE_UNCHECKED:
  368. case NFS3_CREATE_GUARDED:
  369. p = decode_sattr3(p, &args->attrs);
  370. break;
  371. case NFS3_CREATE_EXCLUSIVE:
  372. args->verf = p;
  373. p += 2;
  374. break;
  375. default:
  376. return 0;
  377. }
  378. return xdr_argsize_check(rqstp, p);
  379. }
  380. int
  381. nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
  382. struct nfsd3_createargs *args)
  383. {
  384. if (!(p = decode_fh(p, &args->fh)) ||
  385. !(p = decode_filename(p, &args->name, &args->len)))
  386. return 0;
  387. p = decode_sattr3(p, &args->attrs);
  388. return xdr_argsize_check(rqstp, p);
  389. }
  390. int
  391. nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
  392. struct nfsd3_symlinkargs *args)
  393. {
  394. unsigned int len, avail;
  395. char *old, *new;
  396. struct kvec *vec;
  397. if (!(p = decode_fh(p, &args->ffh)) ||
  398. !(p = decode_filename(p, &args->fname, &args->flen))
  399. )
  400. return 0;
  401. p = decode_sattr3(p, &args->attrs);
  402. /* now decode the pathname, which might be larger than the first page.
  403. * As we have to check for nul's anyway, we copy it into a new page
  404. * This page appears in the rq_res.pages list, but as pages_len is always
  405. * 0, it won't get in the way
  406. */
  407. len = ntohl(*p++);
  408. if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
  409. return 0;
  410. args->tname = new =
  411. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  412. args->tlen = len;
  413. /* first copy and check from the first page */
  414. old = (char*)p;
  415. vec = &rqstp->rq_arg.head[0];
  416. avail = vec->iov_len - (old - (char*)vec->iov_base);
  417. while (len && avail && *old) {
  418. *new++ = *old++;
  419. len--;
  420. avail--;
  421. }
  422. /* now copy next page if there is one */
  423. if (len && !avail && rqstp->rq_arg.page_len) {
  424. avail = rqstp->rq_arg.page_len;
  425. if (avail > PAGE_SIZE)
  426. avail = PAGE_SIZE;
  427. old = page_address(rqstp->rq_arg.pages[0]);
  428. }
  429. while (len && avail && *old) {
  430. *new++ = *old++;
  431. len--;
  432. avail--;
  433. }
  434. *new = '\0';
  435. if (len)
  436. return 0;
  437. return 1;
  438. }
  439. int
  440. nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
  441. struct nfsd3_mknodargs *args)
  442. {
  443. if (!(p = decode_fh(p, &args->fh))
  444. || !(p = decode_filename(p, &args->name, &args->len)))
  445. return 0;
  446. args->ftype = ntohl(*p++);
  447. if (args->ftype == NF3BLK || args->ftype == NF3CHR
  448. || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
  449. p = decode_sattr3(p, &args->attrs);
  450. if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
  451. args->major = ntohl(*p++);
  452. args->minor = ntohl(*p++);
  453. }
  454. return xdr_argsize_check(rqstp, p);
  455. }
  456. int
  457. nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
  458. struct nfsd3_renameargs *args)
  459. {
  460. if (!(p = decode_fh(p, &args->ffh))
  461. || !(p = decode_filename(p, &args->fname, &args->flen))
  462. || !(p = decode_fh(p, &args->tfh))
  463. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  464. return 0;
  465. return xdr_argsize_check(rqstp, p);
  466. }
  467. int
  468. nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
  469. struct nfsd3_readlinkargs *args)
  470. {
  471. if (!(p = decode_fh(p, &args->fh)))
  472. return 0;
  473. args->buffer =
  474. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  475. return xdr_argsize_check(rqstp, p);
  476. }
  477. int
  478. nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
  479. struct nfsd3_linkargs *args)
  480. {
  481. if (!(p = decode_fh(p, &args->ffh))
  482. || !(p = decode_fh(p, &args->tfh))
  483. || !(p = decode_filename(p, &args->tname, &args->tlen)))
  484. return 0;
  485. return xdr_argsize_check(rqstp, p);
  486. }
  487. int
  488. nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
  489. struct nfsd3_readdirargs *args)
  490. {
  491. if (!(p = decode_fh(p, &args->fh)))
  492. return 0;
  493. p = xdr_decode_hyper(p, &args->cookie);
  494. args->verf = p; p += 2;
  495. args->dircount = ~0;
  496. args->count = ntohl(*p++);
  497. if (args->count > PAGE_SIZE)
  498. args->count = PAGE_SIZE;
  499. args->buffer =
  500. page_address(rqstp->rq_respages[rqstp->rq_resused++]);
  501. return xdr_argsize_check(rqstp, p);
  502. }
  503. int
  504. nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
  505. struct nfsd3_readdirargs *args)
  506. {
  507. int len, pn;
  508. u32 max_blocksize = svc_max_payload(rqstp);
  509. if (!(p = decode_fh(p, &args->fh)))
  510. return 0;
  511. p = xdr_decode_hyper(p, &args->cookie);
  512. args->verf = p; p += 2;
  513. args->dircount = ntohl(*p++);
  514. args->count = ntohl(*p++);
  515. len = (args->count > max_blocksize) ? max_blocksize :
  516. args->count;
  517. args->count = len;
  518. while (len > 0) {
  519. pn = rqstp->rq_resused++;
  520. if (!args->buffer)
  521. args->buffer = page_address(rqstp->rq_respages[pn]);
  522. len -= PAGE_SIZE;
  523. }
  524. return xdr_argsize_check(rqstp, p);
  525. }
  526. int
  527. nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
  528. struct nfsd3_commitargs *args)
  529. {
  530. if (!(p = decode_fh(p, &args->fh)))
  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, void *dummy)
  545. {
  546. return xdr_ressize_check(rqstp, p);
  547. }
  548. /* GETATTR */
  549. int
  550. nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
  551. struct nfsd3_attrstat *resp)
  552. {
  553. if (resp->status == 0) {
  554. lease_get_mtime(resp->fh.fh_dentry->d_inode,
  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. struct nfsd3_attrstat *resp)
  564. {
  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. struct nfsd3_diropres *resp)
  572. {
  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. struct nfsd3_accessres *resp)
  584. {
  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. struct nfsd3_readlinkres *resp)
  594. {
  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. struct nfsd3_readres *resp)
  614. {
  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. struct nfsd3_writeres *resp)
  637. {
  638. p = encode_wcc_data(rqstp, p, &resp->fh);
  639. if (resp->status == 0) {
  640. *p++ = htonl(resp->count);
  641. *p++ = htonl(resp->committed);
  642. *p++ = htonl(nfssvc_boot.tv_sec);
  643. *p++ = htonl(nfssvc_boot.tv_usec);
  644. }
  645. return xdr_ressize_check(rqstp, p);
  646. }
  647. /* CREATE, MKDIR, SYMLINK, MKNOD */
  648. int
  649. nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
  650. struct nfsd3_diropres *resp)
  651. {
  652. if (resp->status == 0) {
  653. *p++ = xdr_one;
  654. p = encode_fh(p, &resp->fh);
  655. p = encode_post_op_attr(rqstp, p, &resp->fh);
  656. }
  657. p = encode_wcc_data(rqstp, p, &resp->dirfh);
  658. return xdr_ressize_check(rqstp, p);
  659. }
  660. /* RENAME */
  661. int
  662. nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
  663. struct nfsd3_renameres *resp)
  664. {
  665. p = encode_wcc_data(rqstp, p, &resp->ffh);
  666. p = encode_wcc_data(rqstp, p, &resp->tfh);
  667. return xdr_ressize_check(rqstp, p);
  668. }
  669. /* LINK */
  670. int
  671. nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
  672. struct nfsd3_linkres *resp)
  673. {
  674. p = encode_post_op_attr(rqstp, p, &resp->fh);
  675. p = encode_wcc_data(rqstp, p, &resp->tfh);
  676. return xdr_ressize_check(rqstp, p);
  677. }
  678. /* READDIR */
  679. int
  680. nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
  681. struct nfsd3_readdirres *resp)
  682. {
  683. p = encode_post_op_attr(rqstp, p, &resp->fh);
  684. if (resp->status == 0) {
  685. /* stupid readdir cookie */
  686. memcpy(p, resp->verf, 8); p += 2;
  687. xdr_ressize_check(rqstp, p);
  688. if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
  689. return 1; /*No room for trailer */
  690. rqstp->rq_res.page_len = (resp->count) << 2;
  691. /* add the 'tail' to the end of the 'head' page - page 0. */
  692. rqstp->rq_res.tail[0].iov_base = p;
  693. *p++ = 0; /* no more entries */
  694. *p++ = htonl(resp->common.err == nfserr_eof);
  695. rqstp->rq_res.tail[0].iov_len = 2<<2;
  696. return 1;
  697. } else
  698. return xdr_ressize_check(rqstp, p);
  699. }
  700. static __be32 *
  701. encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
  702. int namlen, u64 ino)
  703. {
  704. *p++ = xdr_one; /* mark entry present */
  705. p = xdr_encode_hyper(p, ino); /* file id */
  706. p = xdr_encode_array(p, name, namlen);/* name length & name */
  707. cd->offset = p; /* remember pointer */
  708. p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
  709. return p;
  710. }
  711. static __be32
  712. compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
  713. const char *name, int namlen)
  714. {
  715. struct svc_export *exp;
  716. struct dentry *dparent, *dchild;
  717. __be32 rv = nfserr_noent;
  718. dparent = cd->fh.fh_dentry;
  719. exp = cd->fh.fh_export;
  720. if (isdotent(name, namlen)) {
  721. if (namlen == 2) {
  722. dchild = dget_parent(dparent);
  723. /*
  724. * Don't return filehandle for ".." if we're at
  725. * the filesystem or export root:
  726. */
  727. if (dchild == dparent)
  728. goto out;
  729. if (dparent == exp->ex_path.dentry)
  730. goto out;
  731. } else
  732. dchild = dget(dparent);
  733. } else
  734. dchild = lookup_one_len(name, dparent, namlen);
  735. if (IS_ERR(dchild))
  736. return rv;
  737. if (d_mountpoint(dchild))
  738. goto out;
  739. if (!dchild->d_inode)
  740. goto out;
  741. rv = fh_compose(fhp, exp, dchild, &cd->fh);
  742. out:
  743. dput(dchild);
  744. return rv;
  745. }
  746. static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen)
  747. {
  748. struct svc_fh fh;
  749. __be32 err;
  750. fh_init(&fh, NFS3_FHSIZE);
  751. err = compose_entry_fh(cd, &fh, name, namlen);
  752. if (err) {
  753. *p++ = 0;
  754. *p++ = 0;
  755. goto out;
  756. }
  757. p = encode_post_op_attr(cd->rqstp, p, &fh);
  758. *p++ = xdr_one; /* yes, a file handle follows */
  759. p = encode_fh(p, &fh);
  760. out:
  761. fh_put(&fh);
  762. return p;
  763. }
  764. /*
  765. * Encode a directory entry. This one works for both normal readdir
  766. * and readdirplus.
  767. * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
  768. * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
  769. *
  770. * The readdirplus baggage is 1+21 words for post_op_attr, plus the
  771. * file handle.
  772. */
  773. #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
  774. #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
  775. static int
  776. encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
  777. loff_t offset, u64 ino, unsigned int d_type, int plus)
  778. {
  779. struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
  780. common);
  781. __be32 *p = cd->buffer;
  782. caddr_t curr_page_addr = NULL;
  783. int pn; /* current page number */
  784. int slen; /* string (name) length */
  785. int elen; /* estimated entry length in words */
  786. int num_entry_words = 0; /* actual number of words */
  787. if (cd->offset) {
  788. u64 offset64 = offset;
  789. if (unlikely(cd->offset1)) {
  790. /* we ended up with offset on a page boundary */
  791. *cd->offset = htonl(offset64 >> 32);
  792. *cd->offset1 = htonl(offset64 & 0xffffffff);
  793. cd->offset1 = NULL;
  794. } else {
  795. xdr_encode_hyper(cd->offset, offset64);
  796. }
  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. if (namlen > NFS3_MAXNAMLEN)
  804. namlen = NFS3_MAXNAMLEN;
  805. slen = XDR_QUADLEN(namlen);
  806. elen = slen + NFS3_ENTRY_BAGGAGE
  807. + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
  808. if (cd->buflen < elen) {
  809. cd->common.err = nfserr_toosmall;
  810. return -EINVAL;
  811. }
  812. /* determine which page in rq_respages[] we are currently filling */
  813. for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
  814. curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
  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);
  824. num_entry_words = p - cd->buffer;
  825. } else if (cd->rqstp->rq_respages[pn+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(cd->rqstp->rq_respages[pn+1]);
  832. p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
  833. if (plus)
  834. p1 = encode_entryplus_baggage(cd, p1, name, namlen);
  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. struct nfsd3_fsstatres *resp)
  898. {
  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. struct nfsd3_fsinfores *resp)
  917. {
  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. struct nfsd3_pathconfres *resp)
  938. {
  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. struct nfsd3_commitres *resp)
  954. {
  955. p = encode_wcc_data(rqstp, p, &resp->fh);
  956. /* Write verifier */
  957. if (resp->status == 0) {
  958. *p++ = htonl(nfssvc_boot.tv_sec);
  959. *p++ = htonl(nfssvc_boot.tv_usec);
  960. }
  961. return xdr_ressize_check(rqstp, p);
  962. }
  963. /*
  964. * XDR release functions
  965. */
  966. int
  967. nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
  968. struct nfsd3_attrstat *resp)
  969. {
  970. fh_put(&resp->fh);
  971. return 1;
  972. }
  973. int
  974. nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
  975. struct nfsd3_fhandle_pair *resp)
  976. {
  977. fh_put(&resp->fh1);
  978. fh_put(&resp->fh2);
  979. return 1;
  980. }