vfs.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * File operations used by nfsd. Some of these have been ripped from
  4. * other parts of the kernel because they weren't exported, others
  5. * are partial duplicates with added or changed functionality.
  6. *
  7. * Note that several functions dget() the dentry upon which they want
  8. * to act, most notably those that create directory entries. Response
  9. * dentry's are dput()'d if necessary in the release callback.
  10. * So if you notice code paths that apparently fail to dput() the
  11. * dentry, don't worry--they have been taken care of.
  12. *
  13. * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
  14. * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
  15. */
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/splice.h>
  19. #include <linux/falloc.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/namei.h>
  22. #include <linux/delay.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/xattr.h>
  26. #include <linux/jhash.h>
  27. #include <linux/ima.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/exportfs.h>
  31. #include <linux/writeback.h>
  32. #include <linux/security.h>
  33. #ifdef CONFIG_NFSD_V3
  34. #include "xdr3.h"
  35. #endif /* CONFIG_NFSD_V3 */
  36. #ifdef CONFIG_NFSD_V4
  37. #include "../internal.h"
  38. #include "acl.h"
  39. #include "idmap.h"
  40. #endif /* CONFIG_NFSD_V4 */
  41. #include "nfsd.h"
  42. #include "vfs.h"
  43. #include "trace.h"
  44. #define NFSDDBG_FACILITY NFSDDBG_FILEOP
  45. /*
  46. * This is a cache of readahead params that help us choose the proper
  47. * readahead strategy. Initially, we set all readahead parameters to 0
  48. * and let the VFS handle things.
  49. * If you increase the number of cached files very much, you'll need to
  50. * add a hash table here.
  51. */
  52. struct raparms {
  53. struct raparms *p_next;
  54. unsigned int p_count;
  55. ino_t p_ino;
  56. dev_t p_dev;
  57. int p_set;
  58. struct file_ra_state p_ra;
  59. unsigned int p_hindex;
  60. };
  61. struct raparm_hbucket {
  62. struct raparms *pb_head;
  63. spinlock_t pb_lock;
  64. } ____cacheline_aligned_in_smp;
  65. #define RAPARM_HASH_BITS 4
  66. #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
  67. #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
  68. static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
  69. /*
  70. * Called from nfsd_lookup and encode_dirent. Check if we have crossed
  71. * a mount point.
  72. * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
  73. * or nfs_ok having possibly changed *dpp and *expp
  74. */
  75. int
  76. nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
  77. struct svc_export **expp)
  78. {
  79. struct svc_export *exp = *expp, *exp2 = NULL;
  80. struct dentry *dentry = *dpp;
  81. struct path path = {.mnt = mntget(exp->ex_path.mnt),
  82. .dentry = dget(dentry)};
  83. int err = 0;
  84. err = follow_down(&path);
  85. if (err < 0)
  86. goto out;
  87. if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
  88. nfsd_mountpoint(dentry, exp) == 2) {
  89. /* This is only a mountpoint in some other namespace */
  90. path_put(&path);
  91. goto out;
  92. }
  93. exp2 = rqst_exp_get_by_name(rqstp, &path);
  94. if (IS_ERR(exp2)) {
  95. err = PTR_ERR(exp2);
  96. /*
  97. * We normally allow NFS clients to continue
  98. * "underneath" a mountpoint that is not exported.
  99. * The exception is V4ROOT, where no traversal is ever
  100. * allowed without an explicit export of the new
  101. * directory.
  102. */
  103. if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
  104. err = 0;
  105. path_put(&path);
  106. goto out;
  107. }
  108. if (nfsd_v4client(rqstp) ||
  109. (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
  110. /* successfully crossed mount point */
  111. /*
  112. * This is subtle: path.dentry is *not* on path.mnt
  113. * at this point. The only reason we are safe is that
  114. * original mnt is pinned down by exp, so we should
  115. * put path *before* putting exp
  116. */
  117. *dpp = path.dentry;
  118. path.dentry = dentry;
  119. *expp = exp2;
  120. exp2 = exp;
  121. }
  122. path_put(&path);
  123. exp_put(exp2);
  124. out:
  125. return err;
  126. }
  127. static void follow_to_parent(struct path *path)
  128. {
  129. struct dentry *dp;
  130. while (path->dentry == path->mnt->mnt_root && follow_up(path))
  131. ;
  132. dp = dget_parent(path->dentry);
  133. dput(path->dentry);
  134. path->dentry = dp;
  135. }
  136. static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
  137. {
  138. struct svc_export *exp2;
  139. struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
  140. .dentry = dget(dparent)};
  141. follow_to_parent(&path);
  142. exp2 = rqst_exp_parent(rqstp, &path);
  143. if (PTR_ERR(exp2) == -ENOENT) {
  144. *dentryp = dget(dparent);
  145. } else if (IS_ERR(exp2)) {
  146. path_put(&path);
  147. return PTR_ERR(exp2);
  148. } else {
  149. *dentryp = dget(path.dentry);
  150. exp_put(*exp);
  151. *exp = exp2;
  152. }
  153. path_put(&path);
  154. return 0;
  155. }
  156. /*
  157. * For nfsd purposes, we treat V4ROOT exports as though there was an
  158. * export at *every* directory.
  159. * We return:
  160. * '1' if this dentry *must* be an export point,
  161. * '2' if it might be, if there is really a mount here, and
  162. * '0' if there is no chance of an export point here.
  163. */
  164. int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
  165. {
  166. if (!d_inode(dentry))
  167. return 0;
  168. if (exp->ex_flags & NFSEXP_V4ROOT)
  169. return 1;
  170. if (nfsd4_is_junction(dentry))
  171. return 1;
  172. if (d_mountpoint(dentry))
  173. /*
  174. * Might only be a mountpoint in a different namespace,
  175. * but we need to check.
  176. */
  177. return 2;
  178. return 0;
  179. }
  180. __be32
  181. nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  182. const char *name, unsigned int len,
  183. struct svc_export **exp_ret, struct dentry **dentry_ret)
  184. {
  185. struct svc_export *exp;
  186. struct dentry *dparent;
  187. struct dentry *dentry;
  188. int host_err;
  189. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  190. dparent = fhp->fh_dentry;
  191. exp = exp_get(fhp->fh_export);
  192. /* Lookup the name, but don't follow links */
  193. if (isdotent(name, len)) {
  194. if (len==1)
  195. dentry = dget(dparent);
  196. else if (dparent != exp->ex_path.dentry)
  197. dentry = dget_parent(dparent);
  198. else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
  199. dentry = dget(dparent); /* .. == . just like at / */
  200. else {
  201. /* checking mountpoint crossing is very different when stepping up */
  202. host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
  203. if (host_err)
  204. goto out_nfserr;
  205. }
  206. } else {
  207. /*
  208. * In the nfsd4_open() case, this may be held across
  209. * subsequent open and delegation acquisition which may
  210. * need to take the child's i_mutex:
  211. */
  212. fh_lock_nested(fhp, I_MUTEX_PARENT);
  213. dentry = lookup_one_len(name, dparent, len);
  214. host_err = PTR_ERR(dentry);
  215. if (IS_ERR(dentry))
  216. goto out_nfserr;
  217. if (nfsd_mountpoint(dentry, exp)) {
  218. /*
  219. * We don't need the i_mutex after all. It's
  220. * still possible we could open this (regular
  221. * files can be mountpoints too), but the
  222. * i_mutex is just there to prevent renames of
  223. * something that we might be about to delegate,
  224. * and a mountpoint won't be renamed:
  225. */
  226. fh_unlock(fhp);
  227. if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
  228. dput(dentry);
  229. goto out_nfserr;
  230. }
  231. }
  232. }
  233. *dentry_ret = dentry;
  234. *exp_ret = exp;
  235. return 0;
  236. out_nfserr:
  237. exp_put(exp);
  238. return nfserrno(host_err);
  239. }
  240. /*
  241. * Look up one component of a pathname.
  242. * N.B. After this call _both_ fhp and resfh need an fh_put
  243. *
  244. * If the lookup would cross a mountpoint, and the mounted filesystem
  245. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  246. * accepted as it stands and the mounted directory is
  247. * returned. Otherwise the covered directory is returned.
  248. * NOTE: this mountpoint crossing is not supported properly by all
  249. * clients and is explicitly disallowed for NFSv3
  250. * NeilBrown <neilb@cse.unsw.edu.au>
  251. */
  252. __be32
  253. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  254. unsigned int len, struct svc_fh *resfh)
  255. {
  256. struct svc_export *exp;
  257. struct dentry *dentry;
  258. __be32 err;
  259. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  260. if (err)
  261. return err;
  262. err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
  263. if (err)
  264. return err;
  265. err = check_nfsd_access(exp, rqstp);
  266. if (err)
  267. goto out;
  268. /*
  269. * Note: we compose the file handle now, but as the
  270. * dentry may be negative, it may need to be updated.
  271. */
  272. err = fh_compose(resfh, exp, dentry, fhp);
  273. if (!err && d_really_is_negative(dentry))
  274. err = nfserr_noent;
  275. out:
  276. dput(dentry);
  277. exp_put(exp);
  278. return err;
  279. }
  280. /*
  281. * Commit metadata changes to stable storage.
  282. */
  283. static int
  284. commit_metadata(struct svc_fh *fhp)
  285. {
  286. struct inode *inode = d_inode(fhp->fh_dentry);
  287. const struct export_operations *export_ops = inode->i_sb->s_export_op;
  288. if (!EX_ISSYNC(fhp->fh_export))
  289. return 0;
  290. if (export_ops->commit_metadata)
  291. return export_ops->commit_metadata(inode);
  292. return sync_inode_metadata(inode, 1);
  293. }
  294. /*
  295. * Go over the attributes and take care of the small differences between
  296. * NFS semantics and what Linux expects.
  297. */
  298. static void
  299. nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
  300. {
  301. /* sanitize the mode change */
  302. if (iap->ia_valid & ATTR_MODE) {
  303. iap->ia_mode &= S_IALLUGO;
  304. iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
  305. }
  306. /* Revoke setuid/setgid on chown */
  307. if (!S_ISDIR(inode->i_mode) &&
  308. ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
  309. iap->ia_valid |= ATTR_KILL_PRIV;
  310. if (iap->ia_valid & ATTR_MODE) {
  311. /* we're setting mode too, just clear the s*id bits */
  312. iap->ia_mode &= ~S_ISUID;
  313. if (iap->ia_mode & S_IXGRP)
  314. iap->ia_mode &= ~S_ISGID;
  315. } else {
  316. /* set ATTR_KILL_* bits and let VFS handle it */
  317. iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
  318. }
  319. }
  320. }
  321. static __be32
  322. nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
  323. struct iattr *iap)
  324. {
  325. struct inode *inode = d_inode(fhp->fh_dentry);
  326. int host_err;
  327. if (iap->ia_size < inode->i_size) {
  328. __be32 err;
  329. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  330. NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
  331. if (err)
  332. return err;
  333. }
  334. host_err = get_write_access(inode);
  335. if (host_err)
  336. goto out_nfserrno;
  337. host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
  338. if (host_err)
  339. goto out_put_write_access;
  340. return 0;
  341. out_put_write_access:
  342. put_write_access(inode);
  343. out_nfserrno:
  344. return nfserrno(host_err);
  345. }
  346. /*
  347. * Set various file attributes. After this call fhp needs an fh_put.
  348. */
  349. __be32
  350. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  351. int check_guard, time_t guardtime)
  352. {
  353. struct dentry *dentry;
  354. struct inode *inode;
  355. int accmode = NFSD_MAY_SATTR;
  356. umode_t ftype = 0;
  357. __be32 err;
  358. int host_err;
  359. bool get_write_count;
  360. bool size_change = (iap->ia_valid & ATTR_SIZE);
  361. if (iap->ia_valid & ATTR_SIZE) {
  362. accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
  363. ftype = S_IFREG;
  364. }
  365. /*
  366. * If utimes(2) and friends are called with times not NULL, we should
  367. * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
  368. * will return EACCESS, when the caller's effective UID does not match
  369. * the owner of the file, and the caller is not privileged. In this
  370. * situation, we should return EPERM(notify_change will return this).
  371. */
  372. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
  373. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  374. if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
  375. accmode |= NFSD_MAY_WRITE;
  376. }
  377. /* Callers that do fh_verify should do the fh_want_write: */
  378. get_write_count = !fhp->fh_dentry;
  379. /* Get inode */
  380. err = fh_verify(rqstp, fhp, ftype, accmode);
  381. if (err)
  382. return err;
  383. if (get_write_count) {
  384. host_err = fh_want_write(fhp);
  385. if (host_err)
  386. goto out;
  387. }
  388. dentry = fhp->fh_dentry;
  389. inode = d_inode(dentry);
  390. /* Ignore any mode updates on symlinks */
  391. if (S_ISLNK(inode->i_mode))
  392. iap->ia_valid &= ~ATTR_MODE;
  393. if (!iap->ia_valid)
  394. return 0;
  395. nfsd_sanitize_attrs(inode, iap);
  396. if (check_guard && guardtime != inode->i_ctime.tv_sec)
  397. return nfserr_notsync;
  398. /*
  399. * The size case is special, it changes the file in addition to the
  400. * attributes, and file systems don't expect it to be mixed with
  401. * "random" attribute changes. We thus split out the size change
  402. * into a separate call to ->setattr, and do the rest as a separate
  403. * setattr call.
  404. */
  405. if (size_change) {
  406. err = nfsd_get_write_access(rqstp, fhp, iap);
  407. if (err)
  408. return err;
  409. }
  410. fh_lock(fhp);
  411. if (size_change) {
  412. /*
  413. * RFC5661, Section 18.30.4:
  414. * Changing the size of a file with SETATTR indirectly
  415. * changes the time_modify and change attributes.
  416. *
  417. * (and similar for the older RFCs)
  418. */
  419. struct iattr size_attr = {
  420. .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
  421. .ia_size = iap->ia_size,
  422. };
  423. host_err = notify_change(dentry, &size_attr, NULL);
  424. if (host_err)
  425. goto out_unlock;
  426. iap->ia_valid &= ~ATTR_SIZE;
  427. /*
  428. * Avoid the additional setattr call below if the only other
  429. * attribute that the client sends is the mtime, as we update
  430. * it as part of the size change above.
  431. */
  432. if ((iap->ia_valid & ~ATTR_MTIME) == 0)
  433. goto out_unlock;
  434. }
  435. iap->ia_valid |= ATTR_CTIME;
  436. host_err = notify_change(dentry, iap, NULL);
  437. out_unlock:
  438. fh_unlock(fhp);
  439. if (size_change)
  440. put_write_access(inode);
  441. out:
  442. if (!host_err)
  443. host_err = commit_metadata(fhp);
  444. return nfserrno(host_err);
  445. }
  446. #if defined(CONFIG_NFSD_V4)
  447. /*
  448. * NFS junction information is stored in an extended attribute.
  449. */
  450. #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
  451. /**
  452. * nfsd4_is_junction - Test if an object could be an NFS junction
  453. *
  454. * @dentry: object to test
  455. *
  456. * Returns 1 if "dentry" appears to contain NFS junction information.
  457. * Otherwise 0 is returned.
  458. */
  459. int nfsd4_is_junction(struct dentry *dentry)
  460. {
  461. struct inode *inode = d_inode(dentry);
  462. if (inode == NULL)
  463. return 0;
  464. if (inode->i_mode & S_IXUGO)
  465. return 0;
  466. if (!(inode->i_mode & S_ISVTX))
  467. return 0;
  468. if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
  469. return 0;
  470. return 1;
  471. }
  472. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  473. __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
  474. struct xdr_netobj *label)
  475. {
  476. __be32 error;
  477. int host_error;
  478. struct dentry *dentry;
  479. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
  480. if (error)
  481. return error;
  482. dentry = fhp->fh_dentry;
  483. inode_lock(d_inode(dentry));
  484. host_error = security_inode_setsecctx(dentry, label->data, label->len);
  485. inode_unlock(d_inode(dentry));
  486. return nfserrno(host_error);
  487. }
  488. #else
  489. __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
  490. struct xdr_netobj *label)
  491. {
  492. return nfserr_notsupp;
  493. }
  494. #endif
  495. __be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
  496. u64 dst_pos, u64 count)
  497. {
  498. return nfserrno(vfs_clone_file_range(src, src_pos, dst, dst_pos,
  499. count));
  500. }
  501. ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
  502. u64 dst_pos, u64 count)
  503. {
  504. /*
  505. * Limit copy to 4MB to prevent indefinitely blocking an nfsd
  506. * thread and client rpc slot. The choice of 4MB is somewhat
  507. * arbitrary. We might instead base this on r/wsize, or make it
  508. * tunable, or use a time instead of a byte limit, or implement
  509. * asynchronous copy. In theory a client could also recognize a
  510. * limit like this and pipeline multiple COPY requests.
  511. */
  512. count = min_t(u64, count, 1 << 22);
  513. return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
  514. }
  515. __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
  516. struct file *file, loff_t offset, loff_t len,
  517. int flags)
  518. {
  519. int error;
  520. if (!S_ISREG(file_inode(file)->i_mode))
  521. return nfserr_inval;
  522. error = vfs_fallocate(file, flags, offset, len);
  523. if (!error)
  524. error = commit_metadata(fhp);
  525. return nfserrno(error);
  526. }
  527. #endif /* defined(CONFIG_NFSD_V4) */
  528. #ifdef CONFIG_NFSD_V3
  529. /*
  530. * Check server access rights to a file system object
  531. */
  532. struct accessmap {
  533. u32 access;
  534. int how;
  535. };
  536. static struct accessmap nfs3_regaccess[] = {
  537. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  538. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  539. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
  540. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
  541. { 0, 0 }
  542. };
  543. static struct accessmap nfs3_diraccess[] = {
  544. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  545. { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
  546. { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
  547. { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
  548. { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
  549. { 0, 0 }
  550. };
  551. static struct accessmap nfs3_anyaccess[] = {
  552. /* Some clients - Solaris 2.6 at least, make an access call
  553. * to the server to check for access for things like /dev/null
  554. * (which really, the server doesn't care about). So
  555. * We provide simple access checking for them, looking
  556. * mainly at mode bits, and we make sure to ignore read-only
  557. * filesystem checks
  558. */
  559. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  560. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  561. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  562. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  563. { 0, 0 }
  564. };
  565. __be32
  566. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  567. {
  568. struct accessmap *map;
  569. struct svc_export *export;
  570. struct dentry *dentry;
  571. u32 query, result = 0, sresult = 0;
  572. __be32 error;
  573. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
  574. if (error)
  575. goto out;
  576. export = fhp->fh_export;
  577. dentry = fhp->fh_dentry;
  578. if (d_is_reg(dentry))
  579. map = nfs3_regaccess;
  580. else if (d_is_dir(dentry))
  581. map = nfs3_diraccess;
  582. else
  583. map = nfs3_anyaccess;
  584. query = *access;
  585. for (; map->access; map++) {
  586. if (map->access & query) {
  587. __be32 err2;
  588. sresult |= map->access;
  589. err2 = nfsd_permission(rqstp, export, dentry, map->how);
  590. switch (err2) {
  591. case nfs_ok:
  592. result |= map->access;
  593. break;
  594. /* the following error codes just mean the access was not allowed,
  595. * rather than an error occurred */
  596. case nfserr_rofs:
  597. case nfserr_acces:
  598. case nfserr_perm:
  599. /* simply don't "or" in the access bit. */
  600. break;
  601. default:
  602. error = err2;
  603. goto out;
  604. }
  605. }
  606. }
  607. *access = result;
  608. if (supported)
  609. *supported = sresult;
  610. out:
  611. return error;
  612. }
  613. #endif /* CONFIG_NFSD_V3 */
  614. static int nfsd_open_break_lease(struct inode *inode, int access)
  615. {
  616. unsigned int mode;
  617. if (access & NFSD_MAY_NOT_BREAK_LEASE)
  618. return 0;
  619. mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
  620. return break_lease(inode, mode | O_NONBLOCK);
  621. }
  622. /*
  623. * Open an existing file or directory.
  624. * The may_flags argument indicates the type of open (read/write/lock)
  625. * and additional flags.
  626. * N.B. After this call fhp needs an fh_put
  627. */
  628. __be32
  629. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
  630. int may_flags, struct file **filp)
  631. {
  632. struct path path;
  633. struct inode *inode;
  634. struct file *file;
  635. int flags = O_RDONLY|O_LARGEFILE;
  636. __be32 err;
  637. int host_err = 0;
  638. validate_process_creds();
  639. /*
  640. * If we get here, then the client has already done an "open",
  641. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  642. * in case a chmod has now revoked permission.
  643. *
  644. * Arguably we should also allow the owner override for
  645. * directories, but we never have and it doesn't seem to have
  646. * caused anyone a problem. If we were to change this, note
  647. * also that our filldir callbacks would need a variant of
  648. * lookup_one_len that doesn't check permissions.
  649. */
  650. if (type == S_IFREG)
  651. may_flags |= NFSD_MAY_OWNER_OVERRIDE;
  652. err = fh_verify(rqstp, fhp, type, may_flags);
  653. if (err)
  654. goto out;
  655. path.mnt = fhp->fh_export->ex_path.mnt;
  656. path.dentry = fhp->fh_dentry;
  657. inode = d_inode(path.dentry);
  658. /* Disallow write access to files with the append-only bit set
  659. * or any access when mandatory locking enabled
  660. */
  661. err = nfserr_perm;
  662. if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
  663. goto out;
  664. /*
  665. * We must ignore files (but only files) which might have mandatory
  666. * locks on them because there is no way to know if the accesser has
  667. * the lock.
  668. */
  669. if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
  670. goto out;
  671. if (!inode->i_fop)
  672. goto out;
  673. host_err = nfsd_open_break_lease(inode, may_flags);
  674. if (host_err) /* NOMEM or WOULDBLOCK */
  675. goto out_nfserr;
  676. if (may_flags & NFSD_MAY_WRITE) {
  677. if (may_flags & NFSD_MAY_READ)
  678. flags = O_RDWR|O_LARGEFILE;
  679. else
  680. flags = O_WRONLY|O_LARGEFILE;
  681. }
  682. file = dentry_open(&path, flags, current_cred());
  683. if (IS_ERR(file)) {
  684. host_err = PTR_ERR(file);
  685. goto out_nfserr;
  686. }
  687. host_err = ima_file_check(file, may_flags);
  688. if (host_err) {
  689. fput(file);
  690. goto out_nfserr;
  691. }
  692. if (may_flags & NFSD_MAY_64BIT_COOKIE)
  693. file->f_mode |= FMODE_64BITHASH;
  694. else
  695. file->f_mode |= FMODE_32BITHASH;
  696. *filp = file;
  697. out_nfserr:
  698. err = nfserrno(host_err);
  699. out:
  700. validate_process_creds();
  701. return err;
  702. }
  703. struct raparms *
  704. nfsd_init_raparms(struct file *file)
  705. {
  706. struct inode *inode = file_inode(file);
  707. dev_t dev = inode->i_sb->s_dev;
  708. ino_t ino = inode->i_ino;
  709. struct raparms *ra, **rap, **frap = NULL;
  710. int depth = 0;
  711. unsigned int hash;
  712. struct raparm_hbucket *rab;
  713. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  714. rab = &raparm_hash[hash];
  715. spin_lock(&rab->pb_lock);
  716. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  717. if (ra->p_ino == ino && ra->p_dev == dev)
  718. goto found;
  719. depth++;
  720. if (ra->p_count == 0)
  721. frap = rap;
  722. }
  723. depth = nfsdstats.ra_size;
  724. if (!frap) {
  725. spin_unlock(&rab->pb_lock);
  726. return NULL;
  727. }
  728. rap = frap;
  729. ra = *frap;
  730. ra->p_dev = dev;
  731. ra->p_ino = ino;
  732. ra->p_set = 0;
  733. ra->p_hindex = hash;
  734. found:
  735. if (rap != &rab->pb_head) {
  736. *rap = ra->p_next;
  737. ra->p_next = rab->pb_head;
  738. rab->pb_head = ra;
  739. }
  740. ra->p_count++;
  741. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  742. spin_unlock(&rab->pb_lock);
  743. if (ra->p_set)
  744. file->f_ra = ra->p_ra;
  745. return ra;
  746. }
  747. void nfsd_put_raparams(struct file *file, struct raparms *ra)
  748. {
  749. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  750. spin_lock(&rab->pb_lock);
  751. ra->p_ra = file->f_ra;
  752. ra->p_set = 1;
  753. ra->p_count--;
  754. spin_unlock(&rab->pb_lock);
  755. }
  756. /*
  757. * Grab and keep cached pages associated with a file in the svc_rqst
  758. * so that they can be passed to the network sendmsg/sendpage routines
  759. * directly. They will be released after the sending has completed.
  760. */
  761. static int
  762. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  763. struct splice_desc *sd)
  764. {
  765. struct svc_rqst *rqstp = sd->u.data;
  766. struct page **pp = rqstp->rq_next_page;
  767. struct page *page = buf->page;
  768. size_t size;
  769. size = sd->len;
  770. if (rqstp->rq_res.page_len == 0) {
  771. get_page(page);
  772. put_page(*rqstp->rq_next_page);
  773. *(rqstp->rq_next_page++) = page;
  774. rqstp->rq_res.page_base = buf->offset;
  775. rqstp->rq_res.page_len = size;
  776. } else if (page != pp[-1]) {
  777. get_page(page);
  778. if (*rqstp->rq_next_page)
  779. put_page(*rqstp->rq_next_page);
  780. *(rqstp->rq_next_page++) = page;
  781. rqstp->rq_res.page_len += size;
  782. } else
  783. rqstp->rq_res.page_len += size;
  784. return size;
  785. }
  786. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  787. struct splice_desc *sd)
  788. {
  789. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  790. }
  791. static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  792. struct file *file, loff_t offset,
  793. unsigned long *count, int host_err)
  794. {
  795. if (host_err >= 0) {
  796. nfsdstats.io_read += host_err;
  797. *count = host_err;
  798. fsnotify_access(file);
  799. trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
  800. return 0;
  801. } else {
  802. trace_nfsd_read_err(rqstp, fhp, offset, host_err);
  803. return nfserrno(host_err);
  804. }
  805. }
  806. __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  807. struct file *file, loff_t offset, unsigned long *count)
  808. {
  809. struct splice_desc sd = {
  810. .len = 0,
  811. .total_len = *count,
  812. .pos = offset,
  813. .u.data = rqstp,
  814. };
  815. int host_err;
  816. trace_nfsd_read_splice(rqstp, fhp, offset, *count);
  817. rqstp->rq_next_page = rqstp->rq_respages + 1;
  818. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  819. return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
  820. }
  821. __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
  822. struct file *file, loff_t offset,
  823. struct kvec *vec, int vlen, unsigned long *count)
  824. {
  825. struct iov_iter iter;
  826. int host_err;
  827. trace_nfsd_read_vector(rqstp, fhp, offset, *count);
  828. iov_iter_kvec(&iter, READ | ITER_KVEC, vec, vlen, *count);
  829. host_err = vfs_iter_read(file, &iter, &offset, 0);
  830. return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
  831. }
  832. /*
  833. * Gathered writes: If another process is currently writing to the file,
  834. * there's a high chance this is another nfsd (triggered by a bulk write
  835. * from a client's biod). Rather than syncing the file with each write
  836. * request, we sleep for 10 msec.
  837. *
  838. * I don't know if this roughly approximates C. Juszak's idea of
  839. * gathered writes, but it's a nice and simple solution (IMHO), and it
  840. * seems to work:-)
  841. *
  842. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  843. * better tool (separate unstable writes and commits) for solving this
  844. * problem.
  845. */
  846. static int wait_for_concurrent_writes(struct file *file)
  847. {
  848. struct inode *inode = file_inode(file);
  849. static ino_t last_ino;
  850. static dev_t last_dev;
  851. int err = 0;
  852. if (atomic_read(&inode->i_writecount) > 1
  853. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  854. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  855. msleep(10);
  856. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  857. }
  858. if (inode->i_state & I_DIRTY) {
  859. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  860. err = vfs_fsync(file, 0);
  861. }
  862. last_ino = inode->i_ino;
  863. last_dev = inode->i_sb->s_dev;
  864. return err;
  865. }
  866. __be32
  867. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  868. loff_t offset, struct kvec *vec, int vlen,
  869. unsigned long *cnt, int stable)
  870. {
  871. struct svc_export *exp;
  872. struct iov_iter iter;
  873. __be32 nfserr;
  874. int host_err;
  875. int use_wgather;
  876. loff_t pos = offset;
  877. unsigned int pflags = current->flags;
  878. rwf_t flags = 0;
  879. trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
  880. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  881. /*
  882. * We want less throttling in balance_dirty_pages()
  883. * and shrink_inactive_list() so that nfs to
  884. * localhost doesn't cause nfsd to lock up due to all
  885. * the client's dirty pages or its congested queue.
  886. */
  887. current->flags |= PF_LESS_THROTTLE;
  888. exp = fhp->fh_export;
  889. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  890. if (!EX_ISSYNC(exp))
  891. stable = NFS_UNSTABLE;
  892. if (stable && !use_wgather)
  893. flags |= RWF_SYNC;
  894. iov_iter_kvec(&iter, WRITE | ITER_KVEC, vec, vlen, *cnt);
  895. host_err = vfs_iter_write(file, &iter, &pos, flags);
  896. if (host_err < 0)
  897. goto out_nfserr;
  898. *cnt = host_err;
  899. nfsdstats.io_write += *cnt;
  900. fsnotify_modify(file);
  901. if (stable && use_wgather)
  902. host_err = wait_for_concurrent_writes(file);
  903. out_nfserr:
  904. if (host_err >= 0) {
  905. trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
  906. nfserr = nfs_ok;
  907. } else {
  908. trace_nfsd_write_err(rqstp, fhp, offset, host_err);
  909. nfserr = nfserrno(host_err);
  910. }
  911. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  912. current_restore_flags(pflags, PF_LESS_THROTTLE);
  913. return nfserr;
  914. }
  915. /*
  916. * Read data from a file. count must contain the requested read count
  917. * on entry. On return, *count contains the number of bytes actually read.
  918. * N.B. After this call fhp needs an fh_put
  919. */
  920. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  921. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  922. {
  923. struct file *file;
  924. struct raparms *ra;
  925. __be32 err;
  926. trace_nfsd_read_start(rqstp, fhp, offset, *count);
  927. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  928. if (err)
  929. return err;
  930. ra = nfsd_init_raparms(file);
  931. if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
  932. err = nfsd_splice_read(rqstp, fhp, file, offset, count);
  933. else
  934. err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count);
  935. if (ra)
  936. nfsd_put_raparams(file, ra);
  937. fput(file);
  938. trace_nfsd_read_done(rqstp, fhp, offset, *count);
  939. return err;
  940. }
  941. /*
  942. * Write data to a file.
  943. * The stable flag requests synchronous writes.
  944. * N.B. After this call fhp needs an fh_put
  945. */
  946. __be32
  947. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
  948. struct kvec *vec, int vlen, unsigned long *cnt, int stable)
  949. {
  950. struct file *file = NULL;
  951. __be32 err = 0;
  952. trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
  953. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  954. if (err)
  955. goto out;
  956. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt, stable);
  957. fput(file);
  958. out:
  959. trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
  960. return err;
  961. }
  962. #ifdef CONFIG_NFSD_V3
  963. /*
  964. * Commit all pending writes to stable storage.
  965. *
  966. * Note: we only guarantee that data that lies within the range specified
  967. * by the 'offset' and 'count' parameters will be synced.
  968. *
  969. * Unfortunately we cannot lock the file to make sure we return full WCC
  970. * data to the client, as locking happens lower down in the filesystem.
  971. */
  972. __be32
  973. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  974. loff_t offset, unsigned long count)
  975. {
  976. struct file *file;
  977. loff_t end = LLONG_MAX;
  978. __be32 err = nfserr_inval;
  979. if (offset < 0)
  980. goto out;
  981. if (count != 0) {
  982. end = offset + (loff_t)count - 1;
  983. if (end < offset)
  984. goto out;
  985. }
  986. err = nfsd_open(rqstp, fhp, S_IFREG,
  987. NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
  988. if (err)
  989. goto out;
  990. if (EX_ISSYNC(fhp->fh_export)) {
  991. int err2 = vfs_fsync_range(file, offset, end, 0);
  992. if (err2 != -EINVAL)
  993. err = nfserrno(err2);
  994. else
  995. err = nfserr_notsupp;
  996. }
  997. fput(file);
  998. out:
  999. return err;
  1000. }
  1001. #endif /* CONFIG_NFSD_V3 */
  1002. static __be32
  1003. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1004. struct iattr *iap)
  1005. {
  1006. /*
  1007. * Mode has already been set earlier in create:
  1008. */
  1009. iap->ia_valid &= ~ATTR_MODE;
  1010. /*
  1011. * Setting uid/gid works only for root. Irix appears to
  1012. * send along the gid on create when it tries to implement
  1013. * setgid directories via NFS:
  1014. */
  1015. if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  1016. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1017. if (iap->ia_valid)
  1018. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1019. /* Callers expect file metadata to be committed here */
  1020. return nfserrno(commit_metadata(resfhp));
  1021. }
  1022. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1023. * setting size to 0 may fail for some specific file systems by the permission
  1024. * checking which requires WRITE permission but the mode is 000.
  1025. * we ignore the resizing(to 0) on the just new created file, since the size is
  1026. * 0 after file created.
  1027. *
  1028. * call this only after vfs_create() is called.
  1029. * */
  1030. static void
  1031. nfsd_check_ignore_resizing(struct iattr *iap)
  1032. {
  1033. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1034. iap->ia_valid &= ~ATTR_SIZE;
  1035. }
  1036. /* The parent directory should already be locked: */
  1037. __be32
  1038. nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1039. char *fname, int flen, struct iattr *iap,
  1040. int type, dev_t rdev, struct svc_fh *resfhp)
  1041. {
  1042. struct dentry *dentry, *dchild;
  1043. struct inode *dirp;
  1044. __be32 err;
  1045. __be32 err2;
  1046. int host_err;
  1047. dentry = fhp->fh_dentry;
  1048. dirp = d_inode(dentry);
  1049. dchild = dget(resfhp->fh_dentry);
  1050. if (!fhp->fh_locked) {
  1051. WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
  1052. dentry);
  1053. err = nfserr_io;
  1054. goto out;
  1055. }
  1056. err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
  1057. if (err)
  1058. goto out;
  1059. if (!(iap->ia_valid & ATTR_MODE))
  1060. iap->ia_mode = 0;
  1061. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1062. err = 0;
  1063. host_err = 0;
  1064. switch (type) {
  1065. case S_IFREG:
  1066. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1067. if (!host_err)
  1068. nfsd_check_ignore_resizing(iap);
  1069. break;
  1070. case S_IFDIR:
  1071. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1072. if (!host_err && unlikely(d_unhashed(dchild))) {
  1073. struct dentry *d;
  1074. d = lookup_one_len(dchild->d_name.name,
  1075. dchild->d_parent,
  1076. dchild->d_name.len);
  1077. if (IS_ERR(d)) {
  1078. host_err = PTR_ERR(d);
  1079. break;
  1080. }
  1081. if (unlikely(d_is_negative(d))) {
  1082. dput(d);
  1083. err = nfserr_serverfault;
  1084. goto out;
  1085. }
  1086. dput(resfhp->fh_dentry);
  1087. resfhp->fh_dentry = dget(d);
  1088. err = fh_update(resfhp);
  1089. dput(dchild);
  1090. dchild = d;
  1091. if (err)
  1092. goto out;
  1093. }
  1094. break;
  1095. case S_IFCHR:
  1096. case S_IFBLK:
  1097. case S_IFIFO:
  1098. case S_IFSOCK:
  1099. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1100. break;
  1101. default:
  1102. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1103. type);
  1104. host_err = -EINVAL;
  1105. }
  1106. if (host_err < 0)
  1107. goto out_nfserr;
  1108. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1109. /*
  1110. * nfsd_create_setattr already committed the child. Transactional
  1111. * filesystems had a chance to commit changes for both parent and
  1112. * child simultaneously making the following commit_metadata a
  1113. * noop.
  1114. */
  1115. err2 = nfserrno(commit_metadata(fhp));
  1116. if (err2)
  1117. err = err2;
  1118. /*
  1119. * Update the file handle to get the new inode info.
  1120. */
  1121. if (!err)
  1122. err = fh_update(resfhp);
  1123. out:
  1124. dput(dchild);
  1125. return err;
  1126. out_nfserr:
  1127. err = nfserrno(host_err);
  1128. goto out;
  1129. }
  1130. /*
  1131. * Create a filesystem object (regular, directory, special).
  1132. * Note that the parent directory is left locked.
  1133. *
  1134. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1135. */
  1136. __be32
  1137. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1138. char *fname, int flen, struct iattr *iap,
  1139. int type, dev_t rdev, struct svc_fh *resfhp)
  1140. {
  1141. struct dentry *dentry, *dchild = NULL;
  1142. struct inode *dirp;
  1143. __be32 err;
  1144. int host_err;
  1145. if (isdotent(fname, flen))
  1146. return nfserr_exist;
  1147. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
  1148. if (err)
  1149. return err;
  1150. dentry = fhp->fh_dentry;
  1151. dirp = d_inode(dentry);
  1152. host_err = fh_want_write(fhp);
  1153. if (host_err)
  1154. return nfserrno(host_err);
  1155. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1156. dchild = lookup_one_len(fname, dentry, flen);
  1157. host_err = PTR_ERR(dchild);
  1158. if (IS_ERR(dchild))
  1159. return nfserrno(host_err);
  1160. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1161. /*
  1162. * We unconditionally drop our ref to dchild as fh_compose will have
  1163. * already grabbed its own ref for it.
  1164. */
  1165. dput(dchild);
  1166. if (err)
  1167. return err;
  1168. return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
  1169. rdev, resfhp);
  1170. }
  1171. #ifdef CONFIG_NFSD_V3
  1172. /*
  1173. * NFSv3 and NFSv4 version of nfsd_create
  1174. */
  1175. __be32
  1176. do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1177. char *fname, int flen, struct iattr *iap,
  1178. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1179. bool *truncp, bool *created)
  1180. {
  1181. struct dentry *dentry, *dchild = NULL;
  1182. struct inode *dirp;
  1183. __be32 err;
  1184. int host_err;
  1185. __u32 v_mtime=0, v_atime=0;
  1186. err = nfserr_perm;
  1187. if (!flen)
  1188. goto out;
  1189. err = nfserr_exist;
  1190. if (isdotent(fname, flen))
  1191. goto out;
  1192. if (!(iap->ia_valid & ATTR_MODE))
  1193. iap->ia_mode = 0;
  1194. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  1195. if (err)
  1196. goto out;
  1197. dentry = fhp->fh_dentry;
  1198. dirp = d_inode(dentry);
  1199. host_err = fh_want_write(fhp);
  1200. if (host_err)
  1201. goto out_nfserr;
  1202. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1203. /*
  1204. * Compose the response file handle.
  1205. */
  1206. dchild = lookup_one_len(fname, dentry, flen);
  1207. host_err = PTR_ERR(dchild);
  1208. if (IS_ERR(dchild))
  1209. goto out_nfserr;
  1210. /* If file doesn't exist, check for permissions to create one */
  1211. if (d_really_is_negative(dchild)) {
  1212. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1213. if (err)
  1214. goto out;
  1215. }
  1216. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1217. if (err)
  1218. goto out;
  1219. if (nfsd_create_is_exclusive(createmode)) {
  1220. /* solaris7 gets confused (bugid 4218508) if these have
  1221. * the high bit set, so just clear the high bits. If this is
  1222. * ever changed to use different attrs for storing the
  1223. * verifier, then do_open_lookup() will also need to be fixed
  1224. * accordingly.
  1225. */
  1226. v_mtime = verifier[0]&0x7fffffff;
  1227. v_atime = verifier[1]&0x7fffffff;
  1228. }
  1229. if (d_really_is_positive(dchild)) {
  1230. err = 0;
  1231. switch (createmode) {
  1232. case NFS3_CREATE_UNCHECKED:
  1233. if (! d_is_reg(dchild))
  1234. goto out;
  1235. else if (truncp) {
  1236. /* in nfsv4, we need to treat this case a little
  1237. * differently. we don't want to truncate the
  1238. * file now; this would be wrong if the OPEN
  1239. * fails for some other reason. furthermore,
  1240. * if the size is nonzero, we should ignore it
  1241. * according to spec!
  1242. */
  1243. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1244. }
  1245. else {
  1246. iap->ia_valid &= ATTR_SIZE;
  1247. goto set_attr;
  1248. }
  1249. break;
  1250. case NFS3_CREATE_EXCLUSIVE:
  1251. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1252. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1253. && d_inode(dchild)->i_size == 0 ) {
  1254. if (created)
  1255. *created = 1;
  1256. break;
  1257. }
  1258. case NFS4_CREATE_EXCLUSIVE4_1:
  1259. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1260. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1261. && d_inode(dchild)->i_size == 0 ) {
  1262. if (created)
  1263. *created = 1;
  1264. goto set_attr;
  1265. }
  1266. /* fallthru */
  1267. case NFS3_CREATE_GUARDED:
  1268. err = nfserr_exist;
  1269. }
  1270. fh_drop_write(fhp);
  1271. goto out;
  1272. }
  1273. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1274. if (host_err < 0) {
  1275. fh_drop_write(fhp);
  1276. goto out_nfserr;
  1277. }
  1278. if (created)
  1279. *created = 1;
  1280. nfsd_check_ignore_resizing(iap);
  1281. if (nfsd_create_is_exclusive(createmode)) {
  1282. /* Cram the verifier into atime/mtime */
  1283. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1284. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1285. /* XXX someone who knows this better please fix it for nsec */
  1286. iap->ia_mtime.tv_sec = v_mtime;
  1287. iap->ia_atime.tv_sec = v_atime;
  1288. iap->ia_mtime.tv_nsec = 0;
  1289. iap->ia_atime.tv_nsec = 0;
  1290. }
  1291. set_attr:
  1292. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1293. /*
  1294. * nfsd_create_setattr already committed the child
  1295. * (and possibly also the parent).
  1296. */
  1297. if (!err)
  1298. err = nfserrno(commit_metadata(fhp));
  1299. /*
  1300. * Update the filehandle to get the new inode info.
  1301. */
  1302. if (!err)
  1303. err = fh_update(resfhp);
  1304. out:
  1305. fh_unlock(fhp);
  1306. if (dchild && !IS_ERR(dchild))
  1307. dput(dchild);
  1308. fh_drop_write(fhp);
  1309. return err;
  1310. out_nfserr:
  1311. err = nfserrno(host_err);
  1312. goto out;
  1313. }
  1314. #endif /* CONFIG_NFSD_V3 */
  1315. /*
  1316. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1317. * fits into the buffer. On return, it contains the true length.
  1318. * N.B. After this call fhp needs an fh_put
  1319. */
  1320. __be32
  1321. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1322. {
  1323. __be32 err;
  1324. const char *link;
  1325. struct path path;
  1326. DEFINE_DELAYED_CALL(done);
  1327. int len;
  1328. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1329. if (unlikely(err))
  1330. return err;
  1331. path.mnt = fhp->fh_export->ex_path.mnt;
  1332. path.dentry = fhp->fh_dentry;
  1333. if (unlikely(!d_is_symlink(path.dentry)))
  1334. return nfserr_inval;
  1335. touch_atime(&path);
  1336. link = vfs_get_link(path.dentry, &done);
  1337. if (IS_ERR(link))
  1338. return nfserrno(PTR_ERR(link));
  1339. len = strlen(link);
  1340. if (len < *lenp)
  1341. *lenp = len;
  1342. memcpy(buf, link, *lenp);
  1343. do_delayed_call(&done);
  1344. return 0;
  1345. }
  1346. /*
  1347. * Create a symlink and look up its inode
  1348. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1349. */
  1350. __be32
  1351. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1352. char *fname, int flen,
  1353. char *path,
  1354. struct svc_fh *resfhp)
  1355. {
  1356. struct dentry *dentry, *dnew;
  1357. __be32 err, cerr;
  1358. int host_err;
  1359. err = nfserr_noent;
  1360. if (!flen || path[0] == '\0')
  1361. goto out;
  1362. err = nfserr_exist;
  1363. if (isdotent(fname, flen))
  1364. goto out;
  1365. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1366. if (err)
  1367. goto out;
  1368. host_err = fh_want_write(fhp);
  1369. if (host_err)
  1370. goto out_nfserr;
  1371. fh_lock(fhp);
  1372. dentry = fhp->fh_dentry;
  1373. dnew = lookup_one_len(fname, dentry, flen);
  1374. host_err = PTR_ERR(dnew);
  1375. if (IS_ERR(dnew))
  1376. goto out_nfserr;
  1377. host_err = vfs_symlink(d_inode(dentry), dnew, path);
  1378. err = nfserrno(host_err);
  1379. if (!err)
  1380. err = nfserrno(commit_metadata(fhp));
  1381. fh_unlock(fhp);
  1382. fh_drop_write(fhp);
  1383. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1384. dput(dnew);
  1385. if (err==0) err = cerr;
  1386. out:
  1387. return err;
  1388. out_nfserr:
  1389. err = nfserrno(host_err);
  1390. goto out;
  1391. }
  1392. /*
  1393. * Create a hardlink
  1394. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1395. */
  1396. __be32
  1397. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1398. char *name, int len, struct svc_fh *tfhp)
  1399. {
  1400. struct dentry *ddir, *dnew, *dold;
  1401. struct inode *dirp;
  1402. __be32 err;
  1403. int host_err;
  1404. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1405. if (err)
  1406. goto out;
  1407. err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
  1408. if (err)
  1409. goto out;
  1410. err = nfserr_isdir;
  1411. if (d_is_dir(tfhp->fh_dentry))
  1412. goto out;
  1413. err = nfserr_perm;
  1414. if (!len)
  1415. goto out;
  1416. err = nfserr_exist;
  1417. if (isdotent(name, len))
  1418. goto out;
  1419. host_err = fh_want_write(tfhp);
  1420. if (host_err) {
  1421. err = nfserrno(host_err);
  1422. goto out;
  1423. }
  1424. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1425. ddir = ffhp->fh_dentry;
  1426. dirp = d_inode(ddir);
  1427. dnew = lookup_one_len(name, ddir, len);
  1428. host_err = PTR_ERR(dnew);
  1429. if (IS_ERR(dnew))
  1430. goto out_nfserr;
  1431. dold = tfhp->fh_dentry;
  1432. err = nfserr_noent;
  1433. if (d_really_is_negative(dold))
  1434. goto out_dput;
  1435. host_err = vfs_link(dold, dirp, dnew, NULL);
  1436. if (!host_err) {
  1437. err = nfserrno(commit_metadata(ffhp));
  1438. if (!err)
  1439. err = nfserrno(commit_metadata(tfhp));
  1440. } else {
  1441. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1442. err = nfserr_acces;
  1443. else
  1444. err = nfserrno(host_err);
  1445. }
  1446. out_dput:
  1447. dput(dnew);
  1448. out_unlock:
  1449. fh_unlock(ffhp);
  1450. fh_drop_write(tfhp);
  1451. out:
  1452. return err;
  1453. out_nfserr:
  1454. err = nfserrno(host_err);
  1455. goto out_unlock;
  1456. }
  1457. /*
  1458. * Rename a file
  1459. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1460. */
  1461. __be32
  1462. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1463. struct svc_fh *tfhp, char *tname, int tlen)
  1464. {
  1465. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1466. struct inode *fdir, *tdir;
  1467. __be32 err;
  1468. int host_err;
  1469. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1470. if (err)
  1471. goto out;
  1472. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1473. if (err)
  1474. goto out;
  1475. fdentry = ffhp->fh_dentry;
  1476. fdir = d_inode(fdentry);
  1477. tdentry = tfhp->fh_dentry;
  1478. tdir = d_inode(tdentry);
  1479. err = nfserr_perm;
  1480. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1481. goto out;
  1482. host_err = fh_want_write(ffhp);
  1483. if (host_err) {
  1484. err = nfserrno(host_err);
  1485. goto out;
  1486. }
  1487. /* cannot use fh_lock as we need deadlock protective ordering
  1488. * so do it by hand */
  1489. trap = lock_rename(tdentry, fdentry);
  1490. ffhp->fh_locked = tfhp->fh_locked = true;
  1491. fill_pre_wcc(ffhp);
  1492. fill_pre_wcc(tfhp);
  1493. odentry = lookup_one_len(fname, fdentry, flen);
  1494. host_err = PTR_ERR(odentry);
  1495. if (IS_ERR(odentry))
  1496. goto out_nfserr;
  1497. host_err = -ENOENT;
  1498. if (d_really_is_negative(odentry))
  1499. goto out_dput_old;
  1500. host_err = -EINVAL;
  1501. if (odentry == trap)
  1502. goto out_dput_old;
  1503. ndentry = lookup_one_len(tname, tdentry, tlen);
  1504. host_err = PTR_ERR(ndentry);
  1505. if (IS_ERR(ndentry))
  1506. goto out_dput_old;
  1507. host_err = -ENOTEMPTY;
  1508. if (ndentry == trap)
  1509. goto out_dput_new;
  1510. host_err = -EXDEV;
  1511. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1512. goto out_dput_new;
  1513. if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
  1514. goto out_dput_new;
  1515. host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
  1516. if (!host_err) {
  1517. host_err = commit_metadata(tfhp);
  1518. if (!host_err)
  1519. host_err = commit_metadata(ffhp);
  1520. }
  1521. out_dput_new:
  1522. dput(ndentry);
  1523. out_dput_old:
  1524. dput(odentry);
  1525. out_nfserr:
  1526. err = nfserrno(host_err);
  1527. /*
  1528. * We cannot rely on fh_unlock on the two filehandles,
  1529. * as that would do the wrong thing if the two directories
  1530. * were the same, so again we do it by hand.
  1531. */
  1532. fill_post_wcc(ffhp);
  1533. fill_post_wcc(tfhp);
  1534. unlock_rename(tdentry, fdentry);
  1535. ffhp->fh_locked = tfhp->fh_locked = false;
  1536. fh_drop_write(ffhp);
  1537. out:
  1538. return err;
  1539. }
  1540. /*
  1541. * Unlink a file or directory
  1542. * N.B. After this call fhp needs an fh_put
  1543. */
  1544. __be32
  1545. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1546. char *fname, int flen)
  1547. {
  1548. struct dentry *dentry, *rdentry;
  1549. struct inode *dirp;
  1550. __be32 err;
  1551. int host_err;
  1552. err = nfserr_acces;
  1553. if (!flen || isdotent(fname, flen))
  1554. goto out;
  1555. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1556. if (err)
  1557. goto out;
  1558. host_err = fh_want_write(fhp);
  1559. if (host_err)
  1560. goto out_nfserr;
  1561. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1562. dentry = fhp->fh_dentry;
  1563. dirp = d_inode(dentry);
  1564. rdentry = lookup_one_len(fname, dentry, flen);
  1565. host_err = PTR_ERR(rdentry);
  1566. if (IS_ERR(rdentry))
  1567. goto out_nfserr;
  1568. if (d_really_is_negative(rdentry)) {
  1569. dput(rdentry);
  1570. err = nfserr_noent;
  1571. goto out;
  1572. }
  1573. if (!type)
  1574. type = d_inode(rdentry)->i_mode & S_IFMT;
  1575. if (type != S_IFDIR)
  1576. host_err = vfs_unlink(dirp, rdentry, NULL);
  1577. else
  1578. host_err = vfs_rmdir(dirp, rdentry);
  1579. if (!host_err)
  1580. host_err = commit_metadata(fhp);
  1581. dput(rdentry);
  1582. out_nfserr:
  1583. err = nfserrno(host_err);
  1584. out:
  1585. return err;
  1586. }
  1587. /*
  1588. * We do this buffering because we must not call back into the file
  1589. * system's ->lookup() method from the filldir callback. That may well
  1590. * deadlock a number of file systems.
  1591. *
  1592. * This is based heavily on the implementation of same in XFS.
  1593. */
  1594. struct buffered_dirent {
  1595. u64 ino;
  1596. loff_t offset;
  1597. int namlen;
  1598. unsigned int d_type;
  1599. char name[];
  1600. };
  1601. struct readdir_data {
  1602. struct dir_context ctx;
  1603. char *dirent;
  1604. size_t used;
  1605. int full;
  1606. };
  1607. static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
  1608. int namlen, loff_t offset, u64 ino,
  1609. unsigned int d_type)
  1610. {
  1611. struct readdir_data *buf =
  1612. container_of(ctx, struct readdir_data, ctx);
  1613. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1614. unsigned int reclen;
  1615. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1616. if (buf->used + reclen > PAGE_SIZE) {
  1617. buf->full = 1;
  1618. return -EINVAL;
  1619. }
  1620. de->namlen = namlen;
  1621. de->offset = offset;
  1622. de->ino = ino;
  1623. de->d_type = d_type;
  1624. memcpy(de->name, name, namlen);
  1625. buf->used += reclen;
  1626. return 0;
  1627. }
  1628. static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
  1629. struct readdir_cd *cdp, loff_t *offsetp)
  1630. {
  1631. struct buffered_dirent *de;
  1632. int host_err;
  1633. int size;
  1634. loff_t offset;
  1635. struct readdir_data buf = {
  1636. .ctx.actor = nfsd_buffered_filldir,
  1637. .dirent = (void *)__get_free_page(GFP_KERNEL)
  1638. };
  1639. if (!buf.dirent)
  1640. return nfserrno(-ENOMEM);
  1641. offset = *offsetp;
  1642. while (1) {
  1643. unsigned int reclen;
  1644. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1645. buf.used = 0;
  1646. buf.full = 0;
  1647. host_err = iterate_dir(file, &buf.ctx);
  1648. if (buf.full)
  1649. host_err = 0;
  1650. if (host_err < 0)
  1651. break;
  1652. size = buf.used;
  1653. if (!size)
  1654. break;
  1655. de = (struct buffered_dirent *)buf.dirent;
  1656. while (size > 0) {
  1657. offset = de->offset;
  1658. if (func(cdp, de->name, de->namlen, de->offset,
  1659. de->ino, de->d_type))
  1660. break;
  1661. if (cdp->err != nfs_ok)
  1662. break;
  1663. reclen = ALIGN(sizeof(*de) + de->namlen,
  1664. sizeof(u64));
  1665. size -= reclen;
  1666. de = (struct buffered_dirent *)((char *)de + reclen);
  1667. }
  1668. if (size > 0) /* We bailed out early */
  1669. break;
  1670. offset = vfs_llseek(file, 0, SEEK_CUR);
  1671. }
  1672. free_page((unsigned long)(buf.dirent));
  1673. if (host_err)
  1674. return nfserrno(host_err);
  1675. *offsetp = offset;
  1676. return cdp->err;
  1677. }
  1678. /*
  1679. * Read entries from a directory.
  1680. * The NFSv3/4 verifier we ignore for now.
  1681. */
  1682. __be32
  1683. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1684. struct readdir_cd *cdp, nfsd_filldir_t func)
  1685. {
  1686. __be32 err;
  1687. struct file *file;
  1688. loff_t offset = *offsetp;
  1689. int may_flags = NFSD_MAY_READ;
  1690. /* NFSv2 only supports 32 bit cookies */
  1691. if (rqstp->rq_vers > 2)
  1692. may_flags |= NFSD_MAY_64BIT_COOKIE;
  1693. err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
  1694. if (err)
  1695. goto out;
  1696. offset = vfs_llseek(file, offset, SEEK_SET);
  1697. if (offset < 0) {
  1698. err = nfserrno((int)offset);
  1699. goto out_close;
  1700. }
  1701. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1702. if (err == nfserr_eof || err == nfserr_toosmall)
  1703. err = nfs_ok; /* can still be found in ->err */
  1704. out_close:
  1705. fput(file);
  1706. out:
  1707. return err;
  1708. }
  1709. /*
  1710. * Get file system stats
  1711. * N.B. After this call fhp needs an fh_put
  1712. */
  1713. __be32
  1714. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1715. {
  1716. __be32 err;
  1717. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1718. if (!err) {
  1719. struct path path = {
  1720. .mnt = fhp->fh_export->ex_path.mnt,
  1721. .dentry = fhp->fh_dentry,
  1722. };
  1723. if (vfs_statfs(&path, stat))
  1724. err = nfserr_io;
  1725. }
  1726. return err;
  1727. }
  1728. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1729. {
  1730. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1731. }
  1732. /*
  1733. * Check for a user's access permissions to this inode.
  1734. */
  1735. __be32
  1736. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1737. struct dentry *dentry, int acc)
  1738. {
  1739. struct inode *inode = d_inode(dentry);
  1740. int err;
  1741. if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
  1742. return 0;
  1743. #if 0
  1744. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1745. acc,
  1746. (acc & NFSD_MAY_READ)? " read" : "",
  1747. (acc & NFSD_MAY_WRITE)? " write" : "",
  1748. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1749. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1750. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1751. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1752. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1753. inode->i_mode,
  1754. IS_IMMUTABLE(inode)? " immut" : "",
  1755. IS_APPEND(inode)? " append" : "",
  1756. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1757. dprintk(" owner %d/%d user %d/%d\n",
  1758. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1759. #endif
  1760. /* Normally we reject any write/sattr etc access on a read-only file
  1761. * system. But if it is IRIX doing check on write-access for a
  1762. * device special file, we ignore rofs.
  1763. */
  1764. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1765. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1766. if (exp_rdonly(rqstp, exp) ||
  1767. __mnt_is_readonly(exp->ex_path.mnt))
  1768. return nfserr_rofs;
  1769. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1770. return nfserr_perm;
  1771. }
  1772. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1773. return nfserr_perm;
  1774. if (acc & NFSD_MAY_LOCK) {
  1775. /* If we cannot rely on authentication in NLM requests,
  1776. * just allow locks, otherwise require read permission, or
  1777. * ownership
  1778. */
  1779. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1780. return 0;
  1781. else
  1782. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1783. }
  1784. /*
  1785. * The file owner always gets access permission for accesses that
  1786. * would normally be checked at open time. This is to make
  1787. * file access work even when the client has done a fchmod(fd, 0).
  1788. *
  1789. * However, `cp foo bar' should fail nevertheless when bar is
  1790. * readonly. A sensible way to do this might be to reject all
  1791. * attempts to truncate a read-only file, because a creat() call
  1792. * always implies file truncation.
  1793. * ... but this isn't really fair. A process may reasonably call
  1794. * ftruncate on an open file descriptor on a file with perm 000.
  1795. * We must trust the client to do permission checking - using "ACCESS"
  1796. * with NFSv3.
  1797. */
  1798. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1799. uid_eq(inode->i_uid, current_fsuid()))
  1800. return 0;
  1801. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1802. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1803. /* Allow read access to binaries even when mode 111 */
  1804. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1805. (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
  1806. acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
  1807. err = inode_permission(inode, MAY_EXEC);
  1808. return err? nfserrno(err) : 0;
  1809. }
  1810. void
  1811. nfsd_racache_shutdown(void)
  1812. {
  1813. struct raparms *raparm, *last_raparm;
  1814. unsigned int i;
  1815. dprintk("nfsd: freeing readahead buffers.\n");
  1816. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1817. raparm = raparm_hash[i].pb_head;
  1818. while(raparm) {
  1819. last_raparm = raparm;
  1820. raparm = raparm->p_next;
  1821. kfree(last_raparm);
  1822. }
  1823. raparm_hash[i].pb_head = NULL;
  1824. }
  1825. }
  1826. /*
  1827. * Initialize readahead param cache
  1828. */
  1829. int
  1830. nfsd_racache_init(int cache_size)
  1831. {
  1832. int i;
  1833. int j = 0;
  1834. int nperbucket;
  1835. struct raparms **raparm = NULL;
  1836. if (raparm_hash[0].pb_head)
  1837. return 0;
  1838. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1839. nperbucket = max(2, nperbucket);
  1840. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1841. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1842. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1843. spin_lock_init(&raparm_hash[i].pb_lock);
  1844. raparm = &raparm_hash[i].pb_head;
  1845. for (j = 0; j < nperbucket; j++) {
  1846. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1847. if (!*raparm)
  1848. goto out_nomem;
  1849. raparm = &(*raparm)->p_next;
  1850. }
  1851. *raparm = NULL;
  1852. }
  1853. nfsdstats.ra_size = cache_size;
  1854. return 0;
  1855. out_nomem:
  1856. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1857. nfsd_racache_shutdown();
  1858. return -ENOMEM;
  1859. }