nfs4state.c 126 KB

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