nfs4state.c 133 KB

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