vfs.c 50 KB

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