nfs4state.c 182 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192
  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/jhash.h>
  44. #include "xdr4.h"
  45. #include "xdr4cb.h"
  46. #include "vfs.h"
  47. #include "current_stateid.h"
  48. #include "netns.h"
  49. #include "pnfs.h"
  50. #define NFSDDBG_FACILITY NFSDDBG_PROC
  51. #define all_ones {{~0,~0},~0}
  52. static const stateid_t one_stateid = {
  53. .si_generation = ~0,
  54. .si_opaque = all_ones,
  55. };
  56. static const stateid_t zero_stateid = {
  57. /* all fields zero */
  58. };
  59. static const stateid_t currentstateid = {
  60. .si_generation = 1,
  61. };
  62. static u64 current_sessionid = 1;
  63. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  64. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  65. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  66. /* forward declarations */
  67. static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
  68. static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
  69. /* Locking: */
  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. /*
  77. * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
  78. * the refcount on the open stateid to drop.
  79. */
  80. static DECLARE_WAIT_QUEUE_HEAD(close_wq);
  81. static struct kmem_cache *openowner_slab;
  82. static struct kmem_cache *lockowner_slab;
  83. static struct kmem_cache *file_slab;
  84. static struct kmem_cache *stateid_slab;
  85. static struct kmem_cache *deleg_slab;
  86. static struct kmem_cache *odstate_slab;
  87. static void free_session(struct nfsd4_session *);
  88. static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
  89. static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
  90. static bool is_session_dead(struct nfsd4_session *ses)
  91. {
  92. return ses->se_flags & NFS4_SESSION_DEAD;
  93. }
  94. static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
  95. {
  96. if (atomic_read(&ses->se_ref) > ref_held_by_me)
  97. return nfserr_jukebox;
  98. ses->se_flags |= NFS4_SESSION_DEAD;
  99. return nfs_ok;
  100. }
  101. static bool is_client_expired(struct nfs4_client *clp)
  102. {
  103. return clp->cl_time == 0;
  104. }
  105. static __be32 get_client_locked(struct nfs4_client *clp)
  106. {
  107. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  108. lockdep_assert_held(&nn->client_lock);
  109. if (is_client_expired(clp))
  110. return nfserr_expired;
  111. atomic_inc(&clp->cl_refcount);
  112. return nfs_ok;
  113. }
  114. /* must be called under the client_lock */
  115. static inline void
  116. renew_client_locked(struct nfs4_client *clp)
  117. {
  118. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  119. if (is_client_expired(clp)) {
  120. WARN_ON(1);
  121. printk("%s: client (clientid %08x/%08x) already expired\n",
  122. __func__,
  123. clp->cl_clientid.cl_boot,
  124. clp->cl_clientid.cl_id);
  125. return;
  126. }
  127. dprintk("renewing client (clientid %08x/%08x)\n",
  128. clp->cl_clientid.cl_boot,
  129. clp->cl_clientid.cl_id);
  130. list_move_tail(&clp->cl_lru, &nn->client_lru);
  131. clp->cl_time = get_seconds();
  132. }
  133. static void put_client_renew_locked(struct nfs4_client *clp)
  134. {
  135. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  136. lockdep_assert_held(&nn->client_lock);
  137. if (!atomic_dec_and_test(&clp->cl_refcount))
  138. return;
  139. if (!is_client_expired(clp))
  140. renew_client_locked(clp);
  141. }
  142. static void put_client_renew(struct nfs4_client *clp)
  143. {
  144. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  145. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  146. return;
  147. if (!is_client_expired(clp))
  148. renew_client_locked(clp);
  149. spin_unlock(&nn->client_lock);
  150. }
  151. static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
  152. {
  153. __be32 status;
  154. if (is_session_dead(ses))
  155. return nfserr_badsession;
  156. status = get_client_locked(ses->se_client);
  157. if (status)
  158. return status;
  159. atomic_inc(&ses->se_ref);
  160. return nfs_ok;
  161. }
  162. static void nfsd4_put_session_locked(struct nfsd4_session *ses)
  163. {
  164. struct nfs4_client *clp = ses->se_client;
  165. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  166. lockdep_assert_held(&nn->client_lock);
  167. if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
  168. free_session(ses);
  169. put_client_renew_locked(clp);
  170. }
  171. static void nfsd4_put_session(struct nfsd4_session *ses)
  172. {
  173. struct nfs4_client *clp = ses->se_client;
  174. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  175. spin_lock(&nn->client_lock);
  176. nfsd4_put_session_locked(ses);
  177. spin_unlock(&nn->client_lock);
  178. }
  179. static struct nfsd4_blocked_lock *
  180. find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
  181. struct nfsd_net *nn)
  182. {
  183. struct nfsd4_blocked_lock *cur, *found = NULL;
  184. spin_lock(&nn->blocked_locks_lock);
  185. list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
  186. if (fh_match(fh, &cur->nbl_fh)) {
  187. list_del_init(&cur->nbl_list);
  188. list_del_init(&cur->nbl_lru);
  189. found = cur;
  190. break;
  191. }
  192. }
  193. spin_unlock(&nn->blocked_locks_lock);
  194. if (found)
  195. posix_unblock_lock(&found->nbl_lock);
  196. return found;
  197. }
  198. static struct nfsd4_blocked_lock *
  199. find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
  200. struct nfsd_net *nn)
  201. {
  202. struct nfsd4_blocked_lock *nbl;
  203. nbl = find_blocked_lock(lo, fh, nn);
  204. if (!nbl) {
  205. nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
  206. if (nbl) {
  207. fh_copy_shallow(&nbl->nbl_fh, fh);
  208. locks_init_lock(&nbl->nbl_lock);
  209. nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
  210. &nfsd4_cb_notify_lock_ops,
  211. NFSPROC4_CLNT_CB_NOTIFY_LOCK);
  212. }
  213. }
  214. return nbl;
  215. }
  216. static void
  217. free_blocked_lock(struct nfsd4_blocked_lock *nbl)
  218. {
  219. locks_release_private(&nbl->nbl_lock);
  220. kfree(nbl);
  221. }
  222. static int
  223. nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
  224. {
  225. /*
  226. * Since this is just an optimization, we don't try very hard if it
  227. * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
  228. * just quit trying on anything else.
  229. */
  230. switch (task->tk_status) {
  231. case -NFS4ERR_DELAY:
  232. rpc_delay(task, 1 * HZ);
  233. return 0;
  234. default:
  235. return 1;
  236. }
  237. }
  238. static void
  239. nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
  240. {
  241. struct nfsd4_blocked_lock *nbl = container_of(cb,
  242. struct nfsd4_blocked_lock, nbl_cb);
  243. free_blocked_lock(nbl);
  244. }
  245. static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
  246. .done = nfsd4_cb_notify_lock_done,
  247. .release = nfsd4_cb_notify_lock_release,
  248. };
  249. static inline struct nfs4_stateowner *
  250. nfs4_get_stateowner(struct nfs4_stateowner *sop)
  251. {
  252. atomic_inc(&sop->so_count);
  253. return sop;
  254. }
  255. static int
  256. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
  257. {
  258. return (sop->so_owner.len == owner->len) &&
  259. 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
  260. }
  261. static struct nfs4_openowner *
  262. find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
  263. struct nfs4_client *clp)
  264. {
  265. struct nfs4_stateowner *so;
  266. lockdep_assert_held(&clp->cl_lock);
  267. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
  268. so_strhash) {
  269. if (!so->so_is_open_owner)
  270. continue;
  271. if (same_owner_str(so, &open->op_owner))
  272. return openowner(nfs4_get_stateowner(so));
  273. }
  274. return NULL;
  275. }
  276. static struct nfs4_openowner *
  277. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  278. struct nfs4_client *clp)
  279. {
  280. struct nfs4_openowner *oo;
  281. spin_lock(&clp->cl_lock);
  282. oo = find_openstateowner_str_locked(hashval, open, clp);
  283. spin_unlock(&clp->cl_lock);
  284. return oo;
  285. }
  286. static inline u32
  287. opaque_hashval(const void *ptr, int nbytes)
  288. {
  289. unsigned char *cptr = (unsigned char *) ptr;
  290. u32 x = 0;
  291. while (nbytes--) {
  292. x *= 37;
  293. x += *cptr++;
  294. }
  295. return x;
  296. }
  297. static void nfsd4_free_file_rcu(struct rcu_head *rcu)
  298. {
  299. struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
  300. kmem_cache_free(file_slab, fp);
  301. }
  302. void
  303. put_nfs4_file(struct nfs4_file *fi)
  304. {
  305. might_lock(&state_lock);
  306. if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
  307. hlist_del_rcu(&fi->fi_hash);
  308. spin_unlock(&state_lock);
  309. WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
  310. WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
  311. call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
  312. }
  313. }
  314. static struct file *
  315. __nfs4_get_fd(struct nfs4_file *f, int oflag)
  316. {
  317. if (f->fi_fds[oflag])
  318. return get_file(f->fi_fds[oflag]);
  319. return NULL;
  320. }
  321. static struct file *
  322. find_writeable_file_locked(struct nfs4_file *f)
  323. {
  324. struct file *ret;
  325. lockdep_assert_held(&f->fi_lock);
  326. ret = __nfs4_get_fd(f, O_WRONLY);
  327. if (!ret)
  328. ret = __nfs4_get_fd(f, O_RDWR);
  329. return ret;
  330. }
  331. static struct file *
  332. find_writeable_file(struct nfs4_file *f)
  333. {
  334. struct file *ret;
  335. spin_lock(&f->fi_lock);
  336. ret = find_writeable_file_locked(f);
  337. spin_unlock(&f->fi_lock);
  338. return ret;
  339. }
  340. static struct file *find_readable_file_locked(struct nfs4_file *f)
  341. {
  342. struct file *ret;
  343. lockdep_assert_held(&f->fi_lock);
  344. ret = __nfs4_get_fd(f, O_RDONLY);
  345. if (!ret)
  346. ret = __nfs4_get_fd(f, O_RDWR);
  347. return ret;
  348. }
  349. static struct file *
  350. find_readable_file(struct nfs4_file *f)
  351. {
  352. struct file *ret;
  353. spin_lock(&f->fi_lock);
  354. ret = find_readable_file_locked(f);
  355. spin_unlock(&f->fi_lock);
  356. return ret;
  357. }
  358. struct file *
  359. find_any_file(struct nfs4_file *f)
  360. {
  361. struct file *ret;
  362. spin_lock(&f->fi_lock);
  363. ret = __nfs4_get_fd(f, O_RDWR);
  364. if (!ret) {
  365. ret = __nfs4_get_fd(f, O_WRONLY);
  366. if (!ret)
  367. ret = __nfs4_get_fd(f, O_RDONLY);
  368. }
  369. spin_unlock(&f->fi_lock);
  370. return ret;
  371. }
  372. static atomic_long_t num_delegations;
  373. unsigned long max_delegations;
  374. /*
  375. * Open owner state (share locks)
  376. */
  377. /* hash tables for lock and open owners */
  378. #define OWNER_HASH_BITS 8
  379. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  380. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  381. static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
  382. {
  383. unsigned int ret;
  384. ret = opaque_hashval(ownername->data, ownername->len);
  385. return ret & OWNER_HASH_MASK;
  386. }
  387. /* hash table for nfs4_file */
  388. #define FILE_HASH_BITS 8
  389. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  390. static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
  391. {
  392. return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
  393. }
  394. static unsigned int file_hashval(struct knfsd_fh *fh)
  395. {
  396. return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
  397. }
  398. static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
  399. static void
  400. __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
  401. {
  402. lockdep_assert_held(&fp->fi_lock);
  403. if (access & NFS4_SHARE_ACCESS_WRITE)
  404. atomic_inc(&fp->fi_access[O_WRONLY]);
  405. if (access & NFS4_SHARE_ACCESS_READ)
  406. atomic_inc(&fp->fi_access[O_RDONLY]);
  407. }
  408. static __be32
  409. nfs4_file_get_access(struct nfs4_file *fp, u32 access)
  410. {
  411. lockdep_assert_held(&fp->fi_lock);
  412. /* Does this access mode make sense? */
  413. if (access & ~NFS4_SHARE_ACCESS_BOTH)
  414. return nfserr_inval;
  415. /* Does it conflict with a deny mode already set? */
  416. if ((access & fp->fi_share_deny) != 0)
  417. return nfserr_share_denied;
  418. __nfs4_file_get_access(fp, access);
  419. return nfs_ok;
  420. }
  421. static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
  422. {
  423. /* Common case is that there is no deny mode. */
  424. if (deny) {
  425. /* Does this deny mode make sense? */
  426. if (deny & ~NFS4_SHARE_DENY_BOTH)
  427. return nfserr_inval;
  428. if ((deny & NFS4_SHARE_DENY_READ) &&
  429. atomic_read(&fp->fi_access[O_RDONLY]))
  430. return nfserr_share_denied;
  431. if ((deny & NFS4_SHARE_DENY_WRITE) &&
  432. atomic_read(&fp->fi_access[O_WRONLY]))
  433. return nfserr_share_denied;
  434. }
  435. return nfs_ok;
  436. }
  437. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  438. {
  439. might_lock(&fp->fi_lock);
  440. if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
  441. struct file *f1 = NULL;
  442. struct file *f2 = NULL;
  443. swap(f1, fp->fi_fds[oflag]);
  444. if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
  445. swap(f2, fp->fi_fds[O_RDWR]);
  446. spin_unlock(&fp->fi_lock);
  447. if (f1)
  448. fput(f1);
  449. if (f2)
  450. fput(f2);
  451. }
  452. }
  453. static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
  454. {
  455. WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
  456. if (access & NFS4_SHARE_ACCESS_WRITE)
  457. __nfs4_file_put_access(fp, O_WRONLY);
  458. if (access & NFS4_SHARE_ACCESS_READ)
  459. __nfs4_file_put_access(fp, O_RDONLY);
  460. }
  461. /*
  462. * Allocate a new open/delegation state counter. This is needed for
  463. * pNFS for proper return on close semantics.
  464. *
  465. * Note that we only allocate it for pNFS-enabled exports, otherwise
  466. * all pointers to struct nfs4_clnt_odstate are always NULL.
  467. */
  468. static struct nfs4_clnt_odstate *
  469. alloc_clnt_odstate(struct nfs4_client *clp)
  470. {
  471. struct nfs4_clnt_odstate *co;
  472. co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
  473. if (co) {
  474. co->co_client = clp;
  475. atomic_set(&co->co_odcount, 1);
  476. }
  477. return co;
  478. }
  479. static void
  480. hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
  481. {
  482. struct nfs4_file *fp = co->co_file;
  483. lockdep_assert_held(&fp->fi_lock);
  484. list_add(&co->co_perfile, &fp->fi_clnt_odstate);
  485. }
  486. static inline void
  487. get_clnt_odstate(struct nfs4_clnt_odstate *co)
  488. {
  489. if (co)
  490. atomic_inc(&co->co_odcount);
  491. }
  492. static void
  493. put_clnt_odstate(struct nfs4_clnt_odstate *co)
  494. {
  495. struct nfs4_file *fp;
  496. if (!co)
  497. return;
  498. fp = co->co_file;
  499. if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
  500. list_del(&co->co_perfile);
  501. spin_unlock(&fp->fi_lock);
  502. nfsd4_return_all_file_layouts(co->co_client, fp);
  503. kmem_cache_free(odstate_slab, co);
  504. }
  505. }
  506. static struct nfs4_clnt_odstate *
  507. find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
  508. {
  509. struct nfs4_clnt_odstate *co;
  510. struct nfs4_client *cl;
  511. if (!new)
  512. return NULL;
  513. cl = new->co_client;
  514. spin_lock(&fp->fi_lock);
  515. list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
  516. if (co->co_client == cl) {
  517. get_clnt_odstate(co);
  518. goto out;
  519. }
  520. }
  521. co = new;
  522. co->co_file = fp;
  523. hash_clnt_odstate_locked(new);
  524. out:
  525. spin_unlock(&fp->fi_lock);
  526. return co;
  527. }
  528. struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
  529. void (*sc_free)(struct nfs4_stid *))
  530. {
  531. struct nfs4_stid *stid;
  532. int new_id;
  533. stid = kmem_cache_zalloc(slab, GFP_KERNEL);
  534. if (!stid)
  535. return NULL;
  536. idr_preload(GFP_KERNEL);
  537. spin_lock(&cl->cl_lock);
  538. new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
  539. spin_unlock(&cl->cl_lock);
  540. idr_preload_end();
  541. if (new_id < 0)
  542. goto out_free;
  543. stid->sc_free = sc_free;
  544. stid->sc_client = cl;
  545. stid->sc_stateid.si_opaque.so_id = new_id;
  546. stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
  547. /* Will be incremented before return to client: */
  548. atomic_set(&stid->sc_count, 1);
  549. spin_lock_init(&stid->sc_lock);
  550. /*
  551. * It shouldn't be a problem to reuse an opaque stateid value.
  552. * I don't think it is for 4.1. But with 4.0 I worry that, for
  553. * example, a stray write retransmission could be accepted by
  554. * the server when it should have been rejected. Therefore,
  555. * adopt a trick from the sctp code to attempt to maximize the
  556. * amount of time until an id is reused, by ensuring they always
  557. * "increase" (mod INT_MAX):
  558. */
  559. return stid;
  560. out_free:
  561. kmem_cache_free(slab, stid);
  562. return NULL;
  563. }
  564. static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
  565. {
  566. struct nfs4_stid *stid;
  567. stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
  568. if (!stid)
  569. return NULL;
  570. return openlockstateid(stid);
  571. }
  572. static void nfs4_free_deleg(struct nfs4_stid *stid)
  573. {
  574. kmem_cache_free(deleg_slab, stid);
  575. atomic_long_dec(&num_delegations);
  576. }
  577. /*
  578. * When we recall a delegation, we should be careful not to hand it
  579. * out again straight away.
  580. * To ensure this we keep a pair of bloom filters ('new' and 'old')
  581. * in which the filehandles of recalled delegations are "stored".
  582. * If a filehandle appear in either filter, a delegation is blocked.
  583. * When a delegation is recalled, the filehandle is stored in the "new"
  584. * filter.
  585. * Every 30 seconds we swap the filters and clear the "new" one,
  586. * unless both are empty of course.
  587. *
  588. * Each filter is 256 bits. We hash the filehandle to 32bit and use the
  589. * low 3 bytes as hash-table indices.
  590. *
  591. * 'blocked_delegations_lock', which is always taken in block_delegations(),
  592. * is used to manage concurrent access. Testing does not need the lock
  593. * except when swapping the two filters.
  594. */
  595. static DEFINE_SPINLOCK(blocked_delegations_lock);
  596. static struct bloom_pair {
  597. int entries, old_entries;
  598. time_t swap_time;
  599. int new; /* index into 'set' */
  600. DECLARE_BITMAP(set[2], 256);
  601. } blocked_delegations;
  602. static int delegation_blocked(struct knfsd_fh *fh)
  603. {
  604. u32 hash;
  605. struct bloom_pair *bd = &blocked_delegations;
  606. if (bd->entries == 0)
  607. return 0;
  608. if (seconds_since_boot() - bd->swap_time > 30) {
  609. spin_lock(&blocked_delegations_lock);
  610. if (seconds_since_boot() - bd->swap_time > 30) {
  611. bd->entries -= bd->old_entries;
  612. bd->old_entries = bd->entries;
  613. memset(bd->set[bd->new], 0,
  614. sizeof(bd->set[0]));
  615. bd->new = 1-bd->new;
  616. bd->swap_time = seconds_since_boot();
  617. }
  618. spin_unlock(&blocked_delegations_lock);
  619. }
  620. hash = jhash(&fh->fh_base, fh->fh_size, 0);
  621. if (test_bit(hash&255, bd->set[0]) &&
  622. test_bit((hash>>8)&255, bd->set[0]) &&
  623. test_bit((hash>>16)&255, bd->set[0]))
  624. return 1;
  625. if (test_bit(hash&255, bd->set[1]) &&
  626. test_bit((hash>>8)&255, bd->set[1]) &&
  627. test_bit((hash>>16)&255, bd->set[1]))
  628. return 1;
  629. return 0;
  630. }
  631. static void block_delegations(struct knfsd_fh *fh)
  632. {
  633. u32 hash;
  634. struct bloom_pair *bd = &blocked_delegations;
  635. hash = jhash(&fh->fh_base, fh->fh_size, 0);
  636. spin_lock(&blocked_delegations_lock);
  637. __set_bit(hash&255, bd->set[bd->new]);
  638. __set_bit((hash>>8)&255, bd->set[bd->new]);
  639. __set_bit((hash>>16)&255, bd->set[bd->new]);
  640. if (bd->entries == 0)
  641. bd->swap_time = seconds_since_boot();
  642. bd->entries += 1;
  643. spin_unlock(&blocked_delegations_lock);
  644. }
  645. static struct nfs4_delegation *
  646. alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
  647. struct nfs4_clnt_odstate *odstate)
  648. {
  649. struct nfs4_delegation *dp;
  650. long n;
  651. dprintk("NFSD alloc_init_deleg\n");
  652. n = atomic_long_inc_return(&num_delegations);
  653. if (n < 0 || n > max_delegations)
  654. goto out_dec;
  655. if (delegation_blocked(&current_fh->fh_handle))
  656. goto out_dec;
  657. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
  658. if (dp == NULL)
  659. goto out_dec;
  660. /*
  661. * delegation seqid's are never incremented. The 4.1 special
  662. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  663. * 0 anyway just for consistency and use 1:
  664. */
  665. dp->dl_stid.sc_stateid.si_generation = 1;
  666. INIT_LIST_HEAD(&dp->dl_perfile);
  667. INIT_LIST_HEAD(&dp->dl_perclnt);
  668. INIT_LIST_HEAD(&dp->dl_recall_lru);
  669. dp->dl_clnt_odstate = odstate;
  670. get_clnt_odstate(odstate);
  671. dp->dl_type = NFS4_OPEN_DELEGATE_READ;
  672. dp->dl_retries = 1;
  673. nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
  674. &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
  675. return dp;
  676. out_dec:
  677. atomic_long_dec(&num_delegations);
  678. return NULL;
  679. }
  680. void
  681. nfs4_put_stid(struct nfs4_stid *s)
  682. {
  683. struct nfs4_file *fp = s->sc_file;
  684. struct nfs4_client *clp = s->sc_client;
  685. might_lock(&clp->cl_lock);
  686. if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
  687. wake_up_all(&close_wq);
  688. return;
  689. }
  690. idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
  691. spin_unlock(&clp->cl_lock);
  692. s->sc_free(s);
  693. if (fp)
  694. put_nfs4_file(fp);
  695. }
  696. void
  697. nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
  698. {
  699. stateid_t *src = &stid->sc_stateid;
  700. spin_lock(&stid->sc_lock);
  701. if (unlikely(++src->si_generation == 0))
  702. src->si_generation = 1;
  703. memcpy(dst, src, sizeof(*dst));
  704. spin_unlock(&stid->sc_lock);
  705. }
  706. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  707. {
  708. struct file *filp = NULL;
  709. spin_lock(&fp->fi_lock);
  710. if (fp->fi_deleg_file && --fp->fi_delegees == 0)
  711. swap(filp, fp->fi_deleg_file);
  712. spin_unlock(&fp->fi_lock);
  713. if (filp) {
  714. vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
  715. fput(filp);
  716. }
  717. }
  718. void nfs4_unhash_stid(struct nfs4_stid *s)
  719. {
  720. s->sc_type = 0;
  721. }
  722. /**
  723. * nfs4_get_existing_delegation - Discover if this delegation already exists
  724. * @clp: a pointer to the nfs4_client we're granting a delegation to
  725. * @fp: a pointer to the nfs4_file we're granting a delegation on
  726. *
  727. * Return:
  728. * On success: NULL if an existing delegation was not found.
  729. *
  730. * On error: -EAGAIN if one was previously granted to this nfs4_client
  731. * for this nfs4_file.
  732. *
  733. */
  734. static int
  735. nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
  736. {
  737. struct nfs4_delegation *searchdp = NULL;
  738. struct nfs4_client *searchclp = NULL;
  739. lockdep_assert_held(&state_lock);
  740. lockdep_assert_held(&fp->fi_lock);
  741. list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
  742. searchclp = searchdp->dl_stid.sc_client;
  743. if (clp == searchclp) {
  744. return -EAGAIN;
  745. }
  746. }
  747. return 0;
  748. }
  749. /**
  750. * hash_delegation_locked - Add a delegation to the appropriate lists
  751. * @dp: a pointer to the nfs4_delegation we are adding.
  752. * @fp: a pointer to the nfs4_file we're granting a delegation on
  753. *
  754. * Return:
  755. * On success: NULL if the delegation was successfully hashed.
  756. *
  757. * On error: -EAGAIN if one was previously granted to this
  758. * nfs4_client for this nfs4_file. Delegation is not hashed.
  759. *
  760. */
  761. static int
  762. hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
  763. {
  764. int status;
  765. struct nfs4_client *clp = dp->dl_stid.sc_client;
  766. lockdep_assert_held(&state_lock);
  767. lockdep_assert_held(&fp->fi_lock);
  768. status = nfs4_get_existing_delegation(clp, fp);
  769. if (status)
  770. return status;
  771. ++fp->fi_delegees;
  772. atomic_inc(&dp->dl_stid.sc_count);
  773. dp->dl_stid.sc_type = NFS4_DELEG_STID;
  774. list_add(&dp->dl_perfile, &fp->fi_delegations);
  775. list_add(&dp->dl_perclnt, &clp->cl_delegations);
  776. return 0;
  777. }
  778. static bool
  779. unhash_delegation_locked(struct nfs4_delegation *dp)
  780. {
  781. struct nfs4_file *fp = dp->dl_stid.sc_file;
  782. lockdep_assert_held(&state_lock);
  783. if (list_empty(&dp->dl_perfile))
  784. return false;
  785. dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
  786. /* Ensure that deleg break won't try to requeue it */
  787. ++dp->dl_time;
  788. spin_lock(&fp->fi_lock);
  789. list_del_init(&dp->dl_perclnt);
  790. list_del_init(&dp->dl_recall_lru);
  791. list_del_init(&dp->dl_perfile);
  792. spin_unlock(&fp->fi_lock);
  793. return true;
  794. }
  795. static void destroy_delegation(struct nfs4_delegation *dp)
  796. {
  797. bool unhashed;
  798. spin_lock(&state_lock);
  799. unhashed = unhash_delegation_locked(dp);
  800. spin_unlock(&state_lock);
  801. if (unhashed) {
  802. put_clnt_odstate(dp->dl_clnt_odstate);
  803. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  804. nfs4_put_stid(&dp->dl_stid);
  805. }
  806. }
  807. static void revoke_delegation(struct nfs4_delegation *dp)
  808. {
  809. struct nfs4_client *clp = dp->dl_stid.sc_client;
  810. WARN_ON(!list_empty(&dp->dl_recall_lru));
  811. put_clnt_odstate(dp->dl_clnt_odstate);
  812. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  813. if (clp->cl_minorversion == 0)
  814. nfs4_put_stid(&dp->dl_stid);
  815. else {
  816. dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
  817. spin_lock(&clp->cl_lock);
  818. list_add(&dp->dl_recall_lru, &clp->cl_revoked);
  819. spin_unlock(&clp->cl_lock);
  820. }
  821. }
  822. /*
  823. * SETCLIENTID state
  824. */
  825. static unsigned int clientid_hashval(u32 id)
  826. {
  827. return id & CLIENT_HASH_MASK;
  828. }
  829. static unsigned int clientstr_hashval(const char *name)
  830. {
  831. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  832. }
  833. /*
  834. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  835. * st_{access,deny}_bmap field of the stateid, in order to track not
  836. * only what share bits are currently in force, but also what
  837. * combinations of share bits previous opens have used. This allows us
  838. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  839. * return an error if the client attempt to downgrade to a combination
  840. * of share bits not explicable by closing some of its previous opens.
  841. *
  842. * XXX: This enforcement is actually incomplete, since we don't keep
  843. * track of access/deny bit combinations; so, e.g., we allow:
  844. *
  845. * OPEN allow read, deny write
  846. * OPEN allow both, deny none
  847. * DOWNGRADE allow read, deny none
  848. *
  849. * which we should reject.
  850. */
  851. static unsigned int
  852. bmap_to_share_mode(unsigned long bmap) {
  853. int i;
  854. unsigned int access = 0;
  855. for (i = 1; i < 4; i++) {
  856. if (test_bit(i, &bmap))
  857. access |= i;
  858. }
  859. return access;
  860. }
  861. /* set share access for a given stateid */
  862. static inline void
  863. set_access(u32 access, struct nfs4_ol_stateid *stp)
  864. {
  865. unsigned char mask = 1 << access;
  866. WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
  867. stp->st_access_bmap |= mask;
  868. }
  869. /* clear share access for a given stateid */
  870. static inline void
  871. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  872. {
  873. unsigned char mask = 1 << access;
  874. WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
  875. stp->st_access_bmap &= ~mask;
  876. }
  877. /* test whether a given stateid has access */
  878. static inline bool
  879. test_access(u32 access, struct nfs4_ol_stateid *stp)
  880. {
  881. unsigned char mask = 1 << access;
  882. return (bool)(stp->st_access_bmap & mask);
  883. }
  884. /* set share deny for a given stateid */
  885. static inline void
  886. set_deny(u32 deny, struct nfs4_ol_stateid *stp)
  887. {
  888. unsigned char mask = 1 << deny;
  889. WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
  890. stp->st_deny_bmap |= mask;
  891. }
  892. /* clear share deny for a given stateid */
  893. static inline void
  894. clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
  895. {
  896. unsigned char mask = 1 << deny;
  897. WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
  898. stp->st_deny_bmap &= ~mask;
  899. }
  900. /* test whether a given stateid is denying specific access */
  901. static inline bool
  902. test_deny(u32 deny, struct nfs4_ol_stateid *stp)
  903. {
  904. unsigned char mask = 1 << deny;
  905. return (bool)(stp->st_deny_bmap & mask);
  906. }
  907. static int nfs4_access_to_omode(u32 access)
  908. {
  909. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  910. case NFS4_SHARE_ACCESS_READ:
  911. return O_RDONLY;
  912. case NFS4_SHARE_ACCESS_WRITE:
  913. return O_WRONLY;
  914. case NFS4_SHARE_ACCESS_BOTH:
  915. return O_RDWR;
  916. }
  917. WARN_ON_ONCE(1);
  918. return O_RDONLY;
  919. }
  920. /*
  921. * A stateid that had a deny mode associated with it is being released
  922. * or downgraded. Recalculate the deny mode on the file.
  923. */
  924. static void
  925. recalculate_deny_mode(struct nfs4_file *fp)
  926. {
  927. struct nfs4_ol_stateid *stp;
  928. spin_lock(&fp->fi_lock);
  929. fp->fi_share_deny = 0;
  930. list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
  931. fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
  932. spin_unlock(&fp->fi_lock);
  933. }
  934. static void
  935. reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
  936. {
  937. int i;
  938. bool change = false;
  939. for (i = 1; i < 4; i++) {
  940. if ((i & deny) != i) {
  941. change = true;
  942. clear_deny(i, stp);
  943. }
  944. }
  945. /* Recalculate per-file deny mode if there was a change */
  946. if (change)
  947. recalculate_deny_mode(stp->st_stid.sc_file);
  948. }
  949. /* release all access and file references for a given stateid */
  950. static void
  951. release_all_access(struct nfs4_ol_stateid *stp)
  952. {
  953. int i;
  954. struct nfs4_file *fp = stp->st_stid.sc_file;
  955. if (fp && stp->st_deny_bmap != 0)
  956. recalculate_deny_mode(fp);
  957. for (i = 1; i < 4; i++) {
  958. if (test_access(i, stp))
  959. nfs4_file_put_access(stp->st_stid.sc_file, i);
  960. clear_access(i, stp);
  961. }
  962. }
  963. static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
  964. {
  965. kfree(sop->so_owner.data);
  966. sop->so_ops->so_free(sop);
  967. }
  968. static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
  969. {
  970. struct nfs4_client *clp = sop->so_client;
  971. might_lock(&clp->cl_lock);
  972. if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
  973. return;
  974. sop->so_ops->so_unhash(sop);
  975. spin_unlock(&clp->cl_lock);
  976. nfs4_free_stateowner(sop);
  977. }
  978. static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
  979. {
  980. struct nfs4_file *fp = stp->st_stid.sc_file;
  981. lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
  982. if (list_empty(&stp->st_perfile))
  983. return false;
  984. spin_lock(&fp->fi_lock);
  985. list_del_init(&stp->st_perfile);
  986. spin_unlock(&fp->fi_lock);
  987. list_del(&stp->st_perstateowner);
  988. return true;
  989. }
  990. static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
  991. {
  992. struct nfs4_ol_stateid *stp = openlockstateid(stid);
  993. put_clnt_odstate(stp->st_clnt_odstate);
  994. release_all_access(stp);
  995. if (stp->st_stateowner)
  996. nfs4_put_stateowner(stp->st_stateowner);
  997. kmem_cache_free(stateid_slab, stid);
  998. }
  999. static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
  1000. {
  1001. struct nfs4_ol_stateid *stp = openlockstateid(stid);
  1002. struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
  1003. struct file *file;
  1004. file = find_any_file(stp->st_stid.sc_file);
  1005. if (file)
  1006. filp_close(file, (fl_owner_t)lo);
  1007. nfs4_free_ol_stateid(stid);
  1008. }
  1009. /*
  1010. * Put the persistent reference to an already unhashed generic stateid, while
  1011. * holding the cl_lock. If it's the last reference, then put it onto the
  1012. * reaplist for later destruction.
  1013. */
  1014. static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
  1015. struct list_head *reaplist)
  1016. {
  1017. struct nfs4_stid *s = &stp->st_stid;
  1018. struct nfs4_client *clp = s->sc_client;
  1019. lockdep_assert_held(&clp->cl_lock);
  1020. WARN_ON_ONCE(!list_empty(&stp->st_locks));
  1021. if (!atomic_dec_and_test(&s->sc_count)) {
  1022. wake_up_all(&close_wq);
  1023. return;
  1024. }
  1025. idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
  1026. list_add(&stp->st_locks, reaplist);
  1027. }
  1028. static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
  1029. {
  1030. lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
  1031. list_del_init(&stp->st_locks);
  1032. nfs4_unhash_stid(&stp->st_stid);
  1033. return unhash_ol_stateid(stp);
  1034. }
  1035. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  1036. {
  1037. struct nfs4_client *clp = stp->st_stid.sc_client;
  1038. bool unhashed;
  1039. spin_lock(&clp->cl_lock);
  1040. unhashed = unhash_lock_stateid(stp);
  1041. spin_unlock(&clp->cl_lock);
  1042. if (unhashed)
  1043. nfs4_put_stid(&stp->st_stid);
  1044. }
  1045. static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
  1046. {
  1047. struct nfs4_client *clp = lo->lo_owner.so_client;
  1048. lockdep_assert_held(&clp->cl_lock);
  1049. list_del_init(&lo->lo_owner.so_strhash);
  1050. }
  1051. /*
  1052. * Free a list of generic stateids that were collected earlier after being
  1053. * fully unhashed.
  1054. */
  1055. static void
  1056. free_ol_stateid_reaplist(struct list_head *reaplist)
  1057. {
  1058. struct nfs4_ol_stateid *stp;
  1059. struct nfs4_file *fp;
  1060. might_sleep();
  1061. while (!list_empty(reaplist)) {
  1062. stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
  1063. st_locks);
  1064. list_del(&stp->st_locks);
  1065. fp = stp->st_stid.sc_file;
  1066. stp->st_stid.sc_free(&stp->st_stid);
  1067. if (fp)
  1068. put_nfs4_file(fp);
  1069. }
  1070. }
  1071. static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
  1072. struct list_head *reaplist)
  1073. {
  1074. struct nfs4_ol_stateid *stp;
  1075. lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
  1076. while (!list_empty(&open_stp->st_locks)) {
  1077. stp = list_entry(open_stp->st_locks.next,
  1078. struct nfs4_ol_stateid, st_locks);
  1079. WARN_ON(!unhash_lock_stateid(stp));
  1080. put_ol_stateid_locked(stp, reaplist);
  1081. }
  1082. }
  1083. static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
  1084. struct list_head *reaplist)
  1085. {
  1086. bool unhashed;
  1087. lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
  1088. unhashed = unhash_ol_stateid(stp);
  1089. release_open_stateid_locks(stp, reaplist);
  1090. return unhashed;
  1091. }
  1092. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  1093. {
  1094. LIST_HEAD(reaplist);
  1095. spin_lock(&stp->st_stid.sc_client->cl_lock);
  1096. if (unhash_open_stateid(stp, &reaplist))
  1097. put_ol_stateid_locked(stp, &reaplist);
  1098. spin_unlock(&stp->st_stid.sc_client->cl_lock);
  1099. free_ol_stateid_reaplist(&reaplist);
  1100. }
  1101. static void unhash_openowner_locked(struct nfs4_openowner *oo)
  1102. {
  1103. struct nfs4_client *clp = oo->oo_owner.so_client;
  1104. lockdep_assert_held(&clp->cl_lock);
  1105. list_del_init(&oo->oo_owner.so_strhash);
  1106. list_del_init(&oo->oo_perclient);
  1107. }
  1108. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  1109. {
  1110. struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
  1111. nfsd_net_id);
  1112. struct nfs4_ol_stateid *s;
  1113. spin_lock(&nn->client_lock);
  1114. s = oo->oo_last_closed_stid;
  1115. if (s) {
  1116. list_del_init(&oo->oo_close_lru);
  1117. oo->oo_last_closed_stid = NULL;
  1118. }
  1119. spin_unlock(&nn->client_lock);
  1120. if (s)
  1121. nfs4_put_stid(&s->st_stid);
  1122. }
  1123. static void release_openowner(struct nfs4_openowner *oo)
  1124. {
  1125. struct nfs4_ol_stateid *stp;
  1126. struct nfs4_client *clp = oo->oo_owner.so_client;
  1127. struct list_head reaplist;
  1128. INIT_LIST_HEAD(&reaplist);
  1129. spin_lock(&clp->cl_lock);
  1130. unhash_openowner_locked(oo);
  1131. while (!list_empty(&oo->oo_owner.so_stateids)) {
  1132. stp = list_first_entry(&oo->oo_owner.so_stateids,
  1133. struct nfs4_ol_stateid, st_perstateowner);
  1134. if (unhash_open_stateid(stp, &reaplist))
  1135. put_ol_stateid_locked(stp, &reaplist);
  1136. }
  1137. spin_unlock(&clp->cl_lock);
  1138. free_ol_stateid_reaplist(&reaplist);
  1139. release_last_closed_stateid(oo);
  1140. nfs4_put_stateowner(&oo->oo_owner);
  1141. }
  1142. static inline int
  1143. hash_sessionid(struct nfs4_sessionid *sessionid)
  1144. {
  1145. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  1146. return sid->sequence % SESSION_HASH_SIZE;
  1147. }
  1148. #ifdef CONFIG_SUNRPC_DEBUG
  1149. static inline void
  1150. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  1151. {
  1152. u32 *ptr = (u32 *)(&sessionid->data[0]);
  1153. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  1154. }
  1155. #else
  1156. static inline void
  1157. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  1158. {
  1159. }
  1160. #endif
  1161. /*
  1162. * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
  1163. * won't be used for replay.
  1164. */
  1165. void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
  1166. {
  1167. struct nfs4_stateowner *so = cstate->replay_owner;
  1168. if (nfserr == nfserr_replay_me)
  1169. return;
  1170. if (!seqid_mutating_err(ntohl(nfserr))) {
  1171. nfsd4_cstate_clear_replay(cstate);
  1172. return;
  1173. }
  1174. if (!so)
  1175. return;
  1176. if (so->so_is_open_owner)
  1177. release_last_closed_stateid(openowner(so));
  1178. so->so_seqid++;
  1179. return;
  1180. }
  1181. static void
  1182. gen_sessionid(struct nfsd4_session *ses)
  1183. {
  1184. struct nfs4_client *clp = ses->se_client;
  1185. struct nfsd4_sessionid *sid;
  1186. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  1187. sid->clientid = clp->cl_clientid;
  1188. sid->sequence = current_sessionid++;
  1189. sid->reserved = 0;
  1190. }
  1191. /*
  1192. * The protocol defines ca_maxresponssize_cached to include the size of
  1193. * the rpc header, but all we need to cache is the data starting after
  1194. * the end of the initial SEQUENCE operation--the rest we regenerate
  1195. * each time. Therefore we can advertise a ca_maxresponssize_cached
  1196. * value that is the number of bytes in our cache plus a few additional
  1197. * bytes. In order to stay on the safe side, and not promise more than
  1198. * we can cache, those additional bytes must be the minimum possible: 24
  1199. * bytes of rpc header (xid through accept state, with AUTH_NULL
  1200. * verifier), 12 for the compound header (with zero-length tag), and 44
  1201. * for the SEQUENCE op response:
  1202. */
  1203. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  1204. static void
  1205. free_session_slots(struct nfsd4_session *ses)
  1206. {
  1207. int i;
  1208. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  1209. kfree(ses->se_slots[i]);
  1210. }
  1211. /*
  1212. * We don't actually need to cache the rpc and session headers, so we
  1213. * can allocate a little less for each slot:
  1214. */
  1215. static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
  1216. {
  1217. u32 size;
  1218. if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
  1219. size = 0;
  1220. else
  1221. size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  1222. return size + sizeof(struct nfsd4_slot);
  1223. }
  1224. /*
  1225. * XXX: If we run out of reserved DRC memory we could (up to a point)
  1226. * re-negotiate active sessions and reduce their slot usage to make
  1227. * room for new connections. For now we just fail the create session.
  1228. */
  1229. static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
  1230. {
  1231. u32 slotsize = slot_bytes(ca);
  1232. u32 num = ca->maxreqs;
  1233. int avail;
  1234. spin_lock(&nfsd_drc_lock);
  1235. avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
  1236. nfsd_drc_max_mem - nfsd_drc_mem_used);
  1237. num = min_t(int, num, avail / slotsize);
  1238. nfsd_drc_mem_used += num * slotsize;
  1239. spin_unlock(&nfsd_drc_lock);
  1240. return num;
  1241. }
  1242. static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
  1243. {
  1244. int slotsize = slot_bytes(ca);
  1245. spin_lock(&nfsd_drc_lock);
  1246. nfsd_drc_mem_used -= slotsize * ca->maxreqs;
  1247. spin_unlock(&nfsd_drc_lock);
  1248. }
  1249. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
  1250. struct nfsd4_channel_attrs *battrs)
  1251. {
  1252. int numslots = fattrs->maxreqs;
  1253. int slotsize = slot_bytes(fattrs);
  1254. struct nfsd4_session *new;
  1255. int mem, i;
  1256. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  1257. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  1258. mem = numslots * sizeof(struct nfsd4_slot *);
  1259. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  1260. if (!new)
  1261. return NULL;
  1262. /* allocate each struct nfsd4_slot and data cache in one piece */
  1263. for (i = 0; i < numslots; i++) {
  1264. new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
  1265. if (!new->se_slots[i])
  1266. goto out_free;
  1267. }
  1268. memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
  1269. memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
  1270. return new;
  1271. out_free:
  1272. while (i--)
  1273. kfree(new->se_slots[i]);
  1274. kfree(new);
  1275. return NULL;
  1276. }
  1277. static void free_conn(struct nfsd4_conn *c)
  1278. {
  1279. svc_xprt_put(c->cn_xprt);
  1280. kfree(c);
  1281. }
  1282. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  1283. {
  1284. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  1285. struct nfs4_client *clp = c->cn_session->se_client;
  1286. spin_lock(&clp->cl_lock);
  1287. if (!list_empty(&c->cn_persession)) {
  1288. list_del(&c->cn_persession);
  1289. free_conn(c);
  1290. }
  1291. nfsd4_probe_callback(clp);
  1292. spin_unlock(&clp->cl_lock);
  1293. }
  1294. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  1295. {
  1296. struct nfsd4_conn *conn;
  1297. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  1298. if (!conn)
  1299. return NULL;
  1300. svc_xprt_get(rqstp->rq_xprt);
  1301. conn->cn_xprt = rqstp->rq_xprt;
  1302. conn->cn_flags = flags;
  1303. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  1304. return conn;
  1305. }
  1306. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1307. {
  1308. conn->cn_session = ses;
  1309. list_add(&conn->cn_persession, &ses->se_conns);
  1310. }
  1311. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1312. {
  1313. struct nfs4_client *clp = ses->se_client;
  1314. spin_lock(&clp->cl_lock);
  1315. __nfsd4_hash_conn(conn, ses);
  1316. spin_unlock(&clp->cl_lock);
  1317. }
  1318. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  1319. {
  1320. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  1321. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  1322. }
  1323. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1324. {
  1325. int ret;
  1326. nfsd4_hash_conn(conn, ses);
  1327. ret = nfsd4_register_conn(conn);
  1328. if (ret)
  1329. /* oops; xprt is already down: */
  1330. nfsd4_conn_lost(&conn->cn_xpt_user);
  1331. /* We may have gained or lost a callback channel: */
  1332. nfsd4_probe_callback_sync(ses->se_client);
  1333. }
  1334. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  1335. {
  1336. u32 dir = NFS4_CDFC4_FORE;
  1337. if (cses->flags & SESSION4_BACK_CHAN)
  1338. dir |= NFS4_CDFC4_BACK;
  1339. return alloc_conn(rqstp, dir);
  1340. }
  1341. /* must be called under client_lock */
  1342. static void nfsd4_del_conns(struct nfsd4_session *s)
  1343. {
  1344. struct nfs4_client *clp = s->se_client;
  1345. struct nfsd4_conn *c;
  1346. spin_lock(&clp->cl_lock);
  1347. while (!list_empty(&s->se_conns)) {
  1348. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  1349. list_del_init(&c->cn_persession);
  1350. spin_unlock(&clp->cl_lock);
  1351. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  1352. free_conn(c);
  1353. spin_lock(&clp->cl_lock);
  1354. }
  1355. spin_unlock(&clp->cl_lock);
  1356. }
  1357. static void __free_session(struct nfsd4_session *ses)
  1358. {
  1359. free_session_slots(ses);
  1360. kfree(ses);
  1361. }
  1362. static void free_session(struct nfsd4_session *ses)
  1363. {
  1364. nfsd4_del_conns(ses);
  1365. nfsd4_put_drc_mem(&ses->se_fchannel);
  1366. __free_session(ses);
  1367. }
  1368. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  1369. {
  1370. int idx;
  1371. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1372. new->se_client = clp;
  1373. gen_sessionid(new);
  1374. INIT_LIST_HEAD(&new->se_conns);
  1375. new->se_cb_seq_nr = 1;
  1376. new->se_flags = cses->flags;
  1377. new->se_cb_prog = cses->callback_prog;
  1378. new->se_cb_sec = cses->cb_sec;
  1379. atomic_set(&new->se_ref, 0);
  1380. idx = hash_sessionid(&new->se_sessionid);
  1381. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  1382. spin_lock(&clp->cl_lock);
  1383. list_add(&new->se_perclnt, &clp->cl_sessions);
  1384. spin_unlock(&clp->cl_lock);
  1385. {
  1386. struct sockaddr *sa = svc_addr(rqstp);
  1387. /*
  1388. * This is a little silly; with sessions there's no real
  1389. * use for the callback address. Use the peer address
  1390. * as a reasonable default for now, but consider fixing
  1391. * the rpc client not to require an address in the
  1392. * future:
  1393. */
  1394. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  1395. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  1396. }
  1397. }
  1398. /* caller must hold client_lock */
  1399. static struct nfsd4_session *
  1400. __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  1401. {
  1402. struct nfsd4_session *elem;
  1403. int idx;
  1404. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1405. lockdep_assert_held(&nn->client_lock);
  1406. dump_sessionid(__func__, sessionid);
  1407. idx = hash_sessionid(sessionid);
  1408. /* Search in the appropriate list */
  1409. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  1410. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  1411. NFS4_MAX_SESSIONID_LEN)) {
  1412. return elem;
  1413. }
  1414. }
  1415. dprintk("%s: session not found\n", __func__);
  1416. return NULL;
  1417. }
  1418. static struct nfsd4_session *
  1419. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
  1420. __be32 *ret)
  1421. {
  1422. struct nfsd4_session *session;
  1423. __be32 status = nfserr_badsession;
  1424. session = __find_in_sessionid_hashtbl(sessionid, net);
  1425. if (!session)
  1426. goto out;
  1427. status = nfsd4_get_session_locked(session);
  1428. if (status)
  1429. session = NULL;
  1430. out:
  1431. *ret = status;
  1432. return session;
  1433. }
  1434. /* caller must hold client_lock */
  1435. static void
  1436. unhash_session(struct nfsd4_session *ses)
  1437. {
  1438. struct nfs4_client *clp = ses->se_client;
  1439. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1440. lockdep_assert_held(&nn->client_lock);
  1441. list_del(&ses->se_hash);
  1442. spin_lock(&ses->se_client->cl_lock);
  1443. list_del(&ses->se_perclnt);
  1444. spin_unlock(&ses->se_client->cl_lock);
  1445. }
  1446. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  1447. static int
  1448. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  1449. {
  1450. /*
  1451. * We're assuming the clid was not given out from a boot
  1452. * precisely 2^32 (about 136 years) before this one. That seems
  1453. * a safe assumption:
  1454. */
  1455. if (clid->cl_boot == (u32)nn->boot_time)
  1456. return 0;
  1457. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  1458. clid->cl_boot, clid->cl_id, nn->boot_time);
  1459. return 1;
  1460. }
  1461. /*
  1462. * XXX Should we use a slab cache ?
  1463. * This type of memory management is somewhat inefficient, but we use it
  1464. * anyway since SETCLIENTID is not a common operation.
  1465. */
  1466. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  1467. {
  1468. struct nfs4_client *clp;
  1469. int i;
  1470. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  1471. if (clp == NULL)
  1472. return NULL;
  1473. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  1474. if (clp->cl_name.data == NULL)
  1475. goto err_no_name;
  1476. clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  1477. OWNER_HASH_SIZE, GFP_KERNEL);
  1478. if (!clp->cl_ownerstr_hashtbl)
  1479. goto err_no_hashtbl;
  1480. for (i = 0; i < OWNER_HASH_SIZE; i++)
  1481. INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
  1482. clp->cl_name.len = name.len;
  1483. INIT_LIST_HEAD(&clp->cl_sessions);
  1484. idr_init(&clp->cl_stateids);
  1485. atomic_set(&clp->cl_refcount, 0);
  1486. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1487. INIT_LIST_HEAD(&clp->cl_idhash);
  1488. INIT_LIST_HEAD(&clp->cl_openowners);
  1489. INIT_LIST_HEAD(&clp->cl_delegations);
  1490. INIT_LIST_HEAD(&clp->cl_lru);
  1491. INIT_LIST_HEAD(&clp->cl_revoked);
  1492. #ifdef CONFIG_NFSD_PNFS
  1493. INIT_LIST_HEAD(&clp->cl_lo_states);
  1494. #endif
  1495. spin_lock_init(&clp->cl_lock);
  1496. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1497. return clp;
  1498. err_no_hashtbl:
  1499. kfree(clp->cl_name.data);
  1500. err_no_name:
  1501. kfree(clp);
  1502. return NULL;
  1503. }
  1504. static void
  1505. free_client(struct nfs4_client *clp)
  1506. {
  1507. while (!list_empty(&clp->cl_sessions)) {
  1508. struct nfsd4_session *ses;
  1509. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  1510. se_perclnt);
  1511. list_del(&ses->se_perclnt);
  1512. WARN_ON_ONCE(atomic_read(&ses->se_ref));
  1513. free_session(ses);
  1514. }
  1515. rpc_destroy_wait_queue(&clp->cl_cb_waitq);
  1516. free_svc_cred(&clp->cl_cred);
  1517. kfree(clp->cl_ownerstr_hashtbl);
  1518. kfree(clp->cl_name.data);
  1519. idr_destroy(&clp->cl_stateids);
  1520. kfree(clp);
  1521. }
  1522. /* must be called under the client_lock */
  1523. static void
  1524. unhash_client_locked(struct nfs4_client *clp)
  1525. {
  1526. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1527. struct nfsd4_session *ses;
  1528. lockdep_assert_held(&nn->client_lock);
  1529. /* Mark the client as expired! */
  1530. clp->cl_time = 0;
  1531. /* Make it invisible */
  1532. if (!list_empty(&clp->cl_idhash)) {
  1533. list_del_init(&clp->cl_idhash);
  1534. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  1535. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  1536. else
  1537. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1538. }
  1539. list_del_init(&clp->cl_lru);
  1540. spin_lock(&clp->cl_lock);
  1541. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  1542. list_del_init(&ses->se_hash);
  1543. spin_unlock(&clp->cl_lock);
  1544. }
  1545. static void
  1546. unhash_client(struct nfs4_client *clp)
  1547. {
  1548. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1549. spin_lock(&nn->client_lock);
  1550. unhash_client_locked(clp);
  1551. spin_unlock(&nn->client_lock);
  1552. }
  1553. static __be32 mark_client_expired_locked(struct nfs4_client *clp)
  1554. {
  1555. if (atomic_read(&clp->cl_refcount))
  1556. return nfserr_jukebox;
  1557. unhash_client_locked(clp);
  1558. return nfs_ok;
  1559. }
  1560. static void
  1561. __destroy_client(struct nfs4_client *clp)
  1562. {
  1563. struct nfs4_openowner *oo;
  1564. struct nfs4_delegation *dp;
  1565. struct list_head reaplist;
  1566. INIT_LIST_HEAD(&reaplist);
  1567. spin_lock(&state_lock);
  1568. while (!list_empty(&clp->cl_delegations)) {
  1569. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  1570. WARN_ON(!unhash_delegation_locked(dp));
  1571. list_add(&dp->dl_recall_lru, &reaplist);
  1572. }
  1573. spin_unlock(&state_lock);
  1574. while (!list_empty(&reaplist)) {
  1575. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  1576. list_del_init(&dp->dl_recall_lru);
  1577. put_clnt_odstate(dp->dl_clnt_odstate);
  1578. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  1579. nfs4_put_stid(&dp->dl_stid);
  1580. }
  1581. while (!list_empty(&clp->cl_revoked)) {
  1582. dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
  1583. list_del_init(&dp->dl_recall_lru);
  1584. nfs4_put_stid(&dp->dl_stid);
  1585. }
  1586. while (!list_empty(&clp->cl_openowners)) {
  1587. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  1588. nfs4_get_stateowner(&oo->oo_owner);
  1589. release_openowner(oo);
  1590. }
  1591. nfsd4_return_all_client_layouts(clp);
  1592. nfsd4_shutdown_callback(clp);
  1593. if (clp->cl_cb_conn.cb_xprt)
  1594. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  1595. free_client(clp);
  1596. }
  1597. static void
  1598. destroy_client(struct nfs4_client *clp)
  1599. {
  1600. unhash_client(clp);
  1601. __destroy_client(clp);
  1602. }
  1603. static void expire_client(struct nfs4_client *clp)
  1604. {
  1605. unhash_client(clp);
  1606. nfsd4_client_record_remove(clp);
  1607. __destroy_client(clp);
  1608. }
  1609. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  1610. {
  1611. memcpy(target->cl_verifier.data, source->data,
  1612. sizeof(target->cl_verifier.data));
  1613. }
  1614. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  1615. {
  1616. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  1617. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  1618. }
  1619. int strdup_if_nonnull(char **target, char *source)
  1620. {
  1621. if (source) {
  1622. *target = kstrdup(source, GFP_KERNEL);
  1623. if (!*target)
  1624. return -ENOMEM;
  1625. } else
  1626. *target = NULL;
  1627. return 0;
  1628. }
  1629. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  1630. {
  1631. int ret;
  1632. ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
  1633. if (ret)
  1634. return ret;
  1635. ret = strdup_if_nonnull(&target->cr_raw_principal,
  1636. source->cr_raw_principal);
  1637. if (ret)
  1638. return ret;
  1639. target->cr_flavor = source->cr_flavor;
  1640. target->cr_uid = source->cr_uid;
  1641. target->cr_gid = source->cr_gid;
  1642. target->cr_group_info = source->cr_group_info;
  1643. get_group_info(target->cr_group_info);
  1644. target->cr_gss_mech = source->cr_gss_mech;
  1645. if (source->cr_gss_mech)
  1646. gss_mech_get(source->cr_gss_mech);
  1647. return 0;
  1648. }
  1649. static int
  1650. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1651. {
  1652. if (o1->len < o2->len)
  1653. return -1;
  1654. if (o1->len > o2->len)
  1655. return 1;
  1656. return memcmp(o1->data, o2->data, o1->len);
  1657. }
  1658. static int same_name(const char *n1, const char *n2)
  1659. {
  1660. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1661. }
  1662. static int
  1663. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1664. {
  1665. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1666. }
  1667. static int
  1668. same_clid(clientid_t *cl1, clientid_t *cl2)
  1669. {
  1670. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1671. }
  1672. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1673. {
  1674. int i;
  1675. if (g1->ngroups != g2->ngroups)
  1676. return false;
  1677. for (i=0; i<g1->ngroups; i++)
  1678. if (!gid_eq(g1->gid[i], g2->gid[i]))
  1679. return false;
  1680. return true;
  1681. }
  1682. /*
  1683. * RFC 3530 language requires clid_inuse be returned when the
  1684. * "principal" associated with a requests differs from that previously
  1685. * used. We use uid, gid's, and gss principal string as our best
  1686. * approximation. We also don't want to allow non-gss use of a client
  1687. * established using gss: in theory cr_principal should catch that
  1688. * change, but in practice cr_principal can be null even in the gss case
  1689. * since gssd doesn't always pass down a principal string.
  1690. */
  1691. static bool is_gss_cred(struct svc_cred *cr)
  1692. {
  1693. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1694. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1695. }
  1696. static bool
  1697. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1698. {
  1699. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1700. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  1701. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  1702. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1703. return false;
  1704. if (cr1->cr_principal == cr2->cr_principal)
  1705. return true;
  1706. if (!cr1->cr_principal || !cr2->cr_principal)
  1707. return false;
  1708. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1709. }
  1710. static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
  1711. {
  1712. struct svc_cred *cr = &rqstp->rq_cred;
  1713. u32 service;
  1714. if (!cr->cr_gss_mech)
  1715. return false;
  1716. service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
  1717. return service == RPC_GSS_SVC_INTEGRITY ||
  1718. service == RPC_GSS_SVC_PRIVACY;
  1719. }
  1720. bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
  1721. {
  1722. struct svc_cred *cr = &rqstp->rq_cred;
  1723. if (!cl->cl_mach_cred)
  1724. return true;
  1725. if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
  1726. return false;
  1727. if (!svc_rqst_integrity_protected(rqstp))
  1728. return false;
  1729. if (cl->cl_cred.cr_raw_principal)
  1730. return 0 == strcmp(cl->cl_cred.cr_raw_principal,
  1731. cr->cr_raw_principal);
  1732. if (!cr->cr_principal)
  1733. return false;
  1734. return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
  1735. }
  1736. static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
  1737. {
  1738. __be32 verf[2];
  1739. /*
  1740. * This is opaque to client, so no need to byte-swap. Use
  1741. * __force to keep sparse happy
  1742. */
  1743. verf[0] = (__force __be32)get_seconds();
  1744. verf[1] = (__force __be32)nn->clverifier_counter++;
  1745. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1746. }
  1747. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1748. {
  1749. clp->cl_clientid.cl_boot = nn->boot_time;
  1750. clp->cl_clientid.cl_id = nn->clientid_counter++;
  1751. gen_confirm(clp, nn);
  1752. }
  1753. static struct nfs4_stid *
  1754. find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
  1755. {
  1756. struct nfs4_stid *ret;
  1757. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1758. if (!ret || !ret->sc_type)
  1759. return NULL;
  1760. return ret;
  1761. }
  1762. static struct nfs4_stid *
  1763. find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1764. {
  1765. struct nfs4_stid *s;
  1766. spin_lock(&cl->cl_lock);
  1767. s = find_stateid_locked(cl, t);
  1768. if (s != NULL) {
  1769. if (typemask & s->sc_type)
  1770. atomic_inc(&s->sc_count);
  1771. else
  1772. s = NULL;
  1773. }
  1774. spin_unlock(&cl->cl_lock);
  1775. return s;
  1776. }
  1777. static struct nfs4_client *create_client(struct xdr_netobj name,
  1778. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1779. {
  1780. struct nfs4_client *clp;
  1781. struct sockaddr *sa = svc_addr(rqstp);
  1782. int ret;
  1783. struct net *net = SVC_NET(rqstp);
  1784. clp = alloc_client(name);
  1785. if (clp == NULL)
  1786. return NULL;
  1787. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1788. if (ret) {
  1789. free_client(clp);
  1790. return NULL;
  1791. }
  1792. nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
  1793. clp->cl_time = get_seconds();
  1794. clear_bit(0, &clp->cl_cb_slot_busy);
  1795. copy_verf(clp, verf);
  1796. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1797. clp->cl_cb_session = NULL;
  1798. clp->net = net;
  1799. return clp;
  1800. }
  1801. static void
  1802. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1803. {
  1804. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1805. struct nfs4_client *clp;
  1806. while (*new) {
  1807. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1808. parent = *new;
  1809. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1810. new = &((*new)->rb_left);
  1811. else
  1812. new = &((*new)->rb_right);
  1813. }
  1814. rb_link_node(&new_clp->cl_namenode, parent, new);
  1815. rb_insert_color(&new_clp->cl_namenode, root);
  1816. }
  1817. static struct nfs4_client *
  1818. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1819. {
  1820. int cmp;
  1821. struct rb_node *node = root->rb_node;
  1822. struct nfs4_client *clp;
  1823. while (node) {
  1824. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1825. cmp = compare_blob(&clp->cl_name, name);
  1826. if (cmp > 0)
  1827. node = node->rb_left;
  1828. else if (cmp < 0)
  1829. node = node->rb_right;
  1830. else
  1831. return clp;
  1832. }
  1833. return NULL;
  1834. }
  1835. static void
  1836. add_to_unconfirmed(struct nfs4_client *clp)
  1837. {
  1838. unsigned int idhashval;
  1839. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1840. lockdep_assert_held(&nn->client_lock);
  1841. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1842. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1843. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1844. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1845. renew_client_locked(clp);
  1846. }
  1847. static void
  1848. move_to_confirmed(struct nfs4_client *clp)
  1849. {
  1850. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1851. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1852. lockdep_assert_held(&nn->client_lock);
  1853. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1854. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1855. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1856. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1857. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1858. renew_client_locked(clp);
  1859. }
  1860. static struct nfs4_client *
  1861. find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
  1862. {
  1863. struct nfs4_client *clp;
  1864. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1865. list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
  1866. if (same_clid(&clp->cl_clientid, clid)) {
  1867. if ((bool)clp->cl_minorversion != sessions)
  1868. return NULL;
  1869. renew_client_locked(clp);
  1870. return clp;
  1871. }
  1872. }
  1873. return NULL;
  1874. }
  1875. static struct nfs4_client *
  1876. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1877. {
  1878. struct list_head *tbl = nn->conf_id_hashtbl;
  1879. lockdep_assert_held(&nn->client_lock);
  1880. return find_client_in_id_table(tbl, clid, sessions);
  1881. }
  1882. static struct nfs4_client *
  1883. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1884. {
  1885. struct list_head *tbl = nn->unconf_id_hashtbl;
  1886. lockdep_assert_held(&nn->client_lock);
  1887. return find_client_in_id_table(tbl, clid, sessions);
  1888. }
  1889. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1890. {
  1891. return clp->cl_exchange_flags != 0;
  1892. }
  1893. static struct nfs4_client *
  1894. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1895. {
  1896. lockdep_assert_held(&nn->client_lock);
  1897. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1898. }
  1899. static struct nfs4_client *
  1900. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1901. {
  1902. lockdep_assert_held(&nn->client_lock);
  1903. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1904. }
  1905. static void
  1906. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1907. {
  1908. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1909. struct sockaddr *sa = svc_addr(rqstp);
  1910. u32 scopeid = rpc_get_scope_id(sa);
  1911. unsigned short expected_family;
  1912. /* Currently, we only support tcp and tcp6 for the callback channel */
  1913. if (se->se_callback_netid_len == 3 &&
  1914. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1915. expected_family = AF_INET;
  1916. else if (se->se_callback_netid_len == 4 &&
  1917. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1918. expected_family = AF_INET6;
  1919. else
  1920. goto out_err;
  1921. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1922. se->se_callback_addr_len,
  1923. (struct sockaddr *)&conn->cb_addr,
  1924. sizeof(conn->cb_addr));
  1925. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1926. goto out_err;
  1927. if (conn->cb_addr.ss_family == AF_INET6)
  1928. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1929. conn->cb_prog = se->se_callback_prog;
  1930. conn->cb_ident = se->se_callback_ident;
  1931. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1932. return;
  1933. out_err:
  1934. conn->cb_addr.ss_family = AF_UNSPEC;
  1935. conn->cb_addrlen = 0;
  1936. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1937. "will not receive delegations\n",
  1938. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1939. return;
  1940. }
  1941. /*
  1942. * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
  1943. */
  1944. static void
  1945. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1946. {
  1947. struct xdr_buf *buf = resp->xdr.buf;
  1948. struct nfsd4_slot *slot = resp->cstate.slot;
  1949. unsigned int base;
  1950. dprintk("--> %s slot %p\n", __func__, slot);
  1951. slot->sl_opcnt = resp->opcnt;
  1952. slot->sl_status = resp->cstate.status;
  1953. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1954. if (nfsd4_not_cached(resp)) {
  1955. slot->sl_datalen = 0;
  1956. return;
  1957. }
  1958. base = resp->cstate.data_offset;
  1959. slot->sl_datalen = buf->len - base;
  1960. if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
  1961. WARN(1, "%s: sessions DRC could not cache compound\n",
  1962. __func__);
  1963. return;
  1964. }
  1965. /*
  1966. * Encode the replay sequence operation from the slot values.
  1967. * If cachethis is FALSE encode the uncached rep error on the next
  1968. * operation which sets resp->p and increments resp->opcnt for
  1969. * nfs4svc_encode_compoundres.
  1970. *
  1971. */
  1972. static __be32
  1973. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1974. struct nfsd4_compoundres *resp)
  1975. {
  1976. struct nfsd4_op *op;
  1977. struct nfsd4_slot *slot = resp->cstate.slot;
  1978. /* Encode the replayed sequence operation */
  1979. op = &args->ops[resp->opcnt - 1];
  1980. nfsd4_encode_operation(resp, op);
  1981. /* Return nfserr_retry_uncached_rep in next operation. */
  1982. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1983. op = &args->ops[resp->opcnt++];
  1984. op->status = nfserr_retry_uncached_rep;
  1985. nfsd4_encode_operation(resp, op);
  1986. }
  1987. return op->status;
  1988. }
  1989. /*
  1990. * The sequence operation is not cached because we can use the slot and
  1991. * session values.
  1992. */
  1993. static __be32
  1994. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1995. struct nfsd4_sequence *seq)
  1996. {
  1997. struct nfsd4_slot *slot = resp->cstate.slot;
  1998. struct xdr_stream *xdr = &resp->xdr;
  1999. __be32 *p;
  2000. __be32 status;
  2001. dprintk("--> %s slot %p\n", __func__, slot);
  2002. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  2003. if (status)
  2004. return status;
  2005. p = xdr_reserve_space(xdr, slot->sl_datalen);
  2006. if (!p) {
  2007. WARN_ON_ONCE(1);
  2008. return nfserr_serverfault;
  2009. }
  2010. xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
  2011. xdr_commit_encode(xdr);
  2012. resp->opcnt = slot->sl_opcnt;
  2013. return slot->sl_status;
  2014. }
  2015. /*
  2016. * Set the exchange_id flags returned by the server.
  2017. */
  2018. static void
  2019. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  2020. {
  2021. #ifdef CONFIG_NFSD_PNFS
  2022. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
  2023. #else
  2024. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  2025. #endif
  2026. /* Referrals are supported, Migration is not. */
  2027. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  2028. /* set the wire flags to return to client. */
  2029. clid->flags = new->cl_exchange_flags;
  2030. }
  2031. static bool client_has_openowners(struct nfs4_client *clp)
  2032. {
  2033. struct nfs4_openowner *oo;
  2034. list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
  2035. if (!list_empty(&oo->oo_owner.so_stateids))
  2036. return true;
  2037. }
  2038. return false;
  2039. }
  2040. static bool client_has_state(struct nfs4_client *clp)
  2041. {
  2042. return client_has_openowners(clp)
  2043. #ifdef CONFIG_NFSD_PNFS
  2044. || !list_empty(&clp->cl_lo_states)
  2045. #endif
  2046. || !list_empty(&clp->cl_delegations)
  2047. || !list_empty(&clp->cl_sessions);
  2048. }
  2049. __be32
  2050. nfsd4_exchange_id(struct svc_rqst *rqstp,
  2051. struct nfsd4_compound_state *cstate,
  2052. struct nfsd4_exchange_id *exid)
  2053. {
  2054. struct nfs4_client *conf, *new;
  2055. struct nfs4_client *unconf = NULL;
  2056. __be32 status;
  2057. char addr_str[INET6_ADDRSTRLEN];
  2058. nfs4_verifier verf = exid->verifier;
  2059. struct sockaddr *sa = svc_addr(rqstp);
  2060. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  2061. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2062. rpc_ntop(sa, addr_str, sizeof(addr_str));
  2063. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  2064. "ip_addr=%s flags %x, spa_how %d\n",
  2065. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  2066. addr_str, exid->flags, exid->spa_how);
  2067. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  2068. return nfserr_inval;
  2069. new = create_client(exid->clname, rqstp, &verf);
  2070. if (new == NULL)
  2071. return nfserr_jukebox;
  2072. switch (exid->spa_how) {
  2073. case SP4_MACH_CRED:
  2074. exid->spo_must_enforce[0] = 0;
  2075. exid->spo_must_enforce[1] = (
  2076. 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  2077. 1 << (OP_EXCHANGE_ID - 32) |
  2078. 1 << (OP_CREATE_SESSION - 32) |
  2079. 1 << (OP_DESTROY_SESSION - 32) |
  2080. 1 << (OP_DESTROY_CLIENTID - 32));
  2081. exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
  2082. 1 << (OP_OPEN_DOWNGRADE) |
  2083. 1 << (OP_LOCKU) |
  2084. 1 << (OP_DELEGRETURN));
  2085. exid->spo_must_allow[1] &= (
  2086. 1 << (OP_TEST_STATEID - 32) |
  2087. 1 << (OP_FREE_STATEID - 32));
  2088. if (!svc_rqst_integrity_protected(rqstp)) {
  2089. status = nfserr_inval;
  2090. goto out_nolock;
  2091. }
  2092. /*
  2093. * Sometimes userspace doesn't give us a principal.
  2094. * Which is a bug, really. Anyway, we can't enforce
  2095. * MACH_CRED in that case, better to give up now:
  2096. */
  2097. if (!new->cl_cred.cr_principal &&
  2098. !new->cl_cred.cr_raw_principal) {
  2099. status = nfserr_serverfault;
  2100. goto out_nolock;
  2101. }
  2102. new->cl_mach_cred = true;
  2103. case SP4_NONE:
  2104. break;
  2105. default: /* checked by xdr code */
  2106. WARN_ON_ONCE(1);
  2107. case SP4_SSV:
  2108. status = nfserr_encr_alg_unsupp;
  2109. goto out_nolock;
  2110. }
  2111. /* Cases below refer to rfc 5661 section 18.35.4: */
  2112. spin_lock(&nn->client_lock);
  2113. conf = find_confirmed_client_by_name(&exid->clname, nn);
  2114. if (conf) {
  2115. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  2116. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  2117. if (update) {
  2118. if (!clp_used_exchangeid(conf)) { /* buggy client */
  2119. status = nfserr_inval;
  2120. goto out;
  2121. }
  2122. if (!nfsd4_mach_creds_match(conf, rqstp)) {
  2123. status = nfserr_wrong_cred;
  2124. goto out;
  2125. }
  2126. if (!creds_match) { /* case 9 */
  2127. status = nfserr_perm;
  2128. goto out;
  2129. }
  2130. if (!verfs_match) { /* case 8 */
  2131. status = nfserr_not_same;
  2132. goto out;
  2133. }
  2134. /* case 6 */
  2135. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  2136. goto out_copy;
  2137. }
  2138. if (!creds_match) { /* case 3 */
  2139. if (client_has_state(conf)) {
  2140. status = nfserr_clid_inuse;
  2141. goto out;
  2142. }
  2143. goto out_new;
  2144. }
  2145. if (verfs_match) { /* case 2 */
  2146. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  2147. goto out_copy;
  2148. }
  2149. /* case 5, client reboot */
  2150. conf = NULL;
  2151. goto out_new;
  2152. }
  2153. if (update) { /* case 7 */
  2154. status = nfserr_noent;
  2155. goto out;
  2156. }
  2157. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  2158. if (unconf) /* case 4, possible retry or client restart */
  2159. unhash_client_locked(unconf);
  2160. /* case 1 (normal case) */
  2161. out_new:
  2162. if (conf) {
  2163. status = mark_client_expired_locked(conf);
  2164. if (status)
  2165. goto out;
  2166. }
  2167. new->cl_minorversion = cstate->minorversion;
  2168. new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
  2169. new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
  2170. gen_clid(new, nn);
  2171. add_to_unconfirmed(new);
  2172. swap(new, conf);
  2173. out_copy:
  2174. exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
  2175. exid->clientid.cl_id = conf->cl_clientid.cl_id;
  2176. exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
  2177. nfsd4_set_ex_flags(conf, exid);
  2178. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  2179. conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
  2180. status = nfs_ok;
  2181. out:
  2182. spin_unlock(&nn->client_lock);
  2183. out_nolock:
  2184. if (new)
  2185. expire_client(new);
  2186. if (unconf)
  2187. expire_client(unconf);
  2188. return status;
  2189. }
  2190. static __be32
  2191. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  2192. {
  2193. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  2194. slot_seqid);
  2195. /* The slot is in use, and no response has been sent. */
  2196. if (slot_inuse) {
  2197. if (seqid == slot_seqid)
  2198. return nfserr_jukebox;
  2199. else
  2200. return nfserr_seq_misordered;
  2201. }
  2202. /* Note unsigned 32-bit arithmetic handles wraparound: */
  2203. if (likely(seqid == slot_seqid + 1))
  2204. return nfs_ok;
  2205. if (seqid == slot_seqid)
  2206. return nfserr_replay_cache;
  2207. return nfserr_seq_misordered;
  2208. }
  2209. /*
  2210. * Cache the create session result into the create session single DRC
  2211. * slot cache by saving the xdr structure. sl_seqid has been set.
  2212. * Do this for solo or embedded create session operations.
  2213. */
  2214. static void
  2215. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  2216. struct nfsd4_clid_slot *slot, __be32 nfserr)
  2217. {
  2218. slot->sl_status = nfserr;
  2219. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  2220. }
  2221. static __be32
  2222. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  2223. struct nfsd4_clid_slot *slot)
  2224. {
  2225. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  2226. return slot->sl_status;
  2227. }
  2228. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  2229. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  2230. 1 + /* MIN tag is length with zero, only length */ \
  2231. 3 + /* version, opcount, opcode */ \
  2232. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  2233. /* seqid, slotID, slotID, cache */ \
  2234. 4 ) * sizeof(__be32))
  2235. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  2236. 2 + /* verifier: AUTH_NULL, length 0 */\
  2237. 1 + /* status */ \
  2238. 1 + /* MIN tag is length with zero, only length */ \
  2239. 3 + /* opcount, opcode, opstatus*/ \
  2240. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  2241. /* seqid, slotID, slotID, slotID, status */ \
  2242. 5 ) * sizeof(__be32))
  2243. static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
  2244. {
  2245. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  2246. if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
  2247. return nfserr_toosmall;
  2248. if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
  2249. return nfserr_toosmall;
  2250. ca->headerpadsz = 0;
  2251. ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
  2252. ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
  2253. ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  2254. ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
  2255. NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
  2256. ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
  2257. /*
  2258. * Note decreasing slot size below client's request may make it
  2259. * difficult for client to function correctly, whereas
  2260. * decreasing the number of slots will (just?) affect
  2261. * performance. When short on memory we therefore prefer to
  2262. * decrease number of slots instead of their size. Clients that
  2263. * request larger slots than they need will get poor results:
  2264. */
  2265. ca->maxreqs = nfsd4_get_drc_mem(ca);
  2266. if (!ca->maxreqs)
  2267. return nfserr_jukebox;
  2268. return nfs_ok;
  2269. }
  2270. /*
  2271. * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
  2272. * These are based on similar macros in linux/sunrpc/msg_prot.h .
  2273. */
  2274. #define RPC_MAX_HEADER_WITH_AUTH_SYS \
  2275. (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
  2276. #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
  2277. (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
  2278. #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
  2279. RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
  2280. #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
  2281. RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
  2282. sizeof(__be32))
  2283. static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
  2284. {
  2285. ca->headerpadsz = 0;
  2286. if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
  2287. return nfserr_toosmall;
  2288. if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
  2289. return nfserr_toosmall;
  2290. ca->maxresp_cached = 0;
  2291. if (ca->maxops < 2)
  2292. return nfserr_toosmall;
  2293. return nfs_ok;
  2294. }
  2295. static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
  2296. {
  2297. switch (cbs->flavor) {
  2298. case RPC_AUTH_NULL:
  2299. case RPC_AUTH_UNIX:
  2300. return nfs_ok;
  2301. default:
  2302. /*
  2303. * GSS case: the spec doesn't allow us to return this
  2304. * error. But it also doesn't allow us not to support
  2305. * GSS.
  2306. * I'd rather this fail hard than return some error the
  2307. * client might think it can already handle:
  2308. */
  2309. return nfserr_encr_alg_unsupp;
  2310. }
  2311. }
  2312. __be32
  2313. nfsd4_create_session(struct svc_rqst *rqstp,
  2314. struct nfsd4_compound_state *cstate,
  2315. struct nfsd4_create_session *cr_ses)
  2316. {
  2317. struct sockaddr *sa = svc_addr(rqstp);
  2318. struct nfs4_client *conf, *unconf;
  2319. struct nfs4_client *old = NULL;
  2320. struct nfsd4_session *new;
  2321. struct nfsd4_conn *conn;
  2322. struct nfsd4_clid_slot *cs_slot = NULL;
  2323. __be32 status = 0;
  2324. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2325. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  2326. return nfserr_inval;
  2327. status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
  2328. if (status)
  2329. return status;
  2330. status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
  2331. if (status)
  2332. return status;
  2333. status = check_backchannel_attrs(&cr_ses->back_channel);
  2334. if (status)
  2335. goto out_release_drc_mem;
  2336. status = nfserr_jukebox;
  2337. new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
  2338. if (!new)
  2339. goto out_release_drc_mem;
  2340. conn = alloc_conn_from_crses(rqstp, cr_ses);
  2341. if (!conn)
  2342. goto out_free_session;
  2343. spin_lock(&nn->client_lock);
  2344. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  2345. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  2346. WARN_ON_ONCE(conf && unconf);
  2347. if (conf) {
  2348. status = nfserr_wrong_cred;
  2349. if (!nfsd4_mach_creds_match(conf, rqstp))
  2350. goto out_free_conn;
  2351. cs_slot = &conf->cl_cs_slot;
  2352. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  2353. if (status) {
  2354. if (status == nfserr_replay_cache)
  2355. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  2356. goto out_free_conn;
  2357. }
  2358. } else if (unconf) {
  2359. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  2360. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  2361. status = nfserr_clid_inuse;
  2362. goto out_free_conn;
  2363. }
  2364. status = nfserr_wrong_cred;
  2365. if (!nfsd4_mach_creds_match(unconf, rqstp))
  2366. goto out_free_conn;
  2367. cs_slot = &unconf->cl_cs_slot;
  2368. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  2369. if (status) {
  2370. /* an unconfirmed replay returns misordered */
  2371. status = nfserr_seq_misordered;
  2372. goto out_free_conn;
  2373. }
  2374. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2375. if (old) {
  2376. status = mark_client_expired_locked(old);
  2377. if (status) {
  2378. old = NULL;
  2379. goto out_free_conn;
  2380. }
  2381. }
  2382. move_to_confirmed(unconf);
  2383. conf = unconf;
  2384. } else {
  2385. status = nfserr_stale_clientid;
  2386. goto out_free_conn;
  2387. }
  2388. status = nfs_ok;
  2389. /* Persistent sessions are not supported */
  2390. cr_ses->flags &= ~SESSION4_PERSIST;
  2391. /* Upshifting from TCP to RDMA is not supported */
  2392. cr_ses->flags &= ~SESSION4_RDMA;
  2393. init_session(rqstp, new, conf, cr_ses);
  2394. nfsd4_get_session_locked(new);
  2395. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  2396. NFS4_MAX_SESSIONID_LEN);
  2397. cs_slot->sl_seqid++;
  2398. cr_ses->seqid = cs_slot->sl_seqid;
  2399. /* cache solo and embedded create sessions under the client_lock */
  2400. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  2401. spin_unlock(&nn->client_lock);
  2402. /* init connection and backchannel */
  2403. nfsd4_init_conn(rqstp, conn, new);
  2404. nfsd4_put_session(new);
  2405. if (old)
  2406. expire_client(old);
  2407. return status;
  2408. out_free_conn:
  2409. spin_unlock(&nn->client_lock);
  2410. free_conn(conn);
  2411. if (old)
  2412. expire_client(old);
  2413. out_free_session:
  2414. __free_session(new);
  2415. out_release_drc_mem:
  2416. nfsd4_put_drc_mem(&cr_ses->fore_channel);
  2417. return status;
  2418. }
  2419. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  2420. {
  2421. switch (*dir) {
  2422. case NFS4_CDFC4_FORE:
  2423. case NFS4_CDFC4_BACK:
  2424. return nfs_ok;
  2425. case NFS4_CDFC4_FORE_OR_BOTH:
  2426. case NFS4_CDFC4_BACK_OR_BOTH:
  2427. *dir = NFS4_CDFC4_BOTH;
  2428. return nfs_ok;
  2429. };
  2430. return nfserr_inval;
  2431. }
  2432. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  2433. {
  2434. struct nfsd4_session *session = cstate->session;
  2435. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2436. __be32 status;
  2437. status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
  2438. if (status)
  2439. return status;
  2440. spin_lock(&nn->client_lock);
  2441. session->se_cb_prog = bc->bc_cb_program;
  2442. session->se_cb_sec = bc->bc_cb_sec;
  2443. spin_unlock(&nn->client_lock);
  2444. nfsd4_probe_callback(session->se_client);
  2445. return nfs_ok;
  2446. }
  2447. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  2448. struct nfsd4_compound_state *cstate,
  2449. struct nfsd4_bind_conn_to_session *bcts)
  2450. {
  2451. __be32 status;
  2452. struct nfsd4_conn *conn;
  2453. struct nfsd4_session *session;
  2454. struct net *net = SVC_NET(rqstp);
  2455. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2456. if (!nfsd4_last_compound_op(rqstp))
  2457. return nfserr_not_only_op;
  2458. spin_lock(&nn->client_lock);
  2459. session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
  2460. spin_unlock(&nn->client_lock);
  2461. if (!session)
  2462. goto out_no_session;
  2463. status = nfserr_wrong_cred;
  2464. if (!nfsd4_mach_creds_match(session->se_client, rqstp))
  2465. goto out;
  2466. status = nfsd4_map_bcts_dir(&bcts->dir);
  2467. if (status)
  2468. goto out;
  2469. conn = alloc_conn(rqstp, bcts->dir);
  2470. status = nfserr_jukebox;
  2471. if (!conn)
  2472. goto out;
  2473. nfsd4_init_conn(rqstp, conn, session);
  2474. status = nfs_ok;
  2475. out:
  2476. nfsd4_put_session(session);
  2477. out_no_session:
  2478. return status;
  2479. }
  2480. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  2481. {
  2482. if (!session)
  2483. return 0;
  2484. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  2485. }
  2486. __be32
  2487. nfsd4_destroy_session(struct svc_rqst *r,
  2488. struct nfsd4_compound_state *cstate,
  2489. struct nfsd4_destroy_session *sessionid)
  2490. {
  2491. struct nfsd4_session *ses;
  2492. __be32 status;
  2493. int ref_held_by_me = 0;
  2494. struct net *net = SVC_NET(r);
  2495. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2496. status = nfserr_not_only_op;
  2497. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  2498. if (!nfsd4_last_compound_op(r))
  2499. goto out;
  2500. ref_held_by_me++;
  2501. }
  2502. dump_sessionid(__func__, &sessionid->sessionid);
  2503. spin_lock(&nn->client_lock);
  2504. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
  2505. if (!ses)
  2506. goto out_client_lock;
  2507. status = nfserr_wrong_cred;
  2508. if (!nfsd4_mach_creds_match(ses->se_client, r))
  2509. goto out_put_session;
  2510. status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
  2511. if (status)
  2512. goto out_put_session;
  2513. unhash_session(ses);
  2514. spin_unlock(&nn->client_lock);
  2515. nfsd4_probe_callback_sync(ses->se_client);
  2516. spin_lock(&nn->client_lock);
  2517. status = nfs_ok;
  2518. out_put_session:
  2519. nfsd4_put_session_locked(ses);
  2520. out_client_lock:
  2521. spin_unlock(&nn->client_lock);
  2522. out:
  2523. return status;
  2524. }
  2525. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  2526. {
  2527. struct nfsd4_conn *c;
  2528. list_for_each_entry(c, &s->se_conns, cn_persession) {
  2529. if (c->cn_xprt == xpt) {
  2530. return c;
  2531. }
  2532. }
  2533. return NULL;
  2534. }
  2535. static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  2536. {
  2537. struct nfs4_client *clp = ses->se_client;
  2538. struct nfsd4_conn *c;
  2539. __be32 status = nfs_ok;
  2540. int ret;
  2541. spin_lock(&clp->cl_lock);
  2542. c = __nfsd4_find_conn(new->cn_xprt, ses);
  2543. if (c)
  2544. goto out_free;
  2545. status = nfserr_conn_not_bound_to_session;
  2546. if (clp->cl_mach_cred)
  2547. goto out_free;
  2548. __nfsd4_hash_conn(new, ses);
  2549. spin_unlock(&clp->cl_lock);
  2550. ret = nfsd4_register_conn(new);
  2551. if (ret)
  2552. /* oops; xprt is already down: */
  2553. nfsd4_conn_lost(&new->cn_xpt_user);
  2554. return nfs_ok;
  2555. out_free:
  2556. spin_unlock(&clp->cl_lock);
  2557. free_conn(new);
  2558. return status;
  2559. }
  2560. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  2561. {
  2562. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  2563. return args->opcnt > session->se_fchannel.maxops;
  2564. }
  2565. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  2566. struct nfsd4_session *session)
  2567. {
  2568. struct xdr_buf *xb = &rqstp->rq_arg;
  2569. return xb->len > session->se_fchannel.maxreq_sz;
  2570. }
  2571. __be32
  2572. nfsd4_sequence(struct svc_rqst *rqstp,
  2573. struct nfsd4_compound_state *cstate,
  2574. struct nfsd4_sequence *seq)
  2575. {
  2576. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2577. struct xdr_stream *xdr = &resp->xdr;
  2578. struct nfsd4_session *session;
  2579. struct nfs4_client *clp;
  2580. struct nfsd4_slot *slot;
  2581. struct nfsd4_conn *conn;
  2582. __be32 status;
  2583. int buflen;
  2584. struct net *net = SVC_NET(rqstp);
  2585. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2586. if (resp->opcnt != 1)
  2587. return nfserr_sequence_pos;
  2588. /*
  2589. * Will be either used or freed by nfsd4_sequence_check_conn
  2590. * below.
  2591. */
  2592. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  2593. if (!conn)
  2594. return nfserr_jukebox;
  2595. spin_lock(&nn->client_lock);
  2596. session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
  2597. if (!session)
  2598. goto out_no_session;
  2599. clp = session->se_client;
  2600. status = nfserr_too_many_ops;
  2601. if (nfsd4_session_too_many_ops(rqstp, session))
  2602. goto out_put_session;
  2603. status = nfserr_req_too_big;
  2604. if (nfsd4_request_too_big(rqstp, session))
  2605. goto out_put_session;
  2606. status = nfserr_badslot;
  2607. if (seq->slotid >= session->se_fchannel.maxreqs)
  2608. goto out_put_session;
  2609. slot = session->se_slots[seq->slotid];
  2610. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  2611. /* We do not negotiate the number of slots yet, so set the
  2612. * maxslots to the session maxreqs which is used to encode
  2613. * sr_highest_slotid and the sr_target_slot id to maxslots */
  2614. seq->maxslots = session->se_fchannel.maxreqs;
  2615. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  2616. slot->sl_flags & NFSD4_SLOT_INUSE);
  2617. if (status == nfserr_replay_cache) {
  2618. status = nfserr_seq_misordered;
  2619. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  2620. goto out_put_session;
  2621. cstate->slot = slot;
  2622. cstate->session = session;
  2623. cstate->clp = clp;
  2624. /* Return the cached reply status and set cstate->status
  2625. * for nfsd4_proc_compound processing */
  2626. status = nfsd4_replay_cache_entry(resp, seq);
  2627. cstate->status = nfserr_replay_cache;
  2628. goto out;
  2629. }
  2630. if (status)
  2631. goto out_put_session;
  2632. status = nfsd4_sequence_check_conn(conn, session);
  2633. conn = NULL;
  2634. if (status)
  2635. goto out_put_session;
  2636. buflen = (seq->cachethis) ?
  2637. session->se_fchannel.maxresp_cached :
  2638. session->se_fchannel.maxresp_sz;
  2639. status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
  2640. nfserr_rep_too_big;
  2641. if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
  2642. goto out_put_session;
  2643. svc_reserve(rqstp, buflen);
  2644. status = nfs_ok;
  2645. /* Success! bump slot seqid */
  2646. slot->sl_seqid = seq->seqid;
  2647. slot->sl_flags |= NFSD4_SLOT_INUSE;
  2648. if (seq->cachethis)
  2649. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  2650. else
  2651. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  2652. cstate->slot = slot;
  2653. cstate->session = session;
  2654. cstate->clp = clp;
  2655. out:
  2656. switch (clp->cl_cb_state) {
  2657. case NFSD4_CB_DOWN:
  2658. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  2659. break;
  2660. case NFSD4_CB_FAULT:
  2661. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  2662. break;
  2663. default:
  2664. seq->status_flags = 0;
  2665. }
  2666. if (!list_empty(&clp->cl_revoked))
  2667. seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
  2668. out_no_session:
  2669. if (conn)
  2670. free_conn(conn);
  2671. spin_unlock(&nn->client_lock);
  2672. return status;
  2673. out_put_session:
  2674. nfsd4_put_session_locked(session);
  2675. goto out_no_session;
  2676. }
  2677. void
  2678. nfsd4_sequence_done(struct nfsd4_compoundres *resp)
  2679. {
  2680. struct nfsd4_compound_state *cs = &resp->cstate;
  2681. if (nfsd4_has_session(cs)) {
  2682. if (cs->status != nfserr_replay_cache) {
  2683. nfsd4_store_cache_entry(resp);
  2684. cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
  2685. }
  2686. /* Drop session reference that was taken in nfsd4_sequence() */
  2687. nfsd4_put_session(cs->session);
  2688. } else if (cs->clp)
  2689. put_client_renew(cs->clp);
  2690. }
  2691. __be32
  2692. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  2693. {
  2694. struct nfs4_client *conf, *unconf;
  2695. struct nfs4_client *clp = NULL;
  2696. __be32 status = 0;
  2697. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2698. spin_lock(&nn->client_lock);
  2699. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  2700. conf = find_confirmed_client(&dc->clientid, true, nn);
  2701. WARN_ON_ONCE(conf && unconf);
  2702. if (conf) {
  2703. if (client_has_state(conf)) {
  2704. status = nfserr_clientid_busy;
  2705. goto out;
  2706. }
  2707. status = mark_client_expired_locked(conf);
  2708. if (status)
  2709. goto out;
  2710. clp = conf;
  2711. } else if (unconf)
  2712. clp = unconf;
  2713. else {
  2714. status = nfserr_stale_clientid;
  2715. goto out;
  2716. }
  2717. if (!nfsd4_mach_creds_match(clp, rqstp)) {
  2718. clp = NULL;
  2719. status = nfserr_wrong_cred;
  2720. goto out;
  2721. }
  2722. unhash_client_locked(clp);
  2723. out:
  2724. spin_unlock(&nn->client_lock);
  2725. if (clp)
  2726. expire_client(clp);
  2727. return status;
  2728. }
  2729. __be32
  2730. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  2731. {
  2732. __be32 status = 0;
  2733. if (rc->rca_one_fs) {
  2734. if (!cstate->current_fh.fh_dentry)
  2735. return nfserr_nofilehandle;
  2736. /*
  2737. * We don't take advantage of the rca_one_fs case.
  2738. * That's OK, it's optional, we can safely ignore it.
  2739. */
  2740. return nfs_ok;
  2741. }
  2742. status = nfserr_complete_already;
  2743. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  2744. &cstate->session->se_client->cl_flags))
  2745. goto out;
  2746. status = nfserr_stale_clientid;
  2747. if (is_client_expired(cstate->session->se_client))
  2748. /*
  2749. * The following error isn't really legal.
  2750. * But we only get here if the client just explicitly
  2751. * destroyed the client. Surely it no longer cares what
  2752. * error it gets back on an operation for the dead
  2753. * client.
  2754. */
  2755. goto out;
  2756. status = nfs_ok;
  2757. nfsd4_client_record_create(cstate->session->se_client);
  2758. out:
  2759. return status;
  2760. }
  2761. __be32
  2762. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2763. struct nfsd4_setclientid *setclid)
  2764. {
  2765. struct xdr_netobj clname = setclid->se_name;
  2766. nfs4_verifier clverifier = setclid->se_verf;
  2767. struct nfs4_client *conf, *new;
  2768. struct nfs4_client *unconf = NULL;
  2769. __be32 status;
  2770. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2771. new = create_client(clname, rqstp, &clverifier);
  2772. if (new == NULL)
  2773. return nfserr_jukebox;
  2774. /* Cases below refer to rfc 3530 section 14.2.33: */
  2775. spin_lock(&nn->client_lock);
  2776. conf = find_confirmed_client_by_name(&clname, nn);
  2777. if (conf && client_has_state(conf)) {
  2778. /* case 0: */
  2779. status = nfserr_clid_inuse;
  2780. if (clp_used_exchangeid(conf))
  2781. goto out;
  2782. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  2783. char addr_str[INET6_ADDRSTRLEN];
  2784. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  2785. sizeof(addr_str));
  2786. dprintk("NFSD: setclientid: string in use by client "
  2787. "at %s\n", addr_str);
  2788. goto out;
  2789. }
  2790. }
  2791. unconf = find_unconfirmed_client_by_name(&clname, nn);
  2792. if (unconf)
  2793. unhash_client_locked(unconf);
  2794. if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
  2795. /* case 1: probable callback update */
  2796. copy_clid(new, conf);
  2797. gen_confirm(new, nn);
  2798. } else /* case 4 (new client) or cases 2, 3 (client reboot): */
  2799. gen_clid(new, nn);
  2800. new->cl_minorversion = 0;
  2801. gen_callback(new, setclid, rqstp);
  2802. add_to_unconfirmed(new);
  2803. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  2804. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  2805. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  2806. new = NULL;
  2807. status = nfs_ok;
  2808. out:
  2809. spin_unlock(&nn->client_lock);
  2810. if (new)
  2811. free_client(new);
  2812. if (unconf)
  2813. expire_client(unconf);
  2814. return status;
  2815. }
  2816. __be32
  2817. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  2818. struct nfsd4_compound_state *cstate,
  2819. struct nfsd4_setclientid_confirm *setclientid_confirm)
  2820. {
  2821. struct nfs4_client *conf, *unconf;
  2822. struct nfs4_client *old = NULL;
  2823. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  2824. clientid_t * clid = &setclientid_confirm->sc_clientid;
  2825. __be32 status;
  2826. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2827. if (STALE_CLIENTID(clid, nn))
  2828. return nfserr_stale_clientid;
  2829. spin_lock(&nn->client_lock);
  2830. conf = find_confirmed_client(clid, false, nn);
  2831. unconf = find_unconfirmed_client(clid, false, nn);
  2832. /*
  2833. * We try hard to give out unique clientid's, so if we get an
  2834. * attempt to confirm the same clientid with a different cred,
  2835. * the client may be buggy; this should never happen.
  2836. *
  2837. * Nevertheless, RFC 7530 recommends INUSE for this case:
  2838. */
  2839. status = nfserr_clid_inuse;
  2840. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  2841. goto out;
  2842. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  2843. goto out;
  2844. /* cases below refer to rfc 3530 section 14.2.34: */
  2845. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  2846. if (conf && same_verf(&confirm, &conf->cl_confirm)) {
  2847. /* case 2: probable retransmit */
  2848. status = nfs_ok;
  2849. } else /* case 4: client hasn't noticed we rebooted yet? */
  2850. status = nfserr_stale_clientid;
  2851. goto out;
  2852. }
  2853. status = nfs_ok;
  2854. if (conf) { /* case 1: callback update */
  2855. old = unconf;
  2856. unhash_client_locked(old);
  2857. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  2858. } else { /* case 3: normal case; new or rebooted client */
  2859. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2860. if (old) {
  2861. status = nfserr_clid_inuse;
  2862. if (client_has_state(old)
  2863. && !same_creds(&unconf->cl_cred,
  2864. &old->cl_cred))
  2865. goto out;
  2866. status = mark_client_expired_locked(old);
  2867. if (status) {
  2868. old = NULL;
  2869. goto out;
  2870. }
  2871. }
  2872. move_to_confirmed(unconf);
  2873. conf = unconf;
  2874. }
  2875. get_client_locked(conf);
  2876. spin_unlock(&nn->client_lock);
  2877. nfsd4_probe_callback(conf);
  2878. spin_lock(&nn->client_lock);
  2879. put_client_renew_locked(conf);
  2880. out:
  2881. spin_unlock(&nn->client_lock);
  2882. if (old)
  2883. expire_client(old);
  2884. return status;
  2885. }
  2886. static struct nfs4_file *nfsd4_alloc_file(void)
  2887. {
  2888. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2889. }
  2890. /* OPEN Share state helper functions */
  2891. static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
  2892. struct nfs4_file *fp)
  2893. {
  2894. lockdep_assert_held(&state_lock);
  2895. atomic_set(&fp->fi_ref, 1);
  2896. spin_lock_init(&fp->fi_lock);
  2897. INIT_LIST_HEAD(&fp->fi_stateids);
  2898. INIT_LIST_HEAD(&fp->fi_delegations);
  2899. INIT_LIST_HEAD(&fp->fi_clnt_odstate);
  2900. fh_copy_shallow(&fp->fi_fhandle, fh);
  2901. fp->fi_deleg_file = NULL;
  2902. fp->fi_had_conflict = false;
  2903. fp->fi_share_deny = 0;
  2904. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2905. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2906. #ifdef CONFIG_NFSD_PNFS
  2907. INIT_LIST_HEAD(&fp->fi_lo_states);
  2908. atomic_set(&fp->fi_lo_recalls, 0);
  2909. #endif
  2910. hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
  2911. }
  2912. void
  2913. nfsd4_free_slabs(void)
  2914. {
  2915. kmem_cache_destroy(odstate_slab);
  2916. kmem_cache_destroy(openowner_slab);
  2917. kmem_cache_destroy(lockowner_slab);
  2918. kmem_cache_destroy(file_slab);
  2919. kmem_cache_destroy(stateid_slab);
  2920. kmem_cache_destroy(deleg_slab);
  2921. }
  2922. int
  2923. nfsd4_init_slabs(void)
  2924. {
  2925. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2926. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2927. if (openowner_slab == NULL)
  2928. goto out;
  2929. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2930. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2931. if (lockowner_slab == NULL)
  2932. goto out_free_openowner_slab;
  2933. file_slab = kmem_cache_create("nfsd4_files",
  2934. sizeof(struct nfs4_file), 0, 0, NULL);
  2935. if (file_slab == NULL)
  2936. goto out_free_lockowner_slab;
  2937. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2938. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2939. if (stateid_slab == NULL)
  2940. goto out_free_file_slab;
  2941. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2942. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2943. if (deleg_slab == NULL)
  2944. goto out_free_stateid_slab;
  2945. odstate_slab = kmem_cache_create("nfsd4_odstate",
  2946. sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
  2947. if (odstate_slab == NULL)
  2948. goto out_free_deleg_slab;
  2949. return 0;
  2950. out_free_deleg_slab:
  2951. kmem_cache_destroy(deleg_slab);
  2952. out_free_stateid_slab:
  2953. kmem_cache_destroy(stateid_slab);
  2954. out_free_file_slab:
  2955. kmem_cache_destroy(file_slab);
  2956. out_free_lockowner_slab:
  2957. kmem_cache_destroy(lockowner_slab);
  2958. out_free_openowner_slab:
  2959. kmem_cache_destroy(openowner_slab);
  2960. out:
  2961. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2962. return -ENOMEM;
  2963. }
  2964. static void init_nfs4_replay(struct nfs4_replay *rp)
  2965. {
  2966. rp->rp_status = nfserr_serverfault;
  2967. rp->rp_buflen = 0;
  2968. rp->rp_buf = rp->rp_ibuf;
  2969. mutex_init(&rp->rp_mutex);
  2970. }
  2971. static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
  2972. struct nfs4_stateowner *so)
  2973. {
  2974. if (!nfsd4_has_session(cstate)) {
  2975. mutex_lock(&so->so_replay.rp_mutex);
  2976. cstate->replay_owner = nfs4_get_stateowner(so);
  2977. }
  2978. }
  2979. void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
  2980. {
  2981. struct nfs4_stateowner *so = cstate->replay_owner;
  2982. if (so != NULL) {
  2983. cstate->replay_owner = NULL;
  2984. mutex_unlock(&so->so_replay.rp_mutex);
  2985. nfs4_put_stateowner(so);
  2986. }
  2987. }
  2988. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2989. {
  2990. struct nfs4_stateowner *sop;
  2991. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2992. if (!sop)
  2993. return NULL;
  2994. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2995. if (!sop->so_owner.data) {
  2996. kmem_cache_free(slab, sop);
  2997. return NULL;
  2998. }
  2999. sop->so_owner.len = owner->len;
  3000. INIT_LIST_HEAD(&sop->so_stateids);
  3001. sop->so_client = clp;
  3002. init_nfs4_replay(&sop->so_replay);
  3003. atomic_set(&sop->so_count, 1);
  3004. return sop;
  3005. }
  3006. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  3007. {
  3008. lockdep_assert_held(&clp->cl_lock);
  3009. list_add(&oo->oo_owner.so_strhash,
  3010. &clp->cl_ownerstr_hashtbl[strhashval]);
  3011. list_add(&oo->oo_perclient, &clp->cl_openowners);
  3012. }
  3013. static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
  3014. {
  3015. unhash_openowner_locked(openowner(so));
  3016. }
  3017. static void nfs4_free_openowner(struct nfs4_stateowner *so)
  3018. {
  3019. struct nfs4_openowner *oo = openowner(so);
  3020. kmem_cache_free(openowner_slab, oo);
  3021. }
  3022. static const struct nfs4_stateowner_operations openowner_ops = {
  3023. .so_unhash = nfs4_unhash_openowner,
  3024. .so_free = nfs4_free_openowner,
  3025. };
  3026. static struct nfs4_ol_stateid *
  3027. nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
  3028. {
  3029. struct nfs4_ol_stateid *local, *ret = NULL;
  3030. struct nfs4_openowner *oo = open->op_openowner;
  3031. lockdep_assert_held(&fp->fi_lock);
  3032. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  3033. /* ignore lock owners */
  3034. if (local->st_stateowner->so_is_open_owner == 0)
  3035. continue;
  3036. if (local->st_stateowner == &oo->oo_owner) {
  3037. ret = local;
  3038. atomic_inc(&ret->st_stid.sc_count);
  3039. break;
  3040. }
  3041. }
  3042. return ret;
  3043. }
  3044. static struct nfs4_openowner *
  3045. alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
  3046. struct nfsd4_compound_state *cstate)
  3047. {
  3048. struct nfs4_client *clp = cstate->clp;
  3049. struct nfs4_openowner *oo, *ret;
  3050. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  3051. if (!oo)
  3052. return NULL;
  3053. oo->oo_owner.so_ops = &openowner_ops;
  3054. oo->oo_owner.so_is_open_owner = 1;
  3055. oo->oo_owner.so_seqid = open->op_seqid;
  3056. oo->oo_flags = 0;
  3057. if (nfsd4_has_session(cstate))
  3058. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3059. oo->oo_time = 0;
  3060. oo->oo_last_closed_stid = NULL;
  3061. INIT_LIST_HEAD(&oo->oo_close_lru);
  3062. spin_lock(&clp->cl_lock);
  3063. ret = find_openstateowner_str_locked(strhashval, open, clp);
  3064. if (ret == NULL) {
  3065. hash_openowner(oo, clp, strhashval);
  3066. ret = oo;
  3067. } else
  3068. nfs4_free_stateowner(&oo->oo_owner);
  3069. spin_unlock(&clp->cl_lock);
  3070. return ret;
  3071. }
  3072. static struct nfs4_ol_stateid *
  3073. init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
  3074. {
  3075. struct nfs4_openowner *oo = open->op_openowner;
  3076. struct nfs4_ol_stateid *retstp = NULL;
  3077. struct nfs4_ol_stateid *stp;
  3078. stp = open->op_stp;
  3079. /* We are moving these outside of the spinlocks to avoid the warnings */
  3080. mutex_init(&stp->st_mutex);
  3081. mutex_lock(&stp->st_mutex);
  3082. spin_lock(&oo->oo_owner.so_client->cl_lock);
  3083. spin_lock(&fp->fi_lock);
  3084. retstp = nfsd4_find_existing_open(fp, open);
  3085. if (retstp)
  3086. goto out_unlock;
  3087. open->op_stp = NULL;
  3088. atomic_inc(&stp->st_stid.sc_count);
  3089. stp->st_stid.sc_type = NFS4_OPEN_STID;
  3090. INIT_LIST_HEAD(&stp->st_locks);
  3091. stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
  3092. get_nfs4_file(fp);
  3093. stp->st_stid.sc_file = fp;
  3094. stp->st_access_bmap = 0;
  3095. stp->st_deny_bmap = 0;
  3096. stp->st_openstp = NULL;
  3097. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  3098. list_add(&stp->st_perfile, &fp->fi_stateids);
  3099. out_unlock:
  3100. spin_unlock(&fp->fi_lock);
  3101. spin_unlock(&oo->oo_owner.so_client->cl_lock);
  3102. if (retstp) {
  3103. mutex_lock(&retstp->st_mutex);
  3104. /* To keep mutex tracking happy */
  3105. mutex_unlock(&stp->st_mutex);
  3106. stp = retstp;
  3107. }
  3108. return stp;
  3109. }
  3110. /*
  3111. * In the 4.0 case we need to keep the owners around a little while to handle
  3112. * CLOSE replay. We still do need to release any file access that is held by
  3113. * them before returning however.
  3114. */
  3115. static void
  3116. move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
  3117. {
  3118. struct nfs4_ol_stateid *last;
  3119. struct nfs4_openowner *oo = openowner(s->st_stateowner);
  3120. struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
  3121. nfsd_net_id);
  3122. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  3123. /*
  3124. * We know that we hold one reference via nfsd4_close, and another
  3125. * "persistent" reference for the client. If the refcount is higher
  3126. * than 2, then there are still calls in progress that are using this
  3127. * stateid. We can't put the sc_file reference until they are finished.
  3128. * Wait for the refcount to drop to 2. Since it has been unhashed,
  3129. * there should be no danger of the refcount going back up again at
  3130. * this point.
  3131. */
  3132. wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
  3133. release_all_access(s);
  3134. if (s->st_stid.sc_file) {
  3135. put_nfs4_file(s->st_stid.sc_file);
  3136. s->st_stid.sc_file = NULL;
  3137. }
  3138. spin_lock(&nn->client_lock);
  3139. last = oo->oo_last_closed_stid;
  3140. oo->oo_last_closed_stid = s;
  3141. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  3142. oo->oo_time = get_seconds();
  3143. spin_unlock(&nn->client_lock);
  3144. if (last)
  3145. nfs4_put_stid(&last->st_stid);
  3146. }
  3147. /* search file_hashtbl[] for file */
  3148. static struct nfs4_file *
  3149. find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
  3150. {
  3151. struct nfs4_file *fp;
  3152. hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
  3153. if (fh_match(&fp->fi_fhandle, fh)) {
  3154. if (atomic_inc_not_zero(&fp->fi_ref))
  3155. return fp;
  3156. }
  3157. }
  3158. return NULL;
  3159. }
  3160. struct nfs4_file *
  3161. find_file(struct knfsd_fh *fh)
  3162. {
  3163. struct nfs4_file *fp;
  3164. unsigned int hashval = file_hashval(fh);
  3165. rcu_read_lock();
  3166. fp = find_file_locked(fh, hashval);
  3167. rcu_read_unlock();
  3168. return fp;
  3169. }
  3170. static struct nfs4_file *
  3171. find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
  3172. {
  3173. struct nfs4_file *fp;
  3174. unsigned int hashval = file_hashval(fh);
  3175. rcu_read_lock();
  3176. fp = find_file_locked(fh, hashval);
  3177. rcu_read_unlock();
  3178. if (fp)
  3179. return fp;
  3180. spin_lock(&state_lock);
  3181. fp = find_file_locked(fh, hashval);
  3182. if (likely(fp == NULL)) {
  3183. nfsd4_init_file(fh, hashval, new);
  3184. fp = new;
  3185. }
  3186. spin_unlock(&state_lock);
  3187. return fp;
  3188. }
  3189. /*
  3190. * Called to check deny when READ with all zero stateid or
  3191. * WRITE with all zero or all one stateid
  3192. */
  3193. static __be32
  3194. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  3195. {
  3196. struct nfs4_file *fp;
  3197. __be32 ret = nfs_ok;
  3198. fp = find_file(&current_fh->fh_handle);
  3199. if (!fp)
  3200. return ret;
  3201. /* Check for conflicting share reservations */
  3202. spin_lock(&fp->fi_lock);
  3203. if (fp->fi_share_deny & deny_type)
  3204. ret = nfserr_locked;
  3205. spin_unlock(&fp->fi_lock);
  3206. put_nfs4_file(fp);
  3207. return ret;
  3208. }
  3209. static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
  3210. {
  3211. struct nfs4_delegation *dp = cb_to_delegation(cb);
  3212. struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
  3213. nfsd_net_id);
  3214. block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
  3215. /*
  3216. * We can't do this in nfsd_break_deleg_cb because it is
  3217. * already holding inode->i_lock.
  3218. *
  3219. * If the dl_time != 0, then we know that it has already been
  3220. * queued for a lease break. Don't queue it again.
  3221. */
  3222. spin_lock(&state_lock);
  3223. if (dp->dl_time == 0) {
  3224. dp->dl_time = get_seconds();
  3225. list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
  3226. }
  3227. spin_unlock(&state_lock);
  3228. }
  3229. static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
  3230. struct rpc_task *task)
  3231. {
  3232. struct nfs4_delegation *dp = cb_to_delegation(cb);
  3233. if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
  3234. return 1;
  3235. switch (task->tk_status) {
  3236. case 0:
  3237. return 1;
  3238. case -EBADHANDLE:
  3239. case -NFS4ERR_BAD_STATEID:
  3240. /*
  3241. * Race: client probably got cb_recall before open reply
  3242. * granting delegation.
  3243. */
  3244. if (dp->dl_retries--) {
  3245. rpc_delay(task, 2 * HZ);
  3246. return 0;
  3247. }
  3248. /*FALLTHRU*/
  3249. default:
  3250. return -1;
  3251. }
  3252. }
  3253. static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
  3254. {
  3255. struct nfs4_delegation *dp = cb_to_delegation(cb);
  3256. nfs4_put_stid(&dp->dl_stid);
  3257. }
  3258. static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
  3259. .prepare = nfsd4_cb_recall_prepare,
  3260. .done = nfsd4_cb_recall_done,
  3261. .release = nfsd4_cb_recall_release,
  3262. };
  3263. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  3264. {
  3265. /*
  3266. * We're assuming the state code never drops its reference
  3267. * without first removing the lease. Since we're in this lease
  3268. * callback (and since the lease code is serialized by the kernel
  3269. * lock) we know the server hasn't removed the lease yet, we know
  3270. * it's safe to take a reference.
  3271. */
  3272. atomic_inc(&dp->dl_stid.sc_count);
  3273. nfsd4_run_cb(&dp->dl_recall);
  3274. }
  3275. /* Called from break_lease() with i_lock held. */
  3276. static bool
  3277. nfsd_break_deleg_cb(struct file_lock *fl)
  3278. {
  3279. bool ret = false;
  3280. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  3281. struct nfs4_delegation *dp;
  3282. if (!fp) {
  3283. WARN(1, "(%p)->fl_owner NULL\n", fl);
  3284. return ret;
  3285. }
  3286. if (fp->fi_had_conflict) {
  3287. WARN(1, "duplicate break on %p\n", fp);
  3288. return ret;
  3289. }
  3290. /*
  3291. * We don't want the locks code to timeout the lease for us;
  3292. * we'll remove it ourself if a delegation isn't returned
  3293. * in time:
  3294. */
  3295. fl->fl_break_time = 0;
  3296. spin_lock(&fp->fi_lock);
  3297. fp->fi_had_conflict = true;
  3298. /*
  3299. * If there are no delegations on the list, then return true
  3300. * so that the lease code will go ahead and delete it.
  3301. */
  3302. if (list_empty(&fp->fi_delegations))
  3303. ret = true;
  3304. else
  3305. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  3306. nfsd_break_one_deleg(dp);
  3307. spin_unlock(&fp->fi_lock);
  3308. return ret;
  3309. }
  3310. static int
  3311. nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
  3312. struct list_head *dispose)
  3313. {
  3314. if (arg & F_UNLCK)
  3315. return lease_modify(onlist, arg, dispose);
  3316. else
  3317. return -EAGAIN;
  3318. }
  3319. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  3320. .lm_break = nfsd_break_deleg_cb,
  3321. .lm_change = nfsd_change_deleg_cb,
  3322. };
  3323. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  3324. {
  3325. if (nfsd4_has_session(cstate))
  3326. return nfs_ok;
  3327. if (seqid == so->so_seqid - 1)
  3328. return nfserr_replay_me;
  3329. if (seqid == so->so_seqid)
  3330. return nfs_ok;
  3331. return nfserr_bad_seqid;
  3332. }
  3333. static __be32 lookup_clientid(clientid_t *clid,
  3334. struct nfsd4_compound_state *cstate,
  3335. struct nfsd_net *nn)
  3336. {
  3337. struct nfs4_client *found;
  3338. if (cstate->clp) {
  3339. found = cstate->clp;
  3340. if (!same_clid(&found->cl_clientid, clid))
  3341. return nfserr_stale_clientid;
  3342. return nfs_ok;
  3343. }
  3344. if (STALE_CLIENTID(clid, nn))
  3345. return nfserr_stale_clientid;
  3346. /*
  3347. * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
  3348. * cached already then we know this is for is for v4.0 and "sessions"
  3349. * will be false.
  3350. */
  3351. WARN_ON_ONCE(cstate->session);
  3352. spin_lock(&nn->client_lock);
  3353. found = find_confirmed_client(clid, false, nn);
  3354. if (!found) {
  3355. spin_unlock(&nn->client_lock);
  3356. return nfserr_expired;
  3357. }
  3358. atomic_inc(&found->cl_refcount);
  3359. spin_unlock(&nn->client_lock);
  3360. /* Cache the nfs4_client in cstate! */
  3361. cstate->clp = found;
  3362. return nfs_ok;
  3363. }
  3364. __be32
  3365. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  3366. struct nfsd4_open *open, struct nfsd_net *nn)
  3367. {
  3368. clientid_t *clientid = &open->op_clientid;
  3369. struct nfs4_client *clp = NULL;
  3370. unsigned int strhashval;
  3371. struct nfs4_openowner *oo = NULL;
  3372. __be32 status;
  3373. if (STALE_CLIENTID(&open->op_clientid, nn))
  3374. return nfserr_stale_clientid;
  3375. /*
  3376. * In case we need it later, after we've already created the
  3377. * file and don't want to risk a further failure:
  3378. */
  3379. open->op_file = nfsd4_alloc_file();
  3380. if (open->op_file == NULL)
  3381. return nfserr_jukebox;
  3382. status = lookup_clientid(clientid, cstate, nn);
  3383. if (status)
  3384. return status;
  3385. clp = cstate->clp;
  3386. strhashval = ownerstr_hashval(&open->op_owner);
  3387. oo = find_openstateowner_str(strhashval, open, clp);
  3388. open->op_openowner = oo;
  3389. if (!oo) {
  3390. goto new_owner;
  3391. }
  3392. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  3393. /* Replace unconfirmed owners without checking for replay. */
  3394. release_openowner(oo);
  3395. open->op_openowner = NULL;
  3396. goto new_owner;
  3397. }
  3398. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  3399. if (status)
  3400. return status;
  3401. goto alloc_stateid;
  3402. new_owner:
  3403. oo = alloc_init_open_stateowner(strhashval, open, cstate);
  3404. if (oo == NULL)
  3405. return nfserr_jukebox;
  3406. open->op_openowner = oo;
  3407. alloc_stateid:
  3408. open->op_stp = nfs4_alloc_open_stateid(clp);
  3409. if (!open->op_stp)
  3410. return nfserr_jukebox;
  3411. if (nfsd4_has_session(cstate) &&
  3412. (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
  3413. open->op_odstate = alloc_clnt_odstate(clp);
  3414. if (!open->op_odstate)
  3415. return nfserr_jukebox;
  3416. }
  3417. return nfs_ok;
  3418. }
  3419. static inline __be32
  3420. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  3421. {
  3422. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  3423. return nfserr_openmode;
  3424. else
  3425. return nfs_ok;
  3426. }
  3427. static int share_access_to_flags(u32 share_access)
  3428. {
  3429. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  3430. }
  3431. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  3432. {
  3433. struct nfs4_stid *ret;
  3434. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  3435. if (!ret)
  3436. return NULL;
  3437. return delegstateid(ret);
  3438. }
  3439. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  3440. {
  3441. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  3442. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  3443. }
  3444. static __be32
  3445. nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
  3446. struct nfs4_delegation **dp)
  3447. {
  3448. int flags;
  3449. __be32 status = nfserr_bad_stateid;
  3450. struct nfs4_delegation *deleg;
  3451. deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
  3452. if (deleg == NULL)
  3453. goto out;
  3454. flags = share_access_to_flags(open->op_share_access);
  3455. status = nfs4_check_delegmode(deleg, flags);
  3456. if (status) {
  3457. nfs4_put_stid(&deleg->dl_stid);
  3458. goto out;
  3459. }
  3460. *dp = deleg;
  3461. out:
  3462. if (!nfsd4_is_deleg_cur(open))
  3463. return nfs_ok;
  3464. if (status)
  3465. return status;
  3466. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  3467. return nfs_ok;
  3468. }
  3469. static inline int nfs4_access_to_access(u32 nfs4_access)
  3470. {
  3471. int flags = 0;
  3472. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  3473. flags |= NFSD_MAY_READ;
  3474. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  3475. flags |= NFSD_MAY_WRITE;
  3476. return flags;
  3477. }
  3478. static inline __be32
  3479. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  3480. struct nfsd4_open *open)
  3481. {
  3482. struct iattr iattr = {
  3483. .ia_valid = ATTR_SIZE,
  3484. .ia_size = 0,
  3485. };
  3486. if (!open->op_truncate)
  3487. return 0;
  3488. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  3489. return nfserr_inval;
  3490. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  3491. }
  3492. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  3493. struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
  3494. struct nfsd4_open *open)
  3495. {
  3496. struct file *filp = NULL;
  3497. __be32 status;
  3498. int oflag = nfs4_access_to_omode(open->op_share_access);
  3499. int access = nfs4_access_to_access(open->op_share_access);
  3500. unsigned char old_access_bmap, old_deny_bmap;
  3501. spin_lock(&fp->fi_lock);
  3502. /*
  3503. * Are we trying to set a deny mode that would conflict with
  3504. * current access?
  3505. */
  3506. status = nfs4_file_check_deny(fp, open->op_share_deny);
  3507. if (status != nfs_ok) {
  3508. spin_unlock(&fp->fi_lock);
  3509. goto out;
  3510. }
  3511. /* set access to the file */
  3512. status = nfs4_file_get_access(fp, open->op_share_access);
  3513. if (status != nfs_ok) {
  3514. spin_unlock(&fp->fi_lock);
  3515. goto out;
  3516. }
  3517. /* Set access bits in stateid */
  3518. old_access_bmap = stp->st_access_bmap;
  3519. set_access(open->op_share_access, stp);
  3520. /* Set new deny mask */
  3521. old_deny_bmap = stp->st_deny_bmap;
  3522. set_deny(open->op_share_deny, stp);
  3523. fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  3524. if (!fp->fi_fds[oflag]) {
  3525. spin_unlock(&fp->fi_lock);
  3526. status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
  3527. if (status)
  3528. goto out_put_access;
  3529. spin_lock(&fp->fi_lock);
  3530. if (!fp->fi_fds[oflag]) {
  3531. fp->fi_fds[oflag] = filp;
  3532. filp = NULL;
  3533. }
  3534. }
  3535. spin_unlock(&fp->fi_lock);
  3536. if (filp)
  3537. fput(filp);
  3538. status = nfsd4_truncate(rqstp, cur_fh, open);
  3539. if (status)
  3540. goto out_put_access;
  3541. out:
  3542. return status;
  3543. out_put_access:
  3544. stp->st_access_bmap = old_access_bmap;
  3545. nfs4_file_put_access(fp, open->op_share_access);
  3546. reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
  3547. goto out;
  3548. }
  3549. static __be32
  3550. 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)
  3551. {
  3552. __be32 status;
  3553. unsigned char old_deny_bmap = stp->st_deny_bmap;
  3554. if (!test_access(open->op_share_access, stp))
  3555. return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
  3556. /* test and set deny mode */
  3557. spin_lock(&fp->fi_lock);
  3558. status = nfs4_file_check_deny(fp, open->op_share_deny);
  3559. if (status == nfs_ok) {
  3560. set_deny(open->op_share_deny, stp);
  3561. fp->fi_share_deny |=
  3562. (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  3563. }
  3564. spin_unlock(&fp->fi_lock);
  3565. if (status != nfs_ok)
  3566. return status;
  3567. status = nfsd4_truncate(rqstp, cur_fh, open);
  3568. if (status != nfs_ok)
  3569. reset_union_bmap_deny(old_deny_bmap, stp);
  3570. return status;
  3571. }
  3572. /* Should we give out recallable state?: */
  3573. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  3574. {
  3575. if (clp->cl_cb_state == NFSD4_CB_UP)
  3576. return true;
  3577. /*
  3578. * In the sessions case, since we don't have to establish a
  3579. * separate connection for callbacks, we assume it's OK
  3580. * until we hear otherwise:
  3581. */
  3582. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  3583. }
  3584. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
  3585. {
  3586. struct file_lock *fl;
  3587. fl = locks_alloc_lock();
  3588. if (!fl)
  3589. return NULL;
  3590. fl->fl_lmops = &nfsd_lease_mng_ops;
  3591. fl->fl_flags = FL_DELEG;
  3592. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  3593. fl->fl_end = OFFSET_MAX;
  3594. fl->fl_owner = (fl_owner_t)fp;
  3595. fl->fl_pid = current->tgid;
  3596. return fl;
  3597. }
  3598. /**
  3599. * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
  3600. * @dp: a pointer to the nfs4_delegation we're adding.
  3601. *
  3602. * Return:
  3603. * On success: Return code will be 0 on success.
  3604. *
  3605. * On error: -EAGAIN if there was an existing delegation.
  3606. * nonzero if there is an error in other cases.
  3607. *
  3608. */
  3609. static int nfs4_setlease(struct nfs4_delegation *dp)
  3610. {
  3611. struct nfs4_file *fp = dp->dl_stid.sc_file;
  3612. struct file_lock *fl;
  3613. struct file *filp;
  3614. int status = 0;
  3615. fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
  3616. if (!fl)
  3617. return -ENOMEM;
  3618. filp = find_readable_file(fp);
  3619. if (!filp) {
  3620. /* We should always have a readable file here */
  3621. WARN_ON_ONCE(1);
  3622. locks_free_lock(fl);
  3623. return -EBADF;
  3624. }
  3625. fl->fl_file = filp;
  3626. status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
  3627. if (fl)
  3628. locks_free_lock(fl);
  3629. if (status)
  3630. goto out_fput;
  3631. spin_lock(&state_lock);
  3632. spin_lock(&fp->fi_lock);
  3633. /* Did the lease get broken before we took the lock? */
  3634. status = -EAGAIN;
  3635. if (fp->fi_had_conflict)
  3636. goto out_unlock;
  3637. /* Race breaker */
  3638. if (fp->fi_deleg_file) {
  3639. status = hash_delegation_locked(dp, fp);
  3640. goto out_unlock;
  3641. }
  3642. fp->fi_deleg_file = filp;
  3643. fp->fi_delegees = 0;
  3644. status = hash_delegation_locked(dp, fp);
  3645. spin_unlock(&fp->fi_lock);
  3646. spin_unlock(&state_lock);
  3647. if (status) {
  3648. /* Should never happen, this is a new fi_deleg_file */
  3649. WARN_ON_ONCE(1);
  3650. goto out_fput;
  3651. }
  3652. return 0;
  3653. out_unlock:
  3654. spin_unlock(&fp->fi_lock);
  3655. spin_unlock(&state_lock);
  3656. out_fput:
  3657. fput(filp);
  3658. return status;
  3659. }
  3660. static struct nfs4_delegation *
  3661. nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
  3662. struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
  3663. {
  3664. int status;
  3665. struct nfs4_delegation *dp;
  3666. if (fp->fi_had_conflict)
  3667. return ERR_PTR(-EAGAIN);
  3668. spin_lock(&state_lock);
  3669. spin_lock(&fp->fi_lock);
  3670. status = nfs4_get_existing_delegation(clp, fp);
  3671. spin_unlock(&fp->fi_lock);
  3672. spin_unlock(&state_lock);
  3673. if (status)
  3674. return ERR_PTR(status);
  3675. dp = alloc_init_deleg(clp, fh, odstate);
  3676. if (!dp)
  3677. return ERR_PTR(-ENOMEM);
  3678. get_nfs4_file(fp);
  3679. spin_lock(&state_lock);
  3680. spin_lock(&fp->fi_lock);
  3681. dp->dl_stid.sc_file = fp;
  3682. if (!fp->fi_deleg_file) {
  3683. spin_unlock(&fp->fi_lock);
  3684. spin_unlock(&state_lock);
  3685. status = nfs4_setlease(dp);
  3686. goto out;
  3687. }
  3688. if (fp->fi_had_conflict) {
  3689. status = -EAGAIN;
  3690. goto out_unlock;
  3691. }
  3692. status = hash_delegation_locked(dp, fp);
  3693. out_unlock:
  3694. spin_unlock(&fp->fi_lock);
  3695. spin_unlock(&state_lock);
  3696. out:
  3697. if (status) {
  3698. put_clnt_odstate(dp->dl_clnt_odstate);
  3699. nfs4_put_stid(&dp->dl_stid);
  3700. return ERR_PTR(status);
  3701. }
  3702. return dp;
  3703. }
  3704. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  3705. {
  3706. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3707. if (status == -EAGAIN)
  3708. open->op_why_no_deleg = WND4_CONTENTION;
  3709. else {
  3710. open->op_why_no_deleg = WND4_RESOURCE;
  3711. switch (open->op_deleg_want) {
  3712. case NFS4_SHARE_WANT_READ_DELEG:
  3713. case NFS4_SHARE_WANT_WRITE_DELEG:
  3714. case NFS4_SHARE_WANT_ANY_DELEG:
  3715. break;
  3716. case NFS4_SHARE_WANT_CANCEL:
  3717. open->op_why_no_deleg = WND4_CANCELLED;
  3718. break;
  3719. case NFS4_SHARE_WANT_NO_DELEG:
  3720. WARN_ON_ONCE(1);
  3721. }
  3722. }
  3723. }
  3724. /*
  3725. * Attempt to hand out a delegation.
  3726. *
  3727. * Note we don't support write delegations, and won't until the vfs has
  3728. * proper support for them.
  3729. */
  3730. static void
  3731. nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
  3732. struct nfs4_ol_stateid *stp)
  3733. {
  3734. struct nfs4_delegation *dp;
  3735. struct nfs4_openowner *oo = openowner(stp->st_stateowner);
  3736. struct nfs4_client *clp = stp->st_stid.sc_client;
  3737. int cb_up;
  3738. int status = 0;
  3739. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  3740. open->op_recall = 0;
  3741. switch (open->op_claim_type) {
  3742. case NFS4_OPEN_CLAIM_PREVIOUS:
  3743. if (!cb_up)
  3744. open->op_recall = 1;
  3745. if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
  3746. goto out_no_deleg;
  3747. break;
  3748. case NFS4_OPEN_CLAIM_NULL:
  3749. case NFS4_OPEN_CLAIM_FH:
  3750. /*
  3751. * Let's not give out any delegations till everyone's
  3752. * had the chance to reclaim theirs, *and* until
  3753. * NLM locks have all been reclaimed:
  3754. */
  3755. if (locks_in_grace(clp->net))
  3756. goto out_no_deleg;
  3757. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  3758. goto out_no_deleg;
  3759. /*
  3760. * Also, if the file was opened for write or
  3761. * create, there's a good chance the client's
  3762. * about to write to it, resulting in an
  3763. * immediate recall (since we don't support
  3764. * write delegations):
  3765. */
  3766. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  3767. goto out_no_deleg;
  3768. if (open->op_create == NFS4_OPEN_CREATE)
  3769. goto out_no_deleg;
  3770. break;
  3771. default:
  3772. goto out_no_deleg;
  3773. }
  3774. dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
  3775. if (IS_ERR(dp))
  3776. goto out_no_deleg;
  3777. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  3778. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  3779. STATEID_VAL(&dp->dl_stid.sc_stateid));
  3780. open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
  3781. nfs4_put_stid(&dp->dl_stid);
  3782. return;
  3783. out_no_deleg:
  3784. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
  3785. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  3786. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
  3787. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  3788. open->op_recall = 1;
  3789. }
  3790. /* 4.1 client asking for a delegation? */
  3791. if (open->op_deleg_want)
  3792. nfsd4_open_deleg_none_ext(open, status);
  3793. return;
  3794. }
  3795. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  3796. struct nfs4_delegation *dp)
  3797. {
  3798. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  3799. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  3800. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3801. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  3802. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  3803. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  3804. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3805. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  3806. }
  3807. /* Otherwise the client must be confused wanting a delegation
  3808. * it already has, therefore we don't return
  3809. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  3810. */
  3811. }
  3812. __be32
  3813. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  3814. {
  3815. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  3816. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  3817. struct nfs4_file *fp = NULL;
  3818. struct nfs4_ol_stateid *stp = NULL;
  3819. struct nfs4_delegation *dp = NULL;
  3820. __be32 status;
  3821. /*
  3822. * Lookup file; if found, lookup stateid and check open request,
  3823. * and check for delegations in the process of being recalled.
  3824. * If not found, create the nfs4_file struct
  3825. */
  3826. fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
  3827. if (fp != open->op_file) {
  3828. status = nfs4_check_deleg(cl, open, &dp);
  3829. if (status)
  3830. goto out;
  3831. spin_lock(&fp->fi_lock);
  3832. stp = nfsd4_find_existing_open(fp, open);
  3833. spin_unlock(&fp->fi_lock);
  3834. } else {
  3835. open->op_file = NULL;
  3836. status = nfserr_bad_stateid;
  3837. if (nfsd4_is_deleg_cur(open))
  3838. goto out;
  3839. }
  3840. /*
  3841. * OPEN the file, or upgrade an existing OPEN.
  3842. * If truncate fails, the OPEN fails.
  3843. */
  3844. if (stp) {
  3845. /* Stateid was found, this is an OPEN upgrade */
  3846. mutex_lock(&stp->st_mutex);
  3847. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  3848. if (status) {
  3849. mutex_unlock(&stp->st_mutex);
  3850. goto out;
  3851. }
  3852. } else {
  3853. /* stp is returned locked. */
  3854. stp = init_open_stateid(fp, open);
  3855. /* See if we lost the race to some other thread */
  3856. if (stp->st_access_bmap != 0) {
  3857. status = nfs4_upgrade_open(rqstp, fp, current_fh,
  3858. stp, open);
  3859. if (status) {
  3860. mutex_unlock(&stp->st_mutex);
  3861. goto out;
  3862. }
  3863. goto upgrade_out;
  3864. }
  3865. status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
  3866. if (status) {
  3867. mutex_unlock(&stp->st_mutex);
  3868. release_open_stateid(stp);
  3869. goto out;
  3870. }
  3871. stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
  3872. open->op_odstate);
  3873. if (stp->st_clnt_odstate == open->op_odstate)
  3874. open->op_odstate = NULL;
  3875. }
  3876. upgrade_out:
  3877. nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
  3878. mutex_unlock(&stp->st_mutex);
  3879. if (nfsd4_has_session(&resp->cstate)) {
  3880. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  3881. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3882. open->op_why_no_deleg = WND4_NOT_WANTED;
  3883. goto nodeleg;
  3884. }
  3885. }
  3886. /*
  3887. * Attempt to hand out a delegation. No error return, because the
  3888. * OPEN succeeds even if we fail.
  3889. */
  3890. nfs4_open_delegation(current_fh, open, stp);
  3891. nodeleg:
  3892. status = nfs_ok;
  3893. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  3894. STATEID_VAL(&stp->st_stid.sc_stateid));
  3895. out:
  3896. /* 4.1 client trying to upgrade/downgrade delegation? */
  3897. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  3898. open->op_deleg_want)
  3899. nfsd4_deleg_xgrade_none_ext(open, dp);
  3900. if (fp)
  3901. put_nfs4_file(fp);
  3902. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  3903. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  3904. /*
  3905. * To finish the open response, we just need to set the rflags.
  3906. */
  3907. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  3908. if (nfsd4_has_session(&resp->cstate))
  3909. open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
  3910. else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
  3911. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  3912. if (dp)
  3913. nfs4_put_stid(&dp->dl_stid);
  3914. if (stp)
  3915. nfs4_put_stid(&stp->st_stid);
  3916. return status;
  3917. }
  3918. void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
  3919. struct nfsd4_open *open)
  3920. {
  3921. if (open->op_openowner) {
  3922. struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
  3923. nfsd4_cstate_assign_replay(cstate, so);
  3924. nfs4_put_stateowner(so);
  3925. }
  3926. if (open->op_file)
  3927. kmem_cache_free(file_slab, open->op_file);
  3928. if (open->op_stp)
  3929. nfs4_put_stid(&open->op_stp->st_stid);
  3930. if (open->op_odstate)
  3931. kmem_cache_free(odstate_slab, open->op_odstate);
  3932. }
  3933. __be32
  3934. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3935. clientid_t *clid)
  3936. {
  3937. struct nfs4_client *clp;
  3938. __be32 status;
  3939. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3940. dprintk("process_renew(%08x/%08x): starting\n",
  3941. clid->cl_boot, clid->cl_id);
  3942. status = lookup_clientid(clid, cstate, nn);
  3943. if (status)
  3944. goto out;
  3945. clp = cstate->clp;
  3946. status = nfserr_cb_path_down;
  3947. if (!list_empty(&clp->cl_delegations)
  3948. && clp->cl_cb_state != NFSD4_CB_UP)
  3949. goto out;
  3950. status = nfs_ok;
  3951. out:
  3952. return status;
  3953. }
  3954. void
  3955. nfsd4_end_grace(struct nfsd_net *nn)
  3956. {
  3957. /* do nothing if grace period already ended */
  3958. if (nn->grace_ended)
  3959. return;
  3960. dprintk("NFSD: end of grace period\n");
  3961. nn->grace_ended = true;
  3962. /*
  3963. * If the server goes down again right now, an NFSv4
  3964. * client will still be allowed to reclaim after it comes back up,
  3965. * even if it hasn't yet had a chance to reclaim state this time.
  3966. *
  3967. */
  3968. nfsd4_record_grace_done(nn);
  3969. /*
  3970. * At this point, NFSv4 clients can still reclaim. But if the
  3971. * server crashes, any that have not yet reclaimed will be out
  3972. * of luck on the next boot.
  3973. *
  3974. * (NFSv4.1+ clients are considered to have reclaimed once they
  3975. * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
  3976. * have reclaimed after their first OPEN.)
  3977. */
  3978. locks_end_grace(&nn->nfsd4_manager);
  3979. /*
  3980. * At this point, and once lockd and/or any other containers
  3981. * exit their grace period, further reclaims will fail and
  3982. * regular locking can resume.
  3983. */
  3984. }
  3985. static time_t
  3986. nfs4_laundromat(struct nfsd_net *nn)
  3987. {
  3988. struct nfs4_client *clp;
  3989. struct nfs4_openowner *oo;
  3990. struct nfs4_delegation *dp;
  3991. struct nfs4_ol_stateid *stp;
  3992. struct nfsd4_blocked_lock *nbl;
  3993. struct list_head *pos, *next, reaplist;
  3994. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  3995. time_t t, new_timeo = nn->nfsd4_lease;
  3996. dprintk("NFSD: laundromat service - starting\n");
  3997. nfsd4_end_grace(nn);
  3998. INIT_LIST_HEAD(&reaplist);
  3999. spin_lock(&nn->client_lock);
  4000. list_for_each_safe(pos, next, &nn->client_lru) {
  4001. clp = list_entry(pos, struct nfs4_client, cl_lru);
  4002. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  4003. t = clp->cl_time - cutoff;
  4004. new_timeo = min(new_timeo, t);
  4005. break;
  4006. }
  4007. if (mark_client_expired_locked(clp)) {
  4008. dprintk("NFSD: client in use (clientid %08x)\n",
  4009. clp->cl_clientid.cl_id);
  4010. continue;
  4011. }
  4012. list_add(&clp->cl_lru, &reaplist);
  4013. }
  4014. spin_unlock(&nn->client_lock);
  4015. list_for_each_safe(pos, next, &reaplist) {
  4016. clp = list_entry(pos, struct nfs4_client, cl_lru);
  4017. dprintk("NFSD: purging unused client (clientid %08x)\n",
  4018. clp->cl_clientid.cl_id);
  4019. list_del_init(&clp->cl_lru);
  4020. expire_client(clp);
  4021. }
  4022. spin_lock(&state_lock);
  4023. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  4024. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4025. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  4026. t = dp->dl_time - cutoff;
  4027. new_timeo = min(new_timeo, t);
  4028. break;
  4029. }
  4030. WARN_ON(!unhash_delegation_locked(dp));
  4031. list_add(&dp->dl_recall_lru, &reaplist);
  4032. }
  4033. spin_unlock(&state_lock);
  4034. while (!list_empty(&reaplist)) {
  4035. dp = list_first_entry(&reaplist, struct nfs4_delegation,
  4036. dl_recall_lru);
  4037. list_del_init(&dp->dl_recall_lru);
  4038. revoke_delegation(dp);
  4039. }
  4040. spin_lock(&nn->client_lock);
  4041. while (!list_empty(&nn->close_lru)) {
  4042. oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
  4043. oo_close_lru);
  4044. if (time_after((unsigned long)oo->oo_time,
  4045. (unsigned long)cutoff)) {
  4046. t = oo->oo_time - cutoff;
  4047. new_timeo = min(new_timeo, t);
  4048. break;
  4049. }
  4050. list_del_init(&oo->oo_close_lru);
  4051. stp = oo->oo_last_closed_stid;
  4052. oo->oo_last_closed_stid = NULL;
  4053. spin_unlock(&nn->client_lock);
  4054. nfs4_put_stid(&stp->st_stid);
  4055. spin_lock(&nn->client_lock);
  4056. }
  4057. spin_unlock(&nn->client_lock);
  4058. /*
  4059. * It's possible for a client to try and acquire an already held lock
  4060. * that is being held for a long time, and then lose interest in it.
  4061. * So, we clean out any un-revisited request after a lease period
  4062. * under the assumption that the client is no longer interested.
  4063. *
  4064. * RFC5661, sec. 9.6 states that the client must not rely on getting
  4065. * notifications and must continue to poll for locks, even when the
  4066. * server supports them. Thus this shouldn't lead to clients blocking
  4067. * indefinitely once the lock does become free.
  4068. */
  4069. BUG_ON(!list_empty(&reaplist));
  4070. spin_lock(&nn->blocked_locks_lock);
  4071. while (!list_empty(&nn->blocked_locks_lru)) {
  4072. nbl = list_first_entry(&nn->blocked_locks_lru,
  4073. struct nfsd4_blocked_lock, nbl_lru);
  4074. if (time_after((unsigned long)nbl->nbl_time,
  4075. (unsigned long)cutoff)) {
  4076. t = nbl->nbl_time - cutoff;
  4077. new_timeo = min(new_timeo, t);
  4078. break;
  4079. }
  4080. list_move(&nbl->nbl_lru, &reaplist);
  4081. list_del_init(&nbl->nbl_list);
  4082. }
  4083. spin_unlock(&nn->blocked_locks_lock);
  4084. while (!list_empty(&reaplist)) {
  4085. nbl = list_first_entry(&nn->blocked_locks_lru,
  4086. struct nfsd4_blocked_lock, nbl_lru);
  4087. list_del_init(&nbl->nbl_lru);
  4088. posix_unblock_lock(&nbl->nbl_lock);
  4089. free_blocked_lock(nbl);
  4090. }
  4091. new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
  4092. return new_timeo;
  4093. }
  4094. static struct workqueue_struct *laundry_wq;
  4095. static void laundromat_main(struct work_struct *);
  4096. static void
  4097. laundromat_main(struct work_struct *laundry)
  4098. {
  4099. time_t t;
  4100. struct delayed_work *dwork = to_delayed_work(laundry);
  4101. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  4102. laundromat_work);
  4103. t = nfs4_laundromat(nn);
  4104. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  4105. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  4106. }
  4107. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
  4108. {
  4109. if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
  4110. return nfserr_bad_stateid;
  4111. return nfs_ok;
  4112. }
  4113. static inline int
  4114. access_permit_read(struct nfs4_ol_stateid *stp)
  4115. {
  4116. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  4117. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  4118. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  4119. }
  4120. static inline int
  4121. access_permit_write(struct nfs4_ol_stateid *stp)
  4122. {
  4123. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  4124. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  4125. }
  4126. static
  4127. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  4128. {
  4129. __be32 status = nfserr_openmode;
  4130. /* For lock stateid's, we test the parent open, not the lock: */
  4131. if (stp->st_openstp)
  4132. stp = stp->st_openstp;
  4133. if ((flags & WR_STATE) && !access_permit_write(stp))
  4134. goto out;
  4135. if ((flags & RD_STATE) && !access_permit_read(stp))
  4136. goto out;
  4137. status = nfs_ok;
  4138. out:
  4139. return status;
  4140. }
  4141. static inline __be32
  4142. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  4143. {
  4144. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  4145. return nfs_ok;
  4146. else if (opens_in_grace(net)) {
  4147. /* Answer in remaining cases depends on existence of
  4148. * conflicting state; so we must wait out the grace period. */
  4149. return nfserr_grace;
  4150. } else if (flags & WR_STATE)
  4151. return nfs4_share_conflict(current_fh,
  4152. NFS4_SHARE_DENY_WRITE);
  4153. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  4154. return nfs4_share_conflict(current_fh,
  4155. NFS4_SHARE_DENY_READ);
  4156. }
  4157. /*
  4158. * Allow READ/WRITE during grace period on recovered state only for files
  4159. * that are not able to provide mandatory locking.
  4160. */
  4161. static inline int
  4162. grace_disallows_io(struct net *net, struct inode *inode)
  4163. {
  4164. return opens_in_grace(net) && mandatory_lock(inode);
  4165. }
  4166. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  4167. {
  4168. /*
  4169. * When sessions are used the stateid generation number is ignored
  4170. * when it is zero.
  4171. */
  4172. if (has_session && in->si_generation == 0)
  4173. return nfs_ok;
  4174. if (in->si_generation == ref->si_generation)
  4175. return nfs_ok;
  4176. /* If the client sends us a stateid from the future, it's buggy: */
  4177. if (nfsd4_stateid_generation_after(in, ref))
  4178. return nfserr_bad_stateid;
  4179. /*
  4180. * However, we could see a stateid from the past, even from a
  4181. * non-buggy client. For example, if the client sends a lock
  4182. * while some IO is outstanding, the lock may bump si_generation
  4183. * while the IO is still in flight. The client could avoid that
  4184. * situation by waiting for responses on all the IO requests,
  4185. * but better performance may result in retrying IO that
  4186. * receives an old_stateid error if requests are rarely
  4187. * reordered in flight:
  4188. */
  4189. return nfserr_old_stateid;
  4190. }
  4191. static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
  4192. {
  4193. if (ols->st_stateowner->so_is_open_owner &&
  4194. !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  4195. return nfserr_bad_stateid;
  4196. return nfs_ok;
  4197. }
  4198. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  4199. {
  4200. struct nfs4_stid *s;
  4201. __be32 status = nfserr_bad_stateid;
  4202. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  4203. return status;
  4204. /* Client debugging aid. */
  4205. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  4206. char addr_str[INET6_ADDRSTRLEN];
  4207. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  4208. sizeof(addr_str));
  4209. pr_warn_ratelimited("NFSD: client %s testing state ID "
  4210. "with incorrect client ID\n", addr_str);
  4211. return status;
  4212. }
  4213. spin_lock(&cl->cl_lock);
  4214. s = find_stateid_locked(cl, stateid);
  4215. if (!s)
  4216. goto out_unlock;
  4217. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  4218. if (status)
  4219. goto out_unlock;
  4220. switch (s->sc_type) {
  4221. case NFS4_DELEG_STID:
  4222. status = nfs_ok;
  4223. break;
  4224. case NFS4_REVOKED_DELEG_STID:
  4225. status = nfserr_deleg_revoked;
  4226. break;
  4227. case NFS4_OPEN_STID:
  4228. case NFS4_LOCK_STID:
  4229. status = nfsd4_check_openowner_confirmed(openlockstateid(s));
  4230. break;
  4231. default:
  4232. printk("unknown stateid type %x\n", s->sc_type);
  4233. /* Fallthrough */
  4234. case NFS4_CLOSED_STID:
  4235. case NFS4_CLOSED_DELEG_STID:
  4236. status = nfserr_bad_stateid;
  4237. }
  4238. out_unlock:
  4239. spin_unlock(&cl->cl_lock);
  4240. return status;
  4241. }
  4242. __be32
  4243. nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
  4244. stateid_t *stateid, unsigned char typemask,
  4245. struct nfs4_stid **s, struct nfsd_net *nn)
  4246. {
  4247. __be32 status;
  4248. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  4249. return nfserr_bad_stateid;
  4250. status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
  4251. if (status == nfserr_stale_clientid) {
  4252. if (cstate->session)
  4253. return nfserr_bad_stateid;
  4254. return nfserr_stale_stateid;
  4255. }
  4256. if (status)
  4257. return status;
  4258. *s = find_stateid_by_type(cstate->clp, stateid, typemask);
  4259. if (!*s)
  4260. return nfserr_bad_stateid;
  4261. return nfs_ok;
  4262. }
  4263. static struct file *
  4264. nfs4_find_file(struct nfs4_stid *s, int flags)
  4265. {
  4266. if (!s)
  4267. return NULL;
  4268. switch (s->sc_type) {
  4269. case NFS4_DELEG_STID:
  4270. if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
  4271. return NULL;
  4272. return get_file(s->sc_file->fi_deleg_file);
  4273. case NFS4_OPEN_STID:
  4274. case NFS4_LOCK_STID:
  4275. if (flags & RD_STATE)
  4276. return find_readable_file(s->sc_file);
  4277. else
  4278. return find_writeable_file(s->sc_file);
  4279. break;
  4280. }
  4281. return NULL;
  4282. }
  4283. static __be32
  4284. nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
  4285. {
  4286. __be32 status;
  4287. status = nfsd4_check_openowner_confirmed(ols);
  4288. if (status)
  4289. return status;
  4290. return nfs4_check_openmode(ols, flags);
  4291. }
  4292. static __be32
  4293. nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
  4294. struct file **filpp, bool *tmp_file, int flags)
  4295. {
  4296. int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
  4297. struct file *file;
  4298. __be32 status;
  4299. file = nfs4_find_file(s, flags);
  4300. if (file) {
  4301. status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  4302. acc | NFSD_MAY_OWNER_OVERRIDE);
  4303. if (status) {
  4304. fput(file);
  4305. return status;
  4306. }
  4307. *filpp = file;
  4308. } else {
  4309. status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
  4310. if (status)
  4311. return status;
  4312. if (tmp_file)
  4313. *tmp_file = true;
  4314. }
  4315. return 0;
  4316. }
  4317. /*
  4318. * Checks for stateid operations
  4319. */
  4320. __be32
  4321. nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
  4322. struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
  4323. stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
  4324. {
  4325. struct inode *ino = d_inode(fhp->fh_dentry);
  4326. struct net *net = SVC_NET(rqstp);
  4327. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4328. struct nfs4_stid *s = NULL;
  4329. __be32 status;
  4330. if (filpp)
  4331. *filpp = NULL;
  4332. if (tmp_file)
  4333. *tmp_file = false;
  4334. if (grace_disallows_io(net, ino))
  4335. return nfserr_grace;
  4336. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
  4337. status = check_special_stateids(net, fhp, stateid, flags);
  4338. goto done;
  4339. }
  4340. status = nfsd4_lookup_stateid(cstate, stateid,
  4341. NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  4342. &s, nn);
  4343. if (status)
  4344. return status;
  4345. status = check_stateid_generation(stateid, &s->sc_stateid,
  4346. nfsd4_has_session(cstate));
  4347. if (status)
  4348. goto out;
  4349. switch (s->sc_type) {
  4350. case NFS4_DELEG_STID:
  4351. status = nfs4_check_delegmode(delegstateid(s), flags);
  4352. break;
  4353. case NFS4_OPEN_STID:
  4354. case NFS4_LOCK_STID:
  4355. status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
  4356. break;
  4357. default:
  4358. status = nfserr_bad_stateid;
  4359. break;
  4360. }
  4361. if (status)
  4362. goto out;
  4363. status = nfs4_check_fh(fhp, s);
  4364. done:
  4365. if (!status && filpp)
  4366. status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
  4367. out:
  4368. if (s)
  4369. nfs4_put_stid(s);
  4370. return status;
  4371. }
  4372. /*
  4373. * Test if the stateid is valid
  4374. */
  4375. __be32
  4376. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4377. struct nfsd4_test_stateid *test_stateid)
  4378. {
  4379. struct nfsd4_test_stateid_id *stateid;
  4380. struct nfs4_client *cl = cstate->session->se_client;
  4381. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  4382. stateid->ts_id_status =
  4383. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  4384. return nfs_ok;
  4385. }
  4386. static __be32
  4387. nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
  4388. {
  4389. struct nfs4_ol_stateid *stp = openlockstateid(s);
  4390. __be32 ret;
  4391. mutex_lock(&stp->st_mutex);
  4392. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  4393. if (ret)
  4394. goto out;
  4395. ret = nfserr_locks_held;
  4396. if (check_for_locks(stp->st_stid.sc_file,
  4397. lockowner(stp->st_stateowner)))
  4398. goto out;
  4399. release_lock_stateid(stp);
  4400. ret = nfs_ok;
  4401. out:
  4402. mutex_unlock(&stp->st_mutex);
  4403. nfs4_put_stid(s);
  4404. return ret;
  4405. }
  4406. __be32
  4407. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4408. struct nfsd4_free_stateid *free_stateid)
  4409. {
  4410. stateid_t *stateid = &free_stateid->fr_stateid;
  4411. struct nfs4_stid *s;
  4412. struct nfs4_delegation *dp;
  4413. struct nfs4_client *cl = cstate->session->se_client;
  4414. __be32 ret = nfserr_bad_stateid;
  4415. spin_lock(&cl->cl_lock);
  4416. s = find_stateid_locked(cl, stateid);
  4417. if (!s)
  4418. goto out_unlock;
  4419. switch (s->sc_type) {
  4420. case NFS4_DELEG_STID:
  4421. ret = nfserr_locks_held;
  4422. break;
  4423. case NFS4_OPEN_STID:
  4424. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  4425. if (ret)
  4426. break;
  4427. ret = nfserr_locks_held;
  4428. break;
  4429. case NFS4_LOCK_STID:
  4430. atomic_inc(&s->sc_count);
  4431. spin_unlock(&cl->cl_lock);
  4432. ret = nfsd4_free_lock_stateid(stateid, s);
  4433. goto out;
  4434. case NFS4_REVOKED_DELEG_STID:
  4435. dp = delegstateid(s);
  4436. list_del_init(&dp->dl_recall_lru);
  4437. spin_unlock(&cl->cl_lock);
  4438. nfs4_put_stid(s);
  4439. ret = nfs_ok;
  4440. goto out;
  4441. /* Default falls through and returns nfserr_bad_stateid */
  4442. }
  4443. out_unlock:
  4444. spin_unlock(&cl->cl_lock);
  4445. out:
  4446. return ret;
  4447. }
  4448. static inline int
  4449. setlkflg (int type)
  4450. {
  4451. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  4452. RD_STATE : WR_STATE;
  4453. }
  4454. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  4455. {
  4456. struct svc_fh *current_fh = &cstate->current_fh;
  4457. struct nfs4_stateowner *sop = stp->st_stateowner;
  4458. __be32 status;
  4459. status = nfsd4_check_seqid(cstate, sop, seqid);
  4460. if (status)
  4461. return status;
  4462. if (stp->st_stid.sc_type == NFS4_CLOSED_STID
  4463. || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
  4464. /*
  4465. * "Closed" stateid's exist *only* to return
  4466. * nfserr_replay_me from the previous step, and
  4467. * revoked delegations are kept only for free_stateid.
  4468. */
  4469. return nfserr_bad_stateid;
  4470. mutex_lock(&stp->st_mutex);
  4471. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  4472. if (status == nfs_ok)
  4473. status = nfs4_check_fh(current_fh, &stp->st_stid);
  4474. if (status != nfs_ok)
  4475. mutex_unlock(&stp->st_mutex);
  4476. return status;
  4477. }
  4478. /*
  4479. * Checks for sequence id mutating operations.
  4480. */
  4481. static __be32
  4482. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  4483. stateid_t *stateid, char typemask,
  4484. struct nfs4_ol_stateid **stpp,
  4485. struct nfsd_net *nn)
  4486. {
  4487. __be32 status;
  4488. struct nfs4_stid *s;
  4489. struct nfs4_ol_stateid *stp = NULL;
  4490. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  4491. seqid, STATEID_VAL(stateid));
  4492. *stpp = NULL;
  4493. status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
  4494. if (status)
  4495. return status;
  4496. stp = openlockstateid(s);
  4497. nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
  4498. status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
  4499. if (!status)
  4500. *stpp = stp;
  4501. else
  4502. nfs4_put_stid(&stp->st_stid);
  4503. return status;
  4504. }
  4505. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  4506. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  4507. {
  4508. __be32 status;
  4509. struct nfs4_openowner *oo;
  4510. struct nfs4_ol_stateid *stp;
  4511. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  4512. NFS4_OPEN_STID, &stp, nn);
  4513. if (status)
  4514. return status;
  4515. oo = openowner(stp->st_stateowner);
  4516. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  4517. mutex_unlock(&stp->st_mutex);
  4518. nfs4_put_stid(&stp->st_stid);
  4519. return nfserr_bad_stateid;
  4520. }
  4521. *stpp = stp;
  4522. return nfs_ok;
  4523. }
  4524. __be32
  4525. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4526. struct nfsd4_open_confirm *oc)
  4527. {
  4528. __be32 status;
  4529. struct nfs4_openowner *oo;
  4530. struct nfs4_ol_stateid *stp;
  4531. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4532. dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
  4533. cstate->current_fh.fh_dentry);
  4534. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  4535. if (status)
  4536. return status;
  4537. status = nfs4_preprocess_seqid_op(cstate,
  4538. oc->oc_seqid, &oc->oc_req_stateid,
  4539. NFS4_OPEN_STID, &stp, nn);
  4540. if (status)
  4541. goto out;
  4542. oo = openowner(stp->st_stateowner);
  4543. status = nfserr_bad_stateid;
  4544. if (oo->oo_flags & NFS4_OO_CONFIRMED) {
  4545. mutex_unlock(&stp->st_mutex);
  4546. goto put_stateid;
  4547. }
  4548. oo->oo_flags |= NFS4_OO_CONFIRMED;
  4549. nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
  4550. mutex_unlock(&stp->st_mutex);
  4551. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  4552. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  4553. nfsd4_client_record_create(oo->oo_owner.so_client);
  4554. status = nfs_ok;
  4555. put_stateid:
  4556. nfs4_put_stid(&stp->st_stid);
  4557. out:
  4558. nfsd4_bump_seqid(cstate, status);
  4559. return status;
  4560. }
  4561. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  4562. {
  4563. if (!test_access(access, stp))
  4564. return;
  4565. nfs4_file_put_access(stp->st_stid.sc_file, access);
  4566. clear_access(access, stp);
  4567. }
  4568. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  4569. {
  4570. switch (to_access) {
  4571. case NFS4_SHARE_ACCESS_READ:
  4572. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  4573. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  4574. break;
  4575. case NFS4_SHARE_ACCESS_WRITE:
  4576. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  4577. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  4578. break;
  4579. case NFS4_SHARE_ACCESS_BOTH:
  4580. break;
  4581. default:
  4582. WARN_ON_ONCE(1);
  4583. }
  4584. }
  4585. __be32
  4586. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  4587. struct nfsd4_compound_state *cstate,
  4588. struct nfsd4_open_downgrade *od)
  4589. {
  4590. __be32 status;
  4591. struct nfs4_ol_stateid *stp;
  4592. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4593. dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
  4594. cstate->current_fh.fh_dentry);
  4595. /* We don't yet support WANT bits: */
  4596. if (od->od_deleg_want)
  4597. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  4598. od->od_deleg_want);
  4599. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  4600. &od->od_stateid, &stp, nn);
  4601. if (status)
  4602. goto out;
  4603. status = nfserr_inval;
  4604. if (!test_access(od->od_share_access, stp)) {
  4605. dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
  4606. stp->st_access_bmap, od->od_share_access);
  4607. goto put_stateid;
  4608. }
  4609. if (!test_deny(od->od_share_deny, stp)) {
  4610. dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
  4611. stp->st_deny_bmap, od->od_share_deny);
  4612. goto put_stateid;
  4613. }
  4614. nfs4_stateid_downgrade(stp, od->od_share_access);
  4615. reset_union_bmap_deny(od->od_share_deny, stp);
  4616. nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
  4617. status = nfs_ok;
  4618. put_stateid:
  4619. mutex_unlock(&stp->st_mutex);
  4620. nfs4_put_stid(&stp->st_stid);
  4621. out:
  4622. nfsd4_bump_seqid(cstate, status);
  4623. return status;
  4624. }
  4625. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  4626. {
  4627. struct nfs4_client *clp = s->st_stid.sc_client;
  4628. bool unhashed;
  4629. LIST_HEAD(reaplist);
  4630. s->st_stid.sc_type = NFS4_CLOSED_STID;
  4631. spin_lock(&clp->cl_lock);
  4632. unhashed = unhash_open_stateid(s, &reaplist);
  4633. if (clp->cl_minorversion) {
  4634. if (unhashed)
  4635. put_ol_stateid_locked(s, &reaplist);
  4636. spin_unlock(&clp->cl_lock);
  4637. free_ol_stateid_reaplist(&reaplist);
  4638. } else {
  4639. spin_unlock(&clp->cl_lock);
  4640. free_ol_stateid_reaplist(&reaplist);
  4641. if (unhashed)
  4642. move_to_close_lru(s, clp->net);
  4643. }
  4644. }
  4645. /*
  4646. * nfs4_unlock_state() called after encode
  4647. */
  4648. __be32
  4649. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4650. struct nfsd4_close *close)
  4651. {
  4652. __be32 status;
  4653. struct nfs4_ol_stateid *stp;
  4654. struct net *net = SVC_NET(rqstp);
  4655. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4656. dprintk("NFSD: nfsd4_close on file %pd\n",
  4657. cstate->current_fh.fh_dentry);
  4658. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  4659. &close->cl_stateid,
  4660. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  4661. &stp, nn);
  4662. nfsd4_bump_seqid(cstate, status);
  4663. if (status)
  4664. goto out;
  4665. nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
  4666. mutex_unlock(&stp->st_mutex);
  4667. nfsd4_close_open_stateid(stp);
  4668. /* put reference from nfs4_preprocess_seqid_op */
  4669. nfs4_put_stid(&stp->st_stid);
  4670. out:
  4671. return status;
  4672. }
  4673. __be32
  4674. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4675. struct nfsd4_delegreturn *dr)
  4676. {
  4677. struct nfs4_delegation *dp;
  4678. stateid_t *stateid = &dr->dr_stateid;
  4679. struct nfs4_stid *s;
  4680. __be32 status;
  4681. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4682. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  4683. return status;
  4684. status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
  4685. if (status)
  4686. goto out;
  4687. dp = delegstateid(s);
  4688. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  4689. if (status)
  4690. goto put_stateid;
  4691. destroy_delegation(dp);
  4692. put_stateid:
  4693. nfs4_put_stid(&dp->dl_stid);
  4694. out:
  4695. return status;
  4696. }
  4697. static inline u64
  4698. end_offset(u64 start, u64 len)
  4699. {
  4700. u64 end;
  4701. end = start + len;
  4702. return end >= start ? end: NFS4_MAX_UINT64;
  4703. }
  4704. /* last octet in a range */
  4705. static inline u64
  4706. last_byte_offset(u64 start, u64 len)
  4707. {
  4708. u64 end;
  4709. WARN_ON_ONCE(!len);
  4710. end = start + len;
  4711. return end > start ? end - 1: NFS4_MAX_UINT64;
  4712. }
  4713. /*
  4714. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  4715. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  4716. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  4717. * locking, this prevents us from being completely protocol-compliant. The
  4718. * real solution to this problem is to start using unsigned file offsets in
  4719. * the VFS, but this is a very deep change!
  4720. */
  4721. static inline void
  4722. nfs4_transform_lock_offset(struct file_lock *lock)
  4723. {
  4724. if (lock->fl_start < 0)
  4725. lock->fl_start = OFFSET_MAX;
  4726. if (lock->fl_end < 0)
  4727. lock->fl_end = OFFSET_MAX;
  4728. }
  4729. static fl_owner_t
  4730. nfsd4_fl_get_owner(fl_owner_t owner)
  4731. {
  4732. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
  4733. nfs4_get_stateowner(&lo->lo_owner);
  4734. return owner;
  4735. }
  4736. static void
  4737. nfsd4_fl_put_owner(fl_owner_t owner)
  4738. {
  4739. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
  4740. if (lo)
  4741. nfs4_put_stateowner(&lo->lo_owner);
  4742. }
  4743. static void
  4744. nfsd4_lm_notify(struct file_lock *fl)
  4745. {
  4746. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
  4747. struct net *net = lo->lo_owner.so_client->net;
  4748. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4749. struct nfsd4_blocked_lock *nbl = container_of(fl,
  4750. struct nfsd4_blocked_lock, nbl_lock);
  4751. bool queue = false;
  4752. /* An empty list means that something else is going to be using it */
  4753. spin_lock(&nn->blocked_locks_lock);
  4754. if (!list_empty(&nbl->nbl_list)) {
  4755. list_del_init(&nbl->nbl_list);
  4756. list_del_init(&nbl->nbl_lru);
  4757. queue = true;
  4758. }
  4759. spin_unlock(&nn->blocked_locks_lock);
  4760. if (queue)
  4761. nfsd4_run_cb(&nbl->nbl_cb);
  4762. }
  4763. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  4764. .lm_notify = nfsd4_lm_notify,
  4765. .lm_get_owner = nfsd4_fl_get_owner,
  4766. .lm_put_owner = nfsd4_fl_put_owner,
  4767. };
  4768. static inline void
  4769. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  4770. {
  4771. struct nfs4_lockowner *lo;
  4772. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  4773. lo = (struct nfs4_lockowner *) fl->fl_owner;
  4774. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  4775. lo->lo_owner.so_owner.len, GFP_KERNEL);
  4776. if (!deny->ld_owner.data)
  4777. /* We just don't care that much */
  4778. goto nevermind;
  4779. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  4780. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  4781. } else {
  4782. nevermind:
  4783. deny->ld_owner.len = 0;
  4784. deny->ld_owner.data = NULL;
  4785. deny->ld_clientid.cl_boot = 0;
  4786. deny->ld_clientid.cl_id = 0;
  4787. }
  4788. deny->ld_start = fl->fl_start;
  4789. deny->ld_length = NFS4_MAX_UINT64;
  4790. if (fl->fl_end != NFS4_MAX_UINT64)
  4791. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  4792. deny->ld_type = NFS4_READ_LT;
  4793. if (fl->fl_type != F_RDLCK)
  4794. deny->ld_type = NFS4_WRITE_LT;
  4795. }
  4796. static struct nfs4_lockowner *
  4797. find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
  4798. {
  4799. unsigned int strhashval = ownerstr_hashval(owner);
  4800. struct nfs4_stateowner *so;
  4801. lockdep_assert_held(&clp->cl_lock);
  4802. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
  4803. so_strhash) {
  4804. if (so->so_is_open_owner)
  4805. continue;
  4806. if (same_owner_str(so, owner))
  4807. return lockowner(nfs4_get_stateowner(so));
  4808. }
  4809. return NULL;
  4810. }
  4811. static struct nfs4_lockowner *
  4812. find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
  4813. {
  4814. struct nfs4_lockowner *lo;
  4815. spin_lock(&clp->cl_lock);
  4816. lo = find_lockowner_str_locked(clp, owner);
  4817. spin_unlock(&clp->cl_lock);
  4818. return lo;
  4819. }
  4820. static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
  4821. {
  4822. unhash_lockowner_locked(lockowner(sop));
  4823. }
  4824. static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
  4825. {
  4826. struct nfs4_lockowner *lo = lockowner(sop);
  4827. kmem_cache_free(lockowner_slab, lo);
  4828. }
  4829. static const struct nfs4_stateowner_operations lockowner_ops = {
  4830. .so_unhash = nfs4_unhash_lockowner,
  4831. .so_free = nfs4_free_lockowner,
  4832. };
  4833. /*
  4834. * Alloc a lock owner structure.
  4835. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  4836. * occurred.
  4837. *
  4838. * strhashval = ownerstr_hashval
  4839. */
  4840. static struct nfs4_lockowner *
  4841. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
  4842. struct nfs4_ol_stateid *open_stp,
  4843. struct nfsd4_lock *lock)
  4844. {
  4845. struct nfs4_lockowner *lo, *ret;
  4846. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  4847. if (!lo)
  4848. return NULL;
  4849. INIT_LIST_HEAD(&lo->lo_blocked);
  4850. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  4851. lo->lo_owner.so_is_open_owner = 0;
  4852. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
  4853. lo->lo_owner.so_ops = &lockowner_ops;
  4854. spin_lock(&clp->cl_lock);
  4855. ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
  4856. if (ret == NULL) {
  4857. list_add(&lo->lo_owner.so_strhash,
  4858. &clp->cl_ownerstr_hashtbl[strhashval]);
  4859. ret = lo;
  4860. } else
  4861. nfs4_free_stateowner(&lo->lo_owner);
  4862. spin_unlock(&clp->cl_lock);
  4863. return ret;
  4864. }
  4865. static void
  4866. init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
  4867. struct nfs4_file *fp, struct inode *inode,
  4868. struct nfs4_ol_stateid *open_stp)
  4869. {
  4870. struct nfs4_client *clp = lo->lo_owner.so_client;
  4871. lockdep_assert_held(&clp->cl_lock);
  4872. atomic_inc(&stp->st_stid.sc_count);
  4873. stp->st_stid.sc_type = NFS4_LOCK_STID;
  4874. stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
  4875. get_nfs4_file(fp);
  4876. stp->st_stid.sc_file = fp;
  4877. stp->st_access_bmap = 0;
  4878. stp->st_deny_bmap = open_stp->st_deny_bmap;
  4879. stp->st_openstp = open_stp;
  4880. mutex_init(&stp->st_mutex);
  4881. list_add(&stp->st_locks, &open_stp->st_locks);
  4882. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  4883. spin_lock(&fp->fi_lock);
  4884. list_add(&stp->st_perfile, &fp->fi_stateids);
  4885. spin_unlock(&fp->fi_lock);
  4886. }
  4887. static struct nfs4_ol_stateid *
  4888. find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
  4889. {
  4890. struct nfs4_ol_stateid *lst;
  4891. struct nfs4_client *clp = lo->lo_owner.so_client;
  4892. lockdep_assert_held(&clp->cl_lock);
  4893. list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
  4894. if (lst->st_stid.sc_file == fp) {
  4895. atomic_inc(&lst->st_stid.sc_count);
  4896. return lst;
  4897. }
  4898. }
  4899. return NULL;
  4900. }
  4901. static struct nfs4_ol_stateid *
  4902. find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
  4903. struct inode *inode, struct nfs4_ol_stateid *ost,
  4904. bool *new)
  4905. {
  4906. struct nfs4_stid *ns = NULL;
  4907. struct nfs4_ol_stateid *lst;
  4908. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  4909. struct nfs4_client *clp = oo->oo_owner.so_client;
  4910. spin_lock(&clp->cl_lock);
  4911. lst = find_lock_stateid(lo, fi);
  4912. if (lst == NULL) {
  4913. spin_unlock(&clp->cl_lock);
  4914. ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
  4915. if (ns == NULL)
  4916. return NULL;
  4917. spin_lock(&clp->cl_lock);
  4918. lst = find_lock_stateid(lo, fi);
  4919. if (likely(!lst)) {
  4920. lst = openlockstateid(ns);
  4921. init_lock_stateid(lst, lo, fi, inode, ost);
  4922. ns = NULL;
  4923. *new = true;
  4924. }
  4925. }
  4926. spin_unlock(&clp->cl_lock);
  4927. if (ns)
  4928. nfs4_put_stid(ns);
  4929. return lst;
  4930. }
  4931. static int
  4932. check_lock_length(u64 offset, u64 length)
  4933. {
  4934. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  4935. (length > ~offset)));
  4936. }
  4937. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  4938. {
  4939. struct nfs4_file *fp = lock_stp->st_stid.sc_file;
  4940. lockdep_assert_held(&fp->fi_lock);
  4941. if (test_access(access, lock_stp))
  4942. return;
  4943. __nfs4_file_get_access(fp, access);
  4944. set_access(access, lock_stp);
  4945. }
  4946. static __be32
  4947. lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
  4948. struct nfs4_ol_stateid *ost,
  4949. struct nfsd4_lock *lock,
  4950. struct nfs4_ol_stateid **plst, bool *new)
  4951. {
  4952. __be32 status;
  4953. struct nfs4_file *fi = ost->st_stid.sc_file;
  4954. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  4955. struct nfs4_client *cl = oo->oo_owner.so_client;
  4956. struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
  4957. struct nfs4_lockowner *lo;
  4958. struct nfs4_ol_stateid *lst;
  4959. unsigned int strhashval;
  4960. bool hashed;
  4961. lo = find_lockowner_str(cl, &lock->lk_new_owner);
  4962. if (!lo) {
  4963. strhashval = ownerstr_hashval(&lock->lk_new_owner);
  4964. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  4965. if (lo == NULL)
  4966. return nfserr_jukebox;
  4967. } else {
  4968. /* with an existing lockowner, seqids must be the same */
  4969. status = nfserr_bad_seqid;
  4970. if (!cstate->minorversion &&
  4971. lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
  4972. goto out;
  4973. }
  4974. retry:
  4975. lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
  4976. if (lst == NULL) {
  4977. status = nfserr_jukebox;
  4978. goto out;
  4979. }
  4980. mutex_lock(&lst->st_mutex);
  4981. /* See if it's still hashed to avoid race with FREE_STATEID */
  4982. spin_lock(&cl->cl_lock);
  4983. hashed = !list_empty(&lst->st_perfile);
  4984. spin_unlock(&cl->cl_lock);
  4985. if (!hashed) {
  4986. mutex_unlock(&lst->st_mutex);
  4987. nfs4_put_stid(&lst->st_stid);
  4988. goto retry;
  4989. }
  4990. status = nfs_ok;
  4991. *plst = lst;
  4992. out:
  4993. nfs4_put_stateowner(&lo->lo_owner);
  4994. return status;
  4995. }
  4996. /*
  4997. * LOCK operation
  4998. */
  4999. __be32
  5000. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  5001. struct nfsd4_lock *lock)
  5002. {
  5003. struct nfs4_openowner *open_sop = NULL;
  5004. struct nfs4_lockowner *lock_sop = NULL;
  5005. struct nfs4_ol_stateid *lock_stp = NULL;
  5006. struct nfs4_ol_stateid *open_stp = NULL;
  5007. struct nfs4_file *fp;
  5008. struct file *filp = NULL;
  5009. struct nfsd4_blocked_lock *nbl = NULL;
  5010. struct file_lock *file_lock = NULL;
  5011. struct file_lock *conflock = NULL;
  5012. __be32 status = 0;
  5013. int lkflg;
  5014. int err;
  5015. bool new = false;
  5016. unsigned char fl_type;
  5017. unsigned int fl_flags = FL_POSIX;
  5018. struct net *net = SVC_NET(rqstp);
  5019. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5020. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  5021. (long long) lock->lk_offset,
  5022. (long long) lock->lk_length);
  5023. if (check_lock_length(lock->lk_offset, lock->lk_length))
  5024. return nfserr_inval;
  5025. if ((status = fh_verify(rqstp, &cstate->current_fh,
  5026. S_IFREG, NFSD_MAY_LOCK))) {
  5027. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  5028. return status;
  5029. }
  5030. if (lock->lk_is_new) {
  5031. if (nfsd4_has_session(cstate))
  5032. /* See rfc 5661 18.10.3: given clientid is ignored: */
  5033. memcpy(&lock->lk_new_clientid,
  5034. &cstate->session->se_client->cl_clientid,
  5035. sizeof(clientid_t));
  5036. status = nfserr_stale_clientid;
  5037. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  5038. goto out;
  5039. /* validate and update open stateid and open seqid */
  5040. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  5041. lock->lk_new_open_seqid,
  5042. &lock->lk_new_open_stateid,
  5043. &open_stp, nn);
  5044. if (status)
  5045. goto out;
  5046. mutex_unlock(&open_stp->st_mutex);
  5047. open_sop = openowner(open_stp->st_stateowner);
  5048. status = nfserr_bad_stateid;
  5049. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  5050. &lock->lk_new_clientid))
  5051. goto out;
  5052. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  5053. &lock_stp, &new);
  5054. } else {
  5055. status = nfs4_preprocess_seqid_op(cstate,
  5056. lock->lk_old_lock_seqid,
  5057. &lock->lk_old_lock_stateid,
  5058. NFS4_LOCK_STID, &lock_stp, nn);
  5059. }
  5060. if (status)
  5061. goto out;
  5062. lock_sop = lockowner(lock_stp->st_stateowner);
  5063. lkflg = setlkflg(lock->lk_type);
  5064. status = nfs4_check_openmode(lock_stp, lkflg);
  5065. if (status)
  5066. goto out;
  5067. status = nfserr_grace;
  5068. if (locks_in_grace(net) && !lock->lk_reclaim)
  5069. goto out;
  5070. status = nfserr_no_grace;
  5071. if (!locks_in_grace(net) && lock->lk_reclaim)
  5072. goto out;
  5073. fp = lock_stp->st_stid.sc_file;
  5074. switch (lock->lk_type) {
  5075. case NFS4_READW_LT:
  5076. if (nfsd4_has_session(cstate))
  5077. fl_flags |= FL_SLEEP;
  5078. /* Fallthrough */
  5079. case NFS4_READ_LT:
  5080. spin_lock(&fp->fi_lock);
  5081. filp = find_readable_file_locked(fp);
  5082. if (filp)
  5083. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  5084. spin_unlock(&fp->fi_lock);
  5085. fl_type = F_RDLCK;
  5086. break;
  5087. case NFS4_WRITEW_LT:
  5088. if (nfsd4_has_session(cstate))
  5089. fl_flags |= FL_SLEEP;
  5090. /* Fallthrough */
  5091. case NFS4_WRITE_LT:
  5092. spin_lock(&fp->fi_lock);
  5093. filp = find_writeable_file_locked(fp);
  5094. if (filp)
  5095. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  5096. spin_unlock(&fp->fi_lock);
  5097. fl_type = F_WRLCK;
  5098. break;
  5099. default:
  5100. status = nfserr_inval;
  5101. goto out;
  5102. }
  5103. if (!filp) {
  5104. status = nfserr_openmode;
  5105. goto out;
  5106. }
  5107. nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
  5108. if (!nbl) {
  5109. dprintk("NFSD: %s: unable to allocate block!\n", __func__);
  5110. status = nfserr_jukebox;
  5111. goto out;
  5112. }
  5113. file_lock = &nbl->nbl_lock;
  5114. file_lock->fl_type = fl_type;
  5115. file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
  5116. file_lock->fl_pid = current->tgid;
  5117. file_lock->fl_file = filp;
  5118. file_lock->fl_flags = fl_flags;
  5119. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  5120. file_lock->fl_start = lock->lk_offset;
  5121. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  5122. nfs4_transform_lock_offset(file_lock);
  5123. conflock = locks_alloc_lock();
  5124. if (!conflock) {
  5125. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  5126. status = nfserr_jukebox;
  5127. goto out;
  5128. }
  5129. if (fl_flags & FL_SLEEP) {
  5130. nbl->nbl_time = jiffies;
  5131. spin_lock(&nn->blocked_locks_lock);
  5132. list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
  5133. list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
  5134. spin_unlock(&nn->blocked_locks_lock);
  5135. }
  5136. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  5137. switch (err) {
  5138. case 0: /* success! */
  5139. nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
  5140. status = 0;
  5141. break;
  5142. case FILE_LOCK_DEFERRED:
  5143. nbl = NULL;
  5144. /* Fallthrough */
  5145. case -EAGAIN: /* conflock holds conflicting lock */
  5146. status = nfserr_denied;
  5147. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  5148. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  5149. break;
  5150. case -EDEADLK:
  5151. status = nfserr_deadlock;
  5152. break;
  5153. default:
  5154. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  5155. status = nfserrno(err);
  5156. break;
  5157. }
  5158. out:
  5159. if (nbl) {
  5160. /* dequeue it if we queued it before */
  5161. if (fl_flags & FL_SLEEP) {
  5162. spin_lock(&nn->blocked_locks_lock);
  5163. list_del_init(&nbl->nbl_list);
  5164. list_del_init(&nbl->nbl_lru);
  5165. spin_unlock(&nn->blocked_locks_lock);
  5166. }
  5167. free_blocked_lock(nbl);
  5168. }
  5169. if (filp)
  5170. fput(filp);
  5171. if (lock_stp) {
  5172. /* Bump seqid manually if the 4.0 replay owner is openowner */
  5173. if (cstate->replay_owner &&
  5174. cstate->replay_owner != &lock_sop->lo_owner &&
  5175. seqid_mutating_err(ntohl(status)))
  5176. lock_sop->lo_owner.so_seqid++;
  5177. mutex_unlock(&lock_stp->st_mutex);
  5178. /*
  5179. * If this is a new, never-before-used stateid, and we are
  5180. * returning an error, then just go ahead and release it.
  5181. */
  5182. if (status && new)
  5183. release_lock_stateid(lock_stp);
  5184. nfs4_put_stid(&lock_stp->st_stid);
  5185. }
  5186. if (open_stp)
  5187. nfs4_put_stid(&open_stp->st_stid);
  5188. nfsd4_bump_seqid(cstate, status);
  5189. if (conflock)
  5190. locks_free_lock(conflock);
  5191. return status;
  5192. }
  5193. /*
  5194. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  5195. * so we do a temporary open here just to get an open file to pass to
  5196. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  5197. * inode operation.)
  5198. */
  5199. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  5200. {
  5201. struct file *file;
  5202. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  5203. if (!err) {
  5204. err = nfserrno(vfs_test_lock(file, lock));
  5205. fput(file);
  5206. }
  5207. return err;
  5208. }
  5209. /*
  5210. * LOCKT operation
  5211. */
  5212. __be32
  5213. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  5214. struct nfsd4_lockt *lockt)
  5215. {
  5216. struct file_lock *file_lock = NULL;
  5217. struct nfs4_lockowner *lo = NULL;
  5218. __be32 status;
  5219. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  5220. if (locks_in_grace(SVC_NET(rqstp)))
  5221. return nfserr_grace;
  5222. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  5223. return nfserr_inval;
  5224. if (!nfsd4_has_session(cstate)) {
  5225. status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
  5226. if (status)
  5227. goto out;
  5228. }
  5229. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  5230. goto out;
  5231. file_lock = locks_alloc_lock();
  5232. if (!file_lock) {
  5233. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  5234. status = nfserr_jukebox;
  5235. goto out;
  5236. }
  5237. switch (lockt->lt_type) {
  5238. case NFS4_READ_LT:
  5239. case NFS4_READW_LT:
  5240. file_lock->fl_type = F_RDLCK;
  5241. break;
  5242. case NFS4_WRITE_LT:
  5243. case NFS4_WRITEW_LT:
  5244. file_lock->fl_type = F_WRLCK;
  5245. break;
  5246. default:
  5247. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  5248. status = nfserr_inval;
  5249. goto out;
  5250. }
  5251. lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
  5252. if (lo)
  5253. file_lock->fl_owner = (fl_owner_t)lo;
  5254. file_lock->fl_pid = current->tgid;
  5255. file_lock->fl_flags = FL_POSIX;
  5256. file_lock->fl_start = lockt->lt_offset;
  5257. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  5258. nfs4_transform_lock_offset(file_lock);
  5259. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  5260. if (status)
  5261. goto out;
  5262. if (file_lock->fl_type != F_UNLCK) {
  5263. status = nfserr_denied;
  5264. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  5265. }
  5266. out:
  5267. if (lo)
  5268. nfs4_put_stateowner(&lo->lo_owner);
  5269. if (file_lock)
  5270. locks_free_lock(file_lock);
  5271. return status;
  5272. }
  5273. __be32
  5274. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  5275. struct nfsd4_locku *locku)
  5276. {
  5277. struct nfs4_ol_stateid *stp;
  5278. struct file *filp = NULL;
  5279. struct file_lock *file_lock = NULL;
  5280. __be32 status;
  5281. int err;
  5282. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  5283. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  5284. (long long) locku->lu_offset,
  5285. (long long) locku->lu_length);
  5286. if (check_lock_length(locku->lu_offset, locku->lu_length))
  5287. return nfserr_inval;
  5288. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  5289. &locku->lu_stateid, NFS4_LOCK_STID,
  5290. &stp, nn);
  5291. if (status)
  5292. goto out;
  5293. filp = find_any_file(stp->st_stid.sc_file);
  5294. if (!filp) {
  5295. status = nfserr_lock_range;
  5296. goto put_stateid;
  5297. }
  5298. file_lock = locks_alloc_lock();
  5299. if (!file_lock) {
  5300. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  5301. status = nfserr_jukebox;
  5302. goto fput;
  5303. }
  5304. file_lock->fl_type = F_UNLCK;
  5305. file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
  5306. file_lock->fl_pid = current->tgid;
  5307. file_lock->fl_file = filp;
  5308. file_lock->fl_flags = FL_POSIX;
  5309. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  5310. file_lock->fl_start = locku->lu_offset;
  5311. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  5312. locku->lu_length);
  5313. nfs4_transform_lock_offset(file_lock);
  5314. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  5315. if (err) {
  5316. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  5317. goto out_nfserr;
  5318. }
  5319. nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
  5320. fput:
  5321. fput(filp);
  5322. put_stateid:
  5323. mutex_unlock(&stp->st_mutex);
  5324. nfs4_put_stid(&stp->st_stid);
  5325. out:
  5326. nfsd4_bump_seqid(cstate, status);
  5327. if (file_lock)
  5328. locks_free_lock(file_lock);
  5329. return status;
  5330. out_nfserr:
  5331. status = nfserrno(err);
  5332. goto fput;
  5333. }
  5334. /*
  5335. * returns
  5336. * true: locks held by lockowner
  5337. * false: no locks held by lockowner
  5338. */
  5339. static bool
  5340. check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
  5341. {
  5342. struct file_lock *fl;
  5343. int status = false;
  5344. struct file *filp = find_any_file(fp);
  5345. struct inode *inode;
  5346. struct file_lock_context *flctx;
  5347. if (!filp) {
  5348. /* Any valid lock stateid should have some sort of access */
  5349. WARN_ON_ONCE(1);
  5350. return status;
  5351. }
  5352. inode = file_inode(filp);
  5353. flctx = inode->i_flctx;
  5354. if (flctx && !list_empty_careful(&flctx->flc_posix)) {
  5355. spin_lock(&flctx->flc_lock);
  5356. list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
  5357. if (fl->fl_owner == (fl_owner_t)lowner) {
  5358. status = true;
  5359. break;
  5360. }
  5361. }
  5362. spin_unlock(&flctx->flc_lock);
  5363. }
  5364. fput(filp);
  5365. return status;
  5366. }
  5367. __be32
  5368. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  5369. struct nfsd4_compound_state *cstate,
  5370. struct nfsd4_release_lockowner *rlockowner)
  5371. {
  5372. clientid_t *clid = &rlockowner->rl_clientid;
  5373. struct nfs4_stateowner *sop;
  5374. struct nfs4_lockowner *lo = NULL;
  5375. struct nfs4_ol_stateid *stp;
  5376. struct xdr_netobj *owner = &rlockowner->rl_owner;
  5377. unsigned int hashval = ownerstr_hashval(owner);
  5378. __be32 status;
  5379. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  5380. struct nfs4_client *clp;
  5381. LIST_HEAD (reaplist);
  5382. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  5383. clid->cl_boot, clid->cl_id);
  5384. status = lookup_clientid(clid, cstate, nn);
  5385. if (status)
  5386. return status;
  5387. clp = cstate->clp;
  5388. /* Find the matching lock stateowner */
  5389. spin_lock(&clp->cl_lock);
  5390. list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
  5391. so_strhash) {
  5392. if (sop->so_is_open_owner || !same_owner_str(sop, owner))
  5393. continue;
  5394. /* see if there are still any locks associated with it */
  5395. lo = lockowner(sop);
  5396. list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
  5397. if (check_for_locks(stp->st_stid.sc_file, lo)) {
  5398. status = nfserr_locks_held;
  5399. spin_unlock(&clp->cl_lock);
  5400. return status;
  5401. }
  5402. }
  5403. nfs4_get_stateowner(sop);
  5404. break;
  5405. }
  5406. if (!lo) {
  5407. spin_unlock(&clp->cl_lock);
  5408. return status;
  5409. }
  5410. unhash_lockowner_locked(lo);
  5411. while (!list_empty(&lo->lo_owner.so_stateids)) {
  5412. stp = list_first_entry(&lo->lo_owner.so_stateids,
  5413. struct nfs4_ol_stateid,
  5414. st_perstateowner);
  5415. WARN_ON(!unhash_lock_stateid(stp));
  5416. put_ol_stateid_locked(stp, &reaplist);
  5417. }
  5418. spin_unlock(&clp->cl_lock);
  5419. free_ol_stateid_reaplist(&reaplist);
  5420. nfs4_put_stateowner(&lo->lo_owner);
  5421. return status;
  5422. }
  5423. static inline struct nfs4_client_reclaim *
  5424. alloc_reclaim(void)
  5425. {
  5426. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  5427. }
  5428. bool
  5429. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  5430. {
  5431. struct nfs4_client_reclaim *crp;
  5432. crp = nfsd4_find_reclaim_client(name, nn);
  5433. return (crp && crp->cr_clp);
  5434. }
  5435. /*
  5436. * failure => all reset bets are off, nfserr_no_grace...
  5437. */
  5438. struct nfs4_client_reclaim *
  5439. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  5440. {
  5441. unsigned int strhashval;
  5442. struct nfs4_client_reclaim *crp;
  5443. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  5444. crp = alloc_reclaim();
  5445. if (crp) {
  5446. strhashval = clientstr_hashval(name);
  5447. INIT_LIST_HEAD(&crp->cr_strhash);
  5448. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  5449. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  5450. crp->cr_clp = NULL;
  5451. nn->reclaim_str_hashtbl_size++;
  5452. }
  5453. return crp;
  5454. }
  5455. void
  5456. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  5457. {
  5458. list_del(&crp->cr_strhash);
  5459. kfree(crp);
  5460. nn->reclaim_str_hashtbl_size--;
  5461. }
  5462. void
  5463. nfs4_release_reclaim(struct nfsd_net *nn)
  5464. {
  5465. struct nfs4_client_reclaim *crp = NULL;
  5466. int i;
  5467. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5468. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  5469. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  5470. struct nfs4_client_reclaim, cr_strhash);
  5471. nfs4_remove_reclaim_record(crp, nn);
  5472. }
  5473. }
  5474. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  5475. }
  5476. /*
  5477. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  5478. struct nfs4_client_reclaim *
  5479. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  5480. {
  5481. unsigned int strhashval;
  5482. struct nfs4_client_reclaim *crp = NULL;
  5483. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  5484. strhashval = clientstr_hashval(recdir);
  5485. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  5486. if (same_name(crp->cr_recdir, recdir)) {
  5487. return crp;
  5488. }
  5489. }
  5490. return NULL;
  5491. }
  5492. /*
  5493. * Called from OPEN. Look for clientid in reclaim list.
  5494. */
  5495. __be32
  5496. nfs4_check_open_reclaim(clientid_t *clid,
  5497. struct nfsd4_compound_state *cstate,
  5498. struct nfsd_net *nn)
  5499. {
  5500. __be32 status;
  5501. /* find clientid in conf_id_hashtbl */
  5502. status = lookup_clientid(clid, cstate, nn);
  5503. if (status)
  5504. return nfserr_reclaim_bad;
  5505. if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
  5506. return nfserr_no_grace;
  5507. if (nfsd4_client_record_check(cstate->clp))
  5508. return nfserr_reclaim_bad;
  5509. return nfs_ok;
  5510. }
  5511. #ifdef CONFIG_NFSD_FAULT_INJECTION
  5512. static inline void
  5513. put_client(struct nfs4_client *clp)
  5514. {
  5515. atomic_dec(&clp->cl_refcount);
  5516. }
  5517. static struct nfs4_client *
  5518. nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  5519. {
  5520. struct nfs4_client *clp;
  5521. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5522. nfsd_net_id);
  5523. if (!nfsd_netns_ready(nn))
  5524. return NULL;
  5525. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5526. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  5527. return clp;
  5528. }
  5529. return NULL;
  5530. }
  5531. u64
  5532. nfsd_inject_print_clients(void)
  5533. {
  5534. struct nfs4_client *clp;
  5535. u64 count = 0;
  5536. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5537. nfsd_net_id);
  5538. char buf[INET6_ADDRSTRLEN];
  5539. if (!nfsd_netns_ready(nn))
  5540. return 0;
  5541. spin_lock(&nn->client_lock);
  5542. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5543. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  5544. pr_info("NFS Client: %s\n", buf);
  5545. ++count;
  5546. }
  5547. spin_unlock(&nn->client_lock);
  5548. return count;
  5549. }
  5550. u64
  5551. nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
  5552. {
  5553. u64 count = 0;
  5554. struct nfs4_client *clp;
  5555. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5556. nfsd_net_id);
  5557. if (!nfsd_netns_ready(nn))
  5558. return count;
  5559. spin_lock(&nn->client_lock);
  5560. clp = nfsd_find_client(addr, addr_size);
  5561. if (clp) {
  5562. if (mark_client_expired_locked(clp) == nfs_ok)
  5563. ++count;
  5564. else
  5565. clp = NULL;
  5566. }
  5567. spin_unlock(&nn->client_lock);
  5568. if (clp)
  5569. expire_client(clp);
  5570. return count;
  5571. }
  5572. u64
  5573. nfsd_inject_forget_clients(u64 max)
  5574. {
  5575. u64 count = 0;
  5576. struct nfs4_client *clp, *next;
  5577. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5578. nfsd_net_id);
  5579. LIST_HEAD(reaplist);
  5580. if (!nfsd_netns_ready(nn))
  5581. return count;
  5582. spin_lock(&nn->client_lock);
  5583. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  5584. if (mark_client_expired_locked(clp) == nfs_ok) {
  5585. list_add(&clp->cl_lru, &reaplist);
  5586. if (max != 0 && ++count >= max)
  5587. break;
  5588. }
  5589. }
  5590. spin_unlock(&nn->client_lock);
  5591. list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
  5592. expire_client(clp);
  5593. return count;
  5594. }
  5595. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  5596. const char *type)
  5597. {
  5598. char buf[INET6_ADDRSTRLEN];
  5599. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  5600. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  5601. }
  5602. static void
  5603. nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
  5604. struct list_head *collect)
  5605. {
  5606. struct nfs4_client *clp = lst->st_stid.sc_client;
  5607. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5608. nfsd_net_id);
  5609. if (!collect)
  5610. return;
  5611. lockdep_assert_held(&nn->client_lock);
  5612. atomic_inc(&clp->cl_refcount);
  5613. list_add(&lst->st_locks, collect);
  5614. }
  5615. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
  5616. struct list_head *collect,
  5617. bool (*func)(struct nfs4_ol_stateid *))
  5618. {
  5619. struct nfs4_openowner *oop;
  5620. struct nfs4_ol_stateid *stp, *st_next;
  5621. struct nfs4_ol_stateid *lst, *lst_next;
  5622. u64 count = 0;
  5623. spin_lock(&clp->cl_lock);
  5624. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  5625. list_for_each_entry_safe(stp, st_next,
  5626. &oop->oo_owner.so_stateids, st_perstateowner) {
  5627. list_for_each_entry_safe(lst, lst_next,
  5628. &stp->st_locks, st_locks) {
  5629. if (func) {
  5630. if (func(lst))
  5631. nfsd_inject_add_lock_to_list(lst,
  5632. collect);
  5633. }
  5634. ++count;
  5635. /*
  5636. * Despite the fact that these functions deal
  5637. * with 64-bit integers for "count", we must
  5638. * ensure that it doesn't blow up the
  5639. * clp->cl_refcount. Throw a warning if we
  5640. * start to approach INT_MAX here.
  5641. */
  5642. WARN_ON_ONCE(count == (INT_MAX / 2));
  5643. if (count == max)
  5644. goto out;
  5645. }
  5646. }
  5647. }
  5648. out:
  5649. spin_unlock(&clp->cl_lock);
  5650. return count;
  5651. }
  5652. static u64
  5653. nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
  5654. u64 max)
  5655. {
  5656. return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
  5657. }
  5658. static u64
  5659. nfsd_print_client_locks(struct nfs4_client *clp)
  5660. {
  5661. u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
  5662. nfsd_print_count(clp, count, "locked files");
  5663. return count;
  5664. }
  5665. u64
  5666. nfsd_inject_print_locks(void)
  5667. {
  5668. struct nfs4_client *clp;
  5669. u64 count = 0;
  5670. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5671. nfsd_net_id);
  5672. if (!nfsd_netns_ready(nn))
  5673. return 0;
  5674. spin_lock(&nn->client_lock);
  5675. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5676. count += nfsd_print_client_locks(clp);
  5677. spin_unlock(&nn->client_lock);
  5678. return count;
  5679. }
  5680. static void
  5681. nfsd_reap_locks(struct list_head *reaplist)
  5682. {
  5683. struct nfs4_client *clp;
  5684. struct nfs4_ol_stateid *stp, *next;
  5685. list_for_each_entry_safe(stp, next, reaplist, st_locks) {
  5686. list_del_init(&stp->st_locks);
  5687. clp = stp->st_stid.sc_client;
  5688. nfs4_put_stid(&stp->st_stid);
  5689. put_client(clp);
  5690. }
  5691. }
  5692. u64
  5693. nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
  5694. {
  5695. unsigned int count = 0;
  5696. struct nfs4_client *clp;
  5697. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5698. nfsd_net_id);
  5699. LIST_HEAD(reaplist);
  5700. if (!nfsd_netns_ready(nn))
  5701. return count;
  5702. spin_lock(&nn->client_lock);
  5703. clp = nfsd_find_client(addr, addr_size);
  5704. if (clp)
  5705. count = nfsd_collect_client_locks(clp, &reaplist, 0);
  5706. spin_unlock(&nn->client_lock);
  5707. nfsd_reap_locks(&reaplist);
  5708. return count;
  5709. }
  5710. u64
  5711. nfsd_inject_forget_locks(u64 max)
  5712. {
  5713. u64 count = 0;
  5714. struct nfs4_client *clp;
  5715. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5716. nfsd_net_id);
  5717. LIST_HEAD(reaplist);
  5718. if (!nfsd_netns_ready(nn))
  5719. return count;
  5720. spin_lock(&nn->client_lock);
  5721. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5722. count += nfsd_collect_client_locks(clp, &reaplist, max - count);
  5723. if (max != 0 && count >= max)
  5724. break;
  5725. }
  5726. spin_unlock(&nn->client_lock);
  5727. nfsd_reap_locks(&reaplist);
  5728. return count;
  5729. }
  5730. static u64
  5731. nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
  5732. struct list_head *collect,
  5733. void (*func)(struct nfs4_openowner *))
  5734. {
  5735. struct nfs4_openowner *oop, *next;
  5736. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5737. nfsd_net_id);
  5738. u64 count = 0;
  5739. lockdep_assert_held(&nn->client_lock);
  5740. spin_lock(&clp->cl_lock);
  5741. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  5742. if (func) {
  5743. func(oop);
  5744. if (collect) {
  5745. atomic_inc(&clp->cl_refcount);
  5746. list_add(&oop->oo_perclient, collect);
  5747. }
  5748. }
  5749. ++count;
  5750. /*
  5751. * Despite the fact that these functions deal with
  5752. * 64-bit integers for "count", we must ensure that
  5753. * it doesn't blow up the clp->cl_refcount. Throw a
  5754. * warning if we start to approach INT_MAX here.
  5755. */
  5756. WARN_ON_ONCE(count == (INT_MAX / 2));
  5757. if (count == max)
  5758. break;
  5759. }
  5760. spin_unlock(&clp->cl_lock);
  5761. return count;
  5762. }
  5763. static u64
  5764. nfsd_print_client_openowners(struct nfs4_client *clp)
  5765. {
  5766. u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
  5767. nfsd_print_count(clp, count, "openowners");
  5768. return count;
  5769. }
  5770. static u64
  5771. nfsd_collect_client_openowners(struct nfs4_client *clp,
  5772. struct list_head *collect, u64 max)
  5773. {
  5774. return nfsd_foreach_client_openowner(clp, max, collect,
  5775. unhash_openowner_locked);
  5776. }
  5777. u64
  5778. nfsd_inject_print_openowners(void)
  5779. {
  5780. struct nfs4_client *clp;
  5781. u64 count = 0;
  5782. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5783. nfsd_net_id);
  5784. if (!nfsd_netns_ready(nn))
  5785. return 0;
  5786. spin_lock(&nn->client_lock);
  5787. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5788. count += nfsd_print_client_openowners(clp);
  5789. spin_unlock(&nn->client_lock);
  5790. return count;
  5791. }
  5792. static void
  5793. nfsd_reap_openowners(struct list_head *reaplist)
  5794. {
  5795. struct nfs4_client *clp;
  5796. struct nfs4_openowner *oop, *next;
  5797. list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
  5798. list_del_init(&oop->oo_perclient);
  5799. clp = oop->oo_owner.so_client;
  5800. release_openowner(oop);
  5801. put_client(clp);
  5802. }
  5803. }
  5804. u64
  5805. nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
  5806. size_t addr_size)
  5807. {
  5808. unsigned int count = 0;
  5809. struct nfs4_client *clp;
  5810. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5811. nfsd_net_id);
  5812. LIST_HEAD(reaplist);
  5813. if (!nfsd_netns_ready(nn))
  5814. return count;
  5815. spin_lock(&nn->client_lock);
  5816. clp = nfsd_find_client(addr, addr_size);
  5817. if (clp)
  5818. count = nfsd_collect_client_openowners(clp, &reaplist, 0);
  5819. spin_unlock(&nn->client_lock);
  5820. nfsd_reap_openowners(&reaplist);
  5821. return count;
  5822. }
  5823. u64
  5824. nfsd_inject_forget_openowners(u64 max)
  5825. {
  5826. u64 count = 0;
  5827. struct nfs4_client *clp;
  5828. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5829. nfsd_net_id);
  5830. LIST_HEAD(reaplist);
  5831. if (!nfsd_netns_ready(nn))
  5832. return count;
  5833. spin_lock(&nn->client_lock);
  5834. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5835. count += nfsd_collect_client_openowners(clp, &reaplist,
  5836. max - count);
  5837. if (max != 0 && count >= max)
  5838. break;
  5839. }
  5840. spin_unlock(&nn->client_lock);
  5841. nfsd_reap_openowners(&reaplist);
  5842. return count;
  5843. }
  5844. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  5845. struct list_head *victims)
  5846. {
  5847. struct nfs4_delegation *dp, *next;
  5848. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5849. nfsd_net_id);
  5850. u64 count = 0;
  5851. lockdep_assert_held(&nn->client_lock);
  5852. spin_lock(&state_lock);
  5853. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  5854. if (victims) {
  5855. /*
  5856. * It's not safe to mess with delegations that have a
  5857. * non-zero dl_time. They might have already been broken
  5858. * and could be processed by the laundromat outside of
  5859. * the state_lock. Just leave them be.
  5860. */
  5861. if (dp->dl_time != 0)
  5862. continue;
  5863. atomic_inc(&clp->cl_refcount);
  5864. WARN_ON(!unhash_delegation_locked(dp));
  5865. list_add(&dp->dl_recall_lru, victims);
  5866. }
  5867. ++count;
  5868. /*
  5869. * Despite the fact that these functions deal with
  5870. * 64-bit integers for "count", we must ensure that
  5871. * it doesn't blow up the clp->cl_refcount. Throw a
  5872. * warning if we start to approach INT_MAX here.
  5873. */
  5874. WARN_ON_ONCE(count == (INT_MAX / 2));
  5875. if (count == max)
  5876. break;
  5877. }
  5878. spin_unlock(&state_lock);
  5879. return count;
  5880. }
  5881. static u64
  5882. nfsd_print_client_delegations(struct nfs4_client *clp)
  5883. {
  5884. u64 count = nfsd_find_all_delegations(clp, 0, NULL);
  5885. nfsd_print_count(clp, count, "delegations");
  5886. return count;
  5887. }
  5888. u64
  5889. nfsd_inject_print_delegations(void)
  5890. {
  5891. struct nfs4_client *clp;
  5892. u64 count = 0;
  5893. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5894. nfsd_net_id);
  5895. if (!nfsd_netns_ready(nn))
  5896. return 0;
  5897. spin_lock(&nn->client_lock);
  5898. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5899. count += nfsd_print_client_delegations(clp);
  5900. spin_unlock(&nn->client_lock);
  5901. return count;
  5902. }
  5903. static void
  5904. nfsd_forget_delegations(struct list_head *reaplist)
  5905. {
  5906. struct nfs4_client *clp;
  5907. struct nfs4_delegation *dp, *next;
  5908. list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
  5909. list_del_init(&dp->dl_recall_lru);
  5910. clp = dp->dl_stid.sc_client;
  5911. revoke_delegation(dp);
  5912. put_client(clp);
  5913. }
  5914. }
  5915. u64
  5916. nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
  5917. size_t addr_size)
  5918. {
  5919. u64 count = 0;
  5920. struct nfs4_client *clp;
  5921. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5922. nfsd_net_id);
  5923. LIST_HEAD(reaplist);
  5924. if (!nfsd_netns_ready(nn))
  5925. return count;
  5926. spin_lock(&nn->client_lock);
  5927. clp = nfsd_find_client(addr, addr_size);
  5928. if (clp)
  5929. count = nfsd_find_all_delegations(clp, 0, &reaplist);
  5930. spin_unlock(&nn->client_lock);
  5931. nfsd_forget_delegations(&reaplist);
  5932. return count;
  5933. }
  5934. u64
  5935. nfsd_inject_forget_delegations(u64 max)
  5936. {
  5937. u64 count = 0;
  5938. struct nfs4_client *clp;
  5939. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5940. nfsd_net_id);
  5941. LIST_HEAD(reaplist);
  5942. if (!nfsd_netns_ready(nn))
  5943. return count;
  5944. spin_lock(&nn->client_lock);
  5945. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5946. count += nfsd_find_all_delegations(clp, max - count, &reaplist);
  5947. if (max != 0 && count >= max)
  5948. break;
  5949. }
  5950. spin_unlock(&nn->client_lock);
  5951. nfsd_forget_delegations(&reaplist);
  5952. return count;
  5953. }
  5954. static void
  5955. nfsd_recall_delegations(struct list_head *reaplist)
  5956. {
  5957. struct nfs4_client *clp;
  5958. struct nfs4_delegation *dp, *next;
  5959. list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
  5960. list_del_init(&dp->dl_recall_lru);
  5961. clp = dp->dl_stid.sc_client;
  5962. /*
  5963. * We skipped all entries that had a zero dl_time before,
  5964. * so we can now reset the dl_time back to 0. If a delegation
  5965. * break comes in now, then it won't make any difference since
  5966. * we're recalling it either way.
  5967. */
  5968. spin_lock(&state_lock);
  5969. dp->dl_time = 0;
  5970. spin_unlock(&state_lock);
  5971. nfsd_break_one_deleg(dp);
  5972. put_client(clp);
  5973. }
  5974. }
  5975. u64
  5976. nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
  5977. size_t addr_size)
  5978. {
  5979. u64 count = 0;
  5980. struct nfs4_client *clp;
  5981. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5982. nfsd_net_id);
  5983. LIST_HEAD(reaplist);
  5984. if (!nfsd_netns_ready(nn))
  5985. return count;
  5986. spin_lock(&nn->client_lock);
  5987. clp = nfsd_find_client(addr, addr_size);
  5988. if (clp)
  5989. count = nfsd_find_all_delegations(clp, 0, &reaplist);
  5990. spin_unlock(&nn->client_lock);
  5991. nfsd_recall_delegations(&reaplist);
  5992. return count;
  5993. }
  5994. u64
  5995. nfsd_inject_recall_delegations(u64 max)
  5996. {
  5997. u64 count = 0;
  5998. struct nfs4_client *clp, *next;
  5999. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  6000. nfsd_net_id);
  6001. LIST_HEAD(reaplist);
  6002. if (!nfsd_netns_ready(nn))
  6003. return count;
  6004. spin_lock(&nn->client_lock);
  6005. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  6006. count += nfsd_find_all_delegations(clp, max - count, &reaplist);
  6007. if (max != 0 && ++count >= max)
  6008. break;
  6009. }
  6010. spin_unlock(&nn->client_lock);
  6011. nfsd_recall_delegations(&reaplist);
  6012. return count;
  6013. }
  6014. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  6015. /*
  6016. * Since the lifetime of a delegation isn't limited to that of an open, a
  6017. * client may quite reasonably hang on to a delegation as long as it has
  6018. * the inode cached. This becomes an obvious problem the first time a
  6019. * client's inode cache approaches the size of the server's total memory.
  6020. *
  6021. * For now we avoid this problem by imposing a hard limit on the number
  6022. * of delegations, which varies according to the server's memory size.
  6023. */
  6024. static void
  6025. set_max_delegations(void)
  6026. {
  6027. /*
  6028. * Allow at most 4 delegations per megabyte of RAM. Quick
  6029. * estimates suggest that in the worst case (where every delegation
  6030. * is for a different inode), a delegation could take about 1.5K,
  6031. * giving a worst case usage of about 6% of memory.
  6032. */
  6033. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  6034. }
  6035. static int nfs4_state_create_net(struct net *net)
  6036. {
  6037. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  6038. int i;
  6039. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  6040. CLIENT_HASH_SIZE, GFP_KERNEL);
  6041. if (!nn->conf_id_hashtbl)
  6042. goto err;
  6043. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  6044. CLIENT_HASH_SIZE, GFP_KERNEL);
  6045. if (!nn->unconf_id_hashtbl)
  6046. goto err_unconf_id;
  6047. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  6048. SESSION_HASH_SIZE, GFP_KERNEL);
  6049. if (!nn->sessionid_hashtbl)
  6050. goto err_sessionid;
  6051. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  6052. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  6053. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  6054. }
  6055. for (i = 0; i < SESSION_HASH_SIZE; i++)
  6056. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  6057. nn->conf_name_tree = RB_ROOT;
  6058. nn->unconf_name_tree = RB_ROOT;
  6059. INIT_LIST_HEAD(&nn->client_lru);
  6060. INIT_LIST_HEAD(&nn->close_lru);
  6061. INIT_LIST_HEAD(&nn->del_recall_lru);
  6062. spin_lock_init(&nn->client_lock);
  6063. spin_lock_init(&nn->blocked_locks_lock);
  6064. INIT_LIST_HEAD(&nn->blocked_locks_lru);
  6065. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  6066. get_net(net);
  6067. return 0;
  6068. err_sessionid:
  6069. kfree(nn->unconf_id_hashtbl);
  6070. err_unconf_id:
  6071. kfree(nn->conf_id_hashtbl);
  6072. err:
  6073. return -ENOMEM;
  6074. }
  6075. static void
  6076. nfs4_state_destroy_net(struct net *net)
  6077. {
  6078. int i;
  6079. struct nfs4_client *clp = NULL;
  6080. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  6081. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  6082. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  6083. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  6084. destroy_client(clp);
  6085. }
  6086. }
  6087. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  6088. while (!list_empty(&nn->unconf_id_hashtbl[i])) {
  6089. clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  6090. destroy_client(clp);
  6091. }
  6092. }
  6093. kfree(nn->sessionid_hashtbl);
  6094. kfree(nn->unconf_id_hashtbl);
  6095. kfree(nn->conf_id_hashtbl);
  6096. put_net(net);
  6097. }
  6098. int
  6099. nfs4_state_start_net(struct net *net)
  6100. {
  6101. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  6102. int ret;
  6103. ret = nfs4_state_create_net(net);
  6104. if (ret)
  6105. return ret;
  6106. nn->boot_time = get_seconds();
  6107. nn->grace_ended = false;
  6108. nn->nfsd4_manager.block_opens = true;
  6109. locks_start_grace(net, &nn->nfsd4_manager);
  6110. nfsd4_client_tracking_init(net);
  6111. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  6112. nn->nfsd4_grace, net);
  6113. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  6114. return 0;
  6115. }
  6116. /* initialization to perform when the nfsd service is started: */
  6117. int
  6118. nfs4_state_start(void)
  6119. {
  6120. int ret;
  6121. ret = set_callback_cred();
  6122. if (ret)
  6123. return ret;
  6124. laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
  6125. if (laundry_wq == NULL) {
  6126. ret = -ENOMEM;
  6127. goto out_cleanup_cred;
  6128. }
  6129. ret = nfsd4_create_callback_queue();
  6130. if (ret)
  6131. goto out_free_laundry;
  6132. set_max_delegations();
  6133. return 0;
  6134. out_free_laundry:
  6135. destroy_workqueue(laundry_wq);
  6136. out_cleanup_cred:
  6137. cleanup_callback_cred();
  6138. return ret;
  6139. }
  6140. void
  6141. nfs4_state_shutdown_net(struct net *net)
  6142. {
  6143. struct nfs4_delegation *dp = NULL;
  6144. struct list_head *pos, *next, reaplist;
  6145. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  6146. struct nfsd4_blocked_lock *nbl;
  6147. cancel_delayed_work_sync(&nn->laundromat_work);
  6148. locks_end_grace(&nn->nfsd4_manager);
  6149. INIT_LIST_HEAD(&reaplist);
  6150. spin_lock(&state_lock);
  6151. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  6152. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  6153. WARN_ON(!unhash_delegation_locked(dp));
  6154. list_add(&dp->dl_recall_lru, &reaplist);
  6155. }
  6156. spin_unlock(&state_lock);
  6157. list_for_each_safe(pos, next, &reaplist) {
  6158. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  6159. list_del_init(&dp->dl_recall_lru);
  6160. put_clnt_odstate(dp->dl_clnt_odstate);
  6161. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  6162. nfs4_put_stid(&dp->dl_stid);
  6163. }
  6164. BUG_ON(!list_empty(&reaplist));
  6165. spin_lock(&nn->blocked_locks_lock);
  6166. while (!list_empty(&nn->blocked_locks_lru)) {
  6167. nbl = list_first_entry(&nn->blocked_locks_lru,
  6168. struct nfsd4_blocked_lock, nbl_lru);
  6169. list_move(&nbl->nbl_lru, &reaplist);
  6170. list_del_init(&nbl->nbl_list);
  6171. }
  6172. spin_unlock(&nn->blocked_locks_lock);
  6173. while (!list_empty(&reaplist)) {
  6174. nbl = list_first_entry(&nn->blocked_locks_lru,
  6175. struct nfsd4_blocked_lock, nbl_lru);
  6176. list_del_init(&nbl->nbl_lru);
  6177. posix_unblock_lock(&nbl->nbl_lock);
  6178. free_blocked_lock(nbl);
  6179. }
  6180. nfsd4_client_tracking_exit(net);
  6181. nfs4_state_destroy_net(net);
  6182. }
  6183. void
  6184. nfs4_state_shutdown(void)
  6185. {
  6186. destroy_workqueue(laundry_wq);
  6187. nfsd4_destroy_callback_queue();
  6188. cleanup_callback_cred();
  6189. }
  6190. static void
  6191. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  6192. {
  6193. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  6194. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  6195. }
  6196. static void
  6197. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  6198. {
  6199. if (cstate->minorversion) {
  6200. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  6201. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  6202. }
  6203. }
  6204. void
  6205. clear_current_stateid(struct nfsd4_compound_state *cstate)
  6206. {
  6207. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  6208. }
  6209. /*
  6210. * functions to set current state id
  6211. */
  6212. void
  6213. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  6214. {
  6215. put_stateid(cstate, &odp->od_stateid);
  6216. }
  6217. void
  6218. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  6219. {
  6220. put_stateid(cstate, &open->op_stateid);
  6221. }
  6222. void
  6223. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  6224. {
  6225. put_stateid(cstate, &close->cl_stateid);
  6226. }
  6227. void
  6228. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  6229. {
  6230. put_stateid(cstate, &lock->lk_resp_stateid);
  6231. }
  6232. /*
  6233. * functions to consume current state id
  6234. */
  6235. void
  6236. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  6237. {
  6238. get_stateid(cstate, &odp->od_stateid);
  6239. }
  6240. void
  6241. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  6242. {
  6243. get_stateid(cstate, &drp->dr_stateid);
  6244. }
  6245. void
  6246. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  6247. {
  6248. get_stateid(cstate, &fsp->fr_stateid);
  6249. }
  6250. void
  6251. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  6252. {
  6253. get_stateid(cstate, &setattr->sa_stateid);
  6254. }
  6255. void
  6256. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  6257. {
  6258. get_stateid(cstate, &close->cl_stateid);
  6259. }
  6260. void
  6261. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  6262. {
  6263. get_stateid(cstate, &locku->lu_stateid);
  6264. }
  6265. void
  6266. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  6267. {
  6268. get_stateid(cstate, &read->rd_stateid);
  6269. }
  6270. void
  6271. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  6272. {
  6273. get_stateid(cstate, &write->wr_stateid);
  6274. }