nfs3proc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Process version 3 NFS requests.
  4. *
  5. * Copyright (C) 1996, 1997, 1998 Olaf Kirch <okir@monad.swb.de>
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/ext2_fs.h>
  9. #include <linux/magic.h>
  10. #include <linux/namei.h>
  11. #include "cache.h"
  12. #include "xdr3.h"
  13. #include "vfs.h"
  14. #include "filecache.h"
  15. #include "trace.h"
  16. #define NFSDDBG_FACILITY NFSDDBG_PROC
  17. static int nfs3_ftypes[] = {
  18. 0, /* NF3NON */
  19. S_IFREG, /* NF3REG */
  20. S_IFDIR, /* NF3DIR */
  21. S_IFBLK, /* NF3BLK */
  22. S_IFCHR, /* NF3CHR */
  23. S_IFLNK, /* NF3LNK */
  24. S_IFSOCK, /* NF3SOCK */
  25. S_IFIFO, /* NF3FIFO */
  26. };
  27. static __be32 nfsd3_map_status(__be32 status)
  28. {
  29. switch (status) {
  30. case nfs_ok:
  31. break;
  32. case nfserr_nofilehandle:
  33. status = nfserr_badhandle;
  34. break;
  35. case nfserr_wrongsec:
  36. case nfserr_file_open:
  37. status = nfserr_acces;
  38. break;
  39. case nfserr_symlink_not_dir:
  40. status = nfserr_notdir;
  41. break;
  42. case nfserr_symlink:
  43. case nfserr_wrong_type:
  44. status = nfserr_inval;
  45. break;
  46. }
  47. return status;
  48. }
  49. /*
  50. * NULL call.
  51. */
  52. static __be32
  53. nfsd3_proc_null(struct svc_rqst *rqstp)
  54. {
  55. return rpc_success;
  56. }
  57. /*
  58. * Get a file's attributes
  59. */
  60. static __be32
  61. nfsd3_proc_getattr(struct svc_rqst *rqstp)
  62. {
  63. struct nfsd_fhandle *argp = rqstp->rq_argp;
  64. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  65. trace_nfsd_vfs_getattr(rqstp, &argp->fh);
  66. fh_copy(&resp->fh, &argp->fh);
  67. resp->status = fh_verify(rqstp, &resp->fh, 0,
  68. NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
  69. if (resp->status != nfs_ok)
  70. goto out;
  71. resp->status = fh_getattr(&resp->fh, &resp->stat);
  72. out:
  73. resp->status = nfsd3_map_status(resp->status);
  74. return rpc_success;
  75. }
  76. /*
  77. * Set a file's attributes
  78. */
  79. static __be32
  80. nfsd3_proc_setattr(struct svc_rqst *rqstp)
  81. {
  82. struct nfsd3_sattrargs *argp = rqstp->rq_argp;
  83. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  84. struct nfsd_attrs attrs = {
  85. .na_iattr = &argp->attrs,
  86. };
  87. const struct timespec64 *guardtime = NULL;
  88. dprintk("nfsd: SETATTR(3) %s\n",
  89. SVCFH_fmt(&argp->fh));
  90. fh_copy(&resp->fh, &argp->fh);
  91. if (argp->check_guard)
  92. guardtime = &argp->guardtime;
  93. resp->status = nfsd_setattr(rqstp, &resp->fh, &attrs, guardtime);
  94. resp->status = nfsd3_map_status(resp->status);
  95. return rpc_success;
  96. }
  97. /*
  98. * Look up a path name component
  99. */
  100. static __be32
  101. nfsd3_proc_lookup(struct svc_rqst *rqstp)
  102. {
  103. struct nfsd3_diropargs *argp = rqstp->rq_argp;
  104. struct nfsd3_diropres *resp = rqstp->rq_resp;
  105. dprintk("nfsd: LOOKUP(3) %s %.*s\n",
  106. SVCFH_fmt(&argp->fh),
  107. argp->len,
  108. argp->name);
  109. fh_copy(&resp->dirfh, &argp->fh);
  110. fh_init(&resp->fh, NFS3_FHSIZE);
  111. resp->status = nfsd_lookup(rqstp, &resp->dirfh,
  112. argp->name, argp->len,
  113. &resp->fh);
  114. resp->status = nfsd3_map_status(resp->status);
  115. return rpc_success;
  116. }
  117. /*
  118. * Check file access
  119. */
  120. static __be32
  121. nfsd3_proc_access(struct svc_rqst *rqstp)
  122. {
  123. struct nfsd3_accessargs *argp = rqstp->rq_argp;
  124. struct nfsd3_accessres *resp = rqstp->rq_resp;
  125. dprintk("nfsd: ACCESS(3) %s 0x%x\n",
  126. SVCFH_fmt(&argp->fh),
  127. argp->access);
  128. fh_copy(&resp->fh, &argp->fh);
  129. resp->access = argp->access;
  130. resp->status = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
  131. resp->status = nfsd3_map_status(resp->status);
  132. return rpc_success;
  133. }
  134. /*
  135. * Read a symlink.
  136. */
  137. static __be32
  138. nfsd3_proc_readlink(struct svc_rqst *rqstp)
  139. {
  140. struct nfsd_fhandle *argp = rqstp->rq_argp;
  141. struct nfsd3_readlinkres *resp = rqstp->rq_resp;
  142. dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
  143. /* Read the symlink. */
  144. fh_copy(&resp->fh, &argp->fh);
  145. resp->len = NFS3_MAXPATHLEN;
  146. resp->pages = rqstp->rq_next_page++;
  147. resp->status = nfsd_readlink(rqstp, &resp->fh,
  148. page_address(*resp->pages), &resp->len);
  149. resp->status = nfsd3_map_status(resp->status);
  150. return rpc_success;
  151. }
  152. /*
  153. * Read a portion of a file.
  154. */
  155. static __be32
  156. nfsd3_proc_read(struct svc_rqst *rqstp)
  157. {
  158. struct nfsd3_readargs *argp = rqstp->rq_argp;
  159. struct nfsd3_readres *resp = rqstp->rq_resp;
  160. dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
  161. SVCFH_fmt(&argp->fh),
  162. (unsigned long) argp->count,
  163. (unsigned long long) argp->offset);
  164. argp->count = min_t(u32, argp->count, svc_max_payload(rqstp));
  165. argp->count = min_t(u32, argp->count, rqstp->rq_res.buflen);
  166. if (argp->offset > (u64)OFFSET_MAX)
  167. argp->offset = (u64)OFFSET_MAX;
  168. if (argp->offset + argp->count > (u64)OFFSET_MAX)
  169. argp->count = (u64)OFFSET_MAX - argp->offset;
  170. resp->pages = rqstp->rq_next_page;
  171. /* Obtain buffer pointer for payload.
  172. * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
  173. * + 1 (xdr opaque byte count) = 26
  174. */
  175. resp->count = argp->count;
  176. svc_reserve_auth(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3) << 2) +
  177. resp->count + 4);
  178. fh_copy(&resp->fh, &argp->fh);
  179. resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
  180. &resp->count, &resp->eof);
  181. resp->status = nfsd3_map_status(resp->status);
  182. return rpc_success;
  183. }
  184. /*
  185. * Write data to a file
  186. */
  187. static __be32
  188. nfsd3_proc_write(struct svc_rqst *rqstp)
  189. {
  190. struct nfsd3_writeargs *argp = rqstp->rq_argp;
  191. struct nfsd3_writeres *resp = rqstp->rq_resp;
  192. unsigned long cnt = argp->len;
  193. dprintk("nfsd: WRITE(3) %s %d bytes at %Lu%s\n",
  194. SVCFH_fmt(&argp->fh),
  195. argp->len,
  196. (unsigned long long) argp->offset,
  197. argp->stable ? " stable" : "");
  198. resp->status = nfserr_fbig;
  199. if (argp->offset > (u64)OFFSET_MAX ||
  200. argp->offset + argp->len > (u64)OFFSET_MAX)
  201. return rpc_success;
  202. fh_copy(&resp->fh, &argp->fh);
  203. resp->committed = argp->stable;
  204. resp->status = nfsd_write(rqstp, &resp->fh, argp->offset,
  205. &argp->payload, &cnt,
  206. resp->committed, resp->verf);
  207. resp->count = cnt;
  208. resp->status = nfsd3_map_status(resp->status);
  209. return rpc_success;
  210. }
  211. /*
  212. * Implement NFSv3's unchecked, guarded, and exclusive CREATE
  213. * semantics for regular files. Except for the created file,
  214. * this operation is stateless on the server.
  215. *
  216. * Upon return, caller must release @fhp and @resfhp.
  217. */
  218. static __be32
  219. nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
  220. struct svc_fh *resfhp, struct nfsd3_createargs *argp)
  221. {
  222. struct iattr *iap = &argp->attrs;
  223. struct dentry *parent, *child;
  224. struct nfsd_attrs attrs = {
  225. .na_iattr = iap,
  226. };
  227. __u32 v_mtime, v_atime;
  228. struct inode *inode;
  229. __be32 status;
  230. int host_err;
  231. trace_nfsd_vfs_create(rqstp, fhp, S_IFREG, argp->name, argp->len);
  232. if (isdotent(argp->name, argp->len))
  233. return nfserr_exist;
  234. if (!(iap->ia_valid & ATTR_MODE))
  235. iap->ia_mode = 0;
  236. status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  237. if (status != nfs_ok)
  238. return status;
  239. parent = fhp->fh_dentry;
  240. inode = d_inode(parent);
  241. host_err = fh_want_write(fhp);
  242. if (host_err)
  243. return nfserrno(host_err);
  244. child = start_creating(&nop_mnt_idmap, parent,
  245. &QSTR_LEN(argp->name, argp->len));
  246. if (IS_ERR(child)) {
  247. status = nfserrno(PTR_ERR(child));
  248. goto out_write;
  249. }
  250. if (d_really_is_negative(child)) {
  251. status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  252. if (status != nfs_ok)
  253. goto out;
  254. }
  255. status = fh_compose(resfhp, fhp->fh_export, child, fhp);
  256. if (status != nfs_ok)
  257. goto out;
  258. v_mtime = 0;
  259. v_atime = 0;
  260. if (argp->createmode == NFS3_CREATE_EXCLUSIVE) {
  261. u32 *verifier = (u32 *)argp->verf;
  262. /*
  263. * Solaris 7 gets confused (bugid 4218508) if these have
  264. * the high bit set, as do xfs filesystems without the
  265. * "bigtime" feature. So just clear the high bits.
  266. */
  267. v_mtime = verifier[0] & 0x7fffffff;
  268. v_atime = verifier[1] & 0x7fffffff;
  269. }
  270. if (d_really_is_positive(child)) {
  271. status = nfs_ok;
  272. switch (argp->createmode) {
  273. case NFS3_CREATE_UNCHECKED:
  274. if (!d_is_reg(child))
  275. break;
  276. iap->ia_valid &= ATTR_SIZE;
  277. goto set_attr;
  278. case NFS3_CREATE_GUARDED:
  279. status = nfserr_exist;
  280. break;
  281. case NFS3_CREATE_EXCLUSIVE:
  282. if (inode_get_mtime_sec(d_inode(child)) == v_mtime &&
  283. inode_get_atime_sec(d_inode(child)) == v_atime &&
  284. d_inode(child)->i_size == 0) {
  285. break;
  286. }
  287. status = nfserr_exist;
  288. }
  289. goto out;
  290. }
  291. if (!IS_POSIXACL(inode))
  292. iap->ia_mode &= ~current_umask();
  293. status = fh_fill_pre_attrs(fhp);
  294. if (status != nfs_ok)
  295. goto out;
  296. host_err = vfs_create(&nop_mnt_idmap, child, iap->ia_mode, NULL);
  297. if (host_err < 0) {
  298. status = nfserrno(host_err);
  299. goto out;
  300. }
  301. fh_fill_post_attrs(fhp);
  302. /* A newly created file already has a file size of zero. */
  303. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  304. iap->ia_valid &= ~ATTR_SIZE;
  305. if (argp->createmode == NFS3_CREATE_EXCLUSIVE) {
  306. iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
  307. ATTR_MTIME_SET | ATTR_ATIME_SET;
  308. iap->ia_mtime.tv_sec = v_mtime;
  309. iap->ia_atime.tv_sec = v_atime;
  310. iap->ia_mtime.tv_nsec = 0;
  311. iap->ia_atime.tv_nsec = 0;
  312. }
  313. set_attr:
  314. status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
  315. out:
  316. end_creating(child);
  317. out_write:
  318. fh_drop_write(fhp);
  319. return status;
  320. }
  321. static __be32
  322. nfsd3_proc_create(struct svc_rqst *rqstp)
  323. {
  324. struct nfsd3_createargs *argp = rqstp->rq_argp;
  325. struct nfsd3_diropres *resp = rqstp->rq_resp;
  326. svc_fh *dirfhp, *newfhp;
  327. dirfhp = fh_copy(&resp->dirfh, &argp->fh);
  328. newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
  329. resp->status = nfsd3_create_file(rqstp, dirfhp, newfhp, argp);
  330. resp->status = nfsd3_map_status(resp->status);
  331. return rpc_success;
  332. }
  333. /*
  334. * Make directory. This operation is not idempotent.
  335. */
  336. static __be32
  337. nfsd3_proc_mkdir(struct svc_rqst *rqstp)
  338. {
  339. struct nfsd3_createargs *argp = rqstp->rq_argp;
  340. struct nfsd3_diropres *resp = rqstp->rq_resp;
  341. struct nfsd_attrs attrs = {
  342. .na_iattr = &argp->attrs,
  343. };
  344. argp->attrs.ia_valid &= ~ATTR_SIZE;
  345. fh_copy(&resp->dirfh, &argp->fh);
  346. fh_init(&resp->fh, NFS3_FHSIZE);
  347. resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
  348. &attrs, S_IFDIR, 0, &resp->fh);
  349. resp->status = nfsd3_map_status(resp->status);
  350. return rpc_success;
  351. }
  352. static __be32
  353. nfsd3_proc_symlink(struct svc_rqst *rqstp)
  354. {
  355. struct nfsd3_symlinkargs *argp = rqstp->rq_argp;
  356. struct nfsd3_diropres *resp = rqstp->rq_resp;
  357. struct nfsd_attrs attrs = {
  358. .na_iattr = &argp->attrs,
  359. };
  360. if (argp->tlen == 0) {
  361. resp->status = nfserr_inval;
  362. goto out;
  363. }
  364. if (argp->tlen > NFS3_MAXPATHLEN) {
  365. resp->status = nfserr_nametoolong;
  366. goto out;
  367. }
  368. argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
  369. page_address(rqstp->rq_arg.pages[0]),
  370. argp->tlen);
  371. if (IS_ERR(argp->tname)) {
  372. resp->status = nfserrno(PTR_ERR(argp->tname));
  373. goto out;
  374. }
  375. fh_copy(&resp->dirfh, &argp->ffh);
  376. fh_init(&resp->fh, NFS3_FHSIZE);
  377. resp->status = nfsd_symlink(rqstp, &resp->dirfh, argp->fname,
  378. argp->flen, argp->tname, &attrs, &resp->fh);
  379. kfree(argp->tname);
  380. out:
  381. resp->status = nfsd3_map_status(resp->status);
  382. return rpc_success;
  383. }
  384. /*
  385. * Make socket/fifo/device.
  386. */
  387. static __be32
  388. nfsd3_proc_mknod(struct svc_rqst *rqstp)
  389. {
  390. struct nfsd3_mknodargs *argp = rqstp->rq_argp;
  391. struct nfsd3_diropres *resp = rqstp->rq_resp;
  392. struct nfsd_attrs attrs = {
  393. .na_iattr = &argp->attrs,
  394. };
  395. int type;
  396. dev_t rdev = 0;
  397. fh_copy(&resp->dirfh, &argp->fh);
  398. fh_init(&resp->fh, NFS3_FHSIZE);
  399. if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
  400. rdev = MKDEV(argp->major, argp->minor);
  401. if (MAJOR(rdev) != argp->major ||
  402. MINOR(rdev) != argp->minor) {
  403. resp->status = nfserr_inval;
  404. goto out;
  405. }
  406. } else if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO) {
  407. resp->status = nfserr_badtype;
  408. goto out;
  409. }
  410. type = nfs3_ftypes[argp->ftype];
  411. resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
  412. &attrs, type, rdev, &resp->fh);
  413. out:
  414. resp->status = nfsd3_map_status(resp->status);
  415. return rpc_success;
  416. }
  417. /*
  418. * Remove file/fifo/socket etc.
  419. */
  420. static __be32
  421. nfsd3_proc_remove(struct svc_rqst *rqstp)
  422. {
  423. struct nfsd3_diropargs *argp = rqstp->rq_argp;
  424. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  425. /* Unlink. -S_IFDIR means file must not be a directory */
  426. fh_copy(&resp->fh, &argp->fh);
  427. resp->status = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR,
  428. argp->name, argp->len);
  429. resp->status = nfsd3_map_status(resp->status);
  430. return rpc_success;
  431. }
  432. /*
  433. * Remove a directory
  434. */
  435. static __be32
  436. nfsd3_proc_rmdir(struct svc_rqst *rqstp)
  437. {
  438. struct nfsd3_diropargs *argp = rqstp->rq_argp;
  439. struct nfsd3_attrstat *resp = rqstp->rq_resp;
  440. fh_copy(&resp->fh, &argp->fh);
  441. resp->status = nfsd_unlink(rqstp, &resp->fh, S_IFDIR,
  442. argp->name, argp->len);
  443. resp->status = nfsd3_map_status(resp->status);
  444. return rpc_success;
  445. }
  446. static __be32
  447. nfsd3_proc_rename(struct svc_rqst *rqstp)
  448. {
  449. struct nfsd3_renameargs *argp = rqstp->rq_argp;
  450. struct nfsd3_renameres *resp = rqstp->rq_resp;
  451. fh_copy(&resp->ffh, &argp->ffh);
  452. fh_copy(&resp->tfh, &argp->tfh);
  453. resp->status = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
  454. &resp->tfh, argp->tname, argp->tlen);
  455. resp->status = nfsd3_map_status(resp->status);
  456. return rpc_success;
  457. }
  458. static __be32
  459. nfsd3_proc_link(struct svc_rqst *rqstp)
  460. {
  461. struct nfsd3_linkargs *argp = rqstp->rq_argp;
  462. struct nfsd3_linkres *resp = rqstp->rq_resp;
  463. fh_copy(&resp->fh, &argp->ffh);
  464. fh_copy(&resp->tfh, &argp->tfh);
  465. resp->status = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
  466. &resp->fh);
  467. resp->status = nfsd3_map_status(resp->status);
  468. return rpc_success;
  469. }
  470. static void nfsd3_init_dirlist_pages(struct svc_rqst *rqstp,
  471. struct nfsd3_readdirres *resp,
  472. u32 count)
  473. {
  474. struct xdr_buf *buf = &resp->dirlist;
  475. struct xdr_stream *xdr = &resp->xdr;
  476. unsigned int sendbuf = min_t(unsigned int, rqstp->rq_res.buflen,
  477. svc_max_payload(rqstp));
  478. memset(buf, 0, sizeof(*buf));
  479. /* Reserve room for the NULL ptr & eof flag (-2 words) */
  480. buf->buflen = clamp(count, (u32)(XDR_UNIT * 2), sendbuf);
  481. buf->buflen -= XDR_UNIT * 2;
  482. buf->pages = rqstp->rq_next_page;
  483. rqstp->rq_next_page += (buf->buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  484. xdr_init_encode_pages(xdr, buf);
  485. }
  486. /*
  487. * Read a portion of a directory.
  488. */
  489. static __be32
  490. nfsd3_proc_readdir(struct svc_rqst *rqstp)
  491. {
  492. struct nfsd3_readdirargs *argp = rqstp->rq_argp;
  493. struct nfsd3_readdirres *resp = rqstp->rq_resp;
  494. loff_t offset;
  495. trace_nfsd_vfs_readdir(rqstp, &argp->fh, argp->count, argp->cookie);
  496. nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
  497. fh_copy(&resp->fh, &argp->fh);
  498. resp->common.err = nfs_ok;
  499. resp->cookie_offset = 0;
  500. resp->rqstp = rqstp;
  501. offset = argp->cookie;
  502. resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
  503. &resp->common, nfs3svc_encode_entry3);
  504. memcpy(resp->verf, argp->verf, 8);
  505. nfs3svc_encode_cookie3(resp, offset);
  506. /* Recycle only pages that were part of the reply */
  507. rqstp->rq_next_page = resp->xdr.page_ptr + 1;
  508. resp->status = nfsd3_map_status(resp->status);
  509. return rpc_success;
  510. }
  511. /*
  512. * Read a portion of a directory, including file handles and attrs.
  513. * For now, we choose to ignore the dircount parameter.
  514. */
  515. static __be32
  516. nfsd3_proc_readdirplus(struct svc_rqst *rqstp)
  517. {
  518. struct nfsd3_readdirargs *argp = rqstp->rq_argp;
  519. struct nfsd3_readdirres *resp = rqstp->rq_resp;
  520. loff_t offset;
  521. trace_nfsd_vfs_readdir(rqstp, &argp->fh, argp->count, argp->cookie);
  522. nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
  523. fh_copy(&resp->fh, &argp->fh);
  524. resp->common.err = nfs_ok;
  525. resp->cookie_offset = 0;
  526. resp->rqstp = rqstp;
  527. offset = argp->cookie;
  528. resp->status = fh_verify(rqstp, &resp->fh, S_IFDIR, NFSD_MAY_NOP);
  529. if (resp->status != nfs_ok)
  530. goto out;
  531. if (resp->fh.fh_export->ex_flags & NFSEXP_NOREADDIRPLUS) {
  532. resp->status = nfserr_notsupp;
  533. goto out;
  534. }
  535. resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
  536. &resp->common, nfs3svc_encode_entryplus3);
  537. memcpy(resp->verf, argp->verf, 8);
  538. nfs3svc_encode_cookie3(resp, offset);
  539. /* Recycle only pages that were part of the reply */
  540. rqstp->rq_next_page = resp->xdr.page_ptr + 1;
  541. out:
  542. resp->status = nfsd3_map_status(resp->status);
  543. return rpc_success;
  544. }
  545. /*
  546. * Get file system stats
  547. */
  548. static __be32
  549. nfsd3_proc_fsstat(struct svc_rqst *rqstp)
  550. {
  551. struct nfsd_fhandle *argp = rqstp->rq_argp;
  552. struct nfsd3_fsstatres *resp = rqstp->rq_resp;
  553. resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats, 0);
  554. fh_put(&argp->fh);
  555. resp->status = nfsd3_map_status(resp->status);
  556. return rpc_success;
  557. }
  558. /*
  559. * Get file system info
  560. */
  561. static __be32
  562. nfsd3_proc_fsinfo(struct svc_rqst *rqstp)
  563. {
  564. struct nfsd_fhandle *argp = rqstp->rq_argp;
  565. struct nfsd3_fsinfores *resp = rqstp->rq_resp;
  566. u32 max_blocksize = svc_max_payload(rqstp);
  567. dprintk("nfsd: FSINFO(3) %s\n",
  568. SVCFH_fmt(&argp->fh));
  569. resp->f_rtmax = max_blocksize;
  570. resp->f_rtpref = max_blocksize;
  571. resp->f_rtmult = PAGE_SIZE;
  572. resp->f_wtmax = max_blocksize;
  573. resp->f_wtpref = max_blocksize;
  574. resp->f_wtmult = PAGE_SIZE;
  575. resp->f_dtpref = max_blocksize;
  576. resp->f_maxfilesize = ~(u32) 0;
  577. resp->f_properties = NFS3_FSF_DEFAULT;
  578. resp->status = fh_verify(rqstp, &argp->fh, 0,
  579. NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
  580. /* Check special features of the file system. May request
  581. * different read/write sizes for file systems known to have
  582. * problems with large blocks */
  583. if (resp->status == nfs_ok) {
  584. struct super_block *sb = argp->fh.fh_dentry->d_sb;
  585. /* Note that we don't care for remote fs's here */
  586. if (sb->s_magic == MSDOS_SUPER_MAGIC) {
  587. resp->f_properties = NFS3_FSF_BILLYBOY;
  588. }
  589. resp->f_maxfilesize = sb->s_maxbytes;
  590. }
  591. fh_put(&argp->fh);
  592. resp->status = nfsd3_map_status(resp->status);
  593. return rpc_success;
  594. }
  595. /*
  596. * Get pathconf info for the specified file
  597. */
  598. static __be32
  599. nfsd3_proc_pathconf(struct svc_rqst *rqstp)
  600. {
  601. struct nfsd_fhandle *argp = rqstp->rq_argp;
  602. struct nfsd3_pathconfres *resp = rqstp->rq_resp;
  603. dprintk("nfsd: PATHCONF(3) %s\n",
  604. SVCFH_fmt(&argp->fh));
  605. /* Set default pathconf */
  606. resp->p_link_max = 255; /* at least */
  607. resp->p_name_max = 255; /* at least */
  608. resp->p_no_trunc = 0;
  609. resp->p_chown_restricted = 1;
  610. resp->p_case_insensitive = 0;
  611. resp->p_case_preserving = 1;
  612. resp->status = fh_verify(rqstp, &argp->fh, 0, NFSD_MAY_NOP);
  613. if (resp->status == nfs_ok) {
  614. struct super_block *sb = argp->fh.fh_dentry->d_sb;
  615. /* Note that we don't care for remote fs's here */
  616. switch (sb->s_magic) {
  617. case EXT2_SUPER_MAGIC:
  618. resp->p_link_max = EXT2_LINK_MAX;
  619. resp->p_name_max = EXT2_NAME_LEN;
  620. break;
  621. case MSDOS_SUPER_MAGIC:
  622. resp->p_case_insensitive = 1;
  623. resp->p_case_preserving = 0;
  624. break;
  625. }
  626. }
  627. fh_put(&argp->fh);
  628. resp->status = nfsd3_map_status(resp->status);
  629. return rpc_success;
  630. }
  631. /*
  632. * Commit a file (range) to stable storage.
  633. */
  634. static __be32
  635. nfsd3_proc_commit(struct svc_rqst *rqstp)
  636. {
  637. struct nfsd3_commitargs *argp = rqstp->rq_argp;
  638. struct nfsd3_commitres *resp = rqstp->rq_resp;
  639. struct nfsd_file *nf;
  640. dprintk("nfsd: COMMIT(3) %s %u@%Lu\n",
  641. SVCFH_fmt(&argp->fh),
  642. argp->count,
  643. (unsigned long long) argp->offset);
  644. fh_copy(&resp->fh, &argp->fh);
  645. resp->status = nfsd_file_acquire_gc(rqstp, &resp->fh, NFSD_MAY_WRITE |
  646. NFSD_MAY_NOT_BREAK_LEASE, &nf);
  647. if (resp->status)
  648. goto out;
  649. resp->status = nfsd_commit(rqstp, &resp->fh, nf, argp->offset,
  650. argp->count, resp->verf);
  651. nfsd_file_put(nf);
  652. out:
  653. resp->status = nfsd3_map_status(resp->status);
  654. return rpc_success;
  655. }
  656. /*
  657. * NFSv3 Server procedures.
  658. * Only the results of non-idempotent operations are cached.
  659. */
  660. #define nfs3svc_encode_attrstatres nfs3svc_encode_attrstat
  661. #define nfs3svc_encode_wccstatres nfs3svc_encode_wccstat
  662. #define nfsd3_mkdirargs nfsd3_createargs
  663. #define nfsd3_readdirplusargs nfsd3_readdirargs
  664. #define nfsd3_fhandleargs nfsd_fhandle
  665. #define nfsd3_attrstatres nfsd3_attrstat
  666. #define nfsd3_wccstatres nfsd3_attrstat
  667. #define nfsd3_createres nfsd3_diropres
  668. #define ST 1 /* status*/
  669. #define FH 17 /* filehandle with length */
  670. #define AT 21 /* attributes */
  671. #define pAT (1+AT) /* post attributes - conditional */
  672. #define WC (7+pAT) /* WCC attributes */
  673. static const struct svc_procedure nfsd_procedures3[22] = {
  674. [NFS3PROC_NULL] = {
  675. .pc_func = nfsd3_proc_null,
  676. .pc_decode = nfssvc_decode_voidarg,
  677. .pc_encode = nfssvc_encode_voidres,
  678. .pc_argsize = sizeof(struct nfsd_voidargs),
  679. .pc_argzero = sizeof(struct nfsd_voidargs),
  680. .pc_ressize = sizeof(struct nfsd_voidres),
  681. .pc_cachetype = RC_NOCACHE,
  682. .pc_xdrressize = ST,
  683. .pc_name = "NULL",
  684. },
  685. [NFS3PROC_GETATTR] = {
  686. .pc_func = nfsd3_proc_getattr,
  687. .pc_decode = nfs3svc_decode_fhandleargs,
  688. .pc_encode = nfs3svc_encode_getattrres,
  689. .pc_release = nfs3svc_release_fhandle,
  690. .pc_argsize = sizeof(struct nfsd_fhandle),
  691. .pc_argzero = sizeof(struct nfsd_fhandle),
  692. .pc_ressize = sizeof(struct nfsd3_attrstatres),
  693. .pc_cachetype = RC_NOCACHE,
  694. .pc_xdrressize = ST+AT,
  695. .pc_name = "GETATTR",
  696. },
  697. [NFS3PROC_SETATTR] = {
  698. .pc_func = nfsd3_proc_setattr,
  699. .pc_decode = nfs3svc_decode_sattrargs,
  700. .pc_encode = nfs3svc_encode_wccstatres,
  701. .pc_release = nfs3svc_release_fhandle,
  702. .pc_argsize = sizeof(struct nfsd3_sattrargs),
  703. .pc_argzero = sizeof(struct nfsd3_sattrargs),
  704. .pc_ressize = sizeof(struct nfsd3_wccstatres),
  705. .pc_cachetype = RC_REPLBUFF,
  706. .pc_xdrressize = ST+WC,
  707. .pc_name = "SETATTR",
  708. },
  709. [NFS3PROC_LOOKUP] = {
  710. .pc_func = nfsd3_proc_lookup,
  711. .pc_decode = nfs3svc_decode_diropargs,
  712. .pc_encode = nfs3svc_encode_lookupres,
  713. .pc_release = nfs3svc_release_fhandle2,
  714. .pc_argsize = sizeof(struct nfsd3_diropargs),
  715. .pc_argzero = sizeof(struct nfsd3_diropargs),
  716. .pc_ressize = sizeof(struct nfsd3_diropres),
  717. .pc_cachetype = RC_NOCACHE,
  718. .pc_xdrressize = ST+FH+pAT+pAT,
  719. .pc_name = "LOOKUP",
  720. },
  721. [NFS3PROC_ACCESS] = {
  722. .pc_func = nfsd3_proc_access,
  723. .pc_decode = nfs3svc_decode_accessargs,
  724. .pc_encode = nfs3svc_encode_accessres,
  725. .pc_release = nfs3svc_release_fhandle,
  726. .pc_argsize = sizeof(struct nfsd3_accessargs),
  727. .pc_argzero = sizeof(struct nfsd3_accessargs),
  728. .pc_ressize = sizeof(struct nfsd3_accessres),
  729. .pc_cachetype = RC_NOCACHE,
  730. .pc_xdrressize = ST+pAT+1,
  731. .pc_name = "ACCESS",
  732. },
  733. [NFS3PROC_READLINK] = {
  734. .pc_func = nfsd3_proc_readlink,
  735. .pc_decode = nfs3svc_decode_fhandleargs,
  736. .pc_encode = nfs3svc_encode_readlinkres,
  737. .pc_release = nfs3svc_release_fhandle,
  738. .pc_argsize = sizeof(struct nfsd_fhandle),
  739. .pc_argzero = sizeof(struct nfsd_fhandle),
  740. .pc_ressize = sizeof(struct nfsd3_readlinkres),
  741. .pc_cachetype = RC_NOCACHE,
  742. .pc_xdrressize = ST+pAT+1+NFS3_MAXPATHLEN/4,
  743. .pc_name = "READLINK",
  744. },
  745. [NFS3PROC_READ] = {
  746. .pc_func = nfsd3_proc_read,
  747. .pc_decode = nfs3svc_decode_readargs,
  748. .pc_encode = nfs3svc_encode_readres,
  749. .pc_release = nfs3svc_release_fhandle,
  750. .pc_argsize = sizeof(struct nfsd3_readargs),
  751. .pc_argzero = sizeof(struct nfsd3_readargs),
  752. .pc_ressize = sizeof(struct nfsd3_readres),
  753. .pc_cachetype = RC_NOCACHE,
  754. .pc_xdrressize = ST+pAT+4+NFSSVC_MAXBLKSIZE/4,
  755. .pc_name = "READ",
  756. },
  757. [NFS3PROC_WRITE] = {
  758. .pc_func = nfsd3_proc_write,
  759. .pc_decode = nfs3svc_decode_writeargs,
  760. .pc_encode = nfs3svc_encode_writeres,
  761. .pc_release = nfs3svc_release_fhandle,
  762. .pc_argsize = sizeof(struct nfsd3_writeargs),
  763. .pc_argzero = sizeof(struct nfsd3_writeargs),
  764. .pc_ressize = sizeof(struct nfsd3_writeres),
  765. .pc_cachetype = RC_REPLBUFF,
  766. .pc_xdrressize = ST+WC+4,
  767. .pc_name = "WRITE",
  768. },
  769. [NFS3PROC_CREATE] = {
  770. .pc_func = nfsd3_proc_create,
  771. .pc_decode = nfs3svc_decode_createargs,
  772. .pc_encode = nfs3svc_encode_createres,
  773. .pc_release = nfs3svc_release_fhandle2,
  774. .pc_argsize = sizeof(struct nfsd3_createargs),
  775. .pc_argzero = sizeof(struct nfsd3_createargs),
  776. .pc_ressize = sizeof(struct nfsd3_createres),
  777. .pc_cachetype = RC_REPLBUFF,
  778. .pc_xdrressize = ST+(1+FH+pAT)+WC,
  779. .pc_name = "CREATE",
  780. },
  781. [NFS3PROC_MKDIR] = {
  782. .pc_func = nfsd3_proc_mkdir,
  783. .pc_decode = nfs3svc_decode_mkdirargs,
  784. .pc_encode = nfs3svc_encode_createres,
  785. .pc_release = nfs3svc_release_fhandle2,
  786. .pc_argsize = sizeof(struct nfsd3_mkdirargs),
  787. .pc_argzero = sizeof(struct nfsd3_mkdirargs),
  788. .pc_ressize = sizeof(struct nfsd3_createres),
  789. .pc_cachetype = RC_REPLBUFF,
  790. .pc_xdrressize = ST+(1+FH+pAT)+WC,
  791. .pc_name = "MKDIR",
  792. },
  793. [NFS3PROC_SYMLINK] = {
  794. .pc_func = nfsd3_proc_symlink,
  795. .pc_decode = nfs3svc_decode_symlinkargs,
  796. .pc_encode = nfs3svc_encode_createres,
  797. .pc_release = nfs3svc_release_fhandle2,
  798. .pc_argsize = sizeof(struct nfsd3_symlinkargs),
  799. .pc_argzero = sizeof(struct nfsd3_symlinkargs),
  800. .pc_ressize = sizeof(struct nfsd3_createres),
  801. .pc_cachetype = RC_REPLBUFF,
  802. .pc_xdrressize = ST+(1+FH+pAT)+WC,
  803. .pc_name = "SYMLINK",
  804. },
  805. [NFS3PROC_MKNOD] = {
  806. .pc_func = nfsd3_proc_mknod,
  807. .pc_decode = nfs3svc_decode_mknodargs,
  808. .pc_encode = nfs3svc_encode_createres,
  809. .pc_release = nfs3svc_release_fhandle2,
  810. .pc_argsize = sizeof(struct nfsd3_mknodargs),
  811. .pc_argzero = sizeof(struct nfsd3_mknodargs),
  812. .pc_ressize = sizeof(struct nfsd3_createres),
  813. .pc_cachetype = RC_REPLBUFF,
  814. .pc_xdrressize = ST+(1+FH+pAT)+WC,
  815. .pc_name = "MKNOD",
  816. },
  817. [NFS3PROC_REMOVE] = {
  818. .pc_func = nfsd3_proc_remove,
  819. .pc_decode = nfs3svc_decode_diropargs,
  820. .pc_encode = nfs3svc_encode_wccstatres,
  821. .pc_release = nfs3svc_release_fhandle,
  822. .pc_argsize = sizeof(struct nfsd3_diropargs),
  823. .pc_argzero = sizeof(struct nfsd3_diropargs),
  824. .pc_ressize = sizeof(struct nfsd3_wccstatres),
  825. .pc_cachetype = RC_REPLBUFF,
  826. .pc_xdrressize = ST+WC,
  827. .pc_name = "REMOVE",
  828. },
  829. [NFS3PROC_RMDIR] = {
  830. .pc_func = nfsd3_proc_rmdir,
  831. .pc_decode = nfs3svc_decode_diropargs,
  832. .pc_encode = nfs3svc_encode_wccstatres,
  833. .pc_release = nfs3svc_release_fhandle,
  834. .pc_argsize = sizeof(struct nfsd3_diropargs),
  835. .pc_argzero = sizeof(struct nfsd3_diropargs),
  836. .pc_ressize = sizeof(struct nfsd3_wccstatres),
  837. .pc_cachetype = RC_REPLBUFF,
  838. .pc_xdrressize = ST+WC,
  839. .pc_name = "RMDIR",
  840. },
  841. [NFS3PROC_RENAME] = {
  842. .pc_func = nfsd3_proc_rename,
  843. .pc_decode = nfs3svc_decode_renameargs,
  844. .pc_encode = nfs3svc_encode_renameres,
  845. .pc_release = nfs3svc_release_fhandle2,
  846. .pc_argsize = sizeof(struct nfsd3_renameargs),
  847. .pc_argzero = sizeof(struct nfsd3_renameargs),
  848. .pc_ressize = sizeof(struct nfsd3_renameres),
  849. .pc_cachetype = RC_REPLBUFF,
  850. .pc_xdrressize = ST+WC+WC,
  851. .pc_name = "RENAME",
  852. },
  853. [NFS3PROC_LINK] = {
  854. .pc_func = nfsd3_proc_link,
  855. .pc_decode = nfs3svc_decode_linkargs,
  856. .pc_encode = nfs3svc_encode_linkres,
  857. .pc_release = nfs3svc_release_fhandle2,
  858. .pc_argsize = sizeof(struct nfsd3_linkargs),
  859. .pc_argzero = sizeof(struct nfsd3_linkargs),
  860. .pc_ressize = sizeof(struct nfsd3_linkres),
  861. .pc_cachetype = RC_REPLBUFF,
  862. .pc_xdrressize = ST+pAT+WC,
  863. .pc_name = "LINK",
  864. },
  865. [NFS3PROC_READDIR] = {
  866. .pc_func = nfsd3_proc_readdir,
  867. .pc_decode = nfs3svc_decode_readdirargs,
  868. .pc_encode = nfs3svc_encode_readdirres,
  869. .pc_release = nfs3svc_release_fhandle,
  870. .pc_argsize = sizeof(struct nfsd3_readdirargs),
  871. .pc_argzero = sizeof(struct nfsd3_readdirargs),
  872. .pc_ressize = sizeof(struct nfsd3_readdirres),
  873. .pc_cachetype = RC_NOCACHE,
  874. .pc_name = "READDIR",
  875. },
  876. [NFS3PROC_READDIRPLUS] = {
  877. .pc_func = nfsd3_proc_readdirplus,
  878. .pc_decode = nfs3svc_decode_readdirplusargs,
  879. .pc_encode = nfs3svc_encode_readdirres,
  880. .pc_release = nfs3svc_release_fhandle,
  881. .pc_argsize = sizeof(struct nfsd3_readdirplusargs),
  882. .pc_argzero = sizeof(struct nfsd3_readdirplusargs),
  883. .pc_ressize = sizeof(struct nfsd3_readdirres),
  884. .pc_cachetype = RC_NOCACHE,
  885. .pc_name = "READDIRPLUS",
  886. },
  887. [NFS3PROC_FSSTAT] = {
  888. .pc_func = nfsd3_proc_fsstat,
  889. .pc_decode = nfs3svc_decode_fhandleargs,
  890. .pc_encode = nfs3svc_encode_fsstatres,
  891. .pc_argsize = sizeof(struct nfsd3_fhandleargs),
  892. .pc_argzero = sizeof(struct nfsd3_fhandleargs),
  893. .pc_ressize = sizeof(struct nfsd3_fsstatres),
  894. .pc_cachetype = RC_NOCACHE,
  895. .pc_xdrressize = ST+pAT+2*6+1,
  896. .pc_name = "FSSTAT",
  897. },
  898. [NFS3PROC_FSINFO] = {
  899. .pc_func = nfsd3_proc_fsinfo,
  900. .pc_decode = nfs3svc_decode_fhandleargs,
  901. .pc_encode = nfs3svc_encode_fsinfores,
  902. .pc_argsize = sizeof(struct nfsd3_fhandleargs),
  903. .pc_argzero = sizeof(struct nfsd3_fhandleargs),
  904. .pc_ressize = sizeof(struct nfsd3_fsinfores),
  905. .pc_cachetype = RC_NOCACHE,
  906. .pc_xdrressize = ST+pAT+12,
  907. .pc_name = "FSINFO",
  908. },
  909. [NFS3PROC_PATHCONF] = {
  910. .pc_func = nfsd3_proc_pathconf,
  911. .pc_decode = nfs3svc_decode_fhandleargs,
  912. .pc_encode = nfs3svc_encode_pathconfres,
  913. .pc_argsize = sizeof(struct nfsd3_fhandleargs),
  914. .pc_argzero = sizeof(struct nfsd3_fhandleargs),
  915. .pc_ressize = sizeof(struct nfsd3_pathconfres),
  916. .pc_cachetype = RC_NOCACHE,
  917. .pc_xdrressize = ST+pAT+6,
  918. .pc_name = "PATHCONF",
  919. },
  920. [NFS3PROC_COMMIT] = {
  921. .pc_func = nfsd3_proc_commit,
  922. .pc_decode = nfs3svc_decode_commitargs,
  923. .pc_encode = nfs3svc_encode_commitres,
  924. .pc_release = nfs3svc_release_fhandle,
  925. .pc_argsize = sizeof(struct nfsd3_commitargs),
  926. .pc_argzero = sizeof(struct nfsd3_commitargs),
  927. .pc_ressize = sizeof(struct nfsd3_commitres),
  928. .pc_cachetype = RC_NOCACHE,
  929. .pc_xdrressize = ST+WC+2,
  930. .pc_name = "COMMIT",
  931. },
  932. };
  933. static DEFINE_PER_CPU_ALIGNED(unsigned long,
  934. nfsd_count3[ARRAY_SIZE(nfsd_procedures3)]);
  935. const struct svc_version nfsd_version3 = {
  936. .vs_vers = 3,
  937. .vs_nproc = ARRAY_SIZE(nfsd_procedures3),
  938. .vs_proc = nfsd_procedures3,
  939. .vs_dispatch = nfsd_dispatch,
  940. .vs_count = nfsd_count3,
  941. .vs_xdrsize = NFS3_SVC_XDRSIZE,
  942. };