vfs.c 50 KB

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