vfs.c 65 KB

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