nfs4state.c 187 KB

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