nfs4state.c 133 KB

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