nfs3xdr.c 27 KB

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