vfs.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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. /*
  1000. * Create a file (regular, directory, device, fifo); UNIX sockets
  1001. * not yet implemented.
  1002. * If the response fh has been verified, the parent directory should
  1003. * already be locked. Note that the parent directory is left locked.
  1004. *
  1005. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1006. */
  1007. __be32
  1008. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1009. char *fname, int flen, struct iattr *iap,
  1010. int type, dev_t rdev, struct svc_fh *resfhp)
  1011. {
  1012. struct dentry *dentry, *dchild = NULL;
  1013. struct inode *dirp;
  1014. __be32 err;
  1015. __be32 err2;
  1016. int host_err;
  1017. err = nfserr_perm;
  1018. if (!flen)
  1019. goto out;
  1020. err = nfserr_exist;
  1021. if (isdotent(fname, flen))
  1022. goto out;
  1023. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1024. if (err)
  1025. goto out;
  1026. dentry = fhp->fh_dentry;
  1027. dirp = d_inode(dentry);
  1028. err = nfserr_notdir;
  1029. if (!dirp->i_op->lookup)
  1030. goto out;
  1031. /*
  1032. * Check whether the response file handle has been verified yet.
  1033. * If it has, the parent directory should already be locked.
  1034. */
  1035. if (!resfhp->fh_dentry) {
  1036. host_err = fh_want_write(fhp);
  1037. if (host_err)
  1038. goto out_nfserr;
  1039. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1040. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1041. dchild = lookup_one_len(fname, dentry, flen);
  1042. host_err = PTR_ERR(dchild);
  1043. if (IS_ERR(dchild))
  1044. goto out_nfserr;
  1045. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1046. if (err)
  1047. goto out;
  1048. } else {
  1049. /* called from nfsd_proc_create */
  1050. dchild = dget(resfhp->fh_dentry);
  1051. if (!fhp->fh_locked) {
  1052. /* not actually possible */
  1053. printk(KERN_ERR
  1054. "nfsd_create: parent %pd2 not locked!\n",
  1055. dentry);
  1056. err = nfserr_io;
  1057. goto out;
  1058. }
  1059. }
  1060. /*
  1061. * Make sure the child dentry is still negative ...
  1062. */
  1063. err = nfserr_exist;
  1064. if (d_really_is_positive(dchild)) {
  1065. dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
  1066. dentry, dchild);
  1067. goto out;
  1068. }
  1069. if (!(iap->ia_valid & ATTR_MODE))
  1070. iap->ia_mode = 0;
  1071. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1072. err = nfserr_inval;
  1073. if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
  1074. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1075. type);
  1076. goto out;
  1077. }
  1078. /*
  1079. * Get the dir op function pointer.
  1080. */
  1081. err = 0;
  1082. host_err = 0;
  1083. switch (type) {
  1084. case S_IFREG:
  1085. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1086. if (!host_err)
  1087. nfsd_check_ignore_resizing(iap);
  1088. break;
  1089. case S_IFDIR:
  1090. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1091. break;
  1092. case S_IFCHR:
  1093. case S_IFBLK:
  1094. case S_IFIFO:
  1095. case S_IFSOCK:
  1096. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1097. break;
  1098. }
  1099. if (host_err < 0)
  1100. goto out_nfserr;
  1101. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1102. /*
  1103. * nfsd_create_setattr already committed the child. Transactional
  1104. * filesystems had a chance to commit changes for both parent and
  1105. * child * simultaneously making the following commit_metadata a
  1106. * noop.
  1107. */
  1108. err2 = nfserrno(commit_metadata(fhp));
  1109. if (err2)
  1110. err = err2;
  1111. /*
  1112. * Update the file handle to get the new inode info.
  1113. */
  1114. if (!err)
  1115. err = fh_update(resfhp);
  1116. out:
  1117. if (dchild && !IS_ERR(dchild))
  1118. dput(dchild);
  1119. return err;
  1120. out_nfserr:
  1121. err = nfserrno(host_err);
  1122. goto out;
  1123. }
  1124. #ifdef CONFIG_NFSD_V3
  1125. /*
  1126. * NFSv3 and NFSv4 version of nfsd_create
  1127. */
  1128. __be32
  1129. do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1130. char *fname, int flen, struct iattr *iap,
  1131. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1132. bool *truncp, bool *created)
  1133. {
  1134. struct dentry *dentry, *dchild = NULL;
  1135. struct inode *dirp;
  1136. __be32 err;
  1137. int host_err;
  1138. __u32 v_mtime=0, v_atime=0;
  1139. err = nfserr_perm;
  1140. if (!flen)
  1141. goto out;
  1142. err = nfserr_exist;
  1143. if (isdotent(fname, flen))
  1144. goto out;
  1145. if (!(iap->ia_valid & ATTR_MODE))
  1146. iap->ia_mode = 0;
  1147. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  1148. if (err)
  1149. goto out;
  1150. dentry = fhp->fh_dentry;
  1151. dirp = d_inode(dentry);
  1152. /* Get all the sanity checks out of the way before
  1153. * we lock the parent. */
  1154. err = nfserr_notdir;
  1155. if (!dirp->i_op->lookup)
  1156. goto out;
  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. struct inode *inode;
  1282. mm_segment_t oldfs;
  1283. __be32 err;
  1284. int host_err;
  1285. struct path path;
  1286. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1287. if (err)
  1288. goto out;
  1289. path.mnt = fhp->fh_export->ex_path.mnt;
  1290. path.dentry = fhp->fh_dentry;
  1291. inode = d_inode(path.dentry);
  1292. err = nfserr_inval;
  1293. if (!inode->i_op->readlink)
  1294. goto out;
  1295. touch_atime(&path);
  1296. /* N.B. Why does this call need a get_fs()??
  1297. * Remove the set_fs and watch the fireworks:-) --okir
  1298. */
  1299. oldfs = get_fs(); set_fs(KERNEL_DS);
  1300. host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
  1301. set_fs(oldfs);
  1302. if (host_err < 0)
  1303. goto out_nfserr;
  1304. *lenp = host_err;
  1305. err = 0;
  1306. out:
  1307. return err;
  1308. out_nfserr:
  1309. err = nfserrno(host_err);
  1310. goto out;
  1311. }
  1312. /*
  1313. * Create a symlink and look up its inode
  1314. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1315. */
  1316. __be32
  1317. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1318. char *fname, int flen,
  1319. char *path,
  1320. struct svc_fh *resfhp)
  1321. {
  1322. struct dentry *dentry, *dnew;
  1323. __be32 err, cerr;
  1324. int host_err;
  1325. err = nfserr_noent;
  1326. if (!flen || path[0] == '\0')
  1327. goto out;
  1328. err = nfserr_exist;
  1329. if (isdotent(fname, flen))
  1330. goto out;
  1331. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1332. if (err)
  1333. goto out;
  1334. host_err = fh_want_write(fhp);
  1335. if (host_err)
  1336. goto out_nfserr;
  1337. fh_lock(fhp);
  1338. dentry = fhp->fh_dentry;
  1339. dnew = lookup_one_len(fname, dentry, flen);
  1340. host_err = PTR_ERR(dnew);
  1341. if (IS_ERR(dnew))
  1342. goto out_nfserr;
  1343. host_err = vfs_symlink(d_inode(dentry), dnew, path);
  1344. err = nfserrno(host_err);
  1345. if (!err)
  1346. err = nfserrno(commit_metadata(fhp));
  1347. fh_unlock(fhp);
  1348. fh_drop_write(fhp);
  1349. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1350. dput(dnew);
  1351. if (err==0) err = cerr;
  1352. out:
  1353. return err;
  1354. out_nfserr:
  1355. err = nfserrno(host_err);
  1356. goto out;
  1357. }
  1358. /*
  1359. * Create a hardlink
  1360. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1361. */
  1362. __be32
  1363. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1364. char *name, int len, struct svc_fh *tfhp)
  1365. {
  1366. struct dentry *ddir, *dnew, *dold;
  1367. struct inode *dirp;
  1368. __be32 err;
  1369. int host_err;
  1370. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1371. if (err)
  1372. goto out;
  1373. err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
  1374. if (err)
  1375. goto out;
  1376. err = nfserr_isdir;
  1377. if (d_is_dir(tfhp->fh_dentry))
  1378. goto out;
  1379. err = nfserr_perm;
  1380. if (!len)
  1381. goto out;
  1382. err = nfserr_exist;
  1383. if (isdotent(name, len))
  1384. goto out;
  1385. host_err = fh_want_write(tfhp);
  1386. if (host_err) {
  1387. err = nfserrno(host_err);
  1388. goto out;
  1389. }
  1390. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1391. ddir = ffhp->fh_dentry;
  1392. dirp = d_inode(ddir);
  1393. dnew = lookup_one_len(name, ddir, len);
  1394. host_err = PTR_ERR(dnew);
  1395. if (IS_ERR(dnew))
  1396. goto out_nfserr;
  1397. dold = tfhp->fh_dentry;
  1398. err = nfserr_noent;
  1399. if (d_really_is_negative(dold))
  1400. goto out_dput;
  1401. host_err = vfs_link(dold, dirp, dnew, NULL);
  1402. if (!host_err) {
  1403. err = nfserrno(commit_metadata(ffhp));
  1404. if (!err)
  1405. err = nfserrno(commit_metadata(tfhp));
  1406. } else {
  1407. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1408. err = nfserr_acces;
  1409. else
  1410. err = nfserrno(host_err);
  1411. }
  1412. out_dput:
  1413. dput(dnew);
  1414. out_unlock:
  1415. fh_unlock(ffhp);
  1416. fh_drop_write(tfhp);
  1417. out:
  1418. return err;
  1419. out_nfserr:
  1420. err = nfserrno(host_err);
  1421. goto out_unlock;
  1422. }
  1423. /*
  1424. * Rename a file
  1425. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1426. */
  1427. __be32
  1428. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1429. struct svc_fh *tfhp, char *tname, int tlen)
  1430. {
  1431. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1432. struct inode *fdir, *tdir;
  1433. __be32 err;
  1434. int host_err;
  1435. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1436. if (err)
  1437. goto out;
  1438. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1439. if (err)
  1440. goto out;
  1441. fdentry = ffhp->fh_dentry;
  1442. fdir = d_inode(fdentry);
  1443. tdentry = tfhp->fh_dentry;
  1444. tdir = d_inode(tdentry);
  1445. err = nfserr_perm;
  1446. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1447. goto out;
  1448. host_err = fh_want_write(ffhp);
  1449. if (host_err) {
  1450. err = nfserrno(host_err);
  1451. goto out;
  1452. }
  1453. /* cannot use fh_lock as we need deadlock protective ordering
  1454. * so do it by hand */
  1455. trap = lock_rename(tdentry, fdentry);
  1456. ffhp->fh_locked = tfhp->fh_locked = true;
  1457. fill_pre_wcc(ffhp);
  1458. fill_pre_wcc(tfhp);
  1459. odentry = lookup_one_len(fname, fdentry, flen);
  1460. host_err = PTR_ERR(odentry);
  1461. if (IS_ERR(odentry))
  1462. goto out_nfserr;
  1463. host_err = -ENOENT;
  1464. if (d_really_is_negative(odentry))
  1465. goto out_dput_old;
  1466. host_err = -EINVAL;
  1467. if (odentry == trap)
  1468. goto out_dput_old;
  1469. ndentry = lookup_one_len(tname, tdentry, tlen);
  1470. host_err = PTR_ERR(ndentry);
  1471. if (IS_ERR(ndentry))
  1472. goto out_dput_old;
  1473. host_err = -ENOTEMPTY;
  1474. if (ndentry == trap)
  1475. goto out_dput_new;
  1476. host_err = -EXDEV;
  1477. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1478. goto out_dput_new;
  1479. if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
  1480. goto out_dput_new;
  1481. host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
  1482. if (!host_err) {
  1483. host_err = commit_metadata(tfhp);
  1484. if (!host_err)
  1485. host_err = commit_metadata(ffhp);
  1486. }
  1487. out_dput_new:
  1488. dput(ndentry);
  1489. out_dput_old:
  1490. dput(odentry);
  1491. out_nfserr:
  1492. err = nfserrno(host_err);
  1493. /*
  1494. * We cannot rely on fh_unlock on the two filehandles,
  1495. * as that would do the wrong thing if the two directories
  1496. * were the same, so again we do it by hand.
  1497. */
  1498. fill_post_wcc(ffhp);
  1499. fill_post_wcc(tfhp);
  1500. unlock_rename(tdentry, fdentry);
  1501. ffhp->fh_locked = tfhp->fh_locked = false;
  1502. fh_drop_write(ffhp);
  1503. out:
  1504. return err;
  1505. }
  1506. /*
  1507. * Unlink a file or directory
  1508. * N.B. After this call fhp needs an fh_put
  1509. */
  1510. __be32
  1511. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1512. char *fname, int flen)
  1513. {
  1514. struct dentry *dentry, *rdentry;
  1515. struct inode *dirp;
  1516. __be32 err;
  1517. int host_err;
  1518. err = nfserr_acces;
  1519. if (!flen || isdotent(fname, flen))
  1520. goto out;
  1521. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1522. if (err)
  1523. goto out;
  1524. host_err = fh_want_write(fhp);
  1525. if (host_err)
  1526. goto out_nfserr;
  1527. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1528. dentry = fhp->fh_dentry;
  1529. dirp = d_inode(dentry);
  1530. rdentry = lookup_one_len(fname, dentry, flen);
  1531. host_err = PTR_ERR(rdentry);
  1532. if (IS_ERR(rdentry))
  1533. goto out_nfserr;
  1534. if (d_really_is_negative(rdentry)) {
  1535. dput(rdentry);
  1536. err = nfserr_noent;
  1537. goto out;
  1538. }
  1539. if (!type)
  1540. type = d_inode(rdentry)->i_mode & S_IFMT;
  1541. if (type != S_IFDIR)
  1542. host_err = vfs_unlink(dirp, rdentry, NULL);
  1543. else
  1544. host_err = vfs_rmdir(dirp, rdentry);
  1545. if (!host_err)
  1546. host_err = commit_metadata(fhp);
  1547. dput(rdentry);
  1548. out_nfserr:
  1549. err = nfserrno(host_err);
  1550. out:
  1551. return err;
  1552. }
  1553. /*
  1554. * We do this buffering because we must not call back into the file
  1555. * system's ->lookup() method from the filldir callback. That may well
  1556. * deadlock a number of file systems.
  1557. *
  1558. * This is based heavily on the implementation of same in XFS.
  1559. */
  1560. struct buffered_dirent {
  1561. u64 ino;
  1562. loff_t offset;
  1563. int namlen;
  1564. unsigned int d_type;
  1565. char name[];
  1566. };
  1567. struct readdir_data {
  1568. struct dir_context ctx;
  1569. char *dirent;
  1570. size_t used;
  1571. int full;
  1572. };
  1573. static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
  1574. int namlen, loff_t offset, u64 ino,
  1575. unsigned int d_type)
  1576. {
  1577. struct readdir_data *buf =
  1578. container_of(ctx, struct readdir_data, ctx);
  1579. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1580. unsigned int reclen;
  1581. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1582. if (buf->used + reclen > PAGE_SIZE) {
  1583. buf->full = 1;
  1584. return -EINVAL;
  1585. }
  1586. de->namlen = namlen;
  1587. de->offset = offset;
  1588. de->ino = ino;
  1589. de->d_type = d_type;
  1590. memcpy(de->name, name, namlen);
  1591. buf->used += reclen;
  1592. return 0;
  1593. }
  1594. static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
  1595. struct readdir_cd *cdp, loff_t *offsetp)
  1596. {
  1597. struct buffered_dirent *de;
  1598. int host_err;
  1599. int size;
  1600. loff_t offset;
  1601. struct readdir_data buf = {
  1602. .ctx.actor = nfsd_buffered_filldir,
  1603. .dirent = (void *)__get_free_page(GFP_KERNEL)
  1604. };
  1605. if (!buf.dirent)
  1606. return nfserrno(-ENOMEM);
  1607. offset = *offsetp;
  1608. while (1) {
  1609. unsigned int reclen;
  1610. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1611. buf.used = 0;
  1612. buf.full = 0;
  1613. host_err = iterate_dir(file, &buf.ctx);
  1614. if (buf.full)
  1615. host_err = 0;
  1616. if (host_err < 0)
  1617. break;
  1618. size = buf.used;
  1619. if (!size)
  1620. break;
  1621. de = (struct buffered_dirent *)buf.dirent;
  1622. while (size > 0) {
  1623. offset = de->offset;
  1624. if (func(cdp, de->name, de->namlen, de->offset,
  1625. de->ino, de->d_type))
  1626. break;
  1627. if (cdp->err != nfs_ok)
  1628. break;
  1629. reclen = ALIGN(sizeof(*de) + de->namlen,
  1630. sizeof(u64));
  1631. size -= reclen;
  1632. de = (struct buffered_dirent *)((char *)de + reclen);
  1633. }
  1634. if (size > 0) /* We bailed out early */
  1635. break;
  1636. offset = vfs_llseek(file, 0, SEEK_CUR);
  1637. }
  1638. free_page((unsigned long)(buf.dirent));
  1639. if (host_err)
  1640. return nfserrno(host_err);
  1641. *offsetp = offset;
  1642. return cdp->err;
  1643. }
  1644. /*
  1645. * Read entries from a directory.
  1646. * The NFSv3/4 verifier we ignore for now.
  1647. */
  1648. __be32
  1649. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1650. struct readdir_cd *cdp, nfsd_filldir_t func)
  1651. {
  1652. __be32 err;
  1653. struct file *file;
  1654. loff_t offset = *offsetp;
  1655. int may_flags = NFSD_MAY_READ;
  1656. /* NFSv2 only supports 32 bit cookies */
  1657. if (rqstp->rq_vers > 2)
  1658. may_flags |= NFSD_MAY_64BIT_COOKIE;
  1659. err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
  1660. if (err)
  1661. goto out;
  1662. offset = vfs_llseek(file, offset, SEEK_SET);
  1663. if (offset < 0) {
  1664. err = nfserrno((int)offset);
  1665. goto out_close;
  1666. }
  1667. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1668. if (err == nfserr_eof || err == nfserr_toosmall)
  1669. err = nfs_ok; /* can still be found in ->err */
  1670. out_close:
  1671. fput(file);
  1672. out:
  1673. return err;
  1674. }
  1675. /*
  1676. * Get file system stats
  1677. * N.B. After this call fhp needs an fh_put
  1678. */
  1679. __be32
  1680. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1681. {
  1682. __be32 err;
  1683. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1684. if (!err) {
  1685. struct path path = {
  1686. .mnt = fhp->fh_export->ex_path.mnt,
  1687. .dentry = fhp->fh_dentry,
  1688. };
  1689. if (vfs_statfs(&path, stat))
  1690. err = nfserr_io;
  1691. }
  1692. return err;
  1693. }
  1694. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1695. {
  1696. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1697. }
  1698. /*
  1699. * Check for a user's access permissions to this inode.
  1700. */
  1701. __be32
  1702. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1703. struct dentry *dentry, int acc)
  1704. {
  1705. struct inode *inode = d_inode(dentry);
  1706. int err;
  1707. if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
  1708. return 0;
  1709. #if 0
  1710. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1711. acc,
  1712. (acc & NFSD_MAY_READ)? " read" : "",
  1713. (acc & NFSD_MAY_WRITE)? " write" : "",
  1714. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1715. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1716. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1717. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1718. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1719. inode->i_mode,
  1720. IS_IMMUTABLE(inode)? " immut" : "",
  1721. IS_APPEND(inode)? " append" : "",
  1722. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1723. dprintk(" owner %d/%d user %d/%d\n",
  1724. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1725. #endif
  1726. /* Normally we reject any write/sattr etc access on a read-only file
  1727. * system. But if it is IRIX doing check on write-access for a
  1728. * device special file, we ignore rofs.
  1729. */
  1730. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1731. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1732. if (exp_rdonly(rqstp, exp) ||
  1733. __mnt_is_readonly(exp->ex_path.mnt))
  1734. return nfserr_rofs;
  1735. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1736. return nfserr_perm;
  1737. }
  1738. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1739. return nfserr_perm;
  1740. if (acc & NFSD_MAY_LOCK) {
  1741. /* If we cannot rely on authentication in NLM requests,
  1742. * just allow locks, otherwise require read permission, or
  1743. * ownership
  1744. */
  1745. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1746. return 0;
  1747. else
  1748. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1749. }
  1750. /*
  1751. * The file owner always gets access permission for accesses that
  1752. * would normally be checked at open time. This is to make
  1753. * file access work even when the client has done a fchmod(fd, 0).
  1754. *
  1755. * However, `cp foo bar' should fail nevertheless when bar is
  1756. * readonly. A sensible way to do this might be to reject all
  1757. * attempts to truncate a read-only file, because a creat() call
  1758. * always implies file truncation.
  1759. * ... but this isn't really fair. A process may reasonably call
  1760. * ftruncate on an open file descriptor on a file with perm 000.
  1761. * We must trust the client to do permission checking - using "ACCESS"
  1762. * with NFSv3.
  1763. */
  1764. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1765. uid_eq(inode->i_uid, current_fsuid()))
  1766. return 0;
  1767. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1768. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1769. /* Allow read access to binaries even when mode 111 */
  1770. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1771. (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
  1772. acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
  1773. err = inode_permission(inode, MAY_EXEC);
  1774. return err? nfserrno(err) : 0;
  1775. }
  1776. void
  1777. nfsd_racache_shutdown(void)
  1778. {
  1779. struct raparms *raparm, *last_raparm;
  1780. unsigned int i;
  1781. dprintk("nfsd: freeing readahead buffers.\n");
  1782. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1783. raparm = raparm_hash[i].pb_head;
  1784. while(raparm) {
  1785. last_raparm = raparm;
  1786. raparm = raparm->p_next;
  1787. kfree(last_raparm);
  1788. }
  1789. raparm_hash[i].pb_head = NULL;
  1790. }
  1791. }
  1792. /*
  1793. * Initialize readahead param cache
  1794. */
  1795. int
  1796. nfsd_racache_init(int cache_size)
  1797. {
  1798. int i;
  1799. int j = 0;
  1800. int nperbucket;
  1801. struct raparms **raparm = NULL;
  1802. if (raparm_hash[0].pb_head)
  1803. return 0;
  1804. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1805. nperbucket = max(2, nperbucket);
  1806. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1807. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1808. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1809. spin_lock_init(&raparm_hash[i].pb_lock);
  1810. raparm = &raparm_hash[i].pb_head;
  1811. for (j = 0; j < nperbucket; j++) {
  1812. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1813. if (!*raparm)
  1814. goto out_nomem;
  1815. raparm = &(*raparm)->p_next;
  1816. }
  1817. *raparm = NULL;
  1818. }
  1819. nfsdstats.ra_size = cache_size;
  1820. return 0;
  1821. out_nomem:
  1822. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1823. nfsd_racache_shutdown();
  1824. return -ENOMEM;
  1825. }