12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852 |
- /* $OpenBSD: nfs_serv.c,v 1.105 2015/07/15 22:16:42 deraadt Exp $ */
- /* $NetBSD: nfs_serv.c,v 1.34 1997/05/12 23:37:12 fvdl Exp $ */
- /*
- * Copyright (c) 1989, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by
- * Rick Macklem at The University of Guelph.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * @(#)nfs_serv.c 8.7 (Berkeley) 5/14/95
- */
- /*
- * nfs version 2 and 3 server calls to vnode ops
- * - these routines generally have 3 phases
- * 1 - break down and validate rpc request in mbuf list
- * 2 - do the vnode ops for the request
- * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
- * 3 - build the rpc reply in an mbuf list
- * nb:
- * - do not mix the phases, since the nfsm_?? macros can return failures
- * on a bad rpc or similar and do not do any vrele() or vput()'s
- *
- * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
- * error number iff error != 0 whereas
- * returning an error from the server function implies a fatal error
- * such as a badly constructed rpc request that should be dropped without
- * a reply.
- * For Version 3, nfsm_reply() does not return for the error case, since
- * most version 3 rpcs return more than the status for error cases.
- */
- #include <sys/param.h>
- #include <sys/systm.h>
- #include <sys/proc.h>
- #include <sys/file.h>
- #include <sys/namei.h>
- #include <sys/vnode.h>
- #include <sys/lock.h>
- #include <sys/mount.h>
- #include <sys/socket.h>
- #include <sys/socketvar.h>
- #include <sys/mbuf.h>
- #include <sys/dirent.h>
- #include <sys/stat.h>
- #include <sys/kernel.h>
- #include <sys/pool.h>
- #include <sys/queue.h>
- #include <sys/unistd.h>
- #include <ufs/ufs/dir.h>
- #include <nfs/nfsproto.h>
- #include <nfs/nfs.h>
- #include <nfs/xdr_subs.h>
- #include <nfs/nfsm_subs.h>
- #include <nfs/nfs_var.h>
- /* Global vars */
- extern u_int32_t nfs_xdrneg1;
- extern u_int32_t nfs_false, nfs_true;
- extern enum vtype nv3tov_type[8];
- extern struct nfsstats nfsstats;
- extern nfstype nfsv2_type[9];
- extern nfstype nfsv3_type[9];
- /*
- * nfs v3 access service
- */
- int
- nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct nfsm_info info;
- struct ucred *cred = &nfsd->nd_cr;
- struct vnode *vp;
- nfsfh_t nfh;
- fhandle_t *fhp;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly, getret;
- char *cp2;
- struct vattr va;
- u_long testmode, nfsmode;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, 1, NULL, &info);
- error = 0;
- goto nfsmout;
- }
- nfsmode = fxdr_unsigned(u_int32_t, *tl);
- if ((nfsmode & NFSV3ACCESS_READ) &&
- nfsrv_access(vp, VREAD, cred, rdonly, procp, 0))
- nfsmode &= ~NFSV3ACCESS_READ;
- if (vp->v_type == VDIR)
- testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
- NFSV3ACCESS_DELETE);
- else
- testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
- if ((nfsmode & testmode) &&
- nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0))
- nfsmode &= ~testmode;
- if (vp->v_type == VDIR)
- testmode = NFSV3ACCESS_LOOKUP;
- else
- testmode = NFSV3ACCESS_EXECUTE;
- if ((nfsmode & testmode) &&
- nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0))
- nfsmode &= ~testmode;
- getret = VOP_GETATTR(vp, &va, cred, procp);
- vput(vp);
- nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, getret, &va, &info);
- tl = nfsm_build(&info.nmi_mb, NFSX_UNSIGNED);
- *tl = txdr_unsigned(nfsmode);
- nfsmout:
- return(error);
- }
- /*
- * nfs getattr service
- */
- int
- nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct nfsm_info info;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfs_fattr *fp;
- struct vattr va;
- struct vnode *vp;
- nfsfh_t nfh;
- fhandle_t *fhp;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly;
- char *cp2;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(0);
- error = 0;
- goto nfsmout;
- }
- error = VOP_GETATTR(vp, &va, cred, procp);
- vput(vp);
- nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
- if (error) {
- error = 0;
- goto nfsmout;
- }
- fp = nfsm_build(&info.nmi_mb, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
- nfsm_srvfattr(nfsd, &va, fp);
- nfsmout:
- return(error);
- }
- /*
- * nfs setattr service
- */
- int
- nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct nfsm_info info;
- struct ucred *cred = &nfsd->nd_cr;
- struct vattr va, preat;
- struct nfsv2_sattr *sp;
- struct nfs_fattr *fp;
- struct vnode *vp;
- nfsfh_t nfh;
- fhandle_t *fhp;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
- int gcheck = 0;
- char *cp2;
- struct timespec guard;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- VATTR_NULL(&va);
- if (info.nmi_v3) {
- va.va_vaflags |= VA_UTIMES_NULL;
- error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep, &info.nmi_dpos);
- if (error)
- goto nfsmout;
- nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
- gcheck = fxdr_unsigned(int, *tl);
- if (gcheck) {
- nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
- fxdr_nfsv3time(tl, &guard);
- }
- } else {
- nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
- /*
- * Nah nah nah nah na nah
- * There is a bug in the Sun client that puts 0xffff in the mode
- * field of sattr when it should put in 0xffffffff. The u_short
- * doesn't sign extend.
- * --> check the low order 2 bytes for 0xffff
- */
- if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
- va.va_mode = nfstov_mode(sp->sa_mode);
- if (sp->sa_uid != nfs_xdrneg1)
- va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
- if (sp->sa_gid != nfs_xdrneg1)
- va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
- if (sp->sa_size != nfs_xdrneg1)
- va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
- if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
- #ifdef notyet
- fxdr_nfsv2time(&sp->sa_atime, &va.va_atime);
- #else
- va.va_atime.tv_sec =
- fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
- va.va_atime.tv_nsec = 0;
- #endif
- }
- if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
- fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime);
- }
- /*
- * Now that we have all the fields, lets do it.
- */
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(2 * NFSX_UNSIGNED);
- nfsm_srvwcc(nfsd, preat_ret, &preat, postat_ret, &va, &info);
- error = 0;
- goto nfsmout;
- }
- if (info.nmi_v3) {
- error = preat_ret = VOP_GETATTR(vp, &preat, cred, procp);
- if (!error && gcheck &&
- (preat.va_ctime.tv_sec != guard.tv_sec ||
- preat.va_ctime.tv_nsec != guard.tv_nsec))
- error = NFSERR_NOT_SYNC;
- if (error) {
- vput(vp);
- nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
- nfsm_srvwcc(nfsd, preat_ret, &preat, postat_ret, &va,
- &info);
- error = 0;
- goto nfsmout;
- }
- }
- /*
- * If the size is being changed write acces is required, otherwise
- * just check for a read only file system.
- */
- if (va.va_size == ((u_quad_t)((quad_t) -1))) {
- if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
- error = EROFS;
- goto out;
- }
- } else {
- if (vp->v_type == VDIR) {
- error = EISDIR;
- goto out;
- } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
- procp, 1)) != 0)
- goto out;
- }
- error = VOP_SETATTR(vp, &va, cred, procp);
- postat_ret = VOP_GETATTR(vp, &va, cred, procp);
- if (!error)
- error = postat_ret;
- out:
- vput(vp);
- nfsm_reply(NFSX_WCCORFATTR(info.nmi_v3));
- if (info.nmi_v3) {
- nfsm_srvwcc(nfsd, preat_ret, &preat, postat_ret, &va,
- &info);
- error = 0;
- goto nfsmout;
- } else {
- fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
- nfsm_srvfattr(nfsd, &va, fp);
- }
- nfsmout:
- return(error);
- }
- /*
- * nfs lookup rpc
- */
- int
- nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfs_fattr *fp;
- struct nameidata nd;
- struct vnode *vp, *dirp;
- struct nfsm_info info;
- nfsfh_t nfh;
- fhandle_t *fhp;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, len, dirattr_ret = 1;
- int v3 = (nfsd->nd_flag & ND_NFSV3);
- char *cp2;
- struct vattr va, dirattr;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_mreq = NULL;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvnamesiz(len);
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = LOOKUP;
- nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
- error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md, &info.nmi_dpos, &dirp, procp);
- if (dirp) {
- if (info.nmi_v3)
- dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
- procp);
- vrele(dirp);
- }
- if (error) {
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3));
- nfsm_srvpostop_attr(nfsd, dirattr_ret, &dirattr, &info);
- return (0);
- }
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- vp = nd.ni_vp;
- memset(fhp, 0, sizeof(nfh));
- fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
- error = VFS_VPTOFH(vp, &fhp->fh_fid);
- if (!error)
- error = VOP_GETATTR(vp, &va, cred, procp);
- vput(vp);
- nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_POSTOPORFATTR(info.nmi_v3)
- + NFSX_POSTOPATTR(info.nmi_v3));
- if (error) {
- nfsm_srvpostop_attr(nfsd, dirattr_ret, &dirattr, &info);
- error = 0;
- goto nfsmout;
- }
- nfsm_srvfhtom(&info.nmi_mb, fhp, info.nmi_v3);
- if (v3) {
- nfsm_srvpostop_attr(nfsd, 0, &va, &info);
- nfsm_srvpostop_attr(nfsd, dirattr_ret, &dirattr, &info);
- } else {
- fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
- nfsm_srvfattr(nfsd, &va, fp);
- }
- nfsmout:
- return(error);
- }
- /*
- * nfs readlink service
- */
- int
- nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct iovec iov;
- struct mbuf *mp = NULL;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly, tlen, len = 0, getret;
- char *cp2;
- struct vnode *vp;
- struct vattr attr;
- nfsfh_t nfh;
- fhandle_t *fhp;
- struct uio uio;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- memset(&uio, 0, sizeof(uio));
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(2 * NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, 1, NULL, &info);
- error = 0;
- goto nfsmout;
- }
- if (vp->v_type != VLNK) {
- if (info.nmi_v3)
- error = EINVAL;
- else
- error = ENXIO;
- goto out;
- }
- MGET(mp, M_WAIT, MT_DATA);
- MCLGET(mp, M_WAIT); /* MLEN < NFS_MAXPATHLEN < MCLBYTES */
- mp->m_len = NFS_MAXPATHLEN;
- len = NFS_MAXPATHLEN;
- iov.iov_base = mtod(mp, caddr_t);
- iov.iov_len = mp->m_len;
- uio.uio_iov = &iov;
- uio.uio_iovcnt = 1;
- uio.uio_offset = 0;
- uio.uio_resid = NFS_MAXPATHLEN;
- uio.uio_rw = UIO_READ;
- uio.uio_segflg = UIO_SYSSPACE;
- uio.uio_procp = NULL;
- error = VOP_READLINK(vp, &uio, cred);
- out:
- getret = VOP_GETATTR(vp, &attr, cred, procp);
- vput(vp);
- if (error)
- m_freem(mp);
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_UNSIGNED);
- if (info.nmi_v3) {
- nfsm_srvpostop_attr(nfsd, getret, &attr, &info);
- if (error) {
- error = 0;
- goto nfsmout;
- }
- }
- if (uio.uio_resid > 0) {
- len -= uio.uio_resid;
- tlen = nfsm_rndup(len);
- nfsm_adj(mp, NFS_MAXPATHLEN-tlen, tlen-len);
- }
- tl = nfsm_build(&info.nmi_mb, NFSX_UNSIGNED);
- *tl = txdr_unsigned(len);
- info.nmi_mb->m_next = mp;
- nfsmout:
- return (error);
- }
- /*
- * nfs read service
- */
- int
- nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct mbuf *m;
- struct nfs_fattr *fp;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- int i, reqlen;
- int error = 0, rdonly, cnt, len, left, siz, tlen, getret = 1;
- char *cp2;
- struct mbuf *m2;
- struct vnode *vp;
- nfsfh_t nfh;
- fhandle_t *fhp;
- struct uio io, *uiop = &io;
- struct vattr va;
- off_t off;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- if (info.nmi_v3) {
- nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
- off = fxdr_hyper(tl);
- } else {
- nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
- off = (off_t)fxdr_unsigned(u_int32_t, *tl);
- }
- nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
- reqlen = fxdr_unsigned(int32_t, *tl);
- if (reqlen > (NFS_SRVMAXDATA(nfsd)) || reqlen <= 0) {
- error = EBADRPC;
- nfsm_reply(0);
- }
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error)
- goto bad;
- if (vp->v_type != VREG) {
- if (info.nmi_v3)
- error = EINVAL;
- else
- error = (vp->v_type == VDIR) ? EISDIR : EACCES;
- }
- if (!error) {
- if ((error = nfsrv_access(vp, VREAD, cred, rdonly, procp, 1)) != 0)
- error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 1);
- }
- getret = VOP_GETATTR(vp, &va, cred, procp);
- if (!error)
- error = getret;
- if (error)
- goto vbad;
- if (off >= va.va_size)
- cnt = 0;
- else if ((off + reqlen) > va.va_size)
- cnt = va.va_size - off;
- else
- cnt = reqlen;
- nfsm_reply(NFSX_POSTOPORFATTR(info.nmi_v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
- if (info.nmi_v3) {
- tl = nfsm_build(&info.nmi_mb, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
- *tl++ = nfs_true;
- fp = (struct nfs_fattr *)tl;
- tl += (NFSX_V3FATTR / sizeof (u_int32_t));
- } else {
- tl = nfsm_build(&info.nmi_mb, NFSX_V2FATTR + NFSX_UNSIGNED);
- fp = (struct nfs_fattr *)tl;
- tl += (NFSX_V2FATTR / sizeof (u_int32_t));
- }
- len = left = nfsm_rndup (cnt);
- if (cnt > 0) {
- struct iovec *iv, *iv2;
- size_t ivlen;
- /*
- * Generate the mbuf list with the uio_iov ref. to it.
- */
- i = 0;
- m = m2 = info.nmi_mb;
- while (left > 0) {
- siz = min(M_TRAILINGSPACE(m), left);
- if (siz > 0) {
- left -= siz;
- i++;
- }
- if (left > 0) {
- MGET(m, M_WAIT, MT_DATA);
- if (left >= MINCLSIZE)
- MCLGET(m, M_WAIT);
- m->m_len = 0;
- m2->m_next = m;
- m2 = m;
- }
- }
- iv = mallocarray(i, sizeof(*iv), M_TEMP, M_WAITOK);
- ivlen = i * sizeof(*iv);
- uiop->uio_iov = iv2 = iv;
- m = info.nmi_mb;
- left = len;
- i = 0;
- while (left > 0) {
- if (m == NULL)
- panic("nfsrv_read iov");
- siz = min(M_TRAILINGSPACE(m), left);
- if (siz > 0) {
- iv->iov_base = mtod(m, caddr_t) + m->m_len;
- iv->iov_len = siz;
- m->m_len += siz;
- left -= siz;
- iv++;
- i++;
- }
- m = m->m_next;
- }
- uiop->uio_iovcnt = i;
- uiop->uio_offset = off;
- uiop->uio_resid = len;
- uiop->uio_rw = UIO_READ;
- uiop->uio_segflg = UIO_SYSSPACE;
- error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
- off = uiop->uio_offset;
- free(iv2, M_TEMP, ivlen);
- if (error || (getret = VOP_GETATTR(vp, &va, cred, procp)) != 0){
- if (!error)
- error = getret;
- m_freem(info.nmi_mreq);
- goto vbad;
- }
- } else
- uiop->uio_resid = 0;
- vput(vp);
- nfsm_srvfattr(nfsd, &va, fp);
- tlen = len - uiop->uio_resid;
- cnt = cnt < tlen ? cnt : tlen;
- tlen = nfsm_rndup (cnt);
- if (len != tlen || tlen != cnt)
- nfsm_adj(info.nmi_mb, len - tlen, tlen - cnt);
- if (info.nmi_v3) {
- *tl++ = txdr_unsigned(cnt);
- if (len < reqlen)
- *tl++ = nfs_true;
- else
- *tl++ = nfs_false;
- }
- *tl = txdr_unsigned(cnt);
- nfsmout:
- return(error);
- vbad:
- vput(vp);
- bad:
- nfsm_reply(0);
- nfsm_srvpostop_attr(nfsd, getret, &va, &info);
- return (0);
- }
- /*
- * nfs write service
- */
- int
- nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfsm_info info;
- int i, cnt;
- struct mbuf *mp;
- struct nfs_fattr *fp;
- struct vattr va, forat;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly, len, forat_ret = 1;
- int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
- int stable = NFSV3WRITE_FILESYNC;
- char *cp2;
- struct vnode *vp;
- nfsfh_t nfh;
- fhandle_t *fhp;
- struct uio io, *uiop = &io;
- off_t off;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- if (info.nmi_mrep == NULL) {
- *mrq = NULL;
- return (0);
- }
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- if (info.nmi_v3) {
- nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
- off = fxdr_hyper(tl);
- tl += 3;
- stable = fxdr_unsigned(int, *tl++);
- } else {
- nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
- off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
- tl += 2;
- }
- retlen = len = fxdr_unsigned(int32_t, *tl);
- cnt = i = 0;
- /*
- * For NFS Version 2, it is not obvious what a write of zero length
- * should do, but I might as well be consistent with Version 3,
- * which is to return ok so long as there are no permission problems.
- */
- if (len > 0) {
- zeroing = 1;
- mp = info.nmi_mrep;
- while (mp) {
- if (mp == info.nmi_md) {
- zeroing = 0;
- adjust = info.nmi_dpos - mtod(mp, caddr_t);
- mp->m_len -= adjust;
- if (mp->m_len > 0 && adjust > 0)
- mp->m_data += adjust;
- }
- if (zeroing)
- mp->m_len = 0;
- else if (mp->m_len > 0) {
- i += mp->m_len;
- if (i > len) {
- mp->m_len -= (i - len);
- zeroing = 1;
- }
- if (mp->m_len > 0)
- cnt++;
- }
- mp = mp->m_next;
- }
- }
- if (len > NFS_MAXDATA || len < 0 || i < len) {
- error = EIO;
- goto bad;
- }
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error)
- goto bad;
- if (info.nmi_v3)
- forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
- if (vp->v_type != VREG) {
- if (info.nmi_v3)
- error = EINVAL;
- else
- error = (vp->v_type == VDIR) ? EISDIR : EACCES;
- goto vbad;
- }
- error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
- if (error)
- goto vbad;
- if (len > 0) {
- struct iovec *iv, *ivp;
- size_t ivlen;
- ivp = mallocarray(cnt, sizeof(*ivp), M_TEMP, M_WAITOK);
- ivlen = cnt * sizeof(*ivp);
- uiop->uio_iov = iv = ivp;
- uiop->uio_iovcnt = cnt;
- mp = info.nmi_mrep;
- while (mp) {
- if (mp->m_len > 0) {
- ivp->iov_base = mtod(mp, caddr_t);
- ivp->iov_len = mp->m_len;
- ivp++;
- }
- mp = mp->m_next;
- }
- if (stable == NFSV3WRITE_UNSTABLE)
- ioflags = IO_NODELOCKED;
- else if (stable == NFSV3WRITE_DATASYNC)
- ioflags = (IO_SYNC | IO_NODELOCKED);
- else
- ioflags = (IO_SYNC | IO_NODELOCKED);
- uiop->uio_resid = len;
- uiop->uio_rw = UIO_WRITE;
- uiop->uio_segflg = UIO_SYSSPACE;
- uiop->uio_procp = NULL;
- uiop->uio_offset = off;
- error = VOP_WRITE(vp, uiop, ioflags, cred);
- nfsstats.srvvop_writes++;
- free(iv, M_TEMP, ivlen);
- }
- aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
- vput(vp);
- if (!error)
- error = aftat_ret;
- nfsm_reply(NFSX_PREOPATTR(info.nmi_v3) + NFSX_POSTOPORFATTR(info.nmi_v3) +
- 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.nmi_v3));
- if (info.nmi_v3) {
- nfsm_srvwcc(nfsd, forat_ret, &forat, aftat_ret, &va, &info);
- if (error) {
- error = 0;
- goto nfsmout;
- }
- tl = nfsm_build(&info.nmi_mb, 4 * NFSX_UNSIGNED);
- *tl++ = txdr_unsigned(retlen);
- if (stable == NFSV3WRITE_UNSTABLE)
- *tl++ = txdr_unsigned(stable);
- else
- *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
- /*
- * Actually, there is no need to txdr these fields,
- * but it may make the values more human readable,
- * for debugging purposes.
- */
- *tl++ = txdr_unsigned(boottime.tv_sec);
- *tl = txdr_unsigned(boottime.tv_nsec/1000);
- } else {
- fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
- nfsm_srvfattr(nfsd, &va, fp);
- }
- nfsmout:
- return(error);
- vbad:
- vput(vp);
- bad:
- nfsm_reply(0);
- nfsm_srvwcc(nfsd, forat_ret, &forat, aftat_ret, &va, &info);
- return (0);
- }
- /*
- * nfs create service
- * now does a truncate to 0 length via. setattr if it already exists
- */
- int
- nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfs_fattr *fp;
- struct vattr va, dirfor, diraft;
- struct nfsv2_sattr *sp;
- struct nfsm_info info;
- u_int32_t *tl;
- struct nameidata nd;
- caddr_t cp;
- int32_t t1;
- int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
- dev_t rdev = 0;
- int how, exclusive_flag = 0;
- char *cp2;
- struct vnode *vp = NULL, *dirp = NULL;
- nfsfh_t nfh;
- fhandle_t *fhp;
- u_quad_t tempsize;
- u_char cverf[NFSX_V3CREATEVERF];
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvnamesiz(len);
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = CREATE;
- nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
- error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
- &info.nmi_dpos, &dirp, procp);
- if (dirp) {
- if (info.nmi_v3)
- dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
- else {
- vrele(dirp);
- dirp = NULL;
- }
- }
- if (error) {
- nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- if (dirp)
- vrele(dirp);
- return (0);
- }
- VATTR_NULL(&va);
- if (info.nmi_v3) {
- nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
- how = fxdr_unsigned(int, *tl);
- switch (how) {
- case NFSV3CREATE_GUARDED:
- if (nd.ni_vp) {
- error = EEXIST;
- break;
- }
- case NFSV3CREATE_UNCHECKED:
- error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep,
- &info.nmi_dpos);
- if (error)
- goto nfsmout;
- break;
- case NFSV3CREATE_EXCLUSIVE:
- nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
- bcopy(cp, cverf, NFSX_V3CREATEVERF);
- exclusive_flag = 1;
- if (nd.ni_vp == NULL)
- va.va_mode = 0;
- break;
- };
- va.va_type = VREG;
- } else {
- nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
- va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
- if (va.va_type == VNON)
- va.va_type = VREG;
- va.va_mode = nfstov_mode(sp->sa_mode);
- switch (va.va_type) {
- case VREG:
- tsize = fxdr_unsigned(int32_t, sp->sa_size);
- if (tsize != -1)
- va.va_size = (u_quad_t)tsize;
- break;
- case VCHR:
- case VBLK:
- case VFIFO:
- rdev = (dev_t)fxdr_unsigned(int32_t, sp->sa_size);
- break;
- default:
- break;
- };
- }
- /*
- * Iff doesn't exist, create it
- * otherwise just truncate to 0 length
- * should I set the mode too ??
- */
- if (nd.ni_vp == NULL) {
- if (va.va_type == VREG || va.va_type == VSOCK) {
- vrele(nd.ni_startdir);
- error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
- if (!error) {
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- if (exclusive_flag) {
- exclusive_flag = 0;
- VATTR_NULL(&va);
- bcopy(cverf, (caddr_t)&va.va_atime,
- NFSX_V3CREATEVERF);
- error = VOP_SETATTR(nd.ni_vp, &va, cred,
- procp);
- }
- }
- } else if (va.va_type == VCHR || va.va_type == VBLK ||
- va.va_type == VFIFO) {
- if (va.va_type == VCHR && rdev == 0xffffffff)
- va.va_type = VFIFO;
- if (va.va_type != VFIFO &&
- (error = suser_ucred(cred))) {
- vrele(nd.ni_startdir);
- if (nd.ni_cnd.cn_flags & HASBUF) {
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- nd.ni_cnd.cn_flags &= ~HASBUF;
- }
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- vput(nd.ni_dvp);
- nfsm_reply(0);
- error = 0;
- goto nfsmout;
- } else
- va.va_rdev = (dev_t)rdev;
- error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
- &va);
- if (error) {
- vrele(nd.ni_startdir);
- if (nd.ni_cnd.cn_flags & HASBUF) {
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- nd.ni_cnd.cn_flags &= ~HASBUF;
- }
- nfsm_reply(0);
- error = 0;
- goto nfsmout;
- }
- nd.ni_cnd.cn_nameiop = LOOKUP;
- nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
- nd.ni_cnd.cn_proc = procp;
- nd.ni_cnd.cn_cred = cred;
- if ((error = vfs_lookup(&nd)) != 0) {
- if (nd.ni_cnd.cn_flags & HASBUF) {
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- nd.ni_cnd.cn_flags &= ~HASBUF;
- }
- nfsm_reply(0);
- error = 0;
- goto nfsmout;
- }
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- if (nd.ni_cnd.cn_flags & ISSYMLINK) {
- vrele(nd.ni_dvp);
- vput(nd.ni_vp);
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- error = EINVAL;
- nfsm_reply(0);
- error = 0;
- goto nfsmout;
- }
- } else {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- nd.ni_cnd.cn_flags &= ~HASBUF;
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- vput(nd.ni_dvp);
- error = ENXIO;
- }
- vp = nd.ni_vp;
- } else {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- nd.ni_cnd.cn_flags &= ~HASBUF;
- vp = nd.ni_vp;
- if (nd.ni_dvp == vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (va.va_size != -1) {
- error = nfsrv_access(vp, VWRITE, cred,
- (nd.ni_cnd.cn_flags & RDONLY), procp, 0);
- if (!error) {
- tempsize = va.va_size;
- VATTR_NULL(&va);
- va.va_size = tempsize;
- error = VOP_SETATTR(vp, &va, cred,
- procp);
- }
- if (error)
- vput(vp);
- }
- }
- if (!error) {
- memset(fhp, 0, sizeof(nfh));
- fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
- error = VFS_VPTOFH(vp, &fhp->fh_fid);
- if (!error)
- error = VOP_GETATTR(vp, &va, cred, procp);
- vput(vp);
- }
- if (info.nmi_v3) {
- if (exclusive_flag && !error &&
- bcmp(cverf, (caddr_t)&va.va_atime, NFSX_V3CREATEVERF))
- error = EEXIST;
- diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
- vrele(dirp);
- }
- nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_FATTR(info.nmi_v3)
- + NFSX_WCCDATA(info.nmi_v3));
- if (info.nmi_v3) {
- if (!error) {
- nfsm_srvpostop_fh(fhp);
- nfsm_srvpostop_attr(nfsd, 0, &va, &info);
- }
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- } else {
- nfsm_srvfhtom(&info.nmi_mb, fhp, info.nmi_v3);
- fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
- nfsm_srvfattr(nfsd, &va, fp);
- }
- return (0);
- nfsmout:
- if (dirp)
- vrele(dirp);
- if (nd.ni_cnd.cn_nameiop != LOOKUP) {
- vrele(nd.ni_startdir);
- if (nd.ni_cnd.cn_flags & HASBUF) {
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- nd.ni_cnd.cn_flags &= ~HASBUF;
- }
- }
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == nd.ni_vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- if (nd.ni_vp)
- vput(nd.ni_vp);
- return (error);
- }
- /*
- * nfs v3 mknod service
- */
- int
- nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct vattr va, dirfor, diraft;
- struct nfsm_info info;
- u_int32_t *tl;
- struct nameidata nd;
- int32_t t1;
- int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
- u_int32_t major, minor;
- enum vtype vtyp;
- char *cp2;
- struct vnode *vp, *dirp = NULL;
- nfsfh_t nfh;
- fhandle_t *fhp;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- nd.ni_cnd.cn_nameiop = 0;
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvnamesiz(len);
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = CREATE;
- nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
- error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md, &info.nmi_dpos, &dirp, procp);
- if (dirp)
- dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
- if (error) {
- nfsm_reply(NFSX_WCCDATA(1));
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- if (dirp)
- vrele(dirp);
- return (0);
- }
- nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
- vtyp = nfsv3tov_type(*tl);
- if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- error = NFSERR_BADTYPE;
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- vput(nd.ni_dvp);
- goto out;
- }
- VATTR_NULL(&va);
- error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep, &info.nmi_dpos);
- if (error)
- goto nfsmout;
- if (vtyp == VCHR || vtyp == VBLK) {
- nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
- major = fxdr_unsigned(u_int32_t, *tl++);
- minor = fxdr_unsigned(u_int32_t, *tl);
- va.va_rdev = makedev(major, minor);
- }
- /*
- * Iff doesn't exist, create it.
- */
- if (nd.ni_vp) {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- error = EEXIST;
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- vput(nd.ni_dvp);
- goto out;
- }
- va.va_type = vtyp;
- if (vtyp == VSOCK) {
- vrele(nd.ni_startdir);
- error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
- if (!error)
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- } else {
- if (va.va_type != VFIFO &&
- (error = suser_ucred(cred))) {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- vput(nd.ni_dvp);
- goto out;
- }
- error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
- if (error) {
- vrele(nd.ni_startdir);
- goto out;
- }
- nd.ni_cnd.cn_nameiop = LOOKUP;
- nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
- nd.ni_cnd.cn_proc = procp;
- nd.ni_cnd.cn_cred = procp->p_ucred;
- error = vfs_lookup(&nd);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- if (error)
- goto out;
- if (nd.ni_cnd.cn_flags & ISSYMLINK) {
- vrele(nd.ni_dvp);
- vput(nd.ni_vp);
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- error = EINVAL;
- }
- }
- out:
- vp = nd.ni_vp;
- if (!error) {
- memset(fhp, 0, sizeof(nfh));
- fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
- error = VFS_VPTOFH(vp, &fhp->fh_fid);
- if (!error)
- error = VOP_GETATTR(vp, &va, cred, procp);
- vput(vp);
- }
- diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
- vrele(dirp);
- nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
- if (!error) {
- nfsm_srvpostop_fh(fhp);
- nfsm_srvpostop_attr(nfsd, 0, &va, &info);
- }
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft, &info);
- return (0);
- nfsmout:
- if (dirp)
- vrele(dirp);
- if (nd.ni_cnd.cn_nameiop) {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- }
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == nd.ni_vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- if (nd.ni_vp)
- vput(nd.ni_vp);
- return (error);
- }
- /*
- * nfs remove service
- */
- int
- nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nameidata nd;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
- char *cp2;
- struct vnode *vp, *dirp;
- struct vattr dirfor, diraft;
- nfsfh_t nfh;
- fhandle_t *fhp;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- vp = NULL;
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvnamesiz(len);
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = DELETE;
- nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
- error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md, &info.nmi_dpos, &dirp, procp);
- if (dirp) {
- if (info.nmi_v3)
- dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
- else {
- vrele(dirp);
- dirp = NULL;
- }
- }
- if (!error) {
- vp = nd.ni_vp;
- if (vp->v_type == VDIR &&
- (error = suser_ucred(cred)) != 0)
- goto out;
- /*
- * The root of a mounted filesystem cannot be deleted.
- */
- if (vp->v_flag & VROOT) {
- error = EBUSY;
- goto out;
- }
- if (vp->v_flag & VTEXT)
- uvm_vnp_uncache(vp);
- out:
- if (!error) {
- error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
- } else {
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- vput(vp);
- }
- }
- if (dirp && info.nmi_v3) {
- diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
- vrele(dirp);
- }
- nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
- if (info.nmi_v3) {
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- return (0);
- }
- nfsmout:
- return(error);
- }
- /*
- * nfs rename service
- */
- int
- nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
- int tdirfor_ret = 1, tdiraft_ret = 1;
- char *cp2;
- struct nameidata fromnd, tond;
- struct vnode *fvp = NULL, *tvp, *tdvp, *fdirp = NULL;
- struct vnode *tdirp = NULL;
- struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
- nfsfh_t fnfh, tnfh;
- fhandle_t *ffhp, *tfhp;
- uid_t saved_uid;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- ffhp = &fnfh.fh_generic;
- tfhp = &tnfh.fh_generic;
- fromnd.ni_cnd.cn_nameiop = 0;
- tond.ni_cnd.cn_nameiop = 0;
- nfsm_srvmtofh(ffhp);
- nfsm_srvnamesiz(len);
- /*
- * Remember our original uid so that we can reset cr_uid before
- * the second nfs_namei() call, in case it is remapped.
- */
- saved_uid = cred->cr_uid;
- fromnd.ni_cnd.cn_cred = cred;
- fromnd.ni_cnd.cn_nameiop = DELETE;
- fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
- error = nfs_namei(&fromnd, ffhp, len, slp, nam, &info.nmi_md,
- &info.nmi_dpos, &fdirp, procp);
- if (fdirp) {
- if (info.nmi_v3)
- fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
- procp);
- else {
- vrele(fdirp);
- fdirp = NULL;
- }
- }
- if (error) {
- nfsm_reply(2 * NFSX_WCCDATA(info.nmi_v3));
- nfsm_srvwcc(nfsd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft,
- &info);
- nfsm_srvwcc(nfsd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft,
- &info);
- if (fdirp)
- vrele(fdirp);
- return (0);
- }
- fvp = fromnd.ni_vp;
- nfsm_srvmtofh(tfhp);
- nfsm_strsiz(len2, NFS_MAXNAMLEN);
- cred->cr_uid = saved_uid;
- tond.ni_cnd.cn_cred = cred;
- tond.ni_cnd.cn_nameiop = RENAME;
- tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
- error = nfs_namei(&tond, tfhp, len2, slp, nam, &info.nmi_md,
- &info.nmi_dpos, &tdirp, procp);
- if (tdirp) {
- if (info.nmi_v3)
- tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
- procp);
- else {
- vrele(tdirp);
- tdirp = NULL;
- }
- }
- if (error) {
- VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
- vrele(fromnd.ni_dvp);
- vrele(fvp);
- goto out1;
- }
- tdvp = tond.ni_dvp;
- tvp = tond.ni_vp;
- if (tvp != NULL) {
- if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
- error = info.nmi_v3 ? EEXIST : EISDIR;
- goto out;
- } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
- error = info.nmi_v3 ? EEXIST : ENOTDIR;
- goto out;
- }
- if (tvp->v_type == VDIR && tvp->v_mountedhere) {
- error = info.nmi_v3 ? EXDEV : ENOTEMPTY;
- goto out;
- }
- }
- if (fvp->v_type == VDIR && fvp->v_mountedhere) {
- error = info.nmi_v3 ? EXDEV : ENOTEMPTY;
- goto out;
- }
- if (fvp->v_mount != tdvp->v_mount) {
- error = info.nmi_v3 ? EXDEV : ENOTEMPTY;
- goto out;
- }
- if (fvp == tdvp)
- error = info.nmi_v3 ? EINVAL : ENOTEMPTY;
- /*
- * If source is the same as the destination (that is the
- * same vnode with the same name in the same directory),
- * then there is nothing to do.
- */
- if (fvp == tvp && fromnd.ni_dvp == tdvp &&
- fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
- !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
- fromnd.ni_cnd.cn_namelen))
- error = -1;
- out:
- if (!error) {
- error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
- tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
- } else {
- VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
- if (tdvp == tvp)
- vrele(tdvp);
- else
- vput(tdvp);
- if (tvp)
- vput(tvp);
- VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
- vrele(fromnd.ni_dvp);
- vrele(fvp);
- if (error == -1)
- error = 0;
- }
- vrele(tond.ni_startdir);
- pool_put(&namei_pool, tond.ni_cnd.cn_pnbuf);
- out1:
- if (fdirp) {
- fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp);
- vrele(fdirp);
- }
- if (tdirp) {
- tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp);
- vrele(tdirp);
- }
- vrele(fromnd.ni_startdir);
- pool_put(&namei_pool, fromnd.ni_cnd.cn_pnbuf);
- nfsm_reply(2 * NFSX_WCCDATA(info.nmi_v3));
- if (info.nmi_v3) {
- nfsm_srvwcc(nfsd, fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft,
- &info);
- nfsm_srvwcc(nfsd, tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft,
- &info);
- }
- return (0);
- nfsmout:
- if (fdirp)
- vrele(fdirp);
- if (tdirp)
- vrele(tdirp);
- if (tond.ni_cnd.cn_nameiop) {
- vrele(tond.ni_startdir);
- pool_put(&namei_pool, tond.ni_cnd.cn_pnbuf);
- }
- if (fromnd.ni_cnd.cn_nameiop) {
- if (fromnd.ni_startdir)
- vrele(fromnd.ni_startdir);
- VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
- /*
- * XXX: Workaround the fact that fromnd.ni_dvp can point
- * to the same vnode as fdirp.
- */
- if (fromnd.ni_dvp != NULL && fromnd.ni_dvp != fdirp)
- vrele(fromnd.ni_dvp);
- if (fvp)
- vrele(fvp);
- }
- return (error);
- }
- /*
- * nfs link service
- */
- int
- nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct nfsm_info info;
- struct ucred *cred = &nfsd->nd_cr;
- struct nameidata nd;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
- int getret = 1;
- char *cp2;
- struct vnode *vp, *xp, *dirp = NULL;
- struct vattr dirfor, diraft, at;
- nfsfh_t nfh, dnfh;
- fhandle_t *fhp, *dfhp;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- dfhp = &dnfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvmtofh(dfhp);
- nfsm_srvnamesiz(len);
- error = nfsrv_fhtovp(fhp, 0, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) +
- NFSX_WCCDATA(info.nmi_v3));
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- error = 0;
- goto nfsmout;
- }
- if (vp->v_type == VDIR && (error = suser_ucred(cred)) != 0)
- goto out1;
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = CREATE;
- nd.ni_cnd.cn_flags = LOCKPARENT;
- error = nfs_namei(&nd, dfhp, len, slp, nam, &info.nmi_md,
- &info.nmi_dpos, &dirp, procp);
- if (dirp) {
- if (info.nmi_v3)
- dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
- procp);
- else {
- vrele(dirp);
- dirp = NULL;
- }
- }
- if (error)
- goto out1;
- xp = nd.ni_vp;
- if (xp != NULL) {
- error = EEXIST;
- goto out;
- }
- xp = nd.ni_dvp;
- if (vp->v_mount != xp->v_mount)
- error = EXDEV;
- out:
- if (!error) {
- error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
- } else {
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == nd.ni_vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- if (nd.ni_vp)
- vrele(nd.ni_vp);
- }
- out1:
- if (info.nmi_v3)
- getret = VOP_GETATTR(vp, &at, cred, procp);
- if (dirp) {
- diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
- vrele(dirp);
- }
- vrele(vp);
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_WCCDATA(info.nmi_v3));
- if (info.nmi_v3) {
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- error = 0;
- }
- nfsmout:
- return(error);
- }
- /*
- * nfs symbolic link service
- */
- int
- nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct vattr va, dirfor, diraft;
- struct nameidata nd;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- struct nfsv2_sattr *sp;
- char *pathcp = NULL, *cp2;
- struct uio io;
- struct iovec iv;
- int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
- struct vnode *dirp = NULL;
- nfsfh_t nfh;
- fhandle_t *fhp;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- nd.ni_cnd.cn_nameiop = 0;
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvnamesiz(len);
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = CREATE;
- nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
- error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
- &info.nmi_dpos, &dirp, procp);
- if (dirp) {
- if (info.nmi_v3)
- dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
- procp);
- else {
- vrele(dirp);
- dirp = NULL;
- }
- }
- if (error)
- goto out;
- VATTR_NULL(&va);
- if (info.nmi_v3) {
- error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep,
- &info.nmi_dpos);
- if (error)
- goto nfsmout;
- }
- nfsm_strsiz(len2, NFS_MAXPATHLEN);
- pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
- iv.iov_base = pathcp;
- iv.iov_len = len2;
- io.uio_resid = len2;
- io.uio_offset = 0;
- io.uio_iov = &iv;
- io.uio_iovcnt = 1;
- io.uio_segflg = UIO_SYSSPACE;
- io.uio_rw = UIO_READ;
- io.uio_procp = NULL;
- nfsm_mtouio(&io, len2);
- if (!info.nmi_v3) {
- nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
- va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
- }
- *(pathcp + len2) = '\0';
- if (nd.ni_vp) {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == nd.ni_vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- vrele(nd.ni_vp);
- error = EEXIST;
- goto out;
- }
- error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
- if (error)
- vrele(nd.ni_startdir);
- else {
- if (info.nmi_v3) {
- nd.ni_cnd.cn_nameiop = LOOKUP;
- nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART | FOLLOW);
- nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
- nd.ni_cnd.cn_proc = procp;
- nd.ni_cnd.cn_cred = cred;
- error = vfs_lookup(&nd);
- if (!error) {
- memset(fhp, 0, sizeof(nfh));
- fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
- error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
- if (!error)
- error = VOP_GETATTR(nd.ni_vp, &va, cred,
- procp);
- vput(nd.ni_vp);
- }
- } else
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- }
- out:
- if (pathcp)
- free(pathcp, M_TEMP, 0);
- if (dirp) {
- diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
- vrele(dirp);
- }
- nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_POSTOPATTR(info.nmi_v3)
- + NFSX_WCCDATA(info.nmi_v3));
- if (info.nmi_v3) {
- if (!error) {
- nfsm_srvpostop_fh(fhp);
- nfsm_srvpostop_attr(nfsd, 0, &va, &info);
- }
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- }
- return (0);
- nfsmout:
- if (nd.ni_cnd.cn_nameiop) {
- vrele(nd.ni_startdir);
- pool_put(&namei_pool, nd.ni_cnd.cn_pnbuf);
- }
- if (dirp)
- vrele(dirp);
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == nd.ni_vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- if (nd.ni_vp)
- vrele(nd.ni_vp);
- if (pathcp)
- free(pathcp, M_TEMP, 0);
- return (error);
- }
- /*
- * nfs mkdir service
- */
- int
- nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct vattr va, dirfor, diraft;
- struct nfs_fattr *fp;
- struct nameidata nd;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
- char *cp2;
- struct vnode *vp, *dirp = NULL;
- nfsfh_t nfh;
- fhandle_t *fhp;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvnamesiz(len);
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = CREATE;
- nd.ni_cnd.cn_flags = LOCKPARENT;
- error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
- &info.nmi_dpos, &dirp, procp);
- if (dirp) {
- if (info.nmi_v3)
- dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
- else {
- vrele(dirp);
- dirp = NULL;
- }
- }
- if (error) {
- nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- if (dirp)
- vrele(dirp);
- return (0);
- }
- VATTR_NULL(&va);
- if (info.nmi_v3) {
- error = nfsm_srvsattr(&info.nmi_md, &va, info.nmi_mrep,
- &info.nmi_dpos);
- if (error)
- goto nfsmout;
- } else {
- nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
- va.va_mode = nfstov_mode(*tl++);
- }
- va.va_type = VDIR;
- vp = nd.ni_vp;
- if (vp != NULL) {
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- vrele(vp);
- error = EEXIST;
- goto out;
- }
- error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
- if (!error) {
- vp = nd.ni_vp;
- memset(fhp, 0, sizeof(nfh));
- fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
- error = VFS_VPTOFH(vp, &fhp->fh_fid);
- if (!error)
- error = VOP_GETATTR(vp, &va, cred, procp);
- vput(vp);
- }
- out:
- if (dirp) {
- diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
- vrele(dirp);
- }
- nfsm_reply(NFSX_SRVFH(info.nmi_v3) + NFSX_POSTOPATTR(info.nmi_v3) +
- NFSX_WCCDATA(info.nmi_v3));
- if (info.nmi_v3) {
- if (!error) {
- nfsm_srvpostop_fh(fhp);
- nfsm_srvpostop_attr(nfsd, 0, &va, &info);
- }
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- } else {
- nfsm_srvfhtom(&info.nmi_mb, fhp, info.nmi_v3);
- fp = nfsm_build(&info.nmi_mb, NFSX_V2FATTR);
- nfsm_srvfattr(nfsd, &va, fp);
- }
- return (0);
- nfsmout:
- if (dirp)
- vrele(dirp);
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == nd.ni_vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- if (nd.ni_vp)
- vrele(nd.ni_vp);
- return (error);
- }
- /*
- * nfs rmdir service
- */
- int
- nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
- char *cp2;
- struct vnode *vp, *dirp = NULL;
- struct vattr dirfor, diraft;
- nfsfh_t nfh;
- fhandle_t *fhp;
- struct nameidata nd;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_srvnamesiz(len);
- nd.ni_cnd.cn_cred = cred;
- nd.ni_cnd.cn_nameiop = DELETE;
- nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
- error = nfs_namei(&nd, fhp, len, slp, nam, &info.nmi_md,
- &info.nmi_dpos, &dirp, procp);
- if (dirp) {
- if (info.nmi_v3)
- dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
- procp);
- else {
- vrele(dirp);
- dirp = NULL;
- }
- }
- if (error) {
- nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- if (dirp)
- vrele(dirp);
- return (0);
- }
- vp = nd.ni_vp;
- if (vp->v_type != VDIR) {
- error = ENOTDIR;
- goto out;
- }
- /*
- * No rmdir "." please.
- */
- if (nd.ni_dvp == vp) {
- error = EINVAL;
- goto out;
- }
- /*
- * The root of a mounted filesystem cannot be deleted.
- */
- if (vp->v_flag & VROOT)
- error = EBUSY;
- out:
- if (!error) {
- error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
- } else {
- VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
- if (nd.ni_dvp == nd.ni_vp)
- vrele(nd.ni_dvp);
- else
- vput(nd.ni_dvp);
- vput(vp);
- }
- if (dirp) {
- diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
- vrele(dirp);
- }
- nfsm_reply(NFSX_WCCDATA(info.nmi_v3));
- if (info.nmi_v3) {
- nfsm_srvwcc(nfsd, dirfor_ret, &dirfor, diraft_ret, &diraft,
- &info);
- error = 0;
- }
- nfsmout:
- return(error);
- }
- /*
- * nfs readdir service
- * - mallocs what it thinks is enough to read
- * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
- * - calls VOP_READDIR()
- * - loops around building the reply
- * if the output generated exceeds count break out of loop
- * - it only knows that it has encountered eof when the VOP_READDIR()
- * reads nothing
- * - as such one readdir rpc will return eof false although you are there
- * and then the next will return eof
- * - it trims out records with d_fileno == 0
- * this doesn't matter for Unix clients, but they might confuse clients
- * for other os'.
- * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
- * than requested, but this may not apply to all filesystems. For
- * example, client NFS does not { although it is never remote mounted
- * anyhow }
- * The alternate call nfsrv_readdirplus() does lookups as well.
- * PS: The NFS protocol spec. does not clarify what the "count" byte
- * argument is a count of.. just name strings and file id's or the
- * entire reply rpc or ...
- * I tried just file name and id sizes and it confused the Sun client,
- * so I am using the full rpc size now. The "paranoia.." comment refers
- * to including the status longwords that are not a part of the dir.
- * "entry" structures, but are in the rpc.
- */
- struct flrep {
- nfsuint64 fl_off;
- u_int32_t fl_postopok;
- u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
- u_int32_t fl_fhok;
- u_int32_t fl_fhsize;
- u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
- };
- int
- nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct dirent *dp;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- char *cpos, *cend, *cp2, *rbuf;
- struct vnode *vp;
- struct vattr at;
- nfsfh_t nfh;
- fhandle_t *fhp;
- struct uio io;
- struct iovec iv;
- int len, nlen, pad, xfer, error = 0, getret = 1;
- int siz, cnt, fullsiz, eofflag, rdonly;
- u_quad_t off, toff, verf;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- if (info.nmi_v3) {
- nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
- toff = fxdr_hyper(tl);
- tl += 2;
- verf = fxdr_hyper(tl);
- tl += 2;
- } else {
- nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
- toff = fxdr_unsigned(u_quad_t, *tl++);
- }
- off = toff;
- cnt = fxdr_unsigned(int, *tl);
- siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
- xfer = NFS_SRVMAXDATA(nfsd);
- if (siz > xfer)
- siz = xfer;
- if (cnt > xfer)
- cnt = xfer;
- fullsiz = siz;
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- if (info.nmi_v3)
- error = getret = VOP_GETATTR(vp, &at, cred, procp);
- if (!error)
- error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
- if (error) {
- vput(vp);
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3));
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- VOP_UNLOCK(vp, 0, procp);
- rbuf = malloc(fullsiz, M_TEMP, M_WAITOK);
- again:
- iv.iov_base = rbuf;
- iv.iov_len = fullsiz;
- io.uio_iov = &iv;
- io.uio_iovcnt = 1;
- io.uio_offset = (off_t)off;
- io.uio_resid = fullsiz;
- io.uio_segflg = UIO_SYSSPACE;
- io.uio_rw = UIO_READ;
- io.uio_procp = NULL;
- eofflag = 0;
- vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
- error = VOP_READDIR(vp, &io, cred, &eofflag);
- off = (off_t)io.uio_offset;
- if (info.nmi_v3) {
- getret = VOP_GETATTR(vp, &at, cred, procp);
- if (!error)
- error = getret;
- }
- VOP_UNLOCK(vp, 0, procp);
- if (error) {
- vrele(vp);
- free(rbuf, M_TEMP, fullsiz);
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3));
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- if (io.uio_resid) {
- siz -= io.uio_resid;
- /*
- * If nothing read, return eof
- * rpc reply
- */
- if (siz == 0) {
- vrele(vp);
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_COOKIEVERF(info.nmi_v3) +
- 2 * NFSX_UNSIGNED);
- if (info.nmi_v3) {
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- tl = nfsm_build(&info.nmi_mb, 4 * NFSX_UNSIGNED);
- txdr_hyper(at.va_filerev, tl);
- tl += 2;
- } else
- tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
- *tl++ = nfs_false;
- *tl = nfs_true;
- free(rbuf, M_TEMP, fullsiz);
- error = 0;
- goto nfsmout;
- }
- }
- /*
- * Check for degenerate cases of nothing useful read.
- * If so go try again
- */
- cpos = rbuf;
- cend = rbuf + siz;
- dp = (struct dirent *)cpos;
- while (cpos < cend && dp->d_fileno == 0) {
- cpos += dp->d_reclen;
- dp = (struct dirent *)cpos;
- }
- if (cpos >= cend) {
- toff = off;
- siz = fullsiz;
- goto again;
- }
- len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_COOKIEVERF(info.nmi_v3) + siz);
- if (info.nmi_v3) {
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
- txdr_hyper(at.va_filerev, tl);
- }
- /* Loop through the records and build reply */
- while (cpos < cend) {
- if (dp->d_fileno != 0) {
- nlen = dp->d_namlen;
- pad = nfsm_padlen(nlen);
- len += (4 * NFSX_UNSIGNED + nlen + pad);
- if (info.nmi_v3)
- len += 2 * NFSX_UNSIGNED;
- if (len > cnt) {
- eofflag = 0;
- break;
- }
- /*
- * Build the directory record xdr from
- * the dirent entry.
- */
- tl = nfsm_build(&info.nmi_mb,
- (info.nmi_v3 ? 3 : 2) * NFSX_UNSIGNED);
- *tl++ = nfs_true;
- if (info.nmi_v3)
- txdr_hyper(dp->d_fileno, tl);
- else
- *tl = txdr_unsigned((u_int32_t)dp->d_fileno);
-
- /* And copy the name */
- nfsm_strtombuf(&info.nmi_mb, dp->d_name, nlen);
-
- /* Finish off the record */
- if (info.nmi_v3) {
- tl = nfsm_build(&info.nmi_mb, 2*NFSX_UNSIGNED);
- txdr_hyper(dp->d_off, tl);
- } else {
- tl = nfsm_build(&info.nmi_mb, NFSX_UNSIGNED);
- *tl = txdr_unsigned((u_int32_t)dp->d_off);
- }
- }
- cpos += dp->d_reclen;
- dp = (struct dirent *)cpos;
- }
- vrele(vp);
- tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
- *tl++ = nfs_false;
- if (eofflag)
- *tl = nfs_true;
- else
- *tl = nfs_false;
- free(rbuf, M_TEMP, fullsiz);
- nfsmout:
- return(error);
- }
- int
- nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct dirent *dp;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- char *cpos, *cend, *cp2, *rbuf;
- struct vnode *vp, *nvp;
- struct flrep fl;
- nfsfh_t nfh;
- fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
- struct uio io;
- struct iovec iv;
- struct vattr va, at, *vap = &va;
- struct nfs_fattr *fp;
- int len, nlen, pad, xfer, error = 0, getret = 1;
- int siz, cnt, fullsiz, eofflag, rdonly, dirlen;
- u_quad_t off, toff, verf;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
- toff = fxdr_hyper(tl);
- tl += 2;
- verf = fxdr_hyper(tl);
- tl += 2;
- siz = fxdr_unsigned(int, *tl++);
- cnt = fxdr_unsigned(int, *tl);
- off = toff;
- siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
- xfer = NFS_SRVMAXDATA(nfsd);
- if (siz > xfer)
- siz = xfer;
- if (cnt > xfer)
- cnt = xfer;
- fullsiz = siz;
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- error = getret = VOP_GETATTR(vp, &at, cred, procp);
- if (!error)
- error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
- if (error) {
- vput(vp);
- nfsm_reply(NFSX_V3POSTOPATTR);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- VOP_UNLOCK(vp, 0, procp);
- rbuf = malloc(fullsiz, M_TEMP, M_WAITOK);
- again:
- iv.iov_base = rbuf;
- iv.iov_len = fullsiz;
- io.uio_iov = &iv;
- io.uio_iovcnt = 1;
- io.uio_offset = (off_t)off;
- io.uio_resid = fullsiz;
- io.uio_segflg = UIO_SYSSPACE;
- io.uio_rw = UIO_READ;
- io.uio_procp = NULL;
- eofflag = 0;
- vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp);
- error = VOP_READDIR(vp, &io, cred, &eofflag);
- off = (u_quad_t)io.uio_offset;
- getret = VOP_GETATTR(vp, &at, cred, procp);
- VOP_UNLOCK(vp, 0, procp);
- if (!error)
- error = getret;
- if (error) {
- vrele(vp);
- free(rbuf, M_TEMP, fullsiz);
- nfsm_reply(NFSX_V3POSTOPATTR);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- if (io.uio_resid) {
- siz -= io.uio_resid;
- /*
- * If nothing read, return eof
- * rpc reply
- */
- if (siz == 0) {
- vrele(vp);
- nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
- 2 * NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- tl = nfsm_build(&info.nmi_mb, 4 * NFSX_UNSIGNED);
- txdr_hyper(at.va_filerev, tl);
- tl += 2;
- *tl++ = nfs_false;
- *tl = nfs_true;
- free(rbuf, M_TEMP, fullsiz);
- error = 0;
- goto nfsmout;
- }
- }
- /*
- * Check for degenerate cases of nothing useful read.
- * If so go try again
- */
- cpos = rbuf;
- cend = rbuf + siz;
- dp = (struct dirent *)cpos;
- while (cpos < cend && dp->d_fileno == 0) {
- cpos += dp->d_reclen;
- dp = (struct dirent *)cpos;
- }
- if (cpos >= cend) {
- toff = off;
- siz = fullsiz;
- goto again;
- }
- /*
- * struct READDIRPLUS3resok {
- * postop_attr dir_attributes;
- * cookieverf3 cookieverf;
- * dirlistplus3 reply;
- * }
- *
- * struct dirlistplus3 {
- * entryplus3 *entries;
- * bool eof;
- * }
- */
- dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
- nfsm_reply(cnt);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
- txdr_hyper(at.va_filerev, tl);
- /* Loop through the records and build reply */
- while (cpos < cend) {
- if (dp->d_fileno != 0) {
- nlen = dp->d_namlen;
- pad = nfsm_padlen(nlen);
- /*
- * For readdir_and_lookup get the vnode using
- * the file number.
- */
- if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
- goto invalid;
- memset(nfhp, 0, NFSX_V3FH);
- nfhp->fh_fsid =
- nvp->v_mount->mnt_stat.f_fsid;
- if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
- vput(nvp);
- goto invalid;
- }
- if (VOP_GETATTR(nvp, vap, cred, procp)) {
- vput(nvp);
- goto invalid;
- }
- vput(nvp);
- /*
- * If either the dircount or maxcount will be
- * exceeded, get out now. Both of these lengths
- * are calculated conservatively, including all
- * XDR overheads.
- *
- * Each entry:
- * 2 * NFSX_UNSIGNED for fileid3
- * 1 * NFSX_UNSIGNED for length of name
- * nlen + pad == space the name takes up
- * 2 * NFSX_UNSIGNED for the cookie
- * 1 * NFSX_UNSIGNED to indicate if file handle present
- * 1 * NFSX_UNSIGNED for the file handle length
- * NFSX_V3FH == space our file handle takes up
- * NFSX_V3POSTOPATTR == space the attributes take up
- * 1 * NFSX_UNSIGNED for next pointer
- */
- len += (8 * NFSX_UNSIGNED + nlen + pad + NFSX_V3FH +
- NFSX_V3POSTOPATTR);
- dirlen += (6 * NFSX_UNSIGNED + nlen + pad);
- if (len > cnt || dirlen > fullsiz) {
- eofflag = 0;
- break;
- }
- tl = nfsm_build(&info.nmi_mb, 3 * NFSX_UNSIGNED);
- *tl++ = nfs_true;
- txdr_hyper(dp->d_fileno, tl);
- /* And copy the name */
- nfsm_strtombuf(&info.nmi_mb, dp->d_name, nlen);
- /*
- * Build the directory record xdr from
- * the dirent entry.
- */
- fp = (struct nfs_fattr *)&fl.fl_fattr;
- nfsm_srvfattr(nfsd, vap, fp);
- fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
- fl.fl_fhok = nfs_true;
- fl.fl_postopok = nfs_true;
- txdr_hyper(dp->d_off, fl.fl_off.nfsuquad);
- /* Now copy the flrep structure out. */
- nfsm_buftombuf(&info.nmi_mb, &fl, sizeof(struct flrep));
- }
- invalid:
- cpos += dp->d_reclen;
- dp = (struct dirent *)cpos;
- }
- vrele(vp);
- tl = nfsm_build(&info.nmi_mb, 2 * NFSX_UNSIGNED);
- *tl++ = nfs_false;
- if (eofflag)
- *tl = nfs_true;
- else
- *tl = nfs_false;
- free(rbuf, M_TEMP, fullsiz);
- nfsmout:
- return(error);
- }
- /*
- * nfs commit service
- */
- int
- nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct vattr bfor, aft;
- struct vnode *vp;
- struct nfsm_info info;
- nfsfh_t nfh;
- fhandle_t *fhp;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
- char *cp2;
- u_quad_t off;
-
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
- /*
- * XXX At this time VOP_FSYNC() does not accept offset and byte
- * count parameters, so these arguments are useless (someday maybe).
- */
- off = fxdr_hyper(tl);
- tl += 2;
- cnt = fxdr_unsigned(int, *tl);
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(2 * NFSX_UNSIGNED);
- nfsm_srvwcc(nfsd, for_ret, &bfor, aft_ret, &aft, &info);
- error = 0;
- goto nfsmout;
- }
- for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
- error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
- aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
- vput(vp);
- nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
- nfsm_srvwcc(nfsd, for_ret, &bfor, aft_ret, &aft, &info);
- if (!error) {
- tl = nfsm_build(&info.nmi_mb, NFSX_V3WRITEVERF);
- *tl++ = txdr_unsigned(boottime.tv_sec);
- *tl = txdr_unsigned(boottime.tv_nsec/1000);
- } else
- error = 0;
- nfsmout:
- return(error);
- }
- /*
- * nfs statfs service
- */
- int
- nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct statfs *sf;
- struct nfs_statfs *sfp;
- struct nfsm_info info;
- u_int32_t *tl;
- int32_t t1;
- int error = 0, rdonly, getret = 1;
- char *cp2;
- struct vnode *vp;
- struct vattr at;
- nfsfh_t nfh;
- fhandle_t *fhp;
- struct statfs statfs;
- u_quad_t tval;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- sf = &statfs;
- error = VFS_STATFS(vp->v_mount, sf, procp);
- getret = VOP_GETATTR(vp, &at, cred, procp);
- vput(vp);
- nfsm_reply(NFSX_POSTOPATTR(info.nmi_v3) + NFSX_STATFS(info.nmi_v3));
- if (info.nmi_v3)
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- if (error) {
- error = 0;
- goto nfsmout;
- }
- sfp = nfsm_build(&info.nmi_mb, NFSX_STATFS(info.nmi_v3));
- if (info.nmi_v3) {
- tval = (u_quad_t)sf->f_blocks;
- tval *= (u_quad_t)sf->f_bsize;
- txdr_hyper(tval, &sfp->sf_tbytes);
- tval = (u_quad_t)sf->f_bfree;
- tval *= (u_quad_t)sf->f_bsize;
- txdr_hyper(tval, &sfp->sf_fbytes);
- tval = (u_quad_t)sf->f_bavail;
- tval *= (u_quad_t)sf->f_bsize;
- txdr_hyper(tval, &sfp->sf_abytes);
- tval = (u_quad_t)sf->f_files;
- txdr_hyper(tval, &sfp->sf_tfiles);
- tval = (u_quad_t)sf->f_ffree;
- txdr_hyper(tval, &sfp->sf_ffiles);
- txdr_hyper(tval, &sfp->sf_afiles);
- sfp->sf_invarsec = 0;
- } else {
- sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
- sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
- sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
- sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
- sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
- }
- nfsmout:
- return(error);
- }
- /*
- * nfs fsinfo service
- */
- int
- nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfsm_info info;
- u_int32_t *tl;
- struct nfsv3_fsinfo *sip;
- int32_t t1;
- int error = 0, rdonly, getret = 1, pref;
- char *cp2;
- struct vnode *vp;
- struct vattr at;
- nfsfh_t nfh;
- fhandle_t *fhp;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- getret = VOP_GETATTR(vp, &at, cred, procp);
- vput(vp);
- nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- sip = nfsm_build(&info.nmi_mb, NFSX_V3FSINFO);
- /*
- * XXX
- * There should be file system VFS OP(s) to get this information.
- * For now, assume ufs.
- */
- if (slp->ns_so->so_type == SOCK_DGRAM)
- pref = NFS_MAXDGRAMDATA;
- else
- pref = NFS_MAXDATA;
- sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
- sip->fs_rtpref = txdr_unsigned(pref);
- sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
- sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
- sip->fs_wtpref = txdr_unsigned(pref);
- sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
- sip->fs_dtpref = txdr_unsigned(pref);
- sip->fs_maxfilesize.nfsuquad[0] = 0xffffffff;
- sip->fs_maxfilesize.nfsuquad[1] = 0xffffffff;
- sip->fs_timedelta.nfsv3_sec = 0;
- sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
- sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
- NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
- NFSV3FSINFO_CANSETTIME);
- nfsmout:
- return(error);
- }
- /*
- * nfs pathconf service
- */
- int
- nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct mbuf *nam = nfsd->nd_nam;
- struct ucred *cred = &nfsd->nd_cr;
- struct nfsm_info info;
- u_int32_t *tl;
- struct nfsv3_pathconf *pc;
- int32_t t1;
- int error = 0, rdonly, getret = 1;
- register_t linkmax, namemax, chownres, notrunc;
- char *cp2;
- struct vnode *vp;
- struct vattr at;
- nfsfh_t nfh;
- fhandle_t *fhp;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- fhp = &nfh.fh_generic;
- nfsm_srvmtofh(fhp);
- error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly);
- if (error) {
- nfsm_reply(NFSX_UNSIGNED);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- error = 0;
- goto nfsmout;
- }
- error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
- if (!error)
- error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
- if (!error)
- error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
- if (!error)
- error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
- getret = VOP_GETATTR(vp, &at, cred, procp);
- vput(vp);
- nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
- nfsm_srvpostop_attr(nfsd, getret, &at, &info);
- if (error) {
- error = 0;
- goto nfsmout;
- }
- pc = nfsm_build(&info.nmi_mb, NFSX_V3PATHCONF);
- pc->pc_linkmax = txdr_unsigned(linkmax);
- pc->pc_namemax = txdr_unsigned(namemax);
- pc->pc_notrunc = txdr_unsigned(notrunc);
- pc->pc_chownrestricted = txdr_unsigned(chownres);
- /*
- * These should probably be supported by VOP_PATHCONF(), but
- * until msdosfs is exportable (why would you want to?), the
- * Unix defaults should be ok.
- */
- pc->pc_caseinsensitive = nfs_false;
- pc->pc_casepreserving = nfs_true;
- nfsmout:
- return(error);
- }
- /*
- * Null operation, used by clients to ping server
- */
- /* ARGSUSED */
- int
- nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct nfsm_info info;
- int error = NFSERR_RETVOID;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- nfsm_reply(0);
- return (0);
- }
- /*
- * No operation, used for obsolete procedures
- */
- /* ARGSUSED */
- int
- nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
- struct proc *procp, struct mbuf **mrq)
- {
- struct nfsm_info info;
- int error;
- info.nmi_mreq = NULL;
- info.nmi_mrep = nfsd->nd_mrep;
- info.nmi_md = nfsd->nd_md;
- info.nmi_dpos = nfsd->nd_dpos;
- info.nmi_v3 = (nfsd->nd_flag & ND_NFSV3);
- if (nfsd->nd_repstat)
- error = nfsd->nd_repstat;
- else
- error = EPROCUNAVAIL;
- nfsm_reply(0);
- return (0);
- }
- /*
- * Perform access checking for vnodes obtained from file handles that would
- * refer to files already opened by a Unix client.
- * You cannot just use vn_writechk() and VOP_ACCESS() for two reasons:
- * 1 - You must check for exported rdonly as well as MNT_RDONLY for the
- * write case
- * 2 - The owner is to be given access irrespective of mode bits for some
- * operations, so that processes that chmod after opening a file don't
- * break. I don't like this because it opens a security hole, but since
- * the nfs server opens a security hole the size of a barn door anyhow,
- * what the heck. A notable exception to this rule is when VOP_ACCESS()
- * returns EPERM (e.g. when a file is immutable) which is always an
- * error.
- */
- int
- nfsrv_access(struct vnode *vp, int flags, struct ucred *cred, int rdonly,
- struct proc *p, int override)
- {
- struct vattr vattr;
- int error;
- if (flags & VWRITE) {
- /* Just vn_writechk() changed to check rdonly */
- /*
- * Disallow write attempts on read-only file systems;
- * unless the file is a socket or a block or character
- * device resident on the file system.
- */
- if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
- switch (vp->v_type) {
- case VREG:
- case VDIR:
- case VLNK:
- return (EROFS);
- default:
- break;
- }
- }
- /*
- * If there's shared text associated with
- * the inode, try to free it up once. If
- * we fail, we can't allow writing.
- */
- if ((vp->v_flag & VTEXT) && !uvm_vnp_uncache(vp))
- return (ETXTBSY);
- }
- error = VOP_ACCESS(vp, flags, cred, p);
- /*
- * Allow certain operations for the owner (reads and writes
- * on files that are already open).
- */
- if (override && error == EACCES &&
- VOP_GETATTR(vp, &vattr, cred, p) == 0 &&
- cred->cr_uid == vattr.va_uid)
- error = 0;
- return error;
- }
|