nfs4state.c 111 KB

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