nfs3xdr.c 27 KB

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