nfs4state.c 140 KB

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