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