nfs4state.c 117 KB

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