nfs4state.c 137 KB

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