vfs.c 49 KB

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