vfs.c 49 KB

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