nfs4state.c 137 KB

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