nfs4state.c 133 KB

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