nfs3xdr.c 27 KB

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