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