nfs4state.c 133 KB

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