nfs4state.c 110 KB

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