nfs4state.c 168 KB

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