nfs4state.c 106 KB

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