nfs4state.c 140 KB

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