vfs.c 50 KB

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