nfs4state.c 109 KB

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