nfs4state.c 129 KB

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