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