nfs3xdr.c 27 KB

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