vfs.c 51 KB

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