nfs4state.c 123 KB

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