vfs.c 49 KB

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