nfs4state.c 130 KB

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