nfs4state.c 137 KB

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