nfs_serv.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  1. /* $OpenBSD: nfs_serv.c,v 1.105 2015/07/15 22:16:42 deraadt Exp $ */
  2. /* $NetBSD: nfs_serv.c,v 1.34 1997/05/12 23:37:12 fvdl Exp $ */
  3. /*
  4. * Copyright (c) 1989, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * This code is derived from software contributed to Berkeley by
  8. * Rick Macklem at The University of Guelph.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)nfs_serv.c 8.7 (Berkeley) 5/14/95
  35. */
  36. /*
  37. * nfs version 2 and 3 server calls to vnode ops
  38. * - these routines generally have 3 phases
  39. * 1 - break down and validate rpc request in mbuf list
  40. * 2 - do the vnode ops for the request
  41. * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
  42. * 3 - build the rpc reply in an mbuf list
  43. * nb:
  44. * - do not mix the phases, since the nfsm_?? macros can return failures
  45. * on a bad rpc or similar and do not do any vrele() or vput()'s
  46. *
  47. * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
  48. * error number iff error != 0 whereas
  49. * returning an error from the server function implies a fatal error
  50. * such as a badly constructed rpc request that should be dropped without
  51. * a reply.
  52. * For Version 3, nfsm_reply() does not return for the error case, since
  53. * most version 3 rpcs return more than the status for error cases.
  54. */
  55. #include <sys/param.h>
  56. #include <sys/systm.h>
  57. #include <sys/proc.h>
  58. #include <sys/file.h>
  59. #include <sys/namei.h>
  60. #include <sys/vnode.h>
  61. #include <sys/lock.h>
  62. #include <sys/mount.h>
  63. #include <sys/socket.h>
  64. #include <sys/socketvar.h>
  65. #include <sys/mbuf.h>
  66. #include <sys/dirent.h>
  67. #include <sys/stat.h>
  68. #include <sys/kernel.h>
  69. #include <sys/pool.h>
  70. #include <sys/queue.h>
  71. #include <sys/unistd.h>
  72. #include <ufs/ufs/dir.h>
  73. #include <nfs/nfsproto.h>
  74. #include <nfs/nfs.h>
  75. #include <nfs/xdr_subs.h>
  76. #include <nfs/nfsm_subs.h>
  77. #include <nfs/nfs_var.h>
  78. /* Global vars */
  79. extern u_int32_t nfs_xdrneg1;
  80. extern u_int32_t nfs_false, nfs_true;
  81. extern enum vtype nv3tov_type[8];
  82. extern struct nfsstats nfsstats;
  83. extern nfstype nfsv2_type[9];
  84. extern nfstype nfsv3_type[9];
  85. /*
  86. * nfs v3 access service
  87. */
  88. int
  89. nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  90. struct proc *procp, struct mbuf **mrq)
  91. {
  92. struct mbuf *nam = nfsd->nd_nam;
  93. struct nfsm_info info;
  94. struct ucred *cred = &nfsd->nd_cr;
  95. struct vnode *vp;
  96. nfsfh_t nfh;
  97. fhandle_t *fhp;
  98. u_int32_t *tl;
  99. int32_t t1;
  100. int error = 0, rdonly, getret;
  101. char *cp2;
  102. struct vattr va;
  103. u_long testmode, nfsmode;
  104. info.nmi_mreq = NULL;
  105. info.nmi_mrep = nfsd->nd_mrep;
  106. info.nmi_md = nfsd->nd_md;
  107. info.nmi_dpos = nfsd->nd_dpos;
  108. fhp = &nfh.fh_generic;
  109. nfsm_srvmtofh(fhp);
  110. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  111. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  112. if (error) {
  113. nfsm_reply(NFSX_UNSIGNED);
  114. nfsm_srvpostop_attr(nfsd, 1, NULL, &info);
  115. error = 0;
  116. goto nfsmout;
  117. }
  118. nfsmode = fxdr_unsigned(u_int32_t, *tl);
  119. if ((nfsmode & NFSV3ACCESS_READ) &&
  120. nfsrv_access(vp, VREAD, cred, rdonly, procp, 0))
  121. nfsmode &= ~NFSV3ACCESS_READ;
  122. if (vp->v_type == VDIR)
  123. testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
  124. NFSV3ACCESS_DELETE);
  125. else
  126. testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
  127. if ((nfsmode & testmode) &&
  128. nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0))
  129. nfsmode &= ~testmode;
  130. if (vp->v_type == VDIR)
  131. testmode = NFSV3ACCESS_LOOKUP;
  132. else
  133. testmode = NFSV3ACCESS_EXECUTE;
  134. if ((nfsmode & testmode) &&
  135. nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0))
  136. nfsmode &= ~testmode;
  137. getret = VOP_GETATTR(vp, &va, cred, procp);
  138. vput(vp);
  139. nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
  140. nfsm_srvpostop_attr(nfsd, getret, &va, &info);
  141. tl = nfsm_build(&info.nmi_mb, NFSX_UNSIGNED);
  142. *tl = txdr_unsigned(nfsmode);
  143. nfsmout:
  144. return(error);
  145. }
  146. /*
  147. * nfs getattr service
  148. */
  149. int
  150. nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  151. struct proc *procp, struct mbuf **mrq)
  152. {
  153. struct mbuf *nam = nfsd->nd_nam;
  154. struct nfsm_info info;
  155. struct ucred *cred = &nfsd->nd_cr;
  156. struct nfs_fattr *fp;
  157. struct vattr va;
  158. struct vnode *vp;
  159. nfsfh_t nfh;
  160. fhandle_t *fhp;
  161. u_int32_t *tl;
  162. int32_t t1;
  163. int error = 0, rdonly;
  164. char *cp2;
  165. info.nmi_mreq = NULL;
  166. info.nmi_mrep = nfsd->nd_mrep;
  167. info.nmi_md = nfsd->nd_md;
  168. info.nmi_dpos = nfsd->nd_dpos;
  169. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  170. fhp = &nfh.fh_generic;
  171. nfsm_srvmtofh(fhp);
  172. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  173. if (error) {
  174. nfsm_reply(0);
  175. error = 0;
  176. goto nfsmout;
  177. }
  178. error = VOP_GETATTR(vp, &va, cred, procp);
  179. vput(vp);
  180. nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
  181. if (error) {
  182. error = 0;
  183. goto nfsmout;
  184. }
  185. fp = nfsm_build(&info.nmi_mb, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
  186. nfsm_srvfattr(nfsd, &va, fp);
  187. nfsmout:
  188. return(error);
  189. }
  190. /*
  191. * nfs setattr service
  192. */
  193. int
  194. nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  195. struct proc *procp, struct mbuf **mrq)
  196. {
  197. struct mbuf *nam = nfsd->nd_nam;
  198. struct nfsm_info info;
  199. struct ucred *cred = &nfsd->nd_cr;
  200. struct vattr va, preat;
  201. struct nfsv2_sattr *sp;
  202. struct nfs_fattr *fp;
  203. struct vnode *vp;
  204. nfsfh_t nfh;
  205. fhandle_t *fhp;
  206. u_int32_t *tl;
  207. int32_t t1;
  208. int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
  209. int gcheck = 0;
  210. char *cp2;
  211. struct timespec guard;
  212. info.nmi_mreq = NULL;
  213. info.nmi_mrep = nfsd->nd_mrep;
  214. info.nmi_md = nfsd->nd_md;
  215. info.nmi_dpos = nfsd->nd_dpos;
  216. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  217. fhp = &nfh.fh_generic;
  218. nfsm_srvmtofh(fhp);
  219. VATTR_NULL(&va);
  220. if (info.nmi_v3) {
  221. va.va_vaflags |= VA_UTIMES_NULL;
  222. error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep, &info.nmi_dpos);
  223. if (error)
  224. goto nfsmout;
  225. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  226. gcheck = fxdr_unsigned(int, *tl);
  227. if (gcheck) {
  228. nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  229. fxdr_nfsv3time(tl, &guard);
  230. }
  231. } else {
  232. nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
  233. /*
  234. * Nah nah nah nah na nah
  235. * There is a bug in the Sun client that puts 0xffff in the mode
  236. * field of sattr when it should put in 0xffffffff. The u_short
  237. * doesn't sign extend.
  238. * --> check the low order 2 bytes for 0xffff
  239. */
  240. if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
  241. va.va_mode = nfstov_mode(sp->sa_mode);
  242. if (sp->sa_uid != nfs_xdrneg1)
  243. va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
  244. if (sp->sa_gid != nfs_xdrneg1)
  245. va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
  246. if (sp->sa_size != nfs_xdrneg1)
  247. va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
  248. if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
  249. #ifdef notyet
  250. fxdr_nfsv2time(&sp->sa_atime, &va.va_atime);
  251. #else
  252. va.va_atime.tv_sec =
  253. fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
  254. va.va_atime.tv_nsec = 0;
  255. #endif
  256. }
  257. if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
  258. fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime);
  259. }
  260. /*
  261. * Now that we have all the fields, lets do it.
  262. */
  263. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  264. if (error) {
  265. nfsm_reply(2 * NFSX_UNSIGNED);
  266. nfsm_srvwcc(nfsd, preat_ret, &preat, postat_ret, &va, &info);
  267. error = 0;
  268. goto nfsmout;
  269. }
  270. if (info.nmi_v3) {
  271. error = preat_ret = VOP_GETATTR(vp, &preat, cred, procp);
  272. if (!error && gcheck &&
  273. (preat.va_ctime.tv_sec != guard.tv_sec ||
  274. preat.va_ctime.tv_nsec != guard.tv_nsec))
  275. error = NFSERR_NOT_SYNC;
  276. if (error) {
  277. vput(vp);
  278. nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
  279. nfsm_srvwcc(nfsd, preat_ret, &preat, postat_ret, &va,
  280. &info);
  281. error = 0;
  282. goto nfsmout;
  283. }
  284. }
  285. /*
  286. * If the size is being changed write acces is required, otherwise
  287. * just check for a read only file system.
  288. */
  289. if (va.va_size == ((u_quad_t)((quad_t) -1))) {
  290. if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
  291. error = EROFS;
  292. goto out;
  293. }
  294. } else {
  295. if (vp->v_type == VDIR) {
  296. error = EISDIR;
  297. goto out;
  298. } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
  299. procp, 1)) != 0)
  300. goto out;
  301. }
  302. error = VOP_SETATTR(vp, &va, cred, procp);
  303. postat_ret = VOP_GETATTR(vp, &va, cred, procp);
  304. if (!error)
  305. error = postat_ret;
  306. out:
  307. vput(vp);
  308. nfsm_reply(NFSX_WCCORFATTR(info.nmi_v3));
  309. if (info.nmi_v3) {
  310. nfsm_srvwcc(nfsd, preat_ret, &preat, postat_ret, &va,
  311. &info);
  312. error = 0;
  313. goto nfsmout;
  314. } else {
  315. fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
  316. nfsm_srvfattr(nfsd, &va, fp);
  317. }
  318. nfsmout:
  319. return(error);
  320. }
  321. /*
  322. * nfs lookup rpc
  323. */
  324. int
  325. nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  326. struct proc *procp, struct mbuf **mrq)
  327. {
  328. struct mbuf *nam = nfsd->nd_nam;
  329. struct ucred *cred = &nfsd->nd_cr;
  330. struct nfs_fattr *fp;
  331. struct nameidata nd;
  332. struct vnode *vp, *dirp;
  333. struct nfsm_info info;
  334. nfsfh_t nfh;
  335. fhandle_t *fhp;
  336. u_int32_t *tl;
  337. int32_t t1;
  338. int error = 0, len, dirattr_ret = 1;
  339. int v3 = (nfsd->nd_flag & ND_NFSV3);
  340. char *cp2;
  341. struct vattr va, dirattr;
  342. info.nmi_mrep = nfsd->nd_mrep;
  343. info.nmi_mreq = NULL;
  344. info.nmi_md = nfsd->nd_md;
  345. info.nmi_dpos = nfsd->nd_dpos;
  346. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  347. fhp = &nfh.fh_generic;
  348. nfsm_srvmtofh(fhp);
  349. nfsm_srvnamesiz(len);
  350. nd.ni_cnd.cn_cred = cred;
  351. nd.ni_cnd.cn_nameiop = LOOKUP;
  352. nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
  353. error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md, &info.nmi_dpos, &dirp, procp);
  354. if (dirp) {
  355. if (info.nmi_v3)
  356. dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
  357. procp);
  358. vrele(dirp);
  359. }
  360. if (error) {
  361. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3));
  362. nfsm_srvpostop_attr(nfsd, dirattr_ret, &dirattr, &info);
  363. return (0);
  364. }
  365. vrele(nd.ni_startdir);
  366. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  367. vp = nd.ni_vp;
  368. memset(fhp, 0, sizeof(nfh));
  369. fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
  370. error = VFS_VPTOFH(vp, &fhp->fh_fid);
  371. if (!error)
  372. error = VOP_GETATTR(vp, &va, cred, procp);
  373. vput(vp);
  374. nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_POSTOPORFATTR(info.nmi_v3)
  375. + NFSX_POSTOPATTR(info.nmi_v3));
  376. if (error) {
  377. nfsm_srvpostop_attr(nfsd, dirattr_ret, &dirattr, &info);
  378. error = 0;
  379. goto nfsmout;
  380. }
  381. nfsm_srvfhtom(&info.nmi_mb, fhp, info.nmi_v3);
  382. if (v3) {
  383. nfsm_srvpostop_attr(nfsd, 0, &va, &info);
  384. nfsm_srvpostop_attr(nfsd, dirattr_ret, &dirattr, &info);
  385. } else {
  386. fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
  387. nfsm_srvfattr(nfsd, &va, fp);
  388. }
  389. nfsmout:
  390. return(error);
  391. }
  392. /*
  393. * nfs readlink service
  394. */
  395. int
  396. nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  397. struct proc *procp, struct mbuf **mrq)
  398. {
  399. struct mbuf *nam = nfsd->nd_nam;
  400. struct ucred *cred = &nfsd->nd_cr;
  401. struct iovec iov;
  402. struct mbuf *mp = NULL;
  403. struct nfsm_info info;
  404. u_int32_t *tl;
  405. int32_t t1;
  406. int error = 0, rdonly, tlen, len = 0, getret;
  407. char *cp2;
  408. struct vnode *vp;
  409. struct vattr attr;
  410. nfsfh_t nfh;
  411. fhandle_t *fhp;
  412. struct uio uio;
  413. info.nmi_mreq = NULL;
  414. info.nmi_mrep = nfsd->nd_mrep;
  415. info.nmi_md = nfsd->nd_md;
  416. info.nmi_dpos = nfsd->nd_dpos;
  417. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  418. memset(&uio, 0, sizeof(uio));
  419. fhp = &nfh.fh_generic;
  420. nfsm_srvmtofh(fhp);
  421. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  422. if (error) {
  423. nfsm_reply(2 * NFSX_UNSIGNED);
  424. nfsm_srvpostop_attr(nfsd, 1, NULL, &info);
  425. error = 0;
  426. goto nfsmout;
  427. }
  428. if (vp->v_type != VLNK) {
  429. if (info.nmi_v3)
  430. error = EINVAL;
  431. else
  432. error = ENXIO;
  433. goto out;
  434. }
  435. MGET(mp, M_WAIT, MT_DATA);
  436. MCLGET(mp, M_WAIT); /* MLEN < NFS_MAXPATHLEN < MCLBYTES */
  437. mp->m_len = NFS_MAXPATHLEN;
  438. len = NFS_MAXPATHLEN;
  439. iov.iov_base = mtod(mp, caddr_t);
  440. iov.iov_len = mp->m_len;
  441. uio.uio_iov = &iov;
  442. uio.uio_iovcnt = 1;
  443. uio.uio_offset = 0;
  444. uio.uio_resid = NFS_MAXPATHLEN;
  445. uio.uio_rw = UIO_READ;
  446. uio.uio_segflg = UIO_SYSSPACE;
  447. uio.uio_procp = NULL;
  448. error = VOP_READLINK(vp, &uio, cred);
  449. out:
  450. getret = VOP_GETATTR(vp, &attr, cred, procp);
  451. vput(vp);
  452. if (error)
  453. m_freem(mp);
  454. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_UNSIGNED);
  455. if (info.nmi_v3) {
  456. nfsm_srvpostop_attr(nfsd, getret, &attr, &info);
  457. if (error) {
  458. error = 0;
  459. goto nfsmout;
  460. }
  461. }
  462. if (uio.uio_resid > 0) {
  463. len -= uio.uio_resid;
  464. tlen = nfsm_rndup(len);
  465. nfsm_adj(mp, NFS_MAXPATHLEN-tlen, tlen-len);
  466. }
  467. tl = nfsm_build(&info.nmi_mb, NFSX_UNSIGNED);
  468. *tl = txdr_unsigned(len);
  469. info.nmi_mb->m_next = mp;
  470. nfsmout:
  471. return (error);
  472. }
  473. /*
  474. * nfs read service
  475. */
  476. int
  477. nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  478. struct proc *procp, struct mbuf **mrq)
  479. {
  480. struct mbuf *nam = nfsd->nd_nam;
  481. struct ucred *cred = &nfsd->nd_cr;
  482. struct mbuf *m;
  483. struct nfs_fattr *fp;
  484. struct nfsm_info info;
  485. u_int32_t *tl;
  486. int32_t t1;
  487. int i, reqlen;
  488. int error = 0, rdonly, cnt, len, left, siz, tlen, getret = 1;
  489. char *cp2;
  490. struct mbuf *m2;
  491. struct vnode *vp;
  492. nfsfh_t nfh;
  493. fhandle_t *fhp;
  494. struct uio io, *uiop = &io;
  495. struct vattr va;
  496. off_t off;
  497. info.nmi_mreq = NULL;
  498. info.nmi_mrep = nfsd->nd_mrep;
  499. info.nmi_md = nfsd->nd_md;
  500. info.nmi_dpos = nfsd->nd_dpos;
  501. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  502. fhp = &nfh.fh_generic;
  503. nfsm_srvmtofh(fhp);
  504. if (info.nmi_v3) {
  505. nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  506. off = fxdr_hyper(tl);
  507. } else {
  508. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  509. off = (off_t)fxdr_unsigned(u_int32_t, *tl);
  510. }
  511. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  512. reqlen = fxdr_unsigned(int32_t, *tl);
  513. if (reqlen > (NFS_SRVMAXDATA(nfsd)) || reqlen <= 0) {
  514. error = EBADRPC;
  515. nfsm_reply(0);
  516. }
  517. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  518. if (error)
  519. goto bad;
  520. if (vp->v_type != VREG) {
  521. if (info.nmi_v3)
  522. error = EINVAL;
  523. else
  524. error = (vp->v_type == VDIR) ? EISDIR : EACCES;
  525. }
  526. if (!error) {
  527. if ((error = nfsrv_access(vp, VREAD, cred, rdonly, procp, 1)) != 0)
  528. error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 1);
  529. }
  530. getret = VOP_GETATTR(vp, &va, cred, procp);
  531. if (!error)
  532. error = getret;
  533. if (error)
  534. goto vbad;
  535. if (off >= va.va_size)
  536. cnt = 0;
  537. else if ((off + reqlen) > va.va_size)
  538. cnt = va.va_size - off;
  539. else
  540. cnt = reqlen;
  541. nfsm_reply(NFSX_POSTOPORFATTR(info.nmi_v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
  542. if (info.nmi_v3) {
  543. tl = nfsm_build(&info.nmi_mb, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
  544. *tl++ = nfs_true;
  545. fp = (struct nfs_fattr *)tl;
  546. tl += (NFSX_V3FATTR / sizeof (u_int32_t));
  547. } else {
  548. tl = nfsm_build(&info.nmi_mb, NFSX_V2FATTR + NFSX_UNSIGNED);
  549. fp = (struct nfs_fattr *)tl;
  550. tl += (NFSX_V2FATTR / sizeof (u_int32_t));
  551. }
  552. len = left = nfsm_rndup (cnt);
  553. if (cnt > 0) {
  554. struct iovec *iv, *iv2;
  555. size_t ivlen;
  556. /*
  557. * Generate the mbuf list with the uio_iov ref. to it.
  558. */
  559. i = 0;
  560. m = m2 = info.nmi_mb;
  561. while (left > 0) {
  562. siz = min(M_TRAILINGSPACE(m), left);
  563. if (siz > 0) {
  564. left -= siz;
  565. i++;
  566. }
  567. if (left > 0) {
  568. MGET(m, M_WAIT, MT_DATA);
  569. if (left >= MINCLSIZE)
  570. MCLGET(m, M_WAIT);
  571. m->m_len = 0;
  572. m2->m_next = m;
  573. m2 = m;
  574. }
  575. }
  576. iv = mallocarray(i, sizeof(*iv), M_TEMP, M_WAITOK);
  577. ivlen = i * sizeof(*iv);
  578. uiop->uio_iov = iv2 = iv;
  579. m = info.nmi_mb;
  580. left = len;
  581. i = 0;
  582. while (left > 0) {
  583. if (m == NULL)
  584. panic("nfsrv_read iov");
  585. siz = min(M_TRAILINGSPACE(m), left);
  586. if (siz > 0) {
  587. iv->iov_base = mtod(m, caddr_t) + m->m_len;
  588. iv->iov_len = siz;
  589. m->m_len += siz;
  590. left -= siz;
  591. iv++;
  592. i++;
  593. }
  594. m = m->m_next;
  595. }
  596. uiop->uio_iovcnt = i;
  597. uiop->uio_offset = off;
  598. uiop->uio_resid = len;
  599. uiop->uio_rw = UIO_READ;
  600. uiop->uio_segflg = UIO_SYSSPACE;
  601. error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
  602. off = uiop->uio_offset;
  603. free(iv2, M_TEMP, ivlen);
  604. if (error || (getret = VOP_GETATTR(vp, &va, cred, procp)) != 0){
  605. if (!error)
  606. error = getret;
  607. m_freem(info.nmi_mreq);
  608. goto vbad;
  609. }
  610. } else
  611. uiop->uio_resid = 0;
  612. vput(vp);
  613. nfsm_srvfattr(nfsd, &va, fp);
  614. tlen = len - uiop->uio_resid;
  615. cnt = cnt < tlen ? cnt : tlen;
  616. tlen = nfsm_rndup (cnt);
  617. if (len != tlen || tlen != cnt)
  618. nfsm_adj(info.nmi_mb, len - tlen, tlen - cnt);
  619. if (info.nmi_v3) {
  620. *tl++ = txdr_unsigned(cnt);
  621. if (len < reqlen)
  622. *tl++ = nfs_true;
  623. else
  624. *tl++ = nfs_false;
  625. }
  626. *tl = txdr_unsigned(cnt);
  627. nfsmout:
  628. return(error);
  629. vbad:
  630. vput(vp);
  631. bad:
  632. nfsm_reply(0);
  633. nfsm_srvpostop_attr(nfsd, getret, &va, &info);
  634. return (0);
  635. }
  636. /*
  637. * nfs write service
  638. */
  639. int
  640. nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  641. struct proc *procp, struct mbuf **mrq)
  642. {
  643. struct mbuf *nam = nfsd->nd_nam;
  644. struct ucred *cred = &nfsd->nd_cr;
  645. struct nfsm_info info;
  646. int i, cnt;
  647. struct mbuf *mp;
  648. struct nfs_fattr *fp;
  649. struct vattr va, forat;
  650. u_int32_t *tl;
  651. int32_t t1;
  652. int error = 0, rdonly, len, forat_ret = 1;
  653. int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
  654. int stable = NFSV3WRITE_FILESYNC;
  655. char *cp2;
  656. struct vnode *vp;
  657. nfsfh_t nfh;
  658. fhandle_t *fhp;
  659. struct uio io, *uiop = &io;
  660. off_t off;
  661. info.nmi_mreq = NULL;
  662. info.nmi_mrep = nfsd->nd_mrep;
  663. info.nmi_md = nfsd->nd_md;
  664. info.nmi_dpos = nfsd->nd_dpos;
  665. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  666. if (info.nmi_mrep == NULL) {
  667. *mrq = NULL;
  668. return (0);
  669. }
  670. fhp = &nfh.fh_generic;
  671. nfsm_srvmtofh(fhp);
  672. if (info.nmi_v3) {
  673. nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
  674. off = fxdr_hyper(tl);
  675. tl += 3;
  676. stable = fxdr_unsigned(int, *tl++);
  677. } else {
  678. nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
  679. off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
  680. tl += 2;
  681. }
  682. retlen = len = fxdr_unsigned(int32_t, *tl);
  683. cnt = i = 0;
  684. /*
  685. * For NFS Version 2, it is not obvious what a write of zero length
  686. * should do, but I might as well be consistent with Version 3,
  687. * which is to return ok so long as there are no permission problems.
  688. */
  689. if (len > 0) {
  690. zeroing = 1;
  691. mp = info.nmi_mrep;
  692. while (mp) {
  693. if (mp == info.nmi_md) {
  694. zeroing = 0;
  695. adjust = info.nmi_dpos - mtod(mp, caddr_t);
  696. mp->m_len -= adjust;
  697. if (mp->m_len > 0 && adjust > 0)
  698. mp->m_data += adjust;
  699. }
  700. if (zeroing)
  701. mp->m_len = 0;
  702. else if (mp->m_len > 0) {
  703. i += mp->m_len;
  704. if (i > len) {
  705. mp->m_len -= (i - len);
  706. zeroing = 1;
  707. }
  708. if (mp->m_len > 0)
  709. cnt++;
  710. }
  711. mp = mp->m_next;
  712. }
  713. }
  714. if (len > NFS_MAXDATA || len < 0 || i < len) {
  715. error = EIO;
  716. goto bad;
  717. }
  718. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  719. if (error)
  720. goto bad;
  721. if (info.nmi_v3)
  722. forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
  723. if (vp->v_type != VREG) {
  724. if (info.nmi_v3)
  725. error = EINVAL;
  726. else
  727. error = (vp->v_type == VDIR) ? EISDIR : EACCES;
  728. goto vbad;
  729. }
  730. error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
  731. if (error)
  732. goto vbad;
  733. if (len > 0) {
  734. struct iovec *iv, *ivp;
  735. size_t ivlen;
  736. ivp = mallocarray(cnt, sizeof(*ivp), M_TEMP, M_WAITOK);
  737. ivlen = cnt * sizeof(*ivp);
  738. uiop->uio_iov = iv = ivp;
  739. uiop->uio_iovcnt = cnt;
  740. mp = info.nmi_mrep;
  741. while (mp) {
  742. if (mp->m_len > 0) {
  743. ivp->iov_base = mtod(mp, caddr_t);
  744. ivp->iov_len = mp->m_len;
  745. ivp++;
  746. }
  747. mp = mp->m_next;
  748. }
  749. if (stable == NFSV3WRITE_UNSTABLE)
  750. ioflags = IO_NODELOCKED;
  751. else if (stable == NFSV3WRITE_DATASYNC)
  752. ioflags = (IO_SYNC | IO_NODELOCKED);
  753. else
  754. ioflags = (IO_SYNC | IO_NODELOCKED);
  755. uiop->uio_resid = len;
  756. uiop->uio_rw = UIO_WRITE;
  757. uiop->uio_segflg = UIO_SYSSPACE;
  758. uiop->uio_procp = NULL;
  759. uiop->uio_offset = off;
  760. error = VOP_WRITE(vp, uiop, ioflags, cred);
  761. nfsstats.srvvop_writes++;
  762. free(iv, M_TEMP, ivlen);
  763. }
  764. aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
  765. vput(vp);
  766. if (!error)
  767. error = aftat_ret;
  768. nfsm_reply(NFSX_PREOPATTR(info.nmi_v3) + NFSX_POSTOPORFATTR(info.nmi_v3) +
  769. 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.nmi_v3));
  770. if (info.nmi_v3) {
  771. nfsm_srvwcc(nfsd, forat_ret, &forat, aftat_ret, &va, &info);
  772. if (error) {
  773. error = 0;
  774. goto nfsmout;
  775. }
  776. tl = nfsm_build(&info.nmi_mb, 4 * NFSX_UNSIGNED);
  777. *tl++ = txdr_unsigned(retlen);
  778. if (stable == NFSV3WRITE_UNSTABLE)
  779. *tl++ = txdr_unsigned(stable);
  780. else
  781. *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
  782. /*
  783. * Actually, there is no need to txdr these fields,
  784. * but it may make the values more human readable,
  785. * for debugging purposes.
  786. */
  787. *tl++ = txdr_unsigned(boottime.tv_sec);
  788. *tl = txdr_unsigned(boottime.tv_nsec/1000);
  789. } else {
  790. fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
  791. nfsm_srvfattr(nfsd, &va, fp);
  792. }
  793. nfsmout:
  794. return(error);
  795. vbad:
  796. vput(vp);
  797. bad:
  798. nfsm_reply(0);
  799. nfsm_srvwcc(nfsd, forat_ret, &forat, aftat_ret, &va, &info);
  800. return (0);
  801. }
  802. /*
  803. * nfs create service
  804. * now does a truncate to 0 length via. setattr if it already exists
  805. */
  806. int
  807. nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  808. struct proc *procp, struct mbuf **mrq)
  809. {
  810. struct mbuf *nam = nfsd->nd_nam;
  811. struct ucred *cred = &nfsd->nd_cr;
  812. struct nfs_fattr *fp;
  813. struct vattr va, dirfor, diraft;
  814. struct nfsv2_sattr *sp;
  815. struct nfsm_info info;
  816. u_int32_t *tl;
  817. struct nameidata nd;
  818. caddr_t cp;
  819. int32_t t1;
  820. int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
  821. dev_t rdev = 0;
  822. int how, exclusive_flag = 0;
  823. char *cp2;
  824. struct vnode *vp = NULL, *dirp = NULL;
  825. nfsfh_t nfh;
  826. fhandle_t *fhp;
  827. u_quad_t tempsize;
  828. u_char cverf[NFSX_V3CREATEVERF];
  829. info.nmi_mreq = NULL;
  830. info.nmi_mrep = nfsd->nd_mrep;
  831. info.nmi_md = nfsd->nd_md;
  832. info.nmi_dpos = nfsd->nd_dpos;
  833. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  834. fhp = &nfh.fh_generic;
  835. nfsm_srvmtofh(fhp);
  836. nfsm_srvnamesiz(len);
  837. nd.ni_cnd.cn_cred = cred;
  838. nd.ni_cnd.cn_nameiop = CREATE;
  839. nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
  840. error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
  841. &info.nmi_dpos, &dirp, procp);
  842. if (dirp) {
  843. if (info.nmi_v3)
  844. dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
  845. else {
  846. vrele(dirp);
  847. dirp = NULL;
  848. }
  849. }
  850. if (error) {
  851. nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
  852. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  853. &info);
  854. if (dirp)
  855. vrele(dirp);
  856. return (0);
  857. }
  858. VATTR_NULL(&va);
  859. if (info.nmi_v3) {
  860. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  861. how = fxdr_unsigned(int, *tl);
  862. switch (how) {
  863. case NFSV3CREATE_GUARDED:
  864. if (nd.ni_vp) {
  865. error = EEXIST;
  866. break;
  867. }
  868. case NFSV3CREATE_UNCHECKED:
  869. error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep,
  870. &info.nmi_dpos);
  871. if (error)
  872. goto nfsmout;
  873. break;
  874. case NFSV3CREATE_EXCLUSIVE:
  875. nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
  876. bcopy(cp, cverf, NFSX_V3CREATEVERF);
  877. exclusive_flag = 1;
  878. if (nd.ni_vp == NULL)
  879. va.va_mode = 0;
  880. break;
  881. };
  882. va.va_type = VREG;
  883. } else {
  884. nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
  885. va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
  886. if (va.va_type == VNON)
  887. va.va_type = VREG;
  888. va.va_mode = nfstov_mode(sp->sa_mode);
  889. switch (va.va_type) {
  890. case VREG:
  891. tsize = fxdr_unsigned(int32_t, sp->sa_size);
  892. if (tsize != -1)
  893. va.va_size = (u_quad_t)tsize;
  894. break;
  895. case VCHR:
  896. case VBLK:
  897. case VFIFO:
  898. rdev = (dev_t)fxdr_unsigned(int32_t, sp->sa_size);
  899. break;
  900. default:
  901. break;
  902. };
  903. }
  904. /*
  905. * Iff doesn't exist, create it
  906. * otherwise just truncate to 0 length
  907. * should I set the mode too ??
  908. */
  909. if (nd.ni_vp == NULL) {
  910. if (va.va_type == VREG || va.va_type == VSOCK) {
  911. vrele(nd.ni_startdir);
  912. error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
  913. if (!error) {
  914. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  915. if (exclusive_flag) {
  916. exclusive_flag = 0;
  917. VATTR_NULL(&va);
  918. bcopy(cverf, (caddr_t)&va.va_atime,
  919. NFSX_V3CREATEVERF);
  920. error = VOP_SETATTR(nd.ni_vp, &va, cred,
  921. procp);
  922. }
  923. }
  924. } else if (va.va_type == VCHR || va.va_type == VBLK ||
  925. va.va_type == VFIFO) {
  926. if (va.va_type == VCHR && rdev == 0xffffffff)
  927. va.va_type = VFIFO;
  928. if (va.va_type != VFIFO &&
  929. (error = suser_ucred(cred))) {
  930. vrele(nd.ni_startdir);
  931. if (nd.ni_cnd.cn_flags & HASBUF) {
  932. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  933. nd.ni_cnd.cn_flags &= ~HASBUF;
  934. }
  935. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  936. vput(nd.ni_dvp);
  937. nfsm_reply(0);
  938. error = 0;
  939. goto nfsmout;
  940. } else
  941. va.va_rdev = (dev_t)rdev;
  942. error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
  943. &va);
  944. if (error) {
  945. vrele(nd.ni_startdir);
  946. if (nd.ni_cnd.cn_flags & HASBUF) {
  947. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  948. nd.ni_cnd.cn_flags &= ~HASBUF;
  949. }
  950. nfsm_reply(0);
  951. error = 0;
  952. goto nfsmout;
  953. }
  954. nd.ni_cnd.cn_nameiop = LOOKUP;
  955. nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
  956. nd.ni_cnd.cn_proc = procp;
  957. nd.ni_cnd.cn_cred = cred;
  958. if ((error = vfs_lookup(&nd)) != 0) {
  959. if (nd.ni_cnd.cn_flags & HASBUF) {
  960. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  961. nd.ni_cnd.cn_flags &= ~HASBUF;
  962. }
  963. nfsm_reply(0);
  964. error = 0;
  965. goto nfsmout;
  966. }
  967. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  968. if (nd.ni_cnd.cn_flags & ISSYMLINK) {
  969. vrele(nd.ni_dvp);
  970. vput(nd.ni_vp);
  971. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  972. error = EINVAL;
  973. nfsm_reply(0);
  974. error = 0;
  975. goto nfsmout;
  976. }
  977. } else {
  978. vrele(nd.ni_startdir);
  979. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  980. nd.ni_cnd.cn_flags &= ~HASBUF;
  981. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  982. vput(nd.ni_dvp);
  983. error = ENXIO;
  984. }
  985. vp = nd.ni_vp;
  986. } else {
  987. vrele(nd.ni_startdir);
  988. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  989. nd.ni_cnd.cn_flags &= ~HASBUF;
  990. vp = nd.ni_vp;
  991. if (nd.ni_dvp == vp)
  992. vrele(nd.ni_dvp);
  993. else
  994. vput(nd.ni_dvp);
  995. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  996. if (va.va_size != -1) {
  997. error = nfsrv_access(vp, VWRITE, cred,
  998. (nd.ni_cnd.cn_flags & RDONLY), procp, 0);
  999. if (!error) {
  1000. tempsize = va.va_size;
  1001. VATTR_NULL(&va);
  1002. va.va_size = tempsize;
  1003. error = VOP_SETATTR(vp, &va, cred,
  1004. procp);
  1005. }
  1006. if (error)
  1007. vput(vp);
  1008. }
  1009. }
  1010. if (!error) {
  1011. memset(fhp, 0, sizeof(nfh));
  1012. fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
  1013. error = VFS_VPTOFH(vp, &fhp->fh_fid);
  1014. if (!error)
  1015. error = VOP_GETATTR(vp, &va, cred, procp);
  1016. vput(vp);
  1017. }
  1018. if (info.nmi_v3) {
  1019. if (exclusive_flag && !error &&
  1020. bcmp(cverf, (caddr_t)&va.va_atime, NFSX_V3CREATEVERF))
  1021. error = EEXIST;
  1022. diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
  1023. vrele(dirp);
  1024. }
  1025. nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_FATTR(info.nmi_v3)
  1026. + NFSX_WCCDATA(info.nmi_v3));
  1027. if (info.nmi_v3) {
  1028. if (!error) {
  1029. nfsm_srvpostop_fh(fhp);
  1030. nfsm_srvpostop_attr(nfsd, 0, &va, &info);
  1031. }
  1032. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1033. &info);
  1034. } else {
  1035. nfsm_srvfhtom(&info.nmi_mb, fhp, info.nmi_v3);
  1036. fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
  1037. nfsm_srvfattr(nfsd, &va, fp);
  1038. }
  1039. return (0);
  1040. nfsmout:
  1041. if (dirp)
  1042. vrele(dirp);
  1043. if (nd.ni_cnd.cn_nameiop != LOOKUP) {
  1044. vrele(nd.ni_startdir);
  1045. if (nd.ni_cnd.cn_flags & HASBUF) {
  1046. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1047. nd.ni_cnd.cn_flags &= ~HASBUF;
  1048. }
  1049. }
  1050. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1051. if (nd.ni_dvp == nd.ni_vp)
  1052. vrele(nd.ni_dvp);
  1053. else
  1054. vput(nd.ni_dvp);
  1055. if (nd.ni_vp)
  1056. vput(nd.ni_vp);
  1057. return (error);
  1058. }
  1059. /*
  1060. * nfs v3 mknod service
  1061. */
  1062. int
  1063. nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1064. struct proc *procp, struct mbuf **mrq)
  1065. {
  1066. struct mbuf *nam = nfsd->nd_nam;
  1067. struct ucred *cred = &nfsd->nd_cr;
  1068. struct vattr va, dirfor, diraft;
  1069. struct nfsm_info info;
  1070. u_int32_t *tl;
  1071. struct nameidata nd;
  1072. int32_t t1;
  1073. int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
  1074. u_int32_t major, minor;
  1075. enum vtype vtyp;
  1076. char *cp2;
  1077. struct vnode *vp, *dirp = NULL;
  1078. nfsfh_t nfh;
  1079. fhandle_t *fhp;
  1080. info.nmi_mreq = NULL;
  1081. info.nmi_mrep = nfsd->nd_mrep;
  1082. info.nmi_md = nfsd->nd_md;
  1083. info.nmi_dpos = nfsd->nd_dpos;
  1084. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1085. nd.ni_cnd.cn_nameiop = 0;
  1086. fhp = &nfh.fh_generic;
  1087. nfsm_srvmtofh(fhp);
  1088. nfsm_srvnamesiz(len);
  1089. nd.ni_cnd.cn_cred = cred;
  1090. nd.ni_cnd.cn_nameiop = CREATE;
  1091. nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
  1092. error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md, &info.nmi_dpos, &dirp, procp);
  1093. if (dirp)
  1094. dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
  1095. if (error) {
  1096. nfsm_reply(NFSX_WCCDATA(1));
  1097. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1098. &info);
  1099. if (dirp)
  1100. vrele(dirp);
  1101. return (0);
  1102. }
  1103. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  1104. vtyp = nfsv3tov_type(*tl);
  1105. if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
  1106. vrele(nd.ni_startdir);
  1107. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1108. error = NFSERR_BADTYPE;
  1109. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1110. vput(nd.ni_dvp);
  1111. goto out;
  1112. }
  1113. VATTR_NULL(&va);
  1114. error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep, &info.nmi_dpos);
  1115. if (error)
  1116. goto nfsmout;
  1117. if (vtyp == VCHR || vtyp == VBLK) {
  1118. nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  1119. major = fxdr_unsigned(u_int32_t, *tl++);
  1120. minor = fxdr_unsigned(u_int32_t, *tl);
  1121. va.va_rdev = makedev(major, minor);
  1122. }
  1123. /*
  1124. * Iff doesn't exist, create it.
  1125. */
  1126. if (nd.ni_vp) {
  1127. vrele(nd.ni_startdir);
  1128. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1129. error = EEXIST;
  1130. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1131. vput(nd.ni_dvp);
  1132. goto out;
  1133. }
  1134. va.va_type = vtyp;
  1135. if (vtyp == VSOCK) {
  1136. vrele(nd.ni_startdir);
  1137. error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
  1138. if (!error)
  1139. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1140. } else {
  1141. if (va.va_type != VFIFO &&
  1142. (error = suser_ucred(cred))) {
  1143. vrele(nd.ni_startdir);
  1144. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1145. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1146. vput(nd.ni_dvp);
  1147. goto out;
  1148. }
  1149. error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
  1150. if (error) {
  1151. vrele(nd.ni_startdir);
  1152. goto out;
  1153. }
  1154. nd.ni_cnd.cn_nameiop = LOOKUP;
  1155. nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
  1156. nd.ni_cnd.cn_proc = procp;
  1157. nd.ni_cnd.cn_cred = procp->p_ucred;
  1158. error = vfs_lookup(&nd);
  1159. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1160. if (error)
  1161. goto out;
  1162. if (nd.ni_cnd.cn_flags & ISSYMLINK) {
  1163. vrele(nd.ni_dvp);
  1164. vput(nd.ni_vp);
  1165. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1166. error = EINVAL;
  1167. }
  1168. }
  1169. out:
  1170. vp = nd.ni_vp;
  1171. if (!error) {
  1172. memset(fhp, 0, sizeof(nfh));
  1173. fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
  1174. error = VFS_VPTOFH(vp, &fhp->fh_fid);
  1175. if (!error)
  1176. error = VOP_GETATTR(vp, &va, cred, procp);
  1177. vput(vp);
  1178. }
  1179. diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
  1180. vrele(dirp);
  1181. nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
  1182. if (!error) {
  1183. nfsm_srvpostop_fh(fhp);
  1184. nfsm_srvpostop_attr(nfsd, 0, &va, &info);
  1185. }
  1186. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft, &info);
  1187. return (0);
  1188. nfsmout:
  1189. if (dirp)
  1190. vrele(dirp);
  1191. if (nd.ni_cnd.cn_nameiop) {
  1192. vrele(nd.ni_startdir);
  1193. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1194. }
  1195. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1196. if (nd.ni_dvp == nd.ni_vp)
  1197. vrele(nd.ni_dvp);
  1198. else
  1199. vput(nd.ni_dvp);
  1200. if (nd.ni_vp)
  1201. vput(nd.ni_vp);
  1202. return (error);
  1203. }
  1204. /*
  1205. * nfs remove service
  1206. */
  1207. int
  1208. nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1209. struct proc *procp, struct mbuf **mrq)
  1210. {
  1211. struct mbuf *nam = nfsd->nd_nam;
  1212. struct ucred *cred = &nfsd->nd_cr;
  1213. struct nameidata nd;
  1214. struct nfsm_info info;
  1215. u_int32_t *tl;
  1216. int32_t t1;
  1217. int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
  1218. char *cp2;
  1219. struct vnode *vp, *dirp;
  1220. struct vattr dirfor, diraft;
  1221. nfsfh_t nfh;
  1222. fhandle_t *fhp;
  1223. info.nmi_mreq = NULL;
  1224. info.nmi_mrep = nfsd->nd_mrep;
  1225. info.nmi_md = nfsd->nd_md;
  1226. info.nmi_dpos = nfsd->nd_dpos;
  1227. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1228. vp = NULL;
  1229. fhp = &nfh.fh_generic;
  1230. nfsm_srvmtofh(fhp);
  1231. nfsm_srvnamesiz(len);
  1232. nd.ni_cnd.cn_cred = cred;
  1233. nd.ni_cnd.cn_nameiop = DELETE;
  1234. nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
  1235. error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md, &info.nmi_dpos, &dirp, procp);
  1236. if (dirp) {
  1237. if (info.nmi_v3)
  1238. dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
  1239. else {
  1240. vrele(dirp);
  1241. dirp = NULL;
  1242. }
  1243. }
  1244. if (!error) {
  1245. vp = nd.ni_vp;
  1246. if (vp->v_type == VDIR &&
  1247. (error = suser_ucred(cred)) != 0)
  1248. goto out;
  1249. /*
  1250. * The root of a mounted filesystem cannot be deleted.
  1251. */
  1252. if (vp->v_flag & VROOT) {
  1253. error = EBUSY;
  1254. goto out;
  1255. }
  1256. if (vp->v_flag & VTEXT)
  1257. uvm_vnp_uncache(vp);
  1258. out:
  1259. if (!error) {
  1260. error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
  1261. } else {
  1262. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1263. if (nd.ni_dvp == vp)
  1264. vrele(nd.ni_dvp);
  1265. else
  1266. vput(nd.ni_dvp);
  1267. vput(vp);
  1268. }
  1269. }
  1270. if (dirp && info.nmi_v3) {
  1271. diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
  1272. vrele(dirp);
  1273. }
  1274. nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
  1275. if (info.nmi_v3) {
  1276. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1277. &info);
  1278. return (0);
  1279. }
  1280. nfsmout:
  1281. return(error);
  1282. }
  1283. /*
  1284. * nfs rename service
  1285. */
  1286. int
  1287. nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1288. struct proc *procp, struct mbuf **mrq)
  1289. {
  1290. struct mbuf *nam = nfsd->nd_nam;
  1291. struct ucred *cred = &nfsd->nd_cr;
  1292. struct nfsm_info info;
  1293. u_int32_t *tl;
  1294. int32_t t1;
  1295. int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
  1296. int tdirfor_ret = 1, tdiraft_ret = 1;
  1297. char *cp2;
  1298. struct nameidata fromnd, tond;
  1299. struct vnode *fvp = NULL, *tvp, *tdvp, *fdirp = NULL;
  1300. struct vnode *tdirp = NULL;
  1301. struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
  1302. nfsfh_t fnfh, tnfh;
  1303. fhandle_t *ffhp, *tfhp;
  1304. uid_t saved_uid;
  1305. info.nmi_mreq = NULL;
  1306. info.nmi_mrep = nfsd->nd_mrep;
  1307. info.nmi_md = nfsd->nd_md;
  1308. info.nmi_dpos = nfsd->nd_dpos;
  1309. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1310. ffhp = &fnfh.fh_generic;
  1311. tfhp = &tnfh.fh_generic;
  1312. fromnd.ni_cnd.cn_nameiop = 0;
  1313. tond.ni_cnd.cn_nameiop = 0;
  1314. nfsm_srvmtofh(ffhp);
  1315. nfsm_srvnamesiz(len);
  1316. /*
  1317. * Remember our original uid so that we can reset cr_uid before
  1318. * the second nfs_namei() call, in case it is remapped.
  1319. */
  1320. saved_uid = cred->cr_uid;
  1321. fromnd.ni_cnd.cn_cred = cred;
  1322. fromnd.ni_cnd.cn_nameiop = DELETE;
  1323. fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
  1324. error = nfs_namei(&fromnd, ffhp, len, slp, nam, &info.nmi_md,
  1325. &info.nmi_dpos, &fdirp, procp);
  1326. if (fdirp) {
  1327. if (info.nmi_v3)
  1328. fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
  1329. procp);
  1330. else {
  1331. vrele(fdirp);
  1332. fdirp = NULL;
  1333. }
  1334. }
  1335. if (error) {
  1336. nfsm_reply(2 * NFSX_WCCDATA(info.nmi_v3));
  1337. nfsm_srvwcc(nfsd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft,
  1338. &info);
  1339. nfsm_srvwcc(nfsd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft,
  1340. &info);
  1341. if (fdirp)
  1342. vrele(fdirp);
  1343. return (0);
  1344. }
  1345. fvp = fromnd.ni_vp;
  1346. nfsm_srvmtofh(tfhp);
  1347. nfsm_strsiz(len2, NFS_MAXNAMLEN);
  1348. cred->cr_uid = saved_uid;
  1349. tond.ni_cnd.cn_cred = cred;
  1350. tond.ni_cnd.cn_nameiop = RENAME;
  1351. tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
  1352. error = nfs_namei(&tond, tfhp, len2, slp, nam, &info.nmi_md,
  1353. &info.nmi_dpos, &tdirp, procp);
  1354. if (tdirp) {
  1355. if (info.nmi_v3)
  1356. tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
  1357. procp);
  1358. else {
  1359. vrele(tdirp);
  1360. tdirp = NULL;
  1361. }
  1362. }
  1363. if (error) {
  1364. VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
  1365. vrele(fromnd.ni_dvp);
  1366. vrele(fvp);
  1367. goto out1;
  1368. }
  1369. tdvp = tond.ni_dvp;
  1370. tvp = tond.ni_vp;
  1371. if (tvp != NULL) {
  1372. if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
  1373. error = info.nmi_v3 ? EEXIST : EISDIR;
  1374. goto out;
  1375. } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
  1376. error = info.nmi_v3 ? EEXIST : ENOTDIR;
  1377. goto out;
  1378. }
  1379. if (tvp->v_type == VDIR && tvp->v_mountedhere) {
  1380. error = info.nmi_v3 ? EXDEV : ENOTEMPTY;
  1381. goto out;
  1382. }
  1383. }
  1384. if (fvp->v_type == VDIR && fvp->v_mountedhere) {
  1385. error = info.nmi_v3 ? EXDEV : ENOTEMPTY;
  1386. goto out;
  1387. }
  1388. if (fvp->v_mount != tdvp->v_mount) {
  1389. error = info.nmi_v3 ? EXDEV : ENOTEMPTY;
  1390. goto out;
  1391. }
  1392. if (fvp == tdvp)
  1393. error = info.nmi_v3 ? EINVAL : ENOTEMPTY;
  1394. /*
  1395. * If source is the same as the destination (that is the
  1396. * same vnode with the same name in the same directory),
  1397. * then there is nothing to do.
  1398. */
  1399. if (fvp == tvp && fromnd.ni_dvp == tdvp &&
  1400. fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
  1401. !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
  1402. fromnd.ni_cnd.cn_namelen))
  1403. error = -1;
  1404. out:
  1405. if (!error) {
  1406. error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
  1407. tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
  1408. } else {
  1409. VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
  1410. if (tdvp == tvp)
  1411. vrele(tdvp);
  1412. else
  1413. vput(tdvp);
  1414. if (tvp)
  1415. vput(tvp);
  1416. VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
  1417. vrele(fromnd.ni_dvp);
  1418. vrele(fvp);
  1419. if (error == -1)
  1420. error = 0;
  1421. }
  1422. vrele(tond.ni_startdir);
  1423. pool_put(&namei_pool, tond.ni_cnd.cn_pnbuf);
  1424. out1:
  1425. if (fdirp) {
  1426. fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp);
  1427. vrele(fdirp);
  1428. }
  1429. if (tdirp) {
  1430. tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp);
  1431. vrele(tdirp);
  1432. }
  1433. vrele(fromnd.ni_startdir);
  1434. pool_put(&namei_pool, fromnd.ni_cnd.cn_pnbuf);
  1435. nfsm_reply(2 * NFSX_WCCDATA(info.nmi_v3));
  1436. if (info.nmi_v3) {
  1437. nfsm_srvwcc(nfsd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft,
  1438. &info);
  1439. nfsm_srvwcc(nfsd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft,
  1440. &info);
  1441. }
  1442. return (0);
  1443. nfsmout:
  1444. if (fdirp)
  1445. vrele(fdirp);
  1446. if (tdirp)
  1447. vrele(tdirp);
  1448. if (tond.ni_cnd.cn_nameiop) {
  1449. vrele(tond.ni_startdir);
  1450. pool_put(&namei_pool, tond.ni_cnd.cn_pnbuf);
  1451. }
  1452. if (fromnd.ni_cnd.cn_nameiop) {
  1453. if (fromnd.ni_startdir)
  1454. vrele(fromnd.ni_startdir);
  1455. VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
  1456. /*
  1457. * XXX: Workaround the fact that fromnd.ni_dvp can point
  1458. * to the same vnode as fdirp.
  1459. */
  1460. if (fromnd.ni_dvp != NULL && fromnd.ni_dvp != fdirp)
  1461. vrele(fromnd.ni_dvp);
  1462. if (fvp)
  1463. vrele(fvp);
  1464. }
  1465. return (error);
  1466. }
  1467. /*
  1468. * nfs link service
  1469. */
  1470. int
  1471. nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1472. struct proc *procp, struct mbuf **mrq)
  1473. {
  1474. struct mbuf *nam = nfsd->nd_nam;
  1475. struct nfsm_info info;
  1476. struct ucred *cred = &nfsd->nd_cr;
  1477. struct nameidata nd;
  1478. u_int32_t *tl;
  1479. int32_t t1;
  1480. int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
  1481. int getret = 1;
  1482. char *cp2;
  1483. struct vnode *vp, *xp, *dirp = NULL;
  1484. struct vattr dirfor, diraft, at;
  1485. nfsfh_t nfh, dnfh;
  1486. fhandle_t *fhp, *dfhp;
  1487. info.nmi_mreq = NULL;
  1488. info.nmi_mrep = nfsd->nd_mrep;
  1489. info.nmi_md = nfsd->nd_md;
  1490. info.nmi_dpos = nfsd->nd_dpos;
  1491. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1492. fhp = &nfh.fh_generic;
  1493. dfhp = &dnfh.fh_generic;
  1494. nfsm_srvmtofh(fhp);
  1495. nfsm_srvmtofh(dfhp);
  1496. nfsm_srvnamesiz(len);
  1497. error = nfsrv_fhtovp(fhp, 0, &vp, cred, slp, nam, &rdonly);
  1498. if (error) {
  1499. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) +
  1500. NFSX_WCCDATA(info.nmi_v3));
  1501. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  1502. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1503. &info);
  1504. error = 0;
  1505. goto nfsmout;
  1506. }
  1507. if (vp->v_type == VDIR && (error = suser_ucred(cred)) != 0)
  1508. goto out1;
  1509. nd.ni_cnd.cn_cred = cred;
  1510. nd.ni_cnd.cn_nameiop = CREATE;
  1511. nd.ni_cnd.cn_flags = LOCKPARENT;
  1512. error = nfs_namei(&nd, dfhp, len, slp, nam, &info.nmi_md,
  1513. &info.nmi_dpos, &dirp, procp);
  1514. if (dirp) {
  1515. if (info.nmi_v3)
  1516. dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
  1517. procp);
  1518. else {
  1519. vrele(dirp);
  1520. dirp = NULL;
  1521. }
  1522. }
  1523. if (error)
  1524. goto out1;
  1525. xp = nd.ni_vp;
  1526. if (xp != NULL) {
  1527. error = EEXIST;
  1528. goto out;
  1529. }
  1530. xp = nd.ni_dvp;
  1531. if (vp->v_mount != xp->v_mount)
  1532. error = EXDEV;
  1533. out:
  1534. if (!error) {
  1535. error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
  1536. } else {
  1537. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1538. if (nd.ni_dvp == nd.ni_vp)
  1539. vrele(nd.ni_dvp);
  1540. else
  1541. vput(nd.ni_dvp);
  1542. if (nd.ni_vp)
  1543. vrele(nd.ni_vp);
  1544. }
  1545. out1:
  1546. if (info.nmi_v3)
  1547. getret = VOP_GETATTR(vp, &at, cred, procp);
  1548. if (dirp) {
  1549. diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
  1550. vrele(dirp);
  1551. }
  1552. vrele(vp);
  1553. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_WCCDATA(info.nmi_v3));
  1554. if (info.nmi_v3) {
  1555. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  1556. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1557. &info);
  1558. error = 0;
  1559. }
  1560. nfsmout:
  1561. return(error);
  1562. }
  1563. /*
  1564. * nfs symbolic link service
  1565. */
  1566. int
  1567. nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1568. struct proc *procp, struct mbuf **mrq)
  1569. {
  1570. struct mbuf *nam = nfsd->nd_nam;
  1571. struct ucred *cred = &nfsd->nd_cr;
  1572. struct vattr va, dirfor, diraft;
  1573. struct nameidata nd;
  1574. struct nfsm_info info;
  1575. u_int32_t *tl;
  1576. int32_t t1;
  1577. struct nfsv2_sattr *sp;
  1578. char *pathcp = NULL, *cp2;
  1579. struct uio io;
  1580. struct iovec iv;
  1581. int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
  1582. struct vnode *dirp = NULL;
  1583. nfsfh_t nfh;
  1584. fhandle_t *fhp;
  1585. info.nmi_mreq = NULL;
  1586. info.nmi_mrep = nfsd->nd_mrep;
  1587. info.nmi_md = nfsd->nd_md;
  1588. info.nmi_dpos = nfsd->nd_dpos;
  1589. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1590. nd.ni_cnd.cn_nameiop = 0;
  1591. fhp = &nfh.fh_generic;
  1592. nfsm_srvmtofh(fhp);
  1593. nfsm_srvnamesiz(len);
  1594. nd.ni_cnd.cn_cred = cred;
  1595. nd.ni_cnd.cn_nameiop = CREATE;
  1596. nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
  1597. error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
  1598. &info.nmi_dpos, &dirp, procp);
  1599. if (dirp) {
  1600. if (info.nmi_v3)
  1601. dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
  1602. procp);
  1603. else {
  1604. vrele(dirp);
  1605. dirp = NULL;
  1606. }
  1607. }
  1608. if (error)
  1609. goto out;
  1610. VATTR_NULL(&va);
  1611. if (info.nmi_v3) {
  1612. error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep,
  1613. &info.nmi_dpos);
  1614. if (error)
  1615. goto nfsmout;
  1616. }
  1617. nfsm_strsiz(len2, NFS_MAXPATHLEN);
  1618. pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
  1619. iv.iov_base = pathcp;
  1620. iv.iov_len = len2;
  1621. io.uio_resid = len2;
  1622. io.uio_offset = 0;
  1623. io.uio_iov = &iv;
  1624. io.uio_iovcnt = 1;
  1625. io.uio_segflg = UIO_SYSSPACE;
  1626. io.uio_rw = UIO_READ;
  1627. io.uio_procp = NULL;
  1628. nfsm_mtouio(&io, len2);
  1629. if (!info.nmi_v3) {
  1630. nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
  1631. va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
  1632. }
  1633. *(pathcp + len2) = '\0';
  1634. if (nd.ni_vp) {
  1635. vrele(nd.ni_startdir);
  1636. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1637. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1638. if (nd.ni_dvp == nd.ni_vp)
  1639. vrele(nd.ni_dvp);
  1640. else
  1641. vput(nd.ni_dvp);
  1642. vrele(nd.ni_vp);
  1643. error = EEXIST;
  1644. goto out;
  1645. }
  1646. error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
  1647. if (error)
  1648. vrele(nd.ni_startdir);
  1649. else {
  1650. if (info.nmi_v3) {
  1651. nd.ni_cnd.cn_nameiop = LOOKUP;
  1652. nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART | FOLLOW);
  1653. nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
  1654. nd.ni_cnd.cn_proc = procp;
  1655. nd.ni_cnd.cn_cred = cred;
  1656. error = vfs_lookup(&nd);
  1657. if (!error) {
  1658. memset(fhp, 0, sizeof(nfh));
  1659. fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
  1660. error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
  1661. if (!error)
  1662. error = VOP_GETATTR(nd.ni_vp, &va, cred,
  1663. procp);
  1664. vput(nd.ni_vp);
  1665. }
  1666. } else
  1667. vrele(nd.ni_startdir);
  1668. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1669. }
  1670. out:
  1671. if (pathcp)
  1672. free(pathcp, M_TEMP, 0);
  1673. if (dirp) {
  1674. diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
  1675. vrele(dirp);
  1676. }
  1677. nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_POSTOPATTR(info.nmi_v3)
  1678. + NFSX_WCCDATA(info.nmi_v3));
  1679. if (info.nmi_v3) {
  1680. if (!error) {
  1681. nfsm_srvpostop_fh(fhp);
  1682. nfsm_srvpostop_attr(nfsd, 0, &va, &info);
  1683. }
  1684. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1685. &info);
  1686. }
  1687. return (0);
  1688. nfsmout:
  1689. if (nd.ni_cnd.cn_nameiop) {
  1690. vrele(nd.ni_startdir);
  1691. pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
  1692. }
  1693. if (dirp)
  1694. vrele(dirp);
  1695. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1696. if (nd.ni_dvp == nd.ni_vp)
  1697. vrele(nd.ni_dvp);
  1698. else
  1699. vput(nd.ni_dvp);
  1700. if (nd.ni_vp)
  1701. vrele(nd.ni_vp);
  1702. if (pathcp)
  1703. free(pathcp, M_TEMP, 0);
  1704. return (error);
  1705. }
  1706. /*
  1707. * nfs mkdir service
  1708. */
  1709. int
  1710. nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1711. struct proc *procp, struct mbuf **mrq)
  1712. {
  1713. struct mbuf *nam = nfsd->nd_nam;
  1714. struct ucred *cred = &nfsd->nd_cr;
  1715. struct vattr va, dirfor, diraft;
  1716. struct nfs_fattr *fp;
  1717. struct nameidata nd;
  1718. struct nfsm_info info;
  1719. u_int32_t *tl;
  1720. int32_t t1;
  1721. int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
  1722. char *cp2;
  1723. struct vnode *vp, *dirp = NULL;
  1724. nfsfh_t nfh;
  1725. fhandle_t *fhp;
  1726. info.nmi_mreq = NULL;
  1727. info.nmi_mrep = nfsd->nd_mrep;
  1728. info.nmi_md = nfsd->nd_md;
  1729. info.nmi_dpos = nfsd->nd_dpos;
  1730. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1731. fhp = &nfh.fh_generic;
  1732. nfsm_srvmtofh(fhp);
  1733. nfsm_srvnamesiz(len);
  1734. nd.ni_cnd.cn_cred = cred;
  1735. nd.ni_cnd.cn_nameiop = CREATE;
  1736. nd.ni_cnd.cn_flags = LOCKPARENT;
  1737. error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
  1738. &info.nmi_dpos, &dirp, procp);
  1739. if (dirp) {
  1740. if (info.nmi_v3)
  1741. dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
  1742. else {
  1743. vrele(dirp);
  1744. dirp = NULL;
  1745. }
  1746. }
  1747. if (error) {
  1748. nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
  1749. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1750. &info);
  1751. if (dirp)
  1752. vrele(dirp);
  1753. return (0);
  1754. }
  1755. VATTR_NULL(&va);
  1756. if (info.nmi_v3) {
  1757. error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep,
  1758. &info.nmi_dpos);
  1759. if (error)
  1760. goto nfsmout;
  1761. } else {
  1762. nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
  1763. va.va_mode = nfstov_mode(*tl++);
  1764. }
  1765. va.va_type = VDIR;
  1766. vp = nd.ni_vp;
  1767. if (vp != NULL) {
  1768. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1769. if (nd.ni_dvp == vp)
  1770. vrele(nd.ni_dvp);
  1771. else
  1772. vput(nd.ni_dvp);
  1773. vrele(vp);
  1774. error = EEXIST;
  1775. goto out;
  1776. }
  1777. error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
  1778. if (!error) {
  1779. vp = nd.ni_vp;
  1780. memset(fhp, 0, sizeof(nfh));
  1781. fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
  1782. error = VFS_VPTOFH(vp, &fhp->fh_fid);
  1783. if (!error)
  1784. error = VOP_GETATTR(vp, &va, cred, procp);
  1785. vput(vp);
  1786. }
  1787. out:
  1788. if (dirp) {
  1789. diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
  1790. vrele(dirp);
  1791. }
  1792. nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_POSTOPATTR(info.nmi_v3) +
  1793. NFSX_WCCDATA(info.nmi_v3));
  1794. if (info.nmi_v3) {
  1795. if (!error) {
  1796. nfsm_srvpostop_fh(fhp);
  1797. nfsm_srvpostop_attr(nfsd, 0, &va, &info);
  1798. }
  1799. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1800. &info);
  1801. } else {
  1802. nfsm_srvfhtom(&info.nmi_mb, fhp, info.nmi_v3);
  1803. fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
  1804. nfsm_srvfattr(nfsd, &va, fp);
  1805. }
  1806. return (0);
  1807. nfsmout:
  1808. if (dirp)
  1809. vrele(dirp);
  1810. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1811. if (nd.ni_dvp == nd.ni_vp)
  1812. vrele(nd.ni_dvp);
  1813. else
  1814. vput(nd.ni_dvp);
  1815. if (nd.ni_vp)
  1816. vrele(nd.ni_vp);
  1817. return (error);
  1818. }
  1819. /*
  1820. * nfs rmdir service
  1821. */
  1822. int
  1823. nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1824. struct proc *procp, struct mbuf **mrq)
  1825. {
  1826. struct mbuf *nam = nfsd->nd_nam;
  1827. struct ucred *cred = &nfsd->nd_cr;
  1828. struct nfsm_info info;
  1829. u_int32_t *tl;
  1830. int32_t t1;
  1831. int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
  1832. char *cp2;
  1833. struct vnode *vp, *dirp = NULL;
  1834. struct vattr dirfor, diraft;
  1835. nfsfh_t nfh;
  1836. fhandle_t *fhp;
  1837. struct nameidata nd;
  1838. info.nmi_mreq = NULL;
  1839. info.nmi_mrep = nfsd->nd_mrep;
  1840. info.nmi_md = nfsd->nd_md;
  1841. info.nmi_dpos = nfsd->nd_dpos;
  1842. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1843. fhp = &nfh.fh_generic;
  1844. nfsm_srvmtofh(fhp);
  1845. nfsm_srvnamesiz(len);
  1846. nd.ni_cnd.cn_cred = cred;
  1847. nd.ni_cnd.cn_nameiop = DELETE;
  1848. nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
  1849. error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
  1850. &info.nmi_dpos, &dirp, procp);
  1851. if (dirp) {
  1852. if (info.nmi_v3)
  1853. dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
  1854. procp);
  1855. else {
  1856. vrele(dirp);
  1857. dirp = NULL;
  1858. }
  1859. }
  1860. if (error) {
  1861. nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
  1862. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1863. &info);
  1864. if (dirp)
  1865. vrele(dirp);
  1866. return (0);
  1867. }
  1868. vp = nd.ni_vp;
  1869. if (vp->v_type != VDIR) {
  1870. error = ENOTDIR;
  1871. goto out;
  1872. }
  1873. /*
  1874. * No rmdir "." please.
  1875. */
  1876. if (nd.ni_dvp == vp) {
  1877. error = EINVAL;
  1878. goto out;
  1879. }
  1880. /*
  1881. * The root of a mounted filesystem cannot be deleted.
  1882. */
  1883. if (vp->v_flag & VROOT)
  1884. error = EBUSY;
  1885. out:
  1886. if (!error) {
  1887. error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
  1888. } else {
  1889. VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
  1890. if (nd.ni_dvp == nd.ni_vp)
  1891. vrele(nd.ni_dvp);
  1892. else
  1893. vput(nd.ni_dvp);
  1894. vput(vp);
  1895. }
  1896. if (dirp) {
  1897. diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
  1898. vrele(dirp);
  1899. }
  1900. nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
  1901. if (info.nmi_v3) {
  1902. nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
  1903. &info);
  1904. error = 0;
  1905. }
  1906. nfsmout:
  1907. return(error);
  1908. }
  1909. /*
  1910. * nfs readdir service
  1911. * - mallocs what it thinks is enough to read
  1912. * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
  1913. * - calls VOP_READDIR()
  1914. * - loops around building the reply
  1915. * if the output generated exceeds count break out of loop
  1916. * - it only knows that it has encountered eof when the VOP_READDIR()
  1917. * reads nothing
  1918. * - as such one readdir rpc will return eof false although you are there
  1919. * and then the next will return eof
  1920. * - it trims out records with d_fileno == 0
  1921. * this doesn't matter for Unix clients, but they might confuse clients
  1922. * for other os'.
  1923. * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
  1924. * than requested, but this may not apply to all filesystems. For
  1925. * example, client NFS does not { although it is never remote mounted
  1926. * anyhow }
  1927. * The alternate call nfsrv_readdirplus() does lookups as well.
  1928. * PS: The NFS protocol spec. does not clarify what the "count" byte
  1929. * argument is a count of.. just name strings and file id's or the
  1930. * entire reply rpc or ...
  1931. * I tried just file name and id sizes and it confused the Sun client,
  1932. * so I am using the full rpc size now. The "paranoia.." comment refers
  1933. * to including the status longwords that are not a part of the dir.
  1934. * "entry" structures, but are in the rpc.
  1935. */
  1936. struct flrep {
  1937. nfsuint64 fl_off;
  1938. u_int32_t fl_postopok;
  1939. u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
  1940. u_int32_t fl_fhok;
  1941. u_int32_t fl_fhsize;
  1942. u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
  1943. };
  1944. int
  1945. nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  1946. struct proc *procp, struct mbuf **mrq)
  1947. {
  1948. struct mbuf *nam = nfsd->nd_nam;
  1949. struct ucred *cred = &nfsd->nd_cr;
  1950. struct dirent *dp;
  1951. struct nfsm_info info;
  1952. u_int32_t *tl;
  1953. int32_t t1;
  1954. char *cpos, *cend, *cp2, *rbuf;
  1955. struct vnode *vp;
  1956. struct vattr at;
  1957. nfsfh_t nfh;
  1958. fhandle_t *fhp;
  1959. struct uio io;
  1960. struct iovec iv;
  1961. int len, nlen, pad, xfer, error = 0, getret = 1;
  1962. int siz, cnt, fullsiz, eofflag, rdonly;
  1963. u_quad_t off, toff, verf;
  1964. info.nmi_mreq = NULL;
  1965. info.nmi_mrep = nfsd->nd_mrep;
  1966. info.nmi_md = nfsd->nd_md;
  1967. info.nmi_dpos = nfsd->nd_dpos;
  1968. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  1969. fhp = &nfh.fh_generic;
  1970. nfsm_srvmtofh(fhp);
  1971. if (info.nmi_v3) {
  1972. nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
  1973. toff = fxdr_hyper(tl);
  1974. tl += 2;
  1975. verf = fxdr_hyper(tl);
  1976. tl += 2;
  1977. } else {
  1978. nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
  1979. toff = fxdr_unsigned(u_quad_t, *tl++);
  1980. }
  1981. off = toff;
  1982. cnt = fxdr_unsigned(int, *tl);
  1983. siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
  1984. xfer = NFS_SRVMAXDATA(nfsd);
  1985. if (siz > xfer)
  1986. siz = xfer;
  1987. if (cnt > xfer)
  1988. cnt = xfer;
  1989. fullsiz = siz;
  1990. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  1991. if (error) {
  1992. nfsm_reply(NFSX_UNSIGNED);
  1993. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  1994. error = 0;
  1995. goto nfsmout;
  1996. }
  1997. if (info.nmi_v3)
  1998. error = getret = VOP_GETATTR(vp, &at, cred, procp);
  1999. if (!error)
  2000. error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
  2001. if (error) {
  2002. vput(vp);
  2003. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3));
  2004. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2005. error = 0;
  2006. goto nfsmout;
  2007. }
  2008. VOP_UNLOCK(vp, 0, procp);
  2009. rbuf = malloc(fullsiz, M_TEMP, M_WAITOK);
  2010. again:
  2011. iv.iov_base = rbuf;
  2012. iv.iov_len = fullsiz;
  2013. io.uio_iov = &iv;
  2014. io.uio_iovcnt = 1;
  2015. io.uio_offset = (off_t)off;
  2016. io.uio_resid = fullsiz;
  2017. io.uio_segflg = UIO_SYSSPACE;
  2018. io.uio_rw = UIO_READ;
  2019. io.uio_procp = NULL;
  2020. eofflag = 0;
  2021. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
  2022. error = VOP_READDIR(vp, &io, cred, &eofflag);
  2023. off = (off_t)io.uio_offset;
  2024. if (info.nmi_v3) {
  2025. getret = VOP_GETATTR(vp, &at, cred, procp);
  2026. if (!error)
  2027. error = getret;
  2028. }
  2029. VOP_UNLOCK(vp, 0, procp);
  2030. if (error) {
  2031. vrele(vp);
  2032. free(rbuf, M_TEMP, fullsiz);
  2033. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3));
  2034. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2035. error = 0;
  2036. goto nfsmout;
  2037. }
  2038. if (io.uio_resid) {
  2039. siz -= io.uio_resid;
  2040. /*
  2041. * If nothing read, return eof
  2042. * rpc reply
  2043. */
  2044. if (siz == 0) {
  2045. vrele(vp);
  2046. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_COOKIEVERF(info.nmi_v3) +
  2047. 2 * NFSX_UNSIGNED);
  2048. if (info.nmi_v3) {
  2049. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2050. tl = nfsm_build(&info.nmi_mb, 4 * NFSX_UNSIGNED);
  2051. txdr_hyper(at.va_filerev, tl);
  2052. tl += 2;
  2053. } else
  2054. tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
  2055. *tl++ = nfs_false;
  2056. *tl = nfs_true;
  2057. free(rbuf, M_TEMP, fullsiz);
  2058. error = 0;
  2059. goto nfsmout;
  2060. }
  2061. }
  2062. /*
  2063. * Check for degenerate cases of nothing useful read.
  2064. * If so go try again
  2065. */
  2066. cpos = rbuf;
  2067. cend = rbuf + siz;
  2068. dp = (struct dirent *)cpos;
  2069. while (cpos < cend && dp->d_fileno == 0) {
  2070. cpos += dp->d_reclen;
  2071. dp = (struct dirent *)cpos;
  2072. }
  2073. if (cpos >= cend) {
  2074. toff = off;
  2075. siz = fullsiz;
  2076. goto again;
  2077. }
  2078. len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
  2079. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_COOKIEVERF(info.nmi_v3) + siz);
  2080. if (info.nmi_v3) {
  2081. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2082. tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
  2083. txdr_hyper(at.va_filerev, tl);
  2084. }
  2085. /* Loop through the records and build reply */
  2086. while (cpos < cend) {
  2087. if (dp->d_fileno != 0) {
  2088. nlen = dp->d_namlen;
  2089. pad = nfsm_padlen(nlen);
  2090. len += (4 * NFSX_UNSIGNED + nlen + pad);
  2091. if (info.nmi_v3)
  2092. len += 2 * NFSX_UNSIGNED;
  2093. if (len > cnt) {
  2094. eofflag = 0;
  2095. break;
  2096. }
  2097. /*
  2098. * Build the directory record xdr from
  2099. * the dirent entry.
  2100. */
  2101. tl = nfsm_build(&info.nmi_mb,
  2102. (info.nmi_v3 ? 3 : 2) * NFSX_UNSIGNED);
  2103. *tl++ = nfs_true;
  2104. if (info.nmi_v3)
  2105. txdr_hyper(dp->d_fileno, tl);
  2106. else
  2107. *tl = txdr_unsigned((u_int32_t)dp->d_fileno);
  2108. /* And copy the name */
  2109. nfsm_strtombuf(&info.nmi_mb, dp->d_name, nlen);
  2110. /* Finish off the record */
  2111. if (info.nmi_v3) {
  2112. tl = nfsm_build(&info.nmi_mb, 2*NFSX_UNSIGNED);
  2113. txdr_hyper(dp->d_off, tl);
  2114. } else {
  2115. tl = nfsm_build(&info.nmi_mb, NFSX_UNSIGNED);
  2116. *tl = txdr_unsigned((u_int32_t)dp->d_off);
  2117. }
  2118. }
  2119. cpos += dp->d_reclen;
  2120. dp = (struct dirent *)cpos;
  2121. }
  2122. vrele(vp);
  2123. tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
  2124. *tl++ = nfs_false;
  2125. if (eofflag)
  2126. *tl = nfs_true;
  2127. else
  2128. *tl = nfs_false;
  2129. free(rbuf, M_TEMP, fullsiz);
  2130. nfsmout:
  2131. return(error);
  2132. }
  2133. int
  2134. nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  2135. struct proc *procp, struct mbuf **mrq)
  2136. {
  2137. struct mbuf *nam = nfsd->nd_nam;
  2138. struct ucred *cred = &nfsd->nd_cr;
  2139. struct dirent *dp;
  2140. struct nfsm_info info;
  2141. u_int32_t *tl;
  2142. int32_t t1;
  2143. char *cpos, *cend, *cp2, *rbuf;
  2144. struct vnode *vp, *nvp;
  2145. struct flrep fl;
  2146. nfsfh_t nfh;
  2147. fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
  2148. struct uio io;
  2149. struct iovec iv;
  2150. struct vattr va, at, *vap = &va;
  2151. struct nfs_fattr *fp;
  2152. int len, nlen, pad, xfer, error = 0, getret = 1;
  2153. int siz, cnt, fullsiz, eofflag, rdonly, dirlen;
  2154. u_quad_t off, toff, verf;
  2155. info.nmi_mreq = NULL;
  2156. info.nmi_mrep = nfsd->nd_mrep;
  2157. info.nmi_md = nfsd->nd_md;
  2158. info.nmi_dpos = nfsd->nd_dpos;
  2159. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  2160. fhp = &nfh.fh_generic;
  2161. nfsm_srvmtofh(fhp);
  2162. nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
  2163. toff = fxdr_hyper(tl);
  2164. tl += 2;
  2165. verf = fxdr_hyper(tl);
  2166. tl += 2;
  2167. siz = fxdr_unsigned(int, *tl++);
  2168. cnt = fxdr_unsigned(int, *tl);
  2169. off = toff;
  2170. siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
  2171. xfer = NFS_SRVMAXDATA(nfsd);
  2172. if (siz > xfer)
  2173. siz = xfer;
  2174. if (cnt > xfer)
  2175. cnt = xfer;
  2176. fullsiz = siz;
  2177. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  2178. if (error) {
  2179. nfsm_reply(NFSX_UNSIGNED);
  2180. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2181. error = 0;
  2182. goto nfsmout;
  2183. }
  2184. error = getret = VOP_GETATTR(vp, &at, cred, procp);
  2185. if (!error)
  2186. error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
  2187. if (error) {
  2188. vput(vp);
  2189. nfsm_reply(NFSX_V3POSTOPATTR);
  2190. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2191. error = 0;
  2192. goto nfsmout;
  2193. }
  2194. VOP_UNLOCK(vp, 0, procp);
  2195. rbuf = malloc(fullsiz, M_TEMP, M_WAITOK);
  2196. again:
  2197. iv.iov_base = rbuf;
  2198. iv.iov_len = fullsiz;
  2199. io.uio_iov = &iv;
  2200. io.uio_iovcnt = 1;
  2201. io.uio_offset = (off_t)off;
  2202. io.uio_resid = fullsiz;
  2203. io.uio_segflg = UIO_SYSSPACE;
  2204. io.uio_rw = UIO_READ;
  2205. io.uio_procp = NULL;
  2206. eofflag = 0;
  2207. vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
  2208. error = VOP_READDIR(vp, &io, cred, &eofflag);
  2209. off = (u_quad_t)io.uio_offset;
  2210. getret = VOP_GETATTR(vp, &at, cred, procp);
  2211. VOP_UNLOCK(vp, 0, procp);
  2212. if (!error)
  2213. error = getret;
  2214. if (error) {
  2215. vrele(vp);
  2216. free(rbuf, M_TEMP, fullsiz);
  2217. nfsm_reply(NFSX_V3POSTOPATTR);
  2218. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2219. error = 0;
  2220. goto nfsmout;
  2221. }
  2222. if (io.uio_resid) {
  2223. siz -= io.uio_resid;
  2224. /*
  2225. * If nothing read, return eof
  2226. * rpc reply
  2227. */
  2228. if (siz == 0) {
  2229. vrele(vp);
  2230. nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
  2231. 2 * NFSX_UNSIGNED);
  2232. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2233. tl = nfsm_build(&info.nmi_mb, 4 * NFSX_UNSIGNED);
  2234. txdr_hyper(at.va_filerev, tl);
  2235. tl += 2;
  2236. *tl++ = nfs_false;
  2237. *tl = nfs_true;
  2238. free(rbuf, M_TEMP, fullsiz);
  2239. error = 0;
  2240. goto nfsmout;
  2241. }
  2242. }
  2243. /*
  2244. * Check for degenerate cases of nothing useful read.
  2245. * If so go try again
  2246. */
  2247. cpos = rbuf;
  2248. cend = rbuf + siz;
  2249. dp = (struct dirent *)cpos;
  2250. while (cpos < cend && dp->d_fileno == 0) {
  2251. cpos += dp->d_reclen;
  2252. dp = (struct dirent *)cpos;
  2253. }
  2254. if (cpos >= cend) {
  2255. toff = off;
  2256. siz = fullsiz;
  2257. goto again;
  2258. }
  2259. /*
  2260. * struct READDIRPLUS3resok {
  2261. * postop_attr dir_attributes;
  2262. * cookieverf3 cookieverf;
  2263. * dirlistplus3 reply;
  2264. * }
  2265. *
  2266. * struct dirlistplus3 {
  2267. * entryplus3 *entries;
  2268. * bool eof;
  2269. * }
  2270. */
  2271. dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
  2272. nfsm_reply(cnt);
  2273. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2274. tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
  2275. txdr_hyper(at.va_filerev, tl);
  2276. /* Loop through the records and build reply */
  2277. while (cpos < cend) {
  2278. if (dp->d_fileno != 0) {
  2279. nlen = dp->d_namlen;
  2280. pad = nfsm_padlen(nlen);
  2281. /*
  2282. * For readdir_and_lookup get the vnode using
  2283. * the file number.
  2284. */
  2285. if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
  2286. goto invalid;
  2287. memset(nfhp, 0, NFSX_V3FH);
  2288. nfhp->fh_fsid =
  2289. nvp->v_mount->mnt_stat.f_fsid;
  2290. if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
  2291. vput(nvp);
  2292. goto invalid;
  2293. }
  2294. if (VOP_GETATTR(nvp, vap, cred, procp)) {
  2295. vput(nvp);
  2296. goto invalid;
  2297. }
  2298. vput(nvp);
  2299. /*
  2300. * If either the dircount or maxcount will be
  2301. * exceeded, get out now. Both of these lengths
  2302. * are calculated conservatively, including all
  2303. * XDR overheads.
  2304. *
  2305. * Each entry:
  2306. * 2 * NFSX_UNSIGNED for fileid3
  2307. * 1 * NFSX_UNSIGNED for length of name
  2308. * nlen + pad == space the name takes up
  2309. * 2 * NFSX_UNSIGNED for the cookie
  2310. * 1 * NFSX_UNSIGNED to indicate if file handle present
  2311. * 1 * NFSX_UNSIGNED for the file handle length
  2312. * NFSX_V3FH == space our file handle takes up
  2313. * NFSX_V3POSTOPATTR == space the attributes take up
  2314. * 1 * NFSX_UNSIGNED for next pointer
  2315. */
  2316. len += (8 * NFSX_UNSIGNED + nlen + pad + NFSX_V3FH +
  2317. NFSX_V3POSTOPATTR);
  2318. dirlen += (6 * NFSX_UNSIGNED + nlen + pad);
  2319. if (len > cnt || dirlen > fullsiz) {
  2320. eofflag = 0;
  2321. break;
  2322. }
  2323. tl = nfsm_build(&info.nmi_mb, 3 * NFSX_UNSIGNED);
  2324. *tl++ = nfs_true;
  2325. txdr_hyper(dp->d_fileno, tl);
  2326. /* And copy the name */
  2327. nfsm_strtombuf(&info.nmi_mb, dp->d_name, nlen);
  2328. /*
  2329. * Build the directory record xdr from
  2330. * the dirent entry.
  2331. */
  2332. fp = (struct nfs_fattr *)&fl.fl_fattr;
  2333. nfsm_srvfattr(nfsd, vap, fp);
  2334. fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
  2335. fl.fl_fhok = nfs_true;
  2336. fl.fl_postopok = nfs_true;
  2337. txdr_hyper(dp->d_off, fl.fl_off.nfsuquad);
  2338. /* Now copy the flrep structure out. */
  2339. nfsm_buftombuf(&info.nmi_mb, &fl, sizeof(struct flrep));
  2340. }
  2341. invalid:
  2342. cpos += dp->d_reclen;
  2343. dp = (struct dirent *)cpos;
  2344. }
  2345. vrele(vp);
  2346. tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
  2347. *tl++ = nfs_false;
  2348. if (eofflag)
  2349. *tl = nfs_true;
  2350. else
  2351. *tl = nfs_false;
  2352. free(rbuf, M_TEMP, fullsiz);
  2353. nfsmout:
  2354. return(error);
  2355. }
  2356. /*
  2357. * nfs commit service
  2358. */
  2359. int
  2360. nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  2361. struct proc *procp, struct mbuf **mrq)
  2362. {
  2363. struct mbuf *nam = nfsd->nd_nam;
  2364. struct ucred *cred = &nfsd->nd_cr;
  2365. struct vattr bfor, aft;
  2366. struct vnode *vp;
  2367. struct nfsm_info info;
  2368. nfsfh_t nfh;
  2369. fhandle_t *fhp;
  2370. u_int32_t *tl;
  2371. int32_t t1;
  2372. int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
  2373. char *cp2;
  2374. u_quad_t off;
  2375. info.nmi_mreq = NULL;
  2376. info.nmi_mrep = nfsd->nd_mrep;
  2377. info.nmi_md = nfsd->nd_md;
  2378. info.nmi_dpos = nfsd->nd_dpos;
  2379. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  2380. fhp = &nfh.fh_generic;
  2381. nfsm_srvmtofh(fhp);
  2382. nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
  2383. /*
  2384. * XXX At this time VOP_FSYNC() does not accept offset and byte
  2385. * count parameters, so these arguments are useless (someday maybe).
  2386. */
  2387. off = fxdr_hyper(tl);
  2388. tl += 2;
  2389. cnt = fxdr_unsigned(int, *tl);
  2390. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  2391. if (error) {
  2392. nfsm_reply(2 * NFSX_UNSIGNED);
  2393. nfsm_srvwcc(nfsd, for_ret, &bfor, aft_ret, &aft, &info);
  2394. error = 0;
  2395. goto nfsmout;
  2396. }
  2397. for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
  2398. error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
  2399. aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
  2400. vput(vp);
  2401. nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
  2402. nfsm_srvwcc(nfsd, for_ret, &bfor, aft_ret, &aft, &info);
  2403. if (!error) {
  2404. tl = nfsm_build(&info.nmi_mb, NFSX_V3WRITEVERF);
  2405. *tl++ = txdr_unsigned(boottime.tv_sec);
  2406. *tl = txdr_unsigned(boottime.tv_nsec/1000);
  2407. } else
  2408. error = 0;
  2409. nfsmout:
  2410. return(error);
  2411. }
  2412. /*
  2413. * nfs statfs service
  2414. */
  2415. int
  2416. nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  2417. struct proc *procp, struct mbuf **mrq)
  2418. {
  2419. struct mbuf *nam = nfsd->nd_nam;
  2420. struct ucred *cred = &nfsd->nd_cr;
  2421. struct statfs *sf;
  2422. struct nfs_statfs *sfp;
  2423. struct nfsm_info info;
  2424. u_int32_t *tl;
  2425. int32_t t1;
  2426. int error = 0, rdonly, getret = 1;
  2427. char *cp2;
  2428. struct vnode *vp;
  2429. struct vattr at;
  2430. nfsfh_t nfh;
  2431. fhandle_t *fhp;
  2432. struct statfs statfs;
  2433. u_quad_t tval;
  2434. info.nmi_mreq = NULL;
  2435. info.nmi_mrep = nfsd->nd_mrep;
  2436. info.nmi_md = nfsd->nd_md;
  2437. info.nmi_dpos = nfsd->nd_dpos;
  2438. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  2439. fhp = &nfh.fh_generic;
  2440. nfsm_srvmtofh(fhp);
  2441. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  2442. if (error) {
  2443. nfsm_reply(NFSX_UNSIGNED);
  2444. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2445. error = 0;
  2446. goto nfsmout;
  2447. }
  2448. sf = &statfs;
  2449. error = VFS_STATFS(vp->v_mount, sf, procp);
  2450. getret = VOP_GETATTR(vp, &at, cred, procp);
  2451. vput(vp);
  2452. nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_STATFS(info.nmi_v3));
  2453. if (info.nmi_v3)
  2454. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2455. if (error) {
  2456. error = 0;
  2457. goto nfsmout;
  2458. }
  2459. sfp = nfsm_build(&info.nmi_mb, NFSX_STATFS(info.nmi_v3));
  2460. if (info.nmi_v3) {
  2461. tval = (u_quad_t)sf->f_blocks;
  2462. tval *= (u_quad_t)sf->f_bsize;
  2463. txdr_hyper(tval, &sfp->sf_tbytes);
  2464. tval = (u_quad_t)sf->f_bfree;
  2465. tval *= (u_quad_t)sf->f_bsize;
  2466. txdr_hyper(tval, &sfp->sf_fbytes);
  2467. tval = (u_quad_t)sf->f_bavail;
  2468. tval *= (u_quad_t)sf->f_bsize;
  2469. txdr_hyper(tval, &sfp->sf_abytes);
  2470. tval = (u_quad_t)sf->f_files;
  2471. txdr_hyper(tval, &sfp->sf_tfiles);
  2472. tval = (u_quad_t)sf->f_ffree;
  2473. txdr_hyper(tval, &sfp->sf_ffiles);
  2474. txdr_hyper(tval, &sfp->sf_afiles);
  2475. sfp->sf_invarsec = 0;
  2476. } else {
  2477. sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
  2478. sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
  2479. sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
  2480. sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
  2481. sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
  2482. }
  2483. nfsmout:
  2484. return(error);
  2485. }
  2486. /*
  2487. * nfs fsinfo service
  2488. */
  2489. int
  2490. nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  2491. struct proc *procp, struct mbuf **mrq)
  2492. {
  2493. struct mbuf *nam = nfsd->nd_nam;
  2494. struct ucred *cred = &nfsd->nd_cr;
  2495. struct nfsm_info info;
  2496. u_int32_t *tl;
  2497. struct nfsv3_fsinfo *sip;
  2498. int32_t t1;
  2499. int error = 0, rdonly, getret = 1, pref;
  2500. char *cp2;
  2501. struct vnode *vp;
  2502. struct vattr at;
  2503. nfsfh_t nfh;
  2504. fhandle_t *fhp;
  2505. info.nmi_mreq = NULL;
  2506. info.nmi_mrep = nfsd->nd_mrep;
  2507. info.nmi_md = nfsd->nd_md;
  2508. info.nmi_dpos = nfsd->nd_dpos;
  2509. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  2510. fhp = &nfh.fh_generic;
  2511. nfsm_srvmtofh(fhp);
  2512. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  2513. if (error) {
  2514. nfsm_reply(NFSX_UNSIGNED);
  2515. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2516. error = 0;
  2517. goto nfsmout;
  2518. }
  2519. getret = VOP_GETATTR(vp, &at, cred, procp);
  2520. vput(vp);
  2521. nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
  2522. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2523. sip = nfsm_build(&info.nmi_mb, NFSX_V3FSINFO);
  2524. /*
  2525. * XXX
  2526. * There should be file system VFS OP(s) to get this information.
  2527. * For now, assume ufs.
  2528. */
  2529. if (slp->ns_so->so_type == SOCK_DGRAM)
  2530. pref = NFS_MAXDGRAMDATA;
  2531. else
  2532. pref = NFS_MAXDATA;
  2533. sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
  2534. sip->fs_rtpref = txdr_unsigned(pref);
  2535. sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
  2536. sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
  2537. sip->fs_wtpref = txdr_unsigned(pref);
  2538. sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
  2539. sip->fs_dtpref = txdr_unsigned(pref);
  2540. sip->fs_maxfilesize.nfsuquad[0] = 0xffffffff;
  2541. sip->fs_maxfilesize.nfsuquad[1] = 0xffffffff;
  2542. sip->fs_timedelta.nfsv3_sec = 0;
  2543. sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
  2544. sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
  2545. NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
  2546. NFSV3FSINFO_CANSETTIME);
  2547. nfsmout:
  2548. return(error);
  2549. }
  2550. /*
  2551. * nfs pathconf service
  2552. */
  2553. int
  2554. nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  2555. struct proc *procp, struct mbuf **mrq)
  2556. {
  2557. struct mbuf *nam = nfsd->nd_nam;
  2558. struct ucred *cred = &nfsd->nd_cr;
  2559. struct nfsm_info info;
  2560. u_int32_t *tl;
  2561. struct nfsv3_pathconf *pc;
  2562. int32_t t1;
  2563. int error = 0, rdonly, getret = 1;
  2564. register_t linkmax, namemax, chownres, notrunc;
  2565. char *cp2;
  2566. struct vnode *vp;
  2567. struct vattr at;
  2568. nfsfh_t nfh;
  2569. fhandle_t *fhp;
  2570. info.nmi_mreq = NULL;
  2571. info.nmi_mrep = nfsd->nd_mrep;
  2572. info.nmi_md = nfsd->nd_md;
  2573. info.nmi_dpos = nfsd->nd_dpos;
  2574. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  2575. fhp = &nfh.fh_generic;
  2576. nfsm_srvmtofh(fhp);
  2577. error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
  2578. if (error) {
  2579. nfsm_reply(NFSX_UNSIGNED);
  2580. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2581. error = 0;
  2582. goto nfsmout;
  2583. }
  2584. error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
  2585. if (!error)
  2586. error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
  2587. if (!error)
  2588. error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
  2589. if (!error)
  2590. error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
  2591. getret = VOP_GETATTR(vp, &at, cred, procp);
  2592. vput(vp);
  2593. nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
  2594. nfsm_srvpostop_attr(nfsd, getret, &at, &info);
  2595. if (error) {
  2596. error = 0;
  2597. goto nfsmout;
  2598. }
  2599. pc = nfsm_build(&info.nmi_mb, NFSX_V3PATHCONF);
  2600. pc->pc_linkmax = txdr_unsigned(linkmax);
  2601. pc->pc_namemax = txdr_unsigned(namemax);
  2602. pc->pc_notrunc = txdr_unsigned(notrunc);
  2603. pc->pc_chownrestricted = txdr_unsigned(chownres);
  2604. /*
  2605. * These should probably be supported by VOP_PATHCONF(), but
  2606. * until msdosfs is exportable (why would you want to?), the
  2607. * Unix defaults should be ok.
  2608. */
  2609. pc->pc_caseinsensitive = nfs_false;
  2610. pc->pc_casepreserving = nfs_true;
  2611. nfsmout:
  2612. return(error);
  2613. }
  2614. /*
  2615. * Null operation, used by clients to ping server
  2616. */
  2617. /* ARGSUSED */
  2618. int
  2619. nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  2620. struct proc *procp, struct mbuf **mrq)
  2621. {
  2622. struct nfsm_info info;
  2623. int error = NFSERR_RETVOID;
  2624. info.nmi_mreq = NULL;
  2625. info.nmi_mrep = nfsd->nd_mrep;
  2626. info.nmi_md = nfsd->nd_md;
  2627. info.nmi_dpos = nfsd->nd_dpos;
  2628. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  2629. nfsm_reply(0);
  2630. return (0);
  2631. }
  2632. /*
  2633. * No operation, used for obsolete procedures
  2634. */
  2635. /* ARGSUSED */
  2636. int
  2637. nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
  2638. struct proc *procp, struct mbuf **mrq)
  2639. {
  2640. struct nfsm_info info;
  2641. int error;
  2642. info.nmi_mreq = NULL;
  2643. info.nmi_mrep = nfsd->nd_mrep;
  2644. info.nmi_md = nfsd->nd_md;
  2645. info.nmi_dpos = nfsd->nd_dpos;
  2646. info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
  2647. if (nfsd->nd_repstat)
  2648. error = nfsd->nd_repstat;
  2649. else
  2650. error = EPROCUNAVAIL;
  2651. nfsm_reply(0);
  2652. return (0);
  2653. }
  2654. /*
  2655. * Perform access checking for vnodes obtained from file handles that would
  2656. * refer to files already opened by a Unix client.
  2657. * You cannot just use vn_writechk() and VOP_ACCESS() for two reasons:
  2658. * 1 - You must check for exported rdonly as well as MNT_RDONLY for the
  2659. * write case
  2660. * 2 - The owner is to be given access irrespective of mode bits for some
  2661. * operations, so that processes that chmod after opening a file don't
  2662. * break. I don't like this because it opens a security hole, but since
  2663. * the nfs server opens a security hole the size of a barn door anyhow,
  2664. * what the heck. A notable exception to this rule is when VOP_ACCESS()
  2665. * returns EPERM (e.g. when a file is immutable) which is always an
  2666. * error.
  2667. */
  2668. int
  2669. nfsrv_access(struct vnode *vp, int flags, struct ucred *cred, int rdonly,
  2670. struct proc *p, int override)
  2671. {
  2672. struct vattr vattr;
  2673. int error;
  2674. if (flags & VWRITE) {
  2675. /* Just vn_writechk() changed to check rdonly */
  2676. /*
  2677. * Disallow write attempts on read-only file systems;
  2678. * unless the file is a socket or a block or character
  2679. * device resident on the file system.
  2680. */
  2681. if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
  2682. switch (vp->v_type) {
  2683. case VREG:
  2684. case VDIR:
  2685. case VLNK:
  2686. return (EROFS);
  2687. default:
  2688. break;
  2689. }
  2690. }
  2691. /*
  2692. * If there's shared text associated with
  2693. * the inode, try to free it up once. If
  2694. * we fail, we can't allow writing.
  2695. */
  2696. if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
  2697. return (ETXTBSY);
  2698. }
  2699. error = VOP_ACCESS(vp, flags, cred, p);
  2700. /*
  2701. * Allow certain operations for the owner (reads and writes
  2702. * on files that are already open).
  2703. */
  2704. if (override && error == EACCES &&
  2705. VOP_GETATTR(vp, &vattr, cred, p) == 0 &&
  2706. cred->cr_uid == vattr.va_uid)
  2707. error = 0;
  2708. return error;
  2709. }