nfs4state.c 111 KB

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