vfs.c 50 KB

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