nfs4state.c 126 KB

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