vfs.c 51 KB

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