nfs4state.c 129 KB

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