nfs4state.c 114 KB

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