nfs4state.c 131 KB

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