nfsproc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Process version 2 NFS requests.
  4. *
  5. * Copyright (C) 1995-1997 Olaf Kirch <[email protected]>
  6. */
  7. #include <linux/namei.h>
  8. #include "cache.h"
  9. #include "xdr.h"
  10. #include "vfs.h"
  11. #define NFSDDBG_FACILITY NFSDDBG_PROC
  12. static __be32
  13. nfsd_proc_null(struct svc_rqst *rqstp)
  14. {
  15. return rpc_success;
  16. }
  17. /*
  18. * Get a file's attributes
  19. * N.B. After this call resp->fh needs an fh_put
  20. */
  21. static __be32
  22. nfsd_proc_getattr(struct svc_rqst *rqstp)
  23. {
  24. struct nfsd_fhandle *argp = rqstp->rq_argp;
  25. struct nfsd_attrstat *resp = rqstp->rq_resp;
  26. dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh));
  27. fh_copy(&resp->fh, &argp->fh);
  28. resp->status = fh_verify(rqstp, &resp->fh, 0,
  29. NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
  30. if (resp->status != nfs_ok)
  31. goto out;
  32. resp->status = fh_getattr(&resp->fh, &resp->stat);
  33. out:
  34. return rpc_success;
  35. }
  36. /*
  37. * Set a file's attributes
  38. * N.B. After this call resp->fh needs an fh_put
  39. */
  40. static __be32
  41. nfsd_proc_setattr(struct svc_rqst *rqstp)
  42. {
  43. struct nfsd_sattrargs *argp = rqstp->rq_argp;
  44. struct nfsd_attrstat *resp = rqstp->rq_resp;
  45. struct iattr *iap = &argp->attrs;
  46. struct nfsd_attrs attrs = {
  47. .na_iattr = iap,
  48. };
  49. struct svc_fh *fhp;
  50. dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n",
  51. SVCFH_fmt(&argp->fh),
  52. argp->attrs.ia_valid, (long) argp->attrs.ia_size);
  53. fhp = fh_copy(&resp->fh, &argp->fh);
  54. /*
  55. * NFSv2 does not differentiate between "set-[ac]time-to-now"
  56. * which only requires access, and "set-[ac]time-to-X" which
  57. * requires ownership.
  58. * So if it looks like it might be "set both to the same time which
  59. * is close to now", and if setattr_prepare fails, then we
  60. * convert to "set to now" instead of "set to explicit time"
  61. *
  62. * We only call setattr_prepare as the last test as technically
  63. * it is not an interface that we should be using.
  64. */
  65. #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
  66. #define MAX_TOUCH_TIME_ERROR (30*60)
  67. if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
  68. iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
  69. /*
  70. * Looks probable.
  71. *
  72. * Now just make sure time is in the right ballpark.
  73. * Solaris, at least, doesn't seem to care what the time
  74. * request is. We require it be within 30 minutes of now.
  75. */
  76. time64_t delta = iap->ia_atime.tv_sec - ktime_get_real_seconds();
  77. resp->status = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
  78. if (resp->status != nfs_ok)
  79. goto out;
  80. if (delta < 0)
  81. delta = -delta;
  82. if (delta < MAX_TOUCH_TIME_ERROR &&
  83. setattr_prepare(&init_user_ns, fhp->fh_dentry, iap) != 0) {
  84. /*
  85. * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
  86. * This will cause notify_change to set these times
  87. * to "now"
  88. */
  89. iap->ia_valid &= ~BOTH_TIME_SET;
  90. }
  91. }
  92. resp->status = nfsd_setattr(rqstp, fhp, &attrs, 0, (time64_t)0);
  93. if (resp->status != nfs_ok)
  94. goto out;
  95. resp->status = fh_getattr(&resp->fh, &resp->stat);
  96. out:
  97. return rpc_success;
  98. }
  99. /* Obsolete, replaced by MNTPROC_MNT. */
  100. static __be32
  101. nfsd_proc_root(struct svc_rqst *rqstp)
  102. {
  103. return rpc_success;
  104. }
  105. /*
  106. * Look up a path name component
  107. * Note: the dentry in the resp->fh may be negative if the file
  108. * doesn't exist yet.
  109. * N.B. After this call resp->fh needs an fh_put
  110. */
  111. static __be32
  112. nfsd_proc_lookup(struct svc_rqst *rqstp)
  113. {
  114. struct nfsd_diropargs *argp = rqstp->rq_argp;
  115. struct nfsd_diropres *resp = rqstp->rq_resp;
  116. dprintk("nfsd: LOOKUP %s %.*s\n",
  117. SVCFH_fmt(&argp->fh), argp->len, argp->name);
  118. fh_init(&resp->fh, NFS_FHSIZE);
  119. resp->status = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
  120. &resp->fh);
  121. fh_put(&argp->fh);
  122. if (resp->status != nfs_ok)
  123. goto out;
  124. resp->status = fh_getattr(&resp->fh, &resp->stat);
  125. out:
  126. return rpc_success;
  127. }
  128. /*
  129. * Read a symlink.
  130. */
  131. static __be32
  132. nfsd_proc_readlink(struct svc_rqst *rqstp)
  133. {
  134. struct nfsd_fhandle *argp = rqstp->rq_argp;
  135. struct nfsd_readlinkres *resp = rqstp->rq_resp;
  136. dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
  137. /* Read the symlink. */
  138. resp->len = NFS_MAXPATHLEN;
  139. resp->page = *(rqstp->rq_next_page++);
  140. resp->status = nfsd_readlink(rqstp, &argp->fh,
  141. page_address(resp->page), &resp->len);
  142. fh_put(&argp->fh);
  143. return rpc_success;
  144. }
  145. /*
  146. * Read a portion of a file.
  147. * N.B. After this call resp->fh needs an fh_put
  148. */
  149. static __be32
  150. nfsd_proc_read(struct svc_rqst *rqstp)
  151. {
  152. struct nfsd_readargs *argp = rqstp->rq_argp;
  153. struct nfsd_readres *resp = rqstp->rq_resp;
  154. unsigned int len;
  155. u32 eof;
  156. int v;
  157. dprintk("nfsd: READ %s %d bytes at %d\n",
  158. SVCFH_fmt(&argp->fh),
  159. argp->count, argp->offset);
  160. argp->count = min_t(u32, argp->count, NFSSVC_MAXBLKSIZE_V2);
  161. argp->count = min_t(u32, argp->count, rqstp->rq_res.buflen);
  162. v = 0;
  163. len = argp->count;
  164. resp->pages = rqstp->rq_next_page;
  165. while (len > 0) {
  166. struct page *page = *(rqstp->rq_next_page++);
  167. rqstp->rq_vec[v].iov_base = page_address(page);
  168. rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
  169. len -= rqstp->rq_vec[v].iov_len;
  170. v++;
  171. }
  172. /* Obtain buffer pointer for payload. 19 is 1 word for
  173. * status, 17 words for fattr, and 1 word for the byte count.
  174. */
  175. svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
  176. resp->count = argp->count;
  177. fh_copy(&resp->fh, &argp->fh);
  178. resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
  179. rqstp->rq_vec, v, &resp->count, &eof);
  180. if (resp->status == nfs_ok)
  181. resp->status = fh_getattr(&resp->fh, &resp->stat);
  182. else if (resp->status == nfserr_jukebox)
  183. return rpc_drop_reply;
  184. return rpc_success;
  185. }
  186. /* Reserved */
  187. static __be32
  188. nfsd_proc_writecache(struct svc_rqst *rqstp)
  189. {
  190. return rpc_success;
  191. }
  192. /*
  193. * Write data to a file
  194. * N.B. After this call resp->fh needs an fh_put
  195. */
  196. static __be32
  197. nfsd_proc_write(struct svc_rqst *rqstp)
  198. {
  199. struct nfsd_writeargs *argp = rqstp->rq_argp;
  200. struct nfsd_attrstat *resp = rqstp->rq_resp;
  201. unsigned long cnt = argp->len;
  202. unsigned int nvecs;
  203. dprintk("nfsd: WRITE %s %u bytes at %d\n",
  204. SVCFH_fmt(&argp->fh),
  205. argp->len, argp->offset);
  206. nvecs = svc_fill_write_vector(rqstp, &argp->payload);
  207. resp->status = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
  208. argp->offset, rqstp->rq_vec, nvecs,
  209. &cnt, NFS_DATA_SYNC, NULL);
  210. if (resp->status == nfs_ok)
  211. resp->status = fh_getattr(&resp->fh, &resp->stat);
  212. else if (resp->status == nfserr_jukebox)
  213. return rpc_drop_reply;
  214. return rpc_success;
  215. }
  216. /*
  217. * CREATE processing is complicated. The keyword here is `overloaded.'
  218. * The parent directory is kept locked between the check for existence
  219. * and the actual create() call in compliance with VFS protocols.
  220. * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
  221. */
  222. static __be32
  223. nfsd_proc_create(struct svc_rqst *rqstp)
  224. {
  225. struct nfsd_createargs *argp = rqstp->rq_argp;
  226. struct nfsd_diropres *resp = rqstp->rq_resp;
  227. svc_fh *dirfhp = &argp->fh;
  228. svc_fh *newfhp = &resp->fh;
  229. struct iattr *attr = &argp->attrs;
  230. struct nfsd_attrs attrs = {
  231. .na_iattr = attr,
  232. };
  233. struct inode *inode;
  234. struct dentry *dchild;
  235. int type, mode;
  236. int hosterr;
  237. dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
  238. dprintk("nfsd: CREATE %s %.*s\n",
  239. SVCFH_fmt(dirfhp), argp->len, argp->name);
  240. /* First verify the parent file handle */
  241. resp->status = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
  242. if (resp->status != nfs_ok)
  243. goto done; /* must fh_put dirfhp even on error */
  244. /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
  245. resp->status = nfserr_exist;
  246. if (isdotent(argp->name, argp->len))
  247. goto done;
  248. hosterr = fh_want_write(dirfhp);
  249. if (hosterr) {
  250. resp->status = nfserrno(hosterr);
  251. goto done;
  252. }
  253. inode_lock_nested(dirfhp->fh_dentry->d_inode, I_MUTEX_PARENT);
  254. dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
  255. if (IS_ERR(dchild)) {
  256. resp->status = nfserrno(PTR_ERR(dchild));
  257. goto out_unlock;
  258. }
  259. fh_init(newfhp, NFS_FHSIZE);
  260. resp->status = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
  261. if (!resp->status && d_really_is_negative(dchild))
  262. resp->status = nfserr_noent;
  263. dput(dchild);
  264. if (resp->status) {
  265. if (resp->status != nfserr_noent)
  266. goto out_unlock;
  267. /*
  268. * If the new file handle wasn't verified, we can't tell
  269. * whether the file exists or not. Time to bail ...
  270. */
  271. resp->status = nfserr_acces;
  272. if (!newfhp->fh_dentry) {
  273. printk(KERN_WARNING
  274. "nfsd_proc_create: file handle not verified\n");
  275. goto out_unlock;
  276. }
  277. }
  278. inode = d_inode(newfhp->fh_dentry);
  279. /* Unfudge the mode bits */
  280. if (attr->ia_valid & ATTR_MODE) {
  281. type = attr->ia_mode & S_IFMT;
  282. mode = attr->ia_mode & ~S_IFMT;
  283. if (!type) {
  284. /* no type, so if target exists, assume same as that,
  285. * else assume a file */
  286. if (inode) {
  287. type = inode->i_mode & S_IFMT;
  288. switch(type) {
  289. case S_IFCHR:
  290. case S_IFBLK:
  291. /* reserve rdev for later checking */
  292. rdev = inode->i_rdev;
  293. attr->ia_valid |= ATTR_SIZE;
  294. fallthrough;
  295. case S_IFIFO:
  296. /* this is probably a permission check..
  297. * at least IRIX implements perm checking on
  298. * echo thing > device-special-file-or-pipe
  299. * by doing a CREATE with type==0
  300. */
  301. resp->status = nfsd_permission(rqstp,
  302. newfhp->fh_export,
  303. newfhp->fh_dentry,
  304. NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
  305. if (resp->status && resp->status != nfserr_rofs)
  306. goto out_unlock;
  307. }
  308. } else
  309. type = S_IFREG;
  310. }
  311. } else if (inode) {
  312. type = inode->i_mode & S_IFMT;
  313. mode = inode->i_mode & ~S_IFMT;
  314. } else {
  315. type = S_IFREG;
  316. mode = 0; /* ??? */
  317. }
  318. attr->ia_valid |= ATTR_MODE;
  319. attr->ia_mode = mode;
  320. /* Special treatment for non-regular files according to the
  321. * gospel of sun micro
  322. */
  323. if (type != S_IFREG) {
  324. if (type != S_IFBLK && type != S_IFCHR) {
  325. rdev = 0;
  326. } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
  327. /* If you think you've seen the worst, grok this. */
  328. type = S_IFIFO;
  329. } else {
  330. /* Okay, char or block special */
  331. if (!rdev)
  332. rdev = wanted;
  333. }
  334. /* we've used the SIZE information, so discard it */
  335. attr->ia_valid &= ~ATTR_SIZE;
  336. /* Make sure the type and device matches */
  337. resp->status = nfserr_exist;
  338. if (inode && inode_wrong_type(inode, type))
  339. goto out_unlock;
  340. }
  341. resp->status = nfs_ok;
  342. if (!inode) {
  343. /* File doesn't exist. Create it and set attrs */
  344. resp->status = nfsd_create_locked(rqstp, dirfhp, &attrs, type,
  345. rdev, newfhp);
  346. } else if (type == S_IFREG) {
  347. dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
  348. argp->name, attr->ia_valid, (long) attr->ia_size);
  349. /* File already exists. We ignore all attributes except
  350. * size, so that creat() behaves exactly like
  351. * open(..., O_CREAT|O_TRUNC|O_WRONLY).
  352. */
  353. attr->ia_valid &= ATTR_SIZE;
  354. if (attr->ia_valid)
  355. resp->status = nfsd_setattr(rqstp, newfhp, &attrs, 0,
  356. (time64_t)0);
  357. }
  358. out_unlock:
  359. inode_unlock(dirfhp->fh_dentry->d_inode);
  360. fh_drop_write(dirfhp);
  361. done:
  362. fh_put(dirfhp);
  363. if (resp->status != nfs_ok)
  364. goto out;
  365. resp->status = fh_getattr(&resp->fh, &resp->stat);
  366. out:
  367. return rpc_success;
  368. }
  369. static __be32
  370. nfsd_proc_remove(struct svc_rqst *rqstp)
  371. {
  372. struct nfsd_diropargs *argp = rqstp->rq_argp;
  373. struct nfsd_stat *resp = rqstp->rq_resp;
  374. dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh),
  375. argp->len, argp->name);
  376. /* Unlink. -SIFDIR means file must not be a directory */
  377. resp->status = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR,
  378. argp->name, argp->len);
  379. fh_put(&argp->fh);
  380. return rpc_success;
  381. }
  382. static __be32
  383. nfsd_proc_rename(struct svc_rqst *rqstp)
  384. {
  385. struct nfsd_renameargs *argp = rqstp->rq_argp;
  386. struct nfsd_stat *resp = rqstp->rq_resp;
  387. dprintk("nfsd: RENAME %s %.*s -> \n",
  388. SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
  389. dprintk("nfsd: -> %s %.*s\n",
  390. SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
  391. resp->status = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
  392. &argp->tfh, argp->tname, argp->tlen);
  393. fh_put(&argp->ffh);
  394. fh_put(&argp->tfh);
  395. return rpc_success;
  396. }
  397. static __be32
  398. nfsd_proc_link(struct svc_rqst *rqstp)
  399. {
  400. struct nfsd_linkargs *argp = rqstp->rq_argp;
  401. struct nfsd_stat *resp = rqstp->rq_resp;
  402. dprintk("nfsd: LINK %s ->\n",
  403. SVCFH_fmt(&argp->ffh));
  404. dprintk("nfsd: %s %.*s\n",
  405. SVCFH_fmt(&argp->tfh),
  406. argp->tlen,
  407. argp->tname);
  408. resp->status = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
  409. &argp->ffh);
  410. fh_put(&argp->ffh);
  411. fh_put(&argp->tfh);
  412. return rpc_success;
  413. }
  414. static __be32
  415. nfsd_proc_symlink(struct svc_rqst *rqstp)
  416. {
  417. struct nfsd_symlinkargs *argp = rqstp->rq_argp;
  418. struct nfsd_stat *resp = rqstp->rq_resp;
  419. struct nfsd_attrs attrs = {
  420. .na_iattr = &argp->attrs,
  421. };
  422. struct svc_fh newfh;
  423. if (argp->tlen > NFS_MAXPATHLEN) {
  424. resp->status = nfserr_nametoolong;
  425. goto out;
  426. }
  427. argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
  428. page_address(rqstp->rq_arg.pages[0]),
  429. argp->tlen);
  430. if (IS_ERR(argp->tname)) {
  431. resp->status = nfserrno(PTR_ERR(argp->tname));
  432. goto out;
  433. }
  434. dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n",
  435. SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
  436. argp->tlen, argp->tname);
  437. fh_init(&newfh, NFS_FHSIZE);
  438. resp->status = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
  439. argp->tname, &attrs, &newfh);
  440. kfree(argp->tname);
  441. fh_put(&argp->ffh);
  442. fh_put(&newfh);
  443. out:
  444. return rpc_success;
  445. }
  446. /*
  447. * Make directory. This operation is not idempotent.
  448. * N.B. After this call resp->fh needs an fh_put
  449. */
  450. static __be32
  451. nfsd_proc_mkdir(struct svc_rqst *rqstp)
  452. {
  453. struct nfsd_createargs *argp = rqstp->rq_argp;
  454. struct nfsd_diropres *resp = rqstp->rq_resp;
  455. struct nfsd_attrs attrs = {
  456. .na_iattr = &argp->attrs,
  457. };
  458. dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
  459. if (resp->fh.fh_dentry) {
  460. printk(KERN_WARNING
  461. "nfsd_proc_mkdir: response already verified??\n");
  462. }
  463. argp->attrs.ia_valid &= ~ATTR_SIZE;
  464. fh_init(&resp->fh, NFS_FHSIZE);
  465. resp->status = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
  466. &attrs, S_IFDIR, 0, &resp->fh);
  467. fh_put(&argp->fh);
  468. if (resp->status != nfs_ok)
  469. goto out;
  470. resp->status = fh_getattr(&resp->fh, &resp->stat);
  471. out:
  472. return rpc_success;
  473. }
  474. /*
  475. * Remove a directory
  476. */
  477. static __be32
  478. nfsd_proc_rmdir(struct svc_rqst *rqstp)
  479. {
  480. struct nfsd_diropargs *argp = rqstp->rq_argp;
  481. struct nfsd_stat *resp = rqstp->rq_resp;
  482. dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
  483. resp->status = nfsd_unlink(rqstp, &argp->fh, S_IFDIR,
  484. argp->name, argp->len);
  485. fh_put(&argp->fh);
  486. return rpc_success;
  487. }
  488. static void nfsd_init_dirlist_pages(struct svc_rqst *rqstp,
  489. struct nfsd_readdirres *resp,
  490. u32 count)
  491. {
  492. struct xdr_buf *buf = &resp->dirlist;
  493. struct xdr_stream *xdr = &resp->xdr;
  494. memset(buf, 0, sizeof(*buf));
  495. /* Reserve room for the NULL ptr & eof flag (-2 words) */
  496. buf->buflen = clamp(count, (u32)(XDR_UNIT * 2), (u32)PAGE_SIZE);
  497. buf->buflen -= XDR_UNIT * 2;
  498. buf->pages = rqstp->rq_next_page;
  499. rqstp->rq_next_page++;
  500. xdr_init_encode_pages(xdr, buf, buf->pages, NULL);
  501. }
  502. /*
  503. * Read a portion of a directory.
  504. */
  505. static __be32
  506. nfsd_proc_readdir(struct svc_rqst *rqstp)
  507. {
  508. struct nfsd_readdirargs *argp = rqstp->rq_argp;
  509. struct nfsd_readdirres *resp = rqstp->rq_resp;
  510. loff_t offset;
  511. dprintk("nfsd: READDIR %s %d bytes at %d\n",
  512. SVCFH_fmt(&argp->fh),
  513. argp->count, argp->cookie);
  514. nfsd_init_dirlist_pages(rqstp, resp, argp->count);
  515. resp->common.err = nfs_ok;
  516. resp->cookie_offset = 0;
  517. offset = argp->cookie;
  518. resp->status = nfsd_readdir(rqstp, &argp->fh, &offset,
  519. &resp->common, nfssvc_encode_entry);
  520. nfssvc_encode_nfscookie(resp, offset);
  521. fh_put(&argp->fh);
  522. return rpc_success;
  523. }
  524. /*
  525. * Get file system info
  526. */
  527. static __be32
  528. nfsd_proc_statfs(struct svc_rqst *rqstp)
  529. {
  530. struct nfsd_fhandle *argp = rqstp->rq_argp;
  531. struct nfsd_statfsres *resp = rqstp->rq_resp;
  532. dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh));
  533. resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
  534. NFSD_MAY_BYPASS_GSS_ON_ROOT);
  535. fh_put(&argp->fh);
  536. return rpc_success;
  537. }
  538. /*
  539. * NFSv2 Server procedures.
  540. * Only the results of non-idempotent operations are cached.
  541. */
  542. #define ST 1 /* status */
  543. #define FH 8 /* filehandle */
  544. #define AT 18 /* attributes */
  545. static const struct svc_procedure nfsd_procedures2[18] = {
  546. [NFSPROC_NULL] = {
  547. .pc_func = nfsd_proc_null,
  548. .pc_decode = nfssvc_decode_voidarg,
  549. .pc_encode = nfssvc_encode_voidres,
  550. .pc_argsize = sizeof(struct nfsd_voidargs),
  551. .pc_argzero = sizeof(struct nfsd_voidargs),
  552. .pc_ressize = sizeof(struct nfsd_voidres),
  553. .pc_cachetype = RC_NOCACHE,
  554. .pc_xdrressize = 0,
  555. .pc_name = "NULL",
  556. },
  557. [NFSPROC_GETATTR] = {
  558. .pc_func = nfsd_proc_getattr,
  559. .pc_decode = nfssvc_decode_fhandleargs,
  560. .pc_encode = nfssvc_encode_attrstatres,
  561. .pc_release = nfssvc_release_attrstat,
  562. .pc_argsize = sizeof(struct nfsd_fhandle),
  563. .pc_argzero = sizeof(struct nfsd_fhandle),
  564. .pc_ressize = sizeof(struct nfsd_attrstat),
  565. .pc_cachetype = RC_NOCACHE,
  566. .pc_xdrressize = ST+AT,
  567. .pc_name = "GETATTR",
  568. },
  569. [NFSPROC_SETATTR] = {
  570. .pc_func = nfsd_proc_setattr,
  571. .pc_decode = nfssvc_decode_sattrargs,
  572. .pc_encode = nfssvc_encode_attrstatres,
  573. .pc_release = nfssvc_release_attrstat,
  574. .pc_argsize = sizeof(struct nfsd_sattrargs),
  575. .pc_argzero = sizeof(struct nfsd_sattrargs),
  576. .pc_ressize = sizeof(struct nfsd_attrstat),
  577. .pc_cachetype = RC_REPLBUFF,
  578. .pc_xdrressize = ST+AT,
  579. .pc_name = "SETATTR",
  580. },
  581. [NFSPROC_ROOT] = {
  582. .pc_func = nfsd_proc_root,
  583. .pc_decode = nfssvc_decode_voidarg,
  584. .pc_encode = nfssvc_encode_voidres,
  585. .pc_argsize = sizeof(struct nfsd_voidargs),
  586. .pc_argzero = sizeof(struct nfsd_voidargs),
  587. .pc_ressize = sizeof(struct nfsd_voidres),
  588. .pc_cachetype = RC_NOCACHE,
  589. .pc_xdrressize = 0,
  590. .pc_name = "ROOT",
  591. },
  592. [NFSPROC_LOOKUP] = {
  593. .pc_func = nfsd_proc_lookup,
  594. .pc_decode = nfssvc_decode_diropargs,
  595. .pc_encode = nfssvc_encode_diropres,
  596. .pc_release = nfssvc_release_diropres,
  597. .pc_argsize = sizeof(struct nfsd_diropargs),
  598. .pc_argzero = sizeof(struct nfsd_diropargs),
  599. .pc_ressize = sizeof(struct nfsd_diropres),
  600. .pc_cachetype = RC_NOCACHE,
  601. .pc_xdrressize = ST+FH+AT,
  602. .pc_name = "LOOKUP",
  603. },
  604. [NFSPROC_READLINK] = {
  605. .pc_func = nfsd_proc_readlink,
  606. .pc_decode = nfssvc_decode_fhandleargs,
  607. .pc_encode = nfssvc_encode_readlinkres,
  608. .pc_argsize = sizeof(struct nfsd_fhandle),
  609. .pc_argzero = sizeof(struct nfsd_fhandle),
  610. .pc_ressize = sizeof(struct nfsd_readlinkres),
  611. .pc_cachetype = RC_NOCACHE,
  612. .pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
  613. .pc_name = "READLINK",
  614. },
  615. [NFSPROC_READ] = {
  616. .pc_func = nfsd_proc_read,
  617. .pc_decode = nfssvc_decode_readargs,
  618. .pc_encode = nfssvc_encode_readres,
  619. .pc_release = nfssvc_release_readres,
  620. .pc_argsize = sizeof(struct nfsd_readargs),
  621. .pc_argzero = sizeof(struct nfsd_readargs),
  622. .pc_ressize = sizeof(struct nfsd_readres),
  623. .pc_cachetype = RC_NOCACHE,
  624. .pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
  625. .pc_name = "READ",
  626. },
  627. [NFSPROC_WRITECACHE] = {
  628. .pc_func = nfsd_proc_writecache,
  629. .pc_decode = nfssvc_decode_voidarg,
  630. .pc_encode = nfssvc_encode_voidres,
  631. .pc_argsize = sizeof(struct nfsd_voidargs),
  632. .pc_argzero = sizeof(struct nfsd_voidargs),
  633. .pc_ressize = sizeof(struct nfsd_voidres),
  634. .pc_cachetype = RC_NOCACHE,
  635. .pc_xdrressize = 0,
  636. .pc_name = "WRITECACHE",
  637. },
  638. [NFSPROC_WRITE] = {
  639. .pc_func = nfsd_proc_write,
  640. .pc_decode = nfssvc_decode_writeargs,
  641. .pc_encode = nfssvc_encode_attrstatres,
  642. .pc_release = nfssvc_release_attrstat,
  643. .pc_argsize = sizeof(struct nfsd_writeargs),
  644. .pc_argzero = sizeof(struct nfsd_writeargs),
  645. .pc_ressize = sizeof(struct nfsd_attrstat),
  646. .pc_cachetype = RC_REPLBUFF,
  647. .pc_xdrressize = ST+AT,
  648. .pc_name = "WRITE",
  649. },
  650. [NFSPROC_CREATE] = {
  651. .pc_func = nfsd_proc_create,
  652. .pc_decode = nfssvc_decode_createargs,
  653. .pc_encode = nfssvc_encode_diropres,
  654. .pc_release = nfssvc_release_diropres,
  655. .pc_argsize = sizeof(struct nfsd_createargs),
  656. .pc_argzero = sizeof(struct nfsd_createargs),
  657. .pc_ressize = sizeof(struct nfsd_diropres),
  658. .pc_cachetype = RC_REPLBUFF,
  659. .pc_xdrressize = ST+FH+AT,
  660. .pc_name = "CREATE",
  661. },
  662. [NFSPROC_REMOVE] = {
  663. .pc_func = nfsd_proc_remove,
  664. .pc_decode = nfssvc_decode_diropargs,
  665. .pc_encode = nfssvc_encode_statres,
  666. .pc_argsize = sizeof(struct nfsd_diropargs),
  667. .pc_argzero = sizeof(struct nfsd_diropargs),
  668. .pc_ressize = sizeof(struct nfsd_stat),
  669. .pc_cachetype = RC_REPLSTAT,
  670. .pc_xdrressize = ST,
  671. .pc_name = "REMOVE",
  672. },
  673. [NFSPROC_RENAME] = {
  674. .pc_func = nfsd_proc_rename,
  675. .pc_decode = nfssvc_decode_renameargs,
  676. .pc_encode = nfssvc_encode_statres,
  677. .pc_argsize = sizeof(struct nfsd_renameargs),
  678. .pc_argzero = sizeof(struct nfsd_renameargs),
  679. .pc_ressize = sizeof(struct nfsd_stat),
  680. .pc_cachetype = RC_REPLSTAT,
  681. .pc_xdrressize = ST,
  682. .pc_name = "RENAME",
  683. },
  684. [NFSPROC_LINK] = {
  685. .pc_func = nfsd_proc_link,
  686. .pc_decode = nfssvc_decode_linkargs,
  687. .pc_encode = nfssvc_encode_statres,
  688. .pc_argsize = sizeof(struct nfsd_linkargs),
  689. .pc_argzero = sizeof(struct nfsd_linkargs),
  690. .pc_ressize = sizeof(struct nfsd_stat),
  691. .pc_cachetype = RC_REPLSTAT,
  692. .pc_xdrressize = ST,
  693. .pc_name = "LINK",
  694. },
  695. [NFSPROC_SYMLINK] = {
  696. .pc_func = nfsd_proc_symlink,
  697. .pc_decode = nfssvc_decode_symlinkargs,
  698. .pc_encode = nfssvc_encode_statres,
  699. .pc_argsize = sizeof(struct nfsd_symlinkargs),
  700. .pc_argzero = sizeof(struct nfsd_symlinkargs),
  701. .pc_ressize = sizeof(struct nfsd_stat),
  702. .pc_cachetype = RC_REPLSTAT,
  703. .pc_xdrressize = ST,
  704. .pc_name = "SYMLINK",
  705. },
  706. [NFSPROC_MKDIR] = {
  707. .pc_func = nfsd_proc_mkdir,
  708. .pc_decode = nfssvc_decode_createargs,
  709. .pc_encode = nfssvc_encode_diropres,
  710. .pc_release = nfssvc_release_diropres,
  711. .pc_argsize = sizeof(struct nfsd_createargs),
  712. .pc_argzero = sizeof(struct nfsd_createargs),
  713. .pc_ressize = sizeof(struct nfsd_diropres),
  714. .pc_cachetype = RC_REPLBUFF,
  715. .pc_xdrressize = ST+FH+AT,
  716. .pc_name = "MKDIR",
  717. },
  718. [NFSPROC_RMDIR] = {
  719. .pc_func = nfsd_proc_rmdir,
  720. .pc_decode = nfssvc_decode_diropargs,
  721. .pc_encode = nfssvc_encode_statres,
  722. .pc_argsize = sizeof(struct nfsd_diropargs),
  723. .pc_argzero = sizeof(struct nfsd_diropargs),
  724. .pc_ressize = sizeof(struct nfsd_stat),
  725. .pc_cachetype = RC_REPLSTAT,
  726. .pc_xdrressize = ST,
  727. .pc_name = "RMDIR",
  728. },
  729. [NFSPROC_READDIR] = {
  730. .pc_func = nfsd_proc_readdir,
  731. .pc_decode = nfssvc_decode_readdirargs,
  732. .pc_encode = nfssvc_encode_readdirres,
  733. .pc_argsize = sizeof(struct nfsd_readdirargs),
  734. .pc_argzero = sizeof(struct nfsd_readdirargs),
  735. .pc_ressize = sizeof(struct nfsd_readdirres),
  736. .pc_cachetype = RC_NOCACHE,
  737. .pc_name = "READDIR",
  738. },
  739. [NFSPROC_STATFS] = {
  740. .pc_func = nfsd_proc_statfs,
  741. .pc_decode = nfssvc_decode_fhandleargs,
  742. .pc_encode = nfssvc_encode_statfsres,
  743. .pc_argsize = sizeof(struct nfsd_fhandle),
  744. .pc_argzero = sizeof(struct nfsd_fhandle),
  745. .pc_ressize = sizeof(struct nfsd_statfsres),
  746. .pc_cachetype = RC_NOCACHE,
  747. .pc_xdrressize = ST+5,
  748. .pc_name = "STATFS",
  749. },
  750. };
  751. static unsigned int nfsd_count2[ARRAY_SIZE(nfsd_procedures2)];
  752. const struct svc_version nfsd_version2 = {
  753. .vs_vers = 2,
  754. .vs_nproc = 18,
  755. .vs_proc = nfsd_procedures2,
  756. .vs_count = nfsd_count2,
  757. .vs_dispatch = nfsd_dispatch,
  758. .vs_xdrsize = NFS2_SVC_XDRSIZE,
  759. };
  760. /*
  761. * Map errnos to NFS errnos.
  762. */
  763. __be32
  764. nfserrno (int errno)
  765. {
  766. static struct {
  767. __be32 nfserr;
  768. int syserr;
  769. } nfs_errtbl[] = {
  770. { nfs_ok, 0 },
  771. { nfserr_perm, -EPERM },
  772. { nfserr_noent, -ENOENT },
  773. { nfserr_io, -EIO },
  774. { nfserr_nxio, -ENXIO },
  775. { nfserr_fbig, -E2BIG },
  776. { nfserr_stale, -EBADF },
  777. { nfserr_acces, -EACCES },
  778. { nfserr_exist, -EEXIST },
  779. { nfserr_xdev, -EXDEV },
  780. { nfserr_mlink, -EMLINK },
  781. { nfserr_nodev, -ENODEV },
  782. { nfserr_notdir, -ENOTDIR },
  783. { nfserr_isdir, -EISDIR },
  784. { nfserr_inval, -EINVAL },
  785. { nfserr_fbig, -EFBIG },
  786. { nfserr_nospc, -ENOSPC },
  787. { nfserr_rofs, -EROFS },
  788. { nfserr_mlink, -EMLINK },
  789. { nfserr_nametoolong, -ENAMETOOLONG },
  790. { nfserr_notempty, -ENOTEMPTY },
  791. #ifdef EDQUOT
  792. { nfserr_dquot, -EDQUOT },
  793. #endif
  794. { nfserr_stale, -ESTALE },
  795. { nfserr_jukebox, -ETIMEDOUT },
  796. { nfserr_jukebox, -ERESTARTSYS },
  797. { nfserr_jukebox, -EAGAIN },
  798. { nfserr_jukebox, -EWOULDBLOCK },
  799. { nfserr_jukebox, -ENOMEM },
  800. { nfserr_io, -ETXTBSY },
  801. { nfserr_notsupp, -EOPNOTSUPP },
  802. { nfserr_toosmall, -ETOOSMALL },
  803. { nfserr_serverfault, -ESERVERFAULT },
  804. { nfserr_serverfault, -ENFILE },
  805. { nfserr_io, -EREMOTEIO },
  806. { nfserr_stale, -EOPENSTALE },
  807. { nfserr_io, -EUCLEAN },
  808. { nfserr_perm, -ENOKEY },
  809. { nfserr_no_grace, -ENOGRACE},
  810. };
  811. int i;
  812. for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
  813. if (nfs_errtbl[i].syserr == errno)
  814. return nfs_errtbl[i].nfserr;
  815. }
  816. WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno);
  817. return nfserr_io;
  818. }