nfs4state.c 115 KB

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