vfs.c 50 KB

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