nfs4state.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <kandros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/swap.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sunrpc/svcauth_gss.h>
  42. #include <linux/sunrpc/addr.h>
  43. #include "xdr4.h"
  44. #include "xdr4cb.h"
  45. #include "vfs.h"
  46. #include "current_stateid.h"
  47. #include "netns.h"
  48. #define NFSDDBG_FACILITY NFSDDBG_PROC
  49. #define all_ones {{~0,~0},~0}
  50. static const stateid_t one_stateid = {
  51. .si_generation = ~0,
  52. .si_opaque = all_ones,
  53. };
  54. static const stateid_t zero_stateid = {
  55. /* all fields zero */
  56. };
  57. static const stateid_t currentstateid = {
  58. .si_generation = 1,
  59. };
  60. static u64 current_sessionid = 1;
  61. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  62. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  63. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  64. /* forward declarations */
  65. static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
  66. /* Locking: */
  67. /* Currently used for almost all code touching nfsv4 state: */
  68. static DEFINE_MUTEX(client_mutex);
  69. /*
  70. * Currently used for the del_recall_lru and file hash table. In an
  71. * effort to decrease the scope of the client_mutex, this spinlock may
  72. * eventually cover more:
  73. */
  74. static DEFINE_SPINLOCK(recall_lock);
  75. static struct kmem_cache *openowner_slab = NULL;
  76. static struct kmem_cache *lockowner_slab = NULL;
  77. static struct kmem_cache *file_slab = NULL;
  78. static struct kmem_cache *stateid_slab = NULL;
  79. static struct kmem_cache *deleg_slab = NULL;
  80. void
  81. nfs4_lock_state(void)
  82. {
  83. mutex_lock(&client_mutex);
  84. }
  85. static void free_session(struct nfsd4_session *);
  86. void nfsd4_put_session(struct nfsd4_session *ses)
  87. {
  88. atomic_dec(&ses->se_ref);
  89. }
  90. static bool is_session_dead(struct nfsd4_session *ses)
  91. {
  92. return ses->se_flags & NFS4_SESSION_DEAD;
  93. }
  94. static __be32 mark_session_dead_locked(struct nfsd4_session *ses)
  95. {
  96. if (atomic_read(&ses->se_ref))
  97. return nfserr_jukebox;
  98. ses->se_flags |= NFS4_SESSION_DEAD;
  99. return nfs_ok;
  100. }
  101. static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
  102. {
  103. if (is_session_dead(ses))
  104. return nfserr_badsession;
  105. atomic_inc(&ses->se_ref);
  106. return nfs_ok;
  107. }
  108. void
  109. nfs4_unlock_state(void)
  110. {
  111. mutex_unlock(&client_mutex);
  112. }
  113. static bool is_client_expired(struct nfs4_client *clp)
  114. {
  115. return clp->cl_time == 0;
  116. }
  117. static __be32 mark_client_expired_locked(struct nfs4_client *clp)
  118. {
  119. if (atomic_read(&clp->cl_refcount))
  120. return nfserr_jukebox;
  121. clp->cl_time = 0;
  122. return nfs_ok;
  123. }
  124. static __be32 mark_client_expired(struct nfs4_client *clp)
  125. {
  126. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  127. __be32 ret;
  128. spin_lock(&nn->client_lock);
  129. ret = mark_client_expired_locked(clp);
  130. spin_unlock(&nn->client_lock);
  131. return ret;
  132. }
  133. static __be32 get_client_locked(struct nfs4_client *clp)
  134. {
  135. if (is_client_expired(clp))
  136. return nfserr_expired;
  137. atomic_inc(&clp->cl_refcount);
  138. return nfs_ok;
  139. }
  140. /* must be called under the client_lock */
  141. static inline void
  142. renew_client_locked(struct nfs4_client *clp)
  143. {
  144. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  145. if (is_client_expired(clp)) {
  146. WARN_ON(1);
  147. printk("%s: client (clientid %08x/%08x) already expired\n",
  148. __func__,
  149. clp->cl_clientid.cl_boot,
  150. clp->cl_clientid.cl_id);
  151. return;
  152. }
  153. dprintk("renewing client (clientid %08x/%08x)\n",
  154. clp->cl_clientid.cl_boot,
  155. clp->cl_clientid.cl_id);
  156. list_move_tail(&clp->cl_lru, &nn->client_lru);
  157. clp->cl_time = get_seconds();
  158. }
  159. static inline void
  160. renew_client(struct nfs4_client *clp)
  161. {
  162. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  163. spin_lock(&nn->client_lock);
  164. renew_client_locked(clp);
  165. spin_unlock(&nn->client_lock);
  166. }
  167. static void put_client_renew_locked(struct nfs4_client *clp)
  168. {
  169. if (!atomic_dec_and_test(&clp->cl_refcount))
  170. return;
  171. if (!is_client_expired(clp))
  172. renew_client_locked(clp);
  173. }
  174. void put_client_renew(struct nfs4_client *clp)
  175. {
  176. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  177. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  178. return;
  179. if (!is_client_expired(clp))
  180. renew_client_locked(clp);
  181. spin_unlock(&nn->client_lock);
  182. }
  183. static inline u32
  184. opaque_hashval(const void *ptr, int nbytes)
  185. {
  186. unsigned char *cptr = (unsigned char *) ptr;
  187. u32 x = 0;
  188. while (nbytes--) {
  189. x *= 37;
  190. x += *cptr++;
  191. }
  192. return x;
  193. }
  194. static void nfsd4_free_file(struct nfs4_file *f)
  195. {
  196. kmem_cache_free(file_slab, f);
  197. }
  198. static inline void
  199. put_nfs4_file(struct nfs4_file *fi)
  200. {
  201. if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
  202. hlist_del(&fi->fi_hash);
  203. spin_unlock(&recall_lock);
  204. iput(fi->fi_inode);
  205. nfsd4_free_file(fi);
  206. }
  207. }
  208. static inline void
  209. get_nfs4_file(struct nfs4_file *fi)
  210. {
  211. atomic_inc(&fi->fi_ref);
  212. }
  213. static int num_delegations;
  214. unsigned long max_delegations;
  215. /*
  216. * Open owner state (share locks)
  217. */
  218. /* hash tables for lock and open owners */
  219. #define OWNER_HASH_BITS 8
  220. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  221. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  222. static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
  223. {
  224. unsigned int ret;
  225. ret = opaque_hashval(ownername->data, ownername->len);
  226. ret += clientid;
  227. return ret & OWNER_HASH_MASK;
  228. }
  229. /* hash table for nfs4_file */
  230. #define FILE_HASH_BITS 8
  231. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  232. static unsigned int file_hashval(struct inode *ino)
  233. {
  234. /* XXX: why are we hashing on inode pointer, anyway? */
  235. return hash_ptr(ino, FILE_HASH_BITS);
  236. }
  237. static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
  238. static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  239. {
  240. WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
  241. atomic_inc(&fp->fi_access[oflag]);
  242. }
  243. static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  244. {
  245. if (oflag == O_RDWR) {
  246. __nfs4_file_get_access(fp, O_RDONLY);
  247. __nfs4_file_get_access(fp, O_WRONLY);
  248. } else
  249. __nfs4_file_get_access(fp, oflag);
  250. }
  251. static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
  252. {
  253. if (fp->fi_fds[oflag]) {
  254. fput(fp->fi_fds[oflag]);
  255. fp->fi_fds[oflag] = NULL;
  256. }
  257. }
  258. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  259. {
  260. if (atomic_dec_and_test(&fp->fi_access[oflag])) {
  261. nfs4_file_put_fd(fp, oflag);
  262. if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
  263. nfs4_file_put_fd(fp, O_RDWR);
  264. }
  265. }
  266. static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  267. {
  268. if (oflag == O_RDWR) {
  269. __nfs4_file_put_access(fp, O_RDONLY);
  270. __nfs4_file_put_access(fp, O_WRONLY);
  271. } else
  272. __nfs4_file_put_access(fp, oflag);
  273. }
  274. static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
  275. kmem_cache *slab)
  276. {
  277. struct idr *stateids = &cl->cl_stateids;
  278. struct nfs4_stid *stid;
  279. int new_id;
  280. stid = kmem_cache_alloc(slab, GFP_KERNEL);
  281. if (!stid)
  282. return NULL;
  283. new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
  284. if (new_id < 0)
  285. goto out_free;
  286. stid->sc_client = cl;
  287. stid->sc_type = 0;
  288. stid->sc_stateid.si_opaque.so_id = new_id;
  289. stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
  290. /* Will be incremented before return to client: */
  291. stid->sc_stateid.si_generation = 0;
  292. /*
  293. * It shouldn't be a problem to reuse an opaque stateid value.
  294. * I don't think it is for 4.1. But with 4.0 I worry that, for
  295. * example, a stray write retransmission could be accepted by
  296. * the server when it should have been rejected. Therefore,
  297. * adopt a trick from the sctp code to attempt to maximize the
  298. * amount of time until an id is reused, by ensuring they always
  299. * "increase" (mod INT_MAX):
  300. */
  301. return stid;
  302. out_free:
  303. kmem_cache_free(slab, stid);
  304. return NULL;
  305. }
  306. static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
  307. {
  308. return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
  309. }
  310. static struct nfs4_delegation *
  311. alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
  312. {
  313. struct nfs4_delegation *dp;
  314. struct nfs4_file *fp = stp->st_file;
  315. dprintk("NFSD alloc_init_deleg\n");
  316. /*
  317. * Major work on the lease subsystem (for example, to support
  318. * calbacks on stat) will be required before we can support
  319. * write delegations properly.
  320. */
  321. if (type != NFS4_OPEN_DELEGATE_READ)
  322. return NULL;
  323. if (fp->fi_had_conflict)
  324. return NULL;
  325. if (num_delegations > max_delegations)
  326. return NULL;
  327. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
  328. if (dp == NULL)
  329. return dp;
  330. dp->dl_stid.sc_type = NFS4_DELEG_STID;
  331. /*
  332. * delegation seqid's are never incremented. The 4.1 special
  333. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  334. * 0 anyway just for consistency and use 1:
  335. */
  336. dp->dl_stid.sc_stateid.si_generation = 1;
  337. num_delegations++;
  338. INIT_LIST_HEAD(&dp->dl_perfile);
  339. INIT_LIST_HEAD(&dp->dl_perclnt);
  340. INIT_LIST_HEAD(&dp->dl_recall_lru);
  341. get_nfs4_file(fp);
  342. dp->dl_file = fp;
  343. dp->dl_type = type;
  344. fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
  345. dp->dl_time = 0;
  346. atomic_set(&dp->dl_count, 1);
  347. nfsd4_init_callback(&dp->dl_recall);
  348. return dp;
  349. }
  350. static void remove_stid(struct nfs4_stid *s)
  351. {
  352. struct idr *stateids = &s->sc_client->cl_stateids;
  353. idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
  354. }
  355. void
  356. nfs4_put_delegation(struct nfs4_delegation *dp)
  357. {
  358. if (atomic_dec_and_test(&dp->dl_count)) {
  359. kmem_cache_free(deleg_slab, dp);
  360. num_delegations--;
  361. }
  362. }
  363. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  364. {
  365. if (atomic_dec_and_test(&fp->fi_delegees)) {
  366. vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
  367. fp->fi_lease = NULL;
  368. fput(fp->fi_deleg_file);
  369. fp->fi_deleg_file = NULL;
  370. }
  371. }
  372. static void unhash_stid(struct nfs4_stid *s)
  373. {
  374. s->sc_type = 0;
  375. }
  376. /* Called under the state lock. */
  377. static void
  378. unhash_delegation(struct nfs4_delegation *dp)
  379. {
  380. list_del_init(&dp->dl_perclnt);
  381. spin_lock(&recall_lock);
  382. list_del_init(&dp->dl_perfile);
  383. list_del_init(&dp->dl_recall_lru);
  384. spin_unlock(&recall_lock);
  385. nfs4_put_deleg_lease(dp->dl_file);
  386. put_nfs4_file(dp->dl_file);
  387. dp->dl_file = NULL;
  388. }
  389. static void destroy_revoked_delegation(struct nfs4_delegation *dp)
  390. {
  391. list_del_init(&dp->dl_recall_lru);
  392. remove_stid(&dp->dl_stid);
  393. nfs4_put_delegation(dp);
  394. }
  395. static void destroy_delegation(struct nfs4_delegation *dp)
  396. {
  397. unhash_delegation(dp);
  398. remove_stid(&dp->dl_stid);
  399. nfs4_put_delegation(dp);
  400. }
  401. static void revoke_delegation(struct nfs4_delegation *dp)
  402. {
  403. struct nfs4_client *clp = dp->dl_stid.sc_client;
  404. if (clp->cl_minorversion == 0)
  405. destroy_delegation(dp);
  406. else {
  407. unhash_delegation(dp);
  408. dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
  409. list_add(&dp->dl_recall_lru, &clp->cl_revoked);
  410. }
  411. }
  412. /*
  413. * SETCLIENTID state
  414. */
  415. static unsigned int clientid_hashval(u32 id)
  416. {
  417. return id & CLIENT_HASH_MASK;
  418. }
  419. static unsigned int clientstr_hashval(const char *name)
  420. {
  421. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  422. }
  423. /*
  424. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  425. * st_{access,deny}_bmap field of the stateid, in order to track not
  426. * only what share bits are currently in force, but also what
  427. * combinations of share bits previous opens have used. This allows us
  428. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  429. * return an error if the client attempt to downgrade to a combination
  430. * of share bits not explicable by closing some of its previous opens.
  431. *
  432. * XXX: This enforcement is actually incomplete, since we don't keep
  433. * track of access/deny bit combinations; so, e.g., we allow:
  434. *
  435. * OPEN allow read, deny write
  436. * OPEN allow both, deny none
  437. * DOWNGRADE allow read, deny none
  438. *
  439. * which we should reject.
  440. */
  441. static unsigned int
  442. bmap_to_share_mode(unsigned long bmap) {
  443. int i;
  444. unsigned int access = 0;
  445. for (i = 1; i < 4; i++) {
  446. if (test_bit(i, &bmap))
  447. access |= i;
  448. }
  449. return access;
  450. }
  451. static bool
  452. test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
  453. unsigned int access, deny;
  454. access = bmap_to_share_mode(stp->st_access_bmap);
  455. deny = bmap_to_share_mode(stp->st_deny_bmap);
  456. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  457. return false;
  458. return true;
  459. }
  460. /* set share access for a given stateid */
  461. static inline void
  462. set_access(u32 access, struct nfs4_ol_stateid *stp)
  463. {
  464. __set_bit(access, &stp->st_access_bmap);
  465. }
  466. /* clear share access for a given stateid */
  467. static inline void
  468. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  469. {
  470. __clear_bit(access, &stp->st_access_bmap);
  471. }
  472. /* test whether a given stateid has access */
  473. static inline bool
  474. test_access(u32 access, struct nfs4_ol_stateid *stp)
  475. {
  476. return test_bit(access, &stp->st_access_bmap);
  477. }
  478. /* set share deny for a given stateid */
  479. static inline void
  480. set_deny(u32 access, struct nfs4_ol_stateid *stp)
  481. {
  482. __set_bit(access, &stp->st_deny_bmap);
  483. }
  484. /* clear share deny for a given stateid */
  485. static inline void
  486. clear_deny(u32 access, struct nfs4_ol_stateid *stp)
  487. {
  488. __clear_bit(access, &stp->st_deny_bmap);
  489. }
  490. /* test whether a given stateid is denying specific access */
  491. static inline bool
  492. test_deny(u32 access, struct nfs4_ol_stateid *stp)
  493. {
  494. return test_bit(access, &stp->st_deny_bmap);
  495. }
  496. static int nfs4_access_to_omode(u32 access)
  497. {
  498. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  499. case NFS4_SHARE_ACCESS_READ:
  500. return O_RDONLY;
  501. case NFS4_SHARE_ACCESS_WRITE:
  502. return O_WRONLY;
  503. case NFS4_SHARE_ACCESS_BOTH:
  504. return O_RDWR;
  505. }
  506. WARN_ON_ONCE(1);
  507. return O_RDONLY;
  508. }
  509. /* release all access and file references for a given stateid */
  510. static void
  511. release_all_access(struct nfs4_ol_stateid *stp)
  512. {
  513. int i;
  514. for (i = 1; i < 4; i++) {
  515. if (test_access(i, stp))
  516. nfs4_file_put_access(stp->st_file,
  517. nfs4_access_to_omode(i));
  518. clear_access(i, stp);
  519. }
  520. }
  521. static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
  522. {
  523. list_del(&stp->st_perfile);
  524. list_del(&stp->st_perstateowner);
  525. }
  526. static void close_generic_stateid(struct nfs4_ol_stateid *stp)
  527. {
  528. release_all_access(stp);
  529. put_nfs4_file(stp->st_file);
  530. stp->st_file = NULL;
  531. }
  532. static void free_generic_stateid(struct nfs4_ol_stateid *stp)
  533. {
  534. remove_stid(&stp->st_stid);
  535. kmem_cache_free(stateid_slab, stp);
  536. }
  537. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  538. {
  539. struct file *file;
  540. unhash_generic_stateid(stp);
  541. unhash_stid(&stp->st_stid);
  542. file = find_any_file(stp->st_file);
  543. if (file)
  544. locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
  545. close_generic_stateid(stp);
  546. free_generic_stateid(stp);
  547. }
  548. static void unhash_lockowner(struct nfs4_lockowner *lo)
  549. {
  550. struct nfs4_ol_stateid *stp;
  551. list_del(&lo->lo_owner.so_strhash);
  552. list_del(&lo->lo_perstateid);
  553. list_del(&lo->lo_owner_ino_hash);
  554. while (!list_empty(&lo->lo_owner.so_stateids)) {
  555. stp = list_first_entry(&lo->lo_owner.so_stateids,
  556. struct nfs4_ol_stateid, st_perstateowner);
  557. release_lock_stateid(stp);
  558. }
  559. }
  560. static void release_lockowner(struct nfs4_lockowner *lo)
  561. {
  562. unhash_lockowner(lo);
  563. nfs4_free_lockowner(lo);
  564. }
  565. static void
  566. release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
  567. {
  568. struct nfs4_lockowner *lo;
  569. while (!list_empty(&open_stp->st_lockowners)) {
  570. lo = list_entry(open_stp->st_lockowners.next,
  571. struct nfs4_lockowner, lo_perstateid);
  572. release_lockowner(lo);
  573. }
  574. }
  575. static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
  576. {
  577. unhash_generic_stateid(stp);
  578. release_stateid_lockowners(stp);
  579. close_generic_stateid(stp);
  580. }
  581. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  582. {
  583. unhash_open_stateid(stp);
  584. unhash_stid(&stp->st_stid);
  585. free_generic_stateid(stp);
  586. }
  587. static void unhash_openowner(struct nfs4_openowner *oo)
  588. {
  589. struct nfs4_ol_stateid *stp;
  590. list_del(&oo->oo_owner.so_strhash);
  591. list_del(&oo->oo_perclient);
  592. while (!list_empty(&oo->oo_owner.so_stateids)) {
  593. stp = list_first_entry(&oo->oo_owner.so_stateids,
  594. struct nfs4_ol_stateid, st_perstateowner);
  595. release_open_stateid(stp);
  596. }
  597. }
  598. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  599. {
  600. struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
  601. if (s) {
  602. unhash_stid(&s->st_stid);
  603. free_generic_stateid(s);
  604. oo->oo_last_closed_stid = NULL;
  605. }
  606. }
  607. static void release_openowner(struct nfs4_openowner *oo)
  608. {
  609. unhash_openowner(oo);
  610. list_del(&oo->oo_close_lru);
  611. release_last_closed_stateid(oo);
  612. nfs4_free_openowner(oo);
  613. }
  614. static inline int
  615. hash_sessionid(struct nfs4_sessionid *sessionid)
  616. {
  617. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  618. return sid->sequence % SESSION_HASH_SIZE;
  619. }
  620. #ifdef NFSD_DEBUG
  621. static inline void
  622. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  623. {
  624. u32 *ptr = (u32 *)(&sessionid->data[0]);
  625. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  626. }
  627. #else
  628. static inline void
  629. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  630. {
  631. }
  632. #endif
  633. /*
  634. * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
  635. * won't be used for replay.
  636. */
  637. void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
  638. {
  639. struct nfs4_stateowner *so = cstate->replay_owner;
  640. if (nfserr == nfserr_replay_me)
  641. return;
  642. if (!seqid_mutating_err(ntohl(nfserr))) {
  643. cstate->replay_owner = NULL;
  644. return;
  645. }
  646. if (!so)
  647. return;
  648. if (so->so_is_open_owner)
  649. release_last_closed_stateid(openowner(so));
  650. so->so_seqid++;
  651. return;
  652. }
  653. static void
  654. gen_sessionid(struct nfsd4_session *ses)
  655. {
  656. struct nfs4_client *clp = ses->se_client;
  657. struct nfsd4_sessionid *sid;
  658. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  659. sid->clientid = clp->cl_clientid;
  660. sid->sequence = current_sessionid++;
  661. sid->reserved = 0;
  662. }
  663. /*
  664. * The protocol defines ca_maxresponssize_cached to include the size of
  665. * the rpc header, but all we need to cache is the data starting after
  666. * the end of the initial SEQUENCE operation--the rest we regenerate
  667. * each time. Therefore we can advertise a ca_maxresponssize_cached
  668. * value that is the number of bytes in our cache plus a few additional
  669. * bytes. In order to stay on the safe side, and not promise more than
  670. * we can cache, those additional bytes must be the minimum possible: 24
  671. * bytes of rpc header (xid through accept state, with AUTH_NULL
  672. * verifier), 12 for the compound header (with zero-length tag), and 44
  673. * for the SEQUENCE op response:
  674. */
  675. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  676. static void
  677. free_session_slots(struct nfsd4_session *ses)
  678. {
  679. int i;
  680. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  681. kfree(ses->se_slots[i]);
  682. }
  683. /*
  684. * We don't actually need to cache the rpc and session headers, so we
  685. * can allocate a little less for each slot:
  686. */
  687. static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
  688. {
  689. u32 size;
  690. if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
  691. size = 0;
  692. else
  693. size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  694. return size + sizeof(struct nfsd4_slot);
  695. }
  696. /*
  697. * XXX: If we run out of reserved DRC memory we could (up to a point)
  698. * re-negotiate active sessions and reduce their slot usage to make
  699. * room for new connections. For now we just fail the create session.
  700. */
  701. static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
  702. {
  703. u32 slotsize = slot_bytes(ca);
  704. u32 num = ca->maxreqs;
  705. int avail;
  706. spin_lock(&nfsd_drc_lock);
  707. avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
  708. nfsd_drc_max_mem - nfsd_drc_mem_used);
  709. num = min_t(int, num, avail / slotsize);
  710. nfsd_drc_mem_used += num * slotsize;
  711. spin_unlock(&nfsd_drc_lock);
  712. return num;
  713. }
  714. static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
  715. {
  716. int slotsize = slot_bytes(ca);
  717. spin_lock(&nfsd_drc_lock);
  718. nfsd_drc_mem_used -= slotsize * ca->maxreqs;
  719. spin_unlock(&nfsd_drc_lock);
  720. }
  721. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *attrs)
  722. {
  723. int numslots = attrs->maxreqs;
  724. int slotsize = slot_bytes(attrs);
  725. struct nfsd4_session *new;
  726. int mem, i;
  727. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  728. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  729. mem = numslots * sizeof(struct nfsd4_slot *);
  730. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  731. if (!new)
  732. return NULL;
  733. /* allocate each struct nfsd4_slot and data cache in one piece */
  734. for (i = 0; i < numslots; i++) {
  735. new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
  736. if (!new->se_slots[i])
  737. goto out_free;
  738. }
  739. return new;
  740. out_free:
  741. while (i--)
  742. kfree(new->se_slots[i]);
  743. kfree(new);
  744. return NULL;
  745. }
  746. static void free_conn(struct nfsd4_conn *c)
  747. {
  748. svc_xprt_put(c->cn_xprt);
  749. kfree(c);
  750. }
  751. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  752. {
  753. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  754. struct nfs4_client *clp = c->cn_session->se_client;
  755. spin_lock(&clp->cl_lock);
  756. if (!list_empty(&c->cn_persession)) {
  757. list_del(&c->cn_persession);
  758. free_conn(c);
  759. }
  760. nfsd4_probe_callback(clp);
  761. spin_unlock(&clp->cl_lock);
  762. }
  763. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  764. {
  765. struct nfsd4_conn *conn;
  766. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  767. if (!conn)
  768. return NULL;
  769. svc_xprt_get(rqstp->rq_xprt);
  770. conn->cn_xprt = rqstp->rq_xprt;
  771. conn->cn_flags = flags;
  772. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  773. return conn;
  774. }
  775. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  776. {
  777. conn->cn_session = ses;
  778. list_add(&conn->cn_persession, &ses->se_conns);
  779. }
  780. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  781. {
  782. struct nfs4_client *clp = ses->se_client;
  783. spin_lock(&clp->cl_lock);
  784. __nfsd4_hash_conn(conn, ses);
  785. spin_unlock(&clp->cl_lock);
  786. }
  787. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  788. {
  789. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  790. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  791. }
  792. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  793. {
  794. int ret;
  795. nfsd4_hash_conn(conn, ses);
  796. ret = nfsd4_register_conn(conn);
  797. if (ret)
  798. /* oops; xprt is already down: */
  799. nfsd4_conn_lost(&conn->cn_xpt_user);
  800. if (conn->cn_flags & NFS4_CDFC4_BACK) {
  801. /* callback channel may be back up */
  802. nfsd4_probe_callback(ses->se_client);
  803. }
  804. }
  805. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  806. {
  807. u32 dir = NFS4_CDFC4_FORE;
  808. if (cses->flags & SESSION4_BACK_CHAN)
  809. dir |= NFS4_CDFC4_BACK;
  810. return alloc_conn(rqstp, dir);
  811. }
  812. /* must be called under client_lock */
  813. static void nfsd4_del_conns(struct nfsd4_session *s)
  814. {
  815. struct nfs4_client *clp = s->se_client;
  816. struct nfsd4_conn *c;
  817. spin_lock(&clp->cl_lock);
  818. while (!list_empty(&s->se_conns)) {
  819. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  820. list_del_init(&c->cn_persession);
  821. spin_unlock(&clp->cl_lock);
  822. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  823. free_conn(c);
  824. spin_lock(&clp->cl_lock);
  825. }
  826. spin_unlock(&clp->cl_lock);
  827. }
  828. static void __free_session(struct nfsd4_session *ses)
  829. {
  830. free_session_slots(ses);
  831. kfree(ses);
  832. }
  833. static void free_session(struct nfsd4_session *ses)
  834. {
  835. struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
  836. lockdep_assert_held(&nn->client_lock);
  837. nfsd4_del_conns(ses);
  838. nfsd4_put_drc_mem(&ses->se_fchannel);
  839. __free_session(ses);
  840. }
  841. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  842. {
  843. int idx;
  844. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  845. new->se_client = clp;
  846. gen_sessionid(new);
  847. INIT_LIST_HEAD(&new->se_conns);
  848. new->se_cb_seq_nr = 1;
  849. new->se_flags = cses->flags;
  850. new->se_cb_prog = cses->callback_prog;
  851. new->se_cb_sec = cses->cb_sec;
  852. atomic_set(&new->se_ref, 0);
  853. idx = hash_sessionid(&new->se_sessionid);
  854. spin_lock(&nn->client_lock);
  855. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  856. spin_lock(&clp->cl_lock);
  857. list_add(&new->se_perclnt, &clp->cl_sessions);
  858. spin_unlock(&clp->cl_lock);
  859. spin_unlock(&nn->client_lock);
  860. memcpy(&new->se_fchannel, &cses->fore_channel,
  861. sizeof(struct nfsd4_channel_attrs));
  862. if (cses->flags & SESSION4_BACK_CHAN) {
  863. struct sockaddr *sa = svc_addr(rqstp);
  864. /*
  865. * This is a little silly; with sessions there's no real
  866. * use for the callback address. Use the peer address
  867. * as a reasonable default for now, but consider fixing
  868. * the rpc client not to require an address in the
  869. * future:
  870. */
  871. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  872. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  873. }
  874. }
  875. /* caller must hold client_lock */
  876. static struct nfsd4_session *
  877. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  878. {
  879. struct nfsd4_session *elem;
  880. int idx;
  881. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  882. dump_sessionid(__func__, sessionid);
  883. idx = hash_sessionid(sessionid);
  884. /* Search in the appropriate list */
  885. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  886. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  887. NFS4_MAX_SESSIONID_LEN)) {
  888. return elem;
  889. }
  890. }
  891. dprintk("%s: session not found\n", __func__);
  892. return NULL;
  893. }
  894. /* caller must hold client_lock */
  895. static void
  896. unhash_session(struct nfsd4_session *ses)
  897. {
  898. list_del(&ses->se_hash);
  899. spin_lock(&ses->se_client->cl_lock);
  900. list_del(&ses->se_perclnt);
  901. spin_unlock(&ses->se_client->cl_lock);
  902. }
  903. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  904. static int
  905. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  906. {
  907. if (clid->cl_boot == nn->boot_time)
  908. return 0;
  909. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  910. clid->cl_boot, clid->cl_id, nn->boot_time);
  911. return 1;
  912. }
  913. /*
  914. * XXX Should we use a slab cache ?
  915. * This type of memory management is somewhat inefficient, but we use it
  916. * anyway since SETCLIENTID is not a common operation.
  917. */
  918. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  919. {
  920. struct nfs4_client *clp;
  921. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  922. if (clp == NULL)
  923. return NULL;
  924. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  925. if (clp->cl_name.data == NULL) {
  926. kfree(clp);
  927. return NULL;
  928. }
  929. clp->cl_name.len = name.len;
  930. return clp;
  931. }
  932. static inline void
  933. free_client(struct nfs4_client *clp)
  934. {
  935. struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
  936. lockdep_assert_held(&nn->client_lock);
  937. while (!list_empty(&clp->cl_sessions)) {
  938. struct nfsd4_session *ses;
  939. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  940. se_perclnt);
  941. list_del(&ses->se_perclnt);
  942. WARN_ON_ONCE(atomic_read(&ses->se_ref));
  943. free_session(ses);
  944. }
  945. free_svc_cred(&clp->cl_cred);
  946. kfree(clp->cl_name.data);
  947. idr_destroy(&clp->cl_stateids);
  948. kfree(clp);
  949. }
  950. /* must be called under the client_lock */
  951. static inline void
  952. unhash_client_locked(struct nfs4_client *clp)
  953. {
  954. struct nfsd4_session *ses;
  955. list_del(&clp->cl_lru);
  956. spin_lock(&clp->cl_lock);
  957. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  958. list_del_init(&ses->se_hash);
  959. spin_unlock(&clp->cl_lock);
  960. }
  961. static void
  962. destroy_client(struct nfs4_client *clp)
  963. {
  964. struct nfs4_openowner *oo;
  965. struct nfs4_delegation *dp;
  966. struct list_head reaplist;
  967. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  968. INIT_LIST_HEAD(&reaplist);
  969. spin_lock(&recall_lock);
  970. while (!list_empty(&clp->cl_delegations)) {
  971. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  972. list_del_init(&dp->dl_perclnt);
  973. list_move(&dp->dl_recall_lru, &reaplist);
  974. }
  975. spin_unlock(&recall_lock);
  976. while (!list_empty(&reaplist)) {
  977. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  978. destroy_delegation(dp);
  979. }
  980. while (!list_empty(&clp->cl_openowners)) {
  981. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  982. release_openowner(oo);
  983. }
  984. nfsd4_shutdown_callback(clp);
  985. if (clp->cl_cb_conn.cb_xprt)
  986. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  987. list_del(&clp->cl_idhash);
  988. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  989. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  990. else
  991. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  992. spin_lock(&nn->client_lock);
  993. unhash_client_locked(clp);
  994. WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
  995. free_client(clp);
  996. spin_unlock(&nn->client_lock);
  997. }
  998. static void expire_client(struct nfs4_client *clp)
  999. {
  1000. nfsd4_client_record_remove(clp);
  1001. destroy_client(clp);
  1002. }
  1003. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  1004. {
  1005. memcpy(target->cl_verifier.data, source->data,
  1006. sizeof(target->cl_verifier.data));
  1007. }
  1008. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  1009. {
  1010. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  1011. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  1012. }
  1013. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  1014. {
  1015. if (source->cr_principal) {
  1016. target->cr_principal =
  1017. kstrdup(source->cr_principal, GFP_KERNEL);
  1018. if (target->cr_principal == NULL)
  1019. return -ENOMEM;
  1020. } else
  1021. target->cr_principal = NULL;
  1022. target->cr_flavor = source->cr_flavor;
  1023. target->cr_uid = source->cr_uid;
  1024. target->cr_gid = source->cr_gid;
  1025. target->cr_group_info = source->cr_group_info;
  1026. get_group_info(target->cr_group_info);
  1027. return 0;
  1028. }
  1029. static long long
  1030. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1031. {
  1032. long long res;
  1033. res = o1->len - o2->len;
  1034. if (res)
  1035. return res;
  1036. return (long long)memcmp(o1->data, o2->data, o1->len);
  1037. }
  1038. static int same_name(const char *n1, const char *n2)
  1039. {
  1040. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1041. }
  1042. static int
  1043. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1044. {
  1045. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1046. }
  1047. static int
  1048. same_clid(clientid_t *cl1, clientid_t *cl2)
  1049. {
  1050. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1051. }
  1052. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1053. {
  1054. int i;
  1055. if (g1->ngroups != g2->ngroups)
  1056. return false;
  1057. for (i=0; i<g1->ngroups; i++)
  1058. if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
  1059. return false;
  1060. return true;
  1061. }
  1062. /*
  1063. * RFC 3530 language requires clid_inuse be returned when the
  1064. * "principal" associated with a requests differs from that previously
  1065. * used. We use uid, gid's, and gss principal string as our best
  1066. * approximation. We also don't want to allow non-gss use of a client
  1067. * established using gss: in theory cr_principal should catch that
  1068. * change, but in practice cr_principal can be null even in the gss case
  1069. * since gssd doesn't always pass down a principal string.
  1070. */
  1071. static bool is_gss_cred(struct svc_cred *cr)
  1072. {
  1073. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1074. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1075. }
  1076. static bool
  1077. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1078. {
  1079. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1080. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  1081. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  1082. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1083. return false;
  1084. if (cr1->cr_principal == cr2->cr_principal)
  1085. return true;
  1086. if (!cr1->cr_principal || !cr2->cr_principal)
  1087. return false;
  1088. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1089. }
  1090. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1091. {
  1092. static u32 current_clientid = 1;
  1093. clp->cl_clientid.cl_boot = nn->boot_time;
  1094. clp->cl_clientid.cl_id = current_clientid++;
  1095. }
  1096. static void gen_confirm(struct nfs4_client *clp)
  1097. {
  1098. __be32 verf[2];
  1099. static u32 i;
  1100. verf[0] = (__be32)get_seconds();
  1101. verf[1] = (__be32)i++;
  1102. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1103. }
  1104. static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
  1105. {
  1106. struct nfs4_stid *ret;
  1107. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1108. if (!ret || !ret->sc_type)
  1109. return NULL;
  1110. return ret;
  1111. }
  1112. static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1113. {
  1114. struct nfs4_stid *s;
  1115. s = find_stateid(cl, t);
  1116. if (!s)
  1117. return NULL;
  1118. if (typemask & s->sc_type)
  1119. return s;
  1120. return NULL;
  1121. }
  1122. static struct nfs4_client *create_client(struct xdr_netobj name,
  1123. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1124. {
  1125. struct nfs4_client *clp;
  1126. struct sockaddr *sa = svc_addr(rqstp);
  1127. int ret;
  1128. struct net *net = SVC_NET(rqstp);
  1129. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1130. clp = alloc_client(name);
  1131. if (clp == NULL)
  1132. return NULL;
  1133. INIT_LIST_HEAD(&clp->cl_sessions);
  1134. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1135. if (ret) {
  1136. spin_lock(&nn->client_lock);
  1137. free_client(clp);
  1138. spin_unlock(&nn->client_lock);
  1139. return NULL;
  1140. }
  1141. idr_init(&clp->cl_stateids);
  1142. atomic_set(&clp->cl_refcount, 0);
  1143. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1144. INIT_LIST_HEAD(&clp->cl_idhash);
  1145. INIT_LIST_HEAD(&clp->cl_openowners);
  1146. INIT_LIST_HEAD(&clp->cl_delegations);
  1147. INIT_LIST_HEAD(&clp->cl_lru);
  1148. INIT_LIST_HEAD(&clp->cl_callbacks);
  1149. INIT_LIST_HEAD(&clp->cl_revoked);
  1150. spin_lock_init(&clp->cl_lock);
  1151. nfsd4_init_callback(&clp->cl_cb_null);
  1152. clp->cl_time = get_seconds();
  1153. clear_bit(0, &clp->cl_cb_slot_busy);
  1154. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1155. copy_verf(clp, verf);
  1156. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1157. gen_confirm(clp);
  1158. clp->cl_cb_session = NULL;
  1159. clp->net = net;
  1160. return clp;
  1161. }
  1162. static void
  1163. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1164. {
  1165. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1166. struct nfs4_client *clp;
  1167. while (*new) {
  1168. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1169. parent = *new;
  1170. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1171. new = &((*new)->rb_left);
  1172. else
  1173. new = &((*new)->rb_right);
  1174. }
  1175. rb_link_node(&new_clp->cl_namenode, parent, new);
  1176. rb_insert_color(&new_clp->cl_namenode, root);
  1177. }
  1178. static struct nfs4_client *
  1179. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1180. {
  1181. long long cmp;
  1182. struct rb_node *node = root->rb_node;
  1183. struct nfs4_client *clp;
  1184. while (node) {
  1185. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1186. cmp = compare_blob(&clp->cl_name, name);
  1187. if (cmp > 0)
  1188. node = node->rb_left;
  1189. else if (cmp < 0)
  1190. node = node->rb_right;
  1191. else
  1192. return clp;
  1193. }
  1194. return NULL;
  1195. }
  1196. static void
  1197. add_to_unconfirmed(struct nfs4_client *clp)
  1198. {
  1199. unsigned int idhashval;
  1200. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1201. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1202. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1203. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1204. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1205. renew_client(clp);
  1206. }
  1207. static void
  1208. move_to_confirmed(struct nfs4_client *clp)
  1209. {
  1210. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1211. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1212. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1213. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1214. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1215. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1216. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1217. renew_client(clp);
  1218. }
  1219. static struct nfs4_client *
  1220. find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
  1221. {
  1222. struct nfs4_client *clp;
  1223. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1224. list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
  1225. if (same_clid(&clp->cl_clientid, clid)) {
  1226. if ((bool)clp->cl_minorversion != sessions)
  1227. return NULL;
  1228. renew_client(clp);
  1229. return clp;
  1230. }
  1231. }
  1232. return NULL;
  1233. }
  1234. static struct nfs4_client *
  1235. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1236. {
  1237. struct list_head *tbl = nn->conf_id_hashtbl;
  1238. return find_client_in_id_table(tbl, clid, sessions);
  1239. }
  1240. static struct nfs4_client *
  1241. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1242. {
  1243. struct list_head *tbl = nn->unconf_id_hashtbl;
  1244. return find_client_in_id_table(tbl, clid, sessions);
  1245. }
  1246. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1247. {
  1248. return clp->cl_exchange_flags != 0;
  1249. }
  1250. static struct nfs4_client *
  1251. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1252. {
  1253. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1254. }
  1255. static struct nfs4_client *
  1256. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1257. {
  1258. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1259. }
  1260. static void
  1261. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1262. {
  1263. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1264. struct sockaddr *sa = svc_addr(rqstp);
  1265. u32 scopeid = rpc_get_scope_id(sa);
  1266. unsigned short expected_family;
  1267. /* Currently, we only support tcp and tcp6 for the callback channel */
  1268. if (se->se_callback_netid_len == 3 &&
  1269. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1270. expected_family = AF_INET;
  1271. else if (se->se_callback_netid_len == 4 &&
  1272. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1273. expected_family = AF_INET6;
  1274. else
  1275. goto out_err;
  1276. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1277. se->se_callback_addr_len,
  1278. (struct sockaddr *)&conn->cb_addr,
  1279. sizeof(conn->cb_addr));
  1280. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1281. goto out_err;
  1282. if (conn->cb_addr.ss_family == AF_INET6)
  1283. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1284. conn->cb_prog = se->se_callback_prog;
  1285. conn->cb_ident = se->se_callback_ident;
  1286. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1287. return;
  1288. out_err:
  1289. conn->cb_addr.ss_family = AF_UNSPEC;
  1290. conn->cb_addrlen = 0;
  1291. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1292. "will not receive delegations\n",
  1293. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1294. return;
  1295. }
  1296. /*
  1297. * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
  1298. */
  1299. void
  1300. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1301. {
  1302. struct nfsd4_slot *slot = resp->cstate.slot;
  1303. unsigned int base;
  1304. dprintk("--> %s slot %p\n", __func__, slot);
  1305. slot->sl_opcnt = resp->opcnt;
  1306. slot->sl_status = resp->cstate.status;
  1307. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1308. if (nfsd4_not_cached(resp)) {
  1309. slot->sl_datalen = 0;
  1310. return;
  1311. }
  1312. slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
  1313. base = (char *)resp->cstate.datap -
  1314. (char *)resp->xbuf->head[0].iov_base;
  1315. if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
  1316. slot->sl_datalen))
  1317. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1318. return;
  1319. }
  1320. /*
  1321. * Encode the replay sequence operation from the slot values.
  1322. * If cachethis is FALSE encode the uncached rep error on the next
  1323. * operation which sets resp->p and increments resp->opcnt for
  1324. * nfs4svc_encode_compoundres.
  1325. *
  1326. */
  1327. static __be32
  1328. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1329. struct nfsd4_compoundres *resp)
  1330. {
  1331. struct nfsd4_op *op;
  1332. struct nfsd4_slot *slot = resp->cstate.slot;
  1333. /* Encode the replayed sequence operation */
  1334. op = &args->ops[resp->opcnt - 1];
  1335. nfsd4_encode_operation(resp, op);
  1336. /* Return nfserr_retry_uncached_rep in next operation. */
  1337. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1338. op = &args->ops[resp->opcnt++];
  1339. op->status = nfserr_retry_uncached_rep;
  1340. nfsd4_encode_operation(resp, op);
  1341. }
  1342. return op->status;
  1343. }
  1344. /*
  1345. * The sequence operation is not cached because we can use the slot and
  1346. * session values.
  1347. */
  1348. __be32
  1349. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1350. struct nfsd4_sequence *seq)
  1351. {
  1352. struct nfsd4_slot *slot = resp->cstate.slot;
  1353. __be32 status;
  1354. dprintk("--> %s slot %p\n", __func__, slot);
  1355. /* Either returns 0 or nfserr_retry_uncached */
  1356. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1357. if (status == nfserr_retry_uncached_rep)
  1358. return status;
  1359. /* The sequence operation has been encoded, cstate->datap set. */
  1360. memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
  1361. resp->opcnt = slot->sl_opcnt;
  1362. resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
  1363. status = slot->sl_status;
  1364. return status;
  1365. }
  1366. /*
  1367. * Set the exchange_id flags returned by the server.
  1368. */
  1369. static void
  1370. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1371. {
  1372. /* pNFS is not supported */
  1373. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1374. /* Referrals are supported, Migration is not. */
  1375. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1376. /* set the wire flags to return to client. */
  1377. clid->flags = new->cl_exchange_flags;
  1378. }
  1379. static bool client_has_state(struct nfs4_client *clp)
  1380. {
  1381. /*
  1382. * Note clp->cl_openowners check isn't quite right: there's no
  1383. * need to count owners without stateid's.
  1384. *
  1385. * Also note we should probably be using this in 4.0 case too.
  1386. */
  1387. return !list_empty(&clp->cl_openowners)
  1388. || !list_empty(&clp->cl_delegations)
  1389. || !list_empty(&clp->cl_sessions);
  1390. }
  1391. __be32
  1392. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1393. struct nfsd4_compound_state *cstate,
  1394. struct nfsd4_exchange_id *exid)
  1395. {
  1396. struct nfs4_client *unconf, *conf, *new;
  1397. __be32 status;
  1398. char addr_str[INET6_ADDRSTRLEN];
  1399. nfs4_verifier verf = exid->verifier;
  1400. struct sockaddr *sa = svc_addr(rqstp);
  1401. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  1402. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1403. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1404. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1405. "ip_addr=%s flags %x, spa_how %d\n",
  1406. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1407. addr_str, exid->flags, exid->spa_how);
  1408. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1409. return nfserr_inval;
  1410. /* Currently only support SP4_NONE */
  1411. switch (exid->spa_how) {
  1412. case SP4_NONE:
  1413. break;
  1414. default: /* checked by xdr code */
  1415. WARN_ON_ONCE(1);
  1416. case SP4_SSV:
  1417. return nfserr_encr_alg_unsupp;
  1418. case SP4_MACH_CRED:
  1419. return nfserr_serverfault; /* no excuse :-/ */
  1420. }
  1421. /* Cases below refer to rfc 5661 section 18.35.4: */
  1422. nfs4_lock_state();
  1423. conf = find_confirmed_client_by_name(&exid->clname, nn);
  1424. if (conf) {
  1425. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  1426. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  1427. if (update) {
  1428. if (!clp_used_exchangeid(conf)) { /* buggy client */
  1429. status = nfserr_inval;
  1430. goto out;
  1431. }
  1432. if (!creds_match) { /* case 9 */
  1433. status = nfserr_perm;
  1434. goto out;
  1435. }
  1436. if (!verfs_match) { /* case 8 */
  1437. status = nfserr_not_same;
  1438. goto out;
  1439. }
  1440. /* case 6 */
  1441. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1442. new = conf;
  1443. goto out_copy;
  1444. }
  1445. if (!creds_match) { /* case 3 */
  1446. if (client_has_state(conf)) {
  1447. status = nfserr_clid_inuse;
  1448. goto out;
  1449. }
  1450. expire_client(conf);
  1451. goto out_new;
  1452. }
  1453. if (verfs_match) { /* case 2 */
  1454. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1455. new = conf;
  1456. goto out_copy;
  1457. }
  1458. /* case 5, client reboot */
  1459. goto out_new;
  1460. }
  1461. if (update) { /* case 7 */
  1462. status = nfserr_noent;
  1463. goto out;
  1464. }
  1465. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  1466. if (unconf) /* case 4, possible retry or client restart */
  1467. expire_client(unconf);
  1468. /* case 1 (normal case) */
  1469. out_new:
  1470. new = create_client(exid->clname, rqstp, &verf);
  1471. if (new == NULL) {
  1472. status = nfserr_jukebox;
  1473. goto out;
  1474. }
  1475. new->cl_minorversion = 1;
  1476. gen_clid(new, nn);
  1477. add_to_unconfirmed(new);
  1478. out_copy:
  1479. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1480. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1481. exid->seqid = new->cl_cs_slot.sl_seqid + 1;
  1482. nfsd4_set_ex_flags(new, exid);
  1483. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1484. new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
  1485. status = nfs_ok;
  1486. out:
  1487. nfs4_unlock_state();
  1488. return status;
  1489. }
  1490. static __be32
  1491. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1492. {
  1493. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1494. slot_seqid);
  1495. /* The slot is in use, and no response has been sent. */
  1496. if (slot_inuse) {
  1497. if (seqid == slot_seqid)
  1498. return nfserr_jukebox;
  1499. else
  1500. return nfserr_seq_misordered;
  1501. }
  1502. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1503. if (likely(seqid == slot_seqid + 1))
  1504. return nfs_ok;
  1505. if (seqid == slot_seqid)
  1506. return nfserr_replay_cache;
  1507. return nfserr_seq_misordered;
  1508. }
  1509. /*
  1510. * Cache the create session result into the create session single DRC
  1511. * slot cache by saving the xdr structure. sl_seqid has been set.
  1512. * Do this for solo or embedded create session operations.
  1513. */
  1514. static void
  1515. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1516. struct nfsd4_clid_slot *slot, __be32 nfserr)
  1517. {
  1518. slot->sl_status = nfserr;
  1519. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1520. }
  1521. static __be32
  1522. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1523. struct nfsd4_clid_slot *slot)
  1524. {
  1525. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1526. return slot->sl_status;
  1527. }
  1528. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1529. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1530. 1 + /* MIN tag is length with zero, only length */ \
  1531. 3 + /* version, opcount, opcode */ \
  1532. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1533. /* seqid, slotID, slotID, cache */ \
  1534. 4 ) * sizeof(__be32))
  1535. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1536. 2 + /* verifier: AUTH_NULL, length 0 */\
  1537. 1 + /* status */ \
  1538. 1 + /* MIN tag is length with zero, only length */ \
  1539. 3 + /* opcount, opcode, opstatus*/ \
  1540. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1541. /* seqid, slotID, slotID, slotID, status */ \
  1542. 5 ) * sizeof(__be32))
  1543. static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
  1544. {
  1545. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  1546. if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
  1547. return nfserr_toosmall;
  1548. if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
  1549. return nfserr_toosmall;
  1550. ca->headerpadsz = 0;
  1551. ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
  1552. ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
  1553. ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  1554. ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
  1555. NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
  1556. ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
  1557. /*
  1558. * Note decreasing slot size below client's request may make it
  1559. * difficult for client to function correctly, whereas
  1560. * decreasing the number of slots will (just?) affect
  1561. * performance. When short on memory we therefore prefer to
  1562. * decrease number of slots instead of their size. Clients that
  1563. * request larger slots than they need will get poor results:
  1564. */
  1565. ca->maxreqs = nfsd4_get_drc_mem(ca);
  1566. if (!ca->maxreqs)
  1567. return nfserr_jukebox;
  1568. return nfs_ok;
  1569. }
  1570. static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
  1571. {
  1572. ca->headerpadsz = 0;
  1573. /*
  1574. * These RPC_MAX_HEADER macros are overkill, especially since we
  1575. * don't even do gss on the backchannel yet. But this is still
  1576. * less than 1k. Tighten up this estimate in the unlikely event
  1577. * it turns out to be a problem for some client:
  1578. */
  1579. if (ca->maxreq_sz < NFS4_enc_cb_recall_sz + RPC_MAX_HEADER_WITH_AUTH)
  1580. return nfserr_toosmall;
  1581. if (ca->maxresp_sz < NFS4_dec_cb_recall_sz + RPC_MAX_REPHEADER_WITH_AUTH)
  1582. return nfserr_toosmall;
  1583. ca->maxresp_cached = 0;
  1584. if (ca->maxops < 2)
  1585. return nfserr_toosmall;
  1586. return nfs_ok;
  1587. }
  1588. __be32
  1589. nfsd4_create_session(struct svc_rqst *rqstp,
  1590. struct nfsd4_compound_state *cstate,
  1591. struct nfsd4_create_session *cr_ses)
  1592. {
  1593. struct sockaddr *sa = svc_addr(rqstp);
  1594. struct nfs4_client *conf, *unconf;
  1595. struct nfsd4_session *new;
  1596. struct nfsd4_conn *conn;
  1597. struct nfsd4_clid_slot *cs_slot = NULL;
  1598. __be32 status = 0;
  1599. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1600. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  1601. return nfserr_inval;
  1602. status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
  1603. if (status)
  1604. return status;
  1605. status = check_backchannel_attrs(&cr_ses->back_channel);
  1606. if (status)
  1607. return status;
  1608. status = nfserr_jukebox;
  1609. new = alloc_session(&cr_ses->fore_channel);
  1610. if (!new)
  1611. goto out_release_drc_mem;
  1612. conn = alloc_conn_from_crses(rqstp, cr_ses);
  1613. if (!conn)
  1614. goto out_free_session;
  1615. nfs4_lock_state();
  1616. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  1617. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  1618. WARN_ON_ONCE(conf && unconf);
  1619. if (conf) {
  1620. cs_slot = &conf->cl_cs_slot;
  1621. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1622. if (status == nfserr_replay_cache) {
  1623. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  1624. goto out_free_conn;
  1625. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  1626. status = nfserr_seq_misordered;
  1627. goto out_free_conn;
  1628. }
  1629. } else if (unconf) {
  1630. struct nfs4_client *old;
  1631. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1632. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  1633. status = nfserr_clid_inuse;
  1634. goto out_free_conn;
  1635. }
  1636. cs_slot = &unconf->cl_cs_slot;
  1637. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1638. if (status) {
  1639. /* an unconfirmed replay returns misordered */
  1640. status = nfserr_seq_misordered;
  1641. goto out_free_conn;
  1642. }
  1643. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1644. if (old) {
  1645. status = mark_client_expired(old);
  1646. if (status)
  1647. goto out_free_conn;
  1648. expire_client(old);
  1649. }
  1650. move_to_confirmed(unconf);
  1651. conf = unconf;
  1652. } else {
  1653. status = nfserr_stale_clientid;
  1654. goto out_free_conn;
  1655. }
  1656. status = nfs_ok;
  1657. /*
  1658. * We do not support RDMA or persistent sessions
  1659. */
  1660. cr_ses->flags &= ~SESSION4_PERSIST;
  1661. cr_ses->flags &= ~SESSION4_RDMA;
  1662. init_session(rqstp, new, conf, cr_ses);
  1663. nfsd4_init_conn(rqstp, conn, new);
  1664. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  1665. NFS4_MAX_SESSIONID_LEN);
  1666. cs_slot->sl_seqid++;
  1667. cr_ses->seqid = cs_slot->sl_seqid;
  1668. /* cache solo and embedded create sessions under the state lock */
  1669. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  1670. nfs4_unlock_state();
  1671. return status;
  1672. out_free_conn:
  1673. nfs4_unlock_state();
  1674. free_conn(conn);
  1675. out_free_session:
  1676. __free_session(new);
  1677. out_release_drc_mem:
  1678. nfsd4_put_drc_mem(&cr_ses->fore_channel);
  1679. return status;
  1680. }
  1681. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  1682. {
  1683. switch (*dir) {
  1684. case NFS4_CDFC4_FORE:
  1685. case NFS4_CDFC4_BACK:
  1686. return nfs_ok;
  1687. case NFS4_CDFC4_FORE_OR_BOTH:
  1688. case NFS4_CDFC4_BACK_OR_BOTH:
  1689. *dir = NFS4_CDFC4_BOTH;
  1690. return nfs_ok;
  1691. };
  1692. return nfserr_inval;
  1693. }
  1694. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  1695. {
  1696. struct nfsd4_session *session = cstate->session;
  1697. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1698. spin_lock(&nn->client_lock);
  1699. session->se_cb_prog = bc->bc_cb_program;
  1700. session->se_cb_sec = bc->bc_cb_sec;
  1701. spin_unlock(&nn->client_lock);
  1702. nfsd4_probe_callback(session->se_client);
  1703. return nfs_ok;
  1704. }
  1705. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  1706. struct nfsd4_compound_state *cstate,
  1707. struct nfsd4_bind_conn_to_session *bcts)
  1708. {
  1709. __be32 status;
  1710. struct nfsd4_conn *conn;
  1711. struct nfsd4_session *session;
  1712. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1713. if (!nfsd4_last_compound_op(rqstp))
  1714. return nfserr_not_only_op;
  1715. nfs4_lock_state();
  1716. spin_lock(&nn->client_lock);
  1717. session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
  1718. spin_unlock(&nn->client_lock);
  1719. status = nfserr_badsession;
  1720. if (!session)
  1721. goto out;
  1722. status = nfsd4_map_bcts_dir(&bcts->dir);
  1723. if (status)
  1724. goto out;
  1725. conn = alloc_conn(rqstp, bcts->dir);
  1726. status = nfserr_jukebox;
  1727. if (!conn)
  1728. goto out;
  1729. nfsd4_init_conn(rqstp, conn, session);
  1730. status = nfs_ok;
  1731. out:
  1732. nfs4_unlock_state();
  1733. return status;
  1734. }
  1735. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  1736. {
  1737. if (!session)
  1738. return 0;
  1739. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  1740. }
  1741. __be32
  1742. nfsd4_destroy_session(struct svc_rqst *r,
  1743. struct nfsd4_compound_state *cstate,
  1744. struct nfsd4_destroy_session *sessionid)
  1745. {
  1746. struct nfsd4_session *ses;
  1747. __be32 status;
  1748. struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
  1749. nfs4_lock_state();
  1750. status = nfserr_not_only_op;
  1751. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  1752. if (!nfsd4_last_compound_op(r))
  1753. goto out;
  1754. }
  1755. dump_sessionid(__func__, &sessionid->sessionid);
  1756. spin_lock(&nn->client_lock);
  1757. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
  1758. status = nfserr_badsession;
  1759. if (!ses)
  1760. goto out_client_lock;
  1761. status = mark_session_dead_locked(ses);
  1762. if (status)
  1763. goto out_client_lock;
  1764. unhash_session(ses);
  1765. spin_unlock(&nn->client_lock);
  1766. nfsd4_probe_callback_sync(ses->se_client);
  1767. spin_lock(&nn->client_lock);
  1768. free_session(ses);
  1769. status = nfs_ok;
  1770. out_client_lock:
  1771. spin_unlock(&nn->client_lock);
  1772. out:
  1773. nfs4_unlock_state();
  1774. return status;
  1775. }
  1776. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  1777. {
  1778. struct nfsd4_conn *c;
  1779. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1780. if (c->cn_xprt == xpt) {
  1781. return c;
  1782. }
  1783. }
  1784. return NULL;
  1785. }
  1786. static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  1787. {
  1788. struct nfs4_client *clp = ses->se_client;
  1789. struct nfsd4_conn *c;
  1790. int ret;
  1791. spin_lock(&clp->cl_lock);
  1792. c = __nfsd4_find_conn(new->cn_xprt, ses);
  1793. if (c) {
  1794. spin_unlock(&clp->cl_lock);
  1795. free_conn(new);
  1796. return;
  1797. }
  1798. __nfsd4_hash_conn(new, ses);
  1799. spin_unlock(&clp->cl_lock);
  1800. ret = nfsd4_register_conn(new);
  1801. if (ret)
  1802. /* oops; xprt is already down: */
  1803. nfsd4_conn_lost(&new->cn_xpt_user);
  1804. return;
  1805. }
  1806. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  1807. {
  1808. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1809. return args->opcnt > session->se_fchannel.maxops;
  1810. }
  1811. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  1812. struct nfsd4_session *session)
  1813. {
  1814. struct xdr_buf *xb = &rqstp->rq_arg;
  1815. return xb->len > session->se_fchannel.maxreq_sz;
  1816. }
  1817. __be32
  1818. nfsd4_sequence(struct svc_rqst *rqstp,
  1819. struct nfsd4_compound_state *cstate,
  1820. struct nfsd4_sequence *seq)
  1821. {
  1822. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1823. struct nfsd4_session *session;
  1824. struct nfs4_client *clp;
  1825. struct nfsd4_slot *slot;
  1826. struct nfsd4_conn *conn;
  1827. __be32 status;
  1828. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1829. if (resp->opcnt != 1)
  1830. return nfserr_sequence_pos;
  1831. /*
  1832. * Will be either used or freed by nfsd4_sequence_check_conn
  1833. * below.
  1834. */
  1835. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  1836. if (!conn)
  1837. return nfserr_jukebox;
  1838. spin_lock(&nn->client_lock);
  1839. status = nfserr_badsession;
  1840. session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
  1841. if (!session)
  1842. goto out_no_session;
  1843. clp = session->se_client;
  1844. status = get_client_locked(clp);
  1845. if (status)
  1846. goto out_no_session;
  1847. status = nfsd4_get_session_locked(session);
  1848. if (status)
  1849. goto out_put_client;
  1850. status = nfserr_too_many_ops;
  1851. if (nfsd4_session_too_many_ops(rqstp, session))
  1852. goto out_put_session;
  1853. status = nfserr_req_too_big;
  1854. if (nfsd4_request_too_big(rqstp, session))
  1855. goto out_put_session;
  1856. status = nfserr_badslot;
  1857. if (seq->slotid >= session->se_fchannel.maxreqs)
  1858. goto out_put_session;
  1859. slot = session->se_slots[seq->slotid];
  1860. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1861. /* We do not negotiate the number of slots yet, so set the
  1862. * maxslots to the session maxreqs which is used to encode
  1863. * sr_highest_slotid and the sr_target_slot id to maxslots */
  1864. seq->maxslots = session->se_fchannel.maxreqs;
  1865. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  1866. slot->sl_flags & NFSD4_SLOT_INUSE);
  1867. if (status == nfserr_replay_cache) {
  1868. status = nfserr_seq_misordered;
  1869. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  1870. goto out_put_session;
  1871. cstate->slot = slot;
  1872. cstate->session = session;
  1873. /* Return the cached reply status and set cstate->status
  1874. * for nfsd4_proc_compound processing */
  1875. status = nfsd4_replay_cache_entry(resp, seq);
  1876. cstate->status = nfserr_replay_cache;
  1877. goto out;
  1878. }
  1879. if (status)
  1880. goto out_put_session;
  1881. nfsd4_sequence_check_conn(conn, session);
  1882. conn = NULL;
  1883. /* Success! bump slot seqid */
  1884. slot->sl_seqid = seq->seqid;
  1885. slot->sl_flags |= NFSD4_SLOT_INUSE;
  1886. if (seq->cachethis)
  1887. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  1888. else
  1889. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  1890. cstate->slot = slot;
  1891. cstate->session = session;
  1892. out:
  1893. switch (clp->cl_cb_state) {
  1894. case NFSD4_CB_DOWN:
  1895. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  1896. break;
  1897. case NFSD4_CB_FAULT:
  1898. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  1899. break;
  1900. default:
  1901. seq->status_flags = 0;
  1902. }
  1903. if (!list_empty(&clp->cl_revoked))
  1904. seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
  1905. out_no_session:
  1906. kfree(conn);
  1907. spin_unlock(&nn->client_lock);
  1908. return status;
  1909. out_put_session:
  1910. nfsd4_put_session(session);
  1911. out_put_client:
  1912. put_client_renew_locked(clp);
  1913. goto out_no_session;
  1914. }
  1915. __be32
  1916. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  1917. {
  1918. struct nfs4_client *conf, *unconf, *clp;
  1919. __be32 status = 0;
  1920. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1921. nfs4_lock_state();
  1922. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  1923. conf = find_confirmed_client(&dc->clientid, true, nn);
  1924. WARN_ON_ONCE(conf && unconf);
  1925. if (conf) {
  1926. clp = conf;
  1927. if (client_has_state(conf)) {
  1928. status = nfserr_clientid_busy;
  1929. goto out;
  1930. }
  1931. } else if (unconf)
  1932. clp = unconf;
  1933. else {
  1934. status = nfserr_stale_clientid;
  1935. goto out;
  1936. }
  1937. expire_client(clp);
  1938. out:
  1939. nfs4_unlock_state();
  1940. return status;
  1941. }
  1942. __be32
  1943. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  1944. {
  1945. __be32 status = 0;
  1946. if (rc->rca_one_fs) {
  1947. if (!cstate->current_fh.fh_dentry)
  1948. return nfserr_nofilehandle;
  1949. /*
  1950. * We don't take advantage of the rca_one_fs case.
  1951. * That's OK, it's optional, we can safely ignore it.
  1952. */
  1953. return nfs_ok;
  1954. }
  1955. nfs4_lock_state();
  1956. status = nfserr_complete_already;
  1957. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  1958. &cstate->session->se_client->cl_flags))
  1959. goto out;
  1960. status = nfserr_stale_clientid;
  1961. if (is_client_expired(cstate->session->se_client))
  1962. /*
  1963. * The following error isn't really legal.
  1964. * But we only get here if the client just explicitly
  1965. * destroyed the client. Surely it no longer cares what
  1966. * error it gets back on an operation for the dead
  1967. * client.
  1968. */
  1969. goto out;
  1970. status = nfs_ok;
  1971. nfsd4_client_record_create(cstate->session->se_client);
  1972. out:
  1973. nfs4_unlock_state();
  1974. return status;
  1975. }
  1976. __be32
  1977. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1978. struct nfsd4_setclientid *setclid)
  1979. {
  1980. struct xdr_netobj clname = setclid->se_name;
  1981. nfs4_verifier clverifier = setclid->se_verf;
  1982. struct nfs4_client *conf, *unconf, *new;
  1983. __be32 status;
  1984. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1985. /* Cases below refer to rfc 3530 section 14.2.33: */
  1986. nfs4_lock_state();
  1987. conf = find_confirmed_client_by_name(&clname, nn);
  1988. if (conf) {
  1989. /* case 0: */
  1990. status = nfserr_clid_inuse;
  1991. if (clp_used_exchangeid(conf))
  1992. goto out;
  1993. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1994. char addr_str[INET6_ADDRSTRLEN];
  1995. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  1996. sizeof(addr_str));
  1997. dprintk("NFSD: setclientid: string in use by client "
  1998. "at %s\n", addr_str);
  1999. goto out;
  2000. }
  2001. }
  2002. unconf = find_unconfirmed_client_by_name(&clname, nn);
  2003. if (unconf)
  2004. expire_client(unconf);
  2005. status = nfserr_jukebox;
  2006. new = create_client(clname, rqstp, &clverifier);
  2007. if (new == NULL)
  2008. goto out;
  2009. if (conf && same_verf(&conf->cl_verifier, &clverifier))
  2010. /* case 1: probable callback update */
  2011. copy_clid(new, conf);
  2012. else /* case 4 (new client) or cases 2, 3 (client reboot): */
  2013. gen_clid(new, nn);
  2014. new->cl_minorversion = 0;
  2015. gen_callback(new, setclid, rqstp);
  2016. add_to_unconfirmed(new);
  2017. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  2018. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  2019. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  2020. status = nfs_ok;
  2021. out:
  2022. nfs4_unlock_state();
  2023. return status;
  2024. }
  2025. __be32
  2026. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  2027. struct nfsd4_compound_state *cstate,
  2028. struct nfsd4_setclientid_confirm *setclientid_confirm)
  2029. {
  2030. struct nfs4_client *conf, *unconf;
  2031. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  2032. clientid_t * clid = &setclientid_confirm->sc_clientid;
  2033. __be32 status;
  2034. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2035. if (STALE_CLIENTID(clid, nn))
  2036. return nfserr_stale_clientid;
  2037. nfs4_lock_state();
  2038. conf = find_confirmed_client(clid, false, nn);
  2039. unconf = find_unconfirmed_client(clid, false, nn);
  2040. /*
  2041. * We try hard to give out unique clientid's, so if we get an
  2042. * attempt to confirm the same clientid with a different cred,
  2043. * there's a bug somewhere. Let's charitably assume it's our
  2044. * bug.
  2045. */
  2046. status = nfserr_serverfault;
  2047. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  2048. goto out;
  2049. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  2050. goto out;
  2051. /* cases below refer to rfc 3530 section 14.2.34: */
  2052. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  2053. if (conf && !unconf) /* case 2: probable retransmit */
  2054. status = nfs_ok;
  2055. else /* case 4: client hasn't noticed we rebooted yet? */
  2056. status = nfserr_stale_clientid;
  2057. goto out;
  2058. }
  2059. status = nfs_ok;
  2060. if (conf) { /* case 1: callback update */
  2061. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  2062. nfsd4_probe_callback(conf);
  2063. expire_client(unconf);
  2064. } else { /* case 3: normal case; new or rebooted client */
  2065. conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2066. if (conf) {
  2067. status = mark_client_expired(conf);
  2068. if (status)
  2069. goto out;
  2070. expire_client(conf);
  2071. }
  2072. move_to_confirmed(unconf);
  2073. nfsd4_probe_callback(unconf);
  2074. }
  2075. out:
  2076. nfs4_unlock_state();
  2077. return status;
  2078. }
  2079. static struct nfs4_file *nfsd4_alloc_file(void)
  2080. {
  2081. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2082. }
  2083. /* OPEN Share state helper functions */
  2084. static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
  2085. {
  2086. unsigned int hashval = file_hashval(ino);
  2087. atomic_set(&fp->fi_ref, 1);
  2088. INIT_LIST_HEAD(&fp->fi_stateids);
  2089. INIT_LIST_HEAD(&fp->fi_delegations);
  2090. fp->fi_inode = igrab(ino);
  2091. fp->fi_had_conflict = false;
  2092. fp->fi_lease = NULL;
  2093. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2094. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2095. spin_lock(&recall_lock);
  2096. hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
  2097. spin_unlock(&recall_lock);
  2098. }
  2099. static void
  2100. nfsd4_free_slab(struct kmem_cache **slab)
  2101. {
  2102. if (*slab == NULL)
  2103. return;
  2104. kmem_cache_destroy(*slab);
  2105. *slab = NULL;
  2106. }
  2107. void
  2108. nfsd4_free_slabs(void)
  2109. {
  2110. nfsd4_free_slab(&openowner_slab);
  2111. nfsd4_free_slab(&lockowner_slab);
  2112. nfsd4_free_slab(&file_slab);
  2113. nfsd4_free_slab(&stateid_slab);
  2114. nfsd4_free_slab(&deleg_slab);
  2115. }
  2116. int
  2117. nfsd4_init_slabs(void)
  2118. {
  2119. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2120. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2121. if (openowner_slab == NULL)
  2122. goto out_nomem;
  2123. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2124. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2125. if (lockowner_slab == NULL)
  2126. goto out_nomem;
  2127. file_slab = kmem_cache_create("nfsd4_files",
  2128. sizeof(struct nfs4_file), 0, 0, NULL);
  2129. if (file_slab == NULL)
  2130. goto out_nomem;
  2131. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2132. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2133. if (stateid_slab == NULL)
  2134. goto out_nomem;
  2135. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2136. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2137. if (deleg_slab == NULL)
  2138. goto out_nomem;
  2139. return 0;
  2140. out_nomem:
  2141. nfsd4_free_slabs();
  2142. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2143. return -ENOMEM;
  2144. }
  2145. void nfs4_free_openowner(struct nfs4_openowner *oo)
  2146. {
  2147. kfree(oo->oo_owner.so_owner.data);
  2148. kmem_cache_free(openowner_slab, oo);
  2149. }
  2150. void nfs4_free_lockowner(struct nfs4_lockowner *lo)
  2151. {
  2152. kfree(lo->lo_owner.so_owner.data);
  2153. kmem_cache_free(lockowner_slab, lo);
  2154. }
  2155. static void init_nfs4_replay(struct nfs4_replay *rp)
  2156. {
  2157. rp->rp_status = nfserr_serverfault;
  2158. rp->rp_buflen = 0;
  2159. rp->rp_buf = rp->rp_ibuf;
  2160. }
  2161. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2162. {
  2163. struct nfs4_stateowner *sop;
  2164. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2165. if (!sop)
  2166. return NULL;
  2167. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2168. if (!sop->so_owner.data) {
  2169. kmem_cache_free(slab, sop);
  2170. return NULL;
  2171. }
  2172. sop->so_owner.len = owner->len;
  2173. INIT_LIST_HEAD(&sop->so_stateids);
  2174. sop->so_client = clp;
  2175. init_nfs4_replay(&sop->so_replay);
  2176. return sop;
  2177. }
  2178. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2179. {
  2180. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2181. list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  2182. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2183. }
  2184. static struct nfs4_openowner *
  2185. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  2186. struct nfs4_openowner *oo;
  2187. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2188. if (!oo)
  2189. return NULL;
  2190. oo->oo_owner.so_is_open_owner = 1;
  2191. oo->oo_owner.so_seqid = open->op_seqid;
  2192. oo->oo_flags = NFS4_OO_NEW;
  2193. oo->oo_time = 0;
  2194. oo->oo_last_closed_stid = NULL;
  2195. INIT_LIST_HEAD(&oo->oo_close_lru);
  2196. hash_openowner(oo, clp, strhashval);
  2197. return oo;
  2198. }
  2199. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2200. struct nfs4_openowner *oo = open->op_openowner;
  2201. stp->st_stid.sc_type = NFS4_OPEN_STID;
  2202. INIT_LIST_HEAD(&stp->st_lockowners);
  2203. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2204. list_add(&stp->st_perfile, &fp->fi_stateids);
  2205. stp->st_stateowner = &oo->oo_owner;
  2206. get_nfs4_file(fp);
  2207. stp->st_file = fp;
  2208. stp->st_access_bmap = 0;
  2209. stp->st_deny_bmap = 0;
  2210. set_access(open->op_share_access, stp);
  2211. set_deny(open->op_share_deny, stp);
  2212. stp->st_openstp = NULL;
  2213. }
  2214. static void
  2215. move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
  2216. {
  2217. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2218. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2219. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  2220. oo->oo_time = get_seconds();
  2221. }
  2222. static int
  2223. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  2224. clientid_t *clid)
  2225. {
  2226. return (sop->so_owner.len == owner->len) &&
  2227. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  2228. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  2229. }
  2230. static struct nfs4_openowner *
  2231. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  2232. bool sessions, struct nfsd_net *nn)
  2233. {
  2234. struct nfs4_stateowner *so;
  2235. struct nfs4_openowner *oo;
  2236. struct nfs4_client *clp;
  2237. list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  2238. if (!so->so_is_open_owner)
  2239. continue;
  2240. if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
  2241. oo = openowner(so);
  2242. clp = oo->oo_owner.so_client;
  2243. if ((bool)clp->cl_minorversion != sessions)
  2244. return NULL;
  2245. renew_client(oo->oo_owner.so_client);
  2246. return oo;
  2247. }
  2248. }
  2249. return NULL;
  2250. }
  2251. /* search file_hashtbl[] for file */
  2252. static struct nfs4_file *
  2253. find_file(struct inode *ino)
  2254. {
  2255. unsigned int hashval = file_hashval(ino);
  2256. struct nfs4_file *fp;
  2257. spin_lock(&recall_lock);
  2258. hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2259. if (fp->fi_inode == ino) {
  2260. get_nfs4_file(fp);
  2261. spin_unlock(&recall_lock);
  2262. return fp;
  2263. }
  2264. }
  2265. spin_unlock(&recall_lock);
  2266. return NULL;
  2267. }
  2268. /*
  2269. * Called to check deny when READ with all zero stateid or
  2270. * WRITE with all zero or all one stateid
  2271. */
  2272. static __be32
  2273. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2274. {
  2275. struct inode *ino = current_fh->fh_dentry->d_inode;
  2276. struct nfs4_file *fp;
  2277. struct nfs4_ol_stateid *stp;
  2278. __be32 ret;
  2279. fp = find_file(ino);
  2280. if (!fp)
  2281. return nfs_ok;
  2282. ret = nfserr_locked;
  2283. /* Search for conflicting share reservations */
  2284. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  2285. if (test_deny(deny_type, stp) ||
  2286. test_deny(NFS4_SHARE_DENY_BOTH, stp))
  2287. goto out;
  2288. }
  2289. ret = nfs_ok;
  2290. out:
  2291. put_nfs4_file(fp);
  2292. return ret;
  2293. }
  2294. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2295. {
  2296. struct nfs4_client *clp = dp->dl_stid.sc_client;
  2297. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2298. /* We're assuming the state code never drops its reference
  2299. * without first removing the lease. Since we're in this lease
  2300. * callback (and since the lease code is serialized by the kernel
  2301. * lock) we know the server hasn't removed the lease yet, we know
  2302. * it's safe to take a reference: */
  2303. atomic_inc(&dp->dl_count);
  2304. list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
  2305. /* only place dl_time is set. protected by lock_flocks*/
  2306. dp->dl_time = get_seconds();
  2307. nfsd4_cb_recall(dp);
  2308. }
  2309. /* Called from break_lease() with lock_flocks() held. */
  2310. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2311. {
  2312. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2313. struct nfs4_delegation *dp;
  2314. if (!fp) {
  2315. WARN(1, "(%p)->fl_owner NULL\n", fl);
  2316. return;
  2317. }
  2318. if (fp->fi_had_conflict) {
  2319. WARN(1, "duplicate break on %p\n", fp);
  2320. return;
  2321. }
  2322. /*
  2323. * We don't want the locks code to timeout the lease for us;
  2324. * we'll remove it ourself if a delegation isn't returned
  2325. * in time:
  2326. */
  2327. fl->fl_break_time = 0;
  2328. spin_lock(&recall_lock);
  2329. fp->fi_had_conflict = true;
  2330. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2331. nfsd_break_one_deleg(dp);
  2332. spin_unlock(&recall_lock);
  2333. }
  2334. static
  2335. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2336. {
  2337. if (arg & F_UNLCK)
  2338. return lease_modify(onlist, arg);
  2339. else
  2340. return -EAGAIN;
  2341. }
  2342. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2343. .lm_break = nfsd_break_deleg_cb,
  2344. .lm_change = nfsd_change_deleg_cb,
  2345. };
  2346. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2347. {
  2348. if (nfsd4_has_session(cstate))
  2349. return nfs_ok;
  2350. if (seqid == so->so_seqid - 1)
  2351. return nfserr_replay_me;
  2352. if (seqid == so->so_seqid)
  2353. return nfs_ok;
  2354. return nfserr_bad_seqid;
  2355. }
  2356. __be32
  2357. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  2358. struct nfsd4_open *open, struct nfsd_net *nn)
  2359. {
  2360. clientid_t *clientid = &open->op_clientid;
  2361. struct nfs4_client *clp = NULL;
  2362. unsigned int strhashval;
  2363. struct nfs4_openowner *oo = NULL;
  2364. __be32 status;
  2365. if (STALE_CLIENTID(&open->op_clientid, nn))
  2366. return nfserr_stale_clientid;
  2367. /*
  2368. * In case we need it later, after we've already created the
  2369. * file and don't want to risk a further failure:
  2370. */
  2371. open->op_file = nfsd4_alloc_file();
  2372. if (open->op_file == NULL)
  2373. return nfserr_jukebox;
  2374. strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
  2375. oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
  2376. open->op_openowner = oo;
  2377. if (!oo) {
  2378. clp = find_confirmed_client(clientid, cstate->minorversion,
  2379. nn);
  2380. if (clp == NULL)
  2381. return nfserr_expired;
  2382. goto new_owner;
  2383. }
  2384. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  2385. /* Replace unconfirmed owners without checking for replay. */
  2386. clp = oo->oo_owner.so_client;
  2387. release_openowner(oo);
  2388. open->op_openowner = NULL;
  2389. goto new_owner;
  2390. }
  2391. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  2392. if (status)
  2393. return status;
  2394. clp = oo->oo_owner.so_client;
  2395. goto alloc_stateid;
  2396. new_owner:
  2397. oo = alloc_init_open_stateowner(strhashval, clp, open);
  2398. if (oo == NULL)
  2399. return nfserr_jukebox;
  2400. open->op_openowner = oo;
  2401. alloc_stateid:
  2402. open->op_stp = nfs4_alloc_stateid(clp);
  2403. if (!open->op_stp)
  2404. return nfserr_jukebox;
  2405. return nfs_ok;
  2406. }
  2407. static inline __be32
  2408. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  2409. {
  2410. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  2411. return nfserr_openmode;
  2412. else
  2413. return nfs_ok;
  2414. }
  2415. static int share_access_to_flags(u32 share_access)
  2416. {
  2417. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  2418. }
  2419. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  2420. {
  2421. struct nfs4_stid *ret;
  2422. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  2423. if (!ret)
  2424. return NULL;
  2425. return delegstateid(ret);
  2426. }
  2427. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  2428. {
  2429. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  2430. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  2431. }
  2432. static __be32
  2433. nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
  2434. struct nfs4_delegation **dp)
  2435. {
  2436. int flags;
  2437. __be32 status = nfserr_bad_stateid;
  2438. *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
  2439. if (*dp == NULL)
  2440. goto out;
  2441. flags = share_access_to_flags(open->op_share_access);
  2442. status = nfs4_check_delegmode(*dp, flags);
  2443. if (status)
  2444. *dp = NULL;
  2445. out:
  2446. if (!nfsd4_is_deleg_cur(open))
  2447. return nfs_ok;
  2448. if (status)
  2449. return status;
  2450. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2451. return nfs_ok;
  2452. }
  2453. static __be32
  2454. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
  2455. {
  2456. struct nfs4_ol_stateid *local;
  2457. struct nfs4_openowner *oo = open->op_openowner;
  2458. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2459. /* ignore lock owners */
  2460. if (local->st_stateowner->so_is_open_owner == 0)
  2461. continue;
  2462. /* remember if we have seen this open owner */
  2463. if (local->st_stateowner == &oo->oo_owner)
  2464. *stpp = local;
  2465. /* check for conflicting share reservations */
  2466. if (!test_share(local, open))
  2467. return nfserr_share_denied;
  2468. }
  2469. return nfs_ok;
  2470. }
  2471. static inline int nfs4_access_to_access(u32 nfs4_access)
  2472. {
  2473. int flags = 0;
  2474. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  2475. flags |= NFSD_MAY_READ;
  2476. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  2477. flags |= NFSD_MAY_WRITE;
  2478. return flags;
  2479. }
  2480. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  2481. struct svc_fh *cur_fh, struct nfsd4_open *open)
  2482. {
  2483. __be32 status;
  2484. int oflag = nfs4_access_to_omode(open->op_share_access);
  2485. int access = nfs4_access_to_access(open->op_share_access);
  2486. if (!fp->fi_fds[oflag]) {
  2487. status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
  2488. &fp->fi_fds[oflag]);
  2489. if (status)
  2490. return status;
  2491. }
  2492. nfs4_file_get_access(fp, oflag);
  2493. return nfs_ok;
  2494. }
  2495. static inline __be32
  2496. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2497. struct nfsd4_open *open)
  2498. {
  2499. struct iattr iattr = {
  2500. .ia_valid = ATTR_SIZE,
  2501. .ia_size = 0,
  2502. };
  2503. if (!open->op_truncate)
  2504. return 0;
  2505. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2506. return nfserr_inval;
  2507. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2508. }
  2509. static __be32
  2510. nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
  2511. {
  2512. u32 op_share_access = open->op_share_access;
  2513. bool new_access;
  2514. __be32 status;
  2515. new_access = !test_access(op_share_access, stp);
  2516. if (new_access) {
  2517. status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
  2518. if (status)
  2519. return status;
  2520. }
  2521. status = nfsd4_truncate(rqstp, cur_fh, open);
  2522. if (status) {
  2523. if (new_access) {
  2524. int oflag = nfs4_access_to_omode(op_share_access);
  2525. nfs4_file_put_access(fp, oflag);
  2526. }
  2527. return status;
  2528. }
  2529. /* remember the open */
  2530. set_access(op_share_access, stp);
  2531. set_deny(open->op_share_deny, stp);
  2532. return nfs_ok;
  2533. }
  2534. static void
  2535. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  2536. {
  2537. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2538. }
  2539. /* Should we give out recallable state?: */
  2540. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  2541. {
  2542. if (clp->cl_cb_state == NFSD4_CB_UP)
  2543. return true;
  2544. /*
  2545. * In the sessions case, since we don't have to establish a
  2546. * separate connection for callbacks, we assume it's OK
  2547. * until we hear otherwise:
  2548. */
  2549. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  2550. }
  2551. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
  2552. {
  2553. struct file_lock *fl;
  2554. fl = locks_alloc_lock();
  2555. if (!fl)
  2556. return NULL;
  2557. locks_init_lock(fl);
  2558. fl->fl_lmops = &nfsd_lease_mng_ops;
  2559. fl->fl_flags = FL_LEASE;
  2560. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2561. fl->fl_end = OFFSET_MAX;
  2562. fl->fl_owner = (fl_owner_t)(dp->dl_file);
  2563. fl->fl_pid = current->tgid;
  2564. return fl;
  2565. }
  2566. static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
  2567. {
  2568. struct nfs4_file *fp = dp->dl_file;
  2569. struct file_lock *fl;
  2570. int status;
  2571. fl = nfs4_alloc_init_lease(dp, flag);
  2572. if (!fl)
  2573. return -ENOMEM;
  2574. fl->fl_file = find_readable_file(fp);
  2575. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2576. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
  2577. if (status) {
  2578. list_del_init(&dp->dl_perclnt);
  2579. locks_free_lock(fl);
  2580. return -ENOMEM;
  2581. }
  2582. fp->fi_lease = fl;
  2583. fp->fi_deleg_file = get_file(fl->fl_file);
  2584. atomic_set(&fp->fi_delegees, 1);
  2585. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2586. return 0;
  2587. }
  2588. static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  2589. {
  2590. struct nfs4_file *fp = dp->dl_file;
  2591. if (!fp->fi_lease)
  2592. return nfs4_setlease(dp, flag);
  2593. spin_lock(&recall_lock);
  2594. if (fp->fi_had_conflict) {
  2595. spin_unlock(&recall_lock);
  2596. return -EAGAIN;
  2597. }
  2598. atomic_inc(&fp->fi_delegees);
  2599. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2600. spin_unlock(&recall_lock);
  2601. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2602. return 0;
  2603. }
  2604. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  2605. {
  2606. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2607. if (status == -EAGAIN)
  2608. open->op_why_no_deleg = WND4_CONTENTION;
  2609. else {
  2610. open->op_why_no_deleg = WND4_RESOURCE;
  2611. switch (open->op_deleg_want) {
  2612. case NFS4_SHARE_WANT_READ_DELEG:
  2613. case NFS4_SHARE_WANT_WRITE_DELEG:
  2614. case NFS4_SHARE_WANT_ANY_DELEG:
  2615. break;
  2616. case NFS4_SHARE_WANT_CANCEL:
  2617. open->op_why_no_deleg = WND4_CANCELLED;
  2618. break;
  2619. case NFS4_SHARE_WANT_NO_DELEG:
  2620. WARN_ON_ONCE(1);
  2621. }
  2622. }
  2623. }
  2624. /*
  2625. * Attempt to hand out a delegation.
  2626. */
  2627. static void
  2628. nfs4_open_delegation(struct net *net, struct svc_fh *fh,
  2629. struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
  2630. {
  2631. struct nfs4_delegation *dp;
  2632. struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
  2633. int cb_up;
  2634. int status = 0, flag = 0;
  2635. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  2636. flag = NFS4_OPEN_DELEGATE_NONE;
  2637. open->op_recall = 0;
  2638. switch (open->op_claim_type) {
  2639. case NFS4_OPEN_CLAIM_PREVIOUS:
  2640. if (!cb_up)
  2641. open->op_recall = 1;
  2642. flag = open->op_delegate_type;
  2643. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2644. goto out;
  2645. break;
  2646. case NFS4_OPEN_CLAIM_NULL:
  2647. /* Let's not give out any delegations till everyone's
  2648. * had the chance to reclaim theirs.... */
  2649. if (locks_in_grace(net))
  2650. goto out;
  2651. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  2652. goto out;
  2653. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2654. flag = NFS4_OPEN_DELEGATE_WRITE;
  2655. else
  2656. flag = NFS4_OPEN_DELEGATE_READ;
  2657. break;
  2658. default:
  2659. goto out;
  2660. }
  2661. dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
  2662. if (dp == NULL)
  2663. goto out_no_deleg;
  2664. status = nfs4_set_delegation(dp, flag);
  2665. if (status)
  2666. goto out_free;
  2667. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  2668. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  2669. STATEID_VAL(&dp->dl_stid.sc_stateid));
  2670. out:
  2671. open->op_delegate_type = flag;
  2672. if (flag == NFS4_OPEN_DELEGATE_NONE) {
  2673. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  2674. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2675. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2676. /* 4.1 client asking for a delegation? */
  2677. if (open->op_deleg_want)
  2678. nfsd4_open_deleg_none_ext(open, status);
  2679. }
  2680. return;
  2681. out_free:
  2682. unhash_stid(&dp->dl_stid);
  2683. nfs4_put_delegation(dp);
  2684. out_no_deleg:
  2685. flag = NFS4_OPEN_DELEGATE_NONE;
  2686. goto out;
  2687. }
  2688. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  2689. struct nfs4_delegation *dp)
  2690. {
  2691. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  2692. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2693. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2694. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  2695. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  2696. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2697. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2698. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  2699. }
  2700. /* Otherwise the client must be confused wanting a delegation
  2701. * it already has, therefore we don't return
  2702. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  2703. */
  2704. }
  2705. /*
  2706. * called with nfs4_lock_state() held.
  2707. */
  2708. __be32
  2709. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2710. {
  2711. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2712. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  2713. struct nfs4_file *fp = NULL;
  2714. struct inode *ino = current_fh->fh_dentry->d_inode;
  2715. struct nfs4_ol_stateid *stp = NULL;
  2716. struct nfs4_delegation *dp = NULL;
  2717. __be32 status;
  2718. /*
  2719. * Lookup file; if found, lookup stateid and check open request,
  2720. * and check for delegations in the process of being recalled.
  2721. * If not found, create the nfs4_file struct
  2722. */
  2723. fp = find_file(ino);
  2724. if (fp) {
  2725. if ((status = nfs4_check_open(fp, open, &stp)))
  2726. goto out;
  2727. status = nfs4_check_deleg(cl, open, &dp);
  2728. if (status)
  2729. goto out;
  2730. } else {
  2731. status = nfserr_bad_stateid;
  2732. if (nfsd4_is_deleg_cur(open))
  2733. goto out;
  2734. status = nfserr_jukebox;
  2735. fp = open->op_file;
  2736. open->op_file = NULL;
  2737. nfsd4_init_file(fp, ino);
  2738. }
  2739. /*
  2740. * OPEN the file, or upgrade an existing OPEN.
  2741. * If truncate fails, the OPEN fails.
  2742. */
  2743. if (stp) {
  2744. /* Stateid was found, this is an OPEN upgrade */
  2745. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  2746. if (status)
  2747. goto out;
  2748. } else {
  2749. status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
  2750. if (status)
  2751. goto out;
  2752. status = nfsd4_truncate(rqstp, current_fh, open);
  2753. if (status)
  2754. goto out;
  2755. stp = open->op_stp;
  2756. open->op_stp = NULL;
  2757. init_open_stateid(stp, fp, open);
  2758. }
  2759. update_stateid(&stp->st_stid.sc_stateid);
  2760. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  2761. if (nfsd4_has_session(&resp->cstate)) {
  2762. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2763. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  2764. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2765. open->op_why_no_deleg = WND4_NOT_WANTED;
  2766. goto nodeleg;
  2767. }
  2768. }
  2769. /*
  2770. * Attempt to hand out a delegation. No error return, because the
  2771. * OPEN succeeds even if we fail.
  2772. */
  2773. nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
  2774. nodeleg:
  2775. status = nfs_ok;
  2776. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  2777. STATEID_VAL(&stp->st_stid.sc_stateid));
  2778. out:
  2779. /* 4.1 client trying to upgrade/downgrade delegation? */
  2780. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  2781. open->op_deleg_want)
  2782. nfsd4_deleg_xgrade_none_ext(open, dp);
  2783. if (fp)
  2784. put_nfs4_file(fp);
  2785. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2786. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  2787. /*
  2788. * To finish the open response, we just need to set the rflags.
  2789. */
  2790. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2791. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  2792. !nfsd4_has_session(&resp->cstate))
  2793. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2794. return status;
  2795. }
  2796. void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
  2797. {
  2798. if (open->op_openowner) {
  2799. struct nfs4_openowner *oo = open->op_openowner;
  2800. if (!list_empty(&oo->oo_owner.so_stateids))
  2801. list_del_init(&oo->oo_close_lru);
  2802. if (oo->oo_flags & NFS4_OO_NEW) {
  2803. if (status) {
  2804. release_openowner(oo);
  2805. open->op_openowner = NULL;
  2806. } else
  2807. oo->oo_flags &= ~NFS4_OO_NEW;
  2808. }
  2809. }
  2810. if (open->op_file)
  2811. nfsd4_free_file(open->op_file);
  2812. if (open->op_stp)
  2813. free_generic_stateid(open->op_stp);
  2814. }
  2815. static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
  2816. {
  2817. struct nfs4_client *found;
  2818. if (STALE_CLIENTID(clid, nn))
  2819. return nfserr_stale_clientid;
  2820. found = find_confirmed_client(clid, session, nn);
  2821. if (clp)
  2822. *clp = found;
  2823. return found ? nfs_ok : nfserr_expired;
  2824. }
  2825. __be32
  2826. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2827. clientid_t *clid)
  2828. {
  2829. struct nfs4_client *clp;
  2830. __be32 status;
  2831. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2832. nfs4_lock_state();
  2833. dprintk("process_renew(%08x/%08x): starting\n",
  2834. clid->cl_boot, clid->cl_id);
  2835. status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
  2836. if (status)
  2837. goto out;
  2838. status = nfserr_cb_path_down;
  2839. if (!list_empty(&clp->cl_delegations)
  2840. && clp->cl_cb_state != NFSD4_CB_UP)
  2841. goto out;
  2842. status = nfs_ok;
  2843. out:
  2844. nfs4_unlock_state();
  2845. return status;
  2846. }
  2847. static void
  2848. nfsd4_end_grace(struct nfsd_net *nn)
  2849. {
  2850. /* do nothing if grace period already ended */
  2851. if (nn->grace_ended)
  2852. return;
  2853. dprintk("NFSD: end of grace period\n");
  2854. nn->grace_ended = true;
  2855. nfsd4_record_grace_done(nn, nn->boot_time);
  2856. locks_end_grace(&nn->nfsd4_manager);
  2857. /*
  2858. * Now that every NFSv4 client has had the chance to recover and
  2859. * to see the (possibly new, possibly shorter) lease time, we
  2860. * can safely set the next grace time to the current lease time:
  2861. */
  2862. nn->nfsd4_grace = nn->nfsd4_lease;
  2863. }
  2864. static time_t
  2865. nfs4_laundromat(struct nfsd_net *nn)
  2866. {
  2867. struct nfs4_client *clp;
  2868. struct nfs4_openowner *oo;
  2869. struct nfs4_delegation *dp;
  2870. struct list_head *pos, *next, reaplist;
  2871. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  2872. time_t t, clientid_val = nn->nfsd4_lease;
  2873. time_t u, test_val = nn->nfsd4_lease;
  2874. nfs4_lock_state();
  2875. dprintk("NFSD: laundromat service - starting\n");
  2876. nfsd4_end_grace(nn);
  2877. INIT_LIST_HEAD(&reaplist);
  2878. spin_lock(&nn->client_lock);
  2879. list_for_each_safe(pos, next, &nn->client_lru) {
  2880. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2881. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2882. t = clp->cl_time - cutoff;
  2883. if (clientid_val > t)
  2884. clientid_val = t;
  2885. break;
  2886. }
  2887. if (mark_client_expired_locked(clp)) {
  2888. dprintk("NFSD: client in use (clientid %08x)\n",
  2889. clp->cl_clientid.cl_id);
  2890. continue;
  2891. }
  2892. list_move(&clp->cl_lru, &reaplist);
  2893. }
  2894. spin_unlock(&nn->client_lock);
  2895. list_for_each_safe(pos, next, &reaplist) {
  2896. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2897. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2898. clp->cl_clientid.cl_id);
  2899. expire_client(clp);
  2900. }
  2901. spin_lock(&recall_lock);
  2902. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  2903. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2904. if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
  2905. continue;
  2906. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2907. u = dp->dl_time - cutoff;
  2908. if (test_val > u)
  2909. test_val = u;
  2910. break;
  2911. }
  2912. list_move(&dp->dl_recall_lru, &reaplist);
  2913. }
  2914. spin_unlock(&recall_lock);
  2915. list_for_each_safe(pos, next, &reaplist) {
  2916. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2917. revoke_delegation(dp);
  2918. }
  2919. test_val = nn->nfsd4_lease;
  2920. list_for_each_safe(pos, next, &nn->close_lru) {
  2921. oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
  2922. if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
  2923. u = oo->oo_time - cutoff;
  2924. if (test_val > u)
  2925. test_val = u;
  2926. break;
  2927. }
  2928. release_openowner(oo);
  2929. }
  2930. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2931. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2932. nfs4_unlock_state();
  2933. return clientid_val;
  2934. }
  2935. static struct workqueue_struct *laundry_wq;
  2936. static void laundromat_main(struct work_struct *);
  2937. static void
  2938. laundromat_main(struct work_struct *laundry)
  2939. {
  2940. time_t t;
  2941. struct delayed_work *dwork = container_of(laundry, struct delayed_work,
  2942. work);
  2943. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  2944. laundromat_work);
  2945. t = nfs4_laundromat(nn);
  2946. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2947. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  2948. }
  2949. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  2950. {
  2951. if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
  2952. return nfserr_bad_stateid;
  2953. return nfs_ok;
  2954. }
  2955. static inline int
  2956. access_permit_read(struct nfs4_ol_stateid *stp)
  2957. {
  2958. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  2959. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  2960. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  2961. }
  2962. static inline int
  2963. access_permit_write(struct nfs4_ol_stateid *stp)
  2964. {
  2965. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  2966. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  2967. }
  2968. static
  2969. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  2970. {
  2971. __be32 status = nfserr_openmode;
  2972. /* For lock stateid's, we test the parent open, not the lock: */
  2973. if (stp->st_openstp)
  2974. stp = stp->st_openstp;
  2975. if ((flags & WR_STATE) && !access_permit_write(stp))
  2976. goto out;
  2977. if ((flags & RD_STATE) && !access_permit_read(stp))
  2978. goto out;
  2979. status = nfs_ok;
  2980. out:
  2981. return status;
  2982. }
  2983. static inline __be32
  2984. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  2985. {
  2986. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2987. return nfs_ok;
  2988. else if (locks_in_grace(net)) {
  2989. /* Answer in remaining cases depends on existence of
  2990. * conflicting state; so we must wait out the grace period. */
  2991. return nfserr_grace;
  2992. } else if (flags & WR_STATE)
  2993. return nfs4_share_conflict(current_fh,
  2994. NFS4_SHARE_DENY_WRITE);
  2995. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2996. return nfs4_share_conflict(current_fh,
  2997. NFS4_SHARE_DENY_READ);
  2998. }
  2999. /*
  3000. * Allow READ/WRITE during grace period on recovered state only for files
  3001. * that are not able to provide mandatory locking.
  3002. */
  3003. static inline int
  3004. grace_disallows_io(struct net *net, struct inode *inode)
  3005. {
  3006. return locks_in_grace(net) && mandatory_lock(inode);
  3007. }
  3008. /* Returns true iff a is later than b: */
  3009. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  3010. {
  3011. return (s32)a->si_generation - (s32)b->si_generation > 0;
  3012. }
  3013. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  3014. {
  3015. /*
  3016. * When sessions are used the stateid generation number is ignored
  3017. * when it is zero.
  3018. */
  3019. if (has_session && in->si_generation == 0)
  3020. return nfs_ok;
  3021. if (in->si_generation == ref->si_generation)
  3022. return nfs_ok;
  3023. /* If the client sends us a stateid from the future, it's buggy: */
  3024. if (stateid_generation_after(in, ref))
  3025. return nfserr_bad_stateid;
  3026. /*
  3027. * However, we could see a stateid from the past, even from a
  3028. * non-buggy client. For example, if the client sends a lock
  3029. * while some IO is outstanding, the lock may bump si_generation
  3030. * while the IO is still in flight. The client could avoid that
  3031. * situation by waiting for responses on all the IO requests,
  3032. * but better performance may result in retrying IO that
  3033. * receives an old_stateid error if requests are rarely
  3034. * reordered in flight:
  3035. */
  3036. return nfserr_old_stateid;
  3037. }
  3038. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  3039. {
  3040. struct nfs4_stid *s;
  3041. struct nfs4_ol_stateid *ols;
  3042. __be32 status;
  3043. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3044. return nfserr_bad_stateid;
  3045. /* Client debugging aid. */
  3046. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  3047. char addr_str[INET6_ADDRSTRLEN];
  3048. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  3049. sizeof(addr_str));
  3050. pr_warn_ratelimited("NFSD: client %s testing state ID "
  3051. "with incorrect client ID\n", addr_str);
  3052. return nfserr_bad_stateid;
  3053. }
  3054. s = find_stateid(cl, stateid);
  3055. if (!s)
  3056. return nfserr_bad_stateid;
  3057. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3058. if (status)
  3059. return status;
  3060. switch (s->sc_type) {
  3061. case NFS4_DELEG_STID:
  3062. return nfs_ok;
  3063. case NFS4_REVOKED_DELEG_STID:
  3064. return nfserr_deleg_revoked;
  3065. case NFS4_OPEN_STID:
  3066. case NFS4_LOCK_STID:
  3067. ols = openlockstateid(s);
  3068. if (ols->st_stateowner->so_is_open_owner
  3069. && !(openowner(ols->st_stateowner)->oo_flags
  3070. & NFS4_OO_CONFIRMED))
  3071. return nfserr_bad_stateid;
  3072. return nfs_ok;
  3073. default:
  3074. printk("unknown stateid type %x\n", s->sc_type);
  3075. case NFS4_CLOSED_STID:
  3076. return nfserr_bad_stateid;
  3077. }
  3078. }
  3079. static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
  3080. struct nfs4_stid **s, bool sessions,
  3081. struct nfsd_net *nn)
  3082. {
  3083. struct nfs4_client *cl;
  3084. __be32 status;
  3085. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3086. return nfserr_bad_stateid;
  3087. status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
  3088. nn, &cl);
  3089. if (status == nfserr_stale_clientid)
  3090. return nfserr_stale_stateid;
  3091. if (status)
  3092. return status;
  3093. *s = find_stateid_by_type(cl, stateid, typemask);
  3094. if (!*s)
  3095. return nfserr_bad_stateid;
  3096. return nfs_ok;
  3097. }
  3098. /*
  3099. * Checks for stateid operations
  3100. */
  3101. __be32
  3102. nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
  3103. stateid_t *stateid, int flags, struct file **filpp)
  3104. {
  3105. struct nfs4_stid *s;
  3106. struct nfs4_ol_stateid *stp = NULL;
  3107. struct nfs4_delegation *dp = NULL;
  3108. struct svc_fh *current_fh = &cstate->current_fh;
  3109. struct inode *ino = current_fh->fh_dentry->d_inode;
  3110. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3111. __be32 status;
  3112. if (filpp)
  3113. *filpp = NULL;
  3114. if (grace_disallows_io(net, ino))
  3115. return nfserr_grace;
  3116. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3117. return check_special_stateids(net, current_fh, stateid, flags);
  3118. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  3119. &s, cstate->minorversion, nn);
  3120. if (status)
  3121. return status;
  3122. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3123. if (status)
  3124. goto out;
  3125. switch (s->sc_type) {
  3126. case NFS4_DELEG_STID:
  3127. dp = delegstateid(s);
  3128. status = nfs4_check_delegmode(dp, flags);
  3129. if (status)
  3130. goto out;
  3131. if (filpp) {
  3132. *filpp = dp->dl_file->fi_deleg_file;
  3133. if (!*filpp) {
  3134. WARN_ON_ONCE(1);
  3135. status = nfserr_serverfault;
  3136. goto out;
  3137. }
  3138. }
  3139. break;
  3140. case NFS4_OPEN_STID:
  3141. case NFS4_LOCK_STID:
  3142. stp = openlockstateid(s);
  3143. status = nfs4_check_fh(current_fh, stp);
  3144. if (status)
  3145. goto out;
  3146. if (stp->st_stateowner->so_is_open_owner
  3147. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3148. goto out;
  3149. status = nfs4_check_openmode(stp, flags);
  3150. if (status)
  3151. goto out;
  3152. if (filpp) {
  3153. if (flags & RD_STATE)
  3154. *filpp = find_readable_file(stp->st_file);
  3155. else
  3156. *filpp = find_writeable_file(stp->st_file);
  3157. }
  3158. break;
  3159. default:
  3160. return nfserr_bad_stateid;
  3161. }
  3162. status = nfs_ok;
  3163. out:
  3164. return status;
  3165. }
  3166. static __be32
  3167. nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
  3168. {
  3169. if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
  3170. return nfserr_locks_held;
  3171. release_lock_stateid(stp);
  3172. return nfs_ok;
  3173. }
  3174. /*
  3175. * Test if the stateid is valid
  3176. */
  3177. __be32
  3178. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3179. struct nfsd4_test_stateid *test_stateid)
  3180. {
  3181. struct nfsd4_test_stateid_id *stateid;
  3182. struct nfs4_client *cl = cstate->session->se_client;
  3183. nfs4_lock_state();
  3184. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3185. stateid->ts_id_status =
  3186. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  3187. nfs4_unlock_state();
  3188. return nfs_ok;
  3189. }
  3190. __be32
  3191. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3192. struct nfsd4_free_stateid *free_stateid)
  3193. {
  3194. stateid_t *stateid = &free_stateid->fr_stateid;
  3195. struct nfs4_stid *s;
  3196. struct nfs4_delegation *dp;
  3197. struct nfs4_client *cl = cstate->session->se_client;
  3198. __be32 ret = nfserr_bad_stateid;
  3199. nfs4_lock_state();
  3200. s = find_stateid(cl, stateid);
  3201. if (!s)
  3202. goto out;
  3203. switch (s->sc_type) {
  3204. case NFS4_DELEG_STID:
  3205. ret = nfserr_locks_held;
  3206. goto out;
  3207. case NFS4_OPEN_STID:
  3208. case NFS4_LOCK_STID:
  3209. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3210. if (ret)
  3211. goto out;
  3212. if (s->sc_type == NFS4_LOCK_STID)
  3213. ret = nfsd4_free_lock_stateid(openlockstateid(s));
  3214. else
  3215. ret = nfserr_locks_held;
  3216. break;
  3217. case NFS4_REVOKED_DELEG_STID:
  3218. dp = delegstateid(s);
  3219. destroy_revoked_delegation(dp);
  3220. ret = nfs_ok;
  3221. break;
  3222. default:
  3223. ret = nfserr_bad_stateid;
  3224. }
  3225. out:
  3226. nfs4_unlock_state();
  3227. return ret;
  3228. }
  3229. static inline int
  3230. setlkflg (int type)
  3231. {
  3232. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3233. RD_STATE : WR_STATE;
  3234. }
  3235. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  3236. {
  3237. struct svc_fh *current_fh = &cstate->current_fh;
  3238. struct nfs4_stateowner *sop = stp->st_stateowner;
  3239. __be32 status;
  3240. status = nfsd4_check_seqid(cstate, sop, seqid);
  3241. if (status)
  3242. return status;
  3243. if (stp->st_stid.sc_type == NFS4_CLOSED_STID
  3244. || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
  3245. /*
  3246. * "Closed" stateid's exist *only* to return
  3247. * nfserr_replay_me from the previous step, and
  3248. * revoked delegations are kept only for free_stateid.
  3249. */
  3250. return nfserr_bad_stateid;
  3251. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  3252. if (status)
  3253. return status;
  3254. return nfs4_check_fh(current_fh, stp);
  3255. }
  3256. /*
  3257. * Checks for sequence id mutating operations.
  3258. */
  3259. static __be32
  3260. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3261. stateid_t *stateid, char typemask,
  3262. struct nfs4_ol_stateid **stpp,
  3263. struct nfsd_net *nn)
  3264. {
  3265. __be32 status;
  3266. struct nfs4_stid *s;
  3267. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  3268. seqid, STATEID_VAL(stateid));
  3269. *stpp = NULL;
  3270. status = nfsd4_lookup_stateid(stateid, typemask, &s,
  3271. cstate->minorversion, nn);
  3272. if (status)
  3273. return status;
  3274. *stpp = openlockstateid(s);
  3275. if (!nfsd4_has_session(cstate))
  3276. cstate->replay_owner = (*stpp)->st_stateowner;
  3277. return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
  3278. }
  3279. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3280. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  3281. {
  3282. __be32 status;
  3283. struct nfs4_openowner *oo;
  3284. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  3285. NFS4_OPEN_STID, stpp, nn);
  3286. if (status)
  3287. return status;
  3288. oo = openowner((*stpp)->st_stateowner);
  3289. if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
  3290. return nfserr_bad_stateid;
  3291. return nfs_ok;
  3292. }
  3293. __be32
  3294. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3295. struct nfsd4_open_confirm *oc)
  3296. {
  3297. __be32 status;
  3298. struct nfs4_openowner *oo;
  3299. struct nfs4_ol_stateid *stp;
  3300. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3301. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  3302. (int)cstate->current_fh.fh_dentry->d_name.len,
  3303. cstate->current_fh.fh_dentry->d_name.name);
  3304. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  3305. if (status)
  3306. return status;
  3307. nfs4_lock_state();
  3308. status = nfs4_preprocess_seqid_op(cstate,
  3309. oc->oc_seqid, &oc->oc_req_stateid,
  3310. NFS4_OPEN_STID, &stp, nn);
  3311. if (status)
  3312. goto out;
  3313. oo = openowner(stp->st_stateowner);
  3314. status = nfserr_bad_stateid;
  3315. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  3316. goto out;
  3317. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3318. update_stateid(&stp->st_stid.sc_stateid);
  3319. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3320. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  3321. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  3322. nfsd4_client_record_create(oo->oo_owner.so_client);
  3323. status = nfs_ok;
  3324. out:
  3325. nfsd4_bump_seqid(cstate, status);
  3326. if (!cstate->replay_owner)
  3327. nfs4_unlock_state();
  3328. return status;
  3329. }
  3330. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  3331. {
  3332. if (!test_access(access, stp))
  3333. return;
  3334. nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
  3335. clear_access(access, stp);
  3336. }
  3337. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  3338. {
  3339. switch (to_access) {
  3340. case NFS4_SHARE_ACCESS_READ:
  3341. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  3342. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3343. break;
  3344. case NFS4_SHARE_ACCESS_WRITE:
  3345. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  3346. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3347. break;
  3348. case NFS4_SHARE_ACCESS_BOTH:
  3349. break;
  3350. default:
  3351. WARN_ON_ONCE(1);
  3352. }
  3353. }
  3354. static void
  3355. reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
  3356. {
  3357. int i;
  3358. for (i = 0; i < 4; i++) {
  3359. if ((i & deny) != i)
  3360. clear_deny(i, stp);
  3361. }
  3362. }
  3363. __be32
  3364. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  3365. struct nfsd4_compound_state *cstate,
  3366. struct nfsd4_open_downgrade *od)
  3367. {
  3368. __be32 status;
  3369. struct nfs4_ol_stateid *stp;
  3370. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3371. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  3372. (int)cstate->current_fh.fh_dentry->d_name.len,
  3373. cstate->current_fh.fh_dentry->d_name.name);
  3374. /* We don't yet support WANT bits: */
  3375. if (od->od_deleg_want)
  3376. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  3377. od->od_deleg_want);
  3378. nfs4_lock_state();
  3379. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  3380. &od->od_stateid, &stp, nn);
  3381. if (status)
  3382. goto out;
  3383. status = nfserr_inval;
  3384. if (!test_access(od->od_share_access, stp)) {
  3385. dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
  3386. stp->st_access_bmap, od->od_share_access);
  3387. goto out;
  3388. }
  3389. if (!test_deny(od->od_share_deny, stp)) {
  3390. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  3391. stp->st_deny_bmap, od->od_share_deny);
  3392. goto out;
  3393. }
  3394. nfs4_stateid_downgrade(stp, od->od_share_access);
  3395. reset_union_bmap_deny(od->od_share_deny, stp);
  3396. update_stateid(&stp->st_stid.sc_stateid);
  3397. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3398. status = nfs_ok;
  3399. out:
  3400. nfsd4_bump_seqid(cstate, status);
  3401. if (!cstate->replay_owner)
  3402. nfs4_unlock_state();
  3403. return status;
  3404. }
  3405. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  3406. {
  3407. unhash_open_stateid(s);
  3408. s->st_stid.sc_type = NFS4_CLOSED_STID;
  3409. }
  3410. /*
  3411. * nfs4_unlock_state() called after encode
  3412. */
  3413. __be32
  3414. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3415. struct nfsd4_close *close)
  3416. {
  3417. __be32 status;
  3418. struct nfs4_openowner *oo;
  3419. struct nfs4_ol_stateid *stp;
  3420. struct net *net = SVC_NET(rqstp);
  3421. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3422. dprintk("NFSD: nfsd4_close on file %.*s\n",
  3423. (int)cstate->current_fh.fh_dentry->d_name.len,
  3424. cstate->current_fh.fh_dentry->d_name.name);
  3425. nfs4_lock_state();
  3426. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  3427. &close->cl_stateid,
  3428. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  3429. &stp, nn);
  3430. nfsd4_bump_seqid(cstate, status);
  3431. if (status)
  3432. goto out;
  3433. oo = openowner(stp->st_stateowner);
  3434. update_stateid(&stp->st_stid.sc_stateid);
  3435. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3436. nfsd4_close_open_stateid(stp);
  3437. if (cstate->minorversion) {
  3438. unhash_stid(&stp->st_stid);
  3439. free_generic_stateid(stp);
  3440. } else
  3441. oo->oo_last_closed_stid = stp;
  3442. if (list_empty(&oo->oo_owner.so_stateids)) {
  3443. if (cstate->minorversion)
  3444. release_openowner(oo);
  3445. else {
  3446. /*
  3447. * In the 4.0 case we need to keep the owners around a
  3448. * little while to handle CLOSE replay.
  3449. */
  3450. move_to_close_lru(oo, SVC_NET(rqstp));
  3451. }
  3452. }
  3453. out:
  3454. if (!cstate->replay_owner)
  3455. nfs4_unlock_state();
  3456. return status;
  3457. }
  3458. __be32
  3459. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3460. struct nfsd4_delegreturn *dr)
  3461. {
  3462. struct nfs4_delegation *dp;
  3463. stateid_t *stateid = &dr->dr_stateid;
  3464. struct nfs4_stid *s;
  3465. __be32 status;
  3466. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3467. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3468. return status;
  3469. nfs4_lock_state();
  3470. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
  3471. cstate->minorversion, nn);
  3472. if (status)
  3473. goto out;
  3474. dp = delegstateid(s);
  3475. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  3476. if (status)
  3477. goto out;
  3478. destroy_delegation(dp);
  3479. out:
  3480. nfs4_unlock_state();
  3481. return status;
  3482. }
  3483. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  3484. #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
  3485. static inline u64
  3486. end_offset(u64 start, u64 len)
  3487. {
  3488. u64 end;
  3489. end = start + len;
  3490. return end >= start ? end: NFS4_MAX_UINT64;
  3491. }
  3492. /* last octet in a range */
  3493. static inline u64
  3494. last_byte_offset(u64 start, u64 len)
  3495. {
  3496. u64 end;
  3497. WARN_ON_ONCE(!len);
  3498. end = start + len;
  3499. return end > start ? end - 1: NFS4_MAX_UINT64;
  3500. }
  3501. static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
  3502. {
  3503. return (file_hashval(inode) + cl_id
  3504. + opaque_hashval(ownername->data, ownername->len))
  3505. & LOCKOWNER_INO_HASH_MASK;
  3506. }
  3507. /*
  3508. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  3509. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  3510. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  3511. * locking, this prevents us from being completely protocol-compliant. The
  3512. * real solution to this problem is to start using unsigned file offsets in
  3513. * the VFS, but this is a very deep change!
  3514. */
  3515. static inline void
  3516. nfs4_transform_lock_offset(struct file_lock *lock)
  3517. {
  3518. if (lock->fl_start < 0)
  3519. lock->fl_start = OFFSET_MAX;
  3520. if (lock->fl_end < 0)
  3521. lock->fl_end = OFFSET_MAX;
  3522. }
  3523. /* Hack!: For now, we're defining this just so we can use a pointer to it
  3524. * as a unique cookie to identify our (NFSv4's) posix locks. */
  3525. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  3526. };
  3527. static inline void
  3528. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  3529. {
  3530. struct nfs4_lockowner *lo;
  3531. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  3532. lo = (struct nfs4_lockowner *) fl->fl_owner;
  3533. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  3534. lo->lo_owner.so_owner.len, GFP_KERNEL);
  3535. if (!deny->ld_owner.data)
  3536. /* We just don't care that much */
  3537. goto nevermind;
  3538. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  3539. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  3540. } else {
  3541. nevermind:
  3542. deny->ld_owner.len = 0;
  3543. deny->ld_owner.data = NULL;
  3544. deny->ld_clientid.cl_boot = 0;
  3545. deny->ld_clientid.cl_id = 0;
  3546. }
  3547. deny->ld_start = fl->fl_start;
  3548. deny->ld_length = NFS4_MAX_UINT64;
  3549. if (fl->fl_end != NFS4_MAX_UINT64)
  3550. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3551. deny->ld_type = NFS4_READ_LT;
  3552. if (fl->fl_type != F_RDLCK)
  3553. deny->ld_type = NFS4_WRITE_LT;
  3554. }
  3555. static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
  3556. {
  3557. struct nfs4_ol_stateid *lst;
  3558. if (!same_owner_str(&lo->lo_owner, owner, clid))
  3559. return false;
  3560. lst = list_first_entry(&lo->lo_owner.so_stateids,
  3561. struct nfs4_ol_stateid, st_perstateowner);
  3562. return lst->st_file->fi_inode == inode;
  3563. }
  3564. static struct nfs4_lockowner *
  3565. find_lockowner_str(struct inode *inode, clientid_t *clid,
  3566. struct xdr_netobj *owner, struct nfsd_net *nn)
  3567. {
  3568. unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
  3569. struct nfs4_lockowner *lo;
  3570. list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
  3571. if (same_lockowner_ino(lo, inode, clid, owner))
  3572. return lo;
  3573. }
  3574. return NULL;
  3575. }
  3576. static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
  3577. {
  3578. struct inode *inode = open_stp->st_file->fi_inode;
  3579. unsigned int inohash = lockowner_ino_hashval(inode,
  3580. clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
  3581. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  3582. list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  3583. list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
  3584. list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
  3585. }
  3586. /*
  3587. * Alloc a lock owner structure.
  3588. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3589. * occurred.
  3590. *
  3591. * strhashval = ownerstr_hashval
  3592. */
  3593. static struct nfs4_lockowner *
  3594. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
  3595. struct nfs4_lockowner *lo;
  3596. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  3597. if (!lo)
  3598. return NULL;
  3599. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  3600. lo->lo_owner.so_is_open_owner = 0;
  3601. /* It is the openowner seqid that will be incremented in encode in the
  3602. * case of new lockowners; so increment the lock seqid manually: */
  3603. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
  3604. hash_lockowner(lo, strhashval, clp, open_stp);
  3605. return lo;
  3606. }
  3607. static struct nfs4_ol_stateid *
  3608. alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
  3609. {
  3610. struct nfs4_ol_stateid *stp;
  3611. struct nfs4_client *clp = lo->lo_owner.so_client;
  3612. stp = nfs4_alloc_stateid(clp);
  3613. if (stp == NULL)
  3614. return NULL;
  3615. stp->st_stid.sc_type = NFS4_LOCK_STID;
  3616. list_add(&stp->st_perfile, &fp->fi_stateids);
  3617. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  3618. stp->st_stateowner = &lo->lo_owner;
  3619. get_nfs4_file(fp);
  3620. stp->st_file = fp;
  3621. stp->st_access_bmap = 0;
  3622. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3623. stp->st_openstp = open_stp;
  3624. return stp;
  3625. }
  3626. static int
  3627. check_lock_length(u64 offset, u64 length)
  3628. {
  3629. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3630. LOFF_OVERFLOW(offset, length)));
  3631. }
  3632. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  3633. {
  3634. struct nfs4_file *fp = lock_stp->st_file;
  3635. int oflag = nfs4_access_to_omode(access);
  3636. if (test_access(access, lock_stp))
  3637. return;
  3638. nfs4_file_get_access(fp, oflag);
  3639. set_access(access, lock_stp);
  3640. }
  3641. static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
  3642. {
  3643. struct nfs4_file *fi = ost->st_file;
  3644. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  3645. struct nfs4_client *cl = oo->oo_owner.so_client;
  3646. struct nfs4_lockowner *lo;
  3647. unsigned int strhashval;
  3648. struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
  3649. lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
  3650. &lock->v.new.owner, nn);
  3651. if (lo) {
  3652. if (!cstate->minorversion)
  3653. return nfserr_bad_seqid;
  3654. /* XXX: a lockowner always has exactly one stateid: */
  3655. *lst = list_first_entry(&lo->lo_owner.so_stateids,
  3656. struct nfs4_ol_stateid, st_perstateowner);
  3657. return nfs_ok;
  3658. }
  3659. strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
  3660. &lock->v.new.owner);
  3661. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  3662. if (lo == NULL)
  3663. return nfserr_jukebox;
  3664. *lst = alloc_init_lock_stateid(lo, fi, ost);
  3665. if (*lst == NULL) {
  3666. release_lockowner(lo);
  3667. return nfserr_jukebox;
  3668. }
  3669. *new = true;
  3670. return nfs_ok;
  3671. }
  3672. /*
  3673. * LOCK operation
  3674. */
  3675. __be32
  3676. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3677. struct nfsd4_lock *lock)
  3678. {
  3679. struct nfs4_openowner *open_sop = NULL;
  3680. struct nfs4_lockowner *lock_sop = NULL;
  3681. struct nfs4_ol_stateid *lock_stp;
  3682. struct file *filp = NULL;
  3683. struct file_lock *file_lock = NULL;
  3684. struct file_lock *conflock = NULL;
  3685. __be32 status = 0;
  3686. bool new_state = false;
  3687. int lkflg;
  3688. int err;
  3689. struct net *net = SVC_NET(rqstp);
  3690. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3691. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3692. (long long) lock->lk_offset,
  3693. (long long) lock->lk_length);
  3694. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3695. return nfserr_inval;
  3696. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3697. S_IFREG, NFSD_MAY_LOCK))) {
  3698. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3699. return status;
  3700. }
  3701. nfs4_lock_state();
  3702. if (lock->lk_is_new) {
  3703. struct nfs4_ol_stateid *open_stp = NULL;
  3704. if (nfsd4_has_session(cstate))
  3705. /* See rfc 5661 18.10.3: given clientid is ignored: */
  3706. memcpy(&lock->v.new.clientid,
  3707. &cstate->session->se_client->cl_clientid,
  3708. sizeof(clientid_t));
  3709. status = nfserr_stale_clientid;
  3710. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  3711. goto out;
  3712. /* validate and update open stateid and open seqid */
  3713. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  3714. lock->lk_new_open_seqid,
  3715. &lock->lk_new_open_stateid,
  3716. &open_stp, nn);
  3717. if (status)
  3718. goto out;
  3719. open_sop = openowner(open_stp->st_stateowner);
  3720. status = nfserr_bad_stateid;
  3721. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  3722. &lock->v.new.clientid))
  3723. goto out;
  3724. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  3725. &lock_stp, &new_state);
  3726. } else
  3727. status = nfs4_preprocess_seqid_op(cstate,
  3728. lock->lk_old_lock_seqid,
  3729. &lock->lk_old_lock_stateid,
  3730. NFS4_LOCK_STID, &lock_stp, nn);
  3731. if (status)
  3732. goto out;
  3733. lock_sop = lockowner(lock_stp->st_stateowner);
  3734. lkflg = setlkflg(lock->lk_type);
  3735. status = nfs4_check_openmode(lock_stp, lkflg);
  3736. if (status)
  3737. goto out;
  3738. status = nfserr_grace;
  3739. if (locks_in_grace(net) && !lock->lk_reclaim)
  3740. goto out;
  3741. status = nfserr_no_grace;
  3742. if (!locks_in_grace(net) && lock->lk_reclaim)
  3743. goto out;
  3744. file_lock = locks_alloc_lock();
  3745. if (!file_lock) {
  3746. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3747. status = nfserr_jukebox;
  3748. goto out;
  3749. }
  3750. locks_init_lock(file_lock);
  3751. switch (lock->lk_type) {
  3752. case NFS4_READ_LT:
  3753. case NFS4_READW_LT:
  3754. filp = find_readable_file(lock_stp->st_file);
  3755. if (filp)
  3756. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  3757. file_lock->fl_type = F_RDLCK;
  3758. break;
  3759. case NFS4_WRITE_LT:
  3760. case NFS4_WRITEW_LT:
  3761. filp = find_writeable_file(lock_stp->st_file);
  3762. if (filp)
  3763. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  3764. file_lock->fl_type = F_WRLCK;
  3765. break;
  3766. default:
  3767. status = nfserr_inval;
  3768. goto out;
  3769. }
  3770. if (!filp) {
  3771. status = nfserr_openmode;
  3772. goto out;
  3773. }
  3774. file_lock->fl_owner = (fl_owner_t)lock_sop;
  3775. file_lock->fl_pid = current->tgid;
  3776. file_lock->fl_file = filp;
  3777. file_lock->fl_flags = FL_POSIX;
  3778. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3779. file_lock->fl_start = lock->lk_offset;
  3780. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3781. nfs4_transform_lock_offset(file_lock);
  3782. conflock = locks_alloc_lock();
  3783. if (!conflock) {
  3784. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3785. status = nfserr_jukebox;
  3786. goto out;
  3787. }
  3788. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  3789. switch (-err) {
  3790. case 0: /* success! */
  3791. update_stateid(&lock_stp->st_stid.sc_stateid);
  3792. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  3793. sizeof(stateid_t));
  3794. status = 0;
  3795. break;
  3796. case (EAGAIN): /* conflock holds conflicting lock */
  3797. status = nfserr_denied;
  3798. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3799. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  3800. break;
  3801. case (EDEADLK):
  3802. status = nfserr_deadlock;
  3803. break;
  3804. default:
  3805. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3806. status = nfserrno(err);
  3807. break;
  3808. }
  3809. out:
  3810. if (status && new_state)
  3811. release_lockowner(lock_sop);
  3812. nfsd4_bump_seqid(cstate, status);
  3813. if (!cstate->replay_owner)
  3814. nfs4_unlock_state();
  3815. if (file_lock)
  3816. locks_free_lock(file_lock);
  3817. if (conflock)
  3818. locks_free_lock(conflock);
  3819. return status;
  3820. }
  3821. /*
  3822. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3823. * so we do a temporary open here just to get an open file to pass to
  3824. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3825. * inode operation.)
  3826. */
  3827. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3828. {
  3829. struct file *file;
  3830. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3831. if (!err) {
  3832. err = nfserrno(vfs_test_lock(file, lock));
  3833. nfsd_close(file);
  3834. }
  3835. return err;
  3836. }
  3837. /*
  3838. * LOCKT operation
  3839. */
  3840. __be32
  3841. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3842. struct nfsd4_lockt *lockt)
  3843. {
  3844. struct inode *inode;
  3845. struct file_lock *file_lock = NULL;
  3846. struct nfs4_lockowner *lo;
  3847. __be32 status;
  3848. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3849. if (locks_in_grace(SVC_NET(rqstp)))
  3850. return nfserr_grace;
  3851. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3852. return nfserr_inval;
  3853. nfs4_lock_state();
  3854. if (!nfsd4_has_session(cstate)) {
  3855. status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
  3856. if (status)
  3857. goto out;
  3858. }
  3859. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3860. goto out;
  3861. inode = cstate->current_fh.fh_dentry->d_inode;
  3862. file_lock = locks_alloc_lock();
  3863. if (!file_lock) {
  3864. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3865. status = nfserr_jukebox;
  3866. goto out;
  3867. }
  3868. locks_init_lock(file_lock);
  3869. switch (lockt->lt_type) {
  3870. case NFS4_READ_LT:
  3871. case NFS4_READW_LT:
  3872. file_lock->fl_type = F_RDLCK;
  3873. break;
  3874. case NFS4_WRITE_LT:
  3875. case NFS4_WRITEW_LT:
  3876. file_lock->fl_type = F_WRLCK;
  3877. break;
  3878. default:
  3879. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3880. status = nfserr_inval;
  3881. goto out;
  3882. }
  3883. lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
  3884. if (lo)
  3885. file_lock->fl_owner = (fl_owner_t)lo;
  3886. file_lock->fl_pid = current->tgid;
  3887. file_lock->fl_flags = FL_POSIX;
  3888. file_lock->fl_start = lockt->lt_offset;
  3889. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3890. nfs4_transform_lock_offset(file_lock);
  3891. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  3892. if (status)
  3893. goto out;
  3894. if (file_lock->fl_type != F_UNLCK) {
  3895. status = nfserr_denied;
  3896. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  3897. }
  3898. out:
  3899. nfs4_unlock_state();
  3900. if (file_lock)
  3901. locks_free_lock(file_lock);
  3902. return status;
  3903. }
  3904. __be32
  3905. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3906. struct nfsd4_locku *locku)
  3907. {
  3908. struct nfs4_lockowner *lo;
  3909. struct nfs4_ol_stateid *stp;
  3910. struct file *filp = NULL;
  3911. struct file_lock *file_lock = NULL;
  3912. __be32 status;
  3913. int err;
  3914. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3915. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3916. (long long) locku->lu_offset,
  3917. (long long) locku->lu_length);
  3918. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3919. return nfserr_inval;
  3920. nfs4_lock_state();
  3921. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  3922. &locku->lu_stateid, NFS4_LOCK_STID,
  3923. &stp, nn);
  3924. if (status)
  3925. goto out;
  3926. filp = find_any_file(stp->st_file);
  3927. if (!filp) {
  3928. status = nfserr_lock_range;
  3929. goto out;
  3930. }
  3931. file_lock = locks_alloc_lock();
  3932. if (!file_lock) {
  3933. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3934. status = nfserr_jukebox;
  3935. goto out;
  3936. }
  3937. lo = lockowner(stp->st_stateowner);
  3938. locks_init_lock(file_lock);
  3939. file_lock->fl_type = F_UNLCK;
  3940. file_lock->fl_owner = (fl_owner_t)lo;
  3941. file_lock->fl_pid = current->tgid;
  3942. file_lock->fl_file = filp;
  3943. file_lock->fl_flags = FL_POSIX;
  3944. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3945. file_lock->fl_start = locku->lu_offset;
  3946. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  3947. locku->lu_length);
  3948. nfs4_transform_lock_offset(file_lock);
  3949. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  3950. if (err) {
  3951. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3952. goto out_nfserr;
  3953. }
  3954. update_stateid(&stp->st_stid.sc_stateid);
  3955. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3956. if (nfsd4_has_session(cstate) && !check_for_locks(stp->st_file, lo)) {
  3957. WARN_ON_ONCE(cstate->replay_owner);
  3958. release_lockowner(lo);
  3959. }
  3960. out:
  3961. nfsd4_bump_seqid(cstate, status);
  3962. if (!cstate->replay_owner)
  3963. nfs4_unlock_state();
  3964. if (file_lock)
  3965. locks_free_lock(file_lock);
  3966. return status;
  3967. out_nfserr:
  3968. status = nfserrno(err);
  3969. goto out;
  3970. }
  3971. /*
  3972. * returns
  3973. * 1: locks held by lockowner
  3974. * 0: no locks held by lockowner
  3975. */
  3976. static int
  3977. check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
  3978. {
  3979. struct file_lock **flpp;
  3980. struct inode *inode = filp->fi_inode;
  3981. int status = 0;
  3982. lock_flocks();
  3983. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3984. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3985. status = 1;
  3986. goto out;
  3987. }
  3988. }
  3989. out:
  3990. unlock_flocks();
  3991. return status;
  3992. }
  3993. __be32
  3994. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3995. struct nfsd4_compound_state *cstate,
  3996. struct nfsd4_release_lockowner *rlockowner)
  3997. {
  3998. clientid_t *clid = &rlockowner->rl_clientid;
  3999. struct nfs4_stateowner *sop;
  4000. struct nfs4_lockowner *lo;
  4001. struct nfs4_ol_stateid *stp;
  4002. struct xdr_netobj *owner = &rlockowner->rl_owner;
  4003. struct list_head matches;
  4004. unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
  4005. __be32 status;
  4006. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4007. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  4008. clid->cl_boot, clid->cl_id);
  4009. nfs4_lock_state();
  4010. status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
  4011. if (status)
  4012. goto out;
  4013. status = nfserr_locks_held;
  4014. INIT_LIST_HEAD(&matches);
  4015. list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  4016. if (sop->so_is_open_owner)
  4017. continue;
  4018. if (!same_owner_str(sop, owner, clid))
  4019. continue;
  4020. list_for_each_entry(stp, &sop->so_stateids,
  4021. st_perstateowner) {
  4022. lo = lockowner(sop);
  4023. if (check_for_locks(stp->st_file, lo))
  4024. goto out;
  4025. list_add(&lo->lo_list, &matches);
  4026. }
  4027. }
  4028. /* Clients probably won't expect us to return with some (but not all)
  4029. * of the lockowner state released; so don't release any until all
  4030. * have been checked. */
  4031. status = nfs_ok;
  4032. while (!list_empty(&matches)) {
  4033. lo = list_entry(matches.next, struct nfs4_lockowner,
  4034. lo_list);
  4035. /* unhash_stateowner deletes so_perclient only
  4036. * for openowners. */
  4037. list_del(&lo->lo_list);
  4038. release_lockowner(lo);
  4039. }
  4040. out:
  4041. nfs4_unlock_state();
  4042. return status;
  4043. }
  4044. static inline struct nfs4_client_reclaim *
  4045. alloc_reclaim(void)
  4046. {
  4047. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  4048. }
  4049. bool
  4050. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  4051. {
  4052. struct nfs4_client_reclaim *crp;
  4053. crp = nfsd4_find_reclaim_client(name, nn);
  4054. return (crp && crp->cr_clp);
  4055. }
  4056. /*
  4057. * failure => all reset bets are off, nfserr_no_grace...
  4058. */
  4059. struct nfs4_client_reclaim *
  4060. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  4061. {
  4062. unsigned int strhashval;
  4063. struct nfs4_client_reclaim *crp;
  4064. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  4065. crp = alloc_reclaim();
  4066. if (crp) {
  4067. strhashval = clientstr_hashval(name);
  4068. INIT_LIST_HEAD(&crp->cr_strhash);
  4069. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  4070. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  4071. crp->cr_clp = NULL;
  4072. nn->reclaim_str_hashtbl_size++;
  4073. }
  4074. return crp;
  4075. }
  4076. void
  4077. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  4078. {
  4079. list_del(&crp->cr_strhash);
  4080. kfree(crp);
  4081. nn->reclaim_str_hashtbl_size--;
  4082. }
  4083. void
  4084. nfs4_release_reclaim(struct nfsd_net *nn)
  4085. {
  4086. struct nfs4_client_reclaim *crp = NULL;
  4087. int i;
  4088. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4089. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  4090. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  4091. struct nfs4_client_reclaim, cr_strhash);
  4092. nfs4_remove_reclaim_record(crp, nn);
  4093. }
  4094. }
  4095. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  4096. }
  4097. /*
  4098. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  4099. struct nfs4_client_reclaim *
  4100. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  4101. {
  4102. unsigned int strhashval;
  4103. struct nfs4_client_reclaim *crp = NULL;
  4104. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  4105. strhashval = clientstr_hashval(recdir);
  4106. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  4107. if (same_name(crp->cr_recdir, recdir)) {
  4108. return crp;
  4109. }
  4110. }
  4111. return NULL;
  4112. }
  4113. /*
  4114. * Called from OPEN. Look for clientid in reclaim list.
  4115. */
  4116. __be32
  4117. nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  4118. {
  4119. struct nfs4_client *clp;
  4120. /* find clientid in conf_id_hashtbl */
  4121. clp = find_confirmed_client(clid, sessions, nn);
  4122. if (clp == NULL)
  4123. return nfserr_reclaim_bad;
  4124. return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
  4125. }
  4126. #ifdef CONFIG_NFSD_FAULT_INJECTION
  4127. u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
  4128. {
  4129. if (mark_client_expired(clp))
  4130. return 0;
  4131. expire_client(clp);
  4132. return 1;
  4133. }
  4134. u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
  4135. {
  4136. char buf[INET6_ADDRSTRLEN];
  4137. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4138. printk(KERN_INFO "NFS Client: %s\n", buf);
  4139. return 1;
  4140. }
  4141. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  4142. const char *type)
  4143. {
  4144. char buf[INET6_ADDRSTRLEN];
  4145. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4146. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  4147. }
  4148. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
  4149. {
  4150. struct nfs4_openowner *oop;
  4151. struct nfs4_lockowner *lop, *lo_next;
  4152. struct nfs4_ol_stateid *stp, *st_next;
  4153. u64 count = 0;
  4154. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  4155. list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
  4156. list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
  4157. if (func)
  4158. func(lop);
  4159. if (++count == max)
  4160. return count;
  4161. }
  4162. }
  4163. }
  4164. return count;
  4165. }
  4166. u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
  4167. {
  4168. return nfsd_foreach_client_lock(clp, max, release_lockowner);
  4169. }
  4170. u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
  4171. {
  4172. u64 count = nfsd_foreach_client_lock(clp, max, NULL);
  4173. nfsd_print_count(clp, count, "locked files");
  4174. return count;
  4175. }
  4176. static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
  4177. {
  4178. struct nfs4_openowner *oop, *next;
  4179. u64 count = 0;
  4180. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  4181. if (func)
  4182. func(oop);
  4183. if (++count == max)
  4184. break;
  4185. }
  4186. return count;
  4187. }
  4188. u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
  4189. {
  4190. return nfsd_foreach_client_open(clp, max, release_openowner);
  4191. }
  4192. u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
  4193. {
  4194. u64 count = nfsd_foreach_client_open(clp, max, NULL);
  4195. nfsd_print_count(clp, count, "open files");
  4196. return count;
  4197. }
  4198. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  4199. struct list_head *victims)
  4200. {
  4201. struct nfs4_delegation *dp, *next;
  4202. u64 count = 0;
  4203. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  4204. if (victims)
  4205. list_move(&dp->dl_recall_lru, victims);
  4206. if (++count == max)
  4207. break;
  4208. }
  4209. return count;
  4210. }
  4211. u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
  4212. {
  4213. struct nfs4_delegation *dp, *next;
  4214. LIST_HEAD(victims);
  4215. u64 count;
  4216. spin_lock(&recall_lock);
  4217. count = nfsd_find_all_delegations(clp, max, &victims);
  4218. spin_unlock(&recall_lock);
  4219. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4220. revoke_delegation(dp);
  4221. return count;
  4222. }
  4223. u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
  4224. {
  4225. struct nfs4_delegation *dp, *next;
  4226. LIST_HEAD(victims);
  4227. u64 count;
  4228. spin_lock(&recall_lock);
  4229. count = nfsd_find_all_delegations(clp, max, &victims);
  4230. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4231. nfsd_break_one_deleg(dp);
  4232. spin_unlock(&recall_lock);
  4233. return count;
  4234. }
  4235. u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
  4236. {
  4237. u64 count = 0;
  4238. spin_lock(&recall_lock);
  4239. count = nfsd_find_all_delegations(clp, max, NULL);
  4240. spin_unlock(&recall_lock);
  4241. nfsd_print_count(clp, count, "delegations");
  4242. return count;
  4243. }
  4244. u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
  4245. {
  4246. struct nfs4_client *clp, *next;
  4247. u64 count = 0;
  4248. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4249. if (!nfsd_netns_ready(nn))
  4250. return 0;
  4251. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  4252. count += func(clp, max - count);
  4253. if ((max != 0) && (count >= max))
  4254. break;
  4255. }
  4256. return count;
  4257. }
  4258. struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  4259. {
  4260. struct nfs4_client *clp;
  4261. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4262. if (!nfsd_netns_ready(nn))
  4263. return NULL;
  4264. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  4265. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  4266. return clp;
  4267. }
  4268. return NULL;
  4269. }
  4270. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  4271. /* initialization to perform at module load time: */
  4272. void
  4273. nfs4_state_init(void)
  4274. {
  4275. }
  4276. /*
  4277. * Since the lifetime of a delegation isn't limited to that of an open, a
  4278. * client may quite reasonably hang on to a delegation as long as it has
  4279. * the inode cached. This becomes an obvious problem the first time a
  4280. * client's inode cache approaches the size of the server's total memory.
  4281. *
  4282. * For now we avoid this problem by imposing a hard limit on the number
  4283. * of delegations, which varies according to the server's memory size.
  4284. */
  4285. static void
  4286. set_max_delegations(void)
  4287. {
  4288. /*
  4289. * Allow at most 4 delegations per megabyte of RAM. Quick
  4290. * estimates suggest that in the worst case (where every delegation
  4291. * is for a different inode), a delegation could take about 1.5K,
  4292. * giving a worst case usage of about 6% of memory.
  4293. */
  4294. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  4295. }
  4296. static int nfs4_state_create_net(struct net *net)
  4297. {
  4298. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4299. int i;
  4300. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4301. CLIENT_HASH_SIZE, GFP_KERNEL);
  4302. if (!nn->conf_id_hashtbl)
  4303. goto err;
  4304. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4305. CLIENT_HASH_SIZE, GFP_KERNEL);
  4306. if (!nn->unconf_id_hashtbl)
  4307. goto err_unconf_id;
  4308. nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  4309. OWNER_HASH_SIZE, GFP_KERNEL);
  4310. if (!nn->ownerstr_hashtbl)
  4311. goto err_ownerstr;
  4312. nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
  4313. LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
  4314. if (!nn->lockowner_ino_hashtbl)
  4315. goto err_lockowner_ino;
  4316. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  4317. SESSION_HASH_SIZE, GFP_KERNEL);
  4318. if (!nn->sessionid_hashtbl)
  4319. goto err_sessionid;
  4320. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4321. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  4322. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  4323. }
  4324. for (i = 0; i < OWNER_HASH_SIZE; i++)
  4325. INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
  4326. for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
  4327. INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
  4328. for (i = 0; i < SESSION_HASH_SIZE; i++)
  4329. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  4330. nn->conf_name_tree = RB_ROOT;
  4331. nn->unconf_name_tree = RB_ROOT;
  4332. INIT_LIST_HEAD(&nn->client_lru);
  4333. INIT_LIST_HEAD(&nn->close_lru);
  4334. INIT_LIST_HEAD(&nn->del_recall_lru);
  4335. spin_lock_init(&nn->client_lock);
  4336. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  4337. get_net(net);
  4338. return 0;
  4339. err_sessionid:
  4340. kfree(nn->lockowner_ino_hashtbl);
  4341. err_lockowner_ino:
  4342. kfree(nn->ownerstr_hashtbl);
  4343. err_ownerstr:
  4344. kfree(nn->unconf_id_hashtbl);
  4345. err_unconf_id:
  4346. kfree(nn->conf_id_hashtbl);
  4347. err:
  4348. return -ENOMEM;
  4349. }
  4350. static void
  4351. nfs4_state_destroy_net(struct net *net)
  4352. {
  4353. int i;
  4354. struct nfs4_client *clp = NULL;
  4355. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4356. struct rb_node *node, *tmp;
  4357. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4358. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  4359. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  4360. destroy_client(clp);
  4361. }
  4362. }
  4363. node = rb_first(&nn->unconf_name_tree);
  4364. while (node != NULL) {
  4365. tmp = node;
  4366. node = rb_next(tmp);
  4367. clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
  4368. rb_erase(tmp, &nn->unconf_name_tree);
  4369. destroy_client(clp);
  4370. }
  4371. kfree(nn->sessionid_hashtbl);
  4372. kfree(nn->lockowner_ino_hashtbl);
  4373. kfree(nn->ownerstr_hashtbl);
  4374. kfree(nn->unconf_id_hashtbl);
  4375. kfree(nn->conf_id_hashtbl);
  4376. put_net(net);
  4377. }
  4378. int
  4379. nfs4_state_start_net(struct net *net)
  4380. {
  4381. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4382. int ret;
  4383. ret = nfs4_state_create_net(net);
  4384. if (ret)
  4385. return ret;
  4386. nfsd4_client_tracking_init(net);
  4387. nn->boot_time = get_seconds();
  4388. locks_start_grace(net, &nn->nfsd4_manager);
  4389. nn->grace_ended = false;
  4390. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  4391. nn->nfsd4_grace, net);
  4392. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  4393. return 0;
  4394. }
  4395. /* initialization to perform when the nfsd service is started: */
  4396. int
  4397. nfs4_state_start(void)
  4398. {
  4399. int ret;
  4400. ret = set_callback_cred();
  4401. if (ret)
  4402. return -ENOMEM;
  4403. laundry_wq = create_singlethread_workqueue("nfsd4");
  4404. if (laundry_wq == NULL) {
  4405. ret = -ENOMEM;
  4406. goto out_recovery;
  4407. }
  4408. ret = nfsd4_create_callback_queue();
  4409. if (ret)
  4410. goto out_free_laundry;
  4411. set_max_delegations();
  4412. return 0;
  4413. out_free_laundry:
  4414. destroy_workqueue(laundry_wq);
  4415. out_recovery:
  4416. return ret;
  4417. }
  4418. /* should be called with the state lock held */
  4419. void
  4420. nfs4_state_shutdown_net(struct net *net)
  4421. {
  4422. struct nfs4_delegation *dp = NULL;
  4423. struct list_head *pos, *next, reaplist;
  4424. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4425. cancel_delayed_work_sync(&nn->laundromat_work);
  4426. locks_end_grace(&nn->nfsd4_manager);
  4427. INIT_LIST_HEAD(&reaplist);
  4428. spin_lock(&recall_lock);
  4429. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  4430. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4431. list_move(&dp->dl_recall_lru, &reaplist);
  4432. }
  4433. spin_unlock(&recall_lock);
  4434. list_for_each_safe(pos, next, &reaplist) {
  4435. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4436. destroy_delegation(dp);
  4437. }
  4438. nfsd4_client_tracking_exit(net);
  4439. nfs4_state_destroy_net(net);
  4440. }
  4441. void
  4442. nfs4_state_shutdown(void)
  4443. {
  4444. destroy_workqueue(laundry_wq);
  4445. nfsd4_destroy_callback_queue();
  4446. }
  4447. static void
  4448. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4449. {
  4450. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  4451. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  4452. }
  4453. static void
  4454. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4455. {
  4456. if (cstate->minorversion) {
  4457. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  4458. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4459. }
  4460. }
  4461. void
  4462. clear_current_stateid(struct nfsd4_compound_state *cstate)
  4463. {
  4464. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4465. }
  4466. /*
  4467. * functions to set current state id
  4468. */
  4469. void
  4470. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4471. {
  4472. put_stateid(cstate, &odp->od_stateid);
  4473. }
  4474. void
  4475. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  4476. {
  4477. put_stateid(cstate, &open->op_stateid);
  4478. }
  4479. void
  4480. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4481. {
  4482. put_stateid(cstate, &close->cl_stateid);
  4483. }
  4484. void
  4485. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  4486. {
  4487. put_stateid(cstate, &lock->lk_resp_stateid);
  4488. }
  4489. /*
  4490. * functions to consume current state id
  4491. */
  4492. void
  4493. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4494. {
  4495. get_stateid(cstate, &odp->od_stateid);
  4496. }
  4497. void
  4498. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  4499. {
  4500. get_stateid(cstate, &drp->dr_stateid);
  4501. }
  4502. void
  4503. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  4504. {
  4505. get_stateid(cstate, &fsp->fr_stateid);
  4506. }
  4507. void
  4508. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  4509. {
  4510. get_stateid(cstate, &setattr->sa_stateid);
  4511. }
  4512. void
  4513. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4514. {
  4515. get_stateid(cstate, &close->cl_stateid);
  4516. }
  4517. void
  4518. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  4519. {
  4520. get_stateid(cstate, &locku->lu_stateid);
  4521. }
  4522. void
  4523. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  4524. {
  4525. get_stateid(cstate, &read->rd_stateid);
  4526. }
  4527. void
  4528. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  4529. {
  4530. get_stateid(cstate, &write->wr_stateid);
  4531. }