nfs4state.c 94 KB

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