vfs.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * File operations used by nfsd. Some of these have been ripped from
  4. * other parts of the kernel because they weren't exported, others
  5. * are partial duplicates with added or changed functionality.
  6. *
  7. * Note that several functions dget() the dentry upon which they want
  8. * to act, most notably those that create directory entries. Response
  9. * dentry's are dput()'d if necessary in the release callback.
  10. * So if you notice code paths that apparently fail to dput() the
  11. * dentry, don't worry--they have been taken care of.
  12. *
  13. * Copyright (C) 1995-1999 Olaf Kirch <[email protected]>
  14. * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <[email protected]>
  15. */
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/splice.h>
  19. #include <linux/falloc.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/namei.h>
  22. #include <linux/delay.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/xattr.h>
  26. #include <linux/jhash.h>
  27. #include <linux/ima.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/slab.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/exportfs.h>
  32. #include <linux/writeback.h>
  33. #include <linux/security.h>
  34. #include "xdr3.h"
  35. #ifdef CONFIG_NFSD_V4
  36. #include "../internal.h"
  37. #include "acl.h"
  38. #include "idmap.h"
  39. #include "xdr4.h"
  40. #endif /* CONFIG_NFSD_V4 */
  41. #include "nfsd.h"
  42. #include "vfs.h"
  43. #include "filecache.h"
  44. #include "trace.h"
  45. #define NFSDDBG_FACILITY NFSDDBG_FILEOP
  46. /*
  47. * Called from nfsd_lookup and encode_dirent. Check if we have crossed
  48. * a mount point.
  49. * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
  50. * or nfs_ok having possibly changed *dpp and *expp
  51. */
  52. int
  53. nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
  54. struct svc_export **expp)
  55. {
  56. struct svc_export *exp = *expp, *exp2 = NULL;
  57. struct dentry *dentry = *dpp;
  58. struct path path = {.mnt = mntget(exp->ex_path.mnt),
  59. .dentry = dget(dentry)};
  60. int err = 0;
  61. err = follow_down(&path);
  62. if (err < 0)
  63. goto out;
  64. if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
  65. nfsd_mountpoint(dentry, exp) == 2) {
  66. /* This is only a mountpoint in some other namespace */
  67. path_put(&path);
  68. goto out;
  69. }
  70. exp2 = rqst_exp_get_by_name(rqstp, &path);
  71. if (IS_ERR(exp2)) {
  72. err = PTR_ERR(exp2);
  73. /*
  74. * We normally allow NFS clients to continue
  75. * "underneath" a mountpoint that is not exported.
  76. * The exception is V4ROOT, where no traversal is ever
  77. * allowed without an explicit export of the new
  78. * directory.
  79. */
  80. if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
  81. err = 0;
  82. path_put(&path);
  83. goto out;
  84. }
  85. if (nfsd_v4client(rqstp) ||
  86. (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
  87. /* successfully crossed mount point */
  88. /*
  89. * This is subtle: path.dentry is *not* on path.mnt
  90. * at this point. The only reason we are safe is that
  91. * original mnt is pinned down by exp, so we should
  92. * put path *before* putting exp
  93. */
  94. *dpp = path.dentry;
  95. path.dentry = dentry;
  96. *expp = exp2;
  97. exp2 = exp;
  98. }
  99. path_put(&path);
  100. exp_put(exp2);
  101. out:
  102. return err;
  103. }
  104. static void follow_to_parent(struct path *path)
  105. {
  106. struct dentry *dp;
  107. while (path->dentry == path->mnt->mnt_root && follow_up(path))
  108. ;
  109. dp = dget_parent(path->dentry);
  110. dput(path->dentry);
  111. path->dentry = dp;
  112. }
  113. static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
  114. {
  115. struct svc_export *exp2;
  116. struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
  117. .dentry = dget(dparent)};
  118. follow_to_parent(&path);
  119. exp2 = rqst_exp_parent(rqstp, &path);
  120. if (PTR_ERR(exp2) == -ENOENT) {
  121. *dentryp = dget(dparent);
  122. } else if (IS_ERR(exp2)) {
  123. path_put(&path);
  124. return PTR_ERR(exp2);
  125. } else {
  126. *dentryp = dget(path.dentry);
  127. exp_put(*exp);
  128. *exp = exp2;
  129. }
  130. path_put(&path);
  131. return 0;
  132. }
  133. /*
  134. * For nfsd purposes, we treat V4ROOT exports as though there was an
  135. * export at *every* directory.
  136. * We return:
  137. * '1' if this dentry *must* be an export point,
  138. * '2' if it might be, if there is really a mount here, and
  139. * '0' if there is no chance of an export point here.
  140. */
  141. int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
  142. {
  143. if (!d_inode(dentry))
  144. return 0;
  145. if (exp->ex_flags & NFSEXP_V4ROOT)
  146. return 1;
  147. if (nfsd4_is_junction(dentry))
  148. return 1;
  149. if (d_mountpoint(dentry))
  150. /*
  151. * Might only be a mountpoint in a different namespace,
  152. * but we need to check.
  153. */
  154. return 2;
  155. return 0;
  156. }
  157. __be32
  158. nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  159. const char *name, unsigned int len,
  160. struct svc_export **exp_ret, struct dentry **dentry_ret)
  161. {
  162. struct svc_export *exp;
  163. struct dentry *dparent;
  164. struct dentry *dentry;
  165. int host_err;
  166. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  167. dparent = fhp->fh_dentry;
  168. exp = exp_get(fhp->fh_export);
  169. /* Lookup the name, but don't follow links */
  170. if (isdotent(name, len)) {
  171. if (len==1)
  172. dentry = dget(dparent);
  173. else if (dparent != exp->ex_path.dentry)
  174. dentry = dget_parent(dparent);
  175. else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
  176. dentry = dget(dparent); /* .. == . just like at / */
  177. else {
  178. /* checking mountpoint crossing is very different when stepping up */
  179. host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
  180. if (host_err)
  181. goto out_nfserr;
  182. }
  183. } else {
  184. dentry = lookup_one_len_unlocked(name, dparent, len);
  185. host_err = PTR_ERR(dentry);
  186. if (IS_ERR(dentry))
  187. goto out_nfserr;
  188. if (nfsd_mountpoint(dentry, exp)) {
  189. host_err = nfsd_cross_mnt(rqstp, &dentry, &exp);
  190. if (host_err) {
  191. dput(dentry);
  192. goto out_nfserr;
  193. }
  194. }
  195. }
  196. *dentry_ret = dentry;
  197. *exp_ret = exp;
  198. return 0;
  199. out_nfserr:
  200. exp_put(exp);
  201. return nfserrno(host_err);
  202. }
  203. /**
  204. * nfsd_lookup - look up a single path component for nfsd
  205. *
  206. * @rqstp: the request context
  207. * @fhp: the file handle of the directory
  208. * @name: the component name, or %NULL to look up parent
  209. * @len: length of name to examine
  210. * @resfh: pointer to pre-initialised filehandle to hold result.
  211. *
  212. * Look up one component of a pathname.
  213. * N.B. After this call _both_ fhp and resfh need an fh_put
  214. *
  215. * If the lookup would cross a mountpoint, and the mounted filesystem
  216. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  217. * accepted as it stands and the mounted directory is
  218. * returned. Otherwise the covered directory is returned.
  219. * NOTE: this mountpoint crossing is not supported properly by all
  220. * clients and is explicitly disallowed for NFSv3
  221. *
  222. */
  223. __be32
  224. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  225. unsigned int len, struct svc_fh *resfh)
  226. {
  227. struct svc_export *exp;
  228. struct dentry *dentry;
  229. __be32 err;
  230. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  231. if (err)
  232. return err;
  233. err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
  234. if (err)
  235. return err;
  236. err = check_nfsd_access(exp, rqstp);
  237. if (err)
  238. goto out;
  239. /*
  240. * Note: we compose the file handle now, but as the
  241. * dentry may be negative, it may need to be updated.
  242. */
  243. err = fh_compose(resfh, exp, dentry, fhp);
  244. if (!err && d_really_is_negative(dentry))
  245. err = nfserr_noent;
  246. out:
  247. dput(dentry);
  248. exp_put(exp);
  249. return err;
  250. }
  251. /*
  252. * Commit metadata changes to stable storage.
  253. */
  254. static int
  255. commit_inode_metadata(struct inode *inode)
  256. {
  257. const struct export_operations *export_ops = inode->i_sb->s_export_op;
  258. if (export_ops->commit_metadata)
  259. return export_ops->commit_metadata(inode);
  260. return sync_inode_metadata(inode, 1);
  261. }
  262. static int
  263. commit_metadata(struct svc_fh *fhp)
  264. {
  265. struct inode *inode = d_inode(fhp->fh_dentry);
  266. if (!EX_ISSYNC(fhp->fh_export))
  267. return 0;
  268. return commit_inode_metadata(inode);
  269. }
  270. /*
  271. * Go over the attributes and take care of the small differences between
  272. * NFS semantics and what Linux expects.
  273. */
  274. static void
  275. nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
  276. {
  277. /* Ignore mode updates on symlinks */
  278. if (S_ISLNK(inode->i_mode))
  279. iap->ia_valid &= ~ATTR_MODE;
  280. /* sanitize the mode change */
  281. if (iap->ia_valid & ATTR_MODE) {
  282. iap->ia_mode &= S_IALLUGO;
  283. iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
  284. }
  285. /* Revoke setuid/setgid on chown */
  286. if (!S_ISDIR(inode->i_mode) &&
  287. ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
  288. iap->ia_valid |= ATTR_KILL_PRIV;
  289. if (iap->ia_valid & ATTR_MODE) {
  290. /* we're setting mode too, just clear the s*id bits */
  291. iap->ia_mode &= ~S_ISUID;
  292. if (iap->ia_mode & S_IXGRP)
  293. iap->ia_mode &= ~S_ISGID;
  294. } else {
  295. /* set ATTR_KILL_* bits and let VFS handle it */
  296. iap->ia_valid |= ATTR_KILL_SUID;
  297. iap->ia_valid |=
  298. setattr_should_drop_sgid(&init_user_ns, inode);
  299. }
  300. }
  301. }
  302. static __be32
  303. nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
  304. struct iattr *iap)
  305. {
  306. struct inode *inode = d_inode(fhp->fh_dentry);
  307. if (iap->ia_size < inode->i_size) {
  308. __be32 err;
  309. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  310. NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
  311. if (err)
  312. return err;
  313. }
  314. return nfserrno(get_write_access(inode));
  315. }
  316. static int __nfsd_setattr(struct dentry *dentry, struct iattr *iap)
  317. {
  318. int host_err;
  319. if (iap->ia_valid & ATTR_SIZE) {
  320. /*
  321. * RFC5661, Section 18.30.4:
  322. * Changing the size of a file with SETATTR indirectly
  323. * changes the time_modify and change attributes.
  324. *
  325. * (and similar for the older RFCs)
  326. */
  327. struct iattr size_attr = {
  328. .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
  329. .ia_size = iap->ia_size,
  330. };
  331. if (iap->ia_size < 0)
  332. return -EFBIG;
  333. host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL);
  334. if (host_err)
  335. return host_err;
  336. iap->ia_valid &= ~ATTR_SIZE;
  337. /*
  338. * Avoid the additional setattr call below if the only other
  339. * attribute that the client sends is the mtime, as we update
  340. * it as part of the size change above.
  341. */
  342. if ((iap->ia_valid & ~ATTR_MTIME) == 0)
  343. return 0;
  344. }
  345. if (!iap->ia_valid)
  346. return 0;
  347. iap->ia_valid |= ATTR_CTIME;
  348. return notify_change(&init_user_ns, dentry, iap, NULL);
  349. }
  350. /**
  351. * nfsd_setattr - Set various file attributes.
  352. * @rqstp: controlling RPC transaction
  353. * @fhp: filehandle of target
  354. * @attr: attributes to set
  355. * @check_guard: set to 1 if guardtime is a valid timestamp
  356. * @guardtime: do not act if ctime.tv_sec does not match this timestamp
  357. *
  358. * This call may adjust the contents of @attr (in particular, this
  359. * call may change the bits in the na_iattr.ia_valid field).
  360. *
  361. * Returns nfs_ok on success, otherwise an NFS status code is
  362. * returned. Caller must release @fhp by calling fh_put in either
  363. * case.
  364. */
  365. __be32
  366. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
  367. struct nfsd_attrs *attr,
  368. int check_guard, time64_t guardtime)
  369. {
  370. struct dentry *dentry;
  371. struct inode *inode;
  372. struct iattr *iap = attr->na_iattr;
  373. int accmode = NFSD_MAY_SATTR;
  374. umode_t ftype = 0;
  375. __be32 err;
  376. int host_err;
  377. bool get_write_count;
  378. bool size_change = (iap->ia_valid & ATTR_SIZE);
  379. int retries;
  380. if (iap->ia_valid & ATTR_SIZE) {
  381. accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
  382. ftype = S_IFREG;
  383. }
  384. /*
  385. * If utimes(2) and friends are called with times not NULL, we should
  386. * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission
  387. * will return EACCES, when the caller's effective UID does not match
  388. * the owner of the file, and the caller is not privileged. In this
  389. * situation, we should return EPERM(notify_change will return this).
  390. */
  391. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) {
  392. accmode |= NFSD_MAY_OWNER_OVERRIDE;
  393. if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET)))
  394. accmode |= NFSD_MAY_WRITE;
  395. }
  396. /* Callers that do fh_verify should do the fh_want_write: */
  397. get_write_count = !fhp->fh_dentry;
  398. /* Get inode */
  399. err = fh_verify(rqstp, fhp, ftype, accmode);
  400. if (err)
  401. return err;
  402. if (get_write_count) {
  403. host_err = fh_want_write(fhp);
  404. if (host_err)
  405. goto out;
  406. }
  407. dentry = fhp->fh_dentry;
  408. inode = d_inode(dentry);
  409. nfsd_sanitize_attrs(inode, iap);
  410. if (check_guard && guardtime != inode->i_ctime.tv_sec)
  411. return nfserr_notsync;
  412. /*
  413. * The size case is special, it changes the file in addition to the
  414. * attributes, and file systems don't expect it to be mixed with
  415. * "random" attribute changes. We thus split out the size change
  416. * into a separate call to ->setattr, and do the rest as a separate
  417. * setattr call.
  418. */
  419. if (size_change) {
  420. err = nfsd_get_write_access(rqstp, fhp, iap);
  421. if (err)
  422. return err;
  423. }
  424. inode_lock(inode);
  425. for (retries = 1;;) {
  426. struct iattr attrs;
  427. /*
  428. * notify_change() can alter its iattr argument, making
  429. * @iap unsuitable for submission multiple times. Make a
  430. * copy for every loop iteration.
  431. */
  432. attrs = *iap;
  433. host_err = __nfsd_setattr(dentry, &attrs);
  434. if (host_err != -EAGAIN || !retries--)
  435. break;
  436. if (!nfsd_wait_for_delegreturn(rqstp, inode))
  437. break;
  438. }
  439. if (attr->na_seclabel && attr->na_seclabel->len)
  440. attr->na_labelerr = security_inode_setsecctx(dentry,
  441. attr->na_seclabel->data, attr->na_seclabel->len);
  442. if (IS_ENABLED(CONFIG_FS_POSIX_ACL) && attr->na_pacl)
  443. attr->na_aclerr = set_posix_acl(&init_user_ns,
  444. inode, ACL_TYPE_ACCESS,
  445. attr->na_pacl);
  446. if (IS_ENABLED(CONFIG_FS_POSIX_ACL) &&
  447. !attr->na_aclerr && attr->na_dpacl && S_ISDIR(inode->i_mode))
  448. attr->na_aclerr = set_posix_acl(&init_user_ns,
  449. inode, ACL_TYPE_DEFAULT,
  450. attr->na_dpacl);
  451. inode_unlock(inode);
  452. if (size_change)
  453. put_write_access(inode);
  454. out:
  455. if (!host_err)
  456. host_err = commit_metadata(fhp);
  457. return nfserrno(host_err);
  458. }
  459. #if defined(CONFIG_NFSD_V4)
  460. /*
  461. * NFS junction information is stored in an extended attribute.
  462. */
  463. #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
  464. /**
  465. * nfsd4_is_junction - Test if an object could be an NFS junction
  466. *
  467. * @dentry: object to test
  468. *
  469. * Returns 1 if "dentry" appears to contain NFS junction information.
  470. * Otherwise 0 is returned.
  471. */
  472. int nfsd4_is_junction(struct dentry *dentry)
  473. {
  474. struct inode *inode = d_inode(dentry);
  475. if (inode == NULL)
  476. return 0;
  477. if (inode->i_mode & S_IXUGO)
  478. return 0;
  479. if (!(inode->i_mode & S_ISVTX))
  480. return 0;
  481. if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME,
  482. NULL, 0) <= 0)
  483. return 0;
  484. return 1;
  485. }
  486. static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp)
  487. {
  488. return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate;
  489. }
  490. __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp,
  491. struct nfsd_file *nf_src, u64 src_pos,
  492. struct nfsd_file *nf_dst, u64 dst_pos,
  493. u64 count, bool sync)
  494. {
  495. struct file *src = nf_src->nf_file;
  496. struct file *dst = nf_dst->nf_file;
  497. errseq_t since;
  498. loff_t cloned;
  499. __be32 ret = 0;
  500. since = READ_ONCE(dst->f_wb_err);
  501. cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0);
  502. if (cloned < 0) {
  503. ret = nfserrno(cloned);
  504. goto out_err;
  505. }
  506. if (count && cloned != count) {
  507. ret = nfserrno(-EINVAL);
  508. goto out_err;
  509. }
  510. if (sync) {
  511. loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX;
  512. int status = vfs_fsync_range(dst, dst_pos, dst_end, 0);
  513. if (!status)
  514. status = filemap_check_wb_err(dst->f_mapping, since);
  515. if (!status)
  516. status = commit_inode_metadata(file_inode(src));
  517. if (status < 0) {
  518. struct nfsd_net *nn = net_generic(nf_dst->nf_net,
  519. nfsd_net_id);
  520. trace_nfsd_clone_file_range_err(rqstp,
  521. &nfsd4_get_cstate(rqstp)->save_fh,
  522. src_pos,
  523. &nfsd4_get_cstate(rqstp)->current_fh,
  524. dst_pos,
  525. count, status);
  526. nfsd_reset_write_verifier(nn);
  527. trace_nfsd_writeverf_reset(nn, rqstp, status);
  528. ret = nfserrno(status);
  529. }
  530. }
  531. out_err:
  532. return ret;
  533. }
  534. ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
  535. u64 dst_pos, u64 count)
  536. {
  537. ssize_t ret;
  538. /*
  539. * Limit copy to 4MB to prevent indefinitely blocking an nfsd
  540. * thread and client rpc slot. The choice of 4MB is somewhat
  541. * arbitrary. We might instead base this on r/wsize, or make it
  542. * tunable, or use a time instead of a byte limit, or implement
  543. * asynchronous copy. In theory a client could also recognize a
  544. * limit like this and pipeline multiple COPY requests.
  545. */
  546. count = min_t(u64, count, 1 << 22);
  547. ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
  548. if (ret == -EOPNOTSUPP || ret == -EXDEV)
  549. ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count,
  550. COPY_FILE_SPLICE);
  551. return ret;
  552. }
  553. __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
  554. struct file *file, loff_t offset, loff_t len,
  555. int flags)
  556. {
  557. int error;
  558. if (!S_ISREG(file_inode(file)->i_mode))
  559. return nfserr_inval;
  560. error = vfs_fallocate(file, flags, offset, len);
  561. if (!error)
  562. error = commit_metadata(fhp);
  563. return nfserrno(error);
  564. }
  565. #endif /* defined(CONFIG_NFSD_V4) */
  566. /*
  567. * Check server access rights to a file system object
  568. */
  569. struct accessmap {
  570. u32 access;
  571. int how;
  572. };
  573. static struct accessmap nfs3_regaccess[] = {
  574. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  575. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  576. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
  577. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
  578. #ifdef CONFIG_NFSD_V4
  579. { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
  580. { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
  581. { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
  582. #endif
  583. { 0, 0 }
  584. };
  585. static struct accessmap nfs3_diraccess[] = {
  586. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  587. { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
  588. { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
  589. { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
  590. { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
  591. #ifdef CONFIG_NFSD_V4
  592. { NFS4_ACCESS_XAREAD, NFSD_MAY_READ },
  593. { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE },
  594. { NFS4_ACCESS_XALIST, NFSD_MAY_READ },
  595. #endif
  596. { 0, 0 }
  597. };
  598. static struct accessmap nfs3_anyaccess[] = {
  599. /* Some clients - Solaris 2.6 at least, make an access call
  600. * to the server to check for access for things like /dev/null
  601. * (which really, the server doesn't care about). So
  602. * We provide simple access checking for them, looking
  603. * mainly at mode bits, and we make sure to ignore read-only
  604. * filesystem checks
  605. */
  606. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  607. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  608. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  609. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  610. { 0, 0 }
  611. };
  612. __be32
  613. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  614. {
  615. struct accessmap *map;
  616. struct svc_export *export;
  617. struct dentry *dentry;
  618. u32 query, result = 0, sresult = 0;
  619. __be32 error;
  620. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
  621. if (error)
  622. goto out;
  623. export = fhp->fh_export;
  624. dentry = fhp->fh_dentry;
  625. if (d_is_reg(dentry))
  626. map = nfs3_regaccess;
  627. else if (d_is_dir(dentry))
  628. map = nfs3_diraccess;
  629. else
  630. map = nfs3_anyaccess;
  631. query = *access;
  632. for (; map->access; map++) {
  633. if (map->access & query) {
  634. __be32 err2;
  635. sresult |= map->access;
  636. err2 = nfsd_permission(rqstp, export, dentry, map->how);
  637. switch (err2) {
  638. case nfs_ok:
  639. result |= map->access;
  640. break;
  641. /* the following error codes just mean the access was not allowed,
  642. * rather than an error occurred */
  643. case nfserr_rofs:
  644. case nfserr_acces:
  645. case nfserr_perm:
  646. /* simply don't "or" in the access bit. */
  647. break;
  648. default:
  649. error = err2;
  650. goto out;
  651. }
  652. }
  653. }
  654. *access = result;
  655. if (supported)
  656. *supported = sresult;
  657. out:
  658. return error;
  659. }
  660. int nfsd_open_break_lease(struct inode *inode, int access)
  661. {
  662. unsigned int mode;
  663. if (access & NFSD_MAY_NOT_BREAK_LEASE)
  664. return 0;
  665. mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
  666. return break_lease(inode, mode | O_NONBLOCK);
  667. }
  668. /*
  669. * Open an existing file or directory.
  670. * The may_flags argument indicates the type of open (read/write/lock)
  671. * and additional flags.
  672. * N.B. After this call fhp needs an fh_put
  673. */
  674. static __be32
  675. __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
  676. int may_flags, struct file **filp)
  677. {
  678. struct path path;
  679. struct inode *inode;
  680. struct file *file;
  681. int flags = O_RDONLY|O_LARGEFILE;
  682. __be32 err;
  683. int host_err = 0;
  684. path.mnt = fhp->fh_export->ex_path.mnt;
  685. path.dentry = fhp->fh_dentry;
  686. inode = d_inode(path.dentry);
  687. err = nfserr_perm;
  688. if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
  689. goto out;
  690. if (!inode->i_fop)
  691. goto out;
  692. host_err = nfsd_open_break_lease(inode, may_flags);
  693. if (host_err) /* NOMEM or WOULDBLOCK */
  694. goto out_nfserr;
  695. if (may_flags & NFSD_MAY_WRITE) {
  696. if (may_flags & NFSD_MAY_READ)
  697. flags = O_RDWR|O_LARGEFILE;
  698. else
  699. flags = O_WRONLY|O_LARGEFILE;
  700. }
  701. file = dentry_open(&path, flags, current_cred());
  702. if (IS_ERR(file)) {
  703. host_err = PTR_ERR(file);
  704. goto out_nfserr;
  705. }
  706. host_err = ima_file_check(file, may_flags);
  707. if (host_err) {
  708. fput(file);
  709. goto out_nfserr;
  710. }
  711. if (may_flags & NFSD_MAY_64BIT_COOKIE)
  712. file->f_mode |= FMODE_64BITHASH;
  713. else
  714. file->f_mode |= FMODE_32BITHASH;
  715. *filp = file;
  716. out_nfserr:
  717. err = nfserrno(host_err);
  718. out:
  719. return err;
  720. }
  721. __be32
  722. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
  723. int may_flags, struct file **filp)
  724. {
  725. __be32 err;
  726. bool retried = false;
  727. validate_process_creds();
  728. /*
  729. * If we get here, then the client has already done an "open",
  730. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  731. * in case a chmod has now revoked permission.
  732. *
  733. * Arguably we should also allow the owner override for
  734. * directories, but we never have and it doesn't seem to have
  735. * caused anyone a problem. If we were to change this, note
  736. * also that our filldir callbacks would need a variant of
  737. * lookup_one_len that doesn't check permissions.
  738. */
  739. if (type == S_IFREG)
  740. may_flags |= NFSD_MAY_OWNER_OVERRIDE;
  741. retry:
  742. err = fh_verify(rqstp, fhp, type, may_flags);
  743. if (!err) {
  744. err = __nfsd_open(rqstp, fhp, type, may_flags, filp);
  745. if (err == nfserr_stale && !retried) {
  746. retried = true;
  747. fh_put(fhp);
  748. goto retry;
  749. }
  750. }
  751. validate_process_creds();
  752. return err;
  753. }
  754. /**
  755. * nfsd_open_verified - Open a regular file for the filecache
  756. * @rqstp: RPC request
  757. * @fhp: NFS filehandle of the file to open
  758. * @may_flags: internal permission flags
  759. * @filp: OUT: open "struct file *"
  760. *
  761. * Returns an nfsstat value in network byte order.
  762. */
  763. __be32
  764. nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, int may_flags,
  765. struct file **filp)
  766. {
  767. __be32 err;
  768. validate_process_creds();
  769. err = __nfsd_open(rqstp, fhp, S_IFREG, may_flags, filp);
  770. validate_process_creds();
  771. return err;
  772. }
  773. /*
  774. * Grab and keep cached pages associated with a file in the svc_rqst
  775. * so that they can be passed to the network sendmsg/sendpage routines
  776. * directly. They will be released after the sending has completed.
  777. */
  778. static int
  779. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  780. struct splice_desc *sd)
  781. {
  782. struct svc_rqst *rqstp = sd->u.data;
  783. struct page *page = buf->page; // may be a compound one
  784. unsigned offset = buf->offset;
  785. struct page *last_page;
  786. last_page = page + (offset + sd->len - 1) / PAGE_SIZE;
  787. for (page += offset / PAGE_SIZE; page <= last_page; page++) {
  788. /*
  789. * Skip page replacement when extending the contents
  790. * of the current page.
  791. */
  792. if (page == *(rqstp->rq_next_page - 1))
  793. continue;
  794. svc_rqst_replace_page(rqstp, page);
  795. }
  796. if (rqstp->rq_res.page_len == 0) // first call
  797. rqstp->rq_res.page_base = offset % PAGE_SIZE;
  798. rqstp->rq_res.page_len += sd->len;
  799. return sd->len;
  800. }
  801. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  802. struct splice_desc *sd)
  803. {
  804. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  805. }
  806. static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len,
  807. size_t expected)
  808. {
  809. if (expected != 0 && len == 0)
  810. return 1;
  811. if (offset+len >= i_size_read(file_inode(file)))
  812. return 1;
  813. return 0;
  814. }
  815. static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  816. struct file *file, loff_t offset,
  817. unsigned long *count, u32 *eof, ssize_t host_err)
  818. {
  819. if (host_err >= 0) {
  820. nfsd_stats_io_read_add(fhp->fh_export, host_err);
  821. *eof = nfsd_eof_on_read(file, offset, host_err, *count);
  822. *count = host_err;
  823. fsnotify_access(file);
  824. trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
  825. return 0;
  826. } else {
  827. trace_nfsd_read_err(rqstp, fhp, offset, host_err);
  828. return nfserrno(host_err);
  829. }
  830. }
  831. __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  832. struct file *file, loff_t offset, unsigned long *count,
  833. u32 *eof)
  834. {
  835. struct splice_desc sd = {
  836. .len = 0,
  837. .total_len = *count,
  838. .pos = offset,
  839. .u.data = rqstp,
  840. };
  841. ssize_t host_err;
  842. trace_nfsd_read_splice(rqstp, fhp, offset, *count);
  843. rqstp->rq_next_page = rqstp->rq_respages + 1;
  844. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  845. return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
  846. }
  847. __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
  848. struct file *file, loff_t offset,
  849. struct kvec *vec, int vlen, unsigned long *count,
  850. u32 *eof)
  851. {
  852. struct iov_iter iter;
  853. loff_t ppos = offset;
  854. ssize_t host_err;
  855. trace_nfsd_read_vector(rqstp, fhp, offset, *count);
  856. iov_iter_kvec(&iter, ITER_DEST, vec, vlen, *count);
  857. host_err = vfs_iter_read(file, &iter, &ppos, 0);
  858. return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err);
  859. }
  860. /*
  861. * Gathered writes: If another process is currently writing to the file,
  862. * there's a high chance this is another nfsd (triggered by a bulk write
  863. * from a client's biod). Rather than syncing the file with each write
  864. * request, we sleep for 10 msec.
  865. *
  866. * I don't know if this roughly approximates C. Juszak's idea of
  867. * gathered writes, but it's a nice and simple solution (IMHO), and it
  868. * seems to work:-)
  869. *
  870. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  871. * better tool (separate unstable writes and commits) for solving this
  872. * problem.
  873. */
  874. static int wait_for_concurrent_writes(struct file *file)
  875. {
  876. struct inode *inode = file_inode(file);
  877. static ino_t last_ino;
  878. static dev_t last_dev;
  879. int err = 0;
  880. if (atomic_read(&inode->i_writecount) > 1
  881. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  882. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  883. msleep(10);
  884. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  885. }
  886. if (inode->i_state & I_DIRTY) {
  887. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  888. err = vfs_fsync(file, 0);
  889. }
  890. last_ino = inode->i_ino;
  891. last_dev = inode->i_sb->s_dev;
  892. return err;
  893. }
  894. __be32
  895. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
  896. loff_t offset, struct kvec *vec, int vlen,
  897. unsigned long *cnt, int stable,
  898. __be32 *verf)
  899. {
  900. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  901. struct file *file = nf->nf_file;
  902. struct super_block *sb = file_inode(file)->i_sb;
  903. struct svc_export *exp;
  904. struct iov_iter iter;
  905. errseq_t since;
  906. __be32 nfserr;
  907. int host_err;
  908. int use_wgather;
  909. loff_t pos = offset;
  910. unsigned long exp_op_flags = 0;
  911. unsigned int pflags = current->flags;
  912. rwf_t flags = 0;
  913. bool restore_flags = false;
  914. trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
  915. if (sb->s_export_op)
  916. exp_op_flags = sb->s_export_op->flags;
  917. if (test_bit(RQ_LOCAL, &rqstp->rq_flags) &&
  918. !(exp_op_flags & EXPORT_OP_REMOTE_FS)) {
  919. /*
  920. * We want throttling in balance_dirty_pages()
  921. * and shrink_inactive_list() to only consider
  922. * the backingdev we are writing to, so that nfs to
  923. * localhost doesn't cause nfsd to lock up due to all
  924. * the client's dirty pages or its congested queue.
  925. */
  926. current->flags |= PF_LOCAL_THROTTLE;
  927. restore_flags = true;
  928. }
  929. exp = fhp->fh_export;
  930. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  931. if (!EX_ISSYNC(exp))
  932. stable = NFS_UNSTABLE;
  933. if (stable && !use_wgather)
  934. flags |= RWF_SYNC;
  935. iov_iter_kvec(&iter, ITER_SOURCE, vec, vlen, *cnt);
  936. since = READ_ONCE(file->f_wb_err);
  937. if (verf)
  938. nfsd_copy_write_verifier(verf, nn);
  939. file_start_write(file);
  940. host_err = vfs_iter_write(file, &iter, &pos, flags);
  941. file_end_write(file);
  942. if (host_err < 0) {
  943. nfsd_reset_write_verifier(nn);
  944. trace_nfsd_writeverf_reset(nn, rqstp, host_err);
  945. goto out_nfserr;
  946. }
  947. *cnt = host_err;
  948. nfsd_stats_io_write_add(exp, *cnt);
  949. fsnotify_modify(file);
  950. host_err = filemap_check_wb_err(file->f_mapping, since);
  951. if (host_err < 0)
  952. goto out_nfserr;
  953. if (stable && use_wgather) {
  954. host_err = wait_for_concurrent_writes(file);
  955. if (host_err < 0) {
  956. nfsd_reset_write_verifier(nn);
  957. trace_nfsd_writeverf_reset(nn, rqstp, host_err);
  958. }
  959. }
  960. out_nfserr:
  961. if (host_err >= 0) {
  962. trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
  963. nfserr = nfs_ok;
  964. } else {
  965. trace_nfsd_write_err(rqstp, fhp, offset, host_err);
  966. nfserr = nfserrno(host_err);
  967. }
  968. if (restore_flags)
  969. current_restore_flags(pflags, PF_LOCAL_THROTTLE);
  970. return nfserr;
  971. }
  972. /*
  973. * Read data from a file. count must contain the requested read count
  974. * on entry. On return, *count contains the number of bytes actually read.
  975. * N.B. After this call fhp needs an fh_put
  976. */
  977. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  978. loff_t offset, struct kvec *vec, int vlen, unsigned long *count,
  979. u32 *eof)
  980. {
  981. struct nfsd_file *nf;
  982. struct file *file;
  983. __be32 err;
  984. trace_nfsd_read_start(rqstp, fhp, offset, *count);
  985. err = nfsd_file_acquire_gc(rqstp, fhp, NFSD_MAY_READ, &nf);
  986. if (err)
  987. return err;
  988. file = nf->nf_file;
  989. if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
  990. err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof);
  991. else
  992. err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof);
  993. nfsd_file_put(nf);
  994. trace_nfsd_read_done(rqstp, fhp, offset, *count);
  995. return err;
  996. }
  997. /*
  998. * Write data to a file.
  999. * The stable flag requests synchronous writes.
  1000. * N.B. After this call fhp needs an fh_put
  1001. */
  1002. __be32
  1003. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
  1004. struct kvec *vec, int vlen, unsigned long *cnt, int stable,
  1005. __be32 *verf)
  1006. {
  1007. struct nfsd_file *nf;
  1008. __be32 err;
  1009. trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
  1010. err = nfsd_file_acquire_gc(rqstp, fhp, NFSD_MAY_WRITE, &nf);
  1011. if (err)
  1012. goto out;
  1013. err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec,
  1014. vlen, cnt, stable, verf);
  1015. nfsd_file_put(nf);
  1016. out:
  1017. trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
  1018. return err;
  1019. }
  1020. /**
  1021. * nfsd_commit - Commit pending writes to stable storage
  1022. * @rqstp: RPC request being processed
  1023. * @fhp: NFS filehandle
  1024. * @nf: target file
  1025. * @offset: raw offset from beginning of file
  1026. * @count: raw count of bytes to sync
  1027. * @verf: filled in with the server's current write verifier
  1028. *
  1029. * Note: we guarantee that data that lies within the range specified
  1030. * by the 'offset' and 'count' parameters will be synced. The server
  1031. * is permitted to sync data that lies outside this range at the
  1032. * same time.
  1033. *
  1034. * Unfortunately we cannot lock the file to make sure we return full WCC
  1035. * data to the client, as locking happens lower down in the filesystem.
  1036. *
  1037. * Return values:
  1038. * An nfsstat value in network byte order.
  1039. */
  1040. __be32
  1041. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf,
  1042. u64 offset, u32 count, __be32 *verf)
  1043. {
  1044. __be32 err = nfs_ok;
  1045. u64 maxbytes;
  1046. loff_t start, end;
  1047. struct nfsd_net *nn;
  1048. /*
  1049. * Convert the client-provided (offset, count) range to a
  1050. * (start, end) range. If the client-provided range falls
  1051. * outside the maximum file size of the underlying FS,
  1052. * clamp the sync range appropriately.
  1053. */
  1054. start = 0;
  1055. end = LLONG_MAX;
  1056. maxbytes = (u64)fhp->fh_dentry->d_sb->s_maxbytes;
  1057. if (offset < maxbytes) {
  1058. start = offset;
  1059. if (count && (offset + count - 1 < maxbytes))
  1060. end = offset + count - 1;
  1061. }
  1062. nn = net_generic(nf->nf_net, nfsd_net_id);
  1063. if (EX_ISSYNC(fhp->fh_export)) {
  1064. errseq_t since = READ_ONCE(nf->nf_file->f_wb_err);
  1065. int err2;
  1066. err2 = vfs_fsync_range(nf->nf_file, start, end, 0);
  1067. switch (err2) {
  1068. case 0:
  1069. nfsd_copy_write_verifier(verf, nn);
  1070. err2 = filemap_check_wb_err(nf->nf_file->f_mapping,
  1071. since);
  1072. err = nfserrno(err2);
  1073. break;
  1074. case -EINVAL:
  1075. err = nfserr_notsupp;
  1076. break;
  1077. default:
  1078. nfsd_reset_write_verifier(nn);
  1079. trace_nfsd_writeverf_reset(nn, rqstp, err2);
  1080. err = nfserrno(err2);
  1081. }
  1082. } else
  1083. nfsd_copy_write_verifier(verf, nn);
  1084. return err;
  1085. }
  1086. /**
  1087. * nfsd_create_setattr - Set a created file's attributes
  1088. * @rqstp: RPC transaction being executed
  1089. * @fhp: NFS filehandle of parent directory
  1090. * @resfhp: NFS filehandle of new object
  1091. * @attrs: requested attributes of new object
  1092. *
  1093. * Returns nfs_ok on success, or an nfsstat in network byte order.
  1094. */
  1095. __be32
  1096. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1097. struct svc_fh *resfhp, struct nfsd_attrs *attrs)
  1098. {
  1099. struct iattr *iap = attrs->na_iattr;
  1100. __be32 status;
  1101. /*
  1102. * Mode has already been set by file creation.
  1103. */
  1104. iap->ia_valid &= ~ATTR_MODE;
  1105. /*
  1106. * Setting uid/gid works only for root. Irix appears to
  1107. * send along the gid on create when it tries to implement
  1108. * setgid directories via NFS:
  1109. */
  1110. if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  1111. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1112. /*
  1113. * Callers expect new file metadata to be committed even
  1114. * if the attributes have not changed.
  1115. */
  1116. if (iap->ia_valid)
  1117. status = nfsd_setattr(rqstp, resfhp, attrs, 0, (time64_t)0);
  1118. else
  1119. status = nfserrno(commit_metadata(resfhp));
  1120. /*
  1121. * Transactional filesystems had a chance to commit changes
  1122. * for both parent and child simultaneously making the
  1123. * following commit_metadata a noop in many cases.
  1124. */
  1125. if (!status)
  1126. status = nfserrno(commit_metadata(fhp));
  1127. /*
  1128. * Update the new filehandle to pick up the new attributes.
  1129. */
  1130. if (!status)
  1131. status = fh_update(resfhp);
  1132. return status;
  1133. }
  1134. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1135. * setting size to 0 may fail for some specific file systems by the permission
  1136. * checking which requires WRITE permission but the mode is 000.
  1137. * we ignore the resizing(to 0) on the just new created file, since the size is
  1138. * 0 after file created.
  1139. *
  1140. * call this only after vfs_create() is called.
  1141. * */
  1142. static void
  1143. nfsd_check_ignore_resizing(struct iattr *iap)
  1144. {
  1145. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1146. iap->ia_valid &= ~ATTR_SIZE;
  1147. }
  1148. /* The parent directory should already be locked: */
  1149. __be32
  1150. nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1151. struct nfsd_attrs *attrs,
  1152. int type, dev_t rdev, struct svc_fh *resfhp)
  1153. {
  1154. struct dentry *dentry, *dchild;
  1155. struct inode *dirp;
  1156. struct iattr *iap = attrs->na_iattr;
  1157. __be32 err;
  1158. int host_err;
  1159. dentry = fhp->fh_dentry;
  1160. dirp = d_inode(dentry);
  1161. dchild = dget(resfhp->fh_dentry);
  1162. err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
  1163. if (err)
  1164. goto out;
  1165. if (!(iap->ia_valid & ATTR_MODE))
  1166. iap->ia_mode = 0;
  1167. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1168. if (!IS_POSIXACL(dirp))
  1169. iap->ia_mode &= ~current_umask();
  1170. err = 0;
  1171. host_err = 0;
  1172. switch (type) {
  1173. case S_IFREG:
  1174. host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true);
  1175. if (!host_err)
  1176. nfsd_check_ignore_resizing(iap);
  1177. break;
  1178. case S_IFDIR:
  1179. host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode);
  1180. if (!host_err && unlikely(d_unhashed(dchild))) {
  1181. struct dentry *d;
  1182. d = lookup_one_len(dchild->d_name.name,
  1183. dchild->d_parent,
  1184. dchild->d_name.len);
  1185. if (IS_ERR(d)) {
  1186. host_err = PTR_ERR(d);
  1187. break;
  1188. }
  1189. if (unlikely(d_is_negative(d))) {
  1190. dput(d);
  1191. err = nfserr_serverfault;
  1192. goto out;
  1193. }
  1194. dput(resfhp->fh_dentry);
  1195. resfhp->fh_dentry = dget(d);
  1196. err = fh_update(resfhp);
  1197. dput(dchild);
  1198. dchild = d;
  1199. if (err)
  1200. goto out;
  1201. }
  1202. break;
  1203. case S_IFCHR:
  1204. case S_IFBLK:
  1205. case S_IFIFO:
  1206. case S_IFSOCK:
  1207. host_err = vfs_mknod(&init_user_ns, dirp, dchild,
  1208. iap->ia_mode, rdev);
  1209. break;
  1210. default:
  1211. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1212. type);
  1213. host_err = -EINVAL;
  1214. }
  1215. if (host_err < 0)
  1216. goto out_nfserr;
  1217. err = nfsd_create_setattr(rqstp, fhp, resfhp, attrs);
  1218. out:
  1219. dput(dchild);
  1220. return err;
  1221. out_nfserr:
  1222. err = nfserrno(host_err);
  1223. goto out;
  1224. }
  1225. /*
  1226. * Create a filesystem object (regular, directory, special).
  1227. * Note that the parent directory is left locked.
  1228. *
  1229. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1230. */
  1231. __be32
  1232. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1233. char *fname, int flen, struct nfsd_attrs *attrs,
  1234. int type, dev_t rdev, struct svc_fh *resfhp)
  1235. {
  1236. struct dentry *dentry, *dchild = NULL;
  1237. __be32 err;
  1238. int host_err;
  1239. if (isdotent(fname, flen))
  1240. return nfserr_exist;
  1241. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
  1242. if (err)
  1243. return err;
  1244. dentry = fhp->fh_dentry;
  1245. host_err = fh_want_write(fhp);
  1246. if (host_err)
  1247. return nfserrno(host_err);
  1248. inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT);
  1249. dchild = lookup_one_len(fname, dentry, flen);
  1250. host_err = PTR_ERR(dchild);
  1251. if (IS_ERR(dchild)) {
  1252. err = nfserrno(host_err);
  1253. goto out_unlock;
  1254. }
  1255. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1256. /*
  1257. * We unconditionally drop our ref to dchild as fh_compose will have
  1258. * already grabbed its own ref for it.
  1259. */
  1260. dput(dchild);
  1261. if (err)
  1262. goto out_unlock;
  1263. fh_fill_pre_attrs(fhp);
  1264. err = nfsd_create_locked(rqstp, fhp, attrs, type, rdev, resfhp);
  1265. fh_fill_post_attrs(fhp);
  1266. out_unlock:
  1267. inode_unlock(dentry->d_inode);
  1268. return err;
  1269. }
  1270. /*
  1271. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1272. * fits into the buffer. On return, it contains the true length.
  1273. * N.B. After this call fhp needs an fh_put
  1274. */
  1275. __be32
  1276. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1277. {
  1278. __be32 err;
  1279. const char *link;
  1280. struct path path;
  1281. DEFINE_DELAYED_CALL(done);
  1282. int len;
  1283. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1284. if (unlikely(err))
  1285. return err;
  1286. path.mnt = fhp->fh_export->ex_path.mnt;
  1287. path.dentry = fhp->fh_dentry;
  1288. if (unlikely(!d_is_symlink(path.dentry)))
  1289. return nfserr_inval;
  1290. touch_atime(&path);
  1291. link = vfs_get_link(path.dentry, &done);
  1292. if (IS_ERR(link))
  1293. return nfserrno(PTR_ERR(link));
  1294. len = strlen(link);
  1295. if (len < *lenp)
  1296. *lenp = len;
  1297. memcpy(buf, link, *lenp);
  1298. do_delayed_call(&done);
  1299. return 0;
  1300. }
  1301. /**
  1302. * nfsd_symlink - Create a symlink and look up its inode
  1303. * @rqstp: RPC transaction being executed
  1304. * @fhp: NFS filehandle of parent directory
  1305. * @fname: filename of the new symlink
  1306. * @flen: length of @fname
  1307. * @path: content of the new symlink (NUL-terminated)
  1308. * @attrs: requested attributes of new object
  1309. * @resfhp: NFS filehandle of new object
  1310. *
  1311. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1312. *
  1313. * Returns nfs_ok on success, or an nfsstat in network byte order.
  1314. */
  1315. __be32
  1316. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1317. char *fname, int flen,
  1318. char *path, struct nfsd_attrs *attrs,
  1319. struct svc_fh *resfhp)
  1320. {
  1321. struct dentry *dentry, *dnew;
  1322. __be32 err, cerr;
  1323. int host_err;
  1324. err = nfserr_noent;
  1325. if (!flen || path[0] == '\0')
  1326. goto out;
  1327. err = nfserr_exist;
  1328. if (isdotent(fname, flen))
  1329. goto out;
  1330. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1331. if (err)
  1332. goto out;
  1333. host_err = fh_want_write(fhp);
  1334. if (host_err) {
  1335. err = nfserrno(host_err);
  1336. goto out;
  1337. }
  1338. dentry = fhp->fh_dentry;
  1339. inode_lock_nested(dentry->d_inode, I_MUTEX_PARENT);
  1340. dnew = lookup_one_len(fname, dentry, flen);
  1341. if (IS_ERR(dnew)) {
  1342. err = nfserrno(PTR_ERR(dnew));
  1343. inode_unlock(dentry->d_inode);
  1344. goto out_drop_write;
  1345. }
  1346. fh_fill_pre_attrs(fhp);
  1347. host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path);
  1348. err = nfserrno(host_err);
  1349. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1350. if (!err)
  1351. nfsd_create_setattr(rqstp, fhp, resfhp, attrs);
  1352. fh_fill_post_attrs(fhp);
  1353. inode_unlock(dentry->d_inode);
  1354. if (!err)
  1355. err = nfserrno(commit_metadata(fhp));
  1356. dput(dnew);
  1357. if (err==0) err = cerr;
  1358. out_drop_write:
  1359. fh_drop_write(fhp);
  1360. out:
  1361. return err;
  1362. }
  1363. /*
  1364. * Create a hardlink
  1365. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1366. */
  1367. __be32
  1368. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1369. char *name, int len, struct svc_fh *tfhp)
  1370. {
  1371. struct dentry *ddir, *dnew, *dold;
  1372. struct inode *dirp;
  1373. __be32 err;
  1374. int host_err;
  1375. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1376. if (err)
  1377. goto out;
  1378. err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
  1379. if (err)
  1380. goto out;
  1381. err = nfserr_isdir;
  1382. if (d_is_dir(tfhp->fh_dentry))
  1383. goto out;
  1384. err = nfserr_perm;
  1385. if (!len)
  1386. goto out;
  1387. err = nfserr_exist;
  1388. if (isdotent(name, len))
  1389. goto out;
  1390. host_err = fh_want_write(tfhp);
  1391. if (host_err) {
  1392. err = nfserrno(host_err);
  1393. goto out;
  1394. }
  1395. ddir = ffhp->fh_dentry;
  1396. dirp = d_inode(ddir);
  1397. inode_lock_nested(dirp, I_MUTEX_PARENT);
  1398. dnew = lookup_one_len(name, ddir, len);
  1399. if (IS_ERR(dnew)) {
  1400. err = nfserrno(PTR_ERR(dnew));
  1401. goto out_unlock;
  1402. }
  1403. dold = tfhp->fh_dentry;
  1404. err = nfserr_noent;
  1405. if (d_really_is_negative(dold))
  1406. goto out_dput;
  1407. fh_fill_pre_attrs(ffhp);
  1408. host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL);
  1409. fh_fill_post_attrs(ffhp);
  1410. inode_unlock(dirp);
  1411. if (!host_err) {
  1412. err = nfserrno(commit_metadata(ffhp));
  1413. if (!err)
  1414. err = nfserrno(commit_metadata(tfhp));
  1415. } else {
  1416. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1417. err = nfserr_acces;
  1418. else
  1419. err = nfserrno(host_err);
  1420. }
  1421. dput(dnew);
  1422. out_drop_write:
  1423. fh_drop_write(tfhp);
  1424. out:
  1425. return err;
  1426. out_dput:
  1427. dput(dnew);
  1428. out_unlock:
  1429. inode_unlock(dirp);
  1430. goto out_drop_write;
  1431. }
  1432. static void
  1433. nfsd_close_cached_files(struct dentry *dentry)
  1434. {
  1435. struct inode *inode = d_inode(dentry);
  1436. if (inode && S_ISREG(inode->i_mode))
  1437. nfsd_file_close_inode_sync(inode);
  1438. }
  1439. static bool
  1440. nfsd_has_cached_files(struct dentry *dentry)
  1441. {
  1442. bool ret = false;
  1443. struct inode *inode = d_inode(dentry);
  1444. if (inode && S_ISREG(inode->i_mode))
  1445. ret = nfsd_file_is_cached(inode);
  1446. return ret;
  1447. }
  1448. /*
  1449. * Rename a file
  1450. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1451. */
  1452. __be32
  1453. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1454. struct svc_fh *tfhp, char *tname, int tlen)
  1455. {
  1456. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1457. struct inode *fdir, *tdir;
  1458. __be32 err;
  1459. int host_err;
  1460. bool close_cached = false;
  1461. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1462. if (err)
  1463. goto out;
  1464. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1465. if (err)
  1466. goto out;
  1467. fdentry = ffhp->fh_dentry;
  1468. fdir = d_inode(fdentry);
  1469. tdentry = tfhp->fh_dentry;
  1470. tdir = d_inode(tdentry);
  1471. err = nfserr_perm;
  1472. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1473. goto out;
  1474. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1475. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1476. goto out;
  1477. if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
  1478. goto out;
  1479. retry:
  1480. host_err = fh_want_write(ffhp);
  1481. if (host_err) {
  1482. err = nfserrno(host_err);
  1483. goto out;
  1484. }
  1485. trap = lock_rename(tdentry, fdentry);
  1486. fh_fill_pre_attrs(ffhp);
  1487. fh_fill_pre_attrs(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 (d_really_is_negative(odentry))
  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. if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) &&
  1506. nfsd_has_cached_files(ndentry)) {
  1507. close_cached = true;
  1508. goto out_dput_old;
  1509. } else {
  1510. struct renamedata rd = {
  1511. .old_mnt_userns = &init_user_ns,
  1512. .old_dir = fdir,
  1513. .old_dentry = odentry,
  1514. .new_mnt_userns = &init_user_ns,
  1515. .new_dir = tdir,
  1516. .new_dentry = ndentry,
  1517. };
  1518. int retries;
  1519. for (retries = 1;;) {
  1520. host_err = vfs_rename(&rd);
  1521. if (host_err != -EAGAIN || !retries--)
  1522. break;
  1523. if (!nfsd_wait_for_delegreturn(rqstp, d_inode(odentry)))
  1524. break;
  1525. }
  1526. if (!host_err) {
  1527. host_err = commit_metadata(tfhp);
  1528. if (!host_err)
  1529. host_err = commit_metadata(ffhp);
  1530. }
  1531. }
  1532. out_dput_new:
  1533. dput(ndentry);
  1534. out_dput_old:
  1535. dput(odentry);
  1536. out_nfserr:
  1537. err = nfserrno(host_err);
  1538. if (!close_cached) {
  1539. fh_fill_post_attrs(ffhp);
  1540. fh_fill_post_attrs(tfhp);
  1541. }
  1542. unlock_rename(tdentry, fdentry);
  1543. fh_drop_write(ffhp);
  1544. /*
  1545. * If the target dentry has cached open files, then we need to try to
  1546. * close them prior to doing the rename. Flushing delayed fput
  1547. * shouldn't be done with locks held however, so we delay it until this
  1548. * point and then reattempt the whole shebang.
  1549. */
  1550. if (close_cached) {
  1551. close_cached = false;
  1552. nfsd_close_cached_files(ndentry);
  1553. dput(ndentry);
  1554. goto retry;
  1555. }
  1556. out:
  1557. return err;
  1558. }
  1559. /*
  1560. * Unlink a file or directory
  1561. * N.B. After this call fhp needs an fh_put
  1562. */
  1563. __be32
  1564. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1565. char *fname, int flen)
  1566. {
  1567. struct dentry *dentry, *rdentry;
  1568. struct inode *dirp;
  1569. struct inode *rinode;
  1570. __be32 err;
  1571. int host_err;
  1572. err = nfserr_acces;
  1573. if (!flen || isdotent(fname, flen))
  1574. goto out;
  1575. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1576. if (err)
  1577. goto out;
  1578. host_err = fh_want_write(fhp);
  1579. if (host_err)
  1580. goto out_nfserr;
  1581. dentry = fhp->fh_dentry;
  1582. dirp = d_inode(dentry);
  1583. inode_lock_nested(dirp, I_MUTEX_PARENT);
  1584. rdentry = lookup_one_len(fname, dentry, flen);
  1585. host_err = PTR_ERR(rdentry);
  1586. if (IS_ERR(rdentry))
  1587. goto out_unlock;
  1588. if (d_really_is_negative(rdentry)) {
  1589. dput(rdentry);
  1590. host_err = -ENOENT;
  1591. goto out_unlock;
  1592. }
  1593. rinode = d_inode(rdentry);
  1594. ihold(rinode);
  1595. if (!type)
  1596. type = d_inode(rdentry)->i_mode & S_IFMT;
  1597. fh_fill_pre_attrs(fhp);
  1598. if (type != S_IFDIR) {
  1599. int retries;
  1600. if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK)
  1601. nfsd_close_cached_files(rdentry);
  1602. for (retries = 1;;) {
  1603. host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL);
  1604. if (host_err != -EAGAIN || !retries--)
  1605. break;
  1606. if (!nfsd_wait_for_delegreturn(rqstp, rinode))
  1607. break;
  1608. }
  1609. } else {
  1610. host_err = vfs_rmdir(&init_user_ns, dirp, rdentry);
  1611. }
  1612. fh_fill_post_attrs(fhp);
  1613. inode_unlock(dirp);
  1614. if (!host_err)
  1615. host_err = commit_metadata(fhp);
  1616. dput(rdentry);
  1617. iput(rinode); /* truncate the inode here */
  1618. out_drop_write:
  1619. fh_drop_write(fhp);
  1620. out_nfserr:
  1621. if (host_err == -EBUSY) {
  1622. /* name is mounted-on. There is no perfect
  1623. * error status.
  1624. */
  1625. if (nfsd_v4client(rqstp))
  1626. err = nfserr_file_open;
  1627. else
  1628. err = nfserr_acces;
  1629. } else {
  1630. err = nfserrno(host_err);
  1631. }
  1632. out:
  1633. return err;
  1634. out_unlock:
  1635. inode_unlock(dirp);
  1636. goto out_drop_write;
  1637. }
  1638. /*
  1639. * We do this buffering because we must not call back into the file
  1640. * system's ->lookup() method from the filldir callback. That may well
  1641. * deadlock a number of file systems.
  1642. *
  1643. * This is based heavily on the implementation of same in XFS.
  1644. */
  1645. struct buffered_dirent {
  1646. u64 ino;
  1647. loff_t offset;
  1648. int namlen;
  1649. unsigned int d_type;
  1650. char name[];
  1651. };
  1652. struct readdir_data {
  1653. struct dir_context ctx;
  1654. char *dirent;
  1655. size_t used;
  1656. int full;
  1657. };
  1658. static bool nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
  1659. int namlen, loff_t offset, u64 ino,
  1660. unsigned int d_type)
  1661. {
  1662. struct readdir_data *buf =
  1663. container_of(ctx, struct readdir_data, ctx);
  1664. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1665. unsigned int reclen;
  1666. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1667. if (buf->used + reclen > PAGE_SIZE) {
  1668. buf->full = 1;
  1669. return false;
  1670. }
  1671. de->namlen = namlen;
  1672. de->offset = offset;
  1673. de->ino = ino;
  1674. de->d_type = d_type;
  1675. memcpy(de->name, name, namlen);
  1676. buf->used += reclen;
  1677. return true;
  1678. }
  1679. static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp,
  1680. nfsd_filldir_t func, struct readdir_cd *cdp,
  1681. loff_t *offsetp)
  1682. {
  1683. struct buffered_dirent *de;
  1684. int host_err;
  1685. int size;
  1686. loff_t offset;
  1687. struct readdir_data buf = {
  1688. .ctx.actor = nfsd_buffered_filldir,
  1689. .dirent = (void *)__get_free_page(GFP_KERNEL)
  1690. };
  1691. if (!buf.dirent)
  1692. return nfserrno(-ENOMEM);
  1693. offset = *offsetp;
  1694. while (1) {
  1695. unsigned int reclen;
  1696. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1697. buf.used = 0;
  1698. buf.full = 0;
  1699. host_err = iterate_dir(file, &buf.ctx);
  1700. if (buf.full)
  1701. host_err = 0;
  1702. if (host_err < 0)
  1703. break;
  1704. size = buf.used;
  1705. if (!size)
  1706. break;
  1707. de = (struct buffered_dirent *)buf.dirent;
  1708. while (size > 0) {
  1709. offset = de->offset;
  1710. if (func(cdp, de->name, de->namlen, de->offset,
  1711. de->ino, de->d_type))
  1712. break;
  1713. if (cdp->err != nfs_ok)
  1714. break;
  1715. trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen);
  1716. reclen = ALIGN(sizeof(*de) + de->namlen,
  1717. sizeof(u64));
  1718. size -= reclen;
  1719. de = (struct buffered_dirent *)((char *)de + reclen);
  1720. }
  1721. if (size > 0) /* We bailed out early */
  1722. break;
  1723. offset = vfs_llseek(file, 0, SEEK_CUR);
  1724. }
  1725. free_page((unsigned long)(buf.dirent));
  1726. if (host_err)
  1727. return nfserrno(host_err);
  1728. *offsetp = offset;
  1729. return cdp->err;
  1730. }
  1731. /*
  1732. * Read entries from a directory.
  1733. * The NFSv3/4 verifier we ignore for now.
  1734. */
  1735. __be32
  1736. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1737. struct readdir_cd *cdp, nfsd_filldir_t func)
  1738. {
  1739. __be32 err;
  1740. struct file *file;
  1741. loff_t offset = *offsetp;
  1742. int may_flags = NFSD_MAY_READ;
  1743. /* NFSv2 only supports 32 bit cookies */
  1744. if (rqstp->rq_vers > 2)
  1745. may_flags |= NFSD_MAY_64BIT_COOKIE;
  1746. err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
  1747. if (err)
  1748. goto out;
  1749. offset = vfs_llseek(file, offset, SEEK_SET);
  1750. if (offset < 0) {
  1751. err = nfserrno((int)offset);
  1752. goto out_close;
  1753. }
  1754. err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp);
  1755. if (err == nfserr_eof || err == nfserr_toosmall)
  1756. err = nfs_ok; /* can still be found in ->err */
  1757. out_close:
  1758. fput(file);
  1759. out:
  1760. return err;
  1761. }
  1762. /*
  1763. * Get file system stats
  1764. * N.B. After this call fhp needs an fh_put
  1765. */
  1766. __be32
  1767. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1768. {
  1769. __be32 err;
  1770. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1771. if (!err) {
  1772. struct path path = {
  1773. .mnt = fhp->fh_export->ex_path.mnt,
  1774. .dentry = fhp->fh_dentry,
  1775. };
  1776. if (vfs_statfs(&path, stat))
  1777. err = nfserr_io;
  1778. }
  1779. return err;
  1780. }
  1781. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1782. {
  1783. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1784. }
  1785. #ifdef CONFIG_NFSD_V4
  1786. /*
  1787. * Helper function to translate error numbers. In the case of xattr operations,
  1788. * some error codes need to be translated outside of the standard translations.
  1789. *
  1790. * ENODATA needs to be translated to nfserr_noxattr.
  1791. * E2BIG to nfserr_xattr2big.
  1792. *
  1793. * Additionally, vfs_listxattr can return -ERANGE. This means that the
  1794. * file has too many extended attributes to retrieve inside an
  1795. * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation:
  1796. * filesystems will allow the adding of extended attributes until they hit
  1797. * their own internal limit. This limit may be larger than XATTR_LIST_MAX.
  1798. * So, at that point, the attributes are present and valid, but can't
  1799. * be retrieved using listxattr, since the upper level xattr code enforces
  1800. * the XATTR_LIST_MAX limit.
  1801. *
  1802. * This bug means that we need to deal with listxattr returning -ERANGE. The
  1803. * best mapping is to return TOOSMALL.
  1804. */
  1805. static __be32
  1806. nfsd_xattr_errno(int err)
  1807. {
  1808. switch (err) {
  1809. case -ENODATA:
  1810. return nfserr_noxattr;
  1811. case -E2BIG:
  1812. return nfserr_xattr2big;
  1813. case -ERANGE:
  1814. return nfserr_toosmall;
  1815. }
  1816. return nfserrno(err);
  1817. }
  1818. /*
  1819. * Retrieve the specified user extended attribute. To avoid always
  1820. * having to allocate the maximum size (since we are not getting
  1821. * a maximum size from the RPC), do a probe + alloc. Hold a reader
  1822. * lock on i_rwsem to prevent the extended attribute from changing
  1823. * size while we're doing this.
  1824. */
  1825. __be32
  1826. nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
  1827. void **bufp, int *lenp)
  1828. {
  1829. ssize_t len;
  1830. __be32 err;
  1831. char *buf;
  1832. struct inode *inode;
  1833. struct dentry *dentry;
  1834. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
  1835. if (err)
  1836. return err;
  1837. err = nfs_ok;
  1838. dentry = fhp->fh_dentry;
  1839. inode = d_inode(dentry);
  1840. inode_lock_shared(inode);
  1841. len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0);
  1842. /*
  1843. * Zero-length attribute, just return.
  1844. */
  1845. if (len == 0) {
  1846. *bufp = NULL;
  1847. *lenp = 0;
  1848. goto out;
  1849. }
  1850. if (len < 0) {
  1851. err = nfsd_xattr_errno(len);
  1852. goto out;
  1853. }
  1854. if (len > *lenp) {
  1855. err = nfserr_toosmall;
  1856. goto out;
  1857. }
  1858. buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
  1859. if (buf == NULL) {
  1860. err = nfserr_jukebox;
  1861. goto out;
  1862. }
  1863. len = vfs_getxattr(&init_user_ns, dentry, name, buf, len);
  1864. if (len <= 0) {
  1865. kvfree(buf);
  1866. buf = NULL;
  1867. err = nfsd_xattr_errno(len);
  1868. }
  1869. *lenp = len;
  1870. *bufp = buf;
  1871. out:
  1872. inode_unlock_shared(inode);
  1873. return err;
  1874. }
  1875. /*
  1876. * Retrieve the xattr names. Since we can't know how many are
  1877. * user extended attributes, we must get all attributes here,
  1878. * and have the XDR encode filter out the "user." ones.
  1879. *
  1880. * While this could always just allocate an XATTR_LIST_MAX
  1881. * buffer, that's a waste, so do a probe + allocate. To
  1882. * avoid any changes between the probe and allocate, wrap
  1883. * this in inode_lock.
  1884. */
  1885. __be32
  1886. nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp,
  1887. int *lenp)
  1888. {
  1889. ssize_t len;
  1890. __be32 err;
  1891. char *buf;
  1892. struct inode *inode;
  1893. struct dentry *dentry;
  1894. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ);
  1895. if (err)
  1896. return err;
  1897. dentry = fhp->fh_dentry;
  1898. inode = d_inode(dentry);
  1899. *lenp = 0;
  1900. inode_lock_shared(inode);
  1901. len = vfs_listxattr(dentry, NULL, 0);
  1902. if (len <= 0) {
  1903. err = nfsd_xattr_errno(len);
  1904. goto out;
  1905. }
  1906. if (len > XATTR_LIST_MAX) {
  1907. err = nfserr_xattr2big;
  1908. goto out;
  1909. }
  1910. /*
  1911. * We're holding i_rwsem - use GFP_NOFS.
  1912. */
  1913. buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS);
  1914. if (buf == NULL) {
  1915. err = nfserr_jukebox;
  1916. goto out;
  1917. }
  1918. len = vfs_listxattr(dentry, buf, len);
  1919. if (len <= 0) {
  1920. kvfree(buf);
  1921. err = nfsd_xattr_errno(len);
  1922. goto out;
  1923. }
  1924. *lenp = len;
  1925. *bufp = buf;
  1926. err = nfs_ok;
  1927. out:
  1928. inode_unlock_shared(inode);
  1929. return err;
  1930. }
  1931. /**
  1932. * nfsd_removexattr - Remove an extended attribute
  1933. * @rqstp: RPC transaction being executed
  1934. * @fhp: NFS filehandle of object with xattr to remove
  1935. * @name: name of xattr to remove (NUL-terminate)
  1936. *
  1937. * Pass in a NULL pointer for delegated_inode, and let the client deal
  1938. * with NFS4ERR_DELAY (same as with e.g. setattr and remove).
  1939. *
  1940. * Returns nfs_ok on success, or an nfsstat in network byte order.
  1941. */
  1942. __be32
  1943. nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name)
  1944. {
  1945. __be32 err;
  1946. int ret;
  1947. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
  1948. if (err)
  1949. return err;
  1950. ret = fh_want_write(fhp);
  1951. if (ret)
  1952. return nfserrno(ret);
  1953. inode_lock(fhp->fh_dentry->d_inode);
  1954. fh_fill_pre_attrs(fhp);
  1955. ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry,
  1956. name, NULL);
  1957. fh_fill_post_attrs(fhp);
  1958. inode_unlock(fhp->fh_dentry->d_inode);
  1959. fh_drop_write(fhp);
  1960. return nfsd_xattr_errno(ret);
  1961. }
  1962. __be32
  1963. nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name,
  1964. void *buf, u32 len, u32 flags)
  1965. {
  1966. __be32 err;
  1967. int ret;
  1968. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE);
  1969. if (err)
  1970. return err;
  1971. ret = fh_want_write(fhp);
  1972. if (ret)
  1973. return nfserrno(ret);
  1974. inode_lock(fhp->fh_dentry->d_inode);
  1975. fh_fill_pre_attrs(fhp);
  1976. ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf,
  1977. len, flags, NULL);
  1978. fh_fill_post_attrs(fhp);
  1979. inode_unlock(fhp->fh_dentry->d_inode);
  1980. fh_drop_write(fhp);
  1981. return nfsd_xattr_errno(ret);
  1982. }
  1983. #endif
  1984. /*
  1985. * Check for a user's access permissions to this inode.
  1986. */
  1987. __be32
  1988. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1989. struct dentry *dentry, int acc)
  1990. {
  1991. struct inode *inode = d_inode(dentry);
  1992. int err;
  1993. if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
  1994. return 0;
  1995. #if 0
  1996. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1997. acc,
  1998. (acc & NFSD_MAY_READ)? " read" : "",
  1999. (acc & NFSD_MAY_WRITE)? " write" : "",
  2000. (acc & NFSD_MAY_EXEC)? " exec" : "",
  2001. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  2002. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  2003. (acc & NFSD_MAY_LOCK)? " lock" : "",
  2004. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  2005. inode->i_mode,
  2006. IS_IMMUTABLE(inode)? " immut" : "",
  2007. IS_APPEND(inode)? " append" : "",
  2008. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  2009. dprintk(" owner %d/%d user %d/%d\n",
  2010. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  2011. #endif
  2012. /* Normally we reject any write/sattr etc access on a read-only file
  2013. * system. But if it is IRIX doing check on write-access for a
  2014. * device special file, we ignore rofs.
  2015. */
  2016. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  2017. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  2018. if (exp_rdonly(rqstp, exp) ||
  2019. __mnt_is_readonly(exp->ex_path.mnt))
  2020. return nfserr_rofs;
  2021. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  2022. return nfserr_perm;
  2023. }
  2024. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  2025. return nfserr_perm;
  2026. if (acc & NFSD_MAY_LOCK) {
  2027. /* If we cannot rely on authentication in NLM requests,
  2028. * just allow locks, otherwise require read permission, or
  2029. * ownership
  2030. */
  2031. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  2032. return 0;
  2033. else
  2034. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  2035. }
  2036. /*
  2037. * The file owner always gets access permission for accesses that
  2038. * would normally be checked at open time. This is to make
  2039. * file access work even when the client has done a fchmod(fd, 0).
  2040. *
  2041. * However, `cp foo bar' should fail nevertheless when bar is
  2042. * readonly. A sensible way to do this might be to reject all
  2043. * attempts to truncate a read-only file, because a creat() call
  2044. * always implies file truncation.
  2045. * ... but this isn't really fair. A process may reasonably call
  2046. * ftruncate on an open file descriptor on a file with perm 000.
  2047. * We must trust the client to do permission checking - using "ACCESS"
  2048. * with NFSv3.
  2049. */
  2050. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  2051. uid_eq(inode->i_uid, current_fsuid()))
  2052. return 0;
  2053. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  2054. err = inode_permission(&init_user_ns, inode,
  2055. acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
  2056. /* Allow read access to binaries even when mode 111 */
  2057. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  2058. (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
  2059. acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
  2060. err = inode_permission(&init_user_ns, inode, MAY_EXEC);
  2061. return err? nfserrno(err) : 0;
  2062. }