nfsproc.c 24 KB

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