nfs4state.c 127 KB

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