nfs4state.c 132 KB

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