nfs3xdr.c 26 KB

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