nfs4state.c 114 KB

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