nfs4state.c 109 KB

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