nfs4state.c 127 KB

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