nfs4state.c 125 KB

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