nfs4state.c 115 KB

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