nfs4state.c 188 KB

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