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