nfs4state.c 182 KB

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