nfs4state.c 117 KB

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