nfs4state.c 114 KB

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