nfs4state.c 118 KB

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