nfs4state.c 129 KB

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