nfs3xdr.c 27 KB

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