nfs4state.c 115 KB

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