nfs3xdr.c 27 KB

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