nfs3xdr.c 27 KB

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