vfs.c 50 KB

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