nfs3xdr.c 27 KB

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