nfs4state.c 136 KB

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