nfs4state.c 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <kandros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/swap.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/sunrpc/svcauth_gss.h>
  41. #include <linux/sunrpc/clnt.h>
  42. #include "xdr4.h"
  43. #include "vfs.h"
  44. #define NFSDDBG_FACILITY NFSDDBG_PROC
  45. /* Globals */
  46. time_t nfsd4_lease = 90; /* default lease time */
  47. time_t nfsd4_grace = 90;
  48. static time_t boot_time;
  49. #define all_ones {{~0,~0},~0}
  50. static const stateid_t one_stateid = {
  51. .si_generation = ~0,
  52. .si_opaque = all_ones,
  53. };
  54. static const stateid_t zero_stateid = {
  55. /* all fields zero */
  56. };
  57. static const stateid_t currentstateid = {
  58. .si_generation = 1,
  59. };
  60. static u64 current_sessionid = 1;
  61. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  62. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  63. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  64. /* forward declarations */
  65. static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
  66. /* Locking: */
  67. /* Currently used for almost all code touching nfsv4 state: */
  68. static DEFINE_MUTEX(client_mutex);
  69. /*
  70. * Currently used for the del_recall_lru and file hash table. In an
  71. * effort to decrease the scope of the client_mutex, this spinlock may
  72. * eventually cover more:
  73. */
  74. static DEFINE_SPINLOCK(recall_lock);
  75. static struct kmem_cache *openowner_slab = NULL;
  76. static struct kmem_cache *lockowner_slab = NULL;
  77. static struct kmem_cache *file_slab = NULL;
  78. static struct kmem_cache *stateid_slab = NULL;
  79. static struct kmem_cache *deleg_slab = NULL;
  80. void
  81. nfs4_lock_state(void)
  82. {
  83. mutex_lock(&client_mutex);
  84. }
  85. static void free_session(struct kref *);
  86. /* Must be called under the client_lock */
  87. static void nfsd4_put_session_locked(struct nfsd4_session *ses)
  88. {
  89. kref_put(&ses->se_ref, free_session);
  90. }
  91. static void nfsd4_get_session(struct nfsd4_session *ses)
  92. {
  93. kref_get(&ses->se_ref);
  94. }
  95. void
  96. nfs4_unlock_state(void)
  97. {
  98. mutex_unlock(&client_mutex);
  99. }
  100. static inline u32
  101. opaque_hashval(const void *ptr, int nbytes)
  102. {
  103. unsigned char *cptr = (unsigned char *) ptr;
  104. u32 x = 0;
  105. while (nbytes--) {
  106. x *= 37;
  107. x += *cptr++;
  108. }
  109. return x;
  110. }
  111. static struct list_head del_recall_lru;
  112. static void nfsd4_free_file(struct nfs4_file *f)
  113. {
  114. kmem_cache_free(file_slab, f);
  115. }
  116. static inline void
  117. put_nfs4_file(struct nfs4_file *fi)
  118. {
  119. if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
  120. list_del(&fi->fi_hash);
  121. spin_unlock(&recall_lock);
  122. iput(fi->fi_inode);
  123. nfsd4_free_file(fi);
  124. }
  125. }
  126. static inline void
  127. get_nfs4_file(struct nfs4_file *fi)
  128. {
  129. atomic_inc(&fi->fi_ref);
  130. }
  131. static int num_delegations;
  132. unsigned int max_delegations;
  133. /*
  134. * Open owner state (share locks)
  135. */
  136. /* hash tables for lock and open owners */
  137. #define OWNER_HASH_BITS 8
  138. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  139. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  140. static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
  141. {
  142. unsigned int ret;
  143. ret = opaque_hashval(ownername->data, ownername->len);
  144. ret += clientid;
  145. return ret & OWNER_HASH_MASK;
  146. }
  147. static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
  148. /* hash table for nfs4_file */
  149. #define FILE_HASH_BITS 8
  150. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  151. static unsigned int file_hashval(struct inode *ino)
  152. {
  153. /* XXX: why are we hashing on inode pointer, anyway? */
  154. return hash_ptr(ino, FILE_HASH_BITS);
  155. }
  156. static struct list_head file_hashtbl[FILE_HASH_SIZE];
  157. static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  158. {
  159. BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
  160. atomic_inc(&fp->fi_access[oflag]);
  161. }
  162. static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  163. {
  164. if (oflag == O_RDWR) {
  165. __nfs4_file_get_access(fp, O_RDONLY);
  166. __nfs4_file_get_access(fp, O_WRONLY);
  167. } else
  168. __nfs4_file_get_access(fp, oflag);
  169. }
  170. static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
  171. {
  172. if (fp->fi_fds[oflag]) {
  173. fput(fp->fi_fds[oflag]);
  174. fp->fi_fds[oflag] = NULL;
  175. }
  176. }
  177. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  178. {
  179. if (atomic_dec_and_test(&fp->fi_access[oflag])) {
  180. nfs4_file_put_fd(fp, oflag);
  181. /*
  182. * It's also safe to get rid of the RDWR open *if*
  183. * we no longer have need of the other kind of access
  184. * or if we already have the other kind of open:
  185. */
  186. if (fp->fi_fds[1-oflag]
  187. || atomic_read(&fp->fi_access[1 - oflag]) == 0)
  188. nfs4_file_put_fd(fp, O_RDWR);
  189. }
  190. }
  191. static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  192. {
  193. if (oflag == O_RDWR) {
  194. __nfs4_file_put_access(fp, O_RDONLY);
  195. __nfs4_file_put_access(fp, O_WRONLY);
  196. } else
  197. __nfs4_file_put_access(fp, oflag);
  198. }
  199. static inline int get_new_stid(struct nfs4_stid *stid)
  200. {
  201. static int min_stateid = 0;
  202. struct idr *stateids = &stid->sc_client->cl_stateids;
  203. int new_stid;
  204. int error;
  205. error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
  206. /*
  207. * Note: the necessary preallocation was done in
  208. * nfs4_alloc_stateid(). The idr code caps the number of
  209. * preallocations that can exist at a time, but the state lock
  210. * prevents anyone from using ours before we get here:
  211. */
  212. BUG_ON(error);
  213. /*
  214. * It shouldn't be a problem to reuse an opaque stateid value.
  215. * I don't think it is for 4.1. But with 4.0 I worry that, for
  216. * example, a stray write retransmission could be accepted by
  217. * the server when it should have been rejected. Therefore,
  218. * adopt a trick from the sctp code to attempt to maximize the
  219. * amount of time until an id is reused, by ensuring they always
  220. * "increase" (mod INT_MAX):
  221. */
  222. min_stateid = new_stid+1;
  223. if (min_stateid == INT_MAX)
  224. min_stateid = 0;
  225. return new_stid;
  226. }
  227. static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
  228. {
  229. stateid_t *s = &stid->sc_stateid;
  230. int new_id;
  231. stid->sc_type = type;
  232. stid->sc_client = cl;
  233. s->si_opaque.so_clid = cl->cl_clientid;
  234. new_id = get_new_stid(stid);
  235. s->si_opaque.so_id = (u32)new_id;
  236. /* Will be incremented before return to client: */
  237. s->si_generation = 0;
  238. }
  239. static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
  240. {
  241. struct idr *stateids = &cl->cl_stateids;
  242. if (!idr_pre_get(stateids, GFP_KERNEL))
  243. return NULL;
  244. /*
  245. * Note: if we fail here (or any time between now and the time
  246. * we actually get the new idr), we won't need to undo the idr
  247. * preallocation, since the idr code caps the number of
  248. * preallocated entries.
  249. */
  250. return kmem_cache_alloc(slab, GFP_KERNEL);
  251. }
  252. static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
  253. {
  254. return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
  255. }
  256. static struct nfs4_delegation *
  257. alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
  258. {
  259. struct nfs4_delegation *dp;
  260. struct nfs4_file *fp = stp->st_file;
  261. dprintk("NFSD alloc_init_deleg\n");
  262. /*
  263. * Major work on the lease subsystem (for example, to support
  264. * calbacks on stat) will be required before we can support
  265. * write delegations properly.
  266. */
  267. if (type != NFS4_OPEN_DELEGATE_READ)
  268. return NULL;
  269. if (fp->fi_had_conflict)
  270. return NULL;
  271. if (num_delegations > max_delegations)
  272. return NULL;
  273. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
  274. if (dp == NULL)
  275. return dp;
  276. init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
  277. /*
  278. * delegation seqid's are never incremented. The 4.1 special
  279. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  280. * 0 anyway just for consistency and use 1:
  281. */
  282. dp->dl_stid.sc_stateid.si_generation = 1;
  283. num_delegations++;
  284. INIT_LIST_HEAD(&dp->dl_perfile);
  285. INIT_LIST_HEAD(&dp->dl_perclnt);
  286. INIT_LIST_HEAD(&dp->dl_recall_lru);
  287. get_nfs4_file(fp);
  288. dp->dl_file = fp;
  289. dp->dl_type = type;
  290. fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
  291. dp->dl_time = 0;
  292. atomic_set(&dp->dl_count, 1);
  293. INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
  294. return dp;
  295. }
  296. void
  297. nfs4_put_delegation(struct nfs4_delegation *dp)
  298. {
  299. if (atomic_dec_and_test(&dp->dl_count)) {
  300. dprintk("NFSD: freeing dp %p\n",dp);
  301. put_nfs4_file(dp->dl_file);
  302. kmem_cache_free(deleg_slab, dp);
  303. num_delegations--;
  304. }
  305. }
  306. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  307. {
  308. if (atomic_dec_and_test(&fp->fi_delegees)) {
  309. vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
  310. fp->fi_lease = NULL;
  311. fput(fp->fi_deleg_file);
  312. fp->fi_deleg_file = NULL;
  313. }
  314. }
  315. static void unhash_stid(struct nfs4_stid *s)
  316. {
  317. struct idr *stateids = &s->sc_client->cl_stateids;
  318. idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
  319. }
  320. /* Called under the state lock. */
  321. static void
  322. unhash_delegation(struct nfs4_delegation *dp)
  323. {
  324. unhash_stid(&dp->dl_stid);
  325. list_del_init(&dp->dl_perclnt);
  326. spin_lock(&recall_lock);
  327. list_del_init(&dp->dl_perfile);
  328. list_del_init(&dp->dl_recall_lru);
  329. spin_unlock(&recall_lock);
  330. nfs4_put_deleg_lease(dp->dl_file);
  331. nfs4_put_delegation(dp);
  332. }
  333. /*
  334. * SETCLIENTID state
  335. */
  336. /* client_lock protects the client lru list and session hash table */
  337. static DEFINE_SPINLOCK(client_lock);
  338. /* Hash tables for nfs4_clientid state */
  339. #define CLIENT_HASH_BITS 4
  340. #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
  341. #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
  342. static unsigned int clientid_hashval(u32 id)
  343. {
  344. return id & CLIENT_HASH_MASK;
  345. }
  346. static unsigned int clientstr_hashval(const char *name)
  347. {
  348. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  349. }
  350. /*
  351. * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
  352. * used in reboot/reset lease grace period processing
  353. *
  354. * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
  355. * setclientid_confirmed info.
  356. *
  357. * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
  358. * setclientid info.
  359. *
  360. * client_lru holds client queue ordered by nfs4_client.cl_time
  361. * for lease renewal.
  362. *
  363. * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
  364. * for last close replay.
  365. */
  366. static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
  367. static int reclaim_str_hashtbl_size = 0;
  368. static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
  369. static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
  370. static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
  371. static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
  372. static struct list_head client_lru;
  373. static struct list_head close_lru;
  374. /*
  375. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  376. * st_{access,deny}_bmap field of the stateid, in order to track not
  377. * only what share bits are currently in force, but also what
  378. * combinations of share bits previous opens have used. This allows us
  379. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  380. * return an error if the client attempt to downgrade to a combination
  381. * of share bits not explicable by closing some of its previous opens.
  382. *
  383. * XXX: This enforcement is actually incomplete, since we don't keep
  384. * track of access/deny bit combinations; so, e.g., we allow:
  385. *
  386. * OPEN allow read, deny write
  387. * OPEN allow both, deny none
  388. * DOWNGRADE allow read, deny none
  389. *
  390. * which we should reject.
  391. */
  392. static void
  393. set_access(unsigned int *access, unsigned long bmap) {
  394. int i;
  395. *access = 0;
  396. for (i = 1; i < 4; i++) {
  397. if (test_bit(i, &bmap))
  398. *access |= i;
  399. }
  400. }
  401. static void
  402. set_deny(unsigned int *deny, unsigned long bmap) {
  403. int i;
  404. *deny = 0;
  405. for (i = 0; i < 4; i++) {
  406. if (test_bit(i, &bmap))
  407. *deny |= i ;
  408. }
  409. }
  410. static int
  411. test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
  412. unsigned int access, deny;
  413. set_access(&access, stp->st_access_bmap);
  414. set_deny(&deny, stp->st_deny_bmap);
  415. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  416. return 0;
  417. return 1;
  418. }
  419. static int nfs4_access_to_omode(u32 access)
  420. {
  421. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  422. case NFS4_SHARE_ACCESS_READ:
  423. return O_RDONLY;
  424. case NFS4_SHARE_ACCESS_WRITE:
  425. return O_WRONLY;
  426. case NFS4_SHARE_ACCESS_BOTH:
  427. return O_RDWR;
  428. }
  429. BUG();
  430. }
  431. static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
  432. {
  433. list_del(&stp->st_perfile);
  434. list_del(&stp->st_perstateowner);
  435. }
  436. static void close_generic_stateid(struct nfs4_ol_stateid *stp)
  437. {
  438. int i;
  439. if (stp->st_access_bmap) {
  440. for (i = 1; i < 4; i++) {
  441. if (test_bit(i, &stp->st_access_bmap))
  442. nfs4_file_put_access(stp->st_file,
  443. nfs4_access_to_omode(i));
  444. __clear_bit(i, &stp->st_access_bmap);
  445. }
  446. }
  447. put_nfs4_file(stp->st_file);
  448. stp->st_file = NULL;
  449. }
  450. static void free_generic_stateid(struct nfs4_ol_stateid *stp)
  451. {
  452. kmem_cache_free(stateid_slab, stp);
  453. }
  454. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  455. {
  456. struct file *file;
  457. unhash_generic_stateid(stp);
  458. unhash_stid(&stp->st_stid);
  459. file = find_any_file(stp->st_file);
  460. if (file)
  461. locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
  462. close_generic_stateid(stp);
  463. free_generic_stateid(stp);
  464. }
  465. static void unhash_lockowner(struct nfs4_lockowner *lo)
  466. {
  467. struct nfs4_ol_stateid *stp;
  468. list_del(&lo->lo_owner.so_strhash);
  469. list_del(&lo->lo_perstateid);
  470. list_del(&lo->lo_owner_ino_hash);
  471. while (!list_empty(&lo->lo_owner.so_stateids)) {
  472. stp = list_first_entry(&lo->lo_owner.so_stateids,
  473. struct nfs4_ol_stateid, st_perstateowner);
  474. release_lock_stateid(stp);
  475. }
  476. }
  477. static void release_lockowner(struct nfs4_lockowner *lo)
  478. {
  479. unhash_lockowner(lo);
  480. nfs4_free_lockowner(lo);
  481. }
  482. static void
  483. release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
  484. {
  485. struct nfs4_lockowner *lo;
  486. while (!list_empty(&open_stp->st_lockowners)) {
  487. lo = list_entry(open_stp->st_lockowners.next,
  488. struct nfs4_lockowner, lo_perstateid);
  489. release_lockowner(lo);
  490. }
  491. }
  492. static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
  493. {
  494. unhash_generic_stateid(stp);
  495. release_stateid_lockowners(stp);
  496. close_generic_stateid(stp);
  497. }
  498. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  499. {
  500. unhash_open_stateid(stp);
  501. unhash_stid(&stp->st_stid);
  502. free_generic_stateid(stp);
  503. }
  504. static void unhash_openowner(struct nfs4_openowner *oo)
  505. {
  506. struct nfs4_ol_stateid *stp;
  507. list_del(&oo->oo_owner.so_strhash);
  508. list_del(&oo->oo_perclient);
  509. while (!list_empty(&oo->oo_owner.so_stateids)) {
  510. stp = list_first_entry(&oo->oo_owner.so_stateids,
  511. struct nfs4_ol_stateid, st_perstateowner);
  512. release_open_stateid(stp);
  513. }
  514. }
  515. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  516. {
  517. struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
  518. if (s) {
  519. unhash_stid(&s->st_stid);
  520. free_generic_stateid(s);
  521. oo->oo_last_closed_stid = NULL;
  522. }
  523. }
  524. static void release_openowner(struct nfs4_openowner *oo)
  525. {
  526. unhash_openowner(oo);
  527. list_del(&oo->oo_close_lru);
  528. release_last_closed_stateid(oo);
  529. nfs4_free_openowner(oo);
  530. }
  531. #define SESSION_HASH_SIZE 512
  532. static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
  533. static inline int
  534. hash_sessionid(struct nfs4_sessionid *sessionid)
  535. {
  536. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  537. return sid->sequence % SESSION_HASH_SIZE;
  538. }
  539. #ifdef NFSD_DEBUG
  540. static inline void
  541. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  542. {
  543. u32 *ptr = (u32 *)(&sessionid->data[0]);
  544. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  545. }
  546. #else
  547. static inline void
  548. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  549. {
  550. }
  551. #endif
  552. static void
  553. gen_sessionid(struct nfsd4_session *ses)
  554. {
  555. struct nfs4_client *clp = ses->se_client;
  556. struct nfsd4_sessionid *sid;
  557. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  558. sid->clientid = clp->cl_clientid;
  559. sid->sequence = current_sessionid++;
  560. sid->reserved = 0;
  561. }
  562. /*
  563. * The protocol defines ca_maxresponssize_cached to include the size of
  564. * the rpc header, but all we need to cache is the data starting after
  565. * the end of the initial SEQUENCE operation--the rest we regenerate
  566. * each time. Therefore we can advertise a ca_maxresponssize_cached
  567. * value that is the number of bytes in our cache plus a few additional
  568. * bytes. In order to stay on the safe side, and not promise more than
  569. * we can cache, those additional bytes must be the minimum possible: 24
  570. * bytes of rpc header (xid through accept state, with AUTH_NULL
  571. * verifier), 12 for the compound header (with zero-length tag), and 44
  572. * for the SEQUENCE op response:
  573. */
  574. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  575. static void
  576. free_session_slots(struct nfsd4_session *ses)
  577. {
  578. int i;
  579. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  580. kfree(ses->se_slots[i]);
  581. }
  582. /*
  583. * We don't actually need to cache the rpc and session headers, so we
  584. * can allocate a little less for each slot:
  585. */
  586. static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
  587. {
  588. return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  589. }
  590. static int nfsd4_sanitize_slot_size(u32 size)
  591. {
  592. size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
  593. size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
  594. return size;
  595. }
  596. /*
  597. * XXX: If we run out of reserved DRC memory we could (up to a point)
  598. * re-negotiate active sessions and reduce their slot usage to make
  599. * room for new connections. For now we just fail the create session.
  600. */
  601. static int nfsd4_get_drc_mem(int slotsize, u32 num)
  602. {
  603. int avail;
  604. num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
  605. spin_lock(&nfsd_drc_lock);
  606. avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
  607. nfsd_drc_max_mem - nfsd_drc_mem_used);
  608. num = min_t(int, num, avail / slotsize);
  609. nfsd_drc_mem_used += num * slotsize;
  610. spin_unlock(&nfsd_drc_lock);
  611. return num;
  612. }
  613. static void nfsd4_put_drc_mem(int slotsize, int num)
  614. {
  615. spin_lock(&nfsd_drc_lock);
  616. nfsd_drc_mem_used -= slotsize * num;
  617. spin_unlock(&nfsd_drc_lock);
  618. }
  619. static struct nfsd4_session *alloc_session(int slotsize, int numslots)
  620. {
  621. struct nfsd4_session *new;
  622. int mem, i;
  623. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  624. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  625. mem = numslots * sizeof(struct nfsd4_slot *);
  626. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  627. if (!new)
  628. return NULL;
  629. /* allocate each struct nfsd4_slot and data cache in one piece */
  630. for (i = 0; i < numslots; i++) {
  631. mem = sizeof(struct nfsd4_slot) + slotsize;
  632. new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
  633. if (!new->se_slots[i])
  634. goto out_free;
  635. }
  636. return new;
  637. out_free:
  638. while (i--)
  639. kfree(new->se_slots[i]);
  640. kfree(new);
  641. return NULL;
  642. }
  643. static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
  644. {
  645. u32 maxrpc = nfsd_serv->sv_max_mesg;
  646. new->maxreqs = numslots;
  647. new->maxresp_cached = min_t(u32, req->maxresp_cached,
  648. slotsize + NFSD_MIN_HDR_SEQ_SZ);
  649. new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
  650. new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
  651. new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  652. }
  653. static void free_conn(struct nfsd4_conn *c)
  654. {
  655. svc_xprt_put(c->cn_xprt);
  656. kfree(c);
  657. }
  658. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  659. {
  660. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  661. struct nfs4_client *clp = c->cn_session->se_client;
  662. spin_lock(&clp->cl_lock);
  663. if (!list_empty(&c->cn_persession)) {
  664. list_del(&c->cn_persession);
  665. free_conn(c);
  666. }
  667. spin_unlock(&clp->cl_lock);
  668. nfsd4_probe_callback(clp);
  669. }
  670. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  671. {
  672. struct nfsd4_conn *conn;
  673. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  674. if (!conn)
  675. return NULL;
  676. svc_xprt_get(rqstp->rq_xprt);
  677. conn->cn_xprt = rqstp->rq_xprt;
  678. conn->cn_flags = flags;
  679. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  680. return conn;
  681. }
  682. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  683. {
  684. conn->cn_session = ses;
  685. list_add(&conn->cn_persession, &ses->se_conns);
  686. }
  687. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  688. {
  689. struct nfs4_client *clp = ses->se_client;
  690. spin_lock(&clp->cl_lock);
  691. __nfsd4_hash_conn(conn, ses);
  692. spin_unlock(&clp->cl_lock);
  693. }
  694. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  695. {
  696. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  697. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  698. }
  699. static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
  700. {
  701. struct nfsd4_conn *conn;
  702. int ret;
  703. conn = alloc_conn(rqstp, dir);
  704. if (!conn)
  705. return nfserr_jukebox;
  706. nfsd4_hash_conn(conn, ses);
  707. ret = nfsd4_register_conn(conn);
  708. if (ret)
  709. /* oops; xprt is already down: */
  710. nfsd4_conn_lost(&conn->cn_xpt_user);
  711. return nfs_ok;
  712. }
  713. static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
  714. {
  715. u32 dir = NFS4_CDFC4_FORE;
  716. if (ses->se_flags & SESSION4_BACK_CHAN)
  717. dir |= NFS4_CDFC4_BACK;
  718. return nfsd4_new_conn(rqstp, ses, dir);
  719. }
  720. /* must be called under client_lock */
  721. static void nfsd4_del_conns(struct nfsd4_session *s)
  722. {
  723. struct nfs4_client *clp = s->se_client;
  724. struct nfsd4_conn *c;
  725. spin_lock(&clp->cl_lock);
  726. while (!list_empty(&s->se_conns)) {
  727. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  728. list_del_init(&c->cn_persession);
  729. spin_unlock(&clp->cl_lock);
  730. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  731. free_conn(c);
  732. spin_lock(&clp->cl_lock);
  733. }
  734. spin_unlock(&clp->cl_lock);
  735. }
  736. static void free_session(struct kref *kref)
  737. {
  738. struct nfsd4_session *ses;
  739. int mem;
  740. BUG_ON(!spin_is_locked(&client_lock));
  741. ses = container_of(kref, struct nfsd4_session, se_ref);
  742. nfsd4_del_conns(ses);
  743. spin_lock(&nfsd_drc_lock);
  744. mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
  745. nfsd_drc_mem_used -= mem;
  746. spin_unlock(&nfsd_drc_lock);
  747. free_session_slots(ses);
  748. kfree(ses);
  749. }
  750. void nfsd4_put_session(struct nfsd4_session *ses)
  751. {
  752. spin_lock(&client_lock);
  753. nfsd4_put_session_locked(ses);
  754. spin_unlock(&client_lock);
  755. }
  756. static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  757. {
  758. struct nfsd4_session *new;
  759. struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
  760. int numslots, slotsize;
  761. int status;
  762. int idx;
  763. /*
  764. * Note decreasing slot size below client's request may
  765. * make it difficult for client to function correctly, whereas
  766. * decreasing the number of slots will (just?) affect
  767. * performance. When short on memory we therefore prefer to
  768. * decrease number of slots instead of their size.
  769. */
  770. slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
  771. numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
  772. if (numslots < 1)
  773. return NULL;
  774. new = alloc_session(slotsize, numslots);
  775. if (!new) {
  776. nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
  777. return NULL;
  778. }
  779. init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
  780. new->se_client = clp;
  781. gen_sessionid(new);
  782. INIT_LIST_HEAD(&new->se_conns);
  783. new->se_cb_seq_nr = 1;
  784. new->se_flags = cses->flags;
  785. new->se_cb_prog = cses->callback_prog;
  786. kref_init(&new->se_ref);
  787. idx = hash_sessionid(&new->se_sessionid);
  788. spin_lock(&client_lock);
  789. list_add(&new->se_hash, &sessionid_hashtbl[idx]);
  790. spin_lock(&clp->cl_lock);
  791. list_add(&new->se_perclnt, &clp->cl_sessions);
  792. spin_unlock(&clp->cl_lock);
  793. spin_unlock(&client_lock);
  794. status = nfsd4_new_conn_from_crses(rqstp, new);
  795. /* whoops: benny points out, status is ignored! (err, or bogus) */
  796. if (status) {
  797. spin_lock(&client_lock);
  798. free_session(&new->se_ref);
  799. spin_unlock(&client_lock);
  800. return NULL;
  801. }
  802. if (cses->flags & SESSION4_BACK_CHAN) {
  803. struct sockaddr *sa = svc_addr(rqstp);
  804. /*
  805. * This is a little silly; with sessions there's no real
  806. * use for the callback address. Use the peer address
  807. * as a reasonable default for now, but consider fixing
  808. * the rpc client not to require an address in the
  809. * future:
  810. */
  811. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  812. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  813. }
  814. nfsd4_probe_callback(clp);
  815. return new;
  816. }
  817. /* caller must hold client_lock */
  818. static struct nfsd4_session *
  819. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
  820. {
  821. struct nfsd4_session *elem;
  822. int idx;
  823. dump_sessionid(__func__, sessionid);
  824. idx = hash_sessionid(sessionid);
  825. /* Search in the appropriate list */
  826. list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
  827. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  828. NFS4_MAX_SESSIONID_LEN)) {
  829. return elem;
  830. }
  831. }
  832. dprintk("%s: session not found\n", __func__);
  833. return NULL;
  834. }
  835. /* caller must hold client_lock */
  836. static void
  837. unhash_session(struct nfsd4_session *ses)
  838. {
  839. list_del(&ses->se_hash);
  840. spin_lock(&ses->se_client->cl_lock);
  841. list_del(&ses->se_perclnt);
  842. spin_unlock(&ses->se_client->cl_lock);
  843. }
  844. /* must be called under the client_lock */
  845. static inline void
  846. renew_client_locked(struct nfs4_client *clp)
  847. {
  848. if (is_client_expired(clp)) {
  849. dprintk("%s: client (clientid %08x/%08x) already expired\n",
  850. __func__,
  851. clp->cl_clientid.cl_boot,
  852. clp->cl_clientid.cl_id);
  853. return;
  854. }
  855. dprintk("renewing client (clientid %08x/%08x)\n",
  856. clp->cl_clientid.cl_boot,
  857. clp->cl_clientid.cl_id);
  858. list_move_tail(&clp->cl_lru, &client_lru);
  859. clp->cl_time = get_seconds();
  860. }
  861. static inline void
  862. renew_client(struct nfs4_client *clp)
  863. {
  864. spin_lock(&client_lock);
  865. renew_client_locked(clp);
  866. spin_unlock(&client_lock);
  867. }
  868. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  869. static int
  870. STALE_CLIENTID(clientid_t *clid)
  871. {
  872. if (clid->cl_boot == boot_time)
  873. return 0;
  874. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  875. clid->cl_boot, clid->cl_id, boot_time);
  876. return 1;
  877. }
  878. /*
  879. * XXX Should we use a slab cache ?
  880. * This type of memory management is somewhat inefficient, but we use it
  881. * anyway since SETCLIENTID is not a common operation.
  882. */
  883. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  884. {
  885. struct nfs4_client *clp;
  886. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  887. if (clp == NULL)
  888. return NULL;
  889. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  890. if (clp->cl_name.data == NULL) {
  891. kfree(clp);
  892. return NULL;
  893. }
  894. clp->cl_name.len = name.len;
  895. return clp;
  896. }
  897. static inline void
  898. free_client(struct nfs4_client *clp)
  899. {
  900. BUG_ON(!spin_is_locked(&client_lock));
  901. while (!list_empty(&clp->cl_sessions)) {
  902. struct nfsd4_session *ses;
  903. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  904. se_perclnt);
  905. list_del(&ses->se_perclnt);
  906. nfsd4_put_session_locked(ses);
  907. }
  908. if (clp->cl_cred.cr_group_info)
  909. put_group_info(clp->cl_cred.cr_group_info);
  910. kfree(clp->cl_principal);
  911. kfree(clp->cl_name.data);
  912. kfree(clp);
  913. }
  914. void
  915. release_session_client(struct nfsd4_session *session)
  916. {
  917. struct nfs4_client *clp = session->se_client;
  918. if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
  919. return;
  920. if (is_client_expired(clp)) {
  921. free_client(clp);
  922. session->se_client = NULL;
  923. } else
  924. renew_client_locked(clp);
  925. spin_unlock(&client_lock);
  926. }
  927. /* must be called under the client_lock */
  928. static inline void
  929. unhash_client_locked(struct nfs4_client *clp)
  930. {
  931. struct nfsd4_session *ses;
  932. mark_client_expired(clp);
  933. list_del(&clp->cl_lru);
  934. spin_lock(&clp->cl_lock);
  935. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  936. list_del_init(&ses->se_hash);
  937. spin_unlock(&clp->cl_lock);
  938. }
  939. static void
  940. expire_client(struct nfs4_client *clp)
  941. {
  942. struct nfs4_openowner *oo;
  943. struct nfs4_delegation *dp;
  944. struct list_head reaplist;
  945. INIT_LIST_HEAD(&reaplist);
  946. spin_lock(&recall_lock);
  947. while (!list_empty(&clp->cl_delegations)) {
  948. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  949. list_del_init(&dp->dl_perclnt);
  950. list_move(&dp->dl_recall_lru, &reaplist);
  951. }
  952. spin_unlock(&recall_lock);
  953. while (!list_empty(&reaplist)) {
  954. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  955. unhash_delegation(dp);
  956. }
  957. while (!list_empty(&clp->cl_openowners)) {
  958. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  959. release_openowner(oo);
  960. }
  961. nfsd4_shutdown_callback(clp);
  962. if (clp->cl_cb_conn.cb_xprt)
  963. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  964. list_del(&clp->cl_idhash);
  965. list_del(&clp->cl_strhash);
  966. spin_lock(&client_lock);
  967. unhash_client_locked(clp);
  968. if (atomic_read(&clp->cl_refcount) == 0)
  969. free_client(clp);
  970. spin_unlock(&client_lock);
  971. }
  972. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  973. {
  974. memcpy(target->cl_verifier.data, source->data,
  975. sizeof(target->cl_verifier.data));
  976. }
  977. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  978. {
  979. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  980. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  981. }
  982. static void copy_cred(struct svc_cred *target, struct svc_cred *source)
  983. {
  984. target->cr_uid = source->cr_uid;
  985. target->cr_gid = source->cr_gid;
  986. target->cr_group_info = source->cr_group_info;
  987. get_group_info(target->cr_group_info);
  988. }
  989. static int same_name(const char *n1, const char *n2)
  990. {
  991. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  992. }
  993. static int
  994. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  995. {
  996. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  997. }
  998. static int
  999. same_clid(clientid_t *cl1, clientid_t *cl2)
  1000. {
  1001. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1002. }
  1003. /* XXX what about NGROUP */
  1004. static int
  1005. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1006. {
  1007. return cr1->cr_uid == cr2->cr_uid;
  1008. }
  1009. static void gen_clid(struct nfs4_client *clp)
  1010. {
  1011. static u32 current_clientid = 1;
  1012. clp->cl_clientid.cl_boot = boot_time;
  1013. clp->cl_clientid.cl_id = current_clientid++;
  1014. }
  1015. static void gen_confirm(struct nfs4_client *clp)
  1016. {
  1017. __be32 verf[2];
  1018. static u32 i;
  1019. verf[0] = (__be32)get_seconds();
  1020. verf[1] = (__be32)i++;
  1021. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1022. }
  1023. static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
  1024. {
  1025. return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1026. }
  1027. static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1028. {
  1029. struct nfs4_stid *s;
  1030. s = find_stateid(cl, t);
  1031. if (!s)
  1032. return NULL;
  1033. if (typemask & s->sc_type)
  1034. return s;
  1035. return NULL;
  1036. }
  1037. static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
  1038. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1039. {
  1040. struct nfs4_client *clp;
  1041. struct sockaddr *sa = svc_addr(rqstp);
  1042. char *princ;
  1043. clp = alloc_client(name);
  1044. if (clp == NULL)
  1045. return NULL;
  1046. INIT_LIST_HEAD(&clp->cl_sessions);
  1047. princ = svc_gss_principal(rqstp);
  1048. if (princ) {
  1049. clp->cl_principal = kstrdup(princ, GFP_KERNEL);
  1050. if (clp->cl_principal == NULL) {
  1051. spin_lock(&client_lock);
  1052. free_client(clp);
  1053. spin_unlock(&client_lock);
  1054. return NULL;
  1055. }
  1056. }
  1057. idr_init(&clp->cl_stateids);
  1058. memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
  1059. atomic_set(&clp->cl_refcount, 0);
  1060. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1061. INIT_LIST_HEAD(&clp->cl_idhash);
  1062. INIT_LIST_HEAD(&clp->cl_strhash);
  1063. INIT_LIST_HEAD(&clp->cl_openowners);
  1064. INIT_LIST_HEAD(&clp->cl_delegations);
  1065. INIT_LIST_HEAD(&clp->cl_lru);
  1066. INIT_LIST_HEAD(&clp->cl_callbacks);
  1067. spin_lock_init(&clp->cl_lock);
  1068. INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
  1069. clp->cl_time = get_seconds();
  1070. clear_bit(0, &clp->cl_cb_slot_busy);
  1071. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1072. copy_verf(clp, verf);
  1073. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1074. clp->cl_flavor = rqstp->rq_flavor;
  1075. copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1076. gen_confirm(clp);
  1077. clp->cl_cb_session = NULL;
  1078. return clp;
  1079. }
  1080. static void
  1081. add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
  1082. {
  1083. unsigned int idhashval;
  1084. list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
  1085. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1086. list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
  1087. renew_client(clp);
  1088. }
  1089. static void
  1090. move_to_confirmed(struct nfs4_client *clp)
  1091. {
  1092. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1093. unsigned int strhashval;
  1094. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1095. list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
  1096. strhashval = clientstr_hashval(clp->cl_recdir);
  1097. list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
  1098. renew_client(clp);
  1099. }
  1100. static struct nfs4_client *
  1101. find_confirmed_client(clientid_t *clid)
  1102. {
  1103. struct nfs4_client *clp;
  1104. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1105. list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
  1106. if (same_clid(&clp->cl_clientid, clid)) {
  1107. renew_client(clp);
  1108. return clp;
  1109. }
  1110. }
  1111. return NULL;
  1112. }
  1113. static struct nfs4_client *
  1114. find_unconfirmed_client(clientid_t *clid)
  1115. {
  1116. struct nfs4_client *clp;
  1117. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1118. list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
  1119. if (same_clid(&clp->cl_clientid, clid))
  1120. return clp;
  1121. }
  1122. return NULL;
  1123. }
  1124. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1125. {
  1126. return clp->cl_exchange_flags != 0;
  1127. }
  1128. static struct nfs4_client *
  1129. find_confirmed_client_by_str(const char *dname, unsigned int hashval)
  1130. {
  1131. struct nfs4_client *clp;
  1132. list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
  1133. if (same_name(clp->cl_recdir, dname))
  1134. return clp;
  1135. }
  1136. return NULL;
  1137. }
  1138. static struct nfs4_client *
  1139. find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
  1140. {
  1141. struct nfs4_client *clp;
  1142. list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
  1143. if (same_name(clp->cl_recdir, dname))
  1144. return clp;
  1145. }
  1146. return NULL;
  1147. }
  1148. static void
  1149. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1150. {
  1151. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1152. struct sockaddr *sa = svc_addr(rqstp);
  1153. u32 scopeid = rpc_get_scope_id(sa);
  1154. unsigned short expected_family;
  1155. /* Currently, we only support tcp and tcp6 for the callback channel */
  1156. if (se->se_callback_netid_len == 3 &&
  1157. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1158. expected_family = AF_INET;
  1159. else if (se->se_callback_netid_len == 4 &&
  1160. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1161. expected_family = AF_INET6;
  1162. else
  1163. goto out_err;
  1164. conn->cb_addrlen = rpc_uaddr2sockaddr(&init_net, se->se_callback_addr_val,
  1165. se->se_callback_addr_len,
  1166. (struct sockaddr *)&conn->cb_addr,
  1167. sizeof(conn->cb_addr));
  1168. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1169. goto out_err;
  1170. if (conn->cb_addr.ss_family == AF_INET6)
  1171. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1172. conn->cb_prog = se->se_callback_prog;
  1173. conn->cb_ident = se->se_callback_ident;
  1174. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1175. return;
  1176. out_err:
  1177. conn->cb_addr.ss_family = AF_UNSPEC;
  1178. conn->cb_addrlen = 0;
  1179. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1180. "will not receive delegations\n",
  1181. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1182. return;
  1183. }
  1184. /*
  1185. * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
  1186. */
  1187. void
  1188. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1189. {
  1190. struct nfsd4_slot *slot = resp->cstate.slot;
  1191. unsigned int base;
  1192. dprintk("--> %s slot %p\n", __func__, slot);
  1193. slot->sl_opcnt = resp->opcnt;
  1194. slot->sl_status = resp->cstate.status;
  1195. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1196. if (nfsd4_not_cached(resp)) {
  1197. slot->sl_datalen = 0;
  1198. return;
  1199. }
  1200. slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
  1201. base = (char *)resp->cstate.datap -
  1202. (char *)resp->xbuf->head[0].iov_base;
  1203. if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
  1204. slot->sl_datalen))
  1205. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1206. return;
  1207. }
  1208. /*
  1209. * Encode the replay sequence operation from the slot values.
  1210. * If cachethis is FALSE encode the uncached rep error on the next
  1211. * operation which sets resp->p and increments resp->opcnt for
  1212. * nfs4svc_encode_compoundres.
  1213. *
  1214. */
  1215. static __be32
  1216. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1217. struct nfsd4_compoundres *resp)
  1218. {
  1219. struct nfsd4_op *op;
  1220. struct nfsd4_slot *slot = resp->cstate.slot;
  1221. /* Encode the replayed sequence operation */
  1222. op = &args->ops[resp->opcnt - 1];
  1223. nfsd4_encode_operation(resp, op);
  1224. /* Return nfserr_retry_uncached_rep in next operation. */
  1225. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1226. op = &args->ops[resp->opcnt++];
  1227. op->status = nfserr_retry_uncached_rep;
  1228. nfsd4_encode_operation(resp, op);
  1229. }
  1230. return op->status;
  1231. }
  1232. /*
  1233. * The sequence operation is not cached because we can use the slot and
  1234. * session values.
  1235. */
  1236. __be32
  1237. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1238. struct nfsd4_sequence *seq)
  1239. {
  1240. struct nfsd4_slot *slot = resp->cstate.slot;
  1241. __be32 status;
  1242. dprintk("--> %s slot %p\n", __func__, slot);
  1243. /* Either returns 0 or nfserr_retry_uncached */
  1244. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1245. if (status == nfserr_retry_uncached_rep)
  1246. return status;
  1247. /* The sequence operation has been encoded, cstate->datap set. */
  1248. memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
  1249. resp->opcnt = slot->sl_opcnt;
  1250. resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
  1251. status = slot->sl_status;
  1252. return status;
  1253. }
  1254. /*
  1255. * Set the exchange_id flags returned by the server.
  1256. */
  1257. static void
  1258. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1259. {
  1260. /* pNFS is not supported */
  1261. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1262. /* Referrals are supported, Migration is not. */
  1263. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1264. /* set the wire flags to return to client. */
  1265. clid->flags = new->cl_exchange_flags;
  1266. }
  1267. __be32
  1268. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1269. struct nfsd4_compound_state *cstate,
  1270. struct nfsd4_exchange_id *exid)
  1271. {
  1272. struct nfs4_client *unconf, *conf, *new;
  1273. int status;
  1274. unsigned int strhashval;
  1275. char dname[HEXDIR_LEN];
  1276. char addr_str[INET6_ADDRSTRLEN];
  1277. nfs4_verifier verf = exid->verifier;
  1278. struct sockaddr *sa = svc_addr(rqstp);
  1279. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1280. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1281. "ip_addr=%s flags %x, spa_how %d\n",
  1282. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1283. addr_str, exid->flags, exid->spa_how);
  1284. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1285. return nfserr_inval;
  1286. /* Currently only support SP4_NONE */
  1287. switch (exid->spa_how) {
  1288. case SP4_NONE:
  1289. break;
  1290. case SP4_SSV:
  1291. return nfserr_serverfault;
  1292. default:
  1293. BUG(); /* checked by xdr code */
  1294. case SP4_MACH_CRED:
  1295. return nfserr_serverfault; /* no excuse :-/ */
  1296. }
  1297. status = nfs4_make_rec_clidname(dname, &exid->clname);
  1298. if (status)
  1299. goto error;
  1300. strhashval = clientstr_hashval(dname);
  1301. nfs4_lock_state();
  1302. status = nfs_ok;
  1303. conf = find_confirmed_client_by_str(dname, strhashval);
  1304. if (conf) {
  1305. if (!clp_used_exchangeid(conf)) {
  1306. status = nfserr_clid_inuse; /* XXX: ? */
  1307. goto out;
  1308. }
  1309. if (!same_verf(&verf, &conf->cl_verifier)) {
  1310. /* 18.35.4 case 8 */
  1311. if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
  1312. status = nfserr_not_same;
  1313. goto out;
  1314. }
  1315. /* Client reboot: destroy old state */
  1316. expire_client(conf);
  1317. goto out_new;
  1318. }
  1319. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1320. /* 18.35.4 case 9 */
  1321. if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
  1322. status = nfserr_perm;
  1323. goto out;
  1324. }
  1325. expire_client(conf);
  1326. goto out_new;
  1327. }
  1328. /*
  1329. * Set bit when the owner id and verifier map to an already
  1330. * confirmed client id (18.35.3).
  1331. */
  1332. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1333. /*
  1334. * Falling into 18.35.4 case 2, possible router replay.
  1335. * Leave confirmed record intact and return same result.
  1336. */
  1337. copy_verf(conf, &verf);
  1338. new = conf;
  1339. goto out_copy;
  1340. }
  1341. /* 18.35.4 case 7 */
  1342. if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
  1343. status = nfserr_noent;
  1344. goto out;
  1345. }
  1346. unconf = find_unconfirmed_client_by_str(dname, strhashval);
  1347. if (unconf) {
  1348. /*
  1349. * Possible retry or client restart. Per 18.35.4 case 4,
  1350. * a new unconfirmed record should be generated regardless
  1351. * of whether any properties have changed.
  1352. */
  1353. expire_client(unconf);
  1354. }
  1355. out_new:
  1356. /* Normal case */
  1357. new = create_client(exid->clname, dname, rqstp, &verf);
  1358. if (new == NULL) {
  1359. status = nfserr_jukebox;
  1360. goto out;
  1361. }
  1362. gen_clid(new);
  1363. add_to_unconfirmed(new, strhashval);
  1364. out_copy:
  1365. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1366. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1367. exid->seqid = 1;
  1368. nfsd4_set_ex_flags(new, exid);
  1369. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1370. new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
  1371. status = nfs_ok;
  1372. out:
  1373. nfs4_unlock_state();
  1374. error:
  1375. dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
  1376. return status;
  1377. }
  1378. static int
  1379. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1380. {
  1381. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1382. slot_seqid);
  1383. /* The slot is in use, and no response has been sent. */
  1384. if (slot_inuse) {
  1385. if (seqid == slot_seqid)
  1386. return nfserr_jukebox;
  1387. else
  1388. return nfserr_seq_misordered;
  1389. }
  1390. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1391. if (likely(seqid == slot_seqid + 1))
  1392. return nfs_ok;
  1393. if (seqid == slot_seqid)
  1394. return nfserr_replay_cache;
  1395. return nfserr_seq_misordered;
  1396. }
  1397. /*
  1398. * Cache the create session result into the create session single DRC
  1399. * slot cache by saving the xdr structure. sl_seqid has been set.
  1400. * Do this for solo or embedded create session operations.
  1401. */
  1402. static void
  1403. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1404. struct nfsd4_clid_slot *slot, int nfserr)
  1405. {
  1406. slot->sl_status = nfserr;
  1407. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1408. }
  1409. static __be32
  1410. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1411. struct nfsd4_clid_slot *slot)
  1412. {
  1413. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1414. return slot->sl_status;
  1415. }
  1416. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1417. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1418. 1 + /* MIN tag is length with zero, only length */ \
  1419. 3 + /* version, opcount, opcode */ \
  1420. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1421. /* seqid, slotID, slotID, cache */ \
  1422. 4 ) * sizeof(__be32))
  1423. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1424. 2 + /* verifier: AUTH_NULL, length 0 */\
  1425. 1 + /* status */ \
  1426. 1 + /* MIN tag is length with zero, only length */ \
  1427. 3 + /* opcount, opcode, opstatus*/ \
  1428. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1429. /* seqid, slotID, slotID, slotID, status */ \
  1430. 5 ) * sizeof(__be32))
  1431. static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
  1432. {
  1433. return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
  1434. || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
  1435. }
  1436. __be32
  1437. nfsd4_create_session(struct svc_rqst *rqstp,
  1438. struct nfsd4_compound_state *cstate,
  1439. struct nfsd4_create_session *cr_ses)
  1440. {
  1441. struct sockaddr *sa = svc_addr(rqstp);
  1442. struct nfs4_client *conf, *unconf;
  1443. struct nfsd4_session *new;
  1444. struct nfsd4_clid_slot *cs_slot = NULL;
  1445. bool confirm_me = false;
  1446. int status = 0;
  1447. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  1448. return nfserr_inval;
  1449. nfs4_lock_state();
  1450. unconf = find_unconfirmed_client(&cr_ses->clientid);
  1451. conf = find_confirmed_client(&cr_ses->clientid);
  1452. if (conf) {
  1453. cs_slot = &conf->cl_cs_slot;
  1454. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1455. if (status == nfserr_replay_cache) {
  1456. dprintk("Got a create_session replay! seqid= %d\n",
  1457. cs_slot->sl_seqid);
  1458. /* Return the cached reply status */
  1459. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  1460. goto out;
  1461. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  1462. status = nfserr_seq_misordered;
  1463. dprintk("Sequence misordered!\n");
  1464. dprintk("Expected seqid= %d but got seqid= %d\n",
  1465. cs_slot->sl_seqid, cr_ses->seqid);
  1466. goto out;
  1467. }
  1468. } else if (unconf) {
  1469. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1470. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  1471. status = nfserr_clid_inuse;
  1472. goto out;
  1473. }
  1474. cs_slot = &unconf->cl_cs_slot;
  1475. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1476. if (status) {
  1477. /* an unconfirmed replay returns misordered */
  1478. status = nfserr_seq_misordered;
  1479. goto out;
  1480. }
  1481. confirm_me = true;
  1482. conf = unconf;
  1483. } else {
  1484. status = nfserr_stale_clientid;
  1485. goto out;
  1486. }
  1487. /*
  1488. * XXX: we should probably set this at creation time, and check
  1489. * for consistent minorversion use throughout:
  1490. */
  1491. conf->cl_minorversion = 1;
  1492. /*
  1493. * We do not support RDMA or persistent sessions
  1494. */
  1495. cr_ses->flags &= ~SESSION4_PERSIST;
  1496. cr_ses->flags &= ~SESSION4_RDMA;
  1497. status = nfserr_toosmall;
  1498. if (check_forechannel_attrs(cr_ses->fore_channel))
  1499. goto out;
  1500. status = nfserr_jukebox;
  1501. new = alloc_init_session(rqstp, conf, cr_ses);
  1502. if (!new)
  1503. goto out;
  1504. status = nfs_ok;
  1505. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  1506. NFS4_MAX_SESSIONID_LEN);
  1507. memcpy(&cr_ses->fore_channel, &new->se_fchannel,
  1508. sizeof(struct nfsd4_channel_attrs));
  1509. cs_slot->sl_seqid++;
  1510. cr_ses->seqid = cs_slot->sl_seqid;
  1511. /* cache solo and embedded create sessions under the state lock */
  1512. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  1513. if (confirm_me)
  1514. move_to_confirmed(conf);
  1515. out:
  1516. nfs4_unlock_state();
  1517. dprintk("%s returns %d\n", __func__, ntohl(status));
  1518. return status;
  1519. }
  1520. static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
  1521. {
  1522. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1523. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  1524. return argp->opcnt == resp->opcnt;
  1525. }
  1526. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  1527. {
  1528. switch (*dir) {
  1529. case NFS4_CDFC4_FORE:
  1530. case NFS4_CDFC4_BACK:
  1531. return nfs_ok;
  1532. case NFS4_CDFC4_FORE_OR_BOTH:
  1533. case NFS4_CDFC4_BACK_OR_BOTH:
  1534. *dir = NFS4_CDFC4_BOTH;
  1535. return nfs_ok;
  1536. };
  1537. return nfserr_inval;
  1538. }
  1539. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  1540. struct nfsd4_compound_state *cstate,
  1541. struct nfsd4_bind_conn_to_session *bcts)
  1542. {
  1543. __be32 status;
  1544. if (!nfsd4_last_compound_op(rqstp))
  1545. return nfserr_not_only_op;
  1546. spin_lock(&client_lock);
  1547. cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
  1548. /* Sorta weird: we only need the refcnt'ing because new_conn acquires
  1549. * client_lock iself: */
  1550. if (cstate->session) {
  1551. nfsd4_get_session(cstate->session);
  1552. atomic_inc(&cstate->session->se_client->cl_refcount);
  1553. }
  1554. spin_unlock(&client_lock);
  1555. if (!cstate->session)
  1556. return nfserr_badsession;
  1557. status = nfsd4_map_bcts_dir(&bcts->dir);
  1558. if (!status)
  1559. nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
  1560. return status;
  1561. }
  1562. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  1563. {
  1564. if (!session)
  1565. return 0;
  1566. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  1567. }
  1568. __be32
  1569. nfsd4_destroy_session(struct svc_rqst *r,
  1570. struct nfsd4_compound_state *cstate,
  1571. struct nfsd4_destroy_session *sessionid)
  1572. {
  1573. struct nfsd4_session *ses;
  1574. u32 status = nfserr_badsession;
  1575. /* Notes:
  1576. * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
  1577. * - Should we return nfserr_back_chan_busy if waiting for
  1578. * callbacks on to-be-destroyed session?
  1579. * - Do we need to clear any callback info from previous session?
  1580. */
  1581. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  1582. if (!nfsd4_last_compound_op(r))
  1583. return nfserr_not_only_op;
  1584. }
  1585. dump_sessionid(__func__, &sessionid->sessionid);
  1586. spin_lock(&client_lock);
  1587. ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
  1588. if (!ses) {
  1589. spin_unlock(&client_lock);
  1590. goto out;
  1591. }
  1592. unhash_session(ses);
  1593. spin_unlock(&client_lock);
  1594. nfs4_lock_state();
  1595. nfsd4_probe_callback_sync(ses->se_client);
  1596. nfs4_unlock_state();
  1597. spin_lock(&client_lock);
  1598. nfsd4_del_conns(ses);
  1599. nfsd4_put_session_locked(ses);
  1600. spin_unlock(&client_lock);
  1601. status = nfs_ok;
  1602. out:
  1603. dprintk("%s returns %d\n", __func__, ntohl(status));
  1604. return status;
  1605. }
  1606. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  1607. {
  1608. struct nfsd4_conn *c;
  1609. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1610. if (c->cn_xprt == xpt) {
  1611. return c;
  1612. }
  1613. }
  1614. return NULL;
  1615. }
  1616. static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  1617. {
  1618. struct nfs4_client *clp = ses->se_client;
  1619. struct nfsd4_conn *c;
  1620. int ret;
  1621. spin_lock(&clp->cl_lock);
  1622. c = __nfsd4_find_conn(new->cn_xprt, ses);
  1623. if (c) {
  1624. spin_unlock(&clp->cl_lock);
  1625. free_conn(new);
  1626. return;
  1627. }
  1628. __nfsd4_hash_conn(new, ses);
  1629. spin_unlock(&clp->cl_lock);
  1630. ret = nfsd4_register_conn(new);
  1631. if (ret)
  1632. /* oops; xprt is already down: */
  1633. nfsd4_conn_lost(&new->cn_xpt_user);
  1634. return;
  1635. }
  1636. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  1637. {
  1638. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1639. return args->opcnt > session->se_fchannel.maxops;
  1640. }
  1641. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  1642. struct nfsd4_session *session)
  1643. {
  1644. struct xdr_buf *xb = &rqstp->rq_arg;
  1645. return xb->len > session->se_fchannel.maxreq_sz;
  1646. }
  1647. __be32
  1648. nfsd4_sequence(struct svc_rqst *rqstp,
  1649. struct nfsd4_compound_state *cstate,
  1650. struct nfsd4_sequence *seq)
  1651. {
  1652. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1653. struct nfsd4_session *session;
  1654. struct nfsd4_slot *slot;
  1655. struct nfsd4_conn *conn;
  1656. int status;
  1657. if (resp->opcnt != 1)
  1658. return nfserr_sequence_pos;
  1659. /*
  1660. * Will be either used or freed by nfsd4_sequence_check_conn
  1661. * below.
  1662. */
  1663. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  1664. if (!conn)
  1665. return nfserr_jukebox;
  1666. spin_lock(&client_lock);
  1667. status = nfserr_badsession;
  1668. session = find_in_sessionid_hashtbl(&seq->sessionid);
  1669. if (!session)
  1670. goto out;
  1671. status = nfserr_too_many_ops;
  1672. if (nfsd4_session_too_many_ops(rqstp, session))
  1673. goto out;
  1674. status = nfserr_req_too_big;
  1675. if (nfsd4_request_too_big(rqstp, session))
  1676. goto out;
  1677. status = nfserr_badslot;
  1678. if (seq->slotid >= session->se_fchannel.maxreqs)
  1679. goto out;
  1680. slot = session->se_slots[seq->slotid];
  1681. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1682. /* We do not negotiate the number of slots yet, so set the
  1683. * maxslots to the session maxreqs which is used to encode
  1684. * sr_highest_slotid and the sr_target_slot id to maxslots */
  1685. seq->maxslots = session->se_fchannel.maxreqs;
  1686. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  1687. slot->sl_flags & NFSD4_SLOT_INUSE);
  1688. if (status == nfserr_replay_cache) {
  1689. status = nfserr_seq_misordered;
  1690. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  1691. goto out;
  1692. cstate->slot = slot;
  1693. cstate->session = session;
  1694. /* Return the cached reply status and set cstate->status
  1695. * for nfsd4_proc_compound processing */
  1696. status = nfsd4_replay_cache_entry(resp, seq);
  1697. cstate->status = nfserr_replay_cache;
  1698. goto out;
  1699. }
  1700. if (status)
  1701. goto out;
  1702. nfsd4_sequence_check_conn(conn, session);
  1703. conn = NULL;
  1704. /* Success! bump slot seqid */
  1705. slot->sl_seqid = seq->seqid;
  1706. slot->sl_flags |= NFSD4_SLOT_INUSE;
  1707. if (seq->cachethis)
  1708. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  1709. else
  1710. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  1711. cstate->slot = slot;
  1712. cstate->session = session;
  1713. out:
  1714. /* Hold a session reference until done processing the compound. */
  1715. if (cstate->session) {
  1716. struct nfs4_client *clp = session->se_client;
  1717. nfsd4_get_session(cstate->session);
  1718. atomic_inc(&clp->cl_refcount);
  1719. switch (clp->cl_cb_state) {
  1720. case NFSD4_CB_DOWN:
  1721. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  1722. break;
  1723. case NFSD4_CB_FAULT:
  1724. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  1725. break;
  1726. default:
  1727. seq->status_flags = 0;
  1728. }
  1729. }
  1730. kfree(conn);
  1731. spin_unlock(&client_lock);
  1732. dprintk("%s: return %d\n", __func__, ntohl(status));
  1733. return status;
  1734. }
  1735. static inline bool has_resources(struct nfs4_client *clp)
  1736. {
  1737. return !list_empty(&clp->cl_openowners)
  1738. || !list_empty(&clp->cl_delegations)
  1739. || !list_empty(&clp->cl_sessions);
  1740. }
  1741. __be32
  1742. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  1743. {
  1744. struct nfs4_client *conf, *unconf, *clp;
  1745. int status = 0;
  1746. nfs4_lock_state();
  1747. unconf = find_unconfirmed_client(&dc->clientid);
  1748. conf = find_confirmed_client(&dc->clientid);
  1749. if (conf) {
  1750. clp = conf;
  1751. if (!is_client_expired(conf) && has_resources(conf)) {
  1752. status = nfserr_clientid_busy;
  1753. goto out;
  1754. }
  1755. /* rfc5661 18.50.3 */
  1756. if (cstate->session && conf == cstate->session->se_client) {
  1757. status = nfserr_clientid_busy;
  1758. goto out;
  1759. }
  1760. } else if (unconf)
  1761. clp = unconf;
  1762. else {
  1763. status = nfserr_stale_clientid;
  1764. goto out;
  1765. }
  1766. expire_client(clp);
  1767. out:
  1768. nfs4_unlock_state();
  1769. dprintk("%s return %d\n", __func__, ntohl(status));
  1770. return status;
  1771. }
  1772. __be32
  1773. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  1774. {
  1775. int status = 0;
  1776. if (rc->rca_one_fs) {
  1777. if (!cstate->current_fh.fh_dentry)
  1778. return nfserr_nofilehandle;
  1779. /*
  1780. * We don't take advantage of the rca_one_fs case.
  1781. * That's OK, it's optional, we can safely ignore it.
  1782. */
  1783. return nfs_ok;
  1784. }
  1785. nfs4_lock_state();
  1786. status = nfserr_complete_already;
  1787. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  1788. &cstate->session->se_client->cl_flags))
  1789. goto out;
  1790. status = nfserr_stale_clientid;
  1791. if (is_client_expired(cstate->session->se_client))
  1792. /*
  1793. * The following error isn't really legal.
  1794. * But we only get here if the client just explicitly
  1795. * destroyed the client. Surely it no longer cares what
  1796. * error it gets back on an operation for the dead
  1797. * client.
  1798. */
  1799. goto out;
  1800. status = nfs_ok;
  1801. nfsd4_client_record_create(cstate->session->se_client);
  1802. out:
  1803. nfs4_unlock_state();
  1804. return status;
  1805. }
  1806. __be32
  1807. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1808. struct nfsd4_setclientid *setclid)
  1809. {
  1810. struct xdr_netobj clname = setclid->se_name;
  1811. nfs4_verifier clverifier = setclid->se_verf;
  1812. unsigned int strhashval;
  1813. struct nfs4_client *conf, *unconf, *new;
  1814. __be32 status;
  1815. char dname[HEXDIR_LEN];
  1816. status = nfs4_make_rec_clidname(dname, &clname);
  1817. if (status)
  1818. return status;
  1819. /*
  1820. * XXX The Duplicate Request Cache (DRC) has been checked (??)
  1821. * We get here on a DRC miss.
  1822. */
  1823. strhashval = clientstr_hashval(dname);
  1824. nfs4_lock_state();
  1825. conf = find_confirmed_client_by_str(dname, strhashval);
  1826. if (conf) {
  1827. /* RFC 3530 14.2.33 CASE 0: */
  1828. status = nfserr_clid_inuse;
  1829. if (clp_used_exchangeid(conf))
  1830. goto out;
  1831. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1832. char addr_str[INET6_ADDRSTRLEN];
  1833. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  1834. sizeof(addr_str));
  1835. dprintk("NFSD: setclientid: string in use by client "
  1836. "at %s\n", addr_str);
  1837. goto out;
  1838. }
  1839. }
  1840. /*
  1841. * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
  1842. * has a description of SETCLIENTID request processing consisting
  1843. * of 5 bullet points, labeled as CASE0 - CASE4 below.
  1844. */
  1845. unconf = find_unconfirmed_client_by_str(dname, strhashval);
  1846. status = nfserr_jukebox;
  1847. if (!conf) {
  1848. /*
  1849. * RFC 3530 14.2.33 CASE 4:
  1850. * placed first, because it is the normal case
  1851. */
  1852. if (unconf)
  1853. expire_client(unconf);
  1854. new = create_client(clname, dname, rqstp, &clverifier);
  1855. if (new == NULL)
  1856. goto out;
  1857. gen_clid(new);
  1858. } else if (same_verf(&conf->cl_verifier, &clverifier)) {
  1859. /*
  1860. * RFC 3530 14.2.33 CASE 1:
  1861. * probable callback update
  1862. */
  1863. if (unconf) {
  1864. /* Note this is removing unconfirmed {*x***},
  1865. * which is stronger than RFC recommended {vxc**}.
  1866. * This has the advantage that there is at most
  1867. * one {*x***} in either list at any time.
  1868. */
  1869. expire_client(unconf);
  1870. }
  1871. new = create_client(clname, dname, rqstp, &clverifier);
  1872. if (new == NULL)
  1873. goto out;
  1874. copy_clid(new, conf);
  1875. } else if (!unconf) {
  1876. /*
  1877. * RFC 3530 14.2.33 CASE 2:
  1878. * probable client reboot; state will be removed if
  1879. * confirmed.
  1880. */
  1881. new = create_client(clname, dname, rqstp, &clverifier);
  1882. if (new == NULL)
  1883. goto out;
  1884. gen_clid(new);
  1885. } else {
  1886. /*
  1887. * RFC 3530 14.2.33 CASE 3:
  1888. * probable client reboot; state will be removed if
  1889. * confirmed.
  1890. */
  1891. expire_client(unconf);
  1892. new = create_client(clname, dname, rqstp, &clverifier);
  1893. if (new == NULL)
  1894. goto out;
  1895. gen_clid(new);
  1896. }
  1897. /*
  1898. * XXX: we should probably set this at creation time, and check
  1899. * for consistent minorversion use throughout:
  1900. */
  1901. new->cl_minorversion = 0;
  1902. gen_callback(new, setclid, rqstp);
  1903. add_to_unconfirmed(new, strhashval);
  1904. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  1905. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  1906. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  1907. status = nfs_ok;
  1908. out:
  1909. nfs4_unlock_state();
  1910. return status;
  1911. }
  1912. /*
  1913. * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
  1914. * a description of SETCLIENTID_CONFIRM request processing consisting of 4
  1915. * bullets, labeled as CASE1 - CASE4 below.
  1916. */
  1917. __be32
  1918. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  1919. struct nfsd4_compound_state *cstate,
  1920. struct nfsd4_setclientid_confirm *setclientid_confirm)
  1921. {
  1922. struct sockaddr *sa = svc_addr(rqstp);
  1923. struct nfs4_client *conf, *unconf;
  1924. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  1925. clientid_t * clid = &setclientid_confirm->sc_clientid;
  1926. __be32 status;
  1927. if (STALE_CLIENTID(clid))
  1928. return nfserr_stale_clientid;
  1929. /*
  1930. * XXX The Duplicate Request Cache (DRC) has been checked (??)
  1931. * We get here on a DRC miss.
  1932. */
  1933. nfs4_lock_state();
  1934. conf = find_confirmed_client(clid);
  1935. unconf = find_unconfirmed_client(clid);
  1936. status = nfserr_clid_inuse;
  1937. if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
  1938. goto out;
  1939. if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
  1940. goto out;
  1941. /*
  1942. * section 14.2.34 of RFC 3530 has a description of
  1943. * SETCLIENTID_CONFIRM request processing consisting
  1944. * of 4 bullet points, labeled as CASE1 - CASE4 below.
  1945. */
  1946. if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
  1947. /*
  1948. * RFC 3530 14.2.34 CASE 1:
  1949. * callback update
  1950. */
  1951. if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
  1952. status = nfserr_clid_inuse;
  1953. else {
  1954. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  1955. nfsd4_probe_callback(conf);
  1956. expire_client(unconf);
  1957. status = nfs_ok;
  1958. }
  1959. } else if (conf && !unconf) {
  1960. /*
  1961. * RFC 3530 14.2.34 CASE 2:
  1962. * probable retransmitted request; play it safe and
  1963. * do nothing.
  1964. */
  1965. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
  1966. status = nfserr_clid_inuse;
  1967. else
  1968. status = nfs_ok;
  1969. } else if (!conf && unconf
  1970. && same_verf(&unconf->cl_confirm, &confirm)) {
  1971. /*
  1972. * RFC 3530 14.2.34 CASE 3:
  1973. * Normal case; new or rebooted client:
  1974. */
  1975. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
  1976. status = nfserr_clid_inuse;
  1977. } else {
  1978. unsigned int hash =
  1979. clientstr_hashval(unconf->cl_recdir);
  1980. conf = find_confirmed_client_by_str(unconf->cl_recdir,
  1981. hash);
  1982. if (conf) {
  1983. nfsd4_client_record_remove(conf);
  1984. expire_client(conf);
  1985. }
  1986. move_to_confirmed(unconf);
  1987. conf = unconf;
  1988. nfsd4_probe_callback(conf);
  1989. status = nfs_ok;
  1990. }
  1991. } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
  1992. && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
  1993. &confirm)))) {
  1994. /*
  1995. * RFC 3530 14.2.34 CASE 4:
  1996. * Client probably hasn't noticed that we rebooted yet.
  1997. */
  1998. status = nfserr_stale_clientid;
  1999. } else {
  2000. /* check that we have hit one of the cases...*/
  2001. status = nfserr_clid_inuse;
  2002. }
  2003. out:
  2004. nfs4_unlock_state();
  2005. return status;
  2006. }
  2007. static struct nfs4_file *nfsd4_alloc_file(void)
  2008. {
  2009. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2010. }
  2011. /* OPEN Share state helper functions */
  2012. static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
  2013. {
  2014. unsigned int hashval = file_hashval(ino);
  2015. atomic_set(&fp->fi_ref, 1);
  2016. INIT_LIST_HEAD(&fp->fi_hash);
  2017. INIT_LIST_HEAD(&fp->fi_stateids);
  2018. INIT_LIST_HEAD(&fp->fi_delegations);
  2019. fp->fi_inode = igrab(ino);
  2020. fp->fi_had_conflict = false;
  2021. fp->fi_lease = NULL;
  2022. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2023. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2024. spin_lock(&recall_lock);
  2025. list_add(&fp->fi_hash, &file_hashtbl[hashval]);
  2026. spin_unlock(&recall_lock);
  2027. }
  2028. static void
  2029. nfsd4_free_slab(struct kmem_cache **slab)
  2030. {
  2031. if (*slab == NULL)
  2032. return;
  2033. kmem_cache_destroy(*slab);
  2034. *slab = NULL;
  2035. }
  2036. void
  2037. nfsd4_free_slabs(void)
  2038. {
  2039. nfsd4_free_slab(&openowner_slab);
  2040. nfsd4_free_slab(&lockowner_slab);
  2041. nfsd4_free_slab(&file_slab);
  2042. nfsd4_free_slab(&stateid_slab);
  2043. nfsd4_free_slab(&deleg_slab);
  2044. }
  2045. int
  2046. nfsd4_init_slabs(void)
  2047. {
  2048. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2049. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2050. if (openowner_slab == NULL)
  2051. goto out_nomem;
  2052. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2053. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2054. if (lockowner_slab == NULL)
  2055. goto out_nomem;
  2056. file_slab = kmem_cache_create("nfsd4_files",
  2057. sizeof(struct nfs4_file), 0, 0, NULL);
  2058. if (file_slab == NULL)
  2059. goto out_nomem;
  2060. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2061. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2062. if (stateid_slab == NULL)
  2063. goto out_nomem;
  2064. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2065. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2066. if (deleg_slab == NULL)
  2067. goto out_nomem;
  2068. return 0;
  2069. out_nomem:
  2070. nfsd4_free_slabs();
  2071. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2072. return -ENOMEM;
  2073. }
  2074. void nfs4_free_openowner(struct nfs4_openowner *oo)
  2075. {
  2076. kfree(oo->oo_owner.so_owner.data);
  2077. kmem_cache_free(openowner_slab, oo);
  2078. }
  2079. void nfs4_free_lockowner(struct nfs4_lockowner *lo)
  2080. {
  2081. kfree(lo->lo_owner.so_owner.data);
  2082. kmem_cache_free(lockowner_slab, lo);
  2083. }
  2084. static void init_nfs4_replay(struct nfs4_replay *rp)
  2085. {
  2086. rp->rp_status = nfserr_serverfault;
  2087. rp->rp_buflen = 0;
  2088. rp->rp_buf = rp->rp_ibuf;
  2089. }
  2090. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2091. {
  2092. struct nfs4_stateowner *sop;
  2093. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2094. if (!sop)
  2095. return NULL;
  2096. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2097. if (!sop->so_owner.data) {
  2098. kmem_cache_free(slab, sop);
  2099. return NULL;
  2100. }
  2101. sop->so_owner.len = owner->len;
  2102. INIT_LIST_HEAD(&sop->so_stateids);
  2103. sop->so_client = clp;
  2104. init_nfs4_replay(&sop->so_replay);
  2105. return sop;
  2106. }
  2107. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2108. {
  2109. list_add(&oo->oo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
  2110. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2111. }
  2112. static struct nfs4_openowner *
  2113. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  2114. struct nfs4_openowner *oo;
  2115. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2116. if (!oo)
  2117. return NULL;
  2118. oo->oo_owner.so_is_open_owner = 1;
  2119. oo->oo_owner.so_seqid = open->op_seqid;
  2120. oo->oo_flags = NFS4_OO_NEW;
  2121. oo->oo_time = 0;
  2122. oo->oo_last_closed_stid = NULL;
  2123. INIT_LIST_HEAD(&oo->oo_close_lru);
  2124. hash_openowner(oo, clp, strhashval);
  2125. return oo;
  2126. }
  2127. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2128. struct nfs4_openowner *oo = open->op_openowner;
  2129. struct nfs4_client *clp = oo->oo_owner.so_client;
  2130. init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
  2131. INIT_LIST_HEAD(&stp->st_lockowners);
  2132. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2133. list_add(&stp->st_perfile, &fp->fi_stateids);
  2134. stp->st_stateowner = &oo->oo_owner;
  2135. get_nfs4_file(fp);
  2136. stp->st_file = fp;
  2137. stp->st_access_bmap = 0;
  2138. stp->st_deny_bmap = 0;
  2139. __set_bit(open->op_share_access, &stp->st_access_bmap);
  2140. __set_bit(open->op_share_deny, &stp->st_deny_bmap);
  2141. stp->st_openstp = NULL;
  2142. }
  2143. static void
  2144. move_to_close_lru(struct nfs4_openowner *oo)
  2145. {
  2146. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2147. list_move_tail(&oo->oo_close_lru, &close_lru);
  2148. oo->oo_time = get_seconds();
  2149. }
  2150. static int
  2151. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  2152. clientid_t *clid)
  2153. {
  2154. return (sop->so_owner.len == owner->len) &&
  2155. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  2156. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  2157. }
  2158. static struct nfs4_openowner *
  2159. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
  2160. {
  2161. struct nfs4_stateowner *so;
  2162. struct nfs4_openowner *oo;
  2163. list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
  2164. if (!so->so_is_open_owner)
  2165. continue;
  2166. if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
  2167. oo = openowner(so);
  2168. renew_client(oo->oo_owner.so_client);
  2169. return oo;
  2170. }
  2171. }
  2172. return NULL;
  2173. }
  2174. /* search file_hashtbl[] for file */
  2175. static struct nfs4_file *
  2176. find_file(struct inode *ino)
  2177. {
  2178. unsigned int hashval = file_hashval(ino);
  2179. struct nfs4_file *fp;
  2180. spin_lock(&recall_lock);
  2181. list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2182. if (fp->fi_inode == ino) {
  2183. get_nfs4_file(fp);
  2184. spin_unlock(&recall_lock);
  2185. return fp;
  2186. }
  2187. }
  2188. spin_unlock(&recall_lock);
  2189. return NULL;
  2190. }
  2191. /*
  2192. * Called to check deny when READ with all zero stateid or
  2193. * WRITE with all zero or all one stateid
  2194. */
  2195. static __be32
  2196. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2197. {
  2198. struct inode *ino = current_fh->fh_dentry->d_inode;
  2199. struct nfs4_file *fp;
  2200. struct nfs4_ol_stateid *stp;
  2201. __be32 ret;
  2202. dprintk("NFSD: nfs4_share_conflict\n");
  2203. fp = find_file(ino);
  2204. if (!fp)
  2205. return nfs_ok;
  2206. ret = nfserr_locked;
  2207. /* Search for conflicting share reservations */
  2208. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  2209. if (test_bit(deny_type, &stp->st_deny_bmap) ||
  2210. test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
  2211. goto out;
  2212. }
  2213. ret = nfs_ok;
  2214. out:
  2215. put_nfs4_file(fp);
  2216. return ret;
  2217. }
  2218. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2219. {
  2220. /* We're assuming the state code never drops its reference
  2221. * without first removing the lease. Since we're in this lease
  2222. * callback (and since the lease code is serialized by the kernel
  2223. * lock) we know the server hasn't removed the lease yet, we know
  2224. * it's safe to take a reference: */
  2225. atomic_inc(&dp->dl_count);
  2226. list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
  2227. /* only place dl_time is set. protected by lock_flocks*/
  2228. dp->dl_time = get_seconds();
  2229. nfsd4_cb_recall(dp);
  2230. }
  2231. /* Called from break_lease() with lock_flocks() held. */
  2232. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2233. {
  2234. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2235. struct nfs4_delegation *dp;
  2236. BUG_ON(!fp);
  2237. /* We assume break_lease is only called once per lease: */
  2238. BUG_ON(fp->fi_had_conflict);
  2239. /*
  2240. * We don't want the locks code to timeout the lease for us;
  2241. * we'll remove it ourself if a delegation isn't returned
  2242. * in time:
  2243. */
  2244. fl->fl_break_time = 0;
  2245. spin_lock(&recall_lock);
  2246. fp->fi_had_conflict = true;
  2247. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2248. nfsd_break_one_deleg(dp);
  2249. spin_unlock(&recall_lock);
  2250. }
  2251. static
  2252. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2253. {
  2254. if (arg & F_UNLCK)
  2255. return lease_modify(onlist, arg);
  2256. else
  2257. return -EAGAIN;
  2258. }
  2259. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2260. .lm_break = nfsd_break_deleg_cb,
  2261. .lm_change = nfsd_change_deleg_cb,
  2262. };
  2263. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2264. {
  2265. if (nfsd4_has_session(cstate))
  2266. return nfs_ok;
  2267. if (seqid == so->so_seqid - 1)
  2268. return nfserr_replay_me;
  2269. if (seqid == so->so_seqid)
  2270. return nfs_ok;
  2271. return nfserr_bad_seqid;
  2272. }
  2273. __be32
  2274. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  2275. struct nfsd4_open *open)
  2276. {
  2277. clientid_t *clientid = &open->op_clientid;
  2278. struct nfs4_client *clp = NULL;
  2279. unsigned int strhashval;
  2280. struct nfs4_openowner *oo = NULL;
  2281. __be32 status;
  2282. if (STALE_CLIENTID(&open->op_clientid))
  2283. return nfserr_stale_clientid;
  2284. /*
  2285. * In case we need it later, after we've already created the
  2286. * file and don't want to risk a further failure:
  2287. */
  2288. open->op_file = nfsd4_alloc_file();
  2289. if (open->op_file == NULL)
  2290. return nfserr_jukebox;
  2291. strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
  2292. oo = find_openstateowner_str(strhashval, open);
  2293. open->op_openowner = oo;
  2294. if (!oo) {
  2295. clp = find_confirmed_client(clientid);
  2296. if (clp == NULL)
  2297. return nfserr_expired;
  2298. goto new_owner;
  2299. }
  2300. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  2301. /* Replace unconfirmed owners without checking for replay. */
  2302. clp = oo->oo_owner.so_client;
  2303. release_openowner(oo);
  2304. open->op_openowner = NULL;
  2305. goto new_owner;
  2306. }
  2307. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  2308. if (status)
  2309. return status;
  2310. clp = oo->oo_owner.so_client;
  2311. goto alloc_stateid;
  2312. new_owner:
  2313. oo = alloc_init_open_stateowner(strhashval, clp, open);
  2314. if (oo == NULL)
  2315. return nfserr_jukebox;
  2316. open->op_openowner = oo;
  2317. alloc_stateid:
  2318. open->op_stp = nfs4_alloc_stateid(clp);
  2319. if (!open->op_stp)
  2320. return nfserr_jukebox;
  2321. return nfs_ok;
  2322. }
  2323. static inline __be32
  2324. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  2325. {
  2326. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  2327. return nfserr_openmode;
  2328. else
  2329. return nfs_ok;
  2330. }
  2331. static int share_access_to_flags(u32 share_access)
  2332. {
  2333. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  2334. }
  2335. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  2336. {
  2337. struct nfs4_stid *ret;
  2338. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  2339. if (!ret)
  2340. return NULL;
  2341. return delegstateid(ret);
  2342. }
  2343. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  2344. {
  2345. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  2346. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  2347. }
  2348. static __be32
  2349. nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
  2350. struct nfs4_delegation **dp)
  2351. {
  2352. int flags;
  2353. __be32 status = nfserr_bad_stateid;
  2354. *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
  2355. if (*dp == NULL)
  2356. goto out;
  2357. flags = share_access_to_flags(open->op_share_access);
  2358. status = nfs4_check_delegmode(*dp, flags);
  2359. if (status)
  2360. *dp = NULL;
  2361. out:
  2362. if (!nfsd4_is_deleg_cur(open))
  2363. return nfs_ok;
  2364. if (status)
  2365. return status;
  2366. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2367. return nfs_ok;
  2368. }
  2369. static __be32
  2370. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
  2371. {
  2372. struct nfs4_ol_stateid *local;
  2373. struct nfs4_openowner *oo = open->op_openowner;
  2374. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2375. /* ignore lock owners */
  2376. if (local->st_stateowner->so_is_open_owner == 0)
  2377. continue;
  2378. /* remember if we have seen this open owner */
  2379. if (local->st_stateowner == &oo->oo_owner)
  2380. *stpp = local;
  2381. /* check for conflicting share reservations */
  2382. if (!test_share(local, open))
  2383. return nfserr_share_denied;
  2384. }
  2385. return nfs_ok;
  2386. }
  2387. static void nfs4_free_stateid(struct nfs4_ol_stateid *s)
  2388. {
  2389. kmem_cache_free(stateid_slab, s);
  2390. }
  2391. static inline int nfs4_access_to_access(u32 nfs4_access)
  2392. {
  2393. int flags = 0;
  2394. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  2395. flags |= NFSD_MAY_READ;
  2396. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  2397. flags |= NFSD_MAY_WRITE;
  2398. return flags;
  2399. }
  2400. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  2401. struct svc_fh *cur_fh, struct nfsd4_open *open)
  2402. {
  2403. __be32 status;
  2404. int oflag = nfs4_access_to_omode(open->op_share_access);
  2405. int access = nfs4_access_to_access(open->op_share_access);
  2406. if (!fp->fi_fds[oflag]) {
  2407. status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
  2408. &fp->fi_fds[oflag]);
  2409. if (status)
  2410. return status;
  2411. }
  2412. nfs4_file_get_access(fp, oflag);
  2413. return nfs_ok;
  2414. }
  2415. static inline __be32
  2416. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2417. struct nfsd4_open *open)
  2418. {
  2419. struct iattr iattr = {
  2420. .ia_valid = ATTR_SIZE,
  2421. .ia_size = 0,
  2422. };
  2423. if (!open->op_truncate)
  2424. return 0;
  2425. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2426. return nfserr_inval;
  2427. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2428. }
  2429. static __be32
  2430. nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
  2431. {
  2432. u32 op_share_access = open->op_share_access;
  2433. bool new_access;
  2434. __be32 status;
  2435. new_access = !test_bit(op_share_access, &stp->st_access_bmap);
  2436. if (new_access) {
  2437. status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
  2438. if (status)
  2439. return status;
  2440. }
  2441. status = nfsd4_truncate(rqstp, cur_fh, open);
  2442. if (status) {
  2443. if (new_access) {
  2444. int oflag = nfs4_access_to_omode(op_share_access);
  2445. nfs4_file_put_access(fp, oflag);
  2446. }
  2447. return status;
  2448. }
  2449. /* remember the open */
  2450. __set_bit(op_share_access, &stp->st_access_bmap);
  2451. __set_bit(open->op_share_deny, &stp->st_deny_bmap);
  2452. return nfs_ok;
  2453. }
  2454. static void
  2455. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  2456. {
  2457. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2458. }
  2459. /* Should we give out recallable state?: */
  2460. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  2461. {
  2462. if (clp->cl_cb_state == NFSD4_CB_UP)
  2463. return true;
  2464. /*
  2465. * In the sessions case, since we don't have to establish a
  2466. * separate connection for callbacks, we assume it's OK
  2467. * until we hear otherwise:
  2468. */
  2469. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  2470. }
  2471. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
  2472. {
  2473. struct file_lock *fl;
  2474. fl = locks_alloc_lock();
  2475. if (!fl)
  2476. return NULL;
  2477. locks_init_lock(fl);
  2478. fl->fl_lmops = &nfsd_lease_mng_ops;
  2479. fl->fl_flags = FL_LEASE;
  2480. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2481. fl->fl_end = OFFSET_MAX;
  2482. fl->fl_owner = (fl_owner_t)(dp->dl_file);
  2483. fl->fl_pid = current->tgid;
  2484. return fl;
  2485. }
  2486. static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
  2487. {
  2488. struct nfs4_file *fp = dp->dl_file;
  2489. struct file_lock *fl;
  2490. int status;
  2491. fl = nfs4_alloc_init_lease(dp, flag);
  2492. if (!fl)
  2493. return -ENOMEM;
  2494. fl->fl_file = find_readable_file(fp);
  2495. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2496. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
  2497. if (status) {
  2498. list_del_init(&dp->dl_perclnt);
  2499. locks_free_lock(fl);
  2500. return -ENOMEM;
  2501. }
  2502. fp->fi_lease = fl;
  2503. fp->fi_deleg_file = fl->fl_file;
  2504. get_file(fp->fi_deleg_file);
  2505. atomic_set(&fp->fi_delegees, 1);
  2506. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2507. return 0;
  2508. }
  2509. static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  2510. {
  2511. struct nfs4_file *fp = dp->dl_file;
  2512. if (!fp->fi_lease)
  2513. return nfs4_setlease(dp, flag);
  2514. spin_lock(&recall_lock);
  2515. if (fp->fi_had_conflict) {
  2516. spin_unlock(&recall_lock);
  2517. return -EAGAIN;
  2518. }
  2519. atomic_inc(&fp->fi_delegees);
  2520. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2521. spin_unlock(&recall_lock);
  2522. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2523. return 0;
  2524. }
  2525. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  2526. {
  2527. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2528. if (status == -EAGAIN)
  2529. open->op_why_no_deleg = WND4_CONTENTION;
  2530. else {
  2531. open->op_why_no_deleg = WND4_RESOURCE;
  2532. switch (open->op_deleg_want) {
  2533. case NFS4_SHARE_WANT_READ_DELEG:
  2534. case NFS4_SHARE_WANT_WRITE_DELEG:
  2535. case NFS4_SHARE_WANT_ANY_DELEG:
  2536. break;
  2537. case NFS4_SHARE_WANT_CANCEL:
  2538. open->op_why_no_deleg = WND4_CANCELLED;
  2539. break;
  2540. case NFS4_SHARE_WANT_NO_DELEG:
  2541. BUG(); /* not supposed to get here */
  2542. }
  2543. }
  2544. }
  2545. /*
  2546. * Attempt to hand out a delegation.
  2547. */
  2548. static void
  2549. nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
  2550. {
  2551. struct nfs4_delegation *dp;
  2552. struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
  2553. int cb_up;
  2554. int status = 0, flag = 0;
  2555. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  2556. flag = NFS4_OPEN_DELEGATE_NONE;
  2557. open->op_recall = 0;
  2558. switch (open->op_claim_type) {
  2559. case NFS4_OPEN_CLAIM_PREVIOUS:
  2560. if (!cb_up)
  2561. open->op_recall = 1;
  2562. flag = open->op_delegate_type;
  2563. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2564. goto out;
  2565. break;
  2566. case NFS4_OPEN_CLAIM_NULL:
  2567. /* Let's not give out any delegations till everyone's
  2568. * had the chance to reclaim theirs.... */
  2569. if (locks_in_grace())
  2570. goto out;
  2571. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  2572. goto out;
  2573. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2574. flag = NFS4_OPEN_DELEGATE_WRITE;
  2575. else
  2576. flag = NFS4_OPEN_DELEGATE_READ;
  2577. break;
  2578. default:
  2579. goto out;
  2580. }
  2581. dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
  2582. if (dp == NULL)
  2583. goto out_no_deleg;
  2584. status = nfs4_set_delegation(dp, flag);
  2585. if (status)
  2586. goto out_free;
  2587. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  2588. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  2589. STATEID_VAL(&dp->dl_stid.sc_stateid));
  2590. out:
  2591. open->op_delegate_type = flag;
  2592. if (flag == NFS4_OPEN_DELEGATE_NONE) {
  2593. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  2594. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2595. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2596. /* 4.1 client asking for a delegation? */
  2597. if (open->op_deleg_want)
  2598. nfsd4_open_deleg_none_ext(open, status);
  2599. }
  2600. return;
  2601. out_free:
  2602. nfs4_put_delegation(dp);
  2603. out_no_deleg:
  2604. flag = NFS4_OPEN_DELEGATE_NONE;
  2605. goto out;
  2606. }
  2607. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  2608. struct nfs4_delegation *dp)
  2609. {
  2610. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  2611. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2612. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2613. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  2614. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  2615. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2616. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2617. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  2618. }
  2619. /* Otherwise the client must be confused wanting a delegation
  2620. * it already has, therefore we don't return
  2621. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  2622. */
  2623. }
  2624. /*
  2625. * called with nfs4_lock_state() held.
  2626. */
  2627. __be32
  2628. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2629. {
  2630. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2631. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  2632. struct nfs4_file *fp = NULL;
  2633. struct inode *ino = current_fh->fh_dentry->d_inode;
  2634. struct nfs4_ol_stateid *stp = NULL;
  2635. struct nfs4_delegation *dp = NULL;
  2636. __be32 status;
  2637. /*
  2638. * Lookup file; if found, lookup stateid and check open request,
  2639. * and check for delegations in the process of being recalled.
  2640. * If not found, create the nfs4_file struct
  2641. */
  2642. fp = find_file(ino);
  2643. if (fp) {
  2644. if ((status = nfs4_check_open(fp, open, &stp)))
  2645. goto out;
  2646. status = nfs4_check_deleg(cl, fp, open, &dp);
  2647. if (status)
  2648. goto out;
  2649. } else {
  2650. status = nfserr_bad_stateid;
  2651. if (nfsd4_is_deleg_cur(open))
  2652. goto out;
  2653. status = nfserr_jukebox;
  2654. fp = open->op_file;
  2655. open->op_file = NULL;
  2656. nfsd4_init_file(fp, ino);
  2657. }
  2658. /*
  2659. * OPEN the file, or upgrade an existing OPEN.
  2660. * If truncate fails, the OPEN fails.
  2661. */
  2662. if (stp) {
  2663. /* Stateid was found, this is an OPEN upgrade */
  2664. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  2665. if (status)
  2666. goto out;
  2667. } else {
  2668. status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
  2669. if (status)
  2670. goto out;
  2671. stp = open->op_stp;
  2672. open->op_stp = NULL;
  2673. init_open_stateid(stp, fp, open);
  2674. status = nfsd4_truncate(rqstp, current_fh, open);
  2675. if (status) {
  2676. release_open_stateid(stp);
  2677. goto out;
  2678. }
  2679. }
  2680. update_stateid(&stp->st_stid.sc_stateid);
  2681. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  2682. if (nfsd4_has_session(&resp->cstate)) {
  2683. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2684. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  2685. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2686. open->op_why_no_deleg = WND4_NOT_WANTED;
  2687. goto nodeleg;
  2688. }
  2689. }
  2690. /*
  2691. * Attempt to hand out a delegation. No error return, because the
  2692. * OPEN succeeds even if we fail.
  2693. */
  2694. nfs4_open_delegation(current_fh, open, stp);
  2695. nodeleg:
  2696. status = nfs_ok;
  2697. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  2698. STATEID_VAL(&stp->st_stid.sc_stateid));
  2699. out:
  2700. /* 4.1 client trying to upgrade/downgrade delegation? */
  2701. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  2702. open->op_deleg_want)
  2703. nfsd4_deleg_xgrade_none_ext(open, dp);
  2704. if (fp)
  2705. put_nfs4_file(fp);
  2706. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2707. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  2708. /*
  2709. * To finish the open response, we just need to set the rflags.
  2710. */
  2711. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2712. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  2713. !nfsd4_has_session(&resp->cstate))
  2714. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2715. return status;
  2716. }
  2717. void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
  2718. {
  2719. if (open->op_openowner) {
  2720. struct nfs4_openowner *oo = open->op_openowner;
  2721. if (!list_empty(&oo->oo_owner.so_stateids))
  2722. list_del_init(&oo->oo_close_lru);
  2723. if (oo->oo_flags & NFS4_OO_NEW) {
  2724. if (status) {
  2725. release_openowner(oo);
  2726. open->op_openowner = NULL;
  2727. } else
  2728. oo->oo_flags &= ~NFS4_OO_NEW;
  2729. }
  2730. }
  2731. if (open->op_file)
  2732. nfsd4_free_file(open->op_file);
  2733. if (open->op_stp)
  2734. nfs4_free_stateid(open->op_stp);
  2735. }
  2736. __be32
  2737. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2738. clientid_t *clid)
  2739. {
  2740. struct nfs4_client *clp;
  2741. __be32 status;
  2742. nfs4_lock_state();
  2743. dprintk("process_renew(%08x/%08x): starting\n",
  2744. clid->cl_boot, clid->cl_id);
  2745. status = nfserr_stale_clientid;
  2746. if (STALE_CLIENTID(clid))
  2747. goto out;
  2748. clp = find_confirmed_client(clid);
  2749. status = nfserr_expired;
  2750. if (clp == NULL) {
  2751. /* We assume the client took too long to RENEW. */
  2752. dprintk("nfsd4_renew: clientid not found!\n");
  2753. goto out;
  2754. }
  2755. status = nfserr_cb_path_down;
  2756. if (!list_empty(&clp->cl_delegations)
  2757. && clp->cl_cb_state != NFSD4_CB_UP)
  2758. goto out;
  2759. status = nfs_ok;
  2760. out:
  2761. nfs4_unlock_state();
  2762. return status;
  2763. }
  2764. static struct lock_manager nfsd4_manager = {
  2765. };
  2766. static void
  2767. nfsd4_end_grace(void)
  2768. {
  2769. dprintk("NFSD: end of grace period\n");
  2770. nfsd4_record_grace_done(&init_net, boot_time);
  2771. locks_end_grace(&nfsd4_manager);
  2772. /*
  2773. * Now that every NFSv4 client has had the chance to recover and
  2774. * to see the (possibly new, possibly shorter) lease time, we
  2775. * can safely set the next grace time to the current lease time:
  2776. */
  2777. nfsd4_grace = nfsd4_lease;
  2778. }
  2779. static time_t
  2780. nfs4_laundromat(void)
  2781. {
  2782. struct nfs4_client *clp;
  2783. struct nfs4_openowner *oo;
  2784. struct nfs4_delegation *dp;
  2785. struct list_head *pos, *next, reaplist;
  2786. time_t cutoff = get_seconds() - nfsd4_lease;
  2787. time_t t, clientid_val = nfsd4_lease;
  2788. time_t u, test_val = nfsd4_lease;
  2789. nfs4_lock_state();
  2790. dprintk("NFSD: laundromat service - starting\n");
  2791. if (locks_in_grace())
  2792. nfsd4_end_grace();
  2793. INIT_LIST_HEAD(&reaplist);
  2794. spin_lock(&client_lock);
  2795. list_for_each_safe(pos, next, &client_lru) {
  2796. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2797. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2798. t = clp->cl_time - cutoff;
  2799. if (clientid_val > t)
  2800. clientid_val = t;
  2801. break;
  2802. }
  2803. if (atomic_read(&clp->cl_refcount)) {
  2804. dprintk("NFSD: client in use (clientid %08x)\n",
  2805. clp->cl_clientid.cl_id);
  2806. continue;
  2807. }
  2808. unhash_client_locked(clp);
  2809. list_add(&clp->cl_lru, &reaplist);
  2810. }
  2811. spin_unlock(&client_lock);
  2812. list_for_each_safe(pos, next, &reaplist) {
  2813. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2814. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2815. clp->cl_clientid.cl_id);
  2816. nfsd4_client_record_remove(clp);
  2817. expire_client(clp);
  2818. }
  2819. spin_lock(&recall_lock);
  2820. list_for_each_safe(pos, next, &del_recall_lru) {
  2821. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2822. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2823. u = dp->dl_time - cutoff;
  2824. if (test_val > u)
  2825. test_val = u;
  2826. break;
  2827. }
  2828. list_move(&dp->dl_recall_lru, &reaplist);
  2829. }
  2830. spin_unlock(&recall_lock);
  2831. list_for_each_safe(pos, next, &reaplist) {
  2832. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2833. unhash_delegation(dp);
  2834. }
  2835. test_val = nfsd4_lease;
  2836. list_for_each_safe(pos, next, &close_lru) {
  2837. oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
  2838. if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
  2839. u = oo->oo_time - cutoff;
  2840. if (test_val > u)
  2841. test_val = u;
  2842. break;
  2843. }
  2844. release_openowner(oo);
  2845. }
  2846. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2847. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2848. nfs4_unlock_state();
  2849. return clientid_val;
  2850. }
  2851. static struct workqueue_struct *laundry_wq;
  2852. static void laundromat_main(struct work_struct *);
  2853. static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
  2854. static void
  2855. laundromat_main(struct work_struct *not_used)
  2856. {
  2857. time_t t;
  2858. t = nfs4_laundromat();
  2859. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2860. queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
  2861. }
  2862. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  2863. {
  2864. if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
  2865. return nfserr_bad_stateid;
  2866. return nfs_ok;
  2867. }
  2868. static int
  2869. STALE_STATEID(stateid_t *stateid)
  2870. {
  2871. if (stateid->si_opaque.so_clid.cl_boot == boot_time)
  2872. return 0;
  2873. dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
  2874. STATEID_VAL(stateid));
  2875. return 1;
  2876. }
  2877. static inline int
  2878. access_permit_read(unsigned long access_bmap)
  2879. {
  2880. return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
  2881. test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
  2882. test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
  2883. }
  2884. static inline int
  2885. access_permit_write(unsigned long access_bmap)
  2886. {
  2887. return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
  2888. test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
  2889. }
  2890. static
  2891. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  2892. {
  2893. __be32 status = nfserr_openmode;
  2894. /* For lock stateid's, we test the parent open, not the lock: */
  2895. if (stp->st_openstp)
  2896. stp = stp->st_openstp;
  2897. if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
  2898. goto out;
  2899. if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
  2900. goto out;
  2901. status = nfs_ok;
  2902. out:
  2903. return status;
  2904. }
  2905. static inline __be32
  2906. check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
  2907. {
  2908. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2909. return nfs_ok;
  2910. else if (locks_in_grace()) {
  2911. /* Answer in remaining cases depends on existence of
  2912. * conflicting state; so we must wait out the grace period. */
  2913. return nfserr_grace;
  2914. } else if (flags & WR_STATE)
  2915. return nfs4_share_conflict(current_fh,
  2916. NFS4_SHARE_DENY_WRITE);
  2917. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2918. return nfs4_share_conflict(current_fh,
  2919. NFS4_SHARE_DENY_READ);
  2920. }
  2921. /*
  2922. * Allow READ/WRITE during grace period on recovered state only for files
  2923. * that are not able to provide mandatory locking.
  2924. */
  2925. static inline int
  2926. grace_disallows_io(struct inode *inode)
  2927. {
  2928. return locks_in_grace() && mandatory_lock(inode);
  2929. }
  2930. /* Returns true iff a is later than b: */
  2931. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  2932. {
  2933. return (s32)a->si_generation - (s32)b->si_generation > 0;
  2934. }
  2935. static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  2936. {
  2937. /*
  2938. * When sessions are used the stateid generation number is ignored
  2939. * when it is zero.
  2940. */
  2941. if (has_session && in->si_generation == 0)
  2942. return nfs_ok;
  2943. if (in->si_generation == ref->si_generation)
  2944. return nfs_ok;
  2945. /* If the client sends us a stateid from the future, it's buggy: */
  2946. if (stateid_generation_after(in, ref))
  2947. return nfserr_bad_stateid;
  2948. /*
  2949. * However, we could see a stateid from the past, even from a
  2950. * non-buggy client. For example, if the client sends a lock
  2951. * while some IO is outstanding, the lock may bump si_generation
  2952. * while the IO is still in flight. The client could avoid that
  2953. * situation by waiting for responses on all the IO requests,
  2954. * but better performance may result in retrying IO that
  2955. * receives an old_stateid error if requests are rarely
  2956. * reordered in flight:
  2957. */
  2958. return nfserr_old_stateid;
  2959. }
  2960. __be32 nfs4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  2961. {
  2962. struct nfs4_stid *s;
  2963. struct nfs4_ol_stateid *ols;
  2964. __be32 status;
  2965. if (STALE_STATEID(stateid))
  2966. return nfserr_stale_stateid;
  2967. s = find_stateid(cl, stateid);
  2968. if (!s)
  2969. return nfserr_stale_stateid;
  2970. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  2971. if (status)
  2972. return status;
  2973. if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
  2974. return nfs_ok;
  2975. ols = openlockstateid(s);
  2976. if (ols->st_stateowner->so_is_open_owner
  2977. && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  2978. return nfserr_bad_stateid;
  2979. return nfs_ok;
  2980. }
  2981. static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s)
  2982. {
  2983. struct nfs4_client *cl;
  2984. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2985. return nfserr_bad_stateid;
  2986. if (STALE_STATEID(stateid))
  2987. return nfserr_stale_stateid;
  2988. cl = find_confirmed_client(&stateid->si_opaque.so_clid);
  2989. if (!cl)
  2990. return nfserr_expired;
  2991. *s = find_stateid_by_type(cl, stateid, typemask);
  2992. if (!*s)
  2993. return nfserr_bad_stateid;
  2994. return nfs_ok;
  2995. }
  2996. /*
  2997. * Checks for stateid operations
  2998. */
  2999. __be32
  3000. nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
  3001. stateid_t *stateid, int flags, struct file **filpp)
  3002. {
  3003. struct nfs4_stid *s;
  3004. struct nfs4_ol_stateid *stp = NULL;
  3005. struct nfs4_delegation *dp = NULL;
  3006. struct svc_fh *current_fh = &cstate->current_fh;
  3007. struct inode *ino = current_fh->fh_dentry->d_inode;
  3008. __be32 status;
  3009. if (filpp)
  3010. *filpp = NULL;
  3011. if (grace_disallows_io(ino))
  3012. return nfserr_grace;
  3013. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3014. return check_special_stateids(current_fh, stateid, flags);
  3015. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s);
  3016. if (status)
  3017. return status;
  3018. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3019. if (status)
  3020. goto out;
  3021. switch (s->sc_type) {
  3022. case NFS4_DELEG_STID:
  3023. dp = delegstateid(s);
  3024. status = nfs4_check_delegmode(dp, flags);
  3025. if (status)
  3026. goto out;
  3027. if (filpp) {
  3028. *filpp = dp->dl_file->fi_deleg_file;
  3029. BUG_ON(!*filpp);
  3030. }
  3031. break;
  3032. case NFS4_OPEN_STID:
  3033. case NFS4_LOCK_STID:
  3034. stp = openlockstateid(s);
  3035. status = nfs4_check_fh(current_fh, stp);
  3036. if (status)
  3037. goto out;
  3038. if (stp->st_stateowner->so_is_open_owner
  3039. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3040. goto out;
  3041. status = nfs4_check_openmode(stp, flags);
  3042. if (status)
  3043. goto out;
  3044. if (filpp) {
  3045. if (flags & RD_STATE)
  3046. *filpp = find_readable_file(stp->st_file);
  3047. else
  3048. *filpp = find_writeable_file(stp->st_file);
  3049. }
  3050. break;
  3051. default:
  3052. return nfserr_bad_stateid;
  3053. }
  3054. status = nfs_ok;
  3055. out:
  3056. return status;
  3057. }
  3058. static __be32
  3059. nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
  3060. {
  3061. if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
  3062. return nfserr_locks_held;
  3063. release_lock_stateid(stp);
  3064. return nfs_ok;
  3065. }
  3066. /*
  3067. * Test if the stateid is valid
  3068. */
  3069. __be32
  3070. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3071. struct nfsd4_test_stateid *test_stateid)
  3072. {
  3073. struct nfsd4_test_stateid_id *stateid;
  3074. struct nfs4_client *cl = cstate->session->se_client;
  3075. nfs4_lock_state();
  3076. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3077. stateid->ts_id_status = nfs4_validate_stateid(cl, &stateid->ts_id_stateid);
  3078. nfs4_unlock_state();
  3079. return nfs_ok;
  3080. }
  3081. __be32
  3082. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3083. struct nfsd4_free_stateid *free_stateid)
  3084. {
  3085. stateid_t *stateid = &free_stateid->fr_stateid;
  3086. struct nfs4_stid *s;
  3087. struct nfs4_client *cl = cstate->session->se_client;
  3088. __be32 ret = nfserr_bad_stateid;
  3089. nfs4_lock_state();
  3090. s = find_stateid(cl, stateid);
  3091. if (!s)
  3092. goto out;
  3093. switch (s->sc_type) {
  3094. case NFS4_DELEG_STID:
  3095. ret = nfserr_locks_held;
  3096. goto out;
  3097. case NFS4_OPEN_STID:
  3098. case NFS4_LOCK_STID:
  3099. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3100. if (ret)
  3101. goto out;
  3102. if (s->sc_type == NFS4_LOCK_STID)
  3103. ret = nfsd4_free_lock_stateid(openlockstateid(s));
  3104. else
  3105. ret = nfserr_locks_held;
  3106. break;
  3107. default:
  3108. ret = nfserr_bad_stateid;
  3109. }
  3110. out:
  3111. nfs4_unlock_state();
  3112. return ret;
  3113. }
  3114. static inline int
  3115. setlkflg (int type)
  3116. {
  3117. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3118. RD_STATE : WR_STATE;
  3119. }
  3120. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  3121. {
  3122. struct svc_fh *current_fh = &cstate->current_fh;
  3123. struct nfs4_stateowner *sop = stp->st_stateowner;
  3124. __be32 status;
  3125. status = nfsd4_check_seqid(cstate, sop, seqid);
  3126. if (status)
  3127. return status;
  3128. if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
  3129. /*
  3130. * "Closed" stateid's exist *only* to return
  3131. * nfserr_replay_me from the previous step.
  3132. */
  3133. return nfserr_bad_stateid;
  3134. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  3135. if (status)
  3136. return status;
  3137. return nfs4_check_fh(current_fh, stp);
  3138. }
  3139. /*
  3140. * Checks for sequence id mutating operations.
  3141. */
  3142. static __be32
  3143. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3144. stateid_t *stateid, char typemask,
  3145. struct nfs4_ol_stateid **stpp)
  3146. {
  3147. __be32 status;
  3148. struct nfs4_stid *s;
  3149. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  3150. seqid, STATEID_VAL(stateid));
  3151. *stpp = NULL;
  3152. status = nfsd4_lookup_stateid(stateid, typemask, &s);
  3153. if (status)
  3154. return status;
  3155. *stpp = openlockstateid(s);
  3156. cstate->replay_owner = (*stpp)->st_stateowner;
  3157. return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
  3158. }
  3159. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
  3160. {
  3161. __be32 status;
  3162. struct nfs4_openowner *oo;
  3163. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  3164. NFS4_OPEN_STID, stpp);
  3165. if (status)
  3166. return status;
  3167. oo = openowner((*stpp)->st_stateowner);
  3168. if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
  3169. return nfserr_bad_stateid;
  3170. return nfs_ok;
  3171. }
  3172. __be32
  3173. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3174. struct nfsd4_open_confirm *oc)
  3175. {
  3176. __be32 status;
  3177. struct nfs4_openowner *oo;
  3178. struct nfs4_ol_stateid *stp;
  3179. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  3180. (int)cstate->current_fh.fh_dentry->d_name.len,
  3181. cstate->current_fh.fh_dentry->d_name.name);
  3182. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  3183. if (status)
  3184. return status;
  3185. nfs4_lock_state();
  3186. status = nfs4_preprocess_seqid_op(cstate,
  3187. oc->oc_seqid, &oc->oc_req_stateid,
  3188. NFS4_OPEN_STID, &stp);
  3189. if (status)
  3190. goto out;
  3191. oo = openowner(stp->st_stateowner);
  3192. status = nfserr_bad_stateid;
  3193. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  3194. goto out;
  3195. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3196. update_stateid(&stp->st_stid.sc_stateid);
  3197. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3198. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  3199. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  3200. nfsd4_client_record_create(oo->oo_owner.so_client);
  3201. status = nfs_ok;
  3202. out:
  3203. if (!cstate->replay_owner)
  3204. nfs4_unlock_state();
  3205. return status;
  3206. }
  3207. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  3208. {
  3209. if (!test_bit(access, &stp->st_access_bmap))
  3210. return;
  3211. nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
  3212. __clear_bit(access, &stp->st_access_bmap);
  3213. }
  3214. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  3215. {
  3216. switch (to_access) {
  3217. case NFS4_SHARE_ACCESS_READ:
  3218. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  3219. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3220. break;
  3221. case NFS4_SHARE_ACCESS_WRITE:
  3222. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  3223. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3224. break;
  3225. case NFS4_SHARE_ACCESS_BOTH:
  3226. break;
  3227. default:
  3228. BUG();
  3229. }
  3230. }
  3231. static void
  3232. reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
  3233. {
  3234. int i;
  3235. for (i = 0; i < 4; i++) {
  3236. if ((i & deny) != i)
  3237. __clear_bit(i, bmap);
  3238. }
  3239. }
  3240. __be32
  3241. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  3242. struct nfsd4_compound_state *cstate,
  3243. struct nfsd4_open_downgrade *od)
  3244. {
  3245. __be32 status;
  3246. struct nfs4_ol_stateid *stp;
  3247. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  3248. (int)cstate->current_fh.fh_dentry->d_name.len,
  3249. cstate->current_fh.fh_dentry->d_name.name);
  3250. /* We don't yet support WANT bits: */
  3251. if (od->od_deleg_want)
  3252. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  3253. od->od_deleg_want);
  3254. nfs4_lock_state();
  3255. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  3256. &od->od_stateid, &stp);
  3257. if (status)
  3258. goto out;
  3259. status = nfserr_inval;
  3260. if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
  3261. dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
  3262. stp->st_access_bmap, od->od_share_access);
  3263. goto out;
  3264. }
  3265. if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
  3266. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  3267. stp->st_deny_bmap, od->od_share_deny);
  3268. goto out;
  3269. }
  3270. nfs4_stateid_downgrade(stp, od->od_share_access);
  3271. reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
  3272. update_stateid(&stp->st_stid.sc_stateid);
  3273. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3274. status = nfs_ok;
  3275. out:
  3276. if (!cstate->replay_owner)
  3277. nfs4_unlock_state();
  3278. return status;
  3279. }
  3280. void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
  3281. {
  3282. struct nfs4_openowner *oo;
  3283. struct nfs4_ol_stateid *s;
  3284. if (!so->so_is_open_owner)
  3285. return;
  3286. oo = openowner(so);
  3287. s = oo->oo_last_closed_stid;
  3288. if (!s)
  3289. return;
  3290. if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
  3291. /* Release the last_closed_stid on the next seqid bump: */
  3292. oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
  3293. return;
  3294. }
  3295. oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
  3296. release_last_closed_stateid(oo);
  3297. }
  3298. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  3299. {
  3300. unhash_open_stateid(s);
  3301. s->st_stid.sc_type = NFS4_CLOSED_STID;
  3302. }
  3303. /*
  3304. * nfs4_unlock_state() called after encode
  3305. */
  3306. __be32
  3307. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3308. struct nfsd4_close *close)
  3309. {
  3310. __be32 status;
  3311. struct nfs4_openowner *oo;
  3312. struct nfs4_ol_stateid *stp;
  3313. dprintk("NFSD: nfsd4_close on file %.*s\n",
  3314. (int)cstate->current_fh.fh_dentry->d_name.len,
  3315. cstate->current_fh.fh_dentry->d_name.name);
  3316. nfs4_lock_state();
  3317. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  3318. &close->cl_stateid,
  3319. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  3320. &stp);
  3321. if (status)
  3322. goto out;
  3323. oo = openowner(stp->st_stateowner);
  3324. status = nfs_ok;
  3325. update_stateid(&stp->st_stid.sc_stateid);
  3326. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3327. nfsd4_close_open_stateid(stp);
  3328. oo->oo_last_closed_stid = stp;
  3329. /* place unused nfs4_stateowners on so_close_lru list to be
  3330. * released by the laundromat service after the lease period
  3331. * to enable us to handle CLOSE replay
  3332. */
  3333. if (list_empty(&oo->oo_owner.so_stateids))
  3334. move_to_close_lru(oo);
  3335. out:
  3336. if (!cstate->replay_owner)
  3337. nfs4_unlock_state();
  3338. return status;
  3339. }
  3340. __be32
  3341. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3342. struct nfsd4_delegreturn *dr)
  3343. {
  3344. struct nfs4_delegation *dp;
  3345. stateid_t *stateid = &dr->dr_stateid;
  3346. struct nfs4_stid *s;
  3347. struct inode *inode;
  3348. __be32 status;
  3349. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3350. return status;
  3351. inode = cstate->current_fh.fh_dentry->d_inode;
  3352. nfs4_lock_state();
  3353. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s);
  3354. if (status)
  3355. goto out;
  3356. dp = delegstateid(s);
  3357. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  3358. if (status)
  3359. goto out;
  3360. unhash_delegation(dp);
  3361. out:
  3362. nfs4_unlock_state();
  3363. return status;
  3364. }
  3365. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  3366. #define LOCKOWNER_INO_HASH_BITS 8
  3367. #define LOCKOWNER_INO_HASH_SIZE (1 << LOCKOWNER_INO_HASH_BITS)
  3368. #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
  3369. static inline u64
  3370. end_offset(u64 start, u64 len)
  3371. {
  3372. u64 end;
  3373. end = start + len;
  3374. return end >= start ? end: NFS4_MAX_UINT64;
  3375. }
  3376. /* last octet in a range */
  3377. static inline u64
  3378. last_byte_offset(u64 start, u64 len)
  3379. {
  3380. u64 end;
  3381. BUG_ON(!len);
  3382. end = start + len;
  3383. return end > start ? end - 1: NFS4_MAX_UINT64;
  3384. }
  3385. static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
  3386. {
  3387. return (file_hashval(inode) + cl_id
  3388. + opaque_hashval(ownername->data, ownername->len))
  3389. & LOCKOWNER_INO_HASH_MASK;
  3390. }
  3391. static struct list_head lockowner_ino_hashtbl[LOCKOWNER_INO_HASH_SIZE];
  3392. /*
  3393. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  3394. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  3395. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  3396. * locking, this prevents us from being completely protocol-compliant. The
  3397. * real solution to this problem is to start using unsigned file offsets in
  3398. * the VFS, but this is a very deep change!
  3399. */
  3400. static inline void
  3401. nfs4_transform_lock_offset(struct file_lock *lock)
  3402. {
  3403. if (lock->fl_start < 0)
  3404. lock->fl_start = OFFSET_MAX;
  3405. if (lock->fl_end < 0)
  3406. lock->fl_end = OFFSET_MAX;
  3407. }
  3408. /* Hack!: For now, we're defining this just so we can use a pointer to it
  3409. * as a unique cookie to identify our (NFSv4's) posix locks. */
  3410. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  3411. };
  3412. static inline void
  3413. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  3414. {
  3415. struct nfs4_lockowner *lo;
  3416. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  3417. lo = (struct nfs4_lockowner *) fl->fl_owner;
  3418. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  3419. lo->lo_owner.so_owner.len, GFP_KERNEL);
  3420. if (!deny->ld_owner.data)
  3421. /* We just don't care that much */
  3422. goto nevermind;
  3423. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  3424. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  3425. } else {
  3426. nevermind:
  3427. deny->ld_owner.len = 0;
  3428. deny->ld_owner.data = NULL;
  3429. deny->ld_clientid.cl_boot = 0;
  3430. deny->ld_clientid.cl_id = 0;
  3431. }
  3432. deny->ld_start = fl->fl_start;
  3433. deny->ld_length = NFS4_MAX_UINT64;
  3434. if (fl->fl_end != NFS4_MAX_UINT64)
  3435. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3436. deny->ld_type = NFS4_READ_LT;
  3437. if (fl->fl_type != F_RDLCK)
  3438. deny->ld_type = NFS4_WRITE_LT;
  3439. }
  3440. static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
  3441. {
  3442. struct nfs4_ol_stateid *lst;
  3443. if (!same_owner_str(&lo->lo_owner, owner, clid))
  3444. return false;
  3445. lst = list_first_entry(&lo->lo_owner.so_stateids,
  3446. struct nfs4_ol_stateid, st_perstateowner);
  3447. return lst->st_file->fi_inode == inode;
  3448. }
  3449. static struct nfs4_lockowner *
  3450. find_lockowner_str(struct inode *inode, clientid_t *clid,
  3451. struct xdr_netobj *owner)
  3452. {
  3453. unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
  3454. struct nfs4_lockowner *lo;
  3455. list_for_each_entry(lo, &lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
  3456. if (same_lockowner_ino(lo, inode, clid, owner))
  3457. return lo;
  3458. }
  3459. return NULL;
  3460. }
  3461. static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
  3462. {
  3463. struct inode *inode = open_stp->st_file->fi_inode;
  3464. unsigned int inohash = lockowner_ino_hashval(inode,
  3465. clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
  3466. list_add(&lo->lo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
  3467. list_add(&lo->lo_owner_ino_hash, &lockowner_ino_hashtbl[inohash]);
  3468. list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
  3469. }
  3470. /*
  3471. * Alloc a lock owner structure.
  3472. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3473. * occurred.
  3474. *
  3475. * strhashval = ownerstr_hashval
  3476. */
  3477. static struct nfs4_lockowner *
  3478. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
  3479. struct nfs4_lockowner *lo;
  3480. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  3481. if (!lo)
  3482. return NULL;
  3483. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  3484. lo->lo_owner.so_is_open_owner = 0;
  3485. /* It is the openowner seqid that will be incremented in encode in the
  3486. * case of new lockowners; so increment the lock seqid manually: */
  3487. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
  3488. hash_lockowner(lo, strhashval, clp, open_stp);
  3489. return lo;
  3490. }
  3491. static struct nfs4_ol_stateid *
  3492. alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
  3493. {
  3494. struct nfs4_ol_stateid *stp;
  3495. struct nfs4_client *clp = lo->lo_owner.so_client;
  3496. stp = nfs4_alloc_stateid(clp);
  3497. if (stp == NULL)
  3498. return NULL;
  3499. init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
  3500. list_add(&stp->st_perfile, &fp->fi_stateids);
  3501. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  3502. stp->st_stateowner = &lo->lo_owner;
  3503. get_nfs4_file(fp);
  3504. stp->st_file = fp;
  3505. stp->st_access_bmap = 0;
  3506. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3507. stp->st_openstp = open_stp;
  3508. return stp;
  3509. }
  3510. static int
  3511. check_lock_length(u64 offset, u64 length)
  3512. {
  3513. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3514. LOFF_OVERFLOW(offset, length)));
  3515. }
  3516. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  3517. {
  3518. struct nfs4_file *fp = lock_stp->st_file;
  3519. int oflag = nfs4_access_to_omode(access);
  3520. if (test_bit(access, &lock_stp->st_access_bmap))
  3521. return;
  3522. nfs4_file_get_access(fp, oflag);
  3523. __set_bit(access, &lock_stp->st_access_bmap);
  3524. }
  3525. __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
  3526. {
  3527. struct nfs4_file *fi = ost->st_file;
  3528. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  3529. struct nfs4_client *cl = oo->oo_owner.so_client;
  3530. struct nfs4_lockowner *lo;
  3531. unsigned int strhashval;
  3532. lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid, &lock->v.new.owner);
  3533. if (lo) {
  3534. if (!cstate->minorversion)
  3535. return nfserr_bad_seqid;
  3536. /* XXX: a lockowner always has exactly one stateid: */
  3537. *lst = list_first_entry(&lo->lo_owner.so_stateids,
  3538. struct nfs4_ol_stateid, st_perstateowner);
  3539. return nfs_ok;
  3540. }
  3541. strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
  3542. &lock->v.new.owner);
  3543. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  3544. if (lo == NULL)
  3545. return nfserr_jukebox;
  3546. *lst = alloc_init_lock_stateid(lo, fi, ost);
  3547. if (*lst == NULL) {
  3548. release_lockowner(lo);
  3549. return nfserr_jukebox;
  3550. }
  3551. *new = true;
  3552. return nfs_ok;
  3553. }
  3554. /*
  3555. * LOCK operation
  3556. */
  3557. __be32
  3558. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3559. struct nfsd4_lock *lock)
  3560. {
  3561. struct nfs4_openowner *open_sop = NULL;
  3562. struct nfs4_lockowner *lock_sop = NULL;
  3563. struct nfs4_ol_stateid *lock_stp;
  3564. struct nfs4_file *fp;
  3565. struct file *filp = NULL;
  3566. struct file_lock file_lock;
  3567. struct file_lock conflock;
  3568. __be32 status = 0;
  3569. bool new_state = false;
  3570. int lkflg;
  3571. int err;
  3572. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3573. (long long) lock->lk_offset,
  3574. (long long) lock->lk_length);
  3575. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3576. return nfserr_inval;
  3577. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3578. S_IFREG, NFSD_MAY_LOCK))) {
  3579. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3580. return status;
  3581. }
  3582. nfs4_lock_state();
  3583. if (lock->lk_is_new) {
  3584. /*
  3585. * Client indicates that this is a new lockowner.
  3586. * Use open owner and open stateid to create lock owner and
  3587. * lock stateid.
  3588. */
  3589. struct nfs4_ol_stateid *open_stp = NULL;
  3590. if (nfsd4_has_session(cstate))
  3591. /* See rfc 5661 18.10.3: given clientid is ignored: */
  3592. memcpy(&lock->v.new.clientid,
  3593. &cstate->session->se_client->cl_clientid,
  3594. sizeof(clientid_t));
  3595. status = nfserr_stale_clientid;
  3596. if (STALE_CLIENTID(&lock->lk_new_clientid))
  3597. goto out;
  3598. /* validate and update open stateid and open seqid */
  3599. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  3600. lock->lk_new_open_seqid,
  3601. &lock->lk_new_open_stateid,
  3602. &open_stp);
  3603. if (status)
  3604. goto out;
  3605. open_sop = openowner(open_stp->st_stateowner);
  3606. status = nfserr_bad_stateid;
  3607. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  3608. &lock->v.new.clientid))
  3609. goto out;
  3610. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  3611. &lock_stp, &new_state);
  3612. if (status)
  3613. goto out;
  3614. } else {
  3615. /* lock (lock owner + lock stateid) already exists */
  3616. status = nfs4_preprocess_seqid_op(cstate,
  3617. lock->lk_old_lock_seqid,
  3618. &lock->lk_old_lock_stateid,
  3619. NFS4_LOCK_STID, &lock_stp);
  3620. if (status)
  3621. goto out;
  3622. }
  3623. lock_sop = lockowner(lock_stp->st_stateowner);
  3624. fp = lock_stp->st_file;
  3625. lkflg = setlkflg(lock->lk_type);
  3626. status = nfs4_check_openmode(lock_stp, lkflg);
  3627. if (status)
  3628. goto out;
  3629. status = nfserr_grace;
  3630. if (locks_in_grace() && !lock->lk_reclaim)
  3631. goto out;
  3632. status = nfserr_no_grace;
  3633. if (!locks_in_grace() && lock->lk_reclaim)
  3634. goto out;
  3635. locks_init_lock(&file_lock);
  3636. switch (lock->lk_type) {
  3637. case NFS4_READ_LT:
  3638. case NFS4_READW_LT:
  3639. filp = find_readable_file(lock_stp->st_file);
  3640. if (filp)
  3641. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  3642. file_lock.fl_type = F_RDLCK;
  3643. break;
  3644. case NFS4_WRITE_LT:
  3645. case NFS4_WRITEW_LT:
  3646. filp = find_writeable_file(lock_stp->st_file);
  3647. if (filp)
  3648. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  3649. file_lock.fl_type = F_WRLCK;
  3650. break;
  3651. default:
  3652. status = nfserr_inval;
  3653. goto out;
  3654. }
  3655. if (!filp) {
  3656. status = nfserr_openmode;
  3657. goto out;
  3658. }
  3659. file_lock.fl_owner = (fl_owner_t)lock_sop;
  3660. file_lock.fl_pid = current->tgid;
  3661. file_lock.fl_file = filp;
  3662. file_lock.fl_flags = FL_POSIX;
  3663. file_lock.fl_lmops = &nfsd_posix_mng_ops;
  3664. file_lock.fl_start = lock->lk_offset;
  3665. file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3666. nfs4_transform_lock_offset(&file_lock);
  3667. /*
  3668. * Try to lock the file in the VFS.
  3669. * Note: locks.c uses the BKL to protect the inode's lock list.
  3670. */
  3671. err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
  3672. switch (-err) {
  3673. case 0: /* success! */
  3674. update_stateid(&lock_stp->st_stid.sc_stateid);
  3675. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  3676. sizeof(stateid_t));
  3677. status = 0;
  3678. break;
  3679. case (EAGAIN): /* conflock holds conflicting lock */
  3680. status = nfserr_denied;
  3681. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3682. nfs4_set_lock_denied(&conflock, &lock->lk_denied);
  3683. break;
  3684. case (EDEADLK):
  3685. status = nfserr_deadlock;
  3686. break;
  3687. default:
  3688. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3689. status = nfserrno(err);
  3690. break;
  3691. }
  3692. out:
  3693. if (status && new_state)
  3694. release_lockowner(lock_sop);
  3695. if (!cstate->replay_owner)
  3696. nfs4_unlock_state();
  3697. return status;
  3698. }
  3699. /*
  3700. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3701. * so we do a temporary open here just to get an open file to pass to
  3702. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3703. * inode operation.)
  3704. */
  3705. static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3706. {
  3707. struct file *file;
  3708. int err;
  3709. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3710. if (err)
  3711. return err;
  3712. err = vfs_test_lock(file, lock);
  3713. nfsd_close(file);
  3714. return err;
  3715. }
  3716. /*
  3717. * LOCKT operation
  3718. */
  3719. __be32
  3720. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3721. struct nfsd4_lockt *lockt)
  3722. {
  3723. struct inode *inode;
  3724. struct file_lock file_lock;
  3725. struct nfs4_lockowner *lo;
  3726. int error;
  3727. __be32 status;
  3728. if (locks_in_grace())
  3729. return nfserr_grace;
  3730. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3731. return nfserr_inval;
  3732. nfs4_lock_state();
  3733. status = nfserr_stale_clientid;
  3734. if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
  3735. goto out;
  3736. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3737. goto out;
  3738. inode = cstate->current_fh.fh_dentry->d_inode;
  3739. locks_init_lock(&file_lock);
  3740. switch (lockt->lt_type) {
  3741. case NFS4_READ_LT:
  3742. case NFS4_READW_LT:
  3743. file_lock.fl_type = F_RDLCK;
  3744. break;
  3745. case NFS4_WRITE_LT:
  3746. case NFS4_WRITEW_LT:
  3747. file_lock.fl_type = F_WRLCK;
  3748. break;
  3749. default:
  3750. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3751. status = nfserr_inval;
  3752. goto out;
  3753. }
  3754. lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
  3755. if (lo)
  3756. file_lock.fl_owner = (fl_owner_t)lo;
  3757. file_lock.fl_pid = current->tgid;
  3758. file_lock.fl_flags = FL_POSIX;
  3759. file_lock.fl_start = lockt->lt_offset;
  3760. file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3761. nfs4_transform_lock_offset(&file_lock);
  3762. status = nfs_ok;
  3763. error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
  3764. if (error) {
  3765. status = nfserrno(error);
  3766. goto out;
  3767. }
  3768. if (file_lock.fl_type != F_UNLCK) {
  3769. status = nfserr_denied;
  3770. nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
  3771. }
  3772. out:
  3773. nfs4_unlock_state();
  3774. return status;
  3775. }
  3776. __be32
  3777. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3778. struct nfsd4_locku *locku)
  3779. {
  3780. struct nfs4_ol_stateid *stp;
  3781. struct file *filp = NULL;
  3782. struct file_lock file_lock;
  3783. __be32 status;
  3784. int err;
  3785. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3786. (long long) locku->lu_offset,
  3787. (long long) locku->lu_length);
  3788. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3789. return nfserr_inval;
  3790. nfs4_lock_state();
  3791. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  3792. &locku->lu_stateid, NFS4_LOCK_STID, &stp);
  3793. if (status)
  3794. goto out;
  3795. filp = find_any_file(stp->st_file);
  3796. if (!filp) {
  3797. status = nfserr_lock_range;
  3798. goto out;
  3799. }
  3800. BUG_ON(!filp);
  3801. locks_init_lock(&file_lock);
  3802. file_lock.fl_type = F_UNLCK;
  3803. file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
  3804. file_lock.fl_pid = current->tgid;
  3805. file_lock.fl_file = filp;
  3806. file_lock.fl_flags = FL_POSIX;
  3807. file_lock.fl_lmops = &nfsd_posix_mng_ops;
  3808. file_lock.fl_start = locku->lu_offset;
  3809. file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
  3810. nfs4_transform_lock_offset(&file_lock);
  3811. /*
  3812. * Try to unlock the file in the VFS.
  3813. */
  3814. err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
  3815. if (err) {
  3816. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3817. goto out_nfserr;
  3818. }
  3819. /*
  3820. * OK, unlock succeeded; the only thing left to do is update the stateid.
  3821. */
  3822. update_stateid(&stp->st_stid.sc_stateid);
  3823. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3824. out:
  3825. if (!cstate->replay_owner)
  3826. nfs4_unlock_state();
  3827. return status;
  3828. out_nfserr:
  3829. status = nfserrno(err);
  3830. goto out;
  3831. }
  3832. /*
  3833. * returns
  3834. * 1: locks held by lockowner
  3835. * 0: no locks held by lockowner
  3836. */
  3837. static int
  3838. check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
  3839. {
  3840. struct file_lock **flpp;
  3841. struct inode *inode = filp->fi_inode;
  3842. int status = 0;
  3843. lock_flocks();
  3844. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3845. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3846. status = 1;
  3847. goto out;
  3848. }
  3849. }
  3850. out:
  3851. unlock_flocks();
  3852. return status;
  3853. }
  3854. __be32
  3855. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3856. struct nfsd4_compound_state *cstate,
  3857. struct nfsd4_release_lockowner *rlockowner)
  3858. {
  3859. clientid_t *clid = &rlockowner->rl_clientid;
  3860. struct nfs4_stateowner *sop;
  3861. struct nfs4_lockowner *lo;
  3862. struct nfs4_ol_stateid *stp;
  3863. struct xdr_netobj *owner = &rlockowner->rl_owner;
  3864. struct list_head matches;
  3865. unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
  3866. __be32 status;
  3867. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  3868. clid->cl_boot, clid->cl_id);
  3869. /* XXX check for lease expiration */
  3870. status = nfserr_stale_clientid;
  3871. if (STALE_CLIENTID(clid))
  3872. return status;
  3873. nfs4_lock_state();
  3874. status = nfserr_locks_held;
  3875. INIT_LIST_HEAD(&matches);
  3876. list_for_each_entry(sop, &ownerstr_hashtbl[hashval], so_strhash) {
  3877. if (sop->so_is_open_owner)
  3878. continue;
  3879. if (!same_owner_str(sop, owner, clid))
  3880. continue;
  3881. list_for_each_entry(stp, &sop->so_stateids,
  3882. st_perstateowner) {
  3883. lo = lockowner(sop);
  3884. if (check_for_locks(stp->st_file, lo))
  3885. goto out;
  3886. list_add(&lo->lo_list, &matches);
  3887. }
  3888. }
  3889. /* Clients probably won't expect us to return with some (but not all)
  3890. * of the lockowner state released; so don't release any until all
  3891. * have been checked. */
  3892. status = nfs_ok;
  3893. while (!list_empty(&matches)) {
  3894. lo = list_entry(matches.next, struct nfs4_lockowner,
  3895. lo_list);
  3896. /* unhash_stateowner deletes so_perclient only
  3897. * for openowners. */
  3898. list_del(&lo->lo_list);
  3899. release_lockowner(lo);
  3900. }
  3901. out:
  3902. nfs4_unlock_state();
  3903. return status;
  3904. }
  3905. static inline struct nfs4_client_reclaim *
  3906. alloc_reclaim(void)
  3907. {
  3908. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  3909. }
  3910. int
  3911. nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
  3912. {
  3913. unsigned int strhashval = clientstr_hashval(name);
  3914. struct nfs4_client *clp;
  3915. clp = find_confirmed_client_by_str(name, strhashval);
  3916. if (!clp)
  3917. return 0;
  3918. return test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
  3919. }
  3920. /*
  3921. * failure => all reset bets are off, nfserr_no_grace...
  3922. */
  3923. int
  3924. nfs4_client_to_reclaim(const char *name)
  3925. {
  3926. unsigned int strhashval;
  3927. struct nfs4_client_reclaim *crp = NULL;
  3928. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  3929. crp = alloc_reclaim();
  3930. if (!crp)
  3931. return 0;
  3932. strhashval = clientstr_hashval(name);
  3933. INIT_LIST_HEAD(&crp->cr_strhash);
  3934. list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
  3935. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  3936. reclaim_str_hashtbl_size++;
  3937. return 1;
  3938. }
  3939. void
  3940. nfs4_release_reclaim(void)
  3941. {
  3942. struct nfs4_client_reclaim *crp = NULL;
  3943. int i;
  3944. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  3945. while (!list_empty(&reclaim_str_hashtbl[i])) {
  3946. crp = list_entry(reclaim_str_hashtbl[i].next,
  3947. struct nfs4_client_reclaim, cr_strhash);
  3948. list_del(&crp->cr_strhash);
  3949. kfree(crp);
  3950. reclaim_str_hashtbl_size--;
  3951. }
  3952. }
  3953. BUG_ON(reclaim_str_hashtbl_size);
  3954. }
  3955. /*
  3956. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  3957. struct nfs4_client_reclaim *
  3958. nfsd4_find_reclaim_client(struct nfs4_client *clp)
  3959. {
  3960. unsigned int strhashval;
  3961. struct nfs4_client_reclaim *crp = NULL;
  3962. dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
  3963. clp->cl_name.len, clp->cl_name.data,
  3964. clp->cl_recdir);
  3965. /* find clp->cl_name in reclaim_str_hashtbl */
  3966. strhashval = clientstr_hashval(clp->cl_recdir);
  3967. list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
  3968. if (same_name(crp->cr_recdir, clp->cl_recdir)) {
  3969. return crp;
  3970. }
  3971. }
  3972. return NULL;
  3973. }
  3974. /*
  3975. * Called from OPEN. Look for clientid in reclaim list.
  3976. */
  3977. __be32
  3978. nfs4_check_open_reclaim(clientid_t *clid)
  3979. {
  3980. struct nfs4_client *clp;
  3981. /* find clientid in conf_id_hashtbl */
  3982. clp = find_confirmed_client(clid);
  3983. if (clp == NULL)
  3984. return nfserr_reclaim_bad;
  3985. return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
  3986. }
  3987. #ifdef CONFIG_NFSD_FAULT_INJECTION
  3988. void nfsd_forget_clients(u64 num)
  3989. {
  3990. struct nfs4_client *clp, *next;
  3991. int count = 0;
  3992. nfs4_lock_state();
  3993. list_for_each_entry_safe(clp, next, &client_lru, cl_lru) {
  3994. nfsd4_client_record_remove(clp);
  3995. expire_client(clp);
  3996. if (++count == num)
  3997. break;
  3998. }
  3999. nfs4_unlock_state();
  4000. printk(KERN_INFO "NFSD: Forgot %d clients", count);
  4001. }
  4002. static void release_lockowner_sop(struct nfs4_stateowner *sop)
  4003. {
  4004. release_lockowner(lockowner(sop));
  4005. }
  4006. static void release_openowner_sop(struct nfs4_stateowner *sop)
  4007. {
  4008. release_openowner(openowner(sop));
  4009. }
  4010. static int nfsd_release_n_owners(u64 num, bool is_open_owner,
  4011. void (*release_sop)(struct nfs4_stateowner *))
  4012. {
  4013. int i, count = 0;
  4014. struct nfs4_stateowner *sop, *next;
  4015. for (i = 0; i < OWNER_HASH_SIZE; i++) {
  4016. list_for_each_entry_safe(sop, next, &ownerstr_hashtbl[i], so_strhash) {
  4017. if (sop->so_is_open_owner != is_open_owner)
  4018. continue;
  4019. release_sop(sop);
  4020. if (++count == num)
  4021. return count;
  4022. }
  4023. }
  4024. return count;
  4025. }
  4026. void nfsd_forget_locks(u64 num)
  4027. {
  4028. int count;
  4029. nfs4_lock_state();
  4030. count = nfsd_release_n_owners(num, false, release_lockowner_sop);
  4031. nfs4_unlock_state();
  4032. printk(KERN_INFO "NFSD: Forgot %d locks", count);
  4033. }
  4034. void nfsd_forget_openowners(u64 num)
  4035. {
  4036. int count;
  4037. nfs4_lock_state();
  4038. count = nfsd_release_n_owners(num, true, release_openowner_sop);
  4039. nfs4_unlock_state();
  4040. printk(KERN_INFO "NFSD: Forgot %d open owners", count);
  4041. }
  4042. int nfsd_process_n_delegations(u64 num, void (*deleg_func)(struct nfs4_delegation *))
  4043. {
  4044. int i, count = 0;
  4045. struct nfs4_file *fp, *fnext;
  4046. struct nfs4_delegation *dp, *dnext;
  4047. for (i = 0; i < FILE_HASH_SIZE; i++) {
  4048. list_for_each_entry_safe(fp, fnext, &file_hashtbl[i], fi_hash) {
  4049. list_for_each_entry_safe(dp, dnext, &fp->fi_delegations, dl_perfile) {
  4050. deleg_func(dp);
  4051. if (++count == num)
  4052. return count;
  4053. }
  4054. }
  4055. }
  4056. return count;
  4057. }
  4058. void nfsd_forget_delegations(u64 num)
  4059. {
  4060. unsigned int count;
  4061. nfs4_lock_state();
  4062. count = nfsd_process_n_delegations(num, unhash_delegation);
  4063. nfs4_unlock_state();
  4064. printk(KERN_INFO "NFSD: Forgot %d delegations", count);
  4065. }
  4066. void nfsd_recall_delegations(u64 num)
  4067. {
  4068. unsigned int count;
  4069. nfs4_lock_state();
  4070. spin_lock(&recall_lock);
  4071. count = nfsd_process_n_delegations(num, nfsd_break_one_deleg);
  4072. spin_unlock(&recall_lock);
  4073. nfs4_unlock_state();
  4074. printk(KERN_INFO "NFSD: Recalled %d delegations", count);
  4075. }
  4076. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  4077. /* initialization to perform at module load time: */
  4078. void
  4079. nfs4_state_init(void)
  4080. {
  4081. int i;
  4082. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4083. INIT_LIST_HEAD(&conf_id_hashtbl[i]);
  4084. INIT_LIST_HEAD(&conf_str_hashtbl[i]);
  4085. INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
  4086. INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
  4087. INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
  4088. }
  4089. for (i = 0; i < SESSION_HASH_SIZE; i++)
  4090. INIT_LIST_HEAD(&sessionid_hashtbl[i]);
  4091. for (i = 0; i < FILE_HASH_SIZE; i++) {
  4092. INIT_LIST_HEAD(&file_hashtbl[i]);
  4093. }
  4094. for (i = 0; i < OWNER_HASH_SIZE; i++) {
  4095. INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
  4096. }
  4097. for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
  4098. INIT_LIST_HEAD(&lockowner_ino_hashtbl[i]);
  4099. INIT_LIST_HEAD(&close_lru);
  4100. INIT_LIST_HEAD(&client_lru);
  4101. INIT_LIST_HEAD(&del_recall_lru);
  4102. reclaim_str_hashtbl_size = 0;
  4103. }
  4104. /*
  4105. * Since the lifetime of a delegation isn't limited to that of an open, a
  4106. * client may quite reasonably hang on to a delegation as long as it has
  4107. * the inode cached. This becomes an obvious problem the first time a
  4108. * client's inode cache approaches the size of the server's total memory.
  4109. *
  4110. * For now we avoid this problem by imposing a hard limit on the number
  4111. * of delegations, which varies according to the server's memory size.
  4112. */
  4113. static void
  4114. set_max_delegations(void)
  4115. {
  4116. /*
  4117. * Allow at most 4 delegations per megabyte of RAM. Quick
  4118. * estimates suggest that in the worst case (where every delegation
  4119. * is for a different inode), a delegation could take about 1.5K,
  4120. * giving a worst case usage of about 6% of memory.
  4121. */
  4122. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  4123. }
  4124. /* initialization to perform when the nfsd service is started: */
  4125. int
  4126. nfs4_state_start(void)
  4127. {
  4128. int ret;
  4129. /*
  4130. * FIXME: For now, we hang most of the pernet global stuff off of
  4131. * init_net until nfsd is fully containerized. Eventually, we'll
  4132. * need to pass a net pointer into this function, take a reference
  4133. * to that instead and then do most of the rest of this on a per-net
  4134. * basis.
  4135. */
  4136. get_net(&init_net);
  4137. nfsd4_client_tracking_init(&init_net);
  4138. boot_time = get_seconds();
  4139. locks_start_grace(&nfsd4_manager);
  4140. printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
  4141. nfsd4_grace);
  4142. ret = set_callback_cred();
  4143. if (ret) {
  4144. ret = -ENOMEM;
  4145. goto out_recovery;
  4146. }
  4147. laundry_wq = create_singlethread_workqueue("nfsd4");
  4148. if (laundry_wq == NULL) {
  4149. ret = -ENOMEM;
  4150. goto out_recovery;
  4151. }
  4152. ret = nfsd4_create_callback_queue();
  4153. if (ret)
  4154. goto out_free_laundry;
  4155. queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
  4156. set_max_delegations();
  4157. return 0;
  4158. out_free_laundry:
  4159. destroy_workqueue(laundry_wq);
  4160. out_recovery:
  4161. nfsd4_client_tracking_exit(&init_net);
  4162. put_net(&init_net);
  4163. return ret;
  4164. }
  4165. static void
  4166. __nfs4_state_shutdown(void)
  4167. {
  4168. int i;
  4169. struct nfs4_client *clp = NULL;
  4170. struct nfs4_delegation *dp = NULL;
  4171. struct list_head *pos, *next, reaplist;
  4172. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4173. while (!list_empty(&conf_id_hashtbl[i])) {
  4174. clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  4175. expire_client(clp);
  4176. }
  4177. while (!list_empty(&unconf_str_hashtbl[i])) {
  4178. clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
  4179. expire_client(clp);
  4180. }
  4181. }
  4182. INIT_LIST_HEAD(&reaplist);
  4183. spin_lock(&recall_lock);
  4184. list_for_each_safe(pos, next, &del_recall_lru) {
  4185. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4186. list_move(&dp->dl_recall_lru, &reaplist);
  4187. }
  4188. spin_unlock(&recall_lock);
  4189. list_for_each_safe(pos, next, &reaplist) {
  4190. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4191. unhash_delegation(dp);
  4192. }
  4193. nfsd4_client_tracking_exit(&init_net);
  4194. put_net(&init_net);
  4195. }
  4196. void
  4197. nfs4_state_shutdown(void)
  4198. {
  4199. cancel_delayed_work_sync(&laundromat_work);
  4200. destroy_workqueue(laundry_wq);
  4201. locks_end_grace(&nfsd4_manager);
  4202. nfs4_lock_state();
  4203. __nfs4_state_shutdown();
  4204. nfs4_unlock_state();
  4205. nfsd4_destroy_callback_queue();
  4206. }
  4207. static void
  4208. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4209. {
  4210. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  4211. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  4212. }
  4213. static void
  4214. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4215. {
  4216. if (cstate->minorversion) {
  4217. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  4218. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4219. }
  4220. }
  4221. void
  4222. clear_current_stateid(struct nfsd4_compound_state *cstate)
  4223. {
  4224. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4225. }
  4226. /*
  4227. * functions to set current state id
  4228. */
  4229. void
  4230. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4231. {
  4232. put_stateid(cstate, &odp->od_stateid);
  4233. }
  4234. void
  4235. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  4236. {
  4237. put_stateid(cstate, &open->op_stateid);
  4238. }
  4239. void
  4240. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4241. {
  4242. put_stateid(cstate, &close->cl_stateid);
  4243. }
  4244. void
  4245. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  4246. {
  4247. put_stateid(cstate, &lock->lk_resp_stateid);
  4248. }
  4249. /*
  4250. * functions to consume current state id
  4251. */
  4252. void
  4253. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4254. {
  4255. get_stateid(cstate, &odp->od_stateid);
  4256. }
  4257. void
  4258. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  4259. {
  4260. get_stateid(cstate, &drp->dr_stateid);
  4261. }
  4262. void
  4263. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  4264. {
  4265. get_stateid(cstate, &fsp->fr_stateid);
  4266. }
  4267. void
  4268. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  4269. {
  4270. get_stateid(cstate, &setattr->sa_stateid);
  4271. }
  4272. void
  4273. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4274. {
  4275. get_stateid(cstate, &close->cl_stateid);
  4276. }
  4277. void
  4278. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  4279. {
  4280. get_stateid(cstate, &locku->lu_stateid);
  4281. }
  4282. void
  4283. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  4284. {
  4285. get_stateid(cstate, &read->rd_stateid);
  4286. }
  4287. void
  4288. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  4289. {
  4290. get_stateid(cstate, &write->wr_stateid);
  4291. }