nfs4state.c 100 KB

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