nfs4state.c 114 KB

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